video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-11-22 3f1386db26fc1d8d7b615907d21d0f196780eda2
csrc/worker/decoder.cpp
@@ -20,14 +20,10 @@
{
    decoder::decoder(ffwrapper::FormatIn *dec)
    :decRef_(dec)
    ,conv_(NULL)
    {}
    
    decoder::~decoder(){
        if (conv_){
            delete conv_;
            conv_ = NULL;
        }
        std::lock_guard<std::mutex> l(mutex_frm_);
        for(auto i : list_frm_){
            free(i.data);
@@ -52,31 +48,32 @@
    }
    int decoder::saveFrame(AVFrame *frame, const int64_t &id){
        AVFrame *cFrame = NULL;
        bool conv = false;
        if (frame->format != AV_PIX_FMT_NV12){
            if (!conv_){
                conv_ = new cvbridge(
                    frame->width, frame->height, frame->format,
                    frame->width, frame->height, AV_PIX_FMT_NV12);
            }
            if (conv_){
                cFrame = conv_->convert2Frame(frame);
                if (!cFrame) return -1;
                conv = true;
            }
        }
        AVFrame * rFrame = frame;
        if (conv && cFrame) rFrame = cFrame;
        FRM frm;
        frm.width = rFrame->width;
        frm.height = rFrame->height;
        frm.format = rFrame->format;
        frm.width = frame->width;
        frm.height = frame->height;
        frm.format = frame->format;
        frm.id = id;
        frm.data = cvbridge::extractFrame(rFrame, &frm.length);
        uint8_t *origin = cvbridge::extractFrame(frame, &frm.length);
        if (!origin) return -1;
        if (conv && cFrame) av_frame_free(&cFrame);
        uint8_t *finale = NULL;
        if (frame->format != AV_PIX_FMT_NV12){
            finale = (uint8_t*)malloc(frm.length);
            unsigned char* SrcU = origin + frm.width * frm.height;
            unsigned char* SrcV = SrcU + frm.width * frm.height / 4 ;
            unsigned char* DstU = finale + frm.width * frm.height;
            memcpy(finale, origin, frm.width * frm.height);
            int i = 0;
            for( i = 0 ; i < frm.width * frm.height / 4 ; i++ ){
                *(DstU++) = *(SrcU++);
                *(DstU++) = *(SrcV++);
            }
            free(origin);
        }else{
            finale = origin;
        }
        frm.data = finale;
        std::lock_guard<std::mutex> l(mutex_frm_);
        while(list_frm_.size() > 50){