Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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