shell32: Always NULL-terminate path in SHGetPathFromIDList.
[wine] / dlls / avifil32 / avifile.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 /* TODO:
21  *  - IAVIStreaming interface is missing for the IAVIStreamImpl
22  *  - IAVIStream_fnFindSample: FIND_INDEX isn't supported.
23  *  - IAVIStream_fnReadFormat: formatchanges aren't read in.
24  *  - IAVIStream_fnDelete: a stub.
25  *  - IAVIStream_fnSetInfo: a stub.
26  *  - make thread safe
27  *
28  * KNOWN Bugs:
29  *  - native version can hangup when reading a file generated with this DLL.
30  *    When index is missing it works, but index seems to be okay.
31  */
32
33 #define COM_NO_WINDOWS_H
34 #include <assert.h>
35 #include <stdarg.h>
36
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winnls.h"
42 #include "winerror.h"
43 #include "mmsystem.h"
44 #include "vfw.h"
45
46 #include "avifile_private.h"
47 #include "extrachunk.h"
48
49 #include "wine/unicode.h"
50 #include "wine/debug.h"
51
52 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
53
54 #ifndef IDX_PER_BLOCK
55 #define IDX_PER_BLOCK 2730
56 #endif
57
58 /***********************************************************************/
59
60 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
61 static ULONG   WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
62 static ULONG   WINAPI IAVIFile_fnRelease(IAVIFile* iface);
63 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
64 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
65 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
66 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
67 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
68 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
69 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
70
71 static const struct IAVIFileVtbl iavift = {
72   IAVIFile_fnQueryInterface,
73   IAVIFile_fnAddRef,
74   IAVIFile_fnRelease,
75   IAVIFile_fnInfo,
76   IAVIFile_fnGetStream,
77   IAVIFile_fnCreateStream,
78   IAVIFile_fnWriteData,
79   IAVIFile_fnReadData,
80   IAVIFile_fnEndRecord,
81   IAVIFile_fnDeleteStream
82 };
83
84 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile*iface,REFIID refiid,LPVOID*obj);
85 static ULONG   WINAPI IPersistFile_fnAddRef(IPersistFile*iface);
86 static ULONG   WINAPI IPersistFile_fnRelease(IPersistFile*iface);
87 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile*iface,CLSID*pClassID);
88 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile*iface);
89 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile*iface,LPCOLESTR pszFileName,DWORD dwMode);
90 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile*iface,LPCOLESTR pszFileName,BOOL fRemember);
91 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile*iface,LPCOLESTR pszFileName);
92 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile*iface,LPOLESTR*ppszFileName);
93
94 static const struct IPersistFileVtbl ipersistft = {
95   IPersistFile_fnQueryInterface,
96   IPersistFile_fnAddRef,
97   IPersistFile_fnRelease,
98   IPersistFile_fnGetClassID,
99   IPersistFile_fnIsDirty,
100   IPersistFile_fnLoad,
101   IPersistFile_fnSave,
102   IPersistFile_fnSaveCompleted,
103   IPersistFile_fnGetCurFile
104 };
105
106 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID *obj);
107 static ULONG   WINAPI IAVIStream_fnAddRef(IAVIStream*iface);
108 static ULONG   WINAPI IAVIStream_fnRelease(IAVIStream* iface);
109 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
110 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
111 static LONG    WINAPI IAVIStream_fnFindSample(IAVIStream*iface,LONG pos,LONG flags);
112 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG *formatsize);
113 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
114 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread);
115 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten);
116 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
117 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG *lpread);
118 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG size);
119 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
120
121 static const struct IAVIStreamVtbl iavist = {
122   IAVIStream_fnQueryInterface,
123   IAVIStream_fnAddRef,
124   IAVIStream_fnRelease,
125   IAVIStream_fnCreate,
126   IAVIStream_fnInfo,
127   IAVIStream_fnFindSample,
128   IAVIStream_fnReadFormat,
129   IAVIStream_fnSetFormat,
130   IAVIStream_fnRead,
131   IAVIStream_fnWrite,
132   IAVIStream_fnDelete,
133   IAVIStream_fnReadData,
134   IAVIStream_fnWriteData,
135   IAVIStream_fnSetInfo
136 };
137
138 typedef struct _IAVIFileImpl IAVIFileImpl;
139
140 typedef struct _IPersistFileImpl {
141   /* IUnknown stuff */
142   const IPersistFileVtbl *lpVtbl;
143
144   /* IPersistFile stuff */
145   IAVIFileImpl     *paf;
146 } IPersistFileImpl;
147
148 typedef struct _IAVIStreamImpl {
149   /* IUnknown stuff */
150   const IAVIStreamVtbl *lpVtbl;
151   LONG              ref;
152
153   /* IAVIStream stuff */
154   IAVIFileImpl     *paf;
155   DWORD             nStream;       /* the n-th stream in file */
156   AVISTREAMINFOW    sInfo;
157
158   LPVOID            lpFormat;
159   DWORD             cbFormat;
160
161   LPVOID            lpHandlerData;
162   DWORD             cbHandlerData;
163
164   EXTRACHUNKS       extra;
165
166   LPDWORD           lpBuffer;
167   DWORD             cbBuffer;       /* size of lpBuffer */
168   DWORD             dwCurrentFrame; /* frame/block currently in lpBuffer */
169
170   LONG              lLastFrame;    /* last correct index in idxFrames */
171   AVIINDEXENTRY    *idxFrames;
172   DWORD             nIdxFrames;     /* upper index limit of idxFrames */
173   AVIINDEXENTRY    *idxFmtChanges;
174   DWORD             nIdxFmtChanges; /* upper index limit of idxFmtChanges */
175 } IAVIStreamImpl;
176
177 struct _IAVIFileImpl {
178   /* IUnknown stuff */
179   const IAVIFileVtbl     *lpVtbl;
180   LONG              ref;
181
182   /* IAVIFile stuff... */
183   IPersistFileImpl  iPersistFile;
184
185   AVIFILEINFOW      fInfo;
186   IAVIStreamImpl   *ppStreams[MAX_AVISTREAMS];
187
188   EXTRACHUNKS       fileextra;
189
190   DWORD             dwMoviChunkPos;  /* some stuff for saving ... */
191   DWORD             dwIdxChunkPos;
192   DWORD             dwNextFramePos;
193   DWORD             dwInitialFrames;
194
195   MMCKINFO          ckLastRecord;
196   AVIINDEXENTRY    *idxRecords;      /* won't be updated while loading */
197   DWORD             nIdxRecords;     /* current fill level */
198   DWORD             cbIdxRecords;    /* size of idxRecords */
199
200   /* IPersistFile stuff ... */
201   HMMIO             hmmio;
202   LPWSTR            szFileName;
203   UINT              uMode;
204   BOOL              fDirty;
205 };
206
207 /***********************************************************************/
208
209 static HRESULT AVIFILE_AddFrame(IAVIStreamImpl *This, DWORD ckid, DWORD size,
210                                 DWORD offset, DWORD flags);
211 static HRESULT AVIFILE_AddRecord(IAVIFileImpl *This);
212 static DWORD   AVIFILE_ComputeMoviStart(IAVIFileImpl *This);
213 static void    AVIFILE_ConstructAVIStream(IAVIFileImpl *paf, DWORD nr,
214                                           LPAVISTREAMINFOW asi);
215 static void    AVIFILE_DestructAVIStream(IAVIStreamImpl *This);
216 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
217 static HRESULT AVIFILE_LoadIndex(IAVIFileImpl *This, DWORD size, DWORD offset);
218 static HRESULT AVIFILE_ParseIndex(IAVIFileImpl *This, AVIINDEXENTRY *lp,
219                                   LONG count, DWORD pos, BOOL *bAbsolute);
220 static HRESULT AVIFILE_ReadBlock(IAVIStreamImpl *This, DWORD start,
221                                  LPVOID buffer, LONG size);
222 static void    AVIFILE_SamplesToBlock(IAVIStreamImpl *This, LPLONG pos,
223                                       LPLONG offset);
224 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
225 static HRESULT AVIFILE_SaveIndex(IAVIFileImpl *This);
226 static ULONG   AVIFILE_SearchStream(IAVIFileImpl *This, DWORD fccType,
227                                     LONG lSkip);
228 static void    AVIFILE_UpdateInfo(IAVIFileImpl *This);
229 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
230                                   FOURCC ckid, DWORD flags, LPVOID buffer,
231                                   LONG size);
232
233 HRESULT AVIFILE_CreateAVIFile(REFIID riid, LPVOID *ppv)
234 {
235   IAVIFileImpl *pfile;
236   HRESULT       hr;
237
238   assert(riid != NULL && ppv != NULL);
239
240   *ppv = NULL;
241
242   pfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIFileImpl));
243   if (pfile == NULL)
244     return AVIERR_MEMORY;
245
246   pfile->lpVtbl = &iavift;
247   pfile->ref = 0;
248   pfile->iPersistFile.lpVtbl = &ipersistft;
249   pfile->iPersistFile.paf = pfile;
250
251   hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
252   if (FAILED(hr))
253     HeapFree(GetProcessHeap(), 0, pfile);
254
255   return hr;
256 }
257
258 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
259                                                 LPVOID *obj)
260 {
261   IAVIFileImpl *This = (IAVIFileImpl *)iface;
262
263   TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
264
265   if (IsEqualGUID(&IID_IUnknown, refiid) ||
266       IsEqualGUID(&IID_IAVIFile, refiid)) {
267     *obj = iface;
268     IAVIFile_AddRef(iface);
269
270     return S_OK;
271   } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
272     *obj = &This->iPersistFile;
273     IAVIFile_AddRef(iface);
274
275     return S_OK;
276   }
277
278   return OLE_E_ENUM_NOMORE;
279 }
280
281 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
282 {
283   IAVIFileImpl *This = (IAVIFileImpl *)iface;
284   ULONG ref = InterlockedIncrement(&This->ref);
285
286   TRACE("(%p) -> %ld\n", iface, ref);
287
288   return ref;
289 }
290
291 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
292 {
293   IAVIFileImpl *This = (IAVIFileImpl *)iface;
294   UINT i;
295   ULONG ref = InterlockedDecrement(&This->ref);
296
297   TRACE("(%p) -> %ld\n", iface, ref);
298
299   if (!ref) {
300     if (This->fDirty) {
301       /* need to write headers to file */
302       AVIFILE_SaveFile(This);
303     }
304
305     for (i = 0; i < This->fInfo.dwStreams; i++) {
306       if (This->ppStreams[i] != NULL) {
307         if (This->ppStreams[i]->ref != 0) {
308           ERR(": someone has still %lu reference to stream %u (%p)!\n",
309                This->ppStreams[i]->ref, i, This->ppStreams[i]);
310         }
311         AVIFILE_DestructAVIStream(This->ppStreams[i]);
312         HeapFree(GetProcessHeap(), 0, This->ppStreams[i]);
313         This->ppStreams[i] = NULL;
314       }
315     }
316
317     if (This->idxRecords != NULL) {
318       HeapFree(GetProcessHeap(), 0, This->idxRecords);
319       This->idxRecords  = NULL;
320       This->nIdxRecords = 0;
321     }
322
323     if (This->fileextra.lp != NULL) {
324       HeapFree(GetProcessHeap(), 0, This->fileextra.lp);
325       This->fileextra.lp = NULL;
326       This->fileextra.cb = 0;
327     }
328
329     HeapFree(GetProcessHeap(), 0, This->szFileName);
330     This->szFileName = NULL;
331
332     if (This->hmmio != NULL) {
333       mmioClose(This->hmmio, 0);
334       This->hmmio = NULL;
335     }
336
337     HeapFree(GetProcessHeap(), 0, This);
338   }
339   return ref;
340 }
341
342 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
343                                       LONG size)
344 {
345   IAVIFileImpl *This = (IAVIFileImpl *)iface;
346
347   TRACE("(%p,%p,%ld)\n",iface,afi,size);
348
349   if (afi == NULL)
350     return AVIERR_BADPARAM;
351   if (size < 0)
352     return AVIERR_BADSIZE;
353
354   AVIFILE_UpdateInfo(This);
355
356   memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
357
358   if ((DWORD)size < sizeof(This->fInfo))
359     return AVIERR_BUFFERTOOSMALL;
360   return AVIERR_OK;
361 }
362
363 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
364                                            DWORD fccType, LONG lParam)
365 {
366   IAVIFileImpl *This = (IAVIFileImpl *)iface;
367
368   ULONG nStream;
369
370   TRACE("(%p,%p,0x%08lX,%ld)\n", iface, avis, fccType, lParam);
371
372   if (avis == NULL || lParam < 0)
373     return AVIERR_BADPARAM;
374
375   nStream = AVIFILE_SearchStream(This, fccType, lParam);
376
377   /* Does the requested stream exist? */
378   if (nStream < This->fInfo.dwStreams &&
379       This->ppStreams[nStream] != NULL) {
380     *avis = (PAVISTREAM)This->ppStreams[nStream];
381     IAVIStream_AddRef(*avis);
382
383     return AVIERR_OK;
384   }
385
386   /* Sorry, but the specified stream doesn't exist */
387   return AVIERR_NODATA;
388 }
389
390 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
391                                               LPAVISTREAMINFOW asi)
392 {
393   IAVIFileImpl *This = (IAVIFileImpl *)iface;
394
395   DWORD n;
396
397   TRACE("(%p,%p,%p)\n", iface, avis, asi);
398
399   /* check parameters */
400   if (avis == NULL || asi == NULL)
401     return AVIERR_BADPARAM;
402
403   *avis = NULL;
404
405   /* Does the user have write permission? */
406   if ((This->uMode & MMIO_RWMODE) == 0)
407     return AVIERR_READONLY;
408
409   /* Can we add another stream? */
410   n = This->fInfo.dwStreams;
411   if (n >= MAX_AVISTREAMS || This->dwMoviChunkPos != 0) {
412     /* already reached max nr of streams
413      * or have already written frames to disk */
414     return AVIERR_UNSUPPORTED;
415   }
416
417   /* check AVISTREAMINFO for some really needed things */
418   if (asi->fccType == 0 || asi->dwScale == 0 || asi->dwRate == 0)
419     return AVIERR_BADFORMAT;
420
421   /* now it seems to be save to add the stream */
422   assert(This->ppStreams[n] == NULL);
423   This->ppStreams[n] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
424                                                    sizeof(IAVIStreamImpl));
425   if (This->ppStreams[n] == NULL)
426     return AVIERR_MEMORY;
427
428   /* initialize the new allocated stream */
429   AVIFILE_ConstructAVIStream(This, n, asi);
430
431   This->fInfo.dwStreams++;
432   This->fDirty = TRUE;
433
434   /* update our AVIFILEINFO structure */
435   AVIFILE_UpdateInfo(This);
436
437   /* return it */
438   *avis = (PAVISTREAM)This->ppStreams[n];
439   IAVIStream_AddRef(*avis);
440
441   return AVIERR_OK;
442 }
443
444 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
445                                            LPVOID lpData, LONG size)
446 {
447   IAVIFileImpl *This = (IAVIFileImpl *)iface;
448
449   TRACE("(%p,0x%08lX,%p,%ld)\n", iface, ckid, lpData, size);
450
451   /* check parameters */
452   if (lpData == NULL)
453     return AVIERR_BADPARAM;
454   if (size < 0)
455     return AVIERR_BADSIZE;
456
457   /* Do we have write permission? */
458   if ((This->uMode & MMIO_RWMODE) == 0)
459     return AVIERR_READONLY;
460
461   This->fDirty = TRUE;
462
463   return WriteExtraChunk(&This->fileextra, ckid, lpData, size);
464 }
465
466 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
467                                           LPVOID lpData, LONG *size)
468 {
469   IAVIFileImpl *This = (IAVIFileImpl *)iface;
470
471   TRACE("(%p,0x%08lX,%p,%p)\n", iface, ckid, lpData, size);
472
473   return ReadExtraChunk(&This->fileextra, ckid, lpData, size);
474 }
475
476 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
477 {
478   IAVIFileImpl *This = (IAVIFileImpl *)iface;
479
480   TRACE("(%p)\n",iface);
481
482   if ((This->uMode & MMIO_RWMODE) == 0)
483     return AVIERR_READONLY;
484
485   This->fDirty = TRUE;
486
487   /* no frames written to any stream? -- compute start of 'movi'-chunk */
488   if (This->dwMoviChunkPos == 0)
489     AVIFILE_ComputeMoviStart(This);
490
491   This->fInfo.dwFlags  |= AVIFILEINFO_ISINTERLEAVED;
492
493   /* already written frames to any stream, ... */
494   if (This->ckLastRecord.dwFlags & MMIO_DIRTY) {
495     /* close last record */
496     if (mmioAscend(This->hmmio, &This->ckLastRecord, 0) != 0)
497       return AVIERR_FILEWRITE;
498
499     AVIFILE_AddRecord(This);
500
501     if (This->fInfo.dwSuggestedBufferSize < This->ckLastRecord.cksize + 3 * sizeof(DWORD))
502       This->fInfo.dwSuggestedBufferSize = This->ckLastRecord.cksize + 3 * sizeof(DWORD);
503   }
504
505   /* write out a new record into file, but don't close it */
506   This->ckLastRecord.cksize  = 0;
507   This->ckLastRecord.fccType = listtypeAVIRECORD;
508   if (mmioSeek(This->hmmio, This->dwNextFramePos, SEEK_SET) == -1)
509     return AVIERR_FILEWRITE;
510   if (mmioCreateChunk(This->hmmio, &This->ckLastRecord, MMIO_CREATELIST) != 0)
511     return AVIERR_FILEWRITE;
512   This->dwNextFramePos += 3 * sizeof(DWORD);
513
514   return AVIERR_OK;
515 }
516
517 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
518                                               LONG lParam)
519 {
520   IAVIFileImpl *This = (IAVIFileImpl *)iface;
521
522   ULONG nStream;
523
524   TRACE("(%p,0x%08lX,%ld)\n", iface, fccType, lParam);
525
526   /* check parameter */
527   if (lParam < 0)
528     return AVIERR_BADPARAM;
529
530   /* Have user write permissions? */
531   if ((This->uMode & MMIO_RWMODE) == 0)
532     return AVIERR_READONLY;
533
534   nStream = AVIFILE_SearchStream(This, fccType, lParam);
535
536   /* Does the requested stream exist? */
537   if (nStream < This->fInfo.dwStreams &&
538       This->ppStreams[nStream] != NULL) {
539     /* ... so delete it now */
540     HeapFree(GetProcessHeap(), 0, This->ppStreams[nStream]);
541
542     if (This->fInfo.dwStreams - nStream > 0)
543       memcpy(This->ppStreams + nStream, This->ppStreams + nStream + 1,
544              (This->fInfo.dwStreams - nStream) * sizeof(IAVIStreamImpl*));
545
546     This->ppStreams[This->fInfo.dwStreams] = NULL;
547     This->fInfo.dwStreams--;
548     This->fDirty = TRUE;
549
550     /* This->fInfo will be updated further when asked for */
551     return AVIERR_OK;
552   } else
553     return AVIERR_NODATA;
554 }
555
556 /***********************************************************************/
557
558 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
559                                                     REFIID refiid, LPVOID *obj)
560 {
561   IPersistFileImpl *This = (IPersistFileImpl *)iface;
562
563   assert(This->paf != NULL);
564
565   return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
566 }
567
568 static ULONG   WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
569 {
570   IPersistFileImpl *This = (IPersistFileImpl *)iface;
571
572   assert(This->paf != NULL);
573
574   return IAVIFile_AddRef((PAVIFILE)This->paf);
575 }
576
577 static ULONG   WINAPI IPersistFile_fnRelease(IPersistFile *iface)
578 {
579   IPersistFileImpl *This = (IPersistFileImpl *)iface;
580
581   assert(This->paf != NULL);
582
583   return IAVIFile_Release((PAVIFILE)This->paf);
584 }
585
586 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
587                                                 LPCLSID pClassID)
588 {
589   TRACE("(%p,%p)\n", iface, pClassID);
590
591   if (pClassID == NULL)
592     return AVIERR_BADPARAM;
593
594   memcpy(pClassID, &CLSID_AVIFile, sizeof(CLSID_AVIFile));
595
596   return AVIERR_OK;
597 }
598
599 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
600 {
601   IPersistFileImpl *This = (IPersistFileImpl *)iface;
602
603   TRACE("(%p)\n", iface);
604
605   assert(This->paf != NULL);
606
607   return (This->paf->fDirty ? S_OK : S_FALSE);
608 }
609
610 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
611                                           LPCOLESTR pszFileName, DWORD dwMode)
612 {
613   IPersistFileImpl *This = (IPersistFileImpl *)iface;
614
615   int len;
616
617   TRACE("(%p,%s,0x%08lX)\n", iface, debugstr_w(pszFileName), dwMode);
618
619   /* check parameter */
620   if (pszFileName == NULL)
621     return AVIERR_BADPARAM;
622
623   assert(This->paf != NULL);
624   if (This->paf->hmmio != NULL)
625     return AVIERR_ERROR; /* No reuse of this object for another file! */
626
627   /* remeber mode and name */
628   This->paf->uMode = dwMode;
629
630   len = lstrlenW(pszFileName) + 1;
631   This->paf->szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
632   if (This->paf->szFileName == NULL)
633     return AVIERR_MEMORY;
634   lstrcpyW(This->paf->szFileName, pszFileName);
635
636   /* try to open the file */
637   This->paf->hmmio = mmioOpenW(This->paf->szFileName, NULL,
638                                MMIO_ALLOCBUF | dwMode);
639   if (This->paf->hmmio == NULL) {
640     /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
641     LPSTR szFileName;
642
643     len = WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1,
644                                NULL, 0, NULL, NULL);
645     szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR));
646     if (szFileName == NULL)
647       return AVIERR_MEMORY;
648
649     WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1, szFileName,
650                         len, NULL, NULL);
651
652     This->paf->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
653     HeapFree(GetProcessHeap(), 0, szFileName);
654     if (This->paf->hmmio == NULL)
655       return AVIERR_FILEOPEN;
656   }
657
658   /* should we create a new file? */
659   if (dwMode & OF_CREATE) {
660     memset(& This->paf->fInfo, 0, sizeof(This->paf->fInfo));
661     This->paf->fInfo.dwFlags = AVIFILEINFO_HASINDEX | AVIFILEINFO_TRUSTCKTYPE;
662
663     return AVIERR_OK;
664   } else
665     return AVIFILE_LoadFile(This->paf);
666 }
667
668 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
669                                           LPCOLESTR pszFileName,BOOL fRemember)
670 {
671   TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
672
673   /* We write directly to disk, so nothing to do. */
674
675   return AVIERR_OK;
676 }
677
678 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
679                                                    LPCOLESTR pszFileName)
680 {
681   TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
682
683   /* We write directly to disk, so nothing to do. */
684
685   return AVIERR_OK;
686 }
687
688 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
689                                                 LPOLESTR *ppszFileName)
690 {
691   IPersistFileImpl *This = (IPersistFileImpl *)iface;
692
693   TRACE("(%p,%p)\n", iface, ppszFileName);
694
695   if (ppszFileName == NULL)
696     return AVIERR_BADPARAM;
697
698   *ppszFileName = NULL;
699
700   assert(This->paf != NULL);
701
702   if (This->paf->szFileName != NULL) {
703     int len = lstrlenW(This->paf->szFileName) + 1;
704
705     *ppszFileName = CoTaskMemAlloc(len * sizeof(WCHAR));
706     if (*ppszFileName == NULL)
707       return AVIERR_MEMORY;
708
709     strcpyW(*ppszFileName, This->paf->szFileName);
710   }
711
712   return AVIERR_OK;
713 }
714
715 /***********************************************************************/
716
717 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
718                                                   REFIID refiid, LPVOID *obj)
719 {
720   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
721
722   TRACE("(%p,%s,%p)\n", iface, debugstr_guid(refiid), obj);
723
724   if (IsEqualGUID(&IID_IUnknown, refiid) ||
725       IsEqualGUID(&IID_IAVIStream, refiid)) {
726     *obj = This;
727     IAVIStream_AddRef(iface);
728
729     return S_OK;
730   }
731   /* FIXME: IAVIStreaming interface */
732
733   return OLE_E_ENUM_NOMORE;
734 }
735
736 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
737 {
738   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
739   ULONG ref = InterlockedIncrement(&This->ref);
740
741   TRACE("(%p) -> %ld\n", iface, ref);
742
743   /* also add ref to parent, so that it doesn't kill us */
744   if (This->paf != NULL)
745     IAVIFile_AddRef((PAVIFILE)This->paf);
746
747   return ref;
748 }
749
750 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
751 {
752   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
753   ULONG ref = InterlockedDecrement(&This->ref);
754
755   TRACE("(%p) -> %ld\n", iface, ref);
756
757   if (This->paf != NULL)
758     IAVIFile_Release((PAVIFILE)This->paf);
759
760   return ref;
761 }
762
763 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
764                                           LPARAM lParam2)
765 {
766   TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
767
768   /* This IAVIStream interface needs an AVIFile */
769   return AVIERR_UNSUPPORTED;
770 }
771
772 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
773                                         LONG size)
774 {
775   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
776
777   TRACE("(%p,%p,%ld)\n", iface, psi, size);
778
779   if (psi == NULL)
780     return AVIERR_BADPARAM;
781   if (size < 0)
782     return AVIERR_BADSIZE;
783
784   memcpy(psi, &This->sInfo, min((DWORD)size, sizeof(This->sInfo)));
785
786   if ((DWORD)size < sizeof(This->sInfo))
787     return AVIERR_BUFFERTOOSMALL;
788   return AVIERR_OK;
789 }
790
791 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
792                                            LONG flags)
793 {
794   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
795
796   LONG offset = 0;
797
798   TRACE("(%p,%ld,0x%08lX)\n",iface,pos,flags);
799
800   if (flags & FIND_FROM_START) {
801     pos = This->sInfo.dwStart;
802     flags &= ~(FIND_FROM_START|FIND_PREV);
803     flags |= FIND_NEXT;
804   }
805
806   if (This->sInfo.dwSampleSize != 0) {
807     /* convert samples into block number with offset */
808     AVIFILE_SamplesToBlock(This, &pos, &offset);
809   }
810
811   if (flags & FIND_TYPE) {
812     if (flags & FIND_KEY) {
813       while (0 <= pos && pos <= This->lLastFrame) {
814         if (This->idxFrames[pos].dwFlags & AVIIF_KEYFRAME)
815           goto RETURN_FOUND;
816
817         if (flags & FIND_NEXT)
818           pos++;
819         else
820           pos--;
821       };
822     } else if (flags & FIND_ANY) {
823       while (0 <= pos && pos <= This->lLastFrame) {
824         if (This->idxFrames[pos].dwChunkLength > 0)
825           goto RETURN_FOUND;
826
827         if (flags & FIND_NEXT)
828           pos++;
829         else
830           pos--;
831
832       };
833     } else if ((flags & FIND_FORMAT) && This->idxFmtChanges != NULL &&
834                This->sInfo.fccType == streamtypeVIDEO) {
835       if (flags & FIND_NEXT) {
836         ULONG n;
837
838         for (n = 0; n < This->sInfo.dwFormatChangeCount; n++)
839           if (This->idxFmtChanges[n].ckid >= pos) {
840             pos = This->idxFmtChanges[n].ckid;
841             goto RETURN_FOUND;
842           }
843       } else {
844         LONG n;
845
846         for (n = (LONG)This->sInfo.dwFormatChangeCount; n >= 0; n--) {
847           if (This->idxFmtChanges[n].ckid <= pos) {
848             pos = This->idxFmtChanges[n].ckid;
849             goto RETURN_FOUND;
850           }
851         }
852
853         if (pos > (LONG)This->sInfo.dwStart)
854           return 0; /* format changes always for first frame */
855       }
856     }
857
858     return -1;
859   }
860
861  RETURN_FOUND:
862   if (pos < (LONG)This->sInfo.dwStart)
863     return -1;
864
865   switch (flags & FIND_RET) {
866   case FIND_LENGTH:
867     /* physical size */
868     pos = This->idxFrames[pos].dwChunkLength;
869     break;
870   case FIND_OFFSET:
871     /* physical position */
872     pos = This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD)
873       + offset * This->sInfo.dwSampleSize;
874     break;
875   case FIND_SIZE:
876     /* logical size */
877     if (This->sInfo.dwSampleSize)
878       pos = This->sInfo.dwSampleSize;
879     else
880       pos = 1;
881     break;
882   case FIND_INDEX:
883     FIXME(": FIND_INDEX flag is not supported!\n");
884     /* This is an index in the index-table on disc. */
885     break;
886   }; /* else logical position */
887
888   return pos;
889 }
890
891 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
892                                               LPVOID format, LONG *formatsize)
893 {
894   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
895
896   TRACE("(%p,%ld,%p,%p)\n", iface, pos, format, formatsize);
897
898   if (formatsize == NULL)
899     return AVIERR_BADPARAM;
900
901   /* only interested in needed buffersize? */
902   if (format == NULL || *formatsize <= 0) {
903     *formatsize = This->cbFormat;
904
905     return AVIERR_OK;
906   }
907
908   /* copy initial format (only as much as will fit) */
909   memcpy(format, This->lpFormat, min(*(DWORD*)formatsize, This->cbFormat));
910   if (*(DWORD*)formatsize < This->cbFormat) {
911     *formatsize = This->cbFormat;
912     return AVIERR_BUFFERTOOSMALL;
913   }
914
915   /* Could format change? When yes will it change? */
916   if ((This->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
917       pos > This->sInfo.dwStart) {
918     LONG lLastFmt;
919
920     lLastFmt = IAVIStream_fnFindSample(iface, pos, FIND_FORMAT|FIND_PREV);
921     if (lLastFmt > 0) {
922       FIXME(": need to read formatchange for %ld -- unimplemented!\n",lLastFmt);
923     }
924   }
925
926   *formatsize = This->cbFormat;
927   return AVIERR_OK;
928 }
929
930 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
931                                              LPVOID format, LONG formatsize)
932 {
933   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
934
935   LPBITMAPINFOHEADER lpbiNew = (LPBITMAPINFOHEADER)format;
936
937   TRACE("(%p,%ld,%p,%ld)\n", iface, pos, format, formatsize);
938
939   /* check parameters */
940   if (format == NULL || formatsize <= 0)
941     return AVIERR_BADPARAM;
942
943   /* Do we have write permission? */
944   if ((This->paf->uMode & MMIO_RWMODE) == 0)
945     return AVIERR_READONLY;
946
947   /* can only set format before frame is written! */
948   if (This->lLastFrame > pos)
949     return AVIERR_UNSUPPORTED;
950
951   /* initial format or a formatchange? */
952   if (This->lpFormat == NULL) {
953     /* initial format */
954     if (This->paf->dwMoviChunkPos != 0)
955       return AVIERR_ERROR; /* user has used API in wrong sequnece! */
956
957     This->lpFormat = HeapAlloc(GetProcessHeap(), 0, formatsize);
958     if (This->lpFormat == NULL)
959       return AVIERR_MEMORY;
960     This->cbFormat = formatsize;
961
962     memcpy(This->lpFormat, format, formatsize);
963
964     /* update some infos about stream */
965     if (This->sInfo.fccType == streamtypeVIDEO) {
966       LONG lDim;
967
968       lDim = This->sInfo.rcFrame.right - This->sInfo.rcFrame.left;
969       if (lDim < lpbiNew->biWidth)
970         This->sInfo.rcFrame.right = This->sInfo.rcFrame.left + lpbiNew->biWidth;
971       lDim = This->sInfo.rcFrame.bottom - This->sInfo.rcFrame.top;
972       if (lDim < lpbiNew->biHeight)
973         This->sInfo.rcFrame.bottom = This->sInfo.rcFrame.top + lpbiNew->biHeight;
974     } else if (This->sInfo.fccType == streamtypeAUDIO)
975       This->sInfo.dwSampleSize = ((LPWAVEFORMATEX)This->lpFormat)->nBlockAlign;
976
977     return AVIERR_OK;
978   } else {
979     MMCKINFO           ck;
980     LPBITMAPINFOHEADER lpbiOld = (LPBITMAPINFOHEADER)This->lpFormat;
981     RGBQUAD           *rgbNew  = (RGBQUAD*)((LPBYTE)lpbiNew + lpbiNew->biSize);
982     AVIPALCHANGE      *lppc = NULL;
983     UINT               n;
984
985     /* perhaps format change, check it ... */
986     if (This->cbFormat != formatsize)
987       return AVIERR_UNSUPPORTED;
988
989     /* no format change, only the initial one */
990     if (memcmp(This->lpFormat, format, formatsize) == 0)
991       return AVIERR_OK;
992
993     /* check that's only the palette, which changes */
994     if (lpbiOld->biSize        != lpbiNew->biSize ||
995         lpbiOld->biWidth       != lpbiNew->biWidth ||
996         lpbiOld->biHeight      != lpbiNew->biHeight ||
997         lpbiOld->biPlanes      != lpbiNew->biPlanes ||
998         lpbiOld->biBitCount    != lpbiNew->biBitCount ||
999         lpbiOld->biCompression != lpbiNew->biCompression ||
1000         lpbiOld->biClrUsed     != lpbiNew->biClrUsed)
1001       return AVIERR_UNSUPPORTED;
1002
1003     This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
1004
1005     /* simply say all colors have changed */
1006     ck.ckid   = MAKEAVICKID(cktypePALchange, This->nStream);
1007     ck.cksize = 2 * sizeof(WORD) + lpbiOld->biClrUsed * sizeof(PALETTEENTRY);
1008     lppc = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1009     if (lppc == NULL)
1010       return AVIERR_MEMORY;
1011
1012     lppc->bFirstEntry = 0;
1013     lppc->bNumEntries = (lpbiOld->biClrUsed < 256 ? lpbiOld->biClrUsed : 0);
1014     lppc->wFlags      = 0;
1015     for (n = 0; n < lpbiOld->biClrUsed; n++) {
1016       lppc->peNew[n].peRed   = rgbNew[n].rgbRed;
1017       lppc->peNew[n].peGreen = rgbNew[n].rgbGreen;
1018       lppc->peNew[n].peBlue  = rgbNew[n].rgbBlue;
1019       lppc->peNew[n].peFlags = 0;
1020     }
1021
1022     if (mmioSeek(This->paf->hmmio, This->paf->dwNextFramePos, SEEK_SET) == -1)
1023       return AVIERR_FILEWRITE;
1024     if (mmioCreateChunk(This->paf->hmmio, &ck, 0) != S_OK)
1025       return AVIERR_FILEWRITE;
1026     if (mmioWrite(This->paf->hmmio, (HPSTR)lppc, ck.cksize) != ck.cksize)
1027       return AVIERR_FILEWRITE;
1028     if (mmioAscend(This->paf->hmmio, &ck, 0) != S_OK)
1029       return AVIERR_FILEWRITE;
1030     This->paf->dwNextFramePos += ck.cksize + 2 * sizeof(DWORD);
1031
1032     HeapFree(GetProcessHeap(), 0, lppc);
1033
1034     return AVIFILE_AddFrame(This, cktypePALchange, n, ck.dwDataOffset, 0);
1035   }
1036 }
1037
1038 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
1039                                         LONG samples, LPVOID buffer,
1040                                         LONG buffersize, LPLONG bytesread,
1041                                         LPLONG samplesread)
1042 {
1043   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1044
1045   DWORD    size;
1046   HRESULT  hr;
1047
1048   TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface, start, samples, buffer,
1049         buffersize, bytesread, samplesread);
1050
1051   /* clear return parameters if given */
1052   if (bytesread != NULL)
1053     *bytesread = 0;
1054   if (samplesread != NULL)
1055     *samplesread = 0;
1056
1057   /* check parameters */
1058   if ((LONG)This->sInfo.dwStart > start)
1059     return AVIERR_NODATA; /* couldn't read before start of stream */
1060   if (This->sInfo.dwStart + This->sInfo.dwLength < (DWORD)start)
1061     return AVIERR_NODATA; /* start is past end of stream */
1062
1063   /* should we read as much as possible? */
1064   if (samples == -1) {
1065     /* User should know how much we have read */
1066     if (bytesread == NULL && samplesread == NULL)
1067       return AVIERR_BADPARAM;
1068
1069     if (This->sInfo.dwSampleSize != 0)
1070       samples = buffersize / This->sInfo.dwSampleSize;
1071     else
1072       samples = 1;
1073   }
1074
1075   /* limit to end of stream */
1076   if ((LONG)This->sInfo.dwLength < samples)
1077     samples = This->sInfo.dwLength;
1078   if ((start - This->sInfo.dwStart) > (This->sInfo.dwLength - samples))
1079     samples = This->sInfo.dwLength - (start - This->sInfo.dwStart);
1080
1081   /* nothing to read? Then leave ... */
1082   if (samples == 0)
1083     return AVIERR_OK;
1084
1085   if (This->sInfo.dwSampleSize != 0) {
1086     /* fixed samplesize -- we can read over frame/block boundaries */
1087     LONG block = start;
1088     LONG offset = 0;
1089
1090     /* convert start sample to block,offset pair */
1091     AVIFILE_SamplesToBlock(This, &block, &offset);
1092
1093     /* convert samples to bytes */
1094     samples *= This->sInfo.dwSampleSize;
1095
1096     while (samples > 0 && buffersize > 0) {
1097       if (block != This->dwCurrentFrame) {
1098         hr = AVIFILE_ReadBlock(This, block, NULL, 0);
1099         if (FAILED(hr))
1100           return hr;
1101       }
1102
1103       size = min((DWORD)samples, (DWORD)buffersize);
1104       size = min(size, This->cbBuffer - offset);
1105       memcpy(buffer, ((BYTE*)&This->lpBuffer[2]) + offset, size);
1106
1107       block++;
1108       offset = 0;
1109       buffer = ((LPBYTE)buffer)+size;
1110       samples    -= size;
1111       buffersize -= size;
1112
1113       /* fill out return parameters if given */
1114       if (bytesread != NULL)
1115         *bytesread   += size;
1116       if (samplesread != NULL)
1117         *samplesread += size / This->sInfo.dwSampleSize;
1118     }
1119
1120     if (samples == 0)
1121       return AVIERR_OK;
1122     else
1123       return AVIERR_BUFFERTOOSMALL;
1124   } else {
1125     /* variable samplesize -- we can only read one full frame/block */
1126     if (samples > 1)
1127       samples = 1;
1128
1129     assert(start <= This->lLastFrame);
1130     size = This->idxFrames[start].dwChunkLength;
1131     if (buffer != NULL && buffersize >= size) {
1132       hr = AVIFILE_ReadBlock(This, start, buffer, size);
1133       if (FAILED(hr))
1134         return hr;
1135     } else if (buffer != NULL)
1136       return AVIERR_BUFFERTOOSMALL;
1137
1138     /* fill out return parameters if given */
1139     if (bytesread != NULL)
1140       *bytesread = size;
1141     if (samplesread != NULL)
1142       *samplesread = samples;
1143
1144     return AVIERR_OK;
1145   }
1146 }
1147
1148 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
1149                                          LONG samples, LPVOID buffer,
1150                                          LONG buffersize, DWORD flags,
1151                                          LPLONG sampwritten,
1152                                          LPLONG byteswritten)
1153 {
1154   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1155
1156   FOURCC  ckid;
1157   HRESULT hr;
1158
1159   TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface, start, samples,
1160         buffer, buffersize, flags, sampwritten, byteswritten);
1161
1162   /* clear return parameters if given */
1163   if (sampwritten != NULL)
1164     *sampwritten = 0;
1165   if (byteswritten != NULL)
1166     *byteswritten = 0;
1167
1168   /* check parameters */
1169   if (buffer == NULL && (buffersize > 0 || samples > 0))
1170     return AVIERR_BADPARAM;
1171
1172   /* Have we write permission? */
1173   if ((This->paf->uMode & MMIO_RWMODE) == 0)
1174     return AVIERR_READONLY;
1175
1176   switch (This->sInfo.fccType) {
1177   case streamtypeAUDIO:
1178     ckid = MAKEAVICKID(cktypeWAVEbytes, This->nStream);
1179     break;
1180   default:
1181     if ((flags & AVIIF_KEYFRAME) && buffersize != 0)
1182       ckid = MAKEAVICKID(cktypeDIBbits, This->nStream);
1183     else
1184       ckid = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1185     break;
1186   };
1187
1188   /* append to end of stream? */
1189   if (start == -1) {
1190     if (This->lLastFrame == -1)
1191       start = This->sInfo.dwStart;
1192     else
1193       start = This->sInfo.dwLength;
1194   } else if (This->lLastFrame == -1)
1195     This->sInfo.dwStart = start;
1196
1197   if (This->sInfo.dwSampleSize != 0) {
1198     /* fixed sample size -- audio like */
1199     if (samples * This->sInfo.dwSampleSize != buffersize)
1200       return AVIERR_BADPARAM;
1201
1202     /* Couldn't skip audio-like data -- User must supply appropriate silence */
1203     if (This->sInfo.dwLength != start)
1204       return AVIERR_UNSUPPORTED;
1205
1206     /* Convert position to frame/block */
1207     start = This->lLastFrame + 1;
1208
1209     if ((This->paf->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) == 0) {
1210       FIXME(": not interleaved, could collect audio data!\n");
1211     }
1212   } else {
1213     /* variable sample size -- video like */
1214     if (samples > 1)
1215       return AVIERR_UNSUPPORTED;
1216
1217     /* must we fill up with empty frames? */
1218     if (This->lLastFrame != -1) {
1219       FOURCC ckid2 = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1220
1221       while (start > This->lLastFrame + 1) {
1222         hr = AVIFILE_WriteBlock(This, This->lLastFrame + 1, ckid2, 0, NULL, 0);
1223         if (FAILED(hr))
1224           return hr;
1225       }
1226     }
1227   }
1228
1229   /* write the block now */
1230   hr = AVIFILE_WriteBlock(This, start, ckid, flags, buffer, buffersize);
1231   if (SUCCEEDED(hr)) {
1232     /* fill out return parameters if given */
1233     if (sampwritten != NULL)
1234       *sampwritten = samples;
1235     if (byteswritten != NULL)
1236       *byteswritten = buffersize;
1237   }
1238
1239   return hr;
1240 }
1241
1242 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
1243                                           LONG samples)
1244 {
1245   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1246
1247   FIXME("(%p,%ld,%ld): stub\n", iface, start, samples);
1248
1249   /* check parameters */
1250   if (start < 0 || samples < 0)
1251     return AVIERR_BADPARAM;
1252
1253   /* Delete before start of stream? */
1254   if (start + samples < This->sInfo.dwStart)
1255     return AVIERR_OK;
1256
1257   /* Delete after end of stream? */
1258   if (start > This->sInfo.dwLength)
1259     return AVIERR_OK;
1260
1261   /* For the rest we need write permissions */
1262   if ((This->paf->uMode & MMIO_RWMODE) == 0)
1263     return AVIERR_READONLY;
1264
1265   /* 1. overwrite the data with JUNK
1266    *
1267    * if ISINTERLEAVED {
1268    *   2. concat all neighboured JUNK-blocks in this record to one
1269    *   3. if this record only contains JUNK and is at end set dwNextFramePos
1270    *      to start of this record, repeat this.
1271    * } else {
1272    *   2. concat all neighboured JUNK-blocks.
1273    *   3. if the JUNK block is at the end, then set dwNextFramePos to
1274    *      start of this block.
1275    * }
1276    */
1277
1278   return AVIERR_UNSUPPORTED;
1279 }
1280
1281 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1282                                             LPVOID lp, LPLONG lpread)
1283 {
1284   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1285
1286   TRACE("(%p,0x%08lX,%p,%p)\n", iface, fcc, lp, lpread);
1287
1288   if (fcc == ckidSTREAMHANDLERDATA) {
1289     if (This->lpHandlerData != NULL && This->cbHandlerData > 0) {
1290       if (lp == NULL || *lpread <= 0) {
1291         *lpread = This->cbHandlerData;
1292         return AVIERR_OK;
1293       }
1294
1295       memcpy(lp, This->lpHandlerData, min(This->cbHandlerData, *lpread));
1296       if (*lpread < This->cbHandlerData)
1297         return AVIERR_BUFFERTOOSMALL;
1298       return AVIERR_OK;
1299     } else
1300       return AVIERR_NODATA;
1301   } else
1302     return ReadExtraChunk(&This->extra, fcc, lp, lpread);
1303 }
1304
1305 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1306                                              LPVOID lp, LONG size)
1307 {
1308   IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1309
1310   TRACE("(%p,0x%08lx,%p,%ld)\n", iface, fcc, lp, size);
1311
1312   /* check parameters */
1313   if (lp == NULL)
1314     return AVIERR_BADPARAM;
1315   if (size <= 0)
1316     return AVIERR_BADSIZE;
1317
1318   /* need write permission */
1319   if ((This->paf->uMode & MMIO_RWMODE) == 0)
1320     return AVIERR_READONLY;
1321
1322   /* already written something to this file? */
1323   if (This->paf->dwMoviChunkPos != 0) {
1324     /* the data will be inserted before the 'movi' chunk, so check for
1325      * enough space */
1326     DWORD dwPos = AVIFILE_ComputeMoviStart(This->paf);
1327
1328     /* ckid,size => 2 * sizeof(DWORD) */
1329     dwPos += 2 * sizeof(DWORD) + size;
1330     if (size >= This->paf->dwMoviChunkPos - 2 * sizeof(DWORD))
1331       return AVIERR_UNSUPPORTED; /* not enough space left */
1332   }
1333
1334   This->paf->fDirty = TRUE;
1335
1336   if (fcc == ckidSTREAMHANDLERDATA) {
1337     if (This->lpHandlerData != NULL) {
1338       FIXME(": handler data already set -- overwirte?\n");
1339       return AVIERR_UNSUPPORTED;
1340     }
1341
1342     This->lpHandlerData = HeapAlloc(GetProcessHeap(), 0, size);
1343     if (This->lpHandlerData == NULL)
1344       return AVIERR_MEMORY;
1345     This->cbHandlerData = size;
1346     memcpy(This->lpHandlerData, lp, size);
1347
1348     return AVIERR_OK;
1349   } else
1350     return WriteExtraChunk(&This->extra, fcc, lp, size);
1351 }
1352
1353 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1354                                            LPAVISTREAMINFOW info, LONG infolen)
1355 {
1356   FIXME("(%p,%p,%ld): stub\n", iface, info, infolen);
1357
1358   return E_FAIL;
1359 }
1360
1361 /***********************************************************************/
1362
1363 static HRESULT AVIFILE_AddFrame(IAVIStreamImpl *This, DWORD ckid, DWORD size, DWORD offset, DWORD flags)
1364 {
1365   /* pre-conditions */
1366   assert(This != NULL);
1367
1368   switch (TWOCCFromFOURCC(ckid)) {
1369   case cktypeDIBbits:
1370     if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1371       flags |= AVIIF_KEYFRAME;
1372     break;
1373   case cktypeDIBcompressed:
1374     if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1375       flags &= ~AVIIF_KEYFRAME;
1376     break;
1377   case cktypePALchange:
1378     if (This->sInfo.fccType != streamtypeVIDEO) {
1379       ERR(": found palette change in non-video stream!\n");
1380       return AVIERR_BADFORMAT;
1381     }
1382     This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
1383     This->sInfo.dwFormatChangeCount++;
1384
1385     if (This->idxFmtChanges == NULL || This->sInfo.dwFormatChangeCount < This->nIdxFmtChanges) {
1386       UINT n = This->sInfo.dwFormatChangeCount;
1387
1388       This->nIdxFmtChanges += 16;
1389       if (This->idxFmtChanges == NULL)
1390         This->idxFmtChanges =
1391           HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nIdxFmtChanges * sizeof(AVIINDEXENTRY));
1392       else
1393         This->idxFmtChanges =
1394           HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->idxFmtChanges,
1395                            This->nIdxFmtChanges * sizeof(AVIINDEXENTRY));
1396       if (This->idxFmtChanges == NULL)
1397         return AVIERR_MEMORY;
1398
1399       This->idxFmtChanges[n].ckid          = This->lLastFrame;
1400       This->idxFmtChanges[n].dwFlags       = 0;
1401       This->idxFmtChanges[n].dwChunkOffset = offset;
1402       This->idxFmtChanges[n].dwChunkLength = size;
1403
1404       return AVIERR_OK;
1405     }
1406     break;
1407   case cktypeWAVEbytes:
1408     if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1409       flags |= AVIIF_KEYFRAME;
1410     break;
1411   default:
1412     WARN(": unknown TWOCC 0x%04X found\n", TWOCCFromFOURCC(ckid));
1413     break;
1414   };
1415
1416   /* first frame is alwasy a keyframe */
1417   if (This->lLastFrame == -1)
1418     flags |= AVIIF_KEYFRAME;
1419
1420   if (This->sInfo.dwSuggestedBufferSize < size)
1421     This->sInfo.dwSuggestedBufferSize = size;
1422
1423   /* get memory for index */
1424   if (This->idxFrames == NULL || This->lLastFrame + 1 >= This->nIdxFrames) {
1425     This->nIdxFrames += 512;
1426     if (This->idxFrames == NULL)
1427       This->idxFrames = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nIdxFrames * sizeof(AVIINDEXENTRY));
1428       else
1429         This->idxFrames = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->idxFrames,
1430                            This->nIdxFrames * sizeof(AVIINDEXENTRY));
1431     if (This->idxFrames == NULL)
1432       return AVIERR_MEMORY;
1433   }
1434
1435   This->lLastFrame++;
1436   This->idxFrames[This->lLastFrame].ckid          = ckid;
1437   This->idxFrames[This->lLastFrame].dwFlags       = flags;
1438   This->idxFrames[This->lLastFrame].dwChunkOffset = offset;
1439   This->idxFrames[This->lLastFrame].dwChunkLength = size;
1440
1441   /* update AVISTREAMINFO structure if necessary */
1442   if (This->sInfo.dwLength <= This->lLastFrame)
1443     This->sInfo.dwLength = This->lLastFrame + 1;
1444
1445   return AVIERR_OK;
1446 }
1447
1448 static HRESULT AVIFILE_AddRecord(IAVIFileImpl *This)
1449 {
1450   /* pre-conditions */
1451   assert(This != NULL && This->ppStreams[0] != NULL);
1452
1453   if (This->idxRecords == NULL || This->cbIdxRecords == 0) {
1454     This->cbIdxRecords += 1024 * sizeof(AVIINDEXENTRY);
1455     This->idxRecords = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->cbIdxRecords);
1456     if (This->idxRecords == NULL)
1457       return AVIERR_MEMORY;
1458   }
1459
1460   assert(This->nIdxRecords < This->cbIdxRecords/sizeof(AVIINDEXENTRY));
1461
1462   This->idxRecords[This->nIdxRecords].ckid          = listtypeAVIRECORD;
1463   This->idxRecords[This->nIdxRecords].dwFlags       = AVIIF_LIST;
1464   This->idxRecords[This->nIdxRecords].dwChunkOffset =
1465     This->ckLastRecord.dwDataOffset - 2 * sizeof(DWORD);
1466   This->idxRecords[This->nIdxRecords].dwChunkLength =
1467     This->ckLastRecord.cksize;
1468   This->nIdxRecords++;
1469
1470   return AVIERR_OK;
1471 }
1472
1473 static DWORD   AVIFILE_ComputeMoviStart(IAVIFileImpl *This)
1474 {
1475   DWORD dwPos;
1476   DWORD nStream;
1477
1478   /* RIFF,hdrl,movi,avih => (3 * 3 + 2) * sizeof(DWORD) = 11 * sizeof(DWORD) */
1479   dwPos = 11 * sizeof(DWORD) + sizeof(MainAVIHeader);
1480
1481   for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1482     IAVIStreamImpl *pStream = This->ppStreams[nStream];
1483
1484     /* strl,strh,strf => (3 + 2 * 2) * sizeof(DWORD) = 7 * sizeof(DWORD) */
1485     dwPos += 7 * sizeof(DWORD) + sizeof(AVIStreamHeader);
1486     dwPos += ((pStream->cbFormat + 1) & ~1U);
1487     if (pStream->lpHandlerData != NULL && pStream->cbHandlerData > 0)
1488       dwPos += 2 * sizeof(DWORD) + ((pStream->cbHandlerData + 1) & ~1U);
1489     if (lstrlenW(pStream->sInfo.szName) > 0)
1490       dwPos += 2 * sizeof(DWORD) + ((lstrlenW(pStream->sInfo.szName) + 1) & ~1U);
1491   }
1492
1493   if (This->dwMoviChunkPos == 0) {
1494     This->dwNextFramePos = dwPos;
1495
1496     /* pad to multiple of AVI_HEADERSIZE only if we are more than 8 bytes away from it */
1497     if (((dwPos + AVI_HEADERSIZE) & ~(AVI_HEADERSIZE - 1)) - dwPos > 2 * sizeof(DWORD))
1498       This->dwNextFramePos = (dwPos + AVI_HEADERSIZE) & ~(AVI_HEADERSIZE - 1);
1499
1500     This->dwMoviChunkPos = This->dwNextFramePos - sizeof(DWORD);
1501   }
1502
1503   return dwPos;
1504 }
1505
1506 static void    AVIFILE_ConstructAVIStream(IAVIFileImpl *paf, DWORD nr, LPAVISTREAMINFOW asi)
1507 {
1508   IAVIStreamImpl *pstream;
1509
1510   /* pre-conditions */
1511   assert(paf != NULL);
1512   assert(nr < MAX_AVISTREAMS);
1513   assert(paf->ppStreams[nr] != NULL);
1514
1515   pstream = paf->ppStreams[nr];
1516
1517   pstream->lpVtbl         = &iavist;
1518   pstream->ref            = 0;
1519   pstream->paf            = paf;
1520   pstream->nStream        = nr;
1521   pstream->dwCurrentFrame = (DWORD)-1;
1522   pstream->lLastFrame    = -1;
1523
1524   if (asi != NULL) {
1525     memcpy(&pstream->sInfo, asi, sizeof(pstream->sInfo));
1526
1527     if (asi->dwLength > 0) {
1528       /* pre-allocate mem for frame-index structure */
1529       pstream->idxFrames =
1530         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asi->dwLength * sizeof(AVIINDEXENTRY));
1531       if (pstream->idxFrames != NULL)
1532         pstream->nIdxFrames = asi->dwLength;
1533     }
1534     if (asi->dwFormatChangeCount > 0) {
1535       /* pre-allocate mem for formatchange-index structure */
1536       pstream->idxFmtChanges =
1537         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asi->dwFormatChangeCount * sizeof(AVIINDEXENTRY));
1538       if (pstream->idxFmtChanges != NULL)
1539         pstream->nIdxFmtChanges = asi->dwFormatChangeCount;
1540     }
1541
1542     /* These values will be computed */
1543     pstream->sInfo.dwLength              = 0;
1544     pstream->sInfo.dwSuggestedBufferSize = 0;
1545     pstream->sInfo.dwFormatChangeCount   = 0;
1546     pstream->sInfo.dwEditCount           = 1;
1547     if (pstream->sInfo.dwSampleSize > 0)
1548       SetRectEmpty(&pstream->sInfo.rcFrame);
1549   }
1550
1551   pstream->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1552 }
1553
1554 static void    AVIFILE_DestructAVIStream(IAVIStreamImpl *This)
1555 {
1556   /* pre-conditions */
1557   assert(This != NULL);
1558
1559   This->dwCurrentFrame = (DWORD)-1;
1560   This->lLastFrame    = -1;
1561   This->paf = NULL;
1562   if (This->idxFrames != NULL) {
1563     HeapFree(GetProcessHeap(), 0, This->idxFrames);
1564     This->idxFrames  = NULL;
1565     This->nIdxFrames = 0;
1566   }
1567   HeapFree(GetProcessHeap(), 0, This->idxFmtChanges);
1568   This->idxFmtChanges = NULL;
1569   if (This->lpBuffer != NULL) {
1570     HeapFree(GetProcessHeap(), 0, This->lpBuffer);
1571     This->lpBuffer = NULL;
1572     This->cbBuffer = 0;
1573   }
1574   if (This->lpHandlerData != NULL) {
1575     HeapFree(GetProcessHeap(), 0, This->lpHandlerData);
1576     This->lpHandlerData = NULL;
1577     This->cbHandlerData = 0;
1578   }
1579   if (This->extra.lp != NULL) {
1580     HeapFree(GetProcessHeap(), 0, This->extra.lp);
1581     This->extra.lp = NULL;
1582     This->extra.cb = 0;
1583   }
1584   if (This->lpFormat != NULL) {
1585     HeapFree(GetProcessHeap(), 0, This->lpFormat);
1586     This->lpFormat = NULL;
1587     This->cbFormat = 0;
1588   }
1589 }
1590
1591 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1592 {
1593   MainAVIHeader   MainAVIHdr;
1594   MMCKINFO        ckRIFF;
1595   MMCKINFO        ckLIST1;
1596   MMCKINFO        ckLIST2;
1597   MMCKINFO        ck;
1598   IAVIStreamImpl *pStream;
1599   DWORD           nStream;
1600   HRESULT         hr;
1601
1602   if (This->hmmio == NULL)
1603     return AVIERR_FILEOPEN;
1604
1605   /* initialize stream ptr's */
1606   memset(This->ppStreams, 0, sizeof(This->ppStreams));
1607
1608   /* try to get "RIFF" chunk -- must not be at beginning of file! */
1609   ckRIFF.fccType = formtypeAVI;
1610   if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) {
1611     ERR(": not an AVI!\n");
1612     return AVIERR_FILEREAD;
1613   }
1614
1615   /* get "LIST" "hdrl" */
1616   ckLIST1.fccType = listtypeAVIHEADER;
1617   hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF, MMIO_FINDLIST);
1618   if (FAILED(hr))
1619     return hr;
1620
1621   /* get "avih" chunk */
1622   ck.ckid = ckidAVIMAINHDR;
1623   hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, MMIO_FINDCHUNK);
1624   if (FAILED(hr))
1625     return hr;
1626
1627   if (ck.cksize != sizeof(MainAVIHdr)) {
1628     ERR(": invalid size of %ld for MainAVIHeader!\n", ck.cksize);
1629     return AVIERR_BADFORMAT;
1630   }
1631   if (mmioRead(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
1632     return AVIERR_FILEREAD;
1633
1634   /* check for MAX_AVISTREAMS limit */
1635   if (MainAVIHdr.dwStreams > MAX_AVISTREAMS) {
1636     WARN("file contains %lu streams, but only supports %d -- change MAX_AVISTREAMS!\n", MainAVIHdr.dwStreams, MAX_AVISTREAMS);
1637     return AVIERR_UNSUPPORTED;
1638   }
1639
1640   /* adjust permissions if copyrighted material in file */
1641   if (MainAVIHdr.dwFlags & AVIFILEINFO_COPYRIGHTED) {
1642     This->uMode &= ~MMIO_RWMODE;
1643     This->uMode |= MMIO_READ;
1644   }
1645
1646   /* convert MainAVIHeader into AVIFILINFOW */
1647   memset(&This->fInfo, 0, sizeof(This->fInfo));
1648   This->fInfo.dwRate                = MainAVIHdr.dwMicroSecPerFrame;
1649   This->fInfo.dwScale               = 1000000;
1650   This->fInfo.dwMaxBytesPerSec      = MainAVIHdr.dwMaxBytesPerSec;
1651   This->fInfo.dwFlags               = MainAVIHdr.dwFlags;
1652   This->fInfo.dwCaps                = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1653   This->fInfo.dwLength              = MainAVIHdr.dwTotalFrames;
1654   This->fInfo.dwStreams             = MainAVIHdr.dwStreams;
1655   This->fInfo.dwSuggestedBufferSize = MainAVIHdr.dwSuggestedBufferSize;
1656   This->fInfo.dwWidth               = MainAVIHdr.dwWidth;
1657   This->fInfo.dwHeight              = MainAVIHdr.dwHeight;
1658   LoadStringW(AVIFILE_hModule, IDS_AVIFILETYPE, This->fInfo.szFileType,
1659               sizeof(This->fInfo.szFileType));
1660
1661   /* go back to into header list */
1662   if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1663     return AVIERR_FILEREAD;
1664
1665   /* foreach stream exists a "LIST","strl" chunk */
1666   for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1667     /* get next nested chunk in this "LIST","strl" */
1668     if (mmioDescend(This->hmmio, &ckLIST2, &ckLIST1, 0) != S_OK)
1669       return AVIERR_FILEREAD;
1670
1671     /* nested chunk must be of type "LIST","strl" -- when not normally JUNK */
1672     if (ckLIST2.ckid == FOURCC_LIST &&
1673         ckLIST2.fccType == listtypeSTREAMHEADER) {
1674       pStream = This->ppStreams[nStream] =
1675         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIStreamImpl));
1676       if (pStream == NULL)
1677         return AVIERR_MEMORY;
1678       AVIFILE_ConstructAVIStream(This, nStream, NULL);
1679
1680       ck.ckid = 0;
1681       while (mmioDescend(This->hmmio, &ck, &ckLIST2, 0) == S_OK) {
1682         switch (ck.ckid) {
1683         case ckidSTREAMHANDLERDATA:
1684           if (pStream->lpHandlerData != NULL)
1685             return AVIERR_BADFORMAT;
1686           pStream->lpHandlerData = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1687           if (pStream->lpHandlerData == NULL)
1688             return AVIERR_MEMORY;
1689           pStream->cbHandlerData = ck.cksize;
1690
1691           if (mmioRead(This->hmmio, (HPSTR)pStream->lpHandlerData, ck.cksize) != ck.cksize)
1692             return AVIERR_FILEREAD;
1693           break;
1694         case ckidSTREAMFORMAT:
1695           if (pStream->lpFormat != NULL)
1696             return AVIERR_BADFORMAT;
1697           if (ck.cksize == 0)
1698             break;
1699
1700           pStream->lpFormat = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1701           if (pStream->lpFormat == NULL)
1702             return AVIERR_MEMORY;
1703           pStream->cbFormat = ck.cksize;
1704
1705           if (mmioRead(This->hmmio, (HPSTR)pStream->lpFormat, ck.cksize) != ck.cksize)
1706             return AVIERR_FILEREAD;
1707
1708           if (pStream->sInfo.fccType == streamtypeVIDEO) {
1709             LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)pStream->lpFormat;
1710
1711             /* some corrections to the video format */
1712             if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
1713               lpbi->biClrUsed = 1u << lpbi->biBitCount;
1714             if (lpbi->biCompression == BI_RGB && lpbi->biSizeImage == 0)
1715               lpbi->biSizeImage = DIBWIDTHBYTES(*lpbi) * lpbi->biHeight;
1716             if (lpbi->biCompression != BI_RGB && lpbi->biBitCount == 8) {
1717               if (pStream->sInfo.fccHandler == mmioFOURCC('R','L','E','0') ||
1718                   pStream->sInfo.fccHandler == mmioFOURCC('R','L','E',' '))
1719                 lpbi->biCompression = BI_RLE8;
1720             }
1721             if (lpbi->biCompression == BI_RGB &&
1722                 (pStream->sInfo.fccHandler == 0 ||
1723                  pStream->sInfo.fccHandler == mmioFOURCC('N','O','N','E')))
1724               pStream->sInfo.fccHandler = comptypeDIB;
1725
1726             /* init rcFrame if it's empty */
1727             if (IsRectEmpty(&pStream->sInfo.rcFrame))
1728               SetRect(&pStream->sInfo.rcFrame, 0, 0, lpbi->biWidth, lpbi->biHeight);
1729           }
1730           break;
1731         case ckidSTREAMHEADER:
1732           {
1733             static const WCHAR streamTypeFmt[] = {'%','4','.','4','h','s',0};
1734
1735             AVIStreamHeader streamHdr;
1736             WCHAR           szType[25];
1737             WCHAR           streamNameFmt[25];
1738             UINT            count;
1739             LONG            n = ck.cksize;
1740
1741             if (ck.cksize > sizeof(streamHdr))
1742               n = sizeof(streamHdr);
1743
1744             if (mmioRead(This->hmmio, (HPSTR)&streamHdr, n) != n)
1745               return AVIERR_FILEREAD;
1746
1747             pStream->sInfo.fccType               = streamHdr.fccType;
1748             pStream->sInfo.fccHandler            = streamHdr.fccHandler;
1749             pStream->sInfo.dwFlags               = streamHdr.dwFlags;
1750             pStream->sInfo.wPriority             = streamHdr.wPriority;
1751             pStream->sInfo.wLanguage             = streamHdr.wLanguage;
1752             pStream->sInfo.dwInitialFrames       = streamHdr.dwInitialFrames;
1753             pStream->sInfo.dwScale               = streamHdr.dwScale;
1754             pStream->sInfo.dwRate                = streamHdr.dwRate;
1755             pStream->sInfo.dwStart               = streamHdr.dwStart;
1756             pStream->sInfo.dwLength              = streamHdr.dwLength;
1757             pStream->sInfo.dwSuggestedBufferSize =
1758               streamHdr.dwSuggestedBufferSize;
1759             pStream->sInfo.dwQuality             = streamHdr.dwQuality;
1760             pStream->sInfo.dwSampleSize          = streamHdr.dwSampleSize;
1761             pStream->sInfo.rcFrame.left          = streamHdr.rcFrame.left;
1762             pStream->sInfo.rcFrame.top           = streamHdr.rcFrame.top;
1763             pStream->sInfo.rcFrame.right         = streamHdr.rcFrame.right;
1764             pStream->sInfo.rcFrame.bottom        = streamHdr.rcFrame.bottom;
1765             pStream->sInfo.dwEditCount           = 0;
1766             pStream->sInfo.dwFormatChangeCount   = 0;
1767
1768             /* generate description for stream like "filename.avi Type #n" */
1769             if (streamHdr.fccType == streamtypeVIDEO)
1770               LoadStringW(AVIFILE_hModule, IDS_VIDEO, szType, sizeof(szType));
1771             else if (streamHdr.fccType == streamtypeAUDIO)
1772               LoadStringW(AVIFILE_hModule, IDS_AUDIO, szType, sizeof(szType));
1773             else
1774               wsprintfW(szType, streamTypeFmt, (char*)&streamHdr.fccType);
1775
1776             /* get count of this streamtype up to this stream */
1777             count = 0;
1778             for (n = nStream; 0 <= n; n--) {
1779               if (This->ppStreams[n]->sInfo.fccHandler == streamHdr.fccType)
1780                 count++;
1781             }
1782
1783             memset(pStream->sInfo.szName, 0, sizeof(pStream->sInfo.szName));
1784
1785             LoadStringW(AVIFILE_hModule, IDS_AVISTREAMFORMAT, streamNameFmt, sizeof(streamNameFmt));
1786
1787             /* FIXME: avoid overflow -- better use wsnprintfW, which doesn't exists ! */
1788             wsprintfW(pStream->sInfo.szName, streamNameFmt,
1789                       AVIFILE_BasenameW(This->szFileName), szType, count);
1790           }
1791           break;
1792         case ckidSTREAMNAME:
1793           { /* streamname will be saved as ASCII string */
1794             LPSTR str = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1795             if (str == NULL)
1796               return AVIERR_MEMORY;
1797
1798             if (mmioRead(This->hmmio, (HPSTR)str, ck.cksize) != ck.cksize)
1799             {
1800               HeapFree(GetProcessHeap(), 0, str);
1801               return AVIERR_FILEREAD;
1802             }
1803
1804             MultiByteToWideChar(CP_ACP, 0, str, -1, pStream->sInfo.szName,
1805                                 sizeof(pStream->sInfo.szName)/sizeof(pStream->sInfo.szName[0]));
1806
1807             HeapFree(GetProcessHeap(), 0, str);
1808           }
1809           break;
1810         case ckidAVIPADDING:
1811         case mmioFOURCC('p','a','d','d'):
1812           break;
1813         default:
1814           WARN(": found extra chunk 0x%08lX\n", ck.ckid);
1815           hr = ReadChunkIntoExtra(&pStream->extra, This->hmmio, &ck);
1816           if (FAILED(hr))
1817             return hr;
1818         };
1819
1820         if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1821           return AVIERR_FILEREAD;
1822       }
1823     } else {
1824       /* nested chunks in "LIST","hdrl" which are not of type "LIST","strl" */
1825       hr = ReadChunkIntoExtra(&This->fileextra, This->hmmio, &ckLIST2);
1826       if (FAILED(hr))
1827         return hr;
1828     }
1829     if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
1830       return AVIERR_FILEREAD;
1831   }
1832
1833   /* read any extra headers in "LIST","hdrl" */
1834   FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, 0);
1835   if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
1836     return AVIERR_FILEREAD;
1837
1838   /* search "LIST","movi" chunk in "RIFF","AVI " */
1839   ckLIST1.fccType = listtypeAVIMOVIE;
1840   hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF,
1841                               MMIO_FINDLIST);
1842   if (FAILED(hr))
1843     return hr;
1844
1845   This->dwMoviChunkPos = ckLIST1.dwDataOffset;
1846   This->dwIdxChunkPos  = ckLIST1.cksize + ckLIST1.dwDataOffset;
1847   if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
1848     return AVIERR_FILEREAD;
1849
1850   /* try to find an index */
1851   ck.ckid = ckidAVINEWINDEX;
1852   hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio,
1853                               &ck, &ckRIFF, MMIO_FINDCHUNK);
1854   if (SUCCEEDED(hr) && ck.cksize > 0) {
1855     if (FAILED(AVIFILE_LoadIndex(This, ck.cksize, ckLIST1.dwDataOffset)))
1856       This->fInfo.dwFlags &= ~AVIFILEINFO_HASINDEX;
1857   }
1858
1859   /* when we haven't found an index or it's bad, then build one
1860    * by parsing 'movi' chunk */
1861   if ((This->fInfo.dwFlags & AVIFILEINFO_HASINDEX) == 0) {
1862     for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++)
1863       This->ppStreams[nStream]->lLastFrame = -1;
1864
1865     if (mmioSeek(This->hmmio, ckLIST1.dwDataOffset + sizeof(DWORD), SEEK_SET) == -1) {
1866       ERR(": Oops, can't seek back to 'movi' chunk!\n");
1867       return AVIERR_FILEREAD;
1868     }
1869
1870     /* seek through the 'movi' list until end */
1871     while (mmioDescend(This->hmmio, &ck, &ckLIST1, 0) == S_OK) {
1872       if (ck.ckid != FOURCC_LIST) {
1873         if (mmioAscend(This->hmmio, &ck, 0) == S_OK) {
1874           nStream = StreamFromFOURCC(ck.ckid);
1875
1876           if (nStream > This->fInfo.dwStreams)
1877             return AVIERR_BADFORMAT;
1878
1879           AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1880                            ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1881         } else {
1882           nStream = StreamFromFOURCC(ck.ckid);
1883           WARN(": file seems to be truncated!\n");
1884           if (nStream <= This->fInfo.dwStreams &&
1885               This->ppStreams[nStream]->sInfo.dwSampleSize > 0) {
1886             ck.cksize = mmioSeek(This->hmmio, 0, SEEK_END);
1887             if (ck.cksize != -1) {
1888               ck.cksize -= ck.dwDataOffset;
1889               ck.cksize &= ~(This->ppStreams[nStream]->sInfo.dwSampleSize - 1);
1890
1891               AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1892                                ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1893             }
1894           }
1895         }
1896       }
1897     }
1898   }
1899
1900   /* find other chunks */
1901   FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckRIFF, 0);
1902
1903   return AVIERR_OK;
1904 }
1905
1906 static HRESULT AVIFILE_LoadIndex(IAVIFileImpl *This, DWORD size, DWORD offset)
1907 {
1908   AVIINDEXENTRY *lp;
1909   DWORD          pos, n;
1910   HRESULT        hr = AVIERR_OK;
1911   BOOL           bAbsolute = TRUE;
1912
1913   lp = HeapAlloc(GetProcessHeap(), 0, IDX_PER_BLOCK * sizeof(AVIINDEXENTRY));
1914   if (lp == NULL)
1915     return AVIERR_MEMORY;
1916
1917   /* adjust limits for index tables, so that inserting will be faster */
1918   for (n = 0; n < This->fInfo.dwStreams; n++) {
1919     IAVIStreamImpl *pStream = This->ppStreams[n];
1920
1921     pStream->lLastFrame = -1;
1922
1923     if (pStream->idxFrames != NULL) {
1924       HeapFree(GetProcessHeap(), 0, pStream->idxFrames);
1925       pStream->idxFrames  = NULL;
1926       pStream->nIdxFrames = 0;
1927     }
1928
1929     if (pStream->sInfo.dwSampleSize != 0) {
1930       if (n > 0 && This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
1931         pStream->nIdxFrames = This->ppStreams[0]->nIdxFrames;
1932       } else if (pStream->sInfo.dwSuggestedBufferSize) {
1933         pStream->nIdxFrames =
1934           pStream->sInfo.dwLength / pStream->sInfo.dwSuggestedBufferSize;
1935       }
1936     } else
1937       pStream->nIdxFrames = pStream->sInfo.dwLength;
1938
1939     pStream->idxFrames =
1940       HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pStream->nIdxFrames * sizeof(AVIINDEXENTRY));
1941     if (pStream->idxFrames == NULL && pStream->nIdxFrames > 0) {
1942       pStream->nIdxFrames = 0;
1943       return AVIERR_MEMORY;
1944     }
1945   }
1946
1947   pos = (DWORD)-1;
1948   while (size != 0) {
1949     LONG read = min(IDX_PER_BLOCK * sizeof(AVIINDEXENTRY), size);
1950
1951     if (mmioRead(This->hmmio, (HPSTR)lp, read) != read) {
1952       hr = AVIERR_FILEREAD;
1953       break;
1954     }
1955     size -= read;
1956
1957     if (pos == (DWORD)-1)
1958       pos = offset - lp->dwChunkOffset + sizeof(DWORD);
1959
1960     AVIFILE_ParseIndex(This, lp, read / sizeof(AVIINDEXENTRY),
1961                        pos, &bAbsolute);
1962   }
1963
1964   HeapFree(GetProcessHeap(), 0, lp);
1965
1966   /* checking ... */
1967   for (n = 0; n < This->fInfo.dwStreams; n++) {
1968     IAVIStreamImpl *pStream = This->ppStreams[n];
1969
1970     if (pStream->sInfo.dwSampleSize == 0 &&
1971         pStream->sInfo.dwLength != pStream->lLastFrame+1)
1972       ERR("stream %lu length mismatch: dwLength=%lu found=%ld\n",
1973            n, pStream->sInfo.dwLength, pStream->lLastFrame);
1974   }
1975
1976   return hr;
1977 }
1978
1979 static HRESULT AVIFILE_ParseIndex(IAVIFileImpl *This, AVIINDEXENTRY *lp,
1980                                   LONG count, DWORD pos, BOOL *bAbsolute)
1981 {
1982   if (lp == NULL)
1983     return AVIERR_BADPARAM;
1984
1985   for (; count > 0; count--, lp++) {
1986     WORD nStream = StreamFromFOURCC(lp->ckid);
1987
1988     if (lp->ckid == listtypeAVIRECORD || nStream == 0x7F)
1989       continue; /* skip these */
1990
1991     if (nStream > This->fInfo.dwStreams)
1992       return AVIERR_BADFORMAT;
1993
1994     if (*bAbsolute && lp->dwChunkOffset < This->dwMoviChunkPos)
1995       *bAbsolute = FALSE;
1996
1997     if (*bAbsolute)
1998       lp->dwChunkOffset += sizeof(DWORD);
1999     else
2000       lp->dwChunkOffset += pos;
2001
2002     if (FAILED(AVIFILE_AddFrame(This->ppStreams[nStream], lp->ckid, lp->dwChunkLength, lp->dwChunkOffset, lp->dwFlags)))
2003       return AVIERR_MEMORY;
2004   }
2005
2006   return AVIERR_OK;
2007 }
2008
2009 static HRESULT AVIFILE_ReadBlock(IAVIStreamImpl *This, DWORD pos,
2010                                  LPVOID buffer, LONG size)
2011 {
2012   /* pre-conditions */
2013   assert(This != NULL);
2014   assert(This->paf != NULL);
2015   assert(This->paf->hmmio != NULL);
2016   assert(This->sInfo.dwStart <= pos && pos < This->sInfo.dwLength);
2017   assert(pos <= This->lLastFrame);
2018
2019   /* should we read as much as block gives us? */
2020   if (size == 0 || size > This->idxFrames[pos].dwChunkLength)
2021     size = This->idxFrames[pos].dwChunkLength;
2022
2023   /* read into out own buffer or given one? */
2024   if (buffer == NULL) {
2025     /* we also read the chunk */
2026     size += 2 * sizeof(DWORD);
2027
2028     /* check that buffer is big enough -- don't trust dwSuggestedBufferSize */
2029     if (This->lpBuffer == NULL || size < This->cbBuffer) {
2030       DWORD maxSize = max(size, This->sInfo.dwSuggestedBufferSize);
2031
2032       if (This->lpBuffer == NULL)
2033         This->lpBuffer = HeapAlloc(GetProcessHeap(), 0, maxSize);
2034       else
2035         This->lpBuffer = HeapReAlloc(GetProcessHeap(), 0, This->lpBuffer, maxSize);
2036       if (This->lpBuffer == NULL)
2037         return AVIERR_MEMORY;
2038       This->cbBuffer = max(size, This->sInfo.dwSuggestedBufferSize);
2039     }
2040
2041     /* now read the complete chunk into our buffer */
2042     if (mmioSeek(This->paf->hmmio, This->idxFrames[pos].dwChunkOffset, SEEK_SET) == -1)
2043       return AVIERR_FILEREAD;
2044     if (mmioRead(This->paf->hmmio, (HPSTR)This->lpBuffer, size) != size)
2045       return AVIERR_FILEREAD;
2046
2047     /* check if it was the correct block which we have read */
2048     if (This->lpBuffer[0] != This->idxFrames[pos].ckid ||
2049         This->lpBuffer[1] != This->idxFrames[pos].dwChunkLength) {
2050       ERR(": block %ld not found at 0x%08lX\n", pos, This->idxFrames[pos].dwChunkOffset);
2051       ERR(": Index says: '%4.4s'(0x%08lX) size 0x%08lX\n",
2052           (char*)&This->idxFrames[pos].ckid, This->idxFrames[pos].ckid,
2053           This->idxFrames[pos].dwChunkLength);
2054       ERR(": Data  says: '%4.4s'(0x%08lX) size 0x%08lX\n",
2055           (char*)&This->lpBuffer[0], This->lpBuffer[0], This->lpBuffer[1]);
2056       return AVIERR_FILEREAD;
2057     }
2058   } else {
2059     if (mmioSeek(This->paf->hmmio, This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD), SEEK_SET) == -1)
2060       return AVIERR_FILEREAD;
2061     if (mmioRead(This->paf->hmmio, (HPSTR)buffer, size) != size)
2062       return AVIERR_FILEREAD;
2063   }
2064
2065   return AVIERR_OK;
2066 }
2067
2068 static void    AVIFILE_SamplesToBlock(IAVIStreamImpl *This, LPLONG pos,
2069                                       LPLONG offset)
2070 {
2071   LONG block;
2072
2073   /* pre-conditions */
2074   assert(This != NULL);
2075   assert(pos != NULL);
2076   assert(offset != NULL);
2077   assert(This->sInfo.dwSampleSize != 0);
2078   assert(*pos >= This->sInfo.dwStart);
2079
2080   /* convert start sample to start bytes */
2081   (*offset)  = (*pos) - This->sInfo.dwStart;
2082   (*offset) *= This->sInfo.dwSampleSize;
2083
2084   /* convert bytes to block number */
2085   for (block = 0; block <= This->lLastFrame; block++) {
2086     if (This->idxFrames[block].dwChunkLength <= *offset)
2087       (*offset) -= This->idxFrames[block].dwChunkLength;
2088     else
2089       break;
2090   }
2091
2092   *pos = block;
2093 }
2094
2095 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
2096 {
2097   MainAVIHeader   MainAVIHdr;
2098   IAVIStreamImpl* pStream;
2099   MMCKINFO        ckRIFF;
2100   MMCKINFO        ckLIST1;
2101   MMCKINFO        ckLIST2;
2102   MMCKINFO        ck;
2103   DWORD           nStream;
2104   DWORD           dwPos;
2105   HRESULT         hr;
2106
2107   /* initialize some things */
2108   if (This->dwMoviChunkPos == 0)
2109     AVIFILE_ComputeMoviStart(This);
2110
2111   /* written one record to much? */
2112   if (This->ckLastRecord.dwFlags & MMIO_DIRTY) {
2113     This->dwNextFramePos -= 3 * sizeof(DWORD);
2114     if (This->nIdxRecords > 0)
2115       This->nIdxRecords--;
2116   }
2117
2118   AVIFILE_UpdateInfo(This);
2119
2120   assert(This->fInfo.dwScale != 0);
2121
2122   memset(&MainAVIHdr, 0, sizeof(MainAVIHdr));
2123   MainAVIHdr.dwMicroSecPerFrame    = MulDiv(This->fInfo.dwRate, 1000000,
2124                                             This->fInfo.dwScale);
2125   MainAVIHdr.dwMaxBytesPerSec      = This->fInfo.dwMaxBytesPerSec;
2126   MainAVIHdr.dwPaddingGranularity  = AVI_HEADERSIZE;
2127   MainAVIHdr.dwFlags               = This->fInfo.dwFlags;
2128   MainAVIHdr.dwTotalFrames         = This->fInfo.dwLength;
2129   MainAVIHdr.dwInitialFrames       = 0;
2130   MainAVIHdr.dwStreams             = This->fInfo.dwStreams;
2131   MainAVIHdr.dwSuggestedBufferSize = This->fInfo.dwSuggestedBufferSize;
2132   MainAVIHdr.dwWidth               = This->fInfo.dwWidth;
2133   MainAVIHdr.dwHeight              = This->fInfo.dwHeight;
2134   MainAVIHdr.dwInitialFrames       = This->dwInitialFrames;
2135
2136   /* now begin writing ... */
2137   mmioSeek(This->hmmio, 0, SEEK_SET);
2138
2139   /* RIFF chunk */
2140   ckRIFF.cksize  = 0;
2141   ckRIFF.fccType = formtypeAVI;
2142   if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
2143     return AVIERR_FILEWRITE;
2144
2145   /* AVI headerlist */
2146   ckLIST1.cksize  = 0;
2147   ckLIST1.fccType = listtypeAVIHEADER;
2148   if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2149     return AVIERR_FILEWRITE;
2150
2151   /* MainAVIHeader */
2152   ck.ckid    = ckidAVIMAINHDR;
2153   ck.cksize  = sizeof(MainAVIHdr);
2154   ck.fccType = 0;
2155   if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2156     return AVIERR_FILEWRITE;
2157   if (mmioWrite(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
2158     return AVIERR_FILEWRITE;
2159   if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2160     return AVIERR_FILEWRITE;
2161
2162   /* write the headers of each stream into a separate streamheader list */
2163   for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2164     AVIStreamHeader strHdr;
2165
2166     pStream = This->ppStreams[nStream];
2167
2168     /* begin the new streamheader list */
2169     ckLIST2.cksize  = 0;
2170     ckLIST2.fccType = listtypeSTREAMHEADER;
2171     if (mmioCreateChunk(This->hmmio, &ckLIST2, MMIO_CREATELIST) != S_OK)
2172       return AVIERR_FILEWRITE;
2173
2174     /* create an AVIStreamHeader from the AVSTREAMINFO */
2175     strHdr.fccType               = pStream->sInfo.fccType;
2176     strHdr.fccHandler            = pStream->sInfo.fccHandler;
2177     strHdr.dwFlags               = pStream->sInfo.dwFlags;
2178     strHdr.wPriority             = pStream->sInfo.wPriority;
2179     strHdr.wLanguage             = pStream->sInfo.wLanguage;
2180     strHdr.dwInitialFrames       = pStream->sInfo.dwInitialFrames;
2181     strHdr.dwScale               = pStream->sInfo.dwScale;
2182     strHdr.dwRate                = pStream->sInfo.dwRate;
2183     strHdr.dwStart               = pStream->sInfo.dwStart;
2184     strHdr.dwLength              = pStream->sInfo.dwLength;
2185     strHdr.dwSuggestedBufferSize = pStream->sInfo.dwSuggestedBufferSize;
2186     strHdr.dwQuality             = pStream->sInfo.dwQuality;
2187     strHdr.dwSampleSize          = pStream->sInfo.dwSampleSize;
2188     strHdr.rcFrame.left          = pStream->sInfo.rcFrame.left;
2189     strHdr.rcFrame.top           = pStream->sInfo.rcFrame.top;
2190     strHdr.rcFrame.right         = pStream->sInfo.rcFrame.right;
2191     strHdr.rcFrame.bottom        = pStream->sInfo.rcFrame.bottom;
2192
2193     /* now write the AVIStreamHeader */
2194     ck.ckid   = ckidSTREAMHEADER;
2195     ck.cksize = sizeof(strHdr);
2196     if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2197       return AVIERR_FILEWRITE;
2198     if (mmioWrite(This->hmmio, (HPSTR)&strHdr, ck.cksize) != ck.cksize)
2199       return AVIERR_FILEWRITE;
2200     if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2201       return AVIERR_FILEWRITE;
2202
2203     /* ... the hopefully ever present streamformat ... */
2204     ck.ckid   = ckidSTREAMFORMAT;
2205     ck.cksize = pStream->cbFormat;
2206     if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2207       return AVIERR_FILEWRITE;
2208     if (pStream->lpFormat != NULL && ck.cksize > 0) {
2209       if (mmioWrite(This->hmmio, (HPSTR)pStream->lpFormat, ck.cksize) != ck.cksize)
2210         return AVIERR_FILEWRITE;
2211     }
2212     if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2213       return AVIERR_FILEWRITE;
2214
2215     /* ... some optional existing handler data ... */
2216     if (pStream->lpHandlerData != NULL && pStream->cbHandlerData > 0) {
2217       ck.ckid   = ckidSTREAMHANDLERDATA;
2218       ck.cksize = pStream->cbHandlerData;
2219       if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2220         return AVIERR_FILEWRITE;
2221       if (mmioWrite(This->hmmio, (HPSTR)pStream->lpHandlerData, ck.cksize) != ck.cksize)
2222         return AVIERR_FILEWRITE;
2223       if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2224         return AVIERR_FILEWRITE;
2225     }
2226
2227     /* ... some optional additional extra chunk for this stream ... */
2228     if (pStream->extra.lp != NULL && pStream->extra.cb > 0) {
2229       /* the chunk header(s) are already in the strucuture */
2230       if (mmioWrite(This->hmmio, (HPSTR)pStream->extra.lp, pStream->extra.cb) != pStream->extra.cb)
2231         return AVIERR_FILEWRITE;
2232     }
2233
2234     /* ... an optional name for this stream ... */
2235     if (lstrlenW(pStream->sInfo.szName) > 0) {
2236       LPSTR str;
2237
2238       ck.ckid   = ckidSTREAMNAME;
2239       ck.cksize = lstrlenW(pStream->sInfo.szName) + 1;
2240       if (ck.cksize & 1) /* align */
2241         ck.cksize++;
2242       if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2243         return AVIERR_FILEWRITE;
2244
2245       /* the streamname must be saved in ASCII not Unicode */
2246       str = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
2247       if (str == NULL)
2248         return AVIERR_MEMORY;
2249       WideCharToMultiByte(CP_ACP, 0, pStream->sInfo.szName, -1, str,
2250                           ck.cksize, NULL, NULL);
2251
2252       if (mmioWrite(This->hmmio, (HPSTR)str, ck.cksize) != ck.cksize) {
2253         HeapFree(GetProcessHeap(), 0, str);     
2254         return AVIERR_FILEWRITE;
2255       }
2256
2257       HeapFree(GetProcessHeap(), 0, str);
2258       if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2259         return AVIERR_FILEWRITE;
2260     }
2261
2262     /* close streamheader list for this stream */
2263     if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
2264       return AVIERR_FILEWRITE;
2265   } /* for (0 <= nStream < MainAVIHdr.dwStreams) */
2266
2267   /* close the aviheader list */
2268   if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2269     return AVIERR_FILEWRITE;
2270
2271   /* check for padding to pre-guessed 'movi'-chunk position */
2272   dwPos = ckLIST1.dwDataOffset + ckLIST1.cksize;
2273   if (This->dwMoviChunkPos - 2 * sizeof(DWORD) > dwPos) {
2274     ck.ckid   = ckidAVIPADDING;
2275     ck.cksize = This->dwMoviChunkPos - dwPos - 4 * sizeof(DWORD);
2276     assert((LONG)ck.cksize >= 0);
2277
2278     if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2279       return AVIERR_FILEWRITE;
2280     if (mmioSeek(This->hmmio, ck.cksize, SEEK_CUR) == -1)
2281       return AVIERR_FILEWRITE;
2282     if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2283       return AVIERR_FILEWRITE;
2284   }
2285
2286   /* now write the 'movi' chunk */
2287   mmioSeek(This->hmmio, This->dwMoviChunkPos - 2 * sizeof(DWORD), SEEK_SET);
2288   ckLIST1.cksize  = 0;
2289   ckLIST1.fccType = listtypeAVIMOVIE;
2290   if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2291     return AVIERR_FILEWRITE;
2292   if (mmioSeek(This->hmmio, This->dwNextFramePos, SEEK_SET) == -1)
2293     return AVIERR_FILEWRITE;
2294   if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2295     return AVIERR_FILEWRITE;
2296
2297   /* write 'idx1' chunk */
2298   hr = AVIFILE_SaveIndex(This);
2299   if (FAILED(hr))
2300     return hr;
2301
2302   /* write optional extra file chunks */
2303   if (This->fileextra.lp != NULL && This->fileextra.cb > 0) {
2304     /* as for the streams, are the chunk header(s) in the structure */
2305     if (mmioWrite(This->hmmio, (HPSTR)This->fileextra.lp, This->fileextra.cb) != This->fileextra.cb)
2306       return AVIERR_FILEWRITE;
2307   }
2308
2309   /* close RIFF chunk */
2310   if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
2311     return AVIERR_FILEWRITE;
2312
2313   /* add some JUNK at end for bad parsers */
2314   memset(&ckRIFF, 0, sizeof(ckRIFF));
2315   mmioWrite(This->hmmio, (HPSTR)&ckRIFF, sizeof(ckRIFF));
2316   mmioFlush(This->hmmio, 0);
2317
2318   return AVIERR_OK;
2319 }
2320
2321 static HRESULT AVIFILE_SaveIndex(IAVIFileImpl *This)
2322 {
2323   IAVIStreamImpl *pStream;
2324   AVIINDEXENTRY   idx;
2325   MMCKINFO        ck;
2326   DWORD           nStream;
2327   LONG            n;
2328
2329   ck.ckid   = ckidAVINEWINDEX;
2330   ck.cksize = 0;
2331   if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2332     return AVIERR_FILEWRITE;
2333
2334   if (This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
2335     /* is interleaved -- write block of coresponding frames */
2336     LONG lInitialFrames = 0;
2337     LONG stepsize;
2338     LONG i;
2339
2340     if (This->ppStreams[0]->sInfo.dwSampleSize == 0)
2341       stepsize = 1;
2342     else
2343       stepsize = AVIStreamTimeToSample((PAVISTREAM)This->ppStreams[0], 1000000);
2344
2345     assert(stepsize > 0);
2346
2347     for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2348       if (lInitialFrames < This->ppStreams[nStream]->sInfo.dwInitialFrames)
2349         lInitialFrames = This->ppStreams[nStream]->sInfo.dwInitialFrames;
2350     }
2351
2352     for (i = -lInitialFrames; i < (LONG)This->fInfo.dwLength - lInitialFrames;
2353          i += stepsize) {
2354       DWORD nFrame = lInitialFrames + i;
2355
2356       assert(nFrame < This->nIdxRecords);
2357
2358       idx.ckid          = listtypeAVIRECORD;
2359       idx.dwFlags       = AVIIF_LIST;
2360       idx.dwChunkLength = This->idxRecords[nFrame].dwChunkLength;
2361       idx.dwChunkOffset = This->idxRecords[nFrame].dwChunkOffset
2362         - This->dwMoviChunkPos;
2363       if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2364         return AVIERR_FILEWRITE;
2365
2366       for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2367         pStream = This->ppStreams[nStream];
2368
2369         /* heave we reached start of this stream? */
2370         if (-(LONG)pStream->sInfo.dwInitialFrames > i)
2371           continue;
2372
2373         if (pStream->sInfo.dwInitialFrames < lInitialFrames)
2374           nFrame -= (lInitialFrames - pStream->sInfo.dwInitialFrames);
2375
2376         /* reached end of this stream? */
2377         if (pStream->lLastFrame <= nFrame)
2378           continue;
2379
2380         if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2381             pStream->sInfo.dwFormatChangeCount != 0 &&
2382             pStream->idxFmtChanges != NULL) {
2383           DWORD pos;
2384
2385           for (pos = 0; pos < pStream->sInfo.dwFormatChangeCount; pos++) {
2386             if (pStream->idxFmtChanges[pos].ckid == nFrame) {
2387               idx.dwFlags = AVIIF_NOTIME;
2388               idx.ckid    = MAKEAVICKID(cktypePALchange, pStream->nStream);
2389               idx.dwChunkLength = pStream->idxFmtChanges[pos].dwChunkLength;
2390               idx.dwChunkOffset = pStream->idxFmtChanges[pos].dwChunkOffset
2391                 - This->dwMoviChunkPos;
2392
2393               if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2394                 return AVIERR_FILEWRITE;
2395               break;
2396             }
2397           }
2398         } /* if have formatchanges */
2399
2400         idx.ckid          = pStream->idxFrames[nFrame].ckid;
2401         idx.dwFlags       = pStream->idxFrames[nFrame].dwFlags;
2402         idx.dwChunkLength = pStream->idxFrames[nFrame].dwChunkLength;
2403         idx.dwChunkOffset = pStream->idxFrames[nFrame].dwChunkOffset
2404           - This->dwMoviChunkPos;
2405         if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2406           return AVIERR_FILEWRITE;
2407       }
2408     }
2409   } else {
2410     /* not interleaved -- write index for each stream at once */
2411     for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2412       pStream = This->ppStreams[nStream];
2413
2414       for (n = 0; n <= pStream->lLastFrame; n++) {
2415         if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2416             (pStream->sInfo.dwFormatChangeCount != 0)) {
2417           DWORD pos;
2418
2419           for (pos = 0; pos < pStream->sInfo.dwFormatChangeCount; pos++) {
2420             if (pStream->idxFmtChanges[pos].ckid == n) {
2421               idx.dwFlags = AVIIF_NOTIME;
2422               idx.ckid    = MAKEAVICKID(cktypePALchange, pStream->nStream);
2423               idx.dwChunkLength = pStream->idxFmtChanges[pos].dwChunkLength;
2424               idx.dwChunkOffset =
2425                 pStream->idxFmtChanges[pos].dwChunkOffset - This->dwMoviChunkPos;
2426               if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2427                 return AVIERR_FILEWRITE;
2428               break;
2429             }
2430           }
2431         } /* if have formatchanges */
2432
2433         idx.ckid          = pStream->idxFrames[n].ckid;
2434         idx.dwFlags       = pStream->idxFrames[n].dwFlags;
2435         idx.dwChunkLength = pStream->idxFrames[n].dwChunkLength;
2436         idx.dwChunkOffset = pStream->idxFrames[n].dwChunkOffset
2437           - This->dwMoviChunkPos;
2438
2439         if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2440           return AVIERR_FILEWRITE;
2441       }
2442     }
2443   } /* if not interleaved */
2444
2445   if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2446     return AVIERR_FILEWRITE;
2447
2448   return AVIERR_OK;
2449 }
2450
2451 static ULONG  AVIFILE_SearchStream(IAVIFileImpl *This, DWORD fcc, LONG lSkip)
2452 {
2453   UINT i;
2454   UINT nStream;
2455
2456   /* pre-condition */
2457   assert(lSkip >= 0);
2458
2459   if (fcc != 0) {
2460     /* search the number of the specified stream */
2461     nStream = (ULONG)-1;
2462     for (i = 0; i < This->fInfo.dwStreams; i++) {
2463       assert(This->ppStreams[i] != NULL);
2464
2465       if (This->ppStreams[i]->sInfo.fccType == fcc) {
2466         if (lSkip == 0) {
2467           nStream = i;
2468           break;
2469         } else
2470           lSkip--;
2471       }
2472     }
2473   } else
2474     nStream = lSkip;
2475
2476   return nStream;
2477 }
2478
2479 static void    AVIFILE_UpdateInfo(IAVIFileImpl *This)
2480 {
2481   UINT i;
2482
2483   /* pre-conditions */
2484   assert(This != NULL);
2485
2486   This->fInfo.dwMaxBytesPerSec      = 0;
2487   This->fInfo.dwCaps                = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
2488   This->fInfo.dwSuggestedBufferSize = 0;
2489   This->fInfo.dwWidth               = 0;
2490   This->fInfo.dwHeight              = 0;
2491   This->fInfo.dwScale               = 0;
2492   This->fInfo.dwRate                = 0;
2493   This->fInfo.dwLength              = 0;
2494   This->dwInitialFrames             = 0;
2495
2496   for (i = 0; i < This->fInfo.dwStreams; i++) {
2497     AVISTREAMINFOW *psi;
2498     DWORD           n;
2499
2500     /* pre-conditions */
2501     assert(This->ppStreams[i] != NULL);
2502
2503     psi = &This->ppStreams[i]->sInfo;
2504     assert(psi->dwScale != 0);
2505     assert(psi->dwRate != 0);
2506
2507     if (i == 0) {
2508       /* use first stream timings as base */
2509       This->fInfo.dwScale  = psi->dwScale;
2510       This->fInfo.dwRate   = psi->dwRate;
2511       This->fInfo.dwLength = psi->dwLength;
2512     } else {
2513       n = AVIStreamSampleToSample((PAVISTREAM)This->ppStreams[0],
2514                                   (PAVISTREAM)This->ppStreams[i],psi->dwLength);
2515       if (This->fInfo.dwLength < n)
2516         This->fInfo.dwLength = n;
2517     }
2518
2519     if (This->dwInitialFrames < psi->dwInitialFrames)
2520       This->dwInitialFrames = psi->dwInitialFrames;
2521
2522     if (This->fInfo.dwSuggestedBufferSize < psi->dwSuggestedBufferSize)
2523       This->fInfo.dwSuggestedBufferSize = psi->dwSuggestedBufferSize;
2524
2525     if (psi->dwSampleSize != 0) {
2526       /* fixed sample size -- exact computation */
2527       This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSampleSize, psi->dwRate,
2528                                              psi->dwScale);
2529     } else {
2530       /* variable sample size -- only upper limit */
2531       This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSuggestedBufferSize,
2532                                              psi->dwRate, psi->dwScale);
2533
2534       /* update dimensions */
2535       n = psi->rcFrame.right - psi->rcFrame.left;
2536       if (This->fInfo.dwWidth < n)
2537         This->fInfo.dwWidth = n;
2538       n = psi->rcFrame.bottom - psi->rcFrame.top;
2539       if (This->fInfo.dwHeight < n)
2540         This->fInfo.dwHeight = n;
2541     }
2542   }
2543 }
2544
2545 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
2546                                   FOURCC ckid, DWORD flags, LPVOID buffer,
2547                                   LONG size)
2548 {
2549   MMCKINFO ck;
2550
2551   ck.ckid    = ckid;
2552   ck.cksize  = size;
2553   ck.fccType = 0;
2554
2555   /* if no frame/block is already written, we must compute start of movi chunk */
2556   if (This->paf->dwMoviChunkPos == 0)
2557     AVIFILE_ComputeMoviStart(This->paf);
2558
2559   if (mmioSeek(This->paf->hmmio, This->paf->dwNextFramePos, SEEK_SET) == -1)
2560     return AVIERR_FILEWRITE;
2561
2562   if (mmioCreateChunk(This->paf->hmmio, &ck, 0) != S_OK)
2563     return AVIERR_FILEWRITE;
2564   if (buffer != NULL && size > 0) {
2565     if (mmioWrite(This->paf->hmmio, (HPSTR)buffer, size) != size)
2566       return AVIERR_FILEWRITE;
2567   }
2568   if (mmioAscend(This->paf->hmmio, &ck, 0) != S_OK)
2569     return AVIERR_FILEWRITE;
2570
2571   This->paf->fDirty         = TRUE;
2572   This->paf->dwNextFramePos = mmioSeek(This->paf->hmmio, 0, SEEK_CUR);
2573
2574   return AVIFILE_AddFrame(This, ckid, size,
2575                           ck.dwDataOffset - 2 * sizeof(DWORD), flags);
2576 }