FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <pthread.h>
28 #include <libvmaf.h>
29 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "avfilter.h"
33 #include "drawutils.h"
34 #include "formats.h"
35 #include "framesync.h"
36 #include "internal.h"
37 #include "video.h"
38 
39 typedef struct LIBVMAFContext {
40  const AVClass *class;
43  char *format;
44  int width;
45  int height;
46  double vmaf_score;
50  int eof;
53  int frame_set;
54  char *model_path;
55  char *log_path;
56  char *log_fmt;
61  int psnr;
62  int ssim;
63  int ms_ssim;
64  char *pool;
66 
67 #define OFFSET(x) offsetof(LIBVMAFContext, x)
68 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
69 
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},
80  { NULL }
81 };
82 
84 
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) \
88 { \
89  LIBVMAFContext *s = (LIBVMAFContext *) ctx; \
90  int ret; \
91  \
92  pthread_mutex_lock(&s->lock); \
93  \
94  while (!s->frame_set && !s->eof) { \
95  pthread_cond_wait(&s->cond, &s->lock); \
96  } \
97  \
98  if (s->frame_set) { \
99  int ref_stride = s->gref->linesize[0]; \
100  int main_stride = s->gmain->linesize[0]; \
101  \
102  const type *ref_ptr = (const type *) s->gref->data[0]; \
103  const type *main_ptr = (const type *) s->gmain->data[0]; \
104  \
105  float *ptr = ref_data; \
106  \
107  int h = s->height; \
108  int w = s->width; \
109  \
110  int i,j; \
111  \
112  for (i = 0; i < h; i++) { \
113  for ( j = 0; j < w; j++) { \
114  ptr[j] = (float)ref_ptr[j]; \
115  } \
116  ref_ptr += ref_stride / sizeof(*ref_ptr); \
117  ptr += stride / sizeof(*ptr); \
118  } \
119  \
120  ptr = main_data; \
121  \
122  for (i = 0; i < h; i++) { \
123  for (j = 0; j < w; j++) { \
124  ptr[j] = (float)main_ptr[j]; \
125  } \
126  main_ptr += main_stride / sizeof(*main_ptr); \
127  ptr += stride / sizeof(*ptr); \
128  } \
129  } \
130  \
131  ret = !s->frame_set; \
132  \
133  s->frame_set = 0; \
134  \
135  pthread_cond_signal(&s->cond); \
136  pthread_mutex_unlock(&s->lock); \
137  \
138  if (ret) { \
139  return 2; \
140  } \
141  \
142  return 0; \
143 }
144 
146 read_frame_fn(uint16_t, 10);
147 
149 {
150  int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
151  int stride, void *ctx);
152 
153  if (s->desc->comp[0].depth <= 8) {
154  read_frame = read_frame_8bit;
155  } else {
156  read_frame = read_frame_10bit;
157  }
158 
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);
163 }
164 
165 static void *call_vmaf(void *ctx)
166 {
167  LIBVMAFContext *s = (LIBVMAFContext *) ctx;
169  av_log(ctx, AV_LOG_INFO, "VMAF score: %f\n",s->vmaf_score);
170  pthread_exit(NULL);
171 }
172 
173 static int do_vmaf(FFFrameSync *fs)
174 {
175  AVFilterContext *ctx = fs->parent;
176  LIBVMAFContext *s = ctx->priv;
177  AVFrame *main, *ref;
178  int ret;
179 
180  ret = ff_framesync_dualinput_get(fs, &main, &ref);
181  if (ret < 0)
182  return ret;
183  if (!ref)
184  return ff_filter_frame(ctx->outputs[0], main);
185 
187 
188  while (s->frame_set != 0) {
189  pthread_cond_wait(&s->cond, &s->lock);
190  }
191 
192  av_frame_ref(s->gref, ref);
193  av_frame_ref(s->gmain, main);
194 
195  s->frame_set = 1;
196 
199 
200  return ff_filter_frame(ctx->outputs[0], main);
201 }
202 
204 {
205  LIBVMAFContext *s = ctx->priv;
206 
207  s->gref = av_frame_alloc();
208  s->gmain = av_frame_alloc();
209 
211  pthread_cond_init (&s->cond, NULL);
212 
213  s->fs.on_event = do_vmaf;
214  return 0;
215 }
216 
218 {
219  static const enum AVPixelFormat pix_fmts[] = {
223  };
224 
225  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
226  if (!fmts_list)
227  return AVERROR(ENOMEM);
228  return ff_set_common_formats(ctx, fmts_list);
229 }
230 
231 
232 static int config_input_ref(AVFilterLink *inlink)
233 {
234  AVFilterContext *ctx = inlink->dst;
235  LIBVMAFContext *s = ctx->priv;
236  int th;
237 
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->desc = av_pix_fmt_desc_get(inlink->format);
249  s->width = ctx->inputs[0]->w;
250  s->height = ctx->inputs[0]->h;
251 
252  th = pthread_create(&s->vmaf_thread, NULL, call_vmaf, (void *) s);
253  if (th) {
254  av_log(ctx, AV_LOG_ERROR, "Thread creation failed.\n");
255  return AVERROR(EINVAL);
256  }
257 
258  return 0;
259 }
260 
261 
262 static int config_output(AVFilterLink *outlink)
263 {
264  AVFilterContext *ctx = outlink->src;
265  LIBVMAFContext *s = ctx->priv;
266  AVFilterLink *mainlink = ctx->inputs[0];
267  int ret;
268 
269  ret = ff_framesync_init_dualinput(&s->fs, ctx);
270  if (ret < 0)
271  return ret;
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)
278  return ret;
279 
280  return 0;
281 }
282 
284 {
285  LIBVMAFContext *s = ctx->priv;
286  return ff_framesync_activate(&s->fs);
287 }
288 
290 {
291  LIBVMAFContext *s = ctx->priv;
292 
293  ff_framesync_uninit(&s->fs);
294 
296  s->eof = 1;
299 
301 
302  av_frame_free(&s->gref);
303  av_frame_free(&s->gmain);
304 
307 }
308 
309 static const AVFilterPad libvmaf_inputs[] = {
310  {
311  .name = "main",
312  .type = AVMEDIA_TYPE_VIDEO,
313  },{
314  .name = "reference",
315  .type = AVMEDIA_TYPE_VIDEO,
316  .config_props = config_input_ref,
317  },
318  { NULL }
319 };
320 
321 static const AVFilterPad libvmaf_outputs[] = {
322  {
323  .name = "default",
324  .type = AVMEDIA_TYPE_VIDEO,
325  .config_props = config_output,
326  },
327  { NULL }
328 };
329 
331  .name = "libvmaf",
332  .description = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
333  .preinit = libvmaf_framesync_preinit,
334  .init = init,
335  .uninit = uninit,
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,
342 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:106
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2419
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
#define pthread_mutex_lock(a)
Definition: ffprobe.c:61
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:164
static int do_vmaf(FFFrameSync *fs)
Definition: vf_libvmaf.c:173
AVOption.
Definition: opt.h:246
static int query_formats(AVFilterContext *ctx)
Definition: vf_libvmaf.c:217
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), little-endian
Definition: pixfmt.h:170
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
Main libavfilter public API header.
const AVPixFmtDescriptor * desc
Definition: vf_libvmaf.c:42
int(* on_event)(struct FFFrameSync *fs)
Callback called when a frame event is ready.
Definition: framesync.h:172
static const AVFilterPad libvmaf_outputs[]
Definition: vf_libvmaf.c:321
AVFilter ff_vf_libvmaf
Definition: vf_libvmaf.c:330
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:117
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:138
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
AVFilterContext * parent
Parent filter context.
Definition: framesync.h:152
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1151
HMTX pthread_mutex_t
Definition: os2threads.h:49
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
AVOptions.
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
Definition: framesync.c:361
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Definition: framesync.c:379
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:395
static double psnr(double d)
Definition: ffmpeg.c:1354
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:146
int main(int argc, char *argv[])
Definition: avio_dir_cmd.c:134
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition: pixfmt.h:176
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:293
Frame sync structure.
Definition: framesync.h:146
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void * priv
private data for use by the filter
Definition: avfilter.h:353
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition: pixfmt.h:172
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:344
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
static const AVOption libvmaf_options[]
Definition: vf_libvmaf.c:70
pthread_cond_t cond
Definition: vf_libvmaf.c:49
#define th
Definition: regdef.h:75
#define OFFSET(x)
Definition: vf_libvmaf.c:67
static av_cold int init(AVFilterContext *ctx)
Definition: vf_libvmaf.c:203
#define read_frame_fn(type, bits)
Definition: vf_libvmaf.c:85
AVFormatContext * ctx
Definition: movenc.c:48
pthread_mutex_t lock
Definition: vf_libvmaf.c:48
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:88
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:98
static int activate(AVFilterContext *ctx)
Definition: vf_libvmaf.c:283
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:389
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:65
char * log_path
Definition: vf_libvmaf.c:55
AVFrame * gref
Definition: vf_libvmaf.c:52
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_libvmaf.c:289
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:74
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:379
misc drawing utilities
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
char * format
Definition: vf_libvmaf.c:43
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
#define FLAGS
Definition: vf_libvmaf.c:68
static const AVFilterPad libvmaf_inputs[]
Definition: vf_libvmaf.c:309
const char * name
Filter name.
Definition: avfilter.h:148
AVFrame * gmain
Definition: vf_libvmaf.c:51
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt, uint8_t block_type, AVFormatContext *s)
Definition: bethsoftvid.c:96
FFFrameSync fs
Definition: vf_libvmaf.c:41
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static void compute_vmaf_score(LIBVMAFContext *s)
Definition: vf_libvmaf.c:148
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
static void * call_vmaf(void *ctx)
Definition: vf_libvmaf.c:165
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:127
pthread_t vmaf_thread
Definition: vf_libvmaf.c:47
FRAMESYNC_DEFINE_CLASS(libvmaf, LIBVMAFContext, fs)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
int enable_transform
Definition: vf_libvmaf.c:59
An instance of a filter.
Definition: avfilter.h:338
static int config_output(AVFilterLink *outlink)
Definition: vf_libvmaf.c:262
char * model_path
Definition: vf_libvmaf.c:54
double vmaf_score
Definition: vf_libvmaf.c:46
#define stride
internal API functions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
static int config_input_ref(AVFilterLink *inlink)
Definition: vf_libvmaf.c:232
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
char * log_fmt
Definition: vf_libvmaf.c:56