1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/devsound/sounddevbt/src/Plugin/HwDevice/Audio/MmfBtPcm16ToImaAdpcm.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,183 @@
1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "MmfBtPcm16ToImaAdpcm.h"
1.20 +#include "../../MmfBtFileDependencyUtil.h"
1.21 +
1.22 +/**
1.23 +*
1.24 +* NewL
1.25 +*
1.26 +*/
1.27 +CMMFPcm16ToImaAdpcmHwDevice* CMMFPcm16ToImaAdpcmHwDevice::NewL()
1.28 + {
1.29 + CMMFPcm16ToImaAdpcmHwDevice* self=new(ELeave) CMMFPcm16ToImaAdpcmHwDevice();
1.30 + CleanupStack::PushL(self);
1.31 + self->ConstructL();
1.32 + CleanupStack::Pop(self);
1.33 + return self;
1.34 + }
1.35 +
1.36 +/**
1.37 +*
1.38 +* ~CMMFPcm16ToAlawHwDevice
1.39 +*
1.40 +*/
1.41 +CMMFPcm16ToImaAdpcmHwDevice::~CMMFPcm16ToImaAdpcmHwDevice()
1.42 + {
1.43 + }
1.44 +
1.45 +/**
1.46 +*
1.47 +* ConstructL
1.48 +*
1.49 +*/
1.50 +void CMMFPcm16ToImaAdpcmHwDevice::ConstructL()
1.51 + {
1.52 + iCodec = new (ELeave) CMMFPcm16ToImaAdpcmCodec();
1.53 + }
1.54 +
1.55 +/**
1.56 +*
1.57 +* Codec
1.58 +*
1.59 +*/
1.60 +CMMFSwCodec &CMMFPcm16ToImaAdpcmHwDevice::Codec()
1.61 + {
1.62 + return *iCodec;
1.63 + }
1.64 +
1.65 +/**
1.66 +*
1.67 +* ResetL
1.68 +*
1.69 +*/
1.70 +void CMMFPcm16ToImaAdpcmCodec::ResetL()
1.71 + {
1.72 + //Reset the actual codec
1.73 + TMMFImaAdpcmCodecState state;
1.74 + state.iIndex = 0;
1.75 + state.iPredicted = 0;
1.76 + iPcm16ToImaAdpcm.SetState(state);
1.77 + }
1.78 +
1.79 +/**
1.80 +*
1.81 +* ProcessL
1.82 +* @param aSrc src buffer
1.83 +* @param aDst destination buffer
1.84 +* @return CMMFSwCodec::TCodecProcessResult
1.85 +* This function converts PCM samples to IMA ADPCM samples in
1.86 +* blocks of KImaAdpcmBlockAlign (256) bytes. 1010 source
1.87 +* bytes are required to fill a 256 byte block.
1.88 +* @pre if last buffer and src contains < 1010 bytes discard input
1.89 +* This function throws away the last buffer if it contains < 1010 bytes
1.90 +* (ie we must have sufficient data to process an entire frame )
1.91 +* All other src buffers must contain
1.92 +* All destination buffer must contain
1.93 +*
1.94 +**/
1.95 +CMMFSwCodec::TCodecProcessResult CMMFPcm16ToImaAdpcmCodec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
1.96 + {
1.97 + CMMFSwCodec::TCodecProcessResult result;
1.98 + result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
1.99 +
1.100 + //convert from generic CMMFBuffer to CMMFDataBuffer
1.101 + const CMMFDataBuffer* source = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
1.102 + CMMFDataBuffer* destination = STATIC_CAST(CMMFDataBuffer*, &aDst);
1.103 +
1.104 + //[ source and destination must not be null ]
1.105 + if( !source || !destination )
1.106 + User::Leave( KErrArgument );
1.107 +
1.108 + //[ check preconditions ]
1.109 + if( !BuffersStatus( source, destination ))
1.110 + {
1.111 + User::Leave( KErrArgument );
1.112 + }
1.113 +
1.114 + //[ code the buffers ]
1.115 + ProcessBuffers( *source, *destination, result );
1.116 +
1.117 + return result;
1.118 + }
1.119 +
1.120 +/**
1.121 +*
1.122 +* ProcessBuffers
1.123 +* @param aSource
1.124 +* @param aDestination
1.125 +* @param aResult
1.126 +* all we have to do is find out how many source frames there
1.127 +* are to process and process them
1.128 +* finally returning process complete and fillin the status of the result
1.129 +*
1.130 +**/
1.131 +void CMMFPcm16ToImaAdpcmCodec::ProcessBuffers(const CMMFDataBuffer& aSource, CMMFDataBuffer& aDestination, CMMFSwCodec::TCodecProcessResult& aResult )
1.132 + {
1.133 + //[ calculate how many full buffers are to be processed ]
1.134 + const TUint srcLen = aSource.Data().Length();
1.135 + TInt numFullSrcFrames = srcLen/KSourceFrameSize;
1.136 +
1.137 + TUint8* pSrc = const_cast<TUint8*>(aSource.Data().Ptr());
1.138 + TUint8* pDst = const_cast<TUint8*>(aDestination.Data().Ptr());
1.139 + TInt dstBytesAdded = 0;
1.140 + // calculate number of pcm samples per source frame
1.141 + const TInt KSamplesPerFrame = KSourceFrameSize/(sizeof(TInt16));
1.142 +
1.143 + //[ convert all the buffers ]
1.144 + for( TInt count = 0; count < numFullSrcFrames; count++ )
1.145 + {
1.146 + i16PcmToImaAdpcm.Convert(pSrc, pDst, KSamplesPerFrame );
1.147 + pSrc += KSourceFrameSize;
1.148 + pDst += KCodedFrameSize;
1.149 + dstBytesAdded += KCodedFrameSize;
1.150 + }
1.151 + aResult.iSrcBytesProcessed = numFullSrcFrames*KSourceFrameSize;
1.152 + aResult.iDstBytesAdded = dstBytesAdded;
1.153 + aDestination.Data().SetLength( aResult.iDstBytesAdded);
1.154 + }
1.155 +
1.156 +/**
1.157 +*
1.158 +* BuffersStatus
1.159 +* @param source buffer containing the data to be coded
1.160 +* @param destination buffer containing the coded data
1.161 +* @return TBool EFalse indicates bad buffers
1.162 +*
1.163 +**/
1.164 +TBool CMMFPcm16ToImaAdpcmCodec::BuffersStatus( const CMMFDataBuffer* source, const CMMFDataBuffer* destination )
1.165 + {
1.166 + TBool status = EFalse;
1.167 +
1.168 + //[ demand source and destination positions are zero ]
1.169 + CMMFDataBuffer* pDst = const_cast<CMMFDataBuffer*>( destination );
1.170 + if( source->Position() || destination->Position() )
1.171 + {
1.172 + return status;
1.173 + }
1.174 +
1.175 + //[ Have we got full buffers ]
1.176 + TInt sourceBuffers = source->Data().Length()/KSourceFrameSize;
1.177 + TInt destBuffers = (pDst->Data().MaxLength())/KCodedFrameSize;
1.178 +
1.179 + if( sourceBuffers <= destBuffers ) // the sink can process the source
1.180 + {
1.181 + return ETrue; // note this precondition has been weakened in line with other codecs
1.182 + } // such that it can process partially full buffers
1.183 + // ie you can if you wish use larger buffers than needed and only partially
1.184 + // fill them. We do however expect all the input to be processed.
1.185 + return status;
1.186 + }