使用libavcodec将mp3音频文件解码为pcm音频采样数据【[mp3float @ 0x561c1ec49940] Header missing】
一.打开和关闭输入文件和输出文件
想要解决上面提到的问题,我们需要对mp3文件的格式有个大致了解,为了方便讲解,我这里画了个示意图:
| ID3V2 | 包含了作者,作曲,专辑等信息,长度不固定,扩展了 ID3V1 的信息量。 |
| Frame | 一系列的帧,个数由文件大小和帧长决定 |
| ID3V1 | 包含了作者,作曲,专辑等信息,长度为 128BYTE |
由于av_parser_parse2()这个方法的输入必须是只包含音频编码数据的“裸流”,所以,我们在读取mp3文件的时候,必须跳过ID3V2标签部分,从Frame开始。所以,我们就必须知道ID3V2标签的总长度。下面,我画了个ID3V2标签头的示意图,方便讲解。
| File ID(3) | Version(2) | Flags(1) | Size(4) |
ID3V2标签头固定为10byte,其中,Size部分的值是指除ID3V2标签头之外数据的总长度。需要注意的是,在实际计算长度的时候,这4个字节的最高位需要舍弃,也就是说,只用到了28bit,即:0xxxxxxx0xxxxxxx0xxxxxxx0xxxxxxx
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
static FILE* input_file= nullptr;
static FILE* output_file= nullptr;
static const AVCodec* codec= nullptr;
static AVCodecContext* codec_ctx= nullptr;
static AVPacket* pkt= nullptr;
static AVFrame* frame= nullptr;
static AVCodecParserContext* parser= nullptr;
static enum AVCodecID audio_codec_id; void close_input_output_files(){
if(input_file!= nullptr){
fclose(input_file);
input_file= nullptr;
}
if(output_file!= nullptr){
fclose(output_file);
output_file= nullptr;
}
} int32_t open_input_output_files(const char* input_name,const char* output_name){
if(strlen(input_name)==0||strlen(output_name)==0){
cout<<"Error:empty input or output file name."<<endl;
return -1;
}
close_input_output_files();
input_file=fopen(input_name,"rb");//rb:读取一个二进制文件,该文件必须存在
if(input_file==nullptr){
cerr<<"Error:failed to open input file."<<endl;
return -1;
}
output_file=fopen(output_name,"wb");//wb:打开或新建一个二进制文件,只允许写
if(output_file== nullptr){
cout<<"Error:failed to open output file."<<endl;
return -1;
}
uint8_t ID3V2_Header[10];
fread(ID3V2_Header,10,1,input_file);
long ID3V2_Size=((ID3V2_Header[6]&0x7f)<<21)+((ID3V2_Header[7]&0x7f)<<14)+((ID3V2_Header[8]&0x7f)<<7)+(ID3V2_Header[9]&0x7f)+10;
fseek(input_file,ID3V2_Size,SEEK_SET);
return 0;
}
二.音频解码器的初始化以及销毁
int32_t init_audio_decoder(const char* audio_codec){
if(strcasecmp(audio_codec,"MP3")==0){
audio_codec_id=AV_CODEC_ID_MP3;
cout<<"Select codec id:MP3"<<endl;
}
else if(strcasecmp(audio_codec,"AAC")==0){
audio_codec_id=AV_CODEC_ID_AAC;
cout<<"Select codec id:AAC"<<endl;
}
else{
cerr<<"Error:invalid audio format."<<endl;
return -1;
}
codec=avcodec_find_decoder(audio_codec_id);
if(!codec){
cerr<<"Error:could not find codec."<<endl;
return -1;
}
parser= av_parser_init(codec->id);
if(!parser){
cerr<<"Error:could not init parser."<<endl;
return -1;
}
codec_ctx= avcodec_alloc_context3(codec);
if(!codec_ctx){
cerr<<"Error:could not alloc codec_ctx."<<endl;
return -1;
}
int32_t result=avcodec_open2(codec_ctx,codec, nullptr);
if(result<0){
cerr<<"Error:could not open codec."<<endl;
return -1;
}
frame=av_frame_alloc();
if(!frame){
cerr<<"Error:could not alloc frame."<<endl;
return -1;
}
pkt=av_packet_alloc();
if(!pkt){
cerr<<"Error:could not alloc packet."<<endl;
return -1;
}
return 0;
}
void destroy_audio_decoder(){
av_parser_close(parser);
avcodec_free_context(&codec_ctx);
av_frame_free(&frame);
av_packet_free(&pkt);
}
三.解码循环体
解码循环体至少需要实现以下三个功能:
1.从输入源中循环获取码流包
2.将当前帧传入解码器,获取输出的音频采样数据
3.输出解码获取的音频采样数据到输出文件
从输入源中读取音频数据到缓存:
int32_t read_data_to_buf(uint8_t* buf,int32_t size,int32_t& out_size){
int32_t read_size=fread(buf,1,size,input_file);
if(read_size==0){
cerr<<"Error:read_data_to_buf failed."<<endl;
return -1;
}
out_size=read_size;
return 0;
}
解码循环体:
int32_t end_of_input_file(){
return feof(input_file);
}
static int32_t decode_packet(bool flushing){
int32_t result=0;
result= avcodec_send_packet(codec_ctx,flushing? nullptr:pkt);
if(result<0){
cerr<<"Error:avcodec_send_packet failed,result:"<<result<<endl;
return -1;
}
while(result>=0){
result= avcodec_receive_frame(codec_ctx,frame);
if(result==AVERROR(EAGAIN)||result==AVERROR_EOF){
return 1;
}
else if(result<0){
cerr<<"Error:avcodec_receive_frame failed."<<endl;
return -1;
}
if(flushing){
cout<<"flushing:";
}
write_samples_to_pcm(frame,codec_ctx);
cout<<"frame->nb_samples:"<<frame->nb_samples<<",frame->channels:"<<frame->channels<<endl;
}
return 0;
}
int32_t audio_decoding(){
uint8_t inbuf[AUDIO_INBUF_SIZE+AV_INPUT_BUFFER_PADDING_SIZE]={0};
int32_t result=0;
uint8_t* data= nullptr;
int32_t data_size=0;
while(!end_of_input_file()){
result= read_data_to_buf(inbuf,AUDIO_INBUF_SIZE,data_size);
if(result<0){
cerr<<"Error:read_data_to_buf failed."<<endl;
return -1;
}
data=inbuf;
while(data_size>0){
result=av_parser_parse2(parser,codec_ctx,&pkt->data,&pkt->size,data,data_size,AV_NOPTS_VALUE,AV_NOPTS_VALUE,0);
if(result<0){
cerr<<"Error:av_parser_parse2 failed."<<endl;
return -1;
}
data+=result;
data_size-=result;
if(pkt->size){
cout<<"Parsed packet size:"<<pkt->size<<endl;
decode_packet(false);
}
if (data_size < AUDIO_REFILL_THRESH) {
memmove(inbuf, data, data_size);
data = inbuf;
int len = fread(data + data_size, 1,AUDIO_INBUF_SIZE - data_size, input_file);
if (len > 0)
data_size += len;
}
}
}
decode_packet(true);
return 0;
}
输出解码的音频采样数据:
int32_t write_samples_to_pcm(AVFrame* frame,AVCodecContext* codec_ctx){
int data_size= av_get_bytes_per_sample(codec_ctx->sample_fmt);
if(data_size<0){
cerr<<"Error:failed to calculate data size."<<endl;
return -1;
}
for(int i=0;i<frame->nb_samples;i++){
for(int ch=0;ch<codec_ctx->channels;ch++){
fwrite(frame->data[ch]+i*data_size,1,data_size,output_file);
}
}
return 0;
}
最终,main函数的实现如下:
int main(){
const char* input_file_name="../input.mp3";
const char* output_file_name="../output.pcm";
const char* codec_name="MP3";
int32_t result= open_input_output_files(input_file_name,output_file_name);
if(result<0){
return result;
}
result=init_audio_decoder(codec_name);
if(result<0){
return result;
}
result=audio_decoding();
if(result<0){
return result;
}
destroy_audio_decoder();
close_input_output_files();
return 0;
}
解码完成后,可以使用ffplay播放output.pcm文件:
ffplay -ar 44100 -ac 2 -f f32le -i output.pcm
使用libavcodec将mp3音频文件解码为pcm音频采样数据【[mp3float @ 0x561c1ec49940] Header missing】的更多相关文章
- 视音频数据处理入门:PCM音频采样数据处理
===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...
- 解析WAV音频文件----》生成WAV音频文件头
前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i WAV音频文件介绍: WAV文件是在PC机平台上很常见的.最经典的多媒体音频文件,最早于1991年8月出现在Windows3.1操作系统 ...
- C# NAudio录音和播放音频文件及实时绘制音频波形图(从音频流数据获取,而非设备获取)
下午写了一篇关于NAudio的录音.播放和波形图的博客,不太满意,感觉写的太乱,又总结了下 NAudio是个相对成熟.开源的C#音频开发工具,它包含录音.播放录音.格式转换.混音调整等功能.本次介绍主 ...
- android MediaCodec 音频编解码的实现——转码
原文地址:http://blog.csdn.net/tinsanmr/article/details/51049179 从今天开始 每周不定期更新博客,把这一周在工作与学习中遇到的问题做个总结.俗话说 ...
- FFMPEG视音频编解码零基础学习方法-b
感谢大神分享,虽然现在还看不懂,留着大家一起看啦 PS:有不少人不清楚“FFmpeg”应该怎么读.它读作“ef ef em peg” 0. 背景知识 本章主要介绍一下FFMPEG都用在了哪里(在这里仅 ...
- [总结]FFMPEG视音频编解码零基础学习方法
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...
- [转载] FFMPEG视音频编解码零基础学习方法
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...
- 视音频编解码基本术语及解释&MediaInfo
MEDIA INFO 下载: https://mediaarea.net/en/MediaInfo/Download/Windows 摘要: 整理了一些基本视音频术语,用于入门和查询 ...
- [总结]FFMPEG视音频编解码零基础学习方法【转】
本文转载自:http://blog.csdn.net/leixiaohua1020/article/details/15811977 在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFM ...
- (原创)speex与wav格式音频文件的互相转换(二)
之前写过了如何将speex与wav格式的音频互相转换,如果没有看过的请看一下连接 http://www.cnblogs.com/dongweiq/p/4515186.html 虽然自己实现了相关的压缩 ...
随机推荐
- [Java/LeetCode]算法练习:二进制间距(868/simple)
1 题目描述 题目来源: https://leetcode-cn.com/problems/binary-gap/ 给定一个正整数 n,找到并返回 n 的二进制表示中两个 相邻 1 之间的 最长距离 ...
- Luogu P4114 Qtree1
树剖一好题.我心水了ww 题目描述 给定一棵n个节点的树,有两个操作: CHANGE i ti 把第i条边的边权变成ti QUERY a b 输出从a到b的路径中最大的边权,当a=b的时候,输出0 输 ...
- Disruptor-简单使用
前言 Disruptor是一个高性能的无锁并发框架,其主要应用场景是在高并发.低延迟的系统中,如金融领域的交易系统,游戏服务器等.其优点就是非常快,号称能支撑每秒600万订单.需要注意的是,Disru ...
- 分布式缓存--Redis
目录 一.单点Redis的问题 二.Redis持久化 2.1 RDB持久化 2.1.1 单机安装Redis 2.1.2 RDB内部机制 2.1.3 RDB异步持久化 2.1.14 RDB的缺点 2.2 ...
- 帝国cms 随机调取新闻
<?php $hits_r = $empire->query("select * from {$dbtbpre}ecms_music as t1 join (select rou ...
- [OpenCV-Python] 20 图像金字塔
文章目录 OpenCV-Python:IV OpenCV中的图像处理 20 图像金字塔 20.1 原理 20.2 使用金字塔进行图像融合 OpenCV-Python:IV OpenCV中的图像处理 2 ...
- CogSci 2017-Learning to reinforcement learn
Key 元学习系统(监督+从属)扩展于RL设置 LSTM用强化学习算法进行训练,可以使agent获得一定的学习适应能力 解决的主要问题 DRL受限于特定的领域 DRL训练需要大量的数据 作者参考了Ho ...
- linux DNS域名解析
目录 一.DNS概念 二.域名格式类型 三.查询类型 四.解析类型 五.配置DNS 六.dns解析实验 1.配置正向解析 2.反向解析 3.主从解析 一.DNS概念 概念:域名和IP地址的相互映射的分 ...
- 使用umi+dva做一个demo
最初只是使用react 进行开发项目,发现项目过大状态管理起来就相当困难,虽然有redux, mobx,但是使用起来还是相当繁琐,而目前umi有现成的轮子使用简单,当然愿意尝试了,趁现在假期有时间简单 ...
- 代码随想录算法训练营Day31 贪心算法| 122.买卖股票的最佳时机II 55. 跳跃游戏 45.跳跃游戏II
代码随想录算法训练营 122.买卖股票的最佳时机II 题目链接:122.买卖股票的最佳时机II 给定一个数组,它的第 i个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润. ...