From 8dfc4c6f1740c442bbe332ef1a7a078cb673bf85 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Wed, 4 May 2011 16:26:47 -0700 Subject: [PATCH] factorize and unify GetAlpha() between the C and SSE2 version patch by Christian Duvivier (cduvivier at google dot com) Change-Id: I47ac75010aa4036cf09f13d23043e654c4966a00 --- src/enc/dsp.c | 24 +++++++++++++++--------- src/enc/dsp_sse2.c | 16 +--------------- src/enc/vp8enci.h | 2 ++ 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/enc/dsp.c b/src/enc/dsp.c index 27bbc50b..cbdc9388 100644 --- a/src/enc/dsp.c +++ b/src/enc/dsp.c @@ -24,13 +24,14 @@ static int ClipAlpha(int alpha) { return alpha < 0 ? 0 : alpha > 255 ? 255 : alpha; } -static int GetAlpha(const int histo[MAX_COEFF_THRESH]) { +int VP8GetAlpha(const int histo[MAX_COEFF_THRESH + 1]) { int num = 0, den = 0, val = 0; int k; int alpha; + // note: changing this loop to avoid the numerous "k + 1" slows things down. for (k = 0; k < MAX_COEFF_THRESH; ++k) { - if (histo[k]) { - val += histo[k]; + if (histo[k + 1]) { + val += histo[k + 1]; num += val * (k + 1); den += (k + 1) * (k + 1); } @@ -42,20 +43,25 @@ static int GetAlpha(const int histo[MAX_COEFF_THRESH]) { static int CollectHistogram(const uint8_t* ref, const uint8_t* pred, int start_block, int end_block) { - int histo[MAX_COEFF_THRESH] = { 0 }; + int histo[MAX_COEFF_THRESH + 1] = { 0 }; int16_t out[16]; int j, k; for (j = start_block; j < end_block; ++j) { VP8FTransform(ref + VP8Scan[j], pred + VP8Scan[j], out); + + // Convert coefficients to bin (within out[]). for (k = 0; k < 16; ++k) { const int v = abs(out[k]) >> 2; - if (v) { - const int bin = (v > MAX_COEFF_THRESH) ? MAX_COEFF_THRESH : v; - histo[bin - 1]++; - } + out[k] = (v > MAX_COEFF_THRESH) ? MAX_COEFF_THRESH : v; + } + + // Use bin to update histogram. + for (k = 0; k < 16; ++k) { + histo[out[k]]++; } } - return GetAlpha(histo); + + return VP8GetAlpha(histo); } //----------------------------------------------------------------------------- diff --git a/src/enc/dsp_sse2.c b/src/enc/dsp_sse2.c index ac180c04..9a1749cd 100644 --- a/src/enc/dsp_sse2.c +++ b/src/enc/dsp_sse2.c @@ -61,21 +61,7 @@ static int CollectHistogramSSE2(const uint8_t* ref, const uint8_t* pred, } } - { - int num = 0, den = 0, val = 0; - int alpha; - for (k = 0; k < MAX_COEFF_THRESH; ++k) { - if (histo[k + 1]) { - val += histo[k + 1]; - num += val * (k + 1); - den += (k + 1) * (k + 1); - } - } - // we scale the value to a usable [0..255] range - alpha = den ? 10 * num / den - 5 : 0; - alpha = alpha < 0 ? 0 : alpha > 255 ? 255 : alpha; - return alpha; - } + return VP8GetAlpha(histo); } //----------------------------------------------------------------------------- diff --git a/src/enc/vp8enci.h b/src/enc/vp8enci.h index 14667354..0f8d71f4 100644 --- a/src/enc/vp8enci.h +++ b/src/enc/vp8enci.h @@ -446,6 +446,8 @@ int VP8EncFinishLayer(VP8Encoder* const enc); // finalize coding void VP8EncDeleteLayer(VP8Encoder* enc); // reclaim memory // in dsp.c +int VP8GetAlpha(const int histo[MAX_COEFF_THRESH + 1]); + // Transforms // VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms // will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).