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.

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