urlmon: Added PI_MIMEVERIFICATION tests.
[wine] / dlls / msvfw32 / msvideo_main.c
1 /*
2  * Copyright 1998 Marcus Meissner
3  * Copyright 2000 Bradley Baetz
4  * Copyright 2003 Michael Günnewig
5  * Copyright 2005 Dmitry Timoshkov
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  * FIXME: This all assumes 32 bit codecs
22  *              Win95 appears to prefer 32 bit codecs, even from 16 bit code.
23  *              There is the ICOpenFunction16 to worry about still, though.
24  *      
25  * TODO
26  *      - no thread safety
27  */
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "winnls.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "commdlg.h"
40 #include "vfw.h"
41 #include "msvideo_private.h"
42 #include "wine/debug.h"
43
44 /* Drivers32 settings */
45 #define HKLM_DRIVERS32 "Software\\Microsoft\\Windows NT\\CurrentVersion\\Drivers32"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(msvideo);
48
49 static inline const char *wine_dbgstr_fcc( DWORD fcc )
50 {
51     return wine_dbg_sprintf("%c%c%c%c", 
52                             LOBYTE(LOWORD(fcc)), HIBYTE(LOWORD(fcc)),
53                             LOBYTE(HIWORD(fcc)), HIBYTE(HIWORD(fcc)));
54 }
55
56 LRESULT (CALLBACK *pFnCallTo16)(HDRVR, HIC, UINT, LPARAM, LPARAM) = NULL;
57
58 static WINE_HIC*        MSVIDEO_FirstHic /* = NULL */;
59
60 typedef struct _reg_driver reg_driver;
61 struct _reg_driver
62 {
63     DWORD       fccType;
64     DWORD       fccHandler;
65     DRIVERPROC  proc;
66     LPWSTR      name;
67     reg_driver* next;
68 };
69
70 static reg_driver* reg_driver_list = NULL;
71
72 /* This one is a macro such that it works for both ASCII and Unicode */
73 #define fourcc_to_string(str, fcc) do { \
74         (str)[0] = LOBYTE(LOWORD(fcc)); \
75         (str)[1] = HIBYTE(LOWORD(fcc)); \
76         (str)[2] = LOBYTE(HIWORD(fcc)); \
77         (str)[3] = HIBYTE(HIWORD(fcc)); \
78         } while(0)
79
80 HMODULE MSVFW32_hModule;
81
82 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
83 {
84     TRACE("%p,%x,%p\n", hinst, reason, reserved);
85
86     switch(reason)
87     {
88         case DLL_PROCESS_ATTACH:
89             DisableThreadLibraryCalls(hinst);
90             MSVFW32_hModule = hinst;
91             break;
92     }
93     return TRUE;
94 }
95
96 static int compare_fourcc(DWORD fcc1, DWORD fcc2)
97 {
98   char fcc_str1[4];
99   char fcc_str2[4];
100   fourcc_to_string(fcc_str1, fcc1);
101   fourcc_to_string(fcc_str2, fcc2);
102   return strncasecmp(fcc_str1, fcc_str2, 4);
103 }
104
105 typedef BOOL (*enum_handler_t)(const char*, unsigned int, void*);
106
107 static BOOL enum_drivers(DWORD fccType, enum_handler_t handler, void* param)
108 {
109     CHAR buf[2048], fccTypeStr[5], *s;
110     DWORD i, cnt = 0, lRet;
111     BOOL result = FALSE;
112     HKEY hKey;
113
114     fourcc_to_string(fccTypeStr, fccType);
115     fccTypeStr[4] = '.';
116
117     /* first, go through the registry entries */
118     lRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE, HKLM_DRIVERS32, 0, KEY_QUERY_VALUE, &hKey);
119     if (lRet == ERROR_SUCCESS) 
120     {
121         DWORD name, data, type;
122         i = 0;
123         for (;;)
124         {
125             name = 10;
126             data = sizeof buf - name;
127             lRet = RegEnumValueA(hKey, i++, buf, &name, 0, &type, (LPBYTE)(buf+name), &data);
128             if (lRet == ERROR_NO_MORE_ITEMS) break;
129             if (lRet != ERROR_SUCCESS) continue;
130             if (name != 9 || strncasecmp(buf, fccTypeStr, 5)) continue;
131             buf[name] = '=';
132             if ((result = handler(buf, cnt++, param))) break;
133         }
134         RegCloseKey( hKey );
135     }
136     if (result) return result;
137
138     /* if that didn't work, go through the values in system.ini */
139     if (GetPrivateProfileSectionA("drivers32", buf, sizeof(buf), "system.ini")) 
140     {
141         for (s = buf; *s; s += strlen(s) + 1)
142         {
143             TRACE("got %s\n", s);
144             if (strncasecmp(s, fccTypeStr, 5) || s[9] != '=') continue;
145             if ((result = handler(s, cnt++, param))) break;
146         }
147     }
148
149     return result;
150 }
151
152 /******************************************************************
153  *              MSVIDEO_GetHicPtr
154  *
155  *
156  */
157 WINE_HIC*   MSVIDEO_GetHicPtr(HIC hic)
158 {
159     WINE_HIC*   whic;
160
161     for (whic = MSVIDEO_FirstHic; whic && whic->hic != hic; whic = whic->next);
162     return whic;
163 }
164
165 /***********************************************************************
166  *              VideoForWindowsVersion          [MSVFW32.2]
167  *              VideoForWindowsVersion          [MSVIDEO.2]
168  * Returns the version in major.minor form.
169  * In Windows95 this returns 0x040003b6 (4.950)
170  */
171 DWORD WINAPI VideoForWindowsVersion(void) 
172 {
173     return 0x040003B6; /* 4.950 */
174 }
175
176 static BOOL ICInfo_enum_handler(const char *drv, unsigned int nr, void *param)
177 {
178     ICINFO *lpicinfo = param;
179     DWORD fccHandler = mmioStringToFOURCCA(drv + 5, 0);
180
181     /* exact match of fccHandler or nth driver found */
182     if ((lpicinfo->fccHandler != nr) && (lpicinfo->fccHandler != fccHandler))
183         return FALSE;
184
185     lpicinfo->fccHandler = fccHandler;
186     lpicinfo->dwFlags = 0;
187     lpicinfo->dwVersion = 0;
188     lpicinfo->dwVersionICM = ICVERSION;
189     lpicinfo->szName[0] = 0;
190     lpicinfo->szDescription[0] = 0;
191     MultiByteToWideChar(CP_ACP, 0, drv + 10, -1, lpicinfo->szDriver, 
192                         sizeof(lpicinfo->szDriver)/sizeof(WCHAR));
193
194     return TRUE;
195 }
196
197 /***********************************************************************
198  *              ICInfo                          [MSVFW32.@]
199  * Get information about an installable compressor. Return TRUE if there
200  * is one.
201  *
202  * PARAMS
203  *   fccType     [I] type of compressor (e.g. 'vidc')
204  *   fccHandler  [I] real fcc for handler or <n>th compressor
205  *   lpicinfo    [O] information about compressor
206  */
207 BOOL VFWAPI ICInfo( DWORD fccType, DWORD fccHandler, ICINFO *lpicinfo)
208 {
209     TRACE("(%s,%s/%08x,%p)\n",
210           wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), fccHandler, lpicinfo);
211
212     lpicinfo->fccType = fccType;
213     lpicinfo->fccHandler = fccHandler;
214     return enum_drivers(fccType, ICInfo_enum_handler, lpicinfo);
215 }
216
217 static DWORD IC_HandleRef = 1;
218
219 /***********************************************************************
220  *              ICInstall                       [MSVFW32.@]
221  */
222 BOOL VFWAPI ICInstall(DWORD fccType, DWORD fccHandler, LPARAM lParam, LPSTR szDesc, UINT wFlags) 
223 {
224     reg_driver* driver;
225     unsigned len;
226
227     TRACE("(%s,%s,%p,%p,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), (void*)lParam, szDesc, wFlags);
228
229     /* Check if a driver is already registered */
230     for (driver = reg_driver_list; driver; driver = driver->next)
231     {
232         if (!compare_fourcc(fccType, driver->fccType) &&
233             !compare_fourcc(fccHandler, driver->fccHandler))
234             break;
235     }
236     if (driver) return FALSE;
237
238     /* Register the driver */
239     driver = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(reg_driver));
240     if (!driver) goto oom;
241     driver->fccType = fccType;
242     driver->fccHandler = fccHandler;
243
244     switch(wFlags)
245     {
246     case ICINSTALL_FUNCTION:
247         driver->proc = (DRIVERPROC)lParam;
248         driver->name = NULL;
249         break;
250     case ICINSTALL_DRIVER:
251         driver->proc = NULL;
252         len = MultiByteToWideChar(CP_ACP, 0, (char*)lParam, -1, NULL, 0);
253         driver->name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
254         if (!driver->name) goto oom;
255         MultiByteToWideChar(CP_ACP, 0, (char*)lParam, -1, driver->name, len);
256         break;
257     default:
258         ERR("Invalid flags!\n");
259         HeapFree(GetProcessHeap(), 0, driver);
260         return FALSE;
261    }
262
263    /* Insert our driver in the list*/
264    driver->next = reg_driver_list;
265    reg_driver_list = driver;
266     
267    return TRUE;
268 oom:
269    HeapFree(GetProcessHeap(), 0, driver);
270    return FALSE;
271 }
272
273 /***********************************************************************
274  *              ICRemove                        [MSVFW32.@]
275  */
276 BOOL VFWAPI ICRemove(DWORD fccType, DWORD fccHandler, UINT wFlags) 
277 {
278     reg_driver** pdriver;
279     reg_driver*  drv;
280
281     TRACE("(%s,%s,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wFlags);
282
283     /* Check if a driver is already registered */
284     for (pdriver = &reg_driver_list; *pdriver; pdriver = &(*pdriver)->next)
285     {
286         if (!compare_fourcc(fccType, (*pdriver)->fccType) &&
287             !compare_fourcc(fccHandler, (*pdriver)->fccHandler))
288             break;
289     }
290     if (!*pdriver)
291         return FALSE;
292
293     /* Remove the driver from the list */
294     drv = *pdriver;
295     *pdriver = (*pdriver)->next;
296     HeapFree(GetProcessHeap(), 0, drv->name);
297     HeapFree(GetProcessHeap(), 0, drv);
298     
299     return TRUE;  
300 }
301
302
303 /***********************************************************************
304  *              ICOpen                          [MSVFW32.@]
305  * Opens an installable compressor. Return special handle.
306  */
307 HIC VFWAPI ICOpen(DWORD fccType, DWORD fccHandler, UINT wMode) 
308 {
309     WCHAR               codecname[10];
310     ICOPEN              icopen;
311     HDRVR               hdrv;
312     WINE_HIC*           whic;
313     BOOL                bIs16;
314     static const WCHAR  drv32W[] = {'d','r','i','v','e','r','s','3','2','\0'};
315     reg_driver*         driver;
316
317     TRACE("(%s,%s,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wMode);
318
319     /* Check if there is a registered driver that matches */
320     driver = reg_driver_list;
321     while(driver)
322         if (!compare_fourcc(fccType, driver->fccType) &&
323             !compare_fourcc(fccHandler, driver->fccHandler))
324             break;
325         else
326             driver = driver->next;
327
328     if (driver && driver->proc)
329         /* The driver has been registered at runtime with its driverproc */
330         return MSVIDEO_OpenFunction(fccType, fccHandler, wMode, driver->proc, 0);
331   
332     /* Well, lParam2 is in fact a LPVIDEO_OPEN_PARMS, but it has the
333      * same layout as ICOPEN
334      */
335     icopen.dwSize               = sizeof(ICOPEN);
336     icopen.fccType              = fccType;
337     icopen.fccHandler           = fccHandler;
338     icopen.dwVersion            = 0x00001000; /* FIXME */
339     icopen.dwFlags              = wMode;
340     icopen.dwError              = 0;
341     icopen.pV1Reserved          = NULL;
342     icopen.pV2Reserved          = NULL;
343     icopen.dnDevNode            = 0; /* FIXME */
344         
345     if (!driver) {
346         /* The driver is registered in the registry */
347         fourcc_to_string(codecname, fccType);
348         codecname[4] = '.';
349         fourcc_to_string(codecname + 5, fccHandler);
350         codecname[9] = '\0';
351
352         hdrv = OpenDriver(codecname, drv32W, (LPARAM)&icopen);
353         if (!hdrv) 
354             return 0;
355     } else {
356         /* The driver has been registered at runtime with its name */
357         hdrv = OpenDriver(driver->name, NULL, (LPARAM)&icopen);
358         if (!hdrv) 
359             return 0; 
360     }
361     bIs16 = GetDriverFlags(hdrv) & 0x10000000; /* undocumented flag: WINE_GDF_16BIT */
362
363     if (bIs16 && !pFnCallTo16)
364     {
365         FIXME("Got a 16 bit driver, but no 16 bit support in msvfw\n");
366         return 0;
367     }
368     whic = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_HIC));
369     if (!whic)
370     {
371         CloseDriver(hdrv, 0, 0);
372         return FALSE;
373     }
374     whic->hdrv          = hdrv;
375     /* FIXME: is the signature the real one ? */
376     whic->driverproc    = bIs16 ? (DRIVERPROC)pFnCallTo16 : NULL;
377     whic->driverproc16  = 0;
378     whic->type          = fccType;
379     whic->handler       = fccHandler;
380     while (MSVIDEO_GetHicPtr(HIC_32(IC_HandleRef)) != NULL) IC_HandleRef++;
381     whic->hic           = HIC_32(IC_HandleRef++);
382     whic->next          = MSVIDEO_FirstHic;
383     MSVIDEO_FirstHic = whic;
384
385     TRACE("=> %p\n", whic->hic);
386     return whic->hic;
387 }
388
389 /***********************************************************************
390  *              MSVIDEO_OpenFunction
391  */
392 HIC MSVIDEO_OpenFunction(DWORD fccType, DWORD fccHandler, UINT wMode, 
393                          DRIVERPROC lpfnHandler, DWORD lpfnHandler16) 
394 {
395     ICOPEN      icopen;
396     WINE_HIC*   whic;
397
398     TRACE("(%s,%s,%d,%p,%08x)\n",
399           wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wMode, lpfnHandler, lpfnHandler16);
400
401     icopen.dwSize               = sizeof(ICOPEN);
402     icopen.fccType              = fccType;
403     icopen.fccHandler           = fccHandler;
404     icopen.dwVersion            = ICVERSION;
405     icopen.dwFlags              = wMode;
406     icopen.dwError              = 0;
407     icopen.pV1Reserved          = NULL;
408     icopen.pV2Reserved          = NULL;
409     icopen.dnDevNode            = 0; /* FIXME */
410
411     whic = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_HIC));
412     if (!whic) return 0;
413
414     whic->driverproc   = lpfnHandler;
415     whic->driverproc16 = lpfnHandler16;
416     while (MSVIDEO_GetHicPtr(HIC_32(IC_HandleRef)) != NULL) IC_HandleRef++;
417     whic->hic          = HIC_32(IC_HandleRef++);
418     whic->next         = MSVIDEO_FirstHic;
419     MSVIDEO_FirstHic = whic;
420
421     /* Now try opening/loading the driver. Taken from DRIVER_AddToList */
422     /* What if the function is used more than once? */
423
424     if (MSVIDEO_SendMessage(whic, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) 
425     {
426         WARN("DRV_LOAD failed for hic %p\n", whic->hic);
427         MSVIDEO_FirstHic = whic->next;
428         HeapFree(GetProcessHeap(), 0, whic);
429         return 0;
430     }
431     /* return value is not checked */
432     MSVIDEO_SendMessage(whic, DRV_ENABLE, 0L, 0L);
433
434     whic->driverId = (DWORD)MSVIDEO_SendMessage(whic, DRV_OPEN, 0, (DWORD_PTR)&icopen);
435     /* FIXME: What should we put here? */
436     whic->hdrv = NULL;
437     
438     if (whic->driverId == 0) 
439     {
440         WARN("DRV_OPEN failed for hic %p\n", whic->hic);
441         MSVIDEO_FirstHic = whic->next;
442         HeapFree(GetProcessHeap(), 0, whic);
443         return 0;
444     }
445
446     TRACE("=> %p\n", whic->hic);
447     return whic->hic;
448 }
449
450 /***********************************************************************
451  *              ICOpenFunction                  [MSVFW32.@]
452  */
453 HIC VFWAPI ICOpenFunction(DWORD fccType, DWORD fccHandler, UINT wMode, FARPROC lpfnHandler) 
454 {
455     return MSVIDEO_OpenFunction(fccType, fccHandler, wMode, (DRIVERPROC)lpfnHandler, 0);
456 }
457
458 /***********************************************************************
459  *              ICGetInfo                       [MSVFW32.@]
460  */
461 LRESULT VFWAPI ICGetInfo(HIC hic, ICINFO *picinfo, DWORD cb) 
462 {
463     LRESULT     ret;
464     WINE_HIC*   whic = MSVIDEO_GetHicPtr(hic);
465
466     TRACE("(%p,%p,%d)\n", hic, picinfo, cb);
467
468     whic = MSVIDEO_GetHicPtr(hic);
469     if (!whic) return ICERR_BADHANDLE;
470     if (!picinfo) return MMSYSERR_INVALPARAM;
471
472     /* (WS) The field szDriver should be initialized because the driver 
473      * is not obliged and often will not do it. Some applications, like
474      * VirtualDub, rely on this field and will occasionally crash if it
475      * goes uninitialized.
476      */
477     if (cb >= sizeof(ICINFO)) picinfo->szDriver[0] = '\0';
478
479     ret = ICSendMessage(hic, ICM_GETINFO, (DWORD_PTR)picinfo, cb);
480
481     /* (WS) When szDriver was not supplied by the driver itself, apparently 
482      * Windows will set its value equal to the driver file name. This can
483      * be obtained from the registry as we do here.
484      */
485     if (cb >= sizeof(ICINFO) && picinfo->szDriver[0] == 0)
486     {
487         ICINFO  ii;
488
489         memset(&ii, 0, sizeof(ii));
490         ii.dwSize = sizeof(ii);
491         ICInfo(picinfo->fccType, picinfo->fccHandler, &ii);
492         lstrcpyW(picinfo->szDriver, ii.szDriver);
493     }
494
495     TRACE("     -> 0x%08lx\n", ret);
496     return ret;
497 }
498
499 typedef struct {
500     DWORD fccType;
501     DWORD fccHandler;
502     LPBITMAPINFOHEADER lpbiIn;
503     LPBITMAPINFOHEADER lpbiOut;
504     WORD wMode;
505     DWORD querymsg;
506     HIC hic;
507 } driver_info_t;
508
509 static HIC try_driver(driver_info_t *info)
510 {
511     HIC   hic;
512
513     if ((hic = ICOpen(info->fccType, info->fccHandler, info->wMode))) 
514     {
515         if (!ICSendMessage(hic, info->querymsg, (DWORD_PTR)info->lpbiIn, (DWORD_PTR)info->lpbiOut))
516             return hic;
517         ICClose(hic);
518     }
519     return 0;
520 }
521
522 static BOOL ICLocate_enum_handler(const char *drv, unsigned int nr, void *param)
523 {
524     driver_info_t *info = param;
525     info->fccHandler = mmioStringToFOURCCA(drv + 5, 0);
526     info->hic = try_driver(info);
527     return info->hic != 0;
528 }
529
530 /***********************************************************************
531  *              ICLocate                        [MSVFW32.@]
532  */
533 HIC VFWAPI ICLocate(DWORD fccType, DWORD fccHandler, LPBITMAPINFOHEADER lpbiIn,
534                     LPBITMAPINFOHEADER lpbiOut, WORD wMode)
535 {
536     driver_info_t info;
537
538     TRACE("(%s,%s,%p,%p,0x%04x)\n", 
539           wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), lpbiIn, lpbiOut, wMode);
540
541     info.fccType = fccType;
542     info.fccHandler = fccHandler;
543     info.lpbiIn = lpbiIn;
544     info.lpbiOut = lpbiOut;
545     info.wMode = wMode;
546
547     switch (wMode) 
548     {
549     case ICMODE_FASTCOMPRESS:
550     case ICMODE_COMPRESS:
551         info.querymsg = ICM_COMPRESS_QUERY;
552         break;
553     case ICMODE_FASTDECOMPRESS:
554     case ICMODE_DECOMPRESS:
555         info.querymsg = ICM_DECOMPRESS_QUERY;
556         break;
557     case ICMODE_DRAW:
558         info.querymsg = ICM_DRAW_QUERY;
559         break;
560     default:
561         WARN("Unknown mode (%d)\n", wMode);
562         return 0;
563     }
564
565     /* Easy case: handler/type match, we just fire a query and return */
566     info.hic = try_driver(&info);
567     /* If it didn't work, try each driver in turn. 32 bit codecs only. */
568     /* FIXME: Move this to an init routine? */
569     if (!info.hic) enum_drivers(fccType, ICLocate_enum_handler, &info);
570
571     if (info.hic) 
572     {
573         TRACE("=> %p\n", info.hic);
574         return info.hic;
575     }
576
577     if (fccType == streamtypeVIDEO) 
578         return ICLocate(ICTYPE_VIDEO, fccHandler, lpbiIn, lpbiOut, wMode);
579     
580     WARN("(%s,%s,%p,%p,0x%04x) not found!\n",
581          wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), lpbiIn, lpbiOut, wMode);
582     return 0;
583 }
584
585 /***********************************************************************
586  *              ICGetDisplayFormat                      [MSVFW32.@]
587  */
588 HIC VFWAPI ICGetDisplayFormat(
589         HIC hic,LPBITMAPINFOHEADER lpbiIn,LPBITMAPINFOHEADER lpbiOut,
590         INT depth,INT dx,INT dy)
591 {
592         HIC     tmphic = hic;
593
594         TRACE("(%p,%p,%p,%d,%d,%d)!\n",hic,lpbiIn,lpbiOut,depth,dx,dy);
595
596         if (!tmphic) {
597                 tmphic=ICLocate(ICTYPE_VIDEO,0,lpbiIn,NULL,ICMODE_DECOMPRESS);
598                 if (!tmphic)
599                         return tmphic;
600         }
601         if ((dy == lpbiIn->biHeight) && (dx == lpbiIn->biWidth))
602                 dy = dx = 0; /* no resize needed */
603
604         /* Can we decompress it ? */
605         if (ICDecompressQuery(tmphic,lpbiIn,NULL) != 0)
606                 goto errout; /* no, sorry */
607
608         ICSendMessage(tmphic, ICM_DECOMPRESS_GET_FORMAT, (DWORD_PTR)lpbiIn, (DWORD_PTR)lpbiOut);
609
610         if (lpbiOut->biCompression != 0) {
611            FIXME("Ooch, how come decompressor outputs compressed data (%d)??\n",
612                          lpbiOut->biCompression);
613         }
614         if (lpbiOut->biSize < sizeof(*lpbiOut)) {
615            FIXME("Ooch, size of output BIH is too small (%d)\n",
616                          lpbiOut->biSize);
617            lpbiOut->biSize = sizeof(*lpbiOut);
618         }
619         if (!depth) {
620                 HDC     hdc;
621
622                 hdc = GetDC(0);
623                 depth = GetDeviceCaps(hdc,BITSPIXEL)*GetDeviceCaps(hdc,PLANES);
624                 ReleaseDC(0,hdc);
625                 if (depth==15)  depth = 16;
626                 if (depth<8)    depth =  8;
627         }
628         if (lpbiIn->biBitCount == 8)
629                 depth = 8;
630
631         TRACE("=> %p\n", tmphic);
632         return tmphic;
633 errout:
634         if (hic!=tmphic)
635                 ICClose(tmphic);
636
637         TRACE("=> 0\n");
638         return 0;
639 }
640
641 /***********************************************************************
642  *              ICCompress                      [MSVFW32.@]
643  */
644 DWORD VFWAPIV
645 ICCompress(
646         HIC hic,DWORD dwFlags,LPBITMAPINFOHEADER lpbiOutput,LPVOID lpData,
647         LPBITMAPINFOHEADER lpbiInput,LPVOID lpBits,LPDWORD lpckid,
648         LPDWORD lpdwFlags,LONG lFrameNum,DWORD dwFrameSize,DWORD dwQuality,
649         LPBITMAPINFOHEADER lpbiPrev,LPVOID lpPrev)
650 {
651         ICCOMPRESS      iccmp;
652
653         TRACE("(%p,%d,%p,%p,%p,%p,...)\n",hic,dwFlags,lpbiOutput,lpData,lpbiInput,lpBits);
654
655         iccmp.dwFlags           = dwFlags;
656
657         iccmp.lpbiOutput        = lpbiOutput;
658         iccmp.lpOutput          = lpData;
659         iccmp.lpbiInput         = lpbiInput;
660         iccmp.lpInput           = lpBits;
661
662         iccmp.lpckid            = lpckid;
663         iccmp.lpdwFlags         = lpdwFlags;
664         iccmp.lFrameNum         = lFrameNum;
665         iccmp.dwFrameSize       = dwFrameSize;
666         iccmp.dwQuality         = dwQuality;
667         iccmp.lpbiPrev          = lpbiPrev;
668         iccmp.lpPrev            = lpPrev;
669         return ICSendMessage(hic,ICM_COMPRESS,(DWORD_PTR)&iccmp,sizeof(iccmp));
670 }
671
672 /***********************************************************************
673  *              ICDecompress                    [MSVFW32.@]
674  */
675 DWORD VFWAPIV  ICDecompress(HIC hic,DWORD dwFlags,LPBITMAPINFOHEADER lpbiFormat,
676                                 LPVOID lpData,LPBITMAPINFOHEADER lpbi,LPVOID lpBits)
677 {
678         ICDECOMPRESS    icd;
679         DWORD ret;
680
681         TRACE("(%p,%d,%p,%p,%p,%p)\n",hic,dwFlags,lpbiFormat,lpData,lpbi,lpBits);
682
683         icd.dwFlags     = dwFlags;
684         icd.lpbiInput   = lpbiFormat;
685         icd.lpInput     = lpData;
686
687         icd.lpbiOutput  = lpbi;
688         icd.lpOutput    = lpBits;
689         icd.ckid        = 0;
690         ret = ICSendMessage(hic,ICM_DECOMPRESS,(DWORD_PTR)&icd,sizeof(ICDECOMPRESS));
691
692         TRACE("-> %d\n",ret);
693
694         return ret;
695 }
696
697
698 struct choose_compressor
699 {
700     UINT flags;
701     LPCSTR title;
702     COMPVARS cv;
703 };
704
705 struct codec_info
706 {
707     HIC hic;
708     ICINFO icinfo;
709 };
710
711 static BOOL enum_compressors(HWND list, COMPVARS *pcv, BOOL enum_all)
712 {
713     UINT id, total = 0;
714     ICINFO icinfo;
715
716     id = 0;
717
718     while (ICInfo(pcv->fccType, id, &icinfo))
719     {
720         struct codec_info *ic;
721         DWORD idx;
722         HIC hic;
723
724         id++;
725
726         hic = ICOpen(icinfo.fccType, icinfo.fccHandler, ICMODE_COMPRESS);
727
728         if (hic)
729         {
730             /* for unknown reason fccHandler reported by the driver
731              * doesn't always work, use the one returned by ICInfo instead.
732              */
733             DWORD fccHandler = icinfo.fccHandler;
734
735             if (!enum_all && pcv->lpbiIn)
736             {
737                 if (ICCompressQuery(hic, pcv->lpbiIn, NULL) != ICERR_OK)
738                 {
739                     TRACE("fccHandler %s doesn't support input DIB format %d\n",
740                           wine_dbgstr_fcc(icinfo.fccHandler), pcv->lpbiIn->bmiHeader.biCompression);
741                     ICClose(hic);
742                     continue;
743                 }
744             }
745
746             ICGetInfo(hic, &icinfo, sizeof(icinfo));
747             icinfo.fccHandler = fccHandler;
748
749             idx = SendMessageW(list, CB_ADDSTRING, 0, (LPARAM)icinfo.szDescription);
750
751             ic = HeapAlloc(GetProcessHeap(), 0, sizeof(struct codec_info));
752             ic->icinfo = icinfo;
753             ic->hic = hic;
754             SendMessageW(list, CB_SETITEMDATA, idx, (LPARAM)ic);
755         }
756         total++;
757     }
758
759     return total != 0;
760 }
761
762 static INT_PTR CALLBACK icm_choose_compressor_dlgproc(HWND hdlg, UINT msg, WPARAM wparam, LPARAM lparam)
763 {
764     switch (msg)
765     {
766     case WM_INITDIALOG:
767     {
768         struct codec_info *ic;
769         WCHAR buf[128];
770         struct choose_compressor *choose_comp = (struct choose_compressor *)lparam;
771
772         SetWindowLongPtrW(hdlg, DWLP_USER, lparam);
773
774         /* FIXME */
775         choose_comp->flags &= ~(ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME);
776
777         if (choose_comp->title)
778             SetWindowTextA(hdlg, choose_comp->title);
779
780         if (!(choose_comp->flags & ICMF_CHOOSE_DATARATE))
781         {
782             ShowWindow(GetDlgItem(hdlg, IDC_DATARATE_CHECKBOX), SW_HIDE);
783             ShowWindow(GetDlgItem(hdlg, IDC_DATARATE), SW_HIDE);
784             ShowWindow(GetDlgItem(hdlg, IDC_DATARATE_KB), SW_HIDE);
785         }
786
787         if (!(choose_comp->flags & ICMF_CHOOSE_KEYFRAME))
788         {
789             ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME_CHECKBOX), SW_HIDE);
790             ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME), SW_HIDE);
791             ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME_FRAMES), SW_HIDE);
792         }
793
794         /* FIXME */
795         EnableWindow(GetDlgItem(hdlg, IDC_QUALITY_SCROLL), FALSE);
796         EnableWindow(GetDlgItem(hdlg, IDC_QUALITY_TXT), FALSE);
797
798         /*if (!(choose_comp->flags & ICMF_CHOOSE_PREVIEW))
799             ShowWindow(GetDlgItem(hdlg, IDC_PREVIEW), SW_HIDE);*/
800
801         LoadStringW(MSVFW32_hModule, IDS_FULLFRAMES, buf, 128);
802         SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_ADDSTRING, 0, (LPARAM)buf);
803
804         ic = HeapAlloc(GetProcessHeap(), 0, sizeof(struct codec_info));
805         ic->icinfo.fccType = streamtypeVIDEO;
806         ic->icinfo.fccHandler = comptypeDIB;
807         ic->hic = 0;
808         SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_SETITEMDATA, 0, (LPARAM)ic);
809
810         enum_compressors(GetDlgItem(hdlg, IDC_COMP_LIST), &choose_comp->cv, choose_comp->flags & ICMF_CHOOSE_ALLCOMPRESSORS);
811
812         SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_SETCURSEL, 0, 0);
813         SetFocus(GetDlgItem(hdlg, IDC_COMP_LIST));
814
815         SetWindowLongPtrW(hdlg, DWLP_USER, (ULONG_PTR)choose_comp);
816         break;
817     }
818
819     case WM_COMMAND:
820         switch (LOWORD(wparam))
821         {
822         case IDC_COMP_LIST:
823         {
824             INT cur_sel;
825             struct codec_info *ic;
826             BOOL can_configure = FALSE, can_about = FALSE;
827             struct choose_compressor *choose_comp;
828
829             if (HIWORD(wparam) != CBN_SELCHANGE && HIWORD(wparam) != CBN_SETFOCUS)
830                 break;
831
832             choose_comp = (struct choose_compressor *)GetWindowLongPtrW(hdlg, DWLP_USER);
833
834             cur_sel = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
835
836             ic = (struct codec_info *)SendMessageW((HWND)lparam, CB_GETITEMDATA, cur_sel, 0);
837             if (ic && ic->hic)
838             {
839                 if (ICQueryConfigure(ic->hic) == DRVCNF_OK)
840                     can_configure = TRUE;
841                 if (ICQueryAbout(ic->hic) == DRVCNF_OK)
842                     can_about = TRUE;
843             }
844             EnableWindow(GetDlgItem(hdlg, IDC_CONFIGURE), can_configure);
845             EnableWindow(GetDlgItem(hdlg, IDC_ABOUT), can_about);
846
847             if (choose_comp->flags & ICMF_CHOOSE_DATARATE)
848             {
849                 /* FIXME */
850             }
851             if (choose_comp->flags & ICMF_CHOOSE_KEYFRAME)
852             {
853                 /* FIXME */
854             }
855
856             break;
857         }
858
859         case IDC_CONFIGURE:
860         case IDC_ABOUT:
861         {
862             HWND list = GetDlgItem(hdlg, IDC_COMP_LIST);
863             INT cur_sel;
864             struct codec_info *ic;
865
866             if (HIWORD(wparam) != BN_CLICKED)
867                 break;
868
869             cur_sel = SendMessageW(list, CB_GETCURSEL, 0, 0);
870
871             ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, cur_sel, 0);
872             if (ic && ic->hic)
873             {
874                 if (LOWORD(wparam) == IDC_CONFIGURE)
875                     ICConfigure(ic->hic, hdlg);
876                 else
877                     ICAbout(ic->hic, hdlg);
878             }
879
880             break;
881         }
882
883         case IDOK:
884         {
885             HWND list = GetDlgItem(hdlg, IDC_COMP_LIST);
886             INT cur_sel;
887             struct codec_info *ic;
888
889             if (HIWORD(wparam) != BN_CLICKED)
890                 break;
891
892             cur_sel = SendMessageW(list, CB_GETCURSEL, 0, 0);
893             ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, cur_sel, 0);
894             if (ic)
895             {
896                 struct choose_compressor *choose_comp = (struct choose_compressor *)GetWindowLongPtrW(hdlg, DWLP_USER);
897
898                 choose_comp->cv.hic = ic->hic;
899                 choose_comp->cv.fccType = ic->icinfo.fccType;
900                 choose_comp->cv.fccHandler = ic->icinfo.fccHandler;
901                 /* FIXME: fill everything else */
902
903                 /* prevent closing the codec handle below */
904                 ic->hic = 0;
905             }
906         }
907         /* fall through */
908         case IDCANCEL:
909         {
910             HWND list = GetDlgItem(hdlg, IDC_COMP_LIST);
911             INT idx = 0;
912
913             if (HIWORD(wparam) != BN_CLICKED)
914                 break;
915
916             while (1)
917             {
918                 struct codec_info *ic;
919     
920                 ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, idx++, 0);
921
922                 if (!ic || (LONG_PTR)ic == CB_ERR) break;
923
924                 if (ic->hic) ICClose(ic->hic);
925                 HeapFree(GetProcessHeap(), 0, ic);
926             }
927
928             EndDialog(hdlg, LOWORD(wparam) == IDOK);
929             break;
930         }
931
932         default:
933             break;
934         }
935         break;
936
937     default:
938         break;
939     }
940
941     return FALSE;
942 }
943
944 /***********************************************************************
945  *              ICCompressorChoose   [MSVFW32.@]
946  */
947 BOOL VFWAPI ICCompressorChoose(HWND hwnd, UINT uiFlags, LPVOID pvIn,
948                                LPVOID lpData, PCOMPVARS pc, LPSTR lpszTitle)
949 {
950     struct choose_compressor choose_comp;
951     BOOL ret;
952
953     TRACE("(%p,%08x,%p,%p,%p,%s)\n", hwnd, uiFlags, pvIn, lpData, pc, lpszTitle);
954
955     if (!pc || pc->cbSize != sizeof(COMPVARS))
956         return FALSE;
957
958     if (!(pc->dwFlags & ICMF_COMPVARS_VALID))
959     {
960         pc->dwFlags   = 0;
961         pc->fccType   = pc->fccHandler = 0;
962         pc->hic       = NULL;
963         pc->lpbiIn    = NULL;
964         pc->lpbiOut   = NULL;
965         pc->lpBitsOut = pc->lpBitsPrev = pc->lpState = NULL;
966         pc->lQ        = ICQUALITY_DEFAULT;
967         pc->lKey      = -1;
968         pc->lDataRate = 300; /* kB */
969         pc->lpState   = NULL;
970         pc->cbState   = 0;
971     }
972     if (pc->fccType == 0)
973         pc->fccType = ICTYPE_VIDEO;
974
975     choose_comp.cv = *pc;
976     choose_comp.flags = uiFlags;
977     choose_comp.title = lpszTitle;
978
979     ret = DialogBoxParamW(MSVFW32_hModule, MAKEINTRESOURCEW(ICM_CHOOSE_COMPRESSOR), hwnd,
980                           icm_choose_compressor_dlgproc, (LPARAM)&choose_comp);
981
982     if (ret)
983     {
984         *pc = choose_comp.cv;
985         pc->dwFlags |= ICMF_COMPVARS_VALID;
986     }
987
988     return ret;
989 }
990
991
992 /***********************************************************************
993  *              ICCompressorFree   [MSVFW32.@]
994  */
995 void VFWAPI ICCompressorFree(PCOMPVARS pc)
996 {
997   TRACE("(%p)\n",pc);
998
999   if (pc != NULL && pc->cbSize == sizeof(COMPVARS)) {
1000     if (pc->hic != NULL) {
1001       ICClose(pc->hic);
1002       pc->hic = NULL;
1003     }
1004     HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1005     pc->lpbiIn = NULL;
1006     HeapFree(GetProcessHeap(), 0, pc->lpBitsOut);
1007     pc->lpBitsOut = NULL;
1008     HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1009     pc->lpBitsPrev = NULL;
1010     HeapFree(GetProcessHeap(), 0, pc->lpState);
1011     pc->lpState = NULL;
1012     pc->dwFlags = 0;
1013   }
1014 }
1015
1016
1017 /******************************************************************
1018  *              MSVIDEO_SendMessage
1019  *
1020  *
1021  */
1022 LRESULT MSVIDEO_SendMessage(WINE_HIC* whic, UINT msg, DWORD_PTR lParam1, DWORD_PTR lParam2)
1023 {
1024     LRESULT     ret;
1025
1026 #define XX(x) case x: TRACE("(%p,"#x",0x%08lx,0x%08lx)\n",whic,lParam1,lParam2); break
1027
1028     switch (msg) {
1029         /* DRV_* */
1030         XX(DRV_LOAD);
1031         XX(DRV_ENABLE);
1032         XX(DRV_OPEN);
1033         XX(DRV_CLOSE);
1034         XX(DRV_DISABLE);
1035         XX(DRV_FREE);
1036         /* ICM_RESERVED+X */
1037         XX(ICM_ABOUT);
1038         XX(ICM_CONFIGURE);
1039         XX(ICM_GET);
1040         XX(ICM_GETINFO);
1041         XX(ICM_GETDEFAULTQUALITY);
1042         XX(ICM_GETQUALITY);
1043         XX(ICM_GETSTATE);
1044         XX(ICM_SETQUALITY);
1045         XX(ICM_SET);
1046         XX(ICM_SETSTATE);
1047         /* ICM_USER+X */
1048         XX(ICM_COMPRESS_FRAMES_INFO);
1049         XX(ICM_COMPRESS_GET_FORMAT);
1050         XX(ICM_COMPRESS_GET_SIZE);
1051         XX(ICM_COMPRESS_QUERY);
1052         XX(ICM_COMPRESS_BEGIN);
1053         XX(ICM_COMPRESS);
1054         XX(ICM_COMPRESS_END);
1055         XX(ICM_DECOMPRESS_GET_FORMAT);
1056         XX(ICM_DECOMPRESS_QUERY);
1057         XX(ICM_DECOMPRESS_BEGIN);
1058         XX(ICM_DECOMPRESS);
1059         XX(ICM_DECOMPRESS_END);
1060         XX(ICM_DECOMPRESS_SET_PALETTE);
1061         XX(ICM_DECOMPRESS_GET_PALETTE);
1062         XX(ICM_DRAW_QUERY);
1063         XX(ICM_DRAW_BEGIN);
1064         XX(ICM_DRAW_GET_PALETTE);
1065         XX(ICM_DRAW_START);
1066         XX(ICM_DRAW_STOP);
1067         XX(ICM_DRAW_END);
1068         XX(ICM_DRAW_GETTIME);
1069         XX(ICM_DRAW);
1070         XX(ICM_DRAW_WINDOW);
1071         XX(ICM_DRAW_SETTIME);
1072         XX(ICM_DRAW_REALIZE);
1073         XX(ICM_DRAW_FLUSH);
1074         XX(ICM_DRAW_RENDERBUFFER);
1075         XX(ICM_DRAW_START_PLAY);
1076         XX(ICM_DRAW_STOP_PLAY);
1077         XX(ICM_DRAW_SUGGESTFORMAT);
1078         XX(ICM_DRAW_CHANGEPALETTE);
1079         XX(ICM_GETBUFFERSWANTED);
1080         XX(ICM_GETDEFAULTKEYFRAMERATE);
1081         XX(ICM_DECOMPRESSEX_BEGIN);
1082         XX(ICM_DECOMPRESSEX_QUERY);
1083         XX(ICM_DECOMPRESSEX);
1084         XX(ICM_DECOMPRESSEX_END);
1085         XX(ICM_SET_STATUS_PROC);
1086     default:
1087         FIXME("(%p,0x%08x,0x%08lx,0x%08lx) unknown message\n",whic,msg,lParam1,lParam2);
1088     }
1089     
1090 #undef XX
1091     
1092     if (whic->driverproc) {
1093         /* dwDriverId parameter is the value returned by the DRV_OPEN */
1094         ret = whic->driverproc(whic->driverId, whic->hdrv, msg, lParam1, lParam2);
1095     } else {
1096         ret = SendDriverMessage(whic->hdrv, msg, lParam1, lParam2);
1097     }
1098
1099     TRACE("     -> 0x%08lx\n", ret);
1100     return ret;
1101 }
1102
1103 /***********************************************************************
1104  *              ICSendMessage                   [MSVFW32.@]
1105  */
1106 LRESULT VFWAPI ICSendMessage(HIC hic, UINT msg, DWORD_PTR lParam1, DWORD_PTR lParam2) 
1107 {
1108     WINE_HIC*   whic = MSVIDEO_GetHicPtr(hic);
1109
1110     if (!whic) return ICERR_BADHANDLE;
1111     return MSVIDEO_SendMessage(whic, msg, lParam1, lParam2);
1112 }
1113
1114 /***********************************************************************
1115  *              ICDrawBegin             [MSVFW32.@]
1116  */
1117 DWORD VFWAPIV ICDrawBegin(
1118         HIC                hic,     /* [in] */
1119         DWORD              dwFlags, /* [in] flags */
1120         HPALETTE           hpal,    /* [in] palette to draw with */
1121         HWND               hwnd,    /* [in] window to draw to */
1122         HDC                hdc,     /* [in] HDC to draw to */
1123         INT                xDst,    /* [in] destination rectangle */
1124         INT                yDst,    /* [in] */
1125         INT                dxDst,   /* [in] */
1126         INT                dyDst,   /* [in] */
1127         LPBITMAPINFOHEADER lpbi,    /* [in] format of frame to draw */
1128         INT                xSrc,    /* [in] source rectangle */
1129         INT                ySrc,    /* [in] */
1130         INT                dxSrc,   /* [in] */
1131         INT                dySrc,   /* [in] */
1132         DWORD              dwRate,  /* [in] frames/second = (dwRate/dwScale) */
1133         DWORD              dwScale) /* [in] */
1134 {
1135
1136         ICDRAWBEGIN     icdb;
1137
1138         TRACE("(%p,%d,%p,%p,%p,%u,%u,%u,%u,%p,%u,%u,%u,%u,%d,%d)\n",
1139                   hic, dwFlags, hpal, hwnd, hdc, xDst, yDst, dxDst, dyDst,
1140                   lpbi, xSrc, ySrc, dxSrc, dySrc, dwRate, dwScale);
1141
1142         icdb.dwFlags = dwFlags;
1143         icdb.hpal = hpal;
1144         icdb.hwnd = hwnd;
1145         icdb.hdc = hdc;
1146         icdb.xDst = xDst;
1147         icdb.yDst = yDst;
1148         icdb.dxDst = dxDst;
1149         icdb.dyDst = dyDst;
1150         icdb.lpbi = lpbi;
1151         icdb.xSrc = xSrc;
1152         icdb.ySrc = ySrc;
1153         icdb.dxSrc = dxSrc;
1154         icdb.dySrc = dySrc;
1155         icdb.dwRate = dwRate;
1156         icdb.dwScale = dwScale;
1157         return ICSendMessage(hic,ICM_DRAW_BEGIN,(DWORD_PTR)&icdb,sizeof(icdb));
1158 }
1159
1160 /***********************************************************************
1161  *              ICDraw                  [MSVFW32.@]
1162  */
1163 DWORD VFWAPIV ICDraw(HIC hic, DWORD dwFlags, LPVOID lpFormat, LPVOID lpData, DWORD cbData, LONG lTime) {
1164         ICDRAW  icd;
1165
1166         TRACE("(%p,%d,%p,%p,%d,%d)\n",hic,dwFlags,lpFormat,lpData,cbData,lTime);
1167
1168         icd.dwFlags = dwFlags;
1169         icd.lpFormat = lpFormat;
1170         icd.lpData = lpData;
1171         icd.cbData = cbData;
1172         icd.lTime = lTime;
1173
1174         return ICSendMessage(hic,ICM_DRAW,(DWORD_PTR)&icd,sizeof(icd));
1175 }
1176
1177 /***********************************************************************
1178  *              ICClose                 [MSVFW32.@]
1179  */
1180 LRESULT WINAPI ICClose(HIC hic)
1181 {
1182     WINE_HIC* whic = MSVIDEO_GetHicPtr(hic);
1183     WINE_HIC** p;
1184
1185     TRACE("(%p)\n",hic);
1186
1187     if (!whic) return ICERR_BADHANDLE;
1188
1189     if (whic->driverproc) 
1190     {
1191         MSVIDEO_SendMessage(whic, DRV_CLOSE, 0, 0);
1192         MSVIDEO_SendMessage(whic, DRV_DISABLE, 0, 0);
1193         MSVIDEO_SendMessage(whic, DRV_FREE, 0, 0);
1194     }
1195     else
1196     {
1197         CloseDriver(whic->hdrv, 0, 0);
1198     }
1199
1200     /* remove whic from list */
1201     for (p = &MSVIDEO_FirstHic; *p != NULL; p = &((*p)->next))
1202     {
1203         if ((*p) == whic)
1204         {
1205             *p = whic->next;
1206             break;
1207         }
1208     }
1209
1210     HeapFree(GetProcessHeap(), 0, whic);
1211     return 0;
1212 }
1213
1214
1215
1216 /***********************************************************************
1217  *              ICImageCompress [MSVFW32.@]
1218  */
1219 HANDLE VFWAPI ICImageCompress(
1220         HIC hic, UINT uiFlags,
1221         LPBITMAPINFO lpbiIn, LPVOID lpBits,
1222         LPBITMAPINFO lpbiOut, LONG lQuality,
1223         LONG* plSize)
1224 {
1225         FIXME("(%p,%08x,%p,%p,%p,%d,%p)\n",
1226                 hic, uiFlags, lpbiIn, lpBits, lpbiOut, lQuality, plSize);
1227
1228         return NULL;
1229 }
1230
1231 /***********************************************************************
1232  *              ICImageDecompress       [MSVFW32.@]
1233  */
1234
1235 HANDLE VFWAPI ICImageDecompress(
1236         HIC hic, UINT uiFlags, LPBITMAPINFO lpbiIn,
1237         LPVOID lpBits, LPBITMAPINFO lpbiOut)
1238 {
1239         HGLOBAL hMem = NULL;
1240         BYTE*   pMem = NULL;
1241         BOOL    bReleaseIC = FALSE;
1242         BYTE*   pHdr = NULL;
1243         ULONG   cbHdr = 0;
1244         BOOL    bSucceeded = FALSE;
1245         BOOL    bInDecompress = FALSE;
1246         DWORD   biSizeImage;
1247
1248         TRACE("(%p,%08x,%p,%p,%p)\n",
1249                 hic, uiFlags, lpbiIn, lpBits, lpbiOut);
1250
1251         if ( hic == NULL )
1252         {
1253                 hic = ICDecompressOpen( ICTYPE_VIDEO, 0, &lpbiIn->bmiHeader, (lpbiOut != NULL) ? &lpbiOut->bmiHeader : NULL );
1254                 if ( hic == NULL )
1255                 {
1256                         WARN("no handler\n" );
1257                         goto err;
1258                 }
1259                 bReleaseIC = TRUE;
1260         }
1261         if ( uiFlags != 0 )
1262         {
1263                 FIXME( "unknown flag %08x\n", uiFlags );
1264                 goto err;
1265         }
1266         if ( lpbiIn == NULL || lpBits == NULL )
1267         {
1268                 WARN("invalid argument\n");
1269                 goto err;
1270         }
1271
1272         if ( lpbiOut != NULL )
1273         {
1274                 if ( lpbiOut->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) )
1275                         goto err;
1276                 cbHdr = sizeof(BITMAPINFOHEADER);
1277                 if ( lpbiOut->bmiHeader.biCompression == 3 )
1278                         cbHdr += sizeof(DWORD)*3;
1279                 else
1280                 if ( lpbiOut->bmiHeader.biBitCount <= 8 )
1281                 {
1282                         if ( lpbiOut->bmiHeader.biClrUsed == 0 )
1283                                 cbHdr += sizeof(RGBQUAD) * (1<<lpbiOut->bmiHeader.biBitCount);
1284                         else
1285                                 cbHdr += sizeof(RGBQUAD) * lpbiOut->bmiHeader.biClrUsed;
1286                 }
1287         }
1288         else
1289         {
1290                 TRACE( "get format\n" );
1291
1292                 cbHdr = ICDecompressGetFormatSize(hic,lpbiIn);
1293                 if ( cbHdr < sizeof(BITMAPINFOHEADER) )
1294                         goto err;
1295                 pHdr = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,cbHdr+sizeof(RGBQUAD)*256);
1296                 if ( pHdr == NULL )
1297                         goto err;
1298                 if ( ICDecompressGetFormat( hic, lpbiIn, pHdr ) != ICERR_OK )
1299                         goto err;
1300                 lpbiOut = (BITMAPINFO*)pHdr;
1301                 if ( lpbiOut->bmiHeader.biBitCount <= 8 &&
1302                          ICDecompressGetPalette( hic, lpbiIn, lpbiOut ) != ICERR_OK &&
1303                          lpbiIn->bmiHeader.biBitCount == lpbiOut->bmiHeader.biBitCount )
1304                 {
1305                         if ( lpbiIn->bmiHeader.biClrUsed == 0 )
1306                                 memcpy( lpbiOut->bmiColors, lpbiIn->bmiColors, sizeof(RGBQUAD)*(1<<lpbiOut->bmiHeader.biBitCount) );
1307                         else
1308                                 memcpy( lpbiOut->bmiColors, lpbiIn->bmiColors, sizeof(RGBQUAD)*lpbiIn->bmiHeader.biClrUsed );
1309                 }
1310                 if ( lpbiOut->bmiHeader.biBitCount <= 8 &&
1311                          lpbiOut->bmiHeader.biClrUsed == 0 )
1312                         lpbiOut->bmiHeader.biClrUsed = 1<<lpbiOut->bmiHeader.biBitCount;
1313
1314                 lpbiOut->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1315                 cbHdr = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*lpbiOut->bmiHeader.biClrUsed;
1316         }
1317
1318         biSizeImage = lpbiOut->bmiHeader.biSizeImage;
1319         if ( biSizeImage == 0 )
1320                 biSizeImage = ((((lpbiOut->bmiHeader.biWidth * lpbiOut->bmiHeader.biBitCount + 7) >> 3) + 3) & (~3)) * abs(lpbiOut->bmiHeader.biHeight);
1321
1322         TRACE( "call ICDecompressBegin\n" );
1323
1324         if ( ICDecompressBegin( hic, lpbiIn, lpbiOut ) != ICERR_OK )
1325                 goto err;
1326         bInDecompress = TRUE;
1327
1328         TRACE( "cbHdr %d, biSizeImage %d\n", cbHdr, biSizeImage );
1329
1330         hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_ZEROINIT, cbHdr + biSizeImage );
1331         if ( hMem == NULL )
1332         {
1333                 WARN( "out of memory\n" );
1334                 goto err;
1335         }
1336         pMem = GlobalLock( hMem );
1337         if ( pMem == NULL )
1338                 goto err;
1339         memcpy( pMem, lpbiOut, cbHdr );
1340
1341         TRACE( "call ICDecompress\n" );
1342         if ( ICDecompress( hic, 0, &lpbiIn->bmiHeader, lpBits, &lpbiOut->bmiHeader, pMem+cbHdr ) != ICERR_OK )
1343                 goto err;
1344
1345         bSucceeded = TRUE;
1346 err:
1347         if ( bInDecompress )
1348                 ICDecompressEnd( hic );
1349         if ( bReleaseIC )
1350                 ICClose(hic);
1351         HeapFree(GetProcessHeap(),0,pHdr);
1352         if ( pMem != NULL )
1353                 GlobalUnlock( hMem );
1354         if ( !bSucceeded && hMem != NULL )
1355         {
1356                 GlobalFree(hMem); hMem = NULL;
1357         }
1358
1359         return hMem;
1360 }
1361
1362 /***********************************************************************
1363  *      ICSeqCompressFrame   [MSVFW32.@]
1364  */
1365 LPVOID VFWAPI ICSeqCompressFrame(PCOMPVARS pc, UINT uiFlags, LPVOID lpBits, BOOL *pfKey, LONG *plSize)
1366 {
1367     ICCOMPRESS* icComp = pc->lpState;
1368     DWORD ret;
1369     TRACE("(%p, 0x%08x, %p, %p, %p)\n", pc, uiFlags, lpBits, pfKey, plSize);
1370
1371     if (pc->cbState != sizeof(ICCOMPRESS))
1372     {
1373        ERR("Invalid cbState %i\n", pc->cbState);
1374        return NULL;
1375     }
1376
1377     if (!pc->lKeyCount++)
1378        icComp->dwFlags = ICCOMPRESS_KEYFRAME;
1379     else
1380     {
1381         if (pc->lKey && pc->lKeyCount == (pc->lKey - 1))
1382         /* No key frames if pc->lKey == 0 */
1383            pc->lKeyCount = 0;
1384         icComp->dwFlags = 0;
1385     }
1386
1387     icComp->lpInput = lpBits;
1388     icComp->lFrameNum = pc->lFrame++;
1389     icComp->lpOutput = pc->lpBitsOut;
1390     icComp->lpPrev = pc->lpBitsPrev;
1391     ret = ICSendMessage(pc->hic, ICM_COMPRESS, (DWORD_PTR)icComp, sizeof(icComp));
1392
1393     if (icComp->dwFlags & AVIIF_KEYFRAME)
1394     {
1395        pc->lKeyCount = 1;
1396        *pfKey = TRUE;
1397        TRACE("Key frame\n");
1398     }
1399     else
1400        *pfKey = FALSE;
1401
1402     *plSize = icComp->lpbiOutput->biSizeImage;
1403     TRACE(" -- 0x%08x\n", ret);
1404     if (ret == ICERR_OK)
1405     {
1406        LPVOID oldprev, oldout;
1407 /* We shift Prev and Out, so we don't have to allocate and release memory */
1408        oldprev = pc->lpBitsPrev;
1409        oldout = pc->lpBitsOut;
1410        pc->lpBitsPrev = oldout;
1411        pc->lpBitsOut = oldprev;
1412
1413        TRACE("returning: %p\n", icComp->lpOutput);
1414        return icComp->lpOutput;
1415     }
1416     return NULL;
1417 }
1418
1419 /***********************************************************************
1420  *      ICSeqCompressFrameEnd   [MSVFW32.@]
1421  */
1422 void VFWAPI ICSeqCompressFrameEnd(PCOMPVARS pc)
1423 {
1424     DWORD ret;
1425     TRACE("(%p)\n", pc);
1426     ret = ICSendMessage(pc->hic, ICM_COMPRESS_END, 0, 0);
1427     TRACE(" -- %x\n", ret);
1428     HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1429     HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1430     HeapFree(GetProcessHeap(), 0, pc->lpBitsOut);
1431     HeapFree(GetProcessHeap(), 0, pc->lpState);
1432     pc->lpbiIn = pc->lpBitsPrev = pc->lpBitsOut = pc->lpState = NULL;
1433 }
1434
1435 /***********************************************************************
1436  *      ICSeqCompressFrameStart [MSVFW32.@]
1437  */
1438 BOOL VFWAPI ICSeqCompressFrameStart(PCOMPVARS pc, LPBITMAPINFO lpbiIn)
1439 {
1440     /* I'm ignoring bmiColors as I don't know what to do with it,
1441      * it doesn't appear to be used though
1442      */
1443     DWORD ret;
1444     pc->lpbiIn = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFO));
1445     if (!pc->lpbiIn)
1446         return FALSE;
1447
1448     *pc->lpbiIn = *lpbiIn;
1449     pc->lpBitsPrev = HeapAlloc(GetProcessHeap(), 0, pc->lpbiIn->bmiHeader.biSizeImage);
1450     if (!pc->lpBitsPrev)
1451     {
1452         HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1453         return FALSE;
1454     }
1455
1456     pc->lpState = HeapAlloc(GetProcessHeap(), 0, sizeof(ICCOMPRESS));
1457     if (!pc->lpState)
1458     {
1459        HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1460        HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1461        return FALSE;
1462     }
1463     pc->cbState = sizeof(ICCOMPRESS);
1464
1465     pc->lpBitsOut = HeapAlloc(GetProcessHeap(), 0, pc->lpbiOut->bmiHeader.biSizeImage);
1466     if (!pc->lpBitsOut)
1467     {
1468        HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1469        HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1470        HeapFree(GetProcessHeap(), 0, pc->lpState);
1471        return FALSE;
1472     }
1473     TRACE("Compvars:\n"
1474           "\tpc:\n"
1475           "\tsize: %i\n"
1476           "\tflags: %i\n"
1477           "\thic: %p\n"
1478           "\ttype: %x\n"
1479           "\thandler: %x\n"
1480           "\tin/out: %p/%p\n"
1481           "key/data/quality: %i/%i/%i\n",
1482              pc->cbSize, pc->dwFlags, pc->hic, pc->fccType, pc->fccHandler,
1483              pc->lpbiIn, pc->lpbiOut, pc->lKey, pc->lDataRate, pc->lQ);
1484
1485     ret = ICSendMessage(pc->hic, ICM_COMPRESS_BEGIN, (DWORD_PTR)pc->lpbiIn, (DWORD_PTR)pc->lpbiOut);
1486     TRACE(" -- %x\n", ret);
1487     if (ret == ICERR_OK)
1488     {
1489        ICCOMPRESS* icComp = pc->lpState;
1490        /* Initialise some variables */
1491        pc->lFrame = 0; pc->lKeyCount = 0;
1492
1493        icComp->lpbiOutput = &pc->lpbiOut->bmiHeader;
1494        icComp->lpbiInput = &pc->lpbiIn->bmiHeader;
1495        icComp->lpckid = NULL;
1496        icComp->dwFrameSize = 0;
1497        icComp->dwQuality = pc->lQ;
1498        icComp->lpbiPrev = &pc->lpbiIn->bmiHeader;
1499        return TRUE;
1500     }
1501     HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1502     HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1503     HeapFree(GetProcessHeap(), 0, pc->lpState);
1504     HeapFree(GetProcessHeap(), 0, pc->lpBitsOut);
1505     pc->lpBitsPrev = pc->lpbiIn = pc->lpState = pc->lpBitsOut = NULL;
1506     return FALSE;
1507 }
1508
1509 /***********************************************************************
1510  *      GetFileNamePreview   [MSVFW32.@]
1511  */
1512 static BOOL GetFileNamePreview(LPVOID lpofn,BOOL bSave,BOOL bUnicode)
1513 {
1514   CHAR    szFunctionName[20];
1515   BOOL    (*fnGetFileName)(LPVOID);
1516   HMODULE hComdlg32;
1517   BOOL    ret;
1518
1519   FIXME("(%p,%d,%d), semi-stub!\n",lpofn,bSave,bUnicode);
1520
1521   lstrcpyA(szFunctionName, (bSave ? "GetSaveFileName" : "GetOpenFileName"));
1522   lstrcatA(szFunctionName, (bUnicode ? "W" : "A"));
1523
1524   hComdlg32 = LoadLibraryA("COMDLG32.DLL");
1525   if (hComdlg32 == NULL)
1526     return FALSE;
1527
1528   fnGetFileName = (LPVOID)GetProcAddress(hComdlg32, szFunctionName);
1529   if (fnGetFileName == NULL)
1530     return FALSE;
1531
1532   /* FIXME: need to add OFN_ENABLEHOOK and our own handler */
1533   ret = fnGetFileName(lpofn);
1534
1535   FreeLibrary(hComdlg32);
1536   return ret;
1537 }
1538
1539 /***********************************************************************
1540  *              GetOpenFileNamePreviewA [MSVFW32.@]
1541  */
1542 BOOL WINAPI GetOpenFileNamePreviewA(LPOPENFILENAMEA lpofn)
1543 {
1544   FIXME("(%p), semi-stub!\n", lpofn);
1545
1546   return GetFileNamePreview(lpofn, FALSE, FALSE);
1547 }
1548
1549 /***********************************************************************
1550  *              GetOpenFileNamePreviewW [MSVFW32.@]
1551  */
1552 BOOL WINAPI GetOpenFileNamePreviewW(LPOPENFILENAMEW lpofn)
1553 {
1554   FIXME("(%p), semi-stub!\n", lpofn);
1555
1556   return GetFileNamePreview(lpofn, FALSE, TRUE);
1557 }
1558
1559 /***********************************************************************
1560  *              GetSaveFileNamePreviewA [MSVFW32.@]
1561  */
1562 BOOL WINAPI GetSaveFileNamePreviewA(LPOPENFILENAMEA lpofn)
1563 {
1564   FIXME("(%p), semi-stub!\n", lpofn);
1565
1566   return GetFileNamePreview(lpofn, TRUE, FALSE);
1567 }
1568
1569 /***********************************************************************
1570  *              GetSaveFileNamePreviewW [MSVFW32.@]
1571  */
1572 BOOL WINAPI GetSaveFileNamePreviewW(LPOPENFILENAMEW lpofn)
1573 {
1574   FIXME("(%p), semi-stub!\n", lpofn);
1575
1576   return GetFileNamePreview(lpofn, TRUE, TRUE);
1577 }