video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-11-29 1939b7042d568c50509118a6031829ee5bf5f544
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
#include "decoder.hpp"
 
#include "../ffmpeg/bridge/cvbridge.hpp"
#include "../ffmpeg/format/FormatIn.hpp"
#include "../ffmpeg/data/CodedData.hpp"
#include "../ffmpeg/log/log.hpp"
#include "../common.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)
    :decRef_(dec)
    ,next_idx_(-1)
    {}
    
    decoder::~decoder(){
        
        std::lock_guard<std::mutex> l(mutex_pkt_);
        list_pkt_.clear();
    }
 
    int decoder::initDecoder(){
        if (!decRef_) return -1;
 
        if(decRef_->getCodecContext() == NULL){
                
            bool flag = true;
            flag = decRef_->openCodec(NULL);
 
            if (!flag){
                logIt("FormatIn openCodec Failed!");
                return -1;
            }
        }
        return 0;
    }
 
    int decoder::SetFrame(const CPacket &pkt){
        auto data = pkt.data;
 
        if (!data) return -10;
        if (!decRef_->isVideoPkt(&data->getAVPacket())) return -20;
        
        if (decRef_->getCodecContext() == NULL){
            if (initDecoder() != 0) return -30;
        }
 
        std::lock_guard<std::mutex> l(mutex_pkt_);
        if (data->getAVPacket().flags & AV_PKT_FLAG_KEY){
            logIt("new list cpacket start, next %lld, cur %lld", next_idx_, pkt.id);
            list_pkt_.clear();
        }
        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){
 
        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;
                logIt("decoder new list cpacket");
            }
            
            for (auto &i : list_pkt_){
                if (i.id < next_idx_){
                    continue;
                    logIt("decoder same list cpacket");
                }
 
                *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;
                }
            }
        }
        if (!frame) return;
 
        int pix_fmt = frame->format;
        int width = frame->width;
        int height = frame->height;
        int len = 0;
 
        uint8_t *origin = cvbridge::extractFrame(frame, &len);
        av_frame_free(&frame);
        if (!origin) return;
 
        uint8_t *finale = NULL;
        if (pix_fmt != AV_PIX_FMT_NV12){
            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);
        }else{
            finale = origin;
        }
 
        *data = finale;
        *w = width;
        *h = height;
        *format = pix_fmt;
        *length = len;
    }
 
} // namespace cffmpeg_wrap