本文主要講解:如何將AAC編碼后的數據解碼成PCM。
命令行
用法非常簡單:
ffmpeg -c:a libfdk_aac -i in.aac -f s16le out.pcm
-
-c:a libfdk_aac
- 使用fdk-aac解碼器
- 需要注意的是:這個參數要寫在aac文件那邊,也就是屬於輸入參數
-
-f s16le
- 設置PCM文件最終的采樣格式
編程
需要用到2個庫:
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
}
#define ERROR_BUF(ret) \
char errbuf[1024]; \
av_strerror(ret, errbuf, sizeof (errbuf));
函數聲明
我們最終會將AAC解碼的操作封裝到一個函數中。
// 解碼后的PCM參數
typedef struct {
const char *filename;
int sampleRate;
AVSampleFormat sampleFmt;
int chLayout;
} AudioDecodeSpec;
class FFmpegs {
public:
FFmpegs();
static void aacDecode(const char *inFilename,
AudioDecodeSpec &out);
};
函數實現
變量定義
// 輸入緩沖區的大小
#define IN_DATA_SIZE 20480
// 需要再次讀取輸入文件數據的閾值
#define REFILL_THRESH 4096
// 返回結果
int ret = 0;
// 每次從輸入文件中讀取的長度
int inLen = 0;
// 是否已經讀取到了輸入文件的尾部
int inEnd = 0;
// 用來存放讀取的文件數據
// 加上AV_INPUT_BUFFER_PADDING_SIZE是為了防止某些優化過的reader一次性讀取過多導致越界
char inDataArray[IN_DATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
char *inData = inDataArray;
// 文件
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() << "decoder libfdk_aac not found";
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;
}
創建AVPacket
// 創建AVPacket
pkt = av_packet_alloc();
if (!pkt) {
qDebug() << "av_packet_alloc error";
goto end;
}
創建AVFrame
// 創建AVFrame
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;
}
}
}
// flush解碼器
// pkt->data = NULL;
// pkt->size = 0;
decode(ctx, nullptr, frame, outFile);
具體的解碼操作在decode函數中。
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]);
}
}
設置輸出參數
// 設置輸出參數
out.sampleRate = ctx->sample_rate;
out.sampleFmt = ctx->sample_fmt;
out.chLayout = ctx->channel_layout;
釋放資源
end:
inFile.close();
outFile.close();
av_frame_free(&frame);
av_packet_free(&pkt);
av_parser_close(parserCtx);
avcodec_free_context(&ctx);
函數調用
AudioDecodeSpec out;
out.filename = "F:/out.pcm";
FFmpegs::aacDecode("F:/in.aac", out);
// 44100
qDebug() << out.sampleRate;
// s16
qDebug() << av_get_sample_fmt_name(out.sampleFmt);
// 2
qDebug() << av_get_channel_layout_nb_channels(out.chLayout);