mmlibs/mmfw/src/Plugin/Codec/audio/MmfimaadpcmTopcm16codec.cpp
changeset 0 b8ed18f6c07b
equal deleted inserted replaced
-1:000000000000 0:b8ed18f6c07b
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include "MMFImaAdPcmToPcm16Codec.h"
       
    18 #include <mmfcodec/mmfimaaudiocodecstate.h>
       
    19 
       
    20 #include <mmf/common/mmfaudio.h> // TMMFAudioConfig
       
    21 #include <mmf/plugin/mmfcodecimplementationuids.hrh> // KUidMmfCodecAudioSettings
       
    22 
       
    23 const TUid KUidCodecAudioConfig = {KUidMmfCodecAudioSettings};
       
    24 
       
    25 // __________________________________________________________________________
       
    26 // Implementation
       
    27 
       
    28 CMMFCodec* CMMFImaAdPcmPcm16Codec::NewL(TAny* aInitParams)
       
    29 	{
       
    30 	CMMFImaAdPcmPcm16Codec* self=new(ELeave) CMMFImaAdPcmPcm16Codec();
       
    31 	CleanupStack::PushL(self);
       
    32 	self->ConstructL(aInitParams);
       
    33 	CleanupStack::Pop(self);
       
    34 	return STATIC_CAST( CMMFCodec*, self );
       
    35 	}
       
    36 
       
    37 CMMFImaAdPcmPcm16Codec::~CMMFImaAdPcmPcm16Codec()
       
    38 	{
       
    39 	}
       
    40 
       
    41 CMMFImaAdPcmPcm16Codec::CMMFImaAdPcmPcm16Codec() : iImaAdpcmTo16Pcm(1)
       
    42 	{
       
    43 	}
       
    44 
       
    45 void CMMFImaAdPcmPcm16Codec::ConstructL(TAny*  /*aInitParams*/)
       
    46 	{
       
    47 	iTempSrcBufferPtr = iTempSrcBuffer;
       
    48 	iTempSrcBufferCount = 0;
       
    49 	
       
    50 	iChannels = 1;
       
    51 	iSamplesRate = 0;
       
    52 	iSamplesPerBlock = KImaAdpcmSamplesPerBlock;
       
    53 	iBlockAlign = KImaAdpcmBlockAlign;
       
    54 	}
       
    55 
       
    56 void CMMFImaAdPcmPcm16Codec::ResetL()
       
    57 	{
       
    58 	//Reset the actual codec
       
    59 	TMMFImaAdpcmCodecStateOld state;
       
    60 	state.iIndex = 0;
       
    61 	state.iPredicted = 0;
       
    62 	iImaAdpcmTo16Pcm.SetState(state);
       
    63 	iTempSrcBufferPtr = iTempSrcBuffer;
       
    64 	iTempSrcBufferCount = 0;
       
    65 	}
       
    66 
       
    67 /**
       
    68 CMMFImaAdPcmPcm16Codec::ProcessL
       
    69 
       
    70 This function converts IMA ADPCM samples to PCM 16 samples, 
       
    71 it is for mono ADPCM only at the moment.
       
    72 */
       
    73 TCodecProcessResult CMMFImaAdPcmPcm16Codec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
       
    74 	{
       
    75 	TCodecProcessResult result;
       
    76 	result.iStatus = TCodecProcessResult::EProcessIncomplete;
       
    77 
       
    78 	//convert from generic CMMFBuffer to CMMFDataBuffer
       
    79 	iSrc = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
       
    80 	iDst = STATIC_CAST(CMMFDataBuffer*, &aDst);
       
    81 
       
    82 	const TUint srcLen = iSrc->Data().Length();
       
    83 	const TUint dstMaxLen = iDst->Data().MaxLength();
       
    84 	const TUint sourceRemain = srcLen - iSrc->Position();
       
    85 
       
    86 	if (dstMaxLen < (iSamplesPerBlock * 2))
       
    87 		User::Leave(KErrArgument);
       
    88 
       
    89 	if ((iSrc->FrameNumber() == 0) && (iDst->Position() == 0))
       
    90 		{
       
    91 		ResetL();
       
    92 		}
       
    93 
       
    94 	//reset data if not a consecutive frame number
       
    95 	if ((iSrc->FrameNumber() != iLastFrameNumber) && (iSrc->FrameNumber() != (iLastFrameNumber+1)))
       
    96 		{
       
    97 		iTempSrcBufferPtr = iTempSrcBuffer;
       
    98 		iTempSrcBufferCount = 0;
       
    99 		}
       
   100 	iLastFrameNumber = iSrc->FrameNumber();
       
   101 
       
   102 	TUint dstRemain = (dstMaxLen - iDst->Position());
       
   103 	TUint srcToFillTempBuffer = 0;
       
   104 
       
   105 	//take account of src to be added to temporary buffer
       
   106 	if (iTempSrcBufferCount > 0)
       
   107 		{
       
   108 		srcToFillTempBuffer = iBlockAlign - iTempSrcBufferCount;
       
   109 		
       
   110 		if (srcToFillTempBuffer < sourceRemain) //enough source to fill temporary buffer
       
   111 			dstRemain -= (iSamplesPerBlock * 2);
       
   112 		else //not enough source to fill the temporary buffer
       
   113 			srcToFillTempBuffer = sourceRemain;
       
   114 		}
       
   115 
       
   116 	//calculate how much source is required to fill the destination buffer
       
   117 	TUint blocksRemaining = dstRemain / (iSamplesPerBlock * 2);
       
   118 	TUint maxUsableDst = blocksRemaining * iSamplesPerBlock * 2;
       
   119 	TUint srcToUse = blocksRemaining * iBlockAlign;
       
   120 
       
   121 	srcToUse += srcToFillTempBuffer;
       
   122 	srcToUse = (srcToUse < sourceRemain) ? srcToUse : sourceRemain;
       
   123 	
       
   124 	//we need to cast away CONST even on the source, as the TClass needs a TUint8*
       
   125 	TUint8* pSrc = CONST_CAST(TUint8*,iSrc->Data().Ptr());
       
   126 	pSrc += iSrc->Position();
       
   127 	TUint8* pDst = CONST_CAST(TUint8*,iDst->Data().Ptr());
       
   128 	pDst += iDst->Position();
       
   129 
       
   130 	TUint dstBytesAdded = 0;
       
   131 	TUint srcLeft = srcToUse;
       
   132 
       
   133 	//convert remaining source from previous call to ProcessL
       
   134 	if (iTempSrcBufferCount > 0)
       
   135 		{
       
   136 		//Fill temp buffer from source buffer
       
   137 		while((iTempSrcBufferCount < iBlockAlign) && (srcLeft))
       
   138 			{
       
   139 			*iTempSrcBufferPtr++ = *pSrc++;
       
   140 			iTempSrcBufferCount++;
       
   141 			srcLeft --;
       
   142 			}
       
   143 
       
   144 		if (iTempSrcBufferCount == iBlockAlign) //temp buffer full
       
   145 			{
       
   146 			//reset
       
   147 			iTempSrcBufferCount = 0;
       
   148 			iTempSrcBufferPtr = iTempSrcBuffer;
       
   149 
       
   150 			iImaAdpcmTo16Pcm.Convert(iTempSrcBufferPtr, pDst, iSamplesPerBlock);
       
   151 
       
   152 			pDst += (iSamplesPerBlock * 2);
       
   153 			dstBytesAdded += (iSamplesPerBlock * 2);
       
   154 			}
       
   155 		}
       
   156 
       
   157 	//convert full blocks
       
   158 	while (srcLeft >= iBlockAlign) 
       
   159 		{
       
   160 		iImaAdpcmTo16Pcm.Convert(pSrc, pDst, iSamplesPerBlock);
       
   161 
       
   162 		pSrc += iBlockAlign;
       
   163 		pDst += (iSamplesPerBlock * 2);
       
   164 
       
   165 		dstBytesAdded += (iSamplesPerBlock * 2);	
       
   166 		srcLeft -= iBlockAlign;	
       
   167 		}
       
   168 
       
   169 	while (srcLeft)
       
   170 		{
       
   171 		*iTempSrcBufferPtr++ = *pSrc++;
       
   172 		iTempSrcBufferCount++;
       
   173 		srcLeft--;
       
   174 		}
       
   175 	
       
   176 	//if the source buffer is consumed
       
   177 	if ((srcLen == srcToUse + iSrc->Position()))
       
   178 		{
       
   179 		if (dstBytesAdded < maxUsableDst)
       
   180 			result.iStatus = TCodecProcessResult::EDstNotFilled;
       
   181 		else
       
   182 			result.iStatus = TCodecProcessResult::EProcessComplete;
       
   183 		}
       
   184 
       
   185 	result.iSrcBytesProcessed = srcToUse;
       
   186 	result.iDstBytesAdded = dstBytesAdded;
       
   187 
       
   188 	iDst->Data().SetLength( iDst->Position() + result.iDstBytesAdded);
       
   189 
       
   190 	return result;
       
   191 	}
       
   192 
       
   193 /**
       
   194 Sets codec configuration.
       
   195 
       
   196 @param  aConfigType
       
   197         The UID of the codec to configure.
       
   198 @param  aConfigData
       
   199         The configuration information.
       
   200 */
       
   201 void CMMFImaAdPcmPcm16Codec::ConfigureL(TUid aConfigType, const TDesC8& aConfigData)
       
   202 	{
       
   203 	if (aConfigType != KUidCodecAudioConfig)
       
   204 		{
       
   205 		User::Leave(KErrArgument);
       
   206 		}
       
   207 		
       
   208 	const TMMFAudioConfig& audioConfig = static_cast<const TPckgBuf<TMMFAudioConfig>&>(aConfigData)();
       
   209 	
       
   210 	iChannels = audioConfig.iChannels;
       
   211 	iSamplesRate = audioConfig.iSampleRate;
       
   212 	
       
   213 	switch (iSamplesRate * iChannels)
       
   214 		{
       
   215 		case 8000: // fall through, same as 11025
       
   216 		case 11025:
       
   217 		case 16000:
       
   218 			iBlockAlign = 256;
       
   219 			break;
       
   220 		case 22050:
       
   221 			iBlockAlign = 512;
       
   222 			break;
       
   223 			
       
   224 		case 44100:
       
   225 			iBlockAlign = 1024;
       
   226 			break;
       
   227 			
       
   228 		case 88200:
       
   229 			iBlockAlign = 2048;
       
   230 			break;
       
   231 			
       
   232 		default:
       
   233 			User::Leave(KErrArgument);
       
   234 		}
       
   235 	
       
   236 	
       
   237 	// SamplesPerBlock = [(BlockAlign - 4 * Channels) * 8] / (BitsPerSample * Channels) + 1
       
   238 	iSamplesPerBlock = (iBlockAlign - 4 * iChannels) * 8 / (KImaAdpcmBitsPerSample * iChannels) + 1;
       
   239 	}
       
   240 
       
   241 TInt CMMFImaAdPcmPcm16Codec::Extension_(TUint aExtensionId, TAny*& aExtPtr, TAny*)
       
   242 	{
       
   243 	if (aExtensionId == KUidCustomInterfaceDevSoundFileBlockLength.iUid)	
       
   244 		{
       
   245 		aExtPtr = static_cast<MMMFDevSoundCustomInterfaceFileBlockLength*>(this);
       
   246 		return KErrNone;
       
   247 		}
       
   248 	else
       
   249 		{
       
   250 		return CMMFCodec::Extension_(aExtensionId, aExtPtr, NULL);	
       
   251 		}
       
   252 	}
       
   253 
       
   254 void CMMFImaAdPcmPcm16Codec::SetFileBlockLength(TUint aBlockAlign)
       
   255 	{	
       
   256     iBlockAlign = aBlockAlign;		
       
   257   	iSamplesPerBlock = (iBlockAlign - 4 * iChannels) * 8 / (KImaAdpcmBitsPerSample * iChannels) + 1;
       
   258   		    
       
   259 	}
       
   260