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