Add an empty hook function for WSASetBlockingHook to return.
[wine/testsucceed.git] / dlls / avifil32 / wavfile.c
blob3ba7de1dfcc95007438be5de96c9806a6aa43c01
1 /*
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
20 #include <assert.h>
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "winnls.h"
25 #include "winerror.h"
26 #include "windowsx.h"
27 #include "mmsystem.h"
28 #include "vfw.h"
29 #include "msacm.h"
31 #include "avifile_private.h"
32 #include "extrachunk.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
38 /***********************************************************************/
40 #define formtypeWAVE mmioFOURCC('W','A','V','E')
41 #define ckidWAVEFORMAT mmioFOURCC('f','m','t',' ')
42 #define ckidWAVEFACT mmioFOURCC('f','a','c','t')
43 #define ckidWAVEDATA mmioFOURCC('d','a','t','a')
45 /***********************************************************************/
47 #define ENDIAN_SWAPWORD(x) ((((x) >> 8) & 0xFF) | (((x) & 0xFF) << 8))
48 #define ENDIAN_SWAPDWORD(x) (ENDIAN_SWAPWORD((x >> 16) & 0xFFFF) | \
49 ENDIAN_SWAPWORD(x & 0xFFFF) << 16)
51 #ifdef WORDS_BIGENDIAN
52 #define BE2H_WORD(x) (x)
53 #define BE2H_DWORD(x) (x)
54 #define LE2H_WORD(x) ENDIAN_SWAPWORD(x)
55 #define LE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
56 #else
57 #define BE2H_WORD(x) ENDIAN_SWAPWORD(x)
58 #define BE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
59 #define LE2H_WORD(x) (x)
60 #define LE2H_DWORD(x) (x)
61 #endif
63 typedef struct {
64 FOURCC fccType;
65 DWORD offset;
66 DWORD size;
67 INT encoding;
68 DWORD sampleRate;
69 DWORD channels;
70 } SUNAUDIOHEADER;
72 #define AU_ENCODING_ULAW_8 1
73 #define AU_ENCODING_PCM_8 2
74 #define AU_ENCODING_PCM_16 3
75 #define AU_ENCODING_PCM_24 4
76 #define AU_ENCODING_PCM_32 5
77 #define AU_ENCODING_FLOAT 6
78 #define AU_ENCODING_DOUBLE 7
79 #define AU_ENCODING_ADPCM_G721_32 23
80 #define AU_ENCODING_ADPCM_G722 24
81 #define AU_ENCODING_ADPCM_G723_24 25
82 #define AU_ENCODING_ADPCM_G723_5 26
83 #define AU_ENCODING_ALAW_8 27
85 /***********************************************************************/
87 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
88 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
89 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile* iface);
90 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
91 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
92 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
93 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
94 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
95 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
96 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
98 struct ICOM_VTABLE(IAVIFile) iwavft = {
99 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
100 IAVIFile_fnQueryInterface,
101 IAVIFile_fnAddRef,
102 IAVIFile_fnRelease,
103 IAVIFile_fnInfo,
104 IAVIFile_fnGetStream,
105 IAVIFile_fnCreateStream,
106 IAVIFile_fnWriteData,
107 IAVIFile_fnReadData,
108 IAVIFile_fnEndRecord,
109 IAVIFile_fnDeleteStream
112 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile*iface,REFIID refiid,LPVOID*obj);
113 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile*iface);
114 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile*iface);
115 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile*iface,CLSID*pClassID);
116 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile*iface);
117 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile*iface,LPCOLESTR pszFileName,DWORD dwMode);
118 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile*iface,LPCOLESTR pszFileName,BOOL fRemember);
119 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile*iface,LPCOLESTR pszFileName);
120 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile*iface,LPOLESTR*ppszFileName);
122 struct ICOM_VTABLE(IPersistFile) iwavpft = {
123 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
124 IPersistFile_fnQueryInterface,
125 IPersistFile_fnAddRef,
126 IPersistFile_fnRelease,
127 IPersistFile_fnGetClassID,
128 IPersistFile_fnIsDirty,
129 IPersistFile_fnLoad,
130 IPersistFile_fnSave,
131 IPersistFile_fnSaveCompleted,
132 IPersistFile_fnGetCurFile
135 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID *obj);
136 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream*iface);
137 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface);
138 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
139 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
140 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream*iface,LONG pos,LONG flags);
141 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG *formatsize);
142 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
143 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread);
144 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten);
145 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
146 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG *lpread);
147 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG size);
148 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
150 struct ICOM_VTABLE(IAVIStream) iwavst = {
151 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
152 IAVIStream_fnQueryInterface,
153 IAVIStream_fnAddRef,
154 IAVIStream_fnRelease,
155 IAVIStream_fnCreate,
156 IAVIStream_fnInfo,
157 IAVIStream_fnFindSample,
158 IAVIStream_fnReadFormat,
159 IAVIStream_fnSetFormat,
160 IAVIStream_fnRead,
161 IAVIStream_fnWrite,
162 IAVIStream_fnDelete,
163 IAVIStream_fnReadData,
164 IAVIStream_fnWriteData,
165 IAVIStream_fnSetInfo
168 typedef struct _IAVIFileImpl IAVIFileImpl;
170 typedef struct _IPersistFileImpl {
171 /* IUnknown stuff */
172 ICOM_VFIELD(IPersistFile);
174 /* IPersistFile stuff */
175 IAVIFileImpl *paf;
176 } IPersistFileImpl;
178 typedef struct _IAVIStreamImpl {
179 /* IUnknown stuff */
180 ICOM_VFIELD(IAVIStream);
182 /* IAVIStream stuff */
183 IAVIFileImpl *paf;
184 } IAVIStreamImpl;
186 struct _IAVIFileImpl {
187 /* IUnknown stuff */
188 ICOM_VFIELD(IAVIFile);
189 DWORD ref;
191 /* IAVIFile, IAVIStream stuff... */
192 IPersistFileImpl iPersistFile;
193 IAVIStreamImpl iAVIStream;
195 AVIFILEINFOW fInfo;
196 AVISTREAMINFOW sInfo;
198 LPWAVEFORMATEX lpFormat;
199 LONG cbFormat;
201 MMCKINFO ckData;
203 EXTRACHUNKS extra;
205 /* IPersistFile stuff ... */
206 HMMIO hmmio;
207 LPWSTR szFileName;
208 UINT uMode;
209 BOOL fDirty;
212 /***********************************************************************/
214 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
215 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This);
216 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
218 HRESULT AVIFILE_CreateWAVFile(REFIID riid, LPVOID *ppv)
220 IAVIFileImpl *pfile;
221 HRESULT hr;
223 assert(riid != NULL && ppv != NULL);
225 *ppv = NULL;
227 pfile = (IAVIFileImpl*)LocalAlloc(LPTR, sizeof(IAVIFileImpl));
228 if (pfile == NULL)
229 return AVIERR_MEMORY;
231 pfile->lpVtbl = &iwavft;
232 pfile->iPersistFile.lpVtbl = &iwavpft;
233 pfile->iAVIStream.lpVtbl = &iwavst;
234 pfile->ref = 0;
235 pfile->iPersistFile.paf = pfile;
236 pfile->iAVIStream.paf = pfile;
238 hr = IUnknown_QueryInterface((IUnknown*)pfile, riid, ppv);
239 if (FAILED(hr))
240 LocalFree((HLOCAL)pfile);
242 return hr;
245 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
246 LPVOID *obj)
248 ICOM_THIS(IAVIFileImpl,iface);
250 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
252 if (IsEqualGUID(&IID_IUnknown, refiid) ||
253 IsEqualGUID(&IID_IAVIFile, refiid)) {
254 *obj = iface;
255 return S_OK;
256 } else if (This->fInfo.dwStreams == 1 &&
257 IsEqualGUID(&IID_IAVIStream, refiid)) {
258 *obj = &This->iAVIStream;
259 return S_OK;
260 } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
261 *obj = &This->iPersistFile;
262 return S_OK;
265 return OLE_E_ENUM_NOMORE;
268 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
270 ICOM_THIS(IAVIFileImpl,iface);
272 TRACE("(%p)\n",iface);
274 return ++(This->ref);
277 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
279 ICOM_THIS(IAVIFileImpl,iface);
281 TRACE("(%p)\n",iface);
283 if (!--(This->ref)) {
284 if (This->fDirty) {
285 /* need to write headers to file */
286 AVIFILE_SaveFile(This);
289 if (This->lpFormat != NULL) {
290 GlobalFreePtr(This->lpFormat);
291 This->lpFormat = NULL;
292 This->cbFormat = 0;
294 if (This->extra.lp != NULL) {
295 GlobalFreePtr(This->extra.lp);
296 This->extra.lp = NULL;
297 This->extra.cb = 0;
299 if (This->szFileName != NULL) {
300 LocalFree((HLOCAL)This->szFileName);
301 This->szFileName = NULL;
303 if (This->hmmio != NULL) {
304 mmioClose(This->hmmio, 0);
305 This->hmmio = NULL;
308 LocalFree((HLOCAL)This);
309 return 0;
311 return This->ref;
314 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
315 LONG size)
317 ICOM_THIS(IAVIFileImpl,iface);
319 TRACE("(%p,%p,%ld)\n",iface,afi,size);
321 if (afi == NULL)
322 return AVIERR_BADPARAM;
323 if (size < 0)
324 return AVIERR_BADSIZE;
326 /* update file info */
327 This->fInfo.dwFlags = 0;
328 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
329 if (This->lpFormat != NULL) {
330 assert(This->sInfo.dwScale != 0);
332 This->fInfo.dwStreams = 1;
333 This->fInfo.dwScale = This->sInfo.dwScale;
334 This->fInfo.dwRate = This->sInfo.dwRate;
335 This->fInfo.dwLength = This->sInfo.dwLength;
336 This->fInfo.dwSuggestedBufferSize = This->ckData.cksize;
337 This->fInfo.dwMaxBytesPerSec =
338 MulDiv(This->sInfo.dwSampleSize,This->sInfo.dwRate,This->sInfo.dwScale);
341 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
343 if ((DWORD)size < sizeof(This->fInfo))
344 return AVIERR_BUFFERTOOSMALL;
345 return AVIERR_OK;
348 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
349 DWORD fccType, LONG lParam)
351 ICOM_THIS(IAVIFileImpl,iface);
353 TRACE("(%p,%p,0x%08lX,%ld)\n", iface, avis, fccType, lParam);
355 /* check parameter */
356 if (avis == NULL)
357 return AVIERR_BADPARAM;
359 *avis = NULL;
361 /* Does our stream exists? */
362 if (lParam != 0 || This->fInfo.dwStreams == 0)
363 return AVIERR_NODATA;
364 if (fccType != 0 && fccType != streamtypeAUDIO)
365 return AVIERR_NODATA;
367 *avis = (PAVISTREAM)&This->iAVIStream;
368 IAVIFile_AddRef(iface);
370 return AVIERR_OK;
373 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
374 LPAVISTREAMINFOW asi)
376 ICOM_THIS(IAVIFileImpl,iface);
378 TRACE("(%p,%p,%p)\n", iface, avis, asi);
380 /* check parameters */
381 if (avis == NULL || asi == NULL)
382 return AVIERR_BADPARAM;
384 *avis = NULL;
386 /* We only support one audio stream */
387 if (This->fInfo.dwStreams != 0 || This->lpFormat != NULL)
388 return AVIERR_UNSUPPORTED;
389 if (asi->fccType != streamtypeAUDIO)
390 return AVIERR_UNSUPPORTED;
392 /* Does the user have write permission? */
393 if ((This->uMode & MMIO_RWMODE) == 0)
394 return AVIERR_READONLY;
396 This->cbFormat = 0;
397 This->lpFormat = NULL;
399 memcpy(&This->sInfo, asi, sizeof(This->sInfo));
401 /* make sure streaminfo if okay for us */
402 This->sInfo.fccHandler = 0;
403 This->sInfo.dwFlags = 0;
404 This->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
405 This->sInfo.dwStart = 0;
406 This->sInfo.dwInitialFrames = 0;
407 This->sInfo.dwFormatChangeCount = 0;
408 memset(&This->sInfo.rcFrame, 0, sizeof(This->sInfo.rcFrame));
410 This->fInfo.dwStreams = 1;
411 This->fInfo.dwScale = This->sInfo.dwScale;
412 This->fInfo.dwRate = This->sInfo.dwRate;
413 This->fInfo.dwLength = This->sInfo.dwLength;
415 This->ckData.dwDataOffset = 0;
416 This->ckData.cksize = 0;
418 *avis = (PAVISTREAM)&This->iAVIStream;
419 IAVIFile_AddRef(iface);
421 return AVIERR_OK;
424 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
425 LPVOID lpData, LONG size)
427 ICOM_THIS(IAVIFileImpl,iface);
429 TRACE("(%p,0x%08lX,%p,%ld)\n", iface, ckid, lpData, size);
431 /* check parameters */
432 if (lpData == NULL)
433 return AVIERR_BADPARAM;
434 if (size < 0)
435 return AVIERR_BADSIZE;
437 /* Do we have write permission? */
438 if ((This->uMode & MMIO_RWMODE) == 0)
439 return AVIERR_READONLY;
441 This->fDirty = TRUE;
443 return WriteExtraChunk(&This->extra, ckid, lpData, size);
446 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
447 LPVOID lpData, LONG *size)
449 ICOM_THIS(IAVIFileImpl,iface);
451 TRACE("(%p,0x%08lX,%p,%p)\n", iface, ckid, lpData, size);
453 return ReadExtraChunk(&This->extra, ckid, lpData, size);
456 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
458 TRACE("(%p)\n",iface);
460 /* This is only needed for interleaved files.
461 * We have only one stream, which can't be interleaved.
463 return AVIERR_OK;
466 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
467 LONG lParam)
469 ICOM_THIS(IAVIFileImpl,iface);
471 TRACE("(%p,0x%08lX,%ld)\n", iface, fccType, lParam);
473 /* check parameter */
474 if (lParam < 0)
475 return AVIERR_BADPARAM;
477 /* Have we our audio stream? */
478 if (lParam != 0 || This->fInfo.dwStreams == 0 ||
479 (fccType != 0 && fccType != streamtypeAUDIO))
480 return AVIERR_NODATA;
482 /* Have user write permissions? */
483 if ((This->uMode & MMIO_RWMODE) == 0)
484 return AVIERR_READONLY;
486 GlobalFreePtr(This->lpFormat);
487 This->lpFormat = NULL;
488 This->cbFormat = 0;
490 /* update infos */
491 This->ckData.dwDataOffset = 0;
492 This->ckData.cksize = 0;
494 This->sInfo.dwScale = 0;
495 This->sInfo.dwRate = 0;
496 This->sInfo.dwLength = 0;
497 This->sInfo.dwSuggestedBufferSize = 0;
499 This->fInfo.dwStreams = 0;
500 This->fInfo.dwEditCount++;
502 This->fDirty = TRUE;
504 return AVIERR_OK;
507 /***********************************************************************/
509 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
510 REFIID refiid, LPVOID *obj)
512 ICOM_THIS(IPersistFileImpl,iface);
514 assert(This->paf != NULL);
516 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
519 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
521 ICOM_THIS(IPersistFileImpl,iface);
523 assert(This->paf != NULL);
525 return IAVIFile_AddRef((PAVIFILE)This->paf);
528 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface)
530 ICOM_THIS(IPersistFileImpl,iface);
532 assert(This->paf != NULL);
534 return IAVIFile_Release((PAVIFILE)This->paf);
537 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
538 LPCLSID pClassID)
540 TRACE("(%p,%p)\n", iface, pClassID);
542 if (pClassID == NULL)
543 return AVIERR_BADPARAM;
545 memcpy(pClassID, &CLSID_WAVFile, sizeof(CLSID_WAVFile));
547 return AVIERR_OK;
550 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
552 ICOM_THIS(IPersistFileImpl,iface);
554 TRACE("(%p)\n", iface);
556 assert(This->paf != NULL);
558 return (This->paf->fDirty ? S_OK : S_FALSE);
561 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
562 LPCOLESTR pszFileName, DWORD dwMode)
564 IAVIFileImpl *This = ((IPersistFileImpl*)iface)->paf;
566 WCHAR wszStreamFmt[50];
567 INT len;
569 TRACE("(%p,%s,0x%08lX)\n", iface, debugstr_w(pszFileName), dwMode);
571 /* check parameter */
572 if (pszFileName == NULL)
573 return AVIERR_BADPARAM;
575 assert(This != NULL);
576 if (This->hmmio != NULL)
577 return AVIERR_ERROR; /* No reuse of this object for another file! */
579 /* remeber mode and name */
580 This->uMode = dwMode;
582 len = lstrlenW(pszFileName) + 1;
583 This->szFileName = (LPWSTR)LocalAlloc(LPTR, len * sizeof(WCHAR));
584 if (This->szFileName == NULL)
585 return AVIERR_MEMORY;
586 lstrcpyW(This->szFileName, pszFileName);
588 /* try to open the file */
589 This->hmmio = mmioOpenW(This->szFileName, NULL, MMIO_ALLOCBUF | dwMode);
590 if (This->hmmio == NULL) {
591 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
592 LPSTR szFileName = LocalAlloc(LPTR, len * sizeof(CHAR));
593 if (szFileName == NULL)
594 return AVIERR_MEMORY;
596 WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1, szFileName,
597 len, NULL, NULL);
599 This->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
600 LocalFree((HLOCAL)szFileName);
601 if (This->hmmio == NULL)
602 return AVIERR_FILEOPEN;
605 memset(& This->fInfo, 0, sizeof(This->fInfo));
606 memset(& This->sInfo, 0, sizeof(This->sInfo));
608 LoadStringW(AVIFILE_hModule, IDS_WAVEFILETYPE, This->fInfo.szFileType,
609 sizeof(This->fInfo.szFileType));
610 if (LoadStringW(AVIFILE_hModule, IDS_WAVESTREAMFORMAT,
611 wszStreamFmt, sizeof(wszStreamFmt)) > 0) {
612 wsprintfW(This->sInfo.szName, wszStreamFmt,
613 AVIFILE_BasenameW(This->szFileName));
616 /* should we create a new file? */
617 if (dwMode & OF_CREATE) {
618 /* nothing more to do */
619 return AVIERR_OK;
620 } else
621 return AVIFILE_LoadFile(This);
624 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
625 LPCOLESTR pszFileName,BOOL fRemember)
627 TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
629 /* We write directly to disk, so nothing to do. */
631 return AVIERR_OK;
634 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
635 LPCOLESTR pszFileName)
637 TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
639 /* We write directly to disk, so nothing to do. */
641 return AVIERR_OK;
644 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
645 LPOLESTR *ppszFileName)
647 ICOM_THIS(IPersistFileImpl,iface);
649 TRACE("(%p,%p)\n", iface, ppszFileName);
651 if (ppszFileName == NULL)
652 return AVIERR_BADPARAM;
654 *ppszFileName = NULL;
656 assert(This->paf != NULL);
658 if (This->paf->szFileName != NULL) {
659 int len = lstrlenW(This->paf->szFileName);
661 *ppszFileName = (LPOLESTR)GlobalAllocPtr(GHND, len * sizeof(WCHAR));
662 if (*ppszFileName == NULL)
663 return AVIERR_MEMORY;
665 memcpy(*ppszFileName, This->paf->szFileName, len * sizeof(WCHAR));
668 return AVIERR_OK;
671 /***********************************************************************/
673 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
674 REFIID refiid, LPVOID *obj)
676 ICOM_THIS(IAVIStreamImpl,iface);
678 assert(This->paf != NULL);
680 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
683 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
685 ICOM_THIS(IAVIStreamImpl,iface);
687 assert(This->paf != NULL);
689 return IAVIFile_AddRef((PAVIFILE)This->paf);
692 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
694 ICOM_THIS(IAVIStreamImpl,iface);
696 assert(This->paf != NULL);
698 return IAVIFile_Release((PAVIFILE)This->paf);
701 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
702 LPARAM lParam2)
704 TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
706 /* This IAVIStream interface needs an WAVFile */
707 return AVIERR_UNSUPPORTED;
710 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
711 LONG size)
713 ICOM_THIS(IAVIStreamImpl,iface);
715 TRACE("(%p,%p,%ld)\n", iface, psi, size);
717 if (psi == NULL)
718 return AVIERR_BADPARAM;
719 if (size < 0)
720 return AVIERR_BADSIZE;
722 memcpy(psi, &This->paf->sInfo, min((DWORD)size, sizeof(This->paf->sInfo)));
724 if ((DWORD)size < sizeof(This->paf->sInfo))
725 return AVIERR_BUFFERTOOSMALL;
726 return AVIERR_OK;
729 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
730 LONG flags)
732 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
734 TRACE("(%p,%ld,0x%08lX)\n",iface,pos,flags);
736 /* Do we have data? */
737 if (This->lpFormat == NULL)
738 return -1;
740 /* We don't have an index */
741 if (flags & FIND_INDEX)
742 return -1;
744 if (flags & FIND_FROM_START) {
745 pos = This->sInfo.dwStart;
746 flags &= ~(FIND_FROM_START|FIND_PREV);
747 flags |= FIND_NEXT;
750 if (flags & FIND_FORMAT) {
751 if ((flags & FIND_NEXT) && pos > 0)
752 pos = -1;
753 else
754 pos = 0;
757 if (flags & (FIND_LENGTH|FIND_SIZE))
758 return This->sInfo.dwSampleSize;
759 if (flags & FIND_OFFSET)
760 return This->ckData.dwDataOffset + pos * This->sInfo.dwSampleSize;
762 return pos;
765 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
766 LPVOID format, LONG *formatsize)
768 ICOM_THIS(IAVIStreamImpl,iface);
770 TRACE("(%p,%ld,%p,%p)\n", iface, pos, format, formatsize);
772 if (formatsize == NULL)
773 return AVIERR_BADPARAM;
775 /* only interested in needed buffersize? */
776 if (format == NULL || *formatsize <= 0) {
777 *formatsize = This->paf->cbFormat;
779 return AVIERR_OK;
782 /* copy initial format (only as much as will fit) */
783 memcpy(format, This->paf->lpFormat, min(*formatsize, This->paf->cbFormat));
784 if (*formatsize < This->paf->cbFormat) {
785 *formatsize = This->paf->cbFormat;
786 return AVIERR_BUFFERTOOSMALL;
789 *formatsize = This->paf->cbFormat;
790 return AVIERR_OK;
793 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
794 LPVOID format, LONG formatsize)
796 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
798 TRACE("(%p,%ld,%p,%ld)\n", iface, pos, format, formatsize);
800 /* check parameters */
801 if (format == NULL || formatsize <= sizeof(PCMWAVEFORMAT))
802 return AVIERR_BADPARAM;
804 /* We can only do this to an empty wave file, but ignore call
805 * if still same format */
806 if (This->lpFormat != NULL) {
807 if (formatsize != This->cbFormat ||
808 memcmp(format, This->lpFormat, formatsize) != 0)
809 return AVIERR_UNSUPPORTED;
811 return AVIERR_OK;
814 /* only support start at position 0 */
815 if (pos != 0)
816 return AVIERR_UNSUPPORTED;
818 /* Do we have write permission? */
819 if ((This->uMode & MMIO_RWMODE) == 0)
820 return AVIERR_READONLY;
822 /* get memory for format and copy it */
823 This->lpFormat = (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, formatsize);
824 if (This->lpFormat == NULL)
825 return AVIERR_MEMORY;
827 This->cbFormat = formatsize;
828 memcpy(This->lpFormat, format, formatsize);
830 /* update info's about 'data' chunk */
831 This->ckData.dwDataOffset = formatsize + 7 * sizeof(DWORD);
832 This->ckData.cksize = 0;
834 /* for non-pcm format we need also a 'fact' chunk */
835 if (This->lpFormat->wFormatTag != WAVE_FORMAT_PCM)
836 This->ckData.dwDataOffset += 3 * sizeof(DWORD);
838 /* update stream and file info */
839 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign;
840 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
841 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
842 This->sInfo.dwLength = 0;
843 This->sInfo.dwSuggestedBufferSize = 0;
845 return AVIERR_OK;
848 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
849 LONG samples, LPVOID buffer,
850 LONG buffersize, LPLONG bytesread,
851 LPLONG samplesread)
853 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
855 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface, start, samples, buffer,
856 buffersize, bytesread, samplesread);
858 /* clear return parameters if given */
859 if (bytesread != NULL)
860 *bytesread = 0;
861 if (samplesread != NULL)
862 *samplesread = 0;
864 /* positions without data */
865 if (start < 0 || (DWORD)start > This->sInfo.dwLength)
866 return AVIERR_OK;
868 /* check samples */
869 if (samples < 0)
870 samples = 0;
871 if (buffersize > 0) {
872 if (samples > 0)
873 samples = min((DWORD)samples, buffersize / This->sInfo.dwSampleSize);
874 else
875 samples = buffersize / This->sInfo.dwSampleSize;
878 /* limit to end of stream */
879 if ((DWORD)(start + samples) > This->sInfo.dwLength)
880 samples = This->sInfo.dwLength - start;
882 /* request only the sizes? */
883 if (buffer == NULL || buffersize <= 0) {
884 /* then I need atleast one parameter for it */
885 if (bytesread == NULL && samplesread == NULL)
886 return AVIERR_BADPARAM;
888 if (bytesread != NULL)
889 *bytesread = samples * This->sInfo.dwSampleSize;
890 if (samplesread != NULL)
891 *samplesread = samples;
893 return AVIERR_OK;
896 /* nothing to read? */
897 if (samples == 0)
898 return AVIERR_OK;
900 /* Can I atleast read one sample? */
901 if ((DWORD)buffersize < This->sInfo.dwSampleSize)
902 return AVIERR_BUFFERTOOSMALL;
904 buffersize = samples * This->sInfo.dwSampleSize;
906 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset
907 + start * This->sInfo.dwSampleSize, SEEK_SET) == -1)
908 return AVIERR_FILEREAD;
909 if (mmioRead(This->hmmio, (HPSTR)buffer, buffersize) != buffersize)
910 return AVIERR_FILEREAD;
912 /* fill out return parameters if given */
913 if (bytesread != NULL)
914 *bytesread = buffersize;
915 if (samplesread != NULL)
916 *samplesread = samples;
918 return AVIERR_OK;
921 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
922 LONG samples, LPVOID buffer,
923 LONG buffersize, DWORD flags,
924 LPLONG sampwritten,
925 LPLONG byteswritten)
927 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
929 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface, start, samples,
930 buffer, buffersize, flags, sampwritten, byteswritten);
932 /* clear return parameters if given */
933 if (sampwritten != NULL)
934 *sampwritten = 0;
935 if (byteswritten != NULL)
936 *byteswritten = 0;
938 /* check parameters */
939 if (buffer == NULL && (buffersize > 0 || samples > 0))
940 return AVIERR_BADPARAM;
942 /* Have we write permission? */
943 if ((This->uMode & MMIO_RWMODE) == 0)
944 return AVIERR_READONLY;
946 /* < 0 means "append" */
947 if (start < 0)
948 start = This->sInfo.dwStart + This->sInfo.dwLength;
950 /* check buffersize -- must multiple of samplesize */
951 if (buffersize & ~(This->sInfo.dwSampleSize - 1))
952 return AVIERR_BADSIZE;
954 /* have we anything to write? */
955 if (buffer != NULL && buffersize > 0) {
956 This->fDirty = 1;
958 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset +
959 start * This->sInfo.dwSampleSize, SEEK_SET) == -1)
960 return AVIERR_FILEWRITE;
961 if (mmioWrite(This->hmmio, (HPSTR)buffer, buffersize) != buffersize)
962 return AVIERR_FILEWRITE;
964 This->sInfo.dwLength = max(This->sInfo.dwLength, (DWORD)start + samples);
965 This->ckData.cksize = max(This->ckData.cksize,
966 start * This->sInfo.dwSampleSize + buffersize);
968 /* fill out return parameters if given */
969 if (sampwritten != NULL)
970 *sampwritten = samples;
971 if (byteswritten != NULL)
972 *byteswritten = buffersize;
975 return AVIERR_OK;
978 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
979 LONG samples)
981 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
983 TRACE("(%p,%ld,%ld)\n", iface, start, samples);
985 /* check parameters */
986 if (start < 0 || samples < 0)
987 return AVIERR_BADPARAM;
989 /* Delete before start of stream? */
990 if ((DWORD)(start + samples) < This->sInfo.dwStart)
991 return AVIERR_OK;
993 /* Delete after end of stream? */
994 if ((DWORD)start > This->sInfo.dwLength)
995 return AVIERR_OK;
997 /* For the rest we need write permissions */
998 if ((This->uMode & MMIO_RWMODE) == 0)
999 return AVIERR_READONLY;
1001 if ((DWORD)(start + samples) >= This->sInfo.dwLength) {
1002 /* deletion at end */
1003 samples = This->sInfo.dwLength - start;
1004 This->sInfo.dwLength -= samples;
1005 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
1006 } else if ((DWORD)start <= This->sInfo.dwStart) {
1007 /* deletion at start */
1008 samples = This->sInfo.dwStart - start;
1009 start = This->sInfo.dwStart;
1010 This->ckData.dwDataOffset += samples * This->sInfo.dwSampleSize;
1011 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
1012 } else {
1013 /* deletion inside stream -- needs playlist and cue's */
1014 FIXME(": deletion inside of stream not supported!\n");
1016 return AVIERR_UNSUPPORTED;
1019 This->fDirty = 1;
1021 return AVIERR_OK;
1024 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1025 LPVOID lp, LPLONG lpread)
1027 ICOM_THIS(IAVIStreamImpl,iface);
1029 assert(This->paf != NULL);
1031 return IAVIFile_ReadData((PAVIFILE)This->paf, fcc, lp, lpread);
1034 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1035 LPVOID lp, LONG size)
1037 ICOM_THIS(IAVIStreamImpl,iface);
1039 return IAVIFile_WriteData((PAVIFILE)This->paf, fcc, lp, size);
1042 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1043 LPAVISTREAMINFOW info, LONG infolen)
1045 FIXME("(%p,%p,%ld): stub\n", iface, info, infolen);
1047 return E_FAIL;
1050 /***********************************************************************/
1052 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1054 MMCKINFO ckRIFF;
1055 MMCKINFO ck;
1057 This->sInfo.dwLength = 0; /* just to be sure */
1058 This->fDirty = FALSE;
1060 /* search for RIFF chunk */
1061 ckRIFF.fccType = 0; /* find any */
1062 if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) {
1063 return AVIFILE_LoadSunFile(This);
1066 if (ckRIFF.fccType != formtypeWAVE)
1067 return AVIERR_BADFORMAT;
1069 /* search WAVE format chunk */
1070 ck.ckid = ckidWAVEFORMAT;
1071 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck,
1072 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
1073 return AVIERR_FILEREAD;
1075 /* get memory for format and read it */
1076 This->lpFormat = (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, ck.cksize);
1077 if (This->lpFormat == NULL)
1078 return AVIERR_FILEREAD;
1079 This->cbFormat = ck.cksize;
1081 if (mmioRead(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize)
1082 return AVIERR_FILEREAD;
1083 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1084 return AVIERR_FILEREAD;
1086 /* Non-pcm formats have a fact chunk.
1087 * We don't need it, so simply add it to the extra chunks.
1090 /* find the big data chunk */
1091 This->ckData.ckid = ckidWAVEDATA;
1092 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &This->ckData,
1093 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
1094 return AVIERR_FILEREAD;
1096 memset(&This->sInfo, 0, sizeof(This->sInfo));
1097 This->sInfo.fccType = streamtypeAUDIO;
1098 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
1099 This->sInfo.dwSampleSize =
1100 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
1101 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign;
1102 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize;
1104 This->fInfo.dwStreams = 1;
1106 if (mmioAscend(This->hmmio, &This->ckData, 0) != S_OK) {
1107 /* seems to be truncated */
1108 WARN(": file seems to be truncated!\n");
1109 This->ckData.cksize = mmioSeek(This->hmmio, 0, SEEK_END) -
1110 This->ckData.dwDataOffset;
1111 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign;
1112 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize;
1115 /* ignore errors */
1116 FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck, &ckRIFF, 0);
1118 return AVIERR_OK;
1121 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This)
1123 SUNAUDIOHEADER auhdr;
1125 mmioSeek(This->hmmio, 0, SEEK_SET);
1126 if (mmioRead(This->hmmio, (HPSTR)&auhdr, sizeof(auhdr)) != sizeof(auhdr))
1127 return AVIERR_FILEREAD;
1129 if (auhdr.fccType == 0x0064732E) {
1130 /* header in little endian */
1131 This->ckData.dwDataOffset = LE2H_DWORD(auhdr.offset);
1132 This->ckData.cksize = LE2H_DWORD(auhdr.size);
1134 auhdr.encoding = LE2H_DWORD(auhdr.encoding);
1135 auhdr.sampleRate = LE2H_DWORD(auhdr.sampleRate);
1136 auhdr.channels = LE2H_DWORD(auhdr.channels);
1137 } else if (auhdr.fccType == mmioFOURCC('.','s','n','d')) {
1138 /* header in big endian */
1139 This->ckData.dwDataOffset = BE2H_DWORD(auhdr.offset);
1140 This->ckData.cksize = BE2H_DWORD(auhdr.size);
1142 auhdr.encoding = BE2H_DWORD(auhdr.encoding);
1143 auhdr.sampleRate = BE2H_DWORD(auhdr.sampleRate);
1144 auhdr.channels = BE2H_DWORD(auhdr.channels);
1145 } else
1146 return AVIERR_FILEREAD;
1148 if (auhdr.channels < 1)
1149 return AVIERR_BADFORMAT;
1151 /* get size of header */
1152 switch(auhdr.encoding) {
1153 case AU_ENCODING_ADPCM_G721_32:
1154 This->cbFormat = sizeof(G721_ADPCMWAVEFORMAT); break;
1155 case AU_ENCODING_ADPCM_G723_24:
1156 This->cbFormat = sizeof(G723_ADPCMWAVEFORMAT); break;
1157 case AU_ENCODING_ADPCM_G722:
1158 case AU_ENCODING_ADPCM_G723_5:
1159 WARN("unsupported Sun audio format %d\n", auhdr.encoding);
1160 return AVIERR_UNSUPPORTED; /* FIXME */
1161 default:
1162 This->cbFormat = sizeof(WAVEFORMATEX); break;
1165 This->lpFormat =
1166 (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, This->cbFormat);
1167 if (This->lpFormat == NULL)
1168 return AVIERR_MEMORY;
1170 This->lpFormat->nChannels = auhdr.channels;
1171 This->lpFormat->nSamplesPerSec = auhdr.sampleRate;
1172 switch(auhdr.encoding) {
1173 case AU_ENCODING_ULAW_8:
1174 This->lpFormat->wFormatTag = WAVE_FORMAT_MULAW;
1175 This->lpFormat->wBitsPerSample = 8;
1176 break;
1177 case AU_ENCODING_PCM_8:
1178 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1179 This->lpFormat->wBitsPerSample = 8;
1180 break;
1181 case AU_ENCODING_PCM_16:
1182 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1183 This->lpFormat->wBitsPerSample = 16;
1184 break;
1185 case AU_ENCODING_PCM_24:
1186 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1187 This->lpFormat->wBitsPerSample = 24;
1188 break;
1189 case AU_ENCODING_PCM_32:
1190 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1191 This->lpFormat->wBitsPerSample = 32;
1192 break;
1193 case AU_ENCODING_ALAW_8:
1194 This->lpFormat->wFormatTag = WAVE_FORMAT_ALAW;
1195 This->lpFormat->wBitsPerSample = 8;
1196 break;
1197 case AU_ENCODING_ADPCM_G721_32:
1198 This->lpFormat->wFormatTag = WAVE_FORMAT_G721_ADPCM;
1199 This->lpFormat->wBitsPerSample = (3*5*8);
1200 This->lpFormat->nBlockAlign = 15*15*8;
1201 This->lpFormat->cbSize = sizeof(WORD);
1202 ((LPG721_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0;
1203 break;
1204 case AU_ENCODING_ADPCM_G723_24:
1205 This->lpFormat->wFormatTag = WAVE_FORMAT_G723_ADPCM;
1206 This->lpFormat->wBitsPerSample = (3*5*8);
1207 This->lpFormat->nBlockAlign = 15*15*8;
1208 This->lpFormat->cbSize = 2*sizeof(WORD);
1209 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->cbExtraSize = 0;
1210 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0;
1211 break;
1212 default:
1213 WARN("unsupported Sun audio format %d\n", auhdr.encoding);
1214 return AVIERR_UNSUPPORTED;
1217 This->lpFormat->nBlockAlign =
1218 (This->lpFormat->nChannels * This->lpFormat->wBitsPerSample) / 8;
1219 if (This->lpFormat->nBlockAlign == 0 && This->lpFormat->wBitsPerSample < 8)
1220 This->lpFormat->nBlockAlign++;
1221 This->lpFormat->nAvgBytesPerSec =
1222 This->lpFormat->nBlockAlign * This->lpFormat->nSamplesPerSec;
1224 This->fDirty = 0;
1226 This->sInfo.fccType = streamtypeAUDIO;
1227 This->sInfo.fccHandler = 0;
1228 This->sInfo.dwFlags = 0;
1229 This->sInfo.wPriority = 0;
1230 This->sInfo.wLanguage = 0;
1231 This->sInfo.dwInitialFrames = 0;
1232 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
1233 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
1234 This->sInfo.dwStart = 0;
1235 This->sInfo.dwLength =
1236 This->ckData.cksize / This->lpFormat->nBlockAlign;
1237 This->sInfo.dwSuggestedBufferSize = This->sInfo.dwLength;
1238 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign;
1240 This->fInfo.dwStreams = 1;
1241 This->fInfo.dwScale = 1;
1242 This->fInfo.dwRate = This->lpFormat->nSamplesPerSec;
1243 This->fInfo.dwLength =
1244 MulDiv(This->ckData.cksize, This->lpFormat->nSamplesPerSec,
1245 This->lpFormat->nAvgBytesPerSec);
1247 return AVIERR_OK;
1250 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
1252 MMCKINFO ckRIFF;
1253 MMCKINFO ck;
1255 mmioSeek(This->hmmio, 0, SEEK_SET);
1257 /* create the RIFF chunk with formtype WAVE */
1258 ckRIFF.fccType = formtypeWAVE;
1259 ckRIFF.cksize = 0;
1260 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
1261 return AVIERR_FILEWRITE;
1263 /* the next chunk is the format */
1264 ck.ckid = ckidWAVEFORMAT;
1265 ck.cksize = This->cbFormat;
1266 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1267 return AVIERR_FILEWRITE;
1268 if (This->lpFormat != NULL && This->cbFormat > 0) {
1269 if (mmioWrite(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize)
1270 return AVIERR_FILEWRITE;
1272 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1273 return AVIERR_FILEWRITE;
1275 /* fact chunk is needed for non-pcm waveforms */
1276 if (This->lpFormat != NULL && This->cbFormat > sizeof(PCMWAVEFORMAT) &&
1277 This->lpFormat->wFormatTag != WAVE_FORMAT_PCM) {
1278 WAVEFORMATEX wfx;
1279 DWORD dwFactLength;
1280 HACMSTREAM has;
1282 /* try to open an appropriate audio codec to figure out
1283 * data for fact-chunk */
1284 wfx.wFormatTag = WAVE_FORMAT_PCM;
1285 if (acmFormatSuggest(NULL, This->lpFormat, &wfx,
1286 sizeof(wfx), ACM_FORMATSUGGESTF_WFORMATTAG)) {
1287 acmStreamOpen(&has, NULL, This->lpFormat, &wfx, NULL,
1288 0, 0, ACM_STREAMOPENF_NONREALTIME);
1289 acmStreamSize(has, This->ckData.cksize, &dwFactLength,
1290 ACM_STREAMSIZEF_SOURCE);
1291 dwFactLength /= wfx.nBlockAlign;
1292 acmStreamClose(has, 0);
1294 /* crete the fact chunk */
1295 ck.ckid = ckidWAVEFACT;
1296 ck.cksize = sizeof(dwFactLength);
1298 /* test for enough space before data chunk */
1299 if (mmioSeek(This->hmmio, 0, SEEK_CUR) > This->ckData.dwDataOffset
1300 - ck.cksize - 4 * sizeof(DWORD))
1301 return AVIERR_FILEWRITE;
1302 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1303 return AVIERR_FILEWRITE;
1304 if (mmioWrite(This->hmmio, (HPSTR)&dwFactLength, ck.cksize) != ck.cksize)
1305 return AVIERR_FILEWRITE;
1306 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1307 return AVIERR_FILEWRITE;
1308 } else
1309 ERR(": fact chunk is needed for non-pcm files -- currently no codec found, so skipped!\n");
1312 /* if here was extra stuff, we need to fill it with JUNK */
1313 if (mmioSeek(This->hmmio, 0, SEEK_CUR) + 2 * sizeof(DWORD) < This->ckData.dwDataOffset) {
1314 ck.ckid = ckidAVIPADDING;
1315 ck.cksize = 0;
1316 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1317 return AVIERR_FILEWRITE;
1319 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset
1320 - 2 * sizeof(DWORD), SEEK_SET) == -1)
1321 return AVIERR_FILEWRITE;
1322 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1323 return AVIERR_FILEWRITE;
1326 /* crete the data chunk */
1327 ck.ckid = ckidWAVEDATA;
1328 ck.cksize = This->ckData.cksize;
1329 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1330 return AVIERR_FILEWRITE;
1331 if (mmioSeek(This->hmmio, This->ckData.cksize, SEEK_CUR) == -1)
1332 return AVIERR_FILEWRITE;
1333 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1334 return AVIERR_FILEWRITE;
1336 /* some optional extra chunks? */
1337 if (This->extra.lp != NULL && This->extra.cb > 0) {
1338 /* chunk headers are already in structure */
1339 if (mmioWrite(This->hmmio, This->extra.lp, This->extra.cb) != This->extra.cb)
1340 return AVIERR_FILEWRITE;
1343 /* close RIFF chunk */
1344 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
1345 return AVIERR_FILEWRITE;
1346 if (mmioFlush(This->hmmio, 0) != S_OK)
1347 return AVIERR_FILEWRITE;
1349 return AVIERR_OK;