prepare fixing vfw.h header so it can be compile without include/wine again
[reactos.git] / reactos / lib / avifil32 / wavfile.c
1 /*
2 * Copyright 2002 Michael Günnewig
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #define COM_NO_WINDOWS_H
20 #include <assert.h>
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winnls.h"
28 #include "winerror.h"
29 #include "windowsx.h"
30 #include "mmsystem.h"
31 #include "mmreg.h"
32 #include "vfw.h"
33 #include "msacm.h"
34
35 #include "avifile_private.h"
36 #include "extrachunk.h"
37
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
42
43 /***********************************************************************/
44
45 #define formtypeWAVE mmioFOURCC('W','A','V','E')
46 #define ckidWAVEFORMAT mmioFOURCC('f','m','t',' ')
47 #define ckidWAVEFACT mmioFOURCC('f','a','c','t')
48 #define ckidWAVEDATA mmioFOURCC('d','a','t','a')
49
50 /***********************************************************************/
51
52 #define ENDIAN_SWAPWORD(x) ((((x) >> 8) & 0xFF) | (((x) & 0xFF) << 8))
53 #define ENDIAN_SWAPDWORD(x) (ENDIAN_SWAPWORD((x >> 16) & 0xFFFF) | \
54 ENDIAN_SWAPWORD(x & 0xFFFF) << 16)
55
56 #ifdef WORDS_BIGENDIAN
57 #define BE2H_WORD(x) (x)
58 #define BE2H_DWORD(x) (x)
59 #define LE2H_WORD(x) ENDIAN_SWAPWORD(x)
60 #define LE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
61 #else
62 #define BE2H_WORD(x) ENDIAN_SWAPWORD(x)
63 #define BE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
64 #define LE2H_WORD(x) (x)
65 #define LE2H_DWORD(x) (x)
66 #endif
67
68 typedef struct {
69 FOURCC fccType;
70 DWORD offset;
71 DWORD size;
72 INT encoding;
73 DWORD sampleRate;
74 DWORD channels;
75 } SUNAUDIOHEADER;
76
77 #define AU_ENCODING_ULAW_8 1
78 #define AU_ENCODING_PCM_8 2
79 #define AU_ENCODING_PCM_16 3
80 #define AU_ENCODING_PCM_24 4
81 #define AU_ENCODING_PCM_32 5
82 #define AU_ENCODING_FLOAT 6
83 #define AU_ENCODING_DOUBLE 7
84 #define AU_ENCODING_ADPCM_G721_32 23
85 #define AU_ENCODING_ADPCM_G722 24
86 #define AU_ENCODING_ADPCM_G723_24 25
87 #define AU_ENCODING_ADPCM_G723_5 26
88 #define AU_ENCODING_ALAW_8 27
89
90 /***********************************************************************/
91
92 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
93 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
94 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile* iface);
95 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
96 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
97 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
98 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
99 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
100 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
101 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
102
103 static const struct IAVIFileVtbl iwavft = {
104 IAVIFile_fnQueryInterface,
105 IAVIFile_fnAddRef,
106 IAVIFile_fnRelease,
107 IAVIFile_fnInfo,
108 IAVIFile_fnGetStream,
109 IAVIFile_fnCreateStream,
110 IAVIFile_fnWriteData,
111 IAVIFile_fnReadData,
112 IAVIFile_fnEndRecord,
113 IAVIFile_fnDeleteStream
114 };
115
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);
125
126 static const struct IPersistFileVtbl iwavpft = {
127 IPersistFile_fnQueryInterface,
128 IPersistFile_fnAddRef,
129 IPersistFile_fnRelease,
130 IPersistFile_fnGetClassID,
131 IPersistFile_fnIsDirty,
132 IPersistFile_fnLoad,
133 IPersistFile_fnSave,
134 IPersistFile_fnSaveCompleted,
135 IPersistFile_fnGetCurFile
136 };
137
138 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID *obj);
139 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream*iface);
140 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface);
141 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
142 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
143 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream*iface,LONG pos,LONG flags);
144 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG *formatsize);
145 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
146 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread);
147 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten);
148 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
149 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG *lpread);
150 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG size);
151 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
152
153 static const struct IAVIStreamVtbl iwavst = {
154 IAVIStream_fnQueryInterface,
155 IAVIStream_fnAddRef,
156 IAVIStream_fnRelease,
157 IAVIStream_fnCreate,
158 IAVIStream_fnInfo,
159 IAVIStream_fnFindSample,
160 IAVIStream_fnReadFormat,
161 IAVIStream_fnSetFormat,
162 IAVIStream_fnRead,
163 IAVIStream_fnWrite,
164 IAVIStream_fnDelete,
165 IAVIStream_fnReadData,
166 IAVIStream_fnWriteData,
167 IAVIStream_fnSetInfo
168 };
169
170 typedef struct _IAVIFileImpl IAVIFileImpl;
171
172 typedef struct _IPersistFileImpl {
173 /* IUnknown stuff */
174 const IPersistFileVtbl *lpVtbl;
175
176 /* IPersistFile stuff */
177 IAVIFileImpl *paf;
178 } IPersistFileImpl;
179
180 typedef struct _IAVIStreamImpl {
181 /* IUnknown stuff */
182 const IAVIStreamVtbl *lpVtbl;
183
184 /* IAVIStream stuff */
185 IAVIFileImpl *paf;
186 } IAVIStreamImpl;
187
188 struct _IAVIFileImpl {
189 /* IUnknown stuff */
190 const IAVIFileVtbl *lpVtbl;
191 LONG ref;
192
193 /* IAVIFile, IAVIStream stuff... */
194 IPersistFileImpl iPersistFile;
195 IAVIStreamImpl iAVIStream;
196
197 AVIFILEINFOW fInfo;
198 AVISTREAMINFOW sInfo;
199
200 LPWAVEFORMATEX lpFormat;
201 LONG cbFormat;
202
203 MMCKINFO ckData;
204
205 EXTRACHUNKS extra;
206
207 /* IPersistFile stuff ... */
208 HMMIO hmmio;
209 LPWSTR szFileName;
210 UINT uMode;
211 BOOL fDirty;
212 };
213
214 /***********************************************************************/
215
216 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
217 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This);
218 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
219
220 HRESULT AVIFILE_CreateWAVFile(REFIID riid, LPVOID *ppv)
221 {
222 IAVIFileImpl *pfile;
223 HRESULT hr;
224
225 assert(riid != NULL && ppv != NULL);
226
227 *ppv = NULL;
228
229 pfile = (IAVIFileImpl*)LocalAlloc(LPTR, sizeof(IAVIFileImpl));
230 if (pfile == NULL)
231 return AVIERR_MEMORY;
232
233 pfile->lpVtbl = &iwavft;
234 pfile->iPersistFile.lpVtbl = &iwavpft;
235 pfile->iAVIStream.lpVtbl = &iwavst;
236 pfile->ref = 0;
237 pfile->iPersistFile.paf = pfile;
238 pfile->iAVIStream.paf = pfile;
239
240 hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
241 if (FAILED(hr))
242 LocalFree((HLOCAL)pfile);
243
244 return hr;
245 }
246
247 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
248 LPVOID *obj)
249 {
250 IAVIFileImpl *This = (IAVIFileImpl *)iface;
251
252 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
253
254 if (IsEqualGUID(&IID_IUnknown, refiid) ||
255 IsEqualGUID(&IID_IAVIFile, refiid)) {
256 *obj = iface;
257 return S_OK;
258 } else if (This->fInfo.dwStreams == 1 &&
259 IsEqualGUID(&IID_IAVIStream, refiid)) {
260 *obj = &This->iAVIStream;
261 return S_OK;
262 } else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
263 *obj = &This->iPersistFile;
264 return S_OK;
265 }
266
267 return OLE_E_ENUM_NOMORE;
268 }
269
270 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
271 {
272 IAVIFileImpl *This = (IAVIFileImpl *)iface;
273
274 TRACE("(%p)\n",iface);
275
276 return InterlockedIncrement(&This->ref);
277 }
278
279 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
280 {
281 IAVIFileImpl *This = (IAVIFileImpl *)iface;
282 ULONG ref = InterlockedDecrement(&This->ref);
283
284 TRACE("(%p)\n",iface);
285
286 if (!ref) {
287 if (This->fDirty) {
288 /* need to write headers to file */
289 AVIFILE_SaveFile(This);
290 }
291
292 if (This->lpFormat != NULL) {
293 GlobalFreePtr(This->lpFormat);
294 This->lpFormat = NULL;
295 This->cbFormat = 0;
296 }
297 if (This->extra.lp != NULL) {
298 GlobalFreePtr(This->extra.lp);
299 This->extra.lp = NULL;
300 This->extra.cb = 0;
301 }
302 if (This->szFileName != NULL) {
303 LocalFree((HLOCAL)This->szFileName);
304 This->szFileName = NULL;
305 }
306 if (This->hmmio != NULL) {
307 mmioClose(This->hmmio, 0);
308 This->hmmio = NULL;
309 }
310
311 LocalFree((HLOCAL)This);
312 return 0;
313 }
314 return ref;
315 }
316
317 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
318 LONG size)
319 {
320 IAVIFileImpl *This = (IAVIFileImpl *)iface;
321
322 TRACE("(%p,%p,%ld)\n",iface,afi,size);
323
324 if (afi == NULL)
325 return AVIERR_BADPARAM;
326 if (size < 0)
327 return AVIERR_BADSIZE;
328
329 /* update file info */
330 This->fInfo.dwFlags = 0;
331 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
332 if (This->lpFormat != NULL) {
333 assert(This->sInfo.dwScale != 0);
334
335 This->fInfo.dwStreams = 1;
336 This->fInfo.dwScale = This->sInfo.dwScale;
337 This->fInfo.dwRate = This->sInfo.dwRate;
338 This->fInfo.dwLength = This->sInfo.dwLength;
339 This->fInfo.dwSuggestedBufferSize = This->ckData.cksize;
340 This->fInfo.dwMaxBytesPerSec =
341 MulDiv(This->sInfo.dwSampleSize,This->sInfo.dwRate,This->sInfo.dwScale);
342 }
343
344 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
345
346 if ((DWORD)size < sizeof(This->fInfo))
347 return AVIERR_BUFFERTOOSMALL;
348 return AVIERR_OK;
349 }
350
351 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
352 DWORD fccType, LONG lParam)
353 {
354 IAVIFileImpl *This = (IAVIFileImpl *)iface;
355
356 TRACE("(%p,%p,0x%08lX,%ld)\n", iface, avis, fccType, lParam);
357
358 /* check parameter */
359 if (avis == NULL)
360 return AVIERR_BADPARAM;
361
362 *avis = NULL;
363
364 /* Does our stream exists? */
365 if (lParam != 0 || This->fInfo.dwStreams == 0)
366 return AVIERR_NODATA;
367 if (fccType != 0 && fccType != streamtypeAUDIO)
368 return AVIERR_NODATA;
369
370 *avis = (PAVISTREAM)&This->iAVIStream;
371 IAVIFile_AddRef(iface);
372
373 return AVIERR_OK;
374 }
375
376 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
377 LPAVISTREAMINFOW asi)
378 {
379 IAVIFileImpl *This = (IAVIFileImpl *)iface;
380
381 TRACE("(%p,%p,%p)\n", iface, avis, asi);
382
383 /* check parameters */
384 if (avis == NULL || asi == NULL)
385 return AVIERR_BADPARAM;
386
387 *avis = NULL;
388
389 /* We only support one audio stream */
390 if (This->fInfo.dwStreams != 0 || This->lpFormat != NULL)
391 return AVIERR_UNSUPPORTED;
392 if (asi->fccType != streamtypeAUDIO)
393 return AVIERR_UNSUPPORTED;
394
395 /* Does the user have write permission? */
396 if ((This->uMode & MMIO_RWMODE) == 0)
397 return AVIERR_READONLY;
398
399 This->cbFormat = 0;
400 This->lpFormat = NULL;
401
402 memcpy(&This->sInfo, asi, sizeof(This->sInfo));
403
404 /* make sure streaminfo if okay for us */
405 This->sInfo.fccHandler = 0;
406 This->sInfo.dwFlags = 0;
407 This->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
408 This->sInfo.dwStart = 0;
409 This->sInfo.dwInitialFrames = 0;
410 This->sInfo.dwFormatChangeCount = 0;
411 memset(&This->sInfo.rcFrame, 0, sizeof(This->sInfo.rcFrame));
412
413 This->fInfo.dwStreams = 1;
414 This->fInfo.dwScale = This->sInfo.dwScale;
415 This->fInfo.dwRate = This->sInfo.dwRate;
416 This->fInfo.dwLength = This->sInfo.dwLength;
417
418 This->ckData.dwDataOffset = 0;
419 This->ckData.cksize = 0;
420
421 *avis = (PAVISTREAM)&This->iAVIStream;
422 IAVIFile_AddRef(iface);
423
424 return AVIERR_OK;
425 }
426
427 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
428 LPVOID lpData, LONG size)
429 {
430 IAVIFileImpl *This = (IAVIFileImpl *)iface;
431
432 TRACE("(%p,0x%08lX,%p,%ld)\n", iface, ckid, lpData, size);
433
434 /* check parameters */
435 if (lpData == NULL)
436 return AVIERR_BADPARAM;
437 if (size < 0)
438 return AVIERR_BADSIZE;
439
440 /* Do we have write permission? */
441 if ((This->uMode & MMIO_RWMODE) == 0)
442 return AVIERR_READONLY;
443
444 This->fDirty = TRUE;
445
446 return WriteExtraChunk(&This->extra, ckid, lpData, size);
447 }
448
449 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
450 LPVOID lpData, LONG *size)
451 {
452 IAVIFileImpl *This = (IAVIFileImpl *)iface;
453
454 TRACE("(%p,0x%08lX,%p,%p)\n", iface, ckid, lpData, size);
455
456 return ReadExtraChunk(&This->extra, ckid, lpData, size);
457 }
458
459 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
460 {
461 TRACE("(%p)\n",iface);
462
463 /* This is only needed for interleaved files.
464 * We have only one stream, which can't be interleaved.
465 */
466 return AVIERR_OK;
467 }
468
469 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
470 LONG lParam)
471 {
472 IAVIFileImpl *This = (IAVIFileImpl *)iface;
473
474 TRACE("(%p,0x%08lX,%ld)\n", iface, fccType, lParam);
475
476 /* check parameter */
477 if (lParam < 0)
478 return AVIERR_BADPARAM;
479
480 /* Do we have our audio stream? */
481 if (lParam != 0 || This->fInfo.dwStreams == 0 ||
482 (fccType != 0 && fccType != streamtypeAUDIO))
483 return AVIERR_NODATA;
484
485 /* Have user write permissions? */
486 if ((This->uMode & MMIO_RWMODE) == 0)
487 return AVIERR_READONLY;
488
489 GlobalFreePtr(This->lpFormat);
490 This->lpFormat = NULL;
491 This->cbFormat = 0;
492
493 /* update infos */
494 This->ckData.dwDataOffset = 0;
495 This->ckData.cksize = 0;
496
497 This->sInfo.dwScale = 0;
498 This->sInfo.dwRate = 0;
499 This->sInfo.dwLength = 0;
500 This->sInfo.dwSuggestedBufferSize = 0;
501
502 This->fInfo.dwStreams = 0;
503 This->fInfo.dwEditCount++;
504
505 This->fDirty = TRUE;
506
507 return AVIERR_OK;
508 }
509
510 /***********************************************************************/
511
512 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
513 REFIID refiid, LPVOID *obj)
514 {
515 IPersistFileImpl *This = (IPersistFileImpl *)iface;
516
517 assert(This->paf != NULL);
518
519 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
520 }
521
522 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
523 {
524 IPersistFileImpl *This = (IPersistFileImpl *)iface;
525
526 assert(This->paf != NULL);
527
528 return IAVIFile_AddRef((PAVIFILE)This->paf);
529 }
530
531 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface)
532 {
533 IPersistFileImpl *This = (IPersistFileImpl *)iface;
534
535 assert(This->paf != NULL);
536
537 return IAVIFile_Release((PAVIFILE)This->paf);
538 }
539
540 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
541 LPCLSID pClassID)
542 {
543 TRACE("(%p,%p)\n", iface, pClassID);
544
545 if (pClassID == NULL)
546 return AVIERR_BADPARAM;
547
548 memcpy(pClassID, &CLSID_WAVFile, sizeof(CLSID_WAVFile));
549
550 return AVIERR_OK;
551 }
552
553 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
554 {
555 IPersistFileImpl *This = (IPersistFileImpl *)iface;
556
557 TRACE("(%p)\n", iface);
558
559 assert(This->paf != NULL);
560
561 return (This->paf->fDirty ? S_OK : S_FALSE);
562 }
563
564 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
565 LPCOLESTR pszFileName, DWORD dwMode)
566 {
567 IAVIFileImpl *This = ((IPersistFileImpl*)iface)->paf;
568
569 WCHAR wszStreamFmt[50];
570 INT len;
571
572 TRACE("(%p,%s,0x%08lX)\n", iface, debugstr_w(pszFileName), dwMode);
573
574 /* check parameter */
575 if (pszFileName == NULL)
576 return AVIERR_BADPARAM;
577
578 assert(This != NULL);
579 if (This->hmmio != NULL)
580 return AVIERR_ERROR; /* No reuse of this object for another file! */
581
582 /* remeber mode and name */
583 This->uMode = dwMode;
584
585 len = lstrlenW(pszFileName) + 1;
586 This->szFileName = LocalAlloc(LPTR, len * sizeof(WCHAR));
587 if (This->szFileName == NULL)
588 return AVIERR_MEMORY;
589 lstrcpyW(This->szFileName, pszFileName);
590
591 /* try to open the file */
592 This->hmmio = mmioOpenW(This->szFileName, NULL, MMIO_ALLOCBUF | dwMode);
593 if (This->hmmio == NULL) {
594 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
595 LPSTR szFileName;
596 len = WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1,
597 NULL, 0, NULL, NULL);
598 szFileName = LocalAlloc(LPTR, len * sizeof(CHAR));
599 if (szFileName == NULL)
600 return AVIERR_MEMORY;
601
602 WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1, szFileName,
603 len, NULL, NULL);
604
605 This->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
606 LocalFree((HLOCAL)szFileName);
607 if (This->hmmio == NULL)
608 return AVIERR_FILEOPEN;
609 }
610
611 memset(& This->fInfo, 0, sizeof(This->fInfo));
612 memset(& This->sInfo, 0, sizeof(This->sInfo));
613
614 LoadStringW(AVIFILE_hModule, IDS_WAVEFILETYPE, This->fInfo.szFileType,
615 sizeof(This->fInfo.szFileType));
616 if (LoadStringW(AVIFILE_hModule, IDS_WAVESTREAMFORMAT,
617 wszStreamFmt, sizeof(wszStreamFmt)) > 0) {
618 wsprintfW(This->sInfo.szName, wszStreamFmt,
619 AVIFILE_BasenameW(This->szFileName));
620 }
621
622 /* should we create a new file? */
623 if (dwMode & OF_CREATE) {
624 /* nothing more to do */
625 return AVIERR_OK;
626 } else
627 return AVIFILE_LoadFile(This);
628 }
629
630 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
631 LPCOLESTR pszFileName,BOOL fRemember)
632 {
633 TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
634
635 /* We write directly to disk, so nothing to do. */
636
637 return AVIERR_OK;
638 }
639
640 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
641 LPCOLESTR pszFileName)
642 {
643 TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
644
645 /* We write directly to disk, so nothing to do. */
646
647 return AVIERR_OK;
648 }
649
650 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
651 LPOLESTR *ppszFileName)
652 {
653 IPersistFileImpl *This = (IPersistFileImpl *)iface;
654
655 TRACE("(%p,%p)\n", iface, ppszFileName);
656
657 if (ppszFileName == NULL)
658 return AVIERR_BADPARAM;
659
660 *ppszFileName = NULL;
661
662 assert(This->paf != NULL);
663
664 if (This->paf->szFileName != NULL) {
665 int len = lstrlenW(This->paf->szFileName) + 1;
666
667 *ppszFileName = GlobalAllocPtr(GHND, len * sizeof(WCHAR));
668 if (*ppszFileName == NULL)
669 return AVIERR_MEMORY;
670
671 strcpyW(*ppszFileName, This->paf->szFileName);
672 }
673
674 return AVIERR_OK;
675 }
676
677 /***********************************************************************/
678
679 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
680 REFIID refiid, LPVOID *obj)
681 {
682 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
683
684 assert(This->paf != NULL);
685
686 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
687 }
688
689 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
690 {
691 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
692
693 assert(This->paf != NULL);
694
695 return IAVIFile_AddRef((PAVIFILE)This->paf);
696 }
697
698 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
699 {
700 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
701
702 assert(This->paf != NULL);
703
704 return IAVIFile_Release((PAVIFILE)This->paf);
705 }
706
707 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
708 LPARAM lParam2)
709 {
710 TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
711
712 /* This IAVIStream interface needs an WAVFile */
713 return AVIERR_UNSUPPORTED;
714 }
715
716 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
717 LONG size)
718 {
719 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
720
721 TRACE("(%p,%p,%ld)\n", iface, psi, size);
722
723 if (psi == NULL)
724 return AVIERR_BADPARAM;
725 if (size < 0)
726 return AVIERR_BADSIZE;
727
728 memcpy(psi, &This->paf->sInfo, min((DWORD)size, sizeof(This->paf->sInfo)));
729
730 if ((DWORD)size < sizeof(This->paf->sInfo))
731 return AVIERR_BUFFERTOOSMALL;
732 return AVIERR_OK;
733 }
734
735 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
736 LONG flags)
737 {
738 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
739
740 TRACE("(%p,%ld,0x%08lX)\n",iface,pos,flags);
741
742 /* Do we have data? */
743 if (This->lpFormat == NULL)
744 return -1;
745
746 /* We don't have an index */
747 if (flags & FIND_INDEX)
748 return -1;
749
750 if (flags & FIND_FROM_START) {
751 pos = This->sInfo.dwStart;
752 flags &= ~(FIND_FROM_START|FIND_PREV);
753 flags |= FIND_NEXT;
754 }
755
756 if (flags & FIND_FORMAT) {
757 if ((flags & FIND_NEXT) && pos > 0)
758 pos = -1;
759 else
760 pos = 0;
761 }
762
763 if ((flags & FIND_RET) == FIND_LENGTH ||
764 (flags & FIND_RET) == FIND_SIZE)
765 return This->sInfo.dwSampleSize;
766 if ((flags & FIND_RET) == FIND_OFFSET)
767 return This->ckData.dwDataOffset + pos * This->sInfo.dwSampleSize;
768
769 return pos;
770 }
771
772 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
773 LPVOID format, LONG *formatsize)
774 {
775 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
776
777 TRACE("(%p,%ld,%p,%p)\n", iface, pos, format, formatsize);
778
779 if (formatsize == NULL)
780 return AVIERR_BADPARAM;
781
782 /* only interested in needed buffersize? */
783 if (format == NULL || *formatsize <= 0) {
784 *formatsize = This->paf->cbFormat;
785
786 return AVIERR_OK;
787 }
788
789 /* copy initial format (only as much as will fit) */
790 memcpy(format, This->paf->lpFormat, min(*formatsize, This->paf->cbFormat));
791 if (*formatsize < This->paf->cbFormat) {
792 *formatsize = This->paf->cbFormat;
793 return AVIERR_BUFFERTOOSMALL;
794 }
795
796 *formatsize = This->paf->cbFormat;
797 return AVIERR_OK;
798 }
799
800 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
801 LPVOID format, LONG formatsize)
802 {
803 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
804
805 TRACE("(%p,%ld,%p,%ld)\n", iface, pos, format, formatsize);
806
807 /* check parameters */
808 if (format == NULL || formatsize <= sizeof(PCMWAVEFORMAT))
809 return AVIERR_BADPARAM;
810
811 /* We can only do this to an empty wave file, but ignore call
812 * if still same format */
813 if (This->lpFormat != NULL) {
814 if (formatsize != This->cbFormat ||
815 memcmp(format, This->lpFormat, formatsize) != 0)
816 return AVIERR_UNSUPPORTED;
817
818 return AVIERR_OK;
819 }
820
821 /* only support start at position 0 */
822 if (pos != 0)
823 return AVIERR_UNSUPPORTED;
824
825 /* Do we have write permission? */
826 if ((This->uMode & MMIO_RWMODE) == 0)
827 return AVIERR_READONLY;
828
829 /* get memory for format and copy it */
830 This->lpFormat = (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, formatsize);
831 if (This->lpFormat == NULL)
832 return AVIERR_MEMORY;
833
834 This->cbFormat = formatsize;
835 memcpy(This->lpFormat, format, formatsize);
836
837 /* update info's about 'data' chunk */
838 This->ckData.dwDataOffset = formatsize + 7 * sizeof(DWORD);
839 This->ckData.cksize = 0;
840
841 /* for non-pcm format we need also a 'fact' chunk */
842 if (This->lpFormat->wFormatTag != WAVE_FORMAT_PCM)
843 This->ckData.dwDataOffset += 3 * sizeof(DWORD);
844
845 /* update stream and file info */
846 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign;
847 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
848 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
849 This->sInfo.dwLength = 0;
850 This->sInfo.dwSuggestedBufferSize = 0;
851
852 return AVIERR_OK;
853 }
854
855 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
856 LONG samples, LPVOID buffer,
857 LONG buffersize, LPLONG bytesread,
858 LPLONG samplesread)
859 {
860 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
861
862 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface, start, samples, buffer,
863 buffersize, bytesread, samplesread);
864
865 /* clear return parameters if given */
866 if (bytesread != NULL)
867 *bytesread = 0;
868 if (samplesread != NULL)
869 *samplesread = 0;
870
871 /* positions without data */
872 if (start < 0 || (DWORD)start > This->sInfo.dwLength)
873 return AVIERR_OK;
874
875 /* check samples */
876 if (samples < 0)
877 samples = 0;
878 if (buffersize > 0) {
879 if (samples > 0)
880 samples = min((DWORD)samples, buffersize / This->sInfo.dwSampleSize);
881 else
882 samples = buffersize / This->sInfo.dwSampleSize;
883 }
884
885 /* limit to end of stream */
886 if ((DWORD)(start + samples) > This->sInfo.dwLength)
887 samples = This->sInfo.dwLength - start;
888
889 /* request only the sizes? */
890 if (buffer == NULL || buffersize <= 0) {
891 /* then I need at least one parameter for it */
892 if (bytesread == NULL && samplesread == NULL)
893 return AVIERR_BADPARAM;
894
895 if (bytesread != NULL)
896 *bytesread = samples * This->sInfo.dwSampleSize;
897 if (samplesread != NULL)
898 *samplesread = samples;
899
900 return AVIERR_OK;
901 }
902
903 /* nothing to read? */
904 if (samples == 0)
905 return AVIERR_OK;
906
907 /* Can I read at least one sample? */
908 if ((DWORD)buffersize < This->sInfo.dwSampleSize)
909 return AVIERR_BUFFERTOOSMALL;
910
911 buffersize = samples * This->sInfo.dwSampleSize;
912
913 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset
914 + start * This->sInfo.dwSampleSize, SEEK_SET) == -1)
915 return AVIERR_FILEREAD;
916 if (mmioRead(This->hmmio, (HPSTR)buffer, buffersize) != buffersize)
917 return AVIERR_FILEREAD;
918
919 /* fill out return parameters if given */
920 if (bytesread != NULL)
921 *bytesread = buffersize;
922 if (samplesread != NULL)
923 *samplesread = samples;
924
925 return AVIERR_OK;
926 }
927
928 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start,
929 LONG samples, LPVOID buffer,
930 LONG buffersize, DWORD flags,
931 LPLONG sampwritten,
932 LPLONG byteswritten)
933 {
934 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
935
936 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface, start, samples,
937 buffer, buffersize, flags, sampwritten, byteswritten);
938
939 /* clear return parameters if given */
940 if (sampwritten != NULL)
941 *sampwritten = 0;
942 if (byteswritten != NULL)
943 *byteswritten = 0;
944
945 /* check parameters */
946 if (buffer == NULL && (buffersize > 0 || samples > 0))
947 return AVIERR_BADPARAM;
948
949 /* Do we have write permission? */
950 if ((This->uMode & MMIO_RWMODE) == 0)
951 return AVIERR_READONLY;
952
953 /* < 0 means "append" */
954 if (start < 0)
955 start = This->sInfo.dwStart + This->sInfo.dwLength;
956
957 /* check buffersize -- must multiple of samplesize */
958 if (buffersize & ~(This->sInfo.dwSampleSize - 1))
959 return AVIERR_BADSIZE;
960
961 /* do we have anything to write? */
962 if (buffer != NULL && buffersize > 0) {
963 This->fDirty = 1;
964
965 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset +
966 start * This->sInfo.dwSampleSize, SEEK_SET) == -1)
967 return AVIERR_FILEWRITE;
968 if (mmioWrite(This->hmmio, (HPSTR)buffer, buffersize) != buffersize)
969 return AVIERR_FILEWRITE;
970
971 This->sInfo.dwLength = max(This->sInfo.dwLength, (DWORD)start + samples);
972 This->ckData.cksize = max(This->ckData.cksize,
973 start * This->sInfo.dwSampleSize + buffersize);
974
975 /* fill out return parameters if given */
976 if (sampwritten != NULL)
977 *sampwritten = samples;
978 if (byteswritten != NULL)
979 *byteswritten = buffersize;
980 }
981
982 return AVIERR_OK;
983 }
984
985 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
986 LONG samples)
987 {
988 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
989
990 TRACE("(%p,%ld,%ld)\n", iface, start, samples);
991
992 /* check parameters */
993 if (start < 0 || samples < 0)
994 return AVIERR_BADPARAM;
995
996 /* Delete before start of stream? */
997 if ((DWORD)(start + samples) < This->sInfo.dwStart)
998 return AVIERR_OK;
999
1000 /* Delete after end of stream? */
1001 if ((DWORD)start > This->sInfo.dwLength)
1002 return AVIERR_OK;
1003
1004 /* For the rest we need write permissions */
1005 if ((This->uMode & MMIO_RWMODE) == 0)
1006 return AVIERR_READONLY;
1007
1008 if ((DWORD)(start + samples) >= This->sInfo.dwLength) {
1009 /* deletion at end */
1010 samples = This->sInfo.dwLength - start;
1011 This->sInfo.dwLength -= samples;
1012 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
1013 } else if ((DWORD)start <= This->sInfo.dwStart) {
1014 /* deletion at start */
1015 samples = This->sInfo.dwStart - start;
1016 start = This->sInfo.dwStart;
1017 This->ckData.dwDataOffset += samples * This->sInfo.dwSampleSize;
1018 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
1019 } else {
1020 /* deletion inside stream -- needs playlist and cue's */
1021 FIXME(": deletion inside of stream not supported!\n");
1022
1023 return AVIERR_UNSUPPORTED;
1024 }
1025
1026 This->fDirty = 1;
1027
1028 return AVIERR_OK;
1029 }
1030
1031 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1032 LPVOID lp, LPLONG lpread)
1033 {
1034 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1035
1036 assert(This->paf != NULL);
1037
1038 return IAVIFile_ReadData((PAVIFILE)This->paf, fcc, lp, lpread);
1039 }
1040
1041 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1042 LPVOID lp, LONG size)
1043 {
1044 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
1045
1046 return IAVIFile_WriteData((PAVIFILE)This->paf, fcc, lp, size);
1047 }
1048
1049 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1050 LPAVISTREAMINFOW info, LONG infolen)
1051 {
1052 FIXME("(%p,%p,%ld): stub\n", iface, info, infolen);
1053
1054 return E_FAIL;
1055 }
1056
1057 /***********************************************************************/
1058
1059 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1060 {
1061 MMCKINFO ckRIFF;
1062 MMCKINFO ck;
1063
1064 This->sInfo.dwLength = 0; /* just to be sure */
1065 This->fDirty = FALSE;
1066
1067 /* search for RIFF chunk */
1068 ckRIFF.fccType = 0; /* find any */
1069 if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) {
1070 return AVIFILE_LoadSunFile(This);
1071 }
1072
1073 if (ckRIFF.fccType != formtypeWAVE)
1074 return AVIERR_BADFORMAT;
1075
1076 /* search WAVE format chunk */
1077 ck.ckid = ckidWAVEFORMAT;
1078 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck,
1079 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
1080 return AVIERR_FILEREAD;
1081
1082 /* get memory for format and read it */
1083 This->lpFormat = (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, ck.cksize);
1084 if (This->lpFormat == NULL)
1085 return AVIERR_FILEREAD;
1086 This->cbFormat = ck.cksize;
1087
1088 if (mmioRead(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize)
1089 return AVIERR_FILEREAD;
1090 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1091 return AVIERR_FILEREAD;
1092
1093 /* Non-pcm formats have a fact chunk.
1094 * We don't need it, so simply add it to the extra chunks.
1095 */
1096
1097 /* find the big data chunk */
1098 This->ckData.ckid = ckidWAVEDATA;
1099 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &This->ckData,
1100 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
1101 return AVIERR_FILEREAD;
1102
1103 memset(&This->sInfo, 0, sizeof(This->sInfo));
1104 This->sInfo.fccType = streamtypeAUDIO;
1105 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
1106 This->sInfo.dwSampleSize =
1107 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
1108 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign;
1109 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize;
1110
1111 This->fInfo.dwStreams = 1;
1112
1113 if (mmioAscend(This->hmmio, &This->ckData, 0) != S_OK) {
1114 /* seems to be truncated */
1115 WARN(": file seems to be truncated!\n");
1116 This->ckData.cksize = mmioSeek(This->hmmio, 0, SEEK_END) -
1117 This->ckData.dwDataOffset;
1118 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign;
1119 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize;
1120 }
1121
1122 /* ignore errors */
1123 FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck, &ckRIFF, 0);
1124
1125 return AVIERR_OK;
1126 }
1127
1128 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This)
1129 {
1130 SUNAUDIOHEADER auhdr;
1131
1132 mmioSeek(This->hmmio, 0, SEEK_SET);
1133 if (mmioRead(This->hmmio, (HPSTR)&auhdr, sizeof(auhdr)) != sizeof(auhdr))
1134 return AVIERR_FILEREAD;
1135
1136 if (auhdr.fccType == 0x0064732E) {
1137 /* header in little endian */
1138 This->ckData.dwDataOffset = LE2H_DWORD(auhdr.offset);
1139 This->ckData.cksize = LE2H_DWORD(auhdr.size);
1140
1141 auhdr.encoding = LE2H_DWORD(auhdr.encoding);
1142 auhdr.sampleRate = LE2H_DWORD(auhdr.sampleRate);
1143 auhdr.channels = LE2H_DWORD(auhdr.channels);
1144 } else if (auhdr.fccType == mmioFOURCC('.','s','n','d')) {
1145 /* header in big endian */
1146 This->ckData.dwDataOffset = BE2H_DWORD(auhdr.offset);
1147 This->ckData.cksize = BE2H_DWORD(auhdr.size);
1148
1149 auhdr.encoding = BE2H_DWORD(auhdr.encoding);
1150 auhdr.sampleRate = BE2H_DWORD(auhdr.sampleRate);
1151 auhdr.channels = BE2H_DWORD(auhdr.channels);
1152 } else
1153 return AVIERR_FILEREAD;
1154
1155 if (auhdr.channels < 1)
1156 return AVIERR_BADFORMAT;
1157
1158 /* get size of header */
1159 switch(auhdr.encoding) {
1160 case AU_ENCODING_ADPCM_G721_32:
1161 This->cbFormat = sizeof(G721_ADPCMWAVEFORMAT); break;
1162 case AU_ENCODING_ADPCM_G723_24:
1163 This->cbFormat = sizeof(G723_ADPCMWAVEFORMAT); break;
1164 case AU_ENCODING_ADPCM_G722:
1165 case AU_ENCODING_ADPCM_G723_5:
1166 WARN("unsupported Sun audio format %d\n", auhdr.encoding);
1167 return AVIERR_UNSUPPORTED; /* FIXME */
1168 default:
1169 This->cbFormat = sizeof(WAVEFORMATEX); break;
1170 };
1171
1172 This->lpFormat =
1173 (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, This->cbFormat);
1174 if (This->lpFormat == NULL)
1175 return AVIERR_MEMORY;
1176
1177 This->lpFormat->nChannels = auhdr.channels;
1178 This->lpFormat->nSamplesPerSec = auhdr.sampleRate;
1179 switch(auhdr.encoding) {
1180 case AU_ENCODING_ULAW_8:
1181 This->lpFormat->wFormatTag = WAVE_FORMAT_MULAW;
1182 This->lpFormat->wBitsPerSample = 8;
1183 break;
1184 case AU_ENCODING_PCM_8:
1185 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1186 This->lpFormat->wBitsPerSample = 8;
1187 break;
1188 case AU_ENCODING_PCM_16:
1189 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1190 This->lpFormat->wBitsPerSample = 16;
1191 break;
1192 case AU_ENCODING_PCM_24:
1193 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1194 This->lpFormat->wBitsPerSample = 24;
1195 break;
1196 case AU_ENCODING_PCM_32:
1197 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM;
1198 This->lpFormat->wBitsPerSample = 32;
1199 break;
1200 case AU_ENCODING_ALAW_8:
1201 This->lpFormat->wFormatTag = WAVE_FORMAT_ALAW;
1202 This->lpFormat->wBitsPerSample = 8;
1203 break;
1204 case AU_ENCODING_ADPCM_G721_32:
1205 This->lpFormat->wFormatTag = WAVE_FORMAT_G721_ADPCM;
1206 This->lpFormat->wBitsPerSample = (3*5*8);
1207 This->lpFormat->nBlockAlign = 15*15*8;
1208 This->lpFormat->cbSize = sizeof(WORD);
1209 ((LPG721_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0;
1210 break;
1211 case AU_ENCODING_ADPCM_G723_24:
1212 This->lpFormat->wFormatTag = WAVE_FORMAT_G723_ADPCM;
1213 This->lpFormat->wBitsPerSample = (3*5*8);
1214 This->lpFormat->nBlockAlign = 15*15*8;
1215 This->lpFormat->cbSize = 2*sizeof(WORD);
1216 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->cbExtraSize = 0;
1217 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0;
1218 break;
1219 default:
1220 WARN("unsupported Sun audio format %d\n", auhdr.encoding);
1221 return AVIERR_UNSUPPORTED;
1222 };
1223
1224 This->lpFormat->nBlockAlign =
1225 (This->lpFormat->nChannels * This->lpFormat->wBitsPerSample) / 8;
1226 if (This->lpFormat->nBlockAlign == 0 && This->lpFormat->wBitsPerSample < 8)
1227 This->lpFormat->nBlockAlign++;
1228 This->lpFormat->nAvgBytesPerSec =
1229 This->lpFormat->nBlockAlign * This->lpFormat->nSamplesPerSec;
1230
1231 This->fDirty = 0;
1232
1233 This->sInfo.fccType = streamtypeAUDIO;
1234 This->sInfo.fccHandler = 0;
1235 This->sInfo.dwFlags = 0;
1236 This->sInfo.wPriority = 0;
1237 This->sInfo.wLanguage = 0;
1238 This->sInfo.dwInitialFrames = 0;
1239 This->sInfo.dwScale = This->lpFormat->nBlockAlign;
1240 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec;
1241 This->sInfo.dwStart = 0;
1242 This->sInfo.dwLength =
1243 This->ckData.cksize / This->lpFormat->nBlockAlign;
1244 This->sInfo.dwSuggestedBufferSize = This->sInfo.dwLength;
1245 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign;
1246
1247 This->fInfo.dwStreams = 1;
1248 This->fInfo.dwScale = 1;
1249 This->fInfo.dwRate = This->lpFormat->nSamplesPerSec;
1250 This->fInfo.dwLength =
1251 MulDiv(This->ckData.cksize, This->lpFormat->nSamplesPerSec,
1252 This->lpFormat->nAvgBytesPerSec);
1253
1254 return AVIERR_OK;
1255 }
1256
1257 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
1258 {
1259 MMCKINFO ckRIFF;
1260 MMCKINFO ck;
1261
1262 mmioSeek(This->hmmio, 0, SEEK_SET);
1263
1264 /* create the RIFF chunk with formtype WAVE */
1265 ckRIFF.fccType = formtypeWAVE;
1266 ckRIFF.cksize = 0;
1267 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK)
1268 return AVIERR_FILEWRITE;
1269
1270 /* the next chunk is the format */
1271 ck.ckid = ckidWAVEFORMAT;
1272 ck.cksize = This->cbFormat;
1273 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1274 return AVIERR_FILEWRITE;
1275 if (This->lpFormat != NULL && This->cbFormat > 0) {
1276 if (mmioWrite(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize)
1277 return AVIERR_FILEWRITE;
1278 }
1279 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1280 return AVIERR_FILEWRITE;
1281
1282 /* fact chunk is needed for non-pcm waveforms */
1283 if (This->lpFormat != NULL && This->cbFormat > sizeof(PCMWAVEFORMAT) &&
1284 This->lpFormat->wFormatTag != WAVE_FORMAT_PCM) {
1285 WAVEFORMATEX wfx;
1286 DWORD dwFactLength;
1287 HACMSTREAM has;
1288
1289 /* try to open an appropriate audio codec to figure out
1290 * data for fact-chunk */
1291 wfx.wFormatTag = WAVE_FORMAT_PCM;
1292 if (acmFormatSuggest(NULL, This->lpFormat, &wfx,
1293 sizeof(wfx), ACM_FORMATSUGGESTF_WFORMATTAG)) {
1294 acmStreamOpen(&has, NULL, This->lpFormat, &wfx, NULL,
1295 0, 0, ACM_STREAMOPENF_NONREALTIME);
1296 acmStreamSize(has, This->ckData.cksize, &dwFactLength,
1297 ACM_STREAMSIZEF_SOURCE);
1298 dwFactLength /= wfx.nBlockAlign;
1299 acmStreamClose(has, 0);
1300
1301 /* create the fact chunk */
1302 ck.ckid = ckidWAVEFACT;
1303 ck.cksize = sizeof(dwFactLength);
1304
1305 /* test for enough space before data chunk */
1306 if (mmioSeek(This->hmmio, 0, SEEK_CUR) > This->ckData.dwDataOffset
1307 - ck.cksize - 4 * sizeof(DWORD))
1308 return AVIERR_FILEWRITE;
1309 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1310 return AVIERR_FILEWRITE;
1311 if (mmioWrite(This->hmmio, (HPSTR)&dwFactLength, ck.cksize) != ck.cksize)
1312 return AVIERR_FILEWRITE;
1313 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1314 return AVIERR_FILEWRITE;
1315 } else
1316 ERR(": fact chunk is needed for non-pcm files -- currently no codec found, so skipped!\n");
1317 }
1318
1319 /* if there was extra stuff, we need to fill it with JUNK */
1320 if (mmioSeek(This->hmmio, 0, SEEK_CUR) + 2 * sizeof(DWORD) < This->ckData.dwDataOffset) {
1321 ck.ckid = ckidAVIPADDING;
1322 ck.cksize = 0;
1323 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1324 return AVIERR_FILEWRITE;
1325
1326 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset
1327 - 2 * sizeof(DWORD), SEEK_SET) == -1)
1328 return AVIERR_FILEWRITE;
1329 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1330 return AVIERR_FILEWRITE;
1331 }
1332
1333 /* create the data chunk */
1334 ck.ckid = ckidWAVEDATA;
1335 ck.cksize = This->ckData.cksize;
1336 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK)
1337 return AVIERR_FILEWRITE;
1338 if (mmioSeek(This->hmmio, This->ckData.cksize, SEEK_CUR) == -1)
1339 return AVIERR_FILEWRITE;
1340 if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1341 return AVIERR_FILEWRITE;
1342
1343 /* some optional extra chunks? */
1344 if (This->extra.lp != NULL && This->extra.cb > 0) {
1345 /* chunk headers are already in structure */
1346 if (mmioWrite(This->hmmio, This->extra.lp, This->extra.cb) != This->extra.cb)
1347 return AVIERR_FILEWRITE;
1348 }
1349
1350 /* close RIFF chunk */
1351 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK)
1352 return AVIERR_FILEWRITE;
1353 if (mmioFlush(This->hmmio, 0) != S_OK)
1354 return AVIERR_FILEWRITE;
1355
1356 return AVIERR_OK;
1357 }