]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_libvmaf.c
lavf/bluray: translate a read of 0 to EOF
[ffmpeg] / libavfilter / vf_libvmaf.c
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;
41     FFFrameSync fs;
42     const AVPixFmtDescriptor *desc;
43     int width;
44     int height;
45     double vmaf_score;
46     int vmaf_thread_created;
47     pthread_t vmaf_thread;
48     pthread_mutex_t lock;
49     pthread_cond_t cond;
50     int eof;
51     AVFrame *gmain;
52     AVFrame *gref;
53     int frame_set;
54     char *model_path;
55     char *log_path;
56     char *log_fmt;
57     int disable_clip;
58     int disable_avx;
59     int enable_transform;
60     int phone_model;
61     int psnr;
62     int ssim;
63     int ms_ssim;
64     char *pool;
65     int error;
66 } LIBVMAFContext;
67
68 #define OFFSET(x) offsetof(LIBVMAFContext, x)
69 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
70
71 static const AVOption libvmaf_options[] = {
72     {"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},
73     {"log_path",  "Set the file path to be used to store logs.",                        OFFSET(log_path), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
74     {"log_fmt",  "Set the format of the log (xml or json).",                            OFFSET(log_fmt), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
75     {"enable_transform",  "Enables transform for computing vmaf.",                      OFFSET(enable_transform), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
76     {"phone_model",  "Invokes the phone model that will generate higher VMAF scores.",  OFFSET(phone_model), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
77     {"psnr",  "Enables computing psnr along with vmaf.",                                OFFSET(psnr), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
78     {"ssim",  "Enables computing ssim along with vmaf.",                                OFFSET(ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
79     {"ms_ssim",  "Enables computing ms-ssim along with vmaf.",                          OFFSET(ms_ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
80     {"pool",  "Set the pool method to be used for computing vmaf.",                     OFFSET(pool), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
81     { NULL }
82 };
83
84 FRAMESYNC_DEFINE_CLASS(libvmaf, LIBVMAFContext, fs);
85
86 #define read_frame_fn(type, bits)                                               \
87     static int read_frame_##bits##bit(float *ref_data, float *main_data,        \
88                                       float *temp_data, int stride, void *ctx)  \
89 {                                                                               \
90     LIBVMAFContext *s = (LIBVMAFContext *) ctx;                                 \
91     int ret;                                                                    \
92     \
93     pthread_mutex_lock(&s->lock);                                               \
94     \
95     while (!s->frame_set && !s->eof) {                                          \
96         pthread_cond_wait(&s->cond, &s->lock);                                  \
97     }                                                                           \
98     \
99     if (s->frame_set) {                                                         \
100         int ref_stride = s->gref->linesize[0];                                  \
101         int main_stride = s->gmain->linesize[0];                                \
102         \
103         const type *ref_ptr = (const type *) s->gref->data[0];                  \
104         const type *main_ptr = (const type *) s->gmain->data[0];                \
105         \
106         float *ptr = ref_data;                                                  \
107         \
108         int h = s->height;                                                      \
109         int w = s->width;                                                       \
110         \
111         int i,j;                                                                \
112         \
113         for (i = 0; i < h; i++) {                                               \
114             for ( j = 0; j < w; j++) {                                          \
115                 ptr[j] = (float)ref_ptr[j];                                     \
116             }                                                                   \
117             ref_ptr += ref_stride / sizeof(*ref_ptr);                           \
118             ptr += stride / sizeof(*ptr);                                       \
119         }                                                                       \
120         \
121         ptr = main_data;                                                        \
122         \
123         for (i = 0; i < h; i++) {                                               \
124             for (j = 0; j < w; j++) {                                           \
125                 ptr[j] = (float)main_ptr[j];                                    \
126             }                                                                   \
127             main_ptr += main_stride / sizeof(*main_ptr);                        \
128             ptr += stride / sizeof(*ptr);                                       \
129         }                                                                       \
130     }                                                                           \
131     \
132     ret = !s->frame_set;                                                        \
133     \
134     av_frame_unref(s->gref);                                                    \
135     av_frame_unref(s->gmain);                                                   \
136     s->frame_set = 0;                                                           \
137     \
138     pthread_cond_signal(&s->cond);                                              \
139     pthread_mutex_unlock(&s->lock);                                             \
140     \
141     if (ret) {                                                                  \
142         return 2;                                                               \
143     }                                                                           \
144     \
145     return 0;                                                                   \
146 }
147
148 read_frame_fn(uint8_t, 8);
149 read_frame_fn(uint16_t, 10);
150
151 static void compute_vmaf_score(LIBVMAFContext *s)
152 {
153     int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
154                       int stride, void *ctx);
155     char *format;
156
157     if (s->desc->comp[0].depth <= 8) {
158         read_frame = read_frame_8bit;
159     } else {
160         read_frame = read_frame_10bit;
161     }
162
163     format = (char *) s->desc->name;
164
165     s->error = compute_vmaf(&s->vmaf_score, format, s->width, s->height,
166                             read_frame, s, s->model_path, s->log_path,
167                             s->log_fmt, 0, 0, s->enable_transform,
168                             s->phone_model, s->psnr, s->ssim,
169                             s->ms_ssim, s->pool);
170 }
171
172 static void *call_vmaf(void *ctx)
173 {
174     LIBVMAFContext *s = (LIBVMAFContext *) ctx;
175     compute_vmaf_score(s);
176     if (!s->error) {
177         av_log(ctx, AV_LOG_INFO, "VMAF score: %f\n",s->vmaf_score);
178     } else {
179         pthread_mutex_lock(&s->lock);
180         pthread_cond_signal(&s->cond);
181         pthread_mutex_unlock(&s->lock);
182     }
183     pthread_exit(NULL);
184     return NULL;
185 }
186
187 static int do_vmaf(FFFrameSync *fs)
188 {
189     AVFilterContext *ctx = fs->parent;
190     LIBVMAFContext *s = ctx->priv;
191     AVFrame *master, *ref;
192     int ret;
193
194     ret = ff_framesync_dualinput_get(fs, &master, &ref);
195     if (ret < 0)
196         return ret;
197     if (!ref)
198         return ff_filter_frame(ctx->outputs[0], master);
199
200     pthread_mutex_lock(&s->lock);
201
202     while (s->frame_set && !s->error) {
203         pthread_cond_wait(&s->cond, &s->lock);
204     }
205
206     if (s->error) {
207         av_log(ctx, AV_LOG_ERROR,
208                "libvmaf encountered an error, check log for details\n");
209         pthread_mutex_unlock(&s->lock);
210         return AVERROR(EINVAL);
211     }
212
213     av_frame_ref(s->gref, ref);
214     av_frame_ref(s->gmain, master);
215
216     s->frame_set = 1;
217
218     pthread_cond_signal(&s->cond);
219     pthread_mutex_unlock(&s->lock);
220
221     return ff_filter_frame(ctx->outputs[0], master);
222 }
223
224 static av_cold int init(AVFilterContext *ctx)
225 {
226     LIBVMAFContext *s = ctx->priv;
227
228     s->gref = av_frame_alloc();
229     s->gmain = av_frame_alloc();
230     s->error = 0;
231
232     s->vmaf_thread_created = 0;
233     pthread_mutex_init(&s->lock, NULL);
234     pthread_cond_init (&s->cond, NULL);
235
236     s->fs.on_event = do_vmaf;
237     return 0;
238 }
239
240 static int query_formats(AVFilterContext *ctx)
241 {
242     static const enum AVPixelFormat pix_fmts[] = {
243         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
244         AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV420P10LE,
245         AV_PIX_FMT_NONE
246     };
247
248     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
249     if (!fmts_list)
250         return AVERROR(ENOMEM);
251     return ff_set_common_formats(ctx, fmts_list);
252 }
253
254
255 static int config_input_ref(AVFilterLink *inlink)
256 {
257     AVFilterContext *ctx  = inlink->dst;
258     LIBVMAFContext *s = ctx->priv;
259     int th;
260
261     if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
262         ctx->inputs[0]->h != ctx->inputs[1]->h) {
263         av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
264         return AVERROR(EINVAL);
265     }
266     if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
267         av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
268         return AVERROR(EINVAL);
269     }
270
271     s->desc = av_pix_fmt_desc_get(inlink->format);
272     s->width = ctx->inputs[0]->w;
273     s->height = ctx->inputs[0]->h;
274
275     th = pthread_create(&s->vmaf_thread, NULL, call_vmaf, (void *) s);
276     if (th) {
277         av_log(ctx, AV_LOG_ERROR, "Thread creation failed.\n");
278         return AVERROR(EINVAL);
279     }
280     s->vmaf_thread_created = 1;
281
282     return 0;
283 }
284
285 static int config_output(AVFilterLink *outlink)
286 {
287     AVFilterContext *ctx = outlink->src;
288     LIBVMAFContext *s = ctx->priv;
289     AVFilterLink *mainlink = ctx->inputs[0];
290     int ret;
291
292     ret = ff_framesync_init_dualinput(&s->fs, ctx);
293     if (ret < 0)
294         return ret;
295     outlink->w = mainlink->w;
296     outlink->h = mainlink->h;
297     outlink->time_base = mainlink->time_base;
298     outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
299     outlink->frame_rate = mainlink->frame_rate;
300     if ((ret = ff_framesync_configure(&s->fs)) < 0)
301         return ret;
302
303     return 0;
304 }
305
306 static int activate(AVFilterContext *ctx)
307 {
308     LIBVMAFContext *s = ctx->priv;
309     return ff_framesync_activate(&s->fs);
310 }
311
312 static av_cold void uninit(AVFilterContext *ctx)
313 {
314     LIBVMAFContext *s = ctx->priv;
315
316     ff_framesync_uninit(&s->fs);
317
318     pthread_mutex_lock(&s->lock);
319     s->eof = 1;
320     pthread_cond_signal(&s->cond);
321     pthread_mutex_unlock(&s->lock);
322
323     if (s->vmaf_thread_created)
324     {
325         pthread_join(s->vmaf_thread, NULL);
326         s->vmaf_thread_created = 0;
327     }
328
329     av_frame_free(&s->gref);
330     av_frame_free(&s->gmain);
331
332     pthread_mutex_destroy(&s->lock);
333     pthread_cond_destroy(&s->cond);
334 }
335
336 static const AVFilterPad libvmaf_inputs[] = {
337     {
338         .name         = "main",
339         .type         = AVMEDIA_TYPE_VIDEO,
340     },{
341         .name         = "reference",
342         .type         = AVMEDIA_TYPE_VIDEO,
343         .config_props = config_input_ref,
344     },
345     { NULL }
346 };
347
348 static const AVFilterPad libvmaf_outputs[] = {
349     {
350         .name          = "default",
351         .type          = AVMEDIA_TYPE_VIDEO,
352         .config_props  = config_output,
353     },
354     { NULL }
355 };
356
357 AVFilter ff_vf_libvmaf = {
358     .name          = "libvmaf",
359     .description   = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
360     .preinit       = libvmaf_framesync_preinit,
361     .init          = init,
362     .uninit        = uninit,
363     .query_formats = query_formats,
364     .activate      = activate,
365     .priv_size     = sizeof(LIBVMAFContext),
366     .priv_class    = &libvmaf_class,
367     .inputs        = libvmaf_inputs,
368     .outputs       = libvmaf_outputs,
369 };