Hopefully create a branch and not destroy the svn repository.
[reactos.git] / dll / win32 / avifil32 / api.c
1 /*
2 * Copyright 1999 Marcus Meissner
3 * Copyright 2002-2003 Michael Günnewig
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include <stdarg.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
31
32 #include "ole2.h"
33 #include "shellapi.h"
34 #include "shlobj.h"
35 #include "vfw.h"
36 #include "msacm.h"
37
38 #include "avifile_private.h"
39
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
44
45
46 /***********************************************************************
47 * for AVIBuildFilterW -- uses fixed size table
48 */
49 #define MAX_FILTERS 30 /* 30 => 7kB */
50
51 typedef struct _AVIFilter {
52 WCHAR szClsid[40];
53 WCHAR szExtensions[MAX_FILTERS * 7];
54 } AVIFilter;
55
56 /***********************************************************************
57 * for AVISaveOptions
58 */
59 static struct {
60 UINT uFlags;
61 INT nStreams;
62 PAVISTREAM *ppavis;
63 LPAVICOMPRESSOPTIONS *ppOptions;
64 INT nCurrent;
65 } SaveOpts;
66
67 /***********************************************************************
68 * copied from dlls/ole32/compobj.c
69 */
70 static HRESULT AVIFILE_CLSIDFromString(LPCSTR idstr, LPCLSID id)
71 {
72 BYTE const *s;
73 BYTE *p;
74 INT i;
75 BYTE table[256];
76
77 if (!idstr) {
78 memset(id, 0, sizeof(CLSID));
79 return S_OK;
80 }
81
82 /* validate the CLSID string */
83 if (lstrlenA(idstr) != 38)
84 return CO_E_CLASSSTRING;
85
86 s = (BYTE const*)idstr;
87 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') ||
88 (s[24]!='-') || (s[37]!='}'))
89 return CO_E_CLASSSTRING;
90
91 for (i = 1; i < 37; i++) {
92 if ((i == 9) || (i == 14) || (i == 19) || (i == 24))
93 continue;
94 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
95 ((s[i] >= 'a') && (s[i] <= 'f')) ||
96 ((s[i] >= 'A') && (s[i] <= 'F')))
97 )
98 return CO_E_CLASSSTRING;
99 }
100
101 TRACE("%s -> %p\n", s, id);
102
103 /* quick lookup table */
104 memset(table, 0, 256);
105
106 for (i = 0; i < 10; i++)
107 table['0' + i] = i;
108
109 for (i = 0; i < 6; i++) {
110 table['A' + i] = i+10;
111 table['a' + i] = i+10;
112 }
113
114 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
115 p = (BYTE *) id;
116
117 s++; /* skip leading brace */
118 for (i = 0; i < 4; i++) {
119 p[3 - i] = table[*s]<<4 | table[*(s+1)];
120 s += 2;
121 }
122 p += 4;
123 s++; /* skip - */
124
125 for (i = 0; i < 2; i++) {
126 p[1-i] = table[*s]<<4 | table[*(s+1)];
127 s += 2;
128 }
129 p += 2;
130 s++; /* skip - */
131
132 for (i = 0; i < 2; i++) {
133 p[1-i] = table[*s]<<4 | table[*(s+1)];
134 s += 2;
135 }
136 p += 2;
137 s++; /* skip - */
138
139 /* these are just sequential bytes */
140 for (i = 0; i < 2; i++) {
141 *p++ = table[*s]<<4 | table[*(s+1)];
142 s += 2;
143 }
144 s++; /* skip - */
145
146 for (i = 0; i < 6; i++) {
147 *p++ = table[*s]<<4 | table[*(s+1)];
148 s += 2;
149 }
150
151 return S_OK;
152 }
153
154 static BOOL AVIFILE_GetFileHandlerByExtension(LPCWSTR szFile, LPCLSID lpclsid)
155 {
156 CHAR szRegKey[25];
157 CHAR szValue[100];
158 LPWSTR szExt = strrchrW(szFile, '.');
159 LONG len = sizeof(szValue) / sizeof(szValue[0]);
160
161 if (szExt == NULL)
162 return FALSE;
163
164 szExt++;
165
166 wsprintfA(szRegKey, "AVIFile\\Extensions\\%.3ls", szExt);
167 if (RegQueryValueA(HKEY_CLASSES_ROOT, szRegKey, szValue, &len) != ERROR_SUCCESS)
168 return FALSE;
169
170 return (AVIFILE_CLSIDFromString(szValue, lpclsid) == S_OK);
171 }
172
173 /***********************************************************************
174 * AVIFileInit (AVIFIL32.@)
175 */
176 void WINAPI AVIFileInit(void) {
177 OleInitialize(NULL);
178 }
179
180 /***********************************************************************
181 * AVIFileExit (AVIFIL32.@)
182 */
183 void WINAPI AVIFileExit(void) {
184 /* need to free ole32.dll if we are the last exit call */
185 /* OleUninitialize() */
186 FIXME("(): stub!\n");
187 }
188
189 /***********************************************************************
190 * AVIFileOpen (AVIFIL32.@)
191 * AVIFileOpenA (AVIFIL32.@)
192 */
193 HRESULT WINAPI AVIFileOpenA(PAVIFILE *ppfile, LPCSTR szFile, UINT uMode,
194 LPCLSID lpHandler)
195 {
196 LPWSTR wszFile = NULL;
197 HRESULT hr;
198 int len;
199
200 TRACE("(%p,%s,0x%08X,%s)\n", ppfile, debugstr_a(szFile), uMode,
201 debugstr_guid(lpHandler));
202
203 /* check parameters */
204 if (ppfile == NULL || szFile == NULL)
205 return AVIERR_BADPARAM;
206
207 /* convert ASCII string to Unicode and call unicode function */
208 len = MultiByteToWideChar(CP_ACP, 0, szFile, -1, NULL, 0);
209 if (len <= 0)
210 return AVIERR_BADPARAM;
211
212 wszFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
213 if (wszFile == NULL)
214 return AVIERR_MEMORY;
215
216 MultiByteToWideChar(CP_ACP, 0, szFile, -1, wszFile, len);
217
218 hr = AVIFileOpenW(ppfile, wszFile, uMode, lpHandler);
219
220 HeapFree(GetProcessHeap(), 0, wszFile);
221
222 return hr;
223 }
224
225 /***********************************************************************
226 * AVIFileOpenW (AVIFIL32.@)
227 */
228 HRESULT WINAPI AVIFileOpenW(PAVIFILE *ppfile, LPCWSTR szFile, UINT uMode,
229 LPCLSID lpHandler)
230 {
231 IPersistFile *ppersist = NULL;
232 CLSID clsidHandler;
233 HRESULT hr;
234
235 TRACE("(%p,%s,0x%X,%s)\n", ppfile, debugstr_w(szFile), uMode,
236 debugstr_guid(lpHandler));
237
238 /* check parameters */
239 if (ppfile == NULL || szFile == NULL)
240 return AVIERR_BADPARAM;
241
242 *ppfile = NULL;
243
244 /* if no handler then try guessing it by extension */
245 if (lpHandler == NULL) {
246 if (! AVIFILE_GetFileHandlerByExtension(szFile, &clsidHandler))
247 return AVIERR_UNSUPPORTED;
248 } else
249 clsidHandler = *lpHandler;
250
251 /* create instance of handler */
252 hr = CoCreateInstance(&clsidHandler, NULL, CLSCTX_INPROC, &IID_IAVIFile, (LPVOID*)ppfile);
253 if (FAILED(hr) || *ppfile == NULL)
254 return hr;
255
256 /* ask for IPersistFile interface for loading/creating the file */
257 hr = IAVIFile_QueryInterface(*ppfile, &IID_IPersistFile, (LPVOID*)&ppersist);
258 if (FAILED(hr) || ppersist == NULL) {
259 IAVIFile_Release(*ppfile);
260 *ppfile = NULL;
261 return hr;
262 }
263
264 hr = IPersistFile_Load(ppersist, szFile, uMode);
265 IPersistFile_Release(ppersist);
266 if (FAILED(hr)) {
267 IAVIFile_Release(*ppfile);
268 *ppfile = NULL;
269 }
270
271 return hr;
272 }
273
274 /***********************************************************************
275 * AVIFileAddRef (AVIFIL32.@)
276 */
277 ULONG WINAPI AVIFileAddRef(PAVIFILE pfile)
278 {
279 TRACE("(%p)\n", pfile);
280
281 if (pfile == NULL) {
282 ERR(": bad handle passed!\n");
283 return 0;
284 }
285
286 return IAVIFile_AddRef(pfile);
287 }
288
289 /***********************************************************************
290 * AVIFileRelease (AVIFIL32.@)
291 */
292 ULONG WINAPI AVIFileRelease(PAVIFILE pfile)
293 {
294 TRACE("(%p)\n", pfile);
295
296 if (pfile == NULL) {
297 ERR(": bad handle passed!\n");
298 return 0;
299 }
300
301 return IAVIFile_Release(pfile);
302 }
303
304 /***********************************************************************
305 * AVIFileInfo (AVIFIL32.@)
306 * AVIFileInfoA (AVIFIL32.@)
307 */
308 HRESULT WINAPI AVIFileInfoA(PAVIFILE pfile, LPAVIFILEINFOA afi, LONG size)
309 {
310 AVIFILEINFOW afiw;
311 HRESULT hres;
312
313 TRACE("(%p,%p,%d)\n", pfile, afi, size);
314
315 if (pfile == NULL)
316 return AVIERR_BADHANDLE;
317 if ((DWORD)size < sizeof(AVIFILEINFOA))
318 return AVIERR_BADSIZE;
319
320 hres = IAVIFile_Info(pfile, &afiw, sizeof(afiw));
321
322 memcpy(afi, &afiw, sizeof(*afi) - sizeof(afi->szFileType));
323 WideCharToMultiByte(CP_ACP, 0, afiw.szFileType, -1, afi->szFileType,
324 sizeof(afi->szFileType), NULL, NULL);
325 afi->szFileType[sizeof(afi->szFileType) - 1] = 0;
326
327 return hres;
328 }
329
330 /***********************************************************************
331 * AVIFileInfoW (AVIFIL32.@)
332 */
333 HRESULT WINAPI AVIFileInfoW(PAVIFILE pfile, LPAVIFILEINFOW afiw, LONG size)
334 {
335 TRACE("(%p,%p,%d)\n", pfile, afiw, size);
336
337 if (pfile == NULL)
338 return AVIERR_BADHANDLE;
339
340 return IAVIFile_Info(pfile, afiw, size);
341 }
342
343 /***********************************************************************
344 * AVIFileGetStream (AVIFIL32.@)
345 */
346 HRESULT WINAPI AVIFileGetStream(PAVIFILE pfile, PAVISTREAM *avis,
347 DWORD fccType, LONG lParam)
348 {
349 TRACE("(%p,%p,'%4.4s',%d)\n", pfile, avis, (char*)&fccType, lParam);
350
351 if (pfile == NULL)
352 return AVIERR_BADHANDLE;
353
354 return IAVIFile_GetStream(pfile, avis, fccType, lParam);
355 }
356
357 /***********************************************************************
358 * AVIFileCreateStream (AVIFIL32.@)
359 * AVIFileCreateStreamA (AVIFIL32.@)
360 */
361 HRESULT WINAPI AVIFileCreateStreamA(PAVIFILE pfile, PAVISTREAM *ppavi,
362 LPAVISTREAMINFOA psi)
363 {
364 AVISTREAMINFOW psiw;
365
366 TRACE("(%p,%p,%p)\n", pfile, ppavi, psi);
367
368 if (pfile == NULL)
369 return AVIERR_BADHANDLE;
370
371 /* Only the szName at the end is different */
372 memcpy(&psiw, psi, sizeof(*psi) - sizeof(psi->szName));
373 MultiByteToWideChar(CP_ACP, 0, psi->szName, -1, psiw.szName,
374 sizeof(psiw.szName) / sizeof(psiw.szName[0]));
375
376 return IAVIFile_CreateStream(pfile, ppavi, &psiw);
377 }
378
379 /***********************************************************************
380 * AVIFileCreateStreamW (AVIFIL32.@)
381 */
382 HRESULT WINAPI AVIFileCreateStreamW(PAVIFILE pfile, PAVISTREAM *avis,
383 LPAVISTREAMINFOW asi)
384 {
385 TRACE("(%p,%p,%p)\n", pfile, avis, asi);
386
387 if (pfile == NULL)
388 return AVIERR_BADHANDLE;
389
390 return IAVIFile_CreateStream(pfile, avis, asi);
391 }
392
393 /***********************************************************************
394 * AVIFileWriteData (AVIFIL32.@)
395 */
396 HRESULT WINAPI AVIFileWriteData(PAVIFILE pfile,DWORD fcc,LPVOID lp,LONG size)
397 {
398 TRACE("(%p,'%4.4s',%p,%d)\n", pfile, (char*)&fcc, lp, size);
399
400 if (pfile == NULL)
401 return AVIERR_BADHANDLE;
402
403 return IAVIFile_WriteData(pfile, fcc, lp, size);
404 }
405
406 /***********************************************************************
407 * AVIFileReadData (AVIFIL32.@)
408 */
409 HRESULT WINAPI AVIFileReadData(PAVIFILE pfile,DWORD fcc,LPVOID lp,LPLONG size)
410 {
411 TRACE("(%p,'%4.4s',%p,%p)\n", pfile, (char*)&fcc, lp, size);
412
413 if (pfile == NULL)
414 return AVIERR_BADHANDLE;
415
416 return IAVIFile_ReadData(pfile, fcc, lp, size);
417 }
418
419 /***********************************************************************
420 * AVIFileEndRecord (AVIFIL32.@)
421 */
422 HRESULT WINAPI AVIFileEndRecord(PAVIFILE pfile)
423 {
424 TRACE("(%p)\n", pfile);
425
426 if (pfile == NULL)
427 return AVIERR_BADHANDLE;
428
429 return IAVIFile_EndRecord(pfile);
430 }
431
432 /***********************************************************************
433 * AVIStreamAddRef (AVIFIL32.@)
434 */
435 ULONG WINAPI AVIStreamAddRef(PAVISTREAM pstream)
436 {
437 TRACE("(%p)\n", pstream);
438
439 if (pstream == NULL) {
440 ERR(": bad handle passed!\n");
441 return 0;
442 }
443
444 return IAVIStream_AddRef(pstream);
445 }
446
447 /***********************************************************************
448 * AVIStreamRelease (AVIFIL32.@)
449 */
450 ULONG WINAPI AVIStreamRelease(PAVISTREAM pstream)
451 {
452 TRACE("(%p)\n", pstream);
453
454 if (pstream == NULL) {
455 ERR(": bad handle passed!\n");
456 return 0;
457 }
458
459 return IAVIStream_Release(pstream);
460 }
461
462 /***********************************************************************
463 * AVIStreamCreate (AVIFIL32.@)
464 */
465 HRESULT WINAPI AVIStreamCreate(PAVISTREAM *ppavi, LONG lParam1, LONG lParam2,
466 LPCLSID pclsidHandler)
467 {
468 HRESULT hr;
469
470 TRACE("(%p,0x%08X,0x%08X,%s)\n", ppavi, lParam1, lParam2,
471 debugstr_guid(pclsidHandler));
472
473 if (ppavi == NULL)
474 return AVIERR_BADPARAM;
475
476 *ppavi = NULL;
477 if (pclsidHandler == NULL)
478 return AVIERR_UNSUPPORTED;
479
480 hr = CoCreateInstance(pclsidHandler, NULL, CLSCTX_INPROC, &IID_IAVIStream, (LPVOID*)ppavi);
481 if (FAILED(hr) || *ppavi == NULL)
482 return hr;
483
484 hr = IAVIStream_Create(*ppavi, lParam1, lParam2);
485 if (FAILED(hr)) {
486 IAVIStream_Release(*ppavi);
487 *ppavi = NULL;
488 }
489
490 return hr;
491 }
492
493 /***********************************************************************
494 * AVIStreamInfo (AVIFIL32.@)
495 * AVIStreamInfoA (AVIFIL32.@)
496 */
497 HRESULT WINAPI AVIStreamInfoA(PAVISTREAM pstream, LPAVISTREAMINFOA asi,
498 LONG size)
499 {
500 AVISTREAMINFOW asiw;
501 HRESULT hres;
502
503 TRACE("(%p,%p,%d)\n", pstream, asi, size);
504
505 if (pstream == NULL)
506 return AVIERR_BADHANDLE;
507 if ((DWORD)size < sizeof(AVISTREAMINFOA))
508 return AVIERR_BADSIZE;
509
510 hres = IAVIStream_Info(pstream, &asiw, sizeof(asiw));
511
512 memcpy(asi, &asiw, sizeof(asiw) - sizeof(asiw.szName));
513 WideCharToMultiByte(CP_ACP, 0, asiw.szName, -1, asi->szName,
514 sizeof(asi->szName), NULL, NULL);
515 asi->szName[sizeof(asi->szName) - 1] = 0;
516
517 return hres;
518 }
519
520 /***********************************************************************
521 * AVIStreamInfoW (AVIFIL32.@)
522 */
523 HRESULT WINAPI AVIStreamInfoW(PAVISTREAM pstream, LPAVISTREAMINFOW asi,
524 LONG size)
525 {
526 TRACE("(%p,%p,%d)\n", pstream, asi, size);
527
528 if (pstream == NULL)
529 return AVIERR_BADHANDLE;
530
531 return IAVIStream_Info(pstream, asi, size);
532 }
533
534 /***********************************************************************
535 * AVIStreamFindSample (AVIFIL32.@)
536 */
537 LONG WINAPI AVIStreamFindSample(PAVISTREAM pstream, LONG pos, LONG flags)
538 {
539 TRACE("(%p,%d,0x%X)\n", pstream, pos, flags);
540
541 if (pstream == NULL)
542 return -1;
543
544 return IAVIStream_FindSample(pstream, pos, flags);
545 }
546
547 /***********************************************************************
548 * AVIStreamReadFormat (AVIFIL32.@)
549 */
550 HRESULT WINAPI AVIStreamReadFormat(PAVISTREAM pstream, LONG pos,
551 LPVOID format, LPLONG formatsize)
552 {
553 TRACE("(%p,%d,%p,%p)\n", pstream, pos, format, formatsize);
554
555 if (pstream == NULL)
556 return AVIERR_BADHANDLE;
557
558 return IAVIStream_ReadFormat(pstream, pos, format, formatsize);
559 }
560
561 /***********************************************************************
562 * AVIStreamSetFormat (AVIFIL32.@)
563 */
564 HRESULT WINAPI AVIStreamSetFormat(PAVISTREAM pstream, LONG pos,
565 LPVOID format, LONG formatsize)
566 {
567 TRACE("(%p,%d,%p,%d)\n", pstream, pos, format, formatsize);
568
569 if (pstream == NULL)
570 return AVIERR_BADHANDLE;
571
572 return IAVIStream_SetFormat(pstream, pos, format, formatsize);
573 }
574
575 /***********************************************************************
576 * AVIStreamRead (AVIFIL32.@)
577 */
578 HRESULT WINAPI AVIStreamRead(PAVISTREAM pstream, LONG start, LONG samples,
579 LPVOID buffer, LONG buffersize,
580 LPLONG bytesread, LPLONG samplesread)
581 {
582 TRACE("(%p,%d,%d,%p,%d,%p,%p)\n", pstream, start, samples, buffer,
583 buffersize, bytesread, samplesread);
584
585 if (pstream == NULL)
586 return AVIERR_BADHANDLE;
587
588 return IAVIStream_Read(pstream, start, samples, buffer, buffersize,
589 bytesread, samplesread);
590 }
591
592 /***********************************************************************
593 * AVIStreamWrite (AVIFIL32.@)
594 */
595 HRESULT WINAPI AVIStreamWrite(PAVISTREAM pstream, LONG start, LONG samples,
596 LPVOID buffer, LONG buffersize, DWORD flags,
597 LPLONG sampwritten, LPLONG byteswritten)
598 {
599 TRACE("(%p,%d,%d,%p,%d,0x%X,%p,%p)\n", pstream, start, samples, buffer,
600 buffersize, flags, sampwritten, byteswritten);
601
602 if (pstream == NULL)
603 return AVIERR_BADHANDLE;
604
605 return IAVIStream_Write(pstream, start, samples, buffer, buffersize,
606 flags, sampwritten, byteswritten);
607 }
608
609 /***********************************************************************
610 * AVIStreamReadData (AVIFIL32.@)
611 */
612 HRESULT WINAPI AVIStreamReadData(PAVISTREAM pstream, DWORD fcc, LPVOID lp,
613 LPLONG lpread)
614 {
615 TRACE("(%p,'%4.4s',%p,%p)\n", pstream, (char*)&fcc, lp, lpread);
616
617 if (pstream == NULL)
618 return AVIERR_BADHANDLE;
619
620 return IAVIStream_ReadData(pstream, fcc, lp, lpread);
621 }
622
623 /***********************************************************************
624 * AVIStreamWriteData (AVIFIL32.@)
625 */
626 HRESULT WINAPI AVIStreamWriteData(PAVISTREAM pstream, DWORD fcc, LPVOID lp,
627 LONG size)
628 {
629 TRACE("(%p,'%4.4s',%p,%d)\n", pstream, (char*)&fcc, lp, size);
630
631 if (pstream == NULL)
632 return AVIERR_BADHANDLE;
633
634 return IAVIStream_WriteData(pstream, fcc, lp, size);
635 }
636
637 /***********************************************************************
638 * AVIStreamGetFrameOpen (AVIFIL32.@)
639 */
640 PGETFRAME WINAPI AVIStreamGetFrameOpen(PAVISTREAM pstream,
641 LPBITMAPINFOHEADER lpbiWanted)
642 {
643 PGETFRAME pg = NULL;
644
645 TRACE("(%p,%p)\n", pstream, lpbiWanted);
646
647 if (FAILED(IAVIStream_QueryInterface(pstream, &IID_IGetFrame, (LPVOID*)&pg)) ||
648 pg == NULL) {
649 pg = AVIFILE_CreateGetFrame(pstream);
650 if (pg == NULL)
651 return NULL;
652 }
653
654 if (FAILED(IGetFrame_SetFormat(pg, lpbiWanted, NULL, 0, 0, -1, -1))) {
655 IGetFrame_Release(pg);
656 return NULL;
657 }
658
659 return pg;
660 }
661
662 /***********************************************************************
663 * AVIStreamGetFrame (AVIFIL32.@)
664 */
665 LPVOID WINAPI AVIStreamGetFrame(PGETFRAME pg, LONG pos)
666 {
667 TRACE("(%p,%d)\n", pg, pos);
668
669 if (pg == NULL)
670 return NULL;
671
672 return IGetFrame_GetFrame(pg, pos);
673 }
674
675 /***********************************************************************
676 * AVIStreamGetFrameClose (AVIFIL32.@)
677 */
678 HRESULT WINAPI AVIStreamGetFrameClose(PGETFRAME pg)
679 {
680 TRACE("(%p)\n", pg);
681
682 if (pg != NULL)
683 return IGetFrame_Release(pg);
684 return 0;
685 }
686
687 /***********************************************************************
688 * AVIMakeCompressedStream (AVIFIL32.@)
689 */
690 HRESULT WINAPI AVIMakeCompressedStream(PAVISTREAM *ppsCompressed,
691 PAVISTREAM psSource,
692 LPAVICOMPRESSOPTIONS aco,
693 LPCLSID pclsidHandler)
694 {
695 AVISTREAMINFOW asiw;
696 CHAR szRegKey[25];
697 CHAR szValue[100];
698 CLSID clsidHandler;
699 HRESULT hr;
700 LONG size = sizeof(szValue);
701
702 TRACE("(%p,%p,%p,%s)\n", ppsCompressed, psSource, aco,
703 debugstr_guid(pclsidHandler));
704
705 if (ppsCompressed == NULL)
706 return AVIERR_BADPARAM;
707 if (psSource == NULL)
708 return AVIERR_BADHANDLE;
709
710 *ppsCompressed = NULL;
711
712 /* if no handler given get default ones based on streamtype */
713 if (pclsidHandler == NULL) {
714 hr = IAVIStream_Info(psSource, &asiw, sizeof(asiw));
715 if (FAILED(hr))
716 return hr;
717
718 wsprintfA(szRegKey, "AVIFile\\Compressors\\%4.4s", (char*)&asiw.fccType);
719 if (RegQueryValueA(HKEY_CLASSES_ROOT, szRegKey, szValue, &size) != ERROR_SUCCESS)
720 return AVIERR_UNSUPPORTED;
721 if (AVIFILE_CLSIDFromString(szValue, &clsidHandler) != S_OK)
722 return AVIERR_UNSUPPORTED;
723 } else
724 clsidHandler = *pclsidHandler;
725
726 hr = CoCreateInstance(&clsidHandler, NULL, CLSCTX_INPROC, &IID_IAVIStream, (LPVOID*)ppsCompressed);
727 if (FAILED(hr) || *ppsCompressed == NULL)
728 return hr;
729
730 hr = IAVIStream_Create(*ppsCompressed, (LPARAM)psSource, (LPARAM)aco);
731 if (FAILED(hr)) {
732 IAVIStream_Release(*ppsCompressed);
733 *ppsCompressed = NULL;
734 }
735
736 return hr;
737 }
738
739 /***********************************************************************
740 * AVIMakeFileFromStreams (AVIFIL32.@)
741 */
742 HRESULT WINAPI AVIMakeFileFromStreams(PAVIFILE *ppfile, int nStreams,
743 PAVISTREAM *ppStreams)
744 {
745 TRACE("(%p,%d,%p)\n", ppfile, nStreams, ppStreams);
746
747 if (nStreams < 0 || ppfile == NULL || ppStreams == NULL)
748 return AVIERR_BADPARAM;
749
750 *ppfile = AVIFILE_CreateAVITempFile(nStreams, ppStreams);
751 if (*ppfile == NULL)
752 return AVIERR_MEMORY;
753
754 return AVIERR_OK;
755 }
756
757 /***********************************************************************
758 * AVIStreamOpenFromFile (AVIFIL32.@)
759 * AVIStreamOpenFromFileA (AVIFIL32.@)
760 */
761 HRESULT WINAPI AVIStreamOpenFromFileA(PAVISTREAM *ppavi, LPCSTR szFile,
762 DWORD fccType, LONG lParam,
763 UINT mode, LPCLSID pclsidHandler)
764 {
765 PAVIFILE pfile = NULL;
766 HRESULT hr;
767
768 TRACE("(%p,%s,'%4.4s',%d,0x%X,%s)\n", ppavi, debugstr_a(szFile),
769 (char*)&fccType, lParam, mode, debugstr_guid(pclsidHandler));
770
771 if (ppavi == NULL || szFile == NULL)
772 return AVIERR_BADPARAM;
773
774 *ppavi = NULL;
775
776 hr = AVIFileOpenA(&pfile, szFile, mode, pclsidHandler);
777 if (FAILED(hr) || pfile == NULL)
778 return hr;
779
780 hr = IAVIFile_GetStream(pfile, ppavi, fccType, lParam);
781 IAVIFile_Release(pfile);
782
783 return hr;
784 }
785
786 /***********************************************************************
787 * AVIStreamOpenFromFileW (AVIFIL32.@)
788 */
789 HRESULT WINAPI AVIStreamOpenFromFileW(PAVISTREAM *ppavi, LPCWSTR szFile,
790 DWORD fccType, LONG lParam,
791 UINT mode, LPCLSID pclsidHandler)
792 {
793 PAVIFILE pfile = NULL;
794 HRESULT hr;
795
796 TRACE("(%p,%s,'%4.4s',%d,0x%X,%s)\n", ppavi, debugstr_w(szFile),
797 (char*)&fccType, lParam, mode, debugstr_guid(pclsidHandler));
798
799 if (ppavi == NULL || szFile == NULL)
800 return AVIERR_BADPARAM;
801
802 *ppavi = NULL;
803
804 hr = AVIFileOpenW(&pfile, szFile, mode, pclsidHandler);
805 if (FAILED(hr) || pfile == NULL)
806 return hr;
807
808 hr = IAVIFile_GetStream(pfile, ppavi, fccType, lParam);
809 IAVIFile_Release(pfile);
810
811 return hr;
812 }
813
814 /***********************************************************************
815 * AVIStreamBeginStreaming (AVIFIL32.@)
816 */
817 LONG WINAPI AVIStreamBeginStreaming(PAVISTREAM pavi, LONG lStart, LONG lEnd, LONG lRate)
818 {
819 IAVIStreaming* pstream = NULL;
820 HRESULT hr;
821
822 TRACE("(%p,%d,%d,%d)\n", pavi, lStart, lEnd, lRate);
823
824 if (pavi == NULL)
825 return AVIERR_BADHANDLE;
826
827 hr = IAVIStream_QueryInterface(pavi, &IID_IAVIStreaming, (LPVOID*)&pstream);
828 if (SUCCEEDED(hr) && pstream != NULL) {
829 hr = IAVIStreaming_Begin(pstream, lStart, lEnd, lRate);
830 IAVIStreaming_Release(pstream);
831 } else
832 hr = AVIERR_OK;
833
834 return hr;
835 }
836
837 /***********************************************************************
838 * AVIStreamEndStreaming (AVIFIL32.@)
839 */
840 LONG WINAPI AVIStreamEndStreaming(PAVISTREAM pavi)
841 {
842 IAVIStreaming* pstream = NULL;
843 HRESULT hr;
844
845 TRACE("(%p)\n", pavi);
846
847 hr = IAVIStream_QueryInterface(pavi, &IID_IAVIStreaming, (LPVOID*)&pstream);
848 if (SUCCEEDED(hr) && pstream != NULL) {
849 IAVIStreaming_End(pstream);
850 IAVIStreaming_Release(pstream);
851 }
852
853 return AVIERR_OK;
854 }
855
856 /***********************************************************************
857 * AVIStreamStart (AVIFIL32.@)
858 */
859 LONG WINAPI AVIStreamStart(PAVISTREAM pstream)
860 {
861 AVISTREAMINFOW asiw;
862
863 TRACE("(%p)\n", pstream);
864
865 if (pstream == NULL)
866 return 0;
867
868 if (FAILED(IAVIStream_Info(pstream, &asiw, sizeof(asiw))))
869 return 0;
870
871 return asiw.dwStart;
872 }
873
874 /***********************************************************************
875 * AVIStreamLength (AVIFIL32.@)
876 */
877 LONG WINAPI AVIStreamLength(PAVISTREAM pstream)
878 {
879 AVISTREAMINFOW asiw;
880
881 TRACE("(%p)\n", pstream);
882
883 if (pstream == NULL)
884 return 0;
885
886 if (FAILED(IAVIStream_Info(pstream, &asiw, sizeof(asiw))))
887 return 0;
888
889 return asiw.dwLength;
890 }
891
892 /***********************************************************************
893 * AVIStreamSampleToTime (AVIFIL32.@)
894 */
895 LONG WINAPI AVIStreamSampleToTime(PAVISTREAM pstream, LONG lSample)
896 {
897 AVISTREAMINFOW asiw;
898 LONG time;
899
900 TRACE("(%p,%d)\n", pstream, lSample);
901
902 if (pstream == NULL)
903 return -1;
904
905 if (FAILED(IAVIStream_Info(pstream, &asiw, sizeof(asiw))))
906 return -1;
907 if (asiw.dwRate == 0)
908 return -1;
909
910 /* limit to stream bounds */
911 if (lSample < asiw.dwStart)
912 lSample = asiw.dwStart;
913 if (lSample > asiw.dwStart + asiw.dwLength)
914 lSample = asiw.dwStart + asiw.dwLength;
915
916 if (asiw.dwRate / asiw.dwScale < 1000)
917 time = (LONG)(((float)lSample * asiw.dwScale * 1000) / asiw.dwRate);
918 else
919 time = (LONG)(((float)lSample * asiw.dwScale * 1000 + (asiw.dwRate - 1)) / asiw.dwRate);
920
921 TRACE(" -> %d\n",time);
922 return time;
923 }
924
925 /***********************************************************************
926 * AVIStreamTimeToSample (AVIFIL32.@)
927 */
928 LONG WINAPI AVIStreamTimeToSample(PAVISTREAM pstream, LONG lTime)
929 {
930 AVISTREAMINFOW asiw;
931 ULONG sample;
932
933 TRACE("(%p,%d)\n", pstream, lTime);
934
935 if (pstream == NULL || lTime < 0)
936 return -1;
937
938 if (FAILED(IAVIStream_Info(pstream, &asiw, sizeof(asiw))))
939 return -1;
940 if (asiw.dwScale == 0)
941 return -1;
942
943 if (asiw.dwRate / asiw.dwScale < 1000)
944 sample = (LONG)((((float)asiw.dwRate * lTime) / (asiw.dwScale * 1000)));
945 else
946 sample = (LONG)(((float)asiw.dwRate * lTime + (asiw.dwScale * 1000 - 1)) / (asiw.dwScale * 1000));
947
948 /* limit to stream bounds */
949 if (sample < asiw.dwStart)
950 sample = asiw.dwStart;
951 if (sample > asiw.dwStart + asiw.dwLength)
952 sample = asiw.dwStart + asiw.dwLength;
953
954 TRACE(" -> %d\n", sample);
955 return sample;
956 }
957
958 /***********************************************************************
959 * AVIBuildFilter (AVIFIL32.@)
960 * AVIBuildFilterA (AVIFIL32.@)
961 */
962 HRESULT WINAPI AVIBuildFilterA(LPSTR szFilter, LONG cbFilter, BOOL fSaving)
963 {
964 LPWSTR wszFilter;
965 HRESULT hr;
966
967 TRACE("(%p,%d,%d)\n", szFilter, cbFilter, fSaving);
968
969 /* check parameters */
970 if (szFilter == NULL)
971 return AVIERR_BADPARAM;
972 if (cbFilter < 2)
973 return AVIERR_BADSIZE;
974
975 szFilter[0] = 0;
976 szFilter[1] = 0;
977
978 wszFilter = HeapAlloc(GetProcessHeap(), 0, cbFilter * sizeof(WCHAR));
979 if (wszFilter == NULL)
980 return AVIERR_MEMORY;
981
982 hr = AVIBuildFilterW(wszFilter, cbFilter, fSaving);
983 if (SUCCEEDED(hr)) {
984 WideCharToMultiByte(CP_ACP, 0, wszFilter, cbFilter,
985 szFilter, cbFilter, NULL, NULL);
986 }
987
988 HeapFree(GetProcessHeap(), 0, wszFilter);
989
990 return hr;
991 }
992
993 /***********************************************************************
994 * AVIBuildFilterW (AVIFIL32.@)
995 */
996 HRESULT WINAPI AVIBuildFilterW(LPWSTR szFilter, LONG cbFilter, BOOL fSaving)
997 {
998 static const WCHAR szClsid[] = {'C','L','S','I','D',0};
999 static const WCHAR szExtensionFmt[] = {';','*','.','%','s',0};
1000 static const WCHAR szAVIFileExtensions[] =
1001 {'A','V','I','F','i','l','e','\\','E','x','t','e','n','s','i','o','n','s',0};
1002
1003 AVIFilter *lp;
1004 WCHAR szAllFiles[40];
1005 WCHAR szFileExt[10];
1006 WCHAR szValue[128];
1007 HKEY hKey;
1008 DWORD n, i;
1009 LONG size;
1010 DWORD count = 0;
1011
1012 TRACE("(%p,%d,%d)\n", szFilter, cbFilter, fSaving);
1013
1014 /* check parameters */
1015 if (szFilter == NULL)
1016 return AVIERR_BADPARAM;
1017 if (cbFilter < 2)
1018 return AVIERR_BADSIZE;
1019
1020 lp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_FILTERS * sizeof(AVIFilter));
1021 if (lp == NULL)
1022 return AVIERR_MEMORY;
1023
1024 /*
1025 * 1. iterate over HKEY_CLASSES_ROOT\\AVIFile\\Extensions and collect
1026 * extensions and CLSID's
1027 * 2. iterate over collected CLSID's and copy its description and its
1028 * extensions to szFilter if it fits
1029 *
1030 * First filter is named "All multimedia files" and its filter is a
1031 * collection of all possible extensions except "*.*".
1032 */
1033 if (RegOpenKeyW(HKEY_CLASSES_ROOT, szAVIFileExtensions, &hKey) != S_OK) {
1034 HeapFree(GetProcessHeap(), 0, lp);
1035 return AVIERR_ERROR;
1036 }
1037 for (n = 0;RegEnumKeyW(hKey, n, szFileExt, sizeof(szFileExt)/sizeof(szFileExt[0])) == S_OK;n++) {
1038 /* get CLSID to extension */
1039 size = sizeof(szValue);
1040 if (RegQueryValueW(hKey, szFileExt, szValue, &size) != S_OK)
1041 break;
1042
1043 /* search if the CLSID is already known */
1044 for (i = 1; i <= count; i++) {
1045 if (lstrcmpW(lp[i].szClsid, szValue) == 0)
1046 break; /* a new one */
1047 }
1048
1049 if (i == count + 1) {
1050 /* it's a new CLSID */
1051
1052 /* FIXME: How do we get info's about read/write capabilities? */
1053
1054 if (count >= MAX_FILTERS) {
1055 /* try to inform user of our full fixed size table */
1056 ERR(": More than %d filters found! Adjust MAX_FILTERS in dlls/avifil32/api.c\n", MAX_FILTERS);
1057 break;
1058 }
1059
1060 lstrcpyW(lp[i].szClsid, szValue);
1061
1062 count++;
1063 }
1064
1065 /* append extension to the filter */
1066 wsprintfW(szValue, szExtensionFmt, szFileExt);
1067 if (lp[i].szExtensions[0] == 0)
1068 lstrcatW(lp[i].szExtensions, szValue + 1);
1069 else
1070 lstrcatW(lp[i].szExtensions, szValue);
1071
1072 /* also append to the "all multimedia"-filter */
1073 if (lp[0].szExtensions[0] == 0)
1074 lstrcatW(lp[0].szExtensions, szValue + 1);
1075 else
1076 lstrcatW(lp[0].szExtensions, szValue);
1077 }
1078 RegCloseKey(hKey);
1079
1080 /* 2. get descriptions for the CLSIDs and fill out szFilter */
1081 if (RegOpenKeyW(HKEY_CLASSES_ROOT, szClsid, &hKey) != S_OK) {
1082 HeapFree(GetProcessHeap(), 0, lp);
1083 return AVIERR_ERROR;
1084 }
1085 for (n = 0; n <= count; n++) {
1086 /* first the description */
1087 if (n != 0) {
1088 size = sizeof(szValue);
1089 if (RegQueryValueW(hKey, lp[n].szClsid, szValue, &size) == S_OK) {
1090 size = lstrlenW(szValue);
1091 lstrcpynW(szFilter, szValue, cbFilter);
1092 }
1093 } else
1094 size = LoadStringW(AVIFILE_hModule,IDS_ALLMULTIMEDIA,szFilter,cbFilter);
1095
1096 /* check for enough space */
1097 size++;
1098 if (cbFilter < size + lstrlenW(lp[n].szExtensions) + 2) {
1099 szFilter[0] = 0;
1100 szFilter[1] = 0;
1101 HeapFree(GetProcessHeap(), 0, lp);
1102 RegCloseKey(hKey);
1103 return AVIERR_BUFFERTOOSMALL;
1104 }
1105 cbFilter -= size;
1106 szFilter += size;
1107
1108 /* and then the filter */
1109 lstrcpynW(szFilter, lp[n].szExtensions, cbFilter);
1110 size = lstrlenW(lp[n].szExtensions) + 1;
1111 cbFilter -= size;
1112 szFilter += size;
1113 }
1114
1115 RegCloseKey(hKey);
1116 HeapFree(GetProcessHeap(), 0, lp);
1117
1118 /* add "All files" "*.*" filter if enough space left */
1119 size = LoadStringW(AVIFILE_hModule, IDS_ALLFILES,
1120 szAllFiles, sizeof(szAllFiles)/sizeof(szAllFiles[0])) + 1;
1121 if (cbFilter > size) {
1122 int i;
1123
1124 /* replace '@' with \000 to separate description of filter */
1125 for (i = 0; i < size && szAllFiles[i] != 0; i++) {
1126 if (szAllFiles[i] == '@') {
1127 szAllFiles[i] = 0;
1128 break;
1129 }
1130 }
1131
1132 memcpy(szFilter, szAllFiles, size * sizeof(szAllFiles[0]));
1133 szFilter += size;
1134 szFilter[0] = 0;
1135
1136 return AVIERR_OK;
1137 } else {
1138 szFilter[0] = 0;
1139 return AVIERR_BUFFERTOOSMALL;
1140 }
1141 }
1142
1143 static BOOL AVISaveOptionsFmtChoose(HWND hWnd)
1144 {
1145 LPAVICOMPRESSOPTIONS pOptions = SaveOpts.ppOptions[SaveOpts.nCurrent];
1146 AVISTREAMINFOW sInfo;
1147
1148 TRACE("(%p)\n", hWnd);
1149
1150 if (pOptions == NULL || SaveOpts.ppavis[SaveOpts.nCurrent] == NULL) {
1151 ERR(": bad state!\n");
1152 return FALSE;
1153 }
1154
1155 if (FAILED(AVIStreamInfoW(SaveOpts.ppavis[SaveOpts.nCurrent],
1156 &sInfo, sizeof(sInfo)))) {
1157 ERR(": AVIStreamInfoW failed!\n");
1158 return FALSE;
1159 }
1160
1161 if (sInfo.fccType == streamtypeVIDEO) {
1162 COMPVARS cv;
1163 BOOL ret;
1164
1165 memset(&cv, 0, sizeof(cv));
1166
1167 if ((pOptions->dwFlags & AVICOMPRESSF_VALID) == 0) {
1168 memset(pOptions, 0, sizeof(AVICOMPRESSOPTIONS));
1169 pOptions->fccType = streamtypeVIDEO;
1170 pOptions->fccHandler = comptypeDIB;
1171 pOptions->dwQuality = (DWORD)ICQUALITY_DEFAULT;
1172 }
1173
1174 cv.cbSize = sizeof(cv);
1175 cv.dwFlags = ICMF_COMPVARS_VALID;
1176 /*cv.fccType = pOptions->fccType; */
1177 cv.fccHandler = pOptions->fccHandler;
1178 cv.lQ = pOptions->dwQuality;
1179 cv.lpState = pOptions->lpParms;
1180 cv.cbState = pOptions->cbParms;
1181 if (pOptions->dwFlags & AVICOMPRESSF_KEYFRAMES)
1182 cv.lKey = pOptions->dwKeyFrameEvery;
1183 else
1184 cv.lKey = 0;
1185 if (pOptions->dwFlags & AVICOMPRESSF_DATARATE)
1186 cv.lDataRate = pOptions->dwBytesPerSecond / 1024; /* need kBytes */
1187 else
1188 cv.lDataRate = 0;
1189
1190 ret = ICCompressorChoose(hWnd, SaveOpts.uFlags, NULL,
1191 SaveOpts.ppavis[SaveOpts.nCurrent], &cv, NULL);
1192
1193 if (ret) {
1194 pOptions->fccHandler = cv.fccHandler;
1195 pOptions->lpParms = cv.lpState;
1196 pOptions->cbParms = cv.cbState;
1197 pOptions->dwQuality = cv.lQ;
1198 if (cv.lKey != 0) {
1199 pOptions->dwKeyFrameEvery = cv.lKey;
1200 pOptions->dwFlags |= AVICOMPRESSF_KEYFRAMES;
1201 } else
1202 pOptions->dwFlags &= ~AVICOMPRESSF_KEYFRAMES;
1203 if (cv.lDataRate != 0) {
1204 pOptions->dwBytesPerSecond = cv.lDataRate * 1024; /* need bytes */
1205 pOptions->dwFlags |= AVICOMPRESSF_DATARATE;
1206 } else
1207 pOptions->dwFlags &= ~AVICOMPRESSF_DATARATE;
1208 pOptions->dwFlags |= AVICOMPRESSF_VALID;
1209 }
1210 ICCompressorFree(&cv);
1211
1212 return ret;
1213 } else if (sInfo.fccType == streamtypeAUDIO) {
1214 ACMFORMATCHOOSEW afmtc;
1215 MMRESULT ret;
1216 LONG size;
1217
1218 /* FIXME: check ACM version -- Which version is needed? */
1219
1220 memset(&afmtc, 0, sizeof(afmtc));
1221 afmtc.cbStruct = sizeof(afmtc);
1222 afmtc.fdwStyle = 0;
1223 afmtc.hwndOwner = hWnd;
1224
1225 acmMetrics(NULL, ACM_METRIC_MAX_SIZE_FORMAT, &size);
1226 if ((pOptions->cbFormat == 0 || pOptions->lpFormat == NULL) && size != 0) {
1227 pOptions->lpFormat = HeapAlloc(GetProcessHeap(), 0, size);
1228 pOptions->cbFormat = size;
1229 } else if (pOptions->cbFormat < (DWORD)size) {
1230 pOptions->lpFormat = HeapReAlloc(GetProcessHeap(), 0, pOptions->lpFormat, size);
1231 pOptions->cbFormat = size;
1232 }
1233 if (pOptions->lpFormat == NULL)
1234 return FALSE;
1235 afmtc.pwfx = pOptions->lpFormat;
1236 afmtc.cbwfx = pOptions->cbFormat;
1237
1238 size = 0;
1239 AVIStreamFormatSize(SaveOpts.ppavis[SaveOpts.nCurrent],
1240 sInfo.dwStart, &size);
1241 if (size < (LONG)sizeof(PCMWAVEFORMAT))
1242 size = sizeof(PCMWAVEFORMAT);
1243 afmtc.pwfxEnum = HeapAlloc(GetProcessHeap(), 0, size);
1244 if (afmtc.pwfxEnum != NULL) {
1245 AVIStreamReadFormat(SaveOpts.ppavis[SaveOpts.nCurrent],
1246 sInfo.dwStart, afmtc.pwfxEnum, &size);
1247 afmtc.fdwEnum = ACM_FORMATENUMF_CONVERT;
1248 }
1249
1250 ret = acmFormatChooseW(&afmtc);
1251 if (ret == S_OK)
1252 pOptions->dwFlags |= AVICOMPRESSF_VALID;
1253
1254 HeapFree(GetProcessHeap(), 0, afmtc.pwfxEnum);
1255 return (ret == S_OK ? TRUE : FALSE);
1256 } else {
1257 ERR(": unknown streamtype 0x%08X\n", sInfo.fccType);
1258 return FALSE;
1259 }
1260 }
1261
1262 static void AVISaveOptionsUpdate(HWND hWnd)
1263 {
1264 static const WCHAR szVideoFmt[]={'%','l','d','x','%','l','d','x','%','d',0};
1265 static const WCHAR szAudioFmt[]={'%','s',' ','%','s',0};
1266
1267 WCHAR szFormat[128];
1268 AVISTREAMINFOW sInfo;
1269 LPVOID lpFormat;
1270 LONG size;
1271
1272 TRACE("(%p)\n", hWnd);
1273
1274 SaveOpts.nCurrent = SendDlgItemMessageW(hWnd,IDC_STREAM,CB_GETCURSEL,0,0);
1275 if (SaveOpts.nCurrent < 0)
1276 return;
1277
1278 if (FAILED(AVIStreamInfoW(SaveOpts.ppavis[SaveOpts.nCurrent], &sInfo, sizeof(sInfo))))
1279 return;
1280
1281 AVIStreamFormatSize(SaveOpts.ppavis[SaveOpts.nCurrent],sInfo.dwStart,&size);
1282 if (size > 0) {
1283 szFormat[0] = 0;
1284
1285 /* read format to build format description string */
1286 lpFormat = HeapAlloc(GetProcessHeap(), 0, size);
1287 if (lpFormat != NULL) {
1288 if (SUCCEEDED(AVIStreamReadFormat(SaveOpts.ppavis[SaveOpts.nCurrent],sInfo.dwStart,lpFormat, &size))) {
1289 if (sInfo.fccType == streamtypeVIDEO) {
1290 LPBITMAPINFOHEADER lpbi = lpFormat;
1291 ICINFO icinfo;
1292
1293 wsprintfW(szFormat, szVideoFmt, lpbi->biWidth,
1294 lpbi->biHeight, lpbi->biBitCount);
1295
1296 if (lpbi->biCompression != BI_RGB) {
1297 HIC hic;
1298
1299 hic = ICLocate(ICTYPE_VIDEO, sInfo.fccHandler, lpFormat,
1300 NULL, ICMODE_DECOMPRESS);
1301 if (hic != NULL) {
1302 if (ICGetInfo(hic, &icinfo, sizeof(icinfo)) == S_OK)
1303 lstrcatW(szFormat, icinfo.szDescription);
1304 ICClose(hic);
1305 }
1306 } else {
1307 LoadStringW(AVIFILE_hModule, IDS_UNCOMPRESSED,
1308 icinfo.szDescription,
1309 sizeof(icinfo.szDescription)/sizeof(icinfo.szDescription[0]));
1310 lstrcatW(szFormat, icinfo.szDescription);
1311 }
1312 } else if (sInfo.fccType == streamtypeAUDIO) {
1313 ACMFORMATTAGDETAILSW aftd;
1314 ACMFORMATDETAILSW afd;
1315
1316 memset(&aftd, 0, sizeof(aftd));
1317 memset(&afd, 0, sizeof(afd));
1318
1319 aftd.cbStruct = sizeof(aftd);
1320 aftd.dwFormatTag = afd.dwFormatTag =
1321 ((PWAVEFORMATEX)lpFormat)->wFormatTag;
1322 aftd.cbFormatSize = afd.cbwfx = size;
1323
1324 afd.cbStruct = sizeof(afd);
1325 afd.pwfx = lpFormat;
1326
1327 if (acmFormatTagDetailsW(NULL, &aftd,
1328 ACM_FORMATTAGDETAILSF_FORMATTAG) == S_OK) {
1329 if (acmFormatDetailsW(NULL,&afd,ACM_FORMATDETAILSF_FORMAT) == S_OK)
1330 wsprintfW(szFormat, szAudioFmt, afd.szFormat, aftd.szFormatTag);
1331 }
1332 }
1333 }
1334 HeapFree(GetProcessHeap(), 0, lpFormat);
1335 }
1336
1337 /* set text for format description */
1338 SetDlgItemTextW(hWnd, IDC_FORMATTEXT, szFormat);
1339
1340 /* Disable option button for unsupported streamtypes */
1341 if (sInfo.fccType == streamtypeVIDEO ||
1342 sInfo.fccType == streamtypeAUDIO)
1343 EnableWindow(GetDlgItem(hWnd, IDC_OPTIONS), TRUE);
1344 else
1345 EnableWindow(GetDlgItem(hWnd, IDC_OPTIONS), FALSE);
1346 }
1347
1348 }
1349
1350 static INT_PTR CALLBACK AVISaveOptionsDlgProc(HWND hWnd, UINT uMsg,
1351 WPARAM wParam, LPARAM lParam)
1352 {
1353 DWORD dwInterleave;
1354 BOOL bIsInterleaved;
1355 INT n;
1356
1357 /*TRACE("(%p,%u,0x%04X,0x%08lX)\n", hWnd, uMsg, wParam, lParam);*/
1358
1359 switch (uMsg) {
1360 case WM_INITDIALOG:
1361 SaveOpts.nCurrent = 0;
1362 if (SaveOpts.nStreams == 1) {
1363 EndDialog(hWnd, AVISaveOptionsFmtChoose(hWnd));
1364 return TRUE;
1365 }
1366
1367 /* add streams */
1368 for (n = 0; n < SaveOpts.nStreams; n++) {
1369 AVISTREAMINFOW sInfo;
1370
1371 AVIStreamInfoW(SaveOpts.ppavis[n], &sInfo, sizeof(sInfo));
1372 SendDlgItemMessageW(hWnd, IDC_STREAM, CB_ADDSTRING,
1373 0L, (LPARAM)sInfo.szName);
1374 }
1375
1376 /* select first stream */
1377 SendDlgItemMessageW(hWnd, IDC_STREAM, CB_SETCURSEL, 0, 0);
1378 SendMessageW(hWnd, WM_COMMAND, MAKELONG(IDC_STREAM, CBN_SELCHANGE), (LPARAM)hWnd);
1379
1380 /* initialize interleave */
1381 if (SaveOpts.ppOptions[0] != NULL &&
1382 (SaveOpts.ppOptions[0]->dwFlags & AVICOMPRESSF_VALID)) {
1383 bIsInterleaved = (SaveOpts.ppOptions[0]->dwFlags & AVICOMPRESSF_INTERLEAVE);
1384 dwInterleave = SaveOpts.ppOptions[0]->dwInterleaveEvery;
1385 } else {
1386 bIsInterleaved = TRUE;
1387 dwInterleave = 0;
1388 }
1389 CheckDlgButton(hWnd, IDC_INTERLEAVE, bIsInterleaved);
1390 SetDlgItemInt(hWnd, IDC_INTERLEAVEEVERY, dwInterleave, FALSE);
1391 EnableWindow(GetDlgItem(hWnd, IDC_INTERLEAVEEVERY), bIsInterleaved);
1392 break;
1393 case WM_COMMAND:
1394 switch (LOWORD(wParam)) {
1395 case IDOK:
1396 /* get data from controls and save them */
1397 dwInterleave = GetDlgItemInt(hWnd, IDC_INTERLEAVEEVERY, NULL, 0);
1398 bIsInterleaved = IsDlgButtonChecked(hWnd, IDC_INTERLEAVE);
1399 for (n = 0; n < SaveOpts.nStreams; n++) {
1400 if (SaveOpts.ppOptions[n] != NULL) {
1401 if (bIsInterleaved) {
1402 SaveOpts.ppOptions[n]->dwFlags |= AVICOMPRESSF_INTERLEAVE;
1403 SaveOpts.ppOptions[n]->dwInterleaveEvery = dwInterleave;
1404 } else
1405 SaveOpts.ppOptions[n]->dwFlags &= ~AVICOMPRESSF_INTERLEAVE;
1406 }
1407 }
1408 /* fall through */
1409 case IDCANCEL:
1410 EndDialog(hWnd, LOWORD(wParam) == IDOK);
1411 break;
1412 case IDC_INTERLEAVE:
1413 EnableWindow(GetDlgItem(hWnd, IDC_INTERLEAVEEVERY),
1414 IsDlgButtonChecked(hWnd, IDC_INTERLEAVE));
1415 break;
1416 case IDC_STREAM:
1417 if (HIWORD(wParam) == CBN_SELCHANGE) {
1418 /* update control elements */
1419 AVISaveOptionsUpdate(hWnd);
1420 }
1421 break;
1422 case IDC_OPTIONS:
1423 AVISaveOptionsFmtChoose(hWnd);
1424 break;
1425 };
1426 return TRUE;
1427 };
1428
1429 return FALSE;
1430 }
1431
1432 /***********************************************************************
1433 * AVISaveOptions (AVIFIL32.@)
1434 */
1435 BOOL WINAPI AVISaveOptions(HWND hWnd, UINT uFlags, INT nStreams,
1436 PAVISTREAM *ppavi, LPAVICOMPRESSOPTIONS *ppOptions)
1437 {
1438 LPAVICOMPRESSOPTIONS pSavedOptions = NULL;
1439 INT ret, n;
1440
1441 TRACE("(%p,0x%X,%d,%p,%p)\n", hWnd, uFlags, nStreams,
1442 ppavi, ppOptions);
1443
1444 /* check parameters */
1445 if (nStreams <= 0 || ppavi == NULL || ppOptions == NULL)
1446 return AVIERR_BADPARAM;
1447
1448 /* save options in case the user presses cancel */
1449 if (ppOptions != NULL && nStreams > 1) {
1450 pSavedOptions = HeapAlloc(GetProcessHeap(), 0, nStreams * sizeof(AVICOMPRESSOPTIONS));
1451 if (pSavedOptions == NULL)
1452 return FALSE;
1453
1454 for (n = 0; n < nStreams; n++) {
1455 if (ppOptions[n] != NULL)
1456 memcpy(pSavedOptions + n, ppOptions[n], sizeof(AVICOMPRESSOPTIONS));
1457 }
1458 }
1459
1460 SaveOpts.uFlags = uFlags;
1461 SaveOpts.nStreams = nStreams;
1462 SaveOpts.ppavis = ppavi;
1463 SaveOpts.ppOptions = ppOptions;
1464
1465 ret = DialogBoxW(AVIFILE_hModule, MAKEINTRESOURCEW(IDD_SAVEOPTIONS),
1466 hWnd, AVISaveOptionsDlgProc);
1467
1468 if (ret == -1)
1469 ret = FALSE;
1470
1471 /* restore options when user pressed cancel */
1472 if (pSavedOptions != NULL) {
1473 if (ret == FALSE) {
1474 for (n = 0; n < nStreams; n++) {
1475 if (ppOptions[n] != NULL)
1476 memcpy(ppOptions[n], pSavedOptions + n, sizeof(AVICOMPRESSOPTIONS));
1477 }
1478 }
1479 HeapFree(GetProcessHeap(), 0, pSavedOptions);
1480 }
1481
1482 return ret;
1483 }
1484
1485 /***********************************************************************
1486 * AVISaveOptionsFree (AVIFIL32.@)
1487 */
1488 HRESULT WINAPI AVISaveOptionsFree(INT nStreams,LPAVICOMPRESSOPTIONS*ppOptions)
1489 {
1490 TRACE("(%d,%p)\n", nStreams, ppOptions);
1491
1492 if (nStreams < 0 || ppOptions == NULL)
1493 return AVIERR_BADPARAM;
1494
1495 for (nStreams--; nStreams >= 0; nStreams--) {
1496 if (ppOptions[nStreams] != NULL) {
1497 ppOptions[nStreams]->dwFlags &= ~AVICOMPRESSF_VALID;
1498
1499 if (ppOptions[nStreams]->lpParms != NULL) {
1500 HeapFree(GetProcessHeap(), 0, ppOptions[nStreams]->lpParms);
1501 ppOptions[nStreams]->lpParms = NULL;
1502 ppOptions[nStreams]->cbParms = 0;
1503 }
1504 if (ppOptions[nStreams]->lpFormat != NULL) {
1505 HeapFree(GetProcessHeap(), 0, ppOptions[nStreams]->lpFormat);
1506 ppOptions[nStreams]->lpFormat = NULL;
1507 ppOptions[nStreams]->cbFormat = 0;
1508 }
1509 }
1510 }
1511
1512 return AVIERR_OK;
1513 }
1514
1515 /***********************************************************************
1516 * AVISaveVA (AVIFIL32.@)
1517 */
1518 HRESULT WINAPI AVISaveVA(LPCSTR szFile, CLSID *pclsidHandler,
1519 AVISAVECALLBACK lpfnCallback, int nStream,
1520 PAVISTREAM *ppavi, LPAVICOMPRESSOPTIONS *plpOptions)
1521 {
1522 LPWSTR wszFile = NULL;
1523 HRESULT hr;
1524 int len;
1525
1526 TRACE("%s,%p,%p,%d,%p,%p)\n", debugstr_a(szFile), pclsidHandler,
1527 lpfnCallback, nStream, ppavi, plpOptions);
1528
1529 if (szFile == NULL || ppavi == NULL || plpOptions == NULL)
1530 return AVIERR_BADPARAM;
1531
1532 /* convert ASCII string to Unicode and call Unicode function */
1533 len = MultiByteToWideChar(CP_ACP, 0, szFile, -1, NULL, 0);
1534 if (len <= 0)
1535 return AVIERR_BADPARAM;
1536
1537 wszFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1538 if (wszFile == NULL)
1539 return AVIERR_MEMORY;
1540
1541 MultiByteToWideChar(CP_ACP, 0, szFile, -1, wszFile, len);
1542
1543 hr = AVISaveVW(wszFile, pclsidHandler, lpfnCallback,
1544 nStream, ppavi, plpOptions);
1545
1546 HeapFree(GetProcessHeap(), 0, wszFile);
1547
1548 return hr;
1549 }
1550
1551 /***********************************************************************
1552 * AVIFILE_AVISaveDefaultCallback (internal)
1553 */
1554 static BOOL WINAPI AVIFILE_AVISaveDefaultCallback(INT progress)
1555 {
1556 TRACE("(%d)\n", progress);
1557
1558 return FALSE;
1559 }
1560
1561 /***********************************************************************
1562 * AVISaveVW (AVIFIL32.@)
1563 */
1564 HRESULT WINAPI AVISaveVW(LPCWSTR szFile, CLSID *pclsidHandler,
1565 AVISAVECALLBACK lpfnCallback, int nStreams,
1566 PAVISTREAM *ppavi, LPAVICOMPRESSOPTIONS *plpOptions)
1567 {
1568 LONG lStart[MAX_AVISTREAMS];
1569 PAVISTREAM pOutStreams[MAX_AVISTREAMS];
1570 PAVISTREAM pInStreams[MAX_AVISTREAMS];
1571 AVIFILEINFOW fInfo;
1572 AVISTREAMINFOW sInfo;
1573
1574 PAVIFILE pfile = NULL; /* the output AVI file */
1575 LONG lFirstVideo = -1;
1576 int curStream;
1577
1578 /* for interleaving ... */
1579 DWORD dwInterleave = 0; /* interleave rate */
1580 DWORD dwFileInitialFrames;
1581 LONG lFileLength;
1582 LONG lSampleInc;
1583
1584 /* for reading/writing the data ... */
1585 LPVOID lpBuffer = NULL;
1586 LONG cbBuffer; /* real size of lpBuffer */
1587 LONG lBufferSize; /* needed bytes for format(s), etc. */
1588 LONG lReadBytes;
1589 LONG lReadSamples;
1590 HRESULT hres;
1591
1592 TRACE("(%s,%p,%p,%d,%p,%p)\n", debugstr_w(szFile), pclsidHandler,
1593 lpfnCallback, nStreams, ppavi, plpOptions);
1594
1595 if (szFile == NULL || ppavi == NULL || plpOptions == NULL)
1596 return AVIERR_BADPARAM;
1597 if (nStreams >= MAX_AVISTREAMS) {
1598 WARN("Can't write AVI with %d streams only supports %d -- change MAX_AVISTREAMS!\n", nStreams, MAX_AVISTREAMS);
1599 return AVIERR_INTERNAL;
1600 }
1601
1602 if (lpfnCallback == NULL)
1603 lpfnCallback = AVIFILE_AVISaveDefaultCallback;
1604
1605 /* clear local variable(s) */
1606 for (curStream = 0; curStream < nStreams; curStream++) {
1607 pInStreams[curStream] = NULL;
1608 pOutStreams[curStream] = NULL;
1609 }
1610
1611 /* open output AVI file (create it if it doesn't exist) */
1612 hres = AVIFileOpenW(&pfile, szFile, OF_CREATE|OF_SHARE_EXCLUSIVE|OF_WRITE,
1613 pclsidHandler);
1614 if (FAILED(hres))
1615 return hres;
1616 AVIFileInfoW(pfile, &fInfo, sizeof(fInfo)); /* for dwCaps */
1617
1618 /* initialize our data structures part 1 */
1619 for (curStream = 0; curStream < nStreams; curStream++) {
1620 PAVISTREAM pCurStream = ppavi[curStream];
1621
1622 hres = AVIStreamInfoW(pCurStream, &sInfo, sizeof(sInfo));
1623 if (FAILED(hres))
1624 goto error;
1625
1626 /* search first video stream and check for interleaving */
1627 if (sInfo.fccType == streamtypeVIDEO) {
1628 /* remember first video stream -- needed for interleaving */
1629 if (lFirstVideo < 0)
1630 lFirstVideo = curStream;
1631 } else if (!dwInterleave && plpOptions != NULL) {
1632 /* check if any non-video stream wants to be interleaved */
1633 WARN("options.flags=0x%X options.dwInterleave=%u\n",plpOptions[curStream]->dwFlags,plpOptions[curStream]->dwInterleaveEvery);
1634 if (plpOptions[curStream] != NULL &&
1635 plpOptions[curStream]->dwFlags & AVICOMPRESSF_INTERLEAVE)
1636 dwInterleave = plpOptions[curStream]->dwInterleaveEvery;
1637 }
1638
1639 /* create de-/compressed stream interface if needed */
1640 pInStreams[curStream] = NULL;
1641 if (plpOptions != NULL && plpOptions[curStream] != NULL) {
1642 if (plpOptions[curStream]->fccHandler ||
1643 plpOptions[curStream]->lpFormat != NULL) {
1644 DWORD dwKeySave = plpOptions[curStream]->dwKeyFrameEvery;
1645
1646 if (fInfo.dwCaps & AVIFILECAPS_ALLKEYFRAMES)
1647 plpOptions[curStream]->dwKeyFrameEvery = 1;
1648
1649 hres = AVIMakeCompressedStream(&pInStreams[curStream], pCurStream,
1650 plpOptions[curStream], NULL);
1651 plpOptions[curStream]->dwKeyFrameEvery = dwKeySave;
1652 if (FAILED(hres) || pInStreams[curStream] == NULL) {
1653 pInStreams[curStream] = NULL;
1654 goto error;
1655 }
1656
1657 /* test stream interface and update stream-info */
1658 hres = AVIStreamInfoW(pInStreams[curStream], &sInfo, sizeof(sInfo));
1659 if (FAILED(hres))
1660 goto error;
1661 }
1662 }
1663
1664 /* now handle streams which will only be copied */
1665 if (pInStreams[curStream] == NULL) {
1666 pCurStream = pInStreams[curStream] = ppavi[curStream];
1667 AVIStreamAddRef(pCurStream);
1668 } else
1669 pCurStream = pInStreams[curStream];
1670
1671 lStart[curStream] = sInfo.dwStart;
1672 } /* for all streams */
1673
1674 /* check that first video stream is the first stream */
1675 if (lFirstVideo > 0) {
1676 PAVISTREAM pTmp = pInStreams[lFirstVideo];
1677 LONG lTmp = lStart[lFirstVideo];
1678
1679 pInStreams[lFirstVideo] = pInStreams[0];
1680 pInStreams[0] = pTmp;
1681 lStart[lFirstVideo] = lStart[0];
1682 lStart[0] = lTmp;
1683 lFirstVideo = 0;
1684 }
1685
1686 /* allocate buffer for formats, data, etc. of an initial size of 64 kBytes*/
1687 cbBuffer = 0x00010000;
1688 lpBuffer = HeapAlloc(GetProcessHeap(), 0, cbBuffer);
1689 if (lpBuffer == NULL) {
1690 hres = AVIERR_MEMORY;
1691 goto error;
1692 }
1693
1694 AVIStreamInfoW(pInStreams[0], &sInfo, sizeof(sInfo));
1695 lFileLength = sInfo.dwLength;
1696 dwFileInitialFrames = 0;
1697 if (lFirstVideo >= 0) {
1698 /* check for correct version of the format
1699 * -- need at least BITMAPINFOHEADER or newer
1700 */
1701 lSampleInc = 1;
1702 lBufferSize = cbBuffer;
1703 hres = AVIStreamReadFormat(pInStreams[lFirstVideo], AVIStreamStart(pInStreams[lFirstVideo]), lpBuffer, &lBufferSize);
1704 if (lBufferSize < (LONG)sizeof(BITMAPINFOHEADER))
1705 hres = AVIERR_INTERNAL;
1706 if (FAILED(hres))
1707 goto error;
1708 } else /* use one second blocks for interleaving if no video present */
1709 lSampleInc = AVIStreamTimeToSample(pInStreams[0], 1000000);
1710
1711 /* create output streams */
1712 for (curStream = 0; curStream < nStreams; curStream++) {
1713 AVIStreamInfoW(pInStreams[curStream], &sInfo, sizeof(sInfo));
1714
1715 sInfo.dwInitialFrames = 0;
1716 if (dwInterleave != 0 && curStream > 0 && sInfo.fccType != streamtypeVIDEO) {
1717 /* 750 ms initial frames for non-video streams */
1718 sInfo.dwInitialFrames = AVIStreamTimeToSample(pInStreams[0], 750);
1719 }
1720
1721 hres = AVIFileCreateStreamW(pfile, &pOutStreams[curStream], &sInfo);
1722 if (pOutStreams[curStream] != NULL && SUCCEEDED(hres)) {
1723 /* copy initial format for this stream */
1724 lBufferSize = cbBuffer;
1725 hres = AVIStreamReadFormat(pInStreams[curStream], sInfo.dwStart,
1726 lpBuffer, &lBufferSize);
1727 if (FAILED(hres))
1728 goto error;
1729 hres = AVIStreamSetFormat(pOutStreams[curStream], 0, lpBuffer, lBufferSize);
1730 if (FAILED(hres))
1731 goto error;
1732
1733 /* try to copy stream handler data */
1734 lBufferSize = cbBuffer;
1735 hres = AVIStreamReadData(pInStreams[curStream], ckidSTREAMHANDLERDATA,
1736 lpBuffer, &lBufferSize);
1737 if (SUCCEEDED(hres) && lBufferSize > 0) {
1738 hres = AVIStreamWriteData(pOutStreams[curStream],ckidSTREAMHANDLERDATA,
1739 lpBuffer, lBufferSize);
1740 if (FAILED(hres))
1741 goto error;
1742 }
1743
1744 if (dwFileInitialFrames < sInfo.dwInitialFrames)
1745 dwFileInitialFrames = sInfo.dwInitialFrames;
1746 lReadBytes =
1747 AVIStreamSampleToSample(pOutStreams[0], pInStreams[curStream],
1748 sInfo.dwLength);
1749 if (lFileLength < lReadBytes)
1750 lFileLength = lReadBytes;
1751 } else {
1752 /* creation of de-/compression stream interface failed */
1753 WARN("creation of (de-)compression stream failed for stream %d\n",curStream);
1754 AVIStreamRelease(pInStreams[curStream]);
1755 if (curStream + 1 >= nStreams) {
1756 /* move the others one up */
1757 PAVISTREAM *ppas = &pInStreams[curStream];
1758 int n = nStreams - (curStream + 1);
1759
1760 do {
1761 *ppas = pInStreams[curStream + 1];
1762 } while (--n);
1763 }
1764 nStreams--;
1765 curStream--;
1766 }
1767 } /* create output streams for all input streams */
1768
1769 /* have we still something to write, or lost everything? */
1770 if (nStreams <= 0)
1771 goto error;
1772
1773 if (dwInterleave) {
1774 LONG lCurFrame = -dwFileInitialFrames;
1775
1776 /* interleaved file */
1777 if (dwInterleave == 1)
1778 AVIFileEndRecord(pfile);
1779
1780 for (; lCurFrame < lFileLength; lCurFrame += lSampleInc) {
1781 for (curStream = 0; curStream < nStreams; curStream++) {
1782 LONG lLastSample;
1783
1784 hres = AVIStreamInfoW(pOutStreams[curStream], &sInfo, sizeof(sInfo));
1785 if (FAILED(hres))
1786 goto error;
1787
1788 /* initial frames phase at the end for this stream? */
1789 if (-(LONG)sInfo.dwInitialFrames > lCurFrame)
1790 continue;
1791
1792 if ((lFileLength - lSampleInc) <= lCurFrame) {
1793 lLastSample = AVIStreamLength(pInStreams[curStream]);
1794 lFirstVideo = lLastSample + AVIStreamStart(pInStreams[curStream]);
1795 } else {
1796 if (curStream != 0) {
1797 lFirstVideo =
1798 AVIStreamSampleToSample(pInStreams[curStream], pInStreams[0],
1799 (sInfo.fccType == streamtypeVIDEO ?
1800 (LONG)dwInterleave : lSampleInc) +
1801 sInfo.dwInitialFrames + lCurFrame);
1802 } else
1803 lFirstVideo = lSampleInc + (sInfo.dwInitialFrames + lCurFrame);
1804
1805 lLastSample = AVIStreamEnd(pInStreams[curStream]);
1806 if (lLastSample <= lFirstVideo)
1807 lFirstVideo = lLastSample;
1808 }
1809
1810 /* copy needed samples now */
1811 WARN("copy from stream %d samples %d to %d...\n",curStream,
1812 lStart[curStream],lFirstVideo);
1813 while (lFirstVideo > lStart[curStream]) {
1814 DWORD flags = 0;
1815
1816 /* copy format in case it can change */
1817 lBufferSize = cbBuffer;
1818 hres = AVIStreamReadFormat(pInStreams[curStream], lStart[curStream],
1819 lpBuffer, &lBufferSize);
1820 if (FAILED(hres))
1821 goto error;
1822 AVIStreamSetFormat(pOutStreams[curStream], lStart[curStream],
1823 lpBuffer, lBufferSize);
1824
1825 /* try to read data until we got it, or error */
1826 do {
1827 hres = AVIStreamRead(pInStreams[curStream], lStart[curStream],
1828 lFirstVideo - lStart[curStream], lpBuffer,
1829 cbBuffer, &lReadBytes, &lReadSamples);
1830 } while ((hres == AVIERR_BUFFERTOOSMALL) &&
1831 (lpBuffer = HeapReAlloc(GetProcessHeap(), 0, lpBuffer, cbBuffer *= 2)) != NULL);
1832 if (lpBuffer == NULL)
1833 hres = AVIERR_MEMORY;
1834 if (FAILED(hres))
1835 goto error;
1836
1837 if (AVIStreamIsKeyFrame(pInStreams[curStream], (LONG)sInfo.dwStart))
1838 flags = AVIIF_KEYFRAME;
1839 hres = AVIStreamWrite(pOutStreams[curStream], -1, lReadSamples,
1840 lpBuffer, lReadBytes, flags, NULL, NULL);
1841 if (FAILED(hres))
1842 goto error;
1843
1844 lStart[curStream] += lReadSamples;
1845 }
1846 lStart[curStream] = lFirstVideo;
1847 } /* stream by stream */
1848
1849 /* need to close this block? */
1850 if (dwInterleave == 1) {
1851 hres = AVIFileEndRecord(pfile);
1852 if (FAILED(hres))
1853 break;
1854 }
1855
1856 /* show progress */
1857 if (lpfnCallback(MulDiv(dwFileInitialFrames + lCurFrame, 100,
1858 dwFileInitialFrames + lFileLength))) {
1859 hres = AVIERR_USERABORT;
1860 break;
1861 }
1862 } /* copy frame by frame */
1863 } else {
1864 /* non-interleaved file */
1865
1866 for (curStream = 0; curStream < nStreams; curStream++) {
1867 /* show progress */
1868 if (lpfnCallback(MulDiv(curStream, 100, nStreams))) {
1869 hres = AVIERR_USERABORT;
1870 goto error;
1871 }
1872
1873 AVIStreamInfoW(pInStreams[curStream], &sInfo, sizeof(sInfo));
1874
1875 if (sInfo.dwSampleSize != 0) {
1876 /* sample-based data like audio */
1877 while (sInfo.dwStart < sInfo.dwLength) {
1878 LONG lSamples = cbBuffer / sInfo.dwSampleSize;
1879
1880 /* copy format in case it can change */
1881 lBufferSize = cbBuffer;
1882 hres = AVIStreamReadFormat(pInStreams[curStream], sInfo.dwStart,
1883 lpBuffer, &lBufferSize);
1884 if (FAILED(hres))
1885 goto error;
1886 AVIStreamSetFormat(pOutStreams[curStream], sInfo.dwStart,
1887 lpBuffer, lBufferSize);
1888
1889 /* limit to stream boundaries */
1890 if (lSamples != (LONG)(sInfo.dwLength - sInfo.dwStart))
1891 lSamples = sInfo.dwLength - sInfo.dwStart;
1892
1893 /* now try to read until we get it, or an error occurs */
1894 do {
1895 lReadBytes = cbBuffer;
1896 lReadSamples = 0;
1897 hres = AVIStreamRead(pInStreams[curStream],sInfo.dwStart,lSamples,
1898 lpBuffer,cbBuffer,&lReadBytes,&lReadSamples);
1899 } while ((hres == AVIERR_BUFFERTOOSMALL) &&
1900 (lpBuffer = HeapReAlloc(GetProcessHeap(), 0, lpBuffer, cbBuffer *= 2)) != NULL);
1901 if (lpBuffer == NULL)
1902 hres = AVIERR_MEMORY;
1903 if (FAILED(hres))
1904 goto error;
1905 if (lReadSamples != 0) {
1906 sInfo.dwStart += lReadSamples;
1907 hres = AVIStreamWrite(pOutStreams[curStream], -1, lReadSamples,
1908 lpBuffer, lReadBytes, 0, NULL , NULL);
1909 if (FAILED(hres))
1910 goto error;
1911
1912 /* show progress */
1913 if (lpfnCallback(MulDiv(sInfo.dwStart,100,nStreams*sInfo.dwLength)+
1914 MulDiv(curStream, 100, nStreams))) {
1915 hres = AVIERR_USERABORT;
1916 goto error;
1917 }
1918 } else {
1919 if ((sInfo.dwLength - sInfo.dwStart) != 1) {
1920 hres = AVIERR_FILEREAD;
1921 goto error;
1922 }
1923 }
1924 }
1925 } else {
1926 /* block-based data like video */
1927 for (; sInfo.dwStart < sInfo.dwLength; sInfo.dwStart++) {
1928 DWORD flags = 0;
1929
1930 /* copy format in case it can change */
1931 lBufferSize = cbBuffer;
1932 hres = AVIStreamReadFormat(pInStreams[curStream], sInfo.dwStart,
1933 lpBuffer, &lBufferSize);
1934 if (FAILED(hres))
1935 goto error;
1936 AVIStreamSetFormat(pOutStreams[curStream], sInfo.dwStart,
1937 lpBuffer, lBufferSize);
1938
1939 /* try to read block and resize buffer if necessary */
1940 do {
1941 lReadSamples = 0;
1942 lReadBytes = cbBuffer;
1943 hres = AVIStreamRead(pInStreams[curStream], sInfo.dwStart, 1,
1944 lpBuffer, cbBuffer,&lReadBytes,&lReadSamples);
1945 } while ((hres == AVIERR_BUFFERTOOSMALL) &&
1946 (lpBuffer = HeapReAlloc(GetProcessHeap(), 0, lpBuffer, cbBuffer *= 2)) != NULL);
1947 if (lpBuffer == NULL)
1948 hres = AVIERR_MEMORY;
1949 if (FAILED(hres))
1950 goto error;
1951 if (lReadSamples != 1) {
1952 hres = AVIERR_FILEREAD;
1953 goto error;
1954 }
1955
1956 if (AVIStreamIsKeyFrame(pInStreams[curStream], (LONG)sInfo.dwStart))
1957 flags = AVIIF_KEYFRAME;
1958 hres = AVIStreamWrite(pOutStreams[curStream], -1, lReadSamples,
1959 lpBuffer, lReadBytes, flags, NULL, NULL);
1960 if (FAILED(hres))
1961 goto error;
1962
1963 /* show progress */
1964 if (lpfnCallback(MulDiv(sInfo.dwStart,100,nStreams*sInfo.dwLength)+
1965 MulDiv(curStream, 100, nStreams))) {
1966 hres = AVIERR_USERABORT;
1967 goto error;
1968 }
1969 } /* copy all blocks */
1970 }
1971 } /* copy data stream by stream */
1972 }
1973
1974 error:
1975 HeapFree(GetProcessHeap(), 0, lpBuffer);
1976 if (pfile != NULL) {
1977 for (curStream = 0; curStream < nStreams; curStream++) {
1978 if (pOutStreams[curStream] != NULL)
1979 AVIStreamRelease(pOutStreams[curStream]);
1980 if (pInStreams[curStream] != NULL)
1981 AVIStreamRelease(pInStreams[curStream]);
1982 }
1983
1984 AVIFileRelease(pfile);
1985 }
1986
1987 return hres;
1988 }
1989
1990 /***********************************************************************
1991 * CreateEditableStream (AVIFIL32.@)
1992 */
1993 HRESULT WINAPI CreateEditableStream(PAVISTREAM *ppEditable, PAVISTREAM pSource)
1994 {
1995 IAVIEditStream *pEdit = NULL;
1996 HRESULT hr;
1997
1998 TRACE("(%p,%p)\n", ppEditable, pSource);
1999
2000 if (ppEditable == NULL)
2001 return AVIERR_BADPARAM;
2002
2003 *ppEditable = NULL;
2004
2005 if (pSource != NULL) {
2006 hr = IAVIStream_QueryInterface(pSource, &IID_IAVIEditStream,
2007 (LPVOID*)&pEdit);
2008 if (SUCCEEDED(hr) && pEdit != NULL) {
2009 hr = IAVIEditStream_Clone(pEdit, ppEditable);
2010 IAVIEditStream_Release(pEdit);
2011
2012 return hr;
2013 }
2014 }
2015
2016 /* need own implementation of IAVIEditStream */
2017 pEdit = AVIFILE_CreateEditStream(pSource);
2018 if (pEdit == NULL)
2019 return AVIERR_MEMORY;
2020
2021 hr = IAVIEditStream_QueryInterface(pEdit, &IID_IAVIStream,
2022 (LPVOID*)ppEditable);
2023 IAVIEditStream_Release(pEdit);
2024
2025 return hr;
2026 }
2027
2028 /***********************************************************************
2029 * EditStreamClone (AVIFIL32.@)
2030 */
2031 HRESULT WINAPI EditStreamClone(PAVISTREAM pStream, PAVISTREAM *ppResult)
2032 {
2033 PAVIEDITSTREAM pEdit = NULL;
2034 HRESULT hr;
2035
2036 TRACE("(%p,%p)\n", pStream, ppResult);
2037
2038 if (pStream == NULL)
2039 return AVIERR_BADHANDLE;
2040 if (ppResult == NULL)
2041 return AVIERR_BADPARAM;
2042
2043 *ppResult = NULL;
2044
2045 hr = IAVIStream_QueryInterface(pStream, &IID_IAVIEditStream,(LPVOID*)&pEdit);
2046 if (SUCCEEDED(hr) && pEdit != NULL) {
2047 hr = IAVIEditStream_Clone(pEdit, ppResult);
2048
2049 IAVIEditStream_Release(pEdit);
2050 } else
2051 hr = AVIERR_UNSUPPORTED;
2052
2053 return hr;
2054 }
2055
2056 /***********************************************************************
2057 * EditStreamCopy (AVIFIL32.@)
2058 */
2059 HRESULT WINAPI EditStreamCopy(PAVISTREAM pStream, LONG *plStart,
2060 LONG *plLength, PAVISTREAM *ppResult)
2061 {
2062 PAVIEDITSTREAM pEdit = NULL;
2063 HRESULT hr;
2064
2065 TRACE("(%p,%p,%p,%p)\n", pStream, plStart, plLength, ppResult);
2066
2067 if (pStream == NULL)
2068 return AVIERR_BADHANDLE;
2069 if (plStart == NULL || plLength == NULL || ppResult == NULL)
2070 return AVIERR_BADPARAM;
2071
2072 *ppResult = NULL;
2073
2074 hr = IAVIStream_QueryInterface(pStream, &IID_IAVIEditStream,(LPVOID*)&pEdit);
2075 if (SUCCEEDED(hr) && pEdit != NULL) {
2076 hr = IAVIEditStream_Copy(pEdit, plStart, plLength, ppResult);
2077
2078 IAVIEditStream_Release(pEdit);
2079 } else
2080 hr = AVIERR_UNSUPPORTED;
2081
2082 return hr;
2083 }
2084
2085 /***********************************************************************
2086 * EditStreamCut (AVIFIL32.@)
2087 */
2088 HRESULT WINAPI EditStreamCut(PAVISTREAM pStream, LONG *plStart,
2089 LONG *plLength, PAVISTREAM *ppResult)
2090 {
2091 PAVIEDITSTREAM pEdit = NULL;
2092 HRESULT hr;
2093
2094 TRACE("(%p,%p,%p,%p)\n", pStream, plStart, plLength, ppResult);
2095
2096 if (ppResult != NULL)
2097 *ppResult = NULL;
2098 if (pStream == NULL)
2099 return AVIERR_BADHANDLE;
2100 if (plStart == NULL || plLength == NULL)
2101 return AVIERR_BADPARAM;
2102
2103 hr = IAVIStream_QueryInterface(pStream, &IID_IAVIEditStream,(LPVOID*)&pEdit);
2104 if (SUCCEEDED(hr) && pEdit != NULL) {
2105 hr = IAVIEditStream_Cut(pEdit, plStart, plLength, ppResult);
2106
2107 IAVIEditStream_Release(pEdit);
2108 } else
2109 hr = AVIERR_UNSUPPORTED;
2110
2111 return hr;
2112 }
2113
2114 /***********************************************************************
2115 * EditStreamPaste (AVIFIL32.@)
2116 */
2117 HRESULT WINAPI EditStreamPaste(PAVISTREAM pDest, LONG *plStart, LONG *plLength,
2118 PAVISTREAM pSource, LONG lStart, LONG lEnd)
2119 {
2120 PAVIEDITSTREAM pEdit = NULL;
2121 HRESULT hr;
2122
2123 TRACE("(%p,%p,%p,%p,%d,%d)\n", pDest, plStart, plLength,
2124 pSource, lStart, lEnd);
2125
2126 if (pDest == NULL || pSource == NULL)
2127 return AVIERR_BADHANDLE;
2128 if (plStart == NULL || plLength == NULL || lStart < 0)
2129 return AVIERR_BADPARAM;
2130
2131 hr = IAVIStream_QueryInterface(pDest, &IID_IAVIEditStream,(LPVOID*)&pEdit);
2132 if (SUCCEEDED(hr) && pEdit != NULL) {
2133 hr = IAVIEditStream_Paste(pEdit, plStart, plLength, pSource, lStart, lEnd);
2134
2135 IAVIEditStream_Release(pEdit);
2136 } else
2137 hr = AVIERR_UNSUPPORTED;
2138
2139 return hr;
2140 }
2141
2142 /***********************************************************************
2143 * EditStreamSetInfoA (AVIFIL32.@)
2144 */
2145 HRESULT WINAPI EditStreamSetInfoA(PAVISTREAM pstream, LPAVISTREAMINFOA asi,
2146 LONG size)
2147 {
2148 AVISTREAMINFOW asiw;
2149
2150 TRACE("(%p,%p,%d)\n", pstream, asi, size);
2151
2152 if (pstream == NULL)
2153 return AVIERR_BADHANDLE;
2154 if ((DWORD)size < sizeof(AVISTREAMINFOA))
2155 return AVIERR_BADSIZE;
2156
2157 memcpy(&asiw, asi, sizeof(asiw) - sizeof(asiw.szName));
2158 MultiByteToWideChar(CP_ACP, 0, asi->szName, -1,
2159 asiw.szName, sizeof(asiw.szName)/sizeof(WCHAR));
2160
2161 return EditStreamSetInfoW(pstream, &asiw, sizeof(asiw));
2162 }
2163
2164 /***********************************************************************
2165 * EditStreamSetInfoW (AVIFIL32.@)
2166 */
2167 HRESULT WINAPI EditStreamSetInfoW(PAVISTREAM pstream, LPAVISTREAMINFOW asi,
2168 LONG size)
2169 {
2170 PAVIEDITSTREAM pEdit = NULL;
2171 HRESULT hr;
2172
2173 TRACE("(%p,%p,%d)\n", pstream, asi, size);
2174
2175 hr = IAVIStream_QueryInterface(pstream, &IID_IAVIEditStream,(LPVOID*)&pEdit);
2176 if (SUCCEEDED(hr) && pEdit != NULL) {
2177 hr = IAVIEditStream_SetInfo(pEdit, asi, size);
2178
2179 IAVIEditStream_Release(pEdit);
2180 } else
2181 hr = AVIERR_UNSUPPORTED;
2182
2183 return hr;
2184 }
2185
2186 /***********************************************************************
2187 * EditStreamSetNameA (AVIFIL32.@)
2188 */
2189 HRESULT WINAPI EditStreamSetNameA(PAVISTREAM pstream, LPCSTR szName)
2190 {
2191 AVISTREAMINFOA asia;
2192 HRESULT hres;
2193
2194 TRACE("(%p,%s)\n", pstream, debugstr_a(szName));
2195
2196 if (pstream == NULL)
2197 return AVIERR_BADHANDLE;
2198 if (szName == NULL)
2199 return AVIERR_BADPARAM;
2200
2201 hres = AVIStreamInfoA(pstream, &asia, sizeof(asia));
2202 if (FAILED(hres))
2203 return hres;
2204
2205 memset(asia.szName, 0, sizeof(asia.szName));
2206 lstrcpynA(asia.szName, szName, sizeof(asia.szName)/sizeof(asia.szName[0]));
2207
2208 return EditStreamSetInfoA(pstream, &asia, sizeof(asia));
2209 }
2210
2211 /***********************************************************************
2212 * EditStreamSetNameW (AVIFIL32.@)
2213 */
2214 HRESULT WINAPI EditStreamSetNameW(PAVISTREAM pstream, LPCWSTR szName)
2215 {
2216 AVISTREAMINFOW asiw;
2217 HRESULT hres;
2218
2219 TRACE("(%p,%s)\n", pstream, debugstr_w(szName));
2220
2221 if (pstream == NULL)
2222 return AVIERR_BADHANDLE;
2223 if (szName == NULL)
2224 return AVIERR_BADPARAM;
2225
2226 hres = IAVIStream_Info(pstream, &asiw, sizeof(asiw));
2227 if (FAILED(hres))
2228 return hres;
2229
2230 memset(asiw.szName, 0, sizeof(asiw.szName));
2231 lstrcpynW(asiw.szName, szName, sizeof(asiw.szName)/sizeof(asiw.szName[0]));
2232
2233 return EditStreamSetInfoW(pstream, &asiw, sizeof(asiw));
2234 }
2235
2236 /***********************************************************************
2237 * AVIClearClipboard (AVIFIL32.@)
2238 */
2239 HRESULT WINAPI AVIClearClipboard(void)
2240 {
2241 TRACE("()\n");
2242
2243 return AVIERR_UNSUPPORTED; /* OleSetClipboard(NULL); */
2244 }
2245
2246 /***********************************************************************
2247 * AVIGetFromClipboard (AVIFIL32.@)
2248 */
2249 HRESULT WINAPI AVIGetFromClipboard(PAVIFILE *ppfile)
2250 {
2251 FIXME("(%p), stub!\n", ppfile);
2252
2253 *ppfile = NULL;
2254
2255 return AVIERR_UNSUPPORTED;
2256 }
2257
2258 /***********************************************************************
2259 * AVIMakeStreamFromClipboard (AVIFIL32.@)
2260 */
2261 HRESULT WINAPI AVIMakeStreamFromClipboard(UINT cfFormat, HANDLE hGlobal,
2262 PAVISTREAM * ppstream)
2263 {
2264 FIXME("(0x%08x,%p,%p), stub!\n", cfFormat, hGlobal, ppstream);
2265
2266 if (ppstream == NULL)
2267 return AVIERR_BADHANDLE;
2268
2269 return AVIERR_UNSUPPORTED;
2270 }
2271
2272 /***********************************************************************
2273 * AVIPutFileOnClipboard (AVIFIL32.@)
2274 */
2275 HRESULT WINAPI AVIPutFileOnClipboard(PAVIFILE pfile)
2276 {
2277 FIXME("(%p), stub!\n", pfile);
2278
2279 if (pfile == NULL)
2280 return AVIERR_BADHANDLE;
2281
2282 return AVIERR_UNSUPPORTED;
2283 }
2284
2285 HRESULT WINAPIV AVISaveA(LPCSTR szFile, CLSID * pclsidHandler, AVISAVECALLBACK lpfnCallback,
2286 int nStreams, PAVISTREAM pavi, LPAVICOMPRESSOPTIONS lpOptions, ...)
2287 {
2288 FIXME("(%s,%p,%p,0x%08x,%p,%p), stub!\n", debugstr_a(szFile), pclsidHandler, lpfnCallback,
2289 nStreams, pavi, lpOptions);
2290
2291 return AVIERR_UNSUPPORTED;
2292 }
2293
2294 HRESULT WINAPIV AVISaveW(LPCWSTR szFile, CLSID * pclsidHandler, AVISAVECALLBACK lpfnCallback,
2295 int nStreams, PAVISTREAM pavi, LPAVICOMPRESSOPTIONS lpOptions, ...)
2296 {
2297 FIXME("(%s,%p,%p,0x%08x,%p,%p), stub!\n", debugstr_w(szFile), pclsidHandler, lpfnCallback,
2298 nStreams, pavi, lpOptions);
2299
2300 return AVIERR_UNSUPPORTED;
2301 }