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