2 * Copyright (c) 2017 Ronald S. Bultje <rsbultje@gmail.com>
3 * Copyright (c) 2017 Ashish Pratap Singh <ashk43712@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
24 * Calculate the VMAF between two input videos.
29 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
33 #include "drawutils.h"
35 #include "framesync.h"
39 typedef struct LIBVMAFContext {
42 const AVPixFmtDescriptor *desc;
46 int vmaf_thread_created;
47 pthread_t vmaf_thread;
67 int enable_conf_interval;
71 #define OFFSET(x) offsetof(LIBVMAFContext, x)
72 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
74 static const AVOption libvmaf_options[] = {
75 {"model_path", "Set the model to be used for computing vmaf.", OFFSET(model_path), AV_OPT_TYPE_STRING, {.str="/usr/local/share/model/vmaf_v0.6.1.pkl"}, 0, 1, FLAGS},
76 {"log_path", "Set the file path to be used to store logs.", OFFSET(log_path), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
77 {"log_fmt", "Set the format of the log (csv, json or xml).", OFFSET(log_fmt), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
78 {"enable_transform", "Enables transform for computing vmaf.", OFFSET(enable_transform), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
79 {"phone_model", "Invokes the phone model that will generate higher VMAF scores.", OFFSET(phone_model), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
80 {"psnr", "Enables computing psnr along with vmaf.", OFFSET(psnr), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
81 {"ssim", "Enables computing ssim along with vmaf.", OFFSET(ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
82 {"ms_ssim", "Enables computing ms-ssim along with vmaf.", OFFSET(ms_ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
83 {"pool", "Set the pool method to be used for computing vmaf.", OFFSET(pool), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
84 {"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},
85 {"n_subsample", "Set interval for frame subsampling used when computing vmaf.", OFFSET(n_subsample), AV_OPT_TYPE_INT, {.i64=1}, 1, UINT_MAX, FLAGS},
86 {"enable_conf_interval", "Enables confidence interval.", OFFSET(enable_conf_interval), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
90 FRAMESYNC_DEFINE_CLASS(libvmaf, LIBVMAFContext, fs);
92 #define read_frame_fn(type, bits) \
93 static int read_frame_##bits##bit(float *ref_data, float *main_data, \
94 float *temp_data, int stride, void *ctx) \
96 LIBVMAFContext *s = (LIBVMAFContext *) ctx; \
99 pthread_mutex_lock(&s->lock); \
101 while (!s->frame_set && !s->eof) { \
102 pthread_cond_wait(&s->cond, &s->lock); \
105 if (s->frame_set) { \
106 int ref_stride = s->gref->linesize[0]; \
107 int main_stride = s->gmain->linesize[0]; \
109 const type *ref_ptr = (const type *) s->gref->data[0]; \
110 const type *main_ptr = (const type *) s->gmain->data[0]; \
112 float *ptr = ref_data; \
113 float factor = 1.f / (1 << (bits - 8)); \
120 for (i = 0; i < h; i++) { \
121 for ( j = 0; j < w; j++) { \
122 ptr[j] = ref_ptr[j] * factor; \
124 ref_ptr += ref_stride / sizeof(*ref_ptr); \
125 ptr += stride / sizeof(*ptr); \
130 for (i = 0; i < h; i++) { \
131 for (j = 0; j < w; j++) { \
132 ptr[j] = main_ptr[j] * factor; \
134 main_ptr += main_stride / sizeof(*main_ptr); \
135 ptr += stride / sizeof(*ptr); \
139 ret = !s->frame_set; \
141 av_frame_unref(s->gref); \
142 av_frame_unref(s->gmain); \
145 pthread_cond_signal(&s->cond); \
146 pthread_mutex_unlock(&s->lock); \
155 read_frame_fn(uint8_t, 8);
156 read_frame_fn(uint16_t, 10);
158 static void compute_vmaf_score(LIBVMAFContext *s)
160 int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
161 int stride, void *ctx);
164 if (s->desc->comp[0].depth <= 8) {
165 read_frame = read_frame_8bit;
167 read_frame = read_frame_10bit;
170 format = (char *) s->desc->name;
172 s->error = compute_vmaf(&s->vmaf_score, format, s->width, s->height,
173 read_frame, s, s->model_path, s->log_path,
174 s->log_fmt, 0, 0, s->enable_transform,
175 s->phone_model, s->psnr, s->ssim,
177 s->n_threads, s->n_subsample, s->enable_conf_interval);
180 static void *call_vmaf(void *ctx)
182 LIBVMAFContext *s = (LIBVMAFContext *) ctx;
183 compute_vmaf_score(s);
185 av_log(ctx, AV_LOG_INFO, "VMAF score: %f\n",s->vmaf_score);
187 pthread_mutex_lock(&s->lock);
188 pthread_cond_signal(&s->cond);
189 pthread_mutex_unlock(&s->lock);
195 static int do_vmaf(FFFrameSync *fs)
197 AVFilterContext *ctx = fs->parent;
198 LIBVMAFContext *s = ctx->priv;
199 AVFrame *master, *ref;
202 ret = ff_framesync_dualinput_get(fs, &master, &ref);
206 return ff_filter_frame(ctx->outputs[0], master);
208 pthread_mutex_lock(&s->lock);
210 while (s->frame_set && !s->error) {
211 pthread_cond_wait(&s->cond, &s->lock);
215 av_log(ctx, AV_LOG_ERROR,
216 "libvmaf encountered an error, check log for details\n");
217 pthread_mutex_unlock(&s->lock);
218 return AVERROR(EINVAL);
221 av_frame_ref(s->gref, ref);
222 av_frame_ref(s->gmain, master);
226 pthread_cond_signal(&s->cond);
227 pthread_mutex_unlock(&s->lock);
229 return ff_filter_frame(ctx->outputs[0], master);
232 static av_cold int init(AVFilterContext *ctx)
234 LIBVMAFContext *s = ctx->priv;
236 s->gref = av_frame_alloc();
237 s->gmain = av_frame_alloc();
238 if (!s->gref || !s->gmain)
239 return AVERROR(ENOMEM);
243 s->vmaf_thread_created = 0;
244 pthread_mutex_init(&s->lock, NULL);
245 pthread_cond_init (&s->cond, NULL);
247 s->fs.on_event = do_vmaf;
251 static int query_formats(AVFilterContext *ctx)
253 static const enum AVPixelFormat pix_fmts[] = {
254 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
255 AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV420P10LE,
259 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
261 return AVERROR(ENOMEM);
262 return ff_set_common_formats(ctx, fmts_list);
266 static int config_input_ref(AVFilterLink *inlink)
268 AVFilterContext *ctx = inlink->dst;
269 LIBVMAFContext *s = ctx->priv;
272 if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
273 ctx->inputs[0]->h != ctx->inputs[1]->h) {
274 av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
275 return AVERROR(EINVAL);
277 if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
278 av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
279 return AVERROR(EINVAL);
282 s->desc = av_pix_fmt_desc_get(inlink->format);
283 s->width = ctx->inputs[0]->w;
284 s->height = ctx->inputs[0]->h;
286 th = pthread_create(&s->vmaf_thread, NULL, call_vmaf, (void *) s);
288 av_log(ctx, AV_LOG_ERROR, "Thread creation failed.\n");
289 return AVERROR(EINVAL);
291 s->vmaf_thread_created = 1;
296 static int config_output(AVFilterLink *outlink)
298 AVFilterContext *ctx = outlink->src;
299 LIBVMAFContext *s = ctx->priv;
300 AVFilterLink *mainlink = ctx->inputs[0];
303 ret = ff_framesync_init_dualinput(&s->fs, ctx);
306 outlink->w = mainlink->w;
307 outlink->h = mainlink->h;
308 outlink->time_base = mainlink->time_base;
309 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
310 outlink->frame_rate = mainlink->frame_rate;
311 if ((ret = ff_framesync_configure(&s->fs)) < 0)
317 static int activate(AVFilterContext *ctx)
319 LIBVMAFContext *s = ctx->priv;
320 return ff_framesync_activate(&s->fs);
323 static av_cold void uninit(AVFilterContext *ctx)
325 LIBVMAFContext *s = ctx->priv;
327 ff_framesync_uninit(&s->fs);
329 pthread_mutex_lock(&s->lock);
331 pthread_cond_signal(&s->cond);
332 pthread_mutex_unlock(&s->lock);
334 if (s->vmaf_thread_created)
336 pthread_join(s->vmaf_thread, NULL);
337 s->vmaf_thread_created = 0;
340 av_frame_free(&s->gref);
341 av_frame_free(&s->gmain);
343 pthread_mutex_destroy(&s->lock);
344 pthread_cond_destroy(&s->cond);
347 static const AVFilterPad libvmaf_inputs[] = {
350 .type = AVMEDIA_TYPE_VIDEO,
353 .type = AVMEDIA_TYPE_VIDEO,
354 .config_props = config_input_ref,
359 static const AVFilterPad libvmaf_outputs[] = {
362 .type = AVMEDIA_TYPE_VIDEO,
363 .config_props = config_output,
368 const AVFilter ff_vf_libvmaf = {
370 .description = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
371 .preinit = libvmaf_framesync_preinit,
374 .query_formats = query_formats,
375 .activate = activate,
376 .priv_size = sizeof(LIBVMAFContext),
377 .priv_class = &libvmaf_class,
378 .inputs = libvmaf_inputs,
379 .outputs = libvmaf_outputs,