AAC解码

AAC解码

命令行

ffmpeg -c:a libfdk_aac -i in.aac -f s16le out.pcm

代码

头文件

extern "C" {
    #include <libavcodec/avcodec.h>
    #include <libavutil/avutil.h>
    #include <libavformat/avformat.h>
}

#define ERROR_BUF(ret) \
    char errbuf[1024]; \
    av_strerror(ret, errbuf, sizeof (errbuf));

typedef struct{
    char * file_name;
    int sample_rate;
    int sample_ch_layout;
    AVSampleFormat  sample_fmt;
} AudioSpec;


class FFmpegs
{
public:
    FFmpegs();
    static void decode_audio (const char * in_file_name,AudioSpec &audioSpec);
};

具体实现

// 输入缓冲区的大小
#define IN_DATA_SIZE 20480
// 需要再次读取输入文件数据的阈值
#define REFILL_THRESH 4096

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 decode_audio (const char * in_file_name,AudioSpec &audioSpec){
    // 获取解码器
    AVCodec * decoder = avcodec_find_decoder_by_name("libfdk_aac");
    if(!decoder){
        qDebug() << "decoder libfdk_aac not found";
        return;
    }
    // 创建解析上下文
    AVCodecParserContext * parserCtx  = av_parser_init(decoder->id);
    if (!parserCtx) {
        qDebug() << "av_parser_init error";
        return;
    }
    // 创建上下文
    AVCodecContext * ctx = avcodec_alloc_context3(decoder);
    if(!ctx){
        qDebug() << "avcodec_alloc_context3 error";
        av_parser_close(parserCtx);
        return;
    }
    // 创建 AVPacket
    AVPacket * pkt = av_packet_alloc();
    if(!pkt){
        qDebug() << "av_packet_alloc error";
        avcodec_free_context(&ctx);
        av_parser_close(parserCtx);
        return;
    }
    // 创建 AVFrame
    AVFrame * frame = av_frame_alloc();
    if(!frame){
        qDebug() << "av_frame_alloc error";
        av_packet_free(&pkt);
        avcodec_free_context(&ctx);
        av_parser_close(parserCtx);
        return;
    }
    // 打开解码器
    int ret = avcodec_open2(ctx,decoder,nullptr);
    if(ret < 0){
        ERROR_BUF(ret);
        qDebug() << "avcodec_open2 error" << errbuf;
        av_frame_free(&frame);
        av_packet_free(&pkt);
        avcodec_free_context(&ctx);
        av_parser_close(parserCtx);
        return;
    }
    // 打开文件
    QFile inFile(in_file_name);
    if(!inFile.open(QFile::ReadOnly)){
        qDebug() << "file open error:" << in_file_name;
        av_frame_free(&frame);
        av_packet_free(&pkt);
        avcodec_free_context(&ctx);
        av_parser_close(parserCtx);
        return;
    }

    QFile outFile(audioSpec.file_name);
    if(!outFile.open(QFile::WriteOnly)){
        qDebug() << "file open error:" << audioSpec.file_name;
        inFile.close();
        av_frame_free(&frame);
        av_packet_free(&pkt);
        avcodec_free_context(&ctx);
        av_parser_close(parserCtx);
        return;
    }
    // 读取数据到临时空间
    char inDataArray[IN_DATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
    char *inData = inDataArray;
    int inLen = inFile.read((char *) inData,IN_DATA_SIZE);
    // 是否是文件末尾
    int inEnd = 0;
    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;
             // 回收资源
            outFile.close();
            inFile.close();
            av_frame_free(&frame);
            av_packet_free(&pkt);
            avcodec_free_context(&ctx);
            av_parser_close(parserCtx);
            return;
        }

        // 跳过已解析的数据
        inData += ret;
        // 减去已解析的数据大小
        inLen -= ret;

        // 解码
        if(pkt->size > 0 && decode(ctx,pkt,frame,outFile) <0){
            // 回收资源
           outFile.close();
           inFile.close();
           av_frame_free(&frame);
           av_packet_free(&pkt);
           avcodec_free_context(&ctx);
           av_parser_close(parserCtx);
           return;
        }

        // 再次从文件中读取数据
        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);
    // 设置输出参数
    audioSpec.sample_rate = ctx->sample_rate;
    audioSpec.sample_fmt = ctx->sample_fmt;
    audioSpec.sample_ch_layout = ctx->channel_layout;

    // 回收资源
    outFile.close();
    inFile.close();
    av_frame_free(&frame);
    av_packet_free(&pkt);
    avcodec_free_context(&ctx);
    av_parser_close(parserCtx);
}

方法的调用

// 调用
AudioSpec audioSpec;
audioSpec.file_name = "out.pcm";
decode_audio("in.aac",audioSpec);
qDebug() << audioSpec.sample_rate;
qDebug() << av_get_channel_layout_nb_channels(audioSpec.sample_ch_layout);
qDebug() << av_get_sample_fmt_name(audioSpec.sample_fmt);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值