From 499fe05dc030b31d15c2ad9717e6bcee839f7d12 Mon Sep 17 00:00:00 2001 From: Yaowu Xu Date: Fri, 22 Feb 2013 16:18:08 -0800 Subject: [PATCH] optimize forward 16x16 DCT for accuracy This commit added pre/post scaling for first half of fDCT16x16 to reduce error, by simulation of 100,000 blocks for random inputs, the average sse reduced from 2.1/block to 0.0498/block. also enabled tests for 16x16 fDCT and iDCT Change-Id: Id2a95f0464c6dd4118797d456237ae90274c0f02 --- test/dct16x16_test.cc | 2 +- test/test.mk | 3 +- vp9/encoder/vp9_dct.c | 440 +----------------------------------------- 3 files changed, 5 insertions(+), 440 deletions(-) diff --git a/test/dct16x16_test.cc b/test/dct16x16_test.cc index 194d5fb74..f6d2d5994 100644 --- a/test/dct16x16_test.cc +++ b/test/dct16x16_test.cc @@ -289,7 +289,7 @@ TEST(VP9Idct16x16Test, AccuracyCheck) { } } } -#if 0 +#if 1 // we need enable fdct test once we re-do the 16 point fdct. TEST(VP9Fdct16x16Test, AccuracyCheck) { ACMRandom rnd(ACMRandom::DeterministicSeed()); diff --git a/test/test.mk b/test/test.mk index 29657625c..d879f9fb2 100644 --- a/test/test.mk +++ b/test/test.mk @@ -72,8 +72,9 @@ endif LIBVPX_TEST_SRCS-$(CONFIG_VP9) += convolve_test.cc LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += fdct4x4_test.cc + LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += fdct8x8_test.cc -#LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += dct16x16_test.cc +LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += dct16x16_test.cc LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += variance_test.cc #LIBVPX_TEST_SRCS-$(CONFIG_VP9_ENCODER) += dct32x32_test.cc diff --git a/vp9/encoder/vp9_dct.c b/vp9/encoder/vp9_dct.c index a459e949b..6894c5fee 100644 --- a/vp9/encoder/vp9_dct.c +++ b/vp9/encoder/vp9_dct.c @@ -918,441 +918,7 @@ void vp9_short_walsh8x4_x8_c(short *input, short *output, int pitch) { vp9_short_walsh4x4_x8_c(input + 4, output + 16, pitch); } -#define TEST_INT_16x16_DCT 1 -#if !TEST_INT_16x16_DCT -static void dct16x16_1d(double input[16], double output[16]) { - static const double C1 = 0.995184726672197; - static const double C2 = 0.98078528040323; - static const double C3 = 0.956940335732209; - static const double C4 = 0.923879532511287; - static const double C5 = 0.881921264348355; - static const double C6 = 0.831469612302545; - static const double C7 = 0.773010453362737; - static const double C8 = 0.707106781186548; - static const double C9 = 0.634393284163646; - static const double C10 = 0.555570233019602; - static const double C11 = 0.471396736825998; - static const double C12 = 0.38268343236509; - static const double C13 = 0.290284677254462; - static const double C14 = 0.195090322016128; - static const double C15 = 0.098017140329561; - - vp9_clear_system_state(); // Make it simd safe : __asm emms; - { - double step[16]; - double intermediate[16]; - double temp1, temp2; - - // step 1 - step[ 0] = input[0] + input[15]; - step[ 1] = input[1] + input[14]; - step[ 2] = input[2] + input[13]; - step[ 3] = input[3] + input[12]; - step[ 4] = input[4] + input[11]; - step[ 5] = input[5] + input[10]; - step[ 6] = input[6] + input[ 9]; - step[ 7] = input[7] + input[ 8]; - step[ 8] = input[7] - input[ 8]; - step[ 9] = input[6] - input[ 9]; - step[10] = input[5] - input[10]; - step[11] = input[4] - input[11]; - step[12] = input[3] - input[12]; - step[13] = input[2] - input[13]; - step[14] = input[1] - input[14]; - step[15] = input[0] - input[15]; - - // step 2 - output[0] = step[0] + step[7]; - output[1] = step[1] + step[6]; - output[2] = step[2] + step[5]; - output[3] = step[3] + step[4]; - output[4] = step[3] - step[4]; - output[5] = step[2] - step[5]; - output[6] = step[1] - step[6]; - output[7] = step[0] - step[7]; - - temp1 = step[ 8]*C7; - temp2 = step[15]*C9; - output[ 8] = temp1 + temp2; - - temp1 = step[ 9]*C11; - temp2 = step[14]*C5; - output[ 9] = temp1 - temp2; - - temp1 = step[10]*C3; - temp2 = step[13]*C13; - output[10] = temp1 + temp2; - - temp1 = step[11]*C15; - temp2 = step[12]*C1; - output[11] = temp1 - temp2; - - temp1 = step[11]*C1; - temp2 = step[12]*C15; - output[12] = temp2 + temp1; - - temp1 = step[10]*C13; - temp2 = step[13]*C3; - output[13] = temp2 - temp1; - - temp1 = step[ 9]*C5; - temp2 = step[14]*C11; - output[14] = temp2 + temp1; - - temp1 = step[ 8]*C9; - temp2 = step[15]*C7; - output[15] = temp2 - temp1; - - // step 3 - step[ 0] = output[0] + output[3]; - step[ 1] = output[1] + output[2]; - step[ 2] = output[1] - output[2]; - step[ 3] = output[0] - output[3]; - - temp1 = output[4]*C14; - temp2 = output[7]*C2; - step[ 4] = temp1 + temp2; - - temp1 = output[5]*C10; - temp2 = output[6]*C6; - step[ 5] = temp1 + temp2; - - temp1 = output[5]*C6; - temp2 = output[6]*C10; - step[ 6] = temp2 - temp1; - - temp1 = output[4]*C2; - temp2 = output[7]*C14; - step[ 7] = temp2 - temp1; - - step[ 8] = output[ 8] + output[11]; - step[ 9] = output[ 9] + output[10]; - step[10] = output[ 9] - output[10]; - step[11] = output[ 8] - output[11]; - - step[12] = output[12] + output[15]; - step[13] = output[13] + output[14]; - step[14] = output[13] - output[14]; - step[15] = output[12] - output[15]; - - // step 4 - output[ 0] = (step[ 0] + step[ 1]); - output[ 8] = (step[ 0] - step[ 1]); - - temp1 = step[2]*C12; - temp2 = step[3]*C4; - temp1 = temp1 + temp2; - output[ 4] = 2*(temp1*C8); - - temp1 = step[2]*C4; - temp2 = step[3]*C12; - temp1 = temp2 - temp1; - output[12] = 2*(temp1*C8); - - output[ 2] = 2*((step[4] + step[ 5])*C8); - output[14] = 2*((step[7] - step[ 6])*C8); - - temp1 = step[4] - step[5]; - temp2 = step[6] + step[7]; - output[ 6] = (temp1 + temp2); - output[10] = (temp1 - temp2); - - intermediate[8] = step[8] + step[14]; - intermediate[9] = step[9] + step[15]; - - temp1 = intermediate[8]*C12; - temp2 = intermediate[9]*C4; - temp1 = temp1 - temp2; - output[3] = 2*(temp1*C8); - - temp1 = intermediate[8]*C4; - temp2 = intermediate[9]*C12; - temp1 = temp2 + temp1; - output[13] = 2*(temp1*C8); - - output[ 9] = 2*((step[10] + step[11])*C8); - - intermediate[11] = step[10] - step[11]; - intermediate[12] = step[12] + step[13]; - intermediate[13] = step[12] - step[13]; - intermediate[14] = step[ 8] - step[14]; - intermediate[15] = step[ 9] - step[15]; - - output[15] = (intermediate[11] + intermediate[12]); - output[ 1] = -(intermediate[11] - intermediate[12]); - - output[ 7] = 2*(intermediate[13]*C8); - - temp1 = intermediate[14]*C12; - temp2 = intermediate[15]*C4; - temp1 = temp1 - temp2; - output[11] = -2*(temp1*C8); - - temp1 = intermediate[14]*C4; - temp2 = intermediate[15]*C12; - temp1 = temp2 + temp1; - output[ 5] = 2*(temp1*C8); - } - vp9_clear_system_state(); // Make it simd safe : __asm emms; -} - -void vp9_short_fdct16x16_c(short *input, short *out, int pitch) { - vp9_clear_system_state(); // Make it simd safe : __asm emms; - { - int shortpitch = pitch >> 1; - int i, j; - double output[256]; - // First transform columns - for (i = 0; i < 16; i++) { - double temp_in[16], temp_out[16]; - for (j = 0; j < 16; j++) - temp_in[j] = input[j*shortpitch + i]; - dct16x16_1d(temp_in, temp_out); - for (j = 0; j < 16; j++) - output[j*16 + i] = temp_out[j]; - } - // Then transform rows - for (i = 0; i < 16; ++i) { - double temp_in[16], temp_out[16]; - for (j = 0; j < 16; ++j) - temp_in[j] = output[j + i*16]; - dct16x16_1d(temp_in, temp_out); - for (j = 0; j < 16; ++j) - output[j + i*16] = temp_out[j]; - } - // Scale by some magic number - for (i = 0; i < 256; i++) - out[i] = (short)round(output[i]/2); - } - vp9_clear_system_state(); // Make it simd safe : __asm emms; -} - -#else - -#define NEW_FDCT16 1 -#if !NEW_FDCT16 -static const int16_t C1 = 16305; -static const int16_t C2 = 16069; -static const int16_t C3 = 15679; -static const int16_t C4 = 15137; -static const int16_t C5 = 14449; -static const int16_t C6 = 13623; -static const int16_t C7 = 12665; -static const int16_t C8 = 11585; -static const int16_t C9 = 10394; -static const int16_t C10 = 9102; -static const int16_t C11 = 7723; -static const int16_t C12 = 6270; -static const int16_t C13 = 4756; -static const int16_t C14 = 3196; -static const int16_t C15 = 1606; - -#define RIGHT_SHIFT 14 -#define ROUNDING (1 << (RIGHT_SHIFT - 1)) - -static void dct16x16_1d(int16_t input[16], int16_t output[16], - int last_shift_bits) { - int16_t step[16]; - int intermediate[16]; - int temp1, temp2; - int final_shift = RIGHT_SHIFT; - int final_rounding = ROUNDING; - int output_shift = 0; - int output_rounding = 0; - - final_shift += last_shift_bits; - if (final_shift > 0) - final_rounding = 1 << (final_shift - 1); - - output_shift += last_shift_bits; - if (output_shift > 0) - output_rounding = 1 << (output_shift - 1); - - // step 1 - step[ 0] = input[0] + input[15]; - step[ 1] = input[1] + input[14]; - step[ 2] = input[2] + input[13]; - step[ 3] = input[3] + input[12]; - step[ 4] = input[4] + input[11]; - step[ 5] = input[5] + input[10]; - step[ 6] = input[6] + input[ 9]; - step[ 7] = input[7] + input[ 8]; - step[ 8] = input[7] - input[ 8]; - step[ 9] = input[6] - input[ 9]; - step[10] = input[5] - input[10]; - step[11] = input[4] - input[11]; - step[12] = input[3] - input[12]; - step[13] = input[2] - input[13]; - step[14] = input[1] - input[14]; - step[15] = input[0] - input[15]; - - // step 2 - output[0] = step[0] + step[7]; - output[1] = step[1] + step[6]; - output[2] = step[2] + step[5]; - output[3] = step[3] + step[4]; - output[4] = step[3] - step[4]; - output[5] = step[2] - step[5]; - output[6] = step[1] - step[6]; - output[7] = step[0] - step[7]; - - temp1 = step[ 8] * C7; - temp2 = step[15] * C9; - output[ 8] = (temp1 + temp2 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = step[ 9] * C11; - temp2 = step[14] * C5; - output[ 9] = (temp1 - temp2 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = step[10] * C3; - temp2 = step[13] * C13; - output[10] = (temp1 + temp2 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = step[11] * C15; - temp2 = step[12] * C1; - output[11] = (temp1 - temp2 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = step[11] * C1; - temp2 = step[12] * C15; - output[12] = (temp2 + temp1 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = step[10] * C13; - temp2 = step[13] * C3; - output[13] = (temp2 - temp1 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = step[ 9] * C5; - temp2 = step[14] * C11; - output[14] = (temp2 + temp1 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = step[ 8] * C9; - temp2 = step[15] * C7; - output[15] = (temp2 - temp1 + ROUNDING) >> RIGHT_SHIFT; - - // step 3 - step[ 0] = output[0] + output[3]; - step[ 1] = output[1] + output[2]; - step[ 2] = output[1] - output[2]; - step[ 3] = output[0] - output[3]; - - temp1 = output[4] * C14; - temp2 = output[7] * C2; - step[ 4] = (temp1 + temp2 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = output[5] * C10; - temp2 = output[6] * C6; - step[ 5] = (temp1 + temp2 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = output[5] * C6; - temp2 = output[6] * C10; - step[ 6] = (temp2 - temp1 + ROUNDING) >> RIGHT_SHIFT; - - temp1 = output[4] * C2; - temp2 = output[7] * C14; - step[ 7] = (temp2 - temp1 + ROUNDING) >> RIGHT_SHIFT; - - step[ 8] = output[ 8] + output[11]; - step[ 9] = output[ 9] + output[10]; - step[10] = output[ 9] - output[10]; - step[11] = output[ 8] - output[11]; - - step[12] = output[12] + output[15]; - step[13] = output[13] + output[14]; - step[14] = output[13] - output[14]; - step[15] = output[12] - output[15]; - - // step 4 - output[ 0] = (step[ 0] + step[ 1] + output_rounding) >> output_shift; - output[ 8] = (step[ 0] - step[ 1] + output_rounding) >> output_shift; - - temp1 = step[2] * C12; - temp2 = step[3] * C4; - temp1 = (temp1 + temp2 + final_rounding) >> final_shift; - output[ 4] = (2 * (temp1 * C8) + ROUNDING) >> RIGHT_SHIFT; - - temp1 = step[2] * C4; - temp2 = step[3] * C12; - temp1 = (temp2 - temp1 + final_rounding) >> final_shift; - output[12] = (2 * (temp1 * C8) + ROUNDING) >> RIGHT_SHIFT; - - output[ 2] = (2 * ((step[4] + step[ 5]) * C8) + final_rounding) - >> final_shift; - output[14] = (2 * ((step[7] - step[ 6]) * C8) + final_rounding) - >> final_shift; - - temp1 = step[4] - step[5]; - temp2 = step[6] + step[7]; - output[ 6] = (temp1 + temp2 + output_rounding) >> output_shift; - output[10] = (temp1 - temp2 + output_rounding) >> output_shift; - - intermediate[8] = step[8] + step[14]; - intermediate[9] = step[9] + step[15]; - - temp1 = intermediate[8] * C12; - temp2 = intermediate[9] * C4; - temp1 = (temp1 - temp2 + final_rounding) >> final_shift; - output[3] = (2 * (temp1 * C8) + ROUNDING) >> RIGHT_SHIFT; - - temp1 = intermediate[8] * C4; - temp2 = intermediate[9] * C12; - temp1 = (temp2 + temp1 + final_rounding) >> final_shift; - output[13] = (2 * (temp1 * C8) + ROUNDING) >> RIGHT_SHIFT; - - output[ 9] = (2 * ((step[10] + step[11]) * C8) + final_rounding) - >> final_shift; - - intermediate[11] = step[10] - step[11]; - intermediate[12] = step[12] + step[13]; - intermediate[13] = step[12] - step[13]; - intermediate[14] = step[ 8] - step[14]; - intermediate[15] = step[ 9] - step[15]; - - output[15] = (intermediate[11] + intermediate[12] + output_rounding) - >> output_shift; - output[ 1] = (intermediate[12] - intermediate[11] + output_rounding) - >> output_shift; - - output[ 7] = (2 * (intermediate[13] * C8) + final_rounding) >> final_shift; - - temp1 = intermediate[14] * C12; - temp2 = intermediate[15] * C4; - temp1 = (temp1 - temp2 + final_rounding) >> final_shift; - output[11] = (-2 * (temp1 * C8) + ROUNDING) >> RIGHT_SHIFT; - - temp1 = intermediate[14] * C4; - temp2 = intermediate[15] * C12; - temp1 = (temp2 + temp1 + final_rounding) >> final_shift; - output[ 5] = (2 * (temp1 * C8) + ROUNDING) >> RIGHT_SHIFT; -} - -void vp9_short_fdct16x16_c(int16_t *input, int16_t *out, int pitch) { - int shortpitch = pitch >> 1; - int i, j; - int16_t output[256]; - int16_t *outptr = &output[0]; - - // First transform columns - for (i = 0; i < 16; i++) { - int16_t temp_in[16]; - int16_t temp_out[16]; - for (j = 0; j < 16; j++) - temp_in[j] = input[j * shortpitch + i]; - dct16x16_1d(temp_in, temp_out, 0); - for (j = 0; j < 16; j++) - output[j * 16 + i] = temp_out[j]; - } - - // Then transform rows - for (i = 0; i < 16; ++i) { - dct16x16_1d(outptr, out, 1); - outptr += 16; - out += 16; - } -} -#undef RIGHT_SHIFT -#undef ROUNDING - -#else // Rewrote to use same algorithm as others. static void fdct16_1d(int16_t input[16], int16_t output[16]) { int16_t step[16]; @@ -1466,10 +1032,10 @@ void vp9_short_fdct16x16_c(int16_t *input, int16_t *out, int pitch) { // First transform columns for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) - temp_in[j] = input[j * shortpitch + i]; + temp_in[j] = input[j * shortpitch + i] << 2; fdct16_1d(temp_in, temp_out); for (j = 0; j < 16; j++) - output[j * 16 + i] = temp_out[j]; + output[j * 16 + i] = (temp_out[j] + 1) >> 2; } // Then transform rows @@ -1481,8 +1047,6 @@ void vp9_short_fdct16x16_c(int16_t *input, int16_t *out, int pitch) { out[j + i * 16] = temp_out[j]; } } -#endif -#endif #if CONFIG_INTHT16X16 void fadst16_1d(int16_t *input, int16_t *output) {