1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
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.
10 */
11
12 #ifndef AOM_AV1_COMMON_AV1_TXFM_H_
13 #define AOM_AV1_COMMON_AV1_TXFM_H_
14
15 #include <assert.h>
16 #include <math.h>
17 #include <stdio.h>
18
19 #include "config/aom_config.h"
20
21 #include "av1/common/enums.h"
22 #include "av1/common/blockd.h"
23 #include "aom/aom_integer.h"
24 #include "aom_dsp/aom_dsp_common.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #if !defined(DO_RANGE_CHECK_CLAMP)
31 #define DO_RANGE_CHECK_CLAMP 0
32 #endif
33
34 extern const int32_t av1_cospi_arr_data[7][64];
35 extern const int32_t av1_sinpi_arr_data[7][5];
36
37 #define MAX_TXFM_STAGE_NUM 12
38
39 static const int cos_bit_min = 10;
40 static const int cos_bit_max = 16;
41
42 #define NewSqrt2Bits ((int32_t)12)
43 // 2^12 * sqrt(2)
44 static const int32_t NewSqrt2 = 5793;
45 // 2^12 / sqrt(2)
46 static const int32_t NewInvSqrt2 = 2896;
47
cospi_arr(int n)48 static INLINE const int32_t *cospi_arr(int n) {
49 return av1_cospi_arr_data[n - cos_bit_min];
50 }
51
sinpi_arr(int n)52 static INLINE const int32_t *sinpi_arr(int n) {
53 return av1_sinpi_arr_data[n - cos_bit_min];
54 }
55
range_check_value(int32_t value,int8_t bit)56 static INLINE int32_t range_check_value(int32_t value, int8_t bit) {
57 #if CONFIG_COEFFICIENT_RANGE_CHECKING
58 const int64_t max_value = (1LL << (bit - 1)) - 1;
59 const int64_t min_value = -(1LL << (bit - 1));
60 if (value < min_value || value > max_value) {
61 fprintf(stderr, "coeff out of bit range, value: %d bit %d\n", value, bit);
62 assert(0);
63 }
64 #endif // CONFIG_COEFFICIENT_RANGE_CHECKING
65 #if DO_RANGE_CHECK_CLAMP
66 bit = AOMMIN(bit, 31);
67 return clamp(value, -(1 << (bit - 1)), (1 << (bit - 1)) - 1);
68 #endif // DO_RANGE_CHECK_CLAMP
69 (void)bit;
70 return value;
71 }
72
round_shift(int64_t value,int bit)73 static INLINE int32_t round_shift(int64_t value, int bit) {
74 assert(bit >= 1);
75 return (int32_t)((value + (1ll << (bit - 1))) >> bit);
76 }
77
half_btf(int32_t w0,int32_t in0,int32_t w1,int32_t in1,int bit)78 static INLINE int32_t half_btf(int32_t w0, int32_t in0, int32_t w1, int32_t in1,
79 int bit) {
80 int64_t result_64 = (int64_t)(w0 * in0) + (int64_t)(w1 * in1);
81 int64_t intermediate = result_64 + (1LL << (bit - 1));
82 // NOTE(david.barker): The value 'result_64' may not necessarily fit
83 // into 32 bits. However, the result of this function is nominally
84 // ROUND_POWER_OF_TWO_64(result_64, bit)
85 // and that is required to fit into stage_range[stage] many bits
86 // (checked by range_check_buf()).
87 //
88 // Here we've unpacked that rounding operation, and it can be shown
89 // that the value of 'intermediate' here *does* fit into 32 bits
90 // for any conformant bitstream.
91 // The upshot is that, if you do all this calculation using
92 // wrapping 32-bit arithmetic instead of (non-wrapping) 64-bit arithmetic,
93 // then you'll still get the correct result.
94 // To provide a check on this logic, we assert that 'intermediate'
95 // would fit into an int32 if range checking is enabled.
96 #if CONFIG_COEFFICIENT_RANGE_CHECKING
97 assert(intermediate >= INT32_MIN && intermediate <= INT32_MAX);
98 #endif
99 return (int32_t)(intermediate >> bit);
100 }
101
highbd_clip_pixel_add(uint16_t dest,tran_high_t trans,int bd)102 static INLINE uint16_t highbd_clip_pixel_add(uint16_t dest, tran_high_t trans,
103 int bd) {
104 return clip_pixel_highbd(dest + (int)trans, bd);
105 }
106
107 typedef void (*TxfmFunc)(const int32_t *input, int32_t *output, int8_t cos_bit,
108 const int8_t *stage_range);
109
110 typedef void (*FwdTxfm2dFunc)(const int16_t *input, int32_t *output, int stride,
111 TX_TYPE tx_type, int bd);
112
113 typedef enum TXFM_TYPE {
114 TXFM_TYPE_DCT4,
115 TXFM_TYPE_DCT8,
116 TXFM_TYPE_DCT16,
117 TXFM_TYPE_DCT32,
118 TXFM_TYPE_DCT64,
119 TXFM_TYPE_ADST4,
120 TXFM_TYPE_ADST8,
121 TXFM_TYPE_ADST16,
122 TXFM_TYPE_IDENTITY4,
123 TXFM_TYPE_IDENTITY8,
124 TXFM_TYPE_IDENTITY16,
125 TXFM_TYPE_IDENTITY32,
126 TXFM_TYPES,
127 TXFM_TYPE_INVALID,
128 } TXFM_TYPE;
129
130 typedef struct TXFM_2D_FLIP_CFG {
131 TX_SIZE tx_size;
132 int ud_flip; // flip upside down
133 int lr_flip; // flip left to right
134 const int8_t *shift;
135 int8_t cos_bit_col;
136 int8_t cos_bit_row;
137 int8_t stage_range_col[MAX_TXFM_STAGE_NUM];
138 int8_t stage_range_row[MAX_TXFM_STAGE_NUM];
139 TXFM_TYPE txfm_type_col;
140 TXFM_TYPE txfm_type_row;
141 int stage_num_col;
142 int stage_num_row;
143 } TXFM_2D_FLIP_CFG;
144
get_flip_cfg(TX_TYPE tx_type,int * ud_flip,int * lr_flip)145 static INLINE void get_flip_cfg(TX_TYPE tx_type, int *ud_flip, int *lr_flip) {
146 switch (tx_type) {
147 case DCT_DCT:
148 case ADST_DCT:
149 case DCT_ADST:
150 case ADST_ADST:
151 *ud_flip = 0;
152 *lr_flip = 0;
153 break;
154 case IDTX:
155 case V_DCT:
156 case H_DCT:
157 case V_ADST:
158 case H_ADST:
159 *ud_flip = 0;
160 *lr_flip = 0;
161 break;
162 case FLIPADST_DCT:
163 case FLIPADST_ADST:
164 case V_FLIPADST:
165 *ud_flip = 1;
166 *lr_flip = 0;
167 break;
168 case DCT_FLIPADST:
169 case ADST_FLIPADST:
170 case H_FLIPADST:
171 *ud_flip = 0;
172 *lr_flip = 1;
173 break;
174 case FLIPADST_FLIPADST:
175 *ud_flip = 1;
176 *lr_flip = 1;
177 break;
178 default:
179 *ud_flip = 0;
180 *lr_flip = 0;
181 assert(0);
182 }
183 }
184
set_flip_cfg(TX_TYPE tx_type,TXFM_2D_FLIP_CFG * cfg)185 static INLINE void set_flip_cfg(TX_TYPE tx_type, TXFM_2D_FLIP_CFG *cfg) {
186 get_flip_cfg(tx_type, &cfg->ud_flip, &cfg->lr_flip);
187 }
188
189 // Utility function that returns the log of the ratio of the col and row
190 // sizes.
get_rect_tx_log_ratio(int col,int row)191 static INLINE int get_rect_tx_log_ratio(int col, int row) {
192 if (col == row) return 0;
193 if (col > row) {
194 if (col == row * 2) return 1;
195 if (col == row * 4) return 2;
196 assert(0 && "Unsupported transform size");
197 } else {
198 if (row == col * 2) return -1;
199 if (row == col * 4) return -2;
200 assert(0 && "Unsupported transform size");
201 }
202 return 0; // Invalid
203 }
204
205 void av1_gen_fwd_stage_range(int8_t *stage_range_col, int8_t *stage_range_row,
206 const TXFM_2D_FLIP_CFG *cfg, int bd);
207
208 void av1_gen_inv_stage_range(int8_t *stage_range_col, int8_t *stage_range_row,
209 const TXFM_2D_FLIP_CFG *cfg, TX_SIZE tx_size,
210 int bd);
211
212 void av1_get_fwd_txfm_cfg(TX_TYPE tx_type, TX_SIZE tx_size,
213 TXFM_2D_FLIP_CFG *cfg);
214 void av1_get_inv_txfm_cfg(TX_TYPE tx_type, TX_SIZE tx_size,
215 TXFM_2D_FLIP_CFG *cfg);
216 extern const TXFM_TYPE av1_txfm_type_ls[5][TX_TYPES_1D];
217 extern const int8_t av1_txfm_stage_num_list[TXFM_TYPES];
get_txw_idx(TX_SIZE tx_size)218 static INLINE int get_txw_idx(TX_SIZE tx_size) {
219 return tx_size_wide_log2[tx_size] - tx_size_wide_log2[0];
220 }
get_txh_idx(TX_SIZE tx_size)221 static INLINE int get_txh_idx(TX_SIZE tx_size) {
222 return tx_size_high_log2[tx_size] - tx_size_high_log2[0];
223 }
224
225 void av1_range_check_buf(int32_t stage, const int32_t *input,
226 const int32_t *buf, int32_t size, int8_t bit);
227 #define MAX_TXWH_IDX 5
228 #ifdef __cplusplus
229 }
230 #endif // __cplusplus
231
232 #endif // AOM_AV1_COMMON_AV1_TXFM_H_
233