跟视频一样,对于音频的传输,往往也需要经过压缩,下面是关于海思提供的PCM音频裸流文件压缩和解码库的运用,对比一个文件与其压缩、解码操作后的数据,可以明显的发现PCM->ADPCM的音频压缩过程是有损的。关于文件的对比,本人使用的是BCompare 这个软件,绝对是程序员必备的神器啊!如果没有的童鞋建议大家下来玩玩。下载地址:点击打开链接
由于是用海思提供的压缩和解码库,所以其实并不需要做什么复杂的事,只是简单的写几句调用代码就完事了。配置反倒是比较麻烦的事,所以在下面贴出代码后直接给出本人配置好的工程链接。
压缩,从PCM到ADPCM
#include "stdafx.h" #include "hi_voice_api.h" HI_S32 VoiceEngineState[0x100]; #define VOICE_FRAME_SIZE 160 int _tmain(int argc, _TCHAR* argv[])//压缩,从pcm到adpcm,这种压缩是有损的 { HI_S32 frame; HI_S16 coder, len, frame_size; HI_S16 in_enc_pcmbuf[HI_VOICE_MAX_FRAME_SIZE]; //encoder input HI_S16 ou_enc_unpacked[HI_VOICE_MAX_FRAME_SIZE]; //encoder output coder = 35; FILE *fp_in = NULL, *fp_out = NULL; fopen_s(&fp_in, "paomo.pcm", "rb"); fopen_s(&fp_out,"enpaomo.adpcm", "wb"); frame_size = 2 * VOICE_FRAME_SIZE; if (coder == ADPCM_IMA) frame_size += 1; frame = 0; HI_VOICE_EncReset((HI_VOID*)VoiceEngineState, coder);//压缩编码器复位 while (fread(in_enc_pcmbuf, sizeof(HI_S16), frame_size, fp_in) == (HI_U32)frame_size) { HI_VOICE_EncodeFrame((HI_VOID*)VoiceEngineState, in_enc_pcmbuf, ou_enc_unpacked, frame_size);//压缩编码 fwrite(ou_enc_unpacked, sizeof(HI_S16), 2 + ou_enc_unpacked[1], fp_out);//压缩后数据写入文件 frame++; printf("encoding %d frame\r",frame); } fclose(fp_in); fclose(fp_out); return 0; }
解码,从ADPCM到PCM
#include "stdafx.h" #include "hi_voice_api.h" HI_S32 VoiceEngineState[0x100]; int _tmain(int argc, _TCHAR* argv[])//解码,从adpcm到pcm { HI_S16 ou_dec_pcmbuf[HI_VOICE_MAX_FRAME_SIZE]; //decoder output HI_S16 in_enc_pcmbuf[HI_VOICE_MAX_FRAME_SIZE]; //encoder input HI_S16 ou_enc_unpacked[HI_VOICE_MAX_FRAME_SIZE]; //encoder output HI_S16 coder, len; HI_S32 frame; FILE *fp_in = NULL, *fp_out = NULL; fopen_s(&fp_in, "enpaomo.adpcm", "rb"); fopen_s(&fp_out,"depaomo.pcm", "wb"); coder = 35; frame = 0; HI_VOICE_DecReset((HI_VOID*)VoiceEngineState, coder);//解码器复位 while (fread(&ou_enc_unpacked[0], sizeof(HI_S16), 2, fp_in) == 2) { len = (ou_enc_unpacked[1] & 0x00ff); if (fread(&ou_enc_unpacked[2], sizeof(HI_S16), len, fp_in) != (HI_U32)(len)) { printf("\nfile end!\n"); break; } HI_VOICE_DecodeFrame((HI_VOID*)VoiceEngineState, ou_enc_unpacked, ou_dec_pcmbuf, &len);//解码 fwrite(ou_dec_pcmbuf, sizeof(HI_S16), len, fp_out);//解码后写入文件 frame++; printf("decoding %d frame\r",frame); } fclose(fp_in); fclose(fp_out); return 0; }
工程下载地址:点击打开链接