2 * Copyright 2002 Michael Günnewig
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.
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.
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
19 #define COM_NO_WINDOWS_H
35 #include "avifile_private.h"
36 #include "extrachunk.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
43 /***********************************************************************/
45 #define formtypeWAVE mmioFOURCC('W','A','V','E')
46 #define ckidWAVEFORMAT mmioFOURCC('f','m','t',' ')
47 #define ckidWAVEFACT mmioFOURCC('f','a','c','t')
48 #define ckidWAVEDATA mmioFOURCC('d','a','t','a')
50 /***********************************************************************/
52 #define ENDIAN_SWAPWORD(x) ((((x) >> 8) & 0xFF) | (((x) & 0xFF) << 8))
53 #define ENDIAN_SWAPDWORD(x) (ENDIAN_SWAPWORD((x >> 16) & 0xFFFF) | \
54 ENDIAN_SWAPWORD(x & 0xFFFF) << 16)
56 #ifdef WORDS_BIGENDIAN
57 #define BE2H_WORD(x) (x)
58 #define BE2H_DWORD(x) (x)
59 #define LE2H_WORD(x) ENDIAN_SWAPWORD(x)
60 #define LE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
62 #define BE2H_WORD(x) ENDIAN_SWAPWORD(x)
63 #define BE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
64 #define LE2H_WORD(x) (x)
65 #define LE2H_DWORD(x) (x)
77 #define AU_ENCODING_ULAW_8 1
78 #define AU_ENCODING_PCM_8 2
79 #define AU_ENCODING_PCM_16 3
80 #define AU_ENCODING_PCM_24 4
81 #define AU_ENCODING_PCM_32 5
82 #define AU_ENCODING_FLOAT 6
83 #define AU_ENCODING_DOUBLE 7
84 #define AU_ENCODING_ADPCM_G721_32 23
85 #define AU_ENCODING_ADPCM_G722 24
86 #define AU_ENCODING_ADPCM_G723_24 25
87 #define AU_ENCODING_ADPCM_G723_5 26
88 #define AU_ENCODING_ALAW_8 27
90 /***********************************************************************/
92 static HRESULT WINAPI
IAVIFile_fnQueryInterface(IAVIFile
* iface
,REFIID refiid
,LPVOID
*obj
);
93 static ULONG WINAPI
IAVIFile_fnAddRef(IAVIFile
* iface
);
94 static ULONG WINAPI
IAVIFile_fnRelease(IAVIFile
* iface
);
95 static HRESULT WINAPI
IAVIFile_fnInfo(IAVIFile
*iface
,AVIFILEINFOW
*afi
,LONG size
);
96 static HRESULT WINAPI
IAVIFile_fnGetStream(IAVIFile
*iface
,PAVISTREAM
*avis
,DWORD fccType
,LONG lParam
);
97 static HRESULT WINAPI
IAVIFile_fnCreateStream(IAVIFile
*iface
,PAVISTREAM
*avis
,AVISTREAMINFOW
*asi
);
98 static HRESULT WINAPI
IAVIFile_fnWriteData(IAVIFile
*iface
,DWORD ckid
,LPVOID lpData
,LONG size
);
99 static HRESULT WINAPI
IAVIFile_fnReadData(IAVIFile
*iface
,DWORD ckid
,LPVOID lpData
,LONG
*size
);
100 static HRESULT WINAPI
IAVIFile_fnEndRecord(IAVIFile
*iface
);
101 static HRESULT WINAPI
IAVIFile_fnDeleteStream(IAVIFile
*iface
,DWORD fccType
,LONG lParam
);
103 static const struct IAVIFileVtbl iwavft
= {
104 IAVIFile_fnQueryInterface
,
108 IAVIFile_fnGetStream
,
109 IAVIFile_fnCreateStream
,
110 IAVIFile_fnWriteData
,
112 IAVIFile_fnEndRecord
,
113 IAVIFile_fnDeleteStream
116 static HRESULT WINAPI
IPersistFile_fnQueryInterface(IPersistFile
*iface
,REFIID refiid
,LPVOID
*obj
);
117 static ULONG WINAPI
IPersistFile_fnAddRef(IPersistFile
*iface
);
118 static ULONG WINAPI
IPersistFile_fnRelease(IPersistFile
*iface
);
119 static HRESULT WINAPI
IPersistFile_fnGetClassID(IPersistFile
*iface
,CLSID
*pClassID
);
120 static HRESULT WINAPI
IPersistFile_fnIsDirty(IPersistFile
*iface
);
121 static HRESULT WINAPI
IPersistFile_fnLoad(IPersistFile
*iface
,LPCOLESTR pszFileName
,DWORD dwMode
);
122 static HRESULT WINAPI
IPersistFile_fnSave(IPersistFile
*iface
,LPCOLESTR pszFileName
,BOOL fRemember
);
123 static HRESULT WINAPI
IPersistFile_fnSaveCompleted(IPersistFile
*iface
,LPCOLESTR pszFileName
);
124 static HRESULT WINAPI
IPersistFile_fnGetCurFile(IPersistFile
*iface
,LPOLESTR
*ppszFileName
);
126 static const struct IPersistFileVtbl iwavpft
= {
127 IPersistFile_fnQueryInterface
,
128 IPersistFile_fnAddRef
,
129 IPersistFile_fnRelease
,
130 IPersistFile_fnGetClassID
,
131 IPersistFile_fnIsDirty
,
134 IPersistFile_fnSaveCompleted
,
135 IPersistFile_fnGetCurFile
138 static HRESULT WINAPI
IAVIStream_fnQueryInterface(IAVIStream
*iface
,REFIID refiid
,LPVOID
*obj
);
139 static ULONG WINAPI
IAVIStream_fnAddRef(IAVIStream
*iface
);
140 static ULONG WINAPI
IAVIStream_fnRelease(IAVIStream
* iface
);
141 static HRESULT WINAPI
IAVIStream_fnCreate(IAVIStream
*iface
,LPARAM lParam1
,LPARAM lParam2
);
142 static HRESULT WINAPI
IAVIStream_fnInfo(IAVIStream
*iface
,AVISTREAMINFOW
*psi
,LONG size
);
143 static LONG WINAPI
IAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,LONG flags
);
144 static HRESULT WINAPI
IAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG
*formatsize
);
145 static HRESULT WINAPI
IAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG formatsize
);
146 static HRESULT WINAPI
IAVIStream_fnRead(IAVIStream
*iface
,LONG start
,LONG samples
,LPVOID buffer
,LONG buffersize
,LONG
*bytesread
,LONG
*samplesread
);
147 static HRESULT WINAPI
IAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,LONG samples
,LPVOID buffer
,LONG buffersize
,DWORD flags
,LONG
*sampwritten
,LONG
*byteswritten
);
148 static HRESULT WINAPI
IAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,LONG samples
);
149 static HRESULT WINAPI
IAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,LPVOID lp
,LONG
*lpread
);
150 static HRESULT WINAPI
IAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,LPVOID lp
,LONG size
);
151 static HRESULT WINAPI
IAVIStream_fnSetInfo(IAVIStream
*iface
,AVISTREAMINFOW
*info
,LONG infolen
);
153 static const struct IAVIStreamVtbl iwavst
= {
154 IAVIStream_fnQueryInterface
,
156 IAVIStream_fnRelease
,
159 IAVIStream_fnFindSample
,
160 IAVIStream_fnReadFormat
,
161 IAVIStream_fnSetFormat
,
165 IAVIStream_fnReadData
,
166 IAVIStream_fnWriteData
,
170 typedef struct _IAVIFileImpl IAVIFileImpl
;
172 typedef struct _IPersistFileImpl
{
174 const IPersistFileVtbl
*lpVtbl
;
176 /* IPersistFile stuff */
180 typedef struct _IAVIStreamImpl
{
182 const IAVIStreamVtbl
*lpVtbl
;
184 /* IAVIStream stuff */
188 struct _IAVIFileImpl
{
190 const IAVIFileVtbl
*lpVtbl
;
193 /* IAVIFile, IAVIStream stuff... */
194 IPersistFileImpl iPersistFile
;
195 IAVIStreamImpl iAVIStream
;
198 AVISTREAMINFOW sInfo
;
200 LPWAVEFORMATEX lpFormat
;
207 /* IPersistFile stuff ... */
214 /***********************************************************************/
216 static HRESULT
AVIFILE_LoadFile(IAVIFileImpl
*This
);
217 static HRESULT
AVIFILE_LoadSunFile(IAVIFileImpl
*This
);
218 static HRESULT
AVIFILE_SaveFile(IAVIFileImpl
*This
);
220 HRESULT
AVIFILE_CreateWAVFile(REFIID riid
, LPVOID
*ppv
)
225 assert(riid
!= NULL
&& ppv
!= NULL
);
229 pfile
= (IAVIFileImpl
*)LocalAlloc(LPTR
, sizeof(IAVIFileImpl
));
231 return AVIERR_MEMORY
;
233 pfile
->lpVtbl
= &iwavft
;
234 pfile
->iPersistFile
.lpVtbl
= &iwavpft
;
235 pfile
->iAVIStream
.lpVtbl
= &iwavst
;
237 pfile
->iPersistFile
.paf
= pfile
;
238 pfile
->iAVIStream
.paf
= pfile
;
240 hr
= IAVIFile_QueryInterface((IAVIFile
*)pfile
, riid
, ppv
);
242 LocalFree((HLOCAL
)pfile
);
247 static HRESULT WINAPI
IAVIFile_fnQueryInterface(IAVIFile
*iface
, REFIID refiid
,
250 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
252 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
254 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
255 IsEqualGUID(&IID_IAVIFile
, refiid
)) {
258 } else if (This
->fInfo
.dwStreams
== 1 &&
259 IsEqualGUID(&IID_IAVIStream
, refiid
)) {
260 *obj
= &This
->iAVIStream
;
262 } else if (IsEqualGUID(&IID_IPersistFile
, refiid
)) {
263 *obj
= &This
->iPersistFile
;
267 return OLE_E_ENUM_NOMORE
;
270 static ULONG WINAPI
IAVIFile_fnAddRef(IAVIFile
*iface
)
272 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
274 TRACE("(%p)\n",iface
);
276 return InterlockedIncrement(&This
->ref
);
279 static ULONG WINAPI
IAVIFile_fnRelease(IAVIFile
*iface
)
281 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
282 ULONG ref
= InterlockedDecrement(&This
->ref
);
284 TRACE("(%p)\n",iface
);
288 /* need to write headers to file */
289 AVIFILE_SaveFile(This
);
292 if (This
->lpFormat
!= NULL
) {
293 GlobalFreePtr(This
->lpFormat
);
294 This
->lpFormat
= NULL
;
297 if (This
->extra
.lp
!= NULL
) {
298 GlobalFreePtr(This
->extra
.lp
);
299 This
->extra
.lp
= NULL
;
302 if (This
->szFileName
!= NULL
) {
303 LocalFree((HLOCAL
)This
->szFileName
);
304 This
->szFileName
= NULL
;
306 if (This
->hmmio
!= NULL
) {
307 mmioClose(This
->hmmio
, 0);
311 LocalFree((HLOCAL
)This
);
317 static HRESULT WINAPI
IAVIFile_fnInfo(IAVIFile
*iface
, LPAVIFILEINFOW afi
,
320 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
322 TRACE("(%p,%p,%ld)\n",iface
,afi
,size
);
325 return AVIERR_BADPARAM
;
327 return AVIERR_BADSIZE
;
329 /* update file info */
330 This
->fInfo
.dwFlags
= 0;
331 This
->fInfo
.dwCaps
= AVIFILECAPS_CANREAD
|AVIFILECAPS_CANWRITE
;
332 if (This
->lpFormat
!= NULL
) {
333 assert(This
->sInfo
.dwScale
!= 0);
335 This
->fInfo
.dwStreams
= 1;
336 This
->fInfo
.dwScale
= This
->sInfo
.dwScale
;
337 This
->fInfo
.dwRate
= This
->sInfo
.dwRate
;
338 This
->fInfo
.dwLength
= This
->sInfo
.dwLength
;
339 This
->fInfo
.dwSuggestedBufferSize
= This
->ckData
.cksize
;
340 This
->fInfo
.dwMaxBytesPerSec
=
341 MulDiv(This
->sInfo
.dwSampleSize
,This
->sInfo
.dwRate
,This
->sInfo
.dwScale
);
344 memcpy(afi
, &This
->fInfo
, min((DWORD
)size
, sizeof(This
->fInfo
)));
346 if ((DWORD
)size
< sizeof(This
->fInfo
))
347 return AVIERR_BUFFERTOOSMALL
;
351 static HRESULT WINAPI
IAVIFile_fnGetStream(IAVIFile
*iface
, PAVISTREAM
*avis
,
352 DWORD fccType
, LONG lParam
)
354 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
356 TRACE("(%p,%p,0x%08lX,%ld)\n", iface
, avis
, fccType
, lParam
);
358 /* check parameter */
360 return AVIERR_BADPARAM
;
364 /* Does our stream exists? */
365 if (lParam
!= 0 || This
->fInfo
.dwStreams
== 0)
366 return AVIERR_NODATA
;
367 if (fccType
!= 0 && fccType
!= streamtypeAUDIO
)
368 return AVIERR_NODATA
;
370 *avis
= (PAVISTREAM
)&This
->iAVIStream
;
371 IAVIFile_AddRef(iface
);
376 static HRESULT WINAPI
IAVIFile_fnCreateStream(IAVIFile
*iface
,PAVISTREAM
*avis
,
377 LPAVISTREAMINFOW asi
)
379 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
381 TRACE("(%p,%p,%p)\n", iface
, avis
, asi
);
383 /* check parameters */
384 if (avis
== NULL
|| asi
== NULL
)
385 return AVIERR_BADPARAM
;
389 /* We only support one audio stream */
390 if (This
->fInfo
.dwStreams
!= 0 || This
->lpFormat
!= NULL
)
391 return AVIERR_UNSUPPORTED
;
392 if (asi
->fccType
!= streamtypeAUDIO
)
393 return AVIERR_UNSUPPORTED
;
395 /* Does the user have write permission? */
396 if ((This
->uMode
& MMIO_RWMODE
) == 0)
397 return AVIERR_READONLY
;
400 This
->lpFormat
= NULL
;
402 memcpy(&This
->sInfo
, asi
, sizeof(This
->sInfo
));
404 /* make sure streaminfo if okay for us */
405 This
->sInfo
.fccHandler
= 0;
406 This
->sInfo
.dwFlags
= 0;
407 This
->sInfo
.dwCaps
= AVIFILECAPS_CANREAD
|AVIFILECAPS_CANWRITE
;
408 This
->sInfo
.dwStart
= 0;
409 This
->sInfo
.dwInitialFrames
= 0;
410 This
->sInfo
.dwFormatChangeCount
= 0;
411 memset(&This
->sInfo
.rcFrame
, 0, sizeof(This
->sInfo
.rcFrame
));
413 This
->fInfo
.dwStreams
= 1;
414 This
->fInfo
.dwScale
= This
->sInfo
.dwScale
;
415 This
->fInfo
.dwRate
= This
->sInfo
.dwRate
;
416 This
->fInfo
.dwLength
= This
->sInfo
.dwLength
;
418 This
->ckData
.dwDataOffset
= 0;
419 This
->ckData
.cksize
= 0;
421 *avis
= (PAVISTREAM
)&This
->iAVIStream
;
422 IAVIFile_AddRef(iface
);
427 static HRESULT WINAPI
IAVIFile_fnWriteData(IAVIFile
*iface
, DWORD ckid
,
428 LPVOID lpData
, LONG size
)
430 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
432 TRACE("(%p,0x%08lX,%p,%ld)\n", iface
, ckid
, lpData
, size
);
434 /* check parameters */
436 return AVIERR_BADPARAM
;
438 return AVIERR_BADSIZE
;
440 /* Do we have write permission? */
441 if ((This
->uMode
& MMIO_RWMODE
) == 0)
442 return AVIERR_READONLY
;
446 return WriteExtraChunk(&This
->extra
, ckid
, lpData
, size
);
449 static HRESULT WINAPI
IAVIFile_fnReadData(IAVIFile
*iface
, DWORD ckid
,
450 LPVOID lpData
, LONG
*size
)
452 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
454 TRACE("(%p,0x%08lX,%p,%p)\n", iface
, ckid
, lpData
, size
);
456 return ReadExtraChunk(&This
->extra
, ckid
, lpData
, size
);
459 static HRESULT WINAPI
IAVIFile_fnEndRecord(IAVIFile
*iface
)
461 TRACE("(%p)\n",iface
);
463 /* This is only needed for interleaved files.
464 * We have only one stream, which can't be interleaved.
469 static HRESULT WINAPI
IAVIFile_fnDeleteStream(IAVIFile
*iface
, DWORD fccType
,
472 IAVIFileImpl
*This
= (IAVIFileImpl
*)iface
;
474 TRACE("(%p,0x%08lX,%ld)\n", iface
, fccType
, lParam
);
476 /* check parameter */
478 return AVIERR_BADPARAM
;
480 /* Do we have our audio stream? */
481 if (lParam
!= 0 || This
->fInfo
.dwStreams
== 0 ||
482 (fccType
!= 0 && fccType
!= streamtypeAUDIO
))
483 return AVIERR_NODATA
;
485 /* Have user write permissions? */
486 if ((This
->uMode
& MMIO_RWMODE
) == 0)
487 return AVIERR_READONLY
;
489 GlobalFreePtr(This
->lpFormat
);
490 This
->lpFormat
= NULL
;
494 This
->ckData
.dwDataOffset
= 0;
495 This
->ckData
.cksize
= 0;
497 This
->sInfo
.dwScale
= 0;
498 This
->sInfo
.dwRate
= 0;
499 This
->sInfo
.dwLength
= 0;
500 This
->sInfo
.dwSuggestedBufferSize
= 0;
502 This
->fInfo
.dwStreams
= 0;
503 This
->fInfo
.dwEditCount
++;
510 /***********************************************************************/
512 static HRESULT WINAPI
IPersistFile_fnQueryInterface(IPersistFile
*iface
,
513 REFIID refiid
, LPVOID
*obj
)
515 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
517 assert(This
->paf
!= NULL
);
519 return IAVIFile_QueryInterface((PAVIFILE
)This
->paf
, refiid
, obj
);
522 static ULONG WINAPI
IPersistFile_fnAddRef(IPersistFile
*iface
)
524 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
526 assert(This
->paf
!= NULL
);
528 return IAVIFile_AddRef((PAVIFILE
)This
->paf
);
531 static ULONG WINAPI
IPersistFile_fnRelease(IPersistFile
*iface
)
533 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
535 assert(This
->paf
!= NULL
);
537 return IAVIFile_Release((PAVIFILE
)This
->paf
);
540 static HRESULT WINAPI
IPersistFile_fnGetClassID(IPersistFile
*iface
,
543 TRACE("(%p,%p)\n", iface
, pClassID
);
545 if (pClassID
== NULL
)
546 return AVIERR_BADPARAM
;
548 memcpy(pClassID
, &CLSID_WAVFile
, sizeof(CLSID_WAVFile
));
553 static HRESULT WINAPI
IPersistFile_fnIsDirty(IPersistFile
*iface
)
555 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
557 TRACE("(%p)\n", iface
);
559 assert(This
->paf
!= NULL
);
561 return (This
->paf
->fDirty
? S_OK
: S_FALSE
);
564 static HRESULT WINAPI
IPersistFile_fnLoad(IPersistFile
*iface
,
565 LPCOLESTR pszFileName
, DWORD dwMode
)
567 IAVIFileImpl
*This
= ((IPersistFileImpl
*)iface
)->paf
;
569 WCHAR wszStreamFmt
[50];
572 TRACE("(%p,%s,0x%08lX)\n", iface
, debugstr_w(pszFileName
), dwMode
);
574 /* check parameter */
575 if (pszFileName
== NULL
)
576 return AVIERR_BADPARAM
;
578 assert(This
!= NULL
);
579 if (This
->hmmio
!= NULL
)
580 return AVIERR_ERROR
; /* No reuse of this object for another file! */
582 /* remeber mode and name */
583 This
->uMode
= dwMode
;
585 len
= lstrlenW(pszFileName
) + 1;
586 This
->szFileName
= LocalAlloc(LPTR
, len
* sizeof(WCHAR
));
587 if (This
->szFileName
== NULL
)
588 return AVIERR_MEMORY
;
589 lstrcpyW(This
->szFileName
, pszFileName
);
591 /* try to open the file */
592 This
->hmmio
= mmioOpenW(This
->szFileName
, NULL
, MMIO_ALLOCBUF
| dwMode
);
593 if (This
->hmmio
== NULL
) {
594 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */
596 len
= WideCharToMultiByte(CP_ACP
, 0, This
->szFileName
, -1,
597 NULL
, 0, NULL
, NULL
);
598 szFileName
= LocalAlloc(LPTR
, len
* sizeof(CHAR
));
599 if (szFileName
== NULL
)
600 return AVIERR_MEMORY
;
602 WideCharToMultiByte(CP_ACP
, 0, This
->szFileName
, -1, szFileName
,
605 This
->hmmio
= mmioOpenA(szFileName
, NULL
, MMIO_ALLOCBUF
| dwMode
);
606 LocalFree((HLOCAL
)szFileName
);
607 if (This
->hmmio
== NULL
)
608 return AVIERR_FILEOPEN
;
611 memset(& This
->fInfo
, 0, sizeof(This
->fInfo
));
612 memset(& This
->sInfo
, 0, sizeof(This
->sInfo
));
614 LoadStringW(AVIFILE_hModule
, IDS_WAVEFILETYPE
, This
->fInfo
.szFileType
,
615 sizeof(This
->fInfo
.szFileType
));
616 if (LoadStringW(AVIFILE_hModule
, IDS_WAVESTREAMFORMAT
,
617 wszStreamFmt
, sizeof(wszStreamFmt
)) > 0) {
618 wsprintfW(This
->sInfo
.szName
, wszStreamFmt
,
619 AVIFILE_BasenameW(This
->szFileName
));
622 /* should we create a new file? */
623 if (dwMode
& OF_CREATE
) {
624 /* nothing more to do */
627 return AVIFILE_LoadFile(This
);
630 static HRESULT WINAPI
IPersistFile_fnSave(IPersistFile
*iface
,
631 LPCOLESTR pszFileName
,BOOL fRemember
)
633 TRACE("(%p,%s,%d)\n", iface
, debugstr_w(pszFileName
), fRemember
);
635 /* We write directly to disk, so nothing to do. */
640 static HRESULT WINAPI
IPersistFile_fnSaveCompleted(IPersistFile
*iface
,
641 LPCOLESTR pszFileName
)
643 TRACE("(%p,%s)\n", iface
, debugstr_w(pszFileName
));
645 /* We write directly to disk, so nothing to do. */
650 static HRESULT WINAPI
IPersistFile_fnGetCurFile(IPersistFile
*iface
,
651 LPOLESTR
*ppszFileName
)
653 IPersistFileImpl
*This
= (IPersistFileImpl
*)iface
;
655 TRACE("(%p,%p)\n", iface
, ppszFileName
);
657 if (ppszFileName
== NULL
)
658 return AVIERR_BADPARAM
;
660 *ppszFileName
= NULL
;
662 assert(This
->paf
!= NULL
);
664 if (This
->paf
->szFileName
!= NULL
) {
665 int len
= lstrlenW(This
->paf
->szFileName
) + 1;
667 *ppszFileName
= GlobalAllocPtr(GHND
, len
* sizeof(WCHAR
));
668 if (*ppszFileName
== NULL
)
669 return AVIERR_MEMORY
;
671 strcpyW(*ppszFileName
, This
->paf
->szFileName
);
677 /***********************************************************************/
679 static HRESULT WINAPI
IAVIStream_fnQueryInterface(IAVIStream
*iface
,
680 REFIID refiid
, LPVOID
*obj
)
682 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
684 assert(This
->paf
!= NULL
);
686 return IAVIFile_QueryInterface((PAVIFILE
)This
->paf
, refiid
, obj
);
689 static ULONG WINAPI
IAVIStream_fnAddRef(IAVIStream
*iface
)
691 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
693 assert(This
->paf
!= NULL
);
695 return IAVIFile_AddRef((PAVIFILE
)This
->paf
);
698 static ULONG WINAPI
IAVIStream_fnRelease(IAVIStream
* iface
)
700 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
702 assert(This
->paf
!= NULL
);
704 return IAVIFile_Release((PAVIFILE
)This
->paf
);
707 static HRESULT WINAPI
IAVIStream_fnCreate(IAVIStream
*iface
, LPARAM lParam1
,
710 TRACE("(%p,0x%08lX,0x%08lX)\n", iface
, lParam1
, lParam2
);
712 /* This IAVIStream interface needs an WAVFile */
713 return AVIERR_UNSUPPORTED
;
716 static HRESULT WINAPI
IAVIStream_fnInfo(IAVIStream
*iface
,LPAVISTREAMINFOW psi
,
719 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
721 TRACE("(%p,%p,%ld)\n", iface
, psi
, size
);
724 return AVIERR_BADPARAM
;
726 return AVIERR_BADSIZE
;
728 memcpy(psi
, &This
->paf
->sInfo
, min((DWORD
)size
, sizeof(This
->paf
->sInfo
)));
730 if ((DWORD
)size
< sizeof(This
->paf
->sInfo
))
731 return AVIERR_BUFFERTOOSMALL
;
735 static LONG WINAPI
IAVIStream_fnFindSample(IAVIStream
*iface
, LONG pos
,
738 IAVIFileImpl
*This
= ((IAVIStreamImpl
*)iface
)->paf
;
740 TRACE("(%p,%ld,0x%08lX)\n",iface
,pos
,flags
);
742 /* Do we have data? */
743 if (This
->lpFormat
== NULL
)
746 /* We don't have an index */
747 if (flags
& FIND_INDEX
)
750 if (flags
& FIND_FROM_START
) {
751 pos
= This
->sInfo
.dwStart
;
752 flags
&= ~(FIND_FROM_START
|FIND_PREV
);
756 if (flags
& FIND_FORMAT
) {
757 if ((flags
& FIND_NEXT
) && pos
> 0)
763 if ((flags
& FIND_RET
) == FIND_LENGTH
||
764 (flags
& FIND_RET
) == FIND_SIZE
)
765 return This
->sInfo
.dwSampleSize
;
766 if ((flags
& FIND_RET
) == FIND_OFFSET
)
767 return This
->ckData
.dwDataOffset
+ pos
* This
->sInfo
.dwSampleSize
;
772 static HRESULT WINAPI
IAVIStream_fnReadFormat(IAVIStream
*iface
, LONG pos
,
773 LPVOID format
, LONG
*formatsize
)
775 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
777 TRACE("(%p,%ld,%p,%p)\n", iface
, pos
, format
, formatsize
);
779 if (formatsize
== NULL
)
780 return AVIERR_BADPARAM
;
782 /* only interested in needed buffersize? */
783 if (format
== NULL
|| *formatsize
<= 0) {
784 *formatsize
= This
->paf
->cbFormat
;
789 /* copy initial format (only as much as will fit) */
790 memcpy(format
, This
->paf
->lpFormat
, min(*formatsize
, This
->paf
->cbFormat
));
791 if (*formatsize
< This
->paf
->cbFormat
) {
792 *formatsize
= This
->paf
->cbFormat
;
793 return AVIERR_BUFFERTOOSMALL
;
796 *formatsize
= This
->paf
->cbFormat
;
800 static HRESULT WINAPI
IAVIStream_fnSetFormat(IAVIStream
*iface
, LONG pos
,
801 LPVOID format
, LONG formatsize
)
803 IAVIFileImpl
*This
= ((IAVIStreamImpl
*)iface
)->paf
;
805 TRACE("(%p,%ld,%p,%ld)\n", iface
, pos
, format
, formatsize
);
807 /* check parameters */
808 if (format
== NULL
|| formatsize
<= sizeof(PCMWAVEFORMAT
))
809 return AVIERR_BADPARAM
;
811 /* We can only do this to an empty wave file, but ignore call
812 * if still same format */
813 if (This
->lpFormat
!= NULL
) {
814 if (formatsize
!= This
->cbFormat
||
815 memcmp(format
, This
->lpFormat
, formatsize
) != 0)
816 return AVIERR_UNSUPPORTED
;
821 /* only support start at position 0 */
823 return AVIERR_UNSUPPORTED
;
825 /* Do we have write permission? */
826 if ((This
->uMode
& MMIO_RWMODE
) == 0)
827 return AVIERR_READONLY
;
829 /* get memory for format and copy it */
830 This
->lpFormat
= (LPWAVEFORMATEX
)GlobalAllocPtr(GMEM_MOVEABLE
, formatsize
);
831 if (This
->lpFormat
== NULL
)
832 return AVIERR_MEMORY
;
834 This
->cbFormat
= formatsize
;
835 memcpy(This
->lpFormat
, format
, formatsize
);
837 /* update info's about 'data' chunk */
838 This
->ckData
.dwDataOffset
= formatsize
+ 7 * sizeof(DWORD
);
839 This
->ckData
.cksize
= 0;
841 /* for non-pcm format we need also a 'fact' chunk */
842 if (This
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
)
843 This
->ckData
.dwDataOffset
+= 3 * sizeof(DWORD
);
845 /* update stream and file info */
846 This
->sInfo
.dwSampleSize
= This
->lpFormat
->nBlockAlign
;
847 This
->sInfo
.dwScale
= This
->lpFormat
->nBlockAlign
;
848 This
->sInfo
.dwRate
= This
->lpFormat
->nAvgBytesPerSec
;
849 This
->sInfo
.dwLength
= 0;
850 This
->sInfo
.dwSuggestedBufferSize
= 0;
855 static HRESULT WINAPI
IAVIStream_fnRead(IAVIStream
*iface
, LONG start
,
856 LONG samples
, LPVOID buffer
,
857 LONG buffersize
, LPLONG bytesread
,
860 IAVIFileImpl
*This
= ((IAVIStreamImpl
*)iface
)->paf
;
862 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface
, start
, samples
, buffer
,
863 buffersize
, bytesread
, samplesread
);
865 /* clear return parameters if given */
866 if (bytesread
!= NULL
)
868 if (samplesread
!= NULL
)
871 /* positions without data */
872 if (start
< 0 || (DWORD
)start
> This
->sInfo
.dwLength
)
878 if (buffersize
> 0) {
880 samples
= min((DWORD
)samples
, buffersize
/ This
->sInfo
.dwSampleSize
);
882 samples
= buffersize
/ This
->sInfo
.dwSampleSize
;
885 /* limit to end of stream */
886 if ((DWORD
)(start
+ samples
) > This
->sInfo
.dwLength
)
887 samples
= This
->sInfo
.dwLength
- start
;
889 /* request only the sizes? */
890 if (buffer
== NULL
|| buffersize
<= 0) {
891 /* then I need at least one parameter for it */
892 if (bytesread
== NULL
&& samplesread
== NULL
)
893 return AVIERR_BADPARAM
;
895 if (bytesread
!= NULL
)
896 *bytesread
= samples
* This
->sInfo
.dwSampleSize
;
897 if (samplesread
!= NULL
)
898 *samplesread
= samples
;
903 /* nothing to read? */
907 /* Can I read at least one sample? */
908 if ((DWORD
)buffersize
< This
->sInfo
.dwSampleSize
)
909 return AVIERR_BUFFERTOOSMALL
;
911 buffersize
= samples
* This
->sInfo
.dwSampleSize
;
913 if (mmioSeek(This
->hmmio
, This
->ckData
.dwDataOffset
914 + start
* This
->sInfo
.dwSampleSize
, SEEK_SET
) == -1)
915 return AVIERR_FILEREAD
;
916 if (mmioRead(This
->hmmio
, (HPSTR
)buffer
, buffersize
) != buffersize
)
917 return AVIERR_FILEREAD
;
919 /* fill out return parameters if given */
920 if (bytesread
!= NULL
)
921 *bytesread
= buffersize
;
922 if (samplesread
!= NULL
)
923 *samplesread
= samples
;
928 static HRESULT WINAPI
IAVIStream_fnWrite(IAVIStream
*iface
, LONG start
,
929 LONG samples
, LPVOID buffer
,
930 LONG buffersize
, DWORD flags
,
934 IAVIFileImpl
*This
= ((IAVIStreamImpl
*)iface
)->paf
;
936 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface
, start
, samples
,
937 buffer
, buffersize
, flags
, sampwritten
, byteswritten
);
939 /* clear return parameters if given */
940 if (sampwritten
!= NULL
)
942 if (byteswritten
!= NULL
)
945 /* check parameters */
946 if (buffer
== NULL
&& (buffersize
> 0 || samples
> 0))
947 return AVIERR_BADPARAM
;
949 /* Do we have write permission? */
950 if ((This
->uMode
& MMIO_RWMODE
) == 0)
951 return AVIERR_READONLY
;
953 /* < 0 means "append" */
955 start
= This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
;
957 /* check buffersize -- must multiple of samplesize */
958 if (buffersize
& ~(This
->sInfo
.dwSampleSize
- 1))
959 return AVIERR_BADSIZE
;
961 /* do we have anything to write? */
962 if (buffer
!= NULL
&& buffersize
> 0) {
965 if (mmioSeek(This
->hmmio
, This
->ckData
.dwDataOffset
+
966 start
* This
->sInfo
.dwSampleSize
, SEEK_SET
) == -1)
967 return AVIERR_FILEWRITE
;
968 if (mmioWrite(This
->hmmio
, (HPSTR
)buffer
, buffersize
) != buffersize
)
969 return AVIERR_FILEWRITE
;
971 This
->sInfo
.dwLength
= max(This
->sInfo
.dwLength
, (DWORD
)start
+ samples
);
972 This
->ckData
.cksize
= max(This
->ckData
.cksize
,
973 start
* This
->sInfo
.dwSampleSize
+ buffersize
);
975 /* fill out return parameters if given */
976 if (sampwritten
!= NULL
)
977 *sampwritten
= samples
;
978 if (byteswritten
!= NULL
)
979 *byteswritten
= buffersize
;
985 static HRESULT WINAPI
IAVIStream_fnDelete(IAVIStream
*iface
, LONG start
,
988 IAVIFileImpl
*This
= ((IAVIStreamImpl
*)iface
)->paf
;
990 TRACE("(%p,%ld,%ld)\n", iface
, start
, samples
);
992 /* check parameters */
993 if (start
< 0 || samples
< 0)
994 return AVIERR_BADPARAM
;
996 /* Delete before start of stream? */
997 if ((DWORD
)(start
+ samples
) < This
->sInfo
.dwStart
)
1000 /* Delete after end of stream? */
1001 if ((DWORD
)start
> This
->sInfo
.dwLength
)
1004 /* For the rest we need write permissions */
1005 if ((This
->uMode
& MMIO_RWMODE
) == 0)
1006 return AVIERR_READONLY
;
1008 if ((DWORD
)(start
+ samples
) >= This
->sInfo
.dwLength
) {
1009 /* deletion at end */
1010 samples
= This
->sInfo
.dwLength
- start
;
1011 This
->sInfo
.dwLength
-= samples
;
1012 This
->ckData
.cksize
-= samples
* This
->sInfo
.dwSampleSize
;
1013 } else if ((DWORD
)start
<= This
->sInfo
.dwStart
) {
1014 /* deletion at start */
1015 samples
= This
->sInfo
.dwStart
- start
;
1016 start
= This
->sInfo
.dwStart
;
1017 This
->ckData
.dwDataOffset
+= samples
* This
->sInfo
.dwSampleSize
;
1018 This
->ckData
.cksize
-= samples
* This
->sInfo
.dwSampleSize
;
1020 /* deletion inside stream -- needs playlist and cue's */
1021 FIXME(": deletion inside of stream not supported!\n");
1023 return AVIERR_UNSUPPORTED
;
1031 static HRESULT WINAPI
IAVIStream_fnReadData(IAVIStream
*iface
, DWORD fcc
,
1032 LPVOID lp
, LPLONG lpread
)
1034 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
1036 assert(This
->paf
!= NULL
);
1038 return IAVIFile_ReadData((PAVIFILE
)This
->paf
, fcc
, lp
, lpread
);
1041 static HRESULT WINAPI
IAVIStream_fnWriteData(IAVIStream
*iface
, DWORD fcc
,
1042 LPVOID lp
, LONG size
)
1044 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
1046 return IAVIFile_WriteData((PAVIFILE
)This
->paf
, fcc
, lp
, size
);
1049 static HRESULT WINAPI
IAVIStream_fnSetInfo(IAVIStream
*iface
,
1050 LPAVISTREAMINFOW info
, LONG infolen
)
1052 FIXME("(%p,%p,%ld): stub\n", iface
, info
, infolen
);
1057 /***********************************************************************/
1059 static HRESULT
AVIFILE_LoadFile(IAVIFileImpl
*This
)
1064 This
->sInfo
.dwLength
= 0; /* just to be sure */
1065 This
->fDirty
= FALSE
;
1067 /* search for RIFF chunk */
1068 ckRIFF
.fccType
= 0; /* find any */
1069 if (mmioDescend(This
->hmmio
, &ckRIFF
, NULL
, MMIO_FINDRIFF
) != S_OK
) {
1070 return AVIFILE_LoadSunFile(This
);
1073 if (ckRIFF
.fccType
!= formtypeWAVE
)
1074 return AVIERR_BADFORMAT
;
1076 /* search WAVE format chunk */
1077 ck
.ckid
= ckidWAVEFORMAT
;
1078 if (FindChunkAndKeepExtras(&This
->extra
, This
->hmmio
, &ck
,
1079 &ckRIFF
, MMIO_FINDCHUNK
) != S_OK
)
1080 return AVIERR_FILEREAD
;
1082 /* get memory for format and read it */
1083 This
->lpFormat
= (LPWAVEFORMATEX
)GlobalAllocPtr(GMEM_MOVEABLE
, ck
.cksize
);
1084 if (This
->lpFormat
== NULL
)
1085 return AVIERR_FILEREAD
;
1086 This
->cbFormat
= ck
.cksize
;
1088 if (mmioRead(This
->hmmio
, (HPSTR
)This
->lpFormat
, ck
.cksize
) != ck
.cksize
)
1089 return AVIERR_FILEREAD
;
1090 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1091 return AVIERR_FILEREAD
;
1093 /* Non-pcm formats have a fact chunk.
1094 * We don't need it, so simply add it to the extra chunks.
1097 /* find the big data chunk */
1098 This
->ckData
.ckid
= ckidWAVEDATA
;
1099 if (FindChunkAndKeepExtras(&This
->extra
, This
->hmmio
, &This
->ckData
,
1100 &ckRIFF
, MMIO_FINDCHUNK
) != S_OK
)
1101 return AVIERR_FILEREAD
;
1103 memset(&This
->sInfo
, 0, sizeof(This
->sInfo
));
1104 This
->sInfo
.fccType
= streamtypeAUDIO
;
1105 This
->sInfo
.dwRate
= This
->lpFormat
->nAvgBytesPerSec
;
1106 This
->sInfo
.dwSampleSize
=
1107 This
->sInfo
.dwScale
= This
->lpFormat
->nBlockAlign
;
1108 This
->sInfo
.dwLength
= This
->ckData
.cksize
/ This
->lpFormat
->nBlockAlign
;
1109 This
->sInfo
.dwSuggestedBufferSize
= This
->ckData
.cksize
;
1111 This
->fInfo
.dwStreams
= 1;
1113 if (mmioAscend(This
->hmmio
, &This
->ckData
, 0) != S_OK
) {
1114 /* seems to be truncated */
1115 WARN(": file seems to be truncated!\n");
1116 This
->ckData
.cksize
= mmioSeek(This
->hmmio
, 0, SEEK_END
) -
1117 This
->ckData
.dwDataOffset
;
1118 This
->sInfo
.dwLength
= This
->ckData
.cksize
/ This
->lpFormat
->nBlockAlign
;
1119 This
->sInfo
.dwSuggestedBufferSize
= This
->ckData
.cksize
;
1123 FindChunkAndKeepExtras(&This
->extra
, This
->hmmio
, &ck
, &ckRIFF
, 0);
1128 static HRESULT
AVIFILE_LoadSunFile(IAVIFileImpl
*This
)
1130 SUNAUDIOHEADER auhdr
;
1132 mmioSeek(This
->hmmio
, 0, SEEK_SET
);
1133 if (mmioRead(This
->hmmio
, (HPSTR
)&auhdr
, sizeof(auhdr
)) != sizeof(auhdr
))
1134 return AVIERR_FILEREAD
;
1136 if (auhdr
.fccType
== 0x0064732E) {
1137 /* header in little endian */
1138 This
->ckData
.dwDataOffset
= LE2H_DWORD(auhdr
.offset
);
1139 This
->ckData
.cksize
= LE2H_DWORD(auhdr
.size
);
1141 auhdr
.encoding
= LE2H_DWORD(auhdr
.encoding
);
1142 auhdr
.sampleRate
= LE2H_DWORD(auhdr
.sampleRate
);
1143 auhdr
.channels
= LE2H_DWORD(auhdr
.channels
);
1144 } else if (auhdr
.fccType
== mmioFOURCC('.','s','n','d')) {
1145 /* header in big endian */
1146 This
->ckData
.dwDataOffset
= BE2H_DWORD(auhdr
.offset
);
1147 This
->ckData
.cksize
= BE2H_DWORD(auhdr
.size
);
1149 auhdr
.encoding
= BE2H_DWORD(auhdr
.encoding
);
1150 auhdr
.sampleRate
= BE2H_DWORD(auhdr
.sampleRate
);
1151 auhdr
.channels
= BE2H_DWORD(auhdr
.channels
);
1153 return AVIERR_FILEREAD
;
1155 if (auhdr
.channels
< 1)
1156 return AVIERR_BADFORMAT
;
1158 /* get size of header */
1159 switch(auhdr
.encoding
) {
1160 case AU_ENCODING_ADPCM_G721_32
:
1161 This
->cbFormat
= sizeof(G721_ADPCMWAVEFORMAT
); break;
1162 case AU_ENCODING_ADPCM_G723_24
:
1163 This
->cbFormat
= sizeof(G723_ADPCMWAVEFORMAT
); break;
1164 case AU_ENCODING_ADPCM_G722
:
1165 case AU_ENCODING_ADPCM_G723_5
:
1166 WARN("unsupported Sun audio format %d\n", auhdr
.encoding
);
1167 return AVIERR_UNSUPPORTED
; /* FIXME */
1169 This
->cbFormat
= sizeof(WAVEFORMATEX
); break;
1173 (LPWAVEFORMATEX
)GlobalAllocPtr(GMEM_MOVEABLE
, This
->cbFormat
);
1174 if (This
->lpFormat
== NULL
)
1175 return AVIERR_MEMORY
;
1177 This
->lpFormat
->nChannels
= auhdr
.channels
;
1178 This
->lpFormat
->nSamplesPerSec
= auhdr
.sampleRate
;
1179 switch(auhdr
.encoding
) {
1180 case AU_ENCODING_ULAW_8
:
1181 This
->lpFormat
->wFormatTag
= WAVE_FORMAT_MULAW
;
1182 This
->lpFormat
->wBitsPerSample
= 8;
1184 case AU_ENCODING_PCM_8
:
1185 This
->lpFormat
->wFormatTag
= WAVE_FORMAT_PCM
;
1186 This
->lpFormat
->wBitsPerSample
= 8;
1188 case AU_ENCODING_PCM_16
:
1189 This
->lpFormat
->wFormatTag
= WAVE_FORMAT_PCM
;
1190 This
->lpFormat
->wBitsPerSample
= 16;
1192 case AU_ENCODING_PCM_24
:
1193 This
->lpFormat
->wFormatTag
= WAVE_FORMAT_PCM
;
1194 This
->lpFormat
->wBitsPerSample
= 24;
1196 case AU_ENCODING_PCM_32
:
1197 This
->lpFormat
->wFormatTag
= WAVE_FORMAT_PCM
;
1198 This
->lpFormat
->wBitsPerSample
= 32;
1200 case AU_ENCODING_ALAW_8
:
1201 This
->lpFormat
->wFormatTag
= WAVE_FORMAT_ALAW
;
1202 This
->lpFormat
->wBitsPerSample
= 8;
1204 case AU_ENCODING_ADPCM_G721_32
:
1205 This
->lpFormat
->wFormatTag
= WAVE_FORMAT_G721_ADPCM
;
1206 This
->lpFormat
->wBitsPerSample
= (3*5*8);
1207 This
->lpFormat
->nBlockAlign
= 15*15*8;
1208 This
->lpFormat
->cbSize
= sizeof(WORD
);
1209 ((LPG721_ADPCMWAVEFORMAT
)This
->lpFormat
)->nAuxBlockSize
= 0;
1211 case AU_ENCODING_ADPCM_G723_24
:
1212 This
->lpFormat
->wFormatTag
= WAVE_FORMAT_G723_ADPCM
;
1213 This
->lpFormat
->wBitsPerSample
= (3*5*8);
1214 This
->lpFormat
->nBlockAlign
= 15*15*8;
1215 This
->lpFormat
->cbSize
= 2*sizeof(WORD
);
1216 ((LPG723_ADPCMWAVEFORMAT
)This
->lpFormat
)->cbExtraSize
= 0;
1217 ((LPG723_ADPCMWAVEFORMAT
)This
->lpFormat
)->nAuxBlockSize
= 0;
1220 WARN("unsupported Sun audio format %d\n", auhdr
.encoding
);
1221 return AVIERR_UNSUPPORTED
;
1224 This
->lpFormat
->nBlockAlign
=
1225 (This
->lpFormat
->nChannels
* This
->lpFormat
->wBitsPerSample
) / 8;
1226 if (This
->lpFormat
->nBlockAlign
== 0 && This
->lpFormat
->wBitsPerSample
< 8)
1227 This
->lpFormat
->nBlockAlign
++;
1228 This
->lpFormat
->nAvgBytesPerSec
=
1229 This
->lpFormat
->nBlockAlign
* This
->lpFormat
->nSamplesPerSec
;
1233 This
->sInfo
.fccType
= streamtypeAUDIO
;
1234 This
->sInfo
.fccHandler
= 0;
1235 This
->sInfo
.dwFlags
= 0;
1236 This
->sInfo
.wPriority
= 0;
1237 This
->sInfo
.wLanguage
= 0;
1238 This
->sInfo
.dwInitialFrames
= 0;
1239 This
->sInfo
.dwScale
= This
->lpFormat
->nBlockAlign
;
1240 This
->sInfo
.dwRate
= This
->lpFormat
->nAvgBytesPerSec
;
1241 This
->sInfo
.dwStart
= 0;
1242 This
->sInfo
.dwLength
=
1243 This
->ckData
.cksize
/ This
->lpFormat
->nBlockAlign
;
1244 This
->sInfo
.dwSuggestedBufferSize
= This
->sInfo
.dwLength
;
1245 This
->sInfo
.dwSampleSize
= This
->lpFormat
->nBlockAlign
;
1247 This
->fInfo
.dwStreams
= 1;
1248 This
->fInfo
.dwScale
= 1;
1249 This
->fInfo
.dwRate
= This
->lpFormat
->nSamplesPerSec
;
1250 This
->fInfo
.dwLength
=
1251 MulDiv(This
->ckData
.cksize
, This
->lpFormat
->nSamplesPerSec
,
1252 This
->lpFormat
->nAvgBytesPerSec
);
1257 static HRESULT
AVIFILE_SaveFile(IAVIFileImpl
*This
)
1262 mmioSeek(This
->hmmio
, 0, SEEK_SET
);
1264 /* create the RIFF chunk with formtype WAVE */
1265 ckRIFF
.fccType
= formtypeWAVE
;
1267 if (mmioCreateChunk(This
->hmmio
, &ckRIFF
, MMIO_CREATERIFF
) != S_OK
)
1268 return AVIERR_FILEWRITE
;
1270 /* the next chunk is the format */
1271 ck
.ckid
= ckidWAVEFORMAT
;
1272 ck
.cksize
= This
->cbFormat
;
1273 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
1274 return AVIERR_FILEWRITE
;
1275 if (This
->lpFormat
!= NULL
&& This
->cbFormat
> 0) {
1276 if (mmioWrite(This
->hmmio
, (HPSTR
)This
->lpFormat
, ck
.cksize
) != ck
.cksize
)
1277 return AVIERR_FILEWRITE
;
1279 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1280 return AVIERR_FILEWRITE
;
1282 /* fact chunk is needed for non-pcm waveforms */
1283 if (This
->lpFormat
!= NULL
&& This
->cbFormat
> sizeof(PCMWAVEFORMAT
) &&
1284 This
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1289 /* try to open an appropriate audio codec to figure out
1290 * data for fact-chunk */
1291 wfx
.wFormatTag
= WAVE_FORMAT_PCM
;
1292 if (acmFormatSuggest(NULL
, This
->lpFormat
, &wfx
,
1293 sizeof(wfx
), ACM_FORMATSUGGESTF_WFORMATTAG
)) {
1294 acmStreamOpen(&has
, NULL
, This
->lpFormat
, &wfx
, NULL
,
1295 0, 0, ACM_STREAMOPENF_NONREALTIME
);
1296 acmStreamSize(has
, This
->ckData
.cksize
, &dwFactLength
,
1297 ACM_STREAMSIZEF_SOURCE
);
1298 dwFactLength
/= wfx
.nBlockAlign
;
1299 acmStreamClose(has
, 0);
1301 /* create the fact chunk */
1302 ck
.ckid
= ckidWAVEFACT
;
1303 ck
.cksize
= sizeof(dwFactLength
);
1305 /* test for enough space before data chunk */
1306 if (mmioSeek(This
->hmmio
, 0, SEEK_CUR
) > This
->ckData
.dwDataOffset
1307 - ck
.cksize
- 4 * sizeof(DWORD
))
1308 return AVIERR_FILEWRITE
;
1309 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
1310 return AVIERR_FILEWRITE
;
1311 if (mmioWrite(This
->hmmio
, (HPSTR
)&dwFactLength
, ck
.cksize
) != ck
.cksize
)
1312 return AVIERR_FILEWRITE
;
1313 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1314 return AVIERR_FILEWRITE
;
1316 ERR(": fact chunk is needed for non-pcm files -- currently no codec found, so skipped!\n");
1319 /* if there was extra stuff, we need to fill it with JUNK */
1320 if (mmioSeek(This
->hmmio
, 0, SEEK_CUR
) + 2 * sizeof(DWORD
) < This
->ckData
.dwDataOffset
) {
1321 ck
.ckid
= ckidAVIPADDING
;
1323 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
1324 return AVIERR_FILEWRITE
;
1326 if (mmioSeek(This
->hmmio
, This
->ckData
.dwDataOffset
1327 - 2 * sizeof(DWORD
), SEEK_SET
) == -1)
1328 return AVIERR_FILEWRITE
;
1329 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1330 return AVIERR_FILEWRITE
;
1333 /* create the data chunk */
1334 ck
.ckid
= ckidWAVEDATA
;
1335 ck
.cksize
= This
->ckData
.cksize
;
1336 if (mmioCreateChunk(This
->hmmio
, &ck
, 0) != S_OK
)
1337 return AVIERR_FILEWRITE
;
1338 if (mmioSeek(This
->hmmio
, This
->ckData
.cksize
, SEEK_CUR
) == -1)
1339 return AVIERR_FILEWRITE
;
1340 if (mmioAscend(This
->hmmio
, &ck
, 0) != S_OK
)
1341 return AVIERR_FILEWRITE
;
1343 /* some optional extra chunks? */
1344 if (This
->extra
.lp
!= NULL
&& This
->extra
.cb
> 0) {
1345 /* chunk headers are already in structure */
1346 if (mmioWrite(This
->hmmio
, This
->extra
.lp
, This
->extra
.cb
) != This
->extra
.cb
)
1347 return AVIERR_FILEWRITE
;
1350 /* close RIFF chunk */
1351 if (mmioAscend(This
->hmmio
, &ckRIFF
, 0) != S_OK
)
1352 return AVIERR_FILEWRITE
;
1353 if (mmioFlush(This
->hmmio
, 0) != S_OK
)
1354 return AVIERR_FILEWRITE
;