video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2023-12-26 18a05d269516a5e33d8460291c2f93e73d95adce
csrc/worker/decoder.cpp
@@ -20,19 +20,13 @@
{
    decoder::decoder(ffwrapper::FormatIn *dec)
    :decRef_(dec)
    ,conv_(NULL)
    ,next_idx_(-1)
    {}
    
    decoder::~decoder(){
        if (conv_){
            delete conv_;
            conv_ = NULL;
        }
        std::lock_guard<std::mutex> l(mutex_frm_);
        for(auto i : list_frm_){
            free(i.data);
        }
        list_frm_.clear();
        std::lock_guard<std::mutex> l(mutex_pkt_);
        list_pkt_.clear();
    }
    int decoder::initDecoder(){
@@ -51,46 +45,6 @@
        return 0;
    }
    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.id = id;
        frm.data = cvbridge::extractFrame(rFrame, &frm.length);
        if (conv && cFrame) av_frame_free(&cFrame);
        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();
                free(t.data);
                list_frm_.pop_front();
            }
        }
        if (!frm.data) return 0;
        list_frm_.push_back(frm);
        return list_frm_.size();
    }
    int decoder::SetFrame(const CPacket &pkt){
        auto data = pkt.data;
@@ -101,36 +55,88 @@
            if (initDecoder() != 0) return -30;
        }
        AVFrame *frame = av_frame_alloc();
        AVPacket np(data->getAVPacket());
        av_copy_packet(&np, &data->getAVPacket());
        auto ret = decRef_->decode(frame, &np);
        av_packet_unref(&np);
        if (ret == 0){
            saveFrame(frame, pkt.v_id);
        std::lock_guard<std::mutex> l(mutex_pkt_);
        if (data->getAVPacket().flags & AV_PKT_FLAG_KEY){
            list_pkt_.clear();
        }
        av_frame_free(&frame);
        return ret;
        list_pkt_.push_back(pkt);
        return list_pkt_.size();
    }
    void decoder::GetFrame(unsigned char **data, int *w, int *h, int *format, int *length, int64_t *id){
        *data = NULL;
        *length = 0;
        std::lock_guard<std::mutex> l(mutex_frm_);
        if(list_frm_.empty()){
            *data = NULL;
            *w = *h = 0;
            *id = -1;
            return;
        AVFrame *frame = NULL;
        {
            std::lock_guard<std::mutex> l(mutex_pkt_);
            if (list_pkt_.empty()) return;
            auto check = list_pkt_.front();
            if (check.id > next_idx_){
                next_idx_ = -1;
            }
            for (auto &i : list_pkt_){
                if (i.id < next_idx_){
                    continue;
                }
                *id = i.v_id;
                auto data = i.data;
                AVFrame *frm = av_frame_alloc();
                AVPacket np(data->getAVPacket());
                av_copy_packet(&np, &data->getAVPacket());
                auto ret = decRef_->decode(frm, &np);
                av_packet_unref(&np);
                if (ret == 0){
                    next_idx_ = i.id + 1;
                    if (frame) {av_frame_free(&frame); frame = NULL;}
                    frame = frm;
                }else {
                    av_frame_free(&frm);
                }
            }
        }
        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;
        if (!frame) return;
        int pix_fmt = frame->format;
        int width = frame->width;
        int height = frame->height;
        if (pix_fmt != AV_PIX_FMT_NV12){
            cvbridge* bridge = new cvbridge(width, height, pix_fmt,
                width, height, AV_PIX_FMT_NV12);
            AVFrame* nv12 = bridge->convert2Frame(frame);
            av_frame_free(&frame);
            frame = nv12;
            delete bridge;
            // finale = (uint8_t*)malloc(len);
            // unsigned char* SrcU = origin + width * height;
            // unsigned char* SrcV = SrcU + width * height / 4 ;
            // unsigned char* DstU = finale + width * height;
            // memcpy(finale, origin, width * height);
            // int i = 0;
            // for( i = 0 ; i < width * height / 4 ; i++ ){
            //     *(DstU++) = *(SrcU++);
            //     *(DstU++) = *(SrcV++);
            // }
            // free(origin);
        }
        int len = 0;
        uint8_t* finale = cvbridge::extractFrame(frame, &len);
        av_frame_free(&frame);
        *data = finale;
        *w = width;
        *h = height;
        *format = pix_fmt;
        *length = len;
    }
} // namespace cffmpeg_wrap