[FFmpeg-cvslog] lavfi: add psnr filter

Paul B Mahol git at videolan.org
Mon Jul 8 17:03:40 CEST 2013


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Sat Jul  6 00:21:12 2013 +0000| [6150bec3f873ec3777496ac6a184d4b1190d6f16] | committer: Paul B Mahol

lavfi: add psnr filter

Signed-off-by: Paul B Mahol <onemda at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=6150bec3f873ec3777496ac6a184d4b1190d6f16
---

 Changelog                |    1 +
 doc/filters.texi         |   73 +++++++++++
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/version.h    |    4 +-
 libavfilter/vf_psnr.c    |  324 ++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 402 insertions(+), 2 deletions(-)

diff --git a/Changelog b/Changelog
index 2cf2805..17acaa7 100644
--- a/Changelog
+++ b/Changelog
@@ -69,6 +69,7 @@ version <next>:
 - rotate filter
 - spp filter ported from libmpcodecs
 - libgme support
+- psnr filter
 
 
 version 1.2:
diff --git a/doc/filters.texi b/doc/filters.texi
index 2ac0c46..27fe084 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -5837,6 +5837,79 @@ pp=hb|y/vb|a
 @end example
 @end itemize
 
+ at section psnr
+
+Obtain the average, maximum and minimum PSNR (Peak Signal to Noise
+Ratio) between two input videos.
+
+This filter takes in input two input videos, the first input is
+considered the "main" source and is passed unchanged to the
+output. The second input is used as a "reference" video for computing
+the PSNR.
+
+Both video inputs must have the same resolution and pixel format for
+this filter to work correctly. Also it assumes that both inputs
+have the same number of frames, which are compared one by one.
+
+The obtained average PSNR is printed through the logging system.
+
+The filter stores the accumulated MSE (mean squared error) of each
+frame, and at the end of the processing it is averaged across all frames
+equally, and the following formula is applied to obtain the PSNR:
+
+ at example
+PSNR = 10*log10(MAX^2/MSE)
+ at end example
+
+Where MAX is the average of the maximum values of each component of the
+image.
+
+The filter accepts parameters as a list of @var{key}=@var{value} pairs,
+separated by ":".
+
+The description of the accepted parameters follows.
+
+ at table @option
+ at item stats_file, f
+If specified the filter will use the named file to save the PSNR of
+each individual frame.
+ at end table
+
+The file printed if @var{stats_file} is selected, contains a sequence of
+key/value pairs of the form @var{key}:@var{value} for each compared
+couple of frames.
+
+The shown line contains .
+
+A description of each shown parameter follows:
+
+ at table @option
+ at item n
+sequential number of the input frame, starting from 1
+
+ at item mse_average
+Mean Square Error pixel-by-pixel average difference of the compared
+frames, averaged over all the image components.
+
+ at item mse_y, mse_u, mse_v, mse_r, mse_g, mse_g, mse_a
+Mean Square Error pixel-by-pixel average difference of the compared
+frames for the component specified by the suffix.
+
+ at item psnr_y, psnr_u, psnr_v, psnr_r, psnr_g, psnr_g, psnr_a
+Peak Signal to Noise ratio of the compared frames for the component
+specified by the suffix.
+ at end table
+
+For example:
+ at example
+movie=ref_movie.mpg, setpts=PTS-STARTPTS [main];
+[main][ref] psnr="stats_file=stats.log" [out]
+ at end example
+
+On this example the input file being processed is compared with the
+reference file @file{ref_movie.mpg}. The PSNR of each individual frame
+is stored in @file{stats.log}.
+
 @section removelogo
 
 Suppress a TV station logo, using an image file to determine which
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 66509c5..cf76ee1 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -167,6 +167,7 @@ OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
+OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
 OBJS-$(CONFIG_ROTATE_FILTER)                 += vf_rotate.o
 OBJS-$(CONFIG_SEPARATEFIELDS_FILTER)         += vf_separatefields.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 85a793f..9a11feb 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -162,6 +162,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(PERMS,          perms,          vf);
     REGISTER_FILTER(PIXDESCTEST,    pixdesctest,    vf);
     REGISTER_FILTER(PP,             pp,             vf);
