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.

325 lines
9.9KB

  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. #define SET_META(key, comp, value) \
  90. snprintf(buf, sizeof(buf), "%0.2f", value); \
  91. av_dict_set(metadata, #key #comp, buf, 0); \
  92. static AVFrame *do_psnr(AVFilterContext *ctx, AVFrame *main,
  93. const AVFrame *ref)
  94. {
  95. PSNRContext *s = ctx->priv;
  96. double comp_mse[4], mse = 0;
  97. char buf[32];
  98. int j, c;
  99. AVDictionary **metadata = avpriv_frame_get_metadatap(main);
  100. compute_images_mse((const uint8_t **)main->data, main->linesize,
  101. (const uint8_t **)ref->data, ref->linesize,
  102. main->width, main->height, s->desc, comp_mse);
  103. for (j = 0; j < s->desc->nb_components; j++)
  104. mse += comp_mse[j];
  105. mse /= s->desc->nb_components;
  106. s->min_mse = FFMIN(s->min_mse, mse);
  107. s->max_mse = FFMAX(s->max_mse, mse);
  108. s->mse += mse;
  109. s->nb_frames++;
  110. for (j = 0; j < s->desc->nb_components; j++) {
  111. c = s->is_rgb ? s->rgba_map[j] : j;
  112. SET_META("lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
  113. SET_META("lavfi.psnr.mse_avg", "", mse);
  114. SET_META("lavfi.psnr.s.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
  115. SET_META("lavfi.psnr.s_avg", "", get_psnr(mse, 1, s->average_max));
  116. }
  117. if (s->stats_file) {
  118. fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
  119. for (j = 0; j < s->desc->nb_components; j++) {
  120. c = s->is_rgb ? s->rgba_map[j] : j;
  121. fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
  122. }
  123. for (j = 0; j < s->desc->nb_components; j++) {
  124. c = s->is_rgb ? s->rgba_map[j] : j;
  125. fprintf(s->stats_file, "s%c:%0.2f ", s->comps[j],
  126. get_psnr(comp_mse[c], 1, s->max[c]));
  127. }
  128. fprintf(s->stats_file, "\n");
  129. }
  130. return main;
  131. }
  132. static av_cold int init(AVFilterContext *ctx)
  133. {
  134. PSNRContext *s = ctx->priv;
  135. s->min_mse = +INFINITY;
  136. s->max_mse = -INFINITY;
  137. if (s->stats_file_str) {
  138. s->stats_file = fopen(s->stats_file_str, "w");
  139. if (!s->stats_file) {
  140. int err = AVERROR(errno);
  141. char buf[128];
  142. av_strerror(err, buf, sizeof(buf));
  143. av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
  144. s->stats_file_str, buf);
  145. return err;
  146. }
  147. }
  148. s->dinput.process = do_psnr;
  149. return 0;
  150. }
  151. static int query_formats(AVFilterContext *ctx)
  152. {
  153. static const enum PixelFormat pix_fmts[] = {
  154. AV_PIX_FMT_GRAY8,
  155. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  156. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P,
  157. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  158. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,
  159. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
  160. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  161. AV_PIX_FMT_NONE
  162. };
  163. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  164. return 0;
  165. }
  166. static int config_input_ref(AVFilterLink *inlink)
  167. {
  168. AVFilterContext *ctx = inlink->dst;
  169. PSNRContext *s = ctx->priv;
  170. int j;
  171. s->desc = av_pix_fmt_desc_get(inlink->format);
  172. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  173. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  174. av_log(ctx, AV_LOG_ERROR, "Width and heigth of input videos must be same.\n");
  175. return AVERROR(EINVAL);
  176. }
  177. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  178. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  179. return AVERROR(EINVAL);
  180. }
  181. switch (inlink->format) {
  182. case AV_PIX_FMT_YUV410P:
  183. case AV_PIX_FMT_YUV411P:
  184. case AV_PIX_FMT_YUV420P:
  185. case AV_PIX_FMT_YUV422P:
  186. case AV_PIX_FMT_YUV440P:
  187. case AV_PIX_FMT_YUV444P:
  188. case AV_PIX_FMT_YUVA420P:
  189. case AV_PIX_FMT_YUVA422P:
  190. case AV_PIX_FMT_YUVA444P:
  191. s->max[0] = 235;
  192. s->max[3] = 255;
  193. s->max[1] = s->max[2] = 240;
  194. break;
  195. default:
  196. s->max[0] = s->max[1] = s->max[2] = s->max[3] = 255;
  197. }
  198. s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  199. s->comps[0] = s->is_rgb ? 'r' : 'y' ;
  200. s->comps[1] = s->is_rgb ? 'g' : 'u' ;
  201. s->comps[2] = s->is_rgb ? 'b' : 'v' ;
  202. s->comps[3] = 'a';
  203. for (j = 0; j < s->desc->nb_components; j++)
  204. s->average_max += s->max[j];
  205. s->average_max /= s->desc->nb_components;
  206. return 0;
  207. }
  208. static int config_output(AVFilterLink *outlink)
  209. {
  210. AVFilterContext *ctx = outlink->src;
  211. AVFilterLink *mainlink = ctx->inputs[0];
  212. outlink->w = mainlink->w;
  213. outlink->h = mainlink->h;
  214. outlink->time_base = mainlink->time_base;
  215. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  216. outlink->frame_rate = mainlink->frame_rate;
  217. return 0;
  218. }
  219. static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
  220. {
  221. PSNRContext *s = inlink->dst->priv;
  222. return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
  223. }
  224. static int filter_frame_ref(AVFilterLink *inlink, AVFrame *inpicref)
  225. {
  226. PSNRContext *s = inlink->dst->priv;
  227. return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
  228. }
  229. static int request_frame(AVFilterLink *outlink)
  230. {
  231. PSNRContext *s = outlink->src->priv;
  232. return ff_dualinput_request_frame(&s->dinput, outlink);
  233. }
  234. static av_cold void uninit(AVFilterContext *ctx)
  235. {
  236. PSNRContext *s = ctx->priv;
  237. if (s->nb_frames > 0) {
  238. av_log(ctx, AV_LOG_INFO, "PSNR average:%0.2f min:%0.2f max:%0.2f\n",
  239. get_psnr(s->mse, s->nb_frames, s->average_max),
  240. get_psnr(s->max_mse, 1, s->average_max),
  241. get_psnr(s->min_mse, 1, s->average_max));
  242. }
  243. ff_dualinput_uninit(&s->dinput);
  244. if (s->stats_file)
  245. fclose(s->stats_file);
  246. }
  247. static const AVFilterPad psnr_inputs[] = {
  248. {
  249. .name = "main",
  250. .type = AVMEDIA_TYPE_VIDEO,
  251. .filter_frame = filter_frame_main,
  252. },{
  253. .name = "reference",
  254. .type = AVMEDIA_TYPE_VIDEO,
  255. .filter_frame = filter_frame_ref,
  256. .config_props = config_input_ref,
  257. },
  258. { NULL }
  259. };
  260. static const AVFilterPad psnr_outputs[] = {
  261. {
  262. .name = "default",
  263. .type = AVMEDIA_TYPE_VIDEO,
  264. .config_props = config_output,
  265. .request_frame = request_frame,
  266. },
  267. { NULL }
  268. };
  269. AVFilter avfilter_vf_psnr = {
  270. .name = "psnr",
  271. .description = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
  272. .init = init,
  273. .uninit = uninit,
  274. .query_formats = query_formats,
  275. .priv_size = sizeof(PSNRContext),
  276. .priv_class = &psnr_class,
  277. .inputs = psnr_inputs,
  278. .outputs = psnr_outputs,
  279. };