1. 如何使用命令行将 aac 解码为 pcm ?
ffmpeg -c:a libfdk_aac -i in.aac -f s16le out.pcm
复制代码
2. AVCodecParserContext
这个结构体主要做用是什么?核心函数时什么?
- 初始化
av_parser_init(codec->id);
其参数是 codec_id
,因此同时只能解析一种数据
AVCodecParserContext
用于解析输入的数据并把它们分红一帧一帧的压缩编码数据
- 核心函数
av_parser_parse2()
解析数据得到一个 Packet,从输入的数据流中分离出一帧一帧
的编码压缩数据
。
3. 使用代码将 aac 解码为 pcm 的过程(涉及 6 个元素,比编码多一个)?

4. 使用代码将 aac 解码为 pcm 的完整代码
#include "ffmpegs.h"
#include <QDebug>
#include <QFile>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
}
#define ERROR_BUF(ret) \ char errbuf[1024]; \ av_strerror(ret, errbuf, sizeof (errbuf));
#define IN_DATA_SIZE 20480
#define REFILL_THRESH 4096
FFmpegs::FFmpegs()
{
}
static int decode(AVCodecContext *ctx, AVPacket *pkt, AVFrame *frame, QFile &outFile) {
int ret = avcodec_send_packet(ctx, pkt);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_send_packet error" << errbuf;
return ret;
}
while (true) {
ret = avcodec_receive_frame(ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
} else if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_receive_frame error" << errbuf;
return ret;
}
outFile.write((char *)frame->data[0], frame->linesize[0]);
}
}
void FFmpegs::accDecode(const char *inFilename, AudioEncodeSpec &out) {
int ret = 0;
char inDataArray[IN_DATA_SIZE + AV_INPUT_BUFFER_MIN_SIZE];
char *inData = inDataArray;
int inLen;
int inEnd = 0;
QFile inFile(inFilename);
QFile outFile(out.filename);
AVCodec *codec = nullptr;
AVCodecContext *ctx = nullptr;
AVCodecParserContext *parserCtx = nullptr;
AVPacket *pkt = nullptr;
AVFrame *frame = nullptr;
codec = avcodec_find_decoder_by_name("libfdk_aac");
if (!codec) {
qDebug() << "avcodec_find_decoder_by_name error";
return;
}
parserCtx = av_parser_init(codec->id);
if (!parserCtx) {
qDebug() << "av_parser_init error";
return;
}
ctx = avcodec_alloc_context3(codec);
if (!ctx) {
qDebug() << "avcodec_alloc_context3 error";
goto end;
}
pkt = av_packet_alloc();
if (!pkt) {
qDebug() << "av_packet_alloc error";
goto end;
}
frame = av_frame_alloc();
if (!frame) {
qDebug() << "av_frame_alloc error";
goto end;
}
ret = avcodec_open2(ctx, codec, nullptr);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "avcodec_open2 error" << errbuf;
goto end;
}
if (!inFile.open(QFile::ReadOnly)) {
qDebug() << "file open error:" << inFilename;
goto end;
}
if (!outFile.open(QFile::WriteOnly)) {
qDebug() << "file open error:" << out.filename;
goto end;
}
inLen = inFile.read(inData, IN_DATA_SIZE);
while (inLen > 0) {
ret = av_parser_parse2(parserCtx, ctx,
&pkt->data, &pkt->size,
(uint8_t *)inData, inLen,
AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0) {
ERROR_BUF(ret);
qDebug() << "av_parser_parse2 error" << errbuf;
goto end;
}
inData += ret;
inLen -= ret;
if (pkt->size > 0 && decode(ctx, pkt, frame, outFile) < 0 ) {
goto end;
}
if (inLen < REFILL_THRESH && !inEnd) {
memmove(inDataArray, inData, inLen);
inData = inDataArray;
int len = inFile.read(inData + inLen, IN_DATA_SIZE - inLen);
if (len > 0) {
inLen += len;
} else {
inEnd = 1;
}
}
}
decode(ctx, nullptr, frame, outFile);
out.sampleRate = ctx->sample_rate;
out.sampleFmt = ctx->sample_fmt;
out.chLayout = ctx->channel_layout;
end:
inFile.close();
outFile.close();
av_packet_free(&pkt);
av_frame_free(&frame);
av_parser_close(parserCtx);
avcodec_free_context(&ctx);
}
复制代码