video analysis2.0拆分,ffmpeg封装go接口库
up
zhangmeng
2019-12-02 4514810002f2d5ba98ed886f3dae28e2e8aaf448
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
#ifdef __cplusplus
extern "C"{
#endif
 
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
 
#include "libcffmpeg.h"
 
#ifdef __cplusplus
}
#endif
 
#define release_if_err(fn, lib) do{if(!fn){dlclose(lib); return NULL;}}while(0)
 
libcffmpeg init_libcffmpeg(const char *so_file){
    libcffmpeg lib = dlopen(so_file, RTLD_LAZY);
    if(lib){
 
        fn_destroy = (lib_cffmpeg_destroy)dlsym(lib, "c_ffmpeg_destroy");
        release_if_err(fn_destroy, lib);
 
    }else{
        printf("dlopen - %s\n", dlerror());  
    }
    return lib;
}
 
void release_libcffmpeg(libcffmpeg lib){
    if(lib){
        dlclose(lib);
    }
}
 
cffmpeg wrap_fn_create(void *lib){
    if (!fn_create){
        fn_create = (lib_cffmpeg_create)dlsym(lib, "c_ffmpeg_create");
        release_if_err(fn_create, lib);
    }
 
    return fn_create();
}
 
cffmpeg wrap_fn_create2(void *lib, const char *logfile){
    if (!fn_create2){
        fn_create2 = (lib_cffmpeg_create)dlsym(lib, "c_ffmpeg_create2");
        release_if_err(fn_create2, lib);
    }
    return fn_create2(logfile);
}
 
void wrap_fn_destroy(void *lib, const cffmpeg h){
    fn_destroy(h);
}
 
void wrap_fn_run(void *lib, const cffmpeg h, const char* input){
    if (!fn_run){
        fn_run = (lib_cffmpeg_run)dlsym(lib, "c_ffmpeg_run");
        if (!fn_run) return;
    }
 
    fn_run(h, input);
}
 
int wrap_fn_fps(void *lib, const cffmpeg h){
    if(!fn_fps){
        fn_fps = (lib_cffmpeg_fps)dlsym(lib, "c_ffmpeg_get_fps");
        if (!fn_fps) return 25;
    }
    return fn_fps(h);
}
 
void wrap_fn_run_gb28181(void *lib, const cffmpeg h){
    if (!fn_gb28181){
        fn_gb28181 = (lib_cffmpeg_gb28181)dlsym(lib, "c_ffmpeg_run_gb28181");
        if (!fn_gb28181) return;
    }
    fn_gb28181(h);
}
 
void wrap_fn_use_cpu(void *lib, const cffmpeg h){
    if (!fn_cpu){
        fn_cpu = (lib_cffmpeg_cpu)dlsym(lib, "c_ffmepg_use_cpu");
        if (!fn_cpu) return;
    }
    fn_cpu(h);
}
 
void wrap_fn_recorder(void *lib, const cffmpeg h, const char* id, const char* dir, const int64_t fid, int mind, int maxd, int audio){
    if (!fn_recorder){
        fn_recorder = (lib_cffmpeg_recorder)dlsym(lib, "c_ffmpeg_build_recorder");
        if (!fn_recorder) return;
    }
    fn_recorder(h, id, dir, fid, mind, maxd, audio);
}
 
void wrap_fn_rec_duration(void *lib, const cffmpeg h, const int min, const int max){
    if (!fn_rec_duration){
        fn_rec_duration = (lib_cffmpeg_rec_duration)dlsym(lib, "c_ffmpeg_set_record_duration");
        if (!fn_rec_duration) return;
    }
    fn_rec_duration(h, min, max);
}
 
void wrap_fn_fire_recorder(void *lib, const cffmpeg h, const char* sid, const int64_t id){
    if (!fn_fire_recorder){
        fn_fire_recorder = (lib_cffmpeg_fire_recorder)dlsym(lib, "c_ffmpeg_fire_recorder");
        if (!fn_fire_recorder) return;
    }
    fn_fire_recorder(h, sid, id);
}
 
void wrap_fn_info_recorder(void *lib, const cffmpeg h, int* index, char** recid, int* recidLen, char** fpath, int* pathLen){
    if (!fn_info_recorder){
        fn_info_recorder = (lib_cffmpeg_info_recorder)dlsym(lib, "c_ffmpeg_get_info_recorder");
        if (!fn_info_recorder) return;
    }
    return fn_info_recorder(h, index, recid, recidLen, fpath, pathLen);
}
 
void wrap_fn_decoder(void *lib, const cffmpeg h){
    if (!fn_decoder){
        fn_decoder = (lib_cffmpeg_decoder)dlsym(lib, "c_ffmpeg_build_decoder");
        if (!fn_decoder) return;
    }
    fn_decoder(h);
}
 
void* wrap_fn_decoder_pic(void *lib, const cffmpeg h, int *wid, int *hei, int *format, int *length, int64_t *id){
    if (!fn_decoder_pic){
        fn_decoder_pic = (lib_cffmpeg_pic)dlsym(lib, "c_ffmpeg_get_pic_decoder");
        release_if_err(fn_decoder_pic, lib);
    }
    return fn_decoder_pic(h, wid, hei, format, length, id);
}
 
void wrap_fn_close_stream(void *lib, const cffmpeg h){
    if (!fn_close_stream){
        fn_close_stream = (lib_cffmpeg_close_stream)dlsym(lib, "c_ffmpeg_close_stream");
        if (!fn_close_stream) return;
    }
    fn_close_stream(h);
}
 
void* wrap_fn_get_avpacket(void *lib, const cffmpeg h, int* size, int* key){
    if(!fn_get_avpacket){
        fn_get_avpacket = (lib_cffmpeg_avpacket)dlsym(lib, "c_ffmpeg_get_avpacket");
        release_if_err(fn_get_avpacket, lib);
    }
    return fn_get_avpacket(h, size, key);
}
 
// for encoder
cencoder wrap_fn_create_encoder(void *lib, const int w, const int h, const int fps, const int br, const int pix_fmt, const int scale_flag, const int gi){
    if (!fn_create_encoder){
        fn_create_encoder = (lib_cffmpeg_create_encoder)dlsym(lib, "c_ffmpeg_create_encoder");
        release_if_err(fn_create_encoder, lib);
    }
    return fn_create_encoder(w, h, fps, br, pix_fmt, scale_flag, gi);
}
 
void wrap_fn_destroy_encoder(void *lib, const cencoder h){
    if (!fn_destroy_encoder){
        fn_destroy_encoder = (lib_cffmpeg_destroy_encoder)dlsym(lib, "c_ffmpeg_destroy_encoder");
        if(!fn_destroy_encoder) return;
    }
    fn_destroy_encoder(h);
}
 
void* wrap_fn_encode(void *lib, cencoder hdl, void *in, const int w, const int h, int *out_size, int *key){
    if (!fn_encode){
        fn_encode = (lib_cffmpeg_encode)dlsym(lib, "c_ffmpeg_encode");
        release_if_err(fn_encode, lib);
    }
    
    uint8_t *out = NULL;
    const int flag = fn_encode(hdl, (uint8_t*)in, w, h, &out, out_size, key);
    if (flag == 0 && out != NULL) {
        return out;
    }
    *out_size = 0;
    *key = 0;
    return NULL;
}