kernel32: DETACHED_PROCESS doesn't affect STARTUPINFO console handles.
[wine.git] / dlls / msvfw32 / msvideo_main.c
blobd49ea7422a9e267f06d57eb6477bf75c72bd3ed1
1 /*
2 * Copyright 1998 Marcus Meissner
3 * Copyright 2000 Bradley Baetz
4 * Copyright 2003 Michael Günnewig
5 * Copyright 2005 Dmitry Timoshkov
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.
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.
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
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.
25 * TODO
26 * - no thread safety
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
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"
44 /* Drivers32 settings */
45 #define HKLM_DRIVERS32 "Software\\Microsoft\\Windows NT\\CurrentVersion\\Drivers32"
47 WINE_DEFAULT_DEBUG_CHANNEL(msvideo);
49 /* This one is a macro in order to work for both ASCII and Unicode */
50 #define fourcc_to_string(str, fcc) do { \
51 (str)[0] = LOBYTE(LOWORD(fcc)); \
52 (str)[1] = HIBYTE(LOWORD(fcc)); \
53 (str)[2] = LOBYTE(HIWORD(fcc)); \
54 (str)[3] = HIBYTE(HIWORD(fcc)); \
55 } while(0)
57 static inline const char *wine_dbgstr_fcc( DWORD fcc )
59 char fcc_str[5];
60 fourcc_to_string(fcc_str, fcc);
61 fcc_str[4] = '\0';
62 /* Last byte may be ' ' in some cases like "DIB " */
63 if (isalnum(fcc_str[0]) && isalnum(fcc_str[1]) && isalnum(fcc_str[2])
64 && (isalnum(fcc_str[3]) || isspace(fcc_str[3])))
65 return wine_dbg_sprintf("%s", fcc_str);
66 return wine_dbg_sprintf("0x%08x", fcc);
69 static const char *wine_dbgstr_icerr( int ret )
71 const char *str;
72 if (ret <= ICERR_CUSTOM)
73 return wine_dbg_sprintf("ICERR_CUSTOM (%d)", ret);
74 #define XX(x) case (x): str = #x; break
75 switch (ret)
77 XX(ICERR_OK);
78 XX(ICERR_DONTDRAW);
79 XX(ICERR_NEWPALETTE);
80 XX(ICERR_GOTOKEYFRAME);
81 XX(ICERR_STOPDRAWING);
82 XX(ICERR_UNSUPPORTED);
83 XX(ICERR_BADFORMAT);
84 XX(ICERR_MEMORY);
85 XX(ICERR_INTERNAL);
86 XX(ICERR_BADFLAGS);
87 XX(ICERR_BADPARAM);
88 XX(ICERR_BADSIZE);
89 XX(ICERR_BADHANDLE);
90 XX(ICERR_CANTUPDATE);
91 XX(ICERR_ABORT);
92 XX(ICERR_ERROR);
93 XX(ICERR_BADBITDEPTH);
94 XX(ICERR_BADIMAGESIZE);
95 default: str = wine_dbg_sprintf("UNKNOWN (%d)", ret);
97 #undef XX
98 return str;
101 static WINE_HIC* MSVIDEO_FirstHic /* = NULL */;
103 typedef struct _reg_driver reg_driver;
104 struct _reg_driver
106 DWORD fccType;
107 DWORD fccHandler;
108 DRIVERPROC proc;
109 LPWSTR name;
110 reg_driver* next;
113 static reg_driver* reg_driver_list = NULL;
115 HMODULE MSVFW32_hModule;
117 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
119 TRACE("%p,%x,%p\n", hinst, reason, reserved);
121 switch(reason)
123 case DLL_PROCESS_ATTACH:
124 DisableThreadLibraryCalls(hinst);
125 MSVFW32_hModule = hinst;
126 break;
128 return TRUE;
131 /******************************************************************
132 * MSVIDEO_SendMessage
136 static LRESULT MSVIDEO_SendMessage(WINE_HIC* whic, UINT msg, DWORD_PTR lParam1, DWORD_PTR lParam2)
138 LRESULT ret;
140 #define XX(x) case x: TRACE("(%p,"#x",0x%08lx,0x%08lx)\n",whic,lParam1,lParam2); break
142 switch (msg) {
143 /* DRV_* */
144 XX(DRV_LOAD);
145 XX(DRV_ENABLE);
146 XX(DRV_OPEN);
147 XX(DRV_CLOSE);
148 XX(DRV_DISABLE);
149 XX(DRV_FREE);
150 /* ICM_RESERVED+X */
151 XX(ICM_ABOUT);
152 XX(ICM_CONFIGURE);
153 XX(ICM_GET);
154 XX(ICM_GETINFO);
155 XX(ICM_GETDEFAULTQUALITY);
156 XX(ICM_GETQUALITY);
157 XX(ICM_GETSTATE);
158 XX(ICM_SETQUALITY);
159 XX(ICM_SET);
160 XX(ICM_SETSTATE);
161 /* ICM_USER+X */
162 XX(ICM_COMPRESS_FRAMES_INFO);
163 XX(ICM_COMPRESS_GET_FORMAT);
164 XX(ICM_COMPRESS_GET_SIZE);
165 XX(ICM_COMPRESS_QUERY);
166 XX(ICM_COMPRESS_BEGIN);
167 XX(ICM_COMPRESS);
168 XX(ICM_COMPRESS_END);
169 XX(ICM_DECOMPRESS_GET_FORMAT);
170 XX(ICM_DECOMPRESS_QUERY);
171 XX(ICM_DECOMPRESS_BEGIN);
172 XX(ICM_DECOMPRESS);
173 XX(ICM_DECOMPRESS_END);
174 XX(ICM_DECOMPRESS_SET_PALETTE);
175 XX(ICM_DECOMPRESS_GET_PALETTE);
176 XX(ICM_DRAW_QUERY);
177 XX(ICM_DRAW_BEGIN);
178 XX(ICM_DRAW_GET_PALETTE);
179 XX(ICM_DRAW_START);
180 XX(ICM_DRAW_STOP);
181 XX(ICM_DRAW_END);
182 XX(ICM_DRAW_GETTIME);
183 XX(ICM_DRAW);
184 XX(ICM_DRAW_WINDOW);
185 XX(ICM_DRAW_SETTIME);
186 XX(ICM_DRAW_REALIZE);
187 XX(ICM_DRAW_FLUSH);
188 XX(ICM_DRAW_RENDERBUFFER);
189 XX(ICM_DRAW_START_PLAY);
190 XX(ICM_DRAW_STOP_PLAY);
191 XX(ICM_DRAW_SUGGESTFORMAT);
192 XX(ICM_DRAW_CHANGEPALETTE);
193 XX(ICM_GETBUFFERSWANTED);
194 XX(ICM_GETDEFAULTKEYFRAMERATE);
195 XX(ICM_DECOMPRESSEX_BEGIN);
196 XX(ICM_DECOMPRESSEX_QUERY);
197 XX(ICM_DECOMPRESSEX);
198 XX(ICM_DECOMPRESSEX_END);
199 XX(ICM_SET_STATUS_PROC);
200 default:
201 FIXME("(%p,0x%08x,0x%08lx,0x%08lx) unknown message\n",whic,msg,lParam1,lParam2);
204 #undef XX
206 if (whic->driverproc) {
207 /* dwDriverId parameter is the value returned by the DRV_OPEN */
208 ret = whic->driverproc(whic->driverId, whic->hdrv, msg, lParam1, lParam2);
209 } else {
210 ret = SendDriverMessage(whic->hdrv, msg, lParam1, lParam2);
213 TRACE(" -> %s\n", wine_dbgstr_icerr(ret));
214 return ret;
217 static int compare_fourcc(DWORD fcc1, DWORD fcc2)
219 char fcc_str1[4];
220 char fcc_str2[4];
221 fourcc_to_string(fcc_str1, fcc1);
222 fourcc_to_string(fcc_str2, fcc2);
223 return strncasecmp(fcc_str1, fcc_str2, 4);
226 typedef BOOL (*enum_handler_t)(const char*, unsigned int, void*);
228 static BOOL enum_drivers(DWORD fccType, enum_handler_t handler, void* param)
230 CHAR buf[2048], fccTypeStr[5], *s;
231 DWORD i, cnt = 0, lRet;
232 BOOL result = FALSE;
233 HKEY hKey;
235 fourcc_to_string(fccTypeStr, fccType);
236 fccTypeStr[4] = '.';
238 /* first, go through the registry entries */
239 lRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE, HKLM_DRIVERS32, 0, KEY_QUERY_VALUE, &hKey);
240 if (lRet == ERROR_SUCCESS)
242 DWORD name, data, type;
243 i = 0;
244 for (;;)
246 name = 10;
247 data = sizeof buf - name;
248 lRet = RegEnumValueA(hKey, i++, buf, &name, 0, &type, (LPBYTE)(buf+name), &data);
249 if (lRet == ERROR_NO_MORE_ITEMS) break;
250 if (lRet != ERROR_SUCCESS) continue;
251 if (fccType && (name != 9 || strncasecmp(buf, fccTypeStr, 5))) continue;
252 buf[name] = '=';
253 if ((result = handler(buf, cnt++, param))) break;
255 RegCloseKey( hKey );
257 if (result) return result;
259 /* if that didn't work, go through the values in system.ini */
260 if (GetPrivateProfileSectionA("drivers32", buf, sizeof(buf), "system.ini"))
262 for (s = buf; *s; s += strlen(s) + 1)
264 TRACE("got %s\n", s);
265 if (fccType && (strncasecmp(s, fccTypeStr, 5) || s[9] != '=')) continue;
266 if ((result = handler(s, cnt++, param))) break;
270 return result;
273 /******************************************************************
274 * MSVIDEO_GetHicPtr
278 static WINE_HIC* MSVIDEO_GetHicPtr(HIC hic)
280 WINE_HIC* whic;
282 for (whic = MSVIDEO_FirstHic; whic && whic->hic != hic; whic = whic->next);
283 return whic;
286 /***********************************************************************
287 * VideoForWindowsVersion [MSVFW32.2]
288 * VideoForWindowsVersion [MSVIDEO.2]
289 * Returns the version in major.minor form.
290 * In Windows95 this returns 0x040003b6 (4.950)
292 DWORD WINAPI VideoForWindowsVersion(void)
294 return 0x040003B6; /* 4.950 */
297 static BOOL ICInfo_enum_handler(const char *drv, unsigned int nr, void *param)
299 ICINFO *lpicinfo = param;
300 DWORD fccHandler = mmioStringToFOURCCA(drv + 5, 0);
302 if (lpicinfo->fccHandler != nr && compare_fourcc(lpicinfo->fccHandler, fccHandler))
303 return FALSE;
305 lpicinfo->fccHandler = fccHandler;
306 lpicinfo->dwFlags = 0;
307 lpicinfo->dwVersion = 0;
308 lpicinfo->dwVersionICM = ICVERSION;
309 lpicinfo->szName[0] = 0;
310 lpicinfo->szDescription[0] = 0;
311 MultiByteToWideChar(CP_ACP, 0, drv + 10, -1, lpicinfo->szDriver,
312 sizeof(lpicinfo->szDriver)/sizeof(WCHAR));
314 return TRUE;
317 /***********************************************************************
318 * ICInfo [MSVFW32.@]
319 * Get information about an installable compressor. Return TRUE if there
320 * is one.
322 * PARAMS
323 * fccType [I] type of compressor (e.g. 'vidc')
324 * fccHandler [I] real fcc for handler or <n>th compressor
325 * lpicinfo [O] information about compressor
327 BOOL VFWAPI ICInfo( DWORD fccType, DWORD fccHandler, ICINFO *lpicinfo)
329 TRACE("(%s,%s,%p)\n",
330 wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), lpicinfo);
332 lpicinfo->fccType = fccType;
333 lpicinfo->fccHandler = fccHandler;
334 return enum_drivers(fccType, ICInfo_enum_handler, lpicinfo);
337 static DWORD IC_HandleRef = 1;
339 /***********************************************************************
340 * ICInstall [MSVFW32.@]
342 BOOL VFWAPI ICInstall(DWORD fccType, DWORD fccHandler, LPARAM lParam, LPSTR szDesc, UINT wFlags)
344 reg_driver* driver;
345 unsigned len;
347 TRACE("(%s,%s,%p,%p,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), (void*)lParam, szDesc, wFlags);
349 /* Check if a driver is already registered */
350 for (driver = reg_driver_list; driver; driver = driver->next)
352 if (!compare_fourcc(fccType, driver->fccType) &&
353 !compare_fourcc(fccHandler, driver->fccHandler))
354 break;
356 if (driver) return FALSE;
358 /* Register the driver */
359 driver = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(reg_driver));
360 if (!driver) goto oom;
361 driver->fccType = fccType;
362 driver->fccHandler = fccHandler;
364 switch(wFlags)
366 case ICINSTALL_FUNCTION:
367 driver->proc = (DRIVERPROC)lParam;
368 driver->name = NULL;
369 break;
370 case ICINSTALL_DRIVER:
371 driver->proc = NULL;
372 len = MultiByteToWideChar(CP_ACP, 0, (char*)lParam, -1, NULL, 0);
373 driver->name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
374 if (!driver->name) goto oom;
375 MultiByteToWideChar(CP_ACP, 0, (char*)lParam, -1, driver->name, len);
376 break;
377 default:
378 ERR("Invalid flags!\n");
379 HeapFree(GetProcessHeap(), 0, driver);
380 return FALSE;
383 /* Insert our driver in the list*/
384 driver->next = reg_driver_list;
385 reg_driver_list = driver;
387 return TRUE;
388 oom:
389 HeapFree(GetProcessHeap(), 0, driver);
390 return FALSE;
393 /***********************************************************************
394 * ICRemove [MSVFW32.@]
396 BOOL VFWAPI ICRemove(DWORD fccType, DWORD fccHandler, UINT wFlags)
398 reg_driver** pdriver;
399 reg_driver* drv;
401 TRACE("(%s,%s,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wFlags);
403 /* Check if a driver is already registered */
404 for (pdriver = &reg_driver_list; *pdriver; pdriver = &(*pdriver)->next)
406 if (!compare_fourcc(fccType, (*pdriver)->fccType) &&
407 !compare_fourcc(fccHandler, (*pdriver)->fccHandler))
408 break;
410 if (!*pdriver)
411 return FALSE;
413 /* Remove the driver from the list */
414 drv = *pdriver;
415 *pdriver = (*pdriver)->next;
416 HeapFree(GetProcessHeap(), 0, drv->name);
417 HeapFree(GetProcessHeap(), 0, drv);
419 return TRUE;
423 /***********************************************************************
424 * ICOpen [MSVFW32.@]
425 * Opens an installable compressor. Return special handle.
427 HIC VFWAPI ICOpen(DWORD fccType, DWORD fccHandler, UINT wMode)
429 WCHAR codecname[10];
430 ICOPEN icopen;
431 HDRVR hdrv;
432 WINE_HIC* whic;
433 static const WCHAR drv32W[] = {'d','r','i','v','e','r','s','3','2','\0'};
434 reg_driver* driver;
436 TRACE("(%s,%s,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wMode);
438 if (!fccHandler) /* No specific handler, return the first valid for wMode */
440 HIC local;
441 ICINFO info;
442 DWORD loop = 0;
443 info.dwSize = sizeof(info);
444 while(ICInfo(fccType, loop++, &info))
446 /* Ensure fccHandler is not 0x0 because we will recurse on ICOpen */
447 if(!info.fccHandler)
448 continue;
449 local = ICOpen(fccType, info.fccHandler, wMode);
450 if (local != 0)
452 TRACE("Returning %s as default handler for %s\n",
453 wine_dbgstr_fcc(info.fccHandler), wine_dbgstr_fcc(fccType));
454 return local;
459 /* Check if there is a registered driver that matches */
460 driver = reg_driver_list;
461 while(driver)
462 if (!compare_fourcc(fccType, driver->fccType) &&
463 !compare_fourcc(fccHandler, driver->fccHandler)) {
464 fccType = driver->fccType;
465 fccHandler = driver->fccHandler;
466 break;
467 } else
468 driver = driver->next;
470 if (driver && driver->proc)
471 /* The driver has been registered at runtime with its driverproc */
472 return ICOpenFunction(fccType, fccHandler, wMode, driver->proc);
474 /* Well, lParam2 is in fact a LPVIDEO_OPEN_PARMS, but it has the
475 * same layout as ICOPEN
477 icopen.dwSize = sizeof(ICOPEN);
478 icopen.fccType = fccType;
479 icopen.fccHandler = fccHandler;
480 icopen.dwVersion = 0x00001000; /* FIXME */
481 icopen.dwFlags = wMode;
482 icopen.dwError = 0;
483 icopen.pV1Reserved = NULL;
484 icopen.pV2Reserved = NULL;
485 icopen.dnDevNode = 0; /* FIXME */
487 if (!driver) {
488 /* normalize to lower case as in 'vidc' */
489 ((char*)&fccType)[0] = tolower(((char*)&fccType)[0]);
490 ((char*)&fccType)[1] = tolower(((char*)&fccType)[1]);
491 ((char*)&fccType)[2] = tolower(((char*)&fccType)[2]);
492 ((char*)&fccType)[3] = tolower(((char*)&fccType)[3]);
493 icopen.fccType = fccType;
494 /* Seek the driver in the registry */
495 fourcc_to_string(codecname, fccType);
496 codecname[4] = '.';
497 fourcc_to_string(codecname + 5, fccHandler);
498 codecname[9] = '\0';
500 hdrv = OpenDriver(codecname, drv32W, (LPARAM)&icopen);
501 if (!hdrv)
502 return 0;
503 } else {
504 /* The driver has been registered at runtime with its name */
505 hdrv = OpenDriver(driver->name, NULL, (LPARAM)&icopen);
506 if (!hdrv)
507 return 0;
510 whic = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_HIC));
511 if (!whic)
513 CloseDriver(hdrv, 0, 0);
514 return FALSE;
516 whic->hdrv = hdrv;
517 whic->driverproc = NULL;
518 whic->type = fccType;
519 whic->handler = fccHandler;
520 while (MSVIDEO_GetHicPtr((HIC)(ULONG_PTR)IC_HandleRef) != NULL) IC_HandleRef++;
521 whic->hic = (HIC)(ULONG_PTR)IC_HandleRef++;
522 whic->next = MSVIDEO_FirstHic;
523 MSVIDEO_FirstHic = whic;
525 TRACE("=> %p\n", whic->hic);
526 return whic->hic;
529 /***********************************************************************
530 * ICOpenFunction [MSVFW32.@]
532 HIC VFWAPI ICOpenFunction(DWORD fccType, DWORD fccHandler, UINT wMode, DRIVERPROC lpfnHandler)
534 ICOPEN icopen;
535 WINE_HIC* whic;
537 TRACE("(%s,%s,%d,%p)\n",
538 wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wMode, lpfnHandler);
540 icopen.dwSize = sizeof(ICOPEN);
541 icopen.fccType = fccType;
542 icopen.fccHandler = fccHandler;
543 icopen.dwVersion = ICVERSION;
544 icopen.dwFlags = wMode;
545 icopen.dwError = 0;
546 icopen.pV1Reserved = NULL;
547 icopen.pV2Reserved = NULL;
548 icopen.dnDevNode = 0; /* FIXME */
550 whic = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_HIC));
551 if (!whic) return 0;
553 whic->driverproc = lpfnHandler;
554 while (MSVIDEO_GetHicPtr((HIC)(ULONG_PTR)IC_HandleRef) != NULL) IC_HandleRef++;
555 whic->hic = (HIC)(ULONG_PTR)IC_HandleRef++;
556 whic->next = MSVIDEO_FirstHic;
557 MSVIDEO_FirstHic = whic;
559 /* Now try opening/loading the driver. Taken from DRIVER_AddToList */
560 /* What if the function is used more than once? */
562 if (MSVIDEO_SendMessage(whic, DRV_LOAD, 0L, 0L) != DRV_SUCCESS)
564 WARN("DRV_LOAD failed for hic %p\n", whic->hic);
565 MSVIDEO_FirstHic = whic->next;
566 HeapFree(GetProcessHeap(), 0, whic);
567 return 0;
569 /* return value is not checked */
570 MSVIDEO_SendMessage(whic, DRV_ENABLE, 0L, 0L);
572 whic->driverId = (DWORD)MSVIDEO_SendMessage(whic, DRV_OPEN, 0, (DWORD_PTR)&icopen);
573 /* FIXME: What should we put here? */
574 whic->hdrv = NULL;
576 if (whic->driverId == 0)
578 WARN("DRV_OPEN failed for hic %p\n", whic->hic);
579 MSVIDEO_FirstHic = whic->next;
580 HeapFree(GetProcessHeap(), 0, whic);
581 return 0;
584 TRACE("=> %p\n", whic->hic);
585 return whic->hic;
588 /***********************************************************************
589 * ICGetInfo [MSVFW32.@]
591 LRESULT VFWAPI ICGetInfo(HIC hic, ICINFO *picinfo, DWORD cb)
593 LRESULT ret;
594 WINE_HIC* whic = MSVIDEO_GetHicPtr(hic);
596 TRACE("(%p,%p,%d)\n", hic, picinfo, cb);
598 if (!whic) return ICERR_BADHANDLE;
599 if (!picinfo) return MMSYSERR_INVALPARAM;
601 /* (WS) The field szDriver should be initialized because the driver
602 * is not obliged and often will not do it. Some applications, like
603 * VirtualDub, rely on this field and will occasionally crash if it
604 * goes uninitialized.
606 if (cb >= sizeof(ICINFO)) picinfo->szDriver[0] = '\0';
608 ret = ICSendMessage(hic, ICM_GETINFO, (DWORD_PTR)picinfo, cb);
610 /* (WS) When szDriver was not supplied by the driver itself, apparently
611 * Windows will set its value equal to the driver file name. This can
612 * be obtained from the registry as we do here.
614 if (cb >= sizeof(ICINFO) && picinfo->szDriver[0] == 0)
616 ICINFO ii;
618 memset(&ii, 0, sizeof(ii));
619 ii.dwSize = sizeof(ii);
620 ICInfo(picinfo->fccType, picinfo->fccHandler, &ii);
621 lstrcpyW(picinfo->szDriver, ii.szDriver);
624 return ret;
627 typedef struct {
628 DWORD fccType;
629 DWORD fccHandler;
630 LPBITMAPINFOHEADER lpbiIn;
631 LPBITMAPINFOHEADER lpbiOut;
632 WORD wMode;
633 DWORD querymsg;
634 HIC hic;
635 } driver_info_t;
637 static HIC try_driver(driver_info_t *info)
639 HIC hic;
641 if ((hic = ICOpen(info->fccType, info->fccHandler, info->wMode)))
643 if (!ICSendMessage(hic, info->querymsg, (DWORD_PTR)info->lpbiIn, (DWORD_PTR)info->lpbiOut))
644 return hic;
645 ICClose(hic);
647 return 0;
650 static BOOL ICLocate_enum_handler(const char *drv, unsigned int nr, void *param)
652 driver_info_t *info = param;
653 info->fccHandler = mmioStringToFOURCCA(drv + 5, 0);
654 info->hic = try_driver(info);
655 return info->hic != 0;
658 /***********************************************************************
659 * ICLocate [MSVFW32.@]
661 HIC VFWAPI ICLocate(DWORD fccType, DWORD fccHandler, LPBITMAPINFOHEADER lpbiIn,
662 LPBITMAPINFOHEADER lpbiOut, WORD wMode)
664 driver_info_t info;
666 TRACE("(%s,%s,%p,%p,0x%04x)\n",
667 wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), lpbiIn, lpbiOut, wMode);
669 info.fccType = fccType;
670 info.fccHandler = fccHandler;
671 info.lpbiIn = lpbiIn;
672 info.lpbiOut = lpbiOut;
673 info.wMode = wMode;
675 switch (wMode)
677 case ICMODE_FASTCOMPRESS:
678 case ICMODE_COMPRESS:
679 info.querymsg = ICM_COMPRESS_QUERY;
680 break;
681 case ICMODE_FASTDECOMPRESS:
682 case ICMODE_DECOMPRESS:
683 info.querymsg = ICM_DECOMPRESS_QUERY;
684 break;
685 case ICMODE_DRAW:
686 info.querymsg = ICM_DRAW_QUERY;
687 break;
688 default:
689 WARN("Unknown mode (%d)\n", wMode);
690 return 0;
693 /* Easy case: handler/type match, we just fire a query and return */
694 info.hic = try_driver(&info);
695 /* If it didn't work, try each driver in turn. 32 bit codecs only. */
696 /* FIXME: Move this to an init routine? */
697 if (!info.hic) enum_drivers(fccType, ICLocate_enum_handler, &info);
699 if (info.hic)
701 TRACE("=> %p\n", info.hic);
702 return info.hic;
705 if (fccType == streamtypeVIDEO)
706 return ICLocate(ICTYPE_VIDEO, fccHandler, lpbiIn, lpbiOut, wMode);
708 ERR("Required media codec '%s %s' not found!\n",
709 wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler));
710 return 0;
713 /***********************************************************************
714 * ICGetDisplayFormat [MSVFW32.@]
716 HIC VFWAPI ICGetDisplayFormat(
717 HIC hic,LPBITMAPINFOHEADER lpbiIn,LPBITMAPINFOHEADER lpbiOut,
718 INT depth,INT dx,INT dy)
720 HIC tmphic = hic;
722 TRACE("(%p,%p,%p,%d,%d,%d)!\n",hic,lpbiIn,lpbiOut,depth,dx,dy);
724 if (!tmphic) {
725 tmphic=ICLocate(ICTYPE_VIDEO,0,lpbiIn,NULL,ICMODE_DECOMPRESS);
726 if (!tmphic)
727 return tmphic;
729 if ((dy == lpbiIn->biHeight) && (dx == lpbiIn->biWidth))
730 dy = dx = 0; /* no resize needed */
732 /* Can we decompress it ? */
733 if (ICDecompressQuery(tmphic,lpbiIn,NULL) != 0)
734 goto errout; /* no, sorry */
736 ICSendMessage(tmphic, ICM_DECOMPRESS_GET_FORMAT, (DWORD_PTR)lpbiIn, (DWORD_PTR)lpbiOut);
738 if (lpbiOut->biCompression != 0) {
739 FIXME("Ooch, how come decompressor outputs compressed data (%d)??\n",
740 lpbiOut->biCompression);
742 if (lpbiOut->biSize < sizeof(*lpbiOut)) {
743 FIXME("Ooch, size of output BIH is too small (%d)\n",
744 lpbiOut->biSize);
745 lpbiOut->biSize = sizeof(*lpbiOut);
747 if (!depth) {
748 HDC hdc;
750 hdc = GetDC(0);
751 depth = GetDeviceCaps(hdc,BITSPIXEL)*GetDeviceCaps(hdc,PLANES);
752 ReleaseDC(0,hdc);
753 if (depth==15) depth = 16;
754 if (depth<8) depth = 8;
756 if (lpbiIn->biBitCount == 8)
757 depth = 8;
759 TRACE("=> %p\n", tmphic);
760 return tmphic;
761 errout:
762 if (hic!=tmphic)
763 ICClose(tmphic);
765 TRACE("=> 0\n");
766 return 0;
769 /***********************************************************************
770 * ICCompress [MSVFW32.@]
772 DWORD VFWAPIV
773 ICCompress(
774 HIC hic,DWORD dwFlags,LPBITMAPINFOHEADER lpbiOutput,LPVOID lpData,
775 LPBITMAPINFOHEADER lpbiInput,LPVOID lpBits,LPDWORD lpckid,
776 LPDWORD lpdwFlags,LONG lFrameNum,DWORD dwFrameSize,DWORD dwQuality,
777 LPBITMAPINFOHEADER lpbiPrev,LPVOID lpPrev)
779 ICCOMPRESS iccmp;
781 TRACE("(%p,%d,%p,%p,%p,%p,...)\n",hic,dwFlags,lpbiOutput,lpData,lpbiInput,lpBits);
783 iccmp.dwFlags = dwFlags;
785 iccmp.lpbiOutput = lpbiOutput;
786 iccmp.lpOutput = lpData;
787 iccmp.lpbiInput = lpbiInput;
788 iccmp.lpInput = lpBits;
790 iccmp.lpckid = lpckid;
791 iccmp.lpdwFlags = lpdwFlags;
792 iccmp.lFrameNum = lFrameNum;
793 iccmp.dwFrameSize = dwFrameSize;
794 iccmp.dwQuality = dwQuality;
795 iccmp.lpbiPrev = lpbiPrev;
796 iccmp.lpPrev = lpPrev;
797 return ICSendMessage(hic,ICM_COMPRESS,(DWORD_PTR)&iccmp,sizeof(iccmp));
800 /***********************************************************************
801 * ICDecompress [MSVFW32.@]
803 DWORD VFWAPIV ICDecompress(HIC hic,DWORD dwFlags,LPBITMAPINFOHEADER lpbiFormat,
804 LPVOID lpData,LPBITMAPINFOHEADER lpbi,LPVOID lpBits)
806 ICDECOMPRESS icd;
807 DWORD ret;
809 TRACE("(%p,%d,%p,%p,%p,%p)\n",hic,dwFlags,lpbiFormat,lpData,lpbi,lpBits);
811 icd.dwFlags = dwFlags;
812 icd.lpbiInput = lpbiFormat;
813 icd.lpInput = lpData;
815 icd.lpbiOutput = lpbi;
816 icd.lpOutput = lpBits;
817 icd.ckid = 0;
818 ret = ICSendMessage(hic,ICM_DECOMPRESS,(DWORD_PTR)&icd,sizeof(ICDECOMPRESS));
820 return ret;
824 struct choose_compressor
826 UINT flags;
827 LPCSTR title;
828 COMPVARS cv;
831 struct codec_info
833 HIC hic;
834 ICINFO icinfo;
837 static BOOL enum_compressors(HWND list, COMPVARS *pcv, BOOL enum_all)
839 UINT id, total = 0;
840 ICINFO icinfo;
842 id = 0;
844 while (ICInfo(pcv->fccType, id, &icinfo))
846 struct codec_info *ic;
847 DWORD idx;
848 HIC hic;
850 id++;
852 hic = ICOpen(icinfo.fccType, icinfo.fccHandler, ICMODE_COMPRESS);
854 if (hic)
856 /* for unknown reason fccHandler reported by the driver
857 * doesn't always work, use the one returned by ICInfo instead.
859 DWORD fccHandler = icinfo.fccHandler;
861 if (!enum_all && pcv->lpbiIn)
863 if (ICCompressQuery(hic, pcv->lpbiIn, NULL) != ICERR_OK)
865 TRACE("fccHandler %s doesn't support input DIB format %d\n",
866 wine_dbgstr_fcc(icinfo.fccHandler), pcv->lpbiIn->bmiHeader.biCompression);
867 ICClose(hic);
868 continue;
872 ICGetInfo(hic, &icinfo, sizeof(icinfo));
873 icinfo.fccHandler = fccHandler;
875 idx = SendMessageW(list, CB_ADDSTRING, 0, (LPARAM)icinfo.szDescription);
877 ic = HeapAlloc(GetProcessHeap(), 0, sizeof(struct codec_info));
878 ic->icinfo = icinfo;
879 ic->hic = hic;
880 SendMessageW(list, CB_SETITEMDATA, idx, (LPARAM)ic);
882 total++;
885 return total != 0;
888 static INT_PTR CALLBACK icm_choose_compressor_dlgproc(HWND hdlg, UINT msg, WPARAM wparam, LPARAM lparam)
890 switch (msg)
892 case WM_INITDIALOG:
894 struct codec_info *ic;
895 WCHAR buf[128];
896 struct choose_compressor *choose_comp = (struct choose_compressor *)lparam;
898 SetWindowLongPtrW(hdlg, DWLP_USER, lparam);
900 /* FIXME */
901 choose_comp->flags &= ~(ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME);
903 if (choose_comp->title)
904 SetWindowTextA(hdlg, choose_comp->title);
906 if (!(choose_comp->flags & ICMF_CHOOSE_DATARATE))
908 ShowWindow(GetDlgItem(hdlg, IDC_DATARATE_CHECKBOX), SW_HIDE);
909 ShowWindow(GetDlgItem(hdlg, IDC_DATARATE), SW_HIDE);
910 ShowWindow(GetDlgItem(hdlg, IDC_DATARATE_KB), SW_HIDE);
913 if (!(choose_comp->flags & ICMF_CHOOSE_KEYFRAME))
915 ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME_CHECKBOX), SW_HIDE);
916 ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME), SW_HIDE);
917 ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME_FRAMES), SW_HIDE);
920 /* FIXME */
921 EnableWindow(GetDlgItem(hdlg, IDC_QUALITY_SCROLL), FALSE);
922 EnableWindow(GetDlgItem(hdlg, IDC_QUALITY_TXT), FALSE);
924 /*if (!(choose_comp->flags & ICMF_CHOOSE_PREVIEW))
925 ShowWindow(GetDlgItem(hdlg, IDC_PREVIEW), SW_HIDE);*/
927 LoadStringW(MSVFW32_hModule, IDS_FULLFRAMES, buf, 128);
928 SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_ADDSTRING, 0, (LPARAM)buf);
930 ic = HeapAlloc(GetProcessHeap(), 0, sizeof(struct codec_info));
931 ic->icinfo.fccType = streamtypeVIDEO;
932 ic->icinfo.fccHandler = comptypeDIB;
933 ic->hic = 0;
934 SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_SETITEMDATA, 0, (LPARAM)ic);
936 enum_compressors(GetDlgItem(hdlg, IDC_COMP_LIST), &choose_comp->cv, choose_comp->flags & ICMF_CHOOSE_ALLCOMPRESSORS);
938 SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_SETCURSEL, 0, 0);
939 SetFocus(GetDlgItem(hdlg, IDC_COMP_LIST));
941 SetWindowLongPtrW(hdlg, DWLP_USER, (ULONG_PTR)choose_comp);
942 break;
945 case WM_COMMAND:
946 switch (LOWORD(wparam))
948 case IDC_COMP_LIST:
950 INT cur_sel;
951 struct codec_info *ic;
952 BOOL can_configure = FALSE, can_about = FALSE;
953 struct choose_compressor *choose_comp;
955 if (HIWORD(wparam) != CBN_SELCHANGE && HIWORD(wparam) != CBN_SETFOCUS)
956 break;
958 choose_comp = (struct choose_compressor *)GetWindowLongPtrW(hdlg, DWLP_USER);
960 cur_sel = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
962 ic = (struct codec_info *)SendMessageW((HWND)lparam, CB_GETITEMDATA, cur_sel, 0);
963 if (ic && ic->hic)
965 if (ICQueryConfigure(ic->hic) == DRVCNF_OK)
966 can_configure = TRUE;
967 if (ICQueryAbout(ic->hic) == DRVCNF_OK)
968 can_about = TRUE;
970 EnableWindow(GetDlgItem(hdlg, IDC_CONFIGURE), can_configure);
971 EnableWindow(GetDlgItem(hdlg, IDC_ABOUT), can_about);
973 if (choose_comp->flags & ICMF_CHOOSE_DATARATE)
975 /* FIXME */
977 if (choose_comp->flags & ICMF_CHOOSE_KEYFRAME)
979 /* FIXME */
982 break;
985 case IDC_CONFIGURE:
986 case IDC_ABOUT:
988 HWND list = GetDlgItem(hdlg, IDC_COMP_LIST);
989 INT cur_sel;
990 struct codec_info *ic;
992 if (HIWORD(wparam) != BN_CLICKED)
993 break;
995 cur_sel = SendMessageW(list, CB_GETCURSEL, 0, 0);
997 ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, cur_sel, 0);
998 if (ic && ic->hic)
1000 if (LOWORD(wparam) == IDC_CONFIGURE)
1001 ICConfigure(ic->hic, hdlg);
1002 else
1003 ICAbout(ic->hic, hdlg);
1006 break;
1009 case IDOK:
1011 HWND list = GetDlgItem(hdlg, IDC_COMP_LIST);
1012 INT cur_sel;
1013 struct codec_info *ic;
1015 if (HIWORD(wparam) != BN_CLICKED)
1016 break;
1018 cur_sel = SendMessageW(list, CB_GETCURSEL, 0, 0);
1019 ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, cur_sel, 0);
1020 if (ic)
1022 struct choose_compressor *choose_comp = (struct choose_compressor *)GetWindowLongPtrW(hdlg, DWLP_USER);
1024 choose_comp->cv.hic = ic->hic;
1025 choose_comp->cv.fccType = ic->icinfo.fccType;
1026 choose_comp->cv.fccHandler = ic->icinfo.fccHandler;
1027 /* FIXME: fill everything else */
1029 /* prevent closing the codec handle below */
1030 ic->hic = 0;
1033 /* fall through */
1034 case IDCANCEL:
1036 HWND list = GetDlgItem(hdlg, IDC_COMP_LIST);
1037 INT idx = 0;
1039 if (HIWORD(wparam) != BN_CLICKED)
1040 break;
1042 while (1)
1044 struct codec_info *ic;
1046 ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, idx++, 0);
1048 if (!ic || (LONG_PTR)ic == CB_ERR) break;
1050 if (ic->hic) ICClose(ic->hic);
1051 HeapFree(GetProcessHeap(), 0, ic);
1054 EndDialog(hdlg, LOWORD(wparam) == IDOK);
1055 break;
1058 default:
1059 break;
1061 break;
1063 default:
1064 break;
1067 return FALSE;
1070 /***********************************************************************
1071 * ICCompressorChoose [MSVFW32.@]
1073 BOOL VFWAPI ICCompressorChoose(HWND hwnd, UINT uiFlags, LPVOID pvIn,
1074 LPVOID lpData, PCOMPVARS pc, LPSTR lpszTitle)
1076 struct choose_compressor choose_comp;
1077 BOOL ret;
1079 TRACE("(%p,%08x,%p,%p,%p,%s)\n", hwnd, uiFlags, pvIn, lpData, pc, lpszTitle);
1081 if (!pc || pc->cbSize != sizeof(COMPVARS))
1082 return FALSE;
1084 if (!(pc->dwFlags & ICMF_COMPVARS_VALID))
1086 pc->dwFlags = 0;
1087 pc->fccType = pc->fccHandler = 0;
1088 pc->hic = NULL;
1089 pc->lpbiIn = NULL;
1090 pc->lpbiOut = NULL;
1091 pc->lpBitsOut = pc->lpBitsPrev = pc->lpState = NULL;
1092 pc->lQ = ICQUALITY_DEFAULT;
1093 pc->lKey = -1;
1094 pc->lDataRate = 300; /* kB */
1095 pc->lpState = NULL;
1096 pc->cbState = 0;
1098 if (pc->fccType == 0)
1099 pc->fccType = ICTYPE_VIDEO;
1101 choose_comp.cv = *pc;
1102 choose_comp.flags = uiFlags;
1103 choose_comp.title = lpszTitle;
1105 ret = DialogBoxParamW(MSVFW32_hModule, MAKEINTRESOURCEW(ICM_CHOOSE_COMPRESSOR), hwnd,
1106 icm_choose_compressor_dlgproc, (LPARAM)&choose_comp);
1108 if (ret)
1110 *pc = choose_comp.cv;
1111 pc->dwFlags |= ICMF_COMPVARS_VALID;
1114 return ret;
1118 /***********************************************************************
1119 * ICCompressorFree [MSVFW32.@]
1121 void VFWAPI ICCompressorFree(PCOMPVARS pc)
1123 TRACE("(%p)\n",pc);
1125 if (pc != NULL && pc->cbSize == sizeof(COMPVARS)) {
1126 if (pc->hic != NULL) {
1127 ICClose(pc->hic);
1128 pc->hic = NULL;
1130 HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1131 pc->lpbiIn = NULL;
1132 HeapFree(GetProcessHeap(), 0, pc->lpBitsOut);
1133 pc->lpBitsOut = NULL;
1134 HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1135 pc->lpBitsPrev = NULL;
1136 HeapFree(GetProcessHeap(), 0, pc->lpState);
1137 pc->lpState = NULL;
1138 pc->dwFlags = 0;
1142 /***********************************************************************
1143 * ICSendMessage [MSVFW32.@]
1145 LRESULT VFWAPI ICSendMessage(HIC hic, UINT msg, DWORD_PTR lParam1, DWORD_PTR lParam2)
1147 WINE_HIC* whic = MSVIDEO_GetHicPtr(hic);
1149 if (!whic) return ICERR_BADHANDLE;
1150 return MSVIDEO_SendMessage(whic, msg, lParam1, lParam2);
1153 /***********************************************************************
1154 * ICDrawBegin [MSVFW32.@]
1156 DWORD VFWAPIV ICDrawBegin(
1157 HIC hic, /* [in] */
1158 DWORD dwFlags, /* [in] flags */
1159 HPALETTE hpal, /* [in] palette to draw with */
1160 HWND hwnd, /* [in] window to draw to */
1161 HDC hdc, /* [in] HDC to draw to */
1162 INT xDst, /* [in] destination rectangle */
1163 INT yDst, /* [in] */
1164 INT dxDst, /* [in] */
1165 INT dyDst, /* [in] */
1166 LPBITMAPINFOHEADER lpbi, /* [in] format of frame to draw */
1167 INT xSrc, /* [in] source rectangle */
1168 INT ySrc, /* [in] */
1169 INT dxSrc, /* [in] */
1170 INT dySrc, /* [in] */
1171 DWORD dwRate, /* [in] frames/second = (dwRate/dwScale) */
1172 DWORD dwScale) /* [in] */
1175 ICDRAWBEGIN icdb;
1177 TRACE("(%p,%d,%p,%p,%p,%u,%u,%u,%u,%p,%u,%u,%u,%u,%d,%d)\n",
1178 hic, dwFlags, hpal, hwnd, hdc, xDst, yDst, dxDst, dyDst,
1179 lpbi, xSrc, ySrc, dxSrc, dySrc, dwRate, dwScale);
1181 icdb.dwFlags = dwFlags;
1182 icdb.hpal = hpal;
1183 icdb.hwnd = hwnd;
1184 icdb.hdc = hdc;
1185 icdb.xDst = xDst;
1186 icdb.yDst = yDst;
1187 icdb.dxDst = dxDst;
1188 icdb.dyDst = dyDst;
1189 icdb.lpbi = lpbi;
1190 icdb.xSrc = xSrc;
1191 icdb.ySrc = ySrc;
1192 icdb.dxSrc = dxSrc;
1193 icdb.dySrc = dySrc;
1194 icdb.dwRate = dwRate;
1195 icdb.dwScale = dwScale;
1196 return ICSendMessage(hic,ICM_DRAW_BEGIN,(DWORD_PTR)&icdb,sizeof(icdb));
1199 /***********************************************************************
1200 * ICDraw [MSVFW32.@]
1202 DWORD VFWAPIV ICDraw(HIC hic, DWORD dwFlags, LPVOID lpFormat, LPVOID lpData, DWORD cbData, LONG lTime) {
1203 ICDRAW icd;
1205 TRACE("(%p,%d,%p,%p,%d,%d)\n",hic,dwFlags,lpFormat,lpData,cbData,lTime);
1207 icd.dwFlags = dwFlags;
1208 icd.lpFormat = lpFormat;
1209 icd.lpData = lpData;
1210 icd.cbData = cbData;
1211 icd.lTime = lTime;
1213 return ICSendMessage(hic,ICM_DRAW,(DWORD_PTR)&icd,sizeof(icd));
1216 /***********************************************************************
1217 * ICClose [MSVFW32.@]
1219 LRESULT WINAPI ICClose(HIC hic)
1221 WINE_HIC* whic = MSVIDEO_GetHicPtr(hic);
1222 WINE_HIC** p;
1224 TRACE("(%p)\n",hic);
1226 if (!whic) return ICERR_BADHANDLE;
1228 if (whic->driverproc)
1230 MSVIDEO_SendMessage(whic, DRV_CLOSE, 0, 0);
1231 MSVIDEO_SendMessage(whic, DRV_DISABLE, 0, 0);
1232 MSVIDEO_SendMessage(whic, DRV_FREE, 0, 0);
1234 else
1236 CloseDriver(whic->hdrv, 0, 0);
1239 /* remove whic from list */
1240 for (p = &MSVIDEO_FirstHic; *p != NULL; p = &((*p)->next))
1242 if ((*p) == whic)
1244 *p = whic->next;
1245 break;
1249 HeapFree(GetProcessHeap(), 0, whic);
1250 return 0;
1255 /***********************************************************************
1256 * ICImageCompress [MSVFW32.@]
1258 HANDLE VFWAPI ICImageCompress(
1259 HIC hic, UINT uiFlags,
1260 LPBITMAPINFO lpbiIn, LPVOID lpBits,
1261 LPBITMAPINFO lpbiOut, LONG lQuality,
1262 LONG* plSize)
1264 FIXME("(%p,%08x,%p,%p,%p,%d,%p)\n",
1265 hic, uiFlags, lpbiIn, lpBits, lpbiOut, lQuality, plSize);
1267 return NULL;
1270 /***********************************************************************
1271 * ICImageDecompress [MSVFW32.@]
1274 HANDLE VFWAPI ICImageDecompress(
1275 HIC hic, UINT uiFlags, LPBITMAPINFO lpbiIn,
1276 LPVOID lpBits, LPBITMAPINFO lpbiOut)
1278 HGLOBAL hMem = NULL;
1279 BYTE* pMem = NULL;
1280 BOOL bReleaseIC = FALSE;
1281 BYTE* pHdr = NULL;
1282 ULONG cbHdr = 0;
1283 BOOL bSucceeded = FALSE;
1284 BOOL bInDecompress = FALSE;
1285 DWORD biSizeImage;
1287 TRACE("(%p,%08x,%p,%p,%p)\n",
1288 hic, uiFlags, lpbiIn, lpBits, lpbiOut);
1290 if ( hic == NULL )
1292 hic = ICDecompressOpen( ICTYPE_VIDEO, 0, &lpbiIn->bmiHeader, (lpbiOut != NULL) ? &lpbiOut->bmiHeader : NULL );
1293 if ( hic == NULL )
1295 WARN("no handler\n" );
1296 goto err;
1298 bReleaseIC = TRUE;
1300 if ( uiFlags != 0 )
1302 FIXME( "unknown flag %08x\n", uiFlags );
1303 goto err;
1305 if ( lpbiIn == NULL || lpBits == NULL )
1307 WARN("invalid argument\n");
1308 goto err;
1311 if ( lpbiOut != NULL )
1313 if ( lpbiOut->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) )
1314 goto err;
1315 cbHdr = sizeof(BITMAPINFOHEADER);
1316 if ( lpbiOut->bmiHeader.biCompression == 3 )
1317 cbHdr += sizeof(DWORD)*3;
1318 else
1319 if ( lpbiOut->bmiHeader.biBitCount <= 8 )
1321 if ( lpbiOut->bmiHeader.biClrUsed == 0 )
1322 cbHdr += sizeof(RGBQUAD) * (1<<lpbiOut->bmiHeader.biBitCount);
1323 else
1324 cbHdr += sizeof(RGBQUAD) * lpbiOut->bmiHeader.biClrUsed;
1327 else
1329 TRACE( "get format\n" );
1331 cbHdr = ICDecompressGetFormatSize(hic,lpbiIn);
1332 if ( cbHdr < sizeof(BITMAPINFOHEADER) )
1333 goto err;
1334 pHdr = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,cbHdr+sizeof(RGBQUAD)*256);
1335 if ( pHdr == NULL )
1336 goto err;
1337 if ( ICDecompressGetFormat( hic, lpbiIn, pHdr ) != ICERR_OK )
1338 goto err;
1339 lpbiOut = (BITMAPINFO*)pHdr;
1340 if ( lpbiOut->bmiHeader.biBitCount <= 8 &&
1341 ICDecompressGetPalette( hic, lpbiIn, lpbiOut ) != ICERR_OK &&
1342 lpbiIn->bmiHeader.biBitCount == lpbiOut->bmiHeader.biBitCount )
1344 if ( lpbiIn->bmiHeader.biClrUsed == 0 )
1345 memcpy( lpbiOut->bmiColors, lpbiIn->bmiColors, sizeof(RGBQUAD)*(1<<lpbiOut->bmiHeader.biBitCount) );
1346 else
1347 memcpy( lpbiOut->bmiColors, lpbiIn->bmiColors, sizeof(RGBQUAD)*lpbiIn->bmiHeader.biClrUsed );
1349 if ( lpbiOut->bmiHeader.biBitCount <= 8 &&
1350 lpbiOut->bmiHeader.biClrUsed == 0 )
1351 lpbiOut->bmiHeader.biClrUsed = 1<<lpbiOut->bmiHeader.biBitCount;
1353 lpbiOut->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1354 cbHdr = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*lpbiOut->bmiHeader.biClrUsed;
1357 biSizeImage = lpbiOut->bmiHeader.biSizeImage;
1358 if ( biSizeImage == 0 )
1359 biSizeImage = ((((lpbiOut->bmiHeader.biWidth * lpbiOut->bmiHeader.biBitCount + 7) >> 3) + 3) & (~3)) * abs(lpbiOut->bmiHeader.biHeight);
1361 TRACE( "call ICDecompressBegin\n" );
1363 if ( ICDecompressBegin( hic, lpbiIn, lpbiOut ) != ICERR_OK )
1364 goto err;
1365 bInDecompress = TRUE;
1367 TRACE( "cbHdr %d, biSizeImage %d\n", cbHdr, biSizeImage );
1369 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_ZEROINIT, cbHdr + biSizeImage );
1370 if ( hMem == NULL )
1372 WARN( "out of memory\n" );
1373 goto err;
1375 pMem = GlobalLock( hMem );
1376 if ( pMem == NULL )
1377 goto err;
1378 memcpy( pMem, lpbiOut, cbHdr );
1380 TRACE( "call ICDecompress\n" );
1381 if ( ICDecompress( hic, 0, &lpbiIn->bmiHeader, lpBits, &lpbiOut->bmiHeader, pMem+cbHdr ) != ICERR_OK )
1382 goto err;
1384 bSucceeded = TRUE;
1385 err:
1386 if ( bInDecompress )
1387 ICDecompressEnd( hic );
1388 if ( bReleaseIC )
1389 ICClose(hic);
1390 HeapFree(GetProcessHeap(),0,pHdr);
1391 if ( pMem != NULL )
1392 GlobalUnlock( hMem );
1393 if ( !bSucceeded && hMem != NULL )
1395 GlobalFree(hMem); hMem = NULL;
1398 return hMem;
1401 /***********************************************************************
1402 * ICSeqCompressFrame [MSVFW32.@]
1404 LPVOID VFWAPI ICSeqCompressFrame(PCOMPVARS pc, UINT uiFlags, LPVOID lpBits, BOOL *pfKey, LONG *plSize)
1406 ICCOMPRESS* icComp = pc->lpState;
1407 DWORD ret;
1408 TRACE("(%p, 0x%08x, %p, %p, %p)\n", pc, uiFlags, lpBits, pfKey, plSize);
1410 if (pc->cbState != sizeof(ICCOMPRESS))
1412 ERR("Invalid cbState %i\n", pc->cbState);
1413 return NULL;
1416 if (!pc->lKeyCount++)
1417 icComp->dwFlags = ICCOMPRESS_KEYFRAME;
1418 else
1420 if (pc->lKey && pc->lKeyCount == (pc->lKey - 1))
1421 /* No key frames if pc->lKey == 0 */
1422 pc->lKeyCount = 0;
1423 icComp->dwFlags = 0;
1426 icComp->lpInput = lpBits;
1427 icComp->lFrameNum = pc->lFrame++;
1428 icComp->lpOutput = pc->lpBitsOut;
1429 icComp->lpPrev = pc->lpBitsPrev;
1430 ret = ICSendMessage(pc->hic, ICM_COMPRESS, (DWORD_PTR)icComp, sizeof(*icComp));
1432 if (ret == ICERR_OK)
1434 LPVOID oldprev, oldout;
1436 if (icComp->dwFlags & AVIIF_KEYFRAME)
1438 pc->lKeyCount = 1;
1439 *pfKey = TRUE;
1440 TRACE("Key frame\n");
1442 else
1443 *pfKey = FALSE;
1445 *plSize = icComp->lpbiOutput->biSizeImage;
1447 /* We shift Prev and Out, so we don't have to allocate and release memory */
1448 oldprev = pc->lpBitsPrev;
1449 oldout = pc->lpBitsOut;
1450 pc->lpBitsPrev = oldout;
1451 pc->lpBitsOut = oldprev;
1453 TRACE("returning: %p, compressed frame size %u\n", icComp->lpOutput, *plSize);
1454 return icComp->lpOutput;
1456 return NULL;
1459 static void clear_compvars(PCOMPVARS pc)
1461 HeapFree(GetProcessHeap(), 0, pc->lpbiIn);
1462 HeapFree(GetProcessHeap(), 0, pc->lpBitsPrev);
1463 HeapFree(GetProcessHeap(), 0, pc->lpBitsOut);
1464 HeapFree(GetProcessHeap(), 0, pc->lpState);
1465 pc->lpbiIn = pc->lpBitsPrev = pc->lpBitsOut = pc->lpState = NULL;
1466 if (pc->dwFlags & 0x80000000)
1468 HeapFree(GetProcessHeap(), 0, pc->lpbiOut);
1469 pc->lpbiOut = NULL;
1470 pc->dwFlags &= ~0x80000000;
1474 /***********************************************************************
1475 * ICSeqCompressFrameEnd [MSVFW32.@]
1477 void VFWAPI ICSeqCompressFrameEnd(PCOMPVARS pc)
1479 TRACE("(%p)\n", pc);
1480 ICSendMessage(pc->hic, ICM_COMPRESS_END, 0, 0);
1481 clear_compvars(pc);
1484 /***********************************************************************
1485 * ICSeqCompressFrameStart [MSVFW32.@]
1487 BOOL VFWAPI ICSeqCompressFrameStart(PCOMPVARS pc, LPBITMAPINFO lpbiIn)
1489 /* I'm ignoring bmiColors as I don't know what to do with it,
1490 * it doesn't appear to be used though
1492 DWORD ret;
1493 ICCOMPRESS* icComp;
1494 pc->lpbiIn = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFO));
1495 if (!pc->lpbiIn)
1496 return FALSE;
1498 *pc->lpbiIn = *lpbiIn;
1500 pc->lpState = HeapAlloc(GetProcessHeap(), 0, sizeof(ICCOMPRESS)
1501 + sizeof(*icComp->lpckid) + sizeof(*icComp->lpdwFlags));
1502 if (!pc->lpState)
1503 goto error;
1505 pc->cbState = sizeof(ICCOMPRESS);
1507 if (!pc->lpbiOut)
1509 /* Ask compressor for needed header size */
1510 int size = ICSendMessage(pc->hic, ICM_COMPRESS_GET_FORMAT,
1511 (DWORD_PTR)pc->lpbiIn, 0);
1512 if (size <= 0)
1513 goto error;
1515 pc->lpbiOut = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1516 if (!pc->lpbiOut)
1517 goto error;
1518 /* Flag to show that we allocated lpbiOut for proper cleanup */
1519 pc->dwFlags |= 0x80000000;
1521 ret = ICSendMessage(pc->hic, ICM_COMPRESS_GET_FORMAT,
1522 (DWORD_PTR)pc->lpbiIn, (DWORD_PTR)pc->lpbiOut);
1523 if (ret != ICERR_OK)
1525 ERR("Could not get output format from compressor\n");
1526 goto error;
1528 if (!pc->lpbiOut->bmiHeader.biSizeImage)
1530 /* If we can't know the output frame size for sure at least allocate
1531 * the same size of the input frame and also at least 8Kb to be sure
1532 * that poor compressors will have enough memory to work if the input
1533 * frame is too small.
1535 pc->lpbiOut->bmiHeader.biSizeImage = max(8192, pc->lpbiIn->bmiHeader.biSizeImage);
1536 ERR("Bad codec! Invalid output frame size, guessing from input\n");
1540 TRACE("Input: %ux%u, fcc %s, bpp %u, size %u\n",
1541 pc->lpbiIn->bmiHeader.biWidth, pc->lpbiIn->bmiHeader.biHeight,
1542 wine_dbgstr_fcc(pc->lpbiIn->bmiHeader.biCompression),
1543 pc->lpbiIn->bmiHeader.biBitCount,
1544 pc->lpbiIn->bmiHeader.biSizeImage);
1545 TRACE("Output: %ux%u, fcc %s, bpp %u, size %u\n",
1546 pc->lpbiOut->bmiHeader.biWidth, pc->lpbiOut->bmiHeader.biHeight,
1547 wine_dbgstr_fcc(pc->lpbiOut->bmiHeader.biCompression),
1548 pc->lpbiOut->bmiHeader.biBitCount,
1549 pc->lpbiOut->bmiHeader.biSizeImage);
1551 /* Buffer for compressed frame data */
1552 pc->lpBitsOut = HeapAlloc(GetProcessHeap(), 0, pc->lpbiOut->bmiHeader.biSizeImage);
1553 if (!pc->lpBitsOut)
1554 goto error;
1556 /* Buffer for previous compressed frame data */
1557 pc->lpBitsPrev = HeapAlloc(GetProcessHeap(), 0, pc->lpbiOut->bmiHeader.biSizeImage);
1558 if (!pc->lpBitsPrev)
1559 goto error;
1561 TRACE("Compvars:\n"
1562 "\tsize: %i\n"
1563 "\tflags: 0x%x\n"
1564 "\thic: %p\n"
1565 "\ttype: %s\n"
1566 "\thandler: %s\n"
1567 "\tin/out: %p/%p\n"
1568 "\tkey/data/quality: %i/%i/%i\n",
1569 pc->cbSize, pc->dwFlags, pc->hic, wine_dbgstr_fcc(pc->fccType),
1570 wine_dbgstr_fcc(pc->fccHandler), pc->lpbiIn, pc->lpbiOut, pc->lKey,
1571 pc->lDataRate, pc->lQ);
1573 ret = ICSendMessage(pc->hic, ICM_COMPRESS_BEGIN, (DWORD_PTR)pc->lpbiIn, (DWORD_PTR)pc->lpbiOut);
1574 if (ret == ICERR_OK)
1576 icComp = pc->lpState;
1577 /* Initialise some variables */
1578 pc->lFrame = 0; pc->lKeyCount = 0;
1580 icComp->lpbiOutput = &pc->lpbiOut->bmiHeader;
1581 icComp->lpbiInput = &pc->lpbiIn->bmiHeader;
1582 icComp->lpckid = (DWORD *)(icComp + 1);
1583 *icComp->lpckid = 0;
1584 icComp->lpdwFlags = (DWORD *)((char *)(icComp + 1) + sizeof(*icComp->lpckid));
1585 *icComp->lpdwFlags = 0;
1586 icComp->dwFrameSize = 0;
1587 icComp->dwQuality = pc->lQ;
1588 icComp->lpbiPrev = &pc->lpbiIn->bmiHeader;
1589 return TRUE;
1591 error:
1592 clear_compvars(pc);
1593 return FALSE;
1596 /***********************************************************************
1597 * GetFileNamePreview [MSVFW32.@]
1599 static BOOL GetFileNamePreview(LPVOID lpofn,BOOL bSave,BOOL bUnicode)
1601 CHAR szFunctionName[20];
1602 BOOL (*fnGetFileName)(LPVOID);
1603 HMODULE hComdlg32;
1604 BOOL ret;
1606 FIXME("(%p,%d,%d), semi-stub!\n",lpofn,bSave,bUnicode);
1608 lstrcpyA(szFunctionName, (bSave ? "GetSaveFileName" : "GetOpenFileName"));
1609 lstrcatA(szFunctionName, (bUnicode ? "W" : "A"));
1611 hComdlg32 = LoadLibraryA("COMDLG32.DLL");
1612 if (hComdlg32 == NULL)
1613 return FALSE;
1615 fnGetFileName = (LPVOID)GetProcAddress(hComdlg32, szFunctionName);
1616 if (fnGetFileName == NULL)
1618 FreeLibrary(hComdlg32);
1619 return FALSE;
1622 /* FIXME: need to add OFN_ENABLEHOOK and our own handler */
1623 ret = fnGetFileName(lpofn);
1625 FreeLibrary(hComdlg32);
1626 return ret;
1629 /***********************************************************************
1630 * GetOpenFileNamePreviewA [MSVFW32.@]
1632 BOOL WINAPI GetOpenFileNamePreviewA(LPOPENFILENAMEA lpofn)
1634 FIXME("(%p), semi-stub!\n", lpofn);
1636 return GetFileNamePreview(lpofn, FALSE, FALSE);
1639 /***********************************************************************
1640 * GetOpenFileNamePreviewW [MSVFW32.@]
1642 BOOL WINAPI GetOpenFileNamePreviewW(LPOPENFILENAMEW lpofn)
1644 FIXME("(%p), semi-stub!\n", lpofn);
1646 return GetFileNamePreview(lpofn, FALSE, TRUE);
1649 /***********************************************************************
1650 * GetSaveFileNamePreviewA [MSVFW32.@]
1652 BOOL WINAPI GetSaveFileNamePreviewA(LPOPENFILENAMEA lpofn)
1654 FIXME("(%p), semi-stub!\n", lpofn);
1656 return GetFileNamePreview(lpofn, TRUE, FALSE);
1659 /***********************************************************************
1660 * GetSaveFileNamePreviewW [MSVFW32.@]
1662 BOOL WINAPI GetSaveFileNamePreviewW(LPOPENFILENAMEW lpofn)
1664 FIXME("(%p), semi-stub!\n", lpofn);
1666 return GetFileNamePreview(lpofn, TRUE, TRUE);