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