FFmpeg
Loading...
Searching...
No Matches
vf_libvmaf.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Ronald S. Bultje <rsbultje@gmail.com>
3 * Copyright (c) 2017 Ashish Pratap Singh <ashk43712@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * Calculate the VMAF between two input videos.
25 */
26
27#include "config.h"
28#include "config_components.h"
29
30#include <libvmaf.h>
31
32#include "libavutil/avstring.h"
33#include "libavutil/mem.h"
34#include "libavutil/opt.h"
35#include "libavutil/pixdesc.h"
36#include "avfilter.h"
37#include "drawutils.h"
38#include "filters.h"
39#include "formats.h"
40#include "framesync.h"
41#include "video.h"
42
43#if CONFIG_LIBVMAF_CUDA_FILTER
44#include <libvmaf_cuda.h>
45
46#include "libavutil/hwcontext.h"
48#endif
49
50typedef struct LIBVMAFContext {
51 const AVClass *class;
53 char *log_path;
54 char *log_fmt;
55 char *pool;
58 char *model_cfg;
60 VmafContext *vmaf;
61 VmafModel **model;
62 unsigned model_cnt;
63 unsigned frame_cnt;
64 unsigned bpc;
65#if CONFIG_LIBVMAF_CUDA_FILTER
66 VmafCudaState *cu_state;
67#endif
69
70#define OFFSET(x) offsetof(LIBVMAFContext, x)
71#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
72
73static const AVOption libvmaf_options[] = {
74 {"log_path", "Set the file path to be used to write log.", OFFSET(log_path), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
75 {"log_fmt", "Set the format of the log (csv, json, xml, or sub).", OFFSET(log_fmt), AV_OPT_TYPE_STRING, {.str="xml"}, 0, 1, FLAGS},
76 {"pool", "Set the pool method to be used for computing vmaf.", OFFSET(pool), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
77 {"n_threads", "Set number of threads to be used when computing vmaf.", OFFSET(n_threads), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT_MAX, FLAGS},
78 {"n_subsample", "Set interval for frame subsampling used when computing vmaf.", OFFSET(n_subsample), AV_OPT_TYPE_INT, {.i64=1}, 1, UINT_MAX, FLAGS},
79 {"model", "Set the model to be used for computing vmaf.", OFFSET(model_cfg), AV_OPT_TYPE_STRING, {.str="version=vmaf_v0.6.1"}, 0, 1, FLAGS},
80 {"feature", "Set the feature to be used for computing vmaf.", OFFSET(feature_cfg), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
81 { NULL }
82};
83
85
86static enum VmafPixelFormat pix_fmt_map(enum AVPixelFormat av_pix_fmt)
87{
88 switch (av_pix_fmt) {
93 return VMAF_PIX_FMT_YUV420P;
98 return VMAF_PIX_FMT_YUV422P;
103 return VMAF_PIX_FMT_YUV444P;
104 default:
105 return VMAF_PIX_FMT_UNKNOWN;
106 }
107}
108
109#if HAVE_LIBVMAF_PICTURE_CONVERT
110static VmafColor color_map(const AVFrame *f)
111{
112 VmafColor c = { 0 };
113
114 switch (f->color_range) {
115 case AVCOL_RANGE_MPEG: c.range = VMAF_COLOR_RANGE_LIMITED; break;
116 case AVCOL_RANGE_JPEG: c.range = VMAF_COLOR_RANGE_FULL; break;
117 default: c.range = VMAF_COLOR_RANGE_UNKNOWN; break;
118 }
119
120 switch (f->color_primaries) {
121 case AVCOL_PRI_BT709: c.primaries = VMAF_COLOR_PRIMARIES_BT709; break;
122 case AVCOL_PRI_BT2020: c.primaries = VMAF_COLOR_PRIMARIES_BT2020; break;
123 case AVCOL_PRI_SMPTE432: c.primaries = VMAF_COLOR_PRIMARIES_SMPTE432; break;
124 default: c.primaries = VMAF_COLOR_PRIMARIES_UNKNOWN; break;
125 }
126
127 switch (f->color_trc) {
131 case AVCOL_TRC_BT709: c.trc = VMAF_COLOR_TRC_BT709; break;
132 case AVCOL_TRC_SMPTE2084: c.trc = VMAF_COLOR_TRC_SMPTE2084; break;
133 default: c.trc = VMAF_COLOR_TRC_UNKNOWN; break;
134 }
135
136 switch (f->colorspace) {
137 case AVCOL_SPC_BT709: c.matrix = VMAF_COLOR_MATRIX_BT709; break;
138 case AVCOL_SPC_BT2020_NCL: c.matrix = VMAF_COLOR_MATRIX_BT2020_NCL; break;
139 case AVCOL_SPC_ICTCP: c.matrix = VMAF_COLOR_MATRIX_ICTCP; break;
140 default: c.matrix = VMAF_COLOR_MATRIX_UNKNOWN; break;
141 }
142
143 return c;
144}
145#endif
146
147#if HAVE_LIBVMAF_PICTURE_WRAP
148static int release_frame(VmafPicture *pic, void *cookie)
149{
150 AVFrame *frame = cookie;
152 return 0;
153}
154
155static int wrap_picture_data(AVFrame *src, VmafPicture *dst, unsigned bpc)
156{
158 if (!ref)
159 return AVERROR(ENOMEM);
160
161 VmafPictureWrapped pic_wrapped = {
162 .pix_fmt = pix_fmt_map(src->format),
163 .bpc = bpc,
164 .w = src->width,
165 .h = src->height,
166 .data = { src->data[0], src->data[1], src->data[2] },
167 .stride = { src->linesize[0], src->linesize[1], src->linesize[2] },
168 .cookie = ref,
169 .release_picture = release_frame,
170 };
171
172 if (vmaf_picture_wrap(dst, pic_wrapped)) {
174 return AVERROR(ENOMEM);
175 }
176
177 return 0;
178}
179#else
180static int copy_picture_data(AVFrame *src, VmafPicture *dst, unsigned bpc)
181{
182 const int bytes_per_value = bpc > 8 ? 2 : 1;
183 int err = vmaf_picture_alloc(dst, pix_fmt_map(src->format), bpc,
184 src->width, src->height);
185 if (err)
186 return AVERROR(ENOMEM);
187
188 for (unsigned i = 0; i < 3; i++) {
189 uint8_t *src_data = src->data[i];
190 uint8_t *dst_data = dst->data[i];
191 for (unsigned j = 0; j < dst->h[i]; j++) {
192 memcpy(dst_data, src_data, bytes_per_value * dst->w[i]);
193 src_data += src->linesize[i];
194 dst_data += dst->stride[i];
195 }
196 }
197
198 return 0;
199}
200#endif
201
202static int get_picture(AVFrame *src, VmafPicture *dst, unsigned bpc)
203{
204#if HAVE_LIBVMAF_PICTURE_WRAP
205 int err = wrap_picture_data(src, dst, bpc);
206#else
207 int err = copy_picture_data(src, dst, bpc);
208#endif
209#if HAVE_LIBVMAF_PICTURE_CONVERT
210 if (!err)
211 dst->color = color_map(src);
212#endif
213 return err;
214}
215
217{
218 AVFilterContext *ctx = fs->parent;
219 LIBVMAFContext *s = ctx->priv;
220 VmafPicture pic_ref, pic_dist;
221 AVFrame *ref, *dist;
222 int err = 0;
223
224 int ret = ff_framesync_dualinput_get(fs, &dist, &ref);
225 if (ret < 0)
226 return ret;
227 if (ctx->is_disabled || !ref)
228 return ff_filter_frame(ctx->outputs[0], dist);
229
230 if (dist->color_range != ref->color_range) {
231 av_log(ctx, AV_LOG_WARNING, "distorted and reference "
232 "frames use different color ranges (%s != %s)\n",
234 av_color_range_name(ref->color_range));
235 }
236
237 err = get_picture(ref, &pic_ref, s->bpc);
238 if (err) {
239 av_log(ctx, AV_LOG_ERROR, "problem during vmaf_picture_alloc.\n");
240 return AVERROR(ENOMEM);
241 }
242
243 err = get_picture(dist, &pic_dist, s->bpc);
244 if (err) {
245 av_log(ctx, AV_LOG_ERROR, "problem during vmaf_picture_alloc.\n");
246 vmaf_picture_unref(&pic_ref);
247 return AVERROR(ENOMEM);
248 }
249
250 err = vmaf_read_pictures(s->vmaf, &pic_ref, &pic_dist, s->frame_cnt++);
251 if (err) {
252 av_log(ctx, AV_LOG_ERROR, "problem during vmaf_read_pictures.\n");
253 return AVERROR(EINVAL);
254 }
255
256 return ff_filter_frame(ctx->outputs[0], dist);
257}
258
259static AVDictionary **delimited_dict_parse(char *str, unsigned *cnt)
260{
261 AVDictionary **dict = NULL;
262 char *str_copy = NULL;
263 char *saveptr = NULL;
264 unsigned cnt2;
265 int err = 0;
266
267 if (!str)
268 return NULL;
269
270 cnt2 = 1;
271 for (char *p = str; *p; p++) {
272 if (*p == '|')
273 cnt2++;
274 }
275
276 dict = av_calloc(cnt2, sizeof(*dict));
277 if (!dict)
278 goto fail;
279
280 str_copy = av_strdup(str);
281 if (!str_copy)
282 goto fail;
283
284 *cnt = 0;
285 for (unsigned i = 0; i < cnt2; i++) {
286 char *s = av_strtok(i == 0 ? str_copy : NULL, "|", &saveptr);
287 if (!s)
288 continue;
289 err = av_dict_parse_string(&dict[(*cnt)++], s, "=", ":", 0);
290 if (err)
291 goto fail;
292 }
293
294 av_free(str_copy);
295 return dict;
296
297fail:
298 if (dict) {
299 for (unsigned i = 0; i < *cnt; i++) {
300 if (dict[i])
301 av_dict_free(&dict[i]);
302 }
303 av_free(dict);
304 }
305
306 av_free(str_copy);
307 *cnt = 0;
308 return NULL;
309}
310
312{
313 LIBVMAFContext *s = ctx->priv;
314 AVDictionary **dict = NULL;
315 unsigned dict_cnt;
316 int err = 0;
317
318 if (!s->feature_cfg)
319 return 0;
320
321 dict = delimited_dict_parse(s->feature_cfg, &dict_cnt);
322 if (!dict) {
324 "could not parse feature config: %s\n", s->feature_cfg);
325 return AVERROR(EINVAL);
326 }
327
328 for (unsigned i = 0; i < dict_cnt; i++) {
329 const AVDictionaryEntry *name = av_dict_get(dict[i], "name", NULL,
331 VmafFeatureDictionary *feature_opts_dict = NULL;
332 const AVDictionaryEntry *e = NULL;
333
334 if (!name) {
335 av_log(ctx, AV_LOG_ERROR, "feature without a name: %s\n",
336 s->feature_cfg);
337 err = AVERROR(EINVAL);
338 goto exit;
339 }
340
341 while (e = av_dict_iterate(dict[i], e)) {
342 if (!strcmp(e->key, "name"))
343 continue;
344
345 err = vmaf_feature_dictionary_set(&feature_opts_dict, e->key,
346 e->value);
347 if (err) {
349 "could not set feature option: %s.%s=%s\n",
350 name->value, e->key, e->value);
351 goto exit;
352 }
353 }
354
355 err = vmaf_use_feature(s->vmaf, name->value, feature_opts_dict);
356 if (err) {
358 "problem during vmaf_use_feature: %s\n", name->value);
359 goto exit;
360 }
361 }
362
363exit:
364 for (unsigned i = 0; i < dict_cnt; i++) {
365 if (dict[i])
366 av_dict_free(&dict[i]);
367 }
368 av_free(dict);
369 return err;
370}
371
373{
374 LIBVMAFContext *s = ctx->priv;
375 AVDictionary **dict;
376 unsigned dict_cnt;
377 int err = 0;
378
379 if (!s->model_cfg) return 0;
380
381 dict_cnt = 0;
382 dict = delimited_dict_parse(s->model_cfg, &dict_cnt);
383 if (!dict) {
385 "could not parse model config: %s\n", s->model_cfg);
386 return AVERROR(EINVAL);
387 }
388
389 s->model_cnt = dict_cnt;
390 s->model = av_calloc(s->model_cnt, sizeof(*s->model));
391 if (!s->model)
392 return AVERROR(ENOMEM);
393
394 for (unsigned i = 0; i < dict_cnt; i++) {
395 VmafModelConfig model_cfg = { 0 };
396 const AVDictionaryEntry *e = NULL;
397 char *version = NULL;
398 char *path = NULL;
399
400 while (e = av_dict_iterate(dict[i], e)) {
401 if (!strcmp(e->key, "disable_clip")) {
402 model_cfg.flags |= !strcmp(e->value, "true") ?
403 VMAF_MODEL_FLAG_DISABLE_CLIP : 0;
404 continue;
405 }
406
407 if (!strcmp(e->key, "enable_transform")) {
408 model_cfg.flags |= !strcmp(e->value, "true") ?
409 VMAF_MODEL_FLAG_ENABLE_TRANSFORM : 0;
410 continue;
411 }
412
413 if (!strcmp(e->key, "name")) {
414 model_cfg.name = e->value;
415 continue;
416 }
417
418 if (!strcmp(e->key, "version")) {
419 version = e->value;
420 continue;
421 }
422
423 if (!strcmp(e->key, "path")) {
424 path = e->value;
425 continue;
426 }
427 }
428
429 if (version) {
430 err = vmaf_model_load(&s->model[i], &model_cfg, version);
431 if (err) {
433 "could not load libvmaf model with version: %s\n",
434 version);
435 goto exit;
436 }
437 }
438
439 if (path && !s->model[i]) {
440 err = vmaf_model_load_from_path(&s->model[i], &model_cfg, path);
441 if (err) {
443 "could not load libvmaf model with path: %s\n",
444 path);
445 goto exit;
446 }
447 }
448
449 if (!s->model[i]) {
451 "could not load libvmaf model with config: %s\n",
452 s->model_cfg);
453 goto exit;
454 }
455
456 while (e = av_dict_iterate(dict[i], e)) {
457 VmafFeatureDictionary *feature_opts_dict = NULL;
458 char *feature_opt = NULL;
459
460 char *feature_name = av_strtok(e->key, ".", &feature_opt);
461 if (!feature_opt)
462 continue;
463
464 err = vmaf_feature_dictionary_set(&feature_opts_dict,
465 feature_opt, e->value);
466 if (err) {
468 "could not set feature option: %s.%s=%s\n",
469 feature_name, feature_opt, e->value);
470 err = AVERROR(EINVAL);
471 goto exit;
472 }
473
474 err = vmaf_model_feature_overload(s->model[i], feature_name,
475 feature_opts_dict);
476 if (err) {
478 "could not overload feature: %s\n", feature_name);
479 err = AVERROR(EINVAL);
480 goto exit;
481 }
482 }
483 }
484
485 for (unsigned i = 0; i < s->model_cnt; i++) {
486 err = vmaf_use_features_from_model(s->vmaf, s->model[i]);
487 if (err) {
489 "problem during vmaf_use_features_from_model\n");
490 err = AVERROR(EINVAL);
491 goto exit;
492 }
493 }
494
495exit:
496 for (unsigned i = 0; i < dict_cnt; i++) {
497 if (dict[i])
498 av_dict_free(&dict[i]);
499 }
500 av_free(dict);
501 return err;
502}
503
504static enum VmafLogLevel log_level_map(int log_level)
505{
506 switch (log_level) {
507 case AV_LOG_QUIET:
508 return VMAF_LOG_LEVEL_NONE;
509 case AV_LOG_ERROR:
510 return VMAF_LOG_LEVEL_ERROR;
511 case AV_LOG_WARNING:
512 return VMAF_LOG_LEVEL_WARNING;
513 case AV_LOG_INFO:
514 return VMAF_LOG_LEVEL_INFO;
515 case AV_LOG_DEBUG:
516 return VMAF_LOG_LEVEL_DEBUG;
517 default:
518 return VMAF_LOG_LEVEL_INFO;
519 }
520}
521
523{
524 LIBVMAFContext *s = ctx->priv;
525 int err = 0;
526
527 VmafConfiguration cfg = {
528 .log_level = log_level_map(av_log_get_level()),
529 .n_subsample = s->n_subsample,
530 .n_threads = s->n_threads,
531 };
532
533 err = vmaf_init(&s->vmaf, cfg);
534 if (err)
535 return AVERROR(EINVAL);
536
537 err = parse_models(ctx);
538 if (err)
539 return err;
540
541 err = parse_features(ctx);
542 if (err)
543 return err;
544
545 s->fs.on_event = do_vmaf;
546 return 0;
547}
548
556
558{
559 AVFilterContext *ctx = inlink->dst;
560 LIBVMAFContext *s = ctx->priv;
562 int err = 0;
563
564 if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
565 av_log(ctx, AV_LOG_ERROR, "input pix_fmt must match.\n");
566 err |= AVERROR(EINVAL);
567 }
568
569 if (err)
570 return err;
571
573 s->bpc = desc->comp[0].depth;
574
575 return 0;
576}
577
578static int config_output(AVFilterLink *outlink)
579{
580 AVFilterContext *ctx = outlink->src;
581 LIBVMAFContext *s = ctx->priv;
582 AVFilterLink *mainlink = ctx->inputs[0];
583 FilterLink *il = ff_filter_link(mainlink);
584 FilterLink *ol = ff_filter_link(outlink);
585 int ret;
586
588 if (ret < 0)
589 return ret;
590 outlink->w = mainlink->w;
591 outlink->h = mainlink->h;
592 outlink->time_base = mainlink->time_base;
593 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
594 ol->frame_rate = il->frame_rate;
595 if ((ret = ff_framesync_configure(&s->fs)) < 0)
596 return ret;
597
598 return 0;
599}
600
602{
603 LIBVMAFContext *s = ctx->priv;
604 return ff_framesync_activate(&s->fs);
605}
606
607static enum VmafOutputFormat log_fmt_map(const char *log_fmt)
608{
609 if (log_fmt) {
610 if (!strcmp(log_fmt, "xml"))
611 return VMAF_OUTPUT_FORMAT_XML;
612 if (!strcmp(log_fmt, "json"))
613 return VMAF_OUTPUT_FORMAT_JSON;
614 if (!strcmp(log_fmt, "csv"))
615 return VMAF_OUTPUT_FORMAT_CSV;
616 if (!strcmp(log_fmt, "sub"))
617 return VMAF_OUTPUT_FORMAT_SUB;
618 }
619
620 return VMAF_OUTPUT_FORMAT_XML;
621}
622
623static enum VmafPoolingMethod pool_method_map(const char *pool_method)
624{
625 if (pool_method) {
626 if (!strcmp(pool_method, "min"))
627 return VMAF_POOL_METHOD_MIN;
628 if (!strcmp(pool_method, "mean"))
629 return VMAF_POOL_METHOD_MEAN;
630 if (!strcmp(pool_method, "harmonic_mean"))
631 return VMAF_POOL_METHOD_HARMONIC_MEAN;
632 }
633
634 return VMAF_POOL_METHOD_MEAN;
635}
636
638{
639 LIBVMAFContext *s = ctx->priv;
640 int err = 0;
641
643
644 if (!s->frame_cnt)
645 goto clean_up;
646
647 err = vmaf_read_pictures(s->vmaf, NULL, NULL, 0);
648 if (err) {
650 "problem flushing libvmaf context.\n");
651 }
652
653 for (unsigned i = 0; i < s->model_cnt; i++) {
654 double vmaf_score;
655 err = vmaf_score_pooled(s->vmaf, s->model[i], pool_method_map(s->pool),
656 &vmaf_score, 0, s->frame_cnt - 1);
657 if (err) {
659 "problem getting pooled vmaf score.\n");
660 }
661
662 av_log(ctx, AV_LOG_INFO, "VMAF score: %f\n", vmaf_score);
663 }
664
665 if (s->vmaf) {
666 if (s->log_path && !err)
667 vmaf_write_output(s->vmaf, s->log_path, log_fmt_map(s->log_fmt));
668 }
669
670clean_up:
671 if (s->model) {
672 for (unsigned i = 0; i < s->model_cnt; i++) {
673 if (s->model[i])
674 vmaf_model_destroy(s->model[i]);
675 }
676 av_free(s->model);
677 }
678
679 if (s->vmaf)
680 vmaf_close(s->vmaf);
681}
682
683static const AVFilterPad libvmaf_inputs[] = {
684 {
685 .name = "main",
686 .type = AVMEDIA_TYPE_VIDEO,
687 },
688 {
689 .name = "reference",
690 .type = AVMEDIA_TYPE_VIDEO,
691 .config_props = config_input_ref,
692 },
693};
694
695static const AVFilterPad libvmaf_outputs[] = {
696 {
697 .name = "default",
698 .type = AVMEDIA_TYPE_VIDEO,
699 .config_props = config_output,
700 },
701};
702
704 .p.name = "libvmaf",
705 .p.description = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
706 .p.priv_class = &libvmaf_class,
707 .preinit = libvmaf_framesync_preinit,
708 .init = init,
709 .uninit = uninit,
710 .activate = activate,
711 .priv_size = sizeof(LIBVMAFContext),
715};
716
717#if CONFIG_LIBVMAF_CUDA_FILTER
718static const enum AVPixelFormat supported_formats[] = {
721};
722
723static int format_is_supported(enum AVPixelFormat fmt)
724{
725 int i;
726
727 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
728 if (supported_formats[i] == fmt)
729 return 1;
730 return 0;
731}
732
733static int config_props_cuda(AVFilterLink *outlink)
734{
735 int err;
736 AVFilterContext *ctx = outlink->src;
737 LIBVMAFContext *s = ctx->priv;
738 AVFilterLink *inlink = ctx->inputs[0];
739 FilterLink *inl = ff_filter_link(inlink);
741 AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
742 CUcontext cu_ctx = device_hwctx->cuda_ctx;
743 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frames_ctx->sw_format);
744
745 VmafConfiguration cfg = {
746 .log_level = log_level_map(av_log_get_level()),
747 .n_subsample = s->n_subsample,
748 .n_threads = s->n_threads,
749 };
750
751 VmafCudaPictureConfiguration cuda_pic_cfg = {
752 .pic_params = {
753 .bpc = desc->comp[0].depth,
754 .w = inlink->w,
755 .h = inlink->h,
756 .pix_fmt = pix_fmt_map(frames_ctx->sw_format),
757 },
758 .pic_prealloc_method = VMAF_CUDA_PICTURE_PREALLOCATION_METHOD_DEVICE,
759 };
760
761 VmafCudaConfiguration cuda_cfg = {
762 .cu_ctx = cu_ctx,
763 };
764
765 if (!format_is_supported(frames_ctx->sw_format)) {
767 "Unsupported input format: %s\n", desc->name);
768 return AVERROR(EINVAL);
769 }
770
771 if (ctx->inputs[1]->w != inlink->w || ctx->inputs[1]->h != inlink->h) {
772 av_log(ctx, AV_LOG_ERROR, "CUDA input dimensions must match.\n");
773 return AVERROR(EINVAL);
774 }
775
776 err = vmaf_init(&s->vmaf, cfg);
777 if (err)
778 return AVERROR(EINVAL);
779
780 err = vmaf_cuda_state_init(&s->cu_state, cuda_cfg);
781 if (err)
782 return AVERROR(EINVAL);
783
784 err = vmaf_cuda_import_state(s->vmaf, s->cu_state);
785 if (err)
786 return AVERROR(EINVAL);
787
788 err = vmaf_cuda_preallocate_pictures(s->vmaf, cuda_pic_cfg);
789 if (err < 0)
790 return err;
791
792 err = parse_models(ctx);
793 if (err)
794 return err;
795
796 err = parse_features(ctx);
797 if (err)
798 return err;
799
800 return config_output(outlink);
801}
802
803static int copy_picture_data_cuda(VmafContext* vmaf,
804 AVCUDADeviceContext* device_hwctx,
805 AVFrame* src, VmafPicture* dst,
807{
809 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
810
811 CUDA_MEMCPY2D m = {
812 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
813 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
814 };
815
816 int err = vmaf_cuda_fetch_preallocated_picture(vmaf, dst);
817 if (err)
818 return AVERROR(ENOMEM);
819
820 err = cu->cuCtxPushCurrent(device_hwctx->cuda_ctx);
821 if (err)
822 return AVERROR_EXTERNAL;
823
824 for (unsigned i = 0; i < pix_desc->nb_components; i++) {
825 m.srcDevice = (CUdeviceptr) src->data[i];
826 m.srcPitch = src->linesize[i];
827 m.dstDevice = (CUdeviceptr) dst->data[i];
828 m.dstPitch = dst->stride[i];
829 m.WidthInBytes = dst->w[i] * ((dst->bpc + 7) / 8);
830 m.Height = dst->h[i];
831
832 err = cu->cuMemcpy2D(&m);
833 if (err)
834 return AVERROR_EXTERNAL;
835 break;
836 }
837
838 err = cu->cuCtxPopCurrent(NULL);
839 if (err)
840 return AVERROR_EXTERNAL;
841
842 return 0;
843}
844
845static int do_vmaf_cuda(FFFrameSync* fs)
846{
847 AVFilterContext* ctx = fs->parent;
848 LIBVMAFContext* s = ctx->priv;
849 AVFilterLink *inlink = ctx->inputs[0];
850 FilterLink *inl = ff_filter_link(inlink);
852 AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
853 VmafPicture pic_ref, pic_dist;
854 AVFrame *ref, *dist;
855
856 int err = 0;
857
858 err = ff_framesync_dualinput_get(fs, &dist, &ref);
859 if (err < 0)
860 return err;
861 if (ctx->is_disabled || !ref)
862 return ff_filter_frame(ctx->outputs[0], dist);
863
864 err = copy_picture_data_cuda(s->vmaf, device_hwctx, ref, &pic_ref,
865 frames_ctx->sw_format);
866 if (err) {
867 av_log(ctx, AV_LOG_ERROR, "problem during copy_picture_data_cuda.\n");
868 return AVERROR(ENOMEM);
869 }
870
871 err = copy_picture_data_cuda(s->vmaf, device_hwctx, dist, &pic_dist,
872 frames_ctx->sw_format);
873 if (err) {
874 av_log(ctx, AV_LOG_ERROR, "problem during copy_picture_data_cuda.\n");
875 return AVERROR(ENOMEM);
876 }
877
878 err = vmaf_read_pictures(s->vmaf, &pic_ref, &pic_dist, s->frame_cnt++);
879 if (err) {
880 av_log(ctx, AV_LOG_ERROR, "problem during vmaf_read_pictures.\n");
881 return AVERROR(EINVAL);
882 }
883
884 return ff_filter_frame(ctx->outputs[0], dist);
885}
886
887static av_cold int init_cuda(AVFilterContext *ctx)
888{
889 LIBVMAFContext *s = ctx->priv;
890 s->fs.on_event = do_vmaf_cuda;
891 return 0;
892}
893
894static const AVFilterPad libvmaf_outputs_cuda[] = {
895 {
896 .name = "default",
897 .type = AVMEDIA_TYPE_VIDEO,
898 .config_props = config_props_cuda,
899 },
900};
901
903 .p.name = "libvmaf_cuda",
904 .p.description = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
905 .p.priv_class = &libvmaf_class,
906 .preinit = libvmaf_framesync_preinit,
907 .init = init_cuda,
908 .uninit = uninit,
909 .activate = activate,
910 .priv_size = sizeof(LIBVMAFContext),
912 FILTER_OUTPUTS(libvmaf_outputs_cuda),
914 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
915};
916#endif
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
const FFFilter ff_vf_libvmaf
Definition vf_libvmaf.c:703
const FFFilter ff_vf_libvmaf_cuda
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define FLAGS
Definition cmdutils.c:596
#define NULL
Definition coverity.c:32
static enum AVPixelFormat pix_fmt
static AVFrame * frame
misc drawing utilities
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
static CheckasmConfig cfg
Definition checkasm.c:74
char * path
Definition ffplay.c:385
int ret
Definition ffplay.c:391
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition framesync.c:137
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Definition framesync.c:390
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition framesync.c:352
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
Definition framesync.c:372
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition framesync.c:301
#define FRAMESYNC_DEFINE_CLASS(name, context, field)
Definition framesync.h:352
#define fail
Definition test.h:479
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:43
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition dict.c:213
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition dict.h:74
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:486
#define AV_LOG_QUIET
Print no output.
Definition log.h:192
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_log_get_level(void)
Get the current log level.
Definition log.c:477
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
FFmpeg internal API for CUDA.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:259
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:269
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
version
Definition libkvazaar.c:313
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
AVOptions.
const char * av_color_range_name(enum AVColorRange range)
Definition pixdesc.c:3800
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
@ AV_PIX_FMT_YUV420P10LE
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition pixfmt.h:156
@ AV_PIX_FMT_YUV422P16LE
planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition pixfmt.h:130
@ AV_PIX_FMT_YUV422P12LE
planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition pixfmt.h:272
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUV420P12LE
planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition pixfmt.h:268
@ AV_PIX_FMT_YUV422P10LE
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition pixfmt.h:158
@ AV_PIX_FMT_YUV444P16LE
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition pixfmt.h:132
@ AV_PIX_FMT_YUV444P10LE
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition pixfmt.h:162
@ AV_PIX_FMT_YUV420P16LE
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition pixfmt.h:128
@ AV_PIX_FMT_YUV444P12LE
planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition pixfmt.h:276
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
Definition pixfmt.h:662
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition pixfmt.h:649
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition pixfmt.h:658
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition pixfmt.h:684
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition pixfmt.h:694
@ AVCOL_TRC_BT2020_12
ITU-R BT2020 for 12-bit system.
Definition pixfmt.h:693
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
Definition pixfmt.h:692
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition pixfmt.h:679
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:561
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:713
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition pixfmt.h:722
@ AVCOL_SPC_ICTCP
ITU-R BT.2100-0, ICtCp.
Definition pixfmt.h:727
const char * name
Definition qsvenc.c:142
#define FF_ARRAY_ELEMS(a)
uint8_t * data
The data buffer.
Definition buffer.h:90
This struct is allocated as AVHWDeviceContext.hwctx.
AVCUDADeviceContextInternal * internal
Describe the class of an AVClass context structure.
Definition log.h:76
char * key
Definition dict.h:91
char * value
Definition dict.h:92
An instance of a filter.
Definition avfilter.h:279
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition frame.h:733
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition pixdesc.h:71
Frame sync structure.
Definition framesync.h:168
unsigned frame_cnt
Definition vf_libvmaf.c:63
FFFrameSync fs
Definition vf_libvmaf.c:52
char * model_cfg
Definition vf_libvmaf.c:58
unsigned model_cnt
Definition vf_libvmaf.c:62
char * log_path
Definition vf_libvmaf.c:53
VmafModel ** model
Definition vf_libvmaf.c:61
char * feature_cfg
Definition vf_libvmaf.c:59
unsigned bpc
Definition vf_libvmaf.c:64
VmafContext * vmaf
Definition vf_libvmaf.c:60
#define av_free(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int ref[MAX_W *MAX_W]
static enum AVPixelFormat supported_formats[]
static int format_is_supported(enum AVPixelFormat fmt)
static int config_input_ref(AVFilterLink *inlink)
Definition vf_corr.c:287
static enum VmafPoolingMethod pool_method_map(const char *pool_method)
Definition vf_libvmaf.c:623
static int copy_picture_data(AVFrame *src, VmafPicture *dst, unsigned bpc)
Definition vf_libvmaf.c:180
static const AVFilterPad libvmaf_inputs[]
Definition vf_libvmaf.c:683
static int parse_models(AVFilterContext *ctx)
Definition vf_libvmaf.c:372
static int do_vmaf(FFFrameSync *fs)
Definition vf_libvmaf.c:216
static AVDictionary ** delimited_dict_parse(char *str, unsigned *cnt)
Definition vf_libvmaf.c:259
static enum VmafPixelFormat pix_fmt_map(enum AVPixelFormat av_pix_fmt)
Definition vf_libvmaf.c:86
static int config_input_ref(AVFilterLink *inlink)
Definition vf_libvmaf.c:557
static const AVOption libvmaf_options[]
Definition vf_libvmaf.c:73
static enum VmafLogLevel log_level_map(int log_level)
Definition vf_libvmaf.c:504
static int activate(AVFilterContext *ctx)
Definition vf_libvmaf.c:601
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_libvmaf.c:637
static int parse_features(AVFilterContext *ctx)
Definition vf_libvmaf.c:311
#define OFFSET(x)
Definition vf_libvmaf.c:70
static int config_output(AVFilterLink *outlink)
Definition vf_libvmaf.c:578
static int get_picture(AVFrame *src, VmafPicture *dst, unsigned bpc)
Definition vf_libvmaf.c:202
static enum VmafOutputFormat log_fmt_map(const char *log_fmt)
Definition vf_libvmaf.c:607
static const AVFilterPad libvmaf_outputs[]
Definition vf_libvmaf.c:695
static double c[64]