PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/libavfilter/vf_psnr.c

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