video analysis2.0拆分,ffmpeg封装go接口库
chenshijun
2019-07-17 0367d8e503ca5df1eae359929b8753050ccd9980
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "FilterVideo.hpp"
 
#include <stdexcept>
 
extern "C"{
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
//#include <libavfilter/avfiltergraph.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
}
 
#include "../log/log.hpp"
#include "../configure/conf.hpp"
 
using namespace logif;
 
namespace ffwrapper{
    
FilterVideo::FilterVideo()
:graph_(NULL)
,src_(NULL)
,out_(NULL)
,func_out_filtered_frame_(nullptr)
{}
 
FilterVideo::~FilterVideo()
{
    if (graph_)
    {
        for (int i = 0; i < graph_->nb_filters; ++i)
        {
            avfilter_free(graph_->filters[i]);
        }
        avfilter_graph_free(&graph_);
        graph_ = NULL;
    }
    
}
 
FilterVideo::FilterVideo(const AVCodecContext *dec, const AVCodecContext *enc, 
    const char *filter_spec, FUNC_OUT_FILTERED_FRAME cb)
:graph_(NULL)
,src_(NULL)
,out_(NULL)
,func_out_filtered_frame_(cb)
{
    bool flag = initFilterGraph(dec, enc);
    flag = configureFilterGraph(filter_spec) && flag;
    if(!flag){
        throw std::runtime_error("FilterVideo Init Failed!");
    }
}
///////////////////////////////////////////////////////////////
 
bool FilterVideo::initFilterGraph(const AVCodecContext *dec_ctx, const AVCodecContext *enc_ctx){
    graph_ = avfilter_graph_alloc();
    if(!graph_){
        logIt("alloc filter graph error"); 
 
        return false;
    }
 
    AVFilter* buffersrc = (AVFilter*)avfilter_get_by_name("buffer");
    AVFilter* buffersink = (AVFilter*)avfilter_get_by_name("buffersink");
    if(!buffersrc || !buffersink){
        logIt("can't get \"buffer/sink\" filters"); 
 
        return false;
    }
 
    char args[512];
 
    snprintf(args, sizeof(args),
            "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
            dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
            dec_ctx->time_base.num, dec_ctx->time_base.den,
            dec_ctx->sample_aspect_ratio.num,
            dec_ctx->sample_aspect_ratio.den);
    printf("%s\n", args);
    int ret = avfilter_graph_create_filter(&src_, buffersrc, "in",
                                           args, NULL, graph_);
    if(ret < 0)
    {
        logIt("create in filter failed:%s",
                getAVErrorDesc(ret).c_str()); 
 
        return false;
    } 
 
    ret = avfilter_graph_create_filter(&out_, buffersink, "out",
                                        NULL, NULL, graph_);
    if(ret < 0)
    {
        logIt("create out filter failed:%s",
                getAVErrorDesc(ret).c_str());
        return false;
    }
    
    
    ret = av_opt_set_bin(out_, "pix_fmts",(uint8_t *)&enc_ctx->pix_fmt, 
                        sizeof(enc_ctx->pix_fmt),AV_OPT_SEARCH_CHILDREN);
    if(ret < 0)
    {
        logIt("set out filter opt failed:%s",
                getAVErrorDesc(ret).c_str());
        return false;
    }
 
    return true;
}
 
bool FilterVideo::configureFilterGraph(const char *filter_spec){
 
    AVFilterInOut *outputs = avfilter_inout_alloc();
    AVFilterInOut *inputs  = avfilter_inout_alloc();
 
    if(!outputs || !inputs){
        logIt("create filter in/out failed:{}");
        return false;
    }
 
    /* Endpoints for the filter graph. */
    outputs->name       = av_strdup("in");
    outputs->filter_ctx = src_;
    outputs->pad_idx    = 0;
    outputs->next       = NULL;
    
    inputs->name       = av_strdup("out");
    inputs->filter_ctx = out_;
    inputs->pad_idx    = 0;
    inputs->next       = NULL;
 
    int ret = avfilter_graph_parse_ptr(graph_, filter_spec,
                                       &inputs, &outputs, NULL);
    if(ret < 0){
        logIt("parse filter graph failed:%s",
                getAVErrorDesc(ret).c_str());
        return false;
    }
 
    ret = avfilter_graph_config(graph_, NULL);
    if(ret < 0){
        logIt("config filter graph failed:%s",
                getAVErrorDesc(ret).c_str());
        return false;
    }
 
    avfilter_inout_free(&inputs);
    avfilter_inout_free(&outputs);
 
    return true;
}
 
static int64_t frame_pts = 0;
bool FilterVideo::filterFrame(AVFrame *frame){
 
    // printf("pre filter pts : %lld\n", frame->pts);
    // frame->pts = frame->best_effort_timestamp;
    // printf("post filter pts : %lld\n", frame->pts);
 
    frame->pts = frame_pts++;
 
    int ret = av_buffersrc_add_frame_flags(in(),frame,
                                        AV_BUFFERSRC_FLAG_PUSH);
    if(ret < 0){
        logIt("push frame to filter failed:%s",
                getAVErrorDesc(ret).c_str()); 
 
        return false;
    }
 
    AVFrame *filtered_frame;
 
    bool flag = true;
 
    while(true){
        filtered_frame = av_frame_alloc();
        if(!filtered_frame){
            logIt("alloc filtered frame failed");
 
            break;
        }
 
        ret = av_buffersink_get_frame(out(),filtered_frame);
        if(ret < 0){
            if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){
                // printf("need more frame\n");
                ret = 0;
            }else{
                logIt("pull frame from filter failed:%s",
                            getAVErrorDesc(ret).c_str()); 
            }
            av_frame_free(&filtered_frame);
 
            break;
        }
 
        if(func_out_filtered_frame_){
            flag = func_out_filtered_frame_(filtered_frame);
        }
 
        av_frame_free(&filtered_frame);
 
    }
    return flag;
}
 
}