首页 > 编程语言 >ffmpeg实现的C++纯音频软解码器

ffmpeg实现的C++纯音频软解码器

时间:2023-05-18 12:12:30浏览次数:40  
标签:AUDIO ffmpeg int C++ mAvpkt AACDecoder 解码器 AudioSoftDecoder include

只实现解码,不包含前置的demux,以及后置的resample

要求输入demux后的音频数据

实现的C++类如下:
AudioSoftDecoder.h:

#ifndef _AUDIOSOFTDECODER_H_
#define _AUDIOSOFTDECODER_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

extern "C" {
#include "stdint.h"
#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include <libavcodec/avcodec.h>
}
//解码最小包阈值
static const int AUDIO_REFILL_THRESH = 4096;

class AudioSoftDecoder {
public:
    AudioSoftDecoder();
    virtual ~AudioSoftDecoder();
    virtual int init(int channels, int sampleRate, int64_t bitRate, int bitsPerCodedSample);
    virtual int decode(uint8_t *inBuf, const int inBufLen, uint8_t *outBuf, int *outBufLen, bool isStreamEnd);
    virtual void uninit();

private:
    AVCodecContext *mCodexContext;
    AVCodec *mCodec;
    AVPacket mAvpkt;
    AVFrame *mDecodedFrame;
    std::vector<uint8_t> mRemainData;

protected:
    int mAudioType;
};

#endif /* AUDIOSOFTDECODER_H_ */

AudioSoftDecoder.cpp

#include "AudioSoftDecoder.h"
#include <iostream>

// const int AudioSoftDecoder::AUDIO_OUTBUF_SIZE = 1024*1024*2;
// const int AudioSoftDecoder::AUDIO_INBUF_SIZE = 20480;
// const int AudioSoftDecoder::AUDIO_REFILL_THRESH = 4096;

AudioSoftDecoder::AudioSoftDecoder() : mCodexContext(nullptr),
mCodec(nullptr),
mDecodedFrame(nullptr),
mAudioType(AV_CODEC_ID_AAC){

}

AudioSoftDecoder::~AudioSoftDecoder() {

}

int AudioSoftDecoder::init(int channels, int sampleRate, int64_t bitRate, int bitsPerCodedSample){
   /* register all the codecs */
    avcodec_register_all();

    av_init_packet(&mAvpkt);    

    /* find the MPEG audio decoder */
    mCodec = avcodec_find_decoder((AVCodecID)mAudioType);
    if (!mCodec) {
        std::cout<<"Codec not found";
        return -1;
    }
    
    mCodexContext = avcodec_alloc_context3(mCodec);
    if (!mCodexContext) {
        std::cout<<"Could not allocate audio codec context";
        return -2;
    }

    mCodexContext->channels = channels;
    mCodexContext->sample_rate = sampleRate;
    mCodexContext->bit_rate = bitRate;
    mCodexContext->bits_per_coded_sample = bitsPerCodedSample;
    /* open it */
    if (avcodec_open2(mCodexContext, mCodec, NULL) < 0) {
        std::cout<<"Could not open codec";
        return -3;
    }

    if(!(mDecodedFrame = av_frame_alloc())) {
        std::cout<<"Could not allocate audio frame";
    }

    return 0;
}

void AudioSoftDecoder::uninit() {
    avcodec_free_context(&mCodexContext);
    av_frame_free(&mDecodedFrame);
    mRemainData.clear();
}

