mmlibs/mmfw/Codecs/Src/MMFCodecCommon/MMFAudioImaAdpcmToS16Codec.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     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 // MMFAudioImaAdpcmToS16PcmCodec.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "MMFAudioImaAdpcmToS16PcmCodec.h"
       
    19 
       
    20 /**
       
    21 *
       
    22 * Convert
       
    23 * @param aSrc
       
    24 * @param aDst
       
    25 * @param aSamples
       
    26 *
       
    27 */
       
    28 EXPORT_C void TMMFAudioImaAdpcmToS16PcmCodec::Convert(TUint8* aSrc, TUint8* aDst, TInt aSamples)
       
    29 	{
       
    30 	TInt delta;			// Current adpcm output value 
       
    31     TInt step;			// Stepsize
       
    32     TInt valpred;		// Predicted value 
       
    33     TInt vpdiff;		// Current change to valpred 
       
    34     TInt index;			// Current step change index 
       
    35 
       
    36 	//[Read first sample and index from block header
       
    37 	// we do not need to store the information across calls 
       
    38 	//since we process the entire block here]
       
    39 	valpred = (*aSrc++) & KAndMask8bit;
       
    40 	valpred |= STATIC_CAST(TInt16, ((*aSrc++) << 8));
       
    41 	index = *aSrc++;
       
    42 	TUint8* dst=aDst;
       
    43 
       
    44 	aSrc++; //skip reserved header byte
       
    45 
       
    46 	//Write first sample to dest
       
    47 	*aDst++ = STATIC_CAST( TUint8, valpred);
       
    48 	*aDst++ = STATIC_CAST( TUint8, valpred >> 8);
       
    49 	dst += 2;
       
    50 	aSamples --;
       
    51 
       
    52 	TBool theBufferStep = ETrue;
       
    53     TInt  bufferValue = 0;
       
    54 	for ( ; aSamples > 0 ; aSamples-- ) 
       
    55 		{ 
       
    56 		// Step 1 - get the delta value
       
    57 		if ( theBufferStep) 
       
    58 			{
       
    59 			bufferValue = *aSrc++;
       
    60 			delta = bufferValue & 0xf;
       
    61 			} 
       
    62 		else 
       
    63 			{
       
    64 			delta = (bufferValue >> 4) & 0xf;
       
    65 			}
       
    66 
       
    67 		theBufferStep = !theBufferStep;
       
    68 
       
    69 		ASSERT(index >= 0);
       
    70 		step = KStepSizeTable[index];
       
    71 
       
    72 		vpdiff = step>>3;
       
    73 		if ( delta & 4 ) 
       
    74 			vpdiff += step;
       
    75 		if ( delta & 2 ) 
       
    76 			vpdiff += step>>1;
       
    77 		if ( delta & 1 ) 
       
    78 			vpdiff += step>>2;
       
    79 
       
    80 		if ( delta & 8 )
       
    81 			valpred -= vpdiff;
       
    82 		else
       
    83 			valpred += vpdiff;
       
    84 
       
    85 		if ( valpred > (KClamp - 1) )
       
    86 			valpred = (KClamp - 1);
       
    87 		else if ( valpred < -KClamp )
       
    88 			valpred = -KClamp;
       
    89 
       
    90 		index += KIndexTable[delta];
       
    91 		if ( index < 0 ) 
       
    92 			index = 0;
       
    93 		if ( index > KMaxImaAdpcmTableEntries ) 
       
    94 			index = KMaxImaAdpcmTableEntries;
       
    95 
       
    96 		*dst++ = STATIC_CAST( TUint8, valpred&KAndMask8bit);
       
    97 		*dst++ = STATIC_CAST( TUint8, (valpred>>8) );
       
    98 		}
       
    99 	}
       
   100 
       
   101 
       
   102 
       
   103 // IMA-ADPCM step variation table 
       
   104 const TInt TMMFAudioImaAdpcmToS16PcmCodec::KIndexTable[] =
       
   105  	{
       
   106     -1, -1, -1, -1, 2, 4, 6, 8,
       
   107     -1, -1, -1, -1, 2, 4, 6, 8
       
   108 	};
       
   109 
       
   110 const TInt TMMFAudioImaAdpcmToS16PcmCodec::KStepSizeTable[89] = 
       
   111 	{
       
   112    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
       
   113    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
       
   114    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
       
   115    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
       
   116    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
       
   117    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
       
   118    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
       
   119    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
       
   120    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
       
   121 	};