Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [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 |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [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/. |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include "aom_ports/system_state.h" |
| 14 | |
| 15 | #include "av1/common/reconintra.h" |
| 16 | |
| 17 | #include "av1/encoder/encoder.h" |
| 18 | #include "av1/encoder/encodeframe_utils.h" |
| 19 | #include "av1/encoder/partition_strategy.h" |
| 20 | #include "av1/encoder/rdopt.h" |
| 21 | |
| 22 | static AOM_INLINE int set_deltaq_rdmult(const AV1_COMP *const cpi, |
| 23 | const MACROBLOCK *const x) { |
| 24 | const AV1_COMMON *const cm = &cpi->common; |
| 25 | const CommonQuantParams *quant_params = &cm->quant_params; |
| 26 | return av1_compute_rd_mult(cpi, quant_params->base_qindex + x->delta_qindex + |
| 27 | quant_params->y_dc_delta_q); |
| 28 | } |
| 29 | |
| 30 | void av1_set_ssim_rdmult(const AV1_COMP *const cpi, MvCosts *const mv_costs, |
| 31 | const BLOCK_SIZE bsize, const int mi_row, |
| 32 | const int mi_col, int *const rdmult) { |
| 33 | const AV1_COMMON *const cm = &cpi->common; |
| 34 | |
| 35 | const int bsize_base = BLOCK_16X16; |
| 36 | const int num_mi_w = mi_size_wide[bsize_base]; |
| 37 | const int num_mi_h = mi_size_high[bsize_base]; |
| 38 | const int num_cols = (cm->mi_params.mi_cols + num_mi_w - 1) / num_mi_w; |
| 39 | const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h; |
| 40 | const int num_bcols = (mi_size_wide[bsize] + num_mi_w - 1) / num_mi_w; |
| 41 | const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h; |
| 42 | int row, col; |
| 43 | double num_of_mi = 0.0; |
| 44 | double geom_mean_of_scale = 0.0; |
| 45 | |
Vishesh | 94a6529 | 2020-07-01 15:28:53 +0530 | [diff] [blame] | 46 | assert(cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIM); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 47 | |
| 48 | aom_clear_system_state(); |
| 49 | for (row = mi_row / num_mi_w; |
| 50 | row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) { |
| 51 | for (col = mi_col / num_mi_h; |
| 52 | col < num_cols && col < mi_col / num_mi_h + num_bcols; ++col) { |
| 53 | const int index = row * num_cols + col; |
| 54 | geom_mean_of_scale += log(cpi->ssim_rdmult_scaling_factors[index]); |
| 55 | num_of_mi += 1.0; |
| 56 | } |
| 57 | } |
| 58 | geom_mean_of_scale = exp(geom_mean_of_scale / num_of_mi); |
| 59 | |
| 60 | *rdmult = (int)((double)(*rdmult) * geom_mean_of_scale + 0.5); |
| 61 | *rdmult = AOMMAX(*rdmult, 0); |
| 62 | av1_set_error_per_bit(mv_costs, *rdmult); |
| 63 | aom_clear_system_state(); |
| 64 | } |
| 65 | |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 66 | // Return the end column for the current superblock, in unit of TPL blocks. |
| 67 | static int get_superblock_tpl_column_end(const AV1_COMMON *const cm, int mi_col, |
| 68 | int num_mi_w) { |
| 69 | // Find the start column of this superblock. |
| 70 | const int sb_mi_col_start = (mi_col >> cm->seq_params.mib_size_log2) |
| 71 | << cm->seq_params.mib_size_log2; |
| 72 | // Same but in superres upscaled dimension. |
| 73 | const int sb_mi_col_start_sr = |
| 74 | coded_to_superres_mi(sb_mi_col_start, cm->superres_scale_denominator); |
| 75 | // Width of this superblock in mi units. |
| 76 | const int sb_mi_width = mi_size_wide[cm->seq_params.sb_size]; |
| 77 | // Same but in superres upscaled dimension. |
| 78 | const int sb_mi_width_sr = |
| 79 | coded_to_superres_mi(sb_mi_width, cm->superres_scale_denominator); |
| 80 | // Superblock end in mi units. |
| 81 | const int sb_mi_end = sb_mi_col_start_sr + sb_mi_width_sr; |
| 82 | // Superblock end in TPL units. |
| 83 | return (sb_mi_end + num_mi_w - 1) / num_mi_w; |
| 84 | } |
| 85 | |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 86 | int av1_get_hier_tpl_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x, |
| 87 | const BLOCK_SIZE bsize, const int mi_row, |
| 88 | const int mi_col, int orig_rdmult) { |
| 89 | const AV1_COMMON *const cm = &cpi->common; |
| 90 | const GF_GROUP *const gf_group = &cpi->gf_group; |
| 91 | assert(IMPLIES(cpi->gf_group.size > 0, |
| 92 | cpi->gf_group.index < cpi->gf_group.size)); |
| 93 | const int tpl_idx = cpi->gf_group.index; |
| 94 | const TplDepFrame *tpl_frame = &cpi->tpl_data.tpl_frame[tpl_idx]; |
| 95 | const int deltaq_rdmult = set_deltaq_rdmult(cpi, x); |
| 96 | if (tpl_frame->is_valid == 0) return deltaq_rdmult; |
Deepa K G | 21e5e8e | 2020-03-28 13:26:09 +0530 | [diff] [blame] | 97 | if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return deltaq_rdmult; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 98 | if (tpl_idx >= MAX_TPL_FRAME_IDX) return deltaq_rdmult; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 99 | if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return deltaq_rdmult; |
| 100 | |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 101 | const int mi_col_sr = |
| 102 | coded_to_superres_mi(mi_col, cm->superres_scale_denominator); |
| 103 | const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width); |
| 104 | const int block_mi_width_sr = |
| 105 | coded_to_superres_mi(mi_size_wide[bsize], cm->superres_scale_denominator); |
| 106 | |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 107 | const int bsize_base = BLOCK_16X16; |
| 108 | const int num_mi_w = mi_size_wide[bsize_base]; |
| 109 | const int num_mi_h = mi_size_high[bsize_base]; |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 110 | const int num_cols = (mi_cols_sr + num_mi_w - 1) / num_mi_w; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 111 | const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h; |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 112 | const int num_bcols = (block_mi_width_sr + num_mi_w - 1) / num_mi_w; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 113 | const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h; |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 114 | // This is required because the end col of superblock may be off by 1 in case |
| 115 | // of superres. |
| 116 | const int sb_bcol_end = get_superblock_tpl_column_end(cm, mi_col, num_mi_w); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 117 | int row, col; |
| 118 | double base_block_count = 0.0; |
| 119 | double geom_mean_of_scale = 0.0; |
| 120 | aom_clear_system_state(); |
| 121 | for (row = mi_row / num_mi_w; |
| 122 | row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) { |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 123 | for (col = mi_col_sr / num_mi_h; |
| 124 | col < num_cols && col < mi_col_sr / num_mi_h + num_bcols && |
| 125 | col < sb_bcol_end; |
| 126 | ++col) { |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 127 | const int index = row * num_cols + col; |
| 128 | geom_mean_of_scale += log(cpi->tpl_sb_rdmult_scaling_factors[index]); |
| 129 | base_block_count += 1.0; |
| 130 | } |
| 131 | } |
| 132 | geom_mean_of_scale = exp(geom_mean_of_scale / base_block_count); |
| 133 | int rdmult = (int)((double)orig_rdmult * geom_mean_of_scale + 0.5); |
| 134 | rdmult = AOMMAX(rdmult, 0); |
| 135 | av1_set_error_per_bit(&x->mv_costs, rdmult); |
| 136 | aom_clear_system_state(); |
| 137 | if (bsize == cm->seq_params.sb_size) { |
| 138 | const int rdmult_sb = set_deltaq_rdmult(cpi, x); |
| 139 | assert(rdmult_sb == rdmult); |
| 140 | (void)rdmult_sb; |
| 141 | } |
| 142 | return rdmult; |
| 143 | } |
| 144 | |
| 145 | static AOM_INLINE void update_filter_type_count(FRAME_COUNTS *counts, |
| 146 | const MACROBLOCKD *xd, |
| 147 | const MB_MODE_INFO *mbmi) { |
Hui Su | 93c395b | 2020-10-05 12:00:20 -0700 | [diff] [blame] | 148 | #if CONFIG_REMOVE_DUAL_FILTER |
| 149 | const int ctx = av1_get_pred_context_switchable_interp(xd, 0); |
| 150 | ++counts->switchable_interp[ctx][mbmi->interp_fltr]; |
| 151 | #else |
| 152 | for (int dir = 0; dir < 2; ++dir) { |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 153 | const int ctx = av1_get_pred_context_switchable_interp(xd, dir); |
| 154 | InterpFilter filter = av1_extract_interp_filter(mbmi->interp_filters, dir); |
| 155 | ++counts->switchable_interp[ctx][filter]; |
| 156 | } |
Hui Su | 93c395b | 2020-10-05 12:00:20 -0700 | [diff] [blame] | 157 | #endif // CONFIG_REMOVE_DUAL_FILTER |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void reset_tx_size(MACROBLOCK *x, MB_MODE_INFO *mbmi, |
| 161 | const TX_MODE tx_mode) { |
| 162 | MACROBLOCKD *const xd = &x->e_mbd; |
| 163 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 164 | #if CONFIG_SDP |
| 165 | int plane_index = xd->tree_type == CHROMA_PART; |
| 166 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 167 | if (xd->lossless[mbmi->segment_id]) { |
| 168 | mbmi->tx_size = TX_4X4; |
| 169 | } else if (tx_mode != TX_MODE_SELECT) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 170 | #if CONFIG_SDP |
| 171 | mbmi->tx_size = tx_size_from_tx_mode(mbmi->sb_type[plane_index], tx_mode); |
| 172 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 173 | mbmi->tx_size = tx_size_from_tx_mode(mbmi->sb_type, tx_mode); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 174 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 175 | } else { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 176 | #if CONFIG_SDP |
| 177 | BLOCK_SIZE bsize = mbmi->sb_type[plane_index]; |
| 178 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 179 | BLOCK_SIZE bsize = mbmi->sb_type; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 180 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 181 | TX_SIZE min_tx_size = depth_to_tx_size(MAX_TX_DEPTH, bsize); |
| 182 | mbmi->tx_size = (TX_SIZE)TXSIZEMAX(mbmi->tx_size, min_tx_size); |
| 183 | } |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 184 | #if CONFIG_SDP |
| 185 | if (is_inter_block(mbmi, xd->tree_type)) { |
| 186 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 187 | if (is_inter_block(mbmi)) { |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 188 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 189 | memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size)); |
| 190 | } |
| 191 | const int stride = xd->tx_type_map_stride; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 192 | #if CONFIG_SDP |
| 193 | const int bw = mi_size_wide[mbmi->sb_type[plane_index]]; |
| 194 | for (int row = 0; row < mi_size_high[mbmi->sb_type[plane_index]]; ++row) { |
| 195 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 196 | const int bw = mi_size_wide[mbmi->sb_type]; |
| 197 | for (int row = 0; row < mi_size_high[mbmi->sb_type]; ++row) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 198 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 199 | memset(xd->tx_type_map + row * stride, DCT_DCT, |
| 200 | bw * sizeof(xd->tx_type_map[0])); |
| 201 | } |
| 202 | av1_zero(txfm_info->blk_skip); |
| 203 | txfm_info->skip_txfm = 0; |
| 204 | } |
| 205 | |
| 206 | // This function will copy the best reference mode information from |
| 207 | // MB_MODE_INFO_EXT_FRAME to MB_MODE_INFO_EXT. |
| 208 | static INLINE void copy_mbmi_ext_frame_to_mbmi_ext( |
| 209 | MB_MODE_INFO_EXT *mbmi_ext, |
| 210 | const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_best, uint8_t ref_frame_type) { |
| 211 | memcpy(mbmi_ext->ref_mv_stack[ref_frame_type], mbmi_ext_best->ref_mv_stack, |
| 212 | sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE])); |
| 213 | memcpy(mbmi_ext->weight[ref_frame_type], mbmi_ext_best->weight, |
| 214 | sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE])); |
| 215 | mbmi_ext->mode_context[ref_frame_type] = mbmi_ext_best->mode_context; |
| 216 | mbmi_ext->ref_mv_count[ref_frame_type] = mbmi_ext_best->ref_mv_count; |
| 217 | memcpy(mbmi_ext->global_mvs, mbmi_ext_best->global_mvs, |
| 218 | sizeof(mbmi_ext->global_mvs)); |
| 219 | } |
| 220 | |
| 221 | void av1_update_state(const AV1_COMP *const cpi, ThreadData *td, |
| 222 | const PICK_MODE_CONTEXT *const ctx, int mi_row, |
| 223 | int mi_col, BLOCK_SIZE bsize, RUN_TYPE dry_run) { |
| 224 | int i, x_idx, y; |
| 225 | const AV1_COMMON *const cm = &cpi->common; |
| 226 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
| 227 | const int num_planes = av1_num_planes(cm); |
| 228 | RD_COUNTS *const rdc = &td->rd_counts; |
| 229 | MACROBLOCK *const x = &td->mb; |
| 230 | MACROBLOCKD *const xd = &x->e_mbd; |
| 231 | struct macroblock_plane *const p = x->plane; |
| 232 | struct macroblockd_plane *const pd = xd->plane; |
| 233 | const MB_MODE_INFO *const mi = &ctx->mic; |
| 234 | MB_MODE_INFO *const mi_addr = xd->mi[0]; |
| 235 | const struct segmentation *const seg = &cm->seg; |
venkat sanampudi | 2405502 | 2020-07-03 06:52:28 +0530 | [diff] [blame] | 236 | assert(bsize < BLOCK_SIZES_ALL); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 237 | #if CONFIG_SDP |
| 238 | const int bw = mi_size_wide[mi->sb_type[xd->tree_type == CHROMA_PART]]; |
| 239 | const int bh = mi_size_high[mi->sb_type[xd->tree_type == CHROMA_PART]]; |
| 240 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 241 | const int bw = mi_size_wide[mi->sb_type]; |
| 242 | const int bh = mi_size_high[mi->sb_type]; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 243 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 244 | const int mis = mi_params->mi_stride; |
| 245 | const int mi_width = mi_size_wide[bsize]; |
| 246 | const int mi_height = mi_size_high[bsize]; |
| 247 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 248 | #if CONFIG_SDP |
| 249 | assert(mi->sb_type[xd->tree_type == CHROMA_PART] == bsize); |
| 250 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 251 | assert(mi->sb_type == bsize); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 252 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 253 | |
| 254 | *mi_addr = *mi; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 255 | #if CONFIG_SDP |
| 256 | if (xd->tree_type != CHROMA_PART) |
| 257 | #endif |
| 258 | copy_mbmi_ext_frame_to_mbmi_ext(x->mbmi_ext, &ctx->mbmi_ext_best, |
| 259 | av1_ref_frame_type(ctx->mic.ref_frame)); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 260 | |
| 261 | memcpy(txfm_info->blk_skip, ctx->blk_skip, |
| 262 | sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk); |
| 263 | |
| 264 | txfm_info->skip_txfm = ctx->rd_stats.skip_txfm; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 265 | #if CONFIG_SDP |
| 266 | if (xd->tree_type != CHROMA_PART) { |
| 267 | #endif |
| 268 | xd->tx_type_map = ctx->tx_type_map; |
| 269 | xd->tx_type_map_stride = mi_size_wide[bsize]; |
| 270 | // If not dry_run, copy the transform type data into the frame level buffer. |
| 271 | // Encoder will fetch tx types when writing bitstream. |
| 272 | if (!dry_run) { |
| 273 | const int grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col); |
| 274 | uint8_t *const tx_type_map = mi_params->tx_type_map + grid_idx; |
| 275 | const int mi_stride = mi_params->mi_stride; |
| 276 | for (int blk_row = 0; blk_row < bh; ++blk_row) { |
| 277 | av1_copy_array(tx_type_map + blk_row * mi_stride, |
| 278 | xd->tx_type_map + blk_row * xd->tx_type_map_stride, bw); |
| 279 | } |
| 280 | xd->tx_type_map = tx_type_map; |
| 281 | xd->tx_type_map_stride = mi_stride; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 282 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 283 | #if CONFIG_SDP |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 284 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 285 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 286 | |
| 287 | // If segmentation in use |
| 288 | if (seg->enabled) { |
| 289 | // For in frame complexity AQ copy the segment id from the segment map. |
| 290 | if (cpi->oxcf.q_cfg.aq_mode == COMPLEXITY_AQ) { |
| 291 | const uint8_t *const map = |
| 292 | seg->update_map ? cpi->enc_seg.map : cm->last_frame_seg_map; |
| 293 | mi_addr->segment_id = |
| 294 | map ? get_segment_id(mi_params, map, bsize, mi_row, mi_col) : 0; |
| 295 | reset_tx_size(x, mi_addr, x->txfm_search_params.tx_mode_search_type); |
| 296 | } |
| 297 | // Else for cyclic refresh mode update the segment map, set the segment id |
| 298 | // and then update the quantizer. |
leolzhao | aa4d769 | 2021-01-28 11:00:33 -0800 | [diff] [blame] | 299 | #if CONFIG_SDP |
| 300 | if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && |
| 301 | xd->tree_type == SHARED_PART) { |
| 302 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 303 | if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) { |
leolzhao | aa4d769 | 2021-01-28 11:00:33 -0800 | [diff] [blame] | 304 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 305 | av1_cyclic_refresh_update_segment(cpi, mi_addr, mi_row, mi_col, bsize, |
| 306 | ctx->rd_stats.rate, ctx->rd_stats.dist, |
| 307 | txfm_info->skip_txfm); |
| 308 | } |
| 309 | if (mi_addr->uv_mode == UV_CFL_PRED && !is_cfl_allowed(xd)) |
| 310 | mi_addr->uv_mode = UV_DC_PRED; |
| 311 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 312 | #if CONFIG_SDP |
| 313 | for (i = (xd->tree_type == CHROMA_PART); i < num_planes; ++i) { |
| 314 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 315 | for (i = 0; i < num_planes; ++i) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 316 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 317 | p[i].coeff = ctx->coeff[i]; |
| 318 | p[i].qcoeff = ctx->qcoeff[i]; |
| 319 | p[i].dqcoeff = ctx->dqcoeff[i]; |
| 320 | p[i].eobs = ctx->eobs[i]; |
| 321 | p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i]; |
| 322 | } |
| 323 | for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i]; |
| 324 | // Restore the coding context of the MB to that that was in place |
| 325 | // when the mode was picked for it |
| 326 | for (y = 0; y < mi_height; y++) { |
| 327 | for (x_idx = 0; x_idx < mi_width; x_idx++) { |
| 328 | if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx && |
| 329 | (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 330 | #if CONFIG_SDP |
| 331 | const int mi_idx = |
| 332 | get_alloc_mi_idx(mi_params, mi_row + y, mi_col + x_idx); |
| 333 | xd->mi[x_idx + y * mis] = &mi_params->mi_alloc[mi_idx]; |
| 334 | if (xd->tree_type == LUMA_PART) { |
| 335 | *(xd->mi[x_idx + y * mis]) = *mi_addr; |
| 336 | } else if (xd->tree_type == CHROMA_PART) { |
| 337 | xd->mi[x_idx + y * mis]->sb_type[PLANE_TYPE_UV] = |
| 338 | mi_addr->sb_type[PLANE_TYPE_UV]; |
| 339 | xd->mi[x_idx + y * mis]->uv_mode = mi_addr->uv_mode; |
| 340 | xd->mi[x_idx + y * mis]->angle_delta[PLANE_TYPE_UV] = |
| 341 | mi_addr->angle_delta[PLANE_TYPE_UV]; |
| 342 | xd->mi[x_idx + y * mis]->cfl_alpha_signs = mi_addr->cfl_alpha_signs; |
| 343 | xd->mi[x_idx + y * mis]->cfl_alpha_idx = mi_addr->cfl_alpha_idx; |
| 344 | xd->mi[x_idx + y * mis]->partition = mi_addr->partition; |
| 345 | xd->mi[x_idx + y * mis] |
| 346 | ->palette_mode_info.palette_size[PLANE_TYPE_UV] = |
| 347 | mi_addr->palette_mode_info.palette_size[PLANE_TYPE_UV]; |
| 348 | for (i = PALETTE_MAX_SIZE; i < 3 * PALETTE_MAX_SIZE; i++) |
| 349 | xd->mi[x_idx + y * mis]->palette_mode_info.palette_colors[i] = |
| 350 | mi_addr->palette_mode_info.palette_colors[i]; |
| 351 | } else { |
| 352 | xd->mi[x_idx + y * mis] = mi_addr; |
| 353 | } |
| 354 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 355 | xd->mi[x_idx + y * mis] = mi_addr; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 356 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (cpi->oxcf.q_cfg.aq_mode) |
| 362 | av1_init_plane_quantizers(cpi, x, mi_addr->segment_id); |
| 363 | |
| 364 | if (dry_run) return; |
| 365 | |
| 366 | #if CONFIG_INTERNAL_STATS |
| 367 | { |
| 368 | unsigned int *const mode_chosen_counts = |
| 369 | (unsigned int *)cpi->mode_chosen_counts; // Cast const away. |
| 370 | if (frame_is_intra_only(cm)) { |
| 371 | static const int kf_mode_index[] = { |
| 372 | THR_DC /*DC_PRED*/, |
| 373 | THR_V_PRED /*V_PRED*/, |
| 374 | THR_H_PRED /*H_PRED*/, |
| 375 | THR_D45_PRED /*D45_PRED*/, |
| 376 | THR_D135_PRED /*D135_PRED*/, |
| 377 | THR_D113_PRED /*D113_PRED*/, |
| 378 | THR_D157_PRED /*D157_PRED*/, |
| 379 | THR_D203_PRED /*D203_PRED*/, |
| 380 | THR_D67_PRED /*D67_PRED*/, |
| 381 | THR_SMOOTH, /*SMOOTH_PRED*/ |
| 382 | THR_SMOOTH_V, /*SMOOTH_V_PRED*/ |
| 383 | THR_SMOOTH_H, /*SMOOTH_H_PRED*/ |
| 384 | THR_PAETH /*PAETH_PRED*/, |
| 385 | }; |
| 386 | ++mode_chosen_counts[kf_mode_index[mi_addr->mode]]; |
| 387 | } else { |
| 388 | // Note how often each mode chosen as best |
| 389 | ++mode_chosen_counts[ctx->best_mode_index]; |
| 390 | } |
| 391 | } |
| 392 | #endif |
| 393 | if (!frame_is_intra_only(cm)) { |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 394 | #if CONFIG_SDP |
| 395 | if (is_inter_block(mi_addr, xd->tree_type)) { |
| 396 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 397 | if (is_inter_block(mi_addr)) { |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 398 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 399 | // TODO(sarahparker): global motion stats need to be handled per-tile |
| 400 | // to be compatible with tile-based threading. |
| 401 | update_global_motion_used(mi_addr->mode, bsize, mi_addr, rdc); |
| 402 | } |
| 403 | |
| 404 | if (cm->features.interp_filter == SWITCHABLE && |
| 405 | mi_addr->motion_mode != WARPED_CAUSAL && |
| 406 | !is_nontrans_global_motion(xd, xd->mi[0])) { |
| 407 | update_filter_type_count(td->counts, xd, mi_addr); |
| 408 | } |
| 409 | |
| 410 | rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff; |
| 411 | rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff; |
| 412 | rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff; |
| 413 | } |
| 414 | |
| 415 | const int x_mis = AOMMIN(bw, mi_params->mi_cols - mi_col); |
| 416 | const int y_mis = AOMMIN(bh, mi_params->mi_rows - mi_row); |
| 417 | if (cm->seq_params.order_hint_info.enable_ref_frame_mvs) |
| 418 | av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis); |
| 419 | } |
| 420 | |
| 421 | void av1_update_inter_mode_stats(FRAME_CONTEXT *fc, FRAME_COUNTS *counts, |
| 422 | PREDICTION_MODE mode, int16_t mode_context) { |
| 423 | (void)counts; |
Debargha Mukherjee | 0a45de8 | 2021-03-18 02:00:47 -0700 | [diff] [blame] | 424 | #if CONFIG_NEW_INTER_MODES |
| 425 | const int16_t ismode_ctx = inter_single_mode_ctx(mode_context); |
| 426 | #if CONFIG_ENTROPY_STATS |
| 427 | ++counts->inter_single_mode[ismode_ctx][mode - SINGLE_INTER_MODE_START]; |
| 428 | #endif |
| 429 | update_cdf(fc->inter_single_mode_cdf[ismode_ctx], |
| 430 | mode - SINGLE_INTER_MODE_START, INTER_SINGLE_MODES); |
| 431 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 432 | int16_t mode_ctx = mode_context & NEWMV_CTX_MASK; |
| 433 | if (mode == NEWMV) { |
| 434 | #if CONFIG_ENTROPY_STATS |
| 435 | ++counts->newmv_mode[mode_ctx][0]; |
| 436 | #endif |
| 437 | update_cdf(fc->newmv_cdf[mode_ctx], 0, 2); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | #if CONFIG_ENTROPY_STATS |
| 442 | ++counts->newmv_mode[mode_ctx][1]; |
| 443 | #endif |
| 444 | update_cdf(fc->newmv_cdf[mode_ctx], 1, 2); |
| 445 | |
| 446 | mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK; |
| 447 | if (mode == GLOBALMV) { |
| 448 | #if CONFIG_ENTROPY_STATS |
| 449 | ++counts->zeromv_mode[mode_ctx][0]; |
| 450 | #endif |
| 451 | update_cdf(fc->zeromv_cdf[mode_ctx], 0, 2); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | #if CONFIG_ENTROPY_STATS |
| 456 | ++counts->zeromv_mode[mode_ctx][1]; |
| 457 | #endif |
| 458 | update_cdf(fc->zeromv_cdf[mode_ctx], 1, 2); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 459 | mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK; |
| 460 | #if CONFIG_ENTROPY_STATS |
| 461 | ++counts->refmv_mode[mode_ctx][mode != NEARESTMV]; |
Elliott Karpilovsky | 1ad8f1d | 2021-02-01 22:27:50 -0800 | [diff] [blame] | 462 | #endif // CONFIG_ENTROPY_STATS |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 463 | update_cdf(fc->refmv_cdf[mode_ctx], mode != NEARESTMV, 2); |
Debargha Mukherjee | 0a45de8 | 2021-03-18 02:00:47 -0700 | [diff] [blame] | 464 | #endif // CONFIG_NEW_INTER_MODES |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static void update_palette_cdf(MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi, |
| 468 | FRAME_COUNTS *counts) { |
| 469 | FRAME_CONTEXT *fc = xd->tile_ctx; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 470 | #if CONFIG_SDP |
| 471 | const BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART]; |
| 472 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 473 | const BLOCK_SIZE bsize = mbmi->sb_type; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 474 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 475 | const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 476 | const int palette_bsize_ctx = av1_get_palette_bsize_ctx(bsize); |
| 477 | |
| 478 | (void)counts; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 479 | #if CONFIG_SDP |
| 480 | if (mbmi->mode == DC_PRED && xd->tree_type != CHROMA_PART) { |
| 481 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 482 | if (mbmi->mode == DC_PRED) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 483 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 484 | const int n = pmi->palette_size[0]; |
| 485 | const int palette_mode_ctx = av1_get_palette_mode_ctx(xd); |
| 486 | |
| 487 | #if CONFIG_ENTROPY_STATS |
| 488 | ++counts->palette_y_mode[palette_bsize_ctx][palette_mode_ctx][n > 0]; |
| 489 | #endif |
| 490 | update_cdf(fc->palette_y_mode_cdf[palette_bsize_ctx][palette_mode_ctx], |
| 491 | n > 0, 2); |
| 492 | if (n > 0) { |
| 493 | #if CONFIG_ENTROPY_STATS |
| 494 | ++counts->palette_y_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE]; |
| 495 | #endif |
| 496 | update_cdf(fc->palette_y_size_cdf[palette_bsize_ctx], |
| 497 | n - PALETTE_MIN_SIZE, PALETTE_SIZES); |
| 498 | } |
| 499 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 500 | #if CONFIG_SDP |
| 501 | if (mbmi->uv_mode == UV_DC_PRED && xd->tree_type != LUMA_PART) { |
| 502 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 503 | if (mbmi->uv_mode == UV_DC_PRED) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 504 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 505 | const int n = pmi->palette_size[1]; |
| 506 | const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0); |
| 507 | |
| 508 | #if CONFIG_ENTROPY_STATS |
| 509 | ++counts->palette_uv_mode[palette_uv_mode_ctx][n > 0]; |
| 510 | #endif |
| 511 | update_cdf(fc->palette_uv_mode_cdf[palette_uv_mode_ctx], n > 0, 2); |
| 512 | |
| 513 | if (n > 0) { |
| 514 | #if CONFIG_ENTROPY_STATS |
| 515 | ++counts->palette_uv_size[palette_bsize_ctx][n - PALETTE_MIN_SIZE]; |
| 516 | #endif |
| 517 | update_cdf(fc->palette_uv_size_cdf[palette_bsize_ctx], |
| 518 | n - PALETTE_MIN_SIZE, PALETTE_SIZES); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | void av1_sum_intra_stats(const AV1_COMMON *const cm, FRAME_COUNTS *counts, |
| 524 | MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi, |
| 525 | const MB_MODE_INFO *above_mi, |
| 526 | const MB_MODE_INFO *left_mi, const int intraonly) { |
| 527 | FRAME_CONTEXT *fc = xd->tile_ctx; |
| 528 | const PREDICTION_MODE y_mode = mbmi->mode; |
| 529 | (void)counts; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 530 | #if CONFIG_SDP |
| 531 | const BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART]; |
| 532 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 533 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 534 | #endif |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 535 | #if CONFIG_SDP |
| 536 | if (xd->tree_type != CHROMA_PART) { |
| 537 | #endif |
| 538 | if (intraonly) { |
| 539 | #if CONFIG_ENTROPY_STATS |
| 540 | const PREDICTION_MODE above = av1_above_block_mode(above_mi); |
| 541 | const PREDICTION_MODE left = av1_left_block_mode(left_mi); |
| 542 | const int above_ctx = intra_mode_context[above]; |
| 543 | const int left_ctx = intra_mode_context[left]; |
| 544 | ++counts->kf_y_mode[above_ctx][left_ctx][y_mode]; |
| 545 | #endif // CONFIG_ENTROPY_STATS |
| 546 | update_cdf(get_y_mode_cdf(fc, above_mi, left_mi), y_mode, INTRA_MODES); |
| 547 | } else { |
| 548 | #if CONFIG_ENTROPY_STATS |
| 549 | ++counts->y_mode[size_group_lookup[bsize]][y_mode]; |
| 550 | #endif // CONFIG_ENTROPY_STATS |
| 551 | update_cdf(fc->y_mode_cdf[size_group_lookup[bsize]], y_mode, INTRA_MODES); |
| 552 | } |
leolzhao | 943a08d | 2021-03-30 15:32:52 -0700 | [diff] [blame] | 553 | #if CONFIG_MRLS |
| 554 | if (cm->seq_params.enable_mrls && av1_is_directional_mode(mbmi->mode)) { |
| 555 | update_cdf(fc->mrl_index_cdf, mbmi->mrl_index, MRL_LINE_NUMBER); |
| 556 | } |
| 557 | #endif |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 558 | if (av1_filter_intra_allowed(cm, mbmi)) { |
| 559 | const int use_filter_intra_mode = |
| 560 | mbmi->filter_intra_mode_info.use_filter_intra; |
| 561 | #if CONFIG_ENTROPY_STATS |
| 562 | #if CONFIG_SDP |
| 563 | ++counts->filter_intra[mbmi->sb_type[xd->tree_type == CHROMA_PART]] |
| 564 | [use_filter_intra_mode]; |
| 565 | #else |
| 566 | ++counts->filter_intra[mbmi->sb_type][use_filter_intra_mode]; |
| 567 | #endif |
| 568 | if (use_filter_intra_mode) { |
| 569 | ++counts->filter_intra_mode[mbmi->filter_intra_mode_info |
| 570 | .filter_intra_mode]; |
| 571 | } |
| 572 | #endif // CONFIG_ENTROPY_STATS |
| 573 | #if CONFIG_SDP |
| 574 | update_cdf( |
| 575 | fc->filter_intra_cdfs[mbmi->sb_type[xd->tree_type == CHROMA_PART]], |
| 576 | use_filter_intra_mode, 2); |
| 577 | #else |
| 578 | update_cdf(fc->filter_intra_cdfs[mbmi->sb_type], use_filter_intra_mode, 2); |
| 579 | #endif |
| 580 | if (use_filter_intra_mode) { |
| 581 | update_cdf(fc->filter_intra_mode_cdf, |
| 582 | mbmi->filter_intra_mode_info.filter_intra_mode, |
| 583 | FILTER_INTRA_MODES); |
| 584 | } |
| 585 | } |
| 586 | if (av1_is_directional_mode(mbmi->mode) && av1_use_angle_delta(bsize)) { |
| 587 | #if CONFIG_ENTROPY_STATS |
| 588 | ++counts->angle_delta[mbmi->mode - V_PRED] |
| 589 | [mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA]; |
| 590 | #endif |
SARWER, Mohammed Golam | 4241ffe | 2021-06-08 11:06:06 -0700 | [diff] [blame] | 591 | |
| 592 | #if CONFIG_ORIP |
| 593 | int signal_intra_filter = |
leolzhao | b817d63 | 2021-06-15 18:22:05 -0700 | [diff] [blame] | 594 | av1_signal_orip_for_horver_modes(cm, mbmi, PLANE_TYPE_Y, bsize); |
SARWER, Mohammed Golam | 4241ffe | 2021-06-08 11:06:06 -0700 | [diff] [blame] | 595 | if (signal_intra_filter) { |
| 596 | #if CONFIG_SDP |
| 597 | update_cdf(fc->angle_delta_cdf_hv[PLANE_TYPE_Y][mbmi->mode - V_PRED], |
| 598 | #else |
| 599 | update_cdf(fc->angle_delta_cdf_hv[mbmi->mode - V_PRED], |
| 600 | #endif |
| 601 | get_angle_delta_to_idx(mbmi->angle_delta[PLANE_TYPE_Y]), |
| 602 | 2 * MAX_ANGLE_DELTA + 1 + ADDITIONAL_ANGLE_DELTA); |
| 603 | } else { |
| 604 | #endif |
| 605 | #if CONFIG_SDP |
| 606 | update_cdf(fc->angle_delta_cdf[PLANE_TYPE_Y][mbmi->mode - V_PRED], |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 607 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 608 | update_cdf(fc->angle_delta_cdf[mbmi->mode - V_PRED], |
SARWER, Mohammed Golam | 4241ffe | 2021-06-08 11:06:06 -0700 | [diff] [blame] | 609 | #endif |
| 610 | mbmi->angle_delta[PLANE_TYPE_Y] + MAX_ANGLE_DELTA, |
| 611 | 2 * MAX_ANGLE_DELTA + 1); |
| 612 | |
| 613 | #if CONFIG_ORIP |
| 614 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 615 | #endif |
| 616 | } |
| 617 | #if CONFIG_SDP |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 618 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 619 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 620 | |
| 621 | if (!xd->is_chroma_ref) return; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 622 | #if CONFIG_SDP |
| 623 | if (xd->tree_type != LUMA_PART) { |
| 624 | #endif |
| 625 | const UV_PREDICTION_MODE uv_mode = mbmi->uv_mode; |
| 626 | const CFL_ALLOWED_TYPE cfl_allowed = is_cfl_allowed(xd); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 627 | #if CONFIG_ENTROPY_STATS |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 628 | ++counts->uv_mode[cfl_allowed][y_mode][uv_mode]; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 629 | #endif // CONFIG_ENTROPY_STATS |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 630 | update_cdf(fc->uv_mode_cdf[cfl_allowed][y_mode], uv_mode, |
| 631 | UV_INTRA_MODES - !cfl_allowed); |
| 632 | if (uv_mode == UV_CFL_PRED) { |
| 633 | const int8_t joint_sign = mbmi->cfl_alpha_signs; |
| 634 | const uint8_t idx = mbmi->cfl_alpha_idx; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 635 | |
| 636 | #if CONFIG_ENTROPY_STATS |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 637 | ++counts->cfl_sign[joint_sign]; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 638 | #endif |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 639 | update_cdf(fc->cfl_sign_cdf, joint_sign, CFL_JOINT_SIGNS); |
| 640 | if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) { |
| 641 | aom_cdf_prob *cdf_u = fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)]; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 642 | |
| 643 | #if CONFIG_ENTROPY_STATS |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 644 | ++counts->cfl_alpha[CFL_CONTEXT_U(joint_sign)][CFL_IDX_U(idx)]; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 645 | #endif |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 646 | update_cdf(cdf_u, CFL_IDX_U(idx), CFL_ALPHABET_SIZE); |
| 647 | } |
| 648 | if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) { |
| 649 | aom_cdf_prob *cdf_v = fc->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)]; |
| 650 | |
| 651 | #if CONFIG_ENTROPY_STATS |
| 652 | ++counts->cfl_alpha[CFL_CONTEXT_V(joint_sign)][CFL_IDX_V(idx)]; |
| 653 | #endif |
| 654 | update_cdf(cdf_v, CFL_IDX_V(idx), CFL_ALPHABET_SIZE); |
| 655 | } |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 656 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 657 | if (av1_is_directional_mode(get_uv_mode(uv_mode)) && |
| 658 | av1_use_angle_delta(bsize)) { |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 659 | #if CONFIG_ENTROPY_STATS |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 660 | ++counts->angle_delta[uv_mode - UV_V_PRED] |
| 661 | [mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA]; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 662 | #endif |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 663 | #if CONFIG_SDP |
leolzhao | 0214160 | 2021-02-16 15:06:35 -0800 | [diff] [blame] | 664 | if (cm->seq_params.enable_sdp) |
| 665 | update_cdf(fc->angle_delta_cdf[PLANE_TYPE_UV][uv_mode - UV_V_PRED], |
| 666 | mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA, |
| 667 | 2 * MAX_ANGLE_DELTA + 1); |
| 668 | else |
| 669 | update_cdf(fc->angle_delta_cdf[PLANE_TYPE_Y][uv_mode - UV_V_PRED], |
| 670 | mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA, |
| 671 | 2 * MAX_ANGLE_DELTA + 1); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 672 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 673 | update_cdf(fc->angle_delta_cdf[uv_mode - UV_V_PRED], |
| 674 | mbmi->angle_delta[PLANE_TYPE_UV] + MAX_ANGLE_DELTA, |
| 675 | 2 * MAX_ANGLE_DELTA + 1); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 676 | #endif |
| 677 | } |
| 678 | #if CONFIG_SDP |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 679 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 680 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 681 | if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize)) { |
| 682 | update_palette_cdf(xd, mbmi, counts); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | void av1_restore_context(MACROBLOCK *x, const RD_SEARCH_MACROBLOCK_CONTEXT *ctx, |
| 687 | int mi_row, int mi_col, BLOCK_SIZE bsize, |
| 688 | const int num_planes) { |
| 689 | MACROBLOCKD *xd = &x->e_mbd; |
| 690 | int p; |
| 691 | const int num_4x4_blocks_wide = mi_size_wide[bsize]; |
| 692 | const int num_4x4_blocks_high = mi_size_high[bsize]; |
| 693 | int mi_width = mi_size_wide[bsize]; |
| 694 | int mi_height = mi_size_high[bsize]; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 695 | #if CONFIG_SDP |
| 696 | for (p = (xd->tree_type == CHROMA_PART); p < num_planes; p++) { |
| 697 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 698 | for (p = 0; p < num_planes; p++) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 699 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 700 | int tx_col = mi_col; |
| 701 | int tx_row = mi_row & MAX_MIB_MASK; |
| 702 | memcpy( |
| 703 | xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x), |
| 704 | ctx->a + num_4x4_blocks_wide * p, |
| 705 | (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >> |
| 706 | xd->plane[p].subsampling_x); |
| 707 | memcpy(xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y), |
| 708 | ctx->l + num_4x4_blocks_high * p, |
| 709 | (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >> |
| 710 | xd->plane[p].subsampling_y); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 711 | #if CONFIG_SDP |
| 712 | memcpy(xd->above_partition_context[p] + mi_col, ctx->sa + mi_width * p, |
| 713 | sizeof(*xd->above_partition_context[p]) * mi_width); |
| 714 | memcpy(xd->left_partition_context[p] + (mi_row & MAX_MIB_MASK), |
| 715 | ctx->sl + mi_height * p, |
| 716 | sizeof(xd->left_partition_context[p][0]) * mi_height); |
| 717 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 718 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 719 | #if !CONFIG_SDP |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 720 | memcpy(xd->above_partition_context + mi_col, ctx->sa, |
| 721 | sizeof(*xd->above_partition_context) * mi_width); |
| 722 | memcpy(xd->left_partition_context + (mi_row & MAX_MIB_MASK), ctx->sl, |
| 723 | sizeof(xd->left_partition_context[0]) * mi_height); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 724 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 725 | xd->above_txfm_context = ctx->p_ta; |
| 726 | xd->left_txfm_context = ctx->p_tl; |
| 727 | memcpy(xd->above_txfm_context, ctx->ta, |
| 728 | sizeof(*xd->above_txfm_context) * mi_width); |
| 729 | memcpy(xd->left_txfm_context, ctx->tl, |
| 730 | sizeof(*xd->left_txfm_context) * mi_height); |
| 731 | } |
| 732 | |
| 733 | void av1_save_context(const MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *ctx, |
| 734 | int mi_row, int mi_col, BLOCK_SIZE bsize, |
| 735 | const int num_planes) { |
| 736 | const MACROBLOCKD *xd = &x->e_mbd; |
| 737 | int p; |
| 738 | int mi_width = mi_size_wide[bsize]; |
| 739 | int mi_height = mi_size_high[bsize]; |
| 740 | |
| 741 | // buffer the above/left context information of the block in search. |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 742 | #if CONFIG_SDP |
| 743 | for (p = (xd->tree_type == CHROMA_PART); p < num_planes; ++p) { |
| 744 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 745 | for (p = 0; p < num_planes; ++p) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 746 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 747 | int tx_col = mi_col; |
| 748 | int tx_row = mi_row & MAX_MIB_MASK; |
| 749 | memcpy( |
| 750 | ctx->a + mi_width * p, |
| 751 | xd->above_entropy_context[p] + (tx_col >> xd->plane[p].subsampling_x), |
| 752 | (sizeof(ENTROPY_CONTEXT) * mi_width) >> xd->plane[p].subsampling_x); |
| 753 | memcpy(ctx->l + mi_height * p, |
| 754 | xd->left_entropy_context[p] + (tx_row >> xd->plane[p].subsampling_y), |
| 755 | (sizeof(ENTROPY_CONTEXT) * mi_height) >> xd->plane[p].subsampling_y); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 756 | #if CONFIG_SDP |
| 757 | memcpy(ctx->sa + mi_width * p, xd->above_partition_context[p] + mi_col, |
| 758 | sizeof(*xd->above_partition_context[p]) * mi_width); |
| 759 | memcpy(ctx->sl + mi_height * p, |
| 760 | xd->left_partition_context[p] + (mi_row & MAX_MIB_MASK), |
| 761 | sizeof(xd->left_partition_context[p][0]) * mi_height); |
| 762 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 763 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 764 | #if !CONFIG_SDP |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 765 | memcpy(ctx->sa, xd->above_partition_context + mi_col, |
| 766 | sizeof(*xd->above_partition_context) * mi_width); |
| 767 | memcpy(ctx->sl, xd->left_partition_context + (mi_row & MAX_MIB_MASK), |
| 768 | sizeof(xd->left_partition_context[0]) * mi_height); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 769 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 770 | memcpy(ctx->ta, xd->above_txfm_context, |
| 771 | sizeof(*xd->above_txfm_context) * mi_width); |
| 772 | memcpy(ctx->tl, xd->left_txfm_context, |
| 773 | sizeof(*xd->left_txfm_context) * mi_height); |
| 774 | ctx->p_ta = xd->above_txfm_context; |
| 775 | ctx->p_tl = xd->left_txfm_context; |
| 776 | } |
| 777 | |
| 778 | static void set_partial_sb_partition(const AV1_COMMON *const cm, |
| 779 | MB_MODE_INFO *mi, int bh_in, int bw_in, |
| 780 | int mi_rows_remaining, |
| 781 | int mi_cols_remaining, BLOCK_SIZE bsize, |
| 782 | MB_MODE_INFO **mib) { |
| 783 | int bh = bh_in; |
| 784 | int r, c; |
| 785 | for (r = 0; r < cm->seq_params.mib_size; r += bh) { |
| 786 | int bw = bw_in; |
| 787 | for (c = 0; c < cm->seq_params.mib_size; c += bw) { |
| 788 | const int grid_index = get_mi_grid_idx(&cm->mi_params, r, c); |
| 789 | const int mi_index = get_alloc_mi_idx(&cm->mi_params, r, c); |
| 790 | mib[grid_index] = mi + mi_index; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 791 | #if CONFIG_SDP |
leolzhao | aa4d769 | 2021-01-28 11:00:33 -0800 | [diff] [blame] | 792 | mib[grid_index]->sb_type[PLANE_TYPE_Y] = |
| 793 | mib[grid_index]->sb_type[PLANE_TYPE_UV] = find_partition_size( |
| 794 | bsize, mi_rows_remaining - r, mi_cols_remaining - c, &bh, &bw); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 795 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 796 | mib[grid_index]->sb_type = find_partition_size( |
| 797 | bsize, mi_rows_remaining - r, mi_cols_remaining - c, &bh, &bw); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 798 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | // This function attempts to set all mode info entries in a given superblock |
| 804 | // to the same block partition size. |
| 805 | // However, at the bottom and right borders of the image the requested size |
| 806 | // may not be allowed in which case this code attempts to choose the largest |
| 807 | // allowable partition. |
| 808 | void av1_set_fixed_partitioning(AV1_COMP *cpi, const TileInfo *const tile, |
| 809 | MB_MODE_INFO **mib, int mi_row, int mi_col, |
| 810 | BLOCK_SIZE bsize) { |
| 811 | AV1_COMMON *const cm = &cpi->common; |
| 812 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
| 813 | const int mi_rows_remaining = tile->mi_row_end - mi_row; |
| 814 | const int mi_cols_remaining = tile->mi_col_end - mi_col; |
| 815 | MB_MODE_INFO *const mi_upper_left = |
| 816 | mi_params->mi_alloc + get_alloc_mi_idx(mi_params, mi_row, mi_col); |
| 817 | int bh = mi_size_high[bsize]; |
| 818 | int bw = mi_size_wide[bsize]; |
| 819 | |
| 820 | assert(bsize >= mi_params->mi_alloc_bsize && |
| 821 | "Attempted to use bsize < mi_params->mi_alloc_bsize"); |
| 822 | assert((mi_rows_remaining > 0) && (mi_cols_remaining > 0)); |
| 823 | |
| 824 | // Apply the requested partition size to the SB if it is all "in image" |
| 825 | if ((mi_cols_remaining >= cm->seq_params.mib_size) && |
| 826 | (mi_rows_remaining >= cm->seq_params.mib_size)) { |
| 827 | for (int block_row = 0; block_row < cm->seq_params.mib_size; |
| 828 | block_row += bh) { |
| 829 | for (int block_col = 0; block_col < cm->seq_params.mib_size; |
| 830 | block_col += bw) { |
| 831 | const int grid_index = get_mi_grid_idx(mi_params, block_row, block_col); |
| 832 | const int mi_index = get_alloc_mi_idx(mi_params, block_row, block_col); |
| 833 | mib[grid_index] = mi_upper_left + mi_index; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 834 | #if CONFIG_SDP |
leolzhao | aa4d769 | 2021-01-28 11:00:33 -0800 | [diff] [blame] | 835 | mib[grid_index]->sb_type[PLANE_TYPE_Y] = bsize; |
| 836 | mib[grid_index]->sb_type[PLANE_TYPE_UV] = bsize; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 837 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 838 | mib[grid_index]->sb_type = bsize; |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 839 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | } else { |
| 843 | // Else this is a partial SB. |
| 844 | set_partial_sb_partition(cm, mi_upper_left, bh, bw, mi_rows_remaining, |
| 845 | mi_cols_remaining, bsize, mib); |
| 846 | } |
| 847 | } |
leolzhao | 3db7cca | 2021-01-26 16:53:07 -0800 | [diff] [blame] | 848 | #if CONFIG_SDP |
| 849 | int av1_is_leaf_split_partition(AV1_COMMON *cm, MACROBLOCKD *const xd, |
| 850 | int mi_row, int mi_col, |
| 851 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 852 | int av1_is_leaf_split_partition(AV1_COMMON *cm, int mi_row, int mi_col, |
leolzhao | 3db7cca | 2021-01-26 16:53:07 -0800 | [diff] [blame] | 853 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 854 | BLOCK_SIZE bsize) { |
| 855 | const int bs = mi_size_wide[bsize]; |
| 856 | const int hbs = bs / 2; |
| 857 | assert(bsize >= BLOCK_8X8); |
| 858 | const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT); |
| 859 | |
| 860 | for (int i = 0; i < 4; i++) { |
| 861 | int x_idx = (i & 1) * hbs; |
| 862 | int y_idx = (i >> 1) * hbs; |
| 863 | if ((mi_row + y_idx >= cm->mi_params.mi_rows) || |
| 864 | (mi_col + x_idx >= cm->mi_params.mi_cols)) |
| 865 | return 0; |
leolzhao | 3db7cca | 2021-01-26 16:53:07 -0800 | [diff] [blame] | 866 | #if CONFIG_SDP |
| 867 | if (get_partition(cm, xd->tree_type == CHROMA_PART, mi_row + y_idx, |
| 868 | mi_col + x_idx, subsize) != |
| 869 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 870 | if (get_partition(cm, mi_row + y_idx, mi_col + x_idx, subsize) != |
leolzhao | 3db7cca | 2021-01-26 16:53:07 -0800 | [diff] [blame] | 871 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 872 | PARTITION_NONE && |
| 873 | subsize != BLOCK_8X8) |
| 874 | return 0; |
| 875 | } |
| 876 | return 1; |
| 877 | } |
| 878 | |
Jayasanker J | 37596eb | 2020-08-20 16:39:40 +0530 | [diff] [blame] | 879 | int av1_get_rdmult_delta(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row, |
| 880 | int mi_col, int orig_rdmult) { |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 881 | AV1_COMMON *const cm = &cpi->common; |
| 882 | const GF_GROUP *const gf_group = &cpi->gf_group; |
| 883 | assert(IMPLIES(cpi->gf_group.size > 0, |
| 884 | cpi->gf_group.index < cpi->gf_group.size)); |
| 885 | const int tpl_idx = cpi->gf_group.index; |
| 886 | TplParams *const tpl_data = &cpi->tpl_data; |
| 887 | TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx]; |
| 888 | TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr; |
| 889 | const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2; |
| 890 | int tpl_stride = tpl_frame->stride; |
| 891 | int64_t intra_cost = 0; |
| 892 | int64_t mc_dep_cost = 0; |
| 893 | const int mi_wide = mi_size_wide[bsize]; |
| 894 | const int mi_high = mi_size_high[bsize]; |
| 895 | |
| 896 | if (tpl_frame->is_valid == 0) return orig_rdmult; |
| 897 | |
Deepa K G | 21e5e8e | 2020-03-28 13:26:09 +0530 | [diff] [blame] | 898 | if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return orig_rdmult; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 899 | |
| 900 | if (cpi->gf_group.index >= MAX_TPL_FRAME_IDX) return orig_rdmult; |
| 901 | |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 902 | int mi_count = 0; |
| 903 | const int mi_col_sr = |
| 904 | coded_to_superres_mi(mi_col, cm->superres_scale_denominator); |
| 905 | const int mi_col_end_sr = |
| 906 | coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator); |
| 907 | const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width); |
| 908 | const int step = 1 << block_mis_log2; |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 909 | const int row_step = step; |
| 910 | const int col_step_sr = |
| 911 | coded_to_superres_mi(step, cm->superres_scale_denominator); |
| 912 | for (int row = mi_row; row < mi_row + mi_high; row += row_step) { |
| 913 | for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) { |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 914 | if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue; |
| 915 | TplDepStats *this_stats = |
| 916 | &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)]; |
| 917 | int64_t mc_dep_delta = |
| 918 | RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate, |
| 919 | this_stats->mc_dep_dist); |
| 920 | intra_cost += this_stats->recrf_dist << RDDIV_BITS; |
| 921 | mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 922 | mi_count++; |
| 923 | } |
| 924 | } |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 925 | assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 926 | |
| 927 | aom_clear_system_state(); |
| 928 | |
| 929 | double beta = 1.0; |
Jayasanker J | 37596eb | 2020-08-20 16:39:40 +0530 | [diff] [blame] | 930 | if (mc_dep_cost > 0 && intra_cost > 0) { |
| 931 | const double r0 = cpi->rd.r0; |
| 932 | const double rk = (double)intra_cost / mc_dep_cost; |
| 933 | beta = (r0 / rk); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | int rdmult = av1_get_adaptive_rdmult(cpi, beta); |
| 937 | |
| 938 | aom_clear_system_state(); |
| 939 | |
| 940 | rdmult = AOMMIN(rdmult, orig_rdmult * 3 / 2); |
| 941 | rdmult = AOMMAX(rdmult, orig_rdmult * 1 / 2); |
| 942 | |
| 943 | rdmult = AOMMAX(1, rdmult); |
| 944 | |
| 945 | return rdmult; |
| 946 | } |
| 947 | |
| 948 | // Checks to see if a super block is on a horizontal image edge. |
| 949 | // In most cases this is the "real" edge unless there are formatting |
| 950 | // bars embedded in the stream. |
| 951 | int av1_active_h_edge(const AV1_COMP *cpi, int mi_row, int mi_step) { |
| 952 | int top_edge = 0; |
| 953 | int bottom_edge = cpi->common.mi_params.mi_rows; |
| 954 | int is_active_h_edge = 0; |
| 955 | |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 956 | if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) || |
| 957 | ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) { |
| 958 | is_active_h_edge = 1; |
| 959 | } |
| 960 | return is_active_h_edge; |
| 961 | } |
| 962 | |
| 963 | // Checks to see if a super block is on a vertical image edge. |
| 964 | // In most cases this is the "real" edge unless there are formatting |
| 965 | // bars embedded in the stream. |
| 966 | int av1_active_v_edge(const AV1_COMP *cpi, int mi_col, int mi_step) { |
| 967 | int left_edge = 0; |
| 968 | int right_edge = cpi->common.mi_params.mi_cols; |
| 969 | int is_active_v_edge = 0; |
| 970 | |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 971 | if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) || |
| 972 | ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) { |
| 973 | is_active_v_edge = 1; |
| 974 | } |
| 975 | return is_active_v_edge; |
| 976 | } |
| 977 | |
| 978 | void av1_get_tpl_stats_sb(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row, |
| 979 | int mi_col, SuperBlockEnc *sb_enc) { |
| 980 | sb_enc->tpl_data_count = 0; |
| 981 | |
| 982 | if (!cpi->oxcf.algo_cfg.enable_tpl_model) return; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 983 | if (cpi->common.current_frame.frame_type == KEY_FRAME) return; |
| 984 | const FRAME_UPDATE_TYPE update_type = get_frame_update_type(&cpi->gf_group); |
Debargha Mukherjee | 5f64acd | 2020-08-18 14:32:28 -0700 | [diff] [blame] | 985 | if (update_type == INTNL_OVERLAY_UPDATE || update_type == OVERLAY_UPDATE || |
| 986 | update_type == KFFLT_OVERLAY_UPDATE) |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 987 | return; |
| 988 | assert(IMPLIES(cpi->gf_group.size > 0, |
| 989 | cpi->gf_group.index < cpi->gf_group.size)); |
| 990 | |
| 991 | AV1_COMMON *const cm = &cpi->common; |
| 992 | const int gf_group_index = cpi->gf_group.index; |
| 993 | TplParams *const tpl_data = &cpi->tpl_data; |
| 994 | TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_group_index]; |
| 995 | TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr; |
| 996 | int tpl_stride = tpl_frame->stride; |
| 997 | const int mi_wide = mi_size_wide[bsize]; |
| 998 | const int mi_high = mi_size_high[bsize]; |
| 999 | |
| 1000 | if (tpl_frame->is_valid == 0) return; |
| 1001 | if (gf_group_index >= MAX_TPL_FRAME_IDX) return; |
| 1002 | |
| 1003 | int mi_count = 0; |
| 1004 | int count = 0; |
| 1005 | const int mi_col_sr = |
| 1006 | coded_to_superres_mi(mi_col, cm->superres_scale_denominator); |
| 1007 | const int mi_col_end_sr = |
| 1008 | coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator); |
| 1009 | // mi_cols_sr is mi_cols at superres case. |
| 1010 | const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width); |
| 1011 | |
| 1012 | // TPL store unit size is not the same as the motion estimation unit size. |
| 1013 | // Here always use motion estimation size to avoid getting repetitive inter/ |
| 1014 | // intra cost. |
Yunqing Wang | f0a8cf4 | 2020-08-14 14:50:33 -0700 | [diff] [blame] | 1015 | const BLOCK_SIZE tpl_bsize = convert_length_to_bsize(tpl_data->tpl_bsize_1d); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1016 | assert(mi_size_wide[tpl_bsize] == mi_size_high[tpl_bsize]); |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 1017 | const int row_step = mi_size_high[tpl_bsize]; |
| 1018 | const int col_step_sr = coded_to_superres_mi(mi_size_wide[tpl_bsize], |
| 1019 | cm->superres_scale_denominator); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1020 | |
| 1021 | // Stride is only based on SB size, and we fill in values for every 16x16 |
| 1022 | // block in a SB. |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 1023 | sb_enc->tpl_stride = (mi_col_end_sr - mi_col_sr) / col_step_sr; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1024 | |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 1025 | for (int row = mi_row; row < mi_row + mi_high; row += row_step) { |
| 1026 | for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) { |
| 1027 | assert(count < MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1028 | // Handle partial SB, so that no invalid values are used later. |
| 1029 | if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) { |
| 1030 | sb_enc->tpl_inter_cost[count] = INT64_MAX; |
| 1031 | sb_enc->tpl_intra_cost[count] = INT64_MAX; |
| 1032 | for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) { |
| 1033 | sb_enc->tpl_mv[count][i].as_int = INVALID_MV; |
| 1034 | } |
| 1035 | count++; |
| 1036 | continue; |
| 1037 | } |
| 1038 | |
| 1039 | TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos( |
| 1040 | row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)]; |
| 1041 | sb_enc->tpl_inter_cost[count] = this_stats->inter_cost; |
| 1042 | sb_enc->tpl_intra_cost[count] = this_stats->intra_cost; |
| 1043 | memcpy(sb_enc->tpl_mv[count], this_stats->mv, sizeof(this_stats->mv)); |
| 1044 | mi_count++; |
| 1045 | count++; |
| 1046 | } |
| 1047 | } |
| 1048 | |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 1049 | assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1050 | sb_enc->tpl_data_count = mi_count; |
| 1051 | } |
| 1052 | |
| 1053 | // analysis_type 0: Use mc_dep_cost and intra_cost |
| 1054 | // analysis_type 1: Use count of best inter predictor chosen |
| 1055 | // analysis_type 2: Use cost reduction from intra to inter for best inter |
| 1056 | // predictor chosen |
| 1057 | int av1_get_q_for_deltaq_objective(AV1_COMP *const cpi, BLOCK_SIZE bsize, |
| 1058 | int mi_row, int mi_col) { |
| 1059 | AV1_COMMON *const cm = &cpi->common; |
| 1060 | const GF_GROUP *const gf_group = &cpi->gf_group; |
| 1061 | assert(IMPLIES(cpi->gf_group.size > 0, |
| 1062 | cpi->gf_group.index < cpi->gf_group.size)); |
| 1063 | const int tpl_idx = cpi->gf_group.index; |
| 1064 | TplParams *const tpl_data = &cpi->tpl_data; |
| 1065 | TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx]; |
| 1066 | TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr; |
| 1067 | const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2; |
| 1068 | int tpl_stride = tpl_frame->stride; |
| 1069 | int64_t intra_cost = 0; |
| 1070 | int64_t mc_dep_cost = 0; |
| 1071 | const int mi_wide = mi_size_wide[bsize]; |
| 1072 | const int mi_high = mi_size_high[bsize]; |
| 1073 | const int base_qindex = cm->quant_params.base_qindex; |
| 1074 | |
| 1075 | if (tpl_frame->is_valid == 0) return base_qindex; |
| 1076 | |
Deepa K G | 21e5e8e | 2020-03-28 13:26:09 +0530 | [diff] [blame] | 1077 | if (!is_frame_tpl_eligible(gf_group, gf_group->index)) return base_qindex; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1078 | |
| 1079 | if (cpi->gf_group.index >= MAX_TPL_FRAME_IDX) return base_qindex; |
| 1080 | |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1081 | int mi_count = 0; |
| 1082 | const int mi_col_sr = |
| 1083 | coded_to_superres_mi(mi_col, cm->superres_scale_denominator); |
| 1084 | const int mi_col_end_sr = |
| 1085 | coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator); |
| 1086 | const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width); |
| 1087 | const int step = 1 << block_mis_log2; |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 1088 | const int row_step = step; |
| 1089 | const int col_step_sr = |
| 1090 | coded_to_superres_mi(step, cm->superres_scale_denominator); |
| 1091 | for (int row = mi_row; row < mi_row + mi_high; row += row_step) { |
| 1092 | for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) { |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1093 | if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue; |
| 1094 | TplDepStats *this_stats = |
| 1095 | &tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)]; |
| 1096 | int64_t mc_dep_delta = |
| 1097 | RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate, |
| 1098 | this_stats->mc_dep_dist); |
| 1099 | intra_cost += this_stats->recrf_dist << RDDIV_BITS; |
| 1100 | mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta; |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1101 | mi_count++; |
| 1102 | } |
| 1103 | } |
Urvang Joshi | e198bf1 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 1104 | assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1105 | |
| 1106 | aom_clear_system_state(); |
| 1107 | |
| 1108 | int offset = 0; |
| 1109 | double beta = 1.0; |
| 1110 | if (mc_dep_cost > 0 && intra_cost > 0) { |
| 1111 | const double r0 = cpi->rd.r0; |
| 1112 | const double rk = (double)intra_cost / mc_dep_cost; |
| 1113 | beta = (r0 / rk); |
| 1114 | assert(beta > 0.0); |
| 1115 | } |
| 1116 | offset = av1_get_deltaq_offset(cpi, base_qindex, beta); |
| 1117 | aom_clear_system_state(); |
| 1118 | |
| 1119 | const DeltaQInfo *const delta_q_info = &cm->delta_q_info; |
| 1120 | offset = AOMMIN(offset, delta_q_info->delta_q_res * 9 - 1); |
| 1121 | offset = AOMMAX(offset, -delta_q_info->delta_q_res * 9 + 1); |
| 1122 | int qindex = cm->quant_params.base_qindex + offset; |
Ryan Lei | ccc6ea7 | 2021-01-06 11:43:56 -0800 | [diff] [blame] | 1123 | #if CONFIG_EXTQUANT |
Vibhoothi | 41c6dd7 | 2021-10-12 18:48:26 +0000 | [diff] [blame] | 1124 | qindex = |
| 1125 | AOMMIN(qindex, cm->seq_params.bit_depth == AOM_BITS_8 ? MAXQ_8_BITS |
| 1126 | : cm->seq_params.bit_depth == AOM_BITS_10 ? MAXQ_10_BITS |
| 1127 | : MAXQ); |
Ryan Lei | ccc6ea7 | 2021-01-06 11:43:56 -0800 | [diff] [blame] | 1128 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1129 | qindex = AOMMIN(qindex, MAXQ); |
Ryan Lei | ccc6ea7 | 2021-01-06 11:43:56 -0800 | [diff] [blame] | 1130 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1131 | qindex = AOMMAX(qindex, MINQ); |
| 1132 | |
| 1133 | return qindex; |
| 1134 | } |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1135 | |
| 1136 | void av1_reset_simple_motion_tree_partition(SIMPLE_MOTION_DATA_TREE *sms_tree, |
| 1137 | BLOCK_SIZE bsize) { |
| 1138 | sms_tree->partitioning = PARTITION_NONE; |
| 1139 | |
| 1140 | if (bsize >= BLOCK_8X8) { |
| 1141 | BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT); |
| 1142 | for (int idx = 0; idx < 4; ++idx) |
| 1143 | av1_reset_simple_motion_tree_partition(sms_tree->split[idx], subsize); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | // Record the ref frames that have been selected by square partition blocks. |
| 1148 | void av1_update_picked_ref_frames_mask(MACROBLOCK *const x, int ref_type, |
| 1149 | BLOCK_SIZE bsize, int mib_size, |
| 1150 | int mi_row, int mi_col) { |
| 1151 | assert(mi_size_wide[bsize] == mi_size_high[bsize]); |
| 1152 | const int sb_size_mask = mib_size - 1; |
| 1153 | const int mi_row_in_sb = mi_row & sb_size_mask; |
| 1154 | const int mi_col_in_sb = mi_col & sb_size_mask; |
| 1155 | const int mi_size = mi_size_wide[bsize]; |
| 1156 | for (int i = mi_row_in_sb; i < mi_row_in_sb + mi_size; ++i) { |
| 1157 | for (int j = mi_col_in_sb; j < mi_col_in_sb + mi_size; ++j) { |
| 1158 | x->picked_ref_frames_mask[i * 32 + j] |= 1 << ref_type; |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | static void avg_cdf_symbol(aom_cdf_prob *cdf_ptr_left, aom_cdf_prob *cdf_ptr_tr, |
| 1164 | int num_cdfs, int cdf_stride, int nsymbs, |
| 1165 | int wt_left, int wt_tr) { |
| 1166 | for (int i = 0; i < num_cdfs; i++) { |
| 1167 | for (int j = 0; j <= nsymbs; j++) { |
| 1168 | cdf_ptr_left[i * cdf_stride + j] = |
| 1169 | (aom_cdf_prob)(((int)cdf_ptr_left[i * cdf_stride + j] * wt_left + |
| 1170 | (int)cdf_ptr_tr[i * cdf_stride + j] * wt_tr + |
| 1171 | ((wt_left + wt_tr) / 2)) / |
| 1172 | (wt_left + wt_tr)); |
| 1173 | assert(cdf_ptr_left[i * cdf_stride + j] >= 0 && |
| 1174 | cdf_ptr_left[i * cdf_stride + j] < CDF_PROB_TOP); |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | #define AVERAGE_CDF(cname_left, cname_tr, nsymbs) \ |
| 1180 | AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, CDF_SIZE(nsymbs)) |
| 1181 | |
| 1182 | #define AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, cdf_stride) \ |
| 1183 | do { \ |
| 1184 | aom_cdf_prob *cdf_ptr_left = (aom_cdf_prob *)cname_left; \ |
| 1185 | aom_cdf_prob *cdf_ptr_tr = (aom_cdf_prob *)cname_tr; \ |
| 1186 | int array_size = (int)sizeof(cname_left) / sizeof(aom_cdf_prob); \ |
| 1187 | int num_cdfs = array_size / cdf_stride; \ |
| 1188 | avg_cdf_symbol(cdf_ptr_left, cdf_ptr_tr, num_cdfs, cdf_stride, nsymbs, \ |
| 1189 | wt_left, wt_tr); \ |
| 1190 | } while (0) |
| 1191 | |
| 1192 | static void avg_nmv(nmv_context *nmv_left, nmv_context *nmv_tr, int wt_left, |
| 1193 | int wt_tr) { |
| 1194 | AVERAGE_CDF(nmv_left->joints_cdf, nmv_tr->joints_cdf, 4); |
| 1195 | for (int i = 0; i < 2; i++) { |
| 1196 | AVERAGE_CDF(nmv_left->comps[i].classes_cdf, nmv_tr->comps[i].classes_cdf, |
| 1197 | MV_CLASSES); |
| 1198 | AVERAGE_CDF(nmv_left->comps[i].class0_fp_cdf, |
| 1199 | nmv_tr->comps[i].class0_fp_cdf, MV_FP_SIZE); |
| 1200 | AVERAGE_CDF(nmv_left->comps[i].fp_cdf, nmv_tr->comps[i].fp_cdf, MV_FP_SIZE); |
| 1201 | AVERAGE_CDF(nmv_left->comps[i].sign_cdf, nmv_tr->comps[i].sign_cdf, 2); |
| 1202 | AVERAGE_CDF(nmv_left->comps[i].class0_hp_cdf, |
| 1203 | nmv_tr->comps[i].class0_hp_cdf, 2); |
| 1204 | AVERAGE_CDF(nmv_left->comps[i].hp_cdf, nmv_tr->comps[i].hp_cdf, 2); |
| 1205 | AVERAGE_CDF(nmv_left->comps[i].class0_cdf, nmv_tr->comps[i].class0_cdf, |
| 1206 | CLASS0_SIZE); |
| 1207 | AVERAGE_CDF(nmv_left->comps[i].bits_cdf, nmv_tr->comps[i].bits_cdf, 2); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | // In case of row-based multi-threading of encoder, since we always |
| 1212 | // keep a top - right sync, we can average the top - right SB's CDFs and |
| 1213 | // the left SB's CDFs and use the same for current SB's encoding to |
| 1214 | // improve the performance. This function facilitates the averaging |
| 1215 | // of CDF and used only when row-mt is enabled in encoder. |
| 1216 | void av1_avg_cdf_symbols(FRAME_CONTEXT *ctx_left, FRAME_CONTEXT *ctx_tr, |
| 1217 | int wt_left, int wt_tr) { |
| 1218 | AVERAGE_CDF(ctx_left->txb_skip_cdf, ctx_tr->txb_skip_cdf, 2); |
| 1219 | AVERAGE_CDF(ctx_left->eob_extra_cdf, ctx_tr->eob_extra_cdf, 2); |
| 1220 | AVERAGE_CDF(ctx_left->dc_sign_cdf, ctx_tr->dc_sign_cdf, 2); |
| 1221 | AVERAGE_CDF(ctx_left->eob_flag_cdf16, ctx_tr->eob_flag_cdf16, 5); |
| 1222 | AVERAGE_CDF(ctx_left->eob_flag_cdf32, ctx_tr->eob_flag_cdf32, 6); |
| 1223 | AVERAGE_CDF(ctx_left->eob_flag_cdf64, ctx_tr->eob_flag_cdf64, 7); |
| 1224 | AVERAGE_CDF(ctx_left->eob_flag_cdf128, ctx_tr->eob_flag_cdf128, 8); |
| 1225 | AVERAGE_CDF(ctx_left->eob_flag_cdf256, ctx_tr->eob_flag_cdf256, 9); |
| 1226 | AVERAGE_CDF(ctx_left->eob_flag_cdf512, ctx_tr->eob_flag_cdf512, 10); |
| 1227 | AVERAGE_CDF(ctx_left->eob_flag_cdf1024, ctx_tr->eob_flag_cdf1024, 11); |
| 1228 | AVERAGE_CDF(ctx_left->coeff_base_eob_cdf, ctx_tr->coeff_base_eob_cdf, 3); |
| 1229 | AVERAGE_CDF(ctx_left->coeff_base_cdf, ctx_tr->coeff_base_cdf, 4); |
| 1230 | AVERAGE_CDF(ctx_left->coeff_br_cdf, ctx_tr->coeff_br_cdf, BR_CDF_SIZE); |
Elliott Karpilovsky | 1ad8f1d | 2021-02-01 22:27:50 -0800 | [diff] [blame] | 1231 | #if CONFIG_NEW_INTER_MODES |
Debargha Mukherjee | 0a45de8 | 2021-03-18 02:00:47 -0700 | [diff] [blame] | 1232 | AVERAGE_CDF(ctx_left->inter_single_mode_cdf, ctx_tr->inter_single_mode_cdf, |
| 1233 | INTER_SINGLE_MODES); |
Elliott Karpilovsky | 1ad8f1d | 2021-02-01 22:27:50 -0800 | [diff] [blame] | 1234 | AVERAGE_CDF(ctx_left->drl_cdf[0], ctx_tr->drl_cdf[0], 2); |
| 1235 | AVERAGE_CDF(ctx_left->drl_cdf[1], ctx_tr->drl_cdf[1], 2); |
| 1236 | AVERAGE_CDF(ctx_left->drl_cdf[2], ctx_tr->drl_cdf[2], 2); |
| 1237 | #else |
Debargha Mukherjee | 0a45de8 | 2021-03-18 02:00:47 -0700 | [diff] [blame] | 1238 | AVERAGE_CDF(ctx_left->newmv_cdf, ctx_tr->newmv_cdf, 2); |
| 1239 | AVERAGE_CDF(ctx_left->zeromv_cdf, ctx_tr->zeromv_cdf, 2); |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1240 | AVERAGE_CDF(ctx_left->refmv_cdf, ctx_tr->refmv_cdf, 2); |
| 1241 | AVERAGE_CDF(ctx_left->drl_cdf, ctx_tr->drl_cdf, 2); |
Elliott Karpilovsky | 1ad8f1d | 2021-02-01 22:27:50 -0800 | [diff] [blame] | 1242 | #endif // CONFIG_NEW_INTER_MODES |
Lester Lu | 5fb7304 | 2021-10-01 00:08:55 +0000 | [diff] [blame] | 1243 | #if CONFIG_OPTFLOW_REFINEMENT |
| 1244 | AVERAGE_CDF(ctx_left->inter_compound_mode_cdf, |
| 1245 | ctx_tr->inter_compound_mode_cdf, INTER_COMPOUND_REF_TYPES); |
| 1246 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1247 | AVERAGE_CDF(ctx_left->inter_compound_mode_cdf, |
| 1248 | ctx_tr->inter_compound_mode_cdf, INTER_COMPOUND_MODES); |
Lester Lu | 5fb7304 | 2021-10-01 00:08:55 +0000 | [diff] [blame] | 1249 | #endif // CONFIG_OPTFLOW_REFINEMENT |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1250 | AVERAGE_CDF(ctx_left->compound_type_cdf, ctx_tr->compound_type_cdf, |
| 1251 | MASKED_COMPOUND_TYPES); |
| 1252 | AVERAGE_CDF(ctx_left->wedge_idx_cdf, ctx_tr->wedge_idx_cdf, 16); |
| 1253 | AVERAGE_CDF(ctx_left->interintra_cdf, ctx_tr->interintra_cdf, 2); |
| 1254 | AVERAGE_CDF(ctx_left->wedge_interintra_cdf, ctx_tr->wedge_interintra_cdf, 2); |
| 1255 | AVERAGE_CDF(ctx_left->interintra_mode_cdf, ctx_tr->interintra_mode_cdf, |
| 1256 | INTERINTRA_MODES); |
| 1257 | AVERAGE_CDF(ctx_left->motion_mode_cdf, ctx_tr->motion_mode_cdf, MOTION_MODES); |
| 1258 | AVERAGE_CDF(ctx_left->obmc_cdf, ctx_tr->obmc_cdf, 2); |
| 1259 | AVERAGE_CDF(ctx_left->palette_y_size_cdf, ctx_tr->palette_y_size_cdf, |
| 1260 | PALETTE_SIZES); |
| 1261 | AVERAGE_CDF(ctx_left->palette_uv_size_cdf, ctx_tr->palette_uv_size_cdf, |
| 1262 | PALETTE_SIZES); |
| 1263 | for (int j = 0; j < PALETTE_SIZES; j++) { |
| 1264 | int nsymbs = j + PALETTE_MIN_SIZE; |
| 1265 | AVG_CDF_STRIDE(ctx_left->palette_y_color_index_cdf[j], |
| 1266 | ctx_tr->palette_y_color_index_cdf[j], nsymbs, |
| 1267 | CDF_SIZE(PALETTE_COLORS)); |
| 1268 | AVG_CDF_STRIDE(ctx_left->palette_uv_color_index_cdf[j], |
| 1269 | ctx_tr->palette_uv_color_index_cdf[j], nsymbs, |
| 1270 | CDF_SIZE(PALETTE_COLORS)); |
| 1271 | } |
| 1272 | AVERAGE_CDF(ctx_left->palette_y_mode_cdf, ctx_tr->palette_y_mode_cdf, 2); |
| 1273 | AVERAGE_CDF(ctx_left->palette_uv_mode_cdf, ctx_tr->palette_uv_mode_cdf, 2); |
| 1274 | AVERAGE_CDF(ctx_left->comp_inter_cdf, ctx_tr->comp_inter_cdf, 2); |
| 1275 | AVERAGE_CDF(ctx_left->single_ref_cdf, ctx_tr->single_ref_cdf, 2); |
| 1276 | AVERAGE_CDF(ctx_left->comp_ref_type_cdf, ctx_tr->comp_ref_type_cdf, 2); |
| 1277 | AVERAGE_CDF(ctx_left->uni_comp_ref_cdf, ctx_tr->uni_comp_ref_cdf, 2); |
| 1278 | AVERAGE_CDF(ctx_left->comp_ref_cdf, ctx_tr->comp_ref_cdf, 2); |
| 1279 | AVERAGE_CDF(ctx_left->comp_bwdref_cdf, ctx_tr->comp_bwdref_cdf, 2); |
Sarah Parker | 4247f72 | 2021-06-16 08:31:09 -0700 | [diff] [blame] | 1280 | #if CONFIG_NEW_TX_PARTITION |
| 1281 | // Square blocks |
| 1282 | AVERAGE_CDF(ctx_left->inter_4way_txfm_partition_cdf[0], |
| 1283 | ctx_tr->inter_4way_txfm_partition_cdf[0], 4); |
| 1284 | // Rectangular blocks |
| 1285 | AVERAGE_CDF(ctx_left->inter_4way_txfm_partition_cdf[1], |
| 1286 | ctx_tr->inter_4way_txfm_partition_cdf[1], 4); |
| 1287 | AVERAGE_CDF(ctx_left->inter_2way_txfm_partition_cdf, |
| 1288 | ctx_tr->inter_2way_txfm_partition_cdf, 2); |
| 1289 | AVERAGE_CDF(ctx_left->inter_2way_rect_txfm_partition_cdf, |
| 1290 | ctx_tr->inter_2way_rect_txfm_partition_cdf, 2); |
| 1291 | #else // CONFIG_NEW_TX_PARTITION |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1292 | AVERAGE_CDF(ctx_left->txfm_partition_cdf, ctx_tr->txfm_partition_cdf, 2); |
Sarah Parker | 4247f72 | 2021-06-16 08:31:09 -0700 | [diff] [blame] | 1293 | #endif // CONFIG_NEW_TX_PARTITION |
Debargha Mukherjee | 5bd41f9 | 2020-10-04 11:06:11 -0700 | [diff] [blame] | 1294 | #if !CONFIG_REMOVE_DIST_WTD_COMP |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1295 | AVERAGE_CDF(ctx_left->compound_index_cdf, ctx_tr->compound_index_cdf, 2); |
Debargha Mukherjee | 5bd41f9 | 2020-10-04 11:06:11 -0700 | [diff] [blame] | 1296 | #endif // !CONFIG_REMOVE_DIST_WTD_COMP |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1297 | AVERAGE_CDF(ctx_left->comp_group_idx_cdf, ctx_tr->comp_group_idx_cdf, 2); |
| 1298 | AVERAGE_CDF(ctx_left->skip_mode_cdfs, ctx_tr->skip_mode_cdfs, 2); |
| 1299 | AVERAGE_CDF(ctx_left->skip_txfm_cdfs, ctx_tr->skip_txfm_cdfs, 2); |
| 1300 | AVERAGE_CDF(ctx_left->intra_inter_cdf, ctx_tr->intra_inter_cdf, 2); |
| 1301 | avg_nmv(&ctx_left->nmvc, &ctx_tr->nmvc, wt_left, wt_tr); |
| 1302 | avg_nmv(&ctx_left->ndvc, &ctx_tr->ndvc, wt_left, wt_tr); |
| 1303 | AVERAGE_CDF(ctx_left->intrabc_cdf, ctx_tr->intrabc_cdf, 2); |
| 1304 | AVERAGE_CDF(ctx_left->seg.tree_cdf, ctx_tr->seg.tree_cdf, MAX_SEGMENTS); |
| 1305 | AVERAGE_CDF(ctx_left->seg.pred_cdf, ctx_tr->seg.pred_cdf, 2); |
| 1306 | AVERAGE_CDF(ctx_left->seg.spatial_pred_seg_cdf, |
| 1307 | ctx_tr->seg.spatial_pred_seg_cdf, MAX_SEGMENTS); |
| 1308 | AVERAGE_CDF(ctx_left->filter_intra_cdfs, ctx_tr->filter_intra_cdfs, 2); |
| 1309 | AVERAGE_CDF(ctx_left->filter_intra_mode_cdf, ctx_tr->filter_intra_mode_cdf, |
| 1310 | FILTER_INTRA_MODES); |
| 1311 | AVERAGE_CDF(ctx_left->switchable_restore_cdf, ctx_tr->switchable_restore_cdf, |
| 1312 | RESTORE_SWITCHABLE_TYPES); |
| 1313 | AVERAGE_CDF(ctx_left->wiener_restore_cdf, ctx_tr->wiener_restore_cdf, 2); |
| 1314 | AVERAGE_CDF(ctx_left->sgrproj_restore_cdf, ctx_tr->sgrproj_restore_cdf, 2); |
leolzhao | 943a08d | 2021-03-30 15:32:52 -0700 | [diff] [blame] | 1315 | #if CONFIG_MRLS |
| 1316 | AVERAGE_CDF(ctx_left->mrl_index_cdf, ctx_tr->mrl_index_cdf, MRL_LINE_NUMBER); |
| 1317 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1318 | AVERAGE_CDF(ctx_left->y_mode_cdf, ctx_tr->y_mode_cdf, INTRA_MODES); |
| 1319 | AVG_CDF_STRIDE(ctx_left->uv_mode_cdf[0], ctx_tr->uv_mode_cdf[0], |
| 1320 | UV_INTRA_MODES - 1, CDF_SIZE(UV_INTRA_MODES)); |
| 1321 | AVERAGE_CDF(ctx_left->uv_mode_cdf[1], ctx_tr->uv_mode_cdf[1], UV_INTRA_MODES); |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 1322 | #if CONFIG_SDP |
| 1323 | for (int plane_index = 0; plane_index < PARTITION_STRUCTURE_NUM; |
| 1324 | plane_index++) { |
| 1325 | for (int i = 0; i < PARTITION_CONTEXTS; i++) { |
| 1326 | if (i < 4) { |
| 1327 | AVG_CDF_STRIDE(ctx_left->partition_cdf[plane_index][i], |
| 1328 | ctx_tr->partition_cdf[plane_index][i], 4, CDF_SIZE(10)); |
| 1329 | } else if (i < 16) { |
| 1330 | AVERAGE_CDF(ctx_left->partition_cdf[plane_index][i], |
| 1331 | ctx_tr->partition_cdf[plane_index][i], 10); |
| 1332 | } else { |
| 1333 | AVG_CDF_STRIDE(ctx_left->partition_cdf[plane_index][i], |
| 1334 | ctx_tr->partition_cdf[plane_index][i], 8, CDF_SIZE(10)); |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1339 | for (int i = 0; i < PARTITION_CONTEXTS; i++) { |
| 1340 | if (i < 4) { |
| 1341 | AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 4, |
| 1342 | CDF_SIZE(10)); |
| 1343 | } else if (i < 16) { |
| 1344 | AVERAGE_CDF(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 10); |
| 1345 | } else { |
| 1346 | AVG_CDF_STRIDE(ctx_left->partition_cdf[i], ctx_tr->partition_cdf[i], 8, |
| 1347 | CDF_SIZE(10)); |
| 1348 | } |
| 1349 | } |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 1350 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1351 | AVERAGE_CDF(ctx_left->switchable_interp_cdf, ctx_tr->switchable_interp_cdf, |
| 1352 | SWITCHABLE_FILTERS); |
| 1353 | AVERAGE_CDF(ctx_left->kf_y_cdf, ctx_tr->kf_y_cdf, INTRA_MODES); |
| 1354 | AVERAGE_CDF(ctx_left->angle_delta_cdf, ctx_tr->angle_delta_cdf, |
| 1355 | 2 * MAX_ANGLE_DELTA + 1); |
SARWER, Mohammed Golam | 4241ffe | 2021-06-08 11:06:06 -0700 | [diff] [blame] | 1356 | #if CONFIG_ORIP |
| 1357 | AVERAGE_CDF(ctx_left->angle_delta_cdf_hv, ctx_tr->angle_delta_cdf_hv, |
| 1358 | 2 * MAX_ANGLE_DELTA + 1 + ADDITIONAL_ANGLE_DELTA); |
| 1359 | #endif |
Sarah Parker | 4247f72 | 2021-06-16 08:31:09 -0700 | [diff] [blame] | 1360 | #if CONFIG_NEW_TX_PARTITION |
| 1361 | // Square blocks |
| 1362 | AVERAGE_CDF(ctx_left->intra_4way_txfm_partition_cdf[0], |
| 1363 | ctx_tr->intra_4way_txfm_partition_cdf[0], 4); |
| 1364 | // Rectangular blocks |
| 1365 | AVERAGE_CDF(ctx_left->intra_4way_txfm_partition_cdf[1], |
| 1366 | ctx_tr->intra_4way_txfm_partition_cdf[1], 4); |
| 1367 | AVERAGE_CDF(ctx_left->intra_2way_txfm_partition_cdf, |
| 1368 | ctx_tr->intra_2way_txfm_partition_cdf, 2); |
| 1369 | AVERAGE_CDF(ctx_left->intra_2way_rect_txfm_partition_cdf, |
| 1370 | ctx_tr->intra_2way_rect_txfm_partition_cdf, 2); |
| 1371 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1372 | AVG_CDF_STRIDE(ctx_left->tx_size_cdf[0], ctx_tr->tx_size_cdf[0], MAX_TX_DEPTH, |
| 1373 | CDF_SIZE(MAX_TX_DEPTH + 1)); |
| 1374 | AVERAGE_CDF(ctx_left->tx_size_cdf[1], ctx_tr->tx_size_cdf[1], |
| 1375 | MAX_TX_DEPTH + 1); |
| 1376 | AVERAGE_CDF(ctx_left->tx_size_cdf[2], ctx_tr->tx_size_cdf[2], |
| 1377 | MAX_TX_DEPTH + 1); |
| 1378 | AVERAGE_CDF(ctx_left->tx_size_cdf[3], ctx_tr->tx_size_cdf[3], |
| 1379 | MAX_TX_DEPTH + 1); |
Sarah Parker | 4247f72 | 2021-06-16 08:31:09 -0700 | [diff] [blame] | 1380 | #endif // CONFIG_NEW_TX_PARTITION |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1381 | AVERAGE_CDF(ctx_left->delta_q_cdf, ctx_tr->delta_q_cdf, DELTA_Q_PROBS + 1); |
| 1382 | AVERAGE_CDF(ctx_left->delta_lf_cdf, ctx_tr->delta_lf_cdf, DELTA_LF_PROBS + 1); |
| 1383 | for (int i = 0; i < FRAME_LF_COUNT; i++) { |
| 1384 | AVERAGE_CDF(ctx_left->delta_lf_multi_cdf[i], ctx_tr->delta_lf_multi_cdf[i], |
| 1385 | DELTA_LF_PROBS + 1); |
| 1386 | } |
| 1387 | AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[1], ctx_tr->intra_ext_tx_cdf[1], 7, |
| 1388 | CDF_SIZE(TX_TYPES)); |
| 1389 | AVG_CDF_STRIDE(ctx_left->intra_ext_tx_cdf[2], ctx_tr->intra_ext_tx_cdf[2], 5, |
| 1390 | CDF_SIZE(TX_TYPES)); |
| 1391 | AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[1], ctx_tr->inter_ext_tx_cdf[1], 16, |
| 1392 | CDF_SIZE(TX_TYPES)); |
| 1393 | AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[2], ctx_tr->inter_ext_tx_cdf[2], 12, |
| 1394 | CDF_SIZE(TX_TYPES)); |
| 1395 | AVG_CDF_STRIDE(ctx_left->inter_ext_tx_cdf[3], ctx_tr->inter_ext_tx_cdf[3], 2, |
| 1396 | CDF_SIZE(TX_TYPES)); |
| 1397 | AVERAGE_CDF(ctx_left->cfl_sign_cdf, ctx_tr->cfl_sign_cdf, CFL_JOINT_SIGNS); |
| 1398 | AVERAGE_CDF(ctx_left->cfl_alpha_cdf, ctx_tr->cfl_alpha_cdf, |
| 1399 | CFL_ALPHABET_SIZE); |
Madhu Peringassery Krishnan | ee0d2f0 | 2021-06-14 12:08:32 -0700 | [diff] [blame] | 1400 | #if CONFIG_IST |
| 1401 | AVG_CDF_STRIDE(ctx_left->stx_cdf, ctx_tr->stx_cdf, STX_TYPES, |
| 1402 | CDF_SIZE(STX_TYPES)); |
| 1403 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1404 | } |
| 1405 | |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1406 | // Memset the mbmis at the current superblock to 0 |
| 1407 | void av1_reset_mbmi(CommonModeInfoParams *const mi_params, BLOCK_SIZE sb_size, |
| 1408 | int mi_row, int mi_col) { |
| 1409 | // size of sb in unit of mi (BLOCK_4X4) |
| 1410 | const int sb_size_mi = mi_size_wide[sb_size]; |
| 1411 | const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize]; |
| 1412 | // size of sb in unit of allocated mi size |
| 1413 | const int sb_size_alloc_mi = mi_size_wide[sb_size] / mi_alloc_size_1d; |
| 1414 | assert(mi_params->mi_alloc_stride % sb_size_alloc_mi == 0 && |
| 1415 | "mi is not allocated as a multiple of sb!"); |
| 1416 | assert(mi_params->mi_stride % sb_size_mi == 0 && |
| 1417 | "mi_grid_base is not allocated as a multiple of sb!"); |
| 1418 | |
| 1419 | const int mi_rows = mi_size_high[sb_size]; |
| 1420 | for (int cur_mi_row = 0; cur_mi_row < mi_rows; cur_mi_row++) { |
| 1421 | assert(get_mi_grid_idx(mi_params, 0, mi_col + mi_alloc_size_1d) < |
| 1422 | mi_params->mi_stride); |
| 1423 | const int mi_grid_idx = |
| 1424 | get_mi_grid_idx(mi_params, mi_row + cur_mi_row, mi_col); |
| 1425 | const int alloc_mi_idx = |
| 1426 | get_alloc_mi_idx(mi_params, mi_row + cur_mi_row, mi_col); |
| 1427 | memset(&mi_params->mi_grid_base[mi_grid_idx], 0, |
| 1428 | sb_size_mi * sizeof(*mi_params->mi_grid_base)); |
| 1429 | memset(&mi_params->tx_type_map[mi_grid_idx], 0, |
| 1430 | sb_size_mi * sizeof(*mi_params->tx_type_map)); |
| 1431 | if (cur_mi_row % mi_alloc_size_1d == 0) { |
| 1432 | memset(&mi_params->mi_alloc[alloc_mi_idx], 0, |
| 1433 | sb_size_alloc_mi * sizeof(*mi_params->mi_alloc)); |
| 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | void av1_backup_sb_state(SB_FIRST_PASS_STATS *sb_fp_stats, const AV1_COMP *cpi, |
| 1439 | ThreadData *td, const TileDataEnc *tile_data, |
| 1440 | int mi_row, int mi_col) { |
| 1441 | MACROBLOCK *x = &td->mb; |
| 1442 | MACROBLOCKD *xd = &x->e_mbd; |
| 1443 | const TileInfo *tile_info = &tile_data->tile_info; |
| 1444 | |
| 1445 | const AV1_COMMON *cm = &cpi->common; |
| 1446 | const int num_planes = av1_num_planes(cm); |
| 1447 | const BLOCK_SIZE sb_size = cm->seq_params.sb_size; |
| 1448 | |
| 1449 | xd->above_txfm_context = |
| 1450 | cm->above_contexts.txfm[tile_info->tile_row] + mi_col; |
| 1451 | xd->left_txfm_context = |
| 1452 | xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK); |
| 1453 | av1_save_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size, num_planes); |
| 1454 | |
| 1455 | sb_fp_stats->rd_count = cpi->td.rd_counts; |
| 1456 | sb_fp_stats->split_count = x->txfm_search_info.txb_split_count; |
| 1457 | |
| 1458 | sb_fp_stats->fc = *td->counts; |
| 1459 | |
| 1460 | memcpy(sb_fp_stats->inter_mode_rd_models, tile_data->inter_mode_rd_models, |
| 1461 | sizeof(sb_fp_stats->inter_mode_rd_models)); |
| 1462 | |
| 1463 | memcpy(sb_fp_stats->thresh_freq_fact, x->thresh_freq_fact, |
| 1464 | sizeof(sb_fp_stats->thresh_freq_fact)); |
| 1465 | |
| 1466 | const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col); |
| 1467 | sb_fp_stats->current_qindex = |
| 1468 | cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex; |
| 1469 | |
| 1470 | #if CONFIG_INTERNAL_STATS |
| 1471 | memcpy(sb_fp_stats->mode_chosen_counts, cpi->mode_chosen_counts, |
| 1472 | sizeof(sb_fp_stats->mode_chosen_counts)); |
| 1473 | #endif // CONFIG_INTERNAL_STATS |
| 1474 | } |
| 1475 | |
| 1476 | void av1_restore_sb_state(const SB_FIRST_PASS_STATS *sb_fp_stats, AV1_COMP *cpi, |
| 1477 | ThreadData *td, TileDataEnc *tile_data, int mi_row, |
| 1478 | int mi_col) { |
| 1479 | MACROBLOCK *x = &td->mb; |
| 1480 | |
| 1481 | const AV1_COMMON *cm = &cpi->common; |
| 1482 | const int num_planes = av1_num_planes(cm); |
| 1483 | const BLOCK_SIZE sb_size = cm->seq_params.sb_size; |
| 1484 | |
| 1485 | av1_restore_context(x, &sb_fp_stats->x_ctx, mi_row, mi_col, sb_size, |
| 1486 | num_planes); |
| 1487 | |
| 1488 | cpi->td.rd_counts = sb_fp_stats->rd_count; |
| 1489 | x->txfm_search_info.txb_split_count = sb_fp_stats->split_count; |
| 1490 | |
| 1491 | *td->counts = sb_fp_stats->fc; |
| 1492 | |
| 1493 | memcpy(tile_data->inter_mode_rd_models, sb_fp_stats->inter_mode_rd_models, |
| 1494 | sizeof(sb_fp_stats->inter_mode_rd_models)); |
| 1495 | memcpy(x->thresh_freq_fact, sb_fp_stats->thresh_freq_fact, |
| 1496 | sizeof(sb_fp_stats->thresh_freq_fact)); |
| 1497 | |
| 1498 | const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col); |
| 1499 | cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex = |
| 1500 | sb_fp_stats->current_qindex; |
| 1501 | |
| 1502 | #if CONFIG_INTERNAL_STATS |
| 1503 | memcpy(cpi->mode_chosen_counts, sb_fp_stats->mode_chosen_counts, |
| 1504 | sizeof(sb_fp_stats->mode_chosen_counts)); |
| 1505 | #endif // CONFIG_INTERNAL_STATS |
| 1506 | } |
| 1507 | |
| 1508 | // Update the rate costs of some symbols according to the frequency directed |
| 1509 | // by speed features |
| 1510 | void av1_set_cost_upd_freq(AV1_COMP *cpi, ThreadData *td, |
| 1511 | const TileInfo *const tile_info, const int mi_row, |
| 1512 | const int mi_col) { |
| 1513 | AV1_COMMON *const cm = &cpi->common; |
| 1514 | const int num_planes = av1_num_planes(cm); |
| 1515 | MACROBLOCK *const x = &td->mb; |
| 1516 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1517 | |
| 1518 | switch (cpi->oxcf.cost_upd_freq.coeff) { |
| 1519 | case COST_UPD_TILE: // Tile level |
| 1520 | if (mi_row != tile_info->mi_row_start) break; |
| 1521 | AOM_FALLTHROUGH_INTENDED; |
| 1522 | case COST_UPD_SBROW: // SB row level in tile |
| 1523 | if (mi_col != tile_info->mi_col_start) break; |
| 1524 | AOM_FALLTHROUGH_INTENDED; |
| 1525 | case COST_UPD_SB: // SB level |
| 1526 | if (cpi->sf.inter_sf.disable_sb_level_coeff_cost_upd && |
| 1527 | mi_col != tile_info->mi_col_start) |
| 1528 | break; |
| 1529 | av1_fill_coeff_costs(&x->coeff_costs, xd->tile_ctx, num_planes); |
| 1530 | break; |
| 1531 | default: assert(0); |
| 1532 | } |
| 1533 | |
| 1534 | switch (cpi->oxcf.cost_upd_freq.mode) { |
| 1535 | case COST_UPD_TILE: // Tile level |
| 1536 | if (mi_row != tile_info->mi_row_start) break; |
| 1537 | AOM_FALLTHROUGH_INTENDED; |
| 1538 | case COST_UPD_SBROW: // SB row level in tile |
| 1539 | if (mi_col != tile_info->mi_col_start) break; |
| 1540 | AOM_FALLTHROUGH_INTENDED; |
| 1541 | case COST_UPD_SB: // SB level |
leolzhao | 3db7cca | 2021-01-26 16:53:07 -0800 | [diff] [blame] | 1542 | #if CONFIG_SDP |
| 1543 | av1_fill_mode_rates(cm, xd, &x->mode_costs, xd->tile_ctx); |
| 1544 | #else |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1545 | av1_fill_mode_rates(cm, &x->mode_costs, xd->tile_ctx); |
leolzhao | 3db7cca | 2021-01-26 16:53:07 -0800 | [diff] [blame] | 1546 | #endif |
Jayasanker J | e9ad475 | 2020-06-30 19:30:03 +0530 | [diff] [blame] | 1547 | break; |
| 1548 | default: assert(0); |
| 1549 | } |
| 1550 | switch (cpi->oxcf.cost_upd_freq.mv) { |
| 1551 | case COST_UPD_OFF: break; |
| 1552 | case COST_UPD_TILE: // Tile level |
| 1553 | if (mi_row != tile_info->mi_row_start) break; |
| 1554 | AOM_FALLTHROUGH_INTENDED; |
| 1555 | case COST_UPD_SBROW: // SB row level in tile |
| 1556 | if (mi_col != tile_info->mi_col_start) break; |
| 1557 | AOM_FALLTHROUGH_INTENDED; |
| 1558 | case COST_UPD_SB: // SB level |
| 1559 | if (cpi->sf.inter_sf.disable_sb_level_mv_cost_upd && |
| 1560 | mi_col != tile_info->mi_col_start) |
| 1561 | break; |
| 1562 | av1_fill_mv_costs(xd->tile_ctx, cm->features.cur_frame_force_integer_mv, |
| 1563 | cm->features.allow_high_precision_mv, &x->mv_costs); |
| 1564 | break; |
| 1565 | default: assert(0); |
| 1566 | } |
| 1567 | } |