video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2023-12-26 18a05d269516a5e33d8460291c2f93e73d95adce
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
#include "PicData.hpp"
 
#include <stdexcept>
 
extern "C" {
#include <libavcodec/avcodec.h>
}
 
#include "../log/log.hpp"
using namespace logif;
 
namespace ffwrapper{
    PicData::PicData(const int width, const int height, const int pix_fmt)
    :frame_(nullptr)
    ,data_(nullptr)
    ,data_size_(0)
    ,pix_fmt_(pix_fmt)
    ,width_(width)
    ,height_(height)
    {
        bool flag = init_frame();
        flag = init_AVPicture() && flag;
        if(!flag){
            throw std::runtime_error("init PicData error");
        }
    }
 
 
    PicData::~PicData(){
        free_frame();
    }
 
    bool PicData::init_frame(){
        frame_ = av_frame_alloc();
 
        return !!frame_;
    }
 
    void PicData::free_frame(){
        avpicture_free((AVPicture *)frame_);
        frame_ = NULL;
        data_ = NULL;
 
    }
 
    bool PicData::init_AVPicture(){
        data_size_ = avpicture_get_size((AVPixelFormat)pix_fmt_,
                                         width_, height_);
        if(data_size_ <= 0){
            logIt("avpicture_get_size error");
            return false;
        }
        data_ = (uint8_t*)av_malloc(data_size_);
        const int ret = avpicture_fill((AVPicture *)frame_, data_, 
            (AVPixelFormat)pix_fmt_, width_, height_);
 
        if(ret < 0){
            logIt("avpicture_fill error");
            return false;
 
        }
 
        return true;
    }
}