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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msvidc32);
48 #define CRAM_MAGIC mmioFOURCC('C', 'R', 'A', 'M')
49 #define MSVC_MAGIC mmioFOURCC('M', 'S', 'V', 'C')
50 #define WHAM_MAGIC mmioFOURCC('W', 'H', 'A', 'M')
52 #define PALETTE_COUNT 256
53 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
55 /* FIXME - check the stream size */
56 #define CHECK_STREAM_PTR(n) \
57 if ((stream_ptr + n) > buf_size ) { \
58 WARN("stream_ptr out of bounds (%d >= %d)\n", \
59 stream_ptr + n, buf_size); \
65 typedef struct Msvideo1Context {
67 int mode_8bit; /* if it's not 8-bit, it's 16-bit */
71 msvideo1_decode_8bit( int width, int height, unsigned char *buf, int buf_size,
72 unsigned char *pixels, int stride)
74 int block_ptr, pixel_ptr;
76 int pixel_x, pixel_y; /* pixel width and height iterators */
77 int block_x, block_y; /* block width and height iterators */
78 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
82 /* decoding parameters */
84 unsigned char byte_a, byte_b;
87 unsigned char colors[8];
91 blocks_wide = width / 4;
92 blocks_high = height / 4;
93 total_blocks = blocks_wide * blocks_high;
97 for (block_y = blocks_high; block_y > 0; block_y--) {
98 block_ptr = ((block_y * 4) - 1) * stride;
99 for (block_x = blocks_wide; block_x > 0; block_x--) {
100 /* check if this block should be skipped */
102 block_ptr += block_inc;
108 pixel_ptr = block_ptr;
110 /* get the next two bytes in the encoded data stream */
112 byte_a = buf[stream_ptr++];
113 byte_b = buf[stream_ptr++];
115 /* check if the decode is finished */
116 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
118 else if ((byte_b & 0xFC) == 0x84) {
119 /* skip code, but don't count the current block */
120 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
121 } else if (byte_b < 0x80) {
122 /* 2-color encoding */
123 flags = (byte_b << 8) | byte_a;
126 colors[0] = buf[stream_ptr++];
127 colors[1] = buf[stream_ptr++];
129 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
130 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
133 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
135 pixels[width*(height-(pixel_ptr/width)-1) +
137 colors[(flags & 0x1) ^ 1];
141 pixel_ptr -= row_dec;
143 } else if (byte_b >= 0x90) {
144 /* 8-color encoding */
145 flags = (byte_b << 8) | byte_a;
148 memcpy(colors, &buf[stream_ptr], 8);
151 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
152 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)];
159 pixels[width*(height-(pixel_ptr/width)-1) +
161 colors[((pixel_y & 0x2) << 1) +
162 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
166 pixel_ptr -= row_dec;
169 /* 1-color encoding */
172 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
173 for (pixel_x = 0; pixel_x < 4; pixel_x++)
176 pixels[pixel_ptr++] = colors[0];
178 pixels[width*(height-(pixel_ptr/width)-1) +
179 pixel_ptr%width] = colors[0];
183 pixel_ptr -= row_dec;
187 block_ptr += block_inc;
194 msvideo1_decode_16bit( int width, int height, unsigned char *buf, int buf_size,
195 unsigned short *pixels, int stride)
197 int block_ptr, pixel_ptr;
199 int pixel_x, pixel_y; /* pixel width and height iterators */
200 int block_x, block_y; /* block width and height iterators */
201 int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
205 /* decoding parameters */
207 unsigned char byte_a, byte_b;
208 unsigned short flags;
210 unsigned short colors[8];
214 blocks_wide = width / 4;
215 blocks_high = height / 4;
216 total_blocks = blocks_wide * blocks_high;
218 row_dec = stride + 4;
220 for (block_y = blocks_high; block_y > 0; block_y--) {
221 block_ptr = ((block_y * 4) - 1) * stride;
222 for (block_x = blocks_wide; block_x > 0; block_x--) {
223 /* check if this block should be skipped */
225 block_ptr += block_inc;
231 pixel_ptr = block_ptr;
233 /* get the next two bytes in the encoded data stream */
235 byte_a = buf[stream_ptr++];
236 byte_b = buf[stream_ptr++];
238 /* check if the decode is finished */
239 if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
241 } else if ((byte_b & 0xFC) == 0x84) {
242 /* skip code, but don't count the current block */
243 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
244 } else if (byte_b < 0x80) {
245 /* 2- or 8-color encoding modes */
246 flags = (byte_b << 8) | byte_a;
249 colors[0] = LE_16(&buf[stream_ptr]);
251 colors[1] = LE_16(&buf[stream_ptr]);
254 if (colors[0] & 0x8000) {
255 /* 8-color encoding */
256 CHECK_STREAM_PTR(12);
257 colors[2] = LE_16(&buf[stream_ptr]);
259 colors[3] = LE_16(&buf[stream_ptr]);
261 colors[4] = LE_16(&buf[stream_ptr]);
263 colors[5] = LE_16(&buf[stream_ptr]);
265 colors[6] = LE_16(&buf[stream_ptr]);
267 colors[7] = LE_16(&buf[stream_ptr]);
270 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
271 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
272 pixels[pixel_ptr++] =
273 colors[((pixel_y & 0x2) << 1) +
274 (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
275 pixel_ptr -= row_dec;
278 /* 2-color encoding */
279 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
280 for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
281 pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
282 pixel_ptr -= row_dec;
286 /* otherwise, it's a 1-color block */
287 colors[0] = (byte_b << 8) | byte_a;
289 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
290 for (pixel_x = 0; pixel_x < 4; pixel_x++)
291 pixels[pixel_ptr++] = colors[0];
292 pixel_ptr -= row_dec;
296 block_ptr += block_inc;
303 CRAM_DecompressQuery( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
305 TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info, in, out);
307 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
308 return ICERR_BADPARAM;
310 TRACE("planes = %d\n", in->bmiHeader.biPlanes );
311 TRACE("bpp = %d\n", in->bmiHeader.biBitCount );
312 TRACE("height = %ld\n", in->bmiHeader.biHeight );
313 TRACE("width = %ld\n", in->bmiHeader.biWidth );
314 TRACE("compr = %lx\n", in->bmiHeader.biCompression );
316 if( ( in->bmiHeader.biCompression != CRAM_MAGIC ) &&
317 ( in->bmiHeader.biCompression != MSVC_MAGIC ) &&
318 ( in->bmiHeader.biCompression != WHAM_MAGIC ) )
319 return ICERR_UNSUPPORTED;
321 if( ( in->bmiHeader.biBitCount != 16 ) &&
322 ( in->bmiHeader.biBitCount != 8 ) )
324 TRACE("can't do %d bpp\n", in->bmiHeader.biBitCount );
325 return ICERR_UNSUPPORTED;
328 /* output must be same dimensions as input */
331 if( in->bmiHeader.biBitCount != out->bmiHeader.biBitCount )
332 return ICERR_UNSUPPORTED;
333 if( in->bmiHeader.biPlanes != out->bmiHeader.biPlanes )
334 return ICERR_UNSUPPORTED;
335 if( in->bmiHeader.biHeight != out->bmiHeader.biHeight )
336 return ICERR_UNSUPPORTED;
337 if( in->bmiHeader.biWidth != out->bmiHeader.biWidth )
338 return ICERR_UNSUPPORTED;
347 CRAM_DecompressGetFormat( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
351 TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info, in, out);
353 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
354 return ICERR_BADPARAM;
356 size = in->bmiHeader.biSize;
357 if (in->bmiHeader.biBitCount <= 8)
358 size += in->bmiHeader.biClrUsed * sizeof(RGBQUAD);
362 memcpy( out, in, size );
363 out->bmiHeader.biCompression = BI_RGB;
364 out->bmiHeader.biSizeImage = in->bmiHeader.biHeight
365 * in->bmiHeader.biWidth *4;
372 LRESULT CRAM_DecompressBegin( Msvideo1Context *info, LPBITMAPINFO in, LPBITMAPINFO out )
374 TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info, in, out);
376 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
377 return ICERR_BADPARAM;
379 TRACE("bitmap is %d bpp\n", in->bmiHeader.biBitCount);
380 if( in->bmiHeader.biBitCount == 8 )
382 else if( in->bmiHeader.biBitCount == 16 )
386 ERR("Bad output format\n");
387 return ICERR_BADPARAM;
393 LRESULT CRAM_Decompress( Msvideo1Context *info, ICDECOMPRESS *icd, DWORD size )
395 LONG width, height, stride, sz;
398 TRACE("ICM_DECOMPRESS %p %p %ld\n", info, icd, size);
400 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
401 return ICERR_BADPARAM;
403 /* FIXME: flags are ignored */
405 width = icd->lpbiInput->biWidth;
406 height = icd->lpbiInput->biHeight;
407 bit_per_pixel = icd->lpbiInput->biBitCount;
408 stride = width*bit_per_pixel/8;
409 sz = icd->lpbiInput->biSizeImage;
413 msvideo1_decode_8bit( width, height, icd->lpInput, sz,
414 icd->lpOutput, stride);
418 msvideo1_decode_16bit( width, height, icd->lpInput, sz,
419 icd->lpOutput, stride);
425 LRESULT CRAM_DecompressEx( Msvideo1Context *info, ICDECOMPRESSEX *icd, DWORD size )
427 LONG width, height, stride, sz;
430 TRACE("ICM_DECOMPRESSEX %p %p %ld\n", info, icd, size);
432 if( (info==NULL) || (info->dwMagic!=CRAM_MAGIC) )
433 return ICERR_BADPARAM;
435 /* FIXME: flags are ignored */
437 width = icd->lpbiSrc->biWidth;
438 height = icd->lpbiSrc->biHeight;
439 bit_per_pixel = icd->lpbiSrc->biBitCount;
440 stride = width*bit_per_pixel/8;
441 sz = icd->lpbiSrc->biSizeImage;
445 msvideo1_decode_8bit( width, height, icd->lpSrc, sz,
450 msvideo1_decode_16bit( width, height, icd->lpSrc, sz,
457 /***********************************************************************
458 * DriverProc (MSVIDC32.@)
460 LRESULT WINAPI CRAM_DriverProc( DWORD dwDriverId, HDRVR hdrvr, UINT msg,
461 LONG lParam1, LONG lParam2)
463 Msvideo1Context *info = (Msvideo1Context *) dwDriverId;
466 TRACE("%ld %p %d %ld %ld\n", dwDriverId, hdrvr, msg, lParam1, lParam2);
480 info = HeapAlloc( GetProcessHeap(), 0, sizeof (Msvideo1Context) );
483 memset( info, 0, sizeof info );
484 info->dwMagic = CRAM_MAGIC;
489 case ICM_DECOMPRESS_QUERY:
490 r = CRAM_DecompressQuery( info, (LPBITMAPINFO) lParam1,
491 (LPBITMAPINFO) lParam2 );
494 case ICM_DECOMPRESS_GET_FORMAT:
495 r = CRAM_DecompressGetFormat( info, (LPBITMAPINFO) lParam1,
496 (LPBITMAPINFO) lParam2 );
499 case ICM_DECOMPRESS_GET_PALETTE:
500 FIXME("ICM_DECOMPRESS_GET_PALETTE\n");
503 case ICM_DECOMPRESSEX_QUERY:
504 FIXME("ICM_DECOMPRESSEX_QUERY\n");
508 r = CRAM_Decompress( info, (ICDECOMPRESS*) lParam1,
512 case ICM_DECOMPRESS_BEGIN:
513 r = CRAM_DecompressBegin( info, (LPBITMAPINFO) lParam1,
514 (LPBITMAPINFO) lParam2 );
517 case ICM_DECOMPRESSEX:
518 r = CRAM_DecompressEx( info, (ICDECOMPRESSEX*) lParam1,
523 HeapFree( GetProcessHeap(), 0, info );
533 FIXME("Unknown message: %04x %ld %ld\n", msg, lParam1, lParam2);