https://blog.csdn.net/wzz4420381/article/details/48812729/
https://blog.csdn.net/littlezls/article/details/83501580
https://blog.csdn.net/su_zhi_guang/article/details/3143145
http://www.mp3-tech.org/programmer/docs/adpcm.pdf
https://blog.csdn.net/forfuture3513/article/details/51764814
https://blog.csdn.net/szfhy/article/category/5773109
https://blog.csdn.net/qq_43103848/article/month/2019/03
ADPCM算法浅析
在上图中:
PCM实际上是模拟信号数字化:
1.2 量化的方法
量化的方法主要有均匀量化和非均匀量化。
均匀量化:
非均匀量化
小结:PCM编码早期主要用于话音通信中的多路复用。一般来说,在电信网中传输媒体线路费用约占总成本的65%,设备费用约占成本的35%,因此提高线路利用率是一个重要课题。
1.3 自适应差分脉冲编码调制(ADPCM)的概念
它的核心想法是:
1.4 ADPCM编码框图
接收端的译码器使用与发送端相同的算法,利用传送来的信号来确定量化器和逆量化器中的量化阶大小,并且用它来预测下一个接收信号的预测值。
2 ADPCM代码实现
2.1 ADPCM编码程序逻辑
由待编码文件的起始部分开始,依次读取长度为len的数据段,送入adpcm_encoder,得到编码后的数据,并将结果存入中间文件,直到待编码文件的所有部分都已编码完成。
adpcm_thirdparty_reset(struct adpcm_state *state);; //此函数只需在编码开始前调用一次
while(待编码文件未读完)
{
读取长度为len的数据段,存入固定的数组内;
执行adpcm_encoder,得到编码后的数据段,以及编码后的数据段长度len_2;
将编码后的数据段顺次存入中间文件内;
}
2.2 ADPCM解码程序逻辑
由中间文件的起始部分开始,依次读取长度为len_2的数据段,送入adpcm_decoder,得到编码后的数据,并将结果存入最终文件,直到中间文件的所有部分都已解码完成。
while(中间文件未读完)
{
读取长度为len_2的数据段,存入固定的数组内;
执行adpcm_decoder,得到解码后的数据段;
将解码后的数据段顺次存入最终文件内;
}
2.3 代码示例
#include
#include
struct adpcm_state
{
int valprev;
int index;
};
void adpcm_thirdparty_reset(struct adpcm_state *state);
void adpcm_encoder(short *indata, signed char *outdata, int len, struct adpcm_state *state);
void adpcm_decoder(signed char *indata, short *outdata, int len, struct adpcm_state *state);
#ifndef __STDC__
#define signed
#endif
/* Intel ADPCM step variation table */
static int indexTable[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8,
};
static int 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_thirdparty_reset(struct adpcm_state *state)
{
state->valprev = 0;
state->index = 0;
}
void adpcm_encoder(short *indata, signed char *outdata, int len, struct adpcm_state *state)
{
short *inp; /* Input buffer pointer */
signed char *outp; /* output buffer pointer */
int val; /* Current input sample value */
int sign; /* Current adpcm sign bit */
int delta; /* Current adpcm output value */
int diff; /* Difference between val and valprev */
int step; /* Stepsize */
int valpred; /* Predicted output value */
int vpdiff; /* Current change to valpred */
int index; /* Current step change index */
int outputbuffer; /* place to keep previous 4-bit value */
int bufferstep; /* toggle between outputbuffer/output */
outp = (signed char *)outdata;
inp = indata;
valpred = state->valprev;
index = state->index;
step = stepsizeTable[index];
bufferstep = 1;
for ( ; len > 0 ; len-- )
{
val = *inp++; //val:当前输入的pcm数据
/* Step 1 - compute difference with previous value */
diff = val - valpred; //当前输入的pcm数据与预测的pcm数据(第一次为上一个pcm数据)的差值diff
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 >> 3);
if ( diff >= step ) {
delta = 4;
diff -= step;
vpdiff += step;
}
step >>= 1;
if ( diff >= step ) {
delta |= 2;
diff -= step;
vpdiff += step;
}
step >>= 1;
if ( diff >= step ) {
delta |= 1;
vpdiff += step;
}
/* Step 3 - Update previous value */
if ( sign )
valpred -= vpdiff;
else
valpred += vpdiff;
/* Step 4 - Clamp previous value to 16 bits */
if ( valpred > 32767 )
valpred = 32767;
else if ( valpred < -32768 )
valpred = -32768;
/* Step 5 - Assemble value, update index and step values */
delta |= sign;
index += indexTable[delta];
if ( index < 0 ) index = 0;
if ( index > 88 ) index = 88;
step = stepsizeTable[index];
/* Step 6 - Output value
if ( bufferstep ) {
outputbuffer = (delta << 4) & 0xf0;
} else {
*outp++ = (delta & 0x0f) | outputbuffer;
}*/
if ( bufferstep ) {
outputbuffer = delta & 0x0f;
} else {
*outp++ = ((delta << 4) & 0xf0) | outputbuffer;
}
bufferstep = !bufferstep;
}
/* Output last step, if needed */
if ( !bufferstep )
*outp++ = outputbuffer;
state->valprev = valpred;
state->index = index;
}
void adpcm_decoder(signed char *indata, short *outdata, int len, struct adpcm_state *state)
{
signed char *inp; /* Input buffer pointer */
short *outp; /* output buffer pointer */
int sign; /* Current adpcm sign bit */
int delta; /* Current adpcm output value */
int step; /* Stepsize */
int valpred; /* Predicted value */
int vpdiff; /* Current change to valpred */
int index; /* Current step change index */
int inputbuffer; /* place to keep next 4-bit value */
int bufferstep; /* toggle between inputbuffer/input */
outp = outdata;
inp = (signed char *)indata;
valpred = state->valprev;
index = state->index;
step = stepsizeTable[index];
bufferstep = 0;
for ( ; len > 0 ; len-- ) {
/* Step 1 - get the delta value */
if ( !bufferstep ) {
inputbuffer = *inp++;
delta = inputbuffer & 0xf;
} else {
delta = (inputbuffer >> 4) & 0xf;
}
bufferstep = !bufferstep;
/* Step 2 - Find new index value (for later) */
index += indexTable[delta];
if ( index < 0 ) index = 0;
if ( index > 88 ) index = 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 >> 3;
if ( delta & 4 ) vpdiff += step;
if ( delta & 2 ) vpdiff += step>>1;
if ( delta & 1 ) vpdiff += step>>2;
if ( sign )
valpred -= vpdiff;
else
valpred += vpdiff;
/* Step 5 - clamp output value */
if ( valpred > 32767 )
valpred = 32767;
else if ( valpred < -32768 )
valpred = -32768;
/* Step 6 - Update step value */
step = stepsizeTable[index];
/* Step 7 - Output value */
*outp++ = valpred;
}
state->valprev = valpred;
state->index = index;
}
3 如何理解ADPCM
注释说明
案例:
/* realize the adpcm code and decode, bandwidth needed is 32kbps,
is half of the ulaw coded, 64kbps
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "adpcm.h"
#define RATE 8000
#define SIZE AFMT_S16_LE
#define CHANNELS 2
#define FRAME_SIZE 1000
int main(int argc, char *argv[])
{
int fd, mid;
int arg;
int status;
int i;
// for encode use
unsigned char inbuf[FRAME_SIZE*4]; // 2 channels , 16bit data , so *4
short inenc[FRAME_SIZE]; // 1 channel, 16bit data, but short type, so *1
unsigned char encbuf[FRAME_SIZE/2];
// for decode use
short decbuf[FRAME_SIZE]; // decode restore inenc
unsigned char outbuf[FRAME_SIZE*4]; // restore inbuf
// adpcm
struct adpcm_state enc_state, dec_state;
//----------------------------------------------------------------------
fd = open("/dev/dsp", O_RDWR);
arg = SIZE;
printf("SIZE:=%d\n", arg);
if (fd < 0)
{
perror("Open /dev/dsp fail");
exit(1);
}
arg = SIZE;
status = ioctl(fd, SNDCTL_DSP_SETFMT, &arg);
if (status == -1)
{
perror("SNDCTL_DSP_SETFMT ioctl failed");
exit(1);
}
arg = CHANNELS;
status = ioctl(fd, SNDCTL_DSP_CHANNELS, &arg);
if (status == -1)
{
perror("SNDCTL_DSP_CHANNELS ioctl failed");
exit(1);
}
ioctl(fd, SOUND_PCM_READ_CHANNELS, &arg);
if (arg != CHANNELS)
{
perror("unable to set channels");
exit(1);
}
arg = RATE;
status = ioctl(fd, SNDCTL_DSP_SPEED, &arg);
if (status == -1)
{
perror("SNDCTL_DSP_SPEED ioctl failed");
exit(1);
}
if (arg != RATE)
{
perror("unable to set rate");
exit(1);
}
mid = open("/dev/mixer", O_RDWR);
arg = SOUND_MASK_MIC;
ioctl(mid, SOUND_MIXER_READ_VOLUME, (char *)&arg);
printf("volume is:%d\n", arg);
arg = 55000;
ioctl(mid, SOUND_MIXER_WRITE_VOLUME, (char *)&arg);
//----------------------------------------------------------------------
// encode
enc_state.valprev = 0;
enc_state.index = 0;
//----------------------------------------------------------------------
// decode
dec_state.valprev = 0;
dec_state.index = 0;
//----------------------------------------------------------------------
while(1)
{
// encode
printf("encode\n");
read(fd, inbuf, sizeof(inbuf));
for(i = 0; i < FRAME_SIZE*4; i+=4) inenc[i/4] = inbuf[i] + inbuf[i+1]*256; //获取单声道数据
adpcm_encoder(inenc, encbuf, FRAME_SIZE, &enc_state);
// decode
printf("decode\n");
adpcm_decoder(encbuf, decbuf, FRAME_SIZE/2, &dec_state);
for(i = 0; i < FRAME_SIZE; i++)
{ //由单声道得到双声道数据
outbuf[i*4] = decbuf[i] & 0xff;
outbuf[i*4+1] = decbuf[i] >> 8;
outbuf[i*4+2] = decbuf[i] & 0xff;
outbuf[i*4+3] = decbuf[i] >> 8;
}
write(fd, outbuf, sizeof(outbuf));
}
//----------------------------------------------------------------------
printf("finished\n");
return 0;
}