2 * Copyright (c) 2003-2013 Loren Merritt
3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 /* Computes the Structural Similarity Metric between two video streams.
24 * Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli,
25 * "Image quality assessment: From error visibility to structural similarity,"
26 * IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.
28 * To improve speed, this implementation uses the standard approximation of
29 * overlapped 8x8 block sums, rather than the original gaussian weights.
34 * Caculate the SSIM between two input videos.
37 #include "libavutil/avstring.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/pixdesc.h"
41 #include "dualinput.h"
42 #include "drawutils.h"
48 typedef struct SSIMContext {
50 FFDualInputContext dinput;
55 double ssim[4], ssim_total;
66 #define OFFSET(x) offsetof(SSIMContext, x)
67 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
69 static const AVOption ssim_options[] = {
70 {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
71 {"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
75 AVFILTER_DEFINE_CLASS(ssim);
77 static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
80 snprintf(value, sizeof(value), "%0.2f", d);
83 snprintf(key2, sizeof(key2), "%s%c", key, comp);
84 av_dict_set(metadata, key2, value, 0);
86 av_dict_set(metadata, key, value, 0);
90 static void ssim_4x4xn(const uint8_t *main, ptrdiff_t main_stride,
91 const uint8_t *ref, ptrdiff_t ref_stride,
92 int (*sums)[4], int width)
96 for (z = 0; z < width; z++) {
97 uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
99 for (y = 0; y < 4; y++) {
100 for (x = 0; x < 4; x++) {
101 int a = main[x + y * main_stride];
102 int b = ref[x + y * ref_stride];
121 static float ssim_end1(int s1, int s2, int ss, int s12)
123 static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
124 static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
130 int vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
131 int covar = fs12 * 64 - fs1 * fs2;
133 return (float)(2 * fs1 * fs2 + ssim_c1) * (float)(2 * covar + ssim_c2)
134 / ((float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (float)(vars + ssim_c2));
137 static float ssim_endn(const int (*sum0)[4], const int (*sum1)[4], int width)
142 for (i = 0; i < width; i++)
143 ssim += ssim_end1(sum0[i][0] + sum0[i + 1][0] + sum1[i][0] + sum1[i + 1][0],
144 sum0[i][1] + sum0[i + 1][1] + sum1[i][1] + sum1[i + 1][1],
145 sum0[i][2] + sum0[i + 1][2] + sum1[i][2] + sum1[i + 1][2],
146 sum0[i][3] + sum0[i + 1][3] + sum1[i][3] + sum1[i + 1][3]);
150 static float ssim_plane(SSIMDSPContext *dsp,
151 uint8_t *main, int main_stride,
152 uint8_t *ref, int ref_stride,
153 int width, int height, void *temp)
157 int (*sum0)[4] = temp;
158 int (*sum1)[4] = sum0 + (width >> 2) + 3;
163 for (y = 1; y < height; y++) {
164 for (; z <= y; z++) {
165 FFSWAP(void*, sum0, sum1);
166 dsp->ssim_4x4_line(&main[4 * z * main_stride], main_stride,
167 &ref[4 * z * ref_stride], ref_stride,
171 ssim += dsp->ssim_end_line((const int (*)[4])sum0, (const int (*)[4])sum1, width - 1);
174 return ssim / ((height - 1) * (width - 1));
177 static double ssim_db(double ssim, double weight)
179 return 10 * (log(weight) / log(10) - log(weight - ssim) / log(10));
182 static AVFrame *do_ssim(AVFilterContext *ctx, AVFrame *main,
185 AVDictionary **metadata = avpriv_frame_get_metadatap(main);
186 SSIMContext *s = ctx->priv;
187 float c[4], ssimv = 0.0;
192 for (i = 0; i < s->nb_components; i++) {
193 c[i] = ssim_plane(&s->dsp, main->data[i], main->linesize[i],
194 ref->data[i], ref->linesize[i],
195 s->planewidth[i], s->planeheight[i], s->temp);
196 ssimv += s->coefs[i] * c[i];
199 for (i = 0; i < s->nb_components; i++) {
200 int cidx = s->is_rgb ? s->rgba_map[i] : i;
201 set_meta(metadata, "lavfi.ssim.", s->comps[i], c[cidx]);
203 s->ssim_total += ssimv;
205 set_meta(metadata, "lavfi.ssim.All", 0, ssimv);
206 set_meta(metadata, "lavfi.ssim.dB", 0, ssim_db(ssimv, 1.0));
209 fprintf(s->stats_file, "n:%"PRId64" ", s->nb_frames);
211 for (i = 0; i < s->nb_components; i++) {
212 int cidx = s->is_rgb ? s->rgba_map[i] : i;
213 fprintf(s->stats_file, "%c:%f ", s->comps[i], c[cidx]);
216 fprintf(s->stats_file, "All:%f (%f)\n", ssimv, ssim_db(ssimv, 1.0));
222 static av_cold int init(AVFilterContext *ctx)
224 SSIMContext *s = ctx->priv;
226 if (s->stats_file_str) {
227 s->stats_file = fopen(s->stats_file_str, "w");
228 if (!s->stats_file) {
229 int err = AVERROR(errno);
231 av_strerror(err, buf, sizeof(buf));
232 av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
233 s->stats_file_str, buf);
238 s->dinput.process = do_ssim;
239 s->dinput.shortest = 1;
240 s->dinput.repeatlast = 0;
244 static int query_formats(AVFilterContext *ctx)
246 static const enum AVPixelFormat pix_fmts[] = {
248 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
249 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
250 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
251 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
256 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
258 return AVERROR(ENOMEM);
259 return ff_set_common_formats(ctx, fmts_list);
262 static int config_input_ref(AVFilterLink *inlink)
264 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
265 AVFilterContext *ctx = inlink->dst;
266 SSIMContext *s = ctx->priv;
269 s->nb_components = desc->nb_components;
271 if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
272 ctx->inputs[0]->h != ctx->inputs[1]->h) {
273 av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
274 return AVERROR(EINVAL);
276 if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
277 av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
278 return AVERROR(EINVAL);
281 s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
282 s->comps[0] = s->is_rgb ? 'R' : 'Y';
283 s->comps[1] = s->is_rgb ? 'G' : 'U';
284 s->comps[2] = s->is_rgb ? 'B' : 'V';
287 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
288 s->planeheight[0] = s->planeheight[3] = inlink->h;
289 s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
290 s->planewidth[0] = s->planewidth[3] = inlink->w;
291 for (i = 0; i < s->nb_components; i++)
292 sum += s->planeheight[i] * s->planewidth[i];
293 for (i = 0; i < s->nb_components; i++)
294 s->coefs[i] = (double) s->planeheight[i] * s->planewidth[i] / sum;
296 s->temp = av_malloc((2 * inlink->w + 12) * sizeof(*s->temp));
298 return AVERROR(ENOMEM);
300 s->dsp.ssim_4x4_line = ssim_4x4xn;
301 s->dsp.ssim_end_line = ssim_endn;
303 ff_ssim_init_x86(&s->dsp);
308 static int config_output(AVFilterLink *outlink)
310 AVFilterContext *ctx = outlink->src;
311 SSIMContext *s = ctx->priv;
312 AVFilterLink *mainlink = ctx->inputs[0];
315 outlink->w = mainlink->w;
316 outlink->h = mainlink->h;
317 outlink->time_base = mainlink->time_base;
318 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
319 outlink->frame_rate = mainlink->frame_rate;
321 if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
327 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
329 SSIMContext *s = inlink->dst->priv;
330 return ff_dualinput_filter_frame(&s->dinput, inlink, buf);
333 static int request_frame(AVFilterLink *outlink)
335 SSIMContext *s = outlink->src->priv;
336 return ff_dualinput_request_frame(&s->dinput, outlink);
339 static av_cold void uninit(AVFilterContext *ctx)
341 SSIMContext *s = ctx->priv;
343 if (s->nb_frames > 0) {
347 for (i = 0; i < s->nb_components; i++) {
348 int c = s->is_rgb ? s->rgba_map[i] : i;
349 av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[i], s->ssim[c] / s->nb_frames);
351 av_log(ctx, AV_LOG_INFO, "SSIM%s All:%f (%f)\n", buf,
352 s->ssim_total / s->nb_frames, ssim_db(s->ssim_total, s->nb_frames));
355 ff_dualinput_uninit(&s->dinput);
358 fclose(s->stats_file);
363 static const AVFilterPad ssim_inputs[] = {
366 .type = AVMEDIA_TYPE_VIDEO,
367 .filter_frame = filter_frame,
370 .type = AVMEDIA_TYPE_VIDEO,
371 .filter_frame = filter_frame,
372 .config_props = config_input_ref,
377 static const AVFilterPad ssim_outputs[] = {
380 .type = AVMEDIA_TYPE_VIDEO,
381 .config_props = config_output,
382 .request_frame = request_frame,
387 AVFilter ff_vf_ssim = {
389 .description = NULL_IF_CONFIG_SMALL("Calculate the SSIM between two video streams."),
392 .query_formats = query_formats,
393 .priv_size = sizeof(SSIMContext),
394 .priv_class = &ssim_class,
395 .inputs = ssim_inputs,
396 .outputs = ssim_outputs,