2 * Copyright 1999 Marcus Meissner
3 * Copyright 2002-2003 Michael Günnewig
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.
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.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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.
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.
45 #include "avifile_private.h"
46 #include "extrachunk.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
54 #define IDX_PER_BLOCK 2730
57 /***********************************************************************/
59 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
60 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
61 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile* iface);
62 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
63 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
64 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
65 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
66 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
67 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
68 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
70 static const struct IAVIFileVtbl iavift = {
71 IAVIFile_fnQueryInterface,
76 IAVIFile_fnCreateStream,
80 IAVIFile_fnDeleteStream
83 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile*iface,REFIID refiid,LPVOID*obj);
84 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile*iface);
85 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile*iface);
86 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile*iface,CLSID*pClassID);
87 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile*iface);
88 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile*iface,LPCOLESTR pszFileName,DWORD dwMode);
89 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile*iface,LPCOLESTR pszFileName,BOOL fRemember);
90 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile*iface,LPCOLESTR pszFileName);
91 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile*iface,LPOLESTR*ppszFileName);
93 static const struct IPersistFileVtbl ipersistft = {
94 IPersistFile_fnQueryInterface,
95 IPersistFile_fnAddRef,
96 IPersistFile_fnRelease,
97 IPersistFile_fnGetClassID,
98 IPersistFile_fnIsDirty,
101 IPersistFile_fnSaveCompleted,
102 IPersistFile_fnGetCurFile
105 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID *obj);
106 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream*iface);
107 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface);
108 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
109 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
110 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream*iface,LONG pos,LONG flags);
111 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG *formatsize);
112 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
113 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread);
114 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten);
115 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
116 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG *lpread);
117 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG size);
118 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
120 static const struct IAVIStreamVtbl iavist = {
121 IAVIStream_fnQueryInterface,
123 IAVIStream_fnRelease,
126 IAVIStream_fnFindSample,
127 IAVIStream_fnReadFormat,
128 IAVIStream_fnSetFormat,
132 IAVIStream_fnReadData,
133 IAVIStream_fnWriteData,
137 typedef struct _IAVIFileImpl IAVIFileImpl;
139 typedef struct _IPersistFileImpl {
141 const IPersistFileVtbl *lpVtbl;
143 /* IPersistFile stuff */
147 typedef struct _IAVIStreamImpl {
149 const IAVIStreamVtbl *lpVtbl;
152 /* IAVIStream stuff */
154 DWORD nStream; /* the n-th stream in file */
155 AVISTREAMINFOW sInfo;
160 LPVOID lpHandlerData;
166 DWORD cbBuffer; /* size of lpBuffer */
167 DWORD dwCurrentFrame; /* frame/block currently in lpBuffer */
169 LONG lLastFrame; /* last correct index in idxFrames */
170 AVIINDEXENTRY *idxFrames;
171 DWORD nIdxFrames; /* upper index limit of idxFrames */
172 AVIINDEXENTRY *idxFmtChanges;
173 DWORD nIdxFmtChanges; /* upper index limit of idxFmtChanges */
176 struct _IAVIFileImpl {
178 const IAVIFileVtbl *lpVtbl;
181 /* IAVIFile stuff... */
182 IPersistFileImpl iPersistFile;
185 IAVIStreamImpl *ppStreams[MAX_AVISTREAMS];
187 EXTRACHUNKS fileextra;
189 DWORD dwMoviChunkPos; /* some stuff for saving ... */
191 DWORD dwNextFramePos;
192 DWORD dwInitialFrames;
194 MMCKINFO ckLastRecord;
195 AVIINDEXENTRY *idxRecords; /* won't be updated while loading */
196 DWORD nIdxRecords; /* current fill level */
197 DWORD cbIdxRecords; /* size of idxRecords */
199 /* IPersistFile stuff ... */
206 /***********************************************************************/
208 static HRESULT AVIFILE_AddFrame(IAVIStreamImpl *This, DWORD ckid, DWORD size,
209 DWORD offset, DWORD flags);
210 static HRESULT AVIFILE_AddRecord(IAVIFileImpl *This);
211 static DWORD AVIFILE_ComputeMoviStart(IAVIFileImpl *This);
212 static void AVIFILE_ConstructAVIStream(IAVIFileImpl *paf, DWORD nr,
213 const AVISTREAMINFOW *asi);
214 static void AVIFILE_DestructAVIStream(IAVIStreamImpl *This);
215 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
216 static HRESULT AVIFILE_LoadIndex(const IAVIFileImpl *This, DWORD size, DWORD offset);
217 static HRESULT AVIFILE_ParseIndex(const IAVIFileImpl *This, AVIINDEXENTRY *lp,
218 LONG count, DWORD pos, BOOL *bAbsolute);
219 static HRESULT AVIFILE_ReadBlock(IAVIStreamImpl *This, DWORD start,
220 LPVOID buffer, DWORD size);
221 static void AVIFILE_SamplesToBlock(const IAVIStreamImpl *This, LPLONG pos,
223 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
224 static HRESULT AVIFILE_SaveIndex(const IAVIFileImpl *This);
225 static ULONG AVIFILE_SearchStream(const IAVIFileImpl *This, DWORD fccType,
227 static void AVIFILE_UpdateInfo(IAVIFileImpl *This);
228 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
229 FOURCC ckid, DWORD flags, LPCVOID buffer,
232 HRESULT AVIFILE_CreateAVIFile(REFIID riid, LPVOID *ppv)
237 assert(riid != NULL && ppv != NULL);
241 pfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIFileImpl));
243 return AVIERR_MEMORY;
245 pfile->lpVtbl = &iavift;
247 pfile->iPersistFile.lpVtbl = &ipersistft;
248 pfile->iPersistFile.paf = pfile;
250 hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
252 HeapFree(GetProcessHeap(), 0, pfile);
257 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
260 IAVIFileImpl *This = (IAVIFileImpl *)iface;
262 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
264 if (IsEqualGUID(&IID_IUnknown, refiid) ||
265 IsEqualGUID(&IID_IAVIFile, refiid)) {
267 IAVIFile_AddRef(iface);
270 } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
271 *obj = &This->iPersistFile;
272 IAVIFile_AddRef(iface);
277 return OLE_E_ENUM_NOMORE;
280 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
282 IAVIFileImpl *This = (IAVIFileImpl *)iface;
283 ULONG ref = InterlockedIncrement(&This->ref);
285 TRACE("(%p) -> %d\n", iface, ref);
290 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
292 IAVIFileImpl *This = (IAVIFileImpl *)iface;
294 ULONG ref = InterlockedDecrement(&This->ref);
296 TRACE("(%p) -> %d\n", iface, ref);
300 /* need to write headers to file */
301 AVIFILE_SaveFile(This);
304 for (i = 0; i < This->fInfo.dwStreams; i++) {
305 if (This->ppStreams[i] != NULL) {
306 if (This->ppStreams[i]->ref != 0) {
307 ERR(": someone has still %u reference to stream %u (%p)!\n",
308 This->ppStreams[i]->ref, i, This->ppStreams[i]);
310 AVIFILE_DestructAVIStream(This->ppStreams[i]);
311 HeapFree(GetProcessHeap(), 0, This->ppStreams[i]);
312 This->ppStreams[i] = NULL;
316 if (This->idxRecords != NULL) {
317 HeapFree(GetProcessHeap(), 0, This->idxRecords);
318 This->idxRecords = NULL;
319 This->nIdxRecords = 0;
322 if (This->fileextra.lp != NULL) {
323 HeapFree(GetProcessHeap(), 0, This->fileextra.lp);
324 This->fileextra.lp = NULL;
325 This->fileextra.cb = 0;
328 HeapFree(GetProcessHeap(), 0, This->szFileName);
329 This->szFileName = NULL;
331 if (This->hmmio != NULL) {
332 mmioClose(This->hmmio, 0);
336 HeapFree(GetProcessHeap(), 0, This);
341 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
344 IAVIFileImpl *This = (IAVIFileImpl *)iface;
346 TRACE("(%p,%p,%d)\n",iface,afi,size);
349 return AVIERR_BADPARAM;
351 return AVIERR_BADSIZE;
353 AVIFILE_UpdateInfo(This);
355 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
357 if ((DWORD)size < sizeof(This->fInfo))
358 return AVIERR_BUFFERTOOSMALL;
362 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
363 DWORD fccType, LONG lParam)
365 IAVIFileImpl *This = (IAVIFileImpl *)iface;
369 TRACE("(%p,%p,0x%08X,%d)\n", iface, avis, fccType, lParam);
371 if (avis == NULL || lParam < 0)
372 return AVIERR_BADPARAM;
374 nStream = AVIFILE_SearchStream(This, fccType, lParam);
376 /* Does the requested stream exist? */
377 if (nStream < This->fInfo.dwStreams &&
378 This->ppStreams[nStream] != NULL) {
379 *avis = (PAVISTREAM)This->ppStreams[nStream];
380 IAVIStream_AddRef(*avis);
385 /* Sorry, but the specified stream doesn't exist */
386 return AVIERR_NODATA;
389 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
390 LPAVISTREAMINFOW asi)
392 IAVIFileImpl *This = (IAVIFileImpl *)iface;
396 TRACE("(%p,%p,%p)\n", iface, avis, asi);
398 /* check parameters */
399 if (avis == NULL || asi == NULL)
400 return AVIERR_BADPARAM;
404 /* Does the user have write permission? */
405 if ((This->uMode & MMIO_RWMODE) == 0)
406 return AVIERR_READONLY;
408 /* Can we add another stream? */
409 n = This->fInfo.dwStreams;
410 if (n >= MAX_AVISTREAMS || This->dwMoviChunkPos != 0) {
411 /* already reached max nr of streams
412 * or have already written frames to disk */
413 return AVIERR_UNSUPPORTED;
416 /* check AVISTREAMINFO for some really needed things */
417 if (asi->fccType == 0 || asi->dwScale == 0 || asi->dwRate == 0)
418 return AVIERR_BADFORMAT;
420 /* now it seems to be save to add the stream */
421 assert(This->ppStreams[n] == NULL);
422 This->ppStreams[n] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
423 sizeof(IAVIStreamImpl));
424 if (This->ppStreams[n] == NULL)
425 return AVIERR_MEMORY;
427 /* initialize the new allocated stream */
428 AVIFILE_ConstructAVIStream(This, n, asi);
430 This->fInfo.dwStreams++;
433 /* update our AVIFILEINFO structure */
434 AVIFILE_UpdateInfo(This);
437 *avis = (PAVISTREAM)This->ppStreams[n];
438 IAVIStream_AddRef(*avis);
443 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
444 LPVOID lpData, LONG size)
446 IAVIFileImpl *This = (IAVIFileImpl *)iface;
448 TRACE("(%p,0x%08X,%p,%d)\n", iface, ckid, lpData, size);
450 /* check parameters */
452 return AVIERR_BADPARAM;
454 return AVIERR_BADSIZE;
456 /* Do we have write permission? */
457 if ((This->uMode & MMIO_RWMODE) == 0)
458 return AVIERR_READONLY;
462 return WriteExtraChunk(&This->fileextra, ckid, lpData, size);
465 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
466 LPVOID lpData, LONG *size)
468 IAVIFileImpl *This = (IAVIFileImpl *)iface;
470 TRACE("(%p,0x%08X,%p,%p)\n", iface, ckid, lpData, size);
472 return ReadExtraChunk(&This->fileextra, ckid, lpData, size);
475 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
477 IAVIFileImpl *This = (IAVIFileImpl *)iface;
479 TRACE("(%p)\n",iface);
481 if ((This->uMode & MMIO_RWMODE) == 0)
482 return AVIERR_READONLY;
486 /* no frames written to any stream? -- compute start of 'movi'-chunk */
487 if (This->dwMoviChunkPos == 0)
488 AVIFILE_ComputeMoviStart(This);
490 This->fInfo.dwFlags |= AVIFILEINFO_ISINTERLEAVED;
492 /* already written frames to any stream, ... */
493 if (This->ckLastRecord.dwFlags & MMIO_DIRTY) {
494 /* close last record */
495 if (mmioAscend(This->hmmio, &This->ckLastRecord, 0) != 0)
496 return AVIERR_FILEWRITE;
498 AVIFILE_AddRecord(This);
500 if (This->fInfo.dwSuggestedBufferSize < This->ckLastRecord.cksize + 3 * sizeof(DWORD))
501 This->fInfo.dwSuggestedBufferSize = This->ckLastRecord.cksize + 3 * sizeof(DWORD);
504 /* write out a new record into file, but don't close it */
505 This->ckLastRecord.cksize = 0;
506 This->ckLastRecord.fccType = listtypeAVIRECORD;
507 if (mmioSeek(This->hmmio, This->dwNextFramePos, SEEK_SET) == -1)
508 return AVIERR_FILEWRITE;
509 if (mmioCreateChunk(This->hmmio, &This->ckLastRecord, MMIO_CREATELIST) != 0)
510 return AVIERR_FILEWRITE;
511 This->dwNextFramePos += 3 * sizeof(DWORD);
516 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
519 IAVIFileImpl *This = (IAVIFileImpl *)iface;
523 TRACE("(%p,0x%08X,%d)\n", iface, fccType, lParam);
525 /* check parameter */
527 return AVIERR_BADPARAM;
529 /* Have user write permissions? */
530 if ((This->uMode & MMIO_RWMODE) == 0)
531 return AVIERR_READONLY;
533 nStream = AVIFILE_SearchStream(This, fccType, lParam);
535 /* Does the requested stream exist? */
536 if (nStream < This->fInfo.dwStreams &&
537 This->ppStreams[nStream] != NULL) {
538 /* ... so delete it now */
539 HeapFree(GetProcessHeap(), 0, This->ppStreams[nStream]);
541 if (This->fInfo.dwStreams - nStream > 0)
542 memcpy(This->ppStreams + nStream, This->ppStreams + nStream + 1,
543 (This->fInfo.dwStreams - nStream) * sizeof(IAVIStreamImpl*));
545 This->ppStreams[This->fInfo.dwStreams] = NULL;
546 This->fInfo.dwStreams--;
549 /* This->fInfo will be updated further when asked for */
552 return AVIERR_NODATA;
555 /***********************************************************************/
557 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
558 REFIID refiid, LPVOID *obj)
560 IPersistFileImpl *This = (IPersistFileImpl *)iface;
562 assert(This->paf != NULL);
564 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
567 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
569 IPersistFileImpl *This = (IPersistFileImpl *)iface;
571 assert(This->paf != NULL);
573 return IAVIFile_AddRef((PAVIFILE)This->paf);
576 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface)
578 IPersistFileImpl *This = (IPersistFileImpl *)iface;
580 assert(This->paf != NULL);
582 return IAVIFile_Release((PAVIFILE)This->paf);
585 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
588 TRACE("(%p,%p)\n", iface, pClassID);
590 if (pClassID == NULL)
591 return AVIERR_BADPARAM;
593 *pClassID = CLSID_AVIFile;
598 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
600 IPersistFileImpl *This = (IPersistFileImpl *)iface;
602 TRACE("(%p)\n", iface);
604 assert(This->paf != NULL);
606 return (This->paf->fDirty ? S_OK : S_FALSE);
609 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
610 LPCOLESTR pszFileName, DWORD dwMode)
612 IPersistFileImpl *This = (IPersistFileImpl *)iface;
616 TRACE("(%p,%s,0x%08X)\n", iface, debugstr_w(pszFileName), dwMode);
618 /* check parameter */
619 if (pszFileName == NULL)
620 return AVIERR_BADPARAM;
622 assert(This->paf != NULL);
623 if (This->paf->hmmio != NULL)
624 return AVIERR_ERROR; /* No reuse of this object for another file! */
626 /* remember mode and name */
627 This->paf->uMode = dwMode;
629 len = lstrlenW(pszFileName) + 1;
630 This->paf->szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
631 if (This->paf->szFileName == NULL)
632 return AVIERR_MEMORY;
633 lstrcpyW(This->paf->szFileName, pszFileName);
635 /* try to open the file */
636 This->paf->hmmio = mmioOpenW(This->paf->szFileName, NULL,
637 MMIO_ALLOCBUF | dwMode);
638 if (This->paf->hmmio == NULL) {
639 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
642 len = WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1,
643 NULL, 0, NULL, NULL);
644 szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR));
645 if (szFileName == NULL)
646 return AVIERR_MEMORY;
648 WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1, szFileName,
651 This->paf->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
652 HeapFree(GetProcessHeap(), 0, szFileName);
653 if (This->paf->hmmio == NULL)
654 return AVIERR_FILEOPEN;
657 /* should we create a new file? */
658 if (dwMode & OF_CREATE) {
659 memset(& This->paf->fInfo, 0, sizeof(This->paf->fInfo));
660 This->paf->fInfo.dwFlags = AVIFILEINFO_HASINDEX | AVIFILEINFO_TRUSTCKTYPE;
664 return AVIFILE_LoadFile(This->paf);
667 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
668 LPCOLESTR pszFileName,BOOL fRemember)
670 TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
672 /* We write directly to disk, so nothing to do. */
677 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
678 LPCOLESTR pszFileName)
680 TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
682 /* We write directly to disk, so nothing to do. */
687 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
688 LPOLESTR *ppszFileName)
690 IPersistFileImpl *This = (IPersistFileImpl *)iface;
692 TRACE("(%p,%p)\n", iface, ppszFileName);
694 if (ppszFileName == NULL)
695 return AVIERR_BADPARAM;
697 *ppszFileName = NULL;
699 assert(This->paf != NULL);
701 if (This->paf->szFileName != NULL) {
702 int len = lstrlenW(This->paf->szFileName) + 1;
704 *ppszFileName = CoTaskMemAlloc(len * sizeof(WCHAR));
705 if (*ppszFileName == NULL)
706 return AVIERR_MEMORY;
708 strcpyW(*ppszFileName, This->paf->szFileName);
714 /***********************************************************************/
716 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
717 REFIID refiid, LPVOID *obj)
719 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
721 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(refiid), obj);
723 if (IsEqualGUID(&IID_IUnknown, refiid) ||
724 IsEqualGUID(&IID_IAVIStream, refiid)) {
726 IAVIStream_AddRef(iface);
730 /* FIXME: IAVIStreaming interface */
732 return OLE_E_ENUM_NOMORE;
735 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
737 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
738 ULONG ref = InterlockedIncrement(&This->ref);
740 TRACE("(%p) -> %d\n", iface, ref);
742 /* also add ref to parent, so that it doesn't kill us */
743 if (This->paf != NULL)
744 IAVIFile_AddRef((PAVIFILE)This->paf);
749 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
751 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
752 ULONG ref = InterlockedDecrement(&This->ref);
754 TRACE("(%p) -> %d\n", iface, ref);
756 if (This->paf != NULL)
757 IAVIFile_Release((PAVIFILE)This->paf);
762 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
765 TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
767 /* This IAVIStream interface needs an AVIFile */
768 return AVIERR_UNSUPPORTED;
771 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
774 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
776 TRACE("(%p,%p,%d)\n", iface, psi, size);
779 return AVIERR_BADPARAM;
781 return AVIERR_BADSIZE;
783 memcpy(psi, &This->sInfo, min((DWORD)size, sizeof(This->sInfo)));
785 if ((DWORD)size < sizeof(This->sInfo))
786 return AVIERR_BUFFERTOOSMALL;
790 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
793 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
797 TRACE("(%p,%d,0x%08X)\n",iface,pos,flags);
799 if (flags & FIND_FROM_START) {
800 pos = This->sInfo.dwStart;
801 flags &= ~(FIND_FROM_START|FIND_PREV);
805 if (This->sInfo.dwSampleSize != 0) {
806 /* convert samples into block number with offset */
807 AVIFILE_SamplesToBlock(This, &pos, &offset);
810 if (flags & FIND_TYPE) {
811 if (flags & FIND_KEY) {
812 while (0 <= pos && pos <= This->lLastFrame) {
813 if (This->idxFrames[pos].dwFlags & AVIIF_KEYFRAME)
816 if (flags & FIND_NEXT)
821 } else if (flags & FIND_ANY) {
822 while (0 <= pos && pos <= This->lLastFrame) {
823 if (This->idxFrames[pos].dwChunkLength > 0)
826 if (flags & FIND_NEXT)
832 } else if ((flags & FIND_FORMAT) && This->idxFmtChanges != NULL &&
833 This->sInfo.fccType == streamtypeVIDEO) {
834 if (flags & FIND_NEXT) {
837 for (n = 0; n < This->sInfo.dwFormatChangeCount; n++)
838 if (This->idxFmtChanges[n].ckid >= pos) {
839 pos = This->idxFmtChanges[n].ckid;
845 for (n = (LONG)This->sInfo.dwFormatChangeCount; n >= 0; n--) {
846 if (This->idxFmtChanges[n].ckid <= pos) {
847 pos = This->idxFmtChanges[n].ckid;
852 if (pos > (LONG)This->sInfo.dwStart)
853 return 0; /* format changes always for first frame */
861 if (pos < (LONG)This->sInfo.dwStart)
864 switch (flags & FIND_RET) {
867 pos = This->idxFrames[pos].dwChunkLength;
870 /* physical position */
871 pos = This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD)
872 + offset * This->sInfo.dwSampleSize;
876 if (This->sInfo.dwSampleSize)
877 pos = This->sInfo.dwSampleSize;
882 FIXME(": FIND_INDEX flag is not supported!\n");
883 /* This is an index in the index-table on disc. */
885 }; /* else logical position */
890 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
891 LPVOID format, LONG *formatsize)
893 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
895 TRACE("(%p,%d,%p,%p)\n", iface, pos, format, formatsize);
897 if (formatsize == NULL)
898 return AVIERR_BADPARAM;
900 /* only interested in needed buffersize? */
901 if (format == NULL || *formatsize <= 0) {
902 *formatsize = This->cbFormat;
907 /* copy initial format (only as much as will fit) */
908 memcpy(format, This->lpFormat, min(*(DWORD*)formatsize, This->cbFormat));
909 if (*(DWORD*)formatsize < This->cbFormat) {
910 *formatsize = This->cbFormat;
911 return AVIERR_BUFFERTOOSMALL;
914 /* Could format change? When yes will it change? */
915 if ((This->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
916 pos > This->sInfo.dwStart) {
919 lLastFmt = IAVIStream_fnFindSample(iface, pos, FIND_FORMAT|FIND_PREV);
921 FIXME(": need to read formatchange for %d -- unimplemented!\n",lLastFmt);
925 *formatsize = This->cbFormat;
929 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
930 LPVOID format, LONG formatsize)
932 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
934 LPBITMAPINFOHEADER lpbiNew = format;
936 TRACE("(%p,%d,%p,%d)\n", iface, pos, format, formatsize);
938 /* check parameters */
939 if (format == NULL || formatsize <= 0)
940 return AVIERR_BADPARAM;
942 /* Do we have write permission? */
943 if ((This->paf->uMode & MMIO_RWMODE) == 0)
944 return AVIERR_READONLY;
946 /* can only set format before frame is written! */
947 if (This->lLastFrame > pos)
948 return AVIERR_UNSUPPORTED;
950 /* initial format or a formatchange? */
951 if (This->lpFormat == NULL) {
953 if (This->paf->dwMoviChunkPos != 0)
954 return AVIERR_ERROR; /* user has used API in wrong sequence! */
956 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, formatsize);
957 if (This->lpFormat == NULL)
958 return AVIERR_MEMORY;
959 This->cbFormat = formatsize;
961 memcpy(This->lpFormat, format, formatsize);
963 /* update some infos about stream */
964 if (This->sInfo.fccType == streamtypeVIDEO) {
967 lDim = This->sInfo.rcFrame.right - This->sInfo.rcFrame.left;
968 if (lDim < lpbiNew->biWidth)
969 This->sInfo.rcFrame.right = This->sInfo.rcFrame.left + lpbiNew->biWidth;
970 lDim = This->sInfo.rcFrame.bottom - This->sInfo.rcFrame.top;
971 if (lDim < lpbiNew->biHeight)
972 This->sInfo.rcFrame.bottom = This->sInfo.rcFrame.top + lpbiNew->biHeight;
973 } else if (This->sInfo.fccType == streamtypeAUDIO)
974 This->sInfo.dwSampleSize = ((LPWAVEFORMATEX)This->lpFormat)->nBlockAlign;
979 LPBITMAPINFOHEADER lpbiOld = This->lpFormat;
980 RGBQUAD *rgbNew = (RGBQUAD*)((LPBYTE)lpbiNew + lpbiNew->biSize);
981 AVIPALCHANGE *lppc = NULL;
984 /* perhaps format change, check it ... */
985 if (This->cbFormat != formatsize)
986 return AVIERR_UNSUPPORTED;
988 /* no format change, only the initial one */
989 if (memcmp(This->lpFormat, format, formatsize) == 0)
992 /* check that's only the palette, which changes */
993 if (lpbiOld->biSize != lpbiNew->biSize ||
994 lpbiOld->biWidth != lpbiNew->biWidth ||
995 lpbiOld->biHeight != lpbiNew->biHeight ||
996 lpbiOld->biPlanes != lpbiNew->biPlanes ||
997 lpbiOld->biBitCount != lpbiNew->biBitCount ||
998 lpbiOld->biCompression != lpbiNew->biCompression ||
999 lpbiOld->biClrUsed != lpbiNew->biClrUsed)
1000 return AVIERR_UNSUPPORTED;
1002 This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
1004 /* simply say all colors have changed */
1005 ck.ckid = MAKEAVICKID(cktypePALchange, This->nStream);
1006 ck.cksize = 2 * sizeof(WORD) + lpbiOld->biClrUsed * sizeof(PALETTEENTRY);
1007 lppc = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1009 return AVIERR_MEMORY;
1011 lppc->bFirstEntry = 0;
1012 lppc->bNumEntries = (lpbiOld->biClrUsed < 256 ? lpbiOld->biClrUsed : 0);
1014 for (n = 0; n < lpbiOld->biClrUsed; n++) {
1015 lppc->peNew[n].peRed = rgbNew[n].rgbRed;
1016 lppc->peNew[n].peGreen = rgbNew[n].rgbGreen;
1017 lppc->peNew[n].peBlue = rgbNew[n].rgbBlue;
1018 lppc->peNew[n].peFlags = 0;
1021 if (mmioSeek(This->paf->hmmio, This->paf->dwNextFramePos, SEEK_SET) == -1 ||
1022 mmioCreateChunk(This->paf->hmmio, &ck, 0) != S_OK ||
1023 mmioWrite(This->paf->hmmio, (HPSTR)lppc, ck.cksize) != ck.cksize ||
1024 mmioAscend(This->paf->hmmio, &ck, 0) != S_OK)
1026 HeapFree(GetProcessHeap(), 0, lppc);
1027 return AVIERR_FILEWRITE;
1030 This->paf->dwNextFramePos += ck.cksize + 2 * sizeof(DWORD);
1032 HeapFree(GetProcessHeap(), 0, lppc);
1034 return AVIFILE_AddFrame(This, cktypePALchange, n, ck.dwDataOffset, 0);
1038 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
1039 LONG samples, LPVOID buffer,
1040 LONG buffersize, LPLONG bytesread,
1043 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1048 TRACE("(%p,%d,%d,%p,%d,%p,%p)\n", iface, start, samples, buffer,
1049 buffersize, bytesread, samplesread);
1051 /* clear return parameters if given */
1052 if (bytesread != NULL)
1054 if (samplesread != NULL)
1057 /* check parameters */
1058 if ((LONG)This->sInfo.dwStart > start)
1059 return AVIERR_NODATA; /* couldn't read before start of stream */
1060 if (This->sInfo.dwStart + This->sInfo.dwLength < (DWORD)start)
1061 return AVIERR_NODATA; /* start is past end of stream */
1063 /* should we read as much as possible? */
1064 if (samples == -1) {
1065 /* User should know how much we have read */
1066 if (bytesread == NULL && samplesread == NULL)
1067 return AVIERR_BADPARAM;
1069 if (This->sInfo.dwSampleSize != 0)
1070 samples = buffersize / This->sInfo.dwSampleSize;
1075 /* limit to end of stream */
1076 if ((LONG)This->sInfo.dwLength < samples)
1077 samples = This->sInfo.dwLength;
1078 if ((start - This->sInfo.dwStart) > (This->sInfo.dwLength - samples))
1079 samples = This->sInfo.dwLength - (start - This->sInfo.dwStart);
1081 /* nothing to read? Then leave ... */
1085 if (This->sInfo.dwSampleSize != 0) {
1086 /* fixed samplesize -- we can read over frame/block boundaries */
1090 /* convert start sample to block,offset pair */
1091 AVIFILE_SamplesToBlock(This, &block, &offset);
1093 /* convert samples to bytes */
1094 samples *= This->sInfo.dwSampleSize;
1096 while (samples > 0 && buffersize > 0) {
1097 if (block != This->dwCurrentFrame) {
1098 hr = AVIFILE_ReadBlock(This, block, NULL, 0);
1103 size = min((DWORD)samples, (DWORD)buffersize);
1104 size = min(size, This->cbBuffer - offset);
1105 memcpy(buffer, ((BYTE*)&This->lpBuffer[2]) + offset, size);
1109 buffer = ((LPBYTE)buffer)+size;
1113 /* fill out return parameters if given */
1114 if (bytesread != NULL)
1116 if (samplesread != NULL)
1117 *samplesread += size / This->sInfo.dwSampleSize;
1123 return AVIERR_BUFFERTOOSMALL;
1125 /* variable samplesize -- we can only read one full frame/block */
1129 assert(start <= This->lLastFrame);
1130 size = This->idxFrames[start].dwChunkLength;
1131 if (buffer != NULL && buffersize >= size) {
1132 hr = AVIFILE_ReadBlock(This, start, buffer, size);
1135 } else if (buffer != NULL)
1136 return AVIERR_BUFFERTOOSMALL;
1138 /* fill out return parameters if given */
1139 if (bytesread != NULL)
1141 if (samplesread != NULL)
1142 *samplesread = samples;
1148 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
1149 LONG samples, LPVOID buffer,
1150 LONG buffersize, DWORD flags,
1152 LPLONG byteswritten)
1154 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1159 TRACE("(%p,%d,%d,%p,%d,0x%08X,%p,%p)\n", iface, start, samples,
1160 buffer, buffersize, flags, sampwritten, byteswritten);
1162 /* clear return parameters if given */
1163 if (sampwritten != NULL)
1165 if (byteswritten != NULL)
1168 /* check parameters */
1169 if (buffer == NULL && (buffersize > 0 || samples > 0))
1170 return AVIERR_BADPARAM;
1172 /* Have we write permission? */
1173 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1174 return AVIERR_READONLY;
1176 switch (This->sInfo.fccType) {
1177 case streamtypeAUDIO:
1178 ckid = MAKEAVICKID(cktypeWAVEbytes, This->nStream);
1181 if ((flags & AVIIF_KEYFRAME) && buffersize != 0)
1182 ckid = MAKEAVICKID(cktypeDIBbits, This->nStream);
1184 ckid = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1188 /* append to end of stream? */
1190 if (This->lLastFrame == -1)
1191 start = This->sInfo.dwStart;
1193 start = This->sInfo.dwLength;
1194 } else if (This->lLastFrame == -1)
1195 This->sInfo.dwStart = start;
1197 if (This->sInfo.dwSampleSize != 0) {
1198 /* fixed sample size -- audio like */
1199 if (samples * This->sInfo.dwSampleSize != buffersize)
1200 return AVIERR_BADPARAM;
1202 /* Couldn't skip audio-like data -- User must supply appropriate silence */
1203 if (This->sInfo.dwLength != start)
1204 return AVIERR_UNSUPPORTED;
1206 /* Convert position to frame/block */
1207 start = This->lLastFrame + 1;
1209 if ((This->paf->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) == 0) {
1210 FIXME(": not interleaved, could collect audio data!\n");
1213 /* variable sample size -- video like */
1215 return AVIERR_UNSUPPORTED;
1217 /* must we fill up with empty frames? */
1218 if (This->lLastFrame != -1) {
1219 FOURCC ckid2 = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1221 while (start > This->lLastFrame + 1) {
1222 hr = AVIFILE_WriteBlock(This, This->lLastFrame + 1, ckid2, 0, NULL, 0);
1229 /* write the block now */
1230 hr = AVIFILE_WriteBlock(This, start, ckid, flags, buffer, buffersize);
1231 if (SUCCEEDED(hr)) {
1232 /* fill out return parameters if given */
1233 if (sampwritten != NULL)
1234 *sampwritten = samples;
1235 if (byteswritten != NULL)
1236 *byteswritten = buffersize;
1242 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
1245 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1247 FIXME("(%p,%d,%d): stub\n", iface, start, samples);
1249 /* check parameters */
1250 if (start < 0 || samples < 0)
1251 return AVIERR_BADPARAM;
1253 /* Delete before start of stream? */
1254 if (start + samples < This->sInfo.dwStart)
1257 /* Delete after end of stream? */
1258 if (start > This->sInfo.dwLength)
1261 /* For the rest we need write permissions */
1262 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1263 return AVIERR_READONLY;
1265 /* 1. overwrite the data with JUNK
1267 * if ISINTERLEAVED {
1268 * 2. concat all neighboured JUNK-blocks in this record to one
1269 * 3. if this record only contains JUNK and is at end set dwNextFramePos
1270 * to start of this record, repeat this.
1272 * 2. concat all neighboured JUNK-blocks.
1273 * 3. if the JUNK block is at the end, then set dwNextFramePos to
1274 * start of this block.
1278 return AVIERR_UNSUPPORTED;
1281 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1282 LPVOID lp, LPLONG lpread)
1284 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1286 TRACE("(%p,0x%08X,%p,%p)\n", iface, fcc, lp, lpread);
1288 if (fcc == ckidSTREAMHANDLERDATA) {
1289 if (This->lpHandlerData != NULL && This->cbHandlerData > 0) {
1290 if (lp == NULL || *lpread <= 0) {
1291 *lpread = This->cbHandlerData;
1295 memcpy(lp, This->lpHandlerData, min(This->cbHandlerData, *lpread));
1296 if (*lpread < This->cbHandlerData)
1297 return AVIERR_BUFFERTOOSMALL;
1300 return AVIERR_NODATA;
1302 return ReadExtraChunk(&This->extra, fcc, lp, lpread);
1305 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1306 LPVOID lp, LONG size)
1308 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1310 TRACE("(%p,0x%08x,%p,%d)\n", iface, fcc, lp, size);
1312 /* check parameters */
1314 return AVIERR_BADPARAM;
1316 return AVIERR_BADSIZE;
1318 /* need write permission */
1319 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1320 return AVIERR_READONLY;
1322 /* already written something to this file? */
1323 if (This->paf->dwMoviChunkPos != 0) {
1324 /* the data will be inserted before the 'movi' chunk, so check for
1326 DWORD dwPos = AVIFILE_ComputeMoviStart(This->paf);
1328 /* ckid,size => 2 * sizeof(DWORD) */
1329 dwPos += 2 * sizeof(DWORD) + size;
1330 if (size >= This->paf->dwMoviChunkPos - 2 * sizeof(DWORD))
1331 return AVIERR_UNSUPPORTED; /* not enough space left */
1334 This->paf->fDirty = TRUE;
1336 if (fcc == ckidSTREAMHANDLERDATA) {
1337 if (This->lpHandlerData != NULL) {
1338 FIXME(": handler data already set -- overwirte?\n");
1339 return AVIERR_UNSUPPORTED;
1342 This->lpHandlerData = HeapAlloc(GetProcessHeap(), 0, size);
1343 if (This->lpHandlerData == NULL)
1344 return AVIERR_MEMORY;
1345 This->cbHandlerData = size;
1346 memcpy(This->lpHandlerData, lp, size);
1350 return WriteExtraChunk(&This->extra, fcc, lp, size);
1353 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1354 LPAVISTREAMINFOW info, LONG infolen)
1356 FIXME("(%p,%p,%d): stub\n", iface, info, infolen);
1361 /***********************************************************************/
1363 static HRESULT AVIFILE_AddFrame(IAVIStreamImpl *This, DWORD ckid, DWORD size, DWORD offset, DWORD flags)
1367 /* pre-conditions */
1368 assert(This != NULL);
1370 switch (TWOCCFromFOURCC(ckid)) {
1372 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1373 flags |= AVIIF_KEYFRAME;
1375 case cktypeDIBcompressed:
1376 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1377 flags &= ~AVIIF_KEYFRAME;
1379 case cktypePALchange:
1380 if (This->sInfo.fccType != streamtypeVIDEO) {
1381 ERR(": found palette change in non-video stream!\n");
1382 return AVIERR_BADFORMAT;
1385 if (This->idxFmtChanges == NULL || This->nIdxFmtChanges <= This->sInfo.dwFormatChangeCount) {
1386 DWORD new_count = This->nIdxFmtChanges + 16;
1389 if (This->idxFmtChanges == NULL) {
1390 This->idxFmtChanges =
1391 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(AVIINDEXENTRY));
1392 if (!This->idxFmtChanges) return AVIERR_MEMORY;
1394 new_buffer = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->idxFmtChanges,
1395 new_count * sizeof(AVIINDEXENTRY));
1396 if (!new_buffer) return AVIERR_MEMORY;
1397 This->idxFmtChanges = new_buffer;
1399 This->nIdxFmtChanges = new_count;
1402 This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
1403 n = ++This->sInfo.dwFormatChangeCount;
1404 This->idxFmtChanges[n].ckid = This->lLastFrame;
1405 This->idxFmtChanges[n].dwFlags = 0;
1406 This->idxFmtChanges[n].dwChunkOffset = offset;
1407 This->idxFmtChanges[n].dwChunkLength = size;
1410 case cktypeWAVEbytes:
1411 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1412 flags |= AVIIF_KEYFRAME;
1415 WARN(": unknown TWOCC 0x%04X found\n", TWOCCFromFOURCC(ckid));
1419 /* first frame is always a keyframe */
1420 if (This->lLastFrame == -1)
1421 flags |= AVIIF_KEYFRAME;
1423 if (This->sInfo.dwSuggestedBufferSize < size)
1424 This->sInfo.dwSuggestedBufferSize = size;
1426 /* get memory for index */
1427 if (This->idxFrames == NULL || This->lLastFrame + 1 >= This->nIdxFrames) {
1428 This->nIdxFrames += 512;
1429 if (This->idxFrames == NULL)
1430 This->idxFrames = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nIdxFrames * sizeof(AVIINDEXENTRY));
1432 This->idxFrames = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->idxFrames,
1433 This->nIdxFrames * sizeof(AVIINDEXENTRY));
1434 if (This->idxFrames == NULL)
1435 return AVIERR_MEMORY;
1439 This->idxFrames[This->lLastFrame].ckid = ckid;
1440 This->idxFrames[This->lLastFrame].dwFlags = flags;
1441 This->idxFrames[This->lLastFrame].dwChunkOffset = offset;
1442 This->idxFrames[This->lLastFrame].dwChunkLength = size;
1444 /* update AVISTREAMINFO structure if necessary */
1445 if (This->sInfo.dwLength <= This->lLastFrame)
1446 This->sInfo.dwLength = This->lLastFrame + 1;
1451 static HRESULT AVIFILE_AddRecord(IAVIFileImpl *This)
1453 /* pre-conditions */
1454 assert(This != NULL && This->ppStreams[0] != NULL);
1456 if (This->idxRecords == NULL || This->cbIdxRecords == 0) {
1457 This->cbIdxRecords += 1024 * sizeof(AVIINDEXENTRY);
1458 This->idxRecords = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->cbIdxRecords);
1459 if (This->idxRecords == NULL)
1460 return AVIERR_MEMORY;
1463 assert(This->nIdxRecords < This->cbIdxRecords/sizeof(AVIINDEXENTRY));
1465 This->idxRecords[This->nIdxRecords].ckid = listtypeAVIRECORD;
1466 This->idxRecords[This->nIdxRecords].dwFlags = AVIIF_LIST;
1467 This->idxRecords[This->nIdxRecords].dwChunkOffset =
1468 This->ckLastRecord.dwDataOffset - 2 * sizeof(DWORD);
1469 This->idxRecords[This->nIdxRecords].dwChunkLength =
1470 This->ckLastRecord.cksize;
1471 This->nIdxRecords++;
1476 static DWORD AVIFILE_ComputeMoviStart(IAVIFileImpl *This)
1481 /* RIFF,hdrl,movi,avih => (3 * 3 + 2) * sizeof(DWORD) = 11 * sizeof(DWORD) */
1482 dwPos = 11 * sizeof(DWORD) + sizeof(MainAVIHeader);
1484 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1485 IAVIStreamImpl *pStream = This->ppStreams[nStream];
1487 /* strl,strh,strf => (3 + 2 * 2) * sizeof(DWORD) = 7 * sizeof(DWORD) */
1488 dwPos += 7 * sizeof(DWORD) + sizeof(AVIStreamHeader);
1489 dwPos += ((pStream->cbFormat + 1) & ~1U);
1490 if (pStream->lpHandlerData != NULL && pStream->cbHandlerData > 0)
1491 dwPos += 2 * sizeof(DWORD) + ((pStream->cbHandlerData + 1) & ~1U);
1492 if (lstrlenW(pStream->sInfo.szName) > 0)
1493 dwPos += 2 * sizeof(DWORD) + ((lstrlenW(pStream->sInfo.szName) + 1) & ~1U);
1496 if (This->dwMoviChunkPos == 0) {
1497 This->dwNextFramePos = dwPos;
1499 /* pad to multiple of AVI_HEADERSIZE only if we are more than 8 bytes away from it */
1500 if (((dwPos + AVI_HEADERSIZE) & ~(AVI_HEADERSIZE - 1)) - dwPos > 2 * sizeof(DWORD))
1501 This->dwNextFramePos = (dwPos + AVI_HEADERSIZE) & ~(AVI_HEADERSIZE - 1);
1503 This->dwMoviChunkPos = This->dwNextFramePos - sizeof(DWORD);
1509 static void AVIFILE_ConstructAVIStream(IAVIFileImpl *paf, DWORD nr, const AVISTREAMINFOW *asi)
1511 IAVIStreamImpl *pstream;
1513 /* pre-conditions */
1514 assert(paf != NULL);
1515 assert(nr < MAX_AVISTREAMS);
1516 assert(paf->ppStreams[nr] != NULL);
1518 pstream = paf->ppStreams[nr];
1520 pstream->lpVtbl = &iavist;
1523 pstream->nStream = nr;
1524 pstream->dwCurrentFrame = (DWORD)-1;
1525 pstream->lLastFrame = -1;
1528 memcpy(&pstream->sInfo, asi, sizeof(pstream->sInfo));
1530 if (asi->dwLength > 0) {
1531 /* pre-allocate mem for frame-index structure */
1532 pstream->idxFrames =
1533 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asi->dwLength * sizeof(AVIINDEXENTRY));
1534 if (pstream->idxFrames != NULL)
1535 pstream->nIdxFrames = asi->dwLength;
1537 if (asi->dwFormatChangeCount > 0) {
1538 /* pre-allocate mem for formatchange-index structure */
1539 pstream->idxFmtChanges =
1540 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asi->dwFormatChangeCount * sizeof(AVIINDEXENTRY));
1541 if (pstream->idxFmtChanges != NULL)
1542 pstream->nIdxFmtChanges = asi->dwFormatChangeCount;
1545 /* These values will be computed */
1546 pstream->sInfo.dwLength = 0;
1547 pstream->sInfo.dwSuggestedBufferSize = 0;
1548 pstream->sInfo.dwFormatChangeCount = 0;
1549 pstream->sInfo.dwEditCount = 1;
1550 if (pstream->sInfo.dwSampleSize > 0)
1551 SetRectEmpty(&pstream->sInfo.rcFrame);
1554 pstream->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1557 static void AVIFILE_DestructAVIStream(IAVIStreamImpl *This)
1559 /* pre-conditions */
1560 assert(This != NULL);
1562 This->dwCurrentFrame = (DWORD)-1;
1563 This->lLastFrame = -1;
1565 if (This->idxFrames != NULL) {
1566 HeapFree(GetProcessHeap(), 0, This->idxFrames);
1567 This->idxFrames = NULL;
1568 This->nIdxFrames = 0;
1570 HeapFree(GetProcessHeap(), 0, This->idxFmtChanges);
1571 This->idxFmtChanges = NULL;
1572 if (This->lpBuffer != NULL) {
1573 HeapFree(GetProcessHeap(), 0, This->lpBuffer);
1574 This->lpBuffer = NULL;
1577 if (This->lpHandlerData != NULL) {
1578 HeapFree(GetProcessHeap(), 0, This->lpHandlerData);
1579 This->lpHandlerData = NULL;
1580 This->cbHandlerData = 0;
1582 if (This->extra.lp != NULL) {
1583 HeapFree(GetProcessHeap(), 0, This->extra.lp);
1584 This->extra.lp = NULL;
1587 if (This->lpFormat != NULL) {
1588 HeapFree(GetProcessHeap(), 0, This->lpFormat);
1589 This->lpFormat = NULL;
1594 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1596 MainAVIHeader MainAVIHdr;
1601 IAVIStreamImpl *pStream;
1605 if (This->hmmio == NULL)
1606 return AVIERR_FILEOPEN;
1608 /* initialize stream ptr's */
1609 memset(This->ppStreams, 0, sizeof(This->ppStreams));
1611 /* try to get "RIFF" chunk -- must not be at beginning of file! */
1612 ckRIFF.fccType = formtypeAVI;
1613 if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) {
1614 ERR(": not an AVI!\n");
1615 return AVIERR_FILEREAD;
1618 /* get "LIST" "hdrl" */
1619 ckLIST1.fccType = listtypeAVIHEADER;
1620 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF, MMIO_FINDLIST);
1624 /* get "avih" chunk */
1625 ck.ckid = ckidAVIMAINHDR;
1626 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, MMIO_FINDCHUNK);
1630 if (ck.cksize != sizeof(MainAVIHdr)) {
1631 ERR(": invalid size of %d for MainAVIHeader!\n", ck.cksize);
1632 return AVIERR_BADFORMAT;
1634 if (mmioRead(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
1635 return AVIERR_FILEREAD;
1637 /* check for MAX_AVISTREAMS limit */
1638 if (MainAVIHdr.dwStreams > MAX_AVISTREAMS) {
1639 WARN("file contains %u streams, but only supports %d -- change MAX_AVISTREAMS!\n", MainAVIHdr.dwStreams, MAX_AVISTREAMS);
1640 return AVIERR_UNSUPPORTED;
1643 /* adjust permissions if copyrighted material in file */
1644 if (MainAVIHdr.dwFlags & AVIFILEINFO_COPYRIGHTED) {
1645 This->uMode &= ~MMIO_RWMODE;
1646 This->uMode |= MMIO_READ;
1649 /* convert MainAVIHeader into AVIFILINFOW */
1650 memset(&This->fInfo, 0, sizeof(This->fInfo));
1651 This->fInfo.dwRate = MainAVIHdr.dwMicroSecPerFrame;
1652 This->fInfo.dwScale = 1000000;
1653 This->fInfo.dwMaxBytesPerSec = MainAVIHdr.dwMaxBytesPerSec;
1654 This->fInfo.dwFlags = MainAVIHdr.dwFlags;
1655 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1656 This->fInfo.dwLength = MainAVIHdr.dwTotalFrames;
1657 This->fInfo.dwStreams = MainAVIHdr.dwStreams;
1658 This->fInfo.dwSuggestedBufferSize = 0;
1659 This->fInfo.dwWidth = MainAVIHdr.dwWidth;
1660 This->fInfo.dwHeight = MainAVIHdr.dwHeight;
1661 LoadStringW(AVIFILE_hModule, IDS_AVIFILETYPE, This->fInfo.szFileType,
1662 sizeof(This->fInfo.szFileType)/sizeof(This->fInfo.szFileType[0]));
1664 /* go back to into header list */
1665 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1666 return AVIERR_FILEREAD;
1668 /* foreach stream exists a "LIST","strl" chunk */
1669 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1670 /* get next nested chunk in this "LIST","strl" */
1671 if (mmioDescend(This->hmmio, &ckLIST2, &ckLIST1, 0) != S_OK)
1672 return AVIERR_FILEREAD;
1674 /* nested chunk must be of type "LIST","strl" -- when not normally JUNK */
1675 if (ckLIST2.ckid == FOURCC_LIST &&
1676 ckLIST2.fccType == listtypeSTREAMHEADER) {
1677 pStream = This->ppStreams[nStream] =
1678 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIStreamImpl));
1679 if (pStream == NULL)
1680 return AVIERR_MEMORY;
1681 AVIFILE_ConstructAVIStream(This, nStream, NULL);
1684 while (mmioDescend(This->hmmio, &ck, &ckLIST2, 0) == S_OK) {
1686 case ckidSTREAMHANDLERDATA:
1687 if (pStream->lpHandlerData != NULL)
1688 return AVIERR_BADFORMAT;
1689 pStream->lpHandlerData = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1690 if (pStream->lpHandlerData == NULL)
1691 return AVIERR_MEMORY;
1692 pStream->cbHandlerData = ck.cksize;
1694 if (mmioRead(This->hmmio, pStream->lpHandlerData, ck.cksize) != ck.cksize)
1695 return AVIERR_FILEREAD;
1697 case ckidSTREAMFORMAT:
1698 if (pStream->lpFormat != NULL)
1699 return AVIERR_BADFORMAT;
1703 pStream->lpFormat = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1704 if (pStream->lpFormat == NULL)
1705 return AVIERR_MEMORY;
1706 pStream->cbFormat = ck.cksize;
1708 if (mmioRead(This->hmmio, pStream->lpFormat, ck.cksize) != ck.cksize)
1709 return AVIERR_FILEREAD;
1711 if (pStream->sInfo.fccType == streamtypeVIDEO) {
1712 LPBITMAPINFOHEADER lpbi = pStream->lpFormat;
1714 /* some corrections to the video format */
1715 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
1716 lpbi->biClrUsed = 1u << lpbi->biBitCount;
1717 if (lpbi->biCompression == BI_RGB && lpbi->biSizeImage == 0)
1718 lpbi->biSizeImage = DIBWIDTHBYTES(*lpbi) * lpbi->biHeight;
1719 if (lpbi->biCompression != BI_RGB && lpbi->biBitCount == 8) {
1720 if (pStream->sInfo.fccHandler == mmioFOURCC('R','L','E','0') ||
1721 pStream->sInfo.fccHandler == mmioFOURCC('R','L','E',' '))
1722 lpbi->biCompression = BI_RLE8;
1724 if (lpbi->biCompression == BI_RGB &&
1725 (pStream->sInfo.fccHandler == 0 ||
1726 pStream->sInfo.fccHandler == mmioFOURCC('N','O','N','E')))
1727 pStream->sInfo.fccHandler = comptypeDIB;
1729 /* init rcFrame if it's empty */
1730 if (IsRectEmpty(&pStream->sInfo.rcFrame))
1731 SetRect(&pStream->sInfo.rcFrame, 0, 0, lpbi->biWidth, lpbi->biHeight);
1734 case ckidSTREAMHEADER:
1736 static const WCHAR streamTypeFmt[] = {'%','4','.','4','h','s',0};
1738 AVIStreamHeader streamHdr;
1740 WCHAR streamNameFmt[25];
1744 if (ck.cksize > sizeof(streamHdr))
1745 n = sizeof(streamHdr);
1747 if (mmioRead(This->hmmio, (HPSTR)&streamHdr, n) != n)
1748 return AVIERR_FILEREAD;
1750 pStream->sInfo.fccType = streamHdr.fccType;
1751 pStream->sInfo.fccHandler = streamHdr.fccHandler;
1752 pStream->sInfo.dwFlags = streamHdr.dwFlags;
1753 pStream->sInfo.wPriority = streamHdr.wPriority;
1754 pStream->sInfo.wLanguage = streamHdr.wLanguage;
1755 pStream->sInfo.dwInitialFrames = streamHdr.dwInitialFrames;
1756 pStream->sInfo.dwScale = streamHdr.dwScale;
1757 pStream->sInfo.dwRate = streamHdr.dwRate;
1758 pStream->sInfo.dwStart = streamHdr.dwStart;
1759 pStream->sInfo.dwLength = streamHdr.dwLength;
1760 pStream->sInfo.dwSuggestedBufferSize = 0;
1761 pStream->sInfo.dwQuality = streamHdr.dwQuality;
1762 pStream->sInfo.dwSampleSize = streamHdr.dwSampleSize;
1763 pStream->sInfo.rcFrame.left = streamHdr.rcFrame.left;
1764 pStream->sInfo.rcFrame.top = streamHdr.rcFrame.top;
1765 pStream->sInfo.rcFrame.right = streamHdr.rcFrame.right;
1766 pStream->sInfo.rcFrame.bottom = streamHdr.rcFrame.bottom;
1767 pStream->sInfo.dwEditCount = 0;
1768 pStream->sInfo.dwFormatChangeCount = 0;
1770 /* generate description for stream like "filename.avi Type #n" */
1771 if (streamHdr.fccType == streamtypeVIDEO)
1772 LoadStringW(AVIFILE_hModule, IDS_VIDEO, szType, sizeof(szType)/sizeof(szType[0]));
1773 else if (streamHdr.fccType == streamtypeAUDIO)
1774 LoadStringW(AVIFILE_hModule, IDS_AUDIO, szType, sizeof(szType)/sizeof(szType[0]));
1776 wsprintfW(szType, streamTypeFmt, (char*)&streamHdr.fccType);
1778 /* get count of this streamtype up to this stream */
1780 for (n = nStream; 0 <= n; n--) {
1781 if (This->ppStreams[n]->sInfo.fccHandler == streamHdr.fccType)
1785 memset(pStream->sInfo.szName, 0, sizeof(pStream->sInfo.szName));
1787 LoadStringW(AVIFILE_hModule, IDS_AVISTREAMFORMAT, streamNameFmt, sizeof(streamNameFmt)/sizeof(streamNameFmt[0]));
1789 /* FIXME: avoid overflow -- better use wsnprintfW, which doesn't exists ! */
1790 wsprintfW(pStream->sInfo.szName, streamNameFmt,
1791 AVIFILE_BasenameW(This->szFileName), szType, count);
1794 case ckidSTREAMNAME:
1795 { /* streamname will be saved as ASCII string */
1796 LPSTR str = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1798 return AVIERR_MEMORY;
1800 if (mmioRead(This->hmmio, str, ck.cksize) != ck.cksize)
1802 HeapFree(GetProcessHeap(), 0, str);
1803 return AVIERR_FILEREAD;
1806 MultiByteToWideChar(CP_ACP, 0, str, -1, pStream->sInfo.szName,
1807 sizeof(pStream->sInfo.szName)/sizeof(pStream->sInfo.szName[0]));
1809 HeapFree(GetProcessHeap(), 0, str);
1812 case ckidAVIPADDING:
1813 case mmioFOURCC('p','a','d','d'):
1816 WARN(": found extra chunk 0x%08X\n", ck.ckid);
1817 hr = ReadChunkIntoExtra(&pStream->extra, This->hmmio, &ck);
1821 if (pStream->lpFormat != NULL && pStream->sInfo.fccType == streamtypeAUDIO)
1823 WAVEFORMATEX *wfx = pStream->lpFormat; /* wfx->nBlockAlign = wfx->nChannels * wfx->wBitsPerSample / 8; could be added */
1824 pStream->sInfo.dwSampleSize = wfx->nBlockAlign; /* to deal with corrupt wfx->nBlockAlign but Windows doesn't do this */
1825 TRACE("Block size reset to %u, chan=%u bpp=%u\n", wfx->nBlockAlign, wfx->nChannels, wfx->wBitsPerSample);
1826 pStream->sInfo.dwScale = 1;
1827 pStream->sInfo.dwRate = wfx->nSamplesPerSec;
1829 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1830 return AVIERR_FILEREAD;
1833 /* nested chunks in "LIST","hdrl" which are not of type "LIST","strl" */
1834 hr = ReadChunkIntoExtra(&This->fileextra, This->hmmio, &ckLIST2);
1838 if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
1839 return AVIERR_FILEREAD;
1842 /* read any extra headers in "LIST","hdrl" */
1843 FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, 0);
1844 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
1845 return AVIERR_FILEREAD;
1847 /* search "LIST","movi" chunk in "RIFF","AVI " */
1848 ckLIST1.fccType = listtypeAVIMOVIE;
1849 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF,
1854 This->dwMoviChunkPos = ckLIST1.dwDataOffset;
1855 This->dwIdxChunkPos = ckLIST1.cksize + ckLIST1.dwDataOffset;
1856 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
1857 return AVIERR_FILEREAD;
1859 /* try to find an index */
1860 ck.ckid = ckidAVINEWINDEX;
1861 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio,
1862 &ck, &ckRIFF, MMIO_FINDCHUNK);
1863 if (SUCCEEDED(hr) && ck.cksize > 0) {
1864 if (FAILED(AVIFILE_LoadIndex(This, ck.cksize, ckLIST1.dwDataOffset)))
1865 This->fInfo.dwFlags &= ~AVIFILEINFO_HASINDEX;
1868 /* when we haven't found an index or it's bad, then build one
1869 * by parsing 'movi' chunk */
1870 if ((This->fInfo.dwFlags & AVIFILEINFO_HASINDEX) == 0) {
1871 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++)
1872 This->ppStreams[nStream]->lLastFrame = -1;
1874 if (mmioSeek(This->hmmio, ckLIST1.dwDataOffset + sizeof(DWORD), SEEK_SET) == -1) {
1875 ERR(": Oops, can't seek back to 'movi' chunk!\n");
1876 return AVIERR_FILEREAD;
1879 /* seek through the 'movi' list until end */
1880 while (mmioDescend(This->hmmio, &ck, &ckLIST1, 0) == S_OK) {
1881 if (ck.ckid != FOURCC_LIST) {
1882 if (mmioAscend(This->hmmio, &ck, 0) == S_OK) {
1883 nStream = StreamFromFOURCC(ck.ckid);
1885 if (nStream > This->fInfo.dwStreams)
1886 return AVIERR_BADFORMAT;
1888 AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1889 ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1891 nStream = StreamFromFOURCC(ck.ckid);
1892 WARN(": file seems to be truncated!\n");
1893 if (nStream <= This->fInfo.dwStreams &&
1894 This->ppStreams[nStream]->sInfo.dwSampleSize > 0) {
1895 ck.cksize = mmioSeek(This->hmmio, 0, SEEK_END);
1896 if (ck.cksize != -1) {
1897 ck.cksize -= ck.dwDataOffset;
1898 ck.cksize &= ~(This->ppStreams[nStream]->sInfo.dwSampleSize - 1);
1900 AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1901 ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1909 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++)
1911 DWORD sugbuf = This->ppStreams[nStream]->sInfo.dwSuggestedBufferSize;
1912 if (This->fInfo.dwSuggestedBufferSize < sugbuf)
1913 This->fInfo.dwSuggestedBufferSize = sugbuf;
1916 /* find other chunks */
1917 FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckRIFF, 0);
1922 static HRESULT AVIFILE_LoadIndex(const IAVIFileImpl *This, DWORD size, DWORD offset)
1926 HRESULT hr = AVIERR_OK;
1927 BOOL bAbsolute = TRUE;
1929 lp = HeapAlloc(GetProcessHeap(), 0, IDX_PER_BLOCK * sizeof(AVIINDEXENTRY));
1931 return AVIERR_MEMORY;
1933 /* adjust limits for index tables, so that inserting will be faster */
1934 for (n = 0; n < This->fInfo.dwStreams; n++) {
1935 IAVIStreamImpl *pStream = This->ppStreams[n];
1937 pStream->lLastFrame = -1;
1939 if (pStream->idxFrames != NULL) {
1940 HeapFree(GetProcessHeap(), 0, pStream->idxFrames);
1941 pStream->idxFrames = NULL;
1942 pStream->nIdxFrames = 0;
1945 if (pStream->sInfo.dwSampleSize != 0) {
1946 if (n > 0 && This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
1947 pStream->nIdxFrames = This->ppStreams[0]->nIdxFrames;
1948 } else if (pStream->sInfo.dwSuggestedBufferSize) {
1949 pStream->nIdxFrames =
1950 pStream->sInfo.dwLength / pStream->sInfo.dwSuggestedBufferSize;
1953 pStream->nIdxFrames = pStream->sInfo.dwLength;
1955 pStream->idxFrames =
1956 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pStream->nIdxFrames * sizeof(AVIINDEXENTRY));
1957 if (pStream->idxFrames == NULL && pStream->nIdxFrames > 0) {
1958 pStream->nIdxFrames = 0;
1959 HeapFree(GetProcessHeap(), 0, lp);
1960 return AVIERR_MEMORY;
1966 LONG read = min(IDX_PER_BLOCK * sizeof(AVIINDEXENTRY), size);
1968 if (mmioRead(This->hmmio, (HPSTR)lp, read) != read) {
1969 hr = AVIERR_FILEREAD;
1974 if (pos == (DWORD)-1)
1975 pos = offset - lp->dwChunkOffset + sizeof(DWORD);
1977 AVIFILE_ParseIndex(This, lp, read / sizeof(AVIINDEXENTRY),
1981 HeapFree(GetProcessHeap(), 0, lp);
1984 for (n = 0; n < This->fInfo.dwStreams; n++) {
1985 IAVIStreamImpl *pStream = This->ppStreams[n];
1987 if (pStream->sInfo.dwSampleSize == 0 &&
1988 pStream->sInfo.dwLength != pStream->lLastFrame+1)
1989 ERR("stream %u length mismatch: dwLength=%u found=%d\n",
1990 n, pStream->sInfo.dwLength, pStream->lLastFrame);
1996 static HRESULT AVIFILE_ParseIndex(const IAVIFileImpl *This, AVIINDEXENTRY *lp,
1997 LONG count, DWORD pos, BOOL *bAbsolute)
2000 return AVIERR_BADPARAM;
2002 for (; count > 0; count--, lp++) {
2003 WORD nStream = StreamFromFOURCC(lp->ckid);
2005 if (lp->ckid == listtypeAVIRECORD || nStream == 0x7F)
2006 continue; /* skip these */
2008 if (nStream > This->fInfo.dwStreams)
2009 return AVIERR_BADFORMAT;
2011 if (*bAbsolute && lp->dwChunkOffset < This->dwMoviChunkPos)
2015 lp->dwChunkOffset += sizeof(DWORD);
2017 lp->dwChunkOffset += pos;
2019 if (FAILED(AVIFILE_AddFrame(This->ppStreams[nStream], lp->ckid, lp->dwChunkLength, lp->dwChunkOffset, lp->dwFlags)))
2020 return AVIERR_MEMORY;
2026 static HRESULT AVIFILE_ReadBlock(IAVIStreamImpl *This, DWORD pos,
2027 LPVOID buffer, DWORD size)
2029 /* pre-conditions */
2030 assert(This != NULL);
2031 assert(This->paf != NULL);
2032 assert(This->paf->hmmio != NULL);
2033 assert(This->sInfo.dwStart <= pos && pos < This->sInfo.dwLength);
2034 assert(pos <= This->lLastFrame);
2036 /* should we read as much as block gives us? */
2037 if (size == 0 || size > This->idxFrames[pos].dwChunkLength)
2038 size = This->idxFrames[pos].dwChunkLength;
2040 /* read into out own buffer or given one? */
2041 if (buffer == NULL) {
2042 /* we also read the chunk */
2043 size += 2 * sizeof(DWORD);
2045 /* check that buffer is big enough -- don't trust dwSuggestedBufferSize */
2046 if (This->lpBuffer == NULL || This->cbBuffer < size) {
2047 DWORD maxSize = max(size, This->sInfo.dwSuggestedBufferSize);
2049 if (This->lpBuffer == NULL) {
2050 This->lpBuffer = HeapAlloc(GetProcessHeap(), 0, maxSize);
2051 if (!This->lpBuffer) return AVIERR_MEMORY;
2053 void *new_buffer = HeapReAlloc(GetProcessHeap(), 0, This->lpBuffer, maxSize);
2054 if (!new_buffer) return AVIERR_MEMORY;
2055 This->lpBuffer = new_buffer;
2057 This->cbBuffer = maxSize;
2060 /* now read the complete chunk into our buffer */
2061 if (mmioSeek(This->paf->hmmio, This->idxFrames[pos].dwChunkOffset, SEEK_SET) == -1)
2062 return AVIERR_FILEREAD;
2063 if (mmioRead(This->paf->hmmio, (HPSTR)This->lpBuffer, size) != size)
2064 return AVIERR_FILEREAD;
2066 /* check if it was the correct block which we have read */
2067 if (This->lpBuffer[0] != This->idxFrames[pos].ckid ||
2068 This->lpBuffer[1] != This->idxFrames[pos].dwChunkLength) {
2069 ERR(": block %d not found at 0x%08X\n", pos, This->idxFrames[pos].dwChunkOffset);
2070 ERR(": Index says: '%4.4s'(0x%08X) size 0x%08X\n",
2071 (char*)&This->idxFrames[pos].ckid, This->idxFrames[pos].ckid,
2072 This->idxFrames[pos].dwChunkLength);
2073 ERR(": Data says: '%4.4s'(0x%08X) size 0x%08X\n",
2074 (char*)&This->lpBuffer[0], This->lpBuffer[0], This->lpBuffer[1]);
2075 return AVIERR_FILEREAD;
2078 if (mmioSeek(This->paf->hmmio, This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD), SEEK_SET) == -1)
2079 return AVIERR_FILEREAD;
2080 if (mmioRead(This->paf->hmmio, buffer, size) != size)
2081 return AVIERR_FILEREAD;
2087 static void AVIFILE_SamplesToBlock(const IAVIStreamImpl *This, LPLONG pos, LPLONG offset)
2091 /* pre-conditions */
2092 assert(This != NULL);
2093 assert(pos != NULL);
2094 assert(offset != NULL);
2095 assert(This->sInfo.dwSampleSize != 0);
2096 assert(*pos >= This->sInfo.dwStart);
2098 /* convert start sample to start bytes */
2099 (*offset) = (*pos) - This->sInfo.dwStart;
2100 (*offset) *= This->sInfo.dwSampleSize;
2102 /* convert bytes to block number */
2103 for (block = 0; block <= This->lLastFrame; block++) {
2104 if (This->idxFrames[block].dwChunkLength <= *offset)
2105 (*offset) -= This->idxFrames[block].dwChunkLength;
2113 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
2115 MainAVIHeader MainAVIHdr;
2116 IAVIStreamImpl* pStream;
2125 /* initialize some things */
2126 if (This->dwMoviChunkPos == 0)
2127 AVIFILE_ComputeMoviStart(This);
2129 /* written one record to much? */
2130 if (This->ckLastRecord.dwFlags & MMIO_DIRTY) {
2131 This->dwNextFramePos -= 3 * sizeof(DWORD);
2132 if (This->nIdxRecords > 0)
2133 This->nIdxRecords--;
2136 AVIFILE_UpdateInfo(This);
2138 assert(This->fInfo.dwScale != 0);
2140 memset(&MainAVIHdr, 0, sizeof(MainAVIHdr));
2141 MainAVIHdr.dwMicroSecPerFrame = MulDiv(This->fInfo.dwRate, 1000000,
2142 This->fInfo.dwScale);
2143 MainAVIHdr.dwMaxBytesPerSec = This->fInfo.dwMaxBytesPerSec;
2144 MainAVIHdr.dwPaddingGranularity = AVI_HEADERSIZE;
2145 MainAVIHdr.dwFlags = This->fInfo.dwFlags;
2146 MainAVIHdr.dwTotalFrames = This->fInfo.dwLength;
2147 MainAVIHdr.dwInitialFrames = 0;
2148 MainAVIHdr.dwStreams = This->fInfo.dwStreams;
2149 MainAVIHdr.dwSuggestedBufferSize = This->fInfo.dwSuggestedBufferSize;
2150 MainAVIHdr.dwWidth = This->fInfo.dwWidth;
2151 MainAVIHdr.dwHeight = This->fInfo.dwHeight;
2152 MainAVIHdr.dwInitialFrames = This->dwInitialFrames;
2154 /* now begin writing ... */
2155 mmioSeek(This->hmmio, 0, SEEK_SET);
2159 ckRIFF.fccType = formtypeAVI;
2160 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
2161 return AVIERR_FILEWRITE;
2163 /* AVI headerlist */
2165 ckLIST1.fccType = listtypeAVIHEADER;
2166 if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2167 return AVIERR_FILEWRITE;
2170 ck.ckid = ckidAVIMAINHDR;
2171 ck.cksize = sizeof(MainAVIHdr);
2173 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2174 return AVIERR_FILEWRITE;
2175 if (mmioWrite(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
2176 return AVIERR_FILEWRITE;
2177 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2178 return AVIERR_FILEWRITE;
2180 /* write the headers of each stream into a separate streamheader list */
2181 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2182 AVIStreamHeader strHdr;
2184 pStream = This->ppStreams[nStream];
2186 /* begin the new streamheader list */
2188 ckLIST2.fccType = listtypeSTREAMHEADER;
2189 if (mmioCreateChunk(This->hmmio, &ckLIST2, MMIO_CREATELIST) != S_OK)
2190 return AVIERR_FILEWRITE;
2192 /* create an AVIStreamHeader from the AVSTREAMINFO */
2193 strHdr.fccType = pStream->sInfo.fccType;
2194 strHdr.fccHandler = pStream->sInfo.fccHandler;
2195 strHdr.dwFlags = pStream->sInfo.dwFlags;
2196 strHdr.wPriority = pStream->sInfo.wPriority;
2197 strHdr.wLanguage = pStream->sInfo.wLanguage;
2198 strHdr.dwInitialFrames = pStream->sInfo.dwInitialFrames;
2199 strHdr.dwScale = pStream->sInfo.dwScale;
2200 strHdr.dwRate = pStream->sInfo.dwRate;
2201 strHdr.dwStart = pStream->sInfo.dwStart;
2202 strHdr.dwLength = pStream->sInfo.dwLength;
2203 strHdr.dwSuggestedBufferSize = pStream->sInfo.dwSuggestedBufferSize;
2204 strHdr.dwQuality = pStream->sInfo.dwQuality;
2205 strHdr.dwSampleSize = pStream->sInfo.dwSampleSize;
2206 strHdr.rcFrame.left = pStream->sInfo.rcFrame.left;
2207 strHdr.rcFrame.top = pStream->sInfo.rcFrame.top;
2208 strHdr.rcFrame.right = pStream->sInfo.rcFrame.right;
2209 strHdr.rcFrame.bottom = pStream->sInfo.rcFrame.bottom;
2211 /* now write the AVIStreamHeader */
2212 ck.ckid = ckidSTREAMHEADER;
2213 ck.cksize = sizeof(strHdr);
2214 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2215 return AVIERR_FILEWRITE;
2216 if (mmioWrite(This->hmmio, (HPSTR)&strHdr, ck.cksize) != ck.cksize)
2217 return AVIERR_FILEWRITE;
2218 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2219 return AVIERR_FILEWRITE;
2221 /* ... the hopefully ever present streamformat ... */
2222 ck.ckid = ckidSTREAMFORMAT;
2223 ck.cksize = pStream->cbFormat;
2224 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2225 return AVIERR_FILEWRITE;
2226 if (pStream->lpFormat != NULL && ck.cksize > 0) {
2227 if (mmioWrite(This->hmmio, pStream->lpFormat, ck.cksize) != ck.cksize)
2228 return AVIERR_FILEWRITE;
2230 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2231 return AVIERR_FILEWRITE;
2233 /* ... some optional existing handler data ... */
2234 if (pStream->lpHandlerData != NULL && pStream->cbHandlerData > 0) {
2235 ck.ckid = ckidSTREAMHANDLERDATA;
2236 ck.cksize = pStream->cbHandlerData;
2237 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2238 return AVIERR_FILEWRITE;
2239 if (mmioWrite(This->hmmio, pStream->lpHandlerData, ck.cksize) != ck.cksize)
2240 return AVIERR_FILEWRITE;
2241 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2242 return AVIERR_FILEWRITE;
2245 /* ... some optional additional extra chunk for this stream ... */
2246 if (pStream->extra.lp != NULL && pStream->extra.cb > 0) {
2247 /* the chunk header(s) are already in the structure */
2248 if (mmioWrite(This->hmmio, pStream->extra.lp, pStream->extra.cb) != pStream->extra.cb)
2249 return AVIERR_FILEWRITE;
2252 /* ... an optional name for this stream ... */
2253 if (lstrlenW(pStream->sInfo.szName) > 0) {
2256 ck.ckid = ckidSTREAMNAME;
2257 ck.cksize = lstrlenW(pStream->sInfo.szName) + 1;
2258 if (ck.cksize & 1) /* align */
2260 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2261 return AVIERR_FILEWRITE;
2263 /* the streamname must be saved in ASCII not Unicode */
2264 str = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
2266 return AVIERR_MEMORY;
2267 WideCharToMultiByte(CP_ACP, 0, pStream->sInfo.szName, -1, str,
2268 ck.cksize, NULL, NULL);
2270 if (mmioWrite(This->hmmio, str, ck.cksize) != ck.cksize) {
2271 HeapFree(GetProcessHeap(), 0, str);
2272 return AVIERR_FILEWRITE;
2275 HeapFree(GetProcessHeap(), 0, str);
2276 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2277 return AVIERR_FILEWRITE;
2280 /* close streamheader list for this stream */
2281 if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
2282 return AVIERR_FILEWRITE;
2283 } /* for (0 <= nStream < MainAVIHdr.dwStreams) */
2285 /* close the aviheader list */
2286 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2287 return AVIERR_FILEWRITE;
2289 /* check for padding to pre-guessed 'movi'-chunk position */
2290 dwPos = ckLIST1.dwDataOffset + ckLIST1.cksize;
2291 if (This->dwMoviChunkPos - 2 * sizeof(DWORD) > dwPos) {
2292 ck.ckid = ckidAVIPADDING;
2293 ck.cksize = This->dwMoviChunkPos - dwPos - 4 * sizeof(DWORD);
2294 assert((LONG)ck.cksize >= 0);
2296 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2297 return AVIERR_FILEWRITE;
2298 if (mmioSeek(This->hmmio, ck.cksize, SEEK_CUR) == -1)
2299 return AVIERR_FILEWRITE;
2300 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2301 return AVIERR_FILEWRITE;
2304 /* now write the 'movi' chunk */
2305 mmioSeek(This->hmmio, This->dwMoviChunkPos - 2 * sizeof(DWORD), SEEK_SET);
2307 ckLIST1.fccType = listtypeAVIMOVIE;
2308 if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2309 return AVIERR_FILEWRITE;
2310 if (mmioSeek(This->hmmio, This->dwNextFramePos, SEEK_SET) == -1)
2311 return AVIERR_FILEWRITE;
2312 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2313 return AVIERR_FILEWRITE;
2315 /* write 'idx1' chunk */
2316 hr = AVIFILE_SaveIndex(This);
2320 /* write optional extra file chunks */
2321 if (This->fileextra.lp != NULL && This->fileextra.cb > 0) {
2322 /* as for the streams, are the chunk header(s) in the structure */
2323 if (mmioWrite(This->hmmio, This->fileextra.lp, This->fileextra.cb) != This->fileextra.cb)
2324 return AVIERR_FILEWRITE;
2327 /* close RIFF chunk */
2328 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
2329 return AVIERR_FILEWRITE;
2331 /* add some JUNK at end for bad parsers */
2332 memset(&ckRIFF, 0, sizeof(ckRIFF));
2333 mmioWrite(This->hmmio, (HPSTR)&ckRIFF, sizeof(ckRIFF));
2334 mmioFlush(This->hmmio, 0);
2339 static HRESULT AVIFILE_SaveIndex(const IAVIFileImpl *This)
2341 IAVIStreamImpl *pStream;
2347 ck.ckid = ckidAVINEWINDEX;
2349 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2350 return AVIERR_FILEWRITE;
2352 if (This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
2353 /* is interleaved -- write block of corresponding frames */
2354 LONG lInitialFrames = 0;
2358 if (This->ppStreams[0]->sInfo.dwSampleSize == 0)
2361 stepsize = AVIStreamTimeToSample((PAVISTREAM)This->ppStreams[0], 1000000);
2363 assert(stepsize > 0);
2365 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2366 if (lInitialFrames < This->ppStreams[nStream]->sInfo.dwInitialFrames)
2367 lInitialFrames = This->ppStreams[nStream]->sInfo.dwInitialFrames;
2370 for (i = -lInitialFrames; i < (LONG)This->fInfo.dwLength - lInitialFrames;
2372 DWORD nFrame = lInitialFrames + i;
2374 assert(nFrame < This->nIdxRecords);
2376 idx.ckid = listtypeAVIRECORD;
2377 idx.dwFlags = AVIIF_LIST;
2378 idx.dwChunkLength = This->idxRecords[nFrame].dwChunkLength;
2379 idx.dwChunkOffset = This->idxRecords[nFrame].dwChunkOffset
2380 - This->dwMoviChunkPos;
2381 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2382 return AVIERR_FILEWRITE;
2384 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2385 pStream = This->ppStreams[nStream];
2387 /* heave we reached start of this stream? */
2388 if (-(LONG)pStream->sInfo.dwInitialFrames > i)
2391 if (pStream->sInfo.dwInitialFrames < lInitialFrames)
2392 nFrame -= (lInitialFrames - pStream->sInfo.dwInitialFrames);
2394 /* reached end of this stream? */
2395 if (pStream->lLastFrame <= nFrame)
2398 if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2399 pStream->sInfo.dwFormatChangeCount != 0 &&
2400 pStream->idxFmtChanges != NULL) {
2403 for (pos = 0; pos < pStream->sInfo.dwFormatChangeCount; pos++) {
2404 if (pStream->idxFmtChanges[pos].ckid == nFrame) {
2405 idx.dwFlags = AVIIF_NOTIME;
2406 idx.ckid = MAKEAVICKID(cktypePALchange, pStream->nStream);
2407 idx.dwChunkLength = pStream->idxFmtChanges[pos].dwChunkLength;
2408 idx.dwChunkOffset = pStream->idxFmtChanges[pos].dwChunkOffset
2409 - This->dwMoviChunkPos;
2411 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2412 return AVIERR_FILEWRITE;
2416 } /* if have formatchanges */
2418 idx.ckid = pStream->idxFrames[nFrame].ckid;
2419 idx.dwFlags = pStream->idxFrames[nFrame].dwFlags;
2420 idx.dwChunkLength = pStream->idxFrames[nFrame].dwChunkLength;
2421 idx.dwChunkOffset = pStream->idxFrames[nFrame].dwChunkOffset
2422 - This->dwMoviChunkPos;
2423 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2424 return AVIERR_FILEWRITE;
2428 /* not interleaved -- write index for each stream at once */
2429 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2430 pStream = This->ppStreams[nStream];
2432 for (n = 0; n <= pStream->lLastFrame; n++) {
2433 if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2434 (pStream->sInfo.dwFormatChangeCount != 0)) {
2437 for (pos = 0; pos < pStream->sInfo.dwFormatChangeCount; pos++) {
2438 if (pStream->idxFmtChanges[pos].ckid == n) {
2439 idx.dwFlags = AVIIF_NOTIME;
2440 idx.ckid = MAKEAVICKID(cktypePALchange, pStream->nStream);
2441 idx.dwChunkLength = pStream->idxFmtChanges[pos].dwChunkLength;
2443 pStream->idxFmtChanges[pos].dwChunkOffset - This->dwMoviChunkPos;
2444 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2445 return AVIERR_FILEWRITE;
2449 } /* if have formatchanges */
2451 idx.ckid = pStream->idxFrames[n].ckid;
2452 idx.dwFlags = pStream->idxFrames[n].dwFlags;
2453 idx.dwChunkLength = pStream->idxFrames[n].dwChunkLength;
2454 idx.dwChunkOffset = pStream->idxFrames[n].dwChunkOffset
2455 - This->dwMoviChunkPos;
2457 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2458 return AVIERR_FILEWRITE;
2461 } /* if not interleaved */
2463 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2464 return AVIERR_FILEWRITE;
2469 static ULONG AVIFILE_SearchStream(const IAVIFileImpl *This, DWORD fcc, LONG lSkip)
2478 /* search the number of the specified stream */
2479 nStream = (ULONG)-1;
2480 for (i = 0; i < This->fInfo.dwStreams; i++) {
2481 assert(This->ppStreams[i] != NULL);
2483 if (This->ppStreams[i]->sInfo.fccType == fcc) {
2497 static void AVIFILE_UpdateInfo(IAVIFileImpl *This)
2501 /* pre-conditions */
2502 assert(This != NULL);
2504 This->fInfo.dwMaxBytesPerSec = 0;
2505 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
2506 This->fInfo.dwSuggestedBufferSize = 0;
2507 This->fInfo.dwWidth = 0;
2508 This->fInfo.dwHeight = 0;
2509 This->fInfo.dwScale = 0;
2510 This->fInfo.dwRate = 0;
2511 This->fInfo.dwLength = 0;
2512 This->dwInitialFrames = 0;
2514 for (i = 0; i < This->fInfo.dwStreams; i++) {
2515 AVISTREAMINFOW *psi;
2518 /* pre-conditions */
2519 assert(This->ppStreams[i] != NULL);
2521 psi = &This->ppStreams[i]->sInfo;
2522 assert(psi->dwScale != 0);
2523 assert(psi->dwRate != 0);
2526 /* use first stream timings as base */
2527 This->fInfo.dwScale = psi->dwScale;
2528 This->fInfo.dwRate = psi->dwRate;
2529 This->fInfo.dwLength = psi->dwLength;
2531 n = AVIStreamSampleToSample((PAVISTREAM)This->ppStreams[0],
2532 (PAVISTREAM)This->ppStreams[i],psi->dwLength);
2533 if (This->fInfo.dwLength < n)
2534 This->fInfo.dwLength = n;
2537 if (This->dwInitialFrames < psi->dwInitialFrames)
2538 This->dwInitialFrames = psi->dwInitialFrames;
2540 if (This->fInfo.dwSuggestedBufferSize < psi->dwSuggestedBufferSize)
2541 This->fInfo.dwSuggestedBufferSize = psi->dwSuggestedBufferSize;
2543 if (psi->dwSampleSize != 0) {
2544 /* fixed sample size -- exact computation */
2545 This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSampleSize, psi->dwRate,
2548 /* variable sample size -- only upper limit */
2549 This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSuggestedBufferSize,
2550 psi->dwRate, psi->dwScale);
2552 /* update dimensions */
2553 n = psi->rcFrame.right - psi->rcFrame.left;
2554 if (This->fInfo.dwWidth < n)
2555 This->fInfo.dwWidth = n;
2556 n = psi->rcFrame.bottom - psi->rcFrame.top;
2557 if (This->fInfo.dwHeight < n)
2558 This->fInfo.dwHeight = n;
2563 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
2564 FOURCC ckid, DWORD flags, LPCVOID buffer,
2573 /* if no frame/block is already written, we must compute start of movi chunk */
2574 if (This->paf->dwMoviChunkPos == 0)
2575 AVIFILE_ComputeMoviStart(This->paf);
2577 if (mmioSeek(This->paf->hmmio, This->paf->dwNextFramePos, SEEK_SET) == -1)
2578 return AVIERR_FILEWRITE;
2580 if (mmioCreateChunk(This->paf->hmmio, &ck, 0) != S_OK)
2581 return AVIERR_FILEWRITE;
2582 if (buffer != NULL && size > 0) {
2583 if (mmioWrite(This->paf->hmmio, buffer, size) != size)
2584 return AVIERR_FILEWRITE;
2586 if (mmioAscend(This->paf->hmmio, &ck, 0) != S_OK)
2587 return AVIERR_FILEWRITE;
2589 This->paf->fDirty = TRUE;
2590 This->paf->dwNextFramePos = mmioSeek(This->paf->hmmio, 0, SEEK_CUR);
2592 return AVIFILE_AddFrame(This, ckid, size,
2593 ck.dwDataOffset - 2 * sizeof(DWORD), flags);