You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
12KB

  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 <inttypes.h>
  26. #include <pthread.h>
  27. #include <libvmaf.h>
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "dualinput.h"
  33. #include "drawutils.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. typedef struct LIBVMAFContext {
  38. const AVClass *class;
  39. FFDualInputContext dinput;
  40. const AVPixFmtDescriptor *desc;
  41. char *format;
  42. int width;
  43. int height;
  44. double vmaf_score;
  45. pthread_t vmaf_thread;
  46. pthread_mutex_t lock;
  47. pthread_cond_t cond;
  48. int eof;
  49. AVFrame *gmain;
  50. AVFrame *gref;
  51. int frame_set;
  52. char *model_path;
  53. char *log_path;
  54. char *log_fmt;
  55. int disable_clip;
  56. int disable_avx;
  57. int enable_transform;
  58. int phone_model;
  59. int psnr;
  60. int ssim;
  61. int ms_ssim;
  62. char *pool;
  63. } LIBVMAFContext;
  64. #define OFFSET(x) offsetof(LIBVMAFContext, x)
  65. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  66. static const AVOption libvmaf_options[] = {
  67. {"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},
  68. {"log_path", "Set the file path to be used to store logs.", OFFSET(log_path), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
  69. {"log_fmt", "Set the format of the log (xml or json).", OFFSET(log_fmt), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
  70. {"enable_transform", "Enables transform for computing vmaf.", OFFSET(enable_transform), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  71. {"phone_model", "Invokes the phone model that will generate higher VMAF scores.", OFFSET(phone_model), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  72. {"psnr", "Enables computing psnr along with vmaf.", OFFSET(psnr), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  73. {"ssim", "Enables computing ssim along with vmaf.", OFFSET(ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  74. {"ms_ssim", "Enables computing ms-ssim along with vmaf.", OFFSET(ms_ssim), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  75. {"pool", "Set the pool method to be used for computing vmaf.", OFFSET(pool), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS},
  76. { NULL }
  77. };
  78. AVFILTER_DEFINE_CLASS(libvmaf);
  79. #define read_frame_fn(type, bits) \
  80. static int read_frame_##bits##bit(float *ref_data, float *main_data, \
  81. float *temp_data, int stride, \
  82. double *score, void *ctx) \
  83. { \
  84. LIBVMAFContext *s = (LIBVMAFContext *) ctx; \
  85. int ret; \
  86. \
  87. pthread_mutex_lock(&s->lock); \
  88. \
  89. while (!s->frame_set && !s->eof) { \
  90. pthread_cond_wait(&s->cond, &s->lock); \
  91. } \
  92. \
  93. if (s->frame_set) { \
  94. int ref_stride = s->gref->linesize[0]; \
  95. int main_stride = s->gmain->linesize[0]; \
  96. \
  97. const type *ref_ptr = (const type *) s->gref->data[0]; \
  98. const type *main_ptr = (const type *) s->gmain->data[0]; \
  99. \
  100. float *ptr = ref_data; \
  101. \
  102. int h = s->height; \
  103. int w = s->width; \
  104. \
  105. int i,j; \
  106. \
  107. for (i = 0; i < h; i++) { \
  108. for ( j = 0; j < w; j++) { \
  109. ptr[j] = (float)ref_ptr[j]; \
  110. } \
  111. ref_ptr += ref_stride / sizeof(*ref_ptr); \
  112. ptr += stride / sizeof(*ptr); \
  113. } \
  114. \
  115. ptr = main_data; \
  116. \
  117. for (i = 0; i < h; i++) { \
  118. for (j = 0; j < w; j++) { \
  119. ptr[j] = (float)main_ptr[j]; \
  120. } \
  121. main_ptr += main_stride / sizeof(*main_ptr); \
  122. ptr += stride / sizeof(*ptr); \
  123. } \
  124. } \
  125. \
  126. ret = !s->frame_set; \
  127. \
  128. s->frame_set = 0; \
  129. \
  130. pthread_cond_signal(&s->cond); \
  131. pthread_mutex_unlock(&s->lock); \
  132. \
  133. if (ret) { \
  134. return 2; \
  135. } \
  136. \
  137. return 0; \
  138. }
  139. read_frame_fn(uint8_t, 8);
  140. read_frame_fn(uint16_t, 10);
  141. static void compute_vmaf_score(LIBVMAFContext *s)
  142. {
  143. int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
  144. int stride, double *score, void *ctx);
  145. if (s->desc->comp[0].depth <= 8) {
  146. read_frame = read_frame_8bit;
  147. } else {
  148. read_frame = read_frame_10bit;
  149. }
  150. s->vmaf_score = compute_vmaf(s->format, s->width, s->height, read_frame, s,
  151. s->model_path, s->log_path, s->log_fmt, 0, 0,
  152. s->enable_transform, s->phone_model, s->psnr,
  153. s->ssim, s->ms_ssim, s->pool);
  154. }
  155. static void *call_vmaf(void *ctx)
  156. {
  157. LIBVMAFContext *s = (LIBVMAFContext *) ctx;
  158. compute_vmaf_score(s);
  159. av_log(ctx, AV_LOG_INFO, "VMAF score: %f\n",s->vmaf_score);
  160. pthread_exit(NULL);
  161. }
  162. static AVFrame *do_vmaf(AVFilterContext *ctx, AVFrame *main, const AVFrame *ref)
  163. {
  164. LIBVMAFContext *s = ctx->priv;
  165. pthread_mutex_lock(&s->lock);
  166. while (s->frame_set != 0) {
  167. pthread_cond_wait(&s->cond, &s->lock);
  168. }
  169. av_frame_ref(s->gref, ref);
  170. av_frame_ref(s->gmain, main);
  171. s->frame_set = 1;
  172. pthread_cond_signal(&s->cond);
  173. pthread_mutex_unlock(&s->lock);
  174. return main;
  175. }
  176. static av_cold int init(AVFilterContext *ctx)
  177. {
  178. LIBVMAFContext *s = ctx->priv;
  179. s->gref = av_frame_alloc();
  180. s->gmain = av_frame_alloc();
  181. pthread_mutex_init(&s->lock, NULL);
  182. pthread_cond_init (&s->cond, NULL);
  183. s->dinput.process = do_vmaf;
  184. return 0;
  185. }
  186. static int query_formats(AVFilterContext *ctx)
  187. {
  188. static const enum AVPixelFormat pix_fmts[] = {
  189. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  190. AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV420P10LE,
  191. AV_PIX_FMT_NONE
  192. };
  193. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  194. if (!fmts_list)
  195. return AVERROR(ENOMEM);
  196. return ff_set_common_formats(ctx, fmts_list);
  197. }
  198. static int config_input_ref(AVFilterLink *inlink)
  199. {
  200. AVFilterContext *ctx = inlink->dst;
  201. LIBVMAFContext *s = ctx->priv;
  202. int th;
  203. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  204. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  205. av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
  206. return AVERROR(EINVAL);
  207. }
  208. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  209. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  210. return AVERROR(EINVAL);
  211. }
  212. s->desc = av_pix_fmt_desc_get(inlink->format);
  213. s->width = ctx->inputs[0]->w;
  214. s->height = ctx->inputs[0]->h;
  215. th = pthread_create(&s->vmaf_thread, NULL, call_vmaf, (void *) s);
  216. if (th) {
  217. av_log(ctx, AV_LOG_ERROR, "Thread creation failed.\n");
  218. return AVERROR(EINVAL);
  219. }
  220. return 0;
  221. }
  222. static int config_output(AVFilterLink *outlink)
  223. {
  224. AVFilterContext *ctx = outlink->src;
  225. LIBVMAFContext *s = ctx->priv;
  226. AVFilterLink *mainlink = ctx->inputs[0];
  227. int ret;
  228. outlink->w = mainlink->w;
  229. outlink->h = mainlink->h;
  230. outlink->time_base = mainlink->time_base;
  231. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  232. outlink->frame_rate = mainlink->frame_rate;
  233. if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
  234. return ret;
  235. return 0;
  236. }
  237. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  238. {
  239. LIBVMAFContext *s = inlink->dst->priv;
  240. return ff_dualinput_filter_frame(&s->dinput, inlink, inpicref);
  241. }
  242. static int request_frame(AVFilterLink *outlink)
  243. {
  244. LIBVMAFContext *s = outlink->src->priv;
  245. return ff_dualinput_request_frame(&s->dinput, outlink);
  246. }
  247. static av_cold void uninit(AVFilterContext *ctx)
  248. {
  249. LIBVMAFContext *s = ctx->priv;
  250. ff_dualinput_uninit(&s->dinput);
  251. pthread_mutex_lock(&s->lock);
  252. s->eof = 1;
  253. pthread_cond_signal(&s->cond);
  254. pthread_mutex_unlock(&s->lock);
  255. pthread_join(s->vmaf_thread, NULL);
  256. av_frame_free(&s->gref);
  257. av_frame_free(&s->gmain);
  258. pthread_mutex_destroy(&s->lock);
  259. pthread_cond_destroy(&s->cond);
  260. }
  261. static const AVFilterPad libvmaf_inputs[] = {
  262. {
  263. .name = "main",
  264. .type = AVMEDIA_TYPE_VIDEO,
  265. .filter_frame = filter_frame,
  266. },{
  267. .name = "reference",
  268. .type = AVMEDIA_TYPE_VIDEO,
  269. .filter_frame = filter_frame,
  270. .config_props = config_input_ref,
  271. },
  272. { NULL }
  273. };
  274. static const AVFilterPad libvmaf_outputs[] = {
  275. {
  276. .name = "default",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. .config_props = config_output,
  279. .request_frame = request_frame,
  280. },
  281. { NULL }
  282. };
  283. AVFilter ff_vf_libvmaf = {
  284. .name = "libvmaf",
  285. .description = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
  286. .init = init,
  287. .uninit = uninit,
  288. .query_formats = query_formats,
  289. .priv_size = sizeof(LIBVMAFContext),
  290. .priv_class = &libvmaf_class,
  291. .inputs = libvmaf_inputs,
  292. .outputs = libvmaf_outputs,
  293. };