video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-24 9792631e9d038ac34e287323961b78cc3d470873
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,56 +89,10 @@
        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){