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
44 #include "avifile_private.h"
45 #include "extrachunk.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
52 #define IDX_PER_BLOCK 2730
55 /***********************************************************************/
57 static HRESULT WINAPI
IAVIFile_fnQueryInterface(IAVIFile
* iface
,REFIID refiid
,LPVOID
*obj
);
58 static ULONG WINAPI
IAVIFile_fnAddRef(IAVIFile
* iface
);
59 static ULONG WINAPI
IAVIFile_fnRelease(IAVIFile
* iface
);
60 static HRESULT WINAPI
IAVIFile_fnInfo(IAVIFile
*iface
,AVIFILEINFOW
*afi
,LONG size
);
61 static HRESULT WINAPI
IAVIFile_fnGetStream(IAVIFile
*iface
,PAVISTREAM
*avis
,DWORD fccType
,LONG lParam
);
62 static HRESULT WINAPI
IAVIFile_fnCreateStream(IAVIFile
*iface
,PAVISTREAM
*avis
,AVISTREAMINFOW
*asi
);
63 static HRESULT WINAPI
IAVIFile_fnWriteData(IAVIFile
*iface
,DWORD ckid
,LPVOID lpData
,LONG size
);
64 static HRESULT WINAPI
IAVIFile_fnReadData(IAVIFile
*iface
,DWORD ckid
,LPVOID lpData
,LONG
*size
);
65 static HRESULT WINAPI
IAVIFile_fnEndRecord(IAVIFile
*iface
);
66 static HRESULT WINAPI
IAVIFile_fnDeleteStream(IAVIFile
*iface
,DWORD fccType
,LONG lParam
);
68 struct ICOM_VTABLE(IAVIFile
) iavift
= {
69 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
70 IAVIFile_fnQueryInterface
,
75 IAVIFile_fnCreateStream
,
79 IAVIFile_fnDeleteStream
82 static HRESULT WINAPI
IPersistFile_fnQueryInterface(IPersistFile
*iface
,REFIID refiid
,LPVOID
*obj
);
83 static ULONG WINAPI
IPersistFile_fnAddRef(IPersistFile
*iface
);
84 static ULONG WINAPI
IPersistFile_fnRelease(IPersistFile
*iface
);
85 static HRESULT WINAPI
IPersistFile_fnGetClassID(IPersistFile
*iface
,CLSID
*pClassID
);
86 static HRESULT WINAPI
IPersistFile_fnIsDirty(IPersistFile
*iface
);
87 static HRESULT WINAPI
IPersistFile_fnLoad(IPersistFile
*iface
,LPCOLESTR pszFileName
,DWORD dwMode
);
88 static HRESULT WINAPI
IPersistFile_fnSave(IPersistFile
*iface
,LPCOLESTR pszFileName
,BOOL fRemember
);
89 static HRESULT WINAPI
IPersistFile_fnSaveCompleted(IPersistFile
*iface
,LPCOLESTR pszFileName
);
90 static HRESULT WINAPI
IPersistFile_fnGetCurFile(IPersistFile
*iface
,LPOLESTR
*ppszFileName
);
92 struct ICOM_VTABLE(IPersistFile
) ipersistft
= {
93 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
94 IPersistFile_fnQueryInterface
,
95 IPersistFile_fnAddRef
,
96 IPersistFile_fnRelease
,
97 IPersistFile_fnGetClassID
,
98 IPersistFile_fnIsDirty
,
101 IPersistFile_fnSaveCompleted
,
102 IPersistFile_fnGetCurFile
105 static HRESULT WINAPI
IAVIStream_fnQueryInterface(IAVIStream
*iface
,REFIID refiid
,LPVOID
*obj
);
106 static ULONG WINAPI
IAVIStream_fnAddRef(IAVIStream
*iface
);
107 static ULONG WINAPI
IAVIStream_fnRelease(IAVIStream
* iface
);
108 static HRESULT WINAPI
IAVIStream_fnCreate(IAVIStream
*iface
,LPARAM lParam1
,LPARAM lParam2
);
109 static HRESULT WINAPI
IAVIStream_fnInfo(IAVIStream
*iface
,AVISTREAMINFOW
*psi
,LONG size
);
110 static LONG WINAPI
IAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,LONG flags
);
111 static HRESULT WINAPI
IAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG
*formatsize
);
112 static HRESULT WINAPI
IAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG formatsize
);
113 static HRESULT WINAPI
IAVIStream_fnRead(IAVIStream
*iface
,LONG start
,LONG samples
,LPVOID buffer
,LONG buffersize
,LONG
*bytesread
,LONG
*samplesread
);
114 static HRESULT WINAPI
IAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,LONG samples
,LPVOID buffer
,LONG buffersize
,DWORD flags
,LONG
*sampwritten
,LONG
*byteswritten
);
115 static HRESULT WINAPI
IAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,LONG samples
);
116 static HRESULT WINAPI
IAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,LPVOID lp
,LONG
*lpread
);
117 static HRESULT WINAPI
IAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,LPVOID lp
,LONG size
);
118 static HRESULT WINAPI
IAVIStream_fnSetInfo(IAVIStream
*iface
,AVISTREAMINFOW
*info
,LONG infolen
);
120 struct ICOM_VTABLE(IAVIStream
) iavist
= {
121 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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 ICOM_VFIELD(IPersistFile
);
144 /* IPersistFile stuff */
148 typedef struct _IAVIStreamImpl
{
150 ICOM_VFIELD(IAVIStream
);
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 ICOM_VFIELD(IAVIFile
);
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
= (IAVIFileImpl
*)LocalAlloc(LPTR
, sizeof(IAVIFileImpl
));
244 return AVIERR_MEMORY
;
246 pfile
->lpVtbl
= &iavift
;
248 pfile
->iPersistFile
.lpVtbl
= &ipersistft
;
249 pfile
->iPersistFile
.paf
= pfile
;
251 hr
= IUnknown_QueryInterface((IUnknown
*)pfile
, riid
, ppv
);
253 LocalFree((HLOCAL
)pfile
);
258 static HRESULT WINAPI
IAVIFile_fnQueryInterface(IAVIFile
*iface
, REFIID refiid
,
261 ICOM_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 ICOM_THIS(IAVIFileImpl
,iface
);
285 TRACE("(%p) -> %ld\n", iface
, This
->ref
+ 1);
286 return ++(This
->ref
);
289 static ULONG WINAPI
IAVIFile_fnRelease(IAVIFile
*iface
)
291 ICOM_THIS(IAVIFileImpl
,iface
);
294 TRACE("(%p) -> %ld\n", iface
, This
->ref
- 1);
296 if (!--(This
->ref
)) {
298 /* need to write headers to file */
299 AVIFILE_SaveFile(This
);
302 for (i
= 0; i
< This
->fInfo
.dwStreams
; i
++) {
303 if (This
->ppStreams
[i
] != NULL
) {
304 if (This
->ppStreams
[i
]->ref
!= 0) {
305 ERR(": someone has still a reference to stream %u (%p)!\n",
306 i
, This
->ppStreams
[i
]);
308 AVIFILE_DestructAVIStream(This
->ppStreams
[i
]);
309 LocalFree((HLOCAL
)This
->ppStreams
[i
]);
310 This
->ppStreams
[i
] = NULL
;
314 if (This
->idxRecords
!= NULL
) {
315 GlobalFreePtr(This
->idxRecords
);
316 This
->idxRecords
= NULL
;
317 This
->nIdxRecords
= 0;
320 if (This
->fileextra
.lp
!= NULL
) {
321 GlobalFreePtr(This
->fileextra
.lp
);
322 This
->fileextra
.lp
= NULL
;
323 This
->fileextra
.cb
= 0;
326 if (This
->szFileName
!= NULL
) {
327 LocalFree((HLOCAL
)This
->szFileName
);
328 This
->szFileName
= NULL
;
330 if (This
->hmmio
!= NULL
) {
331 mmioClose(This
->hmmio
, 0);
335 LocalFree((HLOCAL
)This
);
341 static HRESULT WINAPI
IAVIFile_fnInfo(IAVIFile
*iface
, LPAVIFILEINFOW afi
,
344 ICOM_THIS(IAVIFileImpl
,iface
);
346 TRACE("(%p,%p,%ld)\n",iface
,afi
,size
);
349 return AVIERR_BADPARAM
;
351 return AVIERR_BADSIZE
;
353 AVIFILE_UpdateInfo(This
);
355 memcpy(afi
, &This
->fInfo
, min((DWORD
)size
, sizeof(This
->fInfo
)));
357 if ((DWORD
)size
< sizeof(This
->fInfo
))
358 return AVIERR_BUFFERTOOSMALL
;
362 static HRESULT WINAPI
IAVIFile_fnGetStream(IAVIFile
*iface
, PAVISTREAM
*avis
,
363 DWORD fccType
, LONG lParam
)
365 ICOM_THIS(IAVIFileImpl
,iface
);
369 TRACE("(%p,%p,0x%08lX,%ld)\n", iface
, avis
, fccType
, lParam
);
371 if (avis
== NULL
|| lParam
< 0)
372 return AVIERR_BADPARAM
;
374 nStream
= AVIFILE_SearchStream(This
, fccType
, lParam
);
376 /* Does the requested stream exist? */
377 if (nStream
< This
->fInfo
.dwStreams
&&
378 This
->ppStreams
[nStream
] != NULL
) {
379 *avis
= (PAVISTREAM
)This
->ppStreams
[nStream
];
380 IAVIStream_AddRef(*avis
);
385 /* Sorry, but the specified stream doesn't exist */
386 return AVIERR_NODATA
;
389 static HRESULT WINAPI
IAVIFile_fnCreateStream(IAVIFile
*iface
,PAVISTREAM
*avis
,
390 LPAVISTREAMINFOW asi
)
392 ICOM_THIS(IAVIFileImpl
,iface
);
396 TRACE("(%p,%p,%p)\n", iface
, avis
, asi
);
398 /* check parameters */
399 if (avis
== NULL
|| asi
== NULL
)
400 return AVIERR_BADPARAM
;
404 /* Does the user have write permission? */
405 if ((This
->uMode
& MMIO_RWMODE
) == 0)
406 return AVIERR_READONLY
;
408 /* Can we add another stream? */
409 n
= This
->fInfo
.dwStreams
;
410 if (n
>= MAX_AVISTREAMS
|| This
->dwMoviChunkPos
!= 0) {
411 /* already reached max nr of streams
412 * or have already written frames to disk */
413 return AVIERR_UNSUPPORTED
;
416 /* check AVISTREAMINFO for some really needed things */
417 if (asi
->fccType
== 0 || asi
->dwScale
== 0 || asi
->dwRate
== 0)
418 return AVIERR_BADFORMAT
;
420 /* now it seems to be save to add the stream */
421 assert(This
->ppStreams
[n
] == NULL
);
422 This
->ppStreams
[n
] = (IAVIStreamImpl
*)LocalAlloc(LPTR
,
423 sizeof(IAVIStreamImpl
));
424 if (This
->ppStreams
[n
] == NULL
)
425 return AVIERR_MEMORY
;
427 /* initialize the new allocated stream */
428 AVIFILE_ConstructAVIStream(This
, n
, asi
);
430 This
->fInfo
.dwStreams
++;
433 /* update our AVIFILEINFO structure */
434 AVIFILE_UpdateInfo(This
);
437 *avis
= (PAVISTREAM
)This
->ppStreams
[n
];
438 IAVIStream_AddRef(*avis
);
443 static HRESULT WINAPI
IAVIFile_fnWriteData(IAVIFile
*iface
, DWORD ckid
,
444 LPVOID lpData
, LONG size
)
446 ICOM_THIS(IAVIFileImpl
,iface
);
448 TRACE("(%p,0x%08lX,%p,%ld)\n", iface
, ckid
, lpData
, size
);
450 /* check parameters */
452 return AVIERR_BADPARAM
;
454 return AVIERR_BADSIZE
;
456 /* Do we have write permission? */
457 if ((This
->uMode
& MMIO_RWMODE
) == 0)
458 return AVIERR_READONLY
;
462 return WriteExtraChunk(&This
->fileextra
, ckid
, lpData
, size
);
465 static HRESULT WINAPI
IAVIFile_fnReadData(IAVIFile
*iface
, DWORD ckid
,
466 LPVOID lpData
, LONG
*size
)
468 ICOM_THIS(IAVIFileImpl
,iface
);
470 TRACE("(%p,0x%08lX,%p,%p)\n", iface
, ckid
, lpData
, size
);
472 return ReadExtraChunk(&This
->fileextra
, ckid
, lpData
, size
);
475 static HRESULT WINAPI
IAVIFile_fnEndRecord(IAVIFile
*iface
)
477 ICOM_THIS(IAVIFileImpl
,iface
);
479 TRACE("(%p)\n",iface
);
481 if ((This
->uMode
& MMIO_RWMODE
) == 0)
482 return AVIERR_READONLY
;
486 /* no frames written to any stream? -- compute start of 'movi'-chunk */
487 if (This
->dwMoviChunkPos
== 0)
488 AVIFILE_ComputeMoviStart(This
);
490 This
->fInfo
.dwFlags
|= AVIFILEINFO_ISINTERLEAVED
;
492 /* already written frames to any stream, ... */
493 if (This
->ckLastRecord
.dwFlags
& MMIO_DIRTY
) {
494 /* close last record */
495 if (mmioAscend(This
->hmmio
, &This
->ckLastRecord
, 0) != 0)
496 return AVIERR_FILEWRITE
;
498 AVIFILE_AddRecord(This
);
500 if (This
->fInfo
.dwSuggestedBufferSize
< This
->ckLastRecord
.cksize
+ 3 * sizeof(DWORD
))
501 This
->fInfo
.dwSuggestedBufferSize
= This
->ckLastRecord
.cksize
+ 3 * sizeof(DWORD
);
504 /* write out a new record into file, but don't close it */
505 This
->ckLastRecord
.cksize
= 0;
506 This
->ckLastRecord
.fccType
= listtypeAVIRECORD
;
507 if (mmioSeek(This
->hmmio
, This
->dwNextFramePos
, SEEK_SET
) == -1)
508 return AVIERR_FILEWRITE
;
509 if (mmioCreateChunk(This
->hmmio
, &This
->ckLastRecord
, MMIO_CREATELIST
) != 0)
510 return AVIERR_FILEWRITE
;
511 This
->dwNextFramePos
+= 3 * sizeof(DWORD
);
516 static HRESULT WINAPI
IAVIFile_fnDeleteStream(IAVIFile
*iface
, DWORD fccType
,
519 ICOM_THIS(IAVIFileImpl
,iface
);
523 TRACE("(%p,0x%08lX,%ld)\n", iface
, fccType
, lParam
);
525 /* check parameter */
527 return AVIERR_BADPARAM
;
529 /* Habe 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 LocalFree((HLOCAL
)This
->ppStreams
[nStream
]);
541 if (This
->fInfo
.dwStreams
- nStream
> 0)
542 memcpy(This
->ppStreams
+ nStream
, This
->ppStreams
+ nStream
+ 1,
543 (This
->fInfo
.dwStreams
- nStream
) * sizeof(IAVIStreamImpl
*));
545 This
->ppStreams
[This
->fInfo
.dwStreams
] = NULL
;
546 This
->fInfo
.dwStreams
--;
549 /* This->fInfo will be updated further when asked for */
552 return AVIERR_NODATA
;
555 /***********************************************************************/
557 static HRESULT WINAPI
IPersistFile_fnQueryInterface(IPersistFile
*iface
,
558 REFIID refiid
, LPVOID
*obj
)
560 ICOM_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 ICOM_THIS(IPersistFileImpl
,iface
);
571 assert(This
->paf
!= NULL
);
573 return IAVIFile_AddRef((PAVIFILE
)This
->paf
);
576 static ULONG WINAPI
IPersistFile_fnRelease(IPersistFile
*iface
)
578 ICOM_THIS(IPersistFileImpl
,iface
);
580 assert(This
->paf
!= NULL
);
582 return IAVIFile_Release((PAVIFILE
)This
->paf
);
585 static HRESULT WINAPI
IPersistFile_fnGetClassID(IPersistFile
*iface
,
588 TRACE("(%p,%p)\n", iface
, pClassID
);
590 if (pClassID
== NULL
)
591 return AVIERR_BADPARAM
;
593 memcpy(pClassID
, &CLSID_AVIFile
, sizeof(CLSID_AVIFile
));
598 static HRESULT WINAPI
IPersistFile_fnIsDirty(IPersistFile
*iface
)
600 ICOM_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 ICOM_THIS(IPersistFileImpl
,iface
);
616 TRACE("(%p,%s,0x%08lX)\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
= (LPWSTR
)LocalAlloc(LPTR
, 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
= LocalAlloc(LPTR
, len
* sizeof(CHAR
));
641 if (szFileName
== NULL
)
642 return AVIERR_MEMORY
;
644 WideCharToMultiByte(CP_ACP
, 0, This
->paf
->szFileName
, -1, szFileName
,
647 This
->paf
->hmmio
= mmioOpenA(szFileName
, NULL
, MMIO_ALLOCBUF
| dwMode
);
648 LocalFree((HLOCAL
)szFileName
);
649 if (This
->paf
->hmmio
== NULL
)
650 return AVIERR_FILEOPEN
;
653 /* should we create a new file? */
654 if (dwMode
& OF_CREATE
) {
655 memset(& This
->paf
->fInfo
, 0, sizeof(This
->paf
->fInfo
));
656 This
->paf
->fInfo
.dwFlags
= AVIFILEINFO_HASINDEX
| AVIFILEINFO_TRUSTCKTYPE
;
660 return AVIFILE_LoadFile(This
->paf
);
663 static HRESULT WINAPI
IPersistFile_fnSave(IPersistFile
*iface
,
664 LPCOLESTR pszFileName
,BOOL fRemember
)
666 TRACE("(%p,%s,%d)\n", iface
, debugstr_w(pszFileName
), fRemember
);
668 /* We write directly to disk, so nothing to do. */
673 static HRESULT WINAPI
IPersistFile_fnSaveCompleted(IPersistFile
*iface
,
674 LPCOLESTR pszFileName
)
676 TRACE("(%p,%s)\n", iface
, debugstr_w(pszFileName
));
678 /* We write directly to disk, so nothing to do. */
683 static HRESULT WINAPI
IPersistFile_fnGetCurFile(IPersistFile
*iface
,
684 LPOLESTR
*ppszFileName
)
686 ICOM_THIS(IPersistFileImpl
,iface
);
688 TRACE("(%p,%p)\n", iface
, ppszFileName
);
690 if (ppszFileName
== NULL
)
691 return AVIERR_BADPARAM
;
693 *ppszFileName
= NULL
;
695 assert(This
->paf
!= NULL
);
697 if (This
->paf
->szFileName
!= NULL
) {
698 int len
= lstrlenW(This
->paf
->szFileName
);
700 *ppszFileName
= (LPOLESTR
)GlobalAllocPtr(GHND
, len
* sizeof(WCHAR
));
701 if (*ppszFileName
== NULL
)
702 return AVIERR_MEMORY
;
704 memcpy(*ppszFileName
, This
->paf
->szFileName
, len
* sizeof(WCHAR
));
710 /***********************************************************************/
712 static HRESULT WINAPI
IAVIStream_fnQueryInterface(IAVIStream
*iface
,
713 REFIID refiid
, LPVOID
*obj
)
715 ICOM_THIS(IAVIStreamImpl
,iface
);
717 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(refiid
), obj
);
719 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
720 IsEqualGUID(&IID_IAVIStream
, refiid
)) {
722 IAVIStream_AddRef(iface
);
726 /* FIXME: IAVIStreaming interface */
728 return OLE_E_ENUM_NOMORE
;
731 static ULONG WINAPI
IAVIStream_fnAddRef(IAVIStream
*iface
)
733 ICOM_THIS(IAVIStreamImpl
,iface
);
735 TRACE("(%p) -> %ld\n", iface
, This
->ref
+ 1);
737 /* also add ref to parent, so that it doesn't kill us */
738 if (This
->paf
!= NULL
)
739 IAVIFile_AddRef((PAVIFILE
)This
->paf
);
741 return ++(This
->ref
);
744 static ULONG WINAPI
IAVIStream_fnRelease(IAVIStream
* iface
)
746 ICOM_THIS(IAVIStreamImpl
,iface
);
748 TRACE("(%p) -> %ld\n", iface
, This
->ref
- 1);
750 /* we belong to the AVIFile, which must free us! */
751 if (This
->ref
== 0) {
752 ERR(": already released!\n");
756 if (This
->paf
!= NULL
)
757 IAVIFile_Release((PAVIFILE
)This
->paf
);
762 static HRESULT WINAPI
IAVIStream_fnCreate(IAVIStream
*iface
, LPARAM lParam1
,
765 TRACE("(%p,0x%08lX,0x%08lX)\n", iface
, lParam1
, lParam2
);
767 /* This IAVIStream interface needs an AVIFile */
768 return AVIERR_UNSUPPORTED
;
771 static HRESULT WINAPI
IAVIStream_fnInfo(IAVIStream
*iface
,LPAVISTREAMINFOW psi
,
774 ICOM_THIS(IAVIStreamImpl
,iface
);
776 TRACE("(%p,%p,%ld)\n", iface
, psi
, size
);
779 return AVIERR_BADPARAM
;
781 return AVIERR_BADSIZE
;
783 memcpy(psi
, &This
->sInfo
, min((DWORD
)size
, sizeof(This
->sInfo
)));
785 if ((DWORD
)size
< sizeof(This
->sInfo
))
786 return AVIERR_BUFFERTOOSMALL
;
790 static LONG WINAPI
IAVIStream_fnFindSample(IAVIStream
*iface
, LONG pos
,
793 ICOM_THIS(IAVIStreamImpl
,iface
);
797 TRACE("(%p,%ld,0x%08lX)\n",iface
,pos
,flags
);
799 if (flags
& FIND_FROM_START
) {
800 pos
= This
->sInfo
.dwStart
;
801 flags
&= ~(FIND_FROM_START
|FIND_PREV
);
805 if (This
->sInfo
.dwSampleSize
!= 0) {
806 /* convert samples into block number with offset */
807 AVIFILE_SamplesToBlock(This
, &pos
, &offset
);
810 if (flags
& FIND_TYPE
) {
811 if (flags
& FIND_KEY
) {
812 while (0 <= pos
&& pos
<= This
->lLastFrame
) {
813 if (This
->idxFrames
[pos
].dwFlags
& AVIIF_KEYFRAME
)
816 if (flags
& FIND_NEXT
)
821 } else if (flags
& FIND_ANY
) {
822 while (0 <= pos
&& pos
<= This
->lLastFrame
) {
823 if (This
->idxFrames
[pos
].dwChunkLength
> 0)
826 if (flags
& FIND_NEXT
)
832 } else if ((flags
& FIND_FORMAT
) && This
->idxFmtChanges
!= NULL
&&
833 This
->sInfo
.fccType
== streamtypeVIDEO
) {
834 if (flags
& FIND_NEXT
) {
837 for (n
= 0; n
< This
->sInfo
.dwFormatChangeCount
; n
++)
838 if (This
->idxFmtChanges
[n
].ckid
>= pos
)
843 for (n
= (LONG
)This
->sInfo
.dwFormatChangeCount
; n
>= 0; n
--) {
844 if (This
->idxFmtChanges
[n
].ckid
<= pos
)
848 if (pos
> (LONG
)This
->sInfo
.dwStart
)
849 return 0; /* format changes always for first frame */
856 if (flags
& FIND_RET
) {
858 if (flags
& FIND_LENGTH
) {
860 if (This
->sInfo
.dwSampleSize
)
861 pos
= This
->sInfo
.dwSampleSize
;
864 } else if (flags
& FIND_OFFSET
) {
865 /* physical position */
866 pos
= This
->idxFrames
[pos
].dwChunkOffset
+ offset
* This
->sInfo
.dwSampleSize
;
867 } else if (flags
& FIND_SIZE
) {
869 pos
= This
->idxFrames
[pos
].dwChunkLength
;
870 } else if (flags
& FIND_INDEX
) {
871 FIXME(": FIND_INDEX flag is not supported!");
873 pos
= This
->paf
->dwIdxChunkPos
;
874 } /* else logical position */
882 static HRESULT WINAPI
IAVIStream_fnReadFormat(IAVIStream
*iface
, LONG pos
,
883 LPVOID format
, LONG
*formatsize
)
885 ICOM_THIS(IAVIStreamImpl
,iface
);
887 TRACE("(%p,%ld,%p,%p)\n", iface
, pos
, format
, formatsize
);
889 if (formatsize
== NULL
)
890 return AVIERR_BADPARAM
;
892 /* only interested in needed buffersize? */
893 if (format
== NULL
|| *formatsize
<= 0) {
894 *formatsize
= This
->cbFormat
;
899 /* copy initial format (only as much as will fit) */
900 memcpy(format
, This
->lpFormat
, min(*(DWORD
*)formatsize
, This
->cbFormat
));
901 if (*(DWORD
*)formatsize
< This
->cbFormat
) {
902 *formatsize
= This
->cbFormat
;
903 return AVIERR_BUFFERTOOSMALL
;
906 /* Could format change? When yes will it change? */
907 if ((This
->sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
) &&
908 pos
> This
->sInfo
.dwStart
) {
911 lLastFmt
= IAVIStream_fnFindSample(iface
, pos
, FIND_FORMAT
|FIND_PREV
);
913 FIXME(": need to read formatchange for %ld -- unimplemented!\n",lLastFmt
);
917 *formatsize
= This
->cbFormat
;
921 static HRESULT WINAPI
IAVIStream_fnSetFormat(IAVIStream
*iface
, LONG pos
,
922 LPVOID format
, LONG formatsize
)
924 ICOM_THIS(IAVIStreamImpl
,iface
);
926 LPBITMAPINFOHEADER lpbiNew
= (LPBITMAPINFOHEADER
)format
;
928 TRACE("(%p,%ld,%p,%ld)\n", iface
, pos
, format
, formatsize
);
930 /* check parameters */
931 if (format
== NULL
|| formatsize
<= 0)
932 return AVIERR_BADPARAM
;
934 /* Do we have write permission? */
935 if ((This
->paf
->uMode
& MMIO_RWMODE
) == 0)
936 return AVIERR_READONLY
;
938 /* can only set format before frame is written! */
939 if (This
->lLastFrame
> pos
)
940 return AVIERR_UNSUPPORTED
;
942 /* initial format or a formatchange? */
943 if (This
->lpFormat
== NULL
) {
945 if (This
->paf
->dwMoviChunkPos
!= 0)
946 return AVIERR_ERROR
; /* user has used API in wrong sequnece! */
948 This
->lpFormat
= GlobalAllocPtr(GMEM_MOVEABLE
, formatsize
);
949 if (This
->lpFormat
== NULL
)
950 return AVIERR_MEMORY
;
951 This
->cbFormat
= formatsize
;
953 memcpy(This
->lpFormat
, format
, formatsize
);
955 /* update some infos about stream */
956 if (This
->sInfo
.fccType
== streamtypeVIDEO
) {
959 lDim
= This
->sInfo
.rcFrame
.right
- This
->sInfo
.rcFrame
.left
;
960 if (lDim
< lpbiNew
->biWidth
)
961 This
->sInfo
.rcFrame
.right
= This
->sInfo
.rcFrame
.left
+ lpbiNew
->biWidth
;
962 lDim
= This
->sInfo
.rcFrame
.bottom
- This
->sInfo
.rcFrame
.top
;
963 if (lDim
< lpbiNew
->biHeight
)
964 This
->sInfo
.rcFrame
.bottom
= This
->sInfo
.rcFrame
.top
+ lpbiNew
->biHeight
;
965 } else if (This
->sInfo
.fccType
== streamtypeAUDIO
)
966 This
->sInfo
.dwSampleSize
= ((LPWAVEFORMATEX
)This
->lpFormat
)->nBlockAlign
;
971 LPBITMAPINFOHEADER lpbiOld
= (LPBITMAPINFOHEADER
)This
->lpFormat
;
972 RGBQUAD
*rgbNew
= (RGBQUAD
*)((LPBYTE
)lpbiNew
+ lpbiNew
->biSize
);
973 AVIPALCHANGE
*lppc
= NULL
;
976 /* pherhaps formatchange, check it ... */
977 if (This
->cbFormat
!= formatsize
)
978 return AVIERR_UNSUPPORTED
;
980 /* no formatchange, only the initial one */
981 if (memcmp(This
->lpFormat
, format
, formatsize
) == 0)
984 /* check that's only the palette, which changes */
985 if (lpbiOld
->biSize
!= lpbiNew
->biSize
||
986 lpbiOld
->biWidth
!= lpbiNew
->biWidth
||
987 lpbiOld
->biHeight
!= lpbiNew
->biHeight
||
988 lpbiOld
->biPlanes
!= lpbiNew
->biPlanes
||
989 lpbiOld
->biBitCount
!= lpbiNew
->biBitCount
||
990 lpbiOld
->biCompression
!= lpbiNew
->biCompression
||
991 lpbiOld
->biClrUsed
!= lpbiNew
->biClrUsed
)
992 return AVIERR_UNSUPPORTED
;
994 This
->sInfo
.dwFlags
|= AVISTREAMINFO_FORMATCHANGES
;
996 /* simply say all colors have changed */
997 ck
.ckid
= MAKEAVICKID(cktypePALchange
, This
->nStream
);
998 ck
.cksize
= 2 * sizeof(WORD
) + lpbiOld
->biClrUsed
* sizeof(PALETTEENTRY
);
999 lppc
= (AVIPALCHANGE
*)GlobalAllocPtr(GMEM_MOVEABLE
, ck
.cksize
);
1001 return AVIERR_MEMORY
;
1003 lppc
->bFirstEntry
= 0;
1004 lppc
->bNumEntries
= (lpbiOld
->biClrUsed
< 256 ? lpbiOld
->biClrUsed
: 0);
1006 for (n
= 0; n
< lpbiOld
->biClrUsed
; n
++) {
1007 lppc
->peNew
[n
].peRed
= rgbNew
[n
].rgbRed
;
1008 lppc
->peNew
[n
].peGreen
= rgbNew
[n
].rgbGreen
;
1009 lppc
->peNew
[n
].peBlue
= rgbNew
[n
].rgbBlue
;
1010 lppc
->peNew
[n
].peFlags
= 0;
1013 if (mmioSeek(This
->paf
->hmmio
, This
->paf
->dwNextFramePos
, SEEK_SET
) == -1)
1014 return AVIERR_FILEWRITE
;
1015 if (mmioCreateChunk(This
->paf
->hmmio
, &ck
, 0) != S_OK
)
1016 return AVIERR_FILEWRITE
;
1017 if (mmioWrite(This
->paf
->hmmio
, (HPSTR
)lppc
, ck
.cksize
) != ck
.cksize
)
1018 return AVIERR_FILEWRITE
;
1019 if (mmioAscend(This
->paf
->hmmio
, &ck
, 0) != S_OK
)
1020 return AVIERR_FILEWRITE
;
1021 This
->paf
->dwNextFramePos
+= ck
.cksize
+ 2 * sizeof(DWORD
);
1023 GlobalFreePtr(lppc
);
1025 return AVIFILE_AddFrame(This
, cktypePALchange
, n
, ck
.dwDataOffset
, 0);
1029 static HRESULT WINAPI
IAVIStream_fnRead(IAVIStream
*iface
, LONG start
,
1030 LONG samples
, LPVOID buffer
,
1031 LONG buffersize
, LPLONG bytesread
,
1034 ICOM_THIS(IAVIStreamImpl
,iface
);
1039 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface
, start
, samples
, buffer
,
1040 buffersize
, bytesread
, samplesread
);
1042 /* clear return parameters if given */
1043 if (bytesread
!= NULL
)
1045 if (samplesread
!= NULL
)
1048 /* check parameters */
1049 if ((LONG
)This
->sInfo
.dwStart
> start
)
1050 return AVIERR_NODATA
; /* couldn't read before start of stream */
1051 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< (DWORD
)start
)
1052 return AVIERR_NODATA
; /* start is past end of stream */
1054 /* should we read as much as possible? */
1055 if (samples
== -1) {
1056 /* User should know how much we have read */
1057 if (bytesread
== NULL
&& samplesread
== NULL
)
1058 return AVIERR_BADPARAM
;
1060 if (This
->sInfo
.dwSampleSize
!= 0)
1061 samples
= buffersize
/ This
->sInfo
.dwSampleSize
;
1066 /* limit to end of stream */
1067 if ((LONG
)This
->sInfo
.dwLength
< samples
)
1068 samples
= This
->sInfo
.dwLength
;
1069 if ((start
- This
->sInfo
.dwStart
) > (This
->sInfo
.dwLength
- samples
))
1070 samples
= This
->sInfo
.dwLength
- (start
- This
->sInfo
.dwStart
);
1072 /* nothing to read? Then leave ... */
1076 if (This
->sInfo
.dwSampleSize
!= 0) {
1077 /* fixed samplesize -- we can read over frame/block boundaries */
1081 /* convert start sample to block,offset pair */
1082 AVIFILE_SamplesToBlock(This
, &block
, &offset
);
1084 /* convert samples to bytes */
1085 samples
*= This
->sInfo
.dwSampleSize
;
1087 while (samples
> 0 && buffersize
> 0) {
1088 if (block
!= This
->dwCurrentFrame
) {
1089 hr
= AVIFILE_ReadBlock(This
, block
, NULL
, 0);
1094 size
= min((DWORD
)samples
, (DWORD
)buffersize
);
1095 size
= min(size
, This
->cbBuffer
- offset
);
1096 memcpy(buffer
, ((BYTE
*)&This
->lpBuffer
[2]) + offset
, size
);
1100 ((BYTE
*)buffer
) += size
;
1104 /* fill out return parameters if given */
1105 if (bytesread
!= NULL
)
1107 if (samplesread
!= NULL
)
1108 *samplesread
+= size
/ This
->sInfo
.dwSampleSize
;
1114 return AVIERR_BUFFERTOOSMALL
;
1116 /* variable samplesize -- we can only read one full frame/block */
1120 assert(start
<= This
->lLastFrame
);
1121 size
= This
->idxFrames
[start
].dwChunkLength
;
1122 if (buffer
!= NULL
&& buffersize
>= size
) {
1123 hr
= AVIFILE_ReadBlock(This
, start
, buffer
, size
);
1126 } else if (buffer
!= NULL
)
1127 return AVIERR_BUFFERTOOSMALL
;
1129 /* fill out return parameters if given */
1130 if (bytesread
!= NULL
)
1132 if (samplesread
!= NULL
)
1133 *samplesread
= samples
;
1139 static HRESULT WINAPI
IAVIStream_fnWrite(IAVIStream
*iface
, LONG start
,
1140 LONG samples
, LPVOID buffer
,
1141 LONG buffersize
, DWORD flags
,
1143 LPLONG byteswritten
)
1145 ICOM_THIS(IAVIStreamImpl
,iface
);
1150 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface
, start
, samples
,
1151 buffer
, buffersize
, flags
, sampwritten
, byteswritten
);
1153 /* clear return parameters if given */
1154 if (sampwritten
!= NULL
)
1156 if (byteswritten
!= NULL
)
1159 /* check parameters */
1160 if (buffer
== NULL
&& (buffersize
> 0 || samples
> 0))
1161 return AVIERR_BADPARAM
;
1163 /* Have we write permission? */
1164 if ((This
->paf
->uMode
& MMIO_RWMODE
) == 0)
1165 return AVIERR_READONLY
;
1167 switch (This
->sInfo
.fccType
) {
1168 case streamtypeAUDIO
:
1169 ckid
= MAKEAVICKID(cktypeWAVEbytes
, This
->nStream
);
1172 if ((flags
& AVIIF_KEYFRAME
) && buffersize
!= 0)
1173 ckid
= MAKEAVICKID(cktypeDIBbits
, This
->nStream
);
1175 ckid
= MAKEAVICKID(cktypeDIBcompressed
, This
->nStream
);
1179 /* append to end of stream? */
1181 if (This
->lLastFrame
== -1)
1182 start
= This
->sInfo
.dwStart
;
1184 start
= This
->sInfo
.dwLength
;
1185 } else if (This
->lLastFrame
== -1)
1186 This
->sInfo
.dwStart
= start
;
1188 if (This
->sInfo
.dwSampleSize
!= 0) {
1189 /* fixed sample size -- audio like */
1190 if (samples
* This
->sInfo
.dwSampleSize
!= buffersize
)
1191 return AVIERR_BADPARAM
;
1193 /* Couldn't skip audio-like data -- User must supply appropriate silence */
1194 if (This
->sInfo
.dwLength
!= start
)
1195 return AVIERR_UNSUPPORTED
;
1197 /* Convert position to frame/block */
1198 start
= This
->lLastFrame
+ 1;
1200 if ((This
->paf
->fInfo
.dwFlags
& AVIFILEINFO_ISINTERLEAVED
) == 0) {
1201 FIXME(": not interleaved, could collect audio data!\n");
1204 /* variable sample size -- video like */
1206 return AVIERR_UNSUPPORTED
;
1208 /* must we fill up with empty frames? */
1209 if (This
->lLastFrame
!= -1) {
1210 FOURCC ckid2
= MAKEAVICKID(cktypeDIBcompressed
, This
->nStream
);
1212 while (start
> This
->lLastFrame
+ 1) {
1213 hr
= AVIFILE_WriteBlock(This
, This
->lLastFrame
+ 1, ckid2
, 0, NULL
, 0);
1220 /* write the block now */
1221 hr
= AVIFILE_WriteBlock(This
, start
, ckid
, flags
, buffer
, buffersize
);
1222 if (SUCCEEDED(hr
)) {
1223 /* fill out return parameters if given */
1224 if (sampwritten
!= NULL
)
1225 *sampwritten
= samples
;
1226 if (byteswritten
!= NULL
)
1227 *byteswritten
= buffersize
;
1233 static HRESULT WINAPI
IAVIStream_fnDelete(IAVIStream
*iface
, LONG start
,
1236 ICOM_THIS(IAVIStreamImpl
,iface
);
1238 FIXME("(%p,%ld,%ld): stub\n", iface
, start
, samples
);
1240 /* check parameters */
1241 if (start
< 0 || samples
< 0)
1242 return AVIERR_BADPARAM
;
1244 /* Delete before start of stream? */
1245 if (start
+ samples
< This
->sInfo
.dwStart
)
1248 /* Delete after end of stream? */
1249 if (start
> This
->sInfo
.dwLength
)
1252 /* For the rest we need write permissions */
1253 if ((This
->paf
->uMode
& MMIO_RWMODE
) == 0)
1254 return AVIERR_READONLY
;
1256 /* 1. overwrite the data with JUNK
1258 * if ISINTERLEAVED {
1259 * 2. concat all neighboured JUNK-blocks in this record to one
1260 * 3. if this record only contains JUNK and is at end set dwNextFramePos
1261 * to start of this record, repeat this.
1263 * 2. concat all neighboured JUNK-blocks.
1264 * 3. if the JUNK block is at the end, then set dwNextFramePos to
1265 * start of this block.
1269 return AVIERR_UNSUPPORTED
;
1272 static HRESULT WINAPI
IAVIStream_fnReadData(IAVIStream
*iface
, DWORD fcc
,
1273 LPVOID lp
, LPLONG lpread
)
1275 ICOM_THIS(IAVIStreamImpl
,iface
);
1277 TRACE("(%p,0x%08lX,%p,%p)\n", iface
, fcc
, lp
, lpread
);
1279 if (fcc
== ckidSTREAMHANDLERDATA
) {
1280 if (This
->lpHandlerData
!= NULL
&& This
->cbHandlerData
> 0) {
1281 if (lp
== NULL
|| *lpread
<= 0) {
1282 *lpread
= This
->cbHandlerData
;
1286 memcpy(lp
, This
->lpHandlerData
, min(This
->cbHandlerData
, *lpread
));
1287 if (*lpread
< This
->cbHandlerData
)
1288 return AVIERR_BUFFERTOOSMALL
;
1291 return AVIERR_NODATA
;
1293 return ReadExtraChunk(&This
->extra
, fcc
, lp
, lpread
);
1296 static HRESULT WINAPI
IAVIStream_fnWriteData(IAVIStream
*iface
, DWORD fcc
,
1297 LPVOID lp
, LONG size
)
1299 ICOM_THIS(IAVIStreamImpl
,iface
);
1301 TRACE("(%p,0x%08lx,%p,%ld)\n", iface
, fcc
, lp
, size
);
1303 /* check parameters */
1305 return AVIERR_BADPARAM
;
1307 return AVIERR_BADSIZE
;
1309 /* need write permission */
1310 if ((This
->paf
->uMode
& MMIO_RWMODE
) == 0)
1311 return AVIERR_READONLY
;
1313 /* already written something to this file? */
1314 if (This
->paf
->dwMoviChunkPos
!= 0) {
1315 /* the data will be inserted before the 'movi' chunk, so check for
1317 DWORD dwPos
= AVIFILE_ComputeMoviStart(This
->paf
);
1319 /* ckid,size => 2 * sizeof(DWORD) */
1320 dwPos
+= 2 * sizeof(DWORD
) + size
;
1321 if (size
>= This
->paf
->dwMoviChunkPos
- 2 * sizeof(DWORD
))
1322 return AVIERR_UNSUPPORTED
; /* not enough space left */
1325 This
->paf
->fDirty
= TRUE
;
1327 if (fcc
== ckidSTREAMHANDLERDATA
) {
1328 if (This
->lpHandlerData
!= NULL
) {
1329 FIXME(": handler data already set -- overwirte?\n");
1330 return AVIERR_UNSUPPORTED
;
1333 This
->lpHandlerData
= GlobalAllocPtr(GMEM_MOVEABLE
, size
);
1334 if (This
->lpHandlerData
== NULL
)
1335 return AVIERR_MEMORY
;
1336 This
->cbHandlerData
= size
;
1337 memcpy(This
->lpHandlerData
, lp
, size
);
1341 return WriteExtraChunk(&This
->extra
, fcc
, lp
, size
);
1344 static HRESULT WINAPI
IAVIStream_fnSetInfo(IAVIStream
*iface
,
1345 LPAVISTREAMINFOW info
, LONG infolen
)
1347 FIXME("(%p,%p,%ld): stub\n", iface
, info
, infolen
);
1352 /***********************************************************************/
1354 static HRESULT
AVIFILE_AddFrame(IAVIStreamImpl
*This
, DWORD ckid
, DWORD size
, DWORD offset
, DWORD flags
)
1356 /* pre-conditions */
1357 assert(This
!= NULL
);
1359 switch (TWOCCFromFOURCC(ckid
)) {
1361 if (This
->paf
->fInfo
.dwFlags
& AVIFILEINFO_TRUSTCKTYPE
)
1362 flags
|= AVIIF_KEYFRAME
;
1364 case cktypeDIBcompressed
:
1365 if (This
->paf
->fInfo
.dwFlags
& AVIFILEINFO_TRUSTCKTYPE
)
1366 flags
&= ~AVIIF_KEYFRAME
;
1368 case cktypePALchange
:
1369 if (This
->sInfo
.fccType
!= streamtypeVIDEO
) {
1370 ERR(": found palette change in non-video stream!\n");
1371 return AVIERR_BADFORMAT
;
1373 This
->sInfo
.dwFlags
|= AVISTREAMINFO_FORMATCHANGES
;
1374 This
->sInfo
.dwFormatChangeCount
++;
1376 if (This
->idxFmtChanges
== NULL
|| This
->sInfo
.dwFormatChangeCount
< This
->nIdxFmtChanges
) {
1377 UINT n
= This
->sInfo
.dwFormatChangeCount
;
1379 This
->nIdxFmtChanges
+= 16;
1380 This
->idxFmtChanges
= GlobalReAllocPtr(This
->idxFmtChanges
, This
->nIdxFmtChanges
* sizeof(AVIINDEXENTRY
), GHND
);
1381 if (This
->idxFmtChanges
== NULL
)
1382 return AVIERR_MEMORY
;
1384 This
->idxFmtChanges
[n
].ckid
= This
->lLastFrame
;
1385 This
->idxFmtChanges
[n
].dwFlags
= 0;
1386 This
->idxFmtChanges
[n
].dwChunkOffset
= offset
;
1387 This
->idxFmtChanges
[n
].dwChunkLength
= size
;
1392 case cktypeWAVEbytes
:
1393 if (This
->paf
->fInfo
.dwFlags
& AVIFILEINFO_TRUSTCKTYPE
)
1394 flags
|= AVIIF_KEYFRAME
;
1397 WARN(": unknown TWOCC 0x%04X found\n", TWOCCFromFOURCC(ckid
));
1401 /* first frame is alwasy a keyframe */
1402 if (This
->lLastFrame
== -1)
1403 flags
|= AVIIF_KEYFRAME
;
1405 if (This
->sInfo
.dwSuggestedBufferSize
< size
)
1406 This
->sInfo
.dwSuggestedBufferSize
= size
;
1408 /* get memory for index */
1409 if (This
->idxFrames
== NULL
|| This
->lLastFrame
+ 1 >= This
->nIdxFrames
) {
1410 This
->nIdxFrames
+= 512;
1411 This
->idxFrames
= GlobalReAllocPtr(This
->idxFrames
, This
->nIdxFrames
* sizeof(AVIINDEXENTRY
), GHND
);
1412 if (This
->idxFrames
== NULL
)
1413 return AVIERR_MEMORY
;
1417 This
->idxFrames
[This
->lLastFrame
].ckid
= ckid
;
1418 This
->idxFrames
[This
->lLastFrame
].dwFlags
= flags
;
1419 This
->idxFrames
[This
->lLastFrame
].dwChunkOffset
= offset
;
1420 This
->idxFrames
[This
->lLastFrame
].dwChunkLength
= size
;
1422 /* update AVISTREAMINFO structure if necessary */
1423 if (This
->sInfo
.dwLength
<= This
->lLastFrame
)
1424 This
->sInfo
.dwLength
= This
->lLastFrame
+ 1;
1429 static HRESULT
AVIFILE_AddRecord(IAVIFileImpl
*This
)
1431 /* pre-conditions */
1432 assert(This
!= NULL
&& This
->ppStreams
[0] != NULL
);
1434 if (This
->idxRecords
== NULL
|| This
->cbIdxRecords
== 0) {
1435 This
->cbIdxRecords
+= 1024 * sizeof(AVIINDEXENTRY
);
1436 This
->idxRecords
= GlobalAllocPtr(GHND
, This
->cbIdxRecords
);
1437 if (This
->idxRecords
== NULL
)
1438 return AVIERR_MEMORY
;
1441 assert(This
->nIdxRecords
< This
->cbIdxRecords
/sizeof(AVIINDEXENTRY
));
1443 This
->idxRecords
[This
->nIdxRecords
].ckid
= listtypeAVIRECORD
;
1444 This
->idxRecords
[This
->nIdxRecords
].dwFlags
= AVIIF_LIST
;
1445 This
->idxRecords
[This
->nIdxRecords
].dwChunkOffset
=
1446 This
->ckLastRecord
.dwDataOffset
- 2 * sizeof(DWORD
);
1447 This
->idxRecords
[This
->nIdxRecords
].dwChunkLength
=
1448 This
->ckLastRecord
.cksize
;
1449 This
->nIdxRecords
++;
1454 static DWORD
AVIFILE_ComputeMoviStart(IAVIFileImpl
*This
)
1459 /* RIFF,hdrl,movi,avih => (3 * 3 + 2) * sizeof(DWORD) = 11 * sizeof(DWORD) */
1460 dwPos
= 11 * sizeof(DWORD
) + sizeof(MainAVIHeader
);
1462 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
1463 IAVIStreamImpl
*pStream
= This
->ppStreams
[nStream
];
1465 /* strl,strh,strf => (3 + 2 * 2) * sizeof(DWORD) = 7 * sizeof(DWORD) */
1466 dwPos
+= 7 * sizeof(DWORD
) + sizeof(AVIStreamHeader
);
1467 dwPos
+= ((pStream
->cbFormat
+ 1) & ~1U);
1468 if (pStream
->lpHandlerData
!= NULL
&& pStream
->cbHandlerData
> 0)
1469 dwPos
+= 2 * sizeof(DWORD
) + ((pStream
->cbHandlerData
+ 1) & ~1U);
1470 if (lstrlenW(pStream
->sInfo
.szName
) > 0)
1471 dwPos
+= 2 * sizeof(DWORD
) + ((lstrlenW(pStream
->sInfo
.szName
) + 1) & ~1U);
1474 if (This
->dwMoviChunkPos
== 0) {
1475 This
->dwNextFramePos
= dwPos
;
1477 /* pad to multiple of AVI_HEADERSIZE only if we are more than 8 bytes away from it */
1478 if (((dwPos
+ AVI_HEADERSIZE
) & ~(AVI_HEADERSIZE
- 1)) - dwPos
> 2 * sizeof(DWORD
))
1479 This
->dwNextFramePos
= (dwPos
+ AVI_HEADERSIZE
) & ~(AVI_HEADERSIZE
- 1);
1481 This
->dwMoviChunkPos
= This
->dwNextFramePos
- sizeof(DWORD
);
1487 static void AVIFILE_ConstructAVIStream(IAVIFileImpl
*paf
, DWORD nr
, LPAVISTREAMINFOW asi
)
1489 IAVIStreamImpl
*pstream
;
1491 /* pre-conditions */
1492 assert(paf
!= NULL
);
1493 assert(nr
< MAX_AVISTREAMS
);
1494 assert(paf
->ppStreams
[nr
] != NULL
);
1496 pstream
= paf
->ppStreams
[nr
];
1498 pstream
->lpVtbl
= &iavist
;
1501 pstream
->nStream
= nr
;
1502 pstream
->dwCurrentFrame
= (DWORD
)-1;
1503 pstream
->lLastFrame
= -1;
1506 memcpy(&pstream
->sInfo
, asi
, sizeof(pstream
->sInfo
));
1508 if (asi
->dwLength
> 0) {
1509 /* pre-allocate mem for frame-index structure */
1510 pstream
->idxFrames
=
1511 (AVIINDEXENTRY
*)GlobalAllocPtr(GHND
, asi
->dwLength
* sizeof(AVIINDEXENTRY
));
1512 if (pstream
->idxFrames
!= NULL
)
1513 pstream
->nIdxFrames
= asi
->dwLength
;
1515 if (asi
->dwFormatChangeCount
> 0) {
1516 /* pre-allocate mem for formatchange-index structure */
1517 pstream
->idxFmtChanges
=
1518 (AVIINDEXENTRY
*)GlobalAllocPtr(GHND
, asi
->dwFormatChangeCount
* sizeof(AVIINDEXENTRY
));
1519 if (pstream
->idxFmtChanges
!= NULL
)
1520 pstream
->nIdxFmtChanges
= asi
->dwFormatChangeCount
;
1523 /* These values will be computed */
1524 pstream
->sInfo
.dwLength
= 0;
1525 pstream
->sInfo
.dwSuggestedBufferSize
= 0;
1526 pstream
->sInfo
.dwFormatChangeCount
= 0;
1527 pstream
->sInfo
.dwEditCount
= 1;
1528 if (pstream
->sInfo
.dwSampleSize
> 0)
1529 SetRectEmpty(&pstream
->sInfo
.rcFrame
);
1532 pstream
->sInfo
.dwCaps
= AVIFILECAPS_CANREAD
|AVIFILECAPS_CANWRITE
;
1535 static void AVIFILE_DestructAVIStream(IAVIStreamImpl
*This
)
1537 /* pre-conditions */
1538 assert(This
!= NULL
);
1540 This
->dwCurrentFrame
= (DWORD
)-1;
1541 This
->lLastFrame
= -1;
1543 if (This
->idxFrames
!= NULL
) {
1544 GlobalFreePtr(This
->idxFrames
);
1545 This
->idxFrames
= NULL
;
1546 This
->nIdxFrames
= 0;
1548 if (This
->idxFmtChanges
!= NULL
) {
1549 GlobalFreePtr(This
->idxFmtChanges
);
1550 This
->idxFmtChanges
= NULL
;
1552 if (This
->lpBuffer
!= NULL
) {
1553 GlobalFreePtr(This
->lpBuffer
);
1554 This
->lpBuffer
= NULL
;
1557 if (This
->lpHandlerData
!= NULL
) {
1558 GlobalFreePtr(This
->lpHandlerData
);
1559 This
->lpHandlerData
= NULL
;
1560 This
->cbHandlerData
= 0;
1562 if (This
->extra
.lp
!= NULL
) {
1563 GlobalFreePtr(This
->extra
.lp
);
1564 This
->extra
.lp
= NULL
;
1567 if (This
->lpFormat
!= NULL
) {
1568 GlobalFreePtr(This
->lpFormat
);
1569 This
->lpFormat
= NULL
;
1574 static HRESULT
AVIFILE_LoadFile(IAVIFileImpl
*This
)
1576 MainAVIHeader MainAVIHdr
;
1581 IAVIStreamImpl
*pStream
;
1585 if (This
->hmmio
== NULL
)
1586 return AVIERR_FILEOPEN
;
1588 /* initialize stream ptr's */
1589 memset(This
->ppStreams
, 0, sizeof(This
->ppStreams
));
1591 /* try to get "RIFF" chunk -- must not be at beginning of file! */
1592 ckRIFF
.fccType
= formtypeAVI
;
1593 if (mmioDescend(This
->hmmio
, &ckRIFF
, NULL
, MMIO_FINDRIFF
) != S_OK
) {
1594 ERR(": not an AVI!\n");
1595 return AVIERR_FILEREAD
;
1598 /* get "LIST" "hdrl" */
1599 ckLIST1
.fccType
= listtypeAVIHEADER
;
1600 hr
= FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ckLIST1
, &ckRIFF
, MMIO_FINDLIST
);
1604 /* get "avih" chunk */
1605 ck
.ckid
= ckidAVIMAINHDR
;
1606 hr
= FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ck
, &ckLIST1
, MMIO_FINDCHUNK
);
1610 if (ck
.cksize
!= sizeof(MainAVIHdr
)) {
1611 ERR(": invalid size of %ld for MainAVIHeader!\n", ck
.cksize
);
1612 return AVIERR_BADFORMAT
;
1614 if (mmioRead(This
->hmmio
, (HPSTR
)&MainAVIHdr
, ck
.cksize
) != ck
.cksize
)
1615 return AVIERR_FILEREAD
;
1617 /* check for MAX_AVISTREAMS limit */
1618 if (MainAVIHdr
.dwStreams
> MAX_AVISTREAMS
) {
1619 WARN("file contains %lu streams, but only supports %d -- change MAX_AVISTREAMS!\n", MainAVIHdr
.dwStreams
, MAX_AVISTREAMS
);
1620 return AVIERR_UNSUPPORTED
;
1623 /* adjust permissions if copyrighted material in file */
1624 if (MainAVIHdr
.dwFlags
& AVIFILEINFO_COPYRIGHTED
) {
1625 This
->uMode
&= ~MMIO_RWMODE
;
1626 This
->uMode
|= MMIO_READ
;
1629 /* convert MainAVIHeader into AVIFILINFOW */
1630 memset(&This
->fInfo
, 0, sizeof(This
->fInfo
));
1631 This
->fInfo
.dwRate
= MainAVIHdr
.dwMicroSecPerFrame
;
1632 This
->fInfo
.dwScale
= 1000000;
1633 This
->fInfo
.dwMaxBytesPerSec
= MainAVIHdr
.dwMaxBytesPerSec
;
1634 This
->fInfo
.dwFlags
= MainAVIHdr
.dwFlags
;
1635 This
->fInfo
.dwCaps
= AVIFILECAPS_CANREAD
|AVIFILECAPS_CANWRITE
;
1636 This
->fInfo
.dwLength
= MainAVIHdr
.dwTotalFrames
;
1637 This
->fInfo
.dwStreams
= MainAVIHdr
.dwStreams
;
1638 This
->fInfo
.dwSuggestedBufferSize
= MainAVIHdr
.dwSuggestedBufferSize
;
1639 This
->fInfo
.dwWidth
= MainAVIHdr
.dwWidth
;
1640 This
->fInfo
.dwHeight
= MainAVIHdr
.dwHeight
;
1641 LoadStringW(AVIFILE_hModule
, IDS_AVIFILETYPE
, This
->fInfo
.szFileType
,
1642 sizeof(This
->fInfo
.szFileType
));
1644 /* go back to into header list */
1645 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1646 return AVIERR_FILEREAD
;
1648 /* foreach stream exists a "LIST","strl" chunk */
1649 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
1650 /* get next nested chunk in this "LIST","strl" */
1651 if (mmioDescend(This
->hmmio
, &ckLIST2
, &ckLIST1
, 0) != S_OK
)
1652 return AVIERR_FILEREAD
;
1654 /* nested chunk must be of type "LIST","strl" -- when not normally JUNK */
1655 if (ckLIST2
.ckid
== FOURCC_LIST
&&
1656 ckLIST2
.fccType
== listtypeSTREAMHEADER
) {
1657 pStream
= This
->ppStreams
[nStream
] =
1658 (IAVIStreamImpl
*)LocalAlloc(LPTR
, sizeof(IAVIStreamImpl
));
1659 if (pStream
== NULL
)
1660 return AVIERR_MEMORY
;
1661 AVIFILE_ConstructAVIStream(This
, nStream
, NULL
);
1664 while (mmioDescend(This
->hmmio
, &ck
, &ckLIST2
, 0) == S_OK
) {
1666 case ckidSTREAMHANDLERDATA
:
1667 if (pStream
->lpHandlerData
!= NULL
)
1668 return AVIERR_BADFORMAT
;
1669 pStream
->lpHandlerData
= GlobalAllocPtr(GMEM_DDESHARE
|GMEM_MOVEABLE
,
1671 if (pStream
->lpHandlerData
== NULL
)
1672 return AVIERR_MEMORY
;
1673 pStream
->cbHandlerData
= ck
.cksize
;
1675 if (mmioRead(This
->hmmio
, (HPSTR
)pStream
->lpHandlerData
, ck
.cksize
) != ck
.cksize
)
1676 return AVIERR_FILEREAD
;
1678 case ckidSTREAMFORMAT
:
1679 if (pStream
->lpFormat
!= NULL
)
1680 return AVIERR_BADFORMAT
;
1681 pStream
->lpFormat
= GlobalAllocPtr(GMEM_DDESHARE
|GMEM_MOVEABLE
,
1683 if (pStream
->lpFormat
== NULL
)
1684 return AVIERR_MEMORY
;
1685 pStream
->cbFormat
= ck
.cksize
;
1687 if (mmioRead(This
->hmmio
, (HPSTR
)pStream
->lpFormat
, ck
.cksize
) != ck
.cksize
)
1688 return AVIERR_FILEREAD
;
1690 if (pStream
->sInfo
.fccType
== streamtypeVIDEO
) {
1691 LPBITMAPINFOHEADER lpbi
= (LPBITMAPINFOHEADER
)pStream
->lpFormat
;
1693 /* some corrections to the video format */
1694 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
1695 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
1696 if (lpbi
->biCompression
== BI_RGB
&& lpbi
->biSizeImage
== 0)
1697 lpbi
->biSizeImage
= DIBWIDTHBYTES(*lpbi
) * lpbi
->biHeight
;
1698 if (lpbi
->biCompression
!= BI_RGB
&& lpbi
->biBitCount
== 8) {
1699 if (pStream
->sInfo
.fccHandler
== mmioFOURCC('R','L','E','0') ||
1700 pStream
->sInfo
.fccHandler
== mmioFOURCC('R','L','E',' '))
1701 lpbi
->biCompression
= BI_RLE8
;
1703 if (lpbi
->biCompression
== BI_RGB
&&
1704 (pStream
->sInfo
.fccHandler
== 0 ||
1705 pStream
->sInfo
.fccHandler
== mmioFOURCC('N','O','N','E')))
1706 pStream
->sInfo
.fccHandler
= comptypeDIB
;
1708 /* init rcFrame if it's empty */
1709 if (IsRectEmpty(&pStream
->sInfo
.rcFrame
))
1710 SetRect(&pStream
->sInfo
.rcFrame
, 0, 0, lpbi
->biWidth
, lpbi
->biHeight
);
1713 case ckidSTREAMHEADER
:
1715 static const WCHAR streamTypeFmt
[] = {'%','4','.','4','h','s',0};
1717 AVIStreamHeader streamHdr
;
1719 WCHAR streamNameFmt
[25];
1723 if (ck
.cksize
> sizeof(streamHdr
))
1724 n
= sizeof(streamHdr
);
1726 if (mmioRead(This
->hmmio
, (HPSTR
)&streamHdr
, n
) != n
)
1727 return AVIERR_FILEREAD
;
1729 pStream
->sInfo
.fccType
= streamHdr
.fccType
;
1730 pStream
->sInfo
.fccHandler
= streamHdr
.fccHandler
;
1731 pStream
->sInfo
.dwFlags
= streamHdr
.dwFlags
;
1732 pStream
->sInfo
.wPriority
= streamHdr
.wPriority
;
1733 pStream
->sInfo
.wLanguage
= streamHdr
.wLanguage
;
1734 pStream
->sInfo
.dwInitialFrames
= streamHdr
.dwInitialFrames
;
1735 pStream
->sInfo
.dwScale
= streamHdr
.dwScale
;
1736 pStream
->sInfo
.dwRate
= streamHdr
.dwRate
;
1737 pStream
->sInfo
.dwStart
= streamHdr
.dwStart
;
1738 pStream
->sInfo
.dwLength
= streamHdr
.dwLength
;
1739 pStream
->sInfo
.dwSuggestedBufferSize
=
1740 streamHdr
.dwSuggestedBufferSize
;
1741 pStream
->sInfo
.dwQuality
= streamHdr
.dwQuality
;
1742 pStream
->sInfo
.dwSampleSize
= streamHdr
.dwSampleSize
;
1743 pStream
->sInfo
.rcFrame
.left
= streamHdr
.rcFrame
.left
;
1744 pStream
->sInfo
.rcFrame
.top
= streamHdr
.rcFrame
.top
;
1745 pStream
->sInfo
.rcFrame
.right
= streamHdr
.rcFrame
.right
;
1746 pStream
->sInfo
.rcFrame
.bottom
= streamHdr
.rcFrame
.bottom
;
1747 pStream
->sInfo
.dwEditCount
= 0;
1748 pStream
->sInfo
.dwFormatChangeCount
= 0;
1750 /* generate description for stream like "filename.avi Type #n" */
1751 if (streamHdr
.fccType
== streamtypeVIDEO
)
1752 LoadStringW(AVIFILE_hModule
, IDS_VIDEO
, szType
, sizeof(szType
));
1753 else if (streamHdr
.fccType
== streamtypeAUDIO
)
1754 LoadStringW(AVIFILE_hModule
, IDS_AUDIO
, szType
, sizeof(szType
));
1756 wsprintfW(szType
, streamTypeFmt
, (char*)&streamHdr
.fccType
);
1758 /* get count of this streamtype up to this stream */
1760 for (n
= nStream
; 0 <= n
; n
--) {
1761 if (This
->ppStreams
[n
]->sInfo
.fccHandler
== streamHdr
.fccType
)
1765 memset(pStream
->sInfo
.szName
, 0, sizeof(pStream
->sInfo
.szName
));
1767 LoadStringW(AVIFILE_hModule
, IDS_AVISTREAMFORMAT
, streamNameFmt
, sizeof(streamNameFmt
));
1769 /* FIXME: avoid overflow -- better use wsnprintfW, which doesn't exists ! */
1770 wsprintfW(pStream
->sInfo
.szName
, streamNameFmt
,
1771 AVIFILE_BasenameW(This
->szFileName
), szType
, count
);
1774 case ckidSTREAMNAME
:
1775 { /* streamname will be saved as ASCII string */
1776 LPSTR str
= (LPSTR
)LocalAlloc(LMEM_FIXED
, ck
.cksize
);
1778 return AVIERR_MEMORY
;
1780 if (mmioRead(This
->hmmio
, (HPSTR
)str
, ck
.cksize
) != ck
.cksize
)
1781 return AVIERR_FILEREAD
;
1783 MultiByteToWideChar(CP_ACP
, 0, str
, -1, pStream
->sInfo
.szName
,
1784 sizeof(pStream
->sInfo
.szName
)/sizeof(pStream
->sInfo
.szName
[0]));
1786 LocalFree((HLOCAL
)str
);
1789 case ckidAVIPADDING
:
1790 case mmioFOURCC('p','a','d','d'):
1793 WARN(": found extra chunk 0x%08lX\n", ck
.ckid
);
1794 hr
= ReadChunkIntoExtra(&pStream
->extra
, This
->hmmio
, &ck
);
1799 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1800 return AVIERR_FILEREAD
;
1803 /* nested chunks in "LIST","hdrl" which are not of type "LIST","strl" */
1804 hr
= ReadChunkIntoExtra(&This
->fileextra
, This
->hmmio
, &ckLIST2
);
1808 if (mmioAscend(This
->hmmio
, &ckLIST2
, 0) != S_OK
)
1809 return AVIERR_FILEREAD
;
1812 /* read any extra headers in "LIST","hdrl" */
1813 FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ck
, &ckLIST1
, 0);
1814 if (mmioAscend(This
->hmmio
, &ckLIST1
, 0) != S_OK
)
1815 return AVIERR_FILEREAD
;
1817 /* search "LIST","movi" chunk in "RIFF","AVI " */
1818 ckLIST1
.fccType
= listtypeAVIMOVIE
;
1819 hr
= FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ckLIST1
, &ckRIFF
,
1824 This
->dwMoviChunkPos
= ckLIST1
.dwDataOffset
;
1825 This
->dwIdxChunkPos
= ckLIST1
.cksize
+ ckLIST1
.dwDataOffset
;
1826 if (mmioAscend(This
->hmmio
, &ckLIST1
, 0) != S_OK
)
1827 return AVIERR_FILEREAD
;
1829 /* try to find an index */
1830 ck
.ckid
= ckidAVINEWINDEX
;
1831 hr
= FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
,
1832 &ck
, &ckRIFF
, MMIO_FINDCHUNK
);
1833 if (SUCCEEDED(hr
) && ck
.cksize
> 0) {
1834 if (FAILED(AVIFILE_LoadIndex(This
, ck
.cksize
, ckLIST1
.dwDataOffset
)))
1835 This
->fInfo
.dwFlags
&= ~AVIFILEINFO_HASINDEX
;
1838 /* when we haven't found an index or it's bad, then build one
1839 * by parsing 'movi' chunk */
1840 if ((This
->fInfo
.dwFlags
& AVIFILEINFO_HASINDEX
) == 0) {
1841 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++)
1842 This
->ppStreams
[nStream
]->lLastFrame
= -1;
1844 if (mmioSeek(This
->hmmio
, ckLIST1
.dwDataOffset
+ sizeof(DWORD
), SEEK_SET
) == -1) {
1845 ERR(": Oops, can't seek back to 'movi' chunk!\n");
1846 return AVIERR_FILEREAD
;
1849 /* seek through the 'movi' list until end */
1850 while (mmioDescend(This
->hmmio
, &ck
, &ckLIST1
, 0) == S_OK
) {
1851 if (ck
.ckid
!= FOURCC_LIST
) {
1852 if (mmioAscend(This
->hmmio
, &ck
, 0) == S_OK
) {
1853 nStream
= StreamFromFOURCC(ck
.ckid
);
1855 if (nStream
> This
->fInfo
.dwStreams
)
1856 return AVIERR_BADFORMAT
;
1858 AVIFILE_AddFrame(This
->ppStreams
[nStream
], ck
.ckid
, ck
.cksize
,
1859 ck
.dwDataOffset
- 2 * sizeof(DWORD
), 0);
1861 nStream
= StreamFromFOURCC(ck
.ckid
);
1862 WARN(": file seems to be truncated!\n");
1863 if (nStream
<= This
->fInfo
.dwStreams
&&
1864 This
->ppStreams
[nStream
]->sInfo
.dwSampleSize
> 0) {
1865 ck
.cksize
= mmioSeek(This
->hmmio
, 0, SEEK_END
);
1866 if (ck
.cksize
!= -1) {
1867 ck
.cksize
-= ck
.dwDataOffset
;
1868 ck
.cksize
&= ~(This
->ppStreams
[nStream
]->sInfo
.dwSampleSize
- 1);
1870 AVIFILE_AddFrame(This
->ppStreams
[nStream
], ck
.ckid
, ck
.cksize
,
1871 ck
.dwDataOffset
- 2 * sizeof(DWORD
), 0);
1879 /* find other chunks */
1880 FindChunkAndKeepExtras(&This
->fileextra
, This
->hmmio
, &ck
, &ckRIFF
, 0);
1885 static HRESULT
AVIFILE_LoadIndex(IAVIFileImpl
*This
, DWORD size
, DWORD offset
)
1889 HRESULT hr
= AVIERR_OK
;
1890 BOOL bAbsolute
= TRUE
;
1892 lp
= (AVIINDEXENTRY
*)GlobalAllocPtr(GMEM_MOVEABLE
,
1893 IDX_PER_BLOCK
* sizeof(AVIINDEXENTRY
));
1895 return AVIERR_MEMORY
;
1897 /* adjust limits for index tables, so that inserting will be faster */
1898 for (n
= 0; n
< This
->fInfo
.dwStreams
; n
++) {
1899 IAVIStreamImpl
*pStream
= This
->ppStreams
[n
];
1901 pStream
->lLastFrame
= -1;
1903 if (pStream
->idxFrames
!= NULL
) {
1904 GlobalFreePtr(pStream
->idxFrames
);
1905 pStream
->idxFrames
= NULL
;
1906 pStream
->nIdxFrames
= 0;
1909 if (pStream
->sInfo
.dwSampleSize
!= 0) {
1910 if (n
> 0 && This
->fInfo
.dwFlags
& AVIFILEINFO_ISINTERLEAVED
) {
1911 pStream
->nIdxFrames
= This
->ppStreams
[0]->nIdxFrames
;
1912 } else if (pStream
->sInfo
.dwSuggestedBufferSize
) {
1913 pStream
->nIdxFrames
=
1914 pStream
->sInfo
.dwLength
/ pStream
->sInfo
.dwSuggestedBufferSize
;
1917 pStream
->nIdxFrames
= pStream
->sInfo
.dwLength
;
1919 pStream
->idxFrames
=
1920 (AVIINDEXENTRY
*)GlobalAllocPtr(GHND
, pStream
->nIdxFrames
* sizeof(AVIINDEXENTRY
));
1921 if (pStream
->idxFrames
== NULL
&& pStream
->nIdxFrames
> 0) {
1922 pStream
->nIdxFrames
= 0;
1923 return AVIERR_MEMORY
;
1929 LONG read
= min(IDX_PER_BLOCK
* sizeof(AVIINDEXENTRY
), size
);
1931 if (mmioRead(This
->hmmio
, (HPSTR
)lp
, read
) != read
) {
1932 hr
= AVIERR_FILEREAD
;
1937 if (pos
== (DWORD
)-1)
1938 pos
= offset
- lp
->dwChunkOffset
+ sizeof(DWORD
);
1940 AVIFILE_ParseIndex(This
, lp
, read
/ sizeof(AVIINDEXENTRY
),
1948 for (n
= 0; n
< This
->fInfo
.dwStreams
; n
++) {
1949 IAVIStreamImpl
*pStream
= This
->ppStreams
[n
];
1951 if (pStream
->sInfo
.dwSampleSize
== 0 &&
1952 pStream
->sInfo
.dwLength
!= pStream
->lLastFrame
+1)
1953 ERR("stream %lu length mismatch: dwLength=%lu found=%ld\n",
1954 n
, pStream
->sInfo
.dwLength
, pStream
->lLastFrame
);
1960 static HRESULT
AVIFILE_ParseIndex(IAVIFileImpl
*This
, AVIINDEXENTRY
*lp
,
1961 LONG count
, DWORD pos
, BOOL
*bAbsolute
)
1964 return AVIERR_BADPARAM
;
1966 for (; count
> 0; count
--, lp
++) {
1967 WORD nStream
= StreamFromFOURCC(lp
->ckid
);
1969 if (lp
->ckid
== listtypeAVIRECORD
|| nStream
== 0x7F)
1970 continue; /* skip these */
1972 if (nStream
> This
->fInfo
.dwStreams
)
1973 return AVIERR_BADFORMAT
;
1975 if (*bAbsolute
== TRUE
&& lp
->dwChunkOffset
< This
->dwMoviChunkPos
)
1979 lp
->dwChunkOffset
+= sizeof(DWORD
);
1981 lp
->dwChunkOffset
+= pos
;
1983 if (FAILED(AVIFILE_AddFrame(This
->ppStreams
[nStream
], lp
->ckid
, lp
->dwChunkLength
, lp
->dwChunkOffset
, lp
->dwFlags
)))
1984 return AVIERR_MEMORY
;
1990 static HRESULT
AVIFILE_ReadBlock(IAVIStreamImpl
*This
, DWORD pos
,
1991 LPVOID buffer
, LONG size
)
1993 /* pre-conditions */
1994 assert(This
!= NULL
);
1995 assert(This
->paf
!= NULL
);
1996 assert(This
->paf
->hmmio
!= NULL
);
1997 assert(This
->sInfo
.dwStart
<= pos
&& pos
< This
->sInfo
.dwLength
);
1998 assert(pos
<= This
->lLastFrame
);
2000 /* should we read as much as block gives us? */
2001 if (size
== 0 || size
> This
->idxFrames
[pos
].dwChunkLength
)
2002 size
= This
->idxFrames
[pos
].dwChunkLength
;
2004 /* read into out own buffer or given one? */
2005 if (buffer
== NULL
) {
2006 /* we also read the chunk */
2007 size
+= 2 * sizeof(DWORD
);
2009 /* check that buffer is big enough -- don't trust dwSuggestedBufferSize */
2010 if (This
->lpBuffer
== NULL
|| size
< This
->cbBuffer
) {
2012 (LPDWORD
)GlobalReAllocPtr(This
->lpBuffer
, max(size
, This
->sInfo
.dwSuggestedBufferSize
), GMEM_MOVEABLE
);
2013 if (This
->lpBuffer
== NULL
)
2014 return AVIERR_MEMORY
;
2015 This
->cbBuffer
= max(size
, This
->sInfo
.dwSuggestedBufferSize
);
2018 /* now read the complete chunk into our buffer */
2019 if (mmioSeek(This
->paf
->hmmio
, This
->idxFrames
[pos
].dwChunkOffset
, SEEK_SET
) == -1)
2020 return AVIERR_FILEREAD
;
2021 if (mmioRead(This
->paf
->hmmio
, (HPSTR
)This
->lpBuffer
, size
) != size
)
2022 return AVIERR_FILEREAD
;
2024 /* check if it was the correct block which we have read */
2025 if (This
->lpBuffer
[0] != This
->idxFrames
[pos
].ckid
||
2026 This
->lpBuffer
[1] != This
->idxFrames
[pos
].dwChunkLength
) {
2027 ERR(": block %ld not found at 0x%08lX\n", pos
, This
->idxFrames
[pos
].dwChunkOffset
);
2028 ERR(": Index says: '%4.4s'(0x%08lX) size 0x%08lX\n",
2029 (char*)&This
->idxFrames
[pos
].ckid
, This
->idxFrames
[pos
].ckid
,
2030 This
->idxFrames
[pos
].dwChunkLength
);
2031 ERR(": Data says: '%4.4s'(0x%08lX) size 0x%08lX\n",
2032 (char*)&This
->lpBuffer
[0], This
->lpBuffer
[0], This
->lpBuffer
[1]);
2033 return AVIERR_FILEREAD
;
2036 if (mmioSeek(This
->paf
->hmmio
, This
->idxFrames
[pos
].dwChunkOffset
+ 2 * sizeof(DWORD
), SEEK_SET
) == -1)
2037 return AVIERR_FILEREAD
;
2038 if (mmioRead(This
->paf
->hmmio
, (HPSTR
)buffer
, size
) != size
)
2039 return AVIERR_FILEREAD
;
2045 static void AVIFILE_SamplesToBlock(IAVIStreamImpl
*This
, LPLONG pos
,
2050 /* pre-conditions */
2051 assert(This
!= NULL
);
2052 assert(pos
!= NULL
);
2053 assert(offset
!= NULL
);
2054 assert(This
->sInfo
.dwSampleSize
!= 0);
2055 assert(*pos
>= This
->sInfo
.dwStart
);
2057 /* convert start sample to start bytes */
2058 (*offset
) = (*pos
) - This
->sInfo
.dwStart
;
2059 (*offset
) *= This
->sInfo
.dwSampleSize
;
2061 /* convert bytes to block number */
2062 for (block
= 0; block
<= This
->lLastFrame
; block
++) {
2063 if (This
->idxFrames
[block
].dwChunkLength
<= *offset
)
2064 (*offset
) -= This
->idxFrames
[block
].dwChunkLength
;
2072 static HRESULT
AVIFILE_SaveFile(IAVIFileImpl
*This
)
2074 MainAVIHeader MainAVIHdr
;
2075 IAVIStreamImpl
* pStream
;
2084 /* initialize some things */
2085 if (This
->dwMoviChunkPos
== 0)
2086 AVIFILE_ComputeMoviStart(This
);
2088 /* written one record to much? */
2089 if (This
->ckLastRecord
.dwFlags
& MMIO_DIRTY
) {
2090 This
->dwNextFramePos
-= 3 * sizeof(DWORD
);
2091 if (This
->nIdxRecords
> 0)
2092 This
->nIdxRecords
--;
2095 AVIFILE_UpdateInfo(This
);
2097 assert(This
->fInfo
.dwScale
!= 0);
2099 memset(&MainAVIHdr
, 0, sizeof(MainAVIHdr
));
2100 MainAVIHdr
.dwMicroSecPerFrame
= MulDiv(This
->fInfo
.dwRate
, 1000000,
2101 This
->fInfo
.dwScale
);
2102 MainAVIHdr
.dwMaxBytesPerSec
= This
->fInfo
.dwMaxBytesPerSec
;
2103 MainAVIHdr
.dwPaddingGranularity
= AVI_HEADERSIZE
;
2104 MainAVIHdr
.dwFlags
= This
->fInfo
.dwFlags
;
2105 MainAVIHdr
.dwTotalFrames
= This
->fInfo
.dwLength
;
2106 MainAVIHdr
.dwInitialFrames
= 0;
2107 MainAVIHdr
.dwStreams
= This
->fInfo
.dwStreams
;
2108 MainAVIHdr
.dwSuggestedBufferSize
= This
->fInfo
.dwSuggestedBufferSize
;
2109 MainAVIHdr
.dwWidth
= This
->fInfo
.dwWidth
;
2110 MainAVIHdr
.dwHeight
= This
->fInfo
.dwHeight
;
2111 MainAVIHdr
.dwInitialFrames
= This
->dwInitialFrames
;
2113 /* now begin writing ... */
2114 mmioSeek(This
->hmmio
, 0, SEEK_SET
);
2118 ckRIFF
.fccType
= formtypeAVI
;
2119 if (mmioCreateChunk(This
->hmmio
, &ckRIFF
, MMIO_CREATERIFF
) != S_OK
)
2120 return AVIERR_FILEWRITE
;
2122 /* AVI headerlist */
2124 ckLIST1
.fccType
= listtypeAVIHEADER
;
2125 if (mmioCreateChunk(This
->hmmio
, &ckLIST1
, MMIO_CREATELIST
) != S_OK
)
2126 return AVIERR_FILEWRITE
;
2129 ck
.ckid
= ckidAVIMAINHDR
;
2130 ck
.cksize
= sizeof(MainAVIHdr
);
2132 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2133 return AVIERR_FILEWRITE
;
2134 if (mmioWrite(This
->hmmio
, (HPSTR
)&MainAVIHdr
, ck
.cksize
) != ck
.cksize
)
2135 return AVIERR_FILEWRITE
;
2136 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2137 return AVIERR_FILEWRITE
;
2139 /* write the headers of each stream into a separate streamheader list */
2140 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
2141 AVIStreamHeader strHdr
;
2143 pStream
= This
->ppStreams
[nStream
];
2145 /* begin the new streamheader list */
2147 ckLIST2
.fccType
= listtypeSTREAMHEADER
;
2148 if (mmioCreateChunk(This
->hmmio
, &ckLIST2
, MMIO_CREATELIST
) != S_OK
)
2149 return AVIERR_FILEWRITE
;
2151 /* create an AVIStreamHeader from the AVSTREAMINFO */
2152 strHdr
.fccType
= pStream
->sInfo
.fccType
;
2153 strHdr
.fccHandler
= pStream
->sInfo
.fccHandler
;
2154 strHdr
.dwFlags
= pStream
->sInfo
.dwFlags
;
2155 strHdr
.wPriority
= pStream
->sInfo
.wPriority
;
2156 strHdr
.wLanguage
= pStream
->sInfo
.wLanguage
;
2157 strHdr
.dwInitialFrames
= pStream
->sInfo
.dwInitialFrames
;
2158 strHdr
.dwScale
= pStream
->sInfo
.dwScale
;
2159 strHdr
.dwRate
= pStream
->sInfo
.dwRate
;
2160 strHdr
.dwStart
= pStream
->sInfo
.dwStart
;
2161 strHdr
.dwLength
= pStream
->sInfo
.dwLength
;
2162 strHdr
.dwSuggestedBufferSize
= pStream
->sInfo
.dwSuggestedBufferSize
;
2163 strHdr
.dwQuality
= pStream
->sInfo
.dwQuality
;
2164 strHdr
.dwSampleSize
= pStream
->sInfo
.dwSampleSize
;
2165 strHdr
.rcFrame
.left
= pStream
->sInfo
.rcFrame
.left
;
2166 strHdr
.rcFrame
.top
= pStream
->sInfo
.rcFrame
.top
;
2167 strHdr
.rcFrame
.right
= pStream
->sInfo
.rcFrame
.right
;
2168 strHdr
.rcFrame
.bottom
= pStream
->sInfo
.rcFrame
.bottom
;
2170 /* now write the AVIStreamHeader */
2171 ck
.ckid
= ckidSTREAMHEADER
;
2172 ck
.cksize
= sizeof(strHdr
);
2173 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2174 return AVIERR_FILEWRITE
;
2175 if (mmioWrite(This
->hmmio
, (HPSTR
)&strHdr
, ck
.cksize
) != ck
.cksize
)
2176 return AVIERR_FILEWRITE
;
2177 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2178 return AVIERR_FILEWRITE
;
2180 /* ... the hopefully ever present streamformat ... */
2181 ck
.ckid
= ckidSTREAMFORMAT
;
2182 ck
.cksize
= pStream
->cbFormat
;
2183 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2184 return AVIERR_FILEWRITE
;
2185 if (pStream
->lpFormat
!= NULL
&& ck
.cksize
> 0) {
2186 if (mmioWrite(This
->hmmio
, (HPSTR
)pStream
->lpFormat
, ck
.cksize
) != ck
.cksize
)
2187 return AVIERR_FILEWRITE
;
2189 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2190 return AVIERR_FILEWRITE
;
2192 /* ... some optional existing handler data ... */
2193 if (pStream
->lpHandlerData
!= NULL
&& pStream
->cbHandlerData
> 0) {
2194 ck
.ckid
= ckidSTREAMHANDLERDATA
;
2195 ck
.cksize
= pStream
->cbHandlerData
;
2196 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2197 return AVIERR_FILEWRITE
;
2198 if (mmioWrite(This
->hmmio
, (HPSTR
)pStream
->lpHandlerData
, ck
.cksize
) != ck
.cksize
)
2199 return AVIERR_FILEWRITE
;
2200 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2201 return AVIERR_FILEWRITE
;
2204 /* ... some optional additional extra chunk for this stream ... */
2205 if (pStream
->extra
.lp
!= NULL
&& pStream
->extra
.cb
> 0) {
2206 /* the chunk header(s) are already in the strucuture */
2207 if (mmioWrite(This
->hmmio
, (HPSTR
)pStream
->extra
.lp
, pStream
->extra
.cb
) != pStream
->extra
.cb
)
2208 return AVIERR_FILEWRITE
;
2211 /* ... an optional name for this stream ... */
2212 if (lstrlenW(pStream
->sInfo
.szName
) > 0) {
2215 ck
.ckid
= ckidSTREAMNAME
;
2216 ck
.cksize
= lstrlenW(pStream
->sInfo
.szName
) + 1;
2217 if (ck
.cksize
& 1) /* align */
2219 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2220 return AVIERR_FILEWRITE
;
2222 /* the streamname must be saved in ASCII not Unicode */
2223 str
= (LPSTR
)LocalAlloc(LPTR
, ck
.cksize
);
2225 return AVIERR_MEMORY
;
2226 WideCharToMultiByte(CP_ACP
, 0, pStream
->sInfo
.szName
, -1, str
,
2227 ck
.cksize
, NULL
, NULL
);
2229 if (mmioWrite(This
->hmmio
, (HPSTR
)str
, ck
.cksize
) != ck
.cksize
) {
2230 LocalFree((HLOCAL
)str
);
2231 return AVIERR_FILEWRITE
;
2234 LocalFree((HLOCAL
)str
);
2235 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2236 return AVIERR_FILEWRITE
;
2239 /* close streamheader list for this stream */
2240 if (mmioAscend(This
->hmmio
, &ckLIST2
, 0) != S_OK
)
2241 return AVIERR_FILEWRITE
;
2242 } /* for (0 <= nStream < MainAVIHdr.dwStreams) */
2244 /* close the aviheader list */
2245 if (mmioAscend(This
->hmmio
, &ckLIST1
, 0) != S_OK
)
2246 return AVIERR_FILEWRITE
;
2248 /* check for padding to pre-guessed 'movi'-chunk position */
2249 dwPos
= ckLIST1
.dwDataOffset
+ ckLIST1
.cksize
;
2250 if (This
->dwMoviChunkPos
- 2 * sizeof(DWORD
) > dwPos
) {
2251 ck
.ckid
= ckidAVIPADDING
;
2252 ck
.cksize
= This
->dwMoviChunkPos
- dwPos
- 4 * sizeof(DWORD
);
2253 assert((LONG
)ck
.cksize
>= 0);
2255 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2256 return AVIERR_FILEWRITE
;
2257 if (mmioSeek(This
->hmmio
, ck
.cksize
, SEEK_CUR
) == -1)
2258 return AVIERR_FILEWRITE
;
2259 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2260 return AVIERR_FILEWRITE
;
2263 /* now write the 'movi' chunk */
2264 mmioSeek(This
->hmmio
, This
->dwMoviChunkPos
- 2 * sizeof(DWORD
), SEEK_SET
);
2266 ckLIST1
.fccType
= listtypeAVIMOVIE
;
2267 if (mmioCreateChunk(This
->hmmio
, &ckLIST1
, MMIO_CREATELIST
) != S_OK
)
2268 return AVIERR_FILEWRITE
;
2269 if (mmioSeek(This
->hmmio
, This
->dwNextFramePos
, SEEK_SET
) == -1)
2270 return AVIERR_FILEWRITE
;
2271 if (mmioAscend(This
->hmmio
, &ckLIST1
, 0) != S_OK
)
2272 return AVIERR_FILEWRITE
;
2274 /* write 'idx1' chunk */
2275 hr
= AVIFILE_SaveIndex(This
);
2279 /* write optional extra file chunks */
2280 if (This
->fileextra
.lp
!= NULL
&& This
->fileextra
.cb
> 0) {
2281 /* as for the streams, are the chunk header(s) in the structure */
2282 if (mmioWrite(This
->hmmio
, (HPSTR
)This
->fileextra
.lp
, This
->fileextra
.cb
) != This
->fileextra
.cb
)
2283 return AVIERR_FILEWRITE
;
2286 /* close RIFF chunk */
2287 if (mmioAscend(This
->hmmio
, &ckRIFF
, 0) != S_OK
)
2288 return AVIERR_FILEWRITE
;
2290 /* add some JUNK at end for bad parsers */
2291 memset(&ckRIFF
, 0, sizeof(ckRIFF
));
2292 mmioWrite(This
->hmmio
, (HPSTR
)&ckRIFF
, sizeof(ckRIFF
));
2293 mmioFlush(This
->hmmio
, 0);
2298 static HRESULT
AVIFILE_SaveIndex(IAVIFileImpl
*This
)
2300 IAVIStreamImpl
*pStream
;
2306 ck
.ckid
= ckidAVINEWINDEX
;
2308 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
2309 return AVIERR_FILEWRITE
;
2311 if (This
->fInfo
.dwFlags
& AVIFILEINFO_ISINTERLEAVED
) {
2312 /* is interleaved -- write block of coresponding frames */
2313 LONG lInitialFrames
= 0;
2317 if (This
->ppStreams
[0]->sInfo
.dwSampleSize
== 0)
2320 stepsize
= AVIStreamTimeToSample((PAVISTREAM
)This
->ppStreams
[0], 1000000);
2322 assert(stepsize
> 0);
2324 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
2325 if (lInitialFrames
< This
->ppStreams
[nStream
]->sInfo
.dwInitialFrames
)
2326 lInitialFrames
= This
->ppStreams
[nStream
]->sInfo
.dwInitialFrames
;
2329 for (i
= -lInitialFrames
; i
< (LONG
)This
->fInfo
.dwLength
- lInitialFrames
;
2331 DWORD nFrame
= lInitialFrames
+ i
;
2333 assert(nFrame
< This
->nIdxRecords
);
2335 idx
.ckid
= listtypeAVIRECORD
;
2336 idx
.dwFlags
= AVIIF_LIST
;
2337 idx
.dwChunkLength
= This
->idxRecords
[nFrame
].dwChunkLength
;
2338 idx
.dwChunkOffset
= This
->idxRecords
[nFrame
].dwChunkOffset
2339 - This
->dwMoviChunkPos
;
2340 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2341 return AVIERR_FILEWRITE
;
2343 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
2344 pStream
= This
->ppStreams
[nStream
];
2346 /* heave we reached start of this stream? */
2347 if (-(LONG
)pStream
->sInfo
.dwInitialFrames
> i
)
2350 if (pStream
->sInfo
.dwInitialFrames
< lInitialFrames
)
2351 nFrame
-= (lInitialFrames
- pStream
->sInfo
.dwInitialFrames
);
2353 /* reached end of this stream? */
2354 if (pStream
->lLastFrame
<= nFrame
)
2357 if ((pStream
->sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
) &&
2358 pStream
->sInfo
.dwFormatChangeCount
!= 0 &&
2359 pStream
->idxFmtChanges
!= NULL
) {
2362 for (pos
= 0; pos
< pStream
->sInfo
.dwFormatChangeCount
; pos
++) {
2363 if (pStream
->idxFmtChanges
[pos
].ckid
== nFrame
) {
2364 idx
.dwFlags
= AVIIF_NOTIME
;
2365 idx
.ckid
= MAKEAVICKID(cktypePALchange
, pStream
->nStream
);
2366 idx
.dwChunkLength
= pStream
->idxFmtChanges
[pos
].dwChunkLength
;
2367 idx
.dwChunkOffset
= pStream
->idxFmtChanges
[pos
].dwChunkOffset
2368 - This
->dwMoviChunkPos
;
2370 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2371 return AVIERR_FILEWRITE
;
2375 } /* if have formatchanges */
2377 idx
.ckid
= pStream
->idxFrames
[nFrame
].ckid
;
2378 idx
.dwFlags
= pStream
->idxFrames
[nFrame
].dwFlags
;
2379 idx
.dwChunkLength
= pStream
->idxFrames
[nFrame
].dwChunkLength
;
2380 idx
.dwChunkOffset
= pStream
->idxFrames
[nFrame
].dwChunkOffset
2381 - This
->dwMoviChunkPos
;
2382 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2383 return AVIERR_FILEWRITE
;
2387 /* not interleaved -- write index for each stream at once */
2388 for (nStream
= 0; nStream
< This
->fInfo
.dwStreams
; nStream
++) {
2389 pStream
= This
->ppStreams
[nStream
];
2391 if (pStream
->lLastFrame
== -1)
2392 pStream
->lLastFrame
= 0;
2394 for (n
= 0; n
<= pStream
->lLastFrame
; n
++) {
2395 if ((pStream
->sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
) &&
2396 (pStream
->sInfo
.dwFormatChangeCount
!= 0)) {
2399 for (pos
= 0; pos
< pStream
->sInfo
.dwFormatChangeCount
; pos
++) {
2400 if (pStream
->idxFmtChanges
[pos
].ckid
== n
) {
2401 idx
.dwFlags
= AVIIF_NOTIME
;
2402 idx
.ckid
= MAKEAVICKID(cktypePALchange
, pStream
->nStream
);
2403 idx
.dwChunkLength
= pStream
->idxFmtChanges
[pos
].dwChunkLength
;
2405 pStream
->idxFmtChanges
[pos
].dwChunkOffset
- This
->dwMoviChunkPos
;
2406 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2407 return AVIERR_FILEWRITE
;
2411 } /* if have formatchanges */
2413 idx
.ckid
= pStream
->idxFrames
[n
].ckid
;
2414 idx
.dwFlags
= pStream
->idxFrames
[n
].dwFlags
;
2415 idx
.dwChunkLength
= pStream
->idxFrames
[n
].dwChunkLength
;
2416 idx
.dwChunkOffset
= pStream
->idxFrames
[n
].dwChunkOffset
2417 - This
->dwMoviChunkPos
;
2419 if (mmioWrite(This
->hmmio
, (HPSTR
)&idx
, sizeof(idx
)) != sizeof(idx
))
2420 return AVIERR_FILEWRITE
;
2423 } /* if not interleaved */
2425 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
2426 return AVIERR_FILEWRITE
;
2431 static ULONG
AVIFILE_SearchStream(IAVIFileImpl
*This
, DWORD fcc
, LONG lSkip
)
2440 /* search the number of the specified stream */
2441 nStream
= (ULONG
)-1;
2442 for (i
= 0; i
< This
->fInfo
.dwStreams
; i
++) {
2443 assert(This
->ppStreams
[i
] != NULL
);
2445 if (This
->ppStreams
[i
]->sInfo
.fccType
== fcc
) {
2459 static void AVIFILE_UpdateInfo(IAVIFileImpl
*This
)
2463 /* pre-conditions */
2464 assert(This
!= NULL
);
2466 This
->fInfo
.dwMaxBytesPerSec
= 0;
2467 This
->fInfo
.dwCaps
= AVIFILECAPS_CANREAD
|AVIFILECAPS_CANWRITE
;
2468 This
->fInfo
.dwSuggestedBufferSize
= 0;
2469 This
->fInfo
.dwWidth
= 0;
2470 This
->fInfo
.dwHeight
= 0;
2471 This
->fInfo
.dwScale
= 0;
2472 This
->fInfo
.dwRate
= 0;
2473 This
->fInfo
.dwLength
= 0;
2474 This
->dwInitialFrames
= 0;
2476 for (i
= 0; i
< This
->fInfo
.dwStreams
; i
++) {
2477 AVISTREAMINFOW
*psi
;
2480 /* pre-conditions */
2481 assert(This
->ppStreams
[i
] != NULL
);
2483 psi
= &This
->ppStreams
[i
]->sInfo
;
2484 assert(psi
->dwScale
!= 0);
2485 assert(psi
->dwRate
!= 0);
2488 /* use first stream timings as base */
2489 This
->fInfo
.dwScale
= psi
->dwScale
;
2490 This
->fInfo
.dwRate
= psi
->dwRate
;
2491 This
->fInfo
.dwLength
= psi
->dwLength
;
2493 n
= AVIStreamSampleToSample((PAVISTREAM
)This
->ppStreams
[0],
2494 (PAVISTREAM
)This
->ppStreams
[i
],psi
->dwLength
);
2495 if (This
->fInfo
.dwLength
< n
)
2496 This
->fInfo
.dwLength
= n
;
2499 if (This
->dwInitialFrames
< psi
->dwInitialFrames
)
2500 This
->dwInitialFrames
= psi
->dwInitialFrames
;
2502 if (This
->fInfo
.dwSuggestedBufferSize
< psi
->dwSuggestedBufferSize
)
2503 This
->fInfo
.dwSuggestedBufferSize
= psi
->dwSuggestedBufferSize
;
2505 if (psi
->dwSampleSize
!= 0) {
2506 /* fixed sample size -- exact computation */
2507 This
->fInfo
.dwMaxBytesPerSec
+= MulDiv(psi
->dwSampleSize
, psi
->dwRate
,
2510 /* variable sample size -- only upper limit */
2511 This
->fInfo
.dwMaxBytesPerSec
+= MulDiv(psi
->dwSuggestedBufferSize
,
2512 psi
->dwRate
, psi
->dwScale
);
2514 /* update dimensions */
2515 n
= psi
->rcFrame
.right
- psi
->rcFrame
.left
;
2516 if (This
->fInfo
.dwWidth
< n
)
2517 This
->fInfo
.dwWidth
= n
;
2518 n
= psi
->rcFrame
.bottom
- psi
->rcFrame
.top
;
2519 if (This
->fInfo
.dwHeight
< n
)
2520 This
->fInfo
.dwHeight
= n
;
2525 static HRESULT
AVIFILE_WriteBlock(IAVIStreamImpl
*This
, DWORD block
,
2526 FOURCC ckid
, DWORD flags
, LPVOID buffer
,
2535 /* if no frame/block is already written, we must compute start of movi chunk */
2536 if (This
->paf
->dwMoviChunkPos
== 0)
2537 AVIFILE_ComputeMoviStart(This
->paf
);
2539 if (mmioSeek(This
->paf
->hmmio
, This
->paf
->dwNextFramePos
, SEEK_SET
) == -1)
2540 return AVIERR_FILEWRITE
;
2542 if (mmioCreateChunk(This
->paf
->hmmio
, &ck
, 0) != S_OK
)
2543 return AVIERR_FILEWRITE
;
2544 if (buffer
!= NULL
&& size
> 0) {
2545 if (mmioWrite(This
->paf
->hmmio
, (HPSTR
)buffer
, size
) != size
)
2546 return AVIERR_FILEWRITE
;
2548 if (mmioAscend(This
->paf
->hmmio
, &ck
, 0) != S_OK
)
2549 return AVIERR_FILEWRITE
;
2551 This
->paf
->fDirty
= TRUE
;
2552 This
->paf
->dwNextFramePos
= mmioSeek(This
->paf
->hmmio
, 0, SEEK_CUR
);
2554 return AVIFILE_AddFrame(This
, ckid
, size
,
2555 ck
.dwDataOffset
- 2 * sizeof(DWORD
), flags
);