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.

345 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 "drawutils.h"
  33. #include "formats.h"
  34. #include "framesync2.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. typedef struct LIBVMAFContext {
  38. const AVClass *class;
  39. FFFrameSync fs;
  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. FRAMESYNC_DEFINE_CLASS(libvmaf, LIBVMAFContext, fs);
  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 int do_vmaf(FFFrameSync *fs)
  163. {
  164. AVFilterContext *ctx = fs->parent;
  165. LIBVMAFContext *s = ctx->priv;
  166. AVFrame *main, *ref;
  167. int ret;
  168. ret = ff_framesync2_dualinput_get(fs, &main, &ref);
  169. if (ret < 0)
  170. return ret;
  171. if (!ref)
  172. return ff_filter_frame(ctx->outputs[0], main);
  173. pthread_mutex_lock(&s->lock);
  174. while (s->frame_set != 0) {
  175. pthread_cond_wait(&s->cond, &s->lock);
  176. }
  177. av_frame_ref(s->gref, ref);
  178. av_frame_ref(s->gmain, main);
  179. s->frame_set = 1;
  180. pthread_cond_signal(&s->cond);
  181. pthread_mutex_unlock(&s->lock);
  182. return ff_filter_frame(ctx->outputs[0], main);
  183. }
  184. static av_cold int init(AVFilterContext *ctx)
  185. {
  186. LIBVMAFContext *s = ctx->priv;
  187. s->gref = av_frame_alloc();
  188. s->gmain = av_frame_alloc();
  189. pthread_mutex_init(&s->lock, NULL);
  190. pthread_cond_init (&s->cond, NULL);
  191. s->fs.on_event = do_vmaf;
  192. return 0;
  193. }
  194. static int query_formats(AVFilterContext *ctx)
  195. {
  196. static const enum AVPixelFormat pix_fmts[] = {
  197. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  198. AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV420P10LE,
  199. AV_PIX_FMT_NONE
  200. };
  201. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  202. if (!fmts_list)
  203. return AVERROR(ENOMEM);
  204. return ff_set_common_formats(ctx, fmts_list);
  205. }
  206. static int config_input_ref(AVFilterLink *inlink)
  207. {
  208. AVFilterContext *ctx = inlink->dst;
  209. LIBVMAFContext *s = ctx->priv;
  210. int th;
  211. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  212. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  213. av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
  214. return AVERROR(EINVAL);
  215. }
  216. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  217. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  218. return AVERROR(EINVAL);
  219. }
  220. s->desc = av_pix_fmt_desc_get(inlink->format);
  221. s->width = ctx->inputs[0]->w;
  222. s->height = ctx->inputs[0]->h;
  223. th = pthread_create(&s->vmaf_thread, NULL, call_vmaf, (void *) s);
  224. if (th) {
  225. av_log(ctx, AV_LOG_ERROR, "Thread creation failed.\n");
  226. return AVERROR(EINVAL);
  227. }
  228. return 0;
  229. }
  230. static int config_output(AVFilterLink *outlink)
  231. {
  232. AVFilterContext *ctx = outlink->src;
  233. LIBVMAFContext *s = ctx->priv;
  234. AVFilterLink *mainlink = ctx->inputs[0];
  235. int ret;
  236. ret = ff_framesync2_init_dualinput(&s->fs, ctx);
  237. if (ret < 0)
  238. return ret;
  239. outlink->w = mainlink->w;
  240. outlink->h = mainlink->h;
  241. outlink->time_base = mainlink->time_base;
  242. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  243. outlink->frame_rate = mainlink->frame_rate;
  244. if ((ret = ff_framesync2_configure(&s->fs)) < 0)
  245. return ret;
  246. return 0;
  247. }
  248. static int activate(AVFilterContext *ctx)
  249. {
  250. LIBVMAFContext *s = ctx->priv;
  251. return ff_framesync2_activate(&s->fs);
  252. }
  253. static av_cold void uninit(AVFilterContext *ctx)
  254. {
  255. LIBVMAFContext *s = ctx->priv;
  256. ff_framesync2_uninit(&s->fs);
  257. pthread_mutex_lock(&s->lock);
  258. s->eof = 1;
  259. pthread_cond_signal(&s->cond);
  260. pthread_mutex_unlock(&s->lock);
  261. pthread_join(s->vmaf_thread, NULL);
  262. av_frame_free(&s->gref);
  263. av_frame_free(&s->gmain);
  264. pthread_mutex_destroy(&s->lock);
  265. pthread_cond_destroy(&s->cond);
  266. }
  267. static const AVFilterPad libvmaf_inputs[] = {
  268. {
  269. .name = "main",
  270. .type = AVMEDIA_TYPE_VIDEO,
  271. },{
  272. .name = "reference",
  273. .type = AVMEDIA_TYPE_VIDEO,
  274. .config_props = config_input_ref,
  275. },
  276. { NULL }
  277. };
  278. static const AVFilterPad libvmaf_outputs[] = {
  279. {
  280. .name = "default",
  281. .type = AVMEDIA_TYPE_VIDEO,
  282. .config_props = config_output,
  283. },
  284. { NULL }
  285. };
  286. AVFilter ff_vf_libvmaf = {
  287. .name = "libvmaf",
  288. .description = NULL_IF_CONFIG_SMALL("Calculate the VMAF between two video streams."),
  289. .preinit = libvmaf_framesync_preinit,
  290. .init = init,
  291. .uninit = uninit,
  292. .query_formats = query_formats,
  293. .activate = activate,
  294. .priv_size = sizeof(LIBVMAFContext),
  295. .priv_class = &libvmaf_class,
  296. .inputs = libvmaf_inputs,
  297. .outputs = libvmaf_outputs,
  298. };