video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-27 762251329bca80422a8e65d0cdb36e86e84883cf
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "FormatOut.hpp"
 
#include <stdexcept>
#include <vector>
 
#include <unistd.h>
#include <sys/time.h>
 
extern "C"{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
}
 
#include "../log/log.hpp"
#include "../configure/conf.hpp"
 
#include "../property/VideoProp.hpp"
 
#include "../../common/gpu/info.h"
 
using namespace logif;
 
namespace ffwrapper{
    FormatOut::FormatOut()
    :ctx_(NULL)
    ,v_idx_(-1)
    ,a_idx_(-1)
    ,enc_ctx_(NULL)
    ,sync_opts_(0)
    ,record_(false)
    ,fps_(0.0f)
    ,format_name_("mp4")
    ,in_v_stream_(NULL)
    ,in_a_stream_(NULL)
    {}
 
    FormatOut::~FormatOut()
    {
        clear();
    }
 
    void FormatOut::clear(){
 
        if(enc_ctx_){
            avcodec_close(enc_ctx_);
        }
        if(ctx_){
            closeResource();
 
            avformat_free_context(ctx_);
            ctx_ = NULL;
        }
        sync_opts_ = 0;
 
    }
 
    FormatOut::FormatOut(VideoProp &prop,
        const char *filename, char *format_name/*=NULL*/)
        :FormatOut(){
 
        bool flag = true;
        for (int i = 0; i < 2; ++i)
        {
            flag = open(filename, format_name);
            if(flag){
                flag = openCodec(prop);
                if(!prop.gpuAccl()){
                    break;
                }
                if(!flag){
                    prop.gpu_acc_ = false;
                    clear();
                }else{
                    break;
                }
            }
        }
        
        if(!flag){
            throw std::runtime_error("FormatOut Init Failed!");
        }
 
        fps_ = prop.fps_;
    }
///////////////////////////////////////////////////////////////////////
    bool FormatOut::open(const char *filename, const char *format_name){
 
        const int ret = avformat_alloc_output_context2(&ctx_, NULL, format_name, filename);
        if(ret < 0){
            logIt("open %s failed:%s",filename,
                    getAVErrorDesc(ret).c_str()); 
 
            return false;
        }
        
        return true;
    }
 
    void FormatOut::configEncoder(VideoProp &prop){
 
        enc_ctx_->codec_id = AV_CODEC_ID_H264;
        enc_ctx_->codec_type = AVMEDIA_TYPE_VIDEO;
        enc_ctx_->height = (prop.height_ & 0x01) ? prop.height_-1 : prop.height_;
        enc_ctx_->width = (prop.width_ & 0x01) ? prop.width_ - 1 : prop.width_;
 
        enc_ctx_->sample_aspect_ratio = prop.sample_aspect_ratio_;
        
        // v_s_->time_base.num = in->time_base.num;
        // v_s_->time_base.den = in->time_base.den;
        enc_ctx_->time_base.num = 1;
        enc_ctx_->time_base.den = prop.fps_;
    
        
        enc_ctx_->gop_size = 12;
        enc_ctx_->pix_fmt = AV_PIX_FMT_YUV420P;
        enc_ctx_->max_b_frames = 0;
 
        
        enc_ctx_->flags |= AV_CODEC_FLAG_QSCALE;
        enc_ctx_->bit_rate = prop.bit_rate_ * 1000;
        if(!prop.gpuAccl()){
            enc_ctx_->bit_rate *= 0.75;
        }
        enc_ctx_->rc_min_rate = enc_ctx_->bit_rate / 2;
        enc_ctx_->rc_max_rate = enc_ctx_->bit_rate * 2 - enc_ctx_->rc_min_rate;
        enc_ctx_->rc_buffer_size = enc_ctx_->rc_max_rate * 10;
    }
 
    bool FormatOut::openCodec(VideoProp &prop){
 
        AVOutputFormat *ofmt = ctx_->oformat;
        AVCodecID codec_id = AV_CODEC_ID_H264;
        AVCodec *codec = NULL;
 
        if(prop.gpuAccl()){
            codec = avcodec_find_encoder_by_name("h264_nvenc");
            if(!codec){
                logIt("no support nvenc\n");
                prop.gpu_acc_ = false;
            }
        }
 
        if(!prop.gpuAccl()){
            codec = avcodec_find_encoder(codec_id);
        }
        
        if(!codec){
            logIt("can't find encoder %s", codec->name); 
            return false;
        }
        logIt("use encoder %s", codec->name);
 
        AVStream *v = avformat_new_stream(ctx_, codec);
        v_idx_ = 0;
 
        enc_ctx_ = avcodec_alloc_context3(codec);
 
        configEncoder(prop);
        
        if(prop.gpuAccl()){
            av_opt_set(enc_ctx_->priv_data, "preset", "llhp", 0);
            int idle_gpu = gpu::getGPU(120);
            if (prop.gpu_index_ > -1){
                idle_gpu = prop.gpu_index_;
            }
            if(idle_gpu < 0){
                logIt("NO GPU RESOURCE TO ENCODE");
                return false;
            }
            av_opt_set_int(enc_ctx_->priv_data, "gpu", idle_gpu, 0);
            printf("ENCODER USE GPU %d\n", idle_gpu);
        }else{
            av_opt_set(enc_ctx_->priv_data, "preset", prop.preset().c_str(), 0);
        }
        av_opt_set(enc_ctx_->priv_data, "tune", "zerolatency", 0);
        av_opt_set(enc_ctx_->priv_data, "profile", "baseline", 0);
    
        int err =avcodec_open2(enc_ctx_, codec, NULL);
        if( err< 0)
        {
            logIt("can't open output codec: %s", getAVErrorDesc(err).c_str());
            return false;
        }
        err = avcodec_parameters_from_context(v->codecpar, enc_ctx_);
        if (err < 0) {
            logIt("can't avcodec_parameters_from_context: %s", getAVErrorDesc(err).c_str());
            return false;
        }
        ofmt->video_codec = codec_id;
        if(ofmt->flags & AVFMT_GLOBALHEADER)
        {
            enc_ctx_->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
        }
 
 
        return true;
 
    }
    
