📄 wavfile.c
字号:
/*
* Copyright 2002 Michael G黱newig
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <assert.h>
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winnls.h"
#include "winerror.h"
#include "mmsystem.h"
#include "vfw.h"
#include "msacm.h"
#include "avifile_private.h"
#include "extrachunk.h"
#include "wine/unicode.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(avifile);
/***********************************************************************/
#define formtypeWAVE mmioFOURCC('W','A','V','E')
#define ckidWAVEFORMAT mmioFOURCC('f','m','t',' ')
#define ckidWAVEFACT mmioFOURCC('f','a','c','t')
#define ckidWAVEDATA mmioFOURCC('d','a','t','a')
/***********************************************************************/
#define ENDIAN_SWAPWORD(x) ((((x) >> 8) & 0xFF) | (((x) & 0xFF) << 8))
#define ENDIAN_SWAPDWORD(x) (ENDIAN_SWAPWORD((x >> 16) & 0xFFFF) | \
ENDIAN_SWAPWORD(x & 0xFFFF) << 16)
#ifdef WORDS_BIGENDIAN
#define BE2H_WORD(x) (x)
#define BE2H_DWORD(x) (x)
#define LE2H_WORD(x) ENDIAN_SWAPWORD(x)
#define LE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
#else
#define BE2H_WORD(x) ENDIAN_SWAPWORD(x)
#define BE2H_DWORD(x) ENDIAN_SWAPDWORD(x)
#define LE2H_WORD(x) (x)
#define LE2H_DWORD(x) (x)
#endif
typedef struct {
FOURCC fccType;
DWORD offset;
DWORD size;
INT encoding;
DWORD sampleRate;
DWORD channels;
} SUNAUDIOHEADER;
#define AU_ENCODING_ULAW_8 1
#define AU_ENCODING_PCM_8 2
#define AU_ENCODING_PCM_16 3
#define AU_ENCODING_PCM_24 4
#define AU_ENCODING_PCM_32 5
#define AU_ENCODING_FLOAT 6
#define AU_ENCODING_DOUBLE 7
#define AU_ENCODING_ADPCM_G721_32 23
#define AU_ENCODING_ADPCM_G722 24
#define AU_ENCODING_ADPCM_G723_24 25
#define AU_ENCODING_ADPCM_G723_5 26
#define AU_ENCODING_ALAW_8 27
/***********************************************************************/
static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile* iface);
static ULONG WINAPI IAVIFile_fnRelease(IAVIFile* iface);
static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile*iface);
static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
static const struct IAVIFileVtbl iwavft = {
IAVIFile_fnQueryInterface,
IAVIFile_fnAddRef,
IAVIFile_fnRelease,
IAVIFile_fnInfo,
IAVIFile_fnGetStream,
IAVIFile_fnCreateStream,
IAVIFile_fnWriteData,
IAVIFile_fnReadData,
IAVIFile_fnEndRecord,
IAVIFile_fnDeleteStream
};
static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile*iface,REFIID refiid,LPVOID*obj);
static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile*iface);
static ULONG WINAPI IPersistFile_fnRelease(IPersistFile*iface);
static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile*iface,CLSID*pClassID);
static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile*iface);
static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile*iface,LPCOLESTR pszFileName,DWORD dwMode);
static HRESULT WINAPI IPersistFile_fnSave(IPersistFile*iface,LPCOLESTR pszFileName,BOOL fRemember);
static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile*iface,LPCOLESTR pszFileName);
static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile*iface,LPOLESTR*ppszFileName);
static const struct IPersistFileVtbl iwavpft = {
IPersistFile_fnQueryInterface,
IPersistFile_fnAddRef,
IPersistFile_fnRelease,
IPersistFile_fnGetClassID,
IPersistFile_fnIsDirty,
IPersistFile_fnLoad,
IPersistFile_fnSave,
IPersistFile_fnSaveCompleted,
IPersistFile_fnGetCurFile
};
static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID *obj);
static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream*iface);
static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface);
static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
static LONG WINAPI IAVIStream_fnFindSample(IAVIStream*iface,LONG pos,LONG flags);
static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG *formatsize);
static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
static HRESULT WINAPI IAVIStream_fnRead(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,LONG *bytesread,LONG *samplesread);
static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream*iface,LONG start,LONG samples,LPVOID buffer,LONG buffersize,DWORD flags,LONG *sampwritten,LONG *byteswritten);
static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG *lpread);
static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,LPVOID lp,LONG size);
static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
static const struct IAVIStreamVtbl iwavst = {
IAVIStream_fnQueryInterface,
IAVIStream_fnAddRef,
IAVIStream_fnRelease,
IAVIStream_fnCreate,
IAVIStream_fnInfo,
IAVIStream_fnFindSample,
IAVIStream_fnReadFormat,
IAVIStream_fnSetFormat,
IAVIStream_fnRead,
IAVIStream_fnWrite,
IAVIStream_fnDelete,
IAVIStream_fnReadData,
IAVIStream_fnWriteData,
IAVIStream_fnSetInfo
};
typedef struct _IAVIFileImpl IAVIFileImpl;
typedef struct _IPersistFileImpl {
/* IUnknown stuff */
const IPersistFileVtbl *lpVtbl;
/* IPersistFile stuff */
IAVIFileImpl *paf;
} IPersistFileImpl;
typedef struct _IAVIStreamImpl {
/* IUnknown stuff */
const IAVIStreamVtbl *lpVtbl;
/* IAVIStream stuff */
IAVIFileImpl *paf;
} IAVIStreamImpl;
struct _IAVIFileImpl {
/* IUnknown stuff */
const IAVIFileVtbl *lpVtbl;
LONG ref;
/* IAVIFile, IAVIStream stuff... */
IPersistFileImpl iPersistFile;
IAVIStreamImpl iAVIStream;
AVIFILEINFOW fInfo;
AVISTREAMINFOW sInfo;
LPWAVEFORMATEX lpFormat;
LONG cbFormat;
MMCKINFO ckData;
EXTRACHUNKS extra;
/* IPersistFile stuff ... */
HMMIO hmmio;
LPWSTR szFileName;
UINT uMode;
BOOL fDirty;
};
/***********************************************************************/
static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This);
static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This);
static HRESULT AVIFILE_SaveFile(IAVIFileImpl *This);
HRESULT AVIFILE_CreateWAVFile(REFIID riid, LPVOID *ppv)
{
IAVIFileImpl *pfile;
HRESULT hr;
assert(riid != NULL && ppv != NULL);
*ppv = NULL;
pfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAVIFileImpl));
if (pfile == NULL)
return AVIERR_MEMORY;
pfile->lpVtbl = &iwavft;
pfile->iPersistFile.lpVtbl = &iwavpft;
pfile->iAVIStream.lpVtbl = &iwavst;
pfile->ref = 0;
pfile->iPersistFile.paf = pfile;
pfile->iAVIStream.paf = pfile;
hr = IAVIFile_QueryInterface((IAVIFile*)pfile, riid, ppv);
if (FAILED(hr))
HeapFree(GetProcessHeap(), 0, pfile);
return hr;
}
static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
LPVOID *obj)
{
IAVIFileImpl *This = (IAVIFileImpl *)iface;
TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
if (IsEqualGUID(&IID_IUnknown, refiid) ||
IsEqualGUID(&IID_IAVIFile, refiid)) {
*obj = iface;
return S_OK;
} else if (This->fInfo.dwStreams == 1 &&
IsEqualGUID(&IID_IAVIStream, refiid)) {
*obj = &This->iAVIStream;
return S_OK;
} else if (IsEqualGUID(&IID_IPersistFile, refiid)) {
*obj = &This->iPersistFile;
return S_OK;
}
return OLE_E_ENUM_NOMORE;
}
static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface)
{
IAVIFileImpl *This = (IAVIFileImpl *)iface;
TRACE("(%p)\n",iface);
return InterlockedIncrement(&This->ref);
}
static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface)
{
IAVIFileImpl *This = (IAVIFileImpl *)iface;
ULONG ref = InterlockedDecrement(&This->ref);
TRACE("(%p)\n",iface);
if (!ref) {
if (This->fDirty) {
/* need to write headers to file */
AVIFILE_SaveFile(This);
}
if (This->lpFormat != NULL) {
HeapFree(GetProcessHeap(), 0, This->lpFormat);
This->lpFormat = NULL;
This->cbFormat = 0;
}
if (This->extra.lp != NULL) {
HeapFree(GetProcessHeap(), 0, This->extra.lp);
This->extra.lp = NULL;
This->extra.cb = 0;
}
HeapFree(GetProcessHeap(), 0, This->szFileName);
This->szFileName = NULL;
if (This->hmmio != NULL) {
mmioClose(This->hmmio, 0);
This->hmmio = NULL;
}
HeapFree(GetProcessHeap(), 0, This);
return 0;
}
return ref;
}
static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, LPAVIFILEINFOW afi,
LONG size)
{
IAVIFileImpl *This = (IAVIFileImpl *)iface;
TRACE("(%p,%p,%d)\n",iface,afi,size);
if (afi == NULL)
return AVIERR_BADPARAM;
if (size < 0)
return AVIERR_BADSIZE;
/* update file info */
This->fInfo.dwFlags = 0;
This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
if (This->lpFormat != NULL) {
assert(This->sInfo.dwScale != 0);
This->fInfo.dwStreams = 1;
This->fInfo.dwScale = This->sInfo.dwScale;
This->fInfo.dwRate = This->sInfo.dwRate;
This->fInfo.dwLength = This->sInfo.dwLength;
This->fInfo.dwSuggestedBufferSize = This->ckData.cksize;
This->fInfo.dwMaxBytesPerSec =
MulDiv(This->sInfo.dwSampleSize,This->sInfo.dwRate,This->sInfo.dwScale);
}
memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
if ((DWORD)size < sizeof(This->fInfo))
return AVIERR_BUFFERTOOSMALL;
return AVIERR_OK;
}
static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
DWORD fccType, LONG lParam)
{
IAVIFileImpl *This = (IAVIFileImpl *)iface;
TRACE("(%p,%p,0x%08X,%d)\n", iface, avis, fccType, lParam);
/* check parameter */
if (avis == NULL)
return AVIERR_BADPARAM;
*avis = NULL;
/* Does our stream exists? */
if (lParam != 0 || This->fInfo.dwStreams == 0)
return AVIERR_NODATA;
if (fccType != 0 && fccType != streamtypeAUDIO)
return AVIERR_NODATA;
*avis = (PAVISTREAM)&This->iAVIStream;
IAVIFile_AddRef(iface);
return AVIERR_OK;
}
static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
LPAVISTREAMINFOW asi)
{
IAVIFileImpl *This = (IAVIFileImpl *)iface;
TRACE("(%p,%p,%p)\n", iface, avis, asi);
/* check parameters */
if (avis == NULL || asi == NULL)
return AVIERR_BADPARAM;
*avis = NULL;
/* We only support one audio stream */
if (This->fInfo.dwStreams != 0 || This->lpFormat != NULL)
return AVIERR_UNSUPPORTED;
if (asi->fccType != streamtypeAUDIO)
return AVIERR_UNSUPPORTED;
/* Does the user have write permission? */
if ((This->uMode & MMIO_RWMODE) == 0)
return AVIERR_READONLY;
This->cbFormat = 0;
This->lpFormat = NULL;
memcpy(&This->sInfo, asi, sizeof(This->sInfo));
/* make sure streaminfo if okay for us */
This->sInfo.fccHandler = 0;
This->sInfo.dwFlags = 0;
This->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE;
This->sInfo.dwStart = 0;
This->sInfo.dwInitialFrames = 0;
This->sInfo.dwFormatChangeCount = 0;
memset(&This->sInfo.rcFrame, 0, sizeof(This->sInfo.rcFrame));
This->fInfo.dwStreams = 1;
This->fInfo.dwScale = This->sInfo.dwScale;
This->fInfo.dwRate = This->sInfo.dwRate;
This->fInfo.dwLength = This->sInfo.dwLength;
This->ckData.dwDataOffset = 0;
This->ckData.cksize = 0;
*avis = (PAVISTREAM)&This->iAVIStream;
IAVIFile_AddRef(iface);
return AVIERR_OK;
}
static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid,
LPVOID lpData, LONG size)
{
IAVIFileImpl *This = (IAVIFileImpl *)iface;
TRACE("(%p,0x%08X,%p,%d)\n", iface, ckid, lpData, size);
/* check parameters */
if (lpData == NULL)
return AVIERR_BADPARAM;
if (size < 0)
return AVIERR_BADSIZE;
/* Do we have write permission? */
if ((This->uMode & MMIO_RWMODE) == 0)
return AVIERR_READONLY;
This->fDirty = TRUE;
return WriteExtraChunk(&This->extra, ckid, lpData, size);
}
static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid,
LPVOID lpData, LONG *size)
{
IAVIFileImpl *This = (IAVIFileImpl *)iface;
TRACE("(%p,0x%08X,%p,%p)\n", iface, ckid, lpData, size);
return ReadExtraChunk(&This->extra, ckid, lpData, size);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -