OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include <math.h> |
| 12 #include <stdlib.h> |
| 13 #include <string.h> |
| 14 |
| 15 #include "third_party/googletest/src/include/gtest/gtest.h" |
| 16 |
| 17 extern "C" { |
| 18 #include "vp9_rtcd.h" |
| 19 } |
| 20 |
| 21 #include "acm_random.h" |
| 22 #include "vpx/vpx_integer.h" |
| 23 |
| 24 using libvpx_test::ACMRandom; |
| 25 |
| 26 namespace { |
| 27 |
| 28 #ifdef _MSC_VER |
| 29 static int round(double x) { |
| 30 if(x < 0) |
| 31 return (int)ceil(x - 0.5); |
| 32 else |
| 33 return (int)floor(x + 0.5); |
| 34 } |
| 35 #endif |
| 36 |
| 37 void reference_dct_1d(double input[8], double output[8]) { |
| 38 const double kPi = 3.141592653589793238462643383279502884; |
| 39 const double kInvSqrt2 = 0.707106781186547524400844362104; |
| 40 for (int k = 0; k < 8; k++) { |
| 41 output[k] = 0.0; |
| 42 for (int n = 0; n < 8; n++) |
| 43 output[k] += input[n]*cos(kPi*(2*n+1)*k/16.0); |
| 44 if (k == 0) |
| 45 output[k] = output[k]*kInvSqrt2; |
| 46 } |
| 47 } |
| 48 |
| 49 void reference_dct_2d(int16_t input[64], double output[64]) { |
| 50 // First transform columns |
| 51 for (int i = 0; i < 8; ++i) { |
| 52 double temp_in[8], temp_out[8]; |
| 53 for (int j = 0; j < 8; ++j) |
| 54 temp_in[j] = input[j*8 + i]; |
| 55 reference_dct_1d(temp_in, temp_out); |
| 56 for (int j = 0; j < 8; ++j) |
| 57 output[j*8 + i] = temp_out[j]; |
| 58 } |
| 59 // Then transform rows |
| 60 for (int i = 0; i < 8; ++i) { |
| 61 double temp_in[8], temp_out[8]; |
| 62 for (int j = 0; j < 8; ++j) |
| 63 temp_in[j] = output[j + i*8]; |
| 64 reference_dct_1d(temp_in, temp_out); |
| 65 for (int j = 0; j < 8; ++j) |
| 66 output[j + i*8] = temp_out[j]; |
| 67 } |
| 68 // Scale by some magic number |
| 69 for (int i = 0; i < 64; ++i) |
| 70 output[i] *= 2; |
| 71 } |
| 72 |
| 73 void reference_idct_1d(double input[8], double output[8]) { |
| 74 const double kPi = 3.141592653589793238462643383279502884; |
| 75 const double kSqrt2 = 1.414213562373095048801688724209698; |
| 76 for (int k = 0; k < 8; k++) { |
| 77 output[k] = 0.0; |
| 78 for (int n = 0; n < 8; n++) { |
| 79 output[k] += input[n]*cos(kPi*(2*k+1)*n/16.0); |
| 80 if (n == 0) |
| 81 output[k] = output[k]/kSqrt2; |
| 82 } |
| 83 } |
| 84 } |
| 85 |
| 86 void reference_idct_2d(double input[64], int16_t output[64]) { |
| 87 double out[64], out2[64]; |
| 88 // First transform rows |
| 89 for (int i = 0; i < 8; ++i) { |
| 90 double temp_in[8], temp_out[8]; |
| 91 for (int j = 0; j < 8; ++j) |
| 92 temp_in[j] = input[j + i*8]; |
| 93 reference_idct_1d(temp_in, temp_out); |
| 94 for (int j = 0; j < 8; ++j) |
| 95 out[j + i*8] = temp_out[j]; |
| 96 } |
| 97 // Then transform columns |
| 98 for (int i = 0; i < 8; ++i) { |
| 99 double temp_in[8], temp_out[8]; |
| 100 for (int j = 0; j < 8; ++j) |
| 101 temp_in[j] = out[j*8 + i]; |
| 102 reference_idct_1d(temp_in, temp_out); |
| 103 for (int j = 0; j < 8; ++j) |
| 104 out2[j*8 + i] = temp_out[j]; |
| 105 } |
| 106 for (int i = 0; i < 64; ++i) |
| 107 output[i] = round(out2[i]/32); |
| 108 } |
| 109 |
| 110 TEST(VP9Idct8x8Test, AccuracyCheck) { |
| 111 ACMRandom rnd(ACMRandom::DeterministicSeed()); |
| 112 const int count_test_block = 10000; |
| 113 for (int i = 0; i < count_test_block; ++i) { |
| 114 int16_t input[64], coeff[64]; |
| 115 int16_t output_c[64]; |
| 116 double output_r[64]; |
| 117 |
| 118 // Initialize a test block with input range [-255, 255]. |
| 119 for (int j = 0; j < 64; ++j) |
| 120 input[j] = rnd.Rand8() - rnd.Rand8(); |
| 121 |
| 122 const int pitch = 16; |
| 123 vp9_short_fdct8x8_c(input, output_c, pitch); |
| 124 reference_dct_2d(input, output_r); |
| 125 |
| 126 for (int j = 0; j < 64; ++j) { |
| 127 const double diff = output_c[j] - output_r[j]; |
| 128 const double error = diff * diff; |
| 129 // An error in a DCT coefficient isn't that bad. |
| 130 // We care more about the reconstructed pixels. |
| 131 EXPECT_GE(2.0, error) |
| 132 << "Error: 8x8 FDCT/IDCT has error " << error |
| 133 << " at index " << j; |
| 134 } |
| 135 |
| 136 #if 0 |
| 137 // Tests that the reference iDCT and fDCT match. |
| 138 reference_dct_2d(input, output_r); |
| 139 reference_idct_2d(output_r, output_c); |
| 140 for (int j = 0; j < 64; ++j) { |
| 141 const int diff = output_c[j] -input[j]; |
| 142 const int error = diff * diff; |
| 143 EXPECT_EQ(0, error) |
| 144 << "Error: 8x8 FDCT/IDCT has error " << error |
| 145 << " at index " << j; |
| 146 } |
| 147 #endif |
| 148 reference_dct_2d(input, output_r); |
| 149 for (int j = 0; j < 64; ++j) |
| 150 coeff[j] = round(output_r[j]); |
| 151 vp9_short_idct8x8_c(coeff, output_c, pitch); |
| 152 for (int j = 0; j < 64; ++j) { |
| 153 const int diff = output_c[j] -input[j]; |
| 154 const int error = diff * diff; |
| 155 EXPECT_GE(1, error) |
| 156 << "Error: 8x8 FDCT/IDCT has error " << error |
| 157 << " at index " << j; |
| 158 } |
| 159 } |
| 160 } |
| 161 |
| 162 } // namespace |
OLD | NEW |