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