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.

387 lines
12KB

  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. int nb_components;
  46. int planewidth[4];
  47. int planeheight[4];
  48. void (*compute_mse)(struct PSNRContext *s,
  49. const uint8_t *m[4], const int ml[4],
  50. const uint8_t *r[4], const int rl[4],
  51. int w, int h, double mse[4]);
  52. } PSNRContext;
  53. #define OFFSET(x) offsetof(PSNRContext, x)
  54. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  55. static const AVOption psnr_options[] = {
  56. {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  57. {"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  58. { NULL }
  59. };
  60. AVFILTER_DEFINE_CLASS(psnr);
  61. static inline unsigned pow2(unsigned base)
  62. {
  63. return base*base;
  64. }
  65. static inline double get_psnr(double mse, uint64_t nb_frames, int max)
  66. {
  67. return 10.0 * log(pow2(max) / (mse / nb_frames)) / log(10.0);
  68. }
  69. static inline
  70. void compute_images_mse(PSNRContext *s,
  71. const uint8_t *main_data[4], const int main_linesizes[4],
  72. const uint8_t *ref_data[4], const int ref_linesizes[4],
  73. int w, int h, double mse[4])
  74. {
  75. int i, c, j;
  76. for (c = 0; c < s->nb_components; c++) {
  77. const int outw = s->planewidth[c];
  78. const int outh = s->planeheight[c];
  79. const uint8_t *main_line = main_data[c];
  80. const uint8_t *ref_line = ref_data[c];
  81. const int ref_linesize = ref_linesizes[c];
  82. const int main_linesize = main_linesizes[c];
  83. int m = 0;
  84. for (i = 0; i < outh; i++) {
  85. for (j = 0; j < outw; j++)
  86. m += pow2(main_line[j] - ref_line[j]);
  87. ref_line += ref_linesize;
  88. main_line += main_linesize;
  89. }
  90. mse[c] = m / (double)(outw * outh);
  91. }
  92. }
  93. static inline
  94. void compute_images_mse_16bit(PSNRContext *s,
  95. const uint8_t *main_data[4], const int main_linesizes[4],
  96. const uint8_t *ref_data[4], const int ref_linesizes[4],
  97. int w, int h, double mse[4])
  98. {
  99. int i, c, j;
  100. for (c = 0; c < s->nb_components; c++) {
  101. const int outw = s->planewidth[c];
  102. const int outh = s->planeheight[c];
  103. const uint16_t *main_line = (uint16_t *)main_data[c];
  104. const uint16_t *ref_line = (uint16_t *)ref_data[c];
  105. const int ref_linesize = ref_linesizes[c] / 2;
  106. const int main_linesize = main_linesizes[c] / 2;
  107. uint64_t m = 0;
  108. for (i = 0; i < outh; i++) {
  109. for (j = 0; j < outw; j++)
  110. m += pow2(main_line[j] - ref_line[j]);
  111. ref_line += ref_linesize;
  112. main_line += main_linesize;
  113. }
  114. mse[c] = m / (double)(outw * outh);
  115. }
  116. }
  117. static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
  118. {
  119. char value[128];
  120. snprintf(value, sizeof(value), "%0.2f", d);
  121. if (comp) {
  122. char key2[128];
  123. snprintf(key2, sizeof(key2), "%s%c", key, comp);
  124. av_dict_set(metadata, key2, value, 0);
  125. } else {
  126. av_dict_set(metadata, key, value, 0);
  127. }
  128. }
  129. static AVFrame *do_psnr(AVFilterContext *ctx, AVFrame *main,
  130. const AVFrame *ref)
  131. {
  132. PSNRContext *s = ctx->priv;
  133. double comp_mse[4], mse = 0;
  134. int j, c;
  135. AVDictionary **metadata = avpriv_frame_get_metadatap(main);
  136. s->compute_mse(s, (const uint8_t **)main->data, main->linesize,
  137. (const uint8_t **)ref->data, ref->linesize,
  138. main->width, main->height, comp_mse);
  139. for (j = 0; j < s->nb_components; j++)
  140. mse += comp_mse[j];
  141. mse /= s->nb_components;
  142. s->min_mse = FFMIN(s->min_mse, mse);
  143. s->max_mse = FFMAX(s->max_mse, mse);
  144. s->mse += mse;
  145. s->nb_frames++;
  146. for (j = 0; j < s->nb_components; j++) {
  147. c = s->is_rgb ? s->rgba_map[j] : j;
  148. set_meta(metadata, "lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
  149. set_meta(metadata, "lavfi.psnr.mse_avg", 0, mse);
  150. set_meta(metadata, "lavfi.psnr.psnr.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
  151. set_meta(metadata, "lavfi.psnr.psnr_avg", 0, get_psnr(mse, 1, s->average_max));
  152. }
  153. if (s->stats_file) {
  154. fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
  155. for (j = 0; j < s->nb_components; j++) {
  156. c = s->is_rgb ? s->rgba_map[j] : j;
  157. fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
  158. }
  159. for (j = 0; j < s->nb_components; j++) {
  160. c = s->is_rgb ? s->rgba_map[j] : j;
  161. fprintf(s->stats_file, "psnr_%c:%0.2f ", s->comps[j],
  162. get_psnr(comp_mse[c], 1, s->max[c]));
  163. }
  164. fprintf(s->stats_file, "\n");
  165. }
  166. return main;
  167. }
  168. static av_cold int init(AVFilterContext *ctx)
  169. {
  170. PSNRContext *s = ctx->priv;
  171. s->min_mse = +INFINITY;
  172. s->max_mse = -INFINITY;
  173. if (s->stats_file_str) {
  174. s->stats_file = fopen(s->stats_file_str, "w");
  175. if (!s->stats_file) {
  176. int err = AVERROR(errno);
  177. char buf[128];
  178. av_strerror(err, buf, sizeof(buf));
  179. av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
  180. s->stats_file_str, buf);
  181. return err;
  182. }
  183. }
  184. s->dinput.process = do_psnr;
  185. return 0;
  186. }
  187. static int query_formats(AVFilterContext *ctx)
  188. {
  189. static const enum PixelFormat pix_fmts[] = {
  190. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
  191. #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
  192. #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
  193. #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
  194. PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
  195. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  196. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  197. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  198. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  199. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  200. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
  201. AV_PIX_FMT_NONE
  202. };
  203. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  204. return 0;
  205. }
  206. static int config_input_ref(AVFilterLink *inlink)
  207. {
  208. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  209. AVFilterContext *ctx = inlink->dst;
  210. PSNRContext *s = ctx->priv;
  211. int j;
  212. s->nb_components = desc->nb_components;
  213. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  214. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  215. av_log(ctx, AV_LOG_ERROR, "Width and heigth of input videos must be same.\n");
  216. return AVERROR(EINVAL);
  217. }
  218. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  219. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  220. return AVERROR(EINVAL);
  221. }
  222. switch (inlink->format) {
  223. case AV_PIX_FMT_GRAY8:
  224. case AV_PIX_FMT_GRAY16:
  225. case AV_PIX_FMT_GBRP:
  226. case AV_PIX_FMT_GBRP9:
  227. case AV_PIX_FMT_GBRP10:
  228. case AV_PIX_FMT_GBRP12:
  229. case AV_PIX_FMT_GBRP14:
  230. case AV_PIX_FMT_GBRP16:
  231. case AV_PIX_FMT_GBRAP:
  232. case AV_PIX_FMT_GBRAP16:
  233. case AV_PIX_FMT_YUVJ411P:
  234. case AV_PIX_FMT_YUVJ420P:
  235. case AV_PIX_FMT_YUVJ422P:
  236. case AV_PIX_FMT_YUVJ440P:
  237. case AV_PIX_FMT_YUVJ444P:
  238. s->max[0] = (1 << (desc->comp[0].depth_minus1 + 1)) - 1;
  239. s->max[1] = (1 << (desc->comp[1].depth_minus1 + 1)) - 1;
  240. s->max[2] = (1 << (desc->comp[2].depth_minus1 + 1)) - 1;
  241. s->max[3] = (1 << (desc->comp[3].depth_minus1 + 1)) - 1;
  242. break;
  243. default:
  244. s->max[0] = 235 * (1 << (desc->comp[0].depth_minus1 - 7));
  245. s->max[1] = 240 * (1 << (desc->comp[1].depth_minus1 - 7));
  246. s->max[2] = 240 * (1 << (desc->comp[2].depth_minus1 - 7));
  247. s->max[3] = (1 << (desc->comp[3].depth_minus1 + 1)) - 1;
  248. }
  249. s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  250. s->comps[0] = s->is_rgb ? 'r' : 'y' ;
  251. s->comps[1] = s->is_rgb ? 'g' : 'u' ;
  252. s->comps[2] = s->is_rgb ? 'b' : 'v' ;
  253. s->comps[3] = 'a';
  254. for (j = 0; j < s->nb_components; j++)
  255. s->average_max += s->max[j];
  256. s->average_max /= s->nb_components;
  257. s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  258. s->planeheight[0] = s->planeheight[3] = inlink->h;
  259. s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  260. s->planewidth[0] = s->planewidth[3] = inlink->w;
  261. s->compute_mse = desc->comp[0].depth_minus1 > 7 ? compute_images_mse_16bit : compute_images_mse;
  262. return 0;
  263. }
  264. static int config_output(AVFilterLink *outlink)
  265. {
  266. AVFilterContext *ctx = outlink->src;
  267. AVFilterLink *mainlink = ctx->inputs[0];
  268. outlink->w = mainlink->w;
  269. outlink->h = mainlink->h;
  270. outlink->time_base = mainlink->time_base;
  271. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  272. outlink->frame_rate = mainlink->frame_rate;
  273. return 0;
  274. }
  275. static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
  276. {
  277. PSNRContext *s = inlink->dst->priv;
  278. return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
  279. }
  280. static int filter_frame_ref(AVFilterLink *inlink, AVFrame *inpicref)
  281. {
  282. PSNRContext *s = inlink->dst->priv;
  283. return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
  284. }
  285. static int request_frame(AVFilterLink *outlink)
  286. {
  287. PSNRContext *s = outlink->src->priv;
  288. return ff_dualinput_request_frame(&s->dinput, outlink);
  289. }
  290. static av_cold void uninit(AVFilterContext *ctx)
  291. {
  292. PSNRContext *s = ctx->priv;
  293. if (s->nb_frames > 0) {
  294. av_log(ctx, AV_LOG_INFO, "PSNR average:%0.2f min:%0.2f max:%0.2f\n",
  295. get_psnr(s->mse, s->nb_frames, s->average_max),
  296. get_psnr(s->max_mse, 1, s->average_max),
  297. get_psnr(s->min_mse, 1, s->average_max));
  298. }
  299. ff_dualinput_uninit(&s->dinput);
  300. if (s->stats_file)
  301. fclose(s->stats_file);
  302. }
  303. static const AVFilterPad psnr_inputs[] = {
  304. {
  305. .name = "main",
  306. .type = AVMEDIA_TYPE_VIDEO,
  307. .filter_frame = filter_frame_main,
  308. },{
  309. .name = "reference",
  310. .type = AVMEDIA_TYPE_VIDEO,
  311. .filter_frame = filter_frame_ref,
  312. .config_props = config_input_ref,
  313. },
  314. { NULL }
  315. };
  316. static const AVFilterPad psnr_outputs[] = {
  317. {
  318. .name = "default",
  319. .type = AVMEDIA_TYPE_VIDEO,
  320. .config_props = config_output,
  321. .request_frame = request_frame,
  322. },
  323. { NULL }
  324. };
  325. AVFilter avfilter_vf_psnr = {
  326. .name = "psnr",
  327. .description = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
  328. .init = init,
  329. .uninit = uninit,
  330. .query_formats = query_formats,
  331. .priv_size = sizeof(PSNRContext),
  332. .priv_class = &psnr_class,
  333. .inputs = psnr_inputs,
  334. .outputs = psnr_outputs,
  335. };