2 * Copyright 1999 Marcus Meissner
3 * Copyright 2002-2003 Michael Günnewig
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * - IAVIStreaming interface is missing for the IAVIStreamImpl
22 * - IAVIStream_fnFindSample: FIND_INDEX isn't supported.
23 * - IAVIStream_fnReadFormat: formatchanges aren't read in.
24 * - IAVIStream_fnDelete: a stub.
25 * - IAVIStream_fnSetInfo: a stub.
29 * - native version can hangup when reading a file generated with this DLL.
30 * When index is missing it works, but index seems to be okay.
33 #define COM_NO_WINDOWS_H
46 #include "avifile_private.h"
47 #include "extrachunk.h"
49 #include "wine/unicode.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
55 #define IDX_PER_BLOCK 2730
58 /***********************************************************************/
60 static HRESULT WINAPI
IAVIFile_fnQueryInterface(IAVIFile
* iface
,REFIID refiid
,LPVOID
*obj
);
61 static ULONG WINAPI
IAVIFile_fnAddRef(IAVIFile
* iface
);
62 static ULONG WINAPI
IAVIFile_fnRelease(IAVIFile
* iface
);
63 static HRESULT WINAPI
IAVIFile_fnInfo(IAVIFile
*iface
,AVIFILEINFOW
*afi
,LONG size
);
64 static HRESULT WINAPI
IAVIFile_fnGetStream(IAVIFile
*iface
,PAVISTREAM
*avis
,DWORD fccType
,LONG lParam
);
65 static HRESULT WINAPI
IAVIFile_fnCreateStream(IAVIFile
*iface
,PAVISTREAM
*avis
,AVISTREAMINFOW
*asi
);
66 static HRESULT WINAPI
IAVIFile_fnWriteData(IAVIFile
*iface
,DWORD ckid
,LPVOID lpData
,LONG size
);
67 static HRESULT WINAPI
IAVIFile_fnReadData(IAVIFile
*iface
,DWORD ckid
,LPVOID lpData
,LONG
*size
);
68 static HRESULT WINAPI
IAVIFile_fnEndRecord(IAVIFile
*iface
);
69 static HRESULT WINAPI
IAVIFile_fnDeleteStream(IAVIFile
*iface
,DWORD fccType
,LONG lParam
);
71 static const struct IAVIFileVtbl iavift
= {
72 IAVIFile_fnQueryInterface
,
77 IAVIFile_fnCreateStream
,
81 IAVIFile_fnDeleteStream
84 static HRESULT WINAPI
IPersistFile_fnQueryInterface(IPersistFile
*iface
,REFIID refiid
,LPVOID
*obj
);
85 static ULONG WINAPI
IPersistFile_fnAddRef(IPersistFile
*iface
);
86 static ULONG WINAPI
IPersistFile_fnRelease(IPersistFile
*iface
);
87 static HRESULT WINAPI
IPersistFile_fnGetClassID(IPersistFile
*iface
,CLSID
*pClassID
);
88 static HRESULT WINAPI
IPersistFile_fnIsDirty(IPersistFile
*iface
);
89 static HRESULT WINAPI
IPersistFile_fnLoad(IPersistFile
*iface
,LPCOLESTR pszFileName
,DWORD dwMode
);
90 static HRESULT WINAPI
IPersistFile_fnSave(IPersistFile
*iface
,LPCOLESTR pszFileName
,BOOL fRemember
);
91 static HRESULT WINAPI
IPersistFile_fnSaveCompleted(IPersistFile
*iface
,LPCOLESTR pszFileName
);
92 static HRESULT WINAPI
IPersistFile_fnGetCurFile(IPersistFile
*iface
,LPOLESTR
*ppszFileName
);
94 static const struct IPersistFileVtbl ipersistft
= {
95 IPersistFile_fnQueryInterface
,
96 IPersistFile_fnAddRef
,
97 IPersistFile_fnRelease
,
98 IPersistFile_fnGetClassID
,
99 IPersistFile_fnIsDirty
,
102 IPersistFile_fnSaveCompleted
,
103 IPersistFile_fnGetCurFile
106 static HRESULT WINAPI
IAVIStream_fnQueryInterface(IAVIStream
*iface
,REFIID refiid
,LPVOID
*obj
);
107 static ULONG WINAPI
IAVIStream_fnAddRef(IAVIStream
*iface
);
108 static ULONG WINAPI
IAVIStream_fnRelease(IAVIStream
* iface
);
109 static HRESULT WINAPI
IAVIStream_fnCreate(IAVIStream
*iface
,LPARAM lParam1
,LPARAM lParam2
);
110 static HRESULT WINAPI
IAVIStream_fnInfo(IAVIStream
*iface
,AVISTREAMINFOW
*psi
,LONG size
);
111 static LONG WINAPI
IAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,LONG flags
);
112 static HRESULT WINAPI
IAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG
*formatsize
);
113 static HRESULT WINAPI
IAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG formatsize
);
114 static HRESULT WINAPI
IAVIStream_fnRead(IAVIStream
*iface
,LONG start
,LONG samples
,LPVOID buffer
,LONG buffersize
,LONG
*bytesread
,LONG
*samplesread
);
115 static HRESULT WINAPI
IAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,LONG samples
,LPVOID buffer
,LONG buffersize
,DWORD flags
,LONG
*sampwritten
,LONG
*byteswritten
);
116 static HRESULT WINAPI
IAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,LONG samples
);
117 static HRESULT WINAPI
IAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,LPVOID lp
,LONG
*lpread
);
118 static HRESULT WINAPI
IAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,LPVOID lp
,LONG size
);
119 static HRESULT WINAPI
IAVIStream_fnSetInfo(IAVIStream
*iface
,AVISTREAMINFOW
*info
,LONG infolen
);
121 static const struct IAVIStreamVtbl iavist
= {
122 IAVIStream_fnQueryInterface
,
124 IAVIStream_fnRelease
,
127 IAVIStream_fnFindSample
,
128 IAVIStream_fnReadFormat
,
129 IAVIStream_fnSetFormat
,
133 IAVIStream_fnReadData
,
134 IAVIStream_fnWriteData
,
138 typedef struct _IAVIFileImpl IAVIFileImpl
;
140 typedef struct _IPersistFileImpl
{
142 const IPersistFileVtbl
*lpVtbl
;
144 /* IPersistFile stuff */
148 typedef struct _IAVIStreamImpl
{
150 const IAVIStreamVtbl
*lpVtbl
;
153 /* IAVIStream stuff */
155 DWORD nStream
; /* the n-th stream in file */
156 AVISTREAMINFOW sInfo
;
161 LPVOID lpHandlerData
;
167 DWORD cbBuffer
; /* size of lpBuffer */
168 DWORD dwCurrentFrame
; /* frame/block currently in lpBuffer */
170 LONG lLastFrame
; /* last correct index in idxFrames */
171 AVIINDEXENTRY
*idxFrames
;
172 DWORD nIdxFrames
; /* upper index limit of idxFrames */
173 AVIINDEXENTRY
*idxFmtChanges
;
174 DWORD nIdxFmtChanges
; /* upper index limit of idxFmtChanges */
177 struct _IAVIFileImpl
{
179 const IAVIFileVtbl
*lpVtbl
;
182 /* IAVIFile stuff... */
183 IPersistFileImpl iPersistFile
;
186 IAVIStreamImpl
*ppStreams
[MAX_AVISTREAMS
];
188 EXTRACHUNKS fileextra
;
190 DWORD dwMoviChunkPos
; /* some stuff for saving ... */
192 DWORD dwNextFramePos
;
193 DWORD dwInitialFrames
;
195 MMCKINFO ckLastRecord
;
196 AVIINDEXENTRY
*idxRecords
; /* won't be updated while loading */
197 DWORD nIdxRecords
; /* current fill level */
198 DWORD cbIdxRecords
; /* size of idxRecords */
200 /* IPersistFile stuff ... */
207 /***********************************************************************/
209 static HRESULT
AVIFILE_AddFrame(IAVIStreamImpl
*This
, DWORD ckid
, DWORD size
,
210 DWORD offset
, DWORD flags
);
211 static HRESULT
AVIFILE_AddRecord(IAVIFileImpl
*This
);
212 static DWORD
AVIFILE_ComputeMoviStart(IAVIFileImpl
*This
);
213 static void AVIFILE_ConstructAVIStream(IAVIFileImpl
*paf
, DWORD nr
,
214 LPAVISTREAMINFOW asi
);
215 static void AVIFILE_DestructAVIStream(IAVIStreamImpl
*This
);
216 static HRESULT
AVIFILE_LoadFile(IAVIFileImpl
*This
);
217 static HRESULT
AVIFILE_LoadIndex(IAVIFileImpl
*This
, DWORD size
, DWORD offset
);
218 static HRESULT
AVIFILE_ParseIndex(IAVIFileImpl
*This
, AVIINDEXENTRY
*lp
,
219 LONG count
, DWORD pos
, BOOL
*bAbsolute
);
220 static HRESULT
AVIFILE_ReadBlock(IAVIStreamImpl
*This
, DWORD start
,
221 LPVOID buffer
, LONG size
);
222 static void AVIFILE_SamplesToBlock(IAVIStreamImpl
*This
, LPLONG pos
,
224 static HRESULT
AVIFILE_SaveFile(IAVIFileImpl
*This
);
225 static HRESULT
AVIFILE_SaveIndex(IAVIFileImpl
*This
);
226 static ULONG
AVIFILE_SearchStream(IAVIFileImpl
*This
, DWORD fccType
,
228 static void AVIFILE_UpdateInfo(IAVIFileImpl
*This
);
229 static HRESULT
AVIFILE_WriteBlock(IAVIStreamImpl
*This
, DWORD block
,
230 FOURCC ckid
, DWORD flags
, LPVOID buffer
,
233 HRESULT
AVIFILE_CreateAVIFile(REFIID riid
, LPVOID
*ppv
)
238 assert(riid
!= NULL
&& ppv
!= NULL
);
242 pfile
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAVIFileImpl
));
244 return AVIERR_MEMORY
;
246 pfile
->lpVtbl
= &iavift
;
248 pfile
->iPersistFile
.lpVtbl
= &ipersistft
;
249 pfile
->iPersistFile
.paf
= pfile
;
251 hr
= IAVIFile_QueryInterface((IAVIFile
*)pfile
, riid
, ppv
);
253 HeapFree(GetProcessHeap(), 0, pfile
);
258 static HRESULT WINAPI
IAVIFile_fnQueryInterface(IAVIFile
*iface
, REFIID refiid
,
261 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
263 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
265 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
266 IsEqualGUID(&IID_IAVIFile
, refiid
)) {
268 IAVIFile_AddRef(iface
);
271 } else if (IsEqualGUID(&IID_IPersistFile
, refiid
)) {
272 *obj
= &This
->iPersistFile
;
273 IAVIFile_AddRef(iface
);
278 return OLE_E_ENUM_NOMORE
;
281 static ULONG WINAPI
IAVIFile_fnAddRef(IAVIFile
*iface
)
283 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
284 ULONG ref
= InterlockedIncrement(&This
->ref
);
286 TRACE("(%p) -> %ld\n", iface
, ref
);
291 static ULONG WINAPI
IAVIFile_fnRelease(IAVIFile
*iface
)
293 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
295 ULONG ref
= InterlockedDecrement(&This
->ref
);
297 TRACE("(%p) -> %ld\n", iface
, ref
);
301 /* need to write headers to file */
302 AVIFILE_SaveFile(This
);
305 for (i
= 0; i
< This
->fInfo
.dwStreams
; i
++) {
306 if (This
->ppStreams
[i
] != NULL
) {
307 if (This
->ppStreams
[i
]->ref
!= 0) {
308 ERR(": someone has still %lu reference to stream %u (%p)!\n",
309 This
->ppStreams
[i
]->ref
, i
, This
->ppStreams
[i
]);
311 AVIFILE_DestructAVIStream(This
->ppStreams
[i
]);
312 HeapFree(GetProcessHeap(), 0, This
->ppStreams
[i
]);
313 This
->ppStreams
[i
] = NULL
;
317 if (This
->idxRecords
!= NULL
) {
318 HeapFree(GetProcessHeap(), 0, This
->idxRecords
);
319 This
->idxRecords
= NULL
;
320 This
->nIdxRecords
= 0;
323 if (This
->fileextra
.lp
!= NULL
) {
324 HeapFree(GetProcessHeap(), 0, This
->fileextra
.lp
);
325 This
->fileextra
.lp
= NULL
;
326 This
->fileextra
.cb
= 0;
329 if (This
->szFileName
!= NULL
) {
330 HeapFree(GetProcessHeap(), 0, This
->szFileName
);
331 This
->szFileName
= NULL
;
333 if (This
->hmmio
!= NULL
) {
334 mmioClose(This
->hmmio
, 0);
338 HeapFree(GetProcessHeap(), 0, This
);
343 static HRESULT WINAPI
IAVIFile_fnInfo(IAVIFile
*iface
, LPAVIFILEINFOW afi
,
346 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
348 TRACE("(%p,%p,%ld)\n",iface
,afi
,size
);
351 return AVIERR_BADPARAM
;
353 return AVIERR_BADSIZE
;
355 AVIFILE_UpdateInfo(This
);
357 memcpy(afi
, &This
->fInfo
, min((DWORD
)size
, sizeof(This
->fInfo
)));
359 if ((DWORD
)size
< sizeof(This
->fInfo
))
360 return AVIERR_BUFFERTOOSMALL
;
364 static HRESULT WINAPI
IAVIFile_fnGetStream(IAVIFile
*iface
, PAVISTREAM
*avis
,
365 DWORD fccType
, LONG lParam
)
367 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
371 TRACE("(%p,%p,0x%08lX,%ld)\n", iface
, avis
, fccType
, lParam
);
373 if (avis
== NULL
|| lParam
< 0)
374 return AVIERR_BADPARAM
;
376 nStream
= AVIFILE_SearchStream(This
, fccType
, lParam
);
378 /* Does the requested stream exist? */
379 if (nStream
< This
->fInfo
.dwStreams
&&
380 This
->ppStreams
[nStream
] != NULL
) {
381 *avis
= (PAVISTREAM
)This
->ppStreams
[nStream
];
382 IAVIStream_AddRef(*avis
);
387 /* Sorry, but the specified stream doesn't exist */
388 return AVIERR_NODATA
;
391 static HRESULT WINAPI
IAVIFile_fnCreateStream(IAVIFile
*iface
,PAVISTREAM
*avis
,
392 LPAVISTREAMINFOW asi
)
394 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
398 TRACE("(%p,%p,%p)\n", iface
, avis
, asi
);
400 /* check parameters */
401 if (avis
== NULL
|| asi
== NULL
)
402 return AVIERR_BADPARAM
;
406 /* Does the user have write permission? */
407 if ((This
->uMode
& MMIO_RWMODE
) == 0)
408 return AVIERR_READONLY
;
410 /* Can we add another stream? */
411 n
= This
->fInfo
.dwStreams
;
412 if (n
>= MAX_AVISTREAMS
|| This
->dwMoviChunkPos
!= 0) {
413 /* already reached max nr of streams
414 * or have already written frames to disk */
415 return AVIERR_UNSUPPORTED
;
418 /* check AVISTREAMINFO for some really needed things */
419 if (asi
->fccType
== 0 || asi
->dwScale
== 0 || asi
->dwRate
== 0)
420 return AVIERR_BADFORMAT
;
422 /* now it seems to be save to add the stream */
423 assert(This
->ppStreams
[n
] == NULL
);
424 This
->ppStreams
[n
] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
425 sizeof(IAVIStreamImpl
));
426 if (This
->ppStreams
[n
] == NULL
)
427 return AVIERR_MEMORY
;
429 /* initialize the new allocated stream */
430 AVIFILE_ConstructAVIStream(This
, n
, asi
);
432 This
->fInfo
.dwStreams
++;
435 /* update our AVIFILEINFO structure */
436 AVIFILE_UpdateInfo(This
);
439 *avis
= (PAVISTREAM
)This
->ppStreams
[n
];
440 IAVIStream_AddRef(*avis
);
445 static HRESULT WINAPI
IAVIFile_fnWriteData(IAVIFile
*iface
, DWORD ckid
,
446 LPVOID lpData
, LONG size
)
448 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
450 TRACE("(%p,0x%08lX,%p,%ld)\n", iface
, ckid
, lpData
, size
);
452 /* check parameters */
454 return AVIERR_BADPARAM
;
456 return AVIERR_BADSIZE
;
458 /* Do we have write permission? */
459 if ((This
->uMode
& MMIO_RWMODE
) == 0)
460 return AVIERR_READONLY
;
464 return WriteExtraChunk(&This
->fileextra
, ckid
, lpData
, size
);
467 static HRESULT WINAPI
IAVIFile_fnReadData(IAVIFile
*iface
, DWORD ckid
,
468 LPVOID lpData
, LONG
*size
)
470 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
472 TRACE("(%p,0x%08lX,%p,%p)\n", iface
, ckid
, lpData
, size
);
474 return ReadExtraChunk(&This
->fileextra
, ckid
, lpData
, size
);
477 static HRESULT WINAPI
IAVIFile_fnEndRecord(IAVIFile
*iface
)
479 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
481 TRACE("(%p)\n",iface
);
483 if ((This
->uMode
& MMIO_RWMODE
) == 0)
484 return AVIERR_READONLY
;
488 /* no frames written to any stream? -- compute start of 'movi'-chunk */
489 if (This
->dwMoviChunkPos
== 0)
490 AVIFILE_ComputeMoviStart(This
);
492 This
->fInfo
.dwFlags
|= AVIFILEINFO_ISINTERLEAVED
;
494 /* already written frames to any stream, ... */
495 if (This
->ckLastRecord
.dwFlags
& MMIO_DIRTY
) {
496 /* close last record */
497 if (mmioAscend(This
->hmmio
, &This
->ckLastRecord
, 0) != 0)
498 return AVIERR_FILEWRITE
;
500 AVIFILE_AddRecord(This
);
502 if (This
->fInfo
.dwSuggestedBufferSize
< This
->ckLastRecord
.cksize
+ 3 * sizeof(DWORD
))
503 This
->fInfo
.dwSuggestedBufferSize
= This
->ckLastRecord
.cksize
+ 3 * sizeof(DWORD
);
506 /* write out a new record into file, but don't close it */
507 This
->ckLastRecord
.cksize
= 0;
508 This
->ckLastRecord
.fccType
= listtypeAVIRECORD
;
509 if (mmioSeek(This
->hmmio
, This
->dwNextFramePos
, SEEK_SET
) == -1)
510 return AVIERR_FILEWRITE
;
511 if (mmioCreateChunk(This
->hmmio
, &This
->ckLastRecord
, MMIO_CREATELIST
) != 0)
512 return AVIERR_FILEWRITE
;
513 This
->dwNextFramePos
+= 3 * sizeof(DWORD
);
518 static HRESULT WINAPI
IAVIFile_fnDeleteStream(IAVIFile
*iface
, DWORD fccType
,
521 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
525 TRACE("(%p,0x%08lX,%ld)\n", iface
, fccType
, lParam
);
527 /* check parameter */
529 return AVIERR_BADPARAM
;
531 /* Have user write permissions? */
532 if ((This
->uMode
& MMIO_RWMODE
) == 0)
533 return AVIERR_READONLY
;
535 nStream
= AVIFILE_SearchStream(This
, fccType
, lParam
);
537 /* Does the requested stream exist? */
538 if (nStream
< This
->fInfo
.dwStreams
&&
539 This
->ppStreams
[nStream
] != NULL
) {
540 /* ... so delete it now */
541 HeapFree(GetProcessHeap(), 0, This
->ppStreams
[nStream
]);
543 if (This
->fInfo
.dwStreams
- nStream
> 0)
544 memcpy(This
->ppStreams
+ nStream
, This
->ppStreams
+ nStream
+ 1,
545 (This
->fInfo
.dwStreams
- nStream
) * sizeof(IAVIStreamImpl
*));
547 This
->ppStreams
[This
->fInfo
.dwStreams
] = NULL
;
548 This
->fInfo
.dwStreams
--;
551 /* This->fInfo will be updated further when asked for */
554 return AVIERR_NODATA
;
557 /***********************************************************************/
559 static HRESULT WINAPI
IPersistFile_fnQueryInterface(IPersistFile
*iface
,
560 REFIID refiid
, LPVOID
*obj
)
562 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
564 assert(This
->paf
!= NULL
);
566 return IAVIFile_QueryInterface((PAVIFILE
)This
->paf
, refiid
, obj
);
569 static ULONG WINAPI
IPersistFile_fnAddRef(IPersistFile
*iface
)
571 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
573 assert(This
->paf
!= NULL
);
575 return IAVIFile_AddRef((PAVIFILE
)This
->paf
);
578 static ULONG WINAPI
IPersistFile_fnRelease(IPersistFile
*iface
)
580 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
582 assert(This
->paf
!= NULL
);
584 return IAVIFile_Release((PAVIFILE
)This
->paf
);
587 static HRESULT WINAPI
IPersistFile_fnGetClassID(IPersistFile
*iface
,
590 TRACE("(%p,%p)\n", iface
, pClassID
);
592 if (pClassID
== NULL
)
593 return AVIERR_BADPARAM
;
595 memcpy(pClassID
, &CLSID_AVIFile
, sizeof(CLSID_AVIFile
));
600 static HRESULT WINAPI
IPersistFile_fnIsDirty(IPersistFile
*iface
)
602 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
604 TRACE("(%p)\n", iface
);
606 assert(This
->paf
!= NULL
);
608 return (This
->paf
->fDirty
? S_OK
: S_FALSE
);
611 static HRESULT WINAPI
IPersistFile_fnLoad(IPersistFile
*iface
,
612 LPCOLESTR pszFileName
, DWORD dwMode
)
614 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
618 TRACE("(%p,%s,0x%08lX)\n", iface
, debugstr_w(pszFileName
), dwMode
);
620 /* check parameter */
621 if (pszFileName
== NULL
)
622 return AVIERR_BADPARAM
;
624 assert(This
->paf
!= NULL
);
625 if (This
->paf
->hmmio
!= NULL
)
626 return AVIERR_ERROR
; /* No reuse of this object for another file! */
628 /* remeber mode and name */
629 This
->paf
->uMode
= dwMode
;
631 len
= lstrlenW(pszFileName
) + 1;
632 This
->paf
->szFileName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
633 if (This
->paf
->szFileName
== NULL
)
634 return AVIERR_MEMORY
;
635 lstrcpyW(This
->paf
->szFileName
, pszFileName
);
637 /* try to open the file */
638 This
->paf
->hmmio
= mmioOpenW(This
->paf
->szFileName
, NULL
,
639 MMIO_ALLOCBUF
| dwMode
);
640 if (This
->paf
->hmmio
== NULL
) {
641 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
644 len
= WideCharToMultiByte(CP_ACP
, 0, This
->paf
->szFileName
, -1,
645 NULL
, 0, NULL
, NULL
);
646 szFileName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(CHAR
));
647 if (szFileName
== NULL
)
648 return AVIERR_MEMORY
;
650 WideCharToMultiByte(CP_ACP
, 0, This
->paf
->szFileName
, -1, szFileName
,
653 This
->paf
->hmmio
= mmioOpenA(szFileName
, NULL
, MMIO_ALLOCBUF
| dwMode
);
654 HeapFree(GetProcessHeap(), 0, szFileName
);
655 if (This
->paf
->hmmio
== NULL
)
656 return AVIERR_FILEOPEN
;
659 /* should we create a new file? */
660 if (dwMode
& OF_CREATE
) {
661 memset(& This
->paf
->fInfo
, 0, sizeof(This
->paf
->fInfo
));
662 This
->paf
->fInfo
.dwFlags
= AVIFILEINFO_HASINDEX
| AVIFILEINFO_TRUSTCKTYPE
;
666 return AVIFILE_LoadFile(This
->paf
);
669 static HRESULT WINAPI
IPersistFile_fnSave(IPersistFile
*iface
,
670 LPCOLESTR pszFileName
,BOOL fRemember
)
672 TRACE("(%p,%s,%d)\n", iface
, debugstr_w(pszFileName
), fRemember
);
674 /* We write directly to disk, so nothing to do. */
679 static HRESULT WINAPI
IPersistFile_fnSaveCompleted(IPersistFile
*iface
,
680 LPCOLESTR pszFileName
)
682 TRACE("(%p,%s)\n", iface
, debugstr_w(pszFileName
));
684 /* We write directly to disk, so nothing to do. */
689 static HRESULT WINAPI
IPersistFile_fnGetCurFile(IPersistFile
*iface
,
690 LPOLESTR
*ppszFileName
)
692 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
694 TRACE("(%p,%p)\n", iface
, ppszFileName
);
696 if (ppszFileName
== NULL
)
697 return AVIERR_BADPARAM
;
699 *ppszFileName
= NULL
;
701 assert(This
->paf
!= NULL
);
703 if (This
->paf
->szFileName
!= NULL
) {
704 int len
= lstrlenW(This
->paf
->szFileName
) + 1;
706 *ppszFileName
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
707 if (*ppszFileName
== NULL
)
708 return AVIERR_MEMORY
;
710 strcpyW(*ppszFileName
, This
->paf
->szFileName
);
716 /***********************************************************************/
718 static HRESULT WINAPI
IAVIStream_fnQueryInterface(IAVIStream
*iface
,
719 REFIID refiid
, LPVOID
*obj
)
721 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
723 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(refiid
), obj
);
725 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
726 IsEqualGUID(&IID_IAVIStream
, refiid
)) {
728 IAVIStream_AddRef(iface
);
732 /* FIXME: IAVIStreaming interface */
734 return OLE_E_ENUM_NOMORE
;
737 static ULONG WINAPI
IAVIStream_fnAddRef(IAVIStream
*iface
)
739 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
740 ULONG ref
= InterlockedIncrement(&This
->ref
);
742 TRACE("(%p) -> %ld\n", iface
, ref
);
744 /* also add ref to parent, so that it doesn't kill us */
745 if (This
->paf
!= NULL
)
746 IAVIFile_AddRef((PAVIFILE
)This
->paf
);
751 static ULONG WINAPI
IAVIStream_fnRelease(IAVIStream
* iface
)
753 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
754 ULONG ref
= InterlockedDecrement(&This
->ref
);
756 TRACE("(%p) -> %ld\n", iface
, ref
);
758 if (This
->paf
!= NULL
)
759 IAVIFile_Release((PAVIFILE
)This
->paf
);
764 static HRESULT WINAPI
IAVIStream_fnCreate(IAVIStream
*iface
, LPARAM lParam1
,
767 TRACE("(%p,0x%08lX,0x%08lX)\n", iface
, lParam1
, lParam2
);
769 /* This IAVIStream interface needs an AVIFile */
770 return AVIERR_UNSUPPORTED
;
773 static HRESULT WINAPI
IAVIStream_fnInfo(IAVIStream
*iface
,LPAVISTREAMINFOW psi
,
776 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
778 TRACE("(%p,%p,%ld)\n", iface
, psi
, size
);
781 return AVIERR_BADPARAM
;
783 return AVIERR_BADSIZE
;
785 memcpy(psi
, &This
->sInfo
, min((DWORD
)size
, sizeof(This
->sInfo
)));
787 if ((DWORD
)size
< sizeof(This
->sInfo
))
788 return AVIERR_BUFFERTOOSMALL
;
792 static LONG WINAPI
IAVIStream_fnFindSample(IAVIStream
*iface
, LONG pos
,
795 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
799 TRACE("(%p,%ld,0x%08lX)\n",iface
,pos
,flags
);
801 if (flags
& FIND_FROM_START
) {
802 pos
= This
->sInfo
.dwStart
;
803 flags
&= ~(FIND_FROM_START
|FIND_PREV
);
807 if (This
->sInfo
.dwSampleSize
!= 0) {
808 /* convert samples into block number with offset */
809 AVIFILE_SamplesToBlock(This
, &pos
, &offset
);
812 if (flags
& FIND_TYPE
) {
813 if (flags
& FIND_KEY
) {
814 while (0 <= pos
&& pos
<= This
->lLastFrame
) {
815 if (This
->idxFrames
[pos
].dwFlags
& AVIIF_KEYFRAME
)
818 if (flags
& FIND_NEXT
)
823 } else if (flags
& FIND_ANY
) {
824 while (0 <= pos
&& pos
<= This
->lLastFrame
) {
825 if (This
->idxFrames
[pos
].dwChunkLength
> 0)
828 if (flags
& FIND_NEXT
)
834 } else if ((flags
& FIND_FORMAT
) && This
->idxFmtChanges
!= NULL
&&
835 This
->sInfo
.fccType
== streamtypeVIDEO
) {
836 if (flags
& FIND_NEXT
) {
839 for (n
= 0; n
< This
->sInfo
.dwFormatChangeCount
; n
++)
840 if (This
->idxFmtChanges
[n
].ckid
>= pos
) {
841 pos
= This
->idxFmtChanges
[n
].ckid
;
847 for (n
= (LONG
)This
->sInfo
.dwFormatChangeCount
; n
>= 0; n
--) {
848 if (This
->idxFmtChanges
[n
].ckid
<= pos
) {
849 pos
= This
->idxFmtChanges
[n
].ckid
;
854 if (pos
> (LONG
)This
->sInfo
.dwStart
)
855 return 0; /* format changes always for first frame */
863 if (pos
< (LONG
)This
->sInfo
.dwStart
)
866 switch (flags
& FIND_RET
) {
869 pos
= This
->idxFrames
[pos
].dwChunkLength
;
872 /* physical position */
873 pos
= This
->idxFrames
[pos
].dwChunkOffset
+ 2 * sizeof(DWORD
)
874 + offset
* This
->sInfo
.dwSampleSize
;
878 if (This
->sInfo
.dwSampleSize
)
879 pos
= This
->sInfo
.dwSampleSize
;
884 FIXME(": FIND_INDEX flag is not supported!\n");
885 /* This is an index in the index-table on disc. */
887 }; /* else logical position */
892 static HRESULT WINAPI
IAVIStream_fnReadFormat(IAVIStream
*iface
, LONG pos
,
893 LPVOID format
, LONG
*formatsize
)
895 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
897 TRACE("(%p,%ld,%p,%p)\n", iface
, pos
, format
, formatsize
);
899 if (formatsize
== NULL
)
900 return AVIERR_BADPARAM
;
902 /* only interested in needed buffersize? */
903 if (format
== NULL
|| *formatsize
<= 0) {
904 *formatsize
= This
->cbFormat
;
909 /* copy initial format (only as much as will fit) */
910 memcpy(format
, This
->lpFormat
, min(*(DWORD
*)formatsize
, This
->cbFormat
));
911 if (*(DWORD
*)formatsize
< This
->cbFormat
) {
912 *formatsize
= This
->cbFormat
;
913 return AVIERR_BUFFERTOOSMALL
;
916 /* Could format change? When yes will it change? */
917 if ((This
->sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
) &&
918 pos
> This
->sInfo
.dwStart
) {
921 lLastFmt
= IAVIStream_fnFindSample(iface
, pos
, FIND_FORMAT
|FIND_PREV
);
923 FIXME(": need to read formatchange for %ld -- unimplemented!\n",lLastFmt
);
927 *formatsize
= This
->cbFormat
;
931 static HRESULT WINAPI
IAVIStream_fnSetFormat(IAVIStream
*iface
, LONG pos
,
932 LPVOID format
, LONG formatsize
)
934 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
936 LPBITMAPINFOHEADER lpbiNew
= (LPBITMAPINFOHEADER
)format
;
938 TRACE("(%p,%ld,%p,%ld)\n", iface
, pos
, format
, formatsize
);
940 /* check parameters */
941 if (format
== NULL
|| formatsize
<= 0)
942 return AVIERR_BADPARAM
;
944 /* Do we have write permission? */
945 if ((This
->paf
->uMode
& MMIO_RWMODE
) == 0)
946 return AVIERR_READONLY
;
948 /* can only set format before frame is written! */
949 if (This
->lLastFrame
> pos
)
950 return AVIERR_UNSUPPORTED
;
952 /* initial format or a formatchange? */
953 if (This
->lpFormat
== NULL
) {
955 if (This
->paf
->dwMoviChunkPos
!= 0)
956 return AVIERR_ERROR
; /* user has used API in wrong sequnece! */
958 This
->lpFormat
= HeapAlloc(GetProcessHeap(), 0, formatsize
);
959 if (This
->lpFormat
== NULL
)
960 return AVIERR_MEMORY
;
961 This
->cbFormat
= formatsize
;
963 memcpy(This
->lpFormat
, format
, formatsize
);
965 /* update some infos about stream */
966 if (This
->sInfo
.fccType
== streamtypeVIDEO
) {
969 lDim
= This
->sInfo
.rcFrame
.right
- This
->sInfo
.rcFrame
.left
;
970 if (lDim
< lpbiNew
->biWidth
)
971 This
->sInfo
.rcFrame
.right
= This
->sInfo
.rcFrame
.left
+ lpbiNew
->biWidth
;
972 lDim
= This
->sInfo
.rcFrame
.bottom
- This
->sInfo
.rcFrame
.top
;
973 if (lDim
< lpbiNew
->biHeight
)
974 This
->sInfo
.rcFrame
.bottom
= This
->sInfo
.rcFrame
.top
+ lpbiNew
->biHeight
;
975 } else if (This
->sInfo
.fccType
== streamtypeAUDIO
)
976 This
->sInfo
.dwSampleSize
= ((LPWAVEFORMATEX
)This
->lpFormat
)->nBlockAlign
;
981 LPBITMAPINFOHEADER lpbiOld
= (LPBITMAPINFOHEADER
)This
->lpFormat
;
982 RGBQUAD
*rgbNew
= (RGBQUAD
*)((LPBYTE
)lpbiNew
+ lpbiNew
->biSize
);
983 AVIPALCHANGE
*lppc
= NULL
;
986 /* perhaps format change, check it ... */
987 if (This
->cbFormat
!= formatsize
)
988 return AVIERR_UNSUPPORTED
;
990 /* no format change, only the initial one */
991 if (memcmp(This
->lpFormat
, format
, formatsize
) == 0)
994 /* check that's only the palette, which changes */
995 if (lpbiOld
->biSize
!= lpbiNew
->biSize
||
996 lpbiOld
->biWidth
!= lpbiNew
->biWidth
||
997 lpbiOld
->biHeight
!= lpbiNew
->biHeight
||
998 lpbiOld
->biPlanes
!= lpbiNew
->biPlanes
||
999 lpbiOld
->biBitCount
!= lpbiNew
->biBitCount
||
1000 lpbiOld
->biCompression
!= lpbiNew
->biCompression
||
1001 lpbiOld
->biClrUsed
!= lpbiNew
->biClrUsed
)
1002 return AVIERR_UNSUPPORTED
;
1004 This
->sInfo
.dwFlags
|= AVISTREAMINFO_FORMATCHANGES
;
1006 /* simply say all colors have changed */
1007 ck
.ckid
= MAKEAVICKID(cktypePALchange
, This
->nStream
);
1008 ck
.cksize
= 2 * sizeof(WORD
) + lpbiOld
->biClrUsed
* sizeof(PALETTEENTRY
);
1009 lppc
= HeapAlloc(GetProcessHeap(), 0, ck
.cksize
);
1011 return AVIERR_MEMORY
;
1013 lppc
->bFirstEntry
= 0;
1014 lppc
->bNumEntries
= (lpbiOld
->biClrUsed
< 256 ? lpbiOld
->biClrUsed
: 0);
1016 for (n
= 0; n
< lpbiOld
->biClrUsed
; n
++) {
1017 lppc
->peNew
[n
].peRed
= rgbNew
[n
].rgbRed
;
1018 lppc
->peNew
[n
].peGreen
= rgbNew
[n
].rgbGreen
;
1019 lppc
->peNew
[n
].peBlue
= rgbNew
[n
].rgbBlue
;
1020 lppc
->peNew
[n
].peFlags
= 0;
1023 if (mmioSeek(This
->paf
->hmmio
, This
->paf
->dwNextFramePos
, SEEK_SET
) == -1)
1024 return AVIERR_FILEWRITE
;
1025 if (mmioCreateChunk(This
->paf
->hmmio
, &ck
, 0) != S_OK
)
1026 return AVIERR_FILEWRITE
;
1027 if (mmioWrite(This
->paf
->hmmio
, (HPSTR
)lppc
, ck
.cksize
) != ck
.cksize
)
1028 return AVIERR_FILEWRITE
;
1029 if (mmioAscend(This
->paf
->hmmio
, &ck
, 0) != S_OK
)
1030 return AVIERR_FILEWRITE
;
1031 This
->paf
->dwNextFramePos
+= ck
.cksize
+ 2 * sizeof(DWORD
);
1033 HeapFree(GetProcessHeap(), 0, lppc
);
1035 return AVIFILE_AddFrame(This
, cktypePALchange
, n
, ck
.dwDataOffset
, 0);
1039 static HRESULT WINAPI
IAVIStream_fnRead(IAVIStream
*iface
, LONG start
,
1040 LONG samples
, LPVOID buffer
,
1041 LONG buffersize
, LPLONG bytesread
,
1044 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
1049 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface
, start
, samples
, buffer
,
1050 buffersize
, bytesread
, samplesread
);
1052 /* clear return parameters if given */
1053 if (bytesread
!= NULL
)
1055 if (samplesread
!= NULL
)
1058 /* check parameters */
1059 if ((LONG
)This
->sInfo
.dwStart
> start
)
1060 return AVIERR_NODATA
; /* couldn't read before start of stream */
1061 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< (DWORD
)start
)
1062 return AVIERR_NODATA
; /* start is past end of stream */
1064 /* should we read as much as possible? */
1065 if (samples
== -1) {
1066 /* User should know how much we have read */
1067 if (bytesread
== NULL
&& samplesread
== NULL
)
1068 return AVIERR_BADPARAM
;
1070 if (This
->sInfo
.dwSampleSize
!= 0)
1071 samples
= buffersize
/ This
->sInfo
.dwSampleSize
;
1076 /* limit to end of stream */
1077 if ((LONG
)This
->sInfo
.dwLength
< samples
)
1078 samples
= This
->sInfo
.dwLength
;
1079 if ((start
- This
->sInfo
.dwStart
) > (This
->sInfo
.dwLength
- samples
))
1080 samples
= This
->sInfo
.dwLength
- (start
- This
->sInfo
.dwStart
);
1082 /* nothing to read? Then leave ... */
1086 if (This
->sInfo
.dwSampleSize
!= 0) {
1087 /* fixed samplesize -- we can read over frame/block boundaries */
1091 /* convert start sample to block,offset pair */
1092 AVIFILE_SamplesToBlock(This
, &block
, &offset
);
1094 /* convert samples to bytes */
1095 samples
*= This
->sInfo
.dwSampleSize
;
1097 while (samples
> 0 && buffersize
> 0) {
1098 if (block
!= This
->dwCurrentFrame
) {
1099 hr
= AVIFILE_ReadBlock(This
, block
, NULL
, 0);
1104 size
= min((DWORD
)samples
, (DWORD
)buffersize
);
1105 size
= min(size
, This
->cbBuffer
- offset
);
1106 memcpy(buffer
, ((BYTE
*)&This
->lpBuffer
[2]) + offset
, size
);
1110 buffer
= ((LPBYTE
)buffer
)+size
;
1114 /* fill out return parameters if given */
1115 if (bytesread
!= NULL
)
1117 if (samplesread
!= NULL
)
1118 *samplesread
+= size
/ This
->sInfo
.dwSampleSize
;
1124 return AVIERR_BUFFERTOOSMALL
;
1126 /* variable samplesize -- we can only read one full frame/block */
1130 assert(start
<= This
->lLastFrame
);
1131 size
= This
->idxFrames
[start
].dwChunkLength
;
1132 if (buffer
!= NULL
&& buffersize
>= size
) {
1133 hr
= AVIFILE_ReadBlock(This
, start
, buffer
, size
);
1136 } else if (buffer
!= NULL
)
1137 return AVIERR_BUFFERTOOSMALL
;
1139 /* fill out return parameters if given */
1140 if (bytesread
!= NULL
)
1142 if (samplesread
!= NULL
)
1143 *samplesread
= samples
;
1149 static HRESULT WINAPI
IAVIStream_fnWrite(IAVIStream
*iface
, LONG start
,
1150 LONG samples
, LPVOID buffer
,
1151 LONG buffersize
, DWORD flags
,
1153 LPLONG byteswritten
)
1155 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
1160 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface
, start
, samples
,
1161 buffer
, buffersize
, flags
, sampwritten
, byteswritten
);
1163 /* clear return parameters if given */
1164 if (sampwritten
!= NULL
)
1166 if (byteswritten
!= NULL
)
1169 /* check parameters */
1170 if (buffer
== NULL
&& (buffersize
> 0 || samples
> 0))
1171 return AVIERR_BADPARAM
;
1173 /* Have we write permission? */
1174 if ((This
->paf
->uMode
& MMIO_RWMODE
) == 0)
1175 return AVIERR_READONLY
;
1177 switch (This
->sInfo
.fccType
) {
1178 case streamtypeAUDIO
:
1179 ckid
= MAKEAVICKID(cktypeWAVEbytes
, This
->nStream
);
1182 if ((flags
& AVIIF_KEYFRAME
) && buffersize
!= 0)
1183 ckid
= MAKEAVICKID(cktypeDIBbits
, This
->nStream
);
1185 ckid
= MAKEAVICKID(cktypeDIBcompressed
, This
->nStream
);
1189 /* append to end of stream? */
1191 if (This
->lLastFrame
== -1)
1192 start
= This
->sInfo
.dwStart
;
1194 start
= This
->sInfo
.dwLength
;
1195 } else if (This
->lLastFrame
== -1)
1196 This
->sInfo
.dwStart
= start
;
1198 if (This
->sInfo
.dwSampleSize
!= 0) {
1199 /* fixed sample size -- audio like */
1200 if (samples
* This
->sInfo
.dwSampleSize
!= buffersize
)
1201 return AVIERR_BADPARAM
;
1203 /* Couldn't skip audio-like data -- User must supply appropriate silence */
1204 if (This
->sInfo
.dwLength
!= start
)
1205 return AVIERR_UNSUPPORTED
;
1207 /* Convert position to frame/block */
1208 start
= This
->lLastFrame
+ 1;
1210 if ((This
->paf
->fInfo
.dwFlags
& AVIFILEINFO_ISINTERLEAVED
) == 0) {
1211 FIXME(": not interleaved, could collect audio data!\n");
1214 /* variable sample size -- video like */
1216 return AVIERR_UNSUPPORTED
;
1218 /* must we fill up with empty frames? */
1219 if (This
->lLastFrame
!= -1) {
1220 FOURCC ckid2
= MAKEAVICKID(cktypeDIBcompressed
, This
->nStream
);
1222 while (start
> This
->lLastFrame
+ 1) {
1223 hr
= AVIFILE_WriteBlock(This
, This
->lLastFrame
+ 1, ckid2
, 0, NULL
, 0);
1230 /* write the block now */
1231 hr
= AVIFILE_WriteBlock(This
, start
, ckid
, flags
, buffer
, buffersize
);
1232 if (SUCCEEDED(hr
)) {
1233 /* fill out return parameters if given */
1234 if (sampwritten
!= NULL
)
1235 *sampwritten
= samples
;
1236 if (byteswritten
!= NULL
)
1237 *byteswritten
= buffersize
;
1243 static HRESULT WINAPI
IAVIStream_fnDelete(IAVIStream
*iface
, LONG start
,
1246 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
1248 FIXME("(%p,%ld,%ld): stub\n", iface
, start
, samples
);
1250 /* check parameters */
1251 if (start
< 0 || samples
< 0)
1252 return AVIERR_BADPARAM
;
1254 /* Delete before start of stream? */
1255 if (start
+ samples
< This
->sInfo
.dwStart
)
1258 /* Delete after end of stream? */
1259 if (start
> This
->sInfo
.dwLength
)
1262 /* For the rest we need write permissions */
1263 if ((This
->paf
->uMode
& MMIO_RWMODE
) == 0)
1264 return AVIERR_READONLY
;
1266 /* 1. overwrite the data with JUNK
1268 * if ISINTERLEAVED {
1269 * 2. concat all neighboured JUNK-blocks in this record to one
1270 * 3. if this record only contains JUNK and is at end set dwNextFramePos
1271 * to start of this record, repeat this.
1273 * 2. concat all neighboured JUNK-blocks.
1274 * 3. if the JUNK block is at the end, then set dwNextFramePos to
1275 * start of this block.
1279 return AVIERR_UNSUPPORTED
;
1282 static HRESULT WINAPI
IAVIStream_fnReadData(IAVIStream
*iface
, DWORD fcc
,
1283 LPVOID lp
, LPLONG lpread
)
1285 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
1287 TRACE("(%p,0x%08lX,%p,%p)\n", iface
, fcc
, lp
, lpread
);
1289 if (fcc
== ckidSTREAMHANDLERDATA
) {
1290 if (This
->lpHandlerData
!= NULL
&& This
->cbHandlerData
> 0) {
1291 if (lp
== NULL
|| *lpread
<= 0) {
1292 *lpread
= This
->cbHandlerData
;
1296 memcpy(lp
, This
->lpHandlerData
, min(This
->cbHandlerData
, *lpread
));
1297 if (*lpread
< This
->cbHandlerData
)
1298 return AVIERR_BUFFERTOOSMALL
;
1301 return AVIERR_NODATA
;
1303 return ReadExtraChunk(&This
->extra
, fcc
, lp
, lpread
);
1306 static HRESULT WINAPI
IAVIStream_fnWriteData(IAVIStream
*iface
, DWORD fcc
,
1307 LPVOID lp
, LONG size
)
1309 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
1311 TRACE("(%p,0x%08lx,%p,%ld)\n", iface
, fcc
, lp
, size
);
1313 /* check parameters */
1315 return AVIERR_BADPARAM
;
1317 return AVIERR_BADSIZE
;
1319 /* need write permission */
1320 if ((This
->paf
->uMode
& MMIO_RWMODE
) == 0)
1321 return AVIERR_READONLY
;
1323 /* already written something to this file? */
1324 if (This
->paf
->dwMoviChunkPos
!= 0) {
1325 /* the data will be inserted before the 'movi' chunk, so check for
1327 DWORD dwPos
= AVIFILE_ComputeMoviStart(This
->paf
);
1329 /* ckid,size => 2 * sizeof(DWORD) */
1330 dwPos
+= 2 * sizeof(DWORD
) + size
;
1331 if (size
>= This
->paf
->dwMoviChunkPos
- 2 * sizeof(DWORD
))
1332 return AVIERR_UNSUPPORTED
; /* not enough space left */
1335 This
->paf
->fDirty
= TRUE
;
1337 if (fcc
== ckidSTREAMHANDLERDATA
) {
1338 if (This
->lpHandlerData
!= NULL
) {
1339 FIXME(": handler data already set -- overwirte?\n");
1340 return AVIERR_UNSUPPORTED
;
1343 This
->lpHandlerData
= HeapAlloc(GetProcessHeap(), 0, size
);
1344 if (This
->lpHandlerData
== NULL
)
1345 return AVIERR_MEMORY
;
1346 This
->cbHandlerData
= size
;
1347 memcpy(This
->lpHandlerData
, lp
, size
);
1351 return WriteExtraChunk(&This
->extra
, fcc
, lp
, size
);
1354 static HRESULT WINAPI
IAVIStream_fnSetInfo(IAVIStream
*iface
,
1355 LPAVISTREAMINFOW info
, LONG infolen
)
1357 FIXME("(%p,%p,%ld): stub\n", iface
, info
, infolen
);
1362 /***********************************************************************/
1364 static HRESULT
AVIFILE_AddFrame(IAVIStreamImpl
*This
, DWORD ckid
, DWORD size
, DWORD offset
, DWORD flags
)
1366 /* pre-conditions */
1367 assert(This
!= NULL
);
1369 switch (TWOCCFromFOURCC(ckid
)) {
1371 if (This
->paf
->fInfo
.dwFlags
& AVIFILEINFO_TRUSTCKTYPE
)
1372 flags
|= AVIIF_KEYFRAME
;
1374 case cktypeDIBcompressed
:
1375 if (This
->paf
->fInfo
.dwFlags
& AVIFILEINFO_TRUSTCKTYPE
)
1376 flags
&= ~AVIIF_KEYFRAME
;
1378 case cktypePALchange
:
1379 if (This
->sInfo
.fccType
!= streamtypeVIDEO
) {
1380 ERR(": found palette change in non-video stream!\n");
1381 return AVIERR_BADFORMAT
;
1383 This
->sInfo
.dwFlags
|= AVISTREAMINFO_FORMATCHANGES
;
1384 This
->sInfo
.dwFormatChangeCount
++;
1386 if (This
->idxFmtChanges
== NULL
|| This
->sInfo
.dwFormatChangeCount
< This
->nIdxFmtChanges
) {
1387 UINT n
= This
->sInfo
.dwFormatChangeCount
;
1389 This
->nIdxFmtChanges
+= 16;
1390 if (This
->idxFmtChanges
== NULL
)
1391 This
->idxFmtChanges
=
1392 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->nIdxFmtChanges
* sizeof(AVIINDEXENTRY
));
1394 This
->idxFmtChanges
=
1395 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->idxFmtChanges
,
1396 This
->nIdxFmtChanges
* sizeof(AVIINDEXENTRY
));
1397 if (This
->idxFmtChanges
== NULL
)
1398 return AVIERR_MEMORY
;
1400 This
->idxFmtChanges
[n
].ckid
= This
->lLastFrame
;
1401 This
->idxFmtChanges
[n
].dwFlags
= 0;
1402 This
->idxFmtChanges
[n
].dwChunkOffset
= offset
;
1403 This
->idxFmtChanges
[n
].dwChunkLength
= size
;
1408 case cktypeWAVEbytes
:
1409 if (This
->paf
->fInfo
.dwFlags
& AVIFILEINFO_TRUSTCKTYPE
)
1410 flags
|= AVIIF_KEYFRAME
;
1413 WARN(": unknown TWOCC 0x%04X found\n", TWOCCFromFOURCC(ckid
));
1417 /* first frame is alwasy a keyframe */
1418 if (This
->lLastFrame
== -1)
1419 flags
|= AVIIF_KEYFRAME
;
1421 if (This
->sInfo
.dwSuggestedBufferSize
< size
)
1422 This
->sInfo
.dwSuggestedBufferSize
= size
;
1424 /* get memory for index */
1425 if (This
->idxFrames
== NULL
|| This
->lLastFrame
+ 1 >= This
->nIdxFrames
) {
1426 This
->nIdxFrames
+= 512;
1427 if (This
->idxFrames
== NULL
)
1428 This
->idxFrames
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->nIdxFrames
* sizeof(AVIINDEXENTRY
));
1430 This
->idxFrames
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->idxFrames
,
1431 This
->nIdxFrames
* sizeof(AVIINDEXENTRY
));
1432 if (This
->idxFrames
== NULL
)
1433 return AVIERR_MEMORY
;
1437 This
->idxFrames
[This
->lLastFrame
].ckid
= ckid
;
1438 This
->idxFrames
[This
->lLastFrame
].dwFlags
= flags
;
1439 This
->idxFrames
[This
->lLastFrame
].dwChunkOffset
= offset
;
1440 This
->idxFrames
[This
->lLastFrame
].dwChunkLength
= size
;
1442 /* update AVISTREAMINFO structure if necessary */
1443 if (This
->sInfo
.dwLength
<= This
->lLastFrame
)
1444 This
->sInfo
.dwLength
= This
->lLastFrame
+ 1;
1449 static HRESULT
AVIFILE_AddRecord(IAVIFileImpl
*This
)
1451 /* pre-conditions */
1452 assert(This
!= NULL
&& This
->ppStreams
[0] != NULL
);
1454 if (This
->idxRecords
== NULL
|| This
->cbIdxRecords
== 0) {
1455 This
->cbIdxRecords
+= 1024 * sizeof(AVIINDEXENTRY
);
1456 This
->idxRecords
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->cbIdxRecords
);
1457 if (This
->idxRecords
== NULL
)
1458 return AVIERR_MEMORY
;
1461 assert(This
->nIdxRecords
< This
->cbIdxRecords
/sizeof(AVIINDEXENTRY
));
1463 This
->idxRecords
[This
->nIdxRecords
].ckid
= listtypeAVIRECORD
;
1464 This
->idxRecords
[This
->nIdxRecords
].dwFlags
= AVIIF_LIST
;
1465 This
->idxRecords
[This
->nIdxRecords
].dwChunkOffset
=
1466 This
->ckLastRecord
.dwDataOffset
- 2 * sizeof(DWORD
);
1467 This
->idxRecords
[This
->nIdxRecords
].dwChunkLength
=
1468 This
->ckLastRecord
.cksize
;
1469 This
->nIdxRecords
++;
1474 static DWORD
AVIFILE_ComputeMoviStart(IAVIFileImpl
*This
)
1479 /* RIFF,hdrl,movi,avih => (3 * 3 + 2) * sizeof(DWORD) = 11 * sizeof(DWORD) */
1480 dwPos
= 11 * sizeof(DWORD
) + sizeof(MainAVIHeader
);
1482 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
1483 IAVIStreamImpl
*pStream
= This
->ppStreams
[nStream
];
1485 /* strl,strh,strf => (3 + 2 * 2) * sizeof(DWORD) = 7 * sizeof(DWORD) */
1486 dwPos
+= 7 * sizeof(DWORD
) + sizeof(AVIStreamHeader
);
1487 dwPos
+= ((pStream
->cbFormat
+ 1) & ~1U);
1488 if (pStream
->lpHandlerData
!= NULL
&& pStream
->cbHandlerData
> 0)
1489 dwPos
+= 2 * sizeof(DWORD
) + ((pStream
->cbHandlerData
+ 1) & ~1U);
1490 if (lstrlenW(pStream
->sInfo
.szName
) > 0)
1491 dwPos
+= 2 * sizeof(DWORD
) + ((lstrlenW(pStream
->sInfo
.szName
) + 1) & ~1U);
1494 if (This
->dwMoviChunkPos
== 0) {
1495 This
->dwNextFramePos
= dwPos
;
1497 /* pad to multiple of AVI_HEADERSIZE only if we are more than 8 bytes away from it */
1498 if (((dwPos
+ AVI_HEADERSIZE
) & ~(AVI_HEADERSIZE
- 1)) - dwPos
> 2 * sizeof(DWORD
))
1499 This
->dwNextFramePos
= (dwPos
+ AVI_HEADERSIZE
) & ~(AVI_HEADERSIZE
- 1);
1501 This
->dwMoviChunkPos
= This
->dwNextFramePos
- sizeof(DWORD
);
1507 static void AVIFILE_ConstructAVIStream(IAVIFileImpl
*paf
, DWORD nr
, LPAVISTREAMINFOW asi
)
1509 IAVIStreamImpl
*pstream
;
1511 /* pre-conditions */
1512 assert(paf
!= NULL
);
1513 assert(nr
< MAX_AVISTREAMS
);
1514 assert(paf
->ppStreams
[nr
] != NULL
);
1516 pstream
= paf
->ppStreams
[nr
];
1518 pstream
->lpVtbl
= &iavist
;
1521 pstream
->nStream
= nr
;
1522 pstream
->dwCurrentFrame
= (DWORD
)-1;
1523 pstream
->lLastFrame
= -1;
1526 memcpy(&pstream
->sInfo
, asi
, sizeof(pstream
->sInfo
));
1528 if (asi
->dwLength
> 0) {
1529 /* pre-allocate mem for frame-index structure */
1530 pstream
->idxFrames
=
1531 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, asi
->dwLength
* sizeof(AVIINDEXENTRY
));
1532 if (pstream
->idxFrames
!= NULL
)
1533 pstream
->nIdxFrames
= asi
->dwLength
;
1535 if (asi
->dwFormatChangeCount
> 0) {
1536 /* pre-allocate mem for formatchange-index structure */
1537 pstream
->idxFmtChanges
=
1538 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, asi
->dwFormatChangeCount
* sizeof(AVIINDEXENTRY
));
1539 if (pstream
->idxFmtChanges
!= NULL
)
1540 pstream
->nIdxFmtChanges
= asi
->dwFormatChangeCount
;
1543 /* These values will be computed */
1544 pstream
->sInfo
.dwLength
= 0;
1545 pstream
->sInfo
.dwSuggestedBufferSize
= 0;
1546 pstream
->sInfo
.dwFormatChangeCount
= 0;
1547 pstream
->sInfo
.dwEditCount
= 1;
1548 if (pstream
->sInfo
.dwSampleSize
> 0)
1549 SetRectEmpty(&pstream
->sInfo
.rcFrame
);
1552 pstream
->sInfo
.dwCaps
= AVIFILECAPS_CANREAD
|AVIFILECAPS_CANWRITE
;
1555 static void AVIFILE_DestructAVIStream(IAVIStreamImpl
*This
)
1557 /* pre-conditions */
1558 assert(This
!= NULL
);
1560 This
->dwCurrentFrame
= (DWORD
)-1;
1561 This
->lLastFrame
= -1;
1563 if (This
->idxFrames
!= NULL
) {
1564 HeapFree(GetProcessHeap(), 0, This
->idxFrames
);
1565 This
->idxFrames
= NULL
;
1566 This
->nIdxFrames
= 0;
1568 if (This
->idxFmtChanges
!= NULL
) {
1569 HeapFree(GetProcessHeap(), 0, This
->idxFmtChanges
);
1570 This
->idxFmtChanges
= NULL
;
1572 if (This
->lpBuffer
!= NULL
) {
1573 HeapFree(GetProcessHeap(), 0, This
->lpBuffer
);
1574 This
->lpBuffer
= NULL
;
1577 if (This
->lpHandlerData
!= NULL
) {
1578 HeapFree(GetProcessHeap(), 0, This
->lpHandlerData
);
1579 This
->lpHandlerData
= NULL
;
1580 This
->cbHandlerData
= 0;
1582 if (This
->extra
.lp
!= NULL
) {
1583 HeapFree(GetProcessHeap(), 0, This
->extra
.lp
);
1584 This
->extra
.lp
= NULL
;
1587 if (This
->lpFormat
!= NULL
) {
1588 HeapFree(GetProcessHeap(), 0, This
->lpFormat
);
1589 This
->lpFormat
= NULL
;
1594 static HRESULT
AVIFILE_LoadFile(IAVIFileImpl
*This
)
1596 MainAVIHeader MainAVIHdr
;
1601 IAVIStreamImpl
*pStream
;
1605 if (This
->hmmio
== NULL
)
1606 return AVIERR_FILEOPEN
;
1608 /* initialize stream ptr's */
1609 memset(This
->ppStreams
, 0, sizeof(This
->ppStreams
));
1611 /* try to get "RIFF" chunk -- must not be at beginning of file! */
1612 ckRIFF
.fccType
= formtypeAVI
;
1613 if (mmioDescend(This
->hmmio
, &ckRIFF
, NULL
, MMIO_FINDRIFF
) != S_OK
) {
1614 ERR(": not an AVI!\n");
1615 return AVIERR_FILEREAD
;
1618 /* get "LIST" "hdrl" */
1619 ckLIST1
.fccType
= listtypeAVIHEADER
;
1620 hr
= FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ckLIST1
, &ckRIFF
, MMIO_FINDLIST
);
1624 /* get "avih" chunk */
1625 ck
.ckid
= ckidAVIMAINHDR
;
1626 hr
= FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ck
, &ckLIST1
, MMIO_FINDCHUNK
);
1630 if (ck
.cksize
!= sizeof(MainAVIHdr
)) {
1631 ERR(": invalid size of %ld for MainAVIHeader!\n", ck
.cksize
);
1632 return AVIERR_BADFORMAT
;
1634 if (mmioRead(This
->hmmio
, (HPSTR
)&MainAVIHdr
, ck
.cksize
) != ck
.cksize
)
1635 return AVIERR_FILEREAD
;
1637 /* check for MAX_AVISTREAMS limit */
1638 if (MainAVIHdr
.dwStreams
> MAX_AVISTREAMS
) {
1639 WARN("file contains %lu streams, but only supports %d -- change MAX_AVISTREAMS!\n", MainAVIHdr
.dwStreams
, MAX_AVISTREAMS
);
1640 return AVIERR_UNSUPPORTED
;
1643 /* adjust permissions if copyrighted material in file */
1644 if (MainAVIHdr
.dwFlags
& AVIFILEINFO_COPYRIGHTED
) {
1645 This
->uMode
&= ~MMIO_RWMODE
;
1646 This
->uMode
|= MMIO_READ
;
1649 /* convert MainAVIHeader into AVIFILINFOW */
1650 memset(&This
->fInfo
, 0, sizeof(This
->fInfo
));
1651 This
->fInfo
.dwRate
= MainAVIHdr
.dwMicroSecPerFrame
;
1652 This
->fInfo
.dwScale
= 1000000;
1653 This
->fInfo
.dwMaxBytesPerSec
= MainAVIHdr
.dwMaxBytesPerSec
;
1654 This
->fInfo
.dwFlags
= MainAVIHdr
.dwFlags
;
1655 This
->fInfo
.dwCaps
= AVIFILECAPS_CANREAD
|AVIFILECAPS_CANWRITE
;
1656 This
->fInfo
.dwLength
= MainAVIHdr
.dwTotalFrames
;
1657 This
->fInfo
.dwStreams
= MainAVIHdr
.dwStreams
;
1658 This
->fInfo
.dwSuggestedBufferSize
= MainAVIHdr
.dwSuggestedBufferSize
;
1659 This
->fInfo
.dwWidth
= MainAVIHdr
.dwWidth
;
1660 This
->fInfo
.dwHeight
= MainAVIHdr
.dwHeight
;
1661 LoadStringW(AVIFILE_hModule
, IDS_AVIFILETYPE
, This
->fInfo
.szFileType
,
1662 sizeof(This
->fInfo
.szFileType
));
1664 /* go back to into header list */
1665 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1666 return AVIERR_FILEREAD
;
1668 /* foreach stream exists a "LIST","strl" chunk */
1669 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
1670 /* get next nested chunk in this "LIST","strl" */
1671 if (mmioDescend(This
->hmmio
, &ckLIST2
, &ckLIST1
, 0) != S_OK
)
1672 return AVIERR_FILEREAD
;
1674 /* nested chunk must be of type "LIST","strl" -- when not normally JUNK */
1675 if (ckLIST2
.ckid
== FOURCC_LIST
&&
1676 ckLIST2
.fccType
== listtypeSTREAMHEADER
) {
1677 pStream
= This
->ppStreams
[nStream
] =
1678 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAVIStreamImpl
));
1679 if (pStream
== NULL
)
1680 return AVIERR_MEMORY
;
1681 AVIFILE_ConstructAVIStream(This
, nStream
, NULL
);
1684 while (mmioDescend(This
->hmmio
, &ck
, &ckLIST2
, 0) == S_OK
) {
1686 case ckidSTREAMHANDLERDATA
:
1687 if (pStream
->lpHandlerData
!= NULL
)
1688 return AVIERR_BADFORMAT
;
1689 pStream
->lpHandlerData
= HeapAlloc(GetProcessHeap(), 0, ck
.cksize
);
1690 if (pStream
->lpHandlerData
== NULL
)
1691 return AVIERR_MEMORY
;
1692 pStream
->cbHandlerData
= ck
.cksize
;
1694 if (mmioRead(This
->hmmio
, (HPSTR
)pStream
->lpHandlerData
, ck
.cksize
) != ck
.cksize
)
1695 return AVIERR_FILEREAD
;
1697 case ckidSTREAMFORMAT
:
1698 if (pStream
->lpFormat
!= NULL
)
1699 return AVIERR_BADFORMAT
;
1703 pStream
->lpFormat
= HeapAlloc(GetProcessHeap(), 0, ck
.cksize
);
1704 if (pStream
->lpFormat
== NULL
)
1705 return AVIERR_MEMORY
;
1706 pStream
->cbFormat
= ck
.cksize
;
1708 if (mmioRead(This
->hmmio
, (HPSTR
)pStream
->lpFormat
, ck
.cksize
) != ck
.cksize
)
1709 return AVIERR_FILEREAD
;
1711 if (pStream
->sInfo
.fccType
== streamtypeVIDEO
) {
1712 LPBITMAPINFOHEADER lpbi
= (LPBITMAPINFOHEADER
)pStream
->lpFormat
;
1714 /* some corrections to the video format */
1715 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
1716 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
1717 if (lpbi
->biCompression
== BI_RGB
&& lpbi
->biSizeImage
== 0)
1718 lpbi
->biSizeImage
= DIBWIDTHBYTES(*lpbi
) * lpbi
->biHeight
;
1719 if (lpbi
->biCompression
!= BI_RGB
&& lpbi
->biBitCount
== 8) {
1720 if (pStream
->sInfo
.fccHandler
== mmioFOURCC('R','L','E','0') ||
1721 pStream
->sInfo
.fccHandler
== mmioFOURCC('R','L','E',' '))
1722 lpbi
->biCompression
= BI_RLE8
;
1724 if (lpbi
->biCompression
== BI_RGB
&&
1725 (pStream
->sInfo
.fccHandler
== 0 ||
1726 pStream
->sInfo
.fccHandler
== mmioFOURCC('N','O','N','E')))
1727 pStream
->sInfo
.fccHandler
= comptypeDIB
;
1729 /* init rcFrame if it's empty */
1730 if (IsRectEmpty(&pStream
->sInfo
.rcFrame
))
1731 SetRect(&pStream
->sInfo
.rcFrame
, 0, 0, lpbi
->biWidth
, lpbi
->biHeight
);
1734 case ckidSTREAMHEADER
:
1736 static const WCHAR streamTypeFmt
[] = {'%','4','.','4','h','s',0};
1738 AVIStreamHeader streamHdr
;
1740 WCHAR streamNameFmt
[25];
1744 if (ck
.cksize
> sizeof(streamHdr
))
1745 n
= sizeof(streamHdr
);
1747 if (mmioRead(This
->hmmio
, (HPSTR
)&streamHdr
, n
) != n
)
1748 return AVIERR_FILEREAD
;
1750 pStream
->sInfo
.fccType
= streamHdr
.fccType
;
1751 pStream
->sInfo
.fccHandler
= streamHdr
.fccHandler
;
1752 pStream
->sInfo
.dwFlags
= streamHdr
.dwFlags
;
1753 pStream
->sInfo
.wPriority
= streamHdr
.wPriority
;
1754 pStream
->sInfo
.wLanguage
= streamHdr
.wLanguage
;
1755 pStream
->sInfo
.dwInitialFrames
= streamHdr
.dwInitialFrames
;
1756 pStream
->sInfo
.dwScale
= streamHdr
.dwScale
;
1757 pStream
->sInfo
.dwRate
= streamHdr
.dwRate
;
1758 pStream
->sInfo
.dwStart
= streamHdr
.dwStart
;
1759 pStream
->sInfo
.dwLength
= streamHdr
.dwLength
;
1760 pStream
->sInfo
.dwSuggestedBufferSize
=
1761 streamHdr
.dwSuggestedBufferSize
;
1762 pStream
->sInfo
.dwQuality
= streamHdr
.dwQuality
;
1763 pStream
->sInfo
.dwSampleSize
= streamHdr
.dwSampleSize
;
1764 pStream
->sInfo
.rcFrame
.left
= streamHdr
.rcFrame
.left
;
1765 pStream
->sInfo
.rcFrame
.top
= streamHdr
.rcFrame
.top
;
1766 pStream
->sInfo
.rcFrame
.right
= streamHdr
.rcFrame
.right
;
1767 pStream
->sInfo
.rcFrame
.bottom
= streamHdr
.rcFrame
.bottom
;
1768 pStream
->sInfo
.dwEditCount
= 0;
1769 pStream
->sInfo
.dwFormatChangeCount
= 0;
1771 /* generate description for stream like "filename.avi Type #n" */
1772 if (streamHdr
.fccType
== streamtypeVIDEO
)
1773 LoadStringW(AVIFILE_hModule
, IDS_VIDEO
, szType
, sizeof(szType
));
1774 else if (streamHdr
.fccType
== streamtypeAUDIO
)
1775 LoadStringW(AVIFILE_hModule
, IDS_AUDIO
, szType
, sizeof(szType
));
1777 wsprintfW(szType
, streamTypeFmt
, (char*)&streamHdr
.fccType
);
1779 /* get count of this streamtype up to this stream */
1781 for (n
= nStream
; 0 <= n
; n
--) {
1782 if (This
->ppStreams
[n
]->sInfo
.fccHandler
== streamHdr
.fccType
)
1786 memset(pStream
->sInfo
.szName
, 0, sizeof(pStream
->sInfo
.szName
));
1788 LoadStringW(AVIFILE_hModule
, IDS_AVISTREAMFORMAT
, streamNameFmt
, sizeof(streamNameFmt
));
1790 /* FIXME: avoid overflow -- better use wsnprintfW, which doesn't exists ! */
1791 wsprintfW(pStream
->sInfo
.szName
, streamNameFmt
,
1792 AVIFILE_BasenameW(This
->szFileName
), szType
, count
);
1795 case ckidSTREAMNAME
:
1796 { /* streamname will be saved as ASCII string */
1797 LPSTR str
= HeapAlloc(GetProcessHeap(), 0, ck
.cksize
);
1799 return AVIERR_MEMORY
;
1801 if (mmioRead(This
->hmmio
, (HPSTR
)str
, ck
.cksize
) != ck
.cksize
)
1803 HeapFree(GetProcessHeap(), 0, str
);
1804 return AVIERR_FILEREAD
;
1807 MultiByteToWideChar(CP_ACP
, 0, str
, -1, pStream
->sInfo
.szName
,
1808 sizeof(pStream
->sInfo
.szName
)/sizeof(pStream
->sInfo
.szName
[0]));
1810 HeapFree(GetProcessHeap(), 0, str
);
1813 case ckidAVIPADDING
:
1814 case mmioFOURCC('p','a','d','d'):
1817 WARN(": found extra chunk 0x%08lX\n", ck
.ckid
);
1818 hr
= ReadChunkIntoExtra(&pStream
->extra
, This
->hmmio
, &ck
);
1823 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1824 return AVIERR_FILEREAD
;
1827 /* nested chunks in "LIST","hdrl" which are not of type "LIST","strl" */
1828 hr
= ReadChunkIntoExtra(&This
->fileextra
, This
->hmmio
, &ckLIST2
);
1832 if (mmioAscend(This
->hmmio
, &ckLIST2
, 0) != S_OK
)
1833 return AVIERR_FILEREAD
;
1836 /* read any extra headers in "LIST","hdrl" */
1837 FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ck
, &ckLIST1
, 0);
1838 if (mmioAscend(This
->hmmio
, &ckLIST1
, 0) != S_OK
)
1839 return AVIERR_FILEREAD
;
1841 /* search "LIST","movi" chunk in "RIFF","AVI " */
1842 ckLIST1
.fccType
= listtypeAVIMOVIE
;
1843 hr
= FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ckLIST1
, &ckRIFF
,
1848 This
->dwMoviChunkPos
= ckLIST1
.dwDataOffset
;
1849 This
->dwIdxChunkPos
= ckLIST1
.cksize
+ ckLIST1
.dwDataOffset
;
1850 if (mmioAscend(This
->hmmio
, &ckLIST1
, 0) != S_OK
)
1851 return AVIERR_FILEREAD
;
1853 /* try to find an index */
1854 ck
.ckid
= ckidAVINEWINDEX
;
1855 hr
= FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
,
1856 &ck
, &ckRIFF
, MMIO_FINDCHUNK
);
1857 if (SUCCEEDED(hr
) && ck
.cksize
> 0) {
1858 if (FAILED(AVIFILE_LoadIndex(This
, ck
.cksize
, ckLIST1
.dwDataOffset
)))
1859 This
->fInfo
.dwFlags
&= ~AVIFILEINFO_HASINDEX
;
1862 /* when we haven't found an index or it's bad, then build one
1863 * by parsing 'movi' chunk */
1864 if ((This
->fInfo
.dwFlags
& AVIFILEINFO_HASINDEX
) == 0) {
1865 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++)
1866 This
->ppStreams
[nStream
]->lLastFrame
= -1;
1868 if (mmioSeek(This
->hmmio
, ckLIST1
.dwDataOffset
+ sizeof(DWORD
), SEEK_SET
) == -1) {
1869 ERR(": Oops, can't seek back to 'movi' chunk!\n");
1870 return AVIERR_FILEREAD
;
1873 /* seek through the 'movi' list until end */
1874 while (mmioDescend(This
->hmmio
, &ck
, &ckLIST1
, 0) == S_OK
) {
1875 if (ck
.ckid
!= FOURCC_LIST
) {
1876 if (mmioAscend(This
->hmmio
, &ck
, 0) == S_OK
) {
1877 nStream
= StreamFromFOURCC(ck
.ckid
);
1879 if (nStream
> This
->fInfo
.dwStreams
)
1880 return AVIERR_BADFORMAT
;
1882 AVIFILE_AddFrame(This
->ppStreams
[nStream
], ck
.ckid
, ck
.cksize
,
1883 ck
.dwDataOffset
- 2 * sizeof(DWORD
), 0);
1885 nStream
= StreamFromFOURCC(ck
.ckid
);
1886 WARN(": file seems to be truncated!\n");
1887 if (nStream
<= This
->fInfo
.dwStreams
&&
1888 This
->ppStreams
[nStream
]->sInfo
.dwSampleSize
> 0) {
1889 ck
.cksize
= mmioSeek(This
->hmmio
, 0, SEEK_END
);
1890 if (ck
.cksize
!= -1) {
1891 ck
.cksize
-= ck
.dwDataOffset
;
1892 ck
.cksize
&= ~(This
->ppStreams
[nStream
]->sInfo
.dwSampleSize
- 1);
1894 AVIFILE_AddFrame(This
->ppStreams
[nStream
], ck
.ckid
, ck
.cksize
,
1895 ck
.dwDataOffset
- 2 * sizeof(DWORD
), 0);
1903 /* find other chunks */
1904 FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ck
, &ckRIFF
, 0);
1909 static HRESULT
AVIFILE_LoadIndex(IAVIFileImpl
*This
, DWORD size
, DWORD offset
)
1913 HRESULT hr
= AVIERR_OK
;
1914 BOOL bAbsolute
= TRUE
;
1916 lp
= HeapAlloc(GetProcessHeap(), 0, IDX_PER_BLOCK
* sizeof(AVIINDEXENTRY
));
1918 return AVIERR_MEMORY
;
1920 /* adjust limits for index tables, so that inserting will be faster */
1921 for (n
= 0; n
< This
->fInfo
.dwStreams
; n
++) {
1922 IAVIStreamImpl
*pStream
= This
->ppStreams
[n
];
1924 pStream
->lLastFrame
= -1;
1926 if (pStream
->idxFrames
!= NULL
) {
1927 HeapFree(GetProcessHeap(), 0, pStream
->idxFrames
);
1928 pStream
->idxFrames
= NULL
;
1929 pStream
->nIdxFrames
= 0;
1932 if (pStream
->sInfo
.dwSampleSize
!= 0) {
1933 if (n
> 0 && This
->fInfo
.dwFlags
& AVIFILEINFO_ISINTERLEAVED
) {
1934 pStream
->nIdxFrames
= This
->ppStreams
[0]->nIdxFrames
;
1935 } else if (pStream
->sInfo
.dwSuggestedBufferSize
) {
1936 pStream
->nIdxFrames
=
1937 pStream
->sInfo
.dwLength
/ pStream
->sInfo
.dwSuggestedBufferSize
;
1940 pStream
->nIdxFrames
= pStream
->sInfo
.dwLength
;
1942 pStream
->idxFrames
=
1943 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, pStream
->nIdxFrames
* sizeof(AVIINDEXENTRY
));
1944 if (pStream
->idxFrames
== NULL
&& pStream
->nIdxFrames
> 0) {
1945 pStream
->nIdxFrames
= 0;
1946 return AVIERR_MEMORY
;
1952 LONG read
= min(IDX_PER_BLOCK
* sizeof(AVIINDEXENTRY
), size
);
1954 if (mmioRead(This
->hmmio
, (HPSTR
)lp
, read
) != read
) {
1955 hr
= AVIERR_FILEREAD
;
1960 if (pos
== (DWORD
)-1)
1961 pos
= offset
- lp
->dwChunkOffset
+ sizeof(DWORD
);
1963 AVIFILE_ParseIndex(This
, lp
, read
/ sizeof(AVIINDEXENTRY
),
1967 HeapFree(GetProcessHeap(), 0, lp
);
1970 for (n
= 0; n
< This
->fInfo
.dwStreams
; n
++) {
1971 IAVIStreamImpl
*pStream
= This
->ppStreams
[n
];
1973 if (pStream
->sInfo
.dwSampleSize
== 0 &&
1974 pStream
->sInfo
.dwLength
!= pStream
->lLastFrame
+1)
1975 ERR("stream %lu length mismatch: dwLength=%lu found=%ld\n",
1976 n
, pStream
->sInfo
.dwLength
, pStream
->lLastFrame
);
1982 static HRESULT
AVIFILE_ParseIndex(IAVIFileImpl
*This
, AVIINDEXENTRY
*lp
,
1983 LONG count
, DWORD pos
, BOOL
*bAbsolute
)
1986 return AVIERR_BADPARAM
;
1988 for (; count
> 0; count
--, lp
++) {
1989 WORD nStream
= StreamFromFOURCC(lp
->ckid
);
1991 if (lp
->ckid
== listtypeAVIRECORD
|| nStream
== 0x7F)
1992 continue; /* skip these */
1994 if (nStream
> This
->fInfo
.dwStreams
)
1995 return AVIERR_BADFORMAT
;
1997 if (*bAbsolute
&& lp
->dwChunkOffset
< This
->dwMoviChunkPos
)
2001 lp
->dwChunkOffset
+= sizeof(DWORD
);
2003 lp
->dwChunkOffset
+= pos
;
2005 if (FAILED(AVIFILE_AddFrame(This
->ppStreams
[nStream
], lp
->ckid
, lp
->dwChunkLength
, lp
->dwChunkOffset
, lp
->dwFlags
)))
2006 return AVIERR_MEMORY
;
2012 static HRESULT
AVIFILE_ReadBlock(IAVIStreamImpl
*This
, DWORD pos
,
2013 LPVOID buffer
, LONG size
)
2015 /* pre-conditions */
2016 assert(This
!= NULL
);
2017 assert(This
->paf
!= NULL
);
2018 assert(This
->paf
->hmmio
!= NULL
);
2019 assert(This
->sInfo
.dwStart
<= pos
&& pos
< This
->sInfo
.dwLength
);
2020 assert(pos
<= This
->lLastFrame
);
2022 /* should we read as much as block gives us? */
2023 if (size
== 0 || size
> This
->idxFrames
[pos
].dwChunkLength
)
2024 size
= This
->idxFrames
[pos
].dwChunkLength
;
2026 /* read into out own buffer or given one? */
2027 if (buffer
== NULL
) {
2028 /* we also read the chunk */
2029 size
+= 2 * sizeof(DWORD
);
2031 /* check that buffer is big enough -- don't trust dwSuggestedBufferSize */
2032 if (This
->lpBuffer
== NULL
|| size
< This
->cbBuffer
) {
2033 DWORD maxSize
= max(size
, This
->sInfo
.dwSuggestedBufferSize
);
2035 if (This
->lpBuffer
== NULL
)
2036 This
->lpBuffer
= HeapAlloc(GetProcessHeap(), 0, maxSize
);
2038 This
->lpBuffer
= HeapReAlloc(GetProcessHeap(), 0, This
->lpBuffer
, maxSize
);
2039 if (This
->lpBuffer
== NULL
)
2040 return AVIERR_MEMORY
;
2041 This
->cbBuffer
= max(size
, This
->sInfo
.dwSuggestedBufferSize
);
2044 /* now read the complete chunk into our buffer */
2045 if (mmioSeek(This
->paf
->hmmio
, This
->idxFrames
[pos
].dwChunkOffset
, SEEK_SET
) == -1)
2046 return AVIERR_FILEREAD
;
2047 if (mmioRead(This
->paf
->hmmio
, (HPSTR
)This
->lpBuffer
, size
) != size
)
2048 return AVIERR_FILEREAD
;
2050 /* check if it was the correct block which we have read */
2051 if (This
->lpBuffer
[0] != This
->idxFrames
[pos
].ckid
||
2052 This
->lpBuffer
[1] != This
->idxFrames
[pos
].dwChunkLength
) {
2053 ERR(": block %ld not found at 0x%08lX\n", pos
, This
->idxFrames
[pos
].dwChunkOffset
);
2054 ERR(": Index says: '%4.4s'(0x%08lX) size 0x%08lX\n",
2055 (char*)&This
->idxFrames
[pos
].ckid
, This
->idxFrames
[pos
].ckid
,
2056 This
->idxFrames
[pos
].dwChunkLength
);
2057 ERR(": Data says: '%4.4s'(0x%08lX) size 0x%08lX\n",
2058 (char*)&This
->lpBuffer
[0], This
->lpBuffer
[0], This
->lpBuffer
[1]);
2059 return AVIERR_FILEREAD
;
2062 if (mmioSeek(This
->paf
->hmmio
, This
->idxFrames
[pos
].dwChunkOffset
+ 2 * sizeof(DWORD
), SEEK_SET
) == -1)
2063 return AVIERR_FILEREAD
;
2064 if (mmioRead(This
->paf
->hmmio
, (HPSTR
)buffer
, size
) != size
)
2065 return AVIERR_FILEREAD
;
2071 static void AVIFILE_SamplesToBlock(IAVIStreamImpl
*This
, LPLONG pos
,
2076 /* pre-conditions */
2077 assert(This
!= NULL
);
2078 assert(pos
!= NULL
);
2079 assert(offset
!= NULL
);
2080 assert(This
->sInfo
.dwSampleSize
!= 0);
2081 assert(*pos
>= This
->sInfo
.dwStart
);
2083 /* convert start sample to start bytes */
2084 (*offset
) = (*pos
) - This
->sInfo
.dwStart
;
2085 (*offset
) *= This
->sInfo
.dwSampleSize
;
2087 /* convert bytes to block number */
2088 for (block
= 0; block
<= This
->lLastFrame
; block
++) {
2089 if (This
->idxFrames
[block
].dwChunkLength
<= *offset
)
2090 (*offset
) -= This
->idxFrames
[block
].dwChunkLength
;
2098 static HRESULT
AVIFILE_SaveFile(IAVIFileImpl
*This
)
2100 MainAVIHeader MainAVIHdr
;
2101 IAVIStreamImpl
* pStream
;
2110 /* initialize some things */
2111 if (This
->dwMoviChunkPos
== 0)
2112 AVIFILE_ComputeMoviStart(This
);
2114 /* written one record to much? */
2115 if (This
->ckLastRecord
.dwFlags
& MMIO_DIRTY
) {
2116 This
->dwNextFramePos
-= 3 * sizeof(DWORD
);
2117 if (This
->nIdxRecords
> 0)
2118 This
->nIdxRecords
--;
2121 AVIFILE_UpdateInfo(This
);
2123 assert(This
->fInfo
.dwScale
!= 0);
2125 memset(&MainAVIHdr
, 0, sizeof(MainAVIHdr
));
2126 MainAVIHdr
.dwMicroSecPerFrame
= MulDiv(This
->fInfo
.dwRate
, 1000000,
2127 This
->fInfo
.dwScale
);
2128 MainAVIHdr
.dwMaxBytesPerSec
= This
->fInfo
.dwMaxBytesPerSec
;
2129 MainAVIHdr
.dwPaddingGranularity
= AVI_HEADERSIZE
;
2130 MainAVIHdr
.dwFlags
= This
->fInfo
.dwFlags
;
2131 MainAVIHdr
.dwTotalFrames
= This
->fInfo
.dwLength
;
2132 MainAVIHdr
.dwInitialFrames
= 0;
2133 MainAVIHdr
.dwStreams
= This
->fInfo
.dwStreams
;
2134 MainAVIHdr
.dwSuggestedBufferSize
= This
->fInfo
.dwSuggestedBufferSize
;
2135 MainAVIHdr
.dwWidth
= This
->fInfo
.dwWidth
;
2136 MainAVIHdr
.dwHeight
= This
->fInfo
.dwHeight
;
2137 MainAVIHdr
.dwInitialFrames
= This
->dwInitialFrames
;
2139 /* now begin writing ... */
2140 mmioSeek(This
->hmmio
, 0, SEEK_SET
);
2144 ckRIFF
.fccType
= formtypeAVI
;
2145 if (mmioCreateChunk(This
->hmmio
, &ckRIFF
, MMIO_CREATERIFF
) != S_OK
)
2146 return AVIERR_FILEWRITE
;
2148 /* AVI headerlist */
2150 ckLIST1
.fccType
= listtypeAVIHEADER
;
2151 if (mmioCreateChunk(This
->hmmio
, &ckLIST1
, MMIO_CREATELIST
) != S_OK
)
2152 return AVIERR_FILEWRITE
;
2155 ck
.ckid
= ckidAVIMAINHDR
;
2156 ck
.cksize
= sizeof(MainAVIHdr
);
2158 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2159 return AVIERR_FILEWRITE
;
2160 if (mmioWrite(This
->hmmio
, (HPSTR
)&MainAVIHdr
, ck
.cksize
) != ck
.cksize
)
2161 return AVIERR_FILEWRITE
;
2162 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2163 return AVIERR_FILEWRITE
;
2165 /* write the headers of each stream into a separate streamheader list */
2166 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
2167 AVIStreamHeader strHdr
;
2169 pStream
= This
->ppStreams
[nStream
];
2171 /* begin the new streamheader list */
2173 ckLIST2
.fccType
= listtypeSTREAMHEADER
;
2174 if (mmioCreateChunk(This
->hmmio
, &ckLIST2
, MMIO_CREATELIST
) != S_OK
)
2175 return AVIERR_FILEWRITE
;
2177 /* create an AVIStreamHeader from the AVSTREAMINFO */
2178 strHdr
.fccType
= pStream
->sInfo
.fccType
;
2179 strHdr
.fccHandler
= pStream
->sInfo
.fccHandler
;
2180 strHdr
.dwFlags
= pStream
->sInfo
.dwFlags
;
2181 strHdr
.wPriority
= pStream
->sInfo
.wPriority
;
2182 strHdr
.wLanguage
= pStream
->sInfo
.wLanguage
;
2183 strHdr
.dwInitialFrames
= pStream
->sInfo
.dwInitialFrames
;
2184 strHdr
.dwScale
= pStream
->sInfo
.dwScale
;
2185 strHdr
.dwRate
= pStream
->sInfo
.dwRate
;
2186 strHdr
.dwStart
= pStream
->sInfo
.dwStart
;
2187 strHdr
.dwLength
= pStream
->sInfo
.dwLength
;
2188 strHdr
.dwSuggestedBufferSize
= pStream
->sInfo
.dwSuggestedBufferSize
;
2189 strHdr
.dwQuality
= pStream
->sInfo
.dwQuality
;
2190 strHdr
.dwSampleSize
= pStream
->sInfo
.dwSampleSize
;
2191 strHdr
.rcFrame
.left
= pStream
->sInfo
.rcFrame
.left
;
2192 strHdr
.rcFrame
.top
= pStream
->sInfo
.rcFrame
.top
;
2193 strHdr
.rcFrame
.right
= pStream
->sInfo
.rcFrame
.right
;
2194 strHdr
.rcFrame
.bottom
= pStream
->sInfo
.rcFrame
.bottom
;
2196 /* now write the AVIStreamHeader */
2197 ck
.ckid
= ckidSTREAMHEADER
;
2198 ck
.cksize
= sizeof(strHdr
);
2199 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2200 return AVIERR_FILEWRITE
;
2201 if (mmioWrite(This
->hmmio
, (HPSTR
)&strHdr
, ck
.cksize
) != ck
.cksize
)
2202 return AVIERR_FILEWRITE
;
2203 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2204 return AVIERR_FILEWRITE
;
2206 /* ... the hopefully ever present streamformat ... */
2207 ck
.ckid
= ckidSTREAMFORMAT
;
2208 ck
.cksize
= pStream
->cbFormat
;
2209 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2210 return AVIERR_FILEWRITE
;
2211 if (pStream
->lpFormat
!= NULL
&& ck
.cksize
> 0) {
2212 if (mmioWrite(This
->hmmio
, (HPSTR
)pStream
->lpFormat
, ck
.cksize
) != ck
.cksize
)
2213 return AVIERR_FILEWRITE
;
2215 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2216 return AVIERR_FILEWRITE
;
2218 /* ... some optional existing handler data ... */
2219 if (pStream
->lpHandlerData
!= NULL
&& pStream
->cbHandlerData
> 0) {
2220 ck
.ckid
= ckidSTREAMHANDLERDATA
;
2221 ck
.cksize
= pStream
->cbHandlerData
;
2222 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2223 return AVIERR_FILEWRITE
;
2224 if (mmioWrite(This
->hmmio
, (HPSTR
)pStream
->lpHandlerData
, ck
.cksize
) != ck
.cksize
)
2225 return AVIERR_FILEWRITE
;
2226 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2227 return AVIERR_FILEWRITE
;
2230 /* ... some optional additional extra chunk for this stream ... */
2231 if (pStream
->extra
.lp
!= NULL
&& pStream
->extra
.cb
> 0) {
2232 /* the chunk header(s) are already in the strucuture */
2233 if (mmioWrite(This
->hmmio
, (HPSTR
)pStream
->extra
.lp
, pStream
->extra
.cb
) != pStream
->extra
.cb
)
2234 return AVIERR_FILEWRITE
;
2237 /* ... an optional name for this stream ... */
2238 if (lstrlenW(pStream
->sInfo
.szName
) > 0) {
2241 ck
.ckid
= ckidSTREAMNAME
;
2242 ck
.cksize
= lstrlenW(pStream
->sInfo
.szName
) + 1;
2243 if (ck
.cksize
& 1) /* align */
2245 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2246 return AVIERR_FILEWRITE
;
2248 /* the streamname must be saved in ASCII not Unicode */
2249 str
= HeapAlloc(GetProcessHeap(), 0, ck
.cksize
);
2251 return AVIERR_MEMORY
;
2252 WideCharToMultiByte(CP_ACP
, 0, pStream
->sInfo
.szName
, -1, str
,
2253 ck
.cksize
, NULL
, NULL
);
2255 if (mmioWrite(This
->hmmio
, (HPSTR
)str
, ck
.cksize
) != ck
.cksize
) {
2256 HeapFree(GetProcessHeap(), 0, str
);
2257 return AVIERR_FILEWRITE
;
2260 HeapFree(GetProcessHeap(), 0, str
);
2261 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2262 return AVIERR_FILEWRITE
;
2265 /* close streamheader list for this stream */
2266 if (mmioAscend(This
->hmmio
, &ckLIST2
, 0) != S_OK
)
2267 return AVIERR_FILEWRITE
;
2268 } /* for (0 <= nStream < MainAVIHdr.dwStreams) */
2270 /* close the aviheader list */
2271 if (mmioAscend(This
->hmmio
, &ckLIST1
, 0) != S_OK
)
2272 return AVIERR_FILEWRITE
;
2274 /* check for padding to pre-guessed 'movi'-chunk position */
2275 dwPos
= ckLIST1
.dwDataOffset
+ ckLIST1
.cksize
;
2276 if (This
->dwMoviChunkPos
- 2 * sizeof(DWORD
) > dwPos
) {
2277 ck
.ckid
= ckidAVIPADDING
;
2278 ck
.cksize
= This
->dwMoviChunkPos
- dwPos
- 4 * sizeof(DWORD
);
2279 assert((LONG
)ck
.cksize
>= 0);
2281 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2282 return AVIERR_FILEWRITE
;
2283 if (mmioSeek(This
->hmmio
, ck
.cksize
, SEEK_CUR
) == -1)
2284 return AVIERR_FILEWRITE
;
2285 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2286 return AVIERR_FILEWRITE
;
2289 /* now write the 'movi' chunk */
2290 mmioSeek(This
->hmmio
, This
->dwMoviChunkPos
- 2 * sizeof(DWORD
), SEEK_SET
);
2292 ckLIST1
.fccType
= listtypeAVIMOVIE
;
2293 if (mmioCreateChunk(This
->hmmio
, &ckLIST1
, MMIO_CREATELIST
) != S_OK
)
2294 return AVIERR_FILEWRITE
;
2295 if (mmioSeek(This
->hmmio
, This
->dwNextFramePos
, SEEK_SET
) == -1)
2296 return AVIERR_FILEWRITE
;
2297 if (mmioAscend(This
->hmmio
, &ckLIST1
, 0) != S_OK
)
2298 return AVIERR_FILEWRITE
;
2300 /* write 'idx1' chunk */
2301 hr
= AVIFILE_SaveIndex(This
);
2305 /* write optional extra file chunks */
2306 if (This
->fileextra
.lp
!= NULL
&& This
->fileextra
.cb
> 0) {
2307 /* as for the streams, are the chunk header(s) in the structure */
2308 if (mmioWrite(This
->hmmio
, (HPSTR
)This
->fileextra
.lp
, This
->fileextra
.cb
) != This
->fileextra
.cb
)
2309 return AVIERR_FILEWRITE
;
2312 /* close RIFF chunk */
2313 if (mmioAscend(This
->hmmio
, &ckRIFF
, 0) != S_OK
)
2314 return AVIERR_FILEWRITE
;
2316 /* add some JUNK at end for bad parsers */
2317 memset(&ckRIFF
, 0, sizeof(ckRIFF
));
2318 mmioWrite(This
->hmmio
, (HPSTR
)&ckRIFF
, sizeof(ckRIFF
));
2319 mmioFlush(This
->hmmio
, 0);
2324 static HRESULT
AVIFILE_SaveIndex(IAVIFileImpl
*This
)
2326 IAVIStreamImpl
*pStream
;
2332 ck
.ckid
= ckidAVINEWINDEX
;
2334 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2335 return AVIERR_FILEWRITE
;
2337 if (This
->fInfo
.dwFlags
& AVIFILEINFO_ISINTERLEAVED
) {
2338 /* is interleaved -- write block of coresponding frames */
2339 LONG lInitialFrames
= 0;
2343 if (This
->ppStreams
[0]->sInfo
.dwSampleSize
== 0)
2346 stepsize
= AVIStreamTimeToSample((PAVISTREAM
)This
->ppStreams
[0], 1000000);
2348 assert(stepsize
> 0);
2350 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
2351 if (lInitialFrames
< This
->ppStreams
[nStream
]->sInfo
.dwInitialFrames
)
2352 lInitialFrames
= This
->ppStreams
[nStream
]->sInfo
.dwInitialFrames
;
2355 for (i
= -lInitialFrames
; i
< (LONG
)This
->fInfo
.dwLength
- lInitialFrames
;
2357 DWORD nFrame
= lInitialFrames
+ i
;
2359 assert(nFrame
< This
->nIdxRecords
);
2361 idx
.ckid
= listtypeAVIRECORD
;
2362 idx
.dwFlags
= AVIIF_LIST
;
2363 idx
.dwChunkLength
= This
->idxRecords
[nFrame
].dwChunkLength
;
2364 idx
.dwChunkOffset
= This
->idxRecords
[nFrame
].dwChunkOffset
2365 - This
->dwMoviChunkPos
;
2366 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2367 return AVIERR_FILEWRITE
;
2369 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
2370 pStream
= This
->ppStreams
[nStream
];
2372 /* heave we reached start of this stream? */
2373 if (-(LONG
)pStream
->sInfo
.dwInitialFrames
> i
)
2376 if (pStream
->sInfo
.dwInitialFrames
< lInitialFrames
)
2377 nFrame
-= (lInitialFrames
- pStream
->sInfo
.dwInitialFrames
);
2379 /* reached end of this stream? */
2380 if (pStream
->lLastFrame
<= nFrame
)
2383 if ((pStream
->sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
) &&
2384 pStream
->sInfo
.dwFormatChangeCount
!= 0 &&
2385 pStream
->idxFmtChanges
!= NULL
) {
2388 for (pos
= 0; pos
< pStream
->sInfo
.dwFormatChangeCount
; pos
++) {
2389 if (pStream
->idxFmtChanges
[pos
].ckid
== nFrame
) {
2390 idx
.dwFlags
= AVIIF_NOTIME
;
2391 idx
.ckid
= MAKEAVICKID(cktypePALchange
, pStream
->nStream
);
2392 idx
.dwChunkLength
= pStream
->idxFmtChanges
[pos
].dwChunkLength
;
2393 idx
.dwChunkOffset
= pStream
->idxFmtChanges
[pos
].dwChunkOffset
2394 - This
->dwMoviChunkPos
;
2396 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2397 return AVIERR_FILEWRITE
;
2401 } /* if have formatchanges */
2403 idx
.ckid
= pStream
->idxFrames
[nFrame
].ckid
;
2404 idx
.dwFlags
= pStream
->idxFrames
[nFrame
].dwFlags
;
2405 idx
.dwChunkLength
= pStream
->idxFrames
[nFrame
].dwChunkLength
;
2406 idx
.dwChunkOffset
= pStream
->idxFrames
[nFrame
].dwChunkOffset
2407 - This
->dwMoviChunkPos
;
2408 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2409 return AVIERR_FILEWRITE
;
2413 /* not interleaved -- write index for each stream at once */
2414 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
2415 pStream
= This
->ppStreams
[nStream
];
2417 for (n
= 0; n
<= pStream
->lLastFrame
; n
++) {
2418 if ((pStream
->sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
) &&
2419 (pStream
->sInfo
.dwFormatChangeCount
!= 0)) {
2422 for (pos
= 0; pos
< pStream
->sInfo
.dwFormatChangeCount
; pos
++) {
2423 if (pStream
->idxFmtChanges
[pos
].ckid
== n
) {
2424 idx
.dwFlags
= AVIIF_NOTIME
;
2425 idx
.ckid
= MAKEAVICKID(cktypePALchange
, pStream
->nStream
);
2426 idx
.dwChunkLength
= pStream
->idxFmtChanges
[pos
].dwChunkLength
;
2428 pStream
->idxFmtChanges
[pos
].dwChunkOffset
- This
->dwMoviChunkPos
;
2429 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2430 return AVIERR_FILEWRITE
;
2434 } /* if have formatchanges */
2436 idx
.ckid
= pStream
->idxFrames
[n
].ckid
;
2437 idx
.dwFlags
= pStream
->idxFrames
[n
].dwFlags
;
2438 idx
.dwChunkLength
= pStream
->idxFrames
[n
].dwChunkLength
;
2439 idx
.dwChunkOffset
= pStream
->idxFrames
[n
].dwChunkOffset
2440 - This
->dwMoviChunkPos
;
2442 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2443 return AVIERR_FILEWRITE
;
2446 } /* if not interleaved */
2448 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2449 return AVIERR_FILEWRITE
;
2454 static ULONG
AVIFILE_SearchStream(IAVIFileImpl
*This
, DWORD fcc
, LONG lSkip
)
2463 /* search the number of the specified stream */
2464 nStream
= (ULONG
)-1;
2465 for (i
= 0; i
< This
->fInfo
.dwStreams
; i
++) {
2466 assert(This
->ppStreams
[i
] != NULL
);
2468 if (This
->ppStreams
[i
]->sInfo
.fccType
== fcc
) {
2482 static void AVIFILE_UpdateInfo(IAVIFileImpl
*This
)
2486 /* pre-conditions */
2487 assert(This
!= NULL
);
2489 This
->fInfo
.dwMaxBytesPerSec
= 0;
2490 This
->fInfo
.dwCaps
= AVIFILECAPS_CANREAD
|AVIFILECAPS_CANWRITE
;
2491 This
->fInfo
.dwSuggestedBufferSize
= 0;
2492 This
->fInfo
.dwWidth
= 0;
2493 This
->fInfo
.dwHeight
= 0;
2494 This
->fInfo
.dwScale
= 0;
2495 This
->fInfo
.dwRate
= 0;
2496 This
->fInfo
.dwLength
= 0;
2497 This
->dwInitialFrames
= 0;
2499 for (i
= 0; i
< This
->fInfo
.dwStreams
; i
++) {
2500 AVISTREAMINFOW
*psi
;
2503 /* pre-conditions */
2504 assert(This
->ppStreams
[i
] != NULL
);
2506 psi
= &This
->ppStreams
[i
]->sInfo
;
2507 assert(psi
->dwScale
!= 0);
2508 assert(psi
->dwRate
!= 0);
2511 /* use first stream timings as base */
2512 This
->fInfo
.dwScale
= psi
->dwScale
;
2513 This
->fInfo
.dwRate
= psi
->dwRate
;
2514 This
->fInfo
.dwLength
= psi
->dwLength
;
2516 n
= AVIStreamSampleToSample((PAVISTREAM
)This
->ppStreams
[0],
2517 (PAVISTREAM
)This
->ppStreams
[i
],psi
->dwLength
);
2518 if (This
->fInfo
.dwLength
< n
)
2519 This
->fInfo
.dwLength
= n
;
2522 if (This
->dwInitialFrames
< psi
->dwInitialFrames
)
2523 This
->dwInitialFrames
= psi
->dwInitialFrames
;
2525 if (This
->fInfo
.dwSuggestedBufferSize
< psi
->dwSuggestedBufferSize
)
2526 This
->fInfo
.dwSuggestedBufferSize
= psi
->dwSuggestedBufferSize
;
2528 if (psi
->dwSampleSize
!= 0) {
2529 /* fixed sample size -- exact computation */
2530 This
->fInfo
.dwMaxBytesPerSec
+= MulDiv(psi
->dwSampleSize
, psi
->dwRate
,
2533 /* variable sample size -- only upper limit */
2534 This
->fInfo
.dwMaxBytesPerSec
+= MulDiv(psi
->dwSuggestedBufferSize
,
2535 psi
->dwRate
, psi
->dwScale
);
2537 /* update dimensions */
2538 n
= psi
->rcFrame
.right
- psi
->rcFrame
.left
;
2539 if (This
->fInfo
.dwWidth
< n
)
2540 This
->fInfo
.dwWidth
= n
;
2541 n
= psi
->rcFrame
.bottom
- psi
->rcFrame
.top
;
2542 if (This
->fInfo
.dwHeight
< n
)
2543 This
->fInfo
.dwHeight
= n
;
2548 static HRESULT
AVIFILE_WriteBlock(IAVIStreamImpl
*This
, DWORD block
,
2549 FOURCC ckid
, DWORD flags
, LPVOID buffer
,
2558 /* if no frame/block is already written, we must compute start of movi chunk */
2559 if (This
->paf
->dwMoviChunkPos
== 0)
2560 AVIFILE_ComputeMoviStart(This
->paf
);
2562 if (mmioSeek(This
->paf
->hmmio
, This
->paf
->dwNextFramePos
, SEEK_SET
) == -1)
2563 return AVIERR_FILEWRITE
;
2565 if (mmioCreateChunk(This
->paf
->hmmio
, &ck
, 0) != S_OK
)
2566 return AVIERR_FILEWRITE
;
2567 if (buffer
!= NULL
&& size
> 0) {
2568 if (mmioWrite(This
->paf
->hmmio
, (HPSTR
)buffer
, size
) != size
)
2569 return AVIERR_FILEWRITE
;
2571 if (mmioAscend(This
->paf
->hmmio
, &ck
, 0) != S_OK
)
2572 return AVIERR_FILEWRITE
;
2574 This
->paf
->fDirty
= TRUE
;
2575 This
->paf
->dwNextFramePos
= mmioSeek(This
->paf
->hmmio
, 0, SEEK_CUR
);
2577 return AVIFILE_AddFrame(This
, ckid
, size
,
2578 ck
.dwDataOffset
- 2 * sizeof(DWORD
), flags
);