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.

333 lines
10KB

  1. /*
  2. * Copyright (c) 2011 Roger Pau Monné <roger.pau@entel.upc.edu>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2013 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Caculate the PSNR between two input videos.
  25. */
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "dualinput.h"
  30. #include "drawutils.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. typedef struct PSNRContext {
  35. const AVClass *class;
  36. FFDualInputContext dinput;
  37. double mse, min_mse, max_mse;
  38. uint64_t nb_frames;
  39. FILE *stats_file;
  40. char *stats_file_str;
  41. int max[4], average_max;
  42. int is_rgb;
  43. uint8_t rgba_map[4];
  44. char comps[4];
  45. const AVPixFmtDescriptor *desc;
  46. } PSNRContext;
  47. #define OFFSET(x) offsetof(PSNRContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  49. static const AVOption psnr_options[] = {
  50. {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  51. {"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  52. { NULL },
  53. };
  54. AVFILTER_DEFINE_CLASS(psnr);
  55. static inline int pow2(int base)
  56. {
  57. return base*base;
  58. }
  59. static inline double get_psnr(double mse, uint64_t nb_frames, int max)
  60. {
  61. return 10.0 * log(pow2(max) / (mse / nb_frames)) / log(10.0);
  62. }
  63. static inline
  64. void compute_images_mse(const uint8_t *main_data[4], const int main_linesizes[4],
  65. const uint8_t *ref_data[4], const int ref_linesizes[4],
  66. int w, int h, const AVPixFmtDescriptor *desc,
  67. double mse[4])
  68. {
  69. int i, c, j;
  70. for (c = 0; c < desc->nb_components; c++) {
  71. int hsub = c == 1 || c == 2 ? desc->log2_chroma_w : 0;
  72. int vsub = c == 1 || c == 2 ? desc->log2_chroma_h : 0;
  73. const int outw = FF_CEIL_RSHIFT(w, hsub);
  74. const int outh = FF_CEIL_RSHIFT(h, vsub);
  75. const uint8_t *main_line = main_data[c];
  76. const uint8_t *ref_line = ref_data[c];
  77. const int ref_linesize = ref_linesizes[c];
  78. const int main_linesize = main_linesizes[c];
  79. int m = 0;
  80. for (i = 0; i < outh; i++) {
  81. for (j = 0; j < outw; j++)
  82. m += pow2(main_line[j] - ref_line[j]);
  83. ref_line += ref_linesize;
  84. main_line += main_linesize;
  85. }
  86. mse[c] = m / (outw * outh);
  87. }
  88. }
  89. static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
  90. {
  91. char value[128];
  92. snprintf(value, sizeof(value), "%0.2f", d);
  93. if (comp) {
  94. char key2[128];
  95. snprintf(key2, sizeof(key2), "%s%c", key, comp);
  96. av_dict_set(metadata, key2, value, 0);
  97. } else {
  98. av_dict_set(metadata, key, value, 0);
  99. }
  100. }
  101. static AVFrame *do_psnr(AVFilterContext *ctx, AVFrame *main,
  102. const AVFrame *ref)
  103. {
  104. PSNRContext *s = ctx->priv;
  105. double comp_mse[4], mse = 0;
  106. int j, c;
  107. AVDictionary **metadata = avpriv_frame_get_metadatap(main);
  108. compute_images_mse((const uint8_t **)main->data, main->linesize,
  109. (const uint8_t **)ref->data, ref->linesize,
  110. main->width, main->height, s->desc, comp_mse);
  111. for (j = 0; j < s->desc->nb_components; j++)
  112. mse += comp_mse[j];
  113. mse /= s->desc->nb_components;
  114. s->min_mse = FFMIN(s->min_mse, mse);
  115. s->max_mse = FFMAX(s->max_mse, mse);
  116. s->mse += mse;
  117. s->nb_frames++;
  118. for (j = 0; j < s->desc->nb_components; j++) {
  119. c = s->is_rgb ? s->rgba_map[j] : j;
  120. set_meta(metadata, "lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
  121. set_meta(metadata, "lavfi.psnr.mse_avg", 0, mse);
  122. set_meta(metadata, "lavfi.psnr.psnr.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
  123. set_meta(metadata, "lavfi.psnr.psnr_avg", 0, get_psnr(mse, 1, s->average_max));
  124. }
  125. if (s->stats_file) {
  126. fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
  127. for (j = 0; j < s->desc->nb_components; j++) {
  128. c = s->is_rgb ? s->rgba_map[j] : j;
  129. fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
  130. }
  131. for (j = 0; j < s->desc->nb_components; j++) {
  132. c = s->is_rgb ? s->rgba_map[j] : j;
  133. fprintf(s->stats_file, "psnr_%c:%0.2f ", s->comps[j],
  134. get_psnr(comp_mse[c], 1, s->max[c]));
  135. }
  136. fprintf(s->stats_file, "\n");
  137. }
  138. return main;
  139. }
  140. static av_cold int init(AVFilterContext *ctx)
  141. {
  142. PSNRContext *s = ctx->priv;
  143. s->min_mse = +INFINITY;
  144. s->max_mse = -INFINITY;
  145. if (s->stats_file_str) {
  146. s->stats_file = fopen(s->stats_file_str, "w");
  147. if (!s->stats_file) {
  148. int err = AVERROR(errno);
  149. char buf[128];
  150. av_strerror(err, buf, sizeof(buf));
  151. av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
  152. s->stats_file_str, buf);
  153. return err;
  154. }
  155. }
  156. s->dinput.process = do_psnr;
  157. return 0;
  158. }
  159. static int query_formats(AVFilterContext *ctx)
  160. {
  161. static const enum PixelFormat pix_fmts[] = {
  162. AV_PIX_FMT_GRAY8,
  163. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  164. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P,
  165. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  166. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,
  167. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
  168. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  169. AV_PIX_FMT_NONE
  170. };
  171. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  172. return 0;
  173. }
  174. static int config_input_ref(AVFilterLink *inlink)
  175. {
  176. AVFilterContext *ctx = inlink->dst;
  177. PSNRContext *s = ctx->priv;
  178. int j;
  179. s->desc = av_pix_fmt_desc_get(inlink->format);
  180. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  181. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  182. av_log(ctx, AV_LOG_ERROR, "Width and heigth of input videos must be same.\n");
  183. return AVERROR(EINVAL);
  184. }
  185. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  186. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  187. return AVERROR(EINVAL);
  188. }
  189. switch (inlink->format) {
  190. case AV_PIX_FMT_YUV410P:
  191. case AV_PIX_FMT_YUV411P:
  192. case AV_PIX_FMT_YUV420P:
  193. case AV_PIX_FMT_YUV422P:
  194. case AV_PIX_FMT_YUV440P:
  195. case AV_PIX_FMT_YUV444P:
  196. case AV_PIX_FMT_YUVA420P:
  197. case AV_PIX_FMT_YUVA422P:
  198. case AV_PIX_FMT_YUVA444P:
  199. s->max[0] = 235;
  200. s->max[3] = 255;
  201. s->max[1] = s->max[2] = 240;
  202. break;
  203. default:
  204. s->max[0] = s->max[1] = s->max[2] = s->max[3] = 255;
  205. }
  206. s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  207. s->comps[0] = s->is_rgb ? 'r' : 'y' ;
  208. s->comps[1] = s->is_rgb ? 'g' : 'u' ;
  209. s->comps[2] = s->is_rgb ? 'b' : 'v' ;
  210. s->comps[3] = 'a';
  211. for (j = 0; j < s->desc->nb_components; j++)
  212. s->average_max += s->max[j];
  213. s->average_max /= s->desc->nb_components;
  214. return 0;
  215. }
  216. static int config_output(AVFilterLink *outlink)
  217. {
  218. AVFilterContext *ctx = outlink->src;
  219. AVFilterLink *mainlink = ctx->inputs[0];
  220. outlink->w = mainlink->w;
  221. outlink->h = mainlink->h;
  222. outlink->time_base = mainlink->time_base;
  223. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  224. outlink->frame_rate = mainlink->frame_rate;
  225. return 0;
  226. }
  227. static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
  228. {
  229. PSNRContext *s = inlink->dst->priv;
  230. return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
  231. }
  232. static int filter_frame_ref(AVFilterLink *inlink, AVFrame *inpicref)
  233. {
  234. PSNRContext *s = inlink->dst->priv;
  235. return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
  236. }
  237. static int request_frame(AVFilterLink *outlink)
  238. {
  239. PSNRContext *s = outlink->src->priv;
  240. return ff_dualinput_request_frame(&s->dinput, outlink);
  241. }
  242. static av_cold void uninit(AVFilterContext *ctx)
  243. {
  244. PSNRContext *s = ctx->priv;
  245. if (s->nb_frames > 0) {
  246. av_log(ctx, AV_LOG_INFO, "PSNR average:%0.2f min:%0.2f max:%0.2f\n",
  247. get_psnr(s->mse, s->nb_frames, s->average_max),
  248. get_psnr(s->max_mse, 1, s->average_max),
  249. get_psnr(s->min_mse, 1, s->average_max));
  250. }
  251. ff_dualinput_uninit(&s->dinput);
  252. if (s->stats_file)
  253. fclose(s->stats_file);
  254. }
  255. static const AVFilterPad psnr_inputs[] = {
  256. {
  257. .name = "main",
  258. .type = AVMEDIA_TYPE_VIDEO,
  259. .filter_frame = filter_frame_main,
  260. },{
  261. .name = "reference",
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. .filter_frame = filter_frame_ref,
  264. .config_props = config_input_ref,
  265. },
  266. { NULL }
  267. };
  268. static const AVFilterPad psnr_outputs[] = {
  269. {
  270. .name = "default",
  271. .type = AVMEDIA_TYPE_VIDEO,
  272. .config_props = config_output,
  273. .request_frame = request_frame,
  274. },
  275. { NULL }
  276. };
  277. AVFilter avfilter_vf_psnr = {
  278. .name = "psnr",
  279. .description = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
  280. .init = init,
  281. .uninit = uninit,
  282. .query_formats = query_formats,
  283. .priv_size = sizeof(PSNRContext),
  284. .priv_class = &psnr_class,
  285. .inputs = psnr_inputs,
  286. .outputs = psnr_outputs,
  287. };