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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
33 #define COM_NO_WINDOWS_H
47 #include "avifile_private.h"
48 #include "extrachunk.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
56 #define IDX_PER_BLOCK 2730
59 /***********************************************************************/
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);
72 static const struct IAVIFileVtbl iavift = {
73 IAVIFile_fnQueryInterface,
78 IAVIFile_fnCreateStream,
82 IAVIFile_fnDeleteStream
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);
95 static const struct IPersistFileVtbl ipersistft = {
96 IPersistFile_fnQueryInterface,
97 IPersistFile_fnAddRef,
98 IPersistFile_fnRelease,
99 IPersistFile_fnGetClassID,
100 IPersistFile_fnIsDirty,
103 IPersistFile_fnSaveCompleted,
104 IPersistFile_fnGetCurFile
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);
122 static const struct IAVIStreamVtbl iavist = {
123 IAVIStream_fnQueryInterface,
125 IAVIStream_fnRelease,
128 IAVIStream_fnFindSample,
129 IAVIStream_fnReadFormat,
130 IAVIStream_fnSetFormat,
134 IAVIStream_fnReadData,
135 IAVIStream_fnWriteData,
139 typedef struct _IAVIFileImpl IAVIFileImpl;
141 typedef struct _IPersistFileImpl {
143 const IPersistFileVtbl *lpVtbl;
145 /* IPersistFile stuff */
149 typedef struct _IAVIStreamImpl {
151 const IAVIStreamVtbl *lpVtbl;
154 /* IAVIStream stuff */
156 DWORD nStream; /* the n-th stream in file */
157 AVISTREAMINFOW sInfo;
162 LPVOID lpHandlerData;
168 DWORD cbBuffer; /* size of lpBuffer */
169 DWORD dwCurrentFrame; /* frame/block currently in lpBuffer */
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 */
178 struct _IAVIFileImpl {
180 const IAVIFileVtbl *lpVtbl;
183 /* IAVIFile stuff... */
184 IPersistFileImpl iPersistFile;
187 IAVIStreamImpl *ppStreams[MAX_AVISTREAMS];
189 EXTRACHUNKS fileextra;
191 DWORD dwMoviChunkPos; /* some stuff for saving ... */
193 DWORD dwNextFramePos;
194 DWORD dwInitialFrames;
196 MMCKINFO ckLastRecord;
197 AVIINDEXENTRY *idxRecords; /* won't be updated while loading */
198 DWORD nIdxRecords; /* current fill level */
199 DWORD cbIdxRecords; /* size of idxRecords */
201 /* IPersistFile stuff ... */
208 /***********************************************************************/
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,
225 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
226 static HRESULT AVIFILE_SaveIndex(IAVIFileImpl *This);
227 static ULONG AVIFILE_SearchStream(IAVIFileImpl *This, DWORD fccType,
229 static void AVIFILE_UpdateInfo(IAVIFileImpl *This);
230 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
231 FOURCC ckid, DWORD flags, LPVOID buffer,
234 HRESULT AVIFILE_CreateAVIFile(REFIID riid, LPVOID *ppv)
239 assert(riid != NULL && ppv != NULL);
243 pfile = (IAVIFileImpl*)LocalAlloc(LPTR, sizeof(IAVIFileImpl));
245 return AVIERR_MEMORY;
247 pfile->lpVtbl = &iavift;
249 pfile->iPersistFile.lpVtbl = &ipersistft;
250 pfile->iPersistFile.paf = pfile;
252 hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
254 LocalFree((HLOCAL)pfile);
259 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
262 IAVIFileImpl *This = (IAVIFileImpl *)iface;
264 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
266 if (IsEqualGUID(&IID_IUnknown, refiid) ||
267 IsEqualGUID(&IID_IAVIFile, refiid)) {
269 IAVIFile_AddRef(iface);
272 } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
273 *obj = &This->iPersistFile;
274 IAVIFile_AddRef(iface);
279 return OLE_E_ENUM_NOMORE;
282 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
284 IAVIFileImpl *This = (IAVIFileImpl *)iface;
285 ULONG ref = InterlockedIncrement(&This->ref);
287 TRACE("(%p) -> %ld\n", iface, ref);
292 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
294 IAVIFileImpl *This = (IAVIFileImpl *)iface;
296 ULONG ref = InterlockedDecrement(&This->ref);
298 TRACE("(%p) -> %ld\n", iface, ref);
302 /* need to write headers to file */
303 AVIFILE_SaveFile(This);
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]);
312 AVIFILE_DestructAVIStream(This->ppStreams[i]);
313 LocalFree((HLOCAL)This->ppStreams[i]);
314 This->ppStreams[i] = NULL;
318 if (This->idxRecords != NULL) {
319 GlobalFreePtr(This->idxRecords);
320 This->idxRecords = NULL;
321 This->nIdxRecords = 0;
324 if (This->fileextra.lp != NULL) {
325 GlobalFreePtr(This->fileextra.lp);
326 This->fileextra.lp = NULL;
327 This->fileextra.cb = 0;
330 if (This->szFileName != NULL) {
331 LocalFree((HLOCAL)This->szFileName);
332 This->szFileName = NULL;
334 if (This->hmmio != NULL) {
335 mmioClose(This->hmmio, 0);
339 LocalFree((HLOCAL)This);
344 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
347 IAVIFileImpl *This = (IAVIFileImpl *)iface;
349 TRACE("(%p,%p,%ld)\n",iface,afi,size);
352 return AVIERR_BADPARAM;
354 return AVIERR_BADSIZE;
356 AVIFILE_UpdateInfo(This);
358 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
360 if ((DWORD)size < sizeof(This->fInfo))
361 return AVIERR_BUFFERTOOSMALL;
365 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
366 DWORD fccType, LONG lParam)
368 IAVIFileImpl *This = (IAVIFileImpl *)iface;
372 TRACE("(%p,%p,0x%08lX,%ld)\n", iface, avis, fccType, lParam);
374 if (avis == NULL || lParam < 0)
375 return AVIERR_BADPARAM;
377 nStream = AVIFILE_SearchStream(This, fccType, lParam);
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);
388 /* Sorry, but the specified stream doesn't exist */
389 return AVIERR_NODATA;
392 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
393 LPAVISTREAMINFOW asi)
395 IAVIFileImpl *This = (IAVIFileImpl *)iface;
399 TRACE("(%p,%p,%p)\n", iface, avis, asi);
401 /* check parameters */
402 if (avis == NULL || asi == NULL)
403 return AVIERR_BADPARAM;
407 /* Does the user have write permission? */
408 if ((This->uMode & MMIO_RWMODE) == 0)
409 return AVIERR_READONLY;
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;
419 /* check AVISTREAMINFO for some really needed things */
420 if (asi->fccType == 0 || asi->dwScale == 0 || asi->dwRate == 0)
421 return AVIERR_BADFORMAT;
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;
430 /* initialize the new allocated stream */
431 AVIFILE_ConstructAVIStream(This, n, asi);
433 This->fInfo.dwStreams++;
436 /* update our AVIFILEINFO structure */
437 AVIFILE_UpdateInfo(This);
440 *avis = (PAVISTREAM)This->ppStreams[n];
441 IAVIStream_AddRef(*avis);
446 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
447 LPVOID lpData, LONG size)
449 IAVIFileImpl *This = (IAVIFileImpl *)iface;
451 TRACE("(%p,0x%08lX,%p,%ld)\n", iface, ckid, lpData, size);
453 /* check parameters */
455 return AVIERR_BADPARAM;
457 return AVIERR_BADSIZE;
459 /* Do we have write permission? */
460 if ((This->uMode & MMIO_RWMODE) == 0)
461 return AVIERR_READONLY;
465 return WriteExtraChunk(&This->fileextra, ckid, lpData, size);
468 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
469 LPVOID lpData, LONG *size)
471 IAVIFileImpl *This = (IAVIFileImpl *)iface;
473 TRACE("(%p,0x%08lX,%p,%p)\n", iface, ckid, lpData, size);
475 return ReadExtraChunk(&This->fileextra, ckid, lpData, size);
478 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
480 IAVIFileImpl *This = (IAVIFileImpl *)iface;
482 TRACE("(%p)\n",iface);
484 if ((This->uMode & MMIO_RWMODE) == 0)
485 return AVIERR_READONLY;
489 /* no frames written to any stream? -- compute start of 'movi'-chunk */
490 if (This->dwMoviChunkPos == 0)
491 AVIFILE_ComputeMoviStart(This);
493 This->fInfo.dwFlags |= AVIFILEINFO_ISINTERLEAVED;
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;
501 AVIFILE_AddRecord(This);
503 if (This->fInfo.dwSuggestedBufferSize < This->ckLastRecord.cksize + 3 * sizeof(DWORD))
504 This->fInfo.dwSuggestedBufferSize = This->ckLastRecord.cksize + 3 * sizeof(DWORD);
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);
519 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
522 IAVIFileImpl *This = (IAVIFileImpl *)iface;
526 TRACE("(%p,0x%08lX,%ld)\n", iface, fccType, lParam);
528 /* check parameter */
530 return AVIERR_BADPARAM;
532 /* Have user write permissions? */
533 if ((This->uMode & MMIO_RWMODE) == 0)
534 return AVIERR_READONLY;
536 nStream = AVIFILE_SearchStream(This, fccType, lParam);
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]);
544 if (This->fInfo.dwStreams - nStream > 0)
545 memcpy(This->ppStreams + nStream, This->ppStreams + nStream + 1,
546 (This->fInfo.dwStreams - nStream) * sizeof(IAVIStreamImpl*));
548 This->ppStreams[This->fInfo.dwStreams] = NULL;
549 This->fInfo.dwStreams--;
552 /* This->fInfo will be updated further when asked for */
555 return AVIERR_NODATA;
558 /***********************************************************************/
560 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
561 REFIID refiid, LPVOID *obj)
563 IPersistFileImpl *This = (IPersistFileImpl *)iface;
565 assert(This->paf != NULL);
567 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
570 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
572 IPersistFileImpl *This = (IPersistFileImpl *)iface;
574 assert(This->paf != NULL);
576 return IAVIFile_AddRef((PAVIFILE)This->paf);
579 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface)
581 IPersistFileImpl *This = (IPersistFileImpl *)iface;
583 assert(This->paf != NULL);
585 return IAVIFile_Release((PAVIFILE)This->paf);
588 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
591 TRACE("(%p,%p)\n", iface, pClassID);
593 if (pClassID == NULL)
594 return AVIERR_BADPARAM;
596 memcpy(pClassID, &CLSID_AVIFile, sizeof(CLSID_AVIFile));
601 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
603 IPersistFileImpl *This = (IPersistFileImpl *)iface;
605 TRACE("(%p)\n", iface);
607 assert(This->paf != NULL);
609 return (This->paf->fDirty ? S_OK : S_FALSE);
612 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
613 LPCOLESTR pszFileName, DWORD dwMode)
615 IPersistFileImpl *This = (IPersistFileImpl *)iface;
619 TRACE("(%p,%s,0x%08lX)\n", iface, debugstr_w(pszFileName), dwMode);
621 /* check parameter */
622 if (pszFileName == NULL)
623 return AVIERR_BADPARAM;
625 assert(This->paf != NULL);
626 if (This->paf->hmmio != NULL)
627 return AVIERR_ERROR; /* No reuse of this object for another file! */
629 /* remeber mode and name */
630 This->paf->uMode = dwMode;
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);
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 */
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;
651 WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1, szFileName,
654 This->paf->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
655 LocalFree((HLOCAL)szFileName);
656 if (This->paf->hmmio == NULL)
657 return AVIERR_FILEOPEN;
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;
667 return AVIFILE_LoadFile(This->paf);
670 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
671 LPCOLESTR pszFileName,BOOL fRemember)
673 TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
675 /* We write directly to disk, so nothing to do. */
680 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
681 LPCOLESTR pszFileName)
683 TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
685 /* We write directly to disk, so nothing to do. */
690 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
691 LPOLESTR *ppszFileName)
693 IPersistFileImpl *This = (IPersistFileImpl *)iface;
695 TRACE("(%p,%p)\n", iface, ppszFileName);
697 if (ppszFileName == NULL)
698 return AVIERR_BADPARAM;
700 *ppszFileName = NULL;
702 assert(This->paf != NULL);
704 if (This->paf->szFileName != NULL) {
705 int len = lstrlenW(This->paf->szFileName) + 1;
707 *ppszFileName = (LPOLESTR)GlobalAllocPtr(GHND, len * sizeof(WCHAR));
708 if (*ppszFileName == NULL)
709 return AVIERR_MEMORY;
711 strcpyW(*ppszFileName, This->paf->szFileName);
717 /***********************************************************************/
719 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
720 REFIID refiid, LPVOID *obj)
722 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
724 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(refiid), obj);
726 if (IsEqualGUID(&IID_IUnknown, refiid) ||
727 IsEqualGUID(&IID_IAVIStream, refiid)) {
729 IAVIStream_AddRef(iface);
733 /* FIXME: IAVIStreaming interface */
735 return OLE_E_ENUM_NOMORE;
738 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
740 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
741 ULONG ref = InterlockedIncrement(&This->ref);
743 TRACE("(%p) -> %ld\n", iface, ref);
745 /* also add ref to parent, so that it doesn't kill us */
746 if (This->paf != NULL)
747 IAVIFile_AddRef((PAVIFILE)This->paf);
752 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
754 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
755 ULONG ref = InterlockedDecrement(&This->ref);
757 TRACE("(%p) -> %ld\n", iface, ref);
759 if (This->paf != NULL)
760 IAVIFile_Release((PAVIFILE)This->paf);
765 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
768 TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
770 /* This IAVIStream interface needs an AVIFile */
771 return AVIERR_UNSUPPORTED;
774 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
777 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
779 TRACE("(%p,%p,%ld)\n", iface, psi, size);
782 return AVIERR_BADPARAM;
784 return AVIERR_BADSIZE;
786 memcpy(psi, &This->sInfo, min((DWORD)size, sizeof(This->sInfo)));
788 if ((DWORD)size < sizeof(This->sInfo))
789 return AVIERR_BUFFERTOOSMALL;
793 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
796 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
800 TRACE("(%p,%ld,0x%08lX)\n",iface,pos,flags);
802 if (flags & FIND_FROM_START) {
803 pos = This->sInfo.dwStart;
804 flags &= ~(FIND_FROM_START|FIND_PREV);
808 if (This->sInfo.dwSampleSize != 0) {
809 /* convert samples into block number with offset */
810 AVIFILE_SamplesToBlock(This, &pos, &offset);
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)
819 if (flags & FIND_NEXT)
824 } else if (flags & FIND_ANY) {
825 while (0 <= pos && pos <= This->lLastFrame) {
826 if (This->idxFrames[pos].dwChunkLength > 0)
829 if (flags & FIND_NEXT)
835 } else if ((flags & FIND_FORMAT) && This->idxFmtChanges != NULL &&
836 This->sInfo.fccType == streamtypeVIDEO) {
837 if (flags & FIND_NEXT) {
840 for (n = 0; n < This->sInfo.dwFormatChangeCount; n++)
841 if (This->idxFmtChanges[n].ckid >= pos) {
842 pos = This->idxFmtChanges[n].ckid;
848 for (n = (LONG)This->sInfo.dwFormatChangeCount; n >= 0; n--) {
849 if (This->idxFmtChanges[n].ckid <= pos) {
850 pos = This->idxFmtChanges[n].ckid;
855 if (pos > (LONG)This->sInfo.dwStart)
856 return 0; /* format changes always for first frame */
864 if (pos < (LONG)This->sInfo.dwStart)
867 switch (flags & FIND_RET) {
870 pos = This->idxFrames[pos].dwChunkLength;
873 /* physical position */
874 pos = This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD)
875 + offset * This->sInfo.dwSampleSize;
879 if (This->sInfo.dwSampleSize)
880 pos = This->sInfo.dwSampleSize;
885 FIXME(": FIND_INDEX flag is not supported!\n");
886 /* This is an index in the index-table on disc. */
888 }; /* else logical position */
893 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
894 LPVOID format, LONG *formatsize)
896 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
898 TRACE("(%p,%ld,%p,%p)\n", iface, pos, format, formatsize);
900 if (formatsize == NULL)
901 return AVIERR_BADPARAM;
903 /* only interested in needed buffersize? */
904 if (format == NULL || *formatsize <= 0) {
905 *formatsize = This->cbFormat;
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;
917 /* Could format change? When yes will it change? */
918 if ((This->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
919 pos > This->sInfo.dwStart) {
922 lLastFmt = IAVIStream_fnFindSample(iface, pos, FIND_FORMAT|FIND_PREV);
924 FIXME(": need to read formatchange for %ld -- unimplemented!\n",lLastFmt);
928 *formatsize = This->cbFormat;
932 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
933 LPVOID format, LONG formatsize)
935 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
937 LPBITMAPINFOHEADER lpbiNew = (LPBITMAPINFOHEADER)format;
939 TRACE("(%p,%ld,%p,%ld)\n", iface, pos, format, formatsize);
941 /* check parameters */
942 if (format == NULL || formatsize <= 0)
943 return AVIERR_BADPARAM;
945 /* Do we have write permission? */
946 if ((This->paf->uMode & MMIO_RWMODE) == 0)
947 return AVIERR_READONLY;
949 /* can only set format before frame is written! */
950 if (This->lLastFrame > pos)
951 return AVIERR_UNSUPPORTED;
953 /* initial format or a formatchange? */
954 if (This->lpFormat == NULL) {
956 if (This->paf->dwMoviChunkPos != 0)
957 return AVIERR_ERROR; /* user has used API in wrong sequnece! */
959 This->lpFormat = GlobalAllocPtr(GMEM_MOVEABLE, formatsize);
960 if (This->lpFormat == NULL)
961 return AVIERR_MEMORY;
962 This->cbFormat = formatsize;
964 memcpy(This->lpFormat, format, formatsize);
966 /* update some infos about stream */
967 if (This->sInfo.fccType == streamtypeVIDEO) {
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;
982 LPBITMAPINFOHEADER lpbiOld = (LPBITMAPINFOHEADER)This->lpFormat;
983 RGBQUAD *rgbNew = (RGBQUAD*)((LPBYTE)lpbiNew + lpbiNew->biSize);
984 AVIPALCHANGE *lppc = NULL;
987 /* perhaps format change, check it ... */
988 if (This->cbFormat != formatsize)
989 return AVIERR_UNSUPPORTED;
991 /* no format change, only the initial one */
992 if (memcmp(This->lpFormat, format, formatsize) == 0)
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;
1005 This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
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);
1012 return AVIERR_MEMORY;
1014 lppc->bFirstEntry = 0;
1015 lppc->bNumEntries = (lpbiOld->biClrUsed < 256 ? lpbiOld->biClrUsed : 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;
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);
1034 GlobalFreePtr(lppc);
1036 return AVIFILE_AddFrame(This, cktypePALchange, n, ck.dwDataOffset, 0);
1040 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
1041 LONG samples, LPVOID buffer,
1042 LONG buffersize, LPLONG bytesread,
1045 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1050 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface, start, samples, buffer,
1051 buffersize, bytesread, samplesread);
1053 /* clear return parameters if given */
1054 if (bytesread != NULL)
1056 if (samplesread != NULL)
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 */
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;
1071 if (This->sInfo.dwSampleSize != 0)
1072 samples = buffersize / This->sInfo.dwSampleSize;
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);
1083 /* nothing to read? Then leave ... */
1087 if (This->sInfo.dwSampleSize != 0) {
1088 /* fixed samplesize -- we can read over frame/block boundaries */
1089 ULONG block = start;
1092 /* convert start sample to block,offset pair */
1093 AVIFILE_SamplesToBlock(This, &block, &offset);
1095 /* convert samples to bytes */
1096 samples *= This->sInfo.dwSampleSize;
1098 while (samples > 0 && buffersize > 0) {
1099 if (block != This->dwCurrentFrame) {
1100 hr = AVIFILE_ReadBlock(This, block, NULL, 0);
1105 size = min((DWORD)samples, (DWORD)buffersize);
1106 size = min(size, This->cbBuffer - offset);
1107 memcpy(buffer, ((BYTE*)&This->lpBuffer[2]) + offset, size);
1111 buffer = ((LPBYTE)buffer)+size;
1115 /* fill out return parameters if given */
1116 if (bytesread != NULL)
1118 if (samplesread != NULL)
1119 *samplesread += size / This->sInfo.dwSampleSize;
1125 return AVIERR_BUFFERTOOSMALL;
1127 /* variable samplesize -- we can only read one full frame/block */
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);
1137 } else if (buffer != NULL)
1138 return AVIERR_BUFFERTOOSMALL;
1140 /* fill out return parameters if given */
1141 if (bytesread != NULL)
1143 if (samplesread != NULL)
1144 *samplesread = samples;
1150 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
1151 LONG samples, LPVOID buffer,
1152 LONG buffersize, DWORD flags,
1154 LPLONG byteswritten)
1156 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1161 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface, start, samples,
1162 buffer, buffersize, flags, sampwritten, byteswritten);
1164 /* clear return parameters if given */
1165 if (sampwritten != NULL)
1167 if (byteswritten != NULL)
1170 /* check parameters */
1171 if (buffer == NULL && (buffersize > 0 || samples > 0))
1172 return AVIERR_BADPARAM;
1174 /* Have we write permission? */
1175 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1176 return AVIERR_READONLY;
1178 switch (This->sInfo.fccType) {
1179 case streamtypeAUDIO:
1180 ckid = MAKEAVICKID(cktypeWAVEbytes, This->nStream);
1183 if ((flags & AVIIF_KEYFRAME) && buffersize != 0)
1184 ckid = MAKEAVICKID(cktypeDIBbits, This->nStream);
1186 ckid = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1190 /* append to end of stream? */
1192 if (This->lLastFrame == -1)
1193 start = This->sInfo.dwStart;
1195 start = This->sInfo.dwLength;
1196 } else if (This->lLastFrame == -1)
1197 This->sInfo.dwStart = start;
1199 if (This->sInfo.dwSampleSize != 0) {
1200 /* fixed sample size -- audio like */
1201 if (samples * This->sInfo.dwSampleSize != buffersize)
1202 return AVIERR_BADPARAM;
1204 /* Couldn't skip audio-like data -- User must supply appropriate silence */
1205 if (This->sInfo.dwLength != start)
1206 return AVIERR_UNSUPPORTED;
1208 /* Convert position to frame/block */
1209 start = This->lLastFrame + 1;
1211 if ((This->paf->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) == 0) {
1212 FIXME(": not interleaved, could collect audio data!\n");
1215 /* variable sample size -- video like */
1217 return AVIERR_UNSUPPORTED;
1219 /* must we fill up with empty frames? */
1220 if (This->lLastFrame != -1) {
1221 FOURCC ckid2 = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1223 while (start > This->lLastFrame + 1) {
1224 hr = AVIFILE_WriteBlock(This, This->lLastFrame + 1, ckid2, 0, NULL, 0);
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;
1244 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
1247 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1249 FIXME("(%p,%ld,%ld): stub\n", iface, start, samples);
1251 /* check parameters */
1252 if (start < 0 || samples < 0)
1253 return AVIERR_BADPARAM;
1255 /* Delete before start of stream? */
1256 if (start + samples < This->sInfo.dwStart)
1259 /* Delete after end of stream? */
1260 if (start > This->sInfo.dwLength)
1263 /* For the rest we need write permissions */
1264 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1265 return AVIERR_READONLY;
1267 /* 1. overwrite the data with JUNK
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.
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.
1280 return AVIERR_UNSUPPORTED;
1283 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1284 LPVOID lp, LPLONG lpread)
1286 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1288 TRACE("(%p,0x%08lX,%p,%p)\n", iface, fcc, lp, lpread);
1290 if (fcc == ckidSTREAMHANDLERDATA) {
1291 if (This->lpHandlerData != NULL && This->cbHandlerData > 0) {
1292 if (lp == NULL || *lpread <= 0) {
1293 *lpread = This->cbHandlerData;
1297 memcpy(lp, This->lpHandlerData, min(This->cbHandlerData, *lpread));
1298 if (*lpread < This->cbHandlerData)
1299 return AVIERR_BUFFERTOOSMALL;
1302 return AVIERR_NODATA;
1304 return ReadExtraChunk(&This->extra, fcc, lp, lpread);
1307 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1308 LPVOID lp, LONG size)
1310 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1312 TRACE("(%p,0x%08lx,%p,%ld)\n", iface, fcc, lp, size);
1314 /* check parameters */
1316 return AVIERR_BADPARAM;
1318 return AVIERR_BADSIZE;
1320 /* need write permission */
1321 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1322 return AVIERR_READONLY;
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
1328 DWORD dwPos = AVIFILE_ComputeMoviStart(This->paf);
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 */
1336 This->paf->fDirty = TRUE;
1338 if (fcc == ckidSTREAMHANDLERDATA) {
1339 if (This->lpHandlerData != NULL) {
1340 FIXME(": handler data already set -- overwirte?\n");
1341 return AVIERR_UNSUPPORTED;
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);
1352 return WriteExtraChunk(&This->extra, fcc, lp, size);
1355 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1356 LPAVISTREAMINFOW info, LONG infolen)
1358 FIXME("(%p,%p,%ld): stub\n", iface, info, infolen);
1363 /***********************************************************************/
1365 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;
1384 This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
1385 This->sInfo.dwFormatChangeCount++;
1387 if (This->idxFmtChanges == NULL || This->sInfo.dwFormatChangeCount < This->nIdxFmtChanges) {
1388 UINT n = This->sInfo.dwFormatChangeCount;
1390 This->nIdxFmtChanges += 16;
1391 if (This->idxFmtChanges == NULL)
1392 This->idxFmtChanges =
1393 GlobalAllocPtr(GHND, This->nIdxFmtChanges * sizeof(AVIINDEXENTRY));
1395 This->idxFmtChanges =
1396 GlobalReAllocPtr(This->idxFmtChanges,
1397 This->nIdxFmtChanges * sizeof(AVIINDEXENTRY), GHND);
1398 if (This->idxFmtChanges == NULL)
1399 return AVIERR_MEMORY;
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;
1409 case cktypeWAVEbytes:
1410 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1411 flags |= AVIIF_KEYFRAME;
1414 WARN(": unknown TWOCC 0x%04X found\n", TWOCCFromFOURCC(ckid));
1418 /* first frame is alwasy a keyframe */
1419 if (This->lLastFrame == -1)
1420 flags |= AVIIF_KEYFRAME;
1422 if (This->sInfo.dwSuggestedBufferSize < size)
1423 This->sInfo.dwSuggestedBufferSize = size;
1425 /* get memory for index */
1426 if (This->idxFrames == NULL || This->lLastFrame + 1 >= This->nIdxFrames) {
1427 This->nIdxFrames += 512;
1428 if (This->idxFrames == NULL)
1430 GlobalAllocPtr(GHND, This->nIdxFrames * sizeof(AVIINDEXENTRY));
1433 GlobalReAllocPtr(This->idxFrames,
1434 This->nIdxFrames * sizeof(AVIINDEXENTRY), GHND);
1435 if (This->idxFrames == NULL)
1436 return AVIERR_MEMORY;
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;
1445 /* update AVISTREAMINFO structure if necessary */
1446 if (This->sInfo.dwLength <= This->lLastFrame)
1447 This->sInfo.dwLength = This->lLastFrame + 1;
1452 static HRESULT AVIFILE_AddRecord(IAVIFileImpl *This)
1454 /* pre-conditions */
1455 assert(This != NULL && This->ppStreams[0] != NULL);
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;
1464 assert(This->nIdxRecords < This->cbIdxRecords/sizeof(AVIINDEXENTRY));
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++;
1477 static DWORD AVIFILE_ComputeMoviStart(IAVIFileImpl *This)
1482 /* RIFF,hdrl,movi,avih => (3 * 3 + 2) * sizeof(DWORD) = 11 * sizeof(DWORD) */
1483 dwPos = 11 * sizeof(DWORD) + sizeof(MainAVIHeader);
1485 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1486 IAVIStreamImpl *pStream = This->ppStreams[nStream];
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);
1497 if (This->dwMoviChunkPos == 0) {
1498 This->dwNextFramePos = dwPos;
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);
1504 This->dwMoviChunkPos = This->dwNextFramePos - sizeof(DWORD);
1510 static void AVIFILE_ConstructAVIStream(IAVIFileImpl *paf, DWORD nr, LPAVISTREAMINFOW asi)
1512 IAVIStreamImpl *pstream;
1514 /* pre-conditions */
1515 assert(paf != NULL);
1516 assert(nr < MAX_AVISTREAMS);
1517 assert(paf->ppStreams[nr] != NULL);
1519 pstream = paf->ppStreams[nr];
1521 pstream->lpVtbl = &iavist;
1524 pstream->nStream = nr;
1525 pstream->dwCurrentFrame = (DWORD)-1;
1526 pstream->lLastFrame = -1;
1529 memcpy(&pstream->sInfo, asi, sizeof(pstream->sInfo));
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;
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;
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);
1555 pstream->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1558 static void AVIFILE_DestructAVIStream(IAVIStreamImpl *This)
1560 /* pre-conditions */
1561 assert(This != NULL);
1563 This->dwCurrentFrame = (DWORD)-1;
1564 This->lLastFrame = -1;
1566 if (This->idxFrames != NULL) {
1567 GlobalFreePtr(This->idxFrames);
1568 This->idxFrames = NULL;
1569 This->nIdxFrames = 0;
1571 if (This->idxFmtChanges != NULL) {
1572 GlobalFreePtr(This->idxFmtChanges);
1573 This->idxFmtChanges = NULL;
1575 if (This->lpBuffer != NULL) {
1576 GlobalFreePtr(This->lpBuffer);
1577 This->lpBuffer = NULL;
1580 if (This->lpHandlerData != NULL) {
1581 GlobalFreePtr(This->lpHandlerData);
1582 This->lpHandlerData = NULL;
1583 This->cbHandlerData = 0;
1585 if (This->extra.lp != NULL) {
1586 GlobalFreePtr(This->extra.lp);
1587 This->extra.lp = NULL;
1590 if (This->lpFormat != NULL) {
1591 GlobalFreePtr(This->lpFormat);
1592 This->lpFormat = NULL;
1597 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1599 MainAVIHeader MainAVIHdr;
1604 IAVIStreamImpl *pStream;
1608 if (This->hmmio == NULL)
1609 return AVIERR_FILEOPEN;
1611 /* initialize stream ptr's */
1612 memset(This->ppStreams, 0, sizeof(This->ppStreams));
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;
1621 /* get "LIST" "hdrl" */
1622 ckLIST1.fccType = listtypeAVIHEADER;
1623 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF, MMIO_FINDLIST);
1627 /* get "avih" chunk */
1628 ck.ckid = ckidAVIMAINHDR;
1629 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, MMIO_FINDCHUNK);
1633 if (ck.cksize != sizeof(MainAVIHdr)) {
1634 ERR(": invalid size of %ld for MainAVIHeader!\n", ck.cksize);
1635 return AVIERR_BADFORMAT;
1637 if (mmioRead(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
1638 return AVIERR_FILEREAD;
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;
1646 /* adjust permissions if copyrighted material in file */
1647 if (MainAVIHdr.dwFlags & AVIFILEINFO_COPYRIGHTED) {
1648 This->uMode &= ~MMIO_RWMODE;
1649 This->uMode |= MMIO_READ;
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));
1667 /* go back to into header list */
1668 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1669 return AVIERR_FILEREAD;
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;
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);
1687 while (mmioDescend(This->hmmio, &ck, &ckLIST2, 0) == S_OK) {
1689 case ckidSTREAMHANDLERDATA:
1690 if (pStream->lpHandlerData != NULL)
1691 return AVIERR_BADFORMAT;
1692 pStream->lpHandlerData = GlobalAllocPtr(GMEM_DDESHARE|GMEM_MOVEABLE,
1694 if (pStream->lpHandlerData == NULL)
1695 return AVIERR_MEMORY;
1696 pStream->cbHandlerData = ck.cksize;
1698 if (mmioRead(This->hmmio, (HPSTR)pStream->lpHandlerData, ck.cksize) != ck.cksize)
1699 return AVIERR_FILEREAD;
1701 case ckidSTREAMFORMAT:
1702 if (pStream->lpFormat != NULL)
1703 return AVIERR_BADFORMAT;
1707 pStream->lpFormat = GlobalAllocPtr(GMEM_DDESHARE|GMEM_MOVEABLE,
1709 if (pStream->lpFormat == NULL)
1710 return AVIERR_MEMORY;
1711 pStream->cbFormat = ck.cksize;
1713 if (mmioRead(This->hmmio, (HPSTR)pStream->lpFormat, ck.cksize) != ck.cksize)
1714 return AVIERR_FILEREAD;
1716 if (pStream->sInfo.fccType == streamtypeVIDEO) {
1717 LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)pStream->lpFormat;
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;
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;
1734 /* init rcFrame if it's empty */
1735 if (IsRectEmpty(&pStream->sInfo.rcFrame))
1736 SetRect(&pStream->sInfo.rcFrame, 0, 0, lpbi->biWidth, lpbi->biHeight);
1739 case ckidSTREAMHEADER:
1741 static const WCHAR streamTypeFmt[] = {'%','4','.','4','h','s',0};
1743 AVIStreamHeader streamHdr;
1745 WCHAR streamNameFmt[25];
1749 if (ck.cksize > sizeof(streamHdr))
1750 n = sizeof(streamHdr);
1752 if (mmioRead(This->hmmio, (HPSTR)&streamHdr, n) != n)
1753 return AVIERR_FILEREAD;
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;
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));
1782 wsprintfW(szType, streamTypeFmt, (char*)&streamHdr.fccType);
1784 /* get count of this streamtype up to this stream */
1786 for (n = nStream; 0 <= n; n--) {
1787 if (This->ppStreams[n]->sInfo.fccHandler == streamHdr.fccType)
1791 memset(pStream->sInfo.szName, 0, sizeof(pStream->sInfo.szName));
1793 LoadStringW(AVIFILE_hModule, IDS_AVISTREAMFORMAT, streamNameFmt, sizeof(streamNameFmt));
1795 /* FIXME: avoid overflow -- better use wsnprintfW, which doesn't exists ! */
1796 wsprintfW(pStream->sInfo.szName, streamNameFmt,
1797 AVIFILE_BasenameW(This->szFileName), szType, count);
1800 case ckidSTREAMNAME:
1801 { /* streamname will be saved as ASCII string */
1802 LPSTR str = (LPSTR)LocalAlloc(LMEM_FIXED, ck.cksize);
1804 return AVIERR_MEMORY;
1806 if (mmioRead(This->hmmio, (HPSTR)str, ck.cksize) != ck.cksize)
1808 LocalFree((HLOCAL)str);
1809 return AVIERR_FILEREAD;
1812 MultiByteToWideChar(CP_ACP, 0, str, -1, pStream->sInfo.szName,
1813 sizeof(pStream->sInfo.szName)/sizeof(pStream->sInfo.szName[0]));
1815 LocalFree((HLOCAL)str);
1818 case ckidAVIPADDING:
1819 case mmioFOURCC('p','a','d','d'):
1822 WARN(": found extra chunk 0x%08lX\n", ck.ckid);
1823 hr = ReadChunkIntoExtra(&pStream->extra, This->hmmio, &ck);
1828 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1829 return AVIERR_FILEREAD;
1832 /* nested chunks in "LIST","hdrl" which are not of type "LIST","strl" */
1833 hr = ReadChunkIntoExtra(&This->fileextra, This->hmmio, &ckLIST2);
1837 if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
1838 return AVIERR_FILEREAD;
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;
1846 /* search "LIST","movi" chunk in "RIFF","AVI " */
1847 ckLIST1.fccType = listtypeAVIMOVIE;
1848 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF,
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;
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;
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;
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;
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);
1884 if (nStream > This->fInfo.dwStreams)
1885 return AVIERR_BADFORMAT;
1887 AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1888 ck.dwDataOffset - 2 * sizeof(DWORD), 0);
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);
1899 AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1900 ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1908 /* find other chunks */
1909 FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckRIFF, 0);
1914 static HRESULT AVIFILE_LoadIndex(IAVIFileImpl *This, DWORD size, DWORD offset)
1918 HRESULT hr = AVIERR_OK;
1919 BOOL bAbsolute = TRUE;
1921 lp = (AVIINDEXENTRY*)GlobalAllocPtr(GMEM_MOVEABLE,
1922 IDX_PER_BLOCK * sizeof(AVIINDEXENTRY));
1924 return AVIERR_MEMORY;
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];
1930 pStream->lLastFrame = -1;
1932 if (pStream->idxFrames != NULL) {
1933 GlobalFreePtr(pStream->idxFrames);
1934 pStream->idxFrames = NULL;
1935 pStream->nIdxFrames = 0;
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;
1946 pStream->nIdxFrames = pStream->sInfo.dwLength;
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;
1958 LONG read = min(IDX_PER_BLOCK * sizeof(AVIINDEXENTRY), size);
1960 if (mmioRead(This->hmmio, (HPSTR)lp, read) != read) {
1961 hr = AVIERR_FILEREAD;
1966 if (pos == (DWORD)-1)
1967 pos = offset - lp->dwChunkOffset + sizeof(DWORD);
1969 AVIFILE_ParseIndex(This, lp, read / sizeof(AVIINDEXENTRY),
1977 for (n = 0; n < This->fInfo.dwStreams; n++) {
1978 IAVIStreamImpl *pStream = This->ppStreams[n];
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);
1989 static HRESULT AVIFILE_ParseIndex(IAVIFileImpl *This, AVIINDEXENTRY *lp,
1990 LONG count, DWORD pos, BOOL *bAbsolute)
1993 return AVIERR_BADPARAM;
1995 for (; count > 0; count--, lp++) {
1996 WORD nStream = StreamFromFOURCC(lp->ckid);
1998 if (lp->ckid == listtypeAVIRECORD || nStream == 0x7F)
1999 continue; /* skip these */
2001 if (nStream > This->fInfo.dwStreams)
2002 return AVIERR_BADFORMAT;
2004 if (*bAbsolute && lp->dwChunkOffset < This->dwMoviChunkPos)
2008 lp->dwChunkOffset += sizeof(DWORD);
2010 lp->dwChunkOffset += pos;
2012 if (FAILED(AVIFILE_AddFrame(This->ppStreams[nStream], lp->ckid, lp->dwChunkLength, lp->dwChunkOffset, lp->dwFlags)))
2013 return AVIERR_MEMORY;
2019 static HRESULT AVIFILE_ReadBlock(IAVIStreamImpl *This, DWORD pos,
2020 LPVOID buffer, LONG size)
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);
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;
2033 /* read into out own buffer or given one? */
2034 if (buffer == NULL) {
2035 /* we also read the chunk */
2036 size += 2 * sizeof(DWORD);
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);
2042 if (This->lpBuffer == NULL)
2043 This->lpBuffer = (LPDWORD)GlobalAllocPtr(GMEM_MOVEABLE, maxSize);
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);
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;
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;
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;
2079 static void AVIFILE_SamplesToBlock(IAVIStreamImpl *This, LPLONG pos,
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);
2091 /* convert start sample to start bytes */
2092 (*offset) = (*pos) - This->sInfo.dwStart;
2093 (*offset) *= This->sInfo.dwSampleSize;
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;
2106 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
2108 MainAVIHeader MainAVIHdr;
2109 IAVIStreamImpl* pStream;
2118 /* initialize some things */
2119 if (This->dwMoviChunkPos == 0)
2120 AVIFILE_ComputeMoviStart(This);
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--;
2129 AVIFILE_UpdateInfo(This);
2131 assert(This->fInfo.dwScale != 0);
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;
2147 /* now begin writing ... */
2148 mmioSeek(This->hmmio, 0, SEEK_SET);
2152 ckRIFF.fccType = formtypeAVI;
2153 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
2154 return AVIERR_FILEWRITE;
2156 /* AVI headerlist */
2158 ckLIST1.fccType = listtypeAVIHEADER;
2159 if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2160 return AVIERR_FILEWRITE;
2163 ck.ckid = ckidAVIMAINHDR;
2164 ck.cksize = sizeof(MainAVIHdr);
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;
2173 /* write the headers of each stream into a separate streamheader list */
2174 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2175 AVIStreamHeader strHdr;
2177 pStream = This->ppStreams[nStream];
2179 /* begin the new streamheader list */
2181 ckLIST2.fccType = listtypeSTREAMHEADER;
2182 if (mmioCreateChunk(This->hmmio, &ckLIST2, MMIO_CREATELIST) != S_OK)
2183 return AVIERR_FILEWRITE;
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;
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;
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;
2223 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2224 return AVIERR_FILEWRITE;
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;
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;
2245 /* ... an optional name for this stream ... */
2246 if (lstrlenW(pStream->sInfo.szName) > 0) {
2249 ck.ckid = ckidSTREAMNAME;
2250 ck.cksize = lstrlenW(pStream->sInfo.szName) + 1;
2251 if (ck.cksize & 1) /* align */
2253 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2254 return AVIERR_FILEWRITE;
2256 /* the streamname must be saved in ASCII not Unicode */
2257 str = (LPSTR)LocalAlloc(LPTR, ck.cksize);
2259 return AVIERR_MEMORY;
2260 WideCharToMultiByte(CP_ACP, 0, pStream->sInfo.szName, -1, str,
2261 ck.cksize, NULL, NULL);
2263 if (mmioWrite(This->hmmio, (HPSTR)str, ck.cksize) != ck.cksize) {
2264 LocalFree((HLOCAL)str);
2265 return AVIERR_FILEWRITE;
2268 LocalFree((HLOCAL)str);
2269 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2270 return AVIERR_FILEWRITE;
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) */
2278 /* close the aviheader list */
2279 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2280 return AVIERR_FILEWRITE;
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);
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;
2297 /* now write the 'movi' chunk */
2298 mmioSeek(This->hmmio, This->dwMoviChunkPos - 2 * sizeof(DWORD), SEEK_SET);
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;
2308 /* write 'idx1' chunk */
2309 hr = AVIFILE_SaveIndex(This);
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;
2320 /* close RIFF chunk */
2321 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
2322 return AVIERR_FILEWRITE;
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);
2332 static HRESULT AVIFILE_SaveIndex(IAVIFileImpl *This)
2334 IAVIStreamImpl *pStream;
2340 ck.ckid = ckidAVINEWINDEX;
2342 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2343 return AVIERR_FILEWRITE;
2345 if (This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
2346 /* is interleaved -- write block of coresponding frames */
2347 LONG lInitialFrames = 0;
2351 if (This->ppStreams[0]->sInfo.dwSampleSize == 0)
2354 stepsize = AVIStreamTimeToSample((PAVISTREAM)This->ppStreams[0], 1000000);
2356 assert(stepsize > 0);
2358 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2359 if (lInitialFrames < This->ppStreams[nStream]->sInfo.dwInitialFrames)
2360 lInitialFrames = This->ppStreams[nStream]->sInfo.dwInitialFrames;
2363 for (i = -lInitialFrames; i < (LONG)This->fInfo.dwLength - lInitialFrames;
2365 DWORD nFrame = lInitialFrames + i;
2367 assert(nFrame < This->nIdxRecords);
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;
2377 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2378 pStream = This->ppStreams[nStream];
2380 /* heave we reached start of this stream? */
2381 if (-(LONG)pStream->sInfo.dwInitialFrames > i)
2384 if (pStream->sInfo.dwInitialFrames < lInitialFrames)
2385 nFrame -= (lInitialFrames - pStream->sInfo.dwInitialFrames);
2387 /* reached end of this stream? */
2388 if (pStream->lLastFrame <= nFrame)
2391 if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2392 pStream->sInfo.dwFormatChangeCount != 0 &&
2393 pStream->idxFmtChanges != NULL) {
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;
2404 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2405 return AVIERR_FILEWRITE;
2409 } /* if have formatchanges */
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;
2421 /* not interleaved -- write index for each stream at once */
2422 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2423 pStream = This->ppStreams[nStream];
2425 for (n = 0; n <= pStream->lLastFrame; n++) {
2426 if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2427 (pStream->sInfo.dwFormatChangeCount != 0)) {
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;
2436 pStream->idxFmtChanges[pos].dwChunkOffset - This->dwMoviChunkPos;
2437 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2438 return AVIERR_FILEWRITE;
2442 } /* if have formatchanges */
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;
2450 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2451 return AVIERR_FILEWRITE;
2454 } /* if not interleaved */
2456 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2457 return AVIERR_FILEWRITE;
2462 static ULONG AVIFILE_SearchStream(IAVIFileImpl *This, DWORD fcc, LONG lSkip)
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);
2476 if (This->ppStreams[i]->sInfo.fccType == fcc) {
2490 static void AVIFILE_UpdateInfo(IAVIFileImpl *This)
2494 /* pre-conditions */
2495 assert(This != NULL);
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;
2507 for (i = 0; i < This->fInfo.dwStreams; i++) {
2508 AVISTREAMINFOW *psi;
2511 /* pre-conditions */
2512 assert(This->ppStreams[i] != NULL);
2514 psi = &This->ppStreams[i]->sInfo;
2515 assert(psi->dwScale != 0);
2516 assert(psi->dwRate != 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;
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;
2530 if (This->dwInitialFrames < psi->dwInitialFrames)
2531 This->dwInitialFrames = psi->dwInitialFrames;
2533 if (This->fInfo.dwSuggestedBufferSize < psi->dwSuggestedBufferSize)
2534 This->fInfo.dwSuggestedBufferSize = psi->dwSuggestedBufferSize;
2536 if (psi->dwSampleSize != 0) {
2537 /* fixed sample size -- exact computation */
2538 This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSampleSize, psi->dwRate,
2541 /* variable sample size -- only upper limit */
2542 This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSuggestedBufferSize,
2543 psi->dwRate, psi->dwScale);
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;
2556 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
2557 FOURCC ckid, DWORD flags, LPVOID buffer,
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);
2570 if (mmioSeek(This->paf->hmmio, This->paf->dwNextFramePos, SEEK_SET) == -1)
2571 return AVIERR_FILEWRITE;
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;
2579 if (mmioAscend(This->paf->hmmio, &ck, 0) != S_OK)
2580 return AVIERR_FILEWRITE;
2582 This->paf->fDirty = TRUE;
2583 This->paf->dwNextFramePos = mmioSeek(This->paf->hmmio, 0, SEEK_CUR);
2585 return AVIFILE_AddFrame(This, ckid, size,
2586 ck.dwDataOffset - 2 * sizeof(DWORD), flags);