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