音频解码快速应用开发(针对MP3部分)

来源:互联网 发布:硕士 通信算法工程师 编辑:程序博客网 时间:2024/06/10 00:22
首先,注册库中所有支持的文件格式与解码器,这样当打开一个文件时,就可以自动选择对应的解码器。
接下来,打开音频文件,av_open_input_file的后三个参数分别描述了文件格式、缓冲区大小与格式参数。设置为NULL或0
可以让libavformat去自动探测文件格式并使用默认的缓冲区大小。
然后,用有效的信息将AVFormatContext的流域填满,得到需要解码音频流的上下文指针,接下来在库中寻找音频流的解码器。
由于本文仅针对mp3做解码,故其他文件格式不予考虑。
好了,到现在为止,准备工作已经完成。
打开解码器,这个步骤会对解码器进行初始化,包括设定合适的参数与分配所需的内存块。
读取需要解码的文件,并对其进行解码。
如下是示例程序:
int main() {
        AVCodecContext * pavcc;int i, audio_stream_index = -1;AVFormatContext * pavfc;AVCodec * pavc;const char * filename = "test.mp3";int16_t data[DATASIZE], datalen, buflen, bsize = 0;uint8_t buf[BUFSIZE];FILE* pf, *plog = fopen("decoding.log", "a+");if(plog == NULL) {return -1;} av_register_all();if(av_open_input_file(&pavfc, filename, NULL, 0, NULL) != 0) {fprintf(plog, "can't open file : %s/n", filename);exit(1);}if(av_find_stream_info(pavfc) < 0) {fprintf(plog, "can't find stream info/n");exit(1);}for(i = 0; i < pavfc->nb_streams, i++) {if(pavfc->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {audio_stream_index = i;break;}}if(audio_stream_index == -1) {fprintf(plog, "can't find audio stream/n");exit(1);//do something}pavcc = pavfc->streams[audio_stream_index]->codec;if((pavc = avcodec_find_decoder(pavcc->codec_id)) == NULL) {//do somethingfprintf(plog, "can't find decoder/n");//return exit(1);}if(strcmp(pavc->name, "mp3")) {//do somethingfprintf(plog, "the file is not mp3 form/n");exit(1);} if(avcodec_open(pavcc, pavc) == -1) {//do somethingfprintf(plog, "can't open the codec /n");}if((pf = fopen(filename, "rb")) == NULL) {//do something}while(!feof(pf)) {uint8_t * buf_ptr;int data_size;buflen = fread(buf + bsize, sizeof(uint8_t), BUFSIZE - bsize, pf);while(buflen > 0) {buf_ptr = buf;datalen = avcodec_decode_audio2(pavcc, data, &data_size, buf_ptr, buflen);if(datalen < 0) {break;}buf_ptr += datalen;buflen -= datalen;}}avcodec_close(pavcc);fclose(pf);fclose(plog);return 0;}
原创粉丝点击