kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
| 3 | * |
| 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. |
| 10 | |
| 11 | */ |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <limits.h> |
| 15 | #include <math.h> |
| 16 | #include <stdio.h> |
| 17 | |
| 18 | #include "config/aom_dsp_rtcd.h" |
| 19 | #include "config/av1_rtcd.h" |
| 20 | |
| 21 | #include "aom_dsp/aom_dsp_common.h" |
| 22 | #include "aom_dsp/blend.h" |
| 23 | #include "aom_mem/aom_mem.h" |
| 24 | #include "aom_ports/aom_timer.h" |
| 25 | #include "aom_ports/mem.h" |
| 26 | #include "aom_ports/system_state.h" |
| 27 | |
Marco Paniconi | 46f4cb8 | 2020-02-20 11:18:59 -0800 | [diff] [blame] | 28 | #include "av1/encoder/model_rd.h" |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 29 | #include "av1/common/mvref_common.h" |
| 30 | #include "av1/common/pred_common.h" |
| 31 | #include "av1/common/reconinter.h" |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 32 | #include "av1/common/reconintra.h" |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 33 | |
| 34 | #include "av1/encoder/encodemv.h" |
| 35 | #include "av1/encoder/rdopt.h" |
| 36 | #include "av1/encoder/reconinter_enc.h" |
| 37 | |
| 38 | extern int g_pick_inter_mode_cnt; |
Fyodor Kyslov | 3082401 | 2020-07-14 13:48:08 -0700 | [diff] [blame] | 39 | /*!\cond */ |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 40 | typedef struct { |
| 41 | uint8_t *data; |
| 42 | int stride; |
| 43 | int in_use; |
| 44 | } PRED_BUFFER; |
| 45 | |
| 46 | typedef struct { |
| 47 | PRED_BUFFER *best_pred; |
| 48 | PREDICTION_MODE best_mode; |
| 49 | TX_SIZE best_tx_size; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 50 | MV_REFERENCE_FRAME best_ref_frame; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 51 | uint8_t best_mode_skip_txfm; |
Fyodor Kyslov | e0be17e | 2020-05-13 14:20:50 -0700 | [diff] [blame] | 52 | uint8_t best_mode_initial_skip_flag; |
Ravi Chaudhary | 1e4f94b | 2019-06-20 16:19:49 +0530 | [diff] [blame] | 53 | int_interpfilters best_pred_filter; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 54 | } BEST_PICKMODE; |
| 55 | |
| 56 | typedef struct { |
| 57 | MV_REFERENCE_FRAME ref_frame; |
| 58 | PREDICTION_MODE pred_mode; |
| 59 | } REF_MODE; |
Fyodor Kyslov | 3082401 | 2020-07-14 13:48:08 -0700 | [diff] [blame] | 60 | /*!\endcond */ |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 61 | |
Jerome Jiang | 0d58dae | 2020-03-04 14:32:41 -0800 | [diff] [blame] | 62 | static const int pos_shift_16x16[4][4] = { |
| 63 | { 9, 10, 13, 14 }, { 11, 12, 15, 16 }, { 17, 18, 21, 22 }, { 19, 20, 23, 24 } |
| 64 | }; |
| 65 | |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 66 | #define NUM_INTER_MODES_RT 9 |
| 67 | #define NUM_INTER_MODES_REDUCED 8 |
| 68 | |
| 69 | static const REF_MODE ref_mode_set_rt[NUM_INTER_MODES_RT] = { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 70 | { LAST_FRAME, NEARESTMV }, { LAST_FRAME, NEARMV }, |
| 71 | { LAST_FRAME, NEWMV }, { GOLDEN_FRAME, NEARESTMV }, |
| 72 | { GOLDEN_FRAME, NEARMV }, { GOLDEN_FRAME, NEWMV }, |
| 73 | { ALTREF_FRAME, NEARESTMV }, { ALTREF_FRAME, NEARMV }, |
| 74 | { ALTREF_FRAME, NEWMV } |
| 75 | }; |
| 76 | |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 77 | // GLOBALMV in the set below is in fact ZEROMV as we don't do global ME in RT |
| 78 | // mode |
| 79 | static const REF_MODE ref_mode_set_reduced[NUM_INTER_MODES_REDUCED] = { |
| 80 | { LAST_FRAME, GLOBALMV }, { LAST_FRAME, NEARESTMV }, |
| 81 | { GOLDEN_FRAME, GLOBALMV }, { LAST_FRAME, NEARMV }, |
| 82 | { LAST_FRAME, NEWMV }, { GOLDEN_FRAME, NEARESTMV }, |
| 83 | { GOLDEN_FRAME, NEARMV }, { GOLDEN_FRAME, NEWMV } |
| 84 | }; |
| 85 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 86 | static const THR_MODES mode_idx[REF_FRAMES][4] = { |
| 87 | { THR_DC, THR_V_PRED, THR_H_PRED, THR_SMOOTH }, |
| 88 | { THR_NEARESTMV, THR_NEARMV, THR_GLOBALMV, THR_NEWMV }, |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 89 | { THR_NEARESTL2, THR_NEARL2, THR_GLOBALL2, THR_NEWL2 }, |
| 90 | { THR_NEARESTL3, THR_NEARL3, THR_GLOBALL3, THR_NEWL3 }, |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 91 | { THR_NEARESTG, THR_NEARG, THR_GLOBALMV, THR_NEWG }, |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 94 | static const PREDICTION_MODE intra_mode_list[] = { DC_PRED, V_PRED, H_PRED, |
| 95 | SMOOTH_PRED }; |
| 96 | |
| 97 | static INLINE int mode_offset(const PREDICTION_MODE mode) { |
| 98 | if (mode >= NEARESTMV) { |
| 99 | return INTER_OFFSET(mode); |
| 100 | } else { |
| 101 | switch (mode) { |
| 102 | case DC_PRED: return 0; |
| 103 | case V_PRED: return 1; |
| 104 | case H_PRED: return 2; |
| 105 | case SMOOTH_PRED: return 3; |
| 106 | default: assert(0); return -1; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 111 | enum { |
| 112 | // INTER_ALL = (1 << NEARESTMV) | (1 << NEARMV) | (1 << NEWMV), |
| 113 | INTER_NEAREST = (1 << NEARESTMV), |
| 114 | INTER_NEAREST_NEW = (1 << NEARESTMV) | (1 << NEWMV), |
| 115 | INTER_NEAREST_NEAR = (1 << NEARESTMV) | (1 << NEARMV), |
| 116 | INTER_NEAR_NEW = (1 << NEARMV) | (1 << NEWMV), |
| 117 | }; |
| 118 | |
| 119 | static INLINE void init_best_pickmode(BEST_PICKMODE *bp) { |
| 120 | bp->best_mode = NEARESTMV; |
| 121 | bp->best_ref_frame = LAST_FRAME; |
kyslov | bab94fd | 2019-05-03 11:25:17 -0700 | [diff] [blame] | 122 | bp->best_tx_size = TX_8X8; |
Sachin Kumar Garg | 2225853 | 2019-06-18 16:45:06 +0530 | [diff] [blame] | 123 | bp->best_pred_filter = av1_broadcast_interp_filter(EIGHTTAP_REGULAR); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 124 | bp->best_mode_skip_txfm = 0; |
Fyodor Kyslov | e0be17e | 2020-05-13 14:20:50 -0700 | [diff] [blame] | 125 | bp->best_mode_initial_skip_flag = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 126 | bp->best_pred = NULL; |
| 127 | } |
| 128 | |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 129 | /*!\brief Runs Motion Estimation for a specific block and specific ref frame. |
| 130 | * |
| 131 | * \ingroup nonrd_mode_search |
| 132 | * \callgraph |
| 133 | * \callergraph |
| 134 | * Finds the best Motion Vector by running Motion Estimation for a specific |
| 135 | * block and a specific reference frame. Exits early if RDCost of Full Pel part |
| 136 | * exceeds best RD Cost fund so far |
| 137 | * \param[in] cpi Top-level encoder structure |
| 138 | * \param[in] x Pointer to structure holding all the |
| 139 | * data for the current macroblock |
| 140 | * \param[in] bsize Current block size |
| 141 | * \param[in] mi_row Row index in 4x4 units |
| 142 | * \param[in] mi_col Column index in 4x4 units |
| 143 | * \param[in] tmp_mv Pointer to best found New MV |
| 144 | * \param[in] rate_mv Pointer to Rate of the best new MV |
| 145 | * \param[in] best_rd_sofar RD Cost of the best mode found so far |
| 146 | * \param[in] use_base_mv Flag, indicating that tmp_mv holds |
| 147 | * specific MV to start the search with |
| 148 | * |
| 149 | * \return Returns 0 if ME was terminated after Full Pel Search because too |
| 150 | * high RD Cost. Otherwise returns 1. Best New MV is placed into \c tmp_mv. |
| 151 | * Rate estimation for this vector is placed to \c rate_mv |
| 152 | */ |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 153 | static int combined_motion_search(AV1_COMP *cpi, MACROBLOCK *x, |
| 154 | BLOCK_SIZE bsize, int mi_row, int mi_col, |
| 155 | int_mv *tmp_mv, int *rate_mv, |
| 156 | int64_t best_rd_sofar, int use_base_mv) { |
| 157 | MACROBLOCKD *xd = &x->e_mbd; |
| 158 | const AV1_COMMON *cm = &cpi->common; |
| 159 | const int num_planes = av1_num_planes(cm); |
| 160 | MB_MODE_INFO *mi = xd->mi[0]; |
| 161 | struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } }; |
Marco Paniconi | d182da4 | 2020-07-28 09:05:43 -0700 | [diff] [blame] | 162 | int step_param = (cpi->sf.rt_sf.fullpel_search_step_param) |
| 163 | ? cpi->sf.rt_sf.fullpel_search_step_param |
| 164 | : cpi->mv_search_params.mv_step_param; |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 165 | FULLPEL_MV start_mv; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 166 | const int ref = mi->ref_frame[0]; |
| 167 | const MV ref_mv = av1_get_ref_mv(x, mi->ref_mv_idx).as_mv; |
| 168 | MV center_mv; |
| 169 | int dis; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 170 | int rv = 0; |
| 171 | int cost_list[5]; |
| 172 | int search_subpel = 1; |
| 173 | const YV12_BUFFER_CONFIG *scaled_ref_frame = |
| 174 | av1_get_scaled_ref_frame(cpi, ref); |
| 175 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 176 | if (scaled_ref_frame) { |
| 177 | int i; |
| 178 | // Swap out the reference frame for a version that's been scaled to |
| 179 | // match the resolution of the current frame, allowing the existing |
| 180 | // motion search code to be used without additional modifications. |
| 181 | for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0]; |
| 182 | av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL, |
| 183 | num_planes); |
| 184 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 185 | |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 186 | start_mv = get_fullmv_from_mv(&ref_mv); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 187 | |
| 188 | if (!use_base_mv) |
| 189 | center_mv = ref_mv; |
| 190 | else |
| 191 | center_mv = tmp_mv->as_mv; |
Nithya V S | ace29f3 | 2020-04-07 16:15:12 +0530 | [diff] [blame] | 192 | const search_site_config *src_search_sites = |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 193 | cpi->mv_search_params.search_site_cfg[SS_CFG_SRC]; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 194 | FULLPEL_MOTION_SEARCH_PARAMS full_ms_params; |
chiyotsai | 7430da6 | 2020-07-27 14:06:26 -0700 | [diff] [blame] | 195 | av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, ¢er_mv, |
| 196 | src_search_sites, |
| 197 | /*fine_search_interval=*/0); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 198 | |
| 199 | av1_full_pixel_search(start_mv, &full_ms_params, step_param, |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 200 | cond_cost_list(cpi, cost_list), &tmp_mv->as_fullmv, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 201 | NULL); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 202 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 203 | // calculate the bit cost on motion vector |
chiyotsai | 87bb805 | 2020-02-12 16:56:33 -0800 | [diff] [blame] | 204 | MV mvp_full = get_mv_from_fullmv(&tmp_mv->as_fullmv); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 205 | |
Fyodor Kyslov | 648c650 | 2021-02-02 18:41:10 -0800 | [diff] [blame] | 206 | *rate_mv = av1_mv_bit_cost(&mvp_full, &ref_mv, x->mv_costs->nmv_joint_cost, |
| 207 | x->mv_costs->mv_cost_stack, MV_COST_WEIGHT); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 208 | |
| 209 | // TODO(kyslov) Account for Rate Mode! |
| 210 | rv = !(RDCOST(x->rdmult, (*rate_mv), 0) > best_rd_sofar); |
| 211 | |
| 212 | if (rv && search_subpel) { |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 213 | SUBPEL_MOTION_SEARCH_PARAMS ms_params; |
| 214 | av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv, |
chiyotsai | 4d1b7d9 | 2020-04-07 13:56:22 -0700 | [diff] [blame] | 215 | cost_list); |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 216 | MV subpel_start_mv = get_mv_from_fullmv(&tmp_mv->as_fullmv); |
Nithya V S | ace29f3 | 2020-04-07 16:15:12 +0530 | [diff] [blame] | 217 | cpi->mv_search_params.find_fractional_mv_step( |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 218 | xd, cm, &ms_params, subpel_start_mv, &tmp_mv->as_mv, &dis, |
Nithya V S | ace29f3 | 2020-04-07 16:15:12 +0530 | [diff] [blame] | 219 | &x->pred_sse[ref], NULL); |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 220 | |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 221 | *rate_mv = |
Fyodor Kyslov | 648c650 | 2021-02-02 18:41:10 -0800 | [diff] [blame] | 222 | av1_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->mv_costs->nmv_joint_cost, |
| 223 | x->mv_costs->mv_cost_stack, MV_COST_WEIGHT); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (scaled_ref_frame) { |
| 227 | int i; |
| 228 | for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i]; |
| 229 | } |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 230 | // Final MV can not be equal to referance MV as this will trigger assert |
| 231 | // later. This can happen if both NEAREST and NEAR modes were skipped |
| 232 | rv = (tmp_mv->as_mv.col != ref_mv.col || tmp_mv->as_mv.row != ref_mv.row); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 233 | return rv; |
| 234 | } |
| 235 | |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 236 | /*!\brief Searches for the best New Motion Vector. |
| 237 | * |
| 238 | * \ingroup nonrd_mode_search |
| 239 | * \callgraph |
| 240 | * \callergraph |
| 241 | * Finds the best Motion Vector by doing Motion Estimation. Uses reduced |
| 242 | * complexity ME for non-LAST frames or calls \c combined_motion_search |
| 243 | * for LAST reference frame |
| 244 | * \param[in] cpi Top-level encoder structure |
| 245 | * \param[in] x Pointer to structure holding all the |
| 246 | * data for the current macroblock |
| 247 | * \param[in] frame_mv Array that holds MVs for all modes |
| 248 | * and ref frames |
| 249 | * \param[in] ref_frame Reference freme for which to find |
| 250 | * the best New MVs |
| 251 | * \param[in] gf_temporal_ref Flag, indicating temporal reference |
| 252 | * for GOLDEN frame |
| 253 | * \param[in] bsize Current block size |
| 254 | * \param[in] mi_row Row index in 4x4 units |
| 255 | * \param[in] mi_col Column index in 4x4 units |
| 256 | * \param[in] rate_mv Pointer to Rate of the best new MV |
| 257 | * \param[in] best_rdc Pointer to the RD Cost for the best |
| 258 | * mode found so far |
| 259 | * |
| 260 | * \return Returns -1 if the search was not done, otherwise returns 0. |
| 261 | * Best New MV is placed into \c frame_mv array, Rate estimation for this |
| 262 | * vector is placed to \c rate_mv |
| 263 | */ |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 264 | static int search_new_mv(AV1_COMP *cpi, MACROBLOCK *x, |
| 265 | int_mv frame_mv[][REF_FRAMES], |
| 266 | MV_REFERENCE_FRAME ref_frame, int gf_temporal_ref, |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 267 | BLOCK_SIZE bsize, int mi_row, int mi_col, int *rate_mv, |
| 268 | RD_STATS *best_rdc) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 269 | MACROBLOCKD *const xd = &x->e_mbd; |
| 270 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 271 | AV1_COMMON *cm = &cpi->common; |
Vishesh | 39e7409 | 2020-06-16 17:13:48 +0530 | [diff] [blame] | 272 | if (ref_frame > LAST_FRAME && cpi->oxcf.rc_cfg.mode == AOM_CBR && |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 273 | gf_temporal_ref) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 274 | int tmp_sad; |
| 275 | int dis; |
| 276 | int cost_list[5] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX }; |
| 277 | |
| 278 | if (bsize < BLOCK_16X16) return -1; |
| 279 | |
| 280 | tmp_sad = av1_int_pro_motion_estimation( |
| 281 | cpi, x, bsize, mi_row, mi_col, |
chiyotsai | 0b90c41 | 2020-09-29 14:48:16 -0700 | [diff] [blame] | 282 | &x->mbmi_ext.ref_mv_stack[ref_frame][0].this_mv.as_mv); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 283 | |
| 284 | if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 285 | |
| 286 | frame_mv[NEWMV][ref_frame].as_int = mi->mv[0].as_int; |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 287 | int_mv best_mv = mi->mv[0]; |
| 288 | best_mv.as_mv.row >>= 3; |
| 289 | best_mv.as_mv.col >>= 3; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 290 | MV ref_mv = av1_get_ref_mv(x, 0).as_mv; |
| 291 | |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 292 | *rate_mv = av1_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv, &ref_mv, |
Fyodor Kyslov | 648c650 | 2021-02-02 18:41:10 -0800 | [diff] [blame] | 293 | x->mv_costs->nmv_joint_cost, |
| 294 | x->mv_costs->mv_cost_stack, MV_COST_WEIGHT); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 295 | frame_mv[NEWMV][ref_frame].as_mv.row >>= 3; |
| 296 | frame_mv[NEWMV][ref_frame].as_mv.col >>= 3; |
| 297 | |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 298 | SUBPEL_MOTION_SEARCH_PARAMS ms_params; |
| 299 | av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv, |
chiyotsai | 4d1b7d9 | 2020-04-07 13:56:22 -0700 | [diff] [blame] | 300 | cost_list); |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 301 | MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv); |
Nithya V S | ace29f3 | 2020-04-07 16:15:12 +0530 | [diff] [blame] | 302 | cpi->mv_search_params.find_fractional_mv_step( |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 303 | xd, cm, &ms_params, start_mv, &best_mv.as_mv, &dis, |
Nithya V S | ace29f3 | 2020-04-07 16:15:12 +0530 | [diff] [blame] | 304 | &x->pred_sse[ref_frame], NULL); |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 305 | frame_mv[NEWMV][ref_frame].as_int = best_mv.as_int; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 306 | } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col, |
| 307 | &frame_mv[NEWMV][ref_frame], rate_mv, |
| 308 | best_rdc->rdcost, 0)) { |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 315 | /*!\brief Finds predicted motion vectors for a block. |
| 316 | * |
| 317 | * \ingroup nonrd_mode_search |
| 318 | * \callgraph |
| 319 | * \callergraph |
| 320 | * Finds predicted motion vectors for a block from a certain reference frame. |
| 321 | * First, it fills reference MV stack, then picks the test from the stack and |
| 322 | * predicts the final MV for a block for each mode. |
| 323 | * \param[in] cpi Top-level encoder structure |
| 324 | * \param[in] x Pointer to structure holding all the |
| 325 | * data for the current macroblock |
| 326 | * \param[in] ref_frame Reference freme for which to find |
| 327 | * ref MVs |
| 328 | * \param[in] frame_mv Predicted MVs for a block |
| 329 | * \param[in] tile_data Pointer to struct holding adaptive |
| 330 | * data/contexts/models for the tile |
| 331 | * during encoding |
| 332 | * \param[in] yv12_mb Buffer to hold predicted block |
| 333 | * \param[in] bsize Current block size |
| 334 | * \param[in] force_skip_low_temp_var Flag indicating possible mode search |
| 335 | * prune for low temporal variace block |
| 336 | * |
| 337 | * \return Nothing is returned. Instead, predicted MVs are placed into |
| 338 | * \c frame_mv array |
| 339 | */ |
Fyodor Kyslov | 9fa800d | 2020-05-05 13:56:35 -0700 | [diff] [blame] | 340 | static INLINE void find_predictors(AV1_COMP *cpi, MACROBLOCK *x, |
| 341 | MV_REFERENCE_FRAME ref_frame, |
| 342 | int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], |
| 343 | TileDataEnc *tile_data, |
| 344 | struct buf_2d yv12_mb[8][MAX_MB_PLANE], |
| 345 | BLOCK_SIZE bsize, |
| 346 | int force_skip_low_temp_var) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 347 | AV1_COMMON *const cm = &cpi->common; |
| 348 | MACROBLOCKD *const xd = &x->e_mbd; |
| 349 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
chiyotsai | 0b90c41 | 2020-09-29 14:48:16 -0700 | [diff] [blame] | 350 | MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 351 | const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, ref_frame); |
| 352 | const int num_planes = av1_num_planes(cm); |
| 353 | (void)tile_data; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 354 | |
| 355 | x->pred_mv_sad[ref_frame] = INT_MAX; |
| 356 | frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; |
| 357 | // TODO(kyslov) this needs various further optimizations. to be continued.. |
Fyodor Kyslov | 9fa800d | 2020-05-05 13:56:35 -0700 | [diff] [blame] | 358 | assert(yv12 != NULL); |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 359 | if (yv12 != NULL) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 360 | const struct scale_factors *const sf = |
| 361 | get_ref_scale_factors_const(cm, ref_frame); |
Hui Su | b94cd5e | 2019-11-06 12:05:47 -0800 | [diff] [blame] | 362 | av1_setup_pred_block(xd, yv12_mb[ref_frame], yv12, sf, sf, num_planes); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 363 | av1_find_mv_refs(cm, xd, mbmi, ref_frame, mbmi_ext->ref_mv_count, |
Ravi Chaudhary | fa73e20 | 2019-08-19 12:41:26 +0530 | [diff] [blame] | 364 | xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs, |
Hui Su | b94cd5e | 2019-11-06 12:05:47 -0800 | [diff] [blame] | 365 | mbmi_ext->mode_context); |
Ravi Chaudhary | fa73e20 | 2019-08-19 12:41:26 +0530 | [diff] [blame] | 366 | // TODO(Ravi): Populate mbmi_ext->ref_mv_stack[ref_frame][4] and |
| 367 | // mbmi_ext->weight[ref_frame][4] inside av1_find_mv_refs. |
| 368 | av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame); |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 369 | av1_find_best_ref_mvs_from_stack( |
| 370 | cm->features.allow_high_precision_mv, mbmi_ext, ref_frame, |
| 371 | &frame_mv[NEARESTMV][ref_frame], &frame_mv[NEARMV][ref_frame], 0); |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 372 | frame_mv[GLOBALMV][ref_frame] = mbmi_ext->global_mvs[ref_frame]; |
Marco Paniconi | 7da95ed | 2020-03-05 16:20:53 -0800 | [diff] [blame] | 373 | // Early exit for non-LAST frame if force_skip_low_temp_var is set. |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 374 | if (!av1_is_scaled(sf) && bsize >= BLOCK_8X8 && |
Marco Paniconi | 7da95ed | 2020-03-05 16:20:53 -0800 | [diff] [blame] | 375 | !(force_skip_low_temp_var && ref_frame != LAST_FRAME)) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 376 | av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame, |
| 377 | bsize); |
| 378 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 379 | } |
Hui Su | b94cd5e | 2019-11-06 12:05:47 -0800 | [diff] [blame] | 380 | av1_count_overlappable_neighbors(cm, xd); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 381 | mbmi->num_proj_ref = 1; |
| 382 | } |
| 383 | |
Jerome Jiang | 906a94f | 2019-05-01 19:02:58 -0700 | [diff] [blame] | 384 | static void estimate_single_ref_frame_costs(const AV1_COMMON *cm, |
| 385 | const MACROBLOCKD *xd, |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 386 | const ModeCosts *mode_costs, |
| 387 | int segment_id, |
Jerome Jiang | 906a94f | 2019-05-01 19:02:58 -0700 | [diff] [blame] | 388 | unsigned int *ref_costs_single) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 389 | int seg_ref_active = |
| 390 | segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME); |
| 391 | if (seg_ref_active) { |
| 392 | memset(ref_costs_single, 0, REF_FRAMES * sizeof(*ref_costs_single)); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 393 | } else { |
| 394 | int intra_inter_ctx = av1_get_intra_inter_context(xd); |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 395 | ref_costs_single[INTRA_FRAME] = |
| 396 | mode_costs->intra_inter_cost[intra_inter_ctx][0]; |
| 397 | unsigned int base_cost = mode_costs->intra_inter_cost[intra_inter_ctx][1]; |
Marco Paniconi | 62b66dd | 2020-08-27 11:30:37 -0700 | [diff] [blame] | 398 | ref_costs_single[LAST_FRAME] = base_cost; |
| 399 | ref_costs_single[GOLDEN_FRAME] = base_cost; |
| 400 | ref_costs_single[ALTREF_FRAME] = base_cost; |
| 401 | // add cost for last, golden, altref |
| 402 | ref_costs_single[LAST_FRAME] += mode_costs->single_ref_cost[0][0][0]; |
| 403 | ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][0][1]; |
| 404 | ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][1][0]; |
| 405 | ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][0][1]; |
| 406 | ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][2][0]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 410 | static void estimate_comp_ref_frame_costs( |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 411 | const AV1_COMMON *cm, const MACROBLOCKD *xd, const ModeCosts *mode_costs, |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 412 | int segment_id, unsigned int (*ref_costs_comp)[REF_FRAMES]) { |
| 413 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) { |
| 414 | for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) |
| 415 | memset(ref_costs_comp[ref_frame], 0, |
| 416 | REF_FRAMES * sizeof((*ref_costs_comp)[0])); |
| 417 | } else { |
| 418 | int intra_inter_ctx = av1_get_intra_inter_context(xd); |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 419 | unsigned int base_cost = mode_costs->intra_inter_cost[intra_inter_ctx][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 420 | |
| 421 | if (cm->current_frame.reference_mode != SINGLE_REFERENCE) { |
| 422 | // Similar to single ref, determine cost of compound ref frames. |
| 423 | // cost_compound_refs = cost_first_ref + cost_second_ref |
| 424 | const int bwdref_comp_ctx_p = av1_get_pred_context_comp_bwdref_p(xd); |
| 425 | const int bwdref_comp_ctx_p1 = av1_get_pred_context_comp_bwdref_p1(xd); |
| 426 | const int ref_comp_ctx_p = av1_get_pred_context_comp_ref_p(xd); |
| 427 | const int ref_comp_ctx_p1 = av1_get_pred_context_comp_ref_p1(xd); |
| 428 | const int ref_comp_ctx_p2 = av1_get_pred_context_comp_ref_p2(xd); |
| 429 | |
| 430 | const int comp_ref_type_ctx = av1_get_comp_reference_type_context(xd); |
| 431 | unsigned int ref_bicomp_costs[REF_FRAMES] = { 0 }; |
| 432 | |
| 433 | ref_bicomp_costs[LAST_FRAME] = ref_bicomp_costs[LAST2_FRAME] = |
| 434 | ref_bicomp_costs[LAST3_FRAME] = ref_bicomp_costs[GOLDEN_FRAME] = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 435 | base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 436 | ref_bicomp_costs[BWDREF_FRAME] = ref_bicomp_costs[ALTREF2_FRAME] = 0; |
| 437 | ref_bicomp_costs[ALTREF_FRAME] = 0; |
| 438 | |
| 439 | // cost of first ref frame |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 440 | ref_bicomp_costs[LAST_FRAME] += |
| 441 | mode_costs->comp_ref_cost[ref_comp_ctx_p][0][0]; |
| 442 | ref_bicomp_costs[LAST2_FRAME] += |
| 443 | mode_costs->comp_ref_cost[ref_comp_ctx_p][0][0]; |
| 444 | ref_bicomp_costs[LAST3_FRAME] += |
| 445 | mode_costs->comp_ref_cost[ref_comp_ctx_p][0][1]; |
| 446 | ref_bicomp_costs[GOLDEN_FRAME] += |
| 447 | mode_costs->comp_ref_cost[ref_comp_ctx_p][0][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 448 | |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 449 | ref_bicomp_costs[LAST_FRAME] += |
| 450 | mode_costs->comp_ref_cost[ref_comp_ctx_p1][1][0]; |
| 451 | ref_bicomp_costs[LAST2_FRAME] += |
| 452 | mode_costs->comp_ref_cost[ref_comp_ctx_p1][1][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 453 | |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 454 | ref_bicomp_costs[LAST3_FRAME] += |
| 455 | mode_costs->comp_ref_cost[ref_comp_ctx_p2][2][0]; |
| 456 | ref_bicomp_costs[GOLDEN_FRAME] += |
| 457 | mode_costs->comp_ref_cost[ref_comp_ctx_p2][2][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 458 | |
| 459 | // cost of second ref frame |
| 460 | ref_bicomp_costs[BWDREF_FRAME] += |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 461 | mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p][0][0]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 462 | ref_bicomp_costs[ALTREF2_FRAME] += |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 463 | mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p][0][0]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 464 | ref_bicomp_costs[ALTREF_FRAME] += |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 465 | mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p][0][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 466 | |
| 467 | ref_bicomp_costs[BWDREF_FRAME] += |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 468 | mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p1][1][0]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 469 | ref_bicomp_costs[ALTREF2_FRAME] += |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 470 | mode_costs->comp_bwdref_cost[bwdref_comp_ctx_p1][1][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 471 | |
| 472 | // cost: if one ref frame is forward ref, the other ref is backward ref |
| 473 | for (int ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) { |
| 474 | for (int ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) { |
| 475 | ref_costs_comp[ref0][ref1] = |
| 476 | ref_bicomp_costs[ref0] + ref_bicomp_costs[ref1]; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // cost: if both ref frames are the same side. |
| 481 | const int uni_comp_ref_ctx_p = av1_get_pred_context_uni_comp_ref_p(xd); |
| 482 | const int uni_comp_ref_ctx_p1 = av1_get_pred_context_uni_comp_ref_p1(xd); |
| 483 | const int uni_comp_ref_ctx_p2 = av1_get_pred_context_uni_comp_ref_p2(xd); |
| 484 | ref_costs_comp[LAST_FRAME][LAST2_FRAME] = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 485 | base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][0] + |
| 486 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] + |
| 487 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][0]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 488 | ref_costs_comp[LAST_FRAME][LAST3_FRAME] = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 489 | base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][0] + |
| 490 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] + |
| 491 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][1] + |
| 492 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p2][2][0]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 493 | ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 494 | base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][0] + |
| 495 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][0] + |
| 496 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p1][1][1] + |
| 497 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p2][2][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 498 | ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 499 | base_cost + mode_costs->comp_ref_type_cost[comp_ref_type_ctx][0] + |
| 500 | mode_costs->uni_comp_ref_cost[uni_comp_ref_ctx_p][0][1]; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 501 | } else { |
| 502 | for (int ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) { |
| 503 | for (int ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) |
| 504 | ref_costs_comp[ref0][ref1] = 512; |
| 505 | } |
| 506 | ref_costs_comp[LAST_FRAME][LAST2_FRAME] = 512; |
| 507 | ref_costs_comp[LAST_FRAME][LAST3_FRAME] = 512; |
| 508 | ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = 512; |
| 509 | ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = 512; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 514 | static TX_SIZE calculate_tx_size(const AV1_COMP *const cpi, BLOCK_SIZE bsize, |
Cherma Rajan A | 3ed030f | 2019-08-16 18:56:00 +0530 | [diff] [blame] | 515 | MACROBLOCK *const x, unsigned int var, |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 516 | unsigned int sse) { |
Cherma Rajan A | 3ed030f | 2019-08-16 18:56:00 +0530 | [diff] [blame] | 517 | MACROBLOCKD *const xd = &x->e_mbd; |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 518 | TX_SIZE tx_size; |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 519 | const TxfmSearchParams *txfm_params = &x->txfm_search_params; |
| 520 | if (txfm_params->tx_mode_search_type == TX_MODE_SELECT) { |
Fyodor Kyslov | bb36786 | 2021-03-17 12:50:11 -0700 | [diff] [blame] | 521 | if (sse > (var << 1)) |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 522 | tx_size = |
| 523 | AOMMIN(max_txsize_lookup[bsize], |
| 524 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]); |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 525 | else |
| 526 | tx_size = TX_8X8; |
| 527 | |
Vishesh | 734eff9 | 2020-06-20 21:46:36 +0530 | [diff] [blame] | 528 | if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 529 | cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id)) |
| 530 | tx_size = TX_8X8; |
| 531 | else if (tx_size > TX_16X16) |
| 532 | tx_size = TX_16X16; |
| 533 | } else { |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 534 | tx_size = |
| 535 | AOMMIN(max_txsize_lookup[bsize], |
| 536 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]); |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 537 | } |
Jerome Jiang | d4e351d | 2020-01-30 15:13:11 -0800 | [diff] [blame] | 538 | |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 539 | if (txfm_params->tx_mode_search_type != ONLY_4X4 && bsize > BLOCK_32X32) |
Jerome Jiang | d4e351d | 2020-01-30 15:13:11 -0800 | [diff] [blame] | 540 | tx_size = TX_16X16; |
| 541 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 542 | return AOMMIN(tx_size, TX_16X16); |
| 543 | } |
| 544 | |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 545 | static const uint8_t b_width_log2_lookup[BLOCK_SIZES] = { 0, 0, 1, 1, 1, 2, |
| 546 | 2, 2, 3, 3, 3, 4, |
| 547 | 4, 4, 5, 5 }; |
| 548 | static const uint8_t b_height_log2_lookup[BLOCK_SIZES] = { 0, 1, 0, 1, 2, 1, |
| 549 | 2, 3, 2, 3, 4, 3, |
| 550 | 4, 5, 4, 5 }; |
| 551 | |
| 552 | static void block_variance(const uint8_t *src, int src_stride, |
| 553 | const uint8_t *ref, int ref_stride, int w, int h, |
| 554 | unsigned int *sse, int *sum, int block_size, |
| 555 | uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) { |
| 556 | int i, j, k = 0; |
| 557 | |
| 558 | *sse = 0; |
| 559 | *sum = 0; |
| 560 | |
| 561 | for (i = 0; i < h; i += block_size) { |
| 562 | for (j = 0; j < w; j += block_size) { |
| 563 | aom_get8x8var(src + src_stride * i + j, src_stride, |
| 564 | ref + ref_stride * i + j, ref_stride, &sse8x8[k], |
| 565 | &sum8x8[k]); |
| 566 | *sse += sse8x8[k]; |
| 567 | *sum += sum8x8[k]; |
| 568 | var8x8[k] = sse8x8[k] - (uint32_t)(((int64_t)sum8x8[k] * sum8x8[k]) >> 6); |
| 569 | k++; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static void calculate_variance(int bw, int bh, TX_SIZE tx_size, |
| 575 | unsigned int *sse_i, int *sum_i, |
| 576 | unsigned int *var_o, unsigned int *sse_o, |
| 577 | int *sum_o) { |
| 578 | const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size]; |
| 579 | const int nw = 1 << (bw - b_width_log2_lookup[unit_size]); |
| 580 | const int nh = 1 << (bh - b_height_log2_lookup[unit_size]); |
| 581 | int i, j, k = 0; |
| 582 | |
| 583 | for (i = 0; i < nh; i += 2) { |
| 584 | for (j = 0; j < nw; j += 2) { |
| 585 | sse_o[k] = sse_i[i * nw + j] + sse_i[i * nw + j + 1] + |
| 586 | sse_i[(i + 1) * nw + j] + sse_i[(i + 1) * nw + j + 1]; |
| 587 | sum_o[k] = sum_i[i * nw + j] + sum_i[i * nw + j + 1] + |
| 588 | sum_i[(i + 1) * nw + j] + sum_i[(i + 1) * nw + j + 1]; |
| 589 | var_o[k] = sse_o[k] - (uint32_t)(((int64_t)sum_o[k] * sum_o[k]) >> |
| 590 | (b_width_log2_lookup[unit_size] + |
| 591 | b_height_log2_lookup[unit_size] + 6)); |
| 592 | k++; |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // Adjust the ac_thr according to speed, width, height and normalized sum |
| 598 | static int ac_thr_factor(const int speed, const int width, const int height, |
| 599 | const int norm_sum) { |
| 600 | if (speed >= 8 && norm_sum < 5) { |
| 601 | if (width <= 640 && height <= 480) |
| 602 | return 4; |
| 603 | else |
| 604 | return 2; |
| 605 | } |
| 606 | return 1; |
| 607 | } |
| 608 | |
| 609 | static void model_skip_for_sb_y_large(AV1_COMP *cpi, BLOCK_SIZE bsize, |
Marco Paniconi | 4d93222 | 2019-11-03 21:41:46 -0800 | [diff] [blame] | 610 | int mi_row, int mi_col, MACROBLOCK *x, |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 611 | MACROBLOCKD *xd, RD_STATS *rd_stats, |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 612 | int *early_term, int calculate_rd) { |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 613 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 614 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 615 | // we need to divide by 8 before sending to modeling function. |
| 616 | unsigned int sse; |
| 617 | struct macroblock_plane *const p = &x->plane[0]; |
| 618 | struct macroblockd_plane *const pd = &xd->plane[0]; |
Ravi Chaudhary | fb23713 | 2019-06-06 15:50:36 +0530 | [diff] [blame] | 619 | const uint32_t dc_quant = p->dequant_QTX[0]; |
| 620 | const uint32_t ac_quant = p->dequant_QTX[1]; |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 621 | const int64_t dc_thr = dc_quant * dc_quant >> 6; |
| 622 | int64_t ac_thr = ac_quant * ac_quant >> 6; |
| 623 | unsigned int var; |
| 624 | int sum; |
| 625 | |
| 626 | const int bw = b_width_log2_lookup[bsize]; |
| 627 | const int bh = b_height_log2_lookup[bsize]; |
| 628 | const int num8x8 = 1 << (bw + bh - 2); |
| 629 | unsigned int sse8x8[256] = { 0 }; |
| 630 | int sum8x8[256] = { 0 }; |
| 631 | unsigned int var8x8[256] = { 0 }; |
| 632 | TX_SIZE tx_size; |
| 633 | int k; |
| 634 | // Calculate variance for whole partition, and also save 8x8 blocks' variance |
| 635 | // to be used in following transform skipping test. |
| 636 | block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, |
| 637 | 4 << bw, 4 << bh, &sse, &sum, 8, sse8x8, sum8x8, var8x8); |
| 638 | var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4)); |
| 639 | |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 640 | rd_stats->sse = sse; |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 641 | |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 642 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 643 | if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) && |
| 644 | cpi->oxcf.speed > 5) |
| 645 | ac_thr = av1_scale_acskip_thresh(ac_thr, cpi->denoiser.denoising_level, |
| 646 | (abs(sum) >> (bw + bh)), |
| 647 | cpi->svc.temporal_layer_id); |
| 648 | else |
| 649 | ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width, |
| 650 | cpi->common.height, abs(sum) >> (bw + bh)); |
| 651 | #else |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 652 | ac_thr *= ac_thr_factor(cpi->oxcf.speed, cpi->common.width, |
| 653 | cpi->common.height, abs(sum) >> (bw + bh)); |
| 654 | |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 655 | #endif |
Cherma Rajan A | 3ed030f | 2019-08-16 18:56:00 +0530 | [diff] [blame] | 656 | tx_size = calculate_tx_size(cpi, bsize, x, var, sse); |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 657 | // The code below for setting skip flag assumes tranform size of at least 8x8, |
| 658 | // so force this lower limit on transform. |
| 659 | if (tx_size < TX_8X8) tx_size = TX_8X8; |
| 660 | xd->mi[0]->tx_size = tx_size; |
| 661 | |
| 662 | // Evaluate if the partition block is a skippable block in Y plane. |
| 663 | { |
| 664 | unsigned int sse16x16[64] = { 0 }; |
| 665 | int sum16x16[64] = { 0 }; |
| 666 | unsigned int var16x16[64] = { 0 }; |
| 667 | const int num16x16 = num8x8 >> 2; |
| 668 | |
| 669 | unsigned int sse32x32[16] = { 0 }; |
| 670 | int sum32x32[16] = { 0 }; |
| 671 | unsigned int var32x32[16] = { 0 }; |
| 672 | const int num32x32 = num8x8 >> 4; |
| 673 | |
| 674 | int ac_test = 1; |
| 675 | int dc_test = 1; |
| 676 | const int num = (tx_size == TX_8X8) |
| 677 | ? num8x8 |
| 678 | : ((tx_size == TX_16X16) ? num16x16 : num32x32); |
| 679 | const unsigned int *sse_tx = |
| 680 | (tx_size == TX_8X8) ? sse8x8 |
| 681 | : ((tx_size == TX_16X16) ? sse16x16 : sse32x32); |
| 682 | const unsigned int *var_tx = |
| 683 | (tx_size == TX_8X8) ? var8x8 |
| 684 | : ((tx_size == TX_16X16) ? var16x16 : var32x32); |
| 685 | |
| 686 | // Calculate variance if tx_size > TX_8X8 |
| 687 | if (tx_size >= TX_16X16) |
| 688 | calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16, |
| 689 | sum16x16); |
| 690 | if (tx_size == TX_32X32) |
| 691 | calculate_variance(bw, bh, TX_16X16, sse16x16, sum16x16, var32x32, |
| 692 | sse32x32, sum32x32); |
| 693 | |
| 694 | // Skipping test |
| 695 | *early_term = 0; |
| 696 | for (k = 0; k < num; k++) |
| 697 | // Check if all ac coefficients can be quantized to zero. |
| 698 | if (!(var_tx[k] < ac_thr || var == 0)) { |
| 699 | ac_test = 0; |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | for (k = 0; k < num; k++) |
| 704 | // Check if dc coefficient can be quantized to zero. |
| 705 | if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) { |
| 706 | dc_test = 0; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | if (ac_test && dc_test) { |
Marco Paniconi | 4d93222 | 2019-11-03 21:41:46 -0800 | [diff] [blame] | 711 | int skip_uv[2] = { 0 }; |
| 712 | unsigned int var_uv[2]; |
| 713 | unsigned int sse_uv[2]; |
| 714 | AV1_COMMON *const cm = &cpi->common; |
| 715 | // Transform skipping test in UV planes. |
| 716 | for (int i = 1; i <= 2; i++) { |
| 717 | int j = i - 1; |
| 718 | skip_uv[j] = 1; |
| 719 | if (x->color_sensitivity[j]) { |
| 720 | skip_uv[j] = 0; |
| 721 | struct macroblock_plane *const puv = &x->plane[i]; |
| 722 | struct macroblockd_plane *const puvd = &xd->plane[i]; |
| 723 | const BLOCK_SIZE uv_bsize = get_plane_block_size( |
| 724 | bsize, puvd->subsampling_x, puvd->subsampling_y); |
| 725 | // Adjust these thresholds for UV. |
| 726 | const int64_t uv_dc_thr = |
| 727 | (puv->dequant_QTX[0] * puv->dequant_QTX[0]) >> 3; |
| 728 | const int64_t uv_ac_thr = |
| 729 | (puv->dequant_QTX[1] * puv->dequant_QTX[1]) >> 3; |
| 730 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, i, |
| 731 | i); |
| 732 | var_uv[j] = cpi->fn_ptr[uv_bsize].vf(puv->src.buf, puv->src.stride, |
| 733 | puvd->dst.buf, puvd->dst.stride, |
| 734 | &sse_uv[j]); |
| 735 | if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) && |
| 736 | (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j])) |
| 737 | skip_uv[j] = 1; |
| 738 | else |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | if (skip_uv[0] & skip_uv[1]) { |
| 743 | *early_term = 1; |
| 744 | } |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 747 | if (calculate_rd) { |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 748 | if (!*early_term) { |
| 749 | const int bwide = block_size_wide[bsize]; |
| 750 | const int bhigh = block_size_high[bsize]; |
| 751 | |
| 752 | model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh, |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 753 | &rd_stats->rate, &rd_stats->dist); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | if (*early_term) { |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 757 | rd_stats->rate = 0; |
| 758 | rd_stats->dist = sse << 4; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 763 | static void model_rd_for_sb_y(const AV1_COMP *const cpi, BLOCK_SIZE bsize, |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 764 | MACROBLOCK *x, MACROBLOCKD *xd, |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 765 | RD_STATS *rd_stats, int calculate_rd) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 766 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 767 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 768 | // we need to divide by 8 before sending to modeling function. |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 769 | const int ref = xd->mi[0]->ref_frame[0]; |
| 770 | |
kyslov | e7ff3b6 | 2019-04-05 14:15:03 -0700 | [diff] [blame] | 771 | assert(bsize < BLOCK_SIZES_ALL); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 772 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 773 | struct macroblock_plane *const p = &x->plane[0]; |
| 774 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 775 | unsigned int sse; |
| 776 | int rate; |
| 777 | int64_t dist; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 778 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 779 | unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride, |
| 780 | pd->dst.buf, pd->dst.stride, &sse); |
Cherma Rajan A | 3ed030f | 2019-08-16 18:56:00 +0530 | [diff] [blame] | 781 | xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, x, var, sse); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 782 | |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 783 | if (calculate_rd) { |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 784 | const int bwide = block_size_wide[bsize]; |
| 785 | const int bhigh = block_size_high[bsize]; |
| 786 | model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh, &rate, |
| 787 | &dist); |
| 788 | } else { |
| 789 | rate = INT_MAX; // this will be overwritten later with block_yrd |
| 790 | dist = INT_MAX; |
| 791 | } |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 792 | rd_stats->sse = sse; |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 793 | x->pred_sse[ref] = (unsigned int)AOMMIN(sse, UINT_MAX); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 794 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 795 | assert(rate >= 0); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 796 | |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 797 | rd_stats->skip_txfm = (rate == 0); |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 798 | rate = AOMMIN(rate, INT_MAX); |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 799 | rd_stats->rate = rate; |
| 800 | rd_stats->dist = dist; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 803 | /*!\brief Calculates RD Cost using Hadamard transform. |
| 804 | * |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 805 | * \ingroup nonrd_mode_search |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 806 | * \callgraph |
| 807 | * \callergraph |
| 808 | * Calculates RD Cost using Hadamard transform. For low bit depth this function |
| 809 | * uses low-precision set of functions (16-bit) and 32 bit for high bit depth |
| 810 | * \param[in] cpi Top-level encoder structure |
| 811 | * \param[in] x Pointer to structure holding all the data for |
| 812 | the current macroblock |
| 813 | * \param[in] mi_row Row index in 4x4 units |
| 814 | * \param[in] mi_col Column index in 4x4 units |
| 815 | * \param[in] this_rdc Pointer to calculated RD Cost |
| 816 | * \param[in] skippable Pointer to a flag indicating possible tx skip |
| 817 | * \param[in] bsize Current block size |
| 818 | * \param[in] tx_size Transform size |
| 819 | * |
| 820 | * \return Nothing is returned. Instead, calculated RD cost is placed to |
| 821 | * \c this_rdc. \c skippable flag is set if there is no non-zero quantized |
| 822 | * coefficients for Hadamard transform |
| 823 | */ |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 824 | static void block_yrd(AV1_COMP *cpi, MACROBLOCK *x, int mi_row, int mi_col, |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 825 | RD_STATS *this_rdc, int *skippable, BLOCK_SIZE bsize, |
| 826 | TX_SIZE tx_size) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 827 | MACROBLOCKD *xd = &x->e_mbd; |
| 828 | const struct macroblockd_plane *pd = &xd->plane[0]; |
| 829 | struct macroblock_plane *const p = &x->plane[0]; |
| 830 | const int num_4x4_w = mi_size_wide[bsize]; |
| 831 | const int num_4x4_h = mi_size_high[bsize]; |
| 832 | const int step = 1 << (tx_size << 1); |
| 833 | const int block_step = (1 << tx_size); |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 834 | int block = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 835 | const int max_blocks_wide = |
| 836 | num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 : xd->mb_to_right_edge >> 5); |
| 837 | const int max_blocks_high = |
| 838 | num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >> 5); |
| 839 | int eob_cost = 0; |
| 840 | const int bw = 4 * num_4x4_w; |
| 841 | const int bh = 4 * num_4x4_h; |
| 842 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 843 | (void)mi_row; |
| 844 | (void)mi_col; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 845 | (void)cpi; |
| 846 | |
Jerome Jiang | d1f40c8 | 2020-02-18 14:42:46 -0800 | [diff] [blame] | 847 | #if CONFIG_AV1_HIGHBITDEPTH |
| 848 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 849 | aom_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, |
| 850 | p->src.stride, pd->dst.buf, pd->dst.stride, |
| 851 | x->e_mbd.bd); |
| 852 | } else { |
| 853 | aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride, |
| 854 | pd->dst.buf, pd->dst.stride); |
| 855 | } |
| 856 | #else |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 857 | aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride, |
| 858 | pd->dst.buf, pd->dst.stride); |
Jerome Jiang | d1f40c8 | 2020-02-18 14:42:46 -0800 | [diff] [blame] | 859 | #endif |
| 860 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 861 | *skippable = 1; |
| 862 | // Keep track of the row and column of the blocks we use so that we know |
| 863 | // if we are in the unrestricted motion border. |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 864 | for (int r = 0; r < max_blocks_high; r += block_step) { |
| 865 | for (int c = 0; c < num_4x4_w; c += block_step) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 866 | if (c < max_blocks_wide) { |
Jingning Han | f62cbe2 | 2021-01-13 09:38:52 -0800 | [diff] [blame] | 867 | const SCAN_ORDER *const scan_order = &av1_scan_orders[tx_size][DCT_DCT]; |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 868 | const int block_offset = BLOCK_OFFSET(block); |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 869 | #if CONFIG_AV1_HIGHBITDEPTH |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 870 | tran_low_t *const coeff = p->coeff + block_offset; |
| 871 | tran_low_t *const qcoeff = p->qcoeff + block_offset; |
Urvang Joshi | 9543ad7 | 2020-04-17 16:59:46 -0700 | [diff] [blame] | 872 | tran_low_t *const dqcoeff = p->dqcoeff + block_offset; |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 873 | #else |
| 874 | int16_t *const low_coeff = (int16_t *)p->coeff + block_offset; |
| 875 | int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset; |
Urvang Joshi | 9543ad7 | 2020-04-17 16:59:46 -0700 | [diff] [blame] | 876 | int16_t *const low_dqcoeff = (int16_t *)p->dqcoeff + block_offset; |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 877 | #endif |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 878 | uint16_t *const eob = &p->eobs[block]; |
| 879 | const int diff_stride = bw; |
| 880 | const int16_t *src_diff; |
| 881 | src_diff = &p->src_diff[(r * diff_stride + c) << 2]; |
| 882 | |
| 883 | switch (tx_size) { |
| 884 | case TX_64X64: |
| 885 | assert(0); // Not implemented |
| 886 | break; |
| 887 | case TX_32X32: |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 888 | assert(0); // Not used |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 889 | break; |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 890 | #if CONFIG_AV1_HIGHBITDEPTH |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 891 | case TX_16X16: |
| 892 | aom_hadamard_16x16(src_diff, diff_stride, coeff); |
| 893 | av1_quantize_fp(coeff, 16 * 16, p->zbin_QTX, p->round_fp_QTX, |
| 894 | p->quant_fp_QTX, p->quant_shift_QTX, qcoeff, |
| 895 | dqcoeff, p->dequant_QTX, eob, scan_order->scan, |
| 896 | scan_order->iscan); |
| 897 | break; |
| 898 | case TX_8X8: |
| 899 | aom_hadamard_8x8(src_diff, diff_stride, coeff); |
| 900 | av1_quantize_fp(coeff, 8 * 8, p->zbin_QTX, p->round_fp_QTX, |
| 901 | p->quant_fp_QTX, p->quant_shift_QTX, qcoeff, |
| 902 | dqcoeff, p->dequant_QTX, eob, scan_order->scan, |
| 903 | scan_order->iscan); |
| 904 | break; |
Debargha Mukherjee | c32fb59 | 2020-08-21 19:23:43 -0700 | [diff] [blame] | 905 | default: |
| 906 | assert(tx_size == TX_4X4); |
| 907 | aom_fdct4x4(src_diff, coeff, diff_stride); |
| 908 | av1_quantize_fp(coeff, 4 * 4, p->zbin_QTX, p->round_fp_QTX, |
| 909 | p->quant_fp_QTX, p->quant_shift_QTX, qcoeff, |
| 910 | dqcoeff, p->dequant_QTX, eob, scan_order->scan, |
| 911 | scan_order->iscan); |
| 912 | break; |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 913 | #else |
| 914 | case TX_16X16: |
| 915 | aom_hadamard_lp_16x16(src_diff, diff_stride, low_coeff); |
| 916 | av1_quantize_lp(low_coeff, 16 * 16, p->round_fp_QTX, |
| 917 | p->quant_fp_QTX, low_qcoeff, low_dqcoeff, |
| 918 | p->dequant_QTX, eob, scan_order->scan); |
| 919 | break; |
| 920 | case TX_8X8: |
| 921 | aom_hadamard_lp_8x8(src_diff, diff_stride, low_coeff); |
| 922 | av1_quantize_lp(low_coeff, 8 * 8, p->round_fp_QTX, p->quant_fp_QTX, |
| 923 | low_qcoeff, low_dqcoeff, p->dequant_QTX, eob, |
| 924 | scan_order->scan); |
| 925 | break; |
Jerome Jiang | d4e351d | 2020-01-30 15:13:11 -0800 | [diff] [blame] | 926 | default: |
| 927 | assert(tx_size == TX_4X4); |
chiyotsai | 849bba6 | 2020-04-28 13:23:42 -0700 | [diff] [blame] | 928 | aom_fdct4x4_lp(src_diff, low_coeff, diff_stride); |
Jerome Jiang | d4e351d | 2020-01-30 15:13:11 -0800 | [diff] [blame] | 929 | av1_quantize_lp(low_coeff, 4 * 4, p->round_fp_QTX, p->quant_fp_QTX, |
| 930 | low_qcoeff, low_dqcoeff, p->dequant_QTX, eob, |
| 931 | scan_order->scan); |
| 932 | break; |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 933 | #endif |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 934 | } |
Debargha Mukherjee | c32fb59 | 2020-08-21 19:23:43 -0700 | [diff] [blame] | 935 | assert(*eob <= 1024); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 936 | *skippable &= (*eob == 0); |
| 937 | eob_cost += 1; |
| 938 | } |
| 939 | block += step; |
| 940 | } |
| 941 | } |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 942 | this_rdc->skip_txfm = *skippable; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 943 | this_rdc->rate = 0; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 944 | if (this_rdc->sse < INT64_MAX) { |
| 945 | this_rdc->sse = (this_rdc->sse << 6) >> 2; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 946 | if (*skippable) { |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 947 | this_rdc->dist = this_rdc->sse; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 948 | return; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | block = 0; |
| 953 | this_rdc->dist = 0; |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 954 | for (int r = 0; r < max_blocks_high; r += block_step) { |
| 955 | for (int c = 0; c < num_4x4_w; c += block_step) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 956 | if (c < max_blocks_wide) { |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 957 | const int block_offset = BLOCK_OFFSET(block); |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 958 | uint16_t *const eob = &p->eobs[block]; |
| 959 | #if CONFIG_AV1_HIGHBITDEPTH |
| 960 | int64_t dummy; |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 961 | tran_low_t *const coeff = p->coeff + block_offset; |
| 962 | tran_low_t *const qcoeff = p->qcoeff + block_offset; |
Urvang Joshi | 9543ad7 | 2020-04-17 16:59:46 -0700 | [diff] [blame] | 963 | tran_low_t *const dqcoeff = p->dqcoeff + block_offset; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 964 | |
| 965 | if (*eob == 1) |
| 966 | this_rdc->rate += (int)abs(qcoeff[0]); |
| 967 | else if (*eob > 1) |
| 968 | this_rdc->rate += aom_satd(qcoeff, step << 4); |
| 969 | |
| 970 | this_rdc->dist += |
| 971 | av1_block_error(coeff, dqcoeff, step << 4, &dummy) >> 2; |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 972 | #else |
| 973 | int16_t *const low_coeff = (int16_t *)p->coeff + block_offset; |
| 974 | int16_t *const low_qcoeff = (int16_t *)p->qcoeff + block_offset; |
Urvang Joshi | 9543ad7 | 2020-04-17 16:59:46 -0700 | [diff] [blame] | 975 | int16_t *const low_dqcoeff = (int16_t *)p->dqcoeff + block_offset; |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 976 | |
| 977 | if (*eob == 1) |
| 978 | this_rdc->rate += (int)abs(low_qcoeff[0]); |
| 979 | else if (*eob > 1) |
Fyodor Kyslov | 093688b | 2020-01-29 16:43:15 -0800 | [diff] [blame] | 980 | this_rdc->rate += aom_satd_lp(low_qcoeff, step << 4); |
Fyodor Kyslov | 2ab2544 | 2020-01-28 16:41:26 -0800 | [diff] [blame] | 981 | |
| 982 | this_rdc->dist += |
| 983 | av1_block_error_lp(low_coeff, low_dqcoeff, step << 4) >> 2; |
| 984 | #endif |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 985 | } |
| 986 | block += step; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | // If skippable is set, rate gets clobbered later. |
| 991 | this_rdc->rate <<= (2 + AV1_PROB_COST_SHIFT); |
| 992 | this_rdc->rate += (eob_cost << AV1_PROB_COST_SHIFT); |
| 993 | } |
| 994 | |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 995 | static INLINE void init_mbmi(MB_MODE_INFO *mbmi, PREDICTION_MODE pred_mode, |
| 996 | MV_REFERENCE_FRAME ref_frame0, |
| 997 | MV_REFERENCE_FRAME ref_frame1, |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 998 | const AV1_COMMON *cm) { |
| 999 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1000 | mbmi->ref_mv_idx = 0; |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1001 | mbmi->mode = pred_mode; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1002 | mbmi->uv_mode = UV_DC_PRED; |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1003 | mbmi->ref_frame[0] = ref_frame0; |
| 1004 | mbmi->ref_frame[1] = ref_frame1; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1005 | pmi->palette_size[0] = 0; |
| 1006 | pmi->palette_size[1] = 0; |
| 1007 | mbmi->filter_intra_mode_info.use_filter_intra = 0; |
| 1008 | mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0; |
| 1009 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 1010 | mbmi->num_proj_ref = 1; |
| 1011 | mbmi->interintra_mode = 0; |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 1012 | set_default_interp_filters(mbmi, cm->features.interp_filter); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 1015 | #if CONFIG_INTERNAL_STATS |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1016 | static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, |
| 1017 | int mode_index) { |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 1018 | #else |
| 1019 | static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) { |
| 1020 | #endif // CONFIG_INTERNAL_STATS |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1021 | MACROBLOCKD *const xd = &x->e_mbd; |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 1022 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1023 | |
| 1024 | // Take a snapshot of the coding context so it can be |
| 1025 | // restored if we decide to encode this way |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 1026 | ctx->rd_stats.skip_txfm = txfm_info->skip_txfm; |
| 1027 | |
Marco Paniconi | 553e038 | 2020-03-20 19:12:34 -0700 | [diff] [blame] | 1028 | memset(ctx->blk_skip, 0, sizeof(ctx->blk_skip[0]) * ctx->num_4x4_blk); |
| 1029 | memset(ctx->tx_type_map, DCT_DCT, |
| 1030 | sizeof(ctx->tx_type_map[0]) * ctx->num_4x4_blk); |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 1031 | ctx->skippable = txfm_info->skip_txfm; |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 1032 | #if CONFIG_INTERNAL_STATS |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1033 | ctx->best_mode_index = mode_index; |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 1034 | #endif // CONFIG_INTERNAL_STATS |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1035 | ctx->mic = *xd->mi[0]; |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 1036 | ctx->skippable = txfm_info->skip_txfm; |
chiyotsai | 0b90c41 | 2020-09-29 14:48:16 -0700 | [diff] [blame] | 1037 | av1_copy_mbmi_ext_to_mbmi_ext_frame(&ctx->mbmi_ext_best, &x->mbmi_ext, |
Ravi Chaudhary | e1a59ce | 2020-03-24 08:21:18 +0530 | [diff] [blame] | 1038 | av1_ref_frame_type(xd->mi[0]->ref_frame)); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1039 | ctx->comp_pred_diff = 0; |
| 1040 | ctx->hybrid_pred_diff = 0; |
| 1041 | ctx->single_pred_diff = 0; |
| 1042 | } |
| 1043 | |
| 1044 | static int get_pred_buffer(PRED_BUFFER *p, int len) { |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 1045 | for (int i = 0; i < len; i++) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1046 | if (!p[i].in_use) { |
| 1047 | p[i].in_use = 1; |
| 1048 | return i; |
| 1049 | } |
| 1050 | } |
| 1051 | return -1; |
| 1052 | } |
| 1053 | |
| 1054 | static void free_pred_buffer(PRED_BUFFER *p) { |
| 1055 | if (p != NULL) p->in_use = 0; |
| 1056 | } |
| 1057 | |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1058 | static int cost_mv_ref(const ModeCosts *const mode_costs, PREDICTION_MODE mode, |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1059 | int16_t mode_context) { |
| 1060 | if (is_inter_compound_mode(mode)) { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1061 | return mode_costs |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1062 | ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)]; |
| 1063 | } |
| 1064 | |
| 1065 | int mode_cost = 0; |
| 1066 | int16_t mode_ctx = mode_context & NEWMV_CTX_MASK; |
| 1067 | |
| 1068 | assert(is_inter_mode(mode)); |
| 1069 | |
| 1070 | if (mode == NEWMV) { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1071 | mode_cost = mode_costs->newmv_mode_cost[mode_ctx][0]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1072 | return mode_cost; |
| 1073 | } else { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1074 | mode_cost = mode_costs->newmv_mode_cost[mode_ctx][1]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1075 | mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK; |
| 1076 | |
| 1077 | if (mode == GLOBALMV) { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1078 | mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][0]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1079 | return mode_cost; |
| 1080 | } else { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1081 | mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][1]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1082 | mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK; |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1083 | mode_cost += mode_costs->refmv_mode_cost[mode_ctx][mode != NEARESTMV]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1084 | return mode_cost; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | static void newmv_diff_bias(MACROBLOCKD *xd, PREDICTION_MODE this_mode, |
| 1090 | RD_STATS *this_rdc, BLOCK_SIZE bsize, int mv_row, |
Marco Paniconi | 221b899 | 2020-10-01 11:14:41 -0700 | [diff] [blame] | 1091 | int mv_col, int speed, uint32_t spatial_variance, |
Marco Paniconi | 988b34a | 2020-11-09 12:41:13 -0800 | [diff] [blame] | 1092 | CONTENT_STATE_SB content_state_sb) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1093 | // Bias against MVs associated with NEWMV mode that are very different from |
| 1094 | // top/left neighbors. |
| 1095 | if (this_mode == NEWMV) { |
| 1096 | int al_mv_average_row; |
| 1097 | int al_mv_average_col; |
| 1098 | int left_row, left_col; |
| 1099 | int row_diff, col_diff; |
| 1100 | int above_mv_valid = 0; |
| 1101 | int left_mv_valid = 0; |
| 1102 | int above_row = 0; |
| 1103 | int above_col = 0; |
Marco Paniconi | 988b34a | 2020-11-09 12:41:13 -0800 | [diff] [blame] | 1104 | if (bsize >= BLOCK_64X64 && content_state_sb.source_sad != kHighSad && |
Marco Paniconi | 221b899 | 2020-10-01 11:14:41 -0700 | [diff] [blame] | 1105 | spatial_variance < 300 && |
| 1106 | (mv_row > 16 || mv_row < -16 || mv_col > 16 || mv_col < -16)) { |
| 1107 | this_rdc->rdcost = this_rdc->rdcost << 2; |
| 1108 | return; |
| 1109 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1110 | if (xd->above_mbmi) { |
| 1111 | above_mv_valid = xd->above_mbmi->mv[0].as_int != INVALID_MV; |
| 1112 | above_row = xd->above_mbmi->mv[0].as_mv.row; |
| 1113 | above_col = xd->above_mbmi->mv[0].as_mv.col; |
| 1114 | } |
| 1115 | if (xd->left_mbmi) { |
| 1116 | left_mv_valid = xd->left_mbmi->mv[0].as_int != INVALID_MV; |
| 1117 | left_row = xd->left_mbmi->mv[0].as_mv.row; |
| 1118 | left_col = xd->left_mbmi->mv[0].as_mv.col; |
| 1119 | } |
| 1120 | if (above_mv_valid && left_mv_valid) { |
| 1121 | al_mv_average_row = (above_row + left_row + 1) >> 1; |
| 1122 | al_mv_average_col = (above_col + left_col + 1) >> 1; |
| 1123 | } else if (above_mv_valid) { |
| 1124 | al_mv_average_row = above_row; |
| 1125 | al_mv_average_col = above_col; |
| 1126 | } else if (left_mv_valid) { |
| 1127 | al_mv_average_row = left_row; |
| 1128 | al_mv_average_col = left_col; |
| 1129 | } else { |
| 1130 | al_mv_average_row = al_mv_average_col = 0; |
| 1131 | } |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 1132 | row_diff = al_mv_average_row - mv_row; |
| 1133 | col_diff = al_mv_average_col - mv_col; |
Fyodor Kyslov | 990da97 | 2019-11-15 11:15:38 -0800 | [diff] [blame] | 1134 | if (row_diff > 80 || row_diff < -80 || col_diff > 80 || col_diff < -80) { |
| 1135 | if (bsize >= BLOCK_32X32) |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1136 | this_rdc->rdcost = this_rdc->rdcost << 1; |
| 1137 | else |
| 1138 | this_rdc->rdcost = 5 * this_rdc->rdcost >> 2; |
| 1139 | } |
Marco Paniconi | 6a966f1 | 2020-02-13 12:14:28 -0800 | [diff] [blame] | 1140 | } else { |
| 1141 | // Bias for speed >= 8 for low spatial variance. |
| 1142 | if (speed >= 8 && spatial_variance < 150 && |
| 1143 | (mv_row > 64 || mv_row < -64 || mv_col > 64 || mv_col < -64)) |
| 1144 | this_rdc->rdcost = 5 * this_rdc->rdcost >> 2; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1145 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 1148 | static void model_rd_for_sb_uv(AV1_COMP *cpi, BLOCK_SIZE plane_bsize, |
| 1149 | MACROBLOCK *x, MACROBLOCKD *xd, |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 1150 | RD_STATS *this_rdc, int64_t *sse_y, |
| 1151 | int start_plane, int stop_plane) { |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 1152 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 1153 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 1154 | // we need to divide by 8 before sending to modeling function. |
| 1155 | unsigned int sse; |
| 1156 | int rate; |
| 1157 | int64_t dist; |
| 1158 | int i; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1159 | int64_t tot_sse = *sse_y; |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 1160 | |
| 1161 | this_rdc->rate = 0; |
| 1162 | this_rdc->dist = 0; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1163 | this_rdc->skip_txfm = 0; |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 1164 | |
| 1165 | for (i = start_plane; i <= stop_plane; ++i) { |
| 1166 | struct macroblock_plane *const p = &x->plane[i]; |
| 1167 | struct macroblockd_plane *const pd = &xd->plane[i]; |
| 1168 | const uint32_t dc_quant = p->dequant_QTX[0]; |
| 1169 | const uint32_t ac_quant = p->dequant_QTX[1]; |
| 1170 | const BLOCK_SIZE bs = plane_bsize; |
| 1171 | unsigned int var; |
| 1172 | if (!x->color_sensitivity[i - 1]) continue; |
| 1173 | |
| 1174 | var = cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf, |
| 1175 | pd->dst.stride, &sse); |
| 1176 | assert(sse >= var); |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 1177 | tot_sse += sse; |
| 1178 | |
| 1179 | av1_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs], |
| 1180 | dc_quant >> 3, &rate, &dist); |
| 1181 | |
| 1182 | this_rdc->rate += rate >> 1; |
| 1183 | this_rdc->dist += dist << 3; |
| 1184 | |
| 1185 | av1_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs], ac_quant >> 3, |
| 1186 | &rate, &dist); |
| 1187 | |
| 1188 | this_rdc->rate += rate; |
| 1189 | this_rdc->dist += dist << 4; |
| 1190 | } |
| 1191 | |
Fyodor Kyslov | 4c7776b | 2019-09-10 10:31:01 -0700 | [diff] [blame] | 1192 | if (this_rdc->rate == 0) { |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1193 | this_rdc->skip_txfm = 1; |
Fyodor Kyslov | 4c7776b | 2019-09-10 10:31:01 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | if (RDCOST(x->rdmult, this_rdc->rate, this_rdc->dist) >= |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1197 | RDCOST(x->rdmult, 0, tot_sse << 4)) { |
Fyodor Kyslov | 4c7776b | 2019-09-10 10:31:01 -0700 | [diff] [blame] | 1198 | this_rdc->rate = 0; |
| 1199 | this_rdc->dist = tot_sse << 4; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1200 | this_rdc->skip_txfm = 1; |
Fyodor Kyslov | 4c7776b | 2019-09-10 10:31:01 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 1203 | *sse_y = tot_sse; |
| 1204 | } |
| 1205 | |
Fyodor Kyslov | 3082401 | 2020-07-14 13:48:08 -0700 | [diff] [blame] | 1206 | /*!\cond */ |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1207 | struct estimate_block_intra_args { |
| 1208 | AV1_COMP *cpi; |
| 1209 | MACROBLOCK *x; |
| 1210 | PREDICTION_MODE mode; |
| 1211 | int skippable; |
| 1212 | RD_STATS *rdc; |
| 1213 | }; |
Fyodor Kyslov | 3082401 | 2020-07-14 13:48:08 -0700 | [diff] [blame] | 1214 | /*!\endcond */ |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1215 | |
Fyodor Kyslov | 3082401 | 2020-07-14 13:48:08 -0700 | [diff] [blame] | 1216 | /*!\brief Estimation of RD cost of an intra mode for Non-RD optimized case. |
| 1217 | * |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 1218 | * \ingroup nonrd_mode_search |
Fyodor Kyslov | 3082401 | 2020-07-14 13:48:08 -0700 | [diff] [blame] | 1219 | * \callgraph |
| 1220 | * \callergraph |
| 1221 | * Calculates RD Cost for an intra mode for a single TX block using Hadamard |
| 1222 | * transform. |
| 1223 | * \param[in] plane Color plane |
| 1224 | * \param[in] block Index of a TX block in a prediction block |
| 1225 | * \param[in] row Row of a current TX block |
| 1226 | * \param[in] col Column of a current TX block |
| 1227 | * \param[in] plane_bsize Block size of a current prediction block |
| 1228 | * \param[in] tx_size Transform size |
| 1229 | * \param[in] arg Pointer to a structure that holds paramaters |
| 1230 | * for intra mode search |
| 1231 | * |
| 1232 | * \return Nothing is returned. Instead, best mode and RD Cost of the best mode |
| 1233 | * are set in \c args->rdc and \c args->mode |
| 1234 | */ |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1235 | static void estimate_block_intra(int plane, int block, int row, int col, |
| 1236 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
| 1237 | void *arg) { |
| 1238 | struct estimate_block_intra_args *const args = arg; |
| 1239 | AV1_COMP *const cpi = args->cpi; |
| 1240 | AV1_COMMON *const cm = &cpi->common; |
| 1241 | MACROBLOCK *const x = args->x; |
| 1242 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1243 | struct macroblock_plane *const p = &x->plane[plane]; |
| 1244 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Ruiling Song | 4b17f48 | 2019-06-26 09:33:19 +0800 | [diff] [blame] | 1245 | const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size]; |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1246 | uint8_t *const src_buf_base = p->src.buf; |
| 1247 | uint8_t *const dst_buf_base = pd->dst.buf; |
| 1248 | const int64_t src_stride = p->src.stride; |
| 1249 | const int64_t dst_stride = pd->dst.stride; |
| 1250 | RD_STATS this_rdc; |
| 1251 | |
Fyodor Kyslov | 5695d73 | 2019-06-28 16:28:48 -0700 | [diff] [blame] | 1252 | (void)block; |
Fyodor Kyslov | 5695d73 | 2019-06-28 16:28:48 -0700 | [diff] [blame] | 1253 | |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1254 | p->src.buf = &src_buf_base[4 * (row * src_stride + col)]; |
| 1255 | pd->dst.buf = &dst_buf_base[4 * (row * dst_stride + col)]; |
| 1256 | |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 1257 | av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size); |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1258 | av1_invalid_rd_stats(&this_rdc); |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1259 | |
| 1260 | if (plane == 0) { |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1261 | block_yrd(cpi, x, 0, 0, &this_rdc, &args->skippable, bsize_tx, |
Fyodor Kyslov | 1951ed5 | 2019-05-21 16:14:19 -0700 | [diff] [blame] | 1262 | AOMMIN(tx_size, TX_16X16)); |
| 1263 | } else { |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1264 | int64_t sse = 0; |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 1265 | model_rd_for_sb_uv(cpi, plane_bsize, x, xd, &this_rdc, &sse, plane, plane); |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | p->src.buf = src_buf_base; |
| 1269 | pd->dst.buf = dst_buf_base; |
| 1270 | args->rdc->rate += this_rdc.rate; |
| 1271 | args->rdc->dist += this_rdc.dist; |
| 1272 | } |
| 1273 | |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 1274 | static INLINE void update_thresh_freq_fact(AV1_COMP *cpi, MACROBLOCK *x, |
| 1275 | BLOCK_SIZE bsize, |
| 1276 | MV_REFERENCE_FRAME ref_frame, |
| 1277 | THR_MODES best_mode_idx, |
| 1278 | PREDICTION_MODE mode) { |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 1279 | const THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)]; |
| 1280 | const BLOCK_SIZE min_size = AOMMAX(bsize - 3, BLOCK_4X4); |
| 1281 | const BLOCK_SIZE max_size = AOMMIN(bsize + 6, BLOCK_128X128); |
| 1282 | for (BLOCK_SIZE bs = min_size; bs <= max_size; bs += 3) { |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 1283 | int *freq_fact = &x->thresh_freq_fact[bs][thr_mode_idx]; |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 1284 | if (thr_mode_idx == best_mode_idx) { |
| 1285 | *freq_fact -= (*freq_fact >> 4); |
| 1286 | } else { |
| 1287 | *freq_fact = |
| 1288 | AOMMIN(*freq_fact + RD_THRESH_INC, |
| 1289 | cpi->sf.inter_sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT); |
| 1290 | } |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 1294 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 1295 | static void av1_pickmode_ctx_den_update( |
| 1296 | AV1_PICKMODE_CTX_DEN *ctx_den, int64_t zero_last_cost_orig, |
| 1297 | unsigned int ref_frame_cost[REF_FRAMES], |
| 1298 | int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], int reuse_inter_pred, |
| 1299 | BEST_PICKMODE *bp) { |
| 1300 | ctx_den->zero_last_cost_orig = zero_last_cost_orig; |
| 1301 | ctx_den->ref_frame_cost = ref_frame_cost; |
| 1302 | ctx_den->frame_mv = frame_mv; |
| 1303 | ctx_den->reuse_inter_pred = reuse_inter_pred; |
| 1304 | ctx_den->best_tx_size = bp->best_tx_size; |
| 1305 | ctx_den->best_mode = bp->best_mode; |
| 1306 | ctx_den->best_ref_frame = bp->best_ref_frame; |
| 1307 | ctx_den->best_pred_filter = bp->best_pred_filter; |
| 1308 | ctx_den->best_mode_skip_txfm = bp->best_mode_skip_txfm; |
| 1309 | } |
| 1310 | |
| 1311 | static void recheck_zeromv_after_denoising( |
| 1312 | AV1_COMP *cpi, MB_MODE_INFO *const mi, MACROBLOCK *x, MACROBLOCKD *const xd, |
| 1313 | AV1_DENOISER_DECISION decision, AV1_PICKMODE_CTX_DEN *ctx_den, |
| 1314 | struct buf_2d yv12_mb[4][MAX_MB_PLANE], RD_STATS *best_rdc, |
| 1315 | BEST_PICKMODE *best_pickmode, BLOCK_SIZE bsize, int mi_row, int mi_col) { |
| 1316 | // If INTRA or GOLDEN reference was selected, re-evaluate ZEROMV on |
| 1317 | // denoised result. Only do this under noise conditions, and if rdcost of |
| 1318 | // ZEROMV onoriginal source is not significantly higher than rdcost of best |
| 1319 | // mode. |
| 1320 | if (cpi->noise_estimate.enabled && cpi->noise_estimate.level > kLow && |
| 1321 | ctx_den->zero_last_cost_orig < (best_rdc->rdcost << 3) && |
| 1322 | ((ctx_den->best_ref_frame == INTRA_FRAME && decision >= FILTER_BLOCK) || |
| 1323 | (ctx_den->best_ref_frame == GOLDEN_FRAME && |
| 1324 | cpi->svc.number_spatial_layers == 1 && |
| 1325 | decision == FILTER_ZEROMV_BLOCK))) { |
| 1326 | // Check if we should pick ZEROMV on denoised signal. |
| 1327 | AV1_COMMON *const cm = &cpi->common; |
| 1328 | RD_STATS this_rdc; |
| 1329 | const ModeCosts *mode_costs = &x->mode_costs; |
| 1330 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
| 1331 | MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext; |
| 1332 | |
| 1333 | mi->mode = GLOBALMV; |
| 1334 | mi->ref_frame[0] = LAST_FRAME; |
| 1335 | mi->ref_frame[1] = NONE_FRAME; |
| 1336 | set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME); |
| 1337 | mi->mv[0].as_int = 0; |
| 1338 | mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR); |
| 1339 | xd->plane[0].pre[0] = yv12_mb[LAST_FRAME][0]; |
| 1340 | av1_enc_build_inter_predictor_y(xd, mi_row, mi_col); |
| 1341 | model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, 1); |
| 1342 | |
| 1343 | const int16_t mode_ctx = |
| 1344 | av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame); |
| 1345 | this_rdc.rate += cost_mv_ref(mode_costs, GLOBALMV, mode_ctx); |
| 1346 | |
| 1347 | this_rdc.rate += ctx_den->ref_frame_cost[LAST_FRAME]; |
| 1348 | this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist); |
| 1349 | txfm_info->skip_txfm = this_rdc.skip_txfm; |
| 1350 | // Don't switch to ZEROMV if the rdcost for ZEROMV on denoised source |
| 1351 | // is higher than best_ref mode (on original source). |
| 1352 | if (this_rdc.rdcost > best_rdc->rdcost) { |
| 1353 | this_rdc = *best_rdc; |
| 1354 | mi->mode = best_pickmode->best_mode; |
| 1355 | mi->ref_frame[0] = best_pickmode->best_ref_frame; |
| 1356 | set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME); |
| 1357 | mi->interp_filters = best_pickmode->best_pred_filter; |
| 1358 | if (best_pickmode->best_ref_frame == INTRA_FRAME) { |
| 1359 | mi->mv[0].as_int = INVALID_MV; |
| 1360 | } else { |
| 1361 | mi->mv[0].as_int = ctx_den |
| 1362 | ->frame_mv[best_pickmode->best_mode] |
| 1363 | [best_pickmode->best_ref_frame] |
| 1364 | .as_int; |
| 1365 | if (ctx_den->reuse_inter_pred) { |
| 1366 | xd->plane[0].pre[0] = yv12_mb[GOLDEN_FRAME][0]; |
| 1367 | av1_enc_build_inter_predictor_y(xd, mi_row, mi_col); |
| 1368 | } |
| 1369 | } |
| 1370 | mi->tx_size = best_pickmode->best_tx_size; |
| 1371 | txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm; |
| 1372 | } else { |
| 1373 | ctx_den->best_ref_frame = LAST_FRAME; |
| 1374 | *best_rdc = this_rdc; |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | #endif // CONFIG_AV1_TEMPORAL_DENOISING |
| 1379 | |
Jerome Jiang | 0d58dae | 2020-03-04 14:32:41 -0800 | [diff] [blame] | 1380 | static INLINE int get_force_skip_low_temp_var_small_sb(uint8_t *variance_low, |
| 1381 | int mi_row, int mi_col, |
| 1382 | BLOCK_SIZE bsize) { |
| 1383 | // Relative indices of MB inside the superblock. |
| 1384 | const int mi_x = mi_row & 0xF; |
| 1385 | const int mi_y = mi_col & 0xF; |
| 1386 | // Relative indices of 16x16 block inside the superblock. |
| 1387 | const int i = mi_x >> 2; |
| 1388 | const int j = mi_y >> 2; |
| 1389 | int force_skip_low_temp_var = 0; |
| 1390 | // Set force_skip_low_temp_var based on the block size and block offset. |
| 1391 | switch (bsize) { |
| 1392 | case BLOCK_64X64: force_skip_low_temp_var = variance_low[0]; break; |
| 1393 | case BLOCK_64X32: |
| 1394 | if (!mi_y && !mi_x) { |
| 1395 | force_skip_low_temp_var = variance_low[1]; |
| 1396 | } else if (!mi_y && mi_x) { |
| 1397 | force_skip_low_temp_var = variance_low[2]; |
| 1398 | } |
| 1399 | break; |
| 1400 | case BLOCK_32X64: |
| 1401 | if (!mi_y && !mi_x) { |
| 1402 | force_skip_low_temp_var = variance_low[3]; |
| 1403 | } else if (mi_y && !mi_x) { |
| 1404 | force_skip_low_temp_var = variance_low[4]; |
| 1405 | } |
| 1406 | break; |
| 1407 | case BLOCK_32X32: |
| 1408 | if (!mi_y && !mi_x) { |
| 1409 | force_skip_low_temp_var = variance_low[5]; |
| 1410 | } else if (mi_y && !mi_x) { |
| 1411 | force_skip_low_temp_var = variance_low[6]; |
| 1412 | } else if (!mi_y && mi_x) { |
| 1413 | force_skip_low_temp_var = variance_low[7]; |
| 1414 | } else if (mi_y && mi_x) { |
| 1415 | force_skip_low_temp_var = variance_low[8]; |
| 1416 | } |
| 1417 | break; |
| 1418 | case BLOCK_32X16: |
| 1419 | case BLOCK_16X32: |
| 1420 | case BLOCK_16X16: |
| 1421 | force_skip_low_temp_var = variance_low[pos_shift_16x16[i][j]]; |
| 1422 | break; |
| 1423 | default: break; |
| 1424 | } |
| 1425 | |
| 1426 | return force_skip_low_temp_var; |
| 1427 | } |
| 1428 | |
Fyodor Kyslov | 59745d9 | 2019-07-30 18:16:54 -0700 | [diff] [blame] | 1429 | static INLINE int get_force_skip_low_temp_var(uint8_t *variance_low, int mi_row, |
| 1430 | int mi_col, BLOCK_SIZE bsize) { |
| 1431 | int force_skip_low_temp_var = 0; |
| 1432 | int x, y; |
Jerome Jiang | a996e77 | 2020-02-25 11:40:13 -0800 | [diff] [blame] | 1433 | x = (mi_col & 0x1F) >> 4; |
| 1434 | // y = (mi_row & 0x1F) >> 4; |
| 1435 | // const int idx64 = (y << 1) + x; |
| 1436 | y = (mi_row & 0x17) >> 3; |
| 1437 | const int idx64 = y + x; |
| 1438 | |
| 1439 | x = (mi_col & 0xF) >> 3; |
| 1440 | // y = (mi_row & 0xF) >> 3; |
| 1441 | // const int idx32 = (y << 1) + x; |
| 1442 | y = (mi_row & 0xB) >> 2; |
| 1443 | const int idx32 = y + x; |
| 1444 | |
| 1445 | x = (mi_col & 0x7) >> 2; |
| 1446 | // y = (mi_row & 0x7) >> 2; |
| 1447 | // const int idx16 = (y << 1) + x; |
| 1448 | y = (mi_row & 0x5) >> 1; |
| 1449 | const int idx16 = y + x; |
Fyodor Kyslov | 59745d9 | 2019-07-30 18:16:54 -0700 | [diff] [blame] | 1450 | // Set force_skip_low_temp_var based on the block size and block offset. |
| 1451 | switch (bsize) { |
| 1452 | case BLOCK_128X128: force_skip_low_temp_var = variance_low[0]; break; |
Jerome Jiang | a996e77 | 2020-02-25 11:40:13 -0800 | [diff] [blame] | 1453 | case BLOCK_128X64: |
| 1454 | assert((mi_col & 0x1F) == 0); |
| 1455 | force_skip_low_temp_var = variance_low[1 + ((mi_row & 0x1F) != 0)]; |
| 1456 | break; |
| 1457 | case BLOCK_64X128: |
| 1458 | assert((mi_row & 0x1F) == 0); |
| 1459 | force_skip_low_temp_var = variance_low[3 + ((mi_col & 0x1F) != 0)]; |
| 1460 | break; |
Fyodor Kyslov | 59745d9 | 2019-07-30 18:16:54 -0700 | [diff] [blame] | 1461 | case BLOCK_64X64: |
Jerome Jiang | a996e77 | 2020-02-25 11:40:13 -0800 | [diff] [blame] | 1462 | // Location of this 64x64 block inside the 128x128 superblock |
| 1463 | force_skip_low_temp_var = variance_low[5 + idx64]; |
| 1464 | break; |
| 1465 | case BLOCK_64X32: |
| 1466 | x = (mi_col & 0x1F) >> 4; |
| 1467 | y = (mi_row & 0x1F) >> 3; |
| 1468 | /* |
| 1469 | .---------------.---------------. |
| 1470 | | x=0,y=0,idx=0 | x=0,y=0,idx=2 | |
| 1471 | :---------------+---------------: |
| 1472 | | x=0,y=1,idx=1 | x=1,y=1,idx=3 | |
| 1473 | :---------------+---------------: |
| 1474 | | x=0,y=2,idx=4 | x=1,y=2,idx=6 | |
| 1475 | :---------------+---------------: |
| 1476 | | x=0,y=3,idx=5 | x=1,y=3,idx=7 | |
| 1477 | '---------------'---------------' |
| 1478 | */ |
| 1479 | const int idx64x32 = (x << 1) + (y % 2) + ((y >> 1) << 2); |
| 1480 | force_skip_low_temp_var = variance_low[9 + idx64x32]; |
| 1481 | break; |
| 1482 | case BLOCK_32X64: |
| 1483 | x = (mi_col & 0x1F) >> 3; |
| 1484 | y = (mi_row & 0x1F) >> 4; |
| 1485 | const int idx32x64 = (y << 2) + x; |
| 1486 | force_skip_low_temp_var = variance_low[17 + idx32x64]; |
| 1487 | break; |
Fyodor Kyslov | 59745d9 | 2019-07-30 18:16:54 -0700 | [diff] [blame] | 1488 | case BLOCK_32X32: |
Jerome Jiang | a996e77 | 2020-02-25 11:40:13 -0800 | [diff] [blame] | 1489 | force_skip_low_temp_var = variance_low[25 + (idx64 << 2) + idx32]; |
| 1490 | break; |
| 1491 | case BLOCK_32X16: |
| 1492 | case BLOCK_16X32: |
Fyodor Kyslov | 59745d9 | 2019-07-30 18:16:54 -0700 | [diff] [blame] | 1493 | case BLOCK_16X16: |
Jerome Jiang | a996e77 | 2020-02-25 11:40:13 -0800 | [diff] [blame] | 1494 | force_skip_low_temp_var = |
| 1495 | variance_low[41 + (idx64 << 4) + (idx32 << 2) + idx16]; |
| 1496 | break; |
Fyodor Kyslov | 59745d9 | 2019-07-30 18:16:54 -0700 | [diff] [blame] | 1497 | default: break; |
| 1498 | } |
| 1499 | return force_skip_low_temp_var; |
| 1500 | } |
| 1501 | |
Fyodor Kyslov | 9442669 | 2019-08-29 10:41:55 -0700 | [diff] [blame] | 1502 | #define FILTER_SEARCH_SIZE 2 |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1503 | /*!\brief Searches for the best intrpolation filter |
| 1504 | * |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 1505 | * \ingroup nonrd_mode_search |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1506 | * \callgraph |
| 1507 | * \callergraph |
| 1508 | * Iterates through subset of possible interpolation filters (currently |
| 1509 | * only EIGHTTAP_REGULAR and EIGTHTAP_SMOOTH in both directions) and selects |
| 1510 | * the one that gives lowest RD cost. RD cost is calculated using curvfit model |
| 1511 | * |
| 1512 | * \param[in] cpi Top-level encoder structure |
| 1513 | * \param[in] x Pointer to structure holding all the |
| 1514 | * data for the current macroblock |
| 1515 | * \param[in] this_rdc Pointer to calculated RD Cost |
| 1516 | * \param[in] mi_row Row index in 4x4 units |
| 1517 | * \param[in] mi_col Column index in 4x4 units |
| 1518 | * \param[in] tmp Pointer to a temporary buffer for |
| 1519 | * prediction re-use |
| 1520 | * \param[in] bsize Current block size |
| 1521 | * \param[in] reuse_inter_pred Flag, indicating prediction re-use |
| 1522 | * \param[out] this_mode_pred Pointer to store prediction buffer |
| 1523 | * for prediction re-use |
| 1524 | * \param[out] this_early_term Flag, indicating that transform can be |
| 1525 | * skipped |
| 1526 | * \param[in] use_model_yrd_large Flag, indicating special logic to handle |
| 1527 | * large blocks |
| 1528 | * |
| 1529 | * \return Nothing is returned. Instead, calculated RD cost is placed to |
| 1530 | * \c this_rdc and best filter is placed to \c mi->interp_filters. In case |
| 1531 | * \c reuse_inter_pred flag is set, this function also ouputs |
| 1532 | * \c this_mode_pred. Also \c this_early_temp is set if transform can be |
| 1533 | * skipped |
| 1534 | */ |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1535 | static void search_filter_ref(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc, |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1536 | int mi_row, int mi_col, PRED_BUFFER *tmp, |
| 1537 | BLOCK_SIZE bsize, int reuse_inter_pred, |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 1538 | PRED_BUFFER **this_mode_pred, |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1539 | int *this_early_term, int use_model_yrd_large) { |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1540 | AV1_COMMON *const cm = &cpi->common; |
| 1541 | MACROBLOCKD *const xd = &x->e_mbd; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1542 | struct macroblockd_plane *const pd = &xd->plane[0]; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1543 | MB_MODE_INFO *const mi = xd->mi[0]; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1544 | const int bw = block_size_wide[bsize]; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1545 | RD_STATS pf_rd_stats[FILTER_SEARCH_SIZE] = { 0 }; |
Fyodor Kyslov | 9442669 | 2019-08-29 10:41:55 -0700 | [diff] [blame] | 1546 | TX_SIZE pf_tx_size[FILTER_SEARCH_SIZE] = { 0 }; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1547 | PRED_BUFFER *current_pred = *this_mode_pred; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1548 | int best_skip = 0; |
| 1549 | int best_early_term = 0; |
| 1550 | int64_t best_cost = INT64_MAX; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1551 | int best_filter_index = -1; |
Fyodor Kyslov | 9442669 | 2019-08-29 10:41:55 -0700 | [diff] [blame] | 1552 | InterpFilter filters[FILTER_SEARCH_SIZE] = { EIGHTTAP_REGULAR, |
| 1553 | EIGHTTAP_SMOOTH }; |
Hui Su | 2210085 | 2020-11-06 11:11:27 -0800 | [diff] [blame] | 1554 | for (int i = 0; i < FILTER_SEARCH_SIZE; ++i) { |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1555 | int64_t cost; |
| 1556 | InterpFilter filter = filters[i]; |
| 1557 | mi->interp_filters = av1_broadcast_interp_filter(filter); |
Fyodor Kyslov | 4dfc85b | 2020-02-04 17:47:38 -0800 | [diff] [blame] | 1558 | av1_enc_build_inter_predictor_y(xd, mi_row, mi_col); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1559 | if (use_model_yrd_large) |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1560 | model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 1561 | &pf_rd_stats[i], this_early_term, 1); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1562 | else |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 1563 | model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[i], 1); |
Hui Su | 2210085 | 2020-11-06 11:11:27 -0800 | [diff] [blame] | 1564 | pf_rd_stats[i].rate += av1_get_switchable_rate( |
| 1565 | x, xd, cm->features.interp_filter, cm->seq_params.enable_dual_filter); |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1566 | cost = RDCOST(x->rdmult, pf_rd_stats[i].rate, pf_rd_stats[i].dist); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1567 | pf_tx_size[i] = mi->tx_size; |
| 1568 | if (cost < best_cost) { |
| 1569 | best_filter_index = i; |
| 1570 | best_cost = cost; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1571 | best_skip = pf_rd_stats[i].skip_txfm; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1572 | best_early_term = *this_early_term; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1573 | if (reuse_inter_pred) { |
| 1574 | if (*this_mode_pred != current_pred) { |
| 1575 | free_pred_buffer(*this_mode_pred); |
| 1576 | *this_mode_pred = current_pred; |
| 1577 | } |
| 1578 | current_pred = &tmp[get_pred_buffer(tmp, 3)]; |
| 1579 | pd->dst.buf = current_pred->data; |
| 1580 | pd->dst.stride = bw; |
| 1581 | } |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1582 | } |
| 1583 | } |
Fyodor Kyslov | 9442669 | 2019-08-29 10:41:55 -0700 | [diff] [blame] | 1584 | assert(best_filter_index >= 0 && best_filter_index < FILTER_SEARCH_SIZE); |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1585 | if (reuse_inter_pred && *this_mode_pred != current_pred) |
| 1586 | free_pred_buffer(current_pred); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1587 | |
| 1588 | mi->interp_filters = av1_broadcast_interp_filter(filters[best_filter_index]); |
| 1589 | mi->tx_size = pf_tx_size[best_filter_index]; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1590 | this_rdc->rate = pf_rd_stats[best_filter_index].rate; |
| 1591 | this_rdc->dist = pf_rd_stats[best_filter_index].dist; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1592 | this_rdc->sse = pf_rd_stats[best_filter_index].sse; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1593 | this_rdc->skip_txfm = (best_skip || best_early_term); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1594 | *this_early_term = best_early_term; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1595 | if (reuse_inter_pred) { |
| 1596 | pd->dst.buf = (*this_mode_pred)->data; |
| 1597 | pd->dst.stride = (*this_mode_pred)->stride; |
Marco Paniconi | b43603d | 2020-02-28 10:21:43 -0800 | [diff] [blame] | 1598 | } else if (best_filter_index < FILTER_SEARCH_SIZE - 1) { |
Fyodor Kyslov | 4dfc85b | 2020-02-04 17:47:38 -0800 | [diff] [blame] | 1599 | av1_enc_build_inter_predictor_y(xd, mi_row, mi_col); |
Marco Paniconi | ecf1cc6 | 2019-10-07 19:17:37 -0700 | [diff] [blame] | 1600 | } |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 1603 | #define COLLECT_PICK_MODE_STAT 0 |
| 1604 | |
| 1605 | #if COLLECT_PICK_MODE_STAT |
| 1606 | typedef struct _mode_search_stat { |
| 1607 | int32_t num_blocks[BLOCK_SIZES]; |
| 1608 | int64_t avg_block_times[BLOCK_SIZES]; |
| 1609 | int32_t num_searches[BLOCK_SIZES][MB_MODE_COUNT]; |
| 1610 | int32_t num_nonskipped_searches[BLOCK_SIZES][MB_MODE_COUNT]; |
| 1611 | int64_t search_times[BLOCK_SIZES][MB_MODE_COUNT]; |
| 1612 | int64_t nonskipped_search_times[BLOCK_SIZES][MB_MODE_COUNT]; |
| 1613 | struct aom_usec_timer timer1; |
| 1614 | struct aom_usec_timer timer2; |
| 1615 | } mode_search_stat; |
| 1616 | #endif // COLLECT_PICK_MODE_STAT |
| 1617 | |
Marco Paniconi | 13460a2 | 2019-10-15 15:54:47 -0700 | [diff] [blame] | 1618 | static void compute_intra_yprediction(const AV1_COMMON *cm, |
| 1619 | PREDICTION_MODE mode, BLOCK_SIZE bsize, |
| 1620 | MACROBLOCK *x, MACROBLOCKD *xd) { |
| 1621 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 1622 | struct macroblock_plane *const p = &x->plane[0]; |
| 1623 | uint8_t *const src_buf_base = p->src.buf; |
| 1624 | uint8_t *const dst_buf_base = pd->dst.buf; |
| 1625 | const int src_stride = p->src.stride; |
| 1626 | const int dst_stride = pd->dst.stride; |
| 1627 | int plane = 0; |
| 1628 | int row, col; |
| 1629 | // block and transform sizes, in number of 4x4 blocks log 2 ("*_b") |
| 1630 | // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8 |
| 1631 | // transform size varies per plane, look it up in a common way. |
| 1632 | const TX_SIZE tx_size = max_txsize_lookup[bsize]; |
| 1633 | const BLOCK_SIZE plane_bsize = |
| 1634 | get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y); |
| 1635 | // If mb_to_right_edge is < 0 we are in a situation in which |
| 1636 | // the current block size extends into the UMV and we won't |
| 1637 | // visit the sub blocks that are wholly within the UMV. |
| 1638 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
| 1639 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 1640 | // Keep track of the row and column of the blocks we use so that we know |
| 1641 | // if we are in the unrestricted motion border. |
| 1642 | for (row = 0; row < max_blocks_high; row += (1 << tx_size)) { |
| 1643 | // Skip visiting the sub blocks that are wholly within the UMV. |
| 1644 | for (col = 0; col < max_blocks_wide; col += (1 << tx_size)) { |
| 1645 | p->src.buf = &src_buf_base[4 * (row * (int64_t)src_stride + col)]; |
| 1646 | pd->dst.buf = &dst_buf_base[4 * (row * (int64_t)dst_stride + col)]; |
| 1647 | av1_predict_intra_block(cm, xd, block_size_wide[bsize], |
| 1648 | block_size_high[bsize], tx_size, mode, 0, 0, |
| 1649 | FILTER_INTRA_MODES, pd->dst.buf, dst_stride, |
| 1650 | pd->dst.buf, dst_stride, 0, 0, plane); |
| 1651 | } |
| 1652 | } |
| 1653 | p->src.buf = src_buf_base; |
| 1654 | pd->dst.buf = dst_buf_base; |
| 1655 | } |
| 1656 | |
chiyotsai | 73ddf44 | 2020-06-01 15:42:23 -0700 | [diff] [blame] | 1657 | void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost, |
| 1658 | BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) { |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1659 | AV1_COMMON *const cm = &cpi->common; |
| 1660 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1661 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 1662 | RD_STATS this_rdc, best_rdc; |
| 1663 | struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 }; |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 1664 | const TxfmSearchParams *txfm_params = &x->txfm_search_params; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1665 | const TX_SIZE intra_tx_size = |
| 1666 | AOMMIN(max_txsize_lookup[bsize], |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 1667 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]); |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1668 | int *bmode_costs; |
Marco Paniconi | f38266b | 2021-03-07 22:43:23 -0800 | [diff] [blame] | 1669 | PREDICTION_MODE best_mode = DC_PRED; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1670 | const MB_MODE_INFO *above_mi = xd->above_mbmi; |
| 1671 | const MB_MODE_INFO *left_mi = xd->left_mbmi; |
| 1672 | const PREDICTION_MODE A = av1_above_block_mode(above_mi); |
| 1673 | const PREDICTION_MODE L = av1_left_block_mode(left_mi); |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1674 | bmode_costs = x->mode_costs.y_mode_costs[A][L]; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1675 | |
| 1676 | av1_invalid_rd_stats(&best_rdc); |
| 1677 | av1_invalid_rd_stats(&this_rdc); |
| 1678 | |
Jerome Jiang | cde6f4a | 2020-01-06 11:14:58 -0800 | [diff] [blame] | 1679 | init_mbmi(mi, DC_PRED, INTRA_FRAME, NONE_FRAME, cm); |
| 1680 | mi->mv[0].as_int = mi->mv[1].as_int = INVALID_MV; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1681 | |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1682 | // Change the limit of this loop to add other intra prediction |
| 1683 | // mode tests. |
| 1684 | for (int i = 0; i < 4; ++i) { |
| 1685 | PREDICTION_MODE this_mode = intra_mode_list[i]; |
| 1686 | this_rdc.dist = this_rdc.rate = 0; |
| 1687 | args.mode = this_mode; |
| 1688 | args.skippable = 1; |
| 1689 | args.rdc = &this_rdc; |
| 1690 | mi->tx_size = intra_tx_size; |
Marco Paniconi | f38266b | 2021-03-07 22:43:23 -0800 | [diff] [blame] | 1691 | mi->mode = this_mode; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1692 | av1_foreach_transformed_block_in_plane(xd, bsize, 0, estimate_block_intra, |
| 1693 | &args); |
Deepa K G | c990ca2 | 2021-02-22 17:11:37 +0530 | [diff] [blame] | 1694 | const int skip_ctx = av1_get_skip_txfm_context(xd); |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1695 | if (args.skippable) { |
Deepa K G | c990ca2 | 2021-02-22 17:11:37 +0530 | [diff] [blame] | 1696 | this_rdc.rate = x->mode_costs.skip_txfm_cost[skip_ctx][1]; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1697 | } else { |
Deepa K G | c990ca2 | 2021-02-22 17:11:37 +0530 | [diff] [blame] | 1698 | this_rdc.rate += x->mode_costs.skip_txfm_cost[skip_ctx][0]; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1699 | } |
| 1700 | this_rdc.rate += bmode_costs[this_mode]; |
| 1701 | this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist); |
| 1702 | |
| 1703 | if (this_rdc.rdcost < best_rdc.rdcost) { |
| 1704 | best_rdc = this_rdc; |
Marco Paniconi | f38266b | 2021-03-07 22:43:23 -0800 | [diff] [blame] | 1705 | best_mode = this_mode; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | |
Marco Paniconi | f38266b | 2021-03-07 22:43:23 -0800 | [diff] [blame] | 1709 | mi->mode = best_mode; |
| 1710 | // Keep DC for UV since mode test is based on Y channel only. |
| 1711 | mi->uv_mode = DC_PRED; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1712 | *rd_cost = best_rdc; |
| 1713 | |
| 1714 | #if CONFIG_INTERNAL_STATS |
| 1715 | store_coding_context(x, ctx, mi->mode); |
| 1716 | #else |
| 1717 | store_coding_context(x, ctx); |
| 1718 | #endif // CONFIG_INTERNAL_STATS |
| 1719 | } |
| 1720 | |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1721 | static AOM_INLINE int is_same_gf_and_last_scale(AV1_COMMON *cm) { |
| 1722 | struct scale_factors *const sf_last = get_ref_scale_factors(cm, LAST_FRAME); |
| 1723 | struct scale_factors *const sf_golden = |
| 1724 | get_ref_scale_factors(cm, GOLDEN_FRAME); |
| 1725 | return ((sf_last->x_scale_fp == sf_golden->x_scale_fp) && |
| 1726 | (sf_last->y_scale_fp == sf_golden->y_scale_fp)); |
| 1727 | } |
| 1728 | |
| 1729 | static AOM_INLINE void get_ref_frame_use_mask(AV1_COMP *cpi, MACROBLOCK *x, |
| 1730 | MB_MODE_INFO *mi, int mi_row, |
| 1731 | int mi_col, int bsize, |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 1732 | int gf_temporal_ref, |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1733 | int use_ref_frame[], |
| 1734 | int *force_skip_low_temp_var) { |
| 1735 | AV1_COMMON *const cm = &cpi->common; |
| 1736 | const struct segmentation *const seg = &cm->seg; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1737 | const int is_small_sb = (cm->seq_params.sb_size == BLOCK_64X64); |
| 1738 | |
Marco Paniconi | bc4a75d | 2020-09-07 22:03:45 -0700 | [diff] [blame] | 1739 | // For SVC the usage of alt_ref is determined by the ref_frame_flags. |
| 1740 | int use_alt_ref_frame = cpi->use_svc || cpi->sf.rt_sf.use_nonrd_altref_frame; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1741 | int use_golden_ref_frame = 1; |
| 1742 | |
| 1743 | use_ref_frame[LAST_FRAME] = 1; // we never skip LAST |
| 1744 | |
| 1745 | if (cpi->rc.frames_since_golden == 0 && gf_temporal_ref) { |
| 1746 | use_golden_ref_frame = 0; |
| 1747 | } |
| 1748 | |
| 1749 | if (cpi->sf.rt_sf.short_circuit_low_temp_var && |
| 1750 | x->nonrd_prune_ref_frame_search) { |
| 1751 | if (is_small_sb) |
| 1752 | *force_skip_low_temp_var = get_force_skip_low_temp_var_small_sb( |
chiyotsai | 68eefbe | 2020-05-01 15:07:58 -0700 | [diff] [blame] | 1753 | &x->part_search_info.variance_low[0], mi_row, mi_col, bsize); |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1754 | else |
| 1755 | *force_skip_low_temp_var = get_force_skip_low_temp_var( |
chiyotsai | 68eefbe | 2020-05-01 15:07:58 -0700 | [diff] [blame] | 1756 | &x->part_search_info.variance_low[0], mi_row, mi_col, bsize); |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1757 | // If force_skip_low_temp_var is set, skip golden reference. |
| 1758 | if (*force_skip_low_temp_var) { |
| 1759 | use_golden_ref_frame = 0; |
| 1760 | use_alt_ref_frame = 0; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) && |
| 1765 | get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) { |
| 1766 | use_golden_ref_frame = 1; |
| 1767 | use_alt_ref_frame = 0; |
| 1768 | } |
| 1769 | |
| 1770 | use_alt_ref_frame = |
| 1771 | cpi->ref_frame_flags & AOM_ALT_FLAG ? use_alt_ref_frame : 0; |
| 1772 | use_golden_ref_frame = |
| 1773 | cpi->ref_frame_flags & AOM_GOLD_FLAG ? use_golden_ref_frame : 0; |
| 1774 | |
| 1775 | use_ref_frame[ALTREF_FRAME] = use_alt_ref_frame; |
| 1776 | use_ref_frame[GOLDEN_FRAME] = use_golden_ref_frame; |
| 1777 | } |
| 1778 | |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1779 | /*!\brief Estimates best intra mode for inter mode search |
| 1780 | * |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 1781 | * \ingroup nonrd_mode_search |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1782 | * \callgraph |
| 1783 | * \callergraph |
| 1784 | * |
| 1785 | * Using heuristics based on best inter mode, block size, and other decides |
| 1786 | * whether to check intra modes. If so, estimates and selects best intra mode |
| 1787 | * from the reduced set of intra modes (max 4 intra modes checked) |
| 1788 | * |
| 1789 | * \param[in] cpi Top-level encoder structure |
| 1790 | * \param[in] x Pointer to structure holding all the |
| 1791 | * data for the current macroblock |
| 1792 | * \param[in] bsize Current block size |
| 1793 | * \param[in] use_modeled_non_rd_cost Flag, indicating usage of curvfit |
| 1794 | * model for RD cost |
| 1795 | * \param[in] best_early_term Flag, indicating that TX for the |
| 1796 | * best inter mode was skipped |
| 1797 | * \param[in] ref_cost_intra Cost of signalling intra mode |
| 1798 | * \param[in] reuse_prediction Flag, indicating prediction re-use |
| 1799 | * \param[in] orig_dst Original destination buffer |
| 1800 | * \param[in] tmp_buffers Pointer to a temporary buffers for |
| 1801 | * prediction re-use |
| 1802 | * \param[out] this_mode_pred Pointer to store prediction buffer |
| 1803 | * for prediction re-use |
| 1804 | * \param[in] best_rdc Pointer to RD cost for the best |
| 1805 | * selected intra mode |
| 1806 | * \param[in] best_pickmode Pointer to a structure containing |
| 1807 | * best mode picked so far |
| 1808 | * |
| 1809 | * \return Nothing is returned. Instead, calculated RD cost is placed to |
| 1810 | * \c best_rdc and best selected mode is placed to \c best_pickmode |
| 1811 | */ |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1812 | static void estimate_intra_mode( |
| 1813 | AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int use_modeled_non_rd_cost, |
| 1814 | int best_early_term, unsigned int ref_cost_intra, int reuse_prediction, |
| 1815 | struct buf_2d *orig_dst, PRED_BUFFER *tmp_buffers, |
| 1816 | PRED_BUFFER **this_mode_pred, RD_STATS *best_rdc, |
| 1817 | BEST_PICKMODE *best_pickmode) { |
| 1818 | AV1_COMMON *const cm = &cpi->common; |
| 1819 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1820 | MB_MODE_INFO *const mi = xd->mi[0]; |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 1821 | const TxfmSearchParams *txfm_params = &x->txfm_search_params; |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1822 | const unsigned char segment_id = mi->segment_id; |
| 1823 | const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize]; |
| 1824 | const int *const rd_thresh_freq_fact = x->thresh_freq_fact[bsize]; |
| 1825 | const int mi_row = xd->mi_row; |
| 1826 | const int mi_col = xd->mi_col; |
| 1827 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 1828 | |
| 1829 | const CommonQuantParams *quant_params = &cm->quant_params; |
| 1830 | |
| 1831 | RD_STATS this_rdc; |
| 1832 | |
| 1833 | int intra_cost_penalty = av1_get_intra_cost_penalty( |
| 1834 | quant_params->base_qindex, quant_params->y_dc_delta_q, |
| 1835 | cm->seq_params.bit_depth); |
| 1836 | int64_t inter_mode_thresh = RDCOST(x->rdmult, intra_cost_penalty, 0); |
| 1837 | int perform_intra_pred = cpi->sf.rt_sf.check_intra_pred_nonrd; |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 1838 | // For spatial enhancemanent layer: turn off intra prediction if the |
| 1839 | // previous spatial layer as golden ref is not chosen as best reference. |
| 1840 | // only do this for temporal enhancement layer and on non-key frames. |
| 1841 | if (cpi->svc.spatial_layer_id > 0 && |
| 1842 | best_pickmode->best_ref_frame != GOLDEN_FRAME && |
| 1843 | cpi->svc.temporal_layer_id > 0 && |
| 1844 | !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame) |
| 1845 | perform_intra_pred = 0; |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1846 | |
| 1847 | int do_early_exit_rdthresh = 1; |
| 1848 | |
| 1849 | uint32_t spatial_var_thresh = 50; |
| 1850 | int motion_thresh = 32; |
| 1851 | // Adjust thresholds to make intra mode likely tested if the other |
Marco Paniconi | bc4a75d | 2020-09-07 22:03:45 -0700 | [diff] [blame] | 1852 | // references (golden, alt) are skipped/not checked. For now always |
| 1853 | // adjust for svc mode. |
| 1854 | if (cpi->use_svc || (cpi->sf.rt_sf.use_nonrd_altref_frame == 0 && |
| 1855 | cpi->sf.rt_sf.nonrd_prune_ref_frame_search > 0)) { |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1856 | spatial_var_thresh = 150; |
| 1857 | motion_thresh = 0; |
| 1858 | } |
| 1859 | |
| 1860 | // Some adjustments to checking intra mode based on source variance. |
| 1861 | if (x->source_variance < spatial_var_thresh) { |
| 1862 | // If the best inter mode is large motion or non-LAST ref reduce intra cost |
| 1863 | // penalty, so intra mode is more likely tested. |
| 1864 | if (best_pickmode->best_ref_frame != LAST_FRAME || |
| 1865 | abs(mi->mv[0].as_mv.row) >= motion_thresh || |
| 1866 | abs(mi->mv[0].as_mv.col) >= motion_thresh) { |
| 1867 | intra_cost_penalty = intra_cost_penalty >> 2; |
| 1868 | inter_mode_thresh = RDCOST(x->rdmult, intra_cost_penalty, 0); |
| 1869 | do_early_exit_rdthresh = 0; |
| 1870 | } |
| 1871 | // For big blocks worth checking intra (since only DC will be checked), |
| 1872 | // even if best_early_term is set. |
| 1873 | if (bsize >= BLOCK_32X32) best_early_term = 0; |
| 1874 | } else if (cpi->sf.rt_sf.source_metrics_sb_nonrd && |
Marco Paniconi | 988b34a | 2020-11-09 12:41:13 -0800 | [diff] [blame] | 1875 | x->content_state_sb.source_sad == kLowSad) { |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1876 | perform_intra_pred = 0; |
| 1877 | } |
| 1878 | |
Fyodor Kyslov | e0be17e | 2020-05-13 14:20:50 -0700 | [diff] [blame] | 1879 | if (cpi->sf.rt_sf.skip_intra_pred_if_tx_skip && best_rdc->skip_txfm && |
| 1880 | best_pickmode->best_mode_initial_skip_flag) { |
| 1881 | perform_intra_pred = 0; |
| 1882 | } |
| 1883 | |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1884 | if (!(best_rdc->rdcost == INT64_MAX || |
| 1885 | (perform_intra_pred && !best_early_term && |
| 1886 | best_rdc->rdcost > inter_mode_thresh && |
| 1887 | bsize <= cpi->sf.part_sf.max_intra_bsize))) { |
| 1888 | return; |
| 1889 | } |
| 1890 | |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1891 | struct estimate_block_intra_args args = { cpi, x, DC_PRED, 1, 0 }; |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 1892 | TX_SIZE intra_tx_size = AOMMIN( |
| 1893 | AOMMIN(max_txsize_lookup[bsize], |
| 1894 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]), |
| 1895 | TX_16X16); |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1896 | |
| 1897 | PRED_BUFFER *const best_pred = best_pickmode->best_pred; |
| 1898 | if (reuse_prediction && best_pred != NULL) { |
| 1899 | const int bh = block_size_high[bsize]; |
| 1900 | const int bw = block_size_wide[bsize]; |
| 1901 | if (best_pred->data == orig_dst->buf) { |
| 1902 | *this_mode_pred = &tmp_buffers[get_pred_buffer(tmp_buffers, 3)]; |
| 1903 | aom_convolve_copy(best_pred->data, best_pred->stride, |
| 1904 | (*this_mode_pred)->data, (*this_mode_pred)->stride, bw, |
| 1905 | bh); |
| 1906 | best_pickmode->best_pred = *this_mode_pred; |
| 1907 | } |
| 1908 | } |
| 1909 | pd->dst = *orig_dst; |
| 1910 | |
| 1911 | for (int i = 0; i < 4; ++i) { |
| 1912 | const PREDICTION_MODE this_mode = intra_mode_list[i]; |
| 1913 | const THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)]; |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 1914 | const int64_t mode_rd_thresh = rd_threshes[mode_index]; |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1915 | |
Marco Paniconi | 06affb6 | 2020-10-07 16:11:29 -0700 | [diff] [blame] | 1916 | if (!((1 << this_mode) & cpi->sf.rt_sf.intra_y_mode_bsize_mask_nrd[bsize])) |
Fyodor Kyslov | 2b9242a | 2020-06-19 11:12:59 -0700 | [diff] [blame] | 1917 | continue; |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1918 | |
| 1919 | if (rd_less_than_thresh(best_rdc->rdcost, mode_rd_thresh, |
| 1920 | rd_thresh_freq_fact[mode_index]) && |
| 1921 | (do_early_exit_rdthresh || this_mode == SMOOTH_PRED)) { |
| 1922 | continue; |
| 1923 | } |
| 1924 | const BLOCK_SIZE uv_bsize = get_plane_block_size( |
| 1925 | bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y); |
| 1926 | |
| 1927 | mi->mode = this_mode; |
| 1928 | mi->ref_frame[0] = INTRA_FRAME; |
| 1929 | mi->ref_frame[1] = NONE_FRAME; |
| 1930 | |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1931 | av1_invalid_rd_stats(&this_rdc); |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1932 | args.mode = this_mode; |
| 1933 | args.skippable = 1; |
| 1934 | args.rdc = &this_rdc; |
| 1935 | mi->tx_size = intra_tx_size; |
| 1936 | compute_intra_yprediction(cm, this_mode, bsize, x, xd); |
| 1937 | // Look into selecting tx_size here, based on prediction residual. |
| 1938 | if (use_modeled_non_rd_cost) |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 1939 | model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, 1); |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1940 | else |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1941 | block_yrd(cpi, x, mi_row, mi_col, &this_rdc, &args.skippable, bsize, |
| 1942 | mi->tx_size); |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1943 | // TODO(kyslov@) Need to account for skippable |
| 1944 | if (x->color_sensitivity[0]) { |
| 1945 | av1_foreach_transformed_block_in_plane(xd, uv_bsize, 1, |
| 1946 | estimate_block_intra, &args); |
| 1947 | } |
| 1948 | if (x->color_sensitivity[1]) { |
| 1949 | av1_foreach_transformed_block_in_plane(xd, uv_bsize, 2, |
| 1950 | estimate_block_intra, &args); |
| 1951 | } |
| 1952 | |
| 1953 | int mode_cost = 0; |
| 1954 | if (av1_is_directional_mode(this_mode) && av1_use_angle_delta(bsize)) { |
| 1955 | mode_cost += |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1956 | x->mode_costs.angle_delta_cost[this_mode - V_PRED] |
| 1957 | [MAX_ANGLE_DELTA + |
| 1958 | mi->angle_delta[PLANE_TYPE_Y]]; |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1959 | } |
| 1960 | if (this_mode == DC_PRED && av1_filter_intra_allowed_bsize(cm, bsize)) { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1961 | mode_cost += x->mode_costs.filter_intra_cost[bsize][0]; |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1962 | } |
| 1963 | this_rdc.rate += ref_cost_intra; |
| 1964 | this_rdc.rate += intra_cost_penalty; |
| 1965 | this_rdc.rate += mode_cost; |
| 1966 | this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist); |
| 1967 | |
| 1968 | if (this_rdc.rdcost < best_rdc->rdcost) { |
| 1969 | *best_rdc = this_rdc; |
| 1970 | best_pickmode->best_mode = this_mode; |
Fyodor Kyslov | e0be17e | 2020-05-13 14:20:50 -0700 | [diff] [blame] | 1971 | best_pickmode->best_tx_size = mi->tx_size; |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1972 | best_pickmode->best_ref_frame = INTRA_FRAME; |
| 1973 | mi->uv_mode = this_mode; |
| 1974 | mi->mv[0].as_int = INVALID_MV; |
| 1975 | mi->mv[1].as_int = INVALID_MV; |
| 1976 | } |
| 1977 | } |
Fyodor Kyslov | e0be17e | 2020-05-13 14:20:50 -0700 | [diff] [blame] | 1978 | mi->tx_size = best_pickmode->best_tx_size; |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
chiyotsai | 7f6e21a | 2020-06-16 14:52:30 -0700 | [diff] [blame] | 1981 | static AOM_INLINE int is_filter_search_enabled(const AV1_COMP *cpi, int mi_row, |
| 1982 | int mi_col, BLOCK_SIZE bsize) { |
| 1983 | const AV1_COMMON *const cm = &cpi->common; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1984 | int enable_filter_search = 0; |
| 1985 | |
| 1986 | if (cpi->sf.rt_sf.use_nonrd_filter_search) { |
| 1987 | enable_filter_search = 1; |
| 1988 | if (cpi->sf.interp_sf.cb_pred_filter_search) { |
| 1989 | const int bsl = mi_size_wide_log2[bsize]; |
| 1990 | enable_filter_search = |
| 1991 | (((mi_row + mi_col) >> bsl) + |
| 1992 | get_chessboard_index(cm->current_frame.frame_number)) & |
| 1993 | 0x1; |
| 1994 | } |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1995 | } |
| 1996 | return enable_filter_search; |
| 1997 | } |
| 1998 | |
| 1999 | static AOM_INLINE int skip_mode_by_threshold( |
| 2000 | PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, int_mv mv, |
| 2001 | int frames_since_golden, const int *const rd_threshes, |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 2002 | const int *const rd_thresh_freq_fact, int64_t best_cost, int best_skip, |
| 2003 | int extra_shift) { |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2004 | int skip_this_mode = 0; |
| 2005 | const THR_MODES mode_index = mode_idx[ref_frame][INTER_OFFSET(mode)]; |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 2006 | int64_t mode_rd_thresh = |
| 2007 | best_skip ? ((int64_t)rd_threshes[mode_index]) << (extra_shift + 1) |
| 2008 | : ((int64_t)rd_threshes[mode_index]) << extra_shift; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2009 | |
| 2010 | // Increase mode_rd_thresh value for non-LAST for improved encoding |
| 2011 | // speed |
| 2012 | if (ref_frame != LAST_FRAME) { |
| 2013 | mode_rd_thresh = mode_rd_thresh << 1; |
| 2014 | if (ref_frame == GOLDEN_FRAME && frames_since_golden > 4) |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 2015 | mode_rd_thresh = mode_rd_thresh << (extra_shift + 1); |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | if (rd_less_than_thresh(best_cost, mode_rd_thresh, |
| 2019 | rd_thresh_freq_fact[mode_index])) |
| 2020 | if (mv.as_int != 0) skip_this_mode = 1; |
| 2021 | |
| 2022 | return skip_this_mode; |
| 2023 | } |
| 2024 | |
Marco Paniconi | 988b34a | 2020-11-09 12:41:13 -0800 | [diff] [blame] | 2025 | static AOM_INLINE int skip_mode_by_low_temp( |
| 2026 | PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize, |
| 2027 | CONTENT_STATE_SB content_state_sb, int_mv mv, int force_skip_low_temp_var) { |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2028 | // Skip non-zeromv mode search for non-LAST frame if force_skip_low_temp_var |
| 2029 | // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped |
| 2030 | // later. |
| 2031 | if (force_skip_low_temp_var && ref_frame != LAST_FRAME && mv.as_int != 0) { |
| 2032 | return 1; |
| 2033 | } |
| 2034 | |
Marco Paniconi | 988b34a | 2020-11-09 12:41:13 -0800 | [diff] [blame] | 2035 | if (content_state_sb.source_sad != kHighSad && bsize >= BLOCK_64X64 && |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2036 | force_skip_low_temp_var && mode == NEWMV) { |
| 2037 | return 1; |
| 2038 | } |
| 2039 | return 0; |
| 2040 | } |
| 2041 | |
| 2042 | static AOM_INLINE int skip_mode_by_bsize_and_ref_frame( |
| 2043 | PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize, |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 2044 | int extra_prune, unsigned int sse_zeromv_norm, int more_prune) { |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2045 | const unsigned int thresh_skip_golden = 500; |
| 2046 | |
| 2047 | if (ref_frame != LAST_FRAME && sse_zeromv_norm < thresh_skip_golden && |
| 2048 | mode == NEWMV) |
| 2049 | return 1; |
| 2050 | |
| 2051 | if (bsize == BLOCK_128X128 && mode == NEWMV) return 1; |
| 2052 | |
| 2053 | // Skip testing non-LAST if this flag is set. |
| 2054 | if (extra_prune) { |
| 2055 | if (extra_prune > 1 && ref_frame != LAST_FRAME && |
| 2056 | (bsize > BLOCK_64X64 || (bsize > BLOCK_16X16 && mode == NEWMV))) |
| 2057 | return 1; |
| 2058 | |
| 2059 | if (ref_frame != LAST_FRAME && mode == NEARMV) return 1; |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 2060 | |
| 2061 | if (more_prune && bsize >= BLOCK_32X32 && mode == NEARMV) return 1; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2062 | } |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
Fyodor Kyslov | 915b9b8 | 2019-11-11 11:53:03 -0800 | [diff] [blame] | 2066 | void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data, |
Hui Su | b94cd5e | 2019-11-06 12:05:47 -0800 | [diff] [blame] | 2067 | MACROBLOCK *x, RD_STATS *rd_cost, |
Marco Paniconi | 11067db | 2020-05-18 11:11:42 -0700 | [diff] [blame] | 2068 | BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2069 | AV1_COMMON *const cm = &cpi->common; |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 2070 | SVC *const svc = &cpi->svc; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2071 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2072 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 2073 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 2074 | |
| 2075 | BEST_PICKMODE best_pickmode; |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2076 | #if COLLECT_PICK_MODE_STAT |
| 2077 | static mode_search_stat ms_stat; |
| 2078 | #endif |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2079 | MV_REFERENCE_FRAME ref_frame; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2080 | int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES]; |
| 2081 | uint8_t mode_checked[MB_MODE_COUNT][REF_FRAMES]; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2082 | struct buf_2d yv12_mb[REF_FRAMES][MAX_MB_PLANE]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2083 | RD_STATS this_rdc, best_rdc; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2084 | const unsigned char segment_id = mi->segment_id; |
| 2085 | const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize]; |
Ravi Chaudhary | b61cdea | 2019-07-05 15:01:20 +0530 | [diff] [blame] | 2086 | const int *const rd_thresh_freq_fact = x->thresh_freq_fact[bsize]; |
Fyodor Kyslov | 9fa800d | 2020-05-05 13:56:35 -0700 | [diff] [blame] | 2087 | const InterpFilter filter_ref = cm->features.interp_filter; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2088 | int best_early_term = 0; |
Jerome Jiang | 037f5d2 | 2019-05-02 19:32:42 -0700 | [diff] [blame] | 2089 | unsigned int ref_costs_single[REF_FRAMES], |
| 2090 | ref_costs_comp[REF_FRAMES][REF_FRAMES]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2091 | int force_skip_low_temp_var = 0; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2092 | int use_ref_frame_mask[REF_FRAMES] = { 0 }; |
Fyodor Kyslov | 84abaea | 2019-06-25 14:44:14 -0700 | [diff] [blame] | 2093 | unsigned int sse_zeromv_norm = UINT_MAX; |
Marco Paniconi | 5a2e549 | 2020-12-03 10:22:47 -0800 | [diff] [blame] | 2094 | // Use mode set that includes zeromv (via globalmv) for speed >= 9 for |
| 2095 | // content with low motion. |
| 2096 | int use_zeromv = |
| 2097 | ((cpi->oxcf.speed >= 9 && cpi->rc.avg_frame_low_motion > 70) || |
| 2098 | cpi->sf.rt_sf.nonrd_agressive_skip); |
| 2099 | const int num_inter_modes = |
| 2100 | use_zeromv ? NUM_INTER_MODES_REDUCED : NUM_INTER_MODES_RT; |
| 2101 | const REF_MODE *const ref_mode_set = |
| 2102 | use_zeromv ? ref_mode_set_reduced : ref_mode_set_rt; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2103 | PRED_BUFFER tmp[4]; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 2104 | DECLARE_ALIGNED(16, uint8_t, pred_buf[3 * 128 * 128]); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2105 | PRED_BUFFER *this_mode_pred = NULL; |
Fyodor Kyslov | fb9f22c | 2021-02-22 17:35:50 -0800 | [diff] [blame] | 2106 | const int reuse_inter_pred = cpi->sf.rt_sf.reuse_inter_pred_nonrd && |
| 2107 | cm->seq_params.bit_depth == AOM_BITS_8; |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 2108 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2109 | const int bh = block_size_high[bsize]; |
| 2110 | const int bw = block_size_wide[bsize]; |
| 2111 | const int pixels_in_block = bh * bw; |
| 2112 | struct buf_2d orig_dst = pd->dst; |
Urvang Joshi | 1781462 | 2020-03-27 17:26:17 -0700 | [diff] [blame] | 2113 | const CommonQuantParams *quant_params = &cm->quant_params; |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 2114 | const TxfmSearchParams *txfm_params = &x->txfm_search_params; |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 2115 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2116 | #if COLLECT_PICK_MODE_STAT |
| 2117 | aom_usec_timer_start(&ms_stat.timer2); |
| 2118 | #endif |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2119 | const InterpFilter default_interp_filter = EIGHTTAP_REGULAR; |
Marco Paniconi | b3565a2 | 2020-03-05 15:29:08 -0800 | [diff] [blame] | 2120 | int64_t thresh_sad_pred = INT64_MAX; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2121 | const int mi_row = xd->mi_row; |
| 2122 | const int mi_col = xd->mi_col; |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 2123 | int svc_mv_col = 0; |
| 2124 | int svc_mv_row = 0; |
| 2125 | int force_mv_inter_layer = 0; |
Marco Paniconi | 0fac7ff | 2020-08-14 11:02:28 -0700 | [diff] [blame] | 2126 | int use_modeled_non_rd_cost = 0; |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 2127 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 2128 | const int denoise_recheck_zeromv = 1; |
| 2129 | AV1_PICKMODE_CTX_DEN ctx_den; |
| 2130 | int64_t zero_last_cost_orig = INT64_MAX; |
| 2131 | int denoise_svc_pickmode = 1; |
| 2132 | const int resize_pending = |
| 2133 | (cpi->resize_pending_params.width && cpi->resize_pending_params.height && |
| 2134 | (cpi->common.width != cpi->resize_pending_params.width || |
| 2135 | cpi->common.height != cpi->resize_pending_params.height)); |
| 2136 | |
| 2137 | #endif |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 2138 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2139 | init_best_pickmode(&best_pickmode); |
| 2140 | |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 2141 | const ModeCosts *mode_costs = &x->mode_costs; |
| 2142 | |
| 2143 | estimate_single_ref_frame_costs(cm, xd, mode_costs, segment_id, |
| 2144 | ref_costs_single); |
chiyotsai | 56681b5 | 2019-12-16 11:15:25 -0800 | [diff] [blame] | 2145 | if (cpi->sf.rt_sf.use_comp_ref_nonrd) |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 2146 | estimate_comp_ref_frame_costs(cm, xd, mode_costs, segment_id, |
| 2147 | ref_costs_comp); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2148 | |
| 2149 | memset(&mode_checked[0][0], 0, MB_MODE_COUNT * REF_FRAMES); |
| 2150 | if (reuse_inter_pred) { |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 2151 | for (int i = 0; i < 3; i++) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2152 | tmp[i].data = &pred_buf[pixels_in_block * i]; |
| 2153 | tmp[i].stride = bw; |
| 2154 | tmp[i].in_use = 0; |
| 2155 | } |
| 2156 | tmp[3].data = pd->dst.buf; |
| 2157 | tmp[3].stride = pd->dst.stride; |
| 2158 | tmp[3].in_use = 0; |
| 2159 | } |
| 2160 | |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 2161 | txfm_info->skip_txfm = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2162 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2163 | // initialize mode decisions |
| 2164 | av1_invalid_rd_stats(&best_rdc); |
kyslov | ad34be7 | 2019-05-03 14:04:02 -0700 | [diff] [blame] | 2165 | av1_invalid_rd_stats(&this_rdc); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2166 | av1_invalid_rd_stats(rd_cost); |
chiyotsai | 0f5cd05 | 2020-08-27 14:37:44 -0700 | [diff] [blame] | 2167 | mi->bsize = bsize; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2168 | mi->ref_frame[0] = NONE_FRAME; |
| 2169 | mi->ref_frame[1] = NONE_FRAME; |
| 2170 | |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 2171 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 2172 | if (cpi->oxcf.noise_sensitivity > 0) { |
| 2173 | // if (cpi->use_svc) denoise_svc_pickmode = av1_denoise_svc_non_key(cpi); |
| 2174 | if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode) |
| 2175 | av1_denoiser_reset_frame_stats(ctx); |
| 2176 | } |
| 2177 | #endif |
| 2178 | |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 2179 | const int gf_temporal_ref = is_same_gf_and_last_scale(cm); |
| 2180 | |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 2181 | // If the lower spatial layer uses an averaging filter for downsampling |
| 2182 | // (phase = 8), the target decimated pixel is shifted by (1/2, 1/2) relative |
| 2183 | // to source, so use subpel motion vector to compensate. The nonzero motion |
| 2184 | // is half pixel shifted to left and top, so (-4, -4). This has more effect |
| 2185 | // on higher resolutins, so condition it on that for now. |
| 2186 | if (cpi->use_svc && svc->spatial_layer_id > 0 && |
| 2187 | svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 && |
| 2188 | cm->width * cm->height > 640 * 480) { |
| 2189 | svc_mv_col = -4; |
| 2190 | svc_mv_row = -4; |
| 2191 | } |
| 2192 | |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 2193 | get_ref_frame_use_mask(cpi, x, mi, mi_row, mi_col, bsize, gf_temporal_ref, |
| 2194 | use_ref_frame_mask, &force_skip_low_temp_var); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2195 | |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 2196 | for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2197 | ref_frame_iter <= ALTREF_FRAME; ++ref_frame_iter) { |
| 2198 | if (use_ref_frame_mask[ref_frame_iter]) { |
Fyodor Kyslov | 9fa800d | 2020-05-05 13:56:35 -0700 | [diff] [blame] | 2199 | find_predictors(cpi, x, ref_frame_iter, frame_mv, tile_data, yv12_mb, |
| 2200 | bsize, force_skip_low_temp_var); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2201 | } |
| 2202 | } |
Marco Paniconi | b3565a2 | 2020-03-05 15:29:08 -0800 | [diff] [blame] | 2203 | |
| 2204 | thresh_sad_pred = ((int64_t)x->pred_mv_sad[LAST_FRAME]) << 1; |
| 2205 | // Increase threshold for less agressive pruning. |
| 2206 | if (cpi->sf.rt_sf.nonrd_prune_ref_frame_search == 1) |
| 2207 | thresh_sad_pred += (x->pred_mv_sad[LAST_FRAME] >> 2); |
| 2208 | |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 2209 | const int large_block = bsize >= BLOCK_32X32; |
| 2210 | const int use_model_yrd_large = |
Vishesh | 39e7409 | 2020-06-16 17:13:48 +0530 | [diff] [blame] | 2211 | cpi->oxcf.rc_cfg.mode == AOM_CBR && large_block && |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 2212 | !cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) && |
Fyodor Kyslov | c7825d0 | 2020-03-31 15:04:01 -0700 | [diff] [blame] | 2213 | quant_params->base_qindex && cm->seq_params.bit_depth == 8; |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2214 | |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2215 | const int enable_filter_search = |
chiyotsai | 7f6e21a | 2020-06-16 14:52:30 -0700 | [diff] [blame] | 2216 | is_filter_search_enabled(cpi, mi_row, mi_col, bsize); |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2217 | |
| 2218 | // TODO(marpan): Look into reducing these conditions. For now constrain |
| 2219 | // it to avoid significant bdrate loss. |
Marco Paniconi | 0fac7ff | 2020-08-14 11:02:28 -0700 | [diff] [blame] | 2220 | if (cpi->sf.rt_sf.use_modeled_non_rd_cost) { |
| 2221 | if (cpi->svc.non_reference_frame) |
| 2222 | use_modeled_non_rd_cost = 1; |
| 2223 | else if (cpi->svc.number_temporal_layers > 1 && |
| 2224 | cpi->svc.temporal_layer_id == 0) |
| 2225 | use_modeled_non_rd_cost = 0; |
| 2226 | else |
| 2227 | use_modeled_non_rd_cost = |
| 2228 | (quant_params->base_qindex > 120 && x->source_variance > 100 && |
Marco Paniconi | 988b34a | 2020-11-09 12:41:13 -0800 | [diff] [blame] | 2229 | bsize <= BLOCK_16X16 && !x->content_state_sb.lighting_change && |
| 2230 | x->content_state_sb.source_sad != kHighSad); |
Marco Paniconi | 0fac7ff | 2020-08-14 11:02:28 -0700 | [diff] [blame] | 2231 | } |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2232 | |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2233 | #if COLLECT_PICK_MODE_STAT |
| 2234 | ms_stat.num_blocks[bsize]++; |
| 2235 | #endif |
Fyodor Kyslov | ad32ace | 2020-01-08 14:58:21 -0800 | [diff] [blame] | 2236 | init_mbmi(mi, DC_PRED, NONE_FRAME, NONE_FRAME, cm); |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 2237 | mi->tx_size = AOMMIN( |
| 2238 | AOMMIN(max_txsize_lookup[bsize], |
| 2239 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]), |
| 2240 | TX_16X16); |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2241 | |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 2242 | for (int idx = 0; idx < num_inter_modes; ++idx) { |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2243 | const struct segmentation *const seg = &cm->seg; |
| 2244 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2245 | int rate_mv = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2246 | int is_skippable; |
| 2247 | int this_early_term = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2248 | int skip_this_mv = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2249 | PREDICTION_MODE this_mode; |
chiyotsai | 0b90c41 | 2020-09-29 14:48:16 -0700 | [diff] [blame] | 2250 | MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext; |
Fyodor Kyslov | e0be17e | 2020-05-13 14:20:50 -0700 | [diff] [blame] | 2251 | RD_STATS nonskip_rdc; |
| 2252 | av1_invalid_rd_stats(&nonskip_rdc); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2253 | |
| 2254 | this_mode = ref_mode_set[idx].pred_mode; |
| 2255 | ref_frame = ref_mode_set[idx].ref_frame; |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2256 | |
| 2257 | #if COLLECT_PICK_MODE_STAT |
| 2258 | aom_usec_timer_start(&ms_stat.timer1); |
| 2259 | ms_stat.num_searches[bsize][this_mode]++; |
| 2260 | #endif |
Fyodor Kyslov | ad32ace | 2020-01-08 14:58:21 -0800 | [diff] [blame] | 2261 | mi->mode = this_mode; |
| 2262 | mi->ref_frame[0] = ref_frame; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2263 | |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2264 | if (!use_ref_frame_mask[ref_frame]) continue; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2265 | |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 2266 | force_mv_inter_layer = 0; |
| 2267 | if (cpi->use_svc && svc->spatial_layer_id > 0 && |
| 2268 | ((ref_frame == LAST_FRAME && svc->skip_mvsearch_last) || |
| 2269 | (ref_frame == GOLDEN_FRAME && svc->skip_mvsearch_gf))) { |
| 2270 | // Only test mode if NEARESTMV/NEARMV is (svc_mv_col, svc_mv_row), |
| 2271 | // otherwise set NEWMV to (svc_mv_col, svc_mv_row). |
| 2272 | // Skip newmv and filter search. |
| 2273 | force_mv_inter_layer = 1; |
| 2274 | if (this_mode == NEWMV) { |
| 2275 | frame_mv[this_mode][ref_frame].as_mv.col = svc_mv_col; |
| 2276 | frame_mv[this_mode][ref_frame].as_mv.row = svc_mv_row; |
| 2277 | } else if (frame_mv[this_mode][ref_frame].as_mv.col != svc_mv_col || |
| 2278 | frame_mv[this_mode][ref_frame].as_mv.row != svc_mv_row) { |
Marco Paniconi | ddd5666 | 2019-08-28 15:46:56 -0700 | [diff] [blame] | 2279 | continue; |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 2280 | } |
Marco Paniconi | ddd5666 | 2019-08-28 15:46:56 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2283 | // If the segment reference frame feature is enabled then do nothing if the |
| 2284 | // current ref frame is not allowed. |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2285 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && |
| 2286 | get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2287 | continue; |
| 2288 | |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 2289 | if (skip_mode_by_bsize_and_ref_frame( |
| 2290 | this_mode, ref_frame, bsize, x->nonrd_prune_ref_frame_search, |
| 2291 | sse_zeromv_norm, cpi->sf.rt_sf.nonrd_agressive_skip)) |
Fyodor Kyslov | 84abaea | 2019-06-25 14:44:14 -0700 | [diff] [blame] | 2292 | continue; |
| 2293 | |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2294 | if (skip_mode_by_low_temp(this_mode, ref_frame, bsize, x->content_state_sb, |
| 2295 | frame_mv[this_mode][ref_frame], |
| 2296 | force_skip_low_temp_var)) |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2297 | continue; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2298 | |
Marco Paniconi | b3565a2 | 2020-03-05 15:29:08 -0800 | [diff] [blame] | 2299 | // Disable this drop out case if the ref frame segment level feature is |
| 2300 | // enabled for this segment. This is to prevent the possibility that we |
| 2301 | // end up unable to pick any mode. |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2302 | if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) { |
Marco Paniconi | b3565a2 | 2020-03-05 15:29:08 -0800 | [diff] [blame] | 2303 | // Check for skipping GOLDEN and ALTREF based pred_mv_sad. |
| 2304 | if (cpi->sf.rt_sf.nonrd_prune_ref_frame_search > 0 && |
Marco Paniconi | d1d4628 | 2020-04-02 09:16:14 -0700 | [diff] [blame] | 2305 | x->pred_mv_sad[ref_frame] != INT_MAX && ref_frame != LAST_FRAME) { |
Fyodor Kyslov | 9fa800d | 2020-05-05 13:56:35 -0700 | [diff] [blame] | 2306 | if ((int64_t)(x->pred_mv_sad[ref_frame]) > thresh_sad_pred) continue; |
Marco Paniconi | b3565a2 | 2020-03-05 15:29:08 -0800 | [diff] [blame] | 2307 | } |
Marco Paniconi | b3565a2 | 2020-03-05 15:29:08 -0800 | [diff] [blame] | 2308 | } |
| 2309 | |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2310 | if (skip_mode_by_threshold( |
| 2311 | this_mode, ref_frame, frame_mv[this_mode][ref_frame], |
| 2312 | cpi->rc.frames_since_golden, rd_threshes, rd_thresh_freq_fact, |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 2313 | best_rdc.rdcost, best_pickmode.best_mode_skip_txfm, |
| 2314 | (cpi->sf.rt_sf.nonrd_agressive_skip ? 1 : 0))) |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 2315 | continue; |
| 2316 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2317 | // Select prediction reference frames. |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 2318 | for (int i = 0; i < MAX_MB_PLANE; i++) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2319 | xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; |
| 2320 | } |
| 2321 | |
| 2322 | mi->ref_frame[0] = ref_frame; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 2323 | mi->ref_frame[1] = NONE_FRAME; |
| 2324 | set_ref_ptrs(cm, xd, ref_frame, NONE_FRAME); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2325 | |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 2326 | if (this_mode == NEWMV && !force_mv_inter_layer) { |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 2327 | if (search_new_mv(cpi, x, frame_mv, ref_frame, gf_temporal_ref, bsize, |
| 2328 | mi_row, mi_col, &rate_mv, &best_rdc)) |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2329 | continue; |
| 2330 | } |
| 2331 | |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 2332 | for (PREDICTION_MODE inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV; |
| 2333 | inter_mv_mode++) { |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 2334 | if (inter_mv_mode == this_mode) continue; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2335 | if (mode_checked[inter_mv_mode][ref_frame] && |
| 2336 | frame_mv[this_mode][ref_frame].as_int == |
Marco Paniconi | 6ba75b7 | 2020-03-16 14:13:53 -0700 | [diff] [blame] | 2337 | frame_mv[inter_mv_mode][ref_frame].as_int) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2338 | skip_this_mv = 1; |
| 2339 | break; |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | if (skip_this_mv) continue; |
| 2344 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2345 | mi->mode = this_mode; |
| 2346 | mi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int; |
| 2347 | mi->mv[1].as_int = 0; |
| 2348 | if (reuse_inter_pred) { |
| 2349 | if (!this_mode_pred) { |
| 2350 | this_mode_pred = &tmp[3]; |
| 2351 | } else { |
| 2352 | this_mode_pred = &tmp[get_pred_buffer(tmp, 3)]; |
| 2353 | pd->dst.buf = this_mode_pred->data; |
| 2354 | pd->dst.stride = bw; |
| 2355 | } |
| 2356 | } |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2357 | #if COLLECT_PICK_MODE_STAT |
| 2358 | ms_stat.num_nonskipped_searches[bsize][this_mode]++; |
| 2359 | #endif |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 2360 | if (enable_filter_search && !force_mv_inter_layer && |
Fyodor Kyslov | 0fc618e | 2019-10-02 16:25:14 -0700 | [diff] [blame] | 2361 | ((mi->mv[0].as_mv.row & 0x07) || (mi->mv[0].as_mv.col & 0x07)) && |
Fyodor Kyslov | 54ebdad | 2020-02-27 12:47:02 -0800 | [diff] [blame] | 2362 | (ref_frame == LAST_FRAME || !x->nonrd_prune_ref_frame_search)) { |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 2363 | search_filter_ref(cpi, x, &this_rdc, mi_row, mi_col, tmp, bsize, |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 2364 | reuse_inter_pred, &this_mode_pred, &this_early_term, |
| 2365 | use_model_yrd_large); |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 2366 | } else { |
Marco Paniconi | b43603d | 2020-02-28 10:21:43 -0800 | [diff] [blame] | 2367 | mi->interp_filters = |
| 2368 | (filter_ref == SWITCHABLE) |
| 2369 | ? av1_broadcast_interp_filter(default_interp_filter) |
| 2370 | : av1_broadcast_interp_filter(filter_ref); |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 2371 | if (force_mv_inter_layer) |
| 2372 | mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR); |
| 2373 | |
Fyodor Kyslov | 4dfc85b | 2020-02-04 17:47:38 -0800 | [diff] [blame] | 2374 | av1_enc_build_inter_predictor_y(xd, mi_row, mi_col); |
Marco Paniconi | 46f4cb8 | 2020-02-20 11:18:59 -0800 | [diff] [blame] | 2375 | if (use_model_yrd_large) { |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 2376 | model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, &this_rdc, |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 2377 | &this_early_term, use_modeled_non_rd_cost); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 2378 | } else { |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 2379 | model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, |
Marco Paniconi | bcb2b72 | 2020-02-24 10:25:49 -0800 | [diff] [blame] | 2380 | use_modeled_non_rd_cost); |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 2381 | } |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 2382 | } |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 2383 | |
Fyodor Kyslov | 84abaea | 2019-06-25 14:44:14 -0700 | [diff] [blame] | 2384 | if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0) { |
| 2385 | sse_zeromv_norm = |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 2386 | (unsigned int)(this_rdc.sse >> (b_width_log2_lookup[bsize] + |
| 2387 | b_height_log2_lookup[bsize])); |
Fyodor Kyslov | 84abaea | 2019-06-25 14:44:14 -0700 | [diff] [blame] | 2388 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2389 | |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 2390 | const int skip_ctx = av1_get_skip_txfm_context(xd); |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 2391 | const int skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][1]; |
| 2392 | const int no_skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][0]; |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 2393 | const int64_t sse_y = this_rdc.sse; |
Fyodor Kyslov | a130ded | 2020-05-09 14:48:55 -0700 | [diff] [blame] | 2394 | if (this_early_term) { |
| 2395 | this_rdc.skip_txfm = 1; |
| 2396 | this_rdc.rate = skip_txfm_cost; |
| 2397 | this_rdc.dist = this_rdc.sse << 4; |
| 2398 | } else { |
Marco Paniconi | bcb2b72 | 2020-02-24 10:25:49 -0800 | [diff] [blame] | 2399 | if (use_modeled_non_rd_cost) { |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 2400 | if (this_rdc.skip_txfm) { |
| 2401 | this_rdc.rate = skip_txfm_cost; |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 2402 | } else { |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 2403 | this_rdc.rate += no_skip_txfm_cost; |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 2404 | } |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 2405 | } else { |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 2406 | block_yrd(cpi, x, mi_row, mi_col, &this_rdc, &is_skippable, bsize, |
| 2407 | mi->tx_size); |
| 2408 | if (this_rdc.skip_txfm || |
| 2409 | RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist) >= |
| 2410 | RDCOST(x->rdmult, 0, this_rdc.sse)) { |
Fyodor Kyslov | a130ded | 2020-05-09 14:48:55 -0700 | [diff] [blame] | 2411 | if (!this_rdc.skip_txfm) { |
| 2412 | // Need to store "real" rdc for possible furure use if UV rdc |
| 2413 | // disallows tx skip |
| 2414 | nonskip_rdc = this_rdc; |
| 2415 | nonskip_rdc.rate += no_skip_txfm_cost; |
| 2416 | } |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 2417 | this_rdc.rate = skip_txfm_cost; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 2418 | this_rdc.skip_txfm = 1; |
| 2419 | this_rdc.dist = this_rdc.sse; |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 2420 | } else { |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 2421 | this_rdc.rate += no_skip_txfm_cost; |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 2422 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2423 | } |
Fyodor Kyslov | a130ded | 2020-05-09 14:48:55 -0700 | [diff] [blame] | 2424 | if ((x->color_sensitivity[0] || x->color_sensitivity[1])) { |
| 2425 | RD_STATS rdc_uv; |
| 2426 | const BLOCK_SIZE uv_bsize = get_plane_block_size( |
| 2427 | bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y); |
| 2428 | if (x->color_sensitivity[0]) { |
| 2429 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 2430 | AOM_PLANE_U, AOM_PLANE_U); |
| 2431 | } |
| 2432 | if (x->color_sensitivity[1]) { |
| 2433 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 2434 | AOM_PLANE_V, AOM_PLANE_V); |
| 2435 | } |
| 2436 | model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, &this_rdc.sse, 1, 2); |
| 2437 | // Restore Y rdc if UV rdc disallows txfm skip |
| 2438 | if (this_rdc.skip_txfm && !rdc_uv.skip_txfm && |
| 2439 | nonskip_rdc.rate != INT_MAX) |
| 2440 | this_rdc = nonskip_rdc; |
| 2441 | this_rdc.rate += rdc_uv.rate; |
| 2442 | this_rdc.dist += rdc_uv.dist; |
| 2443 | this_rdc.skip_txfm = this_rdc.skip_txfm && rdc_uv.skip_txfm; |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 2444 | } |
Fyodor Kyslov | abef54f | 2019-08-14 14:43:12 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2447 | // TODO(kyslov) account for UV prediction cost |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2448 | this_rdc.rate += rate_mv; |
| 2449 | const int16_t mode_ctx = |
| 2450 | av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame); |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 2451 | this_rdc.rate += cost_mv_ref(mode_costs, this_mode, mode_ctx); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2452 | |
| 2453 | this_rdc.rate += ref_costs_single[ref_frame]; |
| 2454 | |
| 2455 | this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist); |
Vishesh | 39e7409 | 2020-06-16 17:13:48 +0530 | [diff] [blame] | 2456 | if (cpi->oxcf.rc_cfg.mode == AOM_CBR) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2457 | newmv_diff_bias(xd, this_mode, &this_rdc, bsize, |
| 2458 | frame_mv[this_mode][ref_frame].as_mv.row, |
Marco Paniconi | 6a966f1 | 2020-02-13 12:14:28 -0800 | [diff] [blame] | 2459 | frame_mv[this_mode][ref_frame].as_mv.col, cpi->speed, |
Marco Paniconi | 221b899 | 2020-10-01 11:14:41 -0700 | [diff] [blame] | 2460 | x->source_variance, x->content_state_sb); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2461 | } |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 2462 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 2463 | if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode && |
| 2464 | cpi->denoiser.denoising_level > kDenLowLow) { |
| 2465 | av1_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx); |
| 2466 | // Keep track of zero_last cost. |
| 2467 | if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0) |
| 2468 | zero_last_cost_orig = this_rdc.rdcost; |
| 2469 | } |
| 2470 | #else |
| 2471 | (void)sse_y; |
| 2472 | #endif |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2473 | |
| 2474 | mode_checked[this_mode][ref_frame] = 1; |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2475 | #if COLLECT_PICK_MODE_STAT |
| 2476 | aom_usec_timer_mark(&ms_stat.timer1); |
| 2477 | ms_stat.nonskipped_search_times[bsize][this_mode] += |
| 2478 | aom_usec_timer_elapsed(&ms_stat.timer1); |
| 2479 | #endif |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2480 | if (this_rdc.rdcost < best_rdc.rdcost) { |
| 2481 | best_rdc = this_rdc; |
| 2482 | best_early_term = this_early_term; |
| 2483 | best_pickmode.best_mode = this_mode; |
| 2484 | best_pickmode.best_pred_filter = mi->interp_filters; |
| 2485 | best_pickmode.best_tx_size = mi->tx_size; |
| 2486 | best_pickmode.best_ref_frame = ref_frame; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 2487 | best_pickmode.best_mode_skip_txfm = this_rdc.skip_txfm; |
Fyodor Kyslov | e0be17e | 2020-05-13 14:20:50 -0700 | [diff] [blame] | 2488 | best_pickmode.best_mode_initial_skip_flag = |
| 2489 | (nonskip_rdc.rate == INT_MAX && this_rdc.skip_txfm); |
| 2490 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2491 | if (reuse_inter_pred) { |
| 2492 | free_pred_buffer(best_pickmode.best_pred); |
| 2493 | best_pickmode.best_pred = this_mode_pred; |
| 2494 | } |
| 2495 | } else { |
| 2496 | if (reuse_inter_pred) free_pred_buffer(this_mode_pred); |
| 2497 | } |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 2498 | if (best_early_term && (idx > 0 || cpi->sf.rt_sf.nonrd_agressive_skip)) { |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 2499 | txfm_info->skip_txfm = 1; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2500 | break; |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | mi->mode = best_pickmode.best_mode; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 2505 | mi->interp_filters = best_pickmode.best_pred_filter; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2506 | mi->tx_size = best_pickmode.best_tx_size; |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 2507 | memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size)); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2508 | mi->ref_frame[0] = best_pickmode.best_ref_frame; |
| 2509 | mi->mv[0].as_int = |
| 2510 | frame_mv[best_pickmode.best_mode][best_pickmode.best_ref_frame].as_int; |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 2511 | |
| 2512 | // Perform intra prediction search, if the best SAD is above a certain |
| 2513 | // threshold. |
| 2514 | mi->angle_delta[PLANE_TYPE_Y] = 0; |
| 2515 | mi->angle_delta[PLANE_TYPE_UV] = 0; |
| 2516 | mi->filter_intra_mode_info.use_filter_intra = 0; |
Marco Paniconi | 5b5b19f | 2019-11-19 11:31:48 -0800 | [diff] [blame] | 2517 | |
Fyodor Kyslov | 175b5b2 | 2020-04-28 19:08:51 -0700 | [diff] [blame] | 2518 | estimate_intra_mode(cpi, x, bsize, use_modeled_non_rd_cost, best_early_term, |
| 2519 | ref_costs_single[INTRA_FRAME], reuse_inter_pred, |
| 2520 | &orig_dst, tmp, &this_mode_pred, &best_rdc, |
| 2521 | &best_pickmode); |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 2522 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2523 | pd->dst = orig_dst; |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 2524 | mi->mode = best_pickmode.best_mode; |
| 2525 | mi->ref_frame[0] = best_pickmode.best_ref_frame; |
Fyodor Kyslov | a130ded | 2020-05-09 14:48:55 -0700 | [diff] [blame] | 2526 | txfm_info->skip_txfm = best_rdc.skip_txfm; |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 2527 | |
| 2528 | if (!is_inter_block(mi)) { |
| 2529 | mi->interp_filters = av1_broadcast_interp_filter(SWITCHABLE_FILTERS); |
| 2530 | } |
| 2531 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2532 | if (reuse_inter_pred && best_pickmode.best_pred != NULL) { |
| 2533 | PRED_BUFFER *const best_pred = best_pickmode.best_pred; |
| 2534 | if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) { |
| 2535 | aom_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf, |
Elliott Karpilovsky | ebe812f | 2020-04-13 18:48:50 -0700 | [diff] [blame] | 2536 | pd->dst.stride, bw, bh); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2537 | } |
| 2538 | } |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 2539 | |
| 2540 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 2541 | if (cpi->oxcf.noise_sensitivity > 0 && resize_pending == 0 && |
| 2542 | denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow && |
| 2543 | cpi->denoiser.reset == 0) { |
| 2544 | AV1_DENOISER_DECISION decision = COPY_BLOCK; |
| 2545 | ctx->sb_skip_denoising = 0; |
| 2546 | av1_pickmode_ctx_den_update(&ctx_den, zero_last_cost_orig, ref_costs_single, |
| 2547 | frame_mv, reuse_inter_pred, &best_pickmode); |
| 2548 | av1_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision, |
| 2549 | gf_temporal_ref); |
| 2550 | if (denoise_recheck_zeromv) |
| 2551 | recheck_zeromv_after_denoising(cpi, mi, x, xd, decision, &ctx_den, |
| 2552 | yv12_mb, &best_rdc, &best_pickmode, bsize, |
| 2553 | mi_row, mi_col); |
| 2554 | best_pickmode.best_ref_frame = ctx_den.best_ref_frame; |
| 2555 | } |
| 2556 | #endif |
| 2557 | |
chiyotsai | 868dc90 | 2019-12-12 15:32:52 -0800 | [diff] [blame] | 2558 | if (cpi->sf.inter_sf.adaptive_rd_thresh) { |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 2559 | THR_MODES best_mode_idx = |
| 2560 | mode_idx[best_pickmode.best_ref_frame][mode_offset(mi->mode)]; |
| 2561 | if (best_pickmode.best_ref_frame == INTRA_FRAME) { |
| 2562 | // Only consider the modes that are included in the intra_mode_list. |
| 2563 | int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE); |
Jerome Jiang | cb1f619 | 2019-12-12 18:02:47 -0800 | [diff] [blame] | 2564 | for (int i = 0; i < intra_modes; i++) { |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 2565 | update_thresh_freq_fact(cpi, x, bsize, INTRA_FRAME, best_mode_idx, |
| 2566 | intra_mode_list[i]); |
| 2567 | } |
| 2568 | } else { |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 2569 | PREDICTION_MODE this_mode; |
| 2570 | for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) { |
| 2571 | update_thresh_freq_fact(cpi, x, bsize, best_pickmode.best_ref_frame, |
| 2572 | best_mode_idx, this_mode); |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 2573 | } |
| 2574 | } |
| 2575 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2576 | |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 2577 | #if CONFIG_INTERNAL_STATS |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2578 | store_coding_context(x, ctx, mi->mode); |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 2579 | #else |
| 2580 | store_coding_context(x, ctx); |
| 2581 | #endif // CONFIG_INTERNAL_STATS |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2582 | #if COLLECT_PICK_MODE_STAT |
| 2583 | aom_usec_timer_mark(&ms_stat.timer2); |
| 2584 | ms_stat.avg_block_times[bsize] += aom_usec_timer_elapsed(&ms_stat.timer2); |
| 2585 | // |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 2586 | if ((mi_row + mi_size_high[bsize] >= (cpi->common.mi_params.mi_rows)) && |
| 2587 | (mi_col + mi_size_wide[bsize] >= (cpi->common.mi_params.mi_cols))) { |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 2588 | int i, j; |
| 2589 | PREDICTION_MODE used_modes[3] = { NEARESTMV, NEARMV, NEWMV }; |
| 2590 | BLOCK_SIZE bss[5] = { BLOCK_8X8, BLOCK_16X16, BLOCK_32X32, BLOCK_64X64, |
| 2591 | BLOCK_128X128 }; |
| 2592 | int64_t total_time = 0l; |
| 2593 | int32_t total_blocks = 0; |
| 2594 | |
| 2595 | printf("\n"); |
| 2596 | for (i = 0; i < 5; i++) { |
| 2597 | printf("BS(%d) Num %d, Avg_time %f: ", bss[i], ms_stat.num_blocks[bss[i]], |
| 2598 | ms_stat.num_blocks[bss[i]] > 0 |
| 2599 | ? (float)ms_stat.avg_block_times[bss[i]] / |
| 2600 | ms_stat.num_blocks[bss[i]] |
| 2601 | : 0); |
| 2602 | total_time += ms_stat.avg_block_times[bss[i]]; |
| 2603 | total_blocks += ms_stat.num_blocks[bss[i]]; |
| 2604 | for (j = 0; j < 3; j++) { |
| 2605 | printf("Mode %d, %d/%d tps %f ", used_modes[j], |
| 2606 | ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]], |
| 2607 | ms_stat.num_searches[bss[i]][used_modes[j]], |
| 2608 | ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]] > 0 |
| 2609 | ? (float)ms_stat |
| 2610 | .nonskipped_search_times[bss[i]][used_modes[j]] / |
| 2611 | ms_stat.num_nonskipped_searches[bss[i]][used_modes[j]] |
| 2612 | : 0l); |
| 2613 | } |
| 2614 | printf("\n"); |
| 2615 | } |
| 2616 | printf("Total time = %ld. Total blocks = %d\n", total_time, total_blocks); |
| 2617 | } |
| 2618 | // |
| 2619 | #endif // COLLECT_PICK_MODE_STAT |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 2620 | *rd_cost = best_rdc; |
| 2621 | } |