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.

399 lines
13KB

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