2 * Radius Cinepak Video Decoder
4 * Copyright 2001 Dr. Tim Ferguson (see below)
5 * Portions Copyright 2003 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
22 /* Copyright notice from original source:
23 * ------------------------------------------------------------------------
24 * Radius Cinepak Video Decoder
26 * Dr. Tim Ferguson, 2001.
27 * For more details on the algorithm:
28 * http://www.csse.monash.edu.au/~timf/videocodec.html
30 * This is basically a vector quantiser with adaptive vector density. The
31 * frame is segmented into 4x4 pixel blocks, and each block is coded using
32 * either 1 or 4 vectors.
34 * There are still some issues with this code yet to be resolved. In
35 * particular with decoding in the strip boundaries. However, I have not
36 * yet found a sequence it doesn't work on. Ill keep trying :)
38 * You may freely use this source code. I only ask that you reference its
39 * source in your projects documentation:
40 * Tim Ferguson: http://www.csse.monash.edu.au/~timf/
41 * ------------------------------------------------------------------------ */
51 #include "iccvid_private.h"
53 #include "wine/debug.h"
54 #include "wine/heap.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(iccvid
);
58 static HINSTANCE ICCVID_hModule
;
60 #define ICCVID_MAGIC mmioFOURCC('c', 'v', 'i', 'd')
61 #define compare_fourcc(fcc1, fcc2) (((fcc1)^(fcc2))&~0x20202020)
64 /* ------------------------------------------------------------------------ */
67 unsigned char y0
, y1
, y2
, y3
;
69 unsigned char r
[4], g
[4], b
[4];
73 cvid_codebook
*v4_codebook
[MAX_STRIPS
];
74 cvid_codebook
*v1_codebook
[MAX_STRIPS
];
75 unsigned int strip_num
;
78 typedef struct _ICCVID_Info
86 /* ------------------------------------------------------------------------ */
87 static unsigned char *in_buffer
, uiclip
[1024], *uiclp
= NULL
;
89 #define get_byte() *(in_buffer++)
90 #define skip_byte() in_buffer++
91 #define get_word() ((unsigned short)(in_buffer += 2, \
92 (in_buffer[-2] << 8 | in_buffer[-1])))
93 #define get_long() ((unsigned long)(in_buffer += 4, \
94 (in_buffer[-4] << 24 | in_buffer[-3] << 16 | in_buffer[-2] << 8 | in_buffer[-1])))
97 /* ---------------------------------------------------------------------- */
98 static inline void read_codebook(cvid_codebook
*c
, int mode
)
102 if(mode
) /* black and white */
110 c
->r
[0] = c
->g
[0] = c
->b
[0] = c
->y0
;
111 c
->r
[1] = c
->g
[1] = c
->b
[1] = c
->y1
;
112 c
->r
[2] = c
->g
[2] = c
->b
[2] = c
->y2
;
113 c
->r
[3] = c
->g
[3] = c
->b
[3] = c
->y3
;
117 c
->y0
= get_byte(); /* luma */
121 c
->u
= get_byte(); /* chroma */
125 uvg
= -((c
->u
+1) >> 1) - c
->v
;
128 c
->r
[0] = uiclp
[c
->y0
+ uvr
]; c
->g
[0] = uiclp
[c
->y0
+ uvg
]; c
->b
[0] = uiclp
[c
->y0
+ uvb
];
129 c
->r
[1] = uiclp
[c
->y1
+ uvr
]; c
->g
[1] = uiclp
[c
->y1
+ uvg
]; c
->b
[1] = uiclp
[c
->y1
+ uvb
];
130 c
->r
[2] = uiclp
[c
->y2
+ uvr
]; c
->g
[2] = uiclp
[c
->y2
+ uvg
]; c
->b
[2] = uiclp
[c
->y2
+ uvb
];
131 c
->r
[3] = uiclp
[c
->y3
+ uvr
]; c
->g
[3] = uiclp
[c
->y3
+ uvg
]; c
->b
[3] = uiclp
[c
->y3
+ uvb
];
135 static inline long get_addr(BOOL inverted
, unsigned long x
, unsigned long y
,
136 int frm_stride
, int bpp
, unsigned int out_height
)
138 /* Returns the starting position of a line from top-down or bottom-up */
140 return y
* frm_stride
+ x
* bpp
;
142 return (out_height
- 1 - y
) * frm_stride
+ x
* bpp
;
145 #define MAKECOLOUR32(r,g,b) (((r) << 16) | ((g) << 8) | (b))
146 /*#define MAKECOLOUR24(r,g,b) (((r) << 16) | ((g) << 8) | (b))*/
147 #define MAKECOLOUR16(r,g,b) (((r) >> 3) << 11)| (((g) >> 2) << 5)| (((b) >> 3) << 0)
148 #define MAKECOLOUR15(r,g,b) (((r) >> 3) << 10)| (((g) >> 3) << 5)| (((b) >> 3) << 0)
150 /* ------------------------------------------------------------------------ */
151 static void cvid_v1_32(unsigned char *frm
, unsigned char *limit
, int stride
, BOOL inverted
,
154 unsigned long *vptr
= (unsigned long *)frm
;
163 /* fill 4x4 block of pixels with colour values from codebook */
164 for (y
= 0; y
< 4; y
++)
166 if (&vptr
[y
*row_inc
] < (unsigned long *)limit
) return;
167 for (x
= 0; x
< 4; x
++)
168 vptr
[y
*row_inc
+ x
] = MAKECOLOUR32(cb
->r
[x
/2+(y
/2)*2], cb
->g
[x
/2+(y
/2)*2], cb
->b
[x
/2+(y
/2)*2]);
172 static inline int get_stride(int width
, int depth
)
174 return ((depth
* width
+ 31) >> 3) & ~3;
177 /* ------------------------------------------------------------------------ */
178 static void cvid_v4_32(unsigned char *frm
, unsigned char *limit
, int stride
, BOOL inverted
,
179 cvid_codebook
*cb0
, cvid_codebook
*cb1
, cvid_codebook
*cb2
, cvid_codebook
*cb3
)
181 unsigned long *vptr
= (unsigned long *)frm
;
184 cvid_codebook
* cb
[] = {cb0
,cb1
,cb2
,cb3
};
191 /* fill 4x4 block of pixels with colour values from codebooks */
192 for (y
= 0; y
< 4; y
++)
194 if (&vptr
[y
*row_inc
] < (unsigned long *)limit
) return;
195 for (x
= 0; x
< 4; x
++)
196 vptr
[y
*row_inc
+ x
] = MAKECOLOUR32(cb
[x
/2+(y
/2)*2]->r
[x
%2+(y
%2)*2], cb
[x
/2+(y
/2)*2]->g
[x
%2+(y
%2)*2], cb
[x
/2+(y
/2)*2]->b
[x
%2+(y
%2)*2]);
201 /* ------------------------------------------------------------------------ */
202 static void cvid_v1_24(unsigned char *vptr
, unsigned char *limit
, int stride
, BOOL inverted
,
213 /* fill 4x4 block of pixels with colour values from codebook */
214 for (y
= 0; y
< 4; y
++)
216 if (&vptr
[y
*row_inc
] < limit
) return;
217 for (x
= 0; x
< 4; x
++)
219 vptr
[y
*row_inc
+ x
*3 + 0] = cb
->b
[x
/2+(y
/2)*2];
220 vptr
[y
*row_inc
+ x
*3 + 1] = cb
->g
[x
/2+(y
/2)*2];
221 vptr
[y
*row_inc
+ x
*3 + 2] = cb
->r
[x
/2+(y
/2)*2];
227 /* ------------------------------------------------------------------------ */
228 static void cvid_v4_24(unsigned char *vptr
, unsigned char *limit
, int stride
, BOOL inverted
,
229 cvid_codebook
*cb0
, cvid_codebook
*cb1
, cvid_codebook
*cb2
, cvid_codebook
*cb3
)
232 cvid_codebook
* cb
[] = {cb0
,cb1
,cb2
,cb3
};
240 /* fill 4x4 block of pixels with colour values from codebooks */
241 for (y
= 0; y
< 4; y
++)
243 if (&vptr
[y
*row_inc
] < limit
) return;
244 for (x
= 0; x
< 4; x
++)
246 vptr
[y
*row_inc
+ x
*3 + 0] = cb
[x
/2+(y
/2)*2]->b
[x
%2+(y
%2)*2];
247 vptr
[y
*row_inc
+ x
*3 + 1] = cb
[x
/2+(y
/2)*2]->g
[x
%2+(y
%2)*2];
248 vptr
[y
*row_inc
+ x
*3 + 2] = cb
[x
/2+(y
/2)*2]->r
[x
%2+(y
%2)*2];
254 /* ------------------------------------------------------------------------ */
255 static void cvid_v1_16(unsigned char *frm
, unsigned char *limit
, int stride
, BOOL inverted
,
258 unsigned short *vptr
= (unsigned short *)frm
;
267 /* fill 4x4 block of pixels with colour values from codebook */
268 for (y
= 0; y
< 4; y
++)
270 if (&vptr
[y
*row_inc
] < (unsigned short *)limit
) return;
271 for (x
= 0; x
< 4; x
++)
272 vptr
[y
*row_inc
+ x
] = MAKECOLOUR16(cb
->r
[x
/2+(y
/2)*2], cb
->g
[x
/2+(y
/2)*2], cb
->b
[x
/2+(y
/2)*2]);
277 /* ------------------------------------------------------------------------ */
278 static void cvid_v4_16(unsigned char *frm
, unsigned char *limit
, int stride
, BOOL inverted
,
279 cvid_codebook
*cb0
, cvid_codebook
*cb1
, cvid_codebook
*cb2
, cvid_codebook
*cb3
)
281 unsigned short *vptr
= (unsigned short *)frm
;
283 cvid_codebook
* cb
[] = {cb0
,cb1
,cb2
,cb3
};
291 /* fill 4x4 block of pixels with colour values from codebooks */
292 for (y
= 0; y
< 4; y
++)
294 if (&vptr
[y
*row_inc
] < (unsigned short *)limit
) return;
295 for (x
= 0; x
< 4; x
++)
296 vptr
[y
*row_inc
+ x
] = MAKECOLOUR16(cb
[x
/2+(y
/2)*2]->r
[x
%2+(y
%2)*2], cb
[x
/2+(y
/2)*2]->g
[x
%2+(y
%2)*2], cb
[x
/2+(y
/2)*2]->b
[x
%2+(y
%2)*2]);
300 /* ------------------------------------------------------------------------ */
301 static void cvid_v1_15(unsigned char *frm
, unsigned char *limit
, int stride
, BOOL inverted
,
304 unsigned short *vptr
= (unsigned short *)frm
;
313 /* fill 4x4 block of pixels with colour values from codebook */
314 for (y
= 0; y
< 4; y
++)
316 if (&vptr
[y
*row_inc
] < (unsigned short *)limit
) return;
317 for (x
= 0; x
< 4; x
++)
318 vptr
[y
*row_inc
+ x
] = MAKECOLOUR15(cb
->r
[x
/2+(y
/2)*2], cb
->g
[x
/2+(y
/2)*2], cb
->b
[x
/2+(y
/2)*2]);
323 /* ------------------------------------------------------------------------ */
324 static void cvid_v4_15(unsigned char *frm
, unsigned char *limit
, int stride
, BOOL inverted
,
325 cvid_codebook
*cb0
, cvid_codebook
*cb1
, cvid_codebook
*cb2
, cvid_codebook
*cb3
)
327 unsigned short *vptr
= (unsigned short *)frm
;
329 cvid_codebook
* cb
[] = {cb0
,cb1
,cb2
,cb3
};
337 /* fill 4x4 block of pixels with colour values from codebooks */
338 for (y
= 0; y
< 4; y
++)
340 if (&vptr
[y
*row_inc
] < (unsigned short *)limit
) return;
341 for (x
= 0; x
< 4; x
++)
342 vptr
[y
*row_inc
+ x
] = MAKECOLOUR15(cb
[x
/2+(y
/2)*2]->r
[x
%2+(y
%2)*2], cb
[x
/2+(y
/2)*2]->g
[x
%2+(y
%2)*2], cb
[x
/2+(y
/2)*2]->b
[x
%2+(y
%2)*2]);
347 /* ------------------------------------------------------------------------
348 * Call this function once at the start of the sequence and save the
349 * returned context for calls to decode_cinepak().
351 static cinepak_info
*decode_cinepak_init(void)
353 cinepak_info
*cvinfo
;
356 cvinfo
= heap_alloc( sizeof (cinepak_info
) );
359 cvinfo
->strip_num
= 0;
364 for(i
= -512; i
< 512; i
++)
365 uiclp
[i
] = (i
< 0 ? 0 : (i
> 255 ? 255 : i
));
371 static void free_cvinfo( cinepak_info
*cvinfo
)
375 for( i
=0; i
<cvinfo
->strip_num
; i
++ )
377 heap_free(cvinfo
->v4_codebook
[i
]);
378 heap_free(cvinfo
->v1_codebook
[i
]);
383 typedef void (*fn_cvid_v1
)(unsigned char *frm
, unsigned char *limit
,
384 int stride
, BOOL inverted
, cvid_codebook
*cb
);
385 typedef void (*fn_cvid_v4
)(unsigned char *frm
, unsigned char *limit
,
386 int stride
, BOOL inverted
,
387 cvid_codebook
*cb0
, cvid_codebook
*cb1
,
388 cvid_codebook
*cb2
, cvid_codebook
*cb3
);
390 /* ------------------------------------------------------------------------
391 * This function decodes a buffer containing a Cinepak encoded frame.
393 * context - the context created by decode_cinepak_init().
394 * buf - the input buffer to be decoded
395 * size - the size of the input buffer
396 * output - the output frame buffer (24 or 32 bit per pixel)
397 * out_width - the width of the output frame
398 * out_height - the height of the output frame
399 * bit_per_pixel - the number of bits per pixel allocated to the output
400 * frame (only 24 or 32 bpp are supported)
401 * inverted - if true the output frame is written top-down
403 static void decode_cinepak(cinepak_info
*cvinfo
, unsigned char *buf
, int size
,
404 unsigned char *output
, unsigned int out_width
, unsigned int out_height
, int bit_per_pixel
, BOOL inverted
)
406 cvid_codebook
*v4_codebook
, *v1_codebook
, *codebook
= NULL
;
407 unsigned long x
, y
, y_bottom
, cnum
, strip_id
, chunk_id
,
408 x0
, y0
, x1
, y1
, ci
, flag
, mask
;
409 long top_size
, chunk_size
;
410 unsigned char *frm_ptr
;
411 unsigned int i
, cur_strip
, addr
;
412 int d0
, d1
, d2
, d3
, frm_stride
, bpp
= 3;
413 fn_cvid_v1 cvid_v1
= cvid_v1_24
;
414 fn_cvid_v4 cvid_v4
= cvid_v4_24
;
418 unsigned long length
;
419 unsigned short width
;
420 unsigned short height
;
421 unsigned short strips
;
428 frame
.flags
= get_byte();
429 frame
.length
= get_byte() << 16;
430 frame
.length
|= get_byte() << 8;
431 frame
.length
|= get_byte();
433 switch(bit_per_pixel
)
437 cvid_v1
= cvid_v1_15
;
438 cvid_v4
= cvid_v4_15
;
442 cvid_v1
= cvid_v1_16
;
443 cvid_v4
= cvid_v4_16
;
447 cvid_v1
= cvid_v1_24
;
448 cvid_v4
= cvid_v4_24
;
452 cvid_v1
= cvid_v1_32
;
453 cvid_v4
= cvid_v4_32
;
457 frm_stride
= get_stride(out_width
, bpp
* 8);
460 if(frame
.length
!= size
)
462 if(frame
.length
& 0x01) frame
.length
++; /* AVIs tend to have a size mismatch */
463 if(frame
.length
!= size
)
465 ERR("CVID: corruption %d (QT/AVI) != %ld (CV)\n", size
, frame
.length
);
470 frame
.width
= get_word();
471 frame
.height
= get_word();
472 frame
.strips
= get_word();
474 if(frame
.strips
> cvinfo
->strip_num
)
476 if(frame
.strips
>= MAX_STRIPS
)
478 ERR("CVID: strip overflow (more than %d)\n", MAX_STRIPS
);
482 for(i
= cvinfo
->strip_num
; i
< frame
.strips
; i
++)
484 if((cvinfo
->v4_codebook
[i
] = heap_alloc(sizeof(cvid_codebook
) * 260)) == NULL
)
486 ERR("CVID: codebook v4 alloc err\n");
490 if((cvinfo
->v1_codebook
[i
] = heap_alloc(sizeof(cvid_codebook
) * 260)) == NULL
)
492 ERR("CVID: codebook v1 alloc err\n");
497 cvinfo
->strip_num
= frame
.strips
;
499 TRACE("CVID: %ux%u, strips %u, length %lu\n",
500 frame
.width
, frame
.height
, frame
.strips
, frame
.length
);
502 for(cur_strip
= 0; cur_strip
< frame
.strips
; cur_strip
++)
504 v4_codebook
= cvinfo
->v4_codebook
[cur_strip
];
505 v1_codebook
= cvinfo
->v1_codebook
[cur_strip
];
507 if((cur_strip
> 0) && (!(frame
.flags
& 0x01)))
509 memcpy(cvinfo
->v4_codebook
[cur_strip
], cvinfo
->v4_codebook
[cur_strip
-1], 260 * sizeof(cvid_codebook
));
510 memcpy(cvinfo
->v1_codebook
[cur_strip
], cvinfo
->v1_codebook
[cur_strip
-1], 260 * sizeof(cvid_codebook
));
513 strip_id
= get_word(); /* 1000 = key strip, 1100 = iter strip */
514 top_size
= get_word();
515 y0
= get_word(); /* FIXME: most of these are ignored at the moment */
524 WARN("CVID: Warning x1 (%ld) != width (%d)\n", x1
, out_width
);
526 TRACE(" %d) %04lx %04ld <%ld,%ld> <%ld,%ld> yt %ld\n",
527 cur_strip
, strip_id
, top_size
, x0
, y0
, x1
, y1
, y_bottom
);
531 chunk_id
= get_word();
532 chunk_size
= get_word();
534 TRACE(" %04lx %04ld\n", chunk_id
, chunk_size
);
535 top_size
-= chunk_size
;
540 /* -------------------- Codebook Entries -------------------- */
543 codebook
= (chunk_id
== 0x2200 ? v1_codebook
: v4_codebook
);
545 for(i
= 0; i
< cnum
; i
++) read_codebook(codebook
+i
, 0);
549 case 0x2600: /* 8 bit per pixel */
550 codebook
= (chunk_id
== 0x2600 ? v1_codebook
: v4_codebook
);
552 for(i
= 0; i
< cnum
; i
++) read_codebook(codebook
+i
, 1);
557 codebook
= (chunk_id
== 0x2300 ? v1_codebook
: v4_codebook
);
560 while(chunk_size
> 0)
565 for(i
= 0; i
< 32; i
++)
567 if(flag
& 0x80000000)
570 read_codebook(codebook
+ci
, 0);
577 while(chunk_size
> 0) { skip_byte(); chunk_size
--; }
581 case 0x2700: /* 8 bit per pixel */
582 codebook
= (chunk_id
== 0x2700 ? v1_codebook
: v4_codebook
);
585 while(chunk_size
> 0)
590 for(i
= 0; i
< 32; i
++)
592 if(flag
& 0x80000000)
595 read_codebook(codebook
+ci
, 1);
602 while(chunk_size
> 0) { skip_byte(); chunk_size
--; }
605 /* -------------------- Frame -------------------- */
607 while((chunk_size
> 0) && (y
< y_bottom
))
612 for(i
= 0; i
< 32; i
++)
614 if(y
>= y_bottom
) break;
615 if(flag
& 0x80000000) /* 4 bytes per block */
623 addr
= get_addr(inverted
, x
, y
, frm_stride
, bpp
, out_height
);
624 cvid_v4(frm_ptr
+ addr
, output
, frm_stride
, inverted
, v4_codebook
+d0
, v4_codebook
+d1
, v4_codebook
+d2
, v4_codebook
+d3
);
626 else /* 1 byte per block */
628 addr
= get_addr(inverted
, x
, y
, frm_stride
, bpp
, out_height
);
629 cvid_v1(frm_ptr
+ addr
, output
, frm_stride
, inverted
, v1_codebook
+ get_byte());
643 while(chunk_size
> 0) { skip_byte(); chunk_size
--; }
647 while((chunk_size
> 0) && (y
< y_bottom
))
649 /* ---- flag bits: 0 = SKIP, 10 = V1, 11 = V4 ---- */
654 while((mask
) && (y
< y_bottom
))
660 if(chunk_size
< 0) break;
667 if(flag
& mask
) /* V4 */
675 addr
= get_addr(inverted
, x
, y
, frm_stride
, bpp
, out_height
);
676 cvid_v4(frm_ptr
+ addr
, output
, frm_stride
, inverted
, v4_codebook
+d0
, v4_codebook
+d1
, v4_codebook
+d2
, v4_codebook
+d3
);
682 addr
= get_addr(inverted
, x
, y
, frm_stride
, bpp
, out_height
);
683 cvid_v1(frm_ptr
+ addr
, output
, frm_stride
, inverted
, v1_codebook
+ get_byte());
697 while(chunk_size
> 0) { skip_byte(); chunk_size
--; }
700 case 0x3200: /* each byte is a V1 codebook */
701 while((chunk_size
> 0) && (y
< y_bottom
))
703 addr
= get_addr(inverted
, x
, y
, frm_stride
, bpp
, out_height
);
704 cvid_v1(frm_ptr
+ addr
, output
, frm_stride
, inverted
, v1_codebook
+ get_byte());
714 while(chunk_size
> 0) { skip_byte(); chunk_size
--; }
718 ERR("CVID: unknown chunk_id %08lx\n", chunk_id
);
719 while(chunk_size
> 0) { skip_byte(); chunk_size
--; }
725 if(frame
.length
!= size
)
727 if(frame
.length
& 0x01) frame
.length
++; /* AVIs tend to have a size mismatch */
728 if(frame
.length
!= size
)
732 xlen
= get_byte() << 16;
733 xlen
|= get_byte() << 8;
734 xlen
|= get_byte(); /* Read Len */
735 WARN("CVID: END INFO chunk size %d cvid size1 %ld cvid size2 %ld\n",
736 size
, frame
.length
, xlen
);
741 static void ICCVID_dump_BITMAPINFO(const BITMAPINFO
* bmi
)
749 bmi
->bmiHeader
.biPlanes
,
750 bmi
->bmiHeader
.biBitCount
,
751 bmi
->bmiHeader
.biHeight
,
752 bmi
->bmiHeader
.biWidth
,
753 debugstr_an( (const char *)&bmi
->bmiHeader
.biCompression
, 4 ) );
756 static inline int ICCVID_CheckMask(RGBQUAD bmiColors
[3], COLORREF redMask
, COLORREF blueMask
, COLORREF greenMask
)
758 COLORREF realRedMask
= MAKECOLOUR32(bmiColors
[0].rgbRed
, bmiColors
[0].rgbGreen
, bmiColors
[0].rgbBlue
);
759 COLORREF realBlueMask
= MAKECOLOUR32(bmiColors
[1].rgbRed
, bmiColors
[1].rgbGreen
, bmiColors
[1].rgbBlue
);
760 COLORREF realGreenMask
= MAKECOLOUR32(bmiColors
[2].rgbRed
, bmiColors
[2].rgbGreen
, bmiColors
[2].rgbBlue
);
762 TRACE("\nbmiColors[0] = 0x%08x\nbmiColors[1] = 0x%08x\nbmiColors[2] = 0x%08x\n",
763 realRedMask
, realBlueMask
, realGreenMask
);
765 if ((realRedMask
== redMask
) &&
766 (realBlueMask
== blueMask
) &&
767 (realGreenMask
== greenMask
))
772 static LRESULT
ICCVID_DecompressQuery( ICCVID_Info
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
774 TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info
, in
, out
);
776 if( (info
==NULL
) || (info
->dwMagic
!=ICCVID_MAGIC
) )
777 return ICERR_BADPARAM
;
780 ICCVID_dump_BITMAPINFO(in
);
782 if( in
->bmiHeader
.biCompression
!= ICCVID_MAGIC
)
783 return ICERR_BADFORMAT
;
788 ICCVID_dump_BITMAPINFO(out
);
790 if( in
->bmiHeader
.biPlanes
!= out
->bmiHeader
.biPlanes
)
791 return ICERR_BADFORMAT
;
792 if( in
->bmiHeader
.biHeight
!= out
->bmiHeader
.biHeight
)
794 if( in
->bmiHeader
.biHeight
!= -out
->bmiHeader
.biHeight
)
795 return ICERR_BADFORMAT
;
796 TRACE("Detected inverted height for video output\n");
798 if( in
->bmiHeader
.biWidth
!= out
->bmiHeader
.biWidth
)
799 return ICERR_BADFORMAT
;
801 switch( out
->bmiHeader
.biBitCount
)
804 if ( out
->bmiHeader
.biCompression
== BI_BITFIELDS
)
806 if ( !ICCVID_CheckMask(out
->bmiColors
, 0x7C00, 0x03E0, 0x001F) &&
807 !ICCVID_CheckMask(out
->bmiColors
, 0xF800, 0x07E0, 0x001F) )
809 TRACE("unsupported output bit field(s) for 16-bit colors\n");
810 return ICERR_BADFORMAT
;
818 TRACE("unsupported output bitcount = %d\n", out
->bmiHeader
.biBitCount
);
819 return ICERR_BADFORMAT
;
826 static LRESULT
ICCVID_DecompressGetFormat( ICCVID_Info
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
830 TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info
, in
, out
);
832 if( (info
==NULL
) || (info
->dwMagic
!=ICCVID_MAGIC
) )
833 return ICERR_BADPARAM
;
835 size
= in
->bmiHeader
.biSize
;
836 if (in
->bmiHeader
.biBitCount
<= 8)
837 size
+= in
->bmiHeader
.biClrUsed
* sizeof(RGBQUAD
);
841 memcpy( out
, in
, size
);
842 out
->bmiHeader
.biBitCount
= 24;
843 out
->bmiHeader
.biCompression
= BI_RGB
;
844 out
->bmiHeader
.biSizeImage
= get_stride(in
->bmiHeader
.biWidth
, 24) * in
->bmiHeader
.biHeight
;
850 static LRESULT
ICCVID_DecompressBegin( ICCVID_Info
*info
, LPBITMAPINFO in
, LPBITMAPINFO out
)
852 TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info
, in
, out
);
854 if( (info
==NULL
) || (info
->dwMagic
!=ICCVID_MAGIC
) )
855 return ICERR_BADPARAM
;
857 info
->bits_per_pixel
= out
->bmiHeader
.biBitCount
;
859 if (info
->bits_per_pixel
== 16)
861 if ( out
->bmiHeader
.biCompression
== BI_BITFIELDS
)
863 if ( ICCVID_CheckMask(out
->bmiColors
, 0x7C00, 0x03E0, 0x001F) )
864 info
->bits_per_pixel
= 15;
865 else if ( ICCVID_CheckMask(out
->bmiColors
, 0xF800, 0x07E0, 0x001F) )
866 info
->bits_per_pixel
= 16;
869 TRACE("unsupported output bit field(s) for 16-bit colors\n");
870 return ICERR_UNSUPPORTED
;
874 info
->bits_per_pixel
= 15;
877 TRACE("bit_per_pixel = %d\n", info
->bits_per_pixel
);
880 free_cvinfo( info
->cvinfo
);
881 info
->cvinfo
= decode_cinepak_init();
886 static LRESULT
ICCVID_Decompress( ICCVID_Info
*info
, ICDECOMPRESS
*icd
, DWORD size
)
891 TRACE("ICM_DECOMPRESS %p %p %d\n", info
, icd
, size
);
893 if( (info
==NULL
) || (info
->dwMagic
!=ICCVID_MAGIC
) )
894 return ICERR_BADPARAM
;
895 if (info
->cvinfo
==NULL
)
897 ERR("ICM_DECOMPRESS sent after ICM_DECOMPRESS_END\n");
898 return ICERR_BADPARAM
;
901 width
= icd
->lpbiInput
->biWidth
;
902 height
= icd
->lpbiInput
->biHeight
;
903 inverted
= -icd
->lpbiOutput
->biHeight
== height
;
905 decode_cinepak(info
->cvinfo
, icd
->lpInput
, icd
->lpbiInput
->biSizeImage
,
906 icd
->lpOutput
, width
, height
, info
->bits_per_pixel
, inverted
);
911 static LRESULT
ICCVID_DecompressEx( ICCVID_Info
*info
, ICDECOMPRESSEX
*icd
, DWORD size
)
916 TRACE("ICM_DECOMPRESSEX %p %p %d\n", info
, icd
, size
);
918 if( (info
==NULL
) || (info
->dwMagic
!=ICCVID_MAGIC
) )
919 return ICERR_BADPARAM
;
920 if (info
->cvinfo
==NULL
)
922 ERR("ICM_DECOMPRESSEX sent after ICM_DECOMPRESS_END\n");
923 return ICERR_BADPARAM
;
926 /* FIXME: flags are ignored */
928 width
= icd
->lpbiSrc
->biWidth
;
929 height
= icd
->lpbiSrc
->biHeight
;
930 inverted
= -icd
->lpbiDst
->biHeight
== height
;
932 decode_cinepak(info
->cvinfo
, icd
->lpSrc
, icd
->lpbiSrc
->biSizeImage
,
933 icd
->lpDst
, width
, height
, info
->bits_per_pixel
, inverted
);
938 static LRESULT
ICCVID_Close( ICCVID_Info
*info
)
940 if( (info
==NULL
) || (info
->dwMagic
!=ICCVID_MAGIC
) )
943 free_cvinfo( info
->cvinfo
);
948 static LRESULT
ICCVID_GetInfo( ICCVID_Info
*info
, ICINFO
*icinfo
, DWORD dwSize
)
950 if (!icinfo
) return sizeof(ICINFO
);
951 if (dwSize
< sizeof(ICINFO
)) return 0;
953 icinfo
->dwSize
= sizeof(ICINFO
);
954 icinfo
->fccType
= ICTYPE_VIDEO
;
955 icinfo
->fccHandler
= info
? info
->dwMagic
: ICCVID_MAGIC
;
957 icinfo
->dwVersion
= ICVERSION
;
958 icinfo
->dwVersionICM
= ICVERSION
;
960 LoadStringW(ICCVID_hModule
, IDS_NAME
, icinfo
->szName
, ARRAY_SIZE(icinfo
->szName
));
961 LoadStringW(ICCVID_hModule
, IDS_DESCRIPTION
, icinfo
->szDescription
, ARRAY_SIZE(icinfo
->szDescription
));
962 /* msvfw32 will fill icinfo->szDriver for us */
964 return sizeof(ICINFO
);
967 static LRESULT
ICCVID_DecompressEnd( ICCVID_Info
*info
)
971 free_cvinfo( info
->cvinfo
);
977 LRESULT WINAPI
ICCVID_DriverProc( DWORD_PTR dwDriverId
, HDRVR hdrvr
, UINT msg
,
978 LPARAM lParam1
, LPARAM lParam2
)
980 ICCVID_Info
*info
= (ICCVID_Info
*) dwDriverId
;
982 TRACE("%ld %p %d %ld %ld\n", dwDriverId
, hdrvr
, msg
, lParam1
, lParam2
);
998 ICINFO
*icinfo
= (ICINFO
*)lParam2
;
1002 if (icinfo
&& compare_fourcc(icinfo
->fccType
, ICTYPE_VIDEO
)) return 0;
1004 info
= heap_alloc( sizeof (ICCVID_Info
) );
1007 info
->dwMagic
= ICCVID_MAGIC
;
1008 info
->cvinfo
= NULL
;
1010 return (LRESULT
) info
;
1014 return ICCVID_Close( info
);
1017 return ICCVID_GetInfo( info
, (ICINFO
*)lParam1
, (DWORD
)lParam2
);
1019 case ICM_DECOMPRESS_QUERY
:
1020 return ICCVID_DecompressQuery( info
, (LPBITMAPINFO
) lParam1
,
1021 (LPBITMAPINFO
) lParam2
);
1022 case ICM_DECOMPRESS_GET_FORMAT
:
1023 return ICCVID_DecompressGetFormat( info
, (LPBITMAPINFO
) lParam1
,
1024 (LPBITMAPINFO
) lParam2
);
1025 case ICM_DECOMPRESS_BEGIN
:
1026 return ICCVID_DecompressBegin( info
, (LPBITMAPINFO
) lParam1
,
1027 (LPBITMAPINFO
) lParam2
);
1028 case ICM_DECOMPRESS
:
1029 return ICCVID_Decompress( info
, (ICDECOMPRESS
*) lParam1
,
1031 case ICM_DECOMPRESSEX
:
1032 return ICCVID_DecompressEx( info
, (ICDECOMPRESSEX
*) lParam1
,
1035 case ICM_DECOMPRESS_END
:
1036 return ICCVID_DecompressEnd( info
);
1038 case ICM_COMPRESS_QUERY
:
1039 FIXME("compression not implemented\n");
1040 return ICERR_BADFORMAT
;
1043 return ICERR_UNSUPPORTED
;
1046 FIXME("Unknown message: %04x %ld %ld\n", msg
, lParam1
, lParam2
);
1048 return ICERR_UNSUPPORTED
;
1051 BOOL WINAPI
DllMain(HINSTANCE hModule
, DWORD dwReason
, LPVOID lpReserved
)
1053 TRACE("(%p,%d,%p)\n", hModule
, dwReason
, lpReserved
);
1057 case DLL_PROCESS_ATTACH
:
1058 DisableThreadLibraryCalls(hModule
);
1059 ICCVID_hModule
= hModule
;