libavfilter/vf_psnr.c
6150bec3
 /*
222d7619
  * Copyright (c) 2011 Roger Pau Monné <roger.pau@entel.upc.edu>
6150bec3
  * Copyright (c) 2011 Stefano Sabatini
  * Copyright (c) 2013 Paul B Mahol
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * Caculate the PSNR between two input videos.
  */
 
0303d439
 #include "libavutil/avstring.h"
6150bec3
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "drawutils.h"
 #include "formats.h"
5f5dcf44
 #include "framesync.h"
6150bec3
 #include "internal.h"
ae4c9dde
 #include "psnr.h"
6150bec3
 #include "video.h"
 
 typedef struct PSNRContext {
     const AVClass *class;
3bd11df4
     FFFrameSync fs;
0303d439
     double mse, min_mse, max_mse, mse_comp[4];
6150bec3
     uint64_t nb_frames;
     FILE *stats_file;
     char *stats_file_str;
bc9ce5f6
     int stats_version;
     int stats_header_written;
aabe12eb
     int stats_add_max;
6150bec3
     int max[4], average_max;
     int is_rgb;
     uint8_t rgba_map[4];
     char comps[4];
0227b429
     int nb_components;
     int planewidth[4];
     int planeheight[4];
0303d439
     double planeweight[4];
ae4c9dde
     PSNRDSPContext dsp;
6150bec3
 } PSNRContext;
 
 #define OFFSET(x) offsetof(PSNRContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption psnr_options[] = {
     {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     {"f",          "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
bc9ce5f6
     {"stats_version", "Set the format version for the stats file.",               OFFSET(stats_version),  AV_OPT_TYPE_INT,    {.i64=1},    1, 2, FLAGS },
aabe12eb
     {"output_max",  "Add raw stats (max values) to the output log.",            OFFSET(stats_add_max), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
b211607b
     { NULL }
6150bec3
 };
 
3bd11df4
 FRAMESYNC_DEFINE_CLASS(psnr, PSNRContext, fs);
6150bec3
 
4ea8f575
 static inline unsigned pow_2(unsigned base)
6150bec3
 {
     return base*base;
 }
 
 static inline double get_psnr(double mse, uint64_t nb_frames, int max)
 {
4ea8f575
     return 10.0 * log10(pow_2(max) / (mse / nb_frames));
6150bec3
 }
 
ae4c9dde
 static uint64_t sse_line_8bit(const uint8_t *main_line,  const uint8_t *ref_line, int outw)
6150bec3
 {
ae4c9dde
     int j;
     unsigned m2 = 0;
6150bec3
 
ae4c9dde
     for (j = 0; j < outw; j++)
4ea8f575
         m2 += pow_2(main_line[j] - ref_line[j]);
6150bec3
 
ae4c9dde
     return m2;
 }
 
 static uint64_t sse_line_16bit(const uint8_t *_main_line, const uint8_t *_ref_line, int outw)
 {
     int j;
     uint64_t m2 = 0;
     const uint16_t *main_line = (const uint16_t *) _main_line;
     const uint16_t *ref_line = (const uint16_t *) _ref_line;
 
     for (j = 0; j < outw; j++)
4ea8f575
         m2 += pow_2(main_line[j] - ref_line[j]);
ae4c9dde
 
     return m2;
bf5ceeff
 }
 
 static inline
ae4c9dde
 void compute_images_mse(PSNRContext *s,
bf5ceeff
                         const uint8_t *main_data[4], const int main_linesizes[4],
                         const uint8_t *ref_data[4], const int ref_linesizes[4],
                         int w, int h, double mse[4])
 {
ae4c9dde
     int i, c;
bf5ceeff
 
     for (c = 0; c < s->nb_components; c++) {
         const int outw = s->planewidth[c];
         const int outh = s->planeheight[c];
ae4c9dde
         const uint8_t *main_line = main_data[c];
         const uint8_t *ref_line = ref_data[c];
         const int ref_linesize = ref_linesizes[c];
         const int main_linesize = main_linesizes[c];
bf5ceeff
         uint64_t m = 0;
         for (i = 0; i < outh; i++) {
ae4c9dde
             m += s->dsp.sse_line(main_line, ref_line, outw);
bf5ceeff
             ref_line += ref_linesize;
             main_line += main_linesize;
         }
         mse[c] = m / (double)(outw * outh);
6150bec3
     }
 }
 
a03e79ed
 static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
 {
     char value[128];
     snprintf(value, sizeof(value), "%0.2f", d);
     if (comp) {
         char key2[128];
         snprintf(key2, sizeof(key2), "%s%c", key, comp);
         av_dict_set(metadata, key2, value, 0);
     } else {
         av_dict_set(metadata, key, value, 0);
     }
 }
6150bec3
 
3bd11df4
 static int do_psnr(FFFrameSync *fs)
6150bec3
 {
3bd11df4
     AVFilterContext *ctx = fs->parent;
6150bec3
     PSNRContext *s = ctx->priv;
5d3e9357
     AVFrame *master, *ref;
6150bec3
     double comp_mse[4], mse = 0;
3bd11df4
     int ret, j, c;
     AVDictionary **metadata;
 
5d3e9357
     ret = ff_framesync_dualinput_get(fs, &master, &ref);
3bd11df4
     if (ret < 0)
         return ret;
     if (!ref)
5d3e9357
         return ff_filter_frame(ctx->outputs[0], master);
     metadata = &master->metadata;
6150bec3
 
5d3e9357
     compute_images_mse(s, (const uint8_t **)master->data, master->linesize,
ae4c9dde
                           (const uint8_t **)ref->data, ref->linesize,
5d3e9357
                           master->width, master->height, comp_mse);
6150bec3
 
0227b429
     for (j = 0; j < s->nb_components; j++)
0303d439
         mse += comp_mse[j] * s->planeweight[j];
6150bec3
 
     s->min_mse = FFMIN(s->min_mse, mse);
     s->max_mse = FFMAX(s->max_mse, mse);
 
     s->mse += mse;
0303d439
     for (j = 0; j < s->nb_components; j++)
         s->mse_comp[j] += comp_mse[j];
6150bec3
     s->nb_frames++;
 
0227b429
     for (j = 0; j < s->nb_components; j++) {
6150bec3
         c = s->is_rgb ? s->rgba_map[j] : j;
a03e79ed
         set_meta(metadata, "lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
3c5071db
         set_meta(metadata, "lavfi.psnr.psnr.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
6150bec3
     }
c381af77
     set_meta(metadata, "lavfi.psnr.mse_avg", 0, mse);
     set_meta(metadata, "lavfi.psnr.psnr_avg", 0, get_psnr(mse, 1, s->average_max));
6150bec3
 
     if (s->stats_file) {
bc9ce5f6
         if (s->stats_version == 2 && !s->stats_header_written) {
             fprintf(s->stats_file, "psnr_log_version:2 fields:n");
             fprintf(s->stats_file, ",mse_avg");
             for (j = 0; j < s->nb_components; j++) {
                 fprintf(s->stats_file, ",mse_%c", s->comps[j]);
             }
             fprintf(s->stats_file, ",psnr_avg");
             for (j = 0; j < s->nb_components; j++) {
                 fprintf(s->stats_file, ",psnr_%c", s->comps[j]);
             }
aabe12eb
             if (s->stats_add_max) {
                 fprintf(s->stats_file, ",max_avg");
                 for (j = 0; j < s->nb_components; j++) {
                     fprintf(s->stats_file, ",max_%c", s->comps[j]);
                 }
             }
bc9ce5f6
             fprintf(s->stats_file, "\n");
             s->stats_header_written = 1;
         }
6150bec3
         fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
0227b429
         for (j = 0; j < s->nb_components; j++) {
6150bec3
             c = s->is_rgb ? s->rgba_map[j] : j;
             fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
         }
22b30f92
         fprintf(s->stats_file, "psnr_avg:%0.2f ", get_psnr(mse, 1, s->average_max));
0227b429
         for (j = 0; j < s->nb_components; j++) {
6150bec3
             c = s->is_rgb ? s->rgba_map[j] : j;
3c5071db
             fprintf(s->stats_file, "psnr_%c:%0.2f ", s->comps[j],
6150bec3
                     get_psnr(comp_mse[c], 1, s->max[c]));
         }
aabe12eb
         if (s->stats_version == 2 && s->stats_add_max) {
             fprintf(s->stats_file, "max_avg:%d ", s->average_max);
             for (j = 0; j < s->nb_components; j++) {
                 c = s->is_rgb ? s->rgba_map[j] : j;
                 fprintf(s->stats_file, "max_%c:%d ", s->comps[j], s->max[c]);
             }
         }
6150bec3
         fprintf(s->stats_file, "\n");
     }
 
5d3e9357
     return ff_filter_frame(ctx->outputs[0], master);
6150bec3
 }
 
 static av_cold int init(AVFilterContext *ctx)
 {
     PSNRContext *s = ctx->priv;
 
     s->min_mse = +INFINITY;
     s->max_mse = -INFINITY;
 
0c7b44a0
     if (s->stats_file_str) {
aabe12eb
         if (s->stats_version < 2 && s->stats_add_max) {
             av_log(ctx, AV_LOG_ERROR,
                 "stats_add_max was specified but stats_version < 2.\n" );
             return AVERROR(EINVAL);
         }
0c7b44a0
         if (!strcmp(s->stats_file_str, "-")) {
             s->stats_file = stdout;
         } else {
             s->stats_file = fopen(s->stats_file_str, "w");
             if (!s->stats_file) {
                 int err = AVERROR(errno);
                 char buf[128];
                 av_strerror(err, buf, sizeof(buf));
                 av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
                        s->stats_file_str, buf);
                 return err;
             }
6150bec3
         }
     }
 
3bd11df4
     s->fs.on_event = do_psnr;
6150bec3
     return 0;
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
0f16dfda
     static const enum AVPixelFormat pix_fmts[] = {
bac508fe
         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
bf5ceeff
 #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf,  AV_PIX_FMT_YUV422##suf,  AV_PIX_FMT_YUV444##suf
 #define PF_ALPHA(suf)   AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
 #define PF(suf)         PF_NOALPHA(suf), PF_ALPHA(suf)
         PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
         AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
6150bec3
         AV_PIX_FMT_NONE
     };
 
a0854c08
     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     if (!fmts_list)
         return AVERROR(ENOMEM);
     return ff_set_common_formats(ctx, fmts_list);
6150bec3
 }
 
 static int config_input_ref(AVFilterLink *inlink)
 {
0227b429
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
6150bec3
     AVFilterContext *ctx  = inlink->dst;
     PSNRContext *s = ctx->priv;
9264bb7e
     double average_max;
0303d439
     unsigned sum;
6150bec3
     int j;
 
0227b429
     s->nb_components = desc->nb_components;
6150bec3
     if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
         ctx->inputs[0]->h != ctx->inputs[1]->h) {
f21d0beb
         av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
6150bec3
         return AVERROR(EINVAL);
     }
     if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
         av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
         return AVERROR(EINVAL);
     }
 
5d8e836d
     s->max[0] = (1 << desc->comp[0].depth) - 1;
     s->max[1] = (1 << desc->comp[1].depth) - 1;
     s->max[2] = (1 << desc->comp[2].depth) - 1;
     s->max[3] = (1 << desc->comp[3].depth) - 1;
6150bec3
 
     s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
     s->comps[0] = s->is_rgb ? 'r' : 'y' ;
     s->comps[1] = s->is_rgb ? 'g' : 'u' ;
     s->comps[2] = s->is_rgb ? 'b' : 'v' ;
     s->comps[3] = 'a';
 
21f94684
     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
0227b429
     s->planeheight[0] = s->planeheight[3] = inlink->h;
21f94684
     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
0227b429
     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
0303d439
     sum = 0;
     for (j = 0; j < s->nb_components; j++)
         sum += s->planeheight[j] * s->planewidth[j];
9264bb7e
     average_max = 0;
0303d439
     for (j = 0; j < s->nb_components; j++) {
         s->planeweight[j] = (double) s->planeheight[j] * s->planewidth[j] / sum;
9264bb7e
         average_max += s->max[j] * s->planeweight[j];
0303d439
     }
9264bb7e
     s->average_max = lrint(average_max);
6150bec3
 
5d8e836d
     s->dsp.sse_line = desc->comp[0].depth > 8 ? sse_line_16bit : sse_line_8bit;
ae4c9dde
     if (ARCH_X86)
5d8e836d
         ff_psnr_init_x86(&s->dsp, desc->comp[0].depth);
bf5ceeff
 
6150bec3
     return 0;
 }
 
 static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
231e5015
     PSNRContext *s = ctx->priv;
6150bec3
     AVFilterLink *mainlink = ctx->inputs[0];
231e5015
     int ret;
6150bec3
 
5f5dcf44
     ret = ff_framesync_init_dualinput(&s->fs, ctx);
3bd11df4
     if (ret < 0)
         return ret;
6150bec3
     outlink->w = mainlink->w;
     outlink->h = mainlink->h;
     outlink->time_base = mainlink->time_base;
     outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
     outlink->frame_rate = mainlink->frame_rate;
5f5dcf44
     if ((ret = ff_framesync_configure(&s->fs)) < 0)
231e5015
         return ret;
6150bec3
 
     return 0;
 }
 
