os/mm/mmlibs/mmfw/src/Plugin/Codec/audio/Mmfpcm16Toimaadpcmcodec.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/mmlibs/mmfw/src/Plugin/Codec/audio/Mmfpcm16Toimaadpcmcodec.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,187 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2002 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +
    1.23 +#include "MMFPcm16ToImaAdPcmCodec.h"
    1.24 +
    1.25 +// __________________________________________________________________________
    1.26 +// Implementation
    1.27 +
    1.28 +CMMFCodec* CMMFPcm16ImaAdPcmCodec::NewL(TAny* aInitParams)
    1.29 +	{
    1.30 +	CMMFPcm16ImaAdPcmCodec* self=new(ELeave) CMMFPcm16ImaAdPcmCodec();
    1.31 +	CleanupStack::PushL(self);
    1.32 +	self->ConstructL(aInitParams);
    1.33 +	CleanupStack::Pop(self);
    1.34 +	return STATIC_CAST( CMMFCodec*, self );
    1.35 +	}
    1.36 +
    1.37 +CMMFPcm16ImaAdPcmCodec::~CMMFPcm16ImaAdPcmCodec()
    1.38 +	{
    1.39 +	}
    1.40 +
    1.41 +CMMFPcm16ImaAdPcmCodec::CMMFPcm16ImaAdPcmCodec() : i16PcmToImaAdpcm(1)
    1.42 +	{
    1.43 +	}
    1.44 +
    1.45 +void CMMFPcm16ImaAdPcmCodec::ConstructL(TAny*  /*aInitParams*/)
    1.46 +	{
    1.47 +	iTempSrcBufferPtr = iTempSrcBuffer;
    1.48 +	iTempSrcBufferCount = 0;
    1.49 +	}
    1.50 +
    1.51 +void CMMFPcm16ImaAdPcmCodec::ResetL()
    1.52 +	{
    1.53 +	//Reset the actual codec
    1.54 +	TMMFImaAdpcmCodecStateOld state;
    1.55 +	state.iIndex = 0;
    1.56 +	state.iPredicted = 0;
    1.57 +	i16PcmToImaAdpcm.SetState(state);
    1.58 +	iTempSrcBufferPtr = iTempSrcBuffer;
    1.59 +	iTempSrcBufferCount = 0;
    1.60 +	}
    1.61 +
    1.62 +/*************************************************************
    1.63 +CMMFPcm16ImaAdPcmCodec::ProcessL
    1.64 +
    1.65 +This function converts PCM samples to IMA ADPCM samples in
    1.66 +blocks of KImaAdpcmBlockAlign (256) bytes. 1010 source 
    1.67 +bytes are required to fill a 256 byte block.
    1.68 +**************************************************************/
    1.69 +TCodecProcessResult CMMFPcm16ImaAdPcmCodec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
    1.70 +	{
    1.71 +	TCodecProcessResult result;
    1.72 +	result.iStatus = TCodecProcessResult::EProcessIncomplete;
    1.73 +
    1.74 +	//convert from generic CMMFBuffer to CMMFDataBuffer
    1.75 +	iSrc = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
    1.76 +	iDst = STATIC_CAST(CMMFDataBuffer*, &aDst);
    1.77 +
    1.78 +	// If the Destination is set to zero, ensure the Codec is fully reset,
    1.79 +	// as this may be a reposition
    1.80 +	if ((iSrc->FrameNumber() == 0) && (iDst->Position() == 0))
    1.81 +		{
    1.82 +		ResetL();
    1.83 +		}
    1.84 +
    1.85 +
    1.86 +	const TUint srcLen = iSrc->Data().Length();
    1.87 +	const TUint dstMaxLen = iDst->Data().MaxLength();
    1.88 +	const TUint sourceRemain = srcLen - iSrc->Position();
    1.89 +
    1.90 +	if (dstMaxLen < KImaAdpcmBlockAlign)
    1.91 +		User::Leave(KErrArgument);
    1.92 +
    1.93 +	//reset data if not a consecutive frame number
    1.94 +	if ((iSrc->FrameNumber() != iLastFrameNumber) && (iSrc->FrameNumber() != (iLastFrameNumber+1)))
    1.95 +		{
    1.96 +		iTempSrcBufferPtr = iTempSrcBuffer;
    1.97 +		iTempSrcBufferCount = 0;
    1.98 +		}
    1.99 +	iLastFrameNumber = iSrc->FrameNumber();
   1.100 +
   1.101 +	TUint dstRemain = (dstMaxLen - iDst->Position());	
   1.102 +	TUint srcToFillTempBuffer = 0;
   1.103 +
   1.104 +	//take account of src to be added to temporary buffer
   1.105 +	if (iTempSrcBufferCount > 0)
   1.106 +		{
   1.107 +		srcToFillTempBuffer = KImaAdpcmTempBufferSize - iTempSrcBufferCount;
   1.108 +		if (srcToFillTempBuffer<sourceRemain) //enough source to fill temporary buffer
   1.109 +			dstRemain -= KImaAdpcmBlockAlign;
   1.110 +		else //not enough source to fill the temporary buffer
   1.111 +			srcToFillTempBuffer = sourceRemain;
   1.112 +		}
   1.113 +
   1.114 +	//calculate how much source is required to fill the destination buffer
   1.115 +	TUint blocksRemaining = dstRemain/KImaAdpcmBlockAlign;
   1.116 +	TUint maxUsableDst = blocksRemaining * KImaAdpcmBlockAlign;
   1.117 +	TUint srcToUse = blocksRemaining * KImaAdpcmSamplesPerBlock * 2;
   1.118 +
   1.119 +	srcToUse += srcToFillTempBuffer;
   1.120 +	srcToUse = (srcToUse<sourceRemain ? srcToUse : sourceRemain);
   1.121 +
   1.122 +	//we need to cast away CONST even on the source, as the TClass needs a TUint8*
   1.123 +	TUint8* pSrc = CONST_CAST(TUint8*,iSrc->Data().Ptr());
   1.124 +	pSrc += iSrc->Position();
   1.125 +	TUint8* pDst = CONST_CAST(TUint8*,iDst->Data().Ptr());
   1.126 +	pDst += iDst->Position();
   1.127 +	
   1.128 +	TUint dstBytesAdded = 0;
   1.129 +	TUint srcLeft = srcToUse;
   1.130 +
   1.131 +	//convert remaining source from previous call to ProcessL
   1.132 +	if (iTempSrcBufferCount > 0)
   1.133 +		{
   1.134 +		//Fill temp buffer from source buffer
   1.135 +		while((iTempSrcBufferCount < KImaAdpcmTempBufferSize) && (srcLeft))
   1.136 +			{
   1.137 +			*iTempSrcBufferPtr++ = *pSrc++;
   1.138 +			iTempSrcBufferCount++;
   1.139 +			srcLeft--;
   1.140 +			}
   1.141 +
   1.142 +		if (iTempSrcBufferCount == KImaAdpcmTempBufferSize)
   1.143 +			{
   1.144 +			//reset
   1.145 +			iTempSrcBufferCount = 0;
   1.146 +			iTempSrcBufferPtr = iTempSrcBuffer;
   1.147 +
   1.148 +			i16PcmToImaAdpcm.Convert(iTempSrcBufferPtr, pDst, KImaAdpcmSamplesPerBlock);
   1.149 +
   1.150 +			pDst += KImaAdpcmBlockAlign;
   1.151 +			dstBytesAdded += KImaAdpcmBlockAlign;
   1.152 +			}
   1.153 +		}
   1.154 +
   1.155 +	//convert full blocks
   1.156 +	while (srcLeft >= (KImaAdpcmSamplesPerBlock*2)) 
   1.157 +		{
   1.158 +		i16PcmToImaAdpcm.Convert(pSrc, pDst, KImaAdpcmSamplesPerBlock);
   1.159 +
   1.160 +		pSrc += KImaAdpcmSamplesPerBlock*2;
   1.161 +		pDst += KImaAdpcmBlockAlign;
   1.162 +
   1.163 +		dstBytesAdded += KImaAdpcmBlockAlign;	
   1.164 +		srcLeft -= KImaAdpcmSamplesPerBlock*2;	
   1.165 +		}
   1.166 +
   1.167 +	//save remaining source in iTempSrcBuffer
   1.168 +	while (srcLeft)
   1.169 +		{
   1.170 +		*iTempSrcBufferPtr++ = *pSrc++;
   1.171 +		iTempSrcBufferCount++;
   1.172 +		srcLeft--;
   1.173 +		}
   1.174 +
   1.175 +	//if the source buffer is consumed
   1.176 +	if ((srcLen == srcToUse + iSrc->Position()))
   1.177 +		{
   1.178 +		if (dstBytesAdded < maxUsableDst)
   1.179 +			result.iStatus = TCodecProcessResult::EDstNotFilled;
   1.180 +		else
   1.181 +			result.iStatus = TCodecProcessResult::EProcessComplete;
   1.182 +		}
   1.183 +
   1.184 +	result.iSrcBytesProcessed = srcToUse;
   1.185 +	result.iDstBytesAdded = dstBytesAdded;
   1.186 +
   1.187 +	iDst->Data().SetLength(iDst->Position() + result.iDstBytesAdded);
   1.188 +
   1.189 +	return result;	
   1.190 +	}