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.

363 lines
13KB

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