Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 12 | #ifndef AOM_AV1_ENCODER_RD_H_ |
| 13 | #define AOM_AV1_ENCODER_RD_H_ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
| 15 | #include <limits.h> |
| 16 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 17 | #include "av1/common/blockd.h" |
| 18 | |
| 19 | #include "av1/encoder/block.h" |
| 20 | #include "av1/encoder/context_tree.h" |
| 21 | #include "av1/encoder/cost.h" |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | #define RDDIV_BITS 7 |
| 28 | #define RD_EPB_SHIFT 6 |
| 29 | |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 30 | #define RDCOST(RM, R, D) \ |
| 31 | (ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), AV1_PROB_COST_SHIFT) + \ |
| 32 | ((D) * (1 << RDDIV_BITS))) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 33 | |
sdeng | 256c4d3 | 2019-06-11 12:19:48 -0700 | [diff] [blame] | 34 | #define RDCOST_NEG_R(RM, R, D) \ |
| 35 | (((D) * (1 << RDDIV_BITS)) - \ |
| 36 | ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), AV1_PROB_COST_SHIFT)) |
| 37 | |
Nithya V S | 5bff0a0 | 2020-09-16 14:07:24 +0530 | [diff] [blame] | 38 | #define RDCOST_DBL_WITH_NATIVE_BD_DIST(RM, R, D, BD) \ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 39 | (((((double)(R)) * (RM)) / (double)(1 << AV1_PROB_COST_SHIFT)) + \ |
Nithya V S | 5bff0a0 | 2020-09-16 14:07:24 +0530 | [diff] [blame] | 40 | ((double)((D) >> (2 * (BD - 8))) * (1 << RDDIV_BITS))) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 41 | |
| 42 | #define QIDX_SKIP_THRESH 115 |
| 43 | |
| 44 | #define MV_COST_WEIGHT 108 |
| 45 | #define MV_COST_WEIGHT_SUB 120 |
| 46 | |
chiyotsai | 0b64260 | 2020-01-08 13:31:44 -0800 | [diff] [blame] | 47 | // The fractional part of rd_thresh factor is stored with 5 bits. The maximum |
| 48 | // factor that we allow is two, which is stored as 2 ** (5+1) = 64 |
| 49 | #define RD_THRESH_FAC_FRAC_BITS (5) |
| 50 | #define RD_THRESH_FAC_FRAC_VAL (1 << (RD_THRESH_FAC_FRAC_BITS)) |
| 51 | #define RD_THRESH_MAX_FACT ((RD_THRESH_FAC_FRAC_VAL) << 1) |
| 52 | #define RD_THRESH_LOG_DEC_FACTOR (4) |
| 53 | #define RD_THRESH_INC (1) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 54 | |
Peng Bin | c7101aa | 2018-06-22 20:47:05 +0800 | [diff] [blame] | 55 | // Factor to weigh the rate for switchable interp filters. |
| 56 | #define SWITCHABLE_INTERP_RATE_FACTOR 1 |
| 57 | |
Cherma Rajan A | 628efce | 2019-09-18 18:55:12 +0530 | [diff] [blame] | 58 | enum { |
chiyotsai | f271f9f | 2019-11-25 16:44:25 -0800 | [diff] [blame] | 59 | // Default initialization when we are not using winner mode framework. e.g. |
| 60 | // intrabc |
Cherma Rajan A | 628efce | 2019-09-18 18:55:12 +0530 | [diff] [blame] | 61 | DEFAULT_EVAL = 0, |
chiyotsai | f271f9f | 2019-11-25 16:44:25 -0800 | [diff] [blame] | 62 | // Initialization for selecting winner mode |
Cherma Rajan A | 628efce | 2019-09-18 18:55:12 +0530 | [diff] [blame] | 63 | MODE_EVAL, |
| 64 | // Initialization for winner mode evaluation |
| 65 | WINNER_MODE_EVAL, |
| 66 | // All mode evaluation types |
| 67 | MODE_EVAL_TYPES, |
| 68 | } UENUM1BYTE(MODE_EVAL_TYPE); |
| 69 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 70 | typedef struct RD_OPT { |
| 71 | // Thresh_mult is used to set a threshold for the rd score. A higher value |
| 72 | // means that we will accept the best mode so far more often. This number |
| 73 | // is used in combination with the current block size, and thresh_freq_fact |
| 74 | // to pick a threshold. |
| 75 | int thresh_mult[MAX_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 76 | |
Rupert Swarbrick | 93c39e9 | 2017-07-12 11:11:02 +0100 | [diff] [blame] | 77 | int threshes[MAX_SEGMENTS][BLOCK_SIZES_ALL][MAX_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 78 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 79 | int RDMULT; |
Yue Chen | 7cae98f | 2018-08-24 10:43:16 -0700 | [diff] [blame] | 80 | |
Jingning Han | 94b2854 | 2020-08-03 23:11:27 -0700 | [diff] [blame] | 81 | double r0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 82 | } RD_OPT; |
| 83 | |
Deepa K G | 06f2c62 | 2020-04-12 12:24:35 +0530 | [diff] [blame] | 84 | typedef struct { |
| 85 | // Cost of transmitting the actual motion vector. |
| 86 | // mv_component[0][i] is the cost of motion vector with horizontal component |
| 87 | // (mv_row) equal to i - MV_MAX. |
| 88 | // mv_component[1][i] is the cost of motion vector with vertical component |
| 89 | // (mv_col) equal to i - MV_MAX. |
| 90 | int mv_component[2][MV_VALS]; |
| 91 | |
| 92 | // joint_mv[i] is the cost of transmitting joint mv(MV_JOINT_TYPE) of |
| 93 | // type i. |
| 94 | // TODO(huisu@google.com): we can update dv_joint_cost per SB. |
| 95 | int joint_mv[MV_JOINTS]; |
| 96 | } IntraBCMVCosts; |
| 97 | |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 98 | static INLINE void av1_init_rd_stats(RD_STATS *rd_stats) { |
| 99 | #if CONFIG_RD_DEBUG |
| 100 | int plane; |
| 101 | #endif |
| 102 | rd_stats->rate = 0; |
| 103 | rd_stats->dist = 0; |
| 104 | rd_stats->rdcost = 0; |
| 105 | rd_stats->sse = 0; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 106 | rd_stats->skip_txfm = 1; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 107 | rd_stats->zero_rate = 0; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 108 | #if CONFIG_RD_DEBUG |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 109 | // This may run into problems when monochrome video is |
| 110 | // encoded, as there will only be 1 plane |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 111 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 112 | rd_stats->txb_coeff_cost[plane] = 0; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 113 | { |
| 114 | int r, c; |
| 115 | for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) |
| 116 | for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) |
| 117 | rd_stats->txb_coeff_cost_map[plane][r][c] = 0; |
| 118 | } |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 119 | } |
| 120 | #endif |
| 121 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 122 | |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 123 | static INLINE void av1_invalid_rd_stats(RD_STATS *rd_stats) { |
| 124 | #if CONFIG_RD_DEBUG |
| 125 | int plane; |
| 126 | #endif |
| 127 | rd_stats->rate = INT_MAX; |
| 128 | rd_stats->dist = INT64_MAX; |
| 129 | rd_stats->rdcost = INT64_MAX; |
| 130 | rd_stats->sse = INT64_MAX; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 131 | rd_stats->skip_txfm = 0; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 132 | rd_stats->zero_rate = 0; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 133 | #if CONFIG_RD_DEBUG |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 134 | // This may run into problems when monochrome video is |
| 135 | // encoded, as there will only be 1 plane |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 136 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 137 | rd_stats->txb_coeff_cost[plane] = INT_MAX; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 138 | { |
| 139 | int r, c; |
| 140 | for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) |
| 141 | for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) |
Jingning Han | 46564ea | 2019-10-28 15:29:54 -0700 | [diff] [blame] | 142 | rd_stats->txb_coeff_cost_map[plane][r][c] = INT16_MAX; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 143 | } |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 144 | } |
| 145 | #endif |
| 146 | } |
| 147 | |
| 148 | static INLINE void av1_merge_rd_stats(RD_STATS *rd_stats_dst, |
| 149 | const RD_STATS *rd_stats_src) { |
Hui Su | cd73550 | 2019-04-08 11:23:02 -0700 | [diff] [blame] | 150 | assert(rd_stats_dst->rate != INT_MAX && rd_stats_src->rate != INT_MAX); |
Venkat | 7248175 | 2019-07-01 16:33:49 +0530 | [diff] [blame] | 151 | rd_stats_dst->rate = (int)AOMMIN( |
| 152 | ((int64_t)rd_stats_dst->rate + (int64_t)rd_stats_src->rate), INT_MAX); |
Debargha Mukherjee | de80e76 | 2017-11-30 13:58:56 -0800 | [diff] [blame] | 153 | if (!rd_stats_dst->zero_rate) |
| 154 | rd_stats_dst->zero_rate = rd_stats_src->zero_rate; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 155 | rd_stats_dst->dist += rd_stats_src->dist; |
| 156 | rd_stats_dst->sse += rd_stats_src->sse; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 157 | rd_stats_dst->skip_txfm &= rd_stats_src->skip_txfm; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 158 | #if CONFIG_RD_DEBUG |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 159 | // This may run into problems when monochrome video is |
| 160 | // encoded, as there will only be 1 plane |
Hui Su | cd73550 | 2019-04-08 11:23:02 -0700 | [diff] [blame] | 161 | for (int plane = 0; plane < MAX_MB_PLANE; ++plane) { |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 162 | rd_stats_dst->txb_coeff_cost[plane] += rd_stats_src->txb_coeff_cost[plane]; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 163 | { |
| 164 | // TODO(angiebird): optimize this part |
| 165 | int r, c; |
| 166 | int ref_txb_coeff_cost = 0; |
| 167 | for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) |
| 168 | for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) { |
| 169 | rd_stats_dst->txb_coeff_cost_map[plane][r][c] += |
| 170 | rd_stats_src->txb_coeff_cost_map[plane][r][c]; |
| 171 | ref_txb_coeff_cost += rd_stats_dst->txb_coeff_cost_map[plane][r][c]; |
| 172 | } |
| 173 | assert(ref_txb_coeff_cost == rd_stats_dst->txb_coeff_cost[plane]); |
| 174 | } |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 175 | } |
| 176 | #endif |
| 177 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 178 | |
Venkat | d5d823c | 2019-06-25 16:48:23 +0530 | [diff] [blame] | 179 | static INLINE void av1_accumulate_rd_stats(RD_STATS *rd_stats, int64_t dist, |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 180 | int rate, int skip_txfm, int64_t sse, |
Venkat | d5d823c | 2019-06-25 16:48:23 +0530 | [diff] [blame] | 181 | int zero_rate) { |
| 182 | assert(rd_stats->rate != INT_MAX && rate != INT_MAX); |
| 183 | rd_stats->rate += rate; |
| 184 | if (!rd_stats->zero_rate) rd_stats->zero_rate = zero_rate; |
| 185 | rd_stats->dist += dist; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 186 | rd_stats->skip_txfm &= skip_txfm; |
Venkat | d5d823c | 2019-06-25 16:48:23 +0530 | [diff] [blame] | 187 | rd_stats->sse += sse; |
| 188 | } |
| 189 | |
sdeng | 256c4d3 | 2019-06-11 12:19:48 -0700 | [diff] [blame] | 190 | static INLINE int64_t av1_calculate_rd_cost(int mult, int rate, int64_t dist) { |
| 191 | assert(mult >= 0); |
| 192 | if (rate >= 0) { |
| 193 | return RDCOST(mult, rate, dist); |
| 194 | } |
| 195 | return RDCOST_NEG_R(mult, -rate, dist); |
| 196 | } |
| 197 | |
| 198 | static INLINE void av1_rd_cost_update(int mult, RD_STATS *rd_cost) { |
sdeng | afdb851 | 2019-06-12 16:15:56 -0700 | [diff] [blame] | 199 | if (rd_cost->rate < INT_MAX && rd_cost->dist < INT64_MAX && |
| 200 | rd_cost->rdcost < INT64_MAX) { |
sdeng | 256c4d3 | 2019-06-11 12:19:48 -0700 | [diff] [blame] | 201 | rd_cost->rdcost = av1_calculate_rd_cost(mult, rd_cost->rate, rd_cost->dist); |
| 202 | } else { |
| 203 | av1_invalid_rd_stats(rd_cost); |
| 204 | } |
| 205 | } |
| 206 | |
sdeng | afdb851 | 2019-06-12 16:15:56 -0700 | [diff] [blame] | 207 | static INLINE void av1_rd_stats_subtraction(int mult, |
| 208 | const RD_STATS *const left, |
sdeng | eff3bb4 | 2019-06-11 15:56:04 -0700 | [diff] [blame] | 209 | const RD_STATS *const right, |
| 210 | RD_STATS *result) { |
| 211 | if (left->rate == INT_MAX || right->rate == INT_MAX || |
| 212 | left->dist == INT64_MAX || right->dist == INT64_MAX || |
| 213 | left->rdcost == INT64_MAX || right->rdcost == INT64_MAX) { |
| 214 | av1_invalid_rd_stats(result); |
| 215 | } else { |
| 216 | result->rate = left->rate - right->rate; |
| 217 | result->dist = left->dist - right->dist; |
sdeng | afdb851 | 2019-06-12 16:15:56 -0700 | [diff] [blame] | 218 | result->rdcost = av1_calculate_rd_cost(mult, result->rate, result->dist); |
sdeng | eff3bb4 | 2019-06-11 15:56:04 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 222 | struct TileInfo; |
| 223 | struct TileDataEnc; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 224 | struct AV1_COMP; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 225 | struct macroblock; |
| 226 | |
Yaowu Xu | ea8a6ca | 2018-11-07 14:53:46 -0800 | [diff] [blame] | 227 | int av1_compute_rd_mult_based_on_qindex(const struct AV1_COMP *cpi, int qindex); |
Ravi Chaudhary | 9549205 | 2018-10-24 11:51:28 +0530 | [diff] [blame] | 228 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 229 | int av1_compute_rd_mult(const struct AV1_COMP *cpi, int qindex); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 230 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 231 | void av1_initialize_rd_consts(struct AV1_COMP *cpi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 232 | |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 233 | // Sets the multiplier to convert mv cost to l1 error during motion search. |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 234 | void av1_set_sad_per_bit(const struct AV1_COMP *cpi, MvCosts *mv_costs, |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 235 | int qindex); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 236 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 237 | void av1_model_rd_from_var_lapndz(int64_t var, unsigned int n, |
| 238 | unsigned int qstep, int *rate, int64_t *dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 239 | |
Debargha Mukherjee | 0007983 | 2019-01-17 11:28:52 -0800 | [diff] [blame] | 240 | void av1_model_rd_curvfit(BLOCK_SIZE bsize, double sse_norm, double xqr, |
Debargha Mukherjee | a7b3731 | 2019-01-14 15:15:49 -0800 | [diff] [blame] | 241 | double *rate_f, double *distbysse_f); |
Debargha Mukherjee | 0007983 | 2019-01-17 11:28:52 -0800 | [diff] [blame] | 242 | void av1_model_rd_surffit(BLOCK_SIZE bsize, double sse_norm, double xm, |
| 243 | double yl, double *rate_f, double *distbysse_f); |
Debargha Mukherjee | 483e295 | 2018-07-25 07:34:26 -0700 | [diff] [blame] | 244 | |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 245 | int av1_get_switchable_rate(const MACROBLOCK *x, const MACROBLOCKD *xd, |
Hui Su | 321b175 | 2020-11-06 11:11:27 -0800 | [diff] [blame] | 246 | #if !CONFIG_REMOVE_DUAL_FILTER |
| 247 | int dual_filter, |
| 248 | #endif // !CONFIG_REMOVE_DUAL_FILTER |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 249 | InterpFilter interp_filter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 250 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 251 | YV12_BUFFER_CONFIG *av1_get_scaled_ref_frame(const struct AV1_COMP *cpi, |
| 252 | int ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 253 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 254 | void av1_init_me_luts(void); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 255 | |
Yaowu Xu | 901d622 | 2018-02-21 21:40:48 -0800 | [diff] [blame] | 256 | void av1_set_mvcost(MACROBLOCK *x, int ref, int ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 257 | |
Hui Su | 56e838f | 2019-12-04 14:48:30 -0800 | [diff] [blame] | 258 | void av1_get_entropy_contexts(BLOCK_SIZE plane_bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 259 | const struct macroblockd_plane *pd, |
Wan-Teh Chang | 660f8ed | 2018-05-11 17:28:08 -0700 | [diff] [blame] | 260 | ENTROPY_CONTEXT t_above[MAX_MIB_SIZE], |
| 261 | ENTROPY_CONTEXT t_left[MAX_MIB_SIZE]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 262 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 263 | void av1_set_rd_speed_thresholds(struct AV1_COMP *cpi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 264 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 265 | void av1_update_rd_thresh_fact(const AV1_COMMON *const cm, |
chiyotsai | 0b64260 | 2020-01-08 13:31:44 -0800 | [diff] [blame] | 266 | int (*fact)[MAX_MODES], int rd_thresh, |
| 267 | BLOCK_SIZE bsize, THR_MODES best_mode_index); |
| 268 | |
| 269 | static INLINE void reset_thresh_freq_fact(MACROBLOCK *const x) { |
| 270 | for (int i = 0; i < BLOCK_SIZES_ALL; ++i) { |
| 271 | for (int j = 0; j < MAX_MODES; ++j) { |
| 272 | x->thresh_freq_fact[i][j] = RD_THRESH_FAC_FRAC_VAL; |
| 273 | } |
| 274 | } |
| 275 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 276 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 277 | static INLINE int rd_less_than_thresh(int64_t best_rd, int thresh, |
| 278 | int thresh_fact) { |
| 279 | return best_rd < ((int64_t)thresh * thresh_fact >> 5) || thresh == INT_MAX; |
| 280 | } |
| 281 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 282 | void av1_mv_pred(const struct AV1_COMP *cpi, MACROBLOCK *x, |
| 283 | uint8_t *ref_y_buffer, int ref_y_stride, int ref_frame, |
| 284 | BLOCK_SIZE block_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 285 | |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 286 | // Sets the multiplier to convert mv cost to l2 error during motion search. |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 287 | static INLINE void av1_set_error_per_bit(MvCosts *mv_costs, int rdmult) { |
| 288 | mv_costs->errorperbit = AOMMAX(rdmult >> RD_EPB_SHIFT, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Ranjit Kumar Tulabandu | 824f9db | 2019-07-05 15:16:37 +0530 | [diff] [blame] | 291 | // Get the threshold for R-D optimization of coefficients depending upon mode |
| 292 | // decision/winner mode processing |
| 293 | static INLINE uint32_t get_rd_opt_coeff_thresh( |
| 294 | const uint32_t *const coeff_opt_dist_threshold, |
| 295 | int enable_winner_mode_for_coeff_opt, int is_winner_mode) { |
| 296 | // Default initialization of threshold |
Cherma Rajan A | 628efce | 2019-09-18 18:55:12 +0530 | [diff] [blame] | 297 | uint32_t coeff_opt_thresh = coeff_opt_dist_threshold[DEFAULT_EVAL]; |
Ranjit Kumar Tulabandu | 824f9db | 2019-07-05 15:16:37 +0530 | [diff] [blame] | 298 | // TODO(any): Experiment with coeff_opt_dist_threshold values when |
| 299 | // enable_winner_mode_for_coeff_opt is ON |
| 300 | // TODO(any): Skip the winner mode processing for blocks with lower residual |
| 301 | // energy as R-D optimization of coefficients would have been enabled during |
| 302 | // mode decision |
| 303 | if (enable_winner_mode_for_coeff_opt) { |
| 304 | // Use conservative threshold during mode decision and perform R-D |
| 305 | // optimization of coeffs always for winner modes |
| 306 | if (is_winner_mode) |
Cherma Rajan A | 628efce | 2019-09-18 18:55:12 +0530 | [diff] [blame] | 307 | coeff_opt_thresh = coeff_opt_dist_threshold[WINNER_MODE_EVAL]; |
Ranjit Kumar Tulabandu | 824f9db | 2019-07-05 15:16:37 +0530 | [diff] [blame] | 308 | else |
Cherma Rajan A | 628efce | 2019-09-18 18:55:12 +0530 | [diff] [blame] | 309 | coeff_opt_thresh = coeff_opt_dist_threshold[MODE_EVAL]; |
Ranjit Kumar Tulabandu | 824f9db | 2019-07-05 15:16:37 +0530 | [diff] [blame] | 310 | } |
| 311 | return coeff_opt_thresh; |
| 312 | } |
| 313 | |
Ranjit Kumar Tulabandu | c971730 | 2019-07-08 15:08:09 +0530 | [diff] [blame] | 314 | // Used to reset the state of tx/mb rd hash information |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 315 | static INLINE void reset_hash_records(TxfmSearchInfo *const txfm_info, |
Vishesh | 593ef1b | 2019-11-25 15:52:15 +0530 | [diff] [blame] | 316 | int use_inter_txb_hash) { |
Ranjit Kumar Tulabandu | c971730 | 2019-07-08 15:08:09 +0530 | [diff] [blame] | 317 | int32_t record_idx; |
| 318 | |
| 319 | // Reset the state for use_inter_txb_hash |
Vishesh | 593ef1b | 2019-11-25 15:52:15 +0530 | [diff] [blame] | 320 | if (use_inter_txb_hash) { |
| 321 | for (record_idx = 0; |
| 322 | record_idx < ((MAX_MIB_SIZE >> 1) * (MAX_MIB_SIZE >> 1)); record_idx++) |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 323 | txfm_info->txb_rd_record_8X8[record_idx].num = |
| 324 | txfm_info->txb_rd_record_8X8[record_idx].index_start = 0; |
Vishesh | 593ef1b | 2019-11-25 15:52:15 +0530 | [diff] [blame] | 325 | for (record_idx = 0; |
| 326 | record_idx < ((MAX_MIB_SIZE >> 2) * (MAX_MIB_SIZE >> 2)); record_idx++) |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 327 | txfm_info->txb_rd_record_16X16[record_idx].num = |
| 328 | txfm_info->txb_rd_record_16X16[record_idx].index_start = 0; |
Vishesh | 593ef1b | 2019-11-25 15:52:15 +0530 | [diff] [blame] | 329 | for (record_idx = 0; |
| 330 | record_idx < ((MAX_MIB_SIZE >> 3) * (MAX_MIB_SIZE >> 3)); record_idx++) |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 331 | txfm_info->txb_rd_record_32X32[record_idx].num = |
| 332 | txfm_info->txb_rd_record_32X32[record_idx].index_start = 0; |
Vishesh | 593ef1b | 2019-11-25 15:52:15 +0530 | [diff] [blame] | 333 | for (record_idx = 0; |
| 334 | record_idx < ((MAX_MIB_SIZE >> 4) * (MAX_MIB_SIZE >> 4)); record_idx++) |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 335 | txfm_info->txb_rd_record_64X64[record_idx].num = |
| 336 | txfm_info->txb_rd_record_64X64[record_idx].index_start = 0; |
Vishesh | 593ef1b | 2019-11-25 15:52:15 +0530 | [diff] [blame] | 337 | } |
Ranjit Kumar Tulabandu | c971730 | 2019-07-08 15:08:09 +0530 | [diff] [blame] | 338 | |
| 339 | // Reset the state for use_intra_txb_hash |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 340 | txfm_info->txb_rd_record_intra.num = |
| 341 | txfm_info->txb_rd_record_intra.index_start = 0; |
Ranjit Kumar Tulabandu | c971730 | 2019-07-08 15:08:09 +0530 | [diff] [blame] | 342 | |
| 343 | // Reset the state for use_mb_rd_hash |
chiyotsai | 4c1e5c6 | 2020-04-30 17:54:14 -0700 | [diff] [blame] | 344 | txfm_info->mb_rd_record.num = txfm_info->mb_rd_record.index_start = 0; |
Ranjit Kumar Tulabandu | c971730 | 2019-07-08 15:08:09 +0530 | [diff] [blame] | 345 | } |
| 346 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 347 | void av1_setup_pred_block(const MACROBLOCKD *xd, |
| 348 | struct buf_2d dst[MAX_MB_PLANE], |
Hui Su | b94cd5e | 2019-11-06 12:05:47 -0800 | [diff] [blame] | 349 | const YV12_BUFFER_CONFIG *src, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 350 | const struct scale_factors *scale, |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 351 | const struct scale_factors *scale_uv, |
| 352 | const int num_planes); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 353 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 354 | int av1_get_intra_cost_penalty(int qindex, int qdelta, |
| 355 | aom_bit_depth_t bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 356 | |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 357 | void av1_fill_mode_rates(AV1_COMMON *const cm, ModeCosts *mode_costs, |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 358 | FRAME_CONTEXT *fc); |
| 359 | |
Mufaddal Chakera | ec44906 | 2020-06-03 16:57:56 +0530 | [diff] [blame] | 360 | void av1_fill_lr_rates(ModeCosts *mode_costs, FRAME_CONTEXT *fc); |
| 361 | |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 362 | void av1_fill_coeff_costs(CoeffCosts *coeff_costs, FRAME_CONTEXT *fc, |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 363 | const int num_planes); |
Jingning Han | dfd7232 | 2017-08-09 14:04:12 -0700 | [diff] [blame] | 364 | |
Wan-Teh Chang | ec5cb19 | 2019-09-16 08:50:06 -0700 | [diff] [blame] | 365 | void av1_fill_mv_costs(const FRAME_CONTEXT *fc, int integer_mv, int usehp, |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 366 | MvCosts *mv_costs); |
Debargha Mukherjee | 8d27341 | 2019-09-11 10:36:50 -0700 | [diff] [blame] | 367 | |
Yue Chen | 7cae98f | 2018-08-24 10:43:16 -0700 | [diff] [blame] | 368 | int av1_get_adaptive_rdmult(const struct AV1_COMP *cpi, double beta); |
| 369 | |
Debargha Mukherjee | cfcd5ad | 2019-05-20 20:27:17 -0700 | [diff] [blame] | 370 | int av1_get_deltaq_offset(const struct AV1_COMP *cpi, int qindex, double beta); |
| 371 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 372 | #ifdef __cplusplus |
| 373 | } // extern "C" |
| 374 | #endif |
| 375 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 376 | #endif // AOM_AV1_ENCODER_RD_H_ |