Update contrib.
1 // Copyright (c) 1997-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "MMFCodecBaseDefinitions.h"
17 #include "MMFAudioCodecBase.h"
18 #include <mmf/common/mmfpaniccodes.h>
20 // Base of Audio codecs
21 // These T Classes are "wrapped" by derived MMFCodecs, not exposed directly.
24 void Panic(TInt aPanicCode)
26 _LIT(KMMFCodecBaseDefinitionsPanicCategory, "MMFCodecBaseDefinitions");
27 User::Panic(KMMFCodecBaseDefinitionsPanicCategory, aPanicCode);
31 void TMMFImaAdpcmBaseCodecOld::ResetBuffer()
37 TBool TMMFImaAdpcmBaseCodecOld::OutputStep()
42 void TMMFImaAdpcmTo16PcmCodecOld::Convert(TUint8* aSrc, TUint8* aDst, TInt aSamples)
44 TInt delta; // Current adpcm output value
45 TInt step; // Stepsize
46 TInt valpred; // Predicted value
47 TInt vpdiff; // Current change to valpred
48 TInt index; // Current step change index
50 TInt channelCount=16;//for stereo only
54 //Read first sample and index from block header
55 iState[0].iPredicted = *aSrc++;
56 iState[0].iPredicted |= STATIC_CAST(TInt16, ((*aSrc++) << 8));
57 iState[0].iIndex = *aSrc++;
59 aSrc++; //skip reserved header byte
61 valpred = iState[0].iPredicted;
62 index = iState[0].iIndex;
65 //Write first sample to dest
66 *aDst++ = STATIC_CAST( TUint8, valpred);
67 *aDst++ = STATIC_CAST( TUint8, valpred >> 8);
73 iState[1].iPredicted = *aSrc++;
74 iState[1].iPredicted |= STATIC_CAST(TInt16, ((*aSrc++) << 8));
75 iState[1].iIndex = *aSrc++;
78 *aDst++ = STATIC_CAST( TUint8, iState[1].iPredicted);
79 *aDst++ = STATIC_CAST( TUint8, iState[1].iPredicted >> 8);
84 for ( ; aSamples > 0 ; aSamples-- )
86 // Step 1 - get the delta value
90 delta = iBuffer & 0xf;
94 delta = (iBuffer >> 4) & 0xf;
97 iBufferStep = !iBufferStep;
100 step = iStepSizeTable[index];
115 if ( valpred > (KClamp - 1) )
116 valpred = (KClamp - 1);
117 else if ( valpred < -KClamp )
120 index += iIndexTable[delta];
123 if ( index > KMaxImaAdpcmTableEntries )
124 index = KMaxImaAdpcmTableEntries;
126 *dst++ = STATIC_CAST( TUint8, valpred&KAndMask8bit);
127 *dst++ = STATIC_CAST( TUint8, (valpred>>8)&KAndMask8bit);
132 if (--channelCount == 8)
134 dst=aDst+2; //right channel
135 iState[0].iPredicted=STATIC_CAST(TInt16, valpred);
136 iState[0].iIndex=STATIC_CAST(TUint8,index);
137 valpred = iState[1].iPredicted;
138 index = iState[1].iIndex;
147 iState[1].iPredicted=STATIC_CAST(TInt16, valpred);
148 iState[1].iIndex=STATIC_CAST(TUint8, index);
149 valpred = iState[0].iPredicted;
150 index = iState[0].iIndex;
157 iState[0].iPredicted=STATIC_CAST(TInt16,valpred);
158 iState[0].iIndex=STATIC_CAST(TUint8,index);
162 void TMMF16PcmToImaAdpcmCodecOld::Convert(TUint8* aSrc, TUint8* aDst, TInt aSamples)
164 TInt val; // Current input sample value
165 TInt sign; // Current adpcm sign bit
166 TInt delta; // Current adpcm output value
167 TInt diff; // Difference between val and valprev
168 TInt step; // Stepsize
169 TInt valpred; // Predicted value
170 TInt vpdiff; // Current change to valpred
171 TInt index; // Current step change index
173 TInt16* srcPtr=REINTERPRET_CAST(TInt16*, aSrc);
176 TInt bufferCount=16;//for stereo only
184 iState[0].iPredicted = *aSrc++;
185 iState[0].iPredicted |= STATIC_CAST(TInt16, ((*aSrc++) << 8));
187 valpred = iState[0].iPredicted;
188 index = iState[0].iIndex;
190 step = iStepSizeTable[index];
193 *aDst++ = STATIC_CAST( TUint8, valpred);
194 *aDst++ = STATIC_CAST( TUint8, valpred >> 8);
195 *aDst++ = STATIC_CAST( TUint8, index);
196 *aDst++ = 0; //reserved byte
202 iState[1].iPredicted = *aSrc++;
203 iState[1].iPredicted |= STATIC_CAST(TInt16, ((*aSrc++) << 8));
205 //Write header for second channel
206 *aDst++ = STATIC_CAST( TUint8, iState[1].iPredicted);
207 *aDst++ = STATIC_CAST( TUint8, iState[1].iPredicted >> 8);
208 *aDst++ = STATIC_CAST( TUint8, iState[1].iIndex);
214 for (; aSamples > 0; aSamples--)
220 step = iStepSizeTable[index];
222 // Step 1 - compute difference with previous value
223 diff = val - valpred;
224 sign = (diff < 0) ? 8 : 0;
225 if ( sign ) diff = (-diff);
227 // Step 2 - Divide and clamp
229 // This code *approximately* computes:
230 // delta = diff*4/step;
231 // vpdiff = (delta+0.5)*step/4;
232 // but in shift step bits are dropped. The net result of this is
233 // that even if you have fast mul/div hardware you cannot put it to
234 // good use since the fixup would be too expensive.
237 vpdiff = (step >> 3);
259 // Step 3 - Update previous value
265 // Step 4 - Clamp previous value to 16 bits
266 if ( valpred > KClamp - 1 )
267 valpred = KClamp - 1;
268 else if ( valpred < - KClamp )
271 // Step 5 - Assemble value, update index and step values
274 index += iIndexTable[delta];
275 if ( index < 0 ) index = 0;
276 if ( index > 88 ) index = 88;
278 // Step 6 - Output value
280 iBuffer = delta & 0x0f;
282 *aDst++ = STATIC_CAST( TInt8, ((delta << 4) & 0xf0) | iBuffer);
284 iBufferStep = !iBufferStep;
288 if (--bufferCount==8)
290 src=srcPtr+1; //right channel
291 iState[0].iPredicted = STATIC_CAST(TInt16, valpred);
292 iState[0].iIndex = STATIC_CAST(TUint8, index);
293 valpred = iState[1].iPredicted;
294 index = iState[1].iIndex;
300 iState[1].iPredicted = STATIC_CAST(TInt16, valpred);
301 iState[1].iIndex = STATIC_CAST(TUint8, index);
302 valpred = iState[0].iPredicted;
303 index = iState[0].iIndex;
314 iState[0].iPredicted = STATIC_CAST(TInt16, valpred);
315 iState[0].iIndex = STATIC_CAST(TUint8, index);
319 // IMA-ADPCM step variation table
320 const TInt TMMFImaAdpcmBaseCodecOld::iIndexTable[16] =
322 -1, -1, -1, -1, 2, 4, 6, 8,
323 -1, -1, -1, -1, 2, 4, 6, 8
326 const TInt TMMFImaAdpcmBaseCodecOld::iStepSizeTable[89] =
328 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
329 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
330 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
331 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
332 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
333 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
334 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
335 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
336 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767