kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1 | /* |
James Zern | b7c05bd | 2024-06-11 19:15:10 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved. |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 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 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 18 | #include "av1/common/reconinter.h" |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 19 | #include "av1/common/reconintra.h" |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 20 | |
| 21 | #include "av1/encoder/encodemv.h" |
chiyotsai | 901f406 | 2022-07-06 14:57:53 -0700 | [diff] [blame] | 22 | #include "av1/encoder/intra_mode_search.h" |
| 23 | #include "av1/encoder/model_rd.h" |
| 24 | #include "av1/encoder/motion_search_facade.h" |
Yunqing Wang | 3135d77 | 2022-03-17 23:31:04 -0700 | [diff] [blame] | 25 | #include "av1/encoder/nonrd_opt.h" |
Cheng Chen | 407b986 | 2024-05-08 15:25:17 -0700 | [diff] [blame] | 26 | #include "av1/encoder/palette.h" |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 27 | #include "av1/encoder/reconinter_enc.h" |
Yunqing Wang | 61a7598 | 2021-10-13 12:01:11 -0700 | [diff] [blame] | 28 | #include "av1/encoder/var_based_part.h" |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 29 | |
Aasaipriya | ae4ecc2 | 2022-02-16 17:56:06 +0530 | [diff] [blame] | 30 | static INLINE int early_term_inter_search_with_sse(int early_term_idx, |
| 31 | BLOCK_SIZE bsize, |
| 32 | int64_t this_sse, |
Aasaipriya | 24dd26b | 2022-03-04 17:28:18 +0530 | [diff] [blame] | 33 | int64_t best_sse, |
| 34 | PREDICTION_MODE this_mode) { |
Aasaipriya | ae4ecc2 | 2022-02-16 17:56:06 +0530 | [diff] [blame] | 35 | // Aggressiveness to terminate inter mode search early is adjusted based on |
| 36 | // speed and block size. |
Angie Chiang | 06c3fc6 | 2022-03-08 16:02:06 -0800 | [diff] [blame] | 37 | static const double early_term_thresh[4][4] = { { 0.65, 0.65, 0.65, 0.7 }, |
| 38 | { 0.6, 0.65, 0.85, 0.9 }, |
| 39 | { 0.5, 0.5, 0.55, 0.6 }, |
| 40 | { 0.6, 0.75, 0.85, 0.85 } }; |
| 41 | static const double early_term_thresh_newmv_nearestmv[4] = { 0.3, 0.3, 0.3, |
| 42 | 0.3 }; |
Aasaipriya | 24dd26b | 2022-03-04 17:28:18 +0530 | [diff] [blame] | 43 | |
Aasaipriya | ae4ecc2 | 2022-02-16 17:56:06 +0530 | [diff] [blame] | 44 | const int size_group = size_group_lookup[bsize]; |
| 45 | assert(size_group < 4); |
| 46 | assert((early_term_idx > 0) && (early_term_idx < EARLY_TERM_INDICES)); |
Aasaipriya | 24dd26b | 2022-03-04 17:28:18 +0530 | [diff] [blame] | 47 | const double threshold = |
| 48 | ((early_term_idx == EARLY_TERM_IDX_4) && |
| 49 | (this_mode == NEWMV || this_mode == NEARESTMV)) |
| 50 | ? early_term_thresh_newmv_nearestmv[size_group] |
| 51 | : early_term_thresh[early_term_idx - 1][size_group]; |
Aasaipriya | ae4ecc2 | 2022-02-16 17:56:06 +0530 | [diff] [blame] | 52 | |
| 53 | // Terminate inter mode search early based on best sse so far. |
| 54 | if ((early_term_idx > 0) && (threshold * this_sse > best_sse)) { |
| 55 | return 1; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 60 | static INLINE void init_best_pickmode(BEST_PICKMODE *bp) { |
Aasaipriya | ae4ecc2 | 2022-02-16 17:56:06 +0530 | [diff] [blame] | 61 | bp->best_sse = INT64_MAX; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 62 | bp->best_mode = NEARESTMV; |
| 63 | bp->best_ref_frame = LAST_FRAME; |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 64 | bp->best_second_ref_frame = NONE_FRAME; |
kyslov | bab94fd | 2019-05-03 11:25:17 -0700 | [diff] [blame] | 65 | bp->best_tx_size = TX_8X8; |
Jingning Han | f90062a | 2022-02-25 11:37:38 -0800 | [diff] [blame] | 66 | bp->tx_type = DCT_DCT; |
Sachin Kumar Garg | 2225853 | 2019-06-18 16:45:06 +0530 | [diff] [blame] | 67 | bp->best_pred_filter = av1_broadcast_interp_filter(EIGHTTAP_REGULAR); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 68 | bp->best_mode_skip_txfm = 0; |
Fyodor Kyslov | e0be17e | 2020-05-13 14:20:50 -0700 | [diff] [blame] | 69 | bp->best_mode_initial_skip_flag = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 70 | bp->best_pred = NULL; |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 71 | bp->best_motion_mode = SIMPLE_TRANSLATION; |
| 72 | bp->num_proj_ref = 0; |
Anupam Pandey | a080bf6 | 2022-12-15 16:52:25 +0530 | [diff] [blame] | 73 | av1_zero(bp->wm_params); |
Anupam Pandey | a080bf6 | 2022-12-15 16:52:25 +0530 | [diff] [blame] | 74 | av1_zero(bp->pmi); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Anupam Pandey | 7317937 | 2022-12-21 16:07:14 +0530 | [diff] [blame] | 77 | // Copy best inter mode parameters to best_pickmode |
| 78 | static INLINE void update_search_state_nonrd( |
| 79 | InterModeSearchStateNonrd *search_state, MB_MODE_INFO *const mi, |
| 80 | TxfmSearchInfo *txfm_info, RD_STATS *nonskip_rdc, PICK_MODE_CONTEXT *ctx, |
| 81 | PREDICTION_MODE this_best_mode, const int64_t sse_y) { |
| 82 | BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode; |
Anupam Pandey | 7317937 | 2022-12-21 16:07:14 +0530 | [diff] [blame] | 83 | |
| 84 | best_pickmode->best_sse = sse_y; |
| 85 | best_pickmode->best_mode = this_best_mode; |
| 86 | best_pickmode->best_motion_mode = mi->motion_mode; |
| 87 | best_pickmode->wm_params = mi->wm_params; |
| 88 | best_pickmode->num_proj_ref = mi->num_proj_ref; |
| 89 | best_pickmode->best_pred_filter = mi->interp_filters; |
| 90 | best_pickmode->best_tx_size = mi->tx_size; |
| 91 | best_pickmode->best_ref_frame = mi->ref_frame[0]; |
| 92 | best_pickmode->best_second_ref_frame = mi->ref_frame[1]; |
| 93 | best_pickmode->best_mode_skip_txfm = search_state->this_rdc.skip_txfm; |
| 94 | best_pickmode->best_mode_initial_skip_flag = |
| 95 | (nonskip_rdc->rate == INT_MAX && search_state->this_rdc.skip_txfm); |
| 96 | if (!best_pickmode->best_mode_skip_txfm) { |
Marco Paniconi | d77e40b | 2023-05-15 14:02:06 -0700 | [diff] [blame] | 97 | memcpy(ctx->blk_skip, txfm_info->blk_skip, |
Marco Paniconi | de6f57b | 2023-03-19 23:42:40 -0700 | [diff] [blame] | 98 | sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk); |
Anupam Pandey | 7317937 | 2022-12-21 16:07:14 +0530 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Neeraj Gadgil | 6ad85e8 | 2022-08-18 14:38:01 +0530 | [diff] [blame] | 102 | static INLINE int subpel_select(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, |
Neeraj Gadgil | 14b2f3f | 2022-08-29 09:42:17 +0530 | [diff] [blame] | 103 | int_mv *mv, MV ref_mv, FULLPEL_MV start_mv, |
| 104 | bool fullpel_performed_well) { |
Neeraj Gadgil | c99499a | 2022-09-05 10:51:24 +0530 | [diff] [blame] | 105 | const int frame_lowmotion = cpi->rc.avg_frame_low_motion; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 106 | const int reduce_mv_pel_precision_highmotion = |
| 107 | cpi->sf.rt_sf.reduce_mv_pel_precision_highmotion; |
| 108 | |
Neeraj Gadgil | c99499a | 2022-09-05 10:51:24 +0530 | [diff] [blame] | 109 | // Reduce MV precision for higher int MV value & frame-level motion |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 110 | if (reduce_mv_pel_precision_highmotion >= 3) { |
Neeraj Gadgil | 6ad85e8 | 2022-08-18 14:38:01 +0530 | [diff] [blame] | 111 | int mv_thresh = 4; |
| 112 | const int is_low_resoln = |
| 113 | (cpi->common.width * cpi->common.height <= 320 * 240); |
| 114 | mv_thresh = (bsize > BLOCK_32X32) ? 2 : (bsize > BLOCK_16X16) ? 4 : 6; |
Neeraj Gadgil | c99499a | 2022-09-05 10:51:24 +0530 | [diff] [blame] | 115 | if (frame_lowmotion > 0 && frame_lowmotion < 40) mv_thresh = 12; |
Neeraj Gadgil | 6ad85e8 | 2022-08-18 14:38:01 +0530 | [diff] [blame] | 116 | mv_thresh = (is_low_resoln) ? mv_thresh >> 1 : mv_thresh; |
| 117 | if (abs(mv->as_fullmv.row) >= mv_thresh || |
| 118 | abs(mv->as_fullmv.col) >= mv_thresh) |
| 119 | return HALF_PEL; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 120 | } else if (reduce_mv_pel_precision_highmotion >= 1) { |
Neeraj Gadgil | c99499a | 2022-09-05 10:51:24 +0530 | [diff] [blame] | 121 | int mv_thresh; |
Anupam Pandey | a5356dd | 2022-09-20 18:34:19 +0530 | [diff] [blame] | 122 | const int th_vals[2][3] = { { 4, 8, 10 }, { 4, 6, 8 } }; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 123 | const int th_idx = reduce_mv_pel_precision_highmotion - 1; |
Anupam Pandey | a5356dd | 2022-09-20 18:34:19 +0530 | [diff] [blame] | 124 | assert(th_idx >= 0 && th_idx < 2); |
Neeraj Gadgil | c99499a | 2022-09-05 10:51:24 +0530 | [diff] [blame] | 125 | if (frame_lowmotion > 0 && frame_lowmotion < 40) |
| 126 | mv_thresh = 12; |
| 127 | else |
Anupam Pandey | a5356dd | 2022-09-20 18:34:19 +0530 | [diff] [blame] | 128 | mv_thresh = (bsize >= BLOCK_32X32) ? th_vals[th_idx][0] |
| 129 | : (bsize >= BLOCK_16X16) ? th_vals[th_idx][1] |
| 130 | : th_vals[th_idx][2]; |
Neeraj Gadgil | c99499a | 2022-09-05 10:51:24 +0530 | [diff] [blame] | 131 | if (abs(mv->as_fullmv.row) >= (mv_thresh << 1) || |
| 132 | abs(mv->as_fullmv.col) >= (mv_thresh << 1)) |
| 133 | return FULL_PEL; |
| 134 | else if (abs(mv->as_fullmv.row) >= mv_thresh || |
| 135 | abs(mv->as_fullmv.col) >= mv_thresh) |
| 136 | return HALF_PEL; |
Neeraj Gadgil | 14b2f3f | 2022-08-29 09:42:17 +0530 | [diff] [blame] | 137 | } |
| 138 | // Reduce MV precision for relatively static (e.g. background), low-complex |
| 139 | // large areas |
| 140 | if (cpi->sf.rt_sf.reduce_mv_pel_precision_lowcomplex >= 2) { |
Neeraj Gadgil | 6ad85e8 | 2022-08-18 14:38:01 +0530 | [diff] [blame] | 141 | const int qband = x->qindex >> (QINDEX_BITS - 2); |
| 142 | assert(qband < 4); |
Neeraj Gadgil | 710cc54 | 2022-08-22 12:05:25 +0530 | [diff] [blame] | 143 | if (x->content_state_sb.source_sad_nonrd <= kVeryLowSad && |
Neeraj Gadgil | 6ad85e8 | 2022-08-18 14:38:01 +0530 | [diff] [blame] | 144 | bsize > BLOCK_16X16 && qband != 0) { |
| 145 | if (x->source_variance < 500) |
| 146 | return FULL_PEL; |
| 147 | else if (x->source_variance < 5000) |
| 148 | return HALF_PEL; |
| 149 | } |
Neeraj Gadgil | 14b2f3f | 2022-08-29 09:42:17 +0530 | [diff] [blame] | 150 | } else if (cpi->sf.rt_sf.reduce_mv_pel_precision_lowcomplex >= 1) { |
| 151 | if (fullpel_performed_well && ref_mv.row == 0 && ref_mv.col == 0 && |
| 152 | start_mv.row == 0 && start_mv.col == 0) |
| 153 | return HALF_PEL; |
Neeraj Gadgil | 6ad85e8 | 2022-08-18 14:38:01 +0530 | [diff] [blame] | 154 | } |
| 155 | return cpi->sf.mv_sf.subpel_force_stop; |
Marco Paniconi | 59a7a0d | 2021-11-04 00:09:01 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Yunqing Wang | ddc0bf9 | 2022-12-15 11:17:40 -0800 | [diff] [blame] | 158 | static bool use_aggressive_subpel_search_method(MACROBLOCK *x, |
| 159 | bool use_adaptive_subpel_search, |
| 160 | bool fullpel_performed_well) { |
Neeraj Gadgil | 98c3e0d | 2022-10-13 12:22:42 +0530 | [diff] [blame] | 161 | if (!use_adaptive_subpel_search) return false; |
Anupam Pandey | 4a8ade2 | 2022-10-11 17:57:27 +0530 | [diff] [blame] | 162 | const int qband = x->qindex >> (QINDEX_BITS - 2); |
| 163 | assert(qband < 4); |
Neeraj Gadgil | 98c3e0d | 2022-10-13 12:22:42 +0530 | [diff] [blame] | 164 | if ((qband > 0) && (fullpel_performed_well || |
| 165 | (x->content_state_sb.source_sad_nonrd <= kLowSad) || |
| 166 | (x->source_variance < 100))) |
Anupam Pandey | 4a8ade2 | 2022-10-11 17:57:27 +0530 | [diff] [blame] | 167 | return true; |
James Zern | fa7042e | 2022-10-27 18:25:57 -0700 | [diff] [blame] | 168 | return false; |
Anupam Pandey | 4a8ade2 | 2022-10-11 17:57:27 +0530 | [diff] [blame] | 169 | } |
| 170 | |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 171 | /*!\brief Runs Motion Estimation for a specific block and specific ref frame. |
| 172 | * |
| 173 | * \ingroup nonrd_mode_search |
| 174 | * \callgraph |
| 175 | * \callergraph |
| 176 | * Finds the best Motion Vector by running Motion Estimation for a specific |
| 177 | * block and a specific reference frame. Exits early if RDCost of Full Pel part |
| 178 | * exceeds best RD Cost fund so far |
| 179 | * \param[in] cpi Top-level encoder structure |
| 180 | * \param[in] x Pointer to structure holding all the |
| 181 | * data for the current macroblock |
| 182 | * \param[in] bsize Current block size |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 183 | * \param[in] tmp_mv Pointer to best found New MV |
| 184 | * \param[in] rate_mv Pointer to Rate of the best new MV |
| 185 | * \param[in] best_rd_sofar RD Cost of the best mode found so far |
| 186 | * \param[in] use_base_mv Flag, indicating that tmp_mv holds |
| 187 | * specific MV to start the search with |
| 188 | * |
| 189 | * \return Returns 0 if ME was terminated after Full Pel Search because too |
| 190 | * high RD Cost. Otherwise returns 1. Best New MV is placed into \c tmp_mv. |
| 191 | * Rate estimation for this vector is placed to \c rate_mv |
| 192 | */ |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 193 | static int combined_motion_search(AV1_COMP *cpi, MACROBLOCK *x, |
Marco Paniconi | a1f2968 | 2023-09-17 20:09:51 -0700 | [diff] [blame] | 194 | BLOCK_SIZE bsize, int_mv *tmp_mv, |
| 195 | int *rate_mv, int64_t best_rd_sofar, |
| 196 | int use_base_mv) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 197 | MACROBLOCKD *xd = &x->e_mbd; |
| 198 | const AV1_COMMON *cm = &cpi->common; |
chiyotsai | 7addef9 | 2022-08-25 12:12:06 -0700 | [diff] [blame] | 199 | const SPEED_FEATURES *sf = &cpi->sf; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 200 | MB_MODE_INFO *mi = xd->mi[0]; |
chiyotsai | 7addef9 | 2022-08-25 12:12:06 -0700 | [diff] [blame] | 201 | int step_param = (sf->rt_sf.fullpel_search_step_param) |
| 202 | ? sf->rt_sf.fullpel_search_step_param |
Marco Paniconi | d182da4 | 2020-07-28 09:05:43 -0700 | [diff] [blame] | 203 | : cpi->mv_search_params.mv_step_param; |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 204 | FULLPEL_MV start_mv; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 205 | const int ref = mi->ref_frame[0]; |
| 206 | const MV ref_mv = av1_get_ref_mv(x, mi->ref_mv_idx).as_mv; |
| 207 | MV center_mv; |
| 208 | int dis; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 209 | int rv = 0; |
| 210 | int cost_list[5]; |
| 211 | int search_subpel = 1; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 212 | |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 213 | start_mv = get_fullmv_from_mv(&ref_mv); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 214 | |
| 215 | if (!use_base_mv) |
| 216 | center_mv = ref_mv; |
| 217 | else |
| 218 | center_mv = tmp_mv->as_mv; |
chiyotsai | fbebe9d | 2022-05-23 13:31:41 -0700 | [diff] [blame] | 219 | |
chiyotsai | f7acaa7 | 2023-07-24 11:10:40 -0700 | [diff] [blame] | 220 | const SEARCH_METHODS search_method = |
| 221 | av1_get_default_mv_search_method(x, &cpi->sf.mv_sf, bsize); |
chiyotsai | 945edd6 | 2023-01-24 15:50:09 -0800 | [diff] [blame] | 222 | const search_site_config *src_search_sites = |
| 223 | av1_get_search_site_config(cpi, x, search_method); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 224 | FULLPEL_MOTION_SEARCH_PARAMS full_ms_params; |
Deepa K G | dc6842e | 2023-06-07 15:44:56 +0530 | [diff] [blame] | 225 | FULLPEL_MV_STATS best_mv_stats; |
chiyotsai | 7430da6 | 2020-07-27 14:06:26 -0700 | [diff] [blame] | 226 | av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, ¢er_mv, |
chiyotsai | f7acaa7 | 2023-07-24 11:10:40 -0700 | [diff] [blame] | 227 | start_mv, src_search_sites, search_method, |
chiyotsai | 7430da6 | 2020-07-27 14:06:26 -0700 | [diff] [blame] | 228 | /*fine_search_interval=*/0); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 229 | |
chiyotsai | 271d954 | 2022-04-13 14:47:06 -0700 | [diff] [blame] | 230 | const unsigned int full_var_rd = av1_full_pixel_search( |
| 231 | start_mv, &full_ms_params, step_param, cond_cost_list(cpi, cost_list), |
Deepa K G | dc6842e | 2023-06-07 15:44:56 +0530 | [diff] [blame] | 232 | &tmp_mv->as_fullmv, &best_mv_stats, NULL); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 233 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 234 | // calculate the bit cost on motion vector |
chiyotsai | 87bb805 | 2020-02-12 16:56:33 -0800 | [diff] [blame] | 235 | MV mvp_full = get_mv_from_fullmv(&tmp_mv->as_fullmv); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 236 | |
Fyodor Kyslov | 648c650 | 2021-02-02 18:41:10 -0800 | [diff] [blame] | 237 | *rate_mv = av1_mv_bit_cost(&mvp_full, &ref_mv, x->mv_costs->nmv_joint_cost, |
| 238 | x->mv_costs->mv_cost_stack, MV_COST_WEIGHT); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 239 | |
| 240 | // TODO(kyslov) Account for Rate Mode! |
| 241 | rv = !(RDCOST(x->rdmult, (*rate_mv), 0) > best_rd_sofar); |
| 242 | |
| 243 | if (rv && search_subpel) { |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 244 | SUBPEL_MOTION_SEARCH_PARAMS ms_params; |
| 245 | av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv, |
chiyotsai | 4d1b7d9 | 2020-04-07 13:56:22 -0700 | [diff] [blame] | 246 | cost_list); |
chiyotsai | 7addef9 | 2022-08-25 12:12:06 -0700 | [diff] [blame] | 247 | const bool fullpel_performed_well = |
| 248 | (bsize == BLOCK_64X64 && full_var_rd * 40 < 62267 * 7) || |
| 249 | (bsize == BLOCK_32X32 && full_var_rd * 8 < 42380) || |
| 250 | (bsize == BLOCK_16X16 && full_var_rd * 8 < 10127); |
Neeraj Gadgil | 14b2f3f | 2022-08-29 09:42:17 +0530 | [diff] [blame] | 251 | if (sf->rt_sf.reduce_mv_pel_precision_highmotion || |
| 252 | sf->rt_sf.reduce_mv_pel_precision_lowcomplex) |
| 253 | ms_params.forced_stop = subpel_select(cpi, x, bsize, tmp_mv, ref_mv, |
| 254 | start_mv, fullpel_performed_well); |
chiyotsai | 271d954 | 2022-04-13 14:47:06 -0700 | [diff] [blame] | 255 | |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 256 | MV subpel_start_mv = get_mv_from_fullmv(&tmp_mv->as_fullmv); |
Rachel Barker | fa59a65 | 2023-02-16 19:58:56 +0000 | [diff] [blame] | 257 | assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv)); |
Anupam Pandey | 4a8ade2 | 2022-10-11 17:57:27 +0530 | [diff] [blame] | 258 | // adaptively downgrade subpel search method based on block properties |
| 259 | if (use_aggressive_subpel_search_method( |
Neeraj Gadgil | 98c3e0d | 2022-10-13 12:22:42 +0530 | [diff] [blame] | 260 | x, sf->rt_sf.use_adaptive_subpel_search, fullpel_performed_well)) |
Deepa K G | dc6842e | 2023-06-07 15:44:56 +0530 | [diff] [blame] | 261 | av1_find_best_sub_pixel_tree_pruned_more( |
| 262 | xd, cm, &ms_params, subpel_start_mv, &best_mv_stats, &tmp_mv->as_mv, |
| 263 | &dis, &x->pred_sse[ref], NULL); |
Anupam Pandey | 4a8ade2 | 2022-10-11 17:57:27 +0530 | [diff] [blame] | 264 | else |
chiyotsai | 7addef9 | 2022-08-25 12:12:06 -0700 | [diff] [blame] | 265 | cpi->mv_search_params.find_fractional_mv_step( |
Deepa K G | dc6842e | 2023-06-07 15:44:56 +0530 | [diff] [blame] | 266 | xd, cm, &ms_params, subpel_start_mv, &best_mv_stats, &tmp_mv->as_mv, |
| 267 | &dis, &x->pred_sse[ref], NULL); |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 268 | *rate_mv = |
Fyodor Kyslov | 648c650 | 2021-02-02 18:41:10 -0800 | [diff] [blame] | 269 | av1_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->mv_costs->nmv_joint_cost, |
| 270 | x->mv_costs->mv_cost_stack, MV_COST_WEIGHT); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 271 | } |
James Zern | 286e07b | 2022-09-29 15:44:58 -0700 | [diff] [blame] | 272 | // The final MV can not be equal to the reference MV as this will trigger an |
| 273 | // assert later. This can happen if both NEAREST and NEAR modes were skipped. |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 274 | 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] | 275 | return rv; |
| 276 | } |
| 277 | |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 278 | /*!\brief Searches for the best New Motion Vector. |
| 279 | * |
| 280 | * \ingroup nonrd_mode_search |
| 281 | * \callgraph |
| 282 | * \callergraph |
| 283 | * Finds the best Motion Vector by doing Motion Estimation. Uses reduced |
| 284 | * complexity ME for non-LAST frames or calls \c combined_motion_search |
| 285 | * for LAST reference frame |
| 286 | * \param[in] cpi Top-level encoder structure |
| 287 | * \param[in] x Pointer to structure holding all the |
| 288 | * data for the current macroblock |
| 289 | * \param[in] frame_mv Array that holds MVs for all modes |
| 290 | * and ref frames |
Wan-Teh Chang | fbff8a3 | 2022-03-31 18:43:49 -0700 | [diff] [blame] | 291 | * \param[in] ref_frame Reference frame for which to find |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 292 | * the best New MVs |
| 293 | * \param[in] gf_temporal_ref Flag, indicating temporal reference |
| 294 | * for GOLDEN frame |
| 295 | * \param[in] bsize Current block size |
| 296 | * \param[in] mi_row Row index in 4x4 units |
| 297 | * \param[in] mi_col Column index in 4x4 units |
| 298 | * \param[in] rate_mv Pointer to Rate of the best new MV |
| 299 | * \param[in] best_rdc Pointer to the RD Cost for the best |
| 300 | * mode found so far |
| 301 | * |
| 302 | * \return Returns -1 if the search was not done, otherwise returns 0. |
| 303 | * Best New MV is placed into \c frame_mv array, Rate estimation for this |
| 304 | * vector is placed to \c rate_mv |
| 305 | */ |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 306 | static int search_new_mv(AV1_COMP *cpi, MACROBLOCK *x, |
| 307 | int_mv frame_mv[][REF_FRAMES], |
| 308 | MV_REFERENCE_FRAME ref_frame, int gf_temporal_ref, |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 309 | BLOCK_SIZE bsize, int mi_row, int mi_col, int *rate_mv, |
| 310 | RD_STATS *best_rdc) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 311 | MACROBLOCKD *const xd = &x->e_mbd; |
| 312 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 313 | AV1_COMMON *cm = &cpi->common; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 314 | int_mv *this_ref_frm_newmv = &frame_mv[NEWMV][ref_frame]; |
Marco Paniconi | 5a4ea09 | 2023-08-09 14:36:15 -0700 | [diff] [blame] | 315 | unsigned int y_sad_zero; |
Vishesh | 39e7409 | 2020-06-16 17:13:48 +0530 | [diff] [blame] | 316 | if (ref_frame > LAST_FRAME && cpi->oxcf.rc_cfg.mode == AOM_CBR && |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 317 | gf_temporal_ref) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 318 | int tmp_sad; |
| 319 | int dis; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 320 | |
| 321 | if (bsize < BLOCK_16X16) return -1; |
| 322 | |
Marco Paniconi | 83cb7a5 | 2023-08-26 20:49:53 -0700 | [diff] [blame] | 323 | int me_search_size_col = block_size_wide[bsize] >> 1; |
| 324 | int me_search_size_row = block_size_high[bsize] >> 1; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 325 | tmp_sad = av1_int_pro_motion_estimation( |
| 326 | cpi, x, bsize, mi_row, mi_col, |
Marco Paniconi | 83cb7a5 | 2023-08-26 20:49:53 -0700 | [diff] [blame] | 327 | &x->mbmi_ext.ref_mv_stack[ref_frame][0].this_mv.as_mv, &y_sad_zero, |
| 328 | me_search_size_col, me_search_size_row); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 329 | |
| 330 | if (tmp_sad > x->pred_mv_sad[LAST_FRAME]) return -1; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 331 | |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 332 | this_ref_frm_newmv->as_int = mi->mv[0].as_int; |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 333 | int_mv best_mv = mi->mv[0]; |
| 334 | best_mv.as_mv.row >>= 3; |
| 335 | best_mv.as_mv.col >>= 3; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 336 | MV ref_mv = av1_get_ref_mv(x, 0).as_mv; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 337 | this_ref_frm_newmv->as_mv.row >>= 3; |
| 338 | this_ref_frm_newmv->as_mv.col >>= 3; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 339 | |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 340 | SUBPEL_MOTION_SEARCH_PARAMS ms_params; |
chiyotsai | 94f491f | 2022-03-22 15:28:43 -0700 | [diff] [blame] | 341 | av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv, NULL); |
Neeraj Gadgil | 14b2f3f | 2022-08-29 09:42:17 +0530 | [diff] [blame] | 342 | if (cpi->sf.rt_sf.reduce_mv_pel_precision_highmotion || |
| 343 | cpi->sf.rt_sf.reduce_mv_pel_precision_lowcomplex) { |
| 344 | FULLPEL_MV start_mv = { .row = 0, .col = 0 }; |
| 345 | ms_params.forced_stop = |
| 346 | subpel_select(cpi, x, bsize, &best_mv, ref_mv, start_mv, false); |
| 347 | } |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 348 | MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv); |
Rachel Barker | fa59a65 | 2023-02-16 19:58:56 +0000 | [diff] [blame] | 349 | assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, start_mv)); |
Nithya V S | ace29f3 | 2020-04-07 16:15:12 +0530 | [diff] [blame] | 350 | cpi->mv_search_params.find_fractional_mv_step( |
Deepa K G | dc6842e | 2023-06-07 15:44:56 +0530 | [diff] [blame] | 351 | xd, cm, &ms_params, start_mv, NULL, &best_mv.as_mv, &dis, |
Nithya V S | ace29f3 | 2020-04-07 16:15:12 +0530 | [diff] [blame] | 352 | &x->pred_sse[ref_frame], NULL); |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 353 | this_ref_frm_newmv->as_int = best_mv.as_int; |
chiyotsai | 94f491f | 2022-03-22 15:28:43 -0700 | [diff] [blame] | 354 | |
Neeraj Gadgil | 90ab84e | 2022-11-08 17:13:14 +0530 | [diff] [blame] | 355 | // When NEWMV is same as ref_mv from the drl, it is preferred to code the |
| 356 | // MV as NEARESTMV or NEARMV. In this case, NEWMV needs to be skipped to |
| 357 | // avoid an assert failure at a later stage. The scenario can occur if |
| 358 | // NEARESTMV was not evaluated for ALTREF. |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 359 | if (this_ref_frm_newmv->as_mv.col == ref_mv.col && |
| 360 | this_ref_frm_newmv->as_mv.row == ref_mv.row) |
Neeraj Gadgil | 90ab84e | 2022-11-08 17:13:14 +0530 | [diff] [blame] | 361 | return -1; |
| 362 | |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 363 | *rate_mv = av1_mv_bit_cost(&this_ref_frm_newmv->as_mv, &ref_mv, |
chiyotsai | 94f491f | 2022-03-22 15:28:43 -0700 | [diff] [blame] | 364 | x->mv_costs->nmv_joint_cost, |
| 365 | x->mv_costs->mv_cost_stack, MV_COST_WEIGHT); |
Marco Paniconi | a1f2968 | 2023-09-17 20:09:51 -0700 | [diff] [blame] | 366 | } else if (!combined_motion_search(cpi, x, bsize, &frame_mv[NEWMV][ref_frame], |
| 367 | rate_mv, best_rdc->rdcost, 0)) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Jerome Jiang | 906a94f | 2019-05-01 19:02:58 -0700 | [diff] [blame] | 374 | static void estimate_single_ref_frame_costs(const AV1_COMMON *cm, |
| 375 | const MACROBLOCKD *xd, |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 376 | const ModeCosts *mode_costs, |
chiyotsai | 6d5197a | 2022-07-26 14:28:30 -0700 | [diff] [blame] | 377 | int segment_id, BLOCK_SIZE bsize, |
Jerome Jiang | 906a94f | 2019-05-01 19:02:58 -0700 | [diff] [blame] | 378 | unsigned int *ref_costs_single) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 379 | int seg_ref_active = |
| 380 | segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME); |
| 381 | if (seg_ref_active) { |
| 382 | memset(ref_costs_single, 0, REF_FRAMES * sizeof(*ref_costs_single)); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 383 | } else { |
| 384 | int intra_inter_ctx = av1_get_intra_inter_context(xd); |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 385 | ref_costs_single[INTRA_FRAME] = |
| 386 | mode_costs->intra_inter_cost[intra_inter_ctx][0]; |
| 387 | unsigned int base_cost = mode_costs->intra_inter_cost[intra_inter_ctx][1]; |
chiyotsai | 6d5197a | 2022-07-26 14:28:30 -0700 | [diff] [blame] | 388 | if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT && |
| 389 | is_comp_ref_allowed(bsize)) { |
| 390 | const int comp_ref_type_ctx = av1_get_comp_reference_type_context(xd); |
| 391 | base_cost += mode_costs->comp_ref_type_cost[comp_ref_type_ctx][1]; |
| 392 | } |
Marco Paniconi | 62b66dd | 2020-08-27 11:30:37 -0700 | [diff] [blame] | 393 | ref_costs_single[LAST_FRAME] = base_cost; |
| 394 | ref_costs_single[GOLDEN_FRAME] = base_cost; |
| 395 | ref_costs_single[ALTREF_FRAME] = base_cost; |
| 396 | // add cost for last, golden, altref |
| 397 | ref_costs_single[LAST_FRAME] += mode_costs->single_ref_cost[0][0][0]; |
| 398 | ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][0][1]; |
| 399 | ref_costs_single[GOLDEN_FRAME] += mode_costs->single_ref_cost[0][1][0]; |
| 400 | ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][0][1]; |
| 401 | ref_costs_single[ALTREF_FRAME] += mode_costs->single_ref_cost[0][2][0]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 405 | static INLINE void set_force_skip_flag(const AV1_COMP *const cpi, |
| 406 | MACROBLOCK *const x, unsigned int sse, |
| 407 | int *force_skip) { |
| 408 | if (x->txfm_search_params.tx_mode_search_type == TX_MODE_SELECT && |
| 409 | cpi->sf.rt_sf.tx_size_level_based_on_qstep && |
| 410 | cpi->sf.rt_sf.tx_size_level_based_on_qstep >= 2) { |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 411 | const int qstep = x->plane[AOM_PLANE_Y].dequant_QTX[1] >> (x->e_mbd.bd - 5); |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 412 | const unsigned int qstep_sq = qstep * qstep; |
| 413 | // If the sse is low for low source variance blocks, mark those as |
| 414 | // transform skip. |
| 415 | // Note: Though qstep_sq is based on ac qstep, the threshold is kept |
| 416 | // low so that reliable early estimate of tx skip can be obtained |
| 417 | // through its comparison with sse. |
| 418 | if (sse < qstep_sq && x->source_variance < qstep_sq && |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 419 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 && |
| 420 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 421 | *force_skip = 1; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | #define CAP_TX_SIZE_FOR_BSIZE_GT32(tx_mode_search_type, bsize) \ |
| 426 | (((tx_mode_search_type) != ONLY_4X4 && (bsize) > BLOCK_32X32) ? true : false) |
| 427 | #define TX_SIZE_FOR_BSIZE_GT32 (TX_16X16) |
| 428 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 429 | 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] | 430 | MACROBLOCK *const x, unsigned int var, |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 431 | unsigned int sse, int *force_skip) { |
Cherma Rajan A | 3ed030f | 2019-08-16 18:56:00 +0530 | [diff] [blame] | 432 | MACROBLOCKD *const xd = &x->e_mbd; |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 433 | TX_SIZE tx_size; |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 434 | const TxfmSearchParams *txfm_params = &x->txfm_search_params; |
| 435 | if (txfm_params->tx_mode_search_type == TX_MODE_SELECT) { |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 436 | int multiplier = 8; |
| 437 | unsigned int var_thresh = 0; |
| 438 | unsigned int is_high_var = 1; |
| 439 | // Use quantizer based thresholds to determine transform size. |
| 440 | if (cpi->sf.rt_sf.tx_size_level_based_on_qstep) { |
| 441 | const int qband = x->qindex >> (QINDEX_BITS - 2); |
| 442 | const int mult[4] = { 8, 7, 6, 5 }; |
| 443 | assert(qband < 4); |
| 444 | multiplier = mult[qband]; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 445 | const int qstep = x->plane[AOM_PLANE_Y].dequant_QTX[1] >> (xd->bd - 5); |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 446 | const unsigned int qstep_sq = qstep * qstep; |
| 447 | var_thresh = qstep_sq * 2; |
| 448 | if (cpi->sf.rt_sf.tx_size_level_based_on_qstep >= 2) { |
| 449 | // If the sse is low for low source variance blocks, mark those as |
| 450 | // transform skip. |
| 451 | // Note: Though qstep_sq is based on ac qstep, the threshold is kept |
| 452 | // low so that reliable early estimate of tx skip can be obtained |
| 453 | // through its comparison with sse. |
| 454 | if (sse < qstep_sq && x->source_variance < qstep_sq && |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 455 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 && |
| 456 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 457 | *force_skip = 1; |
| 458 | // Further lower transform size based on aq mode only if residual |
| 459 | // variance is high. |
| 460 | is_high_var = (var >= var_thresh); |
| 461 | } |
| 462 | } |
| 463 | // Choose larger transform size for blocks where dc component is dominant or |
| 464 | // the ac component is low. |
| 465 | if (sse > ((var * multiplier) >> 2) || (var < var_thresh)) |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 466 | tx_size = |
| 467 | AOMMIN(max_txsize_lookup[bsize], |
| 468 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]); |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 469 | else |
| 470 | tx_size = TX_8X8; |
| 471 | |
Vishesh | 734eff9 | 2020-06-20 21:46:36 +0530 | [diff] [blame] | 472 | if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 473 | cyclic_refresh_segment_id_boosted(xd->mi[0]->segment_id) && is_high_var) |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 474 | tx_size = TX_8X8; |
| 475 | else if (tx_size > TX_16X16) |
| 476 | tx_size = TX_16X16; |
| 477 | } else { |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 478 | tx_size = |
| 479 | AOMMIN(max_txsize_lookup[bsize], |
| 480 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]); |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 481 | } |
Jerome Jiang | d4e351d | 2020-01-30 15:13:11 -0800 | [diff] [blame] | 482 | |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 483 | if (CAP_TX_SIZE_FOR_BSIZE_GT32(txfm_params->tx_mode_search_type, bsize)) |
| 484 | tx_size = TX_SIZE_FOR_BSIZE_GT32; |
Jerome Jiang | d4e351d | 2020-01-30 15:13:11 -0800 | [diff] [blame] | 485 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 486 | return AOMMIN(tx_size, TX_16X16); |
| 487 | } |
| 488 | |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 489 | static void block_variance(const uint8_t *src, int src_stride, |
| 490 | const uint8_t *ref, int ref_stride, int w, int h, |
| 491 | unsigned int *sse, int *sum, int block_size, |
| 492 | uint32_t *sse8x8, int *sum8x8, uint32_t *var8x8) { |
venkat sanampudi | cc53822 | 2022-03-07 12:57:37 +0530 | [diff] [blame] | 493 | int k = 0; |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 494 | *sse = 0; |
| 495 | *sum = 0; |
| 496 | |
venkat sanampudi | cc53822 | 2022-03-07 12:57:37 +0530 | [diff] [blame] | 497 | // This function is called for block sizes >= BLOCK_32x32. As per the design |
Diksha Singh | 369372e | 2022-10-21 07:58:27 +0530 | [diff] [blame] | 498 | // the aom_get_var_sse_sum_8x8_quad() processes four 8x8 blocks (in a 8x32) |
| 499 | // per call. Hence the width and height of the block need to be at least 8 and |
| 500 | // 32 samples respectively. |
venkat sanampudi | cc53822 | 2022-03-07 12:57:37 +0530 | [diff] [blame] | 501 | assert(w >= 32); |
| 502 | assert(h >= 8); |
Anupam Pandey | cbe4eb4 | 2022-12-21 12:44:47 +0530 | [diff] [blame] | 503 | for (int row = 0; row < h; row += block_size) { |
| 504 | for (int col = 0; col < w; col += 32) { |
| 505 | aom_get_var_sse_sum_8x8_quad(src + src_stride * row + col, src_stride, |
| 506 | ref + ref_stride * row + col, ref_stride, |
| 507 | &sse8x8[k], &sum8x8[k], sse, sum, |
| 508 | &var8x8[k]); |
venkat sanampudi | cc53822 | 2022-03-07 12:57:37 +0530 | [diff] [blame] | 509 | k += 4; |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 514 | static void block_variance_16x16_dual(const uint8_t *src, int src_stride, |
| 515 | const uint8_t *ref, int ref_stride, int w, |
| 516 | int h, unsigned int *sse, int *sum, |
| 517 | int block_size, uint32_t *sse16x16, |
| 518 | uint32_t *var16x16) { |
| 519 | int k = 0; |
| 520 | *sse = 0; |
| 521 | *sum = 0; |
| 522 | // This function is called for block sizes >= BLOCK_32x32. As per the design |
| 523 | // the aom_get_var_sse_sum_16x16_dual() processes four 16x16 blocks (in a |
| 524 | // 16x32) per call. Hence the width and height of the block need to be at |
| 525 | // least 16 and 32 samples respectively. |
| 526 | assert(w >= 32); |
| 527 | assert(h >= 16); |
Anupam Pandey | cbe4eb4 | 2022-12-21 12:44:47 +0530 | [diff] [blame] | 528 | for (int row = 0; row < h; row += block_size) { |
| 529 | for (int col = 0; col < w; col += 32) { |
| 530 | aom_get_var_sse_sum_16x16_dual(src + src_stride * row + col, src_stride, |
| 531 | ref + ref_stride * row + col, ref_stride, |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 532 | &sse16x16[k], sse, sum, &var16x16[k]); |
| 533 | k += 2; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 538 | static void calculate_variance(int bw, int bh, TX_SIZE tx_size, |
| 539 | unsigned int *sse_i, int *sum_i, |
| 540 | unsigned int *var_o, unsigned int *sse_o, |
| 541 | int *sum_o) { |
| 542 | const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size]; |
| 543 | const int nw = 1 << (bw - b_width_log2_lookup[unit_size]); |
| 544 | const int nh = 1 << (bh - b_height_log2_lookup[unit_size]); |
Anupam Pandey | cbe4eb4 | 2022-12-21 12:44:47 +0530 | [diff] [blame] | 545 | int row, col, k = 0; |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 546 | |
Anupam Pandey | cbe4eb4 | 2022-12-21 12:44:47 +0530 | [diff] [blame] | 547 | for (row = 0; row < nh; row += 2) { |
| 548 | for (col = 0; col < nw; col += 2) { |
| 549 | sse_o[k] = sse_i[row * nw + col] + sse_i[row * nw + col + 1] + |
| 550 | sse_i[(row + 1) * nw + col] + sse_i[(row + 1) * nw + col + 1]; |
| 551 | sum_o[k] = sum_i[row * nw + col] + sum_i[row * nw + col + 1] + |
| 552 | sum_i[(row + 1) * nw + col] + sum_i[(row + 1) * nw + col + 1]; |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 553 | var_o[k] = sse_o[k] - (uint32_t)(((int64_t)sum_o[k] * sum_o[k]) >> |
| 554 | (b_width_log2_lookup[unit_size] + |
| 555 | b_height_log2_lookup[unit_size] + 6)); |
| 556 | k++; |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // Adjust the ac_thr according to speed, width, height and normalized sum |
Yunqing Wang | ddc0bf9 | 2022-12-15 11:17:40 -0800 | [diff] [blame] | 562 | static int ac_thr_factor(int speed, int width, int height, int norm_sum) { |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 563 | if (speed >= 8 && norm_sum < 5) { |
| 564 | if (width <= 640 && height <= 480) |
| 565 | return 4; |
| 566 | else |
| 567 | return 2; |
| 568 | } |
| 569 | return 1; |
| 570 | } |
| 571 | |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 572 | // Sets early_term flag based on chroma planes prediction |
| 573 | static INLINE void set_early_term_based_on_uv_plane( |
| 574 | AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, MACROBLOCKD *xd, int mi_row, |
| 575 | int mi_col, int *early_term, int num_blk, const unsigned int *sse_tx, |
| 576 | const unsigned int *var_tx, int sum, unsigned int var, unsigned int sse) { |
| 577 | AV1_COMMON *const cm = &cpi->common; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 578 | struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y]; |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 579 | const uint32_t dc_quant = p->dequant_QTX[0]; |
| 580 | const uint32_t ac_quant = p->dequant_QTX[1]; |
Marco Paniconi | 0152ba9 | 2023-10-03 14:51:56 -0700 | [diff] [blame] | 581 | int64_t dc_thr = dc_quant * dc_quant >> 6; |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 582 | int64_t ac_thr = ac_quant * ac_quant >> 6; |
| 583 | const int bw = b_width_log2_lookup[bsize]; |
| 584 | const int bh = b_height_log2_lookup[bsize]; |
| 585 | int ac_test = 1; |
| 586 | int dc_test = 1; |
| 587 | const int norm_sum = abs(sum) >> (bw + bh); |
| 588 | |
| 589 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 590 | if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) && |
| 591 | cpi->oxcf.speed > 5) |
| 592 | ac_thr = av1_scale_acskip_thresh(ac_thr, cpi->denoiser.denoising_level, |
| 593 | norm_sum, cpi->svc.temporal_layer_id); |
| 594 | else |
| 595 | ac_thr *= ac_thr_factor(cpi->oxcf.speed, cm->width, cm->height, norm_sum); |
| 596 | #else |
| 597 | ac_thr *= ac_thr_factor(cpi->oxcf.speed, cm->width, cm->height, norm_sum); |
| 598 | |
| 599 | #endif |
| 600 | |
Marco Paniconi | 0152ba9 | 2023-10-03 14:51:56 -0700 | [diff] [blame] | 601 | if (cpi->sf.rt_sf.increase_source_sad_thresh) { |
| 602 | dc_thr = dc_thr << 1; |
| 603 | ac_thr = ac_thr << 2; |
| 604 | } |
| 605 | |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 606 | for (int k = 0; k < num_blk; k++) { |
| 607 | // Check if all ac coefficients can be quantized to zero. |
| 608 | if (!(var_tx[k] < ac_thr || var == 0)) { |
| 609 | ac_test = 0; |
| 610 | break; |
| 611 | } |
| 612 | // Check if dc coefficient can be quantized to zero. |
| 613 | if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) { |
| 614 | dc_test = 0; |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // Check if chroma can be skipped based on ac and dc test flags. |
| 620 | if (ac_test && dc_test) { |
| 621 | int skip_uv[2] = { 0 }; |
| 622 | unsigned int var_uv[2]; |
| 623 | unsigned int sse_uv[2]; |
| 624 | // Transform skipping test in UV planes. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 625 | for (int plane = AOM_PLANE_U; plane <= AOM_PLANE_V; plane++) { |
| 626 | int j = plane - 1; |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 627 | skip_uv[j] = 1; |
Anupam Pandey | 8b325e7 | 2022-12-22 01:51:39 +0530 | [diff] [blame] | 628 | if (x->color_sensitivity[COLOR_SENS_IDX(plane)]) { |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 629 | skip_uv[j] = 0; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 630 | struct macroblock_plane *const puv = &x->plane[plane]; |
| 631 | struct macroblockd_plane *const puvd = &xd->plane[plane]; |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 632 | const BLOCK_SIZE uv_bsize = get_plane_block_size( |
| 633 | bsize, puvd->subsampling_x, puvd->subsampling_y); |
| 634 | // Adjust these thresholds for UV. |
Marco Paniconi | 0152ba9 | 2023-10-03 14:51:56 -0700 | [diff] [blame] | 635 | const int shift_ac = cpi->sf.rt_sf.increase_source_sad_thresh ? 5 : 3; |
| 636 | const int shift_dc = cpi->sf.rt_sf.increase_source_sad_thresh ? 4 : 3; |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 637 | const int64_t uv_dc_thr = |
Marco Paniconi | 0152ba9 | 2023-10-03 14:51:56 -0700 | [diff] [blame] | 638 | (puv->dequant_QTX[0] * puv->dequant_QTX[0]) >> shift_dc; |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 639 | const int64_t uv_ac_thr = |
Marco Paniconi | 0152ba9 | 2023-10-03 14:51:56 -0700 | [diff] [blame] | 640 | (puv->dequant_QTX[1] * puv->dequant_QTX[1]) >> shift_ac; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 641 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 642 | plane, plane); |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 643 | var_uv[j] = cpi->ppi->fn_ptr[uv_bsize].vf(puv->src.buf, puv->src.stride, |
| 644 | puvd->dst.buf, |
| 645 | puvd->dst.stride, &sse_uv[j]); |
| 646 | if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) && |
| 647 | (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j])) |
| 648 | skip_uv[j] = 1; |
| 649 | else |
| 650 | break; |
| 651 | } |
| 652 | } |
| 653 | if (skip_uv[0] & skip_uv[1]) { |
| 654 | *early_term = 1; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | static INLINE void calc_rate_dist_block_param(AV1_COMP *cpi, MACROBLOCK *x, |
| 660 | RD_STATS *rd_stats, |
| 661 | int calculate_rd, int *early_term, |
| 662 | BLOCK_SIZE bsize, |
| 663 | unsigned int sse) { |
| 664 | if (calculate_rd) { |
| 665 | if (!*early_term) { |
| 666 | const int bw = block_size_wide[bsize]; |
| 667 | const int bh = block_size_high[bsize]; |
| 668 | |
| 669 | model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, rd_stats->sse, bw * bh, |
| 670 | &rd_stats->rate, &rd_stats->dist); |
| 671 | } |
| 672 | |
| 673 | if (*early_term) { |
| 674 | rd_stats->rate = 0; |
| 675 | rd_stats->dist = sse << 4; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 680 | static void model_skip_for_sb_y_large_64(AV1_COMP *cpi, BLOCK_SIZE bsize, |
| 681 | int mi_row, int mi_col, MACROBLOCK *x, |
| 682 | MACROBLOCKD *xd, RD_STATS *rd_stats, |
| 683 | int *early_term, int calculate_rd, |
| 684 | int64_t best_sse, |
| 685 | unsigned int *var_output, |
| 686 | unsigned int var_prune_threshold) { |
| 687 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 688 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 689 | // we need to divide by 8 before sending to modeling function. |
| 690 | unsigned int sse; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 691 | struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y]; |
| 692 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y]; |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 693 | int test_skip = 1; |
| 694 | unsigned int var; |
| 695 | int sum; |
| 696 | const int bw = b_width_log2_lookup[bsize]; |
| 697 | const int bh = b_height_log2_lookup[bsize]; |
| 698 | unsigned int sse16x16[64] = { 0 }; |
| 699 | unsigned int var16x16[64] = { 0 }; |
| 700 | assert(xd->mi[0]->tx_size == TX_16X16); |
| 701 | assert(bsize > BLOCK_32X32); |
| 702 | |
| 703 | // Calculate variance for whole partition, and also save 16x16 blocks' |
| 704 | // variance to be used in following transform skipping test. |
| 705 | block_variance_16x16_dual(p->src.buf, p->src.stride, pd->dst.buf, |
| 706 | pd->dst.stride, 4 << bw, 4 << bh, &sse, &sum, 16, |
| 707 | sse16x16, var16x16); |
| 708 | |
| 709 | var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4)); |
| 710 | if (var_output) { |
| 711 | *var_output = var; |
| 712 | if (*var_output > var_prune_threshold) { |
| 713 | return; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | rd_stats->sse = sse; |
| 718 | // Skipping test |
| 719 | *early_term = 0; |
| 720 | set_force_skip_flag(cpi, x, sse, early_term); |
| 721 | // The code below for setting skip flag assumes transform size of at least |
| 722 | // 8x8, so force this lower limit on transform. |
| 723 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 724 | if (!calculate_rd && cpi->sf.rt_sf.sse_early_term_inter_search && |
| 725 | early_term_inter_search_with_sse( |
| 726 | cpi->sf.rt_sf.sse_early_term_inter_search, bsize, sse, best_sse, |
| 727 | mi->mode)) |
| 728 | test_skip = 0; |
| 729 | |
| 730 | if (*early_term) test_skip = 0; |
| 731 | |
| 732 | // Evaluate if the partition block is a skippable block in Y plane. |
| 733 | if (test_skip) { |
| 734 | const unsigned int *sse_tx = sse16x16; |
| 735 | const unsigned int *var_tx = var16x16; |
| 736 | const unsigned int num_block = (1 << (bw + bh - 2)) >> 2; |
| 737 | set_early_term_based_on_uv_plane(cpi, x, bsize, xd, mi_row, mi_col, |
| 738 | early_term, num_block, sse_tx, var_tx, sum, |
| 739 | var, sse); |
| 740 | } |
| 741 | calc_rate_dist_block_param(cpi, x, rd_stats, calculate_rd, early_term, bsize, |
| 742 | sse); |
| 743 | } |
| 744 | |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 745 | 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] | 746 | int mi_row, int mi_col, MACROBLOCK *x, |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 747 | MACROBLOCKD *xd, RD_STATS *rd_stats, |
Marco Paniconi | 493cf67 | 2022-02-25 00:09:45 -0800 | [diff] [blame] | 748 | int *early_term, int calculate_rd, |
chiyotsai | a1fa877 | 2022-06-22 16:57:59 -0700 | [diff] [blame] | 749 | int64_t best_sse, |
| 750 | unsigned int *var_output, |
| 751 | unsigned int var_prune_threshold) { |
Ranjit Kumar Tulabandu | cd4a307 | 2022-09-02 15:53:57 +0530 | [diff] [blame] | 752 | if (x->force_zeromv_skip_for_blk) { |
Marco Paniconi | 7ae1635 | 2022-03-08 00:19:24 -0800 | [diff] [blame] | 753 | *early_term = 1; |
| 754 | rd_stats->rate = 0; |
| 755 | rd_stats->dist = 0; |
| 756 | rd_stats->sse = 0; |
| 757 | return; |
| 758 | } |
| 759 | |
Anupam Pandey | 65c7e1b | 2022-11-23 12:52:52 +0530 | [diff] [blame] | 760 | // For block sizes greater than 32x32, the transform size is always 16x16. |
| 761 | // This function avoids calling calculate_variance() for tx_size 16x16 cases |
| 762 | // by directly populating variance at tx_size level from |
| 763 | // block_variance_16x16_dual() function. |
| 764 | const TxfmSearchParams *txfm_params = &x->txfm_search_params; |
| 765 | if (CAP_TX_SIZE_FOR_BSIZE_GT32(txfm_params->tx_mode_search_type, bsize)) { |
| 766 | xd->mi[0]->tx_size = TX_SIZE_FOR_BSIZE_GT32; |
| 767 | model_skip_for_sb_y_large_64(cpi, bsize, mi_row, mi_col, x, xd, rd_stats, |
| 768 | early_term, calculate_rd, best_sse, var_output, |
| 769 | var_prune_threshold); |
| 770 | return; |
| 771 | } |
| 772 | |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 773 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 774 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 775 | // we need to divide by 8 before sending to modeling function. |
| 776 | unsigned int sse; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 777 | struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y]; |
| 778 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y]; |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 779 | int test_skip = 1; |
| 780 | unsigned int var; |
| 781 | int sum; |
| 782 | |
| 783 | const int bw = b_width_log2_lookup[bsize]; |
| 784 | const int bh = b_height_log2_lookup[bsize]; |
| 785 | unsigned int sse8x8[256] = { 0 }; |
| 786 | int sum8x8[256] = { 0 }; |
| 787 | unsigned int var8x8[256] = { 0 }; |
| 788 | TX_SIZE tx_size; |
| 789 | |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 790 | // Calculate variance for whole partition, and also save 8x8 blocks' variance |
| 791 | // to be used in following transform skipping test. |
| 792 | block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, |
| 793 | 4 << bw, 4 << bh, &sse, &sum, 8, sse8x8, sum8x8, var8x8); |
| 794 | var = sse - (unsigned int)(((int64_t)sum * sum) >> (bw + bh + 4)); |
chiyotsai | a1fa877 | 2022-06-22 16:57:59 -0700 | [diff] [blame] | 795 | if (var_output) { |
| 796 | *var_output = var; |
| 797 | if (*var_output > var_prune_threshold) { |
| 798 | return; |
| 799 | } |
| 800 | } |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 801 | |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 802 | rd_stats->sse = sse; |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 803 | // Skipping test |
| 804 | *early_term = 0; |
| 805 | tx_size = calculate_tx_size(cpi, bsize, x, var, sse, early_term); |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 806 | assert(tx_size <= TX_16X16); |
James Zern | 286e07b | 2022-09-29 15:44:58 -0700 | [diff] [blame] | 807 | // The code below for setting skip flag assumes transform size of at least |
| 808 | // 8x8, so force this lower limit on transform. |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 809 | if (tx_size < TX_8X8) tx_size = TX_8X8; |
| 810 | xd->mi[0]->tx_size = tx_size; |
| 811 | |
Aasaipriya | 24dd26b | 2022-03-04 17:28:18 +0530 | [diff] [blame] | 812 | MB_MODE_INFO *const mi = xd->mi[0]; |
Marco Paniconi | 493cf67 | 2022-02-25 00:09:45 -0800 | [diff] [blame] | 813 | if (!calculate_rd && cpi->sf.rt_sf.sse_early_term_inter_search && |
| 814 | early_term_inter_search_with_sse( |
Aasaipriya | 24dd26b | 2022-03-04 17:28:18 +0530 | [diff] [blame] | 815 | cpi->sf.rt_sf.sse_early_term_inter_search, bsize, sse, best_sse, |
| 816 | mi->mode)) |
Marco Paniconi | 493cf67 | 2022-02-25 00:09:45 -0800 | [diff] [blame] | 817 | test_skip = 0; |
| 818 | |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 819 | if (*early_term) test_skip = 0; |
| 820 | |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 821 | // Evaluate if the partition block is a skippable block in Y plane. |
Marco Paniconi | 493cf67 | 2022-02-25 00:09:45 -0800 | [diff] [blame] | 822 | if (test_skip) { |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 823 | unsigned int sse16x16[64] = { 0 }; |
| 824 | int sum16x16[64] = { 0 }; |
| 825 | unsigned int var16x16[64] = { 0 }; |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 826 | const unsigned int *sse_tx = sse8x8; |
| 827 | const unsigned int *var_tx = var8x8; |
| 828 | unsigned int num_blks = 1 << (bw + bh - 2); |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 829 | |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 830 | if (tx_size >= TX_16X16) { |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 831 | calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16, |
| 832 | sum16x16); |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 833 | sse_tx = sse16x16; |
| 834 | var_tx = var16x16; |
| 835 | num_blks = num_blks >> 2; |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 836 | } |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 837 | set_early_term_based_on_uv_plane(cpi, x, bsize, xd, mi_row, mi_col, |
| 838 | early_term, num_blks, sse_tx, var_tx, sum, |
| 839 | var, sse); |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 840 | } |
Anupam Pandey | 93048ff | 2022-11-23 11:16:51 +0530 | [diff] [blame] | 841 | calc_rate_dist_block_param(cpi, x, rd_stats, calculate_rd, early_term, bsize, |
| 842 | sse); |
Fyodor Kyslov | 7cfbaa7 | 2019-05-31 13:37:29 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 845 | 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] | 846 | MACROBLOCK *x, MACROBLOCKD *xd, |
chiyotsai | 1538b07 | 2022-07-26 10:25:14 -0700 | [diff] [blame] | 847 | RD_STATS *rd_stats, unsigned int *var_out, |
Ranjit Kumar Tulabandu | cd4a307 | 2022-09-02 15:53:57 +0530 | [diff] [blame] | 848 | int calculate_rd, int *early_term) { |
| 849 | if (x->force_zeromv_skip_for_blk && early_term != NULL) { |
| 850 | *early_term = 1; |
| 851 | rd_stats->rate = 0; |
| 852 | rd_stats->dist = 0; |
| 853 | rd_stats->sse = 0; |
| 854 | } |
| 855 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 856 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 857 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 858 | // we need to divide by 8 before sending to modeling function. |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 859 | const int ref = xd->mi[0]->ref_frame[0]; |
| 860 | |
kyslov | e7ff3b6 | 2019-04-05 14:15:03 -0700 | [diff] [blame] | 861 | assert(bsize < BLOCK_SIZES_ALL); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 862 | |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 863 | struct macroblock_plane *const p = &x->plane[AOM_PLANE_Y]; |
| 864 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y]; |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 865 | unsigned int sse; |
| 866 | int rate; |
| 867 | int64_t dist; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 868 | |
Mufaddal Chakera | 1679627 | 2021-02-24 14:03:29 +0530 | [diff] [blame] | 869 | unsigned int var = cpi->ppi->fn_ptr[bsize].vf( |
| 870 | p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, &sse); |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 871 | int force_skip = 0; |
| 872 | xd->mi[0]->tx_size = calculate_tx_size(cpi, bsize, x, var, sse, &force_skip); |
chiyotsai | 1538b07 | 2022-07-26 10:25:14 -0700 | [diff] [blame] | 873 | if (var_out) { |
| 874 | *var_out = var; |
| 875 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 876 | |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 877 | if (calculate_rd && (!force_skip || ref == INTRA_FRAME)) { |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 878 | const int bwide = block_size_wide[bsize]; |
| 879 | const int bhigh = block_size_high[bsize]; |
| 880 | model_rd_with_curvfit(cpi, x, bsize, AOM_PLANE_Y, sse, bwide * bhigh, &rate, |
| 881 | &dist); |
| 882 | } else { |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 883 | rate = INT_MAX; // this will be overwritten later with av1_block_yrd |
Fyodor Kyslov | 1dc0102 | 2019-07-02 16:47:44 -0700 | [diff] [blame] | 884 | dist = INT_MAX; |
| 885 | } |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 886 | rd_stats->sse = sse; |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 887 | x->pred_sse[ref] = (unsigned int)AOMMIN(sse, UINT_MAX); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 888 | |
Nithya V S | 5879522 | 2022-03-30 14:34:52 +0530 | [diff] [blame] | 889 | if (force_skip && ref > INTRA_FRAME) { |
| 890 | rate = 0; |
| 891 | dist = (int64_t)sse << 4; |
| 892 | } |
| 893 | |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 894 | assert(rate >= 0); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 895 | |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 896 | rd_stats->skip_txfm = (rate == 0); |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 897 | rate = AOMMIN(rate, INT_MAX); |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 898 | rd_stats->rate = rate; |
| 899 | rd_stats->dist = dist; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Yunqing Wang | ddc0bf9 | 2022-12-15 11:17:40 -0800 | [diff] [blame] | 902 | static INLINE int get_drl_cost(PREDICTION_MODE this_mode, int ref_mv_idx, |
chiyotsai | 901f406 | 2022-07-06 14:57:53 -0700 | [diff] [blame] | 903 | const MB_MODE_INFO_EXT *mbmi_ext, |
| 904 | const int (*const drl_mode_cost0)[2], |
| 905 | int8_t ref_frame_type) { |
| 906 | int cost = 0; |
| 907 | if (this_mode == NEWMV || this_mode == NEW_NEWMV) { |
| 908 | for (int idx = 0; idx < 2; ++idx) { |
| 909 | if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) { |
| 910 | uint8_t drl_ctx = av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx); |
| 911 | cost += drl_mode_cost0[drl_ctx][ref_mv_idx != idx]; |
| 912 | if (ref_mv_idx == idx) return cost; |
| 913 | } |
| 914 | } |
| 915 | return cost; |
| 916 | } |
| 917 | |
| 918 | if (have_nearmv_in_inter_mode(this_mode)) { |
| 919 | for (int idx = 1; idx < 3; ++idx) { |
| 920 | if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) { |
| 921 | uint8_t drl_ctx = av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx); |
| 922 | cost += drl_mode_cost0[drl_ctx][ref_mv_idx != (idx - 1)]; |
| 923 | if (ref_mv_idx == (idx - 1)) return cost; |
| 924 | } |
| 925 | } |
| 926 | return cost; |
| 927 | } |
| 928 | return cost; |
| 929 | } |
| 930 | |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 931 | static int cost_mv_ref(const ModeCosts *const mode_costs, PREDICTION_MODE mode, |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 932 | int16_t mode_context) { |
| 933 | if (is_inter_compound_mode(mode)) { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 934 | return mode_costs |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 935 | ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)]; |
| 936 | } |
| 937 | |
| 938 | int mode_cost = 0; |
| 939 | int16_t mode_ctx = mode_context & NEWMV_CTX_MASK; |
| 940 | |
| 941 | assert(is_inter_mode(mode)); |
| 942 | |
| 943 | if (mode == NEWMV) { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 944 | mode_cost = mode_costs->newmv_mode_cost[mode_ctx][0]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 945 | return mode_cost; |
| 946 | } else { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 947 | mode_cost = mode_costs->newmv_mode_cost[mode_ctx][1]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 948 | mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK; |
| 949 | |
| 950 | if (mode == GLOBALMV) { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 951 | mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][0]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 952 | return mode_cost; |
| 953 | } else { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 954 | mode_cost += mode_costs->zeromv_mode_cost[mode_ctx][1]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 955 | mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK; |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 956 | mode_cost += mode_costs->refmv_mode_cost[mode_ctx][mode != NEARESTMV]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 957 | return mode_cost; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | static void newmv_diff_bias(MACROBLOCKD *xd, PREDICTION_MODE this_mode, |
| 963 | RD_STATS *this_rdc, BLOCK_SIZE bsize, int mv_row, |
Marco Paniconi | 221b899 | 2020-10-01 11:14:41 -0700 | [diff] [blame] | 964 | int mv_col, int speed, uint32_t spatial_variance, |
Marco Paniconi | 988b34a | 2020-11-09 12:41:13 -0800 | [diff] [blame] | 965 | CONTENT_STATE_SB content_state_sb) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 966 | // Bias against MVs associated with NEWMV mode that are very different from |
| 967 | // top/left neighbors. |
| 968 | if (this_mode == NEWMV) { |
| 969 | int al_mv_average_row; |
| 970 | int al_mv_average_col; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 971 | int row_diff, col_diff; |
| 972 | int above_mv_valid = 0; |
| 973 | int left_mv_valid = 0; |
Wan-Teh Chang | c739f21 | 2021-10-18 15:57:11 -0700 | [diff] [blame] | 974 | int above_row = INVALID_MV_ROW_COL, above_col = INVALID_MV_ROW_COL; |
| 975 | int left_row = INVALID_MV_ROW_COL, left_col = INVALID_MV_ROW_COL; |
Neeraj Gadgil | 710cc54 | 2022-08-22 12:05:25 +0530 | [diff] [blame] | 976 | if (bsize >= BLOCK_64X64 && content_state_sb.source_sad_nonrd != kHighSad && |
Marco Paniconi | 221b899 | 2020-10-01 11:14:41 -0700 | [diff] [blame] | 977 | spatial_variance < 300 && |
| 978 | (mv_row > 16 || mv_row < -16 || mv_col > 16 || mv_col < -16)) { |
| 979 | this_rdc->rdcost = this_rdc->rdcost << 2; |
| 980 | return; |
| 981 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 982 | if (xd->above_mbmi) { |
| 983 | above_mv_valid = xd->above_mbmi->mv[0].as_int != INVALID_MV; |
| 984 | above_row = xd->above_mbmi->mv[0].as_mv.row; |
| 985 | above_col = xd->above_mbmi->mv[0].as_mv.col; |
| 986 | } |
| 987 | if (xd->left_mbmi) { |
| 988 | left_mv_valid = xd->left_mbmi->mv[0].as_int != INVALID_MV; |
| 989 | left_row = xd->left_mbmi->mv[0].as_mv.row; |
| 990 | left_col = xd->left_mbmi->mv[0].as_mv.col; |
| 991 | } |
| 992 | if (above_mv_valid && left_mv_valid) { |
| 993 | al_mv_average_row = (above_row + left_row + 1) >> 1; |
| 994 | al_mv_average_col = (above_col + left_col + 1) >> 1; |
| 995 | } else if (above_mv_valid) { |
| 996 | al_mv_average_row = above_row; |
| 997 | al_mv_average_col = above_col; |
| 998 | } else if (left_mv_valid) { |
| 999 | al_mv_average_row = left_row; |
| 1000 | al_mv_average_col = left_col; |
| 1001 | } else { |
| 1002 | al_mv_average_row = al_mv_average_col = 0; |
| 1003 | } |
Jerome Jiang | c480d82 | 2019-05-01 18:30:37 -0700 | [diff] [blame] | 1004 | row_diff = al_mv_average_row - mv_row; |
| 1005 | col_diff = al_mv_average_col - mv_col; |
Fyodor Kyslov | 990da97 | 2019-11-15 11:15:38 -0800 | [diff] [blame] | 1006 | if (row_diff > 80 || row_diff < -80 || col_diff > 80 || col_diff < -80) { |
| 1007 | if (bsize >= BLOCK_32X32) |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1008 | this_rdc->rdcost = this_rdc->rdcost << 1; |
| 1009 | else |
| 1010 | this_rdc->rdcost = 5 * this_rdc->rdcost >> 2; |
| 1011 | } |
Marco Paniconi | 6a966f1 | 2020-02-13 12:14:28 -0800 | [diff] [blame] | 1012 | } else { |
| 1013 | // Bias for speed >= 8 for low spatial variance. |
| 1014 | if (speed >= 8 && spatial_variance < 150 && |
| 1015 | (mv_row > 64 || mv_row < -64 || mv_col > 64 || mv_col < -64)) |
| 1016 | this_rdc->rdcost = 5 * this_rdc->rdcost >> 2; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1017 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 1020 | static INLINE void update_thresh_freq_fact(AV1_COMP *cpi, MACROBLOCK *x, |
| 1021 | BLOCK_SIZE bsize, |
| 1022 | MV_REFERENCE_FRAME ref_frame, |
| 1023 | THR_MODES best_mode_idx, |
| 1024 | PREDICTION_MODE mode) { |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 1025 | const THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)]; |
| 1026 | const BLOCK_SIZE min_size = AOMMAX(bsize - 3, BLOCK_4X4); |
| 1027 | const BLOCK_SIZE max_size = AOMMIN(bsize + 6, BLOCK_128X128); |
| 1028 | for (BLOCK_SIZE bs = min_size; bs <= max_size; bs += 3) { |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 1029 | int *freq_fact = &x->thresh_freq_fact[bs][thr_mode_idx]; |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 1030 | if (thr_mode_idx == best_mode_idx) { |
| 1031 | *freq_fact -= (*freq_fact >> 4); |
| 1032 | } else { |
| 1033 | *freq_fact = |
| 1034 | AOMMIN(*freq_fact + RD_THRESH_INC, |
| 1035 | cpi->sf.inter_sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT); |
| 1036 | } |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 1040 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 1041 | static void av1_pickmode_ctx_den_update( |
| 1042 | AV1_PICKMODE_CTX_DEN *ctx_den, int64_t zero_last_cost_orig, |
| 1043 | unsigned int ref_frame_cost[REF_FRAMES], |
| 1044 | int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], int reuse_inter_pred, |
| 1045 | BEST_PICKMODE *bp) { |
| 1046 | ctx_den->zero_last_cost_orig = zero_last_cost_orig; |
| 1047 | ctx_den->ref_frame_cost = ref_frame_cost; |
| 1048 | ctx_den->frame_mv = frame_mv; |
| 1049 | ctx_den->reuse_inter_pred = reuse_inter_pred; |
| 1050 | ctx_den->best_tx_size = bp->best_tx_size; |
| 1051 | ctx_den->best_mode = bp->best_mode; |
| 1052 | ctx_den->best_ref_frame = bp->best_ref_frame; |
| 1053 | ctx_den->best_pred_filter = bp->best_pred_filter; |
| 1054 | ctx_den->best_mode_skip_txfm = bp->best_mode_skip_txfm; |
| 1055 | } |
| 1056 | |
| 1057 | static void recheck_zeromv_after_denoising( |
| 1058 | AV1_COMP *cpi, MB_MODE_INFO *const mi, MACROBLOCK *x, MACROBLOCKD *const xd, |
| 1059 | AV1_DENOISER_DECISION decision, AV1_PICKMODE_CTX_DEN *ctx_den, |
| 1060 | struct buf_2d yv12_mb[4][MAX_MB_PLANE], RD_STATS *best_rdc, |
| 1061 | BEST_PICKMODE *best_pickmode, BLOCK_SIZE bsize, int mi_row, int mi_col) { |
| 1062 | // If INTRA or GOLDEN reference was selected, re-evaluate ZEROMV on |
| 1063 | // denoised result. Only do this under noise conditions, and if rdcost of |
James Zern | 286e07b | 2022-09-29 15:44:58 -0700 | [diff] [blame] | 1064 | // ZEROMV on original source is not significantly higher than rdcost of best |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 1065 | // mode. |
| 1066 | if (cpi->noise_estimate.enabled && cpi->noise_estimate.level > kLow && |
| 1067 | ctx_den->zero_last_cost_orig < (best_rdc->rdcost << 3) && |
| 1068 | ((ctx_den->best_ref_frame == INTRA_FRAME && decision >= FILTER_BLOCK) || |
| 1069 | (ctx_den->best_ref_frame == GOLDEN_FRAME && |
| 1070 | cpi->svc.number_spatial_layers == 1 && |
| 1071 | decision == FILTER_ZEROMV_BLOCK))) { |
| 1072 | // Check if we should pick ZEROMV on denoised signal. |
| 1073 | AV1_COMMON *const cm = &cpi->common; |
| 1074 | RD_STATS this_rdc; |
| 1075 | const ModeCosts *mode_costs = &x->mode_costs; |
| 1076 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
| 1077 | MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext; |
| 1078 | |
| 1079 | mi->mode = GLOBALMV; |
| 1080 | mi->ref_frame[0] = LAST_FRAME; |
| 1081 | mi->ref_frame[1] = NONE_FRAME; |
| 1082 | set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME); |
| 1083 | mi->mv[0].as_int = 0; |
| 1084 | mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR); |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1085 | xd->plane[AOM_PLANE_Y].pre[0] = yv12_mb[LAST_FRAME][AOM_PLANE_Y]; |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 1086 | av1_enc_build_inter_predictor_y(xd, mi_row, mi_col); |
chiyotsai | 1538b07 | 2022-07-26 10:25:14 -0700 | [diff] [blame] | 1087 | unsigned int var; |
Ranjit Kumar Tulabandu | cd4a307 | 2022-09-02 15:53:57 +0530 | [diff] [blame] | 1088 | model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc, &var, 1, NULL); |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 1089 | |
| 1090 | const int16_t mode_ctx = |
| 1091 | av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame); |
| 1092 | this_rdc.rate += cost_mv_ref(mode_costs, GLOBALMV, mode_ctx); |
| 1093 | |
| 1094 | this_rdc.rate += ctx_den->ref_frame_cost[LAST_FRAME]; |
| 1095 | this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist); |
| 1096 | txfm_info->skip_txfm = this_rdc.skip_txfm; |
| 1097 | // Don't switch to ZEROMV if the rdcost for ZEROMV on denoised source |
| 1098 | // is higher than best_ref mode (on original source). |
| 1099 | if (this_rdc.rdcost > best_rdc->rdcost) { |
| 1100 | this_rdc = *best_rdc; |
| 1101 | mi->mode = best_pickmode->best_mode; |
| 1102 | mi->ref_frame[0] = best_pickmode->best_ref_frame; |
| 1103 | set_ref_ptrs(cm, xd, mi->ref_frame[0], NONE_FRAME); |
| 1104 | mi->interp_filters = best_pickmode->best_pred_filter; |
| 1105 | if (best_pickmode->best_ref_frame == INTRA_FRAME) { |
| 1106 | mi->mv[0].as_int = INVALID_MV; |
| 1107 | } else { |
| 1108 | mi->mv[0].as_int = ctx_den |
| 1109 | ->frame_mv[best_pickmode->best_mode] |
| 1110 | [best_pickmode->best_ref_frame] |
| 1111 | .as_int; |
| 1112 | if (ctx_den->reuse_inter_pred) { |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1113 | xd->plane[AOM_PLANE_Y].pre[0] = yv12_mb[GOLDEN_FRAME][AOM_PLANE_Y]; |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 1114 | av1_enc_build_inter_predictor_y(xd, mi_row, mi_col); |
| 1115 | } |
| 1116 | } |
| 1117 | mi->tx_size = best_pickmode->best_tx_size; |
| 1118 | txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm; |
| 1119 | } else { |
| 1120 | ctx_den->best_ref_frame = LAST_FRAME; |
| 1121 | *best_rdc = this_rdc; |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | #endif // CONFIG_AV1_TEMPORAL_DENOISING |
| 1126 | |
James Zern | 286e07b | 2022-09-29 15:44:58 -0700 | [diff] [blame] | 1127 | /*!\brief Searches for the best interpolation filter |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1128 | * |
Fyodor Kyslov | 2a3768e | 2020-07-20 14:38:05 -0700 | [diff] [blame] | 1129 | * \ingroup nonrd_mode_search |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1130 | * \callgraph |
| 1131 | * \callergraph |
Marco Paniconi | 010b1a8 | 2021-08-24 22:41:50 -0700 | [diff] [blame] | 1132 | * Iterates through subset of possible interpolation filters (EIGHTTAP_REGULAR, |
| 1133 | * EIGTHTAP_SMOOTH, MULTITAP_SHARP, depending on FILTER_SEARCH_SIZE) and selects |
| 1134 | * the one that gives lowest RD cost. RD cost is calculated using curvfit model. |
| 1135 | * Support for dual filters (different filters in the x & y directions) is |
| 1136 | * allowed if sf.interp_sf.disable_dual_filter = 0. |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1137 | * |
| 1138 | * \param[in] cpi Top-level encoder structure |
| 1139 | * \param[in] x Pointer to structure holding all the |
| 1140 | * data for the current macroblock |
| 1141 | * \param[in] this_rdc Pointer to calculated RD Cost |
Cherma Rajan A | c832755 | 2022-11-11 10:36:09 +0530 | [diff] [blame] | 1142 | * \param[in] inter_pred_params_sr Pointer to structure holding parameters of |
| 1143 | inter prediction for single reference |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1144 | * \param[in] mi_row Row index in 4x4 units |
| 1145 | * \param[in] mi_col Column index in 4x4 units |
Cheng Chen | 9463449 | 2022-11-16 17:37:52 -0800 | [diff] [blame] | 1146 | * \param[in] tmp_buffer Pointer to a temporary buffer for |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1147 | * prediction re-use |
| 1148 | * \param[in] bsize Current block size |
| 1149 | * \param[in] reuse_inter_pred Flag, indicating prediction re-use |
| 1150 | * \param[out] this_mode_pred Pointer to store prediction buffer |
| 1151 | * for prediction re-use |
| 1152 | * \param[out] this_early_term Flag, indicating that transform can be |
| 1153 | * skipped |
chiyotsai | 1538b07 | 2022-07-26 10:25:14 -0700 | [diff] [blame] | 1154 | * \param[out] var The residue variance of the current |
| 1155 | * predictor. |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1156 | * \param[in] use_model_yrd_large Flag, indicating special logic to handle |
| 1157 | * large blocks |
Marco Paniconi | 493cf67 | 2022-02-25 00:09:45 -0800 | [diff] [blame] | 1158 | * \param[in] best_sse Best sse so far. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1159 | * \param[in] is_single_pred Flag, indicating single mode. |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1160 | * |
Wan-Teh Chang | 0defff8 | 2022-08-23 14:17:01 -0700 | [diff] [blame] | 1161 | * \remark Nothing is returned. Instead, calculated RD cost is placed to |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1162 | * \c this_rdc and best filter is placed to \c mi->interp_filters. In case |
James Zern | 286e07b | 2022-09-29 15:44:58 -0700 | [diff] [blame] | 1163 | * \c reuse_inter_pred flag is set, this function also outputs |
Fyodor Kyslov | 487b013 | 2020-07-14 17:23:17 -0700 | [diff] [blame] | 1164 | * \c this_mode_pred. Also \c this_early_temp is set if transform can be |
| 1165 | * skipped |
| 1166 | */ |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1167 | static void search_filter_ref(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc, |
Cherma Rajan A | c832755 | 2022-11-11 10:36:09 +0530 | [diff] [blame] | 1168 | InterPredParams *inter_pred_params_sr, int mi_row, |
Cheng Chen | 9463449 | 2022-11-16 17:37:52 -0800 | [diff] [blame] | 1169 | int mi_col, PRED_BUFFER *tmp_buffer, |
| 1170 | BLOCK_SIZE bsize, int reuse_inter_pred, |
Fyodor Kyslov | 62fc03a | 2020-05-05 19:35:07 -0700 | [diff] [blame] | 1171 | PRED_BUFFER **this_mode_pred, |
chiyotsai | 1538b07 | 2022-07-26 10:25:14 -0700 | [diff] [blame] | 1172 | int *this_early_term, unsigned int *var, |
Neeraj Gadgil | 1513bf0 | 2022-10-28 23:43:22 +0530 | [diff] [blame] | 1173 | int use_model_yrd_large, int64_t best_sse, |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1174 | int is_single_pred) { |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1175 | AV1_COMMON *const cm = &cpi->common; |
| 1176 | MACROBLOCKD *const xd = &x->e_mbd; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1177 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y]; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1178 | MB_MODE_INFO *const mi = xd->mi[0]; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1179 | const int bw = block_size_wide[bsize]; |
Marco Paniconi | 010b1a8 | 2021-08-24 22:41:50 -0700 | [diff] [blame] | 1180 | int dim_factor = |
| 1181 | (cpi->sf.interp_sf.disable_dual_filter == 0) ? FILTER_SEARCH_SIZE : 1; |
| 1182 | RD_STATS pf_rd_stats[FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE] = { 0 }; |
| 1183 | TX_SIZE pf_tx_size[FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE] = { 0 }; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1184 | PRED_BUFFER *current_pred = *this_mode_pred; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1185 | int best_skip = 0; |
| 1186 | int best_early_term = 0; |
| 1187 | int64_t best_cost = INT64_MAX; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1188 | int best_filter_index = -1; |
Cherma Rajan A | c832755 | 2022-11-11 10:36:09 +0530 | [diff] [blame] | 1189 | |
| 1190 | SubpelParams subpel_params; |
| 1191 | // Initialize inter prediction params at mode level for single reference |
| 1192 | // mode. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1193 | if (is_single_pred) |
Cherma Rajan A | c832755 | 2022-11-11 10:36:09 +0530 | [diff] [blame] | 1194 | init_inter_mode_params(&mi->mv[0].as_mv, inter_pred_params_sr, |
| 1195 | &subpel_params, xd->block_ref_scale_factors[0], |
| 1196 | pd->pre->width, pd->pre->height); |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1197 | for (int filter_idx = 0; filter_idx < FILTER_SEARCH_SIZE * FILTER_SEARCH_SIZE; |
| 1198 | ++filter_idx) { |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1199 | int64_t cost; |
Marco Paniconi | 010b1a8 | 2021-08-24 22:41:50 -0700 | [diff] [blame] | 1200 | if (cpi->sf.interp_sf.disable_dual_filter && |
Anupam Pandey | 50b881e | 2022-11-14 16:49:27 +0530 | [diff] [blame] | 1201 | filters_ref_set[filter_idx].as_filters.x_filter != |
| 1202 | filters_ref_set[filter_idx].as_filters.y_filter) |
Marco Paniconi | 010b1a8 | 2021-08-24 22:41:50 -0700 | [diff] [blame] | 1203 | continue; |
Anupam Pandey | 50b881e | 2022-11-14 16:49:27 +0530 | [diff] [blame] | 1204 | |
| 1205 | mi->interp_filters.as_int = filters_ref_set[filter_idx].as_int; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1206 | if (is_single_pred) |
Cherma Rajan A | c832755 | 2022-11-11 10:36:09 +0530 | [diff] [blame] | 1207 | av1_enc_build_inter_predictor_y_nonrd(xd, inter_pred_params_sr, |
| 1208 | &subpel_params); |
Neeraj Gadgil | 1513bf0 | 2022-10-28 23:43:22 +0530 | [diff] [blame] | 1209 | else |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1210 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 1211 | AOM_PLANE_Y, AOM_PLANE_Y); |
chiyotsai | 1538b07 | 2022-07-26 10:25:14 -0700 | [diff] [blame] | 1212 | unsigned int curr_var = UINT_MAX; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1213 | if (use_model_yrd_large) |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1214 | model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1215 | &pf_rd_stats[filter_idx], this_early_term, 1, |
| 1216 | best_sse, &curr_var, UINT_MAX); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1217 | else |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1218 | model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[filter_idx], &curr_var, |
| 1219 | 1, NULL); |
| 1220 | pf_rd_stats[filter_idx].rate += av1_get_switchable_rate( |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1221 | x, xd, cm->features.interp_filter, cm->seq_params->enable_dual_filter); |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1222 | cost = RDCOST(x->rdmult, pf_rd_stats[filter_idx].rate, |
| 1223 | pf_rd_stats[filter_idx].dist); |
| 1224 | pf_tx_size[filter_idx] = mi->tx_size; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1225 | if (cost < best_cost) { |
chiyotsai | 1538b07 | 2022-07-26 10:25:14 -0700 | [diff] [blame] | 1226 | *var = curr_var; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1227 | best_filter_index = filter_idx; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1228 | best_cost = cost; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1229 | best_skip = pf_rd_stats[filter_idx].skip_txfm; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1230 | best_early_term = *this_early_term; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1231 | if (reuse_inter_pred) { |
| 1232 | if (*this_mode_pred != current_pred) { |
| 1233 | free_pred_buffer(*this_mode_pred); |
| 1234 | *this_mode_pred = current_pred; |
| 1235 | } |
Cheng Chen | 9463449 | 2022-11-16 17:37:52 -0800 | [diff] [blame] | 1236 | current_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)]; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1237 | pd->dst.buf = current_pred->data; |
| 1238 | pd->dst.stride = bw; |
| 1239 | } |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1240 | } |
| 1241 | } |
Marco Paniconi | 010b1a8 | 2021-08-24 22:41:50 -0700 | [diff] [blame] | 1242 | assert(best_filter_index >= 0 && |
| 1243 | best_filter_index < dim_factor * FILTER_SEARCH_SIZE); |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1244 | if (reuse_inter_pred && *this_mode_pred != current_pred) |
| 1245 | free_pred_buffer(current_pred); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1246 | |
Anupam Pandey | 50b881e | 2022-11-14 16:49:27 +0530 | [diff] [blame] | 1247 | mi->interp_filters.as_int = filters_ref_set[best_filter_index].as_int; |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1248 | mi->tx_size = pf_tx_size[best_filter_index]; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1249 | this_rdc->rate = pf_rd_stats[best_filter_index].rate; |
| 1250 | this_rdc->dist = pf_rd_stats[best_filter_index].dist; |
Fyodor Kyslov | e869241 | 2020-05-04 17:47:13 -0700 | [diff] [blame] | 1251 | this_rdc->sse = pf_rd_stats[best_filter_index].sse; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 1252 | this_rdc->skip_txfm = (best_skip || best_early_term); |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1253 | *this_early_term = best_early_term; |
Marco Paniconi | 966ec04 | 2019-10-08 09:33:18 -0700 | [diff] [blame] | 1254 | if (reuse_inter_pred) { |
| 1255 | pd->dst.buf = (*this_mode_pred)->data; |
| 1256 | pd->dst.stride = (*this_mode_pred)->stride; |
Marco Paniconi | 010b1a8 | 2021-08-24 22:41:50 -0700 | [diff] [blame] | 1257 | } else if (best_filter_index < dim_factor * FILTER_SEARCH_SIZE - 1) { |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1258 | if (is_single_pred) |
Cherma Rajan A | c832755 | 2022-11-11 10:36:09 +0530 | [diff] [blame] | 1259 | av1_enc_build_inter_predictor_y_nonrd(xd, inter_pred_params_sr, |
| 1260 | &subpel_params); |
Neeraj Gadgil | 9fd7a84 | 2022-11-07 11:22:44 +0530 | [diff] [blame] | 1261 | else |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1262 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 1263 | AOM_PLANE_Y, AOM_PLANE_Y); |
Marco Paniconi | ecf1cc6 | 2019-10-07 19:17:37 -0700 | [diff] [blame] | 1264 | } |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1265 | } |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1266 | #if !CONFIG_REALTIME_ONLY |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1267 | |
Jingning Han | dd4faa8 | 2022-03-23 11:53:13 -0700 | [diff] [blame] | 1268 | static AOM_INLINE int is_warped_mode_allowed(const AV1_COMP *cpi, |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1269 | MACROBLOCK *const x, |
| 1270 | const MB_MODE_INFO *mbmi) { |
Jingning Han | dd4faa8 | 2022-03-23 11:53:13 -0700 | [diff] [blame] | 1271 | const FeatureFlags *const features = &cpi->common.features; |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1272 | const MACROBLOCKD *xd = &x->e_mbd; |
| 1273 | |
Jingning Han | dd4faa8 | 2022-03-23 11:53:13 -0700 | [diff] [blame] | 1274 | if (cpi->sf.inter_sf.extra_prune_warped) return 0; |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1275 | if (has_second_ref(mbmi)) return 0; |
| 1276 | MOTION_MODE last_motion_mode_allowed = SIMPLE_TRANSLATION; |
| 1277 | |
| 1278 | if (features->switchable_motion_mode) { |
| 1279 | // Determine which motion modes to search if more than SIMPLE_TRANSLATION |
| 1280 | // is allowed. |
| 1281 | last_motion_mode_allowed = motion_mode_allowed( |
| 1282 | xd->global_motion, xd, mbmi, features->allow_warped_motion); |
| 1283 | } |
| 1284 | |
| 1285 | if (last_motion_mode_allowed == WARPED_CAUSAL) { |
| 1286 | return 1; |
| 1287 | } |
| 1288 | |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | static void calc_num_proj_ref(AV1_COMP *cpi, MACROBLOCK *x, MB_MODE_INFO *mi) { |
| 1293 | AV1_COMMON *const cm = &cpi->common; |
| 1294 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1295 | const FeatureFlags *const features = &cm->features; |
| 1296 | |
| 1297 | mi->num_proj_ref = 1; |
| 1298 | WARP_SAMPLE_INFO *const warp_sample_info = |
| 1299 | &x->warp_sample_info[mi->ref_frame[0]]; |
| 1300 | int *pts0 = warp_sample_info->pts; |
| 1301 | int *pts_inref0 = warp_sample_info->pts_inref; |
| 1302 | MOTION_MODE last_motion_mode_allowed = SIMPLE_TRANSLATION; |
| 1303 | |
| 1304 | if (features->switchable_motion_mode) { |
| 1305 | // Determine which motion modes to search if more than SIMPLE_TRANSLATION |
| 1306 | // is allowed. |
| 1307 | last_motion_mode_allowed = motion_mode_allowed( |
| 1308 | xd->global_motion, xd, mi, features->allow_warped_motion); |
| 1309 | } |
| 1310 | |
| 1311 | if (last_motion_mode_allowed == WARPED_CAUSAL) { |
| 1312 | if (warp_sample_info->num < 0) { |
| 1313 | warp_sample_info->num = av1_findSamples(cm, xd, pts0, pts_inref0); |
| 1314 | } |
| 1315 | mi->num_proj_ref = warp_sample_info->num; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | static void search_motion_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *this_rdc, |
| 1320 | int mi_row, int mi_col, BLOCK_SIZE bsize, |
| 1321 | int *this_early_term, int use_model_yrd_large, |
Marco Paniconi | 493cf67 | 2022-02-25 00:09:45 -0800 | [diff] [blame] | 1322 | int *rate_mv, int64_t best_sse) { |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1323 | AV1_COMMON *const cm = &cpi->common; |
| 1324 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1325 | const FeatureFlags *const features = &cm->features; |
| 1326 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 1327 | RD_STATS pf_rd_stats[MOTION_MODE_SEARCH_SIZE] = { 0 }; |
| 1328 | int best_skip = 0; |
| 1329 | int best_early_term = 0; |
| 1330 | int64_t best_cost = INT64_MAX; |
| 1331 | int best_mode_index = -1; |
| 1332 | const int interp_filter = features->interp_filter; |
| 1333 | |
| 1334 | const MOTION_MODE motion_modes[MOTION_MODE_SEARCH_SIZE] = { |
| 1335 | SIMPLE_TRANSLATION, WARPED_CAUSAL |
| 1336 | }; |
Jingning Han | dd4faa8 | 2022-03-23 11:53:13 -0700 | [diff] [blame] | 1337 | int mode_search_size = is_warped_mode_allowed(cpi, x, mi) ? 2 : 1; |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1338 | |
| 1339 | WARP_SAMPLE_INFO *const warp_sample_info = |
| 1340 | &x->warp_sample_info[mi->ref_frame[0]]; |
| 1341 | int *pts0 = warp_sample_info->pts; |
| 1342 | int *pts_inref0 = warp_sample_info->pts_inref; |
| 1343 | |
| 1344 | const int total_samples = mi->num_proj_ref; |
| 1345 | if (total_samples == 0) { |
| 1346 | // Do not search WARPED_CAUSAL if there are no samples to use to determine |
| 1347 | // warped parameters. |
| 1348 | mode_search_size = 1; |
| 1349 | } |
| 1350 | |
| 1351 | const MB_MODE_INFO base_mbmi = *mi; |
| 1352 | MB_MODE_INFO best_mbmi; |
| 1353 | |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1354 | for (int mode_index = 0; mode_index < mode_search_size; ++mode_index) { |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1355 | int64_t cost = INT64_MAX; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1356 | MOTION_MODE motion_mode = motion_modes[mode_index]; |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1357 | *mi = base_mbmi; |
| 1358 | mi->motion_mode = motion_mode; |
| 1359 | if (motion_mode == SIMPLE_TRANSLATION) { |
| 1360 | mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR); |
| 1361 | |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1362 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 1363 | AOM_PLANE_Y, AOM_PLANE_Y); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1364 | if (use_model_yrd_large) |
| 1365 | model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1366 | &pf_rd_stats[mode_index], this_early_term, 1, |
| 1367 | best_sse, NULL, UINT_MAX); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1368 | else |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1369 | model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[mode_index], NULL, 1, |
| 1370 | NULL); |
| 1371 | pf_rd_stats[mode_index].rate += |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1372 | av1_get_switchable_rate(x, xd, cm->features.interp_filter, |
| 1373 | cm->seq_params->enable_dual_filter); |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1374 | cost = RDCOST(x->rdmult, pf_rd_stats[mode_index].rate, |
| 1375 | pf_rd_stats[mode_index].dist); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1376 | } else if (motion_mode == WARPED_CAUSAL) { |
| 1377 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
| 1378 | const ModeCosts *mode_costs = &x->mode_costs; |
| 1379 | mi->wm_params.wmtype = DEFAULT_WMTYPE; |
| 1380 | mi->interp_filters = |
| 1381 | av1_broadcast_interp_filter(av1_unswitchable_filter(interp_filter)); |
| 1382 | |
| 1383 | memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0)); |
| 1384 | memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0)); |
| 1385 | // Select the samples according to motion vector difference |
| 1386 | if (mi->num_proj_ref > 1) { |
| 1387 | mi->num_proj_ref = av1_selectSamples(&mi->mv[0].as_mv, pts, pts_inref, |
| 1388 | mi->num_proj_ref, bsize); |
| 1389 | } |
| 1390 | |
| 1391 | // Compute the warped motion parameters with a least squares fit |
| 1392 | // using the collected samples |
| 1393 | if (!av1_find_projection(mi->num_proj_ref, pts, pts_inref, bsize, |
| 1394 | mi->mv[0].as_mv.row, mi->mv[0].as_mv.col, |
| 1395 | &mi->wm_params, mi_row, mi_col)) { |
| 1396 | if (mi->mode == NEWMV) { |
| 1397 | const int_mv mv0 = mi->mv[0]; |
| 1398 | const WarpedMotionParams wm_params0 = mi->wm_params; |
| 1399 | const int num_proj_ref0 = mi->num_proj_ref; |
| 1400 | |
| 1401 | const int_mv ref_mv = av1_get_ref_mv(x, 0); |
| 1402 | SUBPEL_MOTION_SEARCH_PARAMS ms_params; |
| 1403 | av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, |
| 1404 | &ref_mv.as_mv, NULL); |
| 1405 | |
| 1406 | // Refine MV in a small range. |
| 1407 | av1_refine_warped_mv(xd, cm, &ms_params, bsize, pts0, pts_inref0, |
Rachel Barker | c6878ab | 2023-01-09 17:03:54 +0000 | [diff] [blame] | 1408 | total_samples, cpi->sf.mv_sf.warp_search_method, |
| 1409 | cpi->sf.mv_sf.warp_search_iters); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1410 | if (mi->mv[0].as_int == ref_mv.as_int) { |
| 1411 | continue; |
| 1412 | } |
| 1413 | |
| 1414 | if (mv0.as_int != mi->mv[0].as_int) { |
| 1415 | // Keep the refined MV and WM parameters. |
| 1416 | int tmp_rate_mv = av1_mv_bit_cost( |
| 1417 | &mi->mv[0].as_mv, &ref_mv.as_mv, x->mv_costs->nmv_joint_cost, |
| 1418 | x->mv_costs->mv_cost_stack, MV_COST_WEIGHT); |
| 1419 | *rate_mv = tmp_rate_mv; |
| 1420 | } else { |
| 1421 | // Restore the old MV and WM parameters. |
| 1422 | mi->mv[0] = mv0; |
| 1423 | mi->wm_params = wm_params0; |
| 1424 | mi->num_proj_ref = num_proj_ref0; |
| 1425 | } |
| 1426 | } |
| 1427 | // Build the warped predictor |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1428 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 1429 | AOM_PLANE_Y, av1_num_planes(cm) - 1); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1430 | if (use_model_yrd_large) |
| 1431 | model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1432 | &pf_rd_stats[mode_index], this_early_term, |
| 1433 | 1, best_sse, NULL, UINT_MAX); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1434 | else |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1435 | model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rd_stats[mode_index], NULL, |
| 1436 | 1, NULL); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1437 | |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1438 | pf_rd_stats[mode_index].rate += |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1439 | mode_costs->motion_mode_cost[bsize][mi->motion_mode]; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1440 | cost = RDCOST(x->rdmult, pf_rd_stats[mode_index].rate, |
| 1441 | pf_rd_stats[mode_index].dist); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1442 | } else { |
| 1443 | cost = INT64_MAX; |
| 1444 | } |
| 1445 | } |
| 1446 | if (cost < best_cost) { |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1447 | best_mode_index = mode_index; |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1448 | best_cost = cost; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1449 | best_skip = pf_rd_stats[mode_index].skip_txfm; |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1450 | best_early_term = *this_early_term; |
| 1451 | best_mbmi = *mi; |
| 1452 | } |
| 1453 | } |
| 1454 | assert(best_mode_index >= 0 && best_mode_index < FILTER_SEARCH_SIZE); |
| 1455 | |
| 1456 | *mi = best_mbmi; |
| 1457 | this_rdc->rate = pf_rd_stats[best_mode_index].rate; |
| 1458 | this_rdc->dist = pf_rd_stats[best_mode_index].dist; |
| 1459 | this_rdc->sse = pf_rd_stats[best_mode_index].sse; |
| 1460 | this_rdc->skip_txfm = (best_skip || best_early_term); |
| 1461 | *this_early_term = best_early_term; |
| 1462 | if (best_mode_index < FILTER_SEARCH_SIZE - 1) { |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1463 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 1464 | AOM_PLANE_Y, AOM_PLANE_Y); |
Fyodor Kyslov | 70d2ea6 | 2021-07-15 17:12:33 -0700 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | #endif // !CONFIG_REALTIME_ONLY |
Fyodor Kyslov | 2647305 | 2019-08-26 17:09:30 -0700 | [diff] [blame] | 1468 | |
chiyotsai | 1d465d8 | 2022-08-10 11:55:29 -0700 | [diff] [blame] | 1469 | #define COLLECT_NON_SQR_STAT 0 |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 1470 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 1471 | #if COLLECT_NONRD_PICK_MODE_STAT |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 1472 | |
Cheng Chen | 82c6134 | 2022-10-18 16:24:06 -0700 | [diff] [blame] | 1473 | static AOM_INLINE void print_stage_time(const char *stage_name, |
chiyotsai | 1d465d8 | 2022-08-10 11:55:29 -0700 | [diff] [blame] | 1474 | int64_t stage_time, |
| 1475 | int64_t total_time) { |
| 1476 | printf(" %s: %ld (%f%%)\n", stage_name, stage_time, |
| 1477 | 100 * stage_time / (float)total_time); |
| 1478 | } |
Cheng Chen | 82c6134 | 2022-10-18 16:24:06 -0700 | [diff] [blame] | 1479 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 1480 | static void print_time(const mode_search_stat_nonrd *const ms_stat, |
| 1481 | BLOCK_SIZE bsize, int mi_rows, int mi_cols, int mi_row, |
| 1482 | int mi_col) { |
Cheng Chen | 82c6134 | 2022-10-18 16:24:06 -0700 | [diff] [blame] | 1483 | if ((mi_row + mi_size_high[bsize] >= mi_rows) && |
| 1484 | (mi_col + mi_size_wide[bsize] >= mi_cols)) { |
| 1485 | int64_t total_time = 0l; |
| 1486 | int32_t total_blocks = 0; |
| 1487 | for (BLOCK_SIZE bs = 0; bs < BLOCK_SIZES; bs++) { |
| 1488 | total_time += ms_stat->total_block_times[bs]; |
| 1489 | total_blocks += ms_stat->num_blocks[bs]; |
| 1490 | } |
| 1491 | |
| 1492 | printf("\n"); |
| 1493 | for (BLOCK_SIZE bs = 0; bs < BLOCK_SIZES; bs++) { |
| 1494 | if (ms_stat->num_blocks[bs] == 0) { |
| 1495 | continue; |
| 1496 | } |
| 1497 | if (!COLLECT_NON_SQR_STAT && block_size_wide[bs] != block_size_high[bs]) { |
| 1498 | continue; |
| 1499 | } |
| 1500 | |
| 1501 | printf("BLOCK_%dX%d Num %d, Time: %ld (%f%%), Avg_time %f:\n", |
| 1502 | block_size_wide[bs], block_size_high[bs], ms_stat->num_blocks[bs], |
| 1503 | ms_stat->total_block_times[bs], |
| 1504 | 100 * ms_stat->total_block_times[bs] / (float)total_time, |
| 1505 | (float)ms_stat->total_block_times[bs] / ms_stat->num_blocks[bs]); |
| 1506 | for (int j = 0; j < MB_MODE_COUNT; j++) { |
| 1507 | if (ms_stat->nonskipped_search_times[bs][j] == 0) { |
| 1508 | continue; |
| 1509 | } |
| 1510 | |
| 1511 | int64_t total_mode_time = ms_stat->nonskipped_search_times[bs][j]; |
| 1512 | printf(" Mode %d, %d/%d tps %f\n", j, |
| 1513 | ms_stat->num_nonskipped_searches[bs][j], |
| 1514 | ms_stat->num_searches[bs][j], |
| 1515 | ms_stat->num_nonskipped_searches[bs][j] > 0 |
| 1516 | ? (float)ms_stat->nonskipped_search_times[bs][j] / |
| 1517 | ms_stat->num_nonskipped_searches[bs][j] |
| 1518 | : 0l); |
| 1519 | if (j >= INTER_MODE_START) { |
| 1520 | total_mode_time = ms_stat->ms_time[bs][j] + ms_stat->ifs_time[bs][j] + |
| 1521 | ms_stat->model_rd_time[bs][j] + |
| 1522 | ms_stat->txfm_time[bs][j]; |
| 1523 | print_stage_time("Motion Search Time", ms_stat->ms_time[bs][j], |
| 1524 | total_time); |
| 1525 | print_stage_time("Filter Search Time", ms_stat->ifs_time[bs][j], |
| 1526 | total_time); |
| 1527 | print_stage_time("Model RD Time", ms_stat->model_rd_time[bs][j], |
| 1528 | total_time); |
| 1529 | print_stage_time("Tranfm Search Time", ms_stat->txfm_time[bs][j], |
| 1530 | total_time); |
| 1531 | } |
| 1532 | print_stage_time("Total Mode Time", total_mode_time, total_time); |
| 1533 | } |
| 1534 | printf("\n"); |
| 1535 | } |
| 1536 | printf("Total time = %ld. Total blocks = %d\n", total_time, total_blocks); |
| 1537 | } |
| 1538 | } |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 1539 | #endif // COLLECT_NONRD_PICK_MODE_STAT |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 1540 | |
Mudassir Galagnath | 431349c | 2022-08-02 12:31:42 +0530 | [diff] [blame] | 1541 | static bool should_prune_intra_modes_using_neighbors( |
| 1542 | const MACROBLOCKD *xd, bool enable_intra_mode_pruning_using_neighbors, |
| 1543 | PREDICTION_MODE this_mode, PREDICTION_MODE above_mode, |
| 1544 | PREDICTION_MODE left_mode) { |
| 1545 | if (!enable_intra_mode_pruning_using_neighbors) return false; |
| 1546 | |
Mudassir Galaganath | 237404c | 2022-12-21 13:02:19 +0530 | [diff] [blame] | 1547 | // Avoid pruning of DC_PRED as it is the most probable mode to win as per the |
| 1548 | // statistics generated for nonrd intra mode evaluations. |
Mudassir Galagnath | 431349c | 2022-08-02 12:31:42 +0530 | [diff] [blame] | 1549 | if (this_mode == DC_PRED) return false; |
| 1550 | |
| 1551 | // Enable the pruning for current mode only if it is not the winner mode of |
| 1552 | // both the neighboring blocks (left/top). |
| 1553 | return xd->up_available && this_mode != above_mode && xd->left_available && |
| 1554 | this_mode != left_mode; |
| 1555 | } |
| 1556 | |
chiyotsai | 73ddf44 | 2020-06-01 15:42:23 -0700 | [diff] [blame] | 1557 | void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost, |
| 1558 | BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) { |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1559 | AV1_COMMON *const cm = &cpi->common; |
| 1560 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1561 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 1562 | RD_STATS this_rdc, best_rdc; |
Mudassir Galaganath | 7e4b033 | 2023-01-27 13:07:50 +0530 | [diff] [blame] | 1563 | struct estimate_block_intra_args args; |
| 1564 | init_estimate_block_intra_args(&args, cpi, x); |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 1565 | const TxfmSearchParams *txfm_params = &x->txfm_search_params; |
Mudassir Galaganath | 7e4b033 | 2023-01-27 13:07:50 +0530 | [diff] [blame] | 1566 | mi->tx_size = |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1567 | AOMMIN(max_txsize_lookup[bsize], |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 1568 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]); |
Mudassir Galaganath | f5c7d6d | 2023-02-03 12:21:22 +0530 | [diff] [blame] | 1569 | assert(IMPLIES(xd->lossless[mi->segment_id], mi->tx_size == TX_4X4)); |
Mudassir Galaganath | 7e4b033 | 2023-01-27 13:07:50 +0530 | [diff] [blame] | 1570 | const BLOCK_SIZE tx_bsize = txsize_to_bsize[mi->tx_size]; |
| 1571 | |
| 1572 | // If the current block size is the same as the transform block size, enable |
| 1573 | // mode pruning based on the best SAD so far. |
| 1574 | if (cpi->sf.rt_sf.prune_intra_mode_using_best_sad_so_far && bsize == tx_bsize) |
| 1575 | args.prune_mode_based_on_sad = true; |
| 1576 | |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1577 | int *bmode_costs; |
Marco Paniconi | f38266b | 2021-03-07 22:43:23 -0800 | [diff] [blame] | 1578 | PREDICTION_MODE best_mode = DC_PRED; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1579 | const MB_MODE_INFO *above_mi = xd->above_mbmi; |
| 1580 | const MB_MODE_INFO *left_mi = xd->left_mbmi; |
| 1581 | const PREDICTION_MODE A = av1_above_block_mode(above_mi); |
| 1582 | const PREDICTION_MODE L = av1_left_block_mode(left_mi); |
Hui Su | 1b6af72 | 2021-04-08 17:56:50 -0700 | [diff] [blame] | 1583 | const int above_ctx = intra_mode_context[A]; |
| 1584 | const int left_ctx = intra_mode_context[L]; |
Mudassir Galagnath | 431349c | 2022-08-02 12:31:42 +0530 | [diff] [blame] | 1585 | const unsigned int source_variance = x->source_variance; |
Hui Su | 1b6af72 | 2021-04-08 17:56:50 -0700 | [diff] [blame] | 1586 | bmode_costs = x->mode_costs.y_mode_costs[above_ctx][left_ctx]; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1587 | |
| 1588 | av1_invalid_rd_stats(&best_rdc); |
| 1589 | av1_invalid_rd_stats(&this_rdc); |
| 1590 | |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 1591 | init_mbmi_nonrd(mi, DC_PRED, INTRA_FRAME, NONE_FRAME, cm); |
Jerome Jiang | cde6f4a | 2020-01-06 11:14:58 -0800 | [diff] [blame] | 1592 | mi->mv[0].as_int = mi->mv[1].as_int = INVALID_MV; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1593 | |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1594 | // Change the limit of this loop to add other intra prediction |
| 1595 | // mode tests. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 1596 | for (int mode_index = 0; mode_index < RTC_INTRA_MODES; ++mode_index) { |
| 1597 | PREDICTION_MODE this_mode = intra_mode_list[mode_index]; |
Mudassir Galagnath | 02ff970 | 2022-02-16 14:03:26 +0530 | [diff] [blame] | 1598 | |
| 1599 | // As per the statistics generated for intra mode evaluation in the nonrd |
| 1600 | // path, it is found that the probability of H_PRED mode being the winner is |
Mudassir Galaganath | 237404c | 2022-12-21 13:02:19 +0530 | [diff] [blame] | 1601 | // very low when the best mode so far is V_PRED (out of DC_PRED and V_PRED). |
| 1602 | // If V_PRED is the winner mode out of DC_PRED and V_PRED, it could imply |
| 1603 | // the presence of a vertically dominant pattern. Hence, H_PRED mode is not |
| 1604 | // evaluated. |
Mudassir Galagnath | 02ff970 | 2022-02-16 14:03:26 +0530 | [diff] [blame] | 1605 | if (cpi->sf.rt_sf.prune_h_pred_using_best_mode_so_far && |
| 1606 | this_mode == H_PRED && best_mode == V_PRED) |
| 1607 | continue; |
| 1608 | |
Mudassir Galagnath | 431349c | 2022-08-02 12:31:42 +0530 | [diff] [blame] | 1609 | if (should_prune_intra_modes_using_neighbors( |
| 1610 | xd, cpi->sf.rt_sf.enable_intra_mode_pruning_using_neighbors, |
| 1611 | this_mode, A, L)) { |
| 1612 | // Prune V_PRED and H_PRED if source variance of the block is less than |
| 1613 | // or equal to 50. The source variance threshold is obtained empirically. |
| 1614 | if ((this_mode == V_PRED || this_mode == H_PRED) && source_variance <= 50) |
| 1615 | continue; |
| 1616 | |
Mudassir Galaganath | 237404c | 2022-12-21 13:02:19 +0530 | [diff] [blame] | 1617 | // As per the statistics, probability of SMOOTH_PRED being the winner is |
| 1618 | // low when best mode so far is DC_PRED (out of DC_PRED, V_PRED and |
Mudassir Galagnath | 431349c | 2022-08-02 12:31:42 +0530 | [diff] [blame] | 1619 | // H_PRED). Hence, SMOOTH_PRED mode is not evaluated. |
| 1620 | if (best_mode == DC_PRED && this_mode == SMOOTH_PRED) continue; |
| 1621 | } |
| 1622 | |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1623 | this_rdc.dist = this_rdc.rate = 0; |
| 1624 | args.mode = this_mode; |
| 1625 | args.skippable = 1; |
| 1626 | args.rdc = &this_rdc; |
Marco Paniconi | f38266b | 2021-03-07 22:43:23 -0800 | [diff] [blame] | 1627 | mi->mode = this_mode; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1628 | av1_foreach_transformed_block_in_plane(xd, bsize, AOM_PLANE_Y, |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 1629 | av1_estimate_block_intra, &args); |
Mudassir Galaganath | 7e4b033 | 2023-01-27 13:07:50 +0530 | [diff] [blame] | 1630 | |
| 1631 | if (this_rdc.rate == INT_MAX) continue; |
| 1632 | |
Deepa K G | c990ca2 | 2021-02-22 17:11:37 +0530 | [diff] [blame] | 1633 | const int skip_ctx = av1_get_skip_txfm_context(xd); |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1634 | if (args.skippable) { |
Deepa K G | c990ca2 | 2021-02-22 17:11:37 +0530 | [diff] [blame] | 1635 | this_rdc.rate = x->mode_costs.skip_txfm_cost[skip_ctx][1]; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1636 | } else { |
Deepa K G | c990ca2 | 2021-02-22 17:11:37 +0530 | [diff] [blame] | 1637 | this_rdc.rate += x->mode_costs.skip_txfm_cost[skip_ctx][0]; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1638 | } |
| 1639 | this_rdc.rate += bmode_costs[this_mode]; |
| 1640 | this_rdc.rdcost = RDCOST(x->rdmult, this_rdc.rate, this_rdc.dist); |
| 1641 | |
| 1642 | if (this_rdc.rdcost < best_rdc.rdcost) { |
| 1643 | best_rdc = this_rdc; |
Marco Paniconi | f38266b | 2021-03-07 22:43:23 -0800 | [diff] [blame] | 1644 | best_mode = this_mode; |
Fyodor Kyslov | ca638fa | 2021-12-08 19:49:13 -0800 | [diff] [blame] | 1645 | if (!this_rdc.skip_txfm) { |
| 1646 | memset(ctx->blk_skip, 0, |
| 1647 | sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk); |
| 1648 | } |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
Marco Paniconi | 86d1aeb | 2024-05-15 05:59:35 +0000 | [diff] [blame] | 1652 | const unsigned int thresh_sad = |
| 1653 | cpi->sf.rt_sf.prune_palette_search_nonrd > 1 ? 100 : 20; |
Marco Paniconi | c612b96 | 2024-05-08 23:46:26 -0700 | [diff] [blame] | 1654 | const unsigned int best_sad_norm = |
| 1655 | args.best_sad >> |
| 1656 | (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]); |
Marco Paniconi | d97381f | 2024-05-02 11:35:58 -0700 | [diff] [blame] | 1657 | |
| 1658 | // Try palette if it's enabled. |
| 1659 | bool try_palette = |
Cheng Chen | 407b986 | 2024-05-08 15:25:17 -0700 | [diff] [blame] | 1660 | cpi->oxcf.tool_cfg.enable_palette && |
Marco Paniconi | d97381f | 2024-05-02 11:35:58 -0700 | [diff] [blame] | 1661 | av1_allow_palette(cpi->common.features.allow_screen_content_tools, |
| 1662 | mi->bsize); |
Marco Paniconi | 86d1aeb | 2024-05-15 05:59:35 +0000 | [diff] [blame] | 1663 | if (cpi->sf.rt_sf.prune_palette_search_nonrd > 0) { |
Cheng Chen | 407b986 | 2024-05-08 15:25:17 -0700 | [diff] [blame] | 1664 | bool prune = |
| 1665 | (!args.prune_mode_based_on_sad || best_sad_norm > thresh_sad) && |
| 1666 | bsize <= BLOCK_16X16 && x->source_variance > 200; |
| 1667 | try_palette &= prune; |
| 1668 | } |
Marco Paniconi | d97381f | 2024-05-02 11:35:58 -0700 | [diff] [blame] | 1669 | if (try_palette) { |
| 1670 | const TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
| 1671 | const unsigned int intra_ref_frame_cost = 0; |
Marco Paniconi | 06f3b3b | 2024-05-09 22:26:27 -0700 | [diff] [blame] | 1672 | x->color_palette_thresh = (best_sad_norm < 500) ? 32 : 64; |
| 1673 | |
Marco Paniconi | d97381f | 2024-05-02 11:35:58 -0700 | [diff] [blame] | 1674 | // Search palette mode for Luma plane in intra frame. |
| 1675 | av1_search_palette_mode_luma(cpi, x, bsize, intra_ref_frame_cost, ctx, |
| 1676 | &this_rdc, best_rdc.rdcost); |
| 1677 | // Update best mode data. |
Marco Paniconi | 3944d9b | 2024-05-08 10:58:55 -0700 | [diff] [blame] | 1678 | if (this_rdc.rdcost < best_rdc.rdcost) { |
Marco Paniconi | d97381f | 2024-05-02 11:35:58 -0700 | [diff] [blame] | 1679 | best_mode = DC_PRED; |
| 1680 | mi->mv[0].as_int = INVALID_MV; |
| 1681 | mi->mv[1].as_int = INVALID_MV; |
| 1682 | best_rdc.rate = this_rdc.rate; |
| 1683 | best_rdc.dist = this_rdc.dist; |
| 1684 | best_rdc.rdcost = this_rdc.rdcost; |
| 1685 | if (!this_rdc.skip_txfm) { |
| 1686 | memcpy(ctx->blk_skip, txfm_info->blk_skip, |
| 1687 | sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk); |
| 1688 | } |
| 1689 | if (xd->tx_type_map[0] != DCT_DCT) |
| 1690 | av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk); |
| 1691 | } else { |
| 1692 | av1_zero(mi->palette_mode_info); |
| 1693 | } |
| 1694 | } |
| 1695 | |
Marco Paniconi | f38266b | 2021-03-07 22:43:23 -0800 | [diff] [blame] | 1696 | mi->mode = best_mode; |
| 1697 | // Keep DC for UV since mode test is based on Y channel only. |
Fyodor Kyslov | dc9bddb | 2022-01-12 16:30:33 -0800 | [diff] [blame] | 1698 | mi->uv_mode = UV_DC_PRED; |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1699 | *rd_cost = best_rdc; |
| 1700 | |
Marco Paniconi | 1a16d1e | 2023-03-22 14:11:13 -0700 | [diff] [blame] | 1701 | // For lossless: always force the skip flags off. |
| 1702 | // Even though the blk_skip is set to 0 above in the rdcost comparison, |
| 1703 | // do it here again in case the above logic changes. |
| 1704 | if (is_lossless_requested(&cpi->oxcf.rc_cfg)) { |
| 1705 | x->txfm_search_info.skip_txfm = 0; |
| 1706 | memset(ctx->blk_skip, 0, |
| 1707 | sizeof(x->txfm_search_info.blk_skip[0]) * ctx->num_4x4_blk); |
| 1708 | } |
| 1709 | |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1710 | #if CONFIG_INTERNAL_STATS |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 1711 | store_coding_context_nonrd(x, ctx, mi->mode); |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1712 | #else |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 1713 | store_coding_context_nonrd(x, ctx); |
Jerome Jiang | f7e6b90 | 2019-11-25 15:28:23 -0800 | [diff] [blame] | 1714 | #endif // CONFIG_INTERNAL_STATS |
| 1715 | } |
| 1716 | |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1717 | static AOM_INLINE int is_same_gf_and_last_scale(AV1_COMMON *cm) { |
| 1718 | struct scale_factors *const sf_last = get_ref_scale_factors(cm, LAST_FRAME); |
| 1719 | struct scale_factors *const sf_golden = |
| 1720 | get_ref_scale_factors(cm, GOLDEN_FRAME); |
| 1721 | return ((sf_last->x_scale_fp == sf_golden->x_scale_fp) && |
| 1722 | (sf_last->y_scale_fp == sf_golden->y_scale_fp)); |
| 1723 | } |
| 1724 | |
| 1725 | static AOM_INLINE void get_ref_frame_use_mask(AV1_COMP *cpi, MACROBLOCK *x, |
| 1726 | MB_MODE_INFO *mi, int mi_row, |
Wan-Teh Chang | 604883a | 2023-10-18 19:24:21 -0700 | [diff] [blame] | 1727 | int mi_col, BLOCK_SIZE bsize, |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 1728 | int gf_temporal_ref, |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1729 | int use_ref_frame[], |
| 1730 | int *force_skip_low_temp_var) { |
| 1731 | AV1_COMMON *const cm = &cpi->common; |
| 1732 | const struct segmentation *const seg = &cm->seg; |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1733 | const int is_small_sb = (cm->seq_params->sb_size == BLOCK_64X64); |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1734 | |
Marco Paniconi | 52ee0ed | 2022-09-28 12:01:47 -0700 | [diff] [blame] | 1735 | // When the ref_frame_config is used to set the reference frame structure |
| 1736 | // then the usage of alt_ref is determined by the ref_frame_flags |
| 1737 | // (and not the speed feature use_nonrd_altref_frame). |
Marco Paniconi | 5321843 | 2022-10-10 13:00:27 -0700 | [diff] [blame] | 1738 | int use_alt_ref_frame = cpi->ppi->rtc_ref.set_ref_frame_config || |
| 1739 | cpi->sf.rt_sf.use_nonrd_altref_frame; |
Marco Paniconi | 52ee0ed | 2022-09-28 12:01:47 -0700 | [diff] [blame] | 1740 | |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1741 | int use_golden_ref_frame = 1; |
Marco Paniconi | 691b0de | 2021-11-17 15:58:18 -0800 | [diff] [blame] | 1742 | int use_last_ref_frame = 1; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1743 | |
Marco Paniconi | 52ee0ed | 2022-09-28 12:01:47 -0700 | [diff] [blame] | 1744 | // When the ref_frame_config is used to set the reference frame structure: |
| 1745 | // check if LAST is used as a reference. And only remove golden and altref |
| 1746 | // references below if last is used as a reference. |
Marco Paniconi | 5321843 | 2022-10-10 13:00:27 -0700 | [diff] [blame] | 1747 | if (cpi->ppi->rtc_ref.set_ref_frame_config) |
Marco Paniconi | 8a28c20 | 2021-12-02 00:12:58 -0800 | [diff] [blame] | 1748 | use_last_ref_frame = |
| 1749 | cpi->ref_frame_flags & AOM_LAST_FLAG ? use_last_ref_frame : 0; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1750 | |
Marco Paniconi | 52ee0ed | 2022-09-28 12:01:47 -0700 | [diff] [blame] | 1751 | // frame_since_golden is not used when user sets the referene structure. |
Marco Paniconi | 5321843 | 2022-10-10 13:00:27 -0700 | [diff] [blame] | 1752 | if (!cpi->ppi->rtc_ref.set_ref_frame_config && use_last_ref_frame && |
Marco Paniconi | 52ee0ed | 2022-09-28 12:01:47 -0700 | [diff] [blame] | 1753 | cpi->rc.frames_since_golden == 0 && gf_temporal_ref) { |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1754 | use_golden_ref_frame = 0; |
| 1755 | } |
Marco Paniconi | 52ee0ed | 2022-09-28 12:01:47 -0700 | [diff] [blame] | 1756 | |
Marco Paniconi | 8a28c20 | 2021-12-02 00:12:58 -0800 | [diff] [blame] | 1757 | if (use_last_ref_frame && cpi->sf.rt_sf.short_circuit_low_temp_var && |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1758 | x->nonrd_prune_ref_frame_search) { |
| 1759 | if (is_small_sb) |
Yunqing Wang | 61a7598 | 2021-10-13 12:01:11 -0700 | [diff] [blame] | 1760 | *force_skip_low_temp_var = av1_get_force_skip_low_temp_var_small_sb( |
chiyotsai | 68eefbe | 2020-05-01 15:07:58 -0700 | [diff] [blame] | 1761 | &x->part_search_info.variance_low[0], mi_row, mi_col, bsize); |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1762 | else |
Yunqing Wang | 61a7598 | 2021-10-13 12:01:11 -0700 | [diff] [blame] | 1763 | *force_skip_low_temp_var = av1_get_force_skip_low_temp_var( |
chiyotsai | 68eefbe | 2020-05-01 15:07:58 -0700 | [diff] [blame] | 1764 | &x->part_search_info.variance_low[0], mi_row, mi_col, bsize); |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1765 | // If force_skip_low_temp_var is set, skip golden reference. |
| 1766 | if (*force_skip_low_temp_var) { |
| 1767 | use_golden_ref_frame = 0; |
| 1768 | use_alt_ref_frame = 0; |
| 1769 | } |
| 1770 | } |
| 1771 | |
Marco Paniconi | 8a28c20 | 2021-12-02 00:12:58 -0800 | [diff] [blame] | 1772 | if (use_last_ref_frame && |
Ranjit Kumar Tulabandu | cd4a307 | 2022-09-02 15:53:57 +0530 | [diff] [blame] | 1773 | (x->nonrd_prune_ref_frame_search > 2 || x->force_zeromv_skip_for_blk || |
Marco Paniconi | 8a28c20 | 2021-12-02 00:12:58 -0800 | [diff] [blame] | 1774 | (x->nonrd_prune_ref_frame_search > 1 && bsize > BLOCK_64X64))) { |
Marco Paniconi | 9d6cef0 | 2021-10-25 00:02:48 -0700 | [diff] [blame] | 1775 | use_golden_ref_frame = 0; |
| 1776 | use_alt_ref_frame = 0; |
| 1777 | } |
| 1778 | |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1779 | if (segfeature_active(seg, mi->segment_id, SEG_LVL_REF_FRAME) && |
| 1780 | get_segdata(seg, mi->segment_id, SEG_LVL_REF_FRAME) == GOLDEN_FRAME) { |
| 1781 | use_golden_ref_frame = 1; |
| 1782 | use_alt_ref_frame = 0; |
| 1783 | } |
| 1784 | |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 1785 | // Skip golden/altref reference if color is set, on flat blocks with motion. |
| 1786 | // For screen: always skip golden/alt (if color_sensitivity_sb_g/alt is set) |
Marco Paniconi | 0956945 | 2022-09-07 15:05:25 -0700 | [diff] [blame] | 1787 | // except when x->nonrd_prune_ref_frame_search = 0. This latter flag |
James Zern | 286e07b | 2022-09-29 15:44:58 -0700 | [diff] [blame] | 1788 | // may be set in the variance partition when golden is a much better |
Marco Paniconi | 0956945 | 2022-09-07 15:05:25 -0700 | [diff] [blame] | 1789 | // reference than last, in which case it may not be worth skipping |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 1790 | // golden/altref completely. |
| 1791 | // Condition on use_last_ref to make sure there remains at least one |
| 1792 | // reference. |
| 1793 | if (use_last_ref_frame && |
| 1794 | ((cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN && |
Marco Paniconi | 0956945 | 2022-09-07 15:05:25 -0700 | [diff] [blame] | 1795 | x->nonrd_prune_ref_frame_search != 0) || |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 1796 | (x->source_variance < 200 && |
| 1797 | x->content_state_sb.source_sad_nonrd >= kLowSad))) { |
| 1798 | if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 || |
| 1799 | x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1) |
| 1800 | use_golden_ref_frame = 0; |
| 1801 | if (x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 || |
| 1802 | x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_V)] == 1) |
| 1803 | use_alt_ref_frame = 0; |
| 1804 | } |
Marco Paniconi | c6a503f | 2022-08-15 23:16:21 -0700 | [diff] [blame] | 1805 | |
Marco Paniconi | e26457d | 2022-10-11 15:42:17 -0700 | [diff] [blame] | 1806 | // For non-screen: if golden and altref are not being selected as references |
| 1807 | // (use_golden_ref_frame/use_alt_ref_frame = 0) check to allow golden back |
| 1808 | // based on the sad of nearest/nearmv of LAST ref. If this block sad is large, |
| 1809 | // keep golden as reference. Only do this for the agrressive pruning mode and |
| 1810 | // avoid it when color is set for golden reference. |
| 1811 | if (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN && |
| 1812 | (cpi->ref_frame_flags & AOM_LAST_FLAG) && !use_golden_ref_frame && |
| 1813 | !use_alt_ref_frame && x->pred_mv_sad[LAST_FRAME] != INT_MAX && |
| 1814 | x->nonrd_prune_ref_frame_search > 2 && |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 1815 | x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 && |
| 1816 | x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) { |
Marco Paniconi | a580471 | 2023-12-12 16:01:27 -0800 | [diff] [blame] | 1817 | int thr = (cm->width * cm->height > RESOLUTION_288P) ? 100 : 150; |
Marco Paniconi | e26457d | 2022-10-11 15:42:17 -0700 | [diff] [blame] | 1818 | int pred = x->pred_mv_sad[LAST_FRAME] >> |
| 1819 | (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]); |
| 1820 | if (pred > thr) use_golden_ref_frame = 1; |
| 1821 | } |
| 1822 | |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1823 | use_alt_ref_frame = |
| 1824 | cpi->ref_frame_flags & AOM_ALT_FLAG ? use_alt_ref_frame : 0; |
| 1825 | use_golden_ref_frame = |
| 1826 | cpi->ref_frame_flags & AOM_GOLD_FLAG ? use_golden_ref_frame : 0; |
| 1827 | |
Marco Paniconi | 6dfcab2 | 2022-10-31 00:41:42 -0700 | [diff] [blame] | 1828 | // For spatial layers: enable golden ref if it is set by user and |
| 1829 | // corresponds to the lower spatial layer. |
| 1830 | if (cpi->svc.spatial_layer_id > 0 && (cpi->ref_frame_flags & AOM_GOLD_FLAG) && |
| 1831 | x->content_state_sb.source_sad_nonrd < kHighSad) { |
| 1832 | const int buffslot_golden = |
| 1833 | cpi->ppi->rtc_ref.ref_idx[GOLDEN_FRAME - LAST_FRAME]; |
Marco Paniconi | 42b9273 | 2023-02-07 00:07:13 -0800 | [diff] [blame] | 1834 | if (cpi->ppi->rtc_ref.buffer_time_index[buffslot_golden] == |
Marco Paniconi | 6dfcab2 | 2022-10-31 00:41:42 -0700 | [diff] [blame] | 1835 | cpi->svc.current_superframe) |
| 1836 | use_golden_ref_frame = 1; |
| 1837 | } |
| 1838 | |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1839 | use_ref_frame[ALTREF_FRAME] = use_alt_ref_frame; |
| 1840 | use_ref_frame[GOLDEN_FRAME] = use_golden_ref_frame; |
Marco Paniconi | 691b0de | 2021-11-17 15:58:18 -0800 | [diff] [blame] | 1841 | use_ref_frame[LAST_FRAME] = use_last_ref_frame; |
Marco Paniconi | fca9d40 | 2023-06-29 15:58:51 -0700 | [diff] [blame] | 1842 | // Keep this assert on, as only 3 references are used in nonrd_pickmode |
| 1843 | // (LAST, GOLDEN, ALTREF), and if all 3 are not set by user then this |
| 1844 | // frame must be an intra-only frame and hence should never enter the |
| 1845 | // pickmode here for inter frames. |
Marco Paniconi | 8a28c20 | 2021-12-02 00:12:58 -0800 | [diff] [blame] | 1846 | assert(use_last_ref_frame || use_golden_ref_frame || use_alt_ref_frame); |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Neeraj Gadgil | 1513bf0 | 2022-10-28 23:43:22 +0530 | [diff] [blame] | 1849 | static AOM_INLINE int is_filter_search_enabled_blk( |
| 1850 | AV1_COMP *cpi, MACROBLOCK *x, int mi_row, int mi_col, BLOCK_SIZE bsize, |
| 1851 | int segment_id, int cb_pred_filter_search, InterpFilter *filt_select) { |
chiyotsai | 7f6e21a | 2020-06-16 14:52:30 -0700 | [diff] [blame] | 1852 | const AV1_COMMON *const cm = &cpi->common; |
Neeraj Gadgil | 1513bf0 | 2022-10-28 23:43:22 +0530 | [diff] [blame] | 1853 | // filt search disabled |
| 1854 | if (!cpi->sf.rt_sf.use_nonrd_filter_search) return 0; |
| 1855 | // filt search purely based on mode properties |
| 1856 | if (!cb_pred_filter_search) return 1; |
| 1857 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1858 | int enable_interp_search = 0; |
| 1859 | if (!(xd->left_mbmi && xd->above_mbmi)) { |
| 1860 | // neighbors info unavailable |
| 1861 | enable_interp_search = 2; |
| 1862 | } else if (!(is_inter_block(xd->left_mbmi) && |
| 1863 | is_inter_block(xd->above_mbmi))) { |
| 1864 | // neighbor is INTRA |
| 1865 | enable_interp_search = 2; |
| 1866 | } else if (xd->left_mbmi->interp_filters.as_int != |
| 1867 | xd->above_mbmi->interp_filters.as_int) { |
| 1868 | // filters are different |
| 1869 | enable_interp_search = 2; |
| 1870 | } else if ((cb_pred_filter_search == 1) && |
| 1871 | (xd->left_mbmi->interp_filters.as_filters.x_filter != |
| 1872 | EIGHTTAP_REGULAR)) { |
| 1873 | // not regular |
| 1874 | enable_interp_search = 2; |
| 1875 | } else { |
| 1876 | // enable prediction based on chessboard pattern |
| 1877 | if (xd->left_mbmi->interp_filters.as_filters.x_filter == EIGHTTAP_SMOOTH) |
| 1878 | *filt_select = EIGHTTAP_SMOOTH; |
| 1879 | const int bsl = mi_size_wide_log2[bsize]; |
| 1880 | enable_interp_search = |
| 1881 | (bool)((((mi_row + mi_col) >> bsl) + |
| 1882 | get_chessboard_index(cm->current_frame.frame_number)) & |
| 1883 | 0x1); |
| 1884 | if (cyclic_refresh_segment_id_boosted(segment_id)) enable_interp_search = 1; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1885 | } |
Neeraj Gadgil | 1513bf0 | 2022-10-28 23:43:22 +0530 | [diff] [blame] | 1886 | return enable_interp_search; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | static AOM_INLINE int skip_mode_by_threshold( |
| 1890 | PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, int_mv mv, |
| 1891 | int frames_since_golden, const int *const rd_threshes, |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 1892 | const int *const rd_thresh_freq_fact, int64_t best_cost, int best_skip, |
| 1893 | int extra_shift) { |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1894 | int skip_this_mode = 0; |
| 1895 | const THR_MODES mode_index = mode_idx[ref_frame][INTER_OFFSET(mode)]; |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 1896 | int64_t mode_rd_thresh = |
| 1897 | best_skip ? ((int64_t)rd_threshes[mode_index]) << (extra_shift + 1) |
| 1898 | : ((int64_t)rd_threshes[mode_index]) << extra_shift; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1899 | |
| 1900 | // Increase mode_rd_thresh value for non-LAST for improved encoding |
| 1901 | // speed |
| 1902 | if (ref_frame != LAST_FRAME) { |
| 1903 | mode_rd_thresh = mode_rd_thresh << 1; |
| 1904 | if (ref_frame == GOLDEN_FRAME && frames_since_golden > 4) |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 1905 | mode_rd_thresh = mode_rd_thresh << (extra_shift + 1); |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | if (rd_less_than_thresh(best_cost, mode_rd_thresh, |
| 1909 | rd_thresh_freq_fact[mode_index])) |
| 1910 | if (mv.as_int != 0) skip_this_mode = 1; |
| 1911 | |
| 1912 | return skip_this_mode; |
| 1913 | } |
| 1914 | |
Marco Paniconi | 988b34a | 2020-11-09 12:41:13 -0800 | [diff] [blame] | 1915 | static AOM_INLINE int skip_mode_by_low_temp( |
| 1916 | PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize, |
| 1917 | 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] | 1918 | // Skip non-zeromv mode search for non-LAST frame if force_skip_low_temp_var |
| 1919 | // is set. If nearestmv for golden frame is 0, zeromv mode will be skipped |
| 1920 | // later. |
| 1921 | if (force_skip_low_temp_var && ref_frame != LAST_FRAME && mv.as_int != 0) { |
| 1922 | return 1; |
| 1923 | } |
| 1924 | |
Neeraj Gadgil | 710cc54 | 2022-08-22 12:05:25 +0530 | [diff] [blame] | 1925 | if (content_state_sb.source_sad_nonrd != kHighSad && bsize >= BLOCK_64X64 && |
| 1926 | force_skip_low_temp_var && mode == NEWMV) { |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1927 | return 1; |
| 1928 | } |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | static AOM_INLINE int skip_mode_by_bsize_and_ref_frame( |
| 1933 | PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, BLOCK_SIZE bsize, |
Marco Paniconi | f969060 | 2024-04-08 13:53:02 -0700 | [diff] [blame] | 1934 | int extra_prune, unsigned int sse_zeromv_norm, int more_prune, |
| 1935 | int skip_nearmv) { |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1936 | const unsigned int thresh_skip_golden = 500; |
| 1937 | |
| 1938 | if (ref_frame != LAST_FRAME && sse_zeromv_norm < thresh_skip_golden && |
| 1939 | mode == NEWMV) |
| 1940 | return 1; |
| 1941 | |
Marco Paniconi | f969060 | 2024-04-08 13:53:02 -0700 | [diff] [blame] | 1942 | if ((bsize == BLOCK_128X128 && mode == NEWMV) || |
| 1943 | (skip_nearmv && mode == NEARMV)) |
| 1944 | return 1; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1945 | |
| 1946 | // Skip testing non-LAST if this flag is set. |
| 1947 | if (extra_prune) { |
| 1948 | if (extra_prune > 1 && ref_frame != LAST_FRAME && |
Marco Paniconi | 9d6cef0 | 2021-10-25 00:02:48 -0700 | [diff] [blame] | 1949 | (bsize > BLOCK_16X16 && mode == NEWMV)) |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1950 | return 1; |
| 1951 | |
| 1952 | if (ref_frame != LAST_FRAME && mode == NEARMV) return 1; |
Fyodor Kyslov | 2943090 | 2020-11-18 13:44:30 -0800 | [diff] [blame] | 1953 | |
| 1954 | if (more_prune && bsize >= BLOCK_32X32 && mode == NEARMV) return 1; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 1955 | } |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
Marco Paniconi | bdef9a9 | 2023-07-06 15:20:46 -0700 | [diff] [blame] | 1959 | static void set_block_source_sad(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, |
| 1960 | struct buf_2d *yv12_mb) { |
| 1961 | struct macroblock_plane *const p = &x->plane[0]; |
| 1962 | const int y_sad = cpi->ppi->fn_ptr[bsize].sdf(p->src.buf, p->src.stride, |
| 1963 | yv12_mb->buf, yv12_mb->stride); |
| 1964 | if (y_sad == 0) x->block_is_zero_sad = 1; |
| 1965 | } |
| 1966 | |
James Zern | fa7042e | 2022-10-27 18:25:57 -0700 | [diff] [blame] | 1967 | static void set_color_sensitivity(AV1_COMP *cpi, MACROBLOCK *x, |
| 1968 | BLOCK_SIZE bsize, int y_sad, |
| 1969 | unsigned int source_variance, |
| 1970 | struct buf_2d yv12_mb[MAX_MB_PLANE]) { |
Deepa K G | 309d0af | 2022-07-15 00:17:17 +0530 | [diff] [blame] | 1971 | const int subsampling_x = cpi->common.seq_params->subsampling_x; |
| 1972 | const int subsampling_y = cpi->common.seq_params->subsampling_y; |
Marco Paniconi | d4b429f | 2023-07-27 16:06:22 -0700 | [diff] [blame] | 1973 | const int source_sad_nonrd = x->content_state_sb.source_sad_nonrd; |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 1974 | const int high_res = cpi->common.width * cpi->common.height >= 640 * 360; |
| 1975 | if (bsize == cpi->common.seq_params->sb_size) { |
| 1976 | // At superblock level color_sensitivity is already set to 0, 1, or 2. |
| 1977 | // 2 is middle/uncertain level. To avoid additional sad |
| 1978 | // computations when bsize = sb_size force level 2 to 1 (certain color) |
| 1979 | // for motion areas. |
| 1980 | if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 2) { |
| 1981 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = |
| 1982 | source_sad_nonrd >= kMedSad ? 1 : 0; |
| 1983 | } |
| 1984 | if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 2) { |
| 1985 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = |
| 1986 | source_sad_nonrd >= kMedSad ? 1 : 0; |
| 1987 | } |
| 1988 | return; |
| 1989 | } |
Marco Paniconi | 35c281a | 2022-06-08 12:19:03 -0700 | [diff] [blame] | 1990 | int shift = 3; |
Marco Paniconi | aea4a5c | 2023-11-14 10:25:08 -0800 | [diff] [blame] | 1991 | unsigned int source_var_thr = 50; |
| 1992 | int uv_sad_thr = 100; |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 1993 | if (source_sad_nonrd >= kMedSad && x->source_variance > 0 && high_res) |
Marco Paniconi | d4b429f | 2023-07-27 16:06:22 -0700 | [diff] [blame] | 1994 | shift = 4; |
Marco Paniconi | aea4a5c | 2023-11-14 10:25:08 -0800 | [diff] [blame] | 1995 | if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) { |
| 1996 | if (cpi->rc.high_source_sad) shift = 6; |
| 1997 | if (source_sad_nonrd > kMedSad) { |
| 1998 | source_var_thr = 1200; |
| 1999 | uv_sad_thr = 10; |
| 2000 | } |
Marco Paniconi | 35c281a | 2022-06-08 12:19:03 -0700 | [diff] [blame] | 2001 | } |
Marco Paniconi | 737820e | 2021-05-24 11:03:38 -0700 | [diff] [blame] | 2002 | NOISE_LEVEL noise_level = kLow; |
| 2003 | int norm_sad = |
| 2004 | y_sad >> (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]); |
Marco Paniconi | 42223ee | 2022-06-20 22:25:02 -0700 | [diff] [blame] | 2005 | unsigned int thresh_spatial = (cpi->common.width > 1920) ? 5000 : 1000; |
Marco Paniconi | 737820e | 2021-05-24 11:03:38 -0700 | [diff] [blame] | 2006 | // If the spatial source variance is high and the normalized y_sad |
| 2007 | // is low, then y-channel is likely good for mode estimation, so keep |
| 2008 | // color_sensitivity off. For low noise content for now, since there is |
| 2009 | // some bdrate regression for noisy color clip. |
| 2010 | if (cpi->noise_estimate.enabled) |
| 2011 | noise_level = av1_noise_estimate_extract_level(&cpi->noise_estimate); |
Marco Paniconi | 42223ee | 2022-06-20 22:25:02 -0700 | [diff] [blame] | 2012 | if (noise_level == kLow && source_variance > thresh_spatial && |
Marco Paniconi | c57620a | 2022-10-19 14:14:39 -0700 | [diff] [blame] | 2013 | cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN && norm_sad < 50) { |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 2014 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = 0; |
| 2015 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = 0; |
Marco Paniconi | 737820e | 2021-05-24 11:03:38 -0700 | [diff] [blame] | 2016 | return; |
| 2017 | } |
James Zern | bf9d1f8 | 2022-10-27 18:17:47 -0700 | [diff] [blame] | 2018 | const int num_planes = av1_num_planes(&cpi->common); |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2019 | |
| 2020 | for (int plane = AOM_PLANE_U; plane < num_planes; ++plane) { |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 2021 | // Always check if level = 2. If level = 0 check again for |
| 2022 | // motion areas for higher resolns, where color artifacts |
| 2023 | // are more noticeable. |
Anupam Pandey | 8b325e7 | 2022-12-22 01:51:39 +0530 | [diff] [blame] | 2024 | if (x->color_sensitivity[COLOR_SENS_IDX(plane)] == 2 || |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 2025 | (x->color_sensitivity[COLOR_SENS_IDX(plane)] == 0 && |
| 2026 | source_sad_nonrd >= kMedSad && high_res)) { |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2027 | struct macroblock_plane *const p = &x->plane[plane]; |
Marco Paniconi | 737820e | 2021-05-24 11:03:38 -0700 | [diff] [blame] | 2028 | const BLOCK_SIZE bs = |
Deepa K G | 309d0af | 2022-07-15 00:17:17 +0530 | [diff] [blame] | 2029 | get_plane_block_size(bsize, subsampling_x, subsampling_y); |
| 2030 | |
| 2031 | const int uv_sad = cpi->ppi->fn_ptr[bs].sdf( |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2032 | p->src.buf, p->src.stride, yv12_mb[plane].buf, yv12_mb[plane].stride); |
Deepa K G | 309d0af | 2022-07-15 00:17:17 +0530 | [diff] [blame] | 2033 | |
Marco Paniconi | 737820e | 2021-05-24 11:03:38 -0700 | [diff] [blame] | 2034 | const int norm_uv_sad = |
| 2035 | uv_sad >> (b_width_log2_lookup[bs] + b_height_log2_lookup[bs]); |
Anupam Pandey | 8b325e7 | 2022-12-22 01:51:39 +0530 | [diff] [blame] | 2036 | x->color_sensitivity[COLOR_SENS_IDX(plane)] = |
Marco Paniconi | 058ced7 | 2023-02-22 23:28:46 -0800 | [diff] [blame] | 2037 | uv_sad > (y_sad >> shift) && norm_uv_sad > 40; |
Marco Paniconi | aea4a5c | 2023-11-14 10:25:08 -0800 | [diff] [blame] | 2038 | if (source_variance < source_var_thr && norm_uv_sad > uv_sad_thr) |
Anupam Pandey | 8b325e7 | 2022-12-22 01:51:39 +0530 | [diff] [blame] | 2039 | x->color_sensitivity[COLOR_SENS_IDX(plane)] = 1; |
Marco Paniconi | 737820e | 2021-05-24 11:03:38 -0700 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
James Zern | fa7042e | 2022-10-27 18:25:57 -0700 | [diff] [blame] | 2044 | static void setup_compound_prediction(const AV1_COMMON *cm, MACROBLOCK *x, |
| 2045 | struct buf_2d yv12_mb[8][MAX_MB_PLANE], |
| 2046 | const int *use_ref_frame_mask, |
| 2047 | const MV_REFERENCE_FRAME *rf, |
| 2048 | int *ref_mv_idx) { |
Marco Paniconi | 3c87aae | 2021-07-28 10:55:25 -0700 | [diff] [blame] | 2049 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2050 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
| 2051 | MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext; |
Marco Paniconi | 3c87aae | 2021-07-28 10:55:25 -0700 | [diff] [blame] | 2052 | MV_REFERENCE_FRAME ref_frame_comp; |
Marco Paniconi | 3c87aae | 2021-07-28 10:55:25 -0700 | [diff] [blame] | 2053 | if (!use_ref_frame_mask[rf[1]]) { |
| 2054 | // Need to setup pred_block, if it hasn't been done in find_predictors. |
| 2055 | const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, rf[1]); |
| 2056 | const int num_planes = av1_num_planes(cm); |
| 2057 | if (yv12 != NULL) { |
| 2058 | const struct scale_factors *const sf = |
| 2059 | get_ref_scale_factors_const(cm, rf[1]); |
| 2060 | av1_setup_pred_block(xd, yv12_mb[rf[1]], yv12, sf, sf, num_planes); |
| 2061 | } |
| 2062 | } |
| 2063 | ref_frame_comp = av1_ref_frame_type(rf); |
| 2064 | mbmi_ext->mode_context[ref_frame_comp] = 0; |
| 2065 | mbmi_ext->ref_mv_count[ref_frame_comp] = UINT8_MAX; |
| 2066 | av1_find_mv_refs(cm, xd, mbmi, ref_frame_comp, mbmi_ext->ref_mv_count, |
| 2067 | xd->ref_mv_stack, xd->weight, NULL, mbmi_ext->global_mvs, |
| 2068 | mbmi_ext->mode_context); |
| 2069 | av1_copy_usable_ref_mv_stack_and_weight(xd, mbmi_ext, ref_frame_comp); |
| 2070 | *ref_mv_idx = mbmi->ref_mv_idx + 1; |
| 2071 | } |
| 2072 | |
Anupam Pandey | cf5a52e | 2022-12-14 18:25:44 +0530 | [diff] [blame] | 2073 | static void set_compound_mode(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame, |
| 2074 | MV_REFERENCE_FRAME ref_frame2, int ref_mv_idx, |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 2075 | int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2076 | PREDICTION_MODE this_mode) { |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 2077 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2078 | MB_MODE_INFO *const mi = xd->mi[0]; |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 2079 | mi->ref_frame[0] = ref_frame; |
| 2080 | mi->ref_frame[1] = ref_frame2; |
| 2081 | mi->compound_idx = 1; |
| 2082 | mi->comp_group_idx = 0; |
| 2083 | mi->interinter_comp.type = COMPOUND_AVERAGE; |
Marco Paniconi | 3c87aae | 2021-07-28 10:55:25 -0700 | [diff] [blame] | 2084 | MV_REFERENCE_FRAME ref_frame_comp = av1_ref_frame_type(mi->ref_frame); |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2085 | if (this_mode == GLOBAL_GLOBALMV) { |
| 2086 | frame_mv[this_mode][ref_frame].as_int = 0; |
| 2087 | frame_mv[this_mode][ref_frame2].as_int = 0; |
| 2088 | } else if (this_mode == NEAREST_NEARESTMV) { |
| 2089 | frame_mv[this_mode][ref_frame].as_int = |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 2090 | xd->ref_mv_stack[ref_frame_comp][0].this_mv.as_int; |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2091 | frame_mv[this_mode][ref_frame2].as_int = |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 2092 | xd->ref_mv_stack[ref_frame_comp][0].comp_mv.as_int; |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2093 | } else if (this_mode == NEAR_NEARMV) { |
| 2094 | frame_mv[this_mode][ref_frame].as_int = |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 2095 | xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].this_mv.as_int; |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2096 | frame_mv[this_mode][ref_frame2].as_int = |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 2097 | xd->ref_mv_stack[ref_frame_comp][ref_mv_idx].comp_mv.as_int; |
| 2098 | } |
| 2099 | } |
| 2100 | |
chiyotsai | a82b98f | 2022-08-17 15:59:47 -0700 | [diff] [blame] | 2101 | // Prune compound mode if the single mode variance is lower than a fixed |
| 2102 | // percentage of the median value. |
| 2103 | static bool skip_comp_based_on_var( |
| 2104 | const unsigned int (*single_vars)[REF_FRAMES], BLOCK_SIZE bsize) { |
| 2105 | unsigned int best_var = UINT_MAX; |
| 2106 | for (int cur_mode_idx = 0; cur_mode_idx < RTC_INTER_MODES; cur_mode_idx++) { |
| 2107 | for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) { |
| 2108 | best_var = AOMMIN(best_var, single_vars[cur_mode_idx][ref_idx]); |
| 2109 | } |
| 2110 | } |
| 2111 | const unsigned int thresh_64 = (unsigned int)(0.57356805f * 8659); |
| 2112 | const unsigned int thresh_32 = (unsigned int)(0.23964763f * 4281); |
| 2113 | |
chiyotsai | 2effced | 2022-08-31 13:15:08 -0700 | [diff] [blame] | 2114 | // Currently, the thresh for 128 and 16 are not well-tuned. We are using the |
| 2115 | // results from 64 and 32 as an heuristic. |
| 2116 | switch (bsize) { |
| 2117 | case BLOCK_128X128: return best_var < 4 * thresh_64; |
| 2118 | case BLOCK_64X64: return best_var < thresh_64; |
| 2119 | case BLOCK_32X32: return best_var < thresh_32; |
| 2120 | case BLOCK_16X16: return best_var < thresh_32 / 4; |
| 2121 | default: return false; |
chiyotsai | a82b98f | 2022-08-17 15:59:47 -0700 | [diff] [blame] | 2122 | } |
chiyotsai | a82b98f | 2022-08-17 15:59:47 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
chiyotsai | 901f406 | 2022-07-06 14:57:53 -0700 | [diff] [blame] | 2125 | static AOM_FORCE_INLINE void fill_single_inter_mode_costs( |
Yunqing Wang | ddc0bf9 | 2022-12-15 11:17:40 -0800 | [diff] [blame] | 2126 | int (*single_inter_mode_costs)[REF_FRAMES], int num_inter_modes, |
linzhen | e4954c2 | 2022-09-20 18:27:52 +0000 | [diff] [blame] | 2127 | const REF_MODE *reference_mode_set, const ModeCosts *mode_costs, |
chiyotsai | 901f406 | 2022-07-06 14:57:53 -0700 | [diff] [blame] | 2128 | const int16_t *mode_context) { |
| 2129 | bool ref_frame_used[REF_FRAMES] = { false }; |
| 2130 | for (int idx = 0; idx < num_inter_modes; idx++) { |
linzhen | e4954c2 | 2022-09-20 18:27:52 +0000 | [diff] [blame] | 2131 | ref_frame_used[reference_mode_set[idx].ref_frame] = true; |
chiyotsai | 901f406 | 2022-07-06 14:57:53 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | for (int this_ref_frame = LAST_FRAME; this_ref_frame < REF_FRAMES; |
| 2135 | this_ref_frame++) { |
| 2136 | if (!ref_frame_used[this_ref_frame]) { |
| 2137 | continue; |
| 2138 | } |
| 2139 | |
| 2140 | const MV_REFERENCE_FRAME rf[2] = { this_ref_frame, NONE_FRAME }; |
| 2141 | const int16_t mode_ctx = av1_mode_context_analyzer(mode_context, rf); |
| 2142 | for (PREDICTION_MODE this_mode = NEARESTMV; this_mode <= NEWMV; |
| 2143 | this_mode++) { |
| 2144 | single_inter_mode_costs[INTER_OFFSET(this_mode)][this_ref_frame] = |
| 2145 | cost_mv_ref(mode_costs, this_mode, mode_ctx); |
| 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | static AOM_INLINE bool is_globalmv_better( |
| 2151 | PREDICTION_MODE this_mode, MV_REFERENCE_FRAME ref_frame, int rate_mv, |
| 2152 | const ModeCosts *mode_costs, |
| 2153 | const int (*single_inter_mode_costs)[REF_FRAMES], |
| 2154 | const MB_MODE_INFO_EXT *mbmi_ext) { |
| 2155 | const int globalmv_mode_cost = |
| 2156 | single_inter_mode_costs[INTER_OFFSET(GLOBALMV)][ref_frame]; |
| 2157 | int this_mode_cost = |
| 2158 | rate_mv + single_inter_mode_costs[INTER_OFFSET(this_mode)][ref_frame]; |
| 2159 | if (this_mode == NEWMV || this_mode == NEARMV) { |
| 2160 | const MV_REFERENCE_FRAME rf[2] = { ref_frame, NONE_FRAME }; |
| 2161 | this_mode_cost += get_drl_cost( |
| 2162 | NEWMV, 0, mbmi_ext, mode_costs->drl_mode_cost0, av1_ref_frame_type(rf)); |
| 2163 | } |
| 2164 | return this_mode_cost > globalmv_mode_cost; |
| 2165 | } |
| 2166 | |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2167 | // Set up the mv/ref_frames etc based on the comp_index. Returns 1 if it |
| 2168 | // succeeds, 0 if it fails. |
| 2169 | static AOM_INLINE int setup_compound_params_from_comp_idx( |
| 2170 | const AV1_COMP *cpi, MACROBLOCK *x, struct buf_2d yv12_mb[8][MAX_MB_PLANE], |
| 2171 | PREDICTION_MODE *this_mode, MV_REFERENCE_FRAME *ref_frame, |
| 2172 | MV_REFERENCE_FRAME *ref_frame2, int_mv frame_mv[MB_MODE_COUNT][REF_FRAMES], |
| 2173 | const int *use_ref_frame_mask, int comp_index, |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 2174 | bool comp_use_zero_zeromv_only, MV_REFERENCE_FRAME *last_comp_ref_frame, |
| 2175 | BLOCK_SIZE bsize) { |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2176 | const MV_REFERENCE_FRAME *rf = comp_ref_mode_set[comp_index].ref_frame; |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 2177 | int skip_gf = 0; |
| 2178 | int skip_alt = 0; |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2179 | *this_mode = comp_ref_mode_set[comp_index].pred_mode; |
| 2180 | *ref_frame = rf[0]; |
| 2181 | *ref_frame2 = rf[1]; |
| 2182 | assert(*ref_frame == LAST_FRAME); |
| 2183 | assert(*this_mode == GLOBAL_GLOBALMV || *this_mode == NEAREST_NEARESTMV); |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 2184 | if (x->source_variance < 50 && bsize > BLOCK_16X16) { |
| 2185 | if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 || |
| 2186 | x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1) |
| 2187 | skip_gf = 1; |
| 2188 | if (x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 || |
| 2189 | x->color_sensitivity_sb_alt[COLOR_SENS_IDX(AOM_PLANE_V)] == 1) |
| 2190 | skip_alt = 1; |
| 2191 | } |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2192 | if (comp_use_zero_zeromv_only && *this_mode != GLOBAL_GLOBALMV) { |
| 2193 | return 0; |
| 2194 | } |
| 2195 | if (*ref_frame2 == GOLDEN_FRAME && |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 2196 | (cpi->sf.rt_sf.ref_frame_comp_nonrd[0] == 0 || skip_gf || |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2197 | !(cpi->ref_frame_flags & AOM_GOLD_FLAG))) { |
| 2198 | return 0; |
| 2199 | } else if (*ref_frame2 == LAST2_FRAME && |
| 2200 | (cpi->sf.rt_sf.ref_frame_comp_nonrd[1] == 0 || |
| 2201 | !(cpi->ref_frame_flags & AOM_LAST2_FLAG))) { |
| 2202 | return 0; |
| 2203 | } else if (*ref_frame2 == ALTREF_FRAME && |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 2204 | (cpi->sf.rt_sf.ref_frame_comp_nonrd[2] == 0 || skip_alt || |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 2205 | !(cpi->ref_frame_flags & AOM_ALT_FLAG))) { |
| 2206 | return 0; |
| 2207 | } |
| 2208 | int ref_mv_idx = 0; |
| 2209 | if (*last_comp_ref_frame != rf[1]) { |
| 2210 | // Only needs to be done once per reference pair. |
| 2211 | setup_compound_prediction(&cpi->common, x, yv12_mb, use_ref_frame_mask, rf, |
| 2212 | &ref_mv_idx); |
| 2213 | *last_comp_ref_frame = rf[1]; |
| 2214 | } |
| 2215 | set_compound_mode(x, *ref_frame, *ref_frame2, ref_mv_idx, frame_mv, |
| 2216 | *this_mode); |
| 2217 | if (*this_mode != GLOBAL_GLOBALMV && |
| 2218 | frame_mv[*this_mode][*ref_frame].as_int == 0 && |
| 2219 | frame_mv[*this_mode][*ref_frame2].as_int == 0) { |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
| 2223 | return 1; |
| 2224 | } |
| 2225 | |
chiyotsai | 1538b07 | 2022-07-26 10:25:14 -0700 | [diff] [blame] | 2226 | static AOM_INLINE bool previous_mode_performed_poorly( |
| 2227 | PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame, |
| 2228 | const unsigned int (*vars)[REF_FRAMES], |
| 2229 | const int64_t (*uv_dist)[REF_FRAMES]) { |
| 2230 | unsigned int best_var = UINT_MAX; |
| 2231 | int64_t best_uv_dist = INT64_MAX; |
| 2232 | for (int midx = 0; midx < RTC_INTER_MODES; midx++) { |
| 2233 | best_var = AOMMIN(best_var, vars[midx][ref_frame]); |
| 2234 | best_uv_dist = AOMMIN(best_uv_dist, uv_dist[midx][ref_frame]); |
| 2235 | } |
| 2236 | assert(best_var != UINT_MAX && "Invalid variance data."); |
| 2237 | const float mult = 1.125f; |
| 2238 | bool var_bad = mult * best_var < vars[INTER_OFFSET(mode)][ref_frame]; |
| 2239 | if (uv_dist[INTER_OFFSET(mode)][ref_frame] < INT64_MAX && |
| 2240 | best_uv_dist != uv_dist[INTER_OFFSET(mode)][ref_frame]) { |
| 2241 | // If we have chroma info, then take it into account |
| 2242 | var_bad &= mult * best_uv_dist < uv_dist[INTER_OFFSET(mode)][ref_frame]; |
| 2243 | } |
| 2244 | return var_bad; |
| 2245 | } |
| 2246 | |
| 2247 | static AOM_INLINE bool prune_compoundmode_with_singlemode_var( |
| 2248 | PREDICTION_MODE compound_mode, MV_REFERENCE_FRAME ref_frame, |
| 2249 | MV_REFERENCE_FRAME ref_frame2, const int_mv (*frame_mv)[REF_FRAMES], |
| 2250 | const uint8_t (*mode_checked)[REF_FRAMES], |
| 2251 | const unsigned int (*vars)[REF_FRAMES], |
| 2252 | const int64_t (*uv_dist)[REF_FRAMES]) { |
| 2253 | const PREDICTION_MODE single_mode0 = compound_ref0_mode(compound_mode); |
| 2254 | const PREDICTION_MODE single_mode1 = compound_ref1_mode(compound_mode); |
| 2255 | |
| 2256 | bool first_ref_valid = false, second_ref_valid = false; |
| 2257 | bool first_ref_bad = false, second_ref_bad = false; |
| 2258 | if (mode_checked[single_mode0][ref_frame] && |
| 2259 | frame_mv[single_mode0][ref_frame].as_int == |
| 2260 | frame_mv[compound_mode][ref_frame].as_int && |
| 2261 | vars[INTER_OFFSET(single_mode0)][ref_frame] < UINT_MAX) { |
| 2262 | first_ref_valid = true; |
| 2263 | first_ref_bad = |
| 2264 | previous_mode_performed_poorly(single_mode0, ref_frame, vars, uv_dist); |
| 2265 | } |
| 2266 | if (mode_checked[single_mode1][ref_frame2] && |
| 2267 | frame_mv[single_mode1][ref_frame2].as_int == |
| 2268 | frame_mv[compound_mode][ref_frame2].as_int && |
| 2269 | vars[INTER_OFFSET(single_mode1)][ref_frame2] < UINT_MAX) { |
| 2270 | second_ref_valid = true; |
| 2271 | second_ref_bad = |
| 2272 | previous_mode_performed_poorly(single_mode1, ref_frame2, vars, uv_dist); |
| 2273 | } |
| 2274 | if (first_ref_valid && second_ref_valid) { |
| 2275 | return first_ref_bad && second_ref_bad; |
| 2276 | } else if (first_ref_valid || second_ref_valid) { |
| 2277 | return first_ref_bad || second_ref_bad; |
| 2278 | } |
| 2279 | return false; |
| 2280 | } |
| 2281 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2282 | // Function to setup parameters used for inter mode evaluation in non-rd. |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2283 | static AOM_FORCE_INLINE void set_params_nonrd_pick_inter_mode( |
| 2284 | AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state, |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 2285 | RD_STATS *rd_cost, int *force_skip_low_temp_var, int mi_row, int mi_col, |
| 2286 | int gf_temporal_ref, unsigned char segment_id, BLOCK_SIZE bsize |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2287 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 2288 | , |
Anupam Pandey | 23cd339 | 2022-12-22 11:34:09 +0530 | [diff] [blame] | 2289 | PICK_MODE_CONTEXT *ctx, int denoise_svc_pickmode |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2290 | #endif |
| 2291 | ) { |
| 2292 | AV1_COMMON *const cm = &cpi->common; |
| 2293 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2294 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
| 2295 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 2296 | const ModeCosts *mode_costs = &x->mode_costs; |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 2297 | int skip_pred_mv = 0; |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2298 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2299 | // Initialize variance and distortion (chroma) for all modes and reference |
| 2300 | // frames |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2301 | for (int idx = 0; idx < RTC_INTER_MODES; idx++) { |
| 2302 | for (int ref = 0; ref < REF_FRAMES; ref++) { |
| 2303 | search_state->vars[idx][ref] = UINT_MAX; |
| 2304 | search_state->uv_dist[idx][ref] = INT64_MAX; |
| 2305 | } |
| 2306 | } |
| 2307 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2308 | // Initialize values of color sensitivity with sb level color sensitivity |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 2309 | av1_copy(x->color_sensitivity, x->color_sensitivity_sb); |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2310 | |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2311 | init_best_pickmode(&search_state->best_pickmode); |
| 2312 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2313 | // Estimate cost for single reference frames |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2314 | estimate_single_ref_frame_costs(cm, xd, mode_costs, segment_id, bsize, |
| 2315 | search_state->ref_costs_single); |
| 2316 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2317 | // Reset flag to indicate modes evaluated |
Anupam Pandey | a080bf6 | 2022-12-15 16:52:25 +0530 | [diff] [blame] | 2318 | av1_zero(search_state->mode_checked); |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2319 | |
| 2320 | txfm_info->skip_txfm = 0; |
| 2321 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2322 | // Initialize mode decisions |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2323 | av1_invalid_rd_stats(&search_state->best_rdc); |
| 2324 | av1_invalid_rd_stats(&search_state->this_rdc); |
| 2325 | av1_invalid_rd_stats(rd_cost); |
Anupam Pandey | cbe4eb4 | 2022-12-21 12:44:47 +0530 | [diff] [blame] | 2326 | for (int ref_idx = 0; ref_idx < REF_FRAMES; ++ref_idx) { |
| 2327 | x->warp_sample_info[ref_idx].num = -1; |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | mi->bsize = bsize; |
| 2331 | mi->ref_frame[0] = NONE_FRAME; |
| 2332 | mi->ref_frame[1] = NONE_FRAME; |
| 2333 | |
| 2334 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 2335 | if (cpi->oxcf.noise_sensitivity > 0) { |
| 2336 | // if (cpi->ppi->use_svc) denoise_svc_pickmode = |
| 2337 | // av1_denoise_svc_non_key(cpi); |
| 2338 | if (cpi->denoiser.denoising_level > kDenLowLow && denoise_svc_pickmode) |
| 2339 | av1_denoiser_reset_frame_stats(ctx); |
| 2340 | } |
| 2341 | #endif |
| 2342 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2343 | // Populate predicated motion vectors for LAST_FRAME |
Marco Paniconi | a1f2968 | 2023-09-17 20:09:51 -0700 | [diff] [blame] | 2344 | if (cpi->ref_frame_flags & AOM_LAST_FLAG) { |
Anupam Pandey | 05c81f8 | 2022-12-21 12:58:34 +0530 | [diff] [blame] | 2345 | find_predictors(cpi, x, LAST_FRAME, search_state->frame_mv, |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2346 | search_state->yv12_mb, bsize, *force_skip_low_temp_var, |
Marco Paniconi | 0aeecfe | 2023-10-04 00:06:46 -0700 | [diff] [blame] | 2347 | x->force_zeromv_skip_for_blk, |
| 2348 | &search_state->use_scaled_ref_frame[LAST_FRAME]); |
Marco Paniconi | a1f2968 | 2023-09-17 20:09:51 -0700 | [diff] [blame] | 2349 | } |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2350 | // Update mask to use all reference frame |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2351 | get_ref_frame_use_mask(cpi, x, mi, mi_row, mi_col, bsize, gf_temporal_ref, |
| 2352 | search_state->use_ref_frame_mask, |
| 2353 | force_skip_low_temp_var); |
| 2354 | |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 2355 | skip_pred_mv = x->force_zeromv_skip_for_blk || |
| 2356 | (x->nonrd_prune_ref_frame_search > 2 && |
| 2357 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] != 2 && |
| 2358 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] != 2); |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2359 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2360 | // Populate predicated motion vectors for other single reference frame |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2361 | // Start at LAST_FRAME + 1. |
| 2362 | for (MV_REFERENCE_FRAME ref_frame_iter = LAST_FRAME + 1; |
| 2363 | ref_frame_iter <= ALTREF_FRAME; ++ref_frame_iter) { |
| 2364 | if (search_state->use_ref_frame_mask[ref_frame_iter]) { |
Anupam Pandey | 05c81f8 | 2022-12-21 12:58:34 +0530 | [diff] [blame] | 2365 | find_predictors(cpi, x, ref_frame_iter, search_state->frame_mv, |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2366 | search_state->yv12_mb, bsize, *force_skip_low_temp_var, |
Marco Paniconi | 0aeecfe | 2023-10-04 00:06:46 -0700 | [diff] [blame] | 2367 | skip_pred_mv, |
| 2368 | &search_state->use_scaled_ref_frame[ref_frame_iter]); |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2373 | // Function to check the inter mode can be skipped based on mode statistics and |
| 2374 | // speed features settings. |
| 2375 | static AOM_FORCE_INLINE bool skip_inter_mode_nonrd( |
| 2376 | AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state, |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2377 | int64_t *thresh_sad_pred, int *force_mv_inter_layer, int *is_single_pred, |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2378 | PREDICTION_MODE *this_mode, MV_REFERENCE_FRAME *last_comp_ref_frame, |
| 2379 | MV_REFERENCE_FRAME *ref_frame, MV_REFERENCE_FRAME *ref_frame2, int idx, |
Anupam Pandey | cf5a52e | 2022-12-14 18:25:44 +0530 | [diff] [blame] | 2380 | int_mv svc_mv, int force_skip_low_temp_var, unsigned int sse_zeromv_norm, |
Yunqing Wang | ddc0bf9 | 2022-12-15 11:17:40 -0800 | [diff] [blame] | 2381 | int num_inter_modes, unsigned char segment_id, BLOCK_SIZE bsize, |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2382 | bool comp_use_zero_zeromv_only, bool check_globalmv) { |
| 2383 | AV1_COMMON *const cm = &cpi->common; |
| 2384 | const struct segmentation *const seg = &cm->seg; |
| 2385 | const SVC *const svc = &cpi->svc; |
| 2386 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2387 | MB_MODE_INFO *const mi = xd->mi[0]; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2388 | const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf; |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2389 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2390 | // Skip compound mode based on reference frame mask and type of the mode and |
| 2391 | // for allowed compound modes, setup ref mv stack and reference frame. |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2392 | if (idx >= num_inter_modes) { |
| 2393 | const int comp_index = idx - num_inter_modes; |
| 2394 | if (!setup_compound_params_from_comp_idx( |
| 2395 | cpi, x, search_state->yv12_mb, this_mode, ref_frame, ref_frame2, |
| 2396 | search_state->frame_mv, search_state->use_ref_frame_mask, |
Marco Paniconi | 555b3aa | 2023-03-03 09:40:28 -0800 | [diff] [blame] | 2397 | comp_index, comp_use_zero_zeromv_only, last_comp_ref_frame, |
| 2398 | bsize)) { |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2399 | return true; |
| 2400 | } |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2401 | *is_single_pred = 0; |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2402 | } else { |
| 2403 | *this_mode = ref_mode_set[idx].pred_mode; |
| 2404 | *ref_frame = ref_mode_set[idx].ref_frame; |
| 2405 | *ref_frame2 = NONE_FRAME; |
| 2406 | } |
| 2407 | |
Marco Paniconi | d404347 | 2024-03-28 15:18:58 -0700 | [diff] [blame] | 2408 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) && |
| 2409 | (*this_mode != GLOBALMV || *ref_frame != LAST_FRAME)) |
| 2410 | return true; |
| 2411 | |
Marco Paniconi | 608f924 | 2024-04-08 10:34:11 -0700 | [diff] [blame] | 2412 | // Skip the mode if use reference frame mask flag is not set. |
| 2413 | if (!search_state->use_ref_frame_mask[*ref_frame]) return true; |
| 2414 | |
| 2415 | // Skip mode for some modes and reference frames when |
| 2416 | // force_zeromv_skip_for_blk flag is true. |
| 2417 | if (x->force_zeromv_skip_for_blk && |
| 2418 | ((!(*this_mode == NEARESTMV && |
| 2419 | search_state->frame_mv[*this_mode][*ref_frame].as_int == 0) && |
| 2420 | *this_mode != GLOBALMV) || |
| 2421 | *ref_frame != LAST_FRAME)) |
| 2422 | return true; |
| 2423 | |
Marco Paniconi | 5a4ea09 | 2023-08-09 14:36:15 -0700 | [diff] [blame] | 2424 | if (x->sb_me_block && *ref_frame == LAST_FRAME) { |
| 2425 | // We want to make sure to test the superblock MV: |
| 2426 | // so don't skip (return false) for NEAREST_LAST or NEAR_LAST if they |
| 2427 | // have this sb MV. And don't skip NEWMV_LAST: this will be set to |
| 2428 | // sb MV in handle_inter_mode_nonrd(), in case NEAREST or NEAR don't |
| 2429 | // have it. |
| 2430 | if (*this_mode == NEARESTMV && |
| 2431 | search_state->frame_mv[NEARESTMV][LAST_FRAME].as_int == |
| 2432 | x->sb_me_mv.as_int) { |
| 2433 | return false; |
| 2434 | } |
| 2435 | if (*this_mode == NEARMV && |
| 2436 | search_state->frame_mv[NEARMV][LAST_FRAME].as_int == |
| 2437 | x->sb_me_mv.as_int) { |
| 2438 | return false; |
| 2439 | } |
| 2440 | if (*this_mode == NEWMV) { |
| 2441 | return false; |
| 2442 | } |
| 2443 | } |
| 2444 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2445 | // Skip the single reference mode for which mode check flag is set. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2446 | if (*is_single_pred && search_state->mode_checked[*this_mode][*ref_frame]) { |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2447 | return true; |
| 2448 | } |
| 2449 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2450 | // Skip GLOBALMV mode if check_globalmv flag is not enabled. |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2451 | if (!check_globalmv && *this_mode == GLOBALMV) { |
| 2452 | return true; |
| 2453 | } |
| 2454 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2455 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2456 | aom_usec_timer_start(&x->ms_stat_nonrd.timer1); |
| 2457 | x->ms_stat_nonrd.num_searches[bsize][*this_mode]++; |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2458 | #endif |
| 2459 | mi->mode = *this_mode; |
| 2460 | mi->ref_frame[0] = *ref_frame; |
| 2461 | mi->ref_frame[1] = *ref_frame2; |
| 2462 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2463 | // Skip compound mode based on variance of previously evaluated single |
| 2464 | // reference modes. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2465 | if (rt_sf->prune_compoundmode_with_singlemode_var && !*is_single_pred && |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2466 | prune_compoundmode_with_singlemode_var( |
| 2467 | *this_mode, *ref_frame, *ref_frame2, search_state->frame_mv, |
| 2468 | search_state->mode_checked, search_state->vars, |
| 2469 | search_state->uv_dist)) { |
| 2470 | return true; |
| 2471 | } |
| 2472 | |
| 2473 | *force_mv_inter_layer = 0; |
| 2474 | if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 && |
| 2475 | ((*ref_frame == LAST_FRAME && svc->skip_mvsearch_last) || |
| 2476 | (*ref_frame == GOLDEN_FRAME && svc->skip_mvsearch_gf) || |
| 2477 | (*ref_frame == ALTREF_FRAME && svc->skip_mvsearch_altref))) { |
Anupam Pandey | cf5a52e | 2022-12-14 18:25:44 +0530 | [diff] [blame] | 2478 | // Only test mode if NEARESTMV/NEARMV is (svc_mv.mv.col, svc_mv.mv.row), |
| 2479 | // otherwise set NEWMV to (svc_mv.mv.col, svc_mv.mv.row). |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2480 | // Skip newmv and filter search. |
| 2481 | *force_mv_inter_layer = 1; |
| 2482 | if (*this_mode == NEWMV) { |
Anupam Pandey | cf5a52e | 2022-12-14 18:25:44 +0530 | [diff] [blame] | 2483 | search_state->frame_mv[*this_mode][*ref_frame] = svc_mv; |
| 2484 | } else if (search_state->frame_mv[*this_mode][*ref_frame].as_int != |
| 2485 | svc_mv.as_int) { |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2486 | return true; |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | // If the segment reference frame feature is enabled then do nothing if the |
| 2491 | // current ref frame is not allowed. |
| 2492 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && |
| 2493 | get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)(*ref_frame)) |
| 2494 | return true; |
| 2495 | |
Marco Paniconi | a33c841 | 2023-09-29 14:25:46 -0700 | [diff] [blame] | 2496 | // For screen content: skip mode testing based on source_sad. |
Marco Paniconi | bdada5c | 2024-05-22 14:30:55 -0700 | [diff] [blame] | 2497 | if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN && |
| 2498 | !x->force_zeromv_skip_for_blk) { |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2499 | // If source_sad is computed: skip non-zero motion |
| 2500 | // check for stationary (super)blocks. Otherwise if superblock |
Marco Paniconi | a33c841 | 2023-09-29 14:25:46 -0700 | [diff] [blame] | 2501 | // has motion skip the modes with zero motion on last reference |
| 2502 | // for flat blocks, and color is not set. |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2503 | // For the latter condition: the same condition should apply |
| 2504 | // to newmv if (0, 0), so this latter condition is repeated |
| 2505 | // below after search_new_mv. |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2506 | if (rt_sf->source_metrics_sb_nonrd) { |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2507 | if ((search_state->frame_mv[*this_mode][*ref_frame].as_int != 0 && |
| 2508 | x->content_state_sb.source_sad_nonrd == kZeroSad) || |
| 2509 | (search_state->frame_mv[*this_mode][*ref_frame].as_int == 0 && |
Marco Paniconi | a33c841 | 2023-09-29 14:25:46 -0700 | [diff] [blame] | 2510 | x->block_is_zero_sad == 0 && *ref_frame == LAST_FRAME && |
Marco Paniconi | 9a3b768 | 2023-07-10 10:33:13 -0700 | [diff] [blame] | 2511 | ((x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 && |
| 2512 | x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) || |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2513 | cpi->rc.high_source_sad) && |
| 2514 | x->source_variance == 0)) |
| 2515 | return true; |
| 2516 | } |
| 2517 | // Skip NEWMV search for flat blocks. |
Marco Paniconi | 62732c8 | 2024-04-30 15:24:49 -0700 | [diff] [blame] | 2518 | if (rt_sf->skip_newmv_flat_blocks_screen && *this_mode == NEWMV && |
| 2519 | x->source_variance < 100) |
| 2520 | return true; |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2521 | // Skip non-LAST for color on flat blocks. |
| 2522 | if (*ref_frame > LAST_FRAME && x->source_variance == 0 && |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 2523 | (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] == 1 || |
| 2524 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] == 1)) |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2525 | return true; |
| 2526 | } |
| 2527 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2528 | // Skip mode based on block size, reference frame mode and other block |
| 2529 | // properties. |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2530 | if (skip_mode_by_bsize_and_ref_frame( |
| 2531 | *this_mode, *ref_frame, bsize, x->nonrd_prune_ref_frame_search, |
Marco Paniconi | f969060 | 2024-04-08 13:53:02 -0700 | [diff] [blame] | 2532 | sse_zeromv_norm, rt_sf->nonrd_aggressive_skip, |
| 2533 | rt_sf->increase_source_sad_thresh)) |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2534 | return true; |
| 2535 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2536 | // Skip mode based on low temporal variance and souce sad. |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2537 | if (skip_mode_by_low_temp(*this_mode, *ref_frame, bsize, x->content_state_sb, |
| 2538 | search_state->frame_mv[*this_mode][*ref_frame], |
| 2539 | force_skip_low_temp_var)) |
| 2540 | return true; |
| 2541 | |
| 2542 | // Disable this drop out case if the ref frame segment level feature is |
| 2543 | // enabled for this segment. This is to prevent the possibility that we |
| 2544 | // end up unable to pick any mode. |
| 2545 | if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) { |
| 2546 | // Check for skipping GOLDEN and ALTREF based pred_mv_sad. |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2547 | if (rt_sf->nonrd_prune_ref_frame_search > 0 && |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2548 | x->pred_mv_sad[*ref_frame] != INT_MAX && *ref_frame != LAST_FRAME) { |
| 2549 | if ((int64_t)(x->pred_mv_sad[*ref_frame]) > *thresh_sad_pred) return true; |
| 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | // Check for skipping NEARMV based on pred_mv_sad. |
| 2554 | if (*this_mode == NEARMV && x->pred_mv1_sad[*ref_frame] != INT_MAX && |
| 2555 | x->pred_mv1_sad[*ref_frame] > (x->pred_mv0_sad[*ref_frame] << 1)) |
| 2556 | return true; |
| 2557 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2558 | // Skip single reference mode based on rd threshold. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2559 | if (*is_single_pred) { |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2560 | if (skip_mode_by_threshold( |
| 2561 | *this_mode, *ref_frame, |
| 2562 | search_state->frame_mv[*this_mode][*ref_frame], |
| 2563 | cpi->rc.frames_since_golden, cpi->rd.threshes[segment_id][bsize], |
| 2564 | x->thresh_freq_fact[bsize], search_state->best_rdc.rdcost, |
| 2565 | search_state->best_pickmode.best_mode_skip_txfm, |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2566 | (rt_sf->nonrd_aggressive_skip ? 1 : 0))) |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 2567 | return true; |
| 2568 | } |
| 2569 | return false; |
| 2570 | } |
| 2571 | |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2572 | // Function to perform inter mode evaluation for non-rd |
| 2573 | static AOM_FORCE_INLINE bool handle_inter_mode_nonrd( |
| 2574 | AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state, |
| 2575 | PICK_MODE_CONTEXT *ctx, PRED_BUFFER **this_mode_pred, |
| 2576 | PRED_BUFFER *tmp_buffer, InterPredParams inter_pred_params_sr, |
| 2577 | int *best_early_term, unsigned int *sse_zeromv_norm, bool *check_globalmv, |
| 2578 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 2579 | int64_t *zero_last_cost_orig, int denoise_svc_pickmode, |
| 2580 | #endif |
Marco Paniconi | 9a3b768 | 2023-07-10 10:33:13 -0700 | [diff] [blame] | 2581 | int idx, int force_mv_inter_layer, int is_single_pred, int gf_temporal_ref, |
| 2582 | int use_model_yrd_large, int filter_search_enabled_blk, BLOCK_SIZE bsize, |
| 2583 | PREDICTION_MODE this_mode, InterpFilter filt_select, |
Marco Paniconi | 5a4ea09 | 2023-08-09 14:36:15 -0700 | [diff] [blame] | 2584 | int cb_pred_filter_search, int reuse_inter_pred, |
| 2585 | int *sb_me_has_been_tested) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2586 | AV1_COMMON *const cm = &cpi->common; |
| 2587 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2588 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 2589 | const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext; |
| 2590 | const int mi_row = xd->mi_row; |
| 2591 | const int mi_col = xd->mi_col; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 2592 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y]; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2593 | const int bw = block_size_wide[bsize]; |
| 2594 | const InterpFilter filter_ref = cm->features.interp_filter; |
| 2595 | const InterpFilter default_interp_filter = EIGHTTAP_REGULAR; |
| 2596 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
| 2597 | const ModeCosts *mode_costs = &x->mode_costs; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2598 | const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf; |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 2599 | BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2600 | |
| 2601 | MV_REFERENCE_FRAME ref_frame = mi->ref_frame[0]; |
| 2602 | MV_REFERENCE_FRAME ref_frame2 = mi->ref_frame[1]; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2603 | int_mv *const this_mv = &search_state->frame_mv[this_mode][ref_frame]; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2604 | unsigned int var = UINT_MAX; |
| 2605 | int this_early_term = 0; |
| 2606 | int rate_mv = 0; |
| 2607 | int is_skippable; |
| 2608 | int skip_this_mv = 0; |
| 2609 | unsigned int var_threshold = UINT_MAX; |
| 2610 | PREDICTION_MODE this_best_mode; |
| 2611 | RD_STATS nonskip_rdc; |
| 2612 | av1_invalid_rd_stats(&nonskip_rdc); |
| 2613 | |
Marco Paniconi | 5a4ea09 | 2023-08-09 14:36:15 -0700 | [diff] [blame] | 2614 | if (x->sb_me_block && this_mode == NEWMV && ref_frame == LAST_FRAME) { |
| 2615 | // Set the NEWMV_LAST to the sb MV. |
| 2616 | search_state->frame_mv[NEWMV][LAST_FRAME].as_int = x->sb_me_mv.as_int; |
| 2617 | } else if (this_mode == NEWMV && !force_mv_inter_layer) { |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2618 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2619 | aom_usec_timer_start(&x->ms_stat_nonrd.timer2); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2620 | #endif |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2621 | // Find the best motion vector for single/compound mode. |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2622 | const bool skip_newmv = search_new_mv( |
| 2623 | cpi, x, search_state->frame_mv, ref_frame, gf_temporal_ref, bsize, |
| 2624 | mi_row, mi_col, &rate_mv, &search_state->best_rdc); |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2625 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2626 | aom_usec_timer_mark(&x->ms_stat_nonrd.timer2); |
| 2627 | x->ms_stat_nonrd.ms_time[bsize][this_mode] += |
| 2628 | aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2629 | #endif |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2630 | // Skip NEWMV mode, |
| 2631 | // (i). For bsize smaller than 16X16 |
| 2632 | // (ii). Based on sad of the predicted mv w.r.t LAST_FRAME |
| 2633 | // (iii). When motion vector is same as that of reference mv |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2634 | if (skip_newmv) { |
| 2635 | return true; |
| 2636 | } |
| 2637 | } |
| 2638 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2639 | // Check the current motion vector is same as that of previously evaluated |
| 2640 | // motion vectors. |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2641 | for (PREDICTION_MODE inter_mv_mode = NEARESTMV; inter_mv_mode <= NEWMV; |
| 2642 | inter_mv_mode++) { |
| 2643 | if (inter_mv_mode == this_mode) continue; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2644 | if (is_single_pred && |
| 2645 | search_state->mode_checked[inter_mv_mode][ref_frame] && |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2646 | this_mv->as_int == |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2647 | search_state->frame_mv[inter_mv_mode][ref_frame].as_int) { |
| 2648 | skip_this_mv = 1; |
| 2649 | break; |
| 2650 | } |
| 2651 | } |
| 2652 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2653 | // Skip single mode if current motion vector is same that of previously |
| 2654 | // evaluated motion vectors. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2655 | if (skip_this_mv && is_single_pred) return true; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2656 | |
| 2657 | // For screen: for spatially flat blocks with non-zero motion, |
Marco Paniconi | a33c841 | 2023-09-29 14:25:46 -0700 | [diff] [blame] | 2658 | // skip newmv if the motion vector is (0, 0)-LAST, and color is not set. |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2659 | if (this_mode == NEWMV && cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN && |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2660 | cpi->svc.spatial_layer_id == 0 && rt_sf->source_metrics_sb_nonrd) { |
Marco Paniconi | a33c841 | 2023-09-29 14:25:46 -0700 | [diff] [blame] | 2661 | if (this_mv->as_int == 0 && ref_frame == LAST_FRAME && |
| 2662 | x->block_is_zero_sad == 0 && |
Marco Paniconi | 9a3b768 | 2023-07-10 10:33:13 -0700 | [diff] [blame] | 2663 | ((x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_U)] == 0 && |
| 2664 | x->color_sensitivity_sb[COLOR_SENS_IDX(AOM_PLANE_V)] == 0) || |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2665 | cpi->rc.high_source_sad) && |
| 2666 | x->source_variance == 0) |
| 2667 | return true; |
| 2668 | } |
| 2669 | |
| 2670 | mi->mode = this_mode; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2671 | mi->mv[0].as_int = this_mv->as_int; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2672 | mi->mv[1].as_int = 0; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2673 | if (!is_single_pred) |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2674 | mi->mv[1].as_int = search_state->frame_mv[this_mode][ref_frame2].as_int; |
| 2675 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2676 | // Set buffers to store predicted samples for reuse |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2677 | if (reuse_inter_pred) { |
| 2678 | if (!*this_mode_pred) { |
| 2679 | *this_mode_pred = &tmp_buffer[3]; |
| 2680 | } else { |
| 2681 | *this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)]; |
| 2682 | pd->dst.buf = (*this_mode_pred)->data; |
| 2683 | pd->dst.stride = bw; |
| 2684 | } |
| 2685 | } |
| 2686 | |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2687 | mi->motion_mode = SIMPLE_TRANSLATION; |
| 2688 | #if !CONFIG_REALTIME_ONLY |
| 2689 | if (cpi->oxcf.motion_mode_cfg.allow_warped_motion) { |
| 2690 | calc_num_proj_ref(cpi, x, mi); |
| 2691 | } |
| 2692 | #endif |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2693 | // set variance threshold for compound mode pruning |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2694 | if (rt_sf->prune_compoundmode_with_singlecompound_var && !is_single_pred && |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2695 | use_model_yrd_large) { |
| 2696 | const PREDICTION_MODE single_mode0 = compound_ref0_mode(this_mode); |
| 2697 | const PREDICTION_MODE single_mode1 = compound_ref1_mode(this_mode); |
| 2698 | var_threshold = |
| 2699 | AOMMIN(var_threshold, |
| 2700 | search_state->vars[INTER_OFFSET(single_mode0)][ref_frame]); |
| 2701 | var_threshold = |
| 2702 | AOMMIN(var_threshold, |
| 2703 | search_state->vars[INTER_OFFSET(single_mode1)][ref_frame2]); |
| 2704 | } |
| 2705 | |
| 2706 | // decide interpolation filter, build prediction signal, get sse |
| 2707 | const bool is_mv_subpel = |
| 2708 | (mi->mv[0].as_mv.row & 0x07) || (mi->mv[0].as_mv.col & 0x07); |
| 2709 | const bool enable_filt_search_this_mode = |
| 2710 | (filter_search_enabled_blk == 2) |
| 2711 | ? true |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2712 | : (filter_search_enabled_blk && !force_mv_inter_layer && |
| 2713 | is_single_pred && |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2714 | (ref_frame == LAST_FRAME || !x->nonrd_prune_ref_frame_search)); |
| 2715 | if (is_mv_subpel && enable_filt_search_this_mode) { |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2716 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2717 | aom_usec_timer_start(&x->ms_stat_nonrd.timer2); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2718 | #endif |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 2719 | search_filter_ref( |
| 2720 | cpi, x, &search_state->this_rdc, &inter_pred_params_sr, mi_row, mi_col, |
| 2721 | tmp_buffer, bsize, reuse_inter_pred, this_mode_pred, &this_early_term, |
| 2722 | &var, use_model_yrd_large, best_pickmode->best_sse, is_single_pred); |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2723 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2724 | aom_usec_timer_mark(&x->ms_stat_nonrd.timer2); |
| 2725 | x->ms_stat_nonrd.ifs_time[bsize][this_mode] += |
| 2726 | aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2727 | #endif |
| 2728 | #if !CONFIG_REALTIME_ONLY |
| 2729 | } else if (cpi->oxcf.motion_mode_cfg.allow_warped_motion && |
| 2730 | this_mode == NEWMV) { |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2731 | // Find the best motion mode when current mode is NEWMV |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2732 | search_motion_mode(cpi, x, &search_state->this_rdc, mi_row, mi_col, bsize, |
| 2733 | &this_early_term, use_model_yrd_large, &rate_mv, |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 2734 | best_pickmode->best_sse); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2735 | if (this_mode == NEWMV) { |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2736 | this_mv[0] = mi->mv[0]; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2737 | } |
| 2738 | #endif |
| 2739 | } else { |
| 2740 | mi->interp_filters = |
| 2741 | (filter_ref == SWITCHABLE) |
| 2742 | ? av1_broadcast_interp_filter(default_interp_filter) |
| 2743 | : av1_broadcast_interp_filter(filter_ref); |
| 2744 | if (force_mv_inter_layer) |
| 2745 | mi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR); |
| 2746 | |
| 2747 | // If it is sub-pel motion and cb_pred_filter_search is enabled, select |
| 2748 | // the pre-decided filter |
| 2749 | if (is_mv_subpel && cb_pred_filter_search) |
| 2750 | mi->interp_filters = av1_broadcast_interp_filter(filt_select); |
| 2751 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2752 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2753 | aom_usec_timer_start(&x->ms_stat_nonrd.timer2); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2754 | #endif |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2755 | if (is_single_pred) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2756 | SubpelParams subpel_params; |
| 2757 | // Initialize inter mode level params for single reference mode. |
| 2758 | init_inter_mode_params(&mi->mv[0].as_mv, &inter_pred_params_sr, |
| 2759 | &subpel_params, xd->block_ref_scale_factors[0], |
| 2760 | pd->pre->width, pd->pre->height); |
| 2761 | av1_enc_build_inter_predictor_y_nonrd(xd, &inter_pred_params_sr, |
| 2762 | &subpel_params); |
| 2763 | } else { |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 2764 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 2765 | AOM_PLANE_Y, AOM_PLANE_Y); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | if (use_model_yrd_large) { |
| 2769 | model_skip_for_sb_y_large(cpi, bsize, mi_row, mi_col, x, xd, |
| 2770 | &search_state->this_rdc, &this_early_term, 0, |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 2771 | best_pickmode->best_sse, &var, var_threshold); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2772 | } else { |
| 2773 | model_rd_for_sb_y(cpi, bsize, x, xd, &search_state->this_rdc, &var, 0, |
| 2774 | &this_early_term); |
| 2775 | } |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2776 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2777 | aom_usec_timer_mark(&x->ms_stat_nonrd.timer2); |
| 2778 | x->ms_stat_nonrd.model_rd_time[bsize][this_mode] += |
| 2779 | aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2780 | #endif |
| 2781 | } |
| 2782 | |
| 2783 | // update variance for single mode |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2784 | if (is_single_pred) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2785 | search_state->vars[INTER_OFFSET(this_mode)][ref_frame] = var; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2786 | if (this_mv->as_int == 0) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2787 | search_state->vars[INTER_OFFSET(GLOBALMV)][ref_frame] = var; |
| 2788 | } |
| 2789 | } |
| 2790 | // prune compound mode based on single mode var threshold |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2791 | if (!is_single_pred && var > var_threshold) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2792 | if (reuse_inter_pred) free_pred_buffer(*this_mode_pred); |
| 2793 | return true; |
| 2794 | } |
| 2795 | |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2796 | if (ref_frame == LAST_FRAME && this_mv->as_int == 0) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2797 | *sse_zeromv_norm = (unsigned int)(search_state->this_rdc.sse >> |
| 2798 | (b_width_log2_lookup[bsize] + |
| 2799 | b_height_log2_lookup[bsize])); |
| 2800 | } |
| 2801 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2802 | // Perform early termination based on sse. |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2803 | if (rt_sf->sse_early_term_inter_search && |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 2804 | early_term_inter_search_with_sse(rt_sf->sse_early_term_inter_search, |
| 2805 | bsize, search_state->this_rdc.sse, |
| 2806 | best_pickmode->best_sse, this_mode)) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2807 | if (reuse_inter_pred) free_pred_buffer(*this_mode_pred); |
| 2808 | return true; |
| 2809 | } |
| 2810 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2811 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2812 | x->ms_stat_nonrd.num_nonskipped_searches[bsize][this_mode]++; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2813 | #endif |
| 2814 | |
| 2815 | const int skip_ctx = av1_get_skip_txfm_context(xd); |
| 2816 | const int skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][1]; |
| 2817 | const int no_skip_txfm_cost = mode_costs->skip_txfm_cost[skip_ctx][0]; |
| 2818 | const int64_t sse_y = search_state->this_rdc.sse; |
| 2819 | |
| 2820 | if (this_early_term) { |
| 2821 | search_state->this_rdc.skip_txfm = 1; |
| 2822 | search_state->this_rdc.rate = skip_txfm_cost; |
| 2823 | search_state->this_rdc.dist = search_state->this_rdc.sse << 4; |
| 2824 | } else { |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2825 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2826 | aom_usec_timer_start(&x->ms_stat_nonrd.timer2); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2827 | #endif |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2828 | // Calculates RD Cost using Hadamard transform. |
Marco Paniconi | de6f57b | 2023-03-19 23:42:40 -0700 | [diff] [blame] | 2829 | av1_block_yrd(x, &search_state->this_rdc, &is_skippable, bsize, |
| 2830 | mi->tx_size); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2831 | if (search_state->this_rdc.skip_txfm || |
| 2832 | RDCOST(x->rdmult, search_state->this_rdc.rate, |
| 2833 | search_state->this_rdc.dist) >= |
| 2834 | RDCOST(x->rdmult, 0, search_state->this_rdc.sse)) { |
| 2835 | if (!search_state->this_rdc.skip_txfm) { |
| 2836 | // Need to store "real" rdc for possible future use if UV rdc |
| 2837 | // disallows tx skip |
| 2838 | nonskip_rdc = search_state->this_rdc; |
| 2839 | nonskip_rdc.rate += no_skip_txfm_cost; |
| 2840 | } |
| 2841 | search_state->this_rdc.rate = skip_txfm_cost; |
| 2842 | search_state->this_rdc.skip_txfm = 1; |
| 2843 | search_state->this_rdc.dist = search_state->this_rdc.sse; |
| 2844 | } else { |
| 2845 | search_state->this_rdc.rate += no_skip_txfm_cost; |
| 2846 | } |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2847 | |
| 2848 | // Populate predicted sample for chroma planes based on color sensitivity. |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 2849 | if ((x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] || |
| 2850 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2851 | RD_STATS rdc_uv; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 2852 | const BLOCK_SIZE uv_bsize = |
| 2853 | get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x, |
| 2854 | xd->plane[AOM_PLANE_U].subsampling_y); |
| 2855 | if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2856 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 2857 | AOM_PLANE_U, AOM_PLANE_U); |
| 2858 | } |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 2859 | if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2860 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 2861 | AOM_PLANE_V, AOM_PLANE_V); |
| 2862 | } |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2863 | // Compute sse for chroma planes. |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 2864 | const int64_t sse_uv = av1_model_rd_for_sb_uv( |
| 2865 | cpi, uv_bsize, x, xd, &rdc_uv, AOM_PLANE_U, AOM_PLANE_V); |
Marco Paniconi | f289cc9 | 2023-06-27 21:31:04 -0700 | [diff] [blame] | 2866 | if (rdc_uv.dist < x->min_dist_inter_uv) |
| 2867 | x->min_dist_inter_uv = rdc_uv.dist; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2868 | search_state->this_rdc.sse += sse_uv; |
| 2869 | // Restore Y rdc if UV rdc disallows txfm skip |
| 2870 | if (search_state->this_rdc.skip_txfm && !rdc_uv.skip_txfm && |
| 2871 | nonskip_rdc.rate != INT_MAX) |
| 2872 | search_state->this_rdc = nonskip_rdc; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2873 | if (is_single_pred) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2874 | search_state->uv_dist[INTER_OFFSET(this_mode)][ref_frame] = rdc_uv.dist; |
| 2875 | } |
| 2876 | search_state->this_rdc.rate += rdc_uv.rate; |
| 2877 | search_state->this_rdc.dist += rdc_uv.dist; |
| 2878 | search_state->this_rdc.skip_txfm = |
| 2879 | search_state->this_rdc.skip_txfm && rdc_uv.skip_txfm; |
| 2880 | } |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2881 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2882 | aom_usec_timer_mark(&x->ms_stat_nonrd.timer2); |
| 2883 | x->ms_stat_nonrd.txfm_time[bsize][this_mode] += |
| 2884 | aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer2); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2885 | #endif |
| 2886 | } |
| 2887 | |
| 2888 | this_best_mode = this_mode; |
| 2889 | // TODO(kyslov) account for UV prediction cost |
| 2890 | search_state->this_rdc.rate += rate_mv; |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2891 | if (!is_single_pred) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2892 | const int16_t mode_ctx = |
| 2893 | av1_mode_context_analyzer(mbmi_ext->mode_context, mi->ref_frame); |
| 2894 | search_state->this_rdc.rate += cost_mv_ref(mode_costs, this_mode, mode_ctx); |
| 2895 | } else { |
| 2896 | // If the current mode has zeromv but is not GLOBALMV, compare the rate |
| 2897 | // cost. If GLOBALMV is cheaper, use GLOBALMV instead. |
| 2898 | if (this_mode != GLOBALMV && |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2899 | this_mv->as_int == search_state->frame_mv[GLOBALMV][ref_frame].as_int) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2900 | if (is_globalmv_better(this_mode, ref_frame, rate_mv, mode_costs, |
| 2901 | search_state->single_inter_mode_costs, mbmi_ext)) { |
| 2902 | this_best_mode = GLOBALMV; |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | search_state->this_rdc.rate += |
| 2907 | search_state |
| 2908 | ->single_inter_mode_costs[INTER_OFFSET(this_best_mode)][ref_frame]; |
| 2909 | } |
| 2910 | |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2911 | if (is_single_pred && this_mv->as_int == 0 && var < UINT_MAX) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2912 | search_state->vars[INTER_OFFSET(GLOBALMV)][ref_frame] = var; |
| 2913 | } |
| 2914 | |
| 2915 | search_state->this_rdc.rate += search_state->ref_costs_single[ref_frame]; |
| 2916 | |
| 2917 | search_state->this_rdc.rdcost = RDCOST(x->rdmult, search_state->this_rdc.rate, |
| 2918 | search_state->this_rdc.dist); |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 2919 | if (cpi->oxcf.rc_cfg.mode == AOM_CBR && is_single_pred) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2920 | newmv_diff_bias(xd, this_best_mode, &search_state->this_rdc, bsize, |
| 2921 | search_state->frame_mv[this_best_mode][ref_frame].as_mv.row, |
| 2922 | search_state->frame_mv[this_best_mode][ref_frame].as_mv.col, |
| 2923 | cpi->speed, x->source_variance, x->content_state_sb); |
| 2924 | } |
| 2925 | |
| 2926 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 2927 | if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc_pickmode && |
| 2928 | cpi->denoiser.denoising_level > kDenLowLow) { |
| 2929 | av1_denoiser_update_frame_stats(mi, sse_y, this_mode, ctx); |
| 2930 | // Keep track of zero_last cost. |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2931 | if (ref_frame == LAST_FRAME && this_mv->as_int == 0) |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2932 | *zero_last_cost_orig = search_state->this_rdc.rdcost; |
| 2933 | } |
| 2934 | #else |
| 2935 | (void)(sse_y); |
| 2936 | #endif |
| 2937 | |
| 2938 | search_state->mode_checked[this_mode][ref_frame] = 1; |
| 2939 | search_state->mode_checked[this_best_mode][ref_frame] = 1; |
| 2940 | |
| 2941 | if (*check_globalmv) { |
| 2942 | int32_t abs_mv = |
| 2943 | abs(search_state->frame_mv[this_best_mode][ref_frame].as_mv.row) + |
| 2944 | abs(search_state->frame_mv[this_best_mode][ref_frame].as_mv.col); |
| 2945 | // Early exit check: if the magnitude of this_best_mode's mv is small |
| 2946 | // enough, we skip GLOBALMV check in the next loop iteration. |
| 2947 | if (abs_mv < 2) { |
| 2948 | *check_globalmv = false; |
| 2949 | } |
| 2950 | } |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 2951 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 2952 | aom_usec_timer_mark(&x->ms_stat_nonrd.timer1); |
| 2953 | x->ms_stat_nonrd.nonskipped_search_times[bsize][this_mode] += |
| 2954 | aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer1); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2955 | #endif |
| 2956 | |
Marco Paniconi | 5a4ea09 | 2023-08-09 14:36:15 -0700 | [diff] [blame] | 2957 | if (x->sb_me_block && ref_frame == LAST_FRAME && |
| 2958 | search_state->frame_mv[this_best_mode][ref_frame].as_int == |
| 2959 | x->sb_me_mv.as_int) |
| 2960 | *sb_me_has_been_tested = 1; |
| 2961 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 2962 | // Copy best mode params to search state |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2963 | if (search_state->this_rdc.rdcost < search_state->best_rdc.rdcost) { |
| 2964 | search_state->best_rdc = search_state->this_rdc; |
| 2965 | *best_early_term = this_early_term; |
Anupam Pandey | 7317937 | 2022-12-21 16:07:14 +0530 | [diff] [blame] | 2966 | update_search_state_nonrd(search_state, mi, txfm_info, &nonskip_rdc, ctx, |
| 2967 | this_best_mode, sse_y); |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2968 | |
| 2969 | // This is needed for the compound modes. |
| 2970 | search_state->frame_mv_best[this_best_mode][ref_frame].as_int = |
| 2971 | search_state->frame_mv[this_best_mode][ref_frame].as_int; |
| 2972 | if (ref_frame2 > NONE_FRAME) { |
| 2973 | search_state->frame_mv_best[this_best_mode][ref_frame2].as_int = |
| 2974 | search_state->frame_mv[this_best_mode][ref_frame2].as_int; |
| 2975 | } |
| 2976 | |
| 2977 | if (reuse_inter_pred) { |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 2978 | free_pred_buffer(best_pickmode->best_pred); |
| 2979 | best_pickmode->best_pred = *this_mode_pred; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2980 | } |
| 2981 | } else { |
| 2982 | if (reuse_inter_pred) free_pred_buffer(*this_mode_pred); |
| 2983 | } |
| 2984 | |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 2985 | if (*best_early_term && (idx > 0 || rt_sf->nonrd_aggressive_skip)) { |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2986 | txfm_info->skip_txfm = 1; |
Marco Paniconi | 5a4ea09 | 2023-08-09 14:36:15 -0700 | [diff] [blame] | 2987 | if (!x->sb_me_block || *sb_me_has_been_tested) return false; |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 2988 | } |
| 2989 | return true; |
| 2990 | } |
| 2991 | |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 2992 | // Function to perform screen content mode evaluation for non-rd |
| 2993 | static AOM_FORCE_INLINE void handle_screen_content_mode_nonrd( |
| 2994 | AV1_COMP *cpi, MACROBLOCK *x, InterModeSearchStateNonrd *search_state, |
| 2995 | PRED_BUFFER *this_mode_pred, PICK_MODE_CONTEXT *ctx, |
| 2996 | PRED_BUFFER *tmp_buffer, struct buf_2d *orig_dst, int skip_idtx_palette, |
Marco Paniconi | 92960a8 | 2023-05-23 15:32:16 -0700 | [diff] [blame] | 2997 | int try_palette, BLOCK_SIZE bsize, int reuse_inter_pred, int mi_col, |
| 2998 | int mi_row) { |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 2999 | AV1_COMMON *const cm = &cpi->common; |
| 3000 | const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf; |
| 3001 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3002 | MB_MODE_INFO *const mi = xd->mi[0]; |
| 3003 | struct macroblockd_plane *const pd = &xd->plane[0]; |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3004 | const int bw = block_size_wide[bsize]; |
Marco Paniconi | fffd4e8 | 2023-01-24 00:32:07 -0800 | [diff] [blame] | 3005 | const int bh = block_size_high[bsize]; |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3006 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3007 | BEST_PICKMODE *const best_pickmode = &search_state->best_pickmode; |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3008 | |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3009 | // TODO(marpan): Only allow for 8 bit-depth for now, re-enable for 10/12 bit |
| 3010 | // when issue 3359 is fixed. |
Marco Paniconi | 62732c8 | 2024-04-30 15:24:49 -0700 | [diff] [blame] | 3011 | if (cm->seq_params->bit_depth == 8 && rt_sf->use_idtx_nonrd && |
| 3012 | !skip_idtx_palette && !cpi->oxcf.txfm_cfg.use_inter_dct_only && |
| 3013 | !x->force_zeromv_skip_for_blk && |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3014 | is_inter_mode(best_pickmode->best_mode) && |
Marco Paniconi | 92dcb19 | 2023-03-21 09:46:02 -0700 | [diff] [blame] | 3015 | best_pickmode->best_pred != NULL && |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3016 | (!rt_sf->prune_idtx_nonrd || |
| 3017 | (rt_sf->prune_idtx_nonrd && bsize <= BLOCK_32X32 && |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3018 | best_pickmode->best_mode_skip_txfm != 1 && x->source_variance > 200))) { |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3019 | RD_STATS idtx_rdc; |
| 3020 | av1_init_rd_stats(&idtx_rdc); |
| 3021 | int is_skippable; |
| 3022 | this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)]; |
| 3023 | pd->dst.buf = this_mode_pred->data; |
| 3024 | pd->dst.stride = bw; |
Marco Paniconi | 92dcb19 | 2023-03-21 09:46:02 -0700 | [diff] [blame] | 3025 | const PRED_BUFFER *const best_pred = best_pickmode->best_pred; |
| 3026 | av1_block_yrd_idtx(x, best_pred->data, best_pred->stride, &idtx_rdc, |
| 3027 | &is_skippable, bsize, mi->tx_size); |
Marco Paniconi | ead4fc3 | 2023-06-05 11:36:52 -0700 | [diff] [blame] | 3028 | int64_t idx_rdcost_y = RDCOST(x->rdmult, idtx_rdc.rate, idtx_rdc.dist); |
| 3029 | int allow_idtx = 1; |
Marco Paniconi | 92960a8 | 2023-05-23 15:32:16 -0700 | [diff] [blame] | 3030 | // Incorporate color into rd cost. |
| 3031 | if ((x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] || |
| 3032 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) { |
| 3033 | RD_STATS rdc_uv; |
| 3034 | const BLOCK_SIZE uv_bsize = |
| 3035 | get_plane_block_size(bsize, xd->plane[AOM_PLANE_U].subsampling_x, |
| 3036 | xd->plane[AOM_PLANE_U].subsampling_y); |
| 3037 | if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)]) { |
| 3038 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 3039 | AOM_PLANE_U, AOM_PLANE_U); |
| 3040 | } |
| 3041 | if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) { |
| 3042 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 3043 | AOM_PLANE_V, AOM_PLANE_V); |
| 3044 | } |
| 3045 | av1_model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &rdc_uv, AOM_PLANE_U, |
| 3046 | AOM_PLANE_V); |
Marco Paniconi | f289cc9 | 2023-06-27 21:31:04 -0700 | [diff] [blame] | 3047 | if (rdc_uv.dist < x->min_dist_inter_uv) |
| 3048 | x->min_dist_inter_uv = rdc_uv.dist; |
Marco Paniconi | 92960a8 | 2023-05-23 15:32:16 -0700 | [diff] [blame] | 3049 | idtx_rdc.rate += rdc_uv.rate; |
| 3050 | idtx_rdc.dist += rdc_uv.dist; |
| 3051 | idtx_rdc.skip_txfm = idtx_rdc.skip_txfm && rdc_uv.skip_txfm; |
Marco Paniconi | ead4fc3 | 2023-06-05 11:36:52 -0700 | [diff] [blame] | 3052 | if (idx_rdcost_y == 0 && rdc_uv.dist > 0 && x->source_variance < 3000 && |
| 3053 | x->content_state_sb.source_sad_nonrd > kMedSad) |
| 3054 | allow_idtx = 0; |
Marco Paniconi | 92960a8 | 2023-05-23 15:32:16 -0700 | [diff] [blame] | 3055 | } |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3056 | int64_t idx_rdcost = RDCOST(x->rdmult, idtx_rdc.rate, idtx_rdc.dist); |
Marco Paniconi | ead4fc3 | 2023-06-05 11:36:52 -0700 | [diff] [blame] | 3057 | if (allow_idtx && idx_rdcost < search_state->best_rdc.rdcost) { |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3058 | best_pickmode->tx_type = IDTX; |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3059 | search_state->best_rdc.rdcost = idx_rdcost; |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3060 | best_pickmode->best_mode_skip_txfm = idtx_rdc.skip_txfm; |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3061 | if (!idtx_rdc.skip_txfm) { |
Marco Paniconi | d77e40b | 2023-05-15 14:02:06 -0700 | [diff] [blame] | 3062 | memcpy(ctx->blk_skip, txfm_info->blk_skip, |
Marco Paniconi | de6f57b | 2023-03-19 23:42:40 -0700 | [diff] [blame] | 3063 | sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk); |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3064 | } |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3065 | xd->tx_type_map[0] = best_pickmode->tx_type; |
| 3066 | memset(ctx->tx_type_map, best_pickmode->tx_type, ctx->num_4x4_blk); |
| 3067 | memset(xd->tx_type_map, best_pickmode->tx_type, ctx->num_4x4_blk); |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3068 | } |
| 3069 | pd->dst = *orig_dst; |
| 3070 | } |
| 3071 | |
| 3072 | if (!try_palette) return; |
| 3073 | const unsigned int intra_ref_frame_cost = |
| 3074 | search_state->ref_costs_single[INTRA_FRAME]; |
| 3075 | |
Marco Paniconi | fffd4e8 | 2023-01-24 00:32:07 -0800 | [diff] [blame] | 3076 | if (!is_mode_intra(best_pickmode->best_mode)) { |
| 3077 | PRED_BUFFER *const best_pred = best_pickmode->best_pred; |
| 3078 | if (reuse_inter_pred && best_pred != NULL) { |
| 3079 | if (best_pred->data == orig_dst->buf) { |
| 3080 | this_mode_pred = &tmp_buffer[get_pred_buffer(tmp_buffer, 3)]; |
| 3081 | aom_convolve_copy(best_pred->data, best_pred->stride, |
| 3082 | this_mode_pred->data, this_mode_pred->stride, bw, bh); |
| 3083 | best_pickmode->best_pred = this_mode_pred; |
| 3084 | } |
| 3085 | } |
| 3086 | pd->dst = *orig_dst; |
| 3087 | } |
Anupam Pandey | bce4e9a | 2022-12-22 14:45:05 +0530 | [diff] [blame] | 3088 | // Search palette mode for Luma plane in inter frame. |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3089 | av1_search_palette_mode_luma(cpi, x, bsize, intra_ref_frame_cost, ctx, |
| 3090 | &search_state->this_rdc, |
| 3091 | search_state->best_rdc.rdcost); |
Anupam Pandey | bce4e9a | 2022-12-22 14:45:05 +0530 | [diff] [blame] | 3092 | // Update best mode data in search_state |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3093 | if (search_state->this_rdc.rdcost < search_state->best_rdc.rdcost) { |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3094 | best_pickmode->pmi = mi->palette_mode_info; |
| 3095 | best_pickmode->best_mode = DC_PRED; |
Marco Paniconi | fffd4e8 | 2023-01-24 00:32:07 -0800 | [diff] [blame] | 3096 | mi->mv[0].as_int = INVALID_MV; |
| 3097 | mi->mv[1].as_int = INVALID_MV; |
| 3098 | best_pickmode->best_ref_frame = INTRA_FRAME; |
| 3099 | best_pickmode->best_second_ref_frame = NONE; |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3100 | search_state->best_rdc.rate = search_state->this_rdc.rate; |
| 3101 | search_state->best_rdc.dist = search_state->this_rdc.dist; |
| 3102 | search_state->best_rdc.rdcost = search_state->this_rdc.rdcost; |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3103 | best_pickmode->best_mode_skip_txfm = search_state->this_rdc.skip_txfm; |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3104 | // Keep the skip_txfm off if the color_sensitivity is set. |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 3105 | if (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] || |
| 3106 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3107 | search_state->this_rdc.skip_txfm = 0; |
| 3108 | if (!search_state->this_rdc.skip_txfm) { |
| 3109 | memcpy(ctx->blk_skip, txfm_info->blk_skip, |
| 3110 | sizeof(txfm_info->blk_skip[0]) * ctx->num_4x4_blk); |
| 3111 | } |
| 3112 | if (xd->tx_type_map[0] != DCT_DCT) |
| 3113 | av1_copy_array(ctx->tx_type_map, xd->tx_type_map, ctx->num_4x4_blk); |
| 3114 | } |
| 3115 | } |
| 3116 | |
Marco Paniconi | bdada5c | 2024-05-22 14:30:55 -0700 | [diff] [blame] | 3117 | static AOM_INLINE bool enable_palette(AV1_COMP *cpi, bool is_mode_intra, |
| 3118 | BLOCK_SIZE bsize, |
| 3119 | unsigned int source_variance, |
| 3120 | int force_zeromv_skip, |
| 3121 | int skip_idtx_palette, |
| 3122 | int force_palette_test) { |
| 3123 | if (!cpi->oxcf.tool_cfg.enable_palette) return false; |
| 3124 | if (!av1_allow_palette(cpi->common.features.allow_screen_content_tools, |
| 3125 | bsize)) { |
| 3126 | return false; |
| 3127 | } |
| 3128 | if (skip_idtx_palette) return false; |
| 3129 | |
| 3130 | if (cpi->sf.rt_sf.prune_palette_search_nonrd > 1 && |
| 3131 | ((cpi->rc.high_source_sad && cpi->ppi->rtc_ref.non_reference_frame) || |
| 3132 | bsize > BLOCK_16X16)) { |
| 3133 | return false; |
| 3134 | } |
| 3135 | |
| 3136 | if ((is_mode_intra || force_palette_test) && source_variance > 0 && |
| 3137 | !force_zeromv_skip && |
| 3138 | (cpi->rc.high_source_sad || source_variance > 300)) { |
| 3139 | return true; |
| 3140 | } else { |
| 3141 | return false; |
| 3142 | } |
| 3143 | } |
| 3144 | |
Anupam Pandey | b7d3676 | 2022-12-19 12:03:01 +0530 | [diff] [blame] | 3145 | /*!\brief AV1 inter mode selection based on Non-RD optimized model. |
| 3146 | * |
| 3147 | * \ingroup nonrd_mode_search |
| 3148 | * \callgraph |
| 3149 | * Top level function for Non-RD optimized inter mode selection. |
| 3150 | * This finction will loop over subset of inter modes and select the best one |
| 3151 | * based on calculated modelled RD cost. While making decisions which modes to |
| 3152 | * check, this function applies heuristics based on previously checked modes, |
| 3153 | * block residual variance, block size, and other factors to prune certain |
| 3154 | * modes and reference frames. Currently only single reference frame modes |
| 3155 | * are checked. Additional heuristics are applied to decide if intra modes |
| 3156 | * need to be checked. |
| 3157 | * * |
| 3158 | * \param[in] cpi Top-level encoder structure |
| 3159 | * \param[in] tile_data Pointer to struct holding adaptive |
| 3160 | data/contexts/models for the tile during |
| 3161 | encoding |
| 3162 | * \param[in] x Pointer to structure holding all the data for |
| 3163 | the current macroblock |
| 3164 | * \param[in] rd_cost Struct to keep track of the RD information |
| 3165 | * \param[in] bsize Current block size |
| 3166 | * \param[in] ctx Structure to hold snapshot of coding context |
| 3167 | during the mode picking process |
| 3168 | * |
| 3169 | * \remark Nothing is returned. Instead, the MB_MODE_INFO struct inside x |
| 3170 | * is modified to store information about the best mode computed |
| 3171 | * in this function. The rd_cost struct is also updated with the RD stats |
| 3172 | * corresponding to the best mode found. |
| 3173 | */ |
Fyodor Kyslov | 915b9b8 | 2019-11-11 11:53:03 -0800 | [diff] [blame] | 3174 | 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] | 3175 | MACROBLOCK *x, RD_STATS *rd_cost, |
Marco Paniconi | 11067db | 2020-05-18 11:11:42 -0700 | [diff] [blame] | 3176 | BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3177 | AV1_COMMON *const cm = &cpi->common; |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 3178 | SVC *const svc = &cpi->svc; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3179 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3180 | MB_MODE_INFO *const mi = xd->mi[0]; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 3181 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y]; |
chiyotsai | 901f406 | 2022-07-06 14:57:53 -0700 | [diff] [blame] | 3182 | const MB_MODE_INFO_EXT *const mbmi_ext = &x->mbmi_ext; |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 3183 | MV_REFERENCE_FRAME ref_frame, ref_frame2; |
Fyodor Kyslov | fa2d76f | 2020-04-24 14:03:50 -0700 | [diff] [blame] | 3184 | const unsigned char segment_id = mi->segment_id; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3185 | int best_early_term = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3186 | int force_skip_low_temp_var = 0; |
Fyodor Kyslov | 84abaea | 2019-06-25 14:44:14 -0700 | [diff] [blame] | 3187 | unsigned int sse_zeromv_norm = UINT_MAX; |
linzhen | e4954c2 | 2022-09-20 18:27:52 +0000 | [diff] [blame] | 3188 | const int num_inter_modes = NUM_INTER_MODES; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 3189 | const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf; |
| 3190 | bool check_globalmv = rt_sf->check_globalmv_on_single_ref; |
Cheng Chen | 9463449 | 2022-11-16 17:37:52 -0800 | [diff] [blame] | 3191 | PRED_BUFFER tmp_buffer[4]; |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 3192 | DECLARE_ALIGNED(16, uint8_t, pred_buf[MAX_MB_PLANE * MAX_SB_SQUARE]); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3193 | PRED_BUFFER *this_mode_pred = NULL; |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 3194 | const int reuse_inter_pred = |
| 3195 | rt_sf->reuse_inter_pred_nonrd && cm->seq_params->bit_depth == AOM_BITS_8; |
Anupam Pandey | 7dfb6d5 | 2022-12-05 14:19:10 +0530 | [diff] [blame] | 3196 | InterModeSearchStateNonrd search_state; |
| 3197 | av1_zero(search_state.use_ref_frame_mask); |
Marco Paniconi | a1f2968 | 2023-09-17 20:09:51 -0700 | [diff] [blame] | 3198 | av1_zero(search_state.use_scaled_ref_frame); |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3199 | BEST_PICKMODE *const best_pickmode = &search_state.best_pickmode; |
Anupam Pandey | 05c81f8 | 2022-12-21 12:58:34 +0530 | [diff] [blame] | 3200 | (void)tile_data; |
Fyodor Kyslov | b0f0fb1 | 2020-11-10 19:08:36 -0800 | [diff] [blame] | 3201 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3202 | const int bh = block_size_high[bsize]; |
| 3203 | const int bw = block_size_wide[bsize]; |
| 3204 | const int pixels_in_block = bh * bw; |
| 3205 | struct buf_2d orig_dst = pd->dst; |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 3206 | const TxfmSearchParams *txfm_params = &x->txfm_search_params; |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 3207 | TxfmSearchInfo *txfm_info = &x->txfm_search_info; |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 3208 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 3209 | // Mode statistics can be collected only when num_workers is 1 |
| 3210 | assert(cpi->mt_info.num_workers <= 1); |
| 3211 | aom_usec_timer_start(&x->ms_stat_nonrd.bsize_timer); |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 3212 | #endif |
Marco Paniconi | b3565a2 | 2020-03-05 15:29:08 -0800 | [diff] [blame] | 3213 | int64_t thresh_sad_pred = INT64_MAX; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 3214 | const int mi_row = xd->mi_row; |
| 3215 | const int mi_col = xd->mi_col; |
Anupam Pandey | cf5a52e | 2022-12-14 18:25:44 +0530 | [diff] [blame] | 3216 | int_mv svc_mv = { .as_int = 0 }; |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 3217 | int force_mv_inter_layer = 0; |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 3218 | bool comp_use_zero_zeromv_only = 0; |
| 3219 | int tot_num_comp_modes = NUM_COMP_INTER_MODES_RT; |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 3220 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 3221 | const int denoise_recheck_zeromv = 1; |
| 3222 | AV1_PICKMODE_CTX_DEN ctx_den; |
| 3223 | int64_t zero_last_cost_orig = INT64_MAX; |
| 3224 | int denoise_svc_pickmode = 1; |
Yunqing Wang | a6276ca | 2021-09-17 12:01:24 -0700 | [diff] [blame] | 3225 | const int resize_pending = is_frame_resize_pending(cpi); |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 3226 | #endif |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 3227 | const ModeCosts *mode_costs = &x->mode_costs; |
Marco Paniconi | a1f2968 | 2023-09-17 20:09:51 -0700 | [diff] [blame] | 3228 | struct scale_factors sf_no_scale; |
| 3229 | av1_setup_scale_factors_for_frame(&sf_no_scale, cm->width, cm->height, |
| 3230 | cm->width, cm->height); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3231 | if (reuse_inter_pred) { |
Anupam Pandey | cbe4eb4 | 2022-12-21 12:44:47 +0530 | [diff] [blame] | 3232 | for (int buf_idx = 0; buf_idx < 3; buf_idx++) { |
| 3233 | tmp_buffer[buf_idx].data = &pred_buf[pixels_in_block * buf_idx]; |
| 3234 | tmp_buffer[buf_idx].stride = bw; |
| 3235 | tmp_buffer[buf_idx].in_use = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3236 | } |
Cheng Chen | 9463449 | 2022-11-16 17:37:52 -0800 | [diff] [blame] | 3237 | tmp_buffer[3].data = pd->dst.buf; |
| 3238 | tmp_buffer[3].stride = pd->dst.stride; |
| 3239 | tmp_buffer[3].in_use = 0; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 3242 | const int gf_temporal_ref = is_same_gf_and_last_scale(cm); |
| 3243 | |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 3244 | // If the lower spatial layer uses an averaging filter for downsampling |
| 3245 | // (phase = 8), the target decimated pixel is shifted by (1/2, 1/2) relative |
| 3246 | // to source, so use subpel motion vector to compensate. The nonzero motion |
| 3247 | // is half pixel shifted to left and top, so (-4, -4). This has more effect |
James Zern | 286e07b | 2022-09-29 15:44:58 -0700 | [diff] [blame] | 3248 | // on higher resolutions, so condition it on that for now. |
Marco Paniconi | 0d59418 | 2023-09-27 12:17:52 -0700 | [diff] [blame] | 3249 | // Exclude quality layers, which have the same resolution and hence no shift. |
Tarundeep Singh | 15eb4de | 2021-04-21 15:53:10 +0530 | [diff] [blame] | 3250 | if (cpi->ppi->use_svc && svc->spatial_layer_id > 0 && |
Marco Paniconi | 0d59418 | 2023-09-27 12:17:52 -0700 | [diff] [blame] | 3251 | !svc->has_lower_quality_layer && |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 3252 | svc->downsample_filter_phase[svc->spatial_layer_id - 1] == 8 && |
| 3253 | cm->width * cm->height > 640 * 480) { |
Anupam Pandey | cf5a52e | 2022-12-14 18:25:44 +0530 | [diff] [blame] | 3254 | svc_mv.as_mv.row = -4; |
| 3255 | svc_mv.as_mv.col = -4; |
Marco Paniconi | 84cb8b3 | 2021-01-19 14:02:19 -0800 | [diff] [blame] | 3256 | } |
| 3257 | |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 3258 | // Setup parameters used for inter mode evaluation. |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 3259 | set_params_nonrd_pick_inter_mode(cpi, x, &search_state, rd_cost, |
| 3260 | &force_skip_low_temp_var, mi_row, mi_col, |
| 3261 | gf_temporal_ref, segment_id, bsize |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 3262 | #if CONFIG_AV1_TEMPORAL_DENOISING |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 3263 | , |
| 3264 | ctx, denoise_svc_pickmode |
Anupam Pandey | 1e62cfb | 2022-12-06 11:22:47 +0530 | [diff] [blame] | 3265 | #endif |
| 3266 | ); |
Marco Paniconi | d456b6f | 2021-10-26 09:32:03 -0700 | [diff] [blame] | 3267 | |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 3268 | if (rt_sf->use_comp_ref_nonrd && is_comp_ref_allowed(bsize)) { |
Marco Paniconi | 857d12f | 2022-07-26 09:41:37 -0700 | [diff] [blame] | 3269 | // Only search compound if bsize \gt BLOCK_16X16. |
| 3270 | if (bsize > BLOCK_16X16) { |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 3271 | comp_use_zero_zeromv_only = rt_sf->check_only_zero_zeromv_on_large_blocks; |
Marco Paniconi | 857d12f | 2022-07-26 09:41:37 -0700 | [diff] [blame] | 3272 | } else { |
| 3273 | tot_num_comp_modes = 0; |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 3274 | } |
Marco Paniconi | f190c95 | 2021-08-02 23:07:46 -0700 | [diff] [blame] | 3275 | } else { |
| 3276 | tot_num_comp_modes = 0; |
Marco Paniconi | 49f3934 | 2021-07-22 12:06:45 -0700 | [diff] [blame] | 3277 | } |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 3278 | |
Marco Paniconi | e26457d | 2022-10-11 15:42:17 -0700 | [diff] [blame] | 3279 | if (x->pred_mv_sad[LAST_FRAME] != INT_MAX) { |
| 3280 | thresh_sad_pred = ((int64_t)x->pred_mv_sad[LAST_FRAME]) << 1; |
| 3281 | // Increase threshold for less aggressive pruning. |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 3282 | if (rt_sf->nonrd_prune_ref_frame_search == 1) |
Marco Paniconi | e26457d | 2022-10-11 15:42:17 -0700 | [diff] [blame] | 3283 | thresh_sad_pred += (x->pred_mv_sad[LAST_FRAME] >> 2); |
| 3284 | } |
Marco Paniconi | b3565a2 | 2020-03-05 15:29:08 -0800 | [diff] [blame] | 3285 | |
Ranjit Kumar Tulabandu | eaf39d3 | 2022-11-16 17:40:24 +0530 | [diff] [blame] | 3286 | const int use_model_yrd_large = get_model_rd_flag(cpi, xd, bsize); |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 3287 | |
Neeraj Gadgil | 1513bf0 | 2022-10-28 23:43:22 +0530 | [diff] [blame] | 3288 | // decide block-level interp filter search flags: |
| 3289 | // filter_search_enabled_blk: |
| 3290 | // 0: disabled |
| 3291 | // 1: filter search depends on mode properties |
| 3292 | // 2: filter search forced since prediction is unreliable |
| 3293 | // cb_pred_filter_search 0: disabled cb prediction |
| 3294 | InterpFilter filt_select = EIGHTTAP_REGULAR; |
| 3295 | const int cb_pred_filter_search = |
| 3296 | x->content_state_sb.source_sad_nonrd > kVeryLowSad |
| 3297 | ? cpi->sf.interp_sf.cb_pred_filter_search |
| 3298 | : 0; |
| 3299 | const int filter_search_enabled_blk = |
| 3300 | is_filter_search_enabled_blk(cpi, x, mi_row, mi_col, bsize, segment_id, |
| 3301 | cb_pred_filter_search, &filt_select); |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 3302 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 3303 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 3304 | x->ms_stat_nonrd.num_blocks[bsize]++; |
Fyodor Kyslov | 45ec956 | 2019-09-26 13:02:24 -0700 | [diff] [blame] | 3305 | #endif |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 3306 | init_mbmi_nonrd(mi, DC_PRED, NONE_FRAME, NONE_FRAME, cm); |
chiyotsai | a36d900 | 2020-04-29 16:48:21 -0700 | [diff] [blame] | 3307 | mi->tx_size = AOMMIN( |
| 3308 | AOMMIN(max_txsize_lookup[bsize], |
| 3309 | tx_mode_to_biggest_tx_size[txfm_params->tx_mode_search_type]), |
| 3310 | TX_16X16); |
Neeraj Gadgil | 50207d4 | 2022-03-31 16:26:12 +0530 | [diff] [blame] | 3311 | |
Anupam Pandey | 7dfb6d5 | 2022-12-05 14:19:10 +0530 | [diff] [blame] | 3312 | fill_single_inter_mode_costs(search_state.single_inter_mode_costs, |
| 3313 | num_inter_modes, ref_mode_set, mode_costs, |
linzhen | e4954c2 | 2022-09-20 18:27:52 +0000 | [diff] [blame] | 3314 | mbmi_ext->mode_context); |
chiyotsai | 901f406 | 2022-07-06 14:57:53 -0700 | [diff] [blame] | 3315 | |
chiyotsai | d4e5cef | 2022-07-19 14:48:24 -0700 | [diff] [blame] | 3316 | MV_REFERENCE_FRAME last_comp_ref_frame = NONE_FRAME; |
| 3317 | |
Cherma Rajan A | c832755 | 2022-11-11 10:36:09 +0530 | [diff] [blame] | 3318 | // Initialize inter prediction params at block level for single reference |
| 3319 | // mode. |
| 3320 | InterPredParams inter_pred_params_sr; |
| 3321 | init_inter_block_params(&inter_pred_params_sr, pd->width, pd->height, |
| 3322 | mi_row * MI_SIZE, mi_col * MI_SIZE, pd->subsampling_x, |
| 3323 | pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd), |
| 3324 | /*is_intrabc=*/0); |
| 3325 | inter_pred_params_sr.conv_params = |
| 3326 | get_conv_params(/*do_average=*/0, AOM_PLANE_Y, xd->bd); |
| 3327 | |
Marco Paniconi | 65d65996 | 2024-03-09 21:29:46 -0800 | [diff] [blame] | 3328 | x->block_is_zero_sad = x->content_state_sb.source_sad_nonrd == kZeroSad || |
| 3329 | segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP); |
Marco Paniconi | bdef9a9 | 2023-07-06 15:20:46 -0700 | [diff] [blame] | 3330 | if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN && |
| 3331 | !x->force_zeromv_skip_for_blk && |
| 3332 | x->content_state_sb.source_sad_nonrd != kZeroSad && |
Marco Paniconi | 89795ce | 2023-09-13 10:09:32 -0700 | [diff] [blame] | 3333 | x->source_variance == 0 && bsize < cm->seq_params->sb_size && |
| 3334 | search_state.yv12_mb[LAST_FRAME][0].width == cm->width && |
| 3335 | search_state.yv12_mb[LAST_FRAME][0].height == cm->height) { |
Marco Paniconi | bdef9a9 | 2023-07-06 15:20:46 -0700 | [diff] [blame] | 3336 | set_block_source_sad(cpi, x, bsize, &search_state.yv12_mb[LAST_FRAME][0]); |
| 3337 | } |
| 3338 | |
Marco Paniconi | 5a4ea09 | 2023-08-09 14:36:15 -0700 | [diff] [blame] | 3339 | int sb_me_has_been_tested = 0; |
| 3340 | x->sb_me_block = x->sb_me_partition; |
| 3341 | // Only use this feature (force testing of superblock motion) if coding |
| 3342 | // block size is large. |
| 3343 | if (x->sb_me_block) { |
| 3344 | if (cm->seq_params->sb_size == BLOCK_128X128 && bsize < BLOCK_64X64) |
| 3345 | x->sb_me_block = 0; |
| 3346 | else if (cm->seq_params->sb_size == BLOCK_64X64 && bsize < BLOCK_32X32) |
| 3347 | x->sb_me_block = 0; |
| 3348 | } |
| 3349 | |
Marco Paniconi | f289cc9 | 2023-06-27 21:31:04 -0700 | [diff] [blame] | 3350 | x->min_dist_inter_uv = INT64_MAX; |
Marco Paniconi | f190c95 | 2021-08-02 23:07:46 -0700 | [diff] [blame] | 3351 | for (int idx = 0; idx < num_inter_modes + tot_num_comp_modes; ++idx) { |
chiyotsai | a82b98f | 2022-08-17 15:59:47 -0700 | [diff] [blame] | 3352 | // If we are at the first compound mode, and the single modes already |
| 3353 | // perform well, then end the search. |
Anupam Pandey | ab37045 | 2022-12-14 11:23:34 +0530 | [diff] [blame] | 3354 | if (rt_sf->skip_compound_based_on_var && idx == num_inter_modes && |
Anupam Pandey | 7dfb6d5 | 2022-12-05 14:19:10 +0530 | [diff] [blame] | 3355 | skip_comp_based_on_var(search_state.vars, bsize)) { |
chiyotsai | a82b98f | 2022-08-17 15:59:47 -0700 | [diff] [blame] | 3356 | break; |
| 3357 | } |
| 3358 | |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 3359 | int is_single_pred = 1; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3360 | PREDICTION_MODE this_mode; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3361 | |
Marco Paniconi | 05f2a53 | 2023-08-10 12:16:22 -0700 | [diff] [blame] | 3362 | if (idx == 0 && !x->force_zeromv_skip_for_blk) { |
Marco Paniconi | 9a3b768 | 2023-07-10 10:33:13 -0700 | [diff] [blame] | 3363 | // Set color sensitivity on first tested mode only. |
| 3364 | // Use y-sad already computed in find_predictors: take the sad with motion |
| 3365 | // vector closest to 0; the uv-sad computed below in set_color_sensitivity |
| 3366 | // is for zeromv. |
| 3367 | // For screen: first check if golden reference is being used, if so, |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 3368 | // force color_sensitivity on (=1) if the color sensitivity for sb_g is 1. |
| 3369 | // The check in set_color_sensitivity() will then follow and check for |
| 3370 | // setting the flag if the level is still 2 or 0. |
Marco Paniconi | 9a3b768 | 2023-07-10 10:33:13 -0700 | [diff] [blame] | 3371 | if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN && |
| 3372 | search_state.use_ref_frame_mask[GOLDEN_FRAME]) { |
| 3373 | if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_U)] == 1) |
| 3374 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] = 1; |
| 3375 | if (x->color_sensitivity_sb_g[COLOR_SENS_IDX(AOM_PLANE_V)] == 1) |
| 3376 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)] = 1; |
Marco Paniconi | dcba6b2 | 2023-08-03 16:21:09 -0700 | [diff] [blame] | 3377 | } |
| 3378 | if (search_state.use_ref_frame_mask[LAST_FRAME] && |
| 3379 | x->pred_mv0_sad[LAST_FRAME] != INT_MAX) { |
Marco Paniconi | 9a3b768 | 2023-07-10 10:33:13 -0700 | [diff] [blame] | 3380 | int y_sad = x->pred_mv0_sad[LAST_FRAME]; |
| 3381 | if (x->pred_mv1_sad[LAST_FRAME] != INT_MAX && |
| 3382 | (abs(search_state.frame_mv[NEARMV][LAST_FRAME].as_mv.col) + |
| 3383 | abs(search_state.frame_mv[NEARMV][LAST_FRAME].as_mv.row)) < |
| 3384 | (abs(search_state.frame_mv[NEARESTMV][LAST_FRAME].as_mv.col) + |
| 3385 | abs(search_state.frame_mv[NEARESTMV][LAST_FRAME].as_mv.row))) |
| 3386 | y_sad = x->pred_mv1_sad[LAST_FRAME]; |
| 3387 | set_color_sensitivity(cpi, x, bsize, y_sad, x->source_variance, |
| 3388 | search_state.yv12_mb[LAST_FRAME]); |
| 3389 | } |
| 3390 | } |
| 3391 | |
Anupam Pandey | a61e10a | 2022-12-06 16:49:32 +0530 | [diff] [blame] | 3392 | // Check the inter mode can be skipped based on mode statistics and speed |
| 3393 | // features settings. |
Anupam Pandey | cf5a52e | 2022-12-14 18:25:44 +0530 | [diff] [blame] | 3394 | if (skip_inter_mode_nonrd(cpi, x, &search_state, &thresh_sad_pred, |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 3395 | &force_mv_inter_layer, &is_single_pred, |
| 3396 | &this_mode, &last_comp_ref_frame, &ref_frame, |
| 3397 | &ref_frame2, idx, svc_mv, force_skip_low_temp_var, |
Anupam Pandey | cf5a52e | 2022-12-14 18:25:44 +0530 | [diff] [blame] | 3398 | sse_zeromv_norm, num_inter_modes, segment_id, |
| 3399 | bsize, comp_use_zero_zeromv_only, check_globalmv)) |
chiyotsai | 901f406 | 2022-07-06 14:57:53 -0700 | [diff] [blame] | 3400 | continue; |
Fyodor Kyslov | e182a9c | 2020-05-06 14:51:33 -0700 | [diff] [blame] | 3401 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3402 | // Select prediction reference frames. |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 3403 | for (int plane = 0; plane < MAX_MB_PLANE; plane++) { |
| 3404 | xd->plane[plane].pre[0] = search_state.yv12_mb[ref_frame][plane]; |
| 3405 | if (!is_single_pred) |
| 3406 | xd->plane[plane].pre[1] = search_state.yv12_mb[ref_frame2][plane]; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | mi->ref_frame[0] = ref_frame; |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 3410 | mi->ref_frame[1] = ref_frame2; |
| 3411 | set_ref_ptrs(cm, xd, ref_frame, ref_frame2); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3412 | |
Marco Paniconi | a1f2968 | 2023-09-17 20:09:51 -0700 | [diff] [blame] | 3413 | // Check if the scaled reference frame should be used. This is set in the |
| 3414 | // find_predictors() for each usable reference. If so, set the |
| 3415 | // block_ref_scale_factors[] to no reference scaling. |
| 3416 | if (search_state.use_scaled_ref_frame[ref_frame]) { |
| 3417 | xd->block_ref_scale_factors[0] = &sf_no_scale; |
| 3418 | } |
| 3419 | if (!is_single_pred && search_state.use_scaled_ref_frame[ref_frame2]) { |
| 3420 | xd->block_ref_scale_factors[1] = &sf_no_scale; |
| 3421 | } |
| 3422 | |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 3423 | // Perform inter mode evaluation for non-rd |
| 3424 | if (!handle_inter_mode_nonrd( |
| 3425 | cpi, x, &search_state, ctx, &this_mode_pred, tmp_buffer, |
| 3426 | inter_pred_params_sr, &best_early_term, &sse_zeromv_norm, |
| 3427 | &check_globalmv, |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 3428 | #if CONFIG_AV1_TEMPORAL_DENOISING |
Anupam Pandey | 3e797ee | 2022-12-12 14:53:03 +0530 | [diff] [blame] | 3429 | &zero_last_cost_orig, denoise_svc_pickmode, |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 3430 | #endif |
Marco Paniconi | 9a3b768 | 2023-07-10 10:33:13 -0700 | [diff] [blame] | 3431 | idx, force_mv_inter_layer, is_single_pred, gf_temporal_ref, |
| 3432 | use_model_yrd_large, filter_search_enabled_blk, bsize, this_mode, |
Marco Paniconi | 5a4ea09 | 2023-08-09 14:36:15 -0700 | [diff] [blame] | 3433 | filt_select, cb_pred_filter_search, reuse_inter_pred, |
| 3434 | &sb_me_has_been_tested)) { |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3435 | break; |
| 3436 | } |
| 3437 | } |
| 3438 | |
Anupam Pandey | bce4e9a | 2022-12-22 14:45:05 +0530 | [diff] [blame] | 3439 | // Restore mode data of best inter mode |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3440 | mi->mode = best_pickmode->best_mode; |
| 3441 | mi->motion_mode = best_pickmode->best_motion_mode; |
| 3442 | mi->wm_params = best_pickmode->wm_params; |
| 3443 | mi->num_proj_ref = best_pickmode->num_proj_ref; |
| 3444 | mi->interp_filters = best_pickmode->best_pred_filter; |
| 3445 | mi->tx_size = best_pickmode->best_tx_size; |
Fyodor Kyslov | 4dfdb5c | 2019-05-24 11:06:56 -0700 | [diff] [blame] | 3446 | memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size)); |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3447 | mi->ref_frame[0] = best_pickmode->best_ref_frame; |
| 3448 | mi->mv[0].as_int = search_state |
| 3449 | .frame_mv_best[best_pickmode->best_mode] |
| 3450 | [best_pickmode->best_ref_frame] |
| 3451 | .as_int; |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 3452 | mi->mv[1].as_int = 0; |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3453 | if (best_pickmode->best_second_ref_frame > INTRA_FRAME) { |
| 3454 | mi->ref_frame[1] = best_pickmode->best_second_ref_frame; |
| 3455 | mi->mv[1].as_int = search_state |
| 3456 | .frame_mv_best[best_pickmode->best_mode] |
| 3457 | [best_pickmode->best_second_ref_frame] |
| 3458 | .as_int; |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 3459 | } |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 3460 | // Perform intra prediction search, if the best SAD is above a certain |
| 3461 | // threshold. |
| 3462 | mi->angle_delta[PLANE_TYPE_Y] = 0; |
| 3463 | mi->angle_delta[PLANE_TYPE_UV] = 0; |
| 3464 | mi->filter_intra_mode_info.use_filter_intra = 0; |
Marco Paniconi | 5b5b19f | 2019-11-19 11:31:48 -0800 | [diff] [blame] | 3465 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 3466 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 3467 | aom_usec_timer_start(&x->ms_stat_nonrd.timer1); |
| 3468 | x->ms_stat_nonrd.num_searches[bsize][DC_PRED]++; |
| 3469 | x->ms_stat_nonrd.num_nonskipped_searches[bsize][DC_PRED]++; |
chiyotsai | 4f6d2e3 | 2022-05-17 14:26:29 -0700 | [diff] [blame] | 3470 | #endif |
| 3471 | |
Marco Paniconi | fffd4e8 | 2023-01-24 00:32:07 -0800 | [diff] [blame] | 3472 | int force_palette_test = 0; |
| 3473 | if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN && |
Marco Paniconi | fffd4e8 | 2023-01-24 00:32:07 -0800 | [diff] [blame] | 3474 | x->content_state_sb.source_sad_nonrd != kZeroSad && |
Marco Paniconi | c7cbcee | 2023-03-13 11:43:20 -0700 | [diff] [blame] | 3475 | bsize <= BLOCK_16X16) { |
Marco Paniconi | f289cc9 | 2023-06-27 21:31:04 -0700 | [diff] [blame] | 3476 | unsigned int thresh_sse = cpi->rc.high_source_sad ? 15000 : 200000; |
| 3477 | unsigned int thresh_source_var = cpi->rc.high_source_sad ? 50 : 200; |
Marco Paniconi | fffd4e8 | 2023-01-24 00:32:07 -0800 | [diff] [blame] | 3478 | unsigned int best_sse_inter_motion = |
| 3479 | (unsigned int)(search_state.best_rdc.sse >> |
| 3480 | (b_width_log2_lookup[bsize] + |
| 3481 | b_height_log2_lookup[bsize])); |
Marco Paniconi | c7cbcee | 2023-03-13 11:43:20 -0700 | [diff] [blame] | 3482 | if (best_sse_inter_motion > thresh_sse && |
| 3483 | x->source_variance > thresh_source_var) |
| 3484 | force_palette_test = 1; |
Marco Paniconi | fffd4e8 | 2023-01-24 00:32:07 -0800 | [diff] [blame] | 3485 | } |
| 3486 | |
Anupam Pandey | bce4e9a | 2022-12-22 14:45:05 +0530 | [diff] [blame] | 3487 | // Evaluate Intra modes in inter frame |
Ranjit Kumar Tulabandu | cd4a307 | 2022-09-02 15:53:57 +0530 | [diff] [blame] | 3488 | if (!x->force_zeromv_skip_for_blk) |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 3489 | av1_estimate_intra_mode(cpi, x, bsize, best_early_term, |
| 3490 | search_state.ref_costs_single[INTRA_FRAME], |
| 3491 | reuse_inter_pred, &orig_dst, tmp_buffer, |
| 3492 | &this_mode_pred, &search_state.best_rdc, |
| 3493 | best_pickmode, ctx); |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 3494 | |
Anupam Pandey | 45caf95 | 2022-12-15 11:16:15 +0530 | [diff] [blame] | 3495 | int skip_idtx_palette = (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] || |
| 3496 | x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)]) && |
| 3497 | x->content_state_sb.source_sad_nonrd != kZeroSad && |
| 3498 | !cpi->rc.high_source_sad; |
Marco Paniconi | 452d6da | 2022-08-23 00:33:09 -0700 | [diff] [blame] | 3499 | |
Marco Paniconi | bdada5c | 2024-05-22 14:30:55 -0700 | [diff] [blame] | 3500 | bool try_palette = enable_palette( |
| 3501 | cpi, is_mode_intra(best_pickmode->best_mode), bsize, x->source_variance, |
| 3502 | x->force_zeromv_skip_for_blk, skip_idtx_palette, force_palette_test); |
Marco Paniconi | 9f9918d | 2023-01-09 12:44:47 -0800 | [diff] [blame] | 3503 | |
Anupam Pandey | ab57828 | 2022-12-14 21:11:31 +0530 | [diff] [blame] | 3504 | // Perform screen content mode evaluation for non-rd |
Marco Paniconi | 92960a8 | 2023-05-23 15:32:16 -0700 | [diff] [blame] | 3505 | handle_screen_content_mode_nonrd( |
| 3506 | cpi, x, &search_state, this_mode_pred, ctx, tmp_buffer, &orig_dst, |
| 3507 | skip_idtx_palette, try_palette, bsize, reuse_inter_pred, mi_col, mi_row); |
Fyodor Kyslov | dc9bddb | 2022-01-12 16:30:33 -0800 | [diff] [blame] | 3508 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 3509 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 3510 | aom_usec_timer_mark(&x->ms_stat_nonrd.timer1); |
| 3511 | x->ms_stat_nonrd.nonskipped_search_times[bsize][DC_PRED] += |
| 3512 | aom_usec_timer_elapsed(&x->ms_stat_nonrd.timer1); |
chiyotsai | 4f6d2e3 | 2022-05-17 14:26:29 -0700 | [diff] [blame] | 3513 | #endif |
| 3514 | |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3515 | pd->dst = orig_dst; |
Anupam Pandey | bce4e9a | 2022-12-22 14:45:05 +0530 | [diff] [blame] | 3516 | // Best mode is finalized. Restore the mode data to mbmi |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3517 | if (try_palette) mi->palette_mode_info = best_pickmode->pmi; |
| 3518 | mi->mode = best_pickmode->best_mode; |
| 3519 | mi->ref_frame[0] = best_pickmode->best_ref_frame; |
| 3520 | mi->ref_frame[1] = best_pickmode->best_second_ref_frame; |
Marco Paniconi | 1a16d1e | 2023-03-22 14:11:13 -0700 | [diff] [blame] | 3521 | // For lossless: always force the skip flags off. |
| 3522 | if (is_lossless_requested(&cpi->oxcf.rc_cfg)) { |
Marco Paniconi | 59806d4 | 2023-03-21 12:12:38 -0700 | [diff] [blame] | 3523 | txfm_info->skip_txfm = 0; |
| 3524 | memset(ctx->blk_skip, 0, sizeof(ctx->blk_skip[0]) * ctx->num_4x4_blk); |
| 3525 | } else { |
| 3526 | txfm_info->skip_txfm = best_pickmode->best_mode_skip_txfm; |
Fyodor Kyslov | ca638fa | 2021-12-08 19:49:13 -0800 | [diff] [blame] | 3527 | } |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 3528 | if (has_second_ref(mi)) { |
| 3529 | mi->comp_group_idx = 0; |
| 3530 | mi->compound_idx = 1; |
| 3531 | mi->interinter_comp.type = COMPOUND_AVERAGE; |
| 3532 | } |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 3533 | |
| 3534 | if (!is_inter_block(mi)) { |
| 3535 | mi->interp_filters = av1_broadcast_interp_filter(SWITCHABLE_FILTERS); |
Marco Paniconi | a1f2968 | 2023-09-17 20:09:51 -0700 | [diff] [blame] | 3536 | } else { |
| 3537 | // If inter mode is selected and ref_frame was one that uses the |
| 3538 | // scaled reference frame, then we can't use reuse_inter_pred. |
| 3539 | if (search_state.use_scaled_ref_frame[best_pickmode->best_ref_frame] || |
| 3540 | (has_second_ref(mi) && |
| 3541 | search_state |
| 3542 | .use_scaled_ref_frame[best_pickmode->best_second_ref_frame])) |
| 3543 | x->reuse_inter_pred = 0; |
kyslov | 82449d1 | 2019-05-02 13:36:50 -0700 | [diff] [blame] | 3544 | } |
| 3545 | |
Anupam Pandey | bce4e9a | 2022-12-22 14:45:05 +0530 | [diff] [blame] | 3546 | // Restore the predicted samples of best mode to final buffer |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3547 | if (reuse_inter_pred && best_pickmode->best_pred != NULL) { |
| 3548 | PRED_BUFFER *const best_pred = best_pickmode->best_pred; |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3549 | if (best_pred->data != orig_dst.buf && is_inter_mode(mi->mode)) { |
| 3550 | aom_convolve_copy(best_pred->data, best_pred->stride, pd->dst.buf, |
Elliott Karpilovsky | ebe812f | 2020-04-13 18:48:50 -0700 | [diff] [blame] | 3551 | pd->dst.stride, bw, bh); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3552 | } |
| 3553 | } |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 3554 | |
| 3555 | #if CONFIG_AV1_TEMPORAL_DENOISING |
| 3556 | if (cpi->oxcf.noise_sensitivity > 0 && resize_pending == 0 && |
| 3557 | denoise_svc_pickmode && cpi->denoiser.denoising_level > kDenLowLow && |
| 3558 | cpi->denoiser.reset == 0) { |
| 3559 | AV1_DENOISER_DECISION decision = COPY_BLOCK; |
| 3560 | ctx->sb_skip_denoising = 0; |
Anupam Pandey | 7dfb6d5 | 2022-12-05 14:19:10 +0530 | [diff] [blame] | 3561 | av1_pickmode_ctx_den_update( |
| 3562 | &ctx_den, zero_last_cost_orig, search_state.ref_costs_single, |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3563 | search_state.frame_mv, reuse_inter_pred, best_pickmode); |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 3564 | av1_denoiser_denoise(cpi, x, mi_row, mi_col, bsize, ctx, &decision, |
| 3565 | gf_temporal_ref); |
| 3566 | if (denoise_recheck_zeromv) |
Anupam Pandey | 7dfb6d5 | 2022-12-05 14:19:10 +0530 | [diff] [blame] | 3567 | recheck_zeromv_after_denoising( |
| 3568 | cpi, mi, x, xd, decision, &ctx_den, search_state.yv12_mb, |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3569 | &search_state.best_rdc, best_pickmode, bsize, mi_row, mi_col); |
| 3570 | best_pickmode->best_ref_frame = ctx_den.best_ref_frame; |
Fyodor Kyslov | b790c9c | 2021-01-12 15:33:36 -0800 | [diff] [blame] | 3571 | } |
| 3572 | #endif |
| 3573 | |
Anupam Pandey | bce4e9a | 2022-12-22 14:45:05 +0530 | [diff] [blame] | 3574 | // Update the factors used for RD thresholding for all modes. |
Marco Paniconi | 9ebbc8b | 2020-11-29 22:05:39 -0800 | [diff] [blame] | 3575 | if (cpi->sf.inter_sf.adaptive_rd_thresh && !has_second_ref(mi)) { |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 3576 | THR_MODES best_mode_idx = |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3577 | mode_idx[best_pickmode->best_ref_frame][mode_offset(mi->mode)]; |
| 3578 | if (best_pickmode->best_ref_frame == INTRA_FRAME) { |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 3579 | // Only consider the modes that are included in the intra_mode_list. |
| 3580 | int intra_modes = sizeof(intra_mode_list) / sizeof(PREDICTION_MODE); |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 3581 | for (int mode_index = 0; mode_index < intra_modes; mode_index++) { |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 3582 | update_thresh_freq_fact(cpi, x, bsize, INTRA_FRAME, best_mode_idx, |
Anupam Pandey | d3ea12a | 2022-12-16 12:41:55 +0530 | [diff] [blame] | 3583 | intra_mode_list[mode_index]); |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 3584 | } |
| 3585 | } else { |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 3586 | PREDICTION_MODE this_mode; |
| 3587 | for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) { |
Anupam Pandey | 2b4957c | 2022-12-19 16:39:52 +0530 | [diff] [blame] | 3588 | update_thresh_freq_fact(cpi, x, bsize, best_pickmode->best_ref_frame, |
Fyodor Kyslov | f5c9690 | 2020-04-27 17:08:56 -0700 | [diff] [blame] | 3589 | best_mode_idx, this_mode); |
Fyodor Kyslov | 67691bd | 2019-07-25 14:28:34 -0700 | [diff] [blame] | 3590 | } |
| 3591 | } |
| 3592 | } |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3593 | |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 3594 | #if CONFIG_INTERNAL_STATS |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 3595 | store_coding_context_nonrd(x, ctx, mi->mode); |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 3596 | #else |
Anupam Pandey | d68ec61 | 2023-01-03 14:15:58 +0530 | [diff] [blame] | 3597 | store_coding_context_nonrd(x, ctx); |
chiyotsai | 03c48a8 | 2019-10-18 16:48:59 -0700 | [diff] [blame] | 3598 | #endif // CONFIG_INTERNAL_STATS |
Cheng Chen | 82c6134 | 2022-10-18 16:24:06 -0700 | [diff] [blame] | 3599 | |
Anupam Pandey | c5ee9a0 | 2023-01-06 12:15:48 +0530 | [diff] [blame] | 3600 | #if COLLECT_NONRD_PICK_MODE_STAT |
| 3601 | aom_usec_timer_mark(&x->ms_stat_nonrd.bsize_timer); |
| 3602 | x->ms_stat_nonrd.total_block_times[bsize] += |
| 3603 | aom_usec_timer_elapsed(&x->ms_stat_nonrd.bsize_timer); |
| 3604 | print_time(&x->ms_stat_nonrd, bsize, cm->mi_params.mi_rows, |
| 3605 | cm->mi_params.mi_cols, mi_row, mi_col); |
| 3606 | #endif // COLLECT_NONRD_PICK_MODE_STAT |
Cheng Chen | 82c6134 | 2022-10-18 16:24:06 -0700 | [diff] [blame] | 3607 | |
Anupam Pandey | 7dfb6d5 | 2022-12-05 14:19:10 +0530 | [diff] [blame] | 3608 | *rd_cost = search_state.best_rdc; |
Marco Paniconi | 0aeecfe | 2023-10-04 00:06:46 -0700 | [diff] [blame] | 3609 | |
| 3610 | // Reset the xd->block_ref_scale_factors[i], as they may have |
| 3611 | // been set to pointer &sf_no_scale, which becomes invalid afer |
| 3612 | // this function. |
| 3613 | set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]); |
kyslov | e3c05a8 | 2019-03-28 11:06:09 -0700 | [diff] [blame] | 3614 | } |