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