28#include "config_components.h"
43#if CONFIG_LIBVMAF_CUDA_FILTER
44#include <libvmaf_cuda.h>
65#if CONFIG_LIBVMAF_CUDA_FILTER
66 VmafCudaState *cu_state;
70#define OFFSET(x) offsetof(LIBVMAFContext, x)
71#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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},
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},
93 return VMAF_PIX_FMT_YUV420P;
98 return VMAF_PIX_FMT_YUV422P;
103 return VMAF_PIX_FMT_YUV444P;
105 return VMAF_PIX_FMT_UNKNOWN;
109#if HAVE_LIBVMAF_PICTURE_CONVERT
110static VmafColor color_map(
const AVFrame *
f)
114 switch (
f->color_range) {
117 default:
c.range = VMAF_COLOR_RANGE_UNKNOWN;
break;
120 switch (
f->color_primaries) {
124 default:
c.primaries = VMAF_COLOR_PRIMARIES_UNKNOWN;
break;
127 switch (
f->color_trc) {
133 default:
c.trc = VMAF_COLOR_TRC_UNKNOWN;
break;
136 switch (
f->colorspace) {
140 default:
c.matrix = VMAF_COLOR_MATRIX_UNKNOWN;
break;
147#if HAVE_LIBVMAF_PICTURE_WRAP
148static int release_frame(VmafPicture *pic,
void *cookie)
155static int wrap_picture_data(
AVFrame *
src, VmafPicture *
dst,
unsigned bpc)
161 VmafPictureWrapped pic_wrapped = {
166 .data = {
src->data[0],
src->data[1],
src->data[2] },
167 .stride = {
src->linesize[0],
src->linesize[1],
src->linesize[2] },
169 .release_picture = release_frame,
172 if (vmaf_picture_wrap(
dst, pic_wrapped)) {
182 const int bytes_per_value = bpc > 8 ? 2 : 1;
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];
204#if HAVE_LIBVMAF_PICTURE_WRAP
205 int err = wrap_picture_data(
src,
dst, bpc);
209#if HAVE_LIBVMAF_PICTURE_CONVERT
211 dst->color = color_map(
src);
220 VmafPicture pic_ref, pic_dist;
227 if (
ctx->is_disabled || !
ref)
232 "frames use different color ranges (%s != %s)\n",
246 vmaf_picture_unref(&pic_ref);
250 err = vmaf_read_pictures(
s->vmaf, &pic_ref, &pic_dist,
s->frame_cnt++);
262 char *str_copy =
NULL;
263 char *saveptr =
NULL;
271 for (
char *p = str; *p; p++) {
285 for (
unsigned i = 0;
i < cnt2;
i++) {
299 for (
unsigned i = 0;
i < *cnt;
i++) {
324 "could not parse feature config: %s\n",
s->feature_cfg);
328 for (
unsigned i = 0;
i < dict_cnt;
i++) {
331 VmafFeatureDictionary *feature_opts_dict =
NULL;
342 if (!strcmp(e->
key,
"name"))
345 err = vmaf_feature_dictionary_set(&feature_opts_dict, e->
key,
349 "could not set feature option: %s.%s=%s\n",
355 err = vmaf_use_feature(
s->vmaf,
name->value, feature_opts_dict);
358 "problem during vmaf_use_feature: %s\n",
name->value);
364 for (
unsigned i = 0;
i < dict_cnt;
i++) {
379 if (!
s->model_cfg)
return 0;
385 "could not parse model config: %s\n",
s->model_cfg);
389 s->model_cnt = dict_cnt;
394 for (
unsigned i = 0;
i < dict_cnt;
i++) {
395 VmafModelConfig model_cfg = { 0 };
401 if (!strcmp(e->
key,
"disable_clip")) {
402 model_cfg.flags |= !strcmp(e->
value,
"true") ?
403 VMAF_MODEL_FLAG_DISABLE_CLIP : 0;
407 if (!strcmp(e->
key,
"enable_transform")) {
408 model_cfg.flags |= !strcmp(e->
value,
"true") ?
409 VMAF_MODEL_FLAG_ENABLE_TRANSFORM : 0;
413 if (!strcmp(e->
key,
"name")) {
414 model_cfg.name = e->
value;
418 if (!strcmp(e->
key,
"version")) {
423 if (!strcmp(e->
key,
"path")) {
430 err = vmaf_model_load(&
s->model[
i], &model_cfg,
version);
433 "could not load libvmaf model with version: %s\n",
439 if (
path && !
s->model[
i]) {
440 err = vmaf_model_load_from_path(&
s->model[
i], &model_cfg,
path);
443 "could not load libvmaf model with path: %s\n",
451 "could not load libvmaf model with config: %s\n",
457 VmafFeatureDictionary *feature_opts_dict =
NULL;
458 char *feature_opt =
NULL;
460 char *feature_name =
av_strtok(e->
key,
".", &feature_opt);
464 err = vmaf_feature_dictionary_set(&feature_opts_dict,
465 feature_opt, e->
value);
468 "could not set feature option: %s.%s=%s\n",
469 feature_name, feature_opt, e->
value);
474 err = vmaf_model_feature_overload(
s->model[
i], feature_name,
478 "could not overload feature: %s\n", feature_name);
485 for (
unsigned i = 0;
i <
s->model_cnt;
i++) {
486 err = vmaf_use_features_from_model(
s->vmaf,
s->model[
i]);
489 "problem during vmaf_use_features_from_model\n");
496 for (
unsigned i = 0;
i < dict_cnt;
i++) {
508 return VMAF_LOG_LEVEL_NONE;
510 return VMAF_LOG_LEVEL_ERROR;
512 return VMAF_LOG_LEVEL_WARNING;
514 return VMAF_LOG_LEVEL_INFO;
516 return VMAF_LOG_LEVEL_DEBUG;
518 return VMAF_LOG_LEVEL_INFO;
527 VmafConfiguration
cfg = {
529 .n_subsample =
s->n_subsample,
530 .n_threads =
s->n_threads,
533 err = vmaf_init(&
s->vmaf,
cfg);
564 if (
ctx->inputs[0]->format !=
ctx->inputs[1]->format) {
573 s->bpc =
desc->comp[0].depth;
590 outlink->
w = mainlink->
w;
591 outlink->
h = mainlink->
h;
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;
620 return VMAF_OUTPUT_FORMAT_XML;
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;
634 return VMAF_POOL_METHOD_MEAN;
647 err = vmaf_read_pictures(
s->vmaf,
NULL,
NULL, 0);
650 "problem flushing libvmaf context.\n");
653 for (
unsigned i = 0;
i <
s->model_cnt;
i++) {
656 &vmaf_score, 0,
s->frame_cnt - 1);
659 "problem getting pooled vmaf score.\n");
666 if (
s->log_path && !err)
672 for (
unsigned i = 0;
i <
s->model_cnt;
i++) {
674 vmaf_model_destroy(
s->model[
i]);
706 .p.priv_class = &libvmaf_class,
707 .preinit = libvmaf_framesync_preinit,
717#if CONFIG_LIBVMAF_CUDA_FILTER
742 CUcontext cu_ctx = device_hwctx->
cuda_ctx;
745 VmafConfiguration
cfg = {
747 .n_subsample =
s->n_subsample,
748 .n_threads =
s->n_threads,
751 VmafCudaPictureConfiguration cuda_pic_cfg = {
753 .bpc =
desc->comp[0].depth,
758 .pic_prealloc_method = VMAF_CUDA_PICTURE_PREALLOCATION_METHOD_DEVICE,
761 VmafCudaConfiguration cuda_cfg = {
767 "Unsupported input format: %s\n",
desc->name);
771 if (
ctx->inputs[1]->w != inlink->w ||
ctx->inputs[1]->h != inlink->h) {
776 err = vmaf_init(&
s->vmaf,
cfg);
780 err = vmaf_cuda_state_init(&
s->cu_state, cuda_cfg);
784 err = vmaf_cuda_import_state(
s->vmaf,
s->cu_state);
788 err = vmaf_cuda_preallocate_pictures(
s->vmaf, cuda_pic_cfg);
803static int copy_picture_data_cuda(VmafContext* vmaf,
812 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
813 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
816 int err = vmaf_cuda_fetch_preallocated_picture(vmaf,
dst);
820 err = cu->cuCtxPushCurrent(device_hwctx->
cuda_ctx);
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];
832 err = cu->cuMemcpy2D(&m);
838 err = cu->cuCtxPopCurrent(
NULL);
853 VmafPicture pic_ref, pic_dist;
861 if (
ctx->is_disabled || !
ref)
864 err = copy_picture_data_cuda(
s->vmaf, device_hwctx,
ref, &pic_ref,
865 frames_ctx->sw_format);
871 err = copy_picture_data_cuda(
s->vmaf, device_hwctx, dist, &pic_dist,
872 frames_ctx->sw_format);
878 err = vmaf_read_pictures(
s->vmaf, &pic_ref, &pic_dist,
s->frame_cnt++);
890 s->fs.on_event = do_vmaf_cuda;
898 .config_props = config_props_cuda,
903 .p.name =
"libvmaf_cuda",
905 .p.priv_class = &libvmaf_class,
906 .preinit = libvmaf_framesync_preinit,
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
const FFFilter ff_vf_libvmaf
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.
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
#define fs(width, name, subs,...)
static enum AVPixelFormat pix_fmt
int(* init)(AVBSFContext *ctx)
static CheckasmConfig cfg
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
#define FRAMESYNC_DEFINE_CLASS(name, context, field)
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ 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...
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
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.
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
#define AVERROR_EXTERNAL
Generic error in an external library.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
#define AV_LOG_QUIET
Print no output.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_log_get_level(void)
Get the current log level.
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().
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)
#define FILTER_OUTPUTS(array)
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
#define FILTER_PIXFMTS_ARRAY(array)
static FilterLink * ff_filter_link(AVFilterLink *link)
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
const char * av_color_range_name(enum AVColorRange range)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AVCOL_RANGE_JPEG
Full range content.
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
@ AV_PIX_FMT_YUV420P10LE
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
@ AV_PIX_FMT_YUV422P16LE
planar YUV 4:2:2, 32bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
@ AV_PIX_FMT_YUV422P12LE
planar YUV 4:2:2,24bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUV420P12LE
planar YUV 4:2:0,18bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
@ AV_PIX_FMT_YUV422P10LE
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
@ AV_PIX_FMT_YUV444P16LE
planar YUV 4:4:4, 48bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
@ AV_PIX_FMT_YUV444P10LE
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
@ AV_PIX_FMT_YUV420P16LE
planar YUV 4:2:0, 24bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
@ AV_PIX_FMT_YUV444P12LE
planar YUV 4:4:4,36bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
@ AVCOL_PRI_BT2020
ITU-R BT2020.
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
@ AVCOL_TRC_BT2020_12
ITU-R BT2020 for 12-bit system.
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
@ AVCOL_TRC_BT709
also ITU-R BT1361
#define AV_PIX_FMT_YUV444P16
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
@ AVCOL_SPC_ICTCP
ITU-R BT.2100-0, ICtCp.
#define FF_ARRAY_ELEMS(a)
uint8_t * data
The data buffer.
This struct is allocated as AVHWDeviceContext.hwctx.
AVCUDADeviceContextInternal * internal
Describe the class of an AVClass context structure.
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * src
source filter
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
This struct describes a set or pool of "hardware" frames (i.e.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
uint8_t nb_components
The number of components each pixel has, (1-4)
Link properties exposed to filter code, but not external callers.
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable.
AVBufferRef * hw_frames_ctx
For hwaccel pixel formats, this should be a reference to the AVHWFramesContext describing the frames.
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)
static enum VmafPoolingMethod pool_method_map(const char *pool_method)
static int copy_picture_data(AVFrame *src, VmafPicture *dst, unsigned bpc)
static const AVFilterPad libvmaf_inputs[]
static int parse_models(AVFilterContext *ctx)
static int do_vmaf(FFFrameSync *fs)
static AVDictionary ** delimited_dict_parse(char *str, unsigned *cnt)
static enum VmafPixelFormat pix_fmt_map(enum AVPixelFormat av_pix_fmt)
static int config_input_ref(AVFilterLink *inlink)
static const AVOption libvmaf_options[]
static enum VmafLogLevel log_level_map(int log_level)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int parse_features(AVFilterContext *ctx)
static int config_output(AVFilterLink *outlink)
static int get_picture(AVFrame *src, VmafPicture *dst, unsigned bpc)
static enum VmafOutputFormat log_fmt_map(const char *log_fmt)
static const AVFilterPad libvmaf_outputs[]