dmime: Implement IDirectMusicSegment_(Download|Unload).
[wine.git] / dlls / msvfw32 / msvideo_main.c
blobb9e49d309236fae057d2223d2158ea2331b93f0e
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 "winternl.h"
39 #include "winuser.h"
40 #include "commdlg.h"
41 #include "vfw.h"
42 #include "msvideo_private.h"
43 #include "wine/debug.h"
44 #include "wine/list.h"
46 /* Drivers32 settings */
47 #define HKLM_DRIVERS32 "Software\\Microsoft\\Windows NT\\CurrentVersion\\Drivers32"
49 WINE_DEFAULT_DEBUG_CHANNEL(msvideo);
51 /* This one is a macro in order to work for both ASCII and Unicode */
52 #define fourcc_to_string(str, fcc) do { \
53 (str)[0] = LOBYTE(LOWORD(fcc)); \
54 (str)[1] = HIBYTE(LOWORD(fcc)); \
55 (str)[2] = LOBYTE(HIWORD(fcc)); \
56 (str)[3] = HIBYTE(HIWORD(fcc)); \
57 } while(0)
59 static inline const char *wine_dbgstr_fcc( DWORD fcc )
61 char fcc_str[5];
62 fourcc_to_string(fcc_str, fcc);
63 fcc_str[4] = '\0';
64 /* Last byte may be ' ' in some cases like "DIB " */
65 if (isalnum(fcc_str[0]) && isalnum(fcc_str[1]) && isalnum(fcc_str[2])
66 && (isalnum(fcc_str[3]) || isspace(fcc_str[3])))
67 return wine_dbg_sprintf("%s", fcc_str);
68 return wine_dbg_sprintf("0x%08lx", fcc);
71 static const char *wine_dbgstr_icerr( int ret )
73 const char *str;
74 if (ret <= ICERR_CUSTOM)
75 return wine_dbg_sprintf("ICERR_CUSTOM (%d)", ret);
76 #define XX(x) case (x): str = #x; break
77 switch (ret)
79 XX(ICERR_OK);
80 XX(ICERR_DONTDRAW);
81 XX(ICERR_NEWPALETTE);
82 XX(ICERR_GOTOKEYFRAME);
83 XX(ICERR_STOPDRAWING);
84 XX(ICERR_UNSUPPORTED);
85 XX(ICERR_BADFORMAT);
86 XX(ICERR_MEMORY);
87 XX(ICERR_INTERNAL);
88 XX(ICERR_BADFLAGS);
89 XX(ICERR_BADPARAM);
90 XX(ICERR_BADSIZE);
91 XX(ICERR_BADHANDLE);
92 XX(ICERR_CANTUPDATE);
93 XX(ICERR_ABORT);
94 XX(ICERR_ERROR);
95 XX(ICERR_BADBITDEPTH);
96 XX(ICERR_BADIMAGESIZE);
97 default: str = wine_dbg_sprintf("UNKNOWN (%d)", ret);
99 #undef XX
100 return str;
103 static WINE_HIC* MSVIDEO_FirstHic /* = NULL */;
105 struct reg_driver
107 DWORD fccType;
108 DWORD fccHandler;
109 DRIVERPROC proc;
110 struct list entry;
113 static struct list reg_driver_list = LIST_INIT(reg_driver_list);
115 HMODULE MSVFW32_hModule;
117 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
119 TRACE("%p,%lx,%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%08Ix,0x%08Ix)\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%08Ix,0x%08Ix) 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 _strnicmp(fcc_str1, fcc_str2, 4);
226 static DWORD get_size_image(LONG width, LONG height, WORD depth)
228 DWORD ret = width * depth;
229 ret = (ret + 7) / 8; /* divide by byte size, rounding up */
230 ret = (ret + 3) & ~3; /* align to 4 bytes */
231 ret *= abs(height);
232 return ret;
235 /******************************************************************
236 * MSVIDEO_GetHicPtr
240 static WINE_HIC* MSVIDEO_GetHicPtr(HIC hic)
242 WINE_HIC* whic;
244 for (whic = MSVIDEO_FirstHic; whic && whic->hic != hic; whic = whic->next);
245 return whic;
248 /***********************************************************************
249 * VideoForWindowsVersion [MSVFW32.2]
250 * VideoForWindowsVersion [MSVIDEO.2]
251 * Returns the version in major.minor form.
252 * In Windows95 this returns 0x040003b6 (4.950)
254 DWORD WINAPI VideoForWindowsVersion(void)
256 return 0x040003B6; /* 4.950 */
259 /***********************************************************************
260 * ICInfo [MSVFW32.@]
261 * Get information about an installable compressor. Return TRUE if there
262 * is one.
264 * PARAMS
265 * fccType [I] type of compressor (e.g. 'vidc')
266 * fccHandler [I] real fcc for handler or <n>th compressor
267 * lpicinfo [O] information about compressor
269 BOOL VFWAPI ICInfo(DWORD type, DWORD handler, ICINFO *info)
271 char name_buf[10], buf[2048];
272 DWORD ret_type, ret_handler;
273 struct reg_driver *driver;
274 DWORD i, count = 0;
275 LONG res;
276 HKEY key;
278 TRACE("type %s, handler %s, info %p.\n",
279 wine_dbgstr_fcc(type), wine_dbgstr_fcc(handler), info);
281 memset(info, 0, sizeof(*info));
282 info->dwSize = sizeof(*info);
283 info->dwVersionICM = ICVERSION;
285 if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, HKLM_DRIVERS32, 0, KEY_QUERY_VALUE, &key))
287 i = 0;
288 for (;;)
290 DWORD name_len = ARRAY_SIZE(name_buf), driver_len = ARRAY_SIZE(info->szDriver);
292 res = RegEnumValueA(key, i++, name_buf, &name_len, 0, 0, (BYTE *)buf, &driver_len);
293 if (res == ERROR_NO_MORE_ITEMS) break;
295 if (name_len != 9 || name_buf[4] != '.') continue;
296 ret_type = mmioStringToFOURCCA(name_buf, 0);
297 ret_handler = mmioStringToFOURCCA(name_buf + 5, 0);
298 if (type && compare_fourcc(type, ret_type)) continue;
299 if (compare_fourcc(handler, ret_handler) && handler != count++) continue;
301 info->fccType = ret_type;
302 info->fccHandler = ret_handler;
303 MultiByteToWideChar(CP_ACP, 0, buf, -1, info->szDriver, ARRAY_SIZE(info->szDriver));
304 TRACE("Returning codec %s, driver %s.\n", debugstr_a(name_buf), debugstr_a(buf));
305 return TRUE;
307 RegCloseKey(key);
310 if (GetPrivateProfileSectionA("drivers32", buf, sizeof(buf), "system.ini"))
312 char *s;
313 for (s = buf; *s; s += strlen(s) + 1)
315 if (s[4] != '.' || s[9] != '=') continue;
316 ret_type = mmioStringToFOURCCA(s, 0);
317 ret_handler = mmioStringToFOURCCA(s + 5, 0);
318 if (type && compare_fourcc(type, ret_type)) continue;
319 if (compare_fourcc(handler, ret_handler) && handler != count++) continue;
321 info->fccType = ret_type;
322 info->fccHandler = ret_handler;
323 MultiByteToWideChar(CP_ACP, 0, s + 10, -1, info->szDriver, ARRAY_SIZE(info->szDriver));
324 TRACE("Returning codec %s, driver %s.\n", debugstr_an(s, 9), debugstr_a(s + 10));
325 return TRUE;
329 LIST_FOR_EACH_ENTRY(driver, &reg_driver_list, struct reg_driver, entry)
331 if (type && compare_fourcc(type, driver->fccType)) continue;
332 if (compare_fourcc(handler, driver->fccHandler) && handler != count++) continue;
333 if (driver->proc(0, NULL, ICM_GETINFO, (DWORD_PTR)info, sizeof(*info)) == sizeof(*info))
334 return TRUE;
337 info->fccType = type;
338 info->fccHandler = handler;
339 WARN("No driver found for codec %s.%s.\n", wine_dbgstr_fcc(type), wine_dbgstr_fcc(handler));
340 return FALSE;
343 static DWORD IC_HandleRef = 1;
345 /***********************************************************************
346 * ICInstall [MSVFW32.@]
348 BOOL VFWAPI ICInstall(DWORD type, DWORD handler, LPARAM lparam, char *desc, UINT flags)
350 struct reg_driver *driver;
352 TRACE("type %s, handler %s, lparam %#Ix, desc %s, flags %#x.\n",
353 wine_dbgstr_fcc(type), wine_dbgstr_fcc(handler), lparam, debugstr_a(desc), flags);
355 LIST_FOR_EACH_ENTRY(driver, &reg_driver_list, struct reg_driver, entry)
357 if (!compare_fourcc(type, driver->fccType)
358 && !compare_fourcc(handler, driver->fccHandler))
360 return FALSE;
364 switch (flags)
366 case ICINSTALL_FUNCTION:
367 if (!(driver = calloc(1, sizeof(*driver))))
368 return FALSE;
369 driver->fccType = type;
370 driver->fccHandler = handler;
371 driver->proc = (DRIVERPROC)lparam;
372 list_add_tail(&reg_driver_list, &driver->entry);
373 return TRUE;
374 case ICINSTALL_DRIVER:
376 const char *driver = (const char *)lparam;
377 char value[10];
378 HKEY key;
379 LONG res;
381 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, HKLM_DRIVERS32, 0, KEY_SET_VALUE, &key))
382 return FALSE;
383 fourcc_to_string(value, type);
384 value[4] = '.';
385 fourcc_to_string(value + 5, handler);
386 value[9] = 0;
387 res = RegSetValueExA(key, value, 0, REG_SZ, (const BYTE *)driver, strlen(driver) + 1);
388 RegCloseKey(key);
389 return !res;
391 default:
392 FIXME("Unhandled flags %#x.\n", flags);
393 return FALSE;
397 /***********************************************************************
398 * ICRemove [MSVFW32.@]
400 BOOL VFWAPI ICRemove(DWORD type, DWORD handler, UINT flags)
402 struct reg_driver *driver;
403 char value[10];
404 HKEY key;
405 LONG res;
407 TRACE("type %s, handler %s, flags %#x.\n",
408 wine_dbgstr_fcc(type), wine_dbgstr_fcc(handler), flags);
410 LIST_FOR_EACH_ENTRY(driver, &reg_driver_list, struct reg_driver, entry)
412 if (!compare_fourcc(type, driver->fccType)
413 && !compare_fourcc(handler, driver->fccHandler))
415 list_remove(&driver->entry);
416 free(driver);
417 return TRUE;
421 if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, HKLM_DRIVERS32, 0, KEY_SET_VALUE, &key))
423 fourcc_to_string(value, type);
424 value[4] = '.';
425 fourcc_to_string(value + 5, handler);
426 value[9] = 0;
427 res = RegDeleteValueA(key, value);
428 RegCloseKey(key);
429 return !res;
432 return FALSE;
436 /***********************************************************************
437 * ICOpen [MSVFW32.@]
438 * Opens an installable compressor. Return special handle.
440 HIC VFWAPI ICOpen(DWORD fccType, DWORD fccHandler, UINT wMode)
442 WCHAR codecname[10];
443 ICOPEN icopen;
444 WINE_HIC* whic;
445 static const WCHAR drv32W[] = {'d','r','i','v','e','r','s','3','2','\0'};
446 struct reg_driver *driver;
447 HDRVR hdrv = NULL;
449 TRACE("(%s,%s,0x%08x)\n", wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wMode);
451 if (!fccHandler) /* No specific handler, return the first valid for wMode */
453 HIC local;
454 ICINFO info;
455 DWORD loop = 0;
456 info.dwSize = sizeof(info);
457 while(ICInfo(fccType, loop++, &info))
459 /* Ensure fccHandler is not 0x0 because we will recurse on ICOpen */
460 if(!info.fccHandler)
461 continue;
462 local = ICOpen(fccType, info.fccHandler, wMode);
463 if (local != 0)
465 TRACE("Returning %s as default handler for %s\n",
466 wine_dbgstr_fcc(info.fccHandler), wine_dbgstr_fcc(fccType));
467 return local;
472 LIST_FOR_EACH_ENTRY(driver, &reg_driver_list, struct reg_driver, entry)
474 if (!compare_fourcc(fccType, driver->fccType)
475 && !compare_fourcc(fccHandler, driver->fccHandler))
477 return ICOpenFunction(driver->fccType, driver->fccHandler, wMode, driver->proc);
481 /* Well, lParam2 is in fact a LPVIDEO_OPEN_PARMS, but it has the
482 * same layout as ICOPEN
484 icopen.dwSize = sizeof(ICOPEN);
485 icopen.fccType = fccType;
486 icopen.fccHandler = fccHandler;
487 icopen.dwVersion = 0x00001000; /* FIXME */
488 icopen.dwFlags = wMode;
489 icopen.dwError = 0;
490 icopen.pV1Reserved = NULL;
491 icopen.pV2Reserved = NULL;
492 icopen.dnDevNode = 0; /* FIXME */
494 if (!hdrv)
496 /* normalize to lower case as in 'vidc' */
497 ((char*)&fccType)[0] = tolower(((char*)&fccType)[0]);
498 ((char*)&fccType)[1] = tolower(((char*)&fccType)[1]);
499 ((char*)&fccType)[2] = tolower(((char*)&fccType)[2]);
500 ((char*)&fccType)[3] = tolower(((char*)&fccType)[3]);
501 icopen.fccType = fccType;
502 /* Seek the driver in the registry */
503 fourcc_to_string(codecname, fccType);
504 codecname[4] = '.';
505 fourcc_to_string(codecname + 5, fccHandler);
506 codecname[9] = '\0';
508 hdrv = OpenDriver(codecname, drv32W, (LPARAM)&icopen);
509 if (!hdrv)
510 return 0;
513 if (!(whic = malloc(sizeof(*whic))))
515 CloseDriver(hdrv, 0, 0);
516 return FALSE;
518 whic->hdrv = hdrv;
519 whic->driverproc = NULL;
520 whic->type = fccType;
521 whic->handler = fccHandler;
522 while (MSVIDEO_GetHicPtr((HIC)(ULONG_PTR)IC_HandleRef) != NULL) IC_HandleRef++;
523 whic->hic = (HIC)(ULONG_PTR)IC_HandleRef++;
524 whic->next = MSVIDEO_FirstHic;
525 MSVIDEO_FirstHic = whic;
527 TRACE("=> %p\n", whic->hic);
528 return whic->hic;
531 /***********************************************************************
532 * ICOpenFunction [MSVFW32.@]
534 HIC VFWAPI ICOpenFunction(DWORD fccType, DWORD fccHandler, UINT wMode, DRIVERPROC lpfnHandler)
536 ICOPEN icopen;
537 WINE_HIC* whic;
539 TRACE("(%s,%s,%d,%p)\n",
540 wine_dbgstr_fcc(fccType), wine_dbgstr_fcc(fccHandler), wMode, lpfnHandler);
542 icopen.dwSize = sizeof(ICOPEN);
543 icopen.fccType = fccType;
544 icopen.fccHandler = fccHandler;
545 icopen.dwVersion = ICVERSION;
546 icopen.dwFlags = wMode;
547 icopen.dwError = 0;
548 icopen.pV1Reserved = NULL;
549 icopen.pV2Reserved = NULL;
550 icopen.dnDevNode = 0; /* FIXME */
552 if (!(whic = malloc(sizeof(*whic))))
553 return NULL;
555 whic->driverproc = lpfnHandler;
556 while (MSVIDEO_GetHicPtr((HIC)(ULONG_PTR)IC_HandleRef) != NULL) IC_HandleRef++;
557 whic->hic = (HIC)(ULONG_PTR)IC_HandleRef++;
558 whic->next = MSVIDEO_FirstHic;
559 MSVIDEO_FirstHic = whic;
561 /* Now try opening/loading the driver. Taken from DRIVER_AddToList */
562 /* What if the function is used more than once? */
564 if (MSVIDEO_SendMessage(whic, DRV_LOAD, 0L, 0L) != DRV_SUCCESS)
566 WARN("DRV_LOAD failed for hic %p\n", whic->hic);
567 MSVIDEO_FirstHic = whic->next;
568 free(whic);
569 return 0;
571 /* return value is not checked */
572 MSVIDEO_SendMessage(whic, DRV_ENABLE, 0L, 0L);
574 whic->driverId = (DWORD)MSVIDEO_SendMessage(whic, DRV_OPEN, 0, (DWORD_PTR)&icopen);
575 /* FIXME: What should we put here? */
576 whic->hdrv = NULL;
578 if (whic->driverId == 0)
580 WARN("DRV_OPEN failed for hic %p\n", whic->hic);
581 MSVIDEO_FirstHic = whic->next;
582 free(whic);
583 return 0;
586 TRACE("=> %p\n", whic->hic);
587 return whic->hic;
590 /***********************************************************************
591 * ICGetInfo [MSVFW32.@]
593 LRESULT VFWAPI ICGetInfo(HIC hic, ICINFO *picinfo, DWORD cb)
595 LRESULT ret;
596 WINE_HIC* whic = MSVIDEO_GetHicPtr(hic);
598 TRACE("(%p,%p,%ld)\n", hic, picinfo, cb);
600 if (!whic) return ICERR_BADHANDLE;
601 if (!picinfo) return MMSYSERR_INVALPARAM;
603 /* (WS) The field szDriver should be initialized because the driver
604 * is not obliged and often will not do it. Some applications, like
605 * VirtualDub, rely on this field and will occasionally crash if it
606 * goes uninitialized.
608 if (cb >= sizeof(ICINFO)) picinfo->szDriver[0] = '\0';
610 ret = ICSendMessage(hic, ICM_GETINFO, (DWORD_PTR)picinfo, cb);
612 /* (WS) When szDriver was not supplied by the driver itself, apparently
613 * Windows will set its value equal to the driver file name. This can
614 * be obtained from the registry as we do here.
616 if (cb >= sizeof(ICINFO) && picinfo->szDriver[0] == 0)
618 ICINFO ii;
620 memset(&ii, 0, sizeof(ii));
621 ii.dwSize = sizeof(ii);
622 ICInfo(picinfo->fccType, picinfo->fccHandler, &ii);
623 lstrcpyW(picinfo->szDriver, ii.szDriver);
626 return ret;
629 /***********************************************************************
630 * ICLocate [MSVFW32.@]
632 HIC VFWAPI ICLocate(DWORD type, DWORD handler, BITMAPINFOHEADER *in,
633 BITMAPINFOHEADER *out, WORD mode)
635 ICINFO info = {sizeof(info)};
636 UINT msg;
637 HIC hic;
638 DWORD i;
640 TRACE("type %s, handler %s, in %p, out %p, mode %u.\n",
641 wine_dbgstr_fcc(type), wine_dbgstr_fcc(handler), in, out, mode);
643 switch (mode)
645 case ICMODE_FASTCOMPRESS:
646 case ICMODE_COMPRESS:
647 msg = ICM_COMPRESS_QUERY;
648 break;
649 case ICMODE_FASTDECOMPRESS:
650 case ICMODE_DECOMPRESS:
651 msg = ICM_DECOMPRESS_QUERY;
652 break;
653 case ICMODE_DRAW:
654 msg = ICM_DRAW_QUERY;
655 break;
656 default:
657 FIXME("Unhandled mode %#x.\n", mode);
658 return 0;
661 if ((hic = ICOpen(type, handler, mode)))
663 if (!ICSendMessage(hic, msg, (DWORD_PTR)in, (DWORD_PTR)out))
665 TRACE("Found codec %s.%s.\n", wine_dbgstr_fcc(type),
666 wine_dbgstr_fcc(handler));
667 return hic;
669 ICClose(hic);
672 for (i = 0; ICInfo(type, i, &info); ++i)
674 if ((hic = ICOpen(info.fccType, info.fccHandler, mode)))
676 if (!ICSendMessage(hic, msg, (DWORD_PTR)in, (DWORD_PTR)out))
678 TRACE("Found codec %s.%s.\n", wine_dbgstr_fcc(info.fccType),
679 wine_dbgstr_fcc(info.fccHandler));
680 return hic;
682 ICClose(hic);
686 if (type == streamtypeVIDEO)
687 return ICLocate(ICTYPE_VIDEO, handler, in, out, mode);
689 WARN("Could not find a driver for codec %s.%s.\n",
690 wine_dbgstr_fcc(type), wine_dbgstr_fcc(handler));
692 return 0;
695 /***********************************************************************
696 * ICGetDisplayFormat [MSVFW32.@]
698 HIC VFWAPI ICGetDisplayFormat(HIC hic, BITMAPINFOHEADER *in, BITMAPINFOHEADER *out,
699 int depth, int width, int height)
701 HIC tmphic = hic;
703 TRACE("(%p, %p, %p, %d, %d, %d)\n", hic, in, out, depth, width, height);
705 if (!tmphic)
707 tmphic = ICLocate(ICTYPE_VIDEO, 0, in, NULL, ICMODE_DECOMPRESS);
708 if (!tmphic)
709 return NULL;
712 if (ICDecompressQuery(tmphic, in, NULL))
713 goto err;
715 if (width <= 0 || height <= 0)
717 width = in->biWidth;
718 height = in->biHeight;
721 if (!depth)
722 depth = 32;
724 *out = *in;
725 out->biSize = sizeof(*out);
726 out->biWidth = width;
727 out->biHeight = height;
728 out->biCompression = BI_RGB;
729 out->biSizeImage = get_size_image(width, height, depth);
731 /* first try the given depth */
732 out->biBitCount = depth;
733 out->biSizeImage = get_size_image(width, height, out->biBitCount);
734 if (!ICDecompressQuery(tmphic, in, out))
736 if (depth == 8)
737 ICDecompressGetPalette(tmphic, in, out);
738 return tmphic;
741 /* then try 16, both with BI_RGB and BI_BITFIELDS */
742 if (depth <= 16)
744 out->biBitCount = 16;
745 out->biSizeImage = get_size_image(width, height, out->biBitCount);
746 if (!ICDecompressQuery(tmphic, in, out))
747 return tmphic;
749 out->biCompression = BI_BITFIELDS;
750 if (!ICDecompressQuery(tmphic, in, out))
751 return tmphic;
752 out->biCompression = BI_RGB;
755 /* then try 24 */
756 if (depth <= 24)
758 out->biBitCount = 24;
759 out->biSizeImage = get_size_image(width, height, out->biBitCount);
760 if (!ICDecompressQuery(tmphic, in, out))
761 return tmphic;
764 /* then try 32 */
765 if (depth <= 32)
767 out->biBitCount = 32;
768 out->biSizeImage = get_size_image(width, height, out->biBitCount);
769 if (!ICDecompressQuery(tmphic, in, out))
770 return tmphic;
773 /* as a last resort, try 32 bpp with the original width and height */
774 out->biWidth = in->biWidth;
775 out->biHeight = in->biHeight;
776 out->biBitCount = 32;
777 out->biSizeImage = get_size_image(out->biWidth, out->biHeight, out->biBitCount);
778 if (!ICDecompressQuery(tmphic, in, out))
779 return tmphic;
781 /* finally, ask the compressor for its default output format */
782 if (!ICSendMessage(tmphic, ICM_DECOMPRESS_GET_FORMAT, (DWORD_PTR)in, (DWORD_PTR)out))
783 return tmphic;
785 err:
786 if (hic != tmphic)
787 ICClose(tmphic);
789 return NULL;
792 /***********************************************************************
793 * ICCompress [MSVFW32.@]
795 DWORD VFWAPIV
796 ICCompress(
797 HIC hic,DWORD dwFlags,LPBITMAPINFOHEADER lpbiOutput,LPVOID lpData,
798 LPBITMAPINFOHEADER lpbiInput,LPVOID lpBits,LPDWORD lpckid,
799 LPDWORD lpdwFlags,LONG lFrameNum,DWORD dwFrameSize,DWORD dwQuality,
800 LPBITMAPINFOHEADER lpbiPrev,LPVOID lpPrev)
802 ICCOMPRESS iccmp;
804 TRACE("(%p,%ld,%p,%p,%p,%p,...)\n",hic,dwFlags,lpbiOutput,lpData,lpbiInput,lpBits);
806 iccmp.dwFlags = dwFlags;
808 iccmp.lpbiOutput = lpbiOutput;
809 iccmp.lpOutput = lpData;
810 iccmp.lpbiInput = lpbiInput;
811 iccmp.lpInput = lpBits;
813 iccmp.lpckid = lpckid;
814 iccmp.lpdwFlags = lpdwFlags;
815 iccmp.lFrameNum = lFrameNum;
816 iccmp.dwFrameSize = dwFrameSize;
817 iccmp.dwQuality = dwQuality;
818 iccmp.lpbiPrev = lpbiPrev;
819 iccmp.lpPrev = lpPrev;
820 return ICSendMessage(hic,ICM_COMPRESS,(DWORD_PTR)&iccmp,sizeof(iccmp));
823 /***********************************************************************
824 * ICDecompress [MSVFW32.@]
826 DWORD VFWAPIV ICDecompress(HIC hic,DWORD dwFlags,LPBITMAPINFOHEADER lpbiFormat,
827 LPVOID lpData,LPBITMAPINFOHEADER lpbi,LPVOID lpBits)
829 ICDECOMPRESS icd;
830 DWORD ret;
832 TRACE("(%p,%ld,%p,%p,%p,%p)\n",hic,dwFlags,lpbiFormat,lpData,lpbi,lpBits);
834 icd.dwFlags = dwFlags;
835 icd.lpbiInput = lpbiFormat;
836 icd.lpInput = lpData;
838 icd.lpbiOutput = lpbi;
839 icd.lpOutput = lpBits;
840 icd.ckid = 0;
841 ret = ICSendMessage(hic,ICM_DECOMPRESS,(DWORD_PTR)&icd,sizeof(ICDECOMPRESS));
843 return ret;
847 struct choose_compressor
849 UINT flags;
850 LPCSTR title;
851 COMPVARS cv;
854 struct codec_info
856 HIC hic;
857 ICINFO icinfo;
860 static BOOL enum_compressors(HWND list, COMPVARS *pcv, BOOL enum_all)
862 UINT id, total = 0;
863 ICINFO icinfo;
865 id = 0;
867 while (ICInfo(pcv->fccType, id, &icinfo))
869 struct codec_info *ic;
870 DWORD idx;
871 HIC hic;
873 id++;
875 hic = ICOpen(icinfo.fccType, icinfo.fccHandler, ICMODE_COMPRESS);
877 if (hic)
879 /* for unknown reason fccHandler reported by the driver
880 * doesn't always work, use the one returned by ICInfo instead.
882 DWORD fccHandler = icinfo.fccHandler;
884 if (!enum_all && pcv->lpbiIn)
886 if (ICCompressQuery(hic, pcv->lpbiIn, NULL) != ICERR_OK)
888 TRACE("fccHandler %s doesn't support input DIB format %ld\n",
889 wine_dbgstr_fcc(icinfo.fccHandler), pcv->lpbiIn->bmiHeader.biCompression);
890 ICClose(hic);
891 continue;
895 ICGetInfo(hic, &icinfo, sizeof(icinfo));
896 icinfo.fccHandler = fccHandler;
898 idx = SendMessageW(list, CB_ADDSTRING, 0, (LPARAM)icinfo.szDescription);
900 ic = malloc(sizeof(*ic));
901 ic->icinfo = icinfo;
902 ic->hic = hic;
903 SendMessageW(list, CB_SETITEMDATA, idx, (LPARAM)ic);
905 total++;
908 return total != 0;
911 static INT_PTR CALLBACK icm_choose_compressor_dlgproc(HWND hdlg, UINT msg, WPARAM wparam, LPARAM lparam)
913 switch (msg)
915 case WM_INITDIALOG:
917 struct codec_info *ic;
918 WCHAR buf[128];
919 struct choose_compressor *choose_comp = (struct choose_compressor *)lparam;
921 SetWindowLongPtrW(hdlg, DWLP_USER, lparam);
923 /* FIXME */
924 choose_comp->flags &= ~(ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME);
926 if (choose_comp->title)
927 SetWindowTextA(hdlg, choose_comp->title);
929 if (!(choose_comp->flags & ICMF_CHOOSE_DATARATE))
931 ShowWindow(GetDlgItem(hdlg, IDC_DATARATE_CHECKBOX), SW_HIDE);
932 ShowWindow(GetDlgItem(hdlg, IDC_DATARATE), SW_HIDE);
933 ShowWindow(GetDlgItem(hdlg, IDC_DATARATE_KB), SW_HIDE);
936 if (!(choose_comp->flags & ICMF_CHOOSE_KEYFRAME))
938 ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME_CHECKBOX), SW_HIDE);
939 ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME), SW_HIDE);
940 ShowWindow(GetDlgItem(hdlg, IDC_KEYFRAME_FRAMES), SW_HIDE);
943 /* FIXME */
944 EnableWindow(GetDlgItem(hdlg, IDC_QUALITY_SCROLL), FALSE);
945 EnableWindow(GetDlgItem(hdlg, IDC_QUALITY_TXT), FALSE);
947 /*if (!(choose_comp->flags & ICMF_CHOOSE_PREVIEW))
948 ShowWindow(GetDlgItem(hdlg, IDC_PREVIEW), SW_HIDE);*/
950 LoadStringW(MSVFW32_hModule, IDS_FULLFRAMES, buf, 128);
951 SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_ADDSTRING, 0, (LPARAM)buf);
953 ic = malloc(sizeof(*ic));
954 ic->icinfo.fccType = streamtypeVIDEO;
955 ic->icinfo.fccHandler = comptypeDIB;
956 ic->hic = 0;
957 SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_SETITEMDATA, 0, (LPARAM)ic);
959 enum_compressors(GetDlgItem(hdlg, IDC_COMP_LIST), &choose_comp->cv, choose_comp->flags & ICMF_CHOOSE_ALLCOMPRESSORS);
961 SendDlgItemMessageW(hdlg, IDC_COMP_LIST, CB_SETCURSEL, 0, 0);
962 SetFocus(GetDlgItem(hdlg, IDC_COMP_LIST));
964 SetWindowLongPtrW(hdlg, DWLP_USER, (ULONG_PTR)choose_comp);
965 break;
968 case WM_COMMAND:
969 switch (LOWORD(wparam))
971 case IDC_COMP_LIST:
973 INT cur_sel;
974 struct codec_info *ic;
975 BOOL can_configure = FALSE, can_about = FALSE;
976 struct choose_compressor *choose_comp;
978 if (HIWORD(wparam) != CBN_SELCHANGE && HIWORD(wparam) != CBN_SETFOCUS)
979 break;
981 choose_comp = (struct choose_compressor *)GetWindowLongPtrW(hdlg, DWLP_USER);
983 cur_sel = SendMessageW((HWND)lparam, CB_GETCURSEL, 0, 0);
985 ic = (struct codec_info *)SendMessageW((HWND)lparam, CB_GETITEMDATA, cur_sel, 0);
986 if (ic && ic->hic)
988 if (ICQueryConfigure(ic->hic) == DRVCNF_OK)
989 can_configure = TRUE;
990 if (ICQueryAbout(ic->hic) == DRVCNF_OK)
991 can_about = TRUE;
993 EnableWindow(GetDlgItem(hdlg, IDC_CONFIGURE), can_configure);
994 EnableWindow(GetDlgItem(hdlg, IDC_ABOUT), can_about);
996 if (choose_comp->flags & ICMF_CHOOSE_DATARATE)
998 /* FIXME */
1000 if (choose_comp->flags & ICMF_CHOOSE_KEYFRAME)
1002 /* FIXME */
1005 break;
1008 case IDC_CONFIGURE:
1009 case IDC_ABOUT:
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);
1020 ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, cur_sel, 0);
1021 if (ic && ic->hic)
1023 if (LOWORD(wparam) == IDC_CONFIGURE)
1024 ICConfigure(ic->hic, hdlg);
1025 else
1026 ICAbout(ic->hic, hdlg);
1029 break;
1032 case IDOK:
1034 HWND list = GetDlgItem(hdlg, IDC_COMP_LIST);
1035 INT cur_sel;
1036 struct codec_info *ic;
1038 if (HIWORD(wparam) != BN_CLICKED)
1039 break;
1041 cur_sel = SendMessageW(list, CB_GETCURSEL, 0, 0);
1042 ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, cur_sel, 0);
1043 if (ic)
1045 struct choose_compressor *choose_comp = (struct choose_compressor *)GetWindowLongPtrW(hdlg, DWLP_USER);
1047 choose_comp->cv.hic = ic->hic;
1048 choose_comp->cv.fccType = ic->icinfo.fccType;
1049 choose_comp->cv.fccHandler = ic->icinfo.fccHandler;
1050 /* FIXME: fill everything else */
1052 /* prevent closing the codec handle below */
1053 ic->hic = 0;
1056 /* fall through */
1057 case IDCANCEL:
1059 HWND list = GetDlgItem(hdlg, IDC_COMP_LIST);
1060 INT idx = 0;
1062 if (HIWORD(wparam) != BN_CLICKED)
1063 break;
1065 while (1)
1067 struct codec_info *ic;
1069 ic = (struct codec_info *)SendMessageW(list, CB_GETITEMDATA, idx++, 0);
1071 if (!ic || (LONG_PTR)ic == CB_ERR) break;
1073 if (ic->hic) ICClose(ic->hic);
1074 free(ic);
1077 EndDialog(hdlg, LOWORD(wparam) == IDOK);
1078 break;
1081 default:
1082 break;
1084 break;
1086 default:
1087 break;
1090 return FALSE;
1093 /***********************************************************************
1094 * ICCompressorChoose [MSVFW32.@]
1096 BOOL VFWAPI ICCompressorChoose(HWND hwnd, UINT uiFlags, LPVOID pvIn,
1097 LPVOID lpData, PCOMPVARS pc, LPSTR lpszTitle)
1099 struct choose_compressor choose_comp;
1100 BOOL ret;
1102 TRACE("(%p,%08x,%p,%p,%p,%s)\n", hwnd, uiFlags, pvIn, lpData, pc, lpszTitle);
1104 if (!pc || pc->cbSize != sizeof(COMPVARS))
1105 return FALSE;
1107 if (!(pc->dwFlags & ICMF_COMPVARS_VALID))
1109 pc->dwFlags = 0;
1110 pc->fccType = pc->fccHandler = 0;
1111 pc->hic = NULL;
1112 pc->lpbiIn = NULL;
1113 pc->lpbiOut = NULL;
1114 pc->lpBitsOut = pc->lpBitsPrev = pc->lpState = NULL;
1115 pc->lQ = ICQUALITY_DEFAULT;
1116 pc->lKey = -1;
1117 pc->lDataRate = 300; /* kB */
1118 pc->lpState = NULL;
1119 pc->cbState = 0;
1121 if (pc->fccType == 0)
1122 pc->fccType = ICTYPE_VIDEO;
1124 choose_comp.cv = *pc;
1125 choose_comp.flags = uiFlags;
1126 choose_comp.title = lpszTitle;
1128 ret = DialogBoxParamW(MSVFW32_hModule, MAKEINTRESOURCEW(ICM_CHOOSE_COMPRESSOR), hwnd,
1129 icm_choose_compressor_dlgproc, (LPARAM)&choose_comp);
1131 if (ret)
1133 *pc = choose_comp.cv;
1134 pc->dwFlags |= ICMF_COMPVARS_VALID;
1137 return ret;
1141 /***********************************************************************
1142 * ICCompressorFree [MSVFW32.@]
1144 void VFWAPI ICCompressorFree(PCOMPVARS pc)
1146 TRACE("(%p)\n", pc);
1148 if (pc && pc->cbSize == sizeof(COMPVARS))
1150 if (pc->hic)
1152 ICClose(pc->hic);
1153 pc->hic = NULL;
1155 free(pc->lpbiIn);
1156 pc->lpbiIn = NULL;
1157 free(pc->lpBitsOut);
1158 pc->lpBitsOut = NULL;
1159 free(pc->lpBitsPrev);
1160 pc->lpBitsPrev = NULL;
1161 free(pc->lpState);
1162 pc->lpState = NULL;
1163 pc->dwFlags = 0;
1167 /***********************************************************************
1168 * ICSendMessage [MSVFW32.@]
1170 LRESULT VFWAPI ICSendMessage(HIC hic, UINT msg, DWORD_PTR lParam1, DWORD_PTR lParam2)
1172 WINE_HIC* whic = MSVIDEO_GetHicPtr(hic);
1174 if (!whic) return ICERR_BADHANDLE;
1175 return MSVIDEO_SendMessage(whic, msg, lParam1, lParam2);
1178 /***********************************************************************
1179 * ICDrawBegin [MSVFW32.@]
1181 DWORD VFWAPIV ICDrawBegin(
1182 HIC hic, /* [in] */
1183 DWORD dwFlags, /* [in] flags */
1184 HPALETTE hpal, /* [in] palette to draw with */
1185 HWND hwnd, /* [in] window to draw to */
1186 HDC hdc, /* [in] HDC to draw to */
1187 INT xDst, /* [in] destination rectangle */
1188 INT yDst, /* [in] */
1189 INT dxDst, /* [in] */
1190 INT dyDst, /* [in] */
1191 LPBITMAPINFOHEADER lpbi, /* [in] format of frame to draw */
1192 INT xSrc, /* [in] source rectangle */
1193 INT ySrc, /* [in] */
1194 INT dxSrc, /* [in] */
1195 INT dySrc, /* [in] */
1196 DWORD dwRate, /* [in] frames/second = (dwRate/dwScale) */
1197 DWORD dwScale) /* [in] */
1200 ICDRAWBEGIN icdb;
1202 TRACE("(%p,%ld,%p,%p,%p,%u,%u,%u,%u,%p,%u,%u,%u,%u,%ld,%ld)\n",
1203 hic, dwFlags, hpal, hwnd, hdc, xDst, yDst, dxDst, dyDst,
1204 lpbi, xSrc, ySrc, dxSrc, dySrc, dwRate, dwScale);
1206 icdb.dwFlags = dwFlags;
1207 icdb.hpal = hpal;
1208 icdb.hwnd = hwnd;
1209 icdb.hdc = hdc;
1210 icdb.xDst = xDst;
1211 icdb.yDst = yDst;
1212 icdb.dxDst = dxDst;
1213 icdb.dyDst = dyDst;
1214 icdb.lpbi = lpbi;
1215 icdb.xSrc = xSrc;
1216 icdb.ySrc = ySrc;
1217 icdb.dxSrc = dxSrc;
1218 icdb.dySrc = dySrc;
1219 icdb.dwRate = dwRate;
1220 icdb.dwScale = dwScale;
1221 return ICSendMessage(hic,ICM_DRAW_BEGIN,(DWORD_PTR)&icdb,sizeof(icdb));
1224 /***********************************************************************
1225 * ICDraw [MSVFW32.@]
1227 DWORD VFWAPIV ICDraw(HIC hic, DWORD dwFlags, LPVOID lpFormat, LPVOID lpData, DWORD cbData, LONG lTime) {
1228 ICDRAW icd;
1230 TRACE("(%p,%ld,%p,%p,%ld,%ld)\n",hic,dwFlags,lpFormat,lpData,cbData,lTime);
1232 icd.dwFlags = dwFlags;
1233 icd.lpFormat = lpFormat;
1234 icd.lpData = lpData;
1235 icd.cbData = cbData;
1236 icd.lTime = lTime;
1238 return ICSendMessage(hic,ICM_DRAW,(DWORD_PTR)&icd,sizeof(icd));
1241 /***********************************************************************
1242 * ICClose [MSVFW32.@]
1244 LRESULT WINAPI ICClose(HIC hic)
1246 WINE_HIC* whic = MSVIDEO_GetHicPtr(hic);
1247 WINE_HIC** p;
1249 TRACE("(%p)\n",hic);
1251 if (!whic) return ICERR_BADHANDLE;
1253 if (whic->driverproc)
1255 MSVIDEO_SendMessage(whic, DRV_CLOSE, 0, 0);
1256 MSVIDEO_SendMessage(whic, DRV_DISABLE, 0, 0);
1257 MSVIDEO_SendMessage(whic, DRV_FREE, 0, 0);
1259 else
1261 CloseDriver(whic->hdrv, 0, 0);
1264 /* remove whic from list */
1265 for (p = &MSVIDEO_FirstHic; *p != NULL; p = &((*p)->next))
1267 if ((*p) == whic)
1269 *p = whic->next;
1270 break;
1274 free(whic);
1275 return 0;
1280 /***********************************************************************
1281 * ICImageCompress [MSVFW32.@]
1283 HANDLE VFWAPI ICImageCompress(
1284 HIC hic, UINT uiFlags,
1285 LPBITMAPINFO lpbiIn, LPVOID lpBits,
1286 LPBITMAPINFO lpbiOut, LONG lQuality,
1287 LONG* plSize)
1289 FIXME("(%p,%08x,%p,%p,%p,%ld,%p)\n",
1290 hic, uiFlags, lpbiIn, lpBits, lpbiOut, lQuality, plSize);
1292 return NULL;
1295 /***********************************************************************
1296 * ICImageDecompress [MSVFW32.@]
1299 HANDLE VFWAPI ICImageDecompress(
1300 HIC hic, UINT uiFlags, LPBITMAPINFO lpbiIn,
1301 LPVOID lpBits, LPBITMAPINFO lpbiOut)
1303 HGLOBAL hMem = NULL;
1304 BYTE* pMem = NULL;
1305 BOOL bReleaseIC = FALSE;
1306 BYTE* pHdr = NULL;
1307 ULONG cbHdr = 0;
1308 BOOL bSucceeded = FALSE;
1309 BOOL bInDecompress = FALSE;
1310 DWORD biSizeImage;
1312 TRACE("(%p,%08x,%p,%p,%p)\n",
1313 hic, uiFlags, lpbiIn, lpBits, lpbiOut);
1315 if ( hic == NULL )
1317 hic = ICDecompressOpen( ICTYPE_VIDEO, 0, &lpbiIn->bmiHeader, (lpbiOut != NULL) ? &lpbiOut->bmiHeader : NULL );
1318 if ( hic == NULL )
1320 WARN("no handler\n" );
1321 goto err;
1323 bReleaseIC = TRUE;
1325 if ( uiFlags != 0 )
1327 FIXME( "unknown flag %08x\n", uiFlags );
1328 goto err;
1330 if ( lpbiIn == NULL || lpBits == NULL )
1332 WARN("invalid argument\n");
1333 goto err;
1336 if ( lpbiOut != NULL )
1338 if ( lpbiOut->bmiHeader.biSize != sizeof(BITMAPINFOHEADER) )
1339 goto err;
1340 cbHdr = sizeof(BITMAPINFOHEADER);
1341 if ( lpbiOut->bmiHeader.biCompression == 3 )
1342 cbHdr += sizeof(DWORD)*3;
1343 else
1344 if ( lpbiOut->bmiHeader.biBitCount <= 8 )
1346 if ( lpbiOut->bmiHeader.biClrUsed == 0 )
1347 cbHdr += sizeof(RGBQUAD) * (1<<lpbiOut->bmiHeader.biBitCount);
1348 else
1349 cbHdr += sizeof(RGBQUAD) * lpbiOut->bmiHeader.biClrUsed;
1352 else
1354 TRACE( "get format\n" );
1356 cbHdr = ICDecompressGetFormatSize(hic,lpbiIn);
1357 if ( cbHdr < sizeof(BITMAPINFOHEADER) )
1358 goto err;
1359 if (!(pHdr = calloc(1, cbHdr + sizeof(RGBQUAD) * 256)))
1360 goto err;
1361 if ( ICDecompressGetFormat( hic, lpbiIn, pHdr ) != ICERR_OK )
1362 goto err;
1363 lpbiOut = (BITMAPINFO*)pHdr;
1364 if ( lpbiOut->bmiHeader.biBitCount <= 8 &&
1365 ICDecompressGetPalette( hic, lpbiIn, lpbiOut ) != ICERR_OK &&
1366 lpbiIn->bmiHeader.biBitCount == lpbiOut->bmiHeader.biBitCount )
1368 if ( lpbiIn->bmiHeader.biClrUsed == 0 )
1369 memcpy( lpbiOut->bmiColors, lpbiIn->bmiColors, sizeof(RGBQUAD)*(1<<lpbiOut->bmiHeader.biBitCount) );
1370 else
1371 memcpy( lpbiOut->bmiColors, lpbiIn->bmiColors, sizeof(RGBQUAD)*lpbiIn->bmiHeader.biClrUsed );
1373 if ( lpbiOut->bmiHeader.biBitCount <= 8 &&
1374 lpbiOut->bmiHeader.biClrUsed == 0 )
1375 lpbiOut->bmiHeader.biClrUsed = 1<<lpbiOut->bmiHeader.biBitCount;
1377 lpbiOut->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1378 cbHdr = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*lpbiOut->bmiHeader.biClrUsed;
1381 biSizeImage = lpbiOut->bmiHeader.biSizeImage;
1382 if ( biSizeImage == 0 )
1383 biSizeImage = get_size_image(lpbiOut->bmiHeader.biWidth, lpbiOut->bmiHeader.biHeight, lpbiOut->bmiHeader.biBitCount);
1385 TRACE( "call ICDecompressBegin\n" );
1387 if ( ICDecompressBegin( hic, lpbiIn, lpbiOut ) != ICERR_OK )
1388 goto err;
1389 bInDecompress = TRUE;
1391 TRACE( "cbHdr %ld, biSizeImage %ld\n", cbHdr, biSizeImage );
1393 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_ZEROINIT, cbHdr + biSizeImage );
1394 if ( hMem == NULL )
1396 WARN( "out of memory\n" );
1397 goto err;
1399 pMem = GlobalLock( hMem );
1400 if ( pMem == NULL )
1401 goto err;
1402 memcpy( pMem, lpbiOut, cbHdr );
1404 TRACE( "call ICDecompress\n" );
1405 if ( ICDecompress( hic, 0, &lpbiIn->bmiHeader, lpBits, &lpbiOut->bmiHeader, pMem+cbHdr ) != ICERR_OK )
1406 goto err;
1408 bSucceeded = TRUE;
1409 err:
1410 if ( bInDecompress )
1411 ICDecompressEnd( hic );
1412 if ( bReleaseIC )
1413 ICClose(hic);
1414 free(pHdr);
1415 if ( pMem != NULL )
1416 GlobalUnlock( hMem );
1417 if ( !bSucceeded && hMem != NULL )
1419 GlobalFree(hMem); hMem = NULL;
1422 return hMem;
1425 /***********************************************************************
1426 * ICSeqCompressFrame [MSVFW32.@]
1428 LPVOID VFWAPI ICSeqCompressFrame(PCOMPVARS pc, UINT uiFlags, LPVOID lpBits, BOOL *pfKey, LONG *plSize)
1430 ICCOMPRESS* icComp = pc->lpState;
1431 DWORD ret;
1432 TRACE("(%p, 0x%08x, %p, %p, %p)\n", pc, uiFlags, lpBits, pfKey, plSize);
1434 if (pc->cbState != sizeof(ICCOMPRESS))
1436 ERR("Invalid cbState %li\n", pc->cbState);
1437 return NULL;
1440 if (!pc->lKeyCount++)
1441 icComp->dwFlags = ICCOMPRESS_KEYFRAME;
1442 else
1444 if (pc->lKey && pc->lKeyCount == (pc->lKey - 1))
1445 /* No key frames if pc->lKey == 0 */
1446 pc->lKeyCount = 0;
1447 icComp->dwFlags = 0;
1450 icComp->lpInput = lpBits;
1451 icComp->lFrameNum = pc->lFrame++;
1452 icComp->lpOutput = pc->lpBitsOut;
1453 icComp->lpPrev = pc->lpBitsPrev;
1454 ret = ICSendMessage(pc->hic, ICM_COMPRESS, (DWORD_PTR)icComp, sizeof(*icComp));
1456 if (ret == ICERR_OK)
1458 LPVOID oldprev, oldout;
1460 if (icComp->dwFlags & AVIIF_KEYFRAME)
1462 pc->lKeyCount = 1;
1463 *pfKey = TRUE;
1464 TRACE("Key frame\n");
1466 else
1467 *pfKey = FALSE;
1469 *plSize = icComp->lpbiOutput->biSizeImage;
1471 /* We shift Prev and Out, so we don't have to allocate and release memory */
1472 oldprev = pc->lpBitsPrev;
1473 oldout = pc->lpBitsOut;
1474 pc->lpBitsPrev = oldout;
1475 pc->lpBitsOut = oldprev;
1477 TRACE("returning: %p, compressed frame size %lu\n", icComp->lpOutput, *plSize);
1478 return icComp->lpOutput;
1480 return NULL;
1483 static void clear_compvars(PCOMPVARS pc)
1485 free(pc->lpbiIn);
1486 free(pc->lpBitsPrev);
1487 free(pc->lpBitsOut);
1488 free(pc->lpState);
1489 pc->lpbiIn = pc->lpBitsPrev = pc->lpBitsOut = pc->lpState = NULL;
1490 if (pc->dwFlags & 0x80000000)
1492 free(pc->lpbiOut);
1493 pc->lpbiOut = NULL;
1494 pc->dwFlags &= ~0x80000000;
1498 /***********************************************************************
1499 * ICSeqCompressFrameEnd [MSVFW32.@]
1501 void VFWAPI ICSeqCompressFrameEnd(PCOMPVARS pc)
1503 TRACE("(%p)\n", pc);
1504 ICSendMessage(pc->hic, ICM_COMPRESS_END, 0, 0);
1505 clear_compvars(pc);
1508 static BITMAPINFO *copy_bitmapinfo(const BITMAPINFO *src)
1510 int num_colors;
1511 unsigned int size;
1512 BITMAPINFO *dst;
1514 if (src->bmiHeader.biClrUsed)
1515 num_colors = min(src->bmiHeader.biClrUsed, 256);
1516 else
1517 num_colors = src->bmiHeader.biBitCount > 8 ? 0 : 1 << src->bmiHeader.biBitCount;
1519 size = FIELD_OFFSET(BITMAPINFO, bmiColors[num_colors]);
1520 if (src->bmiHeader.biCompression == BI_BITFIELDS)
1521 size += 3 * sizeof(DWORD);
1523 if (!(dst = malloc(size)))
1524 return NULL;
1526 memcpy(dst, src, size);
1527 return dst;
1530 /***********************************************************************
1531 * ICSeqCompressFrameStart [MSVFW32.@]
1533 BOOL VFWAPI ICSeqCompressFrameStart(PCOMPVARS pc, LPBITMAPINFO lpbiIn)
1535 /* I'm ignoring bmiColors as I don't know what to do with it,
1536 * it doesn't appear to be used though
1538 DWORD ret;
1539 ICCOMPRESS* icComp;
1541 if (!(pc->lpbiIn = copy_bitmapinfo(lpbiIn)))
1542 return FALSE;
1544 if (!(pc->lpState = malloc(sizeof(ICCOMPRESS) + sizeof(*icComp->lpckid) + sizeof(*icComp->lpdwFlags))))
1545 goto error;
1547 pc->cbState = sizeof(ICCOMPRESS);
1549 if (!pc->lpbiOut)
1551 /* Ask compressor for needed header size */
1552 int size = ICSendMessage(pc->hic, ICM_COMPRESS_GET_FORMAT,
1553 (DWORD_PTR)pc->lpbiIn, 0);
1554 if (size <= 0)
1555 goto error;
1557 if (!(pc->lpbiOut = calloc(1, size)))
1558 goto error;
1559 /* Flag to show that we allocated lpbiOut for proper cleanup */
1560 pc->dwFlags |= 0x80000000;
1562 ret = ICSendMessage(pc->hic, ICM_COMPRESS_GET_FORMAT,
1563 (DWORD_PTR)pc->lpbiIn, (DWORD_PTR)pc->lpbiOut);
1564 if (ret != ICERR_OK)
1566 ERR("Could not get output format from compressor\n");
1567 goto error;
1569 if (!pc->lpbiOut->bmiHeader.biSizeImage)
1571 /* If we can't know the output frame size for sure at least allocate
1572 * the same size of the input frame and also at least 8Kb to be sure
1573 * that poor compressors will have enough memory to work if the input
1574 * frame is too small.
1576 pc->lpbiOut->bmiHeader.biSizeImage = max(8192, pc->lpbiIn->bmiHeader.biSizeImage);
1577 ERR("Bad codec! Invalid output frame size, guessing from input\n");
1581 TRACE("Input: %lux%lu, fcc %s, bpp %u, size %lu\n",
1582 pc->lpbiIn->bmiHeader.biWidth, pc->lpbiIn->bmiHeader.biHeight,
1583 wine_dbgstr_fcc(pc->lpbiIn->bmiHeader.biCompression),
1584 pc->lpbiIn->bmiHeader.biBitCount,
1585 pc->lpbiIn->bmiHeader.biSizeImage);
1586 TRACE("Output: %lux%lu, fcc %s, bpp %u, size %lu\n",
1587 pc->lpbiOut->bmiHeader.biWidth, pc->lpbiOut->bmiHeader.biHeight,
1588 wine_dbgstr_fcc(pc->lpbiOut->bmiHeader.biCompression),
1589 pc->lpbiOut->bmiHeader.biBitCount,
1590 pc->lpbiOut->bmiHeader.biSizeImage);
1592 /* Buffer for compressed frame data */
1593 if (!(pc->lpBitsOut = malloc(pc->lpbiOut->bmiHeader.biSizeImage)))
1594 goto error;
1596 /* Buffer for previous compressed frame data */
1597 if (!(pc->lpBitsPrev = malloc(pc->lpbiOut->bmiHeader.biSizeImage)))
1598 goto error;
1600 TRACE("Compvars:\n"
1601 "\tsize: %lu\n"
1602 "\tflags: 0x%lx\n"
1603 "\thic: %p\n"
1604 "\ttype: %s\n"
1605 "\thandler: %s\n"
1606 "\tin/out: %p/%p\n"
1607 "\tkey/data/quality: %li/%li/%li\n",
1608 pc->cbSize, pc->dwFlags, pc->hic, wine_dbgstr_fcc(pc->fccType),
1609 wine_dbgstr_fcc(pc->fccHandler), pc->lpbiIn, pc->lpbiOut, pc->lKey,
1610 pc->lDataRate, pc->lQ);
1612 ret = ICSendMessage(pc->hic, ICM_COMPRESS_BEGIN, (DWORD_PTR)pc->lpbiIn, (DWORD_PTR)pc->lpbiOut);
1613 if (ret == ICERR_OK)
1615 icComp = pc->lpState;
1616 /* Initialise some variables */
1617 pc->lFrame = 0; pc->lKeyCount = 0;
1619 icComp->lpbiOutput = &pc->lpbiOut->bmiHeader;
1620 icComp->lpbiInput = &pc->lpbiIn->bmiHeader;
1621 icComp->lpckid = (DWORD *)(icComp + 1);
1622 *icComp->lpckid = 0;
1623 icComp->lpdwFlags = (DWORD *)((char *)(icComp + 1) + sizeof(*icComp->lpckid));
1624 *icComp->lpdwFlags = 0;
1625 icComp->dwFrameSize = 0;
1626 icComp->dwQuality = pc->lQ;
1627 icComp->lpbiPrev = &pc->lpbiIn->bmiHeader;
1628 return TRUE;
1630 error:
1631 clear_compvars(pc);
1632 return FALSE;
1635 /***********************************************************************
1636 * GetFileNamePreview [MSVFW32.@]
1638 static BOOL GetFileNamePreview(LPVOID lpofn,BOOL bSave,BOOL bUnicode)
1640 CHAR szFunctionName[20];
1641 BOOL (*fnGetFileName)(LPVOID);
1642 HMODULE hComdlg32;
1643 BOOL ret;
1645 FIXME("(%p,%d,%d), semi-stub!\n",lpofn,bSave,bUnicode);
1647 lstrcpyA(szFunctionName, (bSave ? "GetSaveFileName" : "GetOpenFileName"));
1648 lstrcatA(szFunctionName, (bUnicode ? "W" : "A"));
1650 hComdlg32 = LoadLibraryA("COMDLG32.DLL");
1651 if (hComdlg32 == NULL)
1652 return FALSE;
1654 fnGetFileName = (LPVOID)GetProcAddress(hComdlg32, szFunctionName);
1655 if (fnGetFileName == NULL)
1657 FreeLibrary(hComdlg32);
1658 return FALSE;
1661 /* FIXME: need to add OFN_ENABLEHOOK and our own handler */
1662 ret = fnGetFileName(lpofn);
1664 FreeLibrary(hComdlg32);
1665 return ret;
1668 /***********************************************************************
1669 * GetOpenFileNamePreviewA [MSVFW32.@]
1671 BOOL WINAPI GetOpenFileNamePreviewA(LPOPENFILENAMEA lpofn)
1673 FIXME("(%p), semi-stub!\n", lpofn);
1675 return GetFileNamePreview(lpofn, FALSE, FALSE);
1678 /***********************************************************************
1679 * GetOpenFileNamePreviewW [MSVFW32.@]
1681 BOOL WINAPI GetOpenFileNamePreviewW(LPOPENFILENAMEW lpofn)
1683 FIXME("(%p), semi-stub!\n", lpofn);
1685 return GetFileNamePreview(lpofn, FALSE, TRUE);
1688 /***********************************************************************
1689 * GetSaveFileNamePreviewA [MSVFW32.@]
1691 BOOL WINAPI GetSaveFileNamePreviewA(LPOPENFILENAMEA lpofn)
1693 FIXME("(%p), semi-stub!\n", lpofn);
1695 return GetFileNamePreview(lpofn, TRUE, FALSE);
1698 /***********************************************************************
1699 * GetSaveFileNamePreviewW [MSVFW32.@]
1701 BOOL WINAPI GetSaveFileNamePreviewW(LPOPENFILENAMEW lpofn)
1703 FIXME("(%p), semi-stub!\n", lpofn);
1705 return GetFileNamePreview(lpofn, TRUE, TRUE);