user32/tests: Add a test for custom dialog control data.
[wine.git] / dlls / iccvid / iccvid.c
blob89d282ba78f2a5a3f8fe630728e0aecc46c2d1ee
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"
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)
62 #define MAX_STRIPS 32
64 /* ------------------------------------------------------------------------ */
65 typedef struct
67 unsigned char y0, y1, y2, y3;
68 char u, v;
69 unsigned char r[4], g[4], b[4];
70 } cvid_codebook;
72 typedef struct {
73 cvid_codebook *v4_codebook[MAX_STRIPS];
74 cvid_codebook *v1_codebook[MAX_STRIPS];
75 unsigned int strip_num;
76 } cinepak_info;
78 typedef struct _ICCVID_Info
80 DWORD dwMagic;
81 int bits_per_pixel;
82 cinepak_info *cvinfo;
83 } 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)
100 int uvr, uvg, uvb;
102 if(mode) /* black and white */
104 c->y0 = get_byte();
105 c->y1 = get_byte();
106 c->y2 = get_byte();
107 c->y3 = get_byte();
108 c->u = c->v = 0;
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;
115 else /* colour */
117 c->y0 = get_byte(); /* luma */
118 c->y1 = get_byte();
119 c->y2 = get_byte();
120 c->y3 = get_byte();
121 c->u = get_byte(); /* chroma */
122 c->v = get_byte();
124 uvr = c->v << 1;
125 uvg = -((c->u+1) >> 1) - c->v;
126 uvb = c->u << 1;
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 */
139 if (inverted)
140 return y * frm_stride + x * bpp;
141 else
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,
152 cvid_codebook *cb)
154 unsigned long *vptr = (unsigned long *)frm;
155 int row_inc;
156 int x, y;
158 if (!inverted)
159 row_inc = -stride/4;
160 else
161 row_inc = stride/4;
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]);
173 /* ------------------------------------------------------------------------ */
174 static void cvid_v4_32(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
175 cvid_codebook *cb0, cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
177 unsigned long *vptr = (unsigned long *)frm;
178 int row_inc;
179 int x, y;
180 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
182 if (!inverted)
183 row_inc = -stride/4;
184 else
185 row_inc = stride/4;
187 /* fill 4x4 block of pixels with colour values from codebooks */
188 for (y = 0; y < 4; y++)
190 if (&vptr[y*row_inc] < (unsigned long *)limit) return;
191 for (x = 0; x < 4; x++)
192 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]);
197 /* ------------------------------------------------------------------------ */
198 static void cvid_v1_24(unsigned char *vptr, unsigned char *limit, int stride, BOOL inverted,
199 cvid_codebook *cb)
201 int row_inc;
202 int x, y;
204 if (!inverted)
205 row_inc = -stride;
206 else
207 row_inc = stride;
209 /* fill 4x4 block of pixels with colour values from codebook */
210 for (y = 0; y < 4; y++)
212 if (&vptr[y*row_inc] < limit) return;
213 for (x = 0; x < 4; x++)
215 vptr[y*row_inc + x*3 + 0] = cb->b[x/2+(y/2)*2];
216 vptr[y*row_inc + x*3 + 1] = cb->g[x/2+(y/2)*2];
217 vptr[y*row_inc + x*3 + 2] = cb->r[x/2+(y/2)*2];
223 /* ------------------------------------------------------------------------ */
224 static void cvid_v4_24(unsigned char *vptr, unsigned char *limit, int stride, BOOL inverted,
225 cvid_codebook *cb0, cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
227 int row_inc;
228 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
229 int x, y;
231 if (!inverted)
232 row_inc = -stride;
233 else
234 row_inc = stride;
236 /* fill 4x4 block of pixels with colour values from codebooks */
237 for (y = 0; y < 4; y++)
239 if (&vptr[y*row_inc] < limit) return;
240 for (x = 0; x < 4; x++)
242 vptr[y*row_inc + x*3 + 0] = cb[x/2+(y/2)*2]->b[x%2+(y%2)*2];
243 vptr[y*row_inc + x*3 + 1] = cb[x/2+(y/2)*2]->g[x%2+(y%2)*2];
244 vptr[y*row_inc + x*3 + 2] = cb[x/2+(y/2)*2]->r[x%2+(y%2)*2];
250 /* ------------------------------------------------------------------------ */
251 static void cvid_v1_16(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
252 cvid_codebook *cb)
254 unsigned short *vptr = (unsigned short *)frm;
255 int row_inc;
256 int x, y;
258 if (!inverted)
259 row_inc = -stride/2;
260 else
261 row_inc = stride/2;
263 /* fill 4x4 block of pixels with colour values from codebook */
264 for (y = 0; y < 4; y++)
266 if (&vptr[y*row_inc] < (unsigned short *)limit) return;
267 for (x = 0; x < 4; x++)
268 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]);
273 /* ------------------------------------------------------------------------ */
274 static void cvid_v4_16(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
275 cvid_codebook *cb0, cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
277 unsigned short *vptr = (unsigned short *)frm;
278 int row_inc;
279 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
280 int x, y;
282 if (!inverted)
283 row_inc = -stride/2;
284 else
285 row_inc = stride/2;
287 /* fill 4x4 block of pixels with colour values from codebooks */
288 for (y = 0; y < 4; y++)
290 if (&vptr[y*row_inc] < (unsigned short *)limit) return;
291 for (x = 0; x < 4; x++)
292 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]);
296 /* ------------------------------------------------------------------------ */
297 static void cvid_v1_15(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
298 cvid_codebook *cb)
300 unsigned short *vptr = (unsigned short *)frm;
301 int row_inc;
302 int x, y;
304 if (!inverted)
305 row_inc = -stride/2;
306 else
307 row_inc = stride/2;
309 /* fill 4x4 block of pixels with colour values from codebook */
310 for (y = 0; y < 4; y++)
312 if (&vptr[y*row_inc] < (unsigned short *)limit) return;
313 for (x = 0; x < 4; x++)
314 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]);
319 /* ------------------------------------------------------------------------ */
320 static void cvid_v4_15(unsigned char *frm, unsigned char *limit, int stride, BOOL inverted,
321 cvid_codebook *cb0, cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
323 unsigned short *vptr = (unsigned short *)frm;
324 int row_inc;
325 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
326 int x, y;
328 if (!inverted)
329 row_inc = -stride/2;
330 else
331 row_inc = stride/2;
333 /* fill 4x4 block of pixels with colour values from codebooks */
334 for (y = 0; y < 4; y++)
336 if (&vptr[y*row_inc] < (unsigned short *)limit) return;
337 for (x = 0; x < 4; x++)
338 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]);
343 /* ------------------------------------------------------------------------
344 * Call this function once at the start of the sequence and save the
345 * returned context for calls to decode_cinepak().
347 static cinepak_info *decode_cinepak_init(void)
349 cinepak_info *cvinfo;
350 int i;
352 cvinfo = heap_alloc( sizeof (cinepak_info) );
353 if( !cvinfo )
354 return NULL;
355 cvinfo->strip_num = 0;
357 if(uiclp == NULL)
359 uiclp = uiclip+512;
360 for(i = -512; i < 512; i++)
361 uiclp[i] = (i < 0 ? 0 : (i > 255 ? 255 : i));
364 return cvinfo;
367 static void free_cvinfo( cinepak_info *cvinfo )
369 unsigned int i;
371 for( i=0; i<cvinfo->strip_num; i++ )
373 heap_free(cvinfo->v4_codebook[i]);
374 heap_free(cvinfo->v1_codebook[i]);
376 heap_free( cvinfo );
379 typedef void (*fn_cvid_v1)(unsigned char *frm, unsigned char *limit,
380 int stride, BOOL inverted, cvid_codebook *cb);
381 typedef void (*fn_cvid_v4)(unsigned char *frm, unsigned char *limit,
382 int stride, BOOL inverted,
383 cvid_codebook *cb0, cvid_codebook *cb1,
384 cvid_codebook *cb2, cvid_codebook *cb3);
386 /* ------------------------------------------------------------------------
387 * This function decodes a buffer containing a Cinepak encoded frame.
389 * context - the context created by decode_cinepak_init().
390 * buf - the input buffer to be decoded
391 * size - the size of the input buffer
392 * output - the output frame buffer (24 or 32 bit per pixel)
393 * out_width - the width of the output frame
394 * out_height - the height of the output frame
395 * bit_per_pixel - the number of bits per pixel allocated to the output
396 * frame (only 24 or 32 bpp are supported)
397 * inverted - if true the output frame is written top-down
399 static void decode_cinepak(cinepak_info *cvinfo, unsigned char *buf, int size,
400 unsigned char *output, unsigned int out_width, unsigned int out_height, int bit_per_pixel, BOOL inverted)
402 cvid_codebook *v4_codebook, *v1_codebook, *codebook = NULL;
403 unsigned long x, y, y_bottom, cnum, strip_id, chunk_id,
404 x0, y0, x1, y1, ci, flag, mask;
405 long top_size, chunk_size;
406 unsigned char *frm_ptr;
407 unsigned int i, cur_strip, addr;
408 int d0, d1, d2, d3, frm_stride, bpp = 3;
409 fn_cvid_v1 cvid_v1 = cvid_v1_24;
410 fn_cvid_v4 cvid_v4 = cvid_v4_24;
411 struct frame_header
413 unsigned char flags;
414 unsigned long length;
415 unsigned short width;
416 unsigned short height;
417 unsigned short strips;
418 } frame;
420 y = 0;
421 y_bottom = 0;
422 in_buffer = buf;
424 frame.flags = get_byte();
425 frame.length = get_byte() << 16;
426 frame.length |= get_byte() << 8;
427 frame.length |= get_byte();
429 switch(bit_per_pixel)
431 case 15:
432 bpp = 2;
433 cvid_v1 = cvid_v1_15;
434 cvid_v4 = cvid_v4_15;
435 break;
436 case 16:
437 bpp = 2;
438 cvid_v1 = cvid_v1_16;
439 cvid_v4 = cvid_v4_16;
440 break;
441 case 24:
442 bpp = 3;
443 cvid_v1 = cvid_v1_24;
444 cvid_v4 = cvid_v4_24;
445 break;
446 case 32:
447 bpp = 4;
448 cvid_v1 = cvid_v1_32;
449 cvid_v4 = cvid_v4_32;
450 break;
453 frm_stride = out_width * bpp;
454 frm_ptr = output;
456 if(frame.length != size)
458 if(frame.length & 0x01) frame.length++; /* AVIs tend to have a size mismatch */
459 if(frame.length != size)
461 ERR("CVID: corruption %d (QT/AVI) != %ld (CV)\n", size, frame.length);
462 /* return; */
466 frame.width = get_word();
467 frame.height = get_word();
468 frame.strips = get_word();
470 if(frame.strips > cvinfo->strip_num)
472 if(frame.strips >= MAX_STRIPS)
474 ERR("CVID: strip overflow (more than %d)\n", MAX_STRIPS);
475 return;
478 for(i = cvinfo->strip_num; i < frame.strips; i++)
480 if((cvinfo->v4_codebook[i] = heap_alloc(sizeof(cvid_codebook) * 260)) == NULL)
482 ERR("CVID: codebook v4 alloc err\n");
483 return;
486 if((cvinfo->v1_codebook[i] = heap_alloc(sizeof(cvid_codebook) * 260)) == NULL)
488 ERR("CVID: codebook v1 alloc err\n");
489 return;
493 cvinfo->strip_num = frame.strips;
495 TRACE("CVID: %ux%u, strips %u, length %lu\n",
496 frame.width, frame.height, frame.strips, frame.length);
498 for(cur_strip = 0; cur_strip < frame.strips; cur_strip++)
500 v4_codebook = cvinfo->v4_codebook[cur_strip];
501 v1_codebook = cvinfo->v1_codebook[cur_strip];
503 if((cur_strip > 0) && (!(frame.flags & 0x01)))
505 memcpy(cvinfo->v4_codebook[cur_strip], cvinfo->v4_codebook[cur_strip-1], 260 * sizeof(cvid_codebook));
506 memcpy(cvinfo->v1_codebook[cur_strip], cvinfo->v1_codebook[cur_strip-1], 260 * sizeof(cvid_codebook));
509 strip_id = get_word(); /* 1000 = key strip, 1100 = iter strip */
510 top_size = get_word();
511 y0 = get_word(); /* FIXME: most of these are ignored at the moment */
512 x0 = get_word();
513 y1 = get_word();
514 x1 = get_word();
516 y_bottom += y1;
517 top_size -= 12;
518 x = 0;
519 if(x1 != out_width)
520 WARN("CVID: Warning x1 (%ld) != width (%d)\n", x1, out_width);
522 TRACE(" %d) %04lx %04ld <%ld,%ld> <%ld,%ld> yt %ld\n",
523 cur_strip, strip_id, top_size, x0, y0, x1, y1, y_bottom);
525 while(top_size > 0)
527 chunk_id = get_word();
528 chunk_size = get_word();
530 TRACE(" %04lx %04ld\n", chunk_id, chunk_size);
531 top_size -= chunk_size;
532 chunk_size -= 4;
534 switch(chunk_id)
536 /* -------------------- Codebook Entries -------------------- */
537 case 0x2000:
538 case 0x2200:
539 codebook = (chunk_id == 0x2200 ? v1_codebook : v4_codebook);
540 cnum = chunk_size/6;
541 for(i = 0; i < cnum; i++) read_codebook(codebook+i, 0);
542 break;
544 case 0x2400:
545 case 0x2600: /* 8 bit per pixel */
546 codebook = (chunk_id == 0x2600 ? v1_codebook : v4_codebook);
547 cnum = chunk_size/4;
548 for(i = 0; i < cnum; i++) read_codebook(codebook+i, 1);
549 break;
551 case 0x2100:
552 case 0x2300:
553 codebook = (chunk_id == 0x2300 ? v1_codebook : v4_codebook);
555 ci = 0;
556 while(chunk_size > 0)
558 flag = get_long();
559 chunk_size -= 4;
561 for(i = 0; i < 32; i++)
563 if(flag & 0x80000000)
565 chunk_size -= 6;
566 read_codebook(codebook+ci, 0);
569 ci++;
570 flag <<= 1;
573 while(chunk_size > 0) { skip_byte(); chunk_size--; }
574 break;
576 case 0x2500:
577 case 0x2700: /* 8 bit per pixel */
578 codebook = (chunk_id == 0x2700 ? v1_codebook : v4_codebook);
580 ci = 0;
581 while(chunk_size > 0)
583 flag = get_long();
584 chunk_size -= 4;
586 for(i = 0; i < 32; i++)
588 if(flag & 0x80000000)
590 chunk_size -= 4;
591 read_codebook(codebook+ci, 1);
594 ci++;
595 flag <<= 1;
598 while(chunk_size > 0) { skip_byte(); chunk_size--; }
599 break;
601 /* -------------------- Frame -------------------- */
602 case 0x3000:
603 while((chunk_size > 0) && (y < y_bottom))
605 flag = get_long();
606 chunk_size -= 4;
608 for(i = 0; i < 32; i++)
610 if(y >= y_bottom) break;
611 if(flag & 0x80000000) /* 4 bytes per block */
613 d0 = get_byte();
614 d1 = get_byte();
615 d2 = get_byte();
616 d3 = get_byte();
617 chunk_size -= 4;
619 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
620 cvid_v4(frm_ptr + addr, output, frm_stride, inverted, v4_codebook+d0, v4_codebook+d1, v4_codebook+d2, v4_codebook+d3);
622 else /* 1 byte per block */
624 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
625 cvid_v1(frm_ptr + addr, output, frm_stride, inverted, v1_codebook + get_byte());
627 chunk_size--;
630 x += 4;
631 if(x >= out_width)
633 x = 0;
634 y += 4;
636 flag <<= 1;
639 while(chunk_size > 0) { skip_byte(); chunk_size--; }
640 break;
642 case 0x3100:
643 while((chunk_size > 0) && (y < y_bottom))
645 /* ---- flag bits: 0 = SKIP, 10 = V1, 11 = V4 ---- */
646 flag = get_long();
647 chunk_size -= 4;
648 mask = 0x80000000;
650 while((mask) && (y < y_bottom))
652 if(flag & mask)
654 if(mask == 1)
656 if(chunk_size < 0) break;
657 flag = get_long();
658 chunk_size -= 4;
659 mask = 0x80000000;
661 else mask >>= 1;
663 if(flag & mask) /* V4 */
665 d0 = get_byte();
666 d1 = get_byte();
667 d2 = get_byte();
668 d3 = get_byte();
669 chunk_size -= 4;
671 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
672 cvid_v4(frm_ptr + addr, output, frm_stride, inverted, v4_codebook+d0, v4_codebook+d1, v4_codebook+d2, v4_codebook+d3);
674 else /* V1 */
676 chunk_size--;
678 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
679 cvid_v1(frm_ptr + addr, output, frm_stride, inverted, v1_codebook + get_byte());
681 } /* else SKIP */
683 mask >>= 1;
684 x += 4;
685 if(x >= out_width)
687 x = 0;
688 y += 4;
693 while(chunk_size > 0) { skip_byte(); chunk_size--; }
694 break;
696 case 0x3200: /* each byte is a V1 codebook */
697 while((chunk_size > 0) && (y < y_bottom))
699 addr = get_addr(inverted, x, y, frm_stride, bpp, out_height);
700 cvid_v1(frm_ptr + addr, output, frm_stride, inverted, v1_codebook + get_byte());
702 chunk_size--;
703 x += 4;
704 if(x >= out_width)
706 x = 0;
707 y += 4;
710 while(chunk_size > 0) { skip_byte(); chunk_size--; }
711 break;
713 default:
714 ERR("CVID: unknown chunk_id %08lx\n", chunk_id);
715 while(chunk_size > 0) { skip_byte(); chunk_size--; }
716 break;
721 if(frame.length != size)
723 if(frame.length & 0x01) frame.length++; /* AVIs tend to have a size mismatch */
724 if(frame.length != size)
726 long xlen;
727 skip_byte();
728 xlen = get_byte() << 16;
729 xlen |= get_byte() << 8;
730 xlen |= get_byte(); /* Read Len */
731 WARN("CVID: END INFO chunk size %d cvid size1 %ld cvid size2 %ld\n",
732 size, frame.length, xlen);
737 static void ICCVID_dump_BITMAPINFO(const BITMAPINFO * bmi)
739 TRACE(
740 "planes = %d\n"
741 "bpp = %d\n"
742 "height = %d\n"
743 "width = %d\n"
744 "compr = %s\n",
745 bmi->bmiHeader.biPlanes,
746 bmi->bmiHeader.biBitCount,
747 bmi->bmiHeader.biHeight,
748 bmi->bmiHeader.biWidth,
749 debugstr_an( (const char *)&bmi->bmiHeader.biCompression, 4 ) );
752 static inline int ICCVID_CheckMask(RGBQUAD bmiColors[3], COLORREF redMask, COLORREF blueMask, COLORREF greenMask)
754 COLORREF realRedMask = MAKECOLOUR32(bmiColors[0].rgbRed, bmiColors[0].rgbGreen, bmiColors[0].rgbBlue);
755 COLORREF realBlueMask = MAKECOLOUR32(bmiColors[1].rgbRed, bmiColors[1].rgbGreen, bmiColors[1].rgbBlue);
756 COLORREF realGreenMask = MAKECOLOUR32(bmiColors[2].rgbRed, bmiColors[2].rgbGreen, bmiColors[2].rgbBlue);
758 TRACE("\nbmiColors[0] = 0x%08x\nbmiColors[1] = 0x%08x\nbmiColors[2] = 0x%08x\n",
759 realRedMask, realBlueMask, realGreenMask);
761 if ((realRedMask == redMask) &&
762 (realBlueMask == blueMask) &&
763 (realGreenMask == greenMask))
764 return TRUE;
765 return FALSE;
768 static LRESULT ICCVID_DecompressQuery( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
770 TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info, in, out);
772 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
773 return ICERR_BADPARAM;
775 TRACE("in: ");
776 ICCVID_dump_BITMAPINFO(in);
778 if( in->bmiHeader.biCompression != ICCVID_MAGIC )
779 return ICERR_BADFORMAT;
781 if( out )
783 TRACE("out: ");
784 ICCVID_dump_BITMAPINFO(out);
786 if( in->bmiHeader.biPlanes != out->bmiHeader.biPlanes )
787 return ICERR_BADFORMAT;
788 if( in->bmiHeader.biHeight != out->bmiHeader.biHeight )
790 if( in->bmiHeader.biHeight != -out->bmiHeader.biHeight )
791 return ICERR_BADFORMAT;
792 TRACE("Detected inverted height for video output\n");
794 if( in->bmiHeader.biWidth != out->bmiHeader.biWidth )
795 return ICERR_BADFORMAT;
797 switch( out->bmiHeader.biBitCount )
799 case 16:
800 if ( out->bmiHeader.biCompression == BI_BITFIELDS )
802 if ( !ICCVID_CheckMask(out->bmiColors, 0x7C00, 0x03E0, 0x001F) &&
803 !ICCVID_CheckMask(out->bmiColors, 0xF800, 0x07E0, 0x001F) )
805 TRACE("unsupported output bit field(s) for 16-bit colors\n");
806 return ICERR_BADFORMAT;
809 break;
810 case 24:
811 case 32:
812 break;
813 default:
814 TRACE("unsupported output bitcount = %d\n", out->bmiHeader.biBitCount );
815 return ICERR_BADFORMAT;
819 return ICERR_OK;
822 static LRESULT ICCVID_DecompressGetFormat( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
824 DWORD size;
826 TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info, in, out);
828 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
829 return ICERR_BADPARAM;
831 size = in->bmiHeader.biSize;
832 if (in->bmiHeader.biBitCount <= 8)
833 size += in->bmiHeader.biClrUsed * sizeof(RGBQUAD);
835 if( out )
837 memcpy( out, in, size );
838 out->bmiHeader.biCompression = BI_RGB;
839 out->bmiHeader.biSizeImage = in->bmiHeader.biHeight
840 * in->bmiHeader.biWidth *4;
841 return ICERR_OK;
843 return size;
846 static LRESULT ICCVID_DecompressBegin( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
848 TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info, in, out);
850 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
851 return ICERR_BADPARAM;
853 info->bits_per_pixel = out->bmiHeader.biBitCount;
855 if (info->bits_per_pixel == 16)
857 if ( out->bmiHeader.biCompression == BI_BITFIELDS )
859 if ( ICCVID_CheckMask(out->bmiColors, 0x7C00, 0x03E0, 0x001F) )
860 info->bits_per_pixel = 15;
861 else if ( ICCVID_CheckMask(out->bmiColors, 0xF800, 0x07E0, 0x001F) )
862 info->bits_per_pixel = 16;
863 else
865 TRACE("unsupported output bit field(s) for 16-bit colors\n");
866 return ICERR_UNSUPPORTED;
869 else
870 info->bits_per_pixel = 15;
873 TRACE("bit_per_pixel = %d\n", info->bits_per_pixel);
875 if( info->cvinfo )
876 free_cvinfo( info->cvinfo );
877 info->cvinfo = decode_cinepak_init();
879 return ICERR_OK;
882 static LRESULT ICCVID_Decompress( ICCVID_Info *info, ICDECOMPRESS *icd, DWORD size )
884 LONG width, height;
885 BOOL inverted;
887 TRACE("ICM_DECOMPRESS %p %p %d\n", info, icd, size);
889 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
890 return ICERR_BADPARAM;
891 if (info->cvinfo==NULL)
893 ERR("ICM_DECOMPRESS sent after ICM_DECOMPRESS_END\n");
894 return ICERR_BADPARAM;
897 width = icd->lpbiInput->biWidth;
898 height = icd->lpbiInput->biHeight;
899 inverted = -icd->lpbiOutput->biHeight == height;
901 decode_cinepak(info->cvinfo, icd->lpInput, icd->lpbiInput->biSizeImage,
902 icd->lpOutput, width, height, info->bits_per_pixel, inverted);
904 return ICERR_OK;
907 static LRESULT ICCVID_DecompressEx( ICCVID_Info *info, ICDECOMPRESSEX *icd, DWORD size )
909 LONG width, height;
910 BOOL inverted;
912 TRACE("ICM_DECOMPRESSEX %p %p %d\n", info, icd, size);
914 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
915 return ICERR_BADPARAM;
916 if (info->cvinfo==NULL)
918 ERR("ICM_DECOMPRESSEX sent after ICM_DECOMPRESS_END\n");
919 return ICERR_BADPARAM;
922 /* FIXME: flags are ignored */
924 width = icd->lpbiSrc->biWidth;
925 height = icd->lpbiSrc->biHeight;
926 inverted = -icd->lpbiDst->biHeight == height;
928 decode_cinepak(info->cvinfo, icd->lpSrc, icd->lpbiSrc->biSizeImage,
929 icd->lpDst, width, height, info->bits_per_pixel, inverted);
931 return ICERR_OK;
934 static LRESULT ICCVID_Close( ICCVID_Info *info )
936 if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
937 return 0;
938 if( info->cvinfo )
939 free_cvinfo( info->cvinfo );
940 heap_free( info );
941 return 1;
944 static LRESULT ICCVID_GetInfo( ICCVID_Info *info, ICINFO *icinfo, DWORD dwSize )
946 if (!icinfo) return sizeof(ICINFO);
947 if (dwSize < sizeof(ICINFO)) return 0;
949 icinfo->dwSize = sizeof(ICINFO);
950 icinfo->fccType = ICTYPE_VIDEO;
951 icinfo->fccHandler = info ? info->dwMagic : ICCVID_MAGIC;
952 icinfo->dwFlags = 0;
953 icinfo->dwVersion = ICVERSION;
954 icinfo->dwVersionICM = ICVERSION;
956 LoadStringW(ICCVID_hModule, IDS_NAME, icinfo->szName, sizeof(icinfo->szName)/sizeof(WCHAR));
957 LoadStringW(ICCVID_hModule, IDS_DESCRIPTION, icinfo->szDescription, sizeof(icinfo->szDescription)/sizeof(WCHAR));
958 /* msvfw32 will fill icinfo->szDriver for us */
960 return sizeof(ICINFO);
963 static LRESULT ICCVID_DecompressEnd( ICCVID_Info *info )
965 if( info->cvinfo )
967 free_cvinfo( info->cvinfo );
968 info->cvinfo = NULL;
970 return ICERR_OK;
973 LRESULT WINAPI ICCVID_DriverProc( DWORD_PTR dwDriverId, HDRVR hdrvr, UINT msg,
974 LPARAM lParam1, LPARAM lParam2)
976 ICCVID_Info *info = (ICCVID_Info *) dwDriverId;
978 TRACE("%ld %p %d %ld %ld\n", dwDriverId, hdrvr, msg, lParam1, lParam2);
980 switch( msg )
982 case DRV_LOAD:
983 TRACE("Loaded\n");
984 return 1;
985 case DRV_ENABLE:
986 return 0;
987 case DRV_DISABLE:
988 return 0;
989 case DRV_FREE:
990 return 0;
992 case DRV_OPEN:
994 ICINFO *icinfo = (ICINFO *)lParam2;
996 TRACE("Opened\n");
998 if (icinfo && compare_fourcc(icinfo->fccType, ICTYPE_VIDEO)) return 0;
1000 info = heap_alloc( sizeof (ICCVID_Info) );
1001 if( info )
1003 info->dwMagic = ICCVID_MAGIC;
1004 info->cvinfo = NULL;
1006 return (LRESULT) info;
1009 case DRV_CLOSE:
1010 return ICCVID_Close( info );
1012 case ICM_GETINFO:
1013 return ICCVID_GetInfo( info, (ICINFO *)lParam1, (DWORD)lParam2 );
1015 case ICM_DECOMPRESS_QUERY:
1016 return ICCVID_DecompressQuery( info, (LPBITMAPINFO) lParam1,
1017 (LPBITMAPINFO) lParam2 );
1018 case ICM_DECOMPRESS_GET_FORMAT:
1019 return ICCVID_DecompressGetFormat( info, (LPBITMAPINFO) lParam1,
1020 (LPBITMAPINFO) lParam2 );
1021 case ICM_DECOMPRESS_BEGIN:
1022 return ICCVID_DecompressBegin( info, (LPBITMAPINFO) lParam1,
1023 (LPBITMAPINFO) lParam2 );
1024 case ICM_DECOMPRESS:
1025 return ICCVID_Decompress( info, (ICDECOMPRESS*) lParam1,
1026 (DWORD) lParam2 );
1027 case ICM_DECOMPRESSEX:
1028 return ICCVID_DecompressEx( info, (ICDECOMPRESSEX*) lParam1,
1029 (DWORD) lParam2 );
1031 case ICM_DECOMPRESS_END:
1032 return ICCVID_DecompressEnd( info );
1034 case ICM_COMPRESS_QUERY:
1035 FIXME("compression not implemented\n");
1036 return ICERR_BADFORMAT;
1038 case ICM_CONFIGURE:
1039 return ICERR_UNSUPPORTED;
1041 default:
1042 FIXME("Unknown message: %04x %ld %ld\n", msg, lParam1, lParam2);
1044 return ICERR_UNSUPPORTED;
1047 BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
1049 TRACE("(%p,%d,%p)\n", hModule, dwReason, lpReserved);
1051 switch (dwReason)
1053 case DLL_PROCESS_ATTACH:
1054 DisableThreadLibraryCalls(hModule);
1055 ICCVID_hModule = hModule;
1056 break;
1058 return TRUE;