fdct8x8_test.cc 5.51 KB
Newer Older
Daniel Kang's avatar
Daniel Kang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "third_party/googletest/src/include/gtest/gtest.h"

extern "C" {
18
#include "vp9_rtcd.h"
Scott LaVarnway's avatar
Scott LaVarnway committed
19
void vp9_short_idct8x8_add_c(short *input, uint8_t *output, int pitch);
Daniel Kang's avatar
Daniel Kang committed
20 21 22 23 24 25 26 27 28
}

#include "acm_random.h"
#include "vpx/vpx_integer.h"

using libvpx_test::ACMRandom;

namespace {

James Zern's avatar
James Zern committed
29
TEST(VP9Fdct8x8Test, SignBiasCheck) {
Daniel Kang's avatar
Daniel Kang committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  int16_t test_input_block[64];
  int16_t test_output_block[64];
  const int pitch = 16;
  int count_sign_block[64][2];
  const int count_test_block = 100000;

  memset(count_sign_block, 0, sizeof(count_sign_block));

  for (int i = 0; i < count_test_block; ++i) {
    // Initialize a test block with input range [-255, 255].
    for (int j = 0; j < 64; ++j)
      test_input_block[j] = rnd.Rand8() - rnd.Rand8();

James Zern's avatar
James Zern committed
44
    vp9_short_fdct8x8_c(test_input_block, test_output_block, pitch);
Daniel Kang's avatar
Daniel Kang committed
45 46 47 48 49 50 51 52 53 54

    for (int j = 0; j < 64; ++j) {
      if (test_output_block[j] < 0)
        ++count_sign_block[j][0];
      else if (test_output_block[j] > 0)
        ++count_sign_block[j][1];
    }
  }

  for (int j = 0; j < 64; ++j) {
55 56 57 58 59 60 61 62 63
    const int diff = abs(count_sign_block[j][0] - count_sign_block[j][1]);
    const int max_diff = 1125;
    EXPECT_LT(diff, max_diff)
        << "Error: 8x8 FDCT has a sign bias > "
        << 1. * max_diff / count_test_block * 100 << "%"
        << " for input range [-255, 255] at index " << j
        << " count0: " << count_sign_block[j][0]
        << " count1: " << count_sign_block[j][1]
        << " diff: " << diff;
Daniel Kang's avatar
Daniel Kang committed
64 65 66 67 68 69 70 71 72
  }

  memset(count_sign_block, 0, sizeof(count_sign_block));

  for (int i = 0; i < count_test_block; ++i) {
    // Initialize a test block with input range [-15, 15].
    for (int j = 0; j < 64; ++j)
      test_input_block[j] = (rnd.Rand8() >> 4) - (rnd.Rand8() >> 4);

James Zern's avatar
James Zern committed
73
    vp9_short_fdct8x8_c(test_input_block, test_output_block, pitch);
Daniel Kang's avatar
Daniel Kang committed
74 75 76 77 78 79 80 81 82 83

    for (int j = 0; j < 64; ++j) {
      if (test_output_block[j] < 0)
        ++count_sign_block[j][0];
      else if (test_output_block[j] > 0)
        ++count_sign_block[j][1];
    }
  }

  for (int j = 0; j < 64; ++j) {
84 85 86 87 88 89 90 91 92
    const int diff = abs(count_sign_block[j][0] - count_sign_block[j][1]);
    const int max_diff = 10000;
    EXPECT_LT(diff, max_diff)
        << "Error: 4x4 FDCT has a sign bias > "
        << 1. * max_diff / count_test_block * 100 << "%"
        << " for input range [-15, 15] at index " << j
        << " count0: " << count_sign_block[j][0]
        << " count1: " << count_sign_block[j][1]
        << " diff: " << diff;
Daniel Kang's avatar
Daniel Kang committed
93 94 95
  }
};

James Zern's avatar
James Zern committed
96
TEST(VP9Fdct8x8Test, RoundTripErrorCheck) {
Daniel Kang's avatar
Daniel Kang committed
97 98 99 100 101 102 103
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  int max_error = 0;
  double total_error = 0;
  const int count_test_block = 100000;
  for (int i = 0; i < count_test_block; ++i) {
    int16_t test_input_block[64];
    int16_t test_temp_block[64];
Scott LaVarnway's avatar
Scott LaVarnway committed
104
    uint8_t dst[64], src[64];
Daniel Kang's avatar
Daniel Kang committed
105

Scott LaVarnway's avatar
Scott LaVarnway committed
106 107 108 109
    for (int j = 0; j < 64; ++j) {
      src[j] = rnd.Rand8();
      dst[j] = rnd.Rand8();
    }
Daniel Kang's avatar
Daniel Kang committed
110 111
    // Initialize a test block with input range [-255, 255].
    for (int j = 0; j < 64; ++j)
Scott LaVarnway's avatar
Scott LaVarnway committed
112
      test_input_block[j] = src[j] - dst[j];
Daniel Kang's avatar
Daniel Kang committed
113 114

    const int pitch = 16;
James Zern's avatar
James Zern committed
115
    vp9_short_fdct8x8_c(test_input_block, test_temp_block, pitch);
116 117 118 119 120 121 122 123 124 125 126
    for (int j = 0; j < 64; ++j){
        if(test_temp_block[j] > 0) {
          test_temp_block[j] += 2;
          test_temp_block[j] /= 4;
          test_temp_block[j] *= 4;
        } else {
          test_temp_block[j] -= 2;
          test_temp_block[j] /= 4;
          test_temp_block[j] *= 4;
        }
    }
Scott LaVarnway's avatar
Scott LaVarnway committed
127
    vp9_short_idct8x8_add_c(test_temp_block, dst, 8);
Daniel Kang's avatar
Daniel Kang committed
128 129

    for (int j = 0; j < 64; ++j) {
Scott LaVarnway's avatar
Scott LaVarnway committed
130
      const int diff = dst[j] - src[j];
Daniel Kang's avatar
Daniel Kang committed
131 132 133 134 135 136 137 138 139 140
      const int error = diff * diff;
      if (max_error < error)
        max_error = error;
      total_error += error;
    }
  }

  EXPECT_GE(1, max_error)
      << "Error: 8x8 FDCT/IDCT has an individual roundtrip error > 1";

Daniel Kang's avatar
Daniel Kang committed
141 142
  EXPECT_GE(count_test_block/5, total_error)
      << "Error: 8x8 FDCT/IDCT has average roundtrip error > 1/5 per block";
Daniel Kang's avatar
Daniel Kang committed
143 144
};

James Zern's avatar
James Zern committed
145
TEST(VP9Fdct8x8Test, ExtremalCheck) {
Daniel Kang's avatar
Daniel Kang committed
146 147 148 149 150 151 152
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  int max_error = 0;
  double total_error = 0;
  const int count_test_block = 100000;
  for (int i = 0; i < count_test_block; ++i) {
    int16_t test_input_block[64];
    int16_t test_temp_block[64];
Scott LaVarnway's avatar
Scott LaVarnway committed
153
    uint8_t dst[64], src[64];
Daniel Kang's avatar
Daniel Kang committed
154

Scott LaVarnway's avatar
Scott LaVarnway committed
155 156 157 158 159
    for (int j = 0; j < 64; ++j) {
      src[j] = rnd.Rand8() % 2 ? 255 : 0;
      dst[j] = src[j] > 0 ? 0 : 255;
    }
    // Initialize a test block with input range [-255, 255].
Daniel Kang's avatar
Daniel Kang committed
160
    for (int j = 0; j < 64; ++j)
Scott LaVarnway's avatar
Scott LaVarnway committed
161
      test_input_block[j] = src[j] - dst[j];
Daniel Kang's avatar
Daniel Kang committed
162 163

    const int pitch = 16;
James Zern's avatar
James Zern committed
164
    vp9_short_fdct8x8_c(test_input_block, test_temp_block, pitch);
Scott LaVarnway's avatar
Scott LaVarnway committed
165
    vp9_short_idct8x8_add_c(test_temp_block, dst, 8);
Daniel Kang's avatar
Daniel Kang committed
166 167

    for (int j = 0; j < 64; ++j) {
Scott LaVarnway's avatar
Scott LaVarnway committed
168
      const int diff = dst[j] - src[j];
Daniel Kang's avatar
Daniel Kang committed
169 170 171 172 173 174 175 176 177 178
      const int error = diff * diff;
      if (max_error < error)
        max_error = error;
      total_error += error;
    }

    EXPECT_GE(1, max_error)
        << "Error: Extremal 8x8 FDCT/IDCT has an"
        << " individual roundtrip error > 1";

Daniel Kang's avatar
Daniel Kang committed
179
    EXPECT_GE(count_test_block/5, total_error)
Daniel Kang's avatar
Daniel Kang committed
180
        << "Error: Extremal 8x8 FDCT/IDCT has average"
Daniel Kang's avatar
Daniel Kang committed
181
        << " roundtrip error > 1/5 per block";
Daniel Kang's avatar
Daniel Kang committed
182 183 184 185
  }
};

}  // namespace