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