winefile: Use SendMessageW instead of SNDMSG.
[wine.git] / dlls / avifil32 / wavfile.c
blobd3048d6031857dbd7be9181956c9912f061e7da7
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <assert.h>
20 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winuser.h"
26 #include "winnls.h"
27 #include "winerror.h"
28 #include "mmsystem.h"
29 #include "vfw.h"
30 #include "msacm.h"
32 #include "avifile_private.h"
33 #include "extrachunk.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
40 /***********************************************************************/
42 #define formtypeWAVE mmioFOURCC('W','A','V','E')
43 #define ckidWAVEFORMAT mmioFOURCC('f','m','t',' ')
44 #define ckidWAVEFACT mmioFOURCC('f','a','c','t')
45 #define ckidWAVEDATA mmioFOURCC('d','a','t','a')
47 /***********************************************************************/
49 #define ENDIAN_SWAPWORD(x) ((((x) >> 8) & 0xFF) | (((x) & 0xFF) << 8))
50 #define ENDIAN_SWAPDWORD(x) (ENDIAN_SWAPWORD((x >> 16) & 0xFFFF) | \
51 ENDIAN_SWAPWORD(x & 0xFFFF) << 16)
53 #ifdef WORDS_BIGENDIAN
54 #define BE2H_WORD(x) (x)
55 #define BE2H_DWORD(x) (x)
56 #define LE2H_WORD(x) ENDIAN_SWAPWORD(x)
57 #define LE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
58 #else
59 #define BE2H_WORD(x) ENDIAN_SWAPWORD(x)
60 #define BE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
61 #define LE2H_WORD(x) (x)
62 #define LE2H_DWORD(x) (x)
63 #endif
65 typedef struct {
66 FOURCC fccType;
67 DWORD offset;
68 DWORD size;
69 INT encoding;
70 DWORD sampleRate;
71 DWORD channels;
72 } SUNAUDIOHEADER;
74 #define AU_ENCODING_ULAW_8 1
75 #define AU_ENCODING_PCM_8 2
76 #define AU_ENCODING_PCM_16 3
77 #define AU_ENCODING_PCM_24 4
78 #define AU_ENCODING_PCM_32 5
79 #define AU_ENCODING_FLOAT 6
80 #define AU_ENCODING_DOUBLE 7
81 #define AU_ENCODING_ADPCM_G721_32 23
82 #define AU_ENCODING_ADPCM_G722 24
83 #define AU_ENCODING_ADPCM_G723_24 25
84 #define AU_ENCODING_ADPCM_G723_5 26
85 #define AU_ENCODING_ALAW_8 27
87 /***********************************************************************/
89 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
90 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
91 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile* iface);
92 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
93 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
94 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
95 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
96 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
97 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
98 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
100 static const struct IAVIFileVtbl iwavft = {
101 IAVIFile_fnQueryInterface,
102 IAVIFile_fnAddRef,
103 IAVIFile_fnRelease,
104 IAVIFile_fnInfo,
105 IAVIFile_fnGetStream,
106 IAVIFile_fnCreateStream,
107 IAVIFile_fnWriteData,
108 IAVIFile_fnReadData,
109 IAVIFile_fnEndRecord,
110 IAVIFile_fnDeleteStream
113 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile*iface,REFIID refiid,LPVOID*obj);
114 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile*iface);
115 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile*iface);
116 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile*iface,CLSID*pClassID);
117 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile*iface);
118 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile*iface,LPCOLESTR pszFileName,DWORD dwMode);
119 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile*iface,LPCOLESTR pszFileName,BOOL fRemember);
120 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile*iface,LPCOLESTR pszFileName);
121 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile*iface,LPOLESTR*ppszFileName);
123 static const struct IPersistFileVtbl iwavpft = {
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 static const struct IAVIStreamVtbl iwavst = {
151 IAVIStream_fnQueryInterface,
152 IAVIStream_fnAddRef,
153 IAVIStream_fnRelease,
154 IAVIStream_fnCreate,
155 IAVIStream_fnInfo,
156 IAVIStream_fnFindSample,
157 IAVIStream_fnReadFormat,
158 IAVIStream_fnSetFormat,
159 IAVIStream_fnRead,
160 IAVIStream_fnWrite,
161 IAVIStream_fnDelete,
162 IAVIStream_fnReadData,
163 IAVIStream_fnWriteData,
164 IAVIStream_fnSetInfo
167 typedef struct _IAVIFileImpl IAVIFileImpl;
169 typedef struct _IPersistFileImpl {
170 /* IUnknown stuff */
171 const IPersistFileVtbl *lpVtbl;
173 /* IPersistFile stuff */
174 IAVIFileImpl *paf;
175 } IPersistFileImpl;
177 typedef struct _IAVIStreamImpl {
178 /* IUnknown stuff */
179 const IAVIStreamVtbl *lpVtbl;
181 /* IAVIStream stuff */
182 IAVIFileImpl *paf;
183 } IAVIStreamImpl;
185 struct _IAVIFileImpl {
186 /* IUnknown stuff */
187 const IAVIFileVtbl *lpVtbl;
188 LONG ref;
190 /* IAVIFile, IAVIStream stuff... */
191 IPersistFileImpl iPersistFile;
192 IAVIStreamImpl iAVIStream;
194 AVIFILEINFOW fInfo;
195 AVISTREAMINFOW sInfo;
197 LPWAVEFORMATEX lpFormat;
198 LONG cbFormat;
200 MMCKINFO ckData;
202 EXTRACHUNKS extra;
204 /* IPersistFile stuff ... */
205 HMMIO hmmio;
206 LPWSTR szFileName;
207 UINT uMode;
208 BOOL fDirty;
211 /***********************************************************************/
213 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
214 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This);
215 static HRESULT AVIFILE_SaveFile(const IAVIFileImpl *This);
217 HRESULT AVIFILE_CreateWAVFile(REFIID riid, LPVOID *ppv)
219 IAVIFileImpl *pfile;
220 HRESULT hr;
222 assert(riid != NULL && ppv != NULL);
224 *ppv = NULL;
226 pfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIFileImpl));
227 if (pfile == NULL)
228 return AVIERR_MEMORY;
230 pfile->lpVtbl = &iwavft;
231 pfile->iPersistFile.lpVtbl = &iwavpft;
232 pfile->iAVIStream.lpVtbl = &iwavst;
233 pfile->ref = 0;
234 pfile->iPersistFile.paf = pfile;
235 pfile->iAVIStream.paf = pfile;
237 hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
238 if (FAILED(hr))
239 HeapFree(GetProcessHeap(), 0, pfile);
241 return hr;
244 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
245 LPVOID *obj)
247 IAVIFileImpl *This = (IAVIFileImpl *)iface;
249 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
251 if (IsEqualGUID(&IID_IUnknown, refiid) ||
252 IsEqualGUID(&IID_IAVIFile, refiid)) {
253 *obj = iface;
254 return S_OK;
255 } else if (This->fInfo.dwStreams == 1 &&
256 IsEqualGUID(&IID_IAVIStream, refiid)) {
257 *obj = &This->iAVIStream;
258 return S_OK;
259 } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
260 *obj = &This->iPersistFile;
261 return S_OK;
264 return OLE_E_ENUM_NOMORE;
267 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
269 IAVIFileImpl *This = (IAVIFileImpl *)iface;
271 TRACE("(%p)\n",iface);
273 return InterlockedIncrement(&This->ref);
276 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
278 IAVIFileImpl *This = (IAVIFileImpl *)iface;
279 ULONG ref = InterlockedDecrement(&This->ref);
281 TRACE("(%p)\n",iface);
283 if (!ref) {
284 if (This->fDirty) {
285 /* need to write headers to file */
286 AVIFILE_SaveFile(This);
289 if (This->lpFormat != NULL) {
290 HeapFree(GetProcessHeap(), 0, This->lpFormat);
291 This->lpFormat = NULL;
292 This->cbFormat = 0;
294 if (This->extra.lp != NULL) {
295 HeapFree(GetProcessHeap(), 0, This->extra.lp);
296 This->extra.lp = NULL;
297 This->extra.cb = 0;
299 HeapFree(GetProcessHeap(), 0, This->szFileName);
300 This->szFileName = NULL;
301 if (This->hmmio != NULL) {
302 mmioClose(This->hmmio, 0);
303 This->hmmio = NULL;
306 HeapFree(GetProcessHeap(), 0, This);
307 return 0;
309 return ref;
312 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
313 LONG size)
315 IAVIFileImpl *This = (IAVIFileImpl *)iface;
317 TRACE("(%p,%p,%d)\n",iface,afi,size);
319 if (afi == NULL)
320 return AVIERR_BADPARAM;
321 if (size < 0)
322 return AVIERR_BADSIZE;
324 /* update file info */
325 This->fInfo.dwFlags = 0;
326 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
327 if (This->lpFormat != NULL) {
328 assert(This->sInfo.dwScale != 0);
330 This->fInfo.dwStreams = 1;
331 This->fInfo.dwScale = This->sInfo.dwScale;
332 This->fInfo.dwRate = This->sInfo.dwRate;
333 This->fInfo.dwLength = This->sInfo.dwLength;
334 This->fInfo.dwSuggestedBufferSize = This->ckData.cksize;
335 This->fInfo.dwMaxBytesPerSec =
336 MulDiv(This->sInfo.dwSampleSize,This->sInfo.dwRate,This->sInfo.dwScale);
339 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
341 if ((DWORD)size < sizeof(This->fInfo))
342 return AVIERR_BUFFERTOOSMALL;
343 return AVIERR_OK;
346 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
347 DWORD fccType, LONG lParam)
349 IAVIFileImpl *This = (IAVIFileImpl *)iface;
351 TRACE("(%p,%p,0x%08X,%d)\n", iface, avis, fccType, lParam);
353 /* check parameter */
354 if (avis == NULL)
355 return AVIERR_BADPARAM;
357 *avis = NULL;
359 /* Does our stream exists? */
360 if (lParam != 0 || This->fInfo.dwStreams == 0)
361 return AVIERR_NODATA;
362 if (fccType != 0 && fccType != streamtypeAUDIO)
363 return AVIERR_NODATA;
365 *avis = (PAVISTREAM)&This->iAVIStream;
366 IAVIFile_AddRef(iface);
368 return AVIERR_OK;
371 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
372 LPAVISTREAMINFOW asi)
374 IAVIFileImpl *This = (IAVIFileImpl *)iface;
376 TRACE("(%p,%p,%p)\n", iface, avis, asi);
378 /* check parameters */
379 if (avis == NULL || asi == NULL)
380 return AVIERR_BADPARAM;
382 *avis = NULL;
384 /* We only support one audio stream */
385 if (This->fInfo.dwStreams != 0 || This->lpFormat != NULL)
386 return AVIERR_UNSUPPORTED;
387 if (asi->fccType != streamtypeAUDIO)
388 return AVIERR_UNSUPPORTED;
390 /* Does the user have write permission? */
391 if ((This->uMode & MMIO_RWMODE) == 0)
392 return AVIERR_READONLY;
394 This->cbFormat = 0;
395 This->lpFormat = NULL;
397 memcpy(&This->sInfo, asi, sizeof(This->sInfo));
399 /* make sure streaminfo if okay for us */
400 This->sInfo.fccHandler = 0;
401 This->sInfo.dwFlags = 0;
402 This->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
403 This->sInfo.dwStart = 0;
404 This->sInfo.dwInitialFrames = 0;
405 This->sInfo.dwFormatChangeCount = 0;
406 memset(&This->sInfo.rcFrame, 0, sizeof(This->sInfo.rcFrame));
408 This->fInfo.dwStreams = 1;
409 This->fInfo.dwScale = This->sInfo.dwScale;
410 This->fInfo.dwRate = This->sInfo.dwRate;
411 This->fInfo.dwLength = This->sInfo.dwLength;
413 This->ckData.dwDataOffset = 0;
414 This->ckData.cksize = 0;
416 *avis = (PAVISTREAM)&This->iAVIStream;
417 IAVIFile_AddRef(iface);
419 return AVIERR_OK;
422 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
423 LPVOID lpData, LONG size)
425 IAVIFileImpl *This = (IAVIFileImpl *)iface;
427 TRACE("(%p,0x%08X,%p,%d)\n", iface, ckid, lpData, size);
429 /* check parameters */
430 if (lpData == NULL)
431 return AVIERR_BADPARAM;
432 if (size < 0)
433 return AVIERR_BADSIZE;
435 /* Do we have write permission? */
436 if ((This->uMode & MMIO_RWMODE) == 0)
437 return AVIERR_READONLY;
439 This->fDirty = TRUE;
441 return WriteExtraChunk(&This->extra, ckid, lpData, size);
444 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
445 LPVOID lpData, LONG *size)
447 IAVIFileImpl *This = (IAVIFileImpl *)iface;
449 TRACE("(%p,0x%08X,%p,%p)\n", iface, ckid, lpData, size);
451 return ReadExtraChunk(&This->extra, ckid, lpData, size);
454 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
456 TRACE("(%p)\n",iface);
458 /* This is only needed for interleaved files.
459 * We have only one stream, which can't be interleaved.
461 return AVIERR_OK;
464 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
465 LONG lParam)
467 IAVIFileImpl *This = (IAVIFileImpl *)iface;
469 TRACE("(%p,0x%08X,%d)\n", iface, fccType, lParam);
471 /* check parameter */
472 if (lParam < 0)
473 return AVIERR_BADPARAM;
475 /* Do we have our audio stream? */
476 if (lParam != 0 || This->fInfo.dwStreams == 0 ||
477 (fccType != 0 && fccType != streamtypeAUDIO))
478 return AVIERR_NODATA;
480 /* Have user write permissions? */
481 if ((This->uMode & MMIO_RWMODE) == 0)
482 return AVIERR_READONLY;
484 HeapFree(GetProcessHeap(), 0, This->lpFormat);
485 This->lpFormat = NULL;
486 This->cbFormat = 0;
488 /* update infos */
489 This->ckData.dwDataOffset = 0;
490 This->ckData.cksize = 0;
492 This->sInfo.dwScale = 0;
493 This->sInfo.dwRate = 0;
494 This->sInfo.dwLength = 0;
495 This->sInfo.dwSuggestedBufferSize = 0;
497 This->fInfo.dwStreams = 0;
498 This->fInfo.dwEditCount++;
500 This->fDirty = TRUE;
502 return AVIERR_OK;
505 /***********************************************************************/
507 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
508 REFIID refiid, LPVOID *obj)
510 IPersistFileImpl *This = (IPersistFileImpl *)iface;
512 assert(This->paf != NULL);
514 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
517 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
519 IPersistFileImpl *This = (IPersistFileImpl *)iface;
521 assert(This->paf != NULL);
523 return IAVIFile_AddRef((PAVIFILE)This->paf);
526 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface)
528 IPersistFileImpl *This = (IPersistFileImpl *)iface;
530 assert(This->paf != NULL);
532 return IAVIFile_Release((PAVIFILE)This->paf);
535 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
536 LPCLSID pClassID)
538 TRACE("(%p,%p)\n", iface, pClassID);
540 if (pClassID == NULL)
541 return AVIERR_BADPARAM;
543 memcpy(pClassID, &CLSID_WAVFile, sizeof(CLSID_WAVFile));
545 return AVIERR_OK;
548 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
550 IPersistFileImpl *This = (IPersistFileImpl *)iface;
552 TRACE("(%p)\n", iface);
554 assert(This->paf != NULL);
556 return (This->paf->fDirty ? S_OK : S_FALSE);
559 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
560 LPCOLESTR pszFileName, DWORD dwMode)
562 IAVIFileImpl *This = ((IPersistFileImpl*)iface)->paf;
564 WCHAR wszStreamFmt[50];
565 INT len;
567 TRACE("(%p,%s,0x%08X)\n", iface, debugstr_w(pszFileName), dwMode);
569 /* check parameter */
570 if (pszFileName == NULL)
571 return AVIERR_BADPARAM;
573 assert(This != NULL);
574 if (This->hmmio != NULL)
575 return AVIERR_ERROR; /* No reuse of this object for another file! */
577 /* remeber mode and name */
578 This->uMode = dwMode;
580 len = lstrlenW(pszFileName) + 1;
581 This->szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
582 if (This->szFileName == NULL)
583 return AVIERR_MEMORY;
584 lstrcpyW(This->szFileName, pszFileName);
586 /* try to open the file */
587 This->hmmio = mmioOpenW(This->szFileName, NULL, MMIO_ALLOCBUF | dwMode);
588 if (This->hmmio == NULL) {
589 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
590 LPSTR szFileName;
591 len = WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1,
592 NULL, 0, NULL, NULL);
593 szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR));
594 if (szFileName == NULL)
595 return AVIERR_MEMORY;
597 WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1, szFileName,
598 len, NULL, NULL);
600 This->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
601 HeapFree(GetProcessHeap(), 0, szFileName);
602 if (This->hmmio == NULL)
603 return AVIERR_FILEOPEN;
606 memset(& This->fInfo, 0, sizeof(This->fInfo));
607 memset(& This->sInfo, 0, sizeof(This->sInfo));
609 LoadStringW(AVIFILE_hModule, IDS_WAVEFILETYPE, This->fInfo.szFileType,
610 sizeof(This->fInfo.szFileType));
611 if (LoadStringW(AVIFILE_hModule, IDS_WAVESTREAMFORMAT,
612 wszStreamFmt, sizeof(wszStreamFmt)) > 0) {
613 wsprintfW(This->sInfo.szName, wszStreamFmt,
614 AVIFILE_BasenameW(This->szFileName));
617 /* should we create a new file? */
618 if (dwMode & OF_CREATE) {
619 /* nothing more to do */
620 return AVIERR_OK;
621 } else
622 return AVIFILE_LoadFile(This);
625 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
626 LPCOLESTR pszFileName,BOOL fRemember)
628 TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
630 /* We write directly to disk, so nothing to do. */
632 return AVIERR_OK;
635 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
636 LPCOLESTR pszFileName)
638 TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
640 /* We write directly to disk, so nothing to do. */
642 return AVIERR_OK;
645 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
646 LPOLESTR *ppszFileName)
648 IPersistFileImpl *This = (IPersistFileImpl *)iface;
650 TRACE("(%p,%p)\n", iface, ppszFileName);
652 if (ppszFileName == NULL)
653 return AVIERR_BADPARAM;
655 *ppszFileName = NULL;
657 assert(This->paf != NULL);
659 if (This->paf->szFileName != NULL) {
660 int len = lstrlenW(This->paf->szFileName) + 1;
662 *ppszFileName = CoTaskMemAlloc(len * sizeof(WCHAR));
663 if (*ppszFileName == NULL)
664 return AVIERR_MEMORY;
666 strcpyW(*ppszFileName, This->paf->szFileName);
669 return AVIERR_OK;
672 /***********************************************************************/
674 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
675 REFIID refiid, LPVOID *obj)
677 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
679 assert(This->paf != NULL);
681 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
684 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
686 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
688 assert(This->paf != NULL);
690 return IAVIFile_AddRef((PAVIFILE)This->paf);
693 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
695 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
697 assert(This->paf != NULL);
699 return IAVIFile_Release((PAVIFILE)This->paf);
702 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
703 LPARAM lParam2)
705 TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
707 /* This IAVIStream interface needs an WAVFile */
708 return AVIERR_UNSUPPORTED;
711 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
712 LONG size)
714 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
716 TRACE("(%p,%p,%d)\n", iface, psi, size);
718 if (psi == NULL)
719 return AVIERR_BADPARAM;
720 if (size < 0)
721 return AVIERR_BADSIZE;
723 memcpy(psi, &This->paf->sInfo, min((DWORD)size, sizeof(This->paf->sInfo)));
725 if ((DWORD)size < sizeof(This->paf->sInfo))
726 return AVIERR_BUFFERTOOSMALL;
727 return AVIERR_OK;
730 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
731 LONG flags)
733 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
735 TRACE("(%p,%d,0x%08X)\n",iface,pos,flags);
737 /* Do we have data? */
738 if (This->lpFormat == NULL)
739 return -1;
741 /* We don't have an index */
742 if (flags & FIND_INDEX)
743 return -1;
745 if (flags & FIND_FROM_START) {
746 pos = This->sInfo.dwStart;
747 flags &= ~(FIND_FROM_START|FIND_PREV);
748 flags |= FIND_NEXT;
751 if (flags & FIND_FORMAT) {
752 if ((flags & FIND_NEXT) && pos > 0)
753 pos = -1;
754 else
755 pos = 0;
758 if ((flags & FIND_RET) == FIND_LENGTH ||
759 (flags & FIND_RET) == FIND_SIZE)
760 return This->sInfo.dwSampleSize;
761 if ((flags & FIND_RET) == FIND_OFFSET)
762 return This->ckData.dwDataOffset + pos * This->sInfo.dwSampleSize;
764 return pos;
767 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
768 LPVOID format, LONG *formatsize)
770 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
772 TRACE("(%p,%d,%p,%p)\n", iface, pos, format, formatsize);
774 if (formatsize == NULL)
775 return AVIERR_BADPARAM;
777 /* only interested in needed buffersize? */
778 if (format == NULL || *formatsize <= 0) {
779 *formatsize = This->paf->cbFormat;
781 return AVIERR_OK;
784 /* copy initial format (only as much as will fit) */
785 memcpy(format, This->paf->lpFormat, min(*formatsize, This->paf->cbFormat));
786 if (*formatsize < This->paf->cbFormat) {
787 *formatsize = This->paf->cbFormat;
788 return AVIERR_BUFFERTOOSMALL;
791 *formatsize = This->paf->cbFormat;
792 return AVIERR_OK;
795 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
796 LPVOID format, LONG formatsize)
798 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
800 TRACE("(%p,%d,%p,%d)\n", iface, pos, format, formatsize);
802 /* check parameters */
803 if (format == NULL || formatsize <= sizeof(PCMWAVEFORMAT))
804 return AVIERR_BADPARAM;
806 /* We can only do this to an empty wave file, but ignore call
807 * if still same format */
808 if (This->lpFormat != NULL) {
809 if (formatsize != This->cbFormat ||
810 memcmp(format, This->lpFormat, formatsize) != 0)
811 return AVIERR_UNSUPPORTED;
813 return AVIERR_OK;
816 /* only support start at position 0 */
817 if (pos != 0)
818 return AVIERR_UNSUPPORTED;
820 /* Do we have write permission? */
821 if ((This->uMode & MMIO_RWMODE) == 0)
822 return AVIERR_READONLY;
824 /* get memory for format and copy it */
825 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, formatsize);
826 if (This->lpFormat == NULL)
827 return AVIERR_MEMORY;
829 This->cbFormat = formatsize;
830 memcpy(This->lpFormat, format, formatsize);
832 /* update info's about 'data' chunk */
833 This->ckData.dwDataOffset = formatsize + 7 * sizeof(DWORD);
834 This->ckData.cksize = 0;
836 /* for non-pcm format we need also a 'fact' chunk */
837 if (This->lpFormat->wFormatTag != WAVE_FORMAT_PCM)
838 This->ckData.dwDataOffset += 3 * sizeof(DWORD);
840 /* update stream and file info */
841 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign;
842 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
843 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
844 This->sInfo.dwLength = 0;
845 This->sInfo.dwSuggestedBufferSize = 0;
847 return AVIERR_OK;
850 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
851 LONG samples, LPVOID buffer,
852 LONG buffersize, LPLONG bytesread,
853 LPLONG samplesread)
855 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
857 TRACE("(%p,%d,%d,%p,%d,%p,%p)\n", iface, start, samples, buffer,
858 buffersize, bytesread, samplesread);
860 /* clear return parameters if given */
861 if (bytesread != NULL)
862 *bytesread = 0;
863 if (samplesread != NULL)
864 *samplesread = 0;
866 /* positions without data */
867 if (start < 0 || (DWORD)start > This->sInfo.dwLength)
868 return AVIERR_OK;
870 /* check samples */
871 if (samples < 0)
872 samples = 0;
873 if (buffersize > 0) {
874 if (samples > 0)
875 samples = min((DWORD)samples, buffersize / This->sInfo.dwSampleSize);
876 else
877 samples = buffersize / This->sInfo.dwSampleSize;
880 /* limit to end of stream */
881 if ((DWORD)(start + samples) > This->sInfo.dwLength)
882 samples = This->sInfo.dwLength - start;
884 /* request only the sizes? */
885 if (buffer == NULL || buffersize <= 0) {
886 /* then I need at least one parameter for it */
887 if (bytesread == NULL && samplesread == NULL)
888 return AVIERR_BADPARAM;
890 if (bytesread != NULL)
891 *bytesread = samples * This->sInfo.dwSampleSize;
892 if (samplesread != NULL)
893 *samplesread = samples;
895 return AVIERR_OK;
898 /* nothing to read? */
899 if (samples == 0)
900 return AVIERR_OK;
902 /* Can I read at least one sample? */
903 if ((DWORD)buffersize < This->sInfo.dwSampleSize)
904 return AVIERR_BUFFERTOOSMALL;
906 buffersize = samples * This->sInfo.dwSampleSize;
908 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset
909 + start * This->sInfo.dwSampleSize, SEEK_SET) == -1)
910 return AVIERR_FILEREAD;
911 if (mmioRead(This->hmmio, (HPSTR)buffer, buffersize) != buffersize)
912 return AVIERR_FILEREAD;
914 /* fill out return parameters if given */
915 if (bytesread != NULL)
916 *bytesread = buffersize;
917 if (samplesread != NULL)
918 *samplesread = samples;
920 return AVIERR_OK;
923 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
924 LONG samples, LPVOID buffer,
925 LONG buffersize, DWORD flags,
926 LPLONG sampwritten,
927 LPLONG byteswritten)
929 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
931 TRACE("(%p,%d,%d,%p,%d,0x%08X,%p,%p)\n", iface, start, samples,
932 buffer, buffersize, flags, sampwritten, byteswritten);
934 /* clear return parameters if given */
935 if (sampwritten != NULL)
936 *sampwritten = 0;
937 if (byteswritten != NULL)
938 *byteswritten = 0;
940 /* check parameters */
941 if (buffer == NULL && (buffersize > 0 || samples > 0))
942 return AVIERR_BADPARAM;
944 /* Do we have write permission? */
945 if ((This->uMode & MMIO_RWMODE) == 0)
946 return AVIERR_READONLY;
948 /* < 0 means "append" */
949 if (start < 0)
950 start = This->sInfo.dwStart + This->sInfo.dwLength;
952 /* check buffersize -- must multiple of samplesize */
953 if (buffersize & ~(This->sInfo.dwSampleSize - 1))
954 return AVIERR_BADSIZE;
956 /* do we have anything to write? */
957 if (buffer != NULL && buffersize > 0) {
958 This->fDirty = 1;
960 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset +
961 start * This->sInfo.dwSampleSize, SEEK_SET) == -1)
962 return AVIERR_FILEWRITE;
963 if (mmioWrite(This->hmmio, (HPSTR)buffer, buffersize) != buffersize)
964 return AVIERR_FILEWRITE;
966 This->sInfo.dwLength = max(This->sInfo.dwLength, (DWORD)start + samples);
967 This->ckData.cksize = max(This->ckData.cksize,
968 start * This->sInfo.dwSampleSize + buffersize);
970 /* fill out return parameters if given */
971 if (sampwritten != NULL)
972 *sampwritten = samples;
973 if (byteswritten != NULL)
974 *byteswritten = buffersize;
977 return AVIERR_OK;
980 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
981 LONG samples)
983 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
985 TRACE("(%p,%d,%d)\n", iface, start, samples);
987 /* check parameters */
988 if (start < 0 || samples < 0)
989 return AVIERR_BADPARAM;
991 /* Delete before start of stream? */
992 if ((DWORD)(start + samples) < This->sInfo.dwStart)
993 return AVIERR_OK;
995 /* Delete after end of stream? */
996 if ((DWORD)start > This->sInfo.dwLength)
997 return AVIERR_OK;
999 /* For the rest we need write permissions */
1000 if ((This->uMode & MMIO_RWMODE) == 0)
1001 return AVIERR_READONLY;
1003 if ((DWORD)(start + samples) >= This->sInfo.dwLength) {
1004 /* deletion at end */
1005 samples = This->sInfo.dwLength - start;
1006 This->sInfo.dwLength -= samples;
1007 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
1008 } else if ((DWORD)start <= This->sInfo.dwStart) {
1009 /* deletion at start */
1010 samples = This->sInfo.dwStart - start;
1011 start = This->sInfo.dwStart;
1012 This->ckData.dwDataOffset += samples * This->sInfo.dwSampleSize;
1013 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
1014 } else {
1015 /* deletion inside stream -- needs playlist and cue's */
1016 FIXME(": deletion inside of stream not supported!\n");
1018 return AVIERR_UNSUPPORTED;
1021 This->fDirty = 1;
1023 return AVIERR_OK;
1026 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1027 LPVOID lp, LPLONG lpread)
1029 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1031 assert(This->paf != NULL);
1033 return IAVIFile_ReadData((PAVIFILE)This->paf, fcc, lp, lpread);
1036 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1037 LPVOID lp, LONG size)
1039 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1041 return IAVIFile_WriteData((PAVIFILE)This->paf, fcc, lp, size);
1044 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1045 LPAVISTREAMINFOW info, LONG infolen)
1047 FIXME("(%p,%p,%d): stub\n", iface, info, infolen);
1049 return E_FAIL;
1052 /***********************************************************************/
1054 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1056 MMCKINFO ckRIFF;
1057 MMCKINFO ck;
1059 This->sInfo.dwLength = 0; /* just to be sure */
1060 This->fDirty = FALSE;
1062 /* search for RIFF chunk */
1063 ckRIFF.fccType = 0; /* find any */
1064 if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) {
1065 return AVIFILE_LoadSunFile(This);
1068 if (ckRIFF.fccType != formtypeWAVE)
1069 return AVIERR_BADFORMAT;
1071 /* search WAVE format chunk */
1072 ck.ckid = ckidWAVEFORMAT;
1073 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck,
1074 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
1075 return AVIERR_FILEREAD;
1077 /* get memory for format and read it */
1078 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
1079 if (This->lpFormat == NULL)
1080 return AVIERR_FILEREAD;
1081 This->cbFormat = ck.cksize;
1083 if (mmioRead(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize)
1084 return AVIERR_FILEREAD;
1085 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1086 return AVIERR_FILEREAD;
1088 /* Non-pcm formats have a fact chunk.
1089 * We don't need it, so simply add it to the extra chunks.
1092 /* find the big data chunk */
1093 This->ckData.ckid = ckidWAVEDATA;
1094 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &This->ckData,
1095 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
1096 return AVIERR_FILEREAD;
1098 memset(&This->sInfo, 0, sizeof(This->sInfo));
1099 This->sInfo.fccType = streamtypeAUDIO;
1100 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
1101 This->sInfo.dwSampleSize =
1102 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
1103 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign;
1104 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize;
1106 This->fInfo.dwStreams = 1;
1108 if (mmioAscend(This->hmmio, &This->ckData, 0) != S_OK) {
1109 /* seems to be truncated */
1110 WARN(": file seems to be truncated!\n");
1111 This->ckData.cksize = mmioSeek(This->hmmio, 0, SEEK_END) -
1112 This->ckData.dwDataOffset;
1113 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign;
1114 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize;
1117 /* ignore errors */
1118 FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck, &ckRIFF, 0);
1120 return AVIERR_OK;
1123 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This)
1125 SUNAUDIOHEADER auhdr;
1127 mmioSeek(This->hmmio, 0, SEEK_SET);
1128 if (mmioRead(This->hmmio, (HPSTR)&auhdr, sizeof(auhdr)) != sizeof(auhdr))
1129 return AVIERR_FILEREAD;
1131 if (auhdr.fccType == 0x0064732E) {
1132 /* header in little endian */
1133 This->ckData.dwDataOffset = LE2H_DWORD(auhdr.offset);
1134 This->ckData.cksize = LE2H_DWORD(auhdr.size);
1136 auhdr.encoding = LE2H_DWORD(auhdr.encoding);
1137 auhdr.sampleRate = LE2H_DWORD(auhdr.sampleRate);
1138 auhdr.channels = LE2H_DWORD(auhdr.channels);
1139 } else if (auhdr.fccType == mmioFOURCC('.','s','n','d')) {
1140 /* header in big endian */
1141 This->ckData.dwDataOffset = BE2H_DWORD(auhdr.offset);
1142 This->ckData.cksize = BE2H_DWORD(auhdr.size);
1144 auhdr.encoding = BE2H_DWORD(auhdr.encoding);
1145 auhdr.sampleRate = BE2H_DWORD(auhdr.sampleRate);
1146 auhdr.channels = BE2H_DWORD(auhdr.channels);
1147 } else
1148 return AVIERR_FILEREAD;
1150 if (auhdr.channels < 1)
1151 return AVIERR_BADFORMAT;
1153 /* get size of header */
1154 switch(auhdr.encoding) {
1155 case AU_ENCODING_ADPCM_G721_32:
1156 This->cbFormat = sizeof(G721_ADPCMWAVEFORMAT); break;
1157 case AU_ENCODING_ADPCM_G723_24:
1158 This->cbFormat = sizeof(G723_ADPCMWAVEFORMAT); break;
1159 case AU_ENCODING_ADPCM_G722:
1160 case AU_ENCODING_ADPCM_G723_5:
1161 WARN("unsupported Sun audio format %d\n", auhdr.encoding);
1162 return AVIERR_UNSUPPORTED; /* FIXME */
1163 default:
1164 This->cbFormat = sizeof(WAVEFORMATEX); break;
1167 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, This->cbFormat);
1168 if (This->lpFormat == NULL)
1169 return AVIERR_MEMORY;
1171 This->lpFormat->nChannels = auhdr.channels;
1172 This->lpFormat->nSamplesPerSec = auhdr.sampleRate;
1173 switch(auhdr.encoding) {
1174 case AU_ENCODING_ULAW_8:
1175 This->lpFormat->wFormatTag = WAVE_FORMAT_MULAW;
1176 This->lpFormat->wBitsPerSample = 8;
1177 break;
1178 case AU_ENCODING_PCM_8:
1179 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1180 This->lpFormat->wBitsPerSample = 8;
1181 break;
1182 case AU_ENCODING_PCM_16:
1183 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1184 This->lpFormat->wBitsPerSample = 16;
1185 break;
1186 case AU_ENCODING_PCM_24:
1187 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1188 This->lpFormat->wBitsPerSample = 24;
1189 break;
1190 case AU_ENCODING_PCM_32:
1191 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1192 This->lpFormat->wBitsPerSample = 32;
1193 break;
1194 case AU_ENCODING_ALAW_8:
1195 This->lpFormat->wFormatTag = WAVE_FORMAT_ALAW;
1196 This->lpFormat->wBitsPerSample = 8;
1197 break;
1198 case AU_ENCODING_ADPCM_G721_32:
1199 This->lpFormat->wFormatTag = WAVE_FORMAT_G721_ADPCM;
1200 This->lpFormat->wBitsPerSample = (3*5*8);
1201 This->lpFormat->nBlockAlign = 15*15*8;
1202 This->lpFormat->cbSize = sizeof(WORD);
1203 ((LPG721_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0;
1204 break;
1205 case AU_ENCODING_ADPCM_G723_24:
1206 This->lpFormat->wFormatTag = WAVE_FORMAT_G723_ADPCM;
1207 This->lpFormat->wBitsPerSample = (3*5*8);
1208 This->lpFormat->nBlockAlign = 15*15*8;
1209 This->lpFormat->cbSize = 2*sizeof(WORD);
1210 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->cbExtraSize = 0;
1211 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0;
1212 break;
1213 default:
1214 WARN("unsupported Sun audio format %d\n", auhdr.encoding);
1215 return AVIERR_UNSUPPORTED;
1218 This->lpFormat->nBlockAlign =
1219 (This->lpFormat->nChannels * This->lpFormat->wBitsPerSample) / 8;
1220 if (This->lpFormat->nBlockAlign == 0 && This->lpFormat->wBitsPerSample < 8)
1221 This->lpFormat->nBlockAlign++;
1222 This->lpFormat->nAvgBytesPerSec =
1223 This->lpFormat->nBlockAlign * This->lpFormat->nSamplesPerSec;
1225 This->fDirty = 0;
1227 This->sInfo.fccType = streamtypeAUDIO;
1228 This->sInfo.fccHandler = 0;
1229 This->sInfo.dwFlags = 0;
1230 This->sInfo.wPriority = 0;
1231 This->sInfo.wLanguage = 0;
1232 This->sInfo.dwInitialFrames = 0;
1233 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
1234 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
1235 This->sInfo.dwStart = 0;
1236 This->sInfo.dwLength =
1237 This->ckData.cksize / This->lpFormat->nBlockAlign;
1238 This->sInfo.dwSuggestedBufferSize = This->sInfo.dwLength;
1239 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign;
1241 This->fInfo.dwStreams = 1;
1242 This->fInfo.dwScale = 1;
1243 This->fInfo.dwRate = This->lpFormat->nSamplesPerSec;
1244 This->fInfo.dwLength =
1245 MulDiv(This->ckData.cksize, This->lpFormat->nSamplesPerSec,
1246 This->lpFormat->nAvgBytesPerSec);
1248 return AVIERR_OK;
1251 static HRESULT AVIFILE_SaveFile(const IAVIFileImpl *This)
1253 MMCKINFO ckRIFF;
1254 MMCKINFO ck;
1256 mmioSeek(This->hmmio, 0, SEEK_SET);
1258 /* create the RIFF chunk with formtype WAVE */
1259 ckRIFF.fccType = formtypeWAVE;
1260 ckRIFF.cksize = 0;
1261 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
1262 return AVIERR_FILEWRITE;
1264 /* the next chunk is the format */
1265 ck.ckid = ckidWAVEFORMAT;
1266 ck.cksize = This->cbFormat;
1267 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1268 return AVIERR_FILEWRITE;
1269 if (This->lpFormat != NULL && This->cbFormat > 0) {
1270 if (mmioWrite(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize)
1271 return AVIERR_FILEWRITE;
1273 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1274 return AVIERR_FILEWRITE;
1276 /* fact chunk is needed for non-pcm waveforms */
1277 if (This->lpFormat != NULL && This->cbFormat > sizeof(PCMWAVEFORMAT) &&
1278 This->lpFormat->wFormatTag != WAVE_FORMAT_PCM) {
1279 WAVEFORMATEX wfx;
1280 DWORD dwFactLength;
1281 HACMSTREAM has;
1283 /* try to open an appropriate audio codec to figure out
1284 * data for fact-chunk */
1285 wfx.wFormatTag = WAVE_FORMAT_PCM;
1286 if (acmFormatSuggest(NULL, This->lpFormat, &wfx,
1287 sizeof(wfx), ACM_FORMATSUGGESTF_WFORMATTAG)) {
1288 acmStreamOpen(&has, NULL, This->lpFormat, &wfx, NULL,
1289 0, 0, ACM_STREAMOPENF_NONREALTIME);
1290 acmStreamSize(has, This->ckData.cksize, &dwFactLength,
1291 ACM_STREAMSIZEF_SOURCE);
1292 dwFactLength /= wfx.nBlockAlign;
1293 acmStreamClose(has, 0);
1295 /* create the fact chunk */
1296 ck.ckid = ckidWAVEFACT;
1297 ck.cksize = sizeof(dwFactLength);
1299 /* test for enough space before data chunk */
1300 if (mmioSeek(This->hmmio, 0, SEEK_CUR) > This->ckData.dwDataOffset
1301 - ck.cksize - 4 * sizeof(DWORD))
1302 return AVIERR_FILEWRITE;
1303 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1304 return AVIERR_FILEWRITE;
1305 if (mmioWrite(This->hmmio, (HPSTR)&dwFactLength, ck.cksize) != ck.cksize)
1306 return AVIERR_FILEWRITE;
1307 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1308 return AVIERR_FILEWRITE;
1309 } else
1310 ERR(": fact chunk is needed for non-pcm files -- currently no codec found, so skipped!\n");
1313 /* if there was extra stuff, we need to fill it with JUNK */
1314 if (mmioSeek(This->hmmio, 0, SEEK_CUR) + 2 * sizeof(DWORD) < This->ckData.dwDataOffset) {
1315 ck.ckid = ckidAVIPADDING;
1316 ck.cksize = 0;
1317 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1318 return AVIERR_FILEWRITE;
1320 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset
1321 - 2 * sizeof(DWORD), SEEK_SET) == -1)
1322 return AVIERR_FILEWRITE;
1323 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1324 return AVIERR_FILEWRITE;
1327 /* create the data chunk */
1328 ck.ckid = ckidWAVEDATA;
1329 ck.cksize = This->ckData.cksize;
1330 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1331 return AVIERR_FILEWRITE;
1332 if (mmioSeek(This->hmmio, This->ckData.cksize, SEEK_CUR) == -1)
1333 return AVIERR_FILEWRITE;
1334 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1335 return AVIERR_FILEWRITE;
1337 /* some optional extra chunks? */
1338 if (This->extra.lp != NULL && This->extra.cb > 0) {
1339 /* chunk headers are already in structure */
1340 if (mmioWrite(This->hmmio, This->extra.lp, This->extra.cb) != This->extra.cb)
1341 return AVIERR_FILEWRITE;
1344 /* close RIFF chunk */
1345 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
1346 return AVIERR_FILEWRITE;
1347 if (mmioFlush(This->hmmio, 0) != S_OK)
1348 return AVIERR_FILEWRITE;
1350 return AVIERR_OK;