video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-07-30 a6f5c0ca80b2fdd53351d5957a659a939f530fc2
update rec fid
6个文件已修改
106 ■■■■ 已修改文件
cffmpeg.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/buz/recorder.cpp 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/cffmpeg.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.cpp 64 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.hpp 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
goffmpeg.go 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
cffmpeg.h
@@ -23,7 +23,7 @@
char* c_ffmpeg_get_rec_id(const cffmpeg h, const char* p, int *length);
void c_ffmpeg_build_decoder(const cffmpeg h);
void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei);
void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei, int64_t *id);
void* c_ffmpeg_get_avpacket(const cffmpeg h, int *size, int *key);
//////test
csrc/buz/recorder.cpp
@@ -82,6 +82,9 @@
        void Recorder::end_writer(){
            if(cur_frame == -1) return;
            out_->endWriter();
            logIt("INDEX %d, FILE %s, CURFrame %d, ENDFrame %d\n",
                 file_frame_index_, file_path_.c_str(), cur_frame, end_frame);
            //reinit cur_frame clear list pkt
            {
                std::lock_guard<std::mutex> locker(mutex_pkt_);
@@ -92,7 +95,6 @@
            //callback to frame index and path
            if(func_rec_info_){
                func_rec_info_(id_,file_frame_index_, file_path_);
                // logIt("recoder index %d, file name %s\n", file_frame_index_, file_path_.c_str());
            }else{
                // logIt("recorder has no func_rec_info");
            }
@@ -187,6 +189,7 @@
                    end_frame = maxduration;
                }
            }
            logIt("FIRE REC FRAME ID: %lld", id);
            return 0;
        }
@@ -216,6 +219,7 @@
                cv_.notify_one();
            }
            logIt("CACHE PACKET FRAME ID %lld", pkt.id);
            return 0;
        }
csrc/cffmpeg.cpp
@@ -90,10 +90,10 @@
    s->BuildDecoder();
}
void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei){
void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei, int64_t *id){
    Wrapper *s = (Wrapper*)h;
    unsigned char *data = NULL;
    s->GetPicDecoder(&data, wid, hei);
    s->GetPicDecoder(&data, wid, hei, id);
    return data;
}
csrc/wrapper.cpp
@@ -39,6 +39,8 @@
    ,gb_(0)
    ,cpu_(0)
    ,use_decoder_(false)
    ,minduration(250)
    ,maxduration(750)
    {
        makeTheWorld();
    }
@@ -143,6 +145,10 @@
                run_worker(in.get(), pkt);
                if(!data){
                    map_rec_.clear();
                    std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
                    list_rec_pkt_.clear();
                    break;
                }
                //test
@@ -180,19 +186,59 @@
            auto ret = in->decode(frame, pkt.data);
            if(ret == 1){
                //吐出数据
                cache_pic(frame);
                cache_pic(frame, pkt.id);
            }
        }
        cache_rec_pkt(pkt);
        for(auto &i : map_rec_){
            if (!i.second.rec){
                i.second.rec = i.second.fn_init(in);
            }
            if (i.second.rec){
                if (i.second.rec){
                    std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
                    for(auto &k : list_rec_pkt_){
                        avpacket p = {k.data, k.id};
                        i.second.rec->CachePacket(p);
                    }
                    logIt("start rec %d frames", list_rec_pkt_.size());
                }
            }else if (i.second.rec){
                i.second.rec->CachePacket(pkt);
            }
        }
    }
    int Wrapper::cache_rec_pkt(const avpacket &pkt){
        std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
        //wait I
        if (list_rec_pkt_.empty()) {
            AVPacket &avpkt = pkt.data->getAVPacket();
            if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
                return -1;
            }
        }
        maybe_dump_rec_pkt();
        recpkt k = {pkt.data, pkt.id};
        list_rec_pkt_.push_back(k);
        return 0;
    }
    void Wrapper::maybe_dump_rec_pkt(){
        //超过min/2,丢弃gop
        while (list_rec_pkt_.size() > minduration) {
            list_rec_pkt_.pop_front();
            while(!list_rec_pkt_.empty()){
                auto &cache = list_rec_pkt_.front();
                AVPacket &avpkt = cache.data->getAVPacket();
                if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
                    list_rec_pkt_.pop_front();
                }else{
                    break;
                }
            }
        }
    }
    //////////////recorder
    std::shared_ptr<Recorder> Wrapper::init_recorder(FormatIn *in, std::string id,std::string dir, const int mind, const int maxd){
        if(!in){
@@ -228,6 +274,9 @@
        
        FnRec r = FnRec{fn, rec};
        map_rec_[rid] = r;
        minduration = mindur * 25;
        maxduration = maxdur * 25;
    }
    int Wrapper::FireRecorder(const char* sid,const int64_t &id){
@@ -237,6 +286,7 @@
                iter->second.rec->FireRecorder(id);
            }
        }
        logIt("FIRE REC %s, FRAME ID: %d", sid, id);
    }
    void Wrapper::cache_rec_info(std::string &id, int &index, std::string &path){
@@ -284,7 +334,7 @@
        use_decoder_ = true;
    }
    void Wrapper::cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame){
    void Wrapper::cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame, int64_t &id){
        pic_bgr24 pic;
        if(bridge_){
@@ -295,6 +345,7 @@
            unsigned char *data = (unsigned char*)malloc(pic.w * pic.h * 3);
            bridge_->copyPicture(data, frm);
            pic.data = data;
            pic.id = id;
        }
        
        {
@@ -311,7 +362,7 @@
    }
    void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h){
    void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h, int64_t *id){
        std::lock_guard<std::mutex> l(mutex_pic_);
        if(list_pic_.empty()){
            *data = NULL;
@@ -321,6 +372,7 @@
        }
        auto p = list_pic_.front();
        *data = p.data; *w = p.w; *h = p.h;
        *id = p.id;
        list_pic_.pop_front();
    }
