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