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;
47 pthread_t vmaf_thread;
67 #define OFFSET(x) offsetof(LIBVMAFContext, x)
68 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
70 static const AVOption libvmaf_options[] = {
71 {"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},
72 {"log_path", "Set the file path to be used to store logs.", OFFSET(log_path), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
73 {"log_fmt", "Set the format of the log (xml or json).", OFFSET(log_fmt), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
74 {"enable_transform", "Enables transform for computing vmaf.", OFFSET(enable_transform), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
75 {"phone_model", "Invokes the phone model that will generate higher VMAF scores.", OFFSET(phone_model), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
76 {"psnr", "Enables computing psnr along with vmaf.", OFFSET(psnr), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
77 {"ssim", "Enables computing ssim along with vmaf.", OFFSET(ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
78 {"ms_ssim", "Enables computing ms-ssim along with vmaf.", OFFSET(ms_ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
79 {"pool", "Set the pool method to be used for computing vmaf.", OFFSET(pool), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
83 FRAMESYNC_DEFINE_CLASS(libvmaf, LIBVMAFContext, fs);
85 #define read_frame_fn(type, bits) \
86 static int read_frame_##bits##bit(float *ref_data, float *main_data, \
87 float *temp_data, int stride, void *ctx) \
89 LIBVMAFContext *s = (LIBVMAFContext *) ctx; \
92 pthread_mutex_lock(&s->lock); \
94 while (!s->frame_set && !s->eof) { \
95 pthread_cond_wait(&s->cond, &s->lock); \
99 int ref_stride = s->gref->linesize[0]; \
100 int main_stride = s->gmain->linesize[0]; \
102 const type *ref_ptr = (const type *) s->gref->data[0]; \
103 const type *main_ptr = (const type *) s->gmain->data[0]; \
105 float *ptr = ref_data; \
112 for (i = 0; i < h; i++) { \
113 for ( j = 0; j < w; j++) { \
114 ptr[j] = (float)ref_ptr[j]; \
116 ref_ptr += ref_stride / sizeof(*ref_ptr); \
117 ptr += stride / sizeof(*ptr); \
122 for (i = 0; i < h; i++) { \
123 for (j = 0; j < w; j++) { \
124 ptr[j] = (float)main_ptr[j]; \
126 main_ptr += main_stride / sizeof(*main_ptr); \
127 ptr += stride / sizeof(*ptr); \
131 ret = !s->frame_set; \
135 pthread_cond_signal(&s->cond); \
136 pthread_mutex_unlock(&s->lock); \
145 read_frame_fn(uint8_t, 8);
146 read_frame_fn(uint16_t, 10);
148 static void compute_vmaf_score(LIBVMAFContext *s)
150 int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
151 int stride, void *ctx);
153 if (s->desc->comp[0].depth <= 8) {
154 read_frame = read_frame_8bit;
156 read_frame = read_frame_10bit;
159 s->vmaf_score = compute_vmaf(s->format, s->width, s->height, read_frame, s,
160 s->model_path, s->log_path, s->log_fmt, 0, 0,
161 s->enable_transform, s->phone_model, s->psnr,
162 s->ssim, s->ms_ssim, s->pool);
165 static void *call_vmaf(void *ctx)
167 LIBVMAFContext *s = (LIBVMAFContext *) ctx;
168 compute_vmaf_score(s);
169 av_log(ctx, AV_LOG_INFO, "VMAF score: %f\n",s->vmaf_score);
173 static int do_vmaf(FFFrameSync *fs)
175 AVFilterContext *ctx = fs->parent;
176 LIBVMAFContext *s = ctx->priv;
180 ret = ff_framesync_dualinput_get(fs, &main, &ref);
184 return ff_filter_frame(ctx->outputs[0], main);
186 pthread_mutex_lock(&s->lock);
188 while (s->frame_set != 0) {
189 pthread_cond_wait(&s->cond, &s->lock);
192 av_frame_ref(s->gref, ref);
193 av_frame_ref(s->gmain, main);
197 pthread_cond_signal(&s->cond);
198 pthread_mutex_unlock(&s->lock);
200 return ff_filter_frame(ctx->outputs[0], main);
203 static av_cold int init(AVFilterContext *ctx)
205 LIBVMAFContext *s = ctx->priv;
207 s->gref = av_frame_alloc();
208 s->gmain = av_frame_alloc();
210 pthread_mutex_init(&s->lock, NULL);
211 pthread_cond_init (&s->cond, NULL);
213 s->fs.on_event = do_vmaf;
217 static int query_formats(AVFilterContext *ctx)
219 static const enum AVPixelFormat pix_fmts[] = {
220 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
221 AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV420P10LE,
225 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
227 return AVERROR(ENOMEM);
228 return ff_set_common_formats(ctx, fmts_list);
232 static int config_input_ref(AVFilterLink *inlink)
234 AVFilterContext *ctx = inlink->dst;
235 LIBVMAFContext *s = ctx->priv;
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);
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);
248 s->desc = av_pix_fmt_desc_get(inlink->format);
249 s->width = ctx->inputs[0]->w;
250 s->height = ctx->inputs[0]->h;
252 th = pthread_create(&s->vmaf_thread, NULL, call_vmaf, (void *) s);
254 av_log(ctx, AV_LOG_ERROR, "Thread creation failed.\n");
255 return AVERROR(EINVAL);
262 static int config_output(AVFilterLink *outlink)
264 AVFilterContext *ctx = outlink->src;
265 LIBVMAFContext *s = ctx->priv;
266 AVFilterLink *mainlink = ctx->inputs[0];
269 ret = ff_framesync_init_dualinput(&s->fs, ctx);
272 outlink->w = mainlink->w;
273 outlink->h = mainlink->h;
274 outlink->time_base = mainlink->time_base;
275 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
276 outlink->frame_rate = mainlink->frame_rate;
277 if ((ret = ff_framesync_configure(&s->fs)) < 0)
283 static int activate(AVFilterContext *ctx)
285 LIBVMAFContext *s = ctx->priv;
286 return ff_framesync_activate(&s->fs);
289 static av_cold void uninit(AVFilterContext *ctx)
291 LIBVMAFContext *s = ctx->priv;
293 ff_framesync_uninit(&s->fs);
295 pthread_mutex_lock(&s->lock);
297 pthread_cond_signal(&s->cond);
298 pthread_mutex_unlock(&s->lock);
300 pthread_join(s->vmaf_thread, NULL);
302 av_frame_free(&s->gref);
303 av_frame_free(&s->gmain);
305 pthread_mutex_destroy(&s->lock);
306 pthread_cond_destroy(&s->cond);
309 static const AVFilterPad libvmaf_inputs[] = {
312 .type = AVMEDIA_TYPE_VIDEO,
315 .type = AVMEDIA_TYPE_VIDEO,
316 .config_props = config_input_ref,
321 static const AVFilterPad libvmaf_outputs[] = {
324 .type = AVMEDIA_TYPE_VIDEO,
325 .config_props = config_output,
330 AVFilter ff_vf_libvmaf = {
332 .description = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
333 .preinit = libvmaf_framesync_preinit,
336 .query_formats = query_formats,
337 .activate = activate,
338 .priv_size = sizeof(LIBVMAFContext),
339 .priv_class = &libvmaf_class,
340 .inputs = libvmaf_inputs,
341 .outputs = libvmaf_outputs,