Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Krishna Rapaka | 7319db5 | 2021-09-28 20:35:29 -0700 | [diff] [blame] | 2 | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Vibhoothi | 41c6dd7 | 2021-10-12 18:48:26 +0000 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 3-Clause Clear License |
| 5 | * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
| 6 | * License was not distributed with this source code in the LICENSE file, you |
| 7 | * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the |
| 8 | * Alliance for Open Media Patent License 1.0 was not distributed with this |
| 9 | * source code in the PATENTS file, you can obtain it at |
| 10 | * aomedia.org/license/patent-license/. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11 | */ |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 12 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 13 | #ifndef AOM_AV1_COMMON_AV1_TXFM_H_ |
| 14 | #define AOM_AV1_COMMON_AV1_TXFM_H_ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 15 | |
| 16 | #include <assert.h> |
| 17 | #include <math.h> |
| 18 | #include <stdio.h> |
| 19 | |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 20 | #include "config/aom_config.h" |
| 21 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 22 | #include "av1/common/enums.h" |
Angie Chiang | 155bf9a | 2017-08-06 19:52:57 -0700 | [diff] [blame] | 23 | #include "av1/common/blockd.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 24 | #include "aom/aom_integer.h" |
| 25 | #include "aom_dsp/aom_dsp_common.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 26 | |
Angie Chiang | 9c7089a | 2017-08-08 16:21:11 -0700 | [diff] [blame] | 27 | #ifdef __cplusplus |
| 28 | extern "C" { |
| 29 | #endif |
| 30 | |
Hui Su | 8d95019 | 2018-04-27 15:47:23 -0700 | [diff] [blame] | 31 | #if !defined(DO_RANGE_CHECK_CLAMP) |
| 32 | #define DO_RANGE_CHECK_CLAMP 0 |
| 33 | #endif |
| 34 | |
Yue Chen | 5b53ea1 | 2018-03-09 11:26:43 -0800 | [diff] [blame] | 35 | extern const int32_t av1_cospi_arr_data[7][64]; |
| 36 | extern const int32_t av1_sinpi_arr_data[7][5]; |
| 37 | |
Lester Lu | 68b107b | 2022-12-20 21:24:24 +0000 | [diff] [blame] | 38 | #if CONFIG_CROSS_CHROMA_TX |
| 39 | #define CCTX_PREC_BITS 8 |
| 40 | extern const int32_t cctx_mtx[CCTX_TYPES - 1][2]; |
| 41 | #endif // CONFIG_CROSS_CHROMA_TX |
| 42 | |
Angie Chiang | ce3ad28 | 2017-08-08 09:51:54 -0700 | [diff] [blame] | 43 | #define MAX_TXFM_STAGE_NUM 12 |
| 44 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 45 | static const int cos_bit_min = 10; |
| 46 | static const int cos_bit_max = 16; |
| 47 | |
Martin Storsjo | 01c165a | 2018-07-28 22:11:04 +0300 | [diff] [blame] | 48 | #define NewSqrt2Bits ((int32_t)12) |
Angie Chiang | c8d6c08 | 2018-02-06 14:26:55 -0800 | [diff] [blame] | 49 | // 2^12 * sqrt(2) |
| 50 | static const int32_t NewSqrt2 = 5793; |
| 51 | // 2^12 / sqrt(2) |
| 52 | static const int32_t NewInvSqrt2 = 2896; |
| 53 | |
Frederic Barbier | bbf7bb0 | 2017-05-05 09:37:40 +0200 | [diff] [blame] | 54 | static INLINE const int32_t *cospi_arr(int n) { |
Yue Chen | 5b53ea1 | 2018-03-09 11:26:43 -0800 | [diff] [blame] | 55 | return av1_cospi_arr_data[n - cos_bit_min]; |
Frederic Barbier | bbf7bb0 | 2017-05-05 09:37:40 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Angie Chiang | 8251736 | 2018-01-17 17:31:54 -0800 | [diff] [blame] | 58 | static INLINE const int32_t *sinpi_arr(int n) { |
Yue Chen | 5b53ea1 | 2018-03-09 11:26:43 -0800 | [diff] [blame] | 59 | return av1_sinpi_arr_data[n - cos_bit_min]; |
Angie Chiang | 8251736 | 2018-01-17 17:31:54 -0800 | [diff] [blame] | 60 | } |
| 61 | |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 62 | static INLINE int32_t range_check_value(int32_t value, int8_t bit) { |
| 63 | #if CONFIG_COEFFICIENT_RANGE_CHECKING |
James Zern | b785b95 | 2018-02-01 19:27:09 -0800 | [diff] [blame] | 64 | const int64_t max_value = (1LL << (bit - 1)) - 1; |
| 65 | const int64_t min_value = -(1LL << (bit - 1)); |
| 66 | if (value < min_value || value > max_value) { |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 67 | fprintf(stderr, "coeff out of bit range, value: %d bit %d\n", value, bit); |
Yaowu Xu | e7261ac | 2019-03-15 09:21:33 -0700 | [diff] [blame] | 68 | #if !CONFIG_AV1_ENCODER |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 69 | assert(0); |
Yaowu Xu | e7261ac | 2019-03-15 09:21:33 -0700 | [diff] [blame] | 70 | #endif |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 71 | } |
Hui Su | 8d95019 | 2018-04-27 15:47:23 -0700 | [diff] [blame] | 72 | #endif // CONFIG_COEFFICIENT_RANGE_CHECKING |
| 73 | #if DO_RANGE_CHECK_CLAMP |
| 74 | bit = AOMMIN(bit, 31); |
Angie Chiang | 66b7d7b | 2018-07-25 14:57:17 -0700 | [diff] [blame] | 75 | return clamp(value, -(1 << (bit - 1)), (1 << (bit - 1)) - 1); |
Hui Su | 8d95019 | 2018-04-27 15:47:23 -0700 | [diff] [blame] | 76 | #endif // DO_RANGE_CHECK_CLAMP |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 77 | (void)bit; |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 78 | return value; |
| 79 | } |
| 80 | |
Hui Su | 6d0fdad | 2018-03-12 10:46:40 -0700 | [diff] [blame] | 81 | static INLINE int32_t round_shift(int64_t value, int bit) { |
Yaowu Xu | 637590c | 2016-11-16 15:15:46 -0800 | [diff] [blame] | 82 | assert(bit >= 1); |
Hui Su | 6d0fdad | 2018-03-12 10:46:40 -0700 | [diff] [blame] | 83 | return (int32_t)((value + (1ll << (bit - 1))) >> bit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 86 | static INLINE int32_t half_btf(int32_t w0, int32_t in0, int32_t w1, int32_t in1, |
| 87 | int bit) { |
Angie Chiang | d1c222d | 2018-02-28 19:41:01 -0800 | [diff] [blame] | 88 | int64_t result_64 = (int64_t)(w0 * in0) + (int64_t)(w1 * in1); |
David Barker | 6f33c68 | 2018-09-12 17:18:47 +0100 | [diff] [blame] | 89 | int64_t intermediate = result_64 + (1LL << (bit - 1)); |
Rachel Barker | 8bde145 | 2021-12-16 18:13:47 +0000 | [diff] [blame] | 90 | // NOTE(rachelbarker): The value 'result_64' may not necessarily fit |
David Barker | 6f33c68 | 2018-09-12 17:18:47 +0100 | [diff] [blame] | 91 | // into 32 bits. However, the result of this function is nominally |
| 92 | // ROUND_POWER_OF_TWO_64(result_64, bit) |
| 93 | // and that is required to fit into stage_range[stage] many bits |
| 94 | // (checked by range_check_buf()). |
| 95 | // |
| 96 | // Here we've unpacked that rounding operation, and it can be shown |
| 97 | // that the value of 'intermediate' here *does* fit into 32 bits |
| 98 | // for any conformant bitstream. |
| 99 | // The upshot is that, if you do all this calculation using |
| 100 | // wrapping 32-bit arithmetic instead of (non-wrapping) 64-bit arithmetic, |
| 101 | // then you'll still get the correct result. |
| 102 | // To provide a check on this logic, we assert that 'intermediate' |
| 103 | // would fit into an int32 if range checking is enabled. |
Angie Chiang | d1c222d | 2018-02-28 19:41:01 -0800 | [diff] [blame] | 104 | #if CONFIG_COEFFICIENT_RANGE_CHECKING |
David Barker | 6f33c68 | 2018-09-12 17:18:47 +0100 | [diff] [blame] | 105 | assert(intermediate >= INT32_MIN && intermediate <= INT32_MAX); |
Angie Chiang | d1c222d | 2018-02-28 19:41:01 -0800 | [diff] [blame] | 106 | #endif |
David Barker | 6f33c68 | 2018-09-12 17:18:47 +0100 | [diff] [blame] | 107 | return (int32_t)(intermediate >> bit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Angie Chiang | 8c489a8 | 2018-05-15 16:07:30 -0700 | [diff] [blame] | 110 | static INLINE uint16_t highbd_clip_pixel_add(uint16_t dest, tran_high_t trans, |
| 111 | int bd) { |
| 112 | return clip_pixel_highbd(dest + (int)trans, bd); |
| 113 | } |
| 114 | |
Angie Chiang | d4327bc | 2018-01-22 20:54:04 -0800 | [diff] [blame] | 115 | typedef void (*TxfmFunc)(const int32_t *input, int32_t *output, int8_t cos_bit, |
| 116 | const int8_t *stage_range); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 117 | |
Peng Bin | 9f04180 | 2018-04-09 13:16:26 +0800 | [diff] [blame] | 118 | typedef void (*FwdTxfm2dFunc)(const int16_t *input, int32_t *output, int stride, |
| 119 | TX_TYPE tx_type, int bd); |
| 120 | |
Satish Kumar Suman | 4667aa1 | 2018-12-14 18:28:19 +0530 | [diff] [blame] | 121 | enum { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 122 | TXFM_TYPE_DCT4, |
| 123 | TXFM_TYPE_DCT8, |
| 124 | TXFM_TYPE_DCT16, |
| 125 | TXFM_TYPE_DCT32, |
| 126 | TXFM_TYPE_DCT64, |
| 127 | TXFM_TYPE_ADST4, |
| 128 | TXFM_TYPE_ADST8, |
| 129 | TXFM_TYPE_ADST16, |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 130 | TXFM_TYPE_IDENTITY4, |
| 131 | TXFM_TYPE_IDENTITY8, |
| 132 | TXFM_TYPE_IDENTITY16, |
| 133 | TXFM_TYPE_IDENTITY32, |
Angie Chiang | 0c7b8d8 | 2018-01-23 19:20:44 -0800 | [diff] [blame] | 134 | TXFM_TYPES, |
| 135 | TXFM_TYPE_INVALID, |
Satish Kumar Suman | 4667aa1 | 2018-12-14 18:28:19 +0530 | [diff] [blame] | 136 | } UENUM1BYTE(TXFM_TYPE); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 137 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 138 | typedef struct TXFM_2D_FLIP_CFG { |
Angie Chiang | 0c7b8d8 | 2018-01-23 19:20:44 -0800 | [diff] [blame] | 139 | TX_SIZE tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 140 | int ud_flip; // flip upside down |
| 141 | int lr_flip; // flip left to right |
Angie Chiang | 4a75b5a | 2018-01-10 17:19:06 -0800 | [diff] [blame] | 142 | const int8_t *shift; |
Angie Chiang | d4327bc | 2018-01-22 20:54:04 -0800 | [diff] [blame] | 143 | int8_t cos_bit_col; |
| 144 | int8_t cos_bit_row; |
Angie Chiang | 0c7b8d8 | 2018-01-23 19:20:44 -0800 | [diff] [blame] | 145 | int8_t stage_range_col[MAX_TXFM_STAGE_NUM]; |
| 146 | int8_t stage_range_row[MAX_TXFM_STAGE_NUM]; |
| 147 | TXFM_TYPE txfm_type_col; |
| 148 | TXFM_TYPE txfm_type_row; |
| 149 | int stage_num_col; |
| 150 | int stage_num_row; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 151 | } TXFM_2D_FLIP_CFG; |
| 152 | |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 153 | static INLINE void get_flip_cfg(TX_TYPE tx_type, int *ud_flip, int *lr_flip) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 154 | switch (tx_type) { |
| 155 | case DCT_DCT: |
| 156 | case ADST_DCT: |
| 157 | case DCT_ADST: |
| 158 | case ADST_ADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 159 | *ud_flip = 0; |
| 160 | *lr_flip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 161 | break; |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 162 | case IDTX: |
| 163 | case V_DCT: |
| 164 | case H_DCT: |
| 165 | case V_ADST: |
| 166 | case H_ADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 167 | *ud_flip = 0; |
| 168 | *lr_flip = 0; |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 169 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 170 | case FLIPADST_DCT: |
Sarah Parker | eec47e6 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 171 | case FLIPADST_ADST: |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 172 | case V_FLIPADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 173 | *ud_flip = 1; |
| 174 | *lr_flip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 175 | break; |
| 176 | case DCT_FLIPADST: |
Sarah Parker | eec47e6 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 177 | case ADST_FLIPADST: |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 178 | case H_FLIPADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 179 | *ud_flip = 0; |
| 180 | *lr_flip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 181 | break; |
| 182 | case FLIPADST_FLIPADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 183 | *ud_flip = 1; |
| 184 | *lr_flip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 185 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 186 | default: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 187 | *ud_flip = 0; |
| 188 | *lr_flip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 189 | assert(0); |
| 190 | } |
| 191 | } |
| 192 | |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 193 | static INLINE void set_flip_cfg(TX_TYPE tx_type, TXFM_2D_FLIP_CFG *cfg) { |
| 194 | get_flip_cfg(tx_type, &cfg->ud_flip, &cfg->lr_flip); |
| 195 | } |
| 196 | |
Debargha Mukherjee | 9eabd69 | 2017-11-16 12:44:31 -0800 | [diff] [blame] | 197 | // Utility function that returns the log of the ratio of the col and row |
| 198 | // sizes. |
| 199 | static INLINE int get_rect_tx_log_ratio(int col, int row) { |
| 200 | if (col == row) return 0; |
| 201 | if (col > row) { |
| 202 | if (col == row * 2) return 1; |
| 203 | if (col == row * 4) return 2; |
| 204 | assert(0 && "Unsupported transform size"); |
| 205 | } else { |
| 206 | if (row == col * 2) return -1; |
| 207 | if (row == col * 4) return -2; |
| 208 | assert(0 && "Unsupported transform size"); |
| 209 | } |
| 210 | return 0; // Invalid |
| 211 | } |
| 212 | |
Angie Chiang | ce3ad28 | 2017-08-08 09:51:54 -0700 | [diff] [blame] | 213 | void av1_gen_fwd_stage_range(int8_t *stage_range_col, int8_t *stage_range_row, |
| 214 | const TXFM_2D_FLIP_CFG *cfg, int bd); |
| 215 | |
| 216 | void av1_gen_inv_stage_range(int8_t *stage_range_col, int8_t *stage_range_row, |
Debargha Mukherjee | 1158bff | 2018-01-01 18:23:59 -0800 | [diff] [blame] | 217 | const TXFM_2D_FLIP_CFG *cfg, TX_SIZE tx_size, |
Angie Chiang | ce3ad28 | 2017-08-08 09:51:54 -0700 | [diff] [blame] | 218 | int bd); |
| 219 | |
Urvang Joshi | c502216 | 2017-11-21 15:57:42 -0800 | [diff] [blame] | 220 | void av1_get_fwd_txfm_cfg(TX_TYPE tx_type, TX_SIZE tx_size, |
| 221 | TXFM_2D_FLIP_CFG *cfg); |
| 222 | void av1_get_inv_txfm_cfg(TX_TYPE tx_type, TX_SIZE tx_size, |
| 223 | TXFM_2D_FLIP_CFG *cfg); |
Angie Chiang | 0c7b8d8 | 2018-01-23 19:20:44 -0800 | [diff] [blame] | 224 | extern const TXFM_TYPE av1_txfm_type_ls[5][TX_TYPES_1D]; |
| 225 | extern const int8_t av1_txfm_stage_num_list[TXFM_TYPES]; |
Angie Chiang | 29d2f21 | 2018-01-24 19:42:57 -0800 | [diff] [blame] | 226 | static INLINE int get_txw_idx(TX_SIZE tx_size) { |
| 227 | return tx_size_wide_log2[tx_size] - tx_size_wide_log2[0]; |
| 228 | } |
| 229 | static INLINE int get_txh_idx(TX_SIZE tx_size) { |
| 230 | return tx_size_high_log2[tx_size] - tx_size_high_log2[0]; |
| 231 | } |
Angie Chiang | af311e0 | 2018-08-21 16:08:28 -0700 | [diff] [blame] | 232 | |
| 233 | void av1_range_check_buf(int32_t stage, const int32_t *input, |
| 234 | const int32_t *buf, int32_t size, int8_t bit); |
Angie Chiang | 29d2f21 | 2018-01-24 19:42:57 -0800 | [diff] [blame] | 235 | #define MAX_TXWH_IDX 5 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 236 | #ifdef __cplusplus |
| 237 | } |
| 238 | #endif // __cplusplus |
| 239 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 240 | #endif // AOM_AV1_COMMON_AV1_TXFM_H_ |