vf_psnr.c 12 KB

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