mshtml: Reimplement editor mode right key using command controller.
[wine/wine-kai.git] / dlls / avifil32 / avifile.c
blob3ed638684322076664a159b428b363f1be523347
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #include <assert.h>
34 #include <stdarg.h>
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "winerror.h"
42 #include "mmsystem.h"
43 #include "vfw.h"
45 #include "avifile_private.h"
46 #include "extrachunk.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
53 #ifndef IDX_PER_BLOCK
54 #define IDX_PER_BLOCK 2730
55 #endif
57 /***********************************************************************/
59 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
60 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
61 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile* iface);
62 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
63 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
64 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
65 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
66 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
67 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
68 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
70 static const struct IAVIFileVtbl iavift = {
71 IAVIFile_fnQueryInterface,
72 IAVIFile_fnAddRef,
73 IAVIFile_fnRelease,
74 IAVIFile_fnInfo,
75 IAVIFile_fnGetStream,
76 IAVIFile_fnCreateStream,
77 IAVIFile_fnWriteData,
78 IAVIFile_fnReadData,
79 IAVIFile_fnEndRecord,
80 IAVIFile_fnDeleteStream
83 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile*iface,REFIID refiid,LPVOID*obj);
84 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile*iface);
85 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile*iface);
86 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile*iface,CLSID*pClassID);
87 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile*iface);
88 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile*iface,LPCOLESTR pszFileName,DWORD dwMode);
89 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile*iface,LPCOLESTR pszFileName,BOOL fRemember);
90 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile*iface,LPCOLESTR pszFileName);
91 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile*iface,LPOLESTR*ppszFileName);
93 static const struct IPersistFileVtbl ipersistft = {
94 IPersistFile_fnQueryInterface,
95 IPersistFile_fnAddRef,
96 IPersistFile_fnRelease,
97 IPersistFile_fnGetClassID,
98 IPersistFile_fnIsDirty,
99 IPersistFile_fnLoad,
100 IPersistFile_fnSave,
101 IPersistFile_fnSaveCompleted,
102 IPersistFile_fnGetCurFile
105 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID *obj);
106 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream*iface);
107 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface);
108 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
109 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
110 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream*iface,LONG pos,LONG flags);
111 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG *formatsize);
112 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
113 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread);
114 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten);
115 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
116 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG *lpread);
117 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG size);
118 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
120 static const struct IAVIStreamVtbl iavist = {
121 IAVIStream_fnQueryInterface,
122 IAVIStream_fnAddRef,
123 IAVIStream_fnRelease,
124 IAVIStream_fnCreate,
125 IAVIStream_fnInfo,
126 IAVIStream_fnFindSample,
127 IAVIStream_fnReadFormat,
128 IAVIStream_fnSetFormat,
129 IAVIStream_fnRead,
130 IAVIStream_fnWrite,
131 IAVIStream_fnDelete,
132 IAVIStream_fnReadData,
133 IAVIStream_fnWriteData,
134 IAVIStream_fnSetInfo
137 typedef struct _IAVIFileImpl IAVIFileImpl;
139 typedef struct _IPersistFileImpl {
140 /* IUnknown stuff */
141 const IPersistFileVtbl *lpVtbl;
143 /* IPersistFile stuff */
144 IAVIFileImpl *paf;
145 } IPersistFileImpl;
147 typedef struct _IAVIStreamImpl {
148 /* IUnknown stuff */
149 const IAVIStreamVtbl *lpVtbl;
150 LONG ref;
152 /* IAVIStream stuff */
153 IAVIFileImpl *paf;
154 DWORD nStream; /* the n-th stream in file */
155 AVISTREAMINFOW sInfo;
157 LPVOID lpFormat;
158 DWORD cbFormat;
160 LPVOID lpHandlerData;
161 DWORD cbHandlerData;
163 EXTRACHUNKS extra;
165 LPDWORD lpBuffer;
166 DWORD cbBuffer; /* size of lpBuffer */
167 DWORD dwCurrentFrame; /* frame/block currently in lpBuffer */
169 LONG lLastFrame; /* last correct index in idxFrames */
170 AVIINDEXENTRY *idxFrames;
171 DWORD nIdxFrames; /* upper index limit of idxFrames */
172 AVIINDEXENTRY *idxFmtChanges;
173 DWORD nIdxFmtChanges; /* upper index limit of idxFmtChanges */
174 } IAVIStreamImpl;
176 struct _IAVIFileImpl {
177 /* IUnknown stuff */
178 const IAVIFileVtbl *lpVtbl;
179 LONG ref;
181 /* IAVIFile stuff... */
182 IPersistFileImpl iPersistFile;
184 AVIFILEINFOW fInfo;
185 IAVIStreamImpl *ppStreams[MAX_AVISTREAMS];
187 EXTRACHUNKS fileextra;
189 DWORD dwMoviChunkPos; /* some stuff for saving ... */
190 DWORD dwIdxChunkPos;
191 DWORD dwNextFramePos;
192 DWORD dwInitialFrames;
194 MMCKINFO ckLastRecord;
195 AVIINDEXENTRY *idxRecords; /* won't be updated while loading */
196 DWORD nIdxRecords; /* current fill level */
197 DWORD cbIdxRecords; /* size of idxRecords */
199 /* IPersistFile stuff ... */
200 HMMIO hmmio;
201 LPWSTR szFileName;
202 UINT uMode;
203 BOOL fDirty;
206 /***********************************************************************/
208 static HRESULT AVIFILE_AddFrame(IAVIStreamImpl *This, DWORD ckid, DWORD size,
209 DWORD offset, DWORD flags);
210 static HRESULT AVIFILE_AddRecord(IAVIFileImpl *This);
211 static DWORD AVIFILE_ComputeMoviStart(IAVIFileImpl *This);
212 static void AVIFILE_ConstructAVIStream(IAVIFileImpl *paf, DWORD nr,
213 const AVISTREAMINFOW *asi);
214 static void AVIFILE_DestructAVIStream(IAVIStreamImpl *This);
215 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
216 static HRESULT AVIFILE_LoadIndex(const IAVIFileImpl *This, DWORD size, DWORD offset);
217 static HRESULT AVIFILE_ParseIndex(const IAVIFileImpl *This, AVIINDEXENTRY *lp,
218 LONG count, DWORD pos, BOOL *bAbsolute);
219 static HRESULT AVIFILE_ReadBlock(IAVIStreamImpl *This, DWORD start,
220 LPVOID buffer, LONG size);
221 static void AVIFILE_SamplesToBlock(const IAVIStreamImpl *This, LPLONG pos,
222 LPLONG offset);
223 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
224 static HRESULT AVIFILE_SaveIndex(const IAVIFileImpl *This);
225 static ULONG AVIFILE_SearchStream(const IAVIFileImpl *This, DWORD fccType,
226 LONG lSkip);
227 static void AVIFILE_UpdateInfo(IAVIFileImpl *This);
228 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
229 FOURCC ckid, DWORD flags, LPVOID buffer,
230 LONG size);
232 HRESULT AVIFILE_CreateAVIFile(REFIID riid, LPVOID *ppv)
234 IAVIFileImpl *pfile;
235 HRESULT hr;
237 assert(riid != NULL && ppv != NULL);
239 *ppv = NULL;
241 pfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIFileImpl));
242 if (pfile == NULL)
243 return AVIERR_MEMORY;
245 pfile->lpVtbl = &iavift;
246 pfile->ref = 0;
247 pfile->iPersistFile.lpVtbl = &ipersistft;
248 pfile->iPersistFile.paf = pfile;
250 hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
251 if (FAILED(hr))
252 HeapFree(GetProcessHeap(), 0, pfile);
254 return hr;
257 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
258 LPVOID *obj)
260 IAVIFileImpl *This = (IAVIFileImpl *)iface;
262 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
264 if (IsEqualGUID(&IID_IUnknown, refiid) ||
265 IsEqualGUID(&IID_IAVIFile, refiid)) {
266 *obj = iface;
267 IAVIFile_AddRef(iface);
269 return S_OK;
270 } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
271 *obj = &This->iPersistFile;
272 IAVIFile_AddRef(iface);
274 return S_OK;
277 return OLE_E_ENUM_NOMORE;
280 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
282 IAVIFileImpl *This = (IAVIFileImpl *)iface;
283 ULONG ref = InterlockedIncrement(&This->ref);
285 TRACE("(%p) -> %d\n", iface, ref);
287 return ref;
290 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
292 IAVIFileImpl *This = (IAVIFileImpl *)iface;
293 UINT i;
294 ULONG ref = InterlockedDecrement(&This->ref);
296 TRACE("(%p) -> %d\n", iface, ref);
298 if (!ref) {
299 if (This->fDirty) {
300 /* need to write headers to file */
301 AVIFILE_SaveFile(This);
304 for (i = 0; i < This->fInfo.dwStreams; i++) {
305 if (This->ppStreams[i] != NULL) {
306 if (This->ppStreams[i]->ref != 0) {
307 ERR(": someone has still %u reference to stream %u (%p)!\n",
308 This->ppStreams[i]->ref, i, This->ppStreams[i]);
310 AVIFILE_DestructAVIStream(This->ppStreams[i]);
311 HeapFree(GetProcessHeap(), 0, This->ppStreams[i]);
312 This->ppStreams[i] = NULL;
316 if (This->idxRecords != NULL) {
317 HeapFree(GetProcessHeap(), 0, This->idxRecords);
318 This->idxRecords = NULL;
319 This->nIdxRecords = 0;
322 if (This->fileextra.lp != NULL) {
323 HeapFree(GetProcessHeap(), 0, This->fileextra.lp);
324 This->fileextra.lp = NULL;
325 This->fileextra.cb = 0;
328 HeapFree(GetProcessHeap(), 0, This->szFileName);
329 This->szFileName = NULL;
331 if (This->hmmio != NULL) {
332 mmioClose(This->hmmio, 0);
333 This->hmmio = NULL;
336 HeapFree(GetProcessHeap(), 0, This);
338 return ref;
341 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
342 LONG size)
344 IAVIFileImpl *This = (IAVIFileImpl *)iface;
346 TRACE("(%p,%p,%d)\n",iface,afi,size);
348 if (afi == NULL)
349 return AVIERR_BADPARAM;
350 if (size < 0)
351 return AVIERR_BADSIZE;
353 AVIFILE_UpdateInfo(This);
355 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
357 if ((DWORD)size < sizeof(This->fInfo))
358 return AVIERR_BUFFERTOOSMALL;
359 return AVIERR_OK;
362 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
363 DWORD fccType, LONG lParam)
365 IAVIFileImpl *This = (IAVIFileImpl *)iface;
367 ULONG nStream;
369 TRACE("(%p,%p,0x%08X,%d)\n", iface, avis, fccType, lParam);
371 if (avis == NULL || lParam < 0)
372 return AVIERR_BADPARAM;
374 nStream = AVIFILE_SearchStream(This, fccType, lParam);
376 /* Does the requested stream exist? */
377 if (nStream < This->fInfo.dwStreams &&
378 This->ppStreams[nStream] != NULL) {
379 *avis = (PAVISTREAM)This->ppStreams[nStream];
380 IAVIStream_AddRef(*avis);
382 return AVIERR_OK;
385 /* Sorry, but the specified stream doesn't exist */
386 return AVIERR_NODATA;
389 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
390 LPAVISTREAMINFOW asi)
392 IAVIFileImpl *This = (IAVIFileImpl *)iface;
394 DWORD n;
396 TRACE("(%p,%p,%p)\n", iface, avis, asi);
398 /* check parameters */
399 if (avis == NULL || asi == NULL)
400 return AVIERR_BADPARAM;
402 *avis = NULL;
404 /* Does the user have write permission? */
405 if ((This->uMode & MMIO_RWMODE) == 0)
406 return AVIERR_READONLY;
408 /* Can we add another stream? */
409 n = This->fInfo.dwStreams;
410 if (n >= MAX_AVISTREAMS || This->dwMoviChunkPos != 0) {
411 /* already reached max nr of streams
412 * or have already written frames to disk */
413 return AVIERR_UNSUPPORTED;
416 /* check AVISTREAMINFO for some really needed things */
417 if (asi->fccType == 0 || asi->dwScale == 0 || asi->dwRate == 0)
418 return AVIERR_BADFORMAT;
420 /* now it seems to be save to add the stream */
421 assert(This->ppStreams[n] == NULL);
422 This->ppStreams[n] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
423 sizeof(IAVIStreamImpl));
424 if (This->ppStreams[n] == NULL)
425 return AVIERR_MEMORY;
427 /* initialize the new allocated stream */
428 AVIFILE_ConstructAVIStream(This, n, asi);
430 This->fInfo.dwStreams++;
431 This->fDirty = TRUE;
433 /* update our AVIFILEINFO structure */
434 AVIFILE_UpdateInfo(This);
436 /* return it */
437 *avis = (PAVISTREAM)This->ppStreams[n];
438 IAVIStream_AddRef(*avis);
440 return AVIERR_OK;
443 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
444 LPVOID lpData, LONG size)
446 IAVIFileImpl *This = (IAVIFileImpl *)iface;
448 TRACE("(%p,0x%08X,%p,%d)\n", iface, ckid, lpData, size);
450 /* check parameters */
451 if (lpData == NULL)
452 return AVIERR_BADPARAM;
453 if (size < 0)
454 return AVIERR_BADSIZE;
456 /* Do we have write permission? */
457 if ((This->uMode & MMIO_RWMODE) == 0)
458 return AVIERR_READONLY;
460 This->fDirty = TRUE;
462 return WriteExtraChunk(&This->fileextra, ckid, lpData, size);
465 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
466 LPVOID lpData, LONG *size)
468 IAVIFileImpl *This = (IAVIFileImpl *)iface;
470 TRACE("(%p,0x%08X,%p,%p)\n", iface, ckid, lpData, size);
472 return ReadExtraChunk(&This->fileextra, ckid, lpData, size);
475 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
477 IAVIFileImpl *This = (IAVIFileImpl *)iface;
479 TRACE("(%p)\n",iface);
481 if ((This->uMode & MMIO_RWMODE) == 0)
482 return AVIERR_READONLY;
484 This->fDirty = TRUE;
486 /* no frames written to any stream? -- compute start of 'movi'-chunk */
487 if (This->dwMoviChunkPos == 0)
488 AVIFILE_ComputeMoviStart(This);
490 This->fInfo.dwFlags |= AVIFILEINFO_ISINTERLEAVED;
492 /* already written frames to any stream, ... */
493 if (This->ckLastRecord.dwFlags & MMIO_DIRTY) {
494 /* close last record */
495 if (mmioAscend(This->hmmio, &This->ckLastRecord, 0) != 0)
496 return AVIERR_FILEWRITE;
498 AVIFILE_AddRecord(This);
500 if (This->fInfo.dwSuggestedBufferSize < This->ckLastRecord.cksize + 3 * sizeof(DWORD))
501 This->fInfo.dwSuggestedBufferSize = This->ckLastRecord.cksize + 3 * sizeof(DWORD);
504 /* write out a new record into file, but don't close it */
505 This->ckLastRecord.cksize = 0;
506 This->ckLastRecord.fccType = listtypeAVIRECORD;
507 if (mmioSeek(This->hmmio, This->dwNextFramePos, SEEK_SET) == -1)
508 return AVIERR_FILEWRITE;
509 if (mmioCreateChunk(This->hmmio, &This->ckLastRecord, MMIO_CREATELIST) != 0)
510 return AVIERR_FILEWRITE;
511 This->dwNextFramePos += 3 * sizeof(DWORD);
513 return AVIERR_OK;
516 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
517 LONG lParam)
519 IAVIFileImpl *This = (IAVIFileImpl *)iface;
521 ULONG nStream;
523 TRACE("(%p,0x%08X,%d)\n", iface, fccType, lParam);
525 /* check parameter */
526 if (lParam < 0)
527 return AVIERR_BADPARAM;
529 /* Have user write permissions? */
530 if ((This->uMode & MMIO_RWMODE) == 0)
531 return AVIERR_READONLY;
533 nStream = AVIFILE_SearchStream(This, fccType, lParam);
535 /* Does the requested stream exist? */
536 if (nStream < This->fInfo.dwStreams &&
537 This->ppStreams[nStream] != NULL) {
538 /* ... so delete it now */
539 HeapFree(GetProcessHeap(), 0, This->ppStreams[nStream]);
541 if (This->fInfo.dwStreams - nStream > 0)
542 memcpy(This->ppStreams + nStream, This->ppStreams + nStream + 1,
543 (This->fInfo.dwStreams - nStream) * sizeof(IAVIStreamImpl*));
545 This->ppStreams[This->fInfo.dwStreams] = NULL;
546 This->fInfo.dwStreams--;
547 This->fDirty = TRUE;
549 /* This->fInfo will be updated further when asked for */
550 return AVIERR_OK;
551 } else
552 return AVIERR_NODATA;
555 /***********************************************************************/
557 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
558 REFIID refiid, LPVOID *obj)
560 IPersistFileImpl *This = (IPersistFileImpl *)iface;
562 assert(This->paf != NULL);
564 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
567 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
569 IPersistFileImpl *This = (IPersistFileImpl *)iface;
571 assert(This->paf != NULL);
573 return IAVIFile_AddRef((PAVIFILE)This->paf);
576 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface)
578 IPersistFileImpl *This = (IPersistFileImpl *)iface;
580 assert(This->paf != NULL);
582 return IAVIFile_Release((PAVIFILE)This->paf);
585 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
586 LPCLSID pClassID)
588 TRACE("(%p,%p)\n", iface, pClassID);
590 if (pClassID == NULL)
591 return AVIERR_BADPARAM;
593 memcpy(pClassID, &CLSID_AVIFile, sizeof(CLSID_AVIFile));
595 return AVIERR_OK;
598 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
600 IPersistFileImpl *This = (IPersistFileImpl *)iface;
602 TRACE("(%p)\n", iface);
604 assert(This->paf != NULL);
606 return (This->paf->fDirty ? S_OK : S_FALSE);
609 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
610 LPCOLESTR pszFileName, DWORD dwMode)
612 IPersistFileImpl *This = (IPersistFileImpl *)iface;
614 int len;
616 TRACE("(%p,%s,0x%08X)\n", iface, debugstr_w(pszFileName), dwMode);
618 /* check parameter */
619 if (pszFileName == NULL)
620 return AVIERR_BADPARAM;
622 assert(This->paf != NULL);
623 if (This->paf->hmmio != NULL)
624 return AVIERR_ERROR; /* No reuse of this object for another file! */
626 /* remeber mode and name */
627 This->paf->uMode = dwMode;
629 len = lstrlenW(pszFileName) + 1;
630 This->paf->szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
631 if (This->paf->szFileName == NULL)
632 return AVIERR_MEMORY;
633 lstrcpyW(This->paf->szFileName, pszFileName);
635 /* try to open the file */
636 This->paf->hmmio = mmioOpenW(This->paf->szFileName, NULL,
637 MMIO_ALLOCBUF | dwMode);
638 if (This->paf->hmmio == NULL) {
639 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
640 LPSTR szFileName;
642 len = WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1,
643 NULL, 0, NULL, NULL);
644 szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR));
645 if (szFileName == NULL)
646 return AVIERR_MEMORY;
648 WideCharToMultiByte(CP_ACP, 0, This->paf->szFileName, -1, szFileName,
649 len, NULL, NULL);
651 This->paf->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
652 HeapFree(GetProcessHeap(), 0, szFileName);
653 if (This->paf->hmmio == NULL)
654 return AVIERR_FILEOPEN;
657 /* should we create a new file? */
658 if (dwMode & OF_CREATE) {
659 memset(& This->paf->fInfo, 0, sizeof(This->paf->fInfo));
660 This->paf->fInfo.dwFlags = AVIFILEINFO_HASINDEX | AVIFILEINFO_TRUSTCKTYPE;
662 return AVIERR_OK;
663 } else
664 return AVIFILE_LoadFile(This->paf);
667 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
668 LPCOLESTR pszFileName,BOOL fRemember)
670 TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
672 /* We write directly to disk, so nothing to do. */
674 return AVIERR_OK;
677 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
678 LPCOLESTR pszFileName)
680 TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
682 /* We write directly to disk, so nothing to do. */
684 return AVIERR_OK;
687 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
688 LPOLESTR *ppszFileName)
690 IPersistFileImpl *This = (IPersistFileImpl *)iface;
692 TRACE("(%p,%p)\n", iface, ppszFileName);
694 if (ppszFileName == NULL)
695 return AVIERR_BADPARAM;
697 *ppszFileName = NULL;
699 assert(This->paf != NULL);
701 if (This->paf->szFileName != NULL) {
702 int len = lstrlenW(This->paf->szFileName) + 1;
704 *ppszFileName = CoTaskMemAlloc(len * sizeof(WCHAR));
705 if (*ppszFileName == NULL)
706 return AVIERR_MEMORY;
708 strcpyW(*ppszFileName, This->paf->szFileName);
711 return AVIERR_OK;
714 /***********************************************************************/
716 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
717 REFIID refiid, LPVOID *obj)
719 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
721 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(refiid), obj);
723 if (IsEqualGUID(&IID_IUnknown, refiid) ||
724 IsEqualGUID(&IID_IAVIStream, refiid)) {
725 *obj = This;
726 IAVIStream_AddRef(iface);
728 return S_OK;
730 /* FIXME: IAVIStreaming interface */
732 return OLE_E_ENUM_NOMORE;
735 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
737 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
738 ULONG ref = InterlockedIncrement(&This->ref);
740 TRACE("(%p) -> %d\n", iface, ref);
742 /* also add ref to parent, so that it doesn't kill us */
743 if (This->paf != NULL)
744 IAVIFile_AddRef((PAVIFILE)This->paf);
746 return ref;
749 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
751 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
752 ULONG ref = InterlockedDecrement(&This->ref);
754 TRACE("(%p) -> %d\n", iface, ref);
756 if (This->paf != NULL)
757 IAVIFile_Release((PAVIFILE)This->paf);
759 return ref;
762 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
763 LPARAM lParam2)
765 TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
767 /* This IAVIStream interface needs an AVIFile */
768 return AVIERR_UNSUPPORTED;
771 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
772 LONG size)
774 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
776 TRACE("(%p,%p,%d)\n", iface, psi, size);
778 if (psi == NULL)
779 return AVIERR_BADPARAM;
780 if (size < 0)
781 return AVIERR_BADSIZE;
783 memcpy(psi, &This->sInfo, min((DWORD)size, sizeof(This->sInfo)));
785 if ((DWORD)size < sizeof(This->sInfo))
786 return AVIERR_BUFFERTOOSMALL;
787 return AVIERR_OK;
790 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
791 LONG flags)
793 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
795 LONG offset = 0;
797 TRACE("(%p,%d,0x%08X)\n",iface,pos,flags);
799 if (flags & FIND_FROM_START) {
800 pos = This->sInfo.dwStart;
801 flags &= ~(FIND_FROM_START|FIND_PREV);
802 flags |= FIND_NEXT;
805 if (This->sInfo.dwSampleSize != 0) {
806 /* convert samples into block number with offset */
807 AVIFILE_SamplesToBlock(This, &pos, &offset);
810 if (flags & FIND_TYPE) {
811 if (flags & FIND_KEY) {
812 while (0 <= pos && pos <= This->lLastFrame) {
813 if (This->idxFrames[pos].dwFlags & AVIIF_KEYFRAME)
814 goto RETURN_FOUND;
816 if (flags & FIND_NEXT)
817 pos++;
818 else
819 pos--;
821 } else if (flags & FIND_ANY) {
822 while (0 <= pos && pos <= This->lLastFrame) {
823 if (This->idxFrames[pos].dwChunkLength > 0)
824 goto RETURN_FOUND;
826 if (flags & FIND_NEXT)
827 pos++;
828 else
829 pos--;
832 } else if ((flags & FIND_FORMAT) && This->idxFmtChanges != NULL &&
833 This->sInfo.fccType == streamtypeVIDEO) {
834 if (flags & FIND_NEXT) {
835 ULONG n;
837 for (n = 0; n < This->sInfo.dwFormatChangeCount; n++)
838 if (This->idxFmtChanges[n].ckid >= pos) {
839 pos = This->idxFmtChanges[n].ckid;
840 goto RETURN_FOUND;
842 } else {
843 LONG n;
845 for (n = (LONG)This->sInfo.dwFormatChangeCount; n >= 0; n--) {
846 if (This->idxFmtChanges[n].ckid <= pos) {
847 pos = This->idxFmtChanges[n].ckid;
848 goto RETURN_FOUND;
852 if (pos > (LONG)This->sInfo.dwStart)
853 return 0; /* format changes always for first frame */
857 return -1;
860 RETURN_FOUND:
861 if (pos < (LONG)This->sInfo.dwStart)
862 return -1;
864 switch (flags & FIND_RET) {
865 case FIND_LENGTH:
866 /* physical size */
867 pos = This->idxFrames[pos].dwChunkLength;
868 break;
869 case FIND_OFFSET:
870 /* physical position */
871 pos = This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD)
872 + offset * This->sInfo.dwSampleSize;
873 break;
874 case FIND_SIZE:
875 /* logical size */
876 if (This->sInfo.dwSampleSize)
877 pos = This->sInfo.dwSampleSize;
878 else
879 pos = 1;
880 break;
881 case FIND_INDEX:
882 FIXME(": FIND_INDEX flag is not supported!\n");
883 /* This is an index in the index-table on disc. */
884 break;
885 }; /* else logical position */
887 return pos;
890 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
891 LPVOID format, LONG *formatsize)
893 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
895 TRACE("(%p,%d,%p,%p)\n", iface, pos, format, formatsize);
897 if (formatsize == NULL)
898 return AVIERR_BADPARAM;
900 /* only interested in needed buffersize? */
901 if (format == NULL || *formatsize <= 0) {
902 *formatsize = This->cbFormat;
904 return AVIERR_OK;
907 /* copy initial format (only as much as will fit) */
908 memcpy(format, This->lpFormat, min(*(DWORD*)formatsize, This->cbFormat));
909 if (*(DWORD*)formatsize < This->cbFormat) {
910 *formatsize = This->cbFormat;
911 return AVIERR_BUFFERTOOSMALL;
914 /* Could format change? When yes will it change? */
915 if ((This->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
916 pos > This->sInfo.dwStart) {
917 LONG lLastFmt;
919 lLastFmt = IAVIStream_fnFindSample(iface, pos, FIND_FORMAT|FIND_PREV);
920 if (lLastFmt > 0) {
921 FIXME(": need to read formatchange for %d -- unimplemented!\n",lLastFmt);
925 *formatsize = This->cbFormat;
926 return AVIERR_OK;
929 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
930 LPVOID format, LONG formatsize)
932 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
934 LPBITMAPINFOHEADER lpbiNew = (LPBITMAPINFOHEADER)format;
936 TRACE("(%p,%d,%p,%d)\n", iface, pos, format, formatsize);
938 /* check parameters */
939 if (format == NULL || formatsize <= 0)
940 return AVIERR_BADPARAM;
942 /* Do we have write permission? */
943 if ((This->paf->uMode & MMIO_RWMODE) == 0)
944 return AVIERR_READONLY;
946 /* can only set format before frame is written! */
947 if (This->lLastFrame > pos)
948 return AVIERR_UNSUPPORTED;
950 /* initial format or a formatchange? */
951 if (This->lpFormat == NULL) {
952 /* initial format */
953 if (This->paf->dwMoviChunkPos != 0)
954 return AVIERR_ERROR; /* user has used API in wrong sequnece! */
956 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, formatsize);
957 if (This->lpFormat == NULL)
958 return AVIERR_MEMORY;
959 This->cbFormat = formatsize;
961 memcpy(This->lpFormat, format, formatsize);
963 /* update some infos about stream */
964 if (This->sInfo.fccType == streamtypeVIDEO) {
965 LONG lDim;
967 lDim = This->sInfo.rcFrame.right - This->sInfo.rcFrame.left;
968 if (lDim < lpbiNew->biWidth)
969 This->sInfo.rcFrame.right = This->sInfo.rcFrame.left + lpbiNew->biWidth;
970 lDim = This->sInfo.rcFrame.bottom - This->sInfo.rcFrame.top;
971 if (lDim < lpbiNew->biHeight)
972 This->sInfo.rcFrame.bottom = This->sInfo.rcFrame.top + lpbiNew->biHeight;
973 } else if (This->sInfo.fccType == streamtypeAUDIO)
974 This->sInfo.dwSampleSize = ((LPWAVEFORMATEX)This->lpFormat)->nBlockAlign;
976 return AVIERR_OK;
977 } else {
978 MMCKINFO ck;
979 LPBITMAPINFOHEADER lpbiOld = (LPBITMAPINFOHEADER)This->lpFormat;
980 RGBQUAD *rgbNew = (RGBQUAD*)((LPBYTE)lpbiNew + lpbiNew->biSize);
981 AVIPALCHANGE *lppc = NULL;
982 UINT n;
984 /* perhaps format change, check it ... */
985 if (This->cbFormat != formatsize)
986 return AVIERR_UNSUPPORTED;
988 /* no format change, only the initial one */
989 if (memcmp(This->lpFormat, format, formatsize) == 0)
990 return AVIERR_OK;
992 /* check that's only the palette, which changes */
993 if (lpbiOld->biSize != lpbiNew->biSize ||
994 lpbiOld->biWidth != lpbiNew->biWidth ||
995 lpbiOld->biHeight != lpbiNew->biHeight ||
996 lpbiOld->biPlanes != lpbiNew->biPlanes ||
997 lpbiOld->biBitCount != lpbiNew->biBitCount ||
998 lpbiOld->biCompression != lpbiNew->biCompression ||
999 lpbiOld->biClrUsed != lpbiNew->biClrUsed)
1000 return AVIERR_UNSUPPORTED;
1002 This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
1004 /* simply say all colors have changed */
1005 ck.ckid = MAKEAVICKID(cktypePALchange, This->nStream);
1006 ck.cksize = 2 * sizeof(WORD) + lpbiOld->biClrUsed * sizeof(PALETTEENTRY);
1007 lppc = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1008 if (lppc == NULL)
1009 return AVIERR_MEMORY;
1011 lppc->bFirstEntry = 0;
1012 lppc->bNumEntries = (lpbiOld->biClrUsed < 256 ? lpbiOld->biClrUsed : 0);
1013 lppc->wFlags = 0;
1014 for (n = 0; n < lpbiOld->biClrUsed; n++) {
1015 lppc->peNew[n].peRed = rgbNew[n].rgbRed;
1016 lppc->peNew[n].peGreen = rgbNew[n].rgbGreen;
1017 lppc->peNew[n].peBlue = rgbNew[n].rgbBlue;
1018 lppc->peNew[n].peFlags = 0;
1021 if (mmioSeek(This->paf->hmmio, This->paf->dwNextFramePos, SEEK_SET) == -1)
1022 return AVIERR_FILEWRITE;
1023 if (mmioCreateChunk(This->paf->hmmio, &ck, 0) != S_OK)
1024 return AVIERR_FILEWRITE;
1025 if (mmioWrite(This->paf->hmmio, (HPSTR)lppc, ck.cksize) != ck.cksize)
1026 return AVIERR_FILEWRITE;
1027 if (mmioAscend(This->paf->hmmio, &ck, 0) != S_OK)
1028 return AVIERR_FILEWRITE;
1029 This->paf->dwNextFramePos += ck.cksize + 2 * sizeof(DWORD);
1031 HeapFree(GetProcessHeap(), 0, lppc);
1033 return AVIFILE_AddFrame(This, cktypePALchange, n, ck.dwDataOffset, 0);
1037 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
1038 LONG samples, LPVOID buffer,
1039 LONG buffersize, LPLONG bytesread,
1040 LPLONG samplesread)
1042 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1044 DWORD size;
1045 HRESULT hr;
1047 TRACE("(%p,%d,%d,%p,%d,%p,%p)\n", iface, start, samples, buffer,
1048 buffersize, bytesread, samplesread);
1050 /* clear return parameters if given */
1051 if (bytesread != NULL)
1052 *bytesread = 0;
1053 if (samplesread != NULL)
1054 *samplesread = 0;
1056 /* check parameters */
1057 if ((LONG)This->sInfo.dwStart > start)
1058 return AVIERR_NODATA; /* couldn't read before start of stream */
1059 if (This->sInfo.dwStart + This->sInfo.dwLength < (DWORD)start)
1060 return AVIERR_NODATA; /* start is past end of stream */
1062 /* should we read as much as possible? */
1063 if (samples == -1) {
1064 /* User should know how much we have read */
1065 if (bytesread == NULL && samplesread == NULL)
1066 return AVIERR_BADPARAM;
1068 if (This->sInfo.dwSampleSize != 0)
1069 samples = buffersize / This->sInfo.dwSampleSize;
1070 else
1071 samples = 1;
1074 /* limit to end of stream */
1075 if ((LONG)This->sInfo.dwLength < samples)
1076 samples = This->sInfo.dwLength;
1077 if ((start - This->sInfo.dwStart) > (This->sInfo.dwLength - samples))
1078 samples = This->sInfo.dwLength - (start - This->sInfo.dwStart);
1080 /* nothing to read? Then leave ... */
1081 if (samples == 0)
1082 return AVIERR_OK;
1084 if (This->sInfo.dwSampleSize != 0) {
1085 /* fixed samplesize -- we can read over frame/block boundaries */
1086 LONG block = start;
1087 LONG offset = 0;
1089 /* convert start sample to block,offset pair */
1090 AVIFILE_SamplesToBlock(This, &block, &offset);
1092 /* convert samples to bytes */
1093 samples *= This->sInfo.dwSampleSize;
1095 while (samples > 0 && buffersize > 0) {
1096 if (block != This->dwCurrentFrame) {
1097 hr = AVIFILE_ReadBlock(This, block, NULL, 0);
1098 if (FAILED(hr))
1099 return hr;
1102 size = min((DWORD)samples, (DWORD)buffersize);
1103 size = min(size, This->cbBuffer - offset);
1104 memcpy(buffer, ((BYTE*)&This->lpBuffer[2]) + offset, size);
1106 block++;
1107 offset = 0;
1108 buffer = ((LPBYTE)buffer)+size;
1109 samples -= size;
1110 buffersize -= size;
1112 /* fill out return parameters if given */
1113 if (bytesread != NULL)
1114 *bytesread += size;
1115 if (samplesread != NULL)
1116 *samplesread += size / This->sInfo.dwSampleSize;
1119 if (samples == 0)
1120 return AVIERR_OK;
1121 else
1122 return AVIERR_BUFFERTOOSMALL;
1123 } else {
1124 /* variable samplesize -- we can only read one full frame/block */
1125 if (samples > 1)
1126 samples = 1;
1128 assert(start <= This->lLastFrame);
1129 size = This->idxFrames[start].dwChunkLength;
1130 if (buffer != NULL && buffersize >= size) {
1131 hr = AVIFILE_ReadBlock(This, start, buffer, size);
1132 if (FAILED(hr))
1133 return hr;
1134 } else if (buffer != NULL)
1135 return AVIERR_BUFFERTOOSMALL;
1137 /* fill out return parameters if given */
1138 if (bytesread != NULL)
1139 *bytesread = size;
1140 if (samplesread != NULL)
1141 *samplesread = samples;
1143 return AVIERR_OK;
1147 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
1148 LONG samples, LPVOID buffer,
1149 LONG buffersize, DWORD flags,
1150 LPLONG sampwritten,
1151 LPLONG byteswritten)
1153 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1155 FOURCC ckid;
1156 HRESULT hr;
1158 TRACE("(%p,%d,%d,%p,%d,0x%08X,%p,%p)\n", iface, start, samples,
1159 buffer, buffersize, flags, sampwritten, byteswritten);
1161 /* clear return parameters if given */
1162 if (sampwritten != NULL)
1163 *sampwritten = 0;
1164 if (byteswritten != NULL)
1165 *byteswritten = 0;
1167 /* check parameters */
1168 if (buffer == NULL && (buffersize > 0 || samples > 0))
1169 return AVIERR_BADPARAM;
1171 /* Have we write permission? */
1172 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1173 return AVIERR_READONLY;
1175 switch (This->sInfo.fccType) {
1176 case streamtypeAUDIO:
1177 ckid = MAKEAVICKID(cktypeWAVEbytes, This->nStream);
1178 break;
1179 default:
1180 if ((flags & AVIIF_KEYFRAME) && buffersize != 0)
1181 ckid = MAKEAVICKID(cktypeDIBbits, This->nStream);
1182 else
1183 ckid = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1184 break;
1187 /* append to end of stream? */
1188 if (start == -1) {
1189 if (This->lLastFrame == -1)
1190 start = This->sInfo.dwStart;
1191 else
1192 start = This->sInfo.dwLength;
1193 } else if (This->lLastFrame == -1)
1194 This->sInfo.dwStart = start;
1196 if (This->sInfo.dwSampleSize != 0) {
1197 /* fixed sample size -- audio like */
1198 if (samples * This->sInfo.dwSampleSize != buffersize)
1199 return AVIERR_BADPARAM;
1201 /* Couldn't skip audio-like data -- User must supply appropriate silence */
1202 if (This->sInfo.dwLength != start)
1203 return AVIERR_UNSUPPORTED;
1205 /* Convert position to frame/block */
1206 start = This->lLastFrame + 1;
1208 if ((This->paf->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) == 0) {
1209 FIXME(": not interleaved, could collect audio data!\n");
1211 } else {
1212 /* variable sample size -- video like */
1213 if (samples > 1)
1214 return AVIERR_UNSUPPORTED;
1216 /* must we fill up with empty frames? */
1217 if (This->lLastFrame != -1) {
1218 FOURCC ckid2 = MAKEAVICKID(cktypeDIBcompressed, This->nStream);
1220 while (start > This->lLastFrame + 1) {
1221 hr = AVIFILE_WriteBlock(This, This->lLastFrame + 1, ckid2, 0, NULL, 0);
1222 if (FAILED(hr))
1223 return hr;
1228 /* write the block now */
1229 hr = AVIFILE_WriteBlock(This, start, ckid, flags, buffer, buffersize);
1230 if (SUCCEEDED(hr)) {
1231 /* fill out return parameters if given */
1232 if (sampwritten != NULL)
1233 *sampwritten = samples;
1234 if (byteswritten != NULL)
1235 *byteswritten = buffersize;
1238 return hr;
1241 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
1242 LONG samples)
1244 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1246 FIXME("(%p,%d,%d): stub\n", iface, start, samples);
1248 /* check parameters */
1249 if (start < 0 || samples < 0)
1250 return AVIERR_BADPARAM;
1252 /* Delete before start of stream? */
1253 if (start + samples < This->sInfo.dwStart)
1254 return AVIERR_OK;
1256 /* Delete after end of stream? */
1257 if (start > This->sInfo.dwLength)
1258 return AVIERR_OK;
1260 /* For the rest we need write permissions */
1261 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1262 return AVIERR_READONLY;
1264 /* 1. overwrite the data with JUNK
1266 * if ISINTERLEAVED {
1267 * 2. concat all neighboured JUNK-blocks in this record to one
1268 * 3. if this record only contains JUNK and is at end set dwNextFramePos
1269 * to start of this record, repeat this.
1270 * } else {
1271 * 2. concat all neighboured JUNK-blocks.
1272 * 3. if the JUNK block is at the end, then set dwNextFramePos to
1273 * start of this block.
1277 return AVIERR_UNSUPPORTED;
1280 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1281 LPVOID lp, LPLONG lpread)
1283 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1285 TRACE("(%p,0x%08X,%p,%p)\n", iface, fcc, lp, lpread);
1287 if (fcc == ckidSTREAMHANDLERDATA) {
1288 if (This->lpHandlerData != NULL && This->cbHandlerData > 0) {
1289 if (lp == NULL || *lpread <= 0) {
1290 *lpread = This->cbHandlerData;
1291 return AVIERR_OK;
1294 memcpy(lp, This->lpHandlerData, min(This->cbHandlerData, *lpread));
1295 if (*lpread < This->cbHandlerData)
1296 return AVIERR_BUFFERTOOSMALL;
1297 return AVIERR_OK;
1298 } else
1299 return AVIERR_NODATA;
1300 } else
1301 return ReadExtraChunk(&This->extra, fcc, lp, lpread);
1304 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1305 LPVOID lp, LONG size)
1307 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1309 TRACE("(%p,0x%08x,%p,%d)\n", iface, fcc, lp, size);
1311 /* check parameters */
1312 if (lp == NULL)
1313 return AVIERR_BADPARAM;
1314 if (size <= 0)
1315 return AVIERR_BADSIZE;
1317 /* need write permission */
1318 if ((This->paf->uMode & MMIO_RWMODE) == 0)
1319 return AVIERR_READONLY;
1321 /* already written something to this file? */
1322 if (This->paf->dwMoviChunkPos != 0) {
1323 /* the data will be inserted before the 'movi' chunk, so check for
1324 * enough space */
1325 DWORD dwPos = AVIFILE_ComputeMoviStart(This->paf);
1327 /* ckid,size => 2 * sizeof(DWORD) */
1328 dwPos += 2 * sizeof(DWORD) + size;
1329 if (size >= This->paf->dwMoviChunkPos - 2 * sizeof(DWORD))
1330 return AVIERR_UNSUPPORTED; /* not enough space left */
1333 This->paf->fDirty = TRUE;
1335 if (fcc == ckidSTREAMHANDLERDATA) {
1336 if (This->lpHandlerData != NULL) {
1337 FIXME(": handler data already set -- overwirte?\n");
1338 return AVIERR_UNSUPPORTED;
1341 This->lpHandlerData = HeapAlloc(GetProcessHeap(), 0, size);
1342 if (This->lpHandlerData == NULL)
1343 return AVIERR_MEMORY;
1344 This->cbHandlerData = size;
1345 memcpy(This->lpHandlerData, lp, size);
1347 return AVIERR_OK;
1348 } else
1349 return WriteExtraChunk(&This->extra, fcc, lp, size);
1352 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1353 LPAVISTREAMINFOW info, LONG infolen)
1355 FIXME("(%p,%p,%d): stub\n", iface, info, infolen);
1357 return E_FAIL;
1360 /***********************************************************************/
1362 static HRESULT AVIFILE_AddFrame(IAVIStreamImpl *This, DWORD ckid, DWORD size, DWORD offset, DWORD flags)
1364 /* pre-conditions */
1365 assert(This != NULL);
1367 switch (TWOCCFromFOURCC(ckid)) {
1368 case cktypeDIBbits:
1369 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1370 flags |= AVIIF_KEYFRAME;
1371 break;
1372 case cktypeDIBcompressed:
1373 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1374 flags &= ~AVIIF_KEYFRAME;
1375 break;
1376 case cktypePALchange:
1377 if (This->sInfo.fccType != streamtypeVIDEO) {
1378 ERR(": found palette change in non-video stream!\n");
1379 return AVIERR_BADFORMAT;
1381 This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
1382 This->sInfo.dwFormatChangeCount++;
1384 if (This->idxFmtChanges == NULL || This->sInfo.dwFormatChangeCount < This->nIdxFmtChanges) {
1385 UINT n = This->sInfo.dwFormatChangeCount;
1387 This->nIdxFmtChanges += 16;
1388 if (This->idxFmtChanges == NULL)
1389 This->idxFmtChanges =
1390 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nIdxFmtChanges * sizeof(AVIINDEXENTRY));
1391 else
1392 This->idxFmtChanges =
1393 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->idxFmtChanges,
1394 This->nIdxFmtChanges * sizeof(AVIINDEXENTRY));
1395 if (This->idxFmtChanges == NULL)
1396 return AVIERR_MEMORY;
1398 This->idxFmtChanges[n].ckid = This->lLastFrame;
1399 This->idxFmtChanges[n].dwFlags = 0;
1400 This->idxFmtChanges[n].dwChunkOffset = offset;
1401 This->idxFmtChanges[n].dwChunkLength = size;
1403 return AVIERR_OK;
1405 break;
1406 case cktypeWAVEbytes:
1407 if (This->paf->fInfo.dwFlags & AVIFILEINFO_TRUSTCKTYPE)
1408 flags |= AVIIF_KEYFRAME;
1409 break;
1410 default:
1411 WARN(": unknown TWOCC 0x%04X found\n", TWOCCFromFOURCC(ckid));
1412 break;
1415 /* first frame is alwasy a keyframe */
1416 if (This->lLastFrame == -1)
1417 flags |= AVIIF_KEYFRAME;
1419 if (This->sInfo.dwSuggestedBufferSize < size)
1420 This->sInfo.dwSuggestedBufferSize = size;
1422 /* get memory for index */
1423 if (This->idxFrames == NULL || This->lLastFrame + 1 >= This->nIdxFrames) {
1424 This->nIdxFrames += 512;
1425 if (This->idxFrames == NULL)
1426 This->idxFrames = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nIdxFrames * sizeof(AVIINDEXENTRY));
1427 else
1428 This->idxFrames = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->idxFrames,
1429 This->nIdxFrames * sizeof(AVIINDEXENTRY));
1430 if (This->idxFrames == NULL)
1431 return AVIERR_MEMORY;
1434 This->lLastFrame++;
1435 This->idxFrames[This->lLastFrame].ckid = ckid;
1436 This->idxFrames[This->lLastFrame].dwFlags = flags;
1437 This->idxFrames[This->lLastFrame].dwChunkOffset = offset;
1438 This->idxFrames[This->lLastFrame].dwChunkLength = size;
1440 /* update AVISTREAMINFO structure if necessary */
1441 if (This->sInfo.dwLength <= This->lLastFrame)
1442 This->sInfo.dwLength = This->lLastFrame + 1;
1444 return AVIERR_OK;
1447 static HRESULT AVIFILE_AddRecord(IAVIFileImpl *This)
1449 /* pre-conditions */
1450 assert(This != NULL && This->ppStreams[0] != NULL);
1452 if (This->idxRecords == NULL || This->cbIdxRecords == 0) {
1453 This->cbIdxRecords += 1024 * sizeof(AVIINDEXENTRY);
1454 This->idxRecords = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->cbIdxRecords);
1455 if (This->idxRecords == NULL)
1456 return AVIERR_MEMORY;
1459 assert(This->nIdxRecords < This->cbIdxRecords/sizeof(AVIINDEXENTRY));
1461 This->idxRecords[This->nIdxRecords].ckid = listtypeAVIRECORD;
1462 This->idxRecords[This->nIdxRecords].dwFlags = AVIIF_LIST;
1463 This->idxRecords[This->nIdxRecords].dwChunkOffset =
1464 This->ckLastRecord.dwDataOffset - 2 * sizeof(DWORD);
1465 This->idxRecords[This->nIdxRecords].dwChunkLength =
1466 This->ckLastRecord.cksize;
1467 This->nIdxRecords++;
1469 return AVIERR_OK;
1472 static DWORD AVIFILE_ComputeMoviStart(IAVIFileImpl *This)
1474 DWORD dwPos;
1475 DWORD nStream;
1477 /* RIFF,hdrl,movi,avih => (3 * 3 + 2) * sizeof(DWORD) = 11 * sizeof(DWORD) */
1478 dwPos = 11 * sizeof(DWORD) + sizeof(MainAVIHeader);
1480 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1481 IAVIStreamImpl *pStream = This->ppStreams[nStream];
1483 /* strl,strh,strf => (3 + 2 * 2) * sizeof(DWORD) = 7 * sizeof(DWORD) */
1484 dwPos += 7 * sizeof(DWORD) + sizeof(AVIStreamHeader);
1485 dwPos += ((pStream->cbFormat + 1) & ~1U);
1486 if (pStream->lpHandlerData != NULL && pStream->cbHandlerData > 0)
1487 dwPos += 2 * sizeof(DWORD) + ((pStream->cbHandlerData + 1) & ~1U);
1488 if (lstrlenW(pStream->sInfo.szName) > 0)
1489 dwPos += 2 * sizeof(DWORD) + ((lstrlenW(pStream->sInfo.szName) + 1) & ~1U);
1492 if (This->dwMoviChunkPos == 0) {
1493 This->dwNextFramePos = dwPos;
1495 /* pad to multiple of AVI_HEADERSIZE only if we are more than 8 bytes away from it */
1496 if (((dwPos + AVI_HEADERSIZE) & ~(AVI_HEADERSIZE - 1)) - dwPos > 2 * sizeof(DWORD))
1497 This->dwNextFramePos = (dwPos + AVI_HEADERSIZE) & ~(AVI_HEADERSIZE - 1);
1499 This->dwMoviChunkPos = This->dwNextFramePos - sizeof(DWORD);
1502 return dwPos;
1505 static void AVIFILE_ConstructAVIStream(IAVIFileImpl *paf, DWORD nr, const AVISTREAMINFOW *asi)
1507 IAVIStreamImpl *pstream;
1509 /* pre-conditions */
1510 assert(paf != NULL);
1511 assert(nr < MAX_AVISTREAMS);
1512 assert(paf->ppStreams[nr] != NULL);
1514 pstream = paf->ppStreams[nr];
1516 pstream->lpVtbl = &iavist;
1517 pstream->ref = 0;
1518 pstream->paf = paf;
1519 pstream->nStream = nr;
1520 pstream->dwCurrentFrame = (DWORD)-1;
1521 pstream->lLastFrame = -1;
1523 if (asi != NULL) {
1524 memcpy(&pstream->sInfo, asi, sizeof(pstream->sInfo));
1526 if (asi->dwLength > 0) {
1527 /* pre-allocate mem for frame-index structure */
1528 pstream->idxFrames =
1529 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asi->dwLength * sizeof(AVIINDEXENTRY));
1530 if (pstream->idxFrames != NULL)
1531 pstream->nIdxFrames = asi->dwLength;
1533 if (asi->dwFormatChangeCount > 0) {
1534 /* pre-allocate mem for formatchange-index structure */
1535 pstream->idxFmtChanges =
1536 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asi->dwFormatChangeCount * sizeof(AVIINDEXENTRY));
1537 if (pstream->idxFmtChanges != NULL)
1538 pstream->nIdxFmtChanges = asi->dwFormatChangeCount;
1541 /* These values will be computed */
1542 pstream->sInfo.dwLength = 0;
1543 pstream->sInfo.dwSuggestedBufferSize = 0;
1544 pstream->sInfo.dwFormatChangeCount = 0;
1545 pstream->sInfo.dwEditCount = 1;
1546 if (pstream->sInfo.dwSampleSize > 0)
1547 SetRectEmpty(&pstream->sInfo.rcFrame);
1550 pstream->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1553 static void AVIFILE_DestructAVIStream(IAVIStreamImpl *This)
1555 /* pre-conditions */
1556 assert(This != NULL);
1558 This->dwCurrentFrame = (DWORD)-1;
1559 This->lLastFrame = -1;
1560 This->paf = NULL;
1561 if (This->idxFrames != NULL) {
1562 HeapFree(GetProcessHeap(), 0, This->idxFrames);
1563 This->idxFrames = NULL;
1564 This->nIdxFrames = 0;
1566 HeapFree(GetProcessHeap(), 0, This->idxFmtChanges);
1567 This->idxFmtChanges = NULL;
1568 if (This->lpBuffer != NULL) {
1569 HeapFree(GetProcessHeap(), 0, This->lpBuffer);
1570 This->lpBuffer = NULL;
1571 This->cbBuffer = 0;
1573 if (This->lpHandlerData != NULL) {
1574 HeapFree(GetProcessHeap(), 0, This->lpHandlerData);
1575 This->lpHandlerData = NULL;
1576 This->cbHandlerData = 0;
1578 if (This->extra.lp != NULL) {
1579 HeapFree(GetProcessHeap(), 0, This->extra.lp);
1580 This->extra.lp = NULL;
1581 This->extra.cb = 0;
1583 if (This->lpFormat != NULL) {
1584 HeapFree(GetProcessHeap(), 0, This->lpFormat);
1585 This->lpFormat = NULL;
1586 This->cbFormat = 0;
1590 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1592 MainAVIHeader MainAVIHdr;
1593 MMCKINFO ckRIFF;
1594 MMCKINFO ckLIST1;
1595 MMCKINFO ckLIST2;
1596 MMCKINFO ck;
1597 IAVIStreamImpl *pStream;
1598 DWORD nStream;
1599 HRESULT hr;
1601 if (This->hmmio == NULL)
1602 return AVIERR_FILEOPEN;
1604 /* initialize stream ptr's */
1605 memset(This->ppStreams, 0, sizeof(This->ppStreams));
1607 /* try to get "RIFF" chunk -- must not be at beginning of file! */
1608 ckRIFF.fccType = formtypeAVI;
1609 if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) {
1610 ERR(": not an AVI!\n");
1611 return AVIERR_FILEREAD;
1614 /* get "LIST" "hdrl" */
1615 ckLIST1.fccType = listtypeAVIHEADER;
1616 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF, MMIO_FINDLIST);
1617 if (FAILED(hr))
1618 return hr;
1620 /* get "avih" chunk */
1621 ck.ckid = ckidAVIMAINHDR;
1622 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, MMIO_FINDCHUNK);
1623 if (FAILED(hr))
1624 return hr;
1626 if (ck.cksize != sizeof(MainAVIHdr)) {
1627 ERR(": invalid size of %d for MainAVIHeader!\n", ck.cksize);
1628 return AVIERR_BADFORMAT;
1630 if (mmioRead(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
1631 return AVIERR_FILEREAD;
1633 /* check for MAX_AVISTREAMS limit */
1634 if (MainAVIHdr.dwStreams > MAX_AVISTREAMS) {
1635 WARN("file contains %u streams, but only supports %d -- change MAX_AVISTREAMS!\n", MainAVIHdr.dwStreams, MAX_AVISTREAMS);
1636 return AVIERR_UNSUPPORTED;
1639 /* adjust permissions if copyrighted material in file */
1640 if (MainAVIHdr.dwFlags & AVIFILEINFO_COPYRIGHTED) {
1641 This->uMode &= ~MMIO_RWMODE;
1642 This->uMode |= MMIO_READ;
1645 /* convert MainAVIHeader into AVIFILINFOW */
1646 memset(&This->fInfo, 0, sizeof(This->fInfo));
1647 This->fInfo.dwRate = MainAVIHdr.dwMicroSecPerFrame;
1648 This->fInfo.dwScale = 1000000;
1649 This->fInfo.dwMaxBytesPerSec = MainAVIHdr.dwMaxBytesPerSec;
1650 This->fInfo.dwFlags = MainAVIHdr.dwFlags;
1651 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
1652 This->fInfo.dwLength = MainAVIHdr.dwTotalFrames;
1653 This->fInfo.dwStreams = MainAVIHdr.dwStreams;
1654 This->fInfo.dwSuggestedBufferSize = MainAVIHdr.dwSuggestedBufferSize;
1655 This->fInfo.dwWidth = MainAVIHdr.dwWidth;
1656 This->fInfo.dwHeight = MainAVIHdr.dwHeight;
1657 LoadStringW(AVIFILE_hModule, IDS_AVIFILETYPE, This->fInfo.szFileType,
1658 sizeof(This->fInfo.szFileType));
1660 /* go back to into header list */
1661 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1662 return AVIERR_FILEREAD;
1664 /* foreach stream exists a "LIST","strl" chunk */
1665 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
1666 /* get next nested chunk in this "LIST","strl" */
1667 if (mmioDescend(This->hmmio, &ckLIST2, &ckLIST1, 0) != S_OK)
1668 return AVIERR_FILEREAD;
1670 /* nested chunk must be of type "LIST","strl" -- when not normally JUNK */
1671 if (ckLIST2.ckid == FOURCC_LIST &&
1672 ckLIST2.fccType == listtypeSTREAMHEADER) {
1673 pStream = This->ppStreams[nStream] =
1674 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIStreamImpl));
1675 if (pStream == NULL)
1676 return AVIERR_MEMORY;
1677 AVIFILE_ConstructAVIStream(This, nStream, NULL);
1679 ck.ckid = 0;
1680 while (mmioDescend(This->hmmio, &ck, &ckLIST2, 0) == S_OK) {
1681 switch (ck.ckid) {
1682 case ckidSTREAMHANDLERDATA:
1683 if (pStream->lpHandlerData != NULL)
1684 return AVIERR_BADFORMAT;
1685 pStream->lpHandlerData = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1686 if (pStream->lpHandlerData == NULL)
1687 return AVIERR_MEMORY;
1688 pStream->cbHandlerData = ck.cksize;
1690 if (mmioRead(This->hmmio, (HPSTR)pStream->lpHandlerData, ck.cksize) != ck.cksize)
1691 return AVIERR_FILEREAD;
1692 break;
1693 case ckidSTREAMFORMAT:
1694 if (pStream->lpFormat != NULL)
1695 return AVIERR_BADFORMAT;
1696 if (ck.cksize == 0)
1697 break;
1699 pStream->lpFormat = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1700 if (pStream->lpFormat == NULL)
1701 return AVIERR_MEMORY;
1702 pStream->cbFormat = ck.cksize;
1704 if (mmioRead(This->hmmio, (HPSTR)pStream->lpFormat, ck.cksize) != ck.cksize)
1705 return AVIERR_FILEREAD;
1707 if (pStream->sInfo.fccType == streamtypeVIDEO) {
1708 LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)pStream->lpFormat;
1710 /* some corrections to the video format */
1711 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
1712 lpbi->biClrUsed = 1u << lpbi->biBitCount;
1713 if (lpbi->biCompression == BI_RGB && lpbi->biSizeImage == 0)
1714 lpbi->biSizeImage = DIBWIDTHBYTES(*lpbi) * lpbi->biHeight;
1715 if (lpbi->biCompression != BI_RGB && lpbi->biBitCount == 8) {
1716 if (pStream->sInfo.fccHandler == mmioFOURCC('R','L','E','0') ||
1717 pStream->sInfo.fccHandler == mmioFOURCC('R','L','E',' '))
1718 lpbi->biCompression = BI_RLE8;
1720 if (lpbi->biCompression == BI_RGB &&
1721 (pStream->sInfo.fccHandler == 0 ||
1722 pStream->sInfo.fccHandler == mmioFOURCC('N','O','N','E')))
1723 pStream->sInfo.fccHandler = comptypeDIB;
1725 /* init rcFrame if it's empty */
1726 if (IsRectEmpty(&pStream->sInfo.rcFrame))
1727 SetRect(&pStream->sInfo.rcFrame, 0, 0, lpbi->biWidth, lpbi->biHeight);
1729 break;
1730 case ckidSTREAMHEADER:
1732 static const WCHAR streamTypeFmt[] = {'%','4','.','4','h','s',0};
1734 AVIStreamHeader streamHdr;
1735 WCHAR szType[25];
1736 WCHAR streamNameFmt[25];
1737 UINT count;
1738 LONG n = ck.cksize;
1740 if (ck.cksize > sizeof(streamHdr))
1741 n = sizeof(streamHdr);
1743 if (mmioRead(This->hmmio, (HPSTR)&streamHdr, n) != n)
1744 return AVIERR_FILEREAD;
1746 pStream->sInfo.fccType = streamHdr.fccType;
1747 pStream->sInfo.fccHandler = streamHdr.fccHandler;
1748 pStream->sInfo.dwFlags = streamHdr.dwFlags;
1749 pStream->sInfo.wPriority = streamHdr.wPriority;
1750 pStream->sInfo.wLanguage = streamHdr.wLanguage;
1751 pStream->sInfo.dwInitialFrames = streamHdr.dwInitialFrames;
1752 pStream->sInfo.dwScale = streamHdr.dwScale;
1753 pStream->sInfo.dwRate = streamHdr.dwRate;
1754 pStream->sInfo.dwStart = streamHdr.dwStart;
1755 pStream->sInfo.dwLength = streamHdr.dwLength;
1756 pStream->sInfo.dwSuggestedBufferSize =
1757 streamHdr.dwSuggestedBufferSize;
1758 pStream->sInfo.dwQuality = streamHdr.dwQuality;
1759 pStream->sInfo.dwSampleSize = streamHdr.dwSampleSize;
1760 pStream->sInfo.rcFrame.left = streamHdr.rcFrame.left;
1761 pStream->sInfo.rcFrame.top = streamHdr.rcFrame.top;
1762 pStream->sInfo.rcFrame.right = streamHdr.rcFrame.right;
1763 pStream->sInfo.rcFrame.bottom = streamHdr.rcFrame.bottom;
1764 pStream->sInfo.dwEditCount = 0;
1765 pStream->sInfo.dwFormatChangeCount = 0;
1767 /* generate description for stream like "filename.avi Type #n" */
1768 if (streamHdr.fccType == streamtypeVIDEO)
1769 LoadStringW(AVIFILE_hModule, IDS_VIDEO, szType, sizeof(szType));
1770 else if (streamHdr.fccType == streamtypeAUDIO)
1771 LoadStringW(AVIFILE_hModule, IDS_AUDIO, szType, sizeof(szType));
1772 else
1773 wsprintfW(szType, streamTypeFmt, (char*)&streamHdr.fccType);
1775 /* get count of this streamtype up to this stream */
1776 count = 0;
1777 for (n = nStream; 0 <= n; n--) {
1778 if (This->ppStreams[n]->sInfo.fccHandler == streamHdr.fccType)
1779 count++;
1782 memset(pStream->sInfo.szName, 0, sizeof(pStream->sInfo.szName));
1784 LoadStringW(AVIFILE_hModule, IDS_AVISTREAMFORMAT, streamNameFmt, sizeof(streamNameFmt));
1786 /* FIXME: avoid overflow -- better use wsnprintfW, which doesn't exists ! */
1787 wsprintfW(pStream->sInfo.szName, streamNameFmt,
1788 AVIFILE_BasenameW(This->szFileName), szType, count);
1790 break;
1791 case ckidSTREAMNAME:
1792 { /* streamname will be saved as ASCII string */
1793 LPSTR str = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1794 if (str == NULL)
1795 return AVIERR_MEMORY;
1797 if (mmioRead(This->hmmio, (HPSTR)str, ck.cksize) != ck.cksize)
1799 HeapFree(GetProcessHeap(), 0, str);
1800 return AVIERR_FILEREAD;
1803 MultiByteToWideChar(CP_ACP, 0, str, -1, pStream->sInfo.szName,
1804 sizeof(pStream->sInfo.szName)/sizeof(pStream->sInfo.szName[0]));
1806 HeapFree(GetProcessHeap(), 0, str);
1808 break;
1809 case ckidAVIPADDING:
1810 case mmioFOURCC('p','a','d','d'):
1811 break;
1812 default:
1813 WARN(": found extra chunk 0x%08X\n", ck.ckid);
1814 hr = ReadChunkIntoExtra(&pStream->extra, This->hmmio, &ck);
1815 if (FAILED(hr))
1816 return hr;
1819 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1820 return AVIERR_FILEREAD;
1822 } else {
1823 /* nested chunks in "LIST","hdrl" which are not of type "LIST","strl" */
1824 hr = ReadChunkIntoExtra(&This->fileextra, This->hmmio, &ckLIST2);
1825 if (FAILED(hr))
1826 return hr;
1828 if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
1829 return AVIERR_FILEREAD;
1832 /* read any extra headers in "LIST","hdrl" */
1833 FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckLIST1, 0);
1834 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
1835 return AVIERR_FILEREAD;
1837 /* search "LIST","movi" chunk in "RIFF","AVI " */
1838 ckLIST1.fccType = listtypeAVIMOVIE;
1839 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ckLIST1, &ckRIFF,
1840 MMIO_FINDLIST);
1841 if (FAILED(hr))
1842 return hr;
1844 This->dwMoviChunkPos = ckLIST1.dwDataOffset;
1845 This->dwIdxChunkPos = ckLIST1.cksize + ckLIST1.dwDataOffset;
1846 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
1847 return AVIERR_FILEREAD;
1849 /* try to find an index */
1850 ck.ckid = ckidAVINEWINDEX;
1851 hr = FindChunkAndKeepExtras(&This->fileextra, This->hmmio,
1852 &ck, &ckRIFF, MMIO_FINDCHUNK);
1853 if (SUCCEEDED(hr) && ck.cksize > 0) {
1854 if (FAILED(AVIFILE_LoadIndex(This, ck.cksize, ckLIST1.dwDataOffset)))
1855 This->fInfo.dwFlags &= ~AVIFILEINFO_HASINDEX;
1858 /* when we haven't found an index or it's bad, then build one
1859 * by parsing 'movi' chunk */
1860 if ((This->fInfo.dwFlags & AVIFILEINFO_HASINDEX) == 0) {
1861 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++)
1862 This->ppStreams[nStream]->lLastFrame = -1;
1864 if (mmioSeek(This->hmmio, ckLIST1.dwDataOffset + sizeof(DWORD), SEEK_SET) == -1) {
1865 ERR(": Oops, can't seek back to 'movi' chunk!\n");
1866 return AVIERR_FILEREAD;
1869 /* seek through the 'movi' list until end */
1870 while (mmioDescend(This->hmmio, &ck, &ckLIST1, 0) == S_OK) {
1871 if (ck.ckid != FOURCC_LIST) {
1872 if (mmioAscend(This->hmmio, &ck, 0) == S_OK) {
1873 nStream = StreamFromFOURCC(ck.ckid);
1875 if (nStream > This->fInfo.dwStreams)
1876 return AVIERR_BADFORMAT;
1878 AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1879 ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1880 } else {
1881 nStream = StreamFromFOURCC(ck.ckid);
1882 WARN(": file seems to be truncated!\n");
1883 if (nStream <= This->fInfo.dwStreams &&
1884 This->ppStreams[nStream]->sInfo.dwSampleSize > 0) {
1885 ck.cksize = mmioSeek(This->hmmio, 0, SEEK_END);
1886 if (ck.cksize != -1) {
1887 ck.cksize -= ck.dwDataOffset;
1888 ck.cksize &= ~(This->ppStreams[nStream]->sInfo.dwSampleSize - 1);
1890 AVIFILE_AddFrame(This->ppStreams[nStream], ck.ckid, ck.cksize,
1891 ck.dwDataOffset - 2 * sizeof(DWORD), 0);
1899 /* find other chunks */
1900 FindChunkAndKeepExtras(&This->fileextra, This->hmmio, &ck, &ckRIFF, 0);
1902 return AVIERR_OK;
1905 static HRESULT AVIFILE_LoadIndex(const IAVIFileImpl *This, DWORD size, DWORD offset)
1907 AVIINDEXENTRY *lp;
1908 DWORD pos, n;
1909 HRESULT hr = AVIERR_OK;
1910 BOOL bAbsolute = TRUE;
1912 lp = HeapAlloc(GetProcessHeap(), 0, IDX_PER_BLOCK * sizeof(AVIINDEXENTRY));
1913 if (lp == NULL)
1914 return AVIERR_MEMORY;
1916 /* adjust limits for index tables, so that inserting will be faster */
1917 for (n = 0; n < This->fInfo.dwStreams; n++) {
1918 IAVIStreamImpl *pStream = This->ppStreams[n];
1920 pStream->lLastFrame = -1;
1922 if (pStream->idxFrames != NULL) {
1923 HeapFree(GetProcessHeap(), 0, pStream->idxFrames);
1924 pStream->idxFrames = NULL;
1925 pStream->nIdxFrames = 0;
1928 if (pStream->sInfo.dwSampleSize != 0) {
1929 if (n > 0 && This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
1930 pStream->nIdxFrames = This->ppStreams[0]->nIdxFrames;
1931 } else if (pStream->sInfo.dwSuggestedBufferSize) {
1932 pStream->nIdxFrames =
1933 pStream->sInfo.dwLength / pStream->sInfo.dwSuggestedBufferSize;
1935 } else
1936 pStream->nIdxFrames = pStream->sInfo.dwLength;
1938 pStream->idxFrames =
1939 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pStream->nIdxFrames * sizeof(AVIINDEXENTRY));
1940 if (pStream->idxFrames == NULL && pStream->nIdxFrames > 0) {
1941 pStream->nIdxFrames = 0;
1942 return AVIERR_MEMORY;
1946 pos = (DWORD)-1;
1947 while (size != 0) {
1948 LONG read = min(IDX_PER_BLOCK * sizeof(AVIINDEXENTRY), size);
1950 if (mmioRead(This->hmmio, (HPSTR)lp, read) != read) {
1951 hr = AVIERR_FILEREAD;
1952 break;
1954 size -= read;
1956 if (pos == (DWORD)-1)
1957 pos = offset - lp->dwChunkOffset + sizeof(DWORD);
1959 AVIFILE_ParseIndex(This, lp, read / sizeof(AVIINDEXENTRY),
1960 pos, &bAbsolute);
1963 HeapFree(GetProcessHeap(), 0, lp);
1965 /* checking ... */
1966 for (n = 0; n < This->fInfo.dwStreams; n++) {
1967 IAVIStreamImpl *pStream = This->ppStreams[n];
1969 if (pStream->sInfo.dwSampleSize == 0 &&
1970 pStream->sInfo.dwLength != pStream->lLastFrame+1)
1971 ERR("stream %u length mismatch: dwLength=%u found=%d\n",
1972 n, pStream->sInfo.dwLength, pStream->lLastFrame);
1975 return hr;
1978 static HRESULT AVIFILE_ParseIndex(const IAVIFileImpl *This, AVIINDEXENTRY *lp,
1979 LONG count, DWORD pos, BOOL *bAbsolute)
1981 if (lp == NULL)
1982 return AVIERR_BADPARAM;
1984 for (; count > 0; count--, lp++) {
1985 WORD nStream = StreamFromFOURCC(lp->ckid);
1987 if (lp->ckid == listtypeAVIRECORD || nStream == 0x7F)
1988 continue; /* skip these */
1990 if (nStream > This->fInfo.dwStreams)
1991 return AVIERR_BADFORMAT;
1993 if (*bAbsolute && lp->dwChunkOffset < This->dwMoviChunkPos)
1994 *bAbsolute = FALSE;
1996 if (*bAbsolute)
1997 lp->dwChunkOffset += sizeof(DWORD);
1998 else
1999 lp->dwChunkOffset += pos;
2001 if (FAILED(AVIFILE_AddFrame(This->ppStreams[nStream], lp->ckid, lp->dwChunkLength, lp->dwChunkOffset, lp->dwFlags)))
2002 return AVIERR_MEMORY;
2005 return AVIERR_OK;
2008 static HRESULT AVIFILE_ReadBlock(IAVIStreamImpl *This, DWORD pos,
2009 LPVOID buffer, LONG size)
2011 /* pre-conditions */
2012 assert(This != NULL);
2013 assert(This->paf != NULL);
2014 assert(This->paf->hmmio != NULL);
2015 assert(This->sInfo.dwStart <= pos && pos < This->sInfo.dwLength);
2016 assert(pos <= This->lLastFrame);
2018 /* should we read as much as block gives us? */
2019 if (size == 0 || size > This->idxFrames[pos].dwChunkLength)
2020 size = This->idxFrames[pos].dwChunkLength;
2022 /* read into out own buffer or given one? */
2023 if (buffer == NULL) {
2024 /* we also read the chunk */
2025 size += 2 * sizeof(DWORD);
2027 /* check that buffer is big enough -- don't trust dwSuggestedBufferSize */
2028 if (This->lpBuffer == NULL || size < This->cbBuffer) {
2029 DWORD maxSize = max(size, This->sInfo.dwSuggestedBufferSize);
2031 if (This->lpBuffer == NULL)
2032 This->lpBuffer = HeapAlloc(GetProcessHeap(), 0, maxSize);
2033 else
2034 This->lpBuffer = HeapReAlloc(GetProcessHeap(), 0, This->lpBuffer, maxSize);
2035 if (This->lpBuffer == NULL)
2036 return AVIERR_MEMORY;
2037 This->cbBuffer = max(size, This->sInfo.dwSuggestedBufferSize);
2040 /* now read the complete chunk into our buffer */
2041 if (mmioSeek(This->paf->hmmio, This->idxFrames[pos].dwChunkOffset, SEEK_SET) == -1)
2042 return AVIERR_FILEREAD;
2043 if (mmioRead(This->paf->hmmio, (HPSTR)This->lpBuffer, size) != size)
2044 return AVIERR_FILEREAD;
2046 /* check if it was the correct block which we have read */
2047 if (This->lpBuffer[0] != This->idxFrames[pos].ckid ||
2048 This->lpBuffer[1] != This->idxFrames[pos].dwChunkLength) {
2049 ERR(": block %d not found at 0x%08X\n", pos, This->idxFrames[pos].dwChunkOffset);
2050 ERR(": Index says: '%4.4s'(0x%08X) size 0x%08X\n",
2051 (char*)&This->idxFrames[pos].ckid, This->idxFrames[pos].ckid,
2052 This->idxFrames[pos].dwChunkLength);
2053 ERR(": Data says: '%4.4s'(0x%08X) size 0x%08X\n",
2054 (char*)&This->lpBuffer[0], This->lpBuffer[0], This->lpBuffer[1]);
2055 return AVIERR_FILEREAD;
2057 } else {
2058 if (mmioSeek(This->paf->hmmio, This->idxFrames[pos].dwChunkOffset + 2 * sizeof(DWORD), SEEK_SET) == -1)
2059 return AVIERR_FILEREAD;
2060 if (mmioRead(This->paf->hmmio, (HPSTR)buffer, size) != size)
2061 return AVIERR_FILEREAD;
2064 return AVIERR_OK;
2067 static void AVIFILE_SamplesToBlock(const IAVIStreamImpl *This, LPLONG pos, LPLONG offset)
2069 LONG block;
2071 /* pre-conditions */
2072 assert(This != NULL);
2073 assert(pos != NULL);
2074 assert(offset != NULL);
2075 assert(This->sInfo.dwSampleSize != 0);
2076 assert(*pos >= This->sInfo.dwStart);
2078 /* convert start sample to start bytes */
2079 (*offset) = (*pos) - This->sInfo.dwStart;
2080 (*offset) *= This->sInfo.dwSampleSize;
2082 /* convert bytes to block number */
2083 for (block = 0; block <= This->lLastFrame; block++) {
2084 if (This->idxFrames[block].dwChunkLength <= *offset)
2085 (*offset) -= This->idxFrames[block].dwChunkLength;
2086 else
2087 break;
2090 *pos = block;
2093 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
2095 MainAVIHeader MainAVIHdr;
2096 IAVIStreamImpl* pStream;
2097 MMCKINFO ckRIFF;
2098 MMCKINFO ckLIST1;
2099 MMCKINFO ckLIST2;
2100 MMCKINFO ck;
2101 DWORD nStream;
2102 DWORD dwPos;
2103 HRESULT hr;
2105 /* initialize some things */
2106 if (This->dwMoviChunkPos == 0)
2107 AVIFILE_ComputeMoviStart(This);
2109 /* written one record to much? */
2110 if (This->ckLastRecord.dwFlags & MMIO_DIRTY) {
2111 This->dwNextFramePos -= 3 * sizeof(DWORD);
2112 if (This->nIdxRecords > 0)
2113 This->nIdxRecords--;
2116 AVIFILE_UpdateInfo(This);
2118 assert(This->fInfo.dwScale != 0);
2120 memset(&MainAVIHdr, 0, sizeof(MainAVIHdr));
2121 MainAVIHdr.dwMicroSecPerFrame = MulDiv(This->fInfo.dwRate, 1000000,
2122 This->fInfo.dwScale);
2123 MainAVIHdr.dwMaxBytesPerSec = This->fInfo.dwMaxBytesPerSec;
2124 MainAVIHdr.dwPaddingGranularity = AVI_HEADERSIZE;
2125 MainAVIHdr.dwFlags = This->fInfo.dwFlags;
2126 MainAVIHdr.dwTotalFrames = This->fInfo.dwLength;
2127 MainAVIHdr.dwInitialFrames = 0;
2128 MainAVIHdr.dwStreams = This->fInfo.dwStreams;
2129 MainAVIHdr.dwSuggestedBufferSize = This->fInfo.dwSuggestedBufferSize;
2130 MainAVIHdr.dwWidth = This->fInfo.dwWidth;
2131 MainAVIHdr.dwHeight = This->fInfo.dwHeight;
2132 MainAVIHdr.dwInitialFrames = This->dwInitialFrames;
2134 /* now begin writing ... */
2135 mmioSeek(This->hmmio, 0, SEEK_SET);
2137 /* RIFF chunk */
2138 ckRIFF.cksize = 0;
2139 ckRIFF.fccType = formtypeAVI;
2140 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
2141 return AVIERR_FILEWRITE;
2143 /* AVI headerlist */
2144 ckLIST1.cksize = 0;
2145 ckLIST1.fccType = listtypeAVIHEADER;
2146 if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2147 return AVIERR_FILEWRITE;
2149 /* MainAVIHeader */
2150 ck.ckid = ckidAVIMAINHDR;
2151 ck.cksize = sizeof(MainAVIHdr);
2152 ck.fccType = 0;
2153 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2154 return AVIERR_FILEWRITE;
2155 if (mmioWrite(This->hmmio, (HPSTR)&MainAVIHdr, ck.cksize) != ck.cksize)
2156 return AVIERR_FILEWRITE;
2157 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2158 return AVIERR_FILEWRITE;
2160 /* write the headers of each stream into a separate streamheader list */
2161 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2162 AVIStreamHeader strHdr;
2164 pStream = This->ppStreams[nStream];
2166 /* begin the new streamheader list */
2167 ckLIST2.cksize = 0;
2168 ckLIST2.fccType = listtypeSTREAMHEADER;
2169 if (mmioCreateChunk(This->hmmio, &ckLIST2, MMIO_CREATELIST) != S_OK)
2170 return AVIERR_FILEWRITE;
2172 /* create an AVIStreamHeader from the AVSTREAMINFO */
2173 strHdr.fccType = pStream->sInfo.fccType;
2174 strHdr.fccHandler = pStream->sInfo.fccHandler;
2175 strHdr.dwFlags = pStream->sInfo.dwFlags;
2176 strHdr.wPriority = pStream->sInfo.wPriority;
2177 strHdr.wLanguage = pStream->sInfo.wLanguage;
2178 strHdr.dwInitialFrames = pStream->sInfo.dwInitialFrames;
2179 strHdr.dwScale = pStream->sInfo.dwScale;
2180 strHdr.dwRate = pStream->sInfo.dwRate;
2181 strHdr.dwStart = pStream->sInfo.dwStart;
2182 strHdr.dwLength = pStream->sInfo.dwLength;
2183 strHdr.dwSuggestedBufferSize = pStream->sInfo.dwSuggestedBufferSize;
2184 strHdr.dwQuality = pStream->sInfo.dwQuality;
2185 strHdr.dwSampleSize = pStream->sInfo.dwSampleSize;
2186 strHdr.rcFrame.left = pStream->sInfo.rcFrame.left;
2187 strHdr.rcFrame.top = pStream->sInfo.rcFrame.top;
2188 strHdr.rcFrame.right = pStream->sInfo.rcFrame.right;
2189 strHdr.rcFrame.bottom = pStream->sInfo.rcFrame.bottom;
2191 /* now write the AVIStreamHeader */
2192 ck.ckid = ckidSTREAMHEADER;
2193 ck.cksize = sizeof(strHdr);
2194 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2195 return AVIERR_FILEWRITE;
2196 if (mmioWrite(This->hmmio, (HPSTR)&strHdr, ck.cksize) != ck.cksize)
2197 return AVIERR_FILEWRITE;
2198 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2199 return AVIERR_FILEWRITE;
2201 /* ... the hopefully ever present streamformat ... */
2202 ck.ckid = ckidSTREAMFORMAT;
2203 ck.cksize = pStream->cbFormat;
2204 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2205 return AVIERR_FILEWRITE;
2206 if (pStream->lpFormat != NULL && ck.cksize > 0) {
2207 if (mmioWrite(This->hmmio, (HPSTR)pStream->lpFormat, ck.cksize) != ck.cksize)
2208 return AVIERR_FILEWRITE;
2210 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2211 return AVIERR_FILEWRITE;
2213 /* ... some optional existing handler data ... */
2214 if (pStream->lpHandlerData != NULL && pStream->cbHandlerData > 0) {
2215 ck.ckid = ckidSTREAMHANDLERDATA;
2216 ck.cksize = pStream->cbHandlerData;
2217 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2218 return AVIERR_FILEWRITE;
2219 if (mmioWrite(This->hmmio, (HPSTR)pStream->lpHandlerData, ck.cksize) != ck.cksize)
2220 return AVIERR_FILEWRITE;
2221 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2222 return AVIERR_FILEWRITE;
2225 /* ... some optional additional extra chunk for this stream ... */
2226 if (pStream->extra.lp != NULL && pStream->extra.cb > 0) {
2227 /* the chunk header(s) are already in the strucuture */
2228 if (mmioWrite(This->hmmio, (HPSTR)pStream->extra.lp, pStream->extra.cb) != pStream->extra.cb)
2229 return AVIERR_FILEWRITE;
2232 /* ... an optional name for this stream ... */
2233 if (lstrlenW(pStream->sInfo.szName) > 0) {
2234 LPSTR str;
2236 ck.ckid = ckidSTREAMNAME;
2237 ck.cksize = lstrlenW(pStream->sInfo.szName) + 1;
2238 if (ck.cksize & 1) /* align */
2239 ck.cksize++;
2240 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2241 return AVIERR_FILEWRITE;
2243 /* the streamname must be saved in ASCII not Unicode */
2244 str = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
2245 if (str == NULL)
2246 return AVIERR_MEMORY;
2247 WideCharToMultiByte(CP_ACP, 0, pStream->sInfo.szName, -1, str,
2248 ck.cksize, NULL, NULL);
2250 if (mmioWrite(This->hmmio, (HPSTR)str, ck.cksize) != ck.cksize) {
2251 HeapFree(GetProcessHeap(), 0, str);
2252 return AVIERR_FILEWRITE;
2255 HeapFree(GetProcessHeap(), 0, str);
2256 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2257 return AVIERR_FILEWRITE;
2260 /* close streamheader list for this stream */
2261 if (mmioAscend(This->hmmio, &ckLIST2, 0) != S_OK)
2262 return AVIERR_FILEWRITE;
2263 } /* for (0 <= nStream < MainAVIHdr.dwStreams) */
2265 /* close the aviheader list */
2266 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2267 return AVIERR_FILEWRITE;
2269 /* check for padding to pre-guessed 'movi'-chunk position */
2270 dwPos = ckLIST1.dwDataOffset + ckLIST1.cksize;
2271 if (This->dwMoviChunkPos - 2 * sizeof(DWORD) > dwPos) {
2272 ck.ckid = ckidAVIPADDING;
2273 ck.cksize = This->dwMoviChunkPos - dwPos - 4 * sizeof(DWORD);
2274 assert((LONG)ck.cksize >= 0);
2276 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2277 return AVIERR_FILEWRITE;
2278 if (mmioSeek(This->hmmio, ck.cksize, SEEK_CUR) == -1)
2279 return AVIERR_FILEWRITE;
2280 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2281 return AVIERR_FILEWRITE;
2284 /* now write the 'movi' chunk */
2285 mmioSeek(This->hmmio, This->dwMoviChunkPos - 2 * sizeof(DWORD), SEEK_SET);
2286 ckLIST1.cksize = 0;
2287 ckLIST1.fccType = listtypeAVIMOVIE;
2288 if (mmioCreateChunk(This->hmmio, &ckLIST1, MMIO_CREATELIST) != S_OK)
2289 return AVIERR_FILEWRITE;
2290 if (mmioSeek(This->hmmio, This->dwNextFramePos, SEEK_SET) == -1)
2291 return AVIERR_FILEWRITE;
2292 if (mmioAscend(This->hmmio, &ckLIST1, 0) != S_OK)
2293 return AVIERR_FILEWRITE;
2295 /* write 'idx1' chunk */
2296 hr = AVIFILE_SaveIndex(This);
2297 if (FAILED(hr))
2298 return hr;
2300 /* write optional extra file chunks */
2301 if (This->fileextra.lp != NULL && This->fileextra.cb > 0) {
2302 /* as for the streams, are the chunk header(s) in the structure */
2303 if (mmioWrite(This->hmmio, (HPSTR)This->fileextra.lp, This->fileextra.cb) != This->fileextra.cb)
2304 return AVIERR_FILEWRITE;
2307 /* close RIFF chunk */
2308 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
2309 return AVIERR_FILEWRITE;
2311 /* add some JUNK at end for bad parsers */
2312 memset(&ckRIFF, 0, sizeof(ckRIFF));
2313 mmioWrite(This->hmmio, (HPSTR)&ckRIFF, sizeof(ckRIFF));
2314 mmioFlush(This->hmmio, 0);
2316 return AVIERR_OK;
2319 static HRESULT AVIFILE_SaveIndex(const IAVIFileImpl *This)
2321 IAVIStreamImpl *pStream;
2322 AVIINDEXENTRY idx;
2323 MMCKINFO ck;
2324 DWORD nStream;
2325 LONG n;
2327 ck.ckid = ckidAVINEWINDEX;
2328 ck.cksize = 0;
2329 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
2330 return AVIERR_FILEWRITE;
2332 if (This->fInfo.dwFlags & AVIFILEINFO_ISINTERLEAVED) {
2333 /* is interleaved -- write block of coresponding frames */
2334 LONG lInitialFrames = 0;
2335 LONG stepsize;
2336 LONG i;
2338 if (This->ppStreams[0]->sInfo.dwSampleSize == 0)
2339 stepsize = 1;
2340 else
2341 stepsize = AVIStreamTimeToSample((PAVISTREAM)This->ppStreams[0], 1000000);
2343 assert(stepsize > 0);
2345 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2346 if (lInitialFrames < This->ppStreams[nStream]->sInfo.dwInitialFrames)
2347 lInitialFrames = This->ppStreams[nStream]->sInfo.dwInitialFrames;
2350 for (i = -lInitialFrames; i < (LONG)This->fInfo.dwLength - lInitialFrames;
2351 i += stepsize) {
2352 DWORD nFrame = lInitialFrames + i;
2354 assert(nFrame < This->nIdxRecords);
2356 idx.ckid = listtypeAVIRECORD;
2357 idx.dwFlags = AVIIF_LIST;
2358 idx.dwChunkLength = This->idxRecords[nFrame].dwChunkLength;
2359 idx.dwChunkOffset = This->idxRecords[nFrame].dwChunkOffset
2360 - This->dwMoviChunkPos;
2361 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2362 return AVIERR_FILEWRITE;
2364 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2365 pStream = This->ppStreams[nStream];
2367 /* heave we reached start of this stream? */
2368 if (-(LONG)pStream->sInfo.dwInitialFrames > i)
2369 continue;
2371 if (pStream->sInfo.dwInitialFrames < lInitialFrames)
2372 nFrame -= (lInitialFrames - pStream->sInfo.dwInitialFrames);
2374 /* reached end of this stream? */
2375 if (pStream->lLastFrame <= nFrame)
2376 continue;
2378 if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2379 pStream->sInfo.dwFormatChangeCount != 0 &&
2380 pStream->idxFmtChanges != NULL) {
2381 DWORD pos;
2383 for (pos = 0; pos < pStream->sInfo.dwFormatChangeCount; pos++) {
2384 if (pStream->idxFmtChanges[pos].ckid == nFrame) {
2385 idx.dwFlags = AVIIF_NOTIME;
2386 idx.ckid = MAKEAVICKID(cktypePALchange, pStream->nStream);
2387 idx.dwChunkLength = pStream->idxFmtChanges[pos].dwChunkLength;
2388 idx.dwChunkOffset = pStream->idxFmtChanges[pos].dwChunkOffset
2389 - This->dwMoviChunkPos;
2391 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2392 return AVIERR_FILEWRITE;
2393 break;
2396 } /* if have formatchanges */
2398 idx.ckid = pStream->idxFrames[nFrame].ckid;
2399 idx.dwFlags = pStream->idxFrames[nFrame].dwFlags;
2400 idx.dwChunkLength = pStream->idxFrames[nFrame].dwChunkLength;
2401 idx.dwChunkOffset = pStream->idxFrames[nFrame].dwChunkOffset
2402 - This->dwMoviChunkPos;
2403 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2404 return AVIERR_FILEWRITE;
2407 } else {
2408 /* not interleaved -- write index for each stream at once */
2409 for (nStream = 0; nStream < This->fInfo.dwStreams; nStream++) {
2410 pStream = This->ppStreams[nStream];
2412 for (n = 0; n <= pStream->lLastFrame; n++) {
2413 if ((pStream->sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES) &&
2414 (pStream->sInfo.dwFormatChangeCount != 0)) {
2415 DWORD pos;
2417 for (pos = 0; pos < pStream->sInfo.dwFormatChangeCount; pos++) {
2418 if (pStream->idxFmtChanges[pos].ckid == n) {
2419 idx.dwFlags = AVIIF_NOTIME;
2420 idx.ckid = MAKEAVICKID(cktypePALchange, pStream->nStream);
2421 idx.dwChunkLength = pStream->idxFmtChanges[pos].dwChunkLength;
2422 idx.dwChunkOffset =
2423 pStream->idxFmtChanges[pos].dwChunkOffset - This->dwMoviChunkPos;
2424 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2425 return AVIERR_FILEWRITE;
2426 break;
2429 } /* if have formatchanges */
2431 idx.ckid = pStream->idxFrames[n].ckid;
2432 idx.dwFlags = pStream->idxFrames[n].dwFlags;
2433 idx.dwChunkLength = pStream->idxFrames[n].dwChunkLength;
2434 idx.dwChunkOffset = pStream->idxFrames[n].dwChunkOffset
2435 - This->dwMoviChunkPos;
2437 if (mmioWrite(This->hmmio, (HPSTR)&idx, sizeof(idx)) != sizeof(idx))
2438 return AVIERR_FILEWRITE;
2441 } /* if not interleaved */
2443 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
2444 return AVIERR_FILEWRITE;
2446 return AVIERR_OK;
2449 static ULONG AVIFILE_SearchStream(const IAVIFileImpl *This, DWORD fcc, LONG lSkip)
2451 UINT i;
2452 UINT nStream;
2454 /* pre-condition */
2455 assert(lSkip >= 0);
2457 if (fcc != 0) {
2458 /* search the number of the specified stream */
2459 nStream = (ULONG)-1;
2460 for (i = 0; i < This->fInfo.dwStreams; i++) {
2461 assert(This->ppStreams[i] != NULL);
2463 if (This->ppStreams[i]->sInfo.fccType == fcc) {
2464 if (lSkip == 0) {
2465 nStream = i;
2466 break;
2467 } else
2468 lSkip--;
2471 } else
2472 nStream = lSkip;
2474 return nStream;
2477 static void AVIFILE_UpdateInfo(IAVIFileImpl *This)
2479 UINT i;
2481 /* pre-conditions */
2482 assert(This != NULL);
2484 This->fInfo.dwMaxBytesPerSec = 0;
2485 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
2486 This->fInfo.dwSuggestedBufferSize = 0;
2487 This->fInfo.dwWidth = 0;
2488 This->fInfo.dwHeight = 0;
2489 This->fInfo.dwScale = 0;
2490 This->fInfo.dwRate = 0;
2491 This->fInfo.dwLength = 0;
2492 This->dwInitialFrames = 0;
2494 for (i = 0; i < This->fInfo.dwStreams; i++) {
2495 AVISTREAMINFOW *psi;
2496 DWORD n;
2498 /* pre-conditions */
2499 assert(This->ppStreams[i] != NULL);
2501 psi = &This->ppStreams[i]->sInfo;
2502 assert(psi->dwScale != 0);
2503 assert(psi->dwRate != 0);
2505 if (i == 0) {
2506 /* use first stream timings as base */
2507 This->fInfo.dwScale = psi->dwScale;
2508 This->fInfo.dwRate = psi->dwRate;
2509 This->fInfo.dwLength = psi->dwLength;
2510 } else {
2511 n = AVIStreamSampleToSample((PAVISTREAM)This->ppStreams[0],
2512 (PAVISTREAM)This->ppStreams[i],psi->dwLength);
2513 if (This->fInfo.dwLength < n)
2514 This->fInfo.dwLength = n;
2517 if (This->dwInitialFrames < psi->dwInitialFrames)
2518 This->dwInitialFrames = psi->dwInitialFrames;
2520 if (This->fInfo.dwSuggestedBufferSize < psi->dwSuggestedBufferSize)
2521 This->fInfo.dwSuggestedBufferSize = psi->dwSuggestedBufferSize;
2523 if (psi->dwSampleSize != 0) {
2524 /* fixed sample size -- exact computation */
2525 This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSampleSize, psi->dwRate,
2526 psi->dwScale);
2527 } else {
2528 /* variable sample size -- only upper limit */
2529 This->fInfo.dwMaxBytesPerSec += MulDiv(psi->dwSuggestedBufferSize,
2530 psi->dwRate, psi->dwScale);
2532 /* update dimensions */
2533 n = psi->rcFrame.right - psi->rcFrame.left;
2534 if (This->fInfo.dwWidth < n)
2535 This->fInfo.dwWidth = n;
2536 n = psi->rcFrame.bottom - psi->rcFrame.top;
2537 if (This->fInfo.dwHeight < n)
2538 This->fInfo.dwHeight = n;
2543 static HRESULT AVIFILE_WriteBlock(IAVIStreamImpl *This, DWORD block,
2544 FOURCC ckid, DWORD flags, LPVOID buffer,
2545 LONG size)
2547 MMCKINFO ck;
2549 ck.ckid = ckid;
2550 ck.cksize = size;
2551 ck.fccType = 0;
2553 /* if no frame/block is already written, we must compute start of movi chunk */
2554 if (This->paf->dwMoviChunkPos == 0)
2555 AVIFILE_ComputeMoviStart(This->paf);
2557 if (mmioSeek(This->paf->hmmio, This->paf->dwNextFramePos, SEEK_SET) == -1)
2558 return AVIERR_FILEWRITE;
2560 if (mmioCreateChunk(This->paf->hmmio, &ck, 0) != S_OK)
2561 return AVIERR_FILEWRITE;
2562 if (buffer != NULL && size > 0) {
2563 if (mmioWrite(This->paf->hmmio, (HPSTR)buffer, size) != size)
2564 return AVIERR_FILEWRITE;
2566 if (mmioAscend(This->paf->hmmio, &ck, 0) != S_OK)
2567 return AVIERR_FILEWRITE;
2569 This->paf->fDirty = TRUE;
2570 This->paf->dwNextFramePos = mmioSeek(This->paf->hmmio, 0, SEEK_CUR);
2572 return AVIFILE_AddFrame(This, ckid, size,
2573 ck.dwDataOffset - 2 * sizeof(DWORD), flags);