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