video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-24 9792631e9d038ac34e287323961b78cc3d470873
bug fix
5个文件已修改
103 ■■■■ 已修改文件
csrc/ffmpeg/format/FormatIn.cpp 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/worker/decoder.cpp 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/worker/decoder.hpp 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.cpp 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.hpp 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/ffmpeg/format/FormatIn.cpp
@@ -47,8 +47,11 @@
            io_ctx_ = NULL;
        }
        if(ctx_){
            avformat_close_input(&ctx_);
            avformat_free_context(ctx_);
            if (!ctx_->oformat){
                avformat_free_context(ctx_);
            }else{
                avformat_close_input(&ctx_);
            }
            ctx_ = NULL;
            if(dec_ctx_){
                avcodec_close(dec_ctx_);
@@ -83,7 +86,7 @@
        auto err = av_probe_input_buffer(ctx_->pb, &ctx_->iformat, NULL, NULL, 0, read_io_buff_size_);
        if(err != 0){
            logIt("open with custom io prob input buffer error:%d\n", err);
            logIt("failed:%s", getAVErrorDesc(err).c_str());
            logIt("custom io failed:%s", getAVErrorDesc(err).c_str());
            return -1;
        }
@@ -106,9 +109,10 @@
        int ret = openWithCustomIO(handle_gb28181, handle_gb28181->readData, options);
        if(ret < 0){
            logIt("do openWithCustomIO failed:%d",ret);
        }
        }else{
            ret = avformat_open_input(&ctx_, "", NULL, options);
        }
        ret = avformat_open_input(&ctx_, "", NULL, options);
        // if(ret < 0){
            // logIt("open %s failed:%s",filename,
            //       getAVErrorDesc(ret).c_str());
csrc/worker/decoder.cpp
@@ -3,7 +3,6 @@
#include "../ffmpeg/bridge/cvbridge.hpp"
#include "../ffmpeg/format/FormatIn.hpp"
#include "../ffmpeg/data/CodedData.hpp"
#include "../ffmpeg/data/FrameData.hpp"
#include "../ffmpeg/log/log.hpp"
extern "C"{
@@ -24,24 +23,12 @@
    ,conv_h_(h)
    ,conv_flag_(f)
    ,decRef_(dec)
    ,thread_(nullptr)
    ,stop_{false}
    {}
    
    decoder::~decoder(){
        if (thread_){
            stop_.store(true);
            thread_->join();
        }
        if (conv_){
            delete conv_;
        }
        {
            std::lock_guard<std::mutex> l(mutex_pkt_);
            list_pkt_.clear();
        }
        {
@@ -102,57 +89,11 @@
        return list_pic_.size();
    }
    void decoder::Start(){
        if (thread_) return;
        thread_.reset(new std::thread([&]{
            if (initDecoder() != 0) {
                return;
            }
            while(!stop_.load()){
                std::unique_lock<std::mutex> locker(mutex_pkt_);
                cv_.wait(locker, [&]{
                    return !list_pkt_.empty() || stop_.load();
                });
                if (stop_.load()){
                    break;
                }
                auto pkt = list_pkt_.front();
                list_pkt_.pop_front();
                AVFrame *frame = av_frame_alloc();
                AVPacket np(pkt.data->getAVPacket());
                av_copy_packet(&np, &pkt.data->getAVPacket());
                auto ret = decRef_->decode(frame, &np);
                av_packet_unref(&np);
                if (ret == 0){
                    saveFrame(frame, pkt.id);
                }
                av_frame_free(&frame);
            }
        }));
    }
    int decoder::SetFrame(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id){
        if (!data) return -1;
        if (decRef_->isAudioPkt(&data->getAVPacket())) return -2;
        
        // if (!thread_){
        //     if (initDecoder() != 0) return -3;
        //     Start();
        // }
        // std::lock_guard<std::mutex> l(mutex_pkt_);
        // list_pkt_.push_back({data, id});
        // cv_.notify_one();
        // return list_pkt_.size();
        if (!conv_){
            if (initDecoder() != 0) return -3;
        }
@@ -167,7 +108,6 @@
            saveFrame(frame, id);
        }
        av_frame_free(&frame);
    }
    void decoder::GetFrame(unsigned char **data, int *w, int *h, int64_t *id){
csrc/worker/decoder.hpp
@@ -9,15 +9,13 @@
#include <atomic>
#include <condition_variable>
#include "../common.hpp"
struct AVFrame;
namespace ffwrapper
{
    class FormatIn;
    class cvbridge;
    class CodedData;
} // namespace ffwrapper
namespace cffmpeg_wrap
@@ -41,12 +39,6 @@
        std::list<BGR24> list_pic_;
        std::mutex mutex_pic_;
        std::unique_ptr<std::thread> thread_;
        std::atomic_bool    stop_;
        std::list<CPacket> list_pkt_;
        std::mutex mutex_pkt_;
         std::condition_variable cv_;
    private:
        int initDecoder();
        int saveFrame(AVFrame *frame, int64_t &id);
csrc/wrapper.cpp
@@ -46,6 +46,7 @@
    ,audio_(false)
    ,gb_(0)
    ,cpu_(0)
    ,run_dec_(false)
    ,thread_(nullptr)
    ,stop_stream_(false)
    ,stream_(nullptr)
@@ -133,14 +134,17 @@
        decoder_ = new decoder(in, scale_w_, scale_h_, scale_f_);
        rec_->Load(in);
        if(fn_rec_lazy_) fn_rec_lazy_(in);
        if(fn_rec_lazy_) {
            fn_rec_lazy_();
            fn_rec_lazy_ = nullptr;
        }
    }
    
    void Wrapper::run_worker(ffwrapper::FormatIn *in, std::shared_ptr<ffwrapper::CodedData> data, int64_t &id){
        if (stream_) stream_->SetPacket(data, id);
        if (decoder_ && run_dec_) decoder_->SetFrame(data, id);
        if (rec_->Loaded()) rec_->SetPacket(data, id);
        if (decoder_) decoder_->SetFrame(data, id);
    }
    void Wrapper::deinit_worker(){
@@ -191,7 +195,7 @@
        }else{
            std::string rid(id), dir(output);
            fn_rec_lazy_ = 
            [=](ffwrapper::FormatIn *in){rec_->NewRec(rid.c_str(), dir.c_str(), mindur, maxdur, audio);};
            [=]{rec_->NewRec(rid.c_str(), dir.c_str(), mindur, maxdur, audio);};
        }
    }
@@ -208,7 +212,7 @@
    }
    ////////decoder
    void Wrapper::BuildDecoder(){
        // use_decoder_ = true;
        run_dec_ = true;
    }
    void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h, int64_t *id){
@@ -216,7 +220,7 @@
            decoder_->GetFrame(data, w, h, id);
        }
    }
    void Wrapper::GetPacket(unsigned char **pktData, int *size, int *key){
        if (stream_){
            stream_->GetPacket(pktData, size, key);
@@ -275,7 +279,7 @@
        
        uint8_t *pic = NULL;
        *w = *h = 0;
        int tryTime = 0;
        while (tryTime++ < 100){
            
csrc/wrapper.hpp
@@ -62,6 +62,7 @@
        int scale_w_, scale_h_, scale_f_;
        bool audio_;
        int gb_, cpu_;
        bool run_dec_;
        // decoder 参数
        std::unique_ptr<std::thread> thread_;
        std::atomic_bool    stop_stream_;
@@ -73,7 +74,7 @@
        // 录像类,一直存在
        rec* rec_;
        // 录像请求缓存,等待runstream后添加
        std::function<void(ffwrapper::FormatIn*)> fn_rec_lazy_;
        std::function<void()> fn_rec_lazy_;
    };
    uint8_t* Decode(const char *file, const int gb, int *w, int *h);