video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-26 1f005df2f3ff78458f332f9bf1cf2e78b6a8e8e4
bug fix audio codec
8个文件已修改
113 ■■■■ 已修改文件
csrc/buz/recorder.cpp 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/ffmpeg/format/FormatIn.cpp 31 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/ffmpeg/format/FormatIn.hpp 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/ffmpeg/format/FormatOut.cpp 57 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/ffmpeg/format/FormatOut.hpp 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/ffmpeg/log/log.cpp 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/worker/decoder.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/buz/recorder.cpp
@@ -72,13 +72,16 @@
            out_ = new FormatOut(in_->getFPS(), "mp4");
            
            file_path_ = dir_ + "/" + sole::uuid4().base62() + ".mp4";
            auto ret = out_->JustWriter(in_->getFromatContext(), file_path_.c_str());
            int pid = getpid();
            file_path_ = dir_ + "/" + sole::uuid4().base62() + "-" + std::to_string(pid) + ".mp4";
            auto ret = out_->JustWriter(in_->allStreams(), file_path_.c_str());
            if (ret){
                logIt("start record file: %s", file_path_.c_str());
                return 0;
            }
            logIt("failed to start record: %s", file_path_.c_str());
            return -1;
        }
csrc/ffmpeg/format/FormatIn.cpp
@@ -143,11 +143,13 @@
            return false;
        }
        logIt("there are %d stream", ctx_->nb_streams);
        // logIt("there are %d stream", ctx_->nb_streams);
        for (int i = 0; i < ctx_->nb_streams; ++i)
        {
            auto type = ctx_->streams[i]->codecpar->codec_type;
            logIt("there are %d stream, stream %d, type %d", ctx_->nb_streams, i, type);
            if (type == AVMEDIA_TYPE_VIDEO){
                vs_idx_ = i;
@@ -157,10 +159,15 @@
                }else if(in->r_frame_rate.num >=1 && in->r_frame_rate.den >= 1){
                    fps_ = av_q2d(in->r_frame_rate);
                }
                logIt("in stream fps %f, time_base: %d : %d", fps_, in->time_base.num, in->time_base.den);
                logIt("in stream video fps %f, time_base: %d : %d", fps_, in->time_base.num, in->time_base.den);
            }
            if (type == AVMEDIA_TYPE_AUDIO){
                as_idx_ = i;
                auto in = ctx_->streams[i];
                logIt("in stream audio %d time_base: %d : %d", in->codecpar->codec_id, in->time_base.num, in->time_base.den);
                if (in->codecpar->codec_id == AV_CODEC_ID_AAC)
                    as_idx_ = i;
                else
                    logIt("record not support audio codec: %d", in->codecpar->codec_id);
            }
        }
@@ -199,7 +206,6 @@
                    continue;
                }else{
                    av_dict_set(&avdic, "gpu", std::to_string(idle_gpu).c_str(), 0);
                    // av_dict_set(&avdic, "gpu", std::to_string(2).c_str(), 0);
                }
            }else{
                dec = avcodec_find_decoder(codecpar->codec_id);
@@ -268,6 +274,19 @@
        return NULL;
    }
    std::vector<AVStream*> FormatIn::allStreams(){
        std::vector<AVStream*> vec;
        auto v = getStream(AVMEDIA_TYPE_VIDEO);
        if (v){
            vec.push_back(v);
        }
        auto a = getStream(AVMEDIA_TYPE_AUDIO);
        if (a){
            vec.push_back(a);
        }
        return vec;
    }
    AVCodecContext *FormatIn::getCodecContext(int type){
        return dec_ctx_;
    }
