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 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 38 | #define RDCOST_DBL(RM, R, D) \ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 39 | (((((double)(R)) * (RM)) / (double)(1 << AV1_PROB_COST_SHIFT)) + \ |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 40 | ((double)(D) * (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 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 47 | #define RD_THRESH_MAX_FACT 64 |
| 48 | #define RD_THRESH_INC 1 |
| 49 | |
Peng Bin | c7101aa | 2018-06-22 20:47:05 +0800 | [diff] [blame] | 50 | // Factor to weigh the rate for switchable interp filters. |
| 51 | #define SWITCHABLE_INTERP_RATE_FACTOR 1 |
| 52 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 53 | // This enumerator type needs to be kept aligned with the mode order in |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 54 | // const MODE_DEFINITION av1_mode_order[MAX_MODES] used in the rd code. |
Satish Kumar Suman | 4667aa1 | 2018-12-14 18:28:19 +0530 | [diff] [blame] | 55 | enum { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 56 | THR_NEARESTMV, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 57 | THR_NEARESTL2, |
| 58 | THR_NEARESTL3, |
| 59 | THR_NEARESTB, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 60 | THR_NEARESTA2, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 61 | THR_NEARESTA, |
| 62 | THR_NEARESTG, |
| 63 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 64 | THR_NEWMV, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 65 | THR_NEWL2, |
| 66 | THR_NEWL3, |
| 67 | THR_NEWB, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 68 | THR_NEWA2, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 69 | THR_NEWA, |
| 70 | THR_NEWG, |
| 71 | |
| 72 | THR_NEARMV, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 73 | THR_NEARL2, |
| 74 | THR_NEARL3, |
| 75 | THR_NEARB, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 76 | THR_NEARA2, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 77 | THR_NEARA, |
| 78 | THR_NEARG, |
| 79 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 80 | THR_GLOBALMV, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 81 | THR_GLOBALL2, |
| 82 | THR_GLOBALL3, |
| 83 | THR_GLOBALB, |
| 84 | THR_GLOBALA2, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 85 | THR_GLOBALG, |
elliottk | 05fed21 | 2019-04-01 23:05:51 -0700 | [diff] [blame] | 86 | THR_GLOBALA, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 87 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 88 | THR_COMP_NEAREST_NEARESTLA, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 89 | THR_COMP_NEAREST_NEARESTL2A, |
| 90 | THR_COMP_NEAREST_NEARESTL3A, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 91 | THR_COMP_NEAREST_NEARESTGA, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 92 | THR_COMP_NEAREST_NEARESTLB, |
| 93 | THR_COMP_NEAREST_NEARESTL2B, |
| 94 | THR_COMP_NEAREST_NEARESTL3B, |
| 95 | THR_COMP_NEAREST_NEARESTGB, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 96 | THR_COMP_NEAREST_NEARESTLA2, |
| 97 | THR_COMP_NEAREST_NEARESTL2A2, |
| 98 | THR_COMP_NEAREST_NEARESTL3A2, |
| 99 | THR_COMP_NEAREST_NEARESTGA2, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 100 | THR_COMP_NEAREST_NEARESTLL2, |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 101 | THR_COMP_NEAREST_NEARESTLL3, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 102 | THR_COMP_NEAREST_NEARESTLG, |
| 103 | THR_COMP_NEAREST_NEARESTBA, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 104 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 105 | THR_COMP_NEAR_NEARLA, |
| 106 | THR_COMP_NEW_NEARESTLA, |
| 107 | THR_COMP_NEAREST_NEWLA, |
| 108 | THR_COMP_NEW_NEARLA, |
| 109 | THR_COMP_NEAR_NEWLA, |
| 110 | THR_COMP_NEW_NEWLA, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 111 | THR_COMP_GLOBAL_GLOBALLA, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 112 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 113 | THR_COMP_NEAR_NEARL2A, |
| 114 | THR_COMP_NEW_NEARESTL2A, |
| 115 | THR_COMP_NEAREST_NEWL2A, |
| 116 | THR_COMP_NEW_NEARL2A, |
| 117 | THR_COMP_NEAR_NEWL2A, |
| 118 | THR_COMP_NEW_NEWL2A, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 119 | THR_COMP_GLOBAL_GLOBALL2A, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 120 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 121 | THR_COMP_NEAR_NEARL3A, |
| 122 | THR_COMP_NEW_NEARESTL3A, |
| 123 | THR_COMP_NEAREST_NEWL3A, |
| 124 | THR_COMP_NEW_NEARL3A, |
| 125 | THR_COMP_NEAR_NEWL3A, |
| 126 | THR_COMP_NEW_NEWL3A, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 127 | THR_COMP_GLOBAL_GLOBALL3A, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 128 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 129 | THR_COMP_NEAR_NEARGA, |
| 130 | THR_COMP_NEW_NEARESTGA, |
| 131 | THR_COMP_NEAREST_NEWGA, |
| 132 | THR_COMP_NEW_NEARGA, |
| 133 | THR_COMP_NEAR_NEWGA, |
| 134 | THR_COMP_NEW_NEWGA, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 135 | THR_COMP_GLOBAL_GLOBALGA, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 136 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 137 | THR_COMP_NEAR_NEARLB, |
| 138 | THR_COMP_NEW_NEARESTLB, |
| 139 | THR_COMP_NEAREST_NEWLB, |
| 140 | THR_COMP_NEW_NEARLB, |
| 141 | THR_COMP_NEAR_NEWLB, |
| 142 | THR_COMP_NEW_NEWLB, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 143 | THR_COMP_GLOBAL_GLOBALLB, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 144 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 145 | THR_COMP_NEAR_NEARL2B, |
| 146 | THR_COMP_NEW_NEARESTL2B, |
| 147 | THR_COMP_NEAREST_NEWL2B, |
| 148 | THR_COMP_NEW_NEARL2B, |
| 149 | THR_COMP_NEAR_NEWL2B, |
| 150 | THR_COMP_NEW_NEWL2B, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 151 | THR_COMP_GLOBAL_GLOBALL2B, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 152 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 153 | THR_COMP_NEAR_NEARL3B, |
| 154 | THR_COMP_NEW_NEARESTL3B, |
| 155 | THR_COMP_NEAREST_NEWL3B, |
| 156 | THR_COMP_NEW_NEARL3B, |
| 157 | THR_COMP_NEAR_NEWL3B, |
| 158 | THR_COMP_NEW_NEWL3B, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 159 | THR_COMP_GLOBAL_GLOBALL3B, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 160 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 161 | THR_COMP_NEAR_NEARGB, |
| 162 | THR_COMP_NEW_NEARESTGB, |
| 163 | THR_COMP_NEAREST_NEWGB, |
| 164 | THR_COMP_NEW_NEARGB, |
| 165 | THR_COMP_NEAR_NEWGB, |
| 166 | THR_COMP_NEW_NEWGB, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 167 | THR_COMP_GLOBAL_GLOBALGB, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 168 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 169 | THR_COMP_NEAR_NEARLA2, |
| 170 | THR_COMP_NEW_NEARESTLA2, |
| 171 | THR_COMP_NEAREST_NEWLA2, |
| 172 | THR_COMP_NEW_NEARLA2, |
| 173 | THR_COMP_NEAR_NEWLA2, |
| 174 | THR_COMP_NEW_NEWLA2, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 175 | THR_COMP_GLOBAL_GLOBALLA2, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 176 | |
| 177 | THR_COMP_NEAR_NEARL2A2, |
| 178 | THR_COMP_NEW_NEARESTL2A2, |
| 179 | THR_COMP_NEAREST_NEWL2A2, |
| 180 | THR_COMP_NEW_NEARL2A2, |
| 181 | THR_COMP_NEAR_NEWL2A2, |
| 182 | THR_COMP_NEW_NEWL2A2, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 183 | THR_COMP_GLOBAL_GLOBALL2A2, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 184 | |
| 185 | THR_COMP_NEAR_NEARL3A2, |
| 186 | THR_COMP_NEW_NEARESTL3A2, |
| 187 | THR_COMP_NEAREST_NEWL3A2, |
| 188 | THR_COMP_NEW_NEARL3A2, |
| 189 | THR_COMP_NEAR_NEWL3A2, |
| 190 | THR_COMP_NEW_NEWL3A2, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 191 | THR_COMP_GLOBAL_GLOBALL3A2, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 192 | |
| 193 | THR_COMP_NEAR_NEARGA2, |
| 194 | THR_COMP_NEW_NEARESTGA2, |
| 195 | THR_COMP_NEAREST_NEWGA2, |
| 196 | THR_COMP_NEW_NEARGA2, |
| 197 | THR_COMP_NEAR_NEWGA2, |
| 198 | THR_COMP_NEW_NEWGA2, |
Debargha Mukherjee | ae2f3cf | 2017-11-06 18:44:00 -0800 | [diff] [blame] | 199 | THR_COMP_GLOBAL_GLOBALGA2, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 200 | |
Debargha Mukherjee | c1077e9 | 2017-11-06 20:17:33 -0800 | [diff] [blame] | 201 | THR_COMP_NEAR_NEARLL2, |
| 202 | THR_COMP_NEW_NEARESTLL2, |
| 203 | THR_COMP_NEAREST_NEWLL2, |
| 204 | THR_COMP_NEW_NEARLL2, |
| 205 | THR_COMP_NEAR_NEWLL2, |
| 206 | THR_COMP_NEW_NEWLL2, |
| 207 | THR_COMP_GLOBAL_GLOBALLL2, |
| 208 | |
| 209 | THR_COMP_NEAR_NEARLL3, |
| 210 | THR_COMP_NEW_NEARESTLL3, |
| 211 | THR_COMP_NEAREST_NEWLL3, |
| 212 | THR_COMP_NEW_NEARLL3, |
| 213 | THR_COMP_NEAR_NEWLL3, |
| 214 | THR_COMP_NEW_NEWLL3, |
| 215 | THR_COMP_GLOBAL_GLOBALLL3, |
| 216 | |
| 217 | THR_COMP_NEAR_NEARLG, |
| 218 | THR_COMP_NEW_NEARESTLG, |
| 219 | THR_COMP_NEAREST_NEWLG, |
| 220 | THR_COMP_NEW_NEARLG, |
| 221 | THR_COMP_NEAR_NEWLG, |
| 222 | THR_COMP_NEW_NEWLG, |
| 223 | THR_COMP_GLOBAL_GLOBALLG, |
| 224 | |
| 225 | THR_COMP_NEAR_NEARBA, |
| 226 | THR_COMP_NEW_NEARESTBA, |
| 227 | THR_COMP_NEAREST_NEWBA, |
| 228 | THR_COMP_NEW_NEARBA, |
| 229 | THR_COMP_NEAR_NEWBA, |
| 230 | THR_COMP_NEW_NEWBA, |
| 231 | THR_COMP_GLOBAL_GLOBALBA, |
Debargha Mukherjee | c1077e9 | 2017-11-06 20:17:33 -0800 | [diff] [blame] | 232 | |
Peng Bin | bb82e05 | 2018-09-10 12:15:19 +0800 | [diff] [blame] | 233 | THR_DC, |
| 234 | THR_PAETH, |
| 235 | THR_SMOOTH, |
| 236 | THR_SMOOTH_V, |
| 237 | THR_SMOOTH_H, |
| 238 | THR_H_PRED, |
| 239 | THR_V_PRED, |
| 240 | THR_D135_PRED, |
| 241 | THR_D203_PRED, |
| 242 | THR_D157_PRED, |
| 243 | THR_D67_PRED, |
| 244 | THR_D113_PRED, |
| 245 | THR_D45_PRED, |
| 246 | |
| 247 | MAX_MODES, |
| 248 | |
| 249 | LAST_SINGLE_REF_MODES = THR_GLOBALG, |
| 250 | MAX_SINGLE_REF_MODES = LAST_SINGLE_REF_MODES + 1, |
| 251 | LAST_COMP_REF_MODES = THR_COMP_GLOBAL_GLOBALBA, |
| 252 | MAX_COMP_REF_MODES = LAST_COMP_REF_MODES + 1 |
Satish Kumar Suman | 4667aa1 | 2018-12-14 18:28:19 +0530 | [diff] [blame] | 253 | } UENUM1BYTE(THR_MODES); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 254 | |
Satish Kumar Suman | 4667aa1 | 2018-12-14 18:28:19 +0530 | [diff] [blame] | 255 | enum { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 256 | THR_LAST, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 257 | THR_LAST2, |
| 258 | THR_LAST3, |
| 259 | THR_BWDR, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 260 | THR_ALTR2, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 261 | THR_GOLD, |
| 262 | THR_ALTR, |
| 263 | |
| 264 | THR_COMP_LA, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 265 | THR_COMP_L2A, |
| 266 | THR_COMP_L3A, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 267 | THR_COMP_GA, |
| 268 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 269 | THR_COMP_LB, |
| 270 | THR_COMP_L2B, |
| 271 | THR_COMP_L3B, |
| 272 | THR_COMP_GB, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 273 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 274 | THR_COMP_LA2, |
| 275 | THR_COMP_L2A2, |
| 276 | THR_COMP_L3A2, |
| 277 | THR_COMP_GA2, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 278 | |
| 279 | THR_INTRA, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 280 | |
| 281 | MAX_REFS |
Satish Kumar Suman | 4667aa1 | 2018-12-14 18:28:19 +0530 | [diff] [blame] | 282 | } UENUM1BYTE(THR_MODES_SUB8X8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 283 | |
| 284 | typedef struct RD_OPT { |
| 285 | // Thresh_mult is used to set a threshold for the rd score. A higher value |
| 286 | // means that we will accept the best mode so far more often. This number |
| 287 | // is used in combination with the current block size, and thresh_freq_fact |
| 288 | // to pick a threshold. |
| 289 | int thresh_mult[MAX_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 290 | |
Rupert Swarbrick | 93c39e9 | 2017-07-12 11:11:02 +0100 | [diff] [blame] | 291 | int threshes[MAX_SEGMENTS][BLOCK_SIZES_ALL][MAX_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 292 | |
Zoe Liu | 27deb38 | 2018-03-27 15:13:56 -0700 | [diff] [blame] | 293 | int64_t prediction_type_threshes[REF_FRAMES][REFERENCE_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 294 | |
| 295 | int RDMULT; |
Yue Chen | 7cae98f | 2018-08-24 10:43:16 -0700 | [diff] [blame] | 296 | |
Debargha Mukherjee | d0c0b77 | 2019-05-27 23:05:06 -0700 | [diff] [blame] | 297 | double r0, arf_r0; |
Debargha Mukherjee | 88ff738 | 2019-05-01 12:36:10 -0700 | [diff] [blame] | 298 | double mc_saved_base, mc_count_base; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 299 | } RD_OPT; |
| 300 | |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 301 | static INLINE void av1_init_rd_stats(RD_STATS *rd_stats) { |
| 302 | #if CONFIG_RD_DEBUG |
| 303 | int plane; |
| 304 | #endif |
| 305 | rd_stats->rate = 0; |
| 306 | rd_stats->dist = 0; |
| 307 | rd_stats->rdcost = 0; |
| 308 | rd_stats->sse = 0; |
| 309 | rd_stats->skip = 1; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 310 | rd_stats->zero_rate = 0; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 311 | #if CONFIG_RD_DEBUG |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 312 | // This may run into problems when monochrome video is |
| 313 | // encoded, as there will only be 1 plane |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 314 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 315 | rd_stats->txb_coeff_cost[plane] = 0; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 316 | { |
| 317 | int r, c; |
| 318 | for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) |
| 319 | for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) |
| 320 | rd_stats->txb_coeff_cost_map[plane][r][c] = 0; |
| 321 | } |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 322 | } |
| 323 | #endif |
| 324 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 325 | |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 326 | static INLINE void av1_invalid_rd_stats(RD_STATS *rd_stats) { |
| 327 | #if CONFIG_RD_DEBUG |
| 328 | int plane; |
| 329 | #endif |
| 330 | rd_stats->rate = INT_MAX; |
| 331 | rd_stats->dist = INT64_MAX; |
| 332 | rd_stats->rdcost = INT64_MAX; |
| 333 | rd_stats->sse = INT64_MAX; |
| 334 | rd_stats->skip = 0; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 335 | rd_stats->zero_rate = 0; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 336 | #if CONFIG_RD_DEBUG |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 337 | // This may run into problems when monochrome video is |
| 338 | // encoded, as there will only be 1 plane |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 339 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 340 | rd_stats->txb_coeff_cost[plane] = INT_MAX; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 341 | { |
| 342 | int r, c; |
| 343 | for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) |
| 344 | for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) |
| 345 | rd_stats->txb_coeff_cost_map[plane][r][c] = INT_MAX; |
| 346 | } |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 347 | } |
| 348 | #endif |
| 349 | } |
| 350 | |
| 351 | static INLINE void av1_merge_rd_stats(RD_STATS *rd_stats_dst, |
| 352 | const RD_STATS *rd_stats_src) { |
Hui Su | cd73550 | 2019-04-08 11:23:02 -0700 | [diff] [blame] | 353 | assert(rd_stats_dst->rate != INT_MAX && rd_stats_src->rate != INT_MAX); |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 354 | rd_stats_dst->rate += rd_stats_src->rate; |
Debargha Mukherjee | de80e76 | 2017-11-30 13:58:56 -0800 | [diff] [blame] | 355 | if (!rd_stats_dst->zero_rate) |
| 356 | rd_stats_dst->zero_rate = rd_stats_src->zero_rate; |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 357 | rd_stats_dst->dist += rd_stats_src->dist; |
| 358 | rd_stats_dst->sse += rd_stats_src->sse; |
| 359 | rd_stats_dst->skip &= rd_stats_src->skip; |
| 360 | #if CONFIG_RD_DEBUG |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 361 | // This may run into problems when monochrome video is |
| 362 | // encoded, as there will only be 1 plane |
Hui Su | cd73550 | 2019-04-08 11:23:02 -0700 | [diff] [blame] | 363 | for (int plane = 0; plane < MAX_MB_PLANE; ++plane) { |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 364 | 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] | 365 | { |
| 366 | // TODO(angiebird): optimize this part |
| 367 | int r, c; |
| 368 | int ref_txb_coeff_cost = 0; |
| 369 | for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) |
| 370 | for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) { |
| 371 | rd_stats_dst->txb_coeff_cost_map[plane][r][c] += |
| 372 | rd_stats_src->txb_coeff_cost_map[plane][r][c]; |
| 373 | ref_txb_coeff_cost += rd_stats_dst->txb_coeff_cost_map[plane][r][c]; |
| 374 | } |
| 375 | assert(ref_txb_coeff_cost == rd_stats_dst->txb_coeff_cost[plane]); |
| 376 | } |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 377 | } |
| 378 | #endif |
| 379 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 380 | |
sdeng | 256c4d3 | 2019-06-11 12:19:48 -0700 | [diff] [blame^] | 381 | static INLINE int64_t av1_calculate_rd_cost(int mult, int rate, int64_t dist) { |
| 382 | assert(mult >= 0); |
| 383 | if (rate >= 0) { |
| 384 | return RDCOST(mult, rate, dist); |
| 385 | } |
| 386 | return RDCOST_NEG_R(mult, -rate, dist); |
| 387 | } |
| 388 | |
| 389 | static INLINE void av1_rd_cost_update(int mult, RD_STATS *rd_cost) { |
| 390 | if (rd_cost->rate < INT_MAX && rd_cost->dist < INT64_MAX) { |
| 391 | rd_cost->rdcost = av1_calculate_rd_cost(mult, rd_cost->rate, rd_cost->dist); |
| 392 | } else { |
| 393 | av1_invalid_rd_stats(rd_cost); |
| 394 | } |
| 395 | } |
| 396 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 397 | struct TileInfo; |
| 398 | struct TileDataEnc; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 399 | struct AV1_COMP; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 400 | struct macroblock; |
| 401 | |
Yaowu Xu | ea8a6ca | 2018-11-07 14:53:46 -0800 | [diff] [blame] | 402 | 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] | 403 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 404 | int av1_compute_rd_mult(const struct AV1_COMP *cpi, int qindex); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 405 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 406 | void av1_initialize_rd_consts(struct AV1_COMP *cpi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 407 | |
Ravi Chaudhary | 9549205 | 2018-10-24 11:51:28 +0530 | [diff] [blame] | 408 | void av1_initialize_cost_tables(const AV1_COMMON *const cm, MACROBLOCK *x); |
| 409 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 410 | void av1_initialize_me_consts(const struct AV1_COMP *cpi, MACROBLOCK *x, |
| 411 | int qindex); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 412 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 413 | void av1_model_rd_from_var_lapndz(int64_t var, unsigned int n, |
| 414 | unsigned int qstep, int *rate, int64_t *dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 415 | |
Debargha Mukherjee | 0007983 | 2019-01-17 11:28:52 -0800 | [diff] [blame] | 416 | 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] | 417 | double *rate_f, double *distbysse_f); |
Debargha Mukherjee | 0007983 | 2019-01-17 11:28:52 -0800 | [diff] [blame] | 418 | void av1_model_rd_surffit(BLOCK_SIZE bsize, double sse_norm, double xm, |
| 419 | double yl, double *rate_f, double *distbysse_f); |
Debargha Mukherjee | 483e295 | 2018-07-25 07:34:26 -0700 | [diff] [blame] | 420 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 421 | int av1_get_switchable_rate(const AV1_COMMON *const cm, MACROBLOCK *x, |
| 422 | const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 423 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 424 | int av1_raster_block_offset(BLOCK_SIZE plane_bsize, int raster_block, |
| 425 | int stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 426 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 427 | int16_t *av1_raster_block_offset_int16(BLOCK_SIZE plane_bsize, int raster_block, |
| 428 | int16_t *base); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 429 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 430 | YV12_BUFFER_CONFIG *av1_get_scaled_ref_frame(const struct AV1_COMP *cpi, |
| 431 | int ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 432 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 433 | void av1_init_me_luts(void); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 434 | |
Yaowu Xu | 901d622 | 2018-02-21 21:40:48 -0800 | [diff] [blame] | 435 | void av1_set_mvcost(MACROBLOCK *x, int ref, int ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 436 | |
Yunqing Wang | ef2afb4 | 2018-05-01 09:41:00 -0700 | [diff] [blame] | 437 | void av1_get_entropy_contexts(BLOCK_SIZE bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 438 | const struct macroblockd_plane *pd, |
Wan-Teh Chang | 660f8ed | 2018-05-11 17:28:08 -0700 | [diff] [blame] | 439 | ENTROPY_CONTEXT t_above[MAX_MIB_SIZE], |
| 440 | ENTROPY_CONTEXT t_left[MAX_MIB_SIZE]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 441 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 442 | void av1_set_rd_speed_thresholds(struct AV1_COMP *cpi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 443 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 444 | void av1_update_rd_thresh_fact(const AV1_COMMON *const cm, |
| 445 | int (*fact)[MAX_MODES], int rd_thresh, int bsize, |
| 446 | int best_mode_index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 447 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 448 | static INLINE int rd_less_than_thresh(int64_t best_rd, int thresh, |
| 449 | int thresh_fact) { |
| 450 | return best_rd < ((int64_t)thresh * thresh_fact >> 5) || thresh == INT_MAX; |
| 451 | } |
| 452 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 453 | void av1_mv_pred(const struct AV1_COMP *cpi, MACROBLOCK *x, |
| 454 | uint8_t *ref_y_buffer, int ref_y_stride, int ref_frame, |
| 455 | BLOCK_SIZE block_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 456 | |
| 457 | static INLINE void set_error_per_bit(MACROBLOCK *x, int rdmult) { |
| 458 | x->errorperbit = rdmult >> RD_EPB_SHIFT; |
| 459 | x->errorperbit += (x->errorperbit == 0); |
| 460 | } |
| 461 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 462 | void av1_setup_pred_block(const MACROBLOCKD *xd, |
| 463 | struct buf_2d dst[MAX_MB_PLANE], |
| 464 | const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col, |
| 465 | const struct scale_factors *scale, |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 466 | const struct scale_factors *scale_uv, |
| 467 | const int num_planes); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 468 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 469 | int av1_get_intra_cost_penalty(int qindex, int qdelta, |
| 470 | aom_bit_depth_t bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 471 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 472 | void av1_fill_mode_rates(AV1_COMMON *const cm, MACROBLOCK *x, |
| 473 | FRAME_CONTEXT *fc); |
| 474 | |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 475 | void av1_fill_coeff_costs(MACROBLOCK *x, FRAME_CONTEXT *fc, |
| 476 | const int num_planes); |
Jingning Han | dfd7232 | 2017-08-09 14:04:12 -0700 | [diff] [blame] | 477 | |
Yue Chen | 7cae98f | 2018-08-24 10:43:16 -0700 | [diff] [blame] | 478 | int av1_get_adaptive_rdmult(const struct AV1_COMP *cpi, double beta); |
| 479 | |
Debargha Mukherjee | cfcd5ad | 2019-05-20 20:27:17 -0700 | [diff] [blame] | 480 | int av1_get_deltaq_offset(const struct AV1_COMP *cpi, int qindex, double beta); |
| 481 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 482 | #ifdef __cplusplus |
| 483 | } // extern "C" |
| 484 | #endif |
| 485 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 486 | #endif // AOM_AV1_ENCODER_RD_H_ |