sl@0: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "MMFAudioPcm16ToImaAdpcmCodec.h" sl@0: sl@0: sl@0: EXPORT_C void TMMFAudioPcm16ToImaAdpcmCodec::SetState(const TMMFImaAdpcmCodecState& aState) sl@0: { sl@0: iState = aState ; sl@0: } sl@0: sl@0: EXPORT_C const TMMFImaAdpcmCodecState& TMMFAudioPcm16ToImaAdpcmCodec::GetState() sl@0: { sl@0: return iState; sl@0: } sl@0: sl@0: /** sl@0: * sl@0: * Convert sl@0: * @param aSrc sl@0: * @param aDst sl@0: * @param aSamples sl@0: * sl@0: */ sl@0: EXPORT_C void TMMFAudioPcm16ToImaAdpcmCodec::Convert(TUint8* aSrc, TUint8* aDst, TInt aSamples) sl@0: { sl@0: TInt val; // Current input sample value sl@0: TInt sign; // Current adpcm sign bit sl@0: TInt delta; // Current adpcm output value sl@0: TInt diff; // Difference between val and valprev sl@0: TInt step; // Stepsize sl@0: TInt valpred; // Predicted value sl@0: TInt vpdiff; // Current change to valpred sl@0: TInt index; // Current step change index sl@0: sl@0: TInt16* srcPtr=REINTERPRET_CAST(TInt16*, aSrc); sl@0: TInt16* src=srcPtr; sl@0: sl@0: iState.iPredicted = *aSrc++; sl@0: iState.iPredicted |= STATIC_CAST(TInt16, ((*aSrc++) << 8)); sl@0: sl@0: valpred = iState.iPredicted; sl@0: index = iState.iIndex; sl@0: ASSERT(index >= 0); sl@0: step = KStepSizeTable[index]; sl@0: sl@0: //Write block header sl@0: *aDst++ = STATIC_CAST( TUint8, valpred); sl@0: *aDst++ = STATIC_CAST( TUint8, valpred >> 8); sl@0: *aDst++ = STATIC_CAST( TUint8, index); sl@0: *aDst++ = 0; //reserved byte sl@0: src++; sl@0: aSamples --; sl@0: sl@0: for (; aSamples > 0; aSamples--) sl@0: { sl@0: val = *src; sl@0: src++; sl@0: sl@0: step = KStepSizeTable[index]; sl@0: sl@0: // Step 1 - compute difference with previous value sl@0: diff = val - valpred; sl@0: sign = (diff < 0) ? 8 : 0; sl@0: if ( sign ) diff = (-diff); sl@0: sl@0: // Step 2 - Divide and clamp sl@0: // Note: sl@0: // This code *approximately* computes: sl@0: // delta = diff*4/step; sl@0: // vpdiff = (delta+0.5)*step/4; sl@0: // but in shift step bits are dropped. The net result of this is sl@0: // that even if you have fast mul/div hardware you cannot put it to sl@0: // good use since the fixup would be too expensive. sl@0: // sl@0: delta = 0; sl@0: vpdiff = (step >> 3); sl@0: sl@0: if ( diff >= step ) sl@0: { sl@0: delta = 4; sl@0: diff -= step; sl@0: vpdiff += step; sl@0: } sl@0: step >>= 1; sl@0: if ( diff >= step ) sl@0: { sl@0: delta |= 2; sl@0: diff -= step; sl@0: vpdiff += step; sl@0: } sl@0: step >>= 1; sl@0: if ( diff >= step ) sl@0: { sl@0: delta |= 1; sl@0: vpdiff += step; sl@0: } sl@0: sl@0: // Step 3 - Update previous value sl@0: if ( sign ) sl@0: valpred -= vpdiff; sl@0: else sl@0: valpred += vpdiff; sl@0: sl@0: // Step 4 - Clamp previous value to 16 bits sl@0: if ( valpred > KClamp - 1 ) sl@0: valpred = KClamp - 1; sl@0: else if ( valpred < - KClamp ) sl@0: valpred = - KClamp; sl@0: sl@0: // Step 5 - Assemble value, update index and step values sl@0: delta |= sign; sl@0: sl@0: index += KIndexTable[delta]; sl@0: if ( index < 0 ) index = 0; sl@0: if ( index > 88 ) index = 88; sl@0: sl@0: // Step 6 - Output value sl@0: if (iBufferStep) sl@0: iBuffer = delta & 0x0f; sl@0: else sl@0: *aDst++ = STATIC_CAST( TInt8, ((delta << 4) & 0xf0) | iBuffer); sl@0: sl@0: iBufferStep = !iBufferStep; sl@0: } sl@0: sl@0: iState.iPredicted = STATIC_CAST(TInt16, valpred); sl@0: iState.iIndex = STATIC_CAST(TUint8, index); sl@0: } sl@0: sl@0: // IMA-ADPCM step variation table sl@0: const TInt TMMFAudioPcm16ToImaAdpcmCodec::KIndexTable[] = sl@0: { sl@0: -1, -1, -1, -1, 2, 4, 6, 8, sl@0: -1, -1, -1, -1, 2, 4, 6, 8 sl@0: }; sl@0: sl@0: const TInt TMMFAudioPcm16ToImaAdpcmCodec::KStepSizeTable[] = sl@0: { sl@0: 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, sl@0: 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, sl@0: 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, sl@0: 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, sl@0: 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, sl@0: 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, sl@0: 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, sl@0: 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, sl@0: 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 sl@0: }; sl@0: sl@0: sl@0: sl@0: