mp3decoder.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:7k
- /* ***** 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 "hxtypes.h"
- #include "hxcom.h"
- #include "hxacodec.h"
- #include "mpadecobj.h"
- #include "mp3decoder.h"
- CMP3AudioDecoder::CMP3AudioDecoder()
- {
- m_lRefCount = 0;
- m_pMpaDecObj = NULL;
- m_bInitialized = FALSE;
- m_ulMaxSamplesOut = DEFAULT_MP3_MAXSAMPLESOUT;
- m_ulNumChannels = DEFAULT_MP3_NUMCHANNELS;
- m_ulSampleRate = DEFAULT_MP3_SAMPLERATE;
- m_ulDelay = DEFAULT_MP3_DELAY;
- m_bUseSize = FALSE;
- }
- CMP3AudioDecoder::~CMP3AudioDecoder()
- {
- HX_DELETE(m_pMpaDecObj);
- }
- STDMETHODIMP CMP3AudioDecoder::QueryInterface(REFIID riid, void** ppvObj)
- {
- HX_RESULT retVal = HXR_NOINTERFACE;
- if (ppvObj)
- {
- // Set default
- *ppvObj = NULL;
- // Switch based on IID
- if (IsEqualIID(riid, IID_IUnknown))
- {
- AddRef();
- *ppvObj = this;
- retVal = HXR_OK;
- }
- else if (IsEqualIID(riid, IID_IHXAudioDecoder))
- {
- AddRef();
- *ppvObj = (IHXAudioDecoder*) this;
- retVal = HXR_OK;
- }
- }
- return retVal;
- }
- STDMETHODIMP_(ULONG32) CMP3AudioDecoder::AddRef()
- {
- return InterlockedIncrement(&m_lRefCount);
- }
- STDMETHODIMP_(ULONG32) CMP3AudioDecoder::Release()
- {
- if (InterlockedDecrement(&m_lRefCount) > 0)
- {
- return m_lRefCount;
- }
- delete this;
- return 0;
- }
- STDMETHODIMP CMP3AudioDecoder::OpenDecoder(UINT32 cfgType, const void* config, UINT32 nBytes)
- {
- HX_RESULT retVal = HXR_FAIL;
- HX_DELETE(m_pMpaDecObj);
- m_pMpaDecObj = new CMpaDecObj();
- if (m_pMpaDecObj)
- {
- // See if we have config data
- if (config && nBytes)
- {
- if (nBytes >= 4)
- {
- // Get the version
- BYTE* pBuf = (BYTE*) config;
- UINT32 ulVersion = (pBuf[0] << 24) | (pBuf[1] << 16) | (pBuf[2] << 8) | pBuf[3];
- // Can we support this version?
- if (ulVersion <= MAX_BITSTREAM_VERSION)
- {
- // Parse version 0
- if (ulVersion == 0 && nBytes >= 16)
- {
- // Parse the values
- UINT32 ulNumChannels = (pBuf[4] << 24) | (pBuf[5] << 16) | (pBuf[6] << 8) | pBuf[7];
- UINT32 ulSampleRate = (pBuf[8] << 24) | (pBuf[9] << 16) | (pBuf[10] << 8) | pBuf[11];
- UINT32 ulDelay = (pBuf[12] << 24) | (pBuf[13] << 16) | (pBuf[14] << 8) | pBuf[15];
- UINT32 ulUseSize = (pBuf[16] << 24) | (pBuf[17] << 16) | (pBuf[18] << 8) | pBuf[19];
- // Assign the number of channels if non-NULL
- if (ulNumChannels) m_ulNumChannels = ulNumChannels;
- // Assign the sample rate if non-NULL
- if (ulSampleRate) m_ulSampleRate = ulSampleRate;
- // Assign the delay
- m_ulDelay = ulDelay;
- m_bUseSize = ulUseSize;
- }
- }
- }
- }
- // Clear the return value
- retVal = HXR_OK;
- }
- return retVal;
- }
- STDMETHODIMP CMP3AudioDecoder::Reset()
- {
- HX_RESULT retVal = HXR_OK;
- return retVal;
- }
- STDMETHODIMP CMP3AudioDecoder::Conceal(UINT32 nSamples)
- {
- return HXR_OK;
- }
- STDMETHODIMP CMP3AudioDecoder::Decode(const UCHAR* data, UINT32 nBytes,
- UINT32 &nBytesConsumed, INT16 *samplesOut,
- UINT32& nSamplesOut, BOOL eof)
- {
- HX_RESULT retVal = HXR_FAIL;
- // Set defaults
- nBytesConsumed = 0;
- // Do we have a decoder
- if (m_pMpaDecObj)
- {
- if (!m_bInitialized)
- {
- INT32 lRet = m_pMpaDecObj->Init_n((unsigned char*) data, nBytes,
- m_bUseSize);
- if (lRet)
- {
- int lNumChannels = 0;
- int lBitsPerSample = 0;
- m_pMpaDecObj->GetPCMInfo_v(m_ulSampleRate, lNumChannels, lBitsPerSample);
- m_ulNumChannels = (UINT32) lNumChannels;
- UINT32 ulMaxSamplesPerChannel = (UINT32) m_pMpaDecObj->GetSamplesPerFrame_n();
- m_ulMaxSamplesOut = ulMaxSamplesPerChannel * m_ulNumChannels;
- m_bInitialized = TRUE;
- }
- }
- if (m_bInitialized)
- {
- UINT32 ulSizeIn = nBytes;
- UINT32 ulSizeOut = nSamplesOut << 1; // want bytes not samples
- m_pMpaDecObj->DecodeFrame_v((unsigned char*) data,
- &ulSizeIn,
- (unsigned char*) samplesOut,
- &ulSizeOut);
- nBytesConsumed = (ulSizeIn <= nBytes) ? ulSizeIn : nBytes;
- nSamplesOut = ulSizeOut >> 1; // want samples not bytes
- retVal = (nSamplesOut) ? HXR_OK : HXR_NO_DATA;
- }
- }
- return retVal;
- }
- STDMETHODIMP CMP3AudioDecoder::GetMaxSamplesOut(UINT32& nSamples) CONSTMETHOD
- {
- nSamples = m_ulMaxSamplesOut;
- return HXR_OK;
- }
- STDMETHODIMP CMP3AudioDecoder::GetNChannels(UINT32& nChannels) CONSTMETHOD
- {
- nChannels = m_ulNumChannels;
- return HXR_OK;
- }
- STDMETHODIMP CMP3AudioDecoder::GetSampleRate(UINT32& sampleRate) CONSTMETHOD
- {
- sampleRate = m_ulSampleRate;
- return HXR_OK;
- }
- STDMETHODIMP CMP3AudioDecoder::GetDelay(UINT32& nSamples) CONSTMETHOD
- {
- nSamples = m_ulDelay;
- return HXR_OK;
- }