+    REGISTER_FILTER(PSNR,           psnr,           vf);
     REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
     REGISTER_FILTER(ROTATE,         rotate,         vf);
     REGISTER_FILTER(SAB,            sab,            vf);
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 59060db..15ddcdd 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,8 +30,8 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFILTER_VERSION_MAJOR  3
-#define LIBAVFILTER_VERSION_MINOR  78
-#define LIBAVFILTER_VERSION_MICRO 103
+#define LIBAVFILTER_VERSION_MINOR  79
+#define LIBAVFILTER_VERSION_MICRO 100
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
                                                LIBAVFILTER_VERSION_MINOR, \
diff --git a/libavfilter/vf_psnr.c b/libavfilter/vf_psnr.c
new file mode 100644
index 0000000..6b4fecb
--- /dev/null
+++ b/libavfilter/vf_psnr.c
@@ -0,0 +1,324 @@
+/*
+ * Copyright (c) 2011 Roger Pau Monné <roger.pau at entel.upc.edu>
+ * 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.
+ */
+
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "dualinput.h"
+#include "drawutils.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct PSNRContext {
+    const AVClass *class;
+    FFDualInputContext dinput;
+    double mse, min_mse, max_mse;
+    uint64_t nb_frames;
+    FILE *stats_file;
+    char *stats_file_str;
+    int max[4], average_max;
+    int is_rgb;
+    uint8_t rgba_map[4];
+    char comps[4];
+    const AVPixFmtDescriptor *desc;
+} 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 },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(psnr);
+
+static inline int pow2(int base)
+{
+    return base*base;
+}
+
+static inline double get_psnr(double mse, uint64_t nb_frames, int max)
+{
+    return 10.0 * log(pow2(max) / (mse / nb_frames)) / log(10.0);
+}
+
+static inline
+void compute_images_mse(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, const AVPixFmtDescriptor *desc,
+                        double mse[4])
+{
+    int i, c, j;
+
+    for (c = 0; c < desc->nb_components; c++) {
+        int hsub = c == 1 || c == 2 ? desc->log2_chroma_w : 0;
+        int vsub = c == 1 || c == 2 ? desc->log2_chroma_h : 0;
+        const int outw = FF_CEIL_RSHIFT(w, hsub);
+        const int outh = FF_CEIL_RSHIFT(h, vsub);
+        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];
+        int m = 0;
+
+        for (i = 0; i < outh; i++) {
+            for (j = 0; j < outw; j++)
+                m += pow2(main_line[j] - ref_line[j]);
+            ref_line += ref_linesize;
+            main_line += main_linesize;
+        }
+        mse[c] = m / (outw * outh);
+    }
+}
+
+#define SET_META(key, comp, value) \
+    snprintf(buf, sizeof(buf), "%0.2f", value);  \
+    av_dict_set(metadata, #key #comp, buf, 0); \
+
+static AVFrame *do_psnr(AVFilterContext *ctx, AVFrame *main,
+                        const AVFrame *ref)
+{
+    PSNRContext *s = ctx->priv;
+    double comp_mse[4], mse = 0;
+    char buf[32];
+    int j, c;
+    AVDictionary **metadata = avpriv_frame_get_metadatap(main);
+
+    compute_images_mse((const uint8_t **)main->data, main->linesize,
+                       (const uint8_t **)ref->data, ref->linesize,
+                       main->width, main->height, s->desc, comp_mse);
+
+    for (j = 0; j < s->desc->nb_components; j++)
+        mse += comp_mse[j];
+    mse /= s->desc->nb_components;
+
+    s->min_mse = FFMIN(s->min_mse, mse);
+    s->max_mse = FFMAX(s->max_mse, mse);
+
+    s->mse += mse;
+    s->nb_frames++;
+
+    for (j = 0; j < s->desc->nb_components; j++) {
+        c = s->is_rgb ? s->rgba_map[j] : j;
+        SET_META("lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
+        SET_META("lavfi.psnr.mse_avg", "", mse);
+        SET_META("lavfi.psnr.s.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
+        SET_META("lavfi.psnr.s_avg", "", get_psnr(mse, 1, s->average_max));
+    }
+
+    if (s->stats_file) {
+        fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
+        for (j = 0; j < s->desc->nb_components; j++) {
+            c = s->is_rgb ? s->rgba_map[j] : j;
+            fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
+        }
+        for (j = 0; j < s->desc->nb_components; j++) {
+            c = s->is_rgb ? s->rgba_map[j] : j;
+            fprintf(s->stats_file, "s%c:%0.2f ", s->comps[j],
+                    get_psnr(comp_mse[c], 1, s->max[c]));
+        }
+        fprintf(s->stats_file, "\n");
+    }
+
+    return main;
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    PSNRContext *s = ctx->priv;
+
+    s->min_mse = +INFINITY;
+    s->max_mse = -INFINITY;
+
+    if (s->stats_file_str) {
+        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;
+        }
+    }
+
+    s->dinput.process = do_psnr;
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum PixelFormat pix_fmts[] = {
+        AV_PIX_FMT_GRAY8,
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P,
+        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,
+        AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_input_ref(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx  = inlink->dst;
+    PSNRContext *s = ctx->priv;
+    int j;
+
+    s->desc = av_pix_fmt_desc_get(inlink->format);
+    if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
+        ctx->inputs[0]->h != ctx->inputs[1]->h) {
+        av_log(ctx, AV_LOG_ERROR, "Width and heigth of input videos must be same.\n");
+        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);
+    }
+
+    switch (inlink->format) {
+    case AV_PIX_FMT_YUV410P:
+    case AV_PIX_FMT_YUV411P:
+    case AV_PIX_FMT_YUV420P:
+    case AV_PIX_FMT_YUV422P:
+    case AV_PIX_FMT_YUV440P:
+    case AV_PIX_FMT_YUV444P:
+    case AV_PIX_FMT_YUVA420P:
+    case AV_PIX_FMT_YUVA422P:
+    case AV_PIX_FMT_YUVA444P:
+        s->max[0] = 235;
+        s->max[3] = 255;
+        s->max[1] = s->max[2] = 240;
+        break;
+    default:
+        s->max[0] = s->max[1] = s->max[2] = s->max[3] = 255;
+    }
+
+    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';
+
+    for (j = 0; j < s->desc->nb_components; j++)
+        s->average_max += s->max[j];
+    s->average_max /= s->desc->nb_components;
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterLink *mainlink = ctx->inputs[0];
+
+    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;
+
+    return 0;
+}
+
+static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
+{
+    PSNRContext *s = inlink->dst->priv;
+    return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
+}
+
+static int filter_frame_ref(AVFilterLink *inlink, AVFrame *inpicref)
+{
+    PSNRContext *s = inlink->dst->priv;
+    return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    PSNRContext *s = outlink->src->priv;
+    return ff_dualinput_request_frame(&s->dinput, outlink);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    PSNRContext *s = ctx->priv;
+
+    if (s->nb_frames > 0) {
+        av_log(ctx, AV_LOG_INFO, "PSNR average:%0.2f min:%0.2f max:%0.2f\n",
+               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));
+    }
+
+    ff_dualinput_uninit(&s->dinput);
+
+    if (s->stats_file)
+        fclose(s->stats_file);
+}
+
+static const AVFilterPad psnr_inputs[] = {
+    {
+        .name             = "main",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .filter_frame     = filter_frame_main,
+    },{
+        .name             = "reference",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .filter_frame     = filter_frame_ref,
+        .config_props     = config_input_ref,
+    },
+    { NULL }
+};
+
+static const AVFilterPad psnr_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_output,
+        .request_frame = request_frame,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_psnr = {
+    .name           = "psnr",
+    .description    = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
+    .init           = init,
+    .uninit         = uninit,
+    .query_formats  = query_formats,
+    .priv_size      = sizeof(PSNRContext),
+    .priv_class     = &psnr_class,
+    .inputs         = psnr_inputs,
+    .outputs        = psnr_outputs,
+};



More information about the ffmpeg-cvslog mailing list