2 * Microsoft Video-1 Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * Portions Copyright (C) 2004 Mike McCormack for CodeWeavers
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 of the License, or (at your option) any later version.
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.
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
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/
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.
42 #include "msvidc32_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msvidc32);
48 static HINSTANCE MSVIDC32_hModule;
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')
54 #define PALETTE_COUNT 256
55 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
57 /* FIXME - check the stream size */
58 #define CHECK_STREAM_PTR(n) \
59 if ((stream_ptr + n) > buf_size ) { \
60 WARN("stream_ptr out of bounds (%d >= %d)\n", \
61 stream_ptr + n, buf_size); \
67 typedef struct Msvideo1Context {
69 int mode_8bit; /* if it's not 8-bit, it's 16-bit */
73 msvideo1_decode_8bit( int width, int height, unsigned char *buf, int buf_size,
74 unsigned char *pixels, int stride)
76 int block_ptr, pixel_ptr;
78 int pixel_x, pixel_y; /* pixel width and height iterators */
79 int block_x, block_y; /* block width and height iterators */
80 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
84 /* decoding parameters */
86 unsigned char byte_a, byte_b;
89 unsigned char colors[8];
93 blocks_wide = width / 4;
94 blocks_high = height / 4;
95 total_blocks = blocks_wide * blocks_high;
99 for (block_y = blocks_high; block_y > 0; block_y--) {
100 block_ptr = ((block_y * 4) - 1) * stride;
101 for (block_x = blocks_wide; block_x > 0; block_x--) {
102 /* check if this block should be skipped */
104 block_ptr += block_inc;
110 pixel_ptr = block_ptr;
112 /* get the next two bytes in the encoded data stream */
114 byte_a = buf[stream_ptr++];
115 byte_b = buf[stream_ptr++];
117 /* check if the decode is finished */
118 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
120 else if ((byte_b & 0xFC) == 0x84) {
121 /* skip code, but don't count the current block */
122 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
123 } else if (byte_b < 0x80) {
124 /* 2-color encoding */
125 flags = (byte_b << 8) | byte_a;
128 colors[0] = buf[stream_ptr++];
129 colors[1] = buf[stream_ptr++];
131 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
132 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
135 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
137 pixels[width*(height-(pixel_ptr/width)-1) +
139 colors[(flags & 0x1) ^ 1];
143 pixel_ptr -= row_dec;
145 } else if (byte_b >= 0x90) {
146 /* 8-color encoding */
147 flags = (byte_b << 8) | byte_a;
150 memcpy(colors, &buf[stream_ptr], 8);
153 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
154 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
157 pixels[pixel_ptr++] =
158 colors[((pixel_y & 0x2) << 1) +
159 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
161 pixels[width*(height-(pixel_ptr/width)-1) +
163 colors[((pixel_y & 0x2) << 1) +
164 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
168 pixel_ptr -= row_dec;
171 /* 1-color encoding */
174 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
175 for (pixel_x = 0; pixel_x < 4; pixel_x++)
178 pixels[pixel_ptr++] = colors[0];
180 pixels[width*(height-(pixel_ptr/width)-1) +
181 pixel_ptr%width] = colors[0];
185 pixel_ptr -= row_dec;
189 block_ptr += block_inc;
196 msvideo1_decode_16bit( int width, int height, unsigned char *buf, int buf_size,
197 unsigned short *pixels, int stride)
199 int block_ptr, pixel_ptr;
201 int pixel_x, pixel_y; /* pixel width and height iterators */
202 int block_x, block_y; /* block width and height iterators */
203 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
207 /* decoding parameters */
209 unsigned char byte_a, byte_b;
210 unsigned short flags;
212 unsigned short colors[8];
216 blocks_wide = width / 4;
217 blocks_high = height / 4;
218 total_blocks = blocks_wide * blocks_high;
220 row_dec = stride + 4;
222 for (block_y = blocks_high; block_y > 0; block_y--) {
223 block_ptr = ((block_y * 4) - 1) * stride;
224 for (block_x = blocks_wide; block_x > 0; block_x--) {
225 /* check if this block should be skipped */
227 block_ptr += block_inc;
233 pixel_ptr = block_ptr;
235 /* get the next two bytes in the encoded data stream */
237 byte_a = buf[stream_ptr++];
238 byte_b = buf[stream_ptr++];
240 /* check if the decode is finished */
241 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
243 } else if ((byte_b & 0xFC) == 0x84) {
244 /* skip code, but don't count the current block */
245 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
246 } else if (byte_b < 0x80) {
247 /* 2- or 8-color encoding modes */
248 flags = (byte_b << 8) | byte_a;
251 colors[0] = LE_16(&buf[stream_ptr]);
253 colors[1] = LE_16(&buf[stream_ptr]);
256 if (colors[0] & 0x8000) {
257 /* 8-color encoding */
258 CHECK_STREAM_PTR(12);
259 colors[2] = LE_16(&buf[stream_ptr]);
261 colors[3] = LE_16(&buf[stream_ptr]);
263 colors[4] = LE_16(&buf[stream_ptr]);
265 colors[5] = LE_16(&buf[stream_ptr]);
267 colors[6] = LE_16(&buf[stream_ptr]);
269 colors[7] = LE_16(&buf[stream_ptr]);
272 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
273 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
274 pixels[pixel_ptr++] =
275 colors[((pixel_y & 0x2) << 1) +
276 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
277 pixel_ptr -= row_dec;
280 /* 2-color encoding */
281 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
282 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
283 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
284 pixel_ptr -= row_dec;
288 /* otherwise, it's a 1-color block */
289 colors[0] = (byte_b << 8) | byte_a;
291 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
292 for (pixel_x = 0; pixel_x < 4; pixel_x++)
293 pixels[pixel_ptr++] = colors[0];
294 pixel_ptr -= row_dec;
298 block_ptr += block_inc;
305 CRAM_DecompressQuery( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
307 TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info, in, out);
309 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
310 return ICERR_BADPARAM;
312 TRACE("planes = %d\n", in->bmiHeader.biPlanes );
313 TRACE("bpp = %d\n", in->bmiHeader.biBitCount );
314 TRACE("height = %ld\n", in->bmiHeader.biHeight );
315 TRACE("width = %ld\n", in->bmiHeader.biWidth );
316 TRACE("compr = %lx\n", in->bmiHeader.biCompression );
318 if( ( in->bmiHeader.biCompression != CRAM_MAGIC ) &&
319 ( in->bmiHeader.biCompression != MSVC_MAGIC ) &&
320 ( in->bmiHeader.biCompression != WHAM_MAGIC ) )
321 return ICERR_UNSUPPORTED;
323 if( ( in->bmiHeader.biBitCount != 16 ) &&
324 ( in->bmiHeader.biBitCount != 8 ) )
326 TRACE("can't do %d bpp\n", in->bmiHeader.biBitCount );
327 return ICERR_UNSUPPORTED;
330 /* output must be same dimensions as input */
333 if( in->bmiHeader.biBitCount != out->bmiHeader.biBitCount )
334 return ICERR_UNSUPPORTED;
335 if( in->bmiHeader.biPlanes != out->bmiHeader.biPlanes )
336 return ICERR_UNSUPPORTED;
337 if( in->bmiHeader.biHeight != out->bmiHeader.biHeight )
338 return ICERR_UNSUPPORTED;
339 if( in->bmiHeader.biWidth != out->bmiHeader.biWidth )
340 return ICERR_UNSUPPORTED;
349 CRAM_DecompressGetFormat( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
353 TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info, in, out);
355 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
356 return ICERR_BADPARAM;
358 size = in->bmiHeader.biSize;
359 if (in->bmiHeader.biBitCount <= 8)
360 size += in->bmiHeader.biClrUsed * sizeof(RGBQUAD);
364 memcpy( out, in, size );
365 out->bmiHeader.biCompression = BI_RGB;
366 out->bmiHeader.biSizeImage = in->bmiHeader.biHeight
367 * in->bmiHeader.biWidth *4;
374 static LRESULT CRAM_DecompressBegin( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
376 TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info, in, out);
378 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
379 return ICERR_BADPARAM;
381 TRACE("bitmap is %d bpp\n", in->bmiHeader.biBitCount);
382 if( in->bmiHeader.biBitCount == 8 )
384 else if( in->bmiHeader.biBitCount == 16 )
388 ERR("Bad output format\n");
389 return ICERR_BADPARAM;
395 static LRESULT CRAM_Decompress( Msvideo1Context *info, ICDECOMPRESS *icd, DWORD size )
397 LONG width, height, stride, sz;
400 TRACE("ICM_DECOMPRESS %p %p %ld\n", info, icd, size);
402 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
403 return ICERR_BADPARAM;
405 /* FIXME: flags are ignored */
407 width = icd->lpbiInput->biWidth;
408 height = icd->lpbiInput->biHeight;
409 bit_per_pixel = icd->lpbiInput->biBitCount;
410 stride = width*bit_per_pixel/8;
411 sz = icd->lpbiInput->biSizeImage;
415 msvideo1_decode_8bit( width, height, icd->lpInput, sz,
416 icd->lpOutput, stride);
420 msvideo1_decode_16bit( width, height, icd->lpInput, sz,
421 icd->lpOutput, stride);
427 static LRESULT CRAM_DecompressEx( Msvideo1Context *info, ICDECOMPRESSEX *icd, DWORD size )
429 LONG width, height, stride, sz;
432 TRACE("ICM_DECOMPRESSEX %p %p %ld\n", info, icd, size);
434 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
435 return ICERR_BADPARAM;
437 /* FIXME: flags are ignored */
439 width = icd->lpbiSrc->biWidth;
440 height = icd->lpbiSrc->biHeight;
441 bit_per_pixel = icd->lpbiSrc->biBitCount;
442 stride = width*bit_per_pixel/8;
443 sz = icd->lpbiSrc->biSizeImage;
447 msvideo1_decode_8bit( width, height, icd->lpSrc, sz,
452 msvideo1_decode_16bit( width, height, icd->lpSrc, sz,
459 static LRESULT CRAM_GetInfo( Msvideo1Context *info, ICINFO *icinfo, DWORD dwSize )
461 if (!icinfo) return sizeof(ICINFO);
462 if (dwSize < sizeof(ICINFO)) return 0;
464 icinfo->dwSize = sizeof(ICINFO);
465 icinfo->fccType = ICTYPE_VIDEO;
466 icinfo->fccHandler = info ? info->dwMagic : CRAM_MAGIC;
468 icinfo->dwVersion = ICVERSION;
469 icinfo->dwVersionICM = ICVERSION;
471 LoadStringW(MSVIDC32_hModule, IDS_NAME, icinfo->szName, sizeof(icinfo->szName)/sizeof(WCHAR));
472 LoadStringW(MSVIDC32_hModule, IDS_DESCRIPTION, icinfo->szDescription, sizeof(icinfo->szDescription)/sizeof(WCHAR));
473 /* msvfw32 will fill icinfo->szDriver for us */
475 return sizeof(ICINFO);
478 /***********************************************************************
479 * DriverProc (MSVIDC32.@)
481 LRESULT WINAPI CRAM_DriverProc( DWORD_PTR dwDriverId, HDRVR hdrvr, UINT msg,
482 LPARAM lParam1, LPARAM lParam2 )
484 Msvideo1Context *info = (Msvideo1Context *) dwDriverId;
485 LRESULT r = ICERR_UNSUPPORTED;
487 TRACE("%ld %p %04x %08lx %08lx\n", dwDriverId, hdrvr, msg, lParam1, lParam2);
501 ICINFO *icinfo = (ICINFO *)lParam2;
505 if (icinfo && icinfo->fccType != ICTYPE_VIDEO) return 0;
507 info = HeapAlloc( GetProcessHeap(), 0, sizeof (Msvideo1Context) );
510 memset( info, 0, sizeof info );
511 info->dwMagic = CRAM_MAGIC;
518 HeapFree( GetProcessHeap(), 0, info );
528 r = CRAM_GetInfo( info, (ICINFO *)lParam1, (DWORD)lParam2 );
531 case ICM_DECOMPRESS_QUERY:
532 r = CRAM_DecompressQuery( info, (LPBITMAPINFO) lParam1,
533 (LPBITMAPINFO) lParam2 );
536 case ICM_DECOMPRESS_GET_FORMAT:
537 r = CRAM_DecompressGetFormat( info, (LPBITMAPINFO) lParam1,
538 (LPBITMAPINFO) lParam2 );
541 case ICM_DECOMPRESS_GET_PALETTE:
542 FIXME("ICM_DECOMPRESS_GET_PALETTE\n");
545 case ICM_DECOMPRESSEX_QUERY:
546 FIXME("ICM_DECOMPRESSEX_QUERY\n");
550 r = CRAM_Decompress( info, (ICDECOMPRESS*) lParam1,
554 case ICM_DECOMPRESS_BEGIN:
555 r = CRAM_DecompressBegin( info, (LPBITMAPINFO) lParam1,
556 (LPBITMAPINFO) lParam2 );
559 case ICM_DECOMPRESSEX:
560 r = CRAM_DecompressEx( info, (ICDECOMPRESSEX*) lParam1,
564 case ICM_COMPRESS_QUERY:
565 FIXME("compression not implemented\n");
570 r = ICERR_UNSUPPORTED;
574 FIXME("Unknown message: %04x %ld %ld\n", msg, lParam1, lParam2);
580 /***********************************************************************
583 BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
585 TRACE("(%p,%ld,%p)\n", hModule, dwReason, lpReserved);
589 case DLL_PROCESS_ATTACH:
590 DisableThreadLibraryCalls(hModule);
591 MSVIDC32_hModule = hModule;
594 case DLL_PROCESS_DETACH: