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.1 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')
53 #define compare_fourcc(fcc1, fcc2) (((fcc1)^(fcc2))&~0x20202020)
55 #define PALETTE_COUNT 256
56 #define LE_16(x) ((((const uint8_t *)(x))[1] << 8) | ((const uint8_t *)(x))[0])
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); \
68 typedef struct Msvideo1Context
{
70 BOOL mode_8bit
; /* if it's not 8-bit, it's 16-bit */
74 msvideo1_decode_8bit( int width
, int height
, const unsigned char *buf
, int buf_size
,
75 unsigned char *pixels
, int stride
)
77 int block_ptr
, pixel_ptr
;
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 */
85 /* decoding parameters */
87 unsigned char byte_a
, byte_b
;
90 unsigned char colors
[8];
94 blocks_wide
= width
/ 4;
95 blocks_high
= height
/ 4;
96 total_blocks
= blocks_wide
* blocks_high
;
101 row_dec
= - (stride
- 4); /* such that -row_dec > 0 */
104 for (block_y
= blocks_high
; block_y
> 0; block_y
--) {
106 block_ptr
= ((block_y
* 4) - 1) * stride
;
108 block_ptr
= ((blocks_high
- block_y
) * 4) * stride
;
110 for (block_x
= blocks_wide
; block_x
> 0; block_x
--) {
111 /* check if this block should be skipped */
113 block_ptr
+= block_inc
;
119 pixel_ptr
= block_ptr
;
121 /* get the next two bytes in the encoded data stream */
123 byte_a
= buf
[stream_ptr
++];
124 byte_b
= buf
[stream_ptr
++];
126 /* check if the decode is finished */
127 if ((byte_a
== 0) && (byte_b
== 0) && (total_blocks
== 0))
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
;
137 colors
[0] = buf
[stream_ptr
++];
138 colors
[1] = buf
[stream_ptr
++];
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
;
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)
155 pixels
[pixel_ptr
++] =
156 colors
[((pixel_y
& 0x2) << 1) +
157 (pixel_x
& 0x2) + ((flags
& 0x1) ^ 1)];
158 pixel_ptr
-= row_dec
;
161 /* 1-color encoding */
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
;
171 block_ptr
+= block_inc
;
178 msvideo1_decode_16bit( int width
, int height
, const unsigned char *buf
, int buf_size
,
179 unsigned short *pixels
, int stride
)
181 int block_ptr
, pixel_ptr
;
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 */
189 /* decoding parameters */
191 unsigned char byte_a
, byte_b
;
192 unsigned short flags
;
194 unsigned short colors
[8];
198 blocks_wide
= width
/ 4;
199 blocks_high
= height
/ 4;
200 total_blocks
= blocks_wide
* blocks_high
;
203 row_dec
= stride
+ 4;
205 row_dec
= - (stride
- 4); /* such that -row_dec > 0 */
208 for (block_y
= blocks_high
; block_y
> 0; block_y
--) {
210 block_ptr
= ((block_y
* 4) - 1) * stride
;
212 block_ptr
= ((blocks_high
- block_y
) * 4) * stride
;
214 for (block_x
= blocks_wide
; block_x
> 0; block_x
--) {
215 /* check if this block should be skipped */
217 block_ptr
+= block_inc
;
223 pixel_ptr
= block_ptr
;
225 /* get the next two bytes in the encoded data stream */
227 byte_a
= buf
[stream_ptr
++];
228 byte_b
= buf
[stream_ptr
++];
230 /* check if the decode is finished */
231 if ((byte_a
== 0) && (byte_b
== 0) && (total_blocks
== 0)) {
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
;
241 colors
[0] = LE_16(&buf
[stream_ptr
]);
243 colors
[1] = LE_16(&buf
[stream_ptr
]);
246 if (colors
[0] & 0x8000) {
247 /* 8-color encoding */
248 CHECK_STREAM_PTR(12);
249 colors
[2] = LE_16(&buf
[stream_ptr
]);
251 colors
[3] = LE_16(&buf
[stream_ptr
]);
253 colors
[4] = LE_16(&buf
[stream_ptr
]);
255 colors
[5] = LE_16(&buf
[stream_ptr
]);
257 colors
[6] = LE_16(&buf
[stream_ptr
]);
259 colors
[7] = LE_16(&buf
[stream_ptr
]);
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
;
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
;
278 /* otherwise, it's a 1-color block */
279 colors
[0] = (byte_b
<< 8) | byte_a
;
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
;
288 block_ptr
+= block_inc
;
295 CRAM_DecompressQuery( Msvideo1Context
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
297 TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info
, in
, out
);
299 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
300 return ICERR_BADPARAM
;
302 TRACE("in->planes = %d\n", in
->bmiHeader
.biPlanes
);
303 TRACE("in->bpp = %d\n", in
->bmiHeader
.biBitCount
);
304 TRACE("in->height = %d\n", in
->bmiHeader
.biHeight
);
305 TRACE("in->width = %d\n", in
->bmiHeader
.biWidth
);
306 TRACE("in->compr = 0x%x\n", in
->bmiHeader
.biCompression
);
308 if( ( in
->bmiHeader
.biCompression
!= CRAM_MAGIC
) &&
309 ( in
->bmiHeader
.biCompression
!= MSVC_MAGIC
) &&
310 ( in
->bmiHeader
.biCompression
!= WHAM_MAGIC
) )
312 TRACE("can't do 0x%x compression\n", in
->bmiHeader
.biCompression
);
313 return ICERR_BADFORMAT
;
316 if( ( in
->bmiHeader
.biBitCount
!= 16 ) &&
317 ( in
->bmiHeader
.biBitCount
!= 8 ) )
319 TRACE("can't do %d bpp\n", in
->bmiHeader
.biBitCount
);
320 return ICERR_BADFORMAT
;
323 /* output must be same dimensions as input */
326 TRACE("out->planes = %d\n", out
->bmiHeader
.biPlanes
);
327 TRACE("out->bpp = %d\n", out
->bmiHeader
.biBitCount
);
328 TRACE("out->height = %d\n", out
->bmiHeader
.biHeight
);
329 TRACE("out->width = %d\n", out
->bmiHeader
.biWidth
);
330 if(( in
->bmiHeader
.biBitCount
!= out
->bmiHeader
.biBitCount
) ||
331 ( in
->bmiHeader
.biPlanes
!= out
->bmiHeader
.biPlanes
) ||
332 ( in
->bmiHeader
.biHeight
!= out
->bmiHeader
.biHeight
) ||
333 ( in
->bmiHeader
.biWidth
!= out
->bmiHeader
.biWidth
))
335 TRACE("incompatible output requested\n");
336 return ICERR_BADFORMAT
;
345 CRAM_DecompressGetFormat( Msvideo1Context
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
349 TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info
, in
, out
);
351 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
352 return ICERR_BADPARAM
;
354 size
= in
->bmiHeader
.biSize
;
355 if (in
->bmiHeader
.biBitCount
<= 8)
356 size
+= in
->bmiHeader
.biClrUsed
* sizeof(RGBQUAD
);
360 memcpy( out
, in
, size
);
361 out
->bmiHeader
.biCompression
= BI_RGB
;
362 out
->bmiHeader
.biSizeImage
= in
->bmiHeader
.biHeight
363 * in
->bmiHeader
.biWidth
*4;
370 static LRESULT
CRAM_DecompressBegin( Msvideo1Context
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
372 TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info
, in
, out
);
374 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
375 return ICERR_BADPARAM
;
377 TRACE("bitmap is %d bpp\n", in
->bmiHeader
.biBitCount
);
378 if( in
->bmiHeader
.biBitCount
== 8 )
379 info
->mode_8bit
= TRUE
;
380 else if( in
->bmiHeader
.biBitCount
== 16 )
381 info
->mode_8bit
= FALSE
;
384 info
->mode_8bit
= FALSE
;
385 FIXME("Unsupported output format %i\n", in
->bmiHeader
.biBitCount
);
391 static LRESULT
CRAM_Decompress( Msvideo1Context
*info
, ICDECOMPRESS
*icd
, DWORD size
)
393 LONG width
, height
, stride
, sz
;
395 TRACE("ICM_DECOMPRESS %p %p %d\n", info
, icd
, size
);
397 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
398 return ICERR_BADPARAM
;
400 /* FIXME: flags are ignored */
402 width
= icd
->lpbiInput
->biWidth
;
403 height
= icd
->lpbiInput
->biHeight
;
404 stride
= width
; /* in bytes or 16bit words */
405 sz
= icd
->lpbiInput
->biSizeImage
;
409 msvideo1_decode_8bit( width
, height
, icd
->lpInput
, sz
,
410 icd
->lpOutput
, stride
);
414 msvideo1_decode_16bit( width
, height
, icd
->lpInput
, sz
,
415 icd
->lpOutput
, stride
);
421 static LRESULT
CRAM_DecompressEx( Msvideo1Context
*info
, ICDECOMPRESSEX
*icd
, DWORD size
)
423 LONG width
, height
, stride
, sz
;
425 TRACE("ICM_DECOMPRESSEX %p %p %d\n", info
, icd
, size
);
427 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
428 return ICERR_BADPARAM
;
430 /* FIXME: flags are ignored */
432 width
= icd
->lpbiSrc
->biWidth
;
433 height
= icd
->lpbiSrc
->biHeight
;
435 sz
= icd
->lpbiSrc
->biSizeImage
;
439 msvideo1_decode_8bit( width
, height
, icd
->lpSrc
, sz
,
444 msvideo1_decode_16bit( width
, height
, icd
->lpSrc
, sz
,
451 static LRESULT
CRAM_GetInfo( const Msvideo1Context
*info
, ICINFO
*icinfo
, DWORD dwSize
)
453 if (!icinfo
) return sizeof(ICINFO
);
454 if (dwSize
< sizeof(ICINFO
)) return 0;
456 icinfo
->dwSize
= sizeof(ICINFO
);
457 icinfo
->fccType
= ICTYPE_VIDEO
;
458 icinfo
->fccHandler
= info
? info
->dwMagic
: CRAM_MAGIC
;
460 icinfo
->dwVersion
= ICVERSION
;
461 icinfo
->dwVersionICM
= ICVERSION
;
463 LoadStringW(MSVIDC32_hModule
, IDS_NAME
, icinfo
->szName
, sizeof(icinfo
->szName
)/sizeof(WCHAR
));
464 LoadStringW(MSVIDC32_hModule
, IDS_DESCRIPTION
, icinfo
->szDescription
, sizeof(icinfo
->szDescription
)/sizeof(WCHAR
));
465 /* msvfw32 will fill icinfo->szDriver for us */
467 return sizeof(ICINFO
);
470 /***********************************************************************
471 * DriverProc (MSVIDC32.@)
473 LRESULT WINAPI
CRAM_DriverProc( DWORD_PTR dwDriverId
, HDRVR hdrvr
, UINT msg
,
474 LPARAM lParam1
, LPARAM lParam2
)
476 Msvideo1Context
*info
= (Msvideo1Context
*) dwDriverId
;
477 LRESULT r
= ICERR_UNSUPPORTED
;
479 TRACE("%ld %p %04x %08lx %08lx\n", dwDriverId
, hdrvr
, msg
, lParam1
, lParam2
);
493 ICINFO
*icinfo
= (ICINFO
*)lParam2
;
497 if (icinfo
&& compare_fourcc(icinfo
->fccType
, ICTYPE_VIDEO
)) return 0;
499 info
= HeapAlloc( GetProcessHeap(), 0, sizeof (Msvideo1Context
) );
502 memset( info
, 0, sizeof *info
);
503 info
->dwMagic
= CRAM_MAGIC
;
510 HeapFree( GetProcessHeap(), 0, info
);
520 r
= CRAM_GetInfo( info
, (ICINFO
*)lParam1
, (DWORD
)lParam2
);
523 case ICM_DECOMPRESS_QUERY
:
524 r
= CRAM_DecompressQuery( info
, (LPBITMAPINFO
) lParam1
,
525 (LPBITMAPINFO
) lParam2
);
528 case ICM_DECOMPRESS_GET_FORMAT
:
529 r
= CRAM_DecompressGetFormat( info
, (LPBITMAPINFO
) lParam1
,
530 (LPBITMAPINFO
) lParam2
);
533 case ICM_DECOMPRESS_GET_PALETTE
:
534 FIXME("ICM_DECOMPRESS_GET_PALETTE\n");
537 case ICM_DECOMPRESSEX_QUERY
:
538 FIXME("ICM_DECOMPRESSEX_QUERY\n");
542 r
= CRAM_Decompress( info
, (ICDECOMPRESS
*) lParam1
,
546 case ICM_DECOMPRESS_BEGIN
:
547 r
= CRAM_DecompressBegin( info
, (LPBITMAPINFO
) lParam1
,
548 (LPBITMAPINFO
) lParam2
);
551 case ICM_DECOMPRESSEX
:
552 r
= CRAM_DecompressEx( info
, (ICDECOMPRESSEX
*) lParam1
,
556 case ICM_DECOMPRESS_END
:
560 case ICM_COMPRESS_QUERY
:
563 case ICM_COMPRESS_GET_FORMAT
:
564 case ICM_COMPRESS_END
:
566 FIXME("compression not implemented\n");
573 FIXME("Unknown message: %04x %ld %ld\n", msg
, lParam1
, lParam2
);
579 /***********************************************************************
582 BOOL WINAPI
DllMain(HINSTANCE hModule
, DWORD dwReason
, LPVOID lpReserved
)
584 TRACE("(%p,%d,%p)\n", hModule
, dwReason
, lpReserved
);
588 case DLL_PROCESS_ATTACH
:
589 DisableThreadLibraryCalls(hModule
);
590 MSVIDC32_hModule
= hModule
;