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