📄 riff.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "hlxclib/string.h"
#include "hxtypes.h"
#include "hxcom.h"
#include "hxmarsh.h"
#include "ihxpckts.h"
#include "hxfiles.h"
#include "riff.h"
#include "riffres.h"
#include "hxassert.h"
#include "hxcomm.h"
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
#define LIST_CHUNK_ID 0x4c495354 /* 'LIST' */
#define IFF_FILE_MAGIC_NUMBER 0x464f524d /* 'FORM' */
STDMETHODIMP CRIFFReader::QueryInterface(REFIID riid, void** ppvObj)
{
QInterfaceList qiList[] =
{
{ GET_IIDHANDLE(IID_IUnknown), this},
{ GET_IIDHANDLE(IID_IHXFileResponse), (IHXFileResponse*)this},
{ GET_IIDHANDLE(IID_IHXThreadSafeMethods), (IHXThreadSafeMethods*)this},
};
HX_RESULT retVal = ::QIFind(qiList, QILISTSIZE(qiList), riid, ppvObj);
if (FAILED(retVal) && m_pResponse)
{
retVal = m_pResponse->QueryInterface(riid, ppvObj);
}
return retVal;
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::AddRef
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(ULONG32) CRIFFReader::AddRef()
{
return InterlockedIncrement(&m_lRefCount);
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::Release
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(ULONG32) CRIFFReader::Release()
{
if ( InterlockedDecrement(&m_lRefCount) > 0 )
{
return m_lRefCount;
}
delete this;
return 0;
}
CRIFFReader::CRIFFReader(IUnknown* pContext,
CRIFFResponse* pResponse,
IHXFileObject* pFileObject)
: m_pReassemblyBuffer(NULL)
, m_ulChunkBytesRead(0)
, m_ulChunkSize(0)
{
m_pContext = pContext;
m_pResponse = pResponse;
m_pFileObject = pFileObject;
if ( m_pFileObject )
{
m_pFileObject->AddRef();
}
if ( m_pContext )
m_pContext->AddRef();
if ( m_pResponse )
m_pResponse->AddRef();
m_bFileIsOpen = FALSE;
m_lRefCount = 0;
m_pFilename = 0;
m_ulSizeDiff = 0;
m_ulThisChunkOffset = 0;
//Every time we Read() a data chunk and the amount of the Read is
// odd, we add one to byte-align for the next Read, but we want to
// know that we did this in ReadDone() so we use the following to
// keep track so, in ReadDone(), we can reduce the buffer size by
// one if necessary so the renderer will get just the bytes it
// needs:
m_ulFileSpecifiedReadSize = 0;
}
CRIFFReader::~CRIFFReader()
{
if ( m_bFileIsOpen )
Close();
if ( m_pFilename )
{
delete [] m_pFilename;
m_pFilename = 0;
}
}
HX_RESULT
CRIFFReader::Open(char* filename)
{
if ( !m_pFileObject )
return HXR_UNEXPECTED;
if ( filename )
{
m_pFilename = new char[strlen(filename) + 1];
strcpy(m_pFilename, filename); /* Flawfinder: ignore */
}
m_state = RS_InitPending;
m_ulCurOffset = 0;
m_levelInfo[0].m_startOffset = 0;
m_ulLevel = 0;
/*
if(filename)
{
IHXRequest* pRequest;
if (m_pFileObject->GetRequest(pRequest) == HXR_OK)
{
pRequest->SetURL(filename);
pRequest->Release();
}
else
{
return HXR_FAILED;
}
}
*/
return m_pFileObject->Init(HX_FILE_READ | HX_FILE_BINARY, this);
// return HXR_OK;
}
STDMETHODIMP
CRIFFReader::InitDone(HX_RESULT status)
{
if ( status != HXR_OK )
{
m_state = RS_Ready;
m_pResponse->RIFFOpenDone(status);
return HXR_OK;
}
m_bFileIsOpen = TRUE;
m_state = RS_GetFileTypePending;
return m_pFileObject->Read(sizeof(UINT32) * 2);
}
HX_RESULT
CRIFFReader::Close()
{
if ( m_pFileObject )
{
m_pFileObject->Close();
m_pFileObject->Release();
m_pFileObject = NULL;
}
if ( m_pContext )
{
m_pContext->Release();
m_pContext = NULL;
}
if ( m_pResponse )
{
m_pResponse->Release();
m_pResponse = NULL;
}
m_bFileIsOpen = FALSE;
return HXR_OK;
}
HX_RESULT
CRIFFReader::FindChunk(UINT32 id, BOOL bRelative)
{
m_ulFindChunkId = id;
m_state = RS_FileStartSeekPending;
if ( bRelative && m_levelInfo[m_ulLevel].started )
m_ulSeekOffset = m_levelInfo[m_ulLevel].m_nextChunkOffset;
else
m_ulSeekOffset = m_levelInfo[m_ulLevel].m_startOffset;
m_levelInfo[m_ulLevel].started = TRUE;
return(m_pFileObject->Seek(m_ulSeekOffset, FALSE) );
}
HX_RESULT
CRIFFReader::GetChunk()
{
m_state = RS_ReadChunkHeaderPending;
return(m_pFileObject->Read(sizeof(UINT32) * 2) ); /* Get type and len */
}
STDMETHODIMP
CRIFFReader::ReadDone(HX_RESULT status, IHXBuffer* pBuffer)
{
UCHAR* buf;
UINT32 len;
UINT16 uRem = 0;
HX_RESULT result = HXR_OK;
if ( !m_pResponse )
return HXR_FAIL;
if ( pBuffer )
pBuffer->Get(buf, len);
m_ulCurOffset += len;
//Keep track of "bytes read" value so CRIFFReader::ReadDone()
// can know whether or not it needs to decrement by one the size
// of the buffer it passes off, since some codecs (Intel's ILVC,
// for one) allow for odd numbered video frame sizes:
UINT32 ulActualLenOfBuffToUse = len;
if ( RS_ReadChunkBodyPending == m_state && pBuffer )
{
ulActualLenOfBuffToUse = m_ulFileSpecifiedReadSize;
if ( len > ulActualLenOfBuffToUse &&
len - ulActualLenOfBuffToUse == 1 )
{
//Then a Read(n+1) was done becuase n was odd, so
// make sure buffer has size of n so renderer will
// get just the bytes it needs:
pBuffer->SetSize(ulActualLenOfBuffToUse);
}
}
if ( m_ulFileType == RIFF_FILE_MAGIC_NUMBER ||
m_ulFileType == IFF_FILE_MAGIC_NUMBER )
{
// Make sure the offset is aligned(2 bytes)
uRem = (UINT16)(m_ulCurOffset % 2);
if ( uRem != 0 )
{
m_ulCurOffset += 1;
}
}
switch ( m_state )
{
case RS_GetFileTypePending:
if ( len != sizeof(UINT32) * 2 )
{
m_state = RS_Ready;
m_pResponse->RIFFOpenDone(HXR_FAILED);
return HXR_UNEXPECTED;
}
if ( (UINT32)getlong(buf) == RIFF_FILE_MAGIC_NUMBER )
{
m_state = RS_GetActualFileTypePending;
m_ulFileType = RIFF_FILE_MAGIC_NUMBER;
m_bLittleEndian = TRUE;
m_ulChunkBodyLen = GetLong(&buf[4]);
m_ulSizeDiff = 0;
m_levelInfo[m_ulLevel].m_nextChunkOffset = m_ulCurOffset + m_ulChunkBodyLen;
m_pFileObject->Read(sizeof(UINT32));
return status;
}
else if ( (UINT32)getlong(buf) == IFF_FILE_MAGIC_NUMBER )
{
m_state = RS_GetActualFileTypePending;
m_ulFileType = IFF_FILE_MAGIC_NUMBER;
m_bLittleEndian = FALSE;
m_ulChunkBodyLen = GetLong(&buf[4]);
m_ulSizeDiff = 0;
m_levelInfo[m_ulLevel].m_nextChunkOffset = m_ulCurOffset + m_ulChunkBodyLen;
m_levelInfo[m_ulLevel].m_startOffset = 12;
m_pFileObject->Read(sizeof(UINT32));
return status;
}
else
{
m_ulFileType = (UINT32)getlong(buf);
m_bLittleEndian = FALSE;
m_ulChunkBodyLen = GetLong(&buf[4]);
m_ulSizeDiff = 8;
m_levelInfo[m_ulLevel].m_nextChunkOffset = m_ulCurOffset + m_ulChunkBodyLen - 8;
}
m_state = RS_Ready;
result = m_pResponse->RIFFOpenDone(status);
return(HXR_OK==status? result:status);
case RS_GetActualFileTypePending:
if ( len != sizeof(UINT32) )
{
m_state = RS_Ready;
m_pResponse->RIFFOpenDone(HXR_FAILED);
return HXR_UNEXPECTED;
}
m_ulSubFileType = (UINT32)GetLong(buf);
m_state = RS_Ready;
result = m_pResponse->RIFFOpenDone(status);
return(HXR_OK==status? result:status);
case RS_ChunkHeaderReadPending:
if ( len != sizeof(UINT32) + sizeof(UINT32) ||
(HXR_OK != status) )
{
m_state = RS_Ready;
m_pResponse->RIFFFindChunkDone(HXR_FAILED, 0);
return HXR_UNEXPECTED;
}
if ( (UINT32)getlong(buf) == m_ulFindChunkId )
{
// Found the chunk we were asked for
m_state = RS_Ready;
m_ulChunkBodyLen = GetLong(&buf[4]);
// Make sure the body length is aligned(2 bytes)
if ( m_ulFileType == RIFF_FILE_MAGIC_NUMBER )
{
uRem = (UINT16)(m_ulChunkBodyLen % 2);
if ( uRem != 0 )
{
m_ulChunkBodyLen += 1;
}
}
m_levelInfo[m_ulLevel].m_nextChunkOffset = m_ulCurOffset + m_ulChunkBodyLen;
m_ulThisChunkOffset = m_ulCurOffset;
m_levelInfo[m_ulLevel].m_nextChunkOffset -= m_ulSizeDiff;
m_ulChunkType = m_ulFindChunkId;
if ( m_ulFindChunkId == LIST_CHUNK_ID )
{
m_state = RS_GetListTypePending;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -