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 #include <tuple>
15 
16 #include "third_party/googletest/src/include/gtest/gtest.h"
17 
18 #include "./vp9_rtcd.h"
19 #include "./vpx_dsp_rtcd.h"
20 #include "test/acm_random.h"
21 #include "test/clear_system_state.h"
22 #include "test/register_state_check.h"
23 #include "test/util.h"
24 #include "vp9/common/vp9_entropy.h"
25 #include "vp9/common/vp9_scan.h"
26 #include "vpx/vpx_codec.h"
27 #include "vpx/vpx_integer.h"
28 #include "vpx_ports/mem.h"
29 #include "vpx_ports/msvc.h"  // for round()
30 
31 using libvpx_test::ACMRandom;
32 
33 namespace {
34 
35 const int kNumCoeffs = 256;
36 const double C1 = 0.995184726672197;
37 const double C2 = 0.98078528040323;
38 const double C3 = 0.956940335732209;
39 const double C4 = 0.923879532511287;
40 const double C5 = 0.881921264348355;
41 const double C6 = 0.831469612302545;
42 const double C7 = 0.773010453362737;
43 const double C8 = 0.707106781186548;
44 const double C9 = 0.634393284163646;
45 const double C10 = 0.555570233019602;
46 const double C11 = 0.471396736825998;
47 const double C12 = 0.38268343236509;
48 const double C13 = 0.290284677254462;
49 const double C14 = 0.195090322016128;
50 const double C15 = 0.098017140329561;
51 
butterfly_16x16_dct_1d(double input[16],double output[16])52 void butterfly_16x16_dct_1d(double input[16], double output[16]) {
53   double step[16];
54   double intermediate[16];
55   double temp1, temp2;
56 
57   // step 1
58   step[0] = input[0] + input[15];
59   step[1] = input[1] + input[14];
60   step[2] = input[2] + input[13];
61   step[3] = input[3] + input[12];
62   step[4] = input[4] + input[11];
63   step[5] = input[5] + input[10];
64   step[6] = input[6] + input[9];
65   step[7] = input[7] + input[8];
66   step[8] = input[7] - input[8];
67   step[9] = input[6] - input[9];
68   step[10] = input[5] - input[10];
69   step[11] = input[4] - input[11];
70   step[12] = input[3] - input[12];
71   step[13] = input[2] - input[13];
72   step[14] = input[1] - input[14];
73   step[15] = input[0] - input[15];
74 
75   // step 2
76   output[0] = step[0] + step[7];
77   output[1] = step[1] + step[6];
78   output[2] = step[2] + step[5];
79   output[3] = step[3] + step[4];
80   output[4] = step[3] - step[4];
81   output[5] = step[2] - step[5];
82   output[6] = step[1] - step[6];
83   output[7] = step[0] - step[7];
84 
85   temp1 = step[8] * C7;
86   temp2 = step[15] * C9;
87   output[8] = temp1 + temp2;
88 
89   temp1 = step[9] * C11;
90   temp2 = step[14] * C5;
91   output[9] = temp1 - temp2;
92 
93   temp1 = step[10] * C3;
94   temp2 = step[13] * C13;
95   output[10] = temp1 + temp2;
96 
97   temp1 = step[11] * C15;
98   temp2 = step[12] * C1;
99   output[11] = temp1 - temp2;
100 
101   temp1 = step[11] * C1;
102   temp2 = step[12] * C15;
103   output[12] = temp2 + temp1;
104 
105   temp1 = step[10] * C13;
106   temp2 = step[13] * C3;
107   output[13] = temp2 - temp1;
108 
109   temp1 = step[9] * C5;
110   temp2 = step[14] * C11;
111   output[14] = temp2 + temp1;
112 
113   temp1 = step[8] * C9;
114   temp2 = step[15] * C7;
115   output[15] = temp2 - temp1;
116 
117   // step 3
118   step[0] = output[0] + output[3];
119   step[1] = output[1] + output[2];
120   step[2] = output[1] - output[2];
121   step[3] = output[0] - output[3];
122 
123   temp1 = output[4] * C14;
124   temp2 = output[7] * C2;
125   step[4] = temp1 + temp2;
126 
127   temp1 = output[5] * C10;
128   temp2 = output[6] * C6;
129   step[5] = temp1 + temp2;
130 
131   temp1 = output[5] * C6;
132   temp2 = output[6] * C10;
133   step[6] = temp2 - temp1;
134 
135   temp1 = output[4] * C2;
136   temp2 = output[7] * C14;
137   step[7] = temp2 - temp1;
138 
139   step[8] = output[8] + output[11];
140   step[9] = output[9] + output[10];
141   step[10] = output[9] - output[10];
142   step[11] = output[8] - output[11];
143 
144   step[12] = output[12] + output[15];
145   step[13] = output[13] + output[14];
146   step[14] = output[13] - output[14];
147   step[15] = output[12] - output[15];
148 
149   // step 4
150   output[0] = (step[0] + step[1]);
151   output[8] = (step[0] - step[1]);
152 
153   temp1 = step[2] * C12;
154   temp2 = step[3] * C4;
155   temp1 = temp1 + temp2;
156   output[4] = 2 * (temp1 * C8);
157 
158   temp1 = step[2] * C4;
159   temp2 = step[3] * C12;
160   temp1 = temp2 - temp1;
161   output[12] = 2 * (temp1 * C8);
162 
163   output[2] = 2 * ((step[4] + step[5]) * C8);
164   output[14] = 2 * ((step[7] - step[6]) * C8);
165 
166   temp1 = step[4] - step[5];
167   temp2 = step[6] + step[7];
168   output[6] = (temp1 + temp2);
169   output[10] = (temp1 - temp2);
170 
171   intermediate[8] = step[8] + step[14];
172   intermediate[9] = step[9] + step[15];
173 
174   temp1 = intermediate[8] * C12;
175   temp2 = intermediate[9] * C4;
176   temp1 = temp1 - temp2;
177   output[3] = 2 * (temp1 * C8);
178 
179   temp1 = intermediate[8] * C4;
180   temp2 = intermediate[9] * C12;
181   temp1 = temp2 + temp1;
182   output[13] = 2 * (temp1 * C8);
183 
184   output[9] = 2 * ((step[10] + step[11]) * C8);
185 
186   intermediate[11] = step[10] - step[11];
187   intermediate[12] = step[12] + step[13];
188   intermediate[13] = step[12] - step[13];
189   intermediate[14] = step[8] - step[14];
190   intermediate[15] = step[9] - step[15];
191 
192   output[15] = (intermediate[11] + intermediate[12]);
193   output[1] = -(intermediate[11] - intermediate[12]);
194 
195   output[7] = 2 * (intermediate[13] * C8);
196 
197   temp1 = intermediate[14] * C12;
198   temp2 = intermediate[15] * C4;
199   temp1 = temp1 - temp2;
200   output[11] = -2 * (temp1 * C8);
201 
202   temp1 = intermediate[14] * C4;
203   temp2 = intermediate[15] * C12;
204   temp1 = temp2 + temp1;
205   output[5] = 2 * (temp1 * C8);
206 }
207 
reference_16x16_dct_2d(int16_t input[256],double output[256])208 void reference_16x16_dct_2d(int16_t input[256], double output[256]) {
209   // First transform columns
210   for (int i = 0; i < 16; ++i) {
211     double temp_in[16], temp_out[16];
212     for (int j = 0; j < 16; ++j) temp_in[j] = input[j * 16 + i];
213     butterfly_16x16_dct_1d(temp_in, temp_out);
214     for (int j = 0; j < 16; ++j) output[j * 16 + i] = temp_out[j];
215   }
216   // Then transform rows
217   for (int i = 0; i < 16; ++i) {
218     double temp_in[16], temp_out[16];
219     for (int j = 0; j < 16; ++j) temp_in[j] = output[j + i * 16];
220     butterfly_16x16_dct_1d(temp_in, temp_out);
221     // Scale by some magic number
222     for (int j = 0; j < 16; ++j) output[j + i * 16] = temp_out[j] / 2;
223   }
224 }
225 
226 typedef void (*FdctFunc)(const int16_t *in, tran_low_t *out, int stride);
227 typedef void (*IdctFunc)(const tran_low_t *in, uint8_t *out, int stride);
228 typedef void (*FhtFunc)(const int16_t *in, tran_low_t *out, int stride,
229                         int tx_type);
230 typedef void (*IhtFunc)(const tran_low_t *in, uint8_t *out, int stride,
231                         int tx_type);
232 
233 typedef std::tuple<FdctFunc, IdctFunc, int, vpx_bit_depth_t> Dct16x16Param;
234 typedef std::tuple<FhtFunc, IhtFunc, int, vpx_bit_depth_t> Ht16x16Param;
235 typedef std::tuple<IdctFunc, IdctFunc, int, vpx_bit_depth_t> Idct16x16Param;
236 
fdct16x16_ref(const int16_t * in,tran_low_t * out,int stride,int)237 void fdct16x16_ref(const int16_t *in, tran_low_t *out, int stride,
238                    int /*tx_type*/) {
239   vpx_fdct16x16_c(in, out, stride);
240 }
241 
idct16x16_ref(const tran_low_t * in,uint8_t * dest,int stride,int)242 void idct16x16_ref(const tran_low_t *in, uint8_t *dest, int stride,
243                    int /*tx_type*/) {
244   vpx_idct16x16_256_add_c(in, dest, stride);
245 }
246 
fht16x16_ref(const int16_t * in,tran_low_t * out,int stride,int tx_type)247 void fht16x16_ref(const int16_t *in, tran_low_t *out, int stride, int tx_type) {
248   vp9_fht16x16_c(in, out, stride, tx_type);
249 }
250 
iht16x16_ref(const tran_low_t * in,uint8_t * dest,int stride,int tx_type)251 void iht16x16_ref(const tran_low_t *in, uint8_t *dest, int stride,
252                   int tx_type) {
253   vp9_iht16x16_256_add_c(in, dest, stride, tx_type);
254 }
255 
256 #if CONFIG_VP9_HIGHBITDEPTH
idct16x16_10(const tran_low_t * in,uint8_t * out,int stride)257 void idct16x16_10(const tran_low_t *in, uint8_t *out, int stride) {
258   vpx_highbd_idct16x16_256_add_c(in, CAST_TO_SHORTPTR(out), stride, 10);
259 }
260 
idct16x16_12(const tran_low_t * in,uint8_t * out,int stride)261 void idct16x16_12(const tran_low_t *in, uint8_t *out, int stride) {
262   vpx_highbd_idct16x16_256_add_c(in, CAST_TO_SHORTPTR(out), stride, 12);
263 }
264 
idct16x16_10_ref(const tran_low_t * in,uint8_t * out,int stride,int)265 void idct16x16_10_ref(const tran_low_t *in, uint8_t *out, int stride,
266                       int /*tx_type*/) {
267   idct16x16_10(in, out, stride);
268 }
269 
idct16x16_12_ref(const tran_low_t * in,uint8_t * out,int stride,int)270 void idct16x16_12_ref(const tran_low_t *in, uint8_t *out, int stride,
271                       int /*tx_type*/) {
272   idct16x16_12(in, out, stride);
273 }
274 
iht16x16_10(const tran_low_t * in,uint8_t * out,int stride,int tx_type)275 void iht16x16_10(const tran_low_t *in, uint8_t *out, int stride, int tx_type) {
276   vp9_highbd_iht16x16_256_add_c(in, CAST_TO_SHORTPTR(out), stride, tx_type, 10);
277 }
278 
iht16x16_12(const tran_low_t * in,uint8_t * out,int stride,int tx_type)279 void iht16x16_12(const tran_low_t *in, uint8_t *out, int stride, int tx_type) {
280   vp9_highbd_iht16x16_256_add_c(in, CAST_TO_SHORTPTR(out), stride, tx_type, 12);
281 }
282 
283 #if HAVE_SSE2
idct16x16_10_add_10_c(const tran_low_t * in,uint8_t * out,int stride)284 void idct16x16_10_add_10_c(const tran_low_t *in, uint8_t *out, int stride) {
285   vpx_highbd_idct16x16_10_add_c(in, CAST_TO_SHORTPTR(out), stride, 10);
286 }
287 
idct16x16_10_add_12_c(const tran_low_t * in,uint8_t * out,int stride)288 void idct16x16_10_add_12_c(const tran_low_t *in, uint8_t *out, int stride) {
289   vpx_highbd_idct16x16_10_add_c(in, CAST_TO_SHORTPTR(out), stride, 12);
290 }
291 
idct16x16_256_add_10_sse2(const tran_low_t * in,uint8_t * out,int stride)292 void idct16x16_256_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
293   vpx_highbd_idct16x16_256_add_sse2(in, CAST_TO_SHORTPTR(out), stride, 10);
294 }
295 
idct16x16_256_add_12_sse2(const tran_low_t * in,uint8_t * out,int stride)296 void idct16x16_256_add_12_sse2(const tran_low_t *in, uint8_t *out, int stride) {
297   vpx_highbd_idct16x16_256_add_sse2(in, CAST_TO_SHORTPTR(out), stride, 12);
298 }
299 
idct16x16_10_add_10_sse2(const tran_low_t * in,uint8_t * out,int stride)300 void idct16x16_10_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
301   vpx_highbd_idct16x16_10_add_sse2(in, CAST_TO_SHORTPTR(out), stride, 10);
302 }
303 
idct16x16_10_add_12_sse2(const tran_low_t * in,uint8_t * out,int stride)304 void idct16x16_10_add_12_sse2(const tran_low_t *in, uint8_t *out, int stride) {
305   vpx_highbd_idct16x16_10_add_sse2(in, CAST_TO_SHORTPTR(out), stride, 12);
306 }
307 #endif  // HAVE_SSE2
308 #endif  // CONFIG_VP9_HIGHBITDEPTH
309 
310 class Trans16x16TestBase {
311  public:
~Trans16x16TestBase()312   virtual ~Trans16x16TestBase() {}
313 
314  protected:
315   virtual void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) = 0;
316 
317   virtual void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) = 0;
318 
RunAccuracyCheck()319   void RunAccuracyCheck() {
320     ACMRandom rnd(ACMRandom::DeterministicSeed());
321     uint32_t max_error = 0;
322     int64_t total_error = 0;
323     const int count_test_block = 10000;
324     for (int i = 0; i < count_test_block; ++i) {
325       DECLARE_ALIGNED(16, int16_t, test_input_block[kNumCoeffs]);
326       DECLARE_ALIGNED(16, tran_low_t, test_temp_block[kNumCoeffs]);
327       DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
328       DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
329 #if CONFIG_VP9_HIGHBITDEPTH
330       DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
331       DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
332 #endif
333 
334       // Initialize a test block with input range [-mask_, mask_].
335       for (int j = 0; j < kNumCoeffs; ++j) {
336         if (bit_depth_ == VPX_BITS_8) {
337           src[j] = rnd.Rand8();
338           dst[j] = rnd.Rand8();
339           test_input_block[j] = src[j] - dst[j];
340 #if CONFIG_VP9_HIGHBITDEPTH
341         } else {
342           src16[j] = rnd.Rand16() & mask_;
343           dst16[j] = rnd.Rand16() & mask_;
344           test_input_block[j] = src16[j] - dst16[j];
345 #endif
346         }
347       }
348 
349       ASM_REGISTER_STATE_CHECK(
350           RunFwdTxfm(test_input_block, test_temp_block, pitch_));
351       if (bit_depth_ == VPX_BITS_8) {
352         ASM_REGISTER_STATE_CHECK(RunInvTxfm(test_temp_block, dst, pitch_));
353 #if CONFIG_VP9_HIGHBITDEPTH
354       } else {
355         ASM_REGISTER_STATE_CHECK(
356             RunInvTxfm(test_temp_block, CAST_TO_BYTEPTR(dst16), pitch_));
357 #endif
358       }
359 
360       for (int j = 0; j < kNumCoeffs; ++j) {
361 #if CONFIG_VP9_HIGHBITDEPTH
362         const int32_t diff =
363             bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
364 #else
365         const int32_t diff = dst[j] - src[j];
366 #endif
367         const uint32_t error = diff * diff;
368         if (max_error < error) max_error = error;
369         total_error += error;
370       }
371     }
372 
373     EXPECT_GE(1u << 2 * (bit_depth_ - 8), max_error)
374         << "Error: 16x16 FHT/IHT has an individual round trip error > 1";
375 
376     EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
377         << "Error: 16x16 FHT/IHT has average round trip error > 1 per block";
378   }
379 
RunCoeffCheck()380   void RunCoeffCheck() {
381     ACMRandom rnd(ACMRandom::DeterministicSeed());
382     const int count_test_block = 1000;
383     DECLARE_ALIGNED(16, int16_t, input_block[kNumCoeffs]);
384     DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
385     DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
386 
387     for (int i = 0; i < count_test_block; ++i) {
388       // Initialize a test block with input range [-mask_, mask_].
389       for (int j = 0; j < kNumCoeffs; ++j) {
390         input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
391       }
392 
393       fwd_txfm_ref(input_block, output_ref_block, pitch_, tx_type_);
394       ASM_REGISTER_STATE_CHECK(RunFwdTxfm(input_block, output_block, pitch_));
395 
396       // The minimum quant value is 4.
397       for (int j = 0; j < kNumCoeffs; ++j)
398         EXPECT_EQ(output_block[j], output_ref_block[j]);
399     }
400   }
401 
RunMemCheck()402   void RunMemCheck() {
403     ACMRandom rnd(ACMRandom::DeterministicSeed());
404     const int count_test_block = 1000;
405     DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
406     DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
407     DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
408 
409     for (int i = 0; i < count_test_block; ++i) {
410       // Initialize a test block with input range [-mask_, mask_].
411       for (int j = 0; j < kNumCoeffs; ++j) {
412         input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
413       }
414       if (i == 0) {
415         for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = mask_;
416       } else if (i == 1) {
417         for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = -mask_;
418       }
419 
420       fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);
421       ASM_REGISTER_STATE_CHECK(
422           RunFwdTxfm(input_extreme_block, output_block, pitch_));
423 
424       // The minimum quant value is 4.
425       for (int j = 0; j < kNumCoeffs; ++j) {
426         EXPECT_EQ(output_block[j], output_ref_block[j]);
427         EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
428             << "Error: 16x16 FDCT has coefficient larger than 4*DCT_MAX_VALUE";
429       }
430     }
431   }
432 
RunQuantCheck(int dc_thred,int ac_thred)433   void RunQuantCheck(int dc_thred, int ac_thred) {
434     ACMRandom rnd(ACMRandom::DeterministicSeed());
435     const int count_test_block = 100000;
436     DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
437     DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
438 
439     DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
440     DECLARE_ALIGNED(16, uint8_t, ref[kNumCoeffs]);
441 #if CONFIG_VP9_HIGHBITDEPTH
442     DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
443     DECLARE_ALIGNED(16, uint16_t, ref16[kNumCoeffs]);
444 #endif
445 
446     for (int i = 0; i < count_test_block; ++i) {
447       // Initialize a test block with input range [-mask_, mask_].
448       for (int j = 0; j < kNumCoeffs; ++j) {
449         input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
450       }
451       if (i == 0) {
452         for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = mask_;
453       }
454       if (i == 1) {
455         for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = -mask_;
456       }
457 
458       fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);
459 
460       // clear reconstructed pixel buffers
461       memset(dst, 0, kNumCoeffs * sizeof(uint8_t));
462       memset(ref, 0, kNumCoeffs * sizeof(uint8_t));
463 #if CONFIG_VP9_HIGHBITDEPTH
464       memset(dst16, 0, kNumCoeffs * sizeof(uint16_t));
465       memset(ref16, 0, kNumCoeffs * sizeof(uint16_t));
466 #endif
467 
468       // quantization with maximum allowed step sizes
469       output_ref_block[0] = (output_ref_block[0] / dc_thred) * dc_thred;
470       for (int j = 1; j < kNumCoeffs; ++j) {
471         output_ref_block[j] = (output_ref_block[j] / ac_thred) * ac_thred;
472       }
473       if (bit_depth_ == VPX_BITS_8) {
474         inv_txfm_ref(output_ref_block, ref, pitch_, tx_type_);
475         ASM_REGISTER_STATE_CHECK(RunInvTxfm(output_ref_block, dst, pitch_));
476 #if CONFIG_VP9_HIGHBITDEPTH
477       } else {
478         inv_txfm_ref(output_ref_block, CAST_TO_BYTEPTR(ref16), pitch_,
479                      tx_type_);
480         ASM_REGISTER_STATE_CHECK(
481             RunInvTxfm(output_ref_block, CAST_TO_BYTEPTR(dst16), pitch_));
482 #endif
483       }
484       if (bit_depth_ == VPX_BITS_8) {
485         for (int j = 0; j < kNumCoeffs; ++j) EXPECT_EQ(ref[j], dst[j]);
486 #if CONFIG_VP9_HIGHBITDEPTH
487       } else {
488         for (int j = 0; j < kNumCoeffs; ++j) EXPECT_EQ(ref16[j], dst16[j]);
489 #endif
490       }
491     }
492   }
493 
RunInvAccuracyCheck()494   void RunInvAccuracyCheck() {
495     ACMRandom rnd(ACMRandom::DeterministicSeed());
496     const int count_test_block = 1000;
497     DECLARE_ALIGNED(16, int16_t, in[kNumCoeffs]);
498     DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
499     DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
500     DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
501 #if CONFIG_VP9_HIGHBITDEPTH
502     DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
503     DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
504 #endif  // CONFIG_VP9_HIGHBITDEPTH
505 
506     for (int i = 0; i < count_test_block; ++i) {
507       double out_r[kNumCoeffs];
508 
509       // Initialize a test block with input range [-255, 255].
510       for (int j = 0; j < kNumCoeffs; ++j) {
511         if (bit_depth_ == VPX_BITS_8) {
512           src[j] = rnd.Rand8();
513           dst[j] = rnd.Rand8();
514           in[j] = src[j] - dst[j];
515 #if CONFIG_VP9_HIGHBITDEPTH
516         } else {
517           src16[j] = rnd.Rand16() & mask_;
518           dst16[j] = rnd.Rand16() & mask_;
519           in[j] = src16[j] - dst16[j];
520 #endif  // CONFIG_VP9_HIGHBITDEPTH
521         }
522       }
523 
524       reference_16x16_dct_2d(in, out_r);
525       for (int j = 0; j < kNumCoeffs; ++j) {
526         coeff[j] = static_cast<tran_low_t>(round(out_r[j]));
527       }
528 
529       if (bit_depth_ == VPX_BITS_8) {
530         ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, 16));
531 #if CONFIG_VP9_HIGHBITDEPTH
532       } else {
533         ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, CAST_TO_BYTEPTR(dst16), 16));
534 #endif  // CONFIG_VP9_HIGHBITDEPTH
535       }
536 
537       for (int j = 0; j < kNumCoeffs; ++j) {
538 #if CONFIG_VP9_HIGHBITDEPTH
539         const uint32_t diff =
540             bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
541 #else
542         const uint32_t diff = dst[j] - src[j];
543 #endif  // CONFIG_VP9_HIGHBITDEPTH
544         const uint32_t error = diff * diff;
545         EXPECT_GE(1u, error)
546             << "Error: 16x16 IDCT has error " << error << " at index " << j;
547       }
548     }
549   }
550 
CompareInvReference(IdctFunc ref_txfm,int thresh)551   void CompareInvReference(IdctFunc ref_txfm, int thresh) {
552     ACMRandom rnd(ACMRandom::DeterministicSeed());
553     const int count_test_block = 10000;
554     const int eob = 10;
555     const int16_t *scan = vp9_default_scan_orders[TX_16X16].scan;
556     DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
557     DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
558     DECLARE_ALIGNED(16, uint8_t, ref[kNumCoeffs]);
559 #if CONFIG_VP9_HIGHBITDEPTH
560     DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
561     DECLARE_ALIGNED(16, uint16_t, ref16[kNumCoeffs]);
562 #endif  // CONFIG_VP9_HIGHBITDEPTH
563 
564     for (int i = 0; i < count_test_block; ++i) {
565       for (int j = 0; j < kNumCoeffs; ++j) {
566         if (j < eob) {
567           // Random values less than the threshold, either positive or negative
568           coeff[scan[j]] = rnd(thresh) * (1 - 2 * (i % 2));
569         } else {
570           coeff[scan[j]] = 0;
571         }
572         if (bit_depth_ == VPX_BITS_8) {
573           dst[j] = 0;
574           ref[j] = 0;
575 #if CONFIG_VP9_HIGHBITDEPTH
576         } else {
577           dst16[j] = 0;
578           ref16[j] = 0;
579 #endif  // CONFIG_VP9_HIGHBITDEPTH
580         }
581       }
582       if (bit_depth_ == VPX_BITS_8) {
583         ref_txfm(coeff, ref, pitch_);
584         ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, pitch_));
585       } else {
586 #if CONFIG_VP9_HIGHBITDEPTH
587         ref_txfm(coeff, CAST_TO_BYTEPTR(ref16), pitch_);
588         ASM_REGISTER_STATE_CHECK(
589             RunInvTxfm(coeff, CAST_TO_BYTEPTR(dst16), pitch_));
590 #endif  // CONFIG_VP9_HIGHBITDEPTH
591       }
592 
593       for (int j = 0; j < kNumCoeffs; ++j) {
594 #if CONFIG_VP9_HIGHBITDEPTH
595         const uint32_t diff =
596             bit_depth_ == VPX_BITS_8 ? dst[j] - ref[j] : dst16[j] - ref16[j];
597 #else
598         const uint32_t diff = dst[j] - ref[j];
599 #endif  // CONFIG_VP9_HIGHBITDEPTH
600         const uint32_t error = diff * diff;
601         EXPECT_EQ(0u, error) << "Error: 16x16 IDCT Comparison has error "
602                              << error << " at index " << j;
603       }
604     }
605   }
606 
607   int pitch_;
608   int tx_type_;
609   vpx_bit_depth_t bit_depth_;
610   int mask_;
611   FhtFunc fwd_txfm_ref;
612   IhtFunc inv_txfm_ref;
613 };
614 
615 class Trans16x16DCT : public Trans16x16TestBase,
616                       public ::testing::TestWithParam<Dct16x16Param> {
617  public:
~Trans16x16DCT()618   virtual ~Trans16x16DCT() {}
619 
SetUp()620   virtual void SetUp() {
621     fwd_txfm_ = GET_PARAM(0);
622     inv_txfm_ = GET_PARAM(1);
623     tx_type_ = GET_PARAM(2);
624     bit_depth_ = GET_PARAM(3);
625     pitch_ = 16;
626     fwd_txfm_ref = fdct16x16_ref;
627     inv_txfm_ref = idct16x16_ref;
628     mask_ = (1 << bit_depth_) - 1;
629 #if CONFIG_VP9_HIGHBITDEPTH
630     switch (bit_depth_) {
631       case VPX_BITS_10: inv_txfm_ref = idct16x16_10_ref; break;
632       case VPX_BITS_12: inv_txfm_ref = idct16x16_12_ref; break;
633       default: inv_txfm_ref = idct16x16_ref; break;
634     }
635 #else
636     inv_txfm_ref = idct16x16_ref;
637 #endif
638   }
TearDown()639   virtual void TearDown() { libvpx_test::ClearSystemState(); }
640 
641  protected:
RunFwdTxfm(int16_t * in,tran_low_t * out,int stride)642   void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {
643     fwd_txfm_(in, out, stride);
644   }
RunInvTxfm(tran_low_t * out,uint8_t * dst,int stride)645   void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
646     inv_txfm_(out, dst, stride);
647   }
648 
649   FdctFunc fwd_txfm_;
650   IdctFunc inv_txfm_;
651 };
652 
TEST_P(Trans16x16DCT,AccuracyCheck)653 TEST_P(Trans16x16DCT, AccuracyCheck) { RunAccuracyCheck(); }
654 
TEST_P(Trans16x16DCT,CoeffCheck)655 TEST_P(Trans16x16DCT, CoeffCheck) { RunCoeffCheck(); }
656 
TEST_P(Trans16x16DCT,MemCheck)657 TEST_P(Trans16x16DCT, MemCheck) { RunMemCheck(); }
658 
TEST_P(Trans16x16DCT,QuantCheck)659 TEST_P(Trans16x16DCT, QuantCheck) {
660   // Use maximally allowed quantization step sizes for DC and AC
661   // coefficients respectively.
662   RunQuantCheck(1336, 1828);
663 }
664 
TEST_P(Trans16x16DCT,InvAccuracyCheck)665 TEST_P(Trans16x16DCT, InvAccuracyCheck) { RunInvAccuracyCheck(); }
666 
667 class Trans16x16HT : public Trans16x16TestBase,
668                      public ::testing::TestWithParam<Ht16x16Param> {
669  public:
~Trans16x16HT()670   virtual ~Trans16x16HT() {}
671 
SetUp()672   virtual void SetUp() {
673     fwd_txfm_ = GET_PARAM(0);
674     inv_txfm_ = GET_PARAM(1);
675     tx_type_ = GET_PARAM(2);
676     bit_depth_ = GET_PARAM(3);
677     pitch_ = 16;
678     fwd_txfm_ref = fht16x16_ref;
679     inv_txfm_ref = iht16x16_ref;
680     mask_ = (1 << bit_depth_) - 1;
681 #if CONFIG_VP9_HIGHBITDEPTH
682     switch (bit_depth_) {
683       case VPX_BITS_10: inv_txfm_ref = iht16x16_10; break;
684       case VPX_BITS_12: inv_txfm_ref = iht16x16_12; break;
685       default: inv_txfm_ref = iht16x16_ref; break;
686     }
687 #else
688     inv_txfm_ref = iht16x16_ref;
689 #endif
690   }
TearDown()691   virtual void TearDown() { libvpx_test::ClearSystemState(); }
692 
693  protected:
RunFwdTxfm(int16_t * in,tran_low_t * out,int stride)694   void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {
695     fwd_txfm_(in, out, stride, tx_type_);
696   }
RunInvTxfm(tran_low_t * out,uint8_t * dst,int stride)697   void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
698     inv_txfm_(out, dst, stride, tx_type_);
699   }
700 
701   FhtFunc fwd_txfm_;
702   IhtFunc inv_txfm_;
703 };
704 
TEST_P(Trans16x16HT,AccuracyCheck)705 TEST_P(Trans16x16HT, AccuracyCheck) { RunAccuracyCheck(); }
706 
TEST_P(Trans16x16HT,CoeffCheck)707 TEST_P(Trans16x16HT, CoeffCheck) { RunCoeffCheck(); }
708 
TEST_P(Trans16x16HT,MemCheck)709 TEST_P(Trans16x16HT, MemCheck) { RunMemCheck(); }
710 
TEST_P(Trans16x16HT,QuantCheck)711 TEST_P(Trans16x16HT, QuantCheck) {
712   // The encoder skips any non-DC intra prediction modes,
713   // when the quantization step size goes beyond 988.
714   RunQuantCheck(429, 729);
715 }
716 
717 class InvTrans16x16DCT : public Trans16x16TestBase,
718                          public ::testing::TestWithParam<Idct16x16Param> {
719  public:
~InvTrans16x16DCT()720   virtual ~InvTrans16x16DCT() {}
721 
SetUp()722   virtual void SetUp() {
723     ref_txfm_ = GET_PARAM(0);
724     inv_txfm_ = GET_PARAM(1);
725     thresh_ = GET_PARAM(2);
726     bit_depth_ = GET_PARAM(3);
727     pitch_ = 16;
728     mask_ = (1 << bit_depth_) - 1;
729   }
TearDown()730   virtual void TearDown() { libvpx_test::ClearSystemState(); }
731 
732  protected:
RunFwdTxfm(int16_t *,tran_low_t *,int)733   void RunFwdTxfm(int16_t * /*in*/, tran_low_t * /*out*/, int /*stride*/) {}
RunInvTxfm(tran_low_t * out,uint8_t * dst,int stride)734   void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
735     inv_txfm_(out, dst, stride);
736   }
737 
738   IdctFunc ref_txfm_;
739   IdctFunc inv_txfm_;
740   int thresh_;
741 };
742 
TEST_P(InvTrans16x16DCT,CompareReference)743 TEST_P(InvTrans16x16DCT, CompareReference) {
744   CompareInvReference(ref_txfm_, thresh_);
745 }
746 
747 using std::make_tuple;
748 
749 #if CONFIG_VP9_HIGHBITDEPTH
750 INSTANTIATE_TEST_CASE_P(
751     C, Trans16x16DCT,
752     ::testing::Values(
753         make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_10, 0, VPX_BITS_10),
754         make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_12, 0, VPX_BITS_12),
755         make_tuple(&vpx_fdct16x16_c, &vpx_idct16x16_256_add_c, 0, VPX_BITS_8)));
756 #else
757 INSTANTIATE_TEST_CASE_P(C, Trans16x16DCT,
758                         ::testing::Values(make_tuple(&vpx_fdct16x16_c,
759                                                      &vpx_idct16x16_256_add_c,
760                                                      0, VPX_BITS_8)));
761 #endif  // CONFIG_VP9_HIGHBITDEPTH
762 
763 #if CONFIG_VP9_HIGHBITDEPTH
764 INSTANTIATE_TEST_CASE_P(
765     C, Trans16x16HT,
766     ::testing::Values(
767         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 0, VPX_BITS_10),
768         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 1, VPX_BITS_10),
769         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 2, VPX_BITS_10),
770         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 3, VPX_BITS_10),
771         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 0, VPX_BITS_12),
772         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 1, VPX_BITS_12),
773         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 2, VPX_BITS_12),
774         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 3, VPX_BITS_12),
775         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
776         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
777         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
778         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 3, VPX_BITS_8)));
779 #else
780 INSTANTIATE_TEST_CASE_P(
781     C, Trans16x16HT,
782     ::testing::Values(
783         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
784         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
785         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
786         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 3, VPX_BITS_8)));
787 #endif  // CONFIG_VP9_HIGHBITDEPTH
788 
789 #if HAVE_NEON && !CONFIG_EMULATE_HARDWARE
790 INSTANTIATE_TEST_CASE_P(
791     NEON, Trans16x16DCT,
792     ::testing::Values(make_tuple(&vpx_fdct16x16_neon,
793                                  &vpx_idct16x16_256_add_neon, 0, VPX_BITS_8)));
794 #endif  // HAVE_NEON && !CONFIG_EMULATE_HARDWARE
795 
796 #if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
797 INSTANTIATE_TEST_CASE_P(
798     SSE2, Trans16x16DCT,
799     ::testing::Values(make_tuple(&vpx_fdct16x16_sse2,
800                                  &vpx_idct16x16_256_add_sse2, 0, VPX_BITS_8)));
801 INSTANTIATE_TEST_CASE_P(
802     SSE2, Trans16x16HT,
803     ::testing::Values(make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2,
804                                  0, VPX_BITS_8),
805                       make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2,
806                                  1, VPX_BITS_8),
807                       make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2,
808                                  2, VPX_BITS_8),
809                       make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2,
810                                  3, VPX_BITS_8)));
811 #endif  // HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
812 
813 #if HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
814 INSTANTIATE_TEST_CASE_P(
815     SSE2, Trans16x16DCT,
816     ::testing::Values(
817         make_tuple(&vpx_highbd_fdct16x16_sse2, &idct16x16_10, 0, VPX_BITS_10),
818         make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_256_add_10_sse2, 0,
819                    VPX_BITS_10),
820         make_tuple(&vpx_highbd_fdct16x16_sse2, &idct16x16_12, 0, VPX_BITS_12),
821         make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_256_add_12_sse2, 0,
822                    VPX_BITS_12),
823         make_tuple(&vpx_fdct16x16_sse2, &vpx_idct16x16_256_add_c, 0,
824                    VPX_BITS_8)));
825 INSTANTIATE_TEST_CASE_P(
826     SSE2, Trans16x16HT,
827     ::testing::Values(
828         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
829         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
830         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
831         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 3,
832                    VPX_BITS_8)));
833 // Optimizations take effect at a threshold of 3155, so we use a value close to
834 // that to test both branches.
835 INSTANTIATE_TEST_CASE_P(
836     SSE2, InvTrans16x16DCT,
837     ::testing::Values(make_tuple(&idct16x16_10_add_10_c,
838                                  &idct16x16_10_add_10_sse2, 3167, VPX_BITS_10),
839                       make_tuple(&idct16x16_10, &idct16x16_256_add_10_sse2,
840                                  3167, VPX_BITS_10),
841                       make_tuple(&idct16x16_10_add_12_c,
842                                  &idct16x16_10_add_12_sse2, 3167, VPX_BITS_12),
843                       make_tuple(&idct16x16_12, &idct16x16_256_add_12_sse2,
844                                  3167, VPX_BITS_12)));
845 #endif  // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
846 
847 #if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
848 INSTANTIATE_TEST_CASE_P(MSA, Trans16x16DCT,
849                         ::testing::Values(make_tuple(&vpx_fdct16x16_msa,
850                                                      &vpx_idct16x16_256_add_msa,
851                                                      0, VPX_BITS_8)));
852 INSTANTIATE_TEST_CASE_P(
853     MSA, Trans16x16HT,
854     ::testing::Values(
855         make_tuple(&vp9_fht16x16_msa, &vp9_iht16x16_256_add_msa, 0, VPX_BITS_8),
856         make_tuple(&vp9_fht16x16_msa, &vp9_iht16x16_256_add_msa, 1, VPX_BITS_8),
857         make_tuple(&vp9_fht16x16_msa, &vp9_iht16x16_256_add_msa, 2, VPX_BITS_8),
858         make_tuple(&vp9_fht16x16_msa, &vp9_iht16x16_256_add_msa, 3,
859                    VPX_BITS_8)));
860 #endif  // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
861 
862 #if HAVE_VSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
863 INSTANTIATE_TEST_CASE_P(VSX, Trans16x16DCT,
864                         ::testing::Values(make_tuple(&vpx_fdct16x16_c,
865                                                      &vpx_idct16x16_256_add_vsx,
866                                                      0, VPX_BITS_8)));
867 #endif  // HAVE_VSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
868 }  // namespace
869