3bd11df4
 static int activate(AVFilterContext *ctx)
6150bec3
 {
3bd11df4
     PSNRContext *s = ctx->priv;
5f5dcf44
     return ff_framesync_activate(&s->fs);
6150bec3
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
     PSNRContext *s = ctx->priv;
 
     if (s->nb_frames > 0) {
0303d439
         int j;
         char buf[256];
 
         buf[0] = 0;
3bb58c37
         for (j = 0; j < s->nb_components; j++) {
             int c = s->is_rgb ? s->rgba_map[j] : j;
81d7f118
             av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[j],
3bb58c37
                         get_psnr(s->mse_comp[c], s->nb_frames, s->max[c]));
         }
81d7f118
         av_log(ctx, AV_LOG_INFO, "PSNR%s average:%f min:%f max:%f\n",
0303d439
                buf,
6150bec3
                get_psnr(s->mse, s->nb_frames, s->average_max),
                get_psnr(s->max_mse, 1, s->average_max),
                get_psnr(s->min_mse, 1, s->average_max));
     }
 
5f5dcf44
     ff_framesync_uninit(&s->fs);
6150bec3
 
1ec8c155
     if (s->stats_file && s->stats_file != stdout)
6150bec3
         fclose(s->stats_file);
 }
 
 static const AVFilterPad psnr_inputs[] = {
     {
b211607b
         .name         = "main",
         .type         = AVMEDIA_TYPE_VIDEO,
6150bec3
     },{
b211607b
         .name         = "reference",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input_ref,
6150bec3
     },
     { NULL }
 };
 
 static const AVFilterPad psnr_outputs[] = {
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
         .config_props  = config_output,
     },
     { NULL }
 };
 
325f6e0a
 AVFilter ff_vf_psnr = {
b211607b
     .name          = "psnr",
     .description   = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
3bd11df4
     .preinit       = psnr_framesync_preinit,
b211607b
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
3bd11df4
     .activate      = activate,
b211607b
     .priv_size     = sizeof(PSNRContext),
     .priv_class    = &psnr_class,
     .inputs        = psnr_inputs,
     .outputs       = psnr_outputs,
6150bec3
 };