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