blob: c5688642fa0c30de27ffa5f60fd88d8f3404b801 [file] [log] [blame]
Angie Chiang716f1bd2016-05-11 16:41:36 -07001/*
Yaowu Xubde4ac82016-11-28 15:26:06 -08002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Angie Chiang716f1bd2016-05-11 16:41:36 -07003 *
Yaowu Xubde4ac82016-11-28 15:26:06 -08004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Angie Chiang716f1bd2016-05-11 16:41:36 -070010 */
11
12#include <stdio.h>
Yaowu Xuf883b422016-08-30 14:01:10 -070013#include "test/av1_txfm_test.h"
Angie Chiang716f1bd2016-05-11 16:41:36 -070014
Yaowu Xuc27fc142016-08-22 16:08:15 -070015namespace libaom_test {
Angie Chiang716f1bd2016-05-11 16:41:36 -070016
Jingning Han7e32a4c2017-04-12 10:43:17 -070017int get_txfm1d_size(TX_SIZE tx_size) { return tx_size_wide[tx_size]; }
Angie Chiang716f1bd2016-05-11 16:41:36 -070018
clang-format3a826f12016-08-11 17:46:05 -070019void get_txfm1d_type(TX_TYPE txfm2d_type, TYPE_TXFM *type0, TYPE_TXFM *type1) {
Angie Chiang716f1bd2016-05-11 16:41:36 -070020 switch (txfm2d_type) {
21 case DCT_DCT:
22 *type0 = TYPE_DCT;
23 *type1 = TYPE_DCT;
24 break;
25 case ADST_DCT:
26 *type0 = TYPE_ADST;
27 *type1 = TYPE_DCT;
28 break;
29 case DCT_ADST:
30 *type0 = TYPE_DCT;
31 *type1 = TYPE_ADST;
32 break;
33 case ADST_ADST:
34 *type0 = TYPE_ADST;
35 *type1 = TYPE_ADST;
36 break;
Angie Chiang6a752532016-05-11 18:50:47 -070037 case FLIPADST_DCT:
38 *type0 = TYPE_ADST;
39 *type1 = TYPE_DCT;
40 break;
41 case DCT_FLIPADST:
42 *type0 = TYPE_DCT;
43 *type1 = TYPE_ADST;
44 break;
45 case FLIPADST_FLIPADST:
46 *type0 = TYPE_ADST;
47 *type1 = TYPE_ADST;
48 break;
49 case ADST_FLIPADST:
50 *type0 = TYPE_ADST;
51 *type1 = TYPE_ADST;
52 break;
53 case FLIPADST_ADST:
54 *type0 = TYPE_ADST;
55 *type1 = TYPE_ADST;
56 break;
Debargha Mukherjeeaa84f3e2018-01-04 12:45:10 -080057 case IDTX:
58 *type0 = TYPE_IDTX;
59 *type1 = TYPE_IDTX;
60 break;
61 case H_DCT:
62 *type0 = TYPE_IDTX;
63 *type1 = TYPE_DCT;
64 break;
65 case V_DCT:
66 *type0 = TYPE_DCT;
67 *type1 = TYPE_IDTX;
68 break;
69 case H_ADST:
70 *type0 = TYPE_IDTX;
71 *type1 = TYPE_ADST;
72 break;
73 case V_ADST:
74 *type0 = TYPE_ADST;
75 *type1 = TYPE_IDTX;
76 break;
77 case H_FLIPADST:
78 *type0 = TYPE_IDTX;
79 *type1 = TYPE_ADST;
80 break;
81 case V_FLIPADST:
82 *type0 = TYPE_ADST;
83 *type1 = TYPE_IDTX;
84 break;
Angie Chiang716f1bd2016-05-11 16:41:36 -070085 default:
86 *type0 = TYPE_DCT;
87 *type1 = TYPE_DCT;
88 assert(0);
89 break;
90 }
91}
92
Debargha Mukherjeeaa84f3e2018-01-04 12:45:10 -080093double Sqrt2 = pow(2, 0.5);
Angie Chiang716f1bd2016-05-11 16:41:36 -070094double invSqrt2 = 1 / pow(2, 0.5);
95
Sebastien Alaiwanbdb7e9c2017-07-10 12:58:30 +020096double dct_matrix(double n, double k, int size) {
97 return cos(M_PI * (2 * n + 1) * k / (2 * size));
98}
99
clang-format3a826f12016-08-11 17:46:05 -0700100void reference_dct_1d(const double *in, double *out, int size) {
Angie Chiang716f1bd2016-05-11 16:41:36 -0700101 for (int k = 0; k < size; ++k) {
102 out[k] = 0;
103 for (int n = 0; n < size; ++n) {
Sebastien Alaiwanbdb7e9c2017-07-10 12:58:30 +0200104 out[k] += in[n] * dct_matrix(n, k, size);
Angie Chiang716f1bd2016-05-11 16:41:36 -0700105 }
106 if (k == 0) out[k] = out[k] * invSqrt2;
107 }
108}
109
Sebastien Alaiwand02642f2017-07-10 11:41:44 +0200110void reference_idct_1d(const double *in, double *out, int size) {
Sebastien Alaiwanbdb7e9c2017-07-10 12:58:30 +0200111 for (int k = 0; k < size; ++k) {
112 out[k] = 0;
113 for (int n = 0; n < size; ++n) {
114 if (n == 0)
115 out[k] += invSqrt2 * in[n] * dct_matrix(k, n, size);
Sebastien Alaiwand02642f2017-07-10 11:41:44 +0200116 else
Sebastien Alaiwanbdb7e9c2017-07-10 12:58:30 +0200117 out[k] += in[n] * dct_matrix(k, n, size);
Sebastien Alaiwand02642f2017-07-10 11:41:44 +0200118 }
119 }
120}
121
Sarah Parker95f52602017-10-04 12:45:14 -0700122// TODO(any): Copied from dct.c. Should be replaced by a proper reference
123// function that takes 'double' input & output.
124static void fadst4(const tran_low_t *input, tran_low_t *output) {
125 tran_high_t x0, x1, x2, x3;
126 tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;
127
128 x0 = input[0];
129 x1 = input[1];
130 x2 = input[2];
131 x3 = input[3];
132
133 if (!(x0 | x1 | x2 | x3)) {
134 output[0] = output[1] = output[2] = output[3] = 0;
135 return;
136 }
137
138 s0 = sinpi_1_9 * x0;
139 s1 = sinpi_4_9 * x0;
140 s2 = sinpi_2_9 * x1;
141 s3 = sinpi_1_9 * x1;
142 s4 = sinpi_3_9 * x2;
143 s5 = sinpi_4_9 * x3;
144 s6 = sinpi_2_9 * x3;
145 s7 = x0 + x1 - x3;
146
147 x0 = s0 + s2 + s5;
148 x1 = sinpi_3_9 * s7;
149 x2 = s1 - s3 + s6;
150 x3 = s4;
151
152 s0 = x0 + x3;
153 s1 = x1;
154 s2 = x2 - x3;
155 s3 = x2 - x0 + x3;
156
157 // 1-D transform scaling factor is sqrt(2).
158 output[0] = (tran_low_t)fdct_round_shift(s0);
159 output[1] = (tran_low_t)fdct_round_shift(s1);
160 output[2] = (tran_low_t)fdct_round_shift(s2);
161 output[3] = (tran_low_t)fdct_round_shift(s3);
162}
163
clang-format3a826f12016-08-11 17:46:05 -0700164void reference_adst_1d(const double *in, double *out, int size) {
Sarah Parker95f52602017-10-04 12:45:14 -0700165 if (size == 4) { // Special case.
166 tran_low_t int_input[4];
167 for (int i = 0; i < 4; ++i) {
168 int_input[i] = static_cast<tran_low_t>(round(in[i]));
169 }
170 tran_low_t int_output[4];
171 fadst4(int_input, int_output);
172 for (int i = 0; i < 4; ++i) {
173 out[i] = int_output[i];
174 }
175 return;
176 }
177
Angie Chiang716f1bd2016-05-11 16:41:36 -0700178 for (int k = 0; k < size; ++k) {
179 out[k] = 0;
180 for (int n = 0; n < size; ++n) {
181 out[k] += in[n] * sin(M_PI * (2 * n + 1) * (2 * k + 1) / (4 * size));
182 }
183 }
184}
185
Debargha Mukherjeeaa84f3e2018-01-04 12:45:10 -0800186void reference_idtx_1d(const double *in, double *out, int size) {
187 double scale = 0;
188 if (size == 4)
189 scale = Sqrt2;
190 else if (size == 8)
191 scale = 2;
192 else if (size == 16)
193 scale = 2 * Sqrt2;
194 else if (size == 32)
195 scale = 4;
196 else if (size == 64)
197 scale = 4 * Sqrt2;
198 for (int k = 0; k < size; ++k) {
199 out[k] = in[k] * scale;
200 }
201}
202
clang-format3a826f12016-08-11 17:46:05 -0700203void reference_hybrid_1d(double *in, double *out, int size, int type) {
Angie Chiang716f1bd2016-05-11 16:41:36 -0700204 if (type == TYPE_DCT)
205 reference_dct_1d(in, out, size);
Debargha Mukherjeeaa84f3e2018-01-04 12:45:10 -0800206 else if (type == TYPE_ADST)
Angie Chiang716f1bd2016-05-11 16:41:36 -0700207 reference_adst_1d(in, out, size);
Debargha Mukherjeeaa84f3e2018-01-04 12:45:10 -0800208 else
209 reference_idtx_1d(in, out, size);
Angie Chiang716f1bd2016-05-11 16:41:36 -0700210}
211
Urvang Joshiec6acb22017-12-13 18:54:51 -0800212double get_amplification_factor(TX_TYPE tx_type, TX_SIZE tx_size) {
213 TXFM_2D_FLIP_CFG fwd_txfm_flip_cfg;
214 av1_get_fwd_txfm_cfg(tx_type, tx_size, &fwd_txfm_flip_cfg);
Angie Chiang0c7b8d82018-01-23 19:20:44 -0800215 const int tx_width = tx_size_wide[fwd_txfm_flip_cfg.tx_size];
216 const int tx_height = tx_size_high[fwd_txfm_flip_cfg.tx_size];
Angie Chiang4a75b5a2018-01-10 17:19:06 -0800217 const int8_t *shift = fwd_txfm_flip_cfg.shift;
Urvang Joshiec6acb22017-12-13 18:54:51 -0800218 const int amplify_bit = shift[0] + shift[1] + shift[2];
219 double amplify_factor =
220 amplify_bit >= 0 ? (1 << amplify_bit) : (1.0 / (1 << -amplify_bit));
221
222 // For rectangular transforms, we need to multiply by an extra factor.
223 const int rect_type = get_rect_tx_log_ratio(tx_width, tx_height);
224 if (abs(rect_type) == 1) {
225 amplify_factor *= pow(2, 0.5);
Urvang Joshiec6acb22017-12-13 18:54:51 -0800226 }
227 return amplify_factor;
228}
229
230void reference_hybrid_2d(double *in, double *out, TX_TYPE tx_type,
231 TX_SIZE tx_size) {
232 // Get transform type and size of each dimension.
233 TYPE_TXFM type0;
234 TYPE_TXFM type1;
235 get_txfm1d_type(tx_type, &type0, &type1);
236 const int tx_width = tx_size_wide[tx_size];
237 const int tx_height = tx_size_high[tx_size];
238
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800239 double *const temp_in = new double[AOMMAX(tx_width, tx_height)];
240 double *const temp_out = new double[AOMMAX(tx_width, tx_height)];
241 double *const out_interm = new double[tx_width * tx_height];
242 const int stride = tx_width;
Angie Chiang716f1bd2016-05-11 16:41:36 -0700243
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800244 // Transform columns.
245 for (int c = 0; c < tx_width; ++c) {
246 for (int r = 0; r < tx_height; ++r) {
247 temp_in[r] = in[r * stride + c];
248 }
249 reference_hybrid_1d(temp_in, temp_out, tx_height, type0);
250 for (int r = 0; r < tx_height; ++r) {
251 out_interm[r * stride + c] = temp_out[r];
Angie Chiang716f1bd2016-05-11 16:41:36 -0700252 }
253 }
254
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800255 // Transform rows.
256 for (int r = 0; r < tx_height; ++r) {
257 reference_hybrid_1d(out_interm + r * stride, out + r * stride, tx_width,
258 type1);
Angie Chiang716f1bd2016-05-11 16:41:36 -0700259 }
260
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800261 delete[] temp_in;
262 delete[] temp_out;
263 delete[] out_interm;
Urvang Joshi72359922017-12-11 17:17:40 -0800264
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800265 // These transforms use an approximate 2D DCT transform, by only keeping the
266 // top-left quarter of the coefficients, and repacking them in the first
267 // quarter indices.
268 // TODO(urvang): Refactor this code.
269 if (tx_width == 64 && tx_height == 64) { // tx_size == TX_64X64
Urvang Joshi72359922017-12-11 17:17:40 -0800270 // Zero out top-right 32x32 area.
271 for (int row = 0; row < 32; ++row) {
272 memset(out + row * 64 + 32, 0, 32 * sizeof(*out));
273 }
274 // Zero out the bottom 64x32 area.
275 memset(out + 32 * 64, 0, 32 * 64 * sizeof(*out));
276 // Re-pack non-zero coeffs in the first 32x32 indices.
277 for (int row = 1; row < 32; ++row) {
278 memcpy(out + row * 32, out + row * 64, 32 * sizeof(*out));
279 }
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800280 } else if (tx_width == 32 && tx_height == 64) { // tx_size == TX_32X64
281 // Zero out the bottom 32x32 area.
282 memset(out + 32 * 32, 0, 32 * 32 * sizeof(*out));
283 // Note: no repacking needed here.
284 } else if (tx_width == 64 && tx_height == 32) { // tx_size == TX_64X32
285 // Zero out right 32x32 area.
286 for (int row = 0; row < 32; ++row) {
287 memset(out + row * 64 + 32, 0, 32 * sizeof(*out));
288 }
289 // Re-pack non-zero coeffs in the first 32x32 indices.
290 for (int row = 1; row < 32; ++row) {
291 memcpy(out + row * 32, out + row * 64, 32 * sizeof(*out));
292 }
293 } else if (tx_width == 16 && tx_height == 64) { // tx_size == TX_16X64
294 // Zero out the bottom 16x32 area.
295 memset(out + 16 * 32, 0, 16 * 32 * sizeof(*out));
296 // Note: no repacking needed here.
297 } else if (tx_width == 64 && tx_height == 16) { // tx_size == TX_64X16
298 // Zero out right 32x16 area.
299 for (int row = 0; row < 16; ++row) {
300 memset(out + row * 64 + 32, 0, 32 * sizeof(*out));
301 }
302 // Re-pack non-zero coeffs in the first 32x16 indices.
303 for (int row = 1; row < 16; ++row) {
304 memcpy(out + row * 32, out + row * 64, 32 * sizeof(*out));
305 }
Urvang Joshi72359922017-12-11 17:17:40 -0800306 }
Urvang Joshiec6acb22017-12-13 18:54:51 -0800307
308 // Apply appropriate scale.
309 const double amplify_factor = get_amplification_factor(tx_type, tx_size);
310 for (int c = 0; c < tx_width; ++c) {
311 for (int r = 0; r < tx_height; ++r) {
312 out[r * stride + c] *= amplify_factor;
313 }
314 }
Angie Chiang716f1bd2016-05-11 16:41:36 -0700315}
Angie Chiang6a752532016-05-11 18:50:47 -0700316
clang-format3a826f12016-08-11 17:46:05 -0700317template <typename Type>
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800318void fliplr(Type *dest, int width, int height, int stride) {
319 for (int r = 0; r < height; ++r) {
320 for (int c = 0; c < width / 2; ++c) {
321 const Type tmp = dest[r * stride + c];
322 dest[r * stride + c] = dest[r * stride + width - 1 - c];
323 dest[r * stride + width - 1 - c] = tmp;
Angie Chiang6a752532016-05-11 18:50:47 -0700324 }
325 }
326}
327
clang-format3a826f12016-08-11 17:46:05 -0700328template <typename Type>
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800329void flipud(Type *dest, int width, int height, int stride) {
330 for (int c = 0; c < width; ++c) {
331 for (int r = 0; r < height / 2; ++r) {
332 const Type tmp = dest[r * stride + c];
333 dest[r * stride + c] = dest[(height - 1 - r) * stride + c];
334 dest[(height - 1 - r) * stride + c] = tmp;
Angie Chiang6a752532016-05-11 18:50:47 -0700335 }
336 }
337}
338
clang-format3a826f12016-08-11 17:46:05 -0700339template <typename Type>
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800340void fliplrud(Type *dest, int width, int height, int stride) {
341 for (int r = 0; r < height / 2; ++r) {
342 for (int c = 0; c < width; ++c) {
343 const Type tmp = dest[r * stride + c];
344 dest[r * stride + c] = dest[(height - 1 - r) * stride + width - 1 - c];
345 dest[(height - 1 - r) * stride + width - 1 - c] = tmp;
Angie Chiang6a752532016-05-11 18:50:47 -0700346 }
347 }
348}
349
Urvang Joshi5fb50f82017-12-12 18:48:55 -0800350template void fliplr<double>(double *dest, int width, int height, int stride);
351template void flipud<double>(double *dest, int width, int height, int stride);
352template void fliplrud<double>(double *dest, int width, int height, int stride);
Angie Chiang6a752532016-05-11 18:50:47 -0700353
Angie Chiang9c7089a2017-08-08 16:21:11 -0700354int bd_arr[BD_NUM] = { 8, 10, 12 };
Urvang Joshiab2b36e2017-10-03 11:01:06 -0700355
Urvang Joshiab2b36e2017-10-03 11:01:06 -0700356int8_t low_range_arr[BD_NUM] = { 18, 32, 32 };
Urvang Joshi09191ca2017-11-14 17:10:25 -0800357int8_t high_range_arr[BD_NUM] = { 32, 32, 32 };
Angie Chiang9c7089a2017-08-08 16:21:11 -0700358
359void txfm_stage_range_check(const int8_t *stage_range, int stage_num,
Angie Chiangd4327bc2018-01-22 20:54:04 -0800360 int8_t cos_bit, int low_range, int high_range) {
Angie Chiang9c7089a2017-08-08 16:21:11 -0700361 for (int i = 0; i < stage_num; ++i) {
362 EXPECT_LE(stage_range[i], low_range);
Angie Chiangd4327bc2018-01-22 20:54:04 -0800363 ASSERT_LE(stage_range[i] + cos_bit, high_range) << "stage = " << i;
Angie Chiang9c7089a2017-08-08 16:21:11 -0700364 }
365 for (int i = 0; i < stage_num - 1; ++i) {
366 // make sure there is no overflow while doing half_btf()
Angie Chiangd4327bc2018-01-22 20:54:04 -0800367 ASSERT_LE(stage_range[i + 1] + cos_bit, high_range) << "stage = " << i;
Angie Chiang9c7089a2017-08-08 16:21:11 -0700368 }
369}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700370} // namespace libaom_test