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