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