Change headers to <windows.h> in order to prevent a compilation
[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     
274     TRACE("(%s,%s,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wFlags);
275
276     /* Check if a driver is already registered */
277     for (pdriver = &reg_driver_list; *pdriver; pdriver = &(*pdriver)->next)
278     {
279         if (!compare_fourcc(fccType, (*pdriver)->fccType) &&
280             !compare_fourcc(fccHandler, (*pdriver)->fccHandler))
281             break;
282     }
283     if (!*pdriver)
284         return FALSE;
285
286     /* Remove the driver from the list */
287     *pdriver = (*pdriver)->next;
288     HeapFree(GetProcessHeap(), 0, (*pdriver)->name);
289     HeapFree(GetProcessHeap(), 0, *pdriver);
290     
291     return TRUE;  
292 }
293
294
295 /***********************************************************************
296  *              ICOpen                          [MSVFW32.@]
297  * Opens an installable compressor. Return special handle.
298  */
299 HIC VFWAPI ICOpen(DWORD fccType, DWORD fccHandler, UINT wMode) 
300 {
301     WCHAR               codecname[10];
302     ICOPEN              icopen;
303     HDRVR               hdrv;
304     WINE_HIC*           whic;
305     BOOL                bIs16;
306     static const WCHAR  drv32W[] = {'d','r','i','v','e','r','s','3','2','\0'};
307     reg_driver*         driver;
308
309     TRACE("(%s,%s,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wMode);
310
311     /* Check if there is a registered driver that matches */
312     driver = reg_driver_list;
313     while(driver)
314         if (!compare_fourcc(fccType, driver->fccType) &&
315             !compare_fourcc(fccHandler, driver->fccHandler))
316             break;
317         else
318             driver = driver->next;
319
320     if (driver && driver->proc)
321         /* The driver has been registered at runtime with its driverproc */
322         return MSVIDEO_OpenFunction(fccType, fccHandler, wMode, (DRIVERPROC)driver->proc, (DWORD)NULL);
323   
324     /* Well, lParam2 is in fact a LPVIDEO_OPEN_PARMS, but it has the
325      * same layout as ICOPEN
326      */
327     icopen.dwSize               = sizeof(ICOPEN);
328     icopen.fccType              = fccType;
329     icopen.fccHandler           = fccHandler;
330     icopen.dwVersion            = 0x00001000; /* FIXME */
331     icopen.dwFlags              = wMode;
332     icopen.dwError              = 0;
333     icopen.pV1Reserved          = NULL;
334     icopen.pV2Reserved          = NULL;
335     icopen.dnDevNode            = 0; /* FIXME */
336         
337     if (!driver) {
338         /* The driver is registered in the registry */
339         fourcc_to_string(codecname, fccType);
340         codecname[4] = '.';
341         fourcc_to_string(codecname + 5, fccHandler);
342         codecname[9] = '\0';
343
344         hdrv = OpenDriver(codecname, drv32W, (LPARAM)&icopen);
345         if (!hdrv) 
346         {
347             if (fccType == streamtypeVIDEO) 
348             {
349                 codecname[0] = 'v';
350                 codecname[1] = 'i';
351                 codecname[2] = 'd';
352                 codecname[3] = 'c';
353
354                 fccType = ICTYPE_VIDEO;
355                 hdrv = OpenDriver(codecname, drv32W, (LPARAM)&icopen);
356             }
357             if (!hdrv)
358                 return 0;
359         }
360     } else {
361         /* The driver has been registered at runtime with its name */
362         hdrv = OpenDriver(driver->name, NULL, (LPARAM)&icopen);
363         if (!hdrv) 
364             return 0; 
365     }
366     bIs16 = GetDriverFlags(hdrv) & 0x10000000; /* undocumented flag: WINE_GDF_16BIT */
367
368     if (bIs16 && !pFnCallTo16)
369     {
370         FIXME("Got a 16 bit driver, but no 16 bit support in msvfw\n");
371         return 0;
372     }
373     whic = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_HIC));
374     if (!whic)
375     {
376         CloseDriver(hdrv, 0, 0);
377         return FALSE;
378     }
379     whic->hdrv          = hdrv;
380     /* FIXME: is the signature the real one ? */
381     whic->driverproc    = bIs16 ? (DRIVERPROC)pFnCallTo16 : NULL;
382     whic->driverproc16  = 0;
383     whic->type          = fccType;
384     whic->handler       = fccHandler;
385     while (MSVIDEO_GetHicPtr(HIC_32(IC_HandleRef)) != NULL) IC_HandleRef++;
386     whic->hic           = HIC_32(IC_HandleRef++);
387     whic->next          = MSVIDEO_FirstHic;
388     MSVIDEO_FirstHic = whic;
389
390     TRACE("=> %p\n", whic->hic);
391     return whic->hic;
392 }
393
394 /***********************************************************************
395  *              MSVIDEO_OpenFunction
396  */
397 HIC MSVIDEO_OpenFunction(DWORD fccType, DWORD fccHandler, UINT wMode, 
398                          DRIVERPROC lpfnHandler, DWORD lpfnHandler16) 
399 {
400     ICOPEN      icopen;
401     WINE_HIC*   whic;
402
403     TRACE("(%s,%s,%d,%p,%08lx)\n", 
404           wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wMode, lpfnHandler, lpfnHandler16);
405
406     icopen.dwSize               = sizeof(ICOPEN);
407     icopen.fccType              = fccType;
408     icopen.fccHandler           = fccHandler;
409     icopen.dwVersion            = 0x00001000; /* FIXME */
410     icopen.dwFlags              = wMode;
411     icopen.dwError              = 0;
412     icopen.pV1Reserved          = NULL;
413     icopen.pV2Reserved          = NULL;
414     icopen.dnDevNode            = 0; /* FIXME */
415
416     whic = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_HIC));
417     if (!whic) return 0;
418
419     whic->driverproc   = lpfnHandler;
420     whic->driverproc16 = lpfnHandler16;
421     while (MSVIDEO_GetHicPtr(HIC_32(IC_HandleRef)) != NULL) IC_HandleRef++;
422     whic->hic          = HIC_32(IC_HandleRef++);
423     whic->next         = MSVIDEO_FirstHic;
424     MSVIDEO_FirstHic = whic;
425
426     /* Now try opening/loading the driver. Taken from DRIVER_AddToList */
427     /* What if the function is used more than once? */
428
429     if (MSVIDEO_SendMessage(whic, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) 
430     {
431         WARN("DRV_LOAD failed for hic %p\n", whic->hic);
432         MSVIDEO_FirstHic = whic->next;
433         HeapFree(GetProcessHeap(), 0, whic);
434         return 0;
435     }
436     /* return value is not checked */
437     MSVIDEO_SendMessage(whic, DRV_ENABLE, 0L, 0L);
438
439     whic->driverId = (DWORD)MSVIDEO_SendMessage(whic, DRV_OPEN, 0, (DWORD)&icopen);
440     /* FIXME: What should we put here? */
441     whic->hdrv = NULL;
442     
443     if (whic->driverId == 0) 
444     {
445         WARN("DRV_OPEN failed for hic %p\n", whic->hic);
446         MSVIDEO_FirstHic = whic->next;
447         HeapFree(GetProcessHeap(), 0, whic);
448         return 0;
449     }
450
451     TRACE("=> %p\n", whic->hic);
452     return whic->hic;
453 }
454
455 /***********************************************************************
456  *              ICOpenFunction                  [MSVFW32.@]
457  */
458 HIC VFWAPI ICOpenFunction(DWORD fccType, DWORD fccHandler, UINT wMode, FARPROC lpfnHandler) 
459 {
460     return MSVIDEO_OpenFunction(fccType, fccHandler, wMode, (DRIVERPROC)lpfnHandler, 0);
461 }
462
463 /***********************************************************************
464  *              ICGetInfo                       [MSVFW32.@]
465  */
466 LRESULT VFWAPI ICGetInfo(HIC hic, ICINFO *picinfo, DWORD cb) 
467 {
468     LRESULT     ret;
469     WINE_HIC*   whic = MSVIDEO_GetHicPtr(hic);
470
471     TRACE("(%p,%p,%ld)\n", hic, picinfo, cb);
472
473     whic = MSVIDEO_GetHicPtr(hic);
474     if (!whic) return ICERR_BADHANDLE;
475     if (!picinfo) return MMSYSERR_INVALPARAM;
476
477     /* (WS) The field szDriver should be initialized because the driver 
478      * is not obliged and often will not do it. Some applications, like
479      * VirtualDub, rely on this field and will occasionally crash if it
480      * goes unitialized.
481      */
482     if (cb >= sizeof(ICINFO)) picinfo->szDriver[0] = '\0';
483
484     ret = ICSendMessage(hic, ICM_GETINFO, (DWORD)picinfo, cb);
485
486     /* (WS) When szDriver was not supplied by the driver itself, apparently 
487      * Windows will set its value equal to the driver file name. This can
488      * be obtained from the registry as we do here.
489      */
490     if (cb >= sizeof(ICINFO) && picinfo->szDriver[0] == 0)
491     {
492         ICINFO  ii;
493
494         memset(&ii, 0, sizeof(ii));
495         ii.dwSize = sizeof(ii);
496         ICInfo(picinfo->fccType, picinfo->fccHandler, &ii);
497         lstrcpyW(picinfo->szDriver, ii.szDriver);
498     }
499
500     TRACE("     -> 0x%08lx\n", ret);
501     return ret;
502 }
503
504 typedef struct {
505     DWORD fccType;
506     DWORD fccHandler;
507     LPBITMAPINFOHEADER lpbiIn;
508     LPBITMAPINFOHEADER lpbiOut;
509     WORD wMode;
510     DWORD querymsg;
511     HIC hic;
512 } driver_info_t;
513
514 static HIC try_driver(driver_info_t *info)
515 {
516     HIC   hic;
517
518     if ((hic = ICOpen(info->fccType, info->fccHandler, info->wMode))) 
519     {
520         if (!ICSendMessage(hic, info->querymsg, (DWORD)info->lpbiIn, (DWORD)info->lpbiOut))
521             return hic;
522         ICClose(hic);
523     }
524     return 0;
525 }
526
527 static BOOL ICLocate_enum_handler(const char *drv, int nr, void *param)
528 {
529     driver_info_t *info = (driver_info_t *)param;
530     info->fccHandler = mmioStringToFOURCCA(drv + 5, 0);
531     info->hic = try_driver(info);
532     return info->hic != 0;
533 }
534
535 /***********************************************************************
536  *              ICLocate                        [MSVFW32.@]
537  */
538 HIC VFWAPI ICLocate(DWORD fccType, DWORD fccHandler, LPBITMAPINFOHEADER lpbiIn,
539                     LPBITMAPINFOHEADER lpbiOut, WORD wMode)
540 {
541     driver_info_t info;
542
543     TRACE("(%s,%s,%p,%p,0x%04x)\n", 
544           wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), lpbiIn, lpbiOut, wMode);
545
546     info.fccType = fccType;
547     info.fccHandler = fccHandler;
548     info.lpbiIn = lpbiIn;
549     info.lpbiOut = lpbiOut;
550     info.wMode = wMode;
551
552     switch (wMode) 
553     {
554     case ICMODE_FASTCOMPRESS:
555     case ICMODE_COMPRESS:
556         info.querymsg = ICM_COMPRESS_QUERY;
557         break;
558     case ICMODE_FASTDECOMPRESS:
559     case ICMODE_DECOMPRESS:
560         info.querymsg = ICM_DECOMPRESS_QUERY;
561         break;
562     case ICMODE_DRAW:
563         info.querymsg = ICM_DRAW_QUERY;
564         break;
565     default:
566         WARN("Unknown mode (%d)\n", wMode);
567         return 0;
568     }
569
570     /* Easy case: handler/type match, we just fire a query and return */
571     info.hic = try_driver(&info);
572     /* If it didn't work, try each driver in turn. 32 bit codecs only. */
573     /* FIXME: Move this to an init routine? */
574     if (!info.hic) enum_drivers(fccType, ICLocate_enum_handler, &info);
575
576     if (info.hic) 
577     {
578         TRACE("=> %p\n", info.hic);
579         return info.hic;
580     }
581
582     if (fccType == streamtypeVIDEO) 
583         return ICLocate(ICTYPE_VIDEO, fccHandler, lpbiIn, lpbiOut, wMode);
584     
585     WARN("(%s,%s,%p,%p,0x%04x) not found!\n",
586          wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), lpbiIn, lpbiOut, wMode);
587     return 0;
588 }
589
590 /***********************************************************************
591  *              ICGetDisplayFormat                      [MSVFW32.@]
592  */
593 HIC VFWAPI ICGetDisplayFormat(
594         HIC hic,LPBITMAPINFOHEADER lpbiIn,LPBITMAPINFOHEADER lpbiOut,
595         INT depth,INT dx,INT dy)
596 {
597         HIC     tmphic = hic;
598
599         TRACE("(%p,%p,%p,%d,%d,%d)!\n",hic,lpbiIn,lpbiOut,depth,dx,dy);
600
601         if (!tmphic) {
602                 tmphic=ICLocate(ICTYPE_VIDEO,0,lpbiIn,NULL,ICMODE_DECOMPRESS);
603                 if (!tmphic)
604                         return tmphic;
605         }
606         if ((dy == lpbiIn->biHeight) && (dx == lpbiIn->biWidth))
607                 dy = dx = 0; /* no resize needed */
608
609         /* Can we decompress it ? */
610         if (ICDecompressQuery(tmphic,lpbiIn,NULL) != 0)
611                 goto errout; /* no, sorry */
612
613         ICDecompressGetFormat(tmphic,lpbiIn,lpbiOut);
614
615         if (lpbiOut->biCompression != 0) {
616            FIXME("Ooch, how come decompressor outputs compressed data (%ld)??\n",
617                          lpbiOut->biCompression);
618         }
619         if (lpbiOut->biSize < sizeof(*lpbiOut)) {
620            FIXME("Ooch, size of output BIH is too small (%ld)\n",
621                          lpbiOut->biSize);
622            lpbiOut->biSize = sizeof(*lpbiOut);
623         }
624         if (!depth) {
625                 HDC     hdc;
626
627                 hdc = GetDC(0);
628                 depth = GetDeviceCaps(hdc,BITSPIXEL)*GetDeviceCaps(hdc,PLANES);
629                 ReleaseDC(0,hdc);
630                 if (depth==15)  depth = 16;
631                 if (depth<8)    depth =  8;
632         }
633         if (lpbiIn->biBitCount == 8)
634                 depth = 8;
635
636         TRACE("=> %p\n", tmphic);
637         return tmphic;
638 errout:
639         if (hic!=tmphic)
640                 ICClose(tmphic);
641
642         TRACE("=> 0\n");
643         return 0;
644 }
645
646 /***********************************************************************
647  *              ICCompress                      [MSVFW32.@]
648  */
649 DWORD VFWAPIV
650 ICCompress(
651         HIC hic,DWORD dwFlags,LPBITMAPINFOHEADER lpbiOutput,LPVOID lpData,
652         LPBITMAPINFOHEADER lpbiInput,LPVOID lpBits,LPDWORD lpckid,
653         LPDWORD lpdwFlags,LONG lFrameNum,DWORD dwFrameSize,DWORD dwQuality,
654         LPBITMAPINFOHEADER lpbiPrev,LPVOID lpPrev)
655 {
656         ICCOMPRESS      iccmp;
657
658         TRACE("(%p,%ld,%p,%p,%p,%p,...)\n",hic,dwFlags,lpbiOutput,lpData,lpbiInput,lpBits);
659
660         iccmp.dwFlags           = dwFlags;
661
662         iccmp.lpbiOutput        = lpbiOutput;
663         iccmp.lpOutput          = lpData;
664         iccmp.lpbiInput         = lpbiInput;
665         iccmp.lpInput           = lpBits;
666
667         iccmp.lpckid            = lpckid;
668         iccmp.lpdwFlags         = lpdwFlags;
669         iccmp.lFrameNum         = lFrameNum;
670         iccmp.dwFrameSize       = dwFrameSize;
671         iccmp.dwQuality         = dwQuality;
672         iccmp.lpbiPrev          = lpbiPrev;
673         iccmp.lpPrev            = lpPrev;
674         return ICSendMessage(hic,ICM_COMPRESS,(DWORD)&iccmp,sizeof(iccmp));
675 }
676
677 /***********************************************************************
678  *              ICDecompress                    [MSVFW32.@]
679  */
680 DWORD VFWAPIV  ICDecompress(HIC hic,DWORD dwFlags,LPBITMAPINFOHEADER lpbiFormat,
681                                 LPVOID lpData,LPBITMAPINFOHEADER lpbi,LPVOID lpBits)
682 {
683         ICDECOMPRESS    icd;
684         DWORD ret;
685
686         TRACE("(%p,%ld,%p,%p,%p,%p)\n",hic,dwFlags,lpbiFormat,lpData,lpbi,lpBits);
687
688         TRACE("lpBits[0] == %lx\n",((LPDWORD)lpBits)[0]);
689
690         icd.dwFlags     = dwFlags;
691         icd.lpbiInput   = lpbiFormat;
692         icd.lpInput     = lpData;
693
694         icd.lpbiOutput  = lpbi;
695         icd.lpOutput    = lpBits;
696         icd.ckid        = 0;
697         ret = ICSendMessage(hic,ICM_DECOMPRESS,(DWORD)&icd,sizeof(ICDECOMPRESS));
698
699         TRACE("lpBits[0] == %lx\n",((LPDWORD)lpBits)[0]);
700
701         TRACE("-> %ld\n",ret);
702
703         return ret;
704 }
705
706 /***********************************************************************
707  *              ICCompressorChoose   [MSVFW32.@]
708  */
709 BOOL VFWAPI ICCompressorChoose(HWND hwnd, UINT uiFlags, LPVOID pvIn,
710                                LPVOID lpData, PCOMPVARS pc, LPSTR lpszTitle)
711 {
712   FIXME("(%p,0x%X,%p,%p,%p,%s),stub!\n",hwnd,uiFlags,pvIn,lpData,pc,lpszTitle);
713
714   if (pc == NULL || pc->cbSize != sizeof(COMPVARS))
715     return FALSE;
716
717   if ((pc->dwFlags & ICMF_COMPVARS_VALID) == 0) {
718     pc->dwFlags   = 0;
719     pc->fccType   = pc->fccHandler = 0;
720     pc->hic       = NULL;
721     pc->lpbiOut   = NULL;
722     pc->lpBitsOut = pc->lpBitsPrev = pc->lpState = NULL;
723     pc->lQ        = ICQUALITY_DEFAULT;
724     pc->lKey      = -1;
725     pc->lDataRate = 300; /* kB */
726     pc->lpState   = NULL;
727     pc->cbState   = 0;
728   }
729   if (pc->fccType == 0)
730     pc->fccType = ICTYPE_VIDEO;
731
732   /* FIXME */
733   
734   return FALSE;
735 }
736
737
738 /***********************************************************************
739  *              ICCompressorFree   [MSVFW32.@]
740  */
741 void VFWAPI ICCompressorFree(PCOMPVARS pc)
742 {
743   TRACE("(%p)\n",pc);
744
745   if (pc != NULL && pc->cbSize == sizeof(COMPVARS)) {
746     if (pc->hic != NULL) {
747       ICClose(pc->hic);
748       pc->hic = NULL;
749     }
750     if (pc->lpbiIn != NULL) {
751       HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
752       pc->lpbiIn = NULL;
753     }
754     if (pc->lpBitsOut != NULL) {
755       HeapFree(GetProcessHeap(), 0, pc->lpBitsOut);
756       pc->lpBitsOut = NULL;
757     }
758     if (pc->lpBitsPrev != NULL) {
759       HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
760       pc->lpBitsPrev = NULL;
761     }
762     if (pc->lpState != NULL) {
763       HeapFree(GetProcessHeap(), 0, pc->lpState);
764       pc->lpState = NULL;
765     }
766     pc->dwFlags = 0;
767   }
768 }
769
770
771 /******************************************************************
772  *              MSVIDEO_SendMessage
773  *
774  *
775  */
776 LRESULT MSVIDEO_SendMessage(WINE_HIC* whic, UINT msg, DWORD lParam1, DWORD lParam2)
777 {
778     LRESULT     ret;
779     
780 #define XX(x) case x: TRACE("(%p,"#x",0x%08lx,0x%08lx)\n",whic,lParam1,lParam2); break;
781     
782     switch (msg) {
783         /* DRV_* */
784         XX(DRV_LOAD);
785         XX(DRV_ENABLE);
786         XX(DRV_OPEN);
787         XX(DRV_CLOSE);
788         XX(DRV_DISABLE);
789         XX(DRV_FREE);
790         /* ICM_RESERVED+X */
791         XX(ICM_ABOUT);
792         XX(ICM_CONFIGURE);
793         XX(ICM_GET);
794         XX(ICM_GETINFO);
795         XX(ICM_GETDEFAULTQUALITY);
796         XX(ICM_GETQUALITY);
797         XX(ICM_GETSTATE);
798         XX(ICM_SETQUALITY);
799         XX(ICM_SET);
800         XX(ICM_SETSTATE);
801         /* ICM_USER+X */
802         XX(ICM_COMPRESS_FRAMES_INFO);
803         XX(ICM_COMPRESS_GET_FORMAT);
804         XX(ICM_COMPRESS_GET_SIZE);
805         XX(ICM_COMPRESS_QUERY);
806         XX(ICM_COMPRESS_BEGIN);
807         XX(ICM_COMPRESS);
808         XX(ICM_COMPRESS_END);
809         XX(ICM_DECOMPRESS_GET_FORMAT);
810         XX(ICM_DECOMPRESS_QUERY);
811         XX(ICM_DECOMPRESS_BEGIN);
812         XX(ICM_DECOMPRESS);
813         XX(ICM_DECOMPRESS_END);
814         XX(ICM_DECOMPRESS_SET_PALETTE);
815         XX(ICM_DECOMPRESS_GET_PALETTE);
816         XX(ICM_DRAW_QUERY);
817         XX(ICM_DRAW_BEGIN);
818         XX(ICM_DRAW_GET_PALETTE);
819         XX(ICM_DRAW_START);
820         XX(ICM_DRAW_STOP);
821         XX(ICM_DRAW_END);
822         XX(ICM_DRAW_GETTIME);
823         XX(ICM_DRAW);
824         XX(ICM_DRAW_WINDOW);
825         XX(ICM_DRAW_SETTIME);
826         XX(ICM_DRAW_REALIZE);
827         XX(ICM_DRAW_FLUSH);
828         XX(ICM_DRAW_RENDERBUFFER);
829         XX(ICM_DRAW_START_PLAY);
830         XX(ICM_DRAW_STOP_PLAY);
831         XX(ICM_DRAW_SUGGESTFORMAT);
832         XX(ICM_DRAW_CHANGEPALETTE);
833         XX(ICM_GETBUFFERSWANTED);
834         XX(ICM_GETDEFAULTKEYFRAMERATE);
835         XX(ICM_DECOMPRESSEX_BEGIN);
836         XX(ICM_DECOMPRESSEX_QUERY);
837         XX(ICM_DECOMPRESSEX);
838         XX(ICM_DECOMPRESSEX_END);
839         XX(ICM_SET_STATUS_PROC);
840     default:
841         FIXME("(%p,0x%08lx,0x%08lx,0x%08lx) unknown message\n",whic,(DWORD)msg,lParam1,lParam2);
842     }
843     
844 #undef XX
845     
846     if (whic->driverproc) {
847         /* dwDriverId parameter is the value returned by the DRV_OPEN */
848         ret = whic->driverproc(whic->driverId, whic->hdrv, msg, lParam1, lParam2);
849     } else {
850         ret = SendDriverMessage(whic->hdrv, msg, lParam1, lParam2);
851     }
852
853     TRACE("     -> 0x%08lx\n", ret);
854     return ret;
855 }
856
857 /***********************************************************************
858  *              ICSendMessage                   [MSVFW32.@]
859  */
860 LRESULT VFWAPI ICSendMessage(HIC hic, UINT msg, DWORD lParam1, DWORD lParam2) 
861 {
862     WINE_HIC*   whic = MSVIDEO_GetHicPtr(hic);
863
864     if (!whic) return ICERR_BADHANDLE;
865     return MSVIDEO_SendMessage(whic, msg, lParam1, lParam2);
866 }
867
868 /***********************************************************************
869  *              ICDrawBegin             [MSVFW32.@]
870  */
871 DWORD VFWAPIV ICDrawBegin(
872         HIC                hic,     /* [in] */
873         DWORD              dwFlags, /* [in] flags */
874         HPALETTE           hpal,    /* [in] palette to draw with */
875         HWND               hwnd,    /* [in] window to draw to */
876         HDC                hdc,     /* [in] HDC to draw to */
877         INT                xDst,    /* [in] destination rectangle */
878         INT                yDst,    /* [in] */
879         INT                dxDst,   /* [in] */
880         INT                dyDst,   /* [in] */
881         LPBITMAPINFOHEADER lpbi,    /* [in] format of frame to draw */
882         INT                xSrc,    /* [in] source rectangle */
883         INT                ySrc,    /* [in] */
884         INT                dxSrc,   /* [in] */
885         INT                dySrc,   /* [in] */
886         DWORD              dwRate,  /* [in] frames/second = (dwRate/dwScale) */
887         DWORD              dwScale) /* [in] */
888 {
889
890         ICDRAWBEGIN     icdb;
891
892         TRACE("(%p,%ld,%p,%p,%p,%u,%u,%u,%u,%p,%u,%u,%u,%u,%ld,%ld)\n",
893                   hic, dwFlags, hpal, hwnd, hdc, xDst, yDst, dxDst, dyDst,
894                   lpbi, xSrc, ySrc, dxSrc, dySrc, dwRate, dwScale);
895
896         icdb.dwFlags = dwFlags;
897         icdb.hpal = hpal;
898         icdb.hwnd = hwnd;
899         icdb.hdc = hdc;
900         icdb.xDst = xDst;
901         icdb.yDst = yDst;
902         icdb.dxDst = dxDst;
903         icdb.dyDst = dyDst;
904         icdb.lpbi = lpbi;
905         icdb.xSrc = xSrc;
906         icdb.ySrc = ySrc;
907         icdb.dxSrc = dxSrc;
908         icdb.dySrc = dySrc;
909         icdb.dwRate = dwRate;
910         icdb.dwScale = dwScale;
911         return ICSendMessage(hic,ICM_DRAW_BEGIN,(DWORD)&icdb,sizeof(icdb));
912 }
913
914 /***********************************************************************
915  *              ICDraw                  [MSVFW32.@]
916  */
917 DWORD VFWAPIV ICDraw(HIC hic, DWORD dwFlags, LPVOID lpFormat, LPVOID lpData, DWORD cbData, LONG lTime) {
918         ICDRAW  icd;
919
920         TRACE("(%p,%ld,%p,%p,%ld,%ld)\n",hic,dwFlags,lpFormat,lpData,cbData,lTime);
921
922         icd.dwFlags = dwFlags;
923         icd.lpFormat = lpFormat;
924         icd.lpData = lpData;
925         icd.cbData = cbData;
926         icd.lTime = lTime;
927
928         return ICSendMessage(hic,ICM_DRAW,(DWORD)&icd,sizeof(icd));
929 }
930
931 /***********************************************************************
932  *              ICClose                 [MSVFW32.@]
933  */
934 LRESULT WINAPI ICClose(HIC hic)
935 {
936     WINE_HIC* whic = MSVIDEO_GetHicPtr(hic);
937     WINE_HIC** p;
938
939     TRACE("(%p)\n",hic);
940
941     if (!whic) return ICERR_BADHANDLE;
942
943     if (whic->driverproc) 
944     {
945         MSVIDEO_SendMessage(whic, DRV_CLOSE, 0, 0);
946         MSVIDEO_SendMessage(whic, DRV_DISABLE, 0, 0);
947         MSVIDEO_SendMessage(whic, DRV_FREE, 0, 0);
948     }
949     else
950     {
951         CloseDriver(whic->hdrv, 0, 0);
952     }
953
954     /* remove whic from list */
955     for (p = &MSVIDEO_FirstHic; *p != NULL; p = &((*p)->next))
956     {
957         if ((*p) == whic)
958         {
959             *p = whic->next;
960             break;
961         }
962     }
963
964     HeapFree(GetProcessHeap(), 0, whic);
965     return 0;
966 }
967
968
969
970 /***********************************************************************
971  *              ICImageCompress [MSVFW32.@]
972  */
973 HANDLE VFWAPI ICImageCompress(
974         HIC hic, UINT uiFlags,
975         LPBITMAPINFO lpbiIn, LPVOID lpBits,
976         LPBITMAPINFO lpbiOut, LONG lQuality,
977         LONG* plSize)
978 {
979         FIXME("(%p,%08x,%p,%p,%p,%ld,%p)\n",
980                 hic, uiFlags, lpbiIn, lpBits, lpbiOut, lQuality, plSize);
981
982         return NULL;
983 }
984
985 /***********************************************************************
986  *              ICImageDecompress       [MSVFW32.@]
987  */
988
989 HANDLE VFWAPI ICImageDecompress(
990         HIC hic, UINT uiFlags, LPBITMAPINFO lpbiIn,
991         LPVOID lpBits, LPBITMAPINFO lpbiOut)
992 {
993         HGLOBAL hMem = NULL;
994         BYTE*   pMem = NULL;
995         BOOL    bReleaseIC = FALSE;
996         BYTE*   pHdr = NULL;
997         ULONG   cbHdr = 0;
998         BOOL    bSucceeded = FALSE;
999         BOOL    bInDecompress = FALSE;
1000         DWORD   biSizeImage;
1001
1002         TRACE("(%p,%08x,%p,%p,%p)\n",
1003                 hic, uiFlags, lpbiIn, lpBits, lpbiOut);
1004
1005         if ( hic == NULL )
1006         {
1007                 hic = ICDecompressOpen( ICTYPE_VIDEO, 0, &lpbiIn->bmiHeader, (lpbiOut != NULL) ? &lpbiOut->bmiHeader : NULL );
1008                 if ( hic == NULL )
1009                 {
1010                         WARN("no handler\n" );
1011                         goto err;
1012                 }
1013                 bReleaseIC = TRUE;
1014         }
1015         if ( uiFlags != 0 )
1016         {
1017                 FIXME( "unknown flag %08x\n", uiFlags );
1018                 goto err;
1019         }
1020         if ( lpbiIn == NULL || lpBits == NULL )
1021         {
1022                 WARN("invalid argument\n");
1023                 goto err;
1024         }
1025
1026         if ( lpbiOut != NULL )
1027         {
1028                 if ( lpbiOut->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) )
1029                         goto err;
1030                 cbHdr = sizeof(BITMAPINFOHEADER);
1031                 if ( lpbiOut->bmiHeader.biCompression == 3 )
1032                         cbHdr += sizeof(DWORD)*3;
1033                 else
1034                 if ( lpbiOut->bmiHeader.biBitCount <= 8 )
1035                 {
1036                         if ( lpbiOut->bmiHeader.biClrUsed == 0 )
1037                                 cbHdr += sizeof(RGBQUAD) * (1<<lpbiOut->bmiHeader.biBitCount);
1038                         else
1039                                 cbHdr += sizeof(RGBQUAD) * lpbiOut->bmiHeader.biClrUsed;
1040                 }
1041         }
1042         else
1043         {
1044                 TRACE( "get format\n" );
1045
1046                 cbHdr = ICDecompressGetFormatSize(hic,lpbiIn);
1047                 if ( cbHdr < sizeof(BITMAPINFOHEADER) )
1048                         goto err;
1049                 pHdr = HeapAlloc(GetProcessHeap(),0,cbHdr+sizeof(RGBQUAD)*256);
1050                 if ( pHdr == NULL )
1051                         goto err;
1052                 ZeroMemory( pHdr, cbHdr+sizeof(RGBQUAD)*256 );
1053                 if ( ICDecompressGetFormat( hic, lpbiIn, (BITMAPINFO*)pHdr ) != ICERR_OK )
1054                         goto err;
1055                 lpbiOut = (BITMAPINFO*)pHdr;
1056                 if ( lpbiOut->bmiHeader.biBitCount <= 8 &&
1057                          ICDecompressGetPalette( hic, lpbiIn, lpbiOut ) != ICERR_OK &&
1058                          lpbiIn->bmiHeader.biBitCount == lpbiOut->bmiHeader.biBitCount )
1059                 {
1060                         if ( lpbiIn->bmiHeader.biClrUsed == 0 )
1061                                 memcpy( lpbiOut->bmiColors, lpbiIn->bmiColors, sizeof(RGBQUAD)*(1<<lpbiOut->bmiHeader.biBitCount) );
1062                         else
1063                                 memcpy( lpbiOut->bmiColors, lpbiIn->bmiColors, sizeof(RGBQUAD)*lpbiIn->bmiHeader.biClrUsed );
1064                 }
1065                 if ( lpbiOut->bmiHeader.biBitCount <= 8 &&
1066                          lpbiOut->bmiHeader.biClrUsed == 0 )
1067                         lpbiOut->bmiHeader.biClrUsed = 1<<lpbiOut->bmiHeader.biBitCount;
1068
1069                 lpbiOut->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1070                 cbHdr = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*lpbiOut->bmiHeader.biClrUsed;
1071         }
1072
1073         biSizeImage = lpbiOut->bmiHeader.biSizeImage;
1074         if ( biSizeImage == 0 )
1075                 biSizeImage = ((((lpbiOut->bmiHeader.biWidth * lpbiOut->bmiHeader.biBitCount + 7) >> 3) + 3) & (~3)) * abs(lpbiOut->bmiHeader.biHeight);
1076
1077         TRACE( "call ICDecompressBegin\n" );
1078
1079         if ( ICDecompressBegin( hic, lpbiIn, lpbiOut ) != ICERR_OK )
1080                 goto err;
1081         bInDecompress = TRUE;
1082
1083         TRACE( "cbHdr %ld, biSizeImage %ld\n", cbHdr, biSizeImage );
1084
1085         hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_ZEROINIT, cbHdr + biSizeImage );
1086         if ( hMem == NULL )
1087         {
1088                 WARN( "out of memory\n" );
1089                 goto err;
1090         }
1091         pMem = (BYTE*)GlobalLock( hMem );
1092         if ( pMem == NULL )
1093                 goto err;
1094         memcpy( pMem, lpbiOut, cbHdr );
1095
1096         TRACE( "call ICDecompress\n" );
1097         if ( ICDecompress( hic, 0, &lpbiIn->bmiHeader, lpBits, &lpbiOut->bmiHeader, pMem+cbHdr ) != ICERR_OK )
1098                 goto err;
1099
1100         bSucceeded = TRUE;
1101 err:
1102         if ( bInDecompress )
1103                 ICDecompressEnd( hic );
1104         if ( bReleaseIC )
1105                 ICClose(hic);
1106         HeapFree(GetProcessHeap(),0,pHdr);
1107         if ( pMem != NULL )
1108                 GlobalUnlock( hMem );
1109         if ( !bSucceeded && hMem != NULL )
1110         {
1111                 GlobalFree(hMem); hMem = NULL;
1112         }
1113
1114         return (HANDLE)hMem;
1115 }
1116
1117 /***********************************************************************
1118  *      ICSeqCompressFrame   [MSVFW32.@]
1119  */
1120 LPVOID VFWAPI ICSeqCompressFrame(PCOMPVARS pc, UINT uiFlags, LPVOID lpBits, BOOL *pfKey, LONG *plSize)
1121 {
1122     ICCOMPRESS* icComp = (ICCOMPRESS *)pc->lpState;
1123     DWORD ret;
1124     TRACE("(%p, 0x%08x, %p, %p, %p)\n", pc, uiFlags, lpBits, pfKey, plSize);
1125
1126     if (pc->cbState != sizeof(ICCOMPRESS))
1127     {
1128        ERR("Invalid cbState (%li should be %i)\n", pc->cbState, sizeof(ICCOMPRESS));
1129        return NULL;
1130     }
1131
1132     if (!pc->lKeyCount++)
1133        icComp->dwFlags = ICCOMPRESS_KEYFRAME;
1134     else
1135     {
1136         if (pc->lKey && pc->lKeyCount == (pc->lKey - 1))
1137         /* No key frames if pc->lKey == 0 */
1138            pc->lKeyCount = 0;
1139         icComp->dwFlags = 0;
1140     }
1141
1142     icComp->lpInput = lpBits;
1143     icComp->lFrameNum = pc->lFrame++;
1144     icComp->lpOutput = pc->lpBitsOut;
1145     icComp->lpPrev = pc->lpBitsPrev;
1146     ret = ICSendMessage(pc->hic, ICM_COMPRESS, (DWORD_PTR)icComp, sizeof(icComp));
1147
1148     if (icComp->dwFlags & AVIIF_KEYFRAME)
1149     {
1150        pc->lKeyCount = 1;
1151        *pfKey = TRUE;
1152        TRACE("Key frame\n");
1153     }
1154     else
1155        *pfKey = FALSE;
1156
1157     *plSize = icComp->lpbiOutput->biSizeImage;
1158     TRACE(" -- 0x%08lx\n", ret);
1159     if (ret == ICERR_OK)
1160     {
1161        LPVOID oldprev, oldout;
1162 /* We shift Prev and Out, so we don't have to allocate and release memory */
1163        oldprev = pc->lpBitsPrev;
1164        oldout = pc->lpBitsOut;
1165        pc->lpBitsPrev = oldout;
1166        pc->lpBitsOut = oldprev;
1167
1168        TRACE("returning: %p\n", icComp->lpOutput);
1169        return icComp->lpOutput;
1170     }
1171     return NULL;
1172 }
1173
1174 /***********************************************************************
1175  *      ICSeqCompressFrameEnd   [MSVFW32.@]
1176  */
1177 void VFWAPI ICSeqCompressFrameEnd(PCOMPVARS pc)
1178 {
1179     DWORD ret;
1180     TRACE("(%p)\n", pc);
1181     ret = ICSendMessage(pc->hic, ICM_COMPRESS_END, 0, 0);
1182     TRACE(" -- %lx", ret);
1183     if (pc->lpbiIn)
1184        HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1185     if (pc->lpBitsPrev)
1186        HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1187     if (pc->lpBitsOut)
1188        HeapFree(GetProcessHeap(), 0, pc->lpBitsOut);
1189     if (pc->lpState)
1190        HeapFree(GetProcessHeap(), 0, pc->lpState);
1191     pc->lpbiIn = pc->lpBitsPrev = pc->lpBitsOut = pc->lpState = NULL;
1192 }
1193
1194 /***********************************************************************
1195  *      ICSeqCompressFrameStart [MSVFW32.@]
1196  */
1197 BOOL VFWAPI ICSeqCompressFrameStart(PCOMPVARS pc, LPBITMAPINFO lpbiIn)
1198 {
1199     /* I'm ignoring bmiColors as I don't know what to do with it,
1200      * it doesn't appear to be used though
1201      */
1202     DWORD ret;
1203     pc->lpbiIn = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFO));
1204     if (!pc->lpbiIn)
1205         return FALSE;
1206
1207     memcpy(pc->lpbiIn, lpbiIn, sizeof(BITMAPINFO));
1208     pc->lpBitsPrev = HeapAlloc(GetProcessHeap(), 0, pc->lpbiIn->bmiHeader.biSizeImage);
1209     if (!pc->lpBitsPrev)
1210     {
1211         HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1212         return FALSE;
1213     }
1214
1215     pc->lpState = HeapAlloc(GetProcessHeap(), 0, sizeof(ICCOMPRESS));
1216     if (!pc->lpState)
1217     {
1218        HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1219        HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1220        return FALSE;
1221     }
1222     pc->cbState = sizeof(ICCOMPRESS);
1223
1224     pc->lpBitsOut = HeapAlloc(GetProcessHeap(), 0, pc->lpbiOut->bmiHeader.biSizeImage);
1225     if (!pc->lpBitsOut)
1226     {
1227        HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1228        HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1229        HeapFree(GetProcessHeap(), 0, pc->lpState);
1230        return FALSE;
1231     }
1232     TRACE("Compvars:\n"
1233           "\tpc:\n"
1234           "\tsize: %li\n"
1235           "\tflags: %li\n"
1236           "\thic: %p\n"
1237           "\ttype: %lx\n"
1238           "\thandler: %lx\n"
1239           "\tin/out: %p/%p\n"
1240           "key/data/quality: %li/%li/%li\n",
1241              pc->cbSize, pc->dwFlags, pc->hic, pc->fccType, pc->fccHandler,
1242              pc->lpbiIn, pc->lpbiOut, pc->lKey, pc->lDataRate, pc->lQ);
1243
1244     ret = ICSendMessage(pc->hic, ICM_COMPRESS_BEGIN, (DWORD)pc->lpbiIn, (DWORD)pc->lpbiOut);
1245     TRACE(" -- %lx\n", ret);
1246     if (ret == ICERR_OK)
1247     {
1248        ICCOMPRESS* icComp = (ICCOMPRESS *)pc->lpState;
1249        /* Initialise some variables */
1250        pc->lFrame = 0; pc->lKeyCount = 0;
1251
1252        icComp->lpbiOutput = &pc->lpbiOut->bmiHeader;
1253        icComp->lpbiInput = &pc->lpbiIn->bmiHeader;
1254        icComp->lpckid = NULL;
1255        icComp->dwFrameSize = 0;
1256        icComp->dwQuality = pc->lQ;
1257        icComp->lpbiPrev = &pc->lpbiIn->bmiHeader;
1258        return TRUE;
1259     }
1260     HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1261     HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1262     HeapFree(GetProcessHeap(), 0, pc->lpState);
1263     HeapFree(GetProcessHeap(), 0, pc->lpBitsOut);
1264     pc->lpBitsPrev = pc->lpbiIn = pc->lpState = pc->lpBitsOut = NULL;
1265     return FALSE;
1266 }
1267
1268 /***********************************************************************
1269  *      GetFileNamePreview   [MSVFW32.@]
1270  */
1271 static BOOL GetFileNamePreview(LPVOID lpofn,BOOL bSave,BOOL bUnicode)
1272 {
1273   CHAR    szFunctionName[20];
1274   BOOL    (*fnGetFileName)(LPVOID);
1275   HMODULE hComdlg32;
1276   BOOL    ret;
1277
1278   FIXME("(%p,%d,%d), semi-stub!\n",lpofn,bSave,bUnicode);
1279
1280   lstrcpyA(szFunctionName, (bSave ? "GetSaveFileName" : "GetOpenFileName"));
1281   lstrcatA(szFunctionName, (bUnicode ? "W" : "A"));
1282
1283   hComdlg32 = LoadLibraryA("COMDLG32.DLL");
1284   if (hComdlg32 == NULL)
1285     return FALSE;
1286
1287   fnGetFileName = (LPVOID)GetProcAddress(hComdlg32, szFunctionName);
1288   if (fnGetFileName == NULL)
1289     return FALSE;
1290
1291   /* FIXME: need to add OFN_ENABLEHOOK and our own handler */
1292   ret = fnGetFileName(lpofn);
1293
1294   FreeLibrary(hComdlg32);
1295   return ret;
1296 }
1297
1298 /***********************************************************************
1299  *              GetOpenFileNamePreviewA [MSVFW32.@]
1300  */
1301 BOOL WINAPI GetOpenFileNamePreviewA(LPOPENFILENAMEA lpofn)
1302 {
1303   FIXME("(%p), semi-stub!\n", lpofn);
1304
1305   return GetFileNamePreview(lpofn, FALSE, FALSE);
1306 }
1307
1308 /***********************************************************************
1309  *              GetOpenFileNamePreviewW [MSVFW32.@]
1310  */
1311 BOOL WINAPI GetOpenFileNamePreviewW(LPOPENFILENAMEW lpofn)
1312 {
1313   FIXME("(%p), semi-stub!\n", lpofn);
1314
1315   return GetFileNamePreview(lpofn, FALSE, TRUE);
1316 }
1317
1318 /***********************************************************************
1319  *              GetSaveFileNamePreviewA [MSVFW32.@]
1320  */
1321 BOOL WINAPI GetSaveFileNamePreviewA(LPOPENFILENAMEA lpofn)
1322 {
1323   FIXME("(%p), semi-stub!\n", lpofn);
1324
1325   return GetFileNamePreview(lpofn, TRUE, FALSE);
1326 }
1327
1328 /***********************************************************************
1329  *              GetSaveFileNamePreviewW [MSVFW32.@]
1330  */
1331 BOOL WINAPI GetSaveFileNamePreviewW(LPOPENFILENAMEW lpofn)
1332 {
1333   FIXME("(%p), semi-stub!\n", lpofn);
1334
1335   return GetFileNamePreview(lpofn, TRUE, TRUE);
1336 }