流程如下
代码逻辑
const char *outfilename;
const char *filename;
const AVCodec *codec;
AVCodecContext *codec_ctx = NULL;
AVCodecParserContext *parser_ctx = NULL;
int len = 0;
int ret = 0;
FILE *infile = NULL;
FILE *outfile = NULL;
uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
uint8_t *data = NULL;
size_t data_size = 0;
AVPacket *pkt = NULL;
AVFrame *decoded_frame = NULL;
初始化变量 inbuf 是缓冲区 AV_INPUT_BUFFER_PADDING_SIZE是buf的最小容量如果不够继续读取
解码步骤就是
通过文件查询需要的编码器id
enum AVCodecID audio_codec_id = AV_CODEC_ID_AAC;
if (strstr(filename,“aac”)!=NULL){
audio_codec_id = AV_CODEC_ID_AAC;
}else if(strstr(filename, “mp3”) != NULL)
{
audio_codec_id = AV_CODEC_ID_MP3;
}
通过其获得编码器 codec = avcodec_find_decoder(audio_codec_id);
通过编码器id 获得对应的流解析器 parser_ctx = av_parser_init(codec->id);
通过编码器创建编码器上下文 codec_ctx = avcodec_alloc_context3(codec);
打开编码器
avcodec_open2(codec_ctx, codec, NULL);
给packer 中读取ret的数据
ret = av_parser_parse2(parser_ctx,codec_ctx,&pkt->data,&pkt->size,data,data_size,AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
如果
开始解码
decode(codec_ctx, pkt, decoded_frame, outfile)
avcodec_send_packet:将AVPacket压缩数据给解码器。 avcodec_receive_frame:获取到解码后的AVFrame数据 循环读取 在⼀个循环体内去接收codec的输出,即周期性地调⽤avcodec_receive_*()来接收codec 输出的数据。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include <libavcodec/avcodec.h>
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
static char err_buf[128] = {0};
static void print_sample_format(const AVFrame *frame)
{
printf("ar-samplerate: %uHz\n", frame->sample_rate);
printf("ac-channel: %u\n", frame->channels);
printf("f-format: %u\n", frame->format);// 格式需要注意,实际存储到本地文件时已经改成交错模式
}
static char* av_get_err(int errnum)
{
av_strerror(errnum, err_buf, 128);
return err_buf;
}
static void decode(AVCodecContext *context,AVPacket *packet,AVFrame *frame,FILE *file){
int i,ch;
int ret,data_size;
ret = avcodec_send_packet(context,packet);
if(ret == AVERROR(EAGAIN))
{
fprintf(stderr, "Receive_frame and send_packet both returned EAGAIN, which is an API violation.\n");
}
else if (ret < 0)
{
fprintf(stderr, "Error submitting the packet to the decoder, err:%s, pkt_size:%d\n",
av_get_err(ret), packet->size);
// exit(1);
return;
}
while (ret>0){
ret = avcodec_receive_frame(context,frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0)
{
fprintf(stderr, "Error during decoding\n");
exit(1);
}
data_size = av_get_bytes_per_sample(context->sample_fmt);
if (data_size < 0)
{
/* This should not occur, checking just for paranoia */
fprintf(stderr, "Failed to calculate data size\n");
exit(1);
}
static int s_print_format = 0;
if(s_print_format == 0){
s_print_format = 1;
print_sample_format(frame);
}
for (int i = 0; i < frame->nb_samples; ++i) {
for (int j = 0; j < context->channels; ++j) {
fwrite(frame->data[ch]+data_size*i,1,data_size,file);
}
}
}
}
int main(int argc,char **argv){
const char *outfilename;
const char *filename;
const AVCodec *codec;
AVCodecContext *codec_ctx = NULL;
AVCodecParserContext *parser_ctx = NULL;
int len = 0;
int ret = 0;
FILE *infile = NULL;
FILE *outfile = NULL;
uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
uint8_t *data = NULL;
size_t data_size = 0;
AVPacket *pkt = NULL;
AVFrame *decoded_frame = NULL;
if (argc <= 2)
{
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
exit(0);
}
filename = argv[1];
outfilename = argv[2];
pkt = av_packet_alloc();
enum AVCodecID audio_codec_id = AV_CODEC_ID_AAC;
if (strstr(filename,"aac")!=NULL){
audio_codec_id = AV_CODEC_ID_AAC;
}else if(strstr(filename, "mp3") != NULL)
{
audio_codec_id = AV_CODEC_ID_MP3;
}
else
{
printf("default codec id:%d\n", audio_codec_id);
}
codec = avcodec_find_decoder(audio_codec_id);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
// 获取裸流的解析器 AVCodecParserContext(数据) + AVCodecParser(方法)
parser_ctx = av_parser_init(codec->id);
if (!parser_ctx) {
fprintf(stderr, "Parser not found\n");
exit(1);
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
infile = fopen(filename, "rb");
if (!infile) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
// 打开输出文件
outfile = fopen(outfilename, "wb");
if (!outfile) {
av_free(codec_ctx);
exit(1);
}
data = inbuf;
data_size = fread(inbuf,1,AUDIO_INBUF_SIZE,infile);
while (data_size > 0){
if (!decoded_frame){
if (!(decoded_frame = av_frame_alloc())){
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
ret = av_parser_parse2(parser_ctx,codec_ctx,&pkt->data,&pkt->size,data,data_size,AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0)
{
fprintf(stderr, "Error while parsing\n");
exit(1);
}
data +=ret;
data_size+=ret;
if (pkt->size)
decode(codec_ctx, pkt, decoded_frame, outfile);
if (data_size < AUDIO_INBUF_SIZE){
memmove(inbuf, data, data_size); // 把之前剩的数据拷贝到buffer的起始位置
data = inbuf;
len = fread(data + data_size, 1, AUDIO_INBUF_SIZE - data_size, infile);
if (len > 0)
data_size += len;
}
}
pkt->data =NULL;
pkt->size = 0;
decode(codec_ctx, pkt, decoded_frame, outfile);
fclose(outfile);
fclose(infile);
avcodec_free_context(&codec_ctx);
av_parser_close(parser_ctx);
av_frame_free(&decoded_frame);
av_packet_free(&pkt);
printf("main finish, please enter Enter and exit\n");
return 0;
}
}