本文主要讲解:如何将AAC编码后的数据解码成PCM。
命令行
用法非常简单:
| ffmpeg -c:a libfdk_aac -i in.aac -f s16le out.pcm |
-
-c:a libfdk_aac
- 使用fdk-aac解码器
- 需要注意的是:这个参数要写在aac文件那边,也就是属于输入参数
-
-f s16le
编程
需要用到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解码的操作封装到一个函数中。
| |
| 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; |
| |
| |
| |
| 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
| |
| pkt = av_packet_alloc(); |
| if (!pkt) { |
| qDebug() << "av_packet_alloc error"; |
| goto end; |
| } |
创建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; |
| } |
| } |
| } |
| |
| |
| |
| |
| 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); |
| |
| qDebug() << out.sampleRate; |
| |
| qDebug() << av_get_sample_fmt_name(out.sampleFmt); |
| |
| qDebug() << av_get_channel_layout_nb_channels(out.chLayout); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?