Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 11 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | #ifndef AV1_TXFM_H_ |
| 13 | #define AV1_TXFM_H_ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
| 15 | #include <assert.h> |
| 16 | #include <math.h> |
| 17 | #include <stdio.h> |
| 18 | |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 19 | #include "config/aom_config.h" |
| 20 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 21 | #include "av1/common/enums.h" |
Angie Chiang | 155bf9a | 2017-08-06 19:52:57 -0700 | [diff] [blame] | 22 | #include "av1/common/blockd.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 23 | #include "aom/aom_integer.h" |
| 24 | #include "aom_dsp/aom_dsp_common.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 25 | |
Angie Chiang | 9c7089a | 2017-08-08 16:21:11 -0700 | [diff] [blame] | 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
Hui Su | 8d95019 | 2018-04-27 15:47:23 -0700 | [diff] [blame] | 30 | #if !defined(DO_RANGE_CHECK_CLAMP) |
| 31 | #define DO_RANGE_CHECK_CLAMP 0 |
| 32 | #endif |
| 33 | |
Yue Chen | 5b53ea1 | 2018-03-09 11:26:43 -0800 | [diff] [blame] | 34 | extern const int32_t av1_cospi_arr_data[7][64]; |
| 35 | extern const int32_t av1_sinpi_arr_data[7][5]; |
| 36 | |
Angie Chiang | ce3ad28 | 2017-08-08 09:51:54 -0700 | [diff] [blame] | 37 | #define MAX_TXFM_STAGE_NUM 12 |
| 38 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 39 | static const int cos_bit_min = 10; |
| 40 | static const int cos_bit_max = 16; |
| 41 | |
Angie Chiang | c8d6c08 | 2018-02-06 14:26:55 -0800 | [diff] [blame] | 42 | static const int NewSqrt2Bits = 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 | |
Frederic Barbier | bbf7bb0 | 2017-05-05 09:37:40 +0200 | [diff] [blame] | 48 | static INLINE const int32_t *cospi_arr(int n) { |
Yue Chen | 5b53ea1 | 2018-03-09 11:26:43 -0800 | [diff] [blame] | 49 | return av1_cospi_arr_data[n - cos_bit_min]; |
Frederic Barbier | bbf7bb0 | 2017-05-05 09:37:40 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Angie Chiang | 8251736 | 2018-01-17 17:31:54 -0800 | [diff] [blame] | 52 | static INLINE const int32_t *sinpi_arr(int n) { |
Yue Chen | 5b53ea1 | 2018-03-09 11:26:43 -0800 | [diff] [blame] | 53 | return av1_sinpi_arr_data[n - cos_bit_min]; |
Angie Chiang | 8251736 | 2018-01-17 17:31:54 -0800 | [diff] [blame] | 54 | } |
| 55 | |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 56 | static INLINE int32_t range_check_value(int32_t value, int8_t bit) { |
| 57 | #if CONFIG_COEFFICIENT_RANGE_CHECKING |
James Zern | b785b95 | 2018-02-01 19:27:09 -0800 | [diff] [blame] | 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) { |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 61 | fprintf(stderr, "coeff out of bit range, value: %d bit %d\n", value, bit); |
| 62 | assert(0); |
| 63 | } |
Hui Su | 8d95019 | 2018-04-27 15:47:23 -0700 | [diff] [blame] | 64 | #endif // CONFIG_COEFFICIENT_RANGE_CHECKING |
| 65 | #if DO_RANGE_CHECK_CLAMP |
| 66 | bit = AOMMIN(bit, 31); |
Angie Chiang | 66b7d7b | 2018-07-25 14:57:17 -0700 | [diff] [blame^] | 67 | return clamp(value, -(1 << (bit - 1)), (1 << (bit - 1)) - 1); |
Hui Su | 8d95019 | 2018-04-27 15:47:23 -0700 | [diff] [blame] | 68 | #endif // DO_RANGE_CHECK_CLAMP |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 69 | (void)bit; |
James Zern | dc85759 | 2018-02-01 19:21:48 -0800 | [diff] [blame] | 70 | return value; |
| 71 | } |
| 72 | |
Hui Su | 6d0fdad | 2018-03-12 10:46:40 -0700 | [diff] [blame] | 73 | static INLINE int32_t round_shift(int64_t value, int bit) { |
Yaowu Xu | 637590c | 2016-11-16 15:15:46 -0800 | [diff] [blame] | 74 | assert(bit >= 1); |
Hui Su | 6d0fdad | 2018-03-12 10:46:40 -0700 | [diff] [blame] | 75 | return (int32_t)((value + (1ll << (bit - 1))) >> bit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 78 | static INLINE int32_t half_btf(int32_t w0, int32_t in0, int32_t w1, int32_t in1, |
| 79 | int bit) { |
Angie Chiang | d1c222d | 2018-02-28 19:41:01 -0800 | [diff] [blame] | 80 | int64_t result_64 = (int64_t)(w0 * in0) + (int64_t)(w1 * in1); |
| 81 | #if CONFIG_COEFFICIENT_RANGE_CHECKING |
| 82 | assert(result_64 >= INT32_MIN && result_64 <= INT32_MAX); |
| 83 | #endif |
Hui Su | 6d0fdad | 2018-03-12 10:46:40 -0700 | [diff] [blame] | 84 | return round_shift(result_64, bit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Angie Chiang | 8c489a8 | 2018-05-15 16:07:30 -0700 | [diff] [blame] | 87 | static INLINE uint16_t highbd_clip_pixel_add(uint16_t dest, tran_high_t trans, |
| 88 | int bd) { |
| 89 | return clip_pixel_highbd(dest + (int)trans, bd); |
| 90 | } |
| 91 | |
Angie Chiang | d4327bc | 2018-01-22 20:54:04 -0800 | [diff] [blame] | 92 | typedef void (*TxfmFunc)(const int32_t *input, int32_t *output, int8_t cos_bit, |
| 93 | const int8_t *stage_range); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 94 | |
Peng Bin | 9f04180 | 2018-04-09 13:16:26 +0800 | [diff] [blame] | 95 | typedef void (*FwdTxfm2dFunc)(const int16_t *input, int32_t *output, int stride, |
| 96 | TX_TYPE tx_type, int bd); |
| 97 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 98 | typedef enum TXFM_TYPE { |
| 99 | TXFM_TYPE_DCT4, |
| 100 | TXFM_TYPE_DCT8, |
| 101 | TXFM_TYPE_DCT16, |
| 102 | TXFM_TYPE_DCT32, |
| 103 | TXFM_TYPE_DCT64, |
| 104 | TXFM_TYPE_ADST4, |
| 105 | TXFM_TYPE_ADST8, |
| 106 | TXFM_TYPE_ADST16, |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 107 | TXFM_TYPE_IDENTITY4, |
| 108 | TXFM_TYPE_IDENTITY8, |
| 109 | TXFM_TYPE_IDENTITY16, |
| 110 | TXFM_TYPE_IDENTITY32, |
Angie Chiang | 0c7b8d8 | 2018-01-23 19:20:44 -0800 | [diff] [blame] | 111 | TXFM_TYPES, |
| 112 | TXFM_TYPE_INVALID, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 113 | } TXFM_TYPE; |
| 114 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 115 | typedef struct TXFM_2D_FLIP_CFG { |
Angie Chiang | 0c7b8d8 | 2018-01-23 19:20:44 -0800 | [diff] [blame] | 116 | TX_SIZE tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 117 | int ud_flip; // flip upside down |
| 118 | int lr_flip; // flip left to right |
Angie Chiang | 4a75b5a | 2018-01-10 17:19:06 -0800 | [diff] [blame] | 119 | const int8_t *shift; |
Angie Chiang | d4327bc | 2018-01-22 20:54:04 -0800 | [diff] [blame] | 120 | int8_t cos_bit_col; |
| 121 | int8_t cos_bit_row; |
Angie Chiang | 0c7b8d8 | 2018-01-23 19:20:44 -0800 | [diff] [blame] | 122 | int8_t stage_range_col[MAX_TXFM_STAGE_NUM]; |
| 123 | int8_t stage_range_row[MAX_TXFM_STAGE_NUM]; |
| 124 | TXFM_TYPE txfm_type_col; |
| 125 | TXFM_TYPE txfm_type_row; |
| 126 | int stage_num_col; |
| 127 | int stage_num_row; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 128 | } TXFM_2D_FLIP_CFG; |
| 129 | |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 130 | 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] | 131 | switch (tx_type) { |
| 132 | case DCT_DCT: |
| 133 | case ADST_DCT: |
| 134 | case DCT_ADST: |
| 135 | case ADST_ADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 136 | *ud_flip = 0; |
| 137 | *lr_flip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 138 | break; |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 139 | case IDTX: |
| 140 | case V_DCT: |
| 141 | case H_DCT: |
| 142 | case V_ADST: |
| 143 | case H_ADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 144 | *ud_flip = 0; |
| 145 | *lr_flip = 0; |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 146 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 147 | case FLIPADST_DCT: |
Sarah Parker | eec47e6 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 148 | case FLIPADST_ADST: |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 149 | case V_FLIPADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 150 | *ud_flip = 1; |
| 151 | *lr_flip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 152 | break; |
| 153 | case DCT_FLIPADST: |
Sarah Parker | eec47e6 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 154 | case ADST_FLIPADST: |
Sarah Parker | 3eed417 | 2017-05-15 20:49:22 -0700 | [diff] [blame] | 155 | case H_FLIPADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 156 | *ud_flip = 0; |
| 157 | *lr_flip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 158 | break; |
| 159 | case FLIPADST_FLIPADST: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 160 | *ud_flip = 1; |
| 161 | *lr_flip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 162 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 163 | default: |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 164 | *ud_flip = 0; |
| 165 | *lr_flip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 166 | assert(0); |
| 167 | } |
| 168 | } |
| 169 | |
Angie Chiang | eae8208 | 2018-02-05 16:49:33 -0800 | [diff] [blame] | 170 | static INLINE void set_flip_cfg(TX_TYPE tx_type, TXFM_2D_FLIP_CFG *cfg) { |
| 171 | get_flip_cfg(tx_type, &cfg->ud_flip, &cfg->lr_flip); |
| 172 | } |
| 173 | |
Debargha Mukherjee | 9eabd69 | 2017-11-16 12:44:31 -0800 | [diff] [blame] | 174 | // Utility function that returns the log of the ratio of the col and row |
| 175 | // sizes. |
| 176 | static INLINE int get_rect_tx_log_ratio(int col, int row) { |
| 177 | if (col == row) return 0; |
| 178 | if (col > row) { |
| 179 | if (col == row * 2) return 1; |
| 180 | if (col == row * 4) return 2; |
| 181 | assert(0 && "Unsupported transform size"); |
| 182 | } else { |
| 183 | if (row == col * 2) return -1; |
| 184 | if (row == col * 4) return -2; |
| 185 | assert(0 && "Unsupported transform size"); |
| 186 | } |
| 187 | return 0; // Invalid |
| 188 | } |
| 189 | |
Angie Chiang | ce3ad28 | 2017-08-08 09:51:54 -0700 | [diff] [blame] | 190 | void av1_gen_fwd_stage_range(int8_t *stage_range_col, int8_t *stage_range_row, |
| 191 | const TXFM_2D_FLIP_CFG *cfg, int bd); |
| 192 | |
| 193 | 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] | 194 | const TXFM_2D_FLIP_CFG *cfg, TX_SIZE tx_size, |
Angie Chiang | ce3ad28 | 2017-08-08 09:51:54 -0700 | [diff] [blame] | 195 | int bd); |
| 196 | |
Urvang Joshi | c502216 | 2017-11-21 15:57:42 -0800 | [diff] [blame] | 197 | void av1_get_fwd_txfm_cfg(TX_TYPE tx_type, TX_SIZE tx_size, |
| 198 | TXFM_2D_FLIP_CFG *cfg); |
| 199 | void av1_get_inv_txfm_cfg(TX_TYPE tx_type, TX_SIZE tx_size, |
| 200 | TXFM_2D_FLIP_CFG *cfg); |
Angie Chiang | 0c7b8d8 | 2018-01-23 19:20:44 -0800 | [diff] [blame] | 201 | extern const TXFM_TYPE av1_txfm_type_ls[5][TX_TYPES_1D]; |
| 202 | extern const int8_t av1_txfm_stage_num_list[TXFM_TYPES]; |
Angie Chiang | 29d2f21 | 2018-01-24 19:42:57 -0800 | [diff] [blame] | 203 | static INLINE int get_txw_idx(TX_SIZE tx_size) { |
| 204 | return tx_size_wide_log2[tx_size] - tx_size_wide_log2[0]; |
| 205 | } |
| 206 | static INLINE int get_txh_idx(TX_SIZE tx_size) { |
| 207 | return tx_size_high_log2[tx_size] - tx_size_high_log2[0]; |
| 208 | } |
| 209 | #define MAX_TXWH_IDX 5 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 210 | #ifdef __cplusplus |
| 211 | } |
| 212 | #endif // __cplusplus |
| 213 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 214 | #endif // AV1_TXFM_H_ |