@@ -286,6 +305,10 @@
        return false;
    }
    bool FormatIn::notVideoAudio(AVPacket *pkt){
        return !isVideoPkt(pkt) && !isAudioPkt(pkt);
    }
    int FormatIn::readPacket(AVPacket *pkt_out){
        return av_read_frame(ctx_, pkt_out);
csrc/ffmpeg/format/FormatIn.hpp
@@ -3,6 +3,7 @@
#include <stdint.h>
#include <memory>
#include <vector>
#include "PsToEs.hpp"
struct AVFormatContext;
@@ -41,10 +42,12 @@
        
        bool isVideoPkt(AVPacket *pkt);
        bool isAudioPkt(AVPacket *pkt);
        bool notVideoAudio(AVPacket *pkt);
    private:
        bool allocCodec(AVCodec *dec, AVStream *s, AVDictionary **options);
    public:
        AVStream *getStream(int type = -1);
        std::vector<AVStream*> allStreams();
        AVCodecContext *getCodecContext(int type = 0);
        AVFormatContext *getFromatContext(){return ctx_;}
        const double getFPS()const{return fps_;}
csrc/ffmpeg/format/FormatOut.cpp
@@ -275,11 +275,12 @@
        return true;
    }
    bool FormatOut::copyCodecFromIn(AVFormatContext* in){
    bool FormatOut::copyCodecFromIn(std::vector<AVStream*> in){
        for(int i = 0; i < in->nb_streams; i++)
        {    //根据输入流创建输出流
            AVStream *in_stream = in->streams[i];
        for (int i = 0; i < in.size(); i++){
            AVStream *in_stream = in[i];
            AVStream *out_stream = avformat_new_stream(ctx_, in_stream->codec->codec);
            if(!out_stream)
            {
@@ -297,23 +298,31 @@
            if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
                v_idx_ = i;
            }else{
                a_idx_ = i;
            }
                logIt("copy video from instream");
            out_stream->codecpar->codec_tag = out_stream->codec->codec_tag = 0;
                out_stream->codecpar->codec_tag = out_stream->codec->codec_tag = 0;
            
            if(ctx_->oformat->flags & AVFMT_GLOBALHEADER)
                out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
                if(ctx_->oformat->flags & AVFMT_GLOBALHEADER)
                    out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
            }
            if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO){
                logIt("copy audio from instream");
                a_idx_ = i;
                out_stream->codecpar->codec_tag = out_stream->codec->codec_tag = 0;
                if(ctx_->oformat->flags & AVFMT_GLOBALHEADER)
                    out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
            }
        }
        in_ctx_ = in;
        in_streams_ = in;
        return true;
    }
    bool FormatOut::JustWriter(AVFormatContext *in, const char *filename){
    bool FormatOut::JustWriter(std::vector<AVStream*> in, const char *filename){
        if(ctx_){
            clear();
        }
@@ -335,7 +344,6 @@
            av_dict_set(&avdic,option_key,option_value,0);
            flag = writeHeader(&avdic);
            av_dict_free(&avdic);
        }
        
        return flag;
@@ -408,11 +416,26 @@
        }
        AVStream *in_stream,*out_stream;
        int out_idx = -1;
        for (auto i : in_streams_){
            if (i->index == pkt->stream_index){
                if (i->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
                    out_idx = v_idx_;
                    in_stream = i;
                    break;
                }else if (i->codecpar->codec_type == AVMEDIA_TYPE_AUDIO){
                    in_stream = i;
                    out_idx = a_idx_;
                    break;
                }
            }
        }
        if (out_idx == -1) return;
        out_stream = ctx_->streams[out_idx];
        pkt->stream_index = out_idx;
        
        in_stream = in_ctx_->streams[pkt->stream_index];
        out_stream = ctx_->streams[pkt->stream_index];
        // logIt("BEFORE in stream timebase %d:%d, out timebase %d:%d, \
        // logIt("BEFORE in stream timebase %d:%d, out timebase %d:%d,
        //         pts: %lld, dts: %lld, duration: %lld", 
        //     in_stream->time_base.num, in_stream->time_base.den,
        //     out_stream->time_base.num, out_stream->time_base.den,
csrc/ffmpeg/format/FormatOut.hpp
@@ -35,11 +35,11 @@
        int encode(AVPacket *pkt, AVFrame *frame);
    public:
        bool copyCodecFromIn(AVFormatContext* in);
        bool copyCodecFromIn(std::vector<AVStream*> in);
        bool openResource(const char *filename, const int flags);
        bool closeResource();
        bool JustWriter(AVFormatContext* in, const char *filename);
        bool JustWriter(std::vector<AVStream*> in, const char *filename);
        bool EncodeWriter(const char *filename);
        bool writeFrame(AVPacket *pkt, const int64_t &frame_cnt, bool interleaved = true);
        void adjustPTS(AVPacket *pkt, const int64_t &frame_cnt);
@@ -73,7 +73,7 @@
        std::string             format_name_;
        // rec
        AVFormatContext         *in_ctx_;
        std::vector<AVStream*>  in_streams_;
    };
}
#endif
csrc/ffmpeg/log/log.cpp
@@ -87,6 +87,7 @@
            std::string lc(temp);
            lc = "LIB-libcffmpeg.so-> " + lc;
            spdlog::get(log_name)->error(lc);
            spdlog::get(log_name)->flush();
        }else{
            printf("%s\n", temp);
        }
csrc/worker/decoder.cpp
@@ -92,7 +92,7 @@
    int decoder::SetFrame(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id){
        if (!data) return -1;
        if (decRef_->isAudioPkt(&data->getAVPacket())) return -2;
        if (!decRef_->isVideoPkt(&data->getAVPacket())) return -2;
        
        if (!conv_){
            if (initDecoder() != 0) return -3;
csrc/wrapper.cpp
@@ -186,6 +186,10 @@
                    break;
                }
                if (in->notVideoAudio(&data->getAVPacket())){
                    continue;
                }
                if (!gb_ && id < 0){
                    id++;
                    continue;