Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [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 | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [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 | */ |
| 11 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 12 | #ifndef AOM_AV1_ENCODER_ENCODEMB_H_ |
| 13 | #define AOM_AV1_ENCODER_ENCODEMB_H_ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 15 | #include "config/aom_config.h" |
| 16 | |
Wan-Teh Chang | f2d15ee | 2020-03-10 09:24:43 -0700 | [diff] [blame] | 17 | #include "av1/common/av1_common_int.h" |
Xing Jin | bd91e94 | 2018-06-21 13:43:17 +0800 | [diff] [blame] | 18 | #include "av1/common/txb_common.h" |
Debargha Mukherjee | 2d565f4 | 2020-06-17 18:07:12 -0700 | [diff] [blame] | 19 | #include "av1/encoder/av1_quantize.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 20 | #include "av1/encoder/block.h" |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 21 | #include "av1/encoder/tokenize.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
chiyotsai | c2f3841 | 2020-06-10 16:07:21 -0700 | [diff] [blame] | 26 | enum { |
| 27 | AV1_XFORM_QUANT_FP = 0, |
| 28 | AV1_XFORM_QUANT_B = 1, |
| 29 | AV1_XFORM_QUANT_DC = 2, |
| 30 | AV1_XFORM_QUANT_SKIP_QUANT, |
| 31 | AV1_XFORM_QUANT_TYPES, |
| 32 | } UENUM1BYTE(AV1_XFORM_QUANT); |
| 33 | |
| 34 | // TODO(any): Merge OPT_TYPe and TRELLLIS_OPT_TYPE |
| 35 | // Available optimization types to optimize the quantized coefficients. |
| 36 | enum { |
| 37 | NONE_OPT = 0, // No optimization. |
| 38 | TRELLIS_OPT = 1, // Trellis optimization. See `av1_optimize_b()`. |
| 39 | DROPOUT_OPT = 2, // Dropout optimization. See `av1_dropout_qcoeff()`. |
| 40 | TRELLIS_DROPOUT_OPT = 3 // Perform dropout after trellis optimization. |
| 41 | } UENUM1BYTE(OPT_TYPE); |
| 42 | |
| 43 | enum { |
| 44 | NO_TRELLIS_OPT, // No trellis optimization |
| 45 | FULL_TRELLIS_OPT, // Trellis optimization in all stages |
| 46 | FINAL_PASS_TRELLIS_OPT, // Trellis optimization in only the final encode pass |
| 47 | NO_ESTIMATE_YRD_TRELLIS_OPT // Disable trellis in estimate_yrd_for_sb |
| 48 | } UENUM1BYTE(TRELLIS_OPT_TYPE); |
| 49 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 50 | struct optimize_ctx { |
Wan-Teh Chang | 660f8ed | 2018-05-11 17:28:08 -0700 | [diff] [blame] | 51 | ENTROPY_CONTEXT ta[MAX_MB_PLANE][MAX_MIB_SIZE]; |
| 52 | ENTROPY_CONTEXT tl[MAX_MB_PLANE][MAX_MIB_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct encode_b_args { |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 56 | const struct AV1_COMP *cpi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 57 | MACROBLOCK *x; |
| 58 | struct optimize_ctx *ctx; |
| 59 | int8_t *skip; |
| 60 | ENTROPY_CONTEXT *ta; |
| 61 | ENTROPY_CONTEXT *tl; |
Debargha Mukherjee | 8a191ef | 2020-03-24 00:47:21 -0700 | [diff] [blame] | 62 | RUN_TYPE dry_run; |
| 63 | TRELLIS_OPT_TYPE enable_optimize_b; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 66 | void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, |
Hui Su | 95e9c0c | 2019-12-09 15:21:35 -0800 | [diff] [blame] | 67 | RUN_TYPE dry_run); |
Urvang Joshi | 10df032 | 2018-07-31 14:03:30 -0700 | [diff] [blame] | 68 | |
| 69 | void av1_foreach_transformed_block_in_plane( |
Hui Su | 56e838f | 2019-12-04 14:48:30 -0800 | [diff] [blame] | 70 | const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane, |
Urvang Joshi | 10df032 | 2018-07-31 14:03:30 -0700 | [diff] [blame] | 71 | foreach_transformed_block_visitor visit, void *arg); |
| 72 | |
Urvang Joshi | 1de67aa | 2020-03-20 11:21:57 -0700 | [diff] [blame] | 73 | void av1_encode_sby_pass1(struct AV1_COMP *cpi, MACROBLOCK *x, |
| 74 | BLOCK_SIZE bsize); |
Urvang Joshi | 10df032 | 2018-07-31 14:03:30 -0700 | [diff] [blame] | 75 | |
bohanli | d9d8d67 | 2019-11-21 14:43:26 -0800 | [diff] [blame] | 76 | void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size, |
| 77 | TX_TYPE tx_type, TxfmParam *txfm_param); |
Urvang Joshi | 1de67aa | 2020-03-20 11:21:57 -0700 | [diff] [blame] | 78 | void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx, |
| 79 | int use_quant_b_adapt, QUANT_PARAM *qparam); |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 80 | void av1_setup_qmatrix(const CommonQuantParams *quant_params, |
Urvang Joshi | f409a63 | 2020-03-28 01:03:50 -0700 | [diff] [blame] | 81 | const MACROBLOCKD *xd, int plane, TX_SIZE tx_size, |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 82 | TX_TYPE tx_type, QUANT_PARAM *qparam); |
bohanli | d9d8d67 | 2019-11-21 14:43:26 -0800 | [diff] [blame] | 83 | |
Ravi Chaudhary | 6eaea62 | 2020-08-28 10:28:24 +0530 | [diff] [blame] | 84 | void av1_xform_dc_only(MACROBLOCK *x, int plane, int block, |
| 85 | TxfmParam *txfm_param, int64_t per_px_mean); |
| 86 | |
bohanli | d9d8d67 | 2019-11-21 14:43:26 -0800 | [diff] [blame] | 87 | void av1_xform_quant(MACROBLOCK *x, int plane, int block, int blk_row, |
bohanli | b79e6e7 | 2019-11-22 16:06:38 -0800 | [diff] [blame] | 88 | int blk_col, BLOCK_SIZE plane_bsize, TxfmParam *txfm_param, |
bohanli | d9d8d67 | 2019-11-21 14:43:26 -0800 | [diff] [blame] | 89 | QUANT_PARAM *qparam); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 90 | |
Deepa K G | cb2fa3b | 2020-05-06 19:59:26 +0530 | [diff] [blame] | 91 | void av1_xform(MACROBLOCK *x, int plane, int block, int blk_row, int blk_col, |
| 92 | BLOCK_SIZE plane_bsize, TxfmParam *txfm_param); |
| 93 | |
| 94 | void av1_quant(MACROBLOCK *x, int plane, int block, TxfmParam *txfm_param, |
| 95 | QUANT_PARAM *qparam); |
| 96 | |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 97 | int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *mb, int plane, |
Xing Jin | bd91e94 | 2018-06-21 13:43:17 +0800 | [diff] [blame] | 98 | int block, TX_SIZE tx_size, TX_TYPE tx_type, |
chiyotsai | fc56df8 | 2020-07-09 15:53:46 -0700 | [diff] [blame] | 99 | const TXB_CTX *const txb_ctx, int *rate_cost); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 100 | |
yjshen | 27aa844 | 2020-01-03 16:35:34 -0800 | [diff] [blame] | 101 | // This function can be used as (i) a further optimization to reduce the |
| 102 | // redundancy of quantized coefficients (a.k.a., `qcoeff`) after trellis |
| 103 | // optimization, or (ii) an alternative to trellis optimization in high-speed |
| 104 | // compression mode (e.g., real-time mode under speed-6) due to its LOW time |
| 105 | // complexity. The rational behind is to drop out the may-be redundant quantized |
| 106 | // coefficient which is among a bunch of zeros. NOTE: This algorithm is not as |
| 107 | // accurate as trellis optimization since the hyper-parameters are hard-coded |
| 108 | // instead of dynamic search. More adaptive logic may improve the performance. |
| 109 | // This function should be applied to all or partical block cells. |
| 110 | // Inputs: |
| 111 | // mb: Pointer to the MACROBLOCK to perform dropout on. |
| 112 | // plane: Index of the plane to which the target block belongs. |
| 113 | // block: Index of the target block. |
| 114 | // tx_size: Transform size of the target block. |
| 115 | // tx_type: Transform type of the target block. This field is particularly |
| 116 | // used to find out the scan order of the block. |
| 117 | // qindex: Quantization index used for target block. In general, all blocks |
| 118 | // in a same plane share the same quantization index. This field is |
| 119 | // particularly used to determine how many zeros should be used to |
| 120 | // drop out a coefficient. |
| 121 | // Returns: |
| 122 | // Nothing will be returned, but `qcoeff`, `dqcoeff`, `eob`, as well as |
| 123 | // `txb_entropy_ctx`, which `mb` points to, may be modified by this function. |
| 124 | void av1_dropout_qcoeff(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size, |
| 125 | TX_TYPE tx_type, int qindex); |
| 126 | |
Angie Chiang | bb05a96 | 2021-03-31 11:34:53 -0700 | [diff] [blame] | 127 | void av1_subtract_block(BitDepthInfo bd_info, int rows, int cols, int16_t *diff, |
| 128 | ptrdiff_t diff_stride, const uint8_t *src8, |
| 129 | ptrdiff_t src_stride, const uint8_t *pred8, |
| 130 | ptrdiff_t pred_stride); |
Jingning Han | b7b9d49 | 2019-11-18 15:59:14 -0800 | [diff] [blame] | 131 | |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 132 | void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize, |
| 133 | int blk_col, int blk_row, TX_SIZE tx_size); |
| 134 | |
Hui Su | 56e838f | 2019-12-04 14:48:30 -0800 | [diff] [blame] | 135 | void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE plane_bsize, int plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 136 | |
Hui Su | db0301e | 2018-04-02 10:57:21 -0700 | [diff] [blame] | 137 | static INLINE void av1_set_txb_context(MACROBLOCK *x, int plane, int block, |
| 138 | TX_SIZE tx_size, ENTROPY_CONTEXT *a, |
| 139 | ENTROPY_CONTEXT *l) { |
| 140 | const uint8_t ctx = x->plane[plane].txb_entropy_ctx[block]; |
| 141 | memset(a, ctx, tx_size_wide_unit[tx_size] * sizeof(*a)); |
| 142 | memset(l, ctx, tx_size_high_unit[tx_size] * sizeof(*l)); |
| 143 | } |
Angie Chiang | 36aca33 | 2017-03-23 14:16:24 -0700 | [diff] [blame] | 144 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 145 | void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col, |
| 146 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 147 | |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 148 | void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x, |
Debargha Mukherjee | 8a191ef | 2020-03-24 00:47:21 -0700 | [diff] [blame] | 149 | BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run, |
| 150 | TRELLIS_OPT_TYPE enable_optimize_b); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 151 | |
Debargha Mukherjee | 8a191ef | 2020-03-24 00:47:21 -0700 | [diff] [blame] | 152 | static INLINE int is_trellis_used(TRELLIS_OPT_TYPE optimize_b, |
| 153 | RUN_TYPE dry_run) { |
| 154 | if (optimize_b == NO_TRELLIS_OPT) return false; |
| 155 | if (optimize_b == FINAL_PASS_TRELLIS_OPT && dry_run != OUTPUT_ENABLED) |
| 156 | return false; |
| 157 | return true; |
| 158 | } |
Ravi Chaudhary | 6eaea62 | 2020-08-28 10:28:24 +0530 | [diff] [blame] | 159 | |
Ravi Chaudhary | c4aff3a | 2020-09-10 10:45:48 +0530 | [diff] [blame] | 160 | // Scaling terms (precision of 12 bits) to perform tx-size specific |
| 161 | // normalization that is used in DCT_DCT forward transform. |
| 162 | // For transform blocks of 1:2 and 2:1 - sqrt(2) normalization is used |
| 163 | // For transform blocks of 1:4 and 4:1 - factor of 2 is used |
| 164 | // For transform blocks TX_8x8 and below - an additional factor of 2 is used |
| 165 | // For transform blocks max(width,height)=64 - currently not supported |
| 166 | |
Ravi Chaudhary | 6eaea62 | 2020-08-28 10:28:24 +0530 | [diff] [blame] | 167 | static const uint16_t dc_coeff_scale[TX_SIZES_ALL] = { |
| 168 | 1024, 2048, 4096, 4096, 0, 1448, 1448, 2896, 2896, 2896, |
| 169 | 2896, 0, 0, 2048, 2048, 4096, 4096, 0, 0 |
| 170 | }; |
| 171 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 172 | #ifdef __cplusplus |
| 173 | } // extern "C" |
| 174 | #endif |
| 175 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 176 | #endif // AOM_AV1_ENCODER_ENCODEMB_H_ |