1、ADPCM压缩算法ADPCM压缩算法ADPCM(Adaptive Differential Pulse Code Modulation),是一种针对 16bits( 或8bits或者更高) 声音波形数据的一种有损压缩算法,它将声音流中每次采样的 16bit 数据以 4bit 存储,所以压缩比 1:4. 而且压缩/解压缩算法非常简单,所以是一种低空间消耗,高质量高效率声音获得的好途径。保存声音的数据文件后缀名为 .AUD 的大多用ADPCM 压缩。ADPCM 主要是针对连续的波形数据的,保存的是波形的变化情况,以达到描述整个波形的目的,由于它的编码和解码的过程却很简洁,列在后面,相信大家能够看懂
2、。8bits采样的声音人耳是可以勉强接受的,而 16bit 采样的声音可以算是高音质了。ADPCM 算法却可以将每次采样得到的 16bit 数据压缩到 4bit 。需要注意的是,如果要压缩/解压缩得是立体声信号,采样时,声音信号是放在一起的,需要将两个声道分别处理。ADPCM 压缩过程 首先我们认为声音信号都是从零开始的,那么需要初始化两个变量 int index=0,prev_sample=0;下面的循环将依次处理声音数据流,注意其中的 getnextsample() 应该得到一个 16bit 的采样数据,而 outputdata() 可以将计算出来的数据保存起来,程序中用到的 step_t
3、able,index_adjust 附在后面:int index=0,prev_sample:=0;while (还有数据要处理)cur_sample=getnextsample();/ 得到当前的采样数据delta=cur_sample-prev_sample;/ 计算出和上一个的增量if (delta7) code=7;/ 它描述了声音强度的变化量index += index_adjustcode ;/ 根据声音强度调整下次取steptable 的序号if (index88) index=88;prev_sample=cur_sample;outputode(code|sb);/ 加上符号
4、位保存起来ADPCM 解压缩过程 接压缩实际是压缩的一个逆过程,同样其中的 getnextcode() 应该得到一个编码,,而 outputsample() 可以将解码出来的声音信号保存起来。这段代码同样使用了同一个的 setp_table 和 index_adjust() 附在后面:int index=0,cur_sample=0;while (还有数据要处理) code=getnextcode();/ 得到下一个数据if (code & 8) != 0) sb=1 else sb=0;code&=7;/ 将 code 分离为数据和符号delta = (step_tableindex*cod
5、e)/4+step_tableindex/8;/ 后面加的一项是为了减少误差if (sb=1) delta=-delta;cur_sample+=delta; / 计算出当前的波形数据if (cur_sample32767) output_sample(32767);else if (cur_sample-32768) output_sample(-32768);else output_sample(cur_sample);index+=index_adjustcode;if (index88) index=88;附表 int index_adjust8 = -1,-1,-1,-1,2,4,6
6、,8;int step_table89 =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,
7、7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767TCPMP原代码赏析/* This program is free software ; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of
8、 the License, or* (at your option) any later version.* This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.* You shou
9、ld have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA* $Id: adpcm.h 271 2005-08-09 08:31:35Z picard $* The Core Pocket Media Player* Copyright (c) 2004-2005 Gabor
10、 Kovacs*/#ifndef _ADPCM_H#define _ADPCM_H#define ADPCM_CLASS FOURCC(A,D,P,C)#define ADPCM_MS_ID FOURCC(A,D,M,S)#define ADPCM_IMA_ID FOURCC(A,D,I,M)#define ADPCM_IMA_QT_ID FOURCC(A,D,I,Q)#define ADPCM_G726_ID FOURCC(G,7,2,6)extern void ADPCM_Init();extern void ADPCM_Done();#endif/* This program is fr
11、ee software ; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.* This program is distributed in the hope that it will be useful,* but WITH
12、OUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.* You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundati
13、on, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA* $Id: adpcm.c 565 2006-01-12 14:11:44Z picard $* The Core Pocket Media Player* Copyright (c) 2004-2005 Gabor Kovacs*/#include ./common/common.h#include adpcm.h#include g726/g72x.htypedef struct state int Predictor; int StepIndex; int St
14、ep; int Sample1; int Sample2; int CoEff1; int CoEff2; int IDelta; state;typedef struct adpcmcodec Codec;buffer Data;int Channel; /IMA_QTint16_t* Buffer;state State2;g726_state G7262; adpcm;static const int IndexTable16 = -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8,;static const int StepTa
15、ble89 = 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, 274
16、9, 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;/ AdaptationTable, AdaptCoeff1, and AdaptCoeff2 are from libsndfilestatic const int AdaptationTable = 230, 230, 230, 230, 307, 40
17、9, 512, 614,768, 614, 512, 409, 307, 230, 230, 230;static const int AdaptCoeff1 = 256, 512, 0, 192, 240, 460, 392;static const int AdaptCoeff2 = 0, -256, 0, 64, 0, -208, -232;static _INLINE int IMA_Calc(state* s, int v) int StepIndex; int Predictor; int Diff,Step; Step = StepTables-StepIndex; StepIn
18、dex = s-StepIndex + IndexTablev; if (StepIndex 88) StepIndex = 88; Diff = (2 * (v & 7) + 1) * Step) 3; Predictor = s-Predictor; if (v & 8) Predictor -= Diff; else Predictor += Diff;if (Predictor 32767) Predictor = 32767;else if (Predictor Predictor = Predictor; s-StepIndex = StepIndex; return Predic
19、tor;static _INLINE int MS_Calc(state* s, int v) int Predictor; Predictor = (s-Sample1 * s-CoEff1) + (s-Sample2 * s-CoEff2) 8; Predictor += (v & 0x08) ? v-0x10:v) * s-IDelta;if (Predictor 32767) Predictor = 32767;else if (Predictor Sample2 = s-Sample1; s-Sample1 = Predictor; s-IDelta = (AdaptationTab
20、lev * s-IDelta) 8; if (s-IDelta IDelta = 16; return Predictor;static int Process(adpcm* p, const packet* Packet, const flowstate* State)int i;int Predictor;const uint8_t* In;const uint8_t* InEnd;int16_t* Out = p-Buffer;if (Packet) if (Packet-RefTime = 0) p-Codec.Packet.RefTime = Packet-RefTime; Buff
21、erPack(&p-Data,0); BufferWrite(&p-Data,Packet-Data0,Packet-Length,1024);else p-Codec.Packet.RefTime = TIME_UNKNOWN;if (!BufferRead(&p-Data,&In,p-Codec.In.Format.Format.Audio.BlockAlign) return ERR_NEED_MORE_DATA;InEnd = In + p-Codec.In.Format.Format.Audio.BlockAlign;switch (p-Codec.Node.Class)case A
22、DPCM_G726_ID: g726_state *g1,*g2; g1 = g2 = &p-G7260; if (p-Codec.In.Format.Format.Audio.Channels=2) +g2; switch (p-Codec.In.Format.Format.Audio.Bits) case 2: for (;In 6,g1); Out1 = (int16_t)g726_16_decoder(In0 4,g2); Out2 = (int16_t)g726_16_decoder(In0 2,g1); Out3 = (int16_t)g726_16_decoder(In0,g2)
23、; break; case 3: InEnd -= 2; for (;In 5,g1); Out1 = (int16_t)g726_24_decoder(In0 2,g2); Out2 = (int16_t)g726_24_decoder(In0 7),g1); Out3 = (int16_t)g726_24_decoder(In1 4,g2); Out4 = (int16_t)g726_24_decoder(In1 1,g1); Out5 = (int16_t)g726_24_decoder(In1 6),g2); Out6 = (int16_t)g726_24_decoder(In2 3,
24、g1); Out7 = (int16_t)g726_24_decoder(In2 0,g2); break; case 4: for (;In 4,g1); Out1 = (int16_t)g726_32_decoder(In0,g2); break; case 5: InEnd -= 4; for (;In 3,g1); Out1 = (int16_t)g726_40_decoder(In0 6),g2); Out2 = (int16_t)g726_40_decoder(In1 1,g1); Out3 = (int16_t)g726_40_decoder(In1 4),g2); Out4 = (int16_t)g726_40_decoder(In2 7),g1); Out5 = (int16_t)g726_40_decoder(In3 2,g2); Out6 = (int16_t)g726_40_decoder(In3 5),g1); Out7 = (int16_t)g726_40_decoder(In4 0,g2); break; break;case ADPCM_IMA_QT
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1