- Sync msimtf, msvfw32, msvidc32, msxml3 to Wine-1.1.43.
[reactos.git] / reactos / dll / win32 / 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 #ifdef ORIGINAL
99 row_dec = stride + 4;
100 #else
101 row_dec = - (stride - 4); /* such that -row_dec > 0 */
102 #endif
103
104 for (block_y = blocks_high; block_y > 0; block_y--) {
105 #ifdef ORIGINAL
106 block_ptr = ((block_y * 4) - 1) * stride;
107 #else
108 block_ptr = ((blocks_high - block_y) * 4) * stride;
109 #endif
110 for (block_x = blocks_wide; block_x > 0; block_x--) {
111 /* check if this block should be skipped */
112 if (skip_blocks) {
113 block_ptr += block_inc;
114 skip_blocks--;
115 total_blocks--;
116 continue;
117 }
118
119 pixel_ptr = block_ptr;
120
121 /* get the next two bytes in the encoded data stream */
122 CHECK_STREAM_PTR(2);
123 byte_a = buf[stream_ptr++];
124 byte_b = buf[stream_ptr++];
125
126 /* check if the decode is finished */
127 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
128 return;
129 else if ((byte_b & 0xFC) == 0x84) {
130 /* skip code, but don't count the current block */
131 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
132 } else if (byte_b < 0x80) {
133 /* 2-color encoding */
134 flags = (byte_b << 8) | byte_a;
135
136 CHECK_STREAM_PTR(2);
137 colors[0] = buf[stream_ptr++];
138 colors[1] = buf[stream_ptr++];
139
140 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
141 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
142 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
143 pixel_ptr -= row_dec;
144 }
145 } else if (byte_b >= 0x90) {
146 /* 8-color encoding */
147 flags = (byte_b << 8) | byte_a;
148
149 CHECK_STREAM_PTR(8);
150 memcpy(colors, &buf[stream_ptr], 8);
151 stream_ptr += 8;
152
153 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
154 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
155 pixels[pixel_ptr++] =
156 colors[((pixel_y & 0x2) << 1) +
157 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
158 pixel_ptr -= row_dec;
159 }
160 } else {
161 /* 1-color encoding */
162 colors[0] = byte_a;
163
164 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
165 for (pixel_x = 0; pixel_x < 4; pixel_x++)
166 pixels[pixel_ptr++] = colors[0];
167 pixel_ptr -= row_dec;
168 }
169 }
170
171 block_ptr += block_inc;
172 total_blocks--;
173 }
174 }
175 }
176
177 static void
178 msvideo1_decode_16bit( int width, int height, const unsigned char *buf, int buf_size,
179 unsigned short *pixels, int stride)
180 {
181 int block_ptr, pixel_ptr;
182 int total_blocks;
183 int pixel_x, pixel_y; /* pixel width and height iterators */
184 int block_x, block_y; /* block width and height iterators */
185 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
186 int block_inc;
187 int row_dec;
188
189 /* decoding parameters */
190 int stream_ptr;
191 unsigned char byte_a, byte_b;
192 unsigned short flags;
193 int skip_blocks;
194 unsigned short colors[8];
195
196 stream_ptr = 0;
197 skip_blocks = 0;
198 blocks_wide = width / 4;
199 blocks_high = height / 4;
200 total_blocks = blocks_wide * blocks_high;
201 block_inc = 4;
202 #ifdef ORIGINAL
203 row_dec = stride + 4;
204 #else
205 row_dec = - (stride - 4); /* such that -row_dec > 0 */
206 #endif
207
208 for (block_y = blocks_high; block_y > 0; block_y--) {
209 #ifdef ORIGINAL
210 block_ptr = ((block_y * 4) - 1) * stride;
211 #else
212 block_ptr = ((blocks_high - block_y) * 4) * stride;
213 #endif
214 for (block_x = blocks_wide; block_x > 0; block_x--) {
215 /* check if this block should be skipped */
216 if (skip_blocks) {
217 block_ptr += block_inc;
218 skip_blocks--;
219 total_blocks--;
220 continue;
221 }
222
223 pixel_ptr = block_ptr;
224
225 /* get the next two bytes in the encoded data stream */
226 CHECK_STREAM_PTR(2);
227 byte_a = buf[stream_ptr++];
228 byte_b = buf[stream_ptr++];
229
230 /* check if the decode is finished */
231 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
232 return;
233 } else if ((byte_b & 0xFC) == 0x84) {
234 /* skip code, but don't count the current block */
235 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
236 } else if (byte_b < 0x80) {
237 /* 2- or 8-color encoding modes */
238 flags = (byte_b << 8) | byte_a;
239
240 CHECK_STREAM_PTR(4);
241 colors[0] = LE_16(&buf[stream_ptr]);
242 stream_ptr += 2;
243 colors[1] = LE_16(&buf[stream_ptr]);
244 stream_ptr += 2;
245
246 if (colors[0] & 0x8000) {
247 /* 8-color encoding */
248 CHECK_STREAM_PTR(12);
249 colors[2] = LE_16(&buf[stream_ptr]);
250 stream_ptr += 2;
251 colors[3] = LE_16(&buf[stream_ptr]);
252 stream_ptr += 2;
253 colors[4] = LE_16(&buf[stream_ptr]);
254 stream_ptr += 2;
255 colors[5] = LE_16(&buf[stream_ptr]);
256 stream_ptr += 2;
257 colors[6] = LE_16(&buf[stream_ptr]);
258 stream_ptr += 2;
259 colors[7] = LE_16(&buf[stream_ptr]);
260 stream_ptr += 2;
261
262 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
263 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
264 pixels[pixel_ptr++] =
265 colors[((pixel_y & 0x2) << 1) +
266 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
267 pixel_ptr -= row_dec;
268 }
269 } else {
270 /* 2-color encoding */
271 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
272 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
273 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
274 pixel_ptr -= row_dec;
275 }
276 }
277 } else {
278 /* otherwise, it's a 1-color block */
279 colors[0] = (byte_b << 8) | byte_a;
280
281 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
282 for (pixel_x = 0; pixel_x < 4; pixel_x++)
283 pixels[pixel_ptr++] = colors[0];
284 pixel_ptr -= row_dec;
285 }
286 }
287
288 block_ptr += block_inc;
289 total_blocks--;
290 }
291 }
292 }
293
294 static LRESULT
295 CRAM_DecompressQuery( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
296 {
297 TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info, in, out);
298
299 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
300 return ICERR_BADPARAM;
301
302 TRACE("planes = %d\n", in->bmiHeader.biPlanes );
303 TRACE("bpp = %d\n", in->bmiHeader.biBitCount );
304 TRACE("height = %d\n", in->bmiHeader.biHeight );
305 TRACE("width = %d\n", in->bmiHeader.biWidth );
306 TRACE("compr = %x\n", in->bmiHeader.biCompression );
307
308 if( ( in->bmiHeader.biCompression != CRAM_MAGIC ) &&
309 ( in->bmiHeader.biCompression != MSVC_MAGIC ) &&
310 ( in->bmiHeader.biCompression != WHAM_MAGIC ) )
311 return ICERR_UNSUPPORTED;
312
313 if( ( in->bmiHeader.biBitCount != 16 ) &&
314 ( in->bmiHeader.biBitCount != 8 ) )
315 {
316 TRACE("can't do %d bpp\n", in->bmiHeader.biBitCount );
317 return ICERR_UNSUPPORTED;
318 }
319
320 /* output must be same dimensions as input */
321 if( out )
322 {
323 if( in->bmiHeader.biBitCount != out->bmiHeader.biBitCount )
324 return ICERR_UNSUPPORTED;
325 if( in->bmiHeader.biPlanes != out->bmiHeader.biPlanes )
326 return ICERR_UNSUPPORTED;
327 if( in->bmiHeader.biHeight != out->bmiHeader.biHeight )
328 return ICERR_UNSUPPORTED;
329 if( in->bmiHeader.biWidth != out->bmiHeader.biWidth )
330 return ICERR_UNSUPPORTED;
331 }
332
333 TRACE("OK!\n");
334
335 return ICERR_OK;
336 }
337
338 static LRESULT
339 CRAM_DecompressGetFormat( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
340 {
341 DWORD size;
342
343 TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info, in, out);
344
345 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
346 return ICERR_BADPARAM;
347
348 size = in->bmiHeader.biSize;
349 if (in->bmiHeader.biBitCount <= 8)
350 size += in->bmiHeader.biClrUsed * sizeof(RGBQUAD);
351
352 if( out )
353 {
354 memcpy( out, in, size );
355 out->bmiHeader.biCompression = BI_RGB;
356 out->bmiHeader.biSizeImage = in->bmiHeader.biHeight
357 * in->bmiHeader.biWidth *4;
358 return ICERR_OK;
359 }
360
361 return size;
362 }
363
364 static LRESULT CRAM_DecompressBegin( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
365 {
366 TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info, in, out);
367
368 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
369 return ICERR_BADPARAM;
370
371 TRACE("bitmap is %d bpp\n", in->bmiHeader.biBitCount);
372 if( in->bmiHeader.biBitCount == 8 )
373 info->mode_8bit = 1;
374 else if( in->bmiHeader.biBitCount == 16 )
375 info->mode_8bit = 0;
376 else
377 {
378 ERR("Bad output format\n");
379 return ICERR_BADPARAM;
380 }
381
382 return ICERR_OK;
383 }
384
385 static LRESULT CRAM_Decompress( Msvideo1Context *info, ICDECOMPRESS *icd, DWORD size )
386 {
387 LONG width, height, stride, sz;
388 WORD bit_per_pixel;
389
390 TRACE("ICM_DECOMPRESS %p %p %d\n", info, icd, size);
391
392 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
393 return ICERR_BADPARAM;
394
395 /* FIXME: flags are ignored */
396
397 width = icd->lpbiInput->biWidth;
398 height = icd->lpbiInput->biHeight;
399 bit_per_pixel = icd->lpbiInput->biBitCount;
400 stride = width; /* in bytes or 16bit words */
401 sz = icd->lpbiInput->biSizeImage;
402
403 if (info->mode_8bit)
404 {
405 msvideo1_decode_8bit( width, height, icd->lpInput, sz,
406 icd->lpOutput, stride);
407 }
408 else
409 {
410 msvideo1_decode_16bit( width, height, icd->lpInput, sz,
411 icd->lpOutput, stride);
412 }
413
414 return ICERR_OK;
415 }
416
417 static LRESULT CRAM_DecompressEx( Msvideo1Context *info, ICDECOMPRESSEX *icd, DWORD size )
418 {
419 LONG width, height, stride, sz;
420 WORD bit_per_pixel;
421
422 TRACE("ICM_DECOMPRESSEX %p %p %d\n", info, icd, size);
423
424 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
425 return ICERR_BADPARAM;
426
427 /* FIXME: flags are ignored */
428
429 width = icd->lpbiSrc->biWidth;
430 height = icd->lpbiSrc->biHeight;
431 bit_per_pixel = icd->lpbiSrc->biBitCount;
432 stride = width;
433 sz = icd->lpbiSrc->biSizeImage;
434
435 if (info->mode_8bit)
436 {
437 msvideo1_decode_8bit( width, height, icd->lpSrc, sz,
438 icd->lpDst, stride);
439 }
440 else
441 {
442 msvideo1_decode_16bit( width, height, icd->lpSrc, sz,
443 icd->lpDst, stride);
444 }
445
446 return ICERR_OK;
447 }
448
449 static LRESULT CRAM_GetInfo( const Msvideo1Context *info, ICINFO *icinfo, DWORD dwSize )
450 {
451 if (!icinfo) return sizeof(ICINFO);
452 if (dwSize < sizeof(ICINFO)) return 0;
453
454 icinfo->dwSize = sizeof(ICINFO);
455 icinfo->fccType = ICTYPE_VIDEO;
456 icinfo->fccHandler = info ? info->dwMagic : CRAM_MAGIC;
457 icinfo->dwFlags = 0;
458 icinfo->dwVersion = ICVERSION;
459 icinfo->dwVersionICM = ICVERSION;
460
461 LoadStringW(MSVIDC32_hModule, IDS_NAME, icinfo->szName, sizeof(icinfo->szName)/sizeof(WCHAR));
462 LoadStringW(MSVIDC32_hModule, IDS_DESCRIPTION, icinfo->szDescription, sizeof(icinfo->szDescription)/sizeof(WCHAR));
463 /* msvfw32 will fill icinfo->szDriver for us */
464
465 return sizeof(ICINFO);
466 }
467
468 /***********************************************************************
469 * DriverProc (MSVIDC32.@)
470 */
471 LRESULT WINAPI CRAM_DriverProc( DWORD_PTR dwDriverId, HDRVR hdrvr, UINT msg,
472 LPARAM lParam1, LPARAM lParam2 )
473 {
474 Msvideo1Context *info = (Msvideo1Context *) dwDriverId;
475 LRESULT r = ICERR_UNSUPPORTED;
476
477 TRACE("%ld %p %04x %08lx %08lx\n", dwDriverId, hdrvr, msg, lParam1, lParam2);
478
479 switch( msg )
480 {
481 case DRV_LOAD:
482 TRACE("Loaded\n");
483 r = 1;
484 break;
485
486 case DRV_ENABLE:
487 break;
488
489 case DRV_OPEN:
490 {
491 ICINFO *icinfo = (ICINFO *)lParam2;
492
493 TRACE("Opened\n");
494
495 if (icinfo && compare_fourcc(icinfo->fccType, ICTYPE_VIDEO)) return 0;
496
497 info = HeapAlloc( GetProcessHeap(), 0, sizeof (Msvideo1Context) );
498 if( info )
499 {
500 memset( info, 0, sizeof *info );
501 info->dwMagic = CRAM_MAGIC;
502 }
503 r = (LRESULT) info;
504 break;
505 }
506
507 case DRV_CLOSE:
508 HeapFree( GetProcessHeap(), 0, info );
509 break;
510
511 case DRV_DISABLE:
512 break;
513
514 case DRV_FREE:
515 break;
516
517 case ICM_GETINFO:
518 r = CRAM_GetInfo( info, (ICINFO *)lParam1, (DWORD)lParam2 );
519 break;
520
521 case ICM_DECOMPRESS_QUERY:
522 r = CRAM_DecompressQuery( info, (LPBITMAPINFO) lParam1,
523 (LPBITMAPINFO) lParam2 );
524 break;
525
526 case ICM_DECOMPRESS_GET_FORMAT:
527 r = CRAM_DecompressGetFormat( info, (LPBITMAPINFO) lParam1,
528 (LPBITMAPINFO) lParam2 );
529 break;
530
531 case ICM_DECOMPRESS_GET_PALETTE:
532 FIXME("ICM_DECOMPRESS_GET_PALETTE\n");
533 break;
534
535 case ICM_DECOMPRESSEX_QUERY:
536 FIXME("ICM_DECOMPRESSEX_QUERY\n");
537 break;
538
539 case ICM_DECOMPRESS:
540 r = CRAM_Decompress( info, (ICDECOMPRESS*) lParam1,
541 (DWORD) lParam2 );
542 break;
543
544 case ICM_DECOMPRESS_BEGIN:
545 r = CRAM_DecompressBegin( info, (LPBITMAPINFO) lParam1,
546 (LPBITMAPINFO) lParam2 );
547 break;
548
549 case ICM_DECOMPRESSEX:
550 r = CRAM_DecompressEx( info, (ICDECOMPRESSEX*) lParam1,
551 (DWORD) lParam2 );
552 break;
553
554 case ICM_DECOMPRESS_END:
555 r = ICERR_OK;
556 break;
557
558 case ICM_COMPRESS_QUERY:
559 FIXME("compression not implemented\n");
560 r = ICERR_BADFORMAT;
561 break;
562
563 case ICM_CONFIGURE:
564 r = ICERR_UNSUPPORTED;
565 break;
566
567 default:
568 FIXME("Unknown message: %04x %ld %ld\n", msg, lParam1, lParam2);
569 }
570
571 return r;
572 }
573
574 /***********************************************************************
575 * DllMain
576 */
577 BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
578 {
579 TRACE("(%p,%d,%p)\n", hModule, dwReason, lpReserved);
580
581 switch (dwReason)
582 {
583 case DLL_PROCESS_ATTACH:
584 DisableThreadLibraryCalls(hModule);
585 MSVIDC32_hModule = hModule;
586 break;
587
588 case DLL_PROCESS_DETACH:
589 break;
590 }
591 return TRUE;
592 }