mciqtz32: Remove all remaining tabs.
[wine] / dlls / msvidc32 / msvideo1.c
1 /*
2  * Microsoft Video-1 Decoder
3  * Copyright (C) 2003 the ffmpeg project
4  *
5  * Portions Copyright (C) 2004 Mike McCormack for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22
23 /**
24  * @file msvideo1.c
25  * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
26  * For more information about the MS Video-1 format, visit:
27  *   http://www.pcisys.net/~melanson/codecs/
28  *
29  * This decoder outputs either PAL8 or RGB555 data, depending on the
30  * whether a RGB palette was passed through palctrl;
31  * if it's present, then the data is PAL8; RGB555 otherwise.
32  */
33
34 #include <stdarg.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winuser.h" 
39 #include "commdlg.h"
40 #include "vfw.h"
41 #include "mmsystem.h"
42 #include "msvidc32_private.h"
43  
44 #include "wine/debug.h"
45  
46 WINE_DEFAULT_DEBUG_CHANNEL(msvidc32); 
47
48 static HINSTANCE MSVIDC32_hModule;
49
50 #define CRAM_MAGIC mmioFOURCC('C', 'R', 'A', 'M')
51 #define MSVC_MAGIC mmioFOURCC('M', 'S', 'V', 'C')
52 #define WHAM_MAGIC mmioFOURCC('W', 'H', 'A', 'M')
53 #define compare_fourcc(fcc1, fcc2) (((fcc1)^(fcc2))&~0x20202020)
54
55 #define PALETTE_COUNT 256
56 #define LE_16(x)  ((((const uint8_t *)(x))[1] << 8) | ((const uint8_t *)(x))[0])
57
58 /* FIXME - check the stream size */
59 #define CHECK_STREAM_PTR(n) \
60   if ((stream_ptr + n) > buf_size ) { \
61     WARN("stream_ptr out of bounds (%d >= %d)\n", \
62       stream_ptr + n, buf_size); \
63     return; \
64   }
65
66 typedef BYTE uint8_t;
67
68 typedef struct Msvideo1Context {
69     DWORD dwMagic;
70     int mode_8bit;  /* if it's not 8-bit, it's 16-bit */
71 } Msvideo1Context;
72
73 static void 
74 msvideo1_decode_8bit( int width, int height, const unsigned char *buf, int buf_size,
75                       unsigned char *pixels, int stride)
76 {
77     int block_ptr, pixel_ptr;
78     int total_blocks;
79     int pixel_x, pixel_y;  /* pixel width and height iterators */
80     int block_x, block_y;  /* block width and height iterators */
81     int blocks_wide, blocks_high;  /* width and height in 4x4 blocks */
82     int block_inc;
83     int row_dec;
84
85     /* decoding parameters */
86     int stream_ptr;
87     unsigned char byte_a, byte_b;
88     unsigned short flags;
89     int skip_blocks;
90     unsigned char colors[8];
91
92     stream_ptr = 0;
93     skip_blocks = 0;
94     blocks_wide = width / 4;
95     blocks_high = height / 4;
96     total_blocks = blocks_wide * blocks_high;
97     block_inc = 4;
98     row_dec = stride + 4;
99
100     for (block_y = blocks_high; block_y > 0; block_y--) {
101         block_ptr = ((block_y * 4) - 1) * stride;
102         for (block_x = blocks_wide; block_x > 0; block_x--) {
103             /* check if this block should be skipped */
104             if (skip_blocks) {
105                 block_ptr += block_inc;
106                 skip_blocks--;
107                 total_blocks--;
108                 continue;
109             }
110
111             pixel_ptr = block_ptr;
112
113             /* get the next two bytes in the encoded data stream */
114             CHECK_STREAM_PTR(2);
115             byte_a = buf[stream_ptr++];
116             byte_b = buf[stream_ptr++];
117
118             /* check if the decode is finished */
119             if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
120                 return;
121             else if ((byte_b & 0xFC) == 0x84) {
122                 /* skip code, but don't count the current block */
123                 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
124             } else if (byte_b < 0x80) {
125                 /* 2-color encoding */
126                 flags = (byte_b << 8) | byte_a;
127
128                 CHECK_STREAM_PTR(2);
129                 colors[0] = buf[stream_ptr++];
130                 colors[1] = buf[stream_ptr++];
131
132                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
133                     for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
134                     {
135 #if ORIGINAL
136                         pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
137 #else
138                         pixels[width*(height-(pixel_ptr/width)-1) + 
139                                pixel_ptr%width] = 
140                                colors[(flags & 0x1) ^ 1];
141                         pixel_ptr++;
142 #endif
143                     }
144                     pixel_ptr -= row_dec;
145                 }
146             } else if (byte_b >= 0x90) {
147                 /* 8-color encoding */
148                 flags = (byte_b << 8) | byte_a;
149
150                 CHECK_STREAM_PTR(8);
151                 memcpy(colors, &buf[stream_ptr], 8);
152                 stream_ptr += 8;
153
154                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
155                     for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
156                     {
157 #if ORIGINAL
158                         pixels[pixel_ptr++] = 
159                             colors[((pixel_y & 0x2) << 1) + 
160                                 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
161 #else
162                         pixels[width*(height-(pixel_ptr/width)-1) + 
163                                pixel_ptr%width] = 
164                             colors[((pixel_y & 0x2) << 1) + 
165                                 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
166                         pixel_ptr++;
167 #endif
168                     }
169                     pixel_ptr -= row_dec;
170                 }
171             } else {
172                 /* 1-color encoding */
173                 colors[0] = byte_a;
174
175                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
176                     for (pixel_x = 0; pixel_x < 4; pixel_x++)
177                     {
178 #if ORIGINAL
179                         pixels[pixel_ptr++] = colors[0];
180 #else
181                         pixels[width*(height-(pixel_ptr/width)-1) + 
182                                pixel_ptr%width] = colors[0];
183                         pixel_ptr++;
184 #endif
185                     }
186                     pixel_ptr -= row_dec;
187                 }
188             }
189
190             block_ptr += block_inc;
191             total_blocks--;
192         }
193     }
194 }
195
196 static void
197 msvideo1_decode_16bit( int width, int height, const unsigned char *buf, int buf_size,
198                        unsigned short *pixels, int stride)
199 {
200     int block_ptr, pixel_ptr;
201     int total_blocks;
202     int pixel_x, pixel_y;  /* pixel width and height iterators */
203     int block_x, block_y;  /* block width and height iterators */
204     int blocks_wide, blocks_high;  /* width and height in 4x4 blocks */
205     int block_inc;
206     int row_dec;
207
208     /* decoding parameters */
209     int stream_ptr;
210     unsigned char byte_a, byte_b;
211     unsigned short flags;
212     int skip_blocks;
213     unsigned short colors[8];
214
215     stream_ptr = 0;
216     skip_blocks = 0;
217     blocks_wide = width / 4;
218     blocks_high = height / 4;
219     total_blocks = blocks_wide * blocks_high;
220     block_inc = 4;
221     row_dec = stride + 4;
222
223     for (block_y = blocks_high; block_y > 0; block_y--) {
224         block_ptr = ((block_y * 4) - 1) * stride;
225         for (block_x = blocks_wide; block_x > 0; block_x--) {
226             /* check if this block should be skipped */
227             if (skip_blocks) {
228                 block_ptr += block_inc;
229                 skip_blocks--;
230                 total_blocks--;
231                 continue;
232             }
233
234             pixel_ptr = block_ptr;
235
236             /* get the next two bytes in the encoded data stream */
237             CHECK_STREAM_PTR(2);
238             byte_a = buf[stream_ptr++];
239             byte_b = buf[stream_ptr++];
240
241             /* check if the decode is finished */
242             if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
243                 return;
244             } else if ((byte_b & 0xFC) == 0x84) {
245                 /* skip code, but don't count the current block */
246                 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
247             } else if (byte_b < 0x80) {
248                 /* 2- or 8-color encoding modes */
249                 flags = (byte_b << 8) | byte_a;
250
251                 CHECK_STREAM_PTR(4);
252                 colors[0] = LE_16(&buf[stream_ptr]);
253                 stream_ptr += 2;
254                 colors[1] = LE_16(&buf[stream_ptr]);
255                 stream_ptr += 2;
256
257                 if (colors[0] & 0x8000) {
258                     /* 8-color encoding */
259                     CHECK_STREAM_PTR(12);
260                     colors[2] = LE_16(&buf[stream_ptr]);
261                     stream_ptr += 2;
262                     colors[3] = LE_16(&buf[stream_ptr]);
263                     stream_ptr += 2;
264                     colors[4] = LE_16(&buf[stream_ptr]);
265                     stream_ptr += 2;
266                     colors[5] = LE_16(&buf[stream_ptr]);
267                     stream_ptr += 2;
268                     colors[6] = LE_16(&buf[stream_ptr]);
269                     stream_ptr += 2;
270                     colors[7] = LE_16(&buf[stream_ptr]);
271                     stream_ptr += 2;
272
273                     for (pixel_y = 0; pixel_y < 4; pixel_y++) {
274                         for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
275                             pixels[pixel_ptr++] = 
276                                 colors[((pixel_y & 0x2) << 1) + 
277                                     (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
278                         pixel_ptr -= row_dec;
279                     }
280                 } else {
281                     /* 2-color encoding */
282                     for (pixel_y = 0; pixel_y < 4; pixel_y++) {
283                         for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
284                             pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
285                         pixel_ptr -= row_dec;
286                     }
287                 }
288             } else {
289                 /* otherwise, it's a 1-color block */
290                 colors[0] = (byte_b << 8) | byte_a;
291
292                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
293                     for (pixel_x = 0; pixel_x < 4; pixel_x++)
294                         pixels[pixel_ptr++] = colors[0];
295                     pixel_ptr -= row_dec;
296                 }
297             }
298
299             block_ptr += block_inc;
300             total_blocks--;
301         }
302     }
303 }
304
305 static LRESULT
306 CRAM_DecompressQuery( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
307 {
308     TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info, in, out);
309
310     if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
311         return ICERR_BADPARAM;
312
313     TRACE("planes = %d\n", in->bmiHeader.biPlanes );
314     TRACE("bpp    = %d\n", in->bmiHeader.biBitCount );
315     TRACE("height = %d\n", in->bmiHeader.biHeight );
316     TRACE("width  = %d\n", in->bmiHeader.biWidth );
317     TRACE("compr  = %x\n", in->bmiHeader.biCompression );
318
319     if( ( in->bmiHeader.biCompression != CRAM_MAGIC ) &&
320         ( in->bmiHeader.biCompression != MSVC_MAGIC ) &&
321         ( in->bmiHeader.biCompression != WHAM_MAGIC ) )
322         return ICERR_UNSUPPORTED;
323
324     if( ( in->bmiHeader.biBitCount != 16 ) &&
325         ( in->bmiHeader.biBitCount != 8 ) )
326     {
327         TRACE("can't do %d bpp\n", in->bmiHeader.biBitCount );
328         return ICERR_UNSUPPORTED;
329     }
330
331     /* output must be same dimensions as input */
332     if( out )
333     {
334         if( in->bmiHeader.biBitCount != out->bmiHeader.biBitCount )
335             return ICERR_UNSUPPORTED;
336         if( in->bmiHeader.biPlanes != out->bmiHeader.biPlanes )
337             return ICERR_UNSUPPORTED;
338         if( in->bmiHeader.biHeight != out->bmiHeader.biHeight )
339             return ICERR_UNSUPPORTED;
340         if( in->bmiHeader.biWidth != out->bmiHeader.biWidth )
341             return ICERR_UNSUPPORTED;
342     }
343
344     TRACE("OK!\n");
345
346     return ICERR_OK;
347 }
348
349 static LRESULT 
350 CRAM_DecompressGetFormat( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
351 {
352     DWORD size;
353
354     TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info, in, out);
355
356     if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
357         return ICERR_BADPARAM;
358
359     size = in->bmiHeader.biSize;
360     if (in->bmiHeader.biBitCount <= 8)
361         size += in->bmiHeader.biClrUsed * sizeof(RGBQUAD);
362
363     if( out )
364     {
365         memcpy( out, in, size );
366         out->bmiHeader.biCompression = BI_RGB;
367         out->bmiHeader.biSizeImage = in->bmiHeader.biHeight
368                                    * in->bmiHeader.biWidth *4;
369         return ICERR_OK;
370     }
371
372     return size;
373 }
374
375 static LRESULT CRAM_DecompressBegin( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
376 {
377     TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info, in, out);
378
379     if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
380         return ICERR_BADPARAM;
381
382     TRACE("bitmap is %d bpp\n", in->bmiHeader.biBitCount);
383     if( in->bmiHeader.biBitCount == 8 )
384         info->mode_8bit = 1;
385     else if( in->bmiHeader.biBitCount == 16 )
386         info->mode_8bit = 0;
387     else
388     {
389         ERR("Bad output format\n");
390         return ICERR_BADPARAM;
391     }
392
393     return ICERR_OK;
394 }
395
396 static LRESULT CRAM_Decompress( Msvideo1Context *info, ICDECOMPRESS *icd, DWORD size )
397 {
398     LONG width, height, stride, sz;
399     WORD bit_per_pixel;
400
401     TRACE("ICM_DECOMPRESS %p %p %d\n", info, icd, size);
402
403     if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
404         return ICERR_BADPARAM;
405
406     /* FIXME: flags are ignored */
407
408     width  = icd->lpbiInput->biWidth;
409     height = icd->lpbiInput->biHeight;
410     bit_per_pixel = icd->lpbiInput->biBitCount;
411     stride = width*bit_per_pixel/8;
412     sz = icd->lpbiInput->biSizeImage;
413
414     if (info->mode_8bit)
415     {
416         msvideo1_decode_8bit( width, height, icd->lpInput, sz,
417                               icd->lpOutput, stride);
418     }
419     else
420     {
421         msvideo1_decode_16bit( width, height, icd->lpInput, sz,
422                                icd->lpOutput, stride);
423     }
424
425     return ICERR_OK;
426 }
427
428 static LRESULT CRAM_DecompressEx( Msvideo1Context *info, ICDECOMPRESSEX *icd, DWORD size )
429 {
430     LONG width, height, stride, sz;
431     WORD bit_per_pixel;
432
433     TRACE("ICM_DECOMPRESSEX %p %p %d\n", info, icd, size);
434
435     if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
436         return ICERR_BADPARAM;
437
438     /* FIXME: flags are ignored */
439
440     width  = icd->lpbiSrc->biWidth;
441     height = icd->lpbiSrc->biHeight;
442     bit_per_pixel = icd->lpbiSrc->biBitCount;
443     stride = width*bit_per_pixel/8;
444     sz = icd->lpbiSrc->biSizeImage;
445
446     if (info->mode_8bit)
447     {
448         msvideo1_decode_8bit( width, height, icd->lpSrc, sz, 
449                              icd->lpDst, stride);
450     }
451     else
452     {
453         msvideo1_decode_16bit( width, height, icd->lpSrc, sz,
454                               icd->lpDst, stride);
455     }
456
457     return ICERR_OK;
458 }
459
460 static LRESULT CRAM_GetInfo( const Msvideo1Context *info, ICINFO *icinfo, DWORD dwSize )
461 {
462     if (!icinfo) return sizeof(ICINFO);
463     if (dwSize < sizeof(ICINFO)) return 0;
464
465     icinfo->dwSize = sizeof(ICINFO);
466     icinfo->fccType = ICTYPE_VIDEO;
467     icinfo->fccHandler = info ? info->dwMagic : CRAM_MAGIC;
468     icinfo->dwFlags = 0;
469     icinfo->dwVersion = ICVERSION;
470     icinfo->dwVersionICM = ICVERSION;
471
472     LoadStringW(MSVIDC32_hModule, IDS_NAME, icinfo->szName, sizeof(icinfo->szName)/sizeof(WCHAR));
473     LoadStringW(MSVIDC32_hModule, IDS_DESCRIPTION, icinfo->szDescription, sizeof(icinfo->szDescription)/sizeof(WCHAR));
474     /* msvfw32 will fill icinfo->szDriver for us */
475
476     return sizeof(ICINFO);
477 }
478
479 /***********************************************************************
480  *              DriverProc (MSVIDC32.@)
481  */
482 LRESULT WINAPI CRAM_DriverProc( DWORD_PTR dwDriverId, HDRVR hdrvr, UINT msg,
483                                 LPARAM lParam1, LPARAM lParam2 )
484 {
485     Msvideo1Context *info = (Msvideo1Context *) dwDriverId;
486     LRESULT r = ICERR_UNSUPPORTED;
487
488     TRACE("%ld %p %04x %08lx %08lx\n", dwDriverId, hdrvr, msg, lParam1, lParam2);
489
490     switch( msg )
491     {
492     case DRV_LOAD:
493         TRACE("Loaded\n");
494         r = 1;
495         break;
496
497     case DRV_ENABLE:
498         break;
499
500     case DRV_OPEN:
501     {
502         ICINFO *icinfo = (ICINFO *)lParam2;
503
504         TRACE("Opened\n");
505
506         if (icinfo && compare_fourcc(icinfo->fccType, ICTYPE_VIDEO)) return 0;
507
508         info = HeapAlloc( GetProcessHeap(), 0, sizeof (Msvideo1Context) );
509         if( info )
510         {
511             memset( info, 0, sizeof info );
512             info->dwMagic = CRAM_MAGIC;
513         }
514         r = (LRESULT) info;
515         break;
516     }
517
518     case DRV_CLOSE:
519         HeapFree( GetProcessHeap(), 0, info );
520         break;
521
522     case DRV_DISABLE:
523         break;
524
525     case DRV_FREE:
526         break;
527
528     case ICM_GETINFO:
529         r = CRAM_GetInfo( info, (ICINFO *)lParam1, (DWORD)lParam2 );
530         break;
531
532     case ICM_DECOMPRESS_QUERY:
533         r = CRAM_DecompressQuery( info, (LPBITMAPINFO) lParam1,
534                                        (LPBITMAPINFO) lParam2 );
535         break;
536
537     case ICM_DECOMPRESS_GET_FORMAT:
538         r = CRAM_DecompressGetFormat( info, (LPBITMAPINFO) lParam1,
539                                        (LPBITMAPINFO) lParam2 );
540         break;
541
542     case ICM_DECOMPRESS_GET_PALETTE:
543         FIXME("ICM_DECOMPRESS_GET_PALETTE\n");
544         break;
545
546     case ICM_DECOMPRESSEX_QUERY:
547         FIXME("ICM_DECOMPRESSEX_QUERY\n");
548         break;
549
550     case ICM_DECOMPRESS:
551         r = CRAM_Decompress( info, (ICDECOMPRESS*) lParam1,
552                                   (DWORD) lParam2 );
553         break;
554
555     case ICM_DECOMPRESS_BEGIN:
556         r = CRAM_DecompressBegin( info, (LPBITMAPINFO) lParam1,
557                                        (LPBITMAPINFO) lParam2 );
558         break;
559
560     case ICM_DECOMPRESSEX:
561         r = CRAM_DecompressEx( info, (ICDECOMPRESSEX*) lParam1,
562                                   (DWORD) lParam2 );
563         break;
564
565     case ICM_COMPRESS_QUERY:
566         FIXME("compression not implemented\n");
567         r = ICERR_BADFORMAT;
568         break;
569
570     case ICM_CONFIGURE:
571         r = ICERR_UNSUPPORTED;
572         break;
573
574     default:
575         FIXME("Unknown message: %04x %ld %ld\n", msg, lParam1, lParam2);
576     }
577
578     return r;
579 }
580
581 /***********************************************************************
582  *              DllMain
583  */
584 BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
585 {
586     TRACE("(%p,%d,%p)\n", hModule, dwReason, lpReserved);
587
588     switch (dwReason)
589     {
590     case DLL_PROCESS_ATTACH:
591         DisableThreadLibraryCalls(hModule);
592         MSVIDC32_hModule = hModule;
593         break;
594
595     case DLL_PROCESS_DETACH:
596         break;
597     }
598     return TRUE;
599 }