流程如下:
代码逻辑
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数据 循环读取 在⼀个循环体内去接收