os/mm/mmlibs/mmfw/tsrc/mmfunittest/SwCodecDevices/TSU_MMF_SWCODECDEVICES.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 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 // EPOC includes
    17 #include <e32base.h>
    18 #include <e32math.h>
    19 #include "types.h"
    20 #include "gsm610fr.h"
    21 #include "MMFAudioCodecBase.h"
    22 
    23 // Test system includes
    24 #include "TSU_MMF_SWCODECDEVICES.h"
    25 #include "TSU_MMF_SwCodecDevices_BufferSizes.h"
    26 #include "TSU_MMF_DeviceSuite.h"
    27 #include "MMFAudioSPcm16ToALawCodec.h"
    28 #include "MMFAudioALawToS16PcmCodec.h"
    29 #include "MMFAudioMuLawToS16PcmCodec.h"
    30 #include "MMFAudioSPcm16ToMuLawCodec.h"
    31 #include "mmfpcm16ToImaAdpcm.h"
    32 #include "MmfImaAdpcmtopcm16hwdevice.h"
    33 #include "GSM610.H"
    34 
    35 /**
    36 *
    37 * CTestStep_MMF_SwCodecDevices
    38 *
    39 **/
    40 CTestStep_MMF_SwCodecDevices::CTestStep_MMF_SwCodecDevices()
    41 	{
    42 	iHeapSize = 300000;
    43 	}
    44 
    45 /**
    46 *
    47 * ~CTestStep_MMF_SwCodecDevices
    48 *
    49 */
    50 CTestStep_MMF_SwCodecDevices::~CTestStep_MMF_SwCodecDevices()
    51 	{
    52 	}
    53 
    54 /**
    55 *
    56 * SetSuite
    57 * @param aPtr ref to the test suite
    58 *
    59 **/
    60 void CTestStep_MMF_SwCodecDevices::SetSuite( CTestSuite_MMF_SwCodecDevices* aPtr )
    61 	{
    62 	iRefSuite = aPtr;
    63 	}
    64 /**
    65 *
    66 * GetSuite 
    67 * @return CTestSuite_MMF_SwCodecDevices*
    68 *
    69 **/
    70 CTestSuite_MMF_SwCodecDevices* CTestStep_MMF_SwCodecDevices::GetSuite()
    71 	{
    72 	return iRefSuite;
    73 	}
    74 /**
    75 *
    76 * CTest_MMF_Gsm610
    77 *
    78 */
    79 CTest_MMF_Gsm610::CTest_MMF_Gsm610()
    80 	{
    81 
    82 	}
    83 
    84 /**
    85 *
    86 * CTest_MMF_SwCodecDevices_U_0001
    87 *
    88 */
    89 CTest_MMF_SwCodecDevices_U_0001::CTest_MMF_SwCodecDevices_U_0001()
    90 		{
    91 		// store the name of this test case
    92 		// this is the name that is used by the script file
    93 		iTestStepName = _L("MM-MMF-SWCODECDEVICES-U-0001-HP");
    94 		}
    95 
    96 /**
    97 *
    98 * CTest_MMF_SwCodecDevices_U_0001
    99 *
   100 **/
   101 CTest_MMF_SwCodecDevices_U_0001::~CTest_MMF_SwCodecDevices_U_0001()
   102 		{
   103 
   104 		}
   105 /**
   106 *
   107 *
   108 * ~CTest_MMF_Gsm610
   109 *
   110 */
   111 CTest_MMF_Gsm610::~CTest_MMF_Gsm610()
   112 	{
   113      // nothing to do ..
   114 	}
   115 
   116 /**
   117 *
   118 *
   119 * DoTestStepL
   120 *
   121 */
   122 TVerdict CTest_MMF_SwCodecDevices_U_0001::DoTestStepL()
   123 	{
   124 	__MM_HEAP_MARK;
   125 	TVerdict result = EPass ;
   126 	
   127 	CMmfGsm610ToPcm16HwDevice* pDecoder = CMmfGsm610ToPcm16HwDevice::NewL();
   128 	CleanupStack::PushL(pDecoder);
   129 	   
   130 	// do the same for the inverse operation
   131 	CMmfPcm16ToGsm610HwDevice* pEncoder = CMmfPcm16ToGsm610HwDevice::NewL();
   132 	CleanupStack::PushL(pEncoder);
   133 	CMMFSwCodec& theEncode = pEncoder->Codec();
   134 	
   135 	//[ create buffers of the appropriate size ]
   136 	const TInt srcBufferSize   = theEncode.SourceBufferSize();
   137 	const TInt sinkBufferSize  = theEncode.SinkBufferSize();
   138 	const TInt codedBufferSize = 76*4* KNumFramesInABuffer; // number of coded buffers * size of coded buffer 63*2 
   139 	iNumCodedFrames =  KNumFramesInABuffer;                                //XXX claculate these constants soon <GK>
   140 	
   141 	iScratchData    = CMMFDescriptorBuffer::NewL( codedBufferSize);
   142 
   143 	//[ pcm data buffers ]
   144 	iRefSrcData     = CMMFDescriptorBuffer::NewL( srcBufferSize );      
   145 	iDecodedData    = CMMFDescriptorBuffer::NewL( srcBufferSize ); 
   146 
   147 	//[ coded data buffers ]
   148 	iRefCodedData   = CMMFDescriptorBuffer::NewL( sinkBufferSize );  
   149 	iRefDecodedData = CMMFDescriptorBuffer::NewL( sinkBufferSize );
   150 	iCodedData      = CMMFDescriptorBuffer::NewL( sinkBufferSize );
   151 	
   152 	//[ for now process only the first few buffers 
   153 	// and encode the data]
   154 	INFO_PRINTF1(_L("Encoding Frame..."));
   155 	TInt srcFileSize = 0;
   156 	iSrcFile.Size( srcFileSize);
   157 	TInt buffLen =iRefSrcData->Data().MaxLength();
   158 	TInt numBuffers = srcFileSize/buffLen;
   159 	if( numBuffers > 4 )  // [ not all buffers are processed without error
   160 		// and this is the intention probably of the test sequences]
   161 		numBuffers = 4;  
   162 	TInt badBufferCount = 0;
   163 	TInt goodBufferCount = 0;
   164 	
   165 	for( TInt bufferCount = 0; bufferCount < numBuffers; bufferCount++ )
   166 		{
   167 		//[precondition pointers are ok]
   168 		ReadDataBufferL(iSrcFile, *iRefSrcData );
   169 		
   170 		ParseCodedDataL(iRefCodedData);
   171 		
   172 		CMMFSwCodec::TCodecProcessResult encodeRes = theEncode.ProcessL( *iRefSrcData, *iCodedData );
   173 		if( encodeRes !=  CMMFSwCodec::TCodecProcessResult::EProcessComplete  )
   174 			{
   175 			INFO_PRINTF1( _L("Error Failed to complete coding") );
   176 			return EFail;               
   177 			}
   178 		
   179 		//[ compare results ]
   180 		if( !CompareEncodeResults( iCodedData, iRefCodedData ) )
   181 			{
   182 			result = EFail;
   183 			badBufferCount++;
   184 			}
   185 		else
   186 			{
   187 			goodBufferCount++;
   188 			}
   189 			
   190 		}
   191 
   192 	 	//[ log number of good buffers & number of bad buffers ]
   193 	INFO_PRINTF4(_L("Good Buffers %d, Bad Buffers %d, Total Buffers %d"), 
   194 		goodBufferCount, badBufferCount, badBufferCount+goodBufferCount );
   195 
   196 	 //[pop data from the cleanup stack ]
   197 	 CleanupStack::PopAndDestroy(2, pDecoder); //pDecoder, theCodec
   198 
   199 	 delete iRefSrcData;     // reference source data
   200 	 iRefSrcData = NULL;
   201 	 delete iRefCodedData;   //reference coded data
   202 	 iRefCodedData = NULL;
   203 	 delete iRefDecodedData; // reference decoded data
   204 	 iRefDecodedData = NULL;
   205 	 delete iCodedData;      // buffer of coded data
   206 	 iCodedData = NULL;
   207 	 delete iDecodedData;    // buffer of actual decoded data
   208 	 iDecodedData = NULL;
   209 	 delete iScratchData;    // scratch data buffer
   210 	 iScratchData = NULL;
   211 
   212 	__MM_HEAP_MARKEND;
   213 	  
   214     return result;
   215 	}
   216 
   217 /**
   218 *
   219 * ParseCodedDataL
   220 *
   221 */
   222 void  CTest_MMF_Gsm610::ParseCodedDataL( CMMFDataBuffer* aBuffer )
   223 	{
   224    	codes frame0;
   225 	codes frame1;
   226 
   227 	//[ read all the coded data into the scratch buffer from reference file ]
   228 	ReadDataBufferL( iCodedFile, *iScratchData ); 
   229 	TUint8* dest = const_cast<TUint8*>(aBuffer->Data().Ptr());
   230 	TUint8* src  = const_cast<TUint8*>(iScratchData->Data().Ptr());
   231 
   232 	//[ for all the coded frames parse these frames to proper coded form ]
   233 	for( TInt count = 0; count < iNumCodedFrames; count++ )
   234 		{
   235 		// parse data to frame
   236 		ParseFrameL( frame0, src );
   237 		ParseFrameL( frame1, src );		
   238 		//[ finally pack the two frames into the coded data buffer ]
   239 		PackFrame0( &frame0, reinterpret_cast<TInt8*>(dest) );
   240 		PackFrame1( &frame1, reinterpret_cast<TInt8*>(dest) );
   241 		dest+= KGsmFrameSize;
   242 		}
   243 	
   244 	  aBuffer->Data().SetLength(KGsmFrameSize*iNumCodedFrames);
   245 	}
   246 /**
   247 *
   248 * ParseFrame
   249 * @param aFrame this stores the gsm frame in an unpacked structure
   250 * @param aBuffer this contains the reference file data as 76 words
   251 *
   252 */
   253 void CTest_MMF_Gsm610::ParseFrameL( struct codes& aFrame, TUint8* &aSrc )
   254 	{
   255 	for(TInt i = 0; i < 8; i++ )
   256 		{
   257 		aFrame.LARc[i]  = static_cast<TInt16> (*aSrc++ &KAndMask8bit );
   258 		aFrame.LARc[i] |= static_cast<TInt16>(*aSrc++ << 8 );
   259 		}
   260 	
   261 	for( TInt j = 0; j < 4; j++ )
   262 		{ 
   263 		aFrame.sfc[j].Nc  = static_cast<TInt16> (*aSrc++ &KAndMask8bit );
   264 		aFrame.sfc[j].Nc |= static_cast<TInt16>(*aSrc++ << 8 );
   265 		aFrame.sfc[j].bc  = static_cast<TInt16> (*aSrc++ &KAndMask8bit );
   266 		aFrame.sfc[j].bc |= static_cast<TInt16>(*aSrc++ << 8 );
   267 		aFrame.sfc[j].Mc  = static_cast<TInt16> (*aSrc++ &KAndMask8bit );
   268 		aFrame.sfc[j].Mc |= static_cast<TInt16>(*aSrc++ << 8 );
   269 		aFrame.sfc[j].xmaxc  = static_cast<TInt16> (*aSrc++ &KAndMask8bit );
   270 		aFrame.sfc[j].xmaxc |= static_cast<TInt16>(*aSrc++ << 8 );
   271 		
   272 		for( TInt k = 0; k < 13; k++ )
   273 			{
   274 			aFrame.sfc[j].xMc[k]  = static_cast<TInt16> (*aSrc++ &KAndMask8bit );
   275 			aFrame.sfc[j].xMc[k] |= static_cast<TInt16>(*aSrc++ << 8 );
   276 			}
   277 		
   278 		}
   279 	
   280 	}
   281  
   282 /**
   283 *
   284 * DoTestStepPreambleL
   285 *
   286 */
   287 enum TVerdict CTest_MMF_SwCodecDevices_U_0001::DoTestStepPreambleL(void)
   288 	{
   289 	TVerdict result = EPass;
   290     //[ connect to the file server ]
   291 	User::LeaveIfError( iFs.Connect());
   292 
   293 	//[ read the ref source data ]
   294 	if(!GetStringFromConfig(_L("SectionOne"), _L("SourceData01"), iSourceDataFile) ||
   295 	   !GetStringFromConfig(_L("SectionOne"), _L("CodedData01"), iCodedDataFile) ||
   296 	   !GetStringFromConfig(_L("SectionOne"), _L("DecodedData01"), iDecodedDataFile) )
   297 		{
   298 		return EInconclusive;
   299 		}
   300 
   301 	//[ open the data files]
   302 	OpenFileL(iSrcFile,iSourceDataFile );
   303 	OpenFileL(iCodedFile,iCodedDataFile );
   304 	OpenFileL(iDecodeFile,iDecodedDataFile);
   305 	return result;
   306 	}
   307 
   308 /**
   309 *
   310 * CompareCodedResults
   311 * @param aEncodedFrame
   312 * @param aRefFrame
   313 * @result TBool
   314 *
   315 */
   316 TBool CTest_MMF_Gsm610::CompareEncodeResults( CMMFDataBuffer* aEncoded, CMMFDataBuffer* aRefFrame )
   317 	{
   318     TBool result = ETrue;
   319 	
   320 	//[precondition no encoded frames == refframes ]
   321 	__ASSERT_DEBUG(aEncoded,Panic(EBadArgument));
   322 	__ASSERT_DEBUG(aRefFrame,Panic(EBadArgument));
   323 
   324 	TInt upperLimit = aEncoded->Data().Length()/KGsmFrameSize;
   325 
   326 	const CMMFDataBuffer* encoded = STATIC_CAST(const CMMFDataBuffer*, aEncoded);
   327 	const CMMFDataBuffer* reference = STATIC_CAST(CMMFDataBuffer*, aRefFrame);
   328 
   329 	TUint8* pFrames    = CONST_CAST(TUint8*,encoded->Data().Ptr());
   330 	TUint8* pRefFrames = CONST_CAST(TUint8*,reference->Data().Ptr());
   331 
   332 	TInt badFrameCount = 0;
   333 	TInt goodFrameCount = 0;
   334 
   335     //[ for all frames ]
   336     for( TInt index = 0; index < upperLimit; index++ )
   337 		{
   338 	 //[ compare src and ref frame]
   339 	   if( !CompareGsm610Frames( pFrames, pRefFrames ))
   340 		   {
   341 		   result = EFalse;
   342 		   badFrameCount++;
   343 		   INFO_PRINTF2(_L("Bad Frame Number: %d"), index );
   344 		   }
   345 	   else{
   346 		   goodFrameCount++;
   347 		   }
   348 
   349 	    //[ increment frame pointers by size of gsmencoded frame  
   350 	    pFrames    += KGsmFrameSize;
   351 		pRefFrames += KGsmFrameSize;
   352 		}
   353 
   354 		 	//[ log number of good frames & number of bad frames ]
   355 	INFO_PRINTF4(_L("Good Frames %d, Bad Frames %d, Total Frames %d"), 
   356 		goodFrameCount, badFrameCount, badFrameCount+goodFrameCount );
   357 
   358 	 return result ;
   359 	}
   360 
   361 /**
   362 *
   363 * CompareGsm610Frames
   364 * This function compares two encoded gsm610 frames
   365 * @result TBool Frames are the same or different
   366 *
   367 **/
   368 TBool CTest_MMF_Gsm610::CompareGsm610Frames( TUint8* aGsmFrame,TUint8* aRefGsmFrame )
   369 	{
   370 	 TBool result = ETrue;
   371 	 ASSERT( aGsmFrame );
   372 	 __ASSERT_DEBUG(aGsmFrame,Panic(EBadArgument));
   373 	 __ASSERT_DEBUG(aRefGsmFrame,Panic(EBadArgument));
   374 	 
   375 	 codes codeBuf0;
   376 	 codes codeBuf1;
   377 	 codes refCodeBuf0;
   378 	 codes refCodeBuf1; 
   379 
   380 	 UnpackFrame0(&codeBuf0, aGsmFrame );
   381 	 UnpackFrame1(&codeBuf1, aGsmFrame );
   382 	 UnpackFrame0(&refCodeBuf0, aRefGsmFrame );
   383 	 UnpackFrame1(&refCodeBuf1, aRefGsmFrame );
   384     
   385 	if( !Compare( codeBuf0, refCodeBuf0 ) ||
   386 		!Compare( codeBuf1, refCodeBuf1) )
   387 
   388 		{
   389 		 //LogGsmFrames( codeBuf0, refCodeBuf0 ); // add for debug reasons
   390 		 //LogGsmFrames( codeBuf1, refCodeBuf1 ); // add for debugging reasons
   391 		result = EFalse;
   392 		}
   393 
   394 	 return result ;
   395 	}
   396 
   397 /**
   398 *
   399 * LogGsmFrames
   400 * @param aGsmFrame
   401 * @param aRefGsmFrame 
   402 *
   403 **/
   404 void  CTest_MMF_Gsm610::LogGsmFrames( codes& aGsmFrame, codes& aRefGsmFrame )
   405 	{
   406     //[ print the quantized lar coefficients ]
   407    	INFO_PRINTF1(_L("Coded Frame:")); 
   408 	LogGsmFrame( aGsmFrame );
   409 	INFO_PRINTF1(_L("Reference Frame:")); 
   410 	LogGsmFrame( aRefGsmFrame );
   411 	}
   412 
   413 /**
   414 *
   415 * LogGsmFrame
   416 * prints a GsmFrame to the test log
   417 * @param aFrame 
   418 *
   419 **/
   420 void  CTest_MMF_Gsm610::LogGsmFrame ( codes& aFrame )
   421 	{
   422 	INFO_PRINTF2(_L("LARc[0] = %d"), aFrame.LARc[0]); 
   423 	INFO_PRINTF2(_L("LARc[1] = %d"), aFrame.LARc[1]);
   424 	INFO_PRINTF2(_L("LARc[2] = %d"), aFrame.LARc[2]);
   425 	INFO_PRINTF2(_L("LARc[3] = %d"), aFrame.LARc[3]);
   426 	INFO_PRINTF2(_L("LARc[4] = %d"), aFrame.LARc[4]); 
   427 	INFO_PRINTF2(_L("LARc[5] = %d"), aFrame.LARc[5]);
   428 	INFO_PRINTF2(_L("LARc[6] = %d"), aFrame.LARc[6]);
   429 	INFO_PRINTF2(_L("LARc[7] = %d"), aFrame.LARc[7]);
   430 
   431 	//[ for each sub frame print its data ]
   432 	for( TInt i = 0; i < 4; i++ )
   433 		{
   434          INFO_PRINTF2(_L("Nc = %d"), aFrame.sfc[i].Nc); 
   435 		 INFO_PRINTF2(_L("bc = %d"), aFrame.sfc[i].bc);
   436 		 INFO_PRINTF2(_L("Mc= %d"), aFrame.sfc[i].Mc);
   437 		 INFO_PRINTF2(_L("xmaxc = %d"), aFrame.sfc[i].xmaxc);
   438 		 INFO_PRINTF2(_L("xMc[0] = %d"), aFrame.sfc[i].xMc[0]);
   439 		 INFO_PRINTF2(_L("xMc[1] = %d"), aFrame.sfc[i].xMc[1]);
   440 		 INFO_PRINTF2(_L("xMc[2] = %d"), aFrame.sfc[i].xMc[2]);
   441 		 INFO_PRINTF2(_L("xMc[3] = %d"), aFrame.sfc[i].xMc[3]);
   442 		 INFO_PRINTF2(_L("xMc[4] = %d"), aFrame.sfc[i].xMc[4]);
   443 		 INFO_PRINTF2(_L("xMc[5] = %d"), aFrame.sfc[i].xMc[5]);
   444 		 INFO_PRINTF2(_L("xMc[6] = %d"), aFrame.sfc[i].xMc[6]);
   445 		 INFO_PRINTF2(_L("xMc[7] = %d"), aFrame.sfc[i].xMc[7]);
   446 		 INFO_PRINTF2(_L("xMc[8] = %d"), aFrame.sfc[i].xMc[8]);
   447 		 INFO_PRINTF2(_L("xMc[9] = %d"), aFrame.sfc[i].xMc[9]);
   448 		 INFO_PRINTF2(_L("xMc[10] = %d"), aFrame.sfc[i].xMc[10]);
   449 		 INFO_PRINTF2(_L("xMc[11] = %d"), aFrame.sfc[i].xMc[11]);
   450 		 INFO_PRINTF2(_L("xMc[12] = %d"), aFrame.sfc[i].xMc[12]);
   451 		}
   452 	}
   453 
   454 /**
   455 *
   456 * Compare
   457 *
   458 **/
   459 TBool CTest_MMF_Gsm610::Compare( codes& aFrame1, codes& aFrame2 )
   460 	{
   461      TBool result = ETrue;
   462      
   463 	if( (aFrame1.LARc[0] != aFrame2.LARc[0] ) ||
   464 	    (aFrame1.LARc[1] != aFrame2.LARc[1] ) ||
   465 	    (aFrame1.LARc[2] != aFrame2.LARc[2] ) ||
   466 	    (aFrame1.LARc[3] != aFrame2.LARc[3] ) ||
   467 	    (aFrame1.LARc[4] != aFrame2.LARc[4] ) ||
   468 	    (aFrame1.LARc[5] != aFrame2.LARc[5] ) ||
   469 	    (aFrame1.LARc[6] != aFrame2.LARc[6] ) ||
   470 	    (aFrame1.LARc[7] != aFrame2.LARc[7] ) )
   471 		{
   472 		result = EFalse;
   473 		}
   474 
   475 	for( TInt i = 0; i < 4; i++ )
   476 		{
   477 		if( 
   478          (aFrame1.sfc[i].Nc !=  aFrame2.sfc[i].Nc) ||
   479 		 (aFrame1.sfc[i].bc !=  aFrame2.sfc[i].bc) ||
   480 		 (aFrame1.sfc[i].Mc !=  aFrame2.sfc[i].Mc) ||
   481 		 (aFrame1.sfc[i].xmaxc !=	 aFrame2.sfc[i].xmaxc) ||
   482 		 (aFrame1.sfc[i].xMc[0] != aFrame2.sfc[i].xMc[0]) ||
   483 		 (aFrame1.sfc[i].xMc[1] !=	aFrame2.sfc[i].xMc[1]) ||
   484 		 (aFrame1.sfc[i].xMc[2] !=	aFrame2.sfc[i].xMc[2]) ||
   485 		 (aFrame1.sfc[i].xMc[3] !=	aFrame2.sfc[i].xMc[3]) ||
   486 		 (aFrame1.sfc[i].xMc[4] !=	aFrame2.sfc[i].xMc[4]) ||
   487 		 (aFrame1.sfc[i].xMc[5] !=	aFrame2.sfc[i].xMc[5]) ||
   488 		 (aFrame1.sfc[i].xMc[6] !=	aFrame2.sfc[i].xMc[6]) ||
   489 		 (aFrame1.sfc[i].xMc[7] !=	aFrame2.sfc[i].xMc[7]) ||
   490 		 (aFrame1.sfc[i].xMc[8] !=	aFrame2.sfc[i].xMc[8]) ||
   491 		 (aFrame1.sfc[i].xMc[9] !=	aFrame2.sfc[i].xMc[9]) ||
   492 		 (aFrame1.sfc[i].xMc[10] !=	aFrame2.sfc[i].xMc[10]) ||
   493 		 (aFrame1.sfc[i].xMc[11] !=	aFrame2.sfc[i].xMc[11]) ||
   494 		 (aFrame1.sfc[i].xMc[12] !=	aFrame2.sfc[i].xMc[12]))
   495 			{
   496 			result = EFail;
   497 			}
   498 		}
   499 
   500 	 return result;
   501 	}
   502 
   503 /**
   504 *
   505 * CompareCodedResults
   506 *
   507 */
   508 TBool CTest_MMF_Gsm610::CompareDecodeResults(CMMFDataBuffer* aEncoded, CMMFDataBuffer* aRefFrame )
   509 	{
   510     TBool result = ETrue;
   511 	
   512     //[ precondition the buffers are of the same length ]
   513 	__ASSERT_DEBUG(aEncoded,Panic(EBadArgument));
   514 	__ASSERT_DEBUG(aRefFrame,Panic(EBadArgument));
   515 	__ASSERT_DEBUG(aEncoded->Data().MaxLength() == aRefFrame->Data().MaxLength(),Panic(EBadArgument));
   516 	
   517 	TUint8 *pResults = CONST_CAST(TUint8*,aEncoded->Data().Ptr());  
   518 	TUint8 *pRefData = CONST_CAST(TUint8*,aRefFrame->Data().Ptr());
   519 	TInt numResults  = aEncoded->Data().MaxLength(); 
   520 
   521     if (Mem::Compare( pResults,numResults,pRefData,numResults)!=0)
   522         {
   523 		result = EFalse;
   524 		}
   525   
   526     return result ;
   527 	}
   528 /**
   529 *
   530 * ReadDataL
   531 * Reads entire data file into buffer 
   532 *
   533 **/
   534 void CTest_MMF_Gsm610::ReadDataL( CMMFDataBuffer*& aBuffer, const TDesC& aFile1 )
   535 	{
   536 	TFileName fileName = GetSuite()->DefaultPath();
   537 	fileName.Append(aFile1);
   538 
   539 	RFile file1;
   540 	User::LeaveIfError(file1.Open(iFs, fileName, EFileShareAny | EFileStream | EFileRead));
   541 	CleanupClosePushL(file1);
   542 	TInt fileSize = 0;
   543 	User::LeaveIfError(file1.Size(fileSize));
   544 	aBuffer = CMMFDescriptorBuffer::NewL(fileSize);
   545 	User::LeaveIfError(file1.Read( aBuffer->Data(),fileSize));
   546 	CleanupStack::PopAndDestroy(1); //file1
   547 	}
   548 
   549 /**
   550 *
   551 * OpenFileL
   552 * @param aFile
   553 * @param aFileName
   554 *
   555 **/
   556 void CTest_MMF_Gsm610::OpenFileL( RFile& aFile, const TDesC& aFileName )
   557 	{
   558 	User::LeaveIfError(aFile.Open(iFs, aFileName, EFileShareAny | EFileStream | EFileRead));
   559 	}
   560 
   561 /**
   562 *
   563 * CloseFileL
   564 * @param aFile
   565 *
   566 **/
   567 void CTest_MMF_Gsm610::CloseFileL( RFile& aFile )
   568 	{
   569 	aFile.Close();
   570 	}
   571 
   572 /**
   573 *
   574 * ReadDataBuffer
   575 * @param aFile
   576 * assumes file reads sufficient data
   577 *
   578 **/
   579 void CTest_MMF_Gsm610::ReadDataBufferL( const RFile& aFile, CMMFDataBuffer& aBuffer )
   580 	{
   581 	//[ The read will set the length of the descriptor to the number of bytes read]
   582 	User::LeaveIfError(aFile.Read( aBuffer.Data(),aBuffer.Data().MaxLength() ));
   583 	INFO_PRINTF2(_L("Bytes read = %d"), aBuffer.Data().Length() );
   584    	}
   585 
   586 /**
   587 *
   588 * FillPcmBuffer
   589 * @param aSrcBuffer
   590 * Fill a buffer with a sine wave
   591 *
   592 **/
   593 void CTest_MMF_Gsm610::FillPcmBuffer( CMMFDataBuffer& aSrcBuffer )
   594 	{
   595 	//fill the Src Buffer
   596 	 TUint8* pDst = CONST_CAST(TUint8*,aSrcBuffer.Data().Ptr());
   597 	 TInt length =  aSrcBuffer.Data().MaxLength();
   598 
   599 	 //[encode the data]
   600 	 TInt16 srcValue = 0;
   601 	 TReal val   = 0.0;
   602 	 TReal theta = 0.0; 
   603 	 for(TInt i=0; i<length/2; i++)
   604 		 {
   605 		 //[ assign data and place in buffer]
   606 		  theta = KPi*i/5; // fundamental of 800hz sampled @8khz has 20 db or
   607 		                      // better reproduction through gsm codec
   608 		  User::LeaveIfError(Math::Sin(val,theta));
   609           srcValue = static_cast<TInt16>( 1000 * val );
   610 	      *pDst++ = static_cast<TUint8>( srcValue & KAndMask8bit);
   611 		  *pDst++ = static_cast<TUint8>((srcValue >> 8) & KAndMask8bit );
   612 		  //INFO_PRINTF2(_L("Sine = %d"), srcValue );  //uncomment for debugging purposes     
   613 		 }
   614     
   615 	 aSrcBuffer.Data().SetLength(length);
   616 
   617 	}
   618 
   619 /**
   620 *
   621 * DoTestStepPostambleL
   622 * @result TVerdict
   623 * 
   624 */
   625 TVerdict CTest_MMF_SwCodecDevices_U_0001::DoTestStepPostambleL(void)
   626 	{
   627 	// close files
   628 	CloseFileL( iSrcFile );
   629 	CloseFileL( iCodedFile );
   630 	CloseFileL( iDecodeFile );
   631     //[ clean up the buffers etc ]
   632 	delete iRefSrcData;     // reference source data
   633 	delete iRefCodedData;   //reference coded data
   634 	delete iRefDecodedData; // reference decoded data
   635 	delete iCodedData;      // buffer of coded data
   636 	delete iDecodedData;    // buffer of actual decoded data
   637 	delete iScratchData;    // scratch data buffer
   638 	return EPass;
   639 	}
   640 
   641 /**
   642 *
   643 * UnpackFrame0
   644 * @param aCodeBuf 
   645 * @param pbuf
   646 *
   647 **/
   648 void CTest_MMF_Gsm610::UnpackFrame0(codes* aCodeBuf,  TUint8* pbuf)
   649     {
   650     TInt16* LAR = aCodeBuf->LARc;
   651 
   652     // unpack the LAR[0..7] from the first 4.5 bytes
   653     LAR[0] = (TInt16)((pbuf[0] & 0x3F));
   654     LAR[1] = (TInt16)(((pbuf[0] & 0xC0) >> 6) | ((pbuf[1] & 0x0F) << 2));
   655     LAR[2] = (TInt16)(((pbuf[1] & 0xF0) >> 4) | ((pbuf[2] & 0x01) << 4));
   656     LAR[3] = (TInt16)(((pbuf[2] & 0x3E) >> 1));
   657     LAR[4] = (TInt16)(((pbuf[2] & 0xC0) >> 6) | ((pbuf[3] & 0x03) << 2));
   658     LAR[5] = (TInt16)(((pbuf[3] & 0x3C) >> 2));
   659     LAR[6] = (TInt16)(((pbuf[3] & 0xC0) >> 6) | ((pbuf[4] & 0x01) << 2));
   660     LAR[7] = (TInt16)(((pbuf[4] & 0x0E) >> 1));
   661 
   662     // unpack Nc, bc, Mc, xmaxc, and xMc for each of the 4 sub-frames
   663     for(TInt i = 0; i < 4; i++)
   664         {
   665         struct sfcodes& c = aCodeBuf->sfc[i];
   666 #define sfb(x) (pbuf[4+i*7+x])
   667         c.Nc = (TInt16)(((sfb(0) & 0xF0) >> 4) | ((sfb(1) & 0x07) << 4));
   668         c.bc = (TInt16)(((sfb(1) & 0x18) >> 3));
   669         c.Mc = (TInt16)(((sfb(1) & 0x60) >> 5));
   670         c.xmaxc = (TInt16)(((sfb(1) & 0x80) >> 7) | ((sfb(2) & 0x1F) << 1));
   671         c.xMc[0] = (TInt16)(((sfb(2) & 0xE0) >> 5));
   672         c.xMc[1] = (TInt16)((sfb(3) & 0x07));
   673         c.xMc[2] = (TInt16)(((sfb(3) & 0x3C) >> 3));
   674         c.xMc[3] = (TInt16)(((sfb(3) & 0xC0) >> 6) | ((sfb(4) & 0x01) << 2));
   675         c.xMc[4] = (TInt16)(((sfb(4) & 0x0E) >> 1));
   676         c.xMc[5] = (TInt16)(((sfb(4) & 0x70) >> 4));
   677         c.xMc[6] = (TInt16)(((sfb(4) & 0x80) >> 7) | ((sfb(5) & 0x03) << 1));
   678         c.xMc[7] = (TInt16)(((sfb(5) & 0x1C) >> 2));
   679         c.xMc[8] = (TInt16)(((sfb(5) & 0xE0) >> 5));
   680         c.xMc[9] = (TInt16)((sfb(6) & 0x07));
   681         c.xMc[10] = (TInt16)(((sfb(6) & 0x38) >> 3));
   682         c.xMc[11] = (TInt16)(((sfb(6) & 0xC0) >> 6) | ((sfb(7) & 0x01) << 2));
   683         c.xMc[12] = (TInt16)(((sfb(7) & 0x0E) >> 1));
   684 #undef sfb
   685         }
   686     }
   687 
   688 /**
   689 *
   690 * UnpackFrame1
   691 * @param aCodeBuf 
   692 * @param pbuf
   693 *
   694 **/
   695 void CTest_MMF_Gsm610::UnpackFrame1(struct codes* aCodeBuf, TUint8* pbuf)
   696 {
   697     TInt16* LAR = aCodeBuf->LARc;
   698 
   699     // unpack the LAR[0..7] from the first 4.5 bytes
   700     LAR[0] = (TInt16)(((pbuf[32] & 0xF0) >> 4) | ((pbuf[33] & 0x03) << 4));
   701     LAR[1] = (TInt16)(((pbuf[33] & 0xFC) >> 2));
   702     LAR[2] = (TInt16)(((pbuf[34] & 0x1F)));
   703     LAR[3] = (TInt16)(((pbuf[34] & 0xE0) >> 5) | ((pbuf[35] & 0x03) << 3));
   704     LAR[4] = (TInt16)(((pbuf[35] & 0x3C) >> 2));
   705     LAR[5] = (TInt16)(((pbuf[35] & 0xC0) >> 6) | ((pbuf[36] & 0x03) << 2));
   706     LAR[6] = (TInt16)(((pbuf[36] & 0x1C) >> 2));
   707     LAR[7] = (TInt16)(((pbuf[36] & 0xE0) >> 5));
   708 
   709     // unpack Nc, bc, Mc, xmaxc, and xMc for each of the 4 sub-frames
   710     for(TInt i = 0; i < 4; i++)
   711         {
   712         struct sfcodes& c = aCodeBuf->sfc[i];
   713 #define sfb(x) (pbuf[37+i*7+x])
   714         c.Nc = (TInt16)(sfb(0) & 0x7F);
   715         c.bc = (TInt16)(((sfb(0) & 0x80) >> 7) | ((sfb(1) & 0x01) << 1));
   716         c.Mc = (TInt16)(((sfb(1) & 0x06) >> 1));
   717         c.xmaxc = (TInt16)(((sfb(1) & 0xF8) >> 3) | ((sfb(2) & 0x01) << 5));
   718         c.xMc[0] = (TInt16)(((sfb(2) & 0x0E) >> 1));
   719         c.xMc[1] = (TInt16)(((sfb(2) & 0x70) >> 4));
   720         c.xMc[2] = (TInt16)(((sfb(2) & 0x80) >> 7) | ((sfb(3) & 0x03) << 1));
   721         c.xMc[3] = (TInt16)(((sfb(3) & 0x1C) >> 2));
   722         c.xMc[4] = (TInt16)(((sfb(3) & 0xE0) >> 5));
   723         c.xMc[5] = (TInt16)(((sfb(4) & 0x07)));
   724         c.xMc[6] = (TInt16)(((sfb(4) & 0x38) >> 3));
   725         c.xMc[7] = (TInt16)(((sfb(4) & 0xC0) >> 6) | ((sfb(5) & 0x01) << 2));
   726         c.xMc[8] = (TInt16)(((sfb(5) & 0x0E) >> 1));
   727         c.xMc[9] = (TInt16)(((sfb(5) & 0x70) >> 4));
   728         c.xMc[10] = (TInt16)(((sfb(5) & 0x80) >> 7) | ((sfb(6) & 0x03) << 1));
   729         c.xMc[11] = (TInt16)(((sfb(6) & 0x1C) >> 2));
   730         c.xMc[12] = (TInt16)(((sfb(6) & 0xE0) >> 5));
   731 
   732 #undef sfb
   733         }
   734     }
   735 
   736 /**
   737 *
   738 * PackFrame0 
   739 * Pack the codewords of the even frame into pack buffer.
   740 * Packing as in MS gsm610 encoder.
   741 * @param aCodeBuf  Code words for one speech frame.
   742 * @param pbuf the output buffer
   743 *
   744 **/
   745 void CTest_MMF_Gsm610::PackFrame0(struct codes* aCodeBuf, TInt8* pbuf)
   746     {
   747     TInt16* LAR = aCodeBuf->LARc;
   748     
   749     // pack the LARc[0..7] into the first 4.5 bytes
   750     *pbuf++ = (TUint8)(((LAR[0]     ) & 0x3F) | ((LAR[1] << 6) & 0xC0));
   751     *pbuf++ = (TUint8)(((LAR[1] >> 2) & 0x0F) | ((LAR[2] << 4) & 0xF0));
   752     *pbuf++ = (TUint8)(((LAR[2] >> 4) & 0x01) | ((LAR[3] << 1) & 0x3E) | ((LAR[4] << 6) & 0xC0));
   753     *pbuf++ = (TUint8)(((LAR[4] >> 2) & 0x03) | ((LAR[5] << 2) & 0x3C) | ((LAR[6] << 6) & 0xC0));
   754     *pbuf   = (TUint8)(((LAR[6] >> 2) & 0x01) | ((LAR[7] << 1) & 0x0E));
   755     
   756     // pack Nc, bc, Mc, xmaxc, and xMc for each of the 4 sub-frames
   757     for(TInt i = 0; i < 4; i++)
   758         {
   759         struct sfcodes& c = aCodeBuf->sfc[i];
   760         *pbuf++ |= ((c.Nc << 4) & 0xF0);
   761         *pbuf++ = (TUint8)(((c.Nc >> 4) & 0x07) | ((c.bc << 3) & 0x18) | ((c.Mc << 5) & 0x60) | ((c.xmaxc << 7) & 0x80));
   762         *pbuf++ = (TUint8)(((c.xmaxc >> 1) & 0x1F) | ((c.xMc[0] << 5) & 0xE0));
   763         *pbuf++ = (TUint8)((c.xMc[1] & 0x07) | ((c.xMc[2] << 3) & 0x38) | ((c.xMc[3] << 6) & 0xC0));
   764         *pbuf++ = (TUint8)(((c.xMc[3] >> 2) & 0x01) | ((c.xMc[4] << 1) & 0x0E) | ((c.xMc[5] << 4) & 0x70) | ((c.xMc[6] << 7) & 0x80));
   765         *pbuf++ = (TUint8)(((c.xMc[6] >> 1) & 0x03) | ((c.xMc[7] << 2) & 0x1C) | ((c.xMc[8] << 5) & 0xE0));
   766         *pbuf++ = (TUint8)((c.xMc[9] & 0x07) | ((c.xMc[10] << 3) & 0x38) | ((c.xMc[11] << 6) & 0xC0));
   767         *pbuf   = (TUint8)(((c.xMc[11] >> 2) & 0x01) | ((c.xMc[12] << 1) & 0x0E));
   768         }
   769     }
   770 
   771 /**
   772 *
   773 * PackFrame1 
   774 * Pack the codewords of the even frame into pack buffer.
   775 * Packing as in MS gsm610 encoder.
   776 * @param aCodeBuf  Code words for one speech frame.
   777 * @param pbuf the output buffer
   778 *
   779 **/
   780 void CTest_MMF_Gsm610::PackFrame1(struct codes* aCodeBuf, TInt8* pbuf)
   781     {
   782     TInt16* LAR = aCodeBuf->LARc;
   783     
   784 	pbuf += (PACKSIZE / 2);
   785 
   786     // pack the LARc[0..7] into the first 4.5 bytes, starting with the msb of the first byte
   787     *pbuf++ = (TUint8) (pbuf[0] | ((LAR[0] << 4) & 0xF0));
   788     *pbuf++ = (TUint8)(((LAR[0] >> 4) & 0x03) | ((LAR[1] << 2) & 0xFC));
   789     *pbuf++ = (TUint8)(((LAR[2]     ) & 0x1F) | ((LAR[3] << 5) & 0xE0));
   790     *pbuf++ = (TUint8)(((LAR[3] >> 3) & 0x03) | ((LAR[4] << 2) & 0x3C) | ((LAR[5] << 6) & 0xC0));
   791     *pbuf++ = (TUint8)(((LAR[5] >> 2) & 0x03) | ((LAR[6] << 2) & 0x1C) | ((LAR[7] << 5) & 0xE0));
   792     
   793     // pack Nc, bc, Mc, xmaxc, and xMc for each of the 4 sub-frames
   794     for(TInt i = 0; i < 4; i++)
   795         {
   796         struct sfcodes& c = aCodeBuf->sfc[i];
   797         *pbuf++ = (TUint8)((c.Nc & 0x7F) | ((c.bc << 7) & 0x80));
   798         *pbuf++ = (TUint8)(((c.bc >> 1) & 0x01) | ((c.Mc << 1) & 0x06) | ((c.xmaxc << 3) & 0xF8));
   799         *pbuf++ = (TUint8)(((c.xmaxc >> 5) & 0x01) | ((c.xMc[0] << 1) & 0x0E) | ((c.xMc[1] << 4) & 0x70) | ((c.xMc[2] << 7) & 0x80));
   800         *pbuf++ = (TUint8)(((c.xMc[2] >> 1) & 0x03) | ((c.xMc[3] << 2) & 0x1C) | ((c.xMc[4] << 5) & 0xE0));
   801         *pbuf++ = (TUint8)(((c.xMc[5]) & 0x07) | ((c.xMc[6] << 3) & 0x38) | ((c.xMc[7] << 6) & 0xC0));
   802         *pbuf++ = (TUint8)(((c.xMc[7] >> 2) & 0x01) | ((c.xMc[8] << 1) & 0x0E) | ((c.xMc[9] << 4) & 0x70) | ((c.xMc[10] << 7) & 0x80));
   803         *pbuf++ = (TUint8)(((c.xMc[10] >> 1) & 0x03) | ((c.xMc[11] << 2) & 0x1C) | ((c.xMc[12] << 5) & 0xE0));
   804         }
   805     }
   806 
   807 /**
   808 *
   809 * CTest_MMF_SwCodecDevices_U_0001
   810 *
   811 */
   812 CTest_MMF_SwCodecDevices_U_0002::CTest_MMF_SwCodecDevices_U_0002()
   813 		{
   814 		// store the name of this test case
   815 		// this is the name that is used by the script file
   816 		iTestStepName = _L("MM-MMF-SWCODECDEVICES-U-0002-HP");
   817 		}
   818 /**
   819 *
   820 * CTest_MMF_SwCodecDevices_U_0001
   821 *
   822 **/
   823 CTest_MMF_SwCodecDevices_U_0002::~CTest_MMF_SwCodecDevices_U_0002()
   824 		{
   825 
   826 		}
   827 
   828 /**
   829 *
   830 *
   831 * DoTestStepL
   832 *
   833 **/
   834 TVerdict CTest_MMF_SwCodecDevices_U_0002::DoTestStepL()
   835 	{
   836 	__MM_HEAP_MARK;
   837 	TVerdict result = EPass ;
   838 	
   839 	 CMmfGsm610ToPcm16HwDevice* pDecoder = CMmfGsm610ToPcm16HwDevice::NewL();
   840      CleanupStack::PushL(pDecoder);
   841 	 //[ note this reference should be a ptr ]
   842      CMMFSwCodec& theCodec = pDecoder->Codec();
   843  
   844 	 //[ create buffers of the appropriate size ]
   845 	 const TInt srcBufferSize  = theCodec.SourceBufferSize();
   846 	 const TInt sinkBufferSize = theCodec.SinkBufferSize();
   847 	 const TInt codedBufferSize = 76*4* KNumFramesInABuffer; // number of coded buffers * size of coded buffer 63*2 
   848 	 iNumCodedFrames =  KNumFramesInABuffer;                                //XXX claculate these constants soon <GK>
   849 	
   850 	 iScratchData    = CMMFDescriptorBuffer::NewL( codedBufferSize);
   851 
   852 	 iRefCodedData   = CMMFDescriptorBuffer::NewL( srcBufferSize );  
   853 	 iCodedData      = CMMFDescriptorBuffer::NewL( srcBufferSize );
   854 	 iRefDecodedData = CMMFDescriptorBuffer::NewL( sinkBufferSize );
   855 	 iDecodedData    = CMMFDescriptorBuffer::NewL( sinkBufferSize); 
   856 	 
   857 	 //[ for now process only the first buffer ]
   858 	 //[encode the data]
   859 	 INFO_PRINTF1(_L("Decoding Frames..."));
   860 	 TInt codedFileSize = 0;
   861 	 iCodedFile.Size( codedFileSize);
   862 	
   863      TInt numBuffers = codedFileSize/srcBufferSize;
   864 	 if(numBuffers > 4 )
   865 		 numBuffers = 4;
   866 	 TInt badBufferCount = 0;
   867 	 TInt goodBufferCount = 0;
   868 	 for( TInt bufferCount = 0; bufferCount < numBuffers; bufferCount++ )
   869 		 {
   870 		 ReadDataBufferL(iDecodeFile, *iRefDecodedData );
   871 		 ParseCodedDataL(iCodedData);
   872  
   873 		 CMMFSwCodec::TCodecProcessResult decodeRes = theCodec.ProcessL( *iCodedData, *iDecodedData );
   874 		 if( decodeRes !=  CMMFSwCodec::TCodecProcessResult::EProcessComplete  )
   875 			 {
   876 			 INFO_PRINTF1( _L("Error Failed to complete decoding") );
   877 			 return EFail;               
   878 			 }
   879 	
   880 		 //[ compare results ]
   881 		 if(!CompareDecodeResults( iDecodedData, iRefDecodedData ))
   882 			 {
   883 			 result = EFail;
   884 		     badBufferCount++;
   885 			 }
   886 		 else
   887 			 {
   888 			 goodBufferCount++;
   889 			 }
   890 		
   891 		 }
   892 
   893 	 	//[ log number of good frames & number of bad frames ]
   894 	INFO_PRINTF4(_L("Good Frames %d, Bad Frames %d, Total Frames %d"), 
   895 		goodBufferCount, badBufferCount, badBufferCount+goodBufferCount );
   896 
   897 	 //[pop data from the cleanup stack ]
   898 	 CleanupStack::PopAndDestroy(pDecoder); 
   899 
   900 	 delete iRefCodedData;
   901 	 iRefCodedData = NULL;
   902 	 delete iRefDecodedData;
   903 	 iRefDecodedData = NULL;
   904 	 delete iDecodedData; 
   905 	 iDecodedData = NULL;
   906 	 delete iCodedData ;
   907 	 iCodedData = NULL;
   908 	 delete iScratchData;
   909 	 iScratchData = NULL;
   910 
   911 	__MM_HEAP_MARKEND;                         
   912     return result;
   913 	}
   914 
   915 /**
   916 *
   917 * DoTestStepPreambleL
   918 *
   919 */
   920 enum TVerdict CTest_MMF_SwCodecDevices_U_0002::DoTestStepPreambleL(void)
   921 	{
   922 	TVerdict result = EPass;
   923     //[ connect to the file server ]
   924 	User::LeaveIfError( iFs.Connect());
   925 
   926 	//[ read the ref source data ]
   927 	if(!GetStringFromConfig(_L("SectionOne"), _L("SourceData01"), iSourceDataFile) ||
   928 	   !GetStringFromConfig(_L("SectionOne"), _L("CodedData01"), iCodedDataFile) ||
   929 	   !GetStringFromConfig(_L("SectionOne"), _L("DecodedData01"), iDecodedDataFile) )
   930 		{
   931 		return EInconclusive;
   932 		}
   933 
   934 	//[ open the data files]
   935 	OpenFileL(iSrcFile,iSourceDataFile );
   936 	OpenFileL(iCodedFile,iCodedDataFile );
   937 	OpenFileL(iDecodeFile,iDecodedDataFile);
   938 
   939 	iScratchData = CMMFDescriptorBuffer::NewL(KCodedBufferSize);
   940 
   941 	return result;
   942 	}
   943 
   944 /**
   945 *
   946 * DoTestStepPostambleL
   947 * @result TVerdict
   948 * 
   949 */
   950 TVerdict CTest_MMF_SwCodecDevices_U_0002::DoTestStepPostambleL(void)
   951 	{
   952 	// close files
   953 	CloseFileL( iCodedFile );
   954 	CloseFileL( iDecodeFile );
   955     //[ clean up the buffers etc ]
   956 	delete iRefSrcData;     // reference source data
   957 	delete iRefCodedData;   //reference coded data
   958 	delete iRefDecodedData; // refernce decoded data
   959 	delete iCodedData;      // buffer of coded data
   960 	delete iDecodedData;    // buffer of actual decoded data
   961 	delete iScratchData;    // scratch data buffer
   962 	return EPass;
   963 	}
   964 
   965 /**
   966 *
   967 * CTest_MMF_SwCodecDevices_U_0003
   968 *
   969 **/
   970 CTest_MMF_SwCodecDevices_U_0003::CTest_MMF_SwCodecDevices_U_0003()
   971 		{
   972 		// store the name of this test case
   973 		// this is the name that is used by the script file
   974 		iTestStepName = _L("MM-MMF-SWCODECDEVICES-U-0003-HP");
   975 		}
   976 /**
   977 *
   978 * ~CTest_MMF_SwCodecDevices_U_0003
   979 *
   980 **/
   981 CTest_MMF_SwCodecDevices_U_0003::~CTest_MMF_SwCodecDevices_U_0003()
   982 	{
   983 
   984 	}
   985 
   986 /**
   987 *
   988 * DoTestStepL
   989 *
   990 **/
   991 TVerdict CTest_MMF_SwCodecDevices_U_0003::DoTestStepL()
   992 	{
   993 	__MM_HEAP_MARK;
   994 	TVerdict result = EPass ;
   995 	
   996 	 CMmfGsm610ToPcm16HwDevice* pDecoder = CMmfGsm610ToPcm16HwDevice::NewL();
   997      CleanupStack::PushL(pDecoder);
   998 	 //[ note this reference should be a ptr ]
   999      CMMFSwCodec& theCodec = pDecoder->Codec();
  1000  
  1001 	 // do the same for the inverse operation
  1002 	 CMmfPcm16ToGsm610HwDevice* pEncoder = CMmfPcm16ToGsm610HwDevice::NewL();
  1003      CleanupStack::PushL(pEncoder);
  1004      CMMFSwCodec& theEncode = pEncoder->Codec();
  1005 
  1006 	 //[ create buffers of the appropriate size ]
  1007 	 const TInt srcBufferSize = theEncode.SourceBufferSize();
  1008 	 const TInt sinkBufferSize = theEncode.SinkBufferSize();
  1009 
  1010 	 iRefSrcData     = CMMFDescriptorBuffer::NewL( srcBufferSize );   
  1011 	 iRefCodedData   = CMMFDescriptorBuffer::NewL( sinkBufferSize );  
  1012 	 iRefDecodedData = CMMFDescriptorBuffer::NewL( srcBufferSize );
  1013 	 iCodedData      = CMMFDescriptorBuffer::NewL( sinkBufferSize );   
  1014 	 iDecodedData    = CMMFDescriptorBuffer::NewL( srcBufferSize ); 
  1015 	 
  1016      //[ now get a sine wave of 800hz, code and decode and
  1017 	 // compare the results ]
  1018 	 FillPcmBuffer( *iRefSrcData );
  1019 
  1020 	 // encode
  1021 	CMMFSwCodec::TCodecProcessResult encodeRes = theEncode.ProcessL( *iRefSrcData, *iCodedData );
  1022 	 if( encodeRes !=  CMMFSwCodec::TCodecProcessResult::EProcessComplete  )
  1023 		 {
  1024 		 INFO_PRINTF1( _L("Error Failed to complete coding") );
  1025 		 return EFail;               
  1026 		 }
  1027 
  1028 	 //decode
  1029 	 CMMFSwCodec::TCodecProcessResult decodeRes = theCodec.ProcessL( *iCodedData, *iDecodedData );
  1030 	 if( decodeRes !=  CMMFSwCodec::TCodecProcessResult::EProcessComplete  )
  1031 		 {
  1032 		 INFO_PRINTF1( _L("Error Failed to complete decoding") );
  1033 		 return EFail;               
  1034 		 }
  1035 
  1036 	 //[ because the codec overwrites its input regenerate it ]
  1037 	 FillPcmBuffer( *iRefSrcData );
  1038 
  1039      //[ NOW COMPARE THE RESULTS DISTORTION < 18 DB ]
  1040 	 
  1041 	 TUint8 *pResults = (TUint8*)(iDecodedData->Data().Ptr());  
  1042 	 TUint8 *pRefData = (TUint8*)(iRefSrcData->Data().Ptr());
  1043 	 TInt numResults  = iDecodedData->Data().MaxLength(); 
  1044 	 TReal sum1 = 0.0;
  1045 	 TReal sum2 = 0.0;
  1046 	 TReal ratio = 0.0;
  1047 	 TInt16 temp1 = 0;
  1048 	 TInt16 temp2 = 0;
  1049 	 numResults /= 2; // compensate for bytes to short conversion
  1050 	 //[print the results to allow analysis]
  1051 	 for( TInt index = 0; index < numResults; index++ )
  1052 		 {
  1053 		 temp1  = static_cast<TInt16>((*pResults++) &KAndMask8bit);
  1054 		 temp1 |= static_cast<TInt16>((*pResults++ << 8));
  1055 		 sum1 += temp1*temp1;
  1056 		 temp2  = static_cast<TInt16>((*pRefData++) &KAndMask8bit);
  1057 		 temp2 |= static_cast<TInt16>((*pRefData++ << 8));
  1058 		 sum2 += (temp2-temp1)*(temp2-temp1);
  1059 		 //INFO_PRINTF3( _L("S %d D %d"),temp2, temp1 ); // add for debugging purposes
  1060 		 }
  1061 
  1062 	 //[calculate the ratio ]
  1063 	 ratio = sum1;
  1064 	 ratio /=sum2;
  1065 	 TReal sn = 0.0;
  1066 	 // calculate as 18db
  1067 	 Math::Log( sn, ratio );
  1068 	 sn *= 10;
  1069 
  1070 	 INFO_PRINTF2( _L("Signal to Noise Ratio @800Hz %f db"), sn );
  1071 #ifdef EABI
  1072 	 if( sn < 18 ) //[ @800hz a sn of less than 18db is deemed a failure 
  1073 		           // not that sn is a great measure of a voice coder's quality]
  1074 		 result = EFail ;
  1075 #else
  1076 	 if( sn < 14 ) //DEF086144 - Codec source buffer size is reduced
  1077 		 result = EFail ;
  1078 #endif
  1079 	 //[pop data from the cleanup stack ]
  1080 	 CleanupStack::PopAndDestroy(2, pDecoder); //pDecoder, theCodec,
  1081      delete iRefSrcData;   
  1082 	 iRefSrcData = NULL;
  1083 	 delete iRefCodedData;
  1084 	 iRefCodedData = NULL;
  1085 	 delete iRefDecodedData;
  1086 	 iRefDecodedData = NULL;
  1087 	 delete iCodedData;   
  1088 	 iCodedData = NULL;
  1089 	 delete iDecodedData;
  1090 	 iDecodedData = NULL;
  1091 	 
  1092 	__MM_HEAP_MARKEND;
  1093 
  1094     return result;
  1095 	}
  1096 
  1097 /**
  1098 *
  1099 * DoTestStepPreambleL
  1100 *
  1101 **/
  1102 TVerdict CTest_MMF_SwCodecDevices_U_0003::DoTestStepPreambleL(void)
  1103 	{
  1104 	return EPass;
  1105 	}
  1106 
  1107 /**
  1108 *
  1109 * DoTestStepPostambleL
  1110 *
  1111 **/
  1112 TVerdict CTest_MMF_SwCodecDevices_U_0003::DoTestStepPostambleL(void)
  1113 	{
  1114 	return EPass;
  1115 	}
  1116 
  1117 /**
  1118 *
  1119 * FillSrcBufferL
  1120 * @param aSrcData pointer to the src data which stores 16bit samples
  1121 * @param aNoSamples number of 16bit samples to store
  1122 * @param aOffset offset used to generate the samples (linear range)
  1123 **/
  1124 void TLawUtility::FillSrcBufferL( TUint8* aSrcData, TInt aNoSamples, TInt16 aOffset )
  1125 	{
  1126 	//[precondition aSrcData != NULL ]
  1127 	if( !aSrcData )
  1128 		User::Leave( KErrArgument );
  1129 	const TInt16 KUpperLimit = static_cast<TInt16>(aOffset + aNoSamples);
  1130 	TUint8* pDest = aSrcData ;
  1131 	for( TInt16 i = aOffset; i< KUpperLimit; i++ )
  1132 		{
  1133 	     *pDest++ = static_cast<TUint8>( i & 0xff);
  1134 		 *pDest++ = static_cast<TUint8>( (i >>8) &0xff );
  1135 		}
  1136 	}
  1137 
  1138 /**
  1139 *
  1140 * CompareCodedData
  1141 * @param aCodedData the data coded using symbian codec
  1142 * @param aRefCodedData the data coded using independent implementation
  1143 * @param aNoSamples the number of coded samples
  1144 *
  1145 **/
  1146 TInt TLawUtility::CompareCodedDataL(TUint8* aCodedData,TUint8* aRefCodedData, TInt aNoSamples )
  1147 	{
  1148 	TInt result = KErrNone;
  1149 	//[precondition aCodedData != NULL ]
  1150      if( !aCodedData )
  1151 		 User::Leave( KErrArgument);
  1152 	 //[preciondition aRefCodedData != NULL ]
  1153 	 if( !aRefCodedData )
  1154 		 User::Leave( KErrArgument );
  1155 
  1156 	 //[ use mem compare to compare the data Buffers ]
  1157 	if( Mem::Compare(aCodedData, aNoSamples, aRefCodedData, aNoSamples )!=0)
  1158 		{
  1159 		//[ data is not the same ]
  1160 		for( TInt count = 0; count < aNoSamples; count++ )
  1161 			{
  1162 		 //RDebug::Print(_L("c1 %u c2 %u"), *aCodedData++, *aRefCodedData++); Statement commented under DEF105143
  1163 		      *aCodedData++; 
  1164 		      *aRefCodedData++; 
  1165 			}
  1166 		result = KErrCorrupt;
  1167 		}
  1168 
  1169 	 return result;
  1170 	}
  1171 
  1172 /**
  1173 *
  1174 * AssembleValL
  1175 * @param aDecodedData
  1176 * @return decoded value
  1177 *
  1178 **/
  1179 TInt16 TLawUtility::AssembleValL(TUint8* aDecodedData)
  1180 	{
  1181 	TInt16 val;
  1182     if(!aDecodedData )
  1183 		User::Leave( KErrArgument);
  1184  
  1185 	//assemble the value 
  1186 	val  = static_cast<TInt16>( aDecodedData[0] &KAndMask8bit);  
  1187 	val |=  static_cast<TInt16>((aDecodedData[1] << 8 ));
  1188 
  1189 	return val;
  1190 	}
  1191 
  1192 /**
  1193 *
  1194 * SNRatio
  1195 *
  1196 **/
  1197 TReal TLawUtility::SNRatioL(TUint8* aDecodedData, TUint8* aSrcData, TInt aNoSamples )
  1198 	{
  1199 	const TReal KThreshold = 0.0001;
  1200     TReal ratio = 0.0;
  1201     //[precondition aDecodedData != NULL ]
  1202     if( !aDecodedData )
  1203 		User::Leave( KErrArgument );
  1204 	//[ precondition aSrcData != NULL ]
  1205 	if( !aSrcData )
  1206 		User::Leave( KErrArgument );
  1207     
  1208 	TReal   sumSig      = 0.0;    // numerator
  1209 	TReal   sumNoise    = 0.0; // denominator
  1210 	TInt    difference  = 0;
  1211 	TInt16  dataValue   = 0;
  1212 	TInt    decodeVal   = 0;
  1213 	for( TInt count = 0; count < aNoSamples; count++ )
  1214 		{
  1215         decodeVal = AssembleValL(aDecodedData);
  1216         dataValue = AssembleValL(aSrcData);
  1217 		difference = decodeVal - dataValue;
  1218 		sumSig += (decodeVal*decodeVal);     // sum of the squares of the signal
  1219 		sumNoise += (difference * difference );  // sum of the square of the difference
  1220 		aDecodedData+=2;
  1221 		aSrcData+=2;
  1222 		}
  1223 
  1224 	//[ guard against division by zero ]
  1225 	if( !( sumNoise >= KThreshold ))
  1226 		User::Leave( KErrUnderflow );
  1227 
  1228 	//[ calculate the sn ratio ]
  1229 	//[ 10log10( sumSig/SumNoise ]
  1230 	Math::Log( ratio, (sumSig/sumNoise) );
  1231     ratio *= 10;  // ratio = 10*log( x**2/(error**2)) in db
  1232     return ratio;
  1233 	}
  1234 
  1235 /**
  1236 *
  1237 * CompareSNRatiosL
  1238 * @param aCodecSN   codec under test SN ratio in db
  1239 * @param aCodecSN2  refernce codec SN ratio in db
  1240 * @param aThreshold difference allowed in db
  1241 * @result within tolerance
  1242 *
  1243 **/
  1244 TBool TLawUtility::CompareSNRatiosL( TReal aCodecSN, TReal aCodecSN2, TReal aTolerance )
  1245 	{
  1246     TBool result = ETrue; 
  1247 	TReal difference = (aCodecSN - aCodecSN2);
  1248 	//[ it would be nice to replace this with a abs function ?]
  1249 	if( difference < 0.0 )
  1250 		{
  1251          if( aTolerance > difference )
  1252 			 {
  1253 			 result = EFalse;
  1254 			 }
  1255 		}
  1256 	else
  1257 		{
  1258          if( aTolerance < difference )
  1259 			 {
  1260 			 result = EFalse;
  1261 			 }
  1262 		}
  1263 
  1264 	return result;
  1265 	}
  1266 
  1267 /**
  1268 *
  1269 * ComputeSNL compute the Signal to Noise Ratio
  1270 *
  1271 **/
  1272 TReal TLawUtility::ComputeSNL( TReal aSumSigSquared, TReal aSumErrorSquared )
  1273 	{
  1274 	 TReal sn = 0.0;
  1275      const TReal tolerance = 0.001;
  1276 	 //[precondition error is >= tolerance ]
  1277 	 if( aSumErrorSquared < tolerance )
  1278 		 User::Leave( KErrArgument );
  1279      //[claculate ratio safely ]
  1280 	 Math::Log( sn, (aSumSigSquared/aSumErrorSquared));
  1281 	 sn*= 10;
  1282 	 return sn;
  1283 	}
  1284 
  1285 /**
  1286 *
  1287 * SumSquaredL
  1288 *
  1289 **/
  1290 TReal TLawUtility::SumSquaredL( TUint8* aData, TInt aNoSamples )
  1291 	{
  1292 	//[precondition arg is ok ]
  1293      if( !aData )
  1294 		 {
  1295 		 User::Leave(KErrArgument);
  1296 		 }
  1297 
  1298 	 TUint8* pData = aData;
  1299 	 TInt16 sample ;
  1300 	 TReal sumSigSquared = 0.0;
  1301 	 for( TInt count = 0; count < aNoSamples; count++ )
  1302 		 {
  1303 		 sample  = static_cast<TInt16>( pData[0] &KAndMask8bit);  
  1304 		 sample |=  static_cast<TInt16>((pData[1] << 8 ));
  1305 		 sumSigSquared += (sample*sample);
  1306 		 pData+=2;
  1307 		 }
  1308 
  1309 	 return sumSigSquared;
  1310 	}
  1311 
  1312 /**
  1313 *
  1314 * SumErrorSquaredL
  1315 * @param aData
  1316 * @param aData2
  1317 * @param aNoSamples
  1318 * @result TReal
  1319 *
  1320 **/
  1321 TReal TLawUtility::SumErrorSquaredL( TUint8* aData, TUint8* aData2, TInt aNoSamples )
  1322 	{
  1323 	//[precondition aData is not NULL]
  1324 	 if( !aData )
  1325 		 {
  1326 		 User::Leave(KErrArgument);
  1327 		 }
  1328 
  1329 	 //[precondition aData2 is not NULL ]
  1330 	if( !aData2 )
  1331 		 {
  1332 		 User::Leave(KErrArgument);
  1333 		 }
  1334 
  1335 	 TUint8* pData = aData;
  1336 	 TUint8* pData2 = aData2;
  1337 	 TInt16 sample ;
  1338 	 TInt16 sample2;
  1339 	 TReal sumErrorSquared = 0.0;
  1340 	 TInt error;
  1341 	 for( TInt count = 0; count < aNoSamples; count++ )
  1342 		 {
  1343 		 error = 0;
  1344 		 sample  = static_cast<TInt16>( pData[0] &KAndMask8bit);  
  1345 		 sample |=  static_cast<TInt16>((pData[1] << 8 ));
  1346 		 sample2  = static_cast<TInt16>( pData2[0] &KAndMask8bit);  
  1347 		 sample2 |=  static_cast<TInt16>((pData2[1] << 8 ));
  1348 		 error = sample -sample2; // compute the error
  1349 		 sumErrorSquared += (error*error); // add error squared to the sum
  1350 		 pData  +=2;
  1351 		 pData2 +=2;
  1352 		 }
  1353 
  1354 	 return sumErrorSquared;
  1355 	}
  1356 
  1357 /**
  1358 *
  1359 * CTestMuLawCodec_U_0006
  1360 *
  1361 **/
  1362 CTestMuLawCodec_U_0006::CTestMuLawCodec_U_0006()
  1363 	{
  1364      //[ set test name]
  1365 	iTestStepName = _L("MM-MMF-SWCODECDEVICES-U-0006-HP");
  1366 	}
  1367 
  1368 /**
  1369 *
  1370 * LinearToMuLawSample
  1371 * @param aSample a 16 bit pcm sample
  1372 * @result Mu Law encoded sample
  1373 *
  1374 **/
  1375 TUint8 CTestMuLawCodec_U_0006::LinearToMuLawSample( TInt16 aSample)
  1376 	{
  1377 	const int KBias = 0x84;
  1378 	const int KClip = 32635;
  1379 	TInt sign = (aSample >> 8) & 0x80;
  1380 	if(sign)
  1381 		aSample = static_cast<TInt16>(-aSample);
  1382 	if(aSample > KClip)
  1383 		aSample = KClip;
  1384 	aSample = static_cast<TInt16>(aSample + KBias);
  1385 	TInt exponent = static_cast<TInt>( MuLawCompressTable[(aSample>>7) & 0xFF]);
  1386 	TInt mantissa = (aSample >> (exponent+3)) & 0x0F;
  1387 	TInt compressedByte = ~(sign | (exponent << 4) | mantissa);
  1388 	return static_cast<TUint8>( compressedByte );
  1389 	}
  1390 
  1391 /**
  1392 *
  1393 * ConvertPcmMuLaw
  1394 *
  1395 **/
  1396 void CTestMuLawCodec_U_0006::ConvertPcmMuLawL(TUint8* aSrcData, TUint8* aCodedData, TInt aNumSamples ) 
  1397 	{
  1398 	//[ precondition aSrcData ]
  1399      if( !aSrcData )
  1400 		 User::Leave( KErrArgument );
  1401 	 //[precondition aCodedData ]
  1402 	 if( !aCodedData )
  1403 		 User::Leave( KErrArgument );
  1404 
  1405 	 TUint8* pCoded = aCodedData;
  1406 	 TUint8* pData  = aSrcData ;
  1407 	 TInt16 pcmSample;
  1408 	 for( TInt count = 0; count < aNumSamples; count++ )
  1409 		 {
  1410           //[ code the data ]
  1411 		  pcmSample  = static_cast<TInt16>(pData[0]);
  1412 		  pcmSample |= static_cast<TInt16>((pData[1] << 8 )); 
  1413           *pCoded++ = LinearToMuLawSample(pcmSample);
  1414 		  pData+=2;
  1415 		 }
  1416 	}
  1417 
  1418 /**
  1419 *
  1420 * ConvertMuLawPcm
  1421 * 
  1422 **/
  1423 void CTestMuLawCodec_U_0006::ConvertMuLawPcmL(TUint8* aCoded, TUint8* aDecoded, TInt aNumSamples )
  1424 	{
  1425 	//[ precondition aCoded ]
  1426 	if( !aCoded )
  1427 		User::Leave( KErrArgument );
  1428 	//[precondition aDecoded ]
  1429 	if( !aDecoded )
  1430 		User::Leave( KErrArgument );
  1431 	
  1432 	TInt16  pcmSample;
  1433 	TUint8* pCoded   = aCoded;
  1434 	TUint8* pDecoded = aDecoded;
  1435 	//[ lets convert the data ]
  1436 	for(TInt count = 0; count < aNumSamples; count++ )
  1437 		{
  1438 		pcmSample = MuLawDecompressTable[*pCoded++];
  1439 		*pDecoded++ = static_cast<TUint8>( pcmSample & 0xFF);
  1440 		*pDecoded++ = static_cast<TUint8>((pcmSample >> 8 ) & 0xFF);
  1441 		}
  1442 	}
  1443 
  1444 /**
  1445 *
  1446 * DoTestStepL
  1447 *
  1448 **/
  1449 TVerdict CTestMuLawCodec_U_0006::DoTestStepL()
  1450 	{
  1451 	TVerdict result = EPass;
  1452 	const TInt KSrcBufferSize     = 400;    // small buffer size
  1453 	const TInt KHalfSrcBufferSize = 200;    // small buffer size
  1454 	const TInt KCodedBufferSize   = 200;    // small buffer size
  1455 	const TInt KLowerLimit        = -800; //lower limit of test range
  1456 	const TInt KUpperLimit        = 800;  // upper limit of test range +1
  1457 	
  1458 	//[ allocate memory buffers]
  1459 	TUint8* pSymbianSrcData = new(ELeave)TUint8[KSrcBufferSize];
  1460 	CleanupStack::PushL(pSymbianSrcData);
  1461 	TUint8* pIndependentSrcData = new(ELeave)TUint8[KSrcBufferSize];
  1462 	CleanupStack::PushL(pIndependentSrcData);
  1463 	TUint8* pSymbianCodedData = new(ELeave)TUint8[KCodedBufferSize];
  1464 	CleanupStack::PushL(pSymbianCodedData);
  1465 	TUint8* pIndependentCodedData = new(ELeave)TUint8[KCodedBufferSize];
  1466 	CleanupStack::PushL(pIndependentCodedData);
  1467 	TUint8* pSymbianDecodedData = new(ELeave)TUint8[KSrcBufferSize];
  1468 	CleanupStack::PushL(pSymbianDecodedData);
  1469 	TUint8* pIndependentDecodedData = new(ELeave)TUint8[KSrcBufferSize];
  1470 	CleanupStack::PushL(pIndependentDecodedData);
  1471 	
  1472     TMMFAudioMuLawToS16PcmCodec decoder;
  1473 	TMMFAudioSPcm16ToMuLawCodec encoder;
  1474 
  1475 	TLawUtility helper;
  1476 	TReal symbianCodecSN = 0.0;
  1477 	TReal independentCodecSN = 0.0;
  1478 
  1479 	TReal sumRefSig       = 0.0; // sum of sig squared
  1480 	TReal sumRefError     = 0.0; // sum of error sig squared
  1481 	TReal sumSymbianSig   = 0.0; // sum of sig squared
  1482 	TReal sumSymbianError = 0.0; // sum of error sig squared
  1483 
  1484 	//[ interate over a suitable range and process each buffer]
  1485 	for( TInt index = KLowerLimit; index < KUpperLimit; index+= KHalfSrcBufferSize )
  1486 		{
  1487 		TInt16 offset = static_cast<TInt16>( index);
  1488 		//[ fill the src buffers ]
  1489 		helper.FillSrcBufferL( pSymbianSrcData, KHalfSrcBufferSize, offset );
  1490 		helper.FillSrcBufferL( pIndependentSrcData, KHalfSrcBufferSize, offset );
  1491 
  1492 		//[encode the src data ]
  1493 		encoder.Convert( pSymbianSrcData, pSymbianCodedData, KHalfSrcBufferSize );
  1494 		ConvertPcmMuLawL(pIndependentSrcData,pIndependentCodedData,KHalfSrcBufferSize );
  1495 	
  1496 		//[ decode the data ]
  1497         decoder.Convert( pSymbianCodedData, pSymbianDecodedData, KHalfSrcBufferSize );		
  1498 	    ConvertMuLawPcmL( pIndependentCodedData,pIndependentDecodedData,KHalfSrcBufferSize);		
  1499 	
  1500 		//[ check both codecs code the data similarly]
  1501 		TInt errorCode =helper.CompareCodedDataL(pIndependentCodedData, pSymbianCodedData, KHalfSrcBufferSize );		
  1502 		if( errorCode != KErrNone )
  1503 			{
  1504 			INFO_PRINTF1(_L("Forward Transformation for Mu-Law codec is not conformant to ref codec"));
  1505 			User::LeaveIfError(errorCode);
  1506 			}
  1507 			//[ upate running total sums to be used for signal to noise
  1508 		// ratio calculations ]
  1509 		sumRefSig        += helper.SumSquaredL(pIndependentSrcData, KHalfSrcBufferSize);     
  1510 		sumRefError      += helper.SumErrorSquaredL(pIndependentSrcData,pIndependentDecodedData,KHalfSrcBufferSize); 
  1511 		sumSymbianSig    += helper.SumSquaredL(pSymbianSrcData,KHalfSrcBufferSize);
  1512 		sumSymbianError  += helper.SumErrorSquaredL(pSymbianSrcData,pSymbianDecodedData,KHalfSrcBufferSize);
  1513 		}
  1514  		
  1515 	const TReal KTolerance = 1; // allow for a 1 db tolerance
  1516 	symbianCodecSN     = helper.ComputeSNL(sumSymbianSig,sumSymbianError);
  1517 	independentCodecSN = helper.ComputeSNL(sumRefSig, sumRefError);
  1518     // Gamma = (dynamic range of codec /signal std deviation )
  1519 	INFO_PRINTF1(_L("We would expect S/N ration to be greater than 35db for an MuLaw codec with Gamma = 10"));
  1520 	INFO_PRINTF2(_L("Signal/Noise Ratio Symbian Codec %f"), symbianCodecSN ); 
  1521 	INFO_PRINTF2(_L("Signal/Noise Ratio Reference Codec %f"), independentCodecSN ); 
  1522 
  1523 	//[ compare the s/n ratio's of the two codec implementations]
  1524 	if( !helper.CompareSNRatiosL( symbianCodecSN, independentCodecSN, KTolerance ))
  1525 		{
  1526 		//[ fail the test because the s/n ratios were divergent ]
  1527 		result = EFail;
  1528 		}
  1529 	CleanupStack::PopAndDestroy(6,pSymbianSrcData); //pSymbianSrcData,pIndependentSrcData,
  1530 	                                                //pSymbianCodedData,pIndependentCodedData
  1531 	                                                //pSymbianDecodedData,pIndependentDecodedData 
  1532 	
  1533 	return result;
  1534 	}
  1535 
  1536 /**
  1537 *
  1538 *  DoTestStepPreambleL
  1539 *
  1540 **/
  1541 TVerdict CTestMuLawCodec_U_0006::DoTestStepPreambleL(void)
  1542 	{
  1543 	TVerdict result = EPass;
  1544 	return result; //nothing doing
  1545 	}
  1546 
  1547 /**
  1548 *
  1549 * DoTestStepPostambleL
  1550 *
  1551 **/
  1552 TVerdict CTestMuLawCodec_U_0006::DoTestStepPostambleL(void)
  1553 	{
  1554 	TVerdict result = EPass;
  1555 	return result; //nothing doing
  1556 	}
  1557 
  1558 /**
  1559 *
  1560 * Mu-Law Compression Table
  1561 *
  1562 **/
  1563 const TInt8 CTestMuLawCodec_U_0006::MuLawCompressTable[PcmToMuLawCompressionTableSize] =
  1564 {
  1565 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  1566 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  1567 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  1568 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  1569 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  1570 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  1571 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  1572 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  1573 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  1574 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  1575 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  1576 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  1577 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  1578 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  1579 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  1580 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  1581 };
  1582 
  1583 /**
  1584 *
  1585 * Mu-Law Decompression Table
  1586 *
  1587 **/
  1588 const TInt16 CTestMuLawCodec_U_0006::MuLawDecompressTable[MuLawToPcmCompressionTableSize]=
  1589 {
  1590 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
  1591 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
  1592 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
  1593 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
  1594 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
  1595 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
  1596 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
  1597 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
  1598 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
  1599 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
  1600 -876, -844, -812, -780, -748, -716, -684, -652,
  1601 -620, -588, -556, -524, -492, -460, -428, -396,
  1602 -372, -356, -340, -324, -308, -292, -276, -260,
  1603 -244, -228, -212, -196, -180, -164, -148, -132,
  1604 -120, -112, -104, -96, -88, -80, -72, -64,
  1605 -56, -48, -40, -32, -24, -16, -8, 0,
  1606 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
  1607 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
  1608 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
  1609 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
  1610 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
  1611 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
  1612 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
  1613 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
  1614 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
  1615 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
  1616 876, 844, 812, 780, 748, 716, 684, 652,
  1617 620, 588, 556, 524, 492, 460, 428, 396,
  1618 372, 356, 340, 324, 308, 292, 276, 260,
  1619 244, 228, 212, 196, 180, 164, 148, 132,
  1620 120, 112, 104, 96, 88, 80, 72, 64,
  1621 56, 48, 40, 32, 24, 16, 8, 0
  1622 };
  1623 
  1624 /**
  1625 *
  1626 * CTestALawCodec_U_0004
  1627 *
  1628 **/
  1629 CTestALawCodec_U_0004::CTestALawCodec_U_0004()
  1630 	{
  1631     //[ set test name ]
  1632 	iTestStepName = _L("MM-MMF-SWCODECDEVICES-U-0004-HP");
  1633 	}
  1634 
  1635 /**
  1636 *
  1637 * ConvertPcmALaw converts Pcm 16 to 8bit ALaw
  1638 * @param aSrcData The src data
  1639 * @param aCoded   The coded result
  1640 * @param aNumSamples The number of samples to be processed
  1641 * @precondition aSrcData is not NULL
  1642 * @precondition aCodedData is not NULL
  1643 * @precondition there is sufficient room in the destination
  1644 * to contain the coded samples
  1645 *
  1646 **/
  1647 void CTestALawCodec_U_0004::ConvertPcmALawL(TUint8* aSrcData, TUint8* aCodedData, TInt aNumSamples )
  1648 	{
  1649 	//[ precondition aSrcData ]
  1650      if( !aSrcData )
  1651 		 User::Leave( KErrArgument );
  1652 	 //[precondition aCodedData ]
  1653 	 if( !aCodedData )
  1654 		 User::Leave( KErrArgument );
  1655 
  1656 	 TUint8* pCoded = aCodedData;
  1657 	 TUint8* pData  = aSrcData ;
  1658 	 TInt16 pcmSample;
  1659 	 for( TInt count = 0; count < aNumSamples; count++ )
  1660 		 {
  1661           //[ code the data ]
  1662 		  pcmSample  = static_cast<TInt16>(pData[0]);
  1663 		  pcmSample |= static_cast<TInt16>((pData[1] << 8 )); 
  1664           *pCoded++ = LinearToALawSample(pcmSample);
  1665 		  pData+=2;
  1666 		 }
  1667 	}
  1668 
  1669 /**
  1670 *
  1671 * ConvertALawPcm converts from 8bit ALaw to Pcm16
  1672 * @param aCoded The coded data
  1673 * @param aDecoded The decoded result
  1674 * @param aNumSamples The number of samples to be processed
  1675 * @precondition aCoded is not NULL
  1676 * @precondition aDecoded is not NULL
  1677 * @precondition there is sufficient room in the destination
  1678 * to contain the coded samples
  1679 *
  1680 **/
  1681 void CTestALawCodec_U_0004::ConvertALawPcmL(TUint8* aCoded, TUint8* aDecoded, TInt aNumSamples )
  1682 	{
  1683 	 //[ precondition aSrcData ]
  1684      if( !aCoded )
  1685 		 User::Leave( KErrArgument );
  1686 	 //[precondition aCodedData ]
  1687 	 if( !aDecoded )
  1688 		 User::Leave( KErrArgument );
  1689    
  1690 	 TInt16  pcmSample;
  1691      TUint8* pCoded   = aCoded;
  1692 	 TUint8* pDecoded = aDecoded;
  1693 	 //[ lets convert the data ]
  1694      for(TInt count = 0; count < aNumSamples; count++ )
  1695 		 {
  1696           pcmSample = ALawDecompressTable[*pCoded++];
  1697           *pDecoded++ = static_cast<TUint8>(pcmSample & 0xFF);
  1698 		  *pDecoded++ = static_cast<TUint8>((pcmSample >> 8 ) & 0xFF);
  1699 		 }
  1700 	}
  1701 
  1702 /**
  1703 *
  1704 * LinearToALawSample converts a Pcm16 sample to ALaw
  1705 * @param aSample the PCM 16 sample to be converted
  1706 * @result coded result
  1707 * 
  1708 **/
  1709 TUint8 CTestALawCodec_U_0004::LinearToALawSample(TInt16 aSample)
  1710 	{
  1711 	const TInt KClip = 32635;
  1712 	TInt sign;
  1713 	TInt exponent;
  1714 	TInt mantissa;
  1715 	TUint8 compressedByte;
  1716 	sign = ((~aSample) >> 8) & 0x80;
  1717 	if(!sign)
  1718 		aSample = static_cast<TInt16>(-aSample);
  1719 	if(aSample > KClip)
  1720 		aSample = KClip;
  1721 	if(aSample >= 256)
  1722 		{
  1723 		exponent = static_cast<TInt>( ALawCompressTable[(aSample >> 8) & 0x7F]);
  1724 		mantissa = (aSample >> (exponent + 3) ) & 0x0F;
  1725 		compressedByte = static_cast<TUint8> ((exponent << 4) | mantissa);
  1726 		}
  1727 	else
  1728 		{
  1729 		compressedByte = static_cast<TUint8> (aSample >> 4);
  1730 		}
  1731 	compressedByte ^= (sign ^ 0x55);
  1732 	return compressedByte;
  1733 	}
  1734 
  1735 /**
  1736 *
  1737 * DoTestStepL
  1738 * 
  1739 **/
  1740 TVerdict CTestALawCodec_U_0004::DoTestStepL()
  1741 	{
  1742 	TVerdict result = EPass;
  1743 	const TInt KSrcBufferSize     = 400;    // small buffer size
  1744 	const TInt KHalfSrcBufferSize = 200;    // small buffer size
  1745 	const TInt KCodedBufferSize   = 200;    // small buffer size
  1746 	const TInt KLowerLimit        = -400; //lower limit of test range
  1747 	const TInt KUpperLimit        = 400;  // upper limit of test range +1
  1748 	
  1749 	//[ allocate memory buffers]
  1750 	TUint8* pSymbianSrcData = new(ELeave)TUint8[KSrcBufferSize];
  1751 	CleanupStack::PushL(pSymbianSrcData);
  1752 	TUint8* pIndependentSrcData = new(ELeave)TUint8[KSrcBufferSize];
  1753 	CleanupStack::PushL(pIndependentSrcData);
  1754 	TUint8* pSymbianCodedData = new(ELeave)TUint8[KCodedBufferSize];
  1755 	CleanupStack::PushL(pSymbianCodedData);
  1756 	TUint8* pIndependentCodedData = new(ELeave)TUint8[KCodedBufferSize];
  1757 	CleanupStack::PushL(pIndependentCodedData);
  1758 	TUint8* pSymbianDecodedData = new(ELeave)TUint8[KSrcBufferSize];
  1759 	CleanupStack::PushL(pSymbianDecodedData);
  1760 	TUint8* pIndependentDecodedData = new(ELeave)TUint8[KSrcBufferSize];
  1761 	CleanupStack::PushL(pIndependentDecodedData);
  1762 	
  1763 	TMMFAudioSPcm16ToAlawCodec encoder;
  1764 	TMMFAudioALawToS16PcmCodec decoder;
  1765     
  1766 	TLawUtility helper;
  1767 	TReal symbianCodecSN = 0.0;
  1768 	TReal independentCodecSN = 0.0;
  1769 
  1770 	TReal sumRefSig       = 0.0; // sum of sig squared
  1771 	TReal sumRefError     = 0.0; // sum of error sig squared
  1772 	TReal sumSymbianSig   = 0.0; // sum of sig squared
  1773 	TReal sumSymbianError = 0.0; // sum of error sig squared
  1774 
  1775 	//[ interate over a suitable range and process each buffer]
  1776 	for( TInt index = KLowerLimit; index < KUpperLimit; index+= KHalfSrcBufferSize )
  1777 		{
  1778 		TInt16 offset = static_cast<TInt16>( index);
  1779 		//[ fill the src buffers ]
  1780 		helper.FillSrcBufferL( pSymbianSrcData, KHalfSrcBufferSize, offset );
  1781 		helper.FillSrcBufferL( pIndependentSrcData, KHalfSrcBufferSize, offset );
  1782 
  1783 		//[encode the src data ]
  1784 		encoder.Convert( pSymbianSrcData, pSymbianCodedData, KHalfSrcBufferSize );
  1785 		ConvertPcmALawL(pIndependentSrcData,pIndependentCodedData,KHalfSrcBufferSize );
  1786 	
  1787 		//[ decode the data ]
  1788         decoder.Convert( pSymbianCodedData, pSymbianDecodedData, KHalfSrcBufferSize );		
  1789 	    ConvertALawPcmL( pIndependentCodedData,pIndependentDecodedData,KHalfSrcBufferSize);		
  1790 	
  1791 		//[ check both codecs code the data similarly]
  1792 		 TInt errorCode = helper.CompareCodedDataL(pIndependentCodedData, pSymbianCodedData, KHalfSrcBufferSize );
  1793 		 if( errorCode != KErrNone )
  1794 			 {
  1795 			 INFO_PRINTF1(_L("Forward Transformation for ALaw codec is not conformant to ref codec"));
  1796 			 User::LeaveIfError(errorCode);
  1797 			 }
  1798 	    
  1799 		//[ upate running total sums to be used for signal to noise
  1800 		// ratio calculations ]
  1801 		sumRefSig        += helper.SumSquaredL(pIndependentSrcData, KHalfSrcBufferSize);     
  1802 		sumRefError      += helper.SumErrorSquaredL(pIndependentSrcData,pIndependentDecodedData,KHalfSrcBufferSize); 
  1803 		sumSymbianSig    += helper.SumSquaredL(pSymbianSrcData,KHalfSrcBufferSize);
  1804 		sumSymbianError  += helper.SumErrorSquaredL(pSymbianSrcData,pSymbianDecodedData,KHalfSrcBufferSize);
  1805 		}
  1806 	const TReal KTolerance = 1; // allow for a 1 db tolerance
  1807 	symbianCodecSN     = helper.ComputeSNL(sumSymbianSig,sumSymbianError);
  1808 	independentCodecSN = helper.ComputeSNL(sumRefSig, sumRefError);
  1809     // Gamma = (dynamic range of codec /signal std deviation )
  1810 	INFO_PRINTF1(_L("We would expect S/N ration to be greater than 30db for an ALaw codec with Gamma = 10"));
  1811 	INFO_PRINTF2(_L("Signal/Noise Ratio Symbian Codec %f"), symbianCodecSN ); 
  1812 	INFO_PRINTF2(_L("Signal/Noise Ratio Reference Codec %f"), independentCodecSN ); 
  1813 
  1814 	//[ compare the s/n ratio's of the two codec implementations]
  1815 	if( !helper.CompareSNRatiosL( symbianCodecSN, independentCodecSN, KTolerance ))
  1816 		{
  1817 		//[ fail the test because the s/n ratios were divergent ]
  1818 		result = EFail;
  1819 		}
  1820 	
  1821 	CleanupStack::PopAndDestroy(6,pSymbianSrcData); //pSymbianSrcData,pIndependentSrcData,
  1822 	                                //pSymbianCodedData,pIndependentCodedData
  1823 	                                //pSymbianDecodedData,pIndependentDecodedData 
  1824 	return result;                  
  1825 	}
  1826 
  1827 /**
  1828 *
  1829 * DoTestStepPreambleL
  1830 *
  1831 **/
  1832 TVerdict CTestALawCodec_U_0004::DoTestStepPreambleL(void)
  1833 	{
  1834 	TVerdict result = EPass;
  1835 	return result; //nothing doing
  1836 	}
  1837 /**
  1838 *
  1839 * DoTestStepPostambleL
  1840 *
  1841 **/
  1842 TVerdict CTestALawCodec_U_0004::DoTestStepPostambleL(void)
  1843 	{
  1844 	TVerdict result = EPass;
  1845 	return result; //nothing doing
  1846 	}
  1847 /**
  1848 *
  1849 * ALaw Compression Table
  1850 *
  1851 **/
  1852 const TInt8 CTestALawCodec_U_0004::ALawCompressTable[PcmToALawCompressionTableSize] =
  1853 {
  1854 1,1,2,2,3,3,3,3,
  1855 4,4,4,4,4,4,4,4,
  1856 5,5,5,5,5,5,5,5,
  1857 5,5,5,5,5,5,5,5,
  1858 6,6,6,6,6,6,6,6,
  1859 6,6,6,6,6,6,6,6,
  1860 6,6,6,6,6,6,6,6,
  1861 6,6,6,6,6,6,6,6,
  1862 7,7,7,7,7,7,7,7,
  1863 7,7,7,7,7,7,7,7,
  1864 7,7,7,7,7,7,7,7,
  1865 7,7,7,7,7,7,7,7,
  1866 7,7,7,7,7,7,7,7,
  1867 7,7,7,7,7,7,7,7,
  1868 7,7,7,7,7,7,7,7,
  1869 7,7,7,7,7,7,7,7
  1870 };
  1871 
  1872 /**
  1873 *
  1874 * ALaw Decompression Table 
  1875 *
  1876 **/
  1877 const TInt16 CTestALawCodec_U_0004::ALawDecompressTable[ALawToPcmCompressionTableSize] =
  1878 {
  1879 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
  1880 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
  1881 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
  1882 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
  1883 -22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,
  1884 -30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,
  1885 -11008,-10496,-12032,-11520,-8960, -8448, -9984, -9472,
  1886 -15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568,
  1887 -344, -328, -376, -360, -280, -264, -312, -296,
  1888 -472, -456, -504, -488, -408, -392, -440, -424,
  1889 -88, -72, -120, -104, -24, -8, -56, -40,
  1890 -216, -200, -248, -232, -152, -136, -184, -168,
  1891 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
  1892 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
  1893 -688, -656, -752, -720, -560, -528, -624, -592,
  1894 -944, -912, -1008, -976, -816, -784, -880, -848,
  1895 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
  1896 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
  1897 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
  1898 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
  1899 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
  1900 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
  1901 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
  1902 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
  1903 344, 328, 376, 360, 280, 264, 312, 296,
  1904 472, 456, 504, 488, 408, 392, 440, 424,
  1905 88, 72, 120, 104, 24, 8, 56, 40,
  1906 216, 200, 248, 232, 152, 136, 184, 168,
  1907 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
  1908 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
  1909 688, 656, 752, 720, 560, 528, 624, 592,
  1910 944, 912, 1008, 976, 816, 784, 880, 848
  1911 };
  1912 
  1913 /**
  1914 *
  1915 * CTestIMaadCodec
  1916 *
  1917 **/
  1918 CTestIMaadCodec::CTestIMaadCodec()
  1919 	{
  1920 	//[ set test name ]
  1921 	iTestStepName = _L("MM-MMF-SWCODECDEVICES-U-0022-HP");
  1922 	}
  1923 
  1924 /**
  1925 *
  1926 * DoTestStepL
  1927 *
  1928 **/
  1929 TVerdict CTestIMaadCodec::DoTestStepL()
  1930 	{
  1931 	__MM_HEAP_MARK;    
  1932 	TVerdict result = EPass;
  1933 	TInt srcBufferSize;
  1934 	TInt sinkBufferSize;
  1935 	const TReal KExpectedSNRatioDb = 30.0; //30 db for now
  1936    
  1937 	//[ Create coder and decoder codecs ]
  1938 	CMMFPcm16ToImaAdpcmHwDevice* pHwDevice = CMMFPcm16ToImaAdpcmHwDevice::NewL();
  1939     CleanupStack::PushL( pHwDevice );
  1940 
  1941 	CMMFSwCodec& theCodec = pHwDevice->Codec();
  1942 
  1943 	CMMFImaAdpcmToPcm16CodecHwDevice* pHwDecoder = CMMFImaAdpcmToPcm16CodecHwDevice::NewL();
  1944     CleanupStack::PushL( pHwDecoder );
  1945 
  1946 	CMMFSwCodec& theDecoder = pHwDecoder->Codec();
  1947 
  1948 	//[ Create data buffers with position != 0]
  1949 	srcBufferSize  = 100; // arbitrary non zero size
  1950 	sinkBufferSize = 100;
  1951     CMMFDescriptorBuffer* pSrcBuffer =  CMMFDescriptorBuffer::NewL( srcBufferSize ); 
  1952 	CleanupStack::PushL( pSrcBuffer );
  1953 	
  1954 	CMMFDescriptorBuffer* pSinkBuffer = CMMFDescriptorBuffer::NewL( sinkBufferSize ); 
  1955 	CleanupStack::PushL( pSinkBuffer );
  1956 
  1957 	//[ trap & check error code ]
  1958 	TInt errCode;
  1959 	pSrcBuffer->Data().SetLength(srcBufferSize);
  1960 	pSinkBuffer->Data().SetLength(sinkBufferSize);
  1961 	pSrcBuffer->SetPosition(1);
  1962     TRAP( errCode, theCodec.ProcessL(*pSrcBuffer, *pSinkBuffer));
  1963 	if( errCode != KErrArgument )
  1964 		{
  1965 		result = EFail;
  1966 		return result;
  1967 		}
  1968 
  1969   	//[set position of sink buffer to nonzero value]
  1970 	pSrcBuffer->SetPosition(0);
  1971     pSinkBuffer->SetPosition(1);
  1972     TRAP( errCode, theCodec.ProcessL(*pSrcBuffer, *pSinkBuffer));
  1973 	if( errCode != KErrArgument )
  1974 		{
  1975 		result = EFail;
  1976 		return result;
  1977 		}
  1978 
  1979 	//[set position of sink and src to nonzero value ]
  1980 	pSrcBuffer->SetPosition(1);
  1981     pSinkBuffer->SetPosition(1);
  1982     TRAP( errCode, theCodec.ProcessL(*pSrcBuffer, *pSinkBuffer));
  1983 	if( errCode != KErrArgument )
  1984 		{
  1985 		result = EFail;
  1986 		return result;
  1987 		}
  1988 
  1989 	//[ reset the position of both buffers to zero ]
  1990     pSrcBuffer->SetPosition(0);
  1991     pSinkBuffer->SetPosition(0);
  1992     //[ set the src/sink buffer sizes to src and sink
  1993 	// buffer sizes and fill src with data ]
  1994 	CleanupStack::PopAndDestroy(2, pSrcBuffer); // pSrcBuffer, pSinkBuffer
  1995     
  1996 	//[Create Source & Sink and fill source data in ]
  1997 	srcBufferSize = theCodec.SourceBufferSize();
  1998 	pSrcBuffer  = CMMFDescriptorBuffer::NewL( srcBufferSize );
  1999 	CleanupStack::PushL( pSrcBuffer );
  2000 
  2001 	CMMFDescriptorBuffer* pDecodedBuffer = CMMFDescriptorBuffer::NewL( srcBufferSize );
  2002 	CleanupStack::PushL( pDecodedBuffer );
  2003 
  2004 	sinkBufferSize = theCodec.SinkBufferSize();
  2005 	pSinkBuffer = CMMFDescriptorBuffer::NewL( sinkBufferSize );
  2006     CleanupStack::PushL( pSinkBuffer );
  2007 
  2008 	pSrcBuffer->Data().SetLength(srcBufferSize);
  2009 	pDecodedBuffer->Data().SetLength(srcBufferSize);
  2010 	pSinkBuffer->Data().SetLength(sinkBufferSize);
  2011 
  2012 	//[ fill src buffer with ramp] 
  2013 	FillSrcBufferL( *pSrcBuffer );
  2014     // encode and decode the data
  2015     theCodec.ProcessL(*pSrcBuffer, *pSinkBuffer);
  2016 	theDecoder.ProcessL( *pSinkBuffer, *pDecodedBuffer );
  2017     
  2018 	if(!CompareResults( KExpectedSNRatioDb, pSrcBuffer, pDecodedBuffer))
  2019 		{
  2020 		//Test has failed because sn ratio was not good enough
  2021 		result = EFail;
  2022 		}
  2023 
  2024 	//[ clean up ]
  2025 	CleanupStack::PopAndDestroy( 5, pHwDevice ); // pHwDevice, pHwDecoder, pSrcBuffer, pDecodedBuffer, pSinkBuffer 
  2026 
  2027 	__MM_HEAP_MARKEND;
  2028 	return result;
  2029 	}
  2030 
  2031 /**
  2032 *
  2033 * DoTestStepPreambleL
  2034 *
  2035 **/
  2036 TVerdict CTestIMaadCodec::DoTestStepPreambleL(void)
  2037 	{
  2038     return EPass;
  2039 	}
  2040 
  2041 /**
  2042 *
  2043 * DoTestStepPostambleL
  2044 *
  2045 **/
  2046 TVerdict CTestIMaadCodec::DoTestStepPostambleL(void)
  2047 	{
  2048     return EPass;
  2049 	}
  2050 
  2051 
  2052 /**
  2053 *
  2054 * FillSrcBuffer
  2055 * @param aBuffer
  2056 * This function fills the buffer with a ramp of linear pcm16 data
  2057 *
  2058 **/
  2059 void CTestIMaadCodec::FillSrcBufferL( CMMFDescriptorBuffer& aBuffer )
  2060 	{
  2061 	 TInt slope = 2;
  2062      TInt dataLength = aBuffer.Data().Length(); 
  2063 	 TUint8* pData = const_cast<TUint8*>(aBuffer.Data().Ptr());
  2064 	 TInt noPc16Samples = dataLength/2;
  2065 	 ASSERT( noPc16Samples*slope < 32768 );
  2066 	 for( TInt16 count = 0; count < noPc16Samples ; count++ )
  2067 		 {
  2068 		 TInt16 pcmSample = static_cast<TInt16>( count * slope);
  2069           *pData++ = static_cast<TUint8>( pcmSample & 0xFF );
  2070 		  *pData++ = static_cast<TUint8>( ( pcmSample >> 8 ));
  2071 		 }
  2072 	}
  2073 
  2074 /**
  2075 *
  2076 * CompareResults
  2077 * @param aExpectedSNRatioDb
  2078 * @param aSrcBuffer
  2079 * @param aSinkBuffer
  2080 * @result TBool
  2081 * This function returns True if the computed Signal to Noise Ratio
  2082 * is Greater than or equal to the expected signal to noise ratio.
  2083 * The function will also return EFalse if any of the preconditions
  2084 * are violated.
  2085 * @precondition aSrcBuffer, aSinkBuffer are not NULL
  2086 * @precondition aSrcBuffer data lenegth == aSinkBuffer data length
  2087 * @precondition the data buffers contain pcm16 data
  2088 * 
  2089 **/
  2090 TBool CTestIMaadCodec::CompareResults( TReal aExpectedSNRatioDb, 
  2091 			                  CMMFDescriptorBuffer* aSrcBuffer,     
  2092 		                      CMMFDescriptorBuffer* aSinkBuffer)
  2093 	{
  2094      TBool result = EFalse;
  2095 
  2096 	 //[ precondition pointers are not NULL ]
  2097 	 if( !aSrcBuffer || !aSinkBuffer )
  2098 		 return result;
  2099 
  2100 	 //[ precondition buffer lengths are equal ]
  2101      TInt length = aSrcBuffer->Data().Length();
  2102 	 if( length != aSinkBuffer->Data().Length() )
  2103 		 return result;
  2104 
  2105 	 // buffers must be of even length
  2106 	 if( !(length % sizeof(TInt16) == 0 ))
  2107 		 return result;
  2108 
  2109      TInt pcmLength = length/2;
  2110      TReal sumSignalSquared = 0.0;
  2111 	 TReal sumNoiseSquared  = 0.0;
  2112      TUint8* pSrcData = const_cast<TUint8*>(aSrcBuffer->Data().Ptr());
  2113 	 TUint8* pDecodeData    = const_cast<TUint8*>(aSinkBuffer->Data().Ptr());
  2114      TInt16 sampleOriginal;
  2115 	 TInt16 sampleDecode;
  2116 	 for( TInt count = 0; count < pcmLength; count++ )
  2117 		 {
  2118 		  sampleOriginal  = static_cast<TInt16>( pSrcData[0] &KAndMask8bit);  
  2119 		  sampleOriginal |=  static_cast<TInt16>((pSrcData[1] << 8 ));	
  2120           sampleDecode    = static_cast<TInt16>( pDecodeData[0] &KAndMask8bit);  
  2121 		  sampleDecode   |=  static_cast<TInt16>((pDecodeData[1] << 8 )); 
  2122           pSrcData+=2;
  2123 		  pDecodeData+= 2;
  2124 		  sumSignalSquared += sampleOriginal * sampleOriginal;
  2125 		  TInt noise = sampleOriginal - sampleDecode ;
  2126           sumNoiseSquared  += noise * noise;
  2127 		 }
  2128      
  2129 	 //[ if the noise is low the signals are equivalent and
  2130 	 // overflow can be avoided ]
  2131 	 if( sumNoiseSquared < 0.001 )
  2132 		 {
  2133 		 result = ETrue;
  2134 		 return result;
  2135 		 }
  2136 	 TReal computedSNRatioDb;
  2137 	 Math::Log( computedSNRatioDb, sumSignalSquared/sumNoiseSquared );	 
  2138 	 computedSNRatioDb *= 10;
  2139 
  2140 	 //[ compare claculated s/n ratio against expected ]
  2141      if( computedSNRatioDb >= aExpectedSNRatioDb )
  2142 		 result = ETrue;
  2143 
  2144      return result;
  2145 	}