video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-24 93f44a10e2e8942e57e62bb210a2ca7d206a51b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "decoder.hpp"
 
#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"{
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
}
 
using namespace ffwrapper;
using namespace logif;
 
namespace cffmpeg_wrap
{
    decoder::decoder(ffwrapper::FormatIn *dec, const int w, const int h, const int f)
    :conv_(NULL)
    ,conv_w_(w)
    ,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();
        }
 
        {
            std::lock_guard<std::mutex> l(mutex_pic_);
            for(auto &i : list_pic_){
                free(i.data);
            }
            list_pic_.clear();
        }
    }
 
    int decoder::initDecoder(){
        if (!decRef_) return -1;
 
        if(decRef_->getCodecContext() == NULL){
                
            bool flag = true;
            flag = decRef_->openCodec(NULL);
            auto dec_ctx = decRef_->getCodecContext();
            if(conv_){
                delete conv_;
                conv_ = NULL;
            }
            conv_w_ = conv_w_ == 0 || conv_w_ > dec_ctx->width ? dec_ctx->width : conv_w_;
            conv_h_ = conv_h_ == 0 || conv_h_ > dec_ctx->height ? dec_ctx->height : conv_h_;
            AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
            conv_ = new cvbridge(
                    dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
                    conv_w_, conv_h_, pix_fmt, conv_flag_);
 
            if (!flag){
                logIt("FormatIn openCodec Failed!");
                return -1;
            }
        }
        return 0;
    }
 
    int decoder::saveFrame(AVFrame *frame, int64_t &id){
        //缓存数据
        BGR24 pic;
        AVFrame *frm = frame;
        pic.w = conv_w_;
        pic.h = conv_h_;
        unsigned char *picData = (unsigned char*)malloc(pic.w * pic.h * 3);
        conv_->copyPicture(picData, frm);
        pic.data = picData;
        pic.id = id;
        std::lock_guard<std::mutex> l(mutex_pic_);
        while(list_pic_.size() > 50){
            for(int i = 0; i < 12; i++){
                auto t = list_pic_.front();
                free(t.data);
                list_pic_.pop_front();
            }
        }
        list_pic_.emplace_back(pic);
        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;
        }
 
        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, id);
        }
        av_frame_free(&frame);
 
    }
 
    void decoder::GetFrame(unsigned char **data, int *w, int *h, int64_t *id){
        std::lock_guard<std::mutex> l(mutex_pic_);
        if(list_pic_.empty()){
            *data = NULL;
            *w = 0;
            *h = 0;
            return;
        }
        auto p = list_pic_.front();
        *data = p.data; *w = p.w; *h = p.h;
        *id = p.id;
        list_pic_.pop_front();
    }
 
} // namespace cffmpeg_wrap