os/mm/mmlibs/mmfw/src/Plugin/Codec/audio/MMFPcm16ToImaAdPcmStereoCodec.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 "MMFPcm16ToImaAdPcmStereoCodec.h"
    21 
    22 // __________________________________________________________________________
    23 // Implementation
    24 
    25 CMMFCodec* CMMFPcm16ImaAdPcmStereoCodec::NewL(TAny* aInitParams)
    26 	{
    27 	CMMFPcm16ImaAdPcmStereoCodec* self=new(ELeave) CMMFPcm16ImaAdPcmStereoCodec();
    28 	CleanupStack::PushL(self);
    29 	self->ConstructL(aInitParams);
    30 	CleanupStack::Pop(self);
    31 	return STATIC_CAST( CMMFCodec*, self );
    32 	}
    33 
    34 CMMFPcm16ImaAdPcmStereoCodec::~CMMFPcm16ImaAdPcmStereoCodec()
    35 	{
    36 	}
    37 
    38 CMMFPcm16ImaAdPcmStereoCodec::CMMFPcm16ImaAdPcmStereoCodec() : i16PcmToImaAdpcm(2)
    39 	{
    40 	}
    41 
    42 void CMMFPcm16ImaAdPcmStereoCodec::ConstructL(TAny*  /*aInitParams*/)
    43 	{
    44 	iTempSrcBufferPtr = iTempSrcBuffer;
    45 	iTempSrcBufferCount = 0;
    46 	}
    47 
    48 /*************************************************************
    49 CMMFPcm16ImaAdPcmStereoCodec::ProcessL
    50 
    51 This function converts stereo PCM samples to IMA ADPCM samples 
    52 in blocks of KImaAdpcmBlockAlign (256) bytes. Any left 
    53 over bytes that do not fill a block are currently discarded. 
    54 996 source bytes are required to fill a 256 byte block.
    55 **************************************************************/
    56 TCodecProcessResult CMMFPcm16ImaAdPcmStereoCodec::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 < KImaAdpcmBlockAlign)
    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 = KImaAdpcmStereoTempBufferSize - iTempSrcBufferCount;
    87 
    88 		if (srcToFillTempBuffer<sourceRemain) //enough source to fill temporary buffer
    89 			dstRemain -= KImaAdpcmBlockAlign;
    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/KImaAdpcmBlockAlign;
    96 	TUint maxUsableDst = blocksRemaining * KImaAdpcmBlockAlign;
    97 	TUint srcToUse = blocksRemaining * KImaAdpcmStereoSamplesPerBlock * 4;
    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 < KImaAdpcmStereoTempBufferSize) && (srcLeft))
   116 			{
   117 			*iTempSrcBufferPtr++ = *pSrc++;
   118 			iTempSrcBufferCount++;
   119 			srcLeft--;
   120 			}
   121 
   122 		if (iTempSrcBufferCount == KImaAdpcmStereoTempBufferSize)
   123 			{
   124 			//reset
   125 			iTempSrcBufferCount = 0;
   126 			iTempSrcBufferPtr = iTempSrcBuffer;
   127 
   128 			i16PcmToImaAdpcm.Convert(iTempSrcBufferPtr, pDst, KImaAdpcmStereoSamplesPerBlock);
   129 
   130 			pDst += KImaAdpcmBlockAlign;
   131 			dstBytesAdded += KImaAdpcmBlockAlign;
   132 			}
   133 		}
   134 
   135 	//convert full blocks
   136 	while (srcLeft >= (KImaAdpcmStereoSamplesPerBlock*4)) 
   137 		{
   138 		i16PcmToImaAdpcm.Convert(pSrc, pDst, KImaAdpcmStereoSamplesPerBlock);
   139 
   140 		pSrc += KImaAdpcmStereoSamplesPerBlock*4;
   141 		pDst += KImaAdpcmBlockAlign;
   142 
   143 		dstBytesAdded += KImaAdpcmBlockAlign;	
   144 		srcLeft -= KImaAdpcmStereoSamplesPerBlock*4;	
   145 		}
   146 
   147 	//save remaining source in iTempSrcBuffer
   148 	while (srcLeft)
   149 		{
   150 		*iTempSrcBufferPtr++ = *pSrc++;
   151 		iTempSrcBufferCount++;
   152 		srcLeft--;
   153 		}
   154 
   155 	//if the source buffer is consumed
   156 	if ((srcLen == srcToUse + iSrc->Position()))
   157 		{
   158 		if (dstBytesAdded < maxUsableDst)
   159 			result.iStatus = TCodecProcessResult::EDstNotFilled;
   160 		else
   161 			result.iStatus = TCodecProcessResult::EProcessComplete;
   162 		}
   163 
   164 	result.iSrcBytesProcessed = srcToUse;
   165 	result.iDstBytesAdded = dstBytesAdded;
   166 
   167 	iDst->Data().SetLength(iDst->Position() + result.iDstBytesAdded);
   168 
   169 	return result;
   170 	}