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