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.

419 lines
14KB

  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 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. AVFILTER_DEFINE_CLASS(psnr);
  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 AVFrame *do_psnr(AVFilterContext *ctx, AVFrame *main,
  128. const AVFrame *ref)
  129. {
  130. PSNRContext *s = ctx->priv;
  131. double comp_mse[4], mse = 0;
  132. int j, c;
  133. AVDictionary **metadata = avpriv_frame_get_metadatap(main);
  134. compute_images_mse(s, (const uint8_t **)main->data, main->linesize,
  135. (const uint8_t **)ref->data, ref->linesize,
  136. main->width, main->height, comp_mse);
  137. for (j = 0; j < s->nb_components; j++)
  138. mse += comp_mse[j] * s->planeweight[j];
  139. s->min_mse = FFMIN(s->min_mse, mse);
  140. s->max_mse = FFMAX(s->max_mse, mse);
  141. s->mse += mse;
  142. for (j = 0; j < s->nb_components; j++)
  143. s->mse_comp[j] += comp_mse[j];
  144. s->nb_frames++;
  145. for (j = 0; j < s->nb_components; j++) {
  146. c = s->is_rgb ? s->rgba_map[j] : j;
  147. set_meta(metadata, "lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
  148. set_meta(metadata, "lavfi.psnr.psnr.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
  149. }
  150. set_meta(metadata, "lavfi.psnr.mse_avg", 0, mse);
  151. set_meta(metadata, "lavfi.psnr.psnr_avg", 0, get_psnr(mse, 1, s->average_max));
  152. if (s->stats_file) {
  153. if (s->stats_version == 2 && !s->stats_header_written) {
  154. fprintf(s->stats_file, "psnr_log_version:2 fields:n");
  155. fprintf(s->stats_file, ",mse_avg");
  156. for (j = 0; j < s->nb_components; j++) {
  157. fprintf(s->stats_file, ",mse_%c", s->comps[j]);
  158. }
  159. fprintf(s->stats_file, ",psnr_avg");
  160. for (j = 0; j < s->nb_components; j++) {
  161. fprintf(s->stats_file, ",psnr_%c", s->comps[j]);
  162. }
  163. if (s->stats_add_max) {
  164. fprintf(s->stats_file, ",max_avg");
  165. for (j = 0; j < s->nb_components; j++) {
  166. fprintf(s->stats_file, ",max_%c", s->comps[j]);
  167. }
  168. }
  169. fprintf(s->stats_file, "\n");
  170. s->stats_header_written = 1;
  171. }
  172. fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
  173. for (j = 0; j < s->nb_components; j++) {
  174. c = s->is_rgb ? s->rgba_map[j] : j;
  175. fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
  176. }
  177. fprintf(s->stats_file, "psnr_avg:%0.2f ", get_psnr(mse, 1, s->average_max));
  178. for (j = 0; j < s->nb_components; j++) {
  179. c = s->is_rgb ? s->rgba_map[j] : j;
  180. fprintf(s->stats_file, "psnr_%c:%0.2f ", s->comps[j],
  181. get_psnr(comp_mse[c], 1, s->max[c]));
  182. }
  183. if (s->stats_version == 2 && s->stats_add_max) {
  184. fprintf(s->stats_file, "max_avg:%d ", 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, "max_%c:%d ", s->comps[j], s->max[c]);
  188. }
  189. }
  190. fprintf(s->stats_file, "\n");
  191. }
  192. return main;
  193. }
  194. static av_cold int init(AVFilterContext *ctx)
  195. {
  196. PSNRContext *s = ctx->priv;
  197. s->min_mse = +INFINITY;
  198. s->max_mse = -INFINITY;
  199. if (s->stats_file_str) {
  200. if (s->stats_version < 2 && s->stats_add_max) {
  201. av_log(ctx, AV_LOG_ERROR,
  202. "stats_add_max was specified but stats_version < 2.\n" );
  203. return AVERROR(EINVAL);
  204. }
  205. if (!strcmp(s->stats_file_str, "-")) {
  206. s->stats_file = stdout;
  207. } else {
  208. s->stats_file = fopen(s->stats_file_str, "w");
  209. if (!s->stats_file) {
  210. int err = AVERROR(errno);
  211. char buf[128];
  212. av_strerror(err, buf, sizeof(buf));
  213. av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
  214. s->stats_file_str, buf);
  215. return err;
  216. }
  217. }
  218. }
  219. s->dinput.process = do_psnr;
  220. return 0;
  221. }
  222. static int query_formats(AVFilterContext *ctx)
  223. {
  224. static const enum AVPixelFormat pix_fmts[] = {
  225. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  226. #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
  227. #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
  228. #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
  229. PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
  230. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  231. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  232. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  233. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  234. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  235. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
  236. AV_PIX_FMT_NONE
  237. };
  238. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  239. if (!fmts_list)
  240. return AVERROR(ENOMEM);
  241. return ff_set_common_formats(ctx, fmts_list);
  242. }
  243. static int config_input_ref(AVFilterLink *inlink)
  244. {
  245. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  246. AVFilterContext *ctx = inlink->dst;
  247. PSNRContext *s = ctx->priv;
  248. double average_max;
  249. unsigned sum;
  250. int j;
  251. s->nb_components = desc->nb_components;
  252. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  253. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  254. av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
  255. return AVERROR(EINVAL);
  256. }
  257. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  258. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  259. return AVERROR(EINVAL);
  260. }
  261. s->max[0] = (1 << desc->comp[0].depth) - 1;
  262. s->max[1] = (1 << desc->comp[1].depth) - 1;
  263. s->max[2] = (1 << desc->comp[2].depth) - 1;
  264. s->max[3] = (1 << desc->comp[3].depth) - 1;
  265. s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  266. s->comps[0] = s->is_rgb ? 'r' : 'y' ;
  267. s->comps[1] = s->is_rgb ? 'g' : 'u' ;
  268. s->comps[2] = s->is_rgb ? 'b' : 'v' ;
  269. s->comps[3] = 'a';
  270. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  271. s->planeheight[0] = s->planeheight[3] = inlink->h;
  272. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  273. s->planewidth[0] = s->planewidth[3] = inlink->w;
  274. sum = 0;
  275. for (j = 0; j < s->nb_components; j++)
  276. sum += s->planeheight[j] * s->planewidth[j];
  277. average_max = 0;
  278. for (j = 0; j < s->nb_components; j++) {
  279. s->planeweight[j] = (double) s->planeheight[j] * s->planewidth[j] / sum;
  280. average_max += s->max[j] * s->planeweight[j];
  281. }
  282. s->average_max = lrint(average_max);
  283. s->dsp.sse_line = desc->comp[0].depth > 8 ? sse_line_16bit : sse_line_8bit;
  284. if (ARCH_X86)
  285. ff_psnr_init_x86(&s->dsp, desc->comp[0].depth);
  286. return 0;
  287. }
  288. static int config_output(AVFilterLink *outlink)
  289. {
  290. AVFilterContext *ctx = outlink->src;
  291. PSNRContext *s = ctx->priv;
  292. AVFilterLink *mainlink = ctx->inputs[0];
  293. int ret;
  294. outlink->w = mainlink->w;
  295. outlink->h = mainlink->h;
  296. outlink->time_base = mainlink->time_base;
  297. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  298. outlink->frame_rate = mainlink->frame_rate;
  299. if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
  300. return ret;
  301. return 0;
  302. }
  303. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  304. {
  305. PSNRContext *s = inlink->dst->priv;
  306. return ff_dualinput_filter_frame(&s->dinput, inlink, inpicref);
  307. }
  308. static int request_frame(AVFilterLink *outlink)
  309. {
  310. PSNRContext *s = outlink->src->priv;
  311. return ff_dualinput_request_frame(&s->dinput, outlink);
  312. }
  313. static av_cold void uninit(AVFilterContext *ctx)
  314. {
  315. PSNRContext *s = ctx->priv;
  316. if (s->nb_frames > 0) {
  317. int j;
  318. char buf[256];
  319. buf[0] = 0;
  320. for (j = 0; j < s->nb_components; j++) {
  321. int c = s->is_rgb ? s->rgba_map[j] : j;
  322. av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[j],
  323. get_psnr(s->mse_comp[c], s->nb_frames, s->max[c]));
  324. }
  325. av_log(ctx, AV_LOG_INFO, "PSNR%s average:%f min:%f max:%f\n",
  326. buf,
  327. get_psnr(s->mse, s->nb_frames, s->average_max),
  328. get_psnr(s->max_mse, 1, s->average_max),
  329. get_psnr(s->min_mse, 1, s->average_max));
  330. }
  331. ff_dualinput_uninit(&s->dinput);
  332. if (s->stats_file && s->stats_file != stdout)
  333. fclose(s->stats_file);
  334. }
  335. static const AVFilterPad psnr_inputs[] = {
  336. {
  337. .name = "main",
  338. .type = AVMEDIA_TYPE_VIDEO,
  339. .filter_frame = filter_frame,
  340. },{
  341. .name = "reference",
  342. .type = AVMEDIA_TYPE_VIDEO,
  343. .filter_frame = filter_frame,
  344. .config_props = config_input_ref,
  345. },
  346. { NULL }
  347. };
  348. static const AVFilterPad psnr_outputs[] = {
  349. {
  350. .name = "default",
  351. .type = AVMEDIA_TYPE_VIDEO,
  352. .config_props = config_output,
  353. .request_frame = request_frame,
  354. },
  355. { NULL }
  356. };
  357. AVFilter ff_vf_psnr = {
  358. .name = "psnr",
  359. .description = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
  360. .init = init,
  361. .uninit = uninit,
  362. .query_formats = query_formats,
  363. .priv_size = sizeof(PSNRContext),
  364. .priv_class = &psnr_class,
  365. .inputs = psnr_inputs,
  366. .outputs = psnr_outputs,
  367. };