    AVStream *FormatOut::getStream(){
        if (v_idx_ == -1) return NULL;
        return ctx_->streams[v_idx_];
    }
 
    const AVCodecContext *FormatOut::getCodecContext()const{
        return enc_ctx_;
    }
 
    int FormatOut::encode(AVPacket *pkt, AVFrame *frame){
 
        AVStream *out = getStream();
    
        frame->quality = enc_ctx_->global_quality;
        frame->pict_type = AV_PICTURE_TYPE_NONE;
    
        pkt->data = NULL;
        pkt->size = 0;
    
        int ret = avcodec_send_frame(enc_ctx_, frame);
        if(ret < 0){
            logIt("avcodec_send_frame failed : %s", getAVErrorDesc(ret).c_str());   
            return -1;
        }
    
        while(ret >= 0){
            ret = avcodec_receive_packet(enc_ctx_, pkt);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            }else if (ret < 0) {
                logIt("avcodec_receive_packet : %s", getAVErrorDesc(ret).c_str()); 
                return -1;
            }else{
                if(pkt->pts == AV_NOPTS_VALUE 
                    && !(enc_ctx_->codec->capabilities & AV_CODEC_CAP_DELAY))
                {
                    pkt->pts = sync_opts_++;
                }
                av_packet_rescale_ts(pkt, enc_ctx_->time_base, out->time_base);
                // printf("pkt pts: %lld\n", pkt.pts);
                return 0;
            }
            
        }
 
        return -2;
    }
 
//////////////////////////////////////////////////////////////////////////
    FormatOut::FormatOut(const double fps, const char *format_name)
    :FormatOut(){
 
        format_name_ = format_name;
 
        fps_ = fps;
    }
 
    bool FormatOut::openResource(const char *filename, const int flags){
        if((ctx_->oformat->flags & AVFMT_NOFILE) != AVFMT_NOFILE){
            
            const int err = avio_open2(&ctx_->pb, filename, flags, NULL, NULL);
            if(err < 0)
            {
                logIt("can't save to %s error:%s",filename,
                        getAVErrorDesc(err).c_str()); 
 
                return false;
            }
            strcpy(&ctx_->filename[0], filename);
        }
        return true;
    }
    bool FormatOut::closeResource(){
        if(record_){
            return avio_close(ctx_->pb) == 0;
        }
        return true;
    }
 
    bool FormatOut::addStream(AVStream *s){
        AVStream *in_stream = s;
        
        AVStream *out_stream = avformat_new_stream(ctx_, in_stream->codec->codec);
        if(!out_stream)
        {
            logIt("Failed allocating output stream.\n");
            return false;
        }
        //将输出流的编码信息复制到输入流
        auto ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
        if(ret<0)
        {
            logIt("Failed to copy context from input to output stream codec context\n");
            return false;
        }
        out_stream->codecpar->codec_tag = out_stream->codec->codec_tag = 0;
        
        if(ctx_->oformat->flags & AVFMT_GLOBALHEADER)
            out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
        
        return true;
    }
 
    bool FormatOut::copyCodecFromIn(AVStream *v, AVStream *a){
        if (v){
            v_idx_ = 0;
            in_v_stream_ = v;
 
            if (!addStream(v)){
                logIt("format out add video stream error");
                return false;
            }else{
                logIt("copy video from instream");
            }
        }
        if (a){
            a_idx_ = 1;
            in_a_stream_ = a;
            if (!addStream(a)){
                logIt("format out add audio stream error");
                return false;
            }else{
                logIt("copy audio from instream");
            }
        }
        return true;
    }
 
    bool FormatOut::JustWriter(AVStream *v, AVStream *a, const char *filename){
        if(ctx_){
            clear();
        }
 
        bool flag = open(NULL, format_name_.c_str());
 
        flag = copyCodecFromIn(v, a) && flag;
        if(!flag){
            logIt("FormatOut JustWriter error from in");
            return false;
        }
 
        flag = openResource(filename, 2);
        
        if(flag){
            AVDictionary *avdic = NULL;
            char option_key[]="movflags";
            char option_value[]="frag_keyframe+empty_moov";
            av_dict_set(&avdic,option_key,option_value,0);
            flag = writeHeader(&avdic);
            av_dict_free(&avdic);
        }
        
        return flag;
    }
    
    bool FormatOut::EncodeWriter(const char *filename){
        
        auto flag = openResource(filename, 2);
        if(flag){
            AVDictionary *avdic = NULL;
            char option_key[]="movflags";
            char option_value[]="frag_keyframe+empty_moov";
            av_dict_set(&avdic,option_key,option_value,0);
 
            flag = writeHeader(&avdic);
            av_dict_free(&avdic);        
        }
        return false;
    }
 
    bool FormatOut::endWriter(){
 
        auto flag =  writeTrailer();
        closeResource();
        record_ = false;
 
        return flag;
    }
 
    const char* FormatOut::getFileName() const{
        return ctx_->filename;
    }
