idct8x8_test.cc 2.66 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
/*
 *  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"

17
#include "./vpx_dsp_rtcd.h"
Yaowu Xu's avatar
Yaowu Xu committed
18
#include "test/acm_random.h"
Daniel Kang's avatar
Daniel Kang committed
19
#include "vpx/vpx_integer.h"
20
#include "vpx_ports/msvc.h"  // for round()
Daniel Kang's avatar
Daniel Kang committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

using libvpx_test::ACMRandom;

namespace {

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++)
      output[k] += input[n]*cos(kPi*(2*n+1)*k/16.0);
    if (k == 0)
      output[k] = output[k]*kInvSqrt2;
  }
}

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];
    for (int j = 0; j < 8; ++j)
      temp_in[j] = input[j*8 + i];
    reference_dct_1d(temp_in, temp_out);
    for (int j = 0; j < 8; ++j)
      output[j*8 + i] = temp_out[j];
  }
  // Then transform rows
  for (int i = 0; i < 8; ++i) {
    double temp_in[8], temp_out[8];
    for (int j = 0; j < 8; ++j)
      temp_in[j] = output[j + i*8];
    reference_dct_1d(temp_in, temp_out);
    for (int j = 0; j < 8; ++j)
      output[j + i*8] = temp_out[j];
  }
  // Scale by some magic number
  for (int i = 0; i < 64; ++i)
    output[i] *= 2;
}

James Zern's avatar
James Zern committed
62
TEST(VP9Idct8x8Test, AccuracyCheck) {
Daniel Kang's avatar
Daniel Kang committed
63 64 65
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  const int count_test_block = 10000;
  for (int i = 0; i < count_test_block; ++i) {
66 67
    int16_t input[64];
    tran_low_t coeff[64];
Daniel Kang's avatar
Daniel Kang committed
68
    double output_r[64];
Scott LaVarnway's avatar
Scott LaVarnway committed
69
    uint8_t dst[64], src[64];
Daniel Kang's avatar
Daniel Kang committed
70

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

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

}  // namespace