📄 adpcm.c
字号:
/***********************************************************
Copyright 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
******************************************************************/
/*
** Intel/DVI ADPCM coder/decoder.
**
** The algorithm for this coder was taken from the IMA Compatability Project
** proceedings, Vol 2, Number 2; May 1992.
**
** Version 1.2, 18-Dec-92.
**
*/
#include "adpcm.h"
#include "config.h"
#include <stdio.h>
#ifndef __STDC__
#define signed
#endif
/* Intel ADPCM step variation table */
static int16 indexTable[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8,
};
static int16 stepsizeTable[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
void Adpcm_Encoder(int16 *indata, int8 *outdata, int16 len, adpcm_state *state)
{
int16 *inp; /* Input buffer pointer */
int8 *outp; /* output buffer pointer */
int16 val; /* Current input sample value */
int16 sign; /* Current adpcm sign bit */
int16 delta; /* Current adpcm output value */
int16 diff; /* Difference between val and valprev */
int16 step_L; /* Stepsize */
int16 step_R;
int16 valprev_L; /* Predicted output value */
int16 valprev_R;
int16 vpdiff; /* Current change to valpred */
int16 index_L; /* Current step change index */
int16 index_R;
int16 outputbuffer; /* place to keep previous 4-bit value */
outp = (int8 *)outdata;
inp = indata;
valprev_L = state->valprev_L;
index_L = state->index_L;
step_L = stepsizeTable[index_L];
valprev_R = state->valprev_R;
index_R = state->index_R;
step_R = stepsizeTable[index_R];
for ( ; len > 0 ; len-- ) {
/****************/
/* LEFT CHANNEL */
/****************/
val = *inp++;
/* Step 1 - compute difference with previous value */
diff = val - valprev_L;
sign = (diff < 0) ? 8 : 0;
if ( sign ) diff = (-diff);
/* Step 2 - Divide and clamp */
/* Note:
** This code *approximately* computes:
** delta = diff*4/step;
** vpdiff = (delta+0.5)*step/4;
** but in shift step bits are dropped. The net result of this is
** that even if you have fast mul/div hardware you cannot put it to
** good use since the fixup would be too expensive.
*/
delta = 0;
vpdiff = (step_L >> 3);
if ( diff >= step_L ) {
delta = 4;
diff -= step_L;
vpdiff += step_L;
}
step_L >>= 1;
if ( diff >= step_L ) {
delta |= 2;
diff -= step_L;
vpdiff += step_L;
}
step_L >>= 1;
if ( diff >= step_L ) {
delta |= 1;
vpdiff += step_L;
}
/* Step 3 - Update previous value */
if ( sign )
valprev_L -= vpdiff;
else
valprev_L += vpdiff;
/* Step 4 - Clamp previous value to 16 bits */
if ( valprev_L > 32767 )
valprev_L = 32767;
else if ( valprev_L < -32768 )
valprev_L = -32768;
/* Step 5 - Assemble value, update index and step values */
delta |= sign;
index_L += indexTable[delta];
if ( index_L < 0 ) index_L = 0;
if ( index_L > 88 ) index_L = 88;
step_L = stepsizeTable[index_L];
/* Step 6 - Output value */
outputbuffer = (delta << 4) & 0xf0;
/****************/
/* RIGHT CHANNEL */
/****************/
val = *inp++;
/* Step 1 - compute difference with previous value */
diff = val - valprev_R;
sign = (diff < 0) ? 8 : 0;
if ( sign ) diff = (-diff);
/* Step 2 - Divide and clamp */
/* Note:
** This code *approximately* computes:
** delta = diff*4/step;
** vpdiff = (delta+0.5)*step/4;
** but in shift step bits are dropped. The net result of this is
** that even if you have fast mul/div hardware you cannot put it to
** good use since the fixup would be too expensive.
*/
delta = 0;
vpdiff = (step_R >> 3);
if ( diff >= step_R ) {
delta = 4;
diff -= step_R;
vpdiff += step_R;
}
step_R >>= 1;
if ( diff >= step_R ) {
delta |= 2;
diff -= step_R;
vpdiff += step_R;
}
step_R >>= 1;
if ( diff >= step_R ) {
delta |= 1;
vpdiff += step_R;
}
/* Step 3 - Update previous value */
if ( sign )
valprev_R -= vpdiff;
else
valprev_R += vpdiff;
/* Step 4 - Clamp previous value to 16 bits */
if ( valprev_R > 32767 )
valprev_R = 32767;
else if ( valprev_R < -32768 )
valprev_R = -32768;
/* Step 5 - Assemble value, update index and step values */
delta |= sign;
index_R += indexTable[delta];
if ( index_R < 0 ) index_R = 0;
if ( index_R > 88 ) index_R = 88;
step_R = stepsizeTable[index_R];
/* Step 6 - Output value */
*outp++ = (delta & 0x0f) | outputbuffer;
}
state->valprev_L = valprev_L;
state->index_L = index_L;
state->valprev_R = valprev_R;
state->index_R = index_R;
}
void Adpcm_Decoder(int8 *indata, int16 *outdata, int16 len, adpcm_state *state)
{
int8 *inp; /* Input buffer pointer */
int16 *outp; /* output buffer pointer */
int16 sign; /* Current adpcm sign bit */
int16 delta; /* Current adpcm output value */
int16 step_L; /* Stepsize */
int16 step_R;
int16 valprev_L; /* Predicted value */
int16 valprev_R;
int16 vpdiff; /* Current change to valpred */
int16 index_L; /* Current step change index */
int16 index_R;
int16 inputbuffer; /* place to keep next 4-bit value */
outp = outdata;
inp = (int8 *)indata;
valprev_L = state->valprev_L;
index_L = state->index_L;
step_L = stepsizeTable[index_L];
valprev_R = state->valprev_R;
index_R = state->index_R;
step_R = stepsizeTable[index_R];
for ( ; len > 0 ; len-- ) {
/****************/
/* LEFT CHANNEL */
/****************/
/* Step 1 - get the delta value and compute next index */
inputbuffer = *inp++;
delta = (inputbuffer >> 4) & 0xf;
/* Step 2 - Find new index value (for later) */
index_L += indexTable[delta];
if ( index_L < 0 ) index_L = 0;
if ( index_L > 88 ) index_L = 88;
/* Step 3 - Separate sign and magnitude */
sign = delta & 8;
delta = delta & 7;
/* Step 4 - Compute difference and new predicted value */
/*
** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
** in adpcm_coder.
*/
vpdiff = step_L >> 3;
if ( delta & 4 ) vpdiff += step_L;
if ( delta & 2 ) vpdiff += step_L>>1;
if ( delta & 1 ) vpdiff += step_L>>2;
if ( sign )
valprev_L -= vpdiff;
else
valprev_L += vpdiff;
/* Step 5 - clamp output value */
if ( valprev_L > 32767 )
valprev_L = 32767;
else if ( valprev_L < -32768 )
valprev_L = -32768;
/* Step 6 - Update step value */
step_L = stepsizeTable[index_L];
/* Step 7 - Output value */
*outp++ = valprev_L;
/*****************/
/* RIGHT CHANNEL */
/*****************/
/* Step 1 - get the delta value and compute next index */
delta = inputbuffer & 0xf;
/* Step 2 - Find new index value (for later) */
index_R += indexTable[delta];
if ( index_R < 0 ) index_R = 0;
if ( index_R > 88 ) index_R = 88;
/* Step 3 - Separate sign and magnitude */
sign = delta & 8;
delta = delta & 7;
/* Step 4 - Compute difference and new predicted value */
/*
** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
** in adpcm_coder.
*/
vpdiff = step_R >> 3;
if ( delta & 4 ) vpdiff += step_R;
if ( delta & 2 ) vpdiff += step_R>>1;
if ( delta & 1 ) vpdiff += step_R>>2;
if ( sign )
valprev_R -= vpdiff;
else
valprev_R += vpdiff;
/* Step 5 - clamp output value */
if ( valprev_R > 32767 )
valprev_R = 32767;
else if ( valprev_R < -32768 )
valprev_R = -32768;
/* Step 6 - Update step value */
step_R = stepsizeTable[index_R];
/* Step 7 - Output value */
*outp++ = valprev_R;
}
state->valprev_L = valprev_L;
state->index_L = index_L;
state->valprev_R = valprev_R;
state->index_R = index_R;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -