[FFmpeg-devel] [PATCH] avfilter: add ssim filter
Paul B Mahol
onemda at gmail.com
Mon Jun 22 18:23:57 CEST 2015
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index fc9f455..55cd055 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -197,6 +197,7 @@ OBJS-$(CONFIG_SIGNALSTATS_FILTER) += vf_signalstats.o
OBJS-$(CONFIG_SMARTBLUR_FILTER) += vf_smartblur.o
OBJS-$(CONFIG_SPLIT_FILTER) += split.o
OBJS-$(CONFIG_SPP_FILTER) += vf_spp.o
+OBJS-$(CONFIG_SSIM_FILTER) += vf_ssim.o dualinput.o framesync.o
OBJS-$(CONFIG_STEREO3D_FILTER) += vf_stereo3d.o
OBJS-$(CONFIG_SUBTITLES_FILTER) += vf_subtitles.o
OBJS-$(CONFIG_SUPER2XSAI_FILTER) += vf_super2xsai.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 415ed1b..3898498 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -212,6 +212,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(SMARTBLUR, smartblur, vf);
REGISTER_FILTER(SPLIT, split, vf);
REGISTER_FILTER(SPP, spp, vf);
+ REGISTER_FILTER(SSIM, ssim, vf);
REGISTER_FILTER(STEREO3D, stereo3d, vf);
REGISTER_FILTER(SUBTITLES, subtitles, vf);
REGISTER_FILTER(SUPER2XSAI, super2xsai, vf);
diff --git a/libavfilter/vf_ssim.c b/libavfilter/vf_ssim.c
new file mode 100644
index 0000000..035a3a5
--- /dev/null
+++ b/libavfilter/vf_ssim.c
@@ -0,0 +1,335 @@
+/*
+ * 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 SSIM 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 SSIMContext {
+ const AVClass *class;
+ FFDualInputContext dinput;
+ FILE *stats_file;
+ char *stats_file_str;
+ uint64_t nb_frames;
+ double ssim[4];
+ char comps[4];
+ uint8_t rgba_map[4];
+ int planewidth[4];
+ int planeheight[4];
+} SSIMContext;
+
+#define OFFSET(x) offsetof(SSIMContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption ssim_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(ssim);
+
+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);
+ }
+}
+
+static void ssim_parms_8x8(uint8_t *s, int sp, uint8_t *r, int rp,
+ uint64_t *sum_s, uint64_t *sum_r, uint64_t *sum_sq_s,
+ uint64_t *sum_sq_r, uint64_t *sum_sxr)
+{
+ int i, j;
+
+ for (i = 0; i < 8; i++, s += sp, r += rp) {
+ for (j = 0; j < 8; j++) {
+ *sum_s += s[j];
+ *sum_r += r[j];
+ *sum_sq_s += s[j] * s[j];
+ *sum_sq_r += r[j] * r[j];
+ *sum_sxr += s[j] * r[j];
+ }
+ }
+}
+
+const static int64_t cc1 = 26634; // (64^2*(.01*255)^2
+const static int64_t cc2 = 239708; // (64^2*(.03*255)^2
+
+static double similarity(uint64_t sum_s, uint64_t sum_r, uint64_t sum_sq_s,
+ uint64_t sum_sq_r, uint64_t sum_sxr, int count)
+{
+ int64_t ssim_n, ssim_d;
+ int64_t c1, c2;
+
+ //scale the constants by number of pixels
+ c1 = (cc1 * count * count) >> 12;
+ c2 = (cc2 * count * count) >> 12;
+
+ ssim_n = (2 * sum_s * sum_r + c1)*((int64_t)2 * count * sum_sxr -
+ (int64_t)2 * sum_s * sum_r + c2);
+
+ ssim_d = (sum_s * sum_s + sum_r * sum_r+c1) * ((int64_t)count * sum_sq_s -
+ (int64_t)sum_s * sum_s +
+ (int64_t)count * sum_sq_r -
+ (int64_t)sum_r * sum_r + c2);
+
+ return ssim_n * 1.0 / ssim_d;
+}
+
+static double ssim_8x8(uint8_t *s, int sp, uint8_t *r, int rp)
+{
+ uint64_t sum_s = 0, sum_r= 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0;
+
+ ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, &sum_sxr);
+
+ return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64);
+}
+
+static double ssim(uint8_t *main, int main_stride,
+ uint8_t *ref, int ref_stride,
+ int width, int height)
+{
+ double total = 0;
+ int samples = 0;
+ int i, j;
+
+ for (i = 0; i < height - 8; i += 4) {
+ main += main_stride * 4;
+ ref += ref_stride * 4;
+
+ for (j = 0; j < width - 8; j += 4) {
+ total += ssim_8x8(main + j, main_stride, ref + j, ref_stride);
+ samples++;
+ }
+ }
+
+ return total / samples;
+}
+
+static AVFrame *do_ssim(AVFilterContext *ctx, AVFrame *main,
+ const AVFrame *ref)
+{
+ SSIMContext *s = ctx->priv;
+ double c[4], ssimv;
+ AVDictionary **metadata = avpriv_frame_get_metadatap(main);
+
+ s->nb_frames++;
+
+ c[0] = ssim(main->data[0], main->linesize[0],
+ ref->data[0], ref->linesize[0],
+ s->planewidth[0], s->planeheight[0]);
+
+ c[1] = ssim(main->data[1], main->linesize[1],
+ ref->data[1], ref->linesize[1],
+ s->planewidth[1], s->planeheight[1]);
+
+ c[2] = ssim(main->data[2], main->linesize[2],
+ ref->data[2], ref->linesize[2],
+ s->planewidth[2], s->planeheight[2]);
+
+ ssimv = c[0] * .8 + .1 * (c[1] + c[2]);
+
+ set_meta(metadata, "lavfi.ssim.", s->comps[0], c[0]);
+ set_meta(metadata, "lavfi.ssim.", s->comps[1], c[1]);
+ set_meta(metadata, "lavfi.ssim.", s->comps[2], c[2]);
+ set_meta(metadata, "lavfi.ssim.All", 0, ssimv);
+
+ if (s->stats_file) {
+ fprintf(s->stats_file, "n:%"PRId64" %c:%f %c:%f %c:%f All:%f\n",
+ s->nb_frames, s->comps[0], c[0], s->comps[1],
+ c[1], s->comps[2], c[2], ssimv);
+ }
+
+ s->ssim[0] += c[0];
+ s->ssim[1] += c[1];
+ s->ssim[2] += c[2];
+
+ return main;
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+ SSIMContext *s = ctx->priv;
+
+ 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_ssim;
+ return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum AVPixelFormat pix_fmts[] = {
+#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_NONE
+ };
+
+ AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+ if (!fmts_list)
+ return AVERROR(ENOMEM);
+ return ff_set_common_formats(ctx, fmts_list);
+}
+
+static int config_input_ref(AVFilterLink *inlink)
+{
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+ AVFilterContext *ctx = inlink->dst;
+ SSIMContext *s = ctx->priv;
+ int is_rgb;
+
+ 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 height 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);
+ }
+
+ is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
+ s->comps[0] = is_rgb ? 'R' : 'Y';
+ s->comps[1] = is_rgb ? 'G' : 'U';
+ s->comps[2] = is_rgb ? 'B' : 'V';
+ s->comps[3] = 'A';
+
+ s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
+ s->planeheight[0] = s->planeheight[3] = inlink->h;
+ s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
+ s->planewidth[0] = s->planewidth[3] = inlink->w;
+
+ return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ SSIMContext *s = ctx->priv;
+ AVFilterLink *mainlink = ctx->inputs[0];
+ int ret;
+
+ 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;
+ if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
+ return ret;
+
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
+{
+ SSIMContext *s = inlink->dst->priv;
+ return ff_dualinput_filter_frame(&s->dinput, inlink, inpicref);
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+ SSIMContext *s = outlink->src->priv;
+ return ff_dualinput_request_frame(&s->dinput, outlink);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ SSIMContext *s = ctx->priv;
+
+ if (s->nb_frames > 0) {
+ av_log(ctx, AV_LOG_INFO, "SSIM %c:%f %c:%f %c:%f All: %f\n",
+ s->comps[0], s->ssim[0] / s->nb_frames,
+ s->comps[1], s->ssim[1] / s->nb_frames,
+ s->comps[2], s->ssim[2] / s->nb_frames,
+ (s->ssim[0] * .8 + .1 * (s->ssim[1] + s->ssim[2])) / s->nb_frames);
+ }
+
+ ff_dualinput_uninit(&s->dinput);
+
+ if (s->stats_file)
+ fclose(s->stats_file);
+}
+
+static const AVFilterPad ssim_inputs[] = {
+ {
+ .name = "main",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame,
+ },{
+ .name = "reference",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame,
+ .config_props = config_input_ref,
+ },
+ { NULL }
+};
+
+static const AVFilterPad ssim_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = config_output,
+ .request_frame = request_frame,
+ },
+ { NULL }
+};
+
+AVFilter ff_vf_ssim = {
+ .name = "ssim",
+ .description = NULL_IF_CONFIG_SMALL("Calculate the SSIM between two video streams."),
+ .init = init,
+ .uninit = uninit,
+ .query_formats = query_formats,
+ .priv_size = sizeof(SSIMContext),
+ .priv_class = &ssim_class,
+ .inputs = ssim_inputs,
+ .outputs = ssim_outputs,
+};
--
1.7.11.2
More information about the ffmpeg-devel
mailing list