FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_identity.c
Date: 2024-07-17 14:05:47
Exec Total Coverage
Lines: 0 186 0.0%
Functions: 0 15 0.0%
Branches: 0 88 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2021 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Calculate the Identity between two input videos.
24 */
25
26 #include "config_components.h"
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "avfilter.h"
33 #include "drawutils.h"
34 #include "framesync.h"
35 #include "internal.h"
36 #include "scene_sad.h"
37
38 typedef struct IdentityContext {
39 const AVClass *class;
40 FFFrameSync fs;
41 double score, min_score, max_score, score_comp[4];
42 uint64_t nb_frames;
43 int is_rgb;
44 int is_msad;
45 uint8_t rgba_map[4];
46 int max[4];
47 char comps[4];
48 int nb_components;
49 int nb_threads;
50 int planewidth[4];
51 int planeheight[4];
52 uint64_t **scores;
53 unsigned (*filter_line)(const uint8_t *buf, const uint8_t *ref, int w);
54 int (*filter_slice)(AVFilterContext *ctx, void *arg,
55 int jobnr, int nb_jobs);
56 ff_scene_sad_fn sad;
57 } IdentityContext;
58
59 #define OFFSET(x) offsetof(IdentityContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61
62 static unsigned identity_line_8bit(const uint8_t *main_line, const uint8_t *ref_line, int outw)
63 {
64 unsigned score = 0;
65
66 for (int j = 0; j < outw; j++)
67 score += main_line[j] == ref_line[j];
68
69 return score;
70 }
71
72 static unsigned identity_line_16bit(const uint8_t *mmain_line, const uint8_t *rref_line, int outw)
73 {
74 const uint16_t *main_line = (const uint16_t *)mmain_line;
75 const uint16_t *ref_line = (const uint16_t *)rref_line;
76 unsigned score = 0;
77
78 for (int j = 0; j < outw; j++)
79 score += main_line[j] == ref_line[j];
80
81 return score;
82 }
83
84 typedef struct ThreadData {
85 const uint8_t *main_data[4];
86 const uint8_t *ref_data[4];
87 int main_linesize[4];
88 int ref_linesize[4];
89 int planewidth[4];
90 int planeheight[4];
91 uint64_t **score;
92 int nb_components;
93 } ThreadData;
94
95 static
96 int compute_images_msad(AVFilterContext *ctx, void *arg,
97 int jobnr, int nb_jobs)
98 {
99 IdentityContext *s = ctx->priv;
100 ThreadData *td = arg;
101 uint64_t *score = td->score[jobnr];
102
103 for (int c = 0; c < td->nb_components; c++) {
104 const int outw = td->planewidth[c];
105 const int outh = td->planeheight[c];
106 const int slice_start = (outh * jobnr) / nb_jobs;
107 const int slice_end = (outh * (jobnr+1)) / nb_jobs;
108 const int ref_linesize = td->ref_linesize[c];
109 const int main_linesize = td->main_linesize[c];
110 const uint8_t *main_line = td->main_data[c] + main_linesize * slice_start;
111 const uint8_t *ref_line = td->ref_data[c] + ref_linesize * slice_start;
112 uint64_t m = 0;
113
114 s->sad(main_line, main_linesize, ref_line, ref_linesize,
115 outw, slice_end - slice_start, &m);
116
117 score[c] = m;
118 }
119
120 return 0;
121 }
122
123 static
124 int compute_images_identity(AVFilterContext *ctx, void *arg,
125 int jobnr, int nb_jobs)
126 {
127 IdentityContext *s = ctx->priv;
128 ThreadData *td = arg;
129 uint64_t *score = td->score[jobnr];
130
131 for (int c = 0; c < td->nb_components; c++) {
132 const int outw = td->planewidth[c];
133 const int outh = td->planeheight[c];
134 const int slice_start = (outh * jobnr) / nb_jobs;
135 const int slice_end = (outh * (jobnr+1)) / nb_jobs;
136 const int ref_linesize = td->ref_linesize[c];
137 const int main_linesize = td->main_linesize[c];
138 const uint8_t *main_line = td->main_data[c] + main_linesize * slice_start;
139 const uint8_t *ref_line = td->ref_data[c] + ref_linesize * slice_start;
140 uint64_t m = 0;
141
142 for (int i = slice_start; i < slice_end; i++) {
143 m += s->filter_line(main_line, ref_line, outw);
144 ref_line += ref_linesize;
145 main_line += main_linesize;
146 }
147 score[c] = m;
148 }
149
150 return 0;
151 }
152
153 static void set_meta(AVFilterContext *ctx,
154 AVDictionary **metadata, const char *key, char comp, float d)
155 {
156 char value[128];
157 snprintf(value, sizeof(value), "%f", d);
158 if (comp) {
159 char key2[128];
160 snprintf(key2, sizeof(key2), "lavfi.%s.%s%s%c",
161 ctx->filter->name, ctx->filter->name, key, comp);
162 av_dict_set(metadata, key2, value, 0);
163 } else {
164 char key2[128];
165 snprintf(key2, sizeof(key2), "lavfi.%s.%s%s",
166 ctx->filter->name, ctx->filter->name, key);
167 av_dict_set(metadata, key2, value, 0);
168 }
169 }
170
171 static int do_identity(FFFrameSync *fs)
172 {
173 AVFilterContext *ctx = fs->parent;
174 IdentityContext *s = ctx->priv;
175 AVFrame *master, *ref;
176 double comp_score[4], score = 0.;
177 uint64_t comp_sum[4] = { 0 };
178 AVDictionary **metadata;
179 ThreadData td;
180 int ret;
181
182 ret = ff_framesync_dualinput_get(fs, &master, &ref);
183 if (ret < 0)
184 return ret;
185 if (ctx->is_disabled || !ref)
186 return ff_filter_frame(ctx->outputs[0], master);
187 metadata = &master->metadata;
188
189 td.nb_components = s->nb_components;
190 td.score = s->scores;
191 for (int c = 0; c < s->nb_components; c++) {
192 td.main_data[c] = master->data[c];
193 td.ref_data[c] = ref->data[c];
194 td.main_linesize[c] = master->linesize[c];
195 td.ref_linesize[c] = ref->linesize[c];
196 td.planewidth[c] = s->planewidth[c];
197 td.planeheight[c] = s->planeheight[c];
198 }
199
200 ff_filter_execute(ctx, s->filter_slice, &td, NULL,
201 FFMIN(s->planeheight[1], s->nb_threads));
202
203 for (int j = 0; j < s->nb_threads; j++) {
204 for (int c = 0; c < s->nb_components; c++)
205 comp_sum[c] += s->scores[j][c];
206 }
207
208 for (int c = 0; c < s->nb_components; c++)
209 comp_score[c] = comp_sum[c] / ((double)s->planewidth[c] * s->planeheight[c]);
210
211 for (int c = 0; c < s->nb_components && s->is_msad; c++)
212 comp_score[c] /= (double)s->max[c];
213
214 for (int c = 0; c < s->nb_components; c++)
215 score += comp_score[c];
216 score /= s->nb_components;
217
218 s->min_score = FFMIN(s->min_score, score);
219 s->max_score = FFMAX(s->max_score, score);
220
221 s->score += score;
222
223 for (int j = 0; j < s->nb_components; j++)
224 s->score_comp[j] += comp_score[j];
225 s->nb_frames++;
226
227 for (int j = 0; j < s->nb_components; j++) {
228 int c = s->is_rgb ? s->rgba_map[j] : j;
229 set_meta(ctx, metadata, ".", s->comps[j], comp_score[c]);
230 }
231 set_meta(ctx, metadata, "_avg", 0, score);
232
233 return ff_filter_frame(ctx->outputs[0], master);
234 }
235
236 static av_cold int init(AVFilterContext *ctx)
237 {
238 IdentityContext *s = ctx->priv;
239
240 s->fs.on_event = do_identity;
241
242 return 0;
243 }
244
245 static const enum AVPixelFormat pix_fmts[] = {
246 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
247 #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
248 #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
249 #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
250 PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
251 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
252 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
253 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
254 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
255 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
256 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
257 AV_PIX_FMT_NONE
258 };
259
260 static int config_input_ref(AVFilterLink *inlink)
261 {
262 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
263 AVFilterContext *ctx = inlink->dst;
264 IdentityContext *s = ctx->priv;
265
266 s->nb_threads = ff_filter_get_nb_threads(ctx);
267 s->nb_components = desc->nb_components;
268 if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
269 ctx->inputs[0]->h != ctx->inputs[1]->h) {
270 av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
271 return AVERROR(EINVAL);
272 }
273
274 s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
275 s->comps[0] = s->is_rgb ? 'R' : 'Y' ;
276 s->comps[1] = s->is_rgb ? 'G' : 'U' ;
277 s->comps[2] = s->is_rgb ? 'B' : 'V' ;
278 s->comps[3] = 'A';
279
280 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
281 s->planeheight[0] = s->planeheight[3] = inlink->h;
282 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
283 s->planewidth[0] = s->planewidth[3] = inlink->w;
284
285 s->scores = av_calloc(s->nb_threads, sizeof(*s->scores));
286 if (!s->scores)
287 return AVERROR(ENOMEM);
288
289 for (int t = 0; t < s->nb_threads; t++) {
290 s->scores[t] = av_calloc(s->nb_components, sizeof(*s->scores[0]));
291 if (!s->scores[t])
292 return AVERROR(ENOMEM);
293 }
294
295 s->min_score = +INFINITY;
296 s->max_score = -INFINITY;
297
298 s->max[0] = (1 << desc->comp[0].depth) - 1;
299 s->max[1] = (1 << desc->comp[1].depth) - 1;
300 s->max[2] = (1 << desc->comp[2].depth) - 1;
301 s->max[3] = (1 << desc->comp[3].depth) - 1;
302
303 s->is_msad = !strcmp(ctx->filter->name, "msad");
304 s->filter_slice = !s->is_msad ? compute_images_identity : compute_images_msad;
305 s->filter_line = desc->comp[0].depth > 8 ? identity_line_16bit : identity_line_8bit;
306
307 s->sad = ff_scene_sad_get_fn(desc->comp[0].depth <= 8 ? 8 : 16);
308 if (!s->sad)
309 return AVERROR(EINVAL);
310
311 return 0;
312 }
313
314 static int config_output(AVFilterLink *outlink)
315 {
316 AVFilterContext *ctx = outlink->src;
317 IdentityContext *s = ctx->priv;
318 AVFilterLink *mainlink = ctx->inputs[0];
319 int ret;
320
321 ret = ff_framesync_init_dualinput(&s->fs, ctx);
322 if (ret < 0)
323 return ret;
324 outlink->w = mainlink->w;
325 outlink->h = mainlink->h;
326 outlink->time_base = mainlink->time_base;
327 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
328 outlink->frame_rate = mainlink->frame_rate;
329 if ((ret = ff_framesync_configure(&s->fs)) < 0)
330 return ret;
331
332 outlink->time_base = s->fs.time_base;
333
334 if (av_cmp_q(mainlink->time_base, outlink->time_base) ||
335 av_cmp_q(ctx->inputs[1]->time_base, outlink->time_base))
336 av_log(ctx, AV_LOG_WARNING, "not matching timebases found between first input: %d/%d and second input %d/%d, results may be incorrect!\n",
337 mainlink->time_base.num, mainlink->time_base.den,
338 ctx->inputs[1]->time_base.num, ctx->inputs[1]->time_base.den);
339
340 return 0;
341 }
342
343 static int activate(AVFilterContext *ctx)
344 {
345 IdentityContext *s = ctx->priv;
346 return ff_framesync_activate(&s->fs);
347 }
348
349 static av_cold void uninit(AVFilterContext *ctx)
350 {
351 IdentityContext *s = ctx->priv;
352
353 if (s->nb_frames > 0) {
354 char buf[256];
355
356 buf[0] = 0;
357 for (int j = 0; j < s->nb_components; j++) {
358 int c = s->is_rgb ? s->rgba_map[j] : j;
359 av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[j], s->score_comp[c] / s->nb_frames);
360 }
361
362 av_log(ctx, AV_LOG_INFO, "%s%s average:%f min:%f max:%f\n",
363 ctx->filter->name,
364 buf,
365 s->score / s->nb_frames,
366 s->min_score,
367 s->max_score);
368 }
369
370 ff_framesync_uninit(&s->fs);
371 for (int t = 0; t < s->nb_threads && s->scores; t++)
372 av_freep(&s->scores[t]);
373 av_freep(&s->scores);
374 }
375
376 static const AVFilterPad identity_inputs[] = {
377 {
378 .name = "main",
379 .type = AVMEDIA_TYPE_VIDEO,
380 },{
381 .name = "reference",
382 .type = AVMEDIA_TYPE_VIDEO,
383 .config_props = config_input_ref,
384 },
385 };
386
387 static const AVFilterPad identity_outputs[] = {
388 {
389 .name = "default",
390 .type = AVMEDIA_TYPE_VIDEO,
391 .config_props = config_output,
392 },
393 };
394
395 static const AVOption options[] = {
396 { NULL }
397 };
398
399 #if CONFIG_IDENTITY_FILTER
400
401 #define identity_options options
402 FRAMESYNC_DEFINE_CLASS(identity, IdentityContext, fs);
403
404 const AVFilter ff_vf_identity = {
405 .name = "identity",
406 .description = NULL_IF_CONFIG_SMALL("Calculate the Identity between two video streams."),
407 .preinit = identity_framesync_preinit,
408 .init = init,
409 .uninit = uninit,
410 .activate = activate,
411 .priv_size = sizeof(IdentityContext),
412 .priv_class = &identity_class,
413 FILTER_INPUTS(identity_inputs),
414 FILTER_OUTPUTS(identity_outputs),
415 FILTER_PIXFMTS_ARRAY(pix_fmts),
416 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
417 AVFILTER_FLAG_SLICE_THREADS |
418 AVFILTER_FLAG_METADATA_ONLY,
419 };
420
421 #endif /* CONFIG_IDENTITY_FILTER */
422
423 #if CONFIG_MSAD_FILTER
424
425 #define msad_options options
426 FRAMESYNC_DEFINE_CLASS(msad, IdentityContext, fs);
427
428 const AVFilter ff_vf_msad = {
429 .name = "msad",
430 .description = NULL_IF_CONFIG_SMALL("Calculate the MSAD between two video streams."),
431 .preinit = msad_framesync_preinit,
432 .init = init,
433 .uninit = uninit,
434 .activate = activate,
435 .priv_size = sizeof(IdentityContext),
436 .priv_class = &msad_class,
437 FILTER_INPUTS(identity_inputs),
438 FILTER_OUTPUTS(identity_outputs),
439 FILTER_PIXFMTS_ARRAY(pix_fmts),
440 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
441 AVFILTER_FLAG_SLICE_THREADS |
442 AVFILTER_FLAG_METADATA_ONLY,
443 };
444
445 #endif /* CONFIG_MSAD_FILTER */
446