idct8x8_test.cc 2.85 KB
Newer Older
Daniel Kang's avatar
Daniel Kang committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Daniel Kang's avatar
Daniel Kang committed
3
 *
4 5 6 7 8 9 10 11
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/

Daniel Kang's avatar
Daniel Kang committed
12 13 14 15 16 17 18

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

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

Adrian Grange's avatar
Adrian Grange committed
19
#include "./aom_dsp_rtcd.h"
Yaowu Xu's avatar
Yaowu Xu committed
20
#include "test/acm_random.h"
Adrian Grange's avatar
Adrian Grange committed
21
#include "aom/aom_integer.h"
Daniel Kang's avatar
Daniel Kang committed
22

Yaowu Xu's avatar
Yaowu Xu committed
23
using libaom_test::ACMRandom;
Daniel Kang's avatar
Daniel Kang committed
24 25 26

namespace {

27 28
#ifdef _MSC_VER
static int round(double x) {
Yaowu Xu's avatar
Yaowu Xu committed
29 30
  if (x < 0)
    return static_cast<int>(ceil(x - 0.5));
31
  else
Yaowu Xu's avatar
Yaowu Xu committed
32
    return static_cast<int>(floor(x + 0.5));
33 34 35
}
#endif

Daniel Kang's avatar
Daniel Kang committed
36 37 38 39 40 41
void reference_dct_1d(double input[8], double output[8]) {
  const double kPi = 3.141592653589793238462643383279502884;
  const double kInvSqrt2 = 0.707106781186547524400844362104;
  for (int k = 0; k < 8; k++) {
    output[k] = 0.0;
    for (int n = 0; n < 8; n++)
clang-format's avatar
clang-format committed
42 43
      output[k] += input[n] * cos(kPi * (2 * n + 1) * k / 16.0);
    if (k == 0) output[k] = output[k] * kInvSqrt2;
Daniel Kang's avatar
Daniel Kang committed
44 45 46 47 48 49 50
  }
}

void reference_dct_2d(int16_t input[64], double output[64]) {
  // First transform columns
  for (int i = 0; i < 8; ++i) {
    double temp_in[8], temp_out[8];
clang-format's avatar
clang-format committed
51
    for (int j = 0; j < 8; ++j) temp_in[j] = input[j * 8 + i];
Daniel Kang's avatar
Daniel Kang committed
52
    reference_dct_1d(temp_in, temp_out);
clang-format's avatar
clang-format committed
53
    for (int j = 0; j < 8; ++j) output[j * 8 + i] = temp_out[j];
Daniel Kang's avatar
Daniel Kang committed
54 55 56 57
  }
  // Then transform rows
  for (int i = 0; i < 8; ++i) {
    double temp_in[8], temp_out[8];
clang-format's avatar
clang-format committed
58
    for (int j = 0; j < 8; ++j) temp_in[j] = output[j + i * 8];
Daniel Kang's avatar
Daniel Kang committed
59
    reference_dct_1d(temp_in, temp_out);
clang-format's avatar
clang-format committed
60
    for (int j = 0; j < 8; ++j) output[j + i * 8] = temp_out[j];
Daniel Kang's avatar
Daniel Kang committed
61 62
  }
  // Scale by some magic number
clang-format's avatar
clang-format committed
63
  for (int i = 0; i < 64; ++i) output[i] *= 2;
Daniel Kang's avatar
Daniel Kang committed
64 65
}

Adrian Grange's avatar
Adrian Grange committed
66
TEST(AV1Idct8x8Test, AccuracyCheck) {
Daniel Kang's avatar
Daniel Kang committed
67 68 69
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  const int count_test_block = 10000;
  for (int i = 0; i < count_test_block; ++i) {
70 71
    int16_t input[64];
    tran_low_t coeff[64];
Daniel Kang's avatar
Daniel Kang committed
72
    double output_r[64];
Scott LaVarnway's avatar
Scott LaVarnway committed
73
    uint8_t dst[64], src[64];
Daniel Kang's avatar
Daniel Kang committed
74

Scott LaVarnway's avatar
Scott LaVarnway committed
75 76 77 78
    for (int j = 0; j < 64; ++j) {
      src[j] = rnd.Rand8();
      dst[j] = rnd.Rand8();
    }
Daniel Kang's avatar
Daniel Kang committed
79
    // Initialize a test block with input range [-255, 255].
clang-format's avatar
clang-format committed
80
    for (int j = 0; j < 64; ++j) input[j] = src[j] - dst[j];
Daniel Kang's avatar
Daniel Kang committed
81 82

    reference_dct_2d(input, output_r);
clang-format's avatar
clang-format committed
83
    for (int j = 0; j < 64; ++j) coeff[j] = round(output_r[j]);
Adrian Grange's avatar
Adrian Grange committed
84
    aom_idct8x8_64_add_c(coeff, dst, 8);
Daniel Kang's avatar
Daniel Kang committed
85
    for (int j = 0; j < 64; ++j) {
Scott LaVarnway's avatar
Scott LaVarnway committed
86
      const int diff = dst[j] - src[j];
Daniel Kang's avatar
Daniel Kang committed
87
      const int error = diff * diff;
clang-format's avatar
clang-format committed
88 89
      EXPECT_GE(1, error) << "Error: 8x8 FDCT/IDCT has error " << error
                          << " at index " << j;
Daniel Kang's avatar
Daniel Kang committed
90 91 92 93 94
    }
  }
}

}  // namespace