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 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | #ifndef AV1_ENCODER_BLOCK_H_ |
| 13 | #define AV1_ENCODER_BLOCK_H_ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
| 15 | #include "av1/common/entropymv.h" |
| 16 | #include "av1/common/entropy.h" |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 17 | #if CONFIG_PVQ |
| 18 | #include "av1/encoder/encint.h" |
| 19 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 20 | #include "av1/common/mvref_common.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 21 | |
| 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 26 | #if CONFIG_PVQ |
| 27 | // Maximum possible # of tx blocks in luma plane, which is currently 256, |
| 28 | // since there can be 16x16 of 4x4 tx. |
| 29 | #define MAX_PVQ_BLOCKS_IN_SB (MAX_SB_SQUARE >> 2 * OD_LOG_BSIZE0) |
| 30 | #endif |
| 31 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 32 | typedef struct { |
| 33 | unsigned int sse; |
| 34 | int sum; |
| 35 | unsigned int var; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 36 | } DIFF; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 37 | |
| 38 | typedef struct macroblock_plane { |
| 39 | DECLARE_ALIGNED(16, int16_t, src_diff[MAX_SB_SQUARE]); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 40 | #if CONFIG_PVQ |
| 41 | DECLARE_ALIGNED(16, int16_t, src_int16[MAX_SB_SQUARE]); |
| 42 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 43 | tran_low_t *qcoeff; |
| 44 | tran_low_t *coeff; |
| 45 | uint16_t *eobs; |
Angie Chiang | 74e2307 | 2017-03-24 14:54:23 -0700 | [diff] [blame] | 46 | #if CONFIG_LV_MAP |
| 47 | uint8_t *txb_entropy_ctx; |
| 48 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 49 | struct buf_2d src; |
| 50 | |
| 51 | // Quantizer setings |
| 52 | const int16_t *quant_fp; |
| 53 | const int16_t *round_fp; |
| 54 | const int16_t *quant; |
| 55 | const int16_t *quant_shift; |
| 56 | const int16_t *zbin; |
| 57 | const int16_t *round; |
| 58 | #if CONFIG_NEW_QUANT |
| 59 | const cuml_bins_type_nuq *cuml_bins_nuq[QUANT_PROFILES]; |
| 60 | #endif // CONFIG_NEW_QUANT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 61 | } MACROBLOCK_PLANE; |
| 62 | |
| 63 | /* The [2] dimension is for whether we skip the EOB node (i.e. if previous |
| 64 | * coefficient in this block was zero) or not. */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 65 | typedef unsigned int av1_coeff_cost[PLANE_TYPES][REF_TYPES][COEF_BANDS][2] |
| 66 | [COEFF_CONTEXTS][ENTROPY_TOKENS]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 67 | |
| 68 | typedef struct { |
| 69 | int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES]; |
| 70 | int16_t mode_context[MODE_CTX_REF_FRAMES]; |
Angie Chiang | f0fbf9d | 2017-03-15 15:01:22 -0700 | [diff] [blame] | 71 | #if CONFIG_LV_MAP |
Angie Chiang | c484abe | 2017-03-20 15:43:11 -0700 | [diff] [blame] | 72 | // TODO(angiebird): Reduce the buffer size according to sb_type |
| 73 | tran_low_t tcoeff[MAX_MB_PLANE][MAX_SB_SQUARE]; |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 74 | uint16_t eobs[MAX_MB_PLANE][MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)]; |
| 75 | uint8_t txb_skip_ctx[MAX_MB_PLANE] |
| 76 | [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)]; |
| 77 | int dc_sign_ctx[MAX_MB_PLANE] |
| 78 | [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)]; |
Angie Chiang | f0fbf9d | 2017-03-15 15:01:22 -0700 | [diff] [blame] | 79 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 80 | uint8_t ref_mv_count[MODE_CTX_REF_FRAMES]; |
| 81 | CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE]; |
| 82 | #if CONFIG_EXT_INTER |
| 83 | int16_t compound_mode_context[MODE_CTX_REF_FRAMES]; |
| 84 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 85 | } MB_MODE_INFO_EXT; |
| 86 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 87 | typedef struct { |
| 88 | int col_min; |
| 89 | int col_max; |
| 90 | int row_min; |
| 91 | int row_max; |
| 92 | } MvLimits; |
| 93 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 94 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 95 | typedef struct { |
| 96 | uint8_t best_palette_color_map[MAX_SB_SQUARE]; |
| 97 | float kmeans_data_buf[2 * MAX_SB_SQUARE]; |
| 98 | } PALETTE_BUFFER; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 99 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 100 | |
| 101 | typedef struct macroblock MACROBLOCK; |
| 102 | struct macroblock { |
| 103 | struct macroblock_plane plane[MAX_MB_PLANE]; |
| 104 | |
| 105 | MACROBLOCKD e_mbd; |
| 106 | MB_MODE_INFO_EXT *mbmi_ext; |
| 107 | int skip_block; |
David Barker | d7d78c8 | 2016-10-24 10:55:35 +0100 | [diff] [blame] | 108 | int qindex; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 109 | |
| 110 | // The equivalent error at the current rdmult of one whole bit (not one |
| 111 | // bitcost unit). |
| 112 | int errorperbit; |
| 113 | // The equivalend SAD error of one (whole) bit at the current quantizer |
| 114 | // for large blocks. |
| 115 | int sadperbit16; |
| 116 | // The equivalend SAD error of one (whole) bit at the current quantizer |
| 117 | // for sub-8x8 blocks. |
| 118 | int sadperbit4; |
| 119 | int rddiv; |
| 120 | int rdmult; |
| 121 | int mb_energy; |
| 122 | int *m_search_count_ptr; |
| 123 | int *ex_search_count_ptr; |
| 124 | |
Jingning Han | 9777afc | 2016-10-20 15:17:43 -0700 | [diff] [blame] | 125 | #if CONFIG_VAR_TX |
| 126 | unsigned int txb_split_count; |
| 127 | #endif |
| 128 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 129 | // These are set to their default values at the beginning, and then adjusted |
| 130 | // further in the encoding process. |
| 131 | BLOCK_SIZE min_partition_size; |
| 132 | BLOCK_SIZE max_partition_size; |
| 133 | |
| 134 | int mv_best_ref_index[TOTAL_REFS_PER_FRAME]; |
| 135 | unsigned int max_mv_context[TOTAL_REFS_PER_FRAME]; |
| 136 | unsigned int source_variance; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 137 | unsigned int pred_sse[TOTAL_REFS_PER_FRAME]; |
| 138 | int pred_mv_sad[TOTAL_REFS_PER_FRAME]; |
| 139 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 140 | int *nmvjointcost; |
| 141 | int nmv_vec_cost[NMV_CONTEXTS][MV_JOINTS]; |
| 142 | int *nmvcost[NMV_CONTEXTS][2]; |
| 143 | int *nmvcost_hp[NMV_CONTEXTS][2]; |
| 144 | int **mv_cost_stack[NMV_CONTEXTS]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 145 | int **mvcost; |
Alex Converse | a127a79 | 2017-05-23 15:27:21 -0700 | [diff] [blame^] | 146 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 147 | #if CONFIG_MOTION_VAR |
| 148 | int32_t *wsrc_buf; |
| 149 | int32_t *mask_buf; |
| 150 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 151 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 152 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 153 | PALETTE_BUFFER *palette_buffer; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 154 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 155 | |
| 156 | // These define limits to motion vector components to prevent them |
| 157 | // from extending outside the UMV borders |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 158 | MvLimits mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 159 | |
| 160 | #if CONFIG_VAR_TX |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 161 | uint8_t blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 162 | uint8_t blk_skip_drl[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 163 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 164 | |
| 165 | int skip; |
| 166 | |
Jingning Han | 8efdbc8 | 2017-02-19 14:40:03 -0800 | [diff] [blame] | 167 | #if CONFIG_CB4X4 |
| 168 | int skip_chroma_rd; |
| 169 | #endif |
| 170 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 171 | // note that token_costs is the cost when eob node is skipped |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 172 | av1_coeff_cost token_costs[TX_SIZES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 173 | |
| 174 | int optimize; |
| 175 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 176 | // Used to store sub partition's choices. |
| 177 | MV pred_mv[TOTAL_REFS_PER_FRAME]; |
| 178 | |
| 179 | // Store the best motion vector during motion search |
| 180 | int_mv best_mv; |
| 181 | // Store the second best motion vector during full-pixel motion search |
| 182 | int_mv second_best_mv; |
| 183 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 184 | // use default transform and skip transform type search for intra modes |
| 185 | int use_default_intra_tx_type; |
| 186 | // use default transform and skip transform type search for inter modes |
| 187 | int use_default_inter_tx_type; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 188 | #if CONFIG_PVQ |
| 189 | int rate; |
| 190 | // 1 if neither AC nor DC is coded. Only used during RDO. |
| 191 | int pvq_skip[MAX_MB_PLANE]; |
| 192 | PVQ_QUEUE *pvq_q; |
| 193 | |
| 194 | // Storage for PVQ tx block encodings in a superblock. |
| 195 | // There can be max 16x16 of 4x4 blocks (and YUV) encode by PVQ |
| 196 | // 256 is the max # of 4x4 blocks in a SB (64x64), which comes from: |
| 197 | // 1) Since PVQ is applied to each trasnform-ed block |
| 198 | // 2) 4x4 is the smallest tx size in AV1 |
| 199 | // 3) AV1 allows using smaller tx size than block (i.e. partition) size |
| 200 | // TODO(yushin) : The memory usage could be improved a lot, since this has |
| 201 | // storage for 10 bands and 128 coefficients for every 4x4 block, |
| 202 | PVQ_INFO pvq[MAX_PVQ_BLOCKS_IN_SB][MAX_MB_PLANE]; |
| 203 | daala_enc_ctx daala_enc; |
| 204 | int pvq_speed; |
| 205 | int pvq_coded; // Indicates whether pvq_info needs be stored to tokenize |
| 206 | #endif |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 207 | #if CONFIG_DAALA_DIST |
| 208 | // Keep rate of each 4x4 block in the current macroblock during RDO |
| 209 | // This is needed when using the 8x8 Daala distortion metric during RDO, |
| 210 | // because it evaluates distortion in a different order than the underlying |
| 211 | // 4x4 blocks are coded. |
| 212 | int rate_4x4[256]; |
| 213 | #endif |
Luc Trudeau | e398028 | 2017-04-25 23:17:21 -0400 | [diff] [blame] | 214 | #if CONFIG_CFL |
| 215 | // Whether luma needs to be stored during RDO. |
| 216 | int cfl_store_y; |
| 217 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | #ifdef __cplusplus |
| 221 | } // extern "C" |
| 222 | #endif |
| 223 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 224 | #endif // AV1_ENCODER_BLOCK_H_ |