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 | |
David Michael Barr | 5b2021e | 2017-08-17 18:12:39 +0900 | [diff] [blame] | 24 | #if CONFIG_CFL |
| 25 | #include "av1/common/cfl.h" |
| 26 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 27 | #include "av1/common/common.h" |
| 28 | #include "av1/common/common_data.h" |
| 29 | #include "av1/common/entropy.h" |
| 30 | #include "av1/common/entropymode.h" |
| 31 | #include "av1/common/idct.h" |
| 32 | #include "av1/common/mvref_common.h" |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 33 | #include "av1/common/obmc.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 34 | #include "av1/common/pred_common.h" |
| 35 | #include "av1/common/quant_common.h" |
| 36 | #include "av1/common/reconinter.h" |
| 37 | #include "av1/common/reconintra.h" |
| 38 | #include "av1/common/scan.h" |
| 39 | #include "av1/common/seg_common.h" |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 40 | #if CONFIG_LV_MAP |
| 41 | #include "av1/common/txb_common.h" |
| 42 | #endif |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 43 | #include "av1/common/warped_motion.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 44 | |
Jingning Han | 1aab818 | 2016-06-03 11:09:06 -0700 | [diff] [blame] | 45 | #include "av1/encoder/aq_variance.h" |
Tom Finegan | 17ce8b1 | 2017-02-08 12:46:31 -0800 | [diff] [blame] | 46 | #include "av1/encoder/av1_quantize.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 47 | #include "av1/encoder/cost.h" |
| 48 | #include "av1/encoder/encodemb.h" |
| 49 | #include "av1/encoder/encodemv.h" |
| 50 | #include "av1/encoder/encoder.h" |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 51 | #if CONFIG_LV_MAP |
| 52 | #include "av1/encoder/encodetxb.h" |
| 53 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 54 | #include "av1/encoder/hybrid_fwd_txfm.h" |
| 55 | #include "av1/encoder/mcomp.h" |
| 56 | #include "av1/encoder/palette.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 57 | #include "av1/encoder/ratectrl.h" |
| 58 | #include "av1/encoder/rd.h" |
| 59 | #include "av1/encoder/rdopt.h" |
Debargha Mukherjee | ceebb70 | 2016-10-11 05:26:50 -0700 | [diff] [blame] | 60 | #include "av1/encoder/tokenize.h" |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 61 | #include "av1/encoder/tx_prune_model_weights.h" |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 62 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 63 | #if CONFIG_DUAL_FILTER |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 64 | #define DUAL_FILTER_SET_SIZE (SWITCHABLE_FILTERS * SWITCHABLE_FILTERS) |
Angie Chiang | aadbb02 | 2017-06-01 16:08:03 -0700 | [diff] [blame] | 65 | #if USE_EXTRA_FILTER |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 66 | static const int filter_sets[DUAL_FILTER_SET_SIZE][2] = { |
Angie Chiang | d91ab37 | 2016-11-21 18:16:49 -0800 | [diff] [blame] | 67 | { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 1, 0 }, { 1, 1 }, |
| 68 | { 1, 2 }, { 1, 3 }, { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, |
| 69 | { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 70 | }; |
Angie Chiang | aadbb02 | 2017-06-01 16:08:03 -0700 | [diff] [blame] | 71 | #else // USE_EXTRA_FILTER |
| 72 | static const int filter_sets[DUAL_FILTER_SET_SIZE][2] = { |
| 73 | { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 }, |
| 74 | { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 }, |
| 75 | }; |
| 76 | #endif // USE_EXTRA_FILTER |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 77 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 78 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 79 | #define LAST_FRAME_MODE_MASK \ |
| 80 | ((1 << INTRA_FRAME) | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | \ |
| 81 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 82 | (1 << ALTREF_FRAME)) |
| 83 | #define LAST2_FRAME_MODE_MASK \ |
| 84 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST3_FRAME) | \ |
| 85 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 86 | (1 << ALTREF_FRAME)) |
| 87 | #define LAST3_FRAME_MODE_MASK \ |
| 88 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 89 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 90 | (1 << ALTREF_FRAME)) |
| 91 | #define GOLDEN_FRAME_MODE_MASK \ |
| 92 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 93 | (1 << LAST3_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 94 | (1 << ALTREF_FRAME)) |
| 95 | #define BWDREF_FRAME_MODE_MASK \ |
| 96 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 97 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 98 | (1 << ALTREF_FRAME)) |
| 99 | #define ALTREF2_FRAME_MODE_MASK \ |
| 100 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 101 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | \ |
| 102 | (1 << ALTREF_FRAME)) |
| 103 | #define ALTREF_FRAME_MODE_MASK \ |
| 104 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 105 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | \ |
| 106 | (1 << ALTREF2_FRAME)) |
| 107 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 108 | #if CONFIG_EXT_COMP_REFS |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 109 | #define SECOND_REF_FRAME_MASK \ |
| 110 | ((1 << ALTREF_FRAME) | (1 << ALTREF2_FRAME) | (1 << BWDREF_FRAME) | \ |
| 111 | (1 << GOLDEN_FRAME) | (1 << LAST2_FRAME) | 0x01) |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 112 | #else // !CONFIG_EXT_COMP_REFS |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 113 | #define SECOND_REF_FRAME_MASK \ |
| 114 | ((1 << ALTREF_FRAME) | (1 << ALTREF2_FRAME) | (1 << BWDREF_FRAME) | 0x01) |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 115 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 116 | |
| 117 | #define MIN_EARLY_TERM_INDEX 3 |
| 118 | #define NEW_MV_DISCOUNT_FACTOR 8 |
| 119 | |
| 120 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 121 | #define ANGLE_SKIP_THRESH 10 |
| 122 | #define FILTER_FAST_SEARCH 1 |
| 123 | #endif // CONFIG_EXT_INTRA |
| 124 | |
James Zern | 6793279 | 2017-08-21 11:13:19 -0700 | [diff] [blame] | 125 | static const double ADST_FLIP_SVM[8] = { |
| 126 | /* vertical */ |
| 127 | -6.6623, -2.8062, -3.2531, 3.1671, |
| 128 | /* horizontal */ |
| 129 | -7.7051, -3.2234, -3.6193, 3.4533 |
| 130 | }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 131 | |
| 132 | typedef struct { |
| 133 | PREDICTION_MODE mode; |
| 134 | MV_REFERENCE_FRAME ref_frame[2]; |
| 135 | } MODE_DEFINITION; |
| 136 | |
| 137 | typedef struct { MV_REFERENCE_FRAME ref_frame[2]; } REF_DEFINITION; |
| 138 | |
| 139 | struct rdcost_block_args { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 140 | const AV1_COMP *cpi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 141 | MACROBLOCK *x; |
| 142 | ENTROPY_CONTEXT t_above[2 * MAX_MIB_SIZE]; |
| 143 | ENTROPY_CONTEXT t_left[2 * MAX_MIB_SIZE]; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 144 | RD_STATS rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 145 | int64_t this_rd; |
| 146 | int64_t best_rd; |
| 147 | int exit_early; |
| 148 | int use_fast_coef_costing; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | #define LAST_NEW_MV_INDEX 6 |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 152 | static const MODE_DEFINITION av1_mode_order[MAX_MODES] = { |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 153 | { NEARESTMV, { LAST_FRAME, NONE_FRAME } }, |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 154 | { NEARESTMV, { LAST2_FRAME, NONE_FRAME } }, |
| 155 | { NEARESTMV, { LAST3_FRAME, NONE_FRAME } }, |
| 156 | { NEARESTMV, { BWDREF_FRAME, NONE_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 157 | { NEARESTMV, { ALTREF2_FRAME, NONE_FRAME } }, |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 158 | { NEARESTMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 159 | { NEARESTMV, { GOLDEN_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 160 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 161 | { DC_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 162 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 163 | { NEWMV, { LAST_FRAME, NONE_FRAME } }, |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 164 | { NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 165 | { NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 166 | { NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 167 | { NEWMV, { ALTREF2_FRAME, NONE_FRAME } }, |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 168 | { NEWMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 169 | { NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 170 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 171 | { NEARMV, { LAST_FRAME, NONE_FRAME } }, |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 172 | { NEARMV, { LAST2_FRAME, NONE_FRAME } }, |
| 173 | { NEARMV, { LAST3_FRAME, NONE_FRAME } }, |
| 174 | { NEARMV, { BWDREF_FRAME, NONE_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 175 | { NEARMV, { ALTREF2_FRAME, NONE_FRAME } }, |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 176 | { NEARMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 177 | { NEARMV, { GOLDEN_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 178 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 179 | { GLOBALMV, { LAST_FRAME, NONE_FRAME } }, |
| 180 | { GLOBALMV, { LAST2_FRAME, NONE_FRAME } }, |
| 181 | { GLOBALMV, { LAST3_FRAME, NONE_FRAME } }, |
| 182 | { GLOBALMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 183 | { GLOBALMV, { ALTREF2_FRAME, NONE_FRAME } }, |
| 184 | { GLOBALMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 185 | { GLOBALMV, { ALTREF_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 186 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 187 | // TODO(zoeliu): May need to reconsider the order on the modes to check |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 188 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 189 | { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 190 | { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 191 | { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 192 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 193 | { NEAREST_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 194 | { NEAREST_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 195 | { NEAREST_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 196 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 197 | { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 198 | { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 199 | { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 200 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 201 | |
| 202 | #if CONFIG_EXT_COMP_REFS |
| 203 | { NEAREST_NEARESTMV, { LAST_FRAME, LAST2_FRAME } }, |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 204 | { NEAREST_NEARESTMV, { LAST_FRAME, LAST3_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 205 | { NEAREST_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 206 | { NEAREST_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 207 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 208 | |
Urvang Joshi | 96d1c0a | 2017-10-10 13:15:32 -0700 | [diff] [blame] | 209 | { PAETH_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 210 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 211 | { SMOOTH_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Urvang Joshi | e6ca8e8 | 2017-03-15 14:57:41 -0700 | [diff] [blame] | 212 | { SMOOTH_V_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 213 | { SMOOTH_H_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Urvang Joshi | 6be4a54 | 2016-11-03 15:24:05 -0700 | [diff] [blame] | 214 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 215 | { NEAR_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 216 | { NEW_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 217 | { NEAREST_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 218 | { NEW_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 219 | { NEAR_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 220 | { NEW_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 221 | { GLOBAL_GLOBALMV, { LAST_FRAME, ALTREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 222 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 223 | { NEAR_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 224 | { NEW_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 225 | { NEAREST_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 226 | { NEW_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 227 | { NEAR_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 228 | { NEW_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 229 | { GLOBAL_GLOBALMV, { LAST2_FRAME, ALTREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 230 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 231 | { NEAR_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 232 | { NEW_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 233 | { NEAREST_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 234 | { NEW_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 235 | { NEAR_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 236 | { NEW_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 237 | { GLOBAL_GLOBALMV, { LAST3_FRAME, ALTREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 238 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 239 | { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 240 | { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 241 | { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 242 | { NEW_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 243 | { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 244 | { NEW_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 245 | { GLOBAL_GLOBALMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 246 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 247 | { NEAR_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 248 | { NEW_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 249 | { NEAREST_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 250 | { NEW_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 251 | { NEAR_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 252 | { NEW_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 253 | { GLOBAL_GLOBALMV, { LAST_FRAME, BWDREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 254 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 255 | { NEAR_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 256 | { NEW_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 257 | { NEAREST_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 258 | { NEW_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 259 | { NEAR_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 260 | { NEW_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 261 | { GLOBAL_GLOBALMV, { LAST2_FRAME, BWDREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 262 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 263 | { NEAR_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 264 | { NEW_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 265 | { NEAREST_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 266 | { NEW_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 267 | { NEAR_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 268 | { NEW_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 269 | { GLOBAL_GLOBALMV, { LAST3_FRAME, BWDREF_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 270 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 271 | { NEAR_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 272 | { NEW_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 273 | { NEAREST_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 274 | { NEW_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 275 | { NEAR_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 276 | { NEW_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 277 | { GLOBAL_GLOBALMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 278 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 279 | { NEAR_NEARMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 280 | { NEW_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 281 | { NEAREST_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 282 | { NEW_NEARMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 283 | { NEAR_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 284 | { NEW_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 285 | { GLOBAL_GLOBALMV, { LAST_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 286 | |
| 287 | { NEAR_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 288 | { NEW_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 289 | { NEAREST_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 290 | { NEW_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 291 | { NEAR_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 292 | { NEW_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 293 | { GLOBAL_GLOBALMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 294 | |
| 295 | { NEAR_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 296 | { NEW_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 297 | { NEAREST_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 298 | { NEW_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 299 | { NEAR_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 300 | { NEW_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 301 | { GLOBAL_GLOBALMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 302 | |
| 303 | { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 304 | { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 305 | { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 306 | { NEW_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 307 | { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 308 | { NEW_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 309 | { GLOBAL_GLOBALMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 310 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 311 | { H_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 312 | { V_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 313 | { D135_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 314 | { D207_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 315 | { D153_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 316 | { D63_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 317 | { D117_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 318 | { D45_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 319 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 320 | { GLOBALMV, { LAST_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 321 | { NEARESTMV, { LAST_FRAME, INTRA_FRAME } }, |
| 322 | { NEARMV, { LAST_FRAME, INTRA_FRAME } }, |
| 323 | { NEWMV, { LAST_FRAME, INTRA_FRAME } }, |
| 324 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 325 | { GLOBALMV, { LAST2_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 326 | { NEARESTMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 327 | { NEARMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 328 | { NEWMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 329 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 330 | { GLOBALMV, { LAST3_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 331 | { NEARESTMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 332 | { NEARMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 333 | { NEWMV, { LAST3_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 334 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 335 | { GLOBALMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 336 | { NEARESTMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 337 | { NEARMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 338 | { NEWMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 339 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 340 | { GLOBALMV, { BWDREF_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 341 | { NEARESTMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 342 | { NEARMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 343 | { NEWMV, { BWDREF_FRAME, INTRA_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 344 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 345 | { GLOBALMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 346 | { NEARESTMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
| 347 | { NEARMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
| 348 | { NEWMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 349 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 350 | { GLOBALMV, { ALTREF_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 351 | { NEARESTMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 352 | { NEARMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 353 | { NEWMV, { ALTREF_FRAME, INTRA_FRAME } }, |
Debargha Mukherjee | c1077e9 | 2017-11-06 20:17:33 -0800 | [diff] [blame] | 354 | |
| 355 | #if CONFIG_EXT_COMP_REFS |
| 356 | { NEAR_NEARMV, { LAST_FRAME, LAST2_FRAME } }, |
| 357 | { NEW_NEARESTMV, { LAST_FRAME, LAST2_FRAME } }, |
| 358 | { NEAREST_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 359 | { NEW_NEARMV, { LAST_FRAME, LAST2_FRAME } }, |
| 360 | { NEAR_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 361 | { NEW_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 362 | { GLOBAL_GLOBALMV, { LAST_FRAME, LAST2_FRAME } }, |
| 363 | |
| 364 | { NEAR_NEARMV, { LAST_FRAME, LAST3_FRAME } }, |
| 365 | { NEW_NEARESTMV, { LAST_FRAME, LAST3_FRAME } }, |
| 366 | { NEAREST_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 367 | { NEW_NEARMV, { LAST_FRAME, LAST3_FRAME } }, |
| 368 | { NEAR_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 369 | { NEW_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 370 | { GLOBAL_GLOBALMV, { LAST_FRAME, LAST3_FRAME } }, |
| 371 | |
| 372 | { NEAR_NEARMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 373 | { NEW_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 374 | { NEAREST_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 375 | { NEW_NEARMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 376 | { NEAR_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 377 | { NEW_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 378 | { GLOBAL_GLOBALMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 379 | |
| 380 | { NEAR_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 381 | { NEW_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 382 | { NEAREST_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 383 | { NEW_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 384 | { NEAR_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 385 | { NEW_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 386 | { GLOBAL_GLOBALMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 387 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 388 | }; |
| 389 | |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 390 | static const PREDICTION_MODE intra_rd_search_mode_order[INTRA_MODES] = { |
Urvang Joshi | 96d1c0a | 2017-10-10 13:15:32 -0700 | [diff] [blame] | 391 | DC_PRED, H_PRED, V_PRED, SMOOTH_PRED, PAETH_PRED, |
Urvang Joshi | b7301cd | 2017-11-09 15:08:56 -0800 | [diff] [blame] | 392 | SMOOTH_V_PRED, SMOOTH_H_PRED, D135_PRED, D207_PRED, D153_PRED, |
| 393 | D63_PRED, D117_PRED, D45_PRED, |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 396 | #if CONFIG_CFL |
| 397 | static const UV_PREDICTION_MODE uv_rd_search_mode_order[UV_INTRA_MODES] = { |
Urvang Joshi | b7301cd | 2017-11-09 15:08:56 -0800 | [diff] [blame] | 398 | UV_DC_PRED, UV_CFL_PRED, UV_H_PRED, UV_V_PRED, |
| 399 | UV_SMOOTH_PRED, UV_PAETH_PRED, UV_SMOOTH_V_PRED, UV_SMOOTH_H_PRED, |
| 400 | UV_D135_PRED, UV_D207_PRED, UV_D153_PRED, UV_D63_PRED, |
| 401 | UV_D117_PRED, UV_D45_PRED, |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 402 | }; |
| 403 | #else |
| 404 | #define uv_rd_search_mode_order intra_rd_search_mode_order |
| 405 | #endif // CONFIG_CFL |
| 406 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 407 | static INLINE int write_uniform_cost(int n, int v) { |
hui su | 3749929 | 2017-04-26 09:49:53 -0700 | [diff] [blame] | 408 | const int l = get_unsigned_bits(n); |
| 409 | const int m = (1 << l) - n; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 410 | if (l == 0) return 0; |
| 411 | if (v < m) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 412 | return (l - 1) * av1_cost_bit(128, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 413 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 414 | return l * av1_cost_bit(128, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | // constants for prune 1 and prune 2 decision boundaries |
| 418 | #define FAST_EXT_TX_CORR_MID 0.0 |
| 419 | #define FAST_EXT_TX_EDST_MID 0.1 |
| 420 | #define FAST_EXT_TX_CORR_MARGIN 0.5 |
| 421 | #define FAST_EXT_TX_EDST_MARGIN 0.3 |
| 422 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 423 | int inter_block_yrd(const AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_stats, |
| 424 | BLOCK_SIZE bsize, int64_t ref_best_rd, int fast); |
| 425 | int inter_block_uvrd(const AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_stats, |
| 426 | BLOCK_SIZE bsize, int64_t ref_best_rd, int fast); |
| 427 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 428 | static unsigned pixel_dist_visible_only( |
| 429 | const AV1_COMP *const cpi, const MACROBLOCK *x, const uint8_t *src, |
| 430 | const int src_stride, const uint8_t *dst, const int dst_stride, |
| 431 | const BLOCK_SIZE tx_bsize, int txb_rows, int txb_cols, int visible_rows, |
| 432 | int visible_cols) { |
| 433 | unsigned sse; |
| 434 | |
Debargha Mukherjee | 35a4db3 | 2017-11-14 11:58:16 -0800 | [diff] [blame] | 435 | if (txb_rows == visible_rows && txb_cols == visible_cols) { |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 436 | cpi->fn_ptr[tx_bsize].vf(src, src_stride, dst, dst_stride, &sse); |
| 437 | return sse; |
| 438 | } |
| 439 | #if CONFIG_HIGHBITDEPTH |
| 440 | const MACROBLOCKD *xd = &x->e_mbd; |
| 441 | |
| 442 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 443 | uint64_t sse64 = aom_highbd_sse_odd_size(src, src_stride, dst, dst_stride, |
| 444 | visible_cols, visible_rows); |
| 445 | return (unsigned int)ROUND_POWER_OF_TWO(sse64, (xd->bd - 8) * 2); |
| 446 | } |
| 447 | #else |
| 448 | (void)x; |
| 449 | #endif // CONFIG_HIGHBITDEPTH |
| 450 | sse = aom_sse_odd_size(src, src_stride, dst, dst_stride, visible_cols, |
| 451 | visible_rows); |
| 452 | return sse; |
| 453 | } |
| 454 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 455 | #if CONFIG_DIST_8X8 |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 456 | static uint64_t cdef_dist_8x8_16bit(uint16_t *dst, int dstride, uint16_t *src, |
| 457 | int sstride, int coeff_shift) { |
| 458 | uint64_t svar = 0; |
| 459 | uint64_t dvar = 0; |
| 460 | uint64_t sum_s = 0; |
| 461 | uint64_t sum_d = 0; |
| 462 | uint64_t sum_s2 = 0; |
| 463 | uint64_t sum_d2 = 0; |
| 464 | uint64_t sum_sd = 0; |
| 465 | uint64_t dist = 0; |
| 466 | |
| 467 | int i, j; |
| 468 | for (i = 0; i < 8; i++) { |
| 469 | for (j = 0; j < 8; j++) { |
| 470 | sum_s += src[i * sstride + j]; |
| 471 | sum_d += dst[i * dstride + j]; |
| 472 | sum_s2 += src[i * sstride + j] * src[i * sstride + j]; |
| 473 | sum_d2 += dst[i * dstride + j] * dst[i * dstride + j]; |
| 474 | sum_sd += src[i * sstride + j] * dst[i * dstride + j]; |
| 475 | } |
| 476 | } |
| 477 | /* Compute the variance -- the calculation cannot go negative. */ |
| 478 | svar = sum_s2 - ((sum_s * sum_s + 32) >> 6); |
| 479 | dvar = sum_d2 - ((sum_d * sum_d + 32) >> 6); |
| 480 | |
| 481 | // Tuning of jm's original dering distortion metric used in CDEF tool, |
| 482 | // suggested by jm |
| 483 | const uint64_t a = 4; |
| 484 | const uint64_t b = 2; |
| 485 | const uint64_t c1 = (400 * a << 2 * coeff_shift); |
| 486 | const uint64_t c2 = (b * 20000 * a * a << 4 * coeff_shift); |
| 487 | |
| 488 | dist = |
| 489 | (uint64_t)floor(.5 + |
| 490 | (sum_d2 + sum_s2 - 2 * sum_sd) * .5 * (svar + dvar + c1) / |
| 491 | (sqrt(svar * (double)dvar + c2))); |
| 492 | |
| 493 | // Calibrate dist to have similar rate for the same QP with MSE only |
| 494 | // distortion (as in master branch) |
| 495 | dist = (uint64_t)((float)dist * 0.75); |
| 496 | |
| 497 | return dist; |
| 498 | } |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 499 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 500 | static int od_compute_var_4x4(uint16_t *x, int stride) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 501 | int sum; |
| 502 | int s2; |
| 503 | int i; |
| 504 | sum = 0; |
| 505 | s2 = 0; |
| 506 | for (i = 0; i < 4; i++) { |
| 507 | int j; |
| 508 | for (j = 0; j < 4; j++) { |
| 509 | int t; |
| 510 | |
| 511 | t = x[i * stride + j]; |
| 512 | sum += t; |
| 513 | s2 += t * t; |
| 514 | } |
| 515 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 516 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 517 | return (s2 - (sum * sum >> 4)) >> 4; |
| 518 | } |
| 519 | |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 520 | /* OD_DIST_LP_MID controls the frequency weighting filter used for computing |
| 521 | the distortion. For a value X, the filter is [1 X 1]/(X + 2) and |
| 522 | is applied both horizontally and vertically. For X=5, the filter is |
| 523 | a good approximation for the OD_QM8_Q4_HVS quantization matrix. */ |
| 524 | #define OD_DIST_LP_MID (5) |
| 525 | #define OD_DIST_LP_NORM (OD_DIST_LP_MID + 2) |
| 526 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 527 | static double od_compute_dist_8x8(int use_activity_masking, uint16_t *x, |
| 528 | uint16_t *y, od_coeff *e_lp, int stride) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 529 | double sum; |
| 530 | int min_var; |
| 531 | double mean_var; |
| 532 | double var_stat; |
| 533 | double activity; |
| 534 | double calibration; |
| 535 | int i; |
| 536 | int j; |
| 537 | double vardist; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 538 | |
| 539 | vardist = 0; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 540 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 541 | #if 1 |
| 542 | min_var = INT_MAX; |
| 543 | mean_var = 0; |
| 544 | for (i = 0; i < 3; i++) { |
| 545 | for (j = 0; j < 3; j++) { |
| 546 | int varx; |
| 547 | int vary; |
| 548 | varx = od_compute_var_4x4(x + 2 * i * stride + 2 * j, stride); |
| 549 | vary = od_compute_var_4x4(y + 2 * i * stride + 2 * j, stride); |
| 550 | min_var = OD_MINI(min_var, varx); |
| 551 | mean_var += 1. / (1 + varx); |
| 552 | /* The cast to (double) is to avoid an overflow before the sqrt.*/ |
| 553 | vardist += varx - 2 * sqrt(varx * (double)vary) + vary; |
| 554 | } |
| 555 | } |
| 556 | /* We use a different variance statistic depending on whether activity |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 557 | masking is used, since the harmonic mean appeared slightly worse with |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 558 | masking off. The calibration constant just ensures that we preserve the |
| 559 | rate compared to activity=1. */ |
| 560 | if (use_activity_masking) { |
| 561 | calibration = 1.95; |
| 562 | var_stat = 9. / mean_var; |
| 563 | } else { |
| 564 | calibration = 1.62; |
| 565 | var_stat = min_var; |
| 566 | } |
| 567 | /* 1.62 is a calibration constant, 0.25 is a noise floor and 1/6 is the |
| 568 | activity masking constant. */ |
| 569 | activity = calibration * pow(.25 + var_stat, -1. / 6); |
| 570 | #else |
| 571 | activity = 1; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 572 | #endif // 1 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 573 | sum = 0; |
| 574 | for (i = 0; i < 8; i++) { |
| 575 | for (j = 0; j < 8; j++) |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 576 | sum += e_lp[i * stride + j] * (double)e_lp[i * stride + j]; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 577 | } |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 578 | /* Normalize the filter to unit DC response. */ |
| 579 | sum *= 1. / (OD_DIST_LP_NORM * OD_DIST_LP_NORM * OD_DIST_LP_NORM * |
| 580 | OD_DIST_LP_NORM); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 581 | return activity * activity * (sum + vardist); |
| 582 | } |
| 583 | |
| 584 | // Note : Inputs x and y are in a pixel domain |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 585 | static double od_compute_dist_common(int activity_masking, uint16_t *x, |
| 586 | uint16_t *y, int bsize_w, int bsize_h, |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 587 | int qindex, od_coeff *tmp, |
| 588 | od_coeff *e_lp) { |
| 589 | int i, j; |
| 590 | double sum = 0; |
| 591 | const int mid = OD_DIST_LP_MID; |
| 592 | |
| 593 | for (j = 0; j < bsize_w; j++) { |
| 594 | e_lp[j] = mid * tmp[j] + 2 * tmp[bsize_w + j]; |
| 595 | e_lp[(bsize_h - 1) * bsize_w + j] = mid * tmp[(bsize_h - 1) * bsize_w + j] + |
| 596 | 2 * tmp[(bsize_h - 2) * bsize_w + j]; |
| 597 | } |
| 598 | for (i = 1; i < bsize_h - 1; i++) { |
| 599 | for (j = 0; j < bsize_w; j++) { |
| 600 | e_lp[i * bsize_w + j] = mid * tmp[i * bsize_w + j] + |
| 601 | tmp[(i - 1) * bsize_w + j] + |
| 602 | tmp[(i + 1) * bsize_w + j]; |
| 603 | } |
| 604 | } |
| 605 | for (i = 0; i < bsize_h; i += 8) { |
| 606 | for (j = 0; j < bsize_w; j += 8) { |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 607 | sum += od_compute_dist_8x8(activity_masking, &x[i * bsize_w + j], |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 608 | &y[i * bsize_w + j], &e_lp[i * bsize_w + j], |
| 609 | bsize_w); |
| 610 | } |
| 611 | } |
| 612 | /* Scale according to linear regression against SSE, for 8x8 blocks. */ |
| 613 | if (activity_masking) { |
| 614 | sum *= 2.2 + (1.7 - 2.2) * (qindex - 99) / (210 - 99) + |
| 615 | (qindex < 99 ? 2.5 * (qindex - 99) / 99 * (qindex - 99) / 99 : 0); |
| 616 | } else { |
| 617 | sum *= qindex >= 128 |
| 618 | ? 1.4 + (0.9 - 1.4) * (qindex - 128) / (209 - 128) |
| 619 | : qindex <= 43 ? 1.5 + (2.0 - 1.5) * (qindex - 43) / (16 - 43) |
| 620 | : 1.5 + (1.4 - 1.5) * (qindex - 43) / (128 - 43); |
| 621 | } |
| 622 | |
| 623 | return sum; |
| 624 | } |
| 625 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 626 | static double od_compute_dist(uint16_t *x, uint16_t *y, int bsize_w, |
| 627 | int bsize_h, int qindex) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 628 | assert(bsize_w >= 8 && bsize_h >= 8); |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 629 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 630 | int activity_masking = 0; |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 631 | |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 632 | int i, j; |
| 633 | DECLARE_ALIGNED(16, od_coeff, e[MAX_TX_SQUARE]); |
| 634 | DECLARE_ALIGNED(16, od_coeff, tmp[MAX_TX_SQUARE]); |
| 635 | DECLARE_ALIGNED(16, od_coeff, e_lp[MAX_TX_SQUARE]); |
| 636 | for (i = 0; i < bsize_h; i++) { |
| 637 | for (j = 0; j < bsize_w; j++) { |
| 638 | e[i * bsize_w + j] = x[i * bsize_w + j] - y[i * bsize_w + j]; |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 639 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 640 | } |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 641 | int mid = OD_DIST_LP_MID; |
| 642 | for (i = 0; i < bsize_h; i++) { |
| 643 | tmp[i * bsize_w] = mid * e[i * bsize_w] + 2 * e[i * bsize_w + 1]; |
| 644 | tmp[i * bsize_w + bsize_w - 1] = |
| 645 | mid * e[i * bsize_w + bsize_w - 1] + 2 * e[i * bsize_w + bsize_w - 2]; |
| 646 | for (j = 1; j < bsize_w - 1; j++) { |
| 647 | tmp[i * bsize_w + j] = mid * e[i * bsize_w + j] + e[i * bsize_w + j - 1] + |
| 648 | e[i * bsize_w + j + 1]; |
| 649 | } |
| 650 | } |
| 651 | return od_compute_dist_common(activity_masking, x, y, bsize_w, bsize_h, |
| 652 | qindex, tmp, e_lp); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 655 | static double od_compute_dist_diff(uint16_t *x, int16_t *e, int bsize_w, |
| 656 | int bsize_h, int qindex) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 657 | assert(bsize_w >= 8 && bsize_h >= 8); |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 658 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 659 | int activity_masking = 0; |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 660 | |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 661 | DECLARE_ALIGNED(16, uint16_t, y[MAX_TX_SQUARE]); |
| 662 | DECLARE_ALIGNED(16, od_coeff, tmp[MAX_TX_SQUARE]); |
| 663 | DECLARE_ALIGNED(16, od_coeff, e_lp[MAX_TX_SQUARE]); |
| 664 | int i, j; |
| 665 | for (i = 0; i < bsize_h; i++) { |
| 666 | for (j = 0; j < bsize_w; j++) { |
| 667 | y[i * bsize_w + j] = x[i * bsize_w + j] - e[i * bsize_w + j]; |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 668 | } |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 669 | } |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 670 | int mid = OD_DIST_LP_MID; |
| 671 | for (i = 0; i < bsize_h; i++) { |
| 672 | tmp[i * bsize_w] = mid * e[i * bsize_w] + 2 * e[i * bsize_w + 1]; |
| 673 | tmp[i * bsize_w + bsize_w - 1] = |
| 674 | mid * e[i * bsize_w + bsize_w - 1] + 2 * e[i * bsize_w + bsize_w - 2]; |
| 675 | for (j = 1; j < bsize_w - 1; j++) { |
| 676 | tmp[i * bsize_w + j] = mid * e[i * bsize_w + j] + e[i * bsize_w + j - 1] + |
| 677 | e[i * bsize_w + j + 1]; |
| 678 | } |
| 679 | } |
| 680 | return od_compute_dist_common(activity_masking, x, y, bsize_w, bsize_h, |
| 681 | qindex, tmp, e_lp); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 684 | int64_t av1_dist_8x8(const AV1_COMP *const cpi, const MACROBLOCK *x, |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 685 | const uint8_t *src, int src_stride, const uint8_t *dst, |
| 686 | int dst_stride, const BLOCK_SIZE tx_bsize, int bsw, |
| 687 | int bsh, int visible_w, int visible_h, int qindex) { |
| 688 | int64_t d = 0; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 689 | int i, j; |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 690 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 691 | |
| 692 | DECLARE_ALIGNED(16, uint16_t, orig[MAX_TX_SQUARE]); |
| 693 | DECLARE_ALIGNED(16, uint16_t, rec[MAX_TX_SQUARE]); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 694 | |
Yushin Cho | ee81027 | 2017-09-13 17:30:25 -0700 | [diff] [blame] | 695 | assert(bsw >= 8); |
| 696 | assert(bsh >= 8); |
| 697 | assert((bsw & 0x07) == 0); |
| 698 | assert((bsh & 0x07) == 0); |
| 699 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 700 | if (x->tune_metric == AOM_TUNE_CDEF_DIST || |
| 701 | x->tune_metric == AOM_TUNE_DAALA_DIST) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 702 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 703 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 704 | for (j = 0; j < bsh; j++) |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 705 | for (i = 0; i < bsw; i++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 706 | orig[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 707 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 708 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 709 | for (j = 0; j < bsh; j++) |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 710 | for (i = 0; i < bsw; i++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 711 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(dst)[j * dst_stride + i]; |
| 712 | } else { |
| 713 | for (j = 0; j < visible_h; j++) |
| 714 | for (i = 0; i < visible_w; i++) |
| 715 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(dst)[j * dst_stride + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 716 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 717 | if (visible_w < bsw) { |
| 718 | for (j = 0; j < bsh; j++) |
| 719 | for (i = visible_w; i < bsw; i++) |
| 720 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 721 | } |
| 722 | |
| 723 | if (visible_h < bsh) { |
| 724 | for (j = visible_h; j < bsh; j++) |
| 725 | for (i = 0; i < bsw; i++) |
| 726 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 727 | } |
| 728 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 729 | } else { |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 730 | #endif |
| 731 | for (j = 0; j < bsh; j++) |
| 732 | for (i = 0; i < bsw; i++) orig[j * bsw + i] = src[j * src_stride + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 733 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 734 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 735 | for (j = 0; j < bsh; j++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 736 | for (i = 0; i < bsw; i++) rec[j * bsw + i] = dst[j * dst_stride + i]; |
| 737 | } else { |
| 738 | for (j = 0; j < visible_h; j++) |
| 739 | for (i = 0; i < visible_w; i++) |
| 740 | rec[j * bsw + i] = dst[j * dst_stride + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 741 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 742 | if (visible_w < bsw) { |
| 743 | for (j = 0; j < bsh; j++) |
| 744 | for (i = visible_w; i < bsw; i++) |
| 745 | rec[j * bsw + i] = src[j * src_stride + i]; |
| 746 | } |
| 747 | |
| 748 | if (visible_h < bsh) { |
| 749 | for (j = visible_h; j < bsh; j++) |
| 750 | for (i = 0; i < bsw; i++) |
| 751 | rec[j * bsw + i] = src[j * src_stride + i]; |
| 752 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 753 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 754 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 755 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 756 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 757 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 758 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 759 | if (x->tune_metric == AOM_TUNE_DAALA_DIST) { |
| 760 | d = (int64_t)od_compute_dist(orig, rec, bsw, bsh, qindex); |
| 761 | } else if (x->tune_metric == AOM_TUNE_CDEF_DIST) { |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 762 | int coeff_shift = AOMMAX(xd->bd - 8, 0); |
| 763 | |
| 764 | for (i = 0; i < bsh; i += 8) { |
| 765 | for (j = 0; j < bsw; j += 8) { |
| 766 | d += cdef_dist_8x8_16bit(&rec[i * bsw + j], bsw, &orig[i * bsw + j], |
| 767 | bsw, coeff_shift); |
| 768 | } |
| 769 | } |
| 770 | #if CONFIG_HIGHBITDEPTH |
| 771 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 772 | d = ((uint64_t)d) >> 2 * coeff_shift; |
| 773 | #endif |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 774 | } else { |
| 775 | // Otherwise, MSE by default |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 776 | d = pixel_dist_visible_only(cpi, x, src, src_stride, dst, dst_stride, |
| 777 | tx_bsize, bsh, bsw, visible_h, visible_w); |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 778 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 779 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 780 | return d; |
| 781 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 782 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 783 | static int64_t av1_dist_8x8_diff(const MACROBLOCK *x, const uint8_t *src, |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 784 | int src_stride, const int16_t *diff, |
| 785 | int diff_stride, int bsw, int bsh, |
| 786 | int visible_w, int visible_h, int qindex) { |
| 787 | int64_t d = 0; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 788 | int i, j; |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 789 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 790 | |
| 791 | DECLARE_ALIGNED(16, uint16_t, orig[MAX_TX_SQUARE]); |
| 792 | DECLARE_ALIGNED(16, int16_t, diff16[MAX_TX_SQUARE]); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 793 | |
Yushin Cho | ee81027 | 2017-09-13 17:30:25 -0700 | [diff] [blame] | 794 | assert(bsw >= 8); |
| 795 | assert(bsh >= 8); |
| 796 | assert((bsw & 0x07) == 0); |
| 797 | assert((bsh & 0x07) == 0); |
| 798 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 799 | if (x->tune_metric == AOM_TUNE_CDEF_DIST || |
| 800 | x->tune_metric == AOM_TUNE_DAALA_DIST) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 801 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 802 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 803 | for (j = 0; j < bsh; j++) |
| 804 | for (i = 0; i < bsw; i++) |
| 805 | orig[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 806 | } else { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 807 | #endif |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 808 | for (j = 0; j < bsh; j++) |
| 809 | for (i = 0; i < bsw; i++) orig[j * bsw + i] = src[j * src_stride + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 810 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 811 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 812 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 813 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 814 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 815 | for (j = 0; j < bsh; j++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 816 | for (i = 0; i < bsw; i++) |
| 817 | diff16[j * bsw + i] = diff[j * diff_stride + i]; |
| 818 | } else { |
| 819 | for (j = 0; j < visible_h; j++) |
| 820 | for (i = 0; i < visible_w; i++) |
| 821 | diff16[j * bsw + i] = diff[j * diff_stride + i]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 822 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 823 | if (visible_w < bsw) { |
| 824 | for (j = 0; j < bsh; j++) |
| 825 | for (i = visible_w; i < bsw; i++) diff16[j * bsw + i] = 0; |
| 826 | } |
| 827 | |
| 828 | if (visible_h < bsh) { |
| 829 | for (j = visible_h; j < bsh; j++) |
| 830 | for (i = 0; i < bsw; i++) diff16[j * bsw + i] = 0; |
| 831 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 832 | } |
| 833 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 834 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 835 | if (x->tune_metric == AOM_TUNE_DAALA_DIST) { |
| 836 | d = (int64_t)od_compute_dist_diff(orig, diff16, bsw, bsh, qindex); |
| 837 | } else if (x->tune_metric == AOM_TUNE_CDEF_DIST) { |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 838 | int coeff_shift = AOMMAX(xd->bd - 8, 0); |
| 839 | DECLARE_ALIGNED(16, uint16_t, dst16[MAX_TX_SQUARE]); |
| 840 | |
| 841 | for (i = 0; i < bsh; i++) { |
| 842 | for (j = 0; j < bsw; j++) { |
| 843 | dst16[i * bsw + j] = orig[i * bsw + j] - diff16[i * bsw + j]; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | for (i = 0; i < bsh; i += 8) { |
| 848 | for (j = 0; j < bsw; j += 8) { |
| 849 | d += cdef_dist_8x8_16bit(&dst16[i * bsw + j], bsw, &orig[i * bsw + j], |
| 850 | bsw, coeff_shift); |
| 851 | } |
| 852 | } |
| 853 | // Don't scale 'd' for HBD since it will be done by caller side for diff |
| 854 | // input |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 855 | } else { |
| 856 | // Otherwise, MSE by default |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 857 | d = aom_sum_squares_2d_i16(diff, diff_stride, visible_w, visible_h); |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 858 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 859 | |
| 860 | return d; |
| 861 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 862 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 863 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 864 | static void get_energy_distribution_fine(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 865 | const uint8_t *src, int src_stride, |
| 866 | const uint8_t *dst, int dst_stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 867 | double *hordist, double *verdist) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 868 | const int bw = block_size_wide[bsize]; |
| 869 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 870 | unsigned int esq[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 871 | |
| 872 | const int f_index = bsize - BLOCK_16X16; |
| 873 | if (f_index < 0) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 874 | const int w_shift = bw == 8 ? 1 : 2; |
| 875 | const int h_shift = bh == 8 ? 1 : 2; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 876 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 877 | if (cpi->common.use_highbitdepth) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 878 | const uint16_t *src16 = CONVERT_TO_SHORTPTR(src); |
| 879 | const uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst); |
| 880 | for (int i = 0; i < bh; ++i) |
| 881 | for (int j = 0; j < bw; ++j) { |
| 882 | const int index = (j >> w_shift) + ((i >> h_shift) << 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 883 | esq[index] += |
| 884 | (src16[j + i * src_stride] - dst16[j + i * dst_stride]) * |
| 885 | (src16[j + i * src_stride] - dst16[j + i * dst_stride]); |
| 886 | } |
| 887 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 888 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 889 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 890 | for (int i = 0; i < bh; ++i) |
| 891 | for (int j = 0; j < bw; ++j) { |
| 892 | const int index = (j >> w_shift) + ((i >> h_shift) << 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 893 | esq[index] += (src[j + i * src_stride] - dst[j + i * dst_stride]) * |
| 894 | (src[j + i * src_stride] - dst[j + i * dst_stride]); |
| 895 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 896 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 897 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 898 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 899 | } else { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 900 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[0]); |
| 901 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 902 | &esq[1]); |
| 903 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 904 | &esq[2]); |
| 905 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 906 | dst_stride, &esq[3]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 907 | src += bh / 4 * src_stride; |
| 908 | dst += bh / 4 * dst_stride; |
| 909 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 910 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[4]); |
| 911 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 912 | &esq[5]); |
| 913 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 914 | &esq[6]); |
| 915 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 916 | dst_stride, &esq[7]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 917 | src += bh / 4 * src_stride; |
| 918 | dst += bh / 4 * dst_stride; |
| 919 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 920 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[8]); |
| 921 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 922 | &esq[9]); |
| 923 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 924 | &esq[10]); |
| 925 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 926 | dst_stride, &esq[11]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 927 | src += bh / 4 * src_stride; |
| 928 | dst += bh / 4 * dst_stride; |
| 929 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 930 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[12]); |
| 931 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 932 | &esq[13]); |
| 933 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 934 | &esq[14]); |
| 935 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 936 | dst_stride, &esq[15]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 939 | double total = (double)esq[0] + esq[1] + esq[2] + esq[3] + esq[4] + esq[5] + |
| 940 | esq[6] + esq[7] + esq[8] + esq[9] + esq[10] + esq[11] + |
| 941 | esq[12] + esq[13] + esq[14] + esq[15]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 942 | if (total > 0) { |
| 943 | const double e_recip = 1.0 / total; |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 944 | hordist[0] = ((double)esq[0] + esq[4] + esq[8] + esq[12]) * e_recip; |
| 945 | hordist[1] = ((double)esq[1] + esq[5] + esq[9] + esq[13]) * e_recip; |
| 946 | hordist[2] = ((double)esq[2] + esq[6] + esq[10] + esq[14]) * e_recip; |
| 947 | verdist[0] = ((double)esq[0] + esq[1] + esq[2] + esq[3]) * e_recip; |
| 948 | verdist[1] = ((double)esq[4] + esq[5] + esq[6] + esq[7]) * e_recip; |
| 949 | verdist[2] = ((double)esq[8] + esq[9] + esq[10] + esq[11]) * e_recip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 950 | } else { |
| 951 | hordist[0] = verdist[0] = 0.25; |
| 952 | hordist[1] = verdist[1] = 0.25; |
| 953 | hordist[2] = verdist[2] = 0.25; |
| 954 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 957 | static int adst_vs_flipadst(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
| 958 | const uint8_t *src, int src_stride, |
| 959 | const uint8_t *dst, int dst_stride) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 960 | int prune_bitmask = 0; |
| 961 | double svm_proj_h = 0, svm_proj_v = 0; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 962 | double hdist[3] = { 0, 0, 0 }, vdist[3] = { 0, 0, 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 963 | get_energy_distribution_fine(cpi, bsize, src, src_stride, dst, dst_stride, |
| 964 | hdist, vdist); |
| 965 | |
| 966 | svm_proj_v = vdist[0] * ADST_FLIP_SVM[0] + vdist[1] * ADST_FLIP_SVM[1] + |
| 967 | vdist[2] * ADST_FLIP_SVM[2] + ADST_FLIP_SVM[3]; |
| 968 | svm_proj_h = hdist[0] * ADST_FLIP_SVM[4] + hdist[1] * ADST_FLIP_SVM[5] + |
| 969 | hdist[2] * ADST_FLIP_SVM[6] + ADST_FLIP_SVM[7]; |
| 970 | if (svm_proj_v > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN) |
| 971 | prune_bitmask |= 1 << FLIPADST_1D; |
| 972 | else if (svm_proj_v < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN) |
| 973 | prune_bitmask |= 1 << ADST_1D; |
| 974 | |
| 975 | if (svm_proj_h > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN) |
| 976 | prune_bitmask |= 1 << (FLIPADST_1D + 8); |
| 977 | else if (svm_proj_h < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN) |
| 978 | prune_bitmask |= 1 << (ADST_1D + 8); |
| 979 | |
| 980 | return prune_bitmask; |
| 981 | } |
| 982 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 983 | static void get_horver_correlation(const int16_t *diff, int stride, int w, |
| 984 | int h, double *hcorr, double *vcorr) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 985 | // Returns hor/ver correlation coefficient |
| 986 | const int num = (h - 1) * (w - 1); |
| 987 | double num_r; |
| 988 | int i, j; |
| 989 | int64_t xy_sum = 0, xz_sum = 0; |
| 990 | int64_t x_sum = 0, y_sum = 0, z_sum = 0; |
| 991 | int64_t x2_sum = 0, y2_sum = 0, z2_sum = 0; |
| 992 | double x_var_n, y_var_n, z_var_n, xy_var_n, xz_var_n; |
| 993 | *hcorr = *vcorr = 1; |
| 994 | |
| 995 | assert(num > 0); |
| 996 | num_r = 1.0 / num; |
| 997 | for (i = 1; i < h; ++i) { |
| 998 | for (j = 1; j < w; ++j) { |
| 999 | const int16_t x = diff[i * stride + j]; |
| 1000 | const int16_t y = diff[i * stride + j - 1]; |
| 1001 | const int16_t z = diff[(i - 1) * stride + j]; |
| 1002 | xy_sum += x * y; |
| 1003 | xz_sum += x * z; |
| 1004 | x_sum += x; |
| 1005 | y_sum += y; |
| 1006 | z_sum += z; |
| 1007 | x2_sum += x * x; |
| 1008 | y2_sum += y * y; |
| 1009 | z2_sum += z * z; |
| 1010 | } |
| 1011 | } |
| 1012 | x_var_n = x2_sum - (x_sum * x_sum) * num_r; |
| 1013 | y_var_n = y2_sum - (y_sum * y_sum) * num_r; |
| 1014 | z_var_n = z2_sum - (z_sum * z_sum) * num_r; |
| 1015 | xy_var_n = xy_sum - (x_sum * y_sum) * num_r; |
| 1016 | xz_var_n = xz_sum - (x_sum * z_sum) * num_r; |
| 1017 | if (x_var_n > 0 && y_var_n > 0) { |
| 1018 | *hcorr = xy_var_n / sqrt(x_var_n * y_var_n); |
| 1019 | *hcorr = *hcorr < 0 ? 0 : *hcorr; |
| 1020 | } |
| 1021 | if (x_var_n > 0 && z_var_n > 0) { |
| 1022 | *vcorr = xz_var_n / sqrt(x_var_n * z_var_n); |
| 1023 | *vcorr = *vcorr < 0 ? 0 : *vcorr; |
| 1024 | } |
| 1025 | } |
| 1026 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1027 | int dct_vs_idtx(const int16_t *diff, int stride, int w, int h) { |
| 1028 | double hcorr, vcorr; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1029 | int prune_bitmask = 0; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1030 | get_horver_correlation(diff, stride, w, h, &hcorr, &vcorr); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1031 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1032 | if (vcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1033 | prune_bitmask |= 1 << IDTX_1D; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1034 | else if (vcorr < FAST_EXT_TX_CORR_MID - FAST_EXT_TX_CORR_MARGIN) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1035 | prune_bitmask |= 1 << DCT_1D; |
| 1036 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1037 | if (hcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1038 | prune_bitmask |= 1 << (IDTX_1D + 8); |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1039 | else if (hcorr < FAST_EXT_TX_CORR_MID - FAST_EXT_TX_CORR_MARGIN) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1040 | prune_bitmask |= 1 << (DCT_1D + 8); |
| 1041 | return prune_bitmask; |
| 1042 | } |
| 1043 | |
| 1044 | // Performance drop: 0.5%, Speed improvement: 24% |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1045 | static int prune_two_for_sby(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1046 | MACROBLOCK *x, const MACROBLOCKD *xd, |
| 1047 | int adst_flipadst, int dct_idtx) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1048 | int prune = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1049 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1050 | if (adst_flipadst) { |
| 1051 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1052 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1053 | prune |= adst_vs_flipadst(cpi, bsize, p->src.buf, p->src.stride, |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1054 | pd->dst.buf, pd->dst.stride); |
| 1055 | } |
| 1056 | if (dct_idtx) { |
| 1057 | av1_subtract_plane(x, bsize, 0); |
| 1058 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1059 | const int bw = 4 << (b_width_log2_lookup[bsize]); |
| 1060 | const int bh = 4 << (b_height_log2_lookup[bsize]); |
| 1061 | prune |= dct_vs_idtx(p->src_diff, bw, bw, bh); |
| 1062 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1063 | |
| 1064 | return prune; |
| 1065 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1066 | |
| 1067 | // Performance drop: 0.3%, Speed improvement: 5% |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1068 | static int prune_one_for_sby(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1069 | const MACROBLOCK *x, const MACROBLOCKD *xd) { |
| 1070 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1071 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1072 | return adst_vs_flipadst(cpi, bsize, p->src.buf, p->src.stride, pd->dst.buf, |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1073 | pd->dst.stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Hui Su | 032ab8b | 2017-09-19 14:53:40 -0700 | [diff] [blame] | 1076 | // 1D Transforms used in inter set, this needs to be changed if |
| 1077 | // ext_tx_used_inter is changed |
| 1078 | static const int ext_tx_used_inter_1D[EXT_TX_SETS_INTER][TX_TYPES_1D] = { |
| 1079 | { 1, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, |
Hui Su | 032ab8b | 2017-09-19 14:53:40 -0700 | [diff] [blame] | 1080 | }; |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1081 | |
| 1082 | static void get_energy_distribution_finer(const int16_t *diff, int stride, |
| 1083 | int bw, int bh, float *hordist, |
| 1084 | float *verdist) { |
| 1085 | // First compute downscaled block energy values (esq); downscale factors |
| 1086 | // are defined by w_shift and h_shift. |
| 1087 | unsigned int esq[256]; |
| 1088 | const int w_shift = bw <= 8 ? 0 : 1; |
| 1089 | const int h_shift = bh <= 8 ? 0 : 1; |
| 1090 | const int esq_w = bw <= 8 ? bw : bw / 2; |
| 1091 | const int esq_h = bh <= 8 ? bh : bh / 2; |
| 1092 | const int esq_sz = esq_w * esq_h; |
| 1093 | int i, j; |
| 1094 | memset(esq, 0, esq_sz * sizeof(esq[0])); |
| 1095 | for (i = 0; i < bh; i++) { |
| 1096 | unsigned int *cur_esq_row = esq + (i >> h_shift) * esq_w; |
| 1097 | const int16_t *cur_diff_row = diff + i * stride; |
| 1098 | for (j = 0; j < bw; j++) { |
| 1099 | cur_esq_row[j >> w_shift] += cur_diff_row[j] * cur_diff_row[j]; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | uint64_t total = 0; |
| 1104 | for (i = 0; i < esq_sz; i++) total += esq[i]; |
| 1105 | |
| 1106 | // Output hordist and verdist arrays are normalized 1D projections of esq |
| 1107 | if (total == 0) { |
| 1108 | float hor_val = 1.0f / esq_w; |
| 1109 | for (j = 0; j < esq_w - 1; j++) hordist[j] = hor_val; |
| 1110 | float ver_val = 1.0f / esq_h; |
| 1111 | for (i = 0; i < esq_h - 1; i++) verdist[i] = ver_val; |
| 1112 | return; |
| 1113 | } |
| 1114 | |
| 1115 | const float e_recip = 1.0f / (float)total; |
| 1116 | memset(hordist, 0, (esq_w - 1) * sizeof(hordist[0])); |
| 1117 | memset(verdist, 0, (esq_h - 1) * sizeof(verdist[0])); |
| 1118 | const unsigned int *cur_esq_row; |
| 1119 | for (i = 0; i < esq_h - 1; i++) { |
| 1120 | cur_esq_row = esq + i * esq_w; |
| 1121 | for (j = 0; j < esq_w - 1; j++) { |
| 1122 | hordist[j] += (float)cur_esq_row[j]; |
| 1123 | verdist[i] += (float)cur_esq_row[j]; |
| 1124 | } |
| 1125 | verdist[i] += (float)cur_esq_row[j]; |
| 1126 | } |
| 1127 | cur_esq_row = esq + i * esq_w; |
| 1128 | for (j = 0; j < esq_w - 1; j++) hordist[j] += (float)cur_esq_row[j]; |
| 1129 | |
| 1130 | for (j = 0; j < esq_w - 1; j++) hordist[j] *= e_recip; |
| 1131 | for (i = 0; i < esq_h - 1; i++) verdist[i] *= e_recip; |
| 1132 | } |
| 1133 | |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1134 | // Instead of 1D projections of the block energy distribution computed by |
| 1135 | // get_energy_distribution_finer() this function computes a full |
| 1136 | // two-dimensional energy distribution of the input block. |
| 1137 | static void get_2D_energy_distribution(const int16_t *diff, int stride, int bw, |
| 1138 | int bh, float *edist) { |
| 1139 | unsigned int esq[256] = { 0 }; |
| 1140 | const int esq_w = bw >> 2; |
| 1141 | const int esq_h = bh >> 2; |
| 1142 | const int esq_sz = esq_w * esq_h; |
| 1143 | uint64_t total = 0; |
| 1144 | for (int i = 0; i < bh; i += 4) { |
| 1145 | for (int j = 0; j < bw; j += 4) { |
| 1146 | unsigned int cur_sum_energy = 0; |
| 1147 | for (int k = 0; k < 4; k++) { |
| 1148 | const int16_t *cur_diff = diff + (i + k) * stride + j; |
| 1149 | cur_sum_energy += cur_diff[0] * cur_diff[0] + |
| 1150 | cur_diff[1] * cur_diff[1] + |
| 1151 | cur_diff[2] * cur_diff[2] + cur_diff[3] * cur_diff[3]; |
| 1152 | } |
| 1153 | esq[(i >> 2) * esq_w + (j >> 2)] = cur_sum_energy; |
| 1154 | total += cur_sum_energy; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | const float e_recip = 1.0f / (float)total; |
| 1159 | for (int i = 0; i < esq_sz - 1; i++) edist[i] = esq[i] * e_recip; |
| 1160 | } |
| 1161 | |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1162 | // Similar to get_horver_correlation, but also takes into account first |
| 1163 | // row/column, when computing horizontal/vertical correlation. |
| 1164 | static void get_horver_correlation_full(const int16_t *diff, int stride, int w, |
| 1165 | int h, float *hcorr, float *vcorr) { |
Yaowu Xu | 29373ee | 2017-10-19 15:50:34 -0700 | [diff] [blame] | 1166 | const float num_hor = (float)(h * (w - 1)); |
| 1167 | const float num_ver = (float)((h - 1) * w); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1168 | int i, j; |
| 1169 | |
| 1170 | // The following notation is used: |
| 1171 | // x - current pixel |
| 1172 | // y - left neighbor pixel |
| 1173 | // z - top neighbor pixel |
| 1174 | int64_t xy_sum = 0, xz_sum = 0; |
| 1175 | int64_t xhor_sum = 0, xver_sum = 0, y_sum = 0, z_sum = 0; |
| 1176 | int64_t x2hor_sum = 0, x2ver_sum = 0, y2_sum = 0, z2_sum = 0; |
| 1177 | |
| 1178 | int16_t x, y, z; |
| 1179 | for (j = 1; j < w; ++j) { |
| 1180 | x = diff[j]; |
| 1181 | y = diff[j - 1]; |
| 1182 | xy_sum += x * y; |
| 1183 | xhor_sum += x; |
| 1184 | y_sum += y; |
| 1185 | x2hor_sum += x * x; |
| 1186 | y2_sum += y * y; |
| 1187 | } |
| 1188 | for (i = 1; i < h; ++i) { |
| 1189 | x = diff[i * stride]; |
| 1190 | z = diff[(i - 1) * stride]; |
| 1191 | xz_sum += x * z; |
| 1192 | xver_sum += x; |
| 1193 | z_sum += z; |
| 1194 | x2ver_sum += x * x; |
| 1195 | z2_sum += z * z; |
| 1196 | for (j = 1; j < w; ++j) { |
| 1197 | x = diff[i * stride + j]; |
| 1198 | y = diff[i * stride + j - 1]; |
| 1199 | z = diff[(i - 1) * stride + j]; |
| 1200 | xy_sum += x * y; |
| 1201 | xz_sum += x * z; |
| 1202 | xhor_sum += x; |
| 1203 | xver_sum += x; |
| 1204 | y_sum += y; |
| 1205 | z_sum += z; |
| 1206 | x2hor_sum += x * x; |
| 1207 | x2ver_sum += x * x; |
| 1208 | y2_sum += y * y; |
| 1209 | z2_sum += z * z; |
| 1210 | } |
| 1211 | } |
| 1212 | const float xhor_var_n = x2hor_sum - (xhor_sum * xhor_sum) / num_hor; |
| 1213 | const float y_var_n = y2_sum - (y_sum * y_sum) / num_hor; |
| 1214 | const float xy_var_n = xy_sum - (xhor_sum * y_sum) / num_hor; |
| 1215 | const float xver_var_n = x2ver_sum - (xver_sum * xver_sum) / num_ver; |
| 1216 | const float z_var_n = z2_sum - (z_sum * z_sum) / num_ver; |
| 1217 | const float xz_var_n = xz_sum - (xver_sum * z_sum) / num_ver; |
| 1218 | |
| 1219 | *hcorr = *vcorr = 1; |
| 1220 | if (xhor_var_n > 0 && y_var_n > 0) { |
| 1221 | *hcorr = xy_var_n / sqrtf(xhor_var_n * y_var_n); |
| 1222 | *hcorr = *hcorr < 0 ? 0 : *hcorr; |
| 1223 | } |
| 1224 | if (xver_var_n > 0 && z_var_n > 0) { |
| 1225 | *vcorr = xz_var_n / sqrtf(xver_var_n * z_var_n); |
| 1226 | *vcorr = *vcorr < 0 ? 0 : *vcorr; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | // Performs a forward pass through a neural network with 2 fully-connected |
| 1231 | // layers, assuming ReLU as activation function. Number of output neurons |
| 1232 | // is always equal to 4. |
| 1233 | // fc1, fc2 - weight matrices of the respective layers. |
| 1234 | // b1, b2 - bias vectors of the respective layers. |
| 1235 | static void compute_1D_scores(float *features, int num_features, |
| 1236 | const float *fc1, const float *b1, |
| 1237 | const float *fc2, const float *b2, |
| 1238 | int num_hidden_units, float *dst_scores) { |
| 1239 | assert(num_hidden_units <= 32); |
| 1240 | float hidden_layer[32]; |
| 1241 | for (int i = 0; i < num_hidden_units; i++) { |
| 1242 | const float *cur_coef = fc1 + i * num_features; |
| 1243 | hidden_layer[i] = 0.0f; |
| 1244 | for (int j = 0; j < num_features; j++) |
| 1245 | hidden_layer[i] += cur_coef[j] * features[j]; |
| 1246 | hidden_layer[i] = AOMMAX(hidden_layer[i] + b1[i], 0.0f); |
| 1247 | } |
| 1248 | for (int i = 0; i < 4; i++) { |
| 1249 | const float *cur_coef = fc2 + i * num_hidden_units; |
| 1250 | dst_scores[i] = 0.0f; |
| 1251 | for (int j = 0; j < num_hidden_units; j++) |
| 1252 | dst_scores[i] += cur_coef[j] * hidden_layer[j]; |
| 1253 | dst_scores[i] += b2[i]; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | // Transforms raw scores into a probability distribution across 16 TX types |
| 1258 | static void score_2D_transform_pow8(float *scores_2D, float shift) { |
| 1259 | float sum = 0.0f; |
| 1260 | int i; |
| 1261 | |
| 1262 | for (i = 0; i < 16; i++) { |
| 1263 | float v, v2, v4; |
| 1264 | v = AOMMAX(scores_2D[i] + shift, 0.0f); |
| 1265 | v2 = v * v; |
| 1266 | v4 = v2 * v2; |
| 1267 | scores_2D[i] = v4 * v4; |
| 1268 | sum += scores_2D[i]; |
| 1269 | } |
| 1270 | for (i = 0; i < 16; i++) scores_2D[i] /= sum; |
| 1271 | } |
| 1272 | |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1273 | // Similarly to compute_1D_scores() performs a forward pass through a |
| 1274 | // neural network with two fully-connected layers. The only difference |
| 1275 | // is that it assumes 1 output neuron, as required by the classifier used |
| 1276 | // for TX size pruning. |
| 1277 | static float compute_tx_split_prune_score(float *features, int num_features, |
| 1278 | const float *fc1, const float *b1, |
| 1279 | const float *fc2, float b2, |
| 1280 | int num_hidden_units) { |
| 1281 | assert(num_hidden_units <= 64); |
| 1282 | float hidden_layer[64]; |
| 1283 | for (int i = 0; i < num_hidden_units; i++) { |
| 1284 | const float *cur_coef = fc1 + i * num_features; |
| 1285 | hidden_layer[i] = 0.0f; |
| 1286 | for (int j = 0; j < num_features; j++) |
| 1287 | hidden_layer[i] += cur_coef[j] * features[j]; |
| 1288 | hidden_layer[i] = AOMMAX(hidden_layer[i] + b1[i], 0.0f); |
| 1289 | } |
| 1290 | float dst_score = 0.0f; |
| 1291 | for (int j = 0; j < num_hidden_units; j++) |
| 1292 | dst_score += fc2[j] * hidden_layer[j]; |
| 1293 | dst_score += b2; |
| 1294 | return dst_score; |
| 1295 | } |
| 1296 | |
| 1297 | static int prune_tx_split(BLOCK_SIZE bsize, const int16_t *diff, float hcorr, |
| 1298 | float vcorr) { |
| 1299 | if (bsize <= BLOCK_4X4 || bsize > BLOCK_16X16) return 0; |
| 1300 | |
| 1301 | float features[17]; |
| 1302 | const int bw = block_size_wide[bsize], bh = block_size_high[bsize]; |
| 1303 | const int feature_num = (bw / 4) * (bh / 4) + 1; |
| 1304 | assert(feature_num <= 17); |
| 1305 | |
| 1306 | get_2D_energy_distribution(diff, bw, bw, bh, features); |
| 1307 | features[feature_num - 2] = hcorr; |
| 1308 | features[feature_num - 1] = vcorr; |
| 1309 | |
| 1310 | const int bidx = bsize - BLOCK_4X4 - 1; |
| 1311 | const float *fc1 = av1_prune_tx_split_learned_weights[bidx]; |
| 1312 | const float *b1 = |
| 1313 | fc1 + av1_prune_tx_split_num_hidden_units[bidx] * feature_num; |
| 1314 | const float *fc2 = b1 + av1_prune_tx_split_num_hidden_units[bidx]; |
| 1315 | float b2 = *(fc2 + av1_prune_tx_split_num_hidden_units[bidx]); |
| 1316 | float score = |
| 1317 | compute_tx_split_prune_score(features, feature_num, fc1, b1, fc2, b2, |
| 1318 | av1_prune_tx_split_num_hidden_units[bidx]); |
| 1319 | |
| 1320 | return (score > av1_prune_tx_split_thresholds[bidx]); |
| 1321 | } |
| 1322 | |
| 1323 | static int prune_tx_2D(BLOCK_SIZE bsize, const MACROBLOCK *x, int tx_set_type, |
| 1324 | int tx_type_pruning_aggressiveness, |
| 1325 | int use_tx_split_prune) { |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1326 | if (bsize >= BLOCK_32X32) return 0; |
Hui Su | 9bf8599 | 2017-11-21 14:18:03 -0800 | [diff] [blame] | 1327 | aom_clear_system_state(); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1328 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1329 | const int bidx = AOMMAX(bsize - BLOCK_4X4, 0); |
| 1330 | const float score_thresh = |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1331 | av1_prune_2D_adaptive_thresholds[bidx] |
| 1332 | [tx_type_pruning_aggressiveness - 1]; |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1333 | float hfeatures[16], vfeatures[16]; |
| 1334 | float hscores[4], vscores[4]; |
| 1335 | float scores_2D[16]; |
| 1336 | int tx_type_table_2D[16] = { |
| 1337 | DCT_DCT, DCT_ADST, DCT_FLIPADST, V_DCT, |
| 1338 | ADST_DCT, ADST_ADST, ADST_FLIPADST, V_ADST, |
| 1339 | FLIPADST_DCT, FLIPADST_ADST, FLIPADST_FLIPADST, V_FLIPADST, |
| 1340 | H_DCT, H_ADST, H_FLIPADST, IDTX |
| 1341 | }; |
| 1342 | const int bw = block_size_wide[bsize], bh = block_size_high[bsize]; |
| 1343 | const int hfeatures_num = bw <= 8 ? bw : bw / 2; |
| 1344 | const int vfeatures_num = bh <= 8 ? bh : bh / 2; |
| 1345 | assert(hfeatures_num <= 16); |
| 1346 | assert(vfeatures_num <= 16); |
| 1347 | |
| 1348 | get_energy_distribution_finer(p->src_diff, bw, bw, bh, hfeatures, vfeatures); |
| 1349 | get_horver_correlation_full(p->src_diff, bw, bw, bh, |
| 1350 | &hfeatures[hfeatures_num - 1], |
| 1351 | &vfeatures[vfeatures_num - 1]); |
| 1352 | |
| 1353 | const float *fc1_hor = av1_prune_2D_learned_weights_hor[bidx]; |
| 1354 | const float *b1_hor = |
| 1355 | fc1_hor + av1_prune_2D_num_hidden_units_hor[bidx] * hfeatures_num; |
| 1356 | const float *fc2_hor = b1_hor + av1_prune_2D_num_hidden_units_hor[bidx]; |
| 1357 | const float *b2_hor = fc2_hor + av1_prune_2D_num_hidden_units_hor[bidx] * 4; |
| 1358 | compute_1D_scores(hfeatures, hfeatures_num, fc1_hor, b1_hor, fc2_hor, b2_hor, |
| 1359 | av1_prune_2D_num_hidden_units_hor[bidx], hscores); |
| 1360 | |
| 1361 | const float *fc1_ver = av1_prune_2D_learned_weights_ver[bidx]; |
| 1362 | const float *b1_ver = |
| 1363 | fc1_ver + av1_prune_2D_num_hidden_units_ver[bidx] * vfeatures_num; |
| 1364 | const float *fc2_ver = b1_ver + av1_prune_2D_num_hidden_units_ver[bidx]; |
| 1365 | const float *b2_ver = fc2_ver + av1_prune_2D_num_hidden_units_ver[bidx] * 4; |
| 1366 | compute_1D_scores(vfeatures, vfeatures_num, fc1_ver, b1_ver, fc2_ver, b2_ver, |
| 1367 | av1_prune_2D_num_hidden_units_ver[bidx], vscores); |
| 1368 | |
| 1369 | float score_2D_average = 0.0f; |
| 1370 | for (int i = 0; i < 4; i++) { |
| 1371 | float *cur_scores_2D = scores_2D + i * 4; |
| 1372 | cur_scores_2D[0] = vscores[i] * hscores[0]; |
| 1373 | cur_scores_2D[1] = vscores[i] * hscores[1]; |
| 1374 | cur_scores_2D[2] = vscores[i] * hscores[2]; |
| 1375 | cur_scores_2D[3] = vscores[i] * hscores[3]; |
| 1376 | score_2D_average += cur_scores_2D[0] + cur_scores_2D[1] + cur_scores_2D[2] + |
| 1377 | cur_scores_2D[3]; |
| 1378 | } |
| 1379 | score_2D_average /= 16; |
| 1380 | score_2D_transform_pow8(scores_2D, (20 - score_2D_average)); |
| 1381 | |
| 1382 | // Always keep the TX type with the highest score, prune all others with |
| 1383 | // score below score_thresh. |
| 1384 | int max_score_i = 0; |
| 1385 | float max_score = 0.0f; |
| 1386 | for (int i = 0; i < 16; i++) { |
| 1387 | if (scores_2D[i] > max_score && |
| 1388 | av1_ext_tx_used[tx_set_type][tx_type_table_2D[i]]) { |
| 1389 | max_score = scores_2D[i]; |
| 1390 | max_score_i = i; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | int prune_bitmask = 0; |
| 1395 | for (int i = 0; i < 16; i++) { |
| 1396 | if (scores_2D[i] < score_thresh && i != max_score_i) |
| 1397 | prune_bitmask |= (1 << tx_type_table_2D[i]); |
| 1398 | } |
| 1399 | |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1400 | // Also apply TX size pruning if it's turned on. The value |
| 1401 | // of prune_tx_split_flag indicates whether we should do |
| 1402 | // full TX size search (flag=0) or use the largest available |
| 1403 | // TX size without performing any further search (flag=1). |
| 1404 | int prune_tx_split_flag = 0; |
| 1405 | if (use_tx_split_prune) { |
| 1406 | prune_tx_split_flag = |
| 1407 | prune_tx_split(bsize, p->src_diff, hfeatures[hfeatures_num - 1], |
| 1408 | vfeatures[vfeatures_num - 1]); |
| 1409 | } |
| 1410 | prune_bitmask |= (prune_tx_split_flag << TX_TYPES); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1411 | return prune_bitmask; |
| 1412 | } |
Hui Su | 032ab8b | 2017-09-19 14:53:40 -0700 | [diff] [blame] | 1413 | |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1414 | static int prune_tx(const AV1_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x, |
| 1415 | const MACROBLOCKD *const xd, int tx_set_type, |
| 1416 | int use_tx_split_prune) { |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1417 | int tx_set = ext_tx_set_index[1][tx_set_type]; |
| 1418 | assert(tx_set >= 0); |
| 1419 | const int *tx_set_1D = ext_tx_used_inter_1D[tx_set]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1420 | |
| 1421 | switch (cpi->sf.tx_type_search.prune_mode) { |
| 1422 | case NO_PRUNE: return 0; break; |
| 1423 | case PRUNE_ONE: |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1424 | if (!(tx_set_1D[FLIPADST_1D] & tx_set_1D[ADST_1D])) return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1425 | return prune_one_for_sby(cpi, bsize, x, xd); |
| 1426 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1427 | case PRUNE_TWO: |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1428 | if (!(tx_set_1D[FLIPADST_1D] & tx_set_1D[ADST_1D])) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1429 | if (!(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D])) return 0; |
| 1430 | return prune_two_for_sby(cpi, bsize, x, xd, 0, 1); |
| 1431 | } |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1432 | if (!(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D])) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1433 | return prune_two_for_sby(cpi, bsize, x, xd, 1, 0); |
| 1434 | return prune_two_for_sby(cpi, bsize, x, xd, 1, 1); |
| 1435 | break; |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1436 | case PRUNE_2D_ACCURATE: |
| 1437 | if (tx_set_type == EXT_TX_SET_ALL16) |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1438 | return prune_tx_2D(bsize, x, tx_set_type, 6, use_tx_split_prune); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1439 | else if (tx_set_type == EXT_TX_SET_DTT9_IDTX_1DDCT) |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1440 | return prune_tx_2D(bsize, x, tx_set_type, 4, use_tx_split_prune); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1441 | else |
| 1442 | return 0; |
| 1443 | break; |
| 1444 | case PRUNE_2D_FAST: |
| 1445 | if (tx_set_type == EXT_TX_SET_ALL16) |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1446 | return prune_tx_2D(bsize, x, tx_set_type, 10, use_tx_split_prune); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1447 | else if (tx_set_type == EXT_TX_SET_DTT9_IDTX_1DDCT) |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 1448 | return prune_tx_2D(bsize, x, tx_set_type, 7, use_tx_split_prune); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1449 | else |
| 1450 | return 0; |
| 1451 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1452 | } |
| 1453 | assert(0); |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1457 | static int do_tx_type_search(TX_TYPE tx_type, int prune, |
| 1458 | TX_TYPE_PRUNE_MODE mode) { |
Sebastien Alaiwan | 3bac992 | 2017-11-02 12:34:41 +0100 | [diff] [blame] | 1459 | // TODO(sarahparker) implement for non ext tx |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 1460 | if (mode >= PRUNE_2D_ACCURATE) { |
| 1461 | return !((prune >> tx_type) & 1); |
| 1462 | } else { |
| 1463 | return !(((prune >> vtx_tab[tx_type]) & 1) | |
| 1464 | ((prune >> (htx_tab[tx_type] + 8)) & 1)); |
| 1465 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1468 | static void model_rd_from_sse(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1469 | const MACROBLOCKD *const xd, BLOCK_SIZE bsize, |
| 1470 | int plane, int64_t sse, int *rate, |
| 1471 | int64_t *dist) { |
| 1472 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1473 | const int dequant_shift = |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1474 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1475 | (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd - 5 : |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1476 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1477 | 3; |
| 1478 | |
| 1479 | // Fast approximate the modelling function. |
| 1480 | if (cpi->sf.simple_model_rd_from_var) { |
| 1481 | const int64_t square_error = sse; |
Monty Montgomery | 125c0fc | 2017-10-26 00:44:35 -0400 | [diff] [blame] | 1482 | int quantizer = (pd->dequant_Q3[1] >> dequant_shift); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1483 | |
| 1484 | if (quantizer < 120) |
| 1485 | *rate = (int)((square_error * (280 - quantizer)) >> |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1486 | (16 - AV1_PROB_COST_SHIFT)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1487 | else |
| 1488 | *rate = 0; |
| 1489 | *dist = (square_error * quantizer) >> 8; |
| 1490 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1491 | av1_model_rd_from_var_lapndz(sse, num_pels_log2_lookup[bsize], |
Monty Montgomery | 125c0fc | 2017-10-26 00:44:35 -0400 | [diff] [blame] | 1492 | pd->dequant_Q3[1] >> dequant_shift, rate, |
| 1493 | dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | *dist <<= 4; |
| 1497 | } |
| 1498 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1499 | 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] | 1500 | MACROBLOCK *x, MACROBLOCKD *xd, int plane_from, |
| 1501 | int plane_to, int *out_rate_sum, |
| 1502 | int64_t *out_dist_sum, int *skip_txfm_sb, |
| 1503 | int64_t *skip_sse_sb) { |
| 1504 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 1505 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 1506 | // we need to divide by 8 before sending to modeling function. |
| 1507 | int plane; |
| 1508 | const int ref = xd->mi[0]->mbmi.ref_frame[0]; |
| 1509 | |
| 1510 | int64_t rate_sum = 0; |
| 1511 | int64_t dist_sum = 0; |
| 1512 | int64_t total_sse = 0; |
| 1513 | |
| 1514 | x->pred_sse[ref] = 0; |
| 1515 | |
| 1516 | for (plane = plane_from; plane <= plane_to; ++plane) { |
| 1517 | struct macroblock_plane *const p = &x->plane[plane]; |
| 1518 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1519 | const BLOCK_SIZE bs = AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1520 | unsigned int sse; |
| 1521 | int rate; |
| 1522 | int64_t dist; |
| 1523 | |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1524 | if (x->skip_chroma_rd && plane) continue; |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1525 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1526 | // TODO(geza): Write direct sse functions that do not compute |
| 1527 | // variance as well. |
| 1528 | cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, |
| 1529 | &sse); |
| 1530 | |
| 1531 | if (plane == 0) x->pred_sse[ref] = sse; |
| 1532 | |
| 1533 | total_sse += sse; |
| 1534 | |
| 1535 | model_rd_from_sse(cpi, xd, bs, plane, sse, &rate, &dist); |
| 1536 | |
| 1537 | rate_sum += rate; |
| 1538 | dist_sum += dist; |
| 1539 | } |
| 1540 | |
| 1541 | *skip_txfm_sb = total_sse == 0; |
| 1542 | *skip_sse_sb = total_sse << 4; |
| 1543 | *out_rate_sum = (int)rate_sum; |
| 1544 | *out_dist_sum = dist_sum; |
| 1545 | } |
| 1546 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1547 | int64_t av1_block_error_c(const tran_low_t *coeff, const tran_low_t *dqcoeff, |
| 1548 | intptr_t block_size, int64_t *ssz) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1549 | int i; |
| 1550 | int64_t error = 0, sqcoeff = 0; |
| 1551 | |
| 1552 | for (i = 0; i < block_size; i++) { |
| 1553 | const int diff = coeff[i] - dqcoeff[i]; |
| 1554 | error += diff * diff; |
| 1555 | sqcoeff += coeff[i] * coeff[i]; |
| 1556 | } |
| 1557 | |
| 1558 | *ssz = sqcoeff; |
| 1559 | return error; |
| 1560 | } |
| 1561 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1562 | int64_t av1_block_error_fp_c(const int16_t *coeff, const int16_t *dqcoeff, |
| 1563 | int block_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1564 | int i; |
| 1565 | int64_t error = 0; |
| 1566 | |
| 1567 | for (i = 0; i < block_size; i++) { |
| 1568 | const int diff = coeff[i] - dqcoeff[i]; |
| 1569 | error += diff * diff; |
| 1570 | } |
| 1571 | |
| 1572 | return error; |
| 1573 | } |
| 1574 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1575 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1576 | int64_t av1_highbd_block_error_c(const tran_low_t *coeff, |
| 1577 | const tran_low_t *dqcoeff, intptr_t block_size, |
| 1578 | int64_t *ssz, int bd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1579 | int i; |
| 1580 | int64_t error = 0, sqcoeff = 0; |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 1581 | #if CONFIG_DAALA_TX |
| 1582 | (void)bd; |
| 1583 | int shift = 2 * (TX_COEFF_DEPTH - 11); |
| 1584 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1585 | int shift = 2 * (bd - 8); |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 1586 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1587 | int rounding = shift > 0 ? 1 << (shift - 1) : 0; |
| 1588 | |
| 1589 | for (i = 0; i < block_size; i++) { |
| 1590 | const int64_t diff = coeff[i] - dqcoeff[i]; |
| 1591 | error += diff * diff; |
| 1592 | sqcoeff += (int64_t)coeff[i] * (int64_t)coeff[i]; |
| 1593 | } |
| 1594 | assert(error >= 0 && sqcoeff >= 0); |
| 1595 | error = (error + rounding) >> shift; |
| 1596 | sqcoeff = (sqcoeff + rounding) >> shift; |
| 1597 | |
| 1598 | *ssz = sqcoeff; |
| 1599 | return error; |
| 1600 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1601 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1602 | |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1603 | #if !CONFIG_LV_MAP |
| 1604 | static int cost_coeffs(const AV1_COMMON *const cm, MACROBLOCK *x, int plane, |
| 1605 | int block, TX_SIZE tx_size, const SCAN_ORDER *scan_order, |
| 1606 | const ENTROPY_CONTEXT *a, const ENTROPY_CONTEXT *l, |
| 1607 | int use_fast_coef_costing) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1608 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1609 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 1610 | const struct macroblock_plane *p = &x->plane[plane]; |
| 1611 | const struct macroblockd_plane *pd = &xd->plane[plane]; |
| 1612 | const PLANE_TYPE type = pd->plane_type; |
| 1613 | const uint16_t *band_count = &band_count_table[tx_size][1]; |
| 1614 | const int eob = p->eobs[block]; |
| 1615 | const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block); |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 1616 | const TX_SIZE tx_size_ctx = get_txsize_entropy_ctx(tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1617 | uint8_t token_cache[MAX_TX_SQUARE]; |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 1618 | int pt = combine_entropy_contexts(*a, *l); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1619 | int c, cost; |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 1620 | const int16_t *scan = scan_order->scan; |
| 1621 | const int16_t *nb = scan_order->neighbors; |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 1622 | const int ref = is_inter_block(mbmi); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1623 | int(*head_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] = |
| 1624 | x->token_head_costs[tx_size_ctx][type][ref]; |
| 1625 | int(*tail_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] = |
| 1626 | x->token_tail_costs[tx_size_ctx][type][ref]; |
| 1627 | const int seg_eob = av1_get_tx_eob(&cm->seg, mbmi->segment_id, tx_size); |
Yaowu Xu | abe5215 | 2017-10-20 14:37:54 -0700 | [diff] [blame] | 1628 | int8_t eob_val; |
Thomas Davies | 1052575 | 2017-03-06 12:10:46 +0000 | [diff] [blame] | 1629 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1630 | #if CONFIG_HIGHBITDEPTH |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1631 | const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1632 | #else |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1633 | const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, 8); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1634 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1635 | |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 1636 | (void)cm; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1637 | |
| 1638 | if (eob == 0) { |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1639 | // block zero |
| 1640 | cost = (*head_token_costs)[pt][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1641 | } else { |
| 1642 | if (use_fast_coef_costing) { |
| 1643 | int band_left = *band_count++; |
| 1644 | |
| 1645 | // dc token |
| 1646 | int v = qcoeff[0]; |
| 1647 | int16_t prev_t; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1648 | cost = av1_get_token_cost(v, &prev_t, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1649 | eob_val = (eob == 1) ? EARLY_EOB : NO_EOB; |
| 1650 | cost += av1_get_coeff_token_cost( |
| 1651 | prev_t, eob_val, 1, (*head_token_costs)[pt], (*tail_token_costs)[pt]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1652 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1653 | token_cache[0] = av1_pt_energy_class[prev_t]; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1654 | ++head_token_costs; |
| 1655 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1656 | |
| 1657 | // ac tokens |
| 1658 | for (c = 1; c < eob; c++) { |
| 1659 | const int rc = scan[c]; |
| 1660 | int16_t t; |
| 1661 | |
| 1662 | v = qcoeff[rc]; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1663 | cost += av1_get_token_cost(v, &t, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1664 | eob_val = |
| 1665 | (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB; |
| 1666 | cost += av1_get_coeff_token_cost(t, eob_val, 0, |
| 1667 | (*head_token_costs)[!prev_t], |
| 1668 | (*tail_token_costs)[!prev_t]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1669 | prev_t = t; |
| 1670 | if (!--band_left) { |
| 1671 | band_left = *band_count++; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1672 | ++head_token_costs; |
| 1673 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1674 | } |
| 1675 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1676 | } else { // !use_fast_coef_costing |
| 1677 | int band_left = *band_count++; |
| 1678 | |
| 1679 | // dc token |
| 1680 | int v = qcoeff[0]; |
| 1681 | int16_t tok; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1682 | cost = av1_get_token_cost(v, &tok, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1683 | eob_val = (eob == 1) ? EARLY_EOB : NO_EOB; |
| 1684 | cost += av1_get_coeff_token_cost(tok, eob_val, 1, (*head_token_costs)[pt], |
| 1685 | (*tail_token_costs)[pt]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1686 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1687 | token_cache[0] = av1_pt_energy_class[tok]; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1688 | ++head_token_costs; |
| 1689 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1690 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1691 | // ac tokens |
| 1692 | for (c = 1; c < eob; c++) { |
| 1693 | const int rc = scan[c]; |
| 1694 | |
| 1695 | v = qcoeff[rc]; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1696 | cost += av1_get_token_cost(v, &tok, cat6_bits); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1697 | pt = get_coef_context(nb, token_cache, c); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1698 | eob_val = |
| 1699 | (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB; |
| 1700 | cost += av1_get_coeff_token_cost( |
| 1701 | tok, eob_val, 0, (*head_token_costs)[pt], (*tail_token_costs)[pt]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1702 | token_cache[rc] = av1_pt_energy_class[tok]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1703 | if (!--band_left) { |
| 1704 | band_left = *band_count++; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1705 | ++head_token_costs; |
| 1706 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1707 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1708 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1712 | return cost; |
| 1713 | } |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1714 | #endif // !CONFIG_LV_MAP |
| 1715 | |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 1716 | int av1_cost_coeffs(const AV1_COMP *const cpi, MACROBLOCK *x, int plane, |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 1717 | int blk_row, int blk_col, int block, TX_SIZE tx_size, |
| 1718 | const SCAN_ORDER *scan_order, const ENTROPY_CONTEXT *a, |
| 1719 | const ENTROPY_CONTEXT *l, int use_fast_coef_costing) { |
Angie Chiang | 3627de2 | 2017-08-18 20:15:59 -0700 | [diff] [blame] | 1720 | const AV1_COMMON *const cm = &cpi->common; |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1721 | #if !CONFIG_LV_MAP |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 1722 | (void)blk_row; |
| 1723 | (void)blk_col; |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1724 | return cost_coeffs(cm, x, plane, block, tx_size, scan_order, a, l, |
| 1725 | use_fast_coef_costing); |
Hui Su | 9fa9623 | 2017-10-23 15:46:04 -0700 | [diff] [blame] | 1726 | #else // !CONFIG_LV_MAP |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1727 | (void)scan_order; |
| 1728 | (void)use_fast_coef_costing; |
| 1729 | const MACROBLOCKD *xd = &x->e_mbd; |
| 1730 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 1731 | const struct macroblockd_plane *pd = &xd->plane[plane]; |
| 1732 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1733 | const BLOCK_SIZE plane_bsize = |
| 1734 | AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd)); |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1735 | TXB_CTX txb_ctx; |
| 1736 | get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx); |
Angie Chiang | 3627de2 | 2017-08-18 20:15:59 -0700 | [diff] [blame] | 1737 | return av1_cost_coeffs_txb(cm, x, plane, blk_row, blk_col, block, tx_size, |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 1738 | &txb_ctx); |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1739 | #endif // !CONFIG_LV_MAP |
| 1740 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1741 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1742 | // Get transform block visible dimensions cropped to the MI units. |
| 1743 | static void get_txb_dimensions(const MACROBLOCKD *xd, int plane, |
| 1744 | BLOCK_SIZE plane_bsize, int blk_row, int blk_col, |
| 1745 | BLOCK_SIZE tx_bsize, int *width, int *height, |
| 1746 | int *visible_width, int *visible_height) { |
| 1747 | assert(tx_bsize <= plane_bsize); |
| 1748 | int txb_height = block_size_high[tx_bsize]; |
| 1749 | int txb_width = block_size_wide[tx_bsize]; |
| 1750 | const int block_height = block_size_high[plane_bsize]; |
| 1751 | const int block_width = block_size_wide[plane_bsize]; |
| 1752 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1753 | // TODO(aconverse@google.com): Investigate using crop_width/height here rather |
| 1754 | // than the MI size |
| 1755 | const int block_rows = |
| 1756 | (xd->mb_to_bottom_edge >= 0) |
| 1757 | ? block_height |
| 1758 | : (xd->mb_to_bottom_edge >> (3 + pd->subsampling_y)) + block_height; |
| 1759 | const int block_cols = |
| 1760 | (xd->mb_to_right_edge >= 0) |
| 1761 | ? block_width |
| 1762 | : (xd->mb_to_right_edge >> (3 + pd->subsampling_x)) + block_width; |
| 1763 | const int tx_unit_size = tx_size_wide_log2[0]; |
| 1764 | if (width) *width = txb_width; |
| 1765 | if (height) *height = txb_height; |
| 1766 | *visible_width = clamp(block_cols - (blk_col << tx_unit_size), 0, txb_width); |
| 1767 | *visible_height = |
| 1768 | clamp(block_rows - (blk_row << tx_unit_size), 0, txb_height); |
| 1769 | } |
| 1770 | |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1771 | // Compute the pixel domain distortion from src and dst on all visible 4x4s in |
| 1772 | // the |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1773 | // transform block. |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1774 | static unsigned pixel_dist(const AV1_COMP *const cpi, const MACROBLOCK *x, |
| 1775 | int plane, const uint8_t *src, const int src_stride, |
| 1776 | const uint8_t *dst, const int dst_stride, |
| 1777 | int blk_row, int blk_col, |
| 1778 | const BLOCK_SIZE plane_bsize, |
| 1779 | const BLOCK_SIZE tx_bsize) { |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1780 | int txb_rows, txb_cols, visible_rows, visible_cols; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1781 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1782 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1783 | get_txb_dimensions(xd, plane, plane_bsize, blk_row, blk_col, tx_bsize, |
| 1784 | &txb_cols, &txb_rows, &visible_cols, &visible_rows); |
| 1785 | assert(visible_rows > 0); |
| 1786 | assert(visible_cols > 0); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1787 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1788 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1789 | if (x->using_dist_8x8 && plane == 0 && txb_cols >= 8 && txb_rows >= 8) |
Yushin Cho | fcddadf | 2017-08-30 13:49:38 -0700 | [diff] [blame] | 1790 | return (unsigned)av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride, |
| 1791 | tx_bsize, txb_cols, txb_rows, visible_cols, |
| 1792 | visible_rows, x->qindex); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1793 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1794 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 1795 | unsigned sse = pixel_dist_visible_only(cpi, x, src, src_stride, dst, |
| 1796 | dst_stride, tx_bsize, txb_rows, |
| 1797 | txb_cols, visible_rows, visible_cols); |
| 1798 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1799 | return sse; |
| 1800 | } |
| 1801 | |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1802 | // Compute the pixel domain distortion from diff on all visible 4x4s in the |
| 1803 | // transform block. |
| 1804 | static int64_t pixel_diff_dist(const MACROBLOCK *x, int plane, |
| 1805 | const int16_t *diff, const int diff_stride, |
| 1806 | int blk_row, int blk_col, |
| 1807 | const BLOCK_SIZE plane_bsize, |
| 1808 | const BLOCK_SIZE tx_bsize) { |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1809 | int visible_rows, visible_cols; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1810 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1811 | #if CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1812 | int txb_height = block_size_high[tx_bsize]; |
| 1813 | int txb_width = block_size_wide[tx_bsize]; |
| 1814 | const int src_stride = x->plane[plane].src.stride; |
| 1815 | const int src_idx = (blk_row * src_stride + blk_col) << tx_size_wide_log2[0]; |
| 1816 | const uint8_t *src = &x->plane[plane].src.buf[src_idx]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1817 | #endif |
| 1818 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1819 | get_txb_dimensions(xd, plane, plane_bsize, blk_row, blk_col, tx_bsize, NULL, |
| 1820 | NULL, &visible_cols, &visible_rows); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1821 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1822 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1823 | if (x->using_dist_8x8 && plane == 0 && txb_width >= 8 && txb_height >= 8) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1824 | return av1_dist_8x8_diff(x, src, src_stride, diff, diff_stride, txb_width, |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1825 | txb_height, visible_cols, visible_rows, x->qindex); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1826 | else |
| 1827 | #endif |
| 1828 | return aom_sum_squares_2d_i16(diff, diff_stride, visible_cols, |
| 1829 | visible_rows); |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 1832 | int av1_count_colors(const uint8_t *src, int stride, int rows, int cols, |
| 1833 | int *val_count) { |
| 1834 | const int max_pix_val = 1 << 8; |
| 1835 | memset(val_count, 0, max_pix_val * sizeof(val_count[0])); |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1836 | for (int r = 0; r < rows; ++r) { |
| 1837 | for (int c = 0; c < cols; ++c) { |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 1838 | const int this_val = src[r * stride + c]; |
| 1839 | assert(this_val < max_pix_val); |
| 1840 | ++val_count[this_val]; |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1841 | } |
| 1842 | } |
| 1843 | int n = 0; |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 1844 | for (int i = 0; i < max_pix_val; ++i) { |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1845 | if (val_count[i]) ++n; |
| 1846 | } |
| 1847 | return n; |
| 1848 | } |
| 1849 | |
| 1850 | #if CONFIG_HIGHBITDEPTH |
| 1851 | int av1_count_colors_highbd(const uint8_t *src8, int stride, int rows, int cols, |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 1852 | int bit_depth, int *val_count) { |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1853 | assert(bit_depth <= 12); |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 1854 | const int max_pix_val = 1 << bit_depth; |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1855 | const uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 1856 | memset(val_count, 0, max_pix_val * sizeof(val_count[0])); |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1857 | for (int r = 0; r < rows; ++r) { |
| 1858 | for (int c = 0; c < cols; ++c) { |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 1859 | const int this_val = src[r * stride + c]; |
| 1860 | assert(this_val < max_pix_val); |
| 1861 | ++val_count[this_val]; |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | int n = 0; |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 1865 | for (int i = 0; i < max_pix_val; ++i) { |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1866 | if (val_count[i]) ++n; |
| 1867 | } |
| 1868 | return n; |
| 1869 | } |
| 1870 | #endif // CONFIG_HIGHBITDEPTH |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1871 | |
Sebastien Alaiwan | 95137bd | 2017-11-13 14:47:37 +0100 | [diff] [blame] | 1872 | void av1_inverse_transform_block_facade(MACROBLOCKD *xd, int plane, int block, |
Frederic Barbier | 33b39f0 | 2017-11-21 11:11:24 +0100 | [diff] [blame] | 1873 | int blk_row, int blk_col, int eob, |
| 1874 | int reduced_tx_set) { |
Sebastien Alaiwan | 95137bd | 2017-11-13 14:47:37 +0100 | [diff] [blame] | 1875 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1876 | tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
Sebastien Alaiwan | 95137bd | 2017-11-13 14:47:37 +0100 | [diff] [blame] | 1877 | const PLANE_TYPE plane_type = get_plane_type(plane); |
| 1878 | const TX_SIZE tx_size = av1_get_tx_size(plane, xd); |
| 1879 | const TX_TYPE tx_type = |
| 1880 | av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size); |
| 1881 | const int dst_stride = pd->dst.stride; |
| 1882 | uint8_t *dst = |
| 1883 | &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]]; |
Sebastien Alaiwan | 9f001f3 | 2017-11-28 16:32:33 +0100 | [diff] [blame] | 1884 | av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst, |
| 1885 | dst_stride, eob, reduced_tx_set); |
Sebastien Alaiwan | 95137bd | 2017-11-13 14:47:37 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 1888 | void av1_dist_block(const AV1_COMP *cpi, MACROBLOCK *x, int plane, |
| 1889 | BLOCK_SIZE plane_bsize, int block, int blk_row, int blk_col, |
| 1890 | TX_SIZE tx_size, int64_t *out_dist, int64_t *out_sse, |
| 1891 | OUTPUT_STATUS output_status) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1892 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1893 | const struct macroblock_plane *const p = &x->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1894 | #if CONFIG_DIST_8X8 |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 1895 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1896 | #else // CONFIG_DIST_8X8 |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 1897 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1898 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 1899 | |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1900 | if (cpi->sf.use_transform_domain_distortion |
| 1901 | #if CONFIG_DIST_8X8 |
| 1902 | && !x->using_dist_8x8 |
| 1903 | #endif |
| 1904 | ) { |
hui su | d2f12ba | 2017-04-12 10:08:43 -0700 | [diff] [blame] | 1905 | // Transform domain distortion computation is more efficient as it does |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1906 | // not involve an inverse transform, but it is less accurate. |
Urvang Joshi | 8089315 | 2017-10-27 11:51:14 -0700 | [diff] [blame] | 1907 | const int buffer_length = av1_get_max_eob(tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1908 | int64_t this_sse; |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 1909 | // TX-domain results need to shift down to Q2/D10 to match pixel |
| 1910 | // domain distortion values which are in Q2^2 |
| 1911 | #if CONFIG_DAALA_TX |
| 1912 | int shift = (TX_COEFF_DEPTH - 10) * 2; |
| 1913 | #else |
Jingning Han | ff70545 | 2017-04-27 11:32:15 -0700 | [diff] [blame] | 1914 | int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 1915 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1916 | tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
| 1917 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1918 | |
Monty Montgomery | a26262c | 2017-10-31 07:32:13 -0400 | [diff] [blame] | 1919 | #if CONFIG_DAALA_TX |
| 1920 | *out_dist = av1_highbd_block_error(coeff, dqcoeff, buffer_length, &this_sse, |
| 1921 | xd->bd); |
| 1922 | #else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1923 | #if CONFIG_HIGHBITDEPTH |
Yi Luo | d61e608 | 2017-05-26 16:14:39 -0700 | [diff] [blame] | 1924 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 1925 | *out_dist = av1_highbd_block_error(coeff, dqcoeff, buffer_length, |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1926 | &this_sse, xd->bd); |
Yi Luo | d61e608 | 2017-05-26 16:14:39 -0700 | [diff] [blame] | 1927 | else |
| 1928 | #endif |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1929 | *out_dist = av1_block_error(coeff, dqcoeff, buffer_length, &this_sse); |
Monty Montgomery | a26262c | 2017-10-31 07:32:13 -0400 | [diff] [blame] | 1930 | #endif |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 1931 | |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1932 | *out_dist = RIGHT_SIGNED_SHIFT(*out_dist, shift); |
| 1933 | *out_sse = RIGHT_SIGNED_SHIFT(this_sse, shift); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1934 | } else { |
| 1935 | const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size]; |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1936 | const int bsw = block_size_wide[tx_bsize]; |
| 1937 | const int bsh = block_size_high[tx_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1938 | const int src_stride = x->plane[plane].src.stride; |
| 1939 | const int dst_stride = xd->plane[plane].dst.stride; |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1940 | // Scale the transform block index to pixel unit. |
| 1941 | const int src_idx = (blk_row * src_stride + blk_col) |
| 1942 | << tx_size_wide_log2[0]; |
| 1943 | const int dst_idx = (blk_row * dst_stride + blk_col) |
| 1944 | << tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1945 | const uint8_t *src = &x->plane[plane].src.buf[src_idx]; |
| 1946 | const uint8_t *dst = &xd->plane[plane].dst.buf[dst_idx]; |
| 1947 | const tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 1948 | const uint16_t eob = p->eobs[block]; |
| 1949 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1950 | assert(cpi != NULL); |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1951 | assert(tx_size_wide_log2[0] == tx_size_high_log2[0]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1952 | |
Angie Chiang | c0cf6c0 | 2017-04-04 13:47:27 -0700 | [diff] [blame] | 1953 | { |
| 1954 | const int diff_stride = block_size_wide[plane_bsize]; |
| 1955 | const int diff_idx = (blk_row * diff_stride + blk_col) |
| 1956 | << tx_size_wide_log2[0]; |
| 1957 | const int16_t *diff = &p->src_diff[diff_idx]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1958 | *out_sse = pixel_diff_dist(x, plane, diff, diff_stride, blk_row, blk_col, |
| 1959 | plane_bsize, tx_bsize); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1960 | #if CONFIG_HIGHBITDEPTH |
hui su | b1cc1f9 | 2017-04-11 17:41:29 -0700 | [diff] [blame] | 1961 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 1962 | *out_sse = ROUND_POWER_OF_TWO(*out_sse, (xd->bd - 8) * 2); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1963 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | c0cf6c0 | 2017-04-04 13:47:27 -0700 | [diff] [blame] | 1964 | } |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 1965 | *out_sse *= 16; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1966 | |
| 1967 | if (eob) { |
Angie Chiang | 228cc18 | 2017-04-07 15:22:16 -0700 | [diff] [blame] | 1968 | if (output_status == OUTPUT_HAS_DECODED_PIXELS) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1969 | *out_dist = pixel_dist(cpi, x, plane, src, src_stride, dst, dst_stride, |
| 1970 | blk_row, blk_col, plane_bsize, tx_bsize); |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1971 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1972 | #if CONFIG_HIGHBITDEPTH |
Jingning Han | 6a9dbef | 2017-04-10 10:25:14 -0700 | [diff] [blame] | 1973 | uint8_t *recon; |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1974 | DECLARE_ALIGNED(16, uint16_t, recon16[MAX_TX_SQUARE]); |
Jingning Han | 6a9dbef | 2017-04-10 10:25:14 -0700 | [diff] [blame] | 1975 | |
| 1976 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 1977 | recon = CONVERT_TO_BYTEPTR(recon16); |
| 1978 | else |
| 1979 | recon = (uint8_t *)recon16; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1980 | #else |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1981 | DECLARE_ALIGNED(16, uint8_t, recon[MAX_TX_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1982 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1983 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1984 | #if CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1985 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1986 | aom_highbd_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, |
| 1987 | NULL, 0, bsw, bsh, xd->bd); |
| 1988 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1989 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1990 | aom_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, NULL, |
| 1991 | 0, bsw, bsh); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1992 | #if CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1993 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1994 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 1995 | |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1996 | const PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 1997 | TX_TYPE tx_type = |
| 1998 | av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size); |
Sebastien Alaiwan | 9f001f3 | 2017-11-28 16:32:33 +0100 | [diff] [blame] | 1999 | av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, recon, |
| 2000 | MAX_TX_SIZE, eob, |
| 2001 | cpi->common.reduced_tx_set_used); |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 2002 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2003 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 2004 | if (x->using_dist_8x8 && plane == 0 && (bsw < 8 || bsh < 8)) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 2005 | // Save decoded pixels for inter block in pd->pred to avoid |
| 2006 | // block_8x8_rd_txfm_daala_dist() need to produce them |
| 2007 | // by calling av1_inverse_transform_block() again. |
| 2008 | const int pred_stride = block_size_wide[plane_bsize]; |
| 2009 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 2010 | << tx_size_wide_log2[0]; |
| 2011 | int16_t *pred = &pd->pred[pred_idx]; |
| 2012 | int i, j; |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 2013 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 2014 | #if CONFIG_HIGHBITDEPTH |
| 2015 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 2016 | for (j = 0; j < bsh; j++) |
| 2017 | for (i = 0; i < bsw; i++) |
| 2018 | pred[j * pred_stride + i] = |
| 2019 | CONVERT_TO_SHORTPTR(recon)[j * MAX_TX_SIZE + i]; |
| 2020 | } else { |
| 2021 | #endif |
| 2022 | for (j = 0; j < bsh; j++) |
| 2023 | for (i = 0; i < bsw; i++) |
| 2024 | pred[j * pred_stride + i] = recon[j * MAX_TX_SIZE + i]; |
| 2025 | #if CONFIG_HIGHBITDEPTH |
| 2026 | } |
| 2027 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 2028 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2029 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 2030 | *out_dist = |
| 2031 | pixel_dist(cpi, x, plane, src, src_stride, recon, MAX_TX_SIZE, |
| 2032 | blk_row, blk_col, plane_bsize, tx_bsize); |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 2033 | } |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 2034 | *out_dist *= 16; |
| 2035 | } else { |
| 2036 | *out_dist = *out_sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2037 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2041 | static void block_rd_txfm(int plane, int block, int blk_row, int blk_col, |
| 2042 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) { |
| 2043 | struct rdcost_block_args *args = arg; |
| 2044 | MACROBLOCK *const x = args->x; |
| 2045 | MACROBLOCKD *const xd = &x->e_mbd; |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2046 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | e50f3ec | 2017-04-10 15:50:33 -0700 | [diff] [blame] | 2047 | const AV1_COMP *cpi = args->cpi; |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2048 | ENTROPY_CONTEXT *a = args->t_above + blk_col; |
| 2049 | ENTROPY_CONTEXT *l = args->t_left + blk_row; |
Angie Chiang | 18ad894 | 2017-04-11 12:37:07 -0700 | [diff] [blame] | 2050 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2051 | int64_t rd1, rd2, rd; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2052 | RD_STATS this_rd_stats; |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2053 | |
| 2054 | #if CONFIG_DIST_8X8 |
| 2055 | // If sub8x8 tx, 8x8 or larger partition, and luma channel, |
| 2056 | // dist-8x8 disables early skip, because the distortion metrics for |
| 2057 | // sub8x8 tx (MSE) and reference distortion from 8x8 or larger partition |
| 2058 | // (new distortion metric) are different. |
| 2059 | // Exception is: dist-8x8 is enabled but still MSE is used, |
| 2060 | // i.e. "--tune=" encoder option is not used. |
Yushin Cho | f9970a5 | 2017-10-13 12:57:13 -0700 | [diff] [blame] | 2061 | int bw = block_size_wide[plane_bsize]; |
| 2062 | int bh = block_size_high[plane_bsize]; |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2063 | int disable_early_skip = |
Yushin Cho | f9970a5 | 2017-10-13 12:57:13 -0700 | [diff] [blame] | 2064 | x->using_dist_8x8 && plane == 0 && bw >= 8 && bh >= 8 && |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2065 | (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4) && |
| 2066 | x->tune_metric != AOM_TUNE_PSNR; |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 2067 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 6341f5c | 2017-03-24 14:36:28 -0700 | [diff] [blame] | 2068 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2069 | av1_init_rd_stats(&this_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2070 | |
| 2071 | if (args->exit_early) return; |
| 2072 | |
| 2073 | if (!is_inter_block(mbmi)) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 2074 | av1_predict_intra_block_facade(cm, xd, plane, block, blk_col, blk_row, |
| 2075 | tx_size); |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 2076 | av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size); |
| 2077 | } |
| 2078 | |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2079 | #if !CONFIG_TXK_SEL |
Sarah Parker | e8d0d4c | 2017-12-06 15:11:37 -0800 | [diff] [blame] | 2080 | // full forward transform and quantization |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 2081 | #if DISABLE_TRELLISQ_SEARCH |
| 2082 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Sarah Parker | e8d0d4c | 2017-12-06 15:11:37 -0800 | [diff] [blame] | 2083 | AV1_XFORM_QUANT_B); |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 2084 | #else |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 2085 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Sarah Parker | e8d0d4c | 2017-12-06 15:11:37 -0800 | [diff] [blame] | 2086 | AV1_XFORM_QUANT_FP); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2087 | |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 2088 | // TX-domain results need to shift down to Q2/D10 to match pixel |
| 2089 | // domain distortion values which are in Q2^2 |
| 2090 | #if CONFIG_DAALA_TX |
| 2091 | const int shift = (TX_COEFF_DEPTH - 10) * 2; |
| 2092 | #else |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2093 | const int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 2094 | #endif |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2095 | tran_low_t *const coeff = BLOCK_OFFSET(x->plane[plane].coeff, block); |
| 2096 | tran_low_t *const dqcoeff = BLOCK_OFFSET(xd->plane[plane].dqcoeff, block); |
Urvang Joshi | 8089315 | 2017-10-27 11:51:14 -0700 | [diff] [blame] | 2097 | const int buffer_length = av1_get_max_eob(tx_size); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2098 | int64_t tmp_dist; |
| 2099 | int64_t tmp; |
Monty Montgomery | a26262c | 2017-10-31 07:32:13 -0400 | [diff] [blame] | 2100 | #if CONFIG_DAALA_TX |
| 2101 | tmp_dist = |
| 2102 | av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp, xd->bd); |
| 2103 | #else |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2104 | #if CONFIG_HIGHBITDEPTH |
| 2105 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 2106 | tmp_dist = |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 2107 | av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp, xd->bd); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2108 | else |
| 2109 | #endif |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 2110 | tmp_dist = av1_block_error(coeff, dqcoeff, buffer_length, &tmp); |
Monty Montgomery | a26262c | 2017-10-31 07:32:13 -0400 | [diff] [blame] | 2111 | #endif |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 2112 | tmp_dist = RIGHT_SIGNED_SHIFT(tmp_dist, shift); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2113 | |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 2114 | if ( |
| 2115 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2116 | disable_early_skip || |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 2117 | #endif |
| 2118 | RDCOST(x->rdmult, 0, tmp_dist) + args->this_rd < args->best_rd) { |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2119 | av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size, |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 2120 | a, l, CONFIG_LV_MAP); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2121 | } else { |
| 2122 | args->exit_early = 1; |
| 2123 | return; |
| 2124 | } |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 2125 | #endif // DISABLE_TRELLISQ_SEARCH |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 2126 | |
| 2127 | if (!is_inter_block(mbmi)) { |
| 2128 | struct macroblock_plane *const p = &x->plane[plane]; |
Angie Chiang | bc2288c | 2017-04-09 15:41:17 -0700 | [diff] [blame] | 2129 | av1_inverse_transform_block_facade(xd, plane, block, blk_row, blk_col, |
Frederic Barbier | 33b39f0 | 2017-11-21 11:11:24 +0100 | [diff] [blame] | 2130 | p->eobs[block], cm->reduced_tx_set_used); |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 2131 | av1_dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col, |
| 2132 | tx_size, &this_rd_stats.dist, &this_rd_stats.sse, |
| 2133 | OUTPUT_HAS_DECODED_PIXELS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2134 | } else { |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 2135 | av1_dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col, |
| 2136 | tx_size, &this_rd_stats.dist, &this_rd_stats.sse, |
| 2137 | OUTPUT_HAS_PREDICTED_PIXELS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2138 | } |
Luc Trudeau | c7af36d | 2017-10-11 21:01:00 -0400 | [diff] [blame] | 2139 | rd = RDCOST(x->rdmult, 0, this_rd_stats.dist); |
| 2140 | if (args->this_rd + rd > args->best_rd) { |
| 2141 | args->exit_early = 1; |
| 2142 | return; |
| 2143 | } |
Luc Trudeau | aa94baa | 2017-04-27 10:52:21 -0400 | [diff] [blame] | 2144 | #if CONFIG_CFL |
Luc Trudeau | a8474b6 | 2017-12-07 12:13:45 -0500 | [diff] [blame] | 2145 | if (plane == AOM_PLANE_Y && xd->cfl.store_y && is_cfl_allowed(mbmi)) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 2146 | assert(!is_inter_block(mbmi) || plane_bsize < BLOCK_8X8); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 2147 | cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize); |
Luc Trudeau | aa94baa | 2017-04-27 10:52:21 -0400 | [diff] [blame] | 2148 | } |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 2149 | #endif // CONFIG_CFL |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2150 | const PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2151 | const TX_TYPE tx_type = |
| 2152 | av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size); |
Debargha Mukherjee | d2cfbef | 2017-12-03 16:15:27 -0800 | [diff] [blame] | 2153 | |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 2154 | const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, mbmi); |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 2155 | this_rd_stats.rate = |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 2156 | av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block, tx_size, |
| 2157 | scan_order, a, l, args->use_fast_coef_costing); |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2158 | #else // !CONFIG_TXK_SEL |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2159 | av1_search_txk_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize, |
| 2160 | tx_size, a, l, args->use_fast_coef_costing, |
| 2161 | &this_rd_stats); |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2162 | #endif // !CONFIG_TXK_SEL |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2163 | |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2164 | #if CONFIG_RD_DEBUG |
Angie Chiang | e94556b | 2016-11-09 10:59:30 -0800 | [diff] [blame] | 2165 | av1_update_txb_coeff_cost(&this_rd_stats, plane, tx_size, blk_row, blk_col, |
| 2166 | this_rd_stats.rate); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2167 | #endif // CONFIG_RD_DEBUG |
Yushin Cho | 6341f5c | 2017-03-24 14:36:28 -0700 | [diff] [blame] | 2168 | av1_set_txb_context(x, plane, block, tx_size, a, l); |
Angie Chiang | b3a12b5 | 2017-03-23 14:53:10 -0700 | [diff] [blame] | 2169 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2170 | rd1 = RDCOST(x->rdmult, this_rd_stats.rate, this_rd_stats.dist); |
| 2171 | rd2 = RDCOST(x->rdmult, 0, this_rd_stats.sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2172 | |
| 2173 | // TODO(jingning): temporarily enabled only for luma component |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2174 | rd = AOMMIN(rd1, rd2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2175 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2176 | this_rd_stats.skip &= !x->plane[plane].eobs[block]; |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 2177 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2178 | av1_merge_rd_stats(&args->rd_stats, &this_rd_stats); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2179 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2180 | args->this_rd += rd; |
| 2181 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2182 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2183 | if (!disable_early_skip) |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2184 | #endif |
| 2185 | if (args->this_rd > args->best_rd) { |
| 2186 | args->exit_early = 1; |
| 2187 | return; |
| 2188 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2191 | #if CONFIG_DIST_8X8 |
| 2192 | static void dist_8x8_sub8x8_txfm_rd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2193 | BLOCK_SIZE bsize, |
| 2194 | struct rdcost_block_args *args) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2195 | MACROBLOCKD *const xd = &x->e_mbd; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2196 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 2197 | const struct macroblock_plane *const p = &x->plane[0]; |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 2198 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2199 | const int src_stride = p->src.stride; |
| 2200 | const int dst_stride = pd->dst.stride; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2201 | const uint8_t *src = &p->src.buf[0]; |
| 2202 | const uint8_t *dst = &pd->dst.buf[0]; |
| 2203 | const int16_t *pred = &pd->pred[0]; |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 2204 | int bw = block_size_wide[bsize]; |
| 2205 | int bh = block_size_high[bsize]; |
| 2206 | int visible_w = bw; |
| 2207 | int visible_h = bh; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2208 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2209 | int i, j; |
| 2210 | int64_t rd, rd1, rd2; |
Yushin Cho | f0049ba | 2017-11-20 15:22:43 -0800 | [diff] [blame] | 2211 | int64_t sse = INT64_MAX, dist = INT64_MAX; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2212 | int qindex = x->qindex; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2213 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2214 | assert((bw & 0x07) == 0); |
| 2215 | assert((bh & 0x07) == 0); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2216 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 2217 | get_txb_dimensions(xd, 0, bsize, 0, 0, bsize, &bw, &bh, &visible_w, |
| 2218 | &visible_h); |
| 2219 | |
Yushin Cho | f0049ba | 2017-11-20 15:22:43 -0800 | [diff] [blame] | 2220 | const int diff_stride = block_size_wide[bsize]; |
| 2221 | const int16_t *diff = p->src_diff; |
| 2222 | sse = av1_dist_8x8_diff(x, src, src_stride, diff, diff_stride, bw, bh, |
| 2223 | visible_w, visible_h, qindex); |
| 2224 | sse = ROUND_POWER_OF_TWO(sse, (xd->bd - 8) * 2); |
| 2225 | sse *= 16; |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 2226 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2227 | if (!is_inter_block(mbmi)) { |
Yushin Cho | f0049ba | 2017-11-20 15:22:43 -0800 | [diff] [blame] | 2228 | dist = av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride, bsize, bw, bh, |
| 2229 | visible_w, visible_h, qindex); |
| 2230 | dist *= 16; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2231 | } else { |
Yushin Cho | f0049ba | 2017-11-20 15:22:43 -0800 | [diff] [blame] | 2232 | // For inter mode, the decoded pixels are provided in pd->pred, |
| 2233 | // while the predicted pixels are in dst. |
| 2234 | #if CONFIG_HIGHBITDEPTH |
| 2235 | uint8_t *pred8; |
| 2236 | DECLARE_ALIGNED(16, uint16_t, pred16[MAX_SB_SQUARE]); |
| 2237 | |
| 2238 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 2239 | pred8 = CONVERT_TO_BYTEPTR(pred16); |
| 2240 | else |
| 2241 | pred8 = (uint8_t *)pred16; |
| 2242 | #else |
| 2243 | DECLARE_ALIGNED(16, uint8_t, pred8[MAX_SB_SQUARE]); |
| 2244 | #endif // CONFIG_HIGHBITDEPTH |
| 2245 | |
| 2246 | #if CONFIG_HIGHBITDEPTH |
| 2247 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 2248 | for (j = 0; j < bh; j++) |
| 2249 | for (i = 0; i < bw; i++) |
| 2250 | CONVERT_TO_SHORTPTR(pred8)[j * bw + i] = pred[j * bw + i]; |
| 2251 | } else { |
| 2252 | #endif |
| 2253 | for (j = 0; j < bh; j++) |
| 2254 | for (i = 0; i < bw; i++) pred8[j * bw + i] = (uint8_t)pred[j * bw + i]; |
| 2255 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 2256 | } |
Yushin Cho | f0049ba | 2017-11-20 15:22:43 -0800 | [diff] [blame] | 2257 | #endif // CONFIG_HIGHBITDEPTH |
| 2258 | |
| 2259 | dist = av1_dist_8x8(cpi, x, src, src_stride, pred8, bw, bsize, bw, bh, |
| 2260 | visible_w, visible_h, qindex); |
| 2261 | dist *= 16; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2262 | } |
| 2263 | |
Yushin Cho | f0049ba | 2017-11-20 15:22:43 -0800 | [diff] [blame] | 2264 | if (x->tune_metric == AOM_TUNE_PSNR && xd->bd == 8) { |
| 2265 | assert(args->rd_stats.sse == sse); |
| 2266 | assert(args->rd_stats.dist == dist); |
| 2267 | } |
| 2268 | args->rd_stats.sse = sse; |
| 2269 | args->rd_stats.dist = dist; |
| 2270 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2271 | rd1 = RDCOST(x->rdmult, args->rd_stats.rate, args->rd_stats.dist); |
| 2272 | rd2 = RDCOST(x->rdmult, 0, args->rd_stats.sse); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2273 | rd = AOMMIN(rd1, rd2); |
| 2274 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2275 | args->rd_stats.rdcost = rd; |
| 2276 | args->this_rd = rd; |
Yushin Cho | 04eb959 | 2017-06-21 17:35:06 -0700 | [diff] [blame] | 2277 | |
| 2278 | if (args->this_rd > args->best_rd) args->exit_early = 1; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2279 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2280 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2281 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2282 | static void txfm_rd_in_plane(MACROBLOCK *x, const AV1_COMP *cpi, |
| 2283 | RD_STATS *rd_stats, int64_t ref_best_rd, int plane, |
| 2284 | BLOCK_SIZE bsize, TX_SIZE tx_size, |
| 2285 | int use_fast_coef_casting) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2286 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2287 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2288 | struct rdcost_block_args args; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2289 | av1_zero(args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2290 | args.x = x; |
| 2291 | args.cpi = cpi; |
| 2292 | args.best_rd = ref_best_rd; |
| 2293 | args.use_fast_coef_costing = use_fast_coef_casting; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2294 | av1_init_rd_stats(&args.rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2295 | |
| 2296 | if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size; |
| 2297 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2298 | 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] | 2299 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2300 | av1_foreach_transformed_block_in_plane(xd, bsize, plane, block_rd_txfm, |
| 2301 | &args); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2302 | #if CONFIG_DIST_8X8 |
Yushin Cho | f9970a5 | 2017-10-13 12:57:13 -0700 | [diff] [blame] | 2303 | int bw = block_size_wide[bsize]; |
| 2304 | int bh = block_size_high[bsize]; |
| 2305 | |
| 2306 | if (x->using_dist_8x8 && !args.exit_early && plane == 0 && bw >= 8 && |
| 2307 | bh >= 8 && (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4)) |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2308 | dist_8x8_sub8x8_txfm_rd(cpi, x, bsize, &args); |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2309 | #endif |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2310 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2311 | if (args.exit_early) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2312 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2313 | } else { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2314 | *rd_stats = args.rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2315 | } |
| 2316 | } |
| 2317 | |
Yue Chen | 3dd03e3 | 2017-10-17 15:39:52 -0700 | [diff] [blame] | 2318 | static int tx_size_cost(const AV1_COMMON *const cm, const MACROBLOCK *const x, |
Urvang Joshi | ab8840e | 2017-10-06 16:38:24 -0700 | [diff] [blame] | 2319 | BLOCK_SIZE bsize, TX_SIZE tx_size) { |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2320 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 2321 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2322 | |
Rupert Swarbrick | fcff0b2 | 2017-10-05 09:26:04 +0100 | [diff] [blame] | 2323 | if (cm->tx_mode == TX_MODE_SELECT && block_signals_txsize(mbmi->sb_type)) { |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2324 | const int is_inter = is_inter_block(mbmi); |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 2325 | const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize, is_inter); |
Debargha Mukherjee | 0fa057f | 2017-12-06 17:06:29 -0800 | [diff] [blame] | 2326 | const int depth = tx_size_to_depth(tx_size, bsize, is_inter); |
Urvang Joshi | ab8840e | 2017-10-06 16:38:24 -0700 | [diff] [blame] | 2327 | const int tx_size_ctx = get_tx_size_context(xd); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2328 | int r_tx_size = x->tx_size_cost[tx_size_cat][tx_size_ctx][depth]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2329 | return r_tx_size; |
| 2330 | } else { |
| 2331 | return 0; |
| 2332 | } |
| 2333 | } |
| 2334 | |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2335 | // TODO(angiebird): use this function whenever it's possible |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2336 | int av1_tx_type_cost(const AV1_COMMON *cm, const MACROBLOCK *x, |
| 2337 | const MACROBLOCKD *xd, BLOCK_SIZE bsize, int plane, |
| 2338 | TX_SIZE tx_size, TX_TYPE tx_type) { |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 2339 | if (plane > 0) return 0; |
| 2340 | |
Jingning Han | 243b66b | 2017-06-23 12:11:47 -0700 | [diff] [blame] | 2341 | tx_size = get_min_tx_size(tx_size); |
Jingning Han | 243b66b | 2017-06-23 12:11:47 -0700 | [diff] [blame] | 2342 | |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2343 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 2344 | const int is_inter = is_inter_block(mbmi); |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2345 | if (get_ext_tx_types(tx_size, bsize, is_inter, cm->reduced_tx_set_used) > 1 && |
| 2346 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
| 2347 | const int ext_tx_set = |
| 2348 | get_ext_tx_set(tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
| 2349 | if (is_inter) { |
| 2350 | if (ext_tx_set > 0) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2351 | return x |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2352 | ->inter_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]][tx_type]; |
| 2353 | } else { |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 2354 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) { |
| 2355 | #if CONFIG_FILTER_INTRA |
| 2356 | PREDICTION_MODE intra_dir; |
| 2357 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) |
| 2358 | intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info |
| 2359 | .filter_intra_mode[0]]; |
| 2360 | else |
| 2361 | intra_dir = mbmi->mode; |
| 2362 | return x->intra_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]] |
| 2363 | [intra_dir][tx_type]; |
| 2364 | #else |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2365 | return x->intra_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]] |
| 2366 | [mbmi->mode][tx_type]; |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 2367 | #endif |
| 2368 | } |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2369 | } |
| 2370 | } |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2371 | return 0; |
| 2372 | } |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2373 | static int64_t txfm_yrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2374 | RD_STATS *rd_stats, int64_t ref_best_rd, BLOCK_SIZE bs, |
Urvang Joshi | 9752a2e | 2017-10-02 17:32:27 -0700 | [diff] [blame] | 2375 | TX_TYPE tx_type, TX_SIZE tx_size) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2376 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2377 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2378 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2379 | int64_t rd = INT64_MAX; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 2380 | const int skip_ctx = av1_get_skip_context(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2381 | int s0, s1; |
| 2382 | const int is_inter = is_inter_block(mbmi); |
Jingning Han | bf9c6b7 | 2016-12-14 14:50:45 -0800 | [diff] [blame] | 2383 | const int tx_select = |
| 2384 | cm->tx_mode == TX_MODE_SELECT && mbmi->sb_type >= BLOCK_8X8; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2385 | |
Yue Chen | 3dd03e3 | 2017-10-17 15:39:52 -0700 | [diff] [blame] | 2386 | const int r_tx_size = tx_size_cost(cm, x, bs, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2387 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2388 | assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed_bsize(bs))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2389 | |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 2390 | s0 = x->skip_cost[skip_ctx][0]; |
| 2391 | s1 = x->skip_cost[skip_ctx][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2392 | |
| 2393 | mbmi->tx_type = tx_type; |
| 2394 | mbmi->tx_size = tx_size; |
Yue Chen | 95e13e2 | 2017-11-01 23:56:35 -0700 | [diff] [blame] | 2395 | #if CONFIG_FILTER_INTRA |
| 2396 | if (!is_inter_block(mbmi) && |
| 2397 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] && |
Yue Chen | 18f6c15 | 2017-11-06 11:23:47 -0800 | [diff] [blame] | 2398 | !av1_filter_intra_allowed_txsize(tx_size)) { |
| 2399 | rd_stats->rate = INT_MAX; |
Urvang Joshi | fe8323c | 2017-11-17 15:23:51 -0800 | [diff] [blame] | 2400 | // Note: Initialize following to avoid uninitialied warnings. |
| 2401 | rd_stats->dist = INT64_MAX; |
| 2402 | rd_stats->skip = 0; |
| 2403 | rd_stats->sse = INT64_MAX; |
Yue Chen | 95e13e2 | 2017-11-01 23:56:35 -0700 | [diff] [blame] | 2404 | return INT64_MAX; |
Yue Chen | 18f6c15 | 2017-11-06 11:23:47 -0800 | [diff] [blame] | 2405 | } |
Yue Chen | 95e13e2 | 2017-11-01 23:56:35 -0700 | [diff] [blame] | 2406 | #endif |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2407 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, tx_size, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2408 | cpi->sf.use_fast_coef_costing); |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2409 | if (rd_stats->rate == INT_MAX) return INT64_MAX; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2410 | #if !CONFIG_TXK_SEL |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 2411 | int plane = 0; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2412 | rd_stats->rate += av1_tx_type_cost(cm, x, xd, bs, plane, tx_size, tx_type); |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 2413 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2414 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2415 | if (rd_stats->skip) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2416 | if (is_inter) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2417 | rd = RDCOST(x->rdmult, s1, rd_stats->sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2418 | } else { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2419 | rd = RDCOST(x->rdmult, s1 + r_tx_size * tx_select, rd_stats->sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2420 | } |
| 2421 | } else { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2422 | rd = RDCOST(x->rdmult, rd_stats->rate + s0 + r_tx_size * tx_select, |
| 2423 | rd_stats->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2424 | } |
| 2425 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2426 | if (tx_select) rd_stats->rate += r_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2427 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2428 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 2429 | !(rd_stats->skip)) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2430 | rd = AOMMIN(rd, RDCOST(x->rdmult, s1, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2431 | |
| 2432 | return rd; |
| 2433 | } |
| 2434 | |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2435 | static int skip_txfm_search(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs, |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 2436 | TX_TYPE tx_type, TX_SIZE tx_size, int prune) { |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2437 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 2438 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2439 | const int is_inter = is_inter_block(mbmi); |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2440 | |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2441 | if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) return 1; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2442 | if (FIXED_TX_TYPE && tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2443 | return 1; |
| 2444 | if (!is_inter && x->use_default_intra_tx_type && |
| 2445 | tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2446 | return 1; |
| 2447 | if (is_inter && x->use_default_inter_tx_type && |
| 2448 | tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2449 | return 1; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2450 | const AV1_COMMON *const cm = &cpi->common; |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2451 | const TxSetType tx_set_type = |
| 2452 | get_ext_tx_set_type(tx_size, bs, is_inter, cm->reduced_tx_set_used); |
| 2453 | if (!av1_ext_tx_used[tx_set_type][tx_type]) return 1; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2454 | if (is_inter) { |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2455 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 2456 | if (!do_tx_type_search(tx_type, prune, cpi->sf.tx_type_search.prune_mode)) |
| 2457 | return 1; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2458 | } |
| 2459 | } else { |
| 2460 | if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) { |
| 2461 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) return 1; |
| 2462 | } |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2463 | } |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2464 | return 0; |
| 2465 | } |
| 2466 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2467 | static int64_t estimate_yrd_for_sb(const AV1_COMP *const cpi, BLOCK_SIZE bs, |
| 2468 | MACROBLOCK *x, int *r, int64_t *d, int *s, |
| 2469 | int64_t *sse, int64_t ref_best_rd) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2470 | RD_STATS rd_stats; |
| 2471 | int64_t rd = txfm_yrd(cpi, x, &rd_stats, ref_best_rd, bs, DCT_DCT, |
| 2472 | max_txsize_lookup[bs]); |
| 2473 | *r = rd_stats.rate; |
| 2474 | *d = rd_stats.dist; |
| 2475 | *s = rd_stats.skip; |
| 2476 | *sse = rd_stats.sse; |
| 2477 | return rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2478 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2479 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2480 | static void choose_largest_tx_size(const AV1_COMP *const cpi, MACROBLOCK *x, |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2481 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2482 | BLOCK_SIZE bs) { |
| 2483 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2484 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2485 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2486 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2487 | int64_t this_rd, best_rd = INT64_MAX; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 2488 | const int skip_ctx = av1_get_skip_context(xd); |
| 2489 | int s0 = x->skip_cost[skip_ctx][0]; |
| 2490 | int s1 = x->skip_cost[skip_ctx][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2491 | const int is_inter = is_inter_block(mbmi); |
| 2492 | int prune = 0; |
Angie Chiang | aa0c34b | 2017-04-25 12:25:38 -0700 | [diff] [blame] | 2493 | const int plane = 0; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2494 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2495 | |
| 2496 | mbmi->tx_size = tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2497 | mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size); |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2498 | const TxSetType tx_set_type = |
| 2499 | get_ext_tx_set_type(mbmi->tx_size, bs, is_inter, cm->reduced_tx_set_used); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2500 | |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 2501 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 2502 | !x->use_default_inter_tx_type) { |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 2503 | prune = prune_tx(cpi, bs, x, xd, tx_set_type, 0); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 2504 | } |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 2505 | if (get_ext_tx_types(mbmi->tx_size, bs, is_inter, cm->reduced_tx_set_used) > |
| 2506 | 1 && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2507 | !xd->lossless[mbmi->segment_id]) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2508 | for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) { |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2509 | if (!av1_ext_tx_used[tx_set_type][tx_type]) continue; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2510 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2511 | if (is_inter) { |
| 2512 | if (x->use_default_inter_tx_type && |
| 2513 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2514 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2515 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 2516 | if (!do_tx_type_search(tx_type, prune, |
| 2517 | cpi->sf.tx_type_search.prune_mode)) |
| 2518 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2519 | } |
| 2520 | } else { |
| 2521 | if (x->use_default_intra_tx_type && |
| 2522 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2523 | continue; |
| 2524 | if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) { |
| 2525 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 2526 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | mbmi->tx_type = tx_type; |
| 2530 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2531 | txfm_rd_in_plane(x, cpi, &this_rd_stats, ref_best_rd, 0, bs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2532 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 2533 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2534 | if (this_rd_stats.rate == INT_MAX) continue; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2535 | av1_tx_type_cost(cm, x, xd, bs, plane, mbmi->tx_size, tx_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2536 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2537 | if (this_rd_stats.skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2538 | this_rd = RDCOST(x->rdmult, s1, this_rd_stats.sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2539 | else |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2540 | this_rd = |
| 2541 | RDCOST(x->rdmult, this_rd_stats.rate + s0, this_rd_stats.dist); |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2542 | if (is_inter_block(mbmi) && !xd->lossless[mbmi->segment_id] && |
| 2543 | !this_rd_stats.skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2544 | this_rd = AOMMIN(this_rd, RDCOST(x->rdmult, s1, this_rd_stats.sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2545 | |
| 2546 | if (this_rd < best_rd) { |
| 2547 | best_rd = this_rd; |
| 2548 | best_tx_type = mbmi->tx_type; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2549 | *rd_stats = this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2550 | } |
| 2551 | } |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 2552 | |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 2553 | } else { |
| 2554 | mbmi->tx_type = DCT_DCT; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2555 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size, |
| 2556 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2557 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2558 | mbmi->tx_type = best_tx_type; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2559 | } |
| 2560 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2561 | static void choose_smallest_tx_size(const AV1_COMP *const cpi, MACROBLOCK *x, |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2562 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2563 | BLOCK_SIZE bs) { |
| 2564 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2565 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2566 | |
| 2567 | mbmi->tx_size = TX_4X4; |
| 2568 | mbmi->tx_type = DCT_DCT; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2569 | mbmi->min_tx_size = get_min_tx_size(TX_4X4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2570 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2571 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size, |
| 2572 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2573 | } |
| 2574 | |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2575 | static INLINE int bsize_to_num_blk(BLOCK_SIZE bsize) { |
| 2576 | int num_blk = 1 << (num_pels_log2_lookup[bsize] - 2 * tx_size_wide_log2[0]); |
| 2577 | return num_blk; |
| 2578 | } |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2579 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2580 | static void choose_tx_size_type_from_rd(const AV1_COMP *const cpi, |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2581 | MACROBLOCK *x, RD_STATS *rd_stats, |
| 2582 | int64_t ref_best_rd, BLOCK_SIZE bs) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2583 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2584 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2585 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2586 | int64_t rd = INT64_MAX; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2587 | int n; |
Debargha Mukherjee | 0fa057f | 2017-12-06 17:06:29 -0800 | [diff] [blame] | 2588 | int start_tx; |
| 2589 | int depth; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2590 | int64_t best_rd = INT64_MAX, last_rd = INT64_MAX; |
Debargha Mukherjee | 0fa057f | 2017-12-06 17:06:29 -0800 | [diff] [blame] | 2591 | const int is_inter = is_inter_block(mbmi); |
| 2592 | const TX_SIZE max_rect_tx_size = get_max_rect_tx_size(bs, is_inter); |
| 2593 | TX_SIZE best_tx_size = max_rect_tx_size; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2594 | TX_TYPE best_tx_type = DCT_DCT; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2595 | #if CONFIG_TXK_SEL |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2596 | TX_TYPE best_txk_type[MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)]; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2597 | #endif // CONFIG_TXK_SEL |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2598 | const int tx_select = cm->tx_mode == TX_MODE_SELECT; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2599 | |
| 2600 | av1_invalid_rd_stats(rd_stats); |
| 2601 | |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2602 | if (tx_select) { |
Debargha Mukherjee | 0fa057f | 2017-12-06 17:06:29 -0800 | [diff] [blame] | 2603 | start_tx = max_rect_tx_size; |
| 2604 | depth = 0; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2605 | } else { |
| 2606 | const TX_SIZE chosen_tx_size = |
| 2607 | tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 2608 | start_tx = chosen_tx_size; |
Debargha Mukherjee | 0fa057f | 2017-12-06 17:06:29 -0800 | [diff] [blame] | 2609 | depth = MAX_TX_DEPTH; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2610 | } |
| 2611 | |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 2612 | int prune = 0; |
| 2613 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 2614 | !x->use_default_inter_tx_type) { |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 2615 | prune = prune_tx(cpi, bs, x, xd, EXT_TX_SET_ALL16, 0); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 2616 | } |
| 2617 | |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2618 | last_rd = INT64_MAX; |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 2619 | for (n = start_tx; depth <= MAX_TX_DEPTH; |
| 2620 | depth++, n = sub_tx_size_map[0][n]) { |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2621 | TX_TYPE tx_start = DCT_DCT; |
| 2622 | TX_TYPE tx_end = TX_TYPES; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2623 | #if CONFIG_TXK_SEL |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2624 | // The tx_type becomes dummy when lv_map is on. The tx_type search will be |
| 2625 | // performed in av1_search_txk_type() |
| 2626 | tx_end = DCT_DCT + 1; |
| 2627 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2628 | TX_TYPE tx_type; |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2629 | for (tx_type = tx_start; tx_type < tx_end; ++tx_type) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2630 | RD_STATS this_rd_stats; |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 2631 | if (skip_txfm_search(cpi, x, bs, tx_type, n, prune)) continue; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2632 | rd = txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, n); |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 2633 | |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2634 | // Early termination in transform size search. |
| 2635 | if (cpi->sf.tx_size_search_breakout && |
| 2636 | (rd == INT64_MAX || |
Debargha Mukherjee | 0fa057f | 2017-12-06 17:06:29 -0800 | [diff] [blame] | 2637 | (this_rd_stats.skip == 1 && tx_type != DCT_DCT && n != start_tx) || |
| 2638 | (n != (int)start_tx && rd > last_rd))) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2639 | break; |
Lester Lu | 432012f | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 2640 | } |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2641 | |
| 2642 | last_rd = rd; |
Hui Su | da816a1 | 2017-08-18 14:46:02 -0700 | [diff] [blame] | 2643 | ref_best_rd = AOMMIN(rd, ref_best_rd); |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2644 | if (rd < best_rd) { |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2645 | #if CONFIG_TXK_SEL |
Angie Chiang | bce07f1 | 2017-12-01 16:34:31 -0800 | [diff] [blame] | 2646 | memcpy(best_txk_type, mbmi->txk_type, |
| 2647 | sizeof(best_txk_type[0]) * MAX_SB_SQUARE / |
| 2648 | (TX_SIZE_W_MIN * TX_SIZE_H_MIN)); |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2649 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2650 | best_tx_type = tx_type; |
| 2651 | best_tx_size = n; |
| 2652 | best_rd = rd; |
| 2653 | *rd_stats = this_rd_stats; |
| 2654 | } |
Debargha Mukherjee | 6ea917e | 2017-10-19 09:31:29 -0700 | [diff] [blame] | 2655 | #if !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2656 | const int is_inter = is_inter_block(mbmi); |
| 2657 | if (mbmi->sb_type < BLOCK_8X8 && is_inter) break; |
Debargha Mukherjee | 6ea917e | 2017-10-19 09:31:29 -0700 | [diff] [blame] | 2658 | #endif // !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | mbmi->tx_size = best_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2662 | mbmi->tx_type = best_tx_type; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2663 | #if CONFIG_TXK_SEL |
Angie Chiang | bce07f1 | 2017-12-01 16:34:31 -0800 | [diff] [blame] | 2664 | memcpy(mbmi->txk_type, best_txk_type, |
| 2665 | sizeof(best_txk_type[0]) * MAX_SB_SQUARE / |
| 2666 | (TX_SIZE_W_MIN * TX_SIZE_H_MIN)); |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2667 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2668 | |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2669 | mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2670 | } |
| 2671 | |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2672 | static void super_block_yrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2673 | RD_STATS *rd_stats, BLOCK_SIZE bs, |
| 2674 | int64_t ref_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2675 | MACROBLOCKD *xd = &x->e_mbd; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2676 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2677 | |
| 2678 | assert(bs == xd->mi[0]->mbmi.sb_type); |
| 2679 | |
Yaowu Xu | 1e2aae1 | 2017-02-27 16:33:14 -0800 | [diff] [blame] | 2680 | if (xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2681 | choose_smallest_tx_size(cpi, x, rd_stats, ref_best_rd, bs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2682 | } else if (cpi->sf.tx_size_search_method == USE_LARGESTALL) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2683 | choose_largest_tx_size(cpi, x, rd_stats, ref_best_rd, bs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2684 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2685 | choose_tx_size_type_from_rd(cpi, x, rd_stats, ref_best_rd, bs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | static int conditional_skipintra(PREDICTION_MODE mode, |
| 2690 | PREDICTION_MODE best_intra_mode) { |
| 2691 | if (mode == D117_PRED && best_intra_mode != V_PRED && |
| 2692 | best_intra_mode != D135_PRED) |
| 2693 | return 1; |
| 2694 | if (mode == D63_PRED && best_intra_mode != V_PRED && |
| 2695 | best_intra_mode != D45_PRED) |
| 2696 | return 1; |
| 2697 | if (mode == D207_PRED && best_intra_mode != H_PRED && |
| 2698 | best_intra_mode != D45_PRED) |
| 2699 | return 1; |
| 2700 | if (mode == D153_PRED && best_intra_mode != H_PRED && |
| 2701 | best_intra_mode != D135_PRED) |
| 2702 | return 1; |
| 2703 | return 0; |
| 2704 | } |
| 2705 | |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2706 | // Model based RD estimation for luma intra blocks. |
| 2707 | static int64_t intra_model_yrd(const AV1_COMP *const cpi, MACROBLOCK *const x, |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 2708 | BLOCK_SIZE bsize, int mode_cost) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 2709 | const AV1_COMMON *cm = &cpi->common; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2710 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2711 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 2712 | assert(!is_inter_block(mbmi)); |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2713 | RD_STATS this_rd_stats; |
| 2714 | int row, col; |
| 2715 | int64_t temp_sse, this_rd; |
| 2716 | const TX_SIZE tx_size = tx_size_from_tx_mode(bsize, cpi->common.tx_mode, 0); |
| 2717 | const int stepr = tx_size_high_unit[tx_size]; |
| 2718 | const int stepc = tx_size_wide_unit[tx_size]; |
| 2719 | const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
| 2720 | const int max_blocks_high = max_block_high(xd, bsize, 0); |
| 2721 | mbmi->tx_size = tx_size; |
| 2722 | // Prediction. |
Angie Chiang | 3d005e4 | 2017-04-02 16:31:35 -0700 | [diff] [blame] | 2723 | const int step = stepr * stepc; |
| 2724 | int block = 0; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2725 | for (row = 0; row < max_blocks_high; row += stepr) { |
| 2726 | for (col = 0; col < max_blocks_wide; col += stepc) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 2727 | av1_predict_intra_block_facade(cm, xd, 0, block, col, row, tx_size); |
Angie Chiang | 3d005e4 | 2017-04-02 16:31:35 -0700 | [diff] [blame] | 2728 | block += step; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | // RD estimation. |
| 2732 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &this_rd_stats.rate, |
| 2733 | &this_rd_stats.dist, &this_rd_stats.skip, &temp_sse); |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 2734 | #if CONFIG_EXT_INTRA |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 2735 | if (av1_is_directional_mode(mbmi->mode, bsize) && |
| 2736 | av1_use_angle_delta(bsize)) { |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 2737 | #if CONFIG_EXT_INTRA_MOD |
| 2738 | mode_cost += x->angle_delta_cost[mbmi->mode - V_PRED] |
| 2739 | [MAX_ANGLE_DELTA + mbmi->angle_delta[0]]; |
| 2740 | #else |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 2741 | mode_cost += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 2742 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 2743 | #endif // CONFIG_EXT_INTRA_MOD |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 2744 | } |
| 2745 | #endif // CONFIG_EXT_INTRA |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 2746 | #if CONFIG_FILTER_INTRA |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 2747 | if (mbmi->mode == DC_PRED && av1_filter_intra_allowed_txsize(mbmi->tx_size)) { |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 2748 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) { |
| 2749 | const int mode = mbmi->filter_intra_mode_info.filter_intra_mode[0]; |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 2750 | mode_cost += x->filter_intra_cost[mbmi->tx_size][1] + |
| 2751 | x->filter_intra_mode_cost[0][mode]; |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 2752 | } else { |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 2753 | mode_cost += x->filter_intra_cost[mbmi->tx_size][0]; |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2757 | this_rd = |
| 2758 | RDCOST(x->rdmult, this_rd_stats.rate + mode_cost, this_rd_stats.dist); |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2759 | return this_rd; |
| 2760 | } |
| 2761 | |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 2762 | // Extends 'color_map' array from 'orig_width x orig_height' to 'new_width x |
| 2763 | // new_height'. Extra rows and columns are filled in by copying last valid |
| 2764 | // row/column. |
| 2765 | static void extend_palette_color_map(uint8_t *const color_map, int orig_width, |
| 2766 | int orig_height, int new_width, |
| 2767 | int new_height) { |
| 2768 | int j; |
| 2769 | assert(new_width >= orig_width); |
| 2770 | assert(new_height >= orig_height); |
| 2771 | if (new_width == orig_width && new_height == orig_height) return; |
| 2772 | |
| 2773 | for (j = orig_height - 1; j >= 0; --j) { |
| 2774 | memmove(color_map + j * new_width, color_map + j * orig_width, orig_width); |
| 2775 | // Copy last column to extra columns. |
| 2776 | memset(color_map + j * new_width + orig_width, |
| 2777 | color_map[j * new_width + orig_width - 1], new_width - orig_width); |
| 2778 | } |
| 2779 | // Copy last row to extra rows. |
| 2780 | for (j = orig_height; j < new_height; ++j) { |
| 2781 | memcpy(color_map + j * new_width, color_map + (orig_height - 1) * new_width, |
| 2782 | new_width); |
| 2783 | } |
| 2784 | } |
| 2785 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2786 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 2787 | // Bias toward using colors in the cache. |
| 2788 | // TODO(huisu): Try other schemes to improve compression. |
| 2789 | static void optimize_palette_colors(uint16_t *color_cache, int n_cache, |
| 2790 | int n_colors, int stride, |
| 2791 | float *centroids) { |
| 2792 | if (n_cache <= 0) return; |
| 2793 | for (int i = 0; i < n_colors * stride; i += stride) { |
| 2794 | float min_diff = fabsf(centroids[i] - color_cache[0]); |
| 2795 | int idx = 0; |
| 2796 | for (int j = 1; j < n_cache; ++j) { |
| 2797 | float this_diff = fabsf(centroids[i] - color_cache[j]); |
| 2798 | if (this_diff < min_diff) { |
| 2799 | min_diff = this_diff; |
| 2800 | idx = j; |
| 2801 | } |
| 2802 | } |
| 2803 | if (min_diff < 1.5) centroids[i] = color_cache[idx]; |
| 2804 | } |
| 2805 | } |
| 2806 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 2807 | |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 2808 | // Given the base colors as specified in centroids[], calculate the RD cost |
| 2809 | // of palette mode. |
| 2810 | static void palette_rd_y(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2811 | MB_MODE_INFO *mbmi, BLOCK_SIZE bsize, int palette_ctx, |
| 2812 | int dc_mode_cost, const float *data, float *centroids, |
| 2813 | int n, |
| 2814 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 2815 | uint16_t *color_cache, int n_cache, |
| 2816 | #endif |
| 2817 | MB_MODE_INFO *best_mbmi, |
| 2818 | uint8_t *best_palette_color_map, int64_t *best_rd, |
| 2819 | int64_t *best_model_rd, int *rate, int *rate_tokenonly, |
| 2820 | int *rate_overhead, int64_t *distortion, |
| 2821 | int *skippable) { |
| 2822 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 2823 | optimize_palette_colors(color_cache, n_cache, n, 1, centroids); |
| 2824 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 2825 | int k = av1_remove_duplicates(centroids, n); |
| 2826 | if (k < PALETTE_MIN_SIZE) { |
| 2827 | // Too few unique colors to create a palette. And DC_PRED will work |
| 2828 | // well for that case anyway. So skip. |
| 2829 | return; |
| 2830 | } |
| 2831 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 2832 | #if CONFIG_HIGHBITDEPTH |
| 2833 | if (cpi->common.use_highbitdepth) |
| 2834 | for (int i = 0; i < k; ++i) |
| 2835 | pmi->palette_colors[i] = |
| 2836 | clip_pixel_highbd((int)centroids[i], cpi->common.bit_depth); |
| 2837 | else |
| 2838 | #endif // CONFIG_HIGHBITDEPTH |
| 2839 | for (int i = 0; i < k; ++i) |
| 2840 | pmi->palette_colors[i] = clip_pixel((int)centroids[i]); |
| 2841 | pmi->palette_size[0] = k; |
| 2842 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2843 | uint8_t *const color_map = xd->plane[0].color_index_map; |
| 2844 | int block_width, block_height, rows, cols; |
| 2845 | av1_get_block_dimensions(bsize, 0, xd, &block_width, &block_height, &rows, |
| 2846 | &cols); |
| 2847 | av1_calc_indices(data, centroids, color_map, rows * cols, k, 1); |
| 2848 | extend_palette_color_map(color_map, cols, rows, block_width, block_height); |
| 2849 | int palette_mode_cost = |
| 2850 | dc_mode_cost + |
| 2851 | x->palette_y_size_cost[bsize - BLOCK_8X8][k - PALETTE_MIN_SIZE] + |
| 2852 | write_uniform_cost(k, color_map[0]) + |
| 2853 | x->palette_y_mode_cost[bsize - BLOCK_8X8][palette_ctx][1]; |
| 2854 | palette_mode_cost += av1_palette_color_cost_y(pmi, |
| 2855 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 2856 | color_cache, n_cache, |
| 2857 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 2858 | cpi->common.bit_depth); |
| 2859 | palette_mode_cost += |
| 2860 | av1_cost_color_map(x, 0, 0, bsize, mbmi->tx_size, PALETTE_MAP); |
| 2861 | int64_t this_model_rd = intra_model_yrd(cpi, x, bsize, palette_mode_cost); |
| 2862 | if (*best_model_rd != INT64_MAX && |
| 2863 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 2864 | return; |
| 2865 | if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd; |
| 2866 | RD_STATS tokenonly_rd_stats; |
| 2867 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 2868 | if (tokenonly_rd_stats.rate == INT_MAX) return; |
| 2869 | int this_rate = tokenonly_rd_stats.rate + palette_mode_cost; |
| 2870 | int64_t this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
| 2871 | if (!xd->lossless[mbmi->segment_id] && block_signals_txsize(mbmi->sb_type)) { |
| 2872 | tokenonly_rd_stats.rate -= |
| 2873 | tx_size_cost(&cpi->common, x, bsize, mbmi->tx_size); |
| 2874 | } |
| 2875 | if (this_rd < *best_rd) { |
| 2876 | *best_rd = this_rd; |
| 2877 | memcpy(best_palette_color_map, color_map, |
| 2878 | block_width * block_height * sizeof(color_map[0])); |
| 2879 | *best_mbmi = *mbmi; |
| 2880 | *rate_overhead = this_rate - tokenonly_rd_stats.rate; |
| 2881 | if (rate) *rate = this_rate; |
| 2882 | if (rate_tokenonly) *rate_tokenonly = tokenonly_rd_stats.rate; |
| 2883 | if (distortion) *distortion = tokenonly_rd_stats.dist; |
| 2884 | if (skippable) *skippable = tokenonly_rd_stats.skip; |
| 2885 | } |
| 2886 | } |
| 2887 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 2888 | static int rd_pick_palette_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2889 | BLOCK_SIZE bsize, int palette_ctx, |
| 2890 | int dc_mode_cost, MB_MODE_INFO *best_mbmi, |
| 2891 | uint8_t *best_palette_color_map, |
hui su | 78c611a | 2017-01-13 17:06:04 -0800 | [diff] [blame] | 2892 | int64_t *best_rd, int64_t *best_model_rd, |
| 2893 | int *rate, int *rate_tokenonly, |
| 2894 | int64_t *distortion, int *skippable) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2895 | int rate_overhead = 0; |
| 2896 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2897 | MODE_INFO *const mic = xd->mi[0]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 2898 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 2899 | assert(!is_inter_block(mbmi)); |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 2900 | assert(bsize >= BLOCK_8X8); |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 2901 | int colors, n; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2902 | const int src_stride = x->plane[0].src.stride; |
| 2903 | const uint8_t *const src = x->plane[0].src.buf; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 2904 | uint8_t *const color_map = xd->plane[0].color_index_map; |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 2905 | int block_width, block_height, rows, cols; |
| 2906 | av1_get_block_dimensions(bsize, 0, xd, &block_width, &block_height, &rows, |
| 2907 | &cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2908 | |
| 2909 | assert(cpi->common.allow_screen_content_tools); |
| 2910 | |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 2911 | int count_buf[1 << 12]; // Maximum (1 << 12) color levels. |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2912 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2913 | if (cpi->common.use_highbitdepth) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2914 | colors = av1_count_colors_highbd(src, src_stride, rows, cols, |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 2915 | cpi->common.bit_depth, count_buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2916 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2917 | #endif // CONFIG_HIGHBITDEPTH |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 2918 | colors = av1_count_colors(src, src_stride, rows, cols, count_buf); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2919 | #if CONFIG_FILTER_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 2920 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2921 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2922 | |
| 2923 | if (colors > 1 && colors <= 64) { |
Hui Su | c3769e5 | 2017-11-16 23:13:42 -0800 | [diff] [blame] | 2924 | aom_clear_system_state(); |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 2925 | int r, c, i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2926 | const int max_itr = 50; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2927 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 2928 | float centroids[PALETTE_MAX_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2929 | float lb, ub, val; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2930 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2931 | uint16_t *src16 = CONVERT_TO_SHORTPTR(src); |
| 2932 | if (cpi->common.use_highbitdepth) |
| 2933 | lb = ub = src16[0]; |
| 2934 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2935 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2936 | lb = ub = src[0]; |
| 2937 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2938 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2939 | if (cpi->common.use_highbitdepth) { |
| 2940 | for (r = 0; r < rows; ++r) { |
| 2941 | for (c = 0; c < cols; ++c) { |
| 2942 | val = src16[r * src_stride + c]; |
| 2943 | data[r * cols + c] = val; |
| 2944 | if (val < lb) |
| 2945 | lb = val; |
| 2946 | else if (val > ub) |
| 2947 | ub = val; |
| 2948 | } |
| 2949 | } |
| 2950 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2951 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2952 | for (r = 0; r < rows; ++r) { |
| 2953 | for (c = 0; c < cols; ++c) { |
| 2954 | val = src[r * src_stride + c]; |
| 2955 | data[r * cols + c] = val; |
| 2956 | if (val < lb) |
| 2957 | lb = val; |
| 2958 | else if (val > ub) |
| 2959 | ub = val; |
| 2960 | } |
| 2961 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2962 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2963 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2964 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2965 | |
| 2966 | mbmi->mode = DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2967 | #if CONFIG_FILTER_INTRA |
| 2968 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 2969 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2970 | |
Hui Su | 473cf89 | 2017-11-08 18:14:31 -0800 | [diff] [blame] | 2971 | if (rows * cols > MAX_PALETTE_SQUARE) return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2972 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2973 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2974 | uint16_t color_cache[2 * PALETTE_MAX_SIZE]; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 2975 | const int n_cache = av1_get_palette_cache(xd, 0, color_cache); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2976 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 2977 | |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 2978 | // Find the dominant colors, stored in top_colors[]. |
| 2979 | int top_colors[PALETTE_MAX_SIZE] = { 0 }; |
| 2980 | for (i = 0; i < AOMMIN(colors, PALETTE_MAX_SIZE); ++i) { |
| 2981 | int max_count = 0; |
| 2982 | for (int j = 0; j < (1 << cpi->common.bit_depth); ++j) { |
| 2983 | if (count_buf[j] > max_count) { |
| 2984 | max_count = count_buf[j]; |
| 2985 | top_colors[i] = j; |
| 2986 | } |
| 2987 | } |
| 2988 | assert(max_count > 0); |
| 2989 | count_buf[top_colors[i]] = 0; |
| 2990 | } |
| 2991 | |
| 2992 | // Try the dominant colors directly. |
| 2993 | // TODO(huisu@google.com): Try to avoid duplicate computation in cases |
| 2994 | // where the dominant colors and the k-means results are similar. |
| 2995 | for (n = AOMMIN(colors, PALETTE_MAX_SIZE); n >= 2; --n) { |
| 2996 | for (i = 0; i < n; ++i) centroids[i] = top_colors[i]; |
| 2997 | palette_rd_y(cpi, x, mbmi, bsize, palette_ctx, dc_mode_cost, data, |
| 2998 | centroids, n, |
| 2999 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 3000 | color_cache, n_cache, |
| 3001 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 3002 | best_mbmi, best_palette_color_map, best_rd, best_model_rd, |
| 3003 | rate, rate_tokenonly, &rate_overhead, distortion, skippable); |
| 3004 | } |
| 3005 | |
| 3006 | // K-means clustering. |
| 3007 | for (n = AOMMIN(colors, PALETTE_MAX_SIZE); n >= 2; --n) { |
Urvang Joshi | 773e354 | 2017-05-05 18:09:42 -0700 | [diff] [blame] | 3008 | if (colors == PALETTE_MIN_SIZE) { |
| 3009 | // Special case: These colors automatically become the centroids. |
| 3010 | assert(colors == n); |
| 3011 | assert(colors == 2); |
| 3012 | centroids[0] = lb; |
| 3013 | centroids[1] = ub; |
Urvang Joshi | 773e354 | 2017-05-05 18:09:42 -0700 | [diff] [blame] | 3014 | } else { |
| 3015 | for (i = 0; i < n; ++i) { |
| 3016 | centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2; |
| 3017 | } |
| 3018 | av1_k_means(data, centroids, color_map, rows * cols, n, 1, max_itr); |
Urvang Joshi | 5fb95f7 | 2017-05-05 17:36:16 -0700 | [diff] [blame] | 3019 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3020 | |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 3021 | palette_rd_y(cpi, x, mbmi, bsize, palette_ctx, dc_mode_cost, data, |
| 3022 | centroids, n, |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 3023 | #if CONFIG_PALETTE_DELTA_ENCODING |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 3024 | color_cache, n_cache, |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 3025 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 3026 | best_mbmi, best_palette_color_map, best_rd, best_model_rd, |
| 3027 | rate, rate_tokenonly, &rate_overhead, distortion, skippable); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3028 | } |
| 3029 | } |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3030 | |
| 3031 | if (best_mbmi->palette_mode_info.palette_size[0] > 0) { |
| 3032 | memcpy(color_map, best_palette_color_map, |
Luc Trudeau | 0401e89 | 2017-08-31 00:37:11 -0400 | [diff] [blame] | 3033 | block_width * block_height * sizeof(best_palette_color_map[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3034 | } |
| 3035 | *mbmi = *best_mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3036 | return rate_overhead; |
| 3037 | } |
| 3038 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3039 | #if CONFIG_FILTER_INTRA |
| 3040 | // Return 1 if an filter intra mode is selected; return 0 otherwise. |
| 3041 | static int rd_pick_filter_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 3042 | int *rate, int *rate_tokenonly, |
| 3043 | int64_t *distortion, int *skippable, |
| 3044 | BLOCK_SIZE bsize, int mode_cost, |
Yue Chen | da2eefc | 2017-11-16 15:25:28 -0800 | [diff] [blame] | 3045 | int64_t *best_rd, int64_t *best_model_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3046 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3047 | MODE_INFO *const mic = xd->mi[0]; |
| 3048 | MB_MODE_INFO *mbmi = &mic->mbmi; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3049 | int filter_intra_selected_flag = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3050 | FILTER_INTRA_MODE mode; |
Yue Chen | 18f6c15 | 2017-11-06 11:23:47 -0800 | [diff] [blame] | 3051 | TX_SIZE best_tx_size = TX_8X8; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3052 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3053 | TX_TYPE best_tx_type; |
| 3054 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3055 | av1_zero(filter_intra_mode_info); |
| 3056 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3057 | mbmi->mode = DC_PRED; |
| 3058 | mbmi->palette_mode_info.palette_size[0] = 0; |
| 3059 | |
| 3060 | for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) { |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 3061 | int this_rate; |
| 3062 | int64_t this_rd, this_model_rd; |
| 3063 | RD_STATS tokenonly_rd_stats; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3064 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = mode; |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 3065 | this_model_rd = intra_model_yrd(cpi, x, bsize, mode_cost); |
| 3066 | if (*best_model_rd != INT64_MAX && |
| 3067 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 3068 | continue; |
| 3069 | if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3070 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 3071 | if (tokenonly_rd_stats.rate == INT_MAX) continue; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3072 | this_rate = tokenonly_rd_stats.rate + |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 3073 | x->filter_intra_cost[mbmi->tx_size][1] + |
Yue Chen | 63ce36f | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 3074 | x->filter_intra_mode_cost[0][mode] + mode_cost; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3075 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3076 | |
| 3077 | if (this_rd < *best_rd) { |
| 3078 | *best_rd = this_rd; |
| 3079 | best_tx_size = mic->mbmi.tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3080 | filter_intra_mode_info = mbmi->filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3081 | best_tx_type = mic->mbmi.tx_type; |
| 3082 | *rate = this_rate; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3083 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 3084 | *distortion = tokenonly_rd_stats.dist; |
| 3085 | *skippable = tokenonly_rd_stats.skip; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3086 | filter_intra_selected_flag = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3087 | } |
| 3088 | } |
| 3089 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3090 | if (filter_intra_selected_flag) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3091 | mbmi->mode = DC_PRED; |
| 3092 | mbmi->tx_size = best_tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3093 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = |
| 3094 | filter_intra_mode_info.use_filter_intra_mode[0]; |
| 3095 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = |
| 3096 | filter_intra_mode_info.filter_intra_mode[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3097 | mbmi->tx_type = best_tx_type; |
| 3098 | return 1; |
| 3099 | } else { |
| 3100 | return 0; |
| 3101 | } |
| 3102 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3103 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3104 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3105 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3106 | // Run RD calculation with given luma intra prediction angle., and return |
| 3107 | // the RD cost. Update the best mode info. if the RD cost is the best so far. |
| 3108 | static int64_t calc_rd_given_intra_angle( |
| 3109 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mode_cost, |
| 3110 | int64_t best_rd_in, int8_t angle_delta, int max_angle_delta, int *rate, |
| 3111 | RD_STATS *rd_stats, int *best_angle_delta, TX_SIZE *best_tx_size, |
Angie Chiang | 53bf1e9 | 2017-11-29 16:53:07 -0800 | [diff] [blame] | 3112 | TX_TYPE *best_tx_type, int64_t *best_rd, int64_t *best_model_rd, |
| 3113 | TX_TYPE *best_txk_type) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3114 | int this_rate; |
| 3115 | RD_STATS tokenonly_rd_stats; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3116 | int64_t this_rd, this_model_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3117 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3118 | assert(!is_inter_block(mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3119 | |
Angie Chiang | 53bf1e9 | 2017-11-29 16:53:07 -0800 | [diff] [blame] | 3120 | #if !CONFIG_TXK_SEL |
| 3121 | (void)best_txk_type; |
| 3122 | #endif |
| 3123 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3124 | mbmi->angle_delta[0] = angle_delta; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3125 | this_model_rd = intra_model_yrd(cpi, x, bsize, mode_cost); |
| 3126 | if (*best_model_rd != INT64_MAX && |
| 3127 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 3128 | return INT64_MAX; |
| 3129 | if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3130 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in); |
| 3131 | if (tokenonly_rd_stats.rate == INT_MAX) return INT64_MAX; |
| 3132 | |
| 3133 | this_rate = tokenonly_rd_stats.rate + mode_cost + |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 3134 | #if CONFIG_EXT_INTRA_MOD |
| 3135 | x->angle_delta_cost[mbmi->mode - V_PRED] |
| 3136 | [max_angle_delta + mbmi->angle_delta[0]]; |
| 3137 | #else |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3138 | write_uniform_cost(2 * max_angle_delta + 1, |
| 3139 | mbmi->angle_delta[0] + max_angle_delta); |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 3140 | #endif // CONFIG_EXT_INTRA_MOD |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3141 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3142 | |
| 3143 | if (this_rd < *best_rd) { |
Angie Chiang | 53bf1e9 | 2017-11-29 16:53:07 -0800 | [diff] [blame] | 3144 | #if CONFIG_TXK_SEL |
| 3145 | memcpy(best_txk_type, mbmi->txk_type, |
| 3146 | sizeof(*best_txk_type) * |
| 3147 | (MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN))); |
| 3148 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3149 | *best_rd = this_rd; |
| 3150 | *best_angle_delta = mbmi->angle_delta[0]; |
| 3151 | *best_tx_size = mbmi->tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3152 | *best_tx_type = mbmi->tx_type; |
| 3153 | *rate = this_rate; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3154 | rd_stats->rate = tokenonly_rd_stats.rate; |
| 3155 | rd_stats->dist = tokenonly_rd_stats.dist; |
| 3156 | rd_stats->skip = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3157 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3158 | return this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3159 | } |
| 3160 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3161 | // With given luma directional intra prediction mode, pick the best angle delta |
| 3162 | // Return the RD cost corresponding to the best angle delta. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3163 | static int64_t rd_pick_intra_angle_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3164 | int *rate, RD_STATS *rd_stats, |
| 3165 | BLOCK_SIZE bsize, int mode_cost, |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3166 | int64_t best_rd, |
| 3167 | int64_t *best_model_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3168 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3169 | MODE_INFO *const mic = xd->mi[0]; |
| 3170 | MB_MODE_INFO *mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3171 | assert(!is_inter_block(mbmi)); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3172 | int i, angle_delta, best_angle_delta = 0; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3173 | int first_try = 1; |
Debargha Mukherjee | df0e0d7 | 2017-04-27 15:16:53 -0700 | [diff] [blame] | 3174 | int64_t this_rd, best_rd_in, rd_cost[2 * (MAX_ANGLE_DELTA + 2)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3175 | TX_SIZE best_tx_size = mic->mbmi.tx_size; |
| 3176 | TX_TYPE best_tx_type = mbmi->tx_type; |
Angie Chiang | 53bf1e9 | 2017-11-29 16:53:07 -0800 | [diff] [blame] | 3177 | #if CONFIG_TXK_SEL |
| 3178 | TX_TYPE best_txk_type[MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)]; |
| 3179 | #else |
| 3180 | TX_TYPE *best_txk_type = NULL; |
| 3181 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3182 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3183 | for (i = 0; i < 2 * (MAX_ANGLE_DELTA + 2); ++i) rd_cost[i] = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3184 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3185 | for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
Hui Su | 259d442 | 2017-10-13 10:08:17 -0700 | [diff] [blame] | 3186 | for (i = 0; i < 2; ++i) { |
| 3187 | best_rd_in = (best_rd == INT64_MAX) |
| 3188 | ? INT64_MAX |
| 3189 | : (best_rd + (best_rd >> (first_try ? 3 : 5))); |
| 3190 | this_rd = calc_rd_given_intra_angle( |
| 3191 | cpi, x, bsize, mode_cost, best_rd_in, (1 - 2 * i) * angle_delta, |
| 3192 | MAX_ANGLE_DELTA, rate, rd_stats, &best_angle_delta, &best_tx_size, |
Angie Chiang | 53bf1e9 | 2017-11-29 16:53:07 -0800 | [diff] [blame] | 3193 | &best_tx_type, &best_rd, best_model_rd, best_txk_type); |
Hui Su | 259d442 | 2017-10-13 10:08:17 -0700 | [diff] [blame] | 3194 | rd_cost[2 * angle_delta + i] = this_rd; |
| 3195 | if (first_try && this_rd == INT64_MAX) return best_rd; |
| 3196 | first_try = 0; |
| 3197 | if (angle_delta == 0) { |
| 3198 | rd_cost[1] = this_rd; |
| 3199 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3200 | } |
| 3201 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | assert(best_rd != INT64_MAX); |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3205 | for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3206 | int64_t rd_thresh; |
Hui Su | 259d442 | 2017-10-13 10:08:17 -0700 | [diff] [blame] | 3207 | for (i = 0; i < 2; ++i) { |
| 3208 | int skip_search = 0; |
| 3209 | rd_thresh = best_rd + (best_rd >> 5); |
| 3210 | if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh && |
| 3211 | rd_cost[2 * (angle_delta - 1) + i] > rd_thresh) |
| 3212 | skip_search = 1; |
| 3213 | if (!skip_search) { |
Sebastien Alaiwan | 7fc6b2a | 2017-11-02 18:14:50 +0100 | [diff] [blame] | 3214 | calc_rd_given_intra_angle( |
| 3215 | cpi, x, bsize, mode_cost, best_rd, (1 - 2 * i) * angle_delta, |
| 3216 | MAX_ANGLE_DELTA, rate, rd_stats, &best_angle_delta, &best_tx_size, |
Angie Chiang | 53bf1e9 | 2017-11-29 16:53:07 -0800 | [diff] [blame] | 3217 | &best_tx_type, &best_rd, best_model_rd, best_txk_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3218 | } |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | mbmi->tx_size = best_tx_size; |
| 3223 | mbmi->angle_delta[0] = best_angle_delta; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3224 | mbmi->tx_type = best_tx_type; |
Angie Chiang | 53bf1e9 | 2017-11-29 16:53:07 -0800 | [diff] [blame] | 3225 | #if CONFIG_TXK_SEL |
| 3226 | memcpy(mbmi->txk_type, best_txk_type, |
| 3227 | sizeof(*best_txk_type) * |
| 3228 | (MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN))); |
| 3229 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3230 | return best_rd; |
| 3231 | } |
| 3232 | |
| 3233 | // Indices are sign, integer, and fractional part of the gradient value |
| 3234 | static const uint8_t gradient_to_angle_bin[2][7][16] = { |
| 3235 | { |
| 3236 | { 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0 }, |
| 3237 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, |
| 3238 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 3239 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 3240 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 3241 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 3242 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 3243 | }, |
| 3244 | { |
| 3245 | { 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4 }, |
| 3246 | { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3 }, |
| 3247 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 3248 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 3249 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 3250 | { 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 3251 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 3252 | }, |
| 3253 | }; |
| 3254 | |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 3255 | /* clang-format off */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3256 | static const uint8_t mode_to_angle_bin[INTRA_MODES] = { |
| 3257 | 0, 2, 6, 0, 4, 3, 5, 7, 1, 0, |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3258 | 0, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3259 | }; |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 3260 | /* clang-format on */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3261 | |
| 3262 | static void angle_estimation(const uint8_t *src, int src_stride, int rows, |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3263 | int cols, BLOCK_SIZE bsize, |
| 3264 | uint8_t *directional_mode_skip_mask) { |
| 3265 | memset(directional_mode_skip_mask, 0, |
| 3266 | INTRA_MODES * sizeof(*directional_mode_skip_mask)); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 3267 | // Check if angle_delta is used |
| 3268 | if (!av1_use_angle_delta(bsize)) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3269 | uint64_t hist[DIRECTIONAL_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3270 | memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0])); |
| 3271 | src += src_stride; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3272 | int r, c, dx, dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3273 | for (r = 1; r < rows; ++r) { |
| 3274 | for (c = 1; c < cols; ++c) { |
| 3275 | dx = src[c] - src[c - 1]; |
| 3276 | dy = src[c] - src[c - src_stride]; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3277 | int index; |
| 3278 | const int temp = dx * dx + dy * dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3279 | if (dy == 0) { |
| 3280 | index = 2; |
| 3281 | } else { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3282 | const int sn = (dx > 0) ^ (dy > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3283 | dx = abs(dx); |
| 3284 | dy = abs(dy); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3285 | const int remd = (dx % dy) * 16 / dy; |
| 3286 | const int quot = dx / dy; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3287 | index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3288 | } |
| 3289 | hist[index] += temp; |
| 3290 | } |
| 3291 | src += src_stride; |
| 3292 | } |
| 3293 | |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3294 | int i; |
| 3295 | uint64_t hist_sum = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3296 | for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i]; |
| 3297 | for (i = 0; i < INTRA_MODES; ++i) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3298 | if (av1_is_directional_mode(i, bsize)) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 3299 | const uint8_t angle_bin = mode_to_angle_bin[i]; |
| 3300 | uint64_t score = 2 * hist[angle_bin]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3301 | int weight = 2; |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 3302 | if (angle_bin > 0) { |
| 3303 | score += hist[angle_bin - 1]; |
| 3304 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3305 | } |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 3306 | if (angle_bin < DIRECTIONAL_MODES - 1) { |
| 3307 | score += hist[angle_bin + 1]; |
| 3308 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3309 | } |
| 3310 | if (score * ANGLE_SKIP_THRESH < hist_sum * weight) |
| 3311 | directional_mode_skip_mask[i] = 1; |
| 3312 | } |
| 3313 | } |
| 3314 | } |
| 3315 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3316 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3317 | static void highbd_angle_estimation(const uint8_t *src8, int src_stride, |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3318 | int rows, int cols, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3319 | uint8_t *directional_mode_skip_mask) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3320 | memset(directional_mode_skip_mask, 0, |
| 3321 | INTRA_MODES * sizeof(*directional_mode_skip_mask)); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 3322 | // Check if angle_delta is used |
| 3323 | if (!av1_use_angle_delta(bsize)) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3324 | uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3325 | uint64_t hist[DIRECTIONAL_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3326 | memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0])); |
| 3327 | src += src_stride; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3328 | int r, c, dx, dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3329 | for (r = 1; r < rows; ++r) { |
| 3330 | for (c = 1; c < cols; ++c) { |
| 3331 | dx = src[c] - src[c - 1]; |
| 3332 | dy = src[c] - src[c - src_stride]; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3333 | int index; |
| 3334 | const int temp = dx * dx + dy * dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3335 | if (dy == 0) { |
| 3336 | index = 2; |
| 3337 | } else { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3338 | const int sn = (dx > 0) ^ (dy > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3339 | dx = abs(dx); |
| 3340 | dy = abs(dy); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3341 | const int remd = (dx % dy) * 16 / dy; |
| 3342 | const int quot = dx / dy; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3343 | index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3344 | } |
| 3345 | hist[index] += temp; |
| 3346 | } |
| 3347 | src += src_stride; |
| 3348 | } |
| 3349 | |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3350 | int i; |
| 3351 | uint64_t hist_sum = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3352 | for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i]; |
| 3353 | for (i = 0; i < INTRA_MODES; ++i) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3354 | if (av1_is_directional_mode(i, bsize)) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 3355 | const uint8_t angle_bin = mode_to_angle_bin[i]; |
| 3356 | uint64_t score = 2 * hist[angle_bin]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3357 | int weight = 2; |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 3358 | if (angle_bin > 0) { |
| 3359 | score += hist[angle_bin - 1]; |
| 3360 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3361 | } |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 3362 | if (angle_bin < DIRECTIONAL_MODES - 1) { |
| 3363 | score += hist[angle_bin + 1]; |
| 3364 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3365 | } |
| 3366 | if (score * ANGLE_SKIP_THRESH < hist_sum * weight) |
| 3367 | directional_mode_skip_mask[i] = 1; |
| 3368 | } |
| 3369 | } |
| 3370 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3371 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3372 | #endif // CONFIG_EXT_INTRA |
| 3373 | |
| 3374 | // This function is used only for intra_only frames |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3375 | static int64_t rd_pick_intra_sby_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 3376 | int *rate, int *rate_tokenonly, |
| 3377 | int64_t *distortion, int *skippable, |
| 3378 | BLOCK_SIZE bsize, int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3379 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3380 | MODE_INFO *const mic = xd->mi[0]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3381 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3382 | assert(!is_inter_block(mbmi)); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3383 | MB_MODE_INFO best_mbmi = *mbmi; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 3384 | int64_t best_model_rd = INT64_MAX; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3385 | #if CONFIG_EXT_INTRA |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 3386 | const int rows = block_size_high[bsize]; |
| 3387 | const int cols = block_size_wide[bsize]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3388 | int is_directional_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3389 | uint8_t directional_mode_skip_mask[INTRA_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3390 | const int src_stride = x->plane[0].src.stride; |
| 3391 | const uint8_t *src = x->plane[0].src.buf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3392 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3393 | #if CONFIG_FILTER_INTRA |
| 3394 | int beat_best_rd = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3395 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3396 | const int *bmode_costs; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3397 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 3398 | int palette_y_mode_ctx = 0; |
Hui Su | e87fb23 | 2017-10-05 15:00:15 -0700 | [diff] [blame] | 3399 | const int try_palette = |
| 3400 | av1_allow_palette(cpi->common.allow_screen_content_tools, mbmi->sb_type); |
| 3401 | uint8_t *best_palette_color_map = |
| 3402 | try_palette ? x->palette_buffer->best_palette_color_map : NULL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3403 | const MODE_INFO *above_mi = xd->above_mi; |
| 3404 | const MODE_INFO *left_mi = xd->left_mi; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3405 | const PREDICTION_MODE A = av1_above_block_mode(mic, above_mi, 0); |
| 3406 | const PREDICTION_MODE L = av1_left_block_mode(mic, left_mi, 0); |
Urvang Joshi | 96d1c0a | 2017-10-10 13:15:32 -0700 | [diff] [blame] | 3407 | const PREDICTION_MODE FINAL_MODE_SEARCH = PAETH_PRED + 1; |
Jingning Han | a45d842 | 2017-10-05 09:57:38 -0700 | [diff] [blame] | 3408 | |
| 3409 | #if CONFIG_KF_CTX |
| 3410 | const int above_ctx = intra_mode_context[A]; |
| 3411 | const int left_ctx = intra_mode_context[L]; |
| 3412 | bmode_costs = x->y_mode_costs[above_ctx][left_ctx]; |
| 3413 | #else |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3414 | bmode_costs = x->y_mode_costs[A][L]; |
Jingning Han | a45d842 | 2017-10-05 09:57:38 -0700 | [diff] [blame] | 3415 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3416 | |
| 3417 | #if CONFIG_EXT_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3418 | mbmi->angle_delta[0] = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3419 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3420 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3421 | highbd_angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3422 | directional_mode_skip_mask); |
| 3423 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3424 | #endif // CONFIG_HIGHBITDEPTH |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3425 | angle_estimation(src, src_stride, rows, cols, bsize, |
| 3426 | directional_mode_skip_mask); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3427 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3428 | #if CONFIG_FILTER_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3429 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3430 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3431 | pmi->palette_size[0] = 0; |
Hui Su | e87fb23 | 2017-10-05 15:00:15 -0700 | [diff] [blame] | 3432 | if (try_palette) { |
| 3433 | if (above_mi) { |
| 3434 | palette_y_mode_ctx += |
| 3435 | (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 3436 | } |
| 3437 | if (left_mi) { |
| 3438 | palette_y_mode_ctx += |
| 3439 | (left_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 3440 | } |
| 3441 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3442 | |
| 3443 | if (cpi->sf.tx_type_search.fast_intra_tx_type_search) |
| 3444 | x->use_default_intra_tx_type = 1; |
| 3445 | else |
| 3446 | x->use_default_intra_tx_type = 0; |
| 3447 | |
| 3448 | /* Y Search for intra prediction mode */ |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 3449 | for (int mode_idx = DC_PRED; mode_idx <= FINAL_MODE_SEARCH; ++mode_idx) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3450 | RD_STATS this_rd_stats; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 3451 | int this_rate, this_rate_tokenonly, s; |
| 3452 | int64_t this_distortion, this_rd, this_model_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3453 | if (mode_idx == FINAL_MODE_SEARCH) { |
| 3454 | if (x->use_default_intra_tx_type == 0) break; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3455 | mbmi->mode = best_mbmi.mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3456 | x->use_default_intra_tx_type = 0; |
| 3457 | } else { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 3458 | assert(mode_idx < INTRA_MODES); |
| 3459 | mbmi->mode = intra_rd_search_mode_order[mode_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3460 | } |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 3461 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3462 | #if CONFIG_EXT_INTRA |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 3463 | mbmi->angle_delta[0] = 0; |
| 3464 | #endif // CONFIG_EXT_INTRA |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3465 | this_model_rd = intra_model_yrd(cpi, x, bsize, bmode_costs[mbmi->mode]); |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 3466 | if (best_model_rd != INT64_MAX && |
| 3467 | this_model_rd > best_model_rd + (best_model_rd >> 1)) |
| 3468 | continue; |
| 3469 | if (this_model_rd < best_model_rd) best_model_rd = this_model_rd; |
| 3470 | #if CONFIG_EXT_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3471 | is_directional_mode = av1_is_directional_mode(mbmi->mode, bsize); |
| 3472 | if (is_directional_mode && directional_mode_skip_mask[mbmi->mode]) continue; |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 3473 | if (is_directional_mode && av1_use_angle_delta(bsize)) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3474 | this_rd_stats.rate = INT_MAX; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 3475 | rd_pick_intra_angle_sby(cpi, x, &this_rate, &this_rd_stats, bsize, |
| 3476 | bmode_costs[mbmi->mode], best_rd, &best_model_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3477 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3478 | super_block_yrd(cpi, x, &this_rd_stats, bsize, best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3479 | } |
| 3480 | #else |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3481 | super_block_yrd(cpi, x, &this_rd_stats, bsize, best_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3482 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3483 | this_rate_tokenonly = this_rd_stats.rate; |
| 3484 | this_distortion = this_rd_stats.dist; |
| 3485 | s = this_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3486 | |
| 3487 | if (this_rate_tokenonly == INT_MAX) continue; |
| 3488 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3489 | this_rate = this_rate_tokenonly + bmode_costs[mbmi->mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3490 | |
Rupert Swarbrick | fcff0b2 | 2017-10-05 09:26:04 +0100 | [diff] [blame] | 3491 | if (!xd->lossless[mbmi->segment_id] && |
| 3492 | block_signals_txsize(mbmi->sb_type)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3493 | // super_block_yrd above includes the cost of the tx_size in the |
| 3494 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 3495 | // (prediction granularity), so we account for it in the full rate, |
| 3496 | // not the tokenonly rate. |
Yue Chen | 3dd03e3 | 2017-10-17 15:39:52 -0700 | [diff] [blame] | 3497 | this_rate_tokenonly -= |
| 3498 | tx_size_cost(&cpi->common, x, bsize, mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3499 | } |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 3500 | if (try_palette && mbmi->mode == DC_PRED) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 3501 | this_rate += |
Yue Chen | dab2ca9 | 2017-10-16 17:48:48 -0700 | [diff] [blame] | 3502 | x->palette_y_mode_cost[bsize - BLOCK_8X8][palette_y_mode_ctx][0]; |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 3503 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3504 | #if CONFIG_FILTER_INTRA |
Yue Chen | 95e13e2 | 2017-11-01 23:56:35 -0700 | [diff] [blame] | 3505 | if (mbmi->mode == DC_PRED && av1_filter_intra_allowed_txsize(mbmi->tx_size)) |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 3506 | this_rate += x->filter_intra_cost[mbmi->tx_size][0]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3507 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3508 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3509 | if (is_directional_mode) { |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 3510 | if (av1_use_angle_delta(bsize)) { |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 3511 | #if CONFIG_EXT_INTRA_MOD |
| 3512 | this_rate += |
| 3513 | x->angle_delta_cost[mbmi->mode - V_PRED] |
| 3514 | [MAX_ANGLE_DELTA + mbmi->angle_delta[0]]; |
| 3515 | #else |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 3516 | this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 3517 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 3518 | #endif // CONFIG_EXT_INTRA_MOD |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 3519 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3520 | } |
| 3521 | #endif // CONFIG_EXT_INTRA |
Hui Su | 8dc0092 | 2017-09-14 16:15:55 -0700 | [diff] [blame] | 3522 | #if CONFIG_INTRABC |
| 3523 | if (bsize >= BLOCK_8X8 && cpi->common.allow_screen_content_tools) |
| 3524 | this_rate += x->intrabc_cost[0]; |
| 3525 | #endif // CONFIG_INTRABC |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3526 | this_rd = RDCOST(x->rdmult, this_rate, this_distortion); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3527 | |
| 3528 | if (this_rd < best_rd) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3529 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3530 | best_rd = this_rd; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3531 | #if CONFIG_FILTER_INTRA |
| 3532 | beat_best_rd = 1; |
| 3533 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3534 | *rate = this_rate; |
| 3535 | *rate_tokenonly = this_rate_tokenonly; |
| 3536 | *distortion = this_distortion; |
| 3537 | *skippable = s; |
| 3538 | } |
| 3539 | } |
| 3540 | |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 3541 | if (try_palette) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 3542 | rd_pick_palette_intra_sby(cpi, x, bsize, palette_y_mode_ctx, |
| 3543 | bmode_costs[DC_PRED], &best_mbmi, |
| 3544 | best_palette_color_map, &best_rd, &best_model_rd, |
| 3545 | rate, rate_tokenonly, distortion, skippable); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3546 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3547 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3548 | #if CONFIG_FILTER_INTRA |
Yue Chen | 18f6c15 | 2017-11-06 11:23:47 -0800 | [diff] [blame] | 3549 | if (beat_best_rd && av1_filter_intra_allowed_bsize(bsize) && |
| 3550 | !xd->lossless[mbmi->segment_id]) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3551 | if (rd_pick_filter_intra_sby(cpi, x, rate, rate_tokenonly, distortion, |
| 3552 | skippable, bsize, bmode_costs[DC_PRED], |
Yue Chen | da2eefc | 2017-11-16 15:25:28 -0800 | [diff] [blame] | 3553 | &best_rd, &best_model_rd)) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3554 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3555 | } |
| 3556 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3557 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3558 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3559 | *mbmi = best_mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3560 | return best_rd; |
| 3561 | } |
| 3562 | |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3563 | // Return value 0: early termination triggered, no valid rd cost available; |
| 3564 | // 1: rd cost values are valid. |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 3565 | static int super_block_uvrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 3566 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
| 3567 | int64_t ref_best_rd) { |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3568 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3569 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Luc Trudeau | 52301a2 | 2017-11-29 11:02:55 -0500 | [diff] [blame] | 3570 | struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_U]; |
| 3571 | const TX_SIZE uv_tx_size = av1_get_uv_tx_size(mbmi, pd); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3572 | int plane; |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3573 | int is_cost_valid = 1; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 3574 | av1_init_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3575 | |
| 3576 | if (ref_best_rd < 0) is_cost_valid = 0; |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 3577 | |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 3578 | if (x->skip_chroma_rd) return is_cost_valid; |
Jingning Han | 2d2dac2 | 2017-04-11 09:41:10 -0700 | [diff] [blame] | 3579 | |
Luc Trudeau | 52301a2 | 2017-11-29 11:02:55 -0500 | [diff] [blame] | 3580 | bsize = scale_chroma_bsize(bsize, pd->subsampling_x, pd->subsampling_y); |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 3581 | |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3582 | if (is_inter_block(mbmi) && is_cost_valid) { |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3583 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) |
| 3584 | av1_subtract_plane(x, bsize, plane); |
| 3585 | } |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3586 | |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 3587 | if (is_cost_valid) { |
| 3588 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 3589 | RD_STATS pn_rd_stats; |
| 3590 | txfm_rd_in_plane(x, cpi, &pn_rd_stats, ref_best_rd, plane, bsize, |
| 3591 | uv_tx_size, cpi->sf.use_fast_coef_costing); |
| 3592 | if (pn_rd_stats.rate == INT_MAX) { |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 3593 | is_cost_valid = 0; |
| 3594 | break; |
| 3595 | } |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 3596 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3597 | if (RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) > ref_best_rd && |
| 3598 | RDCOST(x->rdmult, 0, rd_stats->sse) > ref_best_rd) { |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 3599 | is_cost_valid = 0; |
| 3600 | break; |
| 3601 | } |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | if (!is_cost_valid) { |
| 3606 | // reset cost value |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 3607 | av1_invalid_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | return is_cost_valid; |
| 3611 | } |
| 3612 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3613 | void av1_tx_block_rd_b(const AV1_COMP *cpi, MACROBLOCK *x, TX_SIZE tx_size, |
| 3614 | int blk_row, int blk_col, int plane, int block, |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 3615 | int plane_bsize, const ENTROPY_CONTEXT *a, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3616 | const ENTROPY_CONTEXT *l, RD_STATS *rd_stats, int fast, |
| 3617 | TX_SIZE_RD_INFO *rd_info_array) { |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 3618 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3619 | MACROBLOCKD *xd = &x->e_mbd; |
| 3620 | const struct macroblock_plane *const p = &x->plane[plane]; |
| 3621 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yaowu Xu | 5f2749b | 2017-11-02 09:12:10 -0700 | [diff] [blame] | 3622 | #if DISABLE_TRELLISQ_SEARCH |
| 3623 | (void)fast; |
| 3624 | #endif |
Jingning Han | 243b66b | 2017-06-23 12:11:47 -0700 | [diff] [blame] | 3625 | #if CONFIG_TXK_SEL |
| 3626 | av1_search_txk_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize, |
| 3627 | tx_size, a, l, 0, rd_stats); |
| 3628 | return; |
| 3629 | #endif |
Debargha Mukherjee | d2cfbef | 2017-12-03 16:15:27 -0800 | [diff] [blame] | 3630 | // This function is used only for inter |
| 3631 | assert(is_inter_block(&xd->mi[0]->mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3632 | int64_t tmp; |
| 3633 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
Luc Trudeau | 005feb6 | 2017-02-22 13:34:01 -0500 | [diff] [blame] | 3634 | PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 3635 | TX_TYPE tx_type = |
| 3636 | av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size); |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 3637 | const SCAN_ORDER *const scan_order = |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 3638 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3639 | BLOCK_SIZE txm_bsize = txsize_to_bsize[tx_size]; |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 3640 | int bh = block_size_high[txm_bsize]; |
| 3641 | int bw = block_size_wide[txm_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3642 | int src_stride = p->src.stride; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 3643 | uint8_t *src = |
| 3644 | &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]]; |
| 3645 | uint8_t *dst = |
| 3646 | &pd->dst |
| 3647 | .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3648 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3649 | DECLARE_ALIGNED(16, uint16_t, rec_buffer16[MAX_TX_SQUARE]); |
| 3650 | uint8_t *rec_buffer; |
| 3651 | #else |
| 3652 | DECLARE_ALIGNED(16, uint8_t, rec_buffer[MAX_TX_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3653 | #endif // CONFIG_HIGHBITDEPTH |
Timothy B. Terriberry | d62e2a3 | 2017-06-10 16:04:21 -0700 | [diff] [blame] | 3654 | const int diff_stride = block_size_wide[plane_bsize]; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 3655 | const int16_t *diff = |
| 3656 | &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]]; |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 3657 | int txb_coeff_cost; |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 3658 | |
| 3659 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3660 | |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3661 | const int coeff_ctx = get_entropy_context(tx_size, a, l); |
| 3662 | const int coeff_ctx_one_byte = combine_entropy_contexts(*a, *l); |
| 3663 | const uint8_t cur_joint_ctx = (coeff_ctx << 2) + coeff_ctx_one_byte; |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 3664 | |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 3665 | // Note: tmp below is pixel distortion, not TX domain |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 3666 | tmp = pixel_diff_dist(x, plane, diff, diff_stride, blk_row, blk_col, |
| 3667 | plane_bsize, txm_bsize); |
| 3668 | |
| 3669 | #if CONFIG_HIGHBITDEPTH |
| 3670 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 3671 | tmp = ROUND_POWER_OF_TWO(tmp, (xd->bd - 8) * 2); |
| 3672 | #endif // CONFIG_HIGHBITDEPTH |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3673 | |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 3674 | rd_stats->sse += tmp << 4; |
| 3675 | |
| 3676 | if (rd_stats->invalid_rate) { |
| 3677 | rd_stats->dist += tmp << 4; |
| 3678 | rd_stats->rate += rd_stats->zero_rate; |
| 3679 | rd_stats->skip = 1; |
| 3680 | return; |
| 3681 | } |
| 3682 | |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3683 | // Look up RD and terminate early in case when we've already processed exactly |
| 3684 | // the same residual with exactly the same entropy context. |
| 3685 | if (rd_info_array != NULL && rd_info_array[tx_type].valid && |
| 3686 | rd_info_array[tx_type].entropy_context == cur_joint_ctx && |
| 3687 | rd_info_array[tx_type].fast == fast) { |
| 3688 | rd_stats->dist += rd_info_array[tx_type].dist; |
| 3689 | rd_stats->rate += rd_info_array[tx_type].rate; |
Hui Su | 8c2b913 | 2017-12-09 10:40:15 -0800 | [diff] [blame] | 3690 | rd_stats->skip &= rd_info_array[tx_type].eob == 0; |
| 3691 | p->eobs[block] = rd_info_array[tx_type].eob; |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3692 | return; |
| 3693 | } |
| 3694 | |
| 3695 | int64_t cur_dist = 0; |
| 3696 | int cur_rate = 0; |
| 3697 | uint8_t cur_skip = 1; |
| 3698 | |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 3699 | // TODO(any): Use av1_dist_block to compute distortion |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3700 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3701 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 3702 | rec_buffer = CONVERT_TO_BYTEPTR(rec_buffer16); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3703 | 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] | 3704 | 0, NULL, 0, bw, bh, xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3705 | } else { |
| 3706 | rec_buffer = (uint8_t *)rec_buffer16; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3707 | 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] | 3708 | NULL, 0, bw, bh); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3709 | } |
| 3710 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3711 | 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] | 3712 | 0, bw, bh); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3713 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3714 | |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3715 | #if DISABLE_TRELLISQ_SEARCH |
| 3716 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Sarah Parker | e8d0d4c | 2017-12-06 15:11:37 -0800 | [diff] [blame] | 3717 | AV1_XFORM_QUANT_B); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3718 | |
| 3719 | #else |
| 3720 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Sarah Parker | e8d0d4c | 2017-12-06 15:11:37 -0800 | [diff] [blame] | 3721 | AV1_XFORM_QUANT_FP); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3722 | |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 3723 | // TX-domain results need to shift down to Q2/D10 to match pixel |
| 3724 | // domain distortion values which are in Q2^2 |
| 3725 | #if CONFIG_DAALA_TX |
| 3726 | const int shift = (TX_COEFF_DEPTH - 10) * 2; |
| 3727 | #else |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3728 | const int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
Monty Montgomery | 4a05a58 | 2017-11-01 21:21:07 -0400 | [diff] [blame] | 3729 | #endif |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3730 | tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
Urvang Joshi | 8089315 | 2017-10-27 11:51:14 -0700 | [diff] [blame] | 3731 | const int buffer_length = av1_get_max_eob(tx_size); |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 3732 | int64_t tmp_dist, tmp_sse; |
Yunqing Wang | 24d2d5d | 2017-09-20 09:45:13 -0700 | [diff] [blame] | 3733 | #if CONFIG_DIST_8X8 |
Yushin Cho | f9970a5 | 2017-10-13 12:57:13 -0700 | [diff] [blame] | 3734 | int blk_w = block_size_wide[plane_bsize]; |
| 3735 | int blk_h = block_size_high[plane_bsize]; |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 3736 | int disable_early_skip = |
Yushin Cho | f9970a5 | 2017-10-13 12:57:13 -0700 | [diff] [blame] | 3737 | x->using_dist_8x8 && plane == 0 && blk_w >= 8 && blk_h >= 8 && |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 3738 | (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4) && |
| 3739 | x->tune_metric != AOM_TUNE_PSNR; |
Yunqing Wang | 24d2d5d | 2017-09-20 09:45:13 -0700 | [diff] [blame] | 3740 | #endif // CONFIG_DIST_8X8 |
| 3741 | |
Monty Montgomery | a26262c | 2017-10-31 07:32:13 -0400 | [diff] [blame] | 3742 | #if CONFIG_DAALA_TX |
| 3743 | tmp_dist = |
| 3744 | av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp_sse, xd->bd); |
| 3745 | #else |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3746 | #if CONFIG_HIGHBITDEPTH |
| 3747 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 3748 | tmp_dist = |
| 3749 | av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp_sse, xd->bd); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3750 | else |
| 3751 | #endif |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 3752 | tmp_dist = av1_block_error(coeff, dqcoeff, buffer_length, &tmp_sse); |
Monty Montgomery | a26262c | 2017-10-31 07:32:13 -0400 | [diff] [blame] | 3753 | #endif |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 3754 | |
| 3755 | tmp_dist = RIGHT_SIGNED_SHIFT(tmp_dist, shift); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3756 | |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 3757 | if ( |
| 3758 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 3759 | disable_early_skip || |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 3760 | #endif |
| 3761 | RDCOST(x->rdmult, 0, tmp_dist) < rd_stats->ref_rdcost) { |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3762 | av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size, |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 3763 | a, l, fast); |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 3764 | } else { |
| 3765 | rd_stats->rate += rd_stats->zero_rate; |
Yushin Cho | 952eae2 | 2017-10-03 16:21:06 -0700 | [diff] [blame] | 3766 | rd_stats->dist += tmp << 4; |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 3767 | rd_stats->skip = 1; |
| 3768 | rd_stats->invalid_rate = 1; |
| 3769 | return; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3770 | } |
| 3771 | #endif // DISABLE_TRELLISQ_SEARCH |
| 3772 | |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 3773 | const int eob = p->eobs[block]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3774 | |
Sebastien Alaiwan | 9f001f3 | 2017-11-28 16:32:33 +0100 | [diff] [blame] | 3775 | av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, rec_buffer, |
| 3776 | MAX_TX_SIZE, eob, cm->reduced_tx_set_used); |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 3777 | if (eob > 0) { |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3778 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 3779 | if (x->using_dist_8x8 && plane == 0 && (bw < 8 && bh < 8)) { |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 3780 | // Save sub8x8 luma decoded pixels |
| 3781 | // since 8x8 luma decoded pixels are not available for daala-dist |
| 3782 | // after recursive split of BLOCK_8x8 is done. |
| 3783 | const int pred_stride = block_size_wide[plane_bsize]; |
| 3784 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 3785 | << tx_size_wide_log2[0]; |
| 3786 | int16_t *decoded = &pd->pred[pred_idx]; |
| 3787 | int i, j; |
| 3788 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 3789 | #if CONFIG_HIGHBITDEPTH |
| 3790 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 3791 | for (j = 0; j < bh; j++) |
| 3792 | for (i = 0; i < bw; i++) |
| 3793 | decoded[j * pred_stride + i] = |
| 3794 | CONVERT_TO_SHORTPTR(rec_buffer)[j * MAX_TX_SIZE + i]; |
| 3795 | } else { |
| 3796 | #endif |
| 3797 | for (j = 0; j < bh; j++) |
| 3798 | for (i = 0; i < bw; i++) |
| 3799 | decoded[j * pred_stride + i] = rec_buffer[j * MAX_TX_SIZE + i]; |
| 3800 | #if CONFIG_HIGHBITDEPTH |
| 3801 | } |
| 3802 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 3803 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3804 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 3805 | tmp = pixel_dist(cpi, x, plane, src, src_stride, rec_buffer, MAX_TX_SIZE, |
| 3806 | blk_row, blk_col, plane_bsize, txm_bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3807 | } |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3808 | cur_dist = tmp * 16; |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3809 | txb_coeff_cost = av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block, |
| 3810 | tx_size, scan_order, a, l, 0); |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3811 | cur_rate = txb_coeff_cost; |
| 3812 | cur_skip = (eob == 0); |
| 3813 | |
| 3814 | // Save RD results for possible reuse in future. |
| 3815 | if (rd_info_array != NULL) { |
| 3816 | rd_info_array[tx_type].valid = 1; |
| 3817 | rd_info_array[tx_type].entropy_context = cur_joint_ctx; |
| 3818 | rd_info_array[tx_type].fast = fast; |
| 3819 | rd_info_array[tx_type].dist = cur_dist; |
| 3820 | rd_info_array[tx_type].rate = cur_rate; |
Hui Su | 8c2b913 | 2017-12-09 10:40:15 -0800 | [diff] [blame] | 3821 | rd_info_array[tx_type].eob = eob; |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | rd_stats->dist += cur_dist; |
| 3825 | rd_stats->rate += cur_rate; |
| 3826 | rd_stats->skip &= cur_skip; |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 3827 | |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 3828 | #if CONFIG_RD_DEBUG |
Angie Chiang | e94556b | 2016-11-09 10:59:30 -0800 | [diff] [blame] | 3829 | av1_update_txb_coeff_cost(rd_stats, plane, tx_size, blk_row, blk_col, |
| 3830 | txb_coeff_cost); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3831 | #endif // CONFIG_RD_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3832 | } |
| 3833 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3834 | static void select_tx_block(const AV1_COMP *cpi, MACROBLOCK *x, int blk_row, |
Debargha Mukherjee | 9245d89 | 2017-10-06 13:54:31 -0700 | [diff] [blame] | 3835 | int blk_col, int plane, int block, TX_SIZE tx_size, |
| 3836 | int depth, BLOCK_SIZE plane_bsize, |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 3837 | ENTROPY_CONTEXT *ta, ENTROPY_CONTEXT *tl, |
| 3838 | TXFM_CONTEXT *tx_above, TXFM_CONTEXT *tx_left, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3839 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 3840 | int *is_cost_valid, int fast, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3841 | int tx_split_prune_flag, |
| 3842 | TX_SIZE_RD_INFO_NODE *rd_info_node) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3843 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3844 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 3845 | struct macroblock_plane *const p = &x->plane[plane]; |
| 3846 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 3847 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 3848 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 3849 | TX_SIZE(*const inter_tx_size) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3850 | [MAX_MIB_SIZE] = |
| 3851 | (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] | 3852 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 3853 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 3854 | 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] | 3855 | int64_t this_rd = INT64_MAX; |
| 3856 | ENTROPY_CONTEXT *pta = ta + blk_col; |
| 3857 | ENTROPY_CONTEXT *ptl = tl + blk_row; |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 3858 | int ctx = txfm_partition_context(tx_above + blk_col, tx_left + blk_row, |
| 3859 | mbmi->sb_type, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3860 | int64_t sum_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3861 | int tmp_eob = 0; |
| 3862 | int zero_blk_rate; |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 3863 | RD_STATS sum_rd_stats; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 3864 | #if CONFIG_TXK_SEL |
| 3865 | TX_TYPE best_tx_type = TX_TYPES; |
Angie Chiang | bce07f1 | 2017-12-01 16:34:31 -0800 | [diff] [blame] | 3866 | int txk_idx = (blk_row << MAX_MIB_SIZE_LOG2) + blk_col; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 3867 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3868 | |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 3869 | av1_init_rd_stats(&sum_rd_stats); |
Jingning Han | fe45b21 | 2016-11-22 10:30:23 -0800 | [diff] [blame] | 3870 | |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 3871 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3872 | |
| 3873 | if (ref_best_rd < 0) { |
| 3874 | *is_cost_valid = 0; |
| 3875 | return; |
| 3876 | } |
| 3877 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 3878 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3879 | |
| 3880 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 3881 | |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 3882 | #if CONFIG_LV_MAP |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 3883 | TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 3884 | TXB_CTX txb_ctx; |
| 3885 | get_txb_ctx(plane_bsize, tx_size, plane, pta, ptl, &txb_ctx); |
Jingning Han | e981491 | 2017-08-31 16:38:59 -0700 | [diff] [blame] | 3886 | |
Jingning Han | e981491 | 2017-08-31 16:38:59 -0700 | [diff] [blame] | 3887 | zero_blk_rate = x->coeff_costs[txs_ctx][get_plane_type(plane)] |
| 3888 | .txb_skip_cost[txb_ctx.txb_skip_ctx][1]; |
| 3889 | #else |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 3890 | TX_SIZE tx_size_ctx = get_txsize_entropy_ctx(tx_size); |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 3891 | int coeff_ctx = get_entropy_context(tx_size, pta, ptl); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 3892 | zero_blk_rate = |
| 3893 | x->token_head_costs[tx_size_ctx][pd->plane_type][1][0][coeff_ctx][0]; |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 3894 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3895 | |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 3896 | rd_stats->ref_rdcost = ref_best_rd; |
| 3897 | rd_stats->zero_rate = zero_blk_rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3898 | if (cpi->common.tx_mode == TX_MODE_SELECT || tx_size == TX_4X4) { |
| 3899 | inter_tx_size[0][0] = tx_size; |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3900 | av1_tx_block_rd_b( |
| 3901 | cpi, x, tx_size, blk_row, blk_col, plane, block, plane_bsize, pta, ptl, |
| 3902 | rd_stats, fast, |
| 3903 | rd_info_node != NULL ? rd_info_node->rd_info_array : NULL); |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 3904 | if (rd_stats->rate == INT_MAX) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3905 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3906 | if ((RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) >= |
| 3907 | RDCOST(x->rdmult, zero_blk_rate, rd_stats->sse) || |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3908 | rd_stats->skip == 1) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3909 | !xd->lossless[mbmi->segment_id]) { |
Jingning Han | c7ea761 | 2017-01-11 15:01:30 -0800 | [diff] [blame] | 3910 | #if CONFIG_RD_DEBUG |
| 3911 | av1_update_txb_coeff_cost(rd_stats, plane, tx_size, blk_row, blk_col, |
| 3912 | zero_blk_rate - rd_stats->rate); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3913 | #endif // CONFIG_RD_DEBUG |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3914 | rd_stats->rate = zero_blk_rate; |
| 3915 | rd_stats->dist = rd_stats->sse; |
| 3916 | rd_stats->skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3917 | x->blk_skip[plane][blk_row * bw + blk_col] = 1; |
| 3918 | p->eobs[block] = 0; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 3919 | #if CONFIG_TXK_SEL |
| 3920 | mbmi->txk_type[txk_idx] = DCT_DCT; |
| 3921 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3922 | } else { |
| 3923 | x->blk_skip[plane][blk_row * bw + blk_col] = 0; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3924 | rd_stats->skip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3925 | } |
| 3926 | |
Jingning Han | 571189c | 2016-10-24 10:38:43 -0700 | [diff] [blame] | 3927 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) |
Yue Chen | 171c17d | 2017-10-16 18:08:22 -0700 | [diff] [blame] | 3928 | rd_stats->rate += x->txfm_partition_cost[ctx][0]; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3929 | this_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 3930 | #if CONFIG_LV_MAP |
| 3931 | tmp_eob = p->txb_entropy_ctx[block]; |
| 3932 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3933 | tmp_eob = p->eobs[block]; |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 3934 | #endif |
| 3935 | |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 3936 | #if CONFIG_TXK_SEL |
| 3937 | best_tx_type = mbmi->txk_type[txk_idx]; |
| 3938 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3939 | } |
| 3940 | |
Sebastien Alaiwan | 9f001f3 | 2017-11-28 16:32:33 +0100 | [diff] [blame] | 3941 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH && tx_split_prune_flag == 0) { |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 3942 | const TX_SIZE sub_txs = sub_tx_size_map[1][tx_size]; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 3943 | const int bsw = tx_size_wide_unit[sub_txs]; |
| 3944 | const int bsh = tx_size_high_unit[sub_txs]; |
| 3945 | int sub_step = bsw * bsh; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 3946 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3947 | int this_cost_valid = 1; |
| 3948 | int64_t tmp_rd = 0; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3949 | #if CONFIG_DIST_8X8 |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 3950 | int sub8x8_eob[4] = { 0, 0, 0, 0 }; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 3951 | #endif |
Yue Chen | 171c17d | 2017-10-16 18:08:22 -0700 | [diff] [blame] | 3952 | sum_rd_stats.rate = x->txfm_partition_cost[ctx][1]; |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 3953 | |
| 3954 | assert(tx_size < TX_SIZES_ALL); |
| 3955 | |
Jingning Han | 16a9df7 | 2017-07-26 15:27:43 -0700 | [diff] [blame] | 3956 | ref_best_rd = AOMMIN(this_rd, ref_best_rd); |
| 3957 | |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3958 | int blk_idx = 0; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 3959 | for (int r = 0; r < tx_size_high_unit[tx_size]; r += bsh) { |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3960 | for (int c = 0; c < tx_size_wide_unit[tx_size]; c += bsw, ++blk_idx) { |
| 3961 | const int offsetr = blk_row + r; |
| 3962 | const int offsetc = blk_col + c; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 3963 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 3964 | assert(blk_idx < 4); |
| 3965 | select_tx_block( |
| 3966 | cpi, x, offsetr, offsetc, plane, block, sub_txs, depth + 1, |
| 3967 | plane_bsize, ta, tl, tx_above, tx_left, &this_rd_stats, |
| 3968 | ref_best_rd - tmp_rd, &this_cost_valid, fast, 0, |
| 3969 | (rd_info_node != NULL) ? rd_info_node->children[blk_idx] : NULL); |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 3970 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3971 | #if CONFIG_DIST_8X8 |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 3972 | if (!x->using_dist_8x8) |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 3973 | #endif |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 3974 | if (!this_cost_valid) break; |
| 3975 | #if CONFIG_DIST_8X8 |
| 3976 | if (x->using_dist_8x8 && plane == 0 && tx_size == TX_8X8) { |
| 3977 | sub8x8_eob[2 * (r / bsh) + (c / bsw)] = p->eobs[block]; |
| 3978 | } |
| 3979 | #endif // CONFIG_DIST_8X8 |
| 3980 | av1_merge_rd_stats(&sum_rd_stats, &this_rd_stats); |
| 3981 | |
| 3982 | tmp_rd = RDCOST(x->rdmult, sum_rd_stats.rate, sum_rd_stats.dist); |
| 3983 | #if CONFIG_DIST_8X8 |
| 3984 | if (!x->using_dist_8x8) |
| 3985 | #endif |
| 3986 | if (this_rd < tmp_rd) break; |
| 3987 | block += sub_step; |
| 3988 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3989 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3990 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 3991 | if (x->using_dist_8x8 && this_cost_valid && plane == 0 && |
| 3992 | tx_size == TX_8X8) { |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 3993 | const int src_stride = p->src.stride; |
| 3994 | const int dst_stride = pd->dst.stride; |
| 3995 | |
| 3996 | const uint8_t *src = |
| 3997 | &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]]; |
| 3998 | const uint8_t *dst = |
| 3999 | &pd->dst |
| 4000 | .buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]]; |
| 4001 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4002 | int64_t dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4003 | int qindex = x->qindex; |
| 4004 | const int pred_stride = block_size_wide[plane_bsize]; |
| 4005 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 4006 | << tx_size_wide_log2[0]; |
| 4007 | int16_t *pred = &pd->pred[pred_idx]; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 4008 | int i, j; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4009 | int row, col; |
| 4010 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4011 | #if CONFIG_HIGHBITDEPTH |
| 4012 | uint8_t *pred8; |
| 4013 | DECLARE_ALIGNED(16, uint16_t, pred8_16[8 * 8]); |
| 4014 | #else |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4015 | DECLARE_ALIGNED(16, uint8_t, pred8[8 * 8]); |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4016 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4017 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 4018 | dist_8x8 = av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride, |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4019 | BLOCK_8X8, 8, 8, 8, 8, qindex) * |
| 4020 | 16; |
Yushin Cho | f986af1 | 2017-11-20 15:22:43 -0800 | [diff] [blame] | 4021 | if (x->tune_metric == AOM_TUNE_PSNR && xd->bd == 8) |
| 4022 | assert(sum_rd_stats.sse == dist_8x8); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4023 | sum_rd_stats.sse = dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4024 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4025 | #if CONFIG_HIGHBITDEPTH |
| 4026 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 4027 | pred8 = CONVERT_TO_BYTEPTR(pred8_16); |
| 4028 | else |
| 4029 | pred8 = (uint8_t *)pred8_16; |
| 4030 | #endif |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4031 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4032 | #if CONFIG_HIGHBITDEPTH |
| 4033 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 4034 | for (row = 0; row < 2; ++row) { |
| 4035 | for (col = 0; col < 2; ++col) { |
| 4036 | int idx = row * 2 + col; |
| 4037 | int eob = sub8x8_eob[idx]; |
| 4038 | |
| 4039 | if (eob > 0) { |
| 4040 | for (j = 0; j < 4; j++) |
| 4041 | for (i = 0; i < 4; i++) |
| 4042 | CONVERT_TO_SHORTPTR(pred8) |
| 4043 | [(row * 4 + j) * 8 + 4 * col + i] = |
| 4044 | pred[(row * 4 + j) * pred_stride + 4 * col + i]; |
| 4045 | } else { |
| 4046 | for (j = 0; j < 4; j++) |
| 4047 | for (i = 0; i < 4; i++) |
| 4048 | CONVERT_TO_SHORTPTR(pred8) |
| 4049 | [(row * 4 + j) * 8 + 4 * col + i] = CONVERT_TO_SHORTPTR( |
| 4050 | dst)[(row * 4 + j) * dst_stride + 4 * col + i]; |
| 4051 | } |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4052 | } |
| 4053 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4054 | } else { |
| 4055 | #endif |
| 4056 | for (row = 0; row < 2; ++row) { |
| 4057 | for (col = 0; col < 2; ++col) { |
| 4058 | int idx = row * 2 + col; |
| 4059 | int eob = sub8x8_eob[idx]; |
| 4060 | |
| 4061 | if (eob > 0) { |
| 4062 | for (j = 0; j < 4; j++) |
| 4063 | for (i = 0; i < 4; i++) |
| 4064 | pred8[(row * 4 + j) * 8 + 4 * col + i] = |
Yaowu Xu | 7a47170 | 2017-09-29 08:38:37 -0700 | [diff] [blame] | 4065 | (uint8_t)pred[(row * 4 + j) * pred_stride + 4 * col + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4066 | } else { |
| 4067 | for (j = 0; j < 4; j++) |
| 4068 | for (i = 0; i < 4; i++) |
| 4069 | pred8[(row * 4 + j) * 8 + 4 * col + i] = |
| 4070 | dst[(row * 4 + j) * dst_stride + 4 * col + i]; |
| 4071 | } |
| 4072 | } |
| 4073 | } |
| 4074 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4075 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4076 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 4077 | dist_8x8 = av1_dist_8x8(cpi, x, src, src_stride, pred8, 8, BLOCK_8X8, 8, |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4078 | 8, 8, 8, qindex) * |
| 4079 | 16; |
Yushin Cho | f986af1 | 2017-11-20 15:22:43 -0800 | [diff] [blame] | 4080 | if (x->tune_metric == AOM_TUNE_PSNR && xd->bd == 8) |
Yushin Cho | 1cd3462 | 2017-10-06 13:00:41 -0700 | [diff] [blame] | 4081 | assert(sum_rd_stats.dist == dist_8x8); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4082 | sum_rd_stats.dist = dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4083 | tmp_rd = RDCOST(x->rdmult, sum_rd_stats.rate, sum_rd_stats.dist); |
| 4084 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4085 | #endif // CONFIG_DIST_8X8 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4086 | if (this_cost_valid) sum_rd = tmp_rd; |
| 4087 | } |
| 4088 | |
| 4089 | if (this_rd < sum_rd) { |
| 4090 | int idx, idy; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4091 | TX_SIZE tx_size_selected = tx_size; |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4092 | |
| 4093 | #if CONFIG_LV_MAP |
Debargha Mukherjee | 35a4db3 | 2017-11-14 11:58:16 -0800 | [diff] [blame] | 4094 | p->txb_entropy_ctx[block] = tmp_eob; |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4095 | #else |
| 4096 | p->eobs[block] = tmp_eob; |
| 4097 | #endif |
| 4098 | |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4099 | av1_set_txb_context(x, plane, block, tx_size_selected, pta, ptl); |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4100 | |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 4101 | txfm_partition_update(tx_above + blk_col, tx_left + blk_row, tx_size, |
| 4102 | tx_size); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4103 | inter_tx_size[0][0] = tx_size_selected; |
Yue Chen | c5252a6 | 2017-10-31 15:41:12 -0700 | [diff] [blame] | 4104 | for (idy = 0; idy < AOMMAX(1, tx_size_high_unit[tx_size] / 2); ++idy) |
| 4105 | for (idx = 0; idx < AOMMAX(1, tx_size_wide_unit[tx_size] / 2); ++idx) |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4106 | inter_tx_size[idy][idx] = tx_size_selected; |
| 4107 | mbmi->tx_size = tx_size_selected; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4108 | #if CONFIG_TXK_SEL |
| 4109 | mbmi->txk_type[txk_idx] = best_tx_type; |
| 4110 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4111 | if (this_rd == INT64_MAX) *is_cost_valid = 0; |
Debargha Mukherjee | 35a4db3 | 2017-11-14 11:58:16 -0800 | [diff] [blame] | 4112 | x->blk_skip[plane][blk_row * bw + blk_col] = rd_stats->skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4113 | } else { |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 4114 | *rd_stats = sum_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4115 | if (sum_rd == INT64_MAX) *is_cost_valid = 0; |
| 4116 | } |
| 4117 | } |
| 4118 | |
Debargha Mukherjee | edc7346 | 2017-10-31 15:13:32 -0700 | [diff] [blame] | 4119 | static int get_search_init_depth(int mi_width, int mi_height, |
| 4120 | const SPEED_FEATURES *sf) { |
| 4121 | if (sf->tx_size_search_method == USE_LARGESTALL) return MAX_VARTX_DEPTH; |
| 4122 | return (mi_height != mi_width) ? sf->tx_size_search_init_depth_rect |
| 4123 | : sf->tx_size_search_init_depth_sqr; |
| 4124 | } |
| 4125 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4126 | static void select_inter_block_yrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 4127 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 4128 | int64_t ref_best_rd, int fast, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4129 | int tx_split_prune_flag, |
| 4130 | TX_SIZE_RD_INFO_NODE *rd_info_tree) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4131 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4132 | int is_cost_valid = 1; |
| 4133 | int64_t this_rd = 0; |
| 4134 | |
| 4135 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 4136 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4137 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4138 | |
| 4139 | if (is_cost_valid) { |
| 4140 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 4141 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 4142 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 4143 | const int mi_height = block_size_high[plane_bsize] >> tx_size_high_log2[0]; |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 4144 | const TX_SIZE max_tx_size = get_max_rect_tx_size(plane_bsize, 1); |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 4145 | const int bh = tx_size_high_unit[max_tx_size]; |
| 4146 | const int bw = tx_size_wide_unit[max_tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4147 | int idx, idy; |
| 4148 | int block = 0; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 4149 | 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] | 4150 | ENTROPY_CONTEXT ctxa[2 * MAX_MIB_SIZE]; |
| 4151 | ENTROPY_CONTEXT ctxl[2 * MAX_MIB_SIZE]; |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 4152 | TXFM_CONTEXT tx_above[MAX_MIB_SIZE * 2]; |
| 4153 | TXFM_CONTEXT tx_left[MAX_MIB_SIZE * 2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4154 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4155 | RD_STATS pn_rd_stats; |
Debargha Mukherjee | edc7346 | 2017-10-31 15:13:32 -0700 | [diff] [blame] | 4156 | const int init_depth = get_search_init_depth(mi_width, mi_height, &cpi->sf); |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4157 | av1_init_rd_stats(&pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4158 | |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 4159 | av1_get_entropy_contexts(bsize, 0, pd, ctxa, ctxl); |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 4160 | memcpy(tx_above, xd->above_txfm_context, sizeof(TXFM_CONTEXT) * mi_width); |
| 4161 | memcpy(tx_left, xd->left_txfm_context, sizeof(TXFM_CONTEXT) * mi_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4162 | |
| 4163 | for (idy = 0; idy < mi_height; idy += bh) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 4164 | for (idx = 0; idx < mi_width; idx += bw) { |
Sarah Parker | d25ef8c | 2017-10-06 12:17:30 -0700 | [diff] [blame] | 4165 | select_tx_block(cpi, x, idy, idx, 0, block, max_tx_size, init_depth, |
| 4166 | plane_bsize, ctxa, ctxl, tx_above, tx_left, |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4167 | &pn_rd_stats, ref_best_rd - this_rd, &is_cost_valid, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4168 | fast, tx_split_prune_flag, rd_info_tree); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4169 | if (!is_cost_valid || pn_rd_stats.rate == INT_MAX) { |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4170 | av1_invalid_rd_stats(rd_stats); |
| 4171 | return; |
| 4172 | } |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4173 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Debargha Mukherjee | 2c50f9a | 2017-11-15 08:04:57 -0800 | [diff] [blame] | 4174 | this_rd += |
| 4175 | AOMMIN(RDCOST(x->rdmult, pn_rd_stats.rate, pn_rd_stats.dist), |
| 4176 | RDCOST(x->rdmult, pn_rd_stats.zero_rate, pn_rd_stats.sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4177 | block += step; |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4178 | if (rd_info_tree != NULL) rd_info_tree += 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4179 | } |
| 4180 | } |
| 4181 | } |
Debargha Mukherjee | 9c8decb | 2017-12-01 15:14:40 -0800 | [diff] [blame] | 4182 | int64_t zero_rd = RDCOST(x->rdmult, rd_stats->zero_rate, rd_stats->sse); |
| 4183 | this_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
| 4184 | if (zero_rd < this_rd) { |
| 4185 | this_rd = zero_rd; |
| 4186 | rd_stats->skip = 1; |
| 4187 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4188 | if (this_rd > ref_best_rd) is_cost_valid = 0; |
| 4189 | |
| 4190 | if (!is_cost_valid) { |
| 4191 | // reset cost value |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4192 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4196 | 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] | 4197 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4198 | int mi_row, int mi_col, |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 4199 | int64_t ref_best_rd, TX_TYPE tx_type, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4200 | int tx_split_prune_flag, |
| 4201 | TX_SIZE_RD_INFO_NODE *rd_info_tree) { |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4202 | const int fast = cpi->sf.tx_size_search_method > USE_FULL_RD; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4203 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4204 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4205 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4206 | const int is_inter = is_inter_block(mbmi); |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 4207 | const int skip_ctx = av1_get_skip_context(xd); |
| 4208 | int s0 = x->skip_cost[skip_ctx][0]; |
| 4209 | int s1 = x->skip_cost[skip_ctx][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4210 | int64_t rd; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 4211 | int row, col; |
| 4212 | const int max_blocks_high = max_block_high(xd, bsize, 0); |
| 4213 | const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4214 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4215 | // TODO(debargha): enable this as a speed feature where the |
| 4216 | // select_inter_block_yrd() function above will use a simplified search |
| 4217 | // such as not using full optimize, but the inter_block_yrd() function |
| 4218 | // will use more complex search given that the transform partitions have |
| 4219 | // already been decided. |
| 4220 | |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 4221 | (void)cm; |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4222 | (void)mi_row; |
| 4223 | (void)mi_col; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 4224 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4225 | mbmi->tx_type = tx_type; |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 4226 | select_inter_block_yrd(cpi, x, rd_stats, bsize, ref_best_rd, fast, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4227 | tx_split_prune_flag, rd_info_tree); |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4228 | if (rd_stats->rate == INT_MAX) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4229 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4230 | mbmi->min_tx_size = get_min_tx_size(mbmi->inter_tx_size[0][0]); |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 4231 | for (row = 0; row < max_blocks_high / 2; ++row) |
| 4232 | for (col = 0; col < max_blocks_wide / 2; ++col) |
| 4233 | mbmi->min_tx_size = AOMMIN( |
| 4234 | mbmi->min_tx_size, get_min_tx_size(mbmi->inter_tx_size[row][col])); |
| 4235 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4236 | if (fast) { |
| 4237 | // Do a better (non-fast) search with tx sizes already decided. |
| 4238 | // Currently, trellis optimization is turned on only for this pass, and |
| 4239 | // the function below performs a more accurate rd cost calculation based |
| 4240 | // on that. |
| 4241 | if (!inter_block_yrd(cpi, x, rd_stats, bsize, ref_best_rd, 0)) |
| 4242 | return INT64_MAX; |
| 4243 | } |
| 4244 | |
Jingning Han | 1643a0a | 2017-07-05 15:48:25 -0700 | [diff] [blame] | 4245 | #if !CONFIG_TXK_SEL |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 4246 | if (get_ext_tx_types(mbmi->min_tx_size, bsize, is_inter, |
| 4247 | cm->reduced_tx_set_used) > 1 && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4248 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 4249 | const int ext_tx_set = get_ext_tx_set(mbmi->min_tx_size, bsize, is_inter, |
| 4250 | cm->reduced_tx_set_used); |
Sebastien Alaiwan | 7fc6b2a | 2017-11-02 18:14:50 +0100 | [diff] [blame] | 4251 | if (is_inter) { |
| 4252 | if (ext_tx_set > 0) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4253 | rd_stats->rate += |
Sebastien Alaiwan | 7fc6b2a | 2017-11-02 18:14:50 +0100 | [diff] [blame] | 4254 | x->inter_tx_type_costs[ext_tx_set] |
| 4255 | [txsize_sqr_map[mbmi->min_tx_size]] |
| 4256 | [mbmi->tx_type]; |
| 4257 | } else { |
| 4258 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) { |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 4259 | #if CONFIG_FILTER_INTRA |
Sebastien Alaiwan | 7fc6b2a | 2017-11-02 18:14:50 +0100 | [diff] [blame] | 4260 | PREDICTION_MODE intra_dir; |
| 4261 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) |
| 4262 | intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info |
| 4263 | .filter_intra_mode[0]]; |
| 4264 | else |
| 4265 | intra_dir = mbmi->mode; |
| 4266 | rd_stats->rate += x->intra_tx_type_costs[ext_tx_set][mbmi->min_tx_size] |
| 4267 | [intra_dir][mbmi->tx_type]; |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 4268 | #else |
| 4269 | rd_stats->rate += x->intra_tx_type_costs[ext_tx_set][mbmi->min_tx_size] |
| 4270 | [mbmi->mode][mbmi->tx_type]; |
| 4271 | #endif |
Lester Lu | 432012f | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 4272 | } |
| 4273 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4274 | } |
Jingning Han | 1643a0a | 2017-07-05 15:48:25 -0700 | [diff] [blame] | 4275 | #endif // CONFIG_TXK_SEL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4276 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4277 | if (rd_stats->skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4278 | rd = RDCOST(x->rdmult, s1, rd_stats->sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4279 | else |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4280 | rd = RDCOST(x->rdmult, rd_stats->rate + s0, rd_stats->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4281 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4282 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 4283 | !(rd_stats->skip)) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4284 | rd = AOMMIN(rd, RDCOST(x->rdmult, s1, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4285 | |
| 4286 | return rd; |
| 4287 | } |
| 4288 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4289 | // Finds rd cost for a y block, given the transform size partitions |
| 4290 | static void tx_block_yrd(const AV1_COMP *cpi, MACROBLOCK *x, int blk_row, |
| 4291 | int blk_col, int plane, int block, TX_SIZE tx_size, |
| 4292 | BLOCK_SIZE plane_bsize, int depth, |
| 4293 | ENTROPY_CONTEXT *above_ctx, ENTROPY_CONTEXT *left_ctx, |
| 4294 | TXFM_CONTEXT *tx_above, TXFM_CONTEXT *tx_left, |
| 4295 | int64_t ref_best_rd, RD_STATS *rd_stats, int fast) { |
| 4296 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4297 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 4298 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 4299 | BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
| 4300 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 4301 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
| 4302 | TX_SIZE plane_tx_size; |
| 4303 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 4304 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
| 4305 | |
| 4306 | assert(tx_size < TX_SIZES_ALL); |
| 4307 | |
| 4308 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 4309 | |
| 4310 | plane_tx_size = |
| 4311 | plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0] |
| 4312 | : mbmi->inter_tx_size[tx_row][tx_col]; |
| 4313 | |
| 4314 | int ctx = txfm_partition_context(tx_above + blk_col, tx_left + blk_row, |
| 4315 | mbmi->sb_type, tx_size); |
| 4316 | |
| 4317 | av1_init_rd_stats(rd_stats); |
Debargha Mukherjee | 891a877 | 2017-11-22 10:09:37 -0800 | [diff] [blame] | 4318 | if (tx_size == plane_tx_size |
| 4319 | #if DISABLE_VARTX_FOR_CHROMA |
| 4320 | || pd->subsampling_x || pd->subsampling_y |
| 4321 | #endif // DISABLE_VARTX_FOR_CHROMA |
| 4322 | ) { |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4323 | ENTROPY_CONTEXT *ta = above_ctx + blk_col; |
| 4324 | ENTROPY_CONTEXT *tl = left_ctx + blk_row; |
| 4325 | #if CONFIG_LV_MAP |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 4326 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4327 | TXB_CTX txb_ctx; |
| 4328 | get_txb_ctx(plane_bsize, tx_size, plane, ta, tl, &txb_ctx); |
| 4329 | |
| 4330 | const int zero_blk_rate = x->coeff_costs[txs_ctx][get_plane_type(plane)] |
| 4331 | .txb_skip_cost[txb_ctx.txb_skip_ctx][1]; |
| 4332 | #else |
| 4333 | const int coeff_ctx = get_entropy_context(tx_size, ta, tl); |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 4334 | const TX_SIZE tx_size_ctx = get_txsize_entropy_ctx(tx_size); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4335 | const int zero_blk_rate = |
| 4336 | x->token_head_costs[tx_size_ctx][pd->plane_type][1][0][coeff_ctx][0]; |
| 4337 | #endif // CONFIG_LV_MAP |
| 4338 | rd_stats->zero_rate = zero_blk_rate; |
| 4339 | rd_stats->ref_rdcost = ref_best_rd; |
| 4340 | av1_tx_block_rd_b(cpi, x, tx_size, blk_row, blk_col, plane, block, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4341 | plane_bsize, ta, tl, rd_stats, fast, NULL); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4342 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 4343 | if (RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) >= |
| 4344 | RDCOST(x->rdmult, zero_blk_rate, rd_stats->sse) || |
| 4345 | rd_stats->skip == 1) { |
| 4346 | rd_stats->rate = zero_blk_rate; |
| 4347 | rd_stats->dist = rd_stats->sse; |
| 4348 | rd_stats->skip = 1; |
| 4349 | x->blk_skip[plane][blk_row * mi_width + blk_col] = 1; |
| 4350 | x->plane[plane].eobs[block] = 0; |
| 4351 | #if CONFIG_LV_MAP |
| 4352 | x->plane[plane].txb_entropy_ctx[block] = 0; |
| 4353 | #endif // CONFIG_LV_MAP |
| 4354 | } else { |
| 4355 | rd_stats->skip = 0; |
| 4356 | x->blk_skip[plane][blk_row * mi_width + blk_col] = 0; |
| 4357 | } |
| 4358 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) |
| 4359 | rd_stats->rate += x->txfm_partition_cost[ctx][0]; |
| 4360 | av1_set_txb_context(x, plane, block, tx_size, ta, tl); |
| 4361 | txfm_partition_update(tx_above + blk_col, tx_left + blk_row, tx_size, |
| 4362 | tx_size); |
| 4363 | } else { |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 4364 | const TX_SIZE sub_txs = sub_tx_size_map[1][tx_size]; |
Debargha Mukherjee | 5577bd1 | 2017-11-20 16:04:26 -0800 | [diff] [blame] | 4365 | const int bsw = tx_size_wide_unit[sub_txs]; |
| 4366 | const int bsh = tx_size_high_unit[sub_txs]; |
| 4367 | const int step = bsh * bsw; |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4368 | RD_STATS pn_rd_stats; |
| 4369 | int64_t this_rd = 0; |
Debargha Mukherjee | 5577bd1 | 2017-11-20 16:04:26 -0800 | [diff] [blame] | 4370 | assert(bsw > 0 && bsh > 0); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4371 | |
Debargha Mukherjee | 5577bd1 | 2017-11-20 16:04:26 -0800 | [diff] [blame] | 4372 | for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) { |
| 4373 | for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) { |
| 4374 | const int offsetr = blk_row + row; |
| 4375 | const int offsetc = blk_col + col; |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4376 | |
Debargha Mukherjee | 5577bd1 | 2017-11-20 16:04:26 -0800 | [diff] [blame] | 4377 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4378 | |
Debargha Mukherjee | 5577bd1 | 2017-11-20 16:04:26 -0800 | [diff] [blame] | 4379 | av1_init_rd_stats(&pn_rd_stats); |
| 4380 | tx_block_yrd(cpi, x, offsetr, offsetc, plane, block, sub_txs, |
| 4381 | plane_bsize, depth + 1, above_ctx, left_ctx, tx_above, |
| 4382 | tx_left, ref_best_rd - this_rd, &pn_rd_stats, fast); |
| 4383 | if (pn_rd_stats.rate == INT_MAX) { |
| 4384 | av1_invalid_rd_stats(rd_stats); |
| 4385 | return; |
| 4386 | } |
| 4387 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
| 4388 | this_rd += RDCOST(x->rdmult, pn_rd_stats.rate, pn_rd_stats.dist); |
| 4389 | block += step; |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4390 | } |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4391 | } |
Debargha Mukherjee | 5577bd1 | 2017-11-20 16:04:26 -0800 | [diff] [blame] | 4392 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4393 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) |
| 4394 | rd_stats->rate += x->txfm_partition_cost[ctx][1]; |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | // Return value 0: early termination triggered, no valid rd cost available; |
| 4399 | // 1: rd cost values are valid. |
| 4400 | int inter_block_yrd(const AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_stats, |
| 4401 | BLOCK_SIZE bsize, int64_t ref_best_rd, int fast) { |
| 4402 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4403 | int is_cost_valid = 1; |
| 4404 | int64_t this_rd = 0; |
| 4405 | |
| 4406 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 4407 | |
| 4408 | av1_init_rd_stats(rd_stats); |
| 4409 | |
| 4410 | if (is_cost_valid) { |
| 4411 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 4412 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
| 4413 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 4414 | const int mi_height = block_size_high[plane_bsize] >> tx_size_high_log2[0]; |
Debargha Mukherjee | 891a877 | 2017-11-22 10:09:37 -0800 | [diff] [blame] | 4415 | const TX_SIZE max_tx_size = get_vartx_max_txsize( |
| 4416 | xd, plane_bsize, pd->subsampling_x || pd->subsampling_y); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4417 | const int bh = tx_size_high_unit[max_tx_size]; |
| 4418 | const int bw = tx_size_wide_unit[max_tx_size]; |
Debargha Mukherjee | edc7346 | 2017-10-31 15:13:32 -0700 | [diff] [blame] | 4419 | const int init_depth = get_search_init_depth(mi_width, mi_height, &cpi->sf); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4420 | int idx, idy; |
| 4421 | int block = 0; |
| 4422 | int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size]; |
| 4423 | ENTROPY_CONTEXT ctxa[2 * MAX_MIB_SIZE]; |
| 4424 | ENTROPY_CONTEXT ctxl[2 * MAX_MIB_SIZE]; |
| 4425 | TXFM_CONTEXT tx_above[MAX_MIB_SIZE * 2]; |
| 4426 | TXFM_CONTEXT tx_left[MAX_MIB_SIZE * 2]; |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4427 | RD_STATS pn_rd_stats; |
| 4428 | |
| 4429 | av1_get_entropy_contexts(bsize, 0, pd, ctxa, ctxl); |
| 4430 | memcpy(tx_above, xd->above_txfm_context, sizeof(TXFM_CONTEXT) * mi_width); |
| 4431 | memcpy(tx_left, xd->left_txfm_context, sizeof(TXFM_CONTEXT) * mi_height); |
| 4432 | |
| 4433 | for (idy = 0; idy < mi_height; idy += bh) { |
| 4434 | for (idx = 0; idx < mi_width; idx += bw) { |
| 4435 | av1_init_rd_stats(&pn_rd_stats); |
| 4436 | tx_block_yrd(cpi, x, idy, idx, 0, block, max_tx_size, plane_bsize, |
| 4437 | init_depth, ctxa, ctxl, tx_above, tx_left, |
| 4438 | ref_best_rd - this_rd, &pn_rd_stats, fast); |
| 4439 | if (pn_rd_stats.rate == INT_MAX) { |
| 4440 | av1_invalid_rd_stats(rd_stats); |
| 4441 | return 0; |
| 4442 | } |
| 4443 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Debargha Mukherjee | 4d8c6fc | 2017-12-07 11:25:18 -0800 | [diff] [blame] | 4444 | this_rd += |
| 4445 | AOMMIN(RDCOST(x->rdmult, pn_rd_stats.rate, pn_rd_stats.dist), |
| 4446 | RDCOST(x->rdmult, pn_rd_stats.zero_rate, pn_rd_stats.sse)); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4447 | block += step; |
| 4448 | } |
| 4449 | } |
| 4450 | } |
Debargha Mukherjee | 4d8c6fc | 2017-12-07 11:25:18 -0800 | [diff] [blame] | 4451 | int64_t zero_rd = RDCOST(x->rdmult, rd_stats->zero_rate, rd_stats->sse); |
| 4452 | this_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
| 4453 | if (zero_rd < this_rd) { |
| 4454 | this_rd = zero_rd; |
| 4455 | rd_stats->skip = 1; |
| 4456 | } |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4457 | if (this_rd > ref_best_rd) is_cost_valid = 0; |
| 4458 | |
| 4459 | if (!is_cost_valid) { |
| 4460 | // reset cost value |
| 4461 | av1_invalid_rd_stats(rd_stats); |
| 4462 | } |
| 4463 | return is_cost_valid; |
| 4464 | } |
| 4465 | |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4466 | static uint32_t get_block_residue_hash(MACROBLOCK *x, BLOCK_SIZE bsize) { |
| 4467 | const int rows = block_size_high[bsize]; |
| 4468 | const int cols = block_size_wide[bsize]; |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4469 | const struct macroblock_plane *const p = &x->plane[0]; |
| 4470 | const int16_t *diff = &p->src_diff[0]; |
Debargha Mukherjee | fd65c8d | 2017-11-07 15:45:55 -0800 | [diff] [blame] | 4471 | uint16_t hash_data[MAX_SB_SQUARE]; |
| 4472 | memcpy(hash_data, diff, sizeof(*hash_data) * rows * cols); |
| 4473 | return (av1_get_crc_value(&x->tx_rd_record.crc_calculator, |
| 4474 | (uint8_t *)hash_data, 2 * rows * cols) |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4475 | << 7) + |
| 4476 | bsize; |
| 4477 | } |
| 4478 | |
| 4479 | static void save_tx_rd_info(int n4, uint32_t hash, const MACROBLOCK *const x, |
| 4480 | const RD_STATS *const rd_stats, |
Hui Su | 89ef493 | 2017-11-28 10:54:31 -0800 | [diff] [blame] | 4481 | TX_RD_RECORD *tx_rd_record) { |
| 4482 | int index; |
| 4483 | if (tx_rd_record->num < RD_RECORD_BUFFER_LEN) { |
| 4484 | index = |
| 4485 | (tx_rd_record->index_start + tx_rd_record->num) % RD_RECORD_BUFFER_LEN; |
| 4486 | ++tx_rd_record->num; |
| 4487 | } else { |
| 4488 | index = tx_rd_record->index_start; |
| 4489 | tx_rd_record->index_start = |
| 4490 | (tx_rd_record->index_start + 1) % RD_RECORD_BUFFER_LEN; |
| 4491 | } |
| 4492 | TX_RD_INFO *const tx_rd_info = &tx_rd_record->tx_rd_info[index]; |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4493 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 4494 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 4495 | tx_rd_info->hash_value = hash; |
| 4496 | tx_rd_info->tx_type = mbmi->tx_type; |
| 4497 | tx_rd_info->tx_size = mbmi->tx_size; |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4498 | tx_rd_info->min_tx_size = mbmi->min_tx_size; |
| 4499 | memcpy(tx_rd_info->blk_skip, x->blk_skip[0], |
| 4500 | sizeof(tx_rd_info->blk_skip[0]) * n4); |
| 4501 | for (int idy = 0; idy < xd->n8_h; ++idy) |
| 4502 | for (int idx = 0; idx < xd->n8_w; ++idx) |
| 4503 | tx_rd_info->inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4504 | #if CONFIG_TXK_SEL |
| 4505 | av1_copy(tx_rd_info->txk_type, mbmi->txk_type); |
| 4506 | #endif // CONFIG_TXK_SEL |
| 4507 | tx_rd_info->rd_stats = *rd_stats; |
| 4508 | } |
| 4509 | |
| 4510 | static void fetch_tx_rd_info(int n4, const TX_RD_INFO *const tx_rd_info, |
| 4511 | RD_STATS *const rd_stats, MACROBLOCK *const x) { |
| 4512 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4513 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 4514 | mbmi->tx_type = tx_rd_info->tx_type; |
| 4515 | mbmi->tx_size = tx_rd_info->tx_size; |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4516 | mbmi->min_tx_size = tx_rd_info->min_tx_size; |
| 4517 | memcpy(x->blk_skip[0], tx_rd_info->blk_skip, |
| 4518 | sizeof(tx_rd_info->blk_skip[0]) * n4); |
| 4519 | for (int idy = 0; idy < xd->n8_h; ++idy) |
| 4520 | for (int idx = 0; idx < xd->n8_w; ++idx) |
| 4521 | mbmi->inter_tx_size[idy][idx] = tx_rd_info->inter_tx_size[idy][idx]; |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4522 | #if CONFIG_TXK_SEL |
| 4523 | av1_copy(mbmi->txk_type, tx_rd_info->txk_type); |
| 4524 | #endif // CONFIG_TXK_SEL |
| 4525 | *rd_stats = tx_rd_info->rd_stats; |
| 4526 | } |
| 4527 | |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4528 | static int find_tx_size_rd_info(TX_SIZE_RD_RECORD *cur_record, |
| 4529 | const uint32_t hash) { |
| 4530 | // Linear search through the circular buffer to find matching hash. |
| 4531 | int index; |
| 4532 | for (int i = cur_record->num - 1; i >= 0; i--) { |
| 4533 | index = (cur_record->index_start + i) % TX_SIZE_RD_RECORD_BUFFER_LEN; |
| 4534 | if (cur_record->hash_vals[index] == hash) return index; |
| 4535 | } |
| 4536 | |
| 4537 | // If not found - add new RD info into the buffer and return its index |
| 4538 | if (cur_record->num < TX_SIZE_RD_RECORD_BUFFER_LEN) { |
| 4539 | index = (cur_record->index_start + cur_record->num) % |
| 4540 | TX_SIZE_RD_RECORD_BUFFER_LEN; |
| 4541 | cur_record->num++; |
| 4542 | } else { |
| 4543 | index = cur_record->index_start; |
| 4544 | cur_record->index_start = |
| 4545 | (cur_record->index_start + 1) % TX_SIZE_RD_RECORD_BUFFER_LEN; |
| 4546 | } |
| 4547 | |
| 4548 | cur_record->hash_vals[index] = hash; |
| 4549 | av1_zero(cur_record->tx_rd_info[index]); |
| 4550 | return index; |
| 4551 | } |
| 4552 | |
| 4553 | // Go through all TX blocks that could be used in TX size search, compute |
| 4554 | // residual hash values for them and find matching RD info that stores previous |
| 4555 | // RD search results for these TX blocks. The idea is to prevent repeated |
| 4556 | // rate/distortion computations that happen because of the combination of |
| 4557 | // partition and TX size search. The resulting RD info records are returned in |
| 4558 | // the form of a quadtree for easier access in actual TX size search. |
| 4559 | static int find_tx_size_rd_records(MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row, |
| 4560 | int mi_col, |
| 4561 | TX_SIZE_RD_INFO_NODE *dst_rd_info) { |
| 4562 | #if CONFIG_TX64X64 |
| 4563 | TX_SIZE_RD_RECORD *rd_records_table[4] = { x->tx_size_rd_record_8X8, |
| 4564 | x->tx_size_rd_record_16X16, |
| 4565 | x->tx_size_rd_record_32X32, |
| 4566 | x->tx_size_rd_record_64X64 }; |
| 4567 | #else |
| 4568 | TX_SIZE_RD_RECORD *rd_records_table[3] = { x->tx_size_rd_record_8X8, |
| 4569 | x->tx_size_rd_record_16X16, |
| 4570 | x->tx_size_rd_record_32X32 }; |
| 4571 | #endif |
| 4572 | const TX_SIZE max_square_tx_size = max_txsize_lookup[bsize]; |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4573 | const int bw = block_size_wide[bsize]; |
| 4574 | const int bh = block_size_high[bsize]; |
Debargha Mukherjee | de80e76 | 2017-11-30 13:58:56 -0800 | [diff] [blame] | 4575 | |
| 4576 | // Hashing is performed only for square TX sizes larger than TX_4X4 |
| 4577 | if (max_square_tx_size < TX_8X8 || bw != bh) return 0; |
| 4578 | |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4579 | const int diff_stride = bw; |
| 4580 | const struct macroblock_plane *const p = &x->plane[0]; |
| 4581 | const int16_t *diff = &p->src_diff[0]; |
| 4582 | |
| 4583 | // Coordinates of the top-left corner of current block within the superblock |
| 4584 | // measured in pixels: |
| 4585 | const int mi_row_in_sb = (mi_row % MAX_MIB_SIZE) << MI_SIZE_LOG2; |
| 4586 | const int mi_col_in_sb = (mi_col % MAX_MIB_SIZE) << MI_SIZE_LOG2; |
| 4587 | int cur_rd_info_idx = 0; |
| 4588 | int cur_tx_depth = 0; |
| 4589 | uint8_t parent_idx_buf[MAX_SB_SQUARE] = { 0 }; |
| 4590 | |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 4591 | int cur_tx_size = max_txsize_rect_lookup[1][bsize]; |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4592 | while (cur_tx_depth <= MAX_VARTX_DEPTH) { |
| 4593 | const BLOCK_SIZE cur_tx_bsize = txsize_to_bsize[cur_tx_size]; |
| 4594 | const int cur_tx_bw = block_size_wide[cur_tx_bsize]; |
| 4595 | const int cur_tx_bh = block_size_high[cur_tx_bsize]; |
| 4596 | if (cur_tx_bw < 8 || cur_tx_bh < 8) break; |
| 4597 | |
| 4598 | for (int row = 0; row < bh; row += cur_tx_bh) { |
| 4599 | for (int col = 0; col < bw; col += cur_tx_bw) { |
| 4600 | if (cur_tx_bw != cur_tx_bh) { |
| 4601 | // Use dummy nodes for all rectangular transforms within the |
| 4602 | // TX size search tree. |
| 4603 | dst_rd_info[cur_rd_info_idx].rd_info_array = NULL; |
| 4604 | } else { |
| 4605 | // Get spatial location of this TX block within the superblock |
| 4606 | // (measured in cur_tx_bsize units). |
| 4607 | const int row_in_sb = (mi_row_in_sb + row) / cur_tx_bh; |
| 4608 | const int col_in_sb = (mi_col_in_sb + col) / cur_tx_bw; |
| 4609 | |
| 4610 | // Compute FNV-1a hash for this TX block. |
| 4611 | uint32_t hash = 2166136261; |
| 4612 | for (int i = 0; i < cur_tx_bh; i++) { |
| 4613 | const int16_t *cur_diff_row = diff + (row + i) * diff_stride + col; |
| 4614 | for (int j = 0; j < cur_tx_bw; j++) { |
| 4615 | hash = hash ^ clip_pixel(cur_diff_row[j] + 128); |
Yaowu Xu | 5102154 | 2017-11-20 18:23:42 -0800 | [diff] [blame] | 4616 | hash = (uint32_t)((int64_t)hash * 16777619); |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4617 | } |
| 4618 | } |
| 4619 | |
| 4620 | // Find corresponding RD info based on the hash value. |
| 4621 | const int rd_record_idx = |
| 4622 | row_in_sb * (MAX_MIB_SIZE >> (cur_tx_size + 1 - TX_8X8)) + |
| 4623 | col_in_sb; |
| 4624 | int idx = find_tx_size_rd_info( |
| 4625 | &rd_records_table[cur_tx_size - TX_8X8][rd_record_idx], hash); |
| 4626 | dst_rd_info[cur_rd_info_idx].rd_info_array = |
| 4627 | rd_records_table[cur_tx_size - TX_8X8][rd_record_idx] |
| 4628 | .tx_rd_info[idx]; |
| 4629 | } |
| 4630 | |
| 4631 | // Update the output quadtree RD info structure. |
| 4632 | av1_zero(dst_rd_info[cur_rd_info_idx].children); |
| 4633 | if (cur_tx_depth > 0) { |
| 4634 | const int y_odd = (row / cur_tx_bh) % 2; |
| 4635 | const int x_odd = (col / cur_tx_bw) % 2; |
| 4636 | const int child_idx = y_odd ? (x_odd ? 3 : 2) : (x_odd ? 1 : 0); |
| 4637 | dst_rd_info[parent_idx_buf[row * bw + col]].children[child_idx] = |
| 4638 | &dst_rd_info[cur_rd_info_idx]; |
| 4639 | } |
| 4640 | for (int i = row; i < row + cur_tx_bh; ++i) |
| 4641 | memset(parent_idx_buf + i * bw + col, cur_rd_info_idx, cur_tx_bw); |
| 4642 | ++cur_rd_info_idx; |
| 4643 | } |
| 4644 | } |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 4645 | cur_tx_size = sub_tx_size_map[1][cur_tx_size]; |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4646 | ++cur_tx_depth; |
| 4647 | } |
| 4648 | return 1; |
| 4649 | } |
| 4650 | |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4651 | static const uint32_t skip_pred_threshold[3][BLOCK_SIZES_ALL] = { |
| 4652 | { |
| 4653 | 0, 0, 0, 50, 50, 50, 55, 47, 47, 53, 53, 53, 0, 0, 0, 0, |
| 4654 | #if CONFIG_EXT_PARTITION |
| 4655 | 0, 0, 0, |
| 4656 | #endif |
| 4657 | 50, 50, 55, 55, 53, 53, |
| 4658 | #if CONFIG_EXT_PARTITION |
| 4659 | 0, 0, |
| 4660 | #endif |
| 4661 | }, |
| 4662 | { |
| 4663 | 0, 0, 0, 69, 69, 69, 67, 68, 68, 53, 53, 53, 0, 0, 0, 0, |
| 4664 | #if CONFIG_EXT_PARTITION |
| 4665 | 0, 0, 0, |
| 4666 | #endif |
| 4667 | 69, 69, 67, 67, 53, 53, |
| 4668 | #if CONFIG_EXT_PARTITION |
| 4669 | 0, 0, |
| 4670 | #endif |
| 4671 | }, |
| 4672 | { |
| 4673 | 0, 0, 0, 70, 73, 73, 70, 73, 73, 58, 58, 58, 0, 0, 0, 0, |
| 4674 | #if CONFIG_EXT_PARTITION |
| 4675 | 0, 0, 0, |
| 4676 | #endif |
| 4677 | 70, 70, 70, 70, 58, 58, |
| 4678 | #if CONFIG_EXT_PARTITION |
| 4679 | 0, 0, |
| 4680 | #endif |
| 4681 | } |
| 4682 | }; |
| 4683 | |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4684 | // Uses simple features on top of DCT coefficients to quickly predict |
| 4685 | // whether optimal RD decision is to skip encoding the residual. |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4686 | // The sse value is stored in dist. |
| 4687 | static int predict_skip_flag(MACROBLOCK *x, BLOCK_SIZE bsize, int64_t *dist) { |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4688 | const int max_tx_size = |
| 4689 | get_max_rect_tx_size(bsize, is_inter_block(&x->e_mbd.mi[0]->mbmi)); |
| 4690 | const int tx_h = tx_size_high[max_tx_size]; |
| 4691 | const int tx_w = tx_size_wide[max_tx_size]; |
| 4692 | if (tx_h > 16 || tx_w > 16) return 0; |
| 4693 | |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4694 | const int bw = block_size_wide[bsize]; |
| 4695 | const int bh = block_size_high[bsize]; |
Alexander Bokov | 80eedf2 | 2017-11-02 12:48:52 -0700 | [diff] [blame] | 4696 | const MACROBLOCKD *xd = &x->e_mbd; |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4697 | const uint32_t dc_q = (uint32_t)av1_dc_quant_QTX(x->qindex, 0, xd->bd); |
| 4698 | |
| 4699 | *dist = pixel_diff_dist(x, 0, x->plane[0].src_diff, bw, 0, 0, bsize, bsize); |
| 4700 | const int64_t mse = *dist / bw / bh; |
| 4701 | // Normalized quantizer takes the transform upscaling factor (8 for tx size |
| 4702 | // smaller than 32) into account. |
| 4703 | const uint32_t normalized_dc_q = dc_q >> 3; |
| 4704 | const int64_t mse_thresh = (int64_t)normalized_dc_q * normalized_dc_q / 8; |
| 4705 | // Predict not to skip when mse is larger than threshold. |
| 4706 | if (mse > mse_thresh) return 0; |
| 4707 | |
Alexander Bokov | f93feec | 2017-10-11 14:55:50 -0700 | [diff] [blame] | 4708 | DECLARE_ALIGNED(32, tran_low_t, DCT_coefs[32 * 32]); |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4709 | TxfmParam param; |
| 4710 | param.tx_type = DCT_DCT; |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4711 | param.tx_size = max_tx_size; |
Alexander Bokov | 80eedf2 | 2017-11-02 12:48:52 -0700 | [diff] [blame] | 4712 | param.bd = xd->bd; |
Monty Montgomery | 26b8a99 | 2017-11-10 22:45:23 -0500 | [diff] [blame] | 4713 | param.is_hbd = get_bitdepth_data_path_index(xd); |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4714 | param.lossless = 0; |
Sarah Parker | 90024e4 | 2017-10-06 16:50:47 -0700 | [diff] [blame] | 4715 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 4716 | const BLOCK_SIZE plane_bsize = |
| 4717 | get_plane_block_size(xd->mi[0]->mbmi.sb_type, pd); |
| 4718 | // TODO(sarahparker) This assumes reduced_tx_set_used == 0. I will do a |
| 4719 | // follow up refactor to make the actual value of reduced_tx_set_used |
| 4720 | // within this function. |
| 4721 | param.tx_set_type = get_ext_tx_set_type(param.tx_size, plane_bsize, |
| 4722 | is_inter_block(&xd->mi[0]->mbmi), 0); |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4723 | const uint32_t ac_q = (uint32_t)av1_ac_quant_QTX(x->qindex, 0, xd->bd); |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4724 | uint32_t max_quantized_coef = 0; |
| 4725 | const int16_t *src_diff = x->plane[0].src_diff; |
| 4726 | for (int row = 0; row < bh; row += tx_h) { |
| 4727 | for (int col = 0; col < bw; col += tx_w) { |
Alexander Bokov | 7010902 | 2017-10-11 15:09:24 -0700 | [diff] [blame] | 4728 | #if CONFIG_TXMG |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4729 | av1_highbd_fwd_txfm(src_diff + col, DCT_coefs, bw, ¶m); |
Alexander Bokov | 7010902 | 2017-10-11 15:09:24 -0700 | [diff] [blame] | 4730 | #else // CONFIG_TXMG |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4731 | if (param.is_hbd) |
| 4732 | av1_highbd_fwd_txfm(src_diff + col, DCT_coefs, bw, ¶m); |
| 4733 | else |
| 4734 | av1_fwd_txfm(src_diff + col, DCT_coefs, bw, ¶m); |
Alexander Bokov | 7010902 | 2017-10-11 15:09:24 -0700 | [diff] [blame] | 4735 | #endif // CONFIG_TXMG |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4736 | |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4737 | // Operating on TX domain, not pixels; we want the QTX quantizers |
| 4738 | for (int i = 0; i < tx_w * tx_h; ++i) { |
| 4739 | uint32_t cur_quantized_coef = |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4740 | (100 * (uint32_t)abs(DCT_coefs[i])) / (i ? ac_q : dc_q); |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4741 | if (cur_quantized_coef > max_quantized_coef) |
| 4742 | max_quantized_coef = cur_quantized_coef; |
| 4743 | } |
| 4744 | } |
| 4745 | src_diff += tx_h * bw; |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4746 | } |
Alexander Bokov | 80eedf2 | 2017-11-02 12:48:52 -0700 | [diff] [blame] | 4747 | const int bd_idx = (xd->bd == 8) ? 0 : ((xd->bd == 10) ? 1 : 2); |
Hui Su | 991dd22 | 2017-11-27 16:32:00 -0800 | [diff] [blame] | 4748 | return max_quantized_coef < skip_pred_threshold[bd_idx][bsize]; |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | // Used to set proper context for early termination with skip = 1. |
| 4752 | static void set_skip_flag(const AV1_COMP *cpi, MACROBLOCK *x, |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4753 | RD_STATS *rd_stats, int bsize, int64_t dist) { |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4754 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4755 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 4756 | const int n4 = bsize_to_num_blk(bsize); |
Yue Chen | 0797a20 | 2017-10-27 17:24:56 -0700 | [diff] [blame] | 4757 | const TX_SIZE tx_size = get_max_rect_tx_size(bsize, is_inter_block(mbmi)); |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4758 | mbmi->tx_type = DCT_DCT; |
Angie Chiang | 0483813 | 2017-11-30 14:25:15 -0800 | [diff] [blame] | 4759 | #if CONFIG_TXK_SEL |
| 4760 | memset(mbmi->txk_type, DCT_DCT, |
| 4761 | sizeof(mbmi->txk_type[0]) * |
| 4762 | (MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN))); |
| 4763 | #endif |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4764 | for (int idy = 0; idy < xd->n8_h; ++idy) |
| 4765 | for (int idx = 0; idx < xd->n8_w; ++idx) |
| 4766 | mbmi->inter_tx_size[idy][idx] = tx_size; |
| 4767 | mbmi->tx_size = tx_size; |
| 4768 | mbmi->min_tx_size = get_min_tx_size(tx_size); |
| 4769 | memset(x->blk_skip[0], 1, sizeof(uint8_t) * n4); |
| 4770 | rd_stats->skip = 1; |
| 4771 | |
Yue Chen | 171c17d | 2017-10-16 18:08:22 -0700 | [diff] [blame] | 4772 | (void)cpi; |
| 4773 | |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4774 | // Rate. |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 4775 | const int tx_size_ctx = get_txsize_entropy_ctx(tx_size); |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4776 | ENTROPY_CONTEXT ctxa[2 * MAX_MIB_SIZE]; |
| 4777 | ENTROPY_CONTEXT ctxl[2 * MAX_MIB_SIZE]; |
| 4778 | av1_get_entropy_contexts(bsize, 0, &xd->plane[0], ctxa, ctxl); |
Angie Chiang | 4639e08 | 2017-11-30 15:35:45 -0800 | [diff] [blame] | 4779 | #if CONFIG_LV_MAP |
| 4780 | TXB_CTX txb_ctx; |
| 4781 | // Because plane is 0, plane_bsize equal to bsize |
| 4782 | get_txb_ctx(bsize, tx_size, 0, ctxa, ctxl, &txb_ctx); |
| 4783 | int rate = x->coeff_costs[tx_size_ctx][PLANE_TYPE_Y] |
| 4784 | .txb_skip_cost[txb_ctx.txb_skip_ctx][1]; |
| 4785 | #else |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4786 | int coeff_ctx = get_entropy_context(tx_size, ctxa, ctxl); |
| 4787 | int rate = x->token_head_costs[tx_size_ctx][PLANE_TYPE_Y][1][0][coeff_ctx][0]; |
Angie Chiang | 4639e08 | 2017-11-30 15:35:45 -0800 | [diff] [blame] | 4788 | #endif |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4789 | if (tx_size > TX_4X4) { |
| 4790 | int ctx = txfm_partition_context( |
| 4791 | xd->above_txfm_context, xd->left_txfm_context, mbmi->sb_type, tx_size); |
Yue Chen | 171c17d | 2017-10-16 18:08:22 -0700 | [diff] [blame] | 4792 | rate += x->txfm_partition_cost[ctx][0]; |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4793 | } |
| 4794 | #if !CONFIG_TXK_SEL |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4795 | const AV1_COMMON *cm = &cpi->common; |
| 4796 | const int ext_tx_set = get_ext_tx_set(max_txsize_lookup[bsize], bsize, 1, |
| 4797 | cm->reduced_tx_set_used); |
| 4798 | if (get_ext_tx_types(mbmi->min_tx_size, bsize, 1, cm->reduced_tx_set_used) > |
| 4799 | 1 && |
| 4800 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
| 4801 | if (ext_tx_set > 0) |
| 4802 | rate += |
| 4803 | x->inter_tx_type_costs[ext_tx_set][txsize_sqr_map[mbmi->min_tx_size]] |
| 4804 | [mbmi->tx_type]; |
| 4805 | } |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4806 | #endif // CONFIG_TXK_SEL |
| 4807 | rd_stats->rate = rate; |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4808 | #if CONFIG_HIGHBITDEPTH |
| 4809 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4810 | dist = ROUND_POWER_OF_TWO(dist, (xd->bd - 8) * 2); |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4811 | #endif // CONFIG_HIGHBITDEPTH |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4812 | rd_stats->dist = rd_stats->sse = (dist << 4); |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4813 | } |
| 4814 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4815 | static void select_tx_type_yrd(const AV1_COMP *cpi, MACROBLOCK *x, |
Yue Chen | 25dc070 | 2017-10-18 23:36:06 -0700 | [diff] [blame] | 4816 | RD_STATS *rd_stats, BLOCK_SIZE bsize, int mi_row, |
| 4817 | int mi_col, int64_t ref_best_rd) { |
Jingning Han | 2b0eeb1 | 2017-02-23 15:55:37 -0800 | [diff] [blame] | 4818 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4819 | const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; |
| 4820 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4821 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 4822 | int64_t rd = INT64_MAX; |
| 4823 | int64_t best_rd = INT64_MAX; |
| 4824 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
| 4825 | const int is_inter = is_inter_block(mbmi); |
| 4826 | TX_SIZE best_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE]; |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 4827 | TX_SIZE best_tx = max_txsize_rect_lookup[1][bsize]; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 4828 | TX_SIZE best_min_tx_size = TX_SIZES_ALL; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 4829 | uint8_t best_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4830 | TX_TYPE txk_start = DCT_DCT; |
| 4831 | #if CONFIG_TXK_SEL |
| 4832 | TX_TYPE txk_end = DCT_DCT + 1; |
| 4833 | #else |
| 4834 | TX_TYPE txk_end = TX_TYPES; |
| 4835 | #endif |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 4836 | const int n4 = bsize_to_num_blk(bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4837 | int idx, idy; |
| 4838 | int prune = 0; |
Sarah Parker | 90024e4 | 2017-10-06 16:50:47 -0700 | [diff] [blame] | 4839 | // Get the tx_size 1 level down |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 4840 | TX_SIZE min_tx_size = sub_tx_size_map[1][max_txsize_rect_lookup[1][bsize]]; |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 4841 | const TxSetType tx_set_type = get_ext_tx_set_type( |
Sarah Parker | 90024e4 | 2017-10-06 16:50:47 -0700 | [diff] [blame] | 4842 | min_tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
Jingning Han | 3de5353 | 2017-12-07 13:40:32 -0800 | [diff] [blame] | 4843 | int within_border = mi_row >= xd->tile.mi_row_start && |
| 4844 | (mi_row + mi_size_high[bsize] < xd->tile.mi_row_end) && |
| 4845 | mi_col >= xd->tile.mi_col_start && |
| 4846 | (mi_col + mi_size_wide[bsize] < xd->tile.mi_col_end); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4847 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4848 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4849 | |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4850 | const uint32_t hash = get_block_residue_hash(x, bsize); |
| 4851 | TX_RD_RECORD *tx_rd_record = &x->tx_rd_record; |
| 4852 | |
Yue Chen | 25dc070 | 2017-10-18 23:36:06 -0700 | [diff] [blame] | 4853 | if (ref_best_rd != INT64_MAX && within_border) { |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4854 | for (int i = 0; i < tx_rd_record->num; ++i) { |
| 4855 | const int index = (tx_rd_record->index_start + i) % RD_RECORD_BUFFER_LEN; |
| 4856 | // If there is a match in the tx_rd_record, fetch the RD decision and |
| 4857 | // terminate early. |
| 4858 | if (tx_rd_record->tx_rd_info[index].hash_value == hash) { |
| 4859 | TX_RD_INFO *tx_rd_info = &tx_rd_record->tx_rd_info[index]; |
| 4860 | fetch_tx_rd_info(n4, tx_rd_info, rd_stats, x); |
| 4861 | return; |
| 4862 | } |
| 4863 | } |
| 4864 | } |
| 4865 | |
Alexander Bokov | 80eedf2 | 2017-11-02 12:48:52 -0700 | [diff] [blame] | 4866 | // If we predict that skip is the optimal RD decision - set the respective |
| 4867 | // context and terminate early. |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4868 | int64_t dist; |
Alexander Bokov | 80eedf2 | 2017-11-02 12:48:52 -0700 | [diff] [blame] | 4869 | if (is_inter && cpi->sf.tx_type_search.use_skip_flag_prediction && |
Hui Su | 3889c6d | 2017-12-04 17:02:44 -0800 | [diff] [blame] | 4870 | predict_skip_flag(x, bsize, &dist)) { |
| 4871 | set_skip_flag(cpi, x, rd_stats, bsize, dist); |
Hui Su | 89ef493 | 2017-11-28 10:54:31 -0800 | [diff] [blame] | 4872 | // Save the RD search results into tx_rd_record. |
| 4873 | if (within_border) save_tx_rd_info(n4, hash, x, rd_stats, tx_rd_record); |
Alexander Bokov | 80eedf2 | 2017-11-02 12:48:52 -0700 | [diff] [blame] | 4874 | return; |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4875 | } |
| 4876 | |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4877 | // Precompute residual hashes and find existing or add new RD records to |
| 4878 | // store and reuse rate and distortion values to speed up TX size search. |
| 4879 | TX_SIZE_RD_INFO_NODE matched_rd_info[16 + 64 + 256]; |
| 4880 | int found_rd_info = 0; |
| 4881 | if (ref_best_rd != INT64_MAX && within_border) { |
| 4882 | found_rd_info = |
| 4883 | find_tx_size_rd_records(x, bsize, mi_row, mi_col, matched_rd_info); |
| 4884 | } |
| 4885 | |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 4886 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 4887 | !x->use_default_inter_tx_type && !xd->lossless[mbmi->segment_id]) { |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 4888 | prune = prune_tx(cpi, bsize, x, xd, tx_set_type, |
| 4889 | cpi->sf.tx_type_search.use_tx_size_pruning); |
Alexander Bokov | 0c7eb10 | 2017-09-07 18:49:00 -0700 | [diff] [blame] | 4890 | } |
Alexander Bokov | 8829a24 | 2017-08-31 18:07:05 -0700 | [diff] [blame] | 4891 | |
Rupert Swarbrick | de2ea94 | 2017-10-09 15:21:21 +0100 | [diff] [blame] | 4892 | int found = 0; |
| 4893 | |
Alexander Bokov | 79a3724 | 2017-09-29 11:25:55 -0700 | [diff] [blame] | 4894 | int tx_split_prune_flag = 0; |
| 4895 | if (is_inter && cpi->sf.tx_type_search.prune_mode >= PRUNE_2D_ACCURATE) |
| 4896 | tx_split_prune_flag = ((prune >> TX_TYPES) & 1); |
| 4897 | |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4898 | for (tx_type = txk_start; tx_type < txk_end; ++tx_type) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4899 | RD_STATS this_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4900 | av1_init_rd_stats(&this_rd_stats); |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 4901 | if (!av1_ext_tx_used[tx_set_type][tx_type]) continue; |
Sarah Parker | 90024e4 | 2017-10-06 16:50:47 -0700 | [diff] [blame] | 4902 | (void)prune; |
Sebastien Alaiwan | 3bac992 | 2017-11-02 12:34:41 +0100 | [diff] [blame] | 4903 | // TODO(sarahparker) This speed feature has been temporarily disabled |
| 4904 | // with ext-tx because it is not compatible with the current |
| 4905 | // search method. It will be fixed in a followup. |
| 4906 | /* |
| 4907 | if (is_inter) { |
| 4908 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 4909 | if (!do_tx_type_search(tx_type, prune, |
| 4910 | cpi->sf.tx_type_search.prune_mode)) |
| 4911 | continue; |
| 4912 | } |
| 4913 | } else { |
| 4914 | if (!ALLOW_INTRA_EXT_TX && bsize >= BLOCK_8X8) { |
| 4915 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 4916 | } |
| 4917 | } |
| 4918 | */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4919 | if (is_inter && x->use_default_inter_tx_type && |
| 4920 | tx_type != get_default_tx_type(0, xd, 0, max_tx_size)) |
| 4921 | continue; |
| 4922 | |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 4923 | if (xd->lossless[mbmi->segment_id]) |
| 4924 | if (tx_type != DCT_DCT) continue; |
| 4925 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4926 | rd = select_tx_size_fix_type(cpi, x, &this_rd_stats, bsize, mi_row, mi_col, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 4927 | ref_best_rd, tx_type, tx_split_prune_flag, |
| 4928 | found_rd_info ? matched_rd_info : NULL); |
Sarah Parker | 90024e4 | 2017-10-06 16:50:47 -0700 | [diff] [blame] | 4929 | // If the current tx_type is not included in the tx_set for the smallest |
| 4930 | // tx size found, then all vartx partitions were actually transformed with |
| 4931 | // DCT_DCT and we should avoid picking it. |
| 4932 | const TxSetType min_tx_set_type = get_ext_tx_set_type( |
| 4933 | mbmi->min_tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
| 4934 | if (!av1_ext_tx_used[min_tx_set_type][tx_type]) continue; |
Sarah Parker | 90024e4 | 2017-10-06 16:50:47 -0700 | [diff] [blame] | 4935 | |
Hui Su | da816a1 | 2017-08-18 14:46:02 -0700 | [diff] [blame] | 4936 | ref_best_rd = AOMMIN(rd, ref_best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4937 | if (rd < best_rd) { |
| 4938 | best_rd = rd; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4939 | *rd_stats = this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4940 | best_tx_type = mbmi->tx_type; |
| 4941 | best_tx = mbmi->tx_size; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 4942 | best_min_tx_size = mbmi->min_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4943 | memcpy(best_blk_skip, x->blk_skip[0], sizeof(best_blk_skip[0]) * n4); |
Rupert Swarbrick | de2ea94 | 2017-10-09 15:21:21 +0100 | [diff] [blame] | 4944 | found = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4945 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 4946 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 4947 | best_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 4948 | } |
| 4949 | } |
| 4950 | |
Rupert Swarbrick | de2ea94 | 2017-10-09 15:21:21 +0100 | [diff] [blame] | 4951 | // We should always find at least one candidate unless ref_best_rd is less |
| 4952 | // than INT64_MAX (in which case, all the calls to select_tx_size_fix_type |
| 4953 | // might have failed to find something better) |
| 4954 | assert(IMPLIES(!found, ref_best_rd != INT64_MAX)); |
| 4955 | if (!found) return; |
| 4956 | |
| 4957 | // We found a candidate transform to use. Copy our results from the "best" |
| 4958 | // array into mbmi. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4959 | mbmi->tx_type = best_tx_type; |
| 4960 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 4961 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 4962 | mbmi->inter_tx_size[idy][idx] = best_tx_size[idy][idx]; |
| 4963 | mbmi->tx_size = best_tx; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 4964 | mbmi->min_tx_size = best_min_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4965 | memcpy(x->blk_skip[0], best_blk_skip, sizeof(best_blk_skip[0]) * n4); |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 4966 | |
| 4967 | // Save the RD search results into tx_rd_record. |
Hui Su | 89ef493 | 2017-11-28 10:54:31 -0800 | [diff] [blame] | 4968 | if (within_border) save_tx_rd_info(n4, hash, x, rd_stats, tx_rd_record); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4969 | } |
| 4970 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4971 | 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] | 4972 | int blk_col, int plane, int block, TX_SIZE tx_size, |
| 4973 | BLOCK_SIZE plane_bsize, ENTROPY_CONTEXT *above_ctx, |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 4974 | ENTROPY_CONTEXT *left_ctx, RD_STATS *rd_stats, |
| 4975 | int fast) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4976 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4977 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4978 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 4979 | BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
| 4980 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 4981 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
| 4982 | TX_SIZE plane_tx_size; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 4983 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 4984 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4985 | |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 4986 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4987 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4988 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 4989 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 4990 | plane_tx_size = |
| 4991 | plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0] |
| 4992 | : mbmi->inter_tx_size[tx_row][tx_col]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4993 | |
Debargha Mukherjee | 891a877 | 2017-11-22 10:09:37 -0800 | [diff] [blame] | 4994 | if (tx_size == plane_tx_size |
| 4995 | #if DISABLE_VARTX_FOR_CHROMA |
| 4996 | || pd->subsampling_x || pd->subsampling_y |
| 4997 | #endif // DISABLE_VARTX_FOR_CHROMA |
| 4998 | ) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4999 | ENTROPY_CONTEXT *ta = above_ctx + blk_col; |
| 5000 | ENTROPY_CONTEXT *tl = left_ctx + blk_row; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5001 | av1_tx_block_rd_b(cpi, x, tx_size, blk_row, blk_col, plane, block, |
Alexander Bokov | c5ddf06 | 2017-10-17 16:41:46 -0700 | [diff] [blame] | 5002 | plane_bsize, ta, tl, rd_stats, fast, NULL); |
Jingning Han | 328d57b | 2017-07-07 14:40:17 -0700 | [diff] [blame] | 5003 | av1_set_txb_context(x, plane, block, tx_size, ta, tl); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5004 | } else { |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 5005 | const TX_SIZE sub_txs = sub_tx_size_map[1][tx_size]; |
Debargha Mukherjee | 5577bd1 | 2017-11-20 16:04:26 -0800 | [diff] [blame] | 5006 | assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size)); |
| 5007 | assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size)); |
| 5008 | const int bsw = tx_size_wide_unit[sub_txs]; |
| 5009 | const int bsh = tx_size_high_unit[sub_txs]; |
| 5010 | const int step = bsh * bsw; |
| 5011 | assert(bsw > 0 && bsh > 0); |
| 5012 | for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) { |
| 5013 | for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) { |
| 5014 | const int offsetr = blk_row + row; |
| 5015 | const int offsetc = blk_col + col; |
| 5016 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 5017 | tx_block_rd(cpi, x, offsetr, offsetc, plane, block, sub_txs, |
| 5018 | plane_bsize, above_ctx, left_ctx, rd_stats, fast); |
| 5019 | block += step; |
| 5020 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5021 | } |
| 5022 | } |
| 5023 | } |
| 5024 | |
| 5025 | // Return value 0: early termination triggered, no valid rd cost available; |
| 5026 | // 1: rd cost values are valid. |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5027 | int inter_block_uvrd(const AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_stats, |
| 5028 | BLOCK_SIZE bsize, int64_t ref_best_rd, int fast) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5029 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5030 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5031 | int plane; |
| 5032 | int is_cost_valid = 1; |
Debargha Mukherjee | 4d8c6fc | 2017-12-07 11:25:18 -0800 | [diff] [blame] | 5033 | int64_t this_rd = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5034 | |
| 5035 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 5036 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5037 | av1_init_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 5038 | |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 5039 | if (x->skip_chroma_rd) return is_cost_valid; |
Debargha Mukherjee | 3aa2811 | 2017-11-25 07:03:31 -0800 | [diff] [blame] | 5040 | const BLOCK_SIZE bsizec = scale_chroma_bsize( |
| 5041 | bsize, xd->plane[1].subsampling_x, xd->plane[1].subsampling_y); |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 5042 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5043 | if (is_inter_block(mbmi) && is_cost_valid) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5044 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) |
Debargha Mukherjee | 3aa2811 | 2017-11-25 07:03:31 -0800 | [diff] [blame] | 5045 | av1_subtract_plane(x, bsizec, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5046 | } |
| 5047 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5048 | if (is_cost_valid) { |
| 5049 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) { |
| 5050 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Debargha Mukherjee | 3aa2811 | 2017-11-25 07:03:31 -0800 | [diff] [blame] | 5051 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsizec, pd); |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5052 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 5053 | const int mi_height = |
| 5054 | block_size_high[plane_bsize] >> tx_size_high_log2[0]; |
Debargha Mukherjee | 1961988 | 2017-11-22 13:13:14 -0800 | [diff] [blame] | 5055 | TX_SIZE max_tx_size = get_vartx_max_txsize( |
Debargha Mukherjee | 891a877 | 2017-11-22 10:09:37 -0800 | [diff] [blame] | 5056 | xd, plane_bsize, pd->subsampling_x || pd->subsampling_y); |
Debargha Mukherjee | 1961988 | 2017-11-22 13:13:14 -0800 | [diff] [blame] | 5057 | #if DISABLE_VARTX_FOR_CHROMA == 2 |
| 5058 | // If the luma transform size is split at least one level, split the |
| 5059 | // chroma by one level. Otherwise use the largest possible trasnform size |
| 5060 | // for chroma. |
| 5061 | if (pd->subsampling_x || pd->subsampling_y) { |
| 5062 | const TX_SIZE l_max_tx_size = get_vartx_max_txsize(xd, bsizec, 0); |
| 5063 | const int is_split = |
| 5064 | (l_max_tx_size != mbmi->inter_tx_size[0][0] && bsize == bsizec && |
| 5065 | txsize_to_bsize[l_max_tx_size] == bsizec); |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 5066 | if (is_split) max_tx_size = sub_tx_size_map[1][max_tx_size]; |
Debargha Mukherjee | 1961988 | 2017-11-22 13:13:14 -0800 | [diff] [blame] | 5067 | } |
| 5068 | #endif // DISABLE_VARTX_FOR_CHROMA == 2 |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5069 | const int bh = tx_size_high_unit[max_tx_size]; |
| 5070 | const int bw = tx_size_wide_unit[max_tx_size]; |
| 5071 | int idx, idy; |
| 5072 | int block = 0; |
| 5073 | const int step = bh * bw; |
| 5074 | ENTROPY_CONTEXT ta[2 * MAX_MIB_SIZE]; |
| 5075 | ENTROPY_CONTEXT tl[2 * MAX_MIB_SIZE]; |
| 5076 | RD_STATS pn_rd_stats; |
| 5077 | av1_init_rd_stats(&pn_rd_stats); |
Debargha Mukherjee | 3aa2811 | 2017-11-25 07:03:31 -0800 | [diff] [blame] | 5078 | av1_get_entropy_contexts(bsizec, 0, pd, ta, tl); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5079 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5080 | for (idy = 0; idy < mi_height; idy += bh) { |
| 5081 | for (idx = 0; idx < mi_width; idx += bw) { |
| 5082 | tx_block_rd(cpi, x, idy, idx, plane, block, max_tx_size, plane_bsize, |
| 5083 | ta, tl, &pn_rd_stats, fast); |
| 5084 | block += step; |
| 5085 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5086 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5087 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5088 | if (pn_rd_stats.rate == INT_MAX) { |
| 5089 | is_cost_valid = 0; |
| 5090 | break; |
| 5091 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5092 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5093 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5094 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5095 | this_rd = AOMMIN(RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist), |
Debargha Mukherjee | 4d8c6fc | 2017-12-07 11:25:18 -0800 | [diff] [blame] | 5096 | RDCOST(x->rdmult, rd_stats->zero_rate, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5097 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 5098 | if (this_rd > ref_best_rd) { |
| 5099 | is_cost_valid = 0; |
| 5100 | break; |
| 5101 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | if (!is_cost_valid) { |
| 5106 | // reset cost value |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5107 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | return is_cost_valid; |
| 5111 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5112 | |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5113 | static void rd_pick_palette_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 5114 | int dc_mode_cost, |
| 5115 | uint8_t *best_palette_color_map, |
| 5116 | MB_MODE_INFO *const best_mbmi, |
| 5117 | int64_t *best_rd, int *rate, |
| 5118 | int *rate_tokenonly, int64_t *distortion, |
| 5119 | int *skippable) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5120 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5121 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5122 | assert(!is_inter_block(mbmi)); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5123 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5124 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 5125 | assert(bsize >= BLOCK_8X8); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5126 | int this_rate; |
| 5127 | int64_t this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5128 | int colors_u, colors_v, colors; |
| 5129 | const int src_stride = x->plane[1].src.stride; |
| 5130 | const uint8_t *const src_u = x->plane[1].src.buf; |
| 5131 | const uint8_t *const src_v = x->plane[2].src.buf; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5132 | uint8_t *const color_map = xd->plane[1].color_index_map; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5133 | RD_STATS tokenonly_rd_stats; |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5134 | int plane_block_width, plane_block_height, rows, cols; |
| 5135 | av1_get_block_dimensions(bsize, 1, xd, &plane_block_width, |
| 5136 | &plane_block_height, &rows, &cols); |
Hui Su | 473cf89 | 2017-11-08 18:14:31 -0800 | [diff] [blame] | 5137 | if (rows * cols > MAX_PALETTE_SQUARE) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5138 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5139 | mbmi->uv_mode = UV_DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5140 | #if CONFIG_FILTER_INTRA |
| 5141 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 5142 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5143 | |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 5144 | int count_buf[1 << 12]; // Maximum (1 << 12) color levels. |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5145 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5146 | if (cpi->common.use_highbitdepth) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5147 | colors_u = av1_count_colors_highbd(src_u, src_stride, rows, cols, |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 5148 | cpi->common.bit_depth, count_buf); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5149 | colors_v = av1_count_colors_highbd(src_v, src_stride, rows, cols, |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 5150 | cpi->common.bit_depth, count_buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5151 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5152 | #endif // CONFIG_HIGHBITDEPTH |
Hui Su | 4d51bed | 2017-11-29 15:52:40 -0800 | [diff] [blame] | 5153 | colors_u = av1_count_colors(src_u, src_stride, rows, cols, count_buf); |
| 5154 | colors_v = av1_count_colors(src_v, src_stride, rows, cols, count_buf); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5155 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5156 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5157 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5158 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5159 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5160 | uint16_t color_cache[2 * PALETTE_MAX_SIZE]; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 5161 | const int n_cache = av1_get_palette_cache(xd, 1, color_cache); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5162 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 5163 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5164 | colors = colors_u > colors_v ? colors_u : colors_v; |
| 5165 | if (colors > 1 && colors <= 64) { |
Hui Su | c3769e5 | 2017-11-16 23:13:42 -0800 | [diff] [blame] | 5166 | aom_clear_system_state(); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5167 | int r, c, n, i, j; |
| 5168 | const int max_itr = 50; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5169 | float lb_u, ub_u, val_u; |
| 5170 | float lb_v, ub_v, val_v; |
| 5171 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 5172 | float centroids[2 * PALETTE_MAX_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5173 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5174 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5175 | uint16_t *src_u16 = CONVERT_TO_SHORTPTR(src_u); |
| 5176 | uint16_t *src_v16 = CONVERT_TO_SHORTPTR(src_v); |
| 5177 | if (cpi->common.use_highbitdepth) { |
| 5178 | lb_u = src_u16[0]; |
| 5179 | ub_u = src_u16[0]; |
| 5180 | lb_v = src_v16[0]; |
| 5181 | ub_v = src_v16[0]; |
| 5182 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5183 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5184 | lb_u = src_u[0]; |
| 5185 | ub_u = src_u[0]; |
| 5186 | lb_v = src_v[0]; |
| 5187 | ub_v = src_v[0]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5188 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5189 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5190 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5191 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5192 | for (r = 0; r < rows; ++r) { |
| 5193 | for (c = 0; c < cols; ++c) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5194 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5195 | if (cpi->common.use_highbitdepth) { |
| 5196 | val_u = src_u16[r * src_stride + c]; |
| 5197 | val_v = src_v16[r * src_stride + c]; |
| 5198 | data[(r * cols + c) * 2] = val_u; |
| 5199 | data[(r * cols + c) * 2 + 1] = val_v; |
| 5200 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5201 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5202 | val_u = src_u[r * src_stride + c]; |
| 5203 | val_v = src_v[r * src_stride + c]; |
| 5204 | data[(r * cols + c) * 2] = val_u; |
| 5205 | data[(r * cols + c) * 2 + 1] = val_v; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5206 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5207 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5208 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5209 | if (val_u < lb_u) |
| 5210 | lb_u = val_u; |
| 5211 | else if (val_u > ub_u) |
| 5212 | ub_u = val_u; |
| 5213 | if (val_v < lb_v) |
| 5214 | lb_v = val_v; |
| 5215 | else if (val_v > ub_v) |
| 5216 | ub_v = val_v; |
| 5217 | } |
| 5218 | } |
| 5219 | |
| 5220 | for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2; |
| 5221 | --n) { |
| 5222 | for (i = 0; i < n; ++i) { |
| 5223 | centroids[i * 2] = lb_u + (2 * i + 1) * (ub_u - lb_u) / n / 2; |
| 5224 | centroids[i * 2 + 1] = lb_v + (2 * i + 1) * (ub_v - lb_v) / n / 2; |
| 5225 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5226 | av1_k_means(data, centroids, color_map, rows * cols, n, 2, max_itr); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 5227 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5228 | optimize_palette_colors(color_cache, n_cache, n, 2, centroids); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 5229 | // Sort the U channel colors in ascending order. |
| 5230 | for (i = 0; i < 2 * (n - 1); i += 2) { |
| 5231 | int min_idx = i; |
| 5232 | float min_val = centroids[i]; |
| 5233 | for (j = i + 2; j < 2 * n; j += 2) |
| 5234 | if (centroids[j] < min_val) min_val = centroids[j], min_idx = j; |
| 5235 | if (min_idx != i) { |
| 5236 | float temp_u = centroids[i], temp_v = centroids[i + 1]; |
| 5237 | centroids[i] = centroids[min_idx]; |
| 5238 | centroids[i + 1] = centroids[min_idx + 1]; |
| 5239 | centroids[min_idx] = temp_u, centroids[min_idx + 1] = temp_v; |
| 5240 | } |
| 5241 | } |
| 5242 | av1_calc_indices(data, centroids, color_map, rows * cols, n, 2); |
| 5243 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5244 | extend_palette_color_map(color_map, cols, rows, plane_block_width, |
| 5245 | plane_block_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5246 | pmi->palette_size[1] = n; |
| 5247 | for (i = 1; i < 3; ++i) { |
| 5248 | for (j = 0; j < n; ++j) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5249 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5250 | if (cpi->common.use_highbitdepth) |
| 5251 | pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = clip_pixel_highbd( |
| 5252 | (int)centroids[j * 2 + i - 1], cpi->common.bit_depth); |
| 5253 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5254 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5255 | pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = |
| 5256 | clip_pixel((int)centroids[j * 2 + i - 1]); |
| 5257 | } |
| 5258 | } |
| 5259 | |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5260 | super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 5261 | if (tokenonly_rd_stats.rate == INT_MAX) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5262 | this_rate = |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5263 | tokenonly_rd_stats.rate + dc_mode_cost + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5264 | x->palette_uv_size_cost[bsize - BLOCK_8X8][n - PALETTE_MIN_SIZE] + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5265 | write_uniform_cost(n, color_map[0]) + |
Yue Chen | dab2ca9 | 2017-10-16 17:48:48 -0700 | [diff] [blame] | 5266 | x->palette_uv_mode_cost[pmi->palette_size[0] > 0][1]; |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5267 | this_rate += av1_palette_color_cost_uv(pmi, |
| 5268 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 5269 | color_cache, n_cache, |
| 5270 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 5271 | cpi->common.bit_depth); |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 5272 | this_rate += |
| 5273 | av1_cost_color_map(x, 1, 0, bsize, mbmi->tx_size, PALETTE_MAP); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5274 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5275 | if (this_rd < *best_rd) { |
| 5276 | *best_rd = this_rd; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5277 | *best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5278 | memcpy(best_palette_color_map, color_map, |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5279 | plane_block_width * plane_block_height * |
| 5280 | sizeof(best_palette_color_map[0])); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5281 | *rate = this_rate; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5282 | *distortion = tokenonly_rd_stats.dist; |
| 5283 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 5284 | *skippable = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5288 | if (best_mbmi->palette_mode_info.palette_size[1] > 0) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5289 | memcpy(color_map, best_palette_color_map, |
Luc Trudeau | 0401e89 | 2017-08-31 00:37:11 -0400 | [diff] [blame] | 5290 | plane_block_width * plane_block_height * |
| 5291 | sizeof(best_palette_color_map[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5292 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5293 | } |
| 5294 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5295 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5296 | // Run RD calculation with given chroma intra prediction angle., and return |
| 5297 | // the RD cost. Update the best mode info. if the RD cost is the best so far. |
| 5298 | static int64_t pick_intra_angle_routine_sbuv( |
| 5299 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, |
| 5300 | int rate_overhead, int64_t best_rd_in, int *rate, RD_STATS *rd_stats, |
| 5301 | int *best_angle_delta, int64_t *best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5302 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5303 | assert(!is_inter_block(mbmi)); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5304 | int this_rate; |
| 5305 | int64_t this_rd; |
| 5306 | RD_STATS tokenonly_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5307 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5308 | if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in)) |
| 5309 | return INT64_MAX; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5310 | this_rate = tokenonly_rd_stats.rate + rate_overhead; |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 5311 | #if CONFIG_EXT_INTRA_MOD |
| 5312 | this_rate += x->angle_delta_cost[mbmi->uv_mode - V_PRED] |
| 5313 | [mbmi->angle_delta[1] + MAX_ANGLE_DELTA]; |
| 5314 | #endif // CONFIG_EXT_INTRA_MOD |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5315 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5316 | if (this_rd < *best_rd) { |
| 5317 | *best_rd = this_rd; |
| 5318 | *best_angle_delta = mbmi->angle_delta[1]; |
| 5319 | *rate = this_rate; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5320 | rd_stats->rate = tokenonly_rd_stats.rate; |
| 5321 | rd_stats->dist = tokenonly_rd_stats.dist; |
| 5322 | rd_stats->skip = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5323 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5324 | return this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5325 | } |
| 5326 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5327 | // With given chroma directional intra prediction mode, pick the best angle |
| 5328 | // delta. Return true if a RD cost that is smaller than the input one is found. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 5329 | static int rd_pick_intra_angle_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x, |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 5330 | BLOCK_SIZE bsize, int rate_overhead, |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5331 | int64_t best_rd, int *rate, |
| 5332 | RD_STATS *rd_stats) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5333 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5334 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5335 | assert(!is_inter_block(mbmi)); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5336 | int i, angle_delta, best_angle_delta = 0; |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5337 | int64_t this_rd, best_rd_in, rd_cost[2 * (MAX_ANGLE_DELTA + 2)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5338 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5339 | rd_stats->rate = INT_MAX; |
| 5340 | rd_stats->skip = 0; |
| 5341 | rd_stats->dist = INT64_MAX; |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5342 | for (i = 0; i < 2 * (MAX_ANGLE_DELTA + 2); ++i) rd_cost[i] = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5343 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5344 | for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5345 | for (i = 0; i < 2; ++i) { |
| 5346 | best_rd_in = (best_rd == INT64_MAX) |
| 5347 | ? INT64_MAX |
| 5348 | : (best_rd + (best_rd >> ((angle_delta == 0) ? 3 : 5))); |
| 5349 | mbmi->angle_delta[1] = (1 - 2 * i) * angle_delta; |
| 5350 | this_rd = pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, |
| 5351 | best_rd_in, rate, rd_stats, |
| 5352 | &best_angle_delta, &best_rd); |
| 5353 | rd_cost[2 * angle_delta + i] = this_rd; |
| 5354 | if (angle_delta == 0) { |
| 5355 | if (this_rd == INT64_MAX) return 0; |
| 5356 | rd_cost[1] = this_rd; |
| 5357 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5358 | } |
| 5359 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5360 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5361 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5362 | assert(best_rd != INT64_MAX); |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5363 | for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5364 | int64_t rd_thresh; |
| 5365 | for (i = 0; i < 2; ++i) { |
| 5366 | int skip_search = 0; |
| 5367 | rd_thresh = best_rd + (best_rd >> 5); |
| 5368 | if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh && |
| 5369 | rd_cost[2 * (angle_delta - 1) + i] > rd_thresh) |
| 5370 | skip_search = 1; |
| 5371 | if (!skip_search) { |
| 5372 | mbmi->angle_delta[1] = (1 - 2 * i) * angle_delta; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 5373 | pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, best_rd, |
| 5374 | rate, rd_stats, &best_angle_delta, |
| 5375 | &best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5376 | } |
| 5377 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | mbmi->angle_delta[1] = best_angle_delta; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5381 | return rd_stats->rate != INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5382 | } |
| 5383 | #endif // CONFIG_EXT_INTRA |
| 5384 | |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5385 | #if CONFIG_CFL |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5386 | static void txfm_rd_in_plane_once(MACROBLOCK *const x, |
| 5387 | const AV1_COMP *const cpi, BLOCK_SIZE bsize, |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5388 | TX_SIZE tx_size, int plane, int64_t best_rd, |
| 5389 | int64_t *dist, int *rate) { |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5390 | RD_STATS rd_stats; |
| 5391 | av1_init_rd_stats(&rd_stats); |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5392 | txfm_rd_in_plane(x, cpi, &rd_stats, best_rd, plane, bsize, tx_size, |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5393 | cpi->sf.use_fast_coef_costing); |
| 5394 | *dist = rd_stats.dist; |
| 5395 | *rate = rd_stats.rate; |
Luc Trudeau | 056d1f4 | 2017-09-15 17:38:14 -0400 | [diff] [blame] | 5396 | } |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5397 | |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5398 | static int cfl_rd_pick_alpha(MACROBLOCK *const x, const AV1_COMP *const cpi, |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5399 | BLOCK_SIZE bsize, TX_SIZE tx_size, |
| 5400 | int64_t best_rd) { |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5401 | MACROBLOCKD *const xd = &x->e_mbd; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5402 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5403 | bsize = scale_chroma_bsize(bsize, xd->plane[AOM_PLANE_U].subsampling_x, |
| 5404 | xd->plane[AOM_PLANE_U].subsampling_y); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5405 | |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5406 | int rates[CFL_PRED_PLANES][CFL_MAGS_SIZE]; |
| 5407 | int64_t dists[CFL_PRED_PLANES][CFL_MAGS_SIZE]; |
| 5408 | mbmi->cfl_alpha_idx = 0; |
| 5409 | mbmi->cfl_alpha_signs = CFL_SIGN_ZERO * CFL_SIGNS + CFL_SIGN_POS - 1; |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5410 | txfm_rd_in_plane_once(x, cpi, bsize, tx_size, AOM_PLANE_U, best_rd, |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5411 | &dists[CFL_PRED_U][0], &rates[CFL_PRED_U][0]); |
| 5412 | mbmi->cfl_alpha_signs = CFL_SIGN_POS * CFL_SIGNS + CFL_SIGN_ZERO - 1; |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5413 | txfm_rd_in_plane_once(x, cpi, bsize, tx_size, AOM_PLANE_V, best_rd, |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5414 | &dists[CFL_PRED_V][0], &rates[CFL_PRED_V][0]); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5415 | |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5416 | for (int c = 0; c < CFL_ALPHABET_SIZE; c++) { |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5417 | mbmi->cfl_alpha_idx = (c << CFL_ALPHABET_SIZE_LOG2) + c; |
| 5418 | for (int sign = CFL_SIGN_NEG; sign < CFL_SIGNS; sign++) { |
| 5419 | const int m = c * 2 + 1 + (sign == CFL_SIGN_NEG); |
| 5420 | mbmi->cfl_alpha_signs = sign * CFL_SIGNS + sign - 1; |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5421 | txfm_rd_in_plane_once(x, cpi, bsize, tx_size, AOM_PLANE_U, best_rd, |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5422 | &dists[CFL_PRED_U][m], &rates[CFL_PRED_U][m]); |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5423 | txfm_rd_in_plane_once(x, cpi, bsize, tx_size, AOM_PLANE_V, best_rd, |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5424 | &dists[CFL_PRED_V][m], &rates[CFL_PRED_V][m]); |
| 5425 | } |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5426 | } |
| 5427 | |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5428 | int64_t dist; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5429 | int64_t cost; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5430 | int64_t best_cost = INT64_MAX; |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5431 | int best_rate_overhead = INT_MAX; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5432 | #if CONFIG_DEBUG |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5433 | int best_rate = INT_MAX; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5434 | #endif // CONFIG_DEBUG |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5435 | |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5436 | int ind = 0; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5437 | int signs = 0; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5438 | |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5439 | for (int joint_sign = 0; joint_sign < CFL_JOINT_SIGNS; joint_sign++) { |
| 5440 | const int sign_u = CFL_SIGN_U(joint_sign); |
| 5441 | const int sign_v = CFL_SIGN_V(joint_sign); |
| 5442 | const int size_u = (sign_u == CFL_SIGN_ZERO) ? 1 : CFL_ALPHABET_SIZE; |
| 5443 | const int size_v = (sign_v == CFL_SIGN_ZERO) ? 1 : CFL_ALPHABET_SIZE; |
| 5444 | for (int u = 0; u < size_u; u++) { |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5445 | const int idx_u = ((sign_u == CFL_SIGN_ZERO) ? 0 : u * 2 + 1) + |
| 5446 | (sign_u == CFL_SIGN_NEG); |
| 5447 | if (rates[CFL_PRED_U][idx_u] == INT_MAX) continue; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5448 | for (int v = 0; v < size_v; v++) { |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5449 | const int idx_v = ((sign_v == CFL_SIGN_ZERO) ? 0 : v * 2 + 1) + |
| 5450 | (sign_v == CFL_SIGN_NEG); |
| 5451 | if (rates[CFL_PRED_V][idx_v] == INT_MAX) continue; |
| 5452 | dist = dists[CFL_PRED_U][idx_u] + dists[CFL_PRED_V][idx_v]; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5453 | int rate_overhead = x->cfl_cost[joint_sign][CFL_PRED_U][u] + |
| 5454 | x->cfl_cost[joint_sign][CFL_PRED_V][v]; |
| 5455 | int rate = x->intra_uv_mode_cost[mbmi->mode][UV_CFL_PRED] + |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5456 | rate_overhead + rates[CFL_PRED_U][idx_u] + |
| 5457 | rates[CFL_PRED_V][idx_v]; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5458 | cost = RDCOST(x->rdmult, rate, dist); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5459 | if (cost < best_cost) { |
| 5460 | best_cost = cost; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5461 | best_rate_overhead = rate_overhead; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5462 | ind = (u << CFL_ALPHABET_SIZE_LOG2) + v; |
| 5463 | signs = joint_sign; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5464 | #if CONFIG_DEBUG |
| 5465 | best_rate = rate; |
| 5466 | #endif // CONFIG_DEBUG |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5467 | } |
| 5468 | } |
| 5469 | } |
| 5470 | } |
| 5471 | |
| 5472 | mbmi->cfl_alpha_idx = ind; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5473 | mbmi->cfl_alpha_signs = signs; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5474 | #if CONFIG_DEBUG |
Luc Trudeau | 1e84af5 | 2017-11-25 15:00:28 -0500 | [diff] [blame] | 5475 | xd->cfl.rate = best_rate; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5476 | #endif // CONFIG_DEBUG |
| 5477 | return best_rate_overhead; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5478 | } |
| 5479 | #endif // CONFIG_CFL |
| 5480 | |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5481 | static void init_sbuv_mode(MB_MODE_INFO *const mbmi) { |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5482 | mbmi->uv_mode = UV_DC_PRED; |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5483 | mbmi->palette_mode_info.palette_size[1] = 0; |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5484 | #if CONFIG_FILTER_INTRA |
| 5485 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 5486 | #endif // CONFIG_FILTER_INTRA |
| 5487 | } |
| 5488 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 5489 | static int64_t rd_pick_intra_sbuv_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 5490 | int *rate, int *rate_tokenonly, |
| 5491 | int64_t *distortion, int *skippable, |
| 5492 | BLOCK_SIZE bsize, TX_SIZE max_tx_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5493 | MACROBLOCKD *xd = &x->e_mbd; |
| 5494 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5495 | assert(!is_inter_block(mbmi)); |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5496 | MB_MODE_INFO best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5497 | int64_t best_rd = INT64_MAX, this_rd; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5498 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Hui Su | e87fb23 | 2017-10-05 15:00:15 -0700 | [diff] [blame] | 5499 | const int try_palette = |
| 5500 | av1_allow_palette(cpi->common.allow_screen_content_tools, mbmi->sb_type); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5501 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5502 | for (int mode_idx = 0; mode_idx < UV_INTRA_MODES; ++mode_idx) { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 5503 | int this_rate; |
| 5504 | RD_STATS tokenonly_rd_stats; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5505 | UV_PREDICTION_MODE mode = uv_rd_search_mode_order[mode_idx]; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5506 | #if CONFIG_EXT_INTRA |
| 5507 | const int is_directional_mode = |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 5508 | av1_is_directional_mode(get_uv_mode(mode), mbmi->sb_type); |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5509 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 5510 | if (!(cpi->sf.intra_uv_mode_mask[txsize_sqr_up_map[max_tx_size]] & |
| 5511 | (1 << mode))) |
| 5512 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5513 | |
| 5514 | mbmi->uv_mode = mode; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5515 | #if CONFIG_CFL |
| 5516 | int cfl_alpha_rate = 0; |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 5517 | if (mode == UV_CFL_PRED) { |
Luc Trudeau | 4d6ea54 | 2017-11-22 21:24:42 -0500 | [diff] [blame] | 5518 | if (!is_cfl_allowed(mbmi)) continue; |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 5519 | assert(!is_directional_mode); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5520 | const TX_SIZE uv_tx_size = av1_get_uv_tx_size(mbmi, &xd->plane[1]); |
David Michael Barr | 9134586 | 2017-10-14 22:20:36 +0900 | [diff] [blame] | 5521 | cfl_alpha_rate = cfl_rd_pick_alpha(x, cpi, bsize, uv_tx_size, best_rd); |
| 5522 | if (cfl_alpha_rate == INT_MAX) continue; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5523 | } |
| 5524 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5525 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5526 | mbmi->angle_delta[1] = 0; |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 5527 | if (is_directional_mode && av1_use_angle_delta(mbmi->sb_type)) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5528 | const int rate_overhead = x->intra_uv_mode_cost[mbmi->mode][mode] + |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 5529 | #if CONFIG_EXT_INTRA_MOD |
| 5530 | 0; |
| 5531 | #else |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5532 | write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, 0); |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 5533 | #endif // CONFIG_EXT_INTRA_MOD |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5534 | if (!rd_pick_intra_angle_sbuv(cpi, x, bsize, rate_overhead, best_rd, |
| 5535 | &this_rate, &tokenonly_rd_stats)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5536 | continue; |
| 5537 | } else { |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5538 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5539 | if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5540 | continue; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5541 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5542 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5543 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5544 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5545 | this_rate = |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5546 | tokenonly_rd_stats.rate + x->intra_uv_mode_cost[mbmi->mode][mode]; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5547 | |
Luc Trudeau | dff4192 | 2017-07-07 09:47:58 -0400 | [diff] [blame] | 5548 | #if CONFIG_CFL |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 5549 | if (mode == UV_CFL_PRED) { |
Luc Trudeau | 4d6ea54 | 2017-11-22 21:24:42 -0500 | [diff] [blame] | 5550 | assert(is_cfl_allowed(mbmi)); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5551 | this_rate += cfl_alpha_rate; |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5552 | #if CONFIG_DEBUG |
Luc Trudeau | 1e84af5 | 2017-11-25 15:00:28 -0500 | [diff] [blame] | 5553 | assert(xd->cfl.rate == this_rate); |
David Michael Barr | 1f8d095 | 2017-10-11 17:46:39 +0900 | [diff] [blame] | 5554 | #endif // CONFIG_DEBUG |
Luc Trudeau | dff4192 | 2017-07-07 09:47:58 -0400 | [diff] [blame] | 5555 | } |
| 5556 | #endif |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5557 | #if CONFIG_EXT_INTRA |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 5558 | if (is_directional_mode && av1_use_angle_delta(mbmi->sb_type)) { |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 5559 | #if CONFIG_EXT_INTRA_MOD |
| 5560 | this_rate += x->angle_delta_cost[mode - V_PRED] |
| 5561 | [mbmi->angle_delta[1] + MAX_ANGLE_DELTA]; |
| 5562 | #else |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5563 | this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 5564 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 5565 | #endif // CONFIG_EXT_INTRA_MOD |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5566 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5567 | #endif // CONFIG_EXT_INTRA |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 5568 | |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 5569 | if (try_palette && mode == UV_DC_PRED) |
Yue Chen | dab2ca9 | 2017-10-16 17:48:48 -0700 | [diff] [blame] | 5570 | this_rate += x->palette_uv_mode_cost[pmi->palette_size[0] > 0][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5571 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5572 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5573 | |
| 5574 | if (this_rd < best_rd) { |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5575 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5576 | best_rd = this_rd; |
| 5577 | *rate = this_rate; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5578 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 5579 | *distortion = tokenonly_rd_stats.dist; |
| 5580 | *skippable = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5581 | } |
| 5582 | } |
| 5583 | |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 5584 | if (try_palette) { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 5585 | uint8_t *best_palette_color_map = x->palette_buffer->best_palette_color_map; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5586 | rd_pick_palette_intra_sbuv(cpi, x, |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5587 | x->intra_uv_mode_cost[mbmi->mode][UV_DC_PRED], |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5588 | best_palette_color_map, &best_mbmi, &best_rd, |
| 5589 | rate, rate_tokenonly, distortion, skippable); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5590 | } |
| 5591 | |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5592 | *mbmi = best_mbmi; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 5593 | // Make sure we actually chose a mode |
| 5594 | assert(best_rd < INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5595 | return best_rd; |
| 5596 | } |
| 5597 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 5598 | static void choose_intra_uv_mode(const AV1_COMP *const cpi, MACROBLOCK *const x, |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 5599 | BLOCK_SIZE bsize, TX_SIZE max_tx_size, |
| 5600 | int *rate_uv, int *rate_uv_tokenonly, |
| 5601 | int64_t *dist_uv, int *skip_uv, |
| 5602 | UV_PREDICTION_MODE *mode_uv) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5603 | MACROBLOCKD *xd = &x->e_mbd; |
| 5604 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5605 | // Use an estimated rd for uv_intra based on DC_PRED if the |
| 5606 | // appropriate speed flag is set. |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5607 | init_sbuv_mode(mbmi); |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 5608 | if (x->skip_chroma_rd) { |
| 5609 | *rate_uv = 0; |
| 5610 | *rate_uv_tokenonly = 0; |
| 5611 | *dist_uv = 0; |
| 5612 | *skip_uv = 1; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5613 | *mode_uv = UV_DC_PRED; |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 5614 | return; |
| 5615 | } |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5616 | bsize = scale_chroma_bsize(bsize, xd->plane[AOM_PLANE_U].subsampling_x, |
| 5617 | xd->plane[AOM_PLANE_U].subsampling_y); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5618 | #if CONFIG_CFL |
| 5619 | // Only store reconstructed luma when there's chroma RDO. When there's no |
| 5620 | // chroma RDO, the reconstructed luma will be stored in encode_superblock(). |
Luc Trudeau | 1e84af5 | 2017-11-25 15:00:28 -0500 | [diff] [blame] | 5621 | xd->cfl.store_y = !x->skip_chroma_rd; |
| 5622 | if (xd->cfl.store_y) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5623 | // Perform one extra call to txfm_rd_in_plane(), with the values chosen |
| 5624 | // during luma RDO, so we can store reconstructed luma values |
| 5625 | RD_STATS this_rd_stats; |
| 5626 | txfm_rd_in_plane(x, cpi, &this_rd_stats, INT64_MAX, AOM_PLANE_Y, |
| 5627 | mbmi->sb_type, mbmi->tx_size, |
| 5628 | cpi->sf.use_fast_coef_costing); |
Luc Trudeau | 1e84af5 | 2017-11-25 15:00:28 -0500 | [diff] [blame] | 5629 | xd->cfl.store_y = 0; |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5630 | } |
| 5631 | #endif // CONFIG_CFL |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 5632 | rd_pick_intra_sbuv_mode(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv, skip_uv, |
| 5633 | bsize, max_tx_size); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5634 | *mode_uv = mbmi->uv_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5635 | } |
| 5636 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5637 | static int cost_mv_ref(const MACROBLOCK *const x, PREDICTION_MODE mode, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5638 | int16_t mode_context) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5639 | if (is_inter_compound_mode(mode)) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5640 | return x |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 5641 | ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5642 | } |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 5643 | |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 5644 | int mode_cost = 0; |
| 5645 | int16_t mode_ctx = mode_context & NEWMV_CTX_MASK; |
| 5646 | int16_t is_all_zero_mv = mode_context & (1 << ALL_ZERO_FLAG_OFFSET); |
| 5647 | |
| 5648 | assert(is_inter_mode(mode)); |
| 5649 | |
| 5650 | if (mode == NEWMV) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5651 | mode_cost = x->newmv_mode_cost[mode_ctx][0]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 5652 | return mode_cost; |
| 5653 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5654 | mode_cost = x->newmv_mode_cost[mode_ctx][1]; |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5655 | mode_ctx = (mode_context >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 5656 | |
| 5657 | if (is_all_zero_mv) return mode_cost; |
| 5658 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5659 | if (mode == GLOBALMV) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5660 | mode_cost += x->zeromv_mode_cost[mode_ctx][0]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 5661 | return mode_cost; |
| 5662 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5663 | mode_cost += x->zeromv_mode_cost[mode_ctx][1]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 5664 | mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK; |
| 5665 | |
| 5666 | if (mode_context & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6; |
| 5667 | if (mode_context & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7; |
| 5668 | if (mode_context & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8; |
| 5669 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5670 | mode_cost += x->refmv_mode_cost[mode_ctx][mode != NEARESTMV]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 5671 | return mode_cost; |
| 5672 | } |
| 5673 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5674 | } |
| 5675 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 5676 | static int get_interinter_compound_type_bits(BLOCK_SIZE bsize, |
| 5677 | COMPOUND_TYPE comp_type) { |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 5678 | (void)bsize; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 5679 | switch (comp_type) { |
| 5680 | case COMPOUND_AVERAGE: return 0; |
| 5681 | case COMPOUND_WEDGE: return get_interinter_wedge_bits(bsize); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 5682 | case COMPOUND_SEG: return 1; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 5683 | default: assert(0); return 0; |
| 5684 | } |
| 5685 | } |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 5686 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5687 | typedef struct { |
| 5688 | int eobs; |
| 5689 | int brate; |
| 5690 | int byrate; |
| 5691 | int64_t bdist; |
| 5692 | int64_t bsse; |
| 5693 | int64_t brdcost; |
| 5694 | int_mv mvs[2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5695 | int_mv pred_mv[2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5696 | int_mv ref_mv[2]; |
Jingning Han | 276c294 | 2016-12-05 12:37:02 -0800 | [diff] [blame] | 5697 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5698 | ENTROPY_CONTEXT ta[2]; |
| 5699 | ENTROPY_CONTEXT tl[2]; |
| 5700 | } SEG_RDSTAT; |
| 5701 | |
| 5702 | typedef struct { |
| 5703 | int_mv *ref_mv[2]; |
| 5704 | int_mv mvp; |
| 5705 | |
| 5706 | int64_t segment_rd; |
| 5707 | int r; |
| 5708 | int64_t d; |
| 5709 | int64_t sse; |
| 5710 | int segment_yrate; |
| 5711 | PREDICTION_MODE modes[4]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5712 | SEG_RDSTAT rdstat[4][INTER_MODES + INTER_COMPOUND_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5713 | int mvthresh; |
| 5714 | } BEST_SEG_INFO; |
| 5715 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 5716 | static INLINE int mv_check_bounds(const MvLimits *mv_limits, const MV *mv) { |
| 5717 | return (mv->row >> 3) < mv_limits->row_min || |
| 5718 | (mv->row >> 3) > mv_limits->row_max || |
| 5719 | (mv->col >> 3) < mv_limits->col_min || |
| 5720 | (mv->col >> 3) > mv_limits->col_max; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5721 | } |
| 5722 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5723 | // Check if NEARESTMV/NEARMV/GLOBALMV is the cheapest way encode zero motion. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5724 | // TODO(aconverse): Find out if this is still productive then clean up or remove |
| 5725 | static int check_best_zero_mv( |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5726 | const AV1_COMP *const cpi, const MACROBLOCK *const x, |
| 5727 | const int16_t mode_context[TOTAL_REFS_PER_FRAME], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5728 | const int16_t compound_mode_context[TOTAL_REFS_PER_FRAME], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5729 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME], int this_mode, |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 5730 | const MV_REFERENCE_FRAME ref_frames[2], const BLOCK_SIZE bsize, int block, |
| 5731 | int mi_row, int mi_col) { |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 5732 | int_mv zeromv[2] = { {.as_int = 0 } }; |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 5733 | int comp_pred_mode = ref_frames[1] > INTRA_FRAME; |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 5734 | (void)mi_row; |
| 5735 | (void)mi_col; |
Zoe Liu | bc030ee | 2017-07-31 15:20:46 -0700 | [diff] [blame] | 5736 | (void)cpi; |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5737 | if (this_mode == GLOBALMV || this_mode == GLOBAL_GLOBALMV) { |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 5738 | for (int cur_frm = 0; cur_frm < 1 + comp_pred_mode; cur_frm++) { |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 5739 | zeromv[cur_frm].as_int = |
| 5740 | gm_get_motion_vector(&cpi->common.global_motion[ref_frames[cur_frm]], |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 5741 | cpi->common.allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 5742 | mi_col, mi_row, block |
| 5743 | #if CONFIG_AMVR |
| 5744 | , |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 5745 | cpi->common.cur_frame_force_integer_mv |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 5746 | #endif |
| 5747 | ) |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 5748 | .as_int; |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 5749 | } |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 5750 | } |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 5751 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5752 | if ((this_mode == NEARMV || this_mode == NEARESTMV || |
| 5753 | this_mode == GLOBALMV) && |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 5754 | frame_mv[this_mode][ref_frames[0]].as_int == zeromv[0].as_int && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5755 | (ref_frames[1] <= INTRA_FRAME || |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 5756 | frame_mv[this_mode][ref_frames[1]].as_int == zeromv[1].as_int)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5757 | int16_t rfc = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5758 | av1_mode_context_analyzer(mode_context, ref_frames, bsize, block); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5759 | int c1 = cost_mv_ref(x, NEARMV, rfc); |
| 5760 | int c2 = cost_mv_ref(x, NEARESTMV, rfc); |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5761 | int c3 = cost_mv_ref(x, GLOBALMV, rfc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5762 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5763 | if (this_mode == NEARMV) { |
| 5764 | if (c1 > c3) return 0; |
| 5765 | } else if (this_mode == NEARESTMV) { |
| 5766 | if (c2 > c3) return 0; |
| 5767 | } else { |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5768 | assert(this_mode == GLOBALMV); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5769 | if (ref_frames[1] <= INTRA_FRAME) { |
| 5770 | if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0) || |
| 5771 | (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0)) |
| 5772 | return 0; |
| 5773 | } else { |
| 5774 | if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0 && |
| 5775 | frame_mv[NEARESTMV][ref_frames[1]].as_int == 0) || |
| 5776 | (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0 && |
| 5777 | frame_mv[NEARMV][ref_frames[1]].as_int == 0)) |
| 5778 | return 0; |
| 5779 | } |
| 5780 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 5781 | } else if ((this_mode == NEAREST_NEARESTMV || this_mode == NEAR_NEARMV || |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5782 | this_mode == GLOBAL_GLOBALMV) && |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 5783 | frame_mv[this_mode][ref_frames[0]].as_int == zeromv[0].as_int && |
| 5784 | frame_mv[this_mode][ref_frames[1]].as_int == zeromv[1].as_int) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5785 | int16_t rfc = compound_mode_context[ref_frames[0]]; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5786 | int c2 = cost_mv_ref(x, NEAREST_NEARESTMV, rfc); |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5787 | int c3 = cost_mv_ref(x, GLOBAL_GLOBALMV, rfc); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5788 | int c5 = cost_mv_ref(x, NEAR_NEARMV, rfc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5789 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 5790 | if (this_mode == NEAREST_NEARESTMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5791 | if (c2 > c3) return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5792 | } else if (this_mode == NEAR_NEARMV) { |
| 5793 | if (c5 > c3) return 0; |
| 5794 | } else { |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 5795 | assert(this_mode == GLOBAL_GLOBALMV); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5796 | if ((c3 >= c2 && frame_mv[NEAREST_NEARESTMV][ref_frames[0]].as_int == 0 && |
| 5797 | frame_mv[NEAREST_NEARESTMV][ref_frames[1]].as_int == 0) || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5798 | (c3 >= c5 && frame_mv[NEAR_NEARMV][ref_frames[0]].as_int == 0 && |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 5799 | frame_mv[NEAR_NEARMV][ref_frames[1]].as_int == 0)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5800 | return 0; |
| 5801 | } |
| 5802 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5803 | return 1; |
| 5804 | } |
| 5805 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 5806 | static void joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x, |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5807 | BLOCK_SIZE bsize, int_mv *frame_mv, int mi_row, |
| 5808 | int mi_col, int_mv *ref_mv_sub8x8[2], |
| 5809 | const uint8_t *mask, int mask_stride, |
| 5810 | int *rate_mv, const int block) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5811 | const AV1_COMMON *const cm = &cpi->common; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 5812 | const int pw = block_size_wide[bsize]; |
| 5813 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5814 | MACROBLOCKD *xd = &x->e_mbd; |
| 5815 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5816 | // This function should only ever be called for compound modes |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 5817 | assert(has_second_ref(mbmi)); |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 5818 | const int refs[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5819 | int_mv ref_mv[2]; |
| 5820 | int ite, ref; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5821 | struct scale_factors sf; |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 5822 | // ic and ir are the 4x4 coordinates of the sub8x8 at index "block" |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 5823 | const int ic = block & 1; |
| 5824 | const int ir = (block - ic) >> 1; |
Jingning Han | cb63767 | 2017-06-22 09:14:40 -0700 | [diff] [blame] | 5825 | struct macroblockd_plane *const pd = &xd->plane[0]; |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 5826 | const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x) + 4 * ic; |
| 5827 | const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y) + 4 * ir; |
| 5828 | int is_global[2]; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5829 | for (ref = 0; ref < 2; ++ref) { |
Luc Trudeau | f3bf8b1 | 2017-12-08 14:38:41 -0500 | [diff] [blame] | 5830 | const WarpedMotionParams *const wm = |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 5831 | &xd->global_motion[xd->mi[0]->mbmi.ref_frame[ref]]; |
| 5832 | is_global[ref] = is_global_mv_block(xd->mi[0], block, wm->wmtype); |
| 5833 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5834 | |
| 5835 | // Do joint motion search in compound mode to get more accurate mv. |
| 5836 | struct buf_2d backup_yv12[2][MAX_MB_PLANE]; |
| 5837 | int last_besterr[2] = { INT_MAX, INT_MAX }; |
| 5838 | const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = { |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 5839 | av1_get_scaled_ref_frame(cpi, refs[0]), |
| 5840 | av1_get_scaled_ref_frame(cpi, refs[1]) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5841 | }; |
| 5842 | |
| 5843 | // Prediction buffer from second frame. |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5844 | #if CONFIG_HIGHBITDEPTH |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5845 | DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]); |
| 5846 | uint8_t *second_pred; |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5847 | #else // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5848 | DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5849 | #endif // CONFIG_HIGHBITDEPTH |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 5850 | (void)ref_mv_sub8x8; |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 5851 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5852 | for (ref = 0; ref < 2; ++ref) { |
Debargha Mukherjee | 6ea917e | 2017-10-19 09:31:29 -0700 | [diff] [blame] | 5853 | ref_mv[ref] = x->mbmi_ext->ref_mvs[refs[ref]][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5854 | |
| 5855 | if (scaled_ref_frame[ref]) { |
| 5856 | int i; |
| 5857 | // Swap out the reference frame for a version that's been scaled to |
| 5858 | // match the resolution of the current frame, allowing the existing |
| 5859 | // motion search code to be used without additional modifications. |
| 5860 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 5861 | backup_yv12[ref][i] = xd->plane[i].pre[ref]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5862 | av1_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col, |
| 5863 | NULL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5864 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | // Since we have scaled the reference frames to match the size of the current |
| 5868 | // frame we must use a unit scaling factor during mode selection. |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5869 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5870 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 5871 | cm->height, cm->use_highbitdepth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5872 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5873 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 5874 | cm->height); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5875 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5876 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5877 | // Allow joint search multiple times iteratively for each reference frame |
| 5878 | // and break out of the search loop if it couldn't find a better mv. |
| 5879 | for (ite = 0; ite < 4; ite++) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5880 | struct buf_2d ref_yv12[2]; |
| 5881 | int bestsme = INT_MAX; |
| 5882 | int sadpb = x->sadperbit16; |
| 5883 | MV *const best_mv = &x->best_mv.as_mv; |
| 5884 | int search_range = 3; |
| 5885 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 5886 | MvLimits tmp_mv_limits = x->mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5887 | int id = ite % 2; // Even iterations search in the first reference frame, |
| 5888 | // odd iterations search in the second. The predictor |
| 5889 | // found for the 'other' reference frame is factored in. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 5890 | const int plane = 0; |
David Barker | e64d51a | 2017-06-09 14:52:42 +0100 | [diff] [blame] | 5891 | ConvolveParams conv_params = get_conv_params(!id, 0, plane); |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5892 | #if CONFIG_JNT_COMP |
Cheng Chen | 8b1732a | 2017-11-22 18:38:49 -0800 | [diff] [blame] | 5893 | conv_params.use_jnt_comp_avg = 0; |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5894 | #endif |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 5895 | WarpTypesAllowed warp_types; |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 5896 | warp_types.global_warp_allowed = is_global[!id]; |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 5897 | warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5898 | |
| 5899 | // Initialized here because of compiler problem in Visual Studio. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 5900 | ref_yv12[0] = xd->plane[plane].pre[0]; |
| 5901 | ref_yv12[1] = xd->plane[plane].pre[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5902 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5903 | // Get the prediction block from the 'other' reference frame. |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5904 | #if CONFIG_JNT_COMP |
| 5905 | InterpFilters interp_filters = EIGHTTAP_REGULAR; |
| 5906 | #endif // CONFIG_JNT_COMP |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 5907 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5908 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5909 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 5910 | second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5911 | av1_highbd_build_inter_predictor( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5912 | ref_yv12[!id].buf, ref_yv12[!id].stride, second_pred, pw, |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 5913 | &frame_mv[refs[!id]].as_mv, |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5914 | #if CONFIG_JNT_COMP |
| 5915 | &sf, pw, ph, 0, interp_filters, |
| 5916 | #else |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 5917 | &sf, pw, ph, 0, mbmi->interp_filters, |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5918 | #endif // CONFIG_JNT_COMP |
Sebastien Alaiwan | 4879580 | 2017-10-30 12:07:13 +0100 | [diff] [blame] | 5919 | &warp_types, p_col, p_row, plane, MV_PRECISION_Q3, mi_col * MI_SIZE, |
| 5920 | mi_row * MI_SIZE, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5921 | } else { |
| 5922 | second_pred = (uint8_t *)second_pred_alloc_16; |
Zoe Liu | 76fcff7 | 2017-04-24 17:50:53 -0700 | [diff] [blame] | 5923 | #endif // CONFIG_HIGHBITDEPTH |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5924 | av1_build_inter_predictor(ref_yv12[!id].buf, ref_yv12[!id].stride, |
| 5925 | second_pred, pw, &frame_mv[refs[!id]].as_mv, |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5926 | #if CONFIG_JNT_COMP |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5927 | &sf, pw, ph, &conv_params, interp_filters, |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5928 | #else |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5929 | &sf, pw, ph, &conv_params, mbmi->interp_filters, |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5930 | #endif // CONFIG_JNT_COMP |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5931 | &warp_types, p_col, p_row, plane, !id, |
| 5932 | MV_PRECISION_Q3, mi_col * MI_SIZE, |
| 5933 | mi_row * MI_SIZE, xd); |
Zoe Liu | 76fcff7 | 2017-04-24 17:50:53 -0700 | [diff] [blame] | 5934 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5935 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5936 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5937 | |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5938 | #if CONFIG_JNT_COMP |
| 5939 | const int order_idx = id != 0; |
Cheng Chen | f78632e | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 5940 | av1_jnt_comp_weight_assign(cm, mbmi, order_idx, &xd->jcp_param.fwd_offset, |
Cheng Chen | 8263f80 | 2017-11-14 15:50:00 -0800 | [diff] [blame] | 5941 | &xd->jcp_param.bck_offset, |
| 5942 | &xd->jcp_param.use_jnt_comp_avg, 1); |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 5943 | #endif // CONFIG_JNT_COMP |
| 5944 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5945 | // Do compound motion search on the current reference frame. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 5946 | if (id) xd->plane[plane].pre[0] = ref_yv12[id]; |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 5947 | av1_set_mv_search_range(&x->mv_limits, &ref_mv[id].as_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5948 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5949 | // Use the mv result from the single mode as mv predictor. |
| 5950 | // Use the mv result from the single mode as mv predictor. |
| 5951 | *best_mv = frame_mv[refs[id]].as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5952 | |
| 5953 | best_mv->col >>= 3; |
| 5954 | best_mv->row >>= 3; |
| 5955 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5956 | av1_set_mvcost(x, refs[id], id, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5957 | |
| 5958 | // Small-range full-pixel motion search. |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 5959 | bestsme = av1_refining_search_8p_c(x, sadpb, search_range, |
| 5960 | &cpi->fn_ptr[bsize], mask, mask_stride, |
| 5961 | id, &ref_mv[id].as_mv, second_pred); |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 5962 | if (bestsme < INT_MAX) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 5963 | if (mask) |
| 5964 | bestsme = av1_get_mvpred_mask_var(x, best_mv, &ref_mv[id].as_mv, |
| 5965 | second_pred, mask, mask_stride, id, |
| 5966 | &cpi->fn_ptr[bsize], 1); |
| 5967 | else |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 5968 | bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv[id].as_mv, |
| 5969 | second_pred, &cpi->fn_ptr[bsize], 1); |
| 5970 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5971 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 5972 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5973 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 5974 | #if CONFIG_AMVR |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 5975 | if (cpi->common.cur_frame_force_integer_mv) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 5976 | x->best_mv.as_mv.row *= 8; |
| 5977 | x->best_mv.as_mv.col *= 8; |
| 5978 | } |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 5979 | if (bestsme < INT_MAX && cpi->common.cur_frame_force_integer_mv == 0) |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 5980 | #else |
Cheng Chen | 1483a71 | 2017-10-08 13:07:02 -0700 | [diff] [blame] | 5981 | if (bestsme < INT_MAX) |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 5982 | #endif |
Cheng Chen | 1483a71 | 2017-10-08 13:07:02 -0700 | [diff] [blame] | 5983 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5984 | int dis; /* TODO: use dis in distortion calculation later. */ |
| 5985 | unsigned int sse; |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 5986 | bestsme = cpi->find_fractional_mv_step( |
| 5987 | x, &ref_mv[id].as_mv, cpi->common.allow_high_precision_mv, |
| 5988 | x->errorperbit, &cpi->fn_ptr[bsize], 0, |
| 5989 | cpi->sf.mv.subpel_iters_per_step, NULL, x->nmvjointcost, x->mvcost, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 5990 | &dis, &sse, second_pred, mask, mask_stride, id, pw, ph, |
| 5991 | cpi->sf.use_upsampled_references); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | // Restore the pointer to the first (possibly scaled) prediction buffer. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 5995 | if (id) xd->plane[plane].pre[0] = ref_yv12[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5996 | |
| 5997 | if (bestsme < last_besterr[id]) { |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 5998 | frame_mv[refs[id]].as_mv = *best_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5999 | last_besterr[id] = bestsme; |
| 6000 | } else { |
| 6001 | break; |
| 6002 | } |
| 6003 | } |
| 6004 | |
| 6005 | *rate_mv = 0; |
| 6006 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6007 | for (ref = 0; ref < 2; ++ref) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6008 | if (scaled_ref_frame[ref]) { |
| 6009 | // Restore the prediction frame pointers to their unscaled versions. |
| 6010 | int i; |
| 6011 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6012 | xd->plane[i].pre[ref] = backup_yv12[ref][i]; |
| 6013 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6014 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6015 | av1_set_mvcost(x, refs[ref], ref, mbmi->ref_mv_idx); |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6016 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6017 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv, |
| 6018 | &x->mbmi_ext->ref_mvs[refs[ref]][0].as_mv, |
| 6019 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6020 | } |
| 6021 | } |
| 6022 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6023 | static void estimate_ref_frame_costs( |
Yue Chen | 170678a | 2017-10-17 13:43:10 -0700 | [diff] [blame] | 6024 | const AV1_COMMON *cm, const MACROBLOCKD *xd, const MACROBLOCK *x, |
| 6025 | int segment_id, unsigned int *ref_costs_single, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6026 | #if CONFIG_EXT_COMP_REFS |
| 6027 | unsigned int (*ref_costs_comp)[TOTAL_REFS_PER_FRAME], |
| 6028 | #else |
| 6029 | unsigned int *ref_costs_comp, |
| 6030 | #endif // CONFIG_EXT_COMP_REFS |
| 6031 | aom_prob *comp_mode_p) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6032 | int seg_ref_active = |
| 6033 | segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME); |
| 6034 | if (seg_ref_active) { |
| 6035 | memset(ref_costs_single, 0, |
| 6036 | TOTAL_REFS_PER_FRAME * sizeof(*ref_costs_single)); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6037 | #if CONFIG_EXT_COMP_REFS |
| 6038 | int ref_frame; |
| 6039 | for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame) |
| 6040 | memset(ref_costs_comp[ref_frame], 0, |
| 6041 | TOTAL_REFS_PER_FRAME * sizeof((*ref_costs_comp)[0])); |
| 6042 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6043 | memset(ref_costs_comp, 0, TOTAL_REFS_PER_FRAME * sizeof(*ref_costs_comp)); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6044 | #endif // CONFIG_EXT_COMP_REFS |
| 6045 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6046 | *comp_mode_p = 128; |
| 6047 | } else { |
Yue Chen | 170678a | 2017-10-17 13:43:10 -0700 | [diff] [blame] | 6048 | int intra_inter_ctx = av1_get_intra_inter_context(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6049 | |
| 6050 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
Sebastien Alaiwan | feca25a | 2017-11-29 11:22:23 +0100 | [diff] [blame] | 6051 | *comp_mode_p = av1_get_reference_mode_prob(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6052 | } else { |
| 6053 | *comp_mode_p = 128; |
| 6054 | } |
| 6055 | |
Yue Chen | 170678a | 2017-10-17 13:43:10 -0700 | [diff] [blame] | 6056 | ref_costs_single[INTRA_FRAME] = x->intra_inter_cost[intra_inter_ctx][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6057 | |
Zoe Liu | d4d8b86 | 2017-12-06 10:56:01 -0800 | [diff] [blame] | 6058 | unsigned int base_cost = x->intra_inter_cost[intra_inter_ctx][1]; |
| 6059 | |
| 6060 | #if !CONFIG_REF_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6061 | if (cm->reference_mode != COMPOUND_REFERENCE) { |
Zoe Liu | d4d8b86 | 2017-12-06 10:56:01 -0800 | [diff] [blame] | 6062 | #endif // !CONFIG_REF_ADAPT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6063 | aom_prob ref_single_p1 = av1_get_pred_prob_single_ref_p1(cm, xd); |
| 6064 | aom_prob ref_single_p2 = av1_get_pred_prob_single_ref_p2(cm, xd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6065 | aom_prob ref_single_p3 = av1_get_pred_prob_single_ref_p3(cm, xd); |
| 6066 | aom_prob ref_single_p4 = av1_get_pred_prob_single_ref_p4(cm, xd); |
| 6067 | aom_prob ref_single_p5 = av1_get_pred_prob_single_ref_p5(cm, xd); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6068 | aom_prob ref_single_p6 = av1_get_pred_prob_single_ref_p6(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6069 | |
Sebastien Alaiwan | 365e644 | 2017-10-16 11:35:00 +0200 | [diff] [blame] | 6070 | ref_costs_single[LAST_FRAME] = ref_costs_single[LAST2_FRAME] = |
| 6071 | ref_costs_single[LAST3_FRAME] = ref_costs_single[BWDREF_FRAME] = |
| 6072 | ref_costs_single[ALTREF2_FRAME] = ref_costs_single[GOLDEN_FRAME] = |
| 6073 | ref_costs_single[ALTREF_FRAME] = base_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6074 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6075 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6076 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6077 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6078 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6079 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6080 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6081 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6082 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6083 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p3, 0); |
| 6084 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p3, 0); |
| 6085 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p3, 1); |
| 6086 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p3, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6087 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6088 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p2, 0); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6089 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p2, 0); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6090 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6091 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6092 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p4, 0); |
| 6093 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p4, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6094 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6095 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p5, 0); |
| 6096 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p5, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6097 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6098 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p6, 0); |
| 6099 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p6, 1); |
Zoe Liu | d4d8b86 | 2017-12-06 10:56:01 -0800 | [diff] [blame] | 6100 | #if !CONFIG_REF_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6101 | } else { |
| 6102 | ref_costs_single[LAST_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6103 | ref_costs_single[LAST2_FRAME] = 512; |
| 6104 | ref_costs_single[LAST3_FRAME] = 512; |
| 6105 | ref_costs_single[BWDREF_FRAME] = 512; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6106 | ref_costs_single[ALTREF2_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6107 | ref_costs_single[GOLDEN_FRAME] = 512; |
| 6108 | ref_costs_single[ALTREF_FRAME] = 512; |
| 6109 | } |
Zoe Liu | d4d8b86 | 2017-12-06 10:56:01 -0800 | [diff] [blame] | 6110 | #endif // !CONFIG_REF_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6111 | |
| 6112 | if (cm->reference_mode != SINGLE_REFERENCE) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6113 | aom_prob ref_comp_p = av1_get_pred_prob_comp_ref_p(cm, xd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6114 | aom_prob ref_comp_p1 = av1_get_pred_prob_comp_ref_p1(cm, xd); |
| 6115 | aom_prob ref_comp_p2 = av1_get_pred_prob_comp_ref_p2(cm, xd); |
| 6116 | aom_prob bwdref_comp_p = av1_get_pred_prob_comp_bwdref_p(cm, xd); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6117 | aom_prob bwdref_comp_p1 = av1_get_pred_prob_comp_bwdref_p1(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6118 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6119 | #if CONFIG_EXT_COMP_REFS |
| 6120 | aom_prob comp_ref_type_p = av1_get_comp_reference_type_prob(cm, xd); |
| 6121 | unsigned int ref_bicomp_costs[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 6122 | |
| 6123 | ref_bicomp_costs[LAST_FRAME] = ref_bicomp_costs[LAST2_FRAME] = |
| 6124 | ref_bicomp_costs[LAST3_FRAME] = ref_bicomp_costs[GOLDEN_FRAME] = |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6125 | base_cost + av1_cost_bit(comp_ref_type_p, 1); |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6126 | ref_bicomp_costs[BWDREF_FRAME] = ref_bicomp_costs[ALTREF2_FRAME] = 0; |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6127 | ref_bicomp_costs[ALTREF_FRAME] = 0; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6128 | |
| 6129 | ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6130 | ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6131 | ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6132 | ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6133 | |
Zoe Liu | 8781828 | 2017-11-26 17:09:59 -0800 | [diff] [blame] | 6134 | ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 0); |
| 6135 | ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 1); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6136 | |
| 6137 | ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0); |
| 6138 | ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1); |
| 6139 | |
| 6140 | ref_bicomp_costs[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6141 | ref_bicomp_costs[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6142 | ref_bicomp_costs[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1); |
| 6143 | |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6144 | ref_bicomp_costs[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p1, 0); |
| 6145 | ref_bicomp_costs[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p1, 1); |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6146 | |
| 6147 | int ref0, ref1; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6148 | for (ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) { |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6149 | for (ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) { |
| 6150 | ref_costs_comp[ref0][ref1] = |
| 6151 | ref_bicomp_costs[ref0] + ref_bicomp_costs[ref1]; |
| 6152 | } |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6153 | } |
| 6154 | |
| 6155 | aom_prob uni_comp_ref_p = av1_get_pred_prob_uni_comp_ref_p(cm, xd); |
| 6156 | aom_prob uni_comp_ref_p1 = av1_get_pred_prob_uni_comp_ref_p1(cm, xd); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6157 | aom_prob uni_comp_ref_p2 = av1_get_pred_prob_uni_comp_ref_p2(cm, xd); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6158 | |
| 6159 | ref_costs_comp[LAST_FRAME][LAST2_FRAME] = |
| 6160 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6161 | av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 0); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6162 | ref_costs_comp[LAST_FRAME][LAST3_FRAME] = |
| 6163 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6164 | av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 1) + |
| 6165 | av1_cost_bit(uni_comp_ref_p2, 0); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6166 | ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = |
| 6167 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6168 | av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 1) + |
| 6169 | av1_cost_bit(uni_comp_ref_p2, 1); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6170 | |
| 6171 | ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = |
| 6172 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6173 | av1_cost_bit(uni_comp_ref_p, 1); |
| 6174 | |
Sebastien Alaiwan | 365e644 | 2017-10-16 11:35:00 +0200 | [diff] [blame] | 6175 | #else // !CONFIG_EXT_COMP_REFS |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6176 | |
Sebastien Alaiwan | 365e644 | 2017-10-16 11:35:00 +0200 | [diff] [blame] | 6177 | ref_costs_comp[LAST_FRAME] = ref_costs_comp[LAST2_FRAME] = |
| 6178 | ref_costs_comp[LAST3_FRAME] = ref_costs_comp[GOLDEN_FRAME] = |
| 6179 | base_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6180 | |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6181 | ref_costs_comp[BWDREF_FRAME] = ref_costs_comp[ALTREF2_FRAME] = |
| 6182 | ref_costs_comp[ALTREF_FRAME] = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6183 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6184 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6185 | ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6186 | ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6187 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6188 | |
Zoe Liu | 8781828 | 2017-11-26 17:09:59 -0800 | [diff] [blame] | 6189 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 0); |
| 6190 | ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6191 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6192 | ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0); |
| 6193 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6194 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6195 | // NOTE(zoeliu): BWDREF and ALTREF each add an extra cost by coding 1 |
| 6196 | // more bit. |
| 6197 | ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6198 | ref_costs_comp[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6199 | ref_costs_comp[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6200 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6201 | ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p1, 0); |
| 6202 | ref_costs_comp[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p1, 1); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6203 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6204 | } else { |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6205 | #if CONFIG_EXT_COMP_REFS |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6206 | int ref0, ref1; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6207 | for (ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) { |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6208 | for (ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) |
| 6209 | ref_costs_comp[ref0][ref1] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6210 | } |
| 6211 | ref_costs_comp[LAST_FRAME][LAST2_FRAME] = 512; |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6212 | ref_costs_comp[LAST_FRAME][LAST3_FRAME] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6213 | ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = 512; |
| 6214 | ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = 512; |
Sebastien Alaiwan | 365e644 | 2017-10-16 11:35:00 +0200 | [diff] [blame] | 6215 | #else // !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6216 | ref_costs_comp[LAST_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6217 | ref_costs_comp[LAST2_FRAME] = 512; |
| 6218 | ref_costs_comp[LAST3_FRAME] = 512; |
| 6219 | ref_costs_comp[BWDREF_FRAME] = 512; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6220 | ref_costs_comp[ALTREF2_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6221 | ref_costs_comp[ALTREF_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6222 | ref_costs_comp[GOLDEN_FRAME] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6223 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6224 | } |
| 6225 | } |
| 6226 | } |
| 6227 | |
| 6228 | static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, |
| 6229 | int mode_index, |
| 6230 | int64_t comp_pred_diff[REFERENCE_MODES], |
| 6231 | int skippable) { |
| 6232 | MACROBLOCKD *const xd = &x->e_mbd; |
| 6233 | |
| 6234 | // Take a snapshot of the coding context so it can be |
| 6235 | // restored if we decide to encode this way |
| 6236 | ctx->skip = x->skip; |
| 6237 | ctx->skippable = skippable; |
| 6238 | ctx->best_mode_index = mode_index; |
| 6239 | ctx->mic = *xd->mi[0]; |
| 6240 | ctx->mbmi_ext = *x->mbmi_ext; |
| 6241 | ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE]; |
| 6242 | ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE]; |
| 6243 | ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT]; |
| 6244 | } |
| 6245 | |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 6246 | static void setup_buffer_inter( |
| 6247 | const AV1_COMP *const cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame, |
| 6248 | BLOCK_SIZE block_size, int mi_row, int mi_col, |
| 6249 | int_mv frame_nearest_mv[TOTAL_REFS_PER_FRAME], |
| 6250 | int_mv frame_near_mv[TOTAL_REFS_PER_FRAME], |
| 6251 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6252 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6253 | const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame); |
| 6254 | MACROBLOCKD *const xd = &x->e_mbd; |
| 6255 | MODE_INFO *const mi = xd->mi[0]; |
| 6256 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 6257 | const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf; |
| 6258 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 6259 | |
| 6260 | assert(yv12 != NULL); |
| 6261 | |
| 6262 | // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this |
| 6263 | // use the UV scaling factors. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6264 | 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] | 6265 | |
| 6266 | // Gets an initial list of candidate vectors from neighbours and orders them |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 6267 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 6268 | mbmi_ext->ref_mv_stack[ref_frame], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6269 | mbmi_ext->compound_mode_context, candidates, mi_row, mi_col, |
| 6270 | NULL, NULL, mbmi_ext->mode_context); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6271 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6272 | // Candidate refinement carried out at encoder and decoder |
| 6273 | #if CONFIG_AMVR |
| 6274 | av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates, |
| 6275 | &frame_nearest_mv[ref_frame], &frame_near_mv[ref_frame], |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 6276 | cm->cur_frame_force_integer_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6277 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6278 | av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates, |
| 6279 | &frame_nearest_mv[ref_frame], |
| 6280 | &frame_near_mv[ref_frame]); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6281 | #endif |
Debargha Mukherjee | 6ea917e | 2017-10-19 09:31:29 -0700 | [diff] [blame] | 6282 | // Further refinement that is encode side only to test the top few candidates |
| 6283 | // in full and choose the best as the centre point for subsequent searches. |
| 6284 | // The current implementation doesn't support scaling. |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 6285 | av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame, |
| 6286 | block_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6287 | } |
| 6288 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6289 | static void single_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 6290 | BLOCK_SIZE bsize, int mi_row, int mi_col, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6291 | int ref_idx, int *rate_mv) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6292 | MACROBLOCKD *xd = &x->e_mbd; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6293 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6294 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6295 | struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } }; |
| 6296 | int bestsme = INT_MAX; |
| 6297 | int step_param; |
| 6298 | int sadpb = x->sadperbit16; |
| 6299 | MV mvp_full; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6300 | int ref = mbmi->ref_frame[ref_idx]; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 6301 | MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6302 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6303 | MvLimits tmp_mv_limits = x->mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6304 | int cost_list[5]; |
| 6305 | |
| 6306 | const YV12_BUFFER_CONFIG *scaled_ref_frame = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6307 | av1_get_scaled_ref_frame(cpi, ref); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6308 | |
| 6309 | MV pred_mv[3]; |
| 6310 | pred_mv[0] = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 6311 | pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv; |
| 6312 | pred_mv[2] = x->pred_mv[ref]; |
| 6313 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6314 | if (scaled_ref_frame) { |
| 6315 | int i; |
| 6316 | // Swap out the reference frame for a version that's been scaled to |
| 6317 | // match the resolution of the current frame, allowing the existing |
| 6318 | // motion search code to be used without additional modifications. |
| 6319 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6320 | backup_yv12[i] = xd->plane[i].pre[ref_idx]; |
| 6321 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6322 | 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] | 6323 | } |
| 6324 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6325 | av1_set_mv_search_range(&x->mv_limits, &ref_mv); |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6326 | |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6327 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6328 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6329 | // 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] | 6330 | // 0 here is maximum length first step. 1 is AOMMAX >> 1 etc. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6331 | if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) { |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 6332 | // Take the weighted average of the step_params based on the last frame's |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6333 | // max mv magnitude and that based on the best ref mvs of the current |
| 6334 | // block for the given reference. |
| 6335 | step_param = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6336 | (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] | 6337 | 2; |
| 6338 | } else { |
| 6339 | step_param = cpi->mv_step_param; |
| 6340 | } |
| 6341 | |
| 6342 | if (cpi->sf.adaptive_motion_search && bsize < cm->sb_size) { |
| 6343 | int boffset = |
| 6344 | 2 * (b_width_log2_lookup[cm->sb_size] - |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6345 | AOMMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize])); |
| 6346 | step_param = AOMMAX(step_param, boffset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6347 | } |
| 6348 | |
| 6349 | if (cpi->sf.adaptive_motion_search) { |
| 6350 | int bwl = b_width_log2_lookup[bsize]; |
| 6351 | int bhl = b_height_log2_lookup[bsize]; |
| 6352 | int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4); |
| 6353 | |
Debargha Mukherjee | 27be874 | 2017-10-07 23:51:10 -0700 | [diff] [blame] | 6354 | if (tlevel < 5) { |
| 6355 | step_param += 2; |
| 6356 | step_param = AOMMIN(step_param, MAX_MVSEARCH_STEPS - 1); |
| 6357 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6358 | |
| 6359 | // prev_mv_sad is not setup for dynamically scaled frames. |
Debargha Mukherjee | 7166f22 | 2017-09-05 21:32:42 -0700 | [diff] [blame] | 6360 | if (cpi->oxcf.resize_mode != RESIZE_RANDOM) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6361 | int i; |
| 6362 | for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) { |
| 6363 | if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) { |
| 6364 | x->pred_mv[ref].row = 0; |
| 6365 | x->pred_mv[ref].col = 0; |
| 6366 | x->best_mv.as_int = INVALID_MV; |
| 6367 | |
| 6368 | if (scaled_ref_frame) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 6369 | int j; |
| 6370 | for (j = 0; j < MAX_MB_PLANE; ++j) |
| 6371 | xd->plane[j].pre[ref_idx] = backup_yv12[j]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6372 | } |
| 6373 | return; |
| 6374 | } |
| 6375 | } |
| 6376 | } |
| 6377 | } |
| 6378 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6379 | av1_set_mv_search_range(&x->mv_limits, &ref_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6380 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6381 | if (mbmi->motion_mode != SIMPLE_TRANSLATION) |
| 6382 | mvp_full = mbmi->mv[0].as_mv; |
| 6383 | else |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6384 | mvp_full = pred_mv[x->mv_best_ref_index[ref]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6385 | |
| 6386 | mvp_full.col >>= 3; |
| 6387 | mvp_full.row >>= 3; |
| 6388 | |
| 6389 | x->best_mv.as_int = x->second_best_mv.as_int = INVALID_MV; |
| 6390 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6391 | switch (mbmi->motion_mode) { |
| 6392 | case SIMPLE_TRANSLATION: |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 6393 | #if CONFIG_HASH_ME |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6394 | bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, |
| 6395 | sadpb, cond_cost_list(cpi, cost_list), |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 6396 | &ref_mv, INT_MAX, 1, (MI_SIZE * mi_col), |
RogerZhou | d15e7c1 | 2017-09-26 08:49:28 -0700 | [diff] [blame] | 6397 | (MI_SIZE * mi_row), 0); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 6398 | #else |
Sebastien Alaiwan | 1bc94fc | 2017-10-31 10:25:17 +0100 | [diff] [blame] | 6399 | bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, |
| 6400 | sadpb, cond_cost_list(cpi, cost_list), |
| 6401 | &ref_mv, INT_MAX, 1); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 6402 | #endif |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6403 | break; |
| 6404 | case OBMC_CAUSAL: |
| 6405 | bestsme = av1_obmc_full_pixel_diamond( |
| 6406 | cpi, x, &mvp_full, step_param, sadpb, |
| 6407 | MAX_MVSEARCH_STEPS - 1 - step_param, 1, &cpi->fn_ptr[bsize], &ref_mv, |
| 6408 | &(x->best_mv.as_mv), 0); |
| 6409 | break; |
James Zern | 8889673 | 2017-06-23 15:55:09 -0700 | [diff] [blame] | 6410 | default: assert(0 && "Invalid motion mode!\n"); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6411 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6412 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6413 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6414 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6415 | #if CONFIG_AMVR |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 6416 | if (cpi->common.cur_frame_force_integer_mv) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6417 | x->best_mv.as_mv.row *= 8; |
| 6418 | x->best_mv.as_mv.col *= 8; |
| 6419 | } |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 6420 | if (bestsme < INT_MAX && cpi->common.cur_frame_force_integer_mv == 0) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6421 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6422 | if (bestsme < INT_MAX) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6423 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6424 | int dis; /* TODO: use dis in distortion calculation later. */ |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6425 | switch (mbmi->motion_mode) { |
| 6426 | case SIMPLE_TRANSLATION: |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6427 | if (cpi->sf.use_upsampled_references) { |
| 6428 | int best_mv_var; |
| 6429 | const int try_second = x->second_best_mv.as_int != INVALID_MV && |
| 6430 | x->second_best_mv.as_int != x->best_mv.as_int; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 6431 | const int pw = block_size_wide[bsize]; |
| 6432 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6433 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6434 | best_mv_var = cpi->find_fractional_mv_step( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6435 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 6436 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 6437 | cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list), |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6438 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, NULL, |
| 6439 | 0, 0, pw, ph, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6440 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6441 | if (try_second) { |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6442 | const int minc = |
| 6443 | AOMMAX(x->mv_limits.col_min * 8, ref_mv.col - MV_MAX); |
| 6444 | const int maxc = |
| 6445 | AOMMIN(x->mv_limits.col_max * 8, ref_mv.col + MV_MAX); |
| 6446 | const int minr = |
| 6447 | AOMMAX(x->mv_limits.row_min * 8, ref_mv.row - MV_MAX); |
| 6448 | const int maxr = |
| 6449 | AOMMIN(x->mv_limits.row_max * 8, ref_mv.row + MV_MAX); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6450 | int this_var; |
| 6451 | MV best_mv = x->best_mv.as_mv; |
| 6452 | |
| 6453 | x->best_mv = x->second_best_mv; |
| 6454 | if (x->best_mv.as_mv.row * 8 <= maxr && |
| 6455 | x->best_mv.as_mv.row * 8 >= minr && |
| 6456 | x->best_mv.as_mv.col * 8 <= maxc && |
| 6457 | x->best_mv.as_mv.col * 8 >= minc) { |
| 6458 | this_var = cpi->find_fractional_mv_step( |
| 6459 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 6460 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 6461 | cpi->sf.mv.subpel_iters_per_step, |
| 6462 | cond_cost_list(cpi, cost_list), x->nmvjointcost, x->mvcost, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6463 | &dis, &x->pred_sse[ref], NULL, NULL, 0, 0, pw, ph, 1); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6464 | if (this_var < best_mv_var) best_mv = x->best_mv.as_mv; |
| 6465 | x->best_mv.as_mv = best_mv; |
| 6466 | } |
| 6467 | } |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6468 | } else { |
| 6469 | cpi->find_fractional_mv_step( |
| 6470 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 6471 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 6472 | cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list), |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6473 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, NULL, |
| 6474 | 0, 0, 0, 0, 0); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6475 | } |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6476 | break; |
| 6477 | case OBMC_CAUSAL: |
| 6478 | av1_find_best_obmc_sub_pixel_tree_up( |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6479 | x, &x->best_mv.as_mv, &ref_mv, cm->allow_high_precision_mv, |
| 6480 | x->errorperbit, &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 6481 | cpi->sf.mv.subpel_iters_per_step, x->nmvjointcost, x->mvcost, &dis, |
| 6482 | &x->pred_sse[ref], 0, cpi->sf.use_upsampled_references); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6483 | break; |
James Zern | 8889673 | 2017-06-23 15:55:09 -0700 | [diff] [blame] | 6484 | default: assert(0 && "Invalid motion mode!\n"); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6485 | } |
| 6486 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6487 | *rate_mv = av1_mv_bit_cost(&x->best_mv.as_mv, &ref_mv, x->nmvjointcost, |
| 6488 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6489 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6490 | if (cpi->sf.adaptive_motion_search && mbmi->motion_mode == SIMPLE_TRANSLATION) |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 6491 | x->pred_mv[ref] = x->best_mv.as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6492 | |
| 6493 | if (scaled_ref_frame) { |
| 6494 | int i; |
| 6495 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6496 | xd->plane[i].pre[ref_idx] = backup_yv12[i]; |
| 6497 | } |
| 6498 | } |
| 6499 | |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 6500 | static INLINE void restore_dst_buf(MACROBLOCKD *xd, BUFFER_SET dst) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6501 | int i; |
| 6502 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 6503 | xd->plane[i].dst.buf = dst.plane[i]; |
| 6504 | xd->plane[i].dst.stride = dst.stride[i]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6505 | } |
| 6506 | } |
| 6507 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6508 | static void build_second_inter_pred(const AV1_COMP *cpi, MACROBLOCK *x, |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6509 | BLOCK_SIZE bsize, const MV *other_mv, |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6510 | int mi_row, int mi_col, const int block, |
| 6511 | int ref_idx, uint8_t *second_pred) { |
| 6512 | const AV1_COMMON *const cm = &cpi->common; |
| 6513 | const int pw = block_size_wide[bsize]; |
| 6514 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6515 | MACROBLOCKD *xd = &x->e_mbd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6516 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6517 | const int other_ref = mbmi->ref_frame[!ref_idx]; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6518 | struct scale_factors sf; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6519 | struct macroblockd_plane *const pd = &xd->plane[0]; |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 6520 | // ic and ir are the 4x4 coordinates of the sub8x8 at index "block" |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6521 | const int ic = block & 1; |
| 6522 | const int ir = (block - ic) >> 1; |
| 6523 | const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x) + 4 * ic; |
| 6524 | const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y) + 4 * ir; |
Luc Trudeau | f3bf8b1 | 2017-12-08 14:38:41 -0500 | [diff] [blame] | 6525 | const WarpedMotionParams *const wm = &xd->global_motion[other_ref]; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6526 | int is_global = is_global_mv_block(xd->mi[0], block, wm->wmtype); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6527 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6528 | // This function should only ever be called for compound modes |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6529 | assert(has_second_ref(mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6530 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6531 | struct buf_2d backup_yv12[MAX_MB_PLANE]; |
| 6532 | const YV12_BUFFER_CONFIG *const scaled_ref_frame = |
| 6533 | av1_get_scaled_ref_frame(cpi, other_ref); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6534 | |
| 6535 | if (scaled_ref_frame) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6536 | int i; |
| 6537 | // Swap out the reference frame for a version that's been scaled to |
| 6538 | // match the resolution of the current frame, allowing the existing |
| 6539 | // motion search code to be used without additional modifications. |
| 6540 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6541 | backup_yv12[i] = xd->plane[i].pre[!ref_idx]; |
| 6542 | av1_setup_pre_planes(xd, !ref_idx, scaled_ref_frame, mi_row, mi_col, NULL); |
| 6543 | } |
| 6544 | |
| 6545 | // Since we have scaled the reference frames to match the size of the current |
| 6546 | // frame we must use a unit scaling factor during mode selection. |
| 6547 | #if CONFIG_HIGHBITDEPTH |
| 6548 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 6549 | cm->height, cm->use_highbitdepth); |
| 6550 | #else |
| 6551 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 6552 | cm->height); |
| 6553 | #endif // CONFIG_HIGHBITDEPTH |
| 6554 | |
| 6555 | struct buf_2d ref_yv12; |
| 6556 | |
| 6557 | const int plane = 0; |
David Barker | e64d51a | 2017-06-09 14:52:42 +0100 | [diff] [blame] | 6558 | ConvolveParams conv_params = get_conv_params(!ref_idx, 0, plane); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6559 | WarpTypesAllowed warp_types; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6560 | warp_types.global_warp_allowed = is_global; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6561 | warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6562 | |
| 6563 | // Initialized here because of compiler problem in Visual Studio. |
| 6564 | ref_yv12 = xd->plane[plane].pre[!ref_idx]; |
| 6565 | |
| 6566 | // Get the prediction block from the 'other' reference frame. |
| 6567 | #if CONFIG_HIGHBITDEPTH |
| 6568 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 6569 | av1_highbd_build_inter_predictor( |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6570 | ref_yv12.buf, ref_yv12.stride, second_pred, pw, other_mv, &sf, pw, ph, |
Sebastien Alaiwan | 4879580 | 2017-10-30 12:07:13 +0100 | [diff] [blame] | 6571 | 0, mbmi->interp_filters, &warp_types, p_col, p_row, plane, |
| 6572 | MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6573 | } else { |
| 6574 | #endif // CONFIG_HIGHBITDEPTH |
| 6575 | av1_build_inter_predictor( |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6576 | ref_yv12.buf, ref_yv12.stride, second_pred, pw, other_mv, &sf, pw, ph, |
Sebastien Alaiwan | 4879580 | 2017-10-30 12:07:13 +0100 | [diff] [blame] | 6577 | &conv_params, mbmi->interp_filters, &warp_types, p_col, p_row, plane, |
| 6578 | !ref_idx, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6579 | #if CONFIG_HIGHBITDEPTH |
| 6580 | } |
| 6581 | #endif // CONFIG_HIGHBITDEPTH |
| 6582 | |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 6583 | #if CONFIG_JNT_COMP |
Cheng Chen | f78632e | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 6584 | av1_jnt_comp_weight_assign(cm, mbmi, 0, &xd->jcp_param.fwd_offset, |
Cheng Chen | 8263f80 | 2017-11-14 15:50:00 -0800 | [diff] [blame] | 6585 | &xd->jcp_param.bck_offset, |
| 6586 | &xd->jcp_param.use_jnt_comp_avg, 1); |
Cheng Chen | efc55fd | 2017-10-10 12:08:28 -0700 | [diff] [blame] | 6587 | #endif // CONFIG_JNT_COMP |
| 6588 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6589 | if (scaled_ref_frame) { |
| 6590 | // Restore the prediction frame pointers to their unscaled versions. |
| 6591 | int i; |
| 6592 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6593 | xd->plane[i].pre[!ref_idx] = backup_yv12[i]; |
| 6594 | } |
| 6595 | } |
| 6596 | |
| 6597 | // Search for the best mv for one component of a compound, |
| 6598 | // given that the other component is fixed. |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6599 | static void compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x, |
| 6600 | BLOCK_SIZE bsize, MV *this_mv, |
| 6601 | int mi_row, int mi_col, |
| 6602 | const uint8_t *second_pred, |
| 6603 | const uint8_t *mask, int mask_stride, |
| 6604 | int *rate_mv, int ref_idx) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6605 | const int pw = block_size_wide[bsize]; |
| 6606 | const int ph = block_size_high[bsize]; |
| 6607 | MACROBLOCKD *xd = &x->e_mbd; |
| 6608 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6609 | const int ref = mbmi->ref_frame[ref_idx]; |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6610 | int_mv ref_mv = x->mbmi_ext->ref_mvs[ref][0]; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6611 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 6612 | |
| 6613 | struct buf_2d backup_yv12[MAX_MB_PLANE]; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6614 | const YV12_BUFFER_CONFIG *const scaled_ref_frame = |
| 6615 | av1_get_scaled_ref_frame(cpi, ref); |
| 6616 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6617 | // Check that this is either an interinter or an interintra block |
Ryan Lei | 1d1df18 | 2017-06-15 11:38:59 -0700 | [diff] [blame] | 6618 | assert(has_second_ref(mbmi) || |
| 6619 | (ref_idx == 0 && mbmi->ref_frame[1] == INTRA_FRAME)); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6620 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6621 | if (scaled_ref_frame) { |
| 6622 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6623 | // Swap out the reference frame for a version that's been scaled to |
| 6624 | // match the resolution of the current frame, allowing the existing |
| 6625 | // motion search code to be used without additional modifications. |
| 6626 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6627 | backup_yv12[i] = xd->plane[i].pre[ref_idx]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6628 | 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] | 6629 | } |
| 6630 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6631 | struct buf_2d orig_yv12; |
| 6632 | int bestsme = INT_MAX; |
| 6633 | int sadpb = x->sadperbit16; |
| 6634 | MV *const best_mv = &x->best_mv.as_mv; |
| 6635 | int search_range = 3; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6636 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6637 | MvLimits tmp_mv_limits = x->mv_limits; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6638 | |
| 6639 | // Initialized here because of compiler problem in Visual Studio. |
| 6640 | if (ref_idx) { |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6641 | orig_yv12 = pd->pre[0]; |
| 6642 | pd->pre[0] = pd->pre[ref_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6643 | } |
| 6644 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6645 | // Do compound motion search on the current reference frame. |
| 6646 | av1_set_mv_search_range(&x->mv_limits, &ref_mv.as_mv); |
| 6647 | |
| 6648 | // Use the mv result from the single mode as mv predictor. |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6649 | *best_mv = *this_mv; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6650 | |
| 6651 | best_mv->col >>= 3; |
| 6652 | best_mv->row >>= 3; |
| 6653 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6654 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6655 | |
| 6656 | // Small-range full-pixel motion search. |
| 6657 | bestsme = av1_refining_search_8p_c(x, sadpb, search_range, |
| 6658 | &cpi->fn_ptr[bsize], mask, mask_stride, |
| 6659 | ref_idx, &ref_mv.as_mv, second_pred); |
| 6660 | if (bestsme < INT_MAX) { |
| 6661 | if (mask) |
| 6662 | bestsme = |
| 6663 | av1_get_mvpred_mask_var(x, best_mv, &ref_mv.as_mv, second_pred, mask, |
| 6664 | mask_stride, ref_idx, &cpi->fn_ptr[bsize], 1); |
| 6665 | else |
| 6666 | bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv.as_mv, second_pred, |
| 6667 | &cpi->fn_ptr[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6668 | } |
| 6669 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6670 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6671 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6672 | #if CONFIG_AMVR |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 6673 | if (cpi->common.cur_frame_force_integer_mv) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6674 | x->best_mv.as_mv.row *= 8; |
| 6675 | x->best_mv.as_mv.col *= 8; |
| 6676 | } |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 6677 | if (bestsme < INT_MAX && cpi->common.cur_frame_force_integer_mv == 0) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6678 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6679 | if (bestsme < INT_MAX) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6680 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6681 | int dis; /* TODO: use dis in distortion calculation later. */ |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6682 | unsigned int sse; |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6683 | bestsme = cpi->find_fractional_mv_step( |
| 6684 | x, &ref_mv.as_mv, cpi->common.allow_high_precision_mv, x->errorperbit, |
| 6685 | &cpi->fn_ptr[bsize], 0, cpi->sf.mv.subpel_iters_per_step, NULL, |
| 6686 | x->nmvjointcost, x->mvcost, &dis, &sse, second_pred, mask, mask_stride, |
| 6687 | ref_idx, pw, ph, cpi->sf.use_upsampled_references); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | // Restore the pointer to the first (possibly scaled) prediction buffer. |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6691 | if (ref_idx) pd->pre[0] = orig_yv12; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6692 | |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 6693 | if (bestsme < INT_MAX) *this_mv = *best_mv; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6694 | |
| 6695 | *rate_mv = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6696 | |
| 6697 | if (scaled_ref_frame) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6698 | // Restore the prediction frame pointers to their unscaled versions. |
| 6699 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6700 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6701 | xd->plane[i].pre[ref_idx] = backup_yv12[i]; |
| 6702 | } |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6703 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6704 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6705 | *rate_mv += av1_mv_bit_cost(this_mv, &ref_mv.as_mv, x->nmvjointcost, |
| 6706 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6707 | } |
| 6708 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6709 | // Wrapper for compound_single_motion_search, for the common case |
| 6710 | // where the second prediction is also an inter mode. |
| 6711 | static void compound_single_motion_search_interinter( |
| 6712 | const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int_mv *frame_mv, |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6713 | int mi_row, int mi_col, const uint8_t *mask, int mask_stride, int *rate_mv, |
| 6714 | const int block, int ref_idx) { |
| 6715 | MACROBLOCKD *xd = &x->e_mbd; |
| 6716 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6717 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6718 | // This function should only ever be called for compound modes |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6719 | assert(has_second_ref(mbmi)); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6720 | |
| 6721 | // Prediction buffer from second frame. |
| 6722 | #if CONFIG_HIGHBITDEPTH |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6723 | DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]); |
| 6724 | uint8_t *second_pred; |
| 6725 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 6726 | second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16); |
| 6727 | else |
| 6728 | second_pred = (uint8_t *)second_pred_alloc_16; |
| 6729 | #else |
| 6730 | DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]); |
| 6731 | #endif // CONFIG_HIGHBITDEPTH |
| 6732 | |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6733 | MV *this_mv = &frame_mv[mbmi->ref_frame[ref_idx]].as_mv; |
| 6734 | const MV *other_mv = &frame_mv[mbmi->ref_frame[!ref_idx]].as_mv; |
| 6735 | |
| 6736 | build_second_inter_pred(cpi, x, bsize, other_mv, mi_row, mi_col, block, |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6737 | ref_idx, second_pred); |
| 6738 | |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6739 | compound_single_motion_search(cpi, x, bsize, this_mv, mi_row, mi_col, |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6740 | second_pred, mask, mask_stride, rate_mv, |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6741 | ref_idx); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6742 | } |
| 6743 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6744 | static void do_masked_motion_search_indexed( |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6745 | const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv, |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 6746 | const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE bsize, |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 6747 | int mi_row, int mi_col, int_mv *tmp_mv, int *rate_mv, int which) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6748 | // NOTE: which values: 0 - 0 only, 1 - 1 only, 2 - both |
| 6749 | MACROBLOCKD *xd = &x->e_mbd; |
| 6750 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6751 | BLOCK_SIZE sb_type = mbmi->sb_type; |
| 6752 | const uint8_t *mask; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 6753 | const int mask_stride = block_size_wide[bsize]; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 6754 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 6755 | mask = av1_get_compound_type_mask(comp_data, sb_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6756 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6757 | int_mv frame_mv[TOTAL_REFS_PER_FRAME]; |
| 6758 | MV_REFERENCE_FRAME rf[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6759 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6760 | frame_mv[rf[0]].as_int = cur_mv[0].as_int; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6761 | frame_mv[rf[1]].as_int = cur_mv[1].as_int; |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6762 | if (which == 0 || which == 1) { |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6763 | compound_single_motion_search_interinter(cpi, x, bsize, frame_mv, mi_row, |
| 6764 | mi_col, mask, mask_stride, rate_mv, |
| 6765 | 0, which); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 6766 | } else if (which == 2) { |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6767 | joint_motion_search(cpi, x, bsize, frame_mv, mi_row, mi_col, NULL, mask, |
| 6768 | mask_stride, rate_mv, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6769 | } |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 6770 | tmp_mv[0].as_int = frame_mv[rf[0]].as_int; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 6771 | tmp_mv[1].as_int = frame_mv[rf[1]].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6772 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6773 | |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 6774 | // In some situations we want to discount the apparent cost of a new motion |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6775 | // vector. Where there is a subtle motion field and especially where there is |
| 6776 | // low spatial complexity then it can be hard to cover the cost of a new motion |
| 6777 | // vector in a single block, even if that motion vector reduces distortion. |
| 6778 | // However, once established that vector may be usable through the nearest and |
| 6779 | // near mv modes to reduce distortion in subsequent blocks and also improve |
| 6780 | // visual quality. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6781 | 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] | 6782 | int_mv this_mv, |
| 6783 | int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], |
| 6784 | int ref_frame) { |
| 6785 | return (!cpi->rc.is_src_frame_alt_ref && (this_mode == NEWMV) && |
| 6786 | (this_mv.as_int != 0) && |
| 6787 | ((mode_mv[NEARESTMV][ref_frame].as_int == 0) || |
| 6788 | (mode_mv[NEARESTMV][ref_frame].as_int == INVALID_MV)) && |
| 6789 | ((mode_mv[NEARMV][ref_frame].as_int == 0) || |
| 6790 | (mode_mv[NEARMV][ref_frame].as_int == INVALID_MV))); |
| 6791 | } |
| 6792 | |
Yaowu Xu | 671f2bd | 2016-09-30 15:07:57 -0700 | [diff] [blame] | 6793 | #define LEFT_TOP_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3) |
| 6794 | #define RIGHT_BOTTOM_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6795 | |
| 6796 | // TODO(jingning): this mv clamping function should be block size dependent. |
| 6797 | static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) { |
| 6798 | clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN, |
| 6799 | xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN, |
| 6800 | xd->mb_to_top_edge - LEFT_TOP_MARGIN, |
| 6801 | xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN); |
| 6802 | } |
| 6803 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6804 | static int estimate_wedge_sign(const AV1_COMP *cpi, const MACROBLOCK *x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6805 | const BLOCK_SIZE bsize, const uint8_t *pred0, |
| 6806 | int stride0, const uint8_t *pred1, int stride1) { |
| 6807 | const struct macroblock_plane *const p = &x->plane[0]; |
| 6808 | const uint8_t *src = p->src.buf; |
| 6809 | int src_stride = p->src.stride; |
| 6810 | const int f_index = bsize - BLOCK_8X8; |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6811 | const int bw = block_size_wide[bsize]; |
| 6812 | const int bh = block_size_high[bsize]; |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 6813 | uint32_t esq[2][4]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6814 | int64_t tl, br; |
| 6815 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6816 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6817 | if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 6818 | pred0 = CONVERT_TO_BYTEPTR(pred0); |
| 6819 | pred1 = CONVERT_TO_BYTEPTR(pred1); |
| 6820 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6821 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6822 | |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 6823 | cpi->fn_ptr[f_index].vf(src, src_stride, pred0, stride0, &esq[0][0]); |
| 6824 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred0 + bw / 2, stride0, |
| 6825 | &esq[0][1]); |
| 6826 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride, |
| 6827 | pred0 + bh / 2 * stride0, stride0, &esq[0][2]); |
| 6828 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride, |
| 6829 | pred0 + bh / 2 * stride0 + bw / 2, stride0, |
| 6830 | &esq[0][3]); |
| 6831 | cpi->fn_ptr[f_index].vf(src, src_stride, pred1, stride1, &esq[1][0]); |
| 6832 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred1 + bw / 2, stride1, |
| 6833 | &esq[1][1]); |
| 6834 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride, |
| 6835 | pred1 + bh / 2 * stride1, stride0, &esq[1][2]); |
| 6836 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride, |
| 6837 | pred1 + bh / 2 * stride1 + bw / 2, stride0, |
| 6838 | &esq[1][3]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6839 | |
| 6840 | tl = (int64_t)(esq[0][0] + esq[0][1] + esq[0][2]) - |
| 6841 | (int64_t)(esq[1][0] + esq[1][1] + esq[1][2]); |
| 6842 | br = (int64_t)(esq[1][3] + esq[1][1] + esq[1][2]) - |
| 6843 | (int64_t)(esq[0][3] + esq[0][1] + esq[0][2]); |
| 6844 | return (tl + br > 0); |
| 6845 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6846 | |
| 6847 | #if !CONFIG_DUAL_FILTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 6848 | static InterpFilter predict_interp_filter( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6849 | const AV1_COMP *cpi, const MACROBLOCK *x, const BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6850 | const int mi_row, const int mi_col, |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 6851 | InterpFilter (*single_filter)[TOTAL_REFS_PER_FRAME]) { |
| 6852 | InterpFilter best_filter = SWITCHABLE; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6853 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6854 | const MACROBLOCKD *xd = &x->e_mbd; |
| 6855 | int bsl = mi_width_log2_lookup[bsize]; |
| 6856 | int pred_filter_search = |
| 6857 | cpi->sf.cb_pred_filter_search |
| 6858 | ? (((mi_row + mi_col) >> bsl) + |
| 6859 | get_chessboard_index(cm->current_video_frame)) & |
| 6860 | 0x1 |
| 6861 | : 0; |
| 6862 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6863 | const int is_comp_pred = has_second_ref(mbmi); |
| 6864 | const int this_mode = mbmi->mode; |
| 6865 | int refs[2] = { mbmi->ref_frame[0], |
| 6866 | (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6867 | if (pred_filter_search) { |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 6868 | InterpFilter af = SWITCHABLE, lf = SWITCHABLE; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 6869 | if (xd->up_available) |
| 6870 | af = av1_extract_interp_filter( |
| 6871 | xd->mi[-xd->mi_stride]->mbmi.interp_filters, 0); |
| 6872 | if (xd->left_available) |
| 6873 | lf = av1_extract_interp_filter(xd->mi[-1]->mbmi.interp_filters, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6874 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 6875 | if ((this_mode != NEWMV && this_mode != NEW_NEWMV) || (af == lf)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6876 | best_filter = af; |
| 6877 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6878 | if (is_comp_pred) { |
| 6879 | if (cpi->sf.adaptive_mode_search) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6880 | switch (this_mode) { |
| 6881 | case NEAREST_NEARESTMV: |
| 6882 | if (single_filter[NEARESTMV][refs[0]] == |
| 6883 | single_filter[NEARESTMV][refs[1]]) |
| 6884 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 6885 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6886 | case NEAR_NEARMV: |
| 6887 | if (single_filter[NEARMV][refs[0]] == single_filter[NEARMV][refs[1]]) |
| 6888 | best_filter = single_filter[NEARMV][refs[0]]; |
| 6889 | break; |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 6890 | case GLOBAL_GLOBALMV: |
| 6891 | if (single_filter[GLOBALMV][refs[0]] == |
| 6892 | single_filter[GLOBALMV][refs[1]]) |
| 6893 | best_filter = single_filter[GLOBALMV][refs[0]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6894 | break; |
| 6895 | case NEW_NEWMV: |
| 6896 | if (single_filter[NEWMV][refs[0]] == single_filter[NEWMV][refs[1]]) |
| 6897 | best_filter = single_filter[NEWMV][refs[0]]; |
| 6898 | break; |
| 6899 | case NEAREST_NEWMV: |
| 6900 | if (single_filter[NEARESTMV][refs[0]] == |
| 6901 | single_filter[NEWMV][refs[1]]) |
| 6902 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 6903 | break; |
| 6904 | case NEAR_NEWMV: |
| 6905 | if (single_filter[NEARMV][refs[0]] == single_filter[NEWMV][refs[1]]) |
| 6906 | best_filter = single_filter[NEARMV][refs[0]]; |
| 6907 | break; |
| 6908 | case NEW_NEARESTMV: |
| 6909 | if (single_filter[NEWMV][refs[0]] == |
| 6910 | single_filter[NEARESTMV][refs[1]]) |
| 6911 | best_filter = single_filter[NEWMV][refs[0]]; |
| 6912 | break; |
| 6913 | case NEW_NEARMV: |
| 6914 | if (single_filter[NEWMV][refs[0]] == single_filter[NEARMV][refs[1]]) |
| 6915 | best_filter = single_filter[NEWMV][refs[0]]; |
| 6916 | break; |
| 6917 | default: |
| 6918 | if (single_filter[this_mode][refs[0]] == |
| 6919 | single_filter[this_mode][refs[1]]) |
| 6920 | best_filter = single_filter[this_mode][refs[0]]; |
| 6921 | break; |
| 6922 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6923 | } |
| 6924 | } |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 6925 | if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) { |
| 6926 | best_filter = EIGHTTAP_REGULAR; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6927 | } |
| 6928 | return best_filter; |
| 6929 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6930 | #endif // !CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6931 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6932 | // Choose the best wedge index and sign |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6933 | 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] | 6934 | const BLOCK_SIZE bsize, const uint8_t *const p0, |
| 6935 | const uint8_t *const p1, int *const best_wedge_sign, |
| 6936 | int *const best_wedge_index) { |
| 6937 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 6938 | const struct buf_2d *const src = &x->plane[0].src; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 6939 | const int bw = block_size_wide[bsize]; |
| 6940 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6941 | const int N = bw * bh; |
| 6942 | int rate; |
| 6943 | int64_t dist; |
| 6944 | int64_t rd, best_rd = INT64_MAX; |
| 6945 | int wedge_index; |
| 6946 | int wedge_sign; |
| 6947 | int wedge_types = (1 << get_wedge_bits_lookup(bsize)); |
| 6948 | const uint8_t *mask; |
| 6949 | uint64_t sse; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6950 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6951 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 6952 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 6953 | #else |
| 6954 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6955 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6956 | |
| 6957 | DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]); |
| 6958 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 6959 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 6960 | DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]); |
| 6961 | |
| 6962 | int64_t sign_limit; |
| 6963 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6964 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6965 | if (hbd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6966 | aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6967 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6968 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6969 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6970 | 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] | 6971 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 6972 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6973 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6974 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6975 | aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw); |
| 6976 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 6977 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6978 | } |
| 6979 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6980 | sign_limit = ((int64_t)aom_sum_squares_i16(r0, N) - |
| 6981 | (int64_t)aom_sum_squares_i16(r1, N)) * |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6982 | (1 << WEDGE_WEIGHT_BITS) / 2; |
| 6983 | |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6984 | if (N < 64) |
| 6985 | av1_wedge_compute_delta_squares_c(ds, r0, r1, N); |
| 6986 | else |
| 6987 | av1_wedge_compute_delta_squares(ds, r0, r1, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6988 | |
| 6989 | for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6990 | mask = av1_get_contiguous_soft_mask(wedge_index, 0, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6991 | |
| 6992 | // TODO(jingning): Make sse2 functions support N = 16 case |
| 6993 | if (N < 64) |
| 6994 | wedge_sign = av1_wedge_sign_from_residuals_c(ds, mask, N, sign_limit); |
| 6995 | else |
| 6996 | wedge_sign = av1_wedge_sign_from_residuals(ds, mask, N, sign_limit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6997 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6998 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6999 | if (N < 64) |
| 7000 | sse = av1_wedge_sse_from_residuals_c(r1, d10, mask, N); |
| 7001 | else |
| 7002 | sse = av1_wedge_sse_from_residuals(r1, d10, mask, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7003 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
| 7004 | |
| 7005 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7006 | rd = RDCOST(x->rdmult, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7007 | |
| 7008 | if (rd < best_rd) { |
| 7009 | *best_wedge_index = wedge_index; |
| 7010 | *best_wedge_sign = wedge_sign; |
| 7011 | best_rd = rd; |
| 7012 | } |
| 7013 | } |
| 7014 | |
| 7015 | return best_rd; |
| 7016 | } |
| 7017 | |
| 7018 | // Choose the best wedge index the specified sign |
| 7019 | static int64_t pick_wedge_fixed_sign( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7020 | const AV1_COMP *const cpi, const MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7021 | const BLOCK_SIZE bsize, const uint8_t *const p0, const uint8_t *const p1, |
| 7022 | const int wedge_sign, int *const best_wedge_index) { |
| 7023 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 7024 | const struct buf_2d *const src = &x->plane[0].src; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7025 | const int bw = block_size_wide[bsize]; |
| 7026 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7027 | const int N = bw * bh; |
| 7028 | int rate; |
| 7029 | int64_t dist; |
| 7030 | int64_t rd, best_rd = INT64_MAX; |
| 7031 | int wedge_index; |
| 7032 | int wedge_types = (1 << get_wedge_bits_lookup(bsize)); |
| 7033 | const uint8_t *mask; |
| 7034 | uint64_t sse; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7035 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7036 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 7037 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 7038 | #else |
| 7039 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7040 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7041 | |
| 7042 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 7043 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 7044 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7045 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7046 | if (hbd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7047 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7048 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7049 | 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] | 7050 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7051 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7052 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7053 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7054 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 7055 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7059 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7060 | if (N < 64) |
| 7061 | sse = av1_wedge_sse_from_residuals_c(r1, d10, mask, N); |
| 7062 | else |
| 7063 | sse = av1_wedge_sse_from_residuals(r1, d10, mask, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7064 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
| 7065 | |
| 7066 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7067 | rd = RDCOST(x->rdmult, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7068 | |
| 7069 | if (rd < best_rd) { |
| 7070 | *best_wedge_index = wedge_index; |
| 7071 | best_rd = rd; |
| 7072 | } |
| 7073 | } |
| 7074 | |
| 7075 | return best_rd; |
| 7076 | } |
| 7077 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7078 | static int64_t pick_interinter_wedge(const AV1_COMP *const cpi, |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7079 | MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7080 | const BLOCK_SIZE bsize, |
| 7081 | const uint8_t *const p0, |
| 7082 | const uint8_t *const p1) { |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7083 | MACROBLOCKD *const xd = &x->e_mbd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7084 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7085 | const int bw = block_size_wide[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7086 | |
| 7087 | int64_t rd; |
| 7088 | int wedge_index = -1; |
| 7089 | int wedge_sign = 0; |
| 7090 | |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 7091 | assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize)); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 7092 | assert(cpi->common.allow_masked_compound); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7093 | |
| 7094 | if (cpi->sf.fast_wedge_sign_estimate) { |
| 7095 | wedge_sign = estimate_wedge_sign(cpi, x, bsize, p0, bw, p1, bw); |
| 7096 | rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, wedge_sign, &wedge_index); |
| 7097 | } else { |
| 7098 | rd = pick_wedge(cpi, x, bsize, p0, p1, &wedge_sign, &wedge_index); |
| 7099 | } |
| 7100 | |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7101 | mbmi->wedge_sign = wedge_sign; |
| 7102 | mbmi->wedge_index = wedge_index; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7103 | return rd; |
| 7104 | } |
| 7105 | |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7106 | static int64_t pick_interinter_seg(const AV1_COMP *const cpi, |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7107 | MACROBLOCK *const x, const BLOCK_SIZE bsize, |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7108 | const uint8_t *const p0, |
| 7109 | const uint8_t *const p1) { |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7110 | MACROBLOCKD *const xd = &x->e_mbd; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7111 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7112 | const struct buf_2d *const src = &x->plane[0].src; |
| 7113 | const int bw = block_size_wide[bsize]; |
| 7114 | const int bh = block_size_high[bsize]; |
| 7115 | const int N = bw * bh; |
| 7116 | int rate; |
| 7117 | uint64_t sse; |
| 7118 | int64_t dist; |
Debargha Mukherjee | c30934b | 2017-04-25 01:23:51 -0700 | [diff] [blame] | 7119 | int64_t rd0; |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7120 | SEG_MASK_TYPE cur_mask_type; |
| 7121 | int64_t best_rd = INT64_MAX; |
| 7122 | SEG_MASK_TYPE best_mask_type = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7123 | #if CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7124 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 7125 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 7126 | #else |
| 7127 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7128 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7129 | DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]); |
| 7130 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 7131 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 7132 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7133 | #if CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7134 | if (hbd) { |
| 7135 | aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride, |
| 7136 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7137 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
| 7138 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
| 7139 | aom_highbd_subtract_block(bh, bw, d10, bw, CONVERT_TO_BYTEPTR(p1), bw, |
| 7140 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7141 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7142 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7143 | { |
| 7144 | aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw); |
| 7145 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 7146 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
| 7147 | } |
| 7148 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7149 | // try each mask type and its inverse |
| 7150 | for (cur_mask_type = 0; cur_mask_type < SEG_MASK_TYPES; cur_mask_type++) { |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7151 | // build mask and inverse |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7152 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7153 | if (hbd) |
| 7154 | build_compound_seg_mask_highbd( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7155 | xd->seg_mask, cur_mask_type, CONVERT_TO_BYTEPTR(p0), bw, |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7156 | CONVERT_TO_BYTEPTR(p1), bw, bsize, bh, bw, xd->bd); |
| 7157 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7158 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7159 | build_compound_seg_mask(xd->seg_mask, cur_mask_type, p0, bw, p1, bw, |
| 7160 | bsize, bh, bw); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7161 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7162 | // compute rd for mask |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7163 | sse = av1_wedge_sse_from_residuals(r1, d10, xd->seg_mask, N); |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7164 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7165 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7166 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7167 | rd0 = RDCOST(x->rdmult, rate, dist); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7168 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7169 | if (rd0 < best_rd) { |
| 7170 | best_mask_type = cur_mask_type; |
| 7171 | best_rd = rd0; |
| 7172 | } |
| 7173 | } |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7174 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7175 | // make final mask |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7176 | mbmi->mask_type = best_mask_type; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7177 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7178 | if (hbd) |
| 7179 | build_compound_seg_mask_highbd( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7180 | xd->seg_mask, mbmi->mask_type, CONVERT_TO_BYTEPTR(p0), bw, |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7181 | CONVERT_TO_BYTEPTR(p1), bw, bsize, bh, bw, xd->bd); |
| 7182 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7183 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7184 | build_compound_seg_mask(xd->seg_mask, mbmi->mask_type, p0, bw, p1, bw, |
| 7185 | bsize, bh, bw); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7186 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7187 | return best_rd; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7188 | } |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7189 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7190 | static int64_t pick_interintra_wedge(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7191 | const MACROBLOCK *const x, |
| 7192 | const BLOCK_SIZE bsize, |
| 7193 | const uint8_t *const p0, |
| 7194 | const uint8_t *const p1) { |
| 7195 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 7196 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7197 | |
| 7198 | int64_t rd; |
| 7199 | int wedge_index = -1; |
| 7200 | |
| 7201 | assert(is_interintra_wedge_used(bsize)); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 7202 | assert(cpi->common.allow_interintra_compound); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7203 | |
| 7204 | rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, 0, &wedge_index); |
| 7205 | |
| 7206 | mbmi->interintra_wedge_sign = 0; |
| 7207 | mbmi->interintra_wedge_index = wedge_index; |
| 7208 | return rd; |
| 7209 | } |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7210 | |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7211 | static int64_t pick_interinter_mask(const AV1_COMP *const cpi, MACROBLOCK *x, |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7212 | const BLOCK_SIZE bsize, |
| 7213 | const uint8_t *const p0, |
| 7214 | const uint8_t *const p1) { |
| 7215 | const COMPOUND_TYPE compound_type = |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7216 | x->e_mbd.mi[0]->mbmi.interinter_compound_type; |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7217 | switch (compound_type) { |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7218 | case COMPOUND_WEDGE: return pick_interinter_wedge(cpi, x, bsize, p0, p1); |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7219 | case COMPOUND_SEG: return pick_interinter_seg(cpi, x, bsize, p0, p1); |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7220 | default: assert(0); return 0; |
| 7221 | } |
| 7222 | } |
| 7223 | |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7224 | static int interinter_compound_motion_search( |
| 7225 | const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv, |
| 7226 | const BLOCK_SIZE bsize, const int this_mode, int mi_row, int mi_col) { |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7227 | MACROBLOCKD *const xd = &x->e_mbd; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7228 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7229 | int_mv tmp_mv[2]; |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7230 | int tmp_rate_mv = 0; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7231 | const INTERINTER_COMPOUND_DATA compound_data = { |
Debargha Mukherjee | 371968c | 2017-10-29 12:30:04 -0700 | [diff] [blame] | 7232 | mbmi->wedge_index, mbmi->wedge_sign, mbmi->mask_type, xd->seg_mask, |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7233 | mbmi->interinter_compound_type |
| 7234 | }; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7235 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 7236 | if (this_mode == NEW_NEWMV) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7237 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 7238 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 2); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7239 | mbmi->mv[0].as_int = tmp_mv[0].as_int; |
| 7240 | mbmi->mv[1].as_int = tmp_mv[1].as_int; |
| 7241 | } else if (this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7242 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 7243 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 0); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7244 | mbmi->mv[0].as_int = tmp_mv[0].as_int; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 7245 | } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7246 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 7247 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 1); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7248 | mbmi->mv[1].as_int = tmp_mv[1].as_int; |
| 7249 | } |
| 7250 | return tmp_rate_mv; |
| 7251 | } |
| 7252 | |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7253 | static int64_t build_and_cost_compound_type( |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7254 | const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv, |
| 7255 | const BLOCK_SIZE bsize, const int this_mode, int rs2, int rate_mv, |
| 7256 | BUFFER_SET *ctx, int *out_rate_mv, uint8_t **preds0, uint8_t **preds1, |
| 7257 | int *strides, int mi_row, int mi_col) { |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 7258 | const AV1_COMMON *const cm = &cpi->common; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7259 | MACROBLOCKD *xd = &x->e_mbd; |
| 7260 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7261 | int rate_sum; |
| 7262 | int64_t dist_sum; |
| 7263 | int64_t best_rd_cur = INT64_MAX; |
| 7264 | int64_t rd = INT64_MAX; |
| 7265 | int tmp_skip_txfm_sb; |
| 7266 | int64_t tmp_skip_sse_sb; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7267 | const COMPOUND_TYPE compound_type = mbmi->interinter_compound_type; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7268 | |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7269 | best_rd_cur = pick_interinter_mask(cpi, x, bsize, *preds0, *preds1); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7270 | best_rd_cur += RDCOST(x->rdmult, rs2 + rate_mv, 0); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7271 | |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 7272 | if (have_newmv_in_inter_mode(this_mode) && |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7273 | use_masked_motion_search(compound_type)) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7274 | *out_rate_mv = interinter_compound_motion_search(cpi, x, cur_mv, bsize, |
| 7275 | this_mode, mi_row, mi_col); |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 7276 | av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, ctx, bsize); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7277 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 7278 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7279 | rd = RDCOST(x->rdmult, rs2 + *out_rate_mv + rate_sum, dist_sum); |
Zoe Liu | 4d44f5a | 2016-12-14 17:46:19 -0800 | [diff] [blame] | 7280 | if (rd >= best_rd_cur) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7281 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 7282 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 7283 | *out_rate_mv = rate_mv; |
Sebastien Alaiwan | 0cf54d4 | 2017-10-16 16:10:04 +0200 | [diff] [blame] | 7284 | av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0, strides, |
| 7285 | preds1, strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7286 | } |
| 7287 | av1_subtract_plane(x, bsize, 0); |
| 7288 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 7289 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 7290 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7291 | rd = RDCOST(x->rdmult, rs2 + *out_rate_mv + rate_sum, dist_sum); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7292 | best_rd_cur = rd; |
| 7293 | |
| 7294 | } else { |
Sebastien Alaiwan | 0cf54d4 | 2017-10-16 16:10:04 +0200 | [diff] [blame] | 7295 | av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0, strides, |
| 7296 | preds1, strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7297 | av1_subtract_plane(x, bsize, 0); |
| 7298 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 7299 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 7300 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7301 | rd = RDCOST(x->rdmult, rs2 + rate_mv + rate_sum, dist_sum); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7302 | best_rd_cur = rd; |
| 7303 | } |
| 7304 | return best_rd_cur; |
| 7305 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7306 | |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 7307 | typedef struct { |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 7308 | // Inter prediction buffers and respective strides |
| 7309 | uint8_t *above_pred_buf[MAX_MB_PLANE]; |
| 7310 | int above_pred_stride[MAX_MB_PLANE]; |
| 7311 | uint8_t *left_pred_buf[MAX_MB_PLANE]; |
| 7312 | int left_pred_stride[MAX_MB_PLANE]; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7313 | int_mv *single_newmv; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 7314 | // Pointer to array of motion vectors to use for each ref and their rates |
| 7315 | // Should point to first of 2 arrays in 2D array |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7316 | int *single_newmv_rate; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 7317 | // Pointer to array of predicted rate-distortion |
| 7318 | // Should point to first of 2 arrays in 2D array |
| 7319 | int64_t (*modelled_rd)[TOTAL_REFS_PER_FRAME]; |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 7320 | InterpFilter single_filter[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 7321 | } HandleInterModeArgs; |
| 7322 | |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7323 | static int64_t handle_newmv(const AV1_COMP *const cpi, MACROBLOCK *const x, |
| 7324 | const BLOCK_SIZE bsize, |
| 7325 | int_mv (*const mode_mv)[TOTAL_REFS_PER_FRAME], |
| 7326 | const int mi_row, const int mi_col, |
| 7327 | int *const rate_mv, int_mv *const single_newmv, |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 7328 | HandleInterModeArgs *const args) { |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7329 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 7330 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7331 | const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 7332 | const int is_comp_pred = has_second_ref(mbmi); |
| 7333 | const PREDICTION_MODE this_mode = mbmi->mode; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7334 | const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7335 | int_mv *const frame_mv = mode_mv[this_mode]; |
| 7336 | const int refs[2] = { mbmi->ref_frame[0], |
| 7337 | mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1] }; |
| 7338 | int i; |
| 7339 | |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 7340 | (void)args; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7341 | |
| 7342 | if (is_comp_pred) { |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7343 | for (i = 0; i < 2; ++i) { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7344 | single_newmv[refs[i]].as_int = args->single_newmv[refs[i]].as_int; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7345 | } |
| 7346 | |
| 7347 | if (this_mode == NEW_NEWMV) { |
| 7348 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
| 7349 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
| 7350 | |
| 7351 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 7352 | joint_motion_search(cpi, x, bsize, frame_mv, mi_row, mi_col, NULL, NULL, |
| 7353 | 0, rate_mv, 0); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7354 | } else { |
| 7355 | *rate_mv = 0; |
| 7356 | for (i = 0; i < 2; ++i) { |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 7357 | av1_set_mvcost(x, refs[i], i, mbmi->ref_mv_idx); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7358 | *rate_mv += av1_mv_bit_cost( |
| 7359 | &frame_mv[refs[i]].as_mv, &mbmi_ext->ref_mvs[refs[i]][0].as_mv, |
| 7360 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 7361 | } |
| 7362 | } |
| 7363 | } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV) { |
| 7364 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7365 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 7366 | frame_mv[refs[0]].as_int = |
| 7367 | mode_mv[compound_ref0_mode(this_mode)][refs[0]].as_int; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 7368 | compound_single_motion_search_interinter( |
| 7369 | cpi, x, bsize, frame_mv, mi_row, mi_col, NULL, 0, rate_mv, 0, 1); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7370 | } else { |
| 7371 | av1_set_mvcost(x, refs[1], 1, mbmi->ref_mv_idx); |
| 7372 | *rate_mv = av1_mv_bit_cost(&frame_mv[refs[1]].as_mv, |
| 7373 | &mbmi_ext->ref_mvs[refs[1]][0].as_mv, |
| 7374 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 7375 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7376 | } else { |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 7377 | assert(this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7378 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7379 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 7380 | frame_mv[refs[1]].as_int = |
| 7381 | mode_mv[compound_ref1_mode(this_mode)][refs[1]].as_int; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 7382 | compound_single_motion_search_interinter( |
| 7383 | cpi, x, bsize, frame_mv, mi_row, mi_col, NULL, 0, rate_mv, 0, 0); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7384 | } else { |
| 7385 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
| 7386 | *rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
| 7387 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 7388 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 7389 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7390 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7391 | } else { |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7392 | if (is_comp_interintra_pred) { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7393 | x->best_mv = args->single_newmv[refs[0]]; |
| 7394 | *rate_mv = args->single_newmv_rate[refs[0]]; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7395 | } else { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7396 | single_motion_search(cpi, x, bsize, mi_row, mi_col, 0, rate_mv); |
| 7397 | args->single_newmv[refs[0]] = x->best_mv; |
| 7398 | args->single_newmv_rate[refs[0]] = *rate_mv; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7399 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 7400 | |
| 7401 | if (x->best_mv.as_int == INVALID_MV) return INT64_MAX; |
| 7402 | |
| 7403 | frame_mv[refs[0]] = x->best_mv; |
| 7404 | xd->mi[0]->bmi[0].as_mv[0] = x->best_mv; |
| 7405 | |
| 7406 | // Estimate the rate implications of a new mv but discount this |
| 7407 | // under certain circumstances where we want to help initiate a weak |
| 7408 | // motion field, where the distortion gain for a single block may not |
| 7409 | // be enough to overcome the cost of a new mv. |
| 7410 | if (discount_newmv_test(cpi, this_mode, x->best_mv, mode_mv, refs[0])) { |
| 7411 | *rate_mv = AOMMAX(*rate_mv / NEW_MV_DISCOUNT_FACTOR, 1); |
| 7412 | } |
| 7413 | } |
| 7414 | |
| 7415 | return 0; |
| 7416 | } |
| 7417 | |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7418 | int64_t interpolation_filter_search( |
| 7419 | MACROBLOCK *const x, const AV1_COMP *const cpi, BLOCK_SIZE bsize, |
| 7420 | int mi_row, int mi_col, const BUFFER_SET *const tmp_dst, |
| 7421 | BUFFER_SET *const orig_dst, |
| 7422 | InterpFilter (*const single_filter)[TOTAL_REFS_PER_FRAME], |
| 7423 | int64_t *const rd, int *const switchable_rate, int *const skip_txfm_sb, |
| 7424 | int64_t *const skip_sse_sb) { |
| 7425 | const AV1_COMMON *cm = &cpi->common; |
| 7426 | MACROBLOCKD *const xd = &x->e_mbd; |
| 7427 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7428 | int i; |
| 7429 | int tmp_rate; |
| 7430 | int64_t tmp_dist; |
| 7431 | |
| 7432 | (void)single_filter; |
| 7433 | |
| 7434 | InterpFilter assign_filter = SWITCHABLE; |
| 7435 | |
| 7436 | if (cm->interp_filter == SWITCHABLE) { |
| 7437 | #if !CONFIG_DUAL_FILTER |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 7438 | assign_filter = av1_is_interp_needed(xd) |
| 7439 | ? predict_interp_filter(cpi, x, bsize, mi_row, mi_col, |
| 7440 | single_filter) |
| 7441 | : cm->interp_filter; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7442 | #endif // !CONFIG_DUAL_FILTER |
| 7443 | } else { |
| 7444 | assign_filter = cm->interp_filter; |
| 7445 | } |
| 7446 | |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 7447 | set_default_interp_filters(mbmi, assign_filter); |
| 7448 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 7449 | *switchable_rate = av1_get_switchable_rate(cm, x, xd); |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 7450 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, bsize); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7451 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, &tmp_dist, |
| 7452 | skip_txfm_sb, skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7453 | *rd = RDCOST(x->rdmult, *switchable_rate + tmp_rate, tmp_dist); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7454 | |
| 7455 | if (assign_filter == SWITCHABLE) { |
| 7456 | // do interp_filter search |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 7457 | if (av1_is_interp_needed(xd) && av1_is_interp_search_needed(xd)) { |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7458 | #if CONFIG_DUAL_FILTER |
| 7459 | const int filter_set_size = DUAL_FILTER_SET_SIZE; |
| 7460 | #else |
| 7461 | const int filter_set_size = SWITCHABLE_FILTERS; |
| 7462 | #endif // CONFIG_DUAL_FILTER |
| 7463 | int best_in_temp = 0; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7464 | InterpFilters best_filters = mbmi->interp_filters; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7465 | restore_dst_buf(xd, *tmp_dst); |
Michelle Findlay-Olynyk | a3eb912 | 2017-11-27 10:46:45 -0800 | [diff] [blame] | 7466 | |
| 7467 | #if CONFIG_DUAL_FILTER // Speed feature use_fast_interpolation_filter_search |
| 7468 | if (cpi->sf.use_fast_interpolation_filter_search) { |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7469 | int tmp_skip_sb = 0; |
| 7470 | int64_t tmp_skip_sse = INT64_MAX; |
| 7471 | int tmp_rs; |
| 7472 | int64_t tmp_rd; |
Michelle Findlay-Olynyk | a3eb912 | 2017-11-27 10:46:45 -0800 | [diff] [blame] | 7473 | |
| 7474 | // default to (R,R): EIGHTTAP_REGULARxEIGHTTAP_REGULAR |
| 7475 | int best_dual_mode = 0; |
| 7476 | // Find best of {R}x{R,Sm,Sh} |
| 7477 | // EIGHTTAP_REGULAR mode is calculated beforehand |
| 7478 | for (i = 1; i < SWITCHABLE_FILTERS; ++i) { |
| 7479 | tmp_skip_sb = 0; |
| 7480 | tmp_skip_sse = INT64_MAX; |
| 7481 | |
| 7482 | mbmi->interp_filters = |
| 7483 | av1_make_interp_filters(filter_sets[i][0], filter_sets[i][1]); |
| 7484 | |
| 7485 | tmp_rs = av1_get_switchable_rate(cm, x, xd); |
| 7486 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, |
| 7487 | bsize); |
| 7488 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7489 | &tmp_dist, &tmp_skip_sb, &tmp_skip_sse); |
| 7490 | tmp_rd = RDCOST(x->rdmult, tmp_rs + tmp_rate, tmp_dist); |
| 7491 | |
| 7492 | if (tmp_rd < *rd) { |
| 7493 | best_dual_mode = i; |
| 7494 | |
| 7495 | *rd = tmp_rd; |
| 7496 | *switchable_rate = av1_get_switchable_rate(cm, x, xd); |
| 7497 | best_filters = mbmi->interp_filters; |
| 7498 | *skip_txfm_sb = tmp_skip_sb; |
| 7499 | *skip_sse_sb = tmp_skip_sse; |
| 7500 | best_in_temp = !best_in_temp; |
| 7501 | if (best_in_temp) { |
| 7502 | restore_dst_buf(xd, *orig_dst); |
| 7503 | } else { |
| 7504 | restore_dst_buf(xd, *tmp_dst); |
| 7505 | } |
| 7506 | } |
| 7507 | } |
| 7508 | |
| 7509 | // From best of horizontal EIGHTTAP_REGULAR modes, check vertical modes |
| 7510 | for (i = best_dual_mode + SWITCHABLE_FILTERS; i < filter_set_size; |
| 7511 | i += SWITCHABLE_FILTERS) { |
| 7512 | tmp_skip_sb = 0; |
| 7513 | tmp_skip_sse = INT64_MAX; |
| 7514 | |
| 7515 | mbmi->interp_filters = |
| 7516 | av1_make_interp_filters(filter_sets[i][0], filter_sets[i][1]); |
| 7517 | |
| 7518 | tmp_rs = av1_get_switchable_rate(cm, x, xd); |
| 7519 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, |
| 7520 | bsize); |
| 7521 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7522 | &tmp_dist, &tmp_skip_sb, &tmp_skip_sse); |
| 7523 | tmp_rd = RDCOST(x->rdmult, tmp_rs + tmp_rate, tmp_dist); |
| 7524 | |
| 7525 | if (tmp_rd < *rd) { |
| 7526 | *rd = tmp_rd; |
| 7527 | *switchable_rate = av1_get_switchable_rate(cm, x, xd); |
| 7528 | best_filters = mbmi->interp_filters; |
| 7529 | *skip_txfm_sb = tmp_skip_sb; |
| 7530 | *skip_sse_sb = tmp_skip_sse; |
| 7531 | best_in_temp = !best_in_temp; |
| 7532 | if (best_in_temp) { |
| 7533 | restore_dst_buf(xd, *orig_dst); |
| 7534 | } else { |
| 7535 | restore_dst_buf(xd, *tmp_dst); |
| 7536 | } |
| 7537 | } |
| 7538 | } |
| 7539 | } else { |
| 7540 | #endif // CONFIG_DUAL_FILTER Speed feature use_fast_interpolation_filter_search |
| 7541 | // EIGHTTAP_REGULAR mode is calculated beforehand |
| 7542 | for (i = 1; i < filter_set_size; ++i) { |
| 7543 | int tmp_skip_sb = 0; |
| 7544 | int64_t tmp_skip_sse = INT64_MAX; |
| 7545 | int tmp_rs; |
| 7546 | int64_t tmp_rd; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7547 | #if CONFIG_DUAL_FILTER |
Michelle Findlay-Olynyk | a3eb912 | 2017-11-27 10:46:45 -0800 | [diff] [blame] | 7548 | mbmi->interp_filters = |
| 7549 | av1_make_interp_filters(filter_sets[i][0], filter_sets[i][1]); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7550 | #else |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7551 | mbmi->interp_filters = av1_broadcast_interp_filter((InterpFilter)i); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7552 | #endif // CONFIG_DUAL_FILTER |
Michelle Findlay-Olynyk | a3eb912 | 2017-11-27 10:46:45 -0800 | [diff] [blame] | 7553 | tmp_rs = av1_get_switchable_rate(cm, x, xd); |
| 7554 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, |
| 7555 | bsize); |
| 7556 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7557 | &tmp_dist, &tmp_skip_sb, &tmp_skip_sse); |
| 7558 | tmp_rd = RDCOST(x->rdmult, tmp_rs + tmp_rate, tmp_dist); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7559 | |
Michelle Findlay-Olynyk | a3eb912 | 2017-11-27 10:46:45 -0800 | [diff] [blame] | 7560 | if (tmp_rd < *rd) { |
| 7561 | *rd = tmp_rd; |
| 7562 | *switchable_rate = av1_get_switchable_rate(cm, x, xd); |
| 7563 | best_filters = mbmi->interp_filters; |
| 7564 | *skip_txfm_sb = tmp_skip_sb; |
| 7565 | *skip_sse_sb = tmp_skip_sse; |
| 7566 | best_in_temp = !best_in_temp; |
| 7567 | if (best_in_temp) { |
| 7568 | restore_dst_buf(xd, *orig_dst); |
| 7569 | } else { |
| 7570 | restore_dst_buf(xd, *tmp_dst); |
| 7571 | } |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7572 | } |
| 7573 | } |
Michelle Findlay-Olynyk | a3eb912 | 2017-11-27 10:46:45 -0800 | [diff] [blame] | 7574 | #if CONFIG_DUAL_FILTER // Speed feature use_fast_interpolation_filter_search |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7575 | } |
Michelle Findlay-Olynyk | a3eb912 | 2017-11-27 10:46:45 -0800 | [diff] [blame] | 7576 | #endif // CONFIG_DUAL_FILTER Speed feature use_fast_interpolation_filter_search |
| 7577 | |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7578 | if (best_in_temp) { |
| 7579 | restore_dst_buf(xd, *tmp_dst); |
| 7580 | } else { |
| 7581 | restore_dst_buf(xd, *orig_dst); |
| 7582 | } |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7583 | mbmi->interp_filters = best_filters; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7584 | } else { |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7585 | assert(mbmi->interp_filters == |
| 7586 | av1_broadcast_interp_filter(EIGHTTAP_REGULAR)); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 7587 | } |
| 7588 | } |
| 7589 | |
| 7590 | return 0; |
| 7591 | } |
| 7592 | |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7593 | #if CONFIG_DUAL_FILTER |
| 7594 | static InterpFilters condition_interp_filters_on_mv( |
| 7595 | InterpFilters interp_filters, const MACROBLOCKD *xd) { |
| 7596 | InterpFilter filters[2]; |
| 7597 | for (int i = 0; i < 2; ++i) |
| 7598 | filters[i] = (has_subpel_mv_component(xd->mi[0], xd, i)) |
| 7599 | ? av1_extract_interp_filter(interp_filters, i) |
| 7600 | : EIGHTTAP_REGULAR; |
| 7601 | |
| 7602 | return av1_make_interp_filters(filters[0], filters[1]); |
| 7603 | } |
| 7604 | #endif |
| 7605 | |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 7606 | #if CONFIG_EXT_WARPED_MOTION |
| 7607 | static int handle_zero_mv(const AV1_COMMON *const cm, MACROBLOCK *const x, |
| 7608 | BLOCK_SIZE bsize, int mi_col, int mi_row) { |
| 7609 | MACROBLOCKD *xd = &x->e_mbd; |
| 7610 | MODE_INFO *mi = xd->mi[0]; |
| 7611 | MB_MODE_INFO *mbmi = &mi->mbmi; |
| 7612 | int skip = 0; |
| 7613 | |
| 7614 | // Handle the special case of 0 MV. |
| 7615 | if (mbmi->ref_frame[0] > INTRA_FRAME && mbmi->ref_frame[1] <= INTRA_FRAME) { |
| 7616 | int8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
| 7617 | int16_t mode_ctx = x->mbmi_ext->mode_context[ref_frame_type]; |
| 7618 | if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) { |
| 7619 | int_mv zeromv; |
| 7620 | const MV_REFERENCE_FRAME ref = mbmi->ref_frame[0]; |
| 7621 | zeromv.as_int = gm_get_motion_vector(&cm->global_motion[ref], |
| 7622 | cm->allow_high_precision_mv, bsize, |
| 7623 | mi_col, mi_row, 0 |
| 7624 | #if CONFIG_AMVR |
| 7625 | , |
RogerZhou | d904a35 | 2017-11-22 15:01:30 -0800 | [diff] [blame] | 7626 | cm->cur_frame_force_integer_mv |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 7627 | #endif |
| 7628 | ) |
| 7629 | .as_int; |
| 7630 | if (mbmi->mv[0].as_int == zeromv.as_int && mbmi->mode != GLOBALMV) { |
| 7631 | skip = 1; |
| 7632 | } |
| 7633 | } |
| 7634 | } |
| 7635 | return skip; |
| 7636 | } |
| 7637 | #endif // CONFIG_EXT_WARPED_MOTION |
| 7638 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7639 | // TODO(afergs): Refactor the MBMI references in here - there's four |
| 7640 | // TODO(afergs): Refactor optional args - add them to a struct or remove |
| 7641 | static int64_t motion_mode_rd( |
| 7642 | const AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize, |
| 7643 | RD_STATS *rd_stats, RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv, |
| 7644 | int *disable_skip, int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], int mi_row, |
| 7645 | int mi_col, HandleInterModeArgs *const args, const int64_t ref_best_rd, |
| 7646 | const int *refs, int rate_mv, |
Wei-Ting Lin | 85a8f70 | 2017-06-22 13:55:15 -0700 | [diff] [blame] | 7647 | // only used when WARPED_MOTION is on? |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 7648 | int_mv *const single_newmv, int rate2_bmc_nocoeff, |
Sebastien Alaiwan | 1bc94fc | 2017-10-31 10:25:17 +0100 | [diff] [blame] | 7649 | MB_MODE_INFO *best_bmc_mbmi, int rate_mv_bmc, int rs, int *skip_txfm_sb, |
| 7650 | int64_t *skip_sse_sb, BUFFER_SET *orig_dst) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7651 | const AV1_COMMON *const cm = &cpi->common; |
| 7652 | MACROBLOCKD *xd = &x->e_mbd; |
| 7653 | MODE_INFO *mi = xd->mi[0]; |
| 7654 | MB_MODE_INFO *mbmi = &mi->mbmi; |
| 7655 | const int is_comp_pred = has_second_ref(mbmi); |
| 7656 | const PREDICTION_MODE this_mode = mbmi->mode; |
| 7657 | |
| 7658 | (void)mode_mv; |
| 7659 | (void)mi_row; |
| 7660 | (void)mi_col; |
| 7661 | (void)args; |
| 7662 | (void)refs; |
| 7663 | (void)rate_mv; |
| 7664 | (void)is_comp_pred; |
| 7665 | (void)this_mode; |
| 7666 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7667 | MOTION_MODE motion_mode, last_motion_mode_allowed; |
| 7668 | int rate2_nocoeff = 0, best_xskip, best_disable_skip = 0; |
| 7669 | RD_STATS best_rd_stats, best_rd_stats_y, best_rd_stats_uv; |
| 7670 | MB_MODE_INFO base_mbmi, best_mbmi; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7671 | uint8_t best_blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 4]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7672 | |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 7673 | #if CONFIG_EXT_WARPED_MOTION |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 7674 | int pts0[SAMPLES_ARRAY_SIZE], pts_inref0[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 7675 | int pts_mv0[SAMPLES_ARRAY_SIZE], pts_wm[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 7676 | int total_samples; |
Yunqing Wang | 3afbf3f | 2017-11-21 20:16:18 -0800 | [diff] [blame] | 7677 | int best_cand = -1; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 7678 | #else |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7679 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 7680 | #endif // CONFIG_EXT_WARPED_MOTION |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7681 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7682 | av1_invalid_rd_stats(&best_rd_stats); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7683 | |
| 7684 | if (cm->interp_filter == SWITCHABLE) rd_stats->rate += rs; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7685 | aom_clear_system_state(); |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 7686 | #if CONFIG_EXT_WARPED_MOTION |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 7687 | mbmi->num_proj_ref[0] = |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 7688 | findSamples(cm, xd, mi_row, mi_col, pts0, pts_inref0, pts_mv0, pts_wm); |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 7689 | total_samples = mbmi->num_proj_ref[0]; |
Yunqing Wang | 3afbf3f | 2017-11-21 20:16:18 -0800 | [diff] [blame] | 7690 | |
| 7691 | // Find a warped neighbor. |
| 7692 | int cand; |
| 7693 | int best_weight = 0; |
| 7694 | |
| 7695 | // if (this_mode == NEARESTMV) |
| 7696 | for (cand = 0; cand < mbmi->num_proj_ref[0]; cand++) { |
| 7697 | if (pts_wm[cand * 2 + 1] > best_weight) { |
| 7698 | best_weight = pts_wm[cand * 2 + 1]; |
| 7699 | best_cand = cand; |
| 7700 | } |
| 7701 | } |
| 7702 | mbmi->wm_ctx = best_cand; |
| 7703 | best_bmc_mbmi->wm_ctx = mbmi->wm_ctx; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 7704 | #else |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7705 | mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref); |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 7706 | #endif // CONFIG_EXT_WARPED_MOTION |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7707 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7708 | rate2_nocoeff = rd_stats->rate; |
Sebastien Alaiwan | 1f56b8e | 2017-10-31 17:37:16 +0100 | [diff] [blame] | 7709 | last_motion_mode_allowed = motion_mode_allowed(0, xd->global_motion, xd, mi); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7710 | base_mbmi = *mbmi; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7711 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7712 | int64_t best_rd = INT64_MAX; |
| 7713 | for (motion_mode = SIMPLE_TRANSLATION; |
| 7714 | motion_mode <= last_motion_mode_allowed; motion_mode++) { |
| 7715 | int64_t tmp_rd = INT64_MAX; |
| 7716 | int tmp_rate; |
| 7717 | int64_t tmp_dist; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7718 | int tmp_rate2 = |
| 7719 | motion_mode != SIMPLE_TRANSLATION ? rate2_bmc_nocoeff : rate2_nocoeff; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7720 | |
| 7721 | *mbmi = base_mbmi; |
| 7722 | mbmi->motion_mode = motion_mode; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7723 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7724 | *mbmi = *best_bmc_mbmi; |
| 7725 | mbmi->motion_mode = OBMC_CAUSAL; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 7726 | if (!is_comp_pred && have_newmv_in_inter_mode(this_mode)) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7727 | int tmp_rate_mv = 0; |
| 7728 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 7729 | single_motion_search(cpi, x, bsize, mi_row, mi_col, 0, &tmp_rate_mv); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7730 | mbmi->mv[0].as_int = x->best_mv.as_int; |
| 7731 | if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv, |
| 7732 | refs[0])) { |
| 7733 | tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
| 7734 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7735 | tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7736 | #if CONFIG_DUAL_FILTER |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7737 | mbmi->interp_filters = |
| 7738 | condition_interp_filters_on_mv(mbmi->interp_filters, xd); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7739 | #endif // CONFIG_DUAL_FILTER |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 7740 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, bsize); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7741 | } else { |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 7742 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, bsize); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7743 | } |
| 7744 | av1_build_obmc_inter_prediction( |
| 7745 | cm, xd, mi_row, mi_col, args->above_pred_buf, args->above_pred_stride, |
| 7746 | args->left_pred_buf, args->left_pred_stride); |
| 7747 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7748 | &tmp_dist, skip_txfm_sb, skip_sse_sb); |
| 7749 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7750 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7751 | if (mbmi->motion_mode == WARPED_CAUSAL) { |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 7752 | #if CONFIG_EXT_WARPED_MOTION |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 7753 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 7754 | #endif // CONFIG_EXT_WARPED_MOTION |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7755 | *mbmi = *best_bmc_mbmi; |
| 7756 | mbmi->motion_mode = WARPED_CAUSAL; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7757 | mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7758 | mbmi->interp_filters = av1_broadcast_interp_filter( |
| 7759 | av1_unswitchable_filter(cm->interp_filter)); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7760 | |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 7761 | #if CONFIG_EXT_WARPED_MOTION |
Yunqing Wang | 3afbf3f | 2017-11-21 20:16:18 -0800 | [diff] [blame] | 7762 | if (this_mode == NEARESTMV && best_cand != -1) { |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 7763 | MODE_INFO *best_mi = xd->mi[pts_wm[2 * best_cand]]; |
| 7764 | assert(best_mi->mbmi.motion_mode == WARPED_CAUSAL); |
| 7765 | mbmi->wm_params[0] = best_mi->mbmi.wm_params[0]; |
| 7766 | |
| 7767 | // Handle the special case of 0 MV. |
| 7768 | if (handle_zero_mv(cm, x, bsize, mi_col, mi_row)) continue; |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 7769 | |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 7770 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7771 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7772 | &tmp_dist, skip_txfm_sb, skip_sse_sb); |
| 7773 | } else { |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 7774 | memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0)); |
| 7775 | memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0)); |
| 7776 | // Rank the samples by motion vector difference |
| 7777 | if (mbmi->num_proj_ref[0] > 1) { |
| 7778 | mbmi->num_proj_ref[0] = sortSamples(pts_mv0, &mbmi->mv[0].as_mv, pts, |
| 7779 | pts_inref, mbmi->num_proj_ref[0]); |
| 7780 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
| 7781 | } |
| 7782 | #endif // CONFIG_EXT_WARPED_MOTION |
| 7783 | |
| 7784 | if (!find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize, |
| 7785 | mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col, |
| 7786 | &mbmi->wm_params[0], mi_row, mi_col)) { |
| 7787 | // Refine MV for NEWMV mode |
| 7788 | if (!is_comp_pred && have_newmv_in_inter_mode(this_mode)) { |
| 7789 | int tmp_rate_mv = 0; |
| 7790 | const int_mv mv0 = mbmi->mv[0]; |
Luc Trudeau | f3bf8b1 | 2017-12-08 14:38:41 -0500 | [diff] [blame] | 7791 | const WarpedMotionParams wm_params0 = mbmi->wm_params[0]; |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 7792 | #if CONFIG_EXT_WARPED_MOTION |
| 7793 | int num_proj_ref0 = mbmi->num_proj_ref[0]; |
| 7794 | |
| 7795 | // Refine MV in a small range. |
| 7796 | av1_refine_warped_mv(cpi, x, bsize, mi_row, mi_col, pts0, |
| 7797 | pts_inref0, pts_mv0, total_samples); |
| 7798 | #else |
| 7799 | // Refine MV in a small range. |
| 7800 | av1_refine_warped_mv(cpi, x, bsize, mi_row, mi_col, pts, pts_inref); |
| 7801 | #endif // CONFIG_EXT_WARPED_MOTION |
| 7802 | |
| 7803 | // Keep the refined MV and WM parameters. |
| 7804 | if (mv0.as_int != mbmi->mv[0].as_int) { |
| 7805 | const int ref = refs[0]; |
| 7806 | const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 7807 | |
| 7808 | tmp_rate_mv = |
| 7809 | av1_mv_bit_cost(&mbmi->mv[0].as_mv, &ref_mv, x->nmvjointcost, |
| 7810 | x->mvcost, MV_COST_WEIGHT); |
| 7811 | |
| 7812 | if (cpi->sf.adaptive_motion_search) |
| 7813 | x->pred_mv[ref] = mbmi->mv[0].as_mv; |
| 7814 | |
| 7815 | single_newmv[ref] = mbmi->mv[0]; |
| 7816 | |
| 7817 | if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv, |
| 7818 | refs[0])) { |
| 7819 | tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
| 7820 | } |
| 7821 | #if CONFIG_EXT_WARPED_MOTION |
| 7822 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
| 7823 | #endif // CONFIG_EXT_WARPED_MOTION |
| 7824 | tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv; |
| 7825 | #if CONFIG_DUAL_FILTER |
| 7826 | mbmi->interp_filters = |
| 7827 | condition_interp_filters_on_mv(mbmi->interp_filters, xd); |
| 7828 | #endif // CONFIG_DUAL_FILTER |
| 7829 | } else { |
| 7830 | // Restore the old MV and WM parameters. |
| 7831 | mbmi->mv[0] = mv0; |
| 7832 | mbmi->wm_params[0] = wm_params0; |
| 7833 | #if CONFIG_EXT_WARPED_MOTION |
| 7834 | mbmi->num_proj_ref[0] = num_proj_ref0; |
| 7835 | #endif // CONFIG_EXT_WARPED_MOTION |
| 7836 | } |
| 7837 | } |
| 7838 | |
| 7839 | #if CONFIG_EXT_WARPED_MOTION |
| 7840 | // Handle the special case of 0 MV. |
| 7841 | if (handle_zero_mv(cm, x, bsize, mi_col, mi_row)) continue; |
| 7842 | #endif // CONFIG_EXT_WARPED_MOTION |
| 7843 | |
| 7844 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize); |
| 7845 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 7846 | &tmp_dist, skip_txfm_sb, skip_sse_sb); |
| 7847 | } else { |
| 7848 | continue; |
| 7849 | } |
| 7850 | #if CONFIG_EXT_WARPED_MOTION |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7851 | } |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 7852 | #endif // CONFIG_EXT_WARPED_MOTION |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7853 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7854 | x->skip = 0; |
| 7855 | |
| 7856 | rd_stats->dist = 0; |
| 7857 | rd_stats->sse = 0; |
| 7858 | rd_stats->skip = 1; |
| 7859 | rd_stats->rate = tmp_rate2; |
| 7860 | if (last_motion_mode_allowed > SIMPLE_TRANSLATION) { |
Yunqing Wang | 3afbf3f | 2017-11-21 20:16:18 -0800 | [diff] [blame] | 7861 | if (last_motion_mode_allowed == WARPED_CAUSAL) { |
| 7862 | #if CONFIG_EXT_WARPED_MOTION |
| 7863 | int wm_ctx = 0; |
| 7864 | if (mbmi->wm_ctx != -1) { |
| 7865 | wm_ctx = 1; |
| 7866 | if (mbmi->mode == NEARESTMV) wm_ctx = 2; |
| 7867 | } |
| 7868 | |
| 7869 | rd_stats->rate += x->motion_mode_cost[wm_ctx][bsize][mbmi->motion_mode]; |
| 7870 | #else |
Yue Chen | bdc8dab | 2017-07-26 12:05:47 -0700 | [diff] [blame] | 7871 | rd_stats->rate += x->motion_mode_cost[bsize][mbmi->motion_mode]; |
Yunqing Wang | 3afbf3f | 2017-11-21 20:16:18 -0800 | [diff] [blame] | 7872 | #endif // CONFIG_EXT_WARPED_MOTION |
| 7873 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 7874 | rd_stats->rate += x->motion_mode_cost1[bsize][mbmi->motion_mode]; |
Yunqing Wang | 3afbf3f | 2017-11-21 20:16:18 -0800 | [diff] [blame] | 7875 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7876 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7877 | if (mbmi->motion_mode == WARPED_CAUSAL) { |
| 7878 | rd_stats->rate -= rs; |
| 7879 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7880 | if (!*skip_txfm_sb) { |
| 7881 | int64_t rdcosty = INT64_MAX; |
| 7882 | int is_cost_valid_uv = 0; |
| 7883 | |
| 7884 | // cost and distortion |
| 7885 | av1_subtract_plane(x, bsize, 0); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7886 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 7887 | // Motion mode |
Yue Chen | 25dc070 | 2017-10-18 23:36:06 -0700 | [diff] [blame] | 7888 | select_tx_type_yrd(cpi, x, rd_stats_y, bsize, mi_row, mi_col, |
| 7889 | ref_best_rd); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7890 | } else { |
| 7891 | int idx, idy; |
| 7892 | super_block_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd); |
| 7893 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 7894 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 7895 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 7896 | memset(x->blk_skip[0], rd_stats_y->skip, |
| 7897 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 7898 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7899 | |
| 7900 | if (rd_stats_y->rate == INT_MAX) { |
| 7901 | av1_invalid_rd_stats(rd_stats); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7902 | if (mbmi->motion_mode != SIMPLE_TRANSLATION) { |
| 7903 | continue; |
| 7904 | } else { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7905 | restore_dst_buf(xd, *orig_dst); |
| 7906 | return INT64_MAX; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7907 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7908 | } |
| 7909 | |
| 7910 | av1_merge_rd_stats(rd_stats, rd_stats_y); |
| 7911 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7912 | rdcosty = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
| 7913 | rdcosty = AOMMIN(rdcosty, RDCOST(x->rdmult, 0, rd_stats->sse)); |
Sebastien Alaiwan | fb83877 | 2017-10-24 12:02:54 +0200 | [diff] [blame] | 7914 | /* clang-format off */ |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7915 | is_cost_valid_uv = |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 7916 | inter_block_uvrd(cpi, x, rd_stats_uv, bsize, ref_best_rd - rdcosty, |
| 7917 | 0); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7918 | if (!is_cost_valid_uv) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7919 | continue; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7920 | } |
| 7921 | /* clang-format on */ |
| 7922 | av1_merge_rd_stats(rd_stats, rd_stats_uv); |
| 7923 | #if CONFIG_RD_DEBUG |
| 7924 | // record transform block coefficient cost |
| 7925 | // TODO(angiebird): So far rd_debug tool only detects discrepancy of |
| 7926 | // coefficient cost. Therefore, it is fine to copy rd_stats into mbmi |
| 7927 | // here because we already collect the coefficient cost. Move this part to |
| 7928 | // other place when we need to compare non-coefficient cost. |
| 7929 | mbmi->rd_stats = *rd_stats; |
| 7930 | #endif // CONFIG_RD_DEBUG |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 7931 | const int skip_ctx = av1_get_skip_context(xd); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7932 | if (rd_stats->skip) { |
| 7933 | rd_stats->rate -= rd_stats_uv->rate + rd_stats_y->rate; |
| 7934 | rd_stats_y->rate = 0; |
| 7935 | rd_stats_uv->rate = 0; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 7936 | rd_stats->rate += x->skip_cost[skip_ctx][1]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7937 | mbmi->skip = 0; |
| 7938 | // here mbmi->skip temporarily plays a role as what this_skip2 does |
| 7939 | } else if (!xd->lossless[mbmi->segment_id] && |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7940 | (RDCOST(x->rdmult, |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7941 | rd_stats_y->rate + rd_stats_uv->rate + |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 7942 | x->skip_cost[skip_ctx][0], |
| 7943 | rd_stats->dist) >= RDCOST(x->rdmult, |
| 7944 | x->skip_cost[skip_ctx][1], |
| 7945 | rd_stats->sse))) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7946 | rd_stats->rate -= rd_stats_uv->rate + rd_stats_y->rate; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 7947 | rd_stats->rate += x->skip_cost[skip_ctx][1]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7948 | rd_stats->dist = rd_stats->sse; |
| 7949 | rd_stats_y->rate = 0; |
| 7950 | rd_stats_uv->rate = 0; |
| 7951 | mbmi->skip = 1; |
| 7952 | } else { |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 7953 | rd_stats->rate += x->skip_cost[skip_ctx][0]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7954 | mbmi->skip = 0; |
| 7955 | } |
| 7956 | *disable_skip = 0; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7957 | } else { |
| 7958 | x->skip = 1; |
| 7959 | *disable_skip = 1; |
| 7960 | mbmi->tx_size = tx_size_from_tx_mode(bsize, cm->tx_mode, 1); |
| 7961 | |
Sebastien Alaiwan | 1bc94fc | 2017-10-31 10:25:17 +0100 | [diff] [blame] | 7962 | // The cost of skip bit needs to be added. |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7963 | mbmi->skip = 0; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 7964 | rd_stats->rate += x->skip_cost[av1_get_skip_context(xd)][1]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7965 | |
| 7966 | rd_stats->dist = *skip_sse_sb; |
| 7967 | rd_stats->sse = *skip_sse_sb; |
| 7968 | rd_stats_y->rate = 0; |
| 7969 | rd_stats_uv->rate = 0; |
| 7970 | rd_stats->skip = 1; |
| 7971 | } |
| 7972 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 7973 | if (this_mode == GLOBALMV || this_mode == GLOBAL_GLOBALMV) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7974 | if (is_nontrans_global_motion(xd)) { |
| 7975 | rd_stats->rate -= rs; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7976 | mbmi->interp_filters = av1_broadcast_interp_filter( |
| 7977 | av1_unswitchable_filter(cm->interp_filter)); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7978 | } |
| 7979 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7980 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7981 | tmp_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7982 | if (mbmi->motion_mode == SIMPLE_TRANSLATION || (tmp_rd < best_rd)) { |
| 7983 | best_mbmi = *mbmi; |
| 7984 | best_rd = tmp_rd; |
| 7985 | best_rd_stats = *rd_stats; |
| 7986 | best_rd_stats_y = *rd_stats_y; |
| 7987 | best_rd_stats_uv = *rd_stats_uv; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7988 | for (int i = 0; i < MAX_MB_PLANE; ++i) |
| 7989 | memcpy(best_blk_skip[i], x->blk_skip[i], |
| 7990 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 7991 | best_xskip = x->skip; |
| 7992 | best_disable_skip = *disable_skip; |
| 7993 | } |
| 7994 | } |
| 7995 | |
| 7996 | if (best_rd == INT64_MAX) { |
| 7997 | av1_invalid_rd_stats(rd_stats); |
| 7998 | restore_dst_buf(xd, *orig_dst); |
| 7999 | return INT64_MAX; |
| 8000 | } |
| 8001 | *mbmi = best_mbmi; |
| 8002 | *rd_stats = best_rd_stats; |
| 8003 | *rd_stats_y = best_rd_stats_y; |
| 8004 | *rd_stats_uv = best_rd_stats_uv; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8005 | for (int i = 0; i < MAX_MB_PLANE; ++i) |
| 8006 | memcpy(x->blk_skip[i], best_blk_skip[i], |
| 8007 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8008 | x->skip = best_xskip; |
| 8009 | *disable_skip = best_disable_skip; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8010 | |
| 8011 | restore_dst_buf(xd, *orig_dst); |
| 8012 | return 0; |
| 8013 | } |
| 8014 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 8015 | #if CONFIG_EXT_SKIP |
| 8016 | static int64_t skip_mode_rd(const AV1_COMP *const cpi, MACROBLOCK *const x, |
| 8017 | BLOCK_SIZE bsize, int mi_row, int mi_col, |
| 8018 | BUFFER_SET *const orig_dst) { |
| 8019 | const AV1_COMMON *cm = &cpi->common; |
| 8020 | MACROBLOCKD *const xd = &x->e_mbd; |
| 8021 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8022 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 8023 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, bsize); |
| 8024 | |
| 8025 | int64_t total_sse = 0; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 8026 | for (int plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 8027 | const struct macroblock_plane *const p = &x->plane[plane]; |
| 8028 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 8029 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
| 8030 | const int bw = block_size_wide[plane_bsize]; |
| 8031 | const int bh = block_size_high[plane_bsize]; |
| 8032 | |
| 8033 | av1_subtract_plane(x, bsize, plane); |
| 8034 | int64_t sse = aom_sum_squares_2d_i16(p->src_diff, bw, bw, bh); |
| 8035 | sse = sse << 4; |
| 8036 | total_sse += sse; |
| 8037 | } |
| 8038 | x->skip_mode_dist = x->skip_mode_sse = total_sse; |
| 8039 | x->skip_mode_rate = 0; |
| 8040 | x->skip_mode_rdcost = RDCOST(x->rdmult, x->skip_mode_rate, x->skip_mode_dist); |
| 8041 | |
| 8042 | // Save the ref frames / motion vectors |
| 8043 | x->skip_mode_ref_frame[0] = mbmi->ref_frame[0]; |
| 8044 | x->skip_mode_ref_frame[1] = mbmi->ref_frame[1]; |
| 8045 | x->skip_mode_mv[0].as_int = mbmi->mv[0].as_int; |
| 8046 | x->skip_mode_mv[1].as_int = mbmi->mv[1].as_int; |
| 8047 | |
| 8048 | // Save the mode index |
| 8049 | x->skip_mode_index = x->skip_mode_index_candidate; |
| 8050 | |
| 8051 | restore_dst_buf(xd, *orig_dst); |
| 8052 | return 0; |
| 8053 | } |
| 8054 | #endif // CONFIG_EXT_SKIP |
| 8055 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 8056 | static int64_t handle_inter_mode( |
| 8057 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, |
| 8058 | RD_STATS *rd_stats, RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv, |
| 8059 | int *disable_skip, int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], int mi_row, |
| 8060 | int mi_col, HandleInterModeArgs *args, const int64_t ref_best_rd) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8061 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8062 | MACROBLOCKD *xd = &x->e_mbd; |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 8063 | MODE_INFO *mi = xd->mi[0]; |
| 8064 | MB_MODE_INFO *mbmi = &mi->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8065 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 8066 | const int is_comp_pred = has_second_ref(mbmi); |
| 8067 | const int this_mode = mbmi->mode; |
| 8068 | int_mv *frame_mv = mode_mv[this_mode]; |
| 8069 | int i; |
| 8070 | int refs[2] = { mbmi->ref_frame[0], |
| 8071 | (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; |
| 8072 | int_mv cur_mv[2]; |
| 8073 | int rate_mv = 0; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 8074 | int pred_exists = 1; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 8075 | const int bw = block_size_wide[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8076 | int_mv single_newmv[TOTAL_REFS_PER_FRAME]; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8077 | const int *const interintra_mode_cost = |
| 8078 | x->interintra_mode_cost[size_group_lookup[bsize]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8079 | const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8080 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8081 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8082 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 8083 | #else |
| 8084 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_[MAX_MB_PLANE * MAX_SB_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8085 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8086 | uint8_t *tmp_buf; |
| 8087 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8088 | int rate2_bmc_nocoeff; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8089 | MB_MODE_INFO best_bmc_mbmi; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 8090 | int rate_mv_bmc; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 8091 | int64_t rd = INT64_MAX; |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8092 | BUFFER_SET orig_dst, tmp_dst; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8093 | int rs = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8094 | |
| 8095 | int skip_txfm_sb = 0; |
| 8096 | int64_t skip_sse_sb = INT64_MAX; |
Yaowu Xu | b0d0d00 | 2016-11-22 09:26:43 -0800 | [diff] [blame] | 8097 | int16_t mode_ctx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8098 | |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8099 | int compmode_interintra_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8100 | mbmi->use_wedge_interintra = 0; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8101 | int compmode_interinter_cost = 0; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8102 | mbmi->interinter_compound_type = COMPOUND_AVERAGE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8103 | |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 8104 | if (!cm->allow_interintra_compound && is_comp_interintra_pred) |
| 8105 | return INT64_MAX; |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 8106 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8107 | // is_comp_interintra_pred implies !is_comp_pred |
| 8108 | assert(!is_comp_interintra_pred || (!is_comp_pred)); |
| 8109 | // is_comp_interintra_pred implies is_interintra_allowed(mbmi->sb_type) |
| 8110 | assert(!is_comp_interintra_pred || is_interintra_allowed(mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8111 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8112 | if (is_comp_pred) |
| 8113 | mode_ctx = mbmi_ext->compound_mode_context[refs[0]]; |
| 8114 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8115 | mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context, |
| 8116 | mbmi->ref_frame, bsize, -1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8117 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8118 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8119 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 8120 | tmp_buf = CONVERT_TO_BYTEPTR(tmp_buf_); |
| 8121 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8122 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8123 | tmp_buf = tmp_buf_; |
David Barker | b8069f9 | 2016-11-18 14:49:56 +0000 | [diff] [blame] | 8124 | // Make sure that we didn't leave the plane destination buffers set |
| 8125 | // to tmp_buf at the end of the last iteration |
| 8126 | assert(xd->plane[0].dst.buf != tmp_buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8127 | |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 8128 | mbmi->num_proj_ref[0] = 0; |
| 8129 | mbmi->num_proj_ref[1] = 0; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 8130 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8131 | if (is_comp_pred) { |
| 8132 | if (frame_mv[refs[0]].as_int == INVALID_MV || |
| 8133 | frame_mv[refs[1]].as_int == INVALID_MV) |
| 8134 | return INT64_MAX; |
| 8135 | } |
| 8136 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 8137 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8138 | if (have_newmv_in_inter_mode(this_mode)) { |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 8139 | const int64_t ret_val = handle_newmv(cpi, x, bsize, mode_mv, mi_row, mi_col, |
| 8140 | &rate_mv, single_newmv, args); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8141 | if (ret_val != 0) |
| 8142 | return ret_val; |
| 8143 | else |
| 8144 | rd_stats->rate += rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8145 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8146 | for (i = 0; i < is_comp_pred + 1; ++i) { |
| 8147 | cur_mv[i] = frame_mv[refs[i]]; |
Zoe Liu | ed29ea2 | 2017-04-20 16:48:15 -0700 | [diff] [blame] | 8148 | // Clip "next_nearest" so that it does not extend to far out of image |
| 8149 | if (this_mode != NEWMV) clamp_mv2(&cur_mv[i].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8150 | if (mv_check_bounds(&x->mv_limits, &cur_mv[i].as_mv)) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8151 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 8152 | } |
| 8153 | |
Cheng Chen | 0a7f2f5 | 2017-10-10 15:16:09 -0700 | [diff] [blame] | 8154 | #if CONFIG_JNT_COMP |
Cheng Chen | b09e55c | 2017-11-10 12:09:19 -0800 | [diff] [blame] | 8155 | if (is_comp_pred) { |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8156 | if (mbmi->compound_idx == 0) { |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 8157 | int masked_compound_used = is_any_masked_compound_used(bsize); |
| 8158 | masked_compound_used = masked_compound_used && cm->allow_masked_compound; |
| 8159 | |
| 8160 | if (masked_compound_used) { |
| 8161 | const int comp_group_idx_ctx = get_comp_group_idx_context(xd); |
| 8162 | rd_stats->rate += x->comp_group_idx_cost[comp_group_idx_ctx][0]; |
| 8163 | } |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8164 | |
| 8165 | const int comp_index_ctx = get_comp_index_context(cm, xd); |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 8166 | rd_stats->rate += x->comp_idx_cost[comp_index_ctx][mbmi->compound_idx]; |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8167 | } |
Cheng Chen | 0a7f2f5 | 2017-10-10 15:16:09 -0700 | [diff] [blame] | 8168 | } |
| 8169 | #endif // CONFIG_JNT_COMP |
| 8170 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8171 | if (this_mode == NEAREST_NEARESTMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8172 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
| 8173 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 8174 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 8175 | |
| 8176 | for (i = 0; i < 2; ++i) { |
| 8177 | clamp_mv2(&cur_mv[i].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8178 | if (mv_check_bounds(&x->mv_limits, &cur_mv[i].as_mv)) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8179 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 8180 | } |
| 8181 | } |
| 8182 | } |
| 8183 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8184 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 8185 | if (this_mode == NEAREST_NEWMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8186 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 8187 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8188 | #if CONFIG_AMVR |
| 8189 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv, |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 8190 | cm->cur_frame_force_integer_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8191 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8192 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8193 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8194 | clamp_mv2(&cur_mv[0].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8195 | if (mv_check_bounds(&x->mv_limits, &cur_mv[0].as_mv)) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8196 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 8197 | } |
| 8198 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 8199 | if (this_mode == NEW_NEARESTMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8200 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 8201 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8202 | #if CONFIG_AMVR |
| 8203 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv, |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 8204 | cm->cur_frame_force_integer_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8205 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8206 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8207 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8208 | clamp_mv2(&cur_mv[1].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8209 | if (mv_check_bounds(&x->mv_limits, &cur_mv[1].as_mv)) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8210 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 8211 | } |
| 8212 | } |
| 8213 | |
| 8214 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 8215 | int ref_mv_idx = mbmi->ref_mv_idx + 1; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 8216 | if (this_mode == NEAR_NEWMV || this_mode == NEAR_NEARMV) { |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 8217 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8218 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8219 | #if CONFIG_AMVR |
| 8220 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv, |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 8221 | cm->cur_frame_force_integer_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8222 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8223 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8224 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8225 | clamp_mv2(&cur_mv[0].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8226 | if (mv_check_bounds(&x->mv_limits, &cur_mv[0].as_mv)) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8227 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 8228 | } |
| 8229 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 8230 | if (this_mode == NEW_NEARMV || this_mode == NEAR_NEARMV) { |
| 8231 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8232 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8233 | #if CONFIG_AMVR |
| 8234 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv, |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 8235 | cm->cur_frame_force_integer_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8236 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8237 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8238 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8239 | clamp_mv2(&cur_mv[1].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8240 | if (mv_check_bounds(&x->mv_limits, &cur_mv[1].as_mv)) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8241 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 8242 | } |
| 8243 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8244 | |
| 8245 | // do first prediction into the destination buffer. Do the next |
| 8246 | // prediction into a temporary buffer. Then keep track of which one |
| 8247 | // of these currently holds the best predictor, and use the other |
| 8248 | // one for future predictions. In the end, copy from tmp_buf to |
| 8249 | // dst if necessary. |
| 8250 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8251 | tmp_dst.plane[i] = tmp_buf + i * MAX_SB_SQUARE; |
| 8252 | tmp_dst.stride[i] = MAX_SB_SIZE; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 8253 | } |
| 8254 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8255 | orig_dst.plane[i] = xd->plane[i].dst.buf; |
| 8256 | orig_dst.stride[i] = xd->plane[i].dst.stride; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8257 | } |
| 8258 | |
| 8259 | // We don't include the cost of the second reference here, because there |
| 8260 | // are only three options: Last/Golden, ARF/Last or Golden/ARF, or in other |
| 8261 | // words if you present them in that order, the second one is always known |
| 8262 | // if the first is known. |
| 8263 | // |
| 8264 | // Under some circumstances we discount the cost of new mv mode to encourage |
| 8265 | // initiation of a motion field. |
| 8266 | if (discount_newmv_test(cpi, this_mode, frame_mv[refs[0]], mode_mv, |
| 8267 | refs[0])) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8268 | rd_stats->rate += AOMMIN( |
| 8269 | cost_mv_ref(x, this_mode, mode_ctx), |
| 8270 | cost_mv_ref(x, is_comp_pred ? NEAREST_NEARESTMV : NEARESTMV, mode_ctx)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8271 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8272 | rd_stats->rate += cost_mv_ref(x, this_mode, mode_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8273 | } |
| 8274 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8275 | if (RDCOST(x->rdmult, rd_stats->rate, 0) > ref_best_rd && |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8276 | mbmi->mode != NEARESTMV && mbmi->mode != NEAREST_NEARESTMV) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8277 | return INT64_MAX; |
| 8278 | |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8279 | int64_t ret_val = interpolation_filter_search( |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 8280 | x, cpi, bsize, mi_row, mi_col, &tmp_dst, &orig_dst, args->single_filter, |
| 8281 | &rd, &rs, &skip_txfm_sb, &skip_sse_sb); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8282 | if (ret_val != 0) return ret_val; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8283 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8284 | best_bmc_mbmi = *mbmi; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 8285 | rate2_bmc_nocoeff = rd_stats->rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8286 | if (cm->interp_filter == SWITCHABLE) rate2_bmc_nocoeff += rs; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 8287 | rate_mv_bmc = rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8288 | |
Cheng Chen | 3392c43 | 2017-10-23 15:58:23 -0700 | [diff] [blame] | 8289 | #if CONFIG_JNT_COMP |
| 8290 | if (is_comp_pred && mbmi->compound_idx) |
| 8291 | #else |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 8292 | if (is_comp_pred) |
Cheng Chen | 3392c43 | 2017-10-23 15:58:23 -0700 | [diff] [blame] | 8293 | #endif // CONFIG_JNT_COMP |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 8294 | { |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 8295 | int rate_sum, rs2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8296 | int64_t dist_sum; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8297 | int64_t best_rd_compound = INT64_MAX, best_rd_cur = INT64_MAX; |
| 8298 | INTERINTER_COMPOUND_DATA best_compound_data; |
| 8299 | int_mv best_mv[2]; |
| 8300 | int best_tmp_rate_mv = rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8301 | int tmp_skip_txfm_sb; |
| 8302 | int64_t tmp_skip_sse_sb; |
Yaowu Xu | 5e8007f | 2017-06-28 12:39:18 -0700 | [diff] [blame] | 8303 | DECLARE_ALIGNED(16, uint8_t, pred0[2 * MAX_SB_SQUARE]); |
| 8304 | DECLARE_ALIGNED(16, uint8_t, pred1[2 * MAX_SB_SQUARE]); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8305 | uint8_t *preds0[1] = { pred0 }; |
| 8306 | uint8_t *preds1[1] = { pred1 }; |
| 8307 | int strides[1] = { bw }; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 8308 | int tmp_rate_mv; |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 8309 | int masked_compound_used = is_any_masked_compound_used(bsize); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 8310 | masked_compound_used = masked_compound_used && cm->allow_masked_compound; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8311 | COMPOUND_TYPE cur_type; |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 8312 | int best_compmode_interinter_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8313 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8314 | best_mv[0].as_int = cur_mv[0].as_int; |
| 8315 | best_mv[1].as_int = cur_mv[1].as_int; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8316 | memset(&best_compound_data, 0, sizeof(best_compound_data)); |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8317 | uint8_t tmp_mask_buf[2 * MAX_SB_SQUARE]; |
| 8318 | best_compound_data.seg_mask = tmp_mask_buf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8319 | |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 8320 | if (masked_compound_used) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8321 | // get inter predictors to use for masked compound modes |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8322 | av1_build_inter_predictors_for_planes_single_buf( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8323 | xd, bsize, 0, 0, mi_row, mi_col, 0, preds0, strides); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8324 | av1_build_inter_predictors_for_planes_single_buf( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8325 | xd, bsize, 0, 0, mi_row, mi_col, 1, preds1, strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8326 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8327 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8328 | for (cur_type = COMPOUND_AVERAGE; cur_type < COMPOUND_TYPES; cur_type++) { |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 8329 | if (cur_type != COMPOUND_AVERAGE && !masked_compound_used) break; |
Yue Chen | e251825 | 2017-06-05 12:36:46 -0700 | [diff] [blame] | 8330 | if (!is_interinter_compound_used(cur_type, bsize)) continue; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 8331 | tmp_rate_mv = rate_mv; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8332 | best_rd_cur = INT64_MAX; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8333 | mbmi->interinter_compound_type = cur_type; |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8334 | #if CONFIG_JNT_COMP |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 8335 | const int comp_group_idx_ctx = get_comp_group_idx_context(xd); |
| 8336 | int masked_type_cost = 0; |
| 8337 | if (masked_compound_used) { |
| 8338 | if (cur_type == COMPOUND_AVERAGE) { |
| 8339 | masked_type_cost += x->comp_group_idx_cost[comp_group_idx_ctx][0]; |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8340 | |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 8341 | const int comp_index_ctx = get_comp_index_context(cm, xd); |
| 8342 | masked_type_cost += x->comp_idx_cost[comp_index_ctx][1]; |
| 8343 | } else { |
| 8344 | masked_type_cost += x->comp_group_idx_cost[comp_group_idx_ctx][1]; |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8345 | |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 8346 | masked_type_cost += |
| 8347 | x->compound_type_cost[bsize][mbmi->interinter_compound_type - 1]; |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8348 | } |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 8349 | } else { |
| 8350 | const int comp_index_ctx = get_comp_index_context(cm, xd); |
| 8351 | masked_type_cost += x->comp_idx_cost[comp_index_ctx][1]; |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8352 | } |
Cheng Chen | 5a88172 | 2017-11-30 17:05:10 -0800 | [diff] [blame] | 8353 | rs2 = av1_cost_literal(get_interinter_compound_type_bits( |
| 8354 | bsize, mbmi->interinter_compound_type)) + |
| 8355 | masked_type_cost; |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8356 | #else |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 8357 | int masked_type_cost = 0; |
| 8358 | if (masked_compound_used) { |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 8359 | if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize)) |
| 8360 | masked_type_cost += av1_cost_literal(1); |
| 8361 | else |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 8362 | masked_type_cost += |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 8363 | x->compound_type_cost[bsize][mbmi->interinter_compound_type]; |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 8364 | } |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8365 | rs2 = av1_cost_literal(get_interinter_compound_type_bits( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8366 | bsize, mbmi->interinter_compound_type)) + |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 8367 | masked_type_cost; |
Cheng Chen | 2ef24ea | 2017-11-29 12:22:24 -0800 | [diff] [blame] | 8368 | #endif // CONFIG_JNT_COMP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8369 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8370 | switch (cur_type) { |
| 8371 | case COMPOUND_AVERAGE: |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8372 | av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst, |
| 8373 | bsize); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8374 | av1_subtract_plane(x, bsize, 0); |
| 8375 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 8376 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, |
| 8377 | INT64_MAX); |
| 8378 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8379 | best_rd_cur = RDCOST(x->rdmult, rs2 + rate_mv + rate_sum, dist_sum); |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 8380 | best_rd_compound = best_rd_cur; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8381 | break; |
| 8382 | case COMPOUND_WEDGE: |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8383 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh && |
| 8384 | best_rd_compound / 3 < ref_best_rd) { |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8385 | best_rd_cur = build_and_cost_compound_type( |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8386 | cpi, x, cur_mv, bsize, this_mode, rs2, rate_mv, &orig_dst, |
| 8387 | &tmp_rate_mv, preds0, preds1, strides, mi_row, mi_col); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8388 | } |
| 8389 | break; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8390 | case COMPOUND_SEG: |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8391 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh && |
| 8392 | best_rd_compound / 3 < ref_best_rd) { |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8393 | best_rd_cur = build_and_cost_compound_type( |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8394 | cpi, x, cur_mv, bsize, this_mode, rs2, rate_mv, &orig_dst, |
| 8395 | &tmp_rate_mv, preds0, preds1, strides, mi_row, mi_col); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8396 | } |
| 8397 | break; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8398 | default: assert(0); return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8399 | } |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 8400 | |
| 8401 | if (best_rd_cur < best_rd_compound) { |
| 8402 | best_rd_compound = best_rd_cur; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8403 | best_compound_data.wedge_index = mbmi->wedge_index; |
| 8404 | best_compound_data.wedge_sign = mbmi->wedge_sign; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8405 | best_compound_data.mask_type = mbmi->mask_type; |
| 8406 | memcpy(best_compound_data.seg_mask, xd->seg_mask, |
| 8407 | 2 * MAX_SB_SQUARE * sizeof(uint8_t)); |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8408 | best_compound_data.interinter_compound_type = |
| 8409 | mbmi->interinter_compound_type; |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 8410 | best_compmode_interinter_cost = rs2; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 8411 | if (have_newmv_in_inter_mode(this_mode)) { |
| 8412 | if (use_masked_motion_search(cur_type)) { |
| 8413 | best_tmp_rate_mv = tmp_rate_mv; |
| 8414 | best_mv[0].as_int = mbmi->mv[0].as_int; |
| 8415 | best_mv[1].as_int = mbmi->mv[1].as_int; |
| 8416 | } else { |
| 8417 | best_mv[0].as_int = cur_mv[0].as_int; |
| 8418 | best_mv[1].as_int = cur_mv[1].as_int; |
| 8419 | } |
| 8420 | } |
| 8421 | } |
| 8422 | // reset to original mvs for next iteration |
| 8423 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 8424 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8425 | } |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8426 | mbmi->wedge_index = best_compound_data.wedge_index; |
| 8427 | mbmi->wedge_sign = best_compound_data.wedge_sign; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8428 | mbmi->mask_type = best_compound_data.mask_type; |
| 8429 | memcpy(xd->seg_mask, best_compound_data.seg_mask, |
| 8430 | 2 * MAX_SB_SQUARE * sizeof(uint8_t)); |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8431 | mbmi->interinter_compound_type = |
| 8432 | best_compound_data.interinter_compound_type; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8433 | if (have_newmv_in_inter_mode(this_mode)) { |
| 8434 | mbmi->mv[0].as_int = best_mv[0].as_int; |
| 8435 | mbmi->mv[1].as_int = best_mv[1].as_int; |
| 8436 | xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int; |
| 8437 | xd->mi[0]->bmi[0].as_mv[1].as_int = mbmi->mv[1].as_int; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8438 | if (use_masked_motion_search(mbmi->interinter_compound_type)) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8439 | rd_stats->rate += best_tmp_rate_mv - rate_mv; |
| 8440 | rate_mv = best_tmp_rate_mv; |
| 8441 | } |
| 8442 | } |
| 8443 | |
| 8444 | if (ref_best_rd < INT64_MAX && best_rd_compound / 3 > ref_best_rd) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8445 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8446 | return INT64_MAX; |
David Barker | b8069f9 | 2016-11-18 14:49:56 +0000 | [diff] [blame] | 8447 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8448 | |
| 8449 | pred_exists = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8450 | |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 8451 | compmode_interinter_cost = best_compmode_interinter_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8452 | } |
| 8453 | |
| 8454 | if (is_comp_interintra_pred) { |
| 8455 | INTERINTRA_MODE best_interintra_mode = II_DC_PRED; |
| 8456 | int64_t best_interintra_rd = INT64_MAX; |
| 8457 | int rmode, rate_sum; |
| 8458 | int64_t dist_sum; |
| 8459 | int j; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8460 | int tmp_rate_mv = 0; |
| 8461 | int tmp_skip_txfm_sb; |
| 8462 | int64_t tmp_skip_sse_sb; |
| 8463 | DECLARE_ALIGNED(16, uint8_t, intrapred_[2 * MAX_SB_SQUARE]); |
| 8464 | uint8_t *intrapred; |
| 8465 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8466 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8467 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 8468 | intrapred = CONVERT_TO_BYTEPTR(intrapred_); |
| 8469 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8470 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8471 | intrapred = intrapred_; |
| 8472 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 8473 | mbmi->ref_frame[1] = NONE_FRAME; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8474 | for (j = 0; j < MAX_MB_PLANE; j++) { |
| 8475 | xd->plane[j].dst.buf = tmp_buf + j * MAX_SB_SQUARE; |
| 8476 | xd->plane[j].dst.stride = bw; |
| 8477 | } |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 8478 | av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst, bsize); |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8479 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8480 | mbmi->ref_frame[1] = INTRA_FRAME; |
| 8481 | mbmi->use_wedge_interintra = 0; |
| 8482 | |
| 8483 | for (j = 0; j < INTERINTRA_MODES; ++j) { |
| 8484 | mbmi->interintra_mode = (INTERINTRA_MODE)j; |
| 8485 | rmode = interintra_mode_cost[mbmi->interintra_mode]; |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 8486 | av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, &orig_dst, |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8487 | intrapred, bw); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8488 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8489 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 8490 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8491 | rd = RDCOST(x->rdmult, tmp_rate_mv + rate_sum + rmode, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8492 | if (rd < best_interintra_rd) { |
| 8493 | best_interintra_rd = rd; |
| 8494 | best_interintra_mode = mbmi->interintra_mode; |
| 8495 | } |
| 8496 | } |
| 8497 | mbmi->interintra_mode = best_interintra_mode; |
| 8498 | rmode = interintra_mode_cost[mbmi->interintra_mode]; |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 8499 | av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, &orig_dst, |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8500 | intrapred, bw); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8501 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
| 8502 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8503 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 8504 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 8505 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8506 | rd = RDCOST(x->rdmult, rate_mv + rmode + rate_sum, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8507 | best_interintra_rd = rd; |
| 8508 | |
| 8509 | if (ref_best_rd < INT64_MAX && best_interintra_rd > 2 * ref_best_rd) { |
David Barker | b8069f9 | 2016-11-18 14:49:56 +0000 | [diff] [blame] | 8510 | // Don't need to call restore_dst_buf here |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8511 | return INT64_MAX; |
| 8512 | } |
| 8513 | if (is_interintra_wedge_used(bsize)) { |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 8514 | int64_t best_interintra_rd_nowedge = INT64_MAX; |
| 8515 | int64_t best_interintra_rd_wedge = INT64_MAX; |
| 8516 | int_mv tmp_mv; |
Yue Chen | eaf128a | 2017-10-16 17:01:36 -0700 | [diff] [blame] | 8517 | int rwedge = x->wedge_interintra_cost[bsize][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8518 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8519 | rd = RDCOST(x->rdmult, rmode + rate_mv + rwedge + rate_sum, dist_sum); |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 8520 | best_interintra_rd_nowedge = best_interintra_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8521 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8522 | // Disable wedge search if source variance is small |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8523 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh) { |
| 8524 | mbmi->use_wedge_interintra = 1; |
| 8525 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8526 | rwedge = av1_cost_literal(get_interintra_wedge_bits(bsize)) + |
Yue Chen | eaf128a | 2017-10-16 17:01:36 -0700 | [diff] [blame] | 8527 | x->wedge_interintra_cost[bsize][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8528 | |
| 8529 | best_interintra_rd_wedge = |
| 8530 | pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_); |
| 8531 | |
| 8532 | best_interintra_rd_wedge += |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8533 | RDCOST(x->rdmult, rmode + rate_mv + rwedge, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8534 | // Refine motion vector. |
| 8535 | if (have_newmv_in_inter_mode(this_mode)) { |
| 8536 | // get negative of mask |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8537 | const uint8_t *mask = av1_get_contiguous_soft_mask( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8538 | mbmi->interintra_wedge_index, 1, bsize); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 8539 | tmp_mv.as_int = x->mbmi_ext->ref_mvs[refs[0]][0].as_int; |
| 8540 | compound_single_motion_search(cpi, x, bsize, &tmp_mv.as_mv, mi_row, |
| 8541 | mi_col, intrapred, mask, bw, |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 8542 | &tmp_rate_mv, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8543 | mbmi->mv[0].as_int = tmp_mv.as_int; |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 8544 | av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst, |
| 8545 | bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8546 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 8547 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8548 | rd = RDCOST(x->rdmult, rmode + tmp_rate_mv + rwedge + rate_sum, |
| 8549 | dist_sum); |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 8550 | if (rd >= best_interintra_rd_wedge) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8551 | tmp_mv.as_int = cur_mv[0].as_int; |
| 8552 | tmp_rate_mv = rate_mv; |
| 8553 | } |
| 8554 | } else { |
| 8555 | tmp_mv.as_int = cur_mv[0].as_int; |
| 8556 | tmp_rate_mv = rate_mv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8557 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8558 | } |
| 8559 | // Evaluate closer to true rd |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8560 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8561 | rd = |
| 8562 | estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 8563 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 8564 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8565 | rd = RDCOST(x->rdmult, rmode + tmp_rate_mv + rwedge + rate_sum, |
| 8566 | dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8567 | best_interintra_rd_wedge = rd; |
| 8568 | if (best_interintra_rd_wedge < best_interintra_rd_nowedge) { |
| 8569 | mbmi->use_wedge_interintra = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8570 | mbmi->mv[0].as_int = tmp_mv.as_int; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 8571 | rd_stats->rate += tmp_rate_mv - rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8572 | rate_mv = tmp_rate_mv; |
| 8573 | } else { |
| 8574 | mbmi->use_wedge_interintra = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8575 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 8576 | } |
| 8577 | } else { |
| 8578 | mbmi->use_wedge_interintra = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8579 | } |
| 8580 | } |
| 8581 | |
| 8582 | pred_exists = 0; |
Yue Chen | eaf128a | 2017-10-16 17:01:36 -0700 | [diff] [blame] | 8583 | compmode_interintra_cost = x->interintra_cost[size_group_lookup[bsize]][1] + |
| 8584 | interintra_mode_cost[mbmi->interintra_mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8585 | if (is_interintra_wedge_used(bsize)) { |
Yue Chen | eaf128a | 2017-10-16 17:01:36 -0700 | [diff] [blame] | 8586 | compmode_interintra_cost += |
| 8587 | x->wedge_interintra_cost[bsize][mbmi->use_wedge_interintra]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8588 | if (mbmi->use_wedge_interintra) { |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8589 | compmode_interintra_cost += |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8590 | av1_cost_literal(get_interintra_wedge_bits(bsize)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8591 | } |
| 8592 | } |
| 8593 | } else if (is_interintra_allowed(mbmi)) { |
Yue Chen | eaf128a | 2017-10-16 17:01:36 -0700 | [diff] [blame] | 8594 | compmode_interintra_cost = x->interintra_cost[size_group_lookup[bsize]][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8595 | } |
| 8596 | |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 8597 | if (pred_exists == 0) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8598 | int tmp_rate; |
| 8599 | int64_t tmp_dist; |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8600 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, &orig_dst, bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8601 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 8602 | &tmp_dist, &skip_txfm_sb, &skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8603 | rd = RDCOST(x->rdmult, rs + tmp_rate, tmp_dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8604 | } |
| 8605 | |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 8606 | if (!is_comp_pred) |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8607 | args->single_filter[this_mode][refs[0]] = |
| 8608 | av1_extract_interp_filter(mbmi->interp_filters, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8609 | |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 8610 | if (args->modelled_rd != NULL) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8611 | if (is_comp_pred) { |
| 8612 | const int mode0 = compound_ref0_mode(this_mode); |
| 8613 | const int mode1 = compound_ref1_mode(this_mode); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 8614 | const int64_t mrd = AOMMIN(args->modelled_rd[mode0][refs[0]], |
| 8615 | args->modelled_rd[mode1][refs[1]]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8616 | if (rd / 4 * 3 > mrd && ref_best_rd < INT64_MAX) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8617 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8618 | return INT64_MAX; |
| 8619 | } |
| 8620 | } else if (!is_comp_interintra_pred) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 8621 | args->modelled_rd[this_mode][refs[0]] = rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8622 | } |
| 8623 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8624 | |
| 8625 | if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) { |
| 8626 | // if current pred_error modeled rd is substantially more than the best |
| 8627 | // so far, do not bother doing full rd |
| 8628 | if (rd / 2 > ref_best_rd) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8629 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8630 | return INT64_MAX; |
| 8631 | } |
| 8632 | } |
| 8633 | |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8634 | rd_stats->rate += compmode_interintra_cost; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8635 | rate2_bmc_nocoeff += compmode_interintra_cost; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8636 | rd_stats->rate += compmode_interinter_cost; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8637 | |
Sebastien Alaiwan | 1bc94fc | 2017-10-31 10:25:17 +0100 | [diff] [blame] | 8638 | ret_val = motion_mode_rd(cpi, x, bsize, rd_stats, rd_stats_y, rd_stats_uv, |
| 8639 | disable_skip, mode_mv, mi_row, mi_col, args, |
| 8640 | ref_best_rd, refs, rate_mv, single_newmv, |
| 8641 | rate2_bmc_nocoeff, &best_bmc_mbmi, rate_mv_bmc, rs, |
| 8642 | &skip_txfm_sb, &skip_sse_sb, &orig_dst); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8643 | if (ret_val != 0) return ret_val; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 8644 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8645 | return 0; // The rate-distortion cost will be re-calculated by caller. |
| 8646 | } |
| 8647 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8648 | #if CONFIG_INTRABC |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8649 | static int64_t rd_pick_intrabc_mode_sb(const AV1_COMP *cpi, MACROBLOCK *x, |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 8650 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8651 | int64_t best_rd) { |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8652 | const AV1_COMMON *const cm = &cpi->common; |
RogerZhou | ca86546 | 2017-10-05 15:06:27 -0700 | [diff] [blame] | 8653 | if (!av1_allow_intrabc(bsize, cm)) return INT64_MAX; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8654 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8655 | MACROBLOCKD *const xd = &x->e_mbd; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8656 | const TileInfo *tile = &xd->tile; |
| 8657 | MODE_INFO *const mi = xd->mi[0]; |
| 8658 | const int mi_row = -xd->mb_to_top_edge / (8 * MI_SIZE); |
| 8659 | const int mi_col = -xd->mb_to_left_edge / (8 * MI_SIZE); |
| 8660 | const int w = block_size_wide[bsize]; |
| 8661 | const int h = block_size_high[bsize]; |
Hui Su | 8de99a6 | 2017-12-01 12:04:56 -0800 | [diff] [blame] | 8662 | const int sb_row = mi_row >> cm->mib_size_log2; |
| 8663 | const int sb_col = mi_col >> cm->mib_size_log2; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8664 | |
Alex Converse | 44c2bad | 2017-05-11 09:36:10 -0700 | [diff] [blame] | 8665 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 8666 | MV_REFERENCE_FRAME ref_frame = INTRA_FRAME; |
| 8667 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 8668 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 8669 | mbmi_ext->ref_mv_stack[ref_frame], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8670 | mbmi_ext->compound_mode_context, candidates, mi_row, mi_col, |
| 8671 | NULL, NULL, mbmi_ext->mode_context); |
Alex Converse | 44c2bad | 2017-05-11 09:36:10 -0700 | [diff] [blame] | 8672 | |
| 8673 | int_mv nearestmv, nearmv; |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 8674 | #if CONFIG_AMVR |
| 8675 | av1_find_best_ref_mvs(0, candidates, &nearestmv, &nearmv, 0); |
| 8676 | #else |
Alex Converse | 44c2bad | 2017-05-11 09:36:10 -0700 | [diff] [blame] | 8677 | av1_find_best_ref_mvs(0, candidates, &nearestmv, &nearmv); |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 8678 | #endif |
Alex Converse | 44c2bad | 2017-05-11 09:36:10 -0700 | [diff] [blame] | 8679 | |
| 8680 | int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv; |
| 8681 | if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col); |
Hui Su | 1e6bf6b | 2017-11-02 13:00:29 -0700 | [diff] [blame] | 8682 | // Ref DV should not have sub-pel. |
| 8683 | assert((dv_ref.as_mv.col & 7) == 0); |
| 8684 | assert((dv_ref.as_mv.row & 7) == 0); |
Alex Converse | 44c2bad | 2017-05-11 09:36:10 -0700 | [diff] [blame] | 8685 | mbmi_ext->ref_mvs[INTRA_FRAME][0] = dv_ref; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8686 | |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8687 | struct buf_2d yv12_mb[MAX_MB_PLANE]; |
| 8688 | av1_setup_pred_block(xd, yv12_mb, xd->cur_buf, mi_row, mi_col, NULL, NULL); |
| 8689 | for (int i = 0; i < MAX_MB_PLANE; ++i) { |
| 8690 | xd->plane[i].pre[0] = yv12_mb[i]; |
| 8691 | } |
| 8692 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8693 | enum IntrabcMotionDirection { |
| 8694 | IBC_MOTION_ABOVE, |
| 8695 | IBC_MOTION_LEFT, |
| 8696 | IBC_MOTION_DIRECTIONS |
| 8697 | }; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8698 | |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8699 | MB_MODE_INFO *mbmi = &mi->mbmi; |
| 8700 | MB_MODE_INFO best_mbmi = *mbmi; |
| 8701 | RD_STATS best_rdcost = *rd_cost; |
| 8702 | int best_skip = x->skip; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8703 | |
| 8704 | for (enum IntrabcMotionDirection dir = IBC_MOTION_ABOVE; |
| 8705 | dir < IBC_MOTION_DIRECTIONS; ++dir) { |
| 8706 | const MvLimits tmp_mv_limits = x->mv_limits; |
| 8707 | switch (dir) { |
| 8708 | case IBC_MOTION_ABOVE: |
| 8709 | x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE; |
| 8710 | x->mv_limits.col_max = (tile->mi_col_end - mi_col) * MI_SIZE - w; |
| 8711 | x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE; |
Hui Su | 8de99a6 | 2017-12-01 12:04:56 -0800 | [diff] [blame] | 8712 | x->mv_limits.row_max = (sb_row * cm->mib_size - mi_row) * MI_SIZE - h; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8713 | break; |
| 8714 | case IBC_MOTION_LEFT: |
| 8715 | x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE; |
Hui Su | 8de99a6 | 2017-12-01 12:04:56 -0800 | [diff] [blame] | 8716 | x->mv_limits.col_max = (sb_col * cm->mib_size - mi_col) * MI_SIZE - w; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8717 | // TODO(aconverse@google.com): Minimize the overlap between above and |
| 8718 | // left areas. |
| 8719 | x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE; |
| 8720 | int bottom_coded_mi_edge = |
Hui Su | 8de99a6 | 2017-12-01 12:04:56 -0800 | [diff] [blame] | 8721 | AOMMIN((sb_row + 1) * cm->mib_size, tile->mi_row_end); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8722 | x->mv_limits.row_max = (bottom_coded_mi_edge - mi_row) * MI_SIZE - h; |
| 8723 | break; |
| 8724 | default: assert(0); |
| 8725 | } |
| 8726 | assert(x->mv_limits.col_min >= tmp_mv_limits.col_min); |
| 8727 | assert(x->mv_limits.col_max <= tmp_mv_limits.col_max); |
| 8728 | assert(x->mv_limits.row_min >= tmp_mv_limits.row_min); |
| 8729 | assert(x->mv_limits.row_max <= tmp_mv_limits.row_max); |
| 8730 | av1_set_mv_search_range(&x->mv_limits, &dv_ref.as_mv); |
| 8731 | |
| 8732 | if (x->mv_limits.col_max < x->mv_limits.col_min || |
| 8733 | x->mv_limits.row_max < x->mv_limits.row_min) { |
| 8734 | x->mv_limits = tmp_mv_limits; |
| 8735 | continue; |
| 8736 | } |
| 8737 | |
| 8738 | int step_param = cpi->mv_step_param; |
| 8739 | MV mvp_full = dv_ref.as_mv; |
| 8740 | mvp_full.col >>= 3; |
| 8741 | mvp_full.row >>= 3; |
| 8742 | int sadpb = x->sadperbit16; |
| 8743 | int cost_list[5]; |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 8744 | #if CONFIG_HASH_ME |
RogerZhou | d15e7c1 | 2017-09-26 08:49:28 -0700 | [diff] [blame] | 8745 | int bestsme = av1_full_pixel_search( |
| 8746 | cpi, x, bsize, &mvp_full, step_param, sadpb, |
| 8747 | cond_cost_list(cpi, cost_list), &dv_ref.as_mv, INT_MAX, 1, |
| 8748 | (MI_SIZE * mi_col), (MI_SIZE * mi_row), 1); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 8749 | #else |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8750 | int bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, |
| 8751 | sadpb, cond_cost_list(cpi, cost_list), |
| 8752 | &dv_ref.as_mv, INT_MAX, 1); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 8753 | #endif |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8754 | |
| 8755 | x->mv_limits = tmp_mv_limits; |
| 8756 | if (bestsme == INT_MAX) continue; |
| 8757 | mvp_full = x->best_mv.as_mv; |
| 8758 | MV dv = {.row = mvp_full.row * 8, .col = mvp_full.col * 8 }; |
| 8759 | if (mv_check_bounds(&x->mv_limits, &dv)) continue; |
Hui Su | 64463e7 | 2017-11-06 12:36:00 -0800 | [diff] [blame] | 8760 | if (!av1_is_dv_valid(dv, tile, mi_row, mi_col, bsize, cm->mib_size_log2)) |
| 8761 | continue; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8762 | |
Hui Su | 1e6bf6b | 2017-11-02 13:00:29 -0700 | [diff] [blame] | 8763 | // DV should not have sub-pel. |
| 8764 | assert((dv.col & 7) == 0); |
| 8765 | assert((dv.row & 7) == 0); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8766 | memset(&mbmi->palette_mode_info, 0, sizeof(mbmi->palette_mode_info)); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8767 | mbmi->use_intrabc = 1; |
| 8768 | mbmi->mode = DC_PRED; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 8769 | mbmi->uv_mode = UV_DC_PRED; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8770 | mbmi->mv[0].as_mv = dv; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8771 | mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8772 | mbmi->skip = 0; |
| 8773 | x->skip = 0; |
| 8774 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize); |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8775 | |
Hui Su | dfcbfbd | 2017-11-13 12:05:30 -0800 | [diff] [blame] | 8776 | int *dvcost[2] = { (int *)&cpi->dv_cost[0][MV_MAX], |
| 8777 | (int *)&cpi->dv_cost[1][MV_MAX] }; |
Alex Converse | d5d9b6c | 2017-05-23 15:23:45 -0700 | [diff] [blame] | 8778 | // TODO(aconverse@google.com): The full motion field defining discount |
| 8779 | // in MV_COST_WEIGHT is too large. Explore other values. |
Hui Su | dfcbfbd | 2017-11-13 12:05:30 -0800 | [diff] [blame] | 8780 | int rate_mv = av1_mv_bit_cost(&dv, &dv_ref.as_mv, cpi->dv_joint_cost, |
| 8781 | dvcost, MV_COST_WEIGHT_SUB); |
Hui Su | 6c8584f | 2017-09-14 15:37:02 -0700 | [diff] [blame] | 8782 | const int rate_mode = x->intrabc_cost[1]; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8783 | RD_STATS rd_stats, rd_stats_uv; |
| 8784 | av1_subtract_plane(x, bsize, 0); |
Hui Su | 12546aa | 2017-10-13 16:10:01 -0700 | [diff] [blame] | 8785 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 8786 | // Intrabc |
Yue Chen | 25dc070 | 2017-10-18 23:36:06 -0700 | [diff] [blame] | 8787 | select_tx_type_yrd(cpi, x, &rd_stats, bsize, mi_row, mi_col, INT64_MAX); |
Hui Su | 12546aa | 2017-10-13 16:10:01 -0700 | [diff] [blame] | 8788 | } else { |
| 8789 | int idx, idy; |
| 8790 | super_block_yrd(cpi, x, &rd_stats, bsize, INT64_MAX); |
| 8791 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 8792 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 8793 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 8794 | memset(x->blk_skip[0], rd_stats.skip, |
| 8795 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 8796 | } |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8797 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 8798 | av1_merge_rd_stats(&rd_stats, &rd_stats_uv); |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8799 | #if CONFIG_RD_DEBUG |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8800 | mbmi->rd_stats = rd_stats; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8801 | #endif |
| 8802 | |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 8803 | const int skip_ctx = av1_get_skip_context(xd); |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8804 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8805 | RD_STATS rdc_noskip; |
| 8806 | av1_init_rd_stats(&rdc_noskip); |
| 8807 | rdc_noskip.rate = |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 8808 | rate_mode + rate_mv + rd_stats.rate + x->skip_cost[skip_ctx][0]; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8809 | rdc_noskip.dist = rd_stats.dist; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8810 | rdc_noskip.rdcost = RDCOST(x->rdmult, rdc_noskip.rate, rdc_noskip.dist); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8811 | if (rdc_noskip.rdcost < best_rd) { |
| 8812 | best_rd = rdc_noskip.rdcost; |
| 8813 | best_mbmi = *mbmi; |
| 8814 | best_skip = x->skip; |
| 8815 | best_rdcost = rdc_noskip; |
| 8816 | } |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8817 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8818 | x->skip = 1; |
| 8819 | mbmi->skip = 1; |
| 8820 | RD_STATS rdc_skip; |
| 8821 | av1_init_rd_stats(&rdc_skip); |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 8822 | rdc_skip.rate = rate_mode + rate_mv + x->skip_cost[skip_ctx][1]; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8823 | rdc_skip.dist = rd_stats.sse; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8824 | rdc_skip.rdcost = RDCOST(x->rdmult, rdc_skip.rate, rdc_skip.dist); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 8825 | if (rdc_skip.rdcost < best_rd) { |
| 8826 | best_rd = rdc_skip.rdcost; |
| 8827 | best_mbmi = *mbmi; |
| 8828 | best_skip = x->skip; |
| 8829 | best_rdcost = rdc_skip; |
| 8830 | } |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 8831 | } |
| 8832 | *mbmi = best_mbmi; |
| 8833 | *rd_cost = best_rdcost; |
| 8834 | x->skip = best_skip; |
| 8835 | return best_rd; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8836 | } |
| 8837 | #endif // CONFIG_INTRABC |
| 8838 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8839 | void av1_rd_pick_intra_mode_sb(const AV1_COMP *cpi, MACROBLOCK *x, |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 8840 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8841 | PICK_MODE_CONTEXT *ctx, int64_t best_rd) { |
| 8842 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8843 | MACROBLOCKD *const xd = &x->e_mbd; |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 8844 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8845 | struct macroblockd_plane *const pd = xd->plane; |
| 8846 | int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0; |
| 8847 | int y_skip = 0, uv_skip = 0; |
| 8848 | int64_t dist_y = 0, dist_uv = 0; |
| 8849 | TX_SIZE max_uv_tx_size; |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 8850 | |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 8851 | (void)cm; |
| 8852 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8853 | ctx->skip = 0; |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 8854 | mbmi->ref_frame[0] = INTRA_FRAME; |
| 8855 | mbmi->ref_frame[1] = NONE_FRAME; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8856 | #if CONFIG_INTRABC |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 8857 | mbmi->use_intrabc = 0; |
| 8858 | mbmi->mv[0].as_int = 0; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8859 | #endif // CONFIG_INTRABC |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8860 | |
Debargha Mukherjee | edced25 | 2017-10-20 00:02:00 -0700 | [diff] [blame] | 8861 | const int64_t intra_yrd = rd_pick_intra_sby_mode( |
| 8862 | cpi, x, &rate_y, &rate_y_tokenonly, &dist_y, &y_skip, bsize, best_rd); |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8863 | |
| 8864 | if (intra_yrd < best_rd) { |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 8865 | #if CONFIG_CFL |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 8866 | // Only store reconstructed luma when there's chroma RDO. When there's no |
| 8867 | // chroma RDO, the reconstructed luma will be stored in encode_superblock(). |
Luc Trudeau | 1e84af5 | 2017-11-25 15:00:28 -0500 | [diff] [blame] | 8868 | xd->cfl.store_y = !x->skip_chroma_rd; |
| 8869 | if (xd->cfl.store_y) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 8870 | // Perform one extra call to txfm_rd_in_plane(), with the values chosen |
| 8871 | // during luma RDO, so we can store reconstructed luma values |
| 8872 | RD_STATS this_rd_stats; |
Luc Trudeau | 32306c2 | 2017-08-14 14:44:26 -0400 | [diff] [blame] | 8873 | txfm_rd_in_plane(x, cpi, &this_rd_stats, INT64_MAX, AOM_PLANE_Y, |
| 8874 | mbmi->sb_type, mbmi->tx_size, |
| 8875 | cpi->sf.use_fast_coef_costing); |
Luc Trudeau | 1e84af5 | 2017-11-25 15:00:28 -0500 | [diff] [blame] | 8876 | xd->cfl.store_y = 0; |
Luc Trudeau | 32306c2 | 2017-08-14 14:44:26 -0400 | [diff] [blame] | 8877 | } |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 8878 | #endif // CONFIG_CFL |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 8879 | max_uv_tx_size = uv_txsize_lookup[bsize][mbmi->tx_size][pd[1].subsampling_x] |
| 8880 | [pd[1].subsampling_y]; |
| 8881 | init_sbuv_mode(mbmi); |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8882 | if (!x->skip_chroma_rd) |
| 8883 | rd_pick_intra_sbuv_mode(cpi, x, &rate_uv, &rate_uv_tokenonly, &dist_uv, |
| 8884 | &uv_skip, bsize, max_uv_tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8885 | |
Rupert Swarbrick | c6cc1f5 | 2017-10-04 11:52:12 +0100 | [diff] [blame] | 8886 | if (y_skip && (uv_skip || x->skip_chroma_rd)) { |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8887 | rd_cost->rate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly + |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 8888 | x->skip_cost[av1_get_skip_context(xd)][1]; |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8889 | rd_cost->dist = dist_y + dist_uv; |
| 8890 | } else { |
| 8891 | rd_cost->rate = |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 8892 | rate_y + rate_uv + x->skip_cost[av1_get_skip_context(xd)][0]; |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8893 | rd_cost->dist = dist_y + dist_uv; |
| 8894 | } |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8895 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8896 | } else { |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8897 | rd_cost->rate = INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8898 | } |
| 8899 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8900 | #if CONFIG_INTRABC |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8901 | if (rd_cost->rate != INT_MAX && rd_cost->rdcost < best_rd) |
| 8902 | best_rd = rd_cost->rdcost; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8903 | if (rd_pick_intrabc_mode_sb(cpi, x, rd_cost, bsize, best_rd) < best_rd) { |
| 8904 | ctx->skip = x->skip; // FIXME where is the proper place to set this?! |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8905 | assert(rd_cost->rate != INT_MAX); |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8906 | } |
| 8907 | #endif |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 8908 | if (rd_cost->rate == INT_MAX) return; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 8909 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8910 | ctx->mic = *xd->mi[0]; |
| 8911 | ctx->mbmi_ext = *x->mbmi_ext; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8912 | } |
| 8913 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8914 | // Do we have an internal image edge (e.g. formatting bars). |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8915 | int av1_internal_image_edge(const AV1_COMP *cpi) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8916 | return (cpi->oxcf.pass == 2) && |
| 8917 | ((cpi->twopass.this_frame_stats.inactive_zone_rows > 0) || |
| 8918 | (cpi->twopass.this_frame_stats.inactive_zone_cols > 0)); |
| 8919 | } |
| 8920 | |
| 8921 | // Checks to see if a super block is on a horizontal image edge. |
| 8922 | // In most cases this is the "real" edge unless there are formatting |
| 8923 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8924 | 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] | 8925 | int top_edge = 0; |
| 8926 | int bottom_edge = cpi->common.mi_rows; |
| 8927 | int is_active_h_edge = 0; |
| 8928 | |
| 8929 | // For two pass account for any formatting bars detected. |
| 8930 | if (cpi->oxcf.pass == 2) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8931 | const TWO_PASS *const twopass = &cpi->twopass; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8932 | |
| 8933 | // The inactive region is specified in MBs not mi units. |
| 8934 | // The image edge is in the following MB row. |
| 8935 | top_edge += (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
| 8936 | |
| 8937 | bottom_edge -= (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8938 | bottom_edge = AOMMAX(top_edge, bottom_edge); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8939 | } |
| 8940 | |
| 8941 | if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) || |
| 8942 | ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) { |
| 8943 | is_active_h_edge = 1; |
| 8944 | } |
| 8945 | return is_active_h_edge; |
| 8946 | } |
| 8947 | |
| 8948 | // Checks to see if a super block is on a vertical image edge. |
| 8949 | // In most cases this is the "real" edge unless there are formatting |
| 8950 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8951 | 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] | 8952 | int left_edge = 0; |
| 8953 | int right_edge = cpi->common.mi_cols; |
| 8954 | int is_active_v_edge = 0; |
| 8955 | |
| 8956 | // For two pass account for any formatting bars detected. |
| 8957 | if (cpi->oxcf.pass == 2) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8958 | const TWO_PASS *const twopass = &cpi->twopass; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8959 | |
| 8960 | // The inactive region is specified in MBs not mi units. |
| 8961 | // The image edge is in the following MB row. |
| 8962 | left_edge += (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
| 8963 | |
| 8964 | right_edge -= (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8965 | right_edge = AOMMAX(left_edge, right_edge); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8966 | } |
| 8967 | |
| 8968 | if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) || |
| 8969 | ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) { |
| 8970 | is_active_v_edge = 1; |
| 8971 | } |
| 8972 | return is_active_v_edge; |
| 8973 | } |
| 8974 | |
| 8975 | // Checks to see if a super block is at the edge of the active image. |
| 8976 | // In most cases this is the "real" edge unless there are formatting |
| 8977 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8978 | 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] | 8979 | return av1_active_h_edge(cpi, mi_row, cpi->common.mib_size) || |
| 8980 | av1_active_v_edge(cpi, mi_col, cpi->common.mib_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8981 | } |
| 8982 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8983 | 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] | 8984 | MACROBLOCKD *const xd = &x->e_mbd; |
| 8985 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8986 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 8987 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 8988 | assert(bsize >= BLOCK_8X8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8989 | int src_stride = x->plane[1].src.stride; |
| 8990 | const uint8_t *const src_u = x->plane[1].src.buf; |
| 8991 | const uint8_t *const src_v = x->plane[2].src.buf; |
| 8992 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 8993 | float centroids[2 * PALETTE_MAX_SIZE]; |
| 8994 | uint8_t *const color_map = xd->plane[1].color_index_map; |
| 8995 | int r, c; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8996 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8997 | const uint16_t *const src_u16 = CONVERT_TO_SHORTPTR(src_u); |
| 8998 | const uint16_t *const src_v16 = CONVERT_TO_SHORTPTR(src_v); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8999 | #endif // CONFIG_HIGHBITDEPTH |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 9000 | int plane_block_width, plane_block_height, rows, cols; |
| 9001 | av1_get_block_dimensions(bsize, 1, xd, &plane_block_width, |
| 9002 | &plane_block_height, &rows, &cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9003 | (void)cpi; |
| 9004 | |
| 9005 | for (r = 0; r < rows; ++r) { |
| 9006 | for (c = 0; c < cols; ++c) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9007 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9008 | if (cpi->common.use_highbitdepth) { |
| 9009 | data[(r * cols + c) * 2] = src_u16[r * src_stride + c]; |
| 9010 | data[(r * cols + c) * 2 + 1] = src_v16[r * src_stride + c]; |
| 9011 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9012 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9013 | data[(r * cols + c) * 2] = src_u[r * src_stride + c]; |
| 9014 | data[(r * cols + c) * 2 + 1] = src_v[r * src_stride + c]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9015 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9016 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9017 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9018 | } |
| 9019 | } |
| 9020 | |
| 9021 | for (r = 1; r < 3; ++r) { |
| 9022 | for (c = 0; c < pmi->palette_size[1]; ++c) { |
| 9023 | centroids[c * 2 + r - 1] = pmi->palette_colors[r * PALETTE_MAX_SIZE + c]; |
| 9024 | } |
| 9025 | } |
| 9026 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9027 | av1_calc_indices(data, centroids, color_map, rows * cols, |
| 9028 | pmi->palette_size[1], 2); |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 9029 | extend_palette_color_map(color_map, cols, rows, plane_block_width, |
| 9030 | plane_block_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9031 | } |
| 9032 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9033 | static void calc_target_weighted_pred(const AV1_COMMON *cm, const MACROBLOCK *x, |
| 9034 | const MACROBLOCKD *xd, int mi_row, |
| 9035 | int mi_col, const uint8_t *above, |
| 9036 | int above_stride, const uint8_t *left, |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 9037 | int left_stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9038 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 9039 | #if CONFIG_EXT_SKIP |
| 9040 | static void estimate_skip_mode_rdcost( |
| 9041 | const AV1_COMP *const cpi, TileDataEnc *tile_data, MACROBLOCK *const x, |
| 9042 | BLOCK_SIZE bsize, int mi_row, int mi_col, |
| 9043 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME], |
| 9044 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]) { |
| 9045 | const AV1_COMMON *const cm = &cpi->common; |
| 9046 | MACROBLOCKD *const xd = &x->e_mbd; |
| 9047 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 9048 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 9049 | |
| 9050 | int *mode_map = tile_data->mode_map[bsize]; |
| 9051 | static const int flag_list[TOTAL_REFS_PER_FRAME] = { 0, |
| 9052 | AOM_LAST_FLAG, |
| 9053 | AOM_LAST2_FLAG, |
| 9054 | AOM_LAST3_FLAG, |
| 9055 | AOM_GOLD_FLAG, |
| 9056 | AOM_BWD_FLAG, |
| 9057 | AOM_ALT2_FLAG, |
| 9058 | AOM_ALT_FLAG }; |
| 9059 | int i; |
| 9060 | |
| 9061 | for (int midx = 0; midx < MAX_MODES; ++midx) { |
| 9062 | const int mode_index = mode_map[midx]; |
| 9063 | x->skip_mode_index_candidate = mode_index; |
| 9064 | |
| 9065 | const MV_REFERENCE_FRAME ref_frame = |
| 9066 | av1_mode_order[mode_index].ref_frame[0]; |
| 9067 | const MV_REFERENCE_FRAME second_ref_frame = |
| 9068 | av1_mode_order[mode_index].ref_frame[1]; |
| 9069 | const int comp_pred = second_ref_frame > INTRA_FRAME; |
| 9070 | |
| 9071 | if (!comp_pred) continue; |
| 9072 | |
| 9073 | const PREDICTION_MODE this_mode = av1_mode_order[mode_index].mode; |
| 9074 | |
| 9075 | if (!(cpi->ref_frame_flags & flag_list[ref_frame])) continue; |
| 9076 | if (comp_pred && !(cpi->ref_frame_flags & flag_list[second_ref_frame])) |
| 9077 | continue; |
| 9078 | // Check whether current refs/mode align with skip_mode |
| 9079 | if (!(ref_frame == (LAST_FRAME + cm->ref_frame_idx_0) && |
| 9080 | second_ref_frame == (LAST_FRAME + cm->ref_frame_idx_1) && |
| 9081 | this_mode == NEAREST_NEARESTMV)) { |
| 9082 | continue; |
| 9083 | } |
| 9084 | |
| 9085 | frame_mv[this_mode][ref_frame].as_int = |
| 9086 | frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int; |
| 9087 | frame_mv[this_mode][second_ref_frame].as_int = |
| 9088 | frame_mv[compound_ref1_mode(this_mode)][second_ref_frame].as_int; |
| 9089 | |
| 9090 | if (frame_mv[this_mode][ref_frame].as_int == INVALID_MV || |
| 9091 | frame_mv[this_mode][second_ref_frame].as_int == INVALID_MV) |
| 9092 | break; |
| 9093 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 9094 | mbmi->mode = this_mode; |
| 9095 | mbmi->uv_mode = UV_DC_PRED; |
| 9096 | mbmi->ref_frame[0] = ref_frame; |
| 9097 | mbmi->ref_frame[1] = second_ref_frame; |
| 9098 | |
| 9099 | // Obtain NEAREST_NEARESTMV. |
| 9100 | { |
| 9101 | for (i = 0; i < 2; ++i) { |
| 9102 | int_mv cur_mv = frame_mv[mbmi->mode][mbmi->ref_frame[i]]; |
| 9103 | clamp_mv2(&cur_mv.as_mv, xd); |
| 9104 | if (mv_check_bounds(&x->mv_limits, &cur_mv.as_mv)) { |
| 9105 | x->skip_mode_rdcost = INT64_MAX; |
| 9106 | break; |
| 9107 | } |
| 9108 | mbmi->mv[i].as_int = cur_mv.as_int; |
| 9109 | } |
| 9110 | if (x->skip_mode_rdcost == INT64_MAX) break; |
| 9111 | |
| 9112 | const uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
| 9113 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
| 9114 | for (i = 0; i < 2; ++i) { |
| 9115 | int_mv cur_mv = |
| 9116 | (i == 0) ? mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv |
| 9117 | : mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 9118 | clamp_mv2(&cur_mv.as_mv, xd); |
| 9119 | if (mv_check_bounds(&x->mv_limits, &cur_mv.as_mv)) { |
| 9120 | x->skip_mode_rdcost = INT64_MAX; |
| 9121 | break; |
| 9122 | } |
| 9123 | mbmi->mv[i].as_int = cur_mv.as_int; |
| 9124 | } |
| 9125 | if (x->skip_mode_rdcost == INT64_MAX) break; |
| 9126 | } |
| 9127 | } |
| 9128 | |
| 9129 | #if CONFIG_FILTER_INTRA |
| 9130 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 9131 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 9132 | #endif // CONFIG_FILTER_INTRA |
| 9133 | mbmi->interintra_mode = (INTERINTRA_MODE)(II_DC_PRED - 1); |
Zoe Liu | 50c810d | 2017-12-05 16:09:37 -0800 | [diff] [blame^] | 9134 | #if CONFIG_JNT_COMP |
| 9135 | mbmi->comp_group_idx = 0; |
| 9136 | mbmi->compound_idx = 1; |
| 9137 | #endif // CONFIG_JNT_COMP |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 9138 | mbmi->interinter_compound_type = COMPOUND_AVERAGE; |
| 9139 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 9140 | mbmi->ref_mv_idx = 0; |
| 9141 | mbmi->skip_mode = mbmi->skip = 1; |
| 9142 | |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 9143 | set_default_interp_filters(mbmi, cm->interp_filter); |
| 9144 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 9145 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 9146 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 9147 | xd->plane[i].pre[0] = yv12_mb[mbmi->ref_frame[0]][i]; |
| 9148 | xd->plane[i].pre[1] = yv12_mb[mbmi->ref_frame[1]][i]; |
| 9149 | } |
| 9150 | |
| 9151 | BUFFER_SET orig_dst; |
| 9152 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 9153 | orig_dst.plane[i] = xd->plane[i].dst.buf; |
| 9154 | orig_dst.stride[i] = xd->plane[i].dst.stride; |
| 9155 | } |
| 9156 | |
| 9157 | // Obtain the rdcost for skip_mode. |
| 9158 | skip_mode_rd(cpi, x, bsize, mi_row, mi_col, &orig_dst); |
| 9159 | break; |
| 9160 | } |
| 9161 | } |
| 9162 | #endif // CONFIG_EXT_SKIP |
| 9163 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9164 | 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] | 9165 | MACROBLOCK *x, int mi_row, int mi_col, |
Sebastien Alaiwan | 0cf54d4 | 2017-10-16 16:10:04 +0200 | [diff] [blame] | 9166 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
| 9167 | PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9168 | const AV1_COMMON *const cm = &cpi->common; |
| 9169 | const RD_OPT *const rd_opt = &cpi->rd; |
| 9170 | const SPEED_FEATURES *const sf = &cpi->sf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9171 | MACROBLOCKD *const xd = &x->e_mbd; |
| 9172 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Hui Su | e87fb23 | 2017-10-05 15:00:15 -0700 | [diff] [blame] | 9173 | const int try_palette = |
| 9174 | av1_allow_palette(cm->allow_screen_content_tools, mbmi->sb_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9175 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 9176 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 9177 | const struct segmentation *const seg = &cm->seg; |
| 9178 | PREDICTION_MODE this_mode; |
| 9179 | MV_REFERENCE_FRAME ref_frame, second_ref_frame; |
| 9180 | unsigned char segment_id = mbmi->segment_id; |
| 9181 | int comp_pred, i, k; |
| 9182 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 9183 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9184 | int_mv single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } }; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 9185 | int single_newmv_rate[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 9186 | int64_t modelled_rd[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Sebastien Alaiwan | 365e644 | 2017-10-16 11:35:00 +0200 | [diff] [blame] | 9187 | static const int flag_list[TOTAL_REFS_PER_FRAME] = { 0, |
| 9188 | AOM_LAST_FLAG, |
| 9189 | AOM_LAST2_FLAG, |
| 9190 | AOM_LAST3_FLAG, |
| 9191 | AOM_GOLD_FLAG, |
| 9192 | AOM_BWD_FLAG, |
| 9193 | AOM_ALT2_FLAG, |
| 9194 | AOM_ALT_FLAG }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9195 | int64_t best_rd = best_rd_so_far; |
| 9196 | int best_rate_y = INT_MAX, best_rate_uv = INT_MAX; |
| 9197 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 9198 | int64_t best_pred_rd[REFERENCE_MODES]; |
| 9199 | MB_MODE_INFO best_mbmode; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 9200 | const int skip_ctx = av1_get_skip_context(xd); |
| 9201 | int rate_skip0 = x->skip_cost[skip_ctx][0]; |
| 9202 | int rate_skip1 = x->skip_cost[skip_ctx][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9203 | int best_mode_skippable = 0; |
| 9204 | int midx, best_mode_index = -1; |
| 9205 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 9206 | #if CONFIG_EXT_COMP_REFS |
| 9207 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME][TOTAL_REFS_PER_FRAME]; |
| 9208 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9209 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 9210 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9211 | aom_prob comp_mode_p; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9212 | int64_t best_intra_rd = INT64_MAX; |
| 9213 | unsigned int best_pred_sse = UINT_MAX; |
| 9214 | PREDICTION_MODE best_intra_mode = DC_PRED; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 9215 | int rate_uv_intra[TX_SIZES_ALL], rate_uv_tokenonly[TX_SIZES_ALL]; |
| 9216 | int64_t dist_uvs[TX_SIZES_ALL]; |
| 9217 | int skip_uvs[TX_SIZES_ALL]; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 9218 | UV_PREDICTION_MODE mode_uv[TX_SIZES_ALL]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 9219 | PALETTE_MODE_INFO pmi_uv[TX_SIZES_ALL]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9220 | #if CONFIG_EXT_INTRA |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 9221 | int8_t uv_angle_delta[TX_SIZES_ALL]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9222 | int is_directional_mode, angle_stats_ready = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9223 | uint8_t directional_mode_skip_mask[INTRA_MODES]; |
| 9224 | #endif // CONFIG_EXT_INTRA |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9225 | const int intra_cost_penalty = av1_get_intra_cost_penalty( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9226 | cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9227 | const int *const intra_mode_cost = x->mbmode_cost[size_group_lookup[bsize]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9228 | int best_skip2 = 0; |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 9229 | uint16_t ref_frame_skip_mask[2] = { 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9230 | uint32_t mode_skip_mask[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 9231 | MV_REFERENCE_FRAME best_single_inter_ref = LAST_FRAME; |
| 9232 | int64_t best_single_inter_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9233 | int mode_skip_start = sf->mode_skip_start + 1; |
| 9234 | const int *const rd_threshes = rd_opt->threshes[segment_id][bsize]; |
| 9235 | const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize]; |
| 9236 | int64_t mode_threshold[MAX_MODES]; |
| 9237 | int *mode_map = tile_data->mode_map[bsize]; |
| 9238 | const int mode_search_skip_flags = sf->mode_search_skip_flags; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 9239 | |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9240 | HandleInterModeArgs args = { |
Sebastien Alaiwan | 1bc94fc | 2017-10-31 10:25:17 +0100 | [diff] [blame] | 9241 | { NULL }, { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }, |
| 9242 | { NULL }, { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }, |
| 9243 | NULL, NULL, |
| 9244 | NULL, { { 0 } }, |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 9245 | }; |
| 9246 | |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 9247 | const int rows = block_size_high[bsize]; |
| 9248 | const int cols = block_size_wide[bsize]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9249 | int palette_ctx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9250 | const MODE_INFO *above_mi = xd->above_mi; |
| 9251 | const MODE_INFO *left_mi = xd->left_mi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9252 | int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 9253 | int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 9254 | int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 9255 | int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9256 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9257 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9258 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 9259 | int len = sizeof(uint16_t); |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 9260 | args.above_pred_buf[0] = CONVERT_TO_BYTEPTR(x->above_pred_buf); |
| 9261 | args.above_pred_buf[1] = |
| 9262 | CONVERT_TO_BYTEPTR(x->above_pred_buf + MAX_SB_SQUARE * len); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9263 | args.above_pred_buf[2] = |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 9264 | CONVERT_TO_BYTEPTR(x->above_pred_buf + 2 * MAX_SB_SQUARE * len); |
| 9265 | args.left_pred_buf[0] = CONVERT_TO_BYTEPTR(x->left_pred_buf); |
| 9266 | args.left_pred_buf[1] = |
| 9267 | CONVERT_TO_BYTEPTR(x->left_pred_buf + MAX_SB_SQUARE * len); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9268 | args.left_pred_buf[2] = |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 9269 | CONVERT_TO_BYTEPTR(x->left_pred_buf + 2 * MAX_SB_SQUARE * len); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9270 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9271 | #endif // CONFIG_HIGHBITDEPTH |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 9272 | args.above_pred_buf[0] = x->above_pred_buf; |
| 9273 | args.above_pred_buf[1] = x->above_pred_buf + MAX_SB_SQUARE; |
| 9274 | args.above_pred_buf[2] = x->above_pred_buf + 2 * MAX_SB_SQUARE; |
| 9275 | args.left_pred_buf[0] = x->left_pred_buf; |
| 9276 | args.left_pred_buf[1] = x->left_pred_buf + MAX_SB_SQUARE; |
| 9277 | args.left_pred_buf[2] = x->left_pred_buf + 2 * MAX_SB_SQUARE; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9278 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9279 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9280 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9281 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9282 | av1_zero(best_mbmode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9283 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9284 | av1_zero(pmi_uv); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 9285 | if (try_palette) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9286 | if (above_mi) |
| 9287 | palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 9288 | if (left_mi) |
| 9289 | palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 9290 | } |
| 9291 | |
Yue Chen | 170678a | 2017-10-17 13:43:10 -0700 | [diff] [blame] | 9292 | estimate_ref_frame_costs(cm, xd, x, segment_id, ref_costs_single, |
| 9293 | ref_costs_comp, &comp_mode_p); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9294 | |
| 9295 | for (i = 0; i < REFERENCE_MODES; ++i) best_pred_rd[i] = INT64_MAX; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 9296 | for (i = 0; i < TX_SIZES_ALL; i++) rate_uv_intra[i] = INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9297 | for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX; |
| 9298 | for (i = 0; i < MB_MODE_COUNT; ++i) { |
| 9299 | for (k = 0; k < TOTAL_REFS_PER_FRAME; ++k) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9300 | args.single_filter[i][k] = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9301 | } |
| 9302 | } |
| 9303 | |
| 9304 | rd_cost->rate = INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9305 | |
| 9306 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 9307 | x->pred_mv_sad[ref_frame] = INT_MAX; |
| 9308 | x->mbmi_ext->mode_context[ref_frame] = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9309 | x->mbmi_ext->compound_mode_context[ref_frame] = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9310 | if (cpi->ref_frame_flags & flag_list[ref_frame]) { |
| 9311 | assert(get_ref_frame_buffer(cpi, ref_frame) != NULL); |
| 9312 | setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col, |
| 9313 | frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb); |
| 9314 | } |
| 9315 | frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 9316 | frame_mv[GLOBALMV][ref_frame].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 9317 | gm_get_motion_vector(&cm->global_motion[ref_frame], |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 9318 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9319 | 0 |
| 9320 | #if CONFIG_AMVR |
| 9321 | , |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 9322 | cm->cur_frame_force_integer_mv |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9323 | #endif |
| 9324 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 9325 | .as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9326 | frame_mv[NEW_NEWMV][ref_frame].as_int = INVALID_MV; |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 9327 | frame_mv[GLOBAL_GLOBALMV][ref_frame].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 9328 | gm_get_motion_vector(&cm->global_motion[ref_frame], |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 9329 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9330 | 0 |
| 9331 | #if CONFIG_AMVR |
| 9332 | , |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 9333 | cm->cur_frame_force_integer_mv |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9334 | #endif |
| 9335 | ) |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 9336 | .as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9337 | } |
| 9338 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9339 | for (; ref_frame < MODE_CTX_REF_FRAMES; ++ref_frame) { |
| 9340 | MODE_INFO *const mi = xd->mi[0]; |
| 9341 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 9342 | x->mbmi_ext->mode_context[ref_frame] = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9343 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 9344 | mbmi_ext->ref_mv_stack[ref_frame], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9345 | mbmi_ext->compound_mode_context, candidates, mi_row, |
| 9346 | mi_col, NULL, NULL, mbmi_ext->mode_context); |
Jingning Han | 731af49 | 2016-11-17 11:53:23 -0800 | [diff] [blame] | 9347 | if (mbmi_ext->ref_mv_count[ref_frame] < 2) { |
| 9348 | MV_REFERENCE_FRAME rf[2]; |
| 9349 | av1_set_ref_frame(rf, ref_frame); |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 9350 | if (mbmi_ext->ref_mvs[rf[0]][0].as_int != |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 9351 | frame_mv[GLOBALMV][rf[0]].as_int || |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 9352 | mbmi_ext->ref_mvs[rf[0]][1].as_int != |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 9353 | frame_mv[GLOBALMV][rf[0]].as_int || |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 9354 | mbmi_ext->ref_mvs[rf[1]][0].as_int != |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 9355 | frame_mv[GLOBALMV][rf[1]].as_int || |
| 9356 | mbmi_ext->ref_mvs[rf[1]][1].as_int != |
| 9357 | frame_mv[GLOBALMV][rf[1]].as_int) |
Jingning Han | 731af49 | 2016-11-17 11:53:23 -0800 | [diff] [blame] | 9358 | mbmi_ext->mode_context[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET); |
| 9359 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9360 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9361 | |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 9362 | av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col); |
Jingning Han | ad586b9 | 2017-05-23 10:24:57 -0700 | [diff] [blame] | 9363 | |
Yue Chen | f7ba647 | 2017-04-19 11:08:58 -0700 | [diff] [blame] | 9364 | if (check_num_overlappable_neighbors(mbmi) && |
| 9365 | is_motion_variation_allowed_bsize(bsize)) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9366 | av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col, |
| 9367 | args.above_pred_buf, dst_width1, |
| 9368 | dst_height1, args.above_pred_stride); |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 9369 | av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9370 | args.left_pred_buf, dst_width2, |
| 9371 | dst_height2, args.left_pred_stride); |
Jingning Han | 91d9a79 | 2017-04-18 12:01:52 -0700 | [diff] [blame] | 9372 | av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row, |
| 9373 | mi_col); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9374 | calc_target_weighted_pred(cm, x, xd, mi_row, mi_col, args.above_pred_buf[0], |
| 9375 | args.above_pred_stride[0], args.left_pred_buf[0], |
| 9376 | args.left_pred_stride[0]); |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 9377 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9378 | |
| 9379 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 9380 | if (!(cpi->ref_frame_flags & flag_list[ref_frame])) { |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 9381 | // Skip checking missing references in both single and compound reference |
| 9382 | // modes. Note that a mode will be skipped iff both reference frames |
| 9383 | // are masked out. |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 9384 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 9385 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9386 | } else { |
| 9387 | for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) { |
| 9388 | // Skip fixed mv modes for poor references |
| 9389 | if ((x->pred_mv_sad[ref_frame] >> 2) > x->pred_mv_sad[i]) { |
| 9390 | mode_skip_mask[ref_frame] |= INTER_NEAREST_NEAR_ZERO; |
| 9391 | break; |
| 9392 | } |
| 9393 | } |
| 9394 | } |
| 9395 | // If the segment reference frame feature is enabled.... |
| 9396 | // then do nothing if the current ref frame is not allowed.. |
| 9397 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && |
| 9398 | get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) { |
| 9399 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 9400 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 9401 | } |
| 9402 | } |
| 9403 | |
| 9404 | // Disable this drop out case if the ref frame |
| 9405 | // segment level feature is enabled for this segment. This is to |
| 9406 | // prevent the possibility that we end up unable to pick any mode. |
| 9407 | if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) { |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 9408 | // Only consider GLOBALMV/ALTREF_FRAME for alt ref frame, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9409 | // unless ARNR filtering is enabled in which case we want |
| 9410 | // an unfiltered alternative. We allow near/nearest as well |
| 9411 | // because they may result in zero-zero MVs but be cheaper. |
| 9412 | 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] | 9413 | int_mv zeromv; |
Sebastien Alaiwan | 365e644 | 2017-10-16 11:35:00 +0200 | [diff] [blame] | 9414 | ref_frame_skip_mask[0] = (1 << LAST_FRAME) | (1 << LAST2_FRAME) | |
| 9415 | (1 << LAST3_FRAME) | (1 << BWDREF_FRAME) | |
| 9416 | (1 << ALTREF2_FRAME) | (1 << GOLDEN_FRAME); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9417 | ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK; |
| 9418 | // TODO(zoeliu): To further explore whether following needs to be done for |
| 9419 | // BWDREF_FRAME as well. |
| 9420 | mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO; |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 9421 | zeromv.as_int = gm_get_motion_vector(&cm->global_motion[ALTREF_FRAME], |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 9422 | cm->allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9423 | mi_col, mi_row, 0 |
| 9424 | #if CONFIG_AMVR |
| 9425 | , |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 9426 | cm->cur_frame_force_integer_mv |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9427 | #endif |
| 9428 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 9429 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9430 | if (frame_mv[NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9431 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEARMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9432 | if (frame_mv[NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9433 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEARESTMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9434 | if (frame_mv[NEAREST_NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9435 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAREST_NEARESTMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9436 | if (frame_mv[NEAR_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9437 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAR_NEARMV); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9438 | } |
| 9439 | } |
| 9440 | |
| 9441 | if (cpi->rc.is_src_frame_alt_ref) { |
| 9442 | if (sf->alt_ref_search_fp) { |
| 9443 | assert(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]); |
| 9444 | mode_skip_mask[ALTREF_FRAME] = 0; |
| 9445 | ref_frame_skip_mask[0] = ~(1 << ALTREF_FRAME); |
| 9446 | ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK; |
| 9447 | } |
| 9448 | } |
| 9449 | |
| 9450 | if (sf->alt_ref_search_fp) |
| 9451 | if (!cm->show_frame && x->pred_mv_sad[GOLDEN_FRAME] < INT_MAX) |
| 9452 | if (x->pred_mv_sad[ALTREF_FRAME] > (x->pred_mv_sad[GOLDEN_FRAME] << 1)) |
| 9453 | mode_skip_mask[ALTREF_FRAME] |= INTER_ALL; |
| 9454 | |
| 9455 | if (sf->adaptive_mode_search) { |
| 9456 | if (cm->show_frame && !cpi->rc.is_src_frame_alt_ref && |
| 9457 | cpi->rc.frames_since_golden >= 3) |
Yaowu Xu | 36bad47 | 2017-05-16 18:29:53 -0700 | [diff] [blame] | 9458 | if ((x->pred_mv_sad[GOLDEN_FRAME] >> 1) > x->pred_mv_sad[LAST_FRAME]) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9459 | mode_skip_mask[GOLDEN_FRAME] |= INTER_ALL; |
| 9460 | } |
| 9461 | |
| 9462 | if (bsize > sf->max_intra_bsize) { |
| 9463 | ref_frame_skip_mask[0] |= (1 << INTRA_FRAME); |
| 9464 | ref_frame_skip_mask[1] |= (1 << INTRA_FRAME); |
| 9465 | } |
| 9466 | |
| 9467 | mode_skip_mask[INTRA_FRAME] |= |
| 9468 | ~(sf->intra_y_mode_mask[max_txsize_lookup[bsize]]); |
| 9469 | |
| 9470 | for (i = 0; i <= LAST_NEW_MV_INDEX; ++i) mode_threshold[i] = 0; |
| 9471 | for (i = LAST_NEW_MV_INDEX + 1; i < MAX_MODES; ++i) |
| 9472 | mode_threshold[i] = ((int64_t)rd_threshes[i] * rd_thresh_freq_fact[i]) >> 5; |
| 9473 | |
| 9474 | midx = sf->schedule_mode_search ? mode_skip_start : 0; |
| 9475 | while (midx > 4) { |
| 9476 | uint8_t end_pos = 0; |
| 9477 | for (i = 5; i < midx; ++i) { |
| 9478 | if (mode_threshold[mode_map[i - 1]] > mode_threshold[mode_map[i]]) { |
| 9479 | uint8_t tmp = mode_map[i]; |
| 9480 | mode_map[i] = mode_map[i - 1]; |
| 9481 | mode_map[i - 1] = tmp; |
| 9482 | end_pos = i; |
| 9483 | } |
| 9484 | } |
| 9485 | midx = end_pos; |
| 9486 | } |
| 9487 | |
| 9488 | if (cpi->sf.tx_type_search.fast_intra_tx_type_search) |
| 9489 | x->use_default_intra_tx_type = 1; |
| 9490 | else |
| 9491 | x->use_default_intra_tx_type = 0; |
| 9492 | |
| 9493 | if (cpi->sf.tx_type_search.fast_inter_tx_type_search) |
| 9494 | x->use_default_inter_tx_type = 1; |
| 9495 | else |
| 9496 | x->use_default_inter_tx_type = 0; |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 9497 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9498 | for (i = 0; i < MB_MODE_COUNT; ++i) |
| 9499 | for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame) |
| 9500 | modelled_rd[i][ref_frame] = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9501 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 9502 | #if CONFIG_EXT_SKIP |
| 9503 | x->skip_mode_rdcost = -1; |
| 9504 | x->skip_mode_index = -1; |
| 9505 | #endif // CONFIG_EXT_SKIP |
| 9506 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9507 | for (midx = 0; midx < MAX_MODES; ++midx) { |
| 9508 | int mode_index; |
| 9509 | int mode_excluded = 0; |
| 9510 | int64_t this_rd = INT64_MAX; |
| 9511 | int disable_skip = 0; |
| 9512 | int compmode_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9513 | int rate2 = 0, rate_y = 0, rate_uv = 0; |
| 9514 | int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0; |
| 9515 | int skippable = 0; |
| 9516 | int this_skip2 = 0; |
| 9517 | int64_t total_sse = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9518 | uint8_t ref_frame_type; |
Yushin Cho | d0b77ac | 2017-10-20 17:33:16 -0700 | [diff] [blame] | 9519 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9520 | mode_index = mode_map[midx]; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 9521 | #if CONFIG_EXT_SKIP |
| 9522 | x->skip_mode_index_candidate = mode_index; |
| 9523 | #endif // CONFIG_EXT_SKIP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9524 | this_mode = av1_mode_order[mode_index].mode; |
| 9525 | ref_frame = av1_mode_order[mode_index].ref_frame[0]; |
| 9526 | second_ref_frame = av1_mode_order[mode_index].ref_frame[1]; |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 9527 | mbmi->ref_mv_idx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9528 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9529 | if (ref_frame > INTRA_FRAME && second_ref_frame == INTRA_FRAME) { |
| 9530 | // Mode must by compatible |
Debargha Mukherjee | 37f6fe6 | 2017-02-10 21:44:13 -0800 | [diff] [blame] | 9531 | if (!is_interintra_allowed_mode(this_mode)) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9532 | if (!is_interintra_allowed_bsize(bsize)) continue; |
| 9533 | } |
| 9534 | |
| 9535 | if (is_inter_compound_mode(this_mode)) { |
| 9536 | frame_mv[this_mode][ref_frame].as_int = |
| 9537 | frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int; |
| 9538 | frame_mv[this_mode][second_ref_frame].as_int = |
| 9539 | frame_mv[compound_ref1_mode(this_mode)][second_ref_frame].as_int; |
| 9540 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9541 | |
| 9542 | // Look at the reference frame of the best mode so far and set the |
| 9543 | // skip mask to look at a subset of the remaining modes. |
| 9544 | if (midx == mode_skip_start && best_mode_index >= 0) { |
| 9545 | switch (best_mbmode.ref_frame[0]) { |
| 9546 | case INTRA_FRAME: break; |
| 9547 | case LAST_FRAME: |
| 9548 | ref_frame_skip_mask[0] |= LAST_FRAME_MODE_MASK; |
| 9549 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 9550 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9551 | case LAST2_FRAME: |
| 9552 | ref_frame_skip_mask[0] |= LAST2_FRAME_MODE_MASK; |
| 9553 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 9554 | break; |
| 9555 | case LAST3_FRAME: |
| 9556 | ref_frame_skip_mask[0] |= LAST3_FRAME_MODE_MASK; |
| 9557 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 9558 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9559 | case GOLDEN_FRAME: |
| 9560 | ref_frame_skip_mask[0] |= GOLDEN_FRAME_MODE_MASK; |
| 9561 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 9562 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9563 | case BWDREF_FRAME: |
| 9564 | ref_frame_skip_mask[0] |= BWDREF_FRAME_MODE_MASK; |
| 9565 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 9566 | break; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 9567 | case ALTREF2_FRAME: |
| 9568 | ref_frame_skip_mask[0] |= ALTREF2_FRAME_MODE_MASK; |
| 9569 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 9570 | break; |
Sebastien Alaiwan | 365e644 | 2017-10-16 11:35:00 +0200 | [diff] [blame] | 9571 | case ALTREF_FRAME: |
| 9572 | ref_frame_skip_mask[0] |= ALTREF_FRAME_MODE_MASK; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9573 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9574 | break; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 9575 | case NONE_FRAME: |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9576 | case TOTAL_REFS_PER_FRAME: |
| 9577 | assert(0 && "Invalid Reference frame"); |
| 9578 | break; |
| 9579 | } |
| 9580 | } |
| 9581 | |
| 9582 | if ((ref_frame_skip_mask[0] & (1 << ref_frame)) && |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9583 | (ref_frame_skip_mask[1] & (1 << AOMMAX(0, second_ref_frame)))) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9584 | continue; |
| 9585 | |
| 9586 | if (mode_skip_mask[ref_frame] & (1 << this_mode)) continue; |
| 9587 | |
| 9588 | // Test best rd so far against threshold for trying this mode. |
| 9589 | if (best_mode_skippable && sf->schedule_mode_search) |
| 9590 | mode_threshold[mode_index] <<= 1; |
| 9591 | |
| 9592 | if (best_rd < mode_threshold[mode_index]) continue; |
| 9593 | |
Yunqing Wang | ff4fa06 | 2017-04-21 10:56:08 -0700 | [diff] [blame] | 9594 | // This is only used in motion vector unit test. |
| 9595 | if (cpi->oxcf.motion_vector_unit_test && ref_frame == INTRA_FRAME) continue; |
| 9596 | |
Zoe Liu | c01dddb | 2017-11-07 08:44:06 -0800 | [diff] [blame] | 9597 | #if !CONFIG_EXT_COMP_REFS // Changes LL bitstream |
Arild Fuldseth (arilfuld) | 3f42908 | 2017-04-28 15:54:28 +0200 | [diff] [blame] | 9598 | if (cpi->oxcf.pass == 0) { |
| 9599 | // Complexity-compression trade-offs |
| 9600 | // if (ref_frame == ALTREF_FRAME) continue; |
| 9601 | // if (ref_frame == BWDREF_FRAME) continue; |
| 9602 | if (second_ref_frame == ALTREF_FRAME) continue; |
| 9603 | // if (second_ref_frame == BWDREF_FRAME) continue; |
| 9604 | } |
Zoe Liu | c01dddb | 2017-11-07 08:44:06 -0800 | [diff] [blame] | 9605 | #endif // !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9606 | comp_pred = second_ref_frame > INTRA_FRAME; |
| 9607 | if (comp_pred) { |
| 9608 | if (!cpi->allow_comp_inter_inter) continue; |
| 9609 | |
| 9610 | // Skip compound inter modes if ARF is not available. |
| 9611 | if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue; |
| 9612 | |
| 9613 | // Do not allow compound prediction if the segment level reference frame |
| 9614 | // feature is in use as in this case there can only be one reference. |
| 9615 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue; |
| 9616 | |
| 9617 | if ((mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) && |
| 9618 | best_mode_index >= 0 && best_mbmode.ref_frame[0] == INTRA_FRAME) |
| 9619 | continue; |
| 9620 | |
| 9621 | mode_excluded = cm->reference_mode == SINGLE_REFERENCE; |
| 9622 | } else { |
Zoe Liu | d4d8b86 | 2017-12-06 10:56:01 -0800 | [diff] [blame] | 9623 | #if CONFIG_REF_ADAPT |
| 9624 | if (ref_frame != INTRA_FRAME) mode_excluded = 0; |
| 9625 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9626 | if (ref_frame != INTRA_FRAME) |
| 9627 | mode_excluded = cm->reference_mode == COMPOUND_REFERENCE; |
Zoe Liu | d4d8b86 | 2017-12-06 10:56:01 -0800 | [diff] [blame] | 9628 | #endif // CONFIG_REF_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9629 | } |
| 9630 | |
| 9631 | if (ref_frame == INTRA_FRAME) { |
| 9632 | if (sf->adaptive_mode_search) |
| 9633 | if ((x->source_variance << num_pels_log2_lookup[bsize]) > best_pred_sse) |
| 9634 | continue; |
| 9635 | |
| 9636 | if (this_mode != DC_PRED) { |
| 9637 | // Disable intra modes other than DC_PRED for blocks with low variance |
| 9638 | // Threshold for intra skipping based on source variance |
| 9639 | // TODO(debargha): Specialize the threshold for super block sizes |
| 9640 | const unsigned int skip_intra_var_thresh = 64; |
| 9641 | if ((mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) && |
| 9642 | x->source_variance < skip_intra_var_thresh) |
| 9643 | continue; |
| 9644 | // Only search the oblique modes if the best so far is |
| 9645 | // one of the neighboring directional modes |
| 9646 | if ((mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) && |
Urvang Joshi | 96d1c0a | 2017-10-10 13:15:32 -0700 | [diff] [blame] | 9647 | (this_mode >= D45_PRED && this_mode <= PAETH_PRED)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9648 | if (best_mode_index >= 0 && best_mbmode.ref_frame[0] > INTRA_FRAME) |
| 9649 | continue; |
| 9650 | } |
| 9651 | if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 9652 | if (conditional_skipintra(this_mode, best_intra_mode)) continue; |
| 9653 | } |
| 9654 | } |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 9655 | } else if (cm->global_motion[ref_frame].wmtype == IDENTITY && |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 9656 | (!comp_pred || |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 9657 | cm->global_motion[second_ref_frame].wmtype == IDENTITY)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9658 | const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, second_ref_frame }; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9659 | if (!check_best_zero_mv(cpi, x, mbmi_ext->mode_context, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9660 | mbmi_ext->compound_mode_context, frame_mv, |
| 9661 | this_mode, ref_frames, bsize, -1, mi_row, mi_col)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9662 | continue; |
| 9663 | } |
| 9664 | |
| 9665 | mbmi->mode = this_mode; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 9666 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9667 | mbmi->ref_frame[0] = ref_frame; |
| 9668 | mbmi->ref_frame[1] = second_ref_frame; |
| 9669 | pmi->palette_size[0] = 0; |
| 9670 | pmi->palette_size[1] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9671 | #if CONFIG_FILTER_INTRA |
| 9672 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 9673 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 9674 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9675 | // Evaluate all sub-pel filters irrespective of whether we can use |
| 9676 | // them for this frame. |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 9677 | |
| 9678 | set_default_interp_filters(mbmi, cm->interp_filter); |
| 9679 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9680 | mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9681 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9682 | |
| 9683 | x->skip = 0; |
| 9684 | set_ref_ptrs(cm, xd, ref_frame, second_ref_frame); |
| 9685 | |
| 9686 | // Select prediction reference frames. |
| 9687 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 9688 | xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; |
| 9689 | if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i]; |
| 9690 | } |
| 9691 | |
Debargha Mukherjee | cb60379 | 2016-10-04 13:10:23 -0700 | [diff] [blame] | 9692 | mbmi->interintra_mode = (INTERINTRA_MODE)(II_DC_PRED - 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9693 | |
Jingning Han | cf842ad | 2017-10-25 12:02:30 -0700 | [diff] [blame] | 9694 | #if CONFIG_FRAME_MARKER |
| 9695 | if (sf->selective_ref_frame) { |
Debargha Mukherjee | 06b40cc | 2017-11-02 13:39:39 -0700 | [diff] [blame] | 9696 | if (sf->selective_ref_frame == 2) { |
| 9697 | if (mbmi->ref_frame[0] == ALTREF2_FRAME || |
| 9698 | mbmi->ref_frame[1] == ALTREF2_FRAME) |
| 9699 | if (cm->cur_frame->alt2_frame_offset < cm->frame_offset) continue; |
| 9700 | if (mbmi->ref_frame[0] == BWDREF_FRAME || |
| 9701 | mbmi->ref_frame[1] == BWDREF_FRAME) |
| 9702 | if (cm->cur_frame->bwd_frame_offset < cm->frame_offset) continue; |
| 9703 | } |
Jingning Han | cf842ad | 2017-10-25 12:02:30 -0700 | [diff] [blame] | 9704 | if (mbmi->ref_frame[0] == LAST3_FRAME || |
| 9705 | mbmi->ref_frame[1] == LAST3_FRAME) |
| 9706 | if (cm->cur_frame->lst3_frame_offset <= cm->cur_frame->gld_frame_offset) |
| 9707 | continue; |
| 9708 | if (mbmi->ref_frame[0] == LAST2_FRAME || |
| 9709 | mbmi->ref_frame[1] == LAST2_FRAME) |
| 9710 | if (cm->cur_frame->lst2_frame_offset <= cm->cur_frame->gld_frame_offset) |
| 9711 | continue; |
| 9712 | } |
Zoe Liu | 77fb5be | 2017-11-02 14:36:19 -0700 | [diff] [blame] | 9713 | |
| 9714 | // One-sided compound is used only when all reference frames are one-sided. |
| 9715 | if (sf->selective_ref_frame && comp_pred && !cpi->all_one_sided_refs) { |
| 9716 | unsigned int ref_offsets[2]; |
| 9717 | for (i = 0; i < 2; ++i) { |
| 9718 | const int buf_idx = cm->frame_refs[mbmi->ref_frame[i] - LAST_FRAME].idx; |
| 9719 | assert(buf_idx >= 0); |
| 9720 | ref_offsets[i] = cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset; |
| 9721 | } |
| 9722 | if ((ref_offsets[0] <= cm->frame_offset && |
| 9723 | ref_offsets[1] <= cm->frame_offset) || |
| 9724 | (ref_offsets[0] > cm->frame_offset && |
| 9725 | ref_offsets[1] > cm->frame_offset)) |
| 9726 | continue; |
| 9727 | } |
| 9728 | #endif // CONFIG_FRAME_MARKER |
Jingning Han | cf842ad | 2017-10-25 12:02:30 -0700 | [diff] [blame] | 9729 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9730 | if (ref_frame == INTRA_FRAME) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 9731 | RD_STATS rd_stats_y; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9732 | TX_SIZE uv_tx; |
| 9733 | struct macroblockd_plane *const pd = &xd->plane[1]; |
| 9734 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 9735 | is_directional_mode = av1_is_directional_mode(mbmi->mode, bsize); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 9736 | if (is_directional_mode && av1_use_angle_delta(bsize)) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 9737 | int rate_dummy; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 9738 | int64_t model_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9739 | if (!angle_stats_ready) { |
| 9740 | const int src_stride = x->plane[0].src.stride; |
| 9741 | const uint8_t *src = x->plane[0].src.buf; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9742 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9743 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 9744 | highbd_angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9745 | directional_mode_skip_mask); |
| 9746 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9747 | #endif // CONFIG_HIGHBITDEPTH |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 9748 | angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9749 | directional_mode_skip_mask); |
| 9750 | angle_stats_ready = 1; |
| 9751 | } |
| 9752 | if (directional_mode_skip_mask[mbmi->mode]) continue; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 9753 | rd_stats_y.rate = INT_MAX; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 9754 | rd_pick_intra_angle_sby(cpi, x, &rate_dummy, &rd_stats_y, bsize, |
| 9755 | intra_mode_cost[mbmi->mode], best_rd, |
| 9756 | &model_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9757 | } else { |
| 9758 | mbmi->angle_delta[0] = 0; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 9759 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9760 | } |
| 9761 | #else |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 9762 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 9763 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 9764 | rate_y = rd_stats_y.rate; |
| 9765 | distortion_y = rd_stats_y.dist; |
| 9766 | skippable = rd_stats_y.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9767 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9768 | #if CONFIG_FILTER_INTRA |
Yue Chen | 18f6c15 | 2017-11-06 11:23:47 -0800 | [diff] [blame] | 9769 | if (mbmi->mode == DC_PRED && !xd->lossless[mbmi->segment_id] && |
| 9770 | av1_filter_intra_allowed_bsize(mbmi->sb_type)) { |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 9771 | RD_STATS rd_stats_y_fi; |
| 9772 | int filter_intra_selected_flag = 0; |
| 9773 | TX_SIZE best_tx_size = mbmi->tx_size; |
| 9774 | TX_TYPE best_tx_type = mbmi->tx_type; |
| 9775 | FILTER_INTRA_MODE best_fi_mode = FILTER_DC_PRED; |
Yue Chen | 95e13e2 | 2017-11-01 23:56:35 -0700 | [diff] [blame] | 9776 | int64_t best_rd_tmp = INT64_MAX; |
| 9777 | if (rate_y != INT_MAX && |
| 9778 | av1_filter_intra_allowed_txsize(best_tx_size)) { |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 9779 | best_rd_tmp = RDCOST(x->rdmult, |
| 9780 | rate_y + x->filter_intra_cost[mbmi->tx_size][0] + |
| 9781 | intra_mode_cost[mbmi->mode], |
| 9782 | distortion_y); |
Yue Chen | 95e13e2 | 2017-11-01 23:56:35 -0700 | [diff] [blame] | 9783 | } |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 9784 | |
| 9785 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 1; |
| 9786 | for (FILTER_INTRA_MODE fi_mode = FILTER_DC_PRED; |
| 9787 | fi_mode < FILTER_INTRA_MODES; ++fi_mode) { |
| 9788 | int this_rate_tmp; |
| 9789 | int64_t this_rd_tmp; |
| 9790 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = fi_mode; |
| 9791 | |
| 9792 | super_block_yrd(cpi, x, &rd_stats_y_fi, bsize, best_rd); |
| 9793 | if (rd_stats_y_fi.rate == INT_MAX) continue; |
| 9794 | |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 9795 | this_rate_tmp = rd_stats_y_fi.rate + |
| 9796 | x->filter_intra_cost[mbmi->tx_size][1] + |
| 9797 | x->filter_intra_mode_cost[0][fi_mode] + |
| 9798 | intra_mode_cost[mbmi->mode]; |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 9799 | this_rd_tmp = RDCOST(x->rdmult, this_rate_tmp, rd_stats_y_fi.dist); |
| 9800 | |
| 9801 | if (this_rd_tmp < best_rd_tmp) { |
| 9802 | best_tx_size = mbmi->tx_size; |
| 9803 | best_tx_type = mbmi->tx_type; |
| 9804 | best_fi_mode = fi_mode; |
| 9805 | rd_stats_y = rd_stats_y_fi; |
| 9806 | rate_y = rd_stats_y_fi.rate; |
| 9807 | distortion_y = rd_stats_y_fi.dist; |
| 9808 | skippable = rd_stats_y_fi.skip; |
| 9809 | filter_intra_selected_flag = 1; |
| 9810 | best_rd_tmp = this_rd_tmp; |
| 9811 | } |
| 9812 | } |
| 9813 | |
| 9814 | mbmi->tx_size = best_tx_size; |
| 9815 | mbmi->tx_type = best_tx_type; |
| 9816 | if (filter_intra_selected_flag) { |
| 9817 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 1; |
| 9818 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = best_fi_mode; |
| 9819 | } else { |
| 9820 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 9821 | } |
| 9822 | } |
| 9823 | #endif |
| 9824 | |
| 9825 | if (rate_y == INT_MAX) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9826 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 9827 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][pd->subsampling_x] |
| 9828 | [pd->subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9829 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 9830 | choose_intra_uv_mode(cpi, x, bsize, uv_tx, &rate_uv_intra[uv_tx], |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 9831 | &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx], |
| 9832 | &skip_uvs[uv_tx], &mode_uv[uv_tx]); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 9833 | if (try_palette) pmi_uv[uv_tx] = *pmi; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9834 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9835 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9836 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
| 9837 | #endif // CONFIG_EXT_INTRA |
| 9838 | } |
| 9839 | |
| 9840 | rate_uv = rate_uv_tokenonly[uv_tx]; |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 9841 | distortion_uv = dist_uvs[uv_tx]; |
| 9842 | skippable = skippable && skip_uvs[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9843 | mbmi->uv_mode = mode_uv[uv_tx]; |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 9844 | if (try_palette) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9845 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
| 9846 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 9847 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 9848 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
| 9849 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 9850 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9851 | #if CONFIG_EXT_INTRA |
| 9852 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9853 | #endif // CONFIG_EXT_INTRA |
| 9854 | |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 9855 | rate2 = rate_y + intra_mode_cost[mbmi->mode]; |
| 9856 | if (!x->skip_chroma_rd) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9857 | rate2 += rate_uv + x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode]; |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 9858 | |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 9859 | if (try_palette && mbmi->mode == DC_PRED) { |
Yue Chen | dab2ca9 | 2017-10-16 17:48:48 -0700 | [diff] [blame] | 9860 | rate2 += x->palette_y_mode_cost[bsize - BLOCK_8X8][palette_ctx][0]; |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 9861 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9862 | |
Rupert Swarbrick | fcff0b2 | 2017-10-05 09:26:04 +0100 | [diff] [blame] | 9863 | if (!xd->lossless[mbmi->segment_id] && block_signals_txsize(bsize)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9864 | // super_block_yrd above includes the cost of the tx_size in the |
| 9865 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 9866 | // (prediction granularity), so we account for it in the full rate, |
| 9867 | // not the tokenonly rate. |
Yue Chen | 3dd03e3 | 2017-10-17 15:39:52 -0700 | [diff] [blame] | 9868 | rate_y -= tx_size_cost(cm, x, bsize, mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9869 | } |
| 9870 | #if CONFIG_EXT_INTRA |
| 9871 | if (is_directional_mode) { |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 9872 | if (av1_use_angle_delta(bsize)) { |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 9873 | #if CONFIG_EXT_INTRA_MOD |
| 9874 | rate2 += x->angle_delta_cost[mbmi->mode - V_PRED] |
| 9875 | [mbmi->angle_delta[0] + MAX_ANGLE_DELTA]; |
| 9876 | #else |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 9877 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 9878 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 9879 | #endif // CONFIG_EXT_INTRA_MOD |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 9880 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9881 | } |
Luc Trudeau | f24a35f | 2017-09-11 11:56:33 -0400 | [diff] [blame] | 9882 | if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize) && |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 9883 | av1_use_angle_delta(bsize)) { |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 9884 | #if CONFIG_EXT_INTRA_MOD |
| 9885 | rate2 += x->angle_delta_cost[mbmi->uv_mode - V_PRED] |
| 9886 | [mbmi->angle_delta[1] + MAX_ANGLE_DELTA]; |
| 9887 | #else |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 9888 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 9889 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
Joe Young | 3ca43bf | 2017-10-06 15:12:46 -0700 | [diff] [blame] | 9890 | #endif // CONFIG_EXT_INTRA_MOD |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9891 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9892 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9893 | #if CONFIG_FILTER_INTRA |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 9894 | if (mbmi->mode == DC_PRED && |
| 9895 | av1_filter_intra_allowed_txsize(mbmi->tx_size)) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9896 | rate2 += |
Yue Chen | 4eba69b | 2017-11-09 22:37:35 -0800 | [diff] [blame] | 9897 | x->filter_intra_cost[mbmi->tx_size][mbmi->filter_intra_mode_info |
| 9898 | .use_filter_intra_mode[0]]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9899 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) { |
Yue Chen | 57b8ff6 | 2017-10-10 23:37:31 -0700 | [diff] [blame] | 9900 | rate2 += x->filter_intra_mode_cost[0][mbmi->filter_intra_mode_info |
| 9901 | .filter_intra_mode[0]]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9902 | } |
| 9903 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9904 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | 96d1c0a | 2017-10-10 13:15:32 -0700 | [diff] [blame] | 9905 | if (mbmi->mode != DC_PRED && mbmi->mode != PAETH_PRED) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9906 | rate2 += intra_cost_penalty; |
| 9907 | distortion2 = distortion_y + distortion_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9908 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9909 | int_mv backup_ref_mv[2]; |
| 9910 | |
Debargha Mukherjee | 0f248c4 | 2017-09-07 12:40:18 -0700 | [diff] [blame] | 9911 | if (!is_comp_ref_allowed(bsize) && mbmi->ref_frame[1] > INTRA_FRAME) |
| 9912 | continue; |
Jingning Han | c41a549 | 2017-02-24 11:18:52 -0800 | [diff] [blame] | 9913 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9914 | backup_ref_mv[0] = mbmi_ext->ref_mvs[ref_frame][0]; |
| 9915 | if (comp_pred) backup_ref_mv[1] = mbmi_ext->ref_mvs[second_ref_frame][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9916 | if (second_ref_frame == INTRA_FRAME) { |
| 9917 | if (best_single_inter_ref != ref_frame) continue; |
Debargha Mukherjee | cb60379 | 2016-10-04 13:10:23 -0700 | [diff] [blame] | 9918 | mbmi->interintra_mode = intra_to_interintra_mode[best_intra_mode]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9919 | // TODO(debargha|geza.lore): |
| 9920 | // Should we use ext_intra modes for interintra? |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9921 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9922 | mbmi->angle_delta[0] = 0; |
| 9923 | mbmi->angle_delta[1] = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9924 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9925 | #if CONFIG_FILTER_INTRA |
| 9926 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 9927 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 9928 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9929 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9930 | mbmi->ref_mv_idx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9931 | ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9932 | |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 9933 | if (comp_pred) { |
| 9934 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 9935 | int ref_mv_idx = 0; |
| 9936 | // Special case: NEAR_NEWMV and NEW_NEARMV modes use |
| 9937 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 9938 | // mbmi->ref_mv_idx (like NEWMV) |
| 9939 | if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) |
| 9940 | ref_mv_idx = 1; |
| 9941 | |
| 9942 | if (compound_ref0_mode(mbmi->mode) == NEWMV) { |
| 9943 | int_mv this_mv = |
| 9944 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 9945 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 9946 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 9947 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 9948 | } |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 9949 | if (compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 9950 | int_mv this_mv = |
| 9951 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv; |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 9952 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 9953 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 9954 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 9955 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9956 | } |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 9957 | } else { |
Zoe Liu | 1157d50 | 2017-04-30 07:57:14 -0700 | [diff] [blame] | 9958 | if (mbmi->mode == NEWMV && mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 9959 | int ref; |
| 9960 | for (ref = 0; ref < 1 + comp_pred; ++ref) { |
| 9961 | int_mv this_mv = |
| 9962 | (ref == 0) ? mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv |
| 9963 | : mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 9964 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 9965 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 9966 | mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv; |
| 9967 | } |
| 9968 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9969 | } |
Cheng Chen | ca6958c | 2017-10-10 14:00:50 -0700 | [diff] [blame] | 9970 | #if CONFIG_JNT_COMP |
| 9971 | { |
| 9972 | int cum_rate = rate2; |
| 9973 | MB_MODE_INFO backup_mbmi = *mbmi; |
| 9974 | |
| 9975 | int_mv backup_frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 9976 | int_mv backup_single_newmv[TOTAL_REFS_PER_FRAME]; |
| 9977 | int backup_single_newmv_rate[TOTAL_REFS_PER_FRAME]; |
| 9978 | int64_t backup_modelled_rd[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 9979 | |
| 9980 | memcpy(backup_frame_mv, frame_mv, sizeof(frame_mv)); |
| 9981 | memcpy(backup_single_newmv, single_newmv, sizeof(single_newmv)); |
| 9982 | memcpy(backup_single_newmv_rate, single_newmv_rate, |
| 9983 | sizeof(single_newmv_rate)); |
| 9984 | memcpy(backup_modelled_rd, modelled_rd, sizeof(modelled_rd)); |
| 9985 | |
| 9986 | InterpFilters backup_interp_filters = mbmi->interp_filters; |
| 9987 | |
| 9988 | for (int comp_idx = 0; comp_idx < 1 + has_second_ref(mbmi); |
| 9989 | ++comp_idx) { |
| 9990 | RD_STATS rd_stats, rd_stats_y, rd_stats_uv; |
| 9991 | av1_init_rd_stats(&rd_stats); |
| 9992 | av1_init_rd_stats(&rd_stats_y); |
| 9993 | av1_init_rd_stats(&rd_stats_uv); |
| 9994 | rd_stats.rate = cum_rate; |
| 9995 | |
| 9996 | memcpy(frame_mv, backup_frame_mv, sizeof(frame_mv)); |
| 9997 | memcpy(single_newmv, backup_single_newmv, sizeof(single_newmv)); |
| 9998 | memcpy(single_newmv_rate, backup_single_newmv_rate, |
| 9999 | sizeof(single_newmv_rate)); |
| 10000 | memcpy(modelled_rd, backup_modelled_rd, sizeof(modelled_rd)); |
| 10001 | |
| 10002 | mbmi->interp_filters = backup_interp_filters; |
| 10003 | |
| 10004 | int dummy_disable_skip = 0; |
| 10005 | |
| 10006 | // Point to variables that are maintained between loop iterations |
| 10007 | args.single_newmv = single_newmv; |
| 10008 | args.single_newmv_rate = single_newmv_rate; |
| 10009 | args.modelled_rd = modelled_rd; |
| 10010 | mbmi->compound_idx = comp_idx; |
| 10011 | |
| 10012 | int64_t tmp_rd = handle_inter_mode( |
| 10013 | cpi, x, bsize, &rd_stats, &rd_stats_y, &rd_stats_uv, |
| 10014 | &dummy_disable_skip, frame_mv, mi_row, mi_col, &args, best_rd); |
| 10015 | |
| 10016 | if (tmp_rd < INT64_MAX) { |
| 10017 | if (RDCOST(x->rdmult, rd_stats.rate, rd_stats.dist) < |
| 10018 | RDCOST(x->rdmult, 0, rd_stats.sse)) |
| 10019 | tmp_rd = |
| 10020 | RDCOST(x->rdmult, rd_stats.rate + x->skip_cost[skip_ctx][0], |
| 10021 | rd_stats.dist); |
| 10022 | else |
| 10023 | tmp_rd = RDCOST(x->rdmult, |
| 10024 | rd_stats.rate + x->skip_cost[skip_ctx][1] - |
| 10025 | rd_stats_y.rate - rd_stats_uv.rate, |
| 10026 | rd_stats.sse); |
| 10027 | } |
| 10028 | |
| 10029 | if (tmp_rd < this_rd) { |
| 10030 | this_rd = tmp_rd; |
| 10031 | rate2 = rd_stats.rate; |
| 10032 | skippable = rd_stats.skip; |
| 10033 | distortion2 = rd_stats.dist; |
| 10034 | total_sse = rd_stats.sse; |
| 10035 | rate_y = rd_stats_y.rate; |
| 10036 | rate_uv = rd_stats_uv.rate; |
| 10037 | disable_skip = dummy_disable_skip; |
| 10038 | backup_mbmi = *mbmi; |
Jingning Han | 5d0320f | 2017-11-07 08:49:16 -0800 | [diff] [blame] | 10039 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10040 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 10041 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Cheng Chen | ca6958c | 2017-10-10 14:00:50 -0700 | [diff] [blame] | 10042 | } |
| 10043 | } |
| 10044 | *mbmi = backup_mbmi; |
Jingning Han | 5d0320f | 2017-11-07 08:49:16 -0800 | [diff] [blame] | 10045 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10046 | memcpy(x->blk_skip[i], x->blk_skip_drl[i], |
| 10047 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Cheng Chen | ca6958c | 2017-10-10 14:00:50 -0700 | [diff] [blame] | 10048 | } |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 10049 | #else // CONFIG_JNT_COMP |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10050 | { |
| 10051 | RD_STATS rd_stats, rd_stats_y, rd_stats_uv; |
| 10052 | av1_init_rd_stats(&rd_stats); |
| 10053 | rd_stats.rate = rate2; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10054 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10055 | // Point to variables that are maintained between loop iterations |
| 10056 | args.single_newmv = single_newmv; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10057 | args.single_newmv_rate = single_newmv_rate; |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10058 | args.modelled_rd = modelled_rd; |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 10059 | this_rd = handle_inter_mode(cpi, x, bsize, &rd_stats, &rd_stats_y, |
| 10060 | &rd_stats_uv, &disable_skip, frame_mv, |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10061 | mi_row, mi_col, &args, best_rd); |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10062 | rate2 = rd_stats.rate; |
| 10063 | skippable = rd_stats.skip; |
| 10064 | distortion2 = rd_stats.dist; |
| 10065 | total_sse = rd_stats.sse; |
| 10066 | rate_y = rd_stats_y.rate; |
| 10067 | rate_uv = rd_stats_uv.rate; |
| 10068 | } |
Cheng Chen | ca6958c | 2017-10-10 14:00:50 -0700 | [diff] [blame] | 10069 | #endif // CONFIG_JNT_COMP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10070 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 10071 | // TODO(jingning): This needs some refactoring to improve code quality |
| 10072 | // and reduce redundant steps. |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10073 | if ((have_nearmv_in_inter_mode(mbmi->mode) && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10074 | mbmi_ext->ref_mv_count[ref_frame_type] > 2) || |
| 10075 | ((mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) && |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 10076 | mbmi_ext->ref_mv_count[ref_frame_type] > 1)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10077 | int_mv backup_mv = frame_mv[NEARMV][ref_frame]; |
| 10078 | MB_MODE_INFO backup_mbmi = *mbmi; |
| 10079 | int backup_skip = x->skip; |
| 10080 | int64_t tmp_ref_rd = this_rd; |
| 10081 | int ref_idx; |
| 10082 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10083 | // TODO(jingning): This should be deprecated shortly. |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10084 | int idx_offset = have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10085 | int ref_set = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10086 | 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] | 10087 | |
| 10088 | uint8_t drl_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10089 | 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] | 10090 | // Dummy |
| 10091 | int_mv backup_fmv[2]; |
| 10092 | backup_fmv[0] = frame_mv[NEWMV][ref_frame]; |
| 10093 | if (comp_pred) backup_fmv[1] = frame_mv[NEWMV][second_ref_frame]; |
| 10094 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10095 | rate2 += (rate2 < INT_MAX ? x->drl_mode_cost0[drl_ctx][0] : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10096 | |
| 10097 | if (this_rd < INT64_MAX) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10098 | if (RDCOST(x->rdmult, rate_y + rate_uv, distortion2) < |
| 10099 | RDCOST(x->rdmult, 0, total_sse)) |
| 10100 | tmp_ref_rd = RDCOST( |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10101 | x->rdmult, rate2 + x->skip_cost[av1_get_skip_context(xd)][0], |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10102 | distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10103 | else |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 10104 | tmp_ref_rd = |
| 10105 | RDCOST(x->rdmult, |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10106 | rate2 + x->skip_cost[av1_get_skip_context(xd)][1] - |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 10107 | rate_y - rate_uv, |
| 10108 | total_sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10109 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10110 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10111 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 10112 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10113 | |
Cheng Chen | ca6958c | 2017-10-10 14:00:50 -0700 | [diff] [blame] | 10114 | #if CONFIG_JNT_COMP |
| 10115 | for (int sidx = 0; sidx < ref_set * (1 + has_second_ref(mbmi)); ++sidx) |
| 10116 | #else |
| 10117 | for (ref_idx = 0; ref_idx < ref_set; ++ref_idx) |
| 10118 | #endif // CONFIG_JNT_COMP |
| 10119 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10120 | int64_t tmp_alt_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10121 | int dummy_disable_skip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10122 | int_mv cur_mv; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10123 | RD_STATS tmp_rd_stats, tmp_rd_stats_y, tmp_rd_stats_uv; |
Cheng Chen | ca6958c | 2017-10-10 14:00:50 -0700 | [diff] [blame] | 10124 | #if CONFIG_JNT_COMP |
| 10125 | ref_idx = sidx; |
| 10126 | if (has_second_ref(mbmi)) ref_idx /= 2; |
| 10127 | mbmi->compound_idx = sidx % 2; |
| 10128 | #endif // CONFIG_JNT_COMP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10129 | |
Yaowu Xu | 5bfbfdf | 2016-11-22 16:43:34 -0800 | [diff] [blame] | 10130 | av1_invalid_rd_stats(&tmp_rd_stats); |
Yushin Cho | c0f6bf2 | 2017-06-09 16:08:02 -0700 | [diff] [blame] | 10131 | |
Jingning Han | 52617b2 | 2017-04-11 12:50:08 -0700 | [diff] [blame] | 10132 | x->skip = 0; |
Yaowu Xu | 5bfbfdf | 2016-11-22 16:43:34 -0800 | [diff] [blame] | 10133 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10134 | mbmi->ref_mv_idx = 1 + ref_idx; |
| 10135 | |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10136 | if (comp_pred) { |
| 10137 | int ref_mv_idx = mbmi->ref_mv_idx; |
| 10138 | // Special case: NEAR_NEWMV and NEW_NEARMV modes use |
| 10139 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 10140 | // mbmi->ref_mv_idx (like NEWMV) |
| 10141 | if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) |
| 10142 | ref_mv_idx = 1 + mbmi->ref_mv_idx; |
| 10143 | |
| 10144 | if (compound_ref0_mode(mbmi->mode) == NEWMV) { |
| 10145 | int_mv this_mv = |
| 10146 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 10147 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10148 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10149 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 10150 | } else if (compound_ref0_mode(mbmi->mode) == NEARESTMV) { |
| 10151 | int_mv this_mv = |
| 10152 | mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 10153 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10154 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10155 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 10156 | } |
| 10157 | |
| 10158 | if (compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 10159 | int_mv this_mv = |
| 10160 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv; |
| 10161 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10162 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10163 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 10164 | } else if (compound_ref1_mode(mbmi->mode) == NEARESTMV) { |
| 10165 | int_mv this_mv = |
| 10166 | mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 10167 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10168 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10169 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 10170 | } |
| 10171 | } else { |
Jingning Han | c3ef32a | 2017-12-04 09:56:53 -0800 | [diff] [blame] | 10172 | int_mv this_mv = |
| 10173 | mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx] |
| 10174 | .this_mv; |
| 10175 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10176 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10177 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10178 | } |
| 10179 | |
| 10180 | cur_mv = |
| 10181 | mbmi_ext->ref_mv_stack[ref_frame][mbmi->ref_mv_idx + idx_offset] |
| 10182 | .this_mv; |
| 10183 | clamp_mv2(&cur_mv.as_mv, xd); |
| 10184 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 10185 | if (!mv_check_bounds(&x->mv_limits, &cur_mv.as_mv)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10186 | int_mv dummy_single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } }; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10187 | int dummy_single_newmv_rate[TOTAL_REFS_PER_FRAME] = { 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10188 | |
| 10189 | frame_mv[NEARMV][ref_frame] = cur_mv; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10190 | av1_init_rd_stats(&tmp_rd_stats); |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 10191 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10192 | // Point to variables that are not maintained between iterations |
| 10193 | args.single_newmv = dummy_single_newmv; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10194 | args.single_newmv_rate = dummy_single_newmv_rate; |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10195 | args.modelled_rd = NULL; |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 10196 | tmp_alt_rd = handle_inter_mode( |
| 10197 | cpi, x, bsize, &tmp_rd_stats, &tmp_rd_stats_y, &tmp_rd_stats_uv, |
| 10198 | &dummy_disable_skip, frame_mv, mi_row, mi_col, &args, best_rd); |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10199 | // Prevent pointers from escaping local scope |
| 10200 | args.single_newmv = NULL; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10201 | args.single_newmv_rate = NULL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10202 | } |
| 10203 | |
| 10204 | for (i = 0; i < mbmi->ref_mv_idx; ++i) { |
| 10205 | uint8_t drl1_ctx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10206 | drl1_ctx = av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], |
| 10207 | i + idx_offset); |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10208 | tmp_rd_stats.rate += |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10209 | (tmp_rd_stats.rate < INT_MAX ? x->drl_mode_cost0[drl1_ctx][1] |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10210 | : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10211 | } |
| 10212 | |
| 10213 | if (mbmi_ext->ref_mv_count[ref_frame_type] > |
| 10214 | mbmi->ref_mv_idx + idx_offset + 1 && |
| 10215 | ref_idx < ref_set - 1) { |
| 10216 | uint8_t drl1_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10217 | av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], |
| 10218 | mbmi->ref_mv_idx + idx_offset); |
Yaowu Xu | 83ed6fe | 2016-11-22 11:15:29 -0800 | [diff] [blame] | 10219 | tmp_rd_stats.rate += |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10220 | (tmp_rd_stats.rate < INT_MAX ? x->drl_mode_cost0[drl1_ctx][0] |
Yaowu Xu | 83ed6fe | 2016-11-22 11:15:29 -0800 | [diff] [blame] | 10221 | : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10222 | } |
| 10223 | |
| 10224 | if (tmp_alt_rd < INT64_MAX) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10225 | tmp_alt_rd = |
| 10226 | RDCOST(x->rdmult, tmp_rd_stats.rate, tmp_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10227 | } |
| 10228 | |
| 10229 | if (tmp_ref_rd > tmp_alt_rd) { |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10230 | rate2 = tmp_rd_stats.rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10231 | disable_skip = dummy_disable_skip; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10232 | distortion2 = tmp_rd_stats.dist; |
| 10233 | skippable = tmp_rd_stats.skip; |
| 10234 | rate_y = tmp_rd_stats_y.rate; |
| 10235 | rate_uv = tmp_rd_stats_uv.rate; |
| 10236 | total_sse = tmp_rd_stats.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10237 | this_rd = tmp_alt_rd; |
| 10238 | tmp_ref_rd = tmp_alt_rd; |
| 10239 | backup_mbmi = *mbmi; |
| 10240 | backup_skip = x->skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10241 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10242 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 10243 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10244 | } else { |
| 10245 | *mbmi = backup_mbmi; |
| 10246 | x->skip = backup_skip; |
| 10247 | } |
| 10248 | } |
| 10249 | |
| 10250 | frame_mv[NEARMV][ref_frame] = backup_mv; |
| 10251 | frame_mv[NEWMV][ref_frame] = backup_fmv[0]; |
| 10252 | if (comp_pred) frame_mv[NEWMV][second_ref_frame] = backup_fmv[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10253 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10254 | memcpy(x->blk_skip[i], x->blk_skip_drl[i], |
| 10255 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Cheng Chen | ca6958c | 2017-10-10 14:00:50 -0700 | [diff] [blame] | 10256 | #if CONFIG_JNT_COMP |
| 10257 | *mbmi = backup_mbmi; |
| 10258 | #endif // CONFIG_JNT_COMP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10259 | } |
| 10260 | mbmi_ext->ref_mvs[ref_frame][0] = backup_ref_mv[0]; |
| 10261 | if (comp_pred) mbmi_ext->ref_mvs[second_ref_frame][0] = backup_ref_mv[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10262 | |
| 10263 | if (this_rd == INT64_MAX) continue; |
| 10264 | |
Debargha Mukherjee | 0f248c4 | 2017-09-07 12:40:18 -0700 | [diff] [blame] | 10265 | if (is_comp_ref_allowed(mbmi->sb_type)) |
Jingning Han | c41a549 | 2017-02-24 11:18:52 -0800 | [diff] [blame] | 10266 | compmode_cost = av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10267 | |
| 10268 | if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost; |
| 10269 | } |
| 10270 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10271 | // Estimate the reference frame signaling cost and add it |
| 10272 | // to the rolling cost variable. |
| 10273 | if (comp_pred) { |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10274 | #if CONFIG_EXT_COMP_REFS |
| 10275 | rate2 += ref_costs_comp[ref_frame][second_ref_frame]; |
Sebastien Alaiwan | 365e644 | 2017-10-16 11:35:00 +0200 | [diff] [blame] | 10276 | #else // !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10277 | rate2 += ref_costs_comp[ref_frame]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10278 | rate2 += ref_costs_comp[second_ref_frame]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10279 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10280 | } else { |
| 10281 | rate2 += ref_costs_single[ref_frame]; |
| 10282 | } |
| 10283 | |
Sebastien Alaiwan | 1bc94fc | 2017-10-31 10:25:17 +0100 | [diff] [blame] | 10284 | if (ref_frame == INTRA_FRAME) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10285 | if (skippable) { |
| 10286 | // Back out the coefficient coding costs |
| 10287 | rate2 -= (rate_y + rate_uv); |
| 10288 | rate_y = 0; |
| 10289 | rate_uv = 0; |
| 10290 | // Cost the skip mb case |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10291 | rate2 += x->skip_cost[av1_get_skip_context(xd)][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10292 | } else if (ref_frame != INTRA_FRAME && !xd->lossless[mbmi->segment_id]) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10293 | if (RDCOST(x->rdmult, rate_y + rate_uv + rate_skip0, distortion2) < |
| 10294 | RDCOST(x->rdmult, rate_skip1, total_sse)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10295 | // Add in the cost of the no skip flag. |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10296 | rate2 += x->skip_cost[av1_get_skip_context(xd)][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10297 | } else { |
| 10298 | // FIXME(rbultje) make this work for splitmv also |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10299 | rate2 += x->skip_cost[av1_get_skip_context(xd)][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10300 | distortion2 = total_sse; |
| 10301 | assert(total_sse >= 0); |
| 10302 | rate2 -= (rate_y + rate_uv); |
| 10303 | this_skip2 = 1; |
| 10304 | rate_y = 0; |
| 10305 | rate_uv = 0; |
| 10306 | } |
| 10307 | } else { |
| 10308 | // Add in the cost of the no skip flag. |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10309 | rate2 += x->skip_cost[av1_get_skip_context(xd)][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10310 | } |
| 10311 | |
| 10312 | // Calculate the final RD estimate for this mode. |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10313 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10314 | } else { |
| 10315 | this_skip2 = mbmi->skip; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10316 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10317 | if (this_skip2) { |
| 10318 | rate_y = 0; |
| 10319 | rate_uv = 0; |
| 10320 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10321 | } |
| 10322 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10323 | if (ref_frame == INTRA_FRAME) { |
| 10324 | // Keep record of best intra rd |
| 10325 | if (this_rd < best_intra_rd) { |
| 10326 | best_intra_rd = this_rd; |
| 10327 | best_intra_mode = mbmi->mode; |
| 10328 | } |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 10329 | } else if (second_ref_frame == NONE_FRAME) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10330 | if (this_rd < best_single_inter_rd) { |
| 10331 | best_single_inter_rd = this_rd; |
| 10332 | best_single_inter_ref = mbmi->ref_frame[0]; |
| 10333 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10334 | } |
| 10335 | |
| 10336 | if (!disable_skip && ref_frame == INTRA_FRAME) { |
| 10337 | for (i = 0; i < REFERENCE_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10338 | best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10339 | } |
| 10340 | |
| 10341 | // Did this mode help.. i.e. is it the new best mode |
| 10342 | if (this_rd < best_rd || x->skip) { |
| 10343 | if (!mode_excluded) { |
| 10344 | // Note index of best mode so far |
| 10345 | best_mode_index = mode_index; |
| 10346 | |
| 10347 | if (ref_frame == INTRA_FRAME) { |
| 10348 | /* required for left and above block mv */ |
| 10349 | mbmi->mv[0].as_int = 0; |
| 10350 | } else { |
| 10351 | best_pred_sse = x->pred_sse[ref_frame]; |
| 10352 | } |
| 10353 | |
| 10354 | rd_cost->rate = rate2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10355 | rd_cost->dist = distortion2; |
| 10356 | rd_cost->rdcost = this_rd; |
| 10357 | best_rd = this_rd; |
| 10358 | best_mbmode = *mbmi; |
| 10359 | best_skip2 = this_skip2; |
| 10360 | best_mode_skippable = skippable; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10361 | best_rate_y = |
| 10362 | rate_y + |
| 10363 | x->skip_cost[av1_get_skip_context(xd)][this_skip2 || skippable]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10364 | best_rate_uv = rate_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10365 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10366 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
| 10367 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10368 | } |
| 10369 | } |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 10370 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10371 | /* keep record of best compound/single-only prediction */ |
| 10372 | if (!disable_skip && ref_frame != INTRA_FRAME) { |
| 10373 | int64_t single_rd, hybrid_rd, single_rate, hybrid_rate; |
| 10374 | |
| 10375 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
| 10376 | single_rate = rate2 - compmode_cost; |
| 10377 | hybrid_rate = rate2; |
| 10378 | } else { |
| 10379 | single_rate = rate2; |
| 10380 | hybrid_rate = rate2 + compmode_cost; |
| 10381 | } |
| 10382 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10383 | single_rd = RDCOST(x->rdmult, single_rate, distortion2); |
| 10384 | hybrid_rd = RDCOST(x->rdmult, hybrid_rate, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10385 | |
| 10386 | if (!comp_pred) { |
| 10387 | if (single_rd < best_pred_rd[SINGLE_REFERENCE]) |
| 10388 | best_pred_rd[SINGLE_REFERENCE] = single_rd; |
| 10389 | } else { |
| 10390 | if (single_rd < best_pred_rd[COMPOUND_REFERENCE]) |
| 10391 | best_pred_rd[COMPOUND_REFERENCE] = single_rd; |
| 10392 | } |
| 10393 | if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT]) |
| 10394 | best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd; |
| 10395 | } |
| 10396 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10397 | if (x->skip && !comp_pred) break; |
| 10398 | } |
| 10399 | |
| 10400 | if (xd->lossless[mbmi->segment_id] == 0 && best_mode_index >= 0 && |
| 10401 | ((sf->tx_type_search.fast_inter_tx_type_search == 1 && |
| 10402 | is_inter_mode(best_mbmode.mode)) || |
| 10403 | (sf->tx_type_search.fast_intra_tx_type_search == 1 && |
| 10404 | !is_inter_mode(best_mbmode.mode)))) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10405 | int skip_blk = 0; |
| 10406 | RD_STATS rd_stats_y, rd_stats_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10407 | |
| 10408 | x->use_default_inter_tx_type = 0; |
| 10409 | x->use_default_intra_tx_type = 0; |
| 10410 | |
| 10411 | *mbmi = best_mbmode; |
| 10412 | |
| 10413 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 10414 | |
| 10415 | // Select prediction reference frames. |
| 10416 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 10417 | xd->plane[i].pre[0] = yv12_mb[mbmi->ref_frame[0]][i]; |
| 10418 | if (has_second_ref(mbmi)) |
| 10419 | xd->plane[i].pre[1] = yv12_mb[mbmi->ref_frame[1]][i]; |
| 10420 | } |
| 10421 | |
| 10422 | if (is_inter_mode(mbmi->mode)) { |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 10423 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 10424 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10425 | av1_build_obmc_inter_prediction( |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10426 | cm, xd, mi_row, mi_col, args.above_pred_buf, args.above_pred_stride, |
| 10427 | args.left_pred_buf, args.left_pred_stride); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 10428 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10429 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10430 | if (cm->tx_mode == TX_MODE_SELECT || xd->lossless[mbmi->segment_id]) { |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 10431 | // av1_rd_pick_inter_mode_sb |
Yue Chen | 25dc070 | 2017-10-18 23:36:06 -0700 | [diff] [blame] | 10432 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, mi_row, mi_col, |
| 10433 | INT64_MAX); |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 10434 | assert(rd_stats_y.rate != INT_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10435 | } else { |
| 10436 | int idx, idy; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10437 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10438 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 10439 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 10440 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10441 | memset(x->blk_skip[0], rd_stats_y.skip, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10442 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 10443 | } |
| 10444 | |
Debargha Mukherjee | 5166686 | 2017-10-24 14:29:13 -0700 | [diff] [blame] | 10445 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10446 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10447 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 10448 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10449 | } |
| 10450 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10451 | if (RDCOST(x->rdmult, rd_stats_y.rate + rd_stats_uv.rate, |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10452 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10453 | RDCOST(x->rdmult, 0, (rd_stats_y.sse + rd_stats_uv.sse))) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10454 | skip_blk = 1; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10455 | rd_stats_y.rate = x->skip_cost[av1_get_skip_context(xd)][1]; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10456 | rd_stats_uv.rate = 0; |
| 10457 | rd_stats_y.dist = rd_stats_y.sse; |
| 10458 | rd_stats_uv.dist = rd_stats_uv.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10459 | } else { |
| 10460 | skip_blk = 0; |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10461 | rd_stats_y.rate += x->skip_cost[av1_get_skip_context(xd)][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10462 | } |
| 10463 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10464 | if (RDCOST(x->rdmult, best_rate_y + best_rate_uv, rd_cost->dist) > |
| 10465 | RDCOST(x->rdmult, rd_stats_y.rate + rd_stats_uv.rate, |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10466 | (rd_stats_y.dist + rd_stats_uv.dist))) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10467 | int idx, idy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10468 | best_mbmode.tx_type = mbmi->tx_type; |
| 10469 | best_mbmode.tx_size = mbmi->tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10470 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 10471 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 10472 | best_mbmode.inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 10473 | |
| 10474 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10475 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
| 10476 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 10477 | |
| 10478 | best_mbmode.min_tx_size = mbmi->min_tx_size; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10479 | rd_cost->rate += |
| 10480 | (rd_stats_y.rate + rd_stats_uv.rate - best_rate_y - best_rate_uv); |
| 10481 | rd_cost->dist = rd_stats_y.dist + rd_stats_uv.dist; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10482 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10483 | best_skip2 = skip_blk; |
| 10484 | } |
| 10485 | } |
| 10486 | |
| 10487 | // Only try palette mode when the best mode so far is an intra mode. |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 10488 | if (try_palette && !is_inter_mode(best_mbmode.mode)) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10489 | int rate2 = 0; |
Urvang Joshi | 451e0f2 | 2017-01-31 11:18:31 -0800 | [diff] [blame] | 10490 | int64_t distortion2 = 0, best_rd_palette = best_rd, this_rd, |
| 10491 | best_model_rd_palette = INT64_MAX; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 10492 | int skippable = 0, rate_overhead_palette = 0; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10493 | RD_STATS rd_stats_y; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 10494 | TX_SIZE uv_tx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10495 | uint8_t *const best_palette_color_map = |
| 10496 | x->palette_buffer->best_palette_color_map; |
| 10497 | uint8_t *const color_map = xd->plane[0].color_index_map; |
Hui Su | efb755c | 2017-10-26 16:09:05 -0700 | [diff] [blame] | 10498 | MB_MODE_INFO best_mbmi_palette = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10499 | |
| 10500 | mbmi->mode = DC_PRED; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10501 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10502 | mbmi->ref_frame[0] = INTRA_FRAME; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 10503 | mbmi->ref_frame[1] = NONE_FRAME; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 10504 | rate_overhead_palette = rd_pick_palette_intra_sby( |
Urvang Joshi | 451e0f2 | 2017-01-31 11:18:31 -0800 | [diff] [blame] | 10505 | cpi, x, bsize, palette_ctx, intra_mode_cost[DC_PRED], |
| 10506 | &best_mbmi_palette, best_palette_color_map, &best_rd_palette, |
| 10507 | &best_model_rd_palette, NULL, NULL, NULL, NULL); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 10508 | if (pmi->palette_size[0] == 0) goto PALETTE_EXIT; |
| 10509 | memcpy(color_map, best_palette_color_map, |
| 10510 | rows * cols * sizeof(best_palette_color_map[0])); |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10511 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
| 10512 | if (rd_stats_y.rate == INT_MAX) goto PALETTE_EXIT; |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 10513 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x] |
| 10514 | [xd->plane[1].subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10515 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 10516 | choose_intra_uv_mode(cpi, x, bsize, uv_tx, &rate_uv_intra[uv_tx], |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 10517 | &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx], |
| 10518 | &skip_uvs[uv_tx], &mode_uv[uv_tx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10519 | pmi_uv[uv_tx] = *pmi; |
| 10520 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10521 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
| 10522 | #endif // CONFIG_EXT_INTRA |
| 10523 | } |
| 10524 | mbmi->uv_mode = mode_uv[uv_tx]; |
| 10525 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 10526 | if (pmi->palette_size[1] > 0) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10527 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 10528 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 10529 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 10530 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10531 | #if CONFIG_EXT_INTRA |
| 10532 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10533 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10534 | skippable = rd_stats_y.skip && skip_uvs[uv_tx]; |
| 10535 | distortion2 = rd_stats_y.dist + dist_uvs[uv_tx]; |
| 10536 | rate2 = rd_stats_y.rate + rate_overhead_palette + rate_uv_intra[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10537 | rate2 += ref_costs_single[INTRA_FRAME]; |
| 10538 | |
| 10539 | if (skippable) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10540 | rate2 -= (rd_stats_y.rate + rate_uv_tokenonly[uv_tx]); |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10541 | rate2 += x->skip_cost[av1_get_skip_context(xd)][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10542 | } else { |
Zoe Liu | 1eed2df | 2017-10-16 17:13:15 -0700 | [diff] [blame] | 10543 | rate2 += x->skip_cost[av1_get_skip_context(xd)][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10544 | } |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10545 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10546 | if (this_rd < best_rd) { |
| 10547 | best_mode_index = 3; |
| 10548 | mbmi->mv[0].as_int = 0; |
| 10549 | rd_cost->rate = rate2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10550 | rd_cost->dist = distortion2; |
| 10551 | rd_cost->rdcost = this_rd; |
| 10552 | best_rd = this_rd; |
| 10553 | best_mbmode = *mbmi; |
| 10554 | best_skip2 = 0; |
| 10555 | best_mode_skippable = skippable; |
| 10556 | } |
| 10557 | } |
| 10558 | PALETTE_EXIT: |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10559 | |
| 10560 | #if CONFIG_EXT_SKIP |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10561 | best_mbmode.skip_mode = 0; |
| 10562 | if (cm->skip_mode_flag && |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10563 | !segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && |
| 10564 | is_comp_ref_allowed(bsize)) { |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10565 | // Obtain the rdcost for skip_mode. |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10566 | estimate_skip_mode_rdcost(cpi, tile_data, x, bsize, mi_row, mi_col, |
| 10567 | frame_mv, yv12_mb); |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10568 | |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10569 | if (x->skip_mode_rdcost >= 0 && x->skip_mode_rdcost < INT64_MAX) { |
| 10570 | // Update skip mode rdcost. |
| 10571 | const int skip_mode_ctx = av1_get_skip_mode_context(xd); |
| 10572 | x->skip_mode_rate += x->skip_mode_cost[skip_mode_ctx][1]; |
| 10573 | x->skip_mode_rdcost = |
| 10574 | RDCOST(x->rdmult, x->skip_mode_rate, x->skip_mode_dist); |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10575 | |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10576 | // Compare the use of skip_mode with the best intra/inter mode obtained. |
| 10577 | const int64_t best_intra_inter_mode_cost = |
| 10578 | RDCOST(x->rdmult, rd_cost->rate + x->skip_mode_cost[skip_mode_ctx][0], |
| 10579 | rd_cost->dist); |
| 10580 | |
Zoe Liu | 50c810d | 2017-12-05 16:09:37 -0800 | [diff] [blame^] | 10581 | if (x->skip_mode_rdcost <= best_intra_inter_mode_cost) |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10582 | best_mbmode.skip_mode = 1; |
| 10583 | } |
| 10584 | |
| 10585 | if (best_mbmode.skip_mode) { |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10586 | best_mbmode = *mbmi; |
| 10587 | |
| 10588 | best_mbmode.skip_mode = best_mbmode.skip = 1; |
| 10589 | best_mbmode.mode = NEAREST_NEARESTMV; |
| 10590 | best_mbmode.ref_frame[0] = x->skip_mode_ref_frame[0]; |
| 10591 | best_mbmode.ref_frame[1] = x->skip_mode_ref_frame[1]; |
| 10592 | best_mbmode.mv[0].as_int = x->skip_mode_mv[0].as_int; |
| 10593 | best_mbmode.mv[1].as_int = x->skip_mode_mv[1].as_int; |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10594 | best_mbmode.ref_mv_idx = 0; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10595 | |
| 10596 | // Set up tx_size related variables for skip-specific loop filtering. |
| 10597 | best_mbmode.tx_size = block_signals_txsize(bsize) |
| 10598 | ? tx_size_from_tx_mode(bsize, cm->tx_mode, 1) |
Debargha Mukherjee | e4e18fc | 2017-12-06 23:43:24 -0800 | [diff] [blame] | 10599 | : max_txsize_rect_lookup[1][bsize]; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10600 | { |
| 10601 | const int width = block_size_wide[bsize] >> tx_size_wide_log2[0]; |
| 10602 | const int height = block_size_high[bsize] >> tx_size_high_log2[0]; |
| 10603 | for (int idy = 0; idy < height; ++idy) |
| 10604 | for (int idx = 0; idx < width; ++idx) |
| 10605 | best_mbmode.inter_tx_size[idy >> 1][idx >> 1] = best_mbmode.tx_size; |
| 10606 | } |
| 10607 | best_mbmode.min_tx_size = get_min_tx_size(best_mbmode.tx_size); |
| 10608 | set_txfm_ctxs(best_mbmode.tx_size, xd->n8_w, xd->n8_h, best_mbmode.skip, |
| 10609 | xd); |
| 10610 | |
| 10611 | // Set up color-related variables for skip mode. |
| 10612 | best_mbmode.uv_mode = UV_DC_PRED; |
| 10613 | best_mbmode.palette_mode_info.palette_size[0] = 0; |
| 10614 | best_mbmode.palette_mode_info.palette_size[1] = 0; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10615 | best_mbmode.interintra_mode = (INTERINTRA_MODE)(II_DC_PRED - 1); |
| 10616 | best_mbmode.interinter_compound_type = COMPOUND_AVERAGE; |
| 10617 | best_mbmode.motion_mode = SIMPLE_TRANSLATION; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10618 | #if CONFIG_FILTER_INTRA |
| 10619 | best_mbmode.filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 10620 | best_mbmode.filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 10621 | #endif // CONFIG_FILTER_INTRA |
| 10622 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10623 | set_default_interp_filters(&best_mbmode, cm->interp_filter); |
| 10624 | |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10625 | best_mode_index = x->skip_mode_index; |
| 10626 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10627 | // Update rd_cost |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10628 | rd_cost->rate = x->skip_mode_rate; |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10629 | rd_cost->dist = rd_cost->sse = x->skip_mode_dist; |
| 10630 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10631 | |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10632 | best_rd = rd_cost->rdcost; |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10633 | best_skip2 = 1; |
| 10634 | best_mode_skippable = (x->skip_mode_sse == 0); |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10635 | |
| 10636 | x->skip = 1; |
| 10637 | #if 0 |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10638 | // TODO(zoeliu): To investigate why following cause performance drop. |
| 10639 | for (i = 0; i < MAX_MB_PLANE; ++i) { |
| 10640 | memset(x->blk_skip[i], x->skip, sizeof(uint8_t) * ctx->num_4x4_blk); |
| 10641 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10642 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Zoe Liu | 8a5d343 | 2017-11-30 16:33:44 -0800 | [diff] [blame] | 10643 | } |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10644 | #endif // 0 |
| 10645 | } |
| 10646 | } |
| 10647 | #endif // CONFIG_EXT_SKIP |
| 10648 | |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 10649 | // The inter modes' rate costs are not calculated precisely in some cases. |
| 10650 | // Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and |
| 10651 | // GLOBALMV. Here, checks are added for those cases, and the mode decisions |
| 10652 | // are corrected. |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 10653 | if ((best_mbmode.mode == NEWMV || best_mbmode.mode == NEW_NEWMV) |
| 10654 | #if CONFIG_EXT_WARPED_MOTION |
| 10655 | && best_mbmode.motion_mode != WARPED_CAUSAL |
| 10656 | #endif // CONFIG_EXT_WARPED_MOTION |
| 10657 | ) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10658 | const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0], |
| 10659 | best_mbmode.ref_frame[1] }; |
| 10660 | int comp_pred_mode = refs[1] > INTRA_FRAME; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10661 | int_mv zeromv[2]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10662 | const uint8_t rf_type = av1_ref_frame_type(best_mbmode.ref_frame); |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 10663 | zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]], |
| 10664 | cm->allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10665 | mi_col, mi_row, 0 |
| 10666 | #if CONFIG_AMVR |
| 10667 | , |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 10668 | cm->cur_frame_force_integer_mv |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10669 | #endif |
| 10670 | ) |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 10671 | .as_int; |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 10672 | zeromv[1].as_int = comp_pred_mode |
| 10673 | ? gm_get_motion_vector(&cm->global_motion[refs[1]], |
| 10674 | cm->allow_high_precision_mv, |
| 10675 | bsize, mi_col, mi_row, 0 |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10676 | #if CONFIG_AMVR |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 10677 | , |
| 10678 | cm->cur_frame_force_integer_mv |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10679 | #endif |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 10680 | ) |
| 10681 | .as_int |
| 10682 | : 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10683 | if (!comp_pred_mode) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10684 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10685 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10686 | : INT_MAX; |
| 10687 | |
| 10688 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 10689 | int_mv cur_mv = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 10690 | if (cur_mv.as_int == best_mbmode.mv[0].as_int) { |
| 10691 | best_mbmode.mode = NEARMV; |
| 10692 | best_mbmode.ref_mv_idx = i; |
| 10693 | } |
| 10694 | } |
| 10695 | |
| 10696 | if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int) |
| 10697 | best_mbmode.mode = NEARESTMV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10698 | else if (best_mbmode.mv[0].as_int == zeromv[0].as_int) |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 10699 | best_mbmode.mode = GLOBALMV; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10700 | } else { |
| 10701 | int_mv nearestmv[2]; |
| 10702 | int_mv nearmv[2]; |
| 10703 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10704 | if (mbmi_ext->ref_mv_count[rf_type] > 1) { |
| 10705 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][1].this_mv; |
| 10706 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][1].comp_mv; |
| 10707 | } else { |
| 10708 | nearmv[0] = frame_mv[NEARMV][refs[0]]; |
| 10709 | nearmv[1] = frame_mv[NEARMV][refs[1]]; |
| 10710 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10711 | if (mbmi_ext->ref_mv_count[rf_type] >= 1) { |
| 10712 | nearestmv[0] = mbmi_ext->ref_mv_stack[rf_type][0].this_mv; |
| 10713 | nearestmv[1] = mbmi_ext->ref_mv_stack[rf_type][0].comp_mv; |
| 10714 | } else { |
| 10715 | nearestmv[0] = frame_mv[NEARESTMV][refs[0]]; |
| 10716 | nearestmv[1] = frame_mv[NEARESTMV][refs[1]]; |
| 10717 | } |
| 10718 | |
| 10719 | if (nearestmv[0].as_int == best_mbmode.mv[0].as_int && |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10720 | nearestmv[1].as_int == best_mbmode.mv[1].as_int) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10721 | best_mbmode.mode = NEAREST_NEARESTMV; |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10722 | } else { |
| 10723 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
| 10724 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
| 10725 | : INT_MAX; |
| 10726 | |
| 10727 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 10728 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 10729 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][i + 1].comp_mv; |
| 10730 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 10731 | // Try switching to the NEAR_NEARMV mode |
| 10732 | if (nearmv[0].as_int == best_mbmode.mv[0].as_int && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10733 | nearmv[1].as_int == best_mbmode.mv[1].as_int) { |
| 10734 | best_mbmode.mode = NEAR_NEARMV; |
| 10735 | best_mbmode.ref_mv_idx = i; |
| 10736 | } |
| 10737 | } |
| 10738 | |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10739 | if (best_mbmode.mode == NEW_NEWMV && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10740 | best_mbmode.mv[0].as_int == zeromv[0].as_int && |
| 10741 | best_mbmode.mv[1].as_int == zeromv[1].as_int) |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 10742 | best_mbmode.mode = GLOBAL_GLOBALMV; |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10743 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10744 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10745 | } |
| 10746 | |
David Barker | 9620bcd | 2017-03-22 14:46:42 +0000 | [diff] [blame] | 10747 | // Make sure that the ref_mv_idx is only nonzero when we're |
| 10748 | // using a mode which can support ref_mv_idx |
| 10749 | if (best_mbmode.ref_mv_idx != 0 && |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10750 | !(best_mbmode.mode == NEWMV || best_mbmode.mode == NEW_NEWMV || |
Sebastien Alaiwan | 34d5566 | 2017-11-15 09:36:03 +0100 | [diff] [blame] | 10751 | have_nearmv_in_inter_mode(best_mbmode.mode))) { |
David Barker | 9620bcd | 2017-03-22 14:46:42 +0000 | [diff] [blame] | 10752 | best_mbmode.ref_mv_idx = 0; |
| 10753 | } |
| 10754 | |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame] | 10755 | if (best_mbmode.ref_frame[0] > INTRA_FRAME && |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 10756 | best_mbmode.ref_frame[1] <= INTRA_FRAME |
| 10757 | #if CONFIG_EXT_WARPED_MOTION |
| 10758 | && best_mbmode.motion_mode != WARPED_CAUSAL |
| 10759 | #endif // CONFIG_EXT_WARPED_MOTION |
Zoe Liu | f40a957 | 2017-10-13 12:37:19 -0700 | [diff] [blame] | 10760 | #if CONFIG_EXT_SKIP |
| 10761 | && !best_mbmode.skip_mode |
| 10762 | #endif // CONFIG_EXT_SKIP |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 10763 | ) { |
Jingning Han | 731af49 | 2016-11-17 11:53:23 -0800 | [diff] [blame] | 10764 | int8_t ref_frame_type = av1_ref_frame_type(best_mbmode.ref_frame); |
| 10765 | int16_t mode_ctx = mbmi_ext->mode_context[ref_frame_type]; |
David Barker | 68e6e86 | 2016-11-24 15:10:15 +0000 | [diff] [blame] | 10766 | if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) { |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame] | 10767 | int_mv zeromv; |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame] | 10768 | const MV_REFERENCE_FRAME ref = best_mbmode.ref_frame[0]; |
| 10769 | zeromv.as_int = gm_get_motion_vector(&cm->global_motion[ref], |
| 10770 | cm->allow_high_precision_mv, bsize, |
| 10771 | mi_col, mi_row, 0 |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10772 | #if CONFIG_AMVR |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame] | 10773 | , |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 10774 | cm->cur_frame_force_integer_mv |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10775 | #endif |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame] | 10776 | ) |
| 10777 | .as_int; |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame] | 10778 | if (best_mbmode.mv[0].as_int == zeromv.as_int) { |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 10779 | best_mbmode.mode = GLOBALMV; |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10780 | } |
David Barker | 68e6e86 | 2016-11-24 15:10:15 +0000 | [diff] [blame] | 10781 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10782 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10783 | |
| 10784 | if (best_mode_index < 0 || best_rd >= best_rd_so_far) { |
| 10785 | rd_cost->rate = INT_MAX; |
| 10786 | rd_cost->rdcost = INT64_MAX; |
| 10787 | return; |
| 10788 | } |
| 10789 | |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 10790 | assert((cm->interp_filter == SWITCHABLE) || |
| 10791 | (cm->interp_filter == |
| 10792 | av1_extract_interp_filter(best_mbmode.interp_filters, 0)) || |
| 10793 | !is_inter_block(&best_mbmode)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10794 | #if CONFIG_DUAL_FILTER |
| 10795 | assert((cm->interp_filter == SWITCHABLE) || |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 10796 | (cm->interp_filter == |
| 10797 | av1_extract_interp_filter(best_mbmode.interp_filters, 1)) || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10798 | !is_inter_block(&best_mbmode)); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10799 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10800 | |
| 10801 | if (!cpi->rc.is_src_frame_alt_ref) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10802 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
| 10803 | sf->adaptive_rd_thresh, bsize, best_mode_index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10804 | |
| 10805 | // macroblock modes |
| 10806 | *mbmi = best_mbmode; |
| 10807 | x->skip |= best_skip2; |
| 10808 | |
Sebastien Alaiwan | 4879580 | 2017-10-30 12:07:13 +0100 | [diff] [blame] | 10809 | // Note: this section is needed since the mode may have been forced to |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 10810 | // GLOBALMV by the all-zero mode handling of ref-mv. |
| 10811 | if (mbmi->mode == GLOBALMV || mbmi->mode == GLOBAL_GLOBALMV) { |
| 10812 | // Correct the motion mode for GLOBALMV |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 10813 | const MOTION_MODE last_motion_mode_allowed = |
Sebastien Alaiwan | 1f56b8e | 2017-10-31 17:37:16 +0100 | [diff] [blame] | 10814 | motion_mode_allowed(0, xd->global_motion, xd, xd->mi[0]); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 10815 | if (mbmi->motion_mode > last_motion_mode_allowed) |
| 10816 | mbmi->motion_mode = last_motion_mode_allowed; |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 10817 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 10818 | // Correct the interpolation filter for GLOBALMV |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 10819 | if (is_nontrans_global_motion(xd)) { |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 10820 | mbmi->interp_filters = av1_broadcast_interp_filter( |
| 10821 | av1_unswitchable_filter(cm->interp_filter)); |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 10822 | } |
| 10823 | } |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 10824 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10825 | for (i = 0; i < 1 + has_second_ref(mbmi); ++i) { |
| 10826 | if (mbmi->mode != NEWMV) |
| 10827 | mbmi->pred_mv[i].as_int = mbmi->mv[i].as_int; |
| 10828 | else |
| 10829 | mbmi->pred_mv[i].as_int = mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_int; |
| 10830 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10831 | |
| 10832 | for (i = 0; i < REFERENCE_MODES; ++i) { |
| 10833 | if (best_pred_rd[i] == INT64_MAX) |
| 10834 | best_pred_diff[i] = INT_MIN; |
| 10835 | else |
| 10836 | best_pred_diff[i] = best_rd - best_pred_rd[i]; |
| 10837 | } |
| 10838 | |
| 10839 | x->skip |= best_mode_skippable; |
| 10840 | |
| 10841 | assert(best_mode_index >= 0); |
| 10842 | |
| 10843 | store_coding_context(x, ctx, best_mode_index, best_pred_diff, |
| 10844 | best_mode_skippable); |
| 10845 | |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 10846 | if (pmi->palette_size[1] > 0) { |
| 10847 | assert(try_palette); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10848 | restore_uv_color_map(cpi, x); |
| 10849 | } |
| 10850 | } |
| 10851 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10852 | void av1_rd_pick_inter_mode_sb_seg_skip(const AV1_COMP *cpi, |
| 10853 | TileDataEnc *tile_data, MACROBLOCK *x, |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 10854 | int mi_row, int mi_col, |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 10855 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10856 | PICK_MODE_CONTEXT *ctx, |
| 10857 | int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10858 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10859 | MACROBLOCKD *const xd = &x->e_mbd; |
| 10860 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 10861 | unsigned char segment_id = mbmi->segment_id; |
| 10862 | const int comp_pred = 0; |
| 10863 | int i; |
| 10864 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 10865 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10866 | #if CONFIG_EXT_COMP_REFS |
| 10867 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME][TOTAL_REFS_PER_FRAME]; |
| 10868 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10869 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10870 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10871 | aom_prob comp_mode_p; |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 10872 | InterpFilter best_filter = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10873 | int64_t this_rd = INT64_MAX; |
| 10874 | int rate2 = 0; |
| 10875 | const int64_t distortion2 = 0; |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 10876 | (void)mi_row; |
| 10877 | (void)mi_col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10878 | |
Yue Chen | 170678a | 2017-10-17 13:43:10 -0700 | [diff] [blame] | 10879 | estimate_ref_frame_costs(cm, xd, x, segment_id, ref_costs_single, |
| 10880 | ref_costs_comp, &comp_mode_p); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10881 | |
| 10882 | for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX; |
| 10883 | for (i = LAST_FRAME; i < TOTAL_REFS_PER_FRAME; ++i) |
| 10884 | x->pred_mv_sad[i] = INT_MAX; |
| 10885 | |
| 10886 | rd_cost->rate = INT_MAX; |
| 10887 | |
| 10888 | assert(segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)); |
| 10889 | |
| 10890 | mbmi->palette_mode_info.palette_size[0] = 0; |
| 10891 | mbmi->palette_mode_info.palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10892 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10893 | #if CONFIG_FILTER_INTRA |
| 10894 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 10895 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 10896 | #endif // CONFIG_FILTER_INTRA |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 10897 | mbmi->mode = GLOBALMV; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10898 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10899 | mbmi->uv_mode = UV_DC_PRED; |
David Barker | d92f356 | 2017-10-09 17:46:23 +0100 | [diff] [blame] | 10900 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) |
| 10901 | mbmi->ref_frame[0] = get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME); |
| 10902 | else |
| 10903 | mbmi->ref_frame[0] = LAST_FRAME; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 10904 | mbmi->ref_frame[1] = NONE_FRAME; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10905 | mbmi->mv[0].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 10906 | gm_get_motion_vector(&cm->global_motion[mbmi->ref_frame[0]], |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10907 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, 0 |
| 10908 | #if CONFIG_AMVR |
| 10909 | , |
RogerZhou | 10a0380 | 2017-10-26 11:49:48 -0700 | [diff] [blame] | 10910 | cm->cur_frame_force_integer_mv |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10911 | #endif |
| 10912 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10913 | .as_int; |
Jingning Han | 6408895 | 2016-07-11 11:24:24 -0700 | [diff] [blame] | 10914 | mbmi->tx_size = max_txsize_lookup[bsize]; |
Yaowu Xu | ee775b1 | 2016-10-18 10:00:21 -0700 | [diff] [blame] | 10915 | x->skip = 1; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10916 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10917 | mbmi->ref_mv_idx = 0; |
| 10918 | mbmi->pred_mv[0].as_int = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10919 | |
Yue Chen | dead17d | 2017-04-20 16:19:39 -0700 | [diff] [blame] | 10920 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yue Chen | dead17d | 2017-04-20 16:19:39 -0700 | [diff] [blame] | 10921 | av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col); |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 10922 | if (is_motion_variation_allowed_bsize(bsize) && !has_second_ref(mbmi)) { |
| 10923 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 10924 | #if CONFIG_EXT_WARPED_MOTION |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 10925 | int pts_mv[SAMPLES_ARRAY_SIZE], pts_wm[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 10926 | mbmi->num_proj_ref[0] = |
Yunqing Wang | 876a8b0 | 2017-11-13 17:13:27 -0800 | [diff] [blame] | 10927 | findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv, pts_wm); |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 10928 | // Rank the samples by motion vector difference |
| 10929 | if (mbmi->num_proj_ref[0] > 1) |
| 10930 | mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts, |
| 10931 | pts_inref, mbmi->num_proj_ref[0]); |
| 10932 | #else |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 10933 | mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref); |
Yunqing Wang | 97d6a37 | 2017-10-09 14:15:15 -0700 | [diff] [blame] | 10934 | #endif // CONFIG_EXT_WARPED_MOTION |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 10935 | } |
Yue Chen | dead17d | 2017-04-20 16:19:39 -0700 | [diff] [blame] | 10936 | |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 10937 | set_default_interp_filters(mbmi, cm->interp_filter); |
| 10938 | |
| 10939 | if (cm->interp_filter != SWITCHABLE) { |
| 10940 | best_filter = cm->interp_filter; |
| 10941 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10942 | best_filter = EIGHTTAP_REGULAR; |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 10943 | if (av1_is_interp_needed(xd) && av1_is_interp_search_needed(xd) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10944 | x->source_variance >= cpi->sf.disable_filter_search_var_thresh) { |
| 10945 | int rs; |
| 10946 | int best_rs = INT_MAX; |
| 10947 | for (i = 0; i < SWITCHABLE_FILTERS; ++i) { |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 10948 | mbmi->interp_filters = av1_broadcast_interp_filter(i); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10949 | rs = av1_get_switchable_rate(cm, x, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10950 | if (rs < best_rs) { |
| 10951 | best_rs = rs; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 10952 | best_filter = av1_extract_interp_filter(mbmi->interp_filters, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10953 | } |
| 10954 | } |
| 10955 | } |
| 10956 | } |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 10957 | // Set the appropriate filter |
| 10958 | mbmi->interp_filters = av1_broadcast_interp_filter(best_filter); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10959 | rate2 += av1_get_switchable_rate(cm, x, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10960 | |
| 10961 | if (cm->reference_mode == REFERENCE_MODE_SELECT) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10962 | rate2 += av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10963 | |
| 10964 | // Estimate the reference frame signaling cost and add it |
| 10965 | // to the rolling cost variable. |
| 10966 | rate2 += ref_costs_single[LAST_FRAME]; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10967 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10968 | |
| 10969 | rd_cost->rate = rate2; |
| 10970 | rd_cost->dist = distortion2; |
| 10971 | rd_cost->rdcost = this_rd; |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 10972 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10973 | if (this_rd >= best_rd_so_far) { |
| 10974 | rd_cost->rate = INT_MAX; |
| 10975 | rd_cost->rdcost = INT64_MAX; |
| 10976 | return; |
| 10977 | } |
| 10978 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10979 | assert((cm->interp_filter == SWITCHABLE) || |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 10980 | (cm->interp_filter == |
| 10981 | av1_extract_interp_filter(mbmi->interp_filters, 0))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10982 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10983 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 10984 | cpi->sf.adaptive_rd_thresh, bsize, THR_GLOBALMV); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10985 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10986 | av1_zero(best_pred_diff); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10987 | |
Sarah Parker | 2b9ec2e | 2017-10-30 17:34:08 -0700 | [diff] [blame] | 10988 | store_coding_context(x, ctx, THR_GLOBALMV, best_pred_diff, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10989 | } |
| 10990 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 10991 | struct calc_target_weighted_pred_ctxt { |
| 10992 | const MACROBLOCK *x; |
| 10993 | const uint8_t *tmp; |
| 10994 | int tmp_stride; |
| 10995 | int overlap; |
| 10996 | }; |
| 10997 | |
| 10998 | static INLINE void calc_target_weighted_pred_above(MACROBLOCKD *xd, |
| 10999 | int rel_mi_col, |
| 11000 | uint8_t nb_mi_width, |
| 11001 | MODE_INFO *nb_mi, |
| 11002 | void *fun_ctxt) { |
| 11003 | (void)nb_mi; |
| 11004 | |
| 11005 | struct calc_target_weighted_pred_ctxt *ctxt = |
| 11006 | (struct calc_target_weighted_pred_ctxt *)fun_ctxt; |
| 11007 | |
| 11008 | #if CONFIG_HIGHBITDEPTH |
| 11009 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 11010 | #else |
| 11011 | const int is_hbd = 0; |
| 11012 | #endif // CONFIG_HIGHBITDEPTH |
| 11013 | |
| 11014 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 11015 | const uint8_t *const mask1d = av1_get_obmc_mask(ctxt->overlap); |
| 11016 | |
| 11017 | int32_t *wsrc = ctxt->x->wsrc_buf + (rel_mi_col * MI_SIZE); |
| 11018 | int32_t *mask = ctxt->x->mask_buf + (rel_mi_col * MI_SIZE); |
| 11019 | const uint8_t *tmp = ctxt->tmp + rel_mi_col * MI_SIZE; |
| 11020 | |
| 11021 | if (!is_hbd) { |
| 11022 | for (int row = 0; row < ctxt->overlap; ++row) { |
| 11023 | const uint8_t m0 = mask1d[row]; |
| 11024 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11025 | for (int col = 0; col < nb_mi_width * MI_SIZE; ++col) { |
| 11026 | wsrc[col] = m1 * tmp[col]; |
| 11027 | mask[col] = m0; |
| 11028 | } |
| 11029 | wsrc += bw; |
| 11030 | mask += bw; |
| 11031 | tmp += ctxt->tmp_stride; |
| 11032 | } |
| 11033 | #if CONFIG_HIGHBITDEPTH |
| 11034 | } else { |
| 11035 | const uint16_t *tmp16 = CONVERT_TO_SHORTPTR(tmp); |
| 11036 | |
| 11037 | for (int row = 0; row < ctxt->overlap; ++row) { |
| 11038 | const uint8_t m0 = mask1d[row]; |
| 11039 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11040 | for (int col = 0; col < nb_mi_width * MI_SIZE; ++col) { |
| 11041 | wsrc[col] = m1 * tmp16[col]; |
| 11042 | mask[col] = m0; |
| 11043 | } |
| 11044 | wsrc += bw; |
| 11045 | mask += bw; |
| 11046 | tmp16 += ctxt->tmp_stride; |
| 11047 | } |
| 11048 | #endif // CONFIG_HIGHBITDEPTH |
| 11049 | } |
| 11050 | } |
| 11051 | |
| 11052 | static INLINE void calc_target_weighted_pred_left(MACROBLOCKD *xd, |
| 11053 | int rel_mi_row, |
| 11054 | uint8_t nb_mi_height, |
| 11055 | MODE_INFO *nb_mi, |
| 11056 | void *fun_ctxt) { |
| 11057 | (void)nb_mi; |
| 11058 | |
| 11059 | struct calc_target_weighted_pred_ctxt *ctxt = |
| 11060 | (struct calc_target_weighted_pred_ctxt *)fun_ctxt; |
| 11061 | |
| 11062 | #if CONFIG_HIGHBITDEPTH |
| 11063 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 11064 | #else |
| 11065 | const int is_hbd = 0; |
| 11066 | #endif // CONFIG_HIGHBITDEPTH |
| 11067 | |
| 11068 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 11069 | const uint8_t *const mask1d = av1_get_obmc_mask(ctxt->overlap); |
| 11070 | |
| 11071 | int32_t *wsrc = ctxt->x->wsrc_buf + (rel_mi_row * MI_SIZE * bw); |
| 11072 | int32_t *mask = ctxt->x->mask_buf + (rel_mi_row * MI_SIZE * bw); |
| 11073 | const uint8_t *tmp = ctxt->tmp + (rel_mi_row * MI_SIZE * ctxt->tmp_stride); |
| 11074 | |
| 11075 | if (!is_hbd) { |
| 11076 | for (int row = 0; row < nb_mi_height * MI_SIZE; ++row) { |
| 11077 | for (int col = 0; col < ctxt->overlap; ++col) { |
| 11078 | const uint8_t m0 = mask1d[col]; |
| 11079 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11080 | wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 + |
| 11081 | (tmp[col] << AOM_BLEND_A64_ROUND_BITS) * m1; |
| 11082 | mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0; |
| 11083 | } |
| 11084 | wsrc += bw; |
| 11085 | mask += bw; |
| 11086 | tmp += ctxt->tmp_stride; |
| 11087 | } |
| 11088 | #if CONFIG_HIGHBITDEPTH |
| 11089 | } else { |
| 11090 | const uint16_t *tmp16 = CONVERT_TO_SHORTPTR(tmp); |
| 11091 | |
| 11092 | for (int row = 0; row < nb_mi_height * MI_SIZE; ++row) { |
| 11093 | for (int col = 0; col < ctxt->overlap; ++col) { |
| 11094 | const uint8_t m0 = mask1d[col]; |
| 11095 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11096 | wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 + |
| 11097 | (tmp16[col] << AOM_BLEND_A64_ROUND_BITS) * m1; |
| 11098 | mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0; |
| 11099 | } |
| 11100 | wsrc += bw; |
| 11101 | mask += bw; |
| 11102 | tmp16 += ctxt->tmp_stride; |
| 11103 | } |
| 11104 | #endif // CONFIG_HIGHBITDEPTH |
| 11105 | } |
| 11106 | } |
| 11107 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11108 | // This function has a structure similar to av1_build_obmc_inter_prediction |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11109 | // |
| 11110 | // The OBMC predictor is computed as: |
| 11111 | // |
| 11112 | // PObmc(x,y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11113 | // AOM_BLEND_A64(Mh(x), |
| 11114 | // AOM_BLEND_A64(Mv(y), P(x,y), PAbove(x,y)), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11115 | // PLeft(x, y)) |
| 11116 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11117 | // 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] | 11118 | // rounding, this can be written as: |
| 11119 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11120 | // 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] | 11121 | // Mh(x) * Mv(y) * P(x,y) + |
| 11122 | // Mh(x) * Cv(y) * Pabove(x,y) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11123 | // AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11124 | // |
| 11125 | // Where : |
| 11126 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11127 | // Cv(y) = AOM_BLEND_A64_MAX_ALPHA - Mv(y) |
| 11128 | // Ch(y) = AOM_BLEND_A64_MAX_ALPHA - Mh(y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11129 | // |
| 11130 | // This function computes 'wsrc' and 'mask' as: |
| 11131 | // |
| 11132 | // wsrc(x, y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11133 | // 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] | 11134 | // Mh(x) * Cv(y) * Pabove(x,y) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11135 | // AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11136 | // |
| 11137 | // mask(x, y) = Mh(x) * Mv(y) |
| 11138 | // |
| 11139 | // These can then be used to efficiently approximate the error for any |
| 11140 | // predictor P in the context of the provided neighbouring predictors by |
| 11141 | // computing: |
| 11142 | // |
| 11143 | // error(x, y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11144 | // 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] | 11145 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11146 | 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] | 11147 | const MACROBLOCKD *xd, int mi_row, |
| 11148 | int mi_col, const uint8_t *above, |
| 11149 | int above_stride, const uint8_t *left, |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 11150 | int left_stride) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11151 | const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type; |
Jingning Han | ff6ee6a | 2016-12-07 09:55:21 -0800 | [diff] [blame] | 11152 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 11153 | const int bh = xd->n8_h << MI_SIZE_LOG2; |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 11154 | int32_t *mask_buf = x->mask_buf; |
| 11155 | int32_t *wsrc_buf = x->wsrc_buf; |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11156 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11157 | const int src_scale = AOM_BLEND_A64_MAX_ALPHA * AOM_BLEND_A64_MAX_ALPHA; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 11158 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11159 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 11160 | #else |
| 11161 | const int is_hbd = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 11162 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11163 | |
| 11164 | // plane 0 should not be subsampled |
| 11165 | assert(xd->plane[0].subsampling_x == 0); |
| 11166 | assert(xd->plane[0].subsampling_y == 0); |
| 11167 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11168 | av1_zero_array(wsrc_buf, bw * bh); |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11169 | for (int 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] | 11170 | |
| 11171 | // handle above row |
| 11172 | if (xd->up_available) { |
Jingning Han | 440d425 | 2017-07-24 14:07:34 -0700 | [diff] [blame] | 11173 | const int overlap = |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11174 | AOMMIN(block_size_high[bsize], block_size_high[BLOCK_64X64]) >> 1; |
| 11175 | struct calc_target_weighted_pred_ctxt ctxt = { x, above, above_stride, |
| 11176 | overlap }; |
| 11177 | foreach_overlappable_nb_above(cm, (MACROBLOCKD *)xd, mi_col, |
| 11178 | max_neighbor_obmc[b_width_log2_lookup[bsize]], |
| 11179 | calc_target_weighted_pred_above, &ctxt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11180 | } |
| 11181 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11182 | for (int i = 0; i < bw * bh; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11183 | wsrc_buf[i] *= AOM_BLEND_A64_MAX_ALPHA; |
| 11184 | mask_buf[i] *= AOM_BLEND_A64_MAX_ALPHA; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11185 | } |
| 11186 | |
| 11187 | // handle left column |
| 11188 | if (xd->left_available) { |
Jingning Han | 440d425 | 2017-07-24 14:07:34 -0700 | [diff] [blame] | 11189 | const int overlap = |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11190 | AOMMIN(block_size_wide[bsize], block_size_wide[BLOCK_64X64]) >> 1; |
| 11191 | struct calc_target_weighted_pred_ctxt ctxt = { x, left, left_stride, |
| 11192 | overlap }; |
| 11193 | foreach_overlappable_nb_left(cm, (MACROBLOCKD *)xd, mi_row, |
| 11194 | max_neighbor_obmc[b_height_log2_lookup[bsize]], |
| 11195 | calc_target_weighted_pred_left, &ctxt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11196 | } |
| 11197 | |
| 11198 | if (!is_hbd) { |
| 11199 | const uint8_t *src = x->plane[0].src.buf; |
| 11200 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11201 | for (int row = 0; row < bh; ++row) { |
| 11202 | for (int col = 0; col < bw; ++col) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11203 | wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col]; |
| 11204 | } |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11205 | wsrc_buf += bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11206 | src += x->plane[0].src.stride; |
| 11207 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 11208 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11209 | } else { |
| 11210 | const uint16_t *src = CONVERT_TO_SHORTPTR(x->plane[0].src.buf); |
| 11211 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11212 | for (int row = 0; row < bh; ++row) { |
| 11213 | for (int col = 0; col < bw; ++col) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11214 | wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col]; |
| 11215 | } |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11216 | wsrc_buf += bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11217 | src += x->plane[0].src.stride; |
| 11218 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 11219 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11220 | } |
| 11221 | } |