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