video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-10-10 b35d0da3fb4ceec328286851d0e2b316872ab04a
csrc/worker/decoder.cpp
@@ -25,7 +25,7 @@
        std::lock_guard<std::mutex> l(mutex_frm_);
        for(auto i : list_frm_){
            av_frame_free(&i.frm);
            free(i.data);
        }
        list_frm_.clear();
    }
@@ -47,24 +47,33 @@
    }
    int decoder::saveFrame(AVFrame *frame, int64_t &id){
        FRM frm;
        frm.width = frame->width;
        frm.height = frame->height;
        frm.format = frame->format;
        frm.id = id;
        frm.data = cvbridge::extractFrame(frame, &frm.length);
        std::lock_guard<std::mutex> l(mutex_frm_);
        while(list_frm_.size() > 50){
            for(int i = 0; i < 12; i++){
                auto t = list_frm_.front();
                av_frame_free(&t.frm);
                free(t.data);
                list_frm_.pop_front();
            }
        }
        list_frm_.push_back({frame,id});
        if (!frm.data) return 0;
        list_frm_.push_back(frm);
        return list_frm_.size();   
    }
    int decoder::SetFrame(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id){
        if (!data) return -1;
        if (!decRef_->isVideoPkt(&data->getAVPacket())) return -2;
        if (!data) return -10;
        if (!decRef_->isVideoPkt(&data->getAVPacket())) return -20;
        
        if (decRef_->getCodecContext() == NULL){
            if (initDecoder() != 0) return -3;
            if (initDecoder() != 0) return -30;
        }
        AVFrame *frame = av_frame_alloc();
@@ -76,50 +85,27 @@
        if (ret == 0){
            saveFrame(frame, id);
        }
        av_frame_free(&frame);
        return ret;
    }
    void decoder::GetFrame(unsigned char **data, int *w, int *h, int *format, int *length, int64_t *id){
        AVFrame *frm = NULL;
        {
            std::lock_guard<std::mutex> l(mutex_frm_);
            if(list_frm_.empty()){
                *data = NULL;
                *w = *h = 0;
                *id = -1;
                return;
            }
            auto p = list_frm_.front();
            list_frm_.pop_front();
            frm = p.frm;
            *id = p.id;
            *w = frm->width;
            *h = frm->height;
            *format = frm->format;
        }
        *length = avpicture_get_size((enum AVPixelFormat)frm->format, frm->width, frm->height);
        if (*length <= 0){
            logIt("get raw frame data error");
        std::lock_guard<std::mutex> l(mutex_frm_);
        if(list_frm_.empty()){
            *data = NULL;
            *w = *h = 0;
            *id = -1;
            return;
        }
        unsigned char *picData = (unsigned char*)malloc(*length);
        auto ret = avpicture_layout((const AVPicture*)frm, (enum AVPixelFormat)frm->format, frm->width, frm->height, picData, *length);
        av_frame_free(&frm);
        if (ret < 0){
            *data = NULL;
            *w = *h = 0;
            *id = -1;
            free(picData);
            return;
        }
        *data = picData;
        auto p = list_frm_.front();
        list_frm_.pop_front();
        *data = p.data;
        *id = p.id;
        *w = p.width;
        *h = p.height;
        *format = p.format;
        *length = p.length;
    }
} // namespace cffmpeg_wrap