csrc/wrapper.hpp
@@ -35,6 +35,8 @@
        unsigned char *data;
        int w;
        int h;
        int64_t id;
    }pic_bgr24;
    namespace buz{
@@ -61,7 +63,7 @@
            std::shared_ptr<buz::Recorder> init_recorder(ffwrapper::FormatIn *in, std::string id,std::string dir, const int mind, const int maxd);
            void cache_rec_info(std::string &id, int &index, std::string &path);
            void cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame);
            void cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame, int64_t &id);
            void cacheAVPacket(const AVPacket &pkt);
        public: 
@@ -80,7 +82,7 @@
            void UseCPU();
        public: //decoder
            void BuildDecoder();
            void GetPicDecoder(unsigned char **data, int *w, int *h);
            void GetPicDecoder(unsigned char **data, int *w, int *h, int64_t *id);
            void GetPacket(unsigned char **pktData, int *size, int *key);
            //active api
@@ -114,6 +116,18 @@
            int scale_w_, scale_h_, scale_f_;
            int gb_, cpu_;
            typedef struct recpkt{
                std::shared_ptr<ffwrapper::CodedData> data;
                int64_t id;
            }recpkt;
            std::list<recpkt> list_rec_pkt_;
            std::mutex mtx_rec_pkt_;
            int cache_rec_pkt(const buz::avpacket &pkt);
            void maybe_dump_rec_pkt();
            int     maxduration;
            int     minduration;
        //////////////////test frame to bgr24
        public:
            uint8_t *decodeJPEG(const char *file, int *w, int *h);
goffmpeg.go
@@ -160,6 +160,7 @@
    return int(i), path
}
// GetRecID get rec id
func (h *GoFFMPEG) GetRecID(p string) string {
    pt := C.CString(p)
    defer C.free(unsafe.Pointer(pt))
@@ -179,20 +180,21 @@
}
// GetPicDecoder get pic from decoder
func (h *GoFFMPEG) GetPicDecoder() ([]byte, int, int) {
func (h *GoFFMPEG) GetPicDecoder() ([]byte, int, int, int64) {
    var width C.int
    var height C.int
    var fid C.longlong
    p := C.wrap_fn_decoder_pic(h.ffmpeg, &width, &height)
    p := C.wrap_fn_decoder_pic(h.ffmpeg, &width, &height, &fid)
    if width == 0 && height == 0 {
        return nil, 0, 0
        return nil, 0, 0, 0
    }
    defer C.free(unsafe.Pointer(p))
    d := C.GoBytes(p, width*height*3)
    wid := int(width)
    hei := int(height)
    return d, wid, hei
    gfid := int64(fid)
    return d, wid, hei, gfid
}
//GetAVPacket get AVPacket