2 * Copyright 2002 Michael Günnewig
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define COM_NO_WINDOWS_H
34 #include "avifile_private.h"
35 #include "extrachunk.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
42 /***********************************************************************/
44 #define formtypeWAVE mmioFOURCC('W','A','V','E')
45 #define ckidWAVEFORMAT mmioFOURCC('f','m','t',' ')
46 #define ckidWAVEFACT mmioFOURCC('f','a','c','t')
47 #define ckidWAVEDATA mmioFOURCC('d','a','t','a')
49 /***********************************************************************/
51 #define ENDIAN_SWAPWORD(x) ((((x) >> 8) & 0xFF) | (((x) & 0xFF) << 8))
52 #define ENDIAN_SWAPDWORD(x) (ENDIAN_SWAPWORD((x >> 16) & 0xFFFF) | \
53 ENDIAN_SWAPWORD(x & 0xFFFF) << 16)
55 #ifdef WORDS_BIGENDIAN
56 #define BE2H_WORD(x) (x)
57 #define BE2H_DWORD(x) (x)
58 #define LE2H_WORD(x) ENDIAN_SWAPWORD(x)
59 #define LE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
61 #define BE2H_WORD(x) ENDIAN_SWAPWORD(x)
62 #define BE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
63 #define LE2H_WORD(x) (x)
64 #define LE2H_DWORD(x) (x)
76 #define AU_ENCODING_ULAW_8 1
77 #define AU_ENCODING_PCM_8 2
78 #define AU_ENCODING_PCM_16 3
79 #define AU_ENCODING_PCM_24 4
80 #define AU_ENCODING_PCM_32 5
81 #define AU_ENCODING_FLOAT 6
82 #define AU_ENCODING_DOUBLE 7
83 #define AU_ENCODING_ADPCM_G721_32 23
84 #define AU_ENCODING_ADPCM_G722 24
85 #define AU_ENCODING_ADPCM_G723_24 25
86 #define AU_ENCODING_ADPCM_G723_5 26
87 #define AU_ENCODING_ALAW_8 27
89 /***********************************************************************/
91 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
92 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
93 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile* iface);
94 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
95 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
96 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
97 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
98 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
99 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
100 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
102 struct ICOM_VTABLE(IAVIFile) iwavft = {
103 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
104 IAVIFile_fnQueryInterface,
108 IAVIFile_fnGetStream,
109 IAVIFile_fnCreateStream,
110 IAVIFile_fnWriteData,
112 IAVIFile_fnEndRecord,
113 IAVIFile_fnDeleteStream
116 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile*iface,REFIID refiid,LPVOID*obj);
117 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile*iface);
118 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile*iface);
119 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile*iface,CLSID*pClassID);
120 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile*iface);
121 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile*iface,LPCOLESTR pszFileName,DWORD dwMode);
122 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile*iface,LPCOLESTR pszFileName,BOOL fRemember);
123 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile*iface,LPCOLESTR pszFileName);
124 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile*iface,LPOLESTR*ppszFileName);
126 struct ICOM_VTABLE(IPersistFile) iwavpft = {
127 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
128 IPersistFile_fnQueryInterface,
129 IPersistFile_fnAddRef,
130 IPersistFile_fnRelease,
131 IPersistFile_fnGetClassID,
132 IPersistFile_fnIsDirty,
135 IPersistFile_fnSaveCompleted,
136 IPersistFile_fnGetCurFile
139 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID *obj);
140 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream*iface);
141 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface);
142 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
143 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
144 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream*iface,LONG pos,LONG flags);
145 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG *formatsize);
146 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
147 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread);
148 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten);
149 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
150 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG *lpread);
151 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG size);
152 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
154 struct ICOM_VTABLE(IAVIStream) iwavst = {
155 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
156 IAVIStream_fnQueryInterface,
158 IAVIStream_fnRelease,
161 IAVIStream_fnFindSample,
162 IAVIStream_fnReadFormat,
163 IAVIStream_fnSetFormat,
167 IAVIStream_fnReadData,
168 IAVIStream_fnWriteData,
172 typedef struct _IAVIFileImpl IAVIFileImpl;
174 typedef struct _IPersistFileImpl {
176 ICOM_VFIELD(IPersistFile);
178 /* IPersistFile stuff */
182 typedef struct _IAVIStreamImpl {
184 ICOM_VFIELD(IAVIStream);
186 /* IAVIStream stuff */
190 struct _IAVIFileImpl {
192 ICOM_VFIELD(IAVIFile);
195 /* IAVIFile, IAVIStream stuff... */
196 IPersistFileImpl iPersistFile;
197 IAVIStreamImpl iAVIStream;
200 AVISTREAMINFOW sInfo;
202 LPWAVEFORMATEX lpFormat;
209 /* IPersistFile stuff ... */
216 /***********************************************************************/
218 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
219 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This);
220 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
222 HRESULT AVIFILE_CreateWAVFile(REFIID riid, LPVOID *ppv)
227 assert(riid != NULL && ppv != NULL);
231 pfile = (IAVIFileImpl*)LocalAlloc(LPTR, sizeof(IAVIFileImpl));
233 return AVIERR_MEMORY;
235 pfile->lpVtbl = &iwavft;
236 pfile->iPersistFile.lpVtbl = &iwavpft;
237 pfile->iAVIStream.lpVtbl = &iwavst;
239 pfile->iPersistFile.paf = pfile;
240 pfile->iAVIStream.paf = pfile;
242 hr = IUnknown_QueryInterface((IUnknown*)pfile, riid, ppv);
244 LocalFree((HLOCAL)pfile);
249 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
252 ICOM_THIS(IAVIFileImpl,iface);
254 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
256 if (IsEqualGUID(&IID_IUnknown, refiid) ||
257 IsEqualGUID(&IID_IAVIFile, refiid)) {
260 } else if (This->fInfo.dwStreams == 1 &&
261 IsEqualGUID(&IID_IAVIStream, refiid)) {
262 *obj = &This->iAVIStream;
264 } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
265 *obj = &This->iPersistFile;
269 return OLE_E_ENUM_NOMORE;
272 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
274 ICOM_THIS(IAVIFileImpl,iface);
276 TRACE("(%p)\n",iface);
278 return ++(This->ref);
281 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
283 ICOM_THIS(IAVIFileImpl,iface);
285 TRACE("(%p)\n",iface);
287 if (!--(This->ref)) {
289 /* need to write headers to file */
290 AVIFILE_SaveFile(This);
293 if (This->lpFormat != NULL) {
294 GlobalFreePtr(This->lpFormat);
295 This->lpFormat = NULL;
298 if (This->extra.lp != NULL) {
299 GlobalFreePtr(This->extra.lp);
300 This->extra.lp = NULL;
303 if (This->szFileName != NULL) {
304 LocalFree((HLOCAL)This->szFileName);
305 This->szFileName = NULL;
307 if (This->hmmio != NULL) {
308 mmioClose(This->hmmio, 0);
312 LocalFree((HLOCAL)This);
318 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
321 ICOM_THIS(IAVIFileImpl,iface);
323 TRACE("(%p,%p,%ld)\n",iface,afi,size);
326 return AVIERR_BADPARAM;
328 return AVIERR_BADSIZE;
330 /* update file info */
331 This->fInfo.dwFlags = 0;
332 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
333 if (This->lpFormat != NULL) {
334 assert(This->sInfo.dwScale != 0);
336 This->fInfo.dwStreams = 1;
337 This->fInfo.dwScale = This->sInfo.dwScale;
338 This->fInfo.dwRate = This->sInfo.dwRate;
339 This->fInfo.dwLength = This->sInfo.dwLength;
340 This->fInfo.dwSuggestedBufferSize = This->ckData.cksize;
341 This->fInfo.dwMaxBytesPerSec =
342 MulDiv(This->sInfo.dwSampleSize,This->sInfo.dwRate,This->sInfo.dwScale);
345 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
347 if ((DWORD)size < sizeof(This->fInfo))
348 return AVIERR_BUFFERTOOSMALL;
352 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
353 DWORD fccType, LONG lParam)
355 ICOM_THIS(IAVIFileImpl,iface);
357 TRACE("(%p,%p,0x%08lX,%ld)\n", iface, avis, fccType, lParam);
359 /* check parameter */
361 return AVIERR_BADPARAM;
365 /* Does our stream exists? */
366 if (lParam != 0 || This->fInfo.dwStreams == 0)
367 return AVIERR_NODATA;
368 if (fccType != 0 && fccType != streamtypeAUDIO)
369 return AVIERR_NODATA;
371 *avis = (PAVISTREAM)&This->iAVIStream;
372 IAVIFile_AddRef(iface);
377 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
378 LPAVISTREAMINFOW asi)
380 ICOM_THIS(IAVIFileImpl,iface);
382 TRACE("(%p,%p,%p)\n", iface, avis, asi);
384 /* check parameters */
385 if (avis == NULL || asi == NULL)
386 return AVIERR_BADPARAM;
390 /* We only support one audio stream */
391 if (This->fInfo.dwStreams != 0 || This->lpFormat != NULL)
392 return AVIERR_UNSUPPORTED;
393 if (asi->fccType != streamtypeAUDIO)
394 return AVIERR_UNSUPPORTED;
396 /* Does the user have write permission? */
397 if ((This->uMode & MMIO_RWMODE) == 0)
398 return AVIERR_READONLY;
401 This->lpFormat = NULL;
403 memcpy(&This->sInfo, asi, sizeof(This->sInfo));
405 /* make sure streaminfo if okay for us */
406 This->sInfo.fccHandler = 0;
407 This->sInfo.dwFlags = 0;
408 This->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
409 This->sInfo.dwStart = 0;
410 This->sInfo.dwInitialFrames = 0;
411 This->sInfo.dwFormatChangeCount = 0;
412 memset(&This->sInfo.rcFrame, 0, sizeof(This->sInfo.rcFrame));
414 This->fInfo.dwStreams = 1;
415 This->fInfo.dwScale = This->sInfo.dwScale;
416 This->fInfo.dwRate = This->sInfo.dwRate;
417 This->fInfo.dwLength = This->sInfo.dwLength;
419 This->ckData.dwDataOffset = 0;
420 This->ckData.cksize = 0;
422 *avis = (PAVISTREAM)&This->iAVIStream;
423 IAVIFile_AddRef(iface);
428 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
429 LPVOID lpData, LONG size)
431 ICOM_THIS(IAVIFileImpl,iface);
433 TRACE("(%p,0x%08lX,%p,%ld)\n", iface, ckid, lpData, size);
435 /* check parameters */
437 return AVIERR_BADPARAM;
439 return AVIERR_BADSIZE;
441 /* Do we have write permission? */
442 if ((This->uMode & MMIO_RWMODE) == 0)
443 return AVIERR_READONLY;
447 return WriteExtraChunk(&This->extra, ckid, lpData, size);
450 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
451 LPVOID lpData, LONG *size)
453 ICOM_THIS(IAVIFileImpl,iface);
455 TRACE("(%p,0x%08lX,%p,%p)\n", iface, ckid, lpData, size);
457 return ReadExtraChunk(&This->extra, ckid, lpData, size);
460 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
462 TRACE("(%p)\n",iface);
464 /* This is only needed for interleaved files.
465 * We have only one stream, which can't be interleaved.
470 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
473 ICOM_THIS(IAVIFileImpl,iface);
475 TRACE("(%p,0x%08lX,%ld)\n", iface, fccType, lParam);
477 /* check parameter */
479 return AVIERR_BADPARAM;
481 /* Have we our audio stream? */
482 if (lParam != 0 || This->fInfo.dwStreams == 0 ||
483 (fccType != 0 && fccType != streamtypeAUDIO))
484 return AVIERR_NODATA;
486 /* Have user write permissions? */
487 if ((This->uMode & MMIO_RWMODE) == 0)
488 return AVIERR_READONLY;
490 GlobalFreePtr(This->lpFormat);
491 This->lpFormat = NULL;
495 This->ckData.dwDataOffset = 0;
496 This->ckData.cksize = 0;
498 This->sInfo.dwScale = 0;
499 This->sInfo.dwRate = 0;
500 This->sInfo.dwLength = 0;
501 This->sInfo.dwSuggestedBufferSize = 0;
503 This->fInfo.dwStreams = 0;
504 This->fInfo.dwEditCount++;
511 /***********************************************************************/
513 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
514 REFIID refiid, LPVOID *obj)
516 ICOM_THIS(IPersistFileImpl,iface);
518 assert(This->paf != NULL);
520 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
523 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
525 ICOM_THIS(IPersistFileImpl,iface);
527 assert(This->paf != NULL);
529 return IAVIFile_AddRef((PAVIFILE)This->paf);
532 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface)
534 ICOM_THIS(IPersistFileImpl,iface);
536 assert(This->paf != NULL);
538 return IAVIFile_Release((PAVIFILE)This->paf);
541 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
544 TRACE("(%p,%p)\n", iface, pClassID);
546 if (pClassID == NULL)
547 return AVIERR_BADPARAM;
549 memcpy(pClassID, &CLSID_WAVFile, sizeof(CLSID_WAVFile));
554 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
556 ICOM_THIS(IPersistFileImpl,iface);
558 TRACE("(%p)\n", iface);
560 assert(This->paf != NULL);
562 return (This->paf->fDirty ? S_OK : S_FALSE);
565 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
566 LPCOLESTR pszFileName, DWORD dwMode)
568 IAVIFileImpl *This = ((IPersistFileImpl*)iface)->paf;
570 WCHAR wszStreamFmt[50];
573 TRACE("(%p,%s,0x%08lX)\n", iface, debugstr_w(pszFileName), dwMode);
575 /* check parameter */
576 if (pszFileName == NULL)
577 return AVIERR_BADPARAM;
579 assert(This != NULL);
580 if (This->hmmio != NULL)
581 return AVIERR_ERROR; /* No reuse of this object for another file! */
583 /* remeber mode and name */
584 This->uMode = dwMode;
586 len = lstrlenW(pszFileName) + 1;
587 This->szFileName = LocalAlloc(LPTR, len * sizeof(WCHAR));
588 if (This->szFileName == NULL)
589 return AVIERR_MEMORY;
590 lstrcpyW(This->szFileName, pszFileName);
592 /* try to open the file */
593 This->hmmio = mmioOpenW(This->szFileName, NULL, MMIO_ALLOCBUF | dwMode);
594 if (This->hmmio == NULL) {
595 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
597 len = WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1,
598 NULL, 0, NULL, NULL);
599 szFileName = LocalAlloc(LPTR, len * sizeof(CHAR));
600 if (szFileName == NULL)
601 return AVIERR_MEMORY;
603 WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1, szFileName,
606 This->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
607 LocalFree((HLOCAL)szFileName);
608 if (This->hmmio == NULL)
609 return AVIERR_FILEOPEN;
612 memset(& This->fInfo, 0, sizeof(This->fInfo));
613 memset(& This->sInfo, 0, sizeof(This->sInfo));
615 LoadStringW(AVIFILE_hModule, IDS_WAVEFILETYPE, This->fInfo.szFileType,
616 sizeof(This->fInfo.szFileType));
617 if (LoadStringW(AVIFILE_hModule, IDS_WAVESTREAMFORMAT,
618 wszStreamFmt, sizeof(wszStreamFmt)) > 0) {
619 wsprintfW(This->sInfo.szName, wszStreamFmt,
620 AVIFILE_BasenameW(This->szFileName));
623 /* should we create a new file? */
624 if (dwMode & OF_CREATE) {
625 /* nothing more to do */
628 return AVIFILE_LoadFile(This);
631 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
632 LPCOLESTR pszFileName,BOOL fRemember)
634 TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
636 /* We write directly to disk, so nothing to do. */
641 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
642 LPCOLESTR pszFileName)
644 TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
646 /* We write directly to disk, so nothing to do. */
651 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
652 LPOLESTR *ppszFileName)
654 ICOM_THIS(IPersistFileImpl,iface);
656 TRACE("(%p,%p)\n", iface, ppszFileName);
658 if (ppszFileName == NULL)
659 return AVIERR_BADPARAM;
661 *ppszFileName = NULL;
663 assert(This->paf != NULL);
665 if (This->paf->szFileName != NULL) {
666 int len = lstrlenW(This->paf->szFileName) + 1;
668 *ppszFileName = GlobalAllocPtr(GHND, len * sizeof(WCHAR));
669 if (*ppszFileName == NULL)
670 return AVIERR_MEMORY;
672 strcpyW(*ppszFileName, This->paf->szFileName);
678 /***********************************************************************/
680 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
681 REFIID refiid, LPVOID *obj)
683 ICOM_THIS(IAVIStreamImpl,iface);
685 assert(This->paf != NULL);
687 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
690 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
692 ICOM_THIS(IAVIStreamImpl,iface);
694 assert(This->paf != NULL);
696 return IAVIFile_AddRef((PAVIFILE)This->paf);
699 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
701 ICOM_THIS(IAVIStreamImpl,iface);
703 assert(This->paf != NULL);
705 return IAVIFile_Release((PAVIFILE)This->paf);
708 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
711 TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
713 /* This IAVIStream interface needs an WAVFile */
714 return AVIERR_UNSUPPORTED;
717 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
720 ICOM_THIS(IAVIStreamImpl,iface);
722 TRACE("(%p,%p,%ld)\n", iface, psi, size);
725 return AVIERR_BADPARAM;
727 return AVIERR_BADSIZE;
729 memcpy(psi, &This->paf->sInfo, min((DWORD)size, sizeof(This->paf->sInfo)));
731 if ((DWORD)size < sizeof(This->paf->sInfo))
732 return AVIERR_BUFFERTOOSMALL;
736 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
739 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
741 TRACE("(%p,%ld,0x%08lX)\n",iface,pos,flags);
743 /* Do we have data? */
744 if (This->lpFormat == NULL)
747 /* We don't have an index */
748 if (flags & FIND_INDEX)
751 if (flags & FIND_FROM_START) {
752 pos = This->sInfo.dwStart;
753 flags &= ~(FIND_FROM_START|FIND_PREV);
757 if (flags & FIND_FORMAT) {
758 if ((flags & FIND_NEXT) && pos > 0)
764 if ((flags & FIND_RET) == FIND_LENGTH ||
765 (flags & FIND_RET) == FIND_SIZE)
766 return This->sInfo.dwSampleSize;
767 if ((flags & FIND_RET) == FIND_OFFSET)
768 return This->ckData.dwDataOffset + pos * This->sInfo.dwSampleSize;
773 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
774 LPVOID format, LONG *formatsize)
776 ICOM_THIS(IAVIStreamImpl,iface);
778 TRACE("(%p,%ld,%p,%p)\n", iface, pos, format, formatsize);
780 if (formatsize == NULL)
781 return AVIERR_BADPARAM;
783 /* only interested in needed buffersize? */
784 if (format == NULL || *formatsize <= 0) {
785 *formatsize = This->paf->cbFormat;
790 /* copy initial format (only as much as will fit) */
791 memcpy(format, This->paf->lpFormat, min(*formatsize, This->paf->cbFormat));
792 if (*formatsize < This->paf->cbFormat) {
793 *formatsize = This->paf->cbFormat;
794 return AVIERR_BUFFERTOOSMALL;
797 *formatsize = This->paf->cbFormat;
801 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
802 LPVOID format, LONG formatsize)
804 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
806 TRACE("(%p,%ld,%p,%ld)\n", iface, pos, format, formatsize);
808 /* check parameters */
809 if (format == NULL || formatsize <= sizeof(PCMWAVEFORMAT))
810 return AVIERR_BADPARAM;
812 /* We can only do this to an empty wave file, but ignore call
813 * if still same format */
814 if (This->lpFormat != NULL) {
815 if (formatsize != This->cbFormat ||
816 memcmp(format, This->lpFormat, formatsize) != 0)
817 return AVIERR_UNSUPPORTED;
822 /* only support start at position 0 */
824 return AVIERR_UNSUPPORTED;
826 /* Do we have write permission? */
827 if ((This->uMode & MMIO_RWMODE) == 0)
828 return AVIERR_READONLY;
830 /* get memory for format and copy it */
831 This->lpFormat = (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, formatsize);
832 if (This->lpFormat == NULL)
833 return AVIERR_MEMORY;
835 This->cbFormat = formatsize;
836 memcpy(This->lpFormat, format, formatsize);
838 /* update info's about 'data' chunk */
839 This->ckData.dwDataOffset = formatsize + 7 * sizeof(DWORD);
840 This->ckData.cksize = 0;
842 /* for non-pcm format we need also a 'fact' chunk */
843 if (This->lpFormat->wFormatTag != WAVE_FORMAT_PCM)
844 This->ckData.dwDataOffset += 3 * sizeof(DWORD);
846 /* update stream and file info */
847 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign;
848 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
849 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
850 This->sInfo.dwLength = 0;
851 This->sInfo.dwSuggestedBufferSize = 0;
856 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
857 LONG samples, LPVOID buffer,
858 LONG buffersize, LPLONG bytesread,
861 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
863 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface, start, samples, buffer,
864 buffersize, bytesread, samplesread);
866 /* clear return parameters if given */
867 if (bytesread != NULL)
869 if (samplesread != NULL)
872 /* positions without data */
873 if (start < 0 || (DWORD)start > This->sInfo.dwLength)
879 if (buffersize > 0) {
881 samples = min((DWORD)samples, buffersize / This->sInfo.dwSampleSize);
883 samples = buffersize / This->sInfo.dwSampleSize;
886 /* limit to end of stream */
887 if ((DWORD)(start + samples) > This->sInfo.dwLength)
888 samples = This->sInfo.dwLength - start;
890 /* request only the sizes? */
891 if (buffer == NULL || buffersize <= 0) {
892 /* then I need atleast one parameter for it */
893 if (bytesread == NULL && samplesread == NULL)
894 return AVIERR_BADPARAM;
896 if (bytesread != NULL)
897 *bytesread = samples * This->sInfo.dwSampleSize;
898 if (samplesread != NULL)
899 *samplesread = samples;
904 /* nothing to read? */
908 /* Can I atleast read one sample? */
909 if ((DWORD)buffersize < This->sInfo.dwSampleSize)
910 return AVIERR_BUFFERTOOSMALL;
912 buffersize = samples * This->sInfo.dwSampleSize;
914 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset
915 + start * This->sInfo.dwSampleSize, SEEK_SET) == -1)
916 return AVIERR_FILEREAD;
917 if (mmioRead(This->hmmio, (HPSTR)buffer, buffersize) != buffersize)
918 return AVIERR_FILEREAD;
920 /* fill out return parameters if given */
921 if (bytesread != NULL)
922 *bytesread = buffersize;
923 if (samplesread != NULL)
924 *samplesread = samples;
929 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
930 LONG samples, LPVOID buffer,
931 LONG buffersize, DWORD flags,
935 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
937 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface, start, samples,
938 buffer, buffersize, flags, sampwritten, byteswritten);
940 /* clear return parameters if given */
941 if (sampwritten != NULL)
943 if (byteswritten != NULL)
946 /* check parameters */
947 if (buffer == NULL && (buffersize > 0 || samples > 0))
948 return AVIERR_BADPARAM;
950 /* Have we write permission? */
951 if ((This->uMode & MMIO_RWMODE) == 0)
952 return AVIERR_READONLY;
954 /* < 0 means "append" */
956 start = This->sInfo.dwStart + This->sInfo.dwLength;
958 /* check buffersize -- must multiple of samplesize */
959 if (buffersize & ~(This->sInfo.dwSampleSize - 1))
960 return AVIERR_BADSIZE;
962 /* have we anything to write? */
963 if (buffer != NULL && buffersize > 0) {
966 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset +
967 start * This->sInfo.dwSampleSize, SEEK_SET) == -1)
968 return AVIERR_FILEWRITE;
969 if (mmioWrite(This->hmmio, (HPSTR)buffer, buffersize) != buffersize)
970 return AVIERR_FILEWRITE;
972 This->sInfo.dwLength = max(This->sInfo.dwLength, (DWORD)start + samples);
973 This->ckData.cksize = max(This->ckData.cksize,
974 start * This->sInfo.dwSampleSize + buffersize);
976 /* fill out return parameters if given */
977 if (sampwritten != NULL)
978 *sampwritten = samples;
979 if (byteswritten != NULL)
980 *byteswritten = buffersize;
986 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
989 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
991 TRACE("(%p,%ld,%ld)\n", iface, start, samples);
993 /* check parameters */
994 if (start < 0 || samples < 0)
995 return AVIERR_BADPARAM;
997 /* Delete before start of stream? */
998 if ((DWORD)(start + samples) < This->sInfo.dwStart)
1001 /* Delete after end of stream? */
1002 if ((DWORD)start > This->sInfo.dwLength)
1005 /* For the rest we need write permissions */
1006 if ((This->uMode & MMIO_RWMODE) == 0)
1007 return AVIERR_READONLY;
1009 if ((DWORD)(start + samples) >= This->sInfo.dwLength) {
1010 /* deletion at end */
1011 samples = This->sInfo.dwLength - start;
1012 This->sInfo.dwLength -= samples;
1013 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
1014 } else if ((DWORD)start <= This->sInfo.dwStart) {
1015 /* deletion at start */
1016 samples = This->sInfo.dwStart - start;
1017 start = This->sInfo.dwStart;
1018 This->ckData.dwDataOffset += samples * This->sInfo.dwSampleSize;
1019 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
1021 /* deletion inside stream -- needs playlist and cue's */
1022 FIXME(": deletion inside of stream not supported!\n");
1024 return AVIERR_UNSUPPORTED;
1032 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1033 LPVOID lp, LPLONG lpread)
1035 ICOM_THIS(IAVIStreamImpl,iface);
1037 assert(This->paf != NULL);
1039 return IAVIFile_ReadData((PAVIFILE)This->paf, fcc, lp, lpread);
1042 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1043 LPVOID lp, LONG size)
1045 ICOM_THIS(IAVIStreamImpl,iface);
1047 return IAVIFile_WriteData((PAVIFILE)This->paf, fcc, lp, size);
1050 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1051 LPAVISTREAMINFOW info, LONG infolen)
1053 FIXME("(%p,%p,%ld): stub\n", iface, info, infolen);
1058 /***********************************************************************/
1060 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1065 This->sInfo.dwLength = 0; /* just to be sure */
1066 This->fDirty = FALSE;
1068 /* search for RIFF chunk */
1069 ckRIFF.fccType = 0; /* find any */
1070 if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) {
1071 return AVIFILE_LoadSunFile(This);
1074 if (ckRIFF.fccType != formtypeWAVE)
1075 return AVIERR_BADFORMAT;
1077 /* search WAVE format chunk */
1078 ck.ckid = ckidWAVEFORMAT;
1079 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck,
1080 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
1081 return AVIERR_FILEREAD;
1083 /* get memory for format and read it */
1084 This->lpFormat = (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, ck.cksize);
1085 if (This->lpFormat == NULL)
1086 return AVIERR_FILEREAD;
1087 This->cbFormat = ck.cksize;
1089 if (mmioRead(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize)
1090 return AVIERR_FILEREAD;
1091 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1092 return AVIERR_FILEREAD;
1094 /* Non-pcm formats have a fact chunk.
1095 * We don't need it, so simply add it to the extra chunks.
1098 /* find the big data chunk */
1099 This->ckData.ckid = ckidWAVEDATA;
1100 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &This->ckData,
1101 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
1102 return AVIERR_FILEREAD;
1104 memset(&This->sInfo, 0, sizeof(This->sInfo));
1105 This->sInfo.fccType = streamtypeAUDIO;
1106 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
1107 This->sInfo.dwSampleSize =
1108 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
1109 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign;
1110 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize;
1112 This->fInfo.dwStreams = 1;
1114 if (mmioAscend(This->hmmio, &This->ckData, 0) != S_OK) {
1115 /* seems to be truncated */
1116 WARN(": file seems to be truncated!\n");
1117 This->ckData.cksize = mmioSeek(This->hmmio, 0, SEEK_END) -
1118 This->ckData.dwDataOffset;
1119 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign;
1120 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize;
1124 FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck, &ckRIFF, 0);
1129 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This)
1131 SUNAUDIOHEADER auhdr;
1133 mmioSeek(This->hmmio, 0, SEEK_SET);
1134 if (mmioRead(This->hmmio, (HPSTR)&auhdr, sizeof(auhdr)) != sizeof(auhdr))
1135 return AVIERR_FILEREAD;
1137 if (auhdr.fccType == 0x0064732E) {
1138 /* header in little endian */
1139 This->ckData.dwDataOffset = LE2H_DWORD(auhdr.offset);
1140 This->ckData.cksize = LE2H_DWORD(auhdr.size);
1142 auhdr.encoding = LE2H_DWORD(auhdr.encoding);
1143 auhdr.sampleRate = LE2H_DWORD(auhdr.sampleRate);
1144 auhdr.channels = LE2H_DWORD(auhdr.channels);
1145 } else if (auhdr.fccType == mmioFOURCC('.','s','n','d')) {
1146 /* header in big endian */
1147 This->ckData.dwDataOffset = BE2H_DWORD(auhdr.offset);
1148 This->ckData.cksize = BE2H_DWORD(auhdr.size);
1150 auhdr.encoding = BE2H_DWORD(auhdr.encoding);
1151 auhdr.sampleRate = BE2H_DWORD(auhdr.sampleRate);
1152 auhdr.channels = BE2H_DWORD(auhdr.channels);
1154 return AVIERR_FILEREAD;
1156 if (auhdr.channels < 1)
1157 return AVIERR_BADFORMAT;
1159 /* get size of header */
1160 switch(auhdr.encoding) {
1161 case AU_ENCODING_ADPCM_G721_32:
1162 This->cbFormat = sizeof(G721_ADPCMWAVEFORMAT); break;
1163 case AU_ENCODING_ADPCM_G723_24:
1164 This->cbFormat = sizeof(G723_ADPCMWAVEFORMAT); break;
1165 case AU_ENCODING_ADPCM_G722:
1166 case AU_ENCODING_ADPCM_G723_5:
1167 WARN("unsupported Sun audio format %d\n", auhdr.encoding);
1168 return AVIERR_UNSUPPORTED; /* FIXME */
1170 This->cbFormat = sizeof(WAVEFORMATEX); break;
1174 (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, This->cbFormat);
1175 if (This->lpFormat == NULL)
1176 return AVIERR_MEMORY;
1178 This->lpFormat->nChannels = auhdr.channels;
1179 This->lpFormat->nSamplesPerSec = auhdr.sampleRate;
1180 switch(auhdr.encoding) {
1181 case AU_ENCODING_ULAW_8:
1182 This->lpFormat->wFormatTag = WAVE_FORMAT_MULAW;
1183 This->lpFormat->wBitsPerSample = 8;
1185 case AU_ENCODING_PCM_8:
1186 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1187 This->lpFormat->wBitsPerSample = 8;
1189 case AU_ENCODING_PCM_16:
1190 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1191 This->lpFormat->wBitsPerSample = 16;
1193 case AU_ENCODING_PCM_24:
1194 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1195 This->lpFormat->wBitsPerSample = 24;
1197 case AU_ENCODING_PCM_32:
1198 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1199 This->lpFormat->wBitsPerSample = 32;
1201 case AU_ENCODING_ALAW_8:
1202 This->lpFormat->wFormatTag = WAVE_FORMAT_ALAW;
1203 This->lpFormat->wBitsPerSample = 8;
1205 case AU_ENCODING_ADPCM_G721_32:
1206 This->lpFormat->wFormatTag = WAVE_FORMAT_G721_ADPCM;
1207 This->lpFormat->wBitsPerSample = (3*5*8);
1208 This->lpFormat->nBlockAlign = 15*15*8;
1209 This->lpFormat->cbSize = sizeof(WORD);
1210 ((LPG721_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0;
1212 case AU_ENCODING_ADPCM_G723_24:
1213 This->lpFormat->wFormatTag = WAVE_FORMAT_G723_ADPCM;
1214 This->lpFormat->wBitsPerSample = (3*5*8);
1215 This->lpFormat->nBlockAlign = 15*15*8;
1216 This->lpFormat->cbSize = 2*sizeof(WORD);
1217 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->cbExtraSize = 0;
1218 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0;
1221 WARN("unsupported Sun audio format %d\n", auhdr.encoding);
1222 return AVIERR_UNSUPPORTED;
1225 This->lpFormat->nBlockAlign =
1226 (This->lpFormat->nChannels * This->lpFormat->wBitsPerSample) / 8;
1227 if (This->lpFormat->nBlockAlign == 0 && This->lpFormat->wBitsPerSample < 8)
1228 This->lpFormat->nBlockAlign++;
1229 This->lpFormat->nAvgBytesPerSec =
1230 This->lpFormat->nBlockAlign * This->lpFormat->nSamplesPerSec;
1234 This->sInfo.fccType = streamtypeAUDIO;
1235 This->sInfo.fccHandler = 0;
1236 This->sInfo.dwFlags = 0;
1237 This->sInfo.wPriority = 0;
1238 This->sInfo.wLanguage = 0;
1239 This->sInfo.dwInitialFrames = 0;
1240 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
1241 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
1242 This->sInfo.dwStart = 0;
1243 This->sInfo.dwLength =
1244 This->ckData.cksize / This->lpFormat->nBlockAlign;
1245 This->sInfo.dwSuggestedBufferSize = This->sInfo.dwLength;
1246 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign;
1248 This->fInfo.dwStreams = 1;
1249 This->fInfo.dwScale = 1;
1250 This->fInfo.dwRate = This->lpFormat->nSamplesPerSec;
1251 This->fInfo.dwLength =
1252 MulDiv(This->ckData.cksize, This->lpFormat->nSamplesPerSec,
1253 This->lpFormat->nAvgBytesPerSec);
1258 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
1263 mmioSeek(This->hmmio, 0, SEEK_SET);
1265 /* create the RIFF chunk with formtype WAVE */
1266 ckRIFF.fccType = formtypeWAVE;
1268 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
1269 return AVIERR_FILEWRITE;
1271 /* the next chunk is the format */
1272 ck.ckid = ckidWAVEFORMAT;
1273 ck.cksize = This->cbFormat;
1274 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1275 return AVIERR_FILEWRITE;
1276 if (This->lpFormat != NULL && This->cbFormat > 0) {
1277 if (mmioWrite(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize)
1278 return AVIERR_FILEWRITE;
1280 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1281 return AVIERR_FILEWRITE;
1283 /* fact chunk is needed for non-pcm waveforms */
1284 if (This->lpFormat != NULL && This->cbFormat > sizeof(PCMWAVEFORMAT) &&
1285 This->lpFormat->wFormatTag != WAVE_FORMAT_PCM) {
1290 /* try to open an appropriate audio codec to figure out
1291 * data for fact-chunk */
1292 wfx.wFormatTag = WAVE_FORMAT_PCM;
1293 if (acmFormatSuggest(NULL, This->lpFormat, &wfx,
1294 sizeof(wfx), ACM_FORMATSUGGESTF_WFORMATTAG)) {
1295 acmStreamOpen(&has, NULL, This->lpFormat, &wfx, NULL,
1296 0, 0, ACM_STREAMOPENF_NONREALTIME);
1297 acmStreamSize(has, This->ckData.cksize, &dwFactLength,
1298 ACM_STREAMSIZEF_SOURCE);
1299 dwFactLength /= wfx.nBlockAlign;
1300 acmStreamClose(has, 0);
1302 /* crete the fact chunk */
1303 ck.ckid = ckidWAVEFACT;
1304 ck.cksize = sizeof(dwFactLength);
1306 /* test for enough space before data chunk */
1307 if (mmioSeek(This->hmmio, 0, SEEK_CUR) > This->ckData.dwDataOffset
1308 - ck.cksize - 4 * sizeof(DWORD))
1309 return AVIERR_FILEWRITE;
1310 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1311 return AVIERR_FILEWRITE;
1312 if (mmioWrite(This->hmmio, (HPSTR)&dwFactLength, ck.cksize) != ck.cksize)
1313 return AVIERR_FILEWRITE;
1314 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1315 return AVIERR_FILEWRITE;
1317 ERR(": fact chunk is needed for non-pcm files -- currently no codec found, so skipped!\n");
1320 /* if here was extra stuff, we need to fill it with JUNK */
1321 if (mmioSeek(This->hmmio, 0, SEEK_CUR) + 2 * sizeof(DWORD) < This->ckData.dwDataOffset) {
1322 ck.ckid = ckidAVIPADDING;
1324 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1325 return AVIERR_FILEWRITE;
1327 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset
1328 - 2 * sizeof(DWORD), SEEK_SET) == -1)
1329 return AVIERR_FILEWRITE;
1330 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1331 return AVIERR_FILEWRITE;
1334 /* crete the data chunk */
1335 ck.ckid = ckidWAVEDATA;
1336 ck.cksize = This->ckData.cksize;
1337 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1338 return AVIERR_FILEWRITE;
1339 if (mmioSeek(This->hmmio, This->ckData.cksize, SEEK_CUR) == -1)
1340 return AVIERR_FILEWRITE;
1341 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1342 return AVIERR_FILEWRITE;
1344 /* some optional extra chunks? */
1345 if (This->extra.lp != NULL && This->extra.cb > 0) {
1346 /* chunk headers are already in structure */
1347 if (mmioWrite(This->hmmio, This->extra.lp, This->extra.cb) != This->extra.cb)
1348 return AVIERR_FILEWRITE;
1351 /* close RIFF chunk */
1352 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
1353 return AVIERR_FILEWRITE;
1354 if (mmioFlush(This->hmmio, 0) != S_OK)
1355 return AVIERR_FILEWRITE;