int AudioSoftDecoder::decode(uint8_t *inBuf, const int inBufLen, uint8_t *outBuf, int *outBufLen, bool isStreamEnd){
    if(!inBuf || !outBuf) {
        std::cout<<"parameter error";
        return -1;
    }

   int maxOutLen = *outBufLen;
   *outBufLen = 0;
   std::cout<<"inbufLen:"<<inBufLen<<std::endl;
    uint8_t *allBuffer = nullptr;
    //合并上次剩余的未解码数据
   if(mRemainData.empty()) {
       mAvpkt.data = inBuf;
       mAvpkt.size = inBufLen;
   } else {
       mRemainData.insert(mRemainData.end(), inBuf, inBuf + inBufLen);
       mAvpkt.data = &mRemainData[0];
       mAvpkt.size = mRemainData.size();
   }


   while (mAvpkt.size > 0) {
        int i, ch;
        int gotFrame = 0;
        int len = avcodec_decode_audio4(mCodexContext, mDecodedFrame, &gotFrame, &mAvpkt);
        if (len < 0) {
            std::cout<<"Error while decoding ret:" << len;
           return -3;
        }
        if (gotFrame) {
            /* if a frame has been decoded, output it */
            int dataSize = av_get_bytes_per_sample(mCodexContext->sample_fmt);
            if (dataSize < 0) {
                /* This should not occur, checking just for paranoia */
                std::cout<<"Failed to calculate data size"<<std::endl;
                return -4;
            }

            for (i=0; i<mDecodedFrame->nb_samples; i++){
                for (ch=0; ch<mCodexContext->channels; ch++){
                    int tmpLen = *outBufLen + dataSize;
                    if(tmpLen > maxOutLen) {
                        std::cout<<"decoder error -4 tmpLen:"<<tmpLen<<" maxOutLen:"<<maxOutLen<<std::endl;
                        return -5;
                    }
                    memcpy(outBuf, mDecodedFrame->data[ch] + dataSize*i, dataSize);
                    outBuf += dataSize;
                    *outBufLen = tmpLen;
                }
            }                
        }
        mAvpkt.size -= len;
        mAvpkt.data += len;
        mAvpkt.dts =
        mAvpkt.pts = AV_NOPTS_VALUE;
        //如不足一帧,则存储起来,与后面数据一同解码
        if (mAvpkt.size < AUDIO_REFILL_THRESH && !isStreamEnd) {
            mRemainData.clear();
            mRemainData.insert(mRemainData.end(), mAvpkt.data, mAvpkt.data + mAvpkt.size);
            return mAvpkt.size;
        }
    }

    if(isStreamEnd) {
        std::cout<<"decoder end!"<<std::endl;
    }

    mRemainData.clear();

    return 0;
}

解码AAC音频:
AACDecoder.h

class AACDecoder : public AudioSoftDecoder{
public:
    AACDecoder();
    ~AACDecoder();

private:
};

AACDecoder.cpp

AACDecoder::AACDecoder(){
    mAudioType = AV_CODEC_ID_AAC;
}

AACDecoder::~AACDecoder() {

}

解码MP3音频:
MP3Decoder.h

class MP3Decoder : public AudioSoftDecoder{
public:
    MP3Decoder();
    ~MP3Decoder();
    
private:
};

MP3Decoder.cpp

MP3Decoder::MP3Decoder(){
    mAudioType = AV_CODEC_ID_MP3;
}

MP3Decoder::~MP3Decoder(){

}

类使用:

int main(int argc, char **argv)
{
    AACDecoder *adecoder = new AACDecoder();
    adecoder->init(2, 44100, 128072, 16);

    const char *outfilename, *filename;
    int len;
    FILE *f, *outfile;
    uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
    uint8_t outbuf[AUDIO_OUTBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];

    if (argc <= 2) {
        std::cout<<"Usage: %s <input file> <output file>"<<argv[0];
        return -1;
    }
    filename    = argv[1];
    outfilename = argv[2];


    f = fopen(filename, "rb");
    if (!f) {
        std::cout<<"Could not open %s"<<filename;
        return -1;
    }
    outfile = fopen(outfilename, "wb");
    if (!outfile) {
        std::cout<<"Could not open "<<outfilename;
        return -1;
    }

    
    int realLen = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
    bool isStreamEnd = false;
    while (realLen > 0) {
        int outLen = AUDIO_OUTBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE;
        len = adecoder->decode(inbuf, realLen, outbuf, &outLen, realLen < AUDIO_INBUF_SIZE);
        if (len < 0) {
            std::cout<<"Error while decoding ret:"<<std::hex<<len<<std::endl;
            return -1;
        }
        if (len >= 0 && outLen > 0) {
            fwrite(outbuf, 1, outLen, outfile);
            std::cout<<" wrote: "<<outLen<<std::endl;
        }
        realLen = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
    }

    adecoder->uninit();
    delete adecoder;

    fclose(outfile);
    fclose(f);

    return 0;
}

执行:

./decode_audio_aac ./maibuqifang_audioData.txt ./aac.pcm

播放解码后的aac.pcm:

ffplay -f s16le -ac 2 -ar 44100 aac.pcm




