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 | |
| 12 | #include <assert.h> |
| 13 | #include <math.h> |
| 14 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "./aom_dsp_rtcd.h" |
Jingning Han | 1aab818 | 2016-06-03 11:09:06 -0700 | [diff] [blame] | 16 | #include "./av1_rtcd.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 17 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | #include "aom_dsp/aom_dsp_common.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 19 | #include "aom_dsp/blend.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 20 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 21 | #include "aom_ports/mem.h" |
| 22 | #include "aom_ports/system_state.h" |
| 23 | |
| 24 | #include "av1/common/common.h" |
| 25 | #include "av1/common/common_data.h" |
| 26 | #include "av1/common/entropy.h" |
| 27 | #include "av1/common/entropymode.h" |
| 28 | #include "av1/common/idct.h" |
| 29 | #include "av1/common/mvref_common.h" |
| 30 | #include "av1/common/pred_common.h" |
| 31 | #include "av1/common/quant_common.h" |
| 32 | #include "av1/common/reconinter.h" |
| 33 | #include "av1/common/reconintra.h" |
| 34 | #include "av1/common/scan.h" |
| 35 | #include "av1/common/seg_common.h" |
| 36 | |
Jingning Han | 1aab818 | 2016-06-03 11:09:06 -0700 | [diff] [blame] | 37 | #include "av1/encoder/aq_variance.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 38 | #include "av1/encoder/cost.h" |
| 39 | #include "av1/encoder/encodemb.h" |
| 40 | #include "av1/encoder/encodemv.h" |
| 41 | #include "av1/encoder/encoder.h" |
| 42 | #include "av1/encoder/hybrid_fwd_txfm.h" |
| 43 | #include "av1/encoder/mcomp.h" |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 44 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 45 | #include "av1/encoder/palette.h" |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 46 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 47 | #include "av1/encoder/quantize.h" |
| 48 | #include "av1/encoder/ratectrl.h" |
| 49 | #include "av1/encoder/rd.h" |
| 50 | #include "av1/encoder/rdopt.h" |
Debargha Mukherjee | ceebb70 | 2016-10-11 05:26:50 -0700 | [diff] [blame] | 51 | #include "av1/encoder/tokenize.h" |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 52 | #if CONFIG_PVQ |
| 53 | #include "av1/encoder/pvq_encoder.h" |
| 54 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 55 | #if CONFIG_DUAL_FILTER |
| 56 | #if CONFIG_EXT_INTERP |
| 57 | static const int filter_sets[25][2] = { |
| 58 | { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 1, 0 }, { 1, 1 }, |
| 59 | { 1, 2 }, { 1, 3 }, { 1, 4 }, { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, |
| 60 | { 2, 4 }, { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }, { 3, 4 }, { 4, 0 }, |
| 61 | { 4, 1 }, { 4, 2 }, { 4, 3 }, { 4, 4 }, |
| 62 | }; |
| 63 | #else |
| 64 | static const int filter_sets[9][2] = { |
| 65 | { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 }, |
| 66 | { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 }, |
| 67 | }; |
| 68 | #endif |
| 69 | #endif |
| 70 | |
| 71 | #if CONFIG_EXT_REFS |
| 72 | |
| 73 | #define LAST_FRAME_MODE_MASK \ |
| 74 | ((1 << INTRA_FRAME) | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | \ |
| 75 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF_FRAME)) |
| 76 | #define LAST2_FRAME_MODE_MASK \ |
| 77 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST3_FRAME) | \ |
| 78 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF_FRAME)) |
| 79 | #define LAST3_FRAME_MODE_MASK \ |
| 80 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 81 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF_FRAME)) |
| 82 | #define GOLDEN_FRAME_MODE_MASK \ |
| 83 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 84 | (1 << LAST3_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF_FRAME)) |
| 85 | #define BWDREF_FRAME_MODE_MASK \ |
| 86 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 87 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME)) |
| 88 | #define ALTREF_FRAME_MODE_MASK \ |
| 89 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 90 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME)) |
| 91 | |
| 92 | #else |
| 93 | |
| 94 | #define LAST_FRAME_MODE_MASK \ |
| 95 | ((1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME) | (1 << INTRA_FRAME)) |
| 96 | #define GOLDEN_FRAME_MODE_MASK \ |
| 97 | ((1 << LAST_FRAME) | (1 << ALTREF_FRAME) | (1 << INTRA_FRAME)) |
| 98 | #define ALTREF_FRAME_MODE_MASK \ |
| 99 | ((1 << LAST_FRAME) | (1 << GOLDEN_FRAME) | (1 << INTRA_FRAME)) |
| 100 | |
| 101 | #endif // CONFIG_EXT_REFS |
| 102 | |
| 103 | #if CONFIG_EXT_REFS |
| 104 | #define SECOND_REF_FRAME_MASK ((1 << ALTREF_FRAME) | (1 << BWDREF_FRAME) | 0x01) |
| 105 | #else |
| 106 | #define SECOND_REF_FRAME_MASK ((1 << ALTREF_FRAME) | 0x01) |
| 107 | #endif // CONFIG_EXT_REFS |
| 108 | |
| 109 | #define MIN_EARLY_TERM_INDEX 3 |
| 110 | #define NEW_MV_DISCOUNT_FACTOR 8 |
| 111 | |
| 112 | #if CONFIG_EXT_INTRA |
| 113 | #define ANGLE_FAST_SEARCH 1 |
| 114 | #define ANGLE_SKIP_THRESH 10 |
| 115 | #define FILTER_FAST_SEARCH 1 |
| 116 | #endif // CONFIG_EXT_INTRA |
| 117 | |
| 118 | const double ADST_FLIP_SVM[8] = { -6.6623, -2.8062, -3.2531, 3.1671, // vert |
| 119 | -7.7051, -3.2234, -3.6193, 3.4533 }; // horz |
| 120 | |
| 121 | typedef struct { |
| 122 | PREDICTION_MODE mode; |
| 123 | MV_REFERENCE_FRAME ref_frame[2]; |
| 124 | } MODE_DEFINITION; |
| 125 | |
| 126 | typedef struct { MV_REFERENCE_FRAME ref_frame[2]; } REF_DEFINITION; |
| 127 | |
| 128 | struct rdcost_block_args { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 129 | const AV1_COMP *cpi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 130 | MACROBLOCK *x; |
| 131 | ENTROPY_CONTEXT t_above[2 * MAX_MIB_SIZE]; |
| 132 | ENTROPY_CONTEXT t_left[2 * MAX_MIB_SIZE]; |
| 133 | int this_rate; |
| 134 | int64_t this_dist; |
| 135 | int64_t this_sse; |
| 136 | int64_t this_rd; |
| 137 | int64_t best_rd; |
| 138 | int exit_early; |
| 139 | int use_fast_coef_costing; |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 140 | const SCAN_ORDER *scan_order; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 141 | uint8_t skippable; |
| 142 | }; |
| 143 | |
| 144 | #define LAST_NEW_MV_INDEX 6 |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 145 | static const MODE_DEFINITION av1_mode_order[MAX_MODES] = { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 146 | { NEARESTMV, { LAST_FRAME, NONE } }, |
| 147 | #if CONFIG_EXT_REFS |
| 148 | { NEARESTMV, { LAST2_FRAME, NONE } }, |
| 149 | { NEARESTMV, { LAST3_FRAME, NONE } }, |
| 150 | { NEARESTMV, { BWDREF_FRAME, NONE } }, |
| 151 | #endif // CONFIG_EXT_REFS |
| 152 | { NEARESTMV, { ALTREF_FRAME, NONE } }, |
| 153 | { NEARESTMV, { GOLDEN_FRAME, NONE } }, |
| 154 | |
| 155 | { DC_PRED, { INTRA_FRAME, NONE } }, |
| 156 | |
| 157 | { NEWMV, { LAST_FRAME, NONE } }, |
| 158 | #if CONFIG_EXT_REFS |
| 159 | { NEWMV, { LAST2_FRAME, NONE } }, |
| 160 | { NEWMV, { LAST3_FRAME, NONE } }, |
| 161 | { NEWMV, { BWDREF_FRAME, NONE } }, |
| 162 | #endif // CONFIG_EXT_REFS |
| 163 | { NEWMV, { ALTREF_FRAME, NONE } }, |
| 164 | { NEWMV, { GOLDEN_FRAME, NONE } }, |
| 165 | |
| 166 | { NEARMV, { LAST_FRAME, NONE } }, |
| 167 | #if CONFIG_EXT_REFS |
| 168 | { NEARMV, { LAST2_FRAME, NONE } }, |
| 169 | { NEARMV, { LAST3_FRAME, NONE } }, |
| 170 | { NEARMV, { BWDREF_FRAME, NONE } }, |
| 171 | #endif // CONFIG_EXT_REFS |
| 172 | { NEARMV, { ALTREF_FRAME, NONE } }, |
| 173 | { NEARMV, { GOLDEN_FRAME, NONE } }, |
| 174 | |
| 175 | #if CONFIG_EXT_INTER |
| 176 | { NEWFROMNEARMV, { LAST_FRAME, NONE } }, |
| 177 | #if CONFIG_EXT_REFS |
| 178 | { NEWFROMNEARMV, { LAST2_FRAME, NONE } }, |
| 179 | { NEWFROMNEARMV, { LAST3_FRAME, NONE } }, |
| 180 | { NEWFROMNEARMV, { BWDREF_FRAME, NONE } }, |
| 181 | #endif // CONFIG_EXT_REFS |
| 182 | { NEWFROMNEARMV, { ALTREF_FRAME, NONE } }, |
| 183 | { NEWFROMNEARMV, { GOLDEN_FRAME, NONE } }, |
| 184 | #endif // CONFIG_EXT_INTER |
| 185 | |
| 186 | { ZEROMV, { LAST_FRAME, NONE } }, |
| 187 | #if CONFIG_EXT_REFS |
| 188 | { ZEROMV, { LAST2_FRAME, NONE } }, |
| 189 | { ZEROMV, { LAST3_FRAME, NONE } }, |
| 190 | { ZEROMV, { BWDREF_FRAME, NONE } }, |
| 191 | #endif // CONFIG_EXT_REFS |
| 192 | { ZEROMV, { GOLDEN_FRAME, NONE } }, |
| 193 | { ZEROMV, { ALTREF_FRAME, NONE } }, |
| 194 | |
| 195 | // TODO(zoeliu): May need to reconsider the order on the modes to check |
| 196 | |
| 197 | #if CONFIG_EXT_INTER |
| 198 | { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 199 | #if CONFIG_EXT_REFS |
| 200 | { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 201 | { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 202 | #endif // CONFIG_EXT_REFS |
| 203 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 204 | #if CONFIG_EXT_REFS |
| 205 | { NEAREST_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 206 | { NEAREST_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 207 | { NEAREST_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 208 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 209 | #endif // CONFIG_EXT_REFS |
| 210 | |
| 211 | #else // CONFIG_EXT_INTER |
| 212 | |
| 213 | { NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 214 | #if CONFIG_EXT_REFS |
| 215 | { NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 216 | { NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 217 | #endif // CONFIG_EXT_REFS |
| 218 | { NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 219 | #if CONFIG_EXT_REFS |
| 220 | { NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 221 | { NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 222 | { NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 223 | { NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 224 | #endif // CONFIG_EXT_REFS |
| 225 | #endif // CONFIG_EXT_INTER |
| 226 | |
| 227 | { TM_PRED, { INTRA_FRAME, NONE } }, |
| 228 | |
| 229 | #if CONFIG_EXT_INTER |
| 230 | { NEAR_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 231 | { NEAREST_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 232 | { NEAR_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 233 | { NEW_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 234 | { NEAREST_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 235 | { NEW_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 236 | { NEAR_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 237 | { NEW_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 238 | { ZERO_ZEROMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 239 | |
| 240 | #if CONFIG_EXT_REFS |
| 241 | { NEAR_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 242 | { NEAREST_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 243 | { NEAR_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 244 | { NEW_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 245 | { NEAREST_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 246 | { NEW_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 247 | { NEAR_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 248 | { NEW_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 249 | { ZERO_ZEROMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 250 | |
| 251 | { NEAR_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 252 | { NEAREST_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 253 | { NEAR_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 254 | { NEW_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 255 | { NEAREST_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 256 | { NEW_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 257 | { NEAR_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 258 | { NEW_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 259 | { ZERO_ZEROMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 260 | #endif // CONFIG_EXT_REFS |
| 261 | |
| 262 | { NEAR_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 263 | { NEAREST_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 264 | { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 265 | { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 266 | { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 267 | { NEW_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 268 | { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 269 | { NEW_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 270 | { ZERO_ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 271 | |
| 272 | #if CONFIG_EXT_REFS |
| 273 | { NEAR_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 274 | { NEAREST_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 275 | { NEAR_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 276 | { NEW_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 277 | { NEAREST_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 278 | { NEW_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 279 | { NEAR_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 280 | { NEW_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 281 | { ZERO_ZEROMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 282 | |
| 283 | { NEAR_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 284 | { NEAREST_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 285 | { NEAR_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 286 | { NEW_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 287 | { NEAREST_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 288 | { NEW_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 289 | { NEAR_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 290 | { NEW_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 291 | { ZERO_ZEROMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 292 | |
| 293 | { NEAR_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 294 | { NEAREST_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 295 | { NEAR_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 296 | { NEW_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 297 | { NEAREST_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 298 | { NEW_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 299 | { NEAR_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 300 | { NEW_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 301 | { ZERO_ZEROMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 302 | |
| 303 | { NEAR_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 304 | { NEAREST_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 305 | { NEAR_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 306 | { NEW_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 307 | { NEAREST_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 308 | { NEW_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 309 | { NEAR_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 310 | { NEW_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 311 | { ZERO_ZEROMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 312 | #endif // CONFIG_EXT_REFS |
| 313 | |
| 314 | #else // CONFIG_EXT_INTER |
| 315 | |
| 316 | { NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 317 | { NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 318 | #if CONFIG_EXT_REFS |
| 319 | { NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 320 | { NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 321 | { NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 322 | { NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 323 | #endif // CONFIG_EXT_REFS |
| 324 | { NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 325 | { NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 326 | |
| 327 | #if CONFIG_EXT_REFS |
| 328 | { NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 329 | { NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 330 | { NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 331 | { NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 332 | { NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 333 | { NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 334 | { NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 335 | { NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 336 | #endif // CONFIG_EXT_REFS |
| 337 | |
| 338 | { ZEROMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 339 | #if CONFIG_EXT_REFS |
| 340 | { ZEROMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 341 | { ZEROMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 342 | #endif // CONFIG_EXT_REFS |
| 343 | { ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 344 | |
| 345 | #if CONFIG_EXT_REFS |
| 346 | { ZEROMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 347 | { ZEROMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 348 | { ZEROMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 349 | { ZEROMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 350 | #endif // CONFIG_EXT_REFS |
| 351 | |
| 352 | #endif // CONFIG_EXT_INTER |
| 353 | |
| 354 | { H_PRED, { INTRA_FRAME, NONE } }, |
| 355 | { V_PRED, { INTRA_FRAME, NONE } }, |
| 356 | { D135_PRED, { INTRA_FRAME, NONE } }, |
| 357 | { D207_PRED, { INTRA_FRAME, NONE } }, |
| 358 | { D153_PRED, { INTRA_FRAME, NONE } }, |
| 359 | { D63_PRED, { INTRA_FRAME, NONE } }, |
| 360 | { D117_PRED, { INTRA_FRAME, NONE } }, |
| 361 | { D45_PRED, { INTRA_FRAME, NONE } }, |
| 362 | |
| 363 | #if CONFIG_EXT_INTER |
| 364 | { ZEROMV, { LAST_FRAME, INTRA_FRAME } }, |
| 365 | { NEARESTMV, { LAST_FRAME, INTRA_FRAME } }, |
| 366 | { NEARMV, { LAST_FRAME, INTRA_FRAME } }, |
| 367 | { NEWMV, { LAST_FRAME, INTRA_FRAME } }, |
| 368 | |
| 369 | #if CONFIG_EXT_REFS |
| 370 | { ZEROMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 371 | { NEARESTMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 372 | { NEARMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 373 | { NEWMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 374 | |
| 375 | { ZEROMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 376 | { NEARESTMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 377 | { NEARMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 378 | { NEWMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 379 | #endif // CONFIG_EXT_REFS |
| 380 | |
| 381 | { ZEROMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 382 | { NEARESTMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 383 | { NEARMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 384 | { NEWMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 385 | |
| 386 | #if CONFIG_EXT_REFS |
| 387 | { ZEROMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 388 | { NEARESTMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 389 | { NEARMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 390 | { NEWMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 391 | #endif // CONFIG_EXT_REFS |
| 392 | |
| 393 | { ZEROMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 394 | { NEARESTMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 395 | { NEARMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 396 | { NEWMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 397 | #endif // CONFIG_EXT_INTER |
| 398 | }; |
| 399 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 400 | static const REF_DEFINITION av1_ref_order[MAX_REFS] = { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 401 | { { LAST_FRAME, NONE } }, |
| 402 | #if CONFIG_EXT_REFS |
| 403 | { { LAST2_FRAME, NONE } }, { { LAST3_FRAME, NONE } }, |
| 404 | { { BWDREF_FRAME, NONE } }, |
| 405 | #endif // CONFIG_EXT_REFS |
| 406 | { { GOLDEN_FRAME, NONE } }, { { ALTREF_FRAME, NONE } }, |
| 407 | |
| 408 | { { LAST_FRAME, ALTREF_FRAME } }, |
| 409 | #if CONFIG_EXT_REFS |
| 410 | { { LAST2_FRAME, ALTREF_FRAME } }, { { LAST3_FRAME, ALTREF_FRAME } }, |
| 411 | #endif // CONFIG_EXT_REFS |
| 412 | { { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 413 | |
| 414 | #if CONFIG_EXT_REFS |
| 415 | { { LAST_FRAME, BWDREF_FRAME } }, { { LAST2_FRAME, BWDREF_FRAME } }, |
| 416 | { { LAST3_FRAME, BWDREF_FRAME } }, { { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 417 | #endif // CONFIG_EXT_REFS |
| 418 | |
| 419 | { { INTRA_FRAME, NONE } }, |
| 420 | }; |
| 421 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 422 | #if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 423 | static INLINE int write_uniform_cost(int n, int v) { |
| 424 | int l = get_unsigned_bits(n), m = (1 << l) - n; |
| 425 | if (l == 0) return 0; |
| 426 | if (v < m) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 427 | return (l - 1) * av1_cost_bit(128, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 428 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 429 | return l * av1_cost_bit(128, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 430 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 431 | #endif // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 432 | |
| 433 | // constants for prune 1 and prune 2 decision boundaries |
| 434 | #define FAST_EXT_TX_CORR_MID 0.0 |
| 435 | #define FAST_EXT_TX_EDST_MID 0.1 |
| 436 | #define FAST_EXT_TX_CORR_MARGIN 0.5 |
| 437 | #define FAST_EXT_TX_EDST_MARGIN 0.3 |
| 438 | |
| 439 | static const TX_TYPE_1D vtx_tab[TX_TYPES] = { |
| 440 | DCT_1D, ADST_1D, DCT_1D, ADST_1D, |
| 441 | #if CONFIG_EXT_TX |
| 442 | FLIPADST_1D, DCT_1D, FLIPADST_1D, ADST_1D, FLIPADST_1D, IDTX_1D, |
| 443 | DCT_1D, IDTX_1D, ADST_1D, IDTX_1D, FLIPADST_1D, IDTX_1D, |
| 444 | #endif // CONFIG_EXT_TX |
| 445 | }; |
| 446 | |
| 447 | static const TX_TYPE_1D htx_tab[TX_TYPES] = { |
| 448 | DCT_1D, DCT_1D, ADST_1D, ADST_1D, |
| 449 | #if CONFIG_EXT_TX |
| 450 | DCT_1D, FLIPADST_1D, FLIPADST_1D, FLIPADST_1D, ADST_1D, IDTX_1D, |
| 451 | IDTX_1D, DCT_1D, IDTX_1D, ADST_1D, IDTX_1D, FLIPADST_1D, |
| 452 | #endif // CONFIG_EXT_TX |
| 453 | }; |
| 454 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 455 | static void get_energy_distribution_fine(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 456 | uint8_t *src, int src_stride, |
| 457 | uint8_t *dst, int dst_stride, |
| 458 | double *hordist, double *verdist) { |
| 459 | int bw = 4 << (b_width_log2_lookup[bsize]); |
| 460 | int bh = 4 << (b_height_log2_lookup[bsize]); |
| 461 | unsigned int esq[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 462 | unsigned int var[16]; |
| 463 | double total = 0; |
| 464 | |
| 465 | const int f_index = bsize - BLOCK_16X16; |
| 466 | if (f_index < 0) { |
| 467 | int i, j, index; |
| 468 | int w_shift = bw == 8 ? 1 : 2; |
| 469 | int h_shift = bh == 8 ? 1 : 2; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 470 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 471 | if (cpi->common.use_highbitdepth) { |
| 472 | uint16_t *src16 = CONVERT_TO_SHORTPTR(src); |
| 473 | uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst); |
| 474 | for (i = 0; i < bh; ++i) |
| 475 | for (j = 0; j < bw; ++j) { |
| 476 | index = (j >> w_shift) + ((i >> h_shift) << 2); |
| 477 | esq[index] += |
| 478 | (src16[j + i * src_stride] - dst16[j + i * dst_stride]) * |
| 479 | (src16[j + i * src_stride] - dst16[j + i * dst_stride]); |
| 480 | } |
| 481 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 482 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 483 | |
| 484 | for (i = 0; i < bh; ++i) |
| 485 | for (j = 0; j < bw; ++j) { |
| 486 | index = (j >> w_shift) + ((i >> h_shift) << 2); |
| 487 | esq[index] += (src[j + i * src_stride] - dst[j + i * dst_stride]) * |
| 488 | (src[j + i * src_stride] - dst[j + i * dst_stride]); |
| 489 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 490 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 491 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 492 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 493 | } else { |
| 494 | var[0] = cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[0]); |
| 495 | var[1] = cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, |
| 496 | dst_stride, &esq[1]); |
| 497 | var[2] = cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, |
| 498 | dst_stride, &esq[2]); |
| 499 | var[3] = cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, |
| 500 | dst + 3 * bw / 4, dst_stride, &esq[3]); |
| 501 | src += bh / 4 * src_stride; |
| 502 | dst += bh / 4 * dst_stride; |
| 503 | |
| 504 | var[4] = cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[4]); |
| 505 | var[5] = cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, |
| 506 | dst_stride, &esq[5]); |
| 507 | var[6] = cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, |
| 508 | dst_stride, &esq[6]); |
| 509 | var[7] = cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, |
| 510 | dst + 3 * bw / 4, dst_stride, &esq[7]); |
| 511 | src += bh / 4 * src_stride; |
| 512 | dst += bh / 4 * dst_stride; |
| 513 | |
| 514 | var[8] = cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[8]); |
| 515 | var[9] = cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, |
| 516 | dst_stride, &esq[9]); |
| 517 | var[10] = cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, |
| 518 | dst_stride, &esq[10]); |
| 519 | var[11] = cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, |
| 520 | dst + 3 * bw / 4, dst_stride, &esq[11]); |
| 521 | src += bh / 4 * src_stride; |
| 522 | dst += bh / 4 * dst_stride; |
| 523 | |
| 524 | var[12] = |
| 525 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[12]); |
| 526 | var[13] = cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, |
| 527 | dst_stride, &esq[13]); |
| 528 | var[14] = cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, |
| 529 | dst_stride, &esq[14]); |
| 530 | var[15] = cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, |
| 531 | dst + 3 * bw / 4, dst_stride, &esq[15]); |
| 532 | } |
| 533 | |
| 534 | total = esq[0] + esq[1] + esq[2] + esq[3] + esq[4] + esq[5] + esq[6] + |
| 535 | esq[7] + esq[8] + esq[9] + esq[10] + esq[11] + esq[12] + esq[13] + |
| 536 | esq[14] + esq[15]; |
| 537 | if (total > 0) { |
| 538 | const double e_recip = 1.0 / total; |
| 539 | hordist[0] = |
| 540 | ((double)esq[0] + (double)esq[4] + (double)esq[8] + (double)esq[12]) * |
| 541 | e_recip; |
| 542 | hordist[1] = |
| 543 | ((double)esq[1] + (double)esq[5] + (double)esq[9] + (double)esq[13]) * |
| 544 | e_recip; |
| 545 | hordist[2] = |
| 546 | ((double)esq[2] + (double)esq[6] + (double)esq[10] + (double)esq[14]) * |
| 547 | e_recip; |
| 548 | verdist[0] = |
| 549 | ((double)esq[0] + (double)esq[1] + (double)esq[2] + (double)esq[3]) * |
| 550 | e_recip; |
| 551 | verdist[1] = |
| 552 | ((double)esq[4] + (double)esq[5] + (double)esq[6] + (double)esq[7]) * |
| 553 | e_recip; |
| 554 | verdist[2] = |
| 555 | ((double)esq[8] + (double)esq[9] + (double)esq[10] + (double)esq[11]) * |
| 556 | e_recip; |
| 557 | } else { |
| 558 | hordist[0] = verdist[0] = 0.25; |
| 559 | hordist[1] = verdist[1] = 0.25; |
| 560 | hordist[2] = verdist[2] = 0.25; |
| 561 | } |
| 562 | (void)var[0]; |
| 563 | (void)var[1]; |
| 564 | (void)var[2]; |
| 565 | (void)var[3]; |
| 566 | (void)var[4]; |
| 567 | (void)var[5]; |
| 568 | (void)var[6]; |
| 569 | (void)var[7]; |
| 570 | (void)var[8]; |
| 571 | (void)var[9]; |
| 572 | (void)var[10]; |
| 573 | (void)var[11]; |
| 574 | (void)var[12]; |
| 575 | (void)var[13]; |
| 576 | (void)var[14]; |
| 577 | (void)var[15]; |
| 578 | } |
| 579 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 580 | static int adst_vs_flipadst(const AV1_COMP *cpi, BLOCK_SIZE bsize, uint8_t *src, |
| 581 | int src_stride, uint8_t *dst, int dst_stride, |
| 582 | double *hdist, double *vdist) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 583 | int prune_bitmask = 0; |
| 584 | double svm_proj_h = 0, svm_proj_v = 0; |
| 585 | get_energy_distribution_fine(cpi, bsize, src, src_stride, dst, dst_stride, |
| 586 | hdist, vdist); |
| 587 | |
| 588 | svm_proj_v = vdist[0] * ADST_FLIP_SVM[0] + vdist[1] * ADST_FLIP_SVM[1] + |
| 589 | vdist[2] * ADST_FLIP_SVM[2] + ADST_FLIP_SVM[3]; |
| 590 | svm_proj_h = hdist[0] * ADST_FLIP_SVM[4] + hdist[1] * ADST_FLIP_SVM[5] + |
| 591 | hdist[2] * ADST_FLIP_SVM[6] + ADST_FLIP_SVM[7]; |
| 592 | if (svm_proj_v > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN) |
| 593 | prune_bitmask |= 1 << FLIPADST_1D; |
| 594 | else if (svm_proj_v < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN) |
| 595 | prune_bitmask |= 1 << ADST_1D; |
| 596 | |
| 597 | if (svm_proj_h > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN) |
| 598 | prune_bitmask |= 1 << (FLIPADST_1D + 8); |
| 599 | else if (svm_proj_h < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN) |
| 600 | prune_bitmask |= 1 << (ADST_1D + 8); |
| 601 | |
| 602 | return prune_bitmask; |
| 603 | } |
| 604 | |
| 605 | #if CONFIG_EXT_TX |
| 606 | static void get_horver_correlation(int16_t *diff, int stride, int w, int h, |
| 607 | double *hcorr, double *vcorr) { |
| 608 | // Returns hor/ver correlation coefficient |
| 609 | const int num = (h - 1) * (w - 1); |
| 610 | double num_r; |
| 611 | int i, j; |
| 612 | int64_t xy_sum = 0, xz_sum = 0; |
| 613 | int64_t x_sum = 0, y_sum = 0, z_sum = 0; |
| 614 | int64_t x2_sum = 0, y2_sum = 0, z2_sum = 0; |
| 615 | double x_var_n, y_var_n, z_var_n, xy_var_n, xz_var_n; |
| 616 | *hcorr = *vcorr = 1; |
| 617 | |
| 618 | assert(num > 0); |
| 619 | num_r = 1.0 / num; |
| 620 | for (i = 1; i < h; ++i) { |
| 621 | for (j = 1; j < w; ++j) { |
| 622 | const int16_t x = diff[i * stride + j]; |
| 623 | const int16_t y = diff[i * stride + j - 1]; |
| 624 | const int16_t z = diff[(i - 1) * stride + j]; |
| 625 | xy_sum += x * y; |
| 626 | xz_sum += x * z; |
| 627 | x_sum += x; |
| 628 | y_sum += y; |
| 629 | z_sum += z; |
| 630 | x2_sum += x * x; |
| 631 | y2_sum += y * y; |
| 632 | z2_sum += z * z; |
| 633 | } |
| 634 | } |
| 635 | x_var_n = x2_sum - (x_sum * x_sum) * num_r; |
| 636 | y_var_n = y2_sum - (y_sum * y_sum) * num_r; |
| 637 | z_var_n = z2_sum - (z_sum * z_sum) * num_r; |
| 638 | xy_var_n = xy_sum - (x_sum * y_sum) * num_r; |
| 639 | xz_var_n = xz_sum - (x_sum * z_sum) * num_r; |
| 640 | if (x_var_n > 0 && y_var_n > 0) { |
| 641 | *hcorr = xy_var_n / sqrt(x_var_n * y_var_n); |
| 642 | *hcorr = *hcorr < 0 ? 0 : *hcorr; |
| 643 | } |
| 644 | if (x_var_n > 0 && z_var_n > 0) { |
| 645 | *vcorr = xz_var_n / sqrt(x_var_n * z_var_n); |
| 646 | *vcorr = *vcorr < 0 ? 0 : *vcorr; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | int dct_vs_idtx(int16_t *diff, int stride, int w, int h, double *hcorr, |
| 651 | double *vcorr) { |
| 652 | int prune_bitmask = 0; |
| 653 | get_horver_correlation(diff, stride, w, h, hcorr, vcorr); |
| 654 | |
| 655 | if (*vcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN) |
| 656 | prune_bitmask |= 1 << IDTX_1D; |
| 657 | else if (*vcorr < FAST_EXT_TX_CORR_MID - FAST_EXT_TX_CORR_MARGIN) |
| 658 | prune_bitmask |= 1 << DCT_1D; |
| 659 | |
| 660 | if (*hcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN) |
| 661 | prune_bitmask |= 1 << (IDTX_1D + 8); |
| 662 | else if (*hcorr < FAST_EXT_TX_CORR_MID - FAST_EXT_TX_CORR_MARGIN) |
| 663 | prune_bitmask |= 1 << (DCT_1D + 8); |
| 664 | return prune_bitmask; |
| 665 | } |
| 666 | |
| 667 | // Performance drop: 0.5%, Speed improvement: 24% |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 668 | static int prune_two_for_sby(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 669 | MACROBLOCK *x, MACROBLOCKD *xd, int adst_flipadst, |
| 670 | int dct_idtx) { |
| 671 | struct macroblock_plane *const p = &x->plane[0]; |
| 672 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 673 | const BLOCK_SIZE bs = get_plane_block_size(bsize, pd); |
| 674 | const int bw = 4 << (b_width_log2_lookup[bs]); |
| 675 | const int bh = 4 << (b_height_log2_lookup[bs]); |
| 676 | double hdist[3] = { 0, 0, 0 }, vdist[3] = { 0, 0, 0 }; |
| 677 | double hcorr, vcorr; |
| 678 | int prune = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 679 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 680 | |
| 681 | if (adst_flipadst) |
| 682 | prune |= adst_vs_flipadst(cpi, bsize, p->src.buf, p->src.stride, |
| 683 | pd->dst.buf, pd->dst.stride, hdist, vdist); |
| 684 | if (dct_idtx) prune |= dct_vs_idtx(p->src_diff, bw, bw, bh, &hcorr, &vcorr); |
| 685 | |
| 686 | return prune; |
| 687 | } |
| 688 | #endif // CONFIG_EXT_TX |
| 689 | |
| 690 | // Performance drop: 0.3%, Speed improvement: 5% |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 691 | static int prune_one_for_sby(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 692 | MACROBLOCK *x, MACROBLOCKD *xd) { |
| 693 | struct macroblock_plane *const p = &x->plane[0]; |
| 694 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 695 | double hdist[3] = { 0, 0, 0 }, vdist[3] = { 0, 0, 0 }; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 696 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 697 | return adst_vs_flipadst(cpi, bsize, p->src.buf, p->src.stride, pd->dst.buf, |
| 698 | pd->dst.stride, hdist, vdist); |
| 699 | } |
| 700 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 701 | static int prune_tx_types(const AV1_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 702 | MACROBLOCKD *xd, int tx_set) { |
| 703 | #if CONFIG_EXT_TX |
| 704 | const int *tx_set_1D = ext_tx_used_inter_1D[tx_set]; |
| 705 | #else |
| 706 | const int tx_set_1D[TX_TYPES_1D] = { 0 }; |
| 707 | #endif |
| 708 | |
| 709 | switch (cpi->sf.tx_type_search.prune_mode) { |
| 710 | case NO_PRUNE: return 0; break; |
| 711 | case PRUNE_ONE: |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 712 | if ((tx_set >= 0) && !(tx_set_1D[FLIPADST_1D] & tx_set_1D[ADST_1D])) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 713 | return 0; |
| 714 | return prune_one_for_sby(cpi, bsize, x, xd); |
| 715 | break; |
| 716 | #if CONFIG_EXT_TX |
| 717 | case PRUNE_TWO: |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 718 | if ((tx_set >= 0) && !(tx_set_1D[FLIPADST_1D] & tx_set_1D[ADST_1D])) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 719 | if (!(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D])) return 0; |
| 720 | return prune_two_for_sby(cpi, bsize, x, xd, 0, 1); |
| 721 | } |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 722 | if ((tx_set >= 0) && !(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D])) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 723 | return prune_two_for_sby(cpi, bsize, x, xd, 1, 0); |
| 724 | return prune_two_for_sby(cpi, bsize, x, xd, 1, 1); |
| 725 | break; |
| 726 | #endif |
| 727 | } |
| 728 | assert(0); |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int do_tx_type_search(TX_TYPE tx_type, int prune) { |
| 733 | // TODO(sarahparker) implement for non ext tx |
| 734 | #if CONFIG_EXT_TX |
| 735 | return !(((prune >> vtx_tab[tx_type]) & 1) | |
| 736 | ((prune >> (htx_tab[tx_type] + 8)) & 1)); |
| 737 | #else |
| 738 | // temporary to avoid compiler warnings |
| 739 | (void)vtx_tab; |
| 740 | (void)htx_tab; |
| 741 | (void)tx_type; |
| 742 | (void)prune; |
| 743 | return 1; |
| 744 | #endif |
| 745 | } |
| 746 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 747 | static void model_rd_from_sse(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 748 | const MACROBLOCKD *const xd, BLOCK_SIZE bsize, |
| 749 | int plane, int64_t sse, int *rate, |
| 750 | int64_t *dist) { |
| 751 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 752 | const int dequant_shift = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 753 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 754 | (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd - 5 : |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 755 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 756 | 3; |
| 757 | |
| 758 | // Fast approximate the modelling function. |
| 759 | if (cpi->sf.simple_model_rd_from_var) { |
| 760 | const int64_t square_error = sse; |
| 761 | int quantizer = (pd->dequant[1] >> dequant_shift); |
| 762 | |
| 763 | if (quantizer < 120) |
| 764 | *rate = (int)((square_error * (280 - quantizer)) >> |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 765 | (16 - AV1_PROB_COST_SHIFT)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 766 | else |
| 767 | *rate = 0; |
| 768 | *dist = (square_error * quantizer) >> 8; |
| 769 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 770 | av1_model_rd_from_var_lapndz(sse, num_pels_log2_lookup[bsize], |
| 771 | pd->dequant[1] >> dequant_shift, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | *dist <<= 4; |
| 775 | } |
| 776 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 777 | static void model_rd_for_sb(const AV1_COMP *const cpi, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 778 | MACROBLOCK *x, MACROBLOCKD *xd, int plane_from, |
| 779 | int plane_to, int *out_rate_sum, |
| 780 | int64_t *out_dist_sum, int *skip_txfm_sb, |
| 781 | int64_t *skip_sse_sb) { |
| 782 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 783 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 784 | // we need to divide by 8 before sending to modeling function. |
| 785 | int plane; |
| 786 | const int ref = xd->mi[0]->mbmi.ref_frame[0]; |
| 787 | |
| 788 | int64_t rate_sum = 0; |
| 789 | int64_t dist_sum = 0; |
| 790 | int64_t total_sse = 0; |
| 791 | |
| 792 | x->pred_sse[ref] = 0; |
| 793 | |
| 794 | for (plane = plane_from; plane <= plane_to; ++plane) { |
| 795 | struct macroblock_plane *const p = &x->plane[plane]; |
| 796 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 797 | const BLOCK_SIZE bs = get_plane_block_size(bsize, pd); |
| 798 | |
| 799 | unsigned int sse; |
| 800 | int rate; |
| 801 | int64_t dist; |
| 802 | |
| 803 | // TODO(geza): Write direct sse functions that do not compute |
| 804 | // variance as well. |
| 805 | cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, |
| 806 | &sse); |
| 807 | |
| 808 | if (plane == 0) x->pred_sse[ref] = sse; |
| 809 | |
| 810 | total_sse += sse; |
| 811 | |
| 812 | model_rd_from_sse(cpi, xd, bs, plane, sse, &rate, &dist); |
| 813 | |
| 814 | rate_sum += rate; |
| 815 | dist_sum += dist; |
| 816 | } |
| 817 | |
| 818 | *skip_txfm_sb = total_sse == 0; |
| 819 | *skip_sse_sb = total_sse << 4; |
| 820 | *out_rate_sum = (int)rate_sum; |
| 821 | *out_dist_sum = dist_sum; |
| 822 | } |
| 823 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 824 | #if CONFIG_PVQ |
| 825 | // Without PVQ, av1_block_error_c() return two kind of errors, |
| 826 | // 1) reconstruction (i.e. decoded) error and |
| 827 | // 2) Squared sum of transformed residue (i.e. 'coeff') |
| 828 | // However, if PVQ is enabled, coeff does not keep the transformed residue |
| 829 | // but instead a transformed original is kept. |
| 830 | // Hence, new parameter ref vector (i.e. transformed predicted signal) |
| 831 | // is required to derive the residue signal, |
| 832 | // i.e. coeff - ref = residue (all transformed). |
| 833 | |
| 834 | // TODO(yushin) : Since 4x4 case does not need ssz, better to refactor into |
| 835 | // a separate function that does not do the extra computations for ssz. |
| 836 | int64_t av1_block_error2_c(const tran_low_t *coeff, const tran_low_t *dqcoeff, |
| 837 | const tran_low_t *ref, intptr_t block_size, |
| 838 | int64_t *ssz) { |
| 839 | int64_t error; |
| 840 | |
| 841 | // Use the existing sse codes for calculating distortion of decoded signal: |
| 842 | // i.e. (orig - decoded)^2 |
| 843 | error = av1_block_error_fp(coeff, dqcoeff, block_size); |
| 844 | // prediction residue^2 = (orig - ref)^2 |
| 845 | *ssz = av1_block_error_fp(coeff, ref, block_size); |
| 846 | |
| 847 | return error; |
| 848 | } |
| 849 | #endif |
| 850 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 851 | int64_t av1_block_error_c(const tran_low_t *coeff, const tran_low_t *dqcoeff, |
| 852 | intptr_t block_size, int64_t *ssz) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 853 | int i; |
| 854 | int64_t error = 0, sqcoeff = 0; |
| 855 | |
| 856 | for (i = 0; i < block_size; i++) { |
| 857 | const int diff = coeff[i] - dqcoeff[i]; |
| 858 | error += diff * diff; |
| 859 | sqcoeff += coeff[i] * coeff[i]; |
| 860 | } |
| 861 | |
| 862 | *ssz = sqcoeff; |
| 863 | return error; |
| 864 | } |
| 865 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 866 | int64_t av1_block_error_fp_c(const int16_t *coeff, const int16_t *dqcoeff, |
| 867 | int block_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 868 | int i; |
| 869 | int64_t error = 0; |
| 870 | |
| 871 | for (i = 0; i < block_size; i++) { |
| 872 | const int diff = coeff[i] - dqcoeff[i]; |
| 873 | error += diff * diff; |
| 874 | } |
| 875 | |
| 876 | return error; |
| 877 | } |
| 878 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 879 | #if CONFIG_AOM_HIGHBITDEPTH |
| 880 | int64_t av1_highbd_block_error_c(const tran_low_t *coeff, |
| 881 | const tran_low_t *dqcoeff, intptr_t block_size, |
| 882 | int64_t *ssz, int bd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 883 | int i; |
| 884 | int64_t error = 0, sqcoeff = 0; |
| 885 | int shift = 2 * (bd - 8); |
| 886 | int rounding = shift > 0 ? 1 << (shift - 1) : 0; |
| 887 | |
| 888 | for (i = 0; i < block_size; i++) { |
| 889 | const int64_t diff = coeff[i] - dqcoeff[i]; |
| 890 | error += diff * diff; |
| 891 | sqcoeff += (int64_t)coeff[i] * (int64_t)coeff[i]; |
| 892 | } |
| 893 | assert(error >= 0 && sqcoeff >= 0); |
| 894 | error = (error + rounding) >> shift; |
| 895 | sqcoeff = (sqcoeff + rounding) >> shift; |
| 896 | |
| 897 | *ssz = sqcoeff; |
| 898 | return error; |
| 899 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 900 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 901 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 902 | #if !CONFIG_PVQ |
Debargha Mukherjee | ceebb70 | 2016-10-11 05:26:50 -0700 | [diff] [blame] | 903 | /* The trailing '0' is a terminator which is used inside av1_cost_coeffs() to |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 904 | * decide whether to include cost of a trailing EOB node or not (i.e. we |
| 905 | * can skip this if the last coefficient in this transform block, e.g. the |
| 906 | * 16th coefficient in a 4x4 block or the 64th coefficient in a 8x8 block, |
| 907 | * were non-zero). */ |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 908 | int av1_cost_coeffs(const AV1_COMMON *const cm, MACROBLOCK *x, int plane, |
| 909 | int block, int coeff_ctx, TX_SIZE tx_size, |
| 910 | const int16_t *scan, const int16_t *nb, |
Debargha Mukherjee | ceebb70 | 2016-10-11 05:26:50 -0700 | [diff] [blame] | 911 | int use_fast_coef_costing) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 912 | MACROBLOCKD *const xd = &x->e_mbd; |
| 913 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 914 | const struct macroblock_plane *p = &x->plane[plane]; |
| 915 | const struct macroblockd_plane *pd = &xd->plane[plane]; |
| 916 | const PLANE_TYPE type = pd->plane_type; |
| 917 | const uint16_t *band_count = &band_count_table[tx_size][1]; |
| 918 | const int eob = p->eobs[block]; |
| 919 | const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block); |
| 920 | const int tx_size_ctx = txsize_sqr_map[tx_size]; |
| 921 | unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] = |
| 922 | x->token_costs[tx_size_ctx][type][is_inter_block(mbmi)]; |
| 923 | uint8_t token_cache[MAX_TX_SQUARE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 924 | int pt = coeff_ctx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 925 | int c, cost; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 926 | #if CONFIG_AOM_HIGHBITDEPTH |
| 927 | const int *cat6_high_cost = av1_get_high_cost_table(xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 928 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 929 | const int *cat6_high_cost = av1_get_high_cost_table(8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 930 | #endif |
| 931 | |
| 932 | #if !CONFIG_VAR_TX && !CONFIG_SUPERTX |
| 933 | // Check for consistency of tx_size with mode info |
| 934 | assert(type == PLANE_TYPE_Y ? mbmi->tx_size == tx_size |
| 935 | : get_uv_tx_size(mbmi, pd) == tx_size); |
| 936 | #endif // !CONFIG_VAR_TX && !CONFIG_SUPERTX |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 937 | (void)cm; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 938 | |
| 939 | if (eob == 0) { |
| 940 | // single eob token |
| 941 | cost = token_costs[0][0][pt][EOB_TOKEN]; |
| 942 | c = 0; |
| 943 | } else { |
| 944 | if (use_fast_coef_costing) { |
| 945 | int band_left = *band_count++; |
| 946 | |
| 947 | // dc token |
| 948 | int v = qcoeff[0]; |
| 949 | int16_t prev_t; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 950 | cost = av1_get_token_cost(v, &prev_t, cat6_high_cost); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 951 | cost += (*token_costs)[0][pt][prev_t]; |
| 952 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 953 | token_cache[0] = av1_pt_energy_class[prev_t]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 954 | ++token_costs; |
| 955 | |
| 956 | // ac tokens |
| 957 | for (c = 1; c < eob; c++) { |
| 958 | const int rc = scan[c]; |
| 959 | int16_t t; |
| 960 | |
| 961 | v = qcoeff[rc]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 962 | cost += av1_get_token_cost(v, &t, cat6_high_cost); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 963 | cost += (*token_costs)[!prev_t][!prev_t][t]; |
| 964 | prev_t = t; |
| 965 | if (!--band_left) { |
| 966 | band_left = *band_count++; |
| 967 | ++token_costs; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | // eob token |
| 972 | if (band_left) cost += (*token_costs)[0][!prev_t][EOB_TOKEN]; |
| 973 | |
| 974 | } else { // !use_fast_coef_costing |
| 975 | int band_left = *band_count++; |
| 976 | |
| 977 | // dc token |
| 978 | int v = qcoeff[0]; |
| 979 | int16_t tok; |
| 980 | unsigned int(*tok_cost_ptr)[COEFF_CONTEXTS][ENTROPY_TOKENS]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 981 | cost = av1_get_token_cost(v, &tok, cat6_high_cost); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 982 | cost += (*token_costs)[0][pt][tok]; |
| 983 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 984 | token_cache[0] = av1_pt_energy_class[tok]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 985 | ++token_costs; |
| 986 | |
| 987 | tok_cost_ptr = &((*token_costs)[!tok]); |
| 988 | |
| 989 | // ac tokens |
| 990 | for (c = 1; c < eob; c++) { |
| 991 | const int rc = scan[c]; |
| 992 | |
| 993 | v = qcoeff[rc]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 994 | cost += av1_get_token_cost(v, &tok, cat6_high_cost); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 995 | pt = get_coef_context(nb, token_cache, c); |
| 996 | cost += (*tok_cost_ptr)[pt][tok]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 997 | token_cache[rc] = av1_pt_energy_class[tok]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 998 | if (!--band_left) { |
| 999 | band_left = *band_count++; |
| 1000 | ++token_costs; |
| 1001 | } |
| 1002 | tok_cost_ptr = &((*token_costs)[!tok]); |
| 1003 | } |
| 1004 | |
| 1005 | // eob token |
| 1006 | if (band_left) { |
| 1007 | pt = get_coef_context(nb, token_cache, c); |
| 1008 | cost += (*token_costs)[0][pt][EOB_TOKEN]; |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1013 | return cost; |
| 1014 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1015 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1016 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1017 | static void dist_block(const AV1_COMP *cpi, MACROBLOCK *x, int plane, int block, |
| 1018 | int blk_row, int blk_col, TX_SIZE tx_size, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1019 | int64_t *out_dist, int64_t *out_sse) { |
| 1020 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1021 | const struct macroblock_plane *const p = &x->plane[plane]; |
| 1022 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1023 | if (cpi->sf.use_transform_domain_distortion) { |
| 1024 | // Transform domain distortion computation is more accurate as it does |
| 1025 | // not involve an inverse transform, but it is less accurate. |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1026 | const int buffer_length = tx_size_2d[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1027 | int64_t this_sse; |
| 1028 | int tx_type = get_tx_type(pd->plane_type, xd, block, tx_size); |
| 1029 | int shift = (MAX_TX_SCALE - get_tx_scale(xd, tx_type, tx_size)) * 2; |
| 1030 | tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
| 1031 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1032 | #if CONFIG_PVQ |
Yaowu Xu | d6ea71c | 2016-11-07 10:24:14 -0800 | [diff] [blame] | 1033 | tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1034 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1035 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1036 | const int bd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd : 8; |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1037 | *out_dist = |
| 1038 | av1_highbd_block_error(coeff, dqcoeff, buffer_length, &this_sse, bd) >> |
| 1039 | shift; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1040 | #elif CONFIG_PVQ |
| 1041 | *out_dist = av1_block_error2_c(coeff, dqcoeff, ref_coeff, buffer_length, |
| 1042 | &this_sse) >> |
| 1043 | shift; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1044 | #else |
| 1045 | *out_dist = |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1046 | av1_block_error(coeff, dqcoeff, buffer_length, &this_sse) >> shift; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1047 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1048 | *out_sse = this_sse >> shift; |
| 1049 | } else { |
| 1050 | const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size]; |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1051 | const int bsw = block_size_wide[tx_bsize]; |
| 1052 | const int bsh = block_size_high[tx_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1053 | const int src_stride = x->plane[plane].src.stride; |
| 1054 | const int dst_stride = xd->plane[plane].dst.stride; |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1055 | // Scale the transform block index to pixel unit. |
| 1056 | const int src_idx = (blk_row * src_stride + blk_col) |
| 1057 | << tx_size_wide_log2[0]; |
| 1058 | const int dst_idx = (blk_row * dst_stride + blk_col) |
| 1059 | << tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1060 | const uint8_t *src = &x->plane[plane].src.buf[src_idx]; |
| 1061 | const uint8_t *dst = &xd->plane[plane].dst.buf[dst_idx]; |
| 1062 | const tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 1063 | const uint16_t eob = p->eobs[block]; |
| 1064 | |
| 1065 | unsigned int tmp; |
| 1066 | |
| 1067 | assert(cpi != NULL); |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1068 | assert(tx_size_wide_log2[0] == tx_size_high_log2[0]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1069 | |
| 1070 | cpi->fn_ptr[tx_bsize].vf(src, src_stride, dst, dst_stride, &tmp); |
| 1071 | *out_sse = (int64_t)tmp * 16; |
| 1072 | |
| 1073 | if (eob) { |
| 1074 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1075 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1076 | DECLARE_ALIGNED(16, uint16_t, recon16[MAX_TX_SQUARE]); |
| 1077 | uint8_t *recon = (uint8_t *)recon16; |
| 1078 | #else |
| 1079 | DECLARE_ALIGNED(16, uint8_t, recon[MAX_TX_SQUARE]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1080 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1081 | |
| 1082 | const PLANE_TYPE plane_type = plane == 0 ? PLANE_TYPE_Y : PLANE_TYPE_UV; |
| 1083 | |
| 1084 | INV_TXFM_PARAM inv_txfm_param; |
| 1085 | |
| 1086 | inv_txfm_param.tx_type = get_tx_type(plane_type, xd, block, tx_size); |
| 1087 | inv_txfm_param.tx_size = tx_size; |
| 1088 | inv_txfm_param.eob = eob; |
| 1089 | inv_txfm_param.lossless = xd->lossless[mbmi->segment_id]; |
| 1090 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1091 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1092 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1093 | recon = CONVERT_TO_BYTEPTR(recon); |
| 1094 | inv_txfm_param.bd = xd->bd; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1095 | aom_highbd_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1096 | NULL, 0, bsw, bsh, xd->bd); |
| 1097 | highbd_inv_txfm_add(dqcoeff, recon, MAX_TX_SIZE, &inv_txfm_param); |
| 1098 | } else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1099 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1100 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1101 | aom_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, NULL, 0, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1102 | bsw, bsh); |
| 1103 | inv_txfm_add(dqcoeff, recon, MAX_TX_SIZE, &inv_txfm_param); |
| 1104 | } |
| 1105 | |
| 1106 | cpi->fn_ptr[tx_bsize].vf(src, src_stride, recon, MAX_TX_SIZE, &tmp); |
| 1107 | } |
| 1108 | |
| 1109 | *out_dist = (int64_t)tmp * 16; |
| 1110 | } |
| 1111 | } |
| 1112 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1113 | #if !CONFIG_PVQ |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 1114 | static int rate_block(int plane, int block, int coeff_ctx, TX_SIZE tx_size, |
Debargha Mukherjee | 2963054 | 2016-09-06 13:15:31 -0700 | [diff] [blame] | 1115 | struct rdcost_block_args *args) { |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 1116 | return av1_cost_coeffs(&args->cpi->common, args->x, plane, block, coeff_ctx, |
| 1117 | tx_size, args->scan_order->scan, |
| 1118 | args->scan_order->neighbors, |
Debargha Mukherjee | ceebb70 | 2016-10-11 05:26:50 -0700 | [diff] [blame] | 1119 | args->use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1120 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1121 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1122 | |
| 1123 | static uint64_t sum_squares_2d(const int16_t *diff, int diff_stride, |
| 1124 | TX_SIZE tx_size) { |
| 1125 | uint64_t sse; |
| 1126 | switch (tx_size) { |
| 1127 | #if CONFIG_EXT_TX |
| 1128 | case TX_4X8: |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1129 | sse = aom_sum_squares_2d_i16(diff, diff_stride, 4) + |
| 1130 | aom_sum_squares_2d_i16(diff + 4 * diff_stride, diff_stride, 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1131 | break; |
| 1132 | case TX_8X4: |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1133 | sse = aom_sum_squares_2d_i16(diff, diff_stride, 4) + |
| 1134 | aom_sum_squares_2d_i16(diff + 4, diff_stride, 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1135 | break; |
| 1136 | case TX_8X16: |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1137 | sse = aom_sum_squares_2d_i16(diff, diff_stride, 8) + |
| 1138 | aom_sum_squares_2d_i16(diff + 8 * diff_stride, diff_stride, 8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1139 | break; |
| 1140 | case TX_16X8: |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1141 | sse = aom_sum_squares_2d_i16(diff, diff_stride, 8) + |
| 1142 | aom_sum_squares_2d_i16(diff + 8, diff_stride, 8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1143 | break; |
| 1144 | case TX_16X32: |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1145 | sse = aom_sum_squares_2d_i16(diff, diff_stride, 16) + |
| 1146 | aom_sum_squares_2d_i16(diff + 16 * diff_stride, diff_stride, 16); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1147 | break; |
| 1148 | case TX_32X16: |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1149 | sse = aom_sum_squares_2d_i16(diff, diff_stride, 16) + |
| 1150 | aom_sum_squares_2d_i16(diff + 16, diff_stride, 16); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1151 | break; |
| 1152 | #endif // CONFIG_EXT_TX |
| 1153 | default: |
| 1154 | assert(tx_size < TX_SIZES); |
Jingning Han | c598cf8 | 2016-10-25 10:37:34 -0700 | [diff] [blame] | 1155 | sse = aom_sum_squares_2d_i16(diff, diff_stride, tx_size_wide[tx_size]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1156 | break; |
| 1157 | } |
| 1158 | return sse; |
| 1159 | } |
| 1160 | |
| 1161 | static void block_rd_txfm(int plane, int block, int blk_row, int blk_col, |
| 1162 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) { |
| 1163 | struct rdcost_block_args *args = arg; |
| 1164 | MACROBLOCK *const x = args->x; |
| 1165 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1166 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1167 | const AV1_COMMON *cm = &args->cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1168 | int64_t rd1, rd2, rd; |
| 1169 | int rate; |
| 1170 | int64_t dist; |
| 1171 | int64_t sse; |
Debargha Mukherjee | 2963054 | 2016-09-06 13:15:31 -0700 | [diff] [blame] | 1172 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1173 | int coeff_ctx = combine_entropy_contexts(*(args->t_above + blk_col), |
| 1174 | *(args->t_left + blk_row)); |
| 1175 | |
| 1176 | if (args->exit_early) return; |
| 1177 | |
| 1178 | if (!is_inter_block(mbmi)) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 1179 | struct encode_b_args b_args = { |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1180 | (AV1_COMMON *)cm, x, NULL, &mbmi->skip, args->t_above, args->t_left, 1 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1181 | }; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1182 | av1_encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 1183 | &b_args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1184 | |
| 1185 | if (args->cpi->sf.use_transform_domain_distortion) { |
| 1186 | dist_block(args->cpi, x, plane, block, blk_row, blk_col, tx_size, &dist, |
| 1187 | &sse); |
| 1188 | } else { |
| 1189 | // Note that the encode block_intra call above already calls |
| 1190 | // inv_txfm_add, so we can't just call dist_block here. |
| 1191 | const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1192 | const aom_variance_fn_t variance = args->cpi->fn_ptr[tx_bsize].vf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1193 | |
| 1194 | const struct macroblock_plane *const p = &x->plane[plane]; |
| 1195 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1196 | |
| 1197 | const int src_stride = p->src.stride; |
| 1198 | const int dst_stride = pd->dst.stride; |
Jingning Han | c598cf8 | 2016-10-25 10:37:34 -0700 | [diff] [blame] | 1199 | const int diff_stride = block_size_wide[plane_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1200 | |
| 1201 | const uint8_t *src = &p->src.buf[4 * (blk_row * src_stride + blk_col)]; |
| 1202 | const uint8_t *dst = &pd->dst.buf[4 * (blk_row * dst_stride + blk_col)]; |
| 1203 | const int16_t *diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)]; |
| 1204 | |
| 1205 | unsigned int tmp; |
| 1206 | sse = sum_squares_2d(diff, diff_stride, tx_size); |
| 1207 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1208 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1209 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 1210 | sse = ROUND_POWER_OF_TWO(sse, (xd->bd - 8) * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1211 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1212 | sse = (int64_t)sse * 16; |
| 1213 | |
| 1214 | variance(src, src_stride, dst, dst_stride, &tmp); |
| 1215 | dist = (int64_t)tmp * 16; |
| 1216 | } |
| 1217 | } else { |
| 1218 | // full forward transform and quantization |
| 1219 | #if CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1220 | av1_xform_quant_fp_nuq(cm, x, plane, block, blk_row, blk_col, plane_bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1221 | tx_size, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1222 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1223 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1224 | AV1_XFORM_QUANT_FP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1225 | #endif // CONFIG_NEW_QUANT |
| 1226 | if (x->plane[plane].eobs[block]) |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1227 | av1_optimize_b(cm, x, plane, block, tx_size, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1228 | dist_block(args->cpi, x, plane, block, blk_row, blk_col, tx_size, &dist, |
| 1229 | &sse); |
| 1230 | } |
| 1231 | |
| 1232 | rd = RDCOST(x->rdmult, x->rddiv, 0, dist); |
| 1233 | if (args->this_rd + rd > args->best_rd) { |
| 1234 | args->exit_early = 1; |
| 1235 | return; |
| 1236 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1237 | #if !CONFIG_PVQ |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 1238 | rate = rate_block(plane, block, coeff_ctx, tx_size, args); |
| 1239 | args->t_above[blk_col] = (x->plane[plane].eobs[block] > 0); |
| 1240 | args->t_left[blk_row] = (x->plane[plane].eobs[block] > 0); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1241 | #else |
| 1242 | rate = x->rate; |
| 1243 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1244 | rd1 = RDCOST(x->rdmult, x->rddiv, rate, dist); |
| 1245 | rd2 = RDCOST(x->rdmult, x->rddiv, 0, sse); |
| 1246 | |
| 1247 | // TODO(jingning): temporarily enabled only for luma component |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1248 | rd = AOMMIN(rd1, rd2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1249 | |
| 1250 | args->this_rate += rate; |
| 1251 | args->this_dist += dist; |
| 1252 | args->this_sse += sse; |
| 1253 | args->this_rd += rd; |
| 1254 | |
| 1255 | if (args->this_rd > args->best_rd) { |
| 1256 | args->exit_early = 1; |
| 1257 | return; |
| 1258 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1259 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1260 | args->skippable &= !x->plane[plane].eobs[block]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1261 | #else |
| 1262 | args->skippable &= x->pvq_skip[plane]; |
| 1263 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1266 | static void txfm_rd_in_plane(MACROBLOCK *x, const AV1_COMP *cpi, int *rate, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1267 | int64_t *distortion, int *skippable, int64_t *sse, |
| 1268 | int64_t ref_best_rd, int plane, BLOCK_SIZE bsize, |
| 1269 | TX_SIZE tx_size, int use_fast_coef_casting) { |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1270 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1271 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1272 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1273 | TX_TYPE tx_type; |
| 1274 | struct rdcost_block_args args; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1275 | av1_zero(args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1276 | args.x = x; |
| 1277 | args.cpi = cpi; |
| 1278 | args.best_rd = ref_best_rd; |
| 1279 | args.use_fast_coef_costing = use_fast_coef_casting; |
| 1280 | args.skippable = 1; |
| 1281 | |
| 1282 | if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size; |
| 1283 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1284 | av1_get_entropy_contexts(bsize, tx_size, pd, args.t_above, args.t_left); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1285 | |
| 1286 | tx_type = get_tx_type(pd->plane_type, xd, 0, tx_size); |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 1287 | args.scan_order = |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1288 | get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1289 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1290 | av1_foreach_transformed_block_in_plane(xd, bsize, plane, block_rd_txfm, |
| 1291 | &args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1292 | if (args.exit_early) { |
| 1293 | *rate = INT_MAX; |
| 1294 | *distortion = INT64_MAX; |
| 1295 | *sse = INT64_MAX; |
| 1296 | *skippable = 0; |
| 1297 | } else { |
| 1298 | *distortion = args.this_dist; |
| 1299 | *rate = args.this_rate; |
| 1300 | *sse = args.this_sse; |
| 1301 | *skippable = args.skippable; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | #if CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1306 | void av1_txfm_rd_in_plane_supertx(MACROBLOCK *x, const AV1_COMP *cpi, int *rate, |
| 1307 | int64_t *distortion, int *skippable, |
| 1308 | int64_t *sse, int64_t ref_best_rd, int plane, |
| 1309 | BLOCK_SIZE bsize, TX_SIZE tx_size, |
| 1310 | int use_fast_coef_casting) { |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1311 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1312 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1313 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1314 | struct rdcost_block_args args; |
| 1315 | TX_TYPE tx_type; |
| 1316 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1317 | av1_zero(args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1318 | args.cpi = cpi; |
| 1319 | args.x = x; |
| 1320 | args.best_rd = ref_best_rd; |
| 1321 | args.use_fast_coef_costing = use_fast_coef_casting; |
| 1322 | |
| 1323 | #if CONFIG_EXT_TX |
| 1324 | assert(tx_size < TX_SIZES); |
| 1325 | #endif // CONFIG_EXT_TX |
| 1326 | |
| 1327 | if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size; |
| 1328 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1329 | av1_get_entropy_contexts(bsize, tx_size, pd, args.t_above, args.t_left); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1330 | |
| 1331 | tx_type = get_tx_type(pd->plane_type, xd, 0, tx_size); |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 1332 | args.scan_order = |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1333 | get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1334 | |
| 1335 | block_rd_txfm(plane, 0, 0, 0, get_plane_block_size(bsize, pd), tx_size, |
| 1336 | &args); |
| 1337 | |
| 1338 | if (args.exit_early) { |
| 1339 | *rate = INT_MAX; |
| 1340 | *distortion = INT64_MAX; |
| 1341 | *sse = INT64_MAX; |
| 1342 | *skippable = 0; |
| 1343 | } else { |
| 1344 | *distortion = args.this_dist; |
| 1345 | *rate = args.this_rate; |
| 1346 | *sse = args.this_sse; |
| 1347 | *skippable = !x->plane[plane].eobs[0]; |
| 1348 | } |
| 1349 | } |
| 1350 | #endif // CONFIG_SUPERTX |
| 1351 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1352 | static int64_t txfm_yrd(const AV1_COMP *const cpi, MACROBLOCK *x, int *r, |
| 1353 | int64_t *d, int *s, int64_t *sse, int64_t ref_best_rd, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1354 | BLOCK_SIZE bs, TX_TYPE tx_type, int tx_size) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1355 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1356 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1357 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 1358 | int64_t rd = INT64_MAX; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1359 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1360 | int s0, s1; |
| 1361 | const int is_inter = is_inter_block(mbmi); |
| 1362 | const int tx_size_ctx = get_tx_size_context(xd); |
| 1363 | const int tx_size_cat = |
| 1364 | is_inter ? inter_tx_size_cat_lookup[bs] : intra_tx_size_cat_lookup[bs]; |
| 1365 | const TX_SIZE coded_tx_size = txsize_sqr_up_map[tx_size]; |
Jingning Han | b0a7130 | 2016-10-25 16:28:49 -0700 | [diff] [blame] | 1366 | const int depth = tx_size_to_depth(coded_tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1367 | const int tx_select = cm->tx_mode == TX_MODE_SELECT; |
Jingning Han | b0a7130 | 2016-10-25 16:28:49 -0700 | [diff] [blame] | 1368 | const int r_tx_size = cpi->tx_size_cost[tx_size_cat][tx_size_ctx][depth]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1369 | |
| 1370 | assert(skip_prob > 0); |
| 1371 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 1372 | assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed_bsize(bs))); |
| 1373 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 1374 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1375 | s0 = av1_cost_bit(skip_prob, 0); |
| 1376 | s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1377 | |
| 1378 | mbmi->tx_type = tx_type; |
| 1379 | mbmi->tx_size = tx_size; |
| 1380 | txfm_rd_in_plane(x, cpi, r, d, s, sse, ref_best_rd, 0, bs, tx_size, |
| 1381 | cpi->sf.use_fast_coef_costing); |
| 1382 | if (*r == INT_MAX) return INT64_MAX; |
| 1383 | #if CONFIG_EXT_TX |
| 1384 | if (get_ext_tx_types(tx_size, bs, is_inter) > 1 && |
| 1385 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
| 1386 | const int ext_tx_set = get_ext_tx_set(tx_size, bs, is_inter); |
| 1387 | if (is_inter) { |
| 1388 | if (ext_tx_set > 0) |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 1389 | *r += |
| 1390 | cpi->inter_tx_type_costs[ext_tx_set][txsize_sqr_map[mbmi->tx_size]] |
| 1391 | [mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1392 | } else { |
| 1393 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) |
| 1394 | *r += cpi->intra_tx_type_costs[ext_tx_set][mbmi->tx_size][mbmi->mode] |
| 1395 | [mbmi->tx_type]; |
| 1396 | } |
| 1397 | } |
| 1398 | #else |
| 1399 | if (tx_size < TX_32X32 && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 1400 | !FIXED_TX_TYPE) { |
| 1401 | if (is_inter) { |
| 1402 | *r += cpi->inter_tx_type_costs[mbmi->tx_size][mbmi->tx_type]; |
| 1403 | } else { |
| 1404 | *r += cpi->intra_tx_type_costs[mbmi->tx_size] |
| 1405 | [intra_mode_to_tx_type_context[mbmi->mode]] |
| 1406 | [mbmi->tx_type]; |
| 1407 | } |
| 1408 | } |
| 1409 | #endif // CONFIG_EXT_TX |
| 1410 | |
| 1411 | if (*s) { |
| 1412 | if (is_inter) { |
| 1413 | rd = RDCOST(x->rdmult, x->rddiv, s1, *sse); |
| 1414 | } else { |
| 1415 | rd = RDCOST(x->rdmult, x->rddiv, s1 + r_tx_size * tx_select, *sse); |
| 1416 | } |
| 1417 | } else { |
| 1418 | rd = RDCOST(x->rdmult, x->rddiv, *r + s0 + r_tx_size * tx_select, *d); |
| 1419 | } |
| 1420 | |
| 1421 | if (tx_select) *r += r_tx_size; |
| 1422 | |
| 1423 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && !(*s)) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1424 | rd = AOMMIN(rd, RDCOST(x->rdmult, x->rddiv, s1, *sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1425 | |
| 1426 | return rd; |
| 1427 | } |
| 1428 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1429 | static int64_t choose_tx_size_fix_type(const AV1_COMP *const cpi, BLOCK_SIZE bs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1430 | MACROBLOCK *x, int *rate, |
| 1431 | int64_t *distortion, int *skip, |
| 1432 | int64_t *psse, int64_t ref_best_rd, |
| 1433 | TX_TYPE tx_type, int prune) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1434 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1435 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1436 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 1437 | int r, s; |
| 1438 | int64_t d, sse; |
| 1439 | int64_t rd = INT64_MAX; |
| 1440 | int n; |
| 1441 | int start_tx, end_tx; |
| 1442 | int64_t best_rd = INT64_MAX, last_rd = INT64_MAX; |
| 1443 | const TX_SIZE max_tx_size = max_txsize_lookup[bs]; |
| 1444 | TX_SIZE best_tx_size = max_tx_size; |
| 1445 | const int tx_select = cm->tx_mode == TX_MODE_SELECT; |
| 1446 | const int is_inter = is_inter_block(mbmi); |
| 1447 | #if CONFIG_EXT_TX |
| 1448 | #if CONFIG_RECT_TX |
Yue Chen | 49587a7 | 2016-09-28 17:09:47 -0700 | [diff] [blame] | 1449 | int evaluate_rect_tx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1450 | #endif // CONFIG_RECT_TX |
| 1451 | int ext_tx_set; |
| 1452 | #endif // CONFIG_EXT_TX |
| 1453 | |
| 1454 | if (tx_select) { |
| 1455 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
Yue Chen | 49587a7 | 2016-09-28 17:09:47 -0700 | [diff] [blame] | 1456 | evaluate_rect_tx = is_rect_tx_allowed(xd, mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1457 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 1458 | start_tx = max_tx_size; |
Hui Su | eafb2e6 | 2016-10-18 10:21:36 -0700 | [diff] [blame] | 1459 | end_tx = (max_tx_size == TX_32X32) ? TX_8X8 : TX_4X4; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1460 | } else { |
| 1461 | const TX_SIZE chosen_tx_size = |
| 1462 | tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 1463 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
Yue Chen | 49587a7 | 2016-09-28 17:09:47 -0700 | [diff] [blame] | 1464 | evaluate_rect_tx = is_rect_tx(chosen_tx_size); |
| 1465 | assert(IMPLIES(evaluate_rect_tx, is_rect_tx_allowed(xd, mbmi))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1466 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 1467 | start_tx = chosen_tx_size; |
| 1468 | end_tx = chosen_tx_size; |
| 1469 | } |
| 1470 | |
| 1471 | *distortion = INT64_MAX; |
| 1472 | *rate = INT_MAX; |
| 1473 | *skip = 0; |
| 1474 | *psse = INT64_MAX; |
| 1475 | |
| 1476 | mbmi->tx_type = tx_type; |
| 1477 | |
| 1478 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
Yue Chen | 49587a7 | 2016-09-28 17:09:47 -0700 | [diff] [blame] | 1479 | if (evaluate_rect_tx) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1480 | const TX_SIZE rect_tx_size = max_txsize_rect_lookup[bs]; |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 1481 | ext_tx_set = get_ext_tx_set(rect_tx_size, bs, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1482 | if (ext_tx_used_inter[ext_tx_set][tx_type]) { |
| 1483 | rd = txfm_yrd(cpi, x, &r, &d, &s, &sse, ref_best_rd, bs, tx_type, |
| 1484 | rect_tx_size); |
| 1485 | best_tx_size = rect_tx_size; |
| 1486 | best_rd = rd; |
| 1487 | *distortion = d; |
| 1488 | *rate = r; |
| 1489 | *skip = s; |
| 1490 | *psse = sse; |
| 1491 | } |
| 1492 | } |
| 1493 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 1494 | |
| 1495 | last_rd = INT64_MAX; |
| 1496 | for (n = start_tx; n >= end_tx; --n) { |
| 1497 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 1498 | if (is_rect_tx(n)) break; |
| 1499 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 1500 | if (FIXED_TX_TYPE && tx_type != get_default_tx_type(0, xd, 0, n)) continue; |
| 1501 | if (!is_inter && x->use_default_intra_tx_type && |
| 1502 | tx_type != get_default_tx_type(0, xd, 0, n)) |
| 1503 | continue; |
| 1504 | if (is_inter && x->use_default_inter_tx_type && |
| 1505 | tx_type != get_default_tx_type(0, xd, 0, n)) |
| 1506 | continue; |
| 1507 | if (max_tx_size == TX_32X32 && n == TX_4X4) continue; |
| 1508 | #if CONFIG_EXT_TX |
| 1509 | ext_tx_set = get_ext_tx_set(n, bs, is_inter); |
| 1510 | if (is_inter) { |
| 1511 | if (!ext_tx_used_inter[ext_tx_set][tx_type]) continue; |
| 1512 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 1513 | if (!do_tx_type_search(tx_type, prune)) continue; |
| 1514 | } |
| 1515 | } else { |
| 1516 | if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) { |
| 1517 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 1518 | } |
| 1519 | if (!ext_tx_used_intra[ext_tx_set][tx_type]) continue; |
| 1520 | } |
| 1521 | #else // CONFIG_EXT_TX |
| 1522 | if (n >= TX_32X32 && tx_type != DCT_DCT) continue; |
| 1523 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 1524 | !do_tx_type_search(tx_type, prune)) |
| 1525 | continue; |
| 1526 | #endif // CONFIG_EXT_TX |
| 1527 | |
| 1528 | rd = txfm_yrd(cpi, x, &r, &d, &s, &sse, ref_best_rd, bs, tx_type, n); |
| 1529 | |
| 1530 | // Early termination in transform size search. |
| 1531 | if (cpi->sf.tx_size_search_breakout && |
| 1532 | (rd == INT64_MAX || (s == 1 && tx_type != DCT_DCT && n < start_tx) || |
| 1533 | (n < (int)max_tx_size && rd > last_rd))) |
| 1534 | break; |
| 1535 | |
| 1536 | last_rd = rd; |
| 1537 | if (rd < best_rd) { |
| 1538 | best_tx_size = n; |
| 1539 | best_rd = rd; |
| 1540 | *distortion = d; |
| 1541 | *rate = r; |
| 1542 | *skip = s; |
| 1543 | *psse = sse; |
| 1544 | } |
| 1545 | } |
| 1546 | mbmi->tx_size = best_tx_size; |
| 1547 | |
| 1548 | return best_rd; |
| 1549 | } |
| 1550 | |
| 1551 | #if CONFIG_EXT_INTER |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1552 | static int64_t estimate_yrd_for_sb(const AV1_COMP *const cpi, BLOCK_SIZE bs, |
| 1553 | MACROBLOCK *x, int *r, int64_t *d, int *s, |
| 1554 | int64_t *sse, int64_t ref_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1555 | return txfm_yrd(cpi, x, r, d, s, sse, ref_best_rd, bs, DCT_DCT, |
| 1556 | max_txsize_lookup[bs]); |
| 1557 | } |
| 1558 | #endif // CONFIG_EXT_INTER |
| 1559 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1560 | static void choose_largest_tx_size(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 1561 | int *rate, int64_t *distortion, int *skip, |
| 1562 | int64_t *sse, int64_t ref_best_rd, |
| 1563 | BLOCK_SIZE bs) { |
| 1564 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1565 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1566 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 1567 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
| 1568 | int r, s; |
| 1569 | int64_t d, psse, this_rd, best_rd = INT64_MAX; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1570 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
| 1571 | int s0 = av1_cost_bit(skip_prob, 0); |
| 1572 | int s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1573 | const int is_inter = is_inter_block(mbmi); |
| 1574 | int prune = 0; |
| 1575 | #if CONFIG_EXT_TX |
| 1576 | int ext_tx_set; |
| 1577 | #endif // CONFIG_EXT_TX |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 1578 | *distortion = INT64_MAX; |
| 1579 | *rate = INT_MAX; |
| 1580 | *skip = 0; |
| 1581 | *sse = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1582 | |
| 1583 | mbmi->tx_size = tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 1584 | |
| 1585 | #if CONFIG_EXT_TX |
| 1586 | ext_tx_set = get_ext_tx_set(mbmi->tx_size, bs, is_inter); |
| 1587 | #endif // CONFIG_EXT_TX |
| 1588 | |
| 1589 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 1590 | #if CONFIG_EXT_TX |
| 1591 | prune = prune_tx_types(cpi, bs, x, xd, ext_tx_set); |
| 1592 | #else |
| 1593 | prune = prune_tx_types(cpi, bs, x, xd, 0); |
| 1594 | #endif |
| 1595 | #if CONFIG_EXT_TX |
| 1596 | if (get_ext_tx_types(mbmi->tx_size, bs, is_inter) > 1 && |
| 1597 | !xd->lossless[mbmi->segment_id]) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1598 | #if CONFIG_PVQ |
| 1599 | od_rollback_buffer pre_buf, post_buf; |
| 1600 | |
| 1601 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 1602 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 1603 | #endif |
| 1604 | |
| 1605 | for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1606 | if (is_inter) { |
| 1607 | if (x->use_default_inter_tx_type && |
| 1608 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 1609 | continue; |
| 1610 | if (!ext_tx_used_inter[ext_tx_set][tx_type]) continue; |
| 1611 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 1612 | if (!do_tx_type_search(tx_type, prune)) continue; |
| 1613 | } |
| 1614 | } else { |
| 1615 | if (x->use_default_intra_tx_type && |
| 1616 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 1617 | continue; |
| 1618 | if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) { |
| 1619 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 1620 | } |
| 1621 | if (!ext_tx_used_intra[ext_tx_set][tx_type]) continue; |
| 1622 | } |
| 1623 | |
| 1624 | mbmi->tx_type = tx_type; |
| 1625 | |
| 1626 | txfm_rd_in_plane(x, cpi, &r, &d, &s, &psse, ref_best_rd, 0, bs, |
| 1627 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1628 | #if CONFIG_PVQ |
| 1629 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 1630 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1631 | if (r == INT_MAX) continue; |
| 1632 | if (get_ext_tx_types(mbmi->tx_size, bs, is_inter) > 1) { |
| 1633 | if (is_inter) { |
| 1634 | if (ext_tx_set > 0) |
| 1635 | r += cpi->inter_tx_type_costs[ext_tx_set][mbmi->tx_size] |
| 1636 | [mbmi->tx_type]; |
| 1637 | } else { |
| 1638 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) |
| 1639 | r += cpi->intra_tx_type_costs[ext_tx_set][mbmi->tx_size][mbmi->mode] |
| 1640 | [mbmi->tx_type]; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | if (s) |
| 1645 | this_rd = RDCOST(x->rdmult, x->rddiv, s1, psse); |
| 1646 | else |
| 1647 | this_rd = RDCOST(x->rdmult, x->rddiv, r + s0, d); |
| 1648 | if (is_inter_block(mbmi) && !xd->lossless[mbmi->segment_id] && !s) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1649 | this_rd = AOMMIN(this_rd, RDCOST(x->rdmult, x->rddiv, s1, psse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1650 | |
| 1651 | if (this_rd < best_rd) { |
| 1652 | best_rd = this_rd; |
| 1653 | best_tx_type = mbmi->tx_type; |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 1654 | *distortion = d; |
| 1655 | *rate = r; |
| 1656 | *skip = s; |
| 1657 | *sse = psse; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1658 | #if CONFIG_PVQ |
| 1659 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 1660 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1661 | } |
| 1662 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1663 | #if CONFIG_PVQ |
| 1664 | od_encode_rollback(&x->daala_enc, &post_buf); |
| 1665 | #endif |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 1666 | } else { |
| 1667 | mbmi->tx_type = DCT_DCT; |
| 1668 | txfm_rd_in_plane(x, cpi, rate, distortion, skip, sse, ref_best_rd, 0, bs, |
| 1669 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1670 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1671 | #else // CONFIG_EXT_TX |
| 1672 | if (mbmi->tx_size < TX_32X32 && !xd->lossless[mbmi->segment_id]) { |
| 1673 | for (tx_type = 0; tx_type < TX_TYPES; ++tx_type) { |
| 1674 | if (!is_inter && x->use_default_intra_tx_type && |
| 1675 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 1676 | continue; |
| 1677 | if (is_inter && x->use_default_inter_tx_type && |
| 1678 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 1679 | continue; |
| 1680 | mbmi->tx_type = tx_type; |
| 1681 | txfm_rd_in_plane(x, cpi, &r, &d, &s, &psse, ref_best_rd, 0, bs, |
| 1682 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
| 1683 | if (r == INT_MAX) continue; |
| 1684 | if (is_inter) { |
| 1685 | r += cpi->inter_tx_type_costs[mbmi->tx_size][mbmi->tx_type]; |
| 1686 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 1687 | !do_tx_type_search(tx_type, prune)) |
| 1688 | continue; |
| 1689 | } else { |
| 1690 | r += cpi->intra_tx_type_costs[mbmi->tx_size] |
| 1691 | [intra_mode_to_tx_type_context[mbmi->mode]] |
| 1692 | [mbmi->tx_type]; |
| 1693 | } |
| 1694 | if (s) |
| 1695 | this_rd = RDCOST(x->rdmult, x->rddiv, s1, psse); |
| 1696 | else |
| 1697 | this_rd = RDCOST(x->rdmult, x->rddiv, r + s0, d); |
| 1698 | if (is_inter && !xd->lossless[mbmi->segment_id] && !s) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1699 | this_rd = AOMMIN(this_rd, RDCOST(x->rdmult, x->rddiv, s1, psse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1700 | |
| 1701 | if (this_rd < best_rd) { |
| 1702 | best_rd = this_rd; |
| 1703 | best_tx_type = mbmi->tx_type; |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 1704 | *distortion = d; |
| 1705 | *rate = r; |
| 1706 | *skip = s; |
| 1707 | *sse = psse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1708 | } |
| 1709 | } |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 1710 | } else { |
| 1711 | mbmi->tx_type = DCT_DCT; |
| 1712 | txfm_rd_in_plane(x, cpi, rate, distortion, skip, sse, ref_best_rd, 0, bs, |
| 1713 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1714 | } |
| 1715 | #endif // CONFIG_EXT_TX |
| 1716 | mbmi->tx_type = best_tx_type; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1719 | static void choose_smallest_tx_size(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 1720 | int *rate, int64_t *distortion, int *skip, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1721 | int64_t *sse, int64_t ref_best_rd, |
| 1722 | BLOCK_SIZE bs) { |
| 1723 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1724 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 1725 | |
| 1726 | mbmi->tx_size = TX_4X4; |
| 1727 | mbmi->tx_type = DCT_DCT; |
| 1728 | |
| 1729 | txfm_rd_in_plane(x, cpi, rate, distortion, skip, sse, ref_best_rd, 0, bs, |
| 1730 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
| 1731 | } |
| 1732 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1733 | static void choose_tx_size_type_from_rd(const AV1_COMP *const cpi, |
| 1734 | MACROBLOCK *x, int *rate, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1735 | int64_t *distortion, int *skip, |
| 1736 | int64_t *psse, int64_t ref_best_rd, |
| 1737 | BLOCK_SIZE bs) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1738 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1739 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 1740 | int r, s; |
| 1741 | int64_t d, sse; |
| 1742 | int64_t rd = INT64_MAX; |
| 1743 | int64_t best_rd = INT64_MAX; |
| 1744 | TX_SIZE best_tx = max_txsize_lookup[bs]; |
| 1745 | const int is_inter = is_inter_block(mbmi); |
| 1746 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
| 1747 | int prune = 0; |
| 1748 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1749 | #if CONFIG_PVQ |
| 1750 | od_rollback_buffer buf; |
| 1751 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1752 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 1753 | // passing -1 in for tx_type indicates that all 1D |
| 1754 | // transforms should be considered for pruning |
| 1755 | prune = prune_tx_types(cpi, bs, x, xd, -1); |
| 1756 | |
| 1757 | *distortion = INT64_MAX; |
| 1758 | *rate = INT_MAX; |
| 1759 | *skip = 0; |
| 1760 | *psse = INT64_MAX; |
| 1761 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1762 | #if CONFIG_PVQ |
| 1763 | od_encode_checkpoint(&x->daala_enc, &buf); |
| 1764 | #endif |
| 1765 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1766 | for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) { |
| 1767 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 1768 | if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1769 | #endif |
| 1770 | rd = choose_tx_size_fix_type(cpi, bs, x, &r, &d, &s, &sse, ref_best_rd, |
| 1771 | tx_type, prune); |
| 1772 | if (rd < best_rd) { |
| 1773 | best_rd = rd; |
| 1774 | *distortion = d; |
| 1775 | *rate = r; |
| 1776 | *skip = s; |
| 1777 | *psse = sse; |
| 1778 | best_tx_type = tx_type; |
| 1779 | best_tx = mbmi->tx_size; |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | mbmi->tx_size = best_tx; |
| 1784 | mbmi->tx_type = best_tx_type; |
| 1785 | |
| 1786 | #if !CONFIG_EXT_TX |
| 1787 | if (mbmi->tx_size >= TX_32X32) assert(mbmi->tx_type == DCT_DCT); |
| 1788 | #endif |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1789 | #if CONFIG_PVQ |
| 1790 | if (best_tx < TX_SIZES) |
| 1791 | txfm_rd_in_plane(x, cpi, &r, &d, &s, &sse, ref_best_rd, 0, bs, best_tx, |
| 1792 | cpi->sf.use_fast_coef_costing); |
| 1793 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1796 | static void super_block_yrd(const AV1_COMP *const cpi, MACROBLOCK *x, int *rate, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1797 | int64_t *distortion, int *skip, int64_t *psse, |
| 1798 | BLOCK_SIZE bs, int64_t ref_best_rd) { |
| 1799 | MACROBLOCKD *xd = &x->e_mbd; |
| 1800 | int64_t sse; |
| 1801 | int64_t *ret_sse = psse ? psse : &sse; |
| 1802 | |
| 1803 | assert(bs == xd->mi[0]->mbmi.sb_type); |
| 1804 | |
| 1805 | if (xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
| 1806 | choose_smallest_tx_size(cpi, x, rate, distortion, skip, ret_sse, |
| 1807 | ref_best_rd, bs); |
| 1808 | } else if (cpi->sf.tx_size_search_method == USE_LARGESTALL) { |
| 1809 | choose_largest_tx_size(cpi, x, rate, distortion, skip, ret_sse, ref_best_rd, |
| 1810 | bs); |
| 1811 | } else { |
| 1812 | choose_tx_size_type_from_rd(cpi, x, rate, distortion, skip, ret_sse, |
| 1813 | ref_best_rd, bs); |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | static int conditional_skipintra(PREDICTION_MODE mode, |
| 1818 | PREDICTION_MODE best_intra_mode) { |
| 1819 | if (mode == D117_PRED && best_intra_mode != V_PRED && |
| 1820 | best_intra_mode != D135_PRED) |
| 1821 | return 1; |
| 1822 | if (mode == D63_PRED && best_intra_mode != V_PRED && |
| 1823 | best_intra_mode != D45_PRED) |
| 1824 | return 1; |
| 1825 | if (mode == D207_PRED && best_intra_mode != H_PRED && |
| 1826 | best_intra_mode != D45_PRED) |
| 1827 | return 1; |
| 1828 | if (mode == D153_PRED && best_intra_mode != H_PRED && |
| 1829 | best_intra_mode != D135_PRED) |
| 1830 | return 1; |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 1834 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1835 | static int rd_pick_palette_intra_sby( |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1836 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int palette_ctx, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1837 | int dc_mode_cost, PALETTE_MODE_INFO *palette_mode_info, |
| 1838 | uint8_t *best_palette_color_map, TX_SIZE *best_tx, TX_TYPE *best_tx_type, |
| 1839 | PREDICTION_MODE *mode_selected, int64_t *best_rd) { |
| 1840 | int rate_overhead = 0; |
| 1841 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1842 | MODE_INFO *const mic = xd->mi[0]; |
| 1843 | const int rows = 4 * num_4x4_blocks_high_lookup[bsize]; |
| 1844 | const int cols = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 1845 | int this_rate, this_rate_tokenonly, s, colors, n; |
| 1846 | int64_t this_distortion, this_rd; |
| 1847 | const int src_stride = x->plane[0].src.stride; |
| 1848 | const uint8_t *const src = x->plane[0].src.buf; |
| 1849 | |
| 1850 | assert(cpi->common.allow_screen_content_tools); |
| 1851 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1852 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1853 | if (cpi->common.use_highbitdepth) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1854 | colors = av1_count_colors_highbd(src, src_stride, rows, cols, |
| 1855 | cpi->common.bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1856 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1857 | #endif // CONFIG_AOM_HIGHBITDEPTH |
| 1858 | colors = av1_count_colors(src, src_stride, rows, cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1859 | palette_mode_info->palette_size[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1860 | #if CONFIG_FILTER_INTRA |
| 1861 | mic->mbmi.filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 1862 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1863 | |
| 1864 | if (colors > 1 && colors <= 64) { |
| 1865 | int r, c, i, j, k; |
| 1866 | const int max_itr = 50; |
Urvang Joshi | 967ff39 | 2016-09-07 14:57:49 -0700 | [diff] [blame] | 1867 | uint8_t color_order[PALETTE_MAX_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1868 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 1869 | float centroids[PALETTE_MAX_SIZE]; |
| 1870 | uint8_t *const color_map = xd->plane[0].color_index_map; |
| 1871 | float lb, ub, val; |
| 1872 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
| 1873 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1874 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1875 | uint16_t *src16 = CONVERT_TO_SHORTPTR(src); |
| 1876 | if (cpi->common.use_highbitdepth) |
| 1877 | lb = ub = src16[0]; |
| 1878 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1879 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1880 | lb = ub = src[0]; |
| 1881 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1882 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1883 | if (cpi->common.use_highbitdepth) { |
| 1884 | for (r = 0; r < rows; ++r) { |
| 1885 | for (c = 0; c < cols; ++c) { |
| 1886 | val = src16[r * src_stride + c]; |
| 1887 | data[r * cols + c] = val; |
| 1888 | if (val < lb) |
| 1889 | lb = val; |
| 1890 | else if (val > ub) |
| 1891 | ub = val; |
| 1892 | } |
| 1893 | } |
| 1894 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1895 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1896 | for (r = 0; r < rows; ++r) { |
| 1897 | for (c = 0; c < cols; ++c) { |
| 1898 | val = src[r * src_stride + c]; |
| 1899 | data[r * cols + c] = val; |
| 1900 | if (val < lb) |
| 1901 | lb = val; |
| 1902 | else if (val > ub) |
| 1903 | ub = val; |
| 1904 | } |
| 1905 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1906 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1907 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1908 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1909 | |
| 1910 | mbmi->mode = DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1911 | #if CONFIG_FILTER_INTRA |
| 1912 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 1913 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1914 | |
| 1915 | if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return 0; |
| 1916 | |
| 1917 | for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2; |
| 1918 | --n) { |
| 1919 | for (i = 0; i < n; ++i) |
| 1920 | centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1921 | av1_k_means(data, centroids, color_map, rows * cols, n, 1, max_itr); |
| 1922 | k = av1_remove_duplicates(centroids, n); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1923 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1924 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1925 | if (cpi->common.use_highbitdepth) |
| 1926 | for (i = 0; i < k; ++i) |
| 1927 | pmi->palette_colors[i] = |
| 1928 | clip_pixel_highbd((int)centroids[i], cpi->common.bit_depth); |
| 1929 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1930 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1931 | for (i = 0; i < k; ++i) |
| 1932 | pmi->palette_colors[i] = clip_pixel((int)centroids[i]); |
| 1933 | pmi->palette_size[0] = k; |
| 1934 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1935 | av1_calc_indices(data, centroids, color_map, rows * cols, k, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1936 | |
| 1937 | super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL, |
| 1938 | bsize, *best_rd); |
| 1939 | if (this_rate_tokenonly == INT_MAX) continue; |
| 1940 | |
| 1941 | this_rate = |
| 1942 | this_rate_tokenonly + dc_mode_cost + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1943 | cpi->common.bit_depth * k * av1_cost_bit(128, 0) + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1944 | cpi->palette_y_size_cost[bsize - BLOCK_8X8][k - 2] + |
| 1945 | write_uniform_cost(k, color_map[0]) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1946 | av1_cost_bit( |
| 1947 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1948 | 1); |
| 1949 | for (i = 0; i < rows; ++i) { |
| 1950 | for (j = (i == 0 ? 1 : 0); j < cols; ++j) { |
Urvang Joshi | 967ff39 | 2016-09-07 14:57:49 -0700 | [diff] [blame] | 1951 | int color_idx; |
| 1952 | const int color_ctx = av1_get_palette_color_context( |
| 1953 | color_map, cols, i, j, k, color_order, &color_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1954 | assert(color_idx >= 0 && color_idx < k); |
| 1955 | this_rate += cpi->palette_y_color_cost[k - 2][color_ctx][color_idx]; |
| 1956 | } |
| 1957 | } |
| 1958 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
| 1959 | |
| 1960 | if (this_rd < *best_rd) { |
| 1961 | *best_rd = this_rd; |
| 1962 | *palette_mode_info = *pmi; |
| 1963 | memcpy(best_palette_color_map, color_map, |
| 1964 | rows * cols * sizeof(color_map[0])); |
| 1965 | *mode_selected = DC_PRED; |
| 1966 | *best_tx = mbmi->tx_size; |
| 1967 | *best_tx_type = mbmi->tx_type; |
| 1968 | rate_overhead = this_rate - this_rate_tokenonly; |
| 1969 | } |
| 1970 | } |
| 1971 | } |
| 1972 | return rate_overhead; |
| 1973 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 1974 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1975 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 1976 | static int64_t rd_pick_intra4x4block( |
| 1977 | const AV1_COMP *const cpi, MACROBLOCK *x, int row, int col, |
| 1978 | PREDICTION_MODE *best_mode, const int *bmode_costs, ENTROPY_CONTEXT *a, |
| 1979 | ENTROPY_CONTEXT *l, int *bestrate, int *bestratey, int64_t *bestdistortion, |
| 1980 | BLOCK_SIZE bsize, int *y_skip, int64_t rd_thresh) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1981 | #if !CONFIG_PVQ |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 1982 | const AV1_COMMON *const cm = &cpi->common; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1983 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1984 | PREDICTION_MODE mode; |
| 1985 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1986 | int64_t best_rd = rd_thresh; |
| 1987 | struct macroblock_plane *p = &x->plane[0]; |
| 1988 | struct macroblockd_plane *pd = &xd->plane[0]; |
| 1989 | const int src_stride = p->src.stride; |
| 1990 | const int dst_stride = pd->dst.stride; |
| 1991 | const uint8_t *src_init = &p->src.buf[row * 4 * src_stride + col * 4]; |
| 1992 | uint8_t *dst_init = &pd->dst.buf[row * 4 * src_stride + col * 4]; |
| 1993 | ENTROPY_CONTEXT ta[2], tempa[2]; |
| 1994 | ENTROPY_CONTEXT tl[2], templ[2]; |
| 1995 | const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; |
| 1996 | const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; |
| 1997 | int idx, idy; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 1998 | int best_can_skip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1999 | uint8_t best_dst[8 * 8]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2000 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2001 | uint16_t best_dst16[8 * 8]; |
| 2002 | #endif |
| 2003 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2004 | #if CONFIG_PVQ |
| 2005 | od_rollback_buffer pre_buf, post_buf; |
| 2006 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 2007 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 2008 | #endif |
| 2009 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2010 | memcpy(ta, a, num_4x4_blocks_wide * sizeof(a[0])); |
| 2011 | memcpy(tl, l, num_4x4_blocks_high * sizeof(l[0])); |
| 2012 | xd->mi[0]->mbmi.tx_size = TX_4X4; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2013 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2014 | xd->mi[0]->mbmi.palette_mode_info.palette_size[0] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2015 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2016 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2017 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2018 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 2019 | for (mode = DC_PRED; mode <= TM_PRED; ++mode) { |
| 2020 | int64_t this_rd; |
| 2021 | int ratey = 0; |
| 2022 | int64_t distortion = 0; |
| 2023 | int rate = bmode_costs[mode]; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2024 | int can_skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2025 | |
| 2026 | if (!(cpi->sf.intra_y_mode_mask[TX_4X4] & (1 << mode))) continue; |
| 2027 | |
| 2028 | // Only do the oblique modes if the best so far is |
| 2029 | // one of the neighboring directional modes |
| 2030 | if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 2031 | if (conditional_skipintra(mode, *best_mode)) continue; |
| 2032 | } |
| 2033 | |
| 2034 | memcpy(tempa, ta, num_4x4_blocks_wide * sizeof(ta[0])); |
| 2035 | memcpy(templ, tl, num_4x4_blocks_high * sizeof(tl[0])); |
| 2036 | |
| 2037 | for (idy = 0; idy < num_4x4_blocks_high; ++idy) { |
| 2038 | for (idx = 0; idx < num_4x4_blocks_wide; ++idx) { |
| 2039 | const int block = (row + idy) * 2 + (col + idx); |
| 2040 | const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride]; |
| 2041 | uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride]; |
| 2042 | int16_t *const src_diff = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2043 | av1_raster_block_offset_int16(BLOCK_8X8, block, p->src_diff); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2044 | xd->mi[0]->bmi[block].as_mode = mode; |
Jingning Han | c4c99da | 2016-10-24 10:27:28 -0700 | [diff] [blame] | 2045 | av1_predict_intra_block(xd, pd->width, pd->height, TX_4X4, mode, dst, |
| 2046 | dst_stride, dst, dst_stride, col + idx, |
| 2047 | row + idy, 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2048 | aom_highbd_subtract_block(4, 4, src_diff, 8, src, src_stride, dst, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2049 | dst_stride, xd->bd); |
| 2050 | if (xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
| 2051 | TX_TYPE tx_type = get_tx_type(PLANE_TYPE_Y, xd, block, TX_4X4); |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2052 | const SCAN_ORDER *scan_order = get_scan(cm, TX_4X4, tx_type, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2053 | const int coeff_ctx = |
| 2054 | combine_entropy_contexts(*(tempa + idx), *(templ + idy)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2055 | #if CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2056 | av1_xform_quant_fp_nuq(cm, x, 0, block, row + idy, col + idx, |
| 2057 | BLOCK_8X8, TX_4X4, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2058 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2059 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2060 | TX_4X4, AV1_XFORM_QUANT_FP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2061 | #endif // CONFIG_NEW_QUANT |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 2062 | ratey += av1_cost_coeffs(cm, x, 0, block, coeff_ctx, TX_4X4, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 2063 | scan_order->scan, scan_order->neighbors, |
| 2064 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2065 | *(tempa + idx) = !(p->eobs[block] == 0); |
| 2066 | *(templ + idy) = !(p->eobs[block] == 0); |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2067 | can_skip &= (p->eobs[block] == 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2068 | if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd) |
| 2069 | goto next_highbd; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2070 | av1_highbd_inv_txfm_add_4x4(BLOCK_OFFSET(pd->dqcoeff, block), dst, |
| 2071 | dst_stride, p->eobs[block], xd->bd, |
| 2072 | DCT_DCT, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2073 | } else { |
| 2074 | int64_t dist; |
| 2075 | unsigned int tmp; |
| 2076 | TX_TYPE tx_type = get_tx_type(PLANE_TYPE_Y, xd, block, TX_4X4); |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2077 | const SCAN_ORDER *scan_order = get_scan(cm, TX_4X4, tx_type, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2078 | const int coeff_ctx = |
| 2079 | combine_entropy_contexts(*(tempa + idx), *(templ + idy)); |
| 2080 | #if CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2081 | av1_xform_quant_fp_nuq(cm, x, 0, block, row + idy, col + idx, |
| 2082 | BLOCK_8X8, TX_4X4, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2083 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2084 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2085 | TX_4X4, AV1_XFORM_QUANT_FP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2086 | #endif // CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2087 | av1_optimize_b(cm, x, 0, block, TX_4X4, coeff_ctx); |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 2088 | ratey += av1_cost_coeffs(cm, x, 0, block, coeff_ctx, TX_4X4, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 2089 | scan_order->scan, scan_order->neighbors, |
| 2090 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2091 | *(tempa + idx) = !(p->eobs[block] == 0); |
| 2092 | *(templ + idy) = !(p->eobs[block] == 0); |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2093 | can_skip &= (p->eobs[block] == 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2094 | av1_highbd_inv_txfm_add_4x4(BLOCK_OFFSET(pd->dqcoeff, block), dst, |
| 2095 | dst_stride, p->eobs[block], xd->bd, |
| 2096 | tx_type, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2097 | cpi->fn_ptr[BLOCK_4X4].vf(src, src_stride, dst, dst_stride, &tmp); |
| 2098 | dist = (int64_t)tmp << 4; |
| 2099 | distortion += dist; |
| 2100 | if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd) |
| 2101 | goto next_highbd; |
| 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | rate += ratey; |
| 2107 | this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); |
| 2108 | |
| 2109 | if (this_rd < best_rd) { |
| 2110 | *bestrate = rate; |
| 2111 | *bestratey = ratey; |
| 2112 | *bestdistortion = distortion; |
| 2113 | best_rd = this_rd; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2114 | best_can_skip = can_skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2115 | *best_mode = mode; |
| 2116 | memcpy(a, tempa, num_4x4_blocks_wide * sizeof(tempa[0])); |
| 2117 | memcpy(l, templ, num_4x4_blocks_high * sizeof(templ[0])); |
| 2118 | for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) { |
| 2119 | memcpy(best_dst16 + idy * 8, |
| 2120 | CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride), |
| 2121 | num_4x4_blocks_wide * 4 * sizeof(uint16_t)); |
| 2122 | } |
| 2123 | } |
| 2124 | next_highbd : {} |
| 2125 | } |
| 2126 | |
| 2127 | if (best_rd >= rd_thresh) return best_rd; |
| 2128 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2129 | if (y_skip) *y_skip &= best_can_skip; |
| 2130 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2131 | for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) { |
| 2132 | memcpy(CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride), |
| 2133 | best_dst16 + idy * 8, num_4x4_blocks_wide * 4 * sizeof(uint16_t)); |
| 2134 | } |
| 2135 | |
| 2136 | return best_rd; |
| 2137 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2138 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2139 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2140 | #if CONFIG_PVQ |
| 2141 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 2142 | #endif |
| 2143 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2144 | for (mode = DC_PRED; mode <= TM_PRED; ++mode) { |
| 2145 | int64_t this_rd; |
| 2146 | int ratey = 0; |
| 2147 | int64_t distortion = 0; |
| 2148 | int rate = bmode_costs[mode]; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2149 | int can_skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2150 | |
| 2151 | if (!(cpi->sf.intra_y_mode_mask[TX_4X4] & (1 << mode))) continue; |
| 2152 | |
| 2153 | // Only do the oblique modes if the best so far is |
| 2154 | // one of the neighboring directional modes |
| 2155 | if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 2156 | if (conditional_skipintra(mode, *best_mode)) continue; |
| 2157 | } |
| 2158 | |
| 2159 | memcpy(tempa, ta, num_4x4_blocks_wide * sizeof(ta[0])); |
| 2160 | memcpy(templ, tl, num_4x4_blocks_high * sizeof(tl[0])); |
| 2161 | |
| 2162 | for (idy = 0; idy < num_4x4_blocks_high; ++idy) { |
| 2163 | for (idx = 0; idx < num_4x4_blocks_wide; ++idx) { |
| 2164 | const int block = (row + idy) * 2 + (col + idx); |
| 2165 | const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride]; |
| 2166 | uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2167 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2168 | int16_t *const src_diff = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2169 | av1_raster_block_offset_int16(BLOCK_8X8, block, p->src_diff); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2170 | #else |
| 2171 | int lossless = xd->lossless[xd->mi[0]->mbmi.segment_id]; |
| 2172 | const int diff_stride = 8; |
| 2173 | tran_low_t *const coeff = BLOCK_OFFSET(x->plane[0].coeff, block); |
| 2174 | tran_low_t *const dqcoeff = BLOCK_OFFSET(xd->plane[0].dqcoeff, block); |
| 2175 | tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block); |
| 2176 | int16_t *pred = &pd->pred[4 * (row * diff_stride + col)]; |
| 2177 | int16_t *src_int16 = &p->src_int16[4 * (row * diff_stride + col)]; |
| 2178 | int i, j, tx_blk_size; |
| 2179 | TX_TYPE tx_type = get_tx_type(PLANE_TYPE_Y, xd, block, TX_4X4); |
| 2180 | int rate_pvq; |
| 2181 | int skip; |
| 2182 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2183 | xd->mi[0]->bmi[block].as_mode = mode; |
Jingning Han | c4c99da | 2016-10-24 10:27:28 -0700 | [diff] [blame] | 2184 | av1_predict_intra_block(xd, pd->width, pd->height, TX_4X4, mode, dst, |
| 2185 | dst_stride, dst, dst_stride, col + idx, |
| 2186 | row + idy, 0); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2187 | #if !CONFIG_PVQ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2188 | aom_subtract_block(4, 4, src_diff, 8, src, src_stride, dst, dst_stride); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2189 | #else |
| 2190 | if (lossless) tx_type = DCT_DCT; |
| 2191 | // transform block size in pixels |
| 2192 | tx_blk_size = 4; |
| 2193 | |
| 2194 | // copy uint8 orig and predicted block to int16 buffer |
| 2195 | // in order to use existing VP10 transform functions |
| 2196 | for (j = 0; j < tx_blk_size; j++) |
| 2197 | for (i = 0; i < tx_blk_size; i++) { |
| 2198 | src_int16[diff_stride * j + i] = src[src_stride * j + i]; |
| 2199 | pred[diff_stride * j + i] = dst[dst_stride * j + i]; |
| 2200 | } |
| 2201 | { |
| 2202 | FWD_TXFM_PARAM fwd_txfm_param; |
| 2203 | fwd_txfm_param.tx_type = tx_type; |
| 2204 | fwd_txfm_param.tx_size = TX_4X4; |
| 2205 | fwd_txfm_param.fwd_txfm_opt = FWD_TXFM_OPT_NORMAL; |
| 2206 | fwd_txfm_param.rd_transform = 0; |
| 2207 | fwd_txfm_param.lossless = lossless; |
| 2208 | fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param); |
| 2209 | fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param); |
| 2210 | } |
| 2211 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2212 | |
| 2213 | if (xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2214 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2215 | TX_TYPE tx_type = get_tx_type(PLANE_TYPE_Y, xd, block, TX_4X4); |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2216 | const SCAN_ORDER *scan_order = get_scan(cm, TX_4X4, tx_type, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2217 | const int coeff_ctx = |
| 2218 | combine_entropy_contexts(*(tempa + idx), *(templ + idy)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2219 | #if CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2220 | av1_xform_quant_fp_nuq(cm, x, 0, block, row + idy, col + idx, |
| 2221 | BLOCK_8X8, TX_4X4, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2222 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2223 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
| 2224 | TX_4X4, AV1_XFORM_QUANT_B); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2225 | #endif // CONFIG_NEW_QUANT |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 2226 | ratey += av1_cost_coeffs(cm, x, 0, block, coeff_ctx, TX_4X4, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 2227 | scan_order->scan, scan_order->neighbors, |
| 2228 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2229 | *(tempa + idx) = !(p->eobs[block] == 0); |
| 2230 | *(templ + idy) = !(p->eobs[block] == 0); |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2231 | can_skip &= (p->eobs[block] == 0); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2232 | #else |
| 2233 | skip = av1_pvq_encode_helper(&x->daala_enc, coeff, ref_coeff, dqcoeff, |
| 2234 | &p->eobs[block], pd->dequant, 0, TX_4X4, |
| 2235 | tx_type, &rate_pvq, x->pvq_speed, NULL); |
| 2236 | ratey += rate_pvq; |
| 2237 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2238 | if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd) |
| 2239 | goto next; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2240 | #if CONFIG_PVQ |
| 2241 | if (!skip) { |
| 2242 | for (j = 0; j < tx_blk_size; j++) |
| 2243 | for (i = 0; i < tx_blk_size; i++) dst[j * dst_stride + i] = 0; |
| 2244 | #endif |
| 2245 | av1_inv_txfm_add_4x4(BLOCK_OFFSET(pd->dqcoeff, block), dst, |
| 2246 | dst_stride, p->eobs[block], DCT_DCT, 1); |
| 2247 | #if CONFIG_PVQ |
| 2248 | } |
| 2249 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2250 | } else { |
| 2251 | int64_t dist; |
| 2252 | unsigned int tmp; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2253 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2254 | TX_TYPE tx_type = get_tx_type(PLANE_TYPE_Y, xd, block, TX_4X4); |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2255 | const SCAN_ORDER *scan_order = get_scan(cm, TX_4X4, tx_type, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2256 | const int coeff_ctx = |
| 2257 | combine_entropy_contexts(*(tempa + idx), *(templ + idy)); |
| 2258 | #if CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2259 | av1_xform_quant_fp_nuq(cm, x, 0, block, row + idy, col + idx, |
| 2260 | BLOCK_8X8, TX_4X4, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2261 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2262 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
| 2263 | TX_4X4, AV1_XFORM_QUANT_FP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2264 | #endif // CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 2265 | av1_optimize_b(cm, x, 0, block, TX_4X4, coeff_ctx); |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 2266 | ratey += av1_cost_coeffs(cm, x, 0, block, coeff_ctx, TX_4X4, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 2267 | scan_order->scan, scan_order->neighbors, |
| 2268 | cpi->sf.use_fast_coef_costing); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2269 | #else |
| 2270 | skip = av1_pvq_encode_helper(&x->daala_enc, coeff, ref_coeff, dqcoeff, |
| 2271 | &p->eobs[block], pd->dequant, 0, TX_4X4, |
| 2272 | tx_type, &rate_pvq, x->pvq_speed, NULL); |
| 2273 | ratey += rate_pvq; |
| 2274 | #endif |
| 2275 | // No need for av1_block_error2_c because the ssz is unused |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2276 | av1_inv_txfm_add_4x4(BLOCK_OFFSET(pd->dqcoeff, block), dst, |
| 2277 | dst_stride, p->eobs[block], tx_type, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2278 | cpi->fn_ptr[BLOCK_4X4].vf(src, src_stride, dst, dst_stride, &tmp); |
| 2279 | dist = (int64_t)tmp << 4; |
| 2280 | distortion += dist; |
| 2281 | // To use the pixel domain distortion, the step below needs to be |
| 2282 | // put behind the inv txfm. Compared to calculating the distortion |
| 2283 | // in the frequency domain, the overhead of encoding effort is low. |
| 2284 | if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd) |
| 2285 | goto next; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2286 | #if CONFIG_PVQ |
| 2287 | if (!skip) { |
| 2288 | for (j = 0; j < tx_blk_size; j++) |
| 2289 | for (i = 0; i < tx_blk_size; i++) dst[j * dst_stride + i] = 0; |
| 2290 | #endif |
| 2291 | #if CONFIG_PVQ |
| 2292 | } |
| 2293 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | rate += ratey; |
| 2299 | this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); |
| 2300 | |
| 2301 | if (this_rd < best_rd) { |
| 2302 | *bestrate = rate; |
| 2303 | *bestratey = ratey; |
| 2304 | *bestdistortion = distortion; |
| 2305 | best_rd = this_rd; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2306 | best_can_skip = can_skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2307 | *best_mode = mode; |
| 2308 | memcpy(a, tempa, num_4x4_blocks_wide * sizeof(tempa[0])); |
| 2309 | memcpy(l, templ, num_4x4_blocks_high * sizeof(templ[0])); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2310 | #if CONFIG_PVQ |
| 2311 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 2312 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2313 | for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) |
| 2314 | memcpy(best_dst + idy * 8, dst_init + idy * dst_stride, |
| 2315 | num_4x4_blocks_wide * 4); |
| 2316 | } |
| 2317 | next : {} |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2318 | #if CONFIG_PVQ |
| 2319 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 2320 | #endif |
| 2321 | } // mode decision loop |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2322 | |
| 2323 | if (best_rd >= rd_thresh) return best_rd; |
| 2324 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2325 | #if CONFIG_PVQ |
| 2326 | od_encode_rollback(&x->daala_enc, &post_buf); |
| 2327 | #endif |
| 2328 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2329 | if (y_skip) *y_skip &= best_can_skip; |
| 2330 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2331 | for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) |
| 2332 | memcpy(dst_init + idy * dst_stride, best_dst + idy * 8, |
| 2333 | num_4x4_blocks_wide * 4); |
| 2334 | |
| 2335 | return best_rd; |
| 2336 | } |
| 2337 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2338 | static int64_t rd_pick_intra_sub_8x8_y_mode(const AV1_COMP *const cpi, |
| 2339 | MACROBLOCK *mb, int *rate, |
| 2340 | int *rate_y, int64_t *distortion, |
| 2341 | int *y_skip, int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2342 | int i, j; |
| 2343 | const MACROBLOCKD *const xd = &mb->e_mbd; |
| 2344 | MODE_INFO *const mic = xd->mi[0]; |
| 2345 | const MODE_INFO *above_mi = xd->above_mi; |
| 2346 | const MODE_INFO *left_mi = xd->left_mi; |
| 2347 | const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type; |
| 2348 | const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; |
| 2349 | const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; |
| 2350 | int idx, idy; |
| 2351 | int cost = 0; |
| 2352 | int64_t total_distortion = 0; |
| 2353 | int tot_rate_y = 0; |
| 2354 | int64_t total_rd = 0; |
| 2355 | const int *bmode_costs = cpi->mbmode_cost[0]; |
| 2356 | |
| 2357 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2358 | mic->mbmi.intra_filter = INTRA_FILTER_LINEAR; |
| 2359 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2360 | #if CONFIG_FILTER_INTRA |
| 2361 | mic->mbmi.filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 2362 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2363 | |
| 2364 | // TODO(any): Add search of the tx_type to improve rd performance at the |
| 2365 | // expense of speed. |
| 2366 | mic->mbmi.tx_type = DCT_DCT; |
| 2367 | mic->mbmi.tx_size = TX_4X4; |
| 2368 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2369 | if (y_skip) *y_skip = 1; |
| 2370 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2371 | // Pick modes for each sub-block (of size 4x4, 4x8, or 8x4) in an 8x8 block. |
| 2372 | for (idy = 0; idy < 2; idy += num_4x4_blocks_high) { |
| 2373 | for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) { |
| 2374 | PREDICTION_MODE best_mode = DC_PRED; |
| 2375 | int r = INT_MAX, ry = INT_MAX; |
| 2376 | int64_t d = INT64_MAX, this_rd = INT64_MAX; |
| 2377 | i = idy * 2 + idx; |
| 2378 | if (cpi->common.frame_type == KEY_FRAME) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2379 | const PREDICTION_MODE A = av1_above_block_mode(mic, above_mi, i); |
| 2380 | const PREDICTION_MODE L = av1_left_block_mode(mic, left_mi, i); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2381 | |
| 2382 | bmode_costs = cpi->y_mode_costs[A][L]; |
| 2383 | } |
| 2384 | |
| 2385 | this_rd = rd_pick_intra4x4block( |
| 2386 | cpi, mb, idy, idx, &best_mode, bmode_costs, |
| 2387 | xd->plane[0].above_context + idx, xd->plane[0].left_context + idy, &r, |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 2388 | &ry, &d, bsize, y_skip, best_rd - total_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2389 | if (this_rd >= best_rd - total_rd) return INT64_MAX; |
| 2390 | |
| 2391 | total_rd += this_rd; |
| 2392 | cost += r; |
| 2393 | total_distortion += d; |
| 2394 | tot_rate_y += ry; |
| 2395 | |
| 2396 | mic->bmi[i].as_mode = best_mode; |
| 2397 | for (j = 1; j < num_4x4_blocks_high; ++j) |
| 2398 | mic->bmi[i + j * 2].as_mode = best_mode; |
| 2399 | for (j = 1; j < num_4x4_blocks_wide; ++j) |
| 2400 | mic->bmi[i + j].as_mode = best_mode; |
| 2401 | |
| 2402 | if (total_rd >= best_rd) return INT64_MAX; |
| 2403 | } |
| 2404 | } |
| 2405 | mic->mbmi.mode = mic->bmi[3].as_mode; |
| 2406 | |
| 2407 | // Add in the cost of the transform type |
| 2408 | if (!xd->lossless[mic->mbmi.segment_id]) { |
| 2409 | int rate_tx_type = 0; |
| 2410 | #if CONFIG_EXT_TX |
| 2411 | if (get_ext_tx_types(TX_4X4, bsize, 0) > 1) { |
| 2412 | const int eset = get_ext_tx_set(TX_4X4, bsize, 0); |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 2413 | rate_tx_type = cpi->intra_tx_type_costs[eset][TX_4X4][mic->mbmi.mode] |
| 2414 | [mic->mbmi.tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2415 | } |
| 2416 | #else |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 2417 | rate_tx_type = |
| 2418 | cpi->intra_tx_type_costs[TX_4X4] |
| 2419 | [intra_mode_to_tx_type_context[mic->mbmi.mode]] |
| 2420 | [mic->mbmi.tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2421 | #endif |
| 2422 | assert(mic->mbmi.tx_size == TX_4X4); |
| 2423 | cost += rate_tx_type; |
| 2424 | tot_rate_y += rate_tx_type; |
| 2425 | } |
| 2426 | |
| 2427 | *rate = cost; |
| 2428 | *rate_y = tot_rate_y; |
| 2429 | *distortion = total_distortion; |
| 2430 | |
| 2431 | return RDCOST(mb->rdmult, mb->rddiv, cost, total_distortion); |
| 2432 | } |
| 2433 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2434 | #if CONFIG_FILTER_INTRA |
| 2435 | // Return 1 if an filter intra mode is selected; return 0 otherwise. |
| 2436 | static int rd_pick_filter_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2437 | int *rate, int *rate_tokenonly, |
| 2438 | int64_t *distortion, int *skippable, |
| 2439 | BLOCK_SIZE bsize, int mode_cost, |
| 2440 | int64_t *best_rd, uint16_t skip_mask) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2441 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2442 | MODE_INFO *const mic = xd->mi[0]; |
| 2443 | MB_MODE_INFO *mbmi = &mic->mbmi; |
| 2444 | int this_rate, this_rate_tokenonly, s; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2445 | int filter_intra_selected_flag = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2446 | int64_t this_distortion, this_rd; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2447 | FILTER_INTRA_MODE mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2448 | TX_SIZE best_tx_size = TX_4X4; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2449 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2450 | TX_TYPE best_tx_type; |
| 2451 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2452 | av1_zero(filter_intra_mode_info); |
| 2453 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2454 | mbmi->mode = DC_PRED; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2455 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2456 | mbmi->palette_mode_info.palette_size[0] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2457 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2458 | |
| 2459 | for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) { |
| 2460 | if (skip_mask & (1 << mode)) continue; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2461 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2462 | super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL, |
| 2463 | bsize, *best_rd); |
| 2464 | if (this_rate_tokenonly == INT_MAX) continue; |
| 2465 | |
| 2466 | this_rate = this_rate_tokenonly + |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2467 | av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 1) + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2468 | write_uniform_cost(FILTER_INTRA_MODES, mode) + mode_cost; |
| 2469 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
| 2470 | |
| 2471 | if (this_rd < *best_rd) { |
| 2472 | *best_rd = this_rd; |
| 2473 | best_tx_size = mic->mbmi.tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2474 | filter_intra_mode_info = mbmi->filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2475 | best_tx_type = mic->mbmi.tx_type; |
| 2476 | *rate = this_rate; |
| 2477 | *rate_tokenonly = this_rate_tokenonly; |
| 2478 | *distortion = this_distortion; |
| 2479 | *skippable = s; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2480 | filter_intra_selected_flag = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2484 | if (filter_intra_selected_flag) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2485 | mbmi->mode = DC_PRED; |
| 2486 | mbmi->tx_size = best_tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2487 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = |
| 2488 | filter_intra_mode_info.use_filter_intra_mode[0]; |
| 2489 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = |
| 2490 | filter_intra_mode_info.filter_intra_mode[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2491 | mbmi->tx_type = best_tx_type; |
| 2492 | return 1; |
| 2493 | } else { |
| 2494 | return 0; |
| 2495 | } |
| 2496 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2497 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2498 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2499 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2500 | static void pick_intra_angle_routine_sby( |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2501 | const AV1_COMP *const cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2502 | int64_t *distortion, int *skippable, int *best_angle_delta, |
| 2503 | TX_SIZE *best_tx_size, TX_TYPE *best_tx_type, INTRA_FILTER *best_filter, |
| 2504 | BLOCK_SIZE bsize, int rate_overhead, int64_t *best_rd) { |
| 2505 | int this_rate, this_rate_tokenonly, s; |
| 2506 | int64_t this_distortion, this_rd; |
| 2507 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
| 2508 | super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL, |
| 2509 | bsize, *best_rd); |
| 2510 | if (this_rate_tokenonly == INT_MAX) return; |
| 2511 | |
| 2512 | this_rate = this_rate_tokenonly + rate_overhead; |
| 2513 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
| 2514 | |
| 2515 | if (this_rd < *best_rd) { |
| 2516 | *best_rd = this_rd; |
| 2517 | *best_angle_delta = mbmi->angle_delta[0]; |
| 2518 | *best_tx_size = mbmi->tx_size; |
| 2519 | *best_filter = mbmi->intra_filter; |
| 2520 | *best_tx_type = mbmi->tx_type; |
| 2521 | *rate = this_rate; |
| 2522 | *rate_tokenonly = this_rate_tokenonly; |
| 2523 | *distortion = this_distortion; |
| 2524 | *skippable = s; |
| 2525 | } |
| 2526 | } |
| 2527 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2528 | static int64_t rd_pick_intra_angle_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2529 | int *rate, int *rate_tokenonly, |
| 2530 | int64_t *distortion, int *skippable, |
| 2531 | BLOCK_SIZE bsize, int rate_overhead, |
| 2532 | int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2533 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2534 | MODE_INFO *const mic = xd->mi[0]; |
| 2535 | MB_MODE_INFO *mbmi = &mic->mbmi; |
| 2536 | int this_rate, this_rate_tokenonly, s; |
| 2537 | int angle_delta, best_angle_delta = 0, p_angle; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2538 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2539 | INTRA_FILTER filter, best_filter = INTRA_FILTER_LINEAR; |
| 2540 | const double rd_adjust = 1.2; |
| 2541 | int64_t this_distortion, this_rd; |
| 2542 | TX_SIZE best_tx_size = mic->mbmi.tx_size; |
| 2543 | TX_TYPE best_tx_type = mbmi->tx_type; |
| 2544 | |
| 2545 | if (ANGLE_FAST_SEARCH) { |
| 2546 | int deltas_level1[3] = { 0, -2, 2 }; |
| 2547 | int deltas_level2[3][2] = { |
| 2548 | { -1, 1 }, { -3, -1 }, { 1, 3 }, |
| 2549 | }; |
| 2550 | const int level1 = 3, level2 = 2; |
| 2551 | int i, j, best_i = -1; |
| 2552 | |
| 2553 | for (i = 0; i < level1; ++i) { |
| 2554 | mic->mbmi.angle_delta[0] = deltas_level1[i]; |
| 2555 | p_angle = |
| 2556 | mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
| 2557 | for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) { |
| 2558 | int64_t tmp_best_rd; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2559 | if ((FILTER_FAST_SEARCH || !av1_is_intra_filter_switchable(p_angle)) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2560 | filter != INTRA_FILTER_LINEAR) |
| 2561 | continue; |
| 2562 | mic->mbmi.intra_filter = filter; |
| 2563 | tmp_best_rd = |
| 2564 | (i == 0 && filter == INTRA_FILTER_LINEAR && best_rd < INT64_MAX) |
| 2565 | ? (int64_t)(best_rd * rd_adjust) |
| 2566 | : best_rd; |
| 2567 | super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, |
| 2568 | NULL, bsize, tmp_best_rd); |
| 2569 | if (this_rate_tokenonly == INT_MAX) { |
| 2570 | if (i == 0 && filter == INTRA_FILTER_LINEAR) |
| 2571 | return best_rd; |
| 2572 | else |
| 2573 | continue; |
| 2574 | } |
| 2575 | this_rate = this_rate_tokenonly + rate_overhead + |
| 2576 | cpi->intra_filter_cost[intra_filter_ctx][filter]; |
| 2577 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
| 2578 | if (i == 0 && filter == INTRA_FILTER_LINEAR && best_rd < INT64_MAX && |
| 2579 | this_rd > best_rd * rd_adjust) |
| 2580 | return best_rd; |
| 2581 | if (this_rd < best_rd) { |
| 2582 | best_i = i; |
| 2583 | best_rd = this_rd; |
| 2584 | best_angle_delta = mbmi->angle_delta[0]; |
| 2585 | best_tx_size = mbmi->tx_size; |
| 2586 | best_filter = mbmi->intra_filter; |
| 2587 | best_tx_type = mbmi->tx_type; |
| 2588 | *rate = this_rate; |
| 2589 | *rate_tokenonly = this_rate_tokenonly; |
| 2590 | *distortion = this_distortion; |
| 2591 | *skippable = s; |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | if (best_i >= 0) { |
| 2597 | for (j = 0; j < level2; ++j) { |
| 2598 | mic->mbmi.angle_delta[0] = deltas_level2[best_i][j]; |
| 2599 | p_angle = |
| 2600 | mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
| 2601 | for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) { |
| 2602 | mic->mbmi.intra_filter = filter; |
| 2603 | if ((FILTER_FAST_SEARCH || |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2604 | !av1_is_intra_filter_switchable(p_angle)) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2605 | filter != INTRA_FILTER_LINEAR) |
| 2606 | continue; |
| 2607 | pick_intra_angle_routine_sby( |
| 2608 | cpi, x, rate, rate_tokenonly, distortion, skippable, |
| 2609 | &best_angle_delta, &best_tx_size, &best_tx_type, &best_filter, |
| 2610 | bsize, |
| 2611 | rate_overhead + cpi->intra_filter_cost[intra_filter_ctx][filter], |
| 2612 | &best_rd); |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | } else { |
| 2617 | for (angle_delta = -MAX_ANGLE_DELTAS; angle_delta <= MAX_ANGLE_DELTAS; |
| 2618 | ++angle_delta) { |
| 2619 | mbmi->angle_delta[0] = angle_delta; |
| 2620 | p_angle = |
| 2621 | mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
| 2622 | for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) { |
| 2623 | mic->mbmi.intra_filter = filter; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2624 | if ((FILTER_FAST_SEARCH || !av1_is_intra_filter_switchable(p_angle)) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2625 | filter != INTRA_FILTER_LINEAR) |
| 2626 | continue; |
| 2627 | pick_intra_angle_routine_sby( |
| 2628 | cpi, x, rate, rate_tokenonly, distortion, skippable, |
| 2629 | &best_angle_delta, &best_tx_size, &best_tx_type, &best_filter, |
| 2630 | bsize, |
| 2631 | rate_overhead + cpi->intra_filter_cost[intra_filter_ctx][filter], |
| 2632 | &best_rd); |
| 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | if (FILTER_FAST_SEARCH && *rate_tokenonly < INT_MAX) { |
| 2638 | mbmi->angle_delta[0] = best_angle_delta; |
| 2639 | p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2640 | if (av1_is_intra_filter_switchable(p_angle)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2641 | for (filter = INTRA_FILTER_LINEAR + 1; filter < INTRA_FILTERS; ++filter) { |
| 2642 | mic->mbmi.intra_filter = filter; |
| 2643 | pick_intra_angle_routine_sby( |
| 2644 | cpi, x, rate, rate_tokenonly, distortion, skippable, |
| 2645 | &best_angle_delta, &best_tx_size, &best_tx_type, &best_filter, |
| 2646 | bsize, |
| 2647 | rate_overhead + cpi->intra_filter_cost[intra_filter_ctx][filter], |
| 2648 | &best_rd); |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | mbmi->tx_size = best_tx_size; |
| 2654 | mbmi->angle_delta[0] = best_angle_delta; |
| 2655 | mic->mbmi.intra_filter = best_filter; |
| 2656 | mbmi->tx_type = best_tx_type; |
| 2657 | return best_rd; |
| 2658 | } |
| 2659 | |
| 2660 | // Indices are sign, integer, and fractional part of the gradient value |
| 2661 | static const uint8_t gradient_to_angle_bin[2][7][16] = { |
| 2662 | { |
| 2663 | { 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0 }, |
| 2664 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, |
| 2665 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 2666 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 2667 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 2668 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 2669 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 2670 | }, |
| 2671 | { |
| 2672 | { 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4 }, |
| 2673 | { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3 }, |
| 2674 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 2675 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 2676 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 2677 | { 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 2678 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 2679 | }, |
| 2680 | }; |
| 2681 | |
| 2682 | static const uint8_t mode_to_angle_bin[INTRA_MODES] = { |
| 2683 | 0, 2, 6, 0, 4, 3, 5, 7, 1, 0, |
| 2684 | }; |
| 2685 | |
| 2686 | static void angle_estimation(const uint8_t *src, int src_stride, int rows, |
| 2687 | int cols, uint8_t *directional_mode_skip_mask) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 2688 | int i, r, c, index, dx, dy, temp, sn, remd, quot; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2689 | uint64_t hist[DIRECTIONAL_MODES]; |
| 2690 | uint64_t hist_sum = 0; |
| 2691 | |
| 2692 | memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0])); |
| 2693 | src += src_stride; |
| 2694 | for (r = 1; r < rows; ++r) { |
| 2695 | for (c = 1; c < cols; ++c) { |
| 2696 | dx = src[c] - src[c - 1]; |
| 2697 | dy = src[c] - src[c - src_stride]; |
| 2698 | temp = dx * dx + dy * dy; |
| 2699 | if (dy == 0) { |
| 2700 | index = 2; |
| 2701 | } else { |
| 2702 | sn = (dx > 0) ^ (dy > 0); |
| 2703 | dx = abs(dx); |
| 2704 | dy = abs(dy); |
| 2705 | remd = dx % dy; |
| 2706 | quot = dx / dy; |
| 2707 | remd = remd * 16 / dy; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2708 | index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2709 | } |
| 2710 | hist[index] += temp; |
| 2711 | } |
| 2712 | src += src_stride; |
| 2713 | } |
| 2714 | |
| 2715 | for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i]; |
| 2716 | for (i = 0; i < INTRA_MODES; ++i) { |
| 2717 | if (i != DC_PRED && i != TM_PRED) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 2718 | const uint8_t angle_bin = mode_to_angle_bin[i]; |
| 2719 | uint64_t score = 2 * hist[angle_bin]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2720 | int weight = 2; |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 2721 | if (angle_bin > 0) { |
| 2722 | score += hist[angle_bin - 1]; |
| 2723 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2724 | } |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 2725 | if (angle_bin < DIRECTIONAL_MODES - 1) { |
| 2726 | score += hist[angle_bin + 1]; |
| 2727 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2728 | } |
| 2729 | if (score * ANGLE_SKIP_THRESH < hist_sum * weight) |
| 2730 | directional_mode_skip_mask[i] = 1; |
| 2731 | } |
| 2732 | } |
| 2733 | } |
| 2734 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2735 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2736 | static void highbd_angle_estimation(const uint8_t *src8, int src_stride, |
| 2737 | int rows, int cols, |
| 2738 | uint8_t *directional_mode_skip_mask) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 2739 | int i, r, c, index, dx, dy, temp, sn, remd, quot; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2740 | uint64_t hist[DIRECTIONAL_MODES]; |
| 2741 | uint64_t hist_sum = 0; |
| 2742 | uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
| 2743 | |
| 2744 | memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0])); |
| 2745 | src += src_stride; |
| 2746 | for (r = 1; r < rows; ++r) { |
| 2747 | for (c = 1; c < cols; ++c) { |
| 2748 | dx = src[c] - src[c - 1]; |
| 2749 | dy = src[c] - src[c - src_stride]; |
| 2750 | temp = dx * dx + dy * dy; |
| 2751 | if (dy == 0) { |
| 2752 | index = 2; |
| 2753 | } else { |
| 2754 | sn = (dx > 0) ^ (dy > 0); |
| 2755 | dx = abs(dx); |
| 2756 | dy = abs(dy); |
| 2757 | remd = dx % dy; |
| 2758 | quot = dx / dy; |
| 2759 | remd = remd * 16 / dy; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2760 | index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2761 | } |
| 2762 | hist[index] += temp; |
| 2763 | } |
| 2764 | src += src_stride; |
| 2765 | } |
| 2766 | |
| 2767 | for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i]; |
| 2768 | for (i = 0; i < INTRA_MODES; ++i) { |
| 2769 | if (i != DC_PRED && i != TM_PRED) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 2770 | const uint8_t angle_bin = mode_to_angle_bin[i]; |
| 2771 | uint64_t score = 2 * hist[angle_bin]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2772 | int weight = 2; |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 2773 | if (angle_bin > 0) { |
| 2774 | score += hist[angle_bin - 1]; |
| 2775 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2776 | } |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 2777 | if (angle_bin < DIRECTIONAL_MODES - 1) { |
| 2778 | score += hist[angle_bin + 1]; |
| 2779 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2780 | } |
| 2781 | if (score * ANGLE_SKIP_THRESH < hist_sum * weight) |
| 2782 | directional_mode_skip_mask[i] = 1; |
| 2783 | } |
| 2784 | } |
| 2785 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2786 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2787 | #endif // CONFIG_EXT_INTRA |
| 2788 | |
| 2789 | // This function is used only for intra_only frames |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2790 | static int64_t rd_pick_intra_sby_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2791 | int *rate, int *rate_tokenonly, |
| 2792 | int64_t *distortion, int *skippable, |
| 2793 | BLOCK_SIZE bsize, int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2794 | uint8_t mode_idx; |
| 2795 | PREDICTION_MODE mode_selected = DC_PRED; |
| 2796 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2797 | MODE_INFO *const mic = xd->mi[0]; |
| 2798 | int this_rate, this_rate_tokenonly, s; |
| 2799 | int64_t this_distortion, this_rd; |
| 2800 | TX_SIZE best_tx = TX_4X4; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2801 | #if CONFIG_EXT_INTRA || CONFIG_PALETTE |
| 2802 | const int rows = 4 * num_4x4_blocks_high_lookup[bsize]; |
| 2803 | const int cols = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 2804 | #endif // CONFIG_EXT_INTRA || CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2805 | #if CONFIG_EXT_INTRA |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2806 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2807 | int is_directional_mode, rate_overhead, best_angle_delta = 0; |
| 2808 | INTRA_FILTER best_filter = INTRA_FILTER_LINEAR; |
| 2809 | uint8_t directional_mode_skip_mask[INTRA_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2810 | const int src_stride = x->plane[0].src.stride; |
| 2811 | const uint8_t *src = x->plane[0].src.buf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2812 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2813 | #if CONFIG_FILTER_INTRA |
| 2814 | int beat_best_rd = 0; |
| 2815 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
| 2816 | uint16_t filter_intra_mode_skip_mask = (1 << FILTER_INTRA_MODES) - 1; |
| 2817 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2818 | TX_TYPE best_tx_type = DCT_DCT; |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2819 | const int *bmode_costs; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2820 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2821 | PALETTE_MODE_INFO palette_mode_info; |
| 2822 | PALETTE_MODE_INFO *const pmi = &mic->mbmi.palette_mode_info; |
| 2823 | uint8_t *best_palette_color_map = |
| 2824 | cpi->common.allow_screen_content_tools |
| 2825 | ? x->palette_buffer->best_palette_color_map |
| 2826 | : NULL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2827 | int palette_ctx = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2828 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2829 | const MODE_INFO *above_mi = xd->above_mi; |
| 2830 | const MODE_INFO *left_mi = xd->left_mi; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2831 | const PREDICTION_MODE A = av1_above_block_mode(mic, above_mi, 0); |
| 2832 | const PREDICTION_MODE L = av1_left_block_mode(mic, left_mi, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2833 | const PREDICTION_MODE FINAL_MODE_SEARCH = TM_PRED + 1; |
| 2834 | const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2835 | #if CONFIG_PVQ |
| 2836 | od_rollback_buffer pre_buf, post_buf; |
| 2837 | |
| 2838 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 2839 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 2840 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2841 | bmode_costs = cpi->y_mode_costs[A][L]; |
| 2842 | |
| 2843 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2844 | mic->mbmi.angle_delta[0] = 0; |
| 2845 | memset(directional_mode_skip_mask, 0, |
| 2846 | sizeof(directional_mode_skip_mask[0]) * INTRA_MODES); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2847 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2848 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 2849 | highbd_angle_estimation(src, src_stride, rows, cols, |
| 2850 | directional_mode_skip_mask); |
| 2851 | else |
| 2852 | #endif |
| 2853 | angle_estimation(src, src_stride, rows, cols, directional_mode_skip_mask); |
| 2854 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2855 | #if CONFIG_FILTER_INTRA |
| 2856 | filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 2857 | mic->mbmi.filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 2858 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2859 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2860 | palette_mode_info.palette_size[0] = 0; |
| 2861 | pmi->palette_size[0] = 0; |
| 2862 | if (above_mi) |
| 2863 | palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 2864 | if (left_mi) |
| 2865 | palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2866 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2867 | |
| 2868 | if (cpi->sf.tx_type_search.fast_intra_tx_type_search) |
| 2869 | x->use_default_intra_tx_type = 1; |
| 2870 | else |
| 2871 | x->use_default_intra_tx_type = 0; |
| 2872 | |
| 2873 | /* Y Search for intra prediction mode */ |
| 2874 | for (mode_idx = DC_PRED; mode_idx <= FINAL_MODE_SEARCH; ++mode_idx) { |
| 2875 | if (mode_idx == FINAL_MODE_SEARCH) { |
| 2876 | if (x->use_default_intra_tx_type == 0) break; |
| 2877 | mic->mbmi.mode = mode_selected; |
| 2878 | x->use_default_intra_tx_type = 0; |
| 2879 | } else { |
| 2880 | mic->mbmi.mode = mode_idx; |
| 2881 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2882 | #if CONFIG_PVQ |
| 2883 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 2884 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2885 | #if CONFIG_EXT_INTRA |
| 2886 | is_directional_mode = |
| 2887 | (mic->mbmi.mode != DC_PRED && mic->mbmi.mode != TM_PRED); |
| 2888 | if (is_directional_mode && directional_mode_skip_mask[mic->mbmi.mode]) |
| 2889 | continue; |
| 2890 | if (is_directional_mode) { |
| 2891 | rate_overhead = bmode_costs[mic->mbmi.mode] + |
| 2892 | write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1, 0); |
| 2893 | this_rate_tokenonly = INT_MAX; |
| 2894 | this_rd = rd_pick_intra_angle_sby(cpi, x, &this_rate, |
| 2895 | &this_rate_tokenonly, &this_distortion, |
| 2896 | &s, bsize, rate_overhead, best_rd); |
| 2897 | } else { |
| 2898 | mic->mbmi.angle_delta[0] = 0; |
| 2899 | super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL, |
| 2900 | bsize, best_rd); |
| 2901 | } |
| 2902 | #else |
| 2903 | super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL, |
| 2904 | bsize, best_rd); |
| 2905 | #endif // CONFIG_EXT_INTRA |
| 2906 | |
| 2907 | if (this_rate_tokenonly == INT_MAX) continue; |
| 2908 | |
| 2909 | this_rate = this_rate_tokenonly + bmode_costs[mic->mbmi.mode]; |
| 2910 | |
| 2911 | if (!xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
| 2912 | // super_block_yrd above includes the cost of the tx_size in the |
| 2913 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 2914 | // (prediction granularity), so we account for it in the full rate, |
| 2915 | // not the tokenonly rate. |
| 2916 | this_rate_tokenonly -= |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 2917 | cpi->tx_size_cost[max_tx_size - TX_8X8][get_tx_size_context(xd)] |
Jingning Han | b0a7130 | 2016-10-25 16:28:49 -0700 | [diff] [blame] | 2918 | [tx_size_to_depth(mic->mbmi.tx_size)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2919 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2920 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2921 | if (cpi->common.allow_screen_content_tools && mic->mbmi.mode == DC_PRED) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2922 | this_rate += av1_cost_bit( |
| 2923 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2924 | #endif // CONFIG_PALETTE |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2925 | #if CONFIG_FILTER_INTRA |
| 2926 | if (mic->mbmi.mode == DC_PRED) |
| 2927 | this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 0); |
| 2928 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2929 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2930 | if (is_directional_mode) { |
| 2931 | int p_angle; |
| 2932 | this_rate += |
| 2933 | write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1, |
| 2934 | MAX_ANGLE_DELTAS + mic->mbmi.angle_delta[0]); |
| 2935 | p_angle = mode_to_angle_map[mic->mbmi.mode] + |
| 2936 | mic->mbmi.angle_delta[0] * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2937 | if (av1_is_intra_filter_switchable(p_angle)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2938 | this_rate += |
| 2939 | cpi->intra_filter_cost[intra_filter_ctx][mic->mbmi.intra_filter]; |
| 2940 | } |
| 2941 | #endif // CONFIG_EXT_INTRA |
| 2942 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2943 | #if CONFIG_FILTER_INTRA |
Debargha Mukherjee | 1ae9f2c | 2016-10-04 14:30:16 -0700 | [diff] [blame] | 2944 | if (best_rd == INT64_MAX || this_rd - best_rd < (best_rd >> 4)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2945 | filter_intra_mode_skip_mask ^= (1 << mic->mbmi.mode); |
| 2946 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2947 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2948 | |
| 2949 | if (this_rd < best_rd) { |
| 2950 | mode_selected = mic->mbmi.mode; |
| 2951 | best_rd = this_rd; |
| 2952 | best_tx = mic->mbmi.tx_size; |
| 2953 | #if CONFIG_EXT_INTRA |
| 2954 | best_angle_delta = mic->mbmi.angle_delta[0]; |
| 2955 | best_filter = mic->mbmi.intra_filter; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2956 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2957 | #if CONFIG_FILTER_INTRA |
| 2958 | beat_best_rd = 1; |
| 2959 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2960 | best_tx_type = mic->mbmi.tx_type; |
| 2961 | *rate = this_rate; |
| 2962 | *rate_tokenonly = this_rate_tokenonly; |
| 2963 | *distortion = this_distortion; |
| 2964 | *skippable = s; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2965 | #if CONFIG_PVQ |
| 2966 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 2967 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2971 | #if CONFIG_PVQ |
| 2972 | od_encode_rollback(&x->daala_enc, &post_buf); |
| 2973 | #endif |
| 2974 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2975 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2976 | if (cpi->common.allow_screen_content_tools) |
| 2977 | rd_pick_palette_intra_sby(cpi, x, bsize, palette_ctx, bmode_costs[DC_PRED], |
| 2978 | &palette_mode_info, best_palette_color_map, |
| 2979 | &best_tx, &best_tx_type, &mode_selected, |
| 2980 | &best_rd); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2981 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2982 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2983 | #if CONFIG_FILTER_INTRA |
| 2984 | if (beat_best_rd) { |
| 2985 | if (rd_pick_filter_intra_sby(cpi, x, rate, rate_tokenonly, distortion, |
| 2986 | skippable, bsize, bmode_costs[DC_PRED], |
| 2987 | &best_rd, filter_intra_mode_skip_mask)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2988 | mode_selected = mic->mbmi.mode; |
| 2989 | best_tx = mic->mbmi.tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2990 | filter_intra_mode_info = mic->mbmi.filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2991 | best_tx_type = mic->mbmi.tx_type; |
| 2992 | } |
| 2993 | } |
| 2994 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2995 | mic->mbmi.filter_intra_mode_info.use_filter_intra_mode[0] = |
| 2996 | filter_intra_mode_info.use_filter_intra_mode[0]; |
| 2997 | if (filter_intra_mode_info.use_filter_intra_mode[0]) { |
| 2998 | mic->mbmi.filter_intra_mode_info.filter_intra_mode[0] = |
| 2999 | filter_intra_mode_info.filter_intra_mode[0]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 3000 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3001 | palette_mode_info.palette_size[0] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 3002 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3003 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3004 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3005 | |
| 3006 | mic->mbmi.mode = mode_selected; |
| 3007 | mic->mbmi.tx_size = best_tx; |
| 3008 | #if CONFIG_EXT_INTRA |
| 3009 | mic->mbmi.angle_delta[0] = best_angle_delta; |
| 3010 | mic->mbmi.intra_filter = best_filter; |
| 3011 | #endif // CONFIG_EXT_INTRA |
| 3012 | mic->mbmi.tx_type = best_tx_type; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 3013 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3014 | pmi->palette_size[0] = palette_mode_info.palette_size[0]; |
| 3015 | if (palette_mode_info.palette_size[0] > 0) { |
| 3016 | memcpy(pmi->palette_colors, palette_mode_info.palette_colors, |
| 3017 | PALETTE_MAX_SIZE * sizeof(palette_mode_info.palette_colors[0])); |
| 3018 | memcpy(xd->plane[0].color_index_map, best_palette_color_map, |
| 3019 | rows * cols * sizeof(best_palette_color_map[0])); |
| 3020 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 3021 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3022 | |
| 3023 | return best_rd; |
| 3024 | } |
| 3025 | |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3026 | // Return value 0: early termination triggered, no valid rd cost available; |
| 3027 | // 1: rd cost values are valid. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3028 | static int super_block_uvrd(const AV1_COMP *const cpi, MACROBLOCK *x, int *rate, |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3029 | int64_t *distortion, int *skippable, int64_t *sse, |
| 3030 | BLOCK_SIZE bsize, int64_t ref_best_rd) { |
| 3031 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3032 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 3033 | const TX_SIZE uv_tx_size = get_uv_tx_size(mbmi, &xd->plane[1]); |
| 3034 | int plane; |
| 3035 | int pnrate = 0, pnskip = 1; |
| 3036 | int64_t pndist = 0, pnsse = 0; |
| 3037 | int is_cost_valid = 1; |
| 3038 | |
| 3039 | if (ref_best_rd < 0) is_cost_valid = 0; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3040 | #if !CONFIG_PVQ |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3041 | if (is_inter_block(mbmi) && is_cost_valid) { |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3042 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) |
| 3043 | av1_subtract_plane(x, bsize, plane); |
| 3044 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3045 | #endif |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3046 | *rate = 0; |
| 3047 | *distortion = 0; |
| 3048 | *sse = 0; |
| 3049 | *skippable = 1; |
| 3050 | |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 3051 | if (is_cost_valid) { |
| 3052 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) { |
| 3053 | txfm_rd_in_plane(x, cpi, &pnrate, &pndist, &pnskip, &pnsse, ref_best_rd, |
| 3054 | plane, bsize, uv_tx_size, cpi->sf.use_fast_coef_costing); |
| 3055 | if (pnrate == INT_MAX) { |
| 3056 | is_cost_valid = 0; |
| 3057 | break; |
| 3058 | } |
| 3059 | *rate += pnrate; |
| 3060 | *distortion += pndist; |
| 3061 | *sse += pnsse; |
| 3062 | *skippable &= pnskip; |
| 3063 | if (RDCOST(x->rdmult, x->rddiv, *rate, *distortion) > ref_best_rd && |
| 3064 | RDCOST(x->rdmult, x->rddiv, 0, *sse) > ref_best_rd) { |
| 3065 | is_cost_valid = 0; |
| 3066 | break; |
| 3067 | } |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | if (!is_cost_valid) { |
| 3072 | // reset cost value |
| 3073 | *rate = INT_MAX; |
| 3074 | *distortion = INT64_MAX; |
| 3075 | *sse = INT64_MAX; |
| 3076 | *skippable = 0; |
| 3077 | } |
| 3078 | |
| 3079 | return is_cost_valid; |
| 3080 | } |
| 3081 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3082 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3083 | void av1_tx_block_rd_b(const AV1_COMP *cpi, MACROBLOCK *x, TX_SIZE tx_size, |
| 3084 | int blk_row, int blk_col, int plane, int block, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3085 | int plane_bsize, int coeff_ctx, RD_STATS *rd_stats) { |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 3086 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3087 | MACROBLOCKD *xd = &x->e_mbd; |
| 3088 | const struct macroblock_plane *const p = &x->plane[plane]; |
| 3089 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 3090 | int64_t tmp; |
| 3091 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 3092 | PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV; |
| 3093 | TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size); |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 3094 | const SCAN_ORDER *const scan_order = |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 3095 | get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3096 | BLOCK_SIZE txm_bsize = txsize_to_bsize[tx_size]; |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3097 | int bh = block_size_high[txm_bsize]; |
| 3098 | int bw = block_size_wide[txm_bsize]; |
| 3099 | int txb_h = tx_size_high_unit[tx_size]; |
| 3100 | int txb_w = tx_size_wide_unit[tx_size]; |
| 3101 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3102 | int src_stride = p->src.stride; |
| 3103 | uint8_t *src = &p->src.buf[4 * blk_row * src_stride + 4 * blk_col]; |
| 3104 | uint8_t *dst = &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3105 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3106 | DECLARE_ALIGNED(16, uint16_t, rec_buffer16[MAX_TX_SQUARE]); |
| 3107 | uint8_t *rec_buffer; |
| 3108 | #else |
| 3109 | DECLARE_ALIGNED(16, uint8_t, rec_buffer[MAX_TX_SQUARE]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3110 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3111 | int max_blocks_high = block_size_high[plane_bsize]; |
| 3112 | int max_blocks_wide = block_size_wide[plane_bsize]; |
| 3113 | const int diff_stride = max_blocks_wide; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3114 | const int16_t *diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)]; |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 3115 | int txb_coeff_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3116 | #if CONFIG_EXT_TX |
| 3117 | assert(tx_size < TX_SIZES); |
| 3118 | #endif // CONFIG_EXT_TX |
| 3119 | |
| 3120 | if (xd->mb_to_bottom_edge < 0) |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3121 | max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3122 | if (xd->mb_to_right_edge < 0) |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3123 | max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x); |
| 3124 | |
| 3125 | max_blocks_high >>= tx_size_wide_log2[0]; |
| 3126 | max_blocks_wide >>= tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3127 | |
| 3128 | #if CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 3129 | av1_xform_quant_fp_nuq(cm, x, plane, block, blk_row, blk_col, plane_bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3130 | tx_size, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3131 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 3132 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3133 | AV1_XFORM_QUANT_FP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3134 | #endif // CONFIG_NEW_QUANT |
| 3135 | |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 3136 | av1_optimize_b(cm, x, plane, block, tx_size, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3137 | |
| 3138 | // TODO(any): Use dist_block to compute distortion |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3139 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3140 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 3141 | rec_buffer = CONVERT_TO_BYTEPTR(rec_buffer16); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3142 | aom_highbd_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL, |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3143 | 0, NULL, 0, bw, bh, xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3144 | } else { |
| 3145 | rec_buffer = (uint8_t *)rec_buffer16; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3146 | aom_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL, 0, |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3147 | NULL, 0, bw, bh); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3148 | } |
| 3149 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3150 | aom_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL, 0, NULL, |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3151 | 0, bw, bh); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3152 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3153 | |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3154 | if (blk_row + txb_h > max_blocks_high || blk_col + txb_w > max_blocks_wide) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3155 | int idx, idy; |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3156 | int blocks_height = AOMMIN(txb_h, max_blocks_high - blk_row); |
| 3157 | int blocks_width = AOMMIN(txb_w, max_blocks_wide - blk_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3158 | tmp = 0; |
| 3159 | for (idy = 0; idy < blocks_height; idy += 2) { |
| 3160 | for (idx = 0; idx < blocks_width; idx += 2) { |
| 3161 | const int16_t *d = diff + 4 * idy * diff_stride + 4 * idx; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3162 | tmp += aom_sum_squares_2d_i16(d, diff_stride, 8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | } else { |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3166 | tmp = sum_squares_2d(diff, diff_stride, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3169 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3170 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 3171 | tmp = ROUND_POWER_OF_TWO(tmp, (xd->bd - 8) * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3172 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3173 | rd_stats->sse += tmp * 16; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3174 | |
| 3175 | if (p->eobs[block] > 0) { |
| 3176 | INV_TXFM_PARAM inv_txfm_param; |
| 3177 | inv_txfm_param.tx_type = tx_type; |
| 3178 | inv_txfm_param.tx_size = tx_size; |
| 3179 | inv_txfm_param.eob = p->eobs[block]; |
| 3180 | inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3181 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3182 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 3183 | inv_txfm_param.bd = xd->bd; |
| 3184 | highbd_inv_txfm_add(dqcoeff, rec_buffer, MAX_TX_SIZE, &inv_txfm_param); |
| 3185 | } else { |
| 3186 | inv_txfm_add(dqcoeff, rec_buffer, MAX_TX_SIZE, &inv_txfm_param); |
| 3187 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3188 | #else // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3189 | inv_txfm_add(dqcoeff, rec_buffer, MAX_TX_SIZE, &inv_txfm_param); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3190 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3191 | |
Jingning Han | 4b47c93 | 2016-11-03 09:20:08 -0700 | [diff] [blame] | 3192 | if (txb_w + blk_col > max_blocks_wide || |
| 3193 | txb_h + blk_row > max_blocks_high) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3194 | int idx, idy; |
| 3195 | unsigned int this_dist; |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3196 | int blocks_height = AOMMIN(txb_h, max_blocks_high - blk_row); |
| 3197 | int blocks_width = AOMMIN(txb_w, max_blocks_wide - blk_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3198 | tmp = 0; |
| 3199 | for (idy = 0; idy < blocks_height; idy += 2) { |
| 3200 | for (idx = 0; idx < blocks_width; idx += 2) { |
| 3201 | uint8_t *const s = src + 4 * idy * src_stride + 4 * idx; |
| 3202 | uint8_t *const r = rec_buffer + 4 * idy * MAX_TX_SIZE + 4 * idx; |
| 3203 | cpi->fn_ptr[BLOCK_8X8].vf(s, src_stride, r, MAX_TX_SIZE, &this_dist); |
| 3204 | tmp += this_dist; |
| 3205 | } |
| 3206 | } |
| 3207 | } else { |
| 3208 | uint32_t this_dist; |
| 3209 | cpi->fn_ptr[txm_bsize].vf(src, src_stride, rec_buffer, MAX_TX_SIZE, |
| 3210 | &this_dist); |
| 3211 | tmp = this_dist; |
| 3212 | } |
| 3213 | } |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3214 | rd_stats->dist += tmp * 16; |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 3215 | txb_coeff_cost = av1_cost_coeffs(cm, x, plane, block, coeff_ctx, tx_size, |
| 3216 | scan_order->scan, scan_order->neighbors, 0); |
| 3217 | rd_stats->rate += txb_coeff_cost; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3218 | rd_stats->skip &= (p->eobs[block] == 0); |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 3219 | #if CONFIG_RD_DEBUG |
| 3220 | rd_stats->txb_coeff_cost[plane] += txb_coeff_cost; |
| 3221 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3222 | } |
| 3223 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3224 | static void select_tx_block(const AV1_COMP *cpi, MACROBLOCK *x, int blk_row, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3225 | int blk_col, int plane, int block, TX_SIZE tx_size, |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 3226 | int depth, BLOCK_SIZE plane_bsize, |
| 3227 | ENTROPY_CONTEXT *ta, ENTROPY_CONTEXT *tl, |
| 3228 | TXFM_CONTEXT *tx_above, TXFM_CONTEXT *tx_left, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3229 | RD_STATS *rd_stats, int64_t ref_best_rd, |
| 3230 | int *is_cost_valid) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3231 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3232 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 3233 | struct macroblock_plane *const p = &x->plane[plane]; |
| 3234 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 3235 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 3236 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 3237 | TX_SIZE(*const inter_tx_size) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3238 | [MAX_MIB_SIZE] = |
| 3239 | (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col]; |
Jingning Han | f65b870 | 2016-10-31 12:13:20 -0700 | [diff] [blame] | 3240 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 3241 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 3242 | const int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3243 | int64_t this_rd = INT64_MAX; |
| 3244 | ENTROPY_CONTEXT *pta = ta + blk_col; |
| 3245 | ENTROPY_CONTEXT *ptl = tl + blk_row; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3246 | int coeff_ctx, i; |
Jingning Han | c8b8936 | 2016-11-01 10:28:53 -0700 | [diff] [blame] | 3247 | int ctx = |
| 3248 | txfm_partition_context(tx_above + (blk_col >> 1), |
| 3249 | tx_left + (blk_row >> 1), mbmi->sb_type, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3250 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3251 | int64_t sum_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3252 | int tmp_eob = 0; |
| 3253 | int zero_blk_rate; |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 3254 | RD_STATS sum_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3255 | av1_init_rd_stats(&sum_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3256 | |
| 3257 | #if CONFIG_EXT_TX |
| 3258 | assert(tx_size < TX_SIZES); |
| 3259 | #endif // CONFIG_EXT_TX |
| 3260 | |
| 3261 | if (ref_best_rd < 0) { |
| 3262 | *is_cost_valid = 0; |
| 3263 | return; |
| 3264 | } |
| 3265 | |
Jingning Han | ce059e8 | 2016-10-31 16:27:28 -0700 | [diff] [blame] | 3266 | coeff_ctx = get_entropy_context(tx_size, pta, ptl); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3267 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3268 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3269 | |
| 3270 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 3271 | |
| 3272 | zero_blk_rate = |
| 3273 | x->token_costs[tx_size][pd->plane_type][1][0][0][coeff_ctx][EOB_TOKEN]; |
| 3274 | |
| 3275 | if (cpi->common.tx_mode == TX_MODE_SELECT || tx_size == TX_4X4) { |
| 3276 | inter_tx_size[0][0] = tx_size; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3277 | av1_tx_block_rd_b(cpi, x, tx_size, blk_row, blk_col, plane, block, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3278 | plane_bsize, coeff_ctx, rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3279 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3280 | if ((RDCOST(x->rdmult, x->rddiv, rd_stats->rate, rd_stats->dist) >= |
| 3281 | RDCOST(x->rdmult, x->rddiv, zero_blk_rate, rd_stats->sse) || |
| 3282 | rd_stats->skip == 1) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3283 | !xd->lossless[mbmi->segment_id]) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3284 | rd_stats->rate = zero_blk_rate; |
| 3285 | rd_stats->dist = rd_stats->sse; |
| 3286 | rd_stats->skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3287 | x->blk_skip[plane][blk_row * bw + blk_col] = 1; |
| 3288 | p->eobs[block] = 0; |
| 3289 | } else { |
| 3290 | x->blk_skip[plane][blk_row * bw + blk_col] = 0; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3291 | rd_stats->skip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3292 | } |
| 3293 | |
Jingning Han | 571189c | 2016-10-24 10:38:43 -0700 | [diff] [blame] | 3294 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3295 | rd_stats->rate += |
| 3296 | av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 0); |
| 3297 | this_rd = RDCOST(x->rdmult, x->rddiv, rd_stats->rate, rd_stats->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3298 | tmp_eob = p->eobs[block]; |
| 3299 | } |
| 3300 | |
Jingning Han | 571189c | 2016-10-24 10:38:43 -0700 | [diff] [blame] | 3301 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3302 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 3303 | const int bsl = tx_size_wide_unit[sub_txs]; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 3304 | int sub_step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs]; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3305 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3306 | int this_cost_valid = 1; |
| 3307 | int64_t tmp_rd = 0; |
| 3308 | |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 3309 | sum_rd_stats.rate = |
| 3310 | av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3311 | #if CONFIG_EXT_TX |
| 3312 | assert(tx_size < TX_SIZES); |
| 3313 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3314 | for (i = 0; i < 4 && this_cost_valid; ++i) { |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 3315 | int offsetr = blk_row + (i >> 1) * bsl; |
| 3316 | int offsetc = blk_col + (i & 0x01) * bsl; |
| 3317 | |
| 3318 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 3319 | |
| 3320 | select_tx_block(cpi, x, offsetr, offsetc, plane, block, sub_txs, |
| 3321 | depth + 1, plane_bsize, ta, tl, tx_above, tx_left, |
| 3322 | &this_rd_stats, ref_best_rd - tmp_rd, &this_cost_valid); |
| 3323 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3324 | av1_merge_rd_stats(&sum_rd_stats, &this_rd_stats); |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 3325 | |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 3326 | tmp_rd = |
| 3327 | RDCOST(x->rdmult, x->rddiv, sum_rd_stats.rate, sum_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3328 | if (this_rd < tmp_rd) break; |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 3329 | block += sub_step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3330 | } |
| 3331 | if (this_cost_valid) sum_rd = tmp_rd; |
| 3332 | } |
| 3333 | |
| 3334 | if (this_rd < sum_rd) { |
| 3335 | int idx, idy; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 3336 | for (i = 0; i < tx_size_wide_unit[tx_size]; ++i) pta[i] = !(tmp_eob == 0); |
| 3337 | for (i = 0; i < tx_size_high_unit[tx_size]; ++i) ptl[i] = !(tmp_eob == 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3338 | txfm_partition_update(tx_above + (blk_col >> 1), tx_left + (blk_row >> 1), |
| 3339 | tx_size); |
| 3340 | inter_tx_size[0][0] = tx_size; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 3341 | for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy) |
| 3342 | for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3343 | inter_tx_size[idy][idx] = tx_size; |
| 3344 | mbmi->tx_size = tx_size; |
| 3345 | if (this_rd == INT64_MAX) *is_cost_valid = 0; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3346 | x->blk_skip[plane][blk_row * bw + blk_col] = rd_stats->skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3347 | } else { |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 3348 | *rd_stats = sum_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3349 | if (sum_rd == INT64_MAX) *is_cost_valid = 0; |
| 3350 | } |
| 3351 | } |
| 3352 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3353 | static void inter_block_yrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 3354 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
| 3355 | int64_t ref_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3356 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3357 | int is_cost_valid = 1; |
| 3358 | int64_t this_rd = 0; |
| 3359 | |
| 3360 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 3361 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3362 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3363 | |
| 3364 | if (is_cost_valid) { |
| 3365 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 3366 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
| 3367 | const int mi_width = num_4x4_blocks_wide_lookup[plane_bsize]; |
| 3368 | const int mi_height = num_4x4_blocks_high_lookup[plane_bsize]; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3369 | const TX_SIZE max_tx_size = max_txsize_lookup[plane_bsize]; |
| 3370 | const int bh = tx_size_high_unit[max_tx_size]; |
| 3371 | const int bw = tx_size_wide_unit[max_tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3372 | int idx, idy; |
| 3373 | int block = 0; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3374 | int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3375 | ENTROPY_CONTEXT ctxa[2 * MAX_MIB_SIZE]; |
| 3376 | ENTROPY_CONTEXT ctxl[2 * MAX_MIB_SIZE]; |
| 3377 | TXFM_CONTEXT tx_above[MAX_MIB_SIZE]; |
| 3378 | TXFM_CONTEXT tx_left[MAX_MIB_SIZE]; |
| 3379 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3380 | RD_STATS pn_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3381 | av1_init_rd_stats(&pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3382 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3383 | av1_get_entropy_contexts(bsize, TX_4X4, pd, ctxa, ctxl); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3384 | memcpy(tx_above, xd->above_txfm_context, |
| 3385 | sizeof(TXFM_CONTEXT) * (mi_width >> 1)); |
| 3386 | memcpy(tx_left, xd->left_txfm_context, |
| 3387 | sizeof(TXFM_CONTEXT) * (mi_height >> 1)); |
| 3388 | |
| 3389 | for (idy = 0; idy < mi_height; idy += bh) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3390 | for (idx = 0; idx < mi_width; idx += bw) { |
| 3391 | select_tx_block(cpi, x, idy, idx, 0, block, max_tx_size, |
| 3392 | mi_height != mi_width, plane_bsize, ctxa, ctxl, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3393 | tx_above, tx_left, &pn_rd_stats, ref_best_rd - this_rd, |
| 3394 | &is_cost_valid); |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3395 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3396 | this_rd += AOMMIN( |
| 3397 | RDCOST(x->rdmult, x->rddiv, pn_rd_stats.rate, pn_rd_stats.dist), |
| 3398 | RDCOST(x->rdmult, x->rddiv, 0, pn_rd_stats.sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3399 | block += step; |
| 3400 | } |
| 3401 | } |
| 3402 | } |
| 3403 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3404 | this_rd = AOMMIN(RDCOST(x->rdmult, x->rddiv, rd_stats->rate, rd_stats->dist), |
| 3405 | RDCOST(x->rdmult, x->rddiv, 0, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3406 | if (this_rd > ref_best_rd) is_cost_valid = 0; |
| 3407 | |
| 3408 | if (!is_cost_valid) { |
| 3409 | // reset cost value |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3410 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3411 | } |
| 3412 | } |
| 3413 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3414 | static int64_t select_tx_size_fix_type(const AV1_COMP *cpi, MACROBLOCK *x, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3415 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3416 | int64_t ref_best_rd, TX_TYPE tx_type) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3417 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3418 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3419 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3420 | const int is_inter = is_inter_block(mbmi); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3421 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
| 3422 | int s0 = av1_cost_bit(skip_prob, 0); |
| 3423 | int s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3424 | int64_t rd; |
| 3425 | |
| 3426 | mbmi->tx_type = tx_type; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3427 | inter_block_yrd(cpi, x, rd_stats, bsize, ref_best_rd); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3428 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
Yue Chen | 49587a7 | 2016-09-28 17:09:47 -0700 | [diff] [blame] | 3429 | if (is_rect_tx_allowed(xd, mbmi)) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3430 | RD_STATS rect_rd_stats; |
| 3431 | int64_t rd_rect_tx; |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3432 | int tx_size_cat = inter_tx_size_cat_lookup[bsize]; |
| 3433 | TX_SIZE tx_size = max_txsize_rect_lookup[bsize]; |
| 3434 | TX_SIZE var_tx_size = mbmi->tx_size; |
| 3435 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3436 | txfm_rd_in_plane(x, cpi, &rect_rd_stats.rate, &rect_rd_stats.dist, |
| 3437 | &rect_rd_stats.skip, &rect_rd_stats.sse, ref_best_rd, 0, |
| 3438 | bsize, tx_size, cpi->sf.use_fast_coef_costing); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3439 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3440 | if (rd_stats->rate != INT_MAX) { |
| 3441 | rd_stats->rate += av1_cost_bit(cm->fc->rect_tx_prob[tx_size_cat], 0); |
| 3442 | if (rd_stats->skip) { |
| 3443 | rd = RDCOST(x->rdmult, x->rddiv, s1, rd_stats->sse); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3444 | } else { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3445 | rd = RDCOST(x->rdmult, x->rddiv, rd_stats->rate + s0, rd_stats->dist); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3446 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3447 | !rd_stats->skip) |
| 3448 | rd = AOMMIN(rd, RDCOST(x->rdmult, x->rddiv, s1, rd_stats->sse)); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3449 | } |
| 3450 | } else { |
| 3451 | rd = INT64_MAX; |
| 3452 | } |
| 3453 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3454 | if (rect_rd_stats.rate != INT_MAX) { |
| 3455 | rect_rd_stats.rate += av1_cost_bit(cm->fc->rect_tx_prob[tx_size_cat], 1); |
| 3456 | if (rect_rd_stats.skip) { |
| 3457 | rd_rect_tx = RDCOST(x->rdmult, x->rddiv, s1, rect_rd_stats.sse); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3458 | } else { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3459 | rd_rect_tx = RDCOST(x->rdmult, x->rddiv, rect_rd_stats.rate + s0, |
| 3460 | rect_rd_stats.dist); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3461 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3462 | !(rect_rd_stats.skip)) |
| 3463 | rd_rect_tx = AOMMIN( |
| 3464 | rd_rect_tx, RDCOST(x->rdmult, x->rddiv, s1, rect_rd_stats.sse)); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3465 | } |
| 3466 | } else { |
| 3467 | rd_rect_tx = INT64_MAX; |
| 3468 | } |
| 3469 | |
| 3470 | if (rd_rect_tx < rd) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3471 | *rd_stats = rect_rd_stats; |
| 3472 | if (!xd->lossless[mbmi->segment_id]) x->blk_skip[0][0] = rd_stats->skip; |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3473 | mbmi->tx_size = tx_size; |
| 3474 | mbmi->inter_tx_size[0][0] = mbmi->tx_size; |
| 3475 | } else { |
| 3476 | mbmi->tx_size = var_tx_size; |
| 3477 | } |
| 3478 | } |
| 3479 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3480 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3481 | if (rd_stats->rate == INT_MAX) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3482 | |
| 3483 | #if CONFIG_EXT_TX |
Peter de Rivaz | c0b4d7a | 2016-09-08 10:55:58 +0100 | [diff] [blame] | 3484 | if (get_ext_tx_types(mbmi->tx_size, bsize, is_inter) > 1 && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3485 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Peter de Rivaz | c0b4d7a | 2016-09-08 10:55:58 +0100 | [diff] [blame] | 3486 | int ext_tx_set = get_ext_tx_set(mbmi->tx_size, bsize, is_inter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3487 | if (is_inter) { |
| 3488 | if (ext_tx_set > 0) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3489 | rd_stats->rate += |
Peter de Rivaz | b85a5a7 | 2016-10-18 11:47:56 +0100 | [diff] [blame] | 3490 | cpi->inter_tx_type_costs[ext_tx_set][txsize_sqr_map[mbmi->tx_size]] |
| 3491 | [mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3492 | } else { |
| 3493 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3494 | rd_stats->rate += cpi->intra_tx_type_costs[ext_tx_set][mbmi->tx_size] |
| 3495 | [mbmi->mode][mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3496 | } |
| 3497 | } |
| 3498 | #else // CONFIG_EXT_TX |
Peter de Rivaz | c0b4d7a | 2016-09-08 10:55:58 +0100 | [diff] [blame] | 3499 | if (mbmi->tx_size < TX_32X32 && !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3500 | if (is_inter) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3501 | rd_stats->rate += cpi->inter_tx_type_costs[mbmi->tx_size][mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3502 | else |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3503 | rd_stats->rate += |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 3504 | cpi->intra_tx_type_costs[mbmi->tx_size] |
| 3505 | [intra_mode_to_tx_type_context[mbmi->mode]] |
| 3506 | [mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3507 | } |
| 3508 | #endif // CONFIG_EXT_TX |
| 3509 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3510 | if (rd_stats->skip) |
| 3511 | rd = RDCOST(x->rdmult, x->rddiv, s1, rd_stats->sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3512 | else |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3513 | rd = RDCOST(x->rdmult, x->rddiv, rd_stats->rate + s0, rd_stats->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3514 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3515 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 3516 | !(rd_stats->skip)) |
| 3517 | rd = AOMMIN(rd, RDCOST(x->rdmult, x->rddiv, s1, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3518 | |
| 3519 | return rd; |
| 3520 | } |
| 3521 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3522 | static void select_tx_type_yrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 3523 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3524 | int64_t ref_best_rd) { |
| 3525 | const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; |
| 3526 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3527 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 3528 | int64_t rd = INT64_MAX; |
| 3529 | int64_t best_rd = INT64_MAX; |
| 3530 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
| 3531 | const int is_inter = is_inter_block(mbmi); |
| 3532 | TX_SIZE best_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE]; |
Debargha Mukherjee | 28d924b | 2016-10-05 00:48:28 -0700 | [diff] [blame] | 3533 | TX_SIZE best_tx = max_txsize_lookup[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3534 | uint8_t best_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 4]; |
| 3535 | const int n4 = 1 << (num_pels_log2_lookup[bsize] - 4); |
| 3536 | int idx, idy; |
| 3537 | int prune = 0; |
| 3538 | #if CONFIG_EXT_TX |
| 3539 | int ext_tx_set = get_ext_tx_set(max_tx_size, bsize, is_inter); |
| 3540 | #endif // CONFIG_EXT_TX |
| 3541 | |
| 3542 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 3543 | #if CONFIG_EXT_TX |
| 3544 | prune = prune_tx_types(cpi, bsize, x, xd, ext_tx_set); |
| 3545 | #else |
| 3546 | prune = prune_tx_types(cpi, bsize, x, xd, 0); |
| 3547 | #endif |
| 3548 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3549 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3550 | |
| 3551 | for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3552 | RD_STATS this_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3553 | av1_init_rd_stats(&this_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3554 | #if CONFIG_EXT_TX |
| 3555 | if (is_inter) { |
| 3556 | if (!ext_tx_used_inter[ext_tx_set][tx_type]) continue; |
| 3557 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 3558 | if (!do_tx_type_search(tx_type, prune)) continue; |
| 3559 | } |
| 3560 | } else { |
| 3561 | if (!ALLOW_INTRA_EXT_TX && bsize >= BLOCK_8X8) { |
| 3562 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 3563 | } |
| 3564 | if (!ext_tx_used_intra[ext_tx_set][tx_type]) continue; |
| 3565 | } |
| 3566 | #else // CONFIG_EXT_TX |
| 3567 | if (max_tx_size >= TX_32X32 && tx_type != DCT_DCT) continue; |
| 3568 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 3569 | !do_tx_type_search(tx_type, prune)) |
| 3570 | continue; |
| 3571 | #endif // CONFIG_EXT_TX |
| 3572 | if (is_inter && x->use_default_inter_tx_type && |
| 3573 | tx_type != get_default_tx_type(0, xd, 0, max_tx_size)) |
| 3574 | continue; |
| 3575 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3576 | rd = select_tx_size_fix_type(cpi, x, &this_rd_stats, bsize, ref_best_rd, |
| 3577 | tx_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3578 | |
| 3579 | if (rd < best_rd) { |
| 3580 | best_rd = rd; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3581 | *rd_stats = this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3582 | best_tx_type = mbmi->tx_type; |
| 3583 | best_tx = mbmi->tx_size; |
| 3584 | memcpy(best_blk_skip, x->blk_skip[0], sizeof(best_blk_skip[0]) * n4); |
| 3585 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 3586 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 3587 | best_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 3588 | } |
| 3589 | } |
| 3590 | |
| 3591 | mbmi->tx_type = best_tx_type; |
| 3592 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 3593 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 3594 | mbmi->inter_tx_size[idy][idx] = best_tx_size[idy][idx]; |
| 3595 | mbmi->tx_size = best_tx; |
Angie Chiang | 628d7c9 | 2016-11-03 16:24:56 -0700 | [diff] [blame] | 3596 | #if CONFIG_RD_DEBUG |
| 3597 | // record plane y's transform block coefficient cost |
| 3598 | mbmi->txb_coeff_cost[0] = rd_stats->txb_coeff_cost[0]; |
| 3599 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3600 | memcpy(x->blk_skip[0], best_blk_skip, sizeof(best_blk_skip[0]) * n4); |
| 3601 | } |
| 3602 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3603 | static void tx_block_rd(const AV1_COMP *cpi, MACROBLOCK *x, int blk_row, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3604 | int blk_col, int plane, int block, TX_SIZE tx_size, |
| 3605 | BLOCK_SIZE plane_bsize, ENTROPY_CONTEXT *above_ctx, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3606 | ENTROPY_CONTEXT *left_ctx, RD_STATS *rd_stats) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3607 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3608 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 3609 | struct macroblock_plane *const p = &x->plane[plane]; |
| 3610 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 3611 | BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
| 3612 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 3613 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
| 3614 | TX_SIZE plane_tx_size; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3615 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 3616 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3617 | |
| 3618 | #if CONFIG_EXT_TX |
| 3619 | assert(tx_size < TX_SIZES); |
| 3620 | #endif // CONFIG_EXT_TX |
| 3621 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3622 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 3623 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 3624 | plane_tx_size = |
| 3625 | plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0] |
| 3626 | : mbmi->inter_tx_size[tx_row][tx_col]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3627 | |
| 3628 | if (tx_size == plane_tx_size) { |
| 3629 | int coeff_ctx, i; |
| 3630 | ENTROPY_CONTEXT *ta = above_ctx + blk_col; |
| 3631 | ENTROPY_CONTEXT *tl = left_ctx + blk_row; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3632 | coeff_ctx = get_entropy_context(tx_size, ta, tl); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3633 | av1_tx_block_rd_b(cpi, x, tx_size, blk_row, blk_col, plane, block, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3634 | plane_bsize, coeff_ctx, rd_stats); |
Jingning Han | 607fa6a | 2016-10-26 10:46:28 -0700 | [diff] [blame] | 3635 | |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 3636 | for (i = 0; i < tx_size_wide_unit[tx_size]; ++i) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3637 | ta[i] = !(p->eobs[block] == 0); |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 3638 | for (i = 0; i < tx_size_high_unit[tx_size]; ++i) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3639 | tl[i] = !(p->eobs[block] == 0); |
| 3640 | } else { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3641 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 3642 | const int bsl = tx_size_wide_unit[sub_txs]; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 3643 | int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3644 | int i; |
| 3645 | |
| 3646 | assert(bsl > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3647 | |
| 3648 | for (i = 0; i < 4; ++i) { |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 3649 | int offsetr = blk_row + (i >> 1) * bsl; |
| 3650 | int offsetc = blk_col + (i & 0x01) * bsl; |
| 3651 | |
| 3652 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 3653 | |
| 3654 | tx_block_rd(cpi, x, offsetr, offsetc, plane, block, sub_txs, plane_bsize, |
| 3655 | above_ctx, left_ctx, rd_stats); |
| 3656 | block += step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3657 | } |
| 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | // Return value 0: early termination triggered, no valid rd cost available; |
| 3662 | // 1: rd cost values are valid. |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3663 | static int inter_block_uvrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 3664 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
| 3665 | int64_t ref_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3666 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3667 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 3668 | int plane; |
| 3669 | int is_cost_valid = 1; |
| 3670 | int64_t this_rd; |
| 3671 | |
| 3672 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 3673 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3674 | av1_init_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3675 | |
| 3676 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 3677 | if (is_rect_tx(mbmi->tx_size)) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3678 | return super_block_uvrd(cpi, x, &rd_stats->rate, &rd_stats->dist, |
| 3679 | &rd_stats->skip, &rd_stats->sse, bsize, |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3680 | ref_best_rd); |
| 3681 | } |
| 3682 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 3683 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3684 | if (is_inter_block(mbmi) && is_cost_valid) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3685 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3686 | av1_subtract_plane(x, bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3687 | } |
| 3688 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3689 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) { |
| 3690 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 3691 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
| 3692 | const int mi_width = num_4x4_blocks_wide_lookup[plane_bsize]; |
| 3693 | const int mi_height = num_4x4_blocks_high_lookup[plane_bsize]; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3694 | const TX_SIZE max_tx_size = max_txsize_lookup[plane_bsize]; |
| 3695 | const int bh = tx_size_high_unit[max_tx_size]; |
| 3696 | const int bw = tx_size_wide_unit[max_tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3697 | int idx, idy; |
| 3698 | int block = 0; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3699 | const int step = bh * bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3700 | ENTROPY_CONTEXT ta[2 * MAX_MIB_SIZE]; |
| 3701 | ENTROPY_CONTEXT tl[2 * MAX_MIB_SIZE]; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3702 | RD_STATS pn_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3703 | av1_init_rd_stats(&pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3704 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3705 | av1_get_entropy_contexts(bsize, TX_4X4, pd, ta, tl); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3706 | |
| 3707 | for (idy = 0; idy < mi_height; idy += bh) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 3708 | for (idx = 0; idx < mi_width; idx += bw) { |
| 3709 | tx_block_rd(cpi, x, idy, idx, plane, block, max_tx_size, plane_bsize, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3710 | ta, tl, &pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3711 | block += step; |
| 3712 | } |
| 3713 | } |
| 3714 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3715 | if (pn_rd_stats.rate == INT_MAX) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3716 | is_cost_valid = 0; |
| 3717 | break; |
| 3718 | } |
| 3719 | |
Angie Chiang | 628d7c9 | 2016-11-03 16:24:56 -0700 | [diff] [blame] | 3720 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3721 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3722 | this_rd = |
| 3723 | AOMMIN(RDCOST(x->rdmult, x->rddiv, rd_stats->rate, rd_stats->dist), |
| 3724 | RDCOST(x->rdmult, x->rddiv, 0, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3725 | |
| 3726 | if (this_rd > ref_best_rd) { |
| 3727 | is_cost_valid = 0; |
| 3728 | break; |
| 3729 | } |
| 3730 | } |
| 3731 | |
| 3732 | if (!is_cost_valid) { |
| 3733 | // reset cost value |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3734 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | return is_cost_valid; |
| 3738 | } |
| 3739 | #endif // CONFIG_VAR_TX |
| 3740 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 3741 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3742 | static void rd_pick_palette_intra_sbuv( |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3743 | const AV1_COMP *const cpi, MACROBLOCK *x, int dc_mode_cost, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3744 | PALETTE_MODE_INFO *palette_mode_info, uint8_t *best_palette_color_map, |
| 3745 | PREDICTION_MODE *mode_selected, int64_t *best_rd, int *rate, |
| 3746 | int *rate_tokenonly, int64_t *distortion, int *skippable) { |
| 3747 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3748 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 3749 | const BLOCK_SIZE bsize = mbmi->sb_type; |
| 3750 | const int rows = |
| 3751 | (4 * num_4x4_blocks_high_lookup[bsize]) >> (xd->plane[1].subsampling_y); |
| 3752 | const int cols = |
| 3753 | (4 * num_4x4_blocks_wide_lookup[bsize]) >> (xd->plane[1].subsampling_x); |
| 3754 | int this_rate, this_rate_tokenonly, s; |
| 3755 | int64_t this_distortion, this_rd; |
| 3756 | int colors_u, colors_v, colors; |
| 3757 | const int src_stride = x->plane[1].src.stride; |
| 3758 | const uint8_t *const src_u = x->plane[1].src.buf; |
| 3759 | const uint8_t *const src_v = x->plane[2].src.buf; |
| 3760 | |
| 3761 | if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return; |
| 3762 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3763 | #if CONFIG_FILTER_INTRA |
| 3764 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 3765 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3766 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3767 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3768 | if (cpi->common.use_highbitdepth) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3769 | colors_u = av1_count_colors_highbd(src_u, src_stride, rows, cols, |
| 3770 | cpi->common.bit_depth); |
| 3771 | colors_v = av1_count_colors_highbd(src_v, src_stride, rows, cols, |
| 3772 | cpi->common.bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3773 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3774 | #endif // CONFIG_AOM_HIGHBITDEPTH |
| 3775 | colors_u = av1_count_colors(src_u, src_stride, rows, cols); |
| 3776 | colors_v = av1_count_colors(src_v, src_stride, rows, cols); |
| 3777 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3778 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3779 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3780 | |
| 3781 | colors = colors_u > colors_v ? colors_u : colors_v; |
| 3782 | if (colors > 1 && colors <= 64) { |
| 3783 | int r, c, n, i, j; |
| 3784 | const int max_itr = 50; |
Urvang Joshi | 967ff39 | 2016-09-07 14:57:49 -0700 | [diff] [blame] | 3785 | uint8_t color_order[PALETTE_MAX_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3786 | int64_t this_sse; |
| 3787 | float lb_u, ub_u, val_u; |
| 3788 | float lb_v, ub_v, val_v; |
| 3789 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 3790 | float centroids[2 * PALETTE_MAX_SIZE]; |
| 3791 | uint8_t *const color_map = xd->plane[1].color_index_map; |
| 3792 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 3793 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3794 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3795 | uint16_t *src_u16 = CONVERT_TO_SHORTPTR(src_u); |
| 3796 | uint16_t *src_v16 = CONVERT_TO_SHORTPTR(src_v); |
| 3797 | if (cpi->common.use_highbitdepth) { |
| 3798 | lb_u = src_u16[0]; |
| 3799 | ub_u = src_u16[0]; |
| 3800 | lb_v = src_v16[0]; |
| 3801 | ub_v = src_v16[0]; |
| 3802 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3803 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3804 | lb_u = src_u[0]; |
| 3805 | ub_u = src_u[0]; |
| 3806 | lb_v = src_v[0]; |
| 3807 | ub_v = src_v[0]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3808 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3809 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3810 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3811 | |
| 3812 | mbmi->uv_mode = DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3813 | #if CONFIG_FILTER_INTRA |
| 3814 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 3815 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3816 | for (r = 0; r < rows; ++r) { |
| 3817 | for (c = 0; c < cols; ++c) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3818 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3819 | if (cpi->common.use_highbitdepth) { |
| 3820 | val_u = src_u16[r * src_stride + c]; |
| 3821 | val_v = src_v16[r * src_stride + c]; |
| 3822 | data[(r * cols + c) * 2] = val_u; |
| 3823 | data[(r * cols + c) * 2 + 1] = val_v; |
| 3824 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3825 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3826 | val_u = src_u[r * src_stride + c]; |
| 3827 | val_v = src_v[r * src_stride + c]; |
| 3828 | data[(r * cols + c) * 2] = val_u; |
| 3829 | data[(r * cols + c) * 2 + 1] = val_v; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3830 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3831 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3832 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3833 | if (val_u < lb_u) |
| 3834 | lb_u = val_u; |
| 3835 | else if (val_u > ub_u) |
| 3836 | ub_u = val_u; |
| 3837 | if (val_v < lb_v) |
| 3838 | lb_v = val_v; |
| 3839 | else if (val_v > ub_v) |
| 3840 | ub_v = val_v; |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2; |
| 3845 | --n) { |
| 3846 | for (i = 0; i < n; ++i) { |
| 3847 | centroids[i * 2] = lb_u + (2 * i + 1) * (ub_u - lb_u) / n / 2; |
| 3848 | centroids[i * 2 + 1] = lb_v + (2 * i + 1) * (ub_v - lb_v) / n / 2; |
| 3849 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3850 | av1_k_means(data, centroids, color_map, rows * cols, n, 2, max_itr); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3851 | pmi->palette_size[1] = n; |
| 3852 | for (i = 1; i < 3; ++i) { |
| 3853 | for (j = 0; j < n; ++j) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3854 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3855 | if (cpi->common.use_highbitdepth) |
| 3856 | pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = clip_pixel_highbd( |
| 3857 | (int)centroids[j * 2 + i - 1], cpi->common.bit_depth); |
| 3858 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3859 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3860 | pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = |
| 3861 | clip_pixel((int)centroids[j * 2 + i - 1]); |
| 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, |
| 3866 | &this_sse, bsize, *best_rd); |
| 3867 | if (this_rate_tokenonly == INT_MAX) continue; |
| 3868 | this_rate = |
| 3869 | this_rate_tokenonly + dc_mode_cost + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3870 | 2 * cpi->common.bit_depth * n * av1_cost_bit(128, 0) + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3871 | cpi->palette_uv_size_cost[bsize - BLOCK_8X8][n - 2] + |
| 3872 | write_uniform_cost(n, color_map[0]) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3873 | av1_cost_bit( |
| 3874 | av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3875 | |
| 3876 | for (i = 0; i < rows; ++i) { |
| 3877 | for (j = (i == 0 ? 1 : 0); j < cols; ++j) { |
Urvang Joshi | 967ff39 | 2016-09-07 14:57:49 -0700 | [diff] [blame] | 3878 | int color_idx; |
| 3879 | const int color_ctx = av1_get_palette_color_context( |
| 3880 | color_map, cols, i, j, n, color_order, &color_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3881 | assert(color_idx >= 0 && color_idx < n); |
| 3882 | this_rate += cpi->palette_uv_color_cost[n - 2][color_ctx][color_idx]; |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
| 3887 | if (this_rd < *best_rd) { |
| 3888 | *best_rd = this_rd; |
| 3889 | *palette_mode_info = *pmi; |
| 3890 | memcpy(best_palette_color_map, color_map, |
| 3891 | rows * cols * sizeof(best_palette_color_map[0])); |
| 3892 | *mode_selected = DC_PRED; |
| 3893 | *rate = this_rate; |
| 3894 | *distortion = this_distortion; |
| 3895 | *rate_tokenonly = this_rate_tokenonly; |
| 3896 | *skippable = s; |
| 3897 | } |
| 3898 | } |
| 3899 | } |
| 3900 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 3901 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3902 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3903 | #if CONFIG_FILTER_INTRA |
| 3904 | // Return 1 if an filter intra mode is selected; return 0 otherwise. |
| 3905 | static int rd_pick_filter_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 3906 | int *rate, int *rate_tokenonly, |
| 3907 | int64_t *distortion, int *skippable, |
| 3908 | BLOCK_SIZE bsize, int64_t *best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3909 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3910 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3911 | int filter_intra_selected_flag = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3912 | int this_rate_tokenonly, this_rate, s; |
| 3913 | int64_t this_distortion, this_sse, this_rd; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3914 | FILTER_INTRA_MODE mode; |
| 3915 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3916 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3917 | av1_zero(filter_intra_mode_info); |
| 3918 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3919 | mbmi->uv_mode = DC_PRED; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 3920 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3921 | mbmi->palette_mode_info.palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 3922 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3923 | |
| 3924 | for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3925 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3926 | if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, |
| 3927 | &this_sse, bsize, *best_rd)) |
| 3928 | continue; |
| 3929 | |
| 3930 | this_rate = this_rate_tokenonly + |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3931 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 1) + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3932 | cpi->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode] + |
| 3933 | write_uniform_cost(FILTER_INTRA_MODES, mode); |
| 3934 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
| 3935 | if (this_rd < *best_rd) { |
| 3936 | *best_rd = this_rd; |
| 3937 | *rate = this_rate; |
| 3938 | *rate_tokenonly = this_rate_tokenonly; |
| 3939 | *distortion = this_distortion; |
| 3940 | *skippable = s; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3941 | filter_intra_mode_info = mbmi->filter_intra_mode_info; |
| 3942 | filter_intra_selected_flag = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3943 | } |
| 3944 | } |
| 3945 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3946 | if (filter_intra_selected_flag) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3947 | mbmi->uv_mode = DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3948 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 3949 | filter_intra_mode_info.use_filter_intra_mode[1]; |
| 3950 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 3951 | filter_intra_mode_info.filter_intra_mode[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3952 | return 1; |
| 3953 | } else { |
| 3954 | return 0; |
| 3955 | } |
| 3956 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3957 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3958 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3959 | #if CONFIG_EXT_INTRA |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3960 | static void pick_intra_angle_routine_sbuv( |
| 3961 | const AV1_COMP *const cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, |
| 3962 | int64_t *distortion, int *skippable, int *best_angle_delta, |
| 3963 | BLOCK_SIZE bsize, int rate_overhead, int64_t *best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3964 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
| 3965 | int this_rate_tokenonly, this_rate, s; |
| 3966 | int64_t this_distortion, this_sse, this_rd; |
| 3967 | |
| 3968 | if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, |
| 3969 | &this_sse, bsize, *best_rd)) |
| 3970 | return; |
| 3971 | |
| 3972 | this_rate = this_rate_tokenonly + rate_overhead; |
| 3973 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
| 3974 | if (this_rd < *best_rd) { |
| 3975 | *best_rd = this_rd; |
| 3976 | *best_angle_delta = mbmi->angle_delta[1]; |
| 3977 | *rate = this_rate; |
| 3978 | *rate_tokenonly = this_rate_tokenonly; |
| 3979 | *distortion = this_distortion; |
| 3980 | *skippable = s; |
| 3981 | } |
| 3982 | } |
| 3983 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3984 | static int rd_pick_intra_angle_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 3985 | int *rate, int *rate_tokenonly, |
| 3986 | int64_t *distortion, int *skippable, |
| 3987 | BLOCK_SIZE bsize, int rate_overhead, |
| 3988 | int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3989 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3990 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 3991 | int this_rate_tokenonly, this_rate, s; |
| 3992 | int64_t this_distortion, this_sse, this_rd; |
| 3993 | int angle_delta, best_angle_delta = 0; |
| 3994 | const double rd_adjust = 1.2; |
| 3995 | |
| 3996 | *rate_tokenonly = INT_MAX; |
| 3997 | if (ANGLE_FAST_SEARCH) { |
| 3998 | int deltas_level1[3] = { 0, -2, 2 }; |
| 3999 | int deltas_level2[3][2] = { |
| 4000 | { -1, 1 }, { -3, -1 }, { 1, 3 }, |
| 4001 | }; |
| 4002 | const int level1 = 3, level2 = 2; |
| 4003 | int i, j, best_i = -1; |
| 4004 | |
| 4005 | for (i = 0; i < level1; ++i) { |
| 4006 | int64_t tmp_best_rd; |
| 4007 | mbmi->angle_delta[1] = deltas_level1[i]; |
| 4008 | tmp_best_rd = (i == 0 && best_rd < INT64_MAX) |
| 4009 | ? (int64_t)(best_rd * rd_adjust) |
| 4010 | : best_rd; |
| 4011 | if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, |
| 4012 | &this_sse, bsize, tmp_best_rd)) { |
| 4013 | if (i == 0) |
| 4014 | break; |
| 4015 | else |
| 4016 | continue; |
| 4017 | } |
| 4018 | this_rate = this_rate_tokenonly + rate_overhead; |
| 4019 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
| 4020 | if (i == 0 && best_rd < INT64_MAX && this_rd > best_rd * rd_adjust) break; |
| 4021 | if (this_rd < best_rd) { |
| 4022 | best_i = i; |
| 4023 | best_rd = this_rd; |
| 4024 | best_angle_delta = mbmi->angle_delta[1]; |
| 4025 | *rate = this_rate; |
| 4026 | *rate_tokenonly = this_rate_tokenonly; |
| 4027 | *distortion = this_distortion; |
| 4028 | *skippable = s; |
| 4029 | } |
| 4030 | } |
| 4031 | |
| 4032 | if (best_i >= 0) { |
| 4033 | for (j = 0; j < level2; ++j) { |
| 4034 | mbmi->angle_delta[1] = deltas_level2[best_i][j]; |
| 4035 | pick_intra_angle_routine_sbuv(cpi, x, rate, rate_tokenonly, distortion, |
| 4036 | skippable, &best_angle_delta, bsize, |
| 4037 | rate_overhead, &best_rd); |
| 4038 | } |
| 4039 | } |
| 4040 | } else { |
| 4041 | for (angle_delta = -MAX_ANGLE_DELTAS; angle_delta <= MAX_ANGLE_DELTAS; |
| 4042 | ++angle_delta) { |
| 4043 | mbmi->angle_delta[1] = angle_delta; |
| 4044 | pick_intra_angle_routine_sbuv(cpi, x, rate, rate_tokenonly, distortion, |
| 4045 | skippable, &best_angle_delta, bsize, |
| 4046 | rate_overhead, &best_rd); |
| 4047 | } |
| 4048 | } |
| 4049 | |
| 4050 | mbmi->angle_delta[1] = best_angle_delta; |
| 4051 | return *rate_tokenonly != INT_MAX; |
| 4052 | } |
| 4053 | #endif // CONFIG_EXT_INTRA |
| 4054 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4055 | static int64_t rd_pick_intra_sbuv_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 4056 | int *rate, int *rate_tokenonly, |
| 4057 | int64_t *distortion, int *skippable, |
| 4058 | BLOCK_SIZE bsize, TX_SIZE max_tx_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4059 | MACROBLOCKD *xd = &x->e_mbd; |
| 4060 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 4061 | PREDICTION_MODE mode; |
| 4062 | PREDICTION_MODE mode_selected = DC_PRED; |
| 4063 | int64_t best_rd = INT64_MAX, this_rd; |
| 4064 | int this_rate_tokenonly, this_rate, s; |
| 4065 | int64_t this_distortion, this_sse; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4066 | #if CONFIG_PVQ |
| 4067 | od_rollback_buffer buf; |
| 4068 | |
| 4069 | od_encode_checkpoint(&x->daala_enc, &buf); |
| 4070 | #endif |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4071 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4072 | const int rows = |
| 4073 | (4 * num_4x4_blocks_high_lookup[bsize]) >> (xd->plane[1].subsampling_y); |
| 4074 | const int cols = |
| 4075 | (4 * num_4x4_blocks_wide_lookup[bsize]) >> (xd->plane[1].subsampling_x); |
| 4076 | PALETTE_MODE_INFO palette_mode_info; |
| 4077 | PALETTE_MODE_INFO *const pmi = &xd->mi[0]->mbmi.palette_mode_info; |
| 4078 | uint8_t *best_palette_color_map = NULL; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4079 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4080 | #if CONFIG_EXT_INTRA |
| 4081 | int is_directional_mode, rate_overhead, best_angle_delta = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4082 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4083 | #if CONFIG_FILTER_INTRA |
| 4084 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
| 4085 | |
| 4086 | filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 4087 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 4088 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4089 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4090 | palette_mode_info.palette_size[1] = 0; |
| 4091 | pmi->palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4092 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4093 | for (mode = DC_PRED; mode <= TM_PRED; ++mode) { |
| 4094 | if (!(cpi->sf.intra_uv_mode_mask[max_tx_size] & (1 << mode))) continue; |
| 4095 | |
| 4096 | mbmi->uv_mode = mode; |
| 4097 | #if CONFIG_EXT_INTRA |
| 4098 | is_directional_mode = (mode != DC_PRED && mode != TM_PRED); |
| 4099 | rate_overhead = cpi->intra_uv_mode_cost[mbmi->mode][mode] + |
| 4100 | write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1, 0); |
| 4101 | mbmi->angle_delta[1] = 0; |
| 4102 | if (mbmi->sb_type >= BLOCK_8X8 && is_directional_mode) { |
| 4103 | if (!rd_pick_intra_angle_sbuv(cpi, x, &this_rate, &this_rate_tokenonly, |
| 4104 | &this_distortion, &s, bsize, rate_overhead, |
| 4105 | best_rd)) |
| 4106 | continue; |
| 4107 | } else { |
| 4108 | if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4109 | &this_sse, bsize, best_rd)) { |
| 4110 | #if CONFIG_PVQ |
| 4111 | od_encode_rollback(&x->daala_enc, &buf); |
| 4112 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4113 | continue; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4114 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4115 | } |
| 4116 | this_rate = this_rate_tokenonly + cpi->intra_uv_mode_cost[mbmi->mode][mode]; |
| 4117 | if (mbmi->sb_type >= BLOCK_8X8 && is_directional_mode) |
| 4118 | this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1, |
| 4119 | MAX_ANGLE_DELTAS + mbmi->angle_delta[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4120 | #else |
| 4121 | if (!super_block_uvrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4122 | &this_sse, bsize, best_rd)) { |
| 4123 | #if CONFIG_PVQ |
| 4124 | od_encode_rollback(&x->daala_enc, &buf); |
| 4125 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4126 | continue; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4127 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4128 | this_rate = this_rate_tokenonly + cpi->intra_uv_mode_cost[mbmi->mode][mode]; |
| 4129 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4130 | #if CONFIG_FILTER_INTRA |
| 4131 | if (mbmi->sb_type >= BLOCK_8X8 && mode == DC_PRED) |
| 4132 | this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 0); |
| 4133 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4134 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4135 | if (cpi->common.allow_screen_content_tools && mbmi->sb_type >= BLOCK_8X8 && |
| 4136 | mode == DC_PRED) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4137 | this_rate += av1_cost_bit( |
| 4138 | av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 0); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4139 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4140 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4141 | #if CONFIG_PVQ |
| 4142 | // For chroma channels, multiply lambda by 0.5 when doing intra prediction |
| 4143 | // NOTE: Chroma intra prediction itself has a separate RDO, |
| 4144 | // though final chroma intra mode's D and R is simply added to |
| 4145 | // those of luma then global RDO is performed to decide the modes of SB. |
| 4146 | // Also, for chroma, the RDO cannot decide tx_size (follow luma's decision) |
| 4147 | // or tx_type (DCT only), then only the intra prediction is |
| 4148 | // chroma's own mode decision based on separate RDO. |
| 4149 | // TODO(yushin) : Seek for more reasonable solution than this. |
| 4150 | this_rd = RDCOST(x->rdmult >> (1 * PVQ_CHROMA_RD), x->rddiv, this_rate, |
| 4151 | this_distortion); |
| 4152 | od_encode_rollback(&x->daala_enc, &buf); |
| 4153 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4154 | this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4155 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4156 | |
| 4157 | if (this_rd < best_rd) { |
| 4158 | mode_selected = mode; |
| 4159 | #if CONFIG_EXT_INTRA |
| 4160 | best_angle_delta = mbmi->angle_delta[1]; |
| 4161 | #endif // CONFIG_EXT_INTRA |
| 4162 | best_rd = this_rd; |
| 4163 | *rate = this_rate; |
| 4164 | *rate_tokenonly = this_rate_tokenonly; |
| 4165 | *distortion = this_distortion; |
| 4166 | *skippable = s; |
| 4167 | } |
| 4168 | } |
| 4169 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4170 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4171 | if (cpi->common.allow_screen_content_tools && mbmi->sb_type >= BLOCK_8X8) { |
| 4172 | best_palette_color_map = x->palette_buffer->best_palette_color_map; |
| 4173 | rd_pick_palette_intra_sbuv( |
| 4174 | cpi, x, cpi->intra_uv_mode_cost[mbmi->mode][DC_PRED], |
| 4175 | &palette_mode_info, best_palette_color_map, &mode_selected, &best_rd, |
| 4176 | rate, rate_tokenonly, distortion, skippable); |
| 4177 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4178 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4179 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4180 | #if CONFIG_FILTER_INTRA |
| 4181 | if (mbmi->sb_type >= BLOCK_8X8) { |
| 4182 | if (rd_pick_filter_intra_sbuv(cpi, x, rate, rate_tokenonly, distortion, |
| 4183 | skippable, bsize, &best_rd)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4184 | mode_selected = mbmi->uv_mode; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4185 | filter_intra_mode_info = mbmi->filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4186 | } |
| 4187 | } |
| 4188 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4189 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 4190 | filter_intra_mode_info.use_filter_intra_mode[1]; |
| 4191 | if (filter_intra_mode_info.use_filter_intra_mode[1]) { |
| 4192 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 4193 | filter_intra_mode_info.filter_intra_mode[1]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4194 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4195 | palette_mode_info.palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4196 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4197 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4198 | #endif // CONFIG_FILTER_INTRA |
| 4199 | |
| 4200 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4201 | mbmi->angle_delta[1] = best_angle_delta; |
| 4202 | #endif // CONFIG_EXT_INTRA |
| 4203 | mbmi->uv_mode = mode_selected; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4204 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4205 | pmi->palette_size[1] = palette_mode_info.palette_size[1]; |
| 4206 | if (palette_mode_info.palette_size[1] > 0) { |
| 4207 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 4208 | palette_mode_info.palette_colors + PALETTE_MAX_SIZE, |
| 4209 | 2 * PALETTE_MAX_SIZE * sizeof(palette_mode_info.palette_colors[0])); |
| 4210 | memcpy(xd->plane[1].color_index_map, best_palette_color_map, |
| 4211 | rows * cols * sizeof(best_palette_color_map[0])); |
| 4212 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4213 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4214 | |
| 4215 | return best_rd; |
| 4216 | } |
| 4217 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4218 | static void choose_intra_uv_mode(const AV1_COMP *const cpi, MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4219 | PICK_MODE_CONTEXT *ctx, BLOCK_SIZE bsize, |
| 4220 | TX_SIZE max_tx_size, int *rate_uv, |
| 4221 | int *rate_uv_tokenonly, int64_t *dist_uv, |
| 4222 | int *skip_uv, PREDICTION_MODE *mode_uv) { |
| 4223 | // Use an estimated rd for uv_intra based on DC_PRED if the |
| 4224 | // appropriate speed flag is set. |
Jingning Han | 3f16725 | 2016-06-07 16:11:42 -0700 | [diff] [blame] | 4225 | (void)ctx; |
| 4226 | rd_pick_intra_sbuv_mode(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv, skip_uv, |
| 4227 | bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize, max_tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4228 | *mode_uv = x->e_mbd.mi[0]->mbmi.uv_mode; |
| 4229 | } |
| 4230 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4231 | static int cost_mv_ref(const AV1_COMP *const cpi, PREDICTION_MODE mode, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4232 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
| 4233 | int is_compound, |
| 4234 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 4235 | int16_t mode_context) { |
| 4236 | #if CONFIG_REF_MV |
| 4237 | int mode_cost = 0; |
| 4238 | #if CONFIG_EXT_INTER |
| 4239 | int16_t mode_ctx = |
| 4240 | is_compound ? mode_context : (mode_context & NEWMV_CTX_MASK); |
| 4241 | #else |
| 4242 | int16_t mode_ctx = mode_context & NEWMV_CTX_MASK; |
| 4243 | #endif // CONFIG_EXT_INTER |
| 4244 | int16_t is_all_zero_mv = mode_context & (1 << ALL_ZERO_FLAG_OFFSET); |
| 4245 | |
| 4246 | assert(is_inter_mode(mode)); |
| 4247 | |
| 4248 | #if CONFIG_EXT_INTER |
| 4249 | if (is_compound) { |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 4250 | return cpi->inter_compound_mode_cost[mode_context] |
| 4251 | [INTER_COMPOUND_OFFSET(mode)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4252 | } else { |
| 4253 | if (mode == NEWMV || mode == NEWFROMNEARMV) { |
| 4254 | #else |
| 4255 | if (mode == NEWMV) { |
| 4256 | #endif // CONFIG_EXT_INTER |
| 4257 | mode_cost = cpi->newmv_mode_cost[mode_ctx][0]; |
| 4258 | #if CONFIG_EXT_INTER |
| 4259 | if (!is_compound) |
| 4260 | mode_cost += cpi->new2mv_mode_cost[mode == NEWFROMNEARMV]; |
| 4261 | #endif // CONFIG_EXT_INTER |
| 4262 | return mode_cost; |
| 4263 | } else { |
| 4264 | mode_cost = cpi->newmv_mode_cost[mode_ctx][1]; |
| 4265 | mode_ctx = (mode_context >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK; |
| 4266 | |
| 4267 | if (is_all_zero_mv) return mode_cost; |
| 4268 | |
| 4269 | if (mode == ZEROMV) { |
| 4270 | mode_cost += cpi->zeromv_mode_cost[mode_ctx][0]; |
| 4271 | return mode_cost; |
| 4272 | } else { |
| 4273 | mode_cost += cpi->zeromv_mode_cost[mode_ctx][1]; |
| 4274 | mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK; |
| 4275 | |
| 4276 | if (mode_context & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6; |
| 4277 | if (mode_context & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7; |
| 4278 | if (mode_context & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8; |
| 4279 | |
| 4280 | mode_cost += cpi->refmv_mode_cost[mode_ctx][mode != NEARESTMV]; |
| 4281 | return mode_cost; |
| 4282 | } |
| 4283 | } |
| 4284 | #if CONFIG_EXT_INTER |
| 4285 | } |
| 4286 | #endif // CONFIG_EXT_INTER |
| 4287 | #else |
| 4288 | assert(is_inter_mode(mode)); |
| 4289 | #if CONFIG_EXT_INTER |
| 4290 | if (is_inter_compound_mode(mode)) { |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 4291 | return cpi->inter_compound_mode_cost[mode_context] |
| 4292 | [INTER_COMPOUND_OFFSET(mode)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4293 | } else { |
| 4294 | #endif // CONFIG_EXT_INTER |
| 4295 | return cpi->inter_mode_cost[mode_context][INTER_OFFSET(mode)]; |
| 4296 | #if CONFIG_EXT_INTER |
| 4297 | } |
| 4298 | #endif // CONFIG_EXT_INTER |
| 4299 | #endif |
| 4300 | } |
| 4301 | |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4302 | #if CONFIG_GLOBAL_MOTION |
| 4303 | static int get_gmbitcost(const Global_Motion_Params *gm, |
| 4304 | const aom_prob *probs) { |
| 4305 | int gmtype_cost[GLOBAL_MOTION_TYPES]; |
| 4306 | int bits; |
| 4307 | av1_cost_tokens(gmtype_cost, probs, av1_global_motion_types_tree); |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 4308 | if (gm->motion_params.wmmat[5] || gm->motion_params.wmmat[4]) { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4309 | bits = (GM_ABS_TRANS_BITS + 1) * 2 + 4 * GM_ABS_ALPHA_BITS + 4; |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 4310 | } else if (gm->motion_params.wmmat[3] || gm->motion_params.wmmat[2]) { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4311 | bits = (GM_ABS_TRANS_BITS + 1) * 2 + 2 * GM_ABS_ALPHA_BITS + 2; |
| 4312 | } else { |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 4313 | bits = ((gm->motion_params.wmmat[1] || gm->motion_params.wmmat[0]) |
| 4314 | ? ((GM_ABS_TRANS_BITS + 1) * 2) |
| 4315 | : 0); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4316 | } |
Sarah Parker | ae51dd8 | 2016-10-18 16:18:23 -0700 | [diff] [blame] | 4317 | return bits ? (bits << AV1_PROB_COST_SHIFT) + gmtype_cost[gm->gmtype] : 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | #define GLOBAL_MOTION_RATE(ref) \ |
| 4321 | (cpi->global_motion_used[ref] >= 2 \ |
| 4322 | ? 0 \ |
| 4323 | : get_gmbitcost(&cm->global_motion[(ref)], \ |
| 4324 | cm->fc->global_motion_types_prob) / \ |
| 4325 | 2); |
| 4326 | #endif // CONFIG_GLOBAL_MOTION |
| 4327 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4328 | static int set_and_cost_bmi_mvs(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 4329 | MACROBLOCKD *xd, int i, PREDICTION_MODE mode, |
| 4330 | int_mv this_mv[2], |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 4331 | int_mv frame_mv[MB_MODE_COUNT] |
| 4332 | [TOTAL_REFS_PER_FRAME], |
| 4333 | int_mv seg_mvs[TOTAL_REFS_PER_FRAME], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4334 | #if CONFIG_EXT_INTER |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 4335 | int_mv compound_seg_newmvs[2], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4336 | #endif // CONFIG_EXT_INTER |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 4337 | int_mv *best_ref_mv[2], const int *mvjcost, |
| 4338 | int *mvcost[2]) { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4339 | #if CONFIG_GLOBAL_MOTION |
| 4340 | const AV1_COMMON *cm = &cpi->common; |
| 4341 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4342 | MODE_INFO *const mic = xd->mi[0]; |
| 4343 | const MB_MODE_INFO *const mbmi = &mic->mbmi; |
| 4344 | const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 4345 | int thismvcost = 0; |
| 4346 | int idx, idy; |
| 4347 | const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[mbmi->sb_type]; |
| 4348 | const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[mbmi->sb_type]; |
| 4349 | const int is_compound = has_second_ref(mbmi); |
| 4350 | int mode_ctx = mbmi_ext->mode_context[mbmi->ref_frame[0]]; |
| 4351 | |
| 4352 | switch (mode) { |
| 4353 | case NEWMV: |
| 4354 | #if CONFIG_EXT_INTER |
| 4355 | case NEWFROMNEARMV: |
| 4356 | #endif // CONFIG_EXT_INTER |
| 4357 | this_mv[0].as_int = seg_mvs[mbmi->ref_frame[0]].as_int; |
| 4358 | #if CONFIG_EXT_INTER |
Alex Converse | 6317c88 | 2016-09-29 14:21:37 -0700 | [diff] [blame] | 4359 | if (!cpi->common.allow_high_precision_mv) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4360 | lower_mv_precision(&this_mv[0].as_mv, 0); |
| 4361 | #endif // CONFIG_EXT_INTER |
| 4362 | |
| 4363 | #if CONFIG_REF_MV |
| 4364 | for (idx = 0; idx < 1 + is_compound; ++idx) { |
| 4365 | this_mv[idx] = seg_mvs[mbmi->ref_frame[idx]]; |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 4366 | av1_set_mvcost(x, mbmi->ref_frame[idx], idx, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4367 | thismvcost += |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4368 | av1_mv_bit_cost(&this_mv[idx].as_mv, &best_ref_mv[idx]->as_mv, |
| 4369 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT_SUB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4370 | } |
| 4371 | (void)mvjcost; |
| 4372 | (void)mvcost; |
| 4373 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4374 | thismvcost += av1_mv_bit_cost(&this_mv[0].as_mv, &best_ref_mv[0]->as_mv, |
| 4375 | mvjcost, mvcost, MV_COST_WEIGHT_SUB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4376 | #if !CONFIG_EXT_INTER |
| 4377 | if (is_compound) { |
| 4378 | this_mv[1].as_int = seg_mvs[mbmi->ref_frame[1]].as_int; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4379 | thismvcost += av1_mv_bit_cost(&this_mv[1].as_mv, &best_ref_mv[1]->as_mv, |
| 4380 | mvjcost, mvcost, MV_COST_WEIGHT_SUB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4381 | } |
| 4382 | #endif // !CONFIG_EXT_INTER |
| 4383 | #endif |
| 4384 | break; |
| 4385 | case NEARMV: |
| 4386 | case NEARESTMV: |
| 4387 | this_mv[0].as_int = frame_mv[mode][mbmi->ref_frame[0]].as_int; |
| 4388 | if (is_compound) |
| 4389 | this_mv[1].as_int = frame_mv[mode][mbmi->ref_frame[1]].as_int; |
| 4390 | break; |
| 4391 | case ZEROMV: |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4392 | #if CONFIG_GLOBAL_MOTION |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 4393 | this_mv[0].as_int = |
| 4394 | gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[0]]) |
| 4395 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4396 | thismvcost += GLOBAL_MOTION_RATE(mbmi->ref_frame[0]); |
| 4397 | if (is_compound) { |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 4398 | this_mv[1].as_int = |
| 4399 | gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[1]]) |
| 4400 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4401 | thismvcost += GLOBAL_MOTION_RATE(mbmi->ref_frame[1]); |
| 4402 | } |
| 4403 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4404 | this_mv[0].as_int = 0; |
| 4405 | if (is_compound) this_mv[1].as_int = 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4406 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4407 | break; |
| 4408 | #if CONFIG_EXT_INTER |
| 4409 | case NEW_NEWMV: |
| 4410 | if (compound_seg_newmvs[0].as_int == INVALID_MV || |
| 4411 | compound_seg_newmvs[1].as_int == INVALID_MV) { |
| 4412 | this_mv[0].as_int = seg_mvs[mbmi->ref_frame[0]].as_int; |
| 4413 | this_mv[1].as_int = seg_mvs[mbmi->ref_frame[1]].as_int; |
| 4414 | } else { |
| 4415 | this_mv[0].as_int = compound_seg_newmvs[0].as_int; |
| 4416 | this_mv[1].as_int = compound_seg_newmvs[1].as_int; |
| 4417 | } |
Alex Converse | 6317c88 | 2016-09-29 14:21:37 -0700 | [diff] [blame] | 4418 | if (!cpi->common.allow_high_precision_mv) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4419 | lower_mv_precision(&this_mv[0].as_mv, 0); |
Alex Converse | 6317c88 | 2016-09-29 14:21:37 -0700 | [diff] [blame] | 4420 | if (!cpi->common.allow_high_precision_mv) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4421 | lower_mv_precision(&this_mv[1].as_mv, 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4422 | thismvcost += av1_mv_bit_cost(&this_mv[0].as_mv, &best_ref_mv[0]->as_mv, |
| 4423 | mvjcost, mvcost, MV_COST_WEIGHT_SUB); |
| 4424 | thismvcost += av1_mv_bit_cost(&this_mv[1].as_mv, &best_ref_mv[1]->as_mv, |
| 4425 | mvjcost, mvcost, MV_COST_WEIGHT_SUB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4426 | break; |
| 4427 | case NEW_NEARMV: |
| 4428 | case NEW_NEARESTMV: |
| 4429 | this_mv[0].as_int = seg_mvs[mbmi->ref_frame[0]].as_int; |
Alex Converse | 6317c88 | 2016-09-29 14:21:37 -0700 | [diff] [blame] | 4430 | if (!cpi->common.allow_high_precision_mv) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4431 | lower_mv_precision(&this_mv[0].as_mv, 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4432 | thismvcost += av1_mv_bit_cost(&this_mv[0].as_mv, &best_ref_mv[0]->as_mv, |
| 4433 | mvjcost, mvcost, MV_COST_WEIGHT_SUB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4434 | this_mv[1].as_int = frame_mv[mode][mbmi->ref_frame[1]].as_int; |
| 4435 | break; |
| 4436 | case NEAR_NEWMV: |
| 4437 | case NEAREST_NEWMV: |
| 4438 | this_mv[0].as_int = frame_mv[mode][mbmi->ref_frame[0]].as_int; |
| 4439 | this_mv[1].as_int = seg_mvs[mbmi->ref_frame[1]].as_int; |
Alex Converse | 6317c88 | 2016-09-29 14:21:37 -0700 | [diff] [blame] | 4440 | if (!cpi->common.allow_high_precision_mv) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4441 | lower_mv_precision(&this_mv[1].as_mv, 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4442 | thismvcost += av1_mv_bit_cost(&this_mv[1].as_mv, &best_ref_mv[1]->as_mv, |
| 4443 | mvjcost, mvcost, MV_COST_WEIGHT_SUB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4444 | break; |
| 4445 | case NEAREST_NEARMV: |
| 4446 | case NEAR_NEARESTMV: |
| 4447 | case NEAREST_NEARESTMV: |
| 4448 | case NEAR_NEARMV: |
| 4449 | this_mv[0].as_int = frame_mv[mode][mbmi->ref_frame[0]].as_int; |
| 4450 | this_mv[1].as_int = frame_mv[mode][mbmi->ref_frame[1]].as_int; |
| 4451 | break; |
| 4452 | case ZERO_ZEROMV: |
| 4453 | this_mv[0].as_int = 0; |
| 4454 | this_mv[1].as_int = 0; |
| 4455 | break; |
| 4456 | #endif // CONFIG_EXT_INTER |
| 4457 | default: break; |
| 4458 | } |
| 4459 | |
| 4460 | mic->bmi[i].as_mv[0].as_int = this_mv[0].as_int; |
| 4461 | if (is_compound) mic->bmi[i].as_mv[1].as_int = this_mv[1].as_int; |
| 4462 | |
| 4463 | mic->bmi[i].as_mode = mode; |
| 4464 | |
| 4465 | #if CONFIG_REF_MV |
| 4466 | if (mode == NEWMV) { |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 4467 | mic->bmi[i].pred_mv[0].as_int = |
| 4468 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_int; |
| 4469 | if (is_compound) |
| 4470 | mic->bmi[i].pred_mv[1].as_int = |
| 4471 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4472 | } else { |
Yaowu Xu | f5bbbfa | 2016-09-26 09:13:38 -0700 | [diff] [blame] | 4473 | mic->bmi[i].pred_mv[0].as_int = this_mv[0].as_int; |
| 4474 | if (is_compound) mic->bmi[i].pred_mv[1].as_int = this_mv[1].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4475 | } |
| 4476 | #endif |
| 4477 | |
| 4478 | for (idy = 0; idy < num_4x4_blocks_high; ++idy) |
| 4479 | for (idx = 0; idx < num_4x4_blocks_wide; ++idx) |
| 4480 | memmove(&mic->bmi[i + idy * 2 + idx], &mic->bmi[i], sizeof(mic->bmi[i])); |
| 4481 | |
| 4482 | #if CONFIG_REF_MV |
| 4483 | #if CONFIG_EXT_INTER |
| 4484 | if (is_compound) |
| 4485 | mode_ctx = mbmi_ext->compound_mode_context[mbmi->ref_frame[0]]; |
| 4486 | else |
| 4487 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4488 | mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context, |
| 4489 | mbmi->ref_frame, mbmi->sb_type, i); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4490 | #endif |
| 4491 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
| 4492 | return cost_mv_ref(cpi, mode, is_compound, mode_ctx) + thismvcost; |
| 4493 | #else |
| 4494 | return cost_mv_ref(cpi, mode, mode_ctx) + thismvcost; |
| 4495 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 4496 | } |
| 4497 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4498 | static int64_t encode_inter_mb_segment(const AV1_COMP *const cpi, MACROBLOCK *x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4499 | int64_t best_yrd, int i, int *labelyrate, |
| 4500 | int64_t *distortion, int64_t *sse, |
| 4501 | ENTROPY_CONTEXT *ta, ENTROPY_CONTEXT *tl, |
| 4502 | int ir, int ic, int mi_row, int mi_col) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4503 | #if !CONFIG_PVQ |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 4504 | const AV1_COMMON *const cm = &cpi->common; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4505 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4506 | int k; |
| 4507 | MACROBLOCKD *xd = &x->e_mbd; |
| 4508 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 4509 | struct macroblock_plane *const p = &x->plane[0]; |
| 4510 | MODE_INFO *const mi = xd->mi[0]; |
| 4511 | const BLOCK_SIZE plane_bsize = get_plane_block_size(mi->mbmi.sb_type, pd); |
Jingning Han | c4049db | 2016-10-27 14:44:13 -0700 | [diff] [blame] | 4512 | const int width = block_size_wide[plane_bsize]; |
| 4513 | const int height = block_size_high[plane_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4514 | int idx, idy; |
| 4515 | const uint8_t *const src = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4516 | &p->src.buf[av1_raster_block_offset(BLOCK_8X8, i, p->src.stride)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4517 | uint8_t *const dst = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4518 | &pd->dst.buf[av1_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4519 | int64_t thisdistortion = 0, thissse = 0; |
| 4520 | int thisrate = 0; |
| 4521 | TX_SIZE tx_size = mi->mbmi.tx_size; |
| 4522 | |
| 4523 | TX_TYPE tx_type = get_tx_type(PLANE_TYPE_Y, xd, i, tx_size); |
Jingning Han | c4049db | 2016-10-27 14:44:13 -0700 | [diff] [blame] | 4524 | const int num_4x4_w = tx_size_wide_unit[tx_size]; |
| 4525 | const int num_4x4_h = tx_size_high_unit[tx_size]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4526 | #if !CONFIG_PVQ |
| 4527 | const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1); |
| 4528 | #else |
| 4529 | (void)cpi; |
| 4530 | (void)ta; |
| 4531 | (void)tl; |
| 4532 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4533 | |
| 4534 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 4535 | assert(IMPLIES(xd->lossless[mi->mbmi.segment_id], tx_size == TX_4X4)); |
| 4536 | assert(IMPLIES(!xd->lossless[mi->mbmi.segment_id], |
| 4537 | tx_size == max_txsize_rect_lookup[mi->mbmi.sb_type])); |
| 4538 | #else |
| 4539 | assert(tx_size == TX_4X4); |
| 4540 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 4541 | assert(tx_type == DCT_DCT); |
| 4542 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4543 | av1_build_inter_predictor_sub8x8(xd, 0, i, ir, ic, mi_row, mi_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4544 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4545 | #if !CONFIG_PVQ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4546 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4547 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4548 | aom_highbd_subtract_block( |
| 4549 | height, width, av1_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff), |
| 4550 | 8, src, p->src.stride, dst, pd->dst.stride, xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4551 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4552 | aom_subtract_block(height, width, |
| 4553 | av1_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4554 | 8, src, p->src.stride, dst, pd->dst.stride); |
| 4555 | } |
| 4556 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4557 | aom_subtract_block(height, width, |
| 4558 | av1_raster_block_offset_int16(BLOCK_8X8, i, p->src_diff), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4559 | 8, src, p->src.stride, dst, pd->dst.stride); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4560 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4561 | #endif // !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4562 | |
| 4563 | k = i; |
| 4564 | for (idy = 0; idy < height / 4; idy += num_4x4_h) { |
| 4565 | for (idx = 0; idx < width / 4; idx += num_4x4_w) { |
| 4566 | int64_t dist, ssz, rd, rd1, rd2; |
| 4567 | int block; |
Yaowu Xu | 02d4c3b | 2016-11-07 10:45:56 -0800 | [diff] [blame] | 4568 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4569 | int coeff_ctx; |
Yaowu Xu | 02d4c3b | 2016-11-07 10:45:56 -0800 | [diff] [blame] | 4570 | #else |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4571 | const int src_stride = p->src.stride; |
| 4572 | const int dst_stride = pd->dst.stride; |
| 4573 | const int diff_stride = 8; |
| 4574 | tran_low_t *coeff; |
| 4575 | tran_low_t *dqcoeff; |
| 4576 | tran_low_t *ref_coeff; |
| 4577 | int16_t *pred = &pd->pred[4 * (ir * diff_stride + ic)]; |
| 4578 | int16_t *src_int16 = &p->src_int16[4 * (ir * diff_stride + ic)]; |
| 4579 | int ii, j, tx_blk_size; |
| 4580 | int rate_pvq; |
| 4581 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4582 | k += (idy * 2 + idx); |
| 4583 | if (tx_size == TX_4X4) |
| 4584 | block = k; |
| 4585 | else |
| 4586 | block = (i ? 2 : 0); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4587 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4588 | coeff_ctx = combine_entropy_contexts(*(ta + (k & 1)), *(tl + (k >> 1))); |
| 4589 | #if CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 4590 | av1_xform_quant_fp_nuq(cm, x, 0, block, idy + (i >> 1), idx + (i & 0x01), |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4591 | BLOCK_8X8, tx_size, coeff_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4592 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 4593 | av1_xform_quant(cm, x, 0, block, idy + (i >> 1), idx + (i & 0x01), |
| 4594 | BLOCK_8X8, tx_size, AV1_XFORM_QUANT_FP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4595 | #endif // CONFIG_NEW_QUANT |
| 4596 | if (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0) |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 4597 | av1_optimize_b(cm, x, 0, block, tx_size, coeff_ctx); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4598 | #else |
| 4599 | coeff = BLOCK_OFFSET(p->coeff, k); |
| 4600 | dqcoeff = BLOCK_OFFSET(pd->dqcoeff, k); |
| 4601 | ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, k); |
| 4602 | |
| 4603 | // transform block size in pixels |
| 4604 | tx_blk_size = 4; |
| 4605 | |
| 4606 | // copy uint8 orig and predicted block to int16 buffer |
| 4607 | // in order to use existing VP10 transform functions |
| 4608 | for (j = 0; j < tx_blk_size; j++) |
| 4609 | for (ii = 0; ii < tx_blk_size; ii++) { |
| 4610 | src_int16[diff_stride * j + ii] = |
| 4611 | src[src_stride * (j + 4 * idy) + (ii + 4 * idx)]; |
| 4612 | pred[diff_stride * j + ii] = |
| 4613 | dst[dst_stride * (j + 4 * idy) + (ii + 4 * idx)]; |
| 4614 | } |
| 4615 | |
Yaowu Xu | 3442b4b | 2016-11-07 10:40:41 -0800 | [diff] [blame] | 4616 | { |
| 4617 | FWD_TXFM_PARAM fwd_txfm_param; |
| 4618 | fwd_txfm_param.tx_type = DCT_DCT; |
| 4619 | fwd_txfm_param.tx_size = TX_4X4; |
| 4620 | fwd_txfm_param.fwd_txfm_opt = FWD_TXFM_OPT_NORMAL; |
| 4621 | fwd_txfm_param.rd_transform = 0; |
| 4622 | fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4623 | |
Yaowu Xu | 3442b4b | 2016-11-07 10:40:41 -0800 | [diff] [blame] | 4624 | fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param); |
| 4625 | fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param); |
| 4626 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4627 | av1_pvq_encode_helper(&x->daala_enc, coeff, ref_coeff, dqcoeff, |
| 4628 | &p->eobs[k], pd->dequant, 0, TX_4X4, tx_type, |
| 4629 | &rate_pvq, x->pvq_speed, NULL); |
| 4630 | #endif |
| 4631 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4632 | dist_block(cpi, x, 0, block, idy + (i >> 1), idx + (i & 0x1), tx_size, |
| 4633 | &dist, &ssz); |
| 4634 | thisdistortion += dist; |
| 4635 | thissse += ssz; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4636 | #if !CONFIG_PVQ |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 4637 | thisrate += |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 4638 | av1_cost_coeffs(cm, x, 0, block, coeff_ctx, tx_size, scan_order->scan, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 4639 | scan_order->neighbors, cpi->sf.use_fast_coef_costing); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4640 | #else |
| 4641 | thisrate += rate_pvq; |
| 4642 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4643 | *(ta + (k & 1)) = !(p->eobs[block] == 0); |
| 4644 | *(tl + (k >> 1)) = !(p->eobs[block] == 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4645 | #if CONFIG_EXT_TX |
| 4646 | if (tx_size == TX_8X4) { |
| 4647 | *(ta + (k & 1) + 1) = *(ta + (k & 1)); |
| 4648 | } |
| 4649 | if (tx_size == TX_4X8) { |
| 4650 | *(tl + (k >> 1) + 1) = *(tl + (k >> 1)); |
| 4651 | } |
| 4652 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4653 | rd1 = RDCOST(x->rdmult, x->rddiv, thisrate, thisdistortion); |
| 4654 | rd2 = RDCOST(x->rdmult, x->rddiv, 0, thissse); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4655 | rd = AOMMIN(rd1, rd2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4656 | if (rd >= best_yrd) return INT64_MAX; |
| 4657 | } |
| 4658 | } |
| 4659 | |
| 4660 | *distortion = thisdistortion; |
| 4661 | *labelyrate = thisrate; |
| 4662 | *sse = thissse; |
| 4663 | |
| 4664 | return RDCOST(x->rdmult, x->rddiv, *labelyrate, *distortion); |
| 4665 | } |
| 4666 | |
| 4667 | typedef struct { |
| 4668 | int eobs; |
| 4669 | int brate; |
| 4670 | int byrate; |
| 4671 | int64_t bdist; |
| 4672 | int64_t bsse; |
| 4673 | int64_t brdcost; |
| 4674 | int_mv mvs[2]; |
| 4675 | #if CONFIG_REF_MV |
| 4676 | int_mv pred_mv[2]; |
| 4677 | #endif |
| 4678 | #if CONFIG_EXT_INTER |
| 4679 | int_mv ref_mv[2]; |
| 4680 | #endif // CONFIG_EXT_INTER |
| 4681 | ENTROPY_CONTEXT ta[2]; |
| 4682 | ENTROPY_CONTEXT tl[2]; |
| 4683 | } SEG_RDSTAT; |
| 4684 | |
| 4685 | typedef struct { |
| 4686 | int_mv *ref_mv[2]; |
| 4687 | int_mv mvp; |
| 4688 | |
| 4689 | int64_t segment_rd; |
| 4690 | int r; |
| 4691 | int64_t d; |
| 4692 | int64_t sse; |
| 4693 | int segment_yrate; |
| 4694 | PREDICTION_MODE modes[4]; |
| 4695 | #if CONFIG_EXT_INTER |
| 4696 | SEG_RDSTAT rdstat[4][INTER_MODES + INTER_COMPOUND_MODES]; |
| 4697 | #else |
| 4698 | SEG_RDSTAT rdstat[4][INTER_MODES]; |
| 4699 | #endif // CONFIG_EXT_INTER |
| 4700 | int mvthresh; |
| 4701 | } BEST_SEG_INFO; |
| 4702 | |
| 4703 | static INLINE int mv_check_bounds(const MACROBLOCK *x, const MV *mv) { |
| 4704 | return (mv->row >> 3) < x->mv_row_min || (mv->row >> 3) > x->mv_row_max || |
| 4705 | (mv->col >> 3) < x->mv_col_min || (mv->col >> 3) > x->mv_col_max; |
| 4706 | } |
| 4707 | |
| 4708 | static INLINE void mi_buf_shift(MACROBLOCK *x, int i) { |
| 4709 | MB_MODE_INFO *const mbmi = &x->e_mbd.mi[0]->mbmi; |
| 4710 | struct macroblock_plane *const p = &x->plane[0]; |
| 4711 | struct macroblockd_plane *const pd = &x->e_mbd.plane[0]; |
| 4712 | |
| 4713 | p->src.buf = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4714 | &p->src.buf[av1_raster_block_offset(BLOCK_8X8, i, p->src.stride)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4715 | assert(((intptr_t)pd->pre[0].buf & 0x7) == 0); |
| 4716 | pd->pre[0].buf = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4717 | &pd->pre[0].buf[av1_raster_block_offset(BLOCK_8X8, i, pd->pre[0].stride)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4718 | if (has_second_ref(mbmi)) |
| 4719 | pd->pre[1].buf = |
| 4720 | &pd->pre[1] |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4721 | .buf[av1_raster_block_offset(BLOCK_8X8, i, pd->pre[1].stride)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | static INLINE void mi_buf_restore(MACROBLOCK *x, struct buf_2d orig_src, |
| 4725 | struct buf_2d orig_pre[2]) { |
| 4726 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
| 4727 | x->plane[0].src = orig_src; |
| 4728 | x->e_mbd.plane[0].pre[0] = orig_pre[0]; |
| 4729 | if (has_second_ref(mbmi)) x->e_mbd.plane[0].pre[1] = orig_pre[1]; |
| 4730 | } |
| 4731 | |
| 4732 | // Check if NEARESTMV/NEARMV/ZEROMV is the cheapest way encode zero motion. |
| 4733 | // TODO(aconverse): Find out if this is still productive then clean up or remove |
| 4734 | static int check_best_zero_mv( |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4735 | const AV1_COMP *const cpi, const int16_t mode_context[TOTAL_REFS_PER_FRAME], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4736 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
| 4737 | const int16_t compound_mode_context[TOTAL_REFS_PER_FRAME], |
| 4738 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 4739 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME], int this_mode, |
| 4740 | const MV_REFERENCE_FRAME ref_frames[2], const BLOCK_SIZE bsize, int block) { |
| 4741 | |
| 4742 | #if !CONFIG_EXT_INTER |
| 4743 | assert(ref_frames[1] != INTRA_FRAME); // Just sanity check |
| 4744 | #endif |
| 4745 | |
| 4746 | if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) && |
| 4747 | frame_mv[this_mode][ref_frames[0]].as_int == 0 && |
| 4748 | (ref_frames[1] <= INTRA_FRAME || |
| 4749 | frame_mv[this_mode][ref_frames[1]].as_int == 0)) { |
| 4750 | #if CONFIG_REF_MV |
| 4751 | int16_t rfc = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4752 | av1_mode_context_analyzer(mode_context, ref_frames, bsize, block); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4753 | #else |
| 4754 | int16_t rfc = mode_context[ref_frames[0]]; |
| 4755 | #endif |
| 4756 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
| 4757 | int c1 = cost_mv_ref(cpi, NEARMV, ref_frames[1] > INTRA_FRAME, rfc); |
| 4758 | int c2 = cost_mv_ref(cpi, NEARESTMV, ref_frames[1] > INTRA_FRAME, rfc); |
| 4759 | int c3 = cost_mv_ref(cpi, ZEROMV, ref_frames[1] > INTRA_FRAME, rfc); |
| 4760 | #else |
| 4761 | int c1 = cost_mv_ref(cpi, NEARMV, rfc); |
| 4762 | int c2 = cost_mv_ref(cpi, NEARESTMV, rfc); |
| 4763 | int c3 = cost_mv_ref(cpi, ZEROMV, rfc); |
| 4764 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 4765 | |
| 4766 | #if !CONFIG_REF_MV |
| 4767 | (void)bsize; |
| 4768 | (void)block; |
| 4769 | #endif |
| 4770 | |
| 4771 | if (this_mode == NEARMV) { |
| 4772 | if (c1 > c3) return 0; |
| 4773 | } else if (this_mode == NEARESTMV) { |
| 4774 | if (c2 > c3) return 0; |
| 4775 | } else { |
| 4776 | assert(this_mode == ZEROMV); |
| 4777 | if (ref_frames[1] <= INTRA_FRAME) { |
| 4778 | if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0) || |
| 4779 | (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0)) |
| 4780 | return 0; |
| 4781 | } else { |
| 4782 | if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0 && |
| 4783 | frame_mv[NEARESTMV][ref_frames[1]].as_int == 0) || |
| 4784 | (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0 && |
| 4785 | frame_mv[NEARMV][ref_frames[1]].as_int == 0)) |
| 4786 | return 0; |
| 4787 | } |
| 4788 | } |
| 4789 | } |
| 4790 | #if CONFIG_EXT_INTER |
| 4791 | else if ((this_mode == NEAREST_NEARESTMV || this_mode == NEAREST_NEARMV || |
| 4792 | this_mode == NEAR_NEARESTMV || this_mode == NEAR_NEARMV || |
| 4793 | this_mode == ZERO_ZEROMV) && |
| 4794 | frame_mv[this_mode][ref_frames[0]].as_int == 0 && |
| 4795 | frame_mv[this_mode][ref_frames[1]].as_int == 0) { |
| 4796 | #if CONFIG_REF_MV |
| 4797 | int16_t rfc = compound_mode_context[ref_frames[0]]; |
| 4798 | int c1 = cost_mv_ref(cpi, NEAREST_NEARMV, 1, rfc); |
| 4799 | int c2 = cost_mv_ref(cpi, NEAREST_NEARESTMV, 1, rfc); |
| 4800 | int c3 = cost_mv_ref(cpi, ZERO_ZEROMV, 1, rfc); |
| 4801 | int c4 = cost_mv_ref(cpi, NEAR_NEARESTMV, 1, rfc); |
| 4802 | int c5 = cost_mv_ref(cpi, NEAR_NEARMV, 1, rfc); |
| 4803 | #else |
| 4804 | int16_t rfc = mode_context[ref_frames[0]]; |
| 4805 | int c1 = cost_mv_ref(cpi, NEAREST_NEARMV, rfc); |
| 4806 | int c2 = cost_mv_ref(cpi, NEAREST_NEARESTMV, rfc); |
| 4807 | int c3 = cost_mv_ref(cpi, ZERO_ZEROMV, rfc); |
| 4808 | int c4 = cost_mv_ref(cpi, NEAR_NEARESTMV, rfc); |
| 4809 | int c5 = cost_mv_ref(cpi, NEAR_NEARMV, rfc); |
| 4810 | #endif |
| 4811 | |
| 4812 | if (this_mode == NEAREST_NEARMV) { |
| 4813 | if (c1 > c3) return 0; |
| 4814 | } else if (this_mode == NEAREST_NEARESTMV) { |
| 4815 | if (c2 > c3) return 0; |
| 4816 | } else if (this_mode == NEAR_NEARESTMV) { |
| 4817 | if (c4 > c3) return 0; |
| 4818 | } else if (this_mode == NEAR_NEARMV) { |
| 4819 | if (c5 > c3) return 0; |
| 4820 | } else { |
| 4821 | assert(this_mode == ZERO_ZEROMV); |
| 4822 | if ((c3 >= c2 && frame_mv[NEAREST_NEARESTMV][ref_frames[0]].as_int == 0 && |
| 4823 | frame_mv[NEAREST_NEARESTMV][ref_frames[1]].as_int == 0) || |
| 4824 | (c3 >= c1 && frame_mv[NEAREST_NEARMV][ref_frames[0]].as_int == 0 && |
| 4825 | frame_mv[NEAREST_NEARMV][ref_frames[1]].as_int == 0) || |
| 4826 | (c3 >= c5 && frame_mv[NEAR_NEARMV][ref_frames[0]].as_int == 0 && |
| 4827 | frame_mv[NEAR_NEARMV][ref_frames[1]].as_int == 0) || |
| 4828 | (c3 >= c4 && frame_mv[NEAR_NEARESTMV][ref_frames[0]].as_int == 0 && |
| 4829 | frame_mv[NEAR_NEARESTMV][ref_frames[1]].as_int == 0)) |
| 4830 | return 0; |
| 4831 | } |
| 4832 | } |
| 4833 | #endif // CONFIG_EXT_INTER |
| 4834 | return 1; |
| 4835 | } |
| 4836 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4837 | static void joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x, |
| 4838 | BLOCK_SIZE bsize, int_mv *frame_mv, int mi_row, |
| 4839 | int mi_col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4840 | #if CONFIG_EXT_INTER |
| 4841 | int_mv *ref_mv_sub8x8[2], |
| 4842 | #endif |
| 4843 | int_mv single_newmv[TOTAL_REFS_PER_FRAME], |
| 4844 | int *rate_mv, const int block) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4845 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4846 | const int pw = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 4847 | const int ph = 4 * num_4x4_blocks_high_lookup[bsize]; |
| 4848 | MACROBLOCKD *xd = &x->e_mbd; |
| 4849 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 4850 | const int refs[2] = { mbmi->ref_frame[0], |
| 4851 | mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1] }; |
| 4852 | int_mv ref_mv[2]; |
| 4853 | int ite, ref; |
| 4854 | #if CONFIG_DUAL_FILTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 4855 | InterpFilter interp_filter[4] = { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4856 | mbmi->interp_filter[0], mbmi->interp_filter[1], mbmi->interp_filter[2], |
| 4857 | mbmi->interp_filter[3], |
| 4858 | }; |
| 4859 | #else |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 4860 | const InterpFilter interp_filter = mbmi->interp_filter; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4861 | #endif |
| 4862 | struct scale_factors sf; |
| 4863 | |
| 4864 | // Do joint motion search in compound mode to get more accurate mv. |
| 4865 | struct buf_2d backup_yv12[2][MAX_MB_PLANE]; |
| 4866 | int last_besterr[2] = { INT_MAX, INT_MAX }; |
| 4867 | const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4868 | av1_get_scaled_ref_frame(cpi, mbmi->ref_frame[0]), |
| 4869 | av1_get_scaled_ref_frame(cpi, mbmi->ref_frame[1]) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4870 | }; |
| 4871 | |
| 4872 | // Prediction buffer from second frame. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4873 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4874 | DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]); |
| 4875 | uint8_t *second_pred; |
| 4876 | #else |
| 4877 | DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4878 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4879 | |
| 4880 | for (ref = 0; ref < 2; ++ref) { |
| 4881 | #if CONFIG_EXT_INTER |
| 4882 | if (bsize < BLOCK_8X8 && ref_mv_sub8x8 != NULL) |
| 4883 | ref_mv[ref].as_int = ref_mv_sub8x8[ref]->as_int; |
| 4884 | else |
| 4885 | #endif // CONFIG_EXT_INTER |
| 4886 | ref_mv[ref] = x->mbmi_ext->ref_mvs[refs[ref]][0]; |
| 4887 | |
| 4888 | if (scaled_ref_frame[ref]) { |
| 4889 | int i; |
| 4890 | // Swap out the reference frame for a version that's been scaled to |
| 4891 | // match the resolution of the current frame, allowing the existing |
| 4892 | // motion search code to be used without additional modifications. |
| 4893 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 4894 | backup_yv12[ref][i] = xd->plane[i].pre[ref]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4895 | av1_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col, |
| 4896 | NULL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | frame_mv[refs[ref]].as_int = single_newmv[refs[ref]].as_int; |
| 4900 | } |
| 4901 | |
| 4902 | // Since we have scaled the reference frames to match the size of the current |
| 4903 | // frame we must use a unit scaling factor during mode selection. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4904 | #if CONFIG_AOM_HIGHBITDEPTH |
| 4905 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 4906 | cm->height, cm->use_highbitdepth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4907 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4908 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 4909 | cm->height); |
| 4910 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4911 | |
| 4912 | // Allow joint search multiple times iteratively for each reference frame |
| 4913 | // and break out of the search loop if it couldn't find a better mv. |
| 4914 | for (ite = 0; ite < 4; ite++) { |
| 4915 | struct buf_2d ref_yv12[2]; |
| 4916 | int bestsme = INT_MAX; |
| 4917 | int sadpb = x->sadperbit16; |
| 4918 | MV *const best_mv = &x->best_mv.as_mv; |
| 4919 | int search_range = 3; |
| 4920 | |
| 4921 | int tmp_col_min = x->mv_col_min; |
| 4922 | int tmp_col_max = x->mv_col_max; |
| 4923 | int tmp_row_min = x->mv_row_min; |
| 4924 | int tmp_row_max = x->mv_row_max; |
| 4925 | int id = ite % 2; // Even iterations search in the first reference frame, |
| 4926 | // odd iterations search in the second. The predictor |
| 4927 | // found for the 'other' reference frame is factored in. |
| 4928 | |
| 4929 | // Initialized here because of compiler problem in Visual Studio. |
| 4930 | ref_yv12[0] = xd->plane[0].pre[0]; |
| 4931 | ref_yv12[1] = xd->plane[0].pre[1]; |
| 4932 | |
| 4933 | #if CONFIG_DUAL_FILTER |
| 4934 | // reload the filter types |
| 4935 | interp_filter[0] = |
| 4936 | (id == 0) ? mbmi->interp_filter[2] : mbmi->interp_filter[0]; |
| 4937 | interp_filter[1] = |
| 4938 | (id == 0) ? mbmi->interp_filter[3] : mbmi->interp_filter[1]; |
| 4939 | #endif |
| 4940 | |
| 4941 | // Get the prediction block from the 'other' reference frame. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4942 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4943 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 4944 | second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4945 | av1_highbd_build_inter_predictor( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4946 | ref_yv12[!id].buf, ref_yv12[!id].stride, second_pred, pw, |
| 4947 | &frame_mv[refs[!id]].as_mv, &sf, pw, ph, 0, interp_filter, |
| 4948 | MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd->bd); |
| 4949 | } else { |
| 4950 | second_pred = (uint8_t *)second_pred_alloc_16; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4951 | av1_build_inter_predictor(ref_yv12[!id].buf, ref_yv12[!id].stride, |
| 4952 | second_pred, pw, &frame_mv[refs[!id]].as_mv, |
| 4953 | &sf, pw, ph, 0, interp_filter, MV_PRECISION_Q3, |
| 4954 | mi_col * MI_SIZE, mi_row * MI_SIZE); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4955 | } |
| 4956 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4957 | av1_build_inter_predictor(ref_yv12[!id].buf, ref_yv12[!id].stride, |
| 4958 | second_pred, pw, &frame_mv[refs[!id]].as_mv, &sf, |
| 4959 | pw, ph, 0, interp_filter, MV_PRECISION_Q3, |
| 4960 | mi_col * MI_SIZE, mi_row * MI_SIZE); |
| 4961 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4962 | |
| 4963 | // Do compound motion search on the current reference frame. |
| 4964 | if (id) xd->plane[0].pre[0] = ref_yv12[id]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4965 | av1_set_mv_search_range(x, &ref_mv[id].as_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4966 | |
| 4967 | // Use the mv result from the single mode as mv predictor. |
| 4968 | *best_mv = frame_mv[refs[id]].as_mv; |
| 4969 | |
| 4970 | best_mv->col >>= 3; |
| 4971 | best_mv->row >>= 3; |
| 4972 | |
| 4973 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 4974 | av1_set_mvcost(x, refs[id], id, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4975 | #endif |
| 4976 | |
| 4977 | // Small-range full-pixel motion search. |
| 4978 | bestsme = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4979 | av1_refining_search_8p_c(x, sadpb, search_range, &cpi->fn_ptr[bsize], |
| 4980 | &ref_mv[id].as_mv, second_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4981 | if (bestsme < INT_MAX) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4982 | bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv[id].as_mv, |
| 4983 | second_pred, &cpi->fn_ptr[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4984 | |
| 4985 | x->mv_col_min = tmp_col_min; |
| 4986 | x->mv_col_max = tmp_col_max; |
| 4987 | x->mv_row_min = tmp_row_min; |
| 4988 | x->mv_row_max = tmp_row_max; |
| 4989 | |
| 4990 | if (bestsme < INT_MAX) { |
| 4991 | int dis; /* TODO: use dis in distortion calculation later. */ |
| 4992 | unsigned int sse; |
| 4993 | if (cpi->sf.use_upsampled_references) { |
| 4994 | // Use up-sampled reference frames. |
| 4995 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 4996 | struct buf_2d backup_pred = pd->pre[0]; |
| 4997 | const YV12_BUFFER_CONFIG *upsampled_ref = |
| 4998 | get_upsampled_ref(cpi, refs[id]); |
| 4999 | |
| 5000 | // Set pred for Y plane |
| 5001 | setup_pred_plane(&pd->pre[0], upsampled_ref->y_buffer, |
| 5002 | upsampled_ref->y_crop_width, |
| 5003 | upsampled_ref->y_crop_height, upsampled_ref->y_stride, |
| 5004 | (mi_row << 3), (mi_col << 3), NULL, pd->subsampling_x, |
| 5005 | pd->subsampling_y); |
| 5006 | |
| 5007 | // If bsize < BLOCK_8X8, adjust pred pointer for this block |
| 5008 | if (bsize < BLOCK_8X8) |
| 5009 | pd->pre[0].buf = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5010 | &pd->pre[0].buf[(av1_raster_block_offset(BLOCK_8X8, block, |
| 5011 | pd->pre[0].stride)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5012 | << 3]; |
| 5013 | |
| 5014 | bestsme = cpi->find_fractional_mv_step( |
| 5015 | x, &ref_mv[id].as_mv, cpi->common.allow_high_precision_mv, |
| 5016 | x->errorperbit, &cpi->fn_ptr[bsize], 0, |
| 5017 | cpi->sf.mv.subpel_iters_per_step, NULL, x->nmvjointcost, x->mvcost, |
| 5018 | &dis, &sse, second_pred, pw, ph, 1); |
| 5019 | |
| 5020 | // Restore the reference frames. |
| 5021 | pd->pre[0] = backup_pred; |
| 5022 | } else { |
| 5023 | (void)block; |
| 5024 | bestsme = cpi->find_fractional_mv_step( |
| 5025 | x, &ref_mv[id].as_mv, cpi->common.allow_high_precision_mv, |
| 5026 | x->errorperbit, &cpi->fn_ptr[bsize], 0, |
| 5027 | cpi->sf.mv.subpel_iters_per_step, NULL, x->nmvjointcost, x->mvcost, |
| 5028 | &dis, &sse, second_pred, pw, ph, 0); |
| 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | // Restore the pointer to the first (possibly scaled) prediction buffer. |
| 5033 | if (id) xd->plane[0].pre[0] = ref_yv12[0]; |
| 5034 | |
| 5035 | if (bestsme < last_besterr[id]) { |
| 5036 | frame_mv[refs[id]].as_mv = *best_mv; |
| 5037 | last_besterr[id] = bestsme; |
| 5038 | } else { |
| 5039 | break; |
| 5040 | } |
| 5041 | } |
| 5042 | |
| 5043 | *rate_mv = 0; |
| 5044 | |
| 5045 | for (ref = 0; ref < 2; ++ref) { |
| 5046 | if (scaled_ref_frame[ref]) { |
| 5047 | // Restore the prediction frame pointers to their unscaled versions. |
| 5048 | int i; |
| 5049 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 5050 | xd->plane[i].pre[ref] = backup_yv12[ref][i]; |
| 5051 | } |
| 5052 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 5053 | av1_set_mvcost(x, refs[ref], ref, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5054 | #endif |
| 5055 | #if CONFIG_EXT_INTER |
| 5056 | if (bsize >= BLOCK_8X8) |
| 5057 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5058 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv, |
| 5059 | &x->mbmi_ext->ref_mvs[refs[ref]][0].as_mv, |
| 5060 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5061 | #if CONFIG_EXT_INTER |
| 5062 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5063 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv, |
| 5064 | &ref_mv_sub8x8[ref]->as_mv, x->nmvjointcost, |
| 5065 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5066 | #endif // CONFIG_EXT_INTER |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | static int64_t rd_pick_best_sub8x8_mode( |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 5071 | const AV1_COMP *const cpi, MACROBLOCK *x, int_mv *best_ref_mv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5072 | int_mv *second_best_ref_mv, int64_t best_rd, int *returntotrate, |
| 5073 | int *returnyrate, int64_t *returndistortion, int *skippable, int64_t *psse, |
| 5074 | int mvthresh, |
| 5075 | #if CONFIG_EXT_INTER |
| 5076 | int_mv seg_mvs[4][2][TOTAL_REFS_PER_FRAME], |
| 5077 | int_mv compound_seg_newmvs[4][2], |
| 5078 | #else |
| 5079 | int_mv seg_mvs[4][TOTAL_REFS_PER_FRAME], |
| 5080 | #endif // CONFIG_EXT_INTER |
| 5081 | BEST_SEG_INFO *bsi_buf, int filter_idx, int mi_row, int mi_col) { |
| 5082 | BEST_SEG_INFO *bsi = bsi_buf + filter_idx; |
| 5083 | #if CONFIG_REF_MV |
| 5084 | int_mv tmp_ref_mv[2]; |
| 5085 | #endif |
| 5086 | MACROBLOCKD *xd = &x->e_mbd; |
| 5087 | MODE_INFO *mi = xd->mi[0]; |
| 5088 | MB_MODE_INFO *mbmi = &mi->mbmi; |
| 5089 | int mode_idx; |
| 5090 | int k, br = 0, idx, idy; |
| 5091 | int64_t bd = 0, block_sse = 0; |
| 5092 | PREDICTION_MODE this_mode; |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 5093 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5094 | struct macroblock_plane *const p = &x->plane[0]; |
| 5095 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 5096 | const int label_count = 4; |
| 5097 | int64_t this_segment_rd = 0; |
| 5098 | int label_mv_thresh; |
| 5099 | int segmentyrate = 0; |
| 5100 | const BLOCK_SIZE bsize = mbmi->sb_type; |
| 5101 | const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; |
| 5102 | const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; |
| 5103 | ENTROPY_CONTEXT t_above[2], t_left[2]; |
| 5104 | int subpelmv = 1, have_ref = 0; |
| 5105 | const int has_second_rf = has_second_ref(mbmi); |
| 5106 | const int inter_mode_mask = cpi->sf.inter_mode_mask[bsize]; |
| 5107 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5108 | #if CONFIG_PVQ |
| 5109 | od_rollback_buffer pre_buf; |
| 5110 | |
| 5111 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 5112 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5113 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 5114 | mbmi->tx_size = |
| 5115 | xd->lossless[mbmi->segment_id] ? TX_4X4 : max_txsize_rect_lookup[bsize]; |
| 5116 | #else |
| 5117 | mbmi->tx_size = TX_4X4; |
| 5118 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 5119 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5120 | av1_zero(*bsi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5121 | |
| 5122 | bsi->segment_rd = best_rd; |
| 5123 | bsi->ref_mv[0] = best_ref_mv; |
| 5124 | bsi->ref_mv[1] = second_best_ref_mv; |
| 5125 | bsi->mvp.as_int = best_ref_mv->as_int; |
| 5126 | bsi->mvthresh = mvthresh; |
| 5127 | |
| 5128 | for (idx = 0; idx < 4; ++idx) bsi->modes[idx] = ZEROMV; |
| 5129 | |
| 5130 | #if CONFIG_REFMV |
| 5131 | for (idx = 0; idx < 4; ++idx) { |
| 5132 | for (k = NEARESTMV; k <= NEWMV; ++k) { |
| 5133 | bsi->rdstat[idx][INTER_OFFSET(k)].pred_mv[0].as_int = INVALID_MV; |
| 5134 | bsi->rdstat[idx][INTER_OFFSET(k)].pred_mv[1].as_int = INVALID_MV; |
| 5135 | |
| 5136 | bsi->rdstat[idx][INTER_OFFSET(k)].mvs[0].as_int = INVALID_MV; |
| 5137 | bsi->rdstat[idx][INTER_OFFSET(k)].mvs[1].as_int = INVALID_MV; |
| 5138 | } |
| 5139 | } |
| 5140 | #endif |
| 5141 | |
| 5142 | memcpy(t_above, pd->above_context, sizeof(t_above)); |
| 5143 | memcpy(t_left, pd->left_context, sizeof(t_left)); |
| 5144 | |
| 5145 | // 64 makes this threshold really big effectively |
| 5146 | // making it so that we very rarely check mvs on |
| 5147 | // segments. setting this to 1 would make mv thresh |
| 5148 | // roughly equal to what it is for macroblocks |
| 5149 | label_mv_thresh = 1 * bsi->mvthresh / label_count; |
| 5150 | |
| 5151 | // Segmentation method overheads |
| 5152 | for (idy = 0; idy < 2; idy += num_4x4_blocks_high) { |
| 5153 | for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) { |
| 5154 | // TODO(jingning,rbultje): rewrite the rate-distortion optimization |
| 5155 | // loop for 4x4/4x8/8x4 block coding. to be replaced with new rd loop |
| 5156 | int_mv mode_mv[MB_MODE_COUNT][2]; |
| 5157 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 5158 | PREDICTION_MODE mode_selected = ZEROMV; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5159 | int64_t new_best_rd = INT64_MAX; |
| 5160 | const int index = idy * 2 + idx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5161 | int ref; |
| 5162 | #if CONFIG_REF_MV |
| 5163 | CANDIDATE_MV ref_mv_stack[2][MAX_REF_MV_STACK_SIZE]; |
| 5164 | uint8_t ref_mv_count[2]; |
| 5165 | #endif |
| 5166 | #if CONFIG_EXT_INTER |
| 5167 | int mv_idx; |
| 5168 | int_mv ref_mvs_sub8x8[2][2]; |
| 5169 | #endif // CONFIG_EXT_INTER |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5170 | #if CONFIG_PVQ |
| 5171 | od_rollback_buffer idx_buf, post_buf; |
| 5172 | od_encode_checkpoint(&x->daala_enc, &idx_buf); |
| 5173 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 5174 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5175 | |
| 5176 | for (ref = 0; ref < 1 + has_second_rf; ++ref) { |
| 5177 | const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref]; |
| 5178 | #if CONFIG_EXT_INTER |
| 5179 | int_mv mv_ref_list[MAX_MV_REF_CANDIDATES]; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5180 | av1_update_mv_context(xd, mi, frame, mv_ref_list, index, mi_row, mi_col, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5181 | NULL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5182 | #endif // CONFIG_EXT_INTER |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 5183 | #if CONFIG_GLOBAL_MOTION |
| 5184 | frame_mv[ZEROMV][frame].as_int = |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 5185 | gm_get_motion_vector(&cm->global_motion[frame]).as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 5186 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5187 | frame_mv[ZEROMV][frame].as_int = 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 5188 | #endif // CONFIG_GLOBAL_MOTION |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5189 | av1_append_sub8x8_mvs_for_idx(cm, xd, index, ref, mi_row, mi_col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5190 | #if CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5191 | ref_mv_stack[ref], &ref_mv_count[ref], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5192 | #endif |
| 5193 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5194 | mv_ref_list, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5195 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5196 | &frame_mv[NEARESTMV][frame], |
| 5197 | &frame_mv[NEARMV][frame]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5198 | |
| 5199 | #if CONFIG_REF_MV |
| 5200 | tmp_ref_mv[ref] = frame_mv[NEARESTMV][mbmi->ref_frame[ref]]; |
| 5201 | lower_mv_precision(&tmp_ref_mv[ref].as_mv, cm->allow_high_precision_mv); |
| 5202 | bsi->ref_mv[ref] = &tmp_ref_mv[ref]; |
| 5203 | mbmi_ext->ref_mvs[frame][0] = tmp_ref_mv[ref]; |
| 5204 | #endif |
| 5205 | |
| 5206 | #if CONFIG_EXT_INTER |
| 5207 | mv_ref_list[0].as_int = frame_mv[NEARESTMV][frame].as_int; |
| 5208 | mv_ref_list[1].as_int = frame_mv[NEARMV][frame].as_int; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5209 | av1_find_best_ref_mvs(cm->allow_high_precision_mv, mv_ref_list, |
| 5210 | &ref_mvs_sub8x8[0][ref], &ref_mvs_sub8x8[1][ref]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5211 | |
| 5212 | if (has_second_rf) { |
| 5213 | frame_mv[ZERO_ZEROMV][frame].as_int = 0; |
| 5214 | frame_mv[NEAREST_NEARESTMV][frame].as_int = |
| 5215 | frame_mv[NEARESTMV][frame].as_int; |
| 5216 | |
| 5217 | if (ref == 0) { |
| 5218 | frame_mv[NEAREST_NEARMV][frame].as_int = |
| 5219 | frame_mv[NEARESTMV][frame].as_int; |
| 5220 | frame_mv[NEAR_NEARESTMV][frame].as_int = |
| 5221 | frame_mv[NEARMV][frame].as_int; |
| 5222 | frame_mv[NEAREST_NEWMV][frame].as_int = |
| 5223 | frame_mv[NEARESTMV][frame].as_int; |
| 5224 | frame_mv[NEAR_NEWMV][frame].as_int = frame_mv[NEARMV][frame].as_int; |
| 5225 | frame_mv[NEAR_NEARMV][frame].as_int = |
| 5226 | frame_mv[NEARMV][frame].as_int; |
| 5227 | } else if (ref == 1) { |
| 5228 | frame_mv[NEAREST_NEARMV][frame].as_int = |
| 5229 | frame_mv[NEARMV][frame].as_int; |
| 5230 | frame_mv[NEAR_NEARESTMV][frame].as_int = |
| 5231 | frame_mv[NEARESTMV][frame].as_int; |
| 5232 | frame_mv[NEW_NEARESTMV][frame].as_int = |
| 5233 | frame_mv[NEARESTMV][frame].as_int; |
| 5234 | frame_mv[NEW_NEARMV][frame].as_int = frame_mv[NEARMV][frame].as_int; |
| 5235 | frame_mv[NEAR_NEARMV][frame].as_int = |
| 5236 | frame_mv[NEARMV][frame].as_int; |
| 5237 | } |
| 5238 | } |
| 5239 | #endif // CONFIG_EXT_INTER |
| 5240 | } |
| 5241 | |
| 5242 | // search for the best motion vector on this segment |
| 5243 | #if CONFIG_EXT_INTER |
| 5244 | for (this_mode = (has_second_rf ? NEAREST_NEARESTMV : NEARESTMV); |
| 5245 | this_mode <= (has_second_rf ? NEW_NEWMV : NEWFROMNEARMV); |
| 5246 | ++this_mode) |
| 5247 | #else |
| 5248 | for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) |
| 5249 | #endif // CONFIG_EXT_INTER |
| 5250 | { |
| 5251 | const struct buf_2d orig_src = x->plane[0].src; |
| 5252 | struct buf_2d orig_pre[2]; |
| 5253 | // This flag controls if the motion estimation will kick off. When it |
| 5254 | // is set to a non-zero value, the encoder will force motion estimation. |
| 5255 | int run_mv_search = 0; |
| 5256 | |
| 5257 | mode_idx = INTER_OFFSET(this_mode); |
| 5258 | #if CONFIG_EXT_INTER |
| 5259 | mv_idx = (this_mode == NEWFROMNEARMV) ? 1 : 0; |
| 5260 | |
| 5261 | for (ref = 0; ref < 1 + has_second_rf; ++ref) |
| 5262 | bsi->ref_mv[ref]->as_int = ref_mvs_sub8x8[mv_idx][ref].as_int; |
| 5263 | #endif // CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5264 | bsi->rdstat[index][mode_idx].brdcost = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5265 | if (!(inter_mode_mask & (1 << this_mode))) continue; |
| 5266 | |
| 5267 | #if CONFIG_REF_MV |
| 5268 | run_mv_search = 2; |
| 5269 | #if !CONFIG_EXT_INTER |
| 5270 | if (filter_idx > 0 && this_mode == NEWMV) { |
| 5271 | BEST_SEG_INFO *ref_bsi = bsi_buf; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5272 | SEG_RDSTAT *ref_rdstat = &ref_bsi->rdstat[index][mode_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5273 | |
| 5274 | if (has_second_rf) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5275 | if (seg_mvs[index][mbmi->ref_frame[0]].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5276 | ref_rdstat->mvs[0].as_int && |
| 5277 | ref_rdstat->mvs[0].as_int != INVALID_MV) |
| 5278 | if (bsi->ref_mv[0]->as_int == ref_rdstat->pred_mv[0].as_int) |
| 5279 | --run_mv_search; |
| 5280 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5281 | if (seg_mvs[index][mbmi->ref_frame[1]].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5282 | ref_rdstat->mvs[1].as_int && |
| 5283 | ref_rdstat->mvs[1].as_int != INVALID_MV) |
| 5284 | if (bsi->ref_mv[1]->as_int == ref_rdstat->pred_mv[1].as_int) |
| 5285 | --run_mv_search; |
| 5286 | } else { |
| 5287 | if (bsi->ref_mv[0]->as_int == ref_rdstat->pred_mv[0].as_int && |
| 5288 | ref_rdstat->mvs[0].as_int != INVALID_MV) { |
| 5289 | run_mv_search = 0; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5290 | seg_mvs[index][mbmi->ref_frame[0]].as_int = |
| 5291 | ref_rdstat->mvs[0].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5292 | } |
| 5293 | } |
| 5294 | |
| 5295 | if (run_mv_search != 0 && filter_idx > 1) { |
| 5296 | ref_bsi = bsi_buf + 1; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5297 | ref_rdstat = &ref_bsi->rdstat[index][mode_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5298 | run_mv_search = 2; |
| 5299 | |
| 5300 | if (has_second_rf) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5301 | if (seg_mvs[index][mbmi->ref_frame[0]].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5302 | ref_rdstat->mvs[0].as_int && |
| 5303 | ref_rdstat->mvs[0].as_int != INVALID_MV) |
| 5304 | if (bsi->ref_mv[0]->as_int == ref_rdstat->pred_mv[0].as_int) |
| 5305 | --run_mv_search; |
| 5306 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5307 | if (seg_mvs[index][mbmi->ref_frame[1]].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5308 | ref_rdstat->mvs[1].as_int && |
| 5309 | ref_rdstat->mvs[1].as_int != INVALID_MV) |
| 5310 | if (bsi->ref_mv[1]->as_int == ref_rdstat->pred_mv[1].as_int) |
| 5311 | --run_mv_search; |
| 5312 | } else { |
| 5313 | if (bsi->ref_mv[0]->as_int == ref_rdstat->pred_mv[0].as_int && |
| 5314 | ref_rdstat->mvs[0].as_int != INVALID_MV) { |
| 5315 | run_mv_search = 0; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5316 | seg_mvs[index][mbmi->ref_frame[0]].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5317 | ref_rdstat->mvs[0].as_int; |
| 5318 | } |
| 5319 | } |
| 5320 | } |
| 5321 | } |
| 5322 | #endif // CONFIG_EXT_INTER |
| 5323 | #endif // CONFIG_REF_MV |
| 5324 | |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 5325 | #if CONFIG_GLOBAL_MOTION |
| 5326 | if (get_gmtype(&cm->global_motion[mbmi->ref_frame[0]]) == GLOBAL_ZERO && |
| 5327 | (!has_second_rf || |
| 5328 | get_gmtype(&cm->global_motion[mbmi->ref_frame[1]]) == GLOBAL_ZERO)) |
| 5329 | #endif // CONFIG_GLOBAL_MOTION |
| 5330 | |
| 5331 | if (!check_best_zero_mv(cpi, mbmi_ext->mode_context, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5332 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 5333 | mbmi_ext->compound_mode_context, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5334 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 5335 | frame_mv, this_mode, mbmi->ref_frame, bsize, |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5336 | index)) |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 5337 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5338 | |
| 5339 | memcpy(orig_pre, pd->pre, sizeof(orig_pre)); |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5340 | memcpy(bsi->rdstat[index][mode_idx].ta, t_above, |
| 5341 | sizeof(bsi->rdstat[index][mode_idx].ta)); |
| 5342 | memcpy(bsi->rdstat[index][mode_idx].tl, t_left, |
| 5343 | sizeof(bsi->rdstat[index][mode_idx].tl)); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5344 | #if CONFIG_PVQ |
| 5345 | od_encode_rollback(&x->daala_enc, &idx_buf); |
| 5346 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5347 | |
| 5348 | // motion search for newmv (single predictor case only) |
| 5349 | if (!has_second_rf && |
| 5350 | #if CONFIG_EXT_INTER |
| 5351 | have_newmv_in_inter_mode(this_mode) && |
Alex Converse | 6317c88 | 2016-09-29 14:21:37 -0700 | [diff] [blame] | 5352 | (seg_mvs[index][mv_idx][mbmi->ref_frame[0]].as_int == INVALID_MV) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5353 | #else |
| 5354 | this_mode == NEWMV && |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5355 | (seg_mvs[index][mbmi->ref_frame[0]].as_int == INVALID_MV || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5356 | run_mv_search) |
| 5357 | #endif // CONFIG_EXT_INTER |
| 5358 | ) { |
| 5359 | int step_param = 0; |
| 5360 | int bestsme = INT_MAX; |
| 5361 | int sadpb = x->sadperbit4; |
| 5362 | MV mvp_full; |
| 5363 | int max_mv; |
| 5364 | int cost_list[5]; |
| 5365 | int tmp_col_min = x->mv_col_min; |
| 5366 | int tmp_col_max = x->mv_col_max; |
| 5367 | int tmp_row_min = x->mv_row_min; |
| 5368 | int tmp_row_max = x->mv_row_max; |
| 5369 | |
| 5370 | /* Is the best so far sufficiently good that we cant justify doing |
| 5371 | * and new motion search. */ |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5372 | if (new_best_rd < label_mv_thresh) break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5373 | |
| 5374 | if (cpi->oxcf.mode != BEST) { |
| 5375 | #if CONFIG_EXT_INTER |
| 5376 | bsi->mvp.as_int = bsi->ref_mv[0]->as_int; |
| 5377 | #else |
| 5378 | // use previous block's result as next block's MV predictor. |
| 5379 | #if !CONFIG_REF_MV |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5380 | if (index > 0) { |
| 5381 | bsi->mvp.as_int = mi->bmi[index - 1].as_mv[0].as_int; |
| 5382 | if (index == 2) |
| 5383 | bsi->mvp.as_int = mi->bmi[index - 2].as_mv[0].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5384 | } |
| 5385 | #endif |
| 5386 | #endif // CONFIG_EXT_INTER |
| 5387 | } |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5388 | max_mv = (index == 0) ? (int)x->max_mv_context[mbmi->ref_frame[0]] |
| 5389 | : AOMMAX(abs(bsi->mvp.as_mv.row), |
| 5390 | abs(bsi->mvp.as_mv.col)) >> |
| 5391 | 3; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5392 | |
| 5393 | if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) { |
| 5394 | // Take wtd average of the step_params based on the last frame's |
| 5395 | // max mv magnitude and the best ref mvs of the current block for |
| 5396 | // the given reference. |
| 5397 | step_param = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5398 | (av1_init_search_range(max_mv) + cpi->mv_step_param) / 2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5399 | } else { |
| 5400 | step_param = cpi->mv_step_param; |
| 5401 | } |
| 5402 | |
| 5403 | #if CONFIG_REF_MV |
| 5404 | mvp_full.row = bsi->ref_mv[0]->as_mv.row >> 3; |
| 5405 | mvp_full.col = bsi->ref_mv[0]->as_mv.col >> 3; |
| 5406 | #else |
| 5407 | mvp_full.row = bsi->mvp.as_mv.row >> 3; |
| 5408 | mvp_full.col = bsi->mvp.as_mv.col >> 3; |
| 5409 | #endif |
| 5410 | |
| 5411 | if (cpi->sf.adaptive_motion_search) { |
| 5412 | mvp_full.row = x->pred_mv[mbmi->ref_frame[0]].row >> 3; |
| 5413 | mvp_full.col = x->pred_mv[mbmi->ref_frame[0]].col >> 3; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5414 | step_param = AOMMAX(step_param, 8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | // adjust src pointer for this block |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5418 | mi_buf_shift(x, index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5419 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5420 | av1_set_mv_search_range(x, &bsi->ref_mv[0]->as_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5421 | |
| 5422 | x->best_mv.as_int = x->second_best_mv.as_int = INVALID_MV; |
| 5423 | |
| 5424 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 5425 | av1_set_mvcost(x, mbmi->ref_frame[0], 0, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5426 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5427 | bestsme = av1_full_pixel_search( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5428 | cpi, x, bsize, &mvp_full, step_param, sadpb, |
| 5429 | cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL, |
| 5430 | &bsi->ref_mv[0]->as_mv, INT_MAX, 1); |
| 5431 | |
| 5432 | x->mv_col_min = tmp_col_min; |
| 5433 | x->mv_col_max = tmp_col_max; |
| 5434 | x->mv_row_min = tmp_row_min; |
| 5435 | x->mv_row_max = tmp_row_max; |
| 5436 | |
| 5437 | if (bestsme < INT_MAX) { |
| 5438 | int distortion; |
| 5439 | if (cpi->sf.use_upsampled_references) { |
| 5440 | int best_mv_var; |
| 5441 | const int try_second = |
| 5442 | x->second_best_mv.as_int != INVALID_MV && |
| 5443 | x->second_best_mv.as_int != x->best_mv.as_int; |
| 5444 | const int pw = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 5445 | const int ph = 4 * num_4x4_blocks_high_lookup[bsize]; |
| 5446 | // Use up-sampled reference frames. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5447 | struct buf_2d backup_pred = pd->pre[0]; |
| 5448 | const YV12_BUFFER_CONFIG *upsampled_ref = |
| 5449 | get_upsampled_ref(cpi, mbmi->ref_frame[0]); |
| 5450 | |
| 5451 | // Set pred for Y plane |
| 5452 | setup_pred_plane( |
| 5453 | &pd->pre[0], upsampled_ref->y_buffer, |
| 5454 | upsampled_ref->y_crop_width, upsampled_ref->y_crop_height, |
| 5455 | upsampled_ref->y_stride, (mi_row << 3), (mi_col << 3), NULL, |
| 5456 | pd->subsampling_x, pd->subsampling_y); |
| 5457 | |
| 5458 | // adjust pred pointer for this block |
| 5459 | pd->pre[0].buf = |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5460 | &pd->pre[0].buf[(av1_raster_block_offset(BLOCK_8X8, index, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5461 | pd->pre[0].stride)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5462 | << 3]; |
| 5463 | |
| 5464 | best_mv_var = cpi->find_fractional_mv_step( |
| 5465 | x, &bsi->ref_mv[0]->as_mv, cm->allow_high_precision_mv, |
| 5466 | x->errorperbit, &cpi->fn_ptr[bsize], |
| 5467 | cpi->sf.mv.subpel_force_stop, |
| 5468 | cpi->sf.mv.subpel_iters_per_step, |
| 5469 | cond_cost_list(cpi, cost_list), x->nmvjointcost, x->mvcost, |
| 5470 | &distortion, &x->pred_sse[mbmi->ref_frame[0]], NULL, pw, ph, |
| 5471 | 1); |
| 5472 | |
| 5473 | if (try_second) { |
| 5474 | int this_var; |
| 5475 | MV best_mv = x->best_mv.as_mv; |
| 5476 | const MV ref_mv = bsi->ref_mv[0]->as_mv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5477 | const int minc = AOMMAX(x->mv_col_min * 8, ref_mv.col - MV_MAX); |
| 5478 | const int maxc = AOMMIN(x->mv_col_max * 8, ref_mv.col + MV_MAX); |
| 5479 | const int minr = AOMMAX(x->mv_row_min * 8, ref_mv.row - MV_MAX); |
| 5480 | const int maxr = AOMMIN(x->mv_row_max * 8, ref_mv.row + MV_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5481 | |
| 5482 | x->best_mv = x->second_best_mv; |
| 5483 | if (x->best_mv.as_mv.row * 8 <= maxr && |
| 5484 | x->best_mv.as_mv.row * 8 >= minr && |
| 5485 | x->best_mv.as_mv.col * 8 <= maxc && |
| 5486 | x->best_mv.as_mv.col * 8 >= minc) { |
| 5487 | this_var = cpi->find_fractional_mv_step( |
| 5488 | x, &bsi->ref_mv[0]->as_mv, cm->allow_high_precision_mv, |
| 5489 | x->errorperbit, &cpi->fn_ptr[bsize], |
| 5490 | cpi->sf.mv.subpel_force_stop, |
| 5491 | cpi->sf.mv.subpel_iters_per_step, |
| 5492 | cond_cost_list(cpi, cost_list), x->nmvjointcost, |
| 5493 | x->mvcost, &distortion, &x->pred_sse[mbmi->ref_frame[0]], |
| 5494 | NULL, pw, ph, 1); |
| 5495 | if (this_var < best_mv_var) best_mv = x->best_mv.as_mv; |
| 5496 | x->best_mv.as_mv = best_mv; |
| 5497 | } |
| 5498 | } |
| 5499 | |
| 5500 | // Restore the reference frames. |
| 5501 | pd->pre[0] = backup_pred; |
| 5502 | } else { |
| 5503 | cpi->find_fractional_mv_step( |
| 5504 | x, &bsi->ref_mv[0]->as_mv, cm->allow_high_precision_mv, |
| 5505 | x->errorperbit, &cpi->fn_ptr[bsize], |
| 5506 | cpi->sf.mv.subpel_force_stop, |
| 5507 | cpi->sf.mv.subpel_iters_per_step, |
| 5508 | cond_cost_list(cpi, cost_list), x->nmvjointcost, x->mvcost, |
| 5509 | &distortion, &x->pred_sse[mbmi->ref_frame[0]], NULL, 0, 0, 0); |
| 5510 | } |
| 5511 | |
| 5512 | // save motion search result for use in compound prediction |
| 5513 | #if CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5514 | seg_mvs[index][mv_idx][mbmi->ref_frame[0]].as_mv = x->best_mv.as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5515 | #else |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5516 | seg_mvs[index][mbmi->ref_frame[0]].as_mv = x->best_mv.as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5517 | #endif // CONFIG_EXT_INTER |
| 5518 | } |
| 5519 | |
| 5520 | if (cpi->sf.adaptive_motion_search) |
| 5521 | x->pred_mv[mbmi->ref_frame[0]] = x->best_mv.as_mv; |
| 5522 | |
| 5523 | #if CONFIG_EXT_INTER |
| 5524 | mode_mv[this_mode][0] = x->best_mv; |
| 5525 | #else |
| 5526 | mode_mv[NEWMV][0] = x->best_mv; |
| 5527 | #endif // CONFIG_EXT_INTER |
| 5528 | |
| 5529 | // restore src pointers |
| 5530 | mi_buf_restore(x, orig_src, orig_pre); |
| 5531 | } |
| 5532 | |
| 5533 | if (has_second_rf) { |
| 5534 | #if CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5535 | if (seg_mvs[index][mv_idx][mbmi->ref_frame[1]].as_int == INVALID_MV || |
| 5536 | seg_mvs[index][mv_idx][mbmi->ref_frame[0]].as_int == INVALID_MV) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5537 | #else |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5538 | if (seg_mvs[index][mbmi->ref_frame[1]].as_int == INVALID_MV || |
| 5539 | seg_mvs[index][mbmi->ref_frame[0]].as_int == INVALID_MV) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5540 | #endif // CONFIG_EXT_INTER |
| 5541 | continue; |
| 5542 | } |
| 5543 | |
| 5544 | #if CONFIG_DUAL_FILTER |
| 5545 | (void)run_mv_search; |
| 5546 | #endif |
| 5547 | |
| 5548 | if (has_second_rf && |
| 5549 | #if CONFIG_EXT_INTER |
| 5550 | this_mode == NEW_NEWMV && |
| 5551 | #else |
| 5552 | this_mode == NEWMV && |
| 5553 | #endif // CONFIG_EXT_INTER |
| 5554 | #if CONFIG_DUAL_FILTER |
| 5555 | (mbmi->interp_filter[0] == EIGHTTAP_REGULAR || run_mv_search)) |
| 5556 | #else |
| 5557 | (mbmi->interp_filter == EIGHTTAP_REGULAR || run_mv_search)) |
| 5558 | #endif |
| 5559 | { |
| 5560 | // adjust src pointers |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5561 | mi_buf_shift(x, index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5562 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 5563 | int rate_mv; |
| 5564 | joint_motion_search(cpi, x, bsize, frame_mv[this_mode], mi_row, |
| 5565 | mi_col, |
| 5566 | #if CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5567 | bsi->ref_mv, seg_mvs[index][mv_idx], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5568 | #else |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5569 | seg_mvs[index], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5570 | #endif // CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5571 | &rate_mv, index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5572 | #if CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5573 | compound_seg_newmvs[index][0].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5574 | frame_mv[this_mode][mbmi->ref_frame[0]].as_int; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5575 | compound_seg_newmvs[index][1].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5576 | frame_mv[this_mode][mbmi->ref_frame[1]].as_int; |
| 5577 | #else |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5578 | seg_mvs[index][mbmi->ref_frame[0]].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5579 | frame_mv[this_mode][mbmi->ref_frame[0]].as_int; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5580 | seg_mvs[index][mbmi->ref_frame[1]].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5581 | frame_mv[this_mode][mbmi->ref_frame[1]].as_int; |
| 5582 | #endif // CONFIG_EXT_INTER |
| 5583 | } |
| 5584 | // restore src pointers |
| 5585 | mi_buf_restore(x, orig_src, orig_pre); |
| 5586 | } |
| 5587 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5588 | bsi->rdstat[index][mode_idx].brate = set_and_cost_bmi_mvs( |
| 5589 | cpi, x, xd, index, this_mode, mode_mv[this_mode], frame_mv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5590 | #if CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5591 | seg_mvs[index][mv_idx], compound_seg_newmvs[index], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5592 | #else |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5593 | seg_mvs[index], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5594 | #endif // CONFIG_EXT_INTER |
| 5595 | bsi->ref_mv, x->nmvjointcost, x->mvcost); |
| 5596 | |
| 5597 | for (ref = 0; ref < 1 + has_second_rf; ++ref) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5598 | bsi->rdstat[index][mode_idx].mvs[ref].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5599 | mode_mv[this_mode][ref].as_int; |
| 5600 | if (num_4x4_blocks_wide > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5601 | bsi->rdstat[index + 1][mode_idx].mvs[ref].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5602 | mode_mv[this_mode][ref].as_int; |
| 5603 | if (num_4x4_blocks_high > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5604 | bsi->rdstat[index + 2][mode_idx].mvs[ref].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5605 | mode_mv[this_mode][ref].as_int; |
| 5606 | #if CONFIG_REF_MV |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5607 | bsi->rdstat[index][mode_idx].pred_mv[ref].as_int = |
| 5608 | mi->bmi[index].pred_mv[ref].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5609 | if (num_4x4_blocks_wide > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5610 | bsi->rdstat[index + 1][mode_idx].pred_mv[ref].as_int = |
| 5611 | mi->bmi[index].pred_mv[ref].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5612 | if (num_4x4_blocks_high > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5613 | bsi->rdstat[index + 2][mode_idx].pred_mv[ref].as_int = |
| 5614 | mi->bmi[index].pred_mv[ref].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5615 | #endif |
| 5616 | #if CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5617 | bsi->rdstat[index][mode_idx].ref_mv[ref].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5618 | bsi->ref_mv[ref]->as_int; |
| 5619 | if (num_4x4_blocks_wide > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5620 | bsi->rdstat[index + 1][mode_idx].ref_mv[ref].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5621 | bsi->ref_mv[ref]->as_int; |
| 5622 | if (num_4x4_blocks_high > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5623 | bsi->rdstat[index + 2][mode_idx].ref_mv[ref].as_int = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5624 | bsi->ref_mv[ref]->as_int; |
| 5625 | #endif // CONFIG_EXT_INTER |
| 5626 | } |
| 5627 | |
| 5628 | // Trap vectors that reach beyond the UMV borders |
| 5629 | if (mv_check_bounds(x, &mode_mv[this_mode][0].as_mv) || |
| 5630 | (has_second_rf && mv_check_bounds(x, &mode_mv[this_mode][1].as_mv))) |
| 5631 | continue; |
| 5632 | |
| 5633 | if (filter_idx > 0) { |
| 5634 | BEST_SEG_INFO *ref_bsi = bsi_buf; |
| 5635 | subpelmv = 0; |
| 5636 | have_ref = 1; |
| 5637 | |
| 5638 | for (ref = 0; ref < 1 + has_second_rf; ++ref) { |
| 5639 | subpelmv |= mv_has_subpel(&mode_mv[this_mode][ref].as_mv); |
| 5640 | #if CONFIG_EXT_INTER |
| 5641 | if (have_newmv_in_inter_mode(this_mode)) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5642 | have_ref &= |
| 5643 | ((mode_mv[this_mode][ref].as_int == |
| 5644 | ref_bsi->rdstat[index][mode_idx].mvs[ref].as_int) && |
| 5645 | (bsi->ref_mv[ref]->as_int == |
| 5646 | ref_bsi->rdstat[index][mode_idx].ref_mv[ref].as_int)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5647 | else |
| 5648 | #endif // CONFIG_EXT_INTER |
| 5649 | have_ref &= mode_mv[this_mode][ref].as_int == |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5650 | ref_bsi->rdstat[index][mode_idx].mvs[ref].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5651 | } |
| 5652 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5653 | have_ref &= ref_bsi->rdstat[index][mode_idx].brate > 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5654 | |
| 5655 | if (filter_idx > 1 && !subpelmv && !have_ref) { |
| 5656 | ref_bsi = bsi_buf + 1; |
| 5657 | have_ref = 1; |
| 5658 | for (ref = 0; ref < 1 + has_second_rf; ++ref) |
| 5659 | #if CONFIG_EXT_INTER |
| 5660 | if (have_newmv_in_inter_mode(this_mode)) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5661 | have_ref &= |
| 5662 | ((mode_mv[this_mode][ref].as_int == |
| 5663 | ref_bsi->rdstat[index][mode_idx].mvs[ref].as_int) && |
| 5664 | (bsi->ref_mv[ref]->as_int == |
| 5665 | ref_bsi->rdstat[index][mode_idx].ref_mv[ref].as_int)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5666 | else |
| 5667 | #endif // CONFIG_EXT_INTER |
| 5668 | have_ref &= mode_mv[this_mode][ref].as_int == |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5669 | ref_bsi->rdstat[index][mode_idx].mvs[ref].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5670 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5671 | have_ref &= ref_bsi->rdstat[index][mode_idx].brate > 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5672 | } |
| 5673 | |
| 5674 | if (!subpelmv && have_ref && |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5675 | ref_bsi->rdstat[index][mode_idx].brdcost < INT64_MAX) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5676 | #if CONFIG_REF_MV |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5677 | bsi->rdstat[index][mode_idx].byrate = |
| 5678 | ref_bsi->rdstat[index][mode_idx].byrate; |
| 5679 | bsi->rdstat[index][mode_idx].bdist = |
| 5680 | ref_bsi->rdstat[index][mode_idx].bdist; |
| 5681 | bsi->rdstat[index][mode_idx].bsse = |
| 5682 | ref_bsi->rdstat[index][mode_idx].bsse; |
| 5683 | bsi->rdstat[index][mode_idx].brate += |
| 5684 | ref_bsi->rdstat[index][mode_idx].byrate; |
| 5685 | bsi->rdstat[index][mode_idx].eobs = |
| 5686 | ref_bsi->rdstat[index][mode_idx].eobs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5687 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5688 | bsi->rdstat[index][mode_idx].brdcost = |
| 5689 | RDCOST(x->rdmult, x->rddiv, bsi->rdstat[index][mode_idx].brate, |
| 5690 | bsi->rdstat[index][mode_idx].bdist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5691 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5692 | memcpy(bsi->rdstat[index][mode_idx].ta, |
| 5693 | ref_bsi->rdstat[index][mode_idx].ta, |
| 5694 | sizeof(bsi->rdstat[index][mode_idx].ta)); |
| 5695 | memcpy(bsi->rdstat[index][mode_idx].tl, |
| 5696 | ref_bsi->rdstat[index][mode_idx].tl, |
| 5697 | sizeof(bsi->rdstat[index][mode_idx].tl)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5698 | #else |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5699 | memcpy(&bsi->rdstat[index][mode_idx], |
| 5700 | &ref_bsi->rdstat[index][mode_idx], sizeof(SEG_RDSTAT)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5701 | #endif |
| 5702 | if (num_4x4_blocks_wide > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5703 | bsi->rdstat[index + 1][mode_idx].eobs = |
| 5704 | ref_bsi->rdstat[index + 1][mode_idx].eobs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5705 | if (num_4x4_blocks_high > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5706 | bsi->rdstat[index + 2][mode_idx].eobs = |
| 5707 | ref_bsi->rdstat[index + 2][mode_idx].eobs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5708 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5709 | if (bsi->rdstat[index][mode_idx].brdcost < new_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5710 | #if CONFIG_REF_MV |
| 5711 | // If the NEWMV mode is using the same motion vector as the |
| 5712 | // NEARESTMV mode, skip the rest rate-distortion calculations |
| 5713 | // and use the inferred motion vector modes. |
| 5714 | if (this_mode == NEWMV) { |
| 5715 | if (has_second_rf) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5716 | if (bsi->rdstat[index][mode_idx].mvs[0].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5717 | bsi->ref_mv[0]->as_int && |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5718 | bsi->rdstat[index][mode_idx].mvs[1].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5719 | bsi->ref_mv[1]->as_int) |
| 5720 | continue; |
| 5721 | } else { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5722 | if (bsi->rdstat[index][mode_idx].mvs[0].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5723 | bsi->ref_mv[0]->as_int) |
| 5724 | continue; |
| 5725 | } |
| 5726 | } |
| 5727 | #endif |
| 5728 | mode_selected = this_mode; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5729 | new_best_rd = bsi->rdstat[index][mode_idx].brdcost; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5730 | #if CONFIG_PVQ |
| 5731 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 5732 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5733 | } |
| 5734 | continue; |
| 5735 | } |
| 5736 | } |
| 5737 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5738 | bsi->rdstat[index][mode_idx].brdcost = encode_inter_mb_segment( |
| 5739 | cpi, x, bsi->segment_rd - this_segment_rd, index, |
| 5740 | &bsi->rdstat[index][mode_idx].byrate, |
| 5741 | &bsi->rdstat[index][mode_idx].bdist, |
| 5742 | &bsi->rdstat[index][mode_idx].bsse, bsi->rdstat[index][mode_idx].ta, |
| 5743 | bsi->rdstat[index][mode_idx].tl, idy, idx, mi_row, mi_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5744 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5745 | if (bsi->rdstat[index][mode_idx].brdcost < INT64_MAX) { |
| 5746 | bsi->rdstat[index][mode_idx].brdcost += RDCOST( |
| 5747 | x->rdmult, x->rddiv, bsi->rdstat[index][mode_idx].brate, 0); |
| 5748 | bsi->rdstat[index][mode_idx].brate += |
| 5749 | bsi->rdstat[index][mode_idx].byrate; |
| 5750 | bsi->rdstat[index][mode_idx].eobs = p->eobs[index]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5751 | if (num_4x4_blocks_wide > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5752 | bsi->rdstat[index + 1][mode_idx].eobs = p->eobs[index + 1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5753 | if (num_4x4_blocks_high > 1) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5754 | bsi->rdstat[index + 2][mode_idx].eobs = p->eobs[index + 2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5755 | } |
| 5756 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5757 | if (bsi->rdstat[index][mode_idx].brdcost < new_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5758 | #if CONFIG_REF_MV |
| 5759 | // If the NEWMV mode is using the same motion vector as the |
| 5760 | // NEARESTMV mode, skip the rest rate-distortion calculations |
| 5761 | // and use the inferred motion vector modes. |
| 5762 | if (this_mode == NEWMV) { |
| 5763 | if (has_second_rf) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5764 | if (bsi->rdstat[index][mode_idx].mvs[0].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5765 | bsi->ref_mv[0]->as_int && |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5766 | bsi->rdstat[index][mode_idx].mvs[1].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5767 | bsi->ref_mv[1]->as_int) |
| 5768 | continue; |
| 5769 | } else { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5770 | if (bsi->rdstat[index][mode_idx].mvs[0].as_int == |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5771 | bsi->ref_mv[0]->as_int) |
| 5772 | continue; |
| 5773 | } |
| 5774 | } |
| 5775 | #endif |
| 5776 | mode_selected = this_mode; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5777 | new_best_rd = bsi->rdstat[index][mode_idx].brdcost; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5778 | |
| 5779 | #if CONFIG_PVQ |
| 5780 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 5781 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5782 | } |
| 5783 | } /*for each 4x4 mode*/ |
| 5784 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5785 | if (new_best_rd == INT64_MAX) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5786 | int iy, midx; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5787 | for (iy = index + 1; iy < 4; ++iy) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5788 | #if CONFIG_EXT_INTER |
| 5789 | for (midx = 0; midx < INTER_MODES + INTER_COMPOUND_MODES; ++midx) |
| 5790 | #else |
| 5791 | for (midx = 0; midx < INTER_MODES; ++midx) |
| 5792 | #endif // CONFIG_EXT_INTER |
| 5793 | bsi->rdstat[iy][midx].brdcost = INT64_MAX; |
| 5794 | bsi->segment_rd = INT64_MAX; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5795 | #if CONFIG_PVQ |
| 5796 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 5797 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5798 | return INT64_MAX; |
| 5799 | } |
| 5800 | |
| 5801 | mode_idx = INTER_OFFSET(mode_selected); |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5802 | memcpy(t_above, bsi->rdstat[index][mode_idx].ta, sizeof(t_above)); |
| 5803 | memcpy(t_left, bsi->rdstat[index][mode_idx].tl, sizeof(t_left)); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5804 | #if CONFIG_PVQ |
| 5805 | od_encode_rollback(&x->daala_enc, &post_buf); |
| 5806 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5807 | |
| 5808 | #if CONFIG_EXT_INTER |
| 5809 | mv_idx = (mode_selected == NEWFROMNEARMV) ? 1 : 0; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5810 | bsi->ref_mv[0]->as_int = bsi->rdstat[index][mode_idx].ref_mv[0].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5811 | if (has_second_rf) |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5812 | bsi->ref_mv[1]->as_int = bsi->rdstat[index][mode_idx].ref_mv[1].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5813 | #endif // CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5814 | set_and_cost_bmi_mvs(cpi, x, xd, index, mode_selected, |
| 5815 | mode_mv[mode_selected], frame_mv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5816 | #if CONFIG_EXT_INTER |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5817 | seg_mvs[index][mv_idx], compound_seg_newmvs[index], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5818 | #else |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5819 | seg_mvs[index], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5820 | #endif // CONFIG_EXT_INTER |
| 5821 | bsi->ref_mv, x->nmvjointcost, x->mvcost); |
| 5822 | |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5823 | br += bsi->rdstat[index][mode_idx].brate; |
| 5824 | bd += bsi->rdstat[index][mode_idx].bdist; |
| 5825 | block_sse += bsi->rdstat[index][mode_idx].bsse; |
| 5826 | segmentyrate += bsi->rdstat[index][mode_idx].byrate; |
| 5827 | this_segment_rd += bsi->rdstat[index][mode_idx].brdcost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5828 | |
| 5829 | if (this_segment_rd > bsi->segment_rd) { |
| 5830 | int iy, midx; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 5831 | for (iy = index + 1; iy < 4; ++iy) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5832 | #if CONFIG_EXT_INTER |
| 5833 | for (midx = 0; midx < INTER_MODES + INTER_COMPOUND_MODES; ++midx) |
| 5834 | #else |
| 5835 | for (midx = 0; midx < INTER_MODES; ++midx) |
| 5836 | #endif // CONFIG_EXT_INTER |
| 5837 | bsi->rdstat[iy][midx].brdcost = INT64_MAX; |
| 5838 | bsi->segment_rd = INT64_MAX; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5839 | #if CONFIG_PVQ |
| 5840 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 5841 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5842 | return INT64_MAX; |
| 5843 | } |
| 5844 | } |
| 5845 | } /* for each label */ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5846 | #if CONFIG_PVQ |
| 5847 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 5848 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5849 | |
| 5850 | bsi->r = br; |
| 5851 | bsi->d = bd; |
| 5852 | bsi->segment_yrate = segmentyrate; |
| 5853 | bsi->segment_rd = this_segment_rd; |
| 5854 | bsi->sse = block_sse; |
| 5855 | |
| 5856 | // update the coding decisions |
| 5857 | for (k = 0; k < 4; ++k) bsi->modes[k] = mi->bmi[k].as_mode; |
| 5858 | |
| 5859 | if (bsi->segment_rd > best_rd) return INT64_MAX; |
| 5860 | /* set it to the best */ |
| 5861 | for (idx = 0; idx < 4; idx++) { |
| 5862 | mode_idx = INTER_OFFSET(bsi->modes[idx]); |
| 5863 | mi->bmi[idx].as_mv[0].as_int = bsi->rdstat[idx][mode_idx].mvs[0].as_int; |
| 5864 | if (has_second_ref(mbmi)) |
| 5865 | mi->bmi[idx].as_mv[1].as_int = bsi->rdstat[idx][mode_idx].mvs[1].as_int; |
| 5866 | #if CONFIG_REF_MV |
Yaowu Xu | f5bbbfa | 2016-09-26 09:13:38 -0700 | [diff] [blame] | 5867 | mi->bmi[idx].pred_mv[0] = bsi->rdstat[idx][mode_idx].pred_mv[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5868 | if (has_second_ref(mbmi)) |
Yaowu Xu | f5bbbfa | 2016-09-26 09:13:38 -0700 | [diff] [blame] | 5869 | mi->bmi[idx].pred_mv[1] = bsi->rdstat[idx][mode_idx].pred_mv[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5870 | #endif |
| 5871 | #if CONFIG_EXT_INTER |
| 5872 | mi->bmi[idx].ref_mv[0].as_int = bsi->rdstat[idx][mode_idx].ref_mv[0].as_int; |
| 5873 | if (has_second_rf) |
| 5874 | mi->bmi[idx].ref_mv[1].as_int = |
| 5875 | bsi->rdstat[idx][mode_idx].ref_mv[1].as_int; |
| 5876 | #endif // CONFIG_EXT_INTER |
| 5877 | x->plane[0].eobs[idx] = bsi->rdstat[idx][mode_idx].eobs; |
| 5878 | mi->bmi[idx].as_mode = bsi->modes[idx]; |
| 5879 | } |
| 5880 | |
| 5881 | /* |
| 5882 | * used to set mbmi->mv.as_int |
| 5883 | */ |
| 5884 | *returntotrate = bsi->r; |
| 5885 | *returndistortion = bsi->d; |
| 5886 | *returnyrate = bsi->segment_yrate; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5887 | *skippable = av1_is_skippable_in_plane(x, BLOCK_8X8, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5888 | *psse = bsi->sse; |
| 5889 | mbmi->mode = bsi->modes[3]; |
| 5890 | |
| 5891 | return bsi->segment_rd; |
| 5892 | } |
| 5893 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5894 | static void estimate_ref_frame_costs(const AV1_COMMON *cm, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5895 | const MACROBLOCKD *xd, int segment_id, |
| 5896 | unsigned int *ref_costs_single, |
| 5897 | unsigned int *ref_costs_comp, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5898 | aom_prob *comp_mode_p) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5899 | int seg_ref_active = |
| 5900 | segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME); |
| 5901 | if (seg_ref_active) { |
| 5902 | memset(ref_costs_single, 0, |
| 5903 | TOTAL_REFS_PER_FRAME * sizeof(*ref_costs_single)); |
| 5904 | memset(ref_costs_comp, 0, TOTAL_REFS_PER_FRAME * sizeof(*ref_costs_comp)); |
| 5905 | *comp_mode_p = 128; |
| 5906 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5907 | aom_prob intra_inter_p = av1_get_intra_inter_prob(cm, xd); |
| 5908 | aom_prob comp_inter_p = 128; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5909 | |
| 5910 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5911 | comp_inter_p = av1_get_reference_mode_prob(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5912 | *comp_mode_p = comp_inter_p; |
| 5913 | } else { |
| 5914 | *comp_mode_p = 128; |
| 5915 | } |
| 5916 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5917 | ref_costs_single[INTRA_FRAME] = av1_cost_bit(intra_inter_p, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5918 | |
| 5919 | if (cm->reference_mode != COMPOUND_REFERENCE) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5920 | aom_prob ref_single_p1 = av1_get_pred_prob_single_ref_p1(cm, xd); |
| 5921 | aom_prob ref_single_p2 = av1_get_pred_prob_single_ref_p2(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5922 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5923 | aom_prob ref_single_p3 = av1_get_pred_prob_single_ref_p3(cm, xd); |
| 5924 | aom_prob ref_single_p4 = av1_get_pred_prob_single_ref_p4(cm, xd); |
| 5925 | aom_prob ref_single_p5 = av1_get_pred_prob_single_ref_p5(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5926 | #endif // CONFIG_EXT_REFS |
| 5927 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5928 | unsigned int base_cost = av1_cost_bit(intra_inter_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5929 | |
| 5930 | ref_costs_single[LAST_FRAME] = |
| 5931 | #if CONFIG_EXT_REFS |
| 5932 | ref_costs_single[LAST2_FRAME] = ref_costs_single[LAST3_FRAME] = |
| 5933 | ref_costs_single[BWDREF_FRAME] = |
| 5934 | #endif // CONFIG_EXT_REFS |
| 5935 | ref_costs_single[GOLDEN_FRAME] = |
| 5936 | ref_costs_single[ALTREF_FRAME] = base_cost; |
| 5937 | |
| 5938 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5939 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 5940 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 5941 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 5942 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 5943 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
| 5944 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5945 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5946 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p3, 0); |
| 5947 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p3, 0); |
| 5948 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p3, 1); |
| 5949 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p3, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5950 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5951 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p2, 0); |
| 5952 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5953 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5954 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p4, 0); |
| 5955 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p4, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5956 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5957 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p5, 0); |
| 5958 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p5, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5959 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5960 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 5961 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 1); |
| 5962 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5963 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5964 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p2, 0); |
| 5965 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5966 | #endif // CONFIG_EXT_REFS |
| 5967 | } else { |
| 5968 | ref_costs_single[LAST_FRAME] = 512; |
| 5969 | #if CONFIG_EXT_REFS |
| 5970 | ref_costs_single[LAST2_FRAME] = 512; |
| 5971 | ref_costs_single[LAST3_FRAME] = 512; |
| 5972 | ref_costs_single[BWDREF_FRAME] = 512; |
| 5973 | #endif // CONFIG_EXT_REFS |
| 5974 | ref_costs_single[GOLDEN_FRAME] = 512; |
| 5975 | ref_costs_single[ALTREF_FRAME] = 512; |
| 5976 | } |
| 5977 | |
| 5978 | if (cm->reference_mode != SINGLE_REFERENCE) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5979 | aom_prob ref_comp_p = av1_get_pred_prob_comp_ref_p(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5980 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5981 | aom_prob ref_comp_p1 = av1_get_pred_prob_comp_ref_p1(cm, xd); |
| 5982 | aom_prob ref_comp_p2 = av1_get_pred_prob_comp_ref_p2(cm, xd); |
| 5983 | aom_prob bwdref_comp_p = av1_get_pred_prob_comp_bwdref_p(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5984 | #endif // CONFIG_EXT_REFS |
| 5985 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5986 | unsigned int base_cost = av1_cost_bit(intra_inter_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5987 | |
| 5988 | ref_costs_comp[LAST_FRAME] = |
| 5989 | #if CONFIG_EXT_REFS |
| 5990 | ref_costs_comp[LAST2_FRAME] = ref_costs_comp[LAST3_FRAME] = |
| 5991 | #endif // CONFIG_EXT_REFS |
| 5992 | ref_costs_comp[GOLDEN_FRAME] = base_cost; |
| 5993 | |
| 5994 | #if CONFIG_EXT_REFS |
| 5995 | ref_costs_comp[BWDREF_FRAME] = ref_costs_comp[ALTREF_FRAME] = 0; |
| 5996 | #endif // CONFIG_EXT_REFS |
| 5997 | |
| 5998 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5999 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6000 | ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6001 | ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6002 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6003 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6004 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1); |
| 6005 | ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6006 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6007 | ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0); |
| 6008 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6009 | |
| 6010 | // NOTE(zoeliu): BWDREF and ALTREF each add an extra cost by coding 1 |
| 6011 | // more bit. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6012 | ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
| 6013 | ref_costs_comp[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6014 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6015 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6016 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6017 | #endif // CONFIG_EXT_REFS |
| 6018 | } else { |
| 6019 | ref_costs_comp[LAST_FRAME] = 512; |
| 6020 | #if CONFIG_EXT_REFS |
| 6021 | ref_costs_comp[LAST2_FRAME] = 512; |
| 6022 | ref_costs_comp[LAST3_FRAME] = 512; |
| 6023 | ref_costs_comp[BWDREF_FRAME] = 512; |
| 6024 | ref_costs_comp[ALTREF_FRAME] = 512; |
| 6025 | #endif // CONFIG_EXT_REFS |
| 6026 | ref_costs_comp[GOLDEN_FRAME] = 512; |
| 6027 | } |
| 6028 | } |
| 6029 | } |
| 6030 | |
| 6031 | static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, |
| 6032 | int mode_index, |
| 6033 | int64_t comp_pred_diff[REFERENCE_MODES], |
| 6034 | int skippable) { |
| 6035 | MACROBLOCKD *const xd = &x->e_mbd; |
| 6036 | |
| 6037 | // Take a snapshot of the coding context so it can be |
| 6038 | // restored if we decide to encode this way |
| 6039 | ctx->skip = x->skip; |
| 6040 | ctx->skippable = skippable; |
| 6041 | ctx->best_mode_index = mode_index; |
| 6042 | ctx->mic = *xd->mi[0]; |
| 6043 | ctx->mbmi_ext = *x->mbmi_ext; |
| 6044 | ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE]; |
| 6045 | ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE]; |
| 6046 | ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT]; |
| 6047 | } |
| 6048 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6049 | static void setup_buffer_inter(const AV1_COMP *const cpi, MACROBLOCK *x, |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 6050 | MV_REFERENCE_FRAME ref_frame, |
| 6051 | BLOCK_SIZE block_size, int mi_row, int mi_col, |
| 6052 | int_mv frame_nearest_mv[TOTAL_REFS_PER_FRAME], |
| 6053 | int_mv frame_near_mv[TOTAL_REFS_PER_FRAME], |
| 6054 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME] |
| 6055 | [MAX_MB_PLANE]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6056 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6057 | const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame); |
| 6058 | MACROBLOCKD *const xd = &x->e_mbd; |
| 6059 | MODE_INFO *const mi = xd->mi[0]; |
| 6060 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 6061 | const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf; |
| 6062 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 6063 | |
| 6064 | assert(yv12 != NULL); |
| 6065 | |
| 6066 | // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this |
| 6067 | // use the UV scaling factors. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6068 | av1_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6069 | |
| 6070 | // Gets an initial list of candidate vectors from neighbours and orders them |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6071 | av1_find_mv_refs( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6072 | cm, xd, mi, ref_frame, |
| 6073 | #if CONFIG_REF_MV |
| 6074 | &mbmi_ext->ref_mv_count[ref_frame], mbmi_ext->ref_mv_stack[ref_frame], |
| 6075 | #if CONFIG_EXT_INTER |
| 6076 | mbmi_ext->compound_mode_context, |
| 6077 | #endif // CONFIG_EXT_INTER |
| 6078 | #endif |
| 6079 | candidates, mi_row, mi_col, NULL, NULL, mbmi_ext->mode_context); |
| 6080 | |
| 6081 | // Candidate refinement carried out at encoder and decoder |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6082 | av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates, |
| 6083 | &frame_nearest_mv[ref_frame], |
| 6084 | &frame_near_mv[ref_frame]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6085 | |
| 6086 | // Further refinement that is encode side only to test the top few candidates |
| 6087 | // in full and choose the best as the centre point for subsequent searches. |
| 6088 | // The current implementation doesn't support scaling. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6089 | if (!av1_is_scaled(sf) && block_size >= BLOCK_8X8) |
| 6090 | av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame, |
| 6091 | block_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6092 | } |
| 6093 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6094 | static void single_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 6095 | BLOCK_SIZE bsize, int mi_row, int mi_col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6096 | #if CONFIG_EXT_INTER |
| 6097 | int ref_idx, int mv_idx, |
| 6098 | #endif // CONFIG_EXT_INTER |
| 6099 | int *rate_mv) { |
| 6100 | MACROBLOCKD *xd = &x->e_mbd; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6101 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6102 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6103 | struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } }; |
| 6104 | int bestsme = INT_MAX; |
| 6105 | int step_param; |
| 6106 | int sadpb = x->sadperbit16; |
| 6107 | MV mvp_full; |
| 6108 | #if CONFIG_EXT_INTER |
| 6109 | int ref = mbmi->ref_frame[ref_idx]; |
| 6110 | MV ref_mv = x->mbmi_ext->ref_mvs[ref][mv_idx].as_mv; |
| 6111 | #else |
| 6112 | int ref = mbmi->ref_frame[0]; |
| 6113 | MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 6114 | int ref_idx = 0; |
| 6115 | #endif // CONFIG_EXT_INTER |
| 6116 | |
| 6117 | int tmp_col_min = x->mv_col_min; |
| 6118 | int tmp_col_max = x->mv_col_max; |
| 6119 | int tmp_row_min = x->mv_row_min; |
| 6120 | int tmp_row_max = x->mv_row_max; |
| 6121 | int cost_list[5]; |
| 6122 | |
| 6123 | const YV12_BUFFER_CONFIG *scaled_ref_frame = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6124 | av1_get_scaled_ref_frame(cpi, ref); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6125 | |
| 6126 | MV pred_mv[3]; |
| 6127 | pred_mv[0] = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 6128 | pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv; |
| 6129 | pred_mv[2] = x->pred_mv[ref]; |
| 6130 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6131 | if (scaled_ref_frame) { |
| 6132 | int i; |
| 6133 | // Swap out the reference frame for a version that's been scaled to |
| 6134 | // match the resolution of the current frame, allowing the existing |
| 6135 | // motion search code to be used without additional modifications. |
| 6136 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6137 | backup_yv12[i] = xd->plane[i].pre[ref_idx]; |
| 6138 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6139 | av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6140 | } |
| 6141 | |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6142 | av1_set_mv_search_range(x, &ref_mv); |
| 6143 | |
| 6144 | #if CONFIG_REF_MV |
| 6145 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
| 6146 | #endif |
| 6147 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6148 | // Work out the size of the first step in the mv step search. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6149 | // 0 here is maximum length first step. 1 is AOMMAX >> 1 etc. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6150 | if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) { |
| 6151 | // Take wtd average of the step_params based on the last frame's |
| 6152 | // max mv magnitude and that based on the best ref mvs of the current |
| 6153 | // block for the given reference. |
| 6154 | step_param = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6155 | (av1_init_search_range(x->max_mv_context[ref]) + cpi->mv_step_param) / |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6156 | 2; |
| 6157 | } else { |
| 6158 | step_param = cpi->mv_step_param; |
| 6159 | } |
| 6160 | |
| 6161 | if (cpi->sf.adaptive_motion_search && bsize < cm->sb_size) { |
| 6162 | int boffset = |
| 6163 | 2 * (b_width_log2_lookup[cm->sb_size] - |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6164 | AOMMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize])); |
| 6165 | step_param = AOMMAX(step_param, boffset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | if (cpi->sf.adaptive_motion_search) { |
| 6169 | int bwl = b_width_log2_lookup[bsize]; |
| 6170 | int bhl = b_height_log2_lookup[bsize]; |
| 6171 | int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4); |
| 6172 | |
| 6173 | if (tlevel < 5) step_param += 2; |
| 6174 | |
| 6175 | // prev_mv_sad is not setup for dynamically scaled frames. |
| 6176 | if (cpi->oxcf.resize_mode != RESIZE_DYNAMIC) { |
| 6177 | int i; |
| 6178 | for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) { |
| 6179 | if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) { |
| 6180 | x->pred_mv[ref].row = 0; |
| 6181 | x->pred_mv[ref].col = 0; |
| 6182 | x->best_mv.as_int = INVALID_MV; |
| 6183 | |
| 6184 | if (scaled_ref_frame) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 6185 | int j; |
| 6186 | for (j = 0; j < MAX_MB_PLANE; ++j) |
| 6187 | xd->plane[j].pre[ref_idx] = backup_yv12[j]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6188 | } |
| 6189 | return; |
| 6190 | } |
| 6191 | } |
| 6192 | } |
| 6193 | } |
| 6194 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6195 | av1_set_mv_search_range(x, &ref_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6196 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6197 | #if CONFIG_MOTION_VAR |
| 6198 | if (mbmi->motion_mode != SIMPLE_TRANSLATION) |
| 6199 | mvp_full = mbmi->mv[0].as_mv; |
| 6200 | else |
| 6201 | #endif // CONFIG_MOTION_VAR |
| 6202 | mvp_full = pred_mv[x->mv_best_ref_index[ref]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6203 | |
| 6204 | mvp_full.col >>= 3; |
| 6205 | mvp_full.row >>= 3; |
| 6206 | |
| 6207 | x->best_mv.as_int = x->second_best_mv.as_int = INVALID_MV; |
| 6208 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6209 | #if CONFIG_MOTION_VAR |
| 6210 | switch (mbmi->motion_mode) { |
| 6211 | case SIMPLE_TRANSLATION: |
| 6212 | #endif // CONFIG_MOTION_VAR |
| 6213 | bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, |
| 6214 | sadpb, cond_cost_list(cpi, cost_list), |
| 6215 | &ref_mv, INT_MAX, 1); |
| 6216 | #if CONFIG_MOTION_VAR |
| 6217 | break; |
| 6218 | case OBMC_CAUSAL: |
| 6219 | bestsme = av1_obmc_full_pixel_diamond( |
| 6220 | cpi, x, &mvp_full, step_param, sadpb, |
| 6221 | MAX_MVSEARCH_STEPS - 1 - step_param, 1, &cpi->fn_ptr[bsize], &ref_mv, |
| 6222 | &(x->best_mv.as_mv), 0); |
| 6223 | break; |
| 6224 | default: assert("Invalid motion mode!\n"); |
| 6225 | } |
| 6226 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6227 | |
| 6228 | x->mv_col_min = tmp_col_min; |
| 6229 | x->mv_col_max = tmp_col_max; |
| 6230 | x->mv_row_min = tmp_row_min; |
| 6231 | x->mv_row_max = tmp_row_max; |
| 6232 | |
| 6233 | if (bestsme < INT_MAX) { |
| 6234 | int dis; /* TODO: use dis in distortion calculation later. */ |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6235 | #if CONFIG_MOTION_VAR |
| 6236 | switch (mbmi->motion_mode) { |
| 6237 | case SIMPLE_TRANSLATION: |
| 6238 | #endif // CONFIG_MOTION_VAR |
| 6239 | if (cpi->sf.use_upsampled_references) { |
| 6240 | int best_mv_var; |
| 6241 | const int try_second = x->second_best_mv.as_int != INVALID_MV && |
| 6242 | x->second_best_mv.as_int != x->best_mv.as_int; |
| 6243 | const int pw = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 6244 | const int ph = 4 * num_4x4_blocks_high_lookup[bsize]; |
| 6245 | // Use up-sampled reference frames. |
| 6246 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 6247 | struct buf_2d backup_pred = pd->pre[ref_idx]; |
| 6248 | const YV12_BUFFER_CONFIG *upsampled_ref = get_upsampled_ref(cpi, ref); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6249 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6250 | // Set pred for Y plane |
| 6251 | setup_pred_plane( |
| 6252 | &pd->pre[ref_idx], upsampled_ref->y_buffer, |
| 6253 | upsampled_ref->y_crop_width, upsampled_ref->y_crop_height, |
| 6254 | upsampled_ref->y_stride, (mi_row << 3), (mi_col << 3), NULL, |
| 6255 | pd->subsampling_x, pd->subsampling_y); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6256 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6257 | best_mv_var = cpi->find_fractional_mv_step( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6258 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 6259 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 6260 | cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list), |
| 6261 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, pw, ph, |
| 6262 | 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6263 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6264 | if (try_second) { |
| 6265 | const int minc = AOMMAX(x->mv_col_min * 8, ref_mv.col - MV_MAX); |
| 6266 | const int maxc = AOMMIN(x->mv_col_max * 8, ref_mv.col + MV_MAX); |
| 6267 | const int minr = AOMMAX(x->mv_row_min * 8, ref_mv.row - MV_MAX); |
| 6268 | const int maxr = AOMMIN(x->mv_row_max * 8, ref_mv.row + MV_MAX); |
| 6269 | int this_var; |
| 6270 | MV best_mv = x->best_mv.as_mv; |
| 6271 | |
| 6272 | x->best_mv = x->second_best_mv; |
| 6273 | if (x->best_mv.as_mv.row * 8 <= maxr && |
| 6274 | x->best_mv.as_mv.row * 8 >= minr && |
| 6275 | x->best_mv.as_mv.col * 8 <= maxc && |
| 6276 | x->best_mv.as_mv.col * 8 >= minc) { |
| 6277 | this_var = cpi->find_fractional_mv_step( |
| 6278 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 6279 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 6280 | cpi->sf.mv.subpel_iters_per_step, |
| 6281 | cond_cost_list(cpi, cost_list), x->nmvjointcost, x->mvcost, |
| 6282 | &dis, &x->pred_sse[ref], NULL, pw, ph, 1); |
| 6283 | if (this_var < best_mv_var) best_mv = x->best_mv.as_mv; |
| 6284 | x->best_mv.as_mv = best_mv; |
| 6285 | } |
| 6286 | } |
| 6287 | |
| 6288 | // Restore the reference frames. |
| 6289 | pd->pre[ref_idx] = backup_pred; |
| 6290 | } else { |
| 6291 | cpi->find_fractional_mv_step( |
| 6292 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 6293 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 6294 | cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list), |
| 6295 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, 0, 0, |
| 6296 | 0); |
| 6297 | } |
| 6298 | #if CONFIG_MOTION_VAR |
| 6299 | break; |
| 6300 | case OBMC_CAUSAL: |
| 6301 | av1_find_best_obmc_sub_pixel_tree_up( |
| 6302 | cpi, x, mi_row, mi_col, &x->best_mv.as_mv, &ref_mv, |
| 6303 | cm->allow_high_precision_mv, x->errorperbit, &cpi->fn_ptr[bsize], |
| 6304 | cpi->sf.mv.subpel_force_stop, cpi->sf.mv.subpel_iters_per_step, |
| 6305 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], 0, |
| 6306 | cpi->sf.use_upsampled_references); |
| 6307 | break; |
| 6308 | default: assert("Invalid motion mode!\n"); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6309 | } |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6310 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6311 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6312 | *rate_mv = av1_mv_bit_cost(&x->best_mv.as_mv, &ref_mv, x->nmvjointcost, |
| 6313 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6314 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6315 | #if CONFIG_MOTION_VAR |
| 6316 | if (cpi->sf.adaptive_motion_search && mbmi->motion_mode == SIMPLE_TRANSLATION) |
| 6317 | #else |
| 6318 | if (cpi->sf.adaptive_motion_search) |
| 6319 | #endif // CONFIG_MOTION_VAR |
| 6320 | x->pred_mv[ref] = x->best_mv.as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6321 | |
| 6322 | if (scaled_ref_frame) { |
| 6323 | int i; |
| 6324 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6325 | xd->plane[i].pre[ref_idx] = backup_yv12[i]; |
| 6326 | } |
| 6327 | } |
| 6328 | |
| 6329 | static INLINE void restore_dst_buf(MACROBLOCKD *xd, |
| 6330 | uint8_t *orig_dst[MAX_MB_PLANE], |
| 6331 | int orig_dst_stride[MAX_MB_PLANE]) { |
| 6332 | int i; |
| 6333 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 6334 | xd->plane[i].dst.buf = orig_dst[i]; |
| 6335 | xd->plane[i].dst.stride = orig_dst_stride[i]; |
| 6336 | } |
| 6337 | } |
| 6338 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6339 | #if CONFIG_EXT_INTER |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6340 | static void do_masked_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6341 | const uint8_t *mask, int mask_stride, |
| 6342 | BLOCK_SIZE bsize, int mi_row, int mi_col, |
| 6343 | int_mv *tmp_mv, int *rate_mv, int ref_idx, |
| 6344 | int mv_idx) { |
| 6345 | MACROBLOCKD *xd = &x->e_mbd; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6346 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6347 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6348 | struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } }; |
| 6349 | int bestsme = INT_MAX; |
| 6350 | int step_param; |
| 6351 | int sadpb = x->sadperbit16; |
| 6352 | MV mvp_full; |
| 6353 | int ref = mbmi->ref_frame[ref_idx]; |
| 6354 | MV ref_mv = x->mbmi_ext->ref_mvs[ref][mv_idx].as_mv; |
| 6355 | |
| 6356 | int tmp_col_min = x->mv_col_min; |
| 6357 | int tmp_col_max = x->mv_col_max; |
| 6358 | int tmp_row_min = x->mv_row_min; |
| 6359 | int tmp_row_max = x->mv_row_max; |
| 6360 | |
| 6361 | const YV12_BUFFER_CONFIG *scaled_ref_frame = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6362 | av1_get_scaled_ref_frame(cpi, ref); |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 6363 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6364 | |
| 6365 | MV pred_mv[3]; |
| 6366 | pred_mv[0] = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 6367 | pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv; |
| 6368 | pred_mv[2] = x->pred_mv[ref]; |
| 6369 | |
| 6370 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6371 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6372 | #endif |
| 6373 | |
| 6374 | if (scaled_ref_frame) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6375 | // Swap out the reference frame for a version that's been scaled to |
| 6376 | // match the resolution of the current frame, allowing the existing |
| 6377 | // motion search code to be used without additional modifications. |
| 6378 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6379 | backup_yv12[i] = xd->plane[i].pre[ref_idx]; |
| 6380 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6381 | av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6382 | } |
| 6383 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6384 | av1_set_mv_search_range(x, &ref_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6385 | |
| 6386 | // Work out the size of the first step in the mv step search. |
| 6387 | // 0 here is maximum length first step. 1 is MAX >> 1 etc. |
| 6388 | if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) { |
| 6389 | // Take wtd average of the step_params based on the last frame's |
| 6390 | // max mv magnitude and that based on the best ref mvs of the current |
| 6391 | // block for the given reference. |
| 6392 | step_param = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6393 | (av1_init_search_range(x->max_mv_context[ref]) + cpi->mv_step_param) / |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6394 | 2; |
| 6395 | } else { |
| 6396 | step_param = cpi->mv_step_param; |
| 6397 | } |
| 6398 | |
| 6399 | // TODO(debargha): is show_frame needed here? |
| 6400 | if (cpi->sf.adaptive_motion_search && bsize < cm->sb_size && cm->show_frame) { |
| 6401 | int boffset = |
| 6402 | 2 * (b_width_log2_lookup[cm->sb_size] - |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6403 | AOMMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize])); |
| 6404 | step_param = AOMMAX(step_param, boffset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6405 | } |
| 6406 | |
| 6407 | if (cpi->sf.adaptive_motion_search) { |
| 6408 | int bwl = b_width_log2_lookup[bsize]; |
| 6409 | int bhl = b_height_log2_lookup[bsize]; |
| 6410 | int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4); |
| 6411 | |
| 6412 | if (tlevel < 5) step_param += 2; |
| 6413 | |
| 6414 | // prev_mv_sad is not setup for dynamically scaled frames. |
| 6415 | if (cpi->oxcf.resize_mode != RESIZE_DYNAMIC) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6416 | for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) { |
| 6417 | if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) { |
| 6418 | x->pred_mv[ref].row = 0; |
| 6419 | x->pred_mv[ref].col = 0; |
| 6420 | tmp_mv->as_int = INVALID_MV; |
| 6421 | |
| 6422 | if (scaled_ref_frame) { |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 6423 | int j; |
| 6424 | for (j = 0; j < MAX_MB_PLANE; ++j) |
| 6425 | xd->plane[j].pre[ref_idx] = backup_yv12[j]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6426 | } |
| 6427 | return; |
| 6428 | } |
| 6429 | } |
| 6430 | } |
| 6431 | } |
| 6432 | |
| 6433 | mvp_full = pred_mv[x->mv_best_ref_index[ref]]; |
| 6434 | |
| 6435 | mvp_full.col >>= 3; |
| 6436 | mvp_full.row >>= 3; |
| 6437 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6438 | bestsme = av1_masked_full_pixel_diamond( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6439 | cpi, x, mask, mask_stride, &mvp_full, step_param, sadpb, |
| 6440 | MAX_MVSEARCH_STEPS - 1 - step_param, 1, &cpi->fn_ptr[bsize], &ref_mv, |
| 6441 | &tmp_mv->as_mv, ref_idx); |
| 6442 | |
| 6443 | x->mv_col_min = tmp_col_min; |
| 6444 | x->mv_col_max = tmp_col_max; |
| 6445 | x->mv_row_min = tmp_row_min; |
| 6446 | x->mv_row_max = tmp_row_max; |
| 6447 | |
| 6448 | if (bestsme < INT_MAX) { |
| 6449 | int dis; /* TODO: use dis in distortion calculation later. */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6450 | av1_find_best_masked_sub_pixel_tree_up( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6451 | cpi, x, mask, mask_stride, mi_row, mi_col, &tmp_mv->as_mv, &ref_mv, |
| 6452 | cm->allow_high_precision_mv, x->errorperbit, &cpi->fn_ptr[bsize], |
| 6453 | cpi->sf.mv.subpel_force_stop, cpi->sf.mv.subpel_iters_per_step, |
| 6454 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], ref_idx, |
| 6455 | cpi->sf.use_upsampled_references); |
| 6456 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6457 | *rate_mv = av1_mv_bit_cost(&tmp_mv->as_mv, &ref_mv, x->nmvjointcost, |
| 6458 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6459 | |
| 6460 | if (cpi->sf.adaptive_motion_search && cm->show_frame) |
| 6461 | x->pred_mv[ref] = tmp_mv->as_mv; |
| 6462 | |
| 6463 | if (scaled_ref_frame) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6464 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6465 | xd->plane[i].pre[ref_idx] = backup_yv12[i]; |
| 6466 | } |
| 6467 | } |
| 6468 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6469 | static void do_masked_motion_search_indexed(const AV1_COMP *const cpi, |
| 6470 | MACROBLOCK *x, int wedge_index, |
| 6471 | int wedge_sign, BLOCK_SIZE bsize, |
| 6472 | int mi_row, int mi_col, |
| 6473 | int_mv *tmp_mv, int *rate_mv, |
| 6474 | int mv_idx[2], int which) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6475 | // NOTE: which values: 0 - 0 only, 1 - 1 only, 2 - both |
| 6476 | MACROBLOCKD *xd = &x->e_mbd; |
| 6477 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6478 | BLOCK_SIZE sb_type = mbmi->sb_type; |
| 6479 | const uint8_t *mask; |
| 6480 | const int mask_stride = 4 * num_4x4_blocks_wide_lookup[bsize]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6481 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, sb_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6482 | |
| 6483 | if (which == 0 || which == 2) |
| 6484 | do_masked_motion_search(cpi, x, mask, mask_stride, bsize, mi_row, mi_col, |
| 6485 | &tmp_mv[0], &rate_mv[0], 0, mv_idx[0]); |
| 6486 | |
| 6487 | if (which == 1 || which == 2) { |
| 6488 | // get the negative mask |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6489 | mask = av1_get_contiguous_soft_mask(wedge_index, !wedge_sign, sb_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6490 | do_masked_motion_search(cpi, x, mask, mask_stride, bsize, mi_row, mi_col, |
| 6491 | &tmp_mv[1], &rate_mv[1], 1, mv_idx[1]); |
| 6492 | } |
| 6493 | } |
| 6494 | #endif // CONFIG_EXT_INTER |
| 6495 | |
| 6496 | // In some situations we want to discount tha pparent cost of a new motion |
| 6497 | // vector. Where there is a subtle motion field and especially where there is |
| 6498 | // low spatial complexity then it can be hard to cover the cost of a new motion |
| 6499 | // vector in a single block, even if that motion vector reduces distortion. |
| 6500 | // However, once established that vector may be usable through the nearest and |
| 6501 | // near mv modes to reduce distortion in subsequent blocks and also improve |
| 6502 | // visual quality. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6503 | static int discount_newmv_test(const AV1_COMP *const cpi, int this_mode, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6504 | int_mv this_mv, |
| 6505 | int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], |
| 6506 | int ref_frame) { |
| 6507 | return (!cpi->rc.is_src_frame_alt_ref && (this_mode == NEWMV) && |
| 6508 | (this_mv.as_int != 0) && |
| 6509 | ((mode_mv[NEARESTMV][ref_frame].as_int == 0) || |
| 6510 | (mode_mv[NEARESTMV][ref_frame].as_int == INVALID_MV)) && |
| 6511 | ((mode_mv[NEARMV][ref_frame].as_int == 0) || |
| 6512 | (mode_mv[NEARMV][ref_frame].as_int == INVALID_MV))); |
| 6513 | } |
| 6514 | |
Yaowu Xu | 671f2bd | 2016-09-30 15:07:57 -0700 | [diff] [blame] | 6515 | #define LEFT_TOP_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3) |
| 6516 | #define RIGHT_BOTTOM_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6517 | |
| 6518 | // TODO(jingning): this mv clamping function should be block size dependent. |
| 6519 | static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) { |
| 6520 | clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN, |
| 6521 | xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN, |
| 6522 | xd->mb_to_top_edge - LEFT_TOP_MARGIN, |
| 6523 | xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN); |
| 6524 | } |
| 6525 | |
| 6526 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6527 | static int estimate_wedge_sign(const AV1_COMP *cpi, const MACROBLOCK *x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6528 | const BLOCK_SIZE bsize, const uint8_t *pred0, |
| 6529 | int stride0, const uint8_t *pred1, int stride1) { |
| 6530 | const struct macroblock_plane *const p = &x->plane[0]; |
| 6531 | const uint8_t *src = p->src.buf; |
| 6532 | int src_stride = p->src.stride; |
| 6533 | const int f_index = bsize - BLOCK_8X8; |
| 6534 | const int bw = 4 << (b_width_log2_lookup[bsize]); |
| 6535 | const int bh = 4 << (b_height_log2_lookup[bsize]); |
| 6536 | uint32_t esq[2][4], var; |
| 6537 | int64_t tl, br; |
| 6538 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6539 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6540 | if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 6541 | pred0 = CONVERT_TO_BYTEPTR(pred0); |
| 6542 | pred1 = CONVERT_TO_BYTEPTR(pred1); |
| 6543 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6544 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6545 | |
| 6546 | var = cpi->fn_ptr[f_index].vf(src, src_stride, pred0, stride0, &esq[0][0]); |
| 6547 | var = cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred0 + bw / 2, |
| 6548 | stride0, &esq[0][1]); |
| 6549 | var = cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride, |
| 6550 | pred0 + bh / 2 * stride0, stride0, &esq[0][2]); |
| 6551 | var = cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride, |
| 6552 | pred0 + bh / 2 * stride0 + bw / 2, stride0, |
| 6553 | &esq[0][3]); |
| 6554 | var = cpi->fn_ptr[f_index].vf(src, src_stride, pred1, stride1, &esq[1][0]); |
| 6555 | var = cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred1 + bw / 2, |
| 6556 | stride1, &esq[1][1]); |
| 6557 | var = cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride, |
| 6558 | pred1 + bh / 2 * stride1, stride0, &esq[1][2]); |
| 6559 | var = cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride, |
| 6560 | pred1 + bh / 2 * stride1 + bw / 2, stride0, |
| 6561 | &esq[1][3]); |
| 6562 | (void)var; |
| 6563 | |
| 6564 | tl = (int64_t)(esq[0][0] + esq[0][1] + esq[0][2]) - |
| 6565 | (int64_t)(esq[1][0] + esq[1][1] + esq[1][2]); |
| 6566 | br = (int64_t)(esq[1][3] + esq[1][1] + esq[1][2]) - |
| 6567 | (int64_t)(esq[0][3] + esq[0][1] + esq[0][2]); |
| 6568 | return (tl + br > 0); |
| 6569 | } |
| 6570 | #endif // CONFIG_EXT_INTER |
| 6571 | |
| 6572 | #if !CONFIG_DUAL_FILTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 6573 | static InterpFilter predict_interp_filter( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6574 | const AV1_COMP *cpi, const MACROBLOCK *x, const BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6575 | const int mi_row, const int mi_col, |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 6576 | InterpFilter (*single_filter)[TOTAL_REFS_PER_FRAME]) { |
| 6577 | InterpFilter best_filter = SWITCHABLE; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6578 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6579 | const MACROBLOCKD *xd = &x->e_mbd; |
| 6580 | int bsl = mi_width_log2_lookup[bsize]; |
| 6581 | int pred_filter_search = |
| 6582 | cpi->sf.cb_pred_filter_search |
| 6583 | ? (((mi_row + mi_col) >> bsl) + |
| 6584 | get_chessboard_index(cm->current_video_frame)) & |
| 6585 | 0x1 |
| 6586 | : 0; |
| 6587 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6588 | const int is_comp_pred = has_second_ref(mbmi); |
| 6589 | const int this_mode = mbmi->mode; |
| 6590 | int refs[2] = { mbmi->ref_frame[0], |
| 6591 | (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6592 | if (pred_filter_search) { |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 6593 | InterpFilter af = SWITCHABLE, lf = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6594 | if (xd->up_available) af = xd->mi[-xd->mi_stride]->mbmi.interp_filter; |
| 6595 | if (xd->left_available) lf = xd->mi[-1]->mbmi.interp_filter; |
| 6596 | |
| 6597 | #if CONFIG_EXT_INTER |
| 6598 | if ((this_mode != NEWMV && this_mode != NEWFROMNEARMV && |
| 6599 | this_mode != NEW_NEWMV) || |
| 6600 | (af == lf)) |
| 6601 | #else |
| 6602 | if ((this_mode != NEWMV) || (af == lf)) |
| 6603 | #endif // CONFIG_EXT_INTER |
| 6604 | best_filter = af; |
| 6605 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6606 | if (is_comp_pred) { |
| 6607 | if (cpi->sf.adaptive_mode_search) { |
| 6608 | #if CONFIG_EXT_INTER |
| 6609 | switch (this_mode) { |
| 6610 | case NEAREST_NEARESTMV: |
| 6611 | if (single_filter[NEARESTMV][refs[0]] == |
| 6612 | single_filter[NEARESTMV][refs[1]]) |
| 6613 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 6614 | break; |
| 6615 | case NEAREST_NEARMV: |
| 6616 | if (single_filter[NEARESTMV][refs[0]] == |
| 6617 | single_filter[NEARMV][refs[1]]) |
| 6618 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 6619 | break; |
| 6620 | case NEAR_NEARESTMV: |
| 6621 | if (single_filter[NEARMV][refs[0]] == |
| 6622 | single_filter[NEARESTMV][refs[1]]) |
| 6623 | best_filter = single_filter[NEARMV][refs[0]]; |
| 6624 | break; |
| 6625 | case NEAR_NEARMV: |
| 6626 | if (single_filter[NEARMV][refs[0]] == single_filter[NEARMV][refs[1]]) |
| 6627 | best_filter = single_filter[NEARMV][refs[0]]; |
| 6628 | break; |
| 6629 | case ZERO_ZEROMV: |
| 6630 | if (single_filter[ZEROMV][refs[0]] == single_filter[ZEROMV][refs[1]]) |
| 6631 | best_filter = single_filter[ZEROMV][refs[0]]; |
| 6632 | break; |
| 6633 | case NEW_NEWMV: |
| 6634 | if (single_filter[NEWMV][refs[0]] == single_filter[NEWMV][refs[1]]) |
| 6635 | best_filter = single_filter[NEWMV][refs[0]]; |
| 6636 | break; |
| 6637 | case NEAREST_NEWMV: |
| 6638 | if (single_filter[NEARESTMV][refs[0]] == |
| 6639 | single_filter[NEWMV][refs[1]]) |
| 6640 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 6641 | break; |
| 6642 | case NEAR_NEWMV: |
| 6643 | if (single_filter[NEARMV][refs[0]] == single_filter[NEWMV][refs[1]]) |
| 6644 | best_filter = single_filter[NEARMV][refs[0]]; |
| 6645 | break; |
| 6646 | case NEW_NEARESTMV: |
| 6647 | if (single_filter[NEWMV][refs[0]] == |
| 6648 | single_filter[NEARESTMV][refs[1]]) |
| 6649 | best_filter = single_filter[NEWMV][refs[0]]; |
| 6650 | break; |
| 6651 | case NEW_NEARMV: |
| 6652 | if (single_filter[NEWMV][refs[0]] == single_filter[NEARMV][refs[1]]) |
| 6653 | best_filter = single_filter[NEWMV][refs[0]]; |
| 6654 | break; |
| 6655 | default: |
| 6656 | if (single_filter[this_mode][refs[0]] == |
| 6657 | single_filter[this_mode][refs[1]]) |
| 6658 | best_filter = single_filter[this_mode][refs[0]]; |
| 6659 | break; |
| 6660 | } |
| 6661 | #else |
| 6662 | if (single_filter[this_mode][refs[0]] == |
| 6663 | single_filter[this_mode][refs[1]]) |
| 6664 | best_filter = single_filter[this_mode][refs[0]]; |
| 6665 | #endif // CONFIG_EXT_INTER |
| 6666 | } |
| 6667 | } |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 6668 | if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) { |
| 6669 | best_filter = EIGHTTAP_REGULAR; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6670 | } |
| 6671 | return best_filter; |
| 6672 | } |
| 6673 | #endif |
| 6674 | |
| 6675 | #if CONFIG_EXT_INTER |
| 6676 | // Choose the best wedge index and sign |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6677 | static int64_t pick_wedge(const AV1_COMP *const cpi, const MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6678 | const BLOCK_SIZE bsize, const uint8_t *const p0, |
| 6679 | const uint8_t *const p1, int *const best_wedge_sign, |
| 6680 | int *const best_wedge_index) { |
| 6681 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 6682 | const struct buf_2d *const src = &x->plane[0].src; |
| 6683 | const int bw = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 6684 | const int bh = 4 * num_4x4_blocks_high_lookup[bsize]; |
| 6685 | const int N = bw * bh; |
| 6686 | int rate; |
| 6687 | int64_t dist; |
| 6688 | int64_t rd, best_rd = INT64_MAX; |
| 6689 | int wedge_index; |
| 6690 | int wedge_sign; |
| 6691 | int wedge_types = (1 << get_wedge_bits_lookup(bsize)); |
| 6692 | const uint8_t *mask; |
| 6693 | uint64_t sse; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6694 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6695 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 6696 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 6697 | #else |
| 6698 | const int bd_round = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6699 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6700 | |
| 6701 | DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]); |
| 6702 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 6703 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 6704 | DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]); |
| 6705 | |
| 6706 | int64_t sign_limit; |
| 6707 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6708 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6709 | if (hbd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6710 | aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6711 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6712 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6713 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6714 | aom_highbd_subtract_block(bh, bw, d10, bw, CONVERT_TO_BYTEPTR(p1), bw, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6715 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 6716 | } else // NOLINT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6717 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6718 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6719 | aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw); |
| 6720 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 6721 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6722 | } |
| 6723 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6724 | sign_limit = ((int64_t)aom_sum_squares_i16(r0, N) - |
| 6725 | (int64_t)aom_sum_squares_i16(r1, N)) * |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6726 | (1 << WEDGE_WEIGHT_BITS) / 2; |
| 6727 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6728 | av1_wedge_compute_delta_squares(ds, r0, r1, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6729 | |
| 6730 | for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6731 | mask = av1_get_contiguous_soft_mask(wedge_index, 0, bsize); |
| 6732 | wedge_sign = av1_wedge_sign_from_residuals(ds, mask, N, sign_limit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6733 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6734 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize); |
| 6735 | sse = av1_wedge_sse_from_residuals(r1, d10, mask, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6736 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
| 6737 | |
| 6738 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
| 6739 | rd = RDCOST(x->rdmult, x->rddiv, rate, dist); |
| 6740 | |
| 6741 | if (rd < best_rd) { |
| 6742 | *best_wedge_index = wedge_index; |
| 6743 | *best_wedge_sign = wedge_sign; |
| 6744 | best_rd = rd; |
| 6745 | } |
| 6746 | } |
| 6747 | |
| 6748 | return best_rd; |
| 6749 | } |
| 6750 | |
| 6751 | // Choose the best wedge index the specified sign |
| 6752 | static int64_t pick_wedge_fixed_sign( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6753 | const AV1_COMP *const cpi, const MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6754 | const BLOCK_SIZE bsize, const uint8_t *const p0, const uint8_t *const p1, |
| 6755 | const int wedge_sign, int *const best_wedge_index) { |
| 6756 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 6757 | const struct buf_2d *const src = &x->plane[0].src; |
| 6758 | const int bw = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 6759 | const int bh = 4 * num_4x4_blocks_high_lookup[bsize]; |
| 6760 | const int N = bw * bh; |
| 6761 | int rate; |
| 6762 | int64_t dist; |
| 6763 | int64_t rd, best_rd = INT64_MAX; |
| 6764 | int wedge_index; |
| 6765 | int wedge_types = (1 << get_wedge_bits_lookup(bsize)); |
| 6766 | const uint8_t *mask; |
| 6767 | uint64_t sse; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6768 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6769 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 6770 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 6771 | #else |
| 6772 | const int bd_round = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6773 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6774 | |
| 6775 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 6776 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 6777 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6778 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6779 | if (hbd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6780 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6781 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6782 | aom_highbd_subtract_block(bh, bw, d10, bw, CONVERT_TO_BYTEPTR(p1), bw, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6783 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 6784 | } else // NOLINT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6785 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6786 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6787 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 6788 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6792 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize); |
| 6793 | sse = av1_wedge_sse_from_residuals(r1, d10, mask, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6794 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
| 6795 | |
| 6796 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
| 6797 | rd = RDCOST(x->rdmult, x->rddiv, rate, dist); |
| 6798 | |
| 6799 | if (rd < best_rd) { |
| 6800 | *best_wedge_index = wedge_index; |
| 6801 | best_rd = rd; |
| 6802 | } |
| 6803 | } |
| 6804 | |
| 6805 | return best_rd; |
| 6806 | } |
| 6807 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6808 | static int64_t pick_interinter_wedge(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6809 | const MACROBLOCK *const x, |
| 6810 | const BLOCK_SIZE bsize, |
| 6811 | const uint8_t *const p0, |
| 6812 | const uint8_t *const p1) { |
| 6813 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 6814 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 6815 | const int bw = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 6816 | |
| 6817 | int64_t rd; |
| 6818 | int wedge_index = -1; |
| 6819 | int wedge_sign = 0; |
| 6820 | |
| 6821 | assert(is_interinter_wedge_used(bsize)); |
| 6822 | |
| 6823 | if (cpi->sf.fast_wedge_sign_estimate) { |
| 6824 | wedge_sign = estimate_wedge_sign(cpi, x, bsize, p0, bw, p1, bw); |
| 6825 | rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, wedge_sign, &wedge_index); |
| 6826 | } else { |
| 6827 | rd = pick_wedge(cpi, x, bsize, p0, p1, &wedge_sign, &wedge_index); |
| 6828 | } |
| 6829 | |
| 6830 | mbmi->interinter_wedge_sign = wedge_sign; |
| 6831 | mbmi->interinter_wedge_index = wedge_index; |
| 6832 | return rd; |
| 6833 | } |
| 6834 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6835 | static int64_t pick_interintra_wedge(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6836 | const MACROBLOCK *const x, |
| 6837 | const BLOCK_SIZE bsize, |
| 6838 | const uint8_t *const p0, |
| 6839 | const uint8_t *const p1) { |
| 6840 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 6841 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 6842 | |
| 6843 | int64_t rd; |
| 6844 | int wedge_index = -1; |
| 6845 | |
| 6846 | assert(is_interintra_wedge_used(bsize)); |
| 6847 | |
| 6848 | rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, 0, &wedge_index); |
| 6849 | |
| 6850 | mbmi->interintra_wedge_sign = 0; |
| 6851 | mbmi->interintra_wedge_index = wedge_index; |
| 6852 | return rd; |
| 6853 | } |
| 6854 | #endif // CONFIG_EXT_INTER |
| 6855 | |
| 6856 | static int64_t handle_inter_mode( |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6857 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int *rate2, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6858 | int64_t *distortion, int *skippable, int *rate_y, int *rate_uv, |
| 6859 | int *disable_skip, int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], int mi_row, |
| 6860 | int mi_col, |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 6861 | #if CONFIG_MOTION_VAR |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6862 | uint8_t *above_pred_buf[3], int above_pred_stride[3], |
| 6863 | uint8_t *left_pred_buf[3], int left_pred_stride[3], |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 6864 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6865 | #if CONFIG_EXT_INTER |
| 6866 | int_mv single_newmvs[2][TOTAL_REFS_PER_FRAME], |
| 6867 | int single_newmvs_rate[2][TOTAL_REFS_PER_FRAME], |
| 6868 | int *compmode_interintra_cost, int *compmode_wedge_cost, |
| 6869 | int64_t (*const modelled_rd)[TOTAL_REFS_PER_FRAME], |
| 6870 | #else |
| 6871 | int_mv single_newmv[TOTAL_REFS_PER_FRAME], |
| 6872 | #endif // CONFIG_EXT_INTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 6873 | InterpFilter (*single_filter)[TOTAL_REFS_PER_FRAME], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6874 | int (*single_skippable)[TOTAL_REFS_PER_FRAME], int64_t *psse, |
| 6875 | const int64_t ref_best_rd) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6876 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6877 | MACROBLOCKD *xd = &x->e_mbd; |
| 6878 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6879 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 6880 | const int is_comp_pred = has_second_ref(mbmi); |
| 6881 | const int this_mode = mbmi->mode; |
| 6882 | int_mv *frame_mv = mode_mv[this_mode]; |
| 6883 | int i; |
| 6884 | int refs[2] = { mbmi->ref_frame[0], |
| 6885 | (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; |
| 6886 | int_mv cur_mv[2]; |
| 6887 | int rate_mv = 0; |
| 6888 | #if CONFIG_EXT_INTER |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 6889 | int pred_exists = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6890 | const int bw = 4 * num_4x4_blocks_wide_lookup[bsize]; |
| 6891 | int mv_idx = (this_mode == NEWFROMNEARMV) ? 1 : 0; |
| 6892 | int_mv single_newmv[TOTAL_REFS_PER_FRAME]; |
| 6893 | const unsigned int *const interintra_mode_cost = |
| 6894 | cpi->interintra_mode_cost[size_group_lookup[bsize]]; |
| 6895 | const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME); |
| 6896 | #if CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6897 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6898 | #endif |
| 6899 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6900 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6901 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 6902 | #else |
| 6903 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_[MAX_MB_PLANE * MAX_SB_SQUARE]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6904 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6905 | uint8_t *tmp_buf; |
| 6906 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 6907 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6908 | int allow_motvar = |
| 6909 | #if CONFIG_EXT_INTER |
| 6910 | !is_comp_interintra_pred && |
| 6911 | #endif // CONFIG_EXT_INTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 6912 | is_motion_variation_allowed(mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6913 | int rate2_nocoeff = 0, best_rate2 = INT_MAX, best_skippable, best_xskip, |
| 6914 | best_disable_skip = 0; |
| 6915 | int best_rate_y, best_rate_uv; |
| 6916 | #if CONFIG_VAR_TX |
| 6917 | uint8_t best_blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 4]; |
| 6918 | #endif // CONFIG_VAR_TX |
| 6919 | int64_t best_distortion = INT64_MAX; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 6920 | int64_t best_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6921 | MB_MODE_INFO best_mbmi; |
| 6922 | #if CONFIG_EXT_INTER |
| 6923 | int rate2_bmc_nocoeff; |
| 6924 | int rate_mv_bmc; |
| 6925 | MB_MODE_INFO best_bmc_mbmi; |
| 6926 | #endif // CONFIG_EXT_INTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 6927 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 6928 | int64_t rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6929 | uint8_t *orig_dst[MAX_MB_PLANE]; |
| 6930 | int orig_dst_stride[MAX_MB_PLANE]; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 6931 | uint8_t *tmp_dst[MAX_MB_PLANE]; |
| 6932 | int tmp_dst_stride[MAX_MB_PLANE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6933 | int rs = 0; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 6934 | InterpFilter assign_filter = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6935 | |
| 6936 | int skip_txfm_sb = 0; |
| 6937 | int64_t skip_sse_sb = INT64_MAX; |
| 6938 | int64_t distortion_y = 0, distortion_uv = 0; |
| 6939 | int16_t mode_ctx = mbmi_ext->mode_context[refs[0]]; |
| 6940 | |
| 6941 | #if CONFIG_EXT_INTER |
| 6942 | *compmode_interintra_cost = 0; |
| 6943 | mbmi->use_wedge_interintra = 0; |
| 6944 | *compmode_wedge_cost = 0; |
| 6945 | mbmi->use_wedge_interinter = 0; |
| 6946 | |
| 6947 | // is_comp_interintra_pred implies !is_comp_pred |
| 6948 | assert(!is_comp_interintra_pred || (!is_comp_pred)); |
| 6949 | // is_comp_interintra_pred implies is_interintra_allowed(mbmi->sb_type) |
| 6950 | assert(!is_comp_interintra_pred || is_interintra_allowed(mbmi)); |
| 6951 | #endif // CONFIG_EXT_INTER |
| 6952 | |
| 6953 | #if CONFIG_REF_MV |
| 6954 | #if CONFIG_EXT_INTER |
| 6955 | if (is_comp_pred) |
| 6956 | mode_ctx = mbmi_ext->compound_mode_context[refs[0]]; |
| 6957 | else |
| 6958 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6959 | mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context, |
| 6960 | mbmi->ref_frame, bsize, -1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6961 | #endif |
| 6962 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6963 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6964 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 6965 | tmp_buf = CONVERT_TO_BYTEPTR(tmp_buf_); |
| 6966 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6967 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6968 | tmp_buf = tmp_buf_; |
| 6969 | |
| 6970 | if (is_comp_pred) { |
| 6971 | if (frame_mv[refs[0]].as_int == INVALID_MV || |
| 6972 | frame_mv[refs[1]].as_int == INVALID_MV) |
| 6973 | return INT64_MAX; |
| 6974 | } |
| 6975 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6976 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6977 | if (have_newmv_in_inter_mode(this_mode)) { |
| 6978 | if (is_comp_pred) { |
| 6979 | #if CONFIG_EXT_INTER |
| 6980 | for (i = 0; i < 2; ++i) { |
| 6981 | single_newmv[refs[i]].as_int = single_newmvs[mv_idx][refs[i]].as_int; |
| 6982 | } |
| 6983 | |
| 6984 | if (this_mode == NEW_NEWMV) { |
| 6985 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
| 6986 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
| 6987 | |
| 6988 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 6989 | joint_motion_search(cpi, x, bsize, frame_mv, mi_row, mi_col, NULL, |
| 6990 | single_newmv, &rate_mv, 0); |
| 6991 | } else { |
| 6992 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6993 | av1_set_mvcost(x, mbmi->ref_frame[0], 0, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6994 | #endif // CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6995 | rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
Zoe Liu | 82c8c92 | 2016-11-01 14:52:34 -0700 | [diff] [blame] | 6996 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6997 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6998 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6999 | av1_set_mvcost(x, mbmi->ref_frame[1], 1, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7000 | #endif // CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7001 | rate_mv += av1_mv_bit_cost( |
Zoe Liu | 82c8c92 | 2016-11-01 14:52:34 -0700 | [diff] [blame] | 7002 | &frame_mv[refs[1]].as_mv, &mbmi_ext->ref_mvs[refs[1]][0].as_mv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7003 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 7004 | } |
| 7005 | } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV) { |
| 7006 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7007 | rate_mv = av1_mv_bit_cost(&frame_mv[refs[1]].as_mv, |
Zoe Liu | 82c8c92 | 2016-11-01 14:52:34 -0700 | [diff] [blame] | 7008 | &mbmi_ext->ref_mvs[refs[1]][0].as_mv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7009 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7010 | } else { |
| 7011 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7012 | rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
Zoe Liu | 82c8c92 | 2016-11-01 14:52:34 -0700 | [diff] [blame] | 7013 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7014 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7015 | } |
| 7016 | #else |
| 7017 | // Initialize mv using single prediction mode result. |
| 7018 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
| 7019 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
| 7020 | |
| 7021 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 7022 | joint_motion_search(cpi, x, bsize, frame_mv, mi_row, mi_col, |
| 7023 | single_newmv, &rate_mv, 0); |
| 7024 | } else { |
| 7025 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 7026 | av1_set_mvcost(x, mbmi->ref_frame[0], 0, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7027 | #endif // CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7028 | rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
Zoe Liu | 82c8c92 | 2016-11-01 14:52:34 -0700 | [diff] [blame] | 7029 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7030 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7031 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 7032 | av1_set_mvcost(x, mbmi->ref_frame[1], 1, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7033 | #endif // CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7034 | rate_mv += av1_mv_bit_cost(&frame_mv[refs[1]].as_mv, |
Zoe Liu | 82c8c92 | 2016-11-01 14:52:34 -0700 | [diff] [blame] | 7035 | &mbmi_ext->ref_mvs[refs[1]][0].as_mv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7036 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7037 | } |
| 7038 | #endif // CONFIG_EXT_INTER |
| 7039 | } else { |
| 7040 | #if CONFIG_EXT_INTER |
| 7041 | if (is_comp_interintra_pred) { |
| 7042 | x->best_mv = single_newmvs[mv_idx][refs[0]]; |
| 7043 | rate_mv = single_newmvs_rate[mv_idx][refs[0]]; |
| 7044 | } else { |
| 7045 | single_motion_search(cpi, x, bsize, mi_row, mi_col, 0, mv_idx, |
| 7046 | &rate_mv); |
| 7047 | single_newmvs[mv_idx][refs[0]] = x->best_mv; |
| 7048 | single_newmvs_rate[mv_idx][refs[0]] = rate_mv; |
| 7049 | } |
| 7050 | #else |
| 7051 | single_motion_search(cpi, x, bsize, mi_row, mi_col, &rate_mv); |
| 7052 | single_newmv[refs[0]] = x->best_mv; |
| 7053 | #endif // CONFIG_EXT_INTER |
| 7054 | |
| 7055 | if (x->best_mv.as_int == INVALID_MV) return INT64_MAX; |
| 7056 | |
| 7057 | frame_mv[refs[0]] = x->best_mv; |
| 7058 | xd->mi[0]->bmi[0].as_mv[0] = x->best_mv; |
| 7059 | |
| 7060 | // Estimate the rate implications of a new mv but discount this |
| 7061 | // under certain circumstances where we want to help initiate a weak |
| 7062 | // motion field, where the distortion gain for a single block may not |
| 7063 | // be enough to overcome the cost of a new mv. |
| 7064 | if (discount_newmv_test(cpi, this_mode, x->best_mv, mode_mv, refs[0])) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7065 | rate_mv = AOMMAX((rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7066 | } |
| 7067 | } |
| 7068 | *rate2 += rate_mv; |
| 7069 | } |
| 7070 | |
| 7071 | for (i = 0; i < is_comp_pred + 1; ++i) { |
| 7072 | cur_mv[i] = frame_mv[refs[i]]; |
| 7073 | // Clip "next_nearest" so that it does not extend to far out of image |
| 7074 | #if CONFIG_EXT_INTER |
| 7075 | if (this_mode != NEWMV && this_mode != NEWFROMNEARMV) |
| 7076 | #else |
| 7077 | if (this_mode != NEWMV) |
| 7078 | #endif // CONFIG_EXT_INTER |
| 7079 | clamp_mv2(&cur_mv[i].as_mv, xd); |
| 7080 | |
| 7081 | if (mv_check_bounds(x, &cur_mv[i].as_mv)) return INT64_MAX; |
| 7082 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 7083 | } |
| 7084 | |
| 7085 | #if CONFIG_REF_MV |
| 7086 | #if CONFIG_EXT_INTER |
| 7087 | if (this_mode == NEAREST_NEARESTMV) { |
| 7088 | #else |
| 7089 | if (this_mode == NEARESTMV && is_comp_pred) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7090 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7091 | #endif // CONFIG_EXT_INTER |
| 7092 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
| 7093 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 7094 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 7095 | |
| 7096 | for (i = 0; i < 2; ++i) { |
| 7097 | clamp_mv2(&cur_mv[i].as_mv, xd); |
| 7098 | if (mv_check_bounds(x, &cur_mv[i].as_mv)) return INT64_MAX; |
| 7099 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 7100 | } |
| 7101 | } |
| 7102 | } |
| 7103 | |
| 7104 | #if CONFIG_EXT_INTER |
| 7105 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
| 7106 | if (this_mode == NEAREST_NEWMV || this_mode == NEAREST_NEARMV) { |
| 7107 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 7108 | |
| 7109 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv); |
| 7110 | clamp_mv2(&cur_mv[0].as_mv, xd); |
| 7111 | if (mv_check_bounds(x, &cur_mv[0].as_mv)) return INT64_MAX; |
| 7112 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 7113 | } |
| 7114 | |
| 7115 | if (this_mode == NEW_NEARESTMV || this_mode == NEAR_NEARESTMV) { |
| 7116 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 7117 | |
| 7118 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv); |
| 7119 | clamp_mv2(&cur_mv[1].as_mv, xd); |
| 7120 | if (mv_check_bounds(x, &cur_mv[1].as_mv)) return INT64_MAX; |
| 7121 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 7122 | } |
| 7123 | } |
| 7124 | |
| 7125 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
| 7126 | if (this_mode == NEAR_NEWMV || this_mode == NEAR_NEARESTMV || |
| 7127 | this_mode == NEAR_NEARMV) { |
| 7128 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][1].this_mv; |
| 7129 | |
| 7130 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv); |
| 7131 | clamp_mv2(&cur_mv[0].as_mv, xd); |
| 7132 | if (mv_check_bounds(x, &cur_mv[0].as_mv)) return INT64_MAX; |
| 7133 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 7134 | } |
| 7135 | |
| 7136 | if (this_mode == NEW_NEARMV || this_mode == NEAREST_NEARMV || |
| 7137 | this_mode == NEAR_NEARMV) { |
| 7138 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][1].comp_mv; |
| 7139 | |
| 7140 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv); |
| 7141 | clamp_mv2(&cur_mv[1].as_mv, xd); |
| 7142 | if (mv_check_bounds(x, &cur_mv[1].as_mv)) return INT64_MAX; |
| 7143 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 7144 | } |
| 7145 | } |
| 7146 | #else |
| 7147 | if (this_mode == NEARMV && is_comp_pred) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7148 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7149 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
| 7150 | int ref_mv_idx = mbmi->ref_mv_idx + 1; |
| 7151 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 7152 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv; |
| 7153 | |
| 7154 | for (i = 0; i < 2; ++i) { |
| 7155 | clamp_mv2(&cur_mv[i].as_mv, xd); |
| 7156 | if (mv_check_bounds(x, &cur_mv[i].as_mv)) return INT64_MAX; |
| 7157 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 7158 | } |
| 7159 | } |
| 7160 | } |
| 7161 | #endif // CONFIG_EXT_INTER |
| 7162 | #endif // CONFIG_REF_MV |
| 7163 | |
| 7164 | // do first prediction into the destination buffer. Do the next |
| 7165 | // prediction into a temporary buffer. Then keep track of which one |
| 7166 | // of these currently holds the best predictor, and use the other |
| 7167 | // one for future predictions. In the end, copy from tmp_buf to |
| 7168 | // dst if necessary. |
| 7169 | for (i = 0; i < MAX_MB_PLANE; i++) { |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7170 | tmp_dst[i] = tmp_buf + i * MAX_SB_SQUARE; |
| 7171 | tmp_dst_stride[i] = MAX_SB_SIZE; |
| 7172 | } |
| 7173 | for (i = 0; i < MAX_MB_PLANE; i++) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7174 | orig_dst[i] = xd->plane[i].dst.buf; |
| 7175 | orig_dst_stride[i] = xd->plane[i].dst.stride; |
| 7176 | } |
| 7177 | |
| 7178 | // We don't include the cost of the second reference here, because there |
| 7179 | // are only three options: Last/Golden, ARF/Last or Golden/ARF, or in other |
| 7180 | // words if you present them in that order, the second one is always known |
| 7181 | // if the first is known. |
| 7182 | // |
| 7183 | // Under some circumstances we discount the cost of new mv mode to encourage |
| 7184 | // initiation of a motion field. |
| 7185 | if (discount_newmv_test(cpi, this_mode, frame_mv[refs[0]], mode_mv, |
| 7186 | refs[0])) { |
| 7187 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7188 | *rate2 += AOMMIN(cost_mv_ref(cpi, this_mode, is_comp_pred, mode_ctx), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7189 | cost_mv_ref(cpi, NEARESTMV, is_comp_pred, mode_ctx)); |
| 7190 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7191 | *rate2 += AOMMIN(cost_mv_ref(cpi, this_mode, mode_ctx), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7192 | cost_mv_ref(cpi, NEARESTMV, mode_ctx)); |
| 7193 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 7194 | } else { |
| 7195 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
| 7196 | *rate2 += cost_mv_ref(cpi, this_mode, is_comp_pred, mode_ctx); |
| 7197 | #else |
| 7198 | *rate2 += cost_mv_ref(cpi, this_mode, mode_ctx); |
| 7199 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 7200 | } |
| 7201 | |
| 7202 | if (RDCOST(x->rdmult, x->rddiv, *rate2, 0) > ref_best_rd && |
| 7203 | #if CONFIG_EXT_INTER |
| 7204 | mbmi->mode != NEARESTMV && mbmi->mode != NEAREST_NEARESTMV |
| 7205 | #else |
| 7206 | mbmi->mode != NEARESTMV |
| 7207 | #endif // CONFIG_EXT_INTER |
| 7208 | ) |
| 7209 | return INT64_MAX; |
| 7210 | |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7211 | if (cm->interp_filter == SWITCHABLE) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7212 | #if !CONFIG_DUAL_FILTER |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7213 | assign_filter = |
| 7214 | predict_interp_filter(cpi, x, bsize, mi_row, mi_col, single_filter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7215 | #endif |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7216 | #if CONFIG_EXT_INTERP || CONFIG_DUAL_FILTER |
| 7217 | if (!av1_is_interp_needed(xd)) assign_filter = EIGHTTAP_REGULAR; |
| 7218 | #endif |
| 7219 | } else { |
| 7220 | assign_filter = cm->interp_filter; |
| 7221 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7222 | |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7223 | { // Do interpolation filter search in the parentheses |
| 7224 | int tmp_rate; |
| 7225 | int64_t tmp_dist; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7226 | #if CONFIG_DUAL_FILTER |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7227 | mbmi->interp_filter[0] = |
| 7228 | assign_filter == SWITCHABLE ? EIGHTTAP_REGULAR : assign_filter; |
| 7229 | mbmi->interp_filter[1] = |
| 7230 | assign_filter == SWITCHABLE ? EIGHTTAP_REGULAR : assign_filter; |
| 7231 | mbmi->interp_filter[2] = |
| 7232 | assign_filter == SWITCHABLE ? EIGHTTAP_REGULAR : assign_filter; |
| 7233 | mbmi->interp_filter[3] = |
| 7234 | assign_filter == SWITCHABLE ? EIGHTTAP_REGULAR : assign_filter; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7235 | #else |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7236 | mbmi->interp_filter = |
| 7237 | assign_filter == SWITCHABLE ? EIGHTTAP_REGULAR : assign_filter; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7238 | #endif |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7239 | rs = av1_get_switchable_rate(cpi, xd); |
| 7240 | av1_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); |
| 7241 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7242 | &tmp_dist, &skip_txfm_sb, &skip_sse_sb); |
| 7243 | rd = RDCOST(x->rdmult, x->rddiv, rs + tmp_rate, tmp_dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7244 | |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7245 | if (assign_filter == SWITCHABLE) { |
| 7246 | // do interp_filter search |
| 7247 | if (av1_is_interp_needed(xd)) { |
| 7248 | int best_in_temp = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7249 | #if CONFIG_DUAL_FILTER |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7250 | InterpFilter best_filter[4]; |
| 7251 | av1_copy(best_filter, mbmi->interp_filter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7252 | #else |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7253 | InterpFilter best_filter = mbmi->interp_filter; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7254 | #endif |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7255 | restore_dst_buf(xd, tmp_dst, tmp_dst_stride); |
| 7256 | #if CONFIG_DUAL_FILTER |
| 7257 | // EIGHTTAP_REGULAR mode is calculated beforehand |
| 7258 | for (i = 1; i < SWITCHABLE_FILTERS * SWITCHABLE_FILTERS; ++i) |
| 7259 | #else |
| 7260 | // EIGHTTAP_REGULAR mode is calculated beforehand |
| 7261 | for (i = 1; i < SWITCHABLE_FILTERS; ++i) |
| 7262 | #endif |
| 7263 | { |
| 7264 | int tmp_skip_sb = 0; |
| 7265 | int64_t tmp_skip_sse = INT64_MAX; |
| 7266 | int tmp_rs; |
Angie Chiang | 3655dcd | 2016-10-28 09:05:27 -0700 | [diff] [blame] | 7267 | int64_t tmp_rd; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7268 | #if CONFIG_DUAL_FILTER |
| 7269 | mbmi->interp_filter[0] = filter_sets[i][0]; |
| 7270 | mbmi->interp_filter[1] = filter_sets[i][1]; |
| 7271 | mbmi->interp_filter[2] = filter_sets[i][0]; |
| 7272 | mbmi->interp_filter[3] = filter_sets[i][1]; |
| 7273 | #else |
| 7274 | mbmi->interp_filter = i; |
| 7275 | #endif |
| 7276 | tmp_rs = av1_get_switchable_rate(cpi, xd); |
| 7277 | av1_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); |
| 7278 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7279 | &tmp_dist, &tmp_skip_sb, &tmp_skip_sse); |
| 7280 | tmp_rd = RDCOST(x->rdmult, x->rddiv, tmp_rs + tmp_rate, tmp_dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7281 | |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7282 | if (tmp_rd < rd) { |
| 7283 | rd = tmp_rd; |
| 7284 | rs = av1_get_switchable_rate(cpi, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7285 | #if CONFIG_DUAL_FILTER |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7286 | av1_copy(best_filter, mbmi->interp_filter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7287 | #else |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7288 | best_filter = mbmi->interp_filter; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7289 | #endif |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7290 | skip_txfm_sb = tmp_skip_sb; |
| 7291 | skip_sse_sb = tmp_skip_sse; |
| 7292 | best_in_temp = !best_in_temp; |
| 7293 | if (best_in_temp) { |
| 7294 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
| 7295 | } else { |
| 7296 | restore_dst_buf(xd, tmp_dst, tmp_dst_stride); |
| 7297 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7298 | } |
| 7299 | } |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7300 | if (best_in_temp) { |
| 7301 | restore_dst_buf(xd, tmp_dst, tmp_dst_stride); |
| 7302 | } else { |
| 7303 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7304 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7305 | #if CONFIG_DUAL_FILTER |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7306 | av1_copy(mbmi->interp_filter, best_filter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7307 | #else |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7308 | mbmi->interp_filter = best_filter; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7309 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7310 | } else { |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7311 | #if !CONFIG_EXT_INTERP && !CONFIG_DUAL_FILTER |
| 7312 | int tmp_rs; |
| 7313 | InterpFilter best_filter = mbmi->interp_filter; |
| 7314 | rs = av1_get_switchable_rate(cpi, xd); |
| 7315 | for (i = 1; i < SWITCHABLE_FILTERS; ++i) { |
| 7316 | mbmi->interp_filter = i; |
| 7317 | tmp_rs = av1_get_switchable_rate(cpi, xd); |
| 7318 | if (tmp_rs < rs) { |
| 7319 | rs = tmp_rs; |
| 7320 | best_filter = i; |
| 7321 | } |
| 7322 | } |
| 7323 | mbmi->interp_filter = best_filter; |
| 7324 | #else |
| 7325 | assert(0); |
| 7326 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7327 | } |
| 7328 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7329 | } |
| 7330 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7331 | #if CONFIG_EXT_INTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7332 | #if CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7333 | best_bmc_mbmi = *mbmi; |
| 7334 | rate_mv_bmc = rate_mv; |
| 7335 | rate2_bmc_nocoeff = *rate2; |
| 7336 | if (cm->interp_filter == SWITCHABLE) rate2_bmc_nocoeff += rs; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7337 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7338 | |
| 7339 | if (is_comp_pred && is_interinter_wedge_used(bsize)) { |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 7340 | int rate_sum, rs2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7341 | int64_t dist_sum; |
| 7342 | int64_t best_rd_nowedge = INT64_MAX; |
| 7343 | int64_t best_rd_wedge = INT64_MAX; |
| 7344 | int tmp_skip_txfm_sb; |
| 7345 | int64_t tmp_skip_sse_sb; |
| 7346 | |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 7347 | rs2 = av1_cost_bit(cm->fc->wedge_interinter_prob[bsize], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7348 | mbmi->use_wedge_interinter = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7349 | av1_build_inter_predictors_sby(xd, mi_row, mi_col, bsize); |
| 7350 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7351 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 7352 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 7353 | if (rd != INT64_MAX) |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 7354 | rd = RDCOST(x->rdmult, x->rddiv, rs2 + rate_mv + rate_sum, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7355 | best_rd_nowedge = rd; |
| 7356 | |
| 7357 | // Disbale wedge search if source variance is small |
| 7358 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh && |
| 7359 | best_rd_nowedge / 3 < ref_best_rd) { |
| 7360 | uint8_t pred0[2 * MAX_SB_SQUARE]; |
| 7361 | uint8_t pred1[2 * MAX_SB_SQUARE]; |
| 7362 | uint8_t *preds0[1] = { pred0 }; |
| 7363 | uint8_t *preds1[1] = { pred1 }; |
| 7364 | int strides[1] = { bw }; |
| 7365 | |
| 7366 | mbmi->use_wedge_interinter = 1; |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 7367 | rs2 = av1_cost_literal(get_interinter_wedge_bits(bsize)) + |
| 7368 | av1_cost_bit(cm->fc->wedge_interinter_prob[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7369 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7370 | av1_build_inter_predictors_for_planes_single_buf( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7371 | xd, bsize, 0, 0, mi_row, mi_col, 0, preds0, strides); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7372 | av1_build_inter_predictors_for_planes_single_buf( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7373 | xd, bsize, 0, 0, mi_row, mi_col, 1, preds1, strides); |
| 7374 | |
| 7375 | // Choose the best wedge |
| 7376 | best_rd_wedge = pick_interinter_wedge(cpi, x, bsize, pred0, pred1); |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 7377 | best_rd_wedge += RDCOST(x->rdmult, x->rddiv, rs2 + rate_mv, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7378 | |
| 7379 | if (have_newmv_in_inter_mode(this_mode)) { |
| 7380 | int_mv tmp_mv[2]; |
| 7381 | int rate_mvs[2], tmp_rate_mv = 0; |
| 7382 | if (this_mode == NEW_NEWMV) { |
| 7383 | int mv_idxs[2] = { 0, 0 }; |
| 7384 | do_masked_motion_search_indexed( |
| 7385 | cpi, x, mbmi->interinter_wedge_index, mbmi->interinter_wedge_sign, |
| 7386 | bsize, mi_row, mi_col, tmp_mv, rate_mvs, mv_idxs, 2); |
| 7387 | tmp_rate_mv = rate_mvs[0] + rate_mvs[1]; |
| 7388 | mbmi->mv[0].as_int = tmp_mv[0].as_int; |
| 7389 | mbmi->mv[1].as_int = tmp_mv[1].as_int; |
| 7390 | } else if (this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV) { |
| 7391 | int mv_idxs[2] = { 0, 0 }; |
| 7392 | do_masked_motion_search_indexed( |
| 7393 | cpi, x, mbmi->interinter_wedge_index, mbmi->interinter_wedge_sign, |
| 7394 | bsize, mi_row, mi_col, tmp_mv, rate_mvs, mv_idxs, 0); |
| 7395 | tmp_rate_mv = rate_mvs[0]; |
| 7396 | mbmi->mv[0].as_int = tmp_mv[0].as_int; |
| 7397 | } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV) { |
| 7398 | int mv_idxs[2] = { 0, 0 }; |
| 7399 | do_masked_motion_search_indexed( |
| 7400 | cpi, x, mbmi->interinter_wedge_index, mbmi->interinter_wedge_sign, |
| 7401 | bsize, mi_row, mi_col, tmp_mv, rate_mvs, mv_idxs, 1); |
| 7402 | tmp_rate_mv = rate_mvs[1]; |
| 7403 | mbmi->mv[1].as_int = tmp_mv[1].as_int; |
| 7404 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7405 | av1_build_inter_predictors_sby(xd, mi_row, mi_col, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7406 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 7407 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 7408 | rd = |
| 7409 | RDCOST(x->rdmult, x->rddiv, rs2 + tmp_rate_mv + rate_sum, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7410 | if (rd < best_rd_wedge) { |
| 7411 | best_rd_wedge = rd; |
| 7412 | } else { |
| 7413 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 7414 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 7415 | tmp_rate_mv = rate_mv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7416 | av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0, |
| 7417 | strides, preds1, strides); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7418 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7419 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7420 | rd = |
| 7421 | estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 7422 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 7423 | if (rd != INT64_MAX) |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 7424 | rd = RDCOST(x->rdmult, x->rddiv, rs2 + tmp_rate_mv + rate_sum, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7425 | dist_sum); |
| 7426 | best_rd_wedge = rd; |
| 7427 | |
| 7428 | if (best_rd_wedge < best_rd_nowedge) { |
| 7429 | mbmi->use_wedge_interinter = 1; |
| 7430 | xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int; |
| 7431 | xd->mi[0]->bmi[0].as_mv[1].as_int = mbmi->mv[1].as_int; |
| 7432 | *rate2 += tmp_rate_mv - rate_mv; |
| 7433 | rate_mv = tmp_rate_mv; |
| 7434 | } else { |
| 7435 | mbmi->use_wedge_interinter = 0; |
| 7436 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 7437 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 7438 | xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int; |
| 7439 | xd->mi[0]->bmi[0].as_mv[1].as_int = mbmi->mv[1].as_int; |
| 7440 | } |
| 7441 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7442 | av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0, |
| 7443 | strides, preds1, strides); |
| 7444 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7445 | rd = |
| 7446 | estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 7447 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 7448 | if (rd != INT64_MAX) |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 7449 | rd = RDCOST(x->rdmult, x->rddiv, rs2 + rate_mv + rate_sum, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7450 | best_rd_wedge = rd; |
| 7451 | if (best_rd_wedge < best_rd_nowedge) { |
| 7452 | mbmi->use_wedge_interinter = 1; |
| 7453 | } else { |
| 7454 | mbmi->use_wedge_interinter = 0; |
| 7455 | } |
| 7456 | } |
| 7457 | } |
| 7458 | if (ref_best_rd < INT64_MAX && |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7459 | AOMMIN(best_rd_wedge, best_rd_nowedge) / 3 > ref_best_rd) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7460 | return INT64_MAX; |
| 7461 | |
| 7462 | pred_exists = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7463 | |
| 7464 | if (mbmi->use_wedge_interinter) |
| 7465 | *compmode_wedge_cost = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7466 | av1_cost_literal(get_interinter_wedge_bits(bsize)) + |
| 7467 | av1_cost_bit(cm->fc->wedge_interinter_prob[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7468 | else |
| 7469 | *compmode_wedge_cost = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7470 | av1_cost_bit(cm->fc->wedge_interinter_prob[bsize], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7471 | } |
| 7472 | |
| 7473 | if (is_comp_interintra_pred) { |
| 7474 | INTERINTRA_MODE best_interintra_mode = II_DC_PRED; |
| 7475 | int64_t best_interintra_rd = INT64_MAX; |
| 7476 | int rmode, rate_sum; |
| 7477 | int64_t dist_sum; |
| 7478 | int j; |
| 7479 | int64_t best_interintra_rd_nowedge = INT64_MAX; |
| 7480 | int64_t best_interintra_rd_wedge = INT64_MAX; |
| 7481 | int rwedge; |
| 7482 | int_mv tmp_mv; |
| 7483 | int tmp_rate_mv = 0; |
| 7484 | int tmp_skip_txfm_sb; |
| 7485 | int64_t tmp_skip_sse_sb; |
| 7486 | DECLARE_ALIGNED(16, uint8_t, intrapred_[2 * MAX_SB_SQUARE]); |
| 7487 | uint8_t *intrapred; |
| 7488 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7489 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7490 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 7491 | intrapred = CONVERT_TO_BYTEPTR(intrapred_); |
| 7492 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7493 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7494 | intrapred = intrapred_; |
| 7495 | |
| 7496 | mbmi->ref_frame[1] = NONE; |
| 7497 | for (j = 0; j < MAX_MB_PLANE; j++) { |
| 7498 | xd->plane[j].dst.buf = tmp_buf + j * MAX_SB_SQUARE; |
| 7499 | xd->plane[j].dst.stride = bw; |
| 7500 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7501 | av1_build_inter_predictors_sby(xd, mi_row, mi_col, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7502 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
| 7503 | mbmi->ref_frame[1] = INTRA_FRAME; |
| 7504 | mbmi->use_wedge_interintra = 0; |
| 7505 | |
| 7506 | for (j = 0; j < INTERINTRA_MODES; ++j) { |
| 7507 | mbmi->interintra_mode = (INTERINTRA_MODE)j; |
| 7508 | rmode = interintra_mode_cost[mbmi->interintra_mode]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7509 | av1_build_intra_predictors_for_interintra(xd, bsize, 0, intrapred, bw); |
| 7510 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7511 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 7512 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
| 7513 | rd = RDCOST(x->rdmult, x->rddiv, rs + tmp_rate_mv + rate_sum, dist_sum); |
| 7514 | if (rd < best_interintra_rd) { |
| 7515 | best_interintra_rd = rd; |
| 7516 | best_interintra_mode = mbmi->interintra_mode; |
| 7517 | } |
| 7518 | } |
| 7519 | mbmi->interintra_mode = best_interintra_mode; |
| 7520 | rmode = interintra_mode_cost[mbmi->interintra_mode]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7521 | av1_build_intra_predictors_for_interintra(xd, bsize, 0, intrapred, bw); |
| 7522 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
| 7523 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7524 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 7525 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 7526 | if (rd != INT64_MAX) |
| 7527 | rd = RDCOST(x->rdmult, x->rddiv, rate_mv + rmode + rate_sum, dist_sum); |
| 7528 | best_interintra_rd = rd; |
| 7529 | |
| 7530 | if (ref_best_rd < INT64_MAX && best_interintra_rd > 2 * ref_best_rd) { |
| 7531 | return INT64_MAX; |
| 7532 | } |
| 7533 | if (is_interintra_wedge_used(bsize)) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7534 | rwedge = av1_cost_bit(cm->fc->wedge_interintra_prob[bsize], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7535 | if (rd != INT64_MAX) |
| 7536 | rd = RDCOST(x->rdmult, x->rddiv, rmode + rate_mv + rwedge + rate_sum, |
| 7537 | dist_sum); |
| 7538 | best_interintra_rd_nowedge = rd; |
| 7539 | |
| 7540 | // Disbale wedge search if source variance is small |
| 7541 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh) { |
| 7542 | mbmi->use_wedge_interintra = 1; |
| 7543 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7544 | rwedge = av1_cost_literal(get_interintra_wedge_bits(bsize)) + |
| 7545 | av1_cost_bit(cm->fc->wedge_interintra_prob[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7546 | |
| 7547 | best_interintra_rd_wedge = |
| 7548 | pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_); |
| 7549 | |
| 7550 | best_interintra_rd_wedge += |
| 7551 | RDCOST(x->rdmult, x->rddiv, rmode + rate_mv + rwedge, 0); |
| 7552 | // Refine motion vector. |
| 7553 | if (have_newmv_in_inter_mode(this_mode)) { |
| 7554 | // get negative of mask |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7555 | const uint8_t *mask = av1_get_contiguous_soft_mask( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7556 | mbmi->interintra_wedge_index, 1, bsize); |
| 7557 | do_masked_motion_search(cpi, x, mask, bw, bsize, mi_row, mi_col, |
| 7558 | &tmp_mv, &tmp_rate_mv, 0, mv_idx); |
| 7559 | mbmi->mv[0].as_int = tmp_mv.as_int; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7560 | av1_build_inter_predictors_sby(xd, mi_row, mi_col, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7561 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 7562 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
| 7563 | rd = RDCOST(x->rdmult, x->rddiv, |
| 7564 | rmode + tmp_rate_mv + rwedge + rate_sum, dist_sum); |
| 7565 | if (rd < best_interintra_rd_wedge) { |
| 7566 | best_interintra_rd_wedge = rd; |
| 7567 | } else { |
| 7568 | tmp_mv.as_int = cur_mv[0].as_int; |
| 7569 | tmp_rate_mv = rate_mv; |
| 7570 | } |
| 7571 | } else { |
| 7572 | tmp_mv.as_int = cur_mv[0].as_int; |
| 7573 | tmp_rate_mv = rate_mv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7574 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7575 | } |
| 7576 | // Evaluate closer to true rd |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7577 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7578 | rd = |
| 7579 | estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 7580 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 7581 | if (rd != INT64_MAX) |
| 7582 | rd = RDCOST(x->rdmult, x->rddiv, |
| 7583 | rmode + tmp_rate_mv + rwedge + rate_sum, dist_sum); |
| 7584 | best_interintra_rd_wedge = rd; |
| 7585 | if (best_interintra_rd_wedge < best_interintra_rd_nowedge) { |
| 7586 | mbmi->use_wedge_interintra = 1; |
| 7587 | best_interintra_rd = best_interintra_rd_wedge; |
| 7588 | mbmi->mv[0].as_int = tmp_mv.as_int; |
| 7589 | *rate2 += tmp_rate_mv - rate_mv; |
| 7590 | rate_mv = tmp_rate_mv; |
| 7591 | } else { |
| 7592 | mbmi->use_wedge_interintra = 0; |
| 7593 | best_interintra_rd = best_interintra_rd_nowedge; |
| 7594 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 7595 | } |
| 7596 | } else { |
| 7597 | mbmi->use_wedge_interintra = 0; |
| 7598 | best_interintra_rd = best_interintra_rd_nowedge; |
| 7599 | } |
| 7600 | } |
| 7601 | |
| 7602 | pred_exists = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7603 | *compmode_interintra_cost = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7604 | av1_cost_bit(cm->fc->interintra_prob[size_group_lookup[bsize]], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7605 | *compmode_interintra_cost += interintra_mode_cost[mbmi->interintra_mode]; |
| 7606 | if (is_interintra_wedge_used(bsize)) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7607 | *compmode_interintra_cost += av1_cost_bit( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7608 | cm->fc->wedge_interintra_prob[bsize], mbmi->use_wedge_interintra); |
| 7609 | if (mbmi->use_wedge_interintra) { |
| 7610 | *compmode_interintra_cost += |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7611 | av1_cost_literal(get_interintra_wedge_bits(bsize)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7612 | } |
| 7613 | } |
| 7614 | } else if (is_interintra_allowed(mbmi)) { |
| 7615 | *compmode_interintra_cost = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7616 | av1_cost_bit(cm->fc->interintra_prob[size_group_lookup[bsize]], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7617 | } |
| 7618 | |
| 7619 | #if CONFIG_EXT_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7620 | if (!av1_is_interp_needed(xd) && cm->interp_filter == SWITCHABLE) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7621 | #if CONFIG_DUAL_FILTER |
| 7622 | for (i = 0; i < 4; ++i) mbmi->interp_filter[i] = EIGHTTAP_REGULAR; |
| 7623 | #else |
| 7624 | mbmi->interp_filter = EIGHTTAP_REGULAR; |
| 7625 | #endif |
| 7626 | pred_exists = 0; |
| 7627 | } |
| 7628 | #endif // CONFIG_EXT_INTERP |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7629 | if (pred_exists == 0) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7630 | int tmp_rate; |
| 7631 | int64_t tmp_dist; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7632 | av1_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7633 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7634 | &tmp_dist, &skip_txfm_sb, &skip_sse_sb); |
| 7635 | rd = RDCOST(x->rdmult, x->rddiv, rs + tmp_rate, tmp_dist); |
| 7636 | } |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7637 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7638 | |
| 7639 | #if CONFIG_DUAL_FILTER |
| 7640 | if (!is_comp_pred) single_filter[this_mode][refs[0]] = mbmi->interp_filter[0]; |
| 7641 | #else |
| 7642 | if (!is_comp_pred) single_filter[this_mode][refs[0]] = mbmi->interp_filter; |
| 7643 | #endif |
| 7644 | |
| 7645 | #if CONFIG_EXT_INTER |
| 7646 | if (modelled_rd != NULL) { |
| 7647 | if (is_comp_pred) { |
| 7648 | const int mode0 = compound_ref0_mode(this_mode); |
| 7649 | const int mode1 = compound_ref1_mode(this_mode); |
| 7650 | int64_t mrd = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7651 | AOMMIN(modelled_rd[mode0][refs[0]], modelled_rd[mode1][refs[1]]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7652 | if (rd / 4 * 3 > mrd && ref_best_rd < INT64_MAX) { |
| 7653 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
| 7654 | return INT64_MAX; |
| 7655 | } |
| 7656 | } else if (!is_comp_interintra_pred) { |
| 7657 | modelled_rd[this_mode][refs[0]] = rd; |
| 7658 | } |
| 7659 | } |
| 7660 | #endif // CONFIG_EXT_INTER |
| 7661 | |
| 7662 | if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) { |
| 7663 | // if current pred_error modeled rd is substantially more than the best |
| 7664 | // so far, do not bother doing full rd |
| 7665 | if (rd / 2 > ref_best_rd) { |
| 7666 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
| 7667 | return INT64_MAX; |
| 7668 | } |
| 7669 | } |
| 7670 | |
| 7671 | if (cm->interp_filter == SWITCHABLE) *rate2 += rs; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7672 | #if CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7673 | rate2_nocoeff = *rate2; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7674 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7675 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7676 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7677 | best_rd = INT64_MAX; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7678 | for (mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 7679 | mbmi->motion_mode < (allow_motvar ? MOTION_MODES : 1); |
| 7680 | mbmi->motion_mode++) { |
Angie Chiang | a2b56d3 | 2016-10-25 17:34:59 -0700 | [diff] [blame] | 7681 | int64_t tmp_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7682 | #if CONFIG_EXT_INTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7683 | int tmp_rate2 = mbmi->motion_mode != SIMPLE_TRANSLATION ? rate2_bmc_nocoeff |
| 7684 | : rate2_nocoeff; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7685 | #else |
| 7686 | int tmp_rate2 = rate2_nocoeff; |
| 7687 | #endif // CONFIG_EXT_INTER |
| 7688 | #if CONFIG_EXT_INTERP |
| 7689 | #if CONFIG_DUAL_FILTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 7690 | InterpFilter obmc_interp_filter[2][2] = { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7691 | { mbmi->interp_filter[0], mbmi->interp_filter[1] }, // obmc == 0 |
| 7692 | { mbmi->interp_filter[0], mbmi->interp_filter[1] } // obmc == 1 |
| 7693 | }; |
| 7694 | #else |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 7695 | InterpFilter obmc_interp_filter[2] = { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7696 | mbmi->interp_filter, // obmc == 0 |
| 7697 | mbmi->interp_filter // obmc == 1 |
| 7698 | }; |
| 7699 | #endif // CONFIG_DUAL_FILTER |
| 7700 | #endif // CONFIG_EXT_INTERP |
| 7701 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7702 | #if CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7703 | int tmp_rate; |
| 7704 | int64_t tmp_dist; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7705 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7706 | #if CONFIG_EXT_INTER |
| 7707 | *mbmi = best_bmc_mbmi; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7708 | mbmi->motion_mode = OBMC_CAUSAL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7709 | #endif // CONFIG_EXT_INTER |
| 7710 | if (!is_comp_pred && have_newmv_in_inter_mode(this_mode)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7711 | int tmp_rate_mv = 0; |
| 7712 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7713 | single_motion_search(cpi, x, bsize, mi_row, mi_col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7714 | #if CONFIG_EXT_INTER |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7715 | 0, mv_idx, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7716 | #endif // CONFIG_EXT_INTER |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7717 | &tmp_rate_mv); |
| 7718 | mbmi->mv[0].as_int = x->best_mv.as_int; |
| 7719 | if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv, |
| 7720 | refs[0])) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7721 | tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7722 | } |
| 7723 | #if CONFIG_EXT_INTER |
| 7724 | tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv; |
| 7725 | #else |
| 7726 | tmp_rate2 = rate2_nocoeff - rate_mv + tmp_rate_mv; |
| 7727 | #endif // CONFIG_EXT_INTER |
| 7728 | #if CONFIG_EXT_INTERP |
| 7729 | #if CONFIG_DUAL_FILTER |
| 7730 | if (!has_subpel_mv_component(xd->mi[0], xd, 0)) |
| 7731 | obmc_interp_filter[1][0] = mbmi->interp_filter[0] = EIGHTTAP_REGULAR; |
| 7732 | if (!has_subpel_mv_component(xd->mi[0], xd, 1)) |
| 7733 | obmc_interp_filter[1][1] = mbmi->interp_filter[1] = EIGHTTAP_REGULAR; |
| 7734 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7735 | if (!av1_is_interp_needed(xd)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7736 | obmc_interp_filter[1] = mbmi->interp_filter = EIGHTTAP_REGULAR; |
| 7737 | #endif // CONFIG_DUAL_FILTER |
| 7738 | // This is not quite correct with CONFIG_DUAL_FILTER when a filter |
| 7739 | // is needed in only one direction |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7740 | if (!av1_is_interp_needed(xd)) tmp_rate2 -= rs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7741 | #endif // CONFIG_EXT_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7742 | av1_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7743 | #if CONFIG_EXT_INTER |
| 7744 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7745 | av1_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7746 | #endif // CONFIG_EXT_INTER |
| 7747 | } |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7748 | av1_build_obmc_inter_prediction(cm, xd, mi_row, mi_col, above_pred_buf, |
| 7749 | above_pred_stride, left_pred_buf, |
| 7750 | left_pred_stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7751 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7752 | &tmp_dist, &skip_txfm_sb, &skip_sse_sb); |
| 7753 | } |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7754 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7755 | |
| 7756 | #if CONFIG_WARPED_MOTION |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7757 | if (mbmi->motion_mode == WARPED_CAUSAL) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7758 | // TODO(yuec): Add code |
| 7759 | } |
| 7760 | #endif // CONFIG_WARPED_MOTION |
| 7761 | x->skip = 0; |
| 7762 | |
| 7763 | *rate2 = tmp_rate2; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7764 | if (allow_motvar) *rate2 += cpi->motion_mode_cost[bsize][mbmi->motion_mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7765 | *distortion = 0; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7766 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7767 | if (!skip_txfm_sb) { |
| 7768 | int skippable_y, skippable_uv; |
| 7769 | int64_t sseuv = INT64_MAX; |
| 7770 | int64_t rdcosty = INT64_MAX; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 7771 | int is_cost_valid_uv = 0; |
| 7772 | #if CONFIG_VAR_TX |
| 7773 | RD_STATS rd_stats_uv; |
| 7774 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7775 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 7776 | // Y cost and distortion |
| 7777 | #if !CONFIG_PVQ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7778 | av1_subtract_plane(x, bsize, 0); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 7779 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7780 | #if CONFIG_VAR_TX |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 7781 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 7782 | RD_STATS rd_stats_y; |
| 7783 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, ref_best_rd); |
| 7784 | *rate_y = rd_stats_y.rate; |
| 7785 | distortion_y = rd_stats_y.dist; |
| 7786 | skippable_y = rd_stats_y.skip; |
| 7787 | *psse = rd_stats_y.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7788 | } else { |
| 7789 | int idx, idy; |
| 7790 | super_block_yrd(cpi, x, rate_y, &distortion_y, &skippable_y, psse, |
| 7791 | bsize, ref_best_rd); |
| 7792 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 7793 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 7794 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 7795 | memset(x->blk_skip[0], skippable_y, |
| 7796 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 7797 | } |
| 7798 | #else |
| 7799 | super_block_yrd(cpi, x, rate_y, &distortion_y, &skippable_y, psse, bsize, |
| 7800 | ref_best_rd); |
| 7801 | #endif // CONFIG_VAR_TX |
| 7802 | |
| 7803 | if (*rate_y == INT_MAX) { |
| 7804 | *rate2 = INT_MAX; |
| 7805 | *distortion = INT64_MAX; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7806 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 7807 | if (mbmi->motion_mode != SIMPLE_TRANSLATION) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7808 | continue; |
| 7809 | } else { |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7810 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7811 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
| 7812 | return INT64_MAX; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7813 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7814 | } |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7815 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7816 | } |
| 7817 | |
| 7818 | *rate2 += *rate_y; |
| 7819 | *distortion += distortion_y; |
| 7820 | |
| 7821 | rdcosty = RDCOST(x->rdmult, x->rddiv, *rate2, *distortion); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7822 | rdcosty = AOMMIN(rdcosty, RDCOST(x->rdmult, x->rddiv, 0, *psse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7823 | |
| 7824 | #if CONFIG_VAR_TX |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 7825 | is_cost_valid_uv = |
| 7826 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, ref_best_rd - rdcosty); |
Angie Chiang | 628d7c9 | 2016-11-03 16:24:56 -0700 | [diff] [blame] | 7827 | #if CONFIG_RD_DEBUG |
| 7828 | // record uv planes' transform block coefficient cost |
| 7829 | mbmi->txb_coeff_cost[1] = rd_stats_uv.txb_coeff_cost[1]; |
| 7830 | mbmi->txb_coeff_cost[2] = rd_stats_uv.txb_coeff_cost[2]; |
| 7831 | #endif |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 7832 | *rate_uv = rd_stats_uv.rate; |
| 7833 | distortion_uv = rd_stats_uv.dist; |
| 7834 | skippable_uv = rd_stats_uv.skip; |
| 7835 | sseuv = rd_stats_uv.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7836 | #else |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 7837 | is_cost_valid_uv = |
| 7838 | super_block_uvrd(cpi, x, rate_uv, &distortion_uv, &skippable_uv, |
| 7839 | &sseuv, bsize, ref_best_rd - rdcosty); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7840 | #endif // CONFIG_VAR_TX |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 7841 | if (!is_cost_valid_uv) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7842 | *rate2 = INT_MAX; |
| 7843 | *distortion = INT64_MAX; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7844 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7845 | continue; |
| 7846 | #else |
Jingning Han | 137b267 | 2016-11-04 08:31:24 -0700 | [diff] [blame] | 7847 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
| 7848 | return INT64_MAX; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7849 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7850 | } |
| 7851 | |
| 7852 | *psse += sseuv; |
| 7853 | *rate2 += *rate_uv; |
| 7854 | *distortion += distortion_uv; |
| 7855 | *skippable = skippable_y && skippable_uv; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7856 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7857 | if (*skippable) { |
| 7858 | *rate2 -= *rate_uv + *rate_y; |
| 7859 | *rate_y = 0; |
| 7860 | *rate_uv = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7861 | *rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7862 | mbmi->skip = 0; |
| 7863 | // here mbmi->skip temporarily plays a role as what this_skip2 does |
| 7864 | } else if (!xd->lossless[mbmi->segment_id] && |
| 7865 | (RDCOST(x->rdmult, x->rddiv, |
| 7866 | *rate_y + *rate_uv + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7867 | av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7868 | *distortion) >= |
| 7869 | RDCOST(x->rdmult, x->rddiv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7870 | av1_cost_bit(av1_get_skip_prob(cm, xd), 1), *psse))) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7871 | *rate2 -= *rate_uv + *rate_y; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7872 | *rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7873 | *distortion = *psse; |
| 7874 | *rate_y = 0; |
| 7875 | *rate_uv = 0; |
| 7876 | mbmi->skip = 1; |
| 7877 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7878 | *rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7879 | mbmi->skip = 0; |
| 7880 | } |
| 7881 | *disable_skip = 0; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7882 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7883 | } else { |
| 7884 | x->skip = 1; |
| 7885 | *disable_skip = 1; |
Debargha Mukherjee | 28d924b | 2016-10-05 00:48:28 -0700 | [diff] [blame] | 7886 | mbmi->tx_size = tx_size_from_tx_mode(bsize, cm->tx_mode, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7887 | |
| 7888 | // The cost of skip bit needs to be added. |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7889 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7890 | mbmi->skip = 0; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7891 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7892 | *rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7893 | |
| 7894 | *distortion = skip_sse_sb; |
| 7895 | *psse = skip_sse_sb; |
| 7896 | *rate_y = 0; |
| 7897 | *rate_uv = 0; |
| 7898 | *skippable = 1; |
| 7899 | } |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 7900 | #if CONFIG_GLOBAL_MOTION |
| 7901 | if (this_mode == ZEROMV) { |
| 7902 | *rate2 += GLOBAL_MOTION_RATE(mbmi->ref_frame[0]); |
| 7903 | if (is_comp_pred) *rate2 += GLOBAL_MOTION_RATE(mbmi->ref_frame[1]); |
| 7904 | } |
| 7905 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7906 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7907 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7908 | tmp_rd = RDCOST(x->rdmult, x->rddiv, *rate2, *distortion); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7909 | if (mbmi->motion_mode == SIMPLE_TRANSLATION || (tmp_rd < best_rd)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7910 | #if CONFIG_EXT_INTERP |
| 7911 | #if CONFIG_DUAL_FILTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7912 | mbmi->interp_filter[0] = obmc_interp_filter[mbmi->motion_mode][0]; |
| 7913 | mbmi->interp_filter[1] = obmc_interp_filter[mbmi->motion_mode][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7914 | #else |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7915 | mbmi->interp_filter = obmc_interp_filter[mbmi->motion_mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7916 | #endif // CONFIG_DUAL_FILTER |
| 7917 | #endif // CONFIG_EXT_INTERP |
| 7918 | best_mbmi = *mbmi; |
| 7919 | best_rd = tmp_rd; |
| 7920 | best_rate2 = *rate2; |
| 7921 | best_rate_y = *rate_y; |
| 7922 | best_rate_uv = *rate_uv; |
| 7923 | #if CONFIG_VAR_TX |
| 7924 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 7925 | memcpy(best_blk_skip[i], x->blk_skip[i], |
| 7926 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 7927 | #endif // CONFIG_VAR_TX |
| 7928 | best_distortion = *distortion; |
| 7929 | best_skippable = *skippable; |
| 7930 | best_xskip = x->skip; |
| 7931 | best_disable_skip = *disable_skip; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7932 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7933 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7934 | x->recon_variance = av1_high_get_sby_perpixel_variance( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7935 | cpi, &xd->plane[0].dst, bsize, xd->bd); |
| 7936 | } else { |
| 7937 | x->recon_variance = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7938 | av1_get_sby_perpixel_variance(cpi, &xd->plane[0].dst, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7939 | } |
| 7940 | #else |
| 7941 | x->recon_variance = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7942 | av1_get_sby_perpixel_variance(cpi, &xd->plane[0].dst, bsize); |
| 7943 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7944 | } |
| 7945 | } |
| 7946 | |
| 7947 | if (best_rd == INT64_MAX) { |
| 7948 | *rate2 = INT_MAX; |
| 7949 | *distortion = INT64_MAX; |
| 7950 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
| 7951 | return INT64_MAX; |
| 7952 | } |
| 7953 | *mbmi = best_mbmi; |
| 7954 | *rate2 = best_rate2; |
| 7955 | *rate_y = best_rate_y; |
| 7956 | *rate_uv = best_rate_uv; |
| 7957 | #if CONFIG_VAR_TX |
| 7958 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 7959 | memcpy(x->blk_skip[i], best_blk_skip[i], |
| 7960 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 7961 | #endif // CONFIG_VAR_TX |
| 7962 | *distortion = best_distortion; |
| 7963 | *skippable = best_skippable; |
| 7964 | x->skip = best_xskip; |
| 7965 | *disable_skip = best_disable_skip; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7966 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7967 | |
| 7968 | if (!is_comp_pred) single_skippable[this_mode][refs[0]] = *skippable; |
| 7969 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7970 | #if !(CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7971 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7972 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7973 | x->recon_variance = av1_high_get_sby_perpixel_variance( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7974 | cpi, &xd->plane[0].dst, bsize, xd->bd); |
| 7975 | } else { |
| 7976 | x->recon_variance = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7977 | av1_get_sby_perpixel_variance(cpi, &xd->plane[0].dst, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7978 | } |
| 7979 | #else |
| 7980 | x->recon_variance = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7981 | av1_get_sby_perpixel_variance(cpi, &xd->plane[0].dst, bsize); |
| 7982 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 7983 | #endif // !(CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7984 | |
| 7985 | restore_dst_buf(xd, orig_dst, orig_dst_stride); |
| 7986 | return 0; // The rate-distortion cost will be re-calculated by caller. |
| 7987 | } |
| 7988 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 7989 | void av1_rd_pick_intra_mode_sb(const AV1_COMP *cpi, MACROBLOCK *x, |
| 7990 | RD_COST *rd_cost, BLOCK_SIZE bsize, |
| 7991 | PICK_MODE_CONTEXT *ctx, int64_t best_rd) { |
| 7992 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7993 | MACROBLOCKD *const xd = &x->e_mbd; |
| 7994 | struct macroblockd_plane *const pd = xd->plane; |
| 7995 | int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0; |
| 7996 | int y_skip = 0, uv_skip = 0; |
| 7997 | int64_t dist_y = 0, dist_uv = 0; |
| 7998 | TX_SIZE max_uv_tx_size; |
| 7999 | ctx->skip = 0; |
| 8000 | xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME; |
| 8001 | xd->mi[0]->mbmi.ref_frame[1] = NONE; |
| 8002 | |
| 8003 | if (bsize >= BLOCK_8X8) { |
| 8004 | if (rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly, &dist_y, |
| 8005 | &y_skip, bsize, best_rd) >= best_rd) { |
| 8006 | rd_cost->rate = INT_MAX; |
| 8007 | return; |
| 8008 | } |
| 8009 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8010 | if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly, |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 8011 | &dist_y, &y_skip, best_rd) >= best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8012 | rd_cost->rate = INT_MAX; |
| 8013 | return; |
| 8014 | } |
| 8015 | } |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 8016 | max_uv_tx_size = uv_txsize_lookup[bsize][xd->mi[0]->mbmi.tx_size] |
| 8017 | [pd[1].subsampling_x][pd[1].subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8018 | rd_pick_intra_sbuv_mode(cpi, x, &rate_uv, &rate_uv_tokenonly, &dist_uv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8019 | &uv_skip, AOMMAX(BLOCK_8X8, bsize), max_uv_tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8020 | |
| 8021 | if (y_skip && uv_skip) { |
| 8022 | rd_cost->rate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8023 | av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8024 | rd_cost->dist = dist_y + dist_uv; |
| 8025 | } else { |
| 8026 | rd_cost->rate = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8027 | rate_y + rate_uv + av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8028 | rd_cost->dist = dist_y + dist_uv; |
| 8029 | } |
| 8030 | |
| 8031 | ctx->mic = *xd->mi[0]; |
| 8032 | ctx->mbmi_ext = *x->mbmi_ext; |
| 8033 | rd_cost->rdcost = RDCOST(x->rdmult, x->rddiv, rd_cost->rate, rd_cost->dist); |
| 8034 | } |
| 8035 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8036 | // Do we have an internal image edge (e.g. formatting bars). |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8037 | int av1_internal_image_edge(const AV1_COMP *cpi) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8038 | return (cpi->oxcf.pass == 2) && |
| 8039 | ((cpi->twopass.this_frame_stats.inactive_zone_rows > 0) || |
| 8040 | (cpi->twopass.this_frame_stats.inactive_zone_cols > 0)); |
| 8041 | } |
| 8042 | |
| 8043 | // Checks to see if a super block is on a horizontal image edge. |
| 8044 | // In most cases this is the "real" edge unless there are formatting |
| 8045 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8046 | int av1_active_h_edge(const AV1_COMP *cpi, int mi_row, int mi_step) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8047 | int top_edge = 0; |
| 8048 | int bottom_edge = cpi->common.mi_rows; |
| 8049 | int is_active_h_edge = 0; |
| 8050 | |
| 8051 | // For two pass account for any formatting bars detected. |
| 8052 | if (cpi->oxcf.pass == 2) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8053 | const TWO_PASS *const twopass = &cpi->twopass; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8054 | |
| 8055 | // The inactive region is specified in MBs not mi units. |
| 8056 | // The image edge is in the following MB row. |
| 8057 | top_edge += (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
| 8058 | |
| 8059 | bottom_edge -= (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8060 | bottom_edge = AOMMAX(top_edge, bottom_edge); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8061 | } |
| 8062 | |
| 8063 | if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) || |
| 8064 | ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) { |
| 8065 | is_active_h_edge = 1; |
| 8066 | } |
| 8067 | return is_active_h_edge; |
| 8068 | } |
| 8069 | |
| 8070 | // Checks to see if a super block is on a vertical image edge. |
| 8071 | // In most cases this is the "real" edge unless there are formatting |
| 8072 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8073 | int av1_active_v_edge(const AV1_COMP *cpi, int mi_col, int mi_step) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8074 | int left_edge = 0; |
| 8075 | int right_edge = cpi->common.mi_cols; |
| 8076 | int is_active_v_edge = 0; |
| 8077 | |
| 8078 | // For two pass account for any formatting bars detected. |
| 8079 | if (cpi->oxcf.pass == 2) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8080 | const TWO_PASS *const twopass = &cpi->twopass; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8081 | |
| 8082 | // The inactive region is specified in MBs not mi units. |
| 8083 | // The image edge is in the following MB row. |
| 8084 | left_edge += (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
| 8085 | |
| 8086 | right_edge -= (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8087 | right_edge = AOMMAX(left_edge, right_edge); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) || |
| 8091 | ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) { |
| 8092 | is_active_v_edge = 1; |
| 8093 | } |
| 8094 | return is_active_v_edge; |
| 8095 | } |
| 8096 | |
| 8097 | // Checks to see if a super block is at the edge of the active image. |
| 8098 | // In most cases this is the "real" edge unless there are formatting |
| 8099 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8100 | int av1_active_edge_sb(const AV1_COMP *cpi, int mi_row, int mi_col) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8101 | return av1_active_h_edge(cpi, mi_row, cpi->common.mib_size) || |
| 8102 | av1_active_v_edge(cpi, mi_col, cpi->common.mib_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8103 | } |
| 8104 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8105 | #if CONFIG_PALETTE |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8106 | static void restore_uv_color_map(const AV1_COMP *const cpi, MACROBLOCK *x) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8107 | MACROBLOCKD *const xd = &x->e_mbd; |
| 8108 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8109 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 8110 | const BLOCK_SIZE bsize = mbmi->sb_type; |
| 8111 | const int rows = |
| 8112 | (4 * num_4x4_blocks_high_lookup[bsize]) >> (xd->plane[1].subsampling_y); |
| 8113 | const int cols = |
| 8114 | (4 * num_4x4_blocks_wide_lookup[bsize]) >> (xd->plane[1].subsampling_x); |
| 8115 | int src_stride = x->plane[1].src.stride; |
| 8116 | const uint8_t *const src_u = x->plane[1].src.buf; |
| 8117 | const uint8_t *const src_v = x->plane[2].src.buf; |
| 8118 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 8119 | float centroids[2 * PALETTE_MAX_SIZE]; |
| 8120 | uint8_t *const color_map = xd->plane[1].color_index_map; |
| 8121 | int r, c; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8122 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8123 | const uint16_t *const src_u16 = CONVERT_TO_SHORTPTR(src_u); |
| 8124 | const uint16_t *const src_v16 = CONVERT_TO_SHORTPTR(src_v); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8125 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8126 | (void)cpi; |
| 8127 | |
| 8128 | for (r = 0; r < rows; ++r) { |
| 8129 | for (c = 0; c < cols; ++c) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8130 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8131 | if (cpi->common.use_highbitdepth) { |
| 8132 | data[(r * cols + c) * 2] = src_u16[r * src_stride + c]; |
| 8133 | data[(r * cols + c) * 2 + 1] = src_v16[r * src_stride + c]; |
| 8134 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8135 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8136 | data[(r * cols + c) * 2] = src_u[r * src_stride + c]; |
| 8137 | data[(r * cols + c) * 2 + 1] = src_v[r * src_stride + c]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8138 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8139 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8140 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8141 | } |
| 8142 | } |
| 8143 | |
| 8144 | for (r = 1; r < 3; ++r) { |
| 8145 | for (c = 0; c < pmi->palette_size[1]; ++c) { |
| 8146 | centroids[c * 2 + r - 1] = pmi->palette_colors[r * PALETTE_MAX_SIZE + c]; |
| 8147 | } |
| 8148 | } |
| 8149 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8150 | av1_calc_indices(data, centroids, color_map, rows * cols, |
| 8151 | pmi->palette_size[1], 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8152 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8153 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8154 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8155 | #if CONFIG_FILTER_INTRA |
| 8156 | static void pick_filter_intra_interframe( |
| 8157 | const AV1_COMP *cpi, MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8158 | BLOCK_SIZE bsize, int *rate_uv_intra, int *rate_uv_tokenonly, |
| 8159 | int64_t *dist_uv, int *skip_uv, PREDICTION_MODE *mode_uv, |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8160 | FILTER_INTRA_MODE_INFO *filter_intra_mode_info_uv, |
| 8161 | #if CONFIG_EXT_INTRA |
| 8162 | int8_t *uv_angle_delta, |
| 8163 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8164 | #if CONFIG_PALETTE |
| 8165 | PALETTE_MODE_INFO *pmi_uv, int palette_ctx, |
| 8166 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8167 | int skip_mask, unsigned int *ref_costs_single, int64_t *best_rd, |
| 8168 | int64_t *best_intra_rd, PREDICTION_MODE *best_intra_mode, |
| 8169 | int *best_mode_index, int *best_skip2, int *best_mode_skippable, |
| 8170 | #if CONFIG_SUPERTX |
| 8171 | int *returnrate_nocoef, |
| 8172 | #endif // CONFIG_SUPERTX |
| 8173 | int64_t *best_pred_rd, MB_MODE_INFO *best_mbmode, RD_COST *rd_cost) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8174 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8175 | MACROBLOCKD *const xd = &x->e_mbd; |
| 8176 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8177 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8178 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8179 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8180 | const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; |
| 8181 | int rate2 = 0, rate_y = INT_MAX, skippable = 0, rate_uv, rate_dummy, i; |
| 8182 | int dc_mode_index; |
| 8183 | const int *const intra_mode_cost = cpi->mbmode_cost[size_group_lookup[bsize]]; |
| 8184 | int64_t distortion2 = 0, distortion_y = 0, this_rd = *best_rd, distortion_uv; |
| 8185 | TX_SIZE uv_tx; |
| 8186 | |
| 8187 | for (i = 0; i < MAX_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8188 | if (av1_mode_order[i].mode == DC_PRED && |
| 8189 | av1_mode_order[i].ref_frame[0] == INTRA_FRAME) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8190 | break; |
| 8191 | dc_mode_index = i; |
| 8192 | assert(i < MAX_MODES); |
| 8193 | |
| 8194 | // TODO(huisu): use skip_mask for further speedup. |
| 8195 | (void)skip_mask; |
| 8196 | mbmi->mode = DC_PRED; |
| 8197 | mbmi->uv_mode = DC_PRED; |
| 8198 | mbmi->ref_frame[0] = INTRA_FRAME; |
| 8199 | mbmi->ref_frame[1] = NONE; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8200 | if (!rd_pick_filter_intra_sby(cpi, x, &rate_dummy, &rate_y, &distortion_y, |
| 8201 | &skippable, bsize, intra_mode_cost[mbmi->mode], |
| 8202 | &this_rd, 0)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8203 | return; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8204 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8205 | if (rate_y == INT_MAX) return; |
| 8206 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 8207 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x] |
| 8208 | [xd->plane[1].subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8209 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
| 8210 | choose_intra_uv_mode(cpi, x, ctx, bsize, uv_tx, &rate_uv_intra[uv_tx], |
| 8211 | &rate_uv_tokenonly[uv_tx], &dist_uv[uv_tx], |
| 8212 | &skip_uv[uv_tx], &mode_uv[uv_tx]); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8213 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8214 | if (cm->allow_screen_content_tools) pmi_uv[uv_tx] = *pmi; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8215 | #endif // CONFIG_PALETTE |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8216 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 8217 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8218 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8219 | #endif // CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8220 | } |
| 8221 | |
| 8222 | rate_uv = rate_uv_tokenonly[uv_tx]; |
| 8223 | distortion_uv = dist_uv[uv_tx]; |
| 8224 | skippable = skippable && skip_uv[uv_tx]; |
| 8225 | mbmi->uv_mode = mode_uv[uv_tx]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8226 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8227 | if (cm->allow_screen_content_tools) { |
| 8228 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
| 8229 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 8230 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 8231 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
| 8232 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8233 | #endif // CONFIG_PALETTE |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8234 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8235 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8236 | #endif // CONFIG_EXT_INTRA |
| 8237 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 8238 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 8239 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 8240 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 8241 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8242 | } |
| 8243 | |
| 8244 | rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv + |
| 8245 | cpi->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8246 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8247 | if (cpi->common.allow_screen_content_tools && mbmi->mode == DC_PRED) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8248 | rate2 += av1_cost_bit( |
| 8249 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8250 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8251 | |
| 8252 | if (!xd->lossless[mbmi->segment_id]) { |
| 8253 | // super_block_yrd above includes the cost of the tx_size in the |
| 8254 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 8255 | // (prediction granularity), so we account for it in the full rate, |
| 8256 | // not the tokenonly rate. |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 8257 | rate_y -= cpi->tx_size_cost[max_tx_size - TX_8X8][get_tx_size_context(xd)] |
Jingning Han | b0a7130 | 2016-10-25 16:28:49 -0700 | [diff] [blame] | 8258 | [tx_size_to_depth(mbmi->tx_size)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8259 | } |
| 8260 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8261 | rate2 += av1_cost_bit(cm->fc->filter_intra_probs[0], |
| 8262 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0]); |
| 8263 | rate2 += write_uniform_cost( |
| 8264 | FILTER_INTRA_MODES, mbmi->filter_intra_mode_info.filter_intra_mode[0]); |
| 8265 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8266 | if (mbmi->uv_mode != DC_PRED && mbmi->uv_mode != TM_PRED) { |
| 8267 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1, |
| 8268 | MAX_ANGLE_DELTAS + mbmi->angle_delta[1]); |
| 8269 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8270 | #endif // CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8271 | if (mbmi->mode == DC_PRED) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8272 | rate2 += |
| 8273 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], |
| 8274 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1]); |
| 8275 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1]) |
| 8276 | rate2 += |
| 8277 | write_uniform_cost(FILTER_INTRA_MODES, |
| 8278 | mbmi->filter_intra_mode_info.filter_intra_mode[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8279 | } |
| 8280 | distortion2 = distortion_y + distortion_uv; |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 8281 | av1_encode_intra_block_plane((AV1_COMMON *)cm, x, bsize, 0, 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8282 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8283 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8284 | x->recon_variance = av1_high_get_sby_perpixel_variance( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8285 | cpi, &xd->plane[0].dst, bsize, xd->bd); |
| 8286 | } else { |
| 8287 | x->recon_variance = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8288 | av1_get_sby_perpixel_variance(cpi, &xd->plane[0].dst, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8289 | } |
| 8290 | #else |
| 8291 | x->recon_variance = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8292 | av1_get_sby_perpixel_variance(cpi, &xd->plane[0].dst, bsize); |
| 8293 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8294 | |
| 8295 | rate2 += ref_costs_single[INTRA_FRAME]; |
| 8296 | |
| 8297 | if (skippable) { |
| 8298 | rate2 -= (rate_y + rate_uv); |
| 8299 | rate_y = 0; |
| 8300 | rate_uv = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8301 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8302 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8303 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8304 | } |
| 8305 | this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8306 | |
| 8307 | if (this_rd < *best_intra_rd) { |
| 8308 | *best_intra_rd = this_rd; |
| 8309 | *best_intra_mode = mbmi->mode; |
| 8310 | } |
| 8311 | for (i = 0; i < REFERENCE_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8312 | best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8313 | |
| 8314 | if (this_rd < *best_rd) { |
| 8315 | *best_mode_index = dc_mode_index; |
| 8316 | mbmi->mv[0].as_int = 0; |
| 8317 | rd_cost->rate = rate2; |
| 8318 | #if CONFIG_SUPERTX |
| 8319 | if (x->skip) |
| 8320 | *returnrate_nocoef = rate2; |
| 8321 | else |
| 8322 | *returnrate_nocoef = rate2 - rate_y - rate_uv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8323 | *returnrate_nocoef -= av1_cost_bit(av1_get_skip_prob(cm, xd), skippable); |
| 8324 | *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd), |
| 8325 | mbmi->ref_frame[0] != INTRA_FRAME); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8326 | #endif // CONFIG_SUPERTX |
| 8327 | rd_cost->dist = distortion2; |
| 8328 | rd_cost->rdcost = this_rd; |
| 8329 | *best_rd = this_rd; |
| 8330 | *best_mbmode = *mbmi; |
| 8331 | *best_skip2 = 0; |
| 8332 | *best_mode_skippable = skippable; |
| 8333 | } |
| 8334 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8335 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8336 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8337 | #if CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8338 | static void calc_target_weighted_pred(const AV1_COMMON *cm, const MACROBLOCK *x, |
| 8339 | const MACROBLOCKD *xd, int mi_row, |
| 8340 | int mi_col, const uint8_t *above, |
| 8341 | int above_stride, const uint8_t *left, |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 8342 | int left_stride); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8343 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8344 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8345 | void av1_rd_pick_inter_mode_sb(const AV1_COMP *cpi, TileDataEnc *tile_data, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8346 | MACROBLOCK *x, int mi_row, int mi_col, |
| 8347 | RD_COST *rd_cost, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8348 | #if CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8349 | int *returnrate_nocoef, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8350 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8351 | BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, |
| 8352 | int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8353 | const AV1_COMMON *const cm = &cpi->common; |
| 8354 | const RD_OPT *const rd_opt = &cpi->rd; |
| 8355 | const SPEED_FEATURES *const sf = &cpi->sf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8356 | MACROBLOCKD *const xd = &x->e_mbd; |
| 8357 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8358 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8359 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8360 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8361 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 8362 | const struct segmentation *const seg = &cm->seg; |
| 8363 | PREDICTION_MODE this_mode; |
| 8364 | MV_REFERENCE_FRAME ref_frame, second_ref_frame; |
| 8365 | unsigned char segment_id = mbmi->segment_id; |
| 8366 | int comp_pred, i, k; |
| 8367 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 8368 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]; |
| 8369 | #if CONFIG_EXT_INTER |
| 8370 | int_mv single_newmvs[2][TOTAL_REFS_PER_FRAME] = { { { 0 } }, { { 0 } } }; |
| 8371 | int single_newmvs_rate[2][TOTAL_REFS_PER_FRAME] = { { 0 }, { 0 } }; |
| 8372 | int64_t modelled_rd[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 8373 | #else |
| 8374 | int_mv single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } }; |
| 8375 | #endif // CONFIG_EXT_INTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 8376 | InterpFilter single_inter_filter[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8377 | int single_skippable[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 8378 | static const int flag_list[TOTAL_REFS_PER_FRAME] = { |
| 8379 | 0, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8380 | AOM_LAST_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8381 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8382 | AOM_LAST2_FLAG, |
| 8383 | AOM_LAST3_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8384 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8385 | AOM_GOLD_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8386 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8387 | AOM_BWD_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8388 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8389 | AOM_ALT_FLAG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8390 | }; |
| 8391 | int64_t best_rd = best_rd_so_far; |
| 8392 | int best_rate_y = INT_MAX, best_rate_uv = INT_MAX; |
| 8393 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 8394 | int64_t best_pred_rd[REFERENCE_MODES]; |
| 8395 | MB_MODE_INFO best_mbmode; |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 8396 | #if CONFIG_REF_MV |
| 8397 | int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 8398 | int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 8399 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8400 | int best_mode_skippable = 0; |
| 8401 | int midx, best_mode_index = -1; |
| 8402 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
| 8403 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8404 | aom_prob comp_mode_p; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8405 | int64_t best_intra_rd = INT64_MAX; |
| 8406 | unsigned int best_pred_sse = UINT_MAX; |
| 8407 | PREDICTION_MODE best_intra_mode = DC_PRED; |
| 8408 | int rate_uv_intra[TX_SIZES], rate_uv_tokenonly[TX_SIZES]; |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 8409 | int64_t dist_uvs[TX_SIZES]; |
| 8410 | int skip_uvs[TX_SIZES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8411 | PREDICTION_MODE mode_uv[TX_SIZES]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8412 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8413 | PALETTE_MODE_INFO pmi_uv[TX_SIZES]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8414 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8415 | #if CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8416 | int8_t uv_angle_delta[TX_SIZES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8417 | int is_directional_mode, angle_stats_ready = 0; |
| 8418 | int rate_overhead, rate_dummy; |
| 8419 | uint8_t directional_mode_skip_mask[INTRA_MODES]; |
| 8420 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8421 | #if CONFIG_FILTER_INTRA |
| 8422 | int8_t dc_skipped = 1; |
| 8423 | FILTER_INTRA_MODE_INFO filter_intra_mode_info_uv[TX_SIZES]; |
| 8424 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8425 | const int intra_cost_penalty = av1_get_intra_cost_penalty( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8426 | cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth); |
| 8427 | const int *const intra_mode_cost = cpi->mbmode_cost[size_group_lookup[bsize]]; |
| 8428 | int best_skip2 = 0; |
| 8429 | uint8_t ref_frame_skip_mask[2] = { 0 }; |
| 8430 | #if CONFIG_EXT_INTER |
| 8431 | uint32_t mode_skip_mask[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 8432 | MV_REFERENCE_FRAME best_single_inter_ref = LAST_FRAME; |
| 8433 | int64_t best_single_inter_rd = INT64_MAX; |
| 8434 | #else |
| 8435 | uint16_t mode_skip_mask[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 8436 | #endif // CONFIG_EXT_INTER |
| 8437 | int mode_skip_start = sf->mode_skip_start + 1; |
| 8438 | const int *const rd_threshes = rd_opt->threshes[segment_id][bsize]; |
| 8439 | const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize]; |
| 8440 | int64_t mode_threshold[MAX_MODES]; |
| 8441 | int *mode_map = tile_data->mode_map[bsize]; |
| 8442 | const int mode_search_skip_flags = sf->mode_search_skip_flags; |
| 8443 | const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 8444 | #if CONFIG_PVQ |
| 8445 | od_rollback_buffer pre_buf; |
| 8446 | #endif |
| 8447 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8448 | #if CONFIG_PALETTE || CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8449 | const int rows = 4 * num_4x4_blocks_high_lookup[bsize]; |
| 8450 | const int cols = 4 * num_4x4_blocks_wide_lookup[bsize]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8451 | #endif // CONFIG_PALETTE || CONFIG_EXT_INTRA |
| 8452 | #if CONFIG_PALETTE |
| 8453 | int palette_ctx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8454 | const MODE_INFO *above_mi = xd->above_mi; |
| 8455 | const MODE_INFO *left_mi = xd->left_mi; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8456 | #endif // CONFIG_PALETTE |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8457 | #if CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8458 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8459 | DECLARE_ALIGNED(16, uint8_t, tmp_buf1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 8460 | DECLARE_ALIGNED(16, uint8_t, tmp_buf2[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 8461 | #else |
| 8462 | DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 8463 | DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_SB_SQUARE]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8464 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8465 | DECLARE_ALIGNED(16, int32_t, weighted_src_buf[MAX_SB_SQUARE]); |
| 8466 | DECLARE_ALIGNED(16, int32_t, mask2d_buf[MAX_SB_SQUARE]); |
| 8467 | uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE]; |
| 8468 | int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 8469 | int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 8470 | int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 8471 | int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 8472 | int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 8473 | int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 8474 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8475 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8476 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 8477 | int len = sizeof(uint16_t); |
| 8478 | dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1); |
| 8479 | dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_SB_SQUARE * len); |
| 8480 | dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_SB_SQUARE * len); |
| 8481 | dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2); |
| 8482 | dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_SB_SQUARE * len); |
| 8483 | dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_SB_SQUARE * len); |
| 8484 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8485 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8486 | dst_buf1[0] = tmp_buf1; |
| 8487 | dst_buf1[1] = tmp_buf1 + MAX_SB_SQUARE; |
| 8488 | dst_buf1[2] = tmp_buf1 + 2 * MAX_SB_SQUARE; |
| 8489 | dst_buf2[0] = tmp_buf2; |
| 8490 | dst_buf2[1] = tmp_buf2 + MAX_SB_SQUARE; |
| 8491 | dst_buf2[2] = tmp_buf2 + 2 * MAX_SB_SQUARE; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8492 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8493 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8494 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8495 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8496 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8497 | av1_zero(best_mbmode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8498 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8499 | #if CONFIG_PALETTE |
| 8500 | av1_zero(pmi_uv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8501 | if (cm->allow_screen_content_tools) { |
| 8502 | if (above_mi) |
| 8503 | palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 8504 | if (left_mi) |
| 8505 | palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 8506 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8507 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8508 | |
| 8509 | #if CONFIG_EXT_INTRA |
| 8510 | memset(directional_mode_skip_mask, 0, |
| 8511 | sizeof(directional_mode_skip_mask[0]) * INTRA_MODES); |
| 8512 | #endif // CONFIG_EXT_INTRA |
| 8513 | |
| 8514 | estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp, |
| 8515 | &comp_mode_p); |
| 8516 | |
| 8517 | for (i = 0; i < REFERENCE_MODES; ++i) best_pred_rd[i] = INT64_MAX; |
| 8518 | for (i = 0; i < TX_SIZES; i++) rate_uv_intra[i] = INT_MAX; |
| 8519 | for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX; |
| 8520 | for (i = 0; i < MB_MODE_COUNT; ++i) { |
| 8521 | for (k = 0; k < TOTAL_REFS_PER_FRAME; ++k) { |
| 8522 | single_inter_filter[i][k] = SWITCHABLE; |
| 8523 | single_skippable[i][k] = 0; |
| 8524 | } |
| 8525 | } |
| 8526 | |
| 8527 | rd_cost->rate = INT_MAX; |
| 8528 | #if CONFIG_SUPERTX |
| 8529 | *returnrate_nocoef = INT_MAX; |
| 8530 | #endif // CONFIG_SUPERTX |
| 8531 | |
| 8532 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 8533 | x->pred_mv_sad[ref_frame] = INT_MAX; |
| 8534 | x->mbmi_ext->mode_context[ref_frame] = 0; |
| 8535 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
| 8536 | x->mbmi_ext->compound_mode_context[ref_frame] = 0; |
| 8537 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 8538 | if (cpi->ref_frame_flags & flag_list[ref_frame]) { |
| 8539 | assert(get_ref_frame_buffer(cpi, ref_frame) != NULL); |
| 8540 | setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col, |
| 8541 | frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb); |
| 8542 | } |
| 8543 | frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8544 | #if CONFIG_GLOBAL_MOTION |
| 8545 | frame_mv[ZEROMV][ref_frame].as_int = |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 8546 | gm_get_motion_vector(&cm->global_motion[ref_frame]).as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8547 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8548 | frame_mv[ZEROMV][ref_frame].as_int = 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8549 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8550 | #if CONFIG_EXT_INTER |
| 8551 | frame_mv[NEWFROMNEARMV][ref_frame].as_int = INVALID_MV; |
| 8552 | frame_mv[NEW_NEWMV][ref_frame].as_int = INVALID_MV; |
| 8553 | frame_mv[ZERO_ZEROMV][ref_frame].as_int = 0; |
| 8554 | #endif // CONFIG_EXT_INTER |
| 8555 | } |
| 8556 | |
| 8557 | #if CONFIG_REF_MV |
| 8558 | for (; ref_frame < MODE_CTX_REF_FRAMES; ++ref_frame) { |
| 8559 | MODE_INFO *const mi = xd->mi[0]; |
| 8560 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 8561 | x->mbmi_ext->mode_context[ref_frame] = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8562 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 8563 | mbmi_ext->ref_mv_stack[ref_frame], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8564 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8565 | mbmi_ext->compound_mode_context, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8566 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8567 | candidates, mi_row, mi_col, NULL, NULL, |
| 8568 | mbmi_ext->mode_context); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8569 | } |
| 8570 | #endif // CONFIG_REF_MV |
| 8571 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8572 | #if CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8573 | av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col, dst_buf1, |
| 8574 | dst_width1, dst_height1, dst_stride1); |
| 8575 | av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, dst_buf2, |
| 8576 | dst_width2, dst_height2, dst_stride2); |
| 8577 | av1_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 8578 | x->mask_buf = mask2d_buf; |
| 8579 | x->wsrc_buf = weighted_src_buf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8580 | calc_target_weighted_pred(cm, x, xd, mi_row, mi_col, dst_buf1[0], |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 8581 | dst_stride1[0], dst_buf2[0], dst_stride2[0]); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8582 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8583 | |
| 8584 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 8585 | if (!(cpi->ref_frame_flags & flag_list[ref_frame])) { |
| 8586 | // Skip checking missing references in both single and compound reference |
| 8587 | // modes. Note that a mode will be skipped iff both reference frames |
| 8588 | // are masked out. |
| 8589 | #if CONFIG_EXT_REFS |
| 8590 | if (ref_frame == BWDREF_FRAME || ref_frame == ALTREF_FRAME) { |
| 8591 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 8592 | ref_frame_skip_mask[1] |= ((1 << ref_frame) | 0x01); |
| 8593 | } else { |
| 8594 | #endif // CONFIG_EXT_REFS |
| 8595 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 8596 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 8597 | #if CONFIG_EXT_REFS |
| 8598 | } |
| 8599 | #endif // CONFIG_EXT_REFS |
| 8600 | } else { |
| 8601 | for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) { |
| 8602 | // Skip fixed mv modes for poor references |
| 8603 | if ((x->pred_mv_sad[ref_frame] >> 2) > x->pred_mv_sad[i]) { |
| 8604 | mode_skip_mask[ref_frame] |= INTER_NEAREST_NEAR_ZERO; |
| 8605 | break; |
| 8606 | } |
| 8607 | } |
| 8608 | } |
| 8609 | // If the segment reference frame feature is enabled.... |
| 8610 | // then do nothing if the current ref frame is not allowed.. |
| 8611 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && |
| 8612 | get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) { |
| 8613 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 8614 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 8615 | } |
| 8616 | } |
| 8617 | |
| 8618 | // Disable this drop out case if the ref frame |
| 8619 | // segment level feature is enabled for this segment. This is to |
| 8620 | // prevent the possibility that we end up unable to pick any mode. |
| 8621 | if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) { |
| 8622 | // Only consider ZEROMV/ALTREF_FRAME for alt ref frame, |
| 8623 | // unless ARNR filtering is enabled in which case we want |
| 8624 | // an unfiltered alternative. We allow near/nearest as well |
| 8625 | // because they may result in zero-zero MVs but be cheaper. |
| 8626 | if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8627 | int_mv zeromv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8628 | ref_frame_skip_mask[0] = (1 << LAST_FRAME) | |
| 8629 | #if CONFIG_EXT_REFS |
| 8630 | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | |
| 8631 | (1 << BWDREF_FRAME) | |
| 8632 | #endif // CONFIG_EXT_REFS |
| 8633 | (1 << GOLDEN_FRAME); |
| 8634 | ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK; |
| 8635 | // TODO(zoeliu): To further explore whether following needs to be done for |
| 8636 | // BWDREF_FRAME as well. |
| 8637 | mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8638 | #if CONFIG_GLOBAL_MOTION |
| 8639 | zeromv.as_int = |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 8640 | gm_get_motion_vector(&cm->global_motion[ALTREF_FRAME]).as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8641 | #else |
| 8642 | zeromv.as_int = 0; |
| 8643 | #endif // CONFIG_GLOBAL_MOTION |
| 8644 | if (frame_mv[NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8645 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEARMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8646 | if (frame_mv[NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8647 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEARESTMV); |
| 8648 | #if CONFIG_EXT_INTER |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8649 | if (frame_mv[NEAREST_NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8650 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAREST_NEARESTMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8651 | if (frame_mv[NEAREST_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8652 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAREST_NEARMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8653 | if (frame_mv[NEAR_NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8654 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAR_NEARESTMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8655 | if (frame_mv[NEAR_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8656 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAR_NEARMV); |
| 8657 | #endif // CONFIG_EXT_INTER |
| 8658 | } |
| 8659 | } |
| 8660 | |
| 8661 | if (cpi->rc.is_src_frame_alt_ref) { |
| 8662 | if (sf->alt_ref_search_fp) { |
| 8663 | assert(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]); |
| 8664 | mode_skip_mask[ALTREF_FRAME] = 0; |
| 8665 | ref_frame_skip_mask[0] = ~(1 << ALTREF_FRAME); |
| 8666 | ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK; |
| 8667 | } |
| 8668 | } |
| 8669 | |
| 8670 | if (sf->alt_ref_search_fp) |
| 8671 | if (!cm->show_frame && x->pred_mv_sad[GOLDEN_FRAME] < INT_MAX) |
| 8672 | if (x->pred_mv_sad[ALTREF_FRAME] > (x->pred_mv_sad[GOLDEN_FRAME] << 1)) |
| 8673 | mode_skip_mask[ALTREF_FRAME] |= INTER_ALL; |
| 8674 | |
| 8675 | if (sf->adaptive_mode_search) { |
| 8676 | if (cm->show_frame && !cpi->rc.is_src_frame_alt_ref && |
| 8677 | cpi->rc.frames_since_golden >= 3) |
| 8678 | if (x->pred_mv_sad[GOLDEN_FRAME] > (x->pred_mv_sad[LAST_FRAME] << 1)) |
| 8679 | mode_skip_mask[GOLDEN_FRAME] |= INTER_ALL; |
| 8680 | } |
| 8681 | |
| 8682 | if (bsize > sf->max_intra_bsize) { |
| 8683 | ref_frame_skip_mask[0] |= (1 << INTRA_FRAME); |
| 8684 | ref_frame_skip_mask[1] |= (1 << INTRA_FRAME); |
| 8685 | } |
| 8686 | |
| 8687 | mode_skip_mask[INTRA_FRAME] |= |
| 8688 | ~(sf->intra_y_mode_mask[max_txsize_lookup[bsize]]); |
| 8689 | |
| 8690 | for (i = 0; i <= LAST_NEW_MV_INDEX; ++i) mode_threshold[i] = 0; |
| 8691 | for (i = LAST_NEW_MV_INDEX + 1; i < MAX_MODES; ++i) |
| 8692 | mode_threshold[i] = ((int64_t)rd_threshes[i] * rd_thresh_freq_fact[i]) >> 5; |
| 8693 | |
| 8694 | midx = sf->schedule_mode_search ? mode_skip_start : 0; |
| 8695 | while (midx > 4) { |
| 8696 | uint8_t end_pos = 0; |
| 8697 | for (i = 5; i < midx; ++i) { |
| 8698 | if (mode_threshold[mode_map[i - 1]] > mode_threshold[mode_map[i]]) { |
| 8699 | uint8_t tmp = mode_map[i]; |
| 8700 | mode_map[i] = mode_map[i - 1]; |
| 8701 | mode_map[i - 1] = tmp; |
| 8702 | end_pos = i; |
| 8703 | } |
| 8704 | } |
| 8705 | midx = end_pos; |
| 8706 | } |
| 8707 | |
| 8708 | if (cpi->sf.tx_type_search.fast_intra_tx_type_search) |
| 8709 | x->use_default_intra_tx_type = 1; |
| 8710 | else |
| 8711 | x->use_default_intra_tx_type = 0; |
| 8712 | |
| 8713 | if (cpi->sf.tx_type_search.fast_inter_tx_type_search) |
| 8714 | x->use_default_inter_tx_type = 1; |
| 8715 | else |
| 8716 | x->use_default_inter_tx_type = 0; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 8717 | #if CONFIG_PVQ |
| 8718 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 8719 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8720 | #if CONFIG_EXT_INTER |
| 8721 | for (i = 0; i < MB_MODE_COUNT; ++i) |
| 8722 | for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame) |
| 8723 | modelled_rd[i][ref_frame] = INT64_MAX; |
| 8724 | #endif // CONFIG_EXT_INTER |
| 8725 | |
| 8726 | for (midx = 0; midx < MAX_MODES; ++midx) { |
| 8727 | int mode_index; |
| 8728 | int mode_excluded = 0; |
| 8729 | int64_t this_rd = INT64_MAX; |
| 8730 | int disable_skip = 0; |
| 8731 | int compmode_cost = 0; |
| 8732 | #if CONFIG_EXT_INTER |
| 8733 | int compmode_interintra_cost = 0; |
| 8734 | int compmode_wedge_cost = 0; |
| 8735 | #endif // CONFIG_EXT_INTER |
| 8736 | int rate2 = 0, rate_y = 0, rate_uv = 0; |
| 8737 | int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0; |
| 8738 | int skippable = 0; |
| 8739 | int this_skip2 = 0; |
| 8740 | int64_t total_sse = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8741 | #if CONFIG_REF_MV |
| 8742 | uint8_t ref_frame_type; |
| 8743 | #endif |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 8744 | #if CONFIG_PVQ |
| 8745 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 8746 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8747 | mode_index = mode_map[midx]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8748 | this_mode = av1_mode_order[mode_index].mode; |
| 8749 | ref_frame = av1_mode_order[mode_index].ref_frame[0]; |
| 8750 | second_ref_frame = av1_mode_order[mode_index].ref_frame[1]; |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 8751 | #if CONFIG_REF_MV |
| 8752 | mbmi->ref_mv_idx = 0; |
| 8753 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8754 | |
| 8755 | #if CONFIG_EXT_INTER |
| 8756 | if (ref_frame > INTRA_FRAME && second_ref_frame == INTRA_FRAME) { |
| 8757 | // Mode must by compatible |
| 8758 | assert(is_interintra_allowed_mode(this_mode)); |
| 8759 | |
| 8760 | if (!is_interintra_allowed_bsize(bsize)) continue; |
| 8761 | } |
| 8762 | |
| 8763 | if (is_inter_compound_mode(this_mode)) { |
| 8764 | frame_mv[this_mode][ref_frame].as_int = |
| 8765 | frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int; |
| 8766 | frame_mv[this_mode][second_ref_frame].as_int = |
| 8767 | frame_mv[compound_ref1_mode(this_mode)][second_ref_frame].as_int; |
| 8768 | } |
| 8769 | #endif // CONFIG_EXT_INTER |
| 8770 | |
| 8771 | // Look at the reference frame of the best mode so far and set the |
| 8772 | // skip mask to look at a subset of the remaining modes. |
| 8773 | if (midx == mode_skip_start && best_mode_index >= 0) { |
| 8774 | switch (best_mbmode.ref_frame[0]) { |
| 8775 | case INTRA_FRAME: break; |
| 8776 | case LAST_FRAME: |
| 8777 | ref_frame_skip_mask[0] |= LAST_FRAME_MODE_MASK; |
| 8778 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 8779 | break; |
| 8780 | #if CONFIG_EXT_REFS |
| 8781 | case LAST2_FRAME: |
| 8782 | ref_frame_skip_mask[0] |= LAST2_FRAME_MODE_MASK; |
| 8783 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 8784 | break; |
| 8785 | case LAST3_FRAME: |
| 8786 | ref_frame_skip_mask[0] |= LAST3_FRAME_MODE_MASK; |
| 8787 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 8788 | break; |
| 8789 | #endif // CONFIG_EXT_REFS |
| 8790 | case GOLDEN_FRAME: |
| 8791 | ref_frame_skip_mask[0] |= GOLDEN_FRAME_MODE_MASK; |
| 8792 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 8793 | break; |
| 8794 | #if CONFIG_EXT_REFS |
| 8795 | case BWDREF_FRAME: |
| 8796 | ref_frame_skip_mask[0] |= BWDREF_FRAME_MODE_MASK; |
| 8797 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 8798 | break; |
| 8799 | #endif // CONFIG_EXT_REFS |
| 8800 | case ALTREF_FRAME: ref_frame_skip_mask[0] |= ALTREF_FRAME_MODE_MASK; |
| 8801 | #if CONFIG_EXT_REFS |
| 8802 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 8803 | #endif // CONFIG_EXT_REFS |
| 8804 | break; |
| 8805 | case NONE: |
| 8806 | case TOTAL_REFS_PER_FRAME: |
| 8807 | assert(0 && "Invalid Reference frame"); |
| 8808 | break; |
| 8809 | } |
| 8810 | } |
| 8811 | |
| 8812 | if ((ref_frame_skip_mask[0] & (1 << ref_frame)) && |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8813 | (ref_frame_skip_mask[1] & (1 << AOMMAX(0, second_ref_frame)))) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8814 | continue; |
| 8815 | |
| 8816 | if (mode_skip_mask[ref_frame] & (1 << this_mode)) continue; |
| 8817 | |
| 8818 | // Test best rd so far against threshold for trying this mode. |
| 8819 | if (best_mode_skippable && sf->schedule_mode_search) |
| 8820 | mode_threshold[mode_index] <<= 1; |
| 8821 | |
| 8822 | if (best_rd < mode_threshold[mode_index]) continue; |
| 8823 | |
| 8824 | comp_pred = second_ref_frame > INTRA_FRAME; |
| 8825 | if (comp_pred) { |
| 8826 | if (!cpi->allow_comp_inter_inter) continue; |
| 8827 | |
| 8828 | // Skip compound inter modes if ARF is not available. |
| 8829 | if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue; |
| 8830 | |
| 8831 | // Do not allow compound prediction if the segment level reference frame |
| 8832 | // feature is in use as in this case there can only be one reference. |
| 8833 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue; |
| 8834 | |
| 8835 | if ((mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) && |
| 8836 | best_mode_index >= 0 && best_mbmode.ref_frame[0] == INTRA_FRAME) |
| 8837 | continue; |
| 8838 | |
| 8839 | mode_excluded = cm->reference_mode == SINGLE_REFERENCE; |
| 8840 | } else { |
| 8841 | if (ref_frame != INTRA_FRAME) |
| 8842 | mode_excluded = cm->reference_mode == COMPOUND_REFERENCE; |
| 8843 | } |
| 8844 | |
| 8845 | if (ref_frame == INTRA_FRAME) { |
| 8846 | if (sf->adaptive_mode_search) |
| 8847 | if ((x->source_variance << num_pels_log2_lookup[bsize]) > best_pred_sse) |
| 8848 | continue; |
| 8849 | |
| 8850 | if (this_mode != DC_PRED) { |
| 8851 | // Disable intra modes other than DC_PRED for blocks with low variance |
| 8852 | // Threshold for intra skipping based on source variance |
| 8853 | // TODO(debargha): Specialize the threshold for super block sizes |
| 8854 | const unsigned int skip_intra_var_thresh = 64; |
| 8855 | if ((mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) && |
| 8856 | x->source_variance < skip_intra_var_thresh) |
| 8857 | continue; |
| 8858 | // Only search the oblique modes if the best so far is |
| 8859 | // one of the neighboring directional modes |
| 8860 | if ((mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) && |
| 8861 | (this_mode >= D45_PRED && this_mode <= TM_PRED)) { |
| 8862 | if (best_mode_index >= 0 && best_mbmode.ref_frame[0] > INTRA_FRAME) |
| 8863 | continue; |
| 8864 | } |
| 8865 | if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 8866 | if (conditional_skipintra(this_mode, best_intra_mode)) continue; |
| 8867 | } |
| 8868 | } |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8869 | #if CONFIG_GLOBAL_MOTION |
| 8870 | } else if (get_gmtype(&cm->global_motion[ref_frame]) == GLOBAL_ZERO && |
| 8871 | (!comp_pred || |
| 8872 | get_gmtype(&cm->global_motion[second_ref_frame]) == |
| 8873 | GLOBAL_ZERO)) { |
| 8874 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8875 | } else { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 8876 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8877 | const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, second_ref_frame }; |
| 8878 | if (!check_best_zero_mv(cpi, mbmi_ext->mode_context, |
| 8879 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
| 8880 | mbmi_ext->compound_mode_context, |
| 8881 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 8882 | frame_mv, this_mode, ref_frames, bsize, -1)) |
| 8883 | continue; |
| 8884 | } |
| 8885 | |
| 8886 | mbmi->mode = this_mode; |
| 8887 | mbmi->uv_mode = DC_PRED; |
| 8888 | mbmi->ref_frame[0] = ref_frame; |
| 8889 | mbmi->ref_frame[1] = second_ref_frame; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8890 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8891 | pmi->palette_size[0] = 0; |
| 8892 | pmi->palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8893 | #endif // CONFIG_PALETTE |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8894 | #if CONFIG_FILTER_INTRA |
| 8895 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 8896 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 8897 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8898 | // Evaluate all sub-pel filters irrespective of whether we can use |
| 8899 | // them for this frame. |
| 8900 | #if CONFIG_DUAL_FILTER |
| 8901 | for (i = 0; i < 4; ++i) { |
| 8902 | mbmi->interp_filter[i] = cm->interp_filter == SWITCHABLE |
| 8903 | ? EIGHTTAP_REGULAR |
| 8904 | : cm->interp_filter; |
| 8905 | } |
| 8906 | #else |
| 8907 | mbmi->interp_filter = |
| 8908 | cm->interp_filter == SWITCHABLE ? EIGHTTAP_REGULAR : cm->interp_filter; |
| 8909 | #endif |
| 8910 | mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8911 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8912 | |
| 8913 | x->skip = 0; |
| 8914 | set_ref_ptrs(cm, xd, ref_frame, second_ref_frame); |
| 8915 | |
| 8916 | // Select prediction reference frames. |
| 8917 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 8918 | xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; |
| 8919 | if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i]; |
| 8920 | } |
| 8921 | |
| 8922 | #if CONFIG_EXT_INTER |
Debargha Mukherjee | cb60379 | 2016-10-04 13:10:23 -0700 | [diff] [blame] | 8923 | mbmi->interintra_mode = (INTERINTRA_MODE)(II_DC_PRED - 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8924 | #endif // CONFIG_EXT_INTER |
| 8925 | |
| 8926 | if (ref_frame == INTRA_FRAME) { |
| 8927 | TX_SIZE uv_tx; |
| 8928 | struct macroblockd_plane *const pd = &xd->plane[1]; |
| 8929 | #if CONFIG_EXT_INTRA |
| 8930 | is_directional_mode = (mbmi->mode != DC_PRED && mbmi->mode != TM_PRED); |
| 8931 | if (is_directional_mode) { |
| 8932 | if (!angle_stats_ready) { |
| 8933 | const int src_stride = x->plane[0].src.stride; |
| 8934 | const uint8_t *src = x->plane[0].src.buf; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8935 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8936 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 8937 | highbd_angle_estimation(src, src_stride, rows, cols, |
| 8938 | directional_mode_skip_mask); |
| 8939 | else |
| 8940 | #endif |
| 8941 | angle_estimation(src, src_stride, rows, cols, |
| 8942 | directional_mode_skip_mask); |
| 8943 | angle_stats_ready = 1; |
| 8944 | } |
| 8945 | if (directional_mode_skip_mask[mbmi->mode]) continue; |
| 8946 | rate_overhead = write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1, 0) + |
| 8947 | intra_mode_cost[mbmi->mode]; |
| 8948 | rate_y = INT_MAX; |
| 8949 | this_rd = |
| 8950 | rd_pick_intra_angle_sby(cpi, x, &rate_dummy, &rate_y, &distortion_y, |
| 8951 | &skippable, bsize, rate_overhead, best_rd); |
| 8952 | } else { |
| 8953 | mbmi->angle_delta[0] = 0; |
| 8954 | super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable, NULL, bsize, |
| 8955 | best_rd); |
| 8956 | } |
| 8957 | #else |
| 8958 | super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable, NULL, bsize, |
| 8959 | best_rd); |
| 8960 | #endif // CONFIG_EXT_INTRA |
| 8961 | |
| 8962 | if (rate_y == INT_MAX) continue; |
| 8963 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8964 | #if CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8965 | if (mbmi->mode == DC_PRED) dc_skipped = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8966 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8967 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 8968 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][pd->subsampling_x] |
| 8969 | [pd->subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8970 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
| 8971 | choose_intra_uv_mode(cpi, x, ctx, bsize, uv_tx, &rate_uv_intra[uv_tx], |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 8972 | &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx], |
| 8973 | &skip_uvs[uv_tx], &mode_uv[uv_tx]); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8974 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8975 | if (cm->allow_screen_content_tools) pmi_uv[uv_tx] = *pmi; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8976 | #endif // CONFIG_PALETTE |
| 8977 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8978 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8979 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
| 8980 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 8981 | #if CONFIG_FILTER_INTRA |
| 8982 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 8983 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8984 | } |
| 8985 | |
| 8986 | rate_uv = rate_uv_tokenonly[uv_tx]; |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 8987 | distortion_uv = dist_uvs[uv_tx]; |
| 8988 | skippable = skippable && skip_uvs[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8989 | mbmi->uv_mode = mode_uv[uv_tx]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8990 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8991 | if (cm->allow_screen_content_tools) { |
| 8992 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
| 8993 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 8994 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 8995 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
| 8996 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 8997 | #endif // CONFIG_PALETTE |
| 8998 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8999 | #if CONFIG_EXT_INTRA |
| 9000 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9001 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9002 | #if CONFIG_FILTER_INTRA |
| 9003 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 9004 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 9005 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 9006 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 9007 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
| 9008 | } |
| 9009 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9010 | |
| 9011 | rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv + |
| 9012 | cpi->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9013 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9014 | if (cpi->common.allow_screen_content_tools && mbmi->mode == DC_PRED) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9015 | rate2 += av1_cost_bit( |
| 9016 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9017 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9018 | |
| 9019 | if (!xd->lossless[mbmi->segment_id]) { |
| 9020 | // super_block_yrd above includes the cost of the tx_size in the |
| 9021 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 9022 | // (prediction granularity), so we account for it in the full rate, |
| 9023 | // not the tokenonly rate. |
Jingning Han | b0a7130 | 2016-10-25 16:28:49 -0700 | [diff] [blame] | 9024 | rate_y -= |
| 9025 | cpi->tx_size_cost[max_tx_size - TX_8X8][get_tx_size_context(xd)] |
| 9026 | [tx_size_to_depth(mbmi->tx_size)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9027 | } |
| 9028 | #if CONFIG_EXT_INTRA |
| 9029 | if (is_directional_mode) { |
| 9030 | int p_angle; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9031 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9032 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1, |
| 9033 | MAX_ANGLE_DELTAS + mbmi->angle_delta[0]); |
| 9034 | p_angle = |
| 9035 | mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9036 | if (av1_is_intra_filter_switchable(p_angle)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9037 | rate2 += cpi->intra_filter_cost[intra_filter_ctx][mbmi->intra_filter]; |
| 9038 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9039 | if (mbmi->uv_mode != DC_PRED && mbmi->uv_mode != TM_PRED) { |
| 9040 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTAS + 1, |
| 9041 | MAX_ANGLE_DELTAS + mbmi->angle_delta[1]); |
| 9042 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9043 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9044 | #if CONFIG_FILTER_INTRA |
| 9045 | if (mbmi->mode == DC_PRED) { |
| 9046 | rate2 += |
| 9047 | av1_cost_bit(cm->fc->filter_intra_probs[0], |
| 9048 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0]); |
| 9049 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) { |
| 9050 | rate2 += write_uniform_cost( |
| 9051 | FILTER_INTRA_MODES, |
| 9052 | mbmi->filter_intra_mode_info.filter_intra_mode[0]); |
| 9053 | } |
| 9054 | } |
| 9055 | if (mbmi->uv_mode == DC_PRED) { |
| 9056 | rate2 += |
| 9057 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], |
| 9058 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1]); |
| 9059 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1]) |
| 9060 | rate2 += write_uniform_cost( |
| 9061 | FILTER_INTRA_MODES, |
| 9062 | mbmi->filter_intra_mode_info.filter_intra_mode[1]); |
| 9063 | } |
| 9064 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9065 | if (this_mode != DC_PRED && this_mode != TM_PRED) |
| 9066 | rate2 += intra_cost_penalty; |
| 9067 | distortion2 = distortion_y + distortion_uv; |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 9068 | av1_encode_intra_block_plane((AV1_COMMON *)cm, x, bsize, 0, 1); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9069 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9070 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9071 | x->recon_variance = av1_high_get_sby_perpixel_variance( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9072 | cpi, &xd->plane[0].dst, bsize, xd->bd); |
| 9073 | } else { |
| 9074 | x->recon_variance = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9075 | av1_get_sby_perpixel_variance(cpi, &xd->plane[0].dst, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9076 | } |
| 9077 | #else |
| 9078 | x->recon_variance = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9079 | av1_get_sby_perpixel_variance(cpi, &xd->plane[0].dst, bsize); |
| 9080 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9081 | } else { |
| 9082 | #if CONFIG_REF_MV |
| 9083 | int_mv backup_ref_mv[2]; |
| 9084 | |
| 9085 | backup_ref_mv[0] = mbmi_ext->ref_mvs[ref_frame][0]; |
| 9086 | if (comp_pred) backup_ref_mv[1] = mbmi_ext->ref_mvs[second_ref_frame][0]; |
| 9087 | #endif |
| 9088 | #if CONFIG_EXT_INTER |
| 9089 | if (second_ref_frame == INTRA_FRAME) { |
| 9090 | if (best_single_inter_ref != ref_frame) continue; |
Debargha Mukherjee | cb60379 | 2016-10-04 13:10:23 -0700 | [diff] [blame] | 9091 | mbmi->interintra_mode = intra_to_interintra_mode[best_intra_mode]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9092 | // TODO(debargha|geza.lore): |
| 9093 | // Should we use ext_intra modes for interintra? |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9094 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9095 | mbmi->angle_delta[0] = 0; |
| 9096 | mbmi->angle_delta[1] = 0; |
| 9097 | mbmi->intra_filter = INTRA_FILTER_LINEAR; |
| 9098 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9099 | #if CONFIG_FILTER_INTRA |
| 9100 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 9101 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 9102 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9103 | } |
| 9104 | #endif // CONFIG_EXT_INTER |
| 9105 | #if CONFIG_REF_MV |
| 9106 | mbmi->ref_mv_idx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9107 | ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9108 | |
| 9109 | if (this_mode == NEWMV && mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
| 9110 | int ref; |
| 9111 | for (ref = 0; ref < 1 + comp_pred; ++ref) { |
| 9112 | int_mv this_mv = |
| 9113 | (ref == 0) ? mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv |
| 9114 | : mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 9115 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << 3, xd->n8_h << 3, xd); |
| 9116 | mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv; |
| 9117 | } |
| 9118 | } |
| 9119 | #endif |
| 9120 | this_rd = handle_inter_mode( |
| 9121 | cpi, x, bsize, &rate2, &distortion2, &skippable, &rate_y, &rate_uv, |
| 9122 | &disable_skip, frame_mv, mi_row, mi_col, |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9123 | #if CONFIG_MOTION_VAR |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 9124 | dst_buf1, dst_stride1, dst_buf2, dst_stride2, |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9125 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9126 | #if CONFIG_EXT_INTER |
| 9127 | single_newmvs, single_newmvs_rate, &compmode_interintra_cost, |
| 9128 | &compmode_wedge_cost, modelled_rd, |
| 9129 | #else |
| 9130 | single_newmv, |
| 9131 | #endif // CONFIG_EXT_INTER |
| 9132 | single_inter_filter, single_skippable, &total_sse, best_rd); |
| 9133 | |
| 9134 | #if CONFIG_REF_MV |
| 9135 | // TODO(jingning): This needs some refactoring to improve code quality |
| 9136 | // and reduce redundant steps. |
| 9137 | if ((mbmi->mode == NEARMV && |
| 9138 | mbmi_ext->ref_mv_count[ref_frame_type] > 2) || |
| 9139 | (mbmi->mode == NEWMV && mbmi_ext->ref_mv_count[ref_frame_type] > 1)) { |
| 9140 | int_mv backup_mv = frame_mv[NEARMV][ref_frame]; |
| 9141 | MB_MODE_INFO backup_mbmi = *mbmi; |
| 9142 | int backup_skip = x->skip; |
| 9143 | int64_t tmp_ref_rd = this_rd; |
| 9144 | int ref_idx; |
| 9145 | |
| 9146 | // TODO(jingning): This should be deprecated shortly. |
| 9147 | int idx_offset = (mbmi->mode == NEARMV) ? 1 : 0; |
| 9148 | int ref_set = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9149 | AOMMIN(2, mbmi_ext->ref_mv_count[ref_frame_type] - 1 - idx_offset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9150 | |
| 9151 | uint8_t drl_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9152 | av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx_offset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9153 | // Dummy |
| 9154 | int_mv backup_fmv[2]; |
| 9155 | backup_fmv[0] = frame_mv[NEWMV][ref_frame]; |
| 9156 | if (comp_pred) backup_fmv[1] = frame_mv[NEWMV][second_ref_frame]; |
| 9157 | |
Debargha Mukherjee | 1ae9f2c | 2016-10-04 14:30:16 -0700 | [diff] [blame] | 9158 | rate2 += (rate2 < INT_MAX ? cpi->drl_mode_cost0[drl_ctx][0] : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9159 | |
| 9160 | if (this_rd < INT64_MAX) { |
| 9161 | if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) < |
| 9162 | RDCOST(x->rdmult, x->rddiv, 0, total_sse)) |
| 9163 | tmp_ref_rd = |
| 9164 | RDCOST(x->rdmult, x->rddiv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9165 | rate2 + av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9166 | distortion2); |
| 9167 | else |
| 9168 | tmp_ref_rd = |
| 9169 | RDCOST(x->rdmult, x->rddiv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9170 | rate2 + av1_cost_bit(av1_get_skip_prob(cm, xd), 1) - |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9171 | rate_y - rate_uv, |
| 9172 | total_sse); |
| 9173 | } |
| 9174 | #if CONFIG_VAR_TX |
| 9175 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 9176 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 9177 | sizeof(uint8_t) * ctx->num_4x4_blk); |
| 9178 | #endif |
| 9179 | |
| 9180 | for (ref_idx = 0; ref_idx < ref_set; ++ref_idx) { |
| 9181 | int64_t tmp_alt_rd = INT64_MAX; |
| 9182 | int tmp_rate = 0, tmp_rate_y = 0, tmp_rate_uv = 0; |
| 9183 | int tmp_skip = 1; |
| 9184 | int64_t tmp_dist = 0, tmp_sse = 0; |
| 9185 | int dummy_disable_skip = 0; |
| 9186 | int ref; |
| 9187 | int_mv cur_mv; |
| 9188 | |
| 9189 | mbmi->ref_mv_idx = 1 + ref_idx; |
| 9190 | |
| 9191 | for (ref = 0; ref < 1 + comp_pred; ++ref) { |
| 9192 | int_mv this_mv = |
| 9193 | (ref == 0) |
| 9194 | ? mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx] |
| 9195 | .this_mv |
| 9196 | : mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx] |
| 9197 | .comp_mv; |
| 9198 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << 3, xd->n8_h << 3, xd); |
| 9199 | mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv; |
| 9200 | } |
| 9201 | |
| 9202 | cur_mv = |
| 9203 | mbmi_ext->ref_mv_stack[ref_frame][mbmi->ref_mv_idx + idx_offset] |
| 9204 | .this_mv; |
| 9205 | clamp_mv2(&cur_mv.as_mv, xd); |
| 9206 | |
| 9207 | if (!mv_check_bounds(x, &cur_mv.as_mv)) { |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 9208 | int dummy_single_skippable[MB_MODE_COUNT] |
| 9209 | [TOTAL_REFS_PER_FRAME] = { { 0 } }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9210 | #if CONFIG_EXT_INTER |
| 9211 | int_mv dummy_single_newmvs[2][TOTAL_REFS_PER_FRAME] = { { { 0 } }, |
| 9212 | { { 0 } } }; |
| 9213 | int dummy_single_newmvs_rate[2][TOTAL_REFS_PER_FRAME] = { { 0 }, |
| 9214 | { 0 } }; |
| 9215 | int dummy_compmode_interintra_cost = 0; |
| 9216 | int dummy_compmode_wedge_cost = 0; |
| 9217 | #else |
| 9218 | int_mv dummy_single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } }; |
| 9219 | #endif |
| 9220 | |
| 9221 | frame_mv[NEARMV][ref_frame] = cur_mv; |
| 9222 | tmp_alt_rd = handle_inter_mode( |
| 9223 | cpi, x, bsize, &tmp_rate, &tmp_dist, &tmp_skip, &tmp_rate_y, |
| 9224 | &tmp_rate_uv, &dummy_disable_skip, frame_mv, mi_row, mi_col, |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9225 | #if CONFIG_MOTION_VAR |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 9226 | dst_buf1, dst_stride1, dst_buf2, dst_stride2, |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9227 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9228 | #if CONFIG_EXT_INTER |
| 9229 | dummy_single_newmvs, dummy_single_newmvs_rate, |
| 9230 | &dummy_compmode_interintra_cost, &dummy_compmode_wedge_cost, |
| 9231 | NULL, |
| 9232 | #else |
| 9233 | dummy_single_newmv, |
| 9234 | #endif |
Jingning Han | 7212096 | 2016-10-24 09:32:41 -0700 | [diff] [blame] | 9235 | single_inter_filter, dummy_single_skippable, &tmp_sse, best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9236 | } |
| 9237 | |
| 9238 | for (i = 0; i < mbmi->ref_mv_idx; ++i) { |
| 9239 | uint8_t drl1_ctx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9240 | drl1_ctx = av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], |
| 9241 | i + idx_offset); |
Yi Luo | e8e8cd8 | 2016-09-21 10:45:01 -0700 | [diff] [blame] | 9242 | tmp_rate += |
| 9243 | (tmp_rate < INT_MAX ? cpi->drl_mode_cost0[drl1_ctx][1] : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9244 | } |
| 9245 | |
| 9246 | if (mbmi_ext->ref_mv_count[ref_frame_type] > |
| 9247 | mbmi->ref_mv_idx + idx_offset + 1 && |
| 9248 | ref_idx < ref_set - 1) { |
| 9249 | uint8_t drl1_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9250 | av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], |
| 9251 | mbmi->ref_mv_idx + idx_offset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9252 | tmp_rate += cpi->drl_mode_cost0[drl1_ctx][0]; |
| 9253 | } |
| 9254 | |
| 9255 | if (tmp_alt_rd < INT64_MAX) { |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9256 | #if CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9257 | tmp_alt_rd = RDCOST(x->rdmult, x->rddiv, tmp_rate, tmp_dist); |
| 9258 | #else |
| 9259 | if (RDCOST(x->rdmult, x->rddiv, tmp_rate_y + tmp_rate_uv, |
| 9260 | tmp_dist) < RDCOST(x->rdmult, x->rddiv, 0, tmp_sse)) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9261 | tmp_alt_rd = |
| 9262 | RDCOST(x->rdmult, x->rddiv, |
| 9263 | tmp_rate + av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
| 9264 | tmp_dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9265 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9266 | tmp_alt_rd = |
| 9267 | RDCOST(x->rdmult, x->rddiv, |
| 9268 | tmp_rate + av1_cost_bit(av1_get_skip_prob(cm, xd), 1) - |
| 9269 | tmp_rate_y - tmp_rate_uv, |
| 9270 | tmp_sse); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9271 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9272 | } |
| 9273 | |
| 9274 | if (tmp_ref_rd > tmp_alt_rd) { |
| 9275 | rate2 = tmp_rate; |
| 9276 | disable_skip = dummy_disable_skip; |
| 9277 | distortion2 = tmp_dist; |
| 9278 | skippable = tmp_skip; |
| 9279 | rate_y = tmp_rate_y; |
| 9280 | rate_uv = tmp_rate_uv; |
| 9281 | total_sse = tmp_sse; |
| 9282 | this_rd = tmp_alt_rd; |
| 9283 | tmp_ref_rd = tmp_alt_rd; |
| 9284 | backup_mbmi = *mbmi; |
| 9285 | backup_skip = x->skip; |
| 9286 | #if CONFIG_VAR_TX |
| 9287 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 9288 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 9289 | sizeof(uint8_t) * ctx->num_4x4_blk); |
| 9290 | #endif |
| 9291 | } else { |
| 9292 | *mbmi = backup_mbmi; |
| 9293 | x->skip = backup_skip; |
| 9294 | } |
| 9295 | } |
| 9296 | |
| 9297 | frame_mv[NEARMV][ref_frame] = backup_mv; |
| 9298 | frame_mv[NEWMV][ref_frame] = backup_fmv[0]; |
| 9299 | if (comp_pred) frame_mv[NEWMV][second_ref_frame] = backup_fmv[1]; |
| 9300 | #if CONFIG_VAR_TX |
| 9301 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 9302 | memcpy(x->blk_skip[i], x->blk_skip_drl[i], |
| 9303 | sizeof(uint8_t) * ctx->num_4x4_blk); |
| 9304 | #endif |
| 9305 | } |
| 9306 | mbmi_ext->ref_mvs[ref_frame][0] = backup_ref_mv[0]; |
| 9307 | if (comp_pred) mbmi_ext->ref_mvs[second_ref_frame][0] = backup_ref_mv[1]; |
| 9308 | #endif // CONFIG_REF_MV |
| 9309 | |
| 9310 | if (this_rd == INT64_MAX) continue; |
| 9311 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9312 | compmode_cost = av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9313 | |
| 9314 | if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost; |
| 9315 | } |
| 9316 | |
| 9317 | #if CONFIG_EXT_INTER |
| 9318 | rate2 += compmode_interintra_cost; |
| 9319 | if (cm->reference_mode != SINGLE_REFERENCE && comp_pred) |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9320 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 9321 | if (mbmi->motion_mode == SIMPLE_TRANSLATION) |
| 9322 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9323 | rate2 += compmode_wedge_cost; |
| 9324 | #endif // CONFIG_EXT_INTER |
| 9325 | |
| 9326 | // Estimate the reference frame signaling cost and add it |
| 9327 | // to the rolling cost variable. |
| 9328 | if (comp_pred) { |
| 9329 | rate2 += ref_costs_comp[ref_frame]; |
| 9330 | #if CONFIG_EXT_REFS |
| 9331 | rate2 += ref_costs_comp[second_ref_frame]; |
| 9332 | #endif // CONFIG_EXT_REFS |
| 9333 | } else { |
| 9334 | rate2 += ref_costs_single[ref_frame]; |
| 9335 | } |
| 9336 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9337 | #if CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9338 | if (ref_frame == INTRA_FRAME) { |
| 9339 | #else |
| 9340 | if (!disable_skip) { |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9341 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9342 | if (skippable) { |
| 9343 | // Back out the coefficient coding costs |
| 9344 | rate2 -= (rate_y + rate_uv); |
| 9345 | rate_y = 0; |
| 9346 | rate_uv = 0; |
| 9347 | // Cost the skip mb case |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9348 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9349 | } else if (ref_frame != INTRA_FRAME && !xd->lossless[mbmi->segment_id]) { |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 9350 | #if CONFIG_REF_MV |
| 9351 | if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv + rate_skip0, |
| 9352 | distortion2) < |
| 9353 | RDCOST(x->rdmult, x->rddiv, rate_skip1, total_sse)) { |
| 9354 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9355 | if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) < |
| 9356 | RDCOST(x->rdmult, x->rddiv, 0, total_sse)) { |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 9357 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9358 | // Add in the cost of the no skip flag. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9359 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9360 | } else { |
| 9361 | // FIXME(rbultje) make this work for splitmv also |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9362 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9363 | distortion2 = total_sse; |
| 9364 | assert(total_sse >= 0); |
| 9365 | rate2 -= (rate_y + rate_uv); |
| 9366 | this_skip2 = 1; |
| 9367 | rate_y = 0; |
| 9368 | rate_uv = 0; |
| 9369 | } |
| 9370 | } else { |
| 9371 | // Add in the cost of the no skip flag. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9372 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9373 | } |
| 9374 | |
| 9375 | // Calculate the final RD estimate for this mode. |
| 9376 | this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9377 | #if CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9378 | } else { |
| 9379 | this_skip2 = mbmi->skip; |
| 9380 | this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); |
| 9381 | if (this_skip2) { |
| 9382 | rate_y = 0; |
| 9383 | rate_uv = 0; |
| 9384 | } |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9385 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9386 | } |
| 9387 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9388 | if (ref_frame == INTRA_FRAME) { |
| 9389 | // Keep record of best intra rd |
| 9390 | if (this_rd < best_intra_rd) { |
| 9391 | best_intra_rd = this_rd; |
| 9392 | best_intra_mode = mbmi->mode; |
| 9393 | } |
| 9394 | #if CONFIG_EXT_INTER |
| 9395 | } else if (second_ref_frame == NONE) { |
| 9396 | if (this_rd < best_single_inter_rd) { |
| 9397 | best_single_inter_rd = this_rd; |
| 9398 | best_single_inter_ref = mbmi->ref_frame[0]; |
| 9399 | } |
| 9400 | #endif // CONFIG_EXT_INTER |
| 9401 | } |
| 9402 | |
| 9403 | if (!disable_skip && ref_frame == INTRA_FRAME) { |
| 9404 | for (i = 0; i < REFERENCE_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9405 | best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9406 | } |
| 9407 | |
| 9408 | // Did this mode help.. i.e. is it the new best mode |
| 9409 | if (this_rd < best_rd || x->skip) { |
| 9410 | if (!mode_excluded) { |
| 9411 | // Note index of best mode so far |
| 9412 | best_mode_index = mode_index; |
| 9413 | |
| 9414 | if (ref_frame == INTRA_FRAME) { |
| 9415 | /* required for left and above block mv */ |
| 9416 | mbmi->mv[0].as_int = 0; |
| 9417 | } else { |
| 9418 | best_pred_sse = x->pred_sse[ref_frame]; |
| 9419 | } |
| 9420 | |
| 9421 | rd_cost->rate = rate2; |
| 9422 | #if CONFIG_SUPERTX |
| 9423 | if (x->skip) |
| 9424 | *returnrate_nocoef = rate2; |
| 9425 | else |
| 9426 | *returnrate_nocoef = rate2 - rate_y - rate_uv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9427 | *returnrate_nocoef -= av1_cost_bit( |
| 9428 | av1_get_skip_prob(cm, xd), disable_skip || skippable || this_skip2); |
| 9429 | *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd), |
| 9430 | mbmi->ref_frame[0] != INTRA_FRAME); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9431 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 9432 | if (is_inter_block(mbmi) && is_motion_variation_allowed(mbmi)) |
| 9433 | *returnrate_nocoef -= cpi->motion_mode_cost[bsize][mbmi->motion_mode]; |
| 9434 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9435 | #endif // CONFIG_SUPERTX |
| 9436 | rd_cost->dist = distortion2; |
| 9437 | rd_cost->rdcost = this_rd; |
| 9438 | best_rd = this_rd; |
| 9439 | best_mbmode = *mbmi; |
| 9440 | best_skip2 = this_skip2; |
| 9441 | best_mode_skippable = skippable; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9442 | best_rate_y = rate_y + av1_cost_bit(av1_get_skip_prob(cm, xd), |
| 9443 | this_skip2 || skippable); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9444 | best_rate_uv = rate_uv; |
| 9445 | |
| 9446 | #if CONFIG_VAR_TX |
| 9447 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 9448 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
| 9449 | sizeof(uint8_t) * ctx->num_4x4_blk); |
| 9450 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9451 | } |
| 9452 | } |
| 9453 | |
| 9454 | /* keep record of best compound/single-only prediction */ |
| 9455 | if (!disable_skip && ref_frame != INTRA_FRAME) { |
| 9456 | int64_t single_rd, hybrid_rd, single_rate, hybrid_rate; |
| 9457 | |
| 9458 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
| 9459 | single_rate = rate2 - compmode_cost; |
| 9460 | hybrid_rate = rate2; |
| 9461 | } else { |
| 9462 | single_rate = rate2; |
| 9463 | hybrid_rate = rate2 + compmode_cost; |
| 9464 | } |
| 9465 | |
| 9466 | single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2); |
| 9467 | hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2); |
| 9468 | |
| 9469 | if (!comp_pred) { |
| 9470 | if (single_rd < best_pred_rd[SINGLE_REFERENCE]) |
| 9471 | best_pred_rd[SINGLE_REFERENCE] = single_rd; |
| 9472 | } else { |
| 9473 | if (single_rd < best_pred_rd[COMPOUND_REFERENCE]) |
| 9474 | best_pred_rd[COMPOUND_REFERENCE] = single_rd; |
| 9475 | } |
| 9476 | if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT]) |
| 9477 | best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd; |
| 9478 | } |
| 9479 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9480 | if (x->skip && !comp_pred) break; |
| 9481 | } |
| 9482 | |
| 9483 | if (xd->lossless[mbmi->segment_id] == 0 && best_mode_index >= 0 && |
| 9484 | ((sf->tx_type_search.fast_inter_tx_type_search == 1 && |
| 9485 | is_inter_mode(best_mbmode.mode)) || |
| 9486 | (sf->tx_type_search.fast_intra_tx_type_search == 1 && |
| 9487 | !is_inter_mode(best_mbmode.mode)))) { |
| 9488 | int rate_y = 0, rate_uv = 0; |
| 9489 | int64_t dist_y = 0, dist_uv = 0; |
| 9490 | int skip_y = 0, skip_uv = 0, skip_blk = 0; |
| 9491 | int64_t sse_y = 0, sse_uv = 0; |
| 9492 | |
| 9493 | x->use_default_inter_tx_type = 0; |
| 9494 | x->use_default_intra_tx_type = 0; |
| 9495 | |
| 9496 | *mbmi = best_mbmode; |
| 9497 | |
| 9498 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 9499 | |
| 9500 | // Select prediction reference frames. |
| 9501 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 9502 | xd->plane[i].pre[0] = yv12_mb[mbmi->ref_frame[0]][i]; |
| 9503 | if (has_second_ref(mbmi)) |
| 9504 | xd->plane[i].pre[1] = yv12_mb[mbmi->ref_frame[1]][i]; |
| 9505 | } |
| 9506 | |
| 9507 | if (is_inter_mode(mbmi->mode)) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 9508 | #if CONFIG_VAR_TX |
| 9509 | RD_STATS rd_stats_uv; |
| 9510 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9511 | av1_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9512 | #if CONFIG_MOTION_VAR |
| 9513 | if (mbmi->motion_mode == OBMC_CAUSAL) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9514 | av1_build_obmc_inter_prediction(cm, xd, mi_row, mi_col, dst_buf1, |
| 9515 | dst_stride1, dst_buf2, dst_stride2); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9516 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9517 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9518 | #if CONFIG_VAR_TX |
| 9519 | if (cm->tx_mode == TX_MODE_SELECT || xd->lossless[mbmi->segment_id]) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 9520 | RD_STATS rd_stats_y; |
| 9521 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 9522 | rate_y = rd_stats_y.rate; |
| 9523 | dist_y = rd_stats_y.dist; |
| 9524 | sse_y = rd_stats_y.sse; |
| 9525 | skip_y = rd_stats_y.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9526 | } else { |
| 9527 | int idx, idy; |
| 9528 | super_block_yrd(cpi, x, &rate_y, &dist_y, &skip_y, &sse_y, bsize, |
| 9529 | INT64_MAX); |
| 9530 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 9531 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 9532 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 9533 | memset(x->blk_skip[0], skip_y, |
| 9534 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 9535 | } |
| 9536 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 9537 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 9538 | rate_uv = rd_stats_uv.rate; |
| 9539 | dist_uv = rd_stats_uv.dist; |
| 9540 | skip_uv = rd_stats_uv.skip; |
| 9541 | sse_uv = rd_stats_uv.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9542 | #else |
| 9543 | super_block_yrd(cpi, x, &rate_y, &dist_y, &skip_y, &sse_y, bsize, |
| 9544 | INT64_MAX); |
| 9545 | super_block_uvrd(cpi, x, &rate_uv, &dist_uv, &skip_uv, &sse_uv, bsize, |
| 9546 | INT64_MAX); |
| 9547 | #endif // CONFIG_VAR_TX |
| 9548 | } else { |
| 9549 | super_block_yrd(cpi, x, &rate_y, &dist_y, &skip_y, &sse_y, bsize, |
| 9550 | INT64_MAX); |
| 9551 | super_block_uvrd(cpi, x, &rate_uv, &dist_uv, &skip_uv, &sse_uv, bsize, |
| 9552 | INT64_MAX); |
| 9553 | } |
| 9554 | |
| 9555 | if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, (dist_y + dist_uv)) > |
| 9556 | RDCOST(x->rdmult, x->rddiv, 0, (sse_y + sse_uv))) { |
| 9557 | skip_blk = 1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9558 | rate_y = av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9559 | rate_uv = 0; |
| 9560 | dist_y = sse_y; |
| 9561 | dist_uv = sse_uv; |
| 9562 | } else { |
| 9563 | skip_blk = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9564 | rate_y += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9565 | } |
| 9566 | |
| 9567 | if (RDCOST(x->rdmult, x->rddiv, best_rate_y + best_rate_uv, rd_cost->dist) > |
| 9568 | RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, (dist_y + dist_uv))) { |
| 9569 | #if CONFIG_VAR_TX |
| 9570 | int idx, idy; |
| 9571 | #endif |
| 9572 | best_mbmode.tx_type = mbmi->tx_type; |
| 9573 | best_mbmode.tx_size = mbmi->tx_size; |
| 9574 | #if CONFIG_VAR_TX |
| 9575 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 9576 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 9577 | best_mbmode.inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 9578 | |
| 9579 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 9580 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
| 9581 | sizeof(uint8_t) * ctx->num_4x4_blk); |
| 9582 | #endif |
| 9583 | rd_cost->rate += (rate_y + rate_uv - best_rate_y - best_rate_uv); |
| 9584 | rd_cost->dist = dist_y + dist_uv; |
| 9585 | rd_cost->rdcost = |
| 9586 | RDCOST(x->rdmult, x->rddiv, rd_cost->rate, rd_cost->dist); |
| 9587 | best_skip2 = skip_blk; |
| 9588 | } |
| 9589 | } |
| 9590 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9591 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9592 | // Only try palette mode when the best mode so far is an intra mode. |
| 9593 | if (cm->allow_screen_content_tools && !is_inter_mode(best_mbmode.mode)) { |
| 9594 | PREDICTION_MODE mode_selected; |
| 9595 | int rate2 = 0, rate_y = 0; |
| 9596 | #if CONFIG_SUPERTX |
| 9597 | int best_rate_nocoef; |
| 9598 | #endif |
| 9599 | int64_t distortion2 = 0, distortion_y = 0, dummy_rd = best_rd, this_rd; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 9600 | int skippable = 0, rate_overhead_palette = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9601 | TX_SIZE best_tx_size, uv_tx; |
| 9602 | TX_TYPE best_tx_type; |
| 9603 | PALETTE_MODE_INFO palette_mode_info; |
| 9604 | uint8_t *const best_palette_color_map = |
| 9605 | x->palette_buffer->best_palette_color_map; |
| 9606 | uint8_t *const color_map = xd->plane[0].color_index_map; |
| 9607 | |
| 9608 | mbmi->mode = DC_PRED; |
| 9609 | mbmi->uv_mode = DC_PRED; |
| 9610 | mbmi->ref_frame[0] = INTRA_FRAME; |
| 9611 | mbmi->ref_frame[1] = NONE; |
| 9612 | palette_mode_info.palette_size[0] = 0; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 9613 | rate_overhead_palette = rd_pick_palette_intra_sby( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9614 | cpi, x, bsize, palette_ctx, intra_mode_cost[DC_PRED], |
| 9615 | &palette_mode_info, best_palette_color_map, &best_tx_size, |
| 9616 | &best_tx_type, &mode_selected, &dummy_rd); |
| 9617 | if (palette_mode_info.palette_size[0] == 0) goto PALETTE_EXIT; |
| 9618 | |
| 9619 | pmi->palette_size[0] = palette_mode_info.palette_size[0]; |
| 9620 | if (palette_mode_info.palette_size[0] > 0) { |
| 9621 | memcpy(pmi->palette_colors, palette_mode_info.palette_colors, |
| 9622 | PALETTE_MAX_SIZE * sizeof(palette_mode_info.palette_colors[0])); |
| 9623 | memcpy(color_map, best_palette_color_map, |
| 9624 | rows * cols * sizeof(best_palette_color_map[0])); |
| 9625 | } |
| 9626 | super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable, NULL, bsize, |
| 9627 | best_rd); |
| 9628 | if (rate_y == INT_MAX) goto PALETTE_EXIT; |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 9629 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x] |
| 9630 | [xd->plane[1].subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9631 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
| 9632 | choose_intra_uv_mode(cpi, x, ctx, bsize, uv_tx, &rate_uv_intra[uv_tx], |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 9633 | &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx], |
| 9634 | &skip_uvs[uv_tx], &mode_uv[uv_tx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9635 | pmi_uv[uv_tx] = *pmi; |
| 9636 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9637 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
| 9638 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9639 | #if CONFIG_FILTER_INTRA |
| 9640 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 9641 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9642 | } |
| 9643 | mbmi->uv_mode = mode_uv[uv_tx]; |
| 9644 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
| 9645 | if (pmi->palette_size[1] > 0) |
| 9646 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 9647 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 9648 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
| 9649 | #if CONFIG_EXT_INTRA |
| 9650 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9651 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9652 | #if CONFIG_FILTER_INTRA |
| 9653 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 9654 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 9655 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 9656 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 9657 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
| 9658 | } |
| 9659 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c287e27 | 2016-10-20 18:19:16 -0700 | [diff] [blame] | 9660 | skippable = skippable && skip_uvs[uv_tx]; |
| 9661 | distortion2 = distortion_y + dist_uvs[uv_tx]; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 9662 | rate2 = rate_y + rate_overhead_palette + rate_uv_intra[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9663 | rate2 += ref_costs_single[INTRA_FRAME]; |
| 9664 | |
| 9665 | if (skippable) { |
| 9666 | rate2 -= (rate_y + rate_uv_tokenonly[uv_tx]); |
| 9667 | #if CONFIG_SUPERTX |
| 9668 | best_rate_nocoef = rate2; |
| 9669 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9670 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9671 | } else { |
| 9672 | #if CONFIG_SUPERTX |
| 9673 | best_rate_nocoef = rate2 - (rate_y + rate_uv_tokenonly[uv_tx]); |
| 9674 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9675 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9676 | } |
| 9677 | this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); |
| 9678 | if (this_rd < best_rd) { |
| 9679 | best_mode_index = 3; |
| 9680 | mbmi->mv[0].as_int = 0; |
| 9681 | rd_cost->rate = rate2; |
| 9682 | #if CONFIG_SUPERTX |
| 9683 | *returnrate_nocoef = best_rate_nocoef; |
| 9684 | #endif |
| 9685 | rd_cost->dist = distortion2; |
| 9686 | rd_cost->rdcost = this_rd; |
| 9687 | best_rd = this_rd; |
| 9688 | best_mbmode = *mbmi; |
| 9689 | best_skip2 = 0; |
| 9690 | best_mode_skippable = skippable; |
| 9691 | } |
| 9692 | } |
| 9693 | PALETTE_EXIT: |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9694 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9695 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9696 | #if CONFIG_FILTER_INTRA |
| 9697 | // TODO(huisu): filter-intra is turned off in lossless mode for now to |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9698 | // avoid a unit test failure |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9699 | if (!xd->lossless[mbmi->segment_id] && |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9700 | #if CONFIG_PALETTE |
| 9701 | mbmi->palette_mode_info.palette_size[0] == 0 && |
| 9702 | #endif // CONFIG_PALETTE |
| 9703 | !dc_skipped && best_mode_index >= 0 && |
| 9704 | best_intra_rd < (best_rd + (best_rd >> 3))) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9705 | pick_filter_intra_interframe( |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 9706 | cpi, x, ctx, bsize, rate_uv_intra, rate_uv_tokenonly, dist_uvs, |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9707 | skip_uvs, mode_uv, filter_intra_mode_info_uv, |
| 9708 | #if CONFIG_EXT_INTRA |
| 9709 | uv_angle_delta, |
| 9710 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9711 | #if CONFIG_PALETTE |
| 9712 | pmi_uv, palette_ctx, |
| 9713 | #endif // CONFIG_PALETTE |
| 9714 | 0, ref_costs_single, &best_rd, &best_intra_rd, &best_intra_mode, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9715 | &best_mode_index, &best_skip2, &best_mode_skippable, |
| 9716 | #if CONFIG_SUPERTX |
| 9717 | returnrate_nocoef, |
| 9718 | #endif // CONFIG_SUPERTX |
| 9719 | best_pred_rd, &best_mbmode, rd_cost); |
| 9720 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9721 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9722 | |
| 9723 | // The inter modes' rate costs are not calculated precisely in some cases. |
| 9724 | // Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and |
| 9725 | // ZEROMV. Here, checks are added for those cases, and the mode decisions |
| 9726 | // are corrected. |
| 9727 | if (best_mbmode.mode == NEWMV |
| 9728 | #if CONFIG_EXT_INTER |
| 9729 | || best_mbmode.mode == NEWFROMNEARMV || best_mbmode.mode == NEW_NEWMV |
| 9730 | #endif // CONFIG_EXT_INTER |
| 9731 | ) { |
| 9732 | const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0], |
| 9733 | best_mbmode.ref_frame[1] }; |
| 9734 | int comp_pred_mode = refs[1] > INTRA_FRAME; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9735 | int_mv zeromv[2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9736 | #if CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9737 | const uint8_t rf_type = av1_ref_frame_type(best_mbmode.ref_frame); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9738 | #endif // CONFIG_REF_MV |
| 9739 | #if CONFIG_GLOBAL_MOTION |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 9740 | zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]]).as_int; |
Sarah Parker | 70c4fab | 2016-11-04 12:35:57 -0700 | [diff] [blame] | 9741 | zeromv[1].as_int = |
| 9742 | comp_pred_mode |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 9743 | ? gm_get_motion_vector(&cm->global_motion[refs[1]]).as_int |
Sarah Parker | 70c4fab | 2016-11-04 12:35:57 -0700 | [diff] [blame] | 9744 | : 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9745 | #else |
| 9746 | zeromv[0].as_int = 0; |
| 9747 | zeromv[1].as_int = 0; |
| 9748 | #endif // CONFIG_GLOBAL_MOTION |
| 9749 | #if CONFIG_REF_MV |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9750 | if (!comp_pred_mode) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9751 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9752 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9753 | : INT_MAX; |
| 9754 | |
| 9755 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 9756 | int_mv cur_mv = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 9757 | if (cur_mv.as_int == best_mbmode.mv[0].as_int) { |
| 9758 | best_mbmode.mode = NEARMV; |
| 9759 | best_mbmode.ref_mv_idx = i; |
| 9760 | } |
| 9761 | } |
| 9762 | |
| 9763 | if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int) |
| 9764 | best_mbmode.mode = NEARESTMV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9765 | else if (best_mbmode.mv[0].as_int == zeromv[0].as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9766 | best_mbmode.mode = ZEROMV; |
| 9767 | } else { |
| 9768 | int_mv nearestmv[2]; |
| 9769 | int_mv nearmv[2]; |
| 9770 | |
| 9771 | #if CONFIG_EXT_INTER |
| 9772 | if (mbmi_ext->ref_mv_count[rf_type] > 1) { |
| 9773 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][1].this_mv; |
| 9774 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][1].comp_mv; |
| 9775 | } else { |
| 9776 | nearmv[0] = frame_mv[NEARMV][refs[0]]; |
| 9777 | nearmv[1] = frame_mv[NEARMV][refs[1]]; |
| 9778 | } |
| 9779 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9780 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9781 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9782 | : INT_MAX; |
| 9783 | |
| 9784 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 9785 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 9786 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][i + 1].comp_mv; |
| 9787 | |
| 9788 | if (nearmv[0].as_int == best_mbmode.mv[0].as_int && |
| 9789 | nearmv[1].as_int == best_mbmode.mv[1].as_int) { |
| 9790 | best_mbmode.mode = NEARMV; |
| 9791 | best_mbmode.ref_mv_idx = i; |
| 9792 | } |
| 9793 | } |
| 9794 | #endif |
| 9795 | if (mbmi_ext->ref_mv_count[rf_type] >= 1) { |
| 9796 | nearestmv[0] = mbmi_ext->ref_mv_stack[rf_type][0].this_mv; |
| 9797 | nearestmv[1] = mbmi_ext->ref_mv_stack[rf_type][0].comp_mv; |
| 9798 | } else { |
| 9799 | nearestmv[0] = frame_mv[NEARESTMV][refs[0]]; |
| 9800 | nearestmv[1] = frame_mv[NEARESTMV][refs[1]]; |
| 9801 | } |
| 9802 | |
| 9803 | if (nearestmv[0].as_int == best_mbmode.mv[0].as_int && |
| 9804 | nearestmv[1].as_int == best_mbmode.mv[1].as_int) |
| 9805 | #if CONFIG_EXT_INTER |
| 9806 | best_mbmode.mode = NEAREST_NEARESTMV; |
| 9807 | else if (nearestmv[0].as_int == best_mbmode.mv[0].as_int && |
| 9808 | nearmv[1].as_int == best_mbmode.mv[1].as_int) |
| 9809 | best_mbmode.mode = NEAREST_NEARMV; |
| 9810 | else if (nearmv[0].as_int == best_mbmode.mv[0].as_int && |
| 9811 | nearestmv[1].as_int == best_mbmode.mv[1].as_int) |
| 9812 | best_mbmode.mode = NEAR_NEARESTMV; |
| 9813 | else if (nearmv[0].as_int == best_mbmode.mv[0].as_int && |
| 9814 | nearmv[1].as_int == best_mbmode.mv[1].as_int) |
| 9815 | best_mbmode.mode = NEAR_NEARMV; |
| 9816 | else if (best_mbmode.mv[0].as_int == 0 && best_mbmode.mv[1].as_int == 0) |
| 9817 | best_mbmode.mode = ZERO_ZEROMV; |
| 9818 | #else |
| 9819 | best_mbmode.mode = NEARESTMV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9820 | else if (best_mbmode.mv[0].as_int == zeromv[0].as_int && |
| 9821 | best_mbmode.mv[1].as_int == zeromv[1].as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9822 | best_mbmode.mode = ZEROMV; |
| 9823 | #endif // CONFIG_EXT_INTER |
| 9824 | } |
| 9825 | #else |
| 9826 | #if CONFIG_EXT_INTER |
| 9827 | if (!comp_pred_mode) { |
| 9828 | #endif // CONFIG_EXT_INTER |
| 9829 | if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int && |
| 9830 | ((comp_pred_mode && |
| 9831 | frame_mv[NEARESTMV][refs[1]].as_int == best_mbmode.mv[1].as_int) || |
| 9832 | !comp_pred_mode)) |
| 9833 | best_mbmode.mode = NEARESTMV; |
| 9834 | else if (frame_mv[NEARMV][refs[0]].as_int == best_mbmode.mv[0].as_int && |
| 9835 | ((comp_pred_mode && |
| 9836 | frame_mv[NEARMV][refs[1]].as_int == |
| 9837 | best_mbmode.mv[1].as_int) || |
| 9838 | !comp_pred_mode)) |
| 9839 | best_mbmode.mode = NEARMV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9840 | else if (best_mbmode.mv[0].as_int == zeromv[0].as_int && |
| 9841 | ((comp_pred_mode && |
| 9842 | best_mbmode.mv[1].as_int == zeromv[1].as_int) || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9843 | !comp_pred_mode)) |
| 9844 | best_mbmode.mode = ZEROMV; |
| 9845 | #if CONFIG_EXT_INTER |
| 9846 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9847 | if (frame_mv[NEAREST_NEARESTMV][refs[0]].as_int == |
| 9848 | best_mbmode.mv[0].as_int && |
| 9849 | frame_mv[NEAREST_NEARESTMV][refs[1]].as_int == |
| 9850 | best_mbmode.mv[1].as_int) |
| 9851 | best_mbmode.mode = NEAREST_NEARESTMV; |
| 9852 | else if (frame_mv[NEAREST_NEARMV][refs[0]].as_int == |
| 9853 | best_mbmode.mv[0].as_int && |
| 9854 | frame_mv[NEAREST_NEARMV][refs[1]].as_int == |
| 9855 | best_mbmode.mv[1].as_int) |
| 9856 | best_mbmode.mode = NEAREST_NEARMV; |
| 9857 | else if (frame_mv[NEAR_NEARESTMV][refs[0]].as_int == |
| 9858 | best_mbmode.mv[0].as_int && |
| 9859 | frame_mv[NEAR_NEARESTMV][refs[1]].as_int == |
| 9860 | best_mbmode.mv[1].as_int) |
| 9861 | best_mbmode.mode = NEAR_NEARESTMV; |
| 9862 | else if (frame_mv[NEAR_NEARMV][refs[0]].as_int == |
| 9863 | best_mbmode.mv[0].as_int && |
| 9864 | frame_mv[NEAR_NEARMV][refs[1]].as_int == |
| 9865 | best_mbmode.mv[1].as_int) |
| 9866 | best_mbmode.mode = NEAR_NEARMV; |
| 9867 | else if (best_mbmode.mv[0].as_int == 0 && best_mbmode.mv[1].as_int == 0) |
| 9868 | best_mbmode.mode = ZERO_ZEROMV; |
| 9869 | } |
| 9870 | #endif // CONFIG_EXT_INTER |
| 9871 | #endif |
| 9872 | } |
| 9873 | |
| 9874 | #if CONFIG_REF_MV |
| 9875 | if (best_mbmode.ref_frame[0] > INTRA_FRAME && best_mbmode.mv[0].as_int == 0 && |
| 9876 | #if CONFIG_EXT_INTER |
| 9877 | (best_mbmode.ref_frame[1] <= INTRA_FRAME) |
| 9878 | #else |
| 9879 | (best_mbmode.ref_frame[1] == NONE || best_mbmode.mv[1].as_int == 0) |
| 9880 | #endif // CONFIG_EXT_INTER |
| 9881 | ) { |
| 9882 | int16_t mode_ctx = mbmi_ext->mode_context[best_mbmode.ref_frame[0]]; |
| 9883 | #if !CONFIG_EXT_INTER |
| 9884 | if (best_mbmode.ref_frame[1] > NONE) |
| 9885 | mode_ctx &= (mbmi_ext->mode_context[best_mbmode.ref_frame[1]] | 0x00ff); |
| 9886 | #endif // !CONFIG_EXT_INTER |
| 9887 | |
| 9888 | if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) best_mbmode.mode = ZEROMV; |
| 9889 | } |
| 9890 | #endif |
| 9891 | |
| 9892 | if (best_mode_index < 0 || best_rd >= best_rd_so_far) { |
| 9893 | rd_cost->rate = INT_MAX; |
| 9894 | rd_cost->rdcost = INT64_MAX; |
| 9895 | return; |
| 9896 | } |
| 9897 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9898 | #if CONFIG_DUAL_FILTER |
| 9899 | assert((cm->interp_filter == SWITCHABLE) || |
| 9900 | (cm->interp_filter == best_mbmode.interp_filter[0]) || |
| 9901 | !is_inter_block(&best_mbmode)); |
| 9902 | assert((cm->interp_filter == SWITCHABLE) || |
| 9903 | (cm->interp_filter == best_mbmode.interp_filter[1]) || |
| 9904 | !is_inter_block(&best_mbmode)); |
| 9905 | if (best_mbmode.ref_frame[1] > INTRA_FRAME) { |
| 9906 | assert((cm->interp_filter == SWITCHABLE) || |
| 9907 | (cm->interp_filter == best_mbmode.interp_filter[2]) || |
| 9908 | !is_inter_block(&best_mbmode)); |
| 9909 | assert((cm->interp_filter == SWITCHABLE) || |
| 9910 | (cm->interp_filter == best_mbmode.interp_filter[3]) || |
| 9911 | !is_inter_block(&best_mbmode)); |
| 9912 | } |
| 9913 | #else |
| 9914 | assert((cm->interp_filter == SWITCHABLE) || |
| 9915 | (cm->interp_filter == best_mbmode.interp_filter) || |
| 9916 | !is_inter_block(&best_mbmode)); |
| 9917 | #endif |
| 9918 | |
| 9919 | if (!cpi->rc.is_src_frame_alt_ref) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9920 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
| 9921 | sf->adaptive_rd_thresh, bsize, best_mode_index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9922 | |
| 9923 | // macroblock modes |
| 9924 | *mbmi = best_mbmode; |
| 9925 | x->skip |= best_skip2; |
| 9926 | |
| 9927 | #if CONFIG_REF_MV |
| 9928 | for (i = 0; i < 1 + has_second_ref(mbmi); ++i) { |
| 9929 | if (mbmi->mode != NEWMV) |
| 9930 | mbmi->pred_mv[i].as_int = mbmi->mv[i].as_int; |
| 9931 | else |
| 9932 | mbmi->pred_mv[i].as_int = mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_int; |
| 9933 | } |
| 9934 | #endif |
| 9935 | |
| 9936 | for (i = 0; i < REFERENCE_MODES; ++i) { |
| 9937 | if (best_pred_rd[i] == INT64_MAX) |
| 9938 | best_pred_diff[i] = INT_MIN; |
| 9939 | else |
| 9940 | best_pred_diff[i] = best_rd - best_pred_rd[i]; |
| 9941 | } |
| 9942 | |
| 9943 | x->skip |= best_mode_skippable; |
| 9944 | |
| 9945 | assert(best_mode_index >= 0); |
| 9946 | |
| 9947 | store_coding_context(x, ctx, best_mode_index, best_pred_diff, |
| 9948 | best_mode_skippable); |
| 9949 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9950 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9951 | if (cm->allow_screen_content_tools && pmi->palette_size[1] > 0) { |
| 9952 | restore_uv_color_map(cpi, x); |
| 9953 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9954 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9955 | } |
| 9956 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9957 | void av1_rd_pick_inter_mode_sb_seg_skip(const AV1_COMP *cpi, |
| 9958 | TileDataEnc *tile_data, MACROBLOCK *x, |
| 9959 | RD_COST *rd_cost, BLOCK_SIZE bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9960 | PICK_MODE_CONTEXT *ctx, |
| 9961 | int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9962 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9963 | MACROBLOCKD *const xd = &x->e_mbd; |
| 9964 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 9965 | unsigned char segment_id = mbmi->segment_id; |
| 9966 | const int comp_pred = 0; |
| 9967 | int i; |
| 9968 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 9969 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
| 9970 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9971 | aom_prob comp_mode_p; |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 9972 | InterpFilter best_filter = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9973 | int64_t this_rd = INT64_MAX; |
| 9974 | int rate2 = 0; |
| 9975 | const int64_t distortion2 = 0; |
| 9976 | |
| 9977 | estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp, |
| 9978 | &comp_mode_p); |
| 9979 | |
| 9980 | for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX; |
| 9981 | for (i = LAST_FRAME; i < TOTAL_REFS_PER_FRAME; ++i) |
| 9982 | x->pred_mv_sad[i] = INT_MAX; |
| 9983 | |
| 9984 | rd_cost->rate = INT_MAX; |
| 9985 | |
| 9986 | assert(segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)); |
| 9987 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9988 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9989 | mbmi->palette_mode_info.palette_size[0] = 0; |
| 9990 | mbmi->palette_mode_info.palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9991 | #endif // CONFIG_PALETTE |
| 9992 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9993 | #if CONFIG_FILTER_INTRA |
| 9994 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 9995 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 9996 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9997 | mbmi->mode = ZEROMV; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9998 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9999 | mbmi->uv_mode = DC_PRED; |
| 10000 | mbmi->ref_frame[0] = LAST_FRAME; |
| 10001 | mbmi->ref_frame[1] = NONE; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10002 | #if CONFIG_GLOBAL_MOTION |
| 10003 | mbmi->mv[0].as_int = |
Debargha Mukherjee | 5f30585 | 2016-11-03 15:47:21 -0700 | [diff] [blame] | 10004 | gm_get_motion_vector(&cm->global_motion[mbmi->ref_frame[0]]).as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10005 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10006 | mbmi->mv[0].as_int = 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10007 | #endif // CONFIG_GLOBAL_MOTION |
Jingning Han | 6408895 | 2016-07-11 11:24:24 -0700 | [diff] [blame] | 10008 | mbmi->tx_size = max_txsize_lookup[bsize]; |
Yaowu Xu | ee775b1 | 2016-10-18 10:00:21 -0700 | [diff] [blame] | 10009 | x->skip = 1; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10010 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10011 | #if CONFIG_REF_MV |
| 10012 | mbmi->ref_mv_idx = 0; |
| 10013 | mbmi->pred_mv[0].as_int = 0; |
| 10014 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10015 | |
| 10016 | if (cm->interp_filter != BILINEAR) { |
| 10017 | best_filter = EIGHTTAP_REGULAR; |
| 10018 | if (cm->interp_filter == SWITCHABLE && |
| 10019 | #if CONFIG_EXT_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10020 | av1_is_interp_needed(xd) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10021 | #endif // CONFIG_EXT_INTERP |
| 10022 | x->source_variance >= cpi->sf.disable_filter_search_var_thresh) { |
| 10023 | int rs; |
| 10024 | int best_rs = INT_MAX; |
| 10025 | for (i = 0; i < SWITCHABLE_FILTERS; ++i) { |
| 10026 | #if CONFIG_DUAL_FILTER |
| 10027 | int k; |
| 10028 | for (k = 0; k < 4; ++k) mbmi->interp_filter[k] = i; |
| 10029 | #else |
| 10030 | mbmi->interp_filter = i; |
| 10031 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10032 | rs = av1_get_switchable_rate(cpi, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10033 | if (rs < best_rs) { |
| 10034 | best_rs = rs; |
| 10035 | #if CONFIG_DUAL_FILTER |
| 10036 | best_filter = mbmi->interp_filter[0]; |
| 10037 | #else |
| 10038 | best_filter = mbmi->interp_filter; |
| 10039 | #endif |
| 10040 | } |
| 10041 | } |
| 10042 | } |
| 10043 | } |
| 10044 | // Set the appropriate filter |
| 10045 | if (cm->interp_filter == SWITCHABLE) { |
| 10046 | #if CONFIG_DUAL_FILTER |
| 10047 | for (i = 0; i < 4; ++i) mbmi->interp_filter[i] = best_filter; |
| 10048 | #else |
| 10049 | mbmi->interp_filter = best_filter; |
| 10050 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10051 | rate2 += av1_get_switchable_rate(cpi, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10052 | } else { |
| 10053 | #if CONFIG_DUAL_FILTER |
| 10054 | for (i = 0; i < 4; ++i) mbmi->interp_filter[0] = cm->interp_filter; |
| 10055 | #else |
| 10056 | mbmi->interp_filter = cm->interp_filter; |
| 10057 | #endif |
| 10058 | } |
| 10059 | |
| 10060 | if (cm->reference_mode == REFERENCE_MODE_SELECT) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10061 | rate2 += av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10062 | |
| 10063 | // Estimate the reference frame signaling cost and add it |
| 10064 | // to the rolling cost variable. |
| 10065 | rate2 += ref_costs_single[LAST_FRAME]; |
| 10066 | this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); |
| 10067 | |
| 10068 | rd_cost->rate = rate2; |
| 10069 | rd_cost->dist = distortion2; |
| 10070 | rd_cost->rdcost = this_rd; |
| 10071 | |
| 10072 | if (this_rd >= best_rd_so_far) { |
| 10073 | rd_cost->rate = INT_MAX; |
| 10074 | rd_cost->rdcost = INT64_MAX; |
| 10075 | return; |
| 10076 | } |
| 10077 | |
| 10078 | #if CONFIG_DUAL_FILTER |
| 10079 | assert((cm->interp_filter == SWITCHABLE) || |
| 10080 | (cm->interp_filter == mbmi->interp_filter[0])); |
| 10081 | #else |
| 10082 | assert((cm->interp_filter == SWITCHABLE) || |
| 10083 | (cm->interp_filter == mbmi->interp_filter)); |
| 10084 | #endif |
| 10085 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10086 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
| 10087 | cpi->sf.adaptive_rd_thresh, bsize, THR_ZEROMV); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10088 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10089 | av1_zero(best_pred_diff); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10090 | |
| 10091 | store_coding_context(x, ctx, THR_ZEROMV, best_pred_diff, 0); |
| 10092 | } |
| 10093 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10094 | void av1_rd_pick_inter_mode_sub8x8(const struct AV1_COMP *cpi, |
| 10095 | TileDataEnc *tile_data, struct macroblock *x, |
| 10096 | int mi_row, int mi_col, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10097 | struct RD_COST *rd_cost, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10098 | #if CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10099 | int *returnrate_nocoef, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10100 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10101 | BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, |
| 10102 | int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10103 | const AV1_COMMON *const cm = &cpi->common; |
| 10104 | const RD_OPT *const rd_opt = &cpi->rd; |
| 10105 | const SPEED_FEATURES *const sf = &cpi->sf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10106 | MACROBLOCKD *const xd = &x->e_mbd; |
| 10107 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 10108 | const struct segmentation *const seg = &cm->seg; |
| 10109 | MV_REFERENCE_FRAME ref_frame, second_ref_frame; |
| 10110 | unsigned char segment_id = mbmi->segment_id; |
| 10111 | int comp_pred, i; |
| 10112 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 10113 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]; |
| 10114 | static const int flag_list[TOTAL_REFS_PER_FRAME] = { |
| 10115 | 0, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10116 | AOM_LAST_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10117 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10118 | AOM_LAST2_FLAG, |
| 10119 | AOM_LAST3_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10120 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10121 | AOM_GOLD_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10122 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10123 | AOM_BWD_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10124 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10125 | AOM_ALT_FLAG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10126 | }; |
| 10127 | int64_t best_rd = best_rd_so_far; |
| 10128 | int64_t best_yrd = best_rd_so_far; // FIXME(rbultje) more precise |
| 10129 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 10130 | int64_t best_pred_rd[REFERENCE_MODES]; |
| 10131 | MB_MODE_INFO best_mbmode; |
| 10132 | int ref_index, best_ref_index = 0; |
| 10133 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
| 10134 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10135 | aom_prob comp_mode_p; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10136 | #if CONFIG_DUAL_FILTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 10137 | InterpFilter tmp_best_filter[4] = { 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10138 | #else |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 10139 | InterpFilter tmp_best_filter = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10140 | #endif |
Jingning Han | 3f16725 | 2016-06-07 16:11:42 -0700 | [diff] [blame] | 10141 | int rate_uv_intra, rate_uv_tokenonly = INT_MAX; |
| 10142 | int64_t dist_uv = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10143 | int skip_uv; |
| 10144 | PREDICTION_MODE mode_uv = DC_PRED; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10145 | const int intra_cost_penalty = av1_get_intra_cost_penalty( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10146 | cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth); |
| 10147 | #if CONFIG_EXT_INTER |
| 10148 | int_mv seg_mvs[4][2][TOTAL_REFS_PER_FRAME]; |
| 10149 | #else |
| 10150 | int_mv seg_mvs[4][TOTAL_REFS_PER_FRAME]; |
| 10151 | #endif // CONFIG_EXT_INTER |
| 10152 | b_mode_info best_bmodes[4]; |
| 10153 | int best_skip2 = 0; |
| 10154 | int ref_frame_skip_mask[2] = { 0 }; |
| 10155 | int internal_active_edge = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10156 | av1_active_edge_sb(cpi, mi_row, mi_col) && av1_internal_image_edge(cpi); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10157 | #if CONFIG_PVQ |
| 10158 | od_rollback_buffer pre_buf; |
| 10159 | |
| 10160 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 10161 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10162 | |
| 10163 | #if CONFIG_SUPERTX |
| 10164 | best_rd_so_far = INT64_MAX; |
| 10165 | best_rd = best_rd_so_far; |
| 10166 | best_yrd = best_rd_so_far; |
| 10167 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10168 | av1_zero(best_mbmode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10169 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10170 | #if CONFIG_FILTER_INTRA |
| 10171 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 10172 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 10173 | #endif // CONFIG_FILTER_INTRA |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10174 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10175 | #if CONFIG_EXT_INTER |
| 10176 | mbmi->use_wedge_interinter = 0; |
| 10177 | mbmi->use_wedge_interintra = 0; |
| 10178 | #endif // CONFIG_EXT_INTER |
| 10179 | |
| 10180 | for (i = 0; i < 4; i++) { |
| 10181 | int j; |
| 10182 | #if CONFIG_EXT_INTER |
| 10183 | int k; |
| 10184 | |
| 10185 | for (k = 0; k < 2; k++) |
| 10186 | for (j = 0; j < TOTAL_REFS_PER_FRAME; j++) |
| 10187 | seg_mvs[i][k][j].as_int = INVALID_MV; |
| 10188 | #else |
| 10189 | for (j = 0; j < TOTAL_REFS_PER_FRAME; j++) |
| 10190 | seg_mvs[i][j].as_int = INVALID_MV; |
| 10191 | #endif // CONFIG_EXT_INTER |
| 10192 | } |
| 10193 | |
| 10194 | estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp, |
| 10195 | &comp_mode_p); |
| 10196 | |
| 10197 | for (i = 0; i < REFERENCE_MODES; ++i) best_pred_rd[i] = INT64_MAX; |
| 10198 | rate_uv_intra = INT_MAX; |
| 10199 | |
| 10200 | rd_cost->rate = INT_MAX; |
| 10201 | #if CONFIG_SUPERTX |
| 10202 | *returnrate_nocoef = INT_MAX; |
| 10203 | #endif |
| 10204 | |
| 10205 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { |
| 10206 | x->mbmi_ext->mode_context[ref_frame] = 0; |
| 10207 | #if CONFIG_REF_MV && CONFIG_EXT_INTER |
| 10208 | x->mbmi_ext->compound_mode_context[ref_frame] = 0; |
| 10209 | #endif // CONFIG_REF_MV && CONFIG_EXT_INTER |
| 10210 | if (cpi->ref_frame_flags & flag_list[ref_frame]) { |
| 10211 | setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col, |
| 10212 | frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb); |
| 10213 | } else { |
| 10214 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 10215 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10216 | } |
| 10217 | frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; |
| 10218 | #if CONFIG_EXT_INTER |
| 10219 | frame_mv[NEWFROMNEARMV][ref_frame].as_int = INVALID_MV; |
| 10220 | #endif // CONFIG_EXT_INTER |
| 10221 | frame_mv[ZEROMV][ref_frame].as_int = 0; |
| 10222 | } |
| 10223 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10224 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10225 | mbmi->palette_mode_info.palette_size[0] = 0; |
| 10226 | mbmi->palette_mode_info.palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10227 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10228 | |
| 10229 | for (ref_index = 0; ref_index < MAX_REFS; ++ref_index) { |
| 10230 | int mode_excluded = 0; |
| 10231 | int64_t this_rd = INT64_MAX; |
| 10232 | int disable_skip = 0; |
| 10233 | int compmode_cost = 0; |
| 10234 | int rate2 = 0, rate_y = 0, rate_uv = 0; |
| 10235 | int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0; |
| 10236 | int skippable = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10237 | int this_skip2 = 0; |
| 10238 | int64_t total_sse = INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10239 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10240 | #if CONFIG_PVQ |
| 10241 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 10242 | #endif |
| 10243 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10244 | ref_frame = av1_ref_order[ref_index].ref_frame[0]; |
| 10245 | second_ref_frame = av1_ref_order[ref_index].ref_frame[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10246 | |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 10247 | #if CONFIG_REF_MV |
| 10248 | mbmi->ref_mv_idx = 0; |
| 10249 | #endif |
| 10250 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10251 | // Look at the reference frame of the best mode so far and set the |
| 10252 | // skip mask to look at a subset of the remaining modes. |
| 10253 | if (ref_index > 2 && sf->mode_skip_start < MAX_MODES) { |
| 10254 | if (ref_index == 3) { |
| 10255 | switch (best_mbmode.ref_frame[0]) { |
| 10256 | case INTRA_FRAME: break; |
| 10257 | case LAST_FRAME: |
| 10258 | ref_frame_skip_mask[0] |= (1 << GOLDEN_FRAME) | |
| 10259 | #if CONFIG_EXT_REFS |
| 10260 | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | |
| 10261 | (1 << BWDREF_FRAME) | |
| 10262 | #endif // CONFIG_EXT_REFS |
| 10263 | (1 << ALTREF_FRAME); |
| 10264 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10265 | break; |
| 10266 | #if CONFIG_EXT_REFS |
| 10267 | case LAST2_FRAME: |
| 10268 | ref_frame_skip_mask[0] |= (1 << LAST_FRAME) | (1 << LAST3_FRAME) | |
| 10269 | (1 << GOLDEN_FRAME) | |
| 10270 | (1 << BWDREF_FRAME) | (1 << ALTREF_FRAME); |
| 10271 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10272 | break; |
| 10273 | case LAST3_FRAME: |
| 10274 | ref_frame_skip_mask[0] |= (1 << LAST_FRAME) | (1 << LAST2_FRAME) | |
| 10275 | (1 << GOLDEN_FRAME) | |
| 10276 | (1 << BWDREF_FRAME) | (1 << ALTREF_FRAME); |
| 10277 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10278 | break; |
| 10279 | #endif // CONFIG_EXT_REFS |
| 10280 | case GOLDEN_FRAME: |
| 10281 | ref_frame_skip_mask[0] |= (1 << LAST_FRAME) | |
| 10282 | #if CONFIG_EXT_REFS |
| 10283 | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | |
| 10284 | (1 << BWDREF_FRAME) | |
| 10285 | #endif // CONFIG_EXT_REFS |
| 10286 | (1 << ALTREF_FRAME); |
| 10287 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10288 | break; |
| 10289 | #if CONFIG_EXT_REFS |
| 10290 | case BWDREF_FRAME: |
| 10291 | ref_frame_skip_mask[0] |= (1 << LAST_FRAME) | (1 << LAST2_FRAME) | |
| 10292 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | |
| 10293 | (1 << ALTREF_FRAME); |
| 10294 | ref_frame_skip_mask[1] |= (1 << ALTREF_FRAME) | 0x01; |
| 10295 | break; |
| 10296 | #endif // CONFIG_EXT_REFS |
| 10297 | case ALTREF_FRAME: |
| 10298 | ref_frame_skip_mask[0] |= (1 << LAST_FRAME) | |
| 10299 | #if CONFIG_EXT_REFS |
| 10300 | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | |
| 10301 | (1 << BWDREF_FRAME) | |
| 10302 | #endif // CONFIG_EXT_REFS |
| 10303 | (1 << GOLDEN_FRAME); |
| 10304 | #if CONFIG_EXT_REFS |
| 10305 | ref_frame_skip_mask[1] |= (1 << BWDREF_FRAME) | 0x01; |
| 10306 | #endif // CONFIG_EXT_REFS |
| 10307 | break; |
| 10308 | case NONE: |
| 10309 | case TOTAL_REFS_PER_FRAME: |
| 10310 | assert(0 && "Invalid Reference frame"); |
| 10311 | break; |
| 10312 | } |
| 10313 | } |
| 10314 | } |
| 10315 | |
| 10316 | if ((ref_frame_skip_mask[0] & (1 << ref_frame)) && |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10317 | (ref_frame_skip_mask[1] & (1 << AOMMAX(0, second_ref_frame)))) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10318 | continue; |
| 10319 | |
| 10320 | // Test best rd so far against threshold for trying this mode. |
| 10321 | if (!internal_active_edge && |
| 10322 | rd_less_than_thresh(best_rd, |
| 10323 | rd_opt->threshes[segment_id][bsize][ref_index], |
| 10324 | tile_data->thresh_freq_fact[bsize][ref_index])) |
| 10325 | continue; |
| 10326 | |
| 10327 | comp_pred = second_ref_frame > INTRA_FRAME; |
| 10328 | if (comp_pred) { |
| 10329 | if (!cpi->allow_comp_inter_inter) continue; |
| 10330 | if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue; |
| 10331 | // Do not allow compound prediction if the segment level reference frame |
| 10332 | // feature is in use as in this case there can only be one reference. |
| 10333 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue; |
| 10334 | |
| 10335 | if ((sf->mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) && |
| 10336 | best_mbmode.ref_frame[0] == INTRA_FRAME) |
| 10337 | continue; |
| 10338 | } |
| 10339 | |
| 10340 | // TODO(jingning, jkoleszar): scaling reference frame not supported for |
| 10341 | // sub8x8 blocks. |
| 10342 | if (ref_frame > INTRA_FRAME && |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10343 | av1_is_scaled(&cm->frame_refs[ref_frame - 1].sf)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10344 | continue; |
| 10345 | |
| 10346 | if (second_ref_frame > INTRA_FRAME && |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10347 | av1_is_scaled(&cm->frame_refs[second_ref_frame - 1].sf)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10348 | continue; |
| 10349 | |
| 10350 | if (comp_pred) |
| 10351 | mode_excluded = cm->reference_mode == SINGLE_REFERENCE; |
| 10352 | else if (ref_frame != INTRA_FRAME) |
| 10353 | mode_excluded = cm->reference_mode == COMPOUND_REFERENCE; |
| 10354 | |
| 10355 | // If the segment reference frame feature is enabled.... |
| 10356 | // then do nothing if the current ref frame is not allowed.. |
| 10357 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && |
| 10358 | get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) { |
| 10359 | continue; |
| 10360 | // Disable this drop out case if the ref frame |
| 10361 | // segment level feature is enabled for this segment. This is to |
| 10362 | // prevent the possibility that we end up unable to pick any mode. |
| 10363 | } else if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) { |
| 10364 | // Only consider ZEROMV/ALTREF_FRAME for alt ref frame, |
| 10365 | // unless ARNR filtering is enabled in which case we want |
| 10366 | // an unfiltered alternative. We allow near/nearest as well |
| 10367 | // because they may result in zero-zero MVs but be cheaper. |
| 10368 | if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) |
| 10369 | continue; |
| 10370 | } |
| 10371 | |
| 10372 | mbmi->tx_size = TX_4X4; |
| 10373 | mbmi->uv_mode = DC_PRED; |
| 10374 | mbmi->ref_frame[0] = ref_frame; |
| 10375 | mbmi->ref_frame[1] = second_ref_frame; |
| 10376 | // Evaluate all sub-pel filters irrespective of whether we can use |
| 10377 | // them for this frame. |
| 10378 | #if CONFIG_DUAL_FILTER |
| 10379 | for (i = 0; i < 4; ++i) |
| 10380 | mbmi->interp_filter[i] = cm->interp_filter == SWITCHABLE |
| 10381 | ? EIGHTTAP_REGULAR |
| 10382 | : cm->interp_filter; |
| 10383 | #else |
| 10384 | mbmi->interp_filter = |
| 10385 | cm->interp_filter == SWITCHABLE ? EIGHTTAP_REGULAR : cm->interp_filter; |
| 10386 | #endif |
| 10387 | x->skip = 0; |
| 10388 | set_ref_ptrs(cm, xd, ref_frame, second_ref_frame); |
| 10389 | |
| 10390 | // Select prediction reference frames. |
| 10391 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 10392 | xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; |
| 10393 | if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i]; |
| 10394 | } |
| 10395 | |
| 10396 | #if CONFIG_VAR_TX |
| 10397 | mbmi->inter_tx_size[0][0] = mbmi->tx_size; |
| 10398 | #endif |
| 10399 | |
| 10400 | if (ref_frame == INTRA_FRAME) { |
| 10401 | int rate; |
| 10402 | if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate, &rate_y, &distortion_y, |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 10403 | NULL, best_rd) >= best_rd) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10404 | continue; |
| 10405 | rate2 += rate; |
| 10406 | rate2 += intra_cost_penalty; |
| 10407 | distortion2 += distortion_y; |
| 10408 | |
| 10409 | if (rate_uv_intra == INT_MAX) { |
| 10410 | choose_intra_uv_mode(cpi, x, ctx, bsize, TX_4X4, &rate_uv_intra, |
| 10411 | &rate_uv_tokenonly, &dist_uv, &skip_uv, &mode_uv); |
| 10412 | } |
| 10413 | rate2 += rate_uv_intra; |
| 10414 | rate_uv = rate_uv_tokenonly; |
| 10415 | distortion2 += dist_uv; |
| 10416 | distortion_uv = dist_uv; |
| 10417 | mbmi->uv_mode = mode_uv; |
| 10418 | } else { |
| 10419 | int rate; |
| 10420 | int64_t distortion; |
| 10421 | int64_t this_rd_thresh; |
| 10422 | int64_t tmp_rd, tmp_best_rd = INT64_MAX, tmp_best_rdu = INT64_MAX; |
| 10423 | int tmp_best_rate = INT_MAX, tmp_best_ratey = INT_MAX; |
| 10424 | int64_t tmp_best_distortion = INT_MAX, tmp_best_sse, uv_sse; |
| 10425 | int tmp_best_skippable = 0; |
| 10426 | int switchable_filter_index; |
| 10427 | int_mv *second_ref = |
| 10428 | comp_pred ? &x->mbmi_ext->ref_mvs[second_ref_frame][0] : NULL; |
| 10429 | b_mode_info tmp_best_bmodes[16]; // Should this be 4 ? |
| 10430 | MB_MODE_INFO tmp_best_mbmode; |
| 10431 | #if CONFIG_DUAL_FILTER |
| 10432 | #if CONFIG_EXT_INTERP |
| 10433 | BEST_SEG_INFO bsi[25]; |
| 10434 | #else |
| 10435 | BEST_SEG_INFO bsi[9]; |
| 10436 | #endif |
| 10437 | #else |
| 10438 | BEST_SEG_INFO bsi[SWITCHABLE_FILTERS]; |
| 10439 | #endif |
| 10440 | int pred_exists = 0; |
| 10441 | int uv_skippable; |
| 10442 | #if CONFIG_EXT_INTER |
| 10443 | int_mv compound_seg_newmvs[4][2]; |
| 10444 | for (i = 0; i < 4; i++) { |
| 10445 | compound_seg_newmvs[i][0].as_int = INVALID_MV; |
| 10446 | compound_seg_newmvs[i][1].as_int = INVALID_MV; |
| 10447 | } |
| 10448 | #endif // CONFIG_EXT_INTER |
| 10449 | |
| 10450 | this_rd_thresh = (ref_frame == LAST_FRAME) |
| 10451 | ? rd_opt->threshes[segment_id][bsize][THR_LAST] |
| 10452 | : rd_opt->threshes[segment_id][bsize][THR_ALTR]; |
| 10453 | #if CONFIG_EXT_REFS |
| 10454 | this_rd_thresh = (ref_frame == LAST2_FRAME) |
| 10455 | ? rd_opt->threshes[segment_id][bsize][THR_LAST2] |
| 10456 | : this_rd_thresh; |
| 10457 | this_rd_thresh = (ref_frame == LAST3_FRAME) |
| 10458 | ? rd_opt->threshes[segment_id][bsize][THR_LAST3] |
| 10459 | : this_rd_thresh; |
Zoe Liu | a6a6dd5 | 2016-10-18 13:03:12 -0700 | [diff] [blame] | 10460 | this_rd_thresh = (ref_frame == BWDREF_FRAME) |
| 10461 | ? rd_opt->threshes[segment_id][bsize][THR_BWDR] |
| 10462 | : this_rd_thresh; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10463 | #endif // CONFIG_EXT_REFS |
| 10464 | this_rd_thresh = (ref_frame == GOLDEN_FRAME) |
| 10465 | ? rd_opt->threshes[segment_id][bsize][THR_GOLD] |
| 10466 | : this_rd_thresh; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10467 | |
| 10468 | // TODO(any): Add search of the tx_type to improve rd performance at the |
| 10469 | // expense of speed. |
| 10470 | mbmi->tx_type = DCT_DCT; |
| 10471 | |
| 10472 | if (cm->interp_filter != BILINEAR) { |
| 10473 | #if CONFIG_DUAL_FILTER |
| 10474 | tmp_best_filter[0] = EIGHTTAP_REGULAR; |
| 10475 | tmp_best_filter[1] = EIGHTTAP_REGULAR; |
| 10476 | tmp_best_filter[2] = EIGHTTAP_REGULAR; |
| 10477 | tmp_best_filter[3] = EIGHTTAP_REGULAR; |
| 10478 | #else |
| 10479 | tmp_best_filter = EIGHTTAP_REGULAR; |
| 10480 | #endif |
| 10481 | if (x->source_variance < sf->disable_filter_search_var_thresh) { |
| 10482 | #if CONFIG_DUAL_FILTER |
| 10483 | tmp_best_filter[0] = EIGHTTAP_REGULAR; |
| 10484 | #else |
| 10485 | tmp_best_filter = EIGHTTAP_REGULAR; |
| 10486 | #endif |
| 10487 | } else if (sf->adaptive_pred_interp_filter == 1 && |
| 10488 | ctx->pred_interp_filter < SWITCHABLE) { |
| 10489 | #if CONFIG_DUAL_FILTER |
| 10490 | tmp_best_filter[0] = ctx->pred_interp_filter; |
| 10491 | #else |
| 10492 | tmp_best_filter = ctx->pred_interp_filter; |
| 10493 | #endif |
| 10494 | } else if (sf->adaptive_pred_interp_filter == 2) { |
| 10495 | #if CONFIG_DUAL_FILTER |
| 10496 | tmp_best_filter[0] = ctx->pred_interp_filter < SWITCHABLE |
| 10497 | ? ctx->pred_interp_filter |
| 10498 | : 0; |
| 10499 | #else |
| 10500 | tmp_best_filter = ctx->pred_interp_filter < SWITCHABLE |
| 10501 | ? ctx->pred_interp_filter |
| 10502 | : 0; |
| 10503 | #endif |
| 10504 | } else { |
| 10505 | #if CONFIG_DUAL_FILTER |
| 10506 | for (switchable_filter_index = 0; |
| 10507 | #if CONFIG_EXT_INTERP |
| 10508 | switchable_filter_index < 25; |
| 10509 | #else |
| 10510 | switchable_filter_index < 9; |
| 10511 | #endif |
| 10512 | ++switchable_filter_index) { |
| 10513 | #else |
| 10514 | for (switchable_filter_index = 0; |
| 10515 | switchable_filter_index < SWITCHABLE_FILTERS; |
| 10516 | ++switchable_filter_index) { |
| 10517 | #endif |
| 10518 | int newbest, rs; |
| 10519 | int64_t rs_rd; |
| 10520 | MB_MODE_INFO_EXT *mbmi_ext = x->mbmi_ext; |
| 10521 | #if CONFIG_DUAL_FILTER |
| 10522 | mbmi->interp_filter[0] = filter_sets[switchable_filter_index][0]; |
| 10523 | mbmi->interp_filter[1] = filter_sets[switchable_filter_index][1]; |
| 10524 | mbmi->interp_filter[2] = filter_sets[switchable_filter_index][0]; |
| 10525 | mbmi->interp_filter[3] = filter_sets[switchable_filter_index][1]; |
| 10526 | #else |
| 10527 | mbmi->interp_filter = switchable_filter_index; |
| 10528 | #endif |
| 10529 | tmp_rd = rd_pick_best_sub8x8_mode( |
| 10530 | cpi, x, &mbmi_ext->ref_mvs[ref_frame][0], second_ref, best_yrd, |
| 10531 | &rate, &rate_y, &distortion, &skippable, &total_sse, |
| 10532 | (int)this_rd_thresh, seg_mvs, |
| 10533 | #if CONFIG_EXT_INTER |
| 10534 | compound_seg_newmvs, |
| 10535 | #endif // CONFIG_EXT_INTER |
| 10536 | bsi, switchable_filter_index, mi_row, mi_col); |
| 10537 | #if CONFIG_EXT_INTERP |
| 10538 | #if CONFIG_DUAL_FILTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10539 | if (!av1_is_interp_needed(xd) && cm->interp_filter == SWITCHABLE && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10540 | (mbmi->interp_filter[0] != EIGHTTAP_REGULAR || |
| 10541 | mbmi->interp_filter[1] != EIGHTTAP_REGULAR)) // invalid config |
| 10542 | continue; |
| 10543 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10544 | if (!av1_is_interp_needed(xd) && cm->interp_filter == SWITCHABLE && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10545 | mbmi->interp_filter != EIGHTTAP_REGULAR) // invalid config |
| 10546 | continue; |
| 10547 | #endif |
| 10548 | #endif // CONFIG_EXT_INTERP |
| 10549 | if (tmp_rd == INT64_MAX) continue; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10550 | rs = av1_get_switchable_rate(cpi, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10551 | rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0); |
| 10552 | if (cm->interp_filter == SWITCHABLE) tmp_rd += rs_rd; |
| 10553 | |
| 10554 | newbest = (tmp_rd < tmp_best_rd); |
| 10555 | if (newbest) { |
| 10556 | #if CONFIG_DUAL_FILTER |
| 10557 | tmp_best_filter[0] = mbmi->interp_filter[0]; |
| 10558 | tmp_best_filter[1] = mbmi->interp_filter[1]; |
| 10559 | tmp_best_filter[2] = mbmi->interp_filter[2]; |
| 10560 | tmp_best_filter[3] = mbmi->interp_filter[3]; |
| 10561 | #else |
| 10562 | tmp_best_filter = mbmi->interp_filter; |
| 10563 | #endif |
| 10564 | tmp_best_rd = tmp_rd; |
| 10565 | } |
| 10566 | if ((newbest && cm->interp_filter == SWITCHABLE) || |
| 10567 | ( |
| 10568 | #if CONFIG_DUAL_FILTER |
| 10569 | mbmi->interp_filter[0] == cm->interp_filter |
| 10570 | #else |
| 10571 | mbmi->interp_filter == cm->interp_filter |
| 10572 | #endif |
| 10573 | && cm->interp_filter != SWITCHABLE)) { |
| 10574 | tmp_best_rdu = tmp_rd; |
| 10575 | tmp_best_rate = rate; |
| 10576 | tmp_best_ratey = rate_y; |
| 10577 | tmp_best_distortion = distortion; |
| 10578 | tmp_best_sse = total_sse; |
| 10579 | tmp_best_skippable = skippable; |
| 10580 | tmp_best_mbmode = *mbmi; |
| 10581 | for (i = 0; i < 4; i++) { |
| 10582 | tmp_best_bmodes[i] = xd->mi[0]->bmi[i]; |
| 10583 | } |
| 10584 | pred_exists = 1; |
| 10585 | } |
| 10586 | } // switchable_filter_index loop |
| 10587 | } |
| 10588 | } |
| 10589 | |
| 10590 | if (tmp_best_rdu == INT64_MAX && pred_exists) continue; |
| 10591 | |
| 10592 | #if CONFIG_DUAL_FILTER |
| 10593 | mbmi->interp_filter[0] = |
| 10594 | (cm->interp_filter == SWITCHABLE ? tmp_best_filter[0] |
| 10595 | : cm->interp_filter); |
| 10596 | mbmi->interp_filter[1] = |
| 10597 | (cm->interp_filter == SWITCHABLE ? tmp_best_filter[1] |
| 10598 | : cm->interp_filter); |
| 10599 | mbmi->interp_filter[2] = |
| 10600 | (cm->interp_filter == SWITCHABLE ? tmp_best_filter[2] |
| 10601 | : cm->interp_filter); |
| 10602 | mbmi->interp_filter[3] = |
| 10603 | (cm->interp_filter == SWITCHABLE ? tmp_best_filter[3] |
| 10604 | : cm->interp_filter); |
| 10605 | #else |
| 10606 | mbmi->interp_filter = |
| 10607 | (cm->interp_filter == SWITCHABLE ? tmp_best_filter |
| 10608 | : cm->interp_filter); |
| 10609 | #endif |
| 10610 | |
| 10611 | if (!pred_exists) { |
| 10612 | // Handles the special case when a filter that is not in the |
| 10613 | // switchable list (bilinear) is indicated at the frame level |
| 10614 | tmp_rd = rd_pick_best_sub8x8_mode( |
| 10615 | cpi, x, &x->mbmi_ext->ref_mvs[ref_frame][0], second_ref, best_yrd, |
| 10616 | &rate, &rate_y, &distortion, &skippable, &total_sse, |
| 10617 | (int)this_rd_thresh, seg_mvs, |
| 10618 | #if CONFIG_EXT_INTER |
| 10619 | compound_seg_newmvs, |
| 10620 | #endif // CONFIG_EXT_INTER |
| 10621 | bsi, 0, mi_row, mi_col); |
| 10622 | #if CONFIG_EXT_INTERP |
| 10623 | #if CONFIG_DUAL_FILTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10624 | if (!av1_is_interp_needed(xd) && cm->interp_filter == SWITCHABLE && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10625 | (mbmi->interp_filter[0] != EIGHTTAP_REGULAR || |
| 10626 | mbmi->interp_filter[1] != EIGHTTAP_REGULAR)) { |
| 10627 | mbmi->interp_filter[0] = EIGHTTAP_REGULAR; |
| 10628 | mbmi->interp_filter[1] = EIGHTTAP_REGULAR; |
| 10629 | } |
| 10630 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10631 | if (!av1_is_interp_needed(xd) && cm->interp_filter == SWITCHABLE && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10632 | mbmi->interp_filter != EIGHTTAP_REGULAR) |
| 10633 | mbmi->interp_filter = EIGHTTAP_REGULAR; |
| 10634 | #endif // CONFIG_DUAL_FILTER |
| 10635 | #endif // CONFIG_EXT_INTERP |
| 10636 | if (tmp_rd == INT64_MAX) continue; |
| 10637 | } else { |
| 10638 | total_sse = tmp_best_sse; |
| 10639 | rate = tmp_best_rate; |
| 10640 | rate_y = tmp_best_ratey; |
| 10641 | distortion = tmp_best_distortion; |
| 10642 | skippable = tmp_best_skippable; |
| 10643 | *mbmi = tmp_best_mbmode; |
| 10644 | for (i = 0; i < 4; i++) xd->mi[0]->bmi[i] = tmp_best_bmodes[i]; |
| 10645 | } |
| 10646 | // Add in the cost of the transform type |
| 10647 | if (!xd->lossless[mbmi->segment_id]) { |
| 10648 | int rate_tx_type = 0; |
| 10649 | #if CONFIG_EXT_TX |
| 10650 | if (get_ext_tx_types(mbmi->tx_size, bsize, 1) > 1) { |
| 10651 | const int eset = get_ext_tx_set(mbmi->tx_size, bsize, 1); |
| 10652 | rate_tx_type = |
| 10653 | cpi->inter_tx_type_costs[eset][mbmi->tx_size][mbmi->tx_type]; |
| 10654 | } |
| 10655 | #else |
| 10656 | if (mbmi->tx_size < TX_32X32) { |
| 10657 | rate_tx_type = cpi->inter_tx_type_costs[mbmi->tx_size][mbmi->tx_type]; |
| 10658 | } |
| 10659 | #endif |
| 10660 | rate += rate_tx_type; |
| 10661 | rate_y += rate_tx_type; |
| 10662 | } |
| 10663 | |
| 10664 | rate2 += rate; |
| 10665 | distortion2 += distortion; |
| 10666 | |
| 10667 | if (cm->interp_filter == SWITCHABLE) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10668 | rate2 += av1_get_switchable_rate(cpi, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10669 | |
| 10670 | if (!mode_excluded) |
| 10671 | mode_excluded = comp_pred ? cm->reference_mode == SINGLE_REFERENCE |
| 10672 | : cm->reference_mode == COMPOUND_REFERENCE; |
| 10673 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10674 | compmode_cost = av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10675 | |
| 10676 | tmp_best_rdu = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10677 | best_rd - AOMMIN(RDCOST(x->rdmult, x->rddiv, rate2, distortion2), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10678 | RDCOST(x->rdmult, x->rddiv, 0, total_sse)); |
| 10679 | |
| 10680 | if (tmp_best_rdu > 0) { |
| 10681 | // If even the 'Y' rd value of split is higher than best so far |
| 10682 | // then dont bother looking at UV |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 10683 | int is_cost_valid_uv; |
| 10684 | #if CONFIG_VAR_TX |
| 10685 | RD_STATS rd_stats_uv; |
| 10686 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10687 | av1_build_inter_predictors_sbuv(&x->e_mbd, mi_row, mi_col, BLOCK_8X8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10688 | #if CONFIG_VAR_TX |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 10689 | is_cost_valid_uv = |
| 10690 | inter_block_uvrd(cpi, x, &rd_stats_uv, BLOCK_8X8, tmp_best_rdu); |
| 10691 | rate_uv = rd_stats_uv.rate; |
| 10692 | distortion_uv = rd_stats_uv.dist; |
| 10693 | uv_skippable = rd_stats_uv.skip; |
| 10694 | uv_sse = rd_stats_uv.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10695 | #else |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 10696 | is_cost_valid_uv = |
| 10697 | super_block_uvrd(cpi, x, &rate_uv, &distortion_uv, &uv_skippable, |
| 10698 | &uv_sse, BLOCK_8X8, tmp_best_rdu); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10699 | #endif |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 10700 | if (!is_cost_valid_uv) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10701 | rate2 += rate_uv; |
| 10702 | distortion2 += distortion_uv; |
| 10703 | skippable = skippable && uv_skippable; |
| 10704 | total_sse += uv_sse; |
| 10705 | } else { |
| 10706 | continue; |
| 10707 | } |
| 10708 | } |
| 10709 | |
| 10710 | if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost; |
| 10711 | |
| 10712 | // Estimate the reference frame signaling cost and add it |
| 10713 | // to the rolling cost variable. |
| 10714 | if (second_ref_frame > INTRA_FRAME) { |
| 10715 | rate2 += ref_costs_comp[ref_frame]; |
| 10716 | #if CONFIG_EXT_REFS |
| 10717 | rate2 += ref_costs_comp[second_ref_frame]; |
| 10718 | #endif // CONFIG_EXT_REFS |
| 10719 | } else { |
| 10720 | rate2 += ref_costs_single[ref_frame]; |
| 10721 | } |
| 10722 | |
| 10723 | if (!disable_skip) { |
| 10724 | // Skip is never coded at the segment level for sub8x8 blocks and instead |
| 10725 | // always coded in the bitstream at the mode info level. |
| 10726 | |
| 10727 | if (ref_frame != INTRA_FRAME && !xd->lossless[mbmi->segment_id]) { |
| 10728 | if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) < |
| 10729 | RDCOST(x->rdmult, x->rddiv, 0, total_sse)) { |
| 10730 | // Add in the cost of the no skip flag. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10731 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10732 | } else { |
| 10733 | // FIXME(rbultje) make this work for splitmv also |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10734 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10735 | distortion2 = total_sse; |
| 10736 | assert(total_sse >= 0); |
| 10737 | rate2 -= (rate_y + rate_uv); |
| 10738 | rate_y = 0; |
| 10739 | rate_uv = 0; |
| 10740 | this_skip2 = 1; |
| 10741 | } |
| 10742 | } else { |
| 10743 | // Add in the cost of the no skip flag. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10744 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10745 | } |
| 10746 | |
| 10747 | // Calculate the final RD estimate for this mode. |
| 10748 | this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); |
| 10749 | } |
| 10750 | |
| 10751 | if (!disable_skip && ref_frame == INTRA_FRAME) { |
| 10752 | for (i = 0; i < REFERENCE_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10753 | best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10754 | } |
| 10755 | |
| 10756 | // Did this mode help.. i.e. is it the new best mode |
| 10757 | if (this_rd < best_rd || x->skip) { |
| 10758 | if (!mode_excluded) { |
| 10759 | // Note index of best mode so far |
| 10760 | best_ref_index = ref_index; |
| 10761 | |
| 10762 | if (ref_frame == INTRA_FRAME) { |
| 10763 | /* required for left and above block mv */ |
| 10764 | mbmi->mv[0].as_int = 0; |
| 10765 | } |
| 10766 | |
| 10767 | rd_cost->rate = rate2; |
| 10768 | #if CONFIG_SUPERTX |
| 10769 | *returnrate_nocoef = rate2 - rate_y - rate_uv; |
| 10770 | if (!disable_skip) |
| 10771 | *returnrate_nocoef -= |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10772 | av1_cost_bit(av1_get_skip_prob(cm, xd), this_skip2); |
| 10773 | *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd), |
| 10774 | mbmi->ref_frame[0] != INTRA_FRAME); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10775 | assert(*returnrate_nocoef > 0); |
| 10776 | #endif // CONFIG_SUPERTX |
| 10777 | rd_cost->dist = distortion2; |
| 10778 | rd_cost->rdcost = this_rd; |
| 10779 | best_rd = this_rd; |
| 10780 | best_yrd = |
| 10781 | best_rd - RDCOST(x->rdmult, x->rddiv, rate_uv, distortion_uv); |
| 10782 | best_mbmode = *mbmi; |
| 10783 | best_skip2 = this_skip2; |
| 10784 | |
| 10785 | #if CONFIG_VAR_TX |
| 10786 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10787 | memset(ctx->blk_skip[i], 0, sizeof(uint8_t) * ctx->num_4x4_blk); |
| 10788 | #endif |
| 10789 | |
| 10790 | for (i = 0; i < 4; i++) best_bmodes[i] = xd->mi[0]->bmi[i]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10791 | } |
| 10792 | } |
| 10793 | |
| 10794 | /* keep record of best compound/single-only prediction */ |
| 10795 | if (!disable_skip && ref_frame != INTRA_FRAME) { |
| 10796 | int64_t single_rd, hybrid_rd, single_rate, hybrid_rate; |
| 10797 | |
| 10798 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
| 10799 | single_rate = rate2 - compmode_cost; |
| 10800 | hybrid_rate = rate2; |
| 10801 | } else { |
| 10802 | single_rate = rate2; |
| 10803 | hybrid_rate = rate2 + compmode_cost; |
| 10804 | } |
| 10805 | |
| 10806 | single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2); |
| 10807 | hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2); |
| 10808 | |
| 10809 | if (!comp_pred && single_rd < best_pred_rd[SINGLE_REFERENCE]) |
| 10810 | best_pred_rd[SINGLE_REFERENCE] = single_rd; |
| 10811 | else if (comp_pred && single_rd < best_pred_rd[COMPOUND_REFERENCE]) |
| 10812 | best_pred_rd[COMPOUND_REFERENCE] = single_rd; |
| 10813 | |
| 10814 | if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT]) |
| 10815 | best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd; |
| 10816 | } |
| 10817 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10818 | if (x->skip && !comp_pred) break; |
| 10819 | } |
| 10820 | |
| 10821 | if (best_rd >= best_rd_so_far) { |
| 10822 | rd_cost->rate = INT_MAX; |
| 10823 | rd_cost->rdcost = INT64_MAX; |
| 10824 | #if CONFIG_SUPERTX |
| 10825 | *returnrate_nocoef = INT_MAX; |
| 10826 | #endif // CONFIG_SUPERTX |
| 10827 | return; |
| 10828 | } |
| 10829 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10830 | if (best_rd == INT64_MAX) { |
| 10831 | rd_cost->rate = INT_MAX; |
| 10832 | rd_cost->dist = INT64_MAX; |
| 10833 | rd_cost->rdcost = INT64_MAX; |
| 10834 | #if CONFIG_SUPERTX |
| 10835 | *returnrate_nocoef = INT_MAX; |
| 10836 | #endif // CONFIG_SUPERTX |
| 10837 | return; |
| 10838 | } |
| 10839 | |
| 10840 | #if CONFIG_DUAL_FILTER |
| 10841 | assert((cm->interp_filter == SWITCHABLE) || |
| 10842 | (cm->interp_filter == best_mbmode.interp_filter[0]) || |
| 10843 | !is_inter_block(&best_mbmode)); |
| 10844 | #else |
| 10845 | assert((cm->interp_filter == SWITCHABLE) || |
| 10846 | (cm->interp_filter == best_mbmode.interp_filter) || |
| 10847 | !is_inter_block(&best_mbmode)); |
| 10848 | #endif |
| 10849 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10850 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
| 10851 | sf->adaptive_rd_thresh, bsize, best_ref_index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10852 | |
| 10853 | // macroblock modes |
| 10854 | *mbmi = best_mbmode; |
| 10855 | #if CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX |
| 10856 | mbmi->inter_tx_size[0][0] = mbmi->tx_size; |
| 10857 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 10858 | |
| 10859 | x->skip |= best_skip2; |
| 10860 | if (!is_inter_block(&best_mbmode)) { |
| 10861 | for (i = 0; i < 4; i++) xd->mi[0]->bmi[i].as_mode = best_bmodes[i].as_mode; |
| 10862 | } else { |
| 10863 | for (i = 0; i < 4; ++i) |
| 10864 | memcpy(&xd->mi[0]->bmi[i], &best_bmodes[i], sizeof(b_mode_info)); |
| 10865 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10866 | #if CONFIG_REF_MV |
Yaowu Xu | f5bbbfa | 2016-09-26 09:13:38 -0700 | [diff] [blame] | 10867 | mbmi->pred_mv[0].as_int = xd->mi[0]->bmi[3].pred_mv[0].as_int; |
| 10868 | mbmi->pred_mv[1].as_int = xd->mi[0]->bmi[3].pred_mv[1].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10869 | #endif |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 10870 | mbmi->mv[0].as_int = xd->mi[0]->bmi[3].as_mv[0].as_int; |
| 10871 | mbmi->mv[1].as_int = xd->mi[0]->bmi[3].as_mv[1].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10872 | } |
| 10873 | |
| 10874 | for (i = 0; i < REFERENCE_MODES; ++i) { |
| 10875 | if (best_pred_rd[i] == INT64_MAX) |
| 10876 | best_pred_diff[i] = INT_MIN; |
| 10877 | else |
| 10878 | best_pred_diff[i] = best_rd - best_pred_rd[i]; |
| 10879 | } |
| 10880 | |
| 10881 | store_coding_context(x, ctx, best_ref_index, best_pred_diff, 0); |
| 10882 | } |
| 10883 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10884 | #if CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10885 | // This function has a structure similar to av1_build_obmc_inter_prediction |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10886 | // |
| 10887 | // The OBMC predictor is computed as: |
| 10888 | // |
| 10889 | // PObmc(x,y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10890 | // AOM_BLEND_A64(Mh(x), |
| 10891 | // AOM_BLEND_A64(Mv(y), P(x,y), PAbove(x,y)), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10892 | // PLeft(x, y)) |
| 10893 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10894 | // Scaling up by AOM_BLEND_A64_MAX_ALPHA ** 2 and omitting the intermediate |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10895 | // rounding, this can be written as: |
| 10896 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10897 | // AOM_BLEND_A64_MAX_ALPHA * AOM_BLEND_A64_MAX_ALPHA * Pobmc(x,y) = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10898 | // Mh(x) * Mv(y) * P(x,y) + |
| 10899 | // Mh(x) * Cv(y) * Pabove(x,y) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10900 | // AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10901 | // |
| 10902 | // Where : |
| 10903 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10904 | // Cv(y) = AOM_BLEND_A64_MAX_ALPHA - Mv(y) |
| 10905 | // Ch(y) = AOM_BLEND_A64_MAX_ALPHA - Mh(y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10906 | // |
| 10907 | // This function computes 'wsrc' and 'mask' as: |
| 10908 | // |
| 10909 | // wsrc(x, y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10910 | // AOM_BLEND_A64_MAX_ALPHA * AOM_BLEND_A64_MAX_ALPHA * src(x, y) - |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10911 | // Mh(x) * Cv(y) * Pabove(x,y) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10912 | // AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10913 | // |
| 10914 | // mask(x, y) = Mh(x) * Mv(y) |
| 10915 | // |
| 10916 | // These can then be used to efficiently approximate the error for any |
| 10917 | // predictor P in the context of the provided neighbouring predictors by |
| 10918 | // computing: |
| 10919 | // |
| 10920 | // error(x, y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10921 | // wsrc(x, y) - mask(x, y) * P(x, y) / (AOM_BLEND_A64_MAX_ALPHA ** 2) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10922 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10923 | static void calc_target_weighted_pred(const AV1_COMMON *cm, const MACROBLOCK *x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10924 | const MACROBLOCKD *xd, int mi_row, |
| 10925 | int mi_col, const uint8_t *above, |
| 10926 | int above_stride, const uint8_t *left, |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 10927 | int left_stride) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10928 | const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type; |
| 10929 | int row, col, i; |
| 10930 | const int bw = 8 * xd->n8_w; |
| 10931 | const int bh = 8 * xd->n8_h; |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 10932 | int32_t *mask_buf = x->mask_buf; |
| 10933 | int32_t *wsrc_buf = x->wsrc_buf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10934 | const int wsrc_stride = bw; |
| 10935 | const int mask_stride = bw; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10936 | const int src_scale = AOM_BLEND_A64_MAX_ALPHA * AOM_BLEND_A64_MAX_ALPHA; |
| 10937 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10938 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 10939 | #else |
| 10940 | const int is_hbd = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10941 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10942 | |
| 10943 | // plane 0 should not be subsampled |
| 10944 | assert(xd->plane[0].subsampling_x == 0); |
| 10945 | assert(xd->plane[0].subsampling_y == 0); |
| 10946 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10947 | av1_zero_array(wsrc_buf, bw * bh); |
| 10948 | for (i = 0; i < bw * bh; ++i) mask_buf[i] = AOM_BLEND_A64_MAX_ALPHA; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10949 | |
| 10950 | // handle above row |
| 10951 | if (xd->up_available) { |
| 10952 | const int overlap = num_4x4_blocks_high_lookup[bsize] * 2; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10953 | const int miw = AOMMIN(xd->n8_w, cm->mi_cols - mi_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10954 | const int mi_row_offset = -1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10955 | const uint8_t *const mask1d = av1_get_obmc_mask(overlap); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10956 | |
| 10957 | assert(miw > 0); |
| 10958 | |
| 10959 | i = 0; |
| 10960 | do { // for each mi in the above row |
| 10961 | const int mi_col_offset = i; |
| 10962 | const MB_MODE_INFO *const above_mbmi = |
| 10963 | &xd->mi[mi_col_offset + mi_row_offset * xd->mi_stride]->mbmi; |
| 10964 | const int mi_step = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10965 | AOMMIN(xd->n8_w, num_8x8_blocks_wide_lookup[above_mbmi->sb_type]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10966 | const int neighbor_bw = mi_step * MI_SIZE; |
| 10967 | |
| 10968 | if (is_neighbor_overlappable(above_mbmi)) { |
| 10969 | const int tmp_stride = above_stride; |
| 10970 | int32_t *wsrc = wsrc_buf + (i * MI_SIZE); |
| 10971 | int32_t *mask = mask_buf + (i * MI_SIZE); |
| 10972 | |
| 10973 | if (!is_hbd) { |
| 10974 | const uint8_t *tmp = above; |
| 10975 | |
| 10976 | for (row = 0; row < overlap; ++row) { |
| 10977 | const uint8_t m0 = mask1d[row]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10978 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10979 | for (col = 0; col < neighbor_bw; ++col) { |
| 10980 | wsrc[col] = m1 * tmp[col]; |
| 10981 | mask[col] = m0; |
| 10982 | } |
| 10983 | wsrc += wsrc_stride; |
| 10984 | mask += mask_stride; |
| 10985 | tmp += tmp_stride; |
| 10986 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10987 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10988 | } else { |
| 10989 | const uint16_t *tmp = CONVERT_TO_SHORTPTR(above); |
| 10990 | |
| 10991 | for (row = 0; row < overlap; ++row) { |
| 10992 | const uint8_t m0 = mask1d[row]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10993 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10994 | for (col = 0; col < neighbor_bw; ++col) { |
| 10995 | wsrc[col] = m1 * tmp[col]; |
| 10996 | mask[col] = m0; |
| 10997 | } |
| 10998 | wsrc += wsrc_stride; |
| 10999 | mask += mask_stride; |
| 11000 | tmp += tmp_stride; |
| 11001 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11002 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11003 | } |
| 11004 | } |
| 11005 | |
| 11006 | above += neighbor_bw; |
| 11007 | i += mi_step; |
| 11008 | } while (i < miw); |
| 11009 | } |
| 11010 | |
| 11011 | for (i = 0; i < bw * bh; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11012 | wsrc_buf[i] *= AOM_BLEND_A64_MAX_ALPHA; |
| 11013 | mask_buf[i] *= AOM_BLEND_A64_MAX_ALPHA; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11014 | } |
| 11015 | |
| 11016 | // handle left column |
| 11017 | if (xd->left_available) { |
| 11018 | const int overlap = num_4x4_blocks_wide_lookup[bsize] * 2; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11019 | const int mih = AOMMIN(xd->n8_h, cm->mi_rows - mi_row); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11020 | const int mi_col_offset = -1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11021 | const uint8_t *const mask1d = av1_get_obmc_mask(overlap); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11022 | |
| 11023 | assert(mih > 0); |
| 11024 | |
| 11025 | i = 0; |
| 11026 | do { // for each mi in the left column |
| 11027 | const int mi_row_offset = i; |
| 11028 | const MB_MODE_INFO *const left_mbmi = |
| 11029 | &xd->mi[mi_col_offset + mi_row_offset * xd->mi_stride]->mbmi; |
| 11030 | const int mi_step = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11031 | AOMMIN(xd->n8_h, num_8x8_blocks_high_lookup[left_mbmi->sb_type]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11032 | const int neighbor_bh = mi_step * MI_SIZE; |
| 11033 | |
| 11034 | if (is_neighbor_overlappable(left_mbmi)) { |
| 11035 | const int tmp_stride = left_stride; |
| 11036 | int32_t *wsrc = wsrc_buf + (i * MI_SIZE * wsrc_stride); |
| 11037 | int32_t *mask = mask_buf + (i * MI_SIZE * mask_stride); |
| 11038 | |
| 11039 | if (!is_hbd) { |
| 11040 | const uint8_t *tmp = left; |
| 11041 | |
| 11042 | for (row = 0; row < neighbor_bh; ++row) { |
| 11043 | for (col = 0; col < overlap; ++col) { |
| 11044 | const uint8_t m0 = mask1d[col]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11045 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11046 | wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 + |
| 11047 | (tmp[col] << AOM_BLEND_A64_ROUND_BITS) * m1; |
| 11048 | mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11049 | } |
| 11050 | wsrc += wsrc_stride; |
| 11051 | mask += mask_stride; |
| 11052 | tmp += tmp_stride; |
| 11053 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11054 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11055 | } else { |
| 11056 | const uint16_t *tmp = CONVERT_TO_SHORTPTR(left); |
| 11057 | |
| 11058 | for (row = 0; row < neighbor_bh; ++row) { |
| 11059 | for (col = 0; col < overlap; ++col) { |
| 11060 | const uint8_t m0 = mask1d[col]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11061 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11062 | wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 + |
| 11063 | (tmp[col] << AOM_BLEND_A64_ROUND_BITS) * m1; |
| 11064 | mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11065 | } |
| 11066 | wsrc += wsrc_stride; |
| 11067 | mask += mask_stride; |
| 11068 | tmp += tmp_stride; |
| 11069 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11070 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11071 | } |
| 11072 | } |
| 11073 | |
| 11074 | left += neighbor_bh * left_stride; |
| 11075 | i += mi_step; |
| 11076 | } while (i < mih); |
| 11077 | } |
| 11078 | |
| 11079 | if (!is_hbd) { |
| 11080 | const uint8_t *src = x->plane[0].src.buf; |
| 11081 | |
| 11082 | for (row = 0; row < bh; ++row) { |
| 11083 | for (col = 0; col < bw; ++col) { |
| 11084 | wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col]; |
| 11085 | } |
| 11086 | wsrc_buf += wsrc_stride; |
| 11087 | src += x->plane[0].src.stride; |
| 11088 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11089 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11090 | } else { |
| 11091 | const uint16_t *src = CONVERT_TO_SHORTPTR(x->plane[0].src.buf); |
| 11092 | |
| 11093 | for (row = 0; row < bh; ++row) { |
| 11094 | for (col = 0; col < bw; ++col) { |
| 11095 | wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col]; |
| 11096 | } |
| 11097 | wsrc_buf += wsrc_stride; |
| 11098 | src += x->plane[0].src.stride; |
| 11099 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11100 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11101 | } |
| 11102 | } |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11103 | #endif // CONFIG_MOTION_VAR |