- Added support for Sun Audio files in wave-file handler.
[wine] / dlls / 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
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"
30
31 #include "avifile_private.h"
32 #include "extrachunk.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
37
38 /***********************************************************************/
39
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')
44
45 /***********************************************************************/
46
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)
50
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
62
63 typedef struct {
64   FOURCC  fccType;
65   DWORD   offset;
66   DWORD   size;
67   INT     encoding;
68   DWORD   sampleRate;
69   DWORD   channels;
70 } SUNAUDIOHEADER;
71
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
84
85 /***********************************************************************/
86
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);
97
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
110 };
111
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);
121
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
133 };
134
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);
149
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
166 };
167
168 typedef struct _IAVIFileImpl IAVIFileImpl;
169
170 typedef struct _IPersistFileImpl {
171   /* IUnknown stuff */
172   ICOM_VFIELD(IPersistFile);
173
174   /* IPersistFile stuff */
175   IAVIFileImpl     *paf;
176 } IPersistFileImpl;
177
178 typedef struct _IAVIStreamImpl {
179   /* IUnknown stuff */
180   ICOM_VFIELD(IAVIStream);
181
182   /* IAVIStream stuff */
183   IAVIFileImpl     *paf;
184 } IAVIStreamImpl;
185
186 struct _IAVIFileImpl {
187   /* IUnknown stuff */
188   ICOM_VFIELD(IAVIFile);
189   DWORD             ref;
190
191   /* IAVIFile, IAVIStream stuff... */
192   IPersistFileImpl  iPersistFile;
193   IAVIStreamImpl    iAVIStream;
194
195   AVIFILEINFOW      fInfo;
196   AVISTREAMINFOW    sInfo;
197
198   LPWAVEFORMATEX    lpFormat;
199   LONG              cbFormat;
200
201   MMCKINFO          ckData;
202
203   EXTRACHUNKS       extra;
204
205   /* IPersistFile stuff ... */
206   HMMIO             hmmio;
207   LPWSTR            szFileName;
208   UINT              uMode;
209   BOOL              fDirty;
210 };
211
212 /***********************************************************************/
213
214 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
215 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This);
216 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
217
218 HRESULT AVIFILE_CreateWAVFile(REFIID riid, LPVOID *ppv)
219 {
220   IAVIFileImpl *pfile;
221   HRESULT       hr;
222
223   assert(riid != NULL && ppv != NULL);
224
225   *ppv = NULL;
226
227   pfile = (IAVIFileImpl*)LocalAlloc(LPTR, sizeof(IAVIFileImpl));
228   if (pfile == NULL)
229     return AVIERR_MEMORY;
230
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;
237
238   hr = IUnknown_QueryInterface((IUnknown*)pfile, riid, ppv);
239   if (FAILED(hr))
240     LocalFree((HLOCAL)pfile);
241
242   return hr;
243 }
244
245 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
246                                                 LPVOID *obj)
247 {
248   ICOM_THIS(IAVIFileImpl,iface);
249
250   TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
251
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;
263   }
264
265   return OLE_E_ENUM_NOMORE;
266 }
267
268 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
269 {
270   ICOM_THIS(IAVIFileImpl,iface);
271
272   TRACE("(%p)\n",iface);
273
274   return ++(This->ref);
275 }
276
277 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
278 {
279   ICOM_THIS(IAVIFileImpl,iface);
280
281   TRACE("(%p)\n",iface);
282
283   if (!--(This->ref)) {
284     if (This->fDirty) {
285       /* need to write headers to file */
286       AVIFILE_SaveFile(This);
287     }
288
289     if (This->lpFormat != NULL) {
290       GlobalFreePtr(This->lpFormat);
291       This->lpFormat = NULL;
292       This->cbFormat = 0;
293     }
294     if (This->extra.lp != NULL) {
295       GlobalFreePtr(This->extra.lp);
296       This->extra.lp = NULL;
297       This->extra.cb = 0;
298     }
299     if (This->szFileName != NULL) {
300       LocalFree((HLOCAL)This->szFileName);
301       This->szFileName = NULL;
302     }
303     if (This->hmmio != NULL) {
304       mmioClose(This->hmmio, 0);
305       This->hmmio = NULL;
306     }
307
308     LocalFree((HLOCAL)This);
309     return 0;
310   }
311   return This->ref;
312 }
313
314 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
315                                       LONG size)
316 {
317   ICOM_THIS(IAVIFileImpl,iface);
318
319   TRACE("(%p,%p,%ld)\n",iface,afi,size);
320
321   if (afi == NULL)
322     return AVIERR_BADPARAM;
323   if (size < 0)
324     return AVIERR_BADSIZE;
325
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);
331
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);
339   }
340
341   memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
342
343   if ((DWORD)size < sizeof(This->fInfo))
344     return AVIERR_BUFFERTOOSMALL;
345   return AVIERR_OK;
346 }
347
348 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
349                                            DWORD fccType, LONG lParam)
350 {
351   ICOM_THIS(IAVIFileImpl,iface);
352
353   TRACE("(%p,%p,0x%08lX,%ld)\n", iface, avis, fccType, lParam);
354
355   /* check parameter */
356   if (avis == NULL)
357     return AVIERR_BADPARAM;
358
359   *avis = NULL;
360
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;
366
367   *avis = (PAVISTREAM)&This->iAVIStream;
368   IAVIFile_AddRef(iface);
369
370   return AVIERR_OK;
371 }
372
373 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
374                                               LPAVISTREAMINFOW asi)
375 {
376   ICOM_THIS(IAVIFileImpl,iface);
377
378   TRACE("(%p,%p,%p)\n", iface, avis, asi);
379
380   /* check parameters */
381   if (avis == NULL || asi == NULL)
382     return AVIERR_BADPARAM;
383
384   *avis = NULL;
385
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;
391
392   /* Does the user have write permission? */
393   if ((This->uMode & MMIO_RWMODE) == 0)
394     return AVIERR_READONLY;
395
396   This->cbFormat = 0;
397   This->lpFormat = NULL;
398
399   memcpy(&This->sInfo, asi, sizeof(This->sInfo));
400
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));
409
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;
414
415   This->ckData.dwDataOffset = 0;
416   This->ckData.cksize       = 0;
417
418   *avis = (PAVISTREAM)&This->iAVIStream;
419   IAVIFile_AddRef(iface);
420
421   return AVIERR_OK;
422 }
423
424 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
425                                            LPVOID lpData, LONG size)
426 {
427   ICOM_THIS(IAVIFileImpl,iface);
428
429   TRACE("(%p,0x%08lX,%p,%ld)\n", iface, ckid, lpData, size);
430
431   /* check parameters */
432   if (lpData == NULL)
433     return AVIERR_BADPARAM;
434   if (size < 0)
435     return AVIERR_BADSIZE;
436
437   /* Do we have write permission? */
438   if ((This->uMode & MMIO_RWMODE) == 0)
439     return AVIERR_READONLY;
440
441   This->fDirty = TRUE;
442
443   return WriteExtraChunk(&This->extra, ckid, lpData, size);
444 }
445
446 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
447                                           LPVOID lpData, LONG *size)
448 {
449   ICOM_THIS(IAVIFileImpl,iface);
450
451   TRACE("(%p,0x%08lX,%p,%p)\n", iface, ckid, lpData, size);
452
453   return ReadExtraChunk(&This->extra, ckid, lpData, size);
454 }
455
456 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface)
457 {
458   TRACE("(%p)\n",iface);
459
460   /* This is only needed for interleaved files.
461    * We have only one stream, which can't be interleaved.
462    */
463   return AVIERR_OK;
464 }
465
466 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
467                                               LONG lParam)
468 {
469   ICOM_THIS(IAVIFileImpl,iface);
470
471   TRACE("(%p,0x%08lX,%ld)\n", iface, fccType, lParam);
472
473   /* check parameter */
474   if (lParam < 0)
475     return AVIERR_BADPARAM;
476
477   /* Have we our audio stream? */
478   if (lParam != 0 || This->fInfo.dwStreams == 0 ||
479       (fccType != 0 && fccType != streamtypeAUDIO))
480     return AVIERR_NODATA;
481
482   /* Have user write permissions? */
483   if ((This->uMode & MMIO_RWMODE) == 0)
484     return AVIERR_READONLY;
485
486   GlobalFreePtr(This->lpFormat);
487   This->lpFormat = NULL;
488   This->cbFormat = 0;
489
490   /* update infos */
491   This->ckData.dwDataOffset = 0;
492   This->ckData.cksize       = 0;
493
494   This->sInfo.dwScale   = 0;
495   This->sInfo.dwRate    = 0;
496   This->sInfo.dwLength  = 0;
497   This->sInfo.dwSuggestedBufferSize = 0;
498
499   This->fInfo.dwStreams = 0;
500   This->fInfo.dwEditCount++;
501
502   This->fDirty = TRUE;
503
504   return AVIERR_OK;
505 }
506
507 /***********************************************************************/
508
509 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
510                                                     REFIID refiid, LPVOID *obj)
511 {
512   ICOM_THIS(IPersistFileImpl,iface);
513
514   assert(This->paf != NULL);
515
516   return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
517 }
518
519 static ULONG   WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
520 {
521   ICOM_THIS(IPersistFileImpl,iface);
522
523   assert(This->paf != NULL);
524
525   return IAVIFile_AddRef((PAVIFILE)This->paf);
526 }
527
528 static ULONG   WINAPI IPersistFile_fnRelease(IPersistFile *iface)
529 {
530   ICOM_THIS(IPersistFileImpl,iface);
531
532   assert(This->paf != NULL);
533
534   return IAVIFile_Release((PAVIFILE)This->paf);
535 }
536
537 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface,
538                                                 LPCLSID pClassID)
539 {
540   TRACE("(%p,%p)\n", iface, pClassID);
541
542   if (pClassID == NULL)
543     return AVIERR_BADPARAM;
544
545   memcpy(pClassID, &CLSID_WAVFile, sizeof(CLSID_WAVFile));
546
547   return AVIERR_OK;
548 }
549
550 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
551 {
552   ICOM_THIS(IPersistFileImpl,iface);
553
554   TRACE("(%p)\n", iface);
555
556   assert(This->paf != NULL);
557
558   return (This->paf->fDirty ? S_OK : S_FALSE);
559 }
560
561 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface,
562                                           LPCOLESTR pszFileName, DWORD dwMode)
563 {
564   IAVIFileImpl *This = ((IPersistFileImpl*)iface)->paf;
565
566   WCHAR wszStreamFmt[50];
567   INT   len;
568
569   TRACE("(%p,%s,0x%08lX)\n", iface, debugstr_w(pszFileName), dwMode);
570
571   /* check parameter */
572   if (pszFileName == NULL)
573     return AVIERR_BADPARAM;
574
575   assert(This != NULL);
576   if (This->hmmio != NULL)
577     return AVIERR_ERROR; /* No reuse of this object for another file! */
578
579   /* remeber mode and name */
580   This->uMode = dwMode;
581
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);
587
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;
595
596     WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1, szFileName,
597                         len, NULL, NULL);
598
599     This->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode);
600     LocalFree((HLOCAL)szFileName);
601     if (This->hmmio == NULL)
602       return AVIERR_FILEOPEN;
603   }
604
605   memset(& This->fInfo, 0, sizeof(This->fInfo));
606   memset(& This->sInfo, 0, sizeof(This->sInfo));
607
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));
614   }
615
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);
622 }
623
624 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface,
625                                           LPCOLESTR pszFileName,BOOL fRemember)
626 {
627   TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember);
628
629   /* We write directly to disk, so nothing to do. */
630
631   return AVIERR_OK;
632 }
633
634 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface,
635                                                    LPCOLESTR pszFileName)
636 {
637   TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName));
638
639   /* We write directly to disk, so nothing to do. */
640
641   return AVIERR_OK;
642 }
643
644 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
645                                                 LPOLESTR *ppszFileName)
646 {
647   ICOM_THIS(IPersistFileImpl,iface);
648
649   TRACE("(%p,%p)\n", iface, ppszFileName);
650
651   if (ppszFileName == NULL)
652     return AVIERR_BADPARAM;
653
654   *ppszFileName = NULL;
655
656   assert(This->paf != NULL);
657
658   if (This->paf->szFileName != NULL) {
659     int len = lstrlenW(This->paf->szFileName);
660
661     *ppszFileName = (LPOLESTR)GlobalAllocPtr(GHND, len * sizeof(WCHAR));
662     if (*ppszFileName == NULL)
663       return AVIERR_MEMORY;
664
665     memcpy(*ppszFileName, This->paf->szFileName, len * sizeof(WCHAR));
666   }
667
668   return AVIERR_OK;
669 }
670
671 /***********************************************************************/
672
673 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
674                                                   REFIID refiid, LPVOID *obj)
675 {
676   ICOM_THIS(IAVIStreamImpl,iface);
677
678   assert(This->paf != NULL);
679
680   return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
681 }
682
683 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
684 {
685   ICOM_THIS(IAVIStreamImpl,iface);
686
687   assert(This->paf != NULL);
688
689   return IAVIFile_AddRef((PAVIFILE)This->paf);
690 }
691
692 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
693 {
694   ICOM_THIS(IAVIStreamImpl,iface);
695
696   assert(This->paf != NULL);
697
698   return IAVIFile_Release((PAVIFILE)This->paf);
699 }
700
701 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1,
702                                           LPARAM lParam2)
703 {
704   TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2);
705
706   /* This IAVIStream interface needs an WAVFile */
707   return AVIERR_UNSUPPORTED;
708 }
709
710 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
711                                         LONG size)
712 {
713   ICOM_THIS(IAVIStreamImpl,iface);
714
715   TRACE("(%p,%p,%ld)\n", iface, psi, size);
716
717   if (psi == NULL)
718     return AVIERR_BADPARAM;
719   if (size < 0)
720     return AVIERR_BADSIZE;
721
722   memcpy(psi, &This->paf->sInfo, min((DWORD)size, sizeof(This->paf->sInfo)));
723
724   if ((DWORD)size < sizeof(This->paf->sInfo))
725     return AVIERR_BUFFERTOOSMALL;
726   return AVIERR_OK;
727 }
728
729 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
730                                            LONG flags)
731 {
732   IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
733
734   TRACE("(%p,%ld,0x%08lX)\n",iface,pos,flags);
735
736   /* Do we have data? */
737   if (This->lpFormat == NULL)
738     return -1;
739
740   /* We don't have an index */
741   if (flags & FIND_INDEX)
742     return -1;
743
744   if (flags & FIND_FROM_START) {
745     pos = This->sInfo.dwStart;
746     flags &= ~(FIND_FROM_START|FIND_PREV);
747     flags |= FIND_NEXT;
748   }
749
750   if (flags & FIND_FORMAT) {
751     if ((flags & FIND_NEXT) && pos > 0)
752       pos = -1;
753     else
754       pos = 0;
755   }
756
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;
761
762   return pos;
763 }
764
765 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
766                                               LPVOID format, LONG *formatsize)
767 {
768   ICOM_THIS(IAVIStreamImpl,iface);
769
770   TRACE("(%p,%ld,%p,%p)\n", iface, pos, format, formatsize);
771
772   if (formatsize == NULL)
773     return AVIERR_BADPARAM;
774
775   /* only interested in needed buffersize? */
776   if (format == NULL || *formatsize <= 0) {
777     *formatsize = This->paf->cbFormat;
778
779     return AVIERR_OK;
780   }
781
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;
787   }
788
789   *formatsize = This->paf->cbFormat;
790   return AVIERR_OK;
791 }
792
793 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
794                                              LPVOID format, LONG formatsize)
795 {
796   IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
797
798   TRACE("(%p,%ld,%p,%ld)\n", iface, pos, format, formatsize);
799
800   /* check parameters */
801   if (format == NULL || formatsize <= sizeof(PCMWAVEFORMAT))
802     return AVIERR_BADPARAM;
803
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;
810
811     return AVIERR_OK;
812   }
813
814   /* only support start at position 0 */
815   if (pos != 0)
816     return AVIERR_UNSUPPORTED;
817
818   /* Do we have write permission? */
819   if ((This->uMode & MMIO_RWMODE) == 0)
820     return AVIERR_READONLY;
821
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;
826
827   This->cbFormat = formatsize;
828   memcpy(This->lpFormat, format, formatsize);
829
830   /* update info's about 'data' chunk */
831   This->ckData.dwDataOffset = formatsize + 7 * sizeof(DWORD);
832   This->ckData.cksize       = 0;
833
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);
837
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;
844
845   return AVIERR_OK;
846 }
847
848 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start,
849                                         LONG samples, LPVOID buffer,
850                                         LONG buffersize, LPLONG bytesread,
851                                         LPLONG samplesread)
852 {
853   IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
854
855   TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface, start, samples, buffer,
856         buffersize, bytesread, samplesread);
857
858   /* clear return parameters if given */
859   if (bytesread != NULL)
860     *bytesread = 0;
861   if (samplesread != NULL)
862     *samplesread = 0;
863
864   /* positions without data */
865   if (start < 0 || (DWORD)start > This->sInfo.dwLength)
866     return AVIERR_OK;
867
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;
876   }
877
878   /* limit to end of stream */
879   if ((DWORD)(start + samples) > This->sInfo.dwLength)
880     samples = This->sInfo.dwLength - start;
881
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;
887
888     if (bytesread != NULL)
889       *bytesread = samples * This->sInfo.dwSampleSize;
890     if (samplesread != NULL)
891       *samplesread = samples;
892
893     return AVIERR_OK;
894   }
895
896   /* nothing to read? */
897   if (samples == 0)
898     return AVIERR_OK;
899
900   /* Can I atleast read one sample? */
901   if ((DWORD)buffersize < This->sInfo.dwSampleSize)
902     return AVIERR_BUFFERTOOSMALL;
903
904   buffersize = samples * This->sInfo.dwSampleSize;
905
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;
911
912   /* fill out return parameters if given */
913   if (bytesread != NULL)
914     *bytesread = buffersize;
915   if (samplesread != NULL)
916     *samplesread = samples;  
917
918   return AVIERR_OK;
919 }
920
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)
926 {
927   IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
928
929   TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface, start, samples,
930         buffer, buffersize, flags, sampwritten, byteswritten);
931
932   /* clear return parameters if given */
933   if (sampwritten != NULL)
934     *sampwritten = 0;
935   if (byteswritten != NULL)
936     *byteswritten = 0;
937
938   /* check parameters */
939   if (buffer == NULL && (buffersize > 0 || samples > 0))
940     return AVIERR_BADPARAM;
941
942   /* Have we write permission? */
943   if ((This->uMode & MMIO_RWMODE) == 0)
944     return AVIERR_READONLY;
945
946   /* < 0 means "append" */
947   if (start < 0)
948     start = This->sInfo.dwStart + This->sInfo.dwLength;
949
950   /* check buffersize -- must multiple of samplesize */
951   if (buffersize & ~(This->sInfo.dwSampleSize - 1))
952     return AVIERR_BADSIZE;
953
954   /* have we anything to write? */
955   if (buffer != NULL && buffersize > 0) {
956     This->fDirty = 1;
957
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;
963
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);
967
968     /* fill out return parameters if given */
969     if (sampwritten != NULL)
970       *sampwritten = samples;
971     if (byteswritten != NULL)
972       *byteswritten = buffersize;
973   }
974
975   return AVIERR_OK;
976 }
977
978 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
979                                           LONG samples)
980 {
981   IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
982
983   TRACE("(%p,%ld,%ld)\n", iface, start, samples);
984
985   /* check parameters */
986   if (start < 0 || samples < 0)
987     return AVIERR_BADPARAM;
988
989   /* Delete before start of stream? */
990   if ((DWORD)(start + samples) < This->sInfo.dwStart)
991     return AVIERR_OK;
992
993   /* Delete after end of stream? */
994   if ((DWORD)start > This->sInfo.dwLength)
995     return AVIERR_OK;
996
997   /* For the rest we need write permissions */
998   if ((This->uMode & MMIO_RWMODE) == 0)
999     return AVIERR_READONLY;
1000
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");
1015
1016     return AVIERR_UNSUPPORTED;
1017   }
1018
1019   This->fDirty = 1;
1020
1021   return AVIERR_OK;
1022 }
1023
1024 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1025                                             LPVOID lp, LPLONG lpread)
1026 {
1027   ICOM_THIS(IAVIStreamImpl,iface);
1028
1029   assert(This->paf != NULL);
1030
1031   return IAVIFile_ReadData((PAVIFILE)This->paf, fcc, lp, lpread);
1032 }
1033
1034 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1035                                              LPVOID lp, LONG size)
1036 {
1037   ICOM_THIS(IAVIStreamImpl,iface);
1038
1039   return IAVIFile_WriteData((PAVIFILE)This->paf, fcc, lp, size);
1040 }
1041
1042 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1043                                            LPAVISTREAMINFOW info, LONG infolen)
1044 {
1045   FIXME("(%p,%p,%ld): stub\n", iface, info, infolen);
1046
1047   return E_FAIL;
1048 }
1049
1050 /***********************************************************************/
1051
1052 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This)
1053 {
1054   MMCKINFO ckRIFF;
1055   MMCKINFO ck;
1056
1057   This->sInfo.dwLength = 0; /* just to be sure */
1058   This->fDirty = FALSE;
1059
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);
1064   }
1065
1066   if (ckRIFF.fccType != formtypeWAVE)
1067     return AVIERR_BADFORMAT;
1068
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;
1074
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;
1080
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;
1085
1086   /* Non-pcm formats have a fact chunk.
1087    * We don't need it, so simply add it to the extra chunks.
1088    */
1089
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;
1095
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;
1103
1104   This->fInfo.dwStreams = 1;
1105
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;
1113   }
1114
1115   /* ignore errors */
1116   FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck, &ckRIFF, 0);
1117
1118   return AVIERR_OK;
1119 }
1120
1121 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This)
1122 {
1123   SUNAUDIOHEADER auhdr;
1124
1125   mmioSeek(This->hmmio, 0, SEEK_SET);
1126   if (mmioRead(This->hmmio, (HPSTR)&auhdr, sizeof(auhdr)) != sizeof(auhdr))
1127     return AVIERR_FILEREAD;
1128
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);
1133
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);
1141
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;
1147
1148   if (auhdr.channels < 1)
1149     return AVIERR_BADFORMAT;
1150
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;
1163   };
1164
1165   This->lpFormat =
1166     (LPWAVEFORMATEX)GlobalAllocPtr(GMEM_MOVEABLE, This->cbFormat);
1167   if (This->lpFormat == NULL)
1168     return AVIERR_MEMORY;
1169
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;
1215   };
1216
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;
1223
1224   This->fDirty = 0;
1225
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;
1239
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);
1246
1247   return AVIERR_OK;
1248 }
1249
1250 static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This)
1251 {
1252   MMCKINFO ckRIFF;
1253   MMCKINFO ck;
1254
1255   mmioSeek(This->hmmio, 0, SEEK_SET);
1256
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;
1262
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;
1271   }
1272   if (mmioAscend(This->hmmio, &ck, 0) != S_OK)
1273     return AVIERR_FILEWRITE;
1274
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;
1281
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);
1293
1294       /* crete the fact chunk */
1295       ck.ckid   = ckidWAVEFACT;
1296       ck.cksize = sizeof(dwFactLength);
1297
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");
1310   }
1311
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;
1318
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;
1324   }
1325
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;
1335
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;
1341   }
1342
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;
1348
1349   return AVIERR_OK;
1350 }