examples/Multimedia/MmfExCodec/MMFExPcm8ToPcm16Codec.cpp

00001 // MMFExPcm8Pcm16Codec.cpp
00002 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
00003 // All rights reserved.
00004 // This component and the accompanying materials are made available
00005 // under the terms of "Eclipse Public License v1.0"
00006 // which accompanies this distribution, and is available
00007 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 //
00009 // Initial Contributors:
00010 // Nokia Corporation - initial contribution.
00011 //
00012 // Contributors:
00013 //
00014 // Description:
00015 //
00016 
00017 #include "MMFExPcm8ToPcm16Codec.h"
00018 
00019 
00020 // Factory function
00021 CMMFCodec* CMMFExPcm8Pcm16Codec::NewL(TAny* )
00022         {
00023         CMMFExPcm8Pcm16Codec* self=new(ELeave) CMMFExPcm8Pcm16Codec();
00024         return self;
00025         }
00026 
00027 // Process source buffer and writes converted data to destination buffer
00028 // The codec expands 1 byte to 2 bytes
00029 TCodecProcessResult CMMFExPcm8Pcm16Codec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
00030         {
00031         TCodecProcessResult result;
00032         result.iStatus = TCodecProcessResult::EProcessIncomplete;
00033 
00034         // cast from generic CMMFBuffer to CMMFDataBuffer
00035         iSrc = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
00036         iDst = STATIC_CAST(CMMFDataBuffer*, &aDst);
00037 
00038         const TUint dstMaxLen = iDst->Data().MaxLength();
00039         if (!dstMaxLen)
00040                 User::Leave(KErrArgument);
00041 
00042         //don't scribble Destination (pDst) by only consuming enough source to fill pDst
00043         TUint srcUse = (dstMaxLen - iDst->Position()) / 2;
00044         const TUint srcLen = iSrc->Data().Length();
00045         const TUint sourceRemain = srcLen - iSrc->Position();
00046 
00047         //make sure we don't blow source by checking against remaining
00048         //and clipping to minimum remaining if necessary
00049         srcUse = (srcUse<sourceRemain ? srcUse : sourceRemain);
00050         
00051         //we need to cast away CONST even on the source, as the TClass needs a TUint8*
00052         TUint8* pSrc = CONST_CAST(TUint8*,iSrc->Data().Ptr());
00053         pSrc += iSrc->Position();
00054         TUint8* pDst = CONST_CAST(TUint8*,iDst->Data().Ptr());
00055         pDst += iDst->Position();
00056 
00057         Convert(pSrc, pDst, srcUse );
00058 
00059         if ((srcUse * 2) + iDst->Position() < dstMaxLen)
00060                 result.iStatus = TCodecProcessResult::EDstNotFilled;
00061 
00062         else if (srcUse + iSrc->Position() >= srcLen)
00063                 result.iStatus = TCodecProcessResult::EProcessComplete;
00064 
00065         result.iSrcBytesProcessed = srcUse;
00066         result.iDstBytesAdded = srcUse * 2;
00067 
00068         iDst->Data().SetLength(iDst->Position() + (srcUse * 2));
00069 
00070         return result;
00071         }
00072 
00073 
00074 // Helper to convert signed 8-bit to signed 16-bit
00075 void CMMFExPcm8Pcm16Codec::Convert(TUint8* aSrc, TUint8* aDst, TInt aSamples)
00076         {
00077         const TInt KAndMask8bit = 0xff;
00078         TInt s16;
00079         while (aSamples--)
00080                 { 
00081                 s16 = (*aSrc++)<<8;
00082                 *aDst++ = STATIC_CAST( TInt8, s16&KAndMask8bit);
00083                 *aDst++ = STATIC_CAST( TInt8, (s16>>8)&KAndMask8bit);
00084                 }
00085         }

Generated by  doxygen 1.6.2