blob: 2c60bf51f7dd85d35de762f4a92f1c0a3774aff2 [file] [log] [blame]
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001/*
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020016 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Michael Günnewig4c04e1f2002-10-18 00:24:41 +000017 */
18
19#include <assert.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000020#include <stdarg.h>
Michael Günnewig4c04e1f2002-10-18 00:24:41 +000021
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000022#include "windef.h"
Michael Günnewig4c04e1f2002-10-18 00:24:41 +000023#include "winbase.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000024#include "wingdi.h"
Michael Günnewig4c04e1f2002-10-18 00:24:41 +000025#include "winuser.h"
26#include "winnls.h"
27#include "winerror.h"
Michael Günnewig4c04e1f2002-10-18 00:24:41 +000028#include "mmsystem.h"
29#include "vfw.h"
Michael Günnewig45af4922002-10-19 00:12:06 +000030#include "msacm.h"
Michael Günnewig4c04e1f2002-10-18 00:24:41 +000031
32#include "avifile_private.h"
33#include "extrachunk.h"
34
Mike McCormack87bacf42004-07-04 00:11:31 +000035#include "wine/unicode.h"
Michael Günnewig4c04e1f2002-10-18 00:24:41 +000036#include "wine/debug.h"
37
38WINE_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
Michael Günnewig645a36c2003-07-21 20:00:36 +000049#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
65typedef 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
Michael Günnewig4c04e1f2002-10-18 00:24:41 +000089static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
90static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
91static ULONG WINAPI IAVIFile_fnRelease(IAVIFile* iface);
92static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
93static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
94static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
95static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
96static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
97static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
98static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
99
Dmitry Timoshkov46256282005-05-27 20:17:35 +0000100static const struct IAVIFileVtbl iwavft = {
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000101 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
113static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile*iface,REFIID refiid,LPVOID*obj);
114static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile*iface);
115static ULONG WINAPI IPersistFile_fnRelease(IPersistFile*iface);
116static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile*iface,CLSID*pClassID);
117static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile*iface);
118static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile*iface,LPCOLESTR pszFileName,DWORD dwMode);
119static HRESULT WINAPI IPersistFile_fnSave(IPersistFile*iface,LPCOLESTR pszFileName,BOOL fRemember);
120static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile*iface,LPCOLESTR pszFileName);
121static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile*iface,LPOLESTR*ppszFileName);
122
Dmitry Timoshkov46256282005-05-27 20:17:35 +0000123static const struct IPersistFileVtbl iwavpft = {
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000124 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
135static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID *obj);
136static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream*iface);
137static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface);
138static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
139static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
140static LONG WINAPI IAVIStream_fnFindSample(IAVIStream*iface,LONG pos,LONG flags);
141static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG *formatsize);
142static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
143static HRESULT WINAPI IAVIStream_fnRead(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread);
144static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten);
145static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
146static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG *lpread);
147static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG size);
148static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
149
Dmitry Timoshkov46256282005-05-27 20:17:35 +0000150static const struct IAVIStreamVtbl iwavst = {
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000151 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
167typedef struct _IAVIFileImpl IAVIFileImpl;
168
169typedef struct _IPersistFileImpl {
170 /* IUnknown stuff */
Dmitry Timoshkov46256282005-05-27 20:17:35 +0000171 const IPersistFileVtbl *lpVtbl;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000172
173 /* IPersistFile stuff */
174 IAVIFileImpl *paf;
175} IPersistFileImpl;
176
177typedef struct _IAVIStreamImpl {
178 /* IUnknown stuff */
Dmitry Timoshkov46256282005-05-27 20:17:35 +0000179 const IAVIStreamVtbl *lpVtbl;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000180
181 /* IAVIStream stuff */
182 IAVIFileImpl *paf;
183} IAVIStreamImpl;
184
185struct _IAVIFileImpl {
186 /* IUnknown stuff */
Dmitry Timoshkov46256282005-05-27 20:17:35 +0000187 const IAVIFileVtbl *lpVtbl;
Mike McCormack723ee0a2005-07-05 14:26:54 +0000188 LONG ref;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000189
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
213static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
214static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This);
Andrew Talbotb01c2c82007-04-02 22:50:46 +0100215static HRESULT AVIFILE_SaveFile(const IAVIFileImpl *This);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000216
217HRESULT 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
Mike McCormackaebc88d2006-04-11 20:44:02 +0900226 pfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIFileImpl));
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000227 if (pfile == NULL)
228 return AVIERR_MEMORY;
229
Alexandre Julliardc2ebe1f2003-04-10 18:17:34 +0000230 pfile->lpVtbl = &iwavft;
231 pfile->iPersistFile.lpVtbl = &iwavpft;
232 pfile->iAVIStream.lpVtbl = &iwavst;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000233 pfile->ref = 0;
234 pfile->iPersistFile.paf = pfile;
235 pfile->iAVIStream.paf = pfile;
236
Francois Gougeta8d2ba92004-10-05 18:10:21 +0000237 hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000238 if (FAILED(hr))
Mike McCormack610fc082006-03-11 00:21:42 +0900239 HeapFree(GetProcessHeap(), 0, pfile);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000240
241 return hr;
242}
243
244static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
245 LPVOID *obj)
246{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000247 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000248
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
267static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
268{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000269 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000270
271 TRACE("(%p)\n",iface);
272
Paul Vriensc905d692005-01-14 15:10:52 +0000273 return InterlockedIncrement(&This->ref);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000274}
275
276static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
277{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000278 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Paul Vriensc905d692005-01-14 15:10:52 +0000279 ULONG ref = InterlockedDecrement(&This->ref);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000280
281 TRACE("(%p)\n",iface);
282
Paul Vriensc905d692005-01-14 15:10:52 +0000283 if (!ref) {
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000284 if (This->fDirty) {
285 /* need to write headers to file */
286 AVIFILE_SaveFile(This);
287 }
288
289 if (This->lpFormat != NULL) {
Mike McCormack610fc082006-03-11 00:21:42 +0900290 HeapFree(GetProcessHeap(), 0, This->lpFormat);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000291 This->lpFormat = NULL;
292 This->cbFormat = 0;
293 }
294 if (This->extra.lp != NULL) {
Mike McCormack610fc082006-03-11 00:21:42 +0900295 HeapFree(GetProcessHeap(), 0, This->extra.lp);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000296 This->extra.lp = NULL;
297 This->extra.cb = 0;
298 }
Michael Stefaniucfee72b62006-06-12 00:11:36 +0200299 HeapFree(GetProcessHeap(), 0, This->szFileName);
300 This->szFileName = NULL;
Francois Gougetd2667a42002-12-02 18:10:57 +0000301 if (This->hmmio != NULL) {
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000302 mmioClose(This->hmmio, 0);
Francois Gougetd2667a42002-12-02 18:10:57 +0000303 This->hmmio = NULL;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000304 }
305
Mike McCormack610fc082006-03-11 00:21:42 +0900306 HeapFree(GetProcessHeap(), 0, This);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000307 return 0;
308 }
Paul Vriensc905d692005-01-14 15:10:52 +0000309 return ref;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000310}
311
312static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
313 LONG size)
314{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000315 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000316
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200317 TRACE("(%p,%p,%d)\n",iface,afi,size);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000318
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) {
Michael Günnewig45af4922002-10-19 00:12:06 +0000328 assert(This->sInfo.dwScale != 0);
329
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000330 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;
Michael Günnewig45af4922002-10-19 00:12:06 +0000335 This->fInfo.dwMaxBytesPerSec =
336 MulDiv(This->sInfo.dwSampleSize,This->sInfo.dwRate,This->sInfo.dwScale);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000337 }
338
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000339 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000340
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000341 if ((DWORD)size < sizeof(This->fInfo))
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000342 return AVIERR_BUFFERTOOSMALL;
343 return AVIERR_OK;
344}
345
346static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
347 DWORD fccType, LONG lParam)
348{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000349 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000350
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200351 TRACE("(%p,%p,0x%08X,%d)\n", iface, avis, fccType, lParam);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000352
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
371static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
372 LPAVISTREAMINFOW asi)
373{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000374 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000375
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
422static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
423 LPVOID lpData, LONG size)
424{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000425 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000426
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200427 TRACE("(%p,0x%08X,%p,%d)\n", iface, ckid, lpData, size);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000428
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
444static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
445 LPVOID lpData, LONG *size)
446{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000447 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000448
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200449 TRACE("(%p,0x%08X,%p,%p)\n", iface, ckid, lpData, size);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000450
451 return ReadExtraChunk(&This->extra, ckid, lpData, size);
452}
453
454static 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
464static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
465 LONG lParam)
466{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000467 IAVIFileImpl *This = (IAVIFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000468
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200469 TRACE("(%p,0x%08X,%d)\n", iface, fccType, lParam);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000470
471 /* check parameter */
472 if (lParam < 0)
473 return AVIERR_BADPARAM;
474
Francois Gouget6fb1a202004-12-20 19:27:06 +0000475 /* Do we have our audio stream? */
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000476 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
Mike McCormack610fc082006-03-11 00:21:42 +0900484 HeapFree(GetProcessHeap(), 0, This->lpFormat);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000485 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
507static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface,
508 REFIID refiid, LPVOID *obj)
509{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000510 IPersistFileImpl *This = (IPersistFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000511
512 assert(This->paf != NULL);
513
514 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
515}
516
517static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface)
518{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000519 IPersistFileImpl *This = (IPersistFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000520
521 assert(This->paf != NULL);
522
523 return IAVIFile_AddRef((PAVIFILE)This->paf);
524}
525
526static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface)
527{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000528 IPersistFileImpl *This = (IPersistFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000529
530 assert(This->paf != NULL);
531
532 return IAVIFile_Release((PAVIFILE)This->paf);
533}
534
535static 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
548static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface)
549{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000550 IPersistFileImpl *This = (IPersistFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000551
552 TRACE("(%p)\n", iface);
553
554 assert(This->paf != NULL);
555
556 return (This->paf->fDirty ? S_OK : S_FALSE);
557}
558
559static 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
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200567 TRACE("(%p,%s,0x%08X)\n", iface, debugstr_w(pszFileName), dwMode);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000568
569 /* check parameter */
570 if (pszFileName == NULL)
571 return AVIERR_BADPARAM;
572
573 assert(This != NULL);
Francois Gougetd2667a42002-12-02 18:10:57 +0000574 if (This->hmmio != NULL)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000575 return AVIERR_ERROR; /* No reuse of this object for another file! */
576
Austin Englishf6caf1b2008-01-16 17:04:38 -0600577 /* remember mode and name */
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000578 This->uMode = dwMode;
579
580 len = lstrlenW(pszFileName) + 1;
Mike McCormack610fc082006-03-11 00:21:42 +0900581 This->szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000582 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);
Michael Günnewig645a36c2003-07-21 20:00:36 +0000588 if (This->hmmio == NULL) {
589 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
Mike McCormack87bacf42004-07-04 00:11:31 +0000590 LPSTR szFileName;
591 len = WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1,
592 NULL, 0, NULL, NULL);
Mike McCormack610fc082006-03-11 00:21:42 +0900593 szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR));
Michael Günnewig645a36c2003-07-21 20:00:36 +0000594 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);
Mike McCormack610fc082006-03-11 00:21:42 +0900601 HeapFree(GetProcessHeap(), 0, szFileName);
Michael Günnewig645a36c2003-07-21 20:00:36 +0000602 if (This->hmmio == NULL)
603 return AVIERR_FILEOPEN;
604 }
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000605
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
625static 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
635static 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
645static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface,
646 LPOLESTR *ppszFileName)
647{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000648 IPersistFileImpl *This = (IPersistFileImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000649
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) {
Mike McCormack87bacf42004-07-04 00:11:31 +0000660 int len = lstrlenW(This->paf->szFileName) + 1;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000661
Mike McCormack29212d82006-03-11 00:21:30 +0900662 *ppszFileName = CoTaskMemAlloc(len * sizeof(WCHAR));
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000663 if (*ppszFileName == NULL)
664 return AVIERR_MEMORY;
665
Mike McCormack87bacf42004-07-04 00:11:31 +0000666 strcpyW(*ppszFileName, This->paf->szFileName);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000667 }
668
669 return AVIERR_OK;
670}
671
672/***********************************************************************/
673
674static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface,
675 REFIID refiid, LPVOID *obj)
676{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000677 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000678
679 assert(This->paf != NULL);
680
681 return IAVIFile_QueryInterface((PAVIFILE)This->paf, refiid, obj);
682}
683
684static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface)
685{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000686 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000687
688 assert(This->paf != NULL);
689
690 return IAVIFile_AddRef((PAVIFILE)This->paf);
691}
692
693static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface)
694{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000695 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000696
697 assert(This->paf != NULL);
698
699 return IAVIFile_Release((PAVIFILE)This->paf);
700}
701
702static 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
711static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface,LPAVISTREAMINFOW psi,
712 LONG size)
713{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000714 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000715
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200716 TRACE("(%p,%p,%d)\n", iface, psi, size);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000717
718 if (psi == NULL)
719 return AVIERR_BADPARAM;
720 if (size < 0)
721 return AVIERR_BADSIZE;
722
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000723 memcpy(psi, &This->paf->sInfo, min((DWORD)size, sizeof(This->paf->sInfo)));
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000724
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000725 if ((DWORD)size < sizeof(This->paf->sInfo))
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000726 return AVIERR_BUFFERTOOSMALL;
727 return AVIERR_OK;
728}
729
730static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos,
731 LONG flags)
732{
733 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
734
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200735 TRACE("(%p,%d,0x%08X)\n",iface,pos,flags);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000736
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
Michael Günnewigedd8bc32003-11-26 22:04:29 +0000758 if ((flags & FIND_RET) == FIND_LENGTH ||
759 (flags & FIND_RET) == FIND_SIZE)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000760 return This->sInfo.dwSampleSize;
Michael Günnewigedd8bc32003-11-26 22:04:29 +0000761 if ((flags & FIND_RET) == FIND_OFFSET)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000762 return This->ckData.dwDataOffset + pos * This->sInfo.dwSampleSize;
763
764 return pos;
765}
766
767static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos,
768 LPVOID format, LONG *formatsize)
769{
Alexandre Julliard39a696a2004-09-06 20:34:29 +0000770 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000771
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200772 TRACE("(%p,%d,%p,%p)\n", iface, pos, format, formatsize);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000773
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
795static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos,
796 LPVOID format, LONG formatsize)
797{
798 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
799
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200800 TRACE("(%p,%d,%p,%d)\n", iface, pos, format, formatsize);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000801
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 */
Mike McCormack610fc082006-03-11 00:21:42 +0900825 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, formatsize);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000826 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
850static 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
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200857 TRACE("(%p,%d,%d,%p,%d,%p,%p)\n", iface, start, samples, buffer,
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000858 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 */
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000867 if (start < 0 || (DWORD)start > This->sInfo.dwLength)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000868 return AVIERR_OK;
869
870 /* check samples */
871 if (samples < 0)
872 samples = 0;
873 if (buffersize > 0) {
874 if (samples > 0)
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000875 samples = min((DWORD)samples, buffersize / This->sInfo.dwSampleSize);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000876 else
877 samples = buffersize / This->sInfo.dwSampleSize;
878 }
879
880 /* limit to end of stream */
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000881 if ((DWORD)(start + samples) > This->sInfo.dwLength)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000882 samples = This->sInfo.dwLength - start;
883
884 /* request only the sizes? */
885 if (buffer == NULL || buffersize <= 0) {
Francois Gouget6fb1a202004-12-20 19:27:06 +0000886 /* then I need at least one parameter for it */
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000887 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
Francois Gouget6fb1a202004-12-20 19:27:06 +0000902 /* Can I read at least one sample? */
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000903 if ((DWORD)buffersize < This->sInfo.dwSampleSize)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000904 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
923static 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
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200931 TRACE("(%p,%d,%d,%p,%d,0x%08X,%p,%p)\n", iface, start, samples,
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000932 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
Francois Gouget6fb1a202004-12-20 19:27:06 +0000944 /* Do we have write permission? */
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000945 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
Francois Gouget6fb1a202004-12-20 19:27:06 +0000956 /* do we have anything to write? */
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000957 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
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000966 This->sInfo.dwLength = max(This->sInfo.dwLength, (DWORD)start + samples);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000967 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
980static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start,
981 LONG samples)
982{
983 IAVIFileImpl *This = ((IAVIStreamImpl*)iface)->paf;
984
Michael Stefaniucf041a2b2006-10-10 01:07:06 +0200985 TRACE("(%p,%d,%d)\n", iface, start, samples);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000986
987 /* check parameters */
988 if (start < 0 || samples < 0)
989 return AVIERR_BADPARAM;
990
991 /* Delete before start of stream? */
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000992 if ((DWORD)(start + samples) < This->sInfo.dwStart)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000993 return AVIERR_OK;
994
995 /* Delete after end of stream? */
Michael Günnewig9f67cfe2003-06-23 18:10:06 +0000996 if ((DWORD)start > This->sInfo.dwLength)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +0000997 return AVIERR_OK;
998
999 /* For the rest we need write permissions */
1000 if ((This->uMode & MMIO_RWMODE) == 0)
1001 return AVIERR_READONLY;
1002
Michael Günnewig9f67cfe2003-06-23 18:10:06 +00001003 if ((DWORD)(start + samples) >= This->sInfo.dwLength) {
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001004 /* deletion at end */
1005 samples = This->sInfo.dwLength - start;
1006 This->sInfo.dwLength -= samples;
1007 This->ckData.cksize -= samples * This->sInfo.dwSampleSize;
Michael Günnewig9f67cfe2003-06-23 18:10:06 +00001008 } else if ((DWORD)start <= This->sInfo.dwStart) {
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001009 /* 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
1026static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc,
1027 LPVOID lp, LPLONG lpread)
1028{
Alexandre Julliard39a696a2004-09-06 20:34:29 +00001029 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001030
1031 assert(This->paf != NULL);
1032
1033 return IAVIFile_ReadData((PAVIFILE)This->paf, fcc, lp, lpread);
1034}
1035
1036static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc,
1037 LPVOID lp, LONG size)
1038{
Alexandre Julliard39a696a2004-09-06 20:34:29 +00001039 IAVIStreamImpl *This = (IAVIStreamImpl *)iface;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001040
1041 return IAVIFile_WriteData((PAVIFILE)This->paf, fcc, lp, size);
1042}
1043
1044static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface,
1045 LPAVISTREAMINFOW info, LONG infolen)
1046{
Michael Stefaniucf041a2b2006-10-10 01:07:06 +02001047 FIXME("(%p,%p,%d): stub\n", iface, info, infolen);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001048
1049 return E_FAIL;
1050}
1051
1052/***********************************************************************/
1053
1054static 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 */
Michael Günnewig45af4922002-10-19 00:12:06 +00001063 ckRIFF.fccType = 0; /* find any */
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001064 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,
Michael Günnewig45af4922002-10-19 00:12:06 +00001074 &ckRIFF, MMIO_FINDCHUNK) != S_OK)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001075 return AVIERR_FILEREAD;
1076
1077 /* get memory for format and read it */
Mike McCormack610fc082006-03-11 00:21:42 +09001078 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, ck.cksize);
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001079 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
Michael Günnewig45af4922002-10-19 00:12:06 +00001088 /* Non-pcm formats have a fact chunk.
1089 * We don't need it, so simply add it to the extra chunks.
1090 */
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001091
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
Michael Günnewig45af4922002-10-19 00:12:06 +00001106 This->fInfo.dwStreams = 1;
1107
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001108 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
1123static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This)
1124{
Michael Günnewig645a36c2003-07-21 20:00:36 +00001125 SUNAUDIOHEADER auhdr;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001126
Michael Günnewig645a36c2003-07-21 20:00:36 +00001127 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
Mike McCormack610fc082006-03-11 00:21:42 +09001167 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, This->cbFormat);
Michael Günnewig645a36c2003-07-21 20:00:36 +00001168 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;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001249}
1250
Andrew Talbotb01c2c82007-04-02 22:50:46 +01001251static HRESULT AVIFILE_SaveFile(const IAVIFileImpl *This)
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001252{
Michael Günnewig45af4922002-10-19 00:12:06 +00001253 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;
Francois Gougetd2667a42002-12-02 18:10:57 +00001286 if (acmFormatSuggest(NULL, This->lpFormat, &wfx,
Michael Günnewig45af4922002-10-19 00:12:06 +00001287 sizeof(wfx), ACM_FORMATSUGGESTF_WFORMATTAG)) {
Francois Gougetd2667a42002-12-02 18:10:57 +00001288 acmStreamOpen(&has, NULL, This->lpFormat, &wfx, NULL,
Michael Günnewig45af4922002-10-19 00:12:06 +00001289 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
Francois Gouget6fb1a202004-12-20 19:27:06 +00001295 /* create the fact chunk */
Michael Günnewig45af4922002-10-19 00:12:06 +00001296 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
Francois Gouget6fb1a202004-12-20 19:27:06 +00001313 /* if there was extra stuff, we need to fill it with JUNK */
Michael Günnewig45af4922002-10-19 00:12:06 +00001314 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
Francois Gouget6fb1a202004-12-20 19:27:06 +00001327 /* create the data chunk */
Michael Günnewig45af4922002-10-19 00:12:06 +00001328 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;
Michael Günnewig4c04e1f2002-10-18 00:24:41 +00001351}