//////////////////////////////////////////////////////////////////////////////////////////////////
    bool FormatOut::writeHeader(AVDictionary **options/* = NULL*/){
 
        const int ret = avformat_write_header(ctx_, options);
        if(ret < 0){
            logIt("write header to file failed:%s",
                    getAVErrorDesc(ret).c_str()); 
            return false;
        }
        
        record_ = true;
        return true;
    }
 
    void FormatOut::adjustPTS(AVPacket *pkt, const int64_t &frame_cnt){
        if (pkt->stream_index >= ctx_->nb_streams){
            logIt("adjustPTS pkt stream index too much");
            return;
        }
        
        if (!in_a_stream_){
            int64_t time_stamp = frame_cnt;
        
            pkt->pos = -1;  
            pkt->stream_index = 0;
            //Write PTS
            AVRational time_base = getStream()->time_base;
 
            AVRational time_base_q = { 1, AV_TIME_BASE };
            //Duration between 2 frames (us)
            // int64_t calc_duration = (double)(AV_TIME_BASE)*(1 / fps_);  //内部时间戳
            int64_t calc_duration = (int64_t)(AV_TIME_BASE / fps_);  //内部时间戳
            //Parameters
            pkt->pts = av_rescale_q(time_stamp*calc_duration, time_base_q, time_base);
            pkt->dts = pkt->pts;
            pkt->duration = av_rescale_q(calc_duration, time_base_q, time_base); 
            return;
        }
 
        AVStream *in_stream,*out_stream;
        int out_idx = -1;
        std::vector<AVStream*> in_streams{in_v_stream_, in_a_stream_};
        for (auto i : in_streams){
            if (i->index == pkt->stream_index){
                if (i->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
                    out_idx = v_idx_;
                    in_stream = i;
                    break;
                }else if (i->codecpar->codec_type == AVMEDIA_TYPE_AUDIO){
                    in_stream = i;
                    out_idx = a_idx_;
                    break;
                }
            }
        }
        if (out_idx == -1) return;
 
        out_stream = ctx_->streams[out_idx];
        pkt->stream_index = out_idx;
        
        // logIt("BEFORE in stream timebase %d:%d, out timebase %d:%d, 
        //         pts: %lld, dts: %lld, duration: %lld", 
        //     in_stream->time_base.num, in_stream->time_base.den,
        //     out_stream->time_base.num, out_stream->time_base.den,
        //     pkt->pts, pkt->dts, pkt->duration);
 
        //转换 PTS/DTS 时序
        pkt->pts = av_rescale_q_rnd(pkt->pts,in_stream->time_base,out_stream->time_base,(enum AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
        pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base, out_stream->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
        pkt->duration = av_rescale_q(pkt->duration, in_stream->time_base, out_stream->time_base); 
        pkt->pos = -1;  
 
        // logIt("AFTER stream %d, pts: %lld, dts: %lld, duration: %lld", 
        // pkt->stream_index, pkt->pts, pkt->dts, pkt->duration);
    }
 
    bool FormatOut::writeFrame(AVPacket *pkt, const int64_t &frame_cnt,
                              bool interleaved/* = true*/){
 
        adjustPTS(pkt, frame_cnt);
        auto ret = writeFrame2(pkt, interleaved);
        if (!ret){
            logIt("write to file failed, pkt.pts: %lld, dts: %lld, frame count: %d",
                    pkt->pts, pkt->dts, frame_cnt);
        }
        return ret;
    }
 
    bool FormatOut::writeFrame2(AVPacket *pkt, bool interleaved){
        
        int ret = 0;
        if(interleaved){
            ret = av_interleaved_write_frame(ctx_, pkt);
        }else{
            // returns 1 if flushed and there is no more data to flush
            ret = av_write_frame(ctx_, pkt);
        }
    
        if(ret < -22 || ret == 0){
            return true;
        }
 
        return false;
    }
 
    bool FormatOut::writeTrailer(){
        const int ret = av_write_trailer(ctx_);
        if(ret != 0)
        {
            logIt("write trailer to file failed:%s",
                    getAVErrorDesc(ret).c_str()); 
            return false;
        }
        
        return true;
    }
    
}