标签:AUDIO,ffmpeg,int,C++,mAvpkt,AACDecoder,解码器,AudioSoftDecoder,include
From: https://www.cnblogs.com/kn-zheng/p/17411535.html

相关文章

  • C++ ffmpeg硬件解码的实现方法
    什么是硬件解码普通解码是利用cpu去解码也就是软件解码硬件解码就是利用gpu去解码为什么要使用硬件解码首先最大的好处快硬解播放出来的视频较为流畅,并且能够延长移动设备播放视频的时间;而软解由于软解加大CPU工作负荷,会占用过多的移动CPU资源,如果CPU能力不足,则软件也将受......
  • c++程序流程结构
    c++程序流程结构c++中支持最基本的三种流程结构:顺序结构、选择结构、循环结构顺序结构:程序按顺序执行,不会发生跳转。选择结构:根据条件是否满足,有选择的执行相应的功能。循环结构:根据条件是否满足,循环多次执行某代码块。if语句:作用:执行满足条件的语句单行if语句:语法:if(条件){ 条件......
  • 初识c++
    c++开发环境搭建初识c++第一个c++程序c++程序框架:实例:#include<iostream>usingnamespacestd;intmain(){ system("pause"); return0;}//#include代表预处理指令iostream中声明了程序所需要的的输入和输出操作的有关信息#include<iostream>//usingnamespace针对命名......
  • MinGW32编译ffmpeg+libsrt
    MinGW编译带srt库的ffmpeg前言MinGW安装CMAKE安装VisualStudio2017安装支持windows的线程库SRT库的编译openssl的安装SRT编译MinGW下的SDL和opensslopensslSDLffmpeg编译PKG-CONFIG配置ffmpeg前言国内关于SRT协议的资料,几乎为0,没什么人用srt协议去编译过ffmpeg,而且这东西在Wind......
  • c++ ffmpeg 推送rtsp码_编译ffmpeg以获得极佳性能
    背景Gemfield最近尝试使用python封装的ffmpeg库(PyAV)来进行mp4文件、rtmp协议及其它协议的decode,具体来说就是将mp4文件(或者rtmp协议的数据,下同)进行demux并逐帧decode。然而在这期间发现了一些decode的性能问题。这些问题概括起来就是2点:python封装的ffmpeg是否能够利用到多核CPU的......
  • 【C++】多态(下)
    @TOC1.单继承中的虚函数表整体代码#include<iostream>usingnamespacestd;classBase{public:virtualvoidFunc1(){cout<<"Base::Func1()"<<endl;}virtualvoidFunc2(){cout<<"Base::Func......
  • c++打卡第二十九天
    模板编程对于模板编程,写template<typenameT>一、函数模板编程1、编辑模板表明返回值T或者无返回值+函数名(T&变量) 2、例题描述请使用模板参数设计实现双倍功能函数,函数功能要求实现返回值为输入参数的两倍,函数参数应能适应整型、浮点型、双精度型等各种类型,返回值类型与......
  • 输入输出流(C++)
    一、问题描述定义一个Dog类,包括体重和年龄两个数据成员及其成员函数,声明一个实例dog1,体重5,年龄10,使用I/O流把dog1的状态写入磁盘文件。再声明一个实例dog2,通过读取文件dog1的状态赋给dog2。分别用文本方式和二进制方式操作文件。二、代码实现1#include<fstream>2#includ......
  • 2654. 使数组所有元素变成 1 的最少操作次数(c++,gcd性质)
    题目链接:2654.使数组所有元素变成1的最少操作次数方法一:计算最短的gcd为1的子数组解题思路本题目标:使得所有的数组元素都变为\(1\),通过求相邻元素\(gcd\)将其赋值给一方的方式;思路:若想操作数最少,那么就是不为\(1\)的数\(x\)和1求\(gcd\),即\(x=gcd(x,1)\),......
  • c++打卡练习(33)
    歌星大赛,十个评委打分,去掉一个最高分,去掉一个最低分,求剩下的八个评分的平均分,作为选手的最终分数流程图:伪代码:源代码:#include<iostream>usingnamespacestd;intmain(){ inta[10],b[8]; inti,j,k,t,sum=0,Ave,max,min; cout<<"输入十个正整数"<<endl; for(i=0;i<10;i++){ ......