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.
34 #define WIN32_NO_STATUS
36 #define COM_NO_WINDOWS_H
42 //#include "winuser.h"
43 //#include "commdlg.h"
45 //#include "mmsystem.h"
46 #include "msvidc32_private.h"
48 #include <wine/debug.h>
50 WINE_DEFAULT_DEBUG_CHANNEL(msvidc32
);
52 static HINSTANCE MSVIDC32_hModule
;
54 #define CRAM_MAGIC mmioFOURCC('C', 'R', 'A', 'M')
55 #define MSVC_MAGIC mmioFOURCC('M', 'S', 'V', 'C')
56 #define WHAM_MAGIC mmioFOURCC('W', 'H', 'A', 'M')
57 #define compare_fourcc(fcc1, fcc2) (((fcc1)^(fcc2))&~0x20202020)
59 #define PALETTE_COUNT 256
60 #define LE_16(x) ((((const uint8_t *)(x))[1] << 8) | ((const uint8_t *)(x))[0])
62 /* FIXME - check the stream size */
63 #define CHECK_STREAM_PTR(n) \
64 if ((stream_ptr + n) > buf_size ) { \
65 WARN("stream_ptr out of bounds (%d >= %d)\n", \
66 stream_ptr + n, buf_size); \
72 typedef struct Msvideo1Context
{
74 BOOL mode_8bit
; /* if it's not 8-bit, it's 16-bit */
78 msvideo1_decode_8bit( int width
, int height
, const unsigned char *buf
, int buf_size
,
79 unsigned char *pixels
, int stride
)
81 int block_ptr
, pixel_ptr
;
83 int pixel_x
, pixel_y
; /* pixel width and height iterators */
84 int block_x
, block_y
; /* block width and height iterators */
85 int blocks_wide
, blocks_high
; /* width and height in 4x4 blocks */
89 /* decoding parameters */
91 unsigned char byte_a
, byte_b
;
94 unsigned char colors
[8];
98 blocks_wide
= width
/ 4;
99 blocks_high
= height
/ 4;
100 total_blocks
= blocks_wide
* blocks_high
;
103 row_dec
= stride
+ 4;
105 row_dec
= - (stride
- 4); /* such that -row_dec > 0 */
108 for (block_y
= blocks_high
; block_y
> 0; block_y
--) {
110 block_ptr
= ((block_y
* 4) - 1) * stride
;
112 block_ptr
= ((blocks_high
- block_y
) * 4) * stride
;
114 for (block_x
= blocks_wide
; block_x
> 0; block_x
--) {
115 /* check if this block should be skipped */
117 block_ptr
+= block_inc
;
123 pixel_ptr
= block_ptr
;
125 /* get the next two bytes in the encoded data stream */
127 byte_a
= buf
[stream_ptr
++];
128 byte_b
= buf
[stream_ptr
++];
130 /* check if the decode is finished */
131 if ((byte_a
== 0) && (byte_b
== 0) && (total_blocks
== 0))
133 else if ((byte_b
& 0xFC) == 0x84) {
134 /* skip code, but don't count the current block */
135 skip_blocks
= ((byte_b
- 0x84) << 8) + byte_a
- 1;
136 } else if (byte_b
< 0x80) {
137 /* 2-color encoding */
138 flags
= (byte_b
<< 8) | byte_a
;
141 colors
[0] = buf
[stream_ptr
++];
142 colors
[1] = buf
[stream_ptr
++];
144 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
145 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++, flags
>>= 1)
146 pixels
[pixel_ptr
++] = colors
[(flags
& 0x1) ^ 1];
147 pixel_ptr
-= row_dec
;
149 } else if (byte_b
>= 0x90) {
150 /* 8-color encoding */
151 flags
= (byte_b
<< 8) | byte_a
;
154 memcpy(colors
, &buf
[stream_ptr
], 8);
157 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
158 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++, flags
>>= 1)
159 pixels
[pixel_ptr
++] =
160 colors
[((pixel_y
& 0x2) << 1) +
161 (pixel_x
& 0x2) + ((flags
& 0x1) ^ 1)];
162 pixel_ptr
-= row_dec
;
165 /* 1-color encoding */
168 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
169 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++)
170 pixels
[pixel_ptr
++] = colors
[0];
171 pixel_ptr
-= row_dec
;
175 block_ptr
+= block_inc
;
182 msvideo1_decode_16bit( int width
, int height
, const unsigned char *buf
, int buf_size
,
183 unsigned short *pixels
, int stride
)
185 int block_ptr
, pixel_ptr
;
187 int pixel_x
, pixel_y
; /* pixel width and height iterators */
188 int block_x
, block_y
; /* block width and height iterators */
189 int blocks_wide
, blocks_high
; /* width and height in 4x4 blocks */
193 /* decoding parameters */
195 unsigned char byte_a
, byte_b
;
196 unsigned short flags
;
198 unsigned short colors
[8];
202 blocks_wide
= width
/ 4;
203 blocks_high
= height
/ 4;
204 total_blocks
= blocks_wide
* blocks_high
;
207 row_dec
= stride
+ 4;
209 row_dec
= - (stride
- 4); /* such that -row_dec > 0 */
212 for (block_y
= blocks_high
; block_y
> 0; block_y
--) {
214 block_ptr
= ((block_y
* 4) - 1) * stride
;
216 block_ptr
= ((blocks_high
- block_y
) * 4) * stride
;
218 for (block_x
= blocks_wide
; block_x
> 0; block_x
--) {
219 /* check if this block should be skipped */
221 block_ptr
+= block_inc
;
227 pixel_ptr
= block_ptr
;
229 /* get the next two bytes in the encoded data stream */
231 byte_a
= buf
[stream_ptr
++];
232 byte_b
= buf
[stream_ptr
++];
234 /* check if the decode is finished */
235 if ((byte_a
== 0) && (byte_b
== 0) && (total_blocks
== 0)) {
237 } else if ((byte_b
& 0xFC) == 0x84) {
238 /* skip code, but don't count the current block */
239 skip_blocks
= ((byte_b
- 0x84) << 8) + byte_a
- 1;
240 } else if (byte_b
< 0x80) {
241 /* 2- or 8-color encoding modes */
242 flags
= (byte_b
<< 8) | byte_a
;
245 colors
[0] = LE_16(&buf
[stream_ptr
]);
247 colors
[1] = LE_16(&buf
[stream_ptr
]);
250 if (colors
[0] & 0x8000) {
251 /* 8-color encoding */
252 CHECK_STREAM_PTR(12);
253 colors
[2] = LE_16(&buf
[stream_ptr
]);
255 colors
[3] = LE_16(&buf
[stream_ptr
]);
257 colors
[4] = LE_16(&buf
[stream_ptr
]);
259 colors
[5] = LE_16(&buf
[stream_ptr
]);
261 colors
[6] = LE_16(&buf
[stream_ptr
]);
263 colors
[7] = LE_16(&buf
[stream_ptr
]);
266 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
267 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++, flags
>>= 1)
268 pixels
[pixel_ptr
++] =
269 colors
[((pixel_y
& 0x2) << 1) +
270 (pixel_x
& 0x2) + ((flags
& 0x1) ^ 1)];
271 pixel_ptr
-= row_dec
;
274 /* 2-color encoding */
275 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
276 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++, flags
>>= 1)
277 pixels
[pixel_ptr
++] = colors
[(flags
& 0x1) ^ 1];
278 pixel_ptr
-= row_dec
;
282 /* otherwise, it's a 1-color block */
283 colors
[0] = (byte_b
<< 8) | byte_a
;
285 for (pixel_y
= 0; pixel_y
< 4; pixel_y
++) {
286 for (pixel_x
= 0; pixel_x
< 4; pixel_x
++)
287 pixels
[pixel_ptr
++] = colors
[0];
288 pixel_ptr
-= row_dec
;
292 block_ptr
+= block_inc
;
299 CRAM_DecompressQuery( Msvideo1Context
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
301 TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info
, in
, out
);
303 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
304 return ICERR_BADPARAM
;
306 TRACE("planes = %d\n", in
->bmiHeader
.biPlanes
);
307 TRACE("bpp = %d\n", in
->bmiHeader
.biBitCount
);
308 TRACE("height = %d\n", in
->bmiHeader
.biHeight
);
309 TRACE("width = %d\n", in
->bmiHeader
.biWidth
);
310 TRACE("compr = %x\n", in
->bmiHeader
.biCompression
);
312 if( ( in
->bmiHeader
.biCompression
!= CRAM_MAGIC
) &&
313 ( in
->bmiHeader
.biCompression
!= MSVC_MAGIC
) &&
314 ( in
->bmiHeader
.biCompression
!= WHAM_MAGIC
) )
315 return ICERR_BADFORMAT
;
317 if( ( in
->bmiHeader
.biBitCount
!= 16 ) &&
318 ( in
->bmiHeader
.biBitCount
!= 8 ) )
320 TRACE("can't do %d bpp\n", in
->bmiHeader
.biBitCount
);
321 return ICERR_BADFORMAT
;
324 /* output must be same dimensions as input */
327 if( in
->bmiHeader
.biBitCount
!= out
->bmiHeader
.biBitCount
)
328 return ICERR_BADFORMAT
;
329 if( in
->bmiHeader
.biPlanes
!= out
->bmiHeader
.biPlanes
)
330 return ICERR_BADFORMAT
;
331 if( in
->bmiHeader
.biHeight
!= out
->bmiHeader
.biHeight
)
332 return ICERR_BADFORMAT
;
333 if( in
->bmiHeader
.biWidth
!= out
->bmiHeader
.biWidth
)
334 return ICERR_BADFORMAT
;
343 CRAM_DecompressGetFormat( Msvideo1Context
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
347 TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info
, in
, out
);
349 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
350 return ICERR_BADPARAM
;
352 size
= in
->bmiHeader
.biSize
;
353 if (in
->bmiHeader
.biBitCount
<= 8)
354 size
+= in
->bmiHeader
.biClrUsed
* sizeof(RGBQUAD
);
358 memcpy( out
, in
, size
);
359 out
->bmiHeader
.biCompression
= BI_RGB
;
360 out
->bmiHeader
.biSizeImage
= in
->bmiHeader
.biHeight
361 * in
->bmiHeader
.biWidth
*4;
368 static LRESULT
CRAM_DecompressBegin( Msvideo1Context
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
370 TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info
, in
, out
);
372 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
373 return ICERR_BADPARAM
;
375 TRACE("bitmap is %d bpp\n", in
->bmiHeader
.biBitCount
);
376 if( in
->bmiHeader
.biBitCount
== 8 )
377 info
->mode_8bit
= TRUE
;
378 else if( in
->bmiHeader
.biBitCount
== 16 )
379 info
->mode_8bit
= FALSE
;
382 info
->mode_8bit
= FALSE
;
383 FIXME("Unsupported output format %i\n", in
->bmiHeader
.biBitCount
);
389 static LRESULT
CRAM_Decompress( Msvideo1Context
*info
, ICDECOMPRESS
*icd
, DWORD size
)
391 LONG width
, height
, stride
, sz
;
393 TRACE("ICM_DECOMPRESS %p %p %d\n", info
, icd
, size
);
395 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
396 return ICERR_BADPARAM
;
398 /* FIXME: flags are ignored */
400 width
= icd
->lpbiInput
->biWidth
;
401 height
= icd
->lpbiInput
->biHeight
;
402 stride
= width
; /* in bytes or 16bit words */
403 sz
= icd
->lpbiInput
->biSizeImage
;
407 msvideo1_decode_8bit( width
, height
, icd
->lpInput
, sz
,
408 icd
->lpOutput
, stride
);
412 msvideo1_decode_16bit( width
, height
, icd
->lpInput
, sz
,
413 icd
->lpOutput
, stride
);
419 static LRESULT
CRAM_DecompressEx( Msvideo1Context
*info
, ICDECOMPRESSEX
*icd
, DWORD size
)
421 LONG width
, height
, stride
, sz
;
423 TRACE("ICM_DECOMPRESSEX %p %p %d\n", info
, icd
, size
);
425 if( (info
==NULL
) || (info
->dwMagic
!=CRAM_MAGIC
) )
426 return ICERR_BADPARAM
;
428 /* FIXME: flags are ignored */
430 width
= icd
->lpbiSrc
->biWidth
;
431 height
= icd
->lpbiSrc
->biHeight
;
433 sz
= icd
->lpbiSrc
->biSizeImage
;
437 msvideo1_decode_8bit( width
, height
, icd
->lpSrc
, sz
,
442 msvideo1_decode_16bit( width
, height
, icd
->lpSrc
, sz
,
449 static LRESULT
CRAM_GetInfo( const Msvideo1Context
*info
, ICINFO
*icinfo
, DWORD dwSize
)
451 if (!icinfo
) return sizeof(ICINFO
);
452 if (dwSize
< sizeof(ICINFO
)) return 0;
454 icinfo
->dwSize
= sizeof(ICINFO
);
455 icinfo
->fccType
= ICTYPE_VIDEO
;
456 icinfo
->fccHandler
= info
? info
->dwMagic
: CRAM_MAGIC
;
458 icinfo
->dwVersion
= ICVERSION
;
459 icinfo
->dwVersionICM
= ICVERSION
;
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 */
465 return sizeof(ICINFO
);
468 /***********************************************************************
469 * DriverProc (MSVIDC32.@)
471 LRESULT WINAPI
CRAM_DriverProc( DWORD_PTR dwDriverId
, HDRVR hdrvr
, UINT msg
,
472 LPARAM lParam1
, LPARAM lParam2
)
474 Msvideo1Context
*info
= (Msvideo1Context
*) dwDriverId
;
475 LRESULT r
= ICERR_UNSUPPORTED
;
477 TRACE("%ld %p %04x %08lx %08lx\n", dwDriverId
, hdrvr
, msg
, lParam1
, lParam2
);
491 ICINFO
*icinfo
= (ICINFO
*)lParam2
;
495 if (icinfo
&& compare_fourcc(icinfo
->fccType
, ICTYPE_VIDEO
)) return 0;
497 info
= HeapAlloc( GetProcessHeap(), 0, sizeof (Msvideo1Context
) );
500 memset( info
, 0, sizeof *info
);
501 info
->dwMagic
= CRAM_MAGIC
;
508 HeapFree( GetProcessHeap(), 0, info
);
518 r
= CRAM_GetInfo( info
, (ICINFO
*)lParam1
, (DWORD
)lParam2
);
521 case ICM_DECOMPRESS_QUERY
:
522 r
= CRAM_DecompressQuery( info
, (LPBITMAPINFO
) lParam1
,
523 (LPBITMAPINFO
) lParam2
);
526 case ICM_DECOMPRESS_GET_FORMAT
:
527 r
= CRAM_DecompressGetFormat( info
, (LPBITMAPINFO
) lParam1
,
528 (LPBITMAPINFO
) lParam2
);
531 case ICM_DECOMPRESS_GET_PALETTE
:
532 FIXME("ICM_DECOMPRESS_GET_PALETTE\n");
535 case ICM_DECOMPRESSEX_QUERY
:
536 FIXME("ICM_DECOMPRESSEX_QUERY\n");
540 r
= CRAM_Decompress( info
, (ICDECOMPRESS
*) lParam1
,
544 case ICM_DECOMPRESS_BEGIN
:
545 r
= CRAM_DecompressBegin( info
, (LPBITMAPINFO
) lParam1
,
546 (LPBITMAPINFO
) lParam2
);
549 case ICM_DECOMPRESSEX
:
550 r
= CRAM_DecompressEx( info
, (ICDECOMPRESSEX
*) lParam1
,
554 case ICM_DECOMPRESS_END
:
558 case ICM_COMPRESS_QUERY
:
559 FIXME("compression not implemented\n");
564 r
= ICERR_UNSUPPORTED
;
568 FIXME("Unknown message: %04x %ld %ld\n", msg
, lParam1
, lParam2
);
574 /***********************************************************************
577 BOOL WINAPI
DllMain(HINSTANCE hModule
, DWORD dwReason
, LPVOID lpReserved
)
579 TRACE("(%p,%d,%p)\n", hModule
, dwReason
, lpReserved
);
583 case DLL_PROCESS_ATTACH
:
584 DisableThreadLibraryCalls(hModule
);
585 MSVIDC32_hModule
= hModule
;