vf_libvmaf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. * @file
  23. * Calculate the VMAF between two input videos.
  24. */
  25. #include <pthread.h>
  26. #include <libvmaf.h>
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "drawutils.h"
  32. #include "formats.h"
  33. #include "framesync.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. typedef struct LIBVMAFContext {
  37. const AVClass *class;
  38. FFFrameSync fs;
  39. const AVPixFmtDescriptor *desc;
  40. int width;
  41. int height;
  42. double vmaf_score;
  43. int vmaf_thread_created;
  44. pthread_t vmaf_thread;
  45. pthread_mutex_t lock;
  46. pthread_cond_t cond;
  47. int eof;
  48. AVFrame *gmain;
  49. AVFrame *gref;
  50. int frame_set;
  51. char *model_path;
  52. char *log_path;
  53. char *log_fmt;
  54. int disable_clip;
  55. int disable_avx;
  56. int enable_transform;
  57. int phone_model;
  58. int psnr;
  59. int ssim;
  60. int ms_ssim;
  61. char *pool;
  62. int n_threads;
  63. int n_subsample;
  64. int enable_conf_interval;
  65. int error;
  66. } LIBVMAFContext;
  67. #define OFFSET(x) offsetof(LIBVMAFContext, x)
  68. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  69. static const AVOption libvmaf_options[] = {
  70. {"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},
  71. {"log_path", "Set the file path to be used to store logs.", OFFSET(log_path), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
  72. {"log_fmt", "Set the format of the log (xml or json).", OFFSET(log_fmt), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
  73. {"enable_transform", "Enables transform for computing vmaf.", OFFSET(enable_transform), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  74. {"phone_model", "Invokes the phone model that will generate higher VMAF scores.", OFFSET(phone_model), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  75. {"psnr", "Enables computing psnr along with vmaf.", OFFSET(psnr), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  76. {"ssim", "Enables computing ssim along with vmaf.", OFFSET(ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  77. {"ms_ssim", "Enables computing ms-ssim along with vmaf.", OFFSET(ms_ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  78. {"pool", "Set the pool method to be used for computing vmaf.", OFFSET(pool), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
  79. {"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},
  80. {"n_subsample", "Set interval for frame subsampling used when computing vmaf.", OFFSET(n_subsample), AV_OPT_TYPE_INT, {.i64=1}, 1, UINT_MAX, FLAGS},
  81. {"enable_conf_interval", "Enables confidence interval.", OFFSET(enable_conf_interval), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  82. { NULL }
  83. };
  84. 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) \
  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. av_frame_unref(s->gref); \
  134. av_frame_unref(s->gmain); \
  135. s->frame_set = 0; \
  136. \
  137. pthread_cond_signal(&s->cond); \
  138. pthread_mutex_unlock(&s->lock); \
  139. \
  140. if (ret) { \
  141. return 2; \
  142. } \
  143. \
  144. return 0; \
  145. }
  146. read_frame_fn(uint8_t, 8);
  147. read_frame_fn(uint16_t, 10);
  148. static void compute_vmaf_score(LIBVMAFContext *s)
  149. {
  150. int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
  151. int stride, void *ctx);
  152. char *format;
  153. if (s->desc->comp[0].depth <= 8) {
  154. read_frame = read_frame_8bit;
  155. } else {
  156. read_frame = read_frame_10bit;
  157. }
  158. format = (char *) s->desc->name;
  159. s->error = compute_vmaf(&s->vmaf_score, format, s->width, s->height,
  160. read_frame, s, s->model_path, s->log_path,
  161. s->log_fmt, 0, 0, s->enable_transform,
  162. s->phone_model, s->psnr, s->ssim,
  163. s->ms_ssim, s->pool,
  164. s->n_threads, s->n_subsample, s->enable_conf_interval);
  165. }
  166. static void *call_vmaf(void *ctx)
  167. {
  168. LIBVMAFContext *s = (LIBVMAFContext *) ctx;
  169. compute_vmaf_score(s);
  170. if (!s->error) {
  171. av_log(ctx, AV_LOG_INFO, "VMAF score: %f\n",s->vmaf_score);
  172. } else {
  173. pthread_mutex_lock(&s->lock);
  174. pthread_cond_signal(&s->cond);
  175. pthread_mutex_unlock(&s->lock);
  176. }
  177. pthread_exit(NULL);
  178. return NULL;
  179. }
  180. static int do_vmaf(FFFrameSync *fs)
  181. {
  182. AVFilterContext *ctx = fs->parent;
  183. LIBVMAFContext *s = ctx->priv;
  184. AVFrame *master, *ref;
  185. int ret;
  186. ret = ff_framesync_dualinput_get(fs, &master, &ref);
  187. if (ret < 0)
  188. return ret;
  189. if (!ref)
  190. return ff_filter_frame(ctx->outputs[0], master);
  191. pthread_mutex_lock(&s->lock);
  192. while (s->frame_set && !s->error) {
  193. pthread_cond_wait(&s->cond, &s->lock);
  194. }
  195. if (s->error) {
  196. av_log(ctx, AV_LOG_ERROR,
  197. "libvmaf encountered an error, check log for details\n");
  198. pthread_mutex_unlock(&s->lock);
  199. return AVERROR(EINVAL);
  200. }
  201. av_frame_ref(s->gref, ref);
  202. av_frame_ref(s->gmain, master);
  203. s->frame_set = 1;
  204. pthread_cond_signal(&s->cond);
  205. pthread_mutex_unlock(&s->lock);
  206. return ff_filter_frame(ctx->outputs[0], master);
  207. }
  208. static av_cold int init(AVFilterContext *ctx)
  209. {
  210. LIBVMAFContext *s = ctx->priv;
  211. s->gref = av_frame_alloc();
  212. s->gmain = av_frame_alloc();
  213. s->error = 0;
  214. s->vmaf_thread_created = 0;
  215. pthread_mutex_init(&s->lock, NULL);
  216. pthread_cond_init (&s->cond, NULL);
  217. s->fs.on_event = do_vmaf;
  218. return 0;
  219. }
  220. static int query_formats(AVFilterContext *ctx)
  221. {
  222. static const enum AVPixelFormat pix_fmts[] = {
  223. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  224. AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV420P10LE,
  225. AV_PIX_FMT_NONE
  226. };
  227. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  228. if (!fmts_list)
  229. return AVERROR(ENOMEM);
  230. return ff_set_common_formats(ctx, fmts_list);
  231. }
  232. static int config_input_ref(AVFilterLink *inlink)
  233. {
  234. AVFilterContext *ctx = inlink->dst;
  235. LIBVMAFContext *s = ctx->priv;
  236. int th;
  237. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  238. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  239. av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
  240. return AVERROR(EINVAL);
  241. }
  242. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  243. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  244. return AVERROR(EINVAL);
  245. }
  246. s->desc = av_pix_fmt_desc_get(inlink->format);
  247. s->width = ctx->inputs[0]->w;
  248. s->height = ctx->inputs[0]->h;
  249. th = pthread_create(&s->vmaf_thread, NULL, call_vmaf, (void *) s);
  250. if (th) {
  251. av_log(ctx, AV_LOG_ERROR, "Thread creation failed.\n");
  252. return AVERROR(EINVAL);
  253. }
  254. s->vmaf_thread_created = 1;
  255. return 0;
  256. }
  257. static int config_output(AVFilterLink *outlink)
  258. {
  259. AVFilterContext *ctx = outlink->src;
  260. LIBVMAFContext *s = ctx->priv;
  261. AVFilterLink *mainlink = ctx->inputs[0];
  262. int ret;
  263. ret = ff_framesync_init_dualinput(&s->fs, ctx);
  264. if (ret < 0)
  265. return ret;
  266. outlink->w = mainlink->w;
  267. outlink->h = mainlink->h;
  268. outlink->time_base = mainlink->time_base;
  269. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  270. outlink->frame_rate = mainlink->frame_rate;
  271. if ((ret = ff_framesync_configure(&s->fs)) < 0)
  272. return ret;
  273. return 0;
  274. }
  275. static int activate(AVFilterContext *ctx)
  276. {
  277. LIBVMAFContext *s = ctx->priv;
  278. return ff_framesync_activate(&s->fs);
  279. }
  280. static av_cold void uninit(AVFilterContext *ctx)
  281. {
  282. LIBVMAFContext *s = ctx->priv;
  283. ff_framesync_uninit(&s->fs);
  284. pthread_mutex_lock(&s->lock);
  285. s->eof = 1;
  286. pthread_cond_signal(&s->cond);
  287. pthread_mutex_unlock(&s->lock);
  288. if (s->vmaf_thread_created)
  289. {
  290. pthread_join(s->vmaf_thread, NULL);
  291. s->vmaf_thread_created = 0;
  292. }
  293. av_frame_free(&s->gref);
  294. av_frame_free(&s->gmain);
  295. pthread_mutex_destroy(&s->lock);
  296. pthread_cond_destroy(&s->cond);
  297. }
  298. static const AVFilterPad libvmaf_inputs[] = {
  299. {
  300. .name = "main",
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. },{
  303. .name = "reference",
  304. .type = AVMEDIA_TYPE_VIDEO,
  305. .config_props = config_input_ref,
  306. },
  307. { NULL }
  308. };
  309. static const AVFilterPad libvmaf_outputs[] = {
  310. {
  311. .name = "default",
  312. .type = AVMEDIA_TYPE_VIDEO,
  313. .config_props = config_output,
  314. },
  315. { NULL }
  316. };
  317. AVFilter ff_vf_libvmaf = {
  318. .name = "libvmaf",
  319. .description = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
  320. .preinit = libvmaf_framesync_preinit,
  321. .init = init,
  322. .uninit = uninit,
  323. .query_formats = query_formats,
  324. .activate = activate,
  325. .priv_size = sizeof(LIBVMAFContext),
  326. .priv_class = &libvmaf_class,
  327. .inputs = libvmaf_inputs,
  328. .outputs = libvmaf_outputs,
  329. };