os/mm/devsound/sounddevbt/src/Plugin/HwDevice/Audio/MmfBtImaadpcmToPcm16HwDevice.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/devsound/sounddevbt/src/Plugin/HwDevice/Audio/MmfBtImaadpcmToPcm16HwDevice.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,239 @@
1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "MmfBtAudioCodec.h"
1.20 +#include <mmfpaniccodes.h>
1.21 +#include "MmfBtImaAdpcmToPcm16HwDevice.h"
1.22 +#include "../../MmfBtFileDependencyUtil.h"
1.23 +
1.24 +/**
1.25 +*
1.26 +* NewL
1.27 +*
1.28 +*/
1.29 +CMMFImaAdpcmToPcm16CodecHwDevice* CMMFImaAdpcmToPcm16CodecHwDevice::NewL()
1.30 + {
1.31 + CMMFImaAdpcmToPcm16CodecHwDevice* self=new(ELeave)CMMFImaAdpcmToPcm16CodecHwDevice();
1.32 + CleanupStack::PushL(self);
1.33 + self->ConstructL();
1.34 + CleanupStack::Pop(self);
1.35 + return self;
1.36 + }
1.37 +
1.38 +/**
1.39 +*
1.40 +* ConstructL
1.41 +*
1.42 +*/
1.43 +void CMMFImaAdpcmToPcm16CodecHwDevice::ConstructL()
1.44 + {
1.45 + iCodec = new (ELeave) CMMFImaAdpcmToPcm16Codec();
1.46 + }
1.47 +
1.48 +/**
1.49 +*
1.50 +* ~CMMFMulawPcm16HwDevice
1.51 +*
1.52 +*/
1.53 +CMMFImaAdpcmToPcm16CodecHwDevice::~CMMFImaAdpcmToPcm16CodecHwDevice()
1.54 + {
1.55 + }
1.56 +
1.57 +/**
1.58 +*
1.59 +* Codec
1.60 +*
1.61 +*/
1.62 +CMMFSwCodec &CMMFImaAdpcmToPcm16CodecHwDevice::Codec()
1.63 + {
1.64 + return *iCodec;
1.65 + }
1.66 +
1.67 +/**
1.68 +@see CMMFSwCodecWrapper::Start
1.69 +
1.70 +this function sets SampleRate and Channels for CMMFImaAdpcmToPcm16Codec
1.71 +*/
1.72 +TInt CMMFImaAdpcmToPcm16CodecHwDevice::Start(TDeviceFunc aFuncCmd, TDeviceFlow aFlowCmd)
1.73 + {
1.74 + TInt err = CMMFSwCodecWrapper::Start(aFuncCmd, aFlowCmd);
1.75 + if (err != 0)
1.76 + return err;
1.77 + return ((CMMFImaAdpcmToPcm16Codec*)iCodec)->Configure(iChannels, iSampleRate);
1.78 + }
1.79 +
1.80 +CMMFImaAdpcmToPcm16Codec::CMMFImaAdpcmToPcm16Codec()
1.81 + {
1.82 + iChannels = 1;
1.83 + iSampleRate = 0;
1.84 + iBlockAlign = KImaAdpcmBlockAlign;
1.85 + iSamplesPerBlock = KImaAdpcmSamplesPerBlock;
1.86 + }
1.87 +/**
1.88 +*
1.89 +* ProcessL
1.90 +* @param aSrc
1.91 +* @param aDst
1.92 +* @pre position of buffer aSrc is 0
1.93 +* @pre position of buffer aDst is 0
1.94 +* @pre sufficient bytes in output to consume input
1.95 +* @return TCodecProcessResult
1.96 +* This function converts IMA ADPCM samples to PCM samples.
1.97 +*
1.98 +*/
1.99 +CMMFSwCodec::TCodecProcessResult CMMFImaAdpcmToPcm16Codec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
1.100 + {
1.101 + CMMFSwCodec::TCodecProcessResult result;
1.102 + result.iCodecProcessStatus = result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
1.103 +
1.104 + //convert from generic CMMFBuffer to CMMFDataBuffer
1.105 + const CMMFDataBuffer* src = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
1.106 + CMMFDataBuffer* dst = STATIC_CAST(CMMFDataBuffer*, &aDst);
1.107 +
1.108 + if( !CheckPreconditions( src, dst ) )
1.109 + {
1.110 + //[ precondition(s) violation ]
1.111 + User::Leave(KErrArgument);
1.112 + }
1.113 +
1.114 + //calculate how much source is required to fill the destination buffer
1.115 + TUint blocksRemaining = src->Data().Length() / iBlockAlign;
1.116 +
1.117 + //we need to cast away CONST even on the source, as the TClass needs a TUint8*
1.118 + TUint8* pSrc = CONST_CAST(TUint8*,src->Data().Ptr());
1.119 + TUint8* pDst = CONST_CAST(TUint8*,dst->Data().Ptr());
1.120 +
1.121 + //[ [process full blocks ]
1.122 + TUint dstBytesAdded = 0;
1.123 + for( TUint count = 0; count < blocksRemaining; count++ )
1.124 + {
1.125 + iImaAdpcmTo16Pcm.Convert(pSrc, pDst, iSamplesPerBlock);
1.126 + pSrc += iBlockAlign;
1.127 + pDst += (iSamplesPerBlock * sizeof(TInt16));
1.128 + dstBytesAdded += (iSamplesPerBlock * sizeof(TInt16));
1.129 + }
1.130 +
1.131 + result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
1.132 + result.iSrcBytesProcessed = blocksRemaining * iBlockAlign;
1.133 + result.iDstBytesAdded = dstBytesAdded;
1.134 + dst->Data().SetLength(result.iDstBytesAdded);
1.135 +
1.136 + //[ check post conditions
1.137 + __ASSERT_DEBUG( (src->Position() == 0), TMmfAudioCodecPanicsNameSpace::Panic( TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
1.138 + __ASSERT_DEBUG( (dst->Position() == 0), TMmfAudioCodecPanicsNameSpace::Panic( TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
1.139 + TInt r1 = src->Data().Length();
1.140 + r1 /= iBlockAlign;
1.141 + TInt r2 = dst->Data().Length();
1.142 + r2 /=(iSamplesPerBlock * sizeof(TInt16));
1.143 + __ASSERT_DEBUG( r1== r2, TMmfAudioCodecPanicsNameSpace::Panic(TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
1.144 + __ASSERT_DEBUG( dst->Data().Length() % 2 == 0, TMmfAudioCodecPanicsNameSpace::Panic( TMmfAudioCodecPanicsNameSpace::EPostConditionViolation )); // pcm output
1.145 +
1.146 + return result;
1.147 + }
1.148 +
1.149 +/**
1.150 +*
1.151 +* Preconditions
1.152 +* This methos tests the preconditions of the ProcessL method
1.153 +* @return TBool ETrue for sucess and EFalse for failure of the preconditions
1.154 +*
1.155 +**/
1.156 +TBool CMMFImaAdpcmToPcm16Codec::CheckPreconditions( const CMMFDataBuffer* aSrcBuffer, CMMFDataBuffer* aDestBuffer )
1.157 + {
1.158 + TBool result = EFalse;
1.159 +
1.160 + if(! aSrcBuffer )
1.161 + {
1.162 + return result;
1.163 + }
1.164 +
1.165 + if( ! aDestBuffer )
1.166 + {
1.167 + return result;
1.168 + }
1.169 +
1.170 + // Check position of src and dest are 0
1.171 + if( aSrcBuffer->Position() )
1.172 + {
1.173 + return result;
1.174 + }
1.175 +
1.176 + // Check position of src and dest are 0
1.177 + if( aDestBuffer->Position() )
1.178 + {
1.179 + return result;
1.180 + }
1.181 +
1.182 + // check there are sufficient bytes in the output to consume the input
1.183 + const TUint KTempBufferSize = iSamplesPerBlock * 2;
1.184 + TInt numInputSubFrames = aSrcBuffer->Data().Length() / iBlockAlign;
1.185 + TInt numOutputSubFrames = aDestBuffer->Data().MaxLength() / KTempBufferSize;
1.186 +
1.187 + //[ we need modulo 1010 bytes on all src frames that are not the last
1.188 + // frame
1.189 + // For the last frame we will code only whole frames and effectively
1.190 + // drop any remaining samples]
1.191 + TBool validInputDataLength = (aSrcBuffer->Data().Length() % iBlockAlign == 0) ;
1.192 +
1.193 + if( (numInputSubFrames > numOutputSubFrames) || // sufficient space in the output for the input
1.194 + (aSrcBuffer->Position() > 0 ) || // position must be zero since we can eat all the data
1.195 + (aDestBuffer->Position() > 0 ) ||
1.196 + (!validInputDataLength)) //position must be zero
1.197 + {
1.198 + return result;
1.199 + }
1.200 +
1.201 + result = ETrue; // preconditions have been satisfied
1.202 +
1.203 + return result;
1.204 + }
1.205 +
1.206 +TInt CMMFImaAdpcmToPcm16Codec::Configure(TUint aChannels, TUint aSampleRate)
1.207 + {
1.208 + iChannels = aChannels;
1.209 + iSampleRate = aSampleRate;
1.210 +
1.211 + switch (iSampleRate * iChannels)
1.212 + {
1.213 + case 8000: // fall through, same as 11025
1.214 + case 11025:
1.215 + case 16000:
1.216 + iBlockAlign = 256;
1.217 + break;
1.218 + case 22050:
1.219 + iBlockAlign = 512;
1.220 + break;
1.221 +
1.222 + case 44100:
1.223 + iBlockAlign = 1024;
1.224 + break;
1.225 +
1.226 + case 88200:
1.227 + iBlockAlign = 2048;
1.228 + break;
1.229 +
1.230 + default:
1.231 + return KErrArgument;
1.232 + }
1.233 +
1.234 + const TUint KImaAdpcmBitsPerSample = 4;
1.235 + // SamplesPerBlock = [(BlockAlign - 4 * Channels) * 8] / (BitsPerSample * Channels) + 1
1.236 + iSamplesPerBlock = (iBlockAlign - 4 * iChannels) * 8 / (KImaAdpcmBitsPerSample * iChannels) + 1;
1.237 +
1.238 + return KErrNone;
1.239 + }
1.240 +
1.241 +
1.242 +