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