devenum: Implement IMoniker::GetClassID().
[wine.git] / dlls / iccvid / iccvid.c
blobe5d514703ee73d5df1d85395b2759a736c378193
1 /*
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 * ------------------------------------------------------------------------ */
43 #include <stdarg.h>
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wingdi.h"
47 #include "winuser.h"
48 #include "commdlg.h"
49 #include "vfw.h"
50 #include "mmsystem.h"
51 #include "iccvid_private.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(iccvid);
57 static HINSTANCE ICCVID_hModule;
59 #define ICCVID_MAGIC mmioFOURCC('c', 'v', 'i', 'd')
60 #define compare_fourcc(fcc1, fcc2) (((fcc1)^(fcc2))&~0x20202020)
61 #define MAX_STRIPS 32
63 /* ------------------------------------------------------------------------ */
64 typedef struct
66 unsigned char y0, y1, y2, y3;
67 char u, v;
68 unsigned char r[4], g[4], b[4];
69 } cvid_codebook;
71 typedef struct {
72 cvid_codebook *v4_codebook[MAX_STRIPS];
73 cvid_codebook *v1_codebook[MAX_STRIPS];
74 unsigned int strip_num;
75 } cinepak_info;
77 typedef struct _ICCVID_Info
79 DWORD dwMagic;
80 int bits_per_pixel;
81 cinepak_info *cvinfo;
82 } ICCVID_Info;
84 static inline void* __WINE_ALLOC_SIZE(1) heap_alloc(size_t size)
86 return HeapAlloc(GetProcessHeap(), 0, size);
89 static inline BOOL heap_free(void *mem)
91 return HeapFree(GetProcessHeap(), 0, mem);
95 /* ------------------------------------------------------------------------ */
96 static unsigned char *in_buffer, uiclip[1024], *uiclp = NULL;
98 #define get_byte() *(in_buffer++)
99 #define skip_byte() in_buffer++
100 #define get_word() ((unsigned short)(in_buffer += 2, \
101 (in_buffer[-2] << 8 | in_buffer[-1])))
102 #define get_long() ((unsigned long)(in_buffer += 4, \
103 (in_buffer[-4] << 24 | in_buffer[-3] << 16 | in_buffer[-2] << 8 | in_buffer[-1])))
106 /* ---------------------------------------------------------------------- */
107 static inline void read_codebook(cvid_codebook *c, int mode)
109 int uvr, uvg, uvb;
111 if(mode) /* black and white */
113 c->y0 = get_byte();
114 c->y1 = get_byte();
115 c->y2 = get_byte();
116 c->y3 = get_byte();
117 c->u = c->v = 0;
119 c->r[0] = c->g[0] = c->b[0] = c->y0;
120 c->r[1] = c->g[1] = c->b[1] = c->y1;
121 c->r[2] = c->g[2] = c->b[2] = c->y2;
122 c->r[3] = c->g[3] = c->b[3] = c->y3;
124 else /* colour */
126 c->y0 = get_byte(); /* luma */
127 c->y1 = get_byte();
128 c->y2 = get_byte();
129 c->y3 = get_byte();
130 c->u = get_byte(); /* chroma */
131 c->v = get_byte();
133 uvr = c->v << 1;
134 uvg = -((c->u+1) >> 1) - c->v;
135 uvb = c->u << 1;
137 c->r[0] = uiclp[c->y0 + uvr]; c->g[0] = uiclp[c->y0 + uvg]; c->b[0] = uiclp[c->y0 + uvb];
138 c->r[1] = uiclp[c->y1 + uvr]; c->g[1] = uiclp[c->y1 + uvg]; c->b[1] = uiclp[c->y1 + uvb];
139 c->r[2] = uiclp[c->y2 + uvr]; c->g[2] = uiclp[c->y2 + uvg]; c->b[2] = uiclp[c->y2 + uvb];
140 c->r[3] = uiclp[c->y3 + uvr]; c->g[3] = uiclp[c->y3 + uvg]; c->b[3] = uiclp[c->y3 + uvb];
144 static inline long get_addr(BOOL inverted, unsigned long x, unsigned long y,
145 int frm_stride, int bpp, unsigned int out_height)
147 /* Returns the starting position of a line from top-down or bottom-up */
148 if (inverted)
149 return y * frm_stride + x * bpp;
150 else
151 return (out_height - 1 - y) * frm_stride + x * bpp;
154 #define MAKECOLOUR32(r,g,b) (((r) << 16) | ((g) << 8) | (b))
155 /*#define MAKECOLOUR24(r,g,b) (((r) << 16) | ((g) << 8) | (b))*/
156 #define MAKECOLOUR16(r,g,b) (((r) >> 3) << 11)| (((g) >> 2) << 5)| (((b) >> 3) << 0)
157 #define MAKECOLOUR15(r,g,b) (((r) >> 3) << 10)| (((g) >> 3) << 5)| (((b) >> 3) << 0)
159 /* ------------------------------------------------------------------------ */
160 static void cvid_v1_32(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
161 cvid_codebook *cb)
163 unsigned long *vptr = (unsigned long *)frm;
164 int row_inc;
165 int x, y;
167 if (!inverted)
168 row_inc = -stride/4;
169 else
170 row_inc = stride/4;
172 /* fill 4x4 block of pixels with colour values from codebook */
173 for (y = 0; y < 4; y++)
175 if (&vptr[y*row_inc] < (unsigned long *)limit) return;
176 for (x = 0; x < 4; x++)
177 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]);
182 /* ------------------------------------------------------------------------ */
183 static void cvid_v4_32(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
184 cvid_codebook *cb0, cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
186 unsigned long *vptr = (unsigned long *)frm;
187 int row_inc;
188 int x, y;
189 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
191 if (!inverted)
192 row_inc = -stride/4;
193 else
194 row_inc = stride/4;
196 /* fill 4x4 block of pixels with colour values from codebooks */
197 for (y = 0; y < 4; y++)
199 if (&vptr[y*row_inc] < (unsigned long *)limit) return;
200 for (x = 0; x < 4; x++)
201 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]);
206 /* ------------------------------------------------------------------------ */
207 static void cvid_v1_24(unsigned char *vptr, unsigned char *limit, int stride, BOOL inverted,
208 cvid_codebook *cb)
210 int row_inc;
211 int x, y;
213 if (!inverted)
214 row_inc = -stride;
215 else
216 row_inc = stride;
218 /* fill 4x4 block of pixels with colour values from codebook */
219 for (y = 0; y < 4; y++)
221 if (&vptr[y*row_inc] < limit) return;
222 for (x = 0; x < 4; x++)
224 vptr[y*row_inc + x*3 + 0] = cb->b[x/2+(y/2)*2];
225 vptr[y*row_inc + x*3 + 1] = cb->g[x/2+(y/2)*2];
226 vptr[y*row_inc + x*3 + 2] = cb->r[x/2+(y/2)*2];
232 /* ------------------------------------------------------------------------ */
233 static void cvid_v4_24(unsigned char *vptr, unsigned char *limit, int stride, BOOL inverted,
234 cvid_codebook *cb0, cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
236 int row_inc;
237 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
238 int x, y;
240 if (!inverted)
241 row_inc = -stride;
242 else
243 row_inc = stride;
245 /* fill 4x4 block of pixels with colour values from codebooks */
246 for (y = 0; y < 4; y++)
248 if (&vptr[y*row_inc] < limit) return;
249 for (x = 0; x < 4; x++)
251 vptr[y*row_inc + x*3 + 0] = cb[x/2+(y/2)*2]->b[x%2+(y%2)*2];
252 vptr[y*row_inc + x*3 + 1] = cb[x/2+(y/2)*2]->g[x%2+(y%2)*2];
253 vptr[y*row_inc + x*3 + 2] = cb[x/2+(y/2)*2]->r[x%2+(y%2)*2];
259 /* ------------------------------------------------------------------------ */
260 static void cvid_v1_16(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
261 cvid_codebook *cb)
263 unsigned short *vptr = (unsigned short *)frm;
264 int row_inc;
265 int x, y;
267 if (!inverted)
268 row_inc = -stride/2;
269 else
270 row_inc = stride/2;
272 /* fill 4x4 block of pixels with colour values from codebook */
273 for (y = 0; y < 4; y++)
275 if (&vptr[y*row_inc] < (unsigned short *)limit) return;
276 for (x = 0; x < 4; x++)
277 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]);
282 /* ------------------------------------------------------------------------ */
283 static void cvid_v4_16(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
284 cvid_codebook *cb0, cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
286 unsigned short *vptr = (unsigned short *)frm;
287 int row_inc;
288 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
289 int x, y;
291 if (!inverted)
292 row_inc = -stride/2;
293 else
294 row_inc = stride/2;
296 /* fill 4x4 block of pixels with colour values from codebooks */
297 for (y = 0; y < 4; y++)
299 if (&vptr[y*row_inc] < (unsigned short *)limit) return;
300 for (x = 0; x < 4; x++)
301 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]);
305 /* ------------------------------------------------------------------------ */
306 static void cvid_v1_15(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
307 cvid_codebook *cb)
309 unsigned short *vptr = (unsigned short *)frm;
310 int row_inc;
311 int x, y;
313 if (!inverted)
314 row_inc = -stride/2;
315 else
316 row_inc = stride/2;
318 /* fill 4x4 block of pixels with colour values from codebook */
319 for (y = 0; y < 4; y++)
321 if (&vptr[y*row_inc] < (unsigned short *)limit) return;
322 for (x = 0; x < 4; x++)
323 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]);
328 /* ------------------------------------------------------------------------ */
329 static void cvid_v4_15(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
330 cvid_codebook *cb0, cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
332 unsigned short *vptr = (unsigned short *)frm;
333 int row_inc;
334 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
335 int x, y;
337 if (!inverted)
338 row_inc = -stride/2;
339 else
340 row_inc = stride/2;
342 /* fill 4x4 block of pixels with colour values from codebooks */
343 for (y = 0; y < 4; y++)
345 if (&vptr[y*row_inc] < (unsigned short *)limit) return;
346 for (x = 0; x < 4; x++)
347 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]);
352 /* ------------------------------------------------------------------------
353 * Call this function once at the start of the sequence and save the
354 * returned context for calls to decode_cinepak().
356 static cinepak_info *decode_cinepak_init(void)
358 cinepak_info *cvinfo;
359 int i;
361 cvinfo = heap_alloc( sizeof (cinepak_info) );
362 if( !cvinfo )
363 return NULL;
364 cvinfo->strip_num = 0;
366 if(uiclp == NULL)
368 uiclp = uiclip+512;
369 for(i = -512; i < 512; i++)
370 uiclp[i] = (i < 0 ? 0 : (i > 255 ? 255 : i));
373 return cvinfo;
376 static void free_cvinfo( cinepak_info *cvinfo )
378 unsigned int i;
380 for( i=0; i<cvinfo->strip_num; i++ )
382 heap_free(cvinfo->v4_codebook[i]);
383 heap_free(cvinfo->v1_codebook[i]);
385 heap_free( cvinfo );
388 typedef void (*fn_cvid_v1)(unsigned char *frm, unsigned char *limit,
389 int stride, BOOL inverted, cvid_codebook *cb);
390 typedef void (*fn_cvid_v4)(unsigned char *frm, unsigned char *limit,
391 int stride, BOOL inverted,
392 cvid_codebook *cb0, cvid_codebook *cb1,
393 cvid_codebook *cb2, cvid_codebook *cb3);
395 /* ------------------------------------------------------------------------
396 * This function decodes a buffer containing a Cinepak encoded frame.
398 * context - the context created by decode_cinepak_init().
399 * buf - the input buffer to be decoded
400 * size - the size of the input buffer
401 * output - the output frame buffer (24 or 32 bit per pixel)
402 * out_width - the width of the output frame
403 * out_height - the height of the output frame
404 * bit_per_pixel - the number of bits per pixel allocated to the output
405 * frame (only 24 or 32 bpp are supported)
406 * inverted - if true the output frame is written top-down
408 static void decode_cinepak(cinepak_info *cvinfo, unsigned char *buf, int size,
409 unsigned char *output, unsigned int out_width, unsigned int out_height, int bit_per_pixel, BOOL inverted)
411 cvid_codebook *v4_codebook, *v1_codebook, *codebook = NULL;
412 unsigned long x, y, y_bottom, cnum, strip_id, chunk_id,
413 x0, y0, x1, y1, ci, flag, mask;
414 long top_size, chunk_size;
415 unsigned char *frm_ptr;
416 unsigned int i, cur_strip, addr;
417 int d0, d1, d2, d3, frm_stride, bpp = 3;
418 fn_cvid_v1 cvid_v1 = cvid_v1_24;
419 fn_cvid_v4 cvid_v4 = cvid_v4_24;
420 struct frame_header
422 unsigned char flags;
423 unsigned long length;
424 unsigned short width;
425 unsigned short height;
426 unsigned short strips;
427 } frame;
429 y = 0;
430 y_bottom = 0;
431 in_buffer = buf;
433 frame.flags = get_byte();
434 frame.length = get_byte() << 16;
435 frame.length |= get_byte() << 8;
436 frame.length |= get_byte();
438 switch(bit_per_pixel)
440 case 15:
441 bpp = 2;
442 cvid_v1 = cvid_v1_15;
443 cvid_v4 = cvid_v4_15;
444 break;
445 case 16:
446 bpp = 2;
447 cvid_v1 = cvid_v1_16;
448 cvid_v4 = cvid_v4_16;
449 break;
450 case 24:
451 bpp = 3;
452 cvid_v1 = cvid_v1_24;
453 cvid_v4 = cvid_v4_24;
454 break;
455 case 32:
456 bpp = 4;
457 cvid_v1 = cvid_v1_32;
458 cvid_v4 = cvid_v4_32;
459 break;
462 frm_stride = out_width * bpp;
463 frm_ptr = output;
465 if(frame.length != size)
467 if(frame.length & 0x01) frame.length++; /* AVIs tend to have a size mismatch */
468 if(frame.length != size)
470 ERR("CVID: corruption %d (QT/AVI) != %ld (CV)\n", size, frame.length);
471 /* return; */
475 frame.width = get_word();
476 frame.height = get_word();
477 frame.strips = get_word();
479 if(frame.strips > cvinfo->strip_num)
481 if(frame.strips >= MAX_STRIPS)
483 ERR("CVID: strip overflow (more than %d)\n", MAX_STRIPS);
484 return;
487 for(i = cvinfo->strip_num; i < frame.strips; i++)
489 if((cvinfo->v4_codebook[i] = heap_alloc(sizeof(cvid_codebook) * 260)) == NULL)
491 ERR("CVID: codebook v4 alloc err\n");
492 return;
495 if((cvinfo->v1_codebook[i] = heap_alloc(sizeof(cvid_codebook) * 260)) == NULL)
497 ERR("CVID: codebook v1 alloc err\n");
498 return;
502 cvinfo->strip_num = frame.strips;
504 TRACE("CVID: %ux%u, strips %u, length %lu\n",
505 frame.width, frame.height, frame.strips, frame.length);
507 for(cur_strip = 0; cur_strip < frame.strips; cur_strip++)
509 v4_codebook = cvinfo->v4_codebook[cur_strip];
510 v1_codebook = cvinfo->v1_codebook[cur_strip];
512 if((cur_strip > 0) && (!(frame.flags & 0x01)))
514 memcpy(cvinfo->v4_codebook[cur_strip], cvinfo->v4_codebook[cur_strip-1], 260 * sizeof(cvid_codebook));
515 memcpy(cvinfo->v1_codebook[cur_strip], cvinfo->v1_codebook[cur_strip-1], 260 * sizeof(cvid_codebook));
518 strip_id = get_word(); /* 1000 = key strip, 1100 = iter strip */
519 top_size = get_word();
520 y0 = get_word(); /* FIXME: most of these are ignored at the moment */
521 x0 = get_word();
522 y1 = get_word();
523 x1 = get_word();
525 y_bottom += y1;
526 top_size -= 12;
527 x = 0;
528 if(x1 != out_width)
529 WARN("CVID: Warning x1 (%ld) != width (%d)\n", x1, out_width);
531 TRACE(" %d) %04lx %04ld <%ld,%ld> <%ld,%ld> yt %ld\n",
532 cur_strip, strip_id, top_size, x0, y0, x1, y1, y_bottom);
534 while(top_size > 0)
536 chunk_id = get_word();
537 chunk_size = get_word();
539 TRACE(" %04lx %04ld\n", chunk_id, chunk_size);
540 top_size -= chunk_size;
541 chunk_size -= 4;
543 switch(chunk_id)
545 /* -------------------- Codebook Entries -------------------- */
546 case 0x2000:
547 case 0x2200:
548 codebook = (chunk_id == 0x2200 ? v1_codebook : v4_codebook);
549 cnum = chunk_size/6;
550 for(i = 0; i < cnum; i++) read_codebook(codebook+i, 0);
551 break;
553 case 0x2400:
554 case 0x2600: /* 8 bit per pixel */
555 codebook = (chunk_id == 0x2600 ? v1_codebook : v4_codebook);
556 cnum = chunk_size/4;
557 for(i = 0; i < cnum; i++) read_codebook(codebook+i, 1);
558 break;
560 case 0x2100:
561 case 0x2300:
562 codebook = (chunk_id == 0x2300 ? v1_codebook : v4_codebook);
564 ci = 0;
565 while(chunk_size > 0)
567 flag = get_long();
568 chunk_size -= 4;
570 for(i = 0; i < 32; i++)
572 if(flag & 0x80000000)
574 chunk_size -= 6;
575 read_codebook(codebook+ci, 0);
578 ci++;
579 flag <<= 1;
582 while(chunk_size > 0) { skip_byte(); chunk_size--; }
583 break;
585 case 0x2500:
586 case 0x2700: /* 8 bit per pixel */
587 codebook = (chunk_id == 0x2700 ? v1_codebook : v4_codebook);
589 ci = 0;
590 while(chunk_size > 0)
592 flag = get_long();
593 chunk_size -= 4;
595 for(i = 0; i < 32; i++)
597 if(flag & 0x80000000)
599 chunk_size -= 4;
600 read_codebook(codebook+ci, 1);
603 ci++;
604 flag <<= 1;
607 while(chunk_size > 0) { skip_byte(); chunk_size--; }
608 break;
610 /* -------------------- Frame -------------------- */
611 case 0x3000:
612 while((chunk_size > 0) && (y < y_bottom))
614 flag = get_long();
615 chunk_size -= 4;
617 for(i = 0; i < 32; i++)
619 if(y >= y_bottom) break;
620 if(flag & 0x80000000) /* 4 bytes per block */
622 d0 = get_byte();
623 d1 = get_byte();
624 d2 = get_byte();
625 d3 = get_byte();
626 chunk_size -= 4;
628 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
629 cvid_v4(frm_ptr + addr, output, frm_stride, inverted, v4_codebook+d0, v4_codebook+d1, v4_codebook+d2, v4_codebook+d3);
631 else /* 1 byte per block */
633 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
634 cvid_v1(frm_ptr + addr, output, frm_stride, inverted, v1_codebook + get_byte());
636 chunk_size--;
639 x += 4;
640 if(x >= out_width)
642 x = 0;
643 y += 4;
645 flag <<= 1;
648 while(chunk_size > 0) { skip_byte(); chunk_size--; }
649 break;
651 case 0x3100:
652 while((chunk_size > 0) && (y < y_bottom))
654 /* ---- flag bits: 0 = SKIP, 10 = V1, 11 = V4 ---- */
655 flag = get_long();
656 chunk_size -= 4;
657 mask = 0x80000000;
659 while((mask) && (y < y_bottom))
661 if(flag & mask)
663 if(mask == 1)
665 if(chunk_size < 0) break;
666 flag = get_long();
667 chunk_size -= 4;
668 mask = 0x80000000;
670 else mask >>= 1;
672 if(flag & mask) /* V4 */
674 d0 = get_byte();
675 d1 = get_byte();
676 d2 = get_byte();
677 d3 = get_byte();
678 chunk_size -= 4;
680 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
681 cvid_v4(frm_ptr + addr, output, frm_stride, inverted, v4_codebook+d0, v4_codebook+d1, v4_codebook+d2, v4_codebook+d3);
683 else /* V1 */
685 chunk_size--;
687 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
688 cvid_v1(frm_ptr + addr, output, frm_stride, inverted, v1_codebook + get_byte());
690 } /* else SKIP */
692 mask >>= 1;
693 x += 4;
694 if(x >= out_width)
696 x = 0;
697 y += 4;
702 while(chunk_size > 0) { skip_byte(); chunk_size--; }
703 break;
705 case 0x3200: /* each byte is a V1 codebook */
706 while((chunk_size > 0) && (y < y_bottom))
708 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
709 cvid_v1(frm_ptr + addr, output, frm_stride, inverted, v1_codebook + get_byte());
711 chunk_size--;
712 x += 4;
713 if(x >= out_width)
715 x = 0;
716 y += 4;
719 while(chunk_size > 0) { skip_byte(); chunk_size--; }
720 break;
722 default:
723 ERR("CVID: unknown chunk_id %08lx\n", chunk_id);
724 while(chunk_size > 0) { skip_byte(); chunk_size--; }
725 break;
730 if(frame.length != size)
732 if(frame.length & 0x01) frame.length++; /* AVIs tend to have a size mismatch */
733 if(frame.length != size)
735 long xlen;
736 skip_byte();
737 xlen = get_byte() << 16;
738 xlen |= get_byte() << 8;
739 xlen |= get_byte(); /* Read Len */
740 WARN("CVID: END INFO chunk size %d cvid size1 %ld cvid size2 %ld\n",
741 size, frame.length, xlen);
746 static void ICCVID_dump_BITMAPINFO(const BITMAPINFO * bmi)
748 TRACE(
749 "planes = %d\n"
750 "bpp = %d\n"
751 "height = %d\n"
752 "width = %d\n"
753 "compr = %s\n",
754 bmi->bmiHeader.biPlanes,
755 bmi->bmiHeader.biBitCount,
756 bmi->bmiHeader.biHeight,
757 bmi->bmiHeader.biWidth,
758 debugstr_an( (const char *)&bmi->bmiHeader.biCompression, 4 ) );
761 static inline int ICCVID_CheckMask(RGBQUAD bmiColors[3], COLORREF redMask, COLORREF blueMask, COLORREF greenMask)
763 COLORREF realRedMask = MAKECOLOUR32(bmiColors[0].rgbRed, bmiColors[0].rgbGreen, bmiColors[0].rgbBlue);
764 COLORREF realBlueMask = MAKECOLOUR32(bmiColors[1].rgbRed, bmiColors[1].rgbGreen, bmiColors[1].rgbBlue);
765 COLORREF realGreenMask = MAKECOLOUR32(bmiColors[2].rgbRed, bmiColors[2].rgbGreen, bmiColors[2].rgbBlue);
767 TRACE("\nbmiColors[0] = 0x%08x\nbmiColors[1] = 0x%08x\nbmiColors[2] = 0x%08x\n",
768 realRedMask, realBlueMask, realGreenMask);
770 if ((realRedMask == redMask) &&
771 (realBlueMask == blueMask) &&
772 (realGreenMask == greenMask))
773 return TRUE;
774 return FALSE;
777 static LRESULT ICCVID_DecompressQuery( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
779 TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info, in, out);
781 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
782 return ICERR_BADPARAM;
784 TRACE("in: ");
785 ICCVID_dump_BITMAPINFO(in);
787 if( in->bmiHeader.biCompression != ICCVID_MAGIC )
788 return ICERR_BADFORMAT;
790 if( out )
792 TRACE("out: ");
793 ICCVID_dump_BITMAPINFO(out);
795 if( in->bmiHeader.biPlanes != out->bmiHeader.biPlanes )
796 return ICERR_BADFORMAT;
797 if( in->bmiHeader.biHeight != out->bmiHeader.biHeight )
799 if( in->bmiHeader.biHeight != -out->bmiHeader.biHeight )
800 return ICERR_BADFORMAT;
801 TRACE("Detected inverted height for video output\n");
803 if( in->bmiHeader.biWidth != out->bmiHeader.biWidth )
804 return ICERR_BADFORMAT;
806 switch( out->bmiHeader.biBitCount )
808 case 16:
809 if ( out->bmiHeader.biCompression == BI_BITFIELDS )
811 if ( !ICCVID_CheckMask(out->bmiColors, 0x7C00, 0x03E0, 0x001F) &&
812 !ICCVID_CheckMask(out->bmiColors, 0xF800, 0x07E0, 0x001F) )
814 TRACE("unsupported output bit field(s) for 16-bit colors\n");
815 return ICERR_BADFORMAT;
818 break;
819 case 24:
820 case 32:
821 break;
822 default:
823 TRACE("unsupported output bitcount = %d\n", out->bmiHeader.biBitCount );
824 return ICERR_BADFORMAT;
828 return ICERR_OK;
831 static LRESULT ICCVID_DecompressGetFormat( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
833 DWORD size;
835 TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info, in, out);
837 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
838 return ICERR_BADPARAM;
840 size = in->bmiHeader.biSize;
841 if (in->bmiHeader.biBitCount <= 8)
842 size += in->bmiHeader.biClrUsed * sizeof(RGBQUAD);
844 if( out )
846 memcpy( out, in, size );
847 out->bmiHeader.biCompression = BI_RGB;
848 out->bmiHeader.biSizeImage = in->bmiHeader.biHeight
849 * in->bmiHeader.biWidth *4;
850 return ICERR_OK;
852 return size;
855 static LRESULT ICCVID_DecompressBegin( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
857 TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info, in, out);
859 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
860 return ICERR_BADPARAM;
862 info->bits_per_pixel = out->bmiHeader.biBitCount;
864 if (info->bits_per_pixel == 16)
866 if ( out->bmiHeader.biCompression == BI_BITFIELDS )
868 if ( ICCVID_CheckMask(out->bmiColors, 0x7C00, 0x03E0, 0x001F) )
869 info->bits_per_pixel = 15;
870 else if ( ICCVID_CheckMask(out->bmiColors, 0xF800, 0x07E0, 0x001F) )
871 info->bits_per_pixel = 16;
872 else
874 TRACE("unsupported output bit field(s) for 16-bit colors\n");
875 return ICERR_UNSUPPORTED;
878 else
879 info->bits_per_pixel = 15;
882 TRACE("bit_per_pixel = %d\n", info->bits_per_pixel);
884 if( info->cvinfo )
885 free_cvinfo( info->cvinfo );
886 info->cvinfo = decode_cinepak_init();
888 return ICERR_OK;
891 static LRESULT ICCVID_Decompress( ICCVID_Info *info, ICDECOMPRESS *icd, DWORD size )
893 LONG width, height;
894 BOOL inverted;
896 TRACE("ICM_DECOMPRESS %p %p %d\n", info, icd, size);
898 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
899 return ICERR_BADPARAM;
900 if (info->cvinfo==NULL)
902 ERR("ICM_DECOMPRESS sent after ICM_DECOMPRESS_END\n");
903 return ICERR_BADPARAM;
906 width = icd->lpbiInput->biWidth;
907 height = icd->lpbiInput->biHeight;
908 inverted = -icd->lpbiOutput->biHeight == height;
910 decode_cinepak(info->cvinfo, icd->lpInput, icd->lpbiInput->biSizeImage,
911 icd->lpOutput, width, height, info->bits_per_pixel, inverted);
913 return ICERR_OK;
916 static LRESULT ICCVID_DecompressEx( ICCVID_Info *info, ICDECOMPRESSEX *icd, DWORD size )
918 LONG width, height;
919 BOOL inverted;
921 TRACE("ICM_DECOMPRESSEX %p %p %d\n", info, icd, size);
923 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
924 return ICERR_BADPARAM;
925 if (info->cvinfo==NULL)
927 ERR("ICM_DECOMPRESSEX sent after ICM_DECOMPRESS_END\n");
928 return ICERR_BADPARAM;
931 /* FIXME: flags are ignored */
933 width = icd->lpbiSrc->biWidth;
934 height = icd->lpbiSrc->biHeight;
935 inverted = -icd->lpbiDst->biHeight == height;
937 decode_cinepak(info->cvinfo, icd->lpSrc, icd->lpbiSrc->biSizeImage,
938 icd->lpDst, width, height, info->bits_per_pixel, inverted);
940 return ICERR_OK;
943 static LRESULT ICCVID_Close( ICCVID_Info *info )
945 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
946 return 0;
947 if( info->cvinfo )
948 free_cvinfo( info->cvinfo );
949 heap_free( info );
950 return 1;
953 static LRESULT ICCVID_GetInfo( ICCVID_Info *info, ICINFO *icinfo, DWORD dwSize )
955 if (!icinfo) return sizeof(ICINFO);
956 if (dwSize < sizeof(ICINFO)) return 0;
958 icinfo->dwSize = sizeof(ICINFO);
959 icinfo->fccType = ICTYPE_VIDEO;
960 icinfo->fccHandler = info ? info->dwMagic : ICCVID_MAGIC;
961 icinfo->dwFlags = 0;
962 icinfo->dwVersion = ICVERSION;
963 icinfo->dwVersionICM = ICVERSION;
965 LoadStringW(ICCVID_hModule, IDS_NAME, icinfo->szName, sizeof(icinfo->szName)/sizeof(WCHAR));
966 LoadStringW(ICCVID_hModule, IDS_DESCRIPTION, icinfo->szDescription, sizeof(icinfo->szDescription)/sizeof(WCHAR));
967 /* msvfw32 will fill icinfo->szDriver for us */
969 return sizeof(ICINFO);
972 static LRESULT ICCVID_DecompressEnd( ICCVID_Info *info )
974 if( info->cvinfo )
976 free_cvinfo( info->cvinfo );
977 info->cvinfo = NULL;
979 return ICERR_OK;
982 LRESULT WINAPI ICCVID_DriverProc( DWORD_PTR dwDriverId, HDRVR hdrvr, UINT msg,
983 LPARAM lParam1, LPARAM lParam2)
985 ICCVID_Info *info = (ICCVID_Info *) dwDriverId;
987 TRACE("%ld %p %d %ld %ld\n", dwDriverId, hdrvr, msg, lParam1, lParam2);
989 switch( msg )
991 case DRV_LOAD:
992 TRACE("Loaded\n");
993 return 1;
994 case DRV_ENABLE:
995 return 0;
996 case DRV_DISABLE:
997 return 0;
998 case DRV_FREE:
999 return 0;
1001 case DRV_OPEN:
1003 ICINFO *icinfo = (ICINFO *)lParam2;
1005 TRACE("Opened\n");
1007 if (icinfo && compare_fourcc(icinfo->fccType, ICTYPE_VIDEO)) return 0;
1009 info = heap_alloc( sizeof (ICCVID_Info) );
1010 if( info )
1012 info->dwMagic = ICCVID_MAGIC;
1013 info->cvinfo = NULL;
1015 return (LRESULT) info;
1018 case DRV_CLOSE:
1019 return ICCVID_Close( info );
1021 case ICM_GETINFO:
1022 return ICCVID_GetInfo( info, (ICINFO *)lParam1, (DWORD)lParam2 );
1024 case ICM_DECOMPRESS_QUERY:
1025 return ICCVID_DecompressQuery( info, (LPBITMAPINFO) lParam1,
1026 (LPBITMAPINFO) lParam2 );
1027 case ICM_DECOMPRESS_GET_FORMAT:
1028 return ICCVID_DecompressGetFormat( info, (LPBITMAPINFO) lParam1,
1029 (LPBITMAPINFO) lParam2 );
1030 case ICM_DECOMPRESS_BEGIN:
1031 return ICCVID_DecompressBegin( info, (LPBITMAPINFO) lParam1,
1032 (LPBITMAPINFO) lParam2 );
1033 case ICM_DECOMPRESS:
1034 return ICCVID_Decompress( info, (ICDECOMPRESS*) lParam1,
1035 (DWORD) lParam2 );
1036 case ICM_DECOMPRESSEX:
1037 return ICCVID_DecompressEx( info, (ICDECOMPRESSEX*) lParam1,
1038 (DWORD) lParam2 );
1040 case ICM_DECOMPRESS_END:
1041 return ICCVID_DecompressEnd( info );
1043 case ICM_COMPRESS_QUERY:
1044 FIXME("compression not implemented\n");
1045 return ICERR_BADFORMAT;
1047 case ICM_CONFIGURE:
1048 return ICERR_UNSUPPORTED;
1050 default:
1051 FIXME("Unknown message: %04x %ld %ld\n", msg, lParam1, lParam2);
1053 return ICERR_UNSUPPORTED;
1056 BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
1058 TRACE("(%p,%d,%p)\n", hModule, dwReason, lpReserved);
1060 switch (dwReason)
1062 case DLL_PROCESS_ATTACH:
1063 DisableThreadLibraryCalls(hModule);
1064 ICCVID_hModule = hModule;
1065 break;
1067 return TRUE;