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