2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * 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.
13 #include "test/av1_txfm_test.h"
15 namespace libaom_test
{
17 int get_txfm1d_size(TX_SIZE tx_size
) { return tx_size_wide
[tx_size
]; }
19 void get_txfm1d_type(TX_TYPE txfm2d_type
, TYPE_TXFM
*type0
, TYPE_TXFM
*type1
) {
20 switch (txfm2d_type
) {
45 case FLIPADST_FLIPADST
:
93 double Sqrt2
= pow(2, 0.5);
94 double invSqrt2
= 1 / pow(2, 0.5);
96 double dct_matrix(double n
, double k
, int size
) {
97 return cos(M_PI
* (2 * n
+ 1) * k
/ (2 * size
));
100 void reference_dct_1d(const double *in
, double *out
, int size
) {
101 for (int k
= 0; k
< size
; ++k
) {
103 for (int n
= 0; n
< size
; ++n
) {
104 out
[k
] += in
[n
] * dct_matrix(n
, k
, size
);
106 if (k
== 0) out
[k
] = out
[k
] * invSqrt2
;
110 void reference_idct_1d(const double *in
, double *out
, int size
) {
111 for (int k
= 0; k
< size
; ++k
) {
113 for (int n
= 0; n
< size
; ++n
) {
115 out
[k
] += invSqrt2
* in
[n
] * dct_matrix(k
, n
, size
);
117 out
[k
] += in
[n
] * dct_matrix(k
, n
, size
);
122 // TODO(any): Copied from dct.c. Should be replaced by a proper reference
123 // function that takes 'double' input & output.
124 static 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
;
133 if (!(x0
| x1
| x2
| x3
)) {
134 output
[0] = output
[1] = output
[2] = output
[3] = 0;
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
);
164 void reference_adst_1d(const double *in
, double *out
, int size
) {
165 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
]));
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
];
178 for (int k
= 0; k
< size
; ++k
) {
180 for (int n
= 0; n
< size
; ++n
) {
181 out
[k
] += in
[n
] * sin(M_PI
* (2 * n
+ 1) * (2 * k
+ 1) / (4 * size
));
186 void reference_idtx_1d(const double *in
, double *out
, int size
) {
198 for (int k
= 0; k
< size
; ++k
) {
199 out
[k
] = in
[k
] * scale
;
203 void reference_hybrid_1d(double *in
, double *out
, int size
, int type
) {
204 if (type
== TYPE_DCT
)
205 reference_dct_1d(in
, out
, size
);
206 else if (type
== TYPE_ADST
)
207 reference_adst_1d(in
, out
, size
);
209 reference_idtx_1d(in
, out
, size
);
212 double 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
);
215 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
];
217 const int8_t *shift
= fwd_txfm_flip_cfg
.shift
;
218 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
));
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);
227 return amplify_factor
;
230 void reference_hybrid_2d(double *in
, double *out
, TX_TYPE tx_type
,
232 // Get transform type and size of each dimension.
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
];
239 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
;
244 // 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
];
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
];
256 for (int r
= 0; r
< tx_height
; ++r
) {
257 reference_hybrid_1d(out_interm
+ r
* stride
, out
+ r
* stride
, tx_width
,
265 // 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
268 // TODO(urvang): Refactor this code.
269 if (tx_width
== 64 && tx_height
== 64) { // tx_size == TX_64X64
270 // Zero out top-right 32x32 area.
271 for (int row
= 0; row
< 32; ++row
) {
272 memset(out
+ row
* 64 + 32, 0, 32 * sizeof(*out
));
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
));
280 } 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
));
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
));
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
));
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
));
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
;
317 template <typename Type
>
318 void 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
;
328 template <typename Type
>
329 void 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
;
339 template <typename Type
>
340 void 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
;
350 template void fliplr
<double>(double *dest
, int width
, int height
, int stride
);
351 template void flipud
<double>(double *dest
, int width
, int height
, int stride
);
352 template void fliplrud
<double>(double *dest
, int width
, int height
, int stride
);
354 int bd_arr
[BD_NUM
] = { 8, 10, 12 };
356 int8_t low_range_arr
[BD_NUM
] = { 18, 32, 32 };
357 int8_t high_range_arr
[BD_NUM
] = { 32, 32, 32 };
359 void txfm_stage_range_check(const int8_t *stage_range
, int stage_num
,
360 int8_t cos_bit
, int low_range
, int high_range
) {
361 for (int i
= 0; i
< stage_num
; ++i
) {
362 EXPECT_LE(stage_range
[i
], low_range
);
363 ASSERT_LE(stage_range
[i
] + cos_bit
, high_range
) << "stage = " << i
;
365 for (int i
= 0; i
< stage_num
- 1; ++i
) {
366 // make sure there is no overflow while doing half_btf()
367 ASSERT_LE(stage_range
[i
+ 1] + cos_bit
, high_range
) << "stage = " << i
;
370 } // namespace libaom_test