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.

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