Merge "Add experimental spatial de-noise filter on key frames."
[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)
212       temp_in[j] = input[j * 16 + i];
213     butterfly_16x16_dct_1d(temp_in, temp_out);
214     for (int j = 0; j < 16; ++j)
215       output[j * 16 + i] = temp_out[j];
216   }
217   // Then transform rows
218   for (int i = 0; i < 16; ++i) {
219     double temp_in[16], temp_out[16];
220     for (int j = 0; j < 16; ++j)
221       temp_in[j] = output[j + i * 16];
222     butterfly_16x16_dct_1d(temp_in, temp_out);
223     // Scale by some magic number
224     for (int j = 0; j < 16; ++j)
225       output[j + i * 16] = temp_out[j]/2;
226   }
227 }
228
229 typedef void (*FdctFunc)(const int16_t *in, tran_low_t *out, int stride);
230 typedef void (*IdctFunc)(const tran_low_t *in, uint8_t *out, int stride);
231 typedef void (*FhtFunc)(const int16_t *in, tran_low_t *out, int stride,
232                         int tx_type);
233 typedef void (*IhtFunc)(const tran_low_t *in, uint8_t *out, int stride,
234                         int tx_type);
235
236 typedef std::tr1::tuple<FdctFunc, IdctFunc, int, vpx_bit_depth_t> Dct16x16Param;
237 typedef std::tr1::tuple<FhtFunc, IhtFunc, int, vpx_bit_depth_t> Ht16x16Param;
238 typedef std::tr1::tuple<IdctFunc, IdctFunc, int, vpx_bit_depth_t>
239     Idct16x16Param;
240
241 void fdct16x16_ref(const int16_t *in, tran_low_t *out, int stride,
242                    int /*tx_type*/) {
243   vpx_fdct16x16_c(in, out, stride);
244 }
245
246 void idct16x16_ref(const tran_low_t *in, uint8_t *dest, int stride,
247                    int /*tx_type*/) {
248   vpx_idct16x16_256_add_c(in, dest, stride);
249 }
250
251 void fht16x16_ref(const int16_t *in, tran_low_t *out, int stride,
252                   int tx_type) {
253   vp9_fht16x16_c(in, out, stride, tx_type);
254 }
255
256 void iht16x16_ref(const tran_low_t *in, uint8_t *dest, int stride,
257                   int tx_type) {
258   vp9_iht16x16_256_add_c(in, dest, stride, tx_type);
259 }
260
261 #if CONFIG_VP9_HIGHBITDEPTH
262 void idct16x16_10(const tran_low_t *in, uint8_t *out, int stride) {
263   vpx_highbd_idct16x16_256_add_c(in, out, stride, 10);
264 }
265
266 void idct16x16_12(const tran_low_t *in, uint8_t *out, int stride) {
267   vpx_highbd_idct16x16_256_add_c(in, out, stride, 12);
268 }
269
270 void idct16x16_10_ref(const tran_low_t *in, uint8_t *out, int stride,
271                       int /*tx_type*/) {
272   idct16x16_10(in, out, stride);
273 }
274
275 void idct16x16_12_ref(const tran_low_t *in, uint8_t *out, int stride,
276                       int /*tx_type*/) {
277   idct16x16_12(in, out, stride);
278 }
279
280 void iht16x16_10(const tran_low_t *in, uint8_t *out, int stride, int tx_type) {
281   vp9_highbd_iht16x16_256_add_c(in, out, stride, tx_type, 10);
282 }
283
284 void iht16x16_12(const tran_low_t *in, uint8_t *out, int stride, int tx_type) {
285   vp9_highbd_iht16x16_256_add_c(in, out, stride, tx_type, 12);
286 }
287
288 #if HAVE_SSE2
289 void idct16x16_10_add_10_c(const tran_low_t *in, uint8_t *out, int stride) {
290   vpx_highbd_idct16x16_10_add_c(in, out, stride, 10);
291 }
292
293 void idct16x16_10_add_12_c(const tran_low_t *in, uint8_t *out, int stride) {
294   vpx_highbd_idct16x16_10_add_c(in, out, stride, 12);
295 }
296
297 void idct16x16_256_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
298   vpx_highbd_idct16x16_256_add_sse2(in, out, stride, 10);
299 }
300
301 void idct16x16_256_add_12_sse2(const tran_low_t *in, uint8_t *out, int stride) {
302   vpx_highbd_idct16x16_256_add_sse2(in, out, stride, 12);
303 }
304
305 void idct16x16_10_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
306   vpx_highbd_idct16x16_10_add_sse2(in, out, stride, 10);
307 }
308
309 void idct16x16_10_add_12_sse2(const tran_low_t *in, uint8_t *out, int stride) {
310   vpx_highbd_idct16x16_10_add_sse2(in, out, stride, 12);
311 }
312 #endif  // HAVE_SSE2
313 #endif  // CONFIG_VP9_HIGHBITDEPTH
314
315 class Trans16x16TestBase {
316  public:
317   virtual ~Trans16x16TestBase() {}
318
319  protected:
320   virtual void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) = 0;
321
322   virtual void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) = 0;
323
324   void RunAccuracyCheck() {
325     ACMRandom rnd(ACMRandom::DeterministicSeed());
326     uint32_t max_error = 0;
327     int64_t total_error = 0;
328     const int count_test_block = 10000;
329     for (int i = 0; i < count_test_block; ++i) {
330       DECLARE_ALIGNED(16, int16_t, test_input_block[kNumCoeffs]);
331       DECLARE_ALIGNED(16, tran_low_t, test_temp_block[kNumCoeffs]);
332       DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
333       DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
334 #if CONFIG_VP9_HIGHBITDEPTH
335       DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
336       DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
337 #endif
338
339       // Initialize a test block with input range [-mask_, mask_].
340       for (int j = 0; j < kNumCoeffs; ++j) {
341         if (bit_depth_ == VPX_BITS_8) {
342           src[j] = rnd.Rand8();
343           dst[j] = rnd.Rand8();
344           test_input_block[j] = src[j] - dst[j];
345 #if CONFIG_VP9_HIGHBITDEPTH
346         } else {
347           src16[j] = rnd.Rand16() & mask_;
348           dst16[j] = rnd.Rand16() & mask_;
349           test_input_block[j] = src16[j] - dst16[j];
350 #endif
351         }
352       }
353
354       ASM_REGISTER_STATE_CHECK(RunFwdTxfm(test_input_block,
355                                           test_temp_block, pitch_));
356       if (bit_depth_ == VPX_BITS_8) {
357         ASM_REGISTER_STATE_CHECK(
358             RunInvTxfm(test_temp_block, dst, pitch_));
359 #if CONFIG_VP9_HIGHBITDEPTH
360       } else {
361         ASM_REGISTER_STATE_CHECK(
362             RunInvTxfm(test_temp_block, CONVERT_TO_BYTEPTR(dst16), pitch_));
363 #endif
364       }
365
366       for (int j = 0; j < kNumCoeffs; ++j) {
367 #if CONFIG_VP9_HIGHBITDEPTH
368         const int32_t diff =
369             bit_depth_ == VPX_BITS_8 ?  dst[j] - src[j] : dst16[j] - src16[j];
370 #else
371         const int32_t diff = dst[j] - src[j];
372 #endif
373         const uint32_t error = diff * diff;
374         if (max_error < error)
375           max_error = error;
376         total_error += error;
377       }
378     }
379
380     EXPECT_GE(1u  << 2 * (bit_depth_ - 8), max_error)
381         << "Error: 16x16 FHT/IHT has an individual round trip error > 1";
382
383     EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
384         << "Error: 16x16 FHT/IHT has average round trip error > 1 per block";
385   }
386
387   void RunCoeffCheck() {
388     ACMRandom rnd(ACMRandom::DeterministicSeed());
389     const int count_test_block = 1000;
390     DECLARE_ALIGNED(16, int16_t, input_block[kNumCoeffs]);
391     DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
392     DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
393
394     for (int i = 0; i < count_test_block; ++i) {
395       // Initialize a test block with input range [-mask_, mask_].
396       for (int j = 0; j < kNumCoeffs; ++j)
397         input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
398
399       fwd_txfm_ref(input_block, output_ref_block, pitch_, tx_type_);
400       ASM_REGISTER_STATE_CHECK(RunFwdTxfm(input_block, output_block, pitch_));
401
402       // The minimum quant value is 4.
403       for (int j = 0; j < kNumCoeffs; ++j)
404         EXPECT_EQ(output_block[j], output_ref_block[j]);
405     }
406   }
407
408   void RunMemCheck() {
409     ACMRandom rnd(ACMRandom::DeterministicSeed());
410     const int count_test_block = 1000;
411     DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
412     DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
413     DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
414
415     for (int i = 0; i < count_test_block; ++i) {
416       // Initialize a test block with input range [-mask_, mask_].
417       for (int j = 0; j < kNumCoeffs; ++j) {
418         input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
419       }
420       if (i == 0) {
421         for (int j = 0; j < kNumCoeffs; ++j)
422           input_extreme_block[j] = mask_;
423       } else if (i == 1) {
424         for (int j = 0; j < kNumCoeffs; ++j)
425           input_extreme_block[j] = -mask_;
426       }
427
428       fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);
429       ASM_REGISTER_STATE_CHECK(RunFwdTxfm(input_extreme_block,
430                                           output_block, pitch_));
431
432       // The minimum quant value is 4.
433       for (int j = 0; j < kNumCoeffs; ++j) {
434         EXPECT_EQ(output_block[j], output_ref_block[j]);
435         EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
436             << "Error: 16x16 FDCT has coefficient larger than 4*DCT_MAX_VALUE";
437       }
438     }
439   }
440
441   void RunQuantCheck(int dc_thred, int ac_thred) {
442     ACMRandom rnd(ACMRandom::DeterministicSeed());
443     const int count_test_block = 100000;
444     DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
445     DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
446
447     DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
448     DECLARE_ALIGNED(16, uint8_t, ref[kNumCoeffs]);
449 #if CONFIG_VP9_HIGHBITDEPTH
450     DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
451     DECLARE_ALIGNED(16, uint16_t, ref16[kNumCoeffs]);
452 #endif
453
454     for (int i = 0; i < count_test_block; ++i) {
455       // Initialize a test block with input range [-mask_, mask_].
456       for (int j = 0; j < kNumCoeffs; ++j) {
457         input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
458       }
459       if (i == 0)
460         for (int j = 0; j < kNumCoeffs; ++j)
461           input_extreme_block[j] = mask_;
462       if (i == 1)
463         for (int j = 0; j < kNumCoeffs; ++j)
464           input_extreme_block[j] = -mask_;
465
466       fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);
467
468       // clear reconstructed pixel buffers
469       memset(dst, 0, kNumCoeffs * sizeof(uint8_t));
470       memset(ref, 0, kNumCoeffs * sizeof(uint8_t));
471 #if CONFIG_VP9_HIGHBITDEPTH
472       memset(dst16, 0, kNumCoeffs * sizeof(uint16_t));
473       memset(ref16, 0, kNumCoeffs * sizeof(uint16_t));
474 #endif
475
476       // quantization with maximum allowed step sizes
477       output_ref_block[0] = (output_ref_block[0] / dc_thred) * dc_thred;
478       for (int j = 1; j < kNumCoeffs; ++j)
479         output_ref_block[j] = (output_ref_block[j] / ac_thred) * ac_thred;
480       if (bit_depth_ == VPX_BITS_8) {
481         inv_txfm_ref(output_ref_block, ref, pitch_, tx_type_);
482         ASM_REGISTER_STATE_CHECK(RunInvTxfm(output_ref_block, dst, pitch_));
483 #if CONFIG_VP9_HIGHBITDEPTH
484       } else {
485         inv_txfm_ref(output_ref_block, CONVERT_TO_BYTEPTR(ref16), pitch_,
486                      tx_type_);
487         ASM_REGISTER_STATE_CHECK(RunInvTxfm(output_ref_block,
488                                             CONVERT_TO_BYTEPTR(dst16), pitch_));
489 #endif
490       }
491       if (bit_depth_ == VPX_BITS_8) {
492         for (int j = 0; j < kNumCoeffs; ++j)
493           EXPECT_EQ(ref[j], dst[j]);
494 #if CONFIG_VP9_HIGHBITDEPTH
495       } else {
496         for (int j = 0; j < kNumCoeffs; ++j)
497           EXPECT_EQ(ref16[j], dst16[j]);
498 #endif
499       }
500     }
501   }
502
503   void RunInvAccuracyCheck() {
504     ACMRandom rnd(ACMRandom::DeterministicSeed());
505     const int count_test_block = 1000;
506     DECLARE_ALIGNED(16, int16_t, in[kNumCoeffs]);
507     DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
508     DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
509     DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
510 #if CONFIG_VP9_HIGHBITDEPTH
511     DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
512     DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
513 #endif  // CONFIG_VP9_HIGHBITDEPTH
514
515     for (int i = 0; i < count_test_block; ++i) {
516       double out_r[kNumCoeffs];
517
518       // Initialize a test block with input range [-255, 255].
519       for (int j = 0; j < kNumCoeffs; ++j) {
520         if (bit_depth_ == VPX_BITS_8) {
521           src[j] = rnd.Rand8();
522           dst[j] = rnd.Rand8();
523           in[j] = src[j] - dst[j];
524 #if CONFIG_VP9_HIGHBITDEPTH
525         } else {
526           src16[j] = rnd.Rand16() & mask_;
527           dst16[j] = rnd.Rand16() & mask_;
528           in[j] = src16[j] - dst16[j];
529 #endif  // CONFIG_VP9_HIGHBITDEPTH
530         }
531       }
532
533       reference_16x16_dct_2d(in, out_r);
534       for (int j = 0; j < kNumCoeffs; ++j)
535         coeff[j] = static_cast<tran_low_t>(round(out_r[j]));
536
537       if (bit_depth_ == VPX_BITS_8) {
538         ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, 16));
539 #if CONFIG_VP9_HIGHBITDEPTH
540       } else {
541         ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, CONVERT_TO_BYTEPTR(dst16),
542                                             16));
543 #endif  // CONFIG_VP9_HIGHBITDEPTH
544       }
545
546       for (int j = 0; j < kNumCoeffs; ++j) {
547 #if CONFIG_VP9_HIGHBITDEPTH
548         const uint32_t diff =
549             bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
550 #else
551         const uint32_t diff = dst[j] - src[j];
552 #endif  // CONFIG_VP9_HIGHBITDEPTH
553         const uint32_t error = diff * diff;
554         EXPECT_GE(1u, error)
555             << "Error: 16x16 IDCT has error " << error
556             << " at index " << j;
557       }
558     }
559   }
560
561   void CompareInvReference(IdctFunc ref_txfm, int thresh) {
562     ACMRandom rnd(ACMRandom::DeterministicSeed());
563     const int count_test_block = 10000;
564     const int eob = 10;
565     const int16_t *scan = vp9_default_scan_orders[TX_16X16].scan;
566     DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
567     DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
568     DECLARE_ALIGNED(16, uint8_t, ref[kNumCoeffs]);
569 #if CONFIG_VP9_HIGHBITDEPTH
570     DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
571     DECLARE_ALIGNED(16, uint16_t, ref16[kNumCoeffs]);
572 #endif  // CONFIG_VP9_HIGHBITDEPTH
573
574     for (int i = 0; i < count_test_block; ++i) {
575       for (int j = 0; j < kNumCoeffs; ++j) {
576         if (j < eob) {
577           // Random values less than the threshold, either positive or negative
578           coeff[scan[j]] = rnd(thresh) * (1 - 2 * (i % 2));
579         } else {
580           coeff[scan[j]] = 0;
581         }
582         if (bit_depth_ == VPX_BITS_8) {
583           dst[j] = 0;
584           ref[j] = 0;
585 #if CONFIG_VP9_HIGHBITDEPTH
586         } else {
587           dst16[j] = 0;
588           ref16[j] = 0;
589 #endif  // CONFIG_VP9_HIGHBITDEPTH
590         }
591       }
592       if (bit_depth_ == VPX_BITS_8) {
593         ref_txfm(coeff, ref, pitch_);
594         ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, pitch_));
595       } else {
596 #if CONFIG_VP9_HIGHBITDEPTH
597         ref_txfm(coeff, CONVERT_TO_BYTEPTR(ref16), pitch_);
598         ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, CONVERT_TO_BYTEPTR(dst16),
599                                  pitch_));
600 #endif  // CONFIG_VP9_HIGHBITDEPTH
601       }
602
603       for (int j = 0; j < kNumCoeffs; ++j) {
604 #if CONFIG_VP9_HIGHBITDEPTH
605         const uint32_t diff =
606             bit_depth_ == VPX_BITS_8 ? dst[j] - ref[j] : dst16[j] - ref16[j];
607 #else
608         const uint32_t diff = dst[j] - ref[j];
609 #endif  // CONFIG_VP9_HIGHBITDEPTH
610         const uint32_t error = diff * diff;
611         EXPECT_EQ(0u, error)
612             << "Error: 16x16 IDCT Comparison has error " << error
613             << " at index " << j;
614       }
615     }
616   }
617
618   int pitch_;
619   int tx_type_;
620   vpx_bit_depth_t bit_depth_;
621   int mask_;
622   FhtFunc fwd_txfm_ref;
623   IhtFunc inv_txfm_ref;
624 };
625
626 class Trans16x16DCT
627     : public Trans16x16TestBase,
628       public ::testing::TestWithParam<Dct16x16Param> {
629  public:
630   virtual ~Trans16x16DCT() {}
631
632   virtual void SetUp() {
633     fwd_txfm_ = GET_PARAM(0);
634     inv_txfm_ = GET_PARAM(1);
635     tx_type_  = GET_PARAM(2);
636     bit_depth_ = GET_PARAM(3);
637     pitch_    = 16;
638     fwd_txfm_ref = fdct16x16_ref;
639     inv_txfm_ref = idct16x16_ref;
640     mask_ = (1 << bit_depth_) - 1;
641 #if CONFIG_VP9_HIGHBITDEPTH
642     switch (bit_depth_) {
643       case VPX_BITS_10:
644         inv_txfm_ref = idct16x16_10_ref;
645         break;
646       case VPX_BITS_12:
647         inv_txfm_ref = idct16x16_12_ref;
648         break;
649       default:
650         inv_txfm_ref = idct16x16_ref;
651         break;
652     }
653 #else
654     inv_txfm_ref = idct16x16_ref;
655 #endif
656   }
657   virtual void TearDown() { libvpx_test::ClearSystemState(); }
658
659  protected:
660   void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {
661     fwd_txfm_(in, out, stride);
662   }
663   void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
664     inv_txfm_(out, dst, stride);
665   }
666
667   FdctFunc fwd_txfm_;
668   IdctFunc inv_txfm_;
669 };
670
671 TEST_P(Trans16x16DCT, AccuracyCheck) {
672   RunAccuracyCheck();
673 }
674
675 TEST_P(Trans16x16DCT, CoeffCheck) {
676   RunCoeffCheck();
677 }
678
679 TEST_P(Trans16x16DCT, MemCheck) {
680   RunMemCheck();
681 }
682
683 TEST_P(Trans16x16DCT, QuantCheck) {
684   // Use maximally allowed quantization step sizes for DC and AC
685   // coefficients respectively.
686   RunQuantCheck(1336, 1828);
687 }
688
689 TEST_P(Trans16x16DCT, InvAccuracyCheck) {
690   RunInvAccuracyCheck();
691 }
692
693 class Trans16x16HT
694     : public Trans16x16TestBase,
695       public ::testing::TestWithParam<Ht16x16Param> {
696  public:
697   virtual ~Trans16x16HT() {}
698
699   virtual void SetUp() {
700     fwd_txfm_ = GET_PARAM(0);
701     inv_txfm_ = GET_PARAM(1);
702     tx_type_  = GET_PARAM(2);
703     bit_depth_ = GET_PARAM(3);
704     pitch_    = 16;
705     fwd_txfm_ref = fht16x16_ref;
706     inv_txfm_ref = iht16x16_ref;
707     mask_ = (1 << bit_depth_) - 1;
708 #if CONFIG_VP9_HIGHBITDEPTH
709     switch (bit_depth_) {
710       case VPX_BITS_10:
711         inv_txfm_ref = iht16x16_10;
712         break;
713       case VPX_BITS_12:
714         inv_txfm_ref = iht16x16_12;
715         break;
716       default:
717         inv_txfm_ref = iht16x16_ref;
718         break;
719     }
720 #else
721     inv_txfm_ref = iht16x16_ref;
722 #endif
723   }
724   virtual void TearDown() { libvpx_test::ClearSystemState(); }
725
726  protected:
727   void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {
728     fwd_txfm_(in, out, stride, tx_type_);
729   }
730   void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
731     inv_txfm_(out, dst, stride, tx_type_);
732   }
733
734   FhtFunc fwd_txfm_;
735   IhtFunc inv_txfm_;
736 };
737
738 TEST_P(Trans16x16HT, AccuracyCheck) {
739   RunAccuracyCheck();
740 }
741
742 TEST_P(Trans16x16HT, CoeffCheck) {
743   RunCoeffCheck();
744 }
745
746 TEST_P(Trans16x16HT, MemCheck) {
747   RunMemCheck();
748 }
749
750 TEST_P(Trans16x16HT, QuantCheck) {
751   // The encoder skips any non-DC intra prediction modes,
752   // when the quantization step size goes beyond 988.
753   RunQuantCheck(429, 729);
754 }
755
756 class InvTrans16x16DCT
757     : public Trans16x16TestBase,
758       public ::testing::TestWithParam<Idct16x16Param> {
759  public:
760   virtual ~InvTrans16x16DCT() {}
761
762   virtual void SetUp() {
763     ref_txfm_ = GET_PARAM(0);
764     inv_txfm_ = GET_PARAM(1);
765     thresh_ = GET_PARAM(2);
766     bit_depth_ = GET_PARAM(3);
767     pitch_ = 16;
768     mask_ = (1 << bit_depth_) - 1;
769 }
770   virtual void TearDown() { libvpx_test::ClearSystemState(); }
771
772  protected:
773   void RunFwdTxfm(int16_t * /*in*/, tran_low_t * /*out*/, int /*stride*/) {}
774   void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
775     inv_txfm_(out, dst, stride);
776   }
777
778   IdctFunc ref_txfm_;
779   IdctFunc inv_txfm_;
780   int thresh_;
781 };
782
783 TEST_P(InvTrans16x16DCT, CompareReference) {
784   CompareInvReference(ref_txfm_, thresh_);
785 }
786
787 class PartialTrans16x16Test
788     : public ::testing::TestWithParam<
789           std::tr1::tuple<FdctFunc, vpx_bit_depth_t> > {
790  public:
791   virtual ~PartialTrans16x16Test() {}
792   virtual void SetUp() {
793     fwd_txfm_ = GET_PARAM(0);
794     bit_depth_ = GET_PARAM(1);
795   }
796
797   virtual void TearDown() { libvpx_test::ClearSystemState(); }
798
799  protected:
800   vpx_bit_depth_t bit_depth_;
801   FdctFunc fwd_txfm_;
802 };
803
804 TEST_P(PartialTrans16x16Test, Extremes) {
805 #if CONFIG_VP9_HIGHBITDEPTH
806   const int16_t maxval =
807       static_cast<int16_t>(clip_pixel_highbd(1 << 30, bit_depth_));
808 #else
809   const int16_t maxval = 255;
810 #endif
811   const int minval = -maxval;
812   DECLARE_ALIGNED(16, int16_t, input[kNumCoeffs]);
813   DECLARE_ALIGNED(16, tran_low_t, output[kNumCoeffs]);
814
815   for (int i = 0; i < kNumCoeffs; ++i) input[i] = maxval;
816   output[0] = 0;
817   ASM_REGISTER_STATE_CHECK(fwd_txfm_(input, output, 16));
818   EXPECT_EQ((maxval * kNumCoeffs) >> 1, output[0]);
819
820   for (int i = 0; i < kNumCoeffs; ++i) input[i] = minval;
821   output[0] = 0;
822   ASM_REGISTER_STATE_CHECK(fwd_txfm_(input, output, 16));
823   EXPECT_EQ((minval * kNumCoeffs) >> 1, output[0]);
824 }
825
826 TEST_P(PartialTrans16x16Test, Random) {
827 #if CONFIG_VP9_HIGHBITDEPTH
828   const int16_t maxval =
829       static_cast<int16_t>(clip_pixel_highbd(1 << 30, bit_depth_));
830 #else
831   const int16_t maxval = 255;
832 #endif
833   DECLARE_ALIGNED(16, int16_t, input[kNumCoeffs]);
834   DECLARE_ALIGNED(16, tran_low_t, output[kNumCoeffs]);
835   ACMRandom rnd(ACMRandom::DeterministicSeed());
836
837   int sum = 0;
838   for (int i = 0; i < kNumCoeffs; ++i) {
839     const int val = (i & 1) ? -rnd(maxval + 1) : rnd(maxval + 1);
840     input[i] = val;
841     sum += val;
842   }
843   output[0] = 0;
844   ASM_REGISTER_STATE_CHECK(fwd_txfm_(input, output, 16));
845   EXPECT_EQ(sum >> 1, output[0]);
846 }
847
848 using std::tr1::make_tuple;
849
850 #if CONFIG_VP9_HIGHBITDEPTH
851 INSTANTIATE_TEST_CASE_P(
852     C, Trans16x16DCT,
853     ::testing::Values(
854         make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_10, 0, VPX_BITS_10),
855         make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_12, 0, VPX_BITS_12),
856         make_tuple(&vpx_fdct16x16_c, &vpx_idct16x16_256_add_c, 0, VPX_BITS_8)));
857 #else
858 INSTANTIATE_TEST_CASE_P(
859     C, Trans16x16DCT,
860     ::testing::Values(
861         make_tuple(&vpx_fdct16x16_c, &vpx_idct16x16_256_add_c, 0, VPX_BITS_8)));
862 #endif  // CONFIG_VP9_HIGHBITDEPTH
863
864 #if CONFIG_VP9_HIGHBITDEPTH
865 INSTANTIATE_TEST_CASE_P(
866     C, Trans16x16HT,
867     ::testing::Values(
868         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 0, VPX_BITS_10),
869         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 1, VPX_BITS_10),
870         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 2, VPX_BITS_10),
871         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 3, VPX_BITS_10),
872         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 0, VPX_BITS_12),
873         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 1, VPX_BITS_12),
874         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 2, VPX_BITS_12),
875         make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 3, VPX_BITS_12),
876         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
877         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
878         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
879         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 3, VPX_BITS_8)));
880 INSTANTIATE_TEST_CASE_P(
881     C, PartialTrans16x16Test,
882     ::testing::Values(make_tuple(&vpx_highbd_fdct16x16_1_c, VPX_BITS_8),
883                       make_tuple(&vpx_highbd_fdct16x16_1_c, VPX_BITS_10),
884                       make_tuple(&vpx_highbd_fdct16x16_1_c, VPX_BITS_12)));
885 #else
886 INSTANTIATE_TEST_CASE_P(
887     C, Trans16x16HT,
888     ::testing::Values(
889         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
890         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
891         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
892         make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 3, VPX_BITS_8)));
893 INSTANTIATE_TEST_CASE_P(C, PartialTrans16x16Test,
894                         ::testing::Values(make_tuple(&vpx_fdct16x16_1_c,
895                                                      VPX_BITS_8)));
896 #endif  // CONFIG_VP9_HIGHBITDEPTH
897
898 #if HAVE_NEON_ASM && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
899 INSTANTIATE_TEST_CASE_P(
900     NEON, Trans16x16DCT,
901     ::testing::Values(
902         make_tuple(&vpx_fdct16x16_c,
903                    &vpx_idct16x16_256_add_neon, 0, VPX_BITS_8)));
904 #endif
905
906 #if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
907 INSTANTIATE_TEST_CASE_P(
908     SSE2, Trans16x16DCT,
909     ::testing::Values(
910         make_tuple(&vpx_fdct16x16_sse2,
911                    &vpx_idct16x16_256_add_sse2, 0, VPX_BITS_8)));
912 INSTANTIATE_TEST_CASE_P(
913     SSE2, Trans16x16HT,
914     ::testing::Values(
915         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 0,
916                    VPX_BITS_8),
917         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 1,
918                    VPX_BITS_8),
919         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 2,
920                    VPX_BITS_8),
921         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 3,
922                    VPX_BITS_8)));
923 INSTANTIATE_TEST_CASE_P(SSE2, PartialTrans16x16Test,
924                         ::testing::Values(make_tuple(&vpx_fdct16x16_1_sse2,
925                                                      VPX_BITS_8)));
926 #endif  // HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
927
928 #if HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
929 INSTANTIATE_TEST_CASE_P(
930     SSE2, Trans16x16DCT,
931     ::testing::Values(
932         make_tuple(&vpx_highbd_fdct16x16_sse2,
933                    &idct16x16_10, 0, VPX_BITS_10),
934         make_tuple(&vpx_highbd_fdct16x16_c,
935                    &idct16x16_256_add_10_sse2, 0, VPX_BITS_10),
936         make_tuple(&vpx_highbd_fdct16x16_sse2,
937                    &idct16x16_12, 0, VPX_BITS_12),
938         make_tuple(&vpx_highbd_fdct16x16_c,
939                    &idct16x16_256_add_12_sse2, 0, VPX_BITS_12),
940         make_tuple(&vpx_fdct16x16_sse2,
941                    &vpx_idct16x16_256_add_c, 0, VPX_BITS_8)));
942 INSTANTIATE_TEST_CASE_P(
943     SSE2, Trans16x16HT,
944     ::testing::Values(
945         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
946         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
947         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
948         make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 3,
949                    VPX_BITS_8)));
950 // Optimizations take effect at a threshold of 3155, so we use a value close to
951 // that to test both branches.
952 INSTANTIATE_TEST_CASE_P(
953     SSE2, InvTrans16x16DCT,
954     ::testing::Values(
955         make_tuple(&idct16x16_10_add_10_c,
956                    &idct16x16_10_add_10_sse2, 3167, VPX_BITS_10),
957         make_tuple(&idct16x16_10,
958                    &idct16x16_256_add_10_sse2, 3167, VPX_BITS_10),
959         make_tuple(&idct16x16_10_add_12_c,
960                    &idct16x16_10_add_12_sse2, 3167, VPX_BITS_12),
961         make_tuple(&idct16x16_12,
962                    &idct16x16_256_add_12_sse2, 3167, VPX_BITS_12)));
963 INSTANTIATE_TEST_CASE_P(SSE2, PartialTrans16x16Test,
964                         ::testing::Values(make_tuple(&vpx_fdct16x16_1_sse2,
965                                                      VPX_BITS_8)));
966 #endif  // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
967
968 #if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
969 INSTANTIATE_TEST_CASE_P(
970     MSA, Trans16x16DCT,
971     ::testing::Values(
972         make_tuple(&vpx_fdct16x16_msa,
973                    &vpx_idct16x16_256_add_msa, 0, VPX_BITS_8)));
974 INSTANTIATE_TEST_CASE_P(
975     MSA, Trans16x16HT,
976     ::testing::Values(
977         make_tuple(&vp9_fht16x16_msa, &vp9_iht16x16_256_add_msa, 0, VPX_BITS_8),
978         make_tuple(&vp9_fht16x16_msa, &vp9_iht16x16_256_add_msa, 1, VPX_BITS_8),
979         make_tuple(&vp9_fht16x16_msa, &vp9_iht16x16_256_add_msa, 2, VPX_BITS_8),
980         make_tuple(&vp9_fht16x16_msa, &vp9_iht16x16_256_add_msa, 3,
981                    VPX_BITS_8)));
982 INSTANTIATE_TEST_CASE_P(MSA, PartialTrans16x16Test,
983                         ::testing::Values(make_tuple(&vpx_fdct16x16_1_msa,
984                                                      VPX_BITS_8)));
985 #endif  // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
986 }  // namespace