os/mm/mmlibs/mmfw/src/Plugin/Codec/audio/MMFImaAdStereoPcmToPcm16Codec.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2002 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 *
    16 */
    17 
    18 
    19 
    20 #include "MMFImaAdStereoPcmToPcm16Codec.h"
    21 
    22 
    23 // __________________________________________________________________________
    24 // Implementation
    25 
    26 CMMFCodec* CMMFImaAdStereoPcmPcm16Codec::NewL(TAny* aInitParams)
    27 	{
    28 	CMMFImaAdStereoPcmPcm16Codec* self=new(ELeave) CMMFImaAdStereoPcmPcm16Codec();
    29 	CleanupStack::PushL(self);
    30 	self->ConstructL(aInitParams);
    31 	CleanupStack::Pop(self);
    32 	return STATIC_CAST( CMMFCodec*, self );
    33 	}
    34 
    35 CMMFImaAdStereoPcmPcm16Codec::~CMMFImaAdStereoPcmPcm16Codec()
    36 	{
    37 	}
    38 
    39 CMMFImaAdStereoPcmPcm16Codec::CMMFImaAdStereoPcmPcm16Codec() : iImaAdpcmTo16Pcm(2)
    40 	{
    41 	}
    42 
    43 void CMMFImaAdStereoPcmPcm16Codec::ConstructL(TAny*  /*aInitParams*/)
    44 	{
    45 	iTempSrcBufferPtr = iTempSrcBuffer;
    46 	iTempSrcBufferCount = 0;
    47 	}
    48 
    49 /*************************************************************
    50 CMMFImaAdStereoPcmPcm16Codec::ProcessL
    51 
    52 This function converts IMA ADPCM stereo samples to PCM samples 
    53 in blocks of KImaAdpcmBlockAlign (256) bytes. 256 
    54 source bytes are required to fill a 996 byte block.
    55 **************************************************************/
    56 TCodecProcessResult CMMFImaAdStereoPcmPcm16Codec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
    57 	{
    58 	TCodecProcessResult result;
    59 	result.iStatus = TCodecProcessResult::EProcessIncomplete;
    60 
    61 	//convert from generic CMMFBuffer to CMMFDataBuffer
    62 	iSrc = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
    63 	iDst = STATIC_CAST(CMMFDataBuffer*, &aDst);
    64 
    65 	const TUint srcLen = iSrc->Data().Length();
    66 	const TUint dstMaxLen = iDst->Data().MaxLength();
    67 	const TUint sourceRemain = srcLen - iSrc->Position();
    68 
    69 	if (dstMaxLen < (KImaAdpcmStereoSamplesPerBlock*4))
    70 		User::Leave(KErrArgument);
    71 
    72 		//reset data if not a consecutive frame number
    73 	if ((iSrc->FrameNumber() != iLastFrameNumber) && (iSrc->FrameNumber() != (iLastFrameNumber+1)))
    74 		{
    75 		iTempSrcBufferPtr = iTempSrcBuffer;
    76 		iTempSrcBufferCount = 0;
    77 		}
    78 	iLastFrameNumber = iSrc->FrameNumber();
    79 
    80 	TUint dstRemain = (dstMaxLen - iDst->Position());
    81 	TUint srcToFillTempBuffer = 0;
    82 
    83 	//take account of src to be added to temporary buffer
    84 	if (iTempSrcBufferCount > 0)
    85 		{
    86 		srcToFillTempBuffer = KImaAdpcmBlockAlign - iTempSrcBufferCount;
    87 		
    88 		if (srcToFillTempBuffer<sourceRemain) //enough source to fill temporary buffer
    89 			dstRemain -= (KImaAdpcmStereoSamplesPerBlock*4);
    90 		else //not enough source to fill the temporary buffer
    91 			srcToFillTempBuffer = sourceRemain;
    92 		}
    93 
    94 	//calculate how much source is required to fill the destination buffer
    95 	TUint blocksRemaining = dstRemain/(KImaAdpcmStereoSamplesPerBlock*4);
    96 	TUint maxUsableDst = blocksRemaining * KImaAdpcmSamplesPerBlock * 4;
    97 	TUint srcToUse = blocksRemaining * KImaAdpcmBlockAlign;
    98 
    99 	srcToUse += srcToFillTempBuffer;
   100 	srcToUse = (srcToUse<sourceRemain ? srcToUse : sourceRemain);
   101 	
   102 	//we need to cast away CONST even on the source, as the TClass needs a TUint8*
   103 	TUint8* pSrc = CONST_CAST(TUint8*,iSrc->Data().Ptr());
   104 	pSrc += iSrc->Position();
   105 	TUint8* pDst = CONST_CAST(TUint8*,iDst->Data().Ptr());
   106 	pDst += iDst->Position();
   107 
   108 	TUint dstBytesAdded = 0;
   109 	TUint srcLeft = srcToUse;
   110 
   111 	//convert remaining source from previous call to ProcessL
   112 	if (iTempSrcBufferCount > 0)
   113 		{
   114 		//Fill temp buffer from source buffer
   115 		while((iTempSrcBufferCount < KImaAdpcmBlockAlign) && (srcLeft))
   116 			{
   117 			*iTempSrcBufferPtr++ = *pSrc++;
   118 			iTempSrcBufferCount++;
   119 			srcLeft--;
   120 			}
   121 
   122 		if(iTempSrcBufferCount == KImaAdpcmBlockAlign)
   123 			{
   124 			//reset
   125 			iTempSrcBufferCount = 0;
   126 			iTempSrcBufferPtr = iTempSrcBuffer;
   127 
   128 			iImaAdpcmTo16Pcm.Convert(iTempSrcBufferPtr, pDst, KImaAdpcmStereoSamplesPerBlock);
   129 
   130 			pDst += (KImaAdpcmStereoSamplesPerBlock*4);
   131 			dstBytesAdded += (KImaAdpcmStereoSamplesPerBlock*4);
   132 			}
   133 		}
   134 
   135 	//convert full blocks
   136 	while (srcLeft >= KImaAdpcmBlockAlign) 
   137 		{
   138 		iImaAdpcmTo16Pcm.Convert(pSrc, pDst, KImaAdpcmStereoSamplesPerBlock);
   139 
   140 		pSrc += KImaAdpcmBlockAlign;
   141 		pDst += (KImaAdpcmStereoSamplesPerBlock*4);
   142 
   143 		dstBytesAdded += (KImaAdpcmStereoSamplesPerBlock*4);	
   144 		srcLeft -= KImaAdpcmBlockAlign;	
   145 		}
   146 
   147 	while (srcLeft)
   148 		{
   149 		*iTempSrcBufferPtr++ = *pSrc++;
   150 		iTempSrcBufferCount++;
   151 		srcLeft--;
   152 		}
   153 	
   154 	//if the source buffer is consumed
   155 	if ((srcLen == srcToUse + iSrc->Position()))
   156 		{
   157 		if (dstBytesAdded < maxUsableDst)
   158 			result.iStatus = TCodecProcessResult::EDstNotFilled;
   159 		else
   160 			result.iStatus = TCodecProcessResult::EProcessComplete;
   161 		}
   162 
   163 	result.iSrcBytesProcessed = srcToUse;
   164 	result.iDstBytesAdded = dstBytesAdded;
   165 
   166 	iDst->Data().SetLength( iDst->Position() + result.iDstBytesAdded);
   167 
   168 	return result;
   169 	}