jscript: Moved resetting lastIndex to do_regexp_match_next.
[wine] / dlls / iccvid / iccvid.c
1 /*
2  * Radius Cinepak Video Decoder
3  *
4  * Copyright 2001 Dr. Tim Ferguson (see below)
5  * Portions Copyright 2003 Mike McCormack for CodeWeavers
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /* Copyright notice from original source:
23  * ------------------------------------------------------------------------
24  * Radius Cinepak Video Decoder
25  *
26  * Dr. Tim Ferguson, 2001.
27  * For more details on the algorithm:
28  *         http://www.csse.monash.edu.au/~timf/videocodec.html
29  *
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.
33  *
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 :)
37  *
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  * ------------------------------------------------------------------------ */
42
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"
52
53 #include "wine/debug.h"
54
55 WINE_DEFAULT_DEBUG_CHANNEL(iccvid);
56
57 static HINSTANCE ICCVID_hModule;
58
59 #define ICCVID_MAGIC mmioFOURCC('c', 'v', 'i', 'd')
60 #define compare_fourcc(fcc1, fcc2) (((fcc1)^(fcc2))&~0x20202020)
61
62 #define DBUG    0
63 #define MAX_STRIPS 32
64
65 /* ------------------------------------------------------------------------ */
66 typedef struct
67 {
68     unsigned char y0, y1, y2, y3;
69     char u, v;
70     unsigned char r[4], g[4], b[4];
71 } cvid_codebook;
72
73 typedef struct {
74     cvid_codebook *v4_codebook[MAX_STRIPS];
75     cvid_codebook *v1_codebook[MAX_STRIPS];
76     unsigned int strip_num;
77 } cinepak_info;
78
79 typedef struct _ICCVID_Info
80 {
81     DWORD         dwMagic;
82     int           bits_per_pixel;
83     cinepak_info *cvinfo;
84 } ICCVID_Info;
85
86 static inline LPVOID heap_alloc( size_t size )
87 {
88     return HeapAlloc( GetProcessHeap(), 0, size );
89 }
90
91 static inline BOOL heap_free( LPVOID ptr )
92 {
93     return HeapFree( GetProcessHeap(), 0, ptr );
94 }
95
96
97 /* ------------------------------------------------------------------------ */
98 static unsigned char *in_buffer, uiclip[1024], *uiclp = NULL;
99
100 #define get_byte() *(in_buffer++)
101 #define skip_byte() in_buffer++
102 #define get_word() ((unsigned short)(in_buffer += 2, \
103     (in_buffer[-2] << 8 | in_buffer[-1])))
104 #define get_long() ((unsigned long)(in_buffer += 4, \
105     (in_buffer[-4] << 24 | in_buffer[-3] << 16 | in_buffer[-2] << 8 | in_buffer[-1])))
106
107
108 /* ---------------------------------------------------------------------- */
109 static inline void read_codebook(cvid_codebook *c, int mode)
110 {
111 int uvr, uvg, uvb;
112
113     if(mode)        /* black and white */
114         {
115         c->y0 = get_byte();
116         c->y1 = get_byte();
117         c->y2 = get_byte();
118         c->y3 = get_byte();
119         c->u = c->v = 0;
120
121         c->r[0] = c->g[0] = c->b[0] = c->y0;
122         c->r[1] = c->g[1] = c->b[1] = c->y1;
123         c->r[2] = c->g[2] = c->b[2] = c->y2;
124         c->r[3] = c->g[3] = c->b[3] = c->y3;
125         }
126     else            /* colour */
127         {
128         c->y0 = get_byte();  /* luma */
129         c->y1 = get_byte();
130         c->y2 = get_byte();
131         c->y3 = get_byte();
132         c->u = get_byte(); /* chroma */
133         c->v = get_byte();
134
135         uvr = c->v << 1;
136         uvg = -((c->u+1) >> 1) - c->v;
137         uvb = c->u << 1;
138
139         c->r[0] = uiclp[c->y0 + uvr]; c->g[0] = uiclp[c->y0 + uvg]; c->b[0] = uiclp[c->y0 + uvb];
140         c->r[1] = uiclp[c->y1 + uvr]; c->g[1] = uiclp[c->y1 + uvg]; c->b[1] = uiclp[c->y1 + uvb];
141         c->r[2] = uiclp[c->y2 + uvr]; c->g[2] = uiclp[c->y2 + uvg]; c->b[2] = uiclp[c->y2 + uvb];
142         c->r[3] = uiclp[c->y3 + uvr]; c->g[3] = uiclp[c->y3 + uvg]; c->b[3] = uiclp[c->y3 + uvb];
143         }
144 }
145
146
147 #define MAKECOLOUR32(r,g,b) (((r) << 16) | ((g) << 8) | (b))
148 /*#define MAKECOLOUR24(r,g,b) (((r) << 16) | ((g) << 8) | (b))*/
149 #define MAKECOLOUR16(r,g,b) (((r) >> 3) << 11)| (((g) >> 2) << 5)| (((b) >> 3) << 0)
150 #define MAKECOLOUR15(r,g,b) (((r) >> 3) << 10)| (((g) >> 3) << 5)| (((b) >> 3) << 0)
151
152 /* ------------------------------------------------------------------------ */
153 static void cvid_v1_32(unsigned char *frm, unsigned char *limit, int stride, cvid_codebook *cb)
154 {
155 unsigned long *vptr = (unsigned long *)frm;
156 #ifndef ORIGINAL
157 int row_inc = -stride/4;
158 #else
159 int row_inc = stride/4;
160 #endif
161 int x, y;
162
163     /* fill 4x4 block of pixels with colour values from codebook */
164     for (y = 0; y < 4; y++)
165     {
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]);
169     }
170 }
171
172
173 /* ------------------------------------------------------------------------ */
174 static void cvid_v4_32(unsigned char *frm, unsigned char *limit, int stride, cvid_codebook *cb0,
175     cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
176 {
177 unsigned long *vptr = (unsigned long *)frm;
178 #ifndef ORIGINAL
179 int row_inc = -stride/4;
180 #else
181 int row_inc = stride/4;
182 #endif
183 int x, y;
184 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
185
186     /* fill 4x4 block of pixels with colour values from codebooks */
187     for (y = 0; y < 4; y++)
188     {
189         if (&vptr[y*row_inc] < (unsigned long *)limit) return;
190         for (x = 0; x < 4; x++)
191             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]);
192     }
193 }
194
195
196 /* ------------------------------------------------------------------------ */
197 static void cvid_v1_24(unsigned char *vptr, unsigned char *limit, int stride, cvid_codebook *cb)
198 {
199 #ifndef ORIGINAL
200 int row_inc = -stride;
201 #else
202 int row_inc = stride;
203 #endif
204 int x, y;
205
206     /* fill 4x4 block of pixels with colour values from codebook */
207     for (y = 0; y < 4; y++)
208     {
209         if (&vptr[y*row_inc] < limit) return;
210         for (x = 0; x < 4; x++)
211         {
212             vptr[y*row_inc + x*3 + 0] = cb->b[x/2+(y/2)*2];
213             vptr[y*row_inc + x*3 + 1] = cb->g[x/2+(y/2)*2];
214             vptr[y*row_inc + x*3 + 2] = cb->r[x/2+(y/2)*2];
215         }
216     }
217 }
218
219
220 /* ------------------------------------------------------------------------ */
221 static void cvid_v4_24(unsigned char *vptr, unsigned char *limit, int stride, cvid_codebook *cb0,
222     cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
223 {
224 #ifndef ORIGINAL
225 int row_inc = -stride;
226 #else
227 int row_inc = stride;
228 #endif
229 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
230 int x, y;
231
232     /* fill 4x4 block of pixels with colour values from codebooks */
233     for (y = 0; y < 4; y++)
234     {
235         if (&vptr[y*row_inc] < limit) return;
236         for (x = 0; x < 4; x++)
237         {
238             vptr[y*row_inc + x*3 + 0] = cb[x/2+(y/2)*2]->b[x%2+(y%2)*2];
239             vptr[y*row_inc + x*3 + 1] = cb[x/2+(y/2)*2]->g[x%2+(y%2)*2];
240             vptr[y*row_inc + x*3 + 2] = cb[x/2+(y/2)*2]->r[x%2+(y%2)*2];
241         }
242     }
243 }
244
245
246 /* ------------------------------------------------------------------------ */
247 static void cvid_v1_16(unsigned char *frm, unsigned char *limit, int stride, cvid_codebook *cb)
248 {
249 unsigned short *vptr = (unsigned short *)frm;
250 #ifndef ORIGINAL
251 int row_inc = -stride/2;
252 #else
253 int row_inc = stride/2;
254 #endif
255 int x, y;
256
257     /* fill 4x4 block of pixels with colour values from codebook */
258     for (y = 0; y < 4; y++)
259     {
260         if (&vptr[y*row_inc] < (unsigned short *)limit) return;
261         for (x = 0; x < 4; x++)
262             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]);
263     }
264 }
265
266
267 /* ------------------------------------------------------------------------ */
268 static void cvid_v4_16(unsigned char *frm, unsigned char *limit, int stride, cvid_codebook *cb0,
269     cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
270 {
271 unsigned short *vptr = (unsigned short *)frm;
272 #ifndef ORIGINAL
273 int row_inc = -stride/2;
274 #else
275 int row_inc = stride/2;
276 #endif
277 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
278 int x, y;
279
280     /* fill 4x4 block of pixels with colour values from codebooks */
281     for (y = 0; y < 4; y++)
282     {
283         if (&vptr[y*row_inc] < (unsigned short *)limit) return;
284         for (x = 0; x < 4; x++)
285             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]);
286     }
287 }
288
289 /* ------------------------------------------------------------------------ */
290 static void cvid_v1_15(unsigned char *frm, unsigned char *limit, int stride, cvid_codebook *cb)
291 {
292 unsigned short *vptr = (unsigned short *)frm;
293 #ifndef ORIGINAL
294 int row_inc = -stride/2;
295 #else
296 int row_inc = stride/2;
297 #endif
298 int x, y;
299
300     /* fill 4x4 block of pixels with colour values from codebook */
301     for (y = 0; y < 4; y++)
302     {
303         if (&vptr[y*row_inc] < (unsigned short *)limit) return;
304         for (x = 0; x < 4; x++)
305             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]);
306     }
307 }
308
309
310 /* ------------------------------------------------------------------------ */
311 static void cvid_v4_15(unsigned char *frm, unsigned char *limit, int stride, cvid_codebook *cb0,
312     cvid_codebook *cb1, cvid_codebook *cb2, cvid_codebook *cb3)
313 {
314 unsigned short *vptr = (unsigned short *)frm;
315 #ifndef ORIGINAL
316 int row_inc = -stride/2;
317 #else
318 int row_inc = stride/2;
319 #endif
320 cvid_codebook * cb[] = {cb0,cb1,cb2,cb3};
321 int x, y;
322
323     /* fill 4x4 block of pixels with colour values from codebooks */
324     for (y = 0; y < 4; y++)
325     {
326         if (&vptr[y*row_inc] < (unsigned short *)limit) return;
327         for (x = 0; x < 4; x++)
328             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]);
329     }
330 }
331
332
333 /* ------------------------------------------------------------------------
334  * Call this function once at the start of the sequence and save the
335  * returned context for calls to decode_cinepak().
336  */
337 static cinepak_info *decode_cinepak_init(void)
338 {
339     cinepak_info *cvinfo;
340     int i;
341
342     cvinfo = heap_alloc( sizeof (cinepak_info) );
343     if( !cvinfo )
344         return NULL;
345     cvinfo->strip_num = 0;
346
347     if(uiclp == NULL)
348     {
349         uiclp = uiclip+512;
350         for(i = -512; i < 512; i++)
351             uiclp[i] = (i < 0 ? 0 : (i > 255 ? 255 : i));
352     }
353
354     return cvinfo;
355 }
356
357 static void free_cvinfo( cinepak_info *cvinfo )
358 {
359     unsigned int i;
360
361     for( i=0; i<cvinfo->strip_num; i++ )
362     {
363         heap_free(cvinfo->v4_codebook[i]);
364         heap_free(cvinfo->v1_codebook[i]);
365     }
366     heap_free( cvinfo );
367 }
368
369 typedef void (*fn_cvid_v1)(unsigned char *frm, unsigned char *limit,
370                            int stride, cvid_codebook *cb);
371 typedef void (*fn_cvid_v4)(unsigned char *frm, unsigned char *limit, int stride,
372                            cvid_codebook *cb0, cvid_codebook *cb1,
373                            cvid_codebook *cb2, cvid_codebook *cb3);
374
375 /* ------------------------------------------------------------------------
376  * This function decodes a buffer containing a Cinepak encoded frame.
377  *
378  * context - the context created by decode_cinepak_init().
379  * buf - the input buffer to be decoded
380  * size - the size of the input buffer
381  * frame - the output frame buffer (24 or 32 bit per pixel)
382  * width - the width of the output frame
383  * height - the height of the output frame
384  * bit_per_pixel - the number of bits per pixel allocated to the output
385  *   frame (only 24 or 32 bpp are supported)
386  */
387 static void decode_cinepak(cinepak_info *cvinfo, unsigned char *buf, int size,
388            unsigned char *frame, unsigned int width, unsigned int height, int bit_per_pixel)
389 {
390     cvid_codebook *v4_codebook, *v1_codebook, *codebook = NULL;
391     unsigned long x, y, y_bottom, frame_flags, strips, cv_width, cv_height,
392                   cnum, strip_id, chunk_id, x0, y0, x1, y1, ci, flag, mask;
393     long len, top_size, chunk_size;
394     unsigned char *frm_ptr;
395     unsigned int i, cur_strip;
396     int d0, d1, d2, d3, frm_stride, bpp = 3;
397     fn_cvid_v1 cvid_v1 = cvid_v1_24;
398     fn_cvid_v4 cvid_v4 = cvid_v4_24;
399
400     y = 0;
401     y_bottom = 0;
402     in_buffer = buf;
403
404     frame_flags = get_byte();
405     len = get_byte() << 16;
406     len |= get_byte() << 8;
407     len |= get_byte();
408
409     switch(bit_per_pixel)
410         {
411         case 15:
412             bpp = 2;
413             cvid_v1 = cvid_v1_15;
414             cvid_v4 = cvid_v4_15;
415             break;
416         case 16:
417             bpp = 2;
418             cvid_v1 = cvid_v1_16;
419             cvid_v4 = cvid_v4_16;
420             break;
421         case 24:
422             bpp = 3;
423             cvid_v1 = cvid_v1_24;
424             cvid_v4 = cvid_v4_24;
425             break;
426         case 32:
427             bpp = 4;
428             cvid_v1 = cvid_v1_32;
429             cvid_v4 = cvid_v4_32;
430             break;
431         }
432
433     frm_stride = width * bpp;
434     frm_ptr = frame;
435
436     if(len != size)
437         {
438         if(len & 0x01) len++; /* AVIs tend to have a size mismatch */
439         if(len != size)
440             {
441             ERR("CVID: corruption %d (QT/AVI) != %ld (CV)\n", size, len);
442             /* return; */
443             }
444         }
445
446     cv_width = get_word();
447     cv_height = get_word();
448     strips = get_word();
449
450     if(strips > cvinfo->strip_num)
451         {
452         if(strips >= MAX_STRIPS)
453             {
454             ERR("CVID: strip overflow (more than %d)\n", MAX_STRIPS);
455             return;
456             }
457
458         for(i = cvinfo->strip_num; i < strips; i++)
459             {
460             if((cvinfo->v4_codebook[i] = heap_alloc(sizeof(cvid_codebook) * 260)) == NULL)
461                 {
462                 ERR("CVID: codebook v4 alloc err\n");
463                 return;
464                 }
465
466             if((cvinfo->v1_codebook[i] = heap_alloc(sizeof(cvid_codebook) * 260)) == NULL)
467                 {
468                 ERR("CVID: codebook v1 alloc err\n");
469                 return;
470                 }
471             }
472         }
473     cvinfo->strip_num = strips;
474
475     TRACE("CVID: <%ld,%ld> strips %ld\n", cv_width, cv_height, strips);
476
477     for(cur_strip = 0; cur_strip < strips; cur_strip++)
478         {
479         v4_codebook = cvinfo->v4_codebook[cur_strip];
480         v1_codebook = cvinfo->v1_codebook[cur_strip];
481
482         if((cur_strip > 0) && (!(frame_flags & 0x01)))
483             {
484             memcpy(cvinfo->v4_codebook[cur_strip], cvinfo->v4_codebook[cur_strip-1], 260 * sizeof(cvid_codebook));
485             memcpy(cvinfo->v1_codebook[cur_strip], cvinfo->v1_codebook[cur_strip-1], 260 * sizeof(cvid_codebook));
486             }
487
488         strip_id = get_word();        /* 1000 = key strip, 1100 = iter strip */
489         top_size = get_word();
490         y0 = get_word();        /* FIXME: most of these are ignored at the moment */
491         x0 = get_word();
492         y1 = get_word();
493         x1 = get_word();
494
495         y_bottom += y1;
496         top_size -= 12;
497         x = 0;
498         if(x1 != width)
499             WARN("CVID: Warning x1 (%ld) != width (%d)\n", x1, width);
500
501         TRACE("   %d) %04lx %04ld <%ld,%ld> <%ld,%ld> yt %ld\n",
502               cur_strip, strip_id, top_size, x0, y0, x1, y1, y_bottom);
503
504         while(top_size > 0)
505             {
506             chunk_id  = get_word();
507             chunk_size = get_word();
508
509             TRACE("        %04lx %04ld\n", chunk_id, chunk_size);
510             top_size -= chunk_size;
511             chunk_size -= 4;
512
513             switch(chunk_id)
514                 {
515                     /* -------------------- Codebook Entries -------------------- */
516                 case 0x2000:
517                 case 0x2200:
518                     codebook = (chunk_id == 0x2200 ? v1_codebook : v4_codebook);
519                     cnum = chunk_size/6;
520                     for(i = 0; i < cnum; i++) read_codebook(codebook+i, 0);
521                     break;
522
523                 case 0x2400:
524                 case 0x2600:        /* 8 bit per pixel */
525                     codebook = (chunk_id == 0x2600 ? v1_codebook : v4_codebook);
526                     cnum = chunk_size/4;
527                     for(i = 0; i < cnum; i++) read_codebook(codebook+i, 1);
528                     break;
529
530                 case 0x2100:
531                 case 0x2300:
532                     codebook = (chunk_id == 0x2300 ? v1_codebook : v4_codebook);
533
534                     ci = 0;
535                     while(chunk_size > 0)
536                         {
537                         flag = get_long();
538                         chunk_size -= 4;
539
540                         for(i = 0; i < 32; i++)
541                             {
542                             if(flag & 0x80000000)
543                                 {
544                                 chunk_size -= 6;
545                                 read_codebook(codebook+ci, 0);
546                                 }
547
548                             ci++;
549                             flag <<= 1;
550                             }
551                         }
552                     while(chunk_size > 0) { skip_byte(); chunk_size--; }
553                     break;
554
555                 case 0x2500:
556                 case 0x2700:        /* 8 bit per pixel */
557                     codebook = (chunk_id == 0x2700 ? v1_codebook : v4_codebook);
558
559                     ci = 0;
560                     while(chunk_size > 0)
561                         {
562                         flag = get_long();
563                         chunk_size -= 4;
564
565                         for(i = 0; i < 32; i++)
566                             {
567                             if(flag & 0x80000000)
568                                 {
569                                 chunk_size -= 4;
570                                 read_codebook(codebook+ci, 1);
571                                 }
572
573                             ci++;
574                             flag <<= 1;
575                             }
576                         }
577                     while(chunk_size > 0) { skip_byte(); chunk_size--; }
578                     break;
579
580                     /* -------------------- Frame -------------------- */
581                 case 0x3000:
582                     while((chunk_size > 0) && (y < y_bottom))
583                         {
584                         flag = get_long();
585                         chunk_size -= 4;
586
587                         for(i = 0; i < 32; i++)
588                             {
589                             if(y >= y_bottom) break;
590                             if(flag & 0x80000000)    /* 4 bytes per block */
591                                 {
592                                 d0 = get_byte();
593                                 d1 = get_byte();
594                                 d2 = get_byte();
595                                 d3 = get_byte();
596                                 chunk_size -= 4;
597 #ifdef ORIGINAL
598                                 cvid_v4(frm_ptr + (y * frm_stride + x * bpp), frm_end, frm_stride, v4_codebook+d0, v4_codebook+d1, v4_codebook+d2, v4_codebook+d3);
599 #else
600                                 cvid_v4(frm_ptr + ((height - 1 - y) * frm_stride + x * bpp), frame, frm_stride, v4_codebook+d0, v4_codebook+d1, v4_codebook+d2, v4_codebook+d3);
601 #endif
602                                 }
603                             else        /* 1 byte per block */
604                                 {
605 #ifdef ORIGINAL
606                                 cvid_v1(frm_ptr + (y * frm_stride + x * bpp), frm_end, frm_stride, v1_codebook + get_byte());
607 #else
608                                 cvid_v1(frm_ptr + ((height - 1 - y) * frm_stride + x * bpp), frame, frm_stride, v1_codebook + get_byte());
609 #endif
610                                 chunk_size--;
611                                 }
612
613                             x += 4;
614                             if(x >= width)
615                                 {
616                                 x = 0;
617                                 y += 4;
618                                 }
619                             flag <<= 1;
620                             }
621                         }
622                     while(chunk_size > 0) { skip_byte(); chunk_size--; }
623                     break;
624
625                 case 0x3100:
626                     while((chunk_size > 0) && (y < y_bottom))
627                         {
628                             /* ---- flag bits: 0 = SKIP, 10 = V1, 11 = V4 ---- */
629                         flag = get_long();
630                         chunk_size -= 4;
631                         mask = 0x80000000;
632
633                         while((mask) && (y < y_bottom))
634                             {
635                             if(flag & mask)
636                                 {
637                                 if(mask == 1)
638                                     {
639                                     if(chunk_size < 0) break;
640                                     flag = get_long();
641                                     chunk_size -= 4;
642                                     mask = 0x80000000;
643                                     }
644                                 else mask >>= 1;
645
646                                 if(flag & mask)        /* V4 */
647                                     {
648                                     d0 = get_byte();
649                                     d1 = get_byte();
650                                     d2 = get_byte();
651                                     d3 = get_byte();
652                                     chunk_size -= 4;
653 #ifdef ORIGINAL
654                                     cvid_v4(frm_ptr + (y * frm_stride + x * bpp), frm_end, frm_stride, v4_codebook+d0, v4_codebook+d1, v4_codebook+d2, v4_codebook+d3);
655 #else
656                                     cvid_v4(frm_ptr + ((height - 1 - y) * frm_stride + x * bpp), frame, frm_stride, v4_codebook+d0, v4_codebook+d1, v4_codebook+d2, v4_codebook+d3);
657 #endif
658                                     }
659                                 else        /* V1 */
660                                     {
661                                     chunk_size--;
662 #ifdef ORIGINAL
663                                     cvid_v1(frm_ptr + (y * frm_stride + x * bpp), frm_end, frm_stride, v1_codebook + get_byte());
664 #else
665                                     cvid_v1(frm_ptr + ((height - 1 - y) * frm_stride + x * bpp), frame, frm_stride, v1_codebook + get_byte());
666 #endif
667                                     }
668                                 }        /* else SKIP */
669
670                             mask >>= 1;
671                             x += 4;
672                             if(x >= width)
673                                 {
674                                 x = 0;
675                                 y += 4;
676                                 }
677                             }
678                         }
679
680                     while(chunk_size > 0) { skip_byte(); chunk_size--; }
681                     break;
682
683                 case 0x3200:        /* each byte is a V1 codebook */
684                     while((chunk_size > 0) && (y < y_bottom))
685                         {
686 #ifdef ORIGINAL
687                         cvid_v1(frm_ptr + (y * frm_stride + x * bpp), frm_end, frm_stride, v1_codebook + get_byte());
688 #else
689                         cvid_v1(frm_ptr + ((height - 1 - y) * frm_stride + x * bpp), frame, frm_stride, v1_codebook + get_byte());
690 #endif
691                         chunk_size--;
692                         x += 4;
693                         if(x >= width)
694                             {
695                             x = 0;
696                             y += 4;
697                             }
698                         }
699                     while(chunk_size > 0) { skip_byte(); chunk_size--; }
700                     break;
701
702                 default:
703                     ERR("CVID: unknown chunk_id %08lx\n", chunk_id);
704                     while(chunk_size > 0) { skip_byte(); chunk_size--; }
705                     break;
706                 }
707             }
708         }
709
710     if(len != size)
711         {
712         if(len & 0x01) len++; /* AVIs tend to have a size mismatch */
713         if(len != size)
714             {
715             long xlen;
716             skip_byte();
717             xlen = get_byte() << 16;
718             xlen |= get_byte() << 8;
719             xlen |= get_byte(); /* Read Len */
720             WARN("CVID: END INFO chunk size %d cvid size1 %ld cvid size2 %ld\n",
721                   size, len, xlen);
722             }
723         }
724 }
725
726 static void ICCVID_dump_BITMAPINFO(const BITMAPINFO * bmi)
727 {
728     TRACE(
729         "planes = %d\n"
730         "bpp    = %d\n"
731         "height = %d\n"
732         "width  = %d\n"
733         "compr  = %s\n",
734         bmi->bmiHeader.biPlanes,
735         bmi->bmiHeader.biBitCount,
736         bmi->bmiHeader.biHeight,
737         bmi->bmiHeader.biWidth,
738         debugstr_an( (const char *)&bmi->bmiHeader.biCompression, 4 ) );
739 }
740
741 static inline int ICCVID_CheckMask(RGBQUAD bmiColors[3], COLORREF redMask, COLORREF blueMask, COLORREF greenMask)
742 {
743     COLORREF realRedMask = MAKECOLOUR32(bmiColors[0].rgbRed, bmiColors[0].rgbGreen, bmiColors[0].rgbBlue);
744     COLORREF realBlueMask = MAKECOLOUR32(bmiColors[1].rgbRed, bmiColors[1].rgbGreen, bmiColors[1].rgbBlue);
745     COLORREF realGreenMask = MAKECOLOUR32(bmiColors[2].rgbRed, bmiColors[2].rgbGreen, bmiColors[2].rgbBlue);
746
747     TRACE("\nbmiColors[0] = 0x%08x\nbmiColors[1] = 0x%08x\nbmiColors[2] = 0x%08x\n",
748         realRedMask, realBlueMask, realGreenMask);
749         
750     if ((realRedMask == redMask) &&
751         (realBlueMask == blueMask) &&
752         (realGreenMask == greenMask))
753         return TRUE;
754     return FALSE;
755 }
756
757 static LRESULT ICCVID_DecompressQuery( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
758 {
759     TRACE("ICM_DECOMPRESS_QUERY %p %p %p\n", info, in, out);
760
761     if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
762         return ICERR_BADPARAM;
763
764     TRACE("in: ");
765     ICCVID_dump_BITMAPINFO(in);
766
767     if( in->bmiHeader.biCompression != ICCVID_MAGIC )
768         return ICERR_BADFORMAT;
769
770     if( out )
771     {
772         TRACE("out: ");
773         ICCVID_dump_BITMAPINFO(out);
774
775         if( in->bmiHeader.biPlanes != out->bmiHeader.biPlanes )
776             return ICERR_BADFORMAT;
777         if( in->bmiHeader.biHeight != out->bmiHeader.biHeight )
778             return ICERR_BADFORMAT;
779         if( in->bmiHeader.biWidth != out->bmiHeader.biWidth )
780             return ICERR_BADFORMAT;
781
782         switch( out->bmiHeader.biBitCount )
783         {
784         case 16:
785             if ( out->bmiHeader.biCompression == BI_BITFIELDS )
786             {
787                 if ( !ICCVID_CheckMask(out->bmiColors, 0x7C00, 0x03E0, 0x001F) &&
788                      !ICCVID_CheckMask(out->bmiColors, 0xF800, 0x07E0, 0x001F) )
789                 {
790                     TRACE("unsupported output bit field(s) for 16-bit colors\n");
791                     return ICERR_BADFORMAT;
792                 }
793             }
794             break;
795         case 24:
796         case 32:
797             break;
798         default:
799             TRACE("unsupported output bitcount = %d\n", out->bmiHeader.biBitCount );
800             return ICERR_BADFORMAT;
801         }
802     }
803
804     return ICERR_OK;
805 }
806
807 static LRESULT ICCVID_DecompressGetFormat( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
808 {
809     DWORD size;
810
811     TRACE("ICM_DECOMPRESS_GETFORMAT %p %p %p\n", info, in, out);
812
813     if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
814         return ICERR_BADPARAM;
815
816     size = in->bmiHeader.biSize;
817     if (in->bmiHeader.biBitCount <= 8)
818         size += in->bmiHeader.biClrUsed * sizeof(RGBQUAD);
819
820     if( out )
821     {
822         memcpy( out, in, size );
823         out->bmiHeader.biCompression = BI_RGB;
824         out->bmiHeader.biSizeImage = in->bmiHeader.biHeight
825                                    * in->bmiHeader.biWidth *4;
826         return ICERR_OK;
827     }
828     return size;
829 }
830
831 static LRESULT ICCVID_DecompressBegin( ICCVID_Info *info, LPBITMAPINFO in, LPBITMAPINFO out )
832 {
833     TRACE("ICM_DECOMPRESS_BEGIN %p %p %p\n", info, in, out);
834
835     if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
836         return ICERR_BADPARAM;
837
838     info->bits_per_pixel = out->bmiHeader.biBitCount;
839
840     if (info->bits_per_pixel == 16)
841     {
842         if ( out->bmiHeader.biCompression == BI_BITFIELDS )
843         {
844             if ( ICCVID_CheckMask(out->bmiColors, 0x7C00, 0x03E0, 0x001F) )
845                 info->bits_per_pixel = 15;
846             else if ( ICCVID_CheckMask(out->bmiColors, 0xF800, 0x07E0, 0x001F) )
847                 info->bits_per_pixel = 16;
848             else
849             {
850                 TRACE("unsupported output bit field(s) for 16-bit colors\n");
851                 return ICERR_UNSUPPORTED;
852             }
853         }
854         else
855             info->bits_per_pixel = 15;
856     }
857
858     TRACE("bit_per_pixel = %d\n", info->bits_per_pixel);
859
860     if( info->cvinfo )
861         free_cvinfo( info->cvinfo );
862     info->cvinfo = decode_cinepak_init();
863
864     return ICERR_OK;
865 }
866
867 static LRESULT ICCVID_Decompress( ICCVID_Info *info, ICDECOMPRESS *icd, DWORD size )
868 {
869     LONG width, height;
870
871     TRACE("ICM_DECOMPRESS %p %p %d\n", info, icd, size);
872
873     if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
874         return ICERR_BADPARAM;
875     if (info->cvinfo==NULL)
876     {
877         ERR("ICM_DECOMPRESS sent after ICM_DECOMPRESS_END\n");
878         return ICERR_BADPARAM;
879     }
880
881     width  = icd->lpbiInput->biWidth;
882     height = icd->lpbiInput->biHeight;
883
884     decode_cinepak(info->cvinfo, icd->lpInput, icd->lpbiInput->biSizeImage,
885                    icd->lpOutput, width, height, info->bits_per_pixel);
886
887     return ICERR_OK;
888 }
889
890 static LRESULT ICCVID_DecompressEx( ICCVID_Info *info, ICDECOMPRESSEX *icd, DWORD size )
891 {
892     LONG width, height;
893
894     TRACE("ICM_DECOMPRESSEX %p %p %d\n", info, icd, size);
895
896     if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
897         return ICERR_BADPARAM;
898     if (info->cvinfo==NULL)
899     {
900         ERR("ICM_DECOMPRESSEX sent after ICM_DECOMPRESS_END\n");
901         return ICERR_BADPARAM;
902     }
903
904     /* FIXME: flags are ignored */
905
906     width  = icd->lpbiSrc->biWidth;
907     height = icd->lpbiSrc->biHeight;
908
909     decode_cinepak(info->cvinfo, icd->lpSrc, icd->lpbiSrc->biSizeImage,
910                    icd->lpDst, width, height, info->bits_per_pixel);
911
912     return ICERR_OK;
913 }
914
915 static LRESULT ICCVID_Close( ICCVID_Info *info )
916 {
917     if( (info==NULL) || (info->dwMagic!=ICCVID_MAGIC) )
918         return 0;
919     if( info->cvinfo )
920         free_cvinfo( info->cvinfo );
921     heap_free( info );
922     return 1;
923 }
924
925 static LRESULT ICCVID_GetInfo( ICCVID_Info *info, ICINFO *icinfo, DWORD dwSize )
926 {
927     if (!icinfo) return sizeof(ICINFO);
928     if (dwSize < sizeof(ICINFO)) return 0;
929
930     icinfo->dwSize = sizeof(ICINFO);
931     icinfo->fccType = ICTYPE_VIDEO;
932     icinfo->fccHandler = info ? info->dwMagic : ICCVID_MAGIC;
933     icinfo->dwFlags = 0;
934     icinfo->dwVersion = ICVERSION;
935     icinfo->dwVersionICM = ICVERSION;
936
937     LoadStringW(ICCVID_hModule, IDS_NAME, icinfo->szName, sizeof(icinfo->szName)/sizeof(WCHAR));
938     LoadStringW(ICCVID_hModule, IDS_DESCRIPTION, icinfo->szDescription, sizeof(icinfo->szDescription)/sizeof(WCHAR));
939     /* msvfw32 will fill icinfo->szDriver for us */
940
941     return sizeof(ICINFO);
942 }
943
944 static LRESULT ICCVID_DecompressEnd( ICCVID_Info *info )
945 {
946     if( info->cvinfo )
947     {
948         free_cvinfo( info->cvinfo );
949         info->cvinfo = NULL;
950     }
951     return ICERR_OK;
952 }
953
954 LRESULT WINAPI ICCVID_DriverProc( DWORD_PTR dwDriverId, HDRVR hdrvr, UINT msg,
955                                   LPARAM lParam1, LPARAM lParam2)
956 {
957     ICCVID_Info *info = (ICCVID_Info *) dwDriverId;
958
959     TRACE("%ld %p %d %ld %ld\n", dwDriverId, hdrvr, msg, lParam1, lParam2);
960
961     switch( msg )
962     {
963     case DRV_LOAD:
964         TRACE("Loaded\n");
965         return 1;
966     case DRV_ENABLE:
967         return 0;
968     case DRV_DISABLE:
969         return 0;
970     case DRV_FREE:
971         return 0;
972
973     case DRV_OPEN:
974     {
975         ICINFO *icinfo = (ICINFO *)lParam2;
976
977         TRACE("Opened\n");
978
979         if (icinfo && compare_fourcc(icinfo->fccType, ICTYPE_VIDEO)) return 0;
980
981         info = heap_alloc( sizeof (ICCVID_Info) );
982         if( info )
983         {
984             info->dwMagic = ICCVID_MAGIC;
985             info->cvinfo = NULL;
986         }
987         return (LRESULT) info;
988     }
989
990     case DRV_CLOSE:
991         return ICCVID_Close( info );
992
993     case ICM_GETINFO:
994         return ICCVID_GetInfo( info, (ICINFO *)lParam1, (DWORD)lParam2 );
995
996     case ICM_DECOMPRESS_QUERY:
997         return ICCVID_DecompressQuery( info, (LPBITMAPINFO) lParam1,
998                                        (LPBITMAPINFO) lParam2 );
999     case ICM_DECOMPRESS_GET_FORMAT:
1000         return ICCVID_DecompressGetFormat( info, (LPBITMAPINFO) lParam1,
1001                                        (LPBITMAPINFO) lParam2 );
1002     case ICM_DECOMPRESS_BEGIN:
1003         return ICCVID_DecompressBegin( info, (LPBITMAPINFO) lParam1,
1004                                        (LPBITMAPINFO) lParam2 );
1005     case ICM_DECOMPRESS:
1006         return ICCVID_Decompress( info, (ICDECOMPRESS*) lParam1,
1007                                   (DWORD) lParam2 );
1008     case ICM_DECOMPRESSEX:
1009         return ICCVID_DecompressEx( info, (ICDECOMPRESSEX*) lParam1, 
1010                                   (DWORD) lParam2 );
1011
1012     case ICM_DECOMPRESS_END:
1013         return ICCVID_DecompressEnd( info );
1014
1015     case ICM_COMPRESS_QUERY:
1016         FIXME("compression not implemented\n");
1017         return ICERR_BADFORMAT;
1018
1019     case ICM_CONFIGURE:
1020         return ICERR_UNSUPPORTED;
1021
1022     default:
1023         FIXME("Unknown message: %04x %ld %ld\n", msg, lParam1, lParam2);
1024     }
1025     return ICERR_UNSUPPORTED;
1026 }
1027
1028 BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
1029 {
1030     TRACE("(%p,%d,%p)\n", hModule, dwReason, lpReserved);
1031
1032     switch (dwReason)
1033     {
1034     case DLL_PROCESS_ATTACH:
1035         DisableThreadLibraryCalls(hModule);
1036         ICCVID_hModule = hModule;
1037         break;
1038
1039     case DLL_PROCESS_DETACH:
1040         break;
1041     }
1042     return TRUE;
1043 }