vf_psnr.c 14 KB

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