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