-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_match_ff.cpp
349 lines (295 loc) · 10.5 KB
/
vm_match_ff.cpp
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
#include "vm_match_ff.h"
extern "C"{
#include <libavutil/opt.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/buffer.h>
#include <libavutil/imgutils.h>
}
#include <format>
#include <map>
#include <algorithm>
#include <thread>
//#include <chrono>
#include "vm_option_ff.h"
#include "vm_log.h"
namespace vm_match_ff
{
class AV_map: public std::map<fnum, AVFrame*>{
public:
using std::map<fnum, AVFrame*>::map;
void clear(){
for(std::pair<const fnum, AVFrame*>& pair : *this)
av_frame_free(&pair.second);
std::map<fnum, AVFrame*>::clear();
}
void erase(std::map<fnum, AVFrame*>::iterator it){
av_frame_free(&it->second);
std::map<fnum, AVFrame*>::erase(it);
}
void erase(fnum key) {
auto it = this->find(key);
if(it != this->end())
erase(it);
}
};
fnum* match_frame_list;
AV_map frame_buffer_map;
fnum frame_buffer_back, buffer_read_pos;
fnum video_frame_num_1;
AVFilterGraph* ssim_graph = avfilter_graph_alloc();
bool can_not_flush_buffer;
// 自动转换pix_fmt并缩放
AVFrame* _auto_pix_fmt_process(AVFrame*& frame){
AVFrame* new_frame = av_frame_alloc();
new_frame->width = vm_option_ff::new_width;
new_frame->height = vm_option_ff::new_height;
new_frame->format = AV_PIX_FMT_GRAY8;
// 为输出帧分配缓冲区
if(av_image_alloc(new_frame->data, new_frame->linesize, vm_option_ff::new_width, vm_option_ff::new_height, AVPixelFormat::AV_PIX_FMT_GRAY8, 32) < 0){
av_frame_free(&new_frame);
vm_log::errore("vm_match_ff::_auto_pix_fmt_process: av_image_alloc: error");
}
SwsContext* sws_ctx = sws_getContext(
frame->width, frame->height, static_cast<AVPixelFormat>(frame->format),
vm_option_ff::new_width, vm_option_ff::new_height, AVPixelFormat::AV_PIX_FMT_GRAY8,
SWS_POINT, NULL, NULL, NULL
);
if(!sws_ctx){
av_frame_free(&new_frame);
vm_log::errore("vm_match_ff::_auto_pix_fmt_process: sws_getContext: error");
}
// 执行转换
sws_scale(sws_ctx,
frame->data, frame->linesize,
0, frame->height,
new_frame->data, new_frame->linesize);
sws_freeContext(sws_ctx);
return new_frame;
}
// 读取 video 2 的下一帧
int8_t _read_frame_2(fnum frame_num){
// 读取
frame_buffer_map[frame_num] = av_frame_alloc();
bool isread = false;
AVPacket packet;
while(av_read_frame(vm_option_ff::formatContext_2, &packet) >= 0){
if(packet.stream_index == vm_option_ff::video_stream_index_2)
if(avcodec_send_packet(vm_option_ff::codecContext_2, &packet) == 0)
while(avcodec_receive_frame(vm_option_ff::codecContext_2, frame_buffer_map[frame_num]) == 0){
isread = true;
break;
}
av_packet_unref(&packet);
if(isread)
break;
}
// 包读完了读缓存,防止缓存剩帧没读
if(!isread)
if(avcodec_send_packet(vm_option_ff::codecContext_2, nullptr) == 0){
// 强制写入所有帧到buffer
while(avcodec_receive_frame(vm_option_ff::codecContext_2, frame_buffer_map[frame_num]) >= 0){
isread = true;
if(++frame_num < vm_option_ff::frame_count_2)
frame_buffer_map[frame_num] = av_frame_alloc();
else{
can_not_flush_buffer = true;
return -1;
}
}
}
if(!isread){
frame_buffer_map.erase(frame_num);
vm_log::error("vm_match_ff::_read_frame_2: Failed to read frame in video 2");
return 1;
}
return 0;
}
void _flush_buffer(){
if(can_not_flush_buffer)
return;
// 读取新一段buffer
if(video_frame_num_1+vm_option_ff::frame_forward >= buffer_read_pos){
for(fnum i = buffer_read_pos;
i < buffer_read_pos + vm_option_ff::frame_forward && i < vm_option_ff::frame_count_2;
++i){
switch(_read_frame_2(i))
{
case 1:
vm_log::error(std::format("vm_match_ff::_flush_buffer: Get frame_2 error in frame {0}", i));
break;
case -1:
buffer_read_pos = vm_option_ff::frame_count_2;
return;
}
}
buffer_read_pos += vm_option_ff::frame_forward;
}
// 移除超出的旧帧
for(auto it = frame_buffer_map.begin(); it != frame_buffer_map.end(); ++it){
if(it->first < std::min(vm_option_ff::frame_count_2, video_frame_num_1-static_cast<fnum>(vm_option_ff::frame_forward)))
frame_buffer_map.erase(it);
else break;
}
}
// 初始化滤镜图并配置SSIM滤镜
void init_ssim_filter_graph(){
if(!ssim_graph)
vm_log::errore("Failed to create filter graph");
// 创建buffer源(主画面)
AVFilterContext* buffersrc_ctx_main = nullptr;
char args_main[512];
snprintf(args_main, sizeof(args_main), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d",
vm_option_ff::new_width, vm_option_ff::new_height, AV_PIX_FMT_GRAY8, 1, 24);
const AVFilter* buffersrc = avfilter_get_by_name("buffer");
if(avfilter_graph_create_filter(&buffersrc_ctx_main, buffersrc, "src_main", args_main, nullptr, ssim_graph) < 0)
vm_log::errore("Failed to create buffer source for main");
// 创建buffer源(参考画面)
AVFilterContext* buffersrc_ctx_ref = nullptr;
char args_ref[512];
snprintf(args_ref, sizeof(args_ref), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d",
vm_option_ff::new_width, vm_option_ff::new_height, AV_PIX_FMT_GRAY8, 1, 24);
if(avfilter_graph_create_filter(&buffersrc_ctx_ref, buffersrc, "src_ref", args_ref, nullptr, ssim_graph) < 0)
vm_log::errore("Failed to create buffer source for reference");
// 创建ssim滤镜
const AVFilter* ssim = avfilter_get_by_name("ssim");
AVFilterContext* ssim_ctx = nullptr;
if(avfilter_graph_create_filter(&ssim_ctx, ssim, "ssim", nullptr, nullptr, ssim_graph) < 0)
vm_log::errore("Failed to create ssim filter");
// 创建buffer汇
const AVFilter* buffersink = avfilter_get_by_name("buffersink");
AVFilterContext* buffersink_ctx = nullptr;
if(avfilter_graph_create_filter(&buffersink_ctx, buffersink, "sink", nullptr, nullptr, ssim_graph) < 0)
vm_log::error("Failed to create buffer sink");
// 链接滤镜
if(avfilter_link(buffersrc_ctx_main, 0, ssim_ctx, 0) < 0 ||
avfilter_link(buffersrc_ctx_ref, 0, ssim_ctx, 1) < 0 ||
avfilter_link(ssim_ctx, 0, buffersink_ctx, 0) < 0)
vm_log::errore("Failed to link filters");
// 配置滤镜图
if(avfilter_graph_config(ssim_graph, nullptr) < 0)
vm_log::errore("Failed to configure filter graph");
}
double compare_ssim(AVFrame* frame_1, AVFrame* frame_2){
if(!frame_1){
vm_log::error("vm_match_ff::compare_ssim: frame_1 is nullptr");
return -1;
}
if(!frame_2){
vm_log::error("vm_match_ff::compare_ssim: frame_2 is nullptr");
return -1;
}
// 获取滤镜图的输入和输出上下文
AVFilterContext* buffersrc_ctx_main = avfilter_graph_get_filter(ssim_graph, "src_main");
AVFilterContext* buffersrc_ctx_ref = avfilter_graph_get_filter(ssim_graph, "src_ref");
AVFilterContext* buffersink_ctx = avfilter_graph_get_filter(ssim_graph, "sink");
// 将帧发送到滤镜图的输入端
av_buffersrc_add_frame_flags(buffersrc_ctx_main, frame_1, AV_BUFFERSRC_FLAG_PUSH);
av_buffersrc_add_frame_flags(buffersrc_ctx_ref, frame_2, AV_BUFFERSRC_FLAG_PUSH);
// 从滤镜图的输出端获取 SSIM 值
AVFrame* frame_out = av_frame_alloc();
if(av_buffersink_get_frame(buffersink_ctx, frame_out) < 0){
vm_log::error("vm_match_ff::compare_ssim: av_buffersink_get_frame: error");
av_frame_free(&frame_out);
return -1;
}
double ssim_value = 0;
if(frame_out->metadata) {
AVDictionaryEntry *tag = av_dict_get(frame_out->metadata, "lavfi.ssim.Y", NULL, 0);
if(tag)
ssim_value = atof(tag->value);
else vm_log::error("vm_match_ff::compare_ssim: av_dict_get(frame_out->metadata ...) is NULL");
}
else vm_log::error("vm_match_ff::compare_ssim: frame_out->metadata is NULL");
av_frame_free(&frame_out);
av_freep(&frame_2->data[0]);
av_frame_free(&frame_2);
if(vm_option_ff::debug)
vm_log::info(std::format("{0} SSIM: {1}", video_frame_num_1, ssim_value));
return ssim_value;
}
bool frame_cmp(AVFrame*& frame_1, AVFrame*& frame_2){
bool is_pass = compare_ssim(frame_1, frame_2) >= vm_option_ff::ssim_threshold;
if(is_pass){
av_freep(&frame_1->data[0]);
av_frame_free(&frame_1);
}
return is_pass;
}
void do_match(){
match_frame_list = new fnum[vm_option_ff::frame_count_1];
buffer_read_pos = video_frame_num_1 = 0;
can_not_flush_buffer = false;
AVFrame *frame_1 = av_frame_alloc(), *frame_2 = av_frame_alloc();
AVFrame *frame_1_resize, *frame_2_resize;
AVPacket packet_1;
// 配置SSIM滤镜
init_ssim_filter_graph();
// 打印进度子线程
std::thread([&](){
fnum denominator = vm_option_ff::frame_count_1-1;
while(true){
vm_log::change_title(std::format(R"({0} / {1} {2:.1f}%)",
video_frame_num_1, denominator, 100.0*video_frame_num_1/denominator));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}).detach();
// 读取并对比
while(av_read_frame(vm_option_ff::formatContext_1, &packet_1) >= 0){
if(packet_1.stream_index == vm_option_ff::video_stream_index_1){
if(avcodec_send_packet(vm_option_ff::codecContext_1, &packet_1) == 0){
while(avcodec_receive_frame(vm_option_ff::codecContext_1, frame_1) == 0){
// 执行
//vm_log::info(std::format("{0}",video_frame_num_1));
frame_1_resize = _auto_pix_fmt_process(frame_1);
_flush_buffer();
bool is_not_finded = true;
for(auto p : frame_buffer_map){
frame_2 = p.second;
frame_2_resize = _auto_pix_fmt_process(frame_2);
if(frame_cmp(frame_1_resize, frame_2_resize)){
frame_buffer_map.erase(p.first);
match_frame_list[video_frame_num_1] = p.first;
is_not_finded = false;
break;
}
}
if(is_not_finded)
match_frame_list[video_frame_num_1] = -1;
++video_frame_num_1;
}
}
}
av_packet_unref(&packet_1);
}
// 发空包,以防缓冲区中仍有帧
if(avcodec_send_packet(vm_option_ff::codecContext_1, nullptr) == 0) {
while(avcodec_receive_frame(vm_option_ff::codecContext_1, frame_1) >= 0) {
// 执行
//vm_log::info(std::format("{0}", video_frame_num_1));
frame_1_resize = _auto_pix_fmt_process(frame_1);
bool is_not_finded = true;
for(auto p : frame_buffer_map){
frame_2 = p.second;
frame_2_resize = _auto_pix_fmt_process(frame_2);
if(frame_cmp(frame_1_resize, frame_2_resize)){
frame_buffer_map.erase(p.first);
match_frame_list[video_frame_num_1] = p.first;
is_not_finded = false;
break;
}
}
if(is_not_finded)
match_frame_list[video_frame_num_1] = -1;
++video_frame_num_1;
}
}
// 清理
av_frame_free(&frame_1);
avformat_close_input(&vm_option_ff::formatContext_1);
avformat_close_input(&vm_option_ff::formatContext_2);
avcodec_free_context(&vm_option_ff::codecContext_1);
avcodec_free_context(&vm_option_ff::codecContext_2);
}
}