Changes of picasa-wine-2.2.2820-5 except to configure
[wine/hacks.git] / dlls / avifil32 / avifile.c
blobe7e5a1163b5d9a247120f4bdee38eab4536e8bdd
1 /*
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
20 /* TODO:
21 * - IAVIStreaming interface is missing for the IAVIStreamImpl
22 * - IAVIStream_fnFindSample: FIND_INDEX isn't supported.
23 * - IAVIStream_fnReadFormat: formatchanges aren't read in.
24 * - IAVIStream_fnDelete: a stub.
25 * - IAVIStream_fnSetInfo: a stub.
26 * - make thread safe
28 * KNOWN Bugs:
29 * - native version can hangup when reading a file generated with this DLL.
30 * When index is missing it works, but index seems to be okay.
33 #define COM_NO_WINDOWS_H
34 #include <assert.h>
35 #include <stdarg.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winnls.h"
42 #include "winerror.h"
43 #include "windowsx.h"
44 #include "mmsystem.h"
45 #include "vfw.h"
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);
55 #ifndef IDX_PER_BLOCK
56 #define IDX_PER_BLOCK 2730
57 #endif
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,
74 IAVIFile_fnAddRef,
75 IAVIFile_fnRelease,
76 IAVIFile_fnInfo,
77 IAVIFile_fnGetStream,
78 IAVIFile_fnCreateStream,
79 IAVIFile_fnWriteData,
80 IAVIFile_fnReadData,
81 IAVIFile_fnEndRecord,
82 IAVIFile_fnDeleteStream
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,
101 IPersistFile_fnLoad,
102 IPersistFile_fnSave,
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,
124 IAVIStream_fnAddRef,
125 IAVIStream_fnRelease,
126 IAVIStream_fnCreate,
127 IAVIStream_fnInfo,
128 IAVIStream_fnFindSample,
129 IAVIStream_fnReadFormat,
130 IAVIStream_fnSetFormat,
131 IAVIStream_fnRead,
132 IAVIStream_fnWrite,
133 IAVIStream_fnDelete,
134 IAVIStream_fnReadData,
135 IAVIStream_fnWriteData,
136 IAVIStream_fnSetInfo
139 typedef struct _IAVIFileImpl IAVIFileImpl;
141 typedef struct _IPersistFileImpl {
142 /* IUnknown stuff */
143 const IPersistFileVtbl *lpVtbl;
145 /* IPersistFile stuff */
146 IAVIFileImpl *paf;
147 } IPersistFileImpl;
149 typedef struct _IAVIStreamImpl {
150 /* IUnknown stuff */
151 const IAVIStreamVtbl *lpVtbl;
152 LONG ref;
154 /* IAVIStream stuff */
155 IAVIFileImpl *paf;
156 DWORD nStream; /* the n-th stream in file */
157 AVISTREAMINFOW sInfo;
159 LPVOID lpFormat;
160 DWORD cbFormat;
162 LPVOID lpHandlerData;
163 DWORD cbHandlerData;
165 EXTRACHUNKS extra;
167 LPDWORD lpBuffer;
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 */
176 } IAVIStreamImpl;
178 struct _IAVIFileImpl {
179 /* IUnknown stuff */
180 const IAVIFileVtbl *lpVtbl;
181 LONG ref;
183 /* IAVIFile stuff... */
184 IPersistFileImpl iPersistFile;
186 AVIFILEINFOW fInfo;
187 IAVIStreamImpl *ppStreams[MAX_AVISTREAMS];
189 EXTRACHUNKS fileextra;
191 DWORD dwMoviChunkPos; /* some stuff for saving ... */
192 DWORD dwIdxChunkPos;
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 ... */
202 HMMIO hmmio;
203 LPWSTR szFileName;
204 UINT uMode;
205 BOOL fDirty;
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,
224 LPLONG offset);
225 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
226 static HRESULT AVIFILE_SaveIndex(IAVIFileImpl *This);
227 static ULONG AVIFILE_SearchStream(IAVIFileImpl *This, DWORD fccType,
228 LONG lSkip);
229 static void AVIFILE_UpdateInfo(IAVIFileImpl *This);
230 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
231 FOURCC ckid, DWORD flags, LPVOID buffer,
232 LONG size);
234 HRESULT AVIFILE_CreateAVIFile(REFIID riid, LPVOID *ppv)
236 IAVIFileImpl *pfile;
237 HRESULT hr;
239 assert(riid != NULL && ppv != NULL);
241 *ppv = NULL;
243 pfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIFileImpl));
244 if (pfile == NULL)
245 return AVIERR_MEMORY;
247 pfile->lpVtbl = &iavift;
248 pfile->ref = 0;
249 pfile->iPersistFile.lpVtbl = &ipersistft;
250 pfile->iPersistFile.paf = pfile;
252 hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
253 if (FAILED(hr))
254 HeapFree(GetProcessHeap(), 0, pfile);
256 return hr;
259 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
260 LPVOID *obj)
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)) {
268 *obj = iface;
269 IAVIFile_AddRef(iface);
271 return S_OK;
272 } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
273 *obj = &This->iPersistFile;
274 IAVIFile_AddRef(iface);
276 return S_OK;
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);
289 return ref;
292 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
294 IAVIFileImpl *This = (IAVIFileImpl *)iface;
295 UINT i;
296 ULONG ref = InterlockedDecrement(&This->ref);
298 TRACE("(%p) -> %ld\n", iface, ref);
300 if (!ref) {
301 if (This->fDirty) {
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 HeapFree(GetProcessHeap(), 0, This->ppStreams[i]);
314 This->ppStreams[i] = NULL;
318 if (This->idxRecords != NULL) {
319 HeapFree(GetProcessHeap(), 0, This->idxRecords);
320 This->idxRecords = NULL;
321 This->nIdxRecords = 0;
324 if (This->fileextra.lp != NULL) {
325 HeapFree(GetProcessHeap(), 0, This->fileextra.lp);
326 This->fileextra.lp = NULL;
327 This->fileextra.cb = 0;
330 if (This->szFileName != NULL) {
331 HeapFree(GetProcessHeap(), 0, This->szFileName);
332 This->szFileName = NULL;
334 if (This->hmmio != NULL) {
335 mmioClose(This->hmmio, 0);
336 This->hmmio = NULL;
339 HeapFree(GetProcessHeap(), 0, This);
341 return ref;
344 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
345 LONG size)
347 IAVIFileImpl *This = (IAVIFileImpl *)iface;
349 TRACE("(%p,%p,%ld)\n",iface,afi,size);
351 if (afi == NULL)
352 return AVIERR_BADPARAM;
353 if (size < 0)
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;
362 return AVIERR_OK;
365 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
366 DWORD fccType, LONG lParam)
368 IAVIFileImpl *This = (IAVIFileImpl *)iface;
370 ULONG nStream;
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);
385 return AVIERR_OK;
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;
397 DWORD n;
399 TRACE("(%p,%p,%p)\n", iface, avis, asi);
401 /* check parameters */
402 if (avis == NULL || asi == NULL)
403 return AVIERR_BADPARAM;
405 *avis = NULL;
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] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
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++;
434 This->fDirty = TRUE;
436 /* update our AVIFILEINFO structure */
437 AVIFILE_UpdateInfo(This);
439 /* return it */
440 *avis = (PAVISTREAM)This->ppStreams[n];
441 IAVIStream_AddRef(*avis);
443 return AVIERR_OK;
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 */
454 if (lpData == NULL)
455 return AVIERR_BADPARAM;
456 if (size < 0)
457 return AVIERR_BADSIZE;
459 /* Do we have write permission? */
460 if ((This->uMode & MMIO_RWMODE) == 0)
461 return AVIERR_READONLY;
463 This->fDirty = TRUE;
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;
487 This->fDirty = TRUE;
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);
516 return AVIERR_OK;
519 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
520 LONG lParam)
522 IAVIFileImpl *This = (IAVIFileImpl *)iface;
524 ULONG nStream;
526 TRACE("(%p,0x%08lX,%ld)\n", iface, fccType, lParam);
528 /* check parameter */
529 if (lParam < 0)
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 HeapFree(GetProcessHeap(), 0, 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--;
550 This->fDirty = TRUE;
552 /* This->fInfo will be updated further when asked for */
553 return AVIERR_OK;
554 } else
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,
589 LPCLSID pClassID)
591 TRACE("(%p,%p)\n", iface, pClassID);
593 if (pClassID == NULL)
594 return AVIERR_BADPARAM;
596 memcpy(pClassID, &CLSID_AVIFile, sizeof(CLSID_AVIFile));
598 return AVIERR_OK;
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;
617 int len;
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 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 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 */
643 LPSTR szFileName;
645 len = WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1,
646 NULL, 0, NULL, NULL);
647 szFileName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(CHAR));
648 if (szFileName == NULL)
649 return AVIERR_MEMORY;
651 WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1, szFileName,
652 len, NULL, NULL);
654 This->paf->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
655 HeapFree(GetProcessHeap(), 0, 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;
665 return AVIERR_OK;
666 } else
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. */
677 return AVIERR_OK;
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. */
687 return AVIERR_OK;
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 = CoTaskMemAlloc(len * sizeof(WCHAR));
708 if (*ppszFileName == NULL)
709 return AVIERR_MEMORY;
711 strcpyW(*ppszFileName, This->paf->szFileName);
714 return AVIERR_OK;
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)) {
728 *obj = This;
729 IAVIStream_AddRef(iface);
731 return S_OK;
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);
749 return ref;
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);
762 return ref;
765 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
766 LPARAM lParam2)
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,
775 LONG size)
777 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
779 TRACE("(%p,%p,%ld)\n", iface, psi, size);
781 if (psi == NULL)
782 return AVIERR_BADPARAM;
783 if (size < 0)
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;
790 return AVIERR_OK;
793 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
794 LONG flags)
796 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
798 LONG offset = 0;
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);
805 flags |= FIND_NEXT;
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)
817 goto RETURN_FOUND;
819 if (flags & FIND_NEXT)
820 pos++;
821 else
822 pos--;
824 } else if (flags & FIND_ANY) {
825 while (0 <= pos && pos <= This->lLastFrame) {
826 if (This->idxFrames[pos].dwChunkLength > 0)
827 goto RETURN_FOUND;
829 if (flags & FIND_NEXT)
830 pos++;
831 else
832 pos--;
835 } else if ((flags & FIND_FORMAT) && This->idxFmtChanges != NULL &&
836 This->sInfo.fccType == streamtypeVIDEO) {
837 if (flags & FIND_NEXT) {
838 ULONG n;
840 for (n = 0; n < This->sInfo.dwFormatChangeCount; n++)
841 if (This->idxFmtChanges[n].ckid >= pos) {
842 pos = This->idxFmtChanges[n].ckid;
843 goto RETURN_FOUND;
845 } else {
846 LONG n;
848 for (n = (LONG)This->sInfo.dwFormatChangeCount; n >= 0; n--) {
849 if (This->idxFmtChanges[n].ckid <= pos) {
850 pos = This->idxFmtChanges[n].ckid;
851 goto RETURN_FOUND;
855 if (pos > (LONG)This->sInfo.dwStart)
856 return 0; /* format changes always for first frame */
860 return -1;
863 RETURN_FOUND:
864 if (pos < (LONG)This->sInfo.dwStart)
865 return -1;
867 switch (flags & FIND_RET) {
868 case FIND_LENGTH:
869 /* physical size */
870 pos = This->idxFrames[pos].dwChunkLength;
871 break;
872 case FIND_OFFSET:
873 /* physical position */
874 pos = This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD)
875 + offset * This->sInfo.dwSampleSize;
876 break;
877 case FIND_SIZE:
878 /* logical size */
879 if (This->sInfo.dwSampleSize)
880 pos = This->sInfo.dwSampleSize;
881 else
882 pos = 1;
883 break;
884 case FIND_INDEX:
885 FIXME(": FIND_INDEX flag is not supported!\n");
886 /* This is an index in the index-table on disc. */
887 break;
888 }; /* else logical position */
890 return pos;
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;
907 return AVIERR_OK;
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) {
920 LONG lLastFmt;
922 lLastFmt = IAVIStream_fnFindSample(iface, pos, FIND_FORMAT|FIND_PREV);
923 if (lLastFmt > 0) {
924 FIXME(": need to read formatchange for %ld -- unimplemented!\n",lLastFmt);
928 *formatsize = This->cbFormat;
929 return AVIERR_OK;
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) {
955 /* initial format */
956 if (This->paf->dwMoviChunkPos != 0)
957 return AVIERR_ERROR; /* user has used API in wrong sequnece! */
959 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, 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) {
968 LONG lDim;
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;
979 return AVIERR_OK;
980 } else {
981 MMCKINFO ck;
982 LPBITMAPINFOHEADER lpbiOld = (LPBITMAPINFOHEADER)This->lpFormat;
983 RGBQUAD *rgbNew = (RGBQUAD*)((LPBYTE)lpbiNew + lpbiNew->biSize);
984 AVIPALCHANGE *lppc = NULL;
985 UINT n;
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)
993 return AVIERR_OK;
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 = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1011 if (lppc == NULL)
1012 return AVIERR_MEMORY;
1014 lppc->bFirstEntry = 0;
1015 lppc->bNumEntries = (lpbiOld->biClrUsed < 256 ? lpbiOld->biClrUsed : 0);
1016 lppc->wFlags = 0;
1017 for (n = 0; n < lpbiOld->biClrUsed; n++) {
1018 lppc->peNew[n].peRed = rgbNew[n].rgbRed;
1019 lppc->peNew[n].peGreen = rgbNew[n].rgbGreen;
1020 lppc->peNew[n].peBlue = rgbNew[n].rgbBlue;
1021 lppc->peNew[n].peFlags = 0;
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 HeapFree(GetProcessHeap(), 0, 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,
1043 LPLONG samplesread)
1045 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1047 DWORD size;
1048 HRESULT hr;
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)
1055 *bytesread = 0;
1056 if (samplesread != NULL)
1057 *samplesread = 0;
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;
1073 else
1074 samples = 1;
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 ... */
1084 if (samples == 0)
1085 return AVIERR_OK;
1087 if (This->sInfo.dwSampleSize != 0) {
1088 /* fixed samplesize -- we can read over frame/block boundaries */
1089 LONG block = start;
1090 LONG offset = 0;
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);
1101 if (FAILED(hr))
1102 return hr;
1105 size = min((DWORD)samples, (DWORD)buffersize);
1106 size = min(size, This->cbBuffer - offset);
1107 memcpy(buffer, ((BYTE*)&This->lpBuffer[2]) + offset, size);
1109 block++;
1110 offset = 0;
1111 buffer = ((LPBYTE)buffer)+size;
1112 samples -= size;
1113 buffersize -= size;
1115 /* fill out return parameters if given */
1116 if (bytesread != NULL)
1117 *bytesread += size;
1118 if (samplesread != NULL)
1119 *samplesread += size / This->sInfo.dwSampleSize;
1122 if (samples == 0)
1123 return AVIERR_OK;
1124 else
1125 return AVIERR_BUFFERTOOSMALL;
1126 } else {
1127 /* variable samplesize -- we can only read one full frame/block */
1128 if (samples > 1)
1129 samples = 1;
1131 assert(start <= This->lLastFrame);
1132 size = This->idxFrames[start].dwChunkLength;
1133 if (buffer != NULL && buffersize >= size) {
1134 hr = AVIFILE_ReadBlock(This, start, buffer, size);
1135 if (FAILED(hr))
1136 return hr;
1137 } else if (buffer != NULL)
1138 return AVIERR_BUFFERTOOSMALL;
1140 /* fill out return parameters if given */
1141 if (bytesread != NULL)
1142 *bytesread = size;
1143 if (samplesread != NULL)
1144 *samplesread = samples;
1146 return AVIERR_OK;
1150 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
1151 LONG samples, LPVOID buffer,
1152 LONG buffersize, DWORD flags,
1153 LPLONG sampwritten,
1154 LPLONG byteswritten)
1156 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1158 FOURCC ckid;
1159 HRESULT hr;
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)
1166 *sampwritten = 0;
1167 if (byteswritten != NULL)
1168 *byteswritten = 0;
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);
1181 break;
1182 default:
1183 if ((flags & AVIIF_KEYFRAME) && buffersize != 0)
1184 ckid = MAKEAVICKID(cktypeDIBbits, This->nStream);
1185 else
1186 ckid = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1187 break;
1190 /* append to end of stream? */
1191 if (start == -1) {
1192 if (This->lLastFrame == -1)
1193 start = This->sInfo.dwStart;
1194 else
1195 start = This->sInfo.dwLength;
1196 } else if (This->lLastFrame == -1)
1197 This->sInfo.dwStart = start;
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");
1214 } else {
1215 /* variable sample size -- video like */
1216 if (samples > 1)
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);
1225 if (FAILED(hr))
1226 return hr;
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;
1241 return hr;
1244 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
1245 LONG samples)
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)
1257 return AVIERR_OK;
1259 /* Delete after end of stream? */
1260 if (start > This->sInfo.dwLength)
1261 return AVIERR_OK;
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.
1273 * } else {
1274 * 2. concat all neighboured JUNK-blocks.
1275 * 3. if the JUNK block is at the end, then set dwNextFramePos to
1276 * start of this block.
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;
1294 return AVIERR_OK;
1297 memcpy(lp, This->lpHandlerData, min(This->cbHandlerData, *lpread));
1298 if (*lpread < This->cbHandlerData)
1299 return AVIERR_BUFFERTOOSMALL;
1300 return AVIERR_OK;
1301 } else
1302 return AVIERR_NODATA;
1303 } else
1304 return ReadExtraChunk(&This->extra, fcc, lp, lpread);
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 */
1315 if (lp == NULL)
1316 return AVIERR_BADPARAM;
1317 if (size <= 0)
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
1327 * enough space */
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);
1350 return AVIERR_OK;
1351 } else
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);
1360 return E_FAIL;
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)) {
1371 case cktypeDIBbits:
1372 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1373 flags |= AVIIF_KEYFRAME;
1374 break;
1375 case cktypeDIBcompressed:
1376 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1377 flags &= ~AVIIF_KEYFRAME;
1378 break;
1379 case cktypePALchange:
1380 if (This->sInfo.fccType != streamtypeVIDEO) {
1381 ERR(": found palette change in non-video stream!\n");
1382 return AVIERR_BADFORMAT;
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 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nIdxFmtChanges * sizeof(AVIINDEXENTRY));
1394 else
1395 This->idxFmtChanges =
1396 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->idxFmtChanges,
1397 This->nIdxFmtChanges * sizeof(AVIINDEXENTRY));
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;
1406 return AVIERR_OK;
1408 break;
1409 case cktypeWAVEbytes:
1410 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1411 flags |= AVIIF_KEYFRAME;
1412 break;
1413 default:
1414 WARN(": unknown TWOCC 0x%04X found\n", TWOCCFromFOURCC(ckid));
1415 break;
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)
1429 This->idxFrames = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nIdxFrames * sizeof(AVIINDEXENTRY));
1430 else
1431 This->idxFrames = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->idxFrames,
1432 This->nIdxFrames * sizeof(AVIINDEXENTRY));
1433 if (This->idxFrames == NULL)
1434 return AVIERR_MEMORY;
1437 This->lLastFrame++;
1438 This->idxFrames[This->lLastFrame].ckid = ckid;
1439 This->idxFrames[This->lLastFrame].dwFlags = flags;
1440 This->idxFrames[This->lLastFrame].dwChunkOffset = offset;
1441 This->idxFrames[This->lLastFrame].dwChunkLength = size;
1443 /* update AVISTREAMINFO structure if necessary */
1444 if (This->sInfo.dwLength <= This->lLastFrame)
1445 This->sInfo.dwLength = This->lLastFrame + 1;
1447 return AVIERR_OK;
1450 static HRESULT AVIFILE_AddRecord(IAVIFileImpl *This)
1452 /* pre-conditions */
1453 assert(This != NULL && This->ppStreams[0] != NULL);
1455 if (This->idxRecords == NULL || This->cbIdxRecords == 0) {
1456 This->cbIdxRecords += 1024 * sizeof(AVIINDEXENTRY);
1457 This->idxRecords = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->cbIdxRecords);
1458 if (This->idxRecords == NULL)
1459 return AVIERR_MEMORY;
1462 assert(This->nIdxRecords < This->cbIdxRecords/sizeof(AVIINDEXENTRY));
1464 This->idxRecords[This->nIdxRecords].ckid = listtypeAVIRECORD;
1465 This->idxRecords[This->nIdxRecords].dwFlags = AVIIF_LIST;
1466 This->idxRecords[This->nIdxRecords].dwChunkOffset =
1467 This->ckLastRecord.dwDataOffset - 2 * sizeof(DWORD);
1468 This->idxRecords[This->nIdxRecords].dwChunkLength =
1469 This->ckLastRecord.cksize;
1470 This->nIdxRecords++;
1472 return AVIERR_OK;
1475 static DWORD AVIFILE_ComputeMoviStart(IAVIFileImpl *This)
1477 DWORD dwPos;
1478 DWORD nStream;
1480 /* RIFF,hdrl,movi,avih => (3 * 3 + 2) * sizeof(DWORD) = 11 * sizeof(DWORD) */
1481 dwPos = 11 * sizeof(DWORD) + sizeof(MainAVIHeader);
1483 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1484 IAVIStreamImpl *pStream = This->ppStreams[nStream];
1486 /* strl,strh,strf => (3 + 2 * 2) * sizeof(DWORD) = 7 * sizeof(DWORD) */
1487 dwPos += 7 * sizeof(DWORD) + sizeof(AVIStreamHeader);
1488 dwPos += ((pStream->cbFormat + 1) & ~1U);
1489 if (pStream->lpHandlerData != NULL && pStream->cbHandlerData > 0)
1490 dwPos += 2 * sizeof(DWORD) + ((pStream->cbHandlerData + 1) & ~1U);
1491 if (lstrlenW(pStream->sInfo.szName) > 0)
1492 dwPos += 2 * sizeof(DWORD) + ((lstrlenW(pStream->sInfo.szName) + 1) & ~1U);
1495 if (This->dwMoviChunkPos == 0) {
1496 This->dwNextFramePos = dwPos;
1498 /* pad to multiple of AVI_HEADERSIZE only if we are more than 8 bytes away from it */
1499 if (((dwPos + AVI_HEADERSIZE) & ~(AVI_HEADERSIZE - 1)) - dwPos > 2 * sizeof(DWORD))
1500 This->dwNextFramePos = (dwPos + AVI_HEADERSIZE) & ~(AVI_HEADERSIZE - 1);
1502 This->dwMoviChunkPos = This->dwNextFramePos - sizeof(DWORD);
1505 return dwPos;
1508 static void AVIFILE_ConstructAVIStream(IAVIFileImpl *paf, DWORD nr, LPAVISTREAMINFOW asi)
1510 IAVIStreamImpl *pstream;
1512 /* pre-conditions */
1513 assert(paf != NULL);
1514 assert(nr < MAX_AVISTREAMS);
1515 assert(paf->ppStreams[nr] != NULL);
1517 pstream = paf->ppStreams[nr];
1519 pstream->lpVtbl = &iavist;
1520 pstream->ref = 0;
1521 pstream->paf = paf;
1522 pstream->nStream = nr;
1523 pstream->dwCurrentFrame = (DWORD)-1;
1524 pstream->lLastFrame = -1;
1526 if (asi != NULL) {
1527 memcpy(&pstream->sInfo, asi, sizeof(pstream->sInfo));
1529 if (asi->dwLength > 0) {
1530 /* pre-allocate mem for frame-index structure */
1531 pstream->idxFrames =
1532 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asi->dwLength * sizeof(AVIINDEXENTRY));
1533 if (pstream->idxFrames != NULL)
1534 pstream->nIdxFrames = asi->dwLength;
1536 if (asi->dwFormatChangeCount > 0) {
1537 /* pre-allocate mem for formatchange-index structure */
1538 pstream->idxFmtChanges =
1539 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asi->dwFormatChangeCount * sizeof(AVIINDEXENTRY));
1540 if (pstream->idxFmtChanges != NULL)
1541 pstream->nIdxFmtChanges = asi->dwFormatChangeCount;
1544 /* These values will be computed */
1545 pstream->sInfo.dwLength = 0;
1546 pstream->sInfo.dwSuggestedBufferSize = 0;
1547 pstream->sInfo.dwFormatChangeCount = 0;
1548 pstream->sInfo.dwEditCount = 1;
1549 if (pstream->sInfo.dwSampleSize > 0)
1550 SetRectEmpty(&pstream->sInfo.rcFrame);
1553 pstream->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1556 static void AVIFILE_DestructAVIStream(IAVIStreamImpl *This)
1558 /* pre-conditions */
1559 assert(This != NULL);
1561 This->dwCurrentFrame = (DWORD)-1;
1562 This->lLastFrame = -1;
1563 This->paf = NULL;
1564 if (This->idxFrames != NULL) {
1565 HeapFree(GetProcessHeap(), 0, This->idxFrames);
1566 This->idxFrames = NULL;
1567 This->nIdxFrames = 0;
1569 if (This->idxFmtChanges != NULL) {
1570 HeapFree(GetProcessHeap(), 0, This->idxFmtChanges);
1571 This->idxFmtChanges = NULL;
1573 if (This->lpBuffer != NULL) {
1574 HeapFree(GetProcessHeap(), 0, This->lpBuffer);
1575 This->lpBuffer = NULL;
1576 This->cbBuffer = 0;
1578 if (This->lpHandlerData != NULL) {
1579 GlobalFreePtr(This->lpHandlerData);
1580 This->lpHandlerData = NULL;
1581 This->cbHandlerData = 0;
1583 if (This->extra.lp != NULL) {
1584 HeapFree(GetProcessHeap(), 0, This->extra.lp);
1585 This->extra.lp = NULL;
1586 This->extra.cb = 0;
1588 if (This->lpFormat != NULL) {
1589 HeapFree(GetProcessHeap(), 0, This->lpFormat);
1590 This->lpFormat = NULL;
1591 This->cbFormat = 0;
1595 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1597 MainAVIHeader MainAVIHdr;
1598 MMCKINFO ckRIFF;
1599 MMCKINFO ckLIST1;
1600 MMCKINFO ckLIST2;
1601 MMCKINFO ck;
1602 IAVIStreamImpl *pStream;
1603 DWORD nStream;
1604 HRESULT hr;
1606 if (This->hmmio == NULL)
1607 return AVIERR_FILEOPEN;
1609 /* initialize stream ptr's */
1610 memset(This->ppStreams, 0, sizeof(This->ppStreams));
1612 /* try to get "RIFF" chunk -- must not be at beginning of file! */
1613 ckRIFF.fccType = formtypeAVI;
1614 if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) {
1615 ERR(": not an AVI!\n");
1616 return AVIERR_FILEREAD;
1619 /* get "LIST" "hdrl" */
1620 ckLIST1.fccType = listtypeAVIHEADER;
1621 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF, MMIO_FINDLIST);
1622 if (FAILED(hr))
1623 return hr;
1625 /* get "avih" chunk */
1626 ck.ckid = ckidAVIMAINHDR;
1627 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, MMIO_FINDCHUNK);
1628 if (FAILED(hr))
1629 return hr;
1631 if (ck.cksize != sizeof(MainAVIHdr)) {
1632 ERR(": invalid size of %ld for MainAVIHeader!\n", ck.cksize);
1633 return AVIERR_BADFORMAT;
1635 if (mmioRead(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
1636 return AVIERR_FILEREAD;
1638 /* check for MAX_AVISTREAMS limit */
1639 if (MainAVIHdr.dwStreams > MAX_AVISTREAMS) {
1640 WARN("file contains %lu streams, but only supports %d -- change MAX_AVISTREAMS!\n", MainAVIHdr.dwStreams, MAX_AVISTREAMS);
1641 return AVIERR_UNSUPPORTED;
1644 /* adjust permissions if copyrighted material in file */
1645 if (MainAVIHdr.dwFlags & AVIFILEINFO_COPYRIGHTED) {
1646 This->uMode &= ~MMIO_RWMODE;
1647 This->uMode |= MMIO_READ;
1650 /* convert MainAVIHeader into AVIFILINFOW */
1651 memset(&This->fInfo, 0, sizeof(This->fInfo));
1652 This->fInfo.dwRate = MainAVIHdr.dwMicroSecPerFrame;
1653 This->fInfo.dwScale = 1000000;
1654 This->fInfo.dwMaxBytesPerSec = MainAVIHdr.dwMaxBytesPerSec;
1655 This->fInfo.dwFlags = MainAVIHdr.dwFlags;
1656 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1657 This->fInfo.dwLength = MainAVIHdr.dwTotalFrames;
1658 This->fInfo.dwStreams = MainAVIHdr.dwStreams;
1659 This->fInfo.dwSuggestedBufferSize = MainAVIHdr.dwSuggestedBufferSize;
1660 This->fInfo.dwWidth = MainAVIHdr.dwWidth;
1661 This->fInfo.dwHeight = MainAVIHdr.dwHeight;
1662 LoadStringW(AVIFILE_hModule, IDS_AVIFILETYPE, This->fInfo.szFileType,
1663 sizeof(This->fInfo.szFileType));
1665 /* go back to into header list */
1666 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1667 return AVIERR_FILEREAD;
1669 /* foreach stream exists a "LIST","strl" chunk */
1670 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1671 /* get next nested chunk in this "LIST","strl" */
1672 if (mmioDescend(This->hmmio, &ckLIST2, &ckLIST1, 0) != S_OK)
1673 return AVIERR_FILEREAD;
1675 /* nested chunk must be of type "LIST","strl" -- when not normally JUNK */
1676 if (ckLIST2.ckid == FOURCC_LIST &&
1677 ckLIST2.fccType == listtypeSTREAMHEADER) {
1678 pStream = This->ppStreams[nStream] =
1679 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIStreamImpl));
1680 if (pStream == NULL)
1681 return AVIERR_MEMORY;
1682 AVIFILE_ConstructAVIStream(This, nStream, NULL);
1684 ck.ckid = 0;
1685 while (mmioDescend(This->hmmio, &ck, &ckLIST2, 0) == S_OK) {
1686 switch (ck.ckid) {
1687 case ckidSTREAMHANDLERDATA:
1688 if (pStream->lpHandlerData != NULL)
1689 return AVIERR_BADFORMAT;
1690 pStream->lpHandlerData = GlobalAllocPtr(GMEM_DDESHARE|GMEM_MOVEABLE,
1691 ck.cksize);
1692 if (pStream->lpHandlerData == NULL)
1693 return AVIERR_MEMORY;
1694 pStream->cbHandlerData = ck.cksize;
1696 if (mmioRead(This->hmmio, (HPSTR)pStream->lpHandlerData, ck.cksize) != ck.cksize)
1697 return AVIERR_FILEREAD;
1698 break;
1699 case ckidSTREAMFORMAT:
1700 if (pStream->lpFormat != NULL)
1701 return AVIERR_BADFORMAT;
1702 if (ck.cksize == 0)
1703 break;
1705 pStream->lpFormat = GlobalAllocPtr(GMEM_DDESHARE|GMEM_MOVEABLE,
1706 ck.cksize);
1707 if (pStream->lpFormat == NULL)
1708 return AVIERR_MEMORY;
1709 pStream->cbFormat = ck.cksize;
1711 if (mmioRead(This->hmmio, (HPSTR)pStream->lpFormat, ck.cksize) != ck.cksize)
1712 return AVIERR_FILEREAD;
1714 if (pStream->sInfo.fccType == streamtypeVIDEO) {
1715 LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)pStream->lpFormat;
1717 /* some corrections to the video format */
1718 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
1719 lpbi->biClrUsed = 1u << lpbi->biBitCount;
1720 if (lpbi->biCompression == BI_RGB && lpbi->biSizeImage == 0)
1721 lpbi->biSizeImage = DIBWIDTHBYTES(*lpbi) * lpbi->biHeight;
1722 if (lpbi->biCompression != BI_RGB && lpbi->biBitCount == 8) {
1723 if (pStream->sInfo.fccHandler == mmioFOURCC('R','L','E','0') ||
1724 pStream->sInfo.fccHandler == mmioFOURCC('R','L','E',' '))
1725 lpbi->biCompression = BI_RLE8;
1727 if (lpbi->biCompression == BI_RGB &&
1728 (pStream->sInfo.fccHandler == 0 ||
1729 pStream->sInfo.fccHandler == mmioFOURCC('N','O','N','E')))
1730 pStream->sInfo.fccHandler = comptypeDIB;
1732 /* init rcFrame if it's empty */
1733 if (IsRectEmpty(&pStream->sInfo.rcFrame))
1734 SetRect(&pStream->sInfo.rcFrame, 0, 0, lpbi->biWidth, lpbi->biHeight);
1736 break;
1737 case ckidSTREAMHEADER:
1739 static const WCHAR streamTypeFmt[] = {'%','4','.','4','h','s',0};
1741 AVIStreamHeader streamHdr;
1742 WCHAR szType[25];
1743 WCHAR streamNameFmt[25];
1744 UINT count;
1745 LONG n = ck.cksize;
1747 if (ck.cksize > sizeof(streamHdr))
1748 n = sizeof(streamHdr);
1750 if (mmioRead(This->hmmio, (HPSTR)&streamHdr, n) != n)
1751 return AVIERR_FILEREAD;
1753 pStream->sInfo.fccType = streamHdr.fccType;
1754 pStream->sInfo.fccHandler = streamHdr.fccHandler;
1755 pStream->sInfo.dwFlags = streamHdr.dwFlags;
1756 pStream->sInfo.wPriority = streamHdr.wPriority;
1757 pStream->sInfo.wLanguage = streamHdr.wLanguage;
1758 pStream->sInfo.dwInitialFrames = streamHdr.dwInitialFrames;
1759 pStream->sInfo.dwScale = streamHdr.dwScale;
1760 pStream->sInfo.dwRate = streamHdr.dwRate;
1761 pStream->sInfo.dwStart = streamHdr.dwStart;
1762 pStream->sInfo.dwLength = streamHdr.dwLength;
1763 pStream->sInfo.dwSuggestedBufferSize =
1764 streamHdr.dwSuggestedBufferSize;
1765 pStream->sInfo.dwQuality = streamHdr.dwQuality;
1766 pStream->sInfo.dwSampleSize = streamHdr.dwSampleSize;
1767 pStream->sInfo.rcFrame.left = streamHdr.rcFrame.left;
1768 pStream->sInfo.rcFrame.top = streamHdr.rcFrame.top;
1769 pStream->sInfo.rcFrame.right = streamHdr.rcFrame.right;
1770 pStream->sInfo.rcFrame.bottom = streamHdr.rcFrame.bottom;
1771 pStream->sInfo.dwEditCount = 0;
1772 pStream->sInfo.dwFormatChangeCount = 0;
1774 /* generate description for stream like "filename.avi Type #n" */
1775 if (streamHdr.fccType == streamtypeVIDEO)
1776 LoadStringW(AVIFILE_hModule, IDS_VIDEO, szType, sizeof(szType));
1777 else if (streamHdr.fccType == streamtypeAUDIO)
1778 LoadStringW(AVIFILE_hModule, IDS_AUDIO, szType, sizeof(szType));
1779 else
1780 wsprintfW(szType, streamTypeFmt, (char*)&streamHdr.fccType);
1782 /* get count of this streamtype up to this stream */
1783 count = 0;
1784 for (n = nStream; 0 <= n; n--) {
1785 if (This->ppStreams[n]->sInfo.fccHandler == streamHdr.fccType)
1786 count++;
1789 memset(pStream->sInfo.szName, 0, sizeof(pStream->sInfo.szName));
1791 LoadStringW(AVIFILE_hModule, IDS_AVISTREAMFORMAT, streamNameFmt, sizeof(streamNameFmt));
1793 /* FIXME: avoid overflow -- better use wsnprintfW, which doesn't exists ! */
1794 wsprintfW(pStream->sInfo.szName, streamNameFmt,
1795 AVIFILE_BasenameW(This->szFileName), szType, count);
1797 break;
1798 case ckidSTREAMNAME:
1799 { /* streamname will be saved as ASCII string */
1800 LPSTR str = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1801 if (str == NULL)
1802 return AVIERR_MEMORY;
1804 if (mmioRead(This->hmmio, (HPSTR)str, ck.cksize) != ck.cksize)
1806 HeapFree(GetProcessHeap(), 0, str);
1807 return AVIERR_FILEREAD;
1810 MultiByteToWideChar(CP_ACP, 0, str, -1, pStream->sInfo.szName,
1811 sizeof(pStream->sInfo.szName)/sizeof(pStream->sInfo.szName[0]));
1813 HeapFree(GetProcessHeap(), 0, str);
1815 break;
1816 case ckidAVIPADDING:
1817 case mmioFOURCC('p','a','d','d'):
1818 break;
1819 default:
1820 WARN(": found extra chunk 0x%08lX\n", ck.ckid);
1821 hr = ReadChunkIntoExtra(&pStream->extra, This->hmmio, &ck);
1822 if (FAILED(hr))
1823 return hr;
1826 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1827 return AVIERR_FILEREAD;
1829 } else {
1830 /* nested chunks in "LIST","hdrl" which are not of type "LIST","strl" */
1831 hr = ReadChunkIntoExtra(&This->fileextra, This->hmmio, &ckLIST2);
1832 if (FAILED(hr))
1833 return hr;
1835 if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
1836 return AVIERR_FILEREAD;
1839 /* read any extra headers in "LIST","hdrl" */
1840 FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, 0);
1841 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
1842 return AVIERR_FILEREAD;
1844 /* search "LIST","movi" chunk in "RIFF","AVI " */
1845 ckLIST1.fccType = listtypeAVIMOVIE;
1846 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF,
1847 MMIO_FINDLIST);
1848 if (FAILED(hr))
1849 return hr;
1851 This->dwMoviChunkPos = ckLIST1.dwDataOffset;
1852 This->dwIdxChunkPos = ckLIST1.cksize + ckLIST1.dwDataOffset;
1853 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
1854 return AVIERR_FILEREAD;
1856 /* try to find an index */
1857 ck.ckid = ckidAVINEWINDEX;
1858 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio,
1859 &ck, &ckRIFF, MMIO_FINDCHUNK);
1860 if (SUCCEEDED(hr) && ck.cksize > 0) {
1861 if (FAILED(AVIFILE_LoadIndex(This, ck.cksize, ckLIST1.dwDataOffset)))
1862 This->fInfo.dwFlags &= ~AVIFILEINFO_HASINDEX;
1865 /* when we haven't found an index or it's bad, then build one
1866 * by parsing 'movi' chunk */
1867 if ((This->fInfo.dwFlags & AVIFILEINFO_HASINDEX) == 0) {
1868 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++)
1869 This->ppStreams[nStream]->lLastFrame = -1;
1871 if (mmioSeek(This->hmmio, ckLIST1.dwDataOffset + sizeof(DWORD), SEEK_SET) == -1) {
1872 ERR(": Oops, can't seek back to 'movi' chunk!\n");
1873 return AVIERR_FILEREAD;
1876 /* seek through the 'movi' list until end */
1877 while (mmioDescend(This->hmmio, &ck, &ckLIST1, 0) == S_OK) {
1878 if (ck.ckid != FOURCC_LIST) {
1879 if (mmioAscend(This->hmmio, &ck, 0) == S_OK) {
1880 nStream = StreamFromFOURCC(ck.ckid);
1882 if (nStream > This->fInfo.dwStreams)
1883 return AVIERR_BADFORMAT;
1885 AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1886 ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1887 } else {
1888 nStream = StreamFromFOURCC(ck.ckid);
1889 WARN(": file seems to be truncated!\n");
1890 if (nStream <= This->fInfo.dwStreams &&
1891 This->ppStreams[nStream]->sInfo.dwSampleSize > 0) {
1892 ck.cksize = mmioSeek(This->hmmio, 0, SEEK_END);
1893 if (ck.cksize != -1) {
1894 ck.cksize -= ck.dwDataOffset;
1895 ck.cksize &= ~(This->ppStreams[nStream]->sInfo.dwSampleSize - 1);
1897 AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1898 ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1906 /* find other chunks */
1907 FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckRIFF, 0);
1909 return AVIERR_OK;
1912 static HRESULT AVIFILE_LoadIndex(IAVIFileImpl *This, DWORD size, DWORD offset)
1914 AVIINDEXENTRY *lp;
1915 DWORD pos, n;
1916 HRESULT hr = AVIERR_OK;
1917 BOOL bAbsolute = TRUE;
1919 lp = HeapAlloc(GetProcessHeap(), 0, IDX_PER_BLOCK * sizeof(AVIINDEXENTRY));
1920 if (lp == NULL)
1921 return AVIERR_MEMORY;
1923 /* adjust limits for index tables, so that inserting will be faster */
1924 for (n = 0; n < This->fInfo.dwStreams; n++) {
1925 IAVIStreamImpl *pStream = This->ppStreams[n];
1927 pStream->lLastFrame = -1;
1929 if (pStream->idxFrames != NULL) {
1930 HeapFree(GetProcessHeap(), 0, pStream->idxFrames);
1931 pStream->idxFrames = NULL;
1932 pStream->nIdxFrames = 0;
1935 if (pStream->sInfo.dwSampleSize != 0) {
1936 if (n > 0 && This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
1937 pStream->nIdxFrames = This->ppStreams[0]->nIdxFrames;
1938 } else if (pStream->sInfo.dwSuggestedBufferSize) {
1939 pStream->nIdxFrames =
1940 pStream->sInfo.dwLength / pStream->sInfo.dwSuggestedBufferSize;
1942 } else
1943 pStream->nIdxFrames = pStream->sInfo.dwLength;
1945 pStream->idxFrames =
1946 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pStream->nIdxFrames * sizeof(AVIINDEXENTRY));
1947 if (pStream->idxFrames == NULL && pStream->nIdxFrames > 0) {
1948 pStream->nIdxFrames = 0;
1949 return AVIERR_MEMORY;
1953 pos = (DWORD)-1;
1954 while (size != 0) {
1955 LONG read = min(IDX_PER_BLOCK * sizeof(AVIINDEXENTRY), size);
1957 if (mmioRead(This->hmmio, (HPSTR)lp, read) != read) {
1958 hr = AVIERR_FILEREAD;
1959 break;
1961 size -= read;
1963 if (pos == (DWORD)-1)
1964 pos = offset - lp->dwChunkOffset + sizeof(DWORD);
1966 AVIFILE_ParseIndex(This, lp, read / sizeof(AVIINDEXENTRY),
1967 pos, &bAbsolute);
1970 if (lp != NULL)
1971 HeapFree(GetProcessHeap(), 0, lp);
1973 /* checking ... */
1974 for (n = 0; n < This->fInfo.dwStreams; n++) {
1975 IAVIStreamImpl *pStream = This->ppStreams[n];
1977 if (pStream->sInfo.dwSampleSize == 0 &&
1978 pStream->sInfo.dwLength != pStream->lLastFrame+1)
1979 ERR("stream %lu length mismatch: dwLength=%lu found=%ld\n",
1980 n, pStream->sInfo.dwLength, pStream->lLastFrame);
1983 return hr;
1986 static HRESULT AVIFILE_ParseIndex(IAVIFileImpl *This, AVIINDEXENTRY *lp,
1987 LONG count, DWORD pos, BOOL *bAbsolute)
1989 if (lp == NULL)
1990 return AVIERR_BADPARAM;
1992 for (; count > 0; count--, lp++) {
1993 WORD nStream = StreamFromFOURCC(lp->ckid);
1995 if (lp->ckid == listtypeAVIRECORD || nStream == 0x7F)
1996 continue; /* skip these */
1998 if (nStream > This->fInfo.dwStreams)
1999 return AVIERR_BADFORMAT;
2001 if (*bAbsolute && lp->dwChunkOffset < This->dwMoviChunkPos)
2002 *bAbsolute = FALSE;
2004 if (*bAbsolute)
2005 lp->dwChunkOffset += sizeof(DWORD);
2006 else
2007 lp->dwChunkOffset += pos;
2009 if (FAILED(AVIFILE_AddFrame(This->ppStreams[nStream], lp->ckid, lp->dwChunkLength, lp->dwChunkOffset, lp->dwFlags)))
2010 return AVIERR_MEMORY;
2013 return AVIERR_OK;
2016 static HRESULT AVIFILE_ReadBlock(IAVIStreamImpl *This, DWORD pos,
2017 LPVOID buffer, LONG size)
2019 /* pre-conditions */
2020 assert(This != NULL);
2021 assert(This->paf != NULL);
2022 assert(This->paf->hmmio != NULL);
2023 assert(This->sInfo.dwStart <= pos && pos < This->sInfo.dwLength);
2024 assert(pos <= This->lLastFrame);
2026 /* should we read as much as block gives us? */
2027 if (size == 0 || size > This->idxFrames[pos].dwChunkLength)
2028 size = This->idxFrames[pos].dwChunkLength;
2030 /* read into out own buffer or given one? */
2031 if (buffer == NULL) {
2032 /* we also read the chunk */
2033 size += 2 * sizeof(DWORD);
2035 /* check that buffer is big enough -- don't trust dwSuggestedBufferSize */
2036 if (This->lpBuffer == NULL || size < This->cbBuffer) {
2037 DWORD maxSize = max(size, This->sInfo.dwSuggestedBufferSize);
2039 if (This->lpBuffer == NULL)
2040 This->lpBuffer = HeapAlloc(GetProcessHeap(), 0, maxSize);
2041 else
2042 This->lpBuffer = HeapReAlloc(GetProcessHeap(), 0, This->lpBuffer, maxSize);
2043 if (This->lpBuffer == NULL)
2044 return AVIERR_MEMORY;
2045 This->cbBuffer = max(size, This->sInfo.dwSuggestedBufferSize);
2048 /* now read the complete chunk into our buffer */
2049 if (mmioSeek(This->paf->hmmio, This->idxFrames[pos].dwChunkOffset, SEEK_SET) == -1)
2050 return AVIERR_FILEREAD;
2051 if (mmioRead(This->paf->hmmio, (HPSTR)This->lpBuffer, size) != size)
2052 return AVIERR_FILEREAD;
2054 /* check if it was the correct block which we have read */
2055 if (This->lpBuffer[0] != This->idxFrames[pos].ckid ||
2056 This->lpBuffer[1] != This->idxFrames[pos].dwChunkLength) {
2057 ERR(": block %ld not found at 0x%08lX\n", pos, This->idxFrames[pos].dwChunkOffset);
2058 ERR(": Index says: '%4.4s'(0x%08lX) size 0x%08lX\n",
2059 (char*)&This->idxFrames[pos].ckid, This->idxFrames[pos].ckid,
2060 This->idxFrames[pos].dwChunkLength);
2061 ERR(": Data says: '%4.4s'(0x%08lX) size 0x%08lX\n",
2062 (char*)&This->lpBuffer[0], This->lpBuffer[0], This->lpBuffer[1]);
2063 return AVIERR_FILEREAD;
2065 } else {
2066 if (mmioSeek(This->paf->hmmio, This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD), SEEK_SET) == -1)
2067 return AVIERR_FILEREAD;
2068 if (mmioRead(This->paf->hmmio, (HPSTR)buffer, size) != size)
2069 return AVIERR_FILEREAD;
2072 return AVIERR_OK;
2075 static void AVIFILE_SamplesToBlock(IAVIStreamImpl *This, LPLONG pos,
2076 LPLONG offset)
2078 LONG block;
2080 /* pre-conditions */
2081 assert(This != NULL);
2082 assert(pos != NULL);
2083 assert(offset != NULL);
2084 assert(This->sInfo.dwSampleSize != 0);
2085 assert(*pos >= This->sInfo.dwStart);
2087 /* convert start sample to start bytes */
2088 (*offset) = (*pos) - This->sInfo.dwStart;
2089 (*offset) *= This->sInfo.dwSampleSize;
2091 /* convert bytes to block number */
2092 for (block = 0; block <= This->lLastFrame; block++) {
2093 if (This->idxFrames[block].dwChunkLength <= *offset)
2094 (*offset) -= This->idxFrames[block].dwChunkLength;
2095 else
2096 break;
2099 *pos = block;
2102 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
2104 MainAVIHeader MainAVIHdr;
2105 IAVIStreamImpl* pStream;
2106 MMCKINFO ckRIFF;
2107 MMCKINFO ckLIST1;
2108 MMCKINFO ckLIST2;
2109 MMCKINFO ck;
2110 DWORD nStream;
2111 DWORD dwPos;
2112 HRESULT hr;
2114 /* initialize some things */
2115 if (This->dwMoviChunkPos == 0)
2116 AVIFILE_ComputeMoviStart(This);
2118 /* written one record to much? */
2119 if (This->ckLastRecord.dwFlags & MMIO_DIRTY) {
2120 This->dwNextFramePos -= 3 * sizeof(DWORD);
2121 if (This->nIdxRecords > 0)
2122 This->nIdxRecords--;
2125 AVIFILE_UpdateInfo(This);
2127 assert(This->fInfo.dwScale != 0);
2129 memset(&MainAVIHdr, 0, sizeof(MainAVIHdr));
2130 MainAVIHdr.dwMicroSecPerFrame = MulDiv(This->fInfo.dwRate, 1000000,
2131 This->fInfo.dwScale);
2132 MainAVIHdr.dwMaxBytesPerSec = This->fInfo.dwMaxBytesPerSec;
2133 MainAVIHdr.dwPaddingGranularity = AVI_HEADERSIZE;
2134 MainAVIHdr.dwFlags = This->fInfo.dwFlags;
2135 MainAVIHdr.dwTotalFrames = This->fInfo.dwLength;
2136 MainAVIHdr.dwInitialFrames = 0;
2137 MainAVIHdr.dwStreams = This->fInfo.dwStreams;
2138 MainAVIHdr.dwSuggestedBufferSize = This->fInfo.dwSuggestedBufferSize;
2139 MainAVIHdr.dwWidth = This->fInfo.dwWidth;
2140 MainAVIHdr.dwHeight = This->fInfo.dwHeight;
2141 MainAVIHdr.dwInitialFrames = This->dwInitialFrames;
2143 /* now begin writing ... */
2144 mmioSeek(This->hmmio, 0, SEEK_SET);
2146 /* RIFF chunk */
2147 ckRIFF.cksize = 0;
2148 ckRIFF.fccType = formtypeAVI;
2149 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
2150 return AVIERR_FILEWRITE;
2152 /* AVI headerlist */
2153 ckLIST1.cksize = 0;
2154 ckLIST1.fccType = listtypeAVIHEADER;
2155 if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2156 return AVIERR_FILEWRITE;
2158 /* MainAVIHeader */
2159 ck.ckid = ckidAVIMAINHDR;
2160 ck.cksize = sizeof(MainAVIHdr);
2161 ck.fccType = 0;
2162 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2163 return AVIERR_FILEWRITE;
2164 if (mmioWrite(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
2165 return AVIERR_FILEWRITE;
2166 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2167 return AVIERR_FILEWRITE;
2169 /* write the headers of each stream into a separate streamheader list */
2170 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2171 AVIStreamHeader strHdr;
2173 pStream = This->ppStreams[nStream];
2175 /* begin the new streamheader list */
2176 ckLIST2.cksize = 0;
2177 ckLIST2.fccType = listtypeSTREAMHEADER;
2178 if (mmioCreateChunk(This->hmmio, &ckLIST2, MMIO_CREATELIST) != S_OK)
2179 return AVIERR_FILEWRITE;
2181 /* create an AVIStreamHeader from the AVSTREAMINFO */
2182 strHdr.fccType = pStream->sInfo.fccType;
2183 strHdr.fccHandler = pStream->sInfo.fccHandler;
2184 strHdr.dwFlags = pStream->sInfo.dwFlags;
2185 strHdr.wPriority = pStream->sInfo.wPriority;
2186 strHdr.wLanguage = pStream->sInfo.wLanguage;
2187 strHdr.dwInitialFrames = pStream->sInfo.dwInitialFrames;
2188 strHdr.dwScale = pStream->sInfo.dwScale;
2189 strHdr.dwRate = pStream->sInfo.dwRate;
2190 strHdr.dwStart = pStream->sInfo.dwStart;
2191 strHdr.dwLength = pStream->sInfo.dwLength;
2192 strHdr.dwSuggestedBufferSize = pStream->sInfo.dwSuggestedBufferSize;
2193 strHdr.dwQuality = pStream->sInfo.dwQuality;
2194 strHdr.dwSampleSize = pStream->sInfo.dwSampleSize;
2195 strHdr.rcFrame.left = pStream->sInfo.rcFrame.left;
2196 strHdr.rcFrame.top = pStream->sInfo.rcFrame.top;
2197 strHdr.rcFrame.right = pStream->sInfo.rcFrame.right;
2198 strHdr.rcFrame.bottom = pStream->sInfo.rcFrame.bottom;
2200 /* now write the AVIStreamHeader */
2201 ck.ckid = ckidSTREAMHEADER;
2202 ck.cksize = sizeof(strHdr);
2203 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2204 return AVIERR_FILEWRITE;
2205 if (mmioWrite(This->hmmio, (HPSTR)&strHdr, ck.cksize) != ck.cksize)
2206 return AVIERR_FILEWRITE;
2207 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2208 return AVIERR_FILEWRITE;
2210 /* ... the hopefully ever present streamformat ... */
2211 ck.ckid = ckidSTREAMFORMAT;
2212 ck.cksize = pStream->cbFormat;
2213 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2214 return AVIERR_FILEWRITE;
2215 if (pStream->lpFormat != NULL && ck.cksize > 0) {
2216 if (mmioWrite(This->hmmio, (HPSTR)pStream->lpFormat, ck.cksize) != ck.cksize)
2217 return AVIERR_FILEWRITE;
2219 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2220 return AVIERR_FILEWRITE;
2222 /* ... some optional existing handler data ... */
2223 if (pStream->lpHandlerData != NULL && pStream->cbHandlerData > 0) {
2224 ck.ckid = ckidSTREAMHANDLERDATA;
2225 ck.cksize = pStream->cbHandlerData;
2226 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2227 return AVIERR_FILEWRITE;
2228 if (mmioWrite(This->hmmio, (HPSTR)pStream->lpHandlerData, ck.cksize) != ck.cksize)
2229 return AVIERR_FILEWRITE;
2230 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2231 return AVIERR_FILEWRITE;
2234 /* ... some optional additional extra chunk for this stream ... */
2235 if (pStream->extra.lp != NULL && pStream->extra.cb > 0) {
2236 /* the chunk header(s) are already in the strucuture */
2237 if (mmioWrite(This->hmmio, (HPSTR)pStream->extra.lp, pStream->extra.cb) != pStream->extra.cb)
2238 return AVIERR_FILEWRITE;
2241 /* ... an optional name for this stream ... */
2242 if (lstrlenW(pStream->sInfo.szName) > 0) {
2243 LPSTR str;
2245 ck.ckid = ckidSTREAMNAME;
2246 ck.cksize = lstrlenW(pStream->sInfo.szName) + 1;
2247 if (ck.cksize & 1) /* align */
2248 ck.cksize++;
2249 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2250 return AVIERR_FILEWRITE;
2252 /* the streamname must be saved in ASCII not Unicode */
2253 str = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ck.cksize);
2254 if (str == NULL)
2255 return AVIERR_MEMORY;
2256 WideCharToMultiByte(CP_ACP, 0, pStream->sInfo.szName, -1, str,
2257 ck.cksize, NULL, NULL);
2259 if (mmioWrite(This->hmmio, (HPSTR)str, ck.cksize) != ck.cksize) {
2260 HeapFree(GetProcessHeap(), 0, str);
2261 return AVIERR_FILEWRITE;
2264 HeapFree(GetProcessHeap(), 0, str);
2265 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2266 return AVIERR_FILEWRITE;
2269 /* close streamheader list for this stream */
2270 if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
2271 return AVIERR_FILEWRITE;
2272 } /* for (0 <= nStream < MainAVIHdr.dwStreams) */
2274 /* close the aviheader list */
2275 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2276 return AVIERR_FILEWRITE;
2278 /* check for padding to pre-guessed 'movi'-chunk position */
2279 dwPos = ckLIST1.dwDataOffset + ckLIST1.cksize;
2280 if (This->dwMoviChunkPos - 2 * sizeof(DWORD) > dwPos) {
2281 ck.ckid = ckidAVIPADDING;
2282 ck.cksize = This->dwMoviChunkPos - dwPos - 4 * sizeof(DWORD);
2283 assert((LONG)ck.cksize >= 0);
2285 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2286 return AVIERR_FILEWRITE;
2287 if (mmioSeek(This->hmmio, ck.cksize, SEEK_CUR) == -1)
2288 return AVIERR_FILEWRITE;
2289 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2290 return AVIERR_FILEWRITE;
2293 /* now write the 'movi' chunk */
2294 mmioSeek(This->hmmio, This->dwMoviChunkPos - 2 * sizeof(DWORD), SEEK_SET);
2295 ckLIST1.cksize = 0;
2296 ckLIST1.fccType = listtypeAVIMOVIE;
2297 if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2298 return AVIERR_FILEWRITE;
2299 if (mmioSeek(This->hmmio, This->dwNextFramePos, SEEK_SET) == -1)
2300 return AVIERR_FILEWRITE;
2301 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2302 return AVIERR_FILEWRITE;
2304 /* write 'idx1' chunk */
2305 hr = AVIFILE_SaveIndex(This);
2306 if (FAILED(hr))
2307 return hr;
2309 /* write optional extra file chunks */
2310 if (This->fileextra.lp != NULL && This->fileextra.cb > 0) {
2311 /* as for the streams, are the chunk header(s) in the structure */
2312 if (mmioWrite(This->hmmio, (HPSTR)This->fileextra.lp, This->fileextra.cb) != This->fileextra.cb)
2313 return AVIERR_FILEWRITE;
2316 /* close RIFF chunk */
2317 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
2318 return AVIERR_FILEWRITE;
2320 /* add some JUNK at end for bad parsers */
2321 memset(&ckRIFF, 0, sizeof(ckRIFF));
2322 mmioWrite(This->hmmio, (HPSTR)&ckRIFF, sizeof(ckRIFF));
2323 mmioFlush(This->hmmio, 0);
2325 return AVIERR_OK;
2328 static HRESULT AVIFILE_SaveIndex(IAVIFileImpl *This)
2330 IAVIStreamImpl *pStream;
2331 AVIINDEXENTRY idx;
2332 MMCKINFO ck;
2333 DWORD nStream;
2334 LONG n;
2336 ck.ckid = ckidAVINEWINDEX;
2337 ck.cksize = 0;
2338 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2339 return AVIERR_FILEWRITE;
2341 if (This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
2342 /* is interleaved -- write block of coresponding frames */
2343 LONG lInitialFrames = 0;
2344 LONG stepsize;
2345 LONG i;
2347 if (This->ppStreams[0]->sInfo.dwSampleSize == 0)
2348 stepsize = 1;
2349 else
2350 stepsize = AVIStreamTimeToSample((PAVISTREAM)This->ppStreams[0], 1000000);
2352 assert(stepsize > 0);
2354 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2355 if (lInitialFrames < This->ppStreams[nStream]->sInfo.dwInitialFrames)
2356 lInitialFrames = This->ppStreams[nStream]->sInfo.dwInitialFrames;
2359 for (i = -lInitialFrames; i < (LONG)This->fInfo.dwLength - lInitialFrames;
2360 i += stepsize) {
2361 DWORD nFrame = lInitialFrames + i;
2363 assert(nFrame < This->nIdxRecords);
2365 idx.ckid = listtypeAVIRECORD;
2366 idx.dwFlags = AVIIF_LIST;
2367 idx.dwChunkLength = This->idxRecords[nFrame].dwChunkLength;
2368 idx.dwChunkOffset = This->idxRecords[nFrame].dwChunkOffset
2369 - This->dwMoviChunkPos;
2370 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2371 return AVIERR_FILEWRITE;
2373 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2374 pStream = This->ppStreams[nStream];
2376 /* heave we reached start of this stream? */
2377 if (-(LONG)pStream->sInfo.dwInitialFrames > i)
2378 continue;
2380 if (pStream->sInfo.dwInitialFrames < lInitialFrames)
2381 nFrame -= (lInitialFrames - pStream->sInfo.dwInitialFrames);
2383 /* reached end of this stream? */
2384 if (pStream->lLastFrame <= nFrame)
2385 continue;
2387 if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2388 pStream->sInfo.dwFormatChangeCount != 0 &&
2389 pStream->idxFmtChanges != NULL) {
2390 DWORD pos;
2392 for (pos = 0; pos < pStream->sInfo.dwFormatChangeCount; pos++) {
2393 if (pStream->idxFmtChanges[pos].ckid == nFrame) {
2394 idx.dwFlags = AVIIF_NOTIME;
2395 idx.ckid = MAKEAVICKID(cktypePALchange, pStream->nStream);
2396 idx.dwChunkLength = pStream->idxFmtChanges[pos].dwChunkLength;
2397 idx.dwChunkOffset = pStream->idxFmtChanges[pos].dwChunkOffset
2398 - This->dwMoviChunkPos;
2400 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2401 return AVIERR_FILEWRITE;
2402 break;
2405 } /* if have formatchanges */
2407 idx.ckid = pStream->idxFrames[nFrame].ckid;
2408 idx.dwFlags = pStream->idxFrames[nFrame].dwFlags;
2409 idx.dwChunkLength = pStream->idxFrames[nFrame].dwChunkLength;
2410 idx.dwChunkOffset = pStream->idxFrames[nFrame].dwChunkOffset
2411 - This->dwMoviChunkPos;
2412 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2413 return AVIERR_FILEWRITE;
2416 } else {
2417 /* not interleaved -- write index for each stream at once */
2418 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2419 pStream = This->ppStreams[nStream];
2421 for (n = 0; n <= pStream->lLastFrame; n++) {
2422 if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2423 (pStream->sInfo.dwFormatChangeCount != 0)) {
2424 DWORD pos;
2426 for (pos = 0; pos < pStream->sInfo.dwFormatChangeCount; pos++) {
2427 if (pStream->idxFmtChanges[pos].ckid == n) {
2428 idx.dwFlags = AVIIF_NOTIME;
2429 idx.ckid = MAKEAVICKID(cktypePALchange, pStream->nStream);
2430 idx.dwChunkLength = pStream->idxFmtChanges[pos].dwChunkLength;
2431 idx.dwChunkOffset =
2432 pStream->idxFmtChanges[pos].dwChunkOffset - This->dwMoviChunkPos;
2433 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2434 return AVIERR_FILEWRITE;
2435 break;
2438 } /* if have formatchanges */
2440 idx.ckid = pStream->idxFrames[n].ckid;
2441 idx.dwFlags = pStream->idxFrames[n].dwFlags;
2442 idx.dwChunkLength = pStream->idxFrames[n].dwChunkLength;
2443 idx.dwChunkOffset = pStream->idxFrames[n].dwChunkOffset
2444 - This->dwMoviChunkPos;
2446 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2447 return AVIERR_FILEWRITE;
2450 } /* if not interleaved */
2452 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2453 return AVIERR_FILEWRITE;
2455 return AVIERR_OK;
2458 static ULONG AVIFILE_SearchStream(IAVIFileImpl *This, DWORD fcc, LONG lSkip)
2460 UINT i;
2461 UINT nStream;
2463 /* pre-condition */
2464 assert(lSkip >= 0);
2466 if (fcc != 0) {
2467 /* search the number of the specified stream */
2468 nStream = (ULONG)-1;
2469 for (i = 0; i < This->fInfo.dwStreams; i++) {
2470 assert(This->ppStreams[i] != NULL);
2472 if (This->ppStreams[i]->sInfo.fccType == fcc) {
2473 if (lSkip == 0) {
2474 nStream = i;
2475 break;
2476 } else
2477 lSkip--;
2480 } else
2481 nStream = lSkip;
2483 return nStream;
2486 static void AVIFILE_UpdateInfo(IAVIFileImpl *This)
2488 UINT i;
2490 /* pre-conditions */
2491 assert(This != NULL);
2493 This->fInfo.dwMaxBytesPerSec = 0;
2494 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
2495 This->fInfo.dwSuggestedBufferSize = 0;
2496 This->fInfo.dwWidth = 0;
2497 This->fInfo.dwHeight = 0;
2498 This->fInfo.dwScale = 0;
2499 This->fInfo.dwRate = 0;
2500 This->fInfo.dwLength = 0;
2501 This->dwInitialFrames = 0;
2503 for (i = 0; i < This->fInfo.dwStreams; i++) {
2504 AVISTREAMINFOW *psi;
2505 DWORD n;
2507 /* pre-conditions */
2508 assert(This->ppStreams[i] != NULL);
2510 psi = &This->ppStreams[i]->sInfo;
2511 assert(psi->dwScale != 0);
2512 assert(psi->dwRate != 0);
2514 if (i == 0) {
2515 /* use first stream timings as base */
2516 This->fInfo.dwScale = psi->dwScale;
2517 This->fInfo.dwRate = psi->dwRate;
2518 This->fInfo.dwLength = psi->dwLength;
2519 } else {
2520 n = AVIStreamSampleToSample((PAVISTREAM)This->ppStreams[0],
2521 (PAVISTREAM)This->ppStreams[i],psi->dwLength);
2522 if (This->fInfo.dwLength < n)
2523 This->fInfo.dwLength = n;
2526 if (This->dwInitialFrames < psi->dwInitialFrames)
2527 This->dwInitialFrames = psi->dwInitialFrames;
2529 if (This->fInfo.dwSuggestedBufferSize < psi->dwSuggestedBufferSize)
2530 This->fInfo.dwSuggestedBufferSize = psi->dwSuggestedBufferSize;
2532 if (psi->dwSampleSize != 0) {
2533 /* fixed sample size -- exact computation */
2534 This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSampleSize, psi->dwRate,
2535 psi->dwScale);
2536 } else {
2537 /* variable sample size -- only upper limit */
2538 This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSuggestedBufferSize,
2539 psi->dwRate, psi->dwScale);
2541 /* update dimensions */
2542 n = psi->rcFrame.right - psi->rcFrame.left;
2543 if (This->fInfo.dwWidth < n)
2544 This->fInfo.dwWidth = n;
2545 n = psi->rcFrame.bottom - psi->rcFrame.top;
2546 if (This->fInfo.dwHeight < n)
2547 This->fInfo.dwHeight = n;
2552 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
2553 FOURCC ckid, DWORD flags, LPVOID buffer,
2554 LONG size)
2556 MMCKINFO ck;
2558 ck.ckid = ckid;
2559 ck.cksize = size;
2560 ck.fccType = 0;
2562 /* if no frame/block is already written, we must compute start of movi chunk */
2563 if (This->paf->dwMoviChunkPos == 0)
2564 AVIFILE_ComputeMoviStart(This->paf);
2566 if (mmioSeek(This->paf->hmmio, This->paf->dwNextFramePos, SEEK_SET) == -1)
2567 return AVIERR_FILEWRITE;
2569 if (mmioCreateChunk(This->paf->hmmio, &ck, 0) != S_OK)
2570 return AVIERR_FILEWRITE;
2571 if (buffer != NULL && size > 0) {
2572 if (mmioWrite(This->paf->hmmio, (HPSTR)buffer, size) != size)
2573 return AVIERR_FILEWRITE;
2575 if (mmioAscend(This->paf->hmmio, &ck, 0) != S_OK)
2576 return AVIERR_FILEWRITE;
2578 This->paf->fDirty = TRUE;
2579 This->paf->dwNextFramePos = mmioSeek(This->paf->hmmio, 0, SEEK_CUR);
2581 return AVIFILE_AddFrame(This, ckid, size,
2582 ck.dwDataOffset - 2 * sizeof(DWORD), flags);