Commit f353b851 authored by Ronald S. Bultje's avatar Ronald S. Bultje Committed by Michael Niedermayer

vf_ssim: fix s->coefs for yuv with non-4:2:0 subsampling.

Reviewed-by: 's avatarPaul B Mahol <onemda@gmail.com>
Signed-off-by: 's avatarMichael Niedermayer <michael@niedermayer.cc>
parent d2e5297e
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
* Caculate the SSIM between two input videos. * Caculate the SSIM between two input videos.
*/ */
#include "libavutil/avstring.h"
#include "libavutil/opt.h" #include "libavutil/opt.h"
#include "libavutil/pixdesc.h" #include "libavutil/pixdesc.h"
#include "avfilter.h" #include "avfilter.h"
...@@ -50,13 +51,14 @@ typedef struct SSIMContext { ...@@ -50,13 +51,14 @@ typedef struct SSIMContext {
char *stats_file_str; char *stats_file_str;
int nb_components; int nb_components;
uint64_t nb_frames; uint64_t nb_frames;
double ssim[4]; double ssim[4], ssim_total;
char comps[4]; char comps[4];
int *coefs; float coefs[4];
uint8_t rgba_map[4]; uint8_t rgba_map[4];
int planewidth[4]; int planewidth[4];
int planeheight[4]; int planeheight[4];
int *temp; int *temp;
int is_rgb;
} SSIMContext; } SSIMContext;
#define OFFSET(x) offsetof(SSIMContext, x) #define OFFSET(x) offsetof(SSIMContext, x)
...@@ -70,10 +72,6 @@ static const AVOption ssim_options[] = { ...@@ -70,10 +72,6 @@ static const AVOption ssim_options[] = {
AVFILTER_DEFINE_CLASS(ssim); AVFILTER_DEFINE_CLASS(ssim);
static const int rgb_coefs[4] = { 1, 1, 1, 3};
static const int yuv_coefs[4] = { 4, 1, 1, 6};
static const int gray_coefs[4] = { 1, 0, 0, 1};
static void set_meta(AVDictionary **metadata, const char *key, char comp, float d) static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
{ {
char value[128]; char value[128];
...@@ -186,37 +184,38 @@ static AVFrame *do_ssim(AVFilterContext *ctx, AVFrame *main, ...@@ -186,37 +184,38 @@ static AVFrame *do_ssim(AVFilterContext *ctx, AVFrame *main,
{ {
AVDictionary **metadata = avpriv_frame_get_metadatap(main); AVDictionary **metadata = avpriv_frame_get_metadatap(main);
SSIMContext *s = ctx->priv; SSIMContext *s = ctx->priv;
float c[4], ssimv; float c[4], ssimv = 0.0;
int i; int i;
s->nb_frames++; s->nb_frames++;
for (i = 0; i < s->nb_components; i++) for (i = 0; i < s->nb_components; i++) {
c[i] = ssim_plane(main->data[i], main->linesize[i], c[i] = ssim_plane(main->data[i], main->linesize[i],
ref->data[i], ref->linesize[i], ref->data[i], ref->linesize[i],
s->planewidth[i], s->planeheight[i], s->temp); s->planewidth[i], s->planeheight[i], s->temp);
ssimv += s->coefs[i] * c[i];
ssimv = (c[0] * s->coefs[0] + c[1] * s->coefs[1] + c[2] * s->coefs[2]) / s->coefs[3]; s->ssim[i] += c[i];
}
for (i = 0; i < s->nb_components; i++) for (i = 0; i < s->nb_components; i++) {
set_meta(metadata, "lavfi.ssim.", s->comps[i], c[i]); int cidx = s->is_rgb ? s->rgba_map[i] : i;
set_meta(metadata, "lavfi.ssim.", s->comps[i], c[cidx]);
}
s->ssim_total += ssimv;
set_meta(metadata, "lavfi.ssim.All", 0, ssimv); set_meta(metadata, "lavfi.ssim.All", 0, ssimv);
set_meta(metadata, "lavfi.ssim.dB", 0, ssim_db(c[0] * s->coefs[0] + c[1] * s->coefs[1] + c[2] * s->coefs[2], s->coefs[3])); set_meta(metadata, "lavfi.ssim.dB", 0, ssim_db(ssimv, 1.0));
if (s->stats_file) { if (s->stats_file) {
fprintf(s->stats_file, "n:%"PRId64" ", s->nb_frames); fprintf(s->stats_file, "n:%"PRId64" ", s->nb_frames);
for (i = 0; i < s->nb_components; i++) for (i = 0; i < s->nb_components; i++) {
fprintf(s->stats_file, "%c:%f ", s->comps[i], c[i]); int cidx = s->is_rgb ? s->rgba_map[i] : i;
fprintf(s->stats_file, "%c:%f ", s->comps[i], c[cidx]);
}
fprintf(s->stats_file, "All:%f (%f)\n", ssimv, ssim_db(c[0] * s->coefs[0] + c[1] * s->coefs[1] + c[2] * s->coefs[2], s->coefs[3])); fprintf(s->stats_file, "All:%f (%f)\n", ssimv, ssim_db(ssimv, 1.0));
} }
s->ssim[0] += c[0];
s->ssim[1] += c[1];
s->ssim[2] += c[2];
return main; return main;
} }
...@@ -265,7 +264,7 @@ static int config_input_ref(AVFilterLink *inlink) ...@@ -265,7 +264,7 @@ static int config_input_ref(AVFilterLink *inlink)
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
AVFilterContext *ctx = inlink->dst; AVFilterContext *ctx = inlink->dst;
SSIMContext *s = ctx->priv; SSIMContext *s = ctx->priv;
int is_rgb; int sum = 0, i;
s->nb_components = desc->nb_components; s->nb_components = desc->nb_components;
...@@ -279,24 +278,20 @@ static int config_input_ref(AVFilterLink *inlink) ...@@ -279,24 +278,20 @@ static int config_input_ref(AVFilterLink *inlink)
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0; s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
s->comps[0] = is_rgb ? 'R' : 'Y'; s->comps[0] = s->is_rgb ? 'R' : 'Y';
s->comps[1] = is_rgb ? 'G' : 'U'; s->comps[1] = s->is_rgb ? 'G' : 'U';
s->comps[2] = is_rgb ? 'B' : 'V'; s->comps[2] = s->is_rgb ? 'B' : 'V';
s->comps[3] = 'A'; s->comps[3] = 'A';
if (is_rgb) {
s->coefs = rgb_coefs;
} else if (s->nb_components == 1) {
s->coefs = gray_coefs;
} else {
s->coefs = yuv_coefs;
}
s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
s->planeheight[0] = s->planeheight[3] = inlink->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[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
s->planewidth[0] = s->planewidth[3] = inlink->w; s->planewidth[0] = s->planewidth[3] = inlink->w;
for (i = 0; i < s->nb_components; i++)
sum += s->planeheight[i] * s->planewidth[i];
for (i = 0; i < s->nb_components; i++)
s->coefs[i] = (double) s->planeheight[i] * s->planewidth[i] / sum;
s->temp = av_malloc((2 * inlink->w + 12) * sizeof(*s->temp)); s->temp = av_malloc((2 * inlink->w + 12) * sizeof(*s->temp));
if (!s->temp) if (!s->temp)
...@@ -341,17 +336,15 @@ static av_cold void uninit(AVFilterContext *ctx) ...@@ -341,17 +336,15 @@ static av_cold void uninit(AVFilterContext *ctx)
SSIMContext *s = ctx->priv; SSIMContext *s = ctx->priv;
if (s->nb_frames > 0) { if (s->nb_frames > 0) {
if (s->nb_components == 3) { char buf[256];
av_log(ctx, AV_LOG_INFO, "SSIM %c:%f %c:%f %c:%f All:%f (%f)\n", int i;
s->comps[0], s->ssim[0] / s->nb_frames, buf[0] = 0;
s->comps[1], s->ssim[1] / s->nb_frames, for (i = 0; i < s->nb_components; i++) {
s->comps[2], s->ssim[2] / s->nb_frames, int c = s->is_rgb ? s->rgba_map[i] : i;
(s->ssim[0] * 4 + s->ssim[1] + s->ssim[2]) / (s->nb_frames * 6), av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[i], s->ssim[c] / s->nb_frames);
ssim_db(s->ssim[0] * 4 + s->ssim[1] + s->ssim[2], s->nb_frames * 6));
} else if (s->nb_components == 1) {
av_log(ctx, AV_LOG_INFO, "SSIM All:%f (%f)\n",
s->ssim[0] / s->nb_frames, ssim_db(s->ssim[0], s->nb_frames));
} }
av_log(ctx, AV_LOG_INFO, "SSIM%s All:%f (%f)\n", buf,
s->ssim_total / s->nb_frames, ssim_db(s->ssim_total, s->nb_frames));
} }
ff_dualinput_uninit(&s->dinput); ff_dualinput_uninit(&s->dinput);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment