mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-17 06:19:54 +02:00
speed-up SSIM calculation
SSIM results are incompatible with previous version! We're now averaging the SSIM value for each pixels instead of printing a frame-level global SSIM value. * Got rid of some old code * switched to uint32_t for accumulation * refactoring SSIM calculation is ~4x faster now. Change-Id: I48d838e66aef5199b9b5cd5cddef6a98411f5673
This commit is contained in:
105
src/enc/filter.c
105
src/enc/filter.c
@ -105,115 +105,28 @@ static void DoFilter(const VP8EncIterator* const it, int level) {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// SSIM metric
|
||||
|
||||
static const double kMinValue = 1.e-10; // minimal threshold
|
||||
|
||||
void VP8SSIMAddStats(const VP8DistoStats* const src, VP8DistoStats* const dst) {
|
||||
dst->w += src->w;
|
||||
dst->xm += src->xm;
|
||||
dst->ym += src->ym;
|
||||
dst->xxm += src->xxm;
|
||||
dst->xym += src->xym;
|
||||
dst->yym += src->yym;
|
||||
}
|
||||
|
||||
double VP8SSIMGet(const VP8DistoStats* const stats) {
|
||||
const double xmxm = stats->xm * stats->xm;
|
||||
const double ymym = stats->ym * stats->ym;
|
||||
const double xmym = stats->xm * stats->ym;
|
||||
const double w2 = stats->w * stats->w;
|
||||
double sxx = stats->xxm * stats->w - xmxm;
|
||||
double syy = stats->yym * stats->w - ymym;
|
||||
double sxy = stats->xym * stats->w - xmym;
|
||||
double C1, C2;
|
||||
double fnum;
|
||||
double fden;
|
||||
// small errors are possible, due to rounding. Clamp to zero.
|
||||
if (sxx < 0.) sxx = 0.;
|
||||
if (syy < 0.) syy = 0.;
|
||||
C1 = 6.5025 * w2;
|
||||
C2 = 58.5225 * w2;
|
||||
fnum = (2 * xmym + C1) * (2 * sxy + C2);
|
||||
fden = (xmxm + ymym + C1) * (sxx + syy + C2);
|
||||
return (fden != 0.) ? fnum / fden : kMinValue;
|
||||
}
|
||||
|
||||
double VP8SSIMGetSquaredError(const VP8DistoStats* const s) {
|
||||
if (s->w > 0.) {
|
||||
const double iw2 = 1. / (s->w * s->w);
|
||||
const double sxx = s->xxm * s->w - s->xm * s->xm;
|
||||
const double syy = s->yym * s->w - s->ym * s->ym;
|
||||
const double sxy = s->xym * s->w - s->xm * s->ym;
|
||||
const double SSE = iw2 * (sxx + syy - 2. * sxy);
|
||||
if (SSE > kMinValue) return SSE;
|
||||
}
|
||||
return kMinValue;
|
||||
}
|
||||
|
||||
#define LIMIT(A, M) ((A) > (M) ? (M) : (A))
|
||||
static void VP8SSIMAccumulateRow(const uint8_t* src1, int stride1,
|
||||
const uint8_t* src2, int stride2,
|
||||
int y, int W, int H,
|
||||
VP8DistoStats* const stats) {
|
||||
int x = 0;
|
||||
const int w0 = LIMIT(VP8_SSIM_KERNEL, W);
|
||||
for (x = 0; x < w0; ++x) {
|
||||
VP8SSIMAccumulateClipped(src1, stride1, src2, stride2, x, y, W, H, stats);
|
||||
}
|
||||
for (; x <= W - 8 + VP8_SSIM_KERNEL; ++x) {
|
||||
VP8SSIMAccumulate(
|
||||
src1 + (y - VP8_SSIM_KERNEL) * stride1 + (x - VP8_SSIM_KERNEL), stride1,
|
||||
src2 + (y - VP8_SSIM_KERNEL) * stride2 + (x - VP8_SSIM_KERNEL), stride2,
|
||||
stats);
|
||||
}
|
||||
for (; x < W; ++x) {
|
||||
VP8SSIMAccumulateClipped(src1, stride1, src2, stride2, x, y, W, H, stats);
|
||||
}
|
||||
}
|
||||
|
||||
void VP8SSIMAccumulatePlane(const uint8_t* src1, int stride1,
|
||||
const uint8_t* src2, int stride2,
|
||||
int W, int H, VP8DistoStats* const stats) {
|
||||
int x, y;
|
||||
const int h0 = LIMIT(VP8_SSIM_KERNEL, H);
|
||||
const int h1 = LIMIT(VP8_SSIM_KERNEL, H - VP8_SSIM_KERNEL);
|
||||
for (y = 0; y < h0; ++y) {
|
||||
for (x = 0; x < W; ++x) {
|
||||
VP8SSIMAccumulateClipped(src1, stride1, src2, stride2, x, y, W, H, stats);
|
||||
}
|
||||
}
|
||||
for (; y < h1; ++y) {
|
||||
VP8SSIMAccumulateRow(src1, stride1, src2, stride2, y, W, H, stats);
|
||||
}
|
||||
for (; y < H; ++y) {
|
||||
for (x = 0; x < W; ++x) {
|
||||
VP8SSIMAccumulateClipped(src1, stride1, src2, stride2, x, y, W, H, stats);
|
||||
}
|
||||
}
|
||||
}
|
||||
#undef LIMIT
|
||||
// SSIM metric for one macroblock
|
||||
|
||||
static double GetMBSSIM(const uint8_t* yuv1, const uint8_t* yuv2) {
|
||||
int x, y;
|
||||
VP8DistoStats s = { .0, .0, .0, .0, .0, .0 };
|
||||
double sum = 0.;
|
||||
|
||||
// compute SSIM in a 10 x 10 window
|
||||
for (y = VP8_SSIM_KERNEL; y < 16 - VP8_SSIM_KERNEL; y++) {
|
||||
for (x = VP8_SSIM_KERNEL; x < 16 - VP8_SSIM_KERNEL; x++) {
|
||||
VP8SSIMAccumulateClipped(yuv1 + Y_OFF_ENC, BPS, yuv2 + Y_OFF_ENC, BPS,
|
||||
x, y, 16, 16, &s);
|
||||
sum += VP8SSIMGetClipped(yuv1 + Y_OFF_ENC, BPS, yuv2 + Y_OFF_ENC, BPS,
|
||||
x, y, 16, 16);
|
||||
}
|
||||
}
|
||||
for (x = 1; x < 7; x++) {
|
||||
for (y = 1; y < 7; y++) {
|
||||
VP8SSIMAccumulateClipped(yuv1 + U_OFF_ENC, BPS, yuv2 + U_OFF_ENC, BPS,
|
||||
x, y, 8, 8, &s);
|
||||
VP8SSIMAccumulateClipped(yuv1 + V_OFF_ENC, BPS, yuv2 + V_OFF_ENC, BPS,
|
||||
x, y, 8, 8, &s);
|
||||
sum += VP8SSIMGetClipped(yuv1 + U_OFF_ENC, BPS, yuv2 + U_OFF_ENC, BPS,
|
||||
x, y, 8, 8);
|
||||
sum += VP8SSIMGetClipped(yuv1 + V_OFF_ENC, BPS, yuv2 + V_OFF_ENC, BPS,
|
||||
x, y, 8, 8);
|
||||
}
|
||||
}
|
||||
return VP8SSIMGet(&s);
|
||||
return sum;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -17,6 +17,10 @@
|
||||
#include "./vp8enci.h"
|
||||
#include "../utils/utils.h"
|
||||
|
||||
typedef double (*AccumulateFunc)(const uint8_t* src, int src_stride,
|
||||
const uint8_t* ref, int ref_stride,
|
||||
int w, int h);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// local-min distortion
|
||||
//
|
||||
@ -67,6 +71,43 @@ static double AccumulateSSE(const uint8_t* src, int src_stride,
|
||||
return total_sse;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
static double AccumulateSSIM(const uint8_t* src, int src_stride,
|
||||
const uint8_t* ref, int ref_stride,
|
||||
int w, int h) {
|
||||
const int w0 = (w < VP8_SSIM_KERNEL) ? w : VP8_SSIM_KERNEL;
|
||||
const int w1 = w - VP8_SSIM_KERNEL - 1;
|
||||
const int h0 = (h < VP8_SSIM_KERNEL) ? h : VP8_SSIM_KERNEL;
|
||||
const int h1 = h - VP8_SSIM_KERNEL - 1;
|
||||
int x, y;
|
||||
double sum = 0.;
|
||||
for (y = 0; y < h0; ++y) {
|
||||
for (x = 0; x < w; ++x) {
|
||||
sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
|
||||
}
|
||||
}
|
||||
for (; y < h1; ++y) {
|
||||
for (x = 0; x < w0; ++x) {
|
||||
sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
|
||||
}
|
||||
for (; x < w1; ++x) {
|
||||
const int off1 = x - VP8_SSIM_KERNEL + (y - VP8_SSIM_KERNEL) * src_stride;
|
||||
const int off2 = x - VP8_SSIM_KERNEL + (y - VP8_SSIM_KERNEL) * ref_stride;
|
||||
sum += VP8SSIMGet(src + off1, src_stride, ref + off2, ref_stride);
|
||||
}
|
||||
for (; x < w; ++x) {
|
||||
sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
|
||||
}
|
||||
}
|
||||
for (; y < h; ++y) {
|
||||
for (x = 0; x < w; ++x) {
|
||||
sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Distortion
|
||||
|
||||
@ -77,6 +118,7 @@ static double GetPSNR(double v, double size) {
|
||||
return (v > 0. && size > 0.) ? -4.3429448 * log(v / (size * 255 * 255.))
|
||||
: kMinDistortion_dB;
|
||||
}
|
||||
|
||||
static double GetLogSSIM(double v, double size) {
|
||||
v = (size > 0.) ? v / size : 1.;
|
||||
return (v < 1.) ? -10.0 * log10(1. - v) : kMinDistortion_dB;
|
||||
@ -88,10 +130,10 @@ int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
|
||||
double disto[4] = { 0. };
|
||||
double sizes[4] = { 0. };
|
||||
double total_size = 0., total_disto = 0.;
|
||||
VP8DistoStats stats[5];
|
||||
|
||||
const AccumulateFunc metric = (type == 0) ? AccumulateSSE :
|
||||
(type == 1) ? AccumulateSSIM :
|
||||
AccumulateLSIM;
|
||||
VP8SSIMDspInit();
|
||||
memset(stats, 0, sizeof(stats));
|
||||
|
||||
if (src == NULL || ref == NULL ||
|
||||
src->width != ref->width || src->height != ref->height ||
|
||||
@ -120,13 +162,7 @@ int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
|
||||
}
|
||||
}
|
||||
sizes[c] = w * h;
|
||||
if (type >= 2) {
|
||||
disto[c] = AccumulateLSIM(tmp1, w, tmp2, w, w, h);
|
||||
} else if (type == 0) {
|
||||
disto[c] = AccumulateSSE(tmp1, w, tmp2, w, w, h);
|
||||
} else {
|
||||
VP8SSIMAccumulatePlane(tmp1, w, tmp2, w, w, h, &stats[c]);
|
||||
}
|
||||
disto[c] = metric(tmp1, w, tmp2, w, w, h);
|
||||
}
|
||||
WebPSafeFree(tmp_plane);
|
||||
}
|
||||
@ -149,62 +185,24 @@ int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
|
||||
sizes[1] = sizes[2] = uv_w * uv_h;
|
||||
sizes[3] = has_alpha ? w * h : 0.;
|
||||
|
||||
if (type >= 2) {
|
||||
disto[0] = AccumulateLSIM(src->y, src->y_stride, ref->y, ref->y_stride,
|
||||
w, h);
|
||||
disto[1] = AccumulateLSIM(src->u, src->uv_stride, ref->u, ref->uv_stride,
|
||||
uv_w, uv_h);
|
||||
disto[2] = AccumulateLSIM(src->v, src->uv_stride, ref->v, ref->uv_stride,
|
||||
uv_w, uv_h);
|
||||
if (has_alpha) {
|
||||
disto[3] = AccumulateLSIM(src->a, src->a_stride, ref->a, ref->a_stride,
|
||||
w, h);
|
||||
}
|
||||
} else if (type == 0) {
|
||||
disto[0] = AccumulateSSE(src->y, src->y_stride, ref->y, ref->y_stride,
|
||||
w, h);
|
||||
disto[1] = AccumulateSSE(src->u, src->uv_stride, ref->u, ref->uv_stride,
|
||||
uv_w, uv_h);
|
||||
disto[2] = AccumulateSSE(src->v, src->uv_stride, ref->v, ref->uv_stride,
|
||||
uv_w, uv_h);
|
||||
if (has_alpha) {
|
||||
disto[3] = AccumulateSSE(src->a, src->a_stride, ref->a, ref->a_stride,
|
||||
w, h);
|
||||
}
|
||||
} else {
|
||||
VP8SSIMAccumulatePlane(src->y, src->y_stride,
|
||||
ref->y, ref->y_stride,
|
||||
w, h, &stats[0]);
|
||||
VP8SSIMAccumulatePlane(src->u, src->uv_stride,
|
||||
ref->u, ref->uv_stride,
|
||||
uv_w, uv_h, &stats[1]);
|
||||
VP8SSIMAccumulatePlane(src->v, src->uv_stride,
|
||||
ref->v, ref->uv_stride,
|
||||
uv_w, uv_h, &stats[2]);
|
||||
if (has_alpha) {
|
||||
VP8SSIMAccumulatePlane(src->a, src->a_stride,
|
||||
ref->a, ref->a_stride,
|
||||
w, h, &stats[3]);
|
||||
}
|
||||
disto[0] = metric(src->y, src->y_stride, ref->y, ref->y_stride, w, h);
|
||||
disto[1] = metric(src->u, src->uv_stride, ref->u, ref->uv_stride,
|
||||
uv_w, uv_h);
|
||||
disto[2] = metric(src->v, src->uv_stride, ref->v, ref->uv_stride,
|
||||
uv_w, uv_h);
|
||||
if (has_alpha) {
|
||||
disto[3] = metric(src->a, src->a_stride, ref->a, ref->a_stride, w, h);
|
||||
}
|
||||
}
|
||||
|
||||
for (c = 0; c < 4; ++c) {
|
||||
if (type == 1) {
|
||||
results[c] = (float)GetLogSSIM(VP8SSIMGet(&stats[c]), 1.);
|
||||
VP8SSIMAddStats(&stats[c], &stats[4]);
|
||||
} else {
|
||||
total_disto += disto[c];
|
||||
total_size += sizes[c];
|
||||
results[c] = (float)GetPSNR(disto[c], sizes[c]);
|
||||
}
|
||||
total_disto += disto[c];
|
||||
total_size += sizes[c];
|
||||
results[c] = (type == 1) ? (float)GetLogSSIM(disto[c], sizes[c])
|
||||
: (float)GetPSNR(disto[c], sizes[c]);
|
||||
}
|
||||
if (type == 1) {
|
||||
results[4] = (float)GetLogSSIM(VP8SSIMGet(&stats[4]), 1.);
|
||||
} else {
|
||||
results[4] = (float)GetPSNR(total_disto, total_size);
|
||||
}
|
||||
|
||||
results[4] = (type == 1) ? (float)GetLogSSIM(total_disto, total_size)
|
||||
: (float)GetPSNR(total_disto, total_size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -474,14 +474,6 @@ int VP8EncStartAlpha(VP8Encoder* const enc); // start alpha coding process
|
||||
int VP8EncFinishAlpha(VP8Encoder* const enc); // finalize compressed data
|
||||
int VP8EncDeleteAlpha(VP8Encoder* const enc); // delete compressed data
|
||||
|
||||
// in filter.c
|
||||
void VP8SSIMAddStats(const VP8DistoStats* const src, VP8DistoStats* const dst);
|
||||
void VP8SSIMAccumulatePlane(const uint8_t* src1, int stride1,
|
||||
const uint8_t* src2, int stride2,
|
||||
int W, int H, VP8DistoStats* const stats);
|
||||
double VP8SSIMGet(const VP8DistoStats* const stats);
|
||||
double VP8SSIMGetSquaredError(const VP8DistoStats* const stats);
|
||||
|
||||
// autofilter
|
||||
void VP8InitFilter(VP8EncIterator* const it);
|
||||
void VP8StoreFilterStats(VP8EncIterator* const it);
|
||||
|
Reference in New Issue
Block a user