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 | #if CONFIG_WARPED_MOTION |
| 44 | #include "av1/common/warped_motion.h" |
| 45 | #endif // CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 46 | |
Jingning Han | 1aab818 | 2016-06-03 11:09:06 -0700 | [diff] [blame] | 47 | #include "av1/encoder/aq_variance.h" |
Tom Finegan | 17ce8b1 | 2017-02-08 12:46:31 -0800 | [diff] [blame] | 48 | #include "av1/encoder/av1_quantize.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 49 | #include "av1/encoder/cost.h" |
| 50 | #include "av1/encoder/encodemb.h" |
| 51 | #include "av1/encoder/encodemv.h" |
| 52 | #include "av1/encoder/encoder.h" |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 53 | #if CONFIG_LV_MAP |
| 54 | #include "av1/encoder/encodetxb.h" |
| 55 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 56 | #include "av1/encoder/hybrid_fwd_txfm.h" |
| 57 | #include "av1/encoder/mcomp.h" |
| 58 | #include "av1/encoder/palette.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 59 | #include "av1/encoder/ratectrl.h" |
| 60 | #include "av1/encoder/rd.h" |
| 61 | #include "av1/encoder/rdopt.h" |
Debargha Mukherjee | ceebb70 | 2016-10-11 05:26:50 -0700 | [diff] [blame] | 62 | #include "av1/encoder/tokenize.h" |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 63 | #if CONFIG_PVQ |
| 64 | #include "av1/encoder/pvq_encoder.h" |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 65 | #include "av1/common/pvq.h" |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 66 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 67 | #if CONFIG_DUAL_FILTER |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 68 | #define DUAL_FILTER_SET_SIZE (SWITCHABLE_FILTERS * SWITCHABLE_FILTERS) |
Angie Chiang | aadbb02 | 2017-06-01 16:08:03 -0700 | [diff] [blame] | 69 | #if USE_EXTRA_FILTER |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 70 | static const int filter_sets[DUAL_FILTER_SET_SIZE][2] = { |
Angie Chiang | d91ab37 | 2016-11-21 18:16:49 -0800 | [diff] [blame] | 71 | { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 1, 0 }, { 1, 1 }, |
| 72 | { 1, 2 }, { 1, 3 }, { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, |
| 73 | { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 74 | }; |
Angie Chiang | aadbb02 | 2017-06-01 16:08:03 -0700 | [diff] [blame] | 75 | #else // USE_EXTRA_FILTER |
| 76 | static const int filter_sets[DUAL_FILTER_SET_SIZE][2] = { |
| 77 | { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 }, |
| 78 | { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 }, |
| 79 | }; |
| 80 | #endif // USE_EXTRA_FILTER |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 81 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 82 | |
| 83 | #if CONFIG_EXT_REFS |
| 84 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 85 | #define LAST_FRAME_MODE_MASK \ |
| 86 | ((1 << INTRA_FRAME) | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | \ |
| 87 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 88 | (1 << ALTREF_FRAME)) |
| 89 | #define LAST2_FRAME_MODE_MASK \ |
| 90 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST3_FRAME) | \ |
| 91 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 92 | (1 << ALTREF_FRAME)) |
| 93 | #define LAST3_FRAME_MODE_MASK \ |
| 94 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 95 | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 96 | (1 << ALTREF_FRAME)) |
| 97 | #define GOLDEN_FRAME_MODE_MASK \ |
| 98 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 99 | (1 << LAST3_FRAME) | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 100 | (1 << ALTREF_FRAME)) |
| 101 | #define BWDREF_FRAME_MODE_MASK \ |
| 102 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 103 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | (1 << ALTREF2_FRAME) | \ |
| 104 | (1 << ALTREF_FRAME)) |
| 105 | #define ALTREF2_FRAME_MODE_MASK \ |
| 106 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 107 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | \ |
| 108 | (1 << ALTREF_FRAME)) |
| 109 | #define ALTREF_FRAME_MODE_MASK \ |
| 110 | ((1 << INTRA_FRAME) | (1 << LAST_FRAME) | (1 << LAST2_FRAME) | \ |
| 111 | (1 << LAST3_FRAME) | (1 << GOLDEN_FRAME) | (1 << BWDREF_FRAME) | \ |
| 112 | (1 << ALTREF2_FRAME)) |
| 113 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 114 | #else // !CONFIG_EXT_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 115 | |
| 116 | #define LAST_FRAME_MODE_MASK \ |
| 117 | ((1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME) | (1 << INTRA_FRAME)) |
| 118 | #define GOLDEN_FRAME_MODE_MASK \ |
| 119 | ((1 << LAST_FRAME) | (1 << ALTREF_FRAME) | (1 << INTRA_FRAME)) |
| 120 | #define ALTREF_FRAME_MODE_MASK \ |
| 121 | ((1 << LAST_FRAME) | (1 << GOLDEN_FRAME) | (1 << INTRA_FRAME)) |
| 122 | |
| 123 | #endif // CONFIG_EXT_REFS |
| 124 | |
| 125 | #if CONFIG_EXT_REFS |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 126 | #if CONFIG_EXT_COMP_REFS |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 127 | #define SECOND_REF_FRAME_MASK \ |
| 128 | ((1 << ALTREF_FRAME) | (1 << ALTREF2_FRAME) | (1 << BWDREF_FRAME) | \ |
| 129 | (1 << GOLDEN_FRAME) | (1 << LAST2_FRAME) | 0x01) |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 130 | #else // !CONFIG_EXT_COMP_REFS |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 131 | #define SECOND_REF_FRAME_MASK \ |
| 132 | ((1 << ALTREF_FRAME) | (1 << ALTREF2_FRAME) | (1 << BWDREF_FRAME) | 0x01) |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 133 | #endif // CONFIG_EXT_COMP_REFS |
| 134 | #else // !CONFIG_EXT_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 135 | #define SECOND_REF_FRAME_MASK ((1 << ALTREF_FRAME) | 0x01) |
| 136 | #endif // CONFIG_EXT_REFS |
| 137 | |
| 138 | #define MIN_EARLY_TERM_INDEX 3 |
| 139 | #define NEW_MV_DISCOUNT_FACTOR 8 |
| 140 | |
| 141 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 142 | #define ANGLE_SKIP_THRESH 10 |
| 143 | #define FILTER_FAST_SEARCH 1 |
| 144 | #endif // CONFIG_EXT_INTRA |
| 145 | |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 146 | // Setting this to 1 will disable trellis optimization within the |
| 147 | // transform search. Trellis optimization will still be applied |
| 148 | // in the final encode. |
Angie Chiang | 91a1cf9 | 2017-08-20 10:50:31 -0700 | [diff] [blame] | 149 | #ifndef DISABLE_TRELLISQ_SEARCH |
Yushin Cho | ba40b52 | 2017-09-25 10:25:36 -0700 | [diff] [blame] | 150 | #define DISABLE_TRELLISQ_SEARCH 0 |
Angie Chiang | 91a1cf9 | 2017-08-20 10:50:31 -0700 | [diff] [blame] | 151 | #endif |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 152 | |
James Zern | 6793279 | 2017-08-21 11:13:19 -0700 | [diff] [blame] | 153 | static const double ADST_FLIP_SVM[8] = { |
| 154 | /* vertical */ |
| 155 | -6.6623, -2.8062, -3.2531, 3.1671, |
| 156 | /* horizontal */ |
| 157 | -7.7051, -3.2234, -3.6193, 3.4533 |
| 158 | }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 159 | |
| 160 | typedef struct { |
| 161 | PREDICTION_MODE mode; |
| 162 | MV_REFERENCE_FRAME ref_frame[2]; |
| 163 | } MODE_DEFINITION; |
| 164 | |
| 165 | typedef struct { MV_REFERENCE_FRAME ref_frame[2]; } REF_DEFINITION; |
| 166 | |
| 167 | struct rdcost_block_args { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 168 | const AV1_COMP *cpi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 169 | MACROBLOCK *x; |
| 170 | ENTROPY_CONTEXT t_above[2 * MAX_MIB_SIZE]; |
| 171 | ENTROPY_CONTEXT t_left[2 * MAX_MIB_SIZE]; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 172 | RD_STATS rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 173 | int64_t this_rd; |
| 174 | int64_t best_rd; |
| 175 | int exit_early; |
| 176 | int use_fast_coef_costing; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | #define LAST_NEW_MV_INDEX 6 |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 180 | static const MODE_DEFINITION av1_mode_order[MAX_MODES] = { |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 181 | { NEARESTMV, { LAST_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 182 | #if CONFIG_EXT_REFS |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 183 | { NEARESTMV, { LAST2_FRAME, NONE_FRAME } }, |
| 184 | { NEARESTMV, { LAST3_FRAME, NONE_FRAME } }, |
| 185 | { NEARESTMV, { BWDREF_FRAME, NONE_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 186 | { NEARESTMV, { ALTREF2_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 187 | #endif // CONFIG_EXT_REFS |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 188 | { NEARESTMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 189 | { NEARESTMV, { GOLDEN_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 190 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 191 | { DC_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 192 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 193 | { NEWMV, { LAST_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 194 | #if CONFIG_EXT_REFS |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 195 | { NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 196 | { NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 197 | { NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 198 | { NEWMV, { ALTREF2_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 199 | #endif // CONFIG_EXT_REFS |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 200 | { NEWMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 201 | { NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 202 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 203 | { NEARMV, { LAST_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 204 | #if CONFIG_EXT_REFS |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 205 | { NEARMV, { LAST2_FRAME, NONE_FRAME } }, |
| 206 | { NEARMV, { LAST3_FRAME, NONE_FRAME } }, |
| 207 | { NEARMV, { BWDREF_FRAME, NONE_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 208 | { NEARMV, { ALTREF2_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 209 | #endif // CONFIG_EXT_REFS |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 210 | { NEARMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 211 | { NEARMV, { GOLDEN_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 212 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 213 | { ZEROMV, { LAST_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 214 | #if CONFIG_EXT_REFS |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 215 | { ZEROMV, { LAST2_FRAME, NONE_FRAME } }, |
| 216 | { ZEROMV, { LAST3_FRAME, NONE_FRAME } }, |
| 217 | { ZEROMV, { BWDREF_FRAME, NONE_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 218 | { ZEROMV, { ALTREF2_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 219 | #endif // CONFIG_EXT_REFS |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 220 | { ZEROMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 221 | { ZEROMV, { ALTREF_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 222 | |
| 223 | // TODO(zoeliu): May need to reconsider the order on the modes to check |
| 224 | |
| 225 | #if CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 226 | |
| 227 | #if CONFIG_COMPOUND_SINGLEREF |
| 228 | // Single ref comp mode |
| 229 | { SR_NEAREST_NEARMV, { LAST_FRAME, NONE_FRAME } }, |
| 230 | #if CONFIG_EXT_REFS |
| 231 | { SR_NEAREST_NEARMV, { LAST2_FRAME, NONE_FRAME } }, |
| 232 | { SR_NEAREST_NEARMV, { LAST3_FRAME, NONE_FRAME } }, |
| 233 | { SR_NEAREST_NEARMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 234 | #endif // CONFIG_EXT_REFS |
| 235 | { SR_NEAREST_NEARMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 236 | { SR_NEAREST_NEARMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 237 | |
| 238 | /* |
| 239 | { SR_NEAREST_NEWMV, { LAST_FRAME, NONE_FRAME } }, |
| 240 | #if CONFIG_EXT_REFS |
| 241 | { SR_NEAREST_NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 242 | { SR_NEAREST_NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 243 | { SR_NEAREST_NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 244 | #endif // CONFIG_EXT_REFS |
| 245 | { SR_NEAREST_NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 246 | { SR_NEAREST_NEWMV, { ALTREF_FRAME, NONE_FRAME } },*/ |
| 247 | |
| 248 | { SR_NEAR_NEWMV, { LAST_FRAME, NONE_FRAME } }, |
| 249 | #if CONFIG_EXT_REFS |
| 250 | { SR_NEAR_NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 251 | { SR_NEAR_NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 252 | { SR_NEAR_NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 253 | #endif // CONFIG_EXT_REFS |
| 254 | { SR_NEAR_NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 255 | { SR_NEAR_NEWMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 256 | |
| 257 | { SR_ZERO_NEWMV, { LAST_FRAME, NONE_FRAME } }, |
| 258 | #if CONFIG_EXT_REFS |
| 259 | { SR_ZERO_NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 260 | { SR_ZERO_NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 261 | { SR_ZERO_NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 262 | #endif // CONFIG_EXT_REFS |
| 263 | { SR_ZERO_NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 264 | { SR_ZERO_NEWMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 265 | |
| 266 | { SR_NEW_NEWMV, { LAST_FRAME, NONE_FRAME } }, |
| 267 | #if CONFIG_EXT_REFS |
| 268 | { SR_NEW_NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 269 | { SR_NEW_NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 270 | { SR_NEW_NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 271 | #endif // CONFIG_EXT_REFS |
| 272 | { SR_NEW_NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 273 | { SR_NEW_NEWMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 274 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 275 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 276 | { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 277 | #if CONFIG_EXT_REFS |
| 278 | { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 279 | { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 280 | #endif // CONFIG_EXT_REFS |
| 281 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 282 | #if CONFIG_EXT_REFS |
| 283 | { NEAREST_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 284 | { NEAREST_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 285 | { NEAREST_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 286 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 287 | { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 288 | { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 289 | { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 290 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 291 | |
| 292 | #if CONFIG_EXT_COMP_REFS |
| 293 | { NEAREST_NEARESTMV, { LAST_FRAME, LAST2_FRAME } }, |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 294 | { NEAREST_NEARESTMV, { LAST_FRAME, LAST3_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 295 | { NEAREST_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 296 | { NEAREST_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 297 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 298 | #endif // CONFIG_EXT_REFS |
| 299 | |
| 300 | #else // CONFIG_EXT_INTER |
| 301 | |
| 302 | { NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 303 | #if CONFIG_EXT_REFS |
| 304 | { NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 305 | { NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 306 | #endif // CONFIG_EXT_REFS |
| 307 | { NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 308 | #if CONFIG_EXT_REFS |
| 309 | { NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 310 | { NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 311 | { NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 312 | { NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 313 | { NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 314 | { NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 315 | { NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 316 | { NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 317 | |
| 318 | #if CONFIG_EXT_COMP_REFS |
| 319 | { NEARESTMV, { LAST_FRAME, LAST2_FRAME } }, |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 320 | { NEARESTMV, { LAST_FRAME, LAST3_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 321 | { NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 322 | { NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 323 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 324 | #endif // CONFIG_EXT_REFS |
| 325 | #endif // CONFIG_EXT_INTER |
| 326 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 327 | { TM_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 328 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 329 | { SMOOTH_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Urvang Joshi | e6ca8e8 | 2017-03-15 14:57:41 -0700 | [diff] [blame] | 330 | #if CONFIG_SMOOTH_HV |
| 331 | { SMOOTH_V_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 332 | { SMOOTH_H_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 333 | #endif // CONFIG_SMOOTH_HV |
Urvang Joshi | 6be4a54 | 2016-11-03 15:24:05 -0700 | [diff] [blame] | 334 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 335 | #if CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 336 | { NEAR_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 337 | { NEW_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 338 | { NEAREST_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 339 | { NEW_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 340 | { NEAR_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 341 | { NEW_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 342 | { ZERO_ZEROMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 343 | |
| 344 | #if CONFIG_EXT_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 345 | { NEAR_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 346 | { NEW_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 347 | { NEAREST_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 348 | { NEW_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 349 | { NEAR_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 350 | { NEW_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 351 | { ZERO_ZEROMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 352 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 353 | { NEAR_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 354 | { NEW_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 355 | { NEAREST_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 356 | { NEW_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 357 | { NEAR_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 358 | { NEW_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 359 | { ZERO_ZEROMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 360 | #endif // CONFIG_EXT_REFS |
| 361 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 362 | { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 363 | { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 364 | { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 365 | { NEW_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 366 | { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 367 | { NEW_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 368 | { ZERO_ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 369 | |
| 370 | #if CONFIG_EXT_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 371 | { NEAR_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 372 | { NEW_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 373 | { NEAREST_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 374 | { NEW_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 375 | { NEAR_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 376 | { NEW_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 377 | { ZERO_ZEROMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 378 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 379 | { NEAR_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 380 | { NEW_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 381 | { NEAREST_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 382 | { NEW_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 383 | { NEAR_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 384 | { NEW_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 385 | { ZERO_ZEROMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 386 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 387 | { NEAR_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 388 | { NEW_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 389 | { NEAREST_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 390 | { NEW_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 391 | { NEAR_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 392 | { NEW_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 393 | { ZERO_ZEROMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 394 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 395 | { NEAR_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 396 | { NEW_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 397 | { NEAREST_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 398 | { NEW_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 399 | { NEAR_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 400 | { NEW_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 401 | { ZERO_ZEROMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 402 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 403 | { NEAR_NEARMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 404 | { NEW_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 405 | { NEAREST_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 406 | { NEW_NEARMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 407 | { NEAR_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 408 | { NEW_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 409 | { ZERO_ZEROMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 410 | |
| 411 | { NEAR_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 412 | { NEW_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 413 | { NEAREST_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 414 | { NEW_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 415 | { NEAR_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 416 | { NEW_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 417 | { ZERO_ZEROMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 418 | |
| 419 | { NEAR_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 420 | { NEW_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 421 | { NEAREST_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 422 | { NEW_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 423 | { NEAR_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 424 | { NEW_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 425 | { ZERO_ZEROMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 426 | |
| 427 | { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 428 | { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 429 | { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 430 | { NEW_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 431 | { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 432 | { NEW_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 433 | { ZERO_ZEROMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 434 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 435 | #if CONFIG_EXT_COMP_REFS |
| 436 | { NEAR_NEARMV, { LAST_FRAME, LAST2_FRAME } }, |
| 437 | { NEW_NEARESTMV, { LAST_FRAME, LAST2_FRAME } }, |
| 438 | { NEAREST_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 439 | { NEW_NEARMV, { LAST_FRAME, LAST2_FRAME } }, |
| 440 | { NEAR_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 441 | { NEW_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 442 | { ZERO_ZEROMV, { LAST_FRAME, LAST2_FRAME } }, |
| 443 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 444 | { NEAR_NEARMV, { LAST_FRAME, LAST3_FRAME } }, |
| 445 | { NEW_NEARESTMV, { LAST_FRAME, LAST3_FRAME } }, |
| 446 | { NEAREST_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 447 | { NEW_NEARMV, { LAST_FRAME, LAST3_FRAME } }, |
| 448 | { NEAR_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 449 | { NEW_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 450 | { ZERO_ZEROMV, { LAST_FRAME, LAST3_FRAME } }, |
| 451 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 452 | { NEAR_NEARMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 453 | { NEW_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 454 | { NEAREST_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 455 | { NEW_NEARMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 456 | { NEAR_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 457 | { NEW_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 458 | { ZERO_ZEROMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 459 | |
| 460 | { NEAR_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 461 | { NEW_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 462 | { NEAREST_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 463 | { NEW_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 464 | { NEAR_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 465 | { NEW_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 466 | { ZERO_ZEROMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 467 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 468 | #endif // CONFIG_EXT_REFS |
| 469 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 470 | #else // !CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 471 | |
| 472 | { NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 473 | { NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 474 | #if CONFIG_EXT_REFS |
| 475 | { NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 476 | { NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 477 | { NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 478 | { NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 479 | #endif // CONFIG_EXT_REFS |
| 480 | { NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 481 | { NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 482 | |
| 483 | #if CONFIG_EXT_REFS |
| 484 | { NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 485 | { NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 486 | { NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 487 | { NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 488 | { NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 489 | { NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 490 | { NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 491 | { NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 492 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 493 | { NEARMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 494 | { NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 495 | { NEARMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 496 | { NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 497 | { NEARMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 498 | { NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 499 | { NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 500 | { NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 501 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 502 | #if CONFIG_EXT_COMP_REFS |
| 503 | { NEARMV, { LAST_FRAME, LAST2_FRAME } }, |
| 504 | { NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 505 | { NEARMV, { LAST_FRAME, LAST3_FRAME } }, |
| 506 | { NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 507 | { NEARMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 508 | { NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 509 | { NEARMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 510 | { NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 511 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 512 | #endif // CONFIG_EXT_REFS |
| 513 | |
| 514 | { ZEROMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 515 | #if CONFIG_EXT_REFS |
| 516 | { ZEROMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 517 | { ZEROMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 518 | #endif // CONFIG_EXT_REFS |
| 519 | { ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 520 | |
| 521 | #if CONFIG_EXT_REFS |
| 522 | { ZEROMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 523 | { ZEROMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 524 | { ZEROMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 525 | { ZEROMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 526 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 527 | { ZEROMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 528 | { ZEROMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 529 | { ZEROMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 530 | { ZEROMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 531 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 532 | #if CONFIG_EXT_COMP_REFS |
| 533 | { ZEROMV, { LAST_FRAME, LAST2_FRAME } }, |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 534 | { ZEROMV, { LAST_FRAME, LAST3_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 535 | { ZEROMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 536 | { ZEROMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 537 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 538 | #endif // CONFIG_EXT_REFS |
| 539 | |
| 540 | #endif // CONFIG_EXT_INTER |
| 541 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 542 | { H_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 543 | { V_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 544 | { D135_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 545 | { D207_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 546 | { D153_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 547 | { D63_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 548 | { D117_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 549 | { D45_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 550 | |
| 551 | #if CONFIG_EXT_INTER |
| 552 | { ZEROMV, { LAST_FRAME, INTRA_FRAME } }, |
| 553 | { NEARESTMV, { LAST_FRAME, INTRA_FRAME } }, |
| 554 | { NEARMV, { LAST_FRAME, INTRA_FRAME } }, |
| 555 | { NEWMV, { LAST_FRAME, INTRA_FRAME } }, |
| 556 | |
| 557 | #if CONFIG_EXT_REFS |
| 558 | { ZEROMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 559 | { NEARESTMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 560 | { NEARMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 561 | { NEWMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 562 | |
| 563 | { ZEROMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 564 | { NEARESTMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 565 | { NEARMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 566 | { NEWMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 567 | #endif // CONFIG_EXT_REFS |
| 568 | |
| 569 | { ZEROMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 570 | { NEARESTMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 571 | { NEARMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 572 | { NEWMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 573 | |
| 574 | #if CONFIG_EXT_REFS |
| 575 | { ZEROMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 576 | { NEARESTMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 577 | { NEARMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 578 | { NEWMV, { BWDREF_FRAME, INTRA_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 579 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 580 | { ZEROMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
| 581 | { NEARESTMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
| 582 | { NEARMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
| 583 | { NEWMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 584 | #endif // CONFIG_EXT_REFS |
| 585 | |
| 586 | { ZEROMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 587 | { NEARESTMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 588 | { NEARMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 589 | { NEWMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 590 | #endif // CONFIG_EXT_INTER |
| 591 | }; |
| 592 | |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 593 | static const PREDICTION_MODE intra_rd_search_mode_order[INTRA_MODES] = { |
Urvang Joshi | 93b543a | 2017-06-01 17:32:41 -0700 | [diff] [blame] | 594 | DC_PRED, H_PRED, V_PRED, SMOOTH_PRED, TM_PRED, |
| 595 | #if CONFIG_SMOOTH_HV |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 596 | SMOOTH_V_PRED, SMOOTH_H_PRED, |
Urvang Joshi | 93b543a | 2017-06-01 17:32:41 -0700 | [diff] [blame] | 597 | #endif // CONFIG_SMOOTH_HV |
| 598 | D135_PRED, D207_PRED, D153_PRED, D63_PRED, D117_PRED, D45_PRED, |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 599 | }; |
| 600 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 601 | #if CONFIG_CFL |
| 602 | static const UV_PREDICTION_MODE uv_rd_search_mode_order[UV_INTRA_MODES] = { |
Urvang Joshi | 93b543a | 2017-06-01 17:32:41 -0700 | [diff] [blame] | 603 | UV_DC_PRED, UV_CFL_PRED, UV_H_PRED, |
| 604 | UV_V_PRED, UV_SMOOTH_PRED, UV_TM_PRED, |
| 605 | #if CONFIG_SMOOTH_HV |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 606 | UV_SMOOTH_V_PRED, UV_SMOOTH_H_PRED, |
Urvang Joshi | 93b543a | 2017-06-01 17:32:41 -0700 | [diff] [blame] | 607 | #endif // CONFIG_SMOOTH_HV |
| 608 | UV_D135_PRED, UV_D207_PRED, UV_D153_PRED, |
| 609 | UV_D63_PRED, UV_D117_PRED, UV_D45_PRED, |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 610 | }; |
| 611 | #else |
| 612 | #define uv_rd_search_mode_order intra_rd_search_mode_order |
| 613 | #endif // CONFIG_CFL |
| 614 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 615 | static INLINE int write_uniform_cost(int n, int v) { |
hui su | 3749929 | 2017-04-26 09:49:53 -0700 | [diff] [blame] | 616 | const int l = get_unsigned_bits(n); |
| 617 | const int m = (1 << l) - n; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 618 | if (l == 0) return 0; |
| 619 | if (v < m) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 620 | return (l - 1) * av1_cost_bit(128, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 621 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 622 | return l * av1_cost_bit(128, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // constants for prune 1 and prune 2 decision boundaries |
| 626 | #define FAST_EXT_TX_CORR_MID 0.0 |
| 627 | #define FAST_EXT_TX_EDST_MID 0.1 |
| 628 | #define FAST_EXT_TX_CORR_MARGIN 0.5 |
| 629 | #define FAST_EXT_TX_EDST_MARGIN 0.3 |
| 630 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 631 | static unsigned pixel_dist_visible_only( |
| 632 | const AV1_COMP *const cpi, const MACROBLOCK *x, const uint8_t *src, |
| 633 | const int src_stride, const uint8_t *dst, const int dst_stride, |
| 634 | const BLOCK_SIZE tx_bsize, int txb_rows, int txb_cols, int visible_rows, |
| 635 | int visible_cols) { |
| 636 | unsigned sse; |
| 637 | |
| 638 | if (txb_rows == visible_rows && txb_cols == visible_cols |
| 639 | #if CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX) |
| 640 | && tx_bsize < BLOCK_SIZES |
| 641 | #endif |
| 642 | ) { |
| 643 | cpi->fn_ptr[tx_bsize].vf(src, src_stride, dst, dst_stride, &sse); |
| 644 | return sse; |
| 645 | } |
| 646 | #if CONFIG_HIGHBITDEPTH |
| 647 | const MACROBLOCKD *xd = &x->e_mbd; |
| 648 | |
| 649 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 650 | uint64_t sse64 = aom_highbd_sse_odd_size(src, src_stride, dst, dst_stride, |
| 651 | visible_cols, visible_rows); |
| 652 | return (unsigned int)ROUND_POWER_OF_TWO(sse64, (xd->bd - 8) * 2); |
| 653 | } |
| 654 | #else |
| 655 | (void)x; |
| 656 | #endif // CONFIG_HIGHBITDEPTH |
| 657 | sse = aom_sse_odd_size(src, src_stride, dst, dst_stride, visible_cols, |
| 658 | visible_rows); |
| 659 | return sse; |
| 660 | } |
| 661 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 662 | #if CONFIG_DIST_8X8 |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 663 | static uint64_t cdef_dist_8x8_16bit(uint16_t *dst, int dstride, uint16_t *src, |
| 664 | int sstride, int coeff_shift) { |
| 665 | uint64_t svar = 0; |
| 666 | uint64_t dvar = 0; |
| 667 | uint64_t sum_s = 0; |
| 668 | uint64_t sum_d = 0; |
| 669 | uint64_t sum_s2 = 0; |
| 670 | uint64_t sum_d2 = 0; |
| 671 | uint64_t sum_sd = 0; |
| 672 | uint64_t dist = 0; |
| 673 | |
| 674 | int i, j; |
| 675 | for (i = 0; i < 8; i++) { |
| 676 | for (j = 0; j < 8; j++) { |
| 677 | sum_s += src[i * sstride + j]; |
| 678 | sum_d += dst[i * dstride + j]; |
| 679 | sum_s2 += src[i * sstride + j] * src[i * sstride + j]; |
| 680 | sum_d2 += dst[i * dstride + j] * dst[i * dstride + j]; |
| 681 | sum_sd += src[i * sstride + j] * dst[i * dstride + j]; |
| 682 | } |
| 683 | } |
| 684 | /* Compute the variance -- the calculation cannot go negative. */ |
| 685 | svar = sum_s2 - ((sum_s * sum_s + 32) >> 6); |
| 686 | dvar = sum_d2 - ((sum_d * sum_d + 32) >> 6); |
| 687 | |
| 688 | // Tuning of jm's original dering distortion metric used in CDEF tool, |
| 689 | // suggested by jm |
| 690 | const uint64_t a = 4; |
| 691 | const uint64_t b = 2; |
| 692 | const uint64_t c1 = (400 * a << 2 * coeff_shift); |
| 693 | const uint64_t c2 = (b * 20000 * a * a << 4 * coeff_shift); |
| 694 | |
| 695 | dist = |
| 696 | (uint64_t)floor(.5 + |
| 697 | (sum_d2 + sum_s2 - 2 * sum_sd) * .5 * (svar + dvar + c1) / |
| 698 | (sqrt(svar * (double)dvar + c2))); |
| 699 | |
| 700 | // Calibrate dist to have similar rate for the same QP with MSE only |
| 701 | // distortion (as in master branch) |
| 702 | dist = (uint64_t)((float)dist * 0.75); |
| 703 | |
| 704 | return dist; |
| 705 | } |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 706 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 707 | static int od_compute_var_4x4(uint16_t *x, int stride) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 708 | int sum; |
| 709 | int s2; |
| 710 | int i; |
| 711 | sum = 0; |
| 712 | s2 = 0; |
| 713 | for (i = 0; i < 4; i++) { |
| 714 | int j; |
| 715 | for (j = 0; j < 4; j++) { |
| 716 | int t; |
| 717 | |
| 718 | t = x[i * stride + j]; |
| 719 | sum += t; |
| 720 | s2 += t * t; |
| 721 | } |
| 722 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 723 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 724 | return (s2 - (sum * sum >> 4)) >> 4; |
| 725 | } |
| 726 | |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 727 | /* OD_DIST_LP_MID controls the frequency weighting filter used for computing |
| 728 | the distortion. For a value X, the filter is [1 X 1]/(X + 2) and |
| 729 | is applied both horizontally and vertically. For X=5, the filter is |
| 730 | a good approximation for the OD_QM8_Q4_HVS quantization matrix. */ |
| 731 | #define OD_DIST_LP_MID (5) |
| 732 | #define OD_DIST_LP_NORM (OD_DIST_LP_MID + 2) |
| 733 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 734 | static double od_compute_dist_8x8(int use_activity_masking, uint16_t *x, |
| 735 | uint16_t *y, od_coeff *e_lp, int stride) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 736 | double sum; |
| 737 | int min_var; |
| 738 | double mean_var; |
| 739 | double var_stat; |
| 740 | double activity; |
| 741 | double calibration; |
| 742 | int i; |
| 743 | int j; |
| 744 | double vardist; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 745 | |
| 746 | vardist = 0; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 747 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 748 | #if 1 |
| 749 | min_var = INT_MAX; |
| 750 | mean_var = 0; |
| 751 | for (i = 0; i < 3; i++) { |
| 752 | for (j = 0; j < 3; j++) { |
| 753 | int varx; |
| 754 | int vary; |
| 755 | varx = od_compute_var_4x4(x + 2 * i * stride + 2 * j, stride); |
| 756 | vary = od_compute_var_4x4(y + 2 * i * stride + 2 * j, stride); |
| 757 | min_var = OD_MINI(min_var, varx); |
| 758 | mean_var += 1. / (1 + varx); |
| 759 | /* The cast to (double) is to avoid an overflow before the sqrt.*/ |
| 760 | vardist += varx - 2 * sqrt(varx * (double)vary) + vary; |
| 761 | } |
| 762 | } |
| 763 | /* We use a different variance statistic depending on whether activity |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 764 | masking is used, since the harmonic mean appeared slightly worse with |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 765 | masking off. The calibration constant just ensures that we preserve the |
| 766 | rate compared to activity=1. */ |
| 767 | if (use_activity_masking) { |
| 768 | calibration = 1.95; |
| 769 | var_stat = 9. / mean_var; |
| 770 | } else { |
| 771 | calibration = 1.62; |
| 772 | var_stat = min_var; |
| 773 | } |
| 774 | /* 1.62 is a calibration constant, 0.25 is a noise floor and 1/6 is the |
| 775 | activity masking constant. */ |
| 776 | activity = calibration * pow(.25 + var_stat, -1. / 6); |
| 777 | #else |
| 778 | activity = 1; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 779 | #endif // 1 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 780 | sum = 0; |
| 781 | for (i = 0; i < 8; i++) { |
| 782 | for (j = 0; j < 8; j++) |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 783 | sum += e_lp[i * stride + j] * (double)e_lp[i * stride + j]; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 784 | } |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 785 | /* Normalize the filter to unit DC response. */ |
| 786 | sum *= 1. / (OD_DIST_LP_NORM * OD_DIST_LP_NORM * OD_DIST_LP_NORM * |
| 787 | OD_DIST_LP_NORM); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 788 | return activity * activity * (sum + vardist); |
| 789 | } |
| 790 | |
| 791 | // Note : Inputs x and y are in a pixel domain |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 792 | static double od_compute_dist_common(int activity_masking, uint16_t *x, |
| 793 | uint16_t *y, int bsize_w, int bsize_h, |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 794 | int qindex, od_coeff *tmp, |
| 795 | od_coeff *e_lp) { |
| 796 | int i, j; |
| 797 | double sum = 0; |
| 798 | const int mid = OD_DIST_LP_MID; |
| 799 | |
| 800 | for (j = 0; j < bsize_w; j++) { |
| 801 | e_lp[j] = mid * tmp[j] + 2 * tmp[bsize_w + j]; |
| 802 | e_lp[(bsize_h - 1) * bsize_w + j] = mid * tmp[(bsize_h - 1) * bsize_w + j] + |
| 803 | 2 * tmp[(bsize_h - 2) * bsize_w + j]; |
| 804 | } |
| 805 | for (i = 1; i < bsize_h - 1; i++) { |
| 806 | for (j = 0; j < bsize_w; j++) { |
| 807 | e_lp[i * bsize_w + j] = mid * tmp[i * bsize_w + j] + |
| 808 | tmp[(i - 1) * bsize_w + j] + |
| 809 | tmp[(i + 1) * bsize_w + j]; |
| 810 | } |
| 811 | } |
| 812 | for (i = 0; i < bsize_h; i += 8) { |
| 813 | for (j = 0; j < bsize_w; j += 8) { |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 814 | sum += od_compute_dist_8x8(activity_masking, &x[i * bsize_w + j], |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 815 | &y[i * bsize_w + j], &e_lp[i * bsize_w + j], |
| 816 | bsize_w); |
| 817 | } |
| 818 | } |
| 819 | /* Scale according to linear regression against SSE, for 8x8 blocks. */ |
| 820 | if (activity_masking) { |
| 821 | sum *= 2.2 + (1.7 - 2.2) * (qindex - 99) / (210 - 99) + |
| 822 | (qindex < 99 ? 2.5 * (qindex - 99) / 99 * (qindex - 99) / 99 : 0); |
| 823 | } else { |
| 824 | sum *= qindex >= 128 |
| 825 | ? 1.4 + (0.9 - 1.4) * (qindex - 128) / (209 - 128) |
| 826 | : qindex <= 43 ? 1.5 + (2.0 - 1.5) * (qindex - 43) / (16 - 43) |
| 827 | : 1.5 + (1.4 - 1.5) * (qindex - 43) / (128 - 43); |
| 828 | } |
| 829 | |
| 830 | return sum; |
| 831 | } |
| 832 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 833 | static double od_compute_dist(uint16_t *x, uint16_t *y, int bsize_w, |
| 834 | int bsize_h, int qindex) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 835 | assert(bsize_w >= 8 && bsize_h >= 8); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 836 | #if CONFIG_PVQ |
| 837 | int activity_masking = 1; |
| 838 | #else |
| 839 | int activity_masking = 0; |
| 840 | #endif |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 841 | int i, j; |
| 842 | DECLARE_ALIGNED(16, od_coeff, e[MAX_TX_SQUARE]); |
| 843 | DECLARE_ALIGNED(16, od_coeff, tmp[MAX_TX_SQUARE]); |
| 844 | DECLARE_ALIGNED(16, od_coeff, e_lp[MAX_TX_SQUARE]); |
| 845 | for (i = 0; i < bsize_h; i++) { |
| 846 | for (j = 0; j < bsize_w; j++) { |
| 847 | 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] | 848 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 849 | } |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 850 | int mid = OD_DIST_LP_MID; |
| 851 | for (i = 0; i < bsize_h; i++) { |
| 852 | tmp[i * bsize_w] = mid * e[i * bsize_w] + 2 * e[i * bsize_w + 1]; |
| 853 | tmp[i * bsize_w + bsize_w - 1] = |
| 854 | mid * e[i * bsize_w + bsize_w - 1] + 2 * e[i * bsize_w + bsize_w - 2]; |
| 855 | for (j = 1; j < bsize_w - 1; j++) { |
| 856 | tmp[i * bsize_w + j] = mid * e[i * bsize_w + j] + e[i * bsize_w + j - 1] + |
| 857 | e[i * bsize_w + j + 1]; |
| 858 | } |
| 859 | } |
| 860 | return od_compute_dist_common(activity_masking, x, y, bsize_w, bsize_h, |
| 861 | qindex, tmp, e_lp); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 864 | static double od_compute_dist_diff(uint16_t *x, int16_t *e, int bsize_w, |
| 865 | int bsize_h, int qindex) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 866 | assert(bsize_w >= 8 && bsize_h >= 8); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 867 | #if CONFIG_PVQ |
| 868 | int activity_masking = 1; |
| 869 | #else |
| 870 | int activity_masking = 0; |
| 871 | #endif |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 872 | DECLARE_ALIGNED(16, uint16_t, y[MAX_TX_SQUARE]); |
| 873 | DECLARE_ALIGNED(16, od_coeff, tmp[MAX_TX_SQUARE]); |
| 874 | DECLARE_ALIGNED(16, od_coeff, e_lp[MAX_TX_SQUARE]); |
| 875 | int i, j; |
| 876 | for (i = 0; i < bsize_h; i++) { |
| 877 | for (j = 0; j < bsize_w; j++) { |
| 878 | 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] | 879 | } |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 880 | } |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 881 | int mid = OD_DIST_LP_MID; |
| 882 | for (i = 0; i < bsize_h; i++) { |
| 883 | tmp[i * bsize_w] = mid * e[i * bsize_w] + 2 * e[i * bsize_w + 1]; |
| 884 | tmp[i * bsize_w + bsize_w - 1] = |
| 885 | mid * e[i * bsize_w + bsize_w - 1] + 2 * e[i * bsize_w + bsize_w - 2]; |
| 886 | for (j = 1; j < bsize_w - 1; j++) { |
| 887 | tmp[i * bsize_w + j] = mid * e[i * bsize_w + j] + e[i * bsize_w + j - 1] + |
| 888 | e[i * bsize_w + j + 1]; |
| 889 | } |
| 890 | } |
| 891 | return od_compute_dist_common(activity_masking, x, y, bsize_w, bsize_h, |
| 892 | qindex, tmp, e_lp); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 895 | 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] | 896 | const uint8_t *src, int src_stride, const uint8_t *dst, |
| 897 | int dst_stride, const BLOCK_SIZE tx_bsize, int bsw, |
| 898 | int bsh, int visible_w, int visible_h, int qindex) { |
| 899 | int64_t d = 0; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 900 | int i, j; |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 901 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 902 | |
| 903 | DECLARE_ALIGNED(16, uint16_t, orig[MAX_TX_SQUARE]); |
| 904 | DECLARE_ALIGNED(16, uint16_t, rec[MAX_TX_SQUARE]); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 905 | |
Yushin Cho | ee81027 | 2017-09-13 17:30:25 -0700 | [diff] [blame] | 906 | assert(bsw >= 8); |
| 907 | assert(bsh >= 8); |
| 908 | assert((bsw & 0x07) == 0); |
| 909 | assert((bsh & 0x07) == 0); |
| 910 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 911 | if (x->tune_metric == AOM_TUNE_CDEF_DIST || |
| 912 | x->tune_metric == AOM_TUNE_DAALA_DIST) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 913 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 914 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 915 | for (j = 0; j < bsh; j++) |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 916 | for (i = 0; i < bsw; i++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 917 | orig[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 918 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 919 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 920 | for (j = 0; j < bsh; j++) |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 921 | for (i = 0; i < bsw; i++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 922 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(dst)[j * dst_stride + i]; |
| 923 | } else { |
| 924 | for (j = 0; j < visible_h; j++) |
| 925 | for (i = 0; i < visible_w; i++) |
| 926 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(dst)[j * dst_stride + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 927 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 928 | if (visible_w < bsw) { |
| 929 | for (j = 0; j < bsh; j++) |
| 930 | for (i = visible_w; i < bsw; i++) |
| 931 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 932 | } |
| 933 | |
| 934 | if (visible_h < bsh) { |
| 935 | for (j = visible_h; j < bsh; j++) |
| 936 | for (i = 0; i < bsw; i++) |
| 937 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 938 | } |
| 939 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 940 | } else { |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 941 | #endif |
| 942 | for (j = 0; j < bsh; j++) |
| 943 | 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] | 944 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 945 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 946 | for (j = 0; j < bsh; j++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 947 | for (i = 0; i < bsw; i++) rec[j * bsw + i] = dst[j * dst_stride + i]; |
| 948 | } else { |
| 949 | for (j = 0; j < visible_h; j++) |
| 950 | for (i = 0; i < visible_w; i++) |
| 951 | rec[j * bsw + i] = dst[j * dst_stride + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 952 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 953 | if (visible_w < bsw) { |
| 954 | for (j = 0; j < bsh; j++) |
| 955 | for (i = visible_w; i < bsw; i++) |
| 956 | rec[j * bsw + i] = src[j * src_stride + i]; |
| 957 | } |
| 958 | |
| 959 | if (visible_h < bsh) { |
| 960 | for (j = visible_h; j < bsh; j++) |
| 961 | for (i = 0; i < bsw; i++) |
| 962 | rec[j * bsw + i] = src[j * src_stride + i]; |
| 963 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 964 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 965 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 966 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 967 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 968 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 969 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 970 | if (x->tune_metric == AOM_TUNE_DAALA_DIST) { |
| 971 | d = (int64_t)od_compute_dist(orig, rec, bsw, bsh, qindex); |
| 972 | } else if (x->tune_metric == AOM_TUNE_CDEF_DIST) { |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 973 | int coeff_shift = AOMMAX(xd->bd - 8, 0); |
| 974 | |
| 975 | for (i = 0; i < bsh; i += 8) { |
| 976 | for (j = 0; j < bsw; j += 8) { |
| 977 | d += cdef_dist_8x8_16bit(&rec[i * bsw + j], bsw, &orig[i * bsw + j], |
| 978 | bsw, coeff_shift); |
| 979 | } |
| 980 | } |
| 981 | #if CONFIG_HIGHBITDEPTH |
| 982 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 983 | d = ((uint64_t)d) >> 2 * coeff_shift; |
| 984 | #endif |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 985 | } else { |
| 986 | // Otherwise, MSE by default |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 987 | d = pixel_dist_visible_only(cpi, x, src, src_stride, dst, dst_stride, |
| 988 | tx_bsize, bsh, bsw, visible_h, visible_w); |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 989 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 990 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 991 | return d; |
| 992 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 993 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 994 | 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] | 995 | int src_stride, const int16_t *diff, |
| 996 | int diff_stride, int bsw, int bsh, |
| 997 | int visible_w, int visible_h, int qindex) { |
| 998 | int64_t d = 0; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 999 | int i, j; |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1000 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1001 | |
| 1002 | DECLARE_ALIGNED(16, uint16_t, orig[MAX_TX_SQUARE]); |
| 1003 | DECLARE_ALIGNED(16, int16_t, diff16[MAX_TX_SQUARE]); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1004 | |
Yushin Cho | ee81027 | 2017-09-13 17:30:25 -0700 | [diff] [blame] | 1005 | assert(bsw >= 8); |
| 1006 | assert(bsh >= 8); |
| 1007 | assert((bsw & 0x07) == 0); |
| 1008 | assert((bsh & 0x07) == 0); |
| 1009 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1010 | if (x->tune_metric == AOM_TUNE_CDEF_DIST || |
| 1011 | x->tune_metric == AOM_TUNE_DAALA_DIST) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 1012 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1013 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1014 | for (j = 0; j < bsh; j++) |
| 1015 | for (i = 0; i < bsw; i++) |
| 1016 | orig[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 1017 | } else { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 1018 | #endif |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1019 | for (j = 0; j < bsh; j++) |
| 1020 | 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] | 1021 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1022 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 1023 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1024 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1025 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1026 | for (j = 0; j < bsh; j++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1027 | for (i = 0; i < bsw; i++) |
| 1028 | diff16[j * bsw + i] = diff[j * diff_stride + i]; |
| 1029 | } else { |
| 1030 | for (j = 0; j < visible_h; j++) |
| 1031 | for (i = 0; i < visible_w; i++) |
| 1032 | diff16[j * bsw + i] = diff[j * diff_stride + i]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1033 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1034 | if (visible_w < bsw) { |
| 1035 | for (j = 0; j < bsh; j++) |
| 1036 | for (i = visible_w; i < bsw; i++) diff16[j * bsw + i] = 0; |
| 1037 | } |
| 1038 | |
| 1039 | if (visible_h < bsh) { |
| 1040 | for (j = visible_h; j < bsh; j++) |
| 1041 | for (i = 0; i < bsw; i++) diff16[j * bsw + i] = 0; |
| 1042 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1043 | } |
| 1044 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 1045 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1046 | if (x->tune_metric == AOM_TUNE_DAALA_DIST) { |
| 1047 | d = (int64_t)od_compute_dist_diff(orig, diff16, bsw, bsh, qindex); |
| 1048 | } else if (x->tune_metric == AOM_TUNE_CDEF_DIST) { |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 1049 | int coeff_shift = AOMMAX(xd->bd - 8, 0); |
| 1050 | DECLARE_ALIGNED(16, uint16_t, dst16[MAX_TX_SQUARE]); |
| 1051 | |
| 1052 | for (i = 0; i < bsh; i++) { |
| 1053 | for (j = 0; j < bsw; j++) { |
| 1054 | dst16[i * bsw + j] = orig[i * bsw + j] - diff16[i * bsw + j]; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | for (i = 0; i < bsh; i += 8) { |
| 1059 | for (j = 0; j < bsw; j += 8) { |
| 1060 | d += cdef_dist_8x8_16bit(&dst16[i * bsw + j], bsw, &orig[i * bsw + j], |
| 1061 | bsw, coeff_shift); |
| 1062 | } |
| 1063 | } |
| 1064 | // Don't scale 'd' for HBD since it will be done by caller side for diff |
| 1065 | // input |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 1066 | } else { |
| 1067 | // Otherwise, MSE by default |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 1068 | 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] | 1069 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1070 | |
| 1071 | return d; |
| 1072 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1073 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 1074 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1075 | 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] | 1076 | const uint8_t *src, int src_stride, |
| 1077 | const uint8_t *dst, int dst_stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1078 | double *hordist, double *verdist) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1079 | const int bw = block_size_wide[bsize]; |
| 1080 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1081 | 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] | 1082 | |
| 1083 | const int f_index = bsize - BLOCK_16X16; |
| 1084 | if (f_index < 0) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1085 | const int w_shift = bw == 8 ? 1 : 2; |
| 1086 | const int h_shift = bh == 8 ? 1 : 2; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1087 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1088 | if (cpi->common.use_highbitdepth) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1089 | const uint16_t *src16 = CONVERT_TO_SHORTPTR(src); |
| 1090 | const uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst); |
| 1091 | for (int i = 0; i < bh; ++i) |
| 1092 | for (int j = 0; j < bw; ++j) { |
| 1093 | const int index = (j >> w_shift) + ((i >> h_shift) << 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1094 | esq[index] += |
| 1095 | (src16[j + i * src_stride] - dst16[j + i * dst_stride]) * |
| 1096 | (src16[j + i * src_stride] - dst16[j + i * dst_stride]); |
| 1097 | } |
| 1098 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1099 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1100 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1101 | for (int i = 0; i < bh; ++i) |
| 1102 | for (int j = 0; j < bw; ++j) { |
| 1103 | const int index = (j >> w_shift) + ((i >> h_shift) << 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1104 | esq[index] += (src[j + i * src_stride] - dst[j + i * dst_stride]) * |
| 1105 | (src[j + i * src_stride] - dst[j + i * dst_stride]); |
| 1106 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1107 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1108 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1109 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1110 | } else { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1111 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[0]); |
| 1112 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 1113 | &esq[1]); |
| 1114 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 1115 | &esq[2]); |
| 1116 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 1117 | dst_stride, &esq[3]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1118 | src += bh / 4 * src_stride; |
| 1119 | dst += bh / 4 * dst_stride; |
| 1120 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1121 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[4]); |
| 1122 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 1123 | &esq[5]); |
| 1124 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 1125 | &esq[6]); |
| 1126 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 1127 | dst_stride, &esq[7]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1128 | src += bh / 4 * src_stride; |
| 1129 | dst += bh / 4 * dst_stride; |
| 1130 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1131 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[8]); |
| 1132 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 1133 | &esq[9]); |
| 1134 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 1135 | &esq[10]); |
| 1136 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 1137 | dst_stride, &esq[11]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1138 | src += bh / 4 * src_stride; |
| 1139 | dst += bh / 4 * dst_stride; |
| 1140 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1141 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[12]); |
| 1142 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 1143 | &esq[13]); |
| 1144 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 1145 | &esq[14]); |
| 1146 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 1147 | dst_stride, &esq[15]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1150 | double total = (double)esq[0] + esq[1] + esq[2] + esq[3] + esq[4] + esq[5] + |
| 1151 | esq[6] + esq[7] + esq[8] + esq[9] + esq[10] + esq[11] + |
| 1152 | esq[12] + esq[13] + esq[14] + esq[15]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1153 | if (total > 0) { |
| 1154 | const double e_recip = 1.0 / total; |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1155 | hordist[0] = ((double)esq[0] + esq[4] + esq[8] + esq[12]) * e_recip; |
| 1156 | hordist[1] = ((double)esq[1] + esq[5] + esq[9] + esq[13]) * e_recip; |
| 1157 | hordist[2] = ((double)esq[2] + esq[6] + esq[10] + esq[14]) * e_recip; |
| 1158 | verdist[0] = ((double)esq[0] + esq[1] + esq[2] + esq[3]) * e_recip; |
| 1159 | verdist[1] = ((double)esq[4] + esq[5] + esq[6] + esq[7]) * e_recip; |
| 1160 | 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] | 1161 | } else { |
| 1162 | hordist[0] = verdist[0] = 0.25; |
| 1163 | hordist[1] = verdist[1] = 0.25; |
| 1164 | hordist[2] = verdist[2] = 0.25; |
| 1165 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 1168 | static int adst_vs_flipadst(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
| 1169 | const uint8_t *src, int src_stride, |
| 1170 | const uint8_t *dst, int dst_stride) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1171 | int prune_bitmask = 0; |
| 1172 | double svm_proj_h = 0, svm_proj_v = 0; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1173 | double hdist[3] = { 0, 0, 0 }, vdist[3] = { 0, 0, 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1174 | get_energy_distribution_fine(cpi, bsize, src, src_stride, dst, dst_stride, |
| 1175 | hdist, vdist); |
| 1176 | |
| 1177 | svm_proj_v = vdist[0] * ADST_FLIP_SVM[0] + vdist[1] * ADST_FLIP_SVM[1] + |
| 1178 | vdist[2] * ADST_FLIP_SVM[2] + ADST_FLIP_SVM[3]; |
| 1179 | svm_proj_h = hdist[0] * ADST_FLIP_SVM[4] + hdist[1] * ADST_FLIP_SVM[5] + |
| 1180 | hdist[2] * ADST_FLIP_SVM[6] + ADST_FLIP_SVM[7]; |
| 1181 | if (svm_proj_v > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN) |
| 1182 | prune_bitmask |= 1 << FLIPADST_1D; |
| 1183 | else if (svm_proj_v < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN) |
| 1184 | prune_bitmask |= 1 << ADST_1D; |
| 1185 | |
| 1186 | if (svm_proj_h > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN) |
| 1187 | prune_bitmask |= 1 << (FLIPADST_1D + 8); |
| 1188 | else if (svm_proj_h < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN) |
| 1189 | prune_bitmask |= 1 << (ADST_1D + 8); |
| 1190 | |
| 1191 | return prune_bitmask; |
| 1192 | } |
| 1193 | |
| 1194 | #if CONFIG_EXT_TX |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1195 | static void get_horver_correlation(const int16_t *diff, int stride, int w, |
| 1196 | int h, double *hcorr, double *vcorr) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1197 | // Returns hor/ver correlation coefficient |
| 1198 | const int num = (h - 1) * (w - 1); |
| 1199 | double num_r; |
| 1200 | int i, j; |
| 1201 | int64_t xy_sum = 0, xz_sum = 0; |
| 1202 | int64_t x_sum = 0, y_sum = 0, z_sum = 0; |
| 1203 | int64_t x2_sum = 0, y2_sum = 0, z2_sum = 0; |
| 1204 | double x_var_n, y_var_n, z_var_n, xy_var_n, xz_var_n; |
| 1205 | *hcorr = *vcorr = 1; |
| 1206 | |
| 1207 | assert(num > 0); |
| 1208 | num_r = 1.0 / num; |
| 1209 | for (i = 1; i < h; ++i) { |
| 1210 | for (j = 1; j < w; ++j) { |
| 1211 | const int16_t x = diff[i * stride + j]; |
| 1212 | const int16_t y = diff[i * stride + j - 1]; |
| 1213 | const int16_t z = diff[(i - 1) * stride + j]; |
| 1214 | xy_sum += x * y; |
| 1215 | xz_sum += x * z; |
| 1216 | x_sum += x; |
| 1217 | y_sum += y; |
| 1218 | z_sum += z; |
| 1219 | x2_sum += x * x; |
| 1220 | y2_sum += y * y; |
| 1221 | z2_sum += z * z; |
| 1222 | } |
| 1223 | } |
| 1224 | x_var_n = x2_sum - (x_sum * x_sum) * num_r; |
| 1225 | y_var_n = y2_sum - (y_sum * y_sum) * num_r; |
| 1226 | z_var_n = z2_sum - (z_sum * z_sum) * num_r; |
| 1227 | xy_var_n = xy_sum - (x_sum * y_sum) * num_r; |
| 1228 | xz_var_n = xz_sum - (x_sum * z_sum) * num_r; |
| 1229 | if (x_var_n > 0 && y_var_n > 0) { |
| 1230 | *hcorr = xy_var_n / sqrt(x_var_n * y_var_n); |
| 1231 | *hcorr = *hcorr < 0 ? 0 : *hcorr; |
| 1232 | } |
| 1233 | if (x_var_n > 0 && z_var_n > 0) { |
| 1234 | *vcorr = xz_var_n / sqrt(x_var_n * z_var_n); |
| 1235 | *vcorr = *vcorr < 0 ? 0 : *vcorr; |
| 1236 | } |
| 1237 | } |
| 1238 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1239 | int dct_vs_idtx(const int16_t *diff, int stride, int w, int h) { |
| 1240 | double hcorr, vcorr; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1241 | int prune_bitmask = 0; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1242 | get_horver_correlation(diff, stride, w, h, &hcorr, &vcorr); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1243 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1244 | if (vcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1245 | prune_bitmask |= 1 << IDTX_1D; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1246 | 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] | 1247 | prune_bitmask |= 1 << DCT_1D; |
| 1248 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1249 | if (hcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1250 | prune_bitmask |= 1 << (IDTX_1D + 8); |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1251 | 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] | 1252 | prune_bitmask |= 1 << (DCT_1D + 8); |
| 1253 | return prune_bitmask; |
| 1254 | } |
| 1255 | |
| 1256 | // Performance drop: 0.5%, Speed improvement: 24% |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1257 | 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] | 1258 | MACROBLOCK *x, const MACROBLOCKD *xd, |
| 1259 | int adst_flipadst, int dct_idtx) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1260 | int prune = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1261 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1262 | if (adst_flipadst) { |
| 1263 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1264 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1265 | prune |= adst_vs_flipadst(cpi, bsize, p->src.buf, p->src.stride, |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1266 | pd->dst.buf, pd->dst.stride); |
| 1267 | } |
| 1268 | if (dct_idtx) { |
| 1269 | av1_subtract_plane(x, bsize, 0); |
| 1270 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1271 | const int bw = 4 << (b_width_log2_lookup[bsize]); |
| 1272 | const int bh = 4 << (b_height_log2_lookup[bsize]); |
| 1273 | prune |= dct_vs_idtx(p->src_diff, bw, bw, bh); |
| 1274 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1275 | |
| 1276 | return prune; |
| 1277 | } |
| 1278 | #endif // CONFIG_EXT_TX |
| 1279 | |
| 1280 | // Performance drop: 0.3%, Speed improvement: 5% |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1281 | 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] | 1282 | const MACROBLOCK *x, const MACROBLOCKD *xd) { |
| 1283 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1284 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1285 | 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] | 1286 | pd->dst.stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Hui Su | 032ab8b | 2017-09-19 14:53:40 -0700 | [diff] [blame] | 1289 | #if CONFIG_EXT_TX |
| 1290 | // 1D Transforms used in inter set, this needs to be changed if |
| 1291 | // ext_tx_used_inter is changed |
| 1292 | static const int ext_tx_used_inter_1D[EXT_TX_SETS_INTER][TX_TYPES_1D] = { |
| 1293 | { 1, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, |
| 1294 | #if CONFIG_MRC_TX |
| 1295 | { 1, 0, 0, 1 }, |
| 1296 | #endif // CONFIG_MRC_TX |
| 1297 | }; |
| 1298 | #endif // CONFIG_EXT_TX |
| 1299 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1300 | static int prune_tx_types(const AV1_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x, |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 1301 | const MACROBLOCKD *const xd, int tx_set) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1302 | #if CONFIG_EXT_TX |
Yaowu Xu | 37fe5fb | 2017-05-09 11:43:45 -0700 | [diff] [blame] | 1303 | const int *tx_set_1D = tx_set >= 0 ? ext_tx_used_inter_1D[tx_set] : NULL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1304 | #else |
| 1305 | const int tx_set_1D[TX_TYPES_1D] = { 0 }; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 1306 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1307 | |
| 1308 | switch (cpi->sf.tx_type_search.prune_mode) { |
| 1309 | case NO_PRUNE: return 0; break; |
| 1310 | case PRUNE_ONE: |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 1311 | if ((tx_set >= 0) && !(tx_set_1D[FLIPADST_1D] & tx_set_1D[ADST_1D])) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1312 | return 0; |
| 1313 | return prune_one_for_sby(cpi, bsize, x, xd); |
| 1314 | break; |
| 1315 | #if CONFIG_EXT_TX |
| 1316 | case PRUNE_TWO: |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 1317 | if ((tx_set >= 0) && !(tx_set_1D[FLIPADST_1D] & tx_set_1D[ADST_1D])) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1318 | if (!(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D])) return 0; |
| 1319 | return prune_two_for_sby(cpi, bsize, x, xd, 0, 1); |
| 1320 | } |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 1321 | if ((tx_set >= 0) && !(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D])) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1322 | return prune_two_for_sby(cpi, bsize, x, xd, 1, 0); |
| 1323 | return prune_two_for_sby(cpi, bsize, x, xd, 1, 1); |
| 1324 | break; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 1325 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1326 | } |
| 1327 | assert(0); |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | static int do_tx_type_search(TX_TYPE tx_type, int prune) { |
| 1332 | // TODO(sarahparker) implement for non ext tx |
| 1333 | #if CONFIG_EXT_TX |
| 1334 | return !(((prune >> vtx_tab[tx_type]) & 1) | |
| 1335 | ((prune >> (htx_tab[tx_type] + 8)) & 1)); |
| 1336 | #else |
| 1337 | // temporary to avoid compiler warnings |
| 1338 | (void)vtx_tab; |
| 1339 | (void)htx_tab; |
| 1340 | (void)tx_type; |
| 1341 | (void)prune; |
| 1342 | return 1; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 1343 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1346 | static void model_rd_from_sse(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1347 | const MACROBLOCKD *const xd, BLOCK_SIZE bsize, |
| 1348 | int plane, int64_t sse, int *rate, |
| 1349 | int64_t *dist) { |
| 1350 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1351 | const int dequant_shift = |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1352 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1353 | (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd - 5 : |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1354 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1355 | 3; |
| 1356 | |
| 1357 | // Fast approximate the modelling function. |
| 1358 | if (cpi->sf.simple_model_rd_from_var) { |
| 1359 | const int64_t square_error = sse; |
| 1360 | int quantizer = (pd->dequant[1] >> dequant_shift); |
| 1361 | |
| 1362 | if (quantizer < 120) |
| 1363 | *rate = (int)((square_error * (280 - quantizer)) >> |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1364 | (16 - AV1_PROB_COST_SHIFT)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1365 | else |
| 1366 | *rate = 0; |
| 1367 | *dist = (square_error * quantizer) >> 8; |
| 1368 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1369 | av1_model_rd_from_var_lapndz(sse, num_pels_log2_lookup[bsize], |
| 1370 | pd->dequant[1] >> dequant_shift, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | *dist <<= 4; |
| 1374 | } |
| 1375 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1376 | 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] | 1377 | MACROBLOCK *x, MACROBLOCKD *xd, int plane_from, |
| 1378 | int plane_to, int *out_rate_sum, |
| 1379 | int64_t *out_dist_sum, int *skip_txfm_sb, |
| 1380 | int64_t *skip_sse_sb) { |
| 1381 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 1382 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 1383 | // we need to divide by 8 before sending to modeling function. |
| 1384 | int plane; |
| 1385 | const int ref = xd->mi[0]->mbmi.ref_frame[0]; |
| 1386 | |
| 1387 | int64_t rate_sum = 0; |
| 1388 | int64_t dist_sum = 0; |
| 1389 | int64_t total_sse = 0; |
| 1390 | |
| 1391 | x->pred_sse[ref] = 0; |
| 1392 | |
| 1393 | for (plane = plane_from; plane <= plane_to; ++plane) { |
| 1394 | struct macroblock_plane *const p = &x->plane[plane]; |
| 1395 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Timothy B. Terriberry | 81ec261 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 1396 | #if CONFIG_CHROMA_SUB8X8 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1397 | const BLOCK_SIZE bs = AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd)); |
| 1398 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1399 | const BLOCK_SIZE bs = get_plane_block_size(bsize, pd); |
Timothy B. Terriberry | 81ec261 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 1400 | #endif // CONFIG_CHROMA_SUB8X8 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1401 | |
| 1402 | unsigned int sse; |
| 1403 | int rate; |
| 1404 | int64_t dist; |
| 1405 | |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1406 | #if CONFIG_CB4X4 |
| 1407 | if (x->skip_chroma_rd && plane) continue; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 1408 | #endif // CONFIG_CB4X4 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1409 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1410 | // TODO(geza): Write direct sse functions that do not compute |
| 1411 | // variance as well. |
| 1412 | cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, |
| 1413 | &sse); |
| 1414 | |
| 1415 | if (plane == 0) x->pred_sse[ref] = sse; |
| 1416 | |
| 1417 | total_sse += sse; |
| 1418 | |
| 1419 | model_rd_from_sse(cpi, xd, bs, plane, sse, &rate, &dist); |
| 1420 | |
| 1421 | rate_sum += rate; |
| 1422 | dist_sum += dist; |
| 1423 | } |
| 1424 | |
| 1425 | *skip_txfm_sb = total_sse == 0; |
| 1426 | *skip_sse_sb = total_sse << 4; |
| 1427 | *out_rate_sum = (int)rate_sum; |
| 1428 | *out_dist_sum = dist_sum; |
| 1429 | } |
| 1430 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1431 | int64_t av1_block_error_c(const tran_low_t *coeff, const tran_low_t *dqcoeff, |
| 1432 | intptr_t block_size, int64_t *ssz) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1433 | int i; |
| 1434 | int64_t error = 0, sqcoeff = 0; |
| 1435 | |
| 1436 | for (i = 0; i < block_size; i++) { |
| 1437 | const int diff = coeff[i] - dqcoeff[i]; |
| 1438 | error += diff * diff; |
| 1439 | sqcoeff += coeff[i] * coeff[i]; |
| 1440 | } |
| 1441 | |
| 1442 | *ssz = sqcoeff; |
| 1443 | return error; |
| 1444 | } |
| 1445 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1446 | int64_t av1_block_error_fp_c(const int16_t *coeff, const int16_t *dqcoeff, |
| 1447 | int block_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1448 | int i; |
| 1449 | int64_t error = 0; |
| 1450 | |
| 1451 | for (i = 0; i < block_size; i++) { |
| 1452 | const int diff = coeff[i] - dqcoeff[i]; |
| 1453 | error += diff * diff; |
| 1454 | } |
| 1455 | |
| 1456 | return error; |
| 1457 | } |
| 1458 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1459 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1460 | int64_t av1_highbd_block_error_c(const tran_low_t *coeff, |
| 1461 | const tran_low_t *dqcoeff, intptr_t block_size, |
| 1462 | int64_t *ssz, int bd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1463 | int i; |
| 1464 | int64_t error = 0, sqcoeff = 0; |
| 1465 | int shift = 2 * (bd - 8); |
| 1466 | int rounding = shift > 0 ? 1 << (shift - 1) : 0; |
| 1467 | |
| 1468 | for (i = 0; i < block_size; i++) { |
| 1469 | const int64_t diff = coeff[i] - dqcoeff[i]; |
| 1470 | error += diff * diff; |
| 1471 | sqcoeff += (int64_t)coeff[i] * (int64_t)coeff[i]; |
| 1472 | } |
| 1473 | assert(error >= 0 && sqcoeff >= 0); |
| 1474 | error = (error + rounding) >> shift; |
| 1475 | sqcoeff = (sqcoeff + rounding) >> shift; |
| 1476 | |
| 1477 | *ssz = sqcoeff; |
| 1478 | return error; |
| 1479 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1480 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1481 | |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1482 | #if CONFIG_PVQ |
| 1483 | // Without PVQ, av1_block_error_c() return two kind of errors, |
| 1484 | // 1) reconstruction (i.e. decoded) error and |
| 1485 | // 2) Squared sum of transformed residue (i.e. 'coeff') |
| 1486 | // However, if PVQ is enabled, coeff does not keep the transformed residue |
| 1487 | // but instead a transformed original is kept. |
| 1488 | // Hence, new parameter ref vector (i.e. transformed predicted signal) |
| 1489 | // is required to derive the residue signal, |
| 1490 | // i.e. coeff - ref = residue (all transformed). |
| 1491 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1492 | #if CONFIG_HIGHBITDEPTH |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1493 | static int64_t av1_highbd_block_error2_c(const tran_low_t *coeff, |
| 1494 | const tran_low_t *dqcoeff, |
| 1495 | const tran_low_t *ref, |
| 1496 | intptr_t block_size, int64_t *ssz, |
| 1497 | int bd) { |
| 1498 | int64_t error; |
| 1499 | int64_t sqcoeff; |
| 1500 | int shift = 2 * (bd - 8); |
| 1501 | int rounding = shift > 0 ? 1 << (shift - 1) : 0; |
| 1502 | // Use the existing sse codes for calculating distortion of decoded signal: |
| 1503 | // i.e. (orig - decoded)^2 |
| 1504 | // For high bit depth, throw away ssz until a 32-bit version of |
| 1505 | // av1_block_error_fp is written. |
| 1506 | int64_t ssz_trash; |
| 1507 | error = av1_block_error(coeff, dqcoeff, block_size, &ssz_trash); |
| 1508 | // prediction residue^2 = (orig - ref)^2 |
| 1509 | sqcoeff = av1_block_error(coeff, ref, block_size, &ssz_trash); |
| 1510 | error = (error + rounding) >> shift; |
| 1511 | sqcoeff = (sqcoeff + rounding) >> shift; |
| 1512 | *ssz = sqcoeff; |
| 1513 | return error; |
| 1514 | } |
| 1515 | #else |
| 1516 | // TODO(yushin) : Since 4x4 case does not need ssz, better to refactor into |
| 1517 | // a separate function that does not do the extra computations for ssz. |
| 1518 | static int64_t av1_block_error2_c(const tran_low_t *coeff, |
| 1519 | const tran_low_t *dqcoeff, |
| 1520 | const tran_low_t *ref, intptr_t block_size, |
| 1521 | int64_t *ssz) { |
| 1522 | int64_t error; |
Yushin Cho | ead552a | 2017-09-25 15:10:25 -0700 | [diff] [blame] | 1523 | int64_t ssz_trash; |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1524 | // Use the existing sse codes for calculating distortion of decoded signal: |
| 1525 | // i.e. (orig - decoded)^2 |
Yushin Cho | ead552a | 2017-09-25 15:10:25 -0700 | [diff] [blame] | 1526 | error = av1_block_error(coeff, dqcoeff, block_size, &ssz_trash); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1527 | // prediction residue^2 = (orig - ref)^2 |
Yushin Cho | ead552a | 2017-09-25 15:10:25 -0700 | [diff] [blame] | 1528 | *ssz = av1_block_error(coeff, ref, block_size, &ssz_trash); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1529 | return error; |
| 1530 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1531 | #endif // CONFIG_HIGHBITDEPTH |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1532 | #endif // CONFIG_PVQ |
| 1533 | |
Jingning Han | ab77e73 | 2017-02-28 15:20:59 -0800 | [diff] [blame] | 1534 | #if !CONFIG_PVQ || CONFIG_VAR_TX |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1535 | #if !CONFIG_LV_MAP |
| 1536 | static int cost_coeffs(const AV1_COMMON *const cm, MACROBLOCK *x, int plane, |
| 1537 | int block, TX_SIZE tx_size, const SCAN_ORDER *scan_order, |
| 1538 | const ENTROPY_CONTEXT *a, const ENTROPY_CONTEXT *l, |
| 1539 | int use_fast_coef_costing) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1540 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1541 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 1542 | const struct macroblock_plane *p = &x->plane[plane]; |
| 1543 | const struct macroblockd_plane *pd = &xd->plane[plane]; |
| 1544 | const PLANE_TYPE type = pd->plane_type; |
| 1545 | const uint16_t *band_count = &band_count_table[tx_size][1]; |
| 1546 | const int eob = p->eobs[block]; |
| 1547 | const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block); |
| 1548 | const int tx_size_ctx = txsize_sqr_map[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1549 | uint8_t token_cache[MAX_TX_SQUARE]; |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 1550 | int pt = combine_entropy_contexts(*a, *l); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1551 | int c, cost; |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 1552 | const int16_t *scan = scan_order->scan; |
| 1553 | const int16_t *nb = scan_order->neighbors; |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 1554 | const int ref = is_inter_block(mbmi); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1555 | int(*head_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] = |
| 1556 | x->token_head_costs[tx_size_ctx][type][ref]; |
| 1557 | int(*tail_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] = |
| 1558 | x->token_tail_costs[tx_size_ctx][type][ref]; |
| 1559 | const int seg_eob = av1_get_tx_eob(&cm->seg, mbmi->segment_id, tx_size); |
| 1560 | int eob_val; |
Thomas Davies | 1052575 | 2017-03-06 12:10:46 +0000 | [diff] [blame] | 1561 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1562 | #if CONFIG_HIGHBITDEPTH |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1563 | 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] | 1564 | #else |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1565 | const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, 8); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1566 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1567 | |
| 1568 | #if !CONFIG_VAR_TX && !CONFIG_SUPERTX |
| 1569 | // Check for consistency of tx_size with mode info |
hui su | 0c6244b | 2017-07-12 17:11:43 -0700 | [diff] [blame] | 1570 | assert(tx_size == av1_get_tx_size(plane, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1571 | #endif // !CONFIG_VAR_TX && !CONFIG_SUPERTX |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 1572 | (void)cm; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1573 | |
| 1574 | if (eob == 0) { |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1575 | // block zero |
| 1576 | cost = (*head_token_costs)[pt][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1577 | } else { |
| 1578 | if (use_fast_coef_costing) { |
| 1579 | int band_left = *band_count++; |
| 1580 | |
| 1581 | // dc token |
| 1582 | int v = qcoeff[0]; |
| 1583 | int16_t prev_t; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1584 | cost = av1_get_token_cost(v, &prev_t, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1585 | eob_val = (eob == 1) ? EARLY_EOB : NO_EOB; |
| 1586 | cost += av1_get_coeff_token_cost( |
| 1587 | 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] | 1588 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1589 | token_cache[0] = av1_pt_energy_class[prev_t]; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1590 | ++head_token_costs; |
| 1591 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1592 | |
| 1593 | // ac tokens |
| 1594 | for (c = 1; c < eob; c++) { |
| 1595 | const int rc = scan[c]; |
| 1596 | int16_t t; |
| 1597 | |
| 1598 | v = qcoeff[rc]; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1599 | cost += av1_get_token_cost(v, &t, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1600 | eob_val = |
| 1601 | (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB; |
| 1602 | cost += av1_get_coeff_token_cost(t, eob_val, 0, |
| 1603 | (*head_token_costs)[!prev_t], |
| 1604 | (*tail_token_costs)[!prev_t]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1605 | prev_t = t; |
| 1606 | if (!--band_left) { |
| 1607 | band_left = *band_count++; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1608 | ++head_token_costs; |
| 1609 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1610 | } |
| 1611 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1612 | } else { // !use_fast_coef_costing |
| 1613 | int band_left = *band_count++; |
| 1614 | |
| 1615 | // dc token |
| 1616 | int v = qcoeff[0]; |
| 1617 | int16_t tok; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1618 | cost = av1_get_token_cost(v, &tok, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1619 | eob_val = (eob == 1) ? EARLY_EOB : NO_EOB; |
| 1620 | cost += av1_get_coeff_token_cost(tok, eob_val, 1, (*head_token_costs)[pt], |
| 1621 | (*tail_token_costs)[pt]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1622 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1623 | token_cache[0] = av1_pt_energy_class[tok]; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1624 | ++head_token_costs; |
| 1625 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1626 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1627 | // ac tokens |
| 1628 | for (c = 1; c < eob; c++) { |
| 1629 | const int rc = scan[c]; |
| 1630 | |
| 1631 | v = qcoeff[rc]; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1632 | cost += av1_get_token_cost(v, &tok, cat6_bits); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1633 | pt = get_coef_context(nb, token_cache, c); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1634 | eob_val = |
| 1635 | (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB; |
| 1636 | cost += av1_get_coeff_token_cost( |
| 1637 | tok, eob_val, 0, (*head_token_costs)[pt], (*tail_token_costs)[pt]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1638 | token_cache[rc] = av1_pt_energy_class[tok]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1639 | if (!--band_left) { |
| 1640 | band_left = *band_count++; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1641 | ++head_token_costs; |
| 1642 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1643 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1644 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1648 | return cost; |
| 1649 | } |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1650 | #endif // !CONFIG_LV_MAP |
| 1651 | |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 1652 | 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] | 1653 | int blk_row, int blk_col, int block, TX_SIZE tx_size, |
| 1654 | const SCAN_ORDER *scan_order, const ENTROPY_CONTEXT *a, |
| 1655 | const ENTROPY_CONTEXT *l, int use_fast_coef_costing) { |
Angie Chiang | 3627de2 | 2017-08-18 20:15:59 -0700 | [diff] [blame] | 1656 | const AV1_COMMON *const cm = &cpi->common; |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1657 | #if !CONFIG_LV_MAP |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 1658 | (void)blk_row; |
| 1659 | (void)blk_col; |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 1660 | #if CONFIG_MRC_TX |
| 1661 | const MACROBLOCKD *xd = &x->e_mbd; |
| 1662 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 1663 | const TX_TYPE tx_type = av1_get_tx_type(xd->plane[plane].plane_type, xd, |
| 1664 | blk_row, blk_col, block, tx_size); |
| 1665 | const int is_inter = is_inter_block(mbmi); |
| 1666 | if (tx_type == MRC_DCT && ((is_inter && SIGNAL_MRC_MASK_INTER) || |
| 1667 | (!is_inter && SIGNAL_MRC_MASK_INTRA))) { |
| 1668 | const int mrc_mask_cost = |
| 1669 | av1_cost_color_map(x, plane, block, mbmi->sb_type, tx_size, MRC_MAP); |
| 1670 | return cost_coeffs(cm, x, plane, block, tx_size, scan_order, a, l, |
| 1671 | use_fast_coef_costing) + |
| 1672 | mrc_mask_cost; |
| 1673 | } |
| 1674 | #endif |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1675 | return cost_coeffs(cm, x, plane, block, tx_size, scan_order, a, l, |
| 1676 | use_fast_coef_costing); |
| 1677 | #else // !CONFIG_LV_MAP |
| 1678 | (void)scan_order; |
| 1679 | (void)use_fast_coef_costing; |
| 1680 | const MACROBLOCKD *xd = &x->e_mbd; |
| 1681 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 1682 | const struct macroblockd_plane *pd = &xd->plane[plane]; |
| 1683 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Timothy B. Terriberry | 81ec261 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 1684 | #if CONFIG_CHROMA_SUB8X8 |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1685 | const BLOCK_SIZE plane_bsize = |
| 1686 | AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd)); |
Timothy B. Terriberry | 81ec261 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 1687 | #elif CONFIG_CB4X4 |
| 1688 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1689 | #else // CONFIG_CB4X4 |
| 1690 | const BLOCK_SIZE plane_bsize = |
| 1691 | get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd); |
| 1692 | #endif // CONFIG_CB4X4 |
| 1693 | |
| 1694 | TXB_CTX txb_ctx; |
| 1695 | get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx); |
Angie Chiang | 3627de2 | 2017-08-18 20:15:59 -0700 | [diff] [blame] | 1696 | 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] | 1697 | &txb_ctx); |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1698 | #endif // !CONFIG_LV_MAP |
| 1699 | } |
Fergus Simpson | 0b96b47 | 2017-03-07 15:20:28 -0800 | [diff] [blame] | 1700 | #endif // !CONFIG_PVQ || CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1701 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1702 | // Get transform block visible dimensions cropped to the MI units. |
| 1703 | static void get_txb_dimensions(const MACROBLOCKD *xd, int plane, |
| 1704 | BLOCK_SIZE plane_bsize, int blk_row, int blk_col, |
| 1705 | BLOCK_SIZE tx_bsize, int *width, int *height, |
| 1706 | int *visible_width, int *visible_height) { |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 1707 | #if !(CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX)) |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1708 | assert(tx_bsize <= plane_bsize); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 1709 | #endif |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1710 | int txb_height = block_size_high[tx_bsize]; |
| 1711 | int txb_width = block_size_wide[tx_bsize]; |
| 1712 | const int block_height = block_size_high[plane_bsize]; |
| 1713 | const int block_width = block_size_wide[plane_bsize]; |
| 1714 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1715 | // TODO(aconverse@google.com): Investigate using crop_width/height here rather |
| 1716 | // than the MI size |
| 1717 | const int block_rows = |
| 1718 | (xd->mb_to_bottom_edge >= 0) |
| 1719 | ? block_height |
| 1720 | : (xd->mb_to_bottom_edge >> (3 + pd->subsampling_y)) + block_height; |
| 1721 | const int block_cols = |
| 1722 | (xd->mb_to_right_edge >= 0) |
| 1723 | ? block_width |
| 1724 | : (xd->mb_to_right_edge >> (3 + pd->subsampling_x)) + block_width; |
| 1725 | const int tx_unit_size = tx_size_wide_log2[0]; |
| 1726 | if (width) *width = txb_width; |
| 1727 | if (height) *height = txb_height; |
| 1728 | *visible_width = clamp(block_cols - (blk_col << tx_unit_size), 0, txb_width); |
| 1729 | *visible_height = |
| 1730 | clamp(block_rows - (blk_row << tx_unit_size), 0, txb_height); |
| 1731 | } |
| 1732 | |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1733 | // Compute the pixel domain distortion from src and dst on all visible 4x4s in |
| 1734 | // the |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1735 | // transform block. |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1736 | static unsigned pixel_dist(const AV1_COMP *const cpi, const MACROBLOCK *x, |
| 1737 | int plane, const uint8_t *src, const int src_stride, |
| 1738 | const uint8_t *dst, const int dst_stride, |
| 1739 | int blk_row, int blk_col, |
| 1740 | const BLOCK_SIZE plane_bsize, |
| 1741 | const BLOCK_SIZE tx_bsize) { |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1742 | int txb_rows, txb_cols, visible_rows, visible_cols; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1743 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1744 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1745 | get_txb_dimensions(xd, plane, plane_bsize, blk_row, blk_col, tx_bsize, |
| 1746 | &txb_cols, &txb_rows, &visible_cols, &visible_rows); |
| 1747 | assert(visible_rows > 0); |
| 1748 | assert(visible_cols > 0); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1749 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1750 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1751 | 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] | 1752 | return (unsigned)av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride, |
| 1753 | tx_bsize, txb_cols, txb_rows, visible_cols, |
| 1754 | visible_rows, x->qindex); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1755 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1756 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 1757 | unsigned sse = pixel_dist_visible_only(cpi, x, src, src_stride, dst, |
| 1758 | dst_stride, tx_bsize, txb_rows, |
| 1759 | txb_cols, visible_rows, visible_cols); |
| 1760 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1761 | return sse; |
| 1762 | } |
| 1763 | |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1764 | // Compute the pixel domain distortion from diff on all visible 4x4s in the |
| 1765 | // transform block. |
| 1766 | static int64_t pixel_diff_dist(const MACROBLOCK *x, int plane, |
| 1767 | const int16_t *diff, const int diff_stride, |
| 1768 | int blk_row, int blk_col, |
| 1769 | const BLOCK_SIZE plane_bsize, |
| 1770 | const BLOCK_SIZE tx_bsize) { |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1771 | int visible_rows, visible_cols; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1772 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1773 | #if CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1774 | int txb_height = block_size_high[tx_bsize]; |
| 1775 | int txb_width = block_size_wide[tx_bsize]; |
| 1776 | const int src_stride = x->plane[plane].src.stride; |
| 1777 | const int src_idx = (blk_row * src_stride + blk_col) << tx_size_wide_log2[0]; |
| 1778 | const uint8_t *src = &x->plane[plane].src.buf[src_idx]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1779 | #endif |
| 1780 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1781 | get_txb_dimensions(xd, plane, plane_bsize, blk_row, blk_col, tx_bsize, NULL, |
| 1782 | NULL, &visible_cols, &visible_rows); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1783 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1784 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1785 | 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] | 1786 | 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] | 1787 | txb_height, visible_cols, visible_rows, x->qindex); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1788 | else |
| 1789 | #endif |
| 1790 | return aom_sum_squares_2d_i16(diff, diff_stride, visible_cols, |
| 1791 | visible_rows); |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1794 | int av1_count_colors(const uint8_t *src, int stride, int rows, int cols) { |
| 1795 | int val_count[256]; |
| 1796 | memset(val_count, 0, sizeof(val_count)); |
| 1797 | for (int r = 0; r < rows; ++r) { |
| 1798 | for (int c = 0; c < cols; ++c) { |
| 1799 | ++val_count[src[r * stride + c]]; |
| 1800 | } |
| 1801 | } |
| 1802 | int n = 0; |
| 1803 | for (int i = 0; i < 256; ++i) { |
| 1804 | if (val_count[i]) ++n; |
| 1805 | } |
| 1806 | return n; |
| 1807 | } |
| 1808 | |
| 1809 | #if CONFIG_HIGHBITDEPTH |
| 1810 | int av1_count_colors_highbd(const uint8_t *src8, int stride, int rows, int cols, |
| 1811 | int bit_depth) { |
| 1812 | assert(bit_depth <= 12); |
| 1813 | const uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
| 1814 | int val_count[1 << 12]; |
| 1815 | memset(val_count, 0, (1 << 12) * sizeof(val_count[0])); |
| 1816 | for (int r = 0; r < rows; ++r) { |
| 1817 | for (int c = 0; c < cols; ++c) { |
| 1818 | ++val_count[src[r * stride + c]]; |
| 1819 | } |
| 1820 | } |
| 1821 | int n = 0; |
| 1822 | for (int i = 0; i < (1 << bit_depth); ++i) { |
| 1823 | if (val_count[i]) ++n; |
| 1824 | } |
| 1825 | return n; |
| 1826 | } |
| 1827 | #endif // CONFIG_HIGHBITDEPTH |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1828 | |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 1829 | void av1_dist_block(const AV1_COMP *cpi, MACROBLOCK *x, int plane, |
| 1830 | BLOCK_SIZE plane_bsize, int block, int blk_row, int blk_col, |
| 1831 | TX_SIZE tx_size, int64_t *out_dist, int64_t *out_sse, |
| 1832 | OUTPUT_STATUS output_status) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1833 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1834 | const struct macroblock_plane *const p = &x->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1835 | #if CONFIG_DIST_8X8 |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 1836 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1837 | #else // CONFIG_DIST_8X8 |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 1838 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1839 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 1840 | |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1841 | if (cpi->sf.use_transform_domain_distortion |
| 1842 | #if CONFIG_DIST_8X8 |
| 1843 | && !x->using_dist_8x8 |
| 1844 | #endif |
| 1845 | ) { |
hui su | d2f12ba | 2017-04-12 10:08:43 -0700 | [diff] [blame] | 1846 | // Transform domain distortion computation is more efficient as it does |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1847 | // not involve an inverse transform, but it is less accurate. |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1848 | const int buffer_length = tx_size_2d[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1849 | int64_t this_sse; |
Jingning Han | ff70545 | 2017-04-27 11:32:15 -0700 | [diff] [blame] | 1850 | int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1851 | tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
| 1852 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1853 | #if CONFIG_PVQ |
Yaowu Xu | d6ea71c | 2016-11-07 10:24:14 -0800 | [diff] [blame] | 1854 | tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1855 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1856 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1857 | const int bd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd : 8; |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1858 | *out_dist = av1_highbd_block_error2_c(coeff, dqcoeff, ref_coeff, |
| 1859 | buffer_length, &this_sse, bd) >> |
| 1860 | shift; |
| 1861 | #else |
| 1862 | *out_dist = av1_block_error2_c(coeff, dqcoeff, ref_coeff, buffer_length, |
| 1863 | &this_sse) >> |
| 1864 | shift; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1865 | #endif // CONFIG_HIGHBITDEPTH |
Yi Luo | d61e608 | 2017-05-26 16:14:39 -0700 | [diff] [blame] | 1866 | #else // !CONFIG_PVQ |
| 1867 | #if CONFIG_HIGHBITDEPTH |
| 1868 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 1869 | *out_dist = av1_highbd_block_error(coeff, dqcoeff, buffer_length, |
| 1870 | &this_sse, xd->bd) >> |
| 1871 | shift; |
| 1872 | else |
| 1873 | #endif |
| 1874 | *out_dist = |
| 1875 | av1_block_error(coeff, dqcoeff, buffer_length, &this_sse) >> shift; |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1876 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1877 | *out_sse = this_sse >> shift; |
| 1878 | } else { |
| 1879 | const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1880 | #if !CONFIG_PVQ || CONFIG_DIST_8X8 |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1881 | const int bsw = block_size_wide[tx_bsize]; |
| 1882 | const int bsh = block_size_high[tx_bsize]; |
Angie Chiang | 50910f6 | 2017-04-03 12:31:34 -0700 | [diff] [blame] | 1883 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1884 | const int src_stride = x->plane[plane].src.stride; |
| 1885 | const int dst_stride = xd->plane[plane].dst.stride; |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1886 | // Scale the transform block index to pixel unit. |
| 1887 | const int src_idx = (blk_row * src_stride + blk_col) |
| 1888 | << tx_size_wide_log2[0]; |
| 1889 | const int dst_idx = (blk_row * dst_stride + blk_col) |
| 1890 | << tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1891 | const uint8_t *src = &x->plane[plane].src.buf[src_idx]; |
| 1892 | const uint8_t *dst = &xd->plane[plane].dst.buf[dst_idx]; |
| 1893 | const tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 1894 | const uint16_t eob = p->eobs[block]; |
| 1895 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1896 | assert(cpi != NULL); |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1897 | assert(tx_size_wide_log2[0] == tx_size_high_log2[0]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1898 | |
Angie Chiang | c0cf6c0 | 2017-04-04 13:47:27 -0700 | [diff] [blame] | 1899 | { |
| 1900 | const int diff_stride = block_size_wide[plane_bsize]; |
| 1901 | const int diff_idx = (blk_row * diff_stride + blk_col) |
| 1902 | << tx_size_wide_log2[0]; |
| 1903 | const int16_t *diff = &p->src_diff[diff_idx]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1904 | *out_sse = pixel_diff_dist(x, plane, diff, diff_stride, blk_row, blk_col, |
| 1905 | plane_bsize, tx_bsize); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1906 | #if CONFIG_HIGHBITDEPTH |
hui su | b1cc1f9 | 2017-04-11 17:41:29 -0700 | [diff] [blame] | 1907 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 1908 | *out_sse = ROUND_POWER_OF_TWO(*out_sse, (xd->bd - 8) * 2); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1909 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | c0cf6c0 | 2017-04-04 13:47:27 -0700 | [diff] [blame] | 1910 | } |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 1911 | *out_sse *= 16; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1912 | |
| 1913 | if (eob) { |
Angie Chiang | 228cc18 | 2017-04-07 15:22:16 -0700 | [diff] [blame] | 1914 | if (output_status == OUTPUT_HAS_DECODED_PIXELS) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1915 | *out_dist = pixel_dist(cpi, x, plane, src, src_stride, dst, dst_stride, |
| 1916 | blk_row, blk_col, plane_bsize, tx_bsize); |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1917 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1918 | #if CONFIG_HIGHBITDEPTH |
Jingning Han | 6a9dbef | 2017-04-10 10:25:14 -0700 | [diff] [blame] | 1919 | uint8_t *recon; |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1920 | DECLARE_ALIGNED(16, uint16_t, recon16[MAX_TX_SQUARE]); |
Jingning Han | 6a9dbef | 2017-04-10 10:25:14 -0700 | [diff] [blame] | 1921 | |
| 1922 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 1923 | recon = CONVERT_TO_BYTEPTR(recon16); |
| 1924 | else |
| 1925 | recon = (uint8_t *)recon16; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1926 | #else |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1927 | DECLARE_ALIGNED(16, uint8_t, recon[MAX_TX_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1928 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1929 | |
Angie Chiang | 50910f6 | 2017-04-03 12:31:34 -0700 | [diff] [blame] | 1930 | #if !CONFIG_PVQ |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1931 | #if CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1932 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1933 | aom_highbd_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, |
| 1934 | NULL, 0, bsw, bsh, xd->bd); |
| 1935 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1936 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1937 | aom_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, NULL, |
| 1938 | 0, bsw, bsh); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1939 | #if CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1940 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1941 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | c0cf6c0 | 2017-04-04 13:47:27 -0700 | [diff] [blame] | 1942 | #else |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1943 | (void)dst; |
Angie Chiang | 50910f6 | 2017-04-03 12:31:34 -0700 | [diff] [blame] | 1944 | #endif // !CONFIG_PVQ |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 1945 | |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 1946 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 1947 | uint8_t *mrc_mask = BLOCK_OFFSET(xd->mrc_mask, block); |
| 1948 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1949 | const PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 1950 | TX_TYPE tx_type = |
| 1951 | av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size); |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 1952 | av1_inverse_transform_block(xd, dqcoeff, |
| 1953 | #if CONFIG_LGT |
| 1954 | xd->mi[0]->mbmi.mode, |
| 1955 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 1956 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 1957 | mrc_mask, |
| 1958 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 1959 | tx_type, tx_size, recon, MAX_TX_SIZE, eob); |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 1960 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1961 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1962 | if (x->using_dist_8x8 && plane == 0 && (bsw < 8 || bsh < 8)) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1963 | // Save decoded pixels for inter block in pd->pred to avoid |
| 1964 | // block_8x8_rd_txfm_daala_dist() need to produce them |
| 1965 | // by calling av1_inverse_transform_block() again. |
| 1966 | const int pred_stride = block_size_wide[plane_bsize]; |
| 1967 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 1968 | << tx_size_wide_log2[0]; |
| 1969 | int16_t *pred = &pd->pred[pred_idx]; |
| 1970 | int i, j; |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 1971 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 1972 | #if CONFIG_HIGHBITDEPTH |
| 1973 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1974 | for (j = 0; j < bsh; j++) |
| 1975 | for (i = 0; i < bsw; i++) |
| 1976 | pred[j * pred_stride + i] = |
| 1977 | CONVERT_TO_SHORTPTR(recon)[j * MAX_TX_SIZE + i]; |
| 1978 | } else { |
| 1979 | #endif |
| 1980 | for (j = 0; j < bsh; j++) |
| 1981 | for (i = 0; i < bsw; i++) |
| 1982 | pred[j * pred_stride + i] = recon[j * MAX_TX_SIZE + i]; |
| 1983 | #if CONFIG_HIGHBITDEPTH |
| 1984 | } |
| 1985 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1986 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1987 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1988 | *out_dist = |
| 1989 | pixel_dist(cpi, x, plane, src, src_stride, recon, MAX_TX_SIZE, |
| 1990 | blk_row, blk_col, plane_bsize, tx_bsize); |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1991 | } |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 1992 | *out_dist *= 16; |
| 1993 | } else { |
| 1994 | *out_dist = *out_sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1995 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1999 | static void block_rd_txfm(int plane, int block, int blk_row, int blk_col, |
| 2000 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) { |
| 2001 | struct rdcost_block_args *args = arg; |
| 2002 | MACROBLOCK *const x = args->x; |
| 2003 | MACROBLOCKD *const xd = &x->e_mbd; |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2004 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | e50f3ec | 2017-04-10 15:50:33 -0700 | [diff] [blame] | 2005 | const AV1_COMP *cpi = args->cpi; |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2006 | ENTROPY_CONTEXT *a = args->t_above + blk_col; |
| 2007 | ENTROPY_CONTEXT *l = args->t_left + blk_row; |
Angie Chiang | 18ad894 | 2017-04-11 12:37:07 -0700 | [diff] [blame] | 2008 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2009 | int64_t rd1, rd2, rd; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2010 | RD_STATS this_rd_stats; |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2011 | |
| 2012 | #if CONFIG_DIST_8X8 |
| 2013 | // If sub8x8 tx, 8x8 or larger partition, and luma channel, |
| 2014 | // dist-8x8 disables early skip, because the distortion metrics for |
| 2015 | // sub8x8 tx (MSE) and reference distortion from 8x8 or larger partition |
| 2016 | // (new distortion metric) are different. |
| 2017 | // Exception is: dist-8x8 is enabled but still MSE is used, |
| 2018 | // i.e. "--tune=" encoder option is not used. |
| 2019 | int disable_early_skip = |
| 2020 | x->using_dist_8x8 && plane == 0 && plane_bsize >= BLOCK_8X8 && |
| 2021 | (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4) && |
| 2022 | x->tune_metric != AOM_TUNE_PSNR; |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 2023 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 6341f5c | 2017-03-24 14:36:28 -0700 | [diff] [blame] | 2024 | |
Jingning Han | e65436b | 2017-06-14 09:26:11 -0700 | [diff] [blame] | 2025 | #if !CONFIG_SUPERTX && !CONFIG_VAR_TX |
hui su | 0c6244b | 2017-07-12 17:11:43 -0700 | [diff] [blame] | 2026 | assert(tx_size == av1_get_tx_size(plane, xd)); |
Yue Chen | 8e689e4 | 2017-06-02 10:56:10 -0700 | [diff] [blame] | 2027 | #endif // !CONFIG_SUPERTX |
Angie Chiang | bc2288c | 2017-04-09 15:41:17 -0700 | [diff] [blame] | 2028 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2029 | av1_init_rd_stats(&this_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2030 | |
| 2031 | if (args->exit_early) return; |
| 2032 | |
| 2033 | if (!is_inter_block(mbmi)) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 2034 | av1_predict_intra_block_facade(cm, xd, plane, block, blk_col, blk_row, |
| 2035 | tx_size); |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 2036 | av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size); |
| 2037 | } |
| 2038 | |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2039 | #if !CONFIG_TXK_SEL |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 2040 | // full forward transform and quantization |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2041 | const int coeff_ctx = combine_entropy_contexts(*a, *l); |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 2042 | #if DISABLE_TRELLISQ_SEARCH |
| 2043 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 2044 | coeff_ctx, AV1_XFORM_QUANT_B); |
| 2045 | #else |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 2046 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 2047 | coeff_ctx, AV1_XFORM_QUANT_FP); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2048 | |
| 2049 | const int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
| 2050 | tran_low_t *const coeff = BLOCK_OFFSET(x->plane[plane].coeff, block); |
| 2051 | tran_low_t *const dqcoeff = BLOCK_OFFSET(xd->plane[plane].dqcoeff, block); |
| 2052 | const int buffer_length = tx_size_2d[tx_size]; |
| 2053 | int64_t tmp_dist; |
| 2054 | int64_t tmp; |
| 2055 | #if CONFIG_HIGHBITDEPTH |
| 2056 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 2057 | tmp_dist = |
| 2058 | av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp, xd->bd) >> |
| 2059 | shift; |
| 2060 | else |
| 2061 | #endif |
| 2062 | tmp_dist = av1_block_error(coeff, dqcoeff, buffer_length, &tmp) >> shift; |
| 2063 | |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 2064 | if ( |
| 2065 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2066 | disable_early_skip || |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 2067 | #endif |
| 2068 | RDCOST(x->rdmult, 0, tmp_dist) + args->this_rd < args->best_rd) { |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2069 | av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size, |
Angie Chiang | 25645b7 | 2017-09-24 14:28:49 -0700 | [diff] [blame] | 2070 | a, l, 1); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 2071 | } else { |
| 2072 | args->exit_early = 1; |
| 2073 | return; |
| 2074 | } |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 2075 | #endif // DISABLE_TRELLISQ_SEARCH |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 2076 | |
Sarah Parker | c5ccd4c | 2017-08-03 11:27:50 -0700 | [diff] [blame] | 2077 | #if CONFIG_MRC_TX |
| 2078 | if (mbmi->tx_type == MRC_DCT && !mbmi->valid_mrc_mask) { |
| 2079 | args->exit_early = 1; |
| 2080 | return; |
| 2081 | } |
| 2082 | #endif // CONFIG_MRC_TX |
| 2083 | |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 2084 | if (!is_inter_block(mbmi)) { |
| 2085 | struct macroblock_plane *const p = &x->plane[plane]; |
Angie Chiang | bc2288c | 2017-04-09 15:41:17 -0700 | [diff] [blame] | 2086 | av1_inverse_transform_block_facade(xd, plane, block, blk_row, blk_col, |
| 2087 | p->eobs[block]); |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 2088 | av1_dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col, |
| 2089 | tx_size, &this_rd_stats.dist, &this_rd_stats.sse, |
| 2090 | OUTPUT_HAS_DECODED_PIXELS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2091 | } else { |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 2092 | av1_dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col, |
| 2093 | tx_size, &this_rd_stats.dist, &this_rd_stats.sse, |
| 2094 | OUTPUT_HAS_PREDICTED_PIXELS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2095 | } |
Luc Trudeau | aa94baa | 2017-04-27 10:52:21 -0400 | [diff] [blame] | 2096 | #if CONFIG_CFL |
Luc Trudeau | fcca37a | 2017-08-14 15:05:07 -0400 | [diff] [blame] | 2097 | if (plane == AOM_PLANE_Y && xd->cfl->store_y) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 2098 | #if CONFIG_CHROMA_SUB8X8 |
| 2099 | assert(!is_inter_block(mbmi) || plane_bsize < BLOCK_8X8); |
| 2100 | #else |
| 2101 | assert(!is_inter_block(mbmi)); |
| 2102 | #endif // CONFIG_CHROMA_SUB8X8 |
| 2103 | cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize); |
Luc Trudeau | aa94baa | 2017-04-27 10:52:21 -0400 | [diff] [blame] | 2104 | } |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 2105 | #endif // CONFIG_CFL |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2106 | rd = RDCOST(x->rdmult, 0, this_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2107 | if (args->this_rd + rd > args->best_rd) { |
| 2108 | args->exit_early = 1; |
| 2109 | return; |
| 2110 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2111 | #if !CONFIG_PVQ |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2112 | const PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2113 | const TX_TYPE tx_type = |
| 2114 | av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size); |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 2115 | 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] | 2116 | this_rd_stats.rate = |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 2117 | av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block, tx_size, |
| 2118 | scan_order, a, l, args->use_fast_coef_costing); |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2119 | #else // !CONFIG_PVQ |
| 2120 | this_rd_stats.rate = x->rate; |
| 2121 | #endif // !CONFIG_PVQ |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2122 | #else // !CONFIG_TXK_SEL |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2123 | av1_search_txk_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize, |
| 2124 | tx_size, a, l, args->use_fast_coef_costing, |
| 2125 | &this_rd_stats); |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2126 | #endif // !CONFIG_TXK_SEL |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2127 | |
| 2128 | #if !CONFIG_PVQ |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2129 | #if CONFIG_RD_DEBUG |
Angie Chiang | e94556b | 2016-11-09 10:59:30 -0800 | [diff] [blame] | 2130 | av1_update_txb_coeff_cost(&this_rd_stats, plane, tx_size, blk_row, blk_col, |
| 2131 | this_rd_stats.rate); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2132 | #endif // CONFIG_RD_DEBUG |
Yushin Cho | 6341f5c | 2017-03-24 14:36:28 -0700 | [diff] [blame] | 2133 | av1_set_txb_context(x, plane, block, tx_size, a, l); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2134 | #endif // !CONFIG_PVQ |
Angie Chiang | b3a12b5 | 2017-03-23 14:53:10 -0700 | [diff] [blame] | 2135 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2136 | rd1 = RDCOST(x->rdmult, this_rd_stats.rate, this_rd_stats.dist); |
| 2137 | rd2 = RDCOST(x->rdmult, 0, this_rd_stats.sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2138 | |
| 2139 | // TODO(jingning): temporarily enabled only for luma component |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2140 | rd = AOMMIN(rd1, rd2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2141 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2142 | #if !CONFIG_PVQ |
| 2143 | this_rd_stats.skip &= !x->plane[plane].eobs[block]; |
| 2144 | #else |
| 2145 | this_rd_stats.skip &= x->pvq_skip[plane]; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2146 | #endif // !CONFIG_PVQ |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2147 | av1_merge_rd_stats(&args->rd_stats, &this_rd_stats); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2148 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2149 | args->this_rd += rd; |
| 2150 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2151 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2152 | if (!disable_early_skip) |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2153 | #endif |
| 2154 | if (args->this_rd > args->best_rd) { |
| 2155 | args->exit_early = 1; |
| 2156 | return; |
| 2157 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2160 | #if CONFIG_DIST_8X8 |
| 2161 | static void dist_8x8_sub8x8_txfm_rd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2162 | BLOCK_SIZE bsize, |
| 2163 | struct rdcost_block_args *args) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2164 | MACROBLOCKD *const xd = &x->e_mbd; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2165 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 2166 | const struct macroblock_plane *const p = &x->plane[0]; |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 2167 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2168 | const int src_stride = p->src.stride; |
| 2169 | const int dst_stride = pd->dst.stride; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2170 | const uint8_t *src = &p->src.buf[0]; |
| 2171 | const uint8_t *dst = &pd->dst.buf[0]; |
| 2172 | const int16_t *pred = &pd->pred[0]; |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 2173 | int bw = block_size_wide[bsize]; |
| 2174 | int bh = block_size_high[bsize]; |
| 2175 | int visible_w = bw; |
| 2176 | int visible_h = bh; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2177 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2178 | int i, j; |
| 2179 | int64_t rd, rd1, rd2; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2180 | unsigned int tmp1, tmp2; |
| 2181 | int qindex = x->qindex; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2182 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2183 | assert((bw & 0x07) == 0); |
| 2184 | assert((bh & 0x07) == 0); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2185 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 2186 | get_txb_dimensions(xd, 0, bsize, 0, 0, bsize, &bw, &bh, &visible_w, |
| 2187 | &visible_h); |
| 2188 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 2189 | #if CONFIG_HIGHBITDEPTH |
| 2190 | uint8_t *pred8; |
| 2191 | DECLARE_ALIGNED(16, uint16_t, pred16[MAX_TX_SQUARE]); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2192 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 2193 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 2194 | pred8 = CONVERT_TO_BYTEPTR(pred16); |
| 2195 | else |
| 2196 | pred8 = (uint8_t *)pred16; |
| 2197 | #else |
| 2198 | DECLARE_ALIGNED(16, uint8_t, pred8[MAX_TX_SQUARE]); |
| 2199 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2200 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 2201 | #if CONFIG_HIGHBITDEPTH |
| 2202 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 2203 | for (j = 0; j < bh; j++) |
| 2204 | for (i = 0; i < bw; i++) |
| 2205 | CONVERT_TO_SHORTPTR(pred8)[j * bw + i] = pred[j * bw + i]; |
| 2206 | } else { |
| 2207 | #endif |
| 2208 | for (j = 0; j < bh; j++) |
Yaowu Xu | 7a47170 | 2017-09-29 08:38:37 -0700 | [diff] [blame] | 2209 | for (i = 0; i < bw; i++) pred8[j * bw + i] = (uint8_t)pred[j * bw + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 2210 | #if CONFIG_HIGHBITDEPTH |
| 2211 | } |
| 2212 | #endif // CONFIG_HIGHBITDEPTH |
| 2213 | |
Yushin Cho | fcddadf | 2017-08-30 13:49:38 -0700 | [diff] [blame] | 2214 | tmp1 = (unsigned)av1_dist_8x8(cpi, x, src, src_stride, pred8, bw, bsize, bw, |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 2215 | bh, visible_w, visible_h, qindex); |
Yushin Cho | fcddadf | 2017-08-30 13:49:38 -0700 | [diff] [blame] | 2216 | tmp2 = (unsigned)av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride, bsize, |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 2217 | bw, bh, visible_w, visible_h, qindex); |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 2218 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2219 | if (!is_inter_block(mbmi)) { |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 2220 | if (x->tune_metric == AOM_TUNE_PSNR) { |
| 2221 | assert(args->rd_stats.sse == tmp1 * 16); |
| 2222 | assert(args->rd_stats.dist == tmp2 * 16); |
| 2223 | } |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2224 | args->rd_stats.sse = (int64_t)tmp1 * 16; |
| 2225 | args->rd_stats.dist = (int64_t)tmp2 * 16; |
| 2226 | } else { |
| 2227 | // For inter mode, the decoded pixels are provided in pd->pred, |
| 2228 | // while the predicted pixels are in dst. |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame^] | 2229 | if (x->tune_metric == AOM_TUNE_PSNR) { |
| 2230 | assert(args->rd_stats.sse == tmp2 * 16); |
| 2231 | assert(args->rd_stats.dist == tmp1 * 16); |
| 2232 | } |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2233 | args->rd_stats.sse = (int64_t)tmp2 * 16; |
| 2234 | args->rd_stats.dist = (int64_t)tmp1 * 16; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2235 | } |
| 2236 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2237 | rd1 = RDCOST(x->rdmult, args->rd_stats.rate, args->rd_stats.dist); |
| 2238 | rd2 = RDCOST(x->rdmult, 0, args->rd_stats.sse); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2239 | rd = AOMMIN(rd1, rd2); |
| 2240 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2241 | args->rd_stats.rdcost = rd; |
| 2242 | args->this_rd = rd; |
Yushin Cho | 04eb959 | 2017-06-21 17:35:06 -0700 | [diff] [blame] | 2243 | |
| 2244 | if (args->this_rd > args->best_rd) args->exit_early = 1; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2245 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2246 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2247 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2248 | static void txfm_rd_in_plane(MACROBLOCK *x, const AV1_COMP *cpi, |
| 2249 | RD_STATS *rd_stats, int64_t ref_best_rd, int plane, |
| 2250 | BLOCK_SIZE bsize, TX_SIZE tx_size, |
| 2251 | int use_fast_coef_casting) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2252 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2253 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2254 | struct rdcost_block_args args; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2255 | av1_zero(args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2256 | args.x = x; |
| 2257 | args.cpi = cpi; |
| 2258 | args.best_rd = ref_best_rd; |
| 2259 | args.use_fast_coef_costing = use_fast_coef_casting; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2260 | av1_init_rd_stats(&args.rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2261 | |
| 2262 | if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size; |
| 2263 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2264 | 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] | 2265 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2266 | av1_foreach_transformed_block_in_plane(xd, bsize, plane, block_rd_txfm, |
| 2267 | &args); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2268 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 2269 | if (x->using_dist_8x8 && !args.exit_early && plane == 0 && |
| 2270 | bsize >= BLOCK_8X8 && |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2271 | (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4)) |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2272 | dist_8x8_sub8x8_txfm_rd(cpi, x, bsize, &args); |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2273 | #endif |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2274 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2275 | if (args.exit_early) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2276 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2277 | } else { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2278 | *rd_stats = args.rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | #if CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2283 | void av1_txfm_rd_in_plane_supertx(MACROBLOCK *x, const AV1_COMP *cpi, int *rate, |
| 2284 | int64_t *distortion, int *skippable, |
| 2285 | int64_t *sse, int64_t ref_best_rd, int plane, |
| 2286 | BLOCK_SIZE bsize, TX_SIZE tx_size, |
| 2287 | int use_fast_coef_casting) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2288 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2289 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 2290 | struct rdcost_block_args args; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2291 | av1_zero(args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2292 | args.cpi = cpi; |
| 2293 | args.x = x; |
| 2294 | args.best_rd = ref_best_rd; |
| 2295 | args.use_fast_coef_costing = use_fast_coef_casting; |
| 2296 | |
| 2297 | #if CONFIG_EXT_TX |
| 2298 | assert(tx_size < TX_SIZES); |
| 2299 | #endif // CONFIG_EXT_TX |
| 2300 | |
| 2301 | if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size; |
| 2302 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2303 | 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] | 2304 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2305 | block_rd_txfm(plane, 0, 0, 0, get_plane_block_size(bsize, pd), tx_size, |
| 2306 | &args); |
| 2307 | |
| 2308 | if (args.exit_early) { |
| 2309 | *rate = INT_MAX; |
| 2310 | *distortion = INT64_MAX; |
| 2311 | *sse = INT64_MAX; |
| 2312 | *skippable = 0; |
| 2313 | } else { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2314 | *distortion = args.rd_stats.dist; |
| 2315 | *rate = args.rd_stats.rate; |
| 2316 | *sse = args.rd_stats.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2317 | *skippable = !x->plane[plane].eobs[0]; |
| 2318 | } |
| 2319 | } |
| 2320 | #endif // CONFIG_SUPERTX |
| 2321 | |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2322 | static int tx_size_cost(const AV1_COMP *const cpi, const MACROBLOCK *const x, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2323 | BLOCK_SIZE bsize, TX_SIZE tx_size) { |
| 2324 | const AV1_COMMON *const cm = &cpi->common; |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2325 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 2326 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2327 | |
Rupert Swarbrick | 93c39e9 | 2017-07-12 11:11:02 +0100 | [diff] [blame] | 2328 | const int tx_select = cm->tx_mode == TX_MODE_SELECT && |
| 2329 | #if CONFIG_EXT_PARTITION_TYPES |
| 2330 | // Currently these block shapes can only use 4x4 |
| 2331 | // transforms |
| 2332 | mbmi->sb_type != BLOCK_4X16 && |
| 2333 | mbmi->sb_type != BLOCK_16X4 && |
| 2334 | #endif |
| 2335 | mbmi->sb_type >= BLOCK_8X8; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2336 | |
| 2337 | if (tx_select) { |
| 2338 | const int is_inter = is_inter_block(mbmi); |
| 2339 | const int tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize] |
| 2340 | : intra_tx_size_cat_lookup[bsize]; |
| 2341 | const TX_SIZE coded_tx_size = txsize_sqr_up_map[tx_size]; |
| 2342 | const int depth = tx_size_to_depth(coded_tx_size); |
| 2343 | const int tx_size_ctx = get_tx_size_context(xd); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2344 | int r_tx_size = x->tx_size_cost[tx_size_cat][tx_size_ctx][depth]; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 2345 | #if CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX) |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 2346 | if (is_quarter_tx_allowed(xd, mbmi, is_inter) && tx_size != coded_tx_size) |
| 2347 | r_tx_size += av1_cost_bit(cm->fc->quarter_tx_size_prob, |
| 2348 | tx_size == quarter_txsize_lookup[bsize]); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 2349 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2350 | return r_tx_size; |
| 2351 | } else { |
| 2352 | return 0; |
| 2353 | } |
| 2354 | } |
| 2355 | |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2356 | // TODO(angiebird): use this function whenever it's possible |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2357 | int av1_tx_type_cost(const AV1_COMMON *cm, const MACROBLOCK *x, |
| 2358 | const MACROBLOCKD *xd, BLOCK_SIZE bsize, int plane, |
| 2359 | TX_SIZE tx_size, TX_TYPE tx_type) { |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 2360 | if (plane > 0) return 0; |
| 2361 | |
Jingning Han | 243b66b | 2017-06-23 12:11:47 -0700 | [diff] [blame] | 2362 | #if CONFIG_VAR_TX |
| 2363 | tx_size = get_min_tx_size(tx_size); |
| 2364 | #endif |
| 2365 | |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2366 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 2367 | const int is_inter = is_inter_block(mbmi); |
| 2368 | #if CONFIG_EXT_TX |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2369 | if (get_ext_tx_types(tx_size, bsize, is_inter, cm->reduced_tx_set_used) > 1 && |
| 2370 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
| 2371 | const int ext_tx_set = |
| 2372 | get_ext_tx_set(tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
| 2373 | if (is_inter) { |
| 2374 | if (ext_tx_set > 0) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2375 | return x |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2376 | ->inter_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]][tx_type]; |
| 2377 | } else { |
| 2378 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2379 | return x->intra_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]] |
| 2380 | [mbmi->mode][tx_type]; |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2381 | } |
| 2382 | } |
| 2383 | #else |
| 2384 | (void)bsize; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2385 | (void)cm; |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2386 | if (tx_size < TX_32X32 && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 2387 | !FIXED_TX_TYPE) { |
| 2388 | if (is_inter) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2389 | return x->inter_tx_type_costs[tx_size][tx_type]; |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2390 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2391 | return x->intra_tx_type_costs[tx_size] |
| 2392 | [intra_mode_to_tx_type_context[mbmi->mode]] |
| 2393 | [tx_type]; |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | #endif // CONFIG_EXT_TX |
| 2397 | return 0; |
| 2398 | } |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2399 | static int64_t txfm_yrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2400 | RD_STATS *rd_stats, int64_t ref_best_rd, BLOCK_SIZE bs, |
| 2401 | TX_TYPE tx_type, int tx_size) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2402 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2403 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2404 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2405 | int64_t rd = INT64_MAX; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2406 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2407 | int s0, s1; |
| 2408 | const int is_inter = is_inter_block(mbmi); |
Jingning Han | bf9c6b7 | 2016-12-14 14:50:45 -0800 | [diff] [blame] | 2409 | const int tx_select = |
| 2410 | cm->tx_mode == TX_MODE_SELECT && mbmi->sb_type >= BLOCK_8X8; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2411 | |
| 2412 | const int r_tx_size = tx_size_cost(cpi, x, bs, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2413 | |
Yushin Cho | cd4f4a2 | 2017-07-10 18:19:05 -0700 | [diff] [blame] | 2414 | #if CONFIG_PVQ |
| 2415 | assert(tx_size >= TX_4X4); |
| 2416 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2417 | assert(skip_prob > 0); |
| 2418 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2419 | assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed_bsize(bs))); |
| 2420 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2421 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2422 | s0 = av1_cost_bit(skip_prob, 0); |
| 2423 | s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2424 | |
| 2425 | mbmi->tx_type = tx_type; |
| 2426 | mbmi->tx_size = tx_size; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2427 | 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] | 2428 | cpi->sf.use_fast_coef_costing); |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2429 | if (rd_stats->rate == INT_MAX) return INT64_MAX; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2430 | #if !CONFIG_TXK_SEL |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 2431 | int plane = 0; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2432 | 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] | 2433 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2434 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2435 | if (rd_stats->skip) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2436 | if (is_inter) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2437 | rd = RDCOST(x->rdmult, s1, rd_stats->sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2438 | } else { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2439 | 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] | 2440 | } |
| 2441 | } else { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2442 | rd = RDCOST(x->rdmult, rd_stats->rate + s0 + r_tx_size * tx_select, |
| 2443 | rd_stats->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2446 | if (tx_select) rd_stats->rate += r_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2447 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2448 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 2449 | !(rd_stats->skip)) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2450 | rd = AOMMIN(rd, RDCOST(x->rdmult, s1, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2451 | |
| 2452 | return rd; |
| 2453 | } |
| 2454 | |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2455 | static int skip_txfm_search(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs, |
| 2456 | TX_TYPE tx_type, TX_SIZE tx_size) { |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2457 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 2458 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2459 | const TX_SIZE max_tx_size = max_txsize_lookup[bs]; |
| 2460 | const int is_inter = is_inter_block(mbmi); |
| 2461 | int prune = 0; |
| 2462 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 2463 | // passing -1 in for tx_type indicates that all 1D |
| 2464 | // transforms should be considered for pruning |
| 2465 | prune = prune_tx_types(cpi, bs, x, xd, -1); |
| 2466 | |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 2467 | #if CONFIG_MRC_TX |
| 2468 | // MRC_DCT only implemented for TX_32X32 so only include this tx in |
| 2469 | // the search for TX_32X32 |
Sarah Parker | 2e08d96 | 2017-08-01 19:51:20 -0700 | [diff] [blame] | 2470 | if (tx_type == MRC_DCT && |
| 2471 | ((is_inter && !USE_MRC_INTER) || (!is_inter && !USE_MRC_INTRA) || |
| 2472 | tx_size != TX_32X32)) |
| 2473 | return 1; |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 2474 | #endif // CONFIG_MRC_TX |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2475 | if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) return 1; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2476 | if (FIXED_TX_TYPE && tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2477 | return 1; |
| 2478 | if (!is_inter && x->use_default_intra_tx_type && |
| 2479 | tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2480 | return 1; |
| 2481 | if (is_inter && x->use_default_inter_tx_type && |
| 2482 | tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2483 | return 1; |
| 2484 | if (max_tx_size >= TX_32X32 && tx_size == TX_4X4) return 1; |
| 2485 | #if CONFIG_EXT_TX |
| 2486 | const AV1_COMMON *const cm = &cpi->common; |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2487 | const TxSetType tx_set_type = |
| 2488 | get_ext_tx_set_type(tx_size, bs, is_inter, cm->reduced_tx_set_used); |
| 2489 | if (!av1_ext_tx_used[tx_set_type][tx_type]) return 1; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2490 | if (is_inter) { |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2491 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 2492 | if (!do_tx_type_search(tx_type, prune)) return 1; |
| 2493 | } |
| 2494 | } else { |
| 2495 | if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) { |
| 2496 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) return 1; |
| 2497 | } |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2498 | } |
| 2499 | #else // CONFIG_EXT_TX |
| 2500 | if (tx_size >= TX_32X32 && tx_type != DCT_DCT) return 1; |
| 2501 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 2502 | !do_tx_type_search(tx_type, prune)) |
| 2503 | return 1; |
| 2504 | #endif // CONFIG_EXT_TX |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
Angie Chiang | 64a2fb8 | 2017-07-06 10:15:16 -0700 | [diff] [blame] | 2508 | #if CONFIG_EXT_INTER && \ |
| 2509 | (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT || CONFIG_INTERINTRA) |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2510 | static int64_t estimate_yrd_for_sb(const AV1_COMP *const cpi, BLOCK_SIZE bs, |
| 2511 | MACROBLOCK *x, int *r, int64_t *d, int *s, |
| 2512 | int64_t *sse, int64_t ref_best_rd) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2513 | RD_STATS rd_stats; |
| 2514 | int64_t rd = txfm_yrd(cpi, x, &rd_stats, ref_best_rd, bs, DCT_DCT, |
| 2515 | max_txsize_lookup[bs]); |
| 2516 | *r = rd_stats.rate; |
| 2517 | *d = rd_stats.dist; |
| 2518 | *s = rd_stats.skip; |
| 2519 | *sse = rd_stats.sse; |
| 2520 | return rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2521 | } |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 2522 | #endif // CONFIG_EXT_INTER && (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2523 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2524 | 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] | 2525 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2526 | BLOCK_SIZE bs) { |
| 2527 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2528 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2529 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2530 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2531 | int64_t this_rd, best_rd = INT64_MAX; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2532 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
| 2533 | int s0 = av1_cost_bit(skip_prob, 0); |
| 2534 | int s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2535 | const int is_inter = is_inter_block(mbmi); |
| 2536 | int prune = 0; |
Angie Chiang | aa0c34b | 2017-04-25 12:25:38 -0700 | [diff] [blame] | 2537 | const int plane = 0; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2538 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2539 | |
| 2540 | 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] | 2541 | #if CONFIG_VAR_TX |
| 2542 | mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2543 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2544 | #if CONFIG_EXT_TX |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2545 | int ext_tx_set = |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 2546 | get_ext_tx_set(mbmi->tx_size, bs, is_inter, cm->reduced_tx_set_used); |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2547 | const TxSetType tx_set_type = |
| 2548 | 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] | 2549 | #endif // CONFIG_EXT_TX |
| 2550 | |
| 2551 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 2552 | #if CONFIG_EXT_TX |
| 2553 | prune = prune_tx_types(cpi, bs, x, xd, ext_tx_set); |
| 2554 | #else |
| 2555 | prune = prune_tx_types(cpi, bs, x, xd, 0); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2556 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2557 | #if CONFIG_EXT_TX |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 2558 | if (get_ext_tx_types(mbmi->tx_size, bs, is_inter, cm->reduced_tx_set_used) > |
| 2559 | 1 && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2560 | !xd->lossless[mbmi->segment_id]) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2561 | #if CONFIG_PVQ |
| 2562 | od_rollback_buffer pre_buf, post_buf; |
| 2563 | |
| 2564 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 2565 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2566 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2567 | |
| 2568 | for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) { |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2569 | if (!av1_ext_tx_used[tx_set_type][tx_type]) continue; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2570 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2571 | if (is_inter) { |
| 2572 | if (x->use_default_inter_tx_type && |
| 2573 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2574 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2575 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 2576 | if (!do_tx_type_search(tx_type, prune)) continue; |
| 2577 | } |
| 2578 | } else { |
| 2579 | if (x->use_default_intra_tx_type && |
| 2580 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2581 | continue; |
| 2582 | if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) { |
| 2583 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 2584 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | mbmi->tx_type = tx_type; |
| 2588 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2589 | 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] | 2590 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2591 | #if CONFIG_PVQ |
| 2592 | od_encode_rollback(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2593 | #endif // CONFIG_PVQ |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2594 | if (this_rd_stats.rate == INT_MAX) continue; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2595 | 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] | 2596 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2597 | if (this_rd_stats.skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2598 | this_rd = RDCOST(x->rdmult, s1, this_rd_stats.sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2599 | else |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2600 | this_rd = |
| 2601 | RDCOST(x->rdmult, this_rd_stats.rate + s0, this_rd_stats.dist); |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2602 | if (is_inter_block(mbmi) && !xd->lossless[mbmi->segment_id] && |
| 2603 | !this_rd_stats.skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2604 | 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] | 2605 | |
| 2606 | if (this_rd < best_rd) { |
| 2607 | best_rd = this_rd; |
| 2608 | best_tx_type = mbmi->tx_type; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2609 | *rd_stats = this_rd_stats; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2610 | #if CONFIG_PVQ |
| 2611 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2612 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2613 | } |
| 2614 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2615 | #if CONFIG_PVQ |
| 2616 | od_encode_rollback(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2617 | #endif // CONFIG_PVQ |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 2618 | } else { |
| 2619 | mbmi->tx_type = DCT_DCT; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2620 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size, |
| 2621 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2622 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2623 | #else // CONFIG_EXT_TX |
| 2624 | if (mbmi->tx_size < TX_32X32 && !xd->lossless[mbmi->segment_id]) { |
| 2625 | for (tx_type = 0; tx_type < TX_TYPES; ++tx_type) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2626 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2627 | if (!is_inter && x->use_default_intra_tx_type && |
| 2628 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2629 | continue; |
| 2630 | if (is_inter && x->use_default_inter_tx_type && |
| 2631 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2632 | continue; |
| 2633 | mbmi->tx_type = tx_type; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2634 | 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] | 2635 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2636 | if (this_rd_stats.rate == INT_MAX) continue; |
Angie Chiang | aa0c34b | 2017-04-25 12:25:38 -0700 | [diff] [blame] | 2637 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2638 | 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] | 2639 | if (is_inter) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2640 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 2641 | !do_tx_type_search(tx_type, prune)) |
| 2642 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2643 | } |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2644 | if (this_rd_stats.skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2645 | this_rd = RDCOST(x->rdmult, s1, this_rd_stats.sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2646 | else |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2647 | this_rd = |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2648 | RDCOST(x->rdmult, this_rd_stats.rate + s0, this_rd_stats.dist); |
| 2649 | if (is_inter && !xd->lossless[mbmi->segment_id] && !this_rd_stats.skip) |
| 2650 | 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] | 2651 | |
| 2652 | if (this_rd < best_rd) { |
| 2653 | best_rd = this_rd; |
| 2654 | best_tx_type = mbmi->tx_type; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2655 | *rd_stats = this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2656 | } |
| 2657 | } |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 2658 | } else { |
| 2659 | mbmi->tx_type = DCT_DCT; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2660 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size, |
| 2661 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2662 | } |
| 2663 | #endif // CONFIG_EXT_TX |
| 2664 | mbmi->tx_type = best_tx_type; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2665 | } |
| 2666 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2667 | 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] | 2668 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2669 | BLOCK_SIZE bs) { |
| 2670 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2671 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2672 | |
| 2673 | mbmi->tx_size = TX_4X4; |
| 2674 | mbmi->tx_type = DCT_DCT; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2675 | #if CONFIG_VAR_TX |
| 2676 | mbmi->min_tx_size = get_min_tx_size(TX_4X4); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2677 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2678 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2679 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size, |
| 2680 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2681 | } |
| 2682 | |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2683 | #if CONFIG_TXK_SEL || CONFIG_VAR_TX |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2684 | static INLINE int bsize_to_num_blk(BLOCK_SIZE bsize) { |
| 2685 | int num_blk = 1 << (num_pels_log2_lookup[bsize] - 2 * tx_size_wide_log2[0]); |
| 2686 | return num_blk; |
| 2687 | } |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2688 | #endif // CONFIG_TXK_SEL || CONFIG_VAR_TX |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2689 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2690 | 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] | 2691 | MACROBLOCK *x, RD_STATS *rd_stats, |
| 2692 | int64_t ref_best_rd, BLOCK_SIZE bs) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2693 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2694 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2695 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2696 | int64_t rd = INT64_MAX; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2697 | int n; |
| 2698 | int start_tx, end_tx; |
| 2699 | int64_t best_rd = INT64_MAX, last_rd = INT64_MAX; |
| 2700 | const TX_SIZE max_tx_size = max_txsize_lookup[bs]; |
| 2701 | TX_SIZE best_tx_size = max_tx_size; |
| 2702 | TX_TYPE best_tx_type = DCT_DCT; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2703 | #if CONFIG_TXK_SEL |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2704 | 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] | 2705 | #endif // CONFIG_TXK_SEL |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2706 | const int tx_select = cm->tx_mode == TX_MODE_SELECT; |
| 2707 | const int is_inter = is_inter_block(mbmi); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2708 | #if CONFIG_PVQ |
| 2709 | od_rollback_buffer buf; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2710 | od_encode_checkpoint(&x->daala_enc, &buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2711 | #endif // CONFIG_PVQ |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2712 | |
| 2713 | av1_invalid_rd_stats(rd_stats); |
| 2714 | |
| 2715 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2716 | int evaluate_rect_tx = 0; |
| 2717 | if (tx_select) { |
| 2718 | evaluate_rect_tx = is_rect_tx_allowed(xd, mbmi); |
| 2719 | } else { |
| 2720 | const TX_SIZE chosen_tx_size = |
| 2721 | tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 2722 | evaluate_rect_tx = is_rect_tx(chosen_tx_size); |
| 2723 | assert(IMPLIES(evaluate_rect_tx, is_rect_tx_allowed(xd, mbmi))); |
| 2724 | } |
| 2725 | if (evaluate_rect_tx) { |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2726 | TX_TYPE tx_start = DCT_DCT; |
| 2727 | TX_TYPE tx_end = TX_TYPES; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2728 | #if CONFIG_TXK_SEL |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2729 | // The tx_type becomes dummy when lv_map is on. The tx_type search will be |
| 2730 | // performed in av1_search_txk_type() |
| 2731 | tx_end = DCT_DCT + 1; |
| 2732 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2733 | TX_TYPE tx_type; |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2734 | for (tx_type = tx_start; tx_type < tx_end; ++tx_type) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2735 | if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) continue; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2736 | const TX_SIZE rect_tx_size = max_txsize_rect_lookup[bs]; |
| 2737 | RD_STATS this_rd_stats; |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2738 | const TxSetType tx_set_type = get_ext_tx_set_type( |
| 2739 | rect_tx_size, bs, is_inter, cm->reduced_tx_set_used); |
| 2740 | if (av1_ext_tx_used[tx_set_type][tx_type]) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2741 | rd = txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, |
| 2742 | rect_tx_size); |
Hui Su | da816a1 | 2017-08-18 14:46:02 -0700 | [diff] [blame] | 2743 | ref_best_rd = AOMMIN(rd, ref_best_rd); |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2744 | if (rd < best_rd) { |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2745 | #if CONFIG_TXK_SEL |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2746 | memcpy(best_txk_type, mbmi->txk_type, sizeof(best_txk_type[0]) * 256); |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2747 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2748 | best_tx_type = tx_type; |
| 2749 | best_tx_size = rect_tx_size; |
| 2750 | best_rd = rd; |
| 2751 | *rd_stats = this_rd_stats; |
| 2752 | } |
| 2753 | } |
Debargha Mukherjee | 094c943 | 2017-02-22 10:31:25 -0800 | [diff] [blame] | 2754 | #if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2755 | const int is_inter = is_inter_block(mbmi); |
| 2756 | if (mbmi->sb_type < BLOCK_8X8 && is_inter) break; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2757 | #endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2758 | } |
| 2759 | } |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 2760 | |
| 2761 | #if CONFIG_RECT_TX_EXT |
| 2762 | // test 1:4/4:1 tx |
| 2763 | int evaluate_quarter_tx = 0; |
| 2764 | if (is_quarter_tx_allowed(xd, mbmi, is_inter)) { |
| 2765 | if (tx_select) { |
| 2766 | evaluate_quarter_tx = 1; |
| 2767 | } else { |
| 2768 | const TX_SIZE chosen_tx_size = |
| 2769 | tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 2770 | evaluate_quarter_tx = chosen_tx_size == quarter_txsize_lookup[bs]; |
| 2771 | } |
| 2772 | } |
| 2773 | if (evaluate_quarter_tx) { |
| 2774 | TX_TYPE tx_start = DCT_DCT; |
| 2775 | TX_TYPE tx_end = TX_TYPES; |
| 2776 | #if CONFIG_TXK_SEL |
| 2777 | // The tx_type becomes dummy when lv_map is on. The tx_type search will be |
| 2778 | // performed in av1_search_txk_type() |
| 2779 | tx_end = DCT_DCT + 1; |
| 2780 | #endif |
| 2781 | TX_TYPE tx_type; |
| 2782 | for (tx_type = tx_start; tx_type < tx_end; ++tx_type) { |
| 2783 | if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) continue; |
| 2784 | const TX_SIZE tx_size = quarter_txsize_lookup[bs]; |
| 2785 | RD_STATS this_rd_stats; |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2786 | const TxSetType tx_set_type = |
| 2787 | get_ext_tx_set_type(tx_size, bs, is_inter, cm->reduced_tx_set_used); |
| 2788 | if (av1_ext_tx_used[tx_set_type][tx_type]) { |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 2789 | rd = |
| 2790 | txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, tx_size); |
| 2791 | if (rd < best_rd) { |
| 2792 | #if CONFIG_TXK_SEL |
| 2793 | memcpy(best_txk_type, mbmi->txk_type, |
| 2794 | sizeof(best_txk_type[0]) * num_blk); |
| 2795 | #endif |
| 2796 | best_tx_type = tx_type; |
| 2797 | best_tx_size = tx_size; |
| 2798 | best_rd = rd; |
| 2799 | *rd_stats = this_rd_stats; |
| 2800 | } |
| 2801 | } |
| 2802 | #if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
| 2803 | const int is_inter = is_inter_block(mbmi); |
| 2804 | if (mbmi->sb_type < BLOCK_8X8 && is_inter) break; |
| 2805 | #endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
| 2806 | } |
| 2807 | } |
| 2808 | #endif // CONFIG_RECT_TX_EXT |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2809 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2810 | |
| 2811 | if (tx_select) { |
| 2812 | start_tx = max_tx_size; |
| 2813 | end_tx = (max_tx_size >= TX_32X32) ? TX_8X8 : TX_4X4; |
| 2814 | } else { |
| 2815 | const TX_SIZE chosen_tx_size = |
| 2816 | tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 2817 | start_tx = chosen_tx_size; |
| 2818 | end_tx = chosen_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2821 | last_rd = INT64_MAX; |
| 2822 | for (n = start_tx; n >= end_tx; --n) { |
Sarah Parker | 3666192 | 2017-04-13 14:26:49 -0700 | [diff] [blame] | 2823 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2824 | if (is_rect_tx(n)) break; |
| 2825 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2826 | TX_TYPE tx_start = DCT_DCT; |
| 2827 | TX_TYPE tx_end = TX_TYPES; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2828 | #if CONFIG_TXK_SEL |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2829 | // The tx_type becomes dummy when lv_map is on. The tx_type search will be |
| 2830 | // performed in av1_search_txk_type() |
| 2831 | tx_end = DCT_DCT + 1; |
| 2832 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2833 | TX_TYPE tx_type; |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2834 | for (tx_type = tx_start; tx_type < tx_end; ++tx_type) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2835 | RD_STATS this_rd_stats; |
| 2836 | if (skip_txfm_search(cpi, x, bs, tx_type, n)) continue; |
| 2837 | rd = txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, n); |
| 2838 | #if CONFIG_PVQ |
| 2839 | od_encode_rollback(&x->daala_enc, &buf); |
| 2840 | #endif // CONFIG_PVQ |
| 2841 | // Early termination in transform size search. |
| 2842 | if (cpi->sf.tx_size_search_breakout && |
| 2843 | (rd == INT64_MAX || |
| 2844 | (this_rd_stats.skip == 1 && tx_type != DCT_DCT && n < start_tx) || |
| 2845 | (n < (int)max_tx_size && rd > last_rd))) |
| 2846 | break; |
| 2847 | |
| 2848 | last_rd = rd; |
Hui Su | da816a1 | 2017-08-18 14:46:02 -0700 | [diff] [blame] | 2849 | ref_best_rd = AOMMIN(rd, ref_best_rd); |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2850 | if (rd < best_rd) { |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2851 | #if CONFIG_TXK_SEL |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2852 | memcpy(best_txk_type, mbmi->txk_type, sizeof(best_txk_type[0]) * 256); |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2853 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2854 | best_tx_type = tx_type; |
| 2855 | best_tx_size = n; |
| 2856 | best_rd = rd; |
| 2857 | *rd_stats = this_rd_stats; |
| 2858 | } |
| 2859 | #if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
| 2860 | const int is_inter = is_inter_block(mbmi); |
| 2861 | if (mbmi->sb_type < BLOCK_8X8 && is_inter) break; |
| 2862 | #endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
| 2863 | } |
| 2864 | } |
| 2865 | mbmi->tx_size = best_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2866 | mbmi->tx_type = best_tx_type; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2867 | #if CONFIG_TXK_SEL |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2868 | memcpy(mbmi->txk_type, best_txk_type, sizeof(best_txk_type[0]) * 256); |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2869 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2870 | |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2871 | #if CONFIG_VAR_TX |
| 2872 | mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2873 | #endif // CONFIG_VAR_TX |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2874 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2875 | #if !CONFIG_EXT_TX |
| 2876 | if (mbmi->tx_size >= TX_32X32) assert(mbmi->tx_type == DCT_DCT); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2877 | #endif // !CONFIG_EXT_TX |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2878 | #if CONFIG_PVQ |
Yushin Cho | 403618e | 2016-11-09 10:45:32 -0800 | [diff] [blame] | 2879 | if (best_rd != INT64_MAX) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2880 | txfm_yrd(cpi, x, rd_stats, ref_best_rd, bs, best_tx_type, best_tx_size); |
Yushin Cho | 05f540a | 2016-11-08 22:12:51 -0800 | [diff] [blame] | 2881 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2882 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2885 | static void super_block_yrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2886 | RD_STATS *rd_stats, BLOCK_SIZE bs, |
| 2887 | int64_t ref_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2888 | MACROBLOCKD *xd = &x->e_mbd; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2889 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2890 | |
| 2891 | assert(bs == xd->mi[0]->mbmi.sb_type); |
| 2892 | |
Yaowu Xu | 1e2aae1 | 2017-02-27 16:33:14 -0800 | [diff] [blame] | 2893 | if (xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2894 | choose_smallest_tx_size(cpi, x, rd_stats, ref_best_rd, bs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2895 | } else if (cpi->sf.tx_size_search_method == USE_LARGESTALL) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2896 | choose_largest_tx_size(cpi, x, rd_stats, ref_best_rd, bs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2897 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2898 | 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] | 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | static int conditional_skipintra(PREDICTION_MODE mode, |
| 2903 | PREDICTION_MODE best_intra_mode) { |
| 2904 | if (mode == D117_PRED && best_intra_mode != V_PRED && |
| 2905 | best_intra_mode != D135_PRED) |
| 2906 | return 1; |
| 2907 | if (mode == D63_PRED && best_intra_mode != V_PRED && |
| 2908 | best_intra_mode != D45_PRED) |
| 2909 | return 1; |
| 2910 | if (mode == D207_PRED && best_intra_mode != H_PRED && |
| 2911 | best_intra_mode != D45_PRED) |
| 2912 | return 1; |
| 2913 | if (mode == D153_PRED && best_intra_mode != H_PRED && |
| 2914 | best_intra_mode != D135_PRED) |
| 2915 | return 1; |
| 2916 | return 0; |
| 2917 | } |
| 2918 | |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2919 | // Model based RD estimation for luma intra blocks. |
| 2920 | 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] | 2921 | BLOCK_SIZE bsize, int mode_cost) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 2922 | const AV1_COMMON *cm = &cpi->common; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2923 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2924 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 2925 | assert(!is_inter_block(mbmi)); |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2926 | RD_STATS this_rd_stats; |
| 2927 | int row, col; |
| 2928 | int64_t temp_sse, this_rd; |
| 2929 | const TX_SIZE tx_size = tx_size_from_tx_mode(bsize, cpi->common.tx_mode, 0); |
| 2930 | const int stepr = tx_size_high_unit[tx_size]; |
| 2931 | const int stepc = tx_size_wide_unit[tx_size]; |
| 2932 | const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
| 2933 | const int max_blocks_high = max_block_high(xd, bsize, 0); |
| 2934 | mbmi->tx_size = tx_size; |
| 2935 | // Prediction. |
Angie Chiang | 3d005e4 | 2017-04-02 16:31:35 -0700 | [diff] [blame] | 2936 | const int step = stepr * stepc; |
| 2937 | int block = 0; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2938 | for (row = 0; row < max_blocks_high; row += stepr) { |
| 2939 | for (col = 0; col < max_blocks_wide; col += stepc) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 2940 | 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] | 2941 | block += step; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2942 | } |
| 2943 | } |
| 2944 | // RD estimation. |
| 2945 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &this_rd_stats.rate, |
| 2946 | &this_rd_stats.dist, &this_rd_stats.skip, &temp_sse); |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 2947 | #if CONFIG_EXT_INTRA |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 2948 | if (av1_is_directional_mode(mbmi->mode, bsize) && |
| 2949 | av1_use_angle_delta(bsize)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 2950 | mode_cost += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 2951 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 2952 | } |
| 2953 | #endif // CONFIG_EXT_INTRA |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 2954 | #if CONFIG_FILTER_INTRA |
| 2955 | if (mbmi->mode == DC_PRED) { |
| 2956 | const aom_prob prob = cpi->common.fc->filter_intra_probs[0]; |
| 2957 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) { |
| 2958 | const int mode = mbmi->filter_intra_mode_info.filter_intra_mode[0]; |
| 2959 | mode_cost += (av1_cost_bit(prob, 1) + |
| 2960 | write_uniform_cost(FILTER_INTRA_MODES, mode)); |
| 2961 | } else { |
| 2962 | mode_cost += av1_cost_bit(prob, 0); |
| 2963 | } |
| 2964 | } |
| 2965 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2966 | this_rd = |
| 2967 | 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] | 2968 | return this_rd; |
| 2969 | } |
| 2970 | |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 2971 | // Extends 'color_map' array from 'orig_width x orig_height' to 'new_width x |
| 2972 | // new_height'. Extra rows and columns are filled in by copying last valid |
| 2973 | // row/column. |
| 2974 | static void extend_palette_color_map(uint8_t *const color_map, int orig_width, |
| 2975 | int orig_height, int new_width, |
| 2976 | int new_height) { |
| 2977 | int j; |
| 2978 | assert(new_width >= orig_width); |
| 2979 | assert(new_height >= orig_height); |
| 2980 | if (new_width == orig_width && new_height == orig_height) return; |
| 2981 | |
| 2982 | for (j = orig_height - 1; j >= 0; --j) { |
| 2983 | memmove(color_map + j * new_width, color_map + j * orig_width, orig_width); |
| 2984 | // Copy last column to extra columns. |
| 2985 | memset(color_map + j * new_width + orig_width, |
| 2986 | color_map[j * new_width + orig_width - 1], new_width - orig_width); |
| 2987 | } |
| 2988 | // Copy last row to extra rows. |
| 2989 | for (j = orig_height; j < new_height; ++j) { |
| 2990 | memcpy(color_map + j * new_width, color_map + (orig_height - 1) * new_width, |
| 2991 | new_width); |
| 2992 | } |
| 2993 | } |
| 2994 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2995 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 2996 | // Bias toward using colors in the cache. |
| 2997 | // TODO(huisu): Try other schemes to improve compression. |
| 2998 | static void optimize_palette_colors(uint16_t *color_cache, int n_cache, |
| 2999 | int n_colors, int stride, |
| 3000 | float *centroids) { |
| 3001 | if (n_cache <= 0) return; |
| 3002 | for (int i = 0; i < n_colors * stride; i += stride) { |
| 3003 | float min_diff = fabsf(centroids[i] - color_cache[0]); |
| 3004 | int idx = 0; |
| 3005 | for (int j = 1; j < n_cache; ++j) { |
| 3006 | float this_diff = fabsf(centroids[i] - color_cache[j]); |
| 3007 | if (this_diff < min_diff) { |
| 3008 | min_diff = this_diff; |
| 3009 | idx = j; |
| 3010 | } |
| 3011 | } |
| 3012 | if (min_diff < 1.5) centroids[i] = color_cache[idx]; |
| 3013 | } |
| 3014 | } |
| 3015 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 3016 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3017 | static int rd_pick_palette_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 3018 | BLOCK_SIZE bsize, int palette_ctx, |
| 3019 | int dc_mode_cost, MB_MODE_INFO *best_mbmi, |
| 3020 | uint8_t *best_palette_color_map, |
hui su | 78c611a | 2017-01-13 17:06:04 -0800 | [diff] [blame] | 3021 | int64_t *best_rd, int64_t *best_model_rd, |
| 3022 | int *rate, int *rate_tokenonly, |
| 3023 | int64_t *distortion, int *skippable) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3024 | int rate_overhead = 0; |
| 3025 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3026 | MODE_INFO *const mic = xd->mi[0]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3027 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3028 | assert(!is_inter_block(mbmi)); |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 3029 | assert(bsize >= BLOCK_8X8); |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3030 | int this_rate, colors, n; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3031 | const int src_stride = x->plane[0].src.stride; |
| 3032 | const uint8_t *const src = x->plane[0].src.buf; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3033 | uint8_t *const color_map = xd->plane[0].color_index_map; |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 3034 | int block_width, block_height, rows, cols; |
| 3035 | av1_get_block_dimensions(bsize, 0, xd, &block_width, &block_height, &rows, |
| 3036 | &cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3037 | |
| 3038 | assert(cpi->common.allow_screen_content_tools); |
| 3039 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3040 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3041 | if (cpi->common.use_highbitdepth) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3042 | colors = av1_count_colors_highbd(src, src_stride, rows, cols, |
| 3043 | cpi->common.bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3044 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3045 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3046 | colors = av1_count_colors(src, src_stride, rows, cols); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3047 | #if CONFIG_FILTER_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3048 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3049 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3050 | |
| 3051 | if (colors > 1 && colors <= 64) { |
Sarah Parker | 9c0e451 | 2017-08-15 16:23:53 -0700 | [diff] [blame] | 3052 | int r, c, i, k, palette_mode_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3053 | const int max_itr = 50; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3054 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 3055 | float centroids[PALETTE_MAX_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3056 | float lb, ub, val; |
hui su | 78c611a | 2017-01-13 17:06:04 -0800 | [diff] [blame] | 3057 | RD_STATS tokenonly_rd_stats; |
| 3058 | int64_t this_rd, this_model_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3059 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3060 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3061 | uint16_t *src16 = CONVERT_TO_SHORTPTR(src); |
| 3062 | if (cpi->common.use_highbitdepth) |
| 3063 | lb = ub = src16[0]; |
| 3064 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3065 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3066 | lb = ub = src[0]; |
| 3067 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3068 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3069 | if (cpi->common.use_highbitdepth) { |
| 3070 | for (r = 0; r < rows; ++r) { |
| 3071 | for (c = 0; c < cols; ++c) { |
| 3072 | val = src16[r * src_stride + c]; |
| 3073 | data[r * cols + c] = val; |
| 3074 | if (val < lb) |
| 3075 | lb = val; |
| 3076 | else if (val > ub) |
| 3077 | ub = val; |
| 3078 | } |
| 3079 | } |
| 3080 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3081 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3082 | for (r = 0; r < rows; ++r) { |
| 3083 | for (c = 0; c < cols; ++c) { |
| 3084 | val = src[r * src_stride + c]; |
| 3085 | data[r * cols + c] = val; |
| 3086 | if (val < lb) |
| 3087 | lb = val; |
| 3088 | else if (val > ub) |
| 3089 | ub = val; |
| 3090 | } |
| 3091 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3092 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3093 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3094 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3095 | |
| 3096 | mbmi->mode = DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3097 | #if CONFIG_FILTER_INTRA |
| 3098 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 3099 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3100 | |
| 3101 | if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return 0; |
| 3102 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 3103 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 3104 | uint16_t color_cache[2 * PALETTE_MAX_SIZE]; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 3105 | const int n_cache = av1_get_palette_cache(xd, 0, color_cache); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 3106 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 3107 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3108 | for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2; |
| 3109 | --n) { |
Urvang Joshi | 773e354 | 2017-05-05 18:09:42 -0700 | [diff] [blame] | 3110 | if (colors == PALETTE_MIN_SIZE) { |
| 3111 | // Special case: These colors automatically become the centroids. |
| 3112 | assert(colors == n); |
| 3113 | assert(colors == 2); |
| 3114 | centroids[0] = lb; |
| 3115 | centroids[1] = ub; |
| 3116 | k = 2; |
| 3117 | } else { |
| 3118 | for (i = 0; i < n; ++i) { |
| 3119 | centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2; |
| 3120 | } |
| 3121 | av1_k_means(data, centroids, color_map, rows * cols, n, 1, max_itr); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 3122 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 3123 | optimize_palette_colors(color_cache, n_cache, n, 1, centroids); |
| 3124 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
Urvang Joshi | 773e354 | 2017-05-05 18:09:42 -0700 | [diff] [blame] | 3125 | k = av1_remove_duplicates(centroids, n); |
| 3126 | if (k < PALETTE_MIN_SIZE) { |
| 3127 | // Too few unique colors to create a palette. And DC_PRED will work |
| 3128 | // well for that case anyway. So skip. |
| 3129 | continue; |
| 3130 | } |
Urvang Joshi | 5fb95f7 | 2017-05-05 17:36:16 -0700 | [diff] [blame] | 3131 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3132 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3133 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3134 | if (cpi->common.use_highbitdepth) |
| 3135 | for (i = 0; i < k; ++i) |
| 3136 | pmi->palette_colors[i] = |
| 3137 | clip_pixel_highbd((int)centroids[i], cpi->common.bit_depth); |
| 3138 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3139 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3140 | for (i = 0; i < k; ++i) |
| 3141 | pmi->palette_colors[i] = clip_pixel((int)centroids[i]); |
| 3142 | pmi->palette_size[0] = k; |
| 3143 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3144 | av1_calc_indices(data, centroids, color_map, rows * cols, k, 1); |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 3145 | extend_palette_color_map(color_map, cols, rows, block_width, |
| 3146 | block_height); |
hui su | 78c611a | 2017-01-13 17:06:04 -0800 | [diff] [blame] | 3147 | palette_mode_cost = |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 3148 | dc_mode_cost + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3149 | x->palette_y_size_cost[bsize - BLOCK_8X8][k - PALETTE_MIN_SIZE] + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3150 | write_uniform_cost(k, color_map[0]) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3151 | av1_cost_bit( |
| 3152 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3153 | 1); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 3154 | palette_mode_cost += av1_palette_color_cost_y(pmi, |
| 3155 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 3156 | color_cache, n_cache, |
| 3157 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 3158 | cpi->common.bit_depth); |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3159 | palette_mode_cost += |
| 3160 | av1_cost_color_map(x, 0, 0, bsize, mbmi->tx_size, PALETTE_MAP); |
hui su | 78c611a | 2017-01-13 17:06:04 -0800 | [diff] [blame] | 3161 | this_model_rd = intra_model_yrd(cpi, x, bsize, palette_mode_cost); |
| 3162 | if (*best_model_rd != INT64_MAX && |
| 3163 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 3164 | continue; |
| 3165 | if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd; |
| 3166 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 3167 | if (tokenonly_rd_stats.rate == INT_MAX) continue; |
| 3168 | this_rate = tokenonly_rd_stats.rate + palette_mode_cost; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3169 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
hui su | 8a63049 | 2017-01-10 18:22:41 -0800 | [diff] [blame] | 3170 | if (!xd->lossless[mbmi->segment_id] && mbmi->sb_type >= BLOCK_8X8) { |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3171 | tokenonly_rd_stats.rate -= tx_size_cost(cpi, x, bsize, mbmi->tx_size); |
hui su | 8a63049 | 2017-01-10 18:22:41 -0800 | [diff] [blame] | 3172 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3173 | if (this_rd < *best_rd) { |
| 3174 | *best_rd = this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3175 | memcpy(best_palette_color_map, color_map, |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 3176 | block_width * block_height * sizeof(color_map[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3177 | *best_mbmi = *mbmi; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3178 | rate_overhead = this_rate - tokenonly_rd_stats.rate; |
hui su | 8a63049 | 2017-01-10 18:22:41 -0800 | [diff] [blame] | 3179 | if (rate) *rate = this_rate; |
| 3180 | if (rate_tokenonly) *rate_tokenonly = tokenonly_rd_stats.rate; |
| 3181 | if (distortion) *distortion = tokenonly_rd_stats.dist; |
| 3182 | if (skippable) *skippable = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3183 | } |
| 3184 | } |
| 3185 | } |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3186 | |
| 3187 | if (best_mbmi->palette_mode_info.palette_size[0] > 0) { |
| 3188 | memcpy(color_map, best_palette_color_map, |
Luc Trudeau | 0401e89 | 2017-08-31 00:37:11 -0400 | [diff] [blame] | 3189 | block_width * block_height * sizeof(best_palette_color_map[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3190 | } |
| 3191 | *mbmi = *best_mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3192 | return rate_overhead; |
| 3193 | } |
| 3194 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3195 | static int64_t rd_pick_intra_sub_8x8_y_subblock_mode( |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3196 | const AV1_COMP *const cpi, MACROBLOCK *x, int row, int col, |
| 3197 | PREDICTION_MODE *best_mode, const int *bmode_costs, ENTROPY_CONTEXT *a, |
| 3198 | ENTROPY_CONTEXT *l, int *bestrate, int *bestratey, int64_t *bestdistortion, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3199 | BLOCK_SIZE bsize, TX_SIZE tx_size, int *y_skip, int64_t rd_thresh) { |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 3200 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3201 | PREDICTION_MODE mode; |
| 3202 | MACROBLOCKD *const xd = &x->e_mbd; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3203 | assert(!is_inter_block(&xd->mi[0]->mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3204 | int64_t best_rd = rd_thresh; |
| 3205 | struct macroblock_plane *p = &x->plane[0]; |
| 3206 | struct macroblockd_plane *pd = &xd->plane[0]; |
| 3207 | const int src_stride = p->src.stride; |
| 3208 | const int dst_stride = pd->dst.stride; |
| 3209 | const uint8_t *src_init = &p->src.buf[row * 4 * src_stride + col * 4]; |
Yushin Cho | 1a2df5e | 2017-01-09 13:36:13 -0800 | [diff] [blame] | 3210 | uint8_t *dst_init = &pd->dst.buf[row * 4 * dst_stride + col * 4]; |
Timothy B. Terriberry | fe67ed6 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 3211 | #if CONFIG_CHROMA_2X2 |
Jingning Han | 276c294 | 2016-12-05 12:37:02 -0800 | [diff] [blame] | 3212 | // TODO(jingning): This is a temporal change. The whole function should be |
| 3213 | // out when cb4x4 is enabled. |
| 3214 | ENTROPY_CONTEXT ta[4], tempa[4]; |
| 3215 | ENTROPY_CONTEXT tl[4], templ[4]; |
| 3216 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3217 | ENTROPY_CONTEXT ta[2], tempa[2]; |
| 3218 | ENTROPY_CONTEXT tl[2], templ[2]; |
Timothy B. Terriberry | fe67ed6 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 3219 | #endif // CONFIG_CHROMA_2X2 |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3220 | |
| 3221 | const int pred_width_in_4x4_blocks = num_4x4_blocks_wide_lookup[bsize]; |
| 3222 | const int pred_height_in_4x4_blocks = num_4x4_blocks_high_lookup[bsize]; |
| 3223 | const int tx_width_unit = tx_size_wide_unit[tx_size]; |
| 3224 | const int tx_height_unit = tx_size_high_unit[tx_size]; |
| 3225 | const int pred_block_width = block_size_wide[bsize]; |
| 3226 | const int pred_block_height = block_size_high[bsize]; |
| 3227 | const int tx_width = tx_size_wide[tx_size]; |
| 3228 | const int tx_height = tx_size_high[tx_size]; |
| 3229 | const int pred_width_in_transform_blocks = pred_block_width / tx_width; |
| 3230 | const int pred_height_in_transform_blocks = pred_block_height / tx_height; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3231 | int idx, idy; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3232 | int best_can_skip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3233 | uint8_t best_dst[8 * 8]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3234 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3235 | uint16_t best_dst16[8 * 8]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3236 | #endif // CONFIG_HIGHBITDEPTH |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3237 | const int is_lossless = xd->lossless[xd->mi[0]->mbmi.segment_id]; |
| 3238 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 3239 | const int sub_bsize = bsize; |
| 3240 | #else |
| 3241 | const int sub_bsize = BLOCK_4X4; |
| 3242 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3243 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3244 | #if CONFIG_PVQ |
| 3245 | od_rollback_buffer pre_buf, post_buf; |
| 3246 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 3247 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3248 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3249 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3250 | assert(bsize < BLOCK_8X8); |
| 3251 | assert(tx_width < 8 || tx_height < 8); |
| 3252 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
hui su | ff5e209 | 2017-03-27 10:07:59 -0700 | [diff] [blame] | 3253 | if (is_lossless) |
| 3254 | assert(tx_width == 4 && tx_height == 4); |
| 3255 | else |
| 3256 | assert(tx_width == pred_block_width && tx_height == pred_block_height); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3257 | #else |
| 3258 | assert(tx_width == 4 && tx_height == 4); |
| 3259 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 3260 | |
| 3261 | memcpy(ta, a, pred_width_in_transform_blocks * sizeof(a[0])); |
| 3262 | memcpy(tl, l, pred_height_in_transform_blocks * sizeof(l[0])); |
| 3263 | |
| 3264 | xd->mi[0]->mbmi.tx_size = tx_size; |
| 3265 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3266 | xd->mi[0]->mbmi.palette_mode_info.palette_size[0] = 0; |
| 3267 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3268 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3269 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3270 | #if CONFIG_PVQ |
| 3271 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 3272 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3273 | for (mode = DC_PRED; mode <= TM_PRED; ++mode) { |
| 3274 | int64_t this_rd; |
| 3275 | int ratey = 0; |
| 3276 | int64_t distortion = 0; |
| 3277 | int rate = bmode_costs[mode]; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3278 | int can_skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3279 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3280 | if (!(cpi->sf.intra_y_mode_mask[txsize_sqr_up_map[tx_size]] & |
| 3281 | (1 << mode))) |
| 3282 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3283 | |
| 3284 | // Only do the oblique modes if the best so far is |
| 3285 | // one of the neighboring directional modes |
| 3286 | if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 3287 | if (conditional_skipintra(mode, *best_mode)) continue; |
| 3288 | } |
| 3289 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3290 | memcpy(tempa, ta, pred_width_in_transform_blocks * sizeof(ta[0])); |
| 3291 | memcpy(templ, tl, pred_height_in_transform_blocks * sizeof(tl[0])); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3292 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3293 | for (idy = 0; idy < pred_height_in_transform_blocks; ++idy) { |
| 3294 | for (idx = 0; idx < pred_width_in_transform_blocks; ++idx) { |
| 3295 | const int block_raster_idx = (row + idy) * 2 + (col + idx); |
| 3296 | const int block = |
| 3297 | av1_raster_order_to_block_index(tx_size, block_raster_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3298 | const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride]; |
| 3299 | uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride]; |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3300 | #if !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3301 | int16_t *const src_diff = av1_raster_block_offset_int16( |
| 3302 | BLOCK_8X8, block_raster_idx, p->src_diff); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3303 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3304 | int skip; |
| 3305 | assert(block < 4); |
| 3306 | assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4, |
| 3307 | idx == 0 && idy == 0)); |
| 3308 | assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4, |
| 3309 | block == 0 || block == 2)); |
| 3310 | xd->mi[0]->bmi[block_raster_idx].as_mode = mode; |
David Barker | 839467f | 2017-01-19 11:06:15 +0000 | [diff] [blame] | 3311 | av1_predict_intra_block( |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 3312 | cm, xd, pd->width, pd->height, txsize_to_bsize[tx_size], mode, |
| 3313 | dst, dst_stride, dst, dst_stride, col + idx, row + idy, 0); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3314 | #if !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3315 | aom_highbd_subtract_block(tx_height, tx_width, src_diff, 8, src, |
| 3316 | src_stride, dst, dst_stride, xd->bd); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3317 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3318 | if (is_lossless) { |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 3319 | TX_TYPE tx_type = |
| 3320 | av1_get_tx_type(PLANE_TYPE_Y, xd, 0, 0, block, tx_size); |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 3321 | const SCAN_ORDER *scan_order = |
| 3322 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3323 | const int coeff_ctx = |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3324 | combine_entropy_contexts(tempa[idx], templ[idy]); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3325 | #if !CONFIG_PVQ |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 3326 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3327 | tx_size, coeff_ctx, AV1_XFORM_QUANT_FP); |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3328 | ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size, |
| 3329 | scan_order, tempa + idx, templ + idy, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 3330 | cpi->sf.use_fast_coef_costing); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3331 | skip = (p->eobs[block] == 0); |
| 3332 | can_skip &= skip; |
| 3333 | tempa[idx] = !skip; |
| 3334 | templ[idy] = !skip; |
| 3335 | #if CONFIG_EXT_TX |
| 3336 | if (tx_size == TX_8X4) { |
| 3337 | tempa[idx + 1] = tempa[idx]; |
| 3338 | } else if (tx_size == TX_4X8) { |
| 3339 | templ[idy + 1] = templ[idy]; |
| 3340 | } |
| 3341 | #endif // CONFIG_EXT_TX |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3342 | #else |
| 3343 | (void)scan_order; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3344 | |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3345 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
| 3346 | tx_size, coeff_ctx, AV1_XFORM_QUANT_B); |
| 3347 | |
| 3348 | ratey += x->rate; |
| 3349 | skip = x->pvq_skip[0]; |
| 3350 | tempa[idx] = !skip; |
| 3351 | templ[idy] = !skip; |
| 3352 | can_skip &= skip; |
| 3353 | #endif |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3354 | if (RDCOST(x->rdmult, ratey, distortion) >= best_rd) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3355 | goto next_highbd; |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3356 | #if CONFIG_PVQ |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3357 | if (!skip) |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3358 | #endif |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3359 | av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block), |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 3360 | #if CONFIG_LGT |
| 3361 | mode, |
| 3362 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3363 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 3364 | BLOCK_OFFSET(xd->mrc_mask, block), |
| 3365 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3366 | DCT_DCT, tx_size, dst, dst_stride, |
| 3367 | p->eobs[block]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3368 | } else { |
| 3369 | int64_t dist; |
| 3370 | unsigned int tmp; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 3371 | TX_TYPE tx_type = |
| 3372 | av1_get_tx_type(PLANE_TYPE_Y, xd, 0, 0, block, tx_size); |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 3373 | const SCAN_ORDER *scan_order = |
| 3374 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3375 | const int coeff_ctx = |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3376 | combine_entropy_contexts(tempa[idx], templ[idy]); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3377 | #if !CONFIG_PVQ |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 3378 | #if DISABLE_TRELLISQ_SEARCH |
| 3379 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
| 3380 | tx_size, coeff_ctx, AV1_XFORM_QUANT_B); |
| 3381 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 3382 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3383 | tx_size, coeff_ctx, AV1_XFORM_QUANT_FP); |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3384 | av1_optimize_b(cm, x, 0, 0, 0, block, BLOCK_8X8, tx_size, |
Angie Chiang | 25645b7 | 2017-09-24 14:28:49 -0700 | [diff] [blame] | 3385 | tempa + idx, templ + idy, 1); |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 3386 | #endif // DISABLE_TRELLISQ_SEARCH |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3387 | ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size, |
| 3388 | scan_order, tempa + idx, templ + idy, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 3389 | cpi->sf.use_fast_coef_costing); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3390 | skip = (p->eobs[block] == 0); |
| 3391 | can_skip &= skip; |
| 3392 | tempa[idx] = !skip; |
| 3393 | templ[idy] = !skip; |
| 3394 | #if CONFIG_EXT_TX |
| 3395 | if (tx_size == TX_8X4) { |
| 3396 | tempa[idx + 1] = tempa[idx]; |
| 3397 | } else if (tx_size == TX_4X8) { |
| 3398 | templ[idy + 1] = templ[idy]; |
| 3399 | } |
| 3400 | #endif // CONFIG_EXT_TX |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3401 | #else |
| 3402 | (void)scan_order; |
| 3403 | |
| 3404 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
| 3405 | tx_size, coeff_ctx, AV1_XFORM_QUANT_FP); |
| 3406 | ratey += x->rate; |
| 3407 | skip = x->pvq_skip[0]; |
| 3408 | tempa[idx] = !skip; |
| 3409 | templ[idy] = !skip; |
| 3410 | can_skip &= skip; |
| 3411 | #endif |
| 3412 | #if CONFIG_PVQ |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3413 | if (!skip) |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3414 | #endif |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3415 | av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block), |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 3416 | #if CONFIG_LGT |
| 3417 | mode, |
| 3418 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3419 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 3420 | BLOCK_OFFSET(xd->mrc_mask, block), |
| 3421 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3422 | tx_type, tx_size, dst, dst_stride, |
| 3423 | p->eobs[block]); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3424 | cpi->fn_ptr[sub_bsize].vf(src, src_stride, dst, dst_stride, &tmp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3425 | dist = (int64_t)tmp << 4; |
| 3426 | distortion += dist; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3427 | if (RDCOST(x->rdmult, ratey, distortion) >= best_rd) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3428 | goto next_highbd; |
| 3429 | } |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | rate += ratey; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3434 | this_rd = RDCOST(x->rdmult, rate, distortion); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3435 | |
| 3436 | if (this_rd < best_rd) { |
| 3437 | *bestrate = rate; |
| 3438 | *bestratey = ratey; |
| 3439 | *bestdistortion = distortion; |
| 3440 | best_rd = this_rd; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3441 | best_can_skip = can_skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3442 | *best_mode = mode; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3443 | memcpy(a, tempa, pred_width_in_transform_blocks * sizeof(tempa[0])); |
| 3444 | memcpy(l, templ, pred_height_in_transform_blocks * sizeof(templ[0])); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3445 | #if CONFIG_PVQ |
| 3446 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 3447 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3448 | for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3449 | memcpy(best_dst16 + idy * 8, |
| 3450 | CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride), |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3451 | pred_width_in_transform_blocks * 4 * sizeof(uint16_t)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3452 | } |
| 3453 | } |
| 3454 | next_highbd : {} |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3455 | #if CONFIG_PVQ |
| 3456 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 3457 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | if (best_rd >= rd_thresh) return best_rd; |
| 3461 | |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3462 | #if CONFIG_PVQ |
| 3463 | od_encode_rollback(&x->daala_enc, &post_buf); |
| 3464 | #endif |
| 3465 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3466 | if (y_skip) *y_skip &= best_can_skip; |
| 3467 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3468 | for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3469 | memcpy(CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride), |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3470 | best_dst16 + idy * 8, |
| 3471 | pred_width_in_transform_blocks * 4 * sizeof(uint16_t)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | return best_rd; |
| 3475 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3476 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3477 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3478 | #if CONFIG_PVQ |
| 3479 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3480 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3481 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3482 | for (mode = DC_PRED; mode <= TM_PRED; ++mode) { |
| 3483 | int64_t this_rd; |
| 3484 | int ratey = 0; |
| 3485 | int64_t distortion = 0; |
| 3486 | int rate = bmode_costs[mode]; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3487 | int can_skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3488 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3489 | if (!(cpi->sf.intra_y_mode_mask[txsize_sqr_up_map[tx_size]] & |
| 3490 | (1 << mode))) { |
| 3491 | continue; |
| 3492 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3493 | |
| 3494 | // Only do the oblique modes if the best so far is |
| 3495 | // one of the neighboring directional modes |
| 3496 | if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 3497 | if (conditional_skipintra(mode, *best_mode)) continue; |
| 3498 | } |
| 3499 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3500 | memcpy(tempa, ta, pred_width_in_transform_blocks * sizeof(ta[0])); |
| 3501 | memcpy(templ, tl, pred_height_in_transform_blocks * sizeof(tl[0])); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3502 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3503 | for (idy = 0; idy < pred_height_in_4x4_blocks; idy += tx_height_unit) { |
| 3504 | for (idx = 0; idx < pred_width_in_4x4_blocks; idx += tx_width_unit) { |
| 3505 | const int block_raster_idx = (row + idy) * 2 + (col + idx); |
| 3506 | int block = av1_raster_order_to_block_index(tx_size, block_raster_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3507 | const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride]; |
| 3508 | uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3509 | #if !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3510 | int16_t *const src_diff = av1_raster_block_offset_int16( |
| 3511 | BLOCK_8X8, block_raster_idx, p->src_diff); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3512 | #endif // !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3513 | int skip; |
| 3514 | assert(block < 4); |
| 3515 | assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4, |
| 3516 | idx == 0 && idy == 0)); |
| 3517 | assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4, |
| 3518 | block == 0 || block == 2)); |
| 3519 | xd->mi[0]->bmi[block_raster_idx].as_mode = mode; |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 3520 | av1_predict_intra_block(cm, xd, pd->width, pd->height, |
David Barker | 839467f | 2017-01-19 11:06:15 +0000 | [diff] [blame] | 3521 | txsize_to_bsize[tx_size], mode, dst, dst_stride, |
| 3522 | dst, dst_stride, |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3523 | #if CONFIG_CB4X4 |
| 3524 | 2 * (col + idx), 2 * (row + idy), |
| 3525 | #else |
| 3526 | col + idx, row + idy, |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3527 | #endif // CONFIG_CB4X4 |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3528 | 0); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3529 | #if !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3530 | aom_subtract_block(tx_height, tx_width, src_diff, 8, src, src_stride, |
| 3531 | dst, dst_stride); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3532 | #endif // !CONFIG_PVQ |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 3533 | TX_TYPE tx_type = |
| 3534 | av1_get_tx_type(PLANE_TYPE_Y, xd, 0, 0, block, tx_size); |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 3535 | const SCAN_ORDER *scan_order = |
| 3536 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3537 | const int coeff_ctx = combine_entropy_contexts(tempa[idx], templ[idy]); |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3538 | #if CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3539 | block = 4 * block; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3540 | #endif // CONFIG_CB4X4 |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3541 | #if !CONFIG_PVQ |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 3542 | #if DISABLE_TRELLISQ_SEARCH |
| 3543 | av1_xform_quant(cm, x, 0, block, |
| 3544 | #if CONFIG_CB4X4 |
| 3545 | 2 * (row + idy), 2 * (col + idx), |
| 3546 | #else |
| 3547 | row + idy, col + idx, |
| 3548 | #endif // CONFIG_CB4X4 |
| 3549 | BLOCK_8X8, tx_size, coeff_ctx, AV1_XFORM_QUANT_B); |
| 3550 | #else |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3551 | const AV1_XFORM_QUANT xform_quant = |
| 3552 | is_lossless ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP; |
| 3553 | av1_xform_quant(cm, x, 0, block, |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3554 | #if CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3555 | 2 * (row + idy), 2 * (col + idx), |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3556 | #else |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3557 | row + idy, col + idx, |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3558 | #endif // CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3559 | BLOCK_8X8, tx_size, coeff_ctx, xform_quant); |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3560 | |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3561 | av1_optimize_b(cm, x, 0, 0, 0, block, BLOCK_8X8, tx_size, tempa + idx, |
Angie Chiang | 25645b7 | 2017-09-24 14:28:49 -0700 | [diff] [blame] | 3562 | templ + idy, 1); |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 3563 | #endif // DISABLE_TRELLISQ_SEARCH |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3564 | ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size, scan_order, |
| 3565 | tempa + idx, templ + idy, |
| 3566 | cpi->sf.use_fast_coef_costing); |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3567 | skip = (p->eobs[block] == 0); |
| 3568 | can_skip &= skip; |
| 3569 | tempa[idx] = !skip; |
| 3570 | templ[idy] = !skip; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3571 | #if CONFIG_EXT_TX |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3572 | if (tx_size == TX_8X4) { |
| 3573 | tempa[idx + 1] = tempa[idx]; |
| 3574 | } else if (tx_size == TX_4X8) { |
| 3575 | templ[idy + 1] = templ[idy]; |
| 3576 | } |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3577 | #endif // CONFIG_EXT_TX |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3578 | #else |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3579 | (void)scan_order; |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3580 | |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3581 | av1_xform_quant(cm, x, 0, block, |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3582 | #if CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3583 | 2 * (row + idy), 2 * (col + idx), |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3584 | #else |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3585 | row + idy, col + idx, |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3586 | #endif // CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3587 | BLOCK_8X8, tx_size, coeff_ctx, AV1_XFORM_QUANT_FP); |
| 3588 | |
| 3589 | ratey += x->rate; |
| 3590 | skip = x->pvq_skip[0]; |
| 3591 | tempa[idx] = !skip; |
| 3592 | templ[idy] = !skip; |
| 3593 | can_skip &= skip; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3594 | #endif // !CONFIG_PVQ |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3595 | |
| 3596 | if (!is_lossless) { // To use the pixel domain distortion, we need to |
| 3597 | // calculate inverse txfm *before* calculating RD |
| 3598 | // cost. Compared to calculating the distortion in |
| 3599 | // the frequency domain, the overhead of encoding |
| 3600 | // effort is low. |
Yushin Cho | b27a17f | 2016-12-23 14:33:02 -0800 | [diff] [blame] | 3601 | #if CONFIG_PVQ |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3602 | if (!skip) |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3603 | #endif // CONFIG_PVQ |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3604 | av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block), |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 3605 | #if CONFIG_LGT |
| 3606 | mode, |
| 3607 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3608 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 3609 | BLOCK_OFFSET(xd->mrc_mask, block), |
| 3610 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3611 | tx_type, tx_size, dst, dst_stride, |
| 3612 | p->eobs[block]); |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3613 | unsigned int tmp; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3614 | cpi->fn_ptr[sub_bsize].vf(src, src_stride, dst, dst_stride, &tmp); |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3615 | const int64_t dist = (int64_t)tmp << 4; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3616 | distortion += dist; |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3617 | } |
| 3618 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3619 | if (RDCOST(x->rdmult, ratey, distortion) >= best_rd) goto next; |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3620 | |
| 3621 | if (is_lossless) { // Calculate inverse txfm *after* RD cost. |
| 3622 | #if CONFIG_PVQ |
| 3623 | if (!skip) |
| 3624 | #endif // CONFIG_PVQ |
| 3625 | av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block), |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 3626 | #if CONFIG_LGT |
| 3627 | mode, |
| 3628 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3629 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 3630 | BLOCK_OFFSET(xd->mrc_mask, block), |
| 3631 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3632 | DCT_DCT, tx_size, dst, dst_stride, |
| 3633 | p->eobs[block]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3634 | } |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | rate += ratey; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3639 | this_rd = RDCOST(x->rdmult, rate, distortion); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3640 | |
| 3641 | if (this_rd < best_rd) { |
| 3642 | *bestrate = rate; |
| 3643 | *bestratey = ratey; |
| 3644 | *bestdistortion = distortion; |
| 3645 | best_rd = this_rd; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3646 | best_can_skip = can_skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3647 | *best_mode = mode; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3648 | memcpy(a, tempa, pred_width_in_transform_blocks * sizeof(tempa[0])); |
| 3649 | memcpy(l, templ, pred_height_in_transform_blocks * sizeof(templ[0])); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3650 | #if CONFIG_PVQ |
| 3651 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3652 | #endif // CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3653 | for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3654 | memcpy(best_dst + idy * 8, dst_init + idy * dst_stride, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3655 | pred_width_in_transform_blocks * 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3656 | } |
| 3657 | next : {} |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3658 | #if CONFIG_PVQ |
| 3659 | od_encode_rollback(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3660 | #endif // CONFIG_PVQ |
| 3661 | } // mode decision loop |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3662 | |
| 3663 | if (best_rd >= rd_thresh) return best_rd; |
| 3664 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3665 | #if CONFIG_PVQ |
| 3666 | od_encode_rollback(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3667 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3668 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3669 | if (y_skip) *y_skip &= best_can_skip; |
| 3670 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3671 | for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3672 | memcpy(dst_init + idy * dst_stride, best_dst + idy * 8, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3673 | pred_width_in_transform_blocks * 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3674 | |
| 3675 | return best_rd; |
| 3676 | } |
| 3677 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3678 | static int64_t rd_pick_intra_sub_8x8_y_mode(const AV1_COMP *const cpi, |
| 3679 | MACROBLOCK *mb, int *rate, |
| 3680 | int *rate_y, int64_t *distortion, |
| 3681 | int *y_skip, int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3682 | const MACROBLOCKD *const xd = &mb->e_mbd; |
| 3683 | MODE_INFO *const mic = xd->mi[0]; |
| 3684 | const MODE_INFO *above_mi = xd->above_mi; |
| 3685 | const MODE_INFO *left_mi = xd->left_mi; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3686 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3687 | assert(!is_inter_block(mbmi)); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3688 | const BLOCK_SIZE bsize = mbmi->sb_type; |
| 3689 | const int pred_width_in_4x4_blocks = num_4x4_blocks_wide_lookup[bsize]; |
| 3690 | const int pred_height_in_4x4_blocks = num_4x4_blocks_high_lookup[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3691 | int idx, idy; |
| 3692 | int cost = 0; |
| 3693 | int64_t total_distortion = 0; |
| 3694 | int tot_rate_y = 0; |
| 3695 | int64_t total_rd = 0; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3696 | const int *bmode_costs = mb->mbmode_cost[0]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3697 | const int is_lossless = xd->lossless[mbmi->segment_id]; |
| 3698 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 3699 | const TX_SIZE tx_size = is_lossless ? TX_4X4 : max_txsize_rect_lookup[bsize]; |
| 3700 | #else |
| 3701 | const TX_SIZE tx_size = TX_4X4; |
| 3702 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3703 | |
| 3704 | #if CONFIG_EXT_INTRA |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3705 | #if CONFIG_INTRA_INTERP |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3706 | mbmi->intra_filter = INTRA_FILTER_LINEAR; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3707 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3708 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3709 | #if CONFIG_FILTER_INTRA |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3710 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3711 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3712 | |
| 3713 | // TODO(any): Add search of the tx_type to improve rd performance at the |
| 3714 | // expense of speed. |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3715 | mbmi->tx_type = DCT_DCT; |
| 3716 | mbmi->tx_size = tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3717 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3718 | if (y_skip) *y_skip = 1; |
| 3719 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3720 | // Pick modes for each prediction sub-block (of size 4x4, 4x8, or 8x4) in this |
| 3721 | // 8x8 coding block. |
| 3722 | for (idy = 0; idy < 2; idy += pred_height_in_4x4_blocks) { |
| 3723 | for (idx = 0; idx < 2; idx += pred_width_in_4x4_blocks) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3724 | PREDICTION_MODE best_mode = DC_PRED; |
| 3725 | int r = INT_MAX, ry = INT_MAX; |
| 3726 | int64_t d = INT64_MAX, this_rd = INT64_MAX; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3727 | int j; |
| 3728 | const int pred_block_idx = idy * 2 + idx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3729 | if (cpi->common.frame_type == KEY_FRAME) { |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3730 | const PREDICTION_MODE A = |
| 3731 | av1_above_block_mode(mic, above_mi, pred_block_idx); |
| 3732 | const PREDICTION_MODE L = |
| 3733 | av1_left_block_mode(mic, left_mi, pred_block_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3734 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3735 | bmode_costs = mb->y_mode_costs[A][L]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3736 | } |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3737 | this_rd = rd_pick_intra_sub_8x8_y_subblock_mode( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3738 | cpi, mb, idy, idx, &best_mode, bmode_costs, |
| 3739 | xd->plane[0].above_context + idx, xd->plane[0].left_context + idy, &r, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3740 | &ry, &d, bsize, tx_size, y_skip, best_rd - total_rd); |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 3741 | #if CONFIG_DIST_8X8 |
| 3742 | if (!cpi->oxcf.using_dist_8x8) |
| 3743 | #endif |
| 3744 | if (this_rd >= best_rd - total_rd) return INT64_MAX; |
| 3745 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3746 | total_rd += this_rd; |
| 3747 | cost += r; |
| 3748 | total_distortion += d; |
| 3749 | tot_rate_y += ry; |
| 3750 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3751 | mic->bmi[pred_block_idx].as_mode = best_mode; |
| 3752 | for (j = 1; j < pred_height_in_4x4_blocks; ++j) |
| 3753 | mic->bmi[pred_block_idx + j * 2].as_mode = best_mode; |
| 3754 | for (j = 1; j < pred_width_in_4x4_blocks; ++j) |
| 3755 | mic->bmi[pred_block_idx + j].as_mode = best_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3756 | |
| 3757 | if (total_rd >= best_rd) return INT64_MAX; |
| 3758 | } |
| 3759 | } |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3760 | mbmi->mode = mic->bmi[3].as_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3761 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3762 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 3763 | if (cpi->oxcf.using_dist_8x8) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 3764 | const struct macroblock_plane *p = &mb->plane[0]; |
| 3765 | const struct macroblockd_plane *pd = &xd->plane[0]; |
| 3766 | const int src_stride = p->src.stride; |
| 3767 | const int dst_stride = pd->dst.stride; |
| 3768 | uint8_t *src = p->src.buf; |
| 3769 | uint8_t *dst = pd->dst.buf; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 3770 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 3771 | // Daala-defined distortion computed for the block of 8x8 pixels |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 3772 | total_distortion = av1_dist_8x8(cpi, mb, src, src_stride, dst, dst_stride, |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3773 | BLOCK_8X8, 8, 8, 8, 8, mb->qindex) |
| 3774 | << 4; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 3775 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3776 | #endif // CONFIG_DIST_8X8 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3777 | // Add in the cost of the transform type |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3778 | if (!is_lossless) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3779 | int rate_tx_type = 0; |
| 3780 | #if CONFIG_EXT_TX |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 3781 | if (get_ext_tx_types(tx_size, bsize, 0, cpi->common.reduced_tx_set_used) > |
| 3782 | 1) { |
| 3783 | const int eset = |
| 3784 | get_ext_tx_set(tx_size, bsize, 0, cpi->common.reduced_tx_set_used); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3785 | rate_tx_type = mb->intra_tx_type_costs[eset][txsize_sqr_map[tx_size]] |
| 3786 | [mbmi->mode][mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3787 | } |
| 3788 | #else |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 3789 | rate_tx_type = |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3790 | mb->intra_tx_type_costs[txsize_sqr_map[tx_size]] |
| 3791 | [intra_mode_to_tx_type_context[mbmi->mode]] |
| 3792 | [mbmi->tx_type]; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3793 | #endif // CONFIG_EXT_TX |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3794 | assert(mbmi->tx_size == tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3795 | cost += rate_tx_type; |
| 3796 | tot_rate_y += rate_tx_type; |
| 3797 | } |
| 3798 | |
| 3799 | *rate = cost; |
| 3800 | *rate_y = tot_rate_y; |
| 3801 | *distortion = total_distortion; |
| 3802 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3803 | return RDCOST(mb->rdmult, cost, total_distortion); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3806 | #if CONFIG_FILTER_INTRA |
| 3807 | // Return 1 if an filter intra mode is selected; return 0 otherwise. |
| 3808 | static int rd_pick_filter_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 3809 | int *rate, int *rate_tokenonly, |
| 3810 | int64_t *distortion, int *skippable, |
| 3811 | BLOCK_SIZE bsize, int mode_cost, |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 3812 | int64_t *best_rd, int64_t *best_model_rd, |
| 3813 | uint16_t skip_mask) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3814 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3815 | MODE_INFO *const mic = xd->mi[0]; |
| 3816 | MB_MODE_INFO *mbmi = &mic->mbmi; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3817 | int filter_intra_selected_flag = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3818 | FILTER_INTRA_MODE mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3819 | TX_SIZE best_tx_size = TX_4X4; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3820 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3821 | TX_TYPE best_tx_type; |
| 3822 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3823 | av1_zero(filter_intra_mode_info); |
| 3824 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3825 | mbmi->mode = DC_PRED; |
| 3826 | mbmi->palette_mode_info.palette_size[0] = 0; |
| 3827 | |
| 3828 | for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) { |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 3829 | int this_rate; |
| 3830 | int64_t this_rd, this_model_rd; |
| 3831 | RD_STATS tokenonly_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3832 | if (skip_mask & (1 << mode)) continue; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3833 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = mode; |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 3834 | this_model_rd = intra_model_yrd(cpi, x, bsize, mode_cost); |
| 3835 | if (*best_model_rd != INT64_MAX && |
| 3836 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 3837 | continue; |
| 3838 | 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] | 3839 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 3840 | if (tokenonly_rd_stats.rate == INT_MAX) continue; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3841 | this_rate = tokenonly_rd_stats.rate + |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3842 | av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 1) + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3843 | write_uniform_cost(FILTER_INTRA_MODES, mode) + mode_cost; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3844 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3845 | |
| 3846 | if (this_rd < *best_rd) { |
| 3847 | *best_rd = this_rd; |
| 3848 | best_tx_size = mic->mbmi.tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3849 | filter_intra_mode_info = mbmi->filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3850 | best_tx_type = mic->mbmi.tx_type; |
| 3851 | *rate = this_rate; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3852 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 3853 | *distortion = tokenonly_rd_stats.dist; |
| 3854 | *skippable = tokenonly_rd_stats.skip; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3855 | filter_intra_selected_flag = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3859 | if (filter_intra_selected_flag) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3860 | mbmi->mode = DC_PRED; |
| 3861 | mbmi->tx_size = best_tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3862 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = |
| 3863 | filter_intra_mode_info.use_filter_intra_mode[0]; |
| 3864 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = |
| 3865 | filter_intra_mode_info.filter_intra_mode[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3866 | mbmi->tx_type = best_tx_type; |
| 3867 | return 1; |
| 3868 | } else { |
| 3869 | return 0; |
| 3870 | } |
| 3871 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3872 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3873 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3874 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3875 | // Run RD calculation with given luma intra prediction angle., and return |
| 3876 | // the RD cost. Update the best mode info. if the RD cost is the best so far. |
| 3877 | static int64_t calc_rd_given_intra_angle( |
| 3878 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mode_cost, |
| 3879 | int64_t best_rd_in, int8_t angle_delta, int max_angle_delta, int *rate, |
| 3880 | RD_STATS *rd_stats, int *best_angle_delta, TX_SIZE *best_tx_size, |
| 3881 | TX_TYPE *best_tx_type, |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3882 | #if CONFIG_INTRA_INTERP |
| 3883 | INTRA_FILTER *best_filter, |
| 3884 | #endif // CONFIG_INTRA_INTERP |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3885 | int64_t *best_rd, int64_t *best_model_rd) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3886 | int this_rate; |
| 3887 | RD_STATS tokenonly_rd_stats; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3888 | int64_t this_rd, this_model_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3889 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3890 | assert(!is_inter_block(mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3891 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3892 | mbmi->angle_delta[0] = angle_delta; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3893 | this_model_rd = intra_model_yrd(cpi, x, bsize, mode_cost); |
| 3894 | if (*best_model_rd != INT64_MAX && |
| 3895 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 3896 | return INT64_MAX; |
| 3897 | 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] | 3898 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in); |
| 3899 | if (tokenonly_rd_stats.rate == INT_MAX) return INT64_MAX; |
| 3900 | |
| 3901 | this_rate = tokenonly_rd_stats.rate + mode_cost + |
| 3902 | write_uniform_cost(2 * max_angle_delta + 1, |
| 3903 | mbmi->angle_delta[0] + max_angle_delta); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3904 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3905 | |
| 3906 | if (this_rd < *best_rd) { |
| 3907 | *best_rd = this_rd; |
| 3908 | *best_angle_delta = mbmi->angle_delta[0]; |
| 3909 | *best_tx_size = mbmi->tx_size; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3910 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3911 | *best_filter = mbmi->intra_filter; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3912 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3913 | *best_tx_type = mbmi->tx_type; |
| 3914 | *rate = this_rate; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3915 | rd_stats->rate = tokenonly_rd_stats.rate; |
| 3916 | rd_stats->dist = tokenonly_rd_stats.dist; |
| 3917 | rd_stats->skip = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3918 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3919 | return this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3920 | } |
| 3921 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3922 | // With given luma directional intra prediction mode, pick the best angle delta |
| 3923 | // Return the RD cost corresponding to the best angle delta. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3924 | 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] | 3925 | int *rate, RD_STATS *rd_stats, |
| 3926 | BLOCK_SIZE bsize, int mode_cost, |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3927 | int64_t best_rd, |
| 3928 | int64_t *best_model_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3929 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3930 | MODE_INFO *const mic = xd->mi[0]; |
| 3931 | MB_MODE_INFO *mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3932 | assert(!is_inter_block(mbmi)); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3933 | int i, angle_delta, best_angle_delta = 0; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3934 | int first_try = 1; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3935 | #if CONFIG_INTRA_INTERP |
| 3936 | int p_angle; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3937 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3938 | INTRA_FILTER filter, best_filter = INTRA_FILTER_LINEAR; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3939 | #endif // CONFIG_INTRA_INTERP |
Debargha Mukherjee | df0e0d7 | 2017-04-27 15:16:53 -0700 | [diff] [blame] | 3940 | 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] | 3941 | TX_SIZE best_tx_size = mic->mbmi.tx_size; |
| 3942 | TX_TYPE best_tx_type = mbmi->tx_type; |
| 3943 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3944 | 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] | 3945 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3946 | for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3947 | #if CONFIG_INTRA_INTERP |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3948 | for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) { |
| 3949 | if (FILTER_FAST_SEARCH && filter != INTRA_FILTER_LINEAR) continue; |
| 3950 | mic->mbmi.intra_filter = filter; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3951 | #endif // CONFIG_INTRA_INTERP |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3952 | for (i = 0; i < 2; ++i) { |
| 3953 | best_rd_in = (best_rd == INT64_MAX) |
| 3954 | ? INT64_MAX |
| 3955 | : (best_rd + (best_rd >> (first_try ? 3 : 5))); |
| 3956 | this_rd = calc_rd_given_intra_angle( |
| 3957 | cpi, x, bsize, |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3958 | #if CONFIG_INTRA_INTERP |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3959 | mode_cost + x->intra_filter_cost[intra_filter_ctx][filter], |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3960 | #else |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3961 | mode_cost, |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3962 | #endif // CONFIG_INTRA_INTERP |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3963 | best_rd_in, (1 - 2 * i) * angle_delta, MAX_ANGLE_DELTA, rate, |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3964 | rd_stats, &best_angle_delta, &best_tx_size, &best_tx_type, |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3965 | #if CONFIG_INTRA_INTERP |
| 3966 | &best_filter, |
| 3967 | #endif // CONFIG_INTRA_INTERP |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3968 | &best_rd, best_model_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3969 | rd_cost[2 * angle_delta + i] = this_rd; |
| 3970 | if (first_try && this_rd == INT64_MAX) return best_rd; |
| 3971 | first_try = 0; |
| 3972 | if (angle_delta == 0) { |
| 3973 | rd_cost[1] = this_rd; |
| 3974 | break; |
| 3975 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3976 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3977 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3978 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3979 | #endif // CONFIG_INTRA_INTERP |
| 3980 | } |
| 3981 | |
| 3982 | assert(best_rd != INT64_MAX); |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3983 | for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3984 | int64_t rd_thresh; |
| 3985 | #if CONFIG_INTRA_INTERP |
| 3986 | for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) { |
| 3987 | if (FILTER_FAST_SEARCH && filter != INTRA_FILTER_LINEAR) continue; |
| 3988 | mic->mbmi.intra_filter = filter; |
| 3989 | #endif // CONFIG_INTRA_INTERP |
| 3990 | for (i = 0; i < 2; ++i) { |
| 3991 | int skip_search = 0; |
| 3992 | rd_thresh = best_rd + (best_rd >> 5); |
| 3993 | if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh && |
| 3994 | rd_cost[2 * (angle_delta - 1) + i] > rd_thresh) |
| 3995 | skip_search = 1; |
| 3996 | if (!skip_search) { |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 3997 | calc_rd_given_intra_angle( |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3998 | cpi, x, bsize, |
| 3999 | #if CONFIG_INTRA_INTERP |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 4000 | mode_cost + x->intra_filter_cost[intra_filter_ctx][filter], |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4001 | #else |
| 4002 | mode_cost, |
| 4003 | #endif // CONFIG_INTRA_INTERP |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 4004 | best_rd, (1 - 2 * i) * angle_delta, MAX_ANGLE_DELTA, rate, |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4005 | rd_stats, &best_angle_delta, &best_tx_size, &best_tx_type, |
| 4006 | #if CONFIG_INTRA_INTERP |
| 4007 | &best_filter, |
| 4008 | #endif // CONFIG_INTRA_INTERP |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 4009 | &best_rd, best_model_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | #if CONFIG_INTRA_INTERP |
| 4013 | } |
| 4014 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4015 | } |
| 4016 | |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4017 | #if CONFIG_INTRA_INTERP |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4018 | if (FILTER_FAST_SEARCH && rd_stats->rate < INT_MAX) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 4019 | p_angle = mode_to_angle_map[mbmi->mode] + best_angle_delta * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4020 | if (av1_is_intra_filter_switchable(p_angle)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4021 | for (filter = INTRA_FILTER_LINEAR + 1; filter < INTRA_FILTERS; ++filter) { |
| 4022 | mic->mbmi.intra_filter = filter; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4023 | this_rd = calc_rd_given_intra_angle( |
| 4024 | cpi, x, bsize, |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 4025 | mode_cost + x->intra_filter_cost[intra_filter_ctx][filter], best_rd, |
| 4026 | best_angle_delta, MAX_ANGLE_DELTA, rate, rd_stats, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4027 | &best_angle_delta, &best_tx_size, &best_tx_type, &best_filter, |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 4028 | &best_rd, best_model_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | } |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4032 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4033 | |
| 4034 | mbmi->tx_size = best_tx_size; |
| 4035 | mbmi->angle_delta[0] = best_angle_delta; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4036 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4037 | mic->mbmi.intra_filter = best_filter; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4038 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4039 | mbmi->tx_type = best_tx_type; |
| 4040 | return best_rd; |
| 4041 | } |
| 4042 | |
| 4043 | // Indices are sign, integer, and fractional part of the gradient value |
| 4044 | static const uint8_t gradient_to_angle_bin[2][7][16] = { |
| 4045 | { |
| 4046 | { 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0 }, |
| 4047 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, |
| 4048 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 4049 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 4050 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 4051 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 4052 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 4053 | }, |
| 4054 | { |
| 4055 | { 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4 }, |
| 4056 | { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3 }, |
| 4057 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 4058 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 4059 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 4060 | { 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 4061 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 4062 | }, |
| 4063 | }; |
| 4064 | |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 4065 | /* clang-format off */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4066 | static const uint8_t mode_to_angle_bin[INTRA_MODES] = { |
| 4067 | 0, 2, 6, 0, 4, 3, 5, 7, 1, 0, |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4068 | 0, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4069 | }; |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 4070 | /* clang-format on */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4071 | |
| 4072 | 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] | 4073 | int cols, BLOCK_SIZE bsize, |
| 4074 | uint8_t *directional_mode_skip_mask) { |
| 4075 | memset(directional_mode_skip_mask, 0, |
| 4076 | INTRA_MODES * sizeof(*directional_mode_skip_mask)); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 4077 | // Check if angle_delta is used |
| 4078 | if (!av1_use_angle_delta(bsize)) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4079 | uint64_t hist[DIRECTIONAL_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4080 | memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0])); |
| 4081 | src += src_stride; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4082 | int r, c, dx, dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4083 | for (r = 1; r < rows; ++r) { |
| 4084 | for (c = 1; c < cols; ++c) { |
| 4085 | dx = src[c] - src[c - 1]; |
| 4086 | dy = src[c] - src[c - src_stride]; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4087 | int index; |
| 4088 | const int temp = dx * dx + dy * dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4089 | if (dy == 0) { |
| 4090 | index = 2; |
| 4091 | } else { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4092 | const int sn = (dx > 0) ^ (dy > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4093 | dx = abs(dx); |
| 4094 | dy = abs(dy); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4095 | const int remd = (dx % dy) * 16 / dy; |
| 4096 | const int quot = dx / dy; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4097 | index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4098 | } |
| 4099 | hist[index] += temp; |
| 4100 | } |
| 4101 | src += src_stride; |
| 4102 | } |
| 4103 | |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4104 | int i; |
| 4105 | uint64_t hist_sum = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4106 | for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i]; |
| 4107 | for (i = 0; i < INTRA_MODES; ++i) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4108 | if (av1_is_directional_mode(i, bsize)) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4109 | const uint8_t angle_bin = mode_to_angle_bin[i]; |
| 4110 | uint64_t score = 2 * hist[angle_bin]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4111 | int weight = 2; |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4112 | if (angle_bin > 0) { |
| 4113 | score += hist[angle_bin - 1]; |
| 4114 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4115 | } |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4116 | if (angle_bin < DIRECTIONAL_MODES - 1) { |
| 4117 | score += hist[angle_bin + 1]; |
| 4118 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4119 | } |
| 4120 | if (score * ANGLE_SKIP_THRESH < hist_sum * weight) |
| 4121 | directional_mode_skip_mask[i] = 1; |
| 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4126 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4127 | static void highbd_angle_estimation(const uint8_t *src8, int src_stride, |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4128 | int rows, int cols, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4129 | uint8_t *directional_mode_skip_mask) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4130 | memset(directional_mode_skip_mask, 0, |
| 4131 | INTRA_MODES * sizeof(*directional_mode_skip_mask)); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 4132 | // Check if angle_delta is used |
| 4133 | if (!av1_use_angle_delta(bsize)) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4134 | uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4135 | uint64_t hist[DIRECTIONAL_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4136 | memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0])); |
| 4137 | src += src_stride; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4138 | int r, c, dx, dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4139 | for (r = 1; r < rows; ++r) { |
| 4140 | for (c = 1; c < cols; ++c) { |
| 4141 | dx = src[c] - src[c - 1]; |
| 4142 | dy = src[c] - src[c - src_stride]; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4143 | int index; |
| 4144 | const int temp = dx * dx + dy * dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4145 | if (dy == 0) { |
| 4146 | index = 2; |
| 4147 | } else { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4148 | const int sn = (dx > 0) ^ (dy > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4149 | dx = abs(dx); |
| 4150 | dy = abs(dy); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4151 | const int remd = (dx % dy) * 16 / dy; |
| 4152 | const int quot = dx / dy; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4153 | index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4154 | } |
| 4155 | hist[index] += temp; |
| 4156 | } |
| 4157 | src += src_stride; |
| 4158 | } |
| 4159 | |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4160 | int i; |
| 4161 | uint64_t hist_sum = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4162 | for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i]; |
| 4163 | for (i = 0; i < INTRA_MODES; ++i) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4164 | if (av1_is_directional_mode(i, bsize)) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4165 | const uint8_t angle_bin = mode_to_angle_bin[i]; |
| 4166 | uint64_t score = 2 * hist[angle_bin]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4167 | int weight = 2; |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4168 | if (angle_bin > 0) { |
| 4169 | score += hist[angle_bin - 1]; |
| 4170 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4171 | } |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4172 | if (angle_bin < DIRECTIONAL_MODES - 1) { |
| 4173 | score += hist[angle_bin + 1]; |
| 4174 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4175 | } |
| 4176 | if (score * ANGLE_SKIP_THRESH < hist_sum * weight) |
| 4177 | directional_mode_skip_mask[i] = 1; |
| 4178 | } |
| 4179 | } |
| 4180 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4181 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4182 | #endif // CONFIG_EXT_INTRA |
| 4183 | |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 4184 | // Returns true if palette can be used for this block. |
| 4185 | static int can_use_palette(const AV1_COMP *const cpi, |
| 4186 | const MB_MODE_INFO *const mbmi) { |
| 4187 | return cpi->common.allow_screen_content_tools && mbmi->sb_type >= BLOCK_8X8 && |
| 4188 | mbmi->sb_type <= BLOCK_LARGEST; |
| 4189 | } |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 4190 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4191 | // This function is used only for intra_only frames |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4192 | static int64_t rd_pick_intra_sby_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 4193 | int *rate, int *rate_tokenonly, |
| 4194 | int64_t *distortion, int *skippable, |
| 4195 | BLOCK_SIZE bsize, int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4196 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4197 | MODE_INFO *const mic = xd->mi[0]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4198 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 4199 | assert(!is_inter_block(mbmi)); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4200 | MB_MODE_INFO best_mbmi = *mbmi; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 4201 | int64_t best_model_rd = INT64_MAX; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4202 | #if CONFIG_EXT_INTRA |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 4203 | const int rows = block_size_high[bsize]; |
| 4204 | const int cols = block_size_wide[bsize]; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4205 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4206 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4207 | #endif // CONFIG_INTRA_INTERP |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4208 | int is_directional_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4209 | uint8_t directional_mode_skip_mask[INTRA_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4210 | const int src_stride = x->plane[0].src.stride; |
| 4211 | const uint8_t *src = x->plane[0].src.buf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4212 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4213 | #if CONFIG_FILTER_INTRA |
| 4214 | int beat_best_rd = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4215 | uint16_t filter_intra_mode_skip_mask = (1 << FILTER_INTRA_MODES) - 1; |
| 4216 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4217 | const int *bmode_costs; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4218 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4219 | uint8_t *best_palette_color_map = |
| 4220 | cpi->common.allow_screen_content_tools |
| 4221 | ? x->palette_buffer->best_palette_color_map |
| 4222 | : NULL; |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4223 | int palette_y_mode_ctx = 0; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 4224 | const int try_palette = can_use_palette(cpi, mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4225 | const MODE_INFO *above_mi = xd->above_mi; |
| 4226 | const MODE_INFO *left_mi = xd->left_mi; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4227 | const PREDICTION_MODE A = av1_above_block_mode(mic, above_mi, 0); |
| 4228 | const PREDICTION_MODE L = av1_left_block_mode(mic, left_mi, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4229 | const PREDICTION_MODE FINAL_MODE_SEARCH = TM_PRED + 1; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4230 | #if CONFIG_PVQ |
| 4231 | od_rollback_buffer pre_buf, post_buf; |
| 4232 | |
| 4233 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 4234 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4235 | #endif // CONFIG_PVQ |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 4236 | bmode_costs = x->y_mode_costs[A][L]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4237 | |
| 4238 | #if CONFIG_EXT_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4239 | mbmi->angle_delta[0] = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4240 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4241 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4242 | highbd_angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4243 | directional_mode_skip_mask); |
| 4244 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4245 | #endif // CONFIG_HIGHBITDEPTH |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4246 | angle_estimation(src, src_stride, rows, cols, bsize, |
| 4247 | directional_mode_skip_mask); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4248 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4249 | #if CONFIG_FILTER_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4250 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4251 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4252 | pmi->palette_size[0] = 0; |
| 4253 | if (above_mi) |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4254 | palette_y_mode_ctx += |
| 4255 | (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4256 | if (left_mi) |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4257 | palette_y_mode_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4258 | |
| 4259 | if (cpi->sf.tx_type_search.fast_intra_tx_type_search) |
| 4260 | x->use_default_intra_tx_type = 1; |
| 4261 | else |
| 4262 | x->use_default_intra_tx_type = 0; |
| 4263 | |
| 4264 | /* Y Search for intra prediction mode */ |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 4265 | 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] | 4266 | RD_STATS this_rd_stats; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 4267 | int this_rate, this_rate_tokenonly, s; |
| 4268 | int64_t this_distortion, this_rd, this_model_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4269 | if (mode_idx == FINAL_MODE_SEARCH) { |
| 4270 | if (x->use_default_intra_tx_type == 0) break; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4271 | mbmi->mode = best_mbmi.mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4272 | x->use_default_intra_tx_type = 0; |
| 4273 | } else { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 4274 | assert(mode_idx < INTRA_MODES); |
| 4275 | mbmi->mode = intra_rd_search_mode_order[mode_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4276 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4277 | #if CONFIG_PVQ |
| 4278 | od_encode_rollback(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4279 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4280 | #if CONFIG_EXT_INTRA |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 4281 | mbmi->angle_delta[0] = 0; |
| 4282 | #endif // CONFIG_EXT_INTRA |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 4283 | 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] | 4284 | if (best_model_rd != INT64_MAX && |
| 4285 | this_model_rd > best_model_rd + (best_model_rd >> 1)) |
| 4286 | continue; |
| 4287 | if (this_model_rd < best_model_rd) best_model_rd = this_model_rd; |
| 4288 | #if CONFIG_EXT_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4289 | is_directional_mode = av1_is_directional_mode(mbmi->mode, bsize); |
| 4290 | if (is_directional_mode && directional_mode_skip_mask[mbmi->mode]) continue; |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 4291 | if (is_directional_mode && av1_use_angle_delta(bsize)) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4292 | this_rd_stats.rate = INT_MAX; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 4293 | rd_pick_intra_angle_sby(cpi, x, &this_rate, &this_rd_stats, bsize, |
| 4294 | bmode_costs[mbmi->mode], best_rd, &best_model_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4295 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 4296 | super_block_yrd(cpi, x, &this_rd_stats, bsize, best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4297 | } |
| 4298 | #else |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 4299 | super_block_yrd(cpi, x, &this_rd_stats, bsize, best_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4300 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 4301 | this_rate_tokenonly = this_rd_stats.rate; |
| 4302 | this_distortion = this_rd_stats.dist; |
| 4303 | s = this_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4304 | |
| 4305 | if (this_rate_tokenonly == INT_MAX) continue; |
| 4306 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4307 | this_rate = this_rate_tokenonly + bmode_costs[mbmi->mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4308 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4309 | if (!xd->lossless[mbmi->segment_id] && mbmi->sb_type >= BLOCK_8X8) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4310 | // super_block_yrd above includes the cost of the tx_size in the |
| 4311 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 4312 | // (prediction granularity), so we account for it in the full rate, |
| 4313 | // not the tokenonly rate. |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 4314 | this_rate_tokenonly -= tx_size_cost(cpi, x, bsize, mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4315 | } |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 4316 | if (try_palette && mbmi->mode == DC_PRED) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4317 | this_rate += |
| 4318 | av1_cost_bit(av1_default_palette_y_mode_prob[bsize - BLOCK_8X8] |
| 4319 | [palette_y_mode_ctx], |
| 4320 | 0); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 4321 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4322 | #if CONFIG_FILTER_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4323 | if (mbmi->mode == DC_PRED) |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4324 | this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 0); |
| 4325 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4326 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4327 | if (is_directional_mode) { |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4328 | #if CONFIG_INTRA_INTERP |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 4329 | const int p_angle = |
| 4330 | mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4331 | if (av1_is_intra_filter_switchable(p_angle)) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 4332 | this_rate += x->intra_filter_cost[intra_filter_ctx][mbmi->intra_filter]; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4333 | #endif // CONFIG_INTRA_INTERP |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 4334 | if (av1_use_angle_delta(bsize)) { |
| 4335 | this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 4336 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
| 4337 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4338 | } |
| 4339 | #endif // CONFIG_EXT_INTRA |
Hui Su | 8dc0092 | 2017-09-14 16:15:55 -0700 | [diff] [blame] | 4340 | #if CONFIG_INTRABC |
| 4341 | if (bsize >= BLOCK_8X8 && cpi->common.allow_screen_content_tools) |
| 4342 | this_rate += x->intrabc_cost[0]; |
| 4343 | #endif // CONFIG_INTRABC |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4344 | this_rd = RDCOST(x->rdmult, this_rate, this_distortion); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4345 | #if CONFIG_FILTER_INTRA |
Debargha Mukherjee | 1ae9f2c | 2016-10-04 14:30:16 -0700 | [diff] [blame] | 4346 | if (best_rd == INT64_MAX || this_rd - best_rd < (best_rd >> 4)) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4347 | filter_intra_mode_skip_mask ^= (1 << mbmi->mode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4348 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4349 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4350 | |
| 4351 | if (this_rd < best_rd) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4352 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4353 | best_rd = this_rd; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4354 | #if CONFIG_FILTER_INTRA |
| 4355 | beat_best_rd = 1; |
| 4356 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4357 | *rate = this_rate; |
| 4358 | *rate_tokenonly = this_rate_tokenonly; |
| 4359 | *distortion = this_distortion; |
| 4360 | *skippable = s; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4361 | #if CONFIG_PVQ |
| 4362 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4363 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4364 | } |
| 4365 | } |
| 4366 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4367 | #if CONFIG_PVQ |
| 4368 | od_encode_rollback(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4369 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4370 | |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 4371 | if (try_palette) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4372 | rd_pick_palette_intra_sby(cpi, x, bsize, palette_y_mode_ctx, |
| 4373 | bmode_costs[DC_PRED], &best_mbmi, |
| 4374 | best_palette_color_map, &best_rd, &best_model_rd, |
| 4375 | rate, rate_tokenonly, distortion, skippable); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4376 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4377 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4378 | #if CONFIG_FILTER_INTRA |
| 4379 | if (beat_best_rd) { |
| 4380 | if (rd_pick_filter_intra_sby(cpi, x, rate, rate_tokenonly, distortion, |
| 4381 | skippable, bsize, bmode_costs[DC_PRED], |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 4382 | &best_rd, &best_model_rd, |
| 4383 | filter_intra_mode_skip_mask)) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4384 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4385 | } |
| 4386 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4387 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4388 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4389 | *mbmi = best_mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4390 | return best_rd; |
| 4391 | } |
| 4392 | |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4393 | // Return value 0: early termination triggered, no valid rd cost available; |
| 4394 | // 1: rd cost values are valid. |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 4395 | static int super_block_uvrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 4396 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
| 4397 | int64_t ref_best_rd) { |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4398 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4399 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
hui su | 0c6244b | 2017-07-12 17:11:43 -0700 | [diff] [blame] | 4400 | const TX_SIZE uv_tx_size = av1_get_uv_tx_size(mbmi, &xd->plane[1]); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4401 | int plane; |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4402 | int is_cost_valid = 1; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 4403 | av1_init_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4404 | |
| 4405 | if (ref_best_rd < 0) is_cost_valid = 0; |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 4406 | |
Jingning Han | 31b6a4f | 2017-02-23 11:05:53 -0800 | [diff] [blame] | 4407 | #if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 4408 | if (x->skip_chroma_rd) return is_cost_valid; |
Jingning Han | 2d2dac2 | 2017-04-11 09:41:10 -0700 | [diff] [blame] | 4409 | |
| 4410 | bsize = scale_chroma_bsize(bsize, xd->plane[1].subsampling_x, |
| 4411 | xd->plane[1].subsampling_y); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4412 | #endif // CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 4413 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4414 | #if !CONFIG_PVQ |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4415 | if (is_inter_block(mbmi) && is_cost_valid) { |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4416 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) |
| 4417 | av1_subtract_plane(x, bsize, plane); |
| 4418 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4419 | #endif // !CONFIG_PVQ |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4420 | |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 4421 | if (is_cost_valid) { |
| 4422 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 4423 | RD_STATS pn_rd_stats; |
| 4424 | txfm_rd_in_plane(x, cpi, &pn_rd_stats, ref_best_rd, plane, bsize, |
| 4425 | uv_tx_size, cpi->sf.use_fast_coef_costing); |
| 4426 | if (pn_rd_stats.rate == INT_MAX) { |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 4427 | is_cost_valid = 0; |
| 4428 | break; |
| 4429 | } |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 4430 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4431 | if (RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) > ref_best_rd && |
| 4432 | RDCOST(x->rdmult, 0, rd_stats->sse) > ref_best_rd) { |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 4433 | is_cost_valid = 0; |
| 4434 | break; |
| 4435 | } |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4436 | } |
| 4437 | } |
| 4438 | |
| 4439 | if (!is_cost_valid) { |
| 4440 | // reset cost value |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 4441 | av1_invalid_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | return is_cost_valid; |
| 4445 | } |
| 4446 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4447 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4448 | void av1_tx_block_rd_b(const AV1_COMP *cpi, MACROBLOCK *x, TX_SIZE tx_size, |
| 4449 | int blk_row, int blk_col, int plane, int block, |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 4450 | int plane_bsize, const ENTROPY_CONTEXT *a, |
| 4451 | const ENTROPY_CONTEXT *l, RD_STATS *rd_stats) { |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 4452 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4453 | MACROBLOCKD *xd = &x->e_mbd; |
| 4454 | const struct macroblock_plane *const p = &x->plane[plane]; |
| 4455 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Jingning Han | 243b66b | 2017-06-23 12:11:47 -0700 | [diff] [blame] | 4456 | |
| 4457 | #if CONFIG_TXK_SEL |
| 4458 | av1_search_txk_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize, |
| 4459 | tx_size, a, l, 0, rd_stats); |
| 4460 | return; |
| 4461 | #endif |
| 4462 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4463 | int64_t tmp; |
| 4464 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 4465 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 4466 | uint8_t *mrc_mask = BLOCK_OFFSET(xd->mrc_mask, block); |
| 4467 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Luc Trudeau | 005feb6 | 2017-02-22 13:34:01 -0500 | [diff] [blame] | 4468 | PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 4469 | TX_TYPE tx_type = |
| 4470 | 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] | 4471 | const SCAN_ORDER *const scan_order = |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 4472 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4473 | BLOCK_SIZE txm_bsize = txsize_to_bsize[tx_size]; |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 4474 | int bh = block_size_high[txm_bsize]; |
| 4475 | int bw = block_size_wide[txm_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4476 | int src_stride = p->src.stride; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 4477 | uint8_t *src = |
| 4478 | &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]]; |
| 4479 | uint8_t *dst = |
| 4480 | &pd->dst |
| 4481 | .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] | 4482 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4483 | DECLARE_ALIGNED(16, uint16_t, rec_buffer16[MAX_TX_SQUARE]); |
| 4484 | uint8_t *rec_buffer; |
| 4485 | #else |
| 4486 | DECLARE_ALIGNED(16, uint8_t, rec_buffer[MAX_TX_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4487 | #endif // CONFIG_HIGHBITDEPTH |
Timothy B. Terriberry | d62e2a3 | 2017-06-10 16:04:21 -0700 | [diff] [blame] | 4488 | const int diff_stride = block_size_wide[plane_bsize]; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 4489 | const int16_t *diff = |
| 4490 | &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] | 4491 | int txb_coeff_cost; |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 4492 | |
| 4493 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4494 | |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 4495 | int coeff_ctx = get_entropy_context(tx_size, a, l); |
| 4496 | |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4497 | tmp = pixel_diff_dist(x, plane, diff, diff_stride, blk_row, blk_col, |
| 4498 | plane_bsize, txm_bsize); |
| 4499 | |
| 4500 | #if CONFIG_HIGHBITDEPTH |
| 4501 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 4502 | tmp = ROUND_POWER_OF_TWO(tmp, (xd->bd - 8) * 2); |
| 4503 | #endif // CONFIG_HIGHBITDEPTH |
| 4504 | rd_stats->sse += tmp << 4; |
| 4505 | |
| 4506 | if (rd_stats->invalid_rate) { |
| 4507 | rd_stats->dist += tmp << 4; |
| 4508 | rd_stats->rate += rd_stats->zero_rate; |
| 4509 | rd_stats->skip = 1; |
| 4510 | return; |
| 4511 | } |
| 4512 | |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 4513 | // TODO(any): Use av1_dist_block to compute distortion |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4514 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4515 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 4516 | rec_buffer = CONVERT_TO_BYTEPTR(rec_buffer16); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4517 | 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] | 4518 | 0, NULL, 0, bw, bh, xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4519 | } else { |
| 4520 | rec_buffer = (uint8_t *)rec_buffer16; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4521 | 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] | 4522 | NULL, 0, bw, bh); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4523 | } |
| 4524 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4525 | 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] | 4526 | 0, bw, bh); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4527 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4528 | |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4529 | #if DISABLE_TRELLISQ_SEARCH |
| 4530 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 4531 | coeff_ctx, AV1_XFORM_QUANT_B); |
| 4532 | |
| 4533 | #else |
| 4534 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 4535 | coeff_ctx, AV1_XFORM_QUANT_FP); |
| 4536 | |
| 4537 | const int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
| 4538 | tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
| 4539 | const int buffer_length = tx_size_2d[tx_size]; |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4540 | int64_t tmp_dist, tmp_sse; |
Yunqing Wang | 24d2d5d | 2017-09-20 09:45:13 -0700 | [diff] [blame] | 4541 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 4542 | int disable_early_skip = |
| 4543 | x->using_dist_8x8 && plane == 0 && plane_bsize >= BLOCK_8X8 && |
| 4544 | (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4) && |
| 4545 | x->tune_metric != AOM_TUNE_PSNR; |
Yunqing Wang | 24d2d5d | 2017-09-20 09:45:13 -0700 | [diff] [blame] | 4546 | #endif // CONFIG_DIST_8X8 |
| 4547 | |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4548 | #if CONFIG_HIGHBITDEPTH |
| 4549 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4550 | tmp_dist = av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp_sse, |
| 4551 | xd->bd) >> |
| 4552 | shift; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4553 | else |
| 4554 | #endif |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4555 | tmp_dist = |
| 4556 | av1_block_error(coeff, dqcoeff, buffer_length, &tmp_sse) >> shift; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4557 | |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4558 | #if CONFIG_MRC_TX |
| 4559 | if (tx_type == MRC_DCT && !xd->mi[0]->mbmi.valid_mrc_mask) { |
| 4560 | av1_invalid_rd_stats(rd_stats); |
| 4561 | return; |
| 4562 | } |
| 4563 | #endif // CONFIG_MRC_TX |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 4564 | if ( |
| 4565 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 4566 | disable_early_skip || |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 4567 | #endif |
| 4568 | RDCOST(x->rdmult, 0, tmp_dist) < rd_stats->ref_rdcost) { |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4569 | av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size, |
Angie Chiang | 25645b7 | 2017-09-24 14:28:49 -0700 | [diff] [blame] | 4570 | a, l, 1); |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4571 | } else { |
| 4572 | rd_stats->rate += rd_stats->zero_rate; |
| 4573 | rd_stats->dist += tmp_sse >> shift; |
| 4574 | rd_stats->sse += tmp_sse >> shift; |
| 4575 | rd_stats->skip = 1; |
| 4576 | rd_stats->invalid_rate = 1; |
| 4577 | return; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4578 | } |
| 4579 | #endif // DISABLE_TRELLISQ_SEARCH |
| 4580 | |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 4581 | const int eob = p->eobs[block]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4582 | |
Lester Lu | 918fe69 | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 4583 | av1_inverse_transform_block(xd, dqcoeff, |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 4584 | #if CONFIG_LGT |
Lester Lu | 918fe69 | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 4585 | xd->mi[0]->mbmi.mode, |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 4586 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 4587 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 4588 | mrc_mask, |
| 4589 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Lester Lu | 918fe69 | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 4590 | tx_type, tx_size, rec_buffer, MAX_TX_SIZE, eob); |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 4591 | if (eob > 0) { |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4592 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4593 | if (x->using_dist_8x8 && plane == 0 && (bw < 8 && bh < 8)) { |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4594 | // Save sub8x8 luma decoded pixels |
| 4595 | // since 8x8 luma decoded pixels are not available for daala-dist |
| 4596 | // after recursive split of BLOCK_8x8 is done. |
| 4597 | const int pred_stride = block_size_wide[plane_bsize]; |
| 4598 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 4599 | << tx_size_wide_log2[0]; |
| 4600 | int16_t *decoded = &pd->pred[pred_idx]; |
| 4601 | int i, j; |
| 4602 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4603 | #if CONFIG_HIGHBITDEPTH |
| 4604 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 4605 | for (j = 0; j < bh; j++) |
| 4606 | for (i = 0; i < bw; i++) |
| 4607 | decoded[j * pred_stride + i] = |
| 4608 | CONVERT_TO_SHORTPTR(rec_buffer)[j * MAX_TX_SIZE + i]; |
| 4609 | } else { |
| 4610 | #endif |
| 4611 | for (j = 0; j < bh; j++) |
| 4612 | for (i = 0; i < bw; i++) |
| 4613 | decoded[j * pred_stride + i] = rec_buffer[j * MAX_TX_SIZE + i]; |
| 4614 | #if CONFIG_HIGHBITDEPTH |
| 4615 | } |
| 4616 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4617 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4618 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 4619 | tmp = pixel_dist(cpi, x, plane, src, src_stride, rec_buffer, MAX_TX_SIZE, |
| 4620 | blk_row, blk_col, plane_bsize, txm_bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4621 | } |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4622 | rd_stats->dist += tmp * 16; |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 4623 | txb_coeff_cost = av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block, |
| 4624 | tx_size, scan_order, a, l, 0); |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 4625 | rd_stats->rate += txb_coeff_cost; |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 4626 | rd_stats->skip &= (eob == 0); |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4627 | |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 4628 | #if CONFIG_RD_DEBUG |
Angie Chiang | e94556b | 2016-11-09 10:59:30 -0800 | [diff] [blame] | 4629 | av1_update_txb_coeff_cost(rd_stats, plane, tx_size, blk_row, blk_col, |
| 4630 | txb_coeff_cost); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4631 | #endif // CONFIG_RD_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4632 | } |
| 4633 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4634 | static void select_tx_block(const AV1_COMP *cpi, MACROBLOCK *x, int blk_row, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4635 | int blk_col, int plane, int block, int block32, |
| 4636 | TX_SIZE tx_size, int depth, BLOCK_SIZE plane_bsize, |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 4637 | ENTROPY_CONTEXT *ta, ENTROPY_CONTEXT *tl, |
| 4638 | TXFM_CONTEXT *tx_above, TXFM_CONTEXT *tx_left, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4639 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4640 | int *is_cost_valid, RD_STATS *rd_stats_stack) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4641 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4642 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 4643 | struct macroblock_plane *const p = &x->plane[plane]; |
| 4644 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 4645 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 4646 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 4647 | TX_SIZE(*const inter_tx_size) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4648 | [MAX_MIB_SIZE] = |
| 4649 | (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] | 4650 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 4651 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 4652 | 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] | 4653 | int64_t this_rd = INT64_MAX; |
| 4654 | ENTROPY_CONTEXT *pta = ta + blk_col; |
| 4655 | ENTROPY_CONTEXT *ptl = tl + blk_row; |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4656 | int i; |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 4657 | int ctx = txfm_partition_context(tx_above + blk_col, tx_left + blk_row, |
| 4658 | mbmi->sb_type, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4659 | int64_t sum_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4660 | int tmp_eob = 0; |
| 4661 | int zero_blk_rate; |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 4662 | RD_STATS sum_rd_stats; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4663 | #if CONFIG_TXK_SEL |
| 4664 | TX_TYPE best_tx_type = TX_TYPES; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 4665 | int txk_idx = (blk_row << 4) + blk_col; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4666 | #endif |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4667 | #if CONFIG_RECT_TX_EXT |
| 4668 | TX_SIZE quarter_txsize = quarter_txsize_lookup[mbmi->sb_type]; |
| 4669 | int check_qttx = is_quarter_tx_allowed(xd, mbmi, is_inter_block(mbmi)) && |
| 4670 | tx_size == max_txsize_rect_lookup[mbmi->sb_type] && |
| 4671 | quarter_txsize != tx_size; |
| 4672 | int is_qttx_picked = 0; |
| 4673 | int eobs_qttx[2] = { 0, 0 }; |
| 4674 | int skip_qttx[2] = { 0, 0 }; |
| 4675 | int block_offset_qttx = check_qttx |
| 4676 | ? tx_size_wide_unit[quarter_txsize] * |
| 4677 | tx_size_high_unit[quarter_txsize] |
| 4678 | : 0; |
| 4679 | int blk_row_offset, blk_col_offset; |
| 4680 | int is_wide_qttx = |
| 4681 | tx_size_wide_unit[quarter_txsize] > tx_size_high_unit[quarter_txsize]; |
| 4682 | blk_row_offset = is_wide_qttx ? tx_size_high_unit[quarter_txsize] : 0; |
| 4683 | blk_col_offset = is_wide_qttx ? 0 : tx_size_wide_unit[quarter_txsize]; |
| 4684 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4685 | |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4686 | av1_init_rd_stats(&sum_rd_stats); |
Jingning Han | fe45b21 | 2016-11-22 10:30:23 -0800 | [diff] [blame] | 4687 | |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 4688 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4689 | |
| 4690 | if (ref_best_rd < 0) { |
| 4691 | *is_cost_valid = 0; |
| 4692 | return; |
| 4693 | } |
| 4694 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4695 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4696 | |
| 4697 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 4698 | |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4699 | #if CONFIG_LV_MAP |
| 4700 | TX_SIZE txs_ctx = get_txsize_context(tx_size); |
| 4701 | TXB_CTX txb_ctx; |
| 4702 | get_txb_ctx(plane_bsize, tx_size, plane, pta, ptl, &txb_ctx); |
Jingning Han | e981491 | 2017-08-31 16:38:59 -0700 | [diff] [blame] | 4703 | |
| 4704 | #if LV_MAP_PROB |
| 4705 | zero_blk_rate = x->coeff_costs[txs_ctx][get_plane_type(plane)] |
| 4706 | .txb_skip_cost[txb_ctx.txb_skip_ctx][1]; |
| 4707 | #else |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4708 | zero_blk_rate = |
| 4709 | av1_cost_bit(xd->fc->txb_skip[txs_ctx][txb_ctx.txb_skip_ctx], 1); |
Jingning Han | e981491 | 2017-08-31 16:38:59 -0700 | [diff] [blame] | 4710 | #endif // LV_MAP_PROB |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4711 | #else |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4712 | int tx_size_ctx = txsize_sqr_map[tx_size]; |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4713 | int coeff_ctx = get_entropy_context(tx_size, pta, ptl); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 4714 | zero_blk_rate = |
| 4715 | 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] | 4716 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4717 | |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4718 | rd_stats->ref_rdcost = ref_best_rd; |
| 4719 | rd_stats->zero_rate = zero_blk_rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4720 | if (cpi->common.tx_mode == TX_MODE_SELECT || tx_size == TX_4X4) { |
| 4721 | inter_tx_size[0][0] = tx_size; |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4722 | |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4723 | #if CONFIG_MRC_TX |
| 4724 | if (tx_size == TX_32X32 && mbmi->tx_type != DCT_DCT && |
| 4725 | rd_stats_stack[block32].rate != INT_MAX && !USE_MRC_INTER) { |
| 4726 | #else |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4727 | if (tx_size == TX_32X32 && mbmi->tx_type != DCT_DCT && |
| 4728 | rd_stats_stack[block32].rate != INT_MAX) { |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4729 | #endif // CONFIG_MRC_TX |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4730 | *rd_stats = rd_stats_stack[block32]; |
| 4731 | p->eobs[block] = !rd_stats->skip; |
| 4732 | x->blk_skip[plane][blk_row * bw + blk_col] = rd_stats->skip; |
| 4733 | } else { |
| 4734 | av1_tx_block_rd_b(cpi, x, tx_size, blk_row, blk_col, plane, block, |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 4735 | plane_bsize, pta, ptl, rd_stats); |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4736 | if (tx_size == TX_32X32) { |
| 4737 | rd_stats_stack[block32] = *rd_stats; |
| 4738 | } |
| 4739 | } |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4740 | if (rd_stats->rate == INT_MAX) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4741 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4742 | if ((RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) >= |
| 4743 | RDCOST(x->rdmult, zero_blk_rate, rd_stats->sse) || |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4744 | rd_stats->skip == 1) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4745 | !xd->lossless[mbmi->segment_id]) { |
Jingning Han | c7ea761 | 2017-01-11 15:01:30 -0800 | [diff] [blame] | 4746 | #if CONFIG_RD_DEBUG |
| 4747 | av1_update_txb_coeff_cost(rd_stats, plane, tx_size, blk_row, blk_col, |
| 4748 | zero_blk_rate - rd_stats->rate); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4749 | #endif // CONFIG_RD_DEBUG |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4750 | rd_stats->rate = zero_blk_rate; |
| 4751 | rd_stats->dist = rd_stats->sse; |
| 4752 | rd_stats->skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4753 | x->blk_skip[plane][blk_row * bw + blk_col] = 1; |
| 4754 | p->eobs[block] = 0; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 4755 | #if CONFIG_TXK_SEL |
| 4756 | mbmi->txk_type[txk_idx] = DCT_DCT; |
| 4757 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4758 | } else { |
| 4759 | x->blk_skip[plane][blk_row * bw + blk_col] = 0; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4760 | rd_stats->skip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4761 | } |
| 4762 | |
Jingning Han | 571189c | 2016-10-24 10:38:43 -0700 | [diff] [blame] | 4763 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4764 | rd_stats->rate += |
| 4765 | av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 0); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4766 | #if CONFIG_RECT_TX_EXT |
| 4767 | if (check_qttx) { |
| 4768 | assert(blk_row == 0 && blk_col == 0); |
| 4769 | rd_stats->rate += av1_cost_bit(cpi->common.fc->quarter_tx_size_prob, 0); |
| 4770 | } |
| 4771 | #endif |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4772 | this_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4773 | #if CONFIG_LV_MAP |
| 4774 | tmp_eob = p->txb_entropy_ctx[block]; |
| 4775 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4776 | tmp_eob = p->eobs[block]; |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4777 | #endif |
| 4778 | |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4779 | #if CONFIG_TXK_SEL |
| 4780 | best_tx_type = mbmi->txk_type[txk_idx]; |
| 4781 | #endif |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4782 | |
| 4783 | #if CONFIG_RECT_TX_EXT |
| 4784 | if (check_qttx) { |
| 4785 | assert(blk_row == 0 && blk_col == 0 && block == 0 && plane == 0); |
| 4786 | |
| 4787 | RD_STATS rd_stats_tmp, rd_stats_qttx; |
| 4788 | int64_t rd_qttx; |
| 4789 | |
| 4790 | av1_init_rd_stats(&rd_stats_qttx); |
| 4791 | av1_init_rd_stats(&rd_stats_tmp); |
| 4792 | |
| 4793 | av1_tx_block_rd_b(cpi, x, quarter_txsize, 0, 0, plane, 0, plane_bsize, |
| 4794 | pta, ptl, &rd_stats_qttx); |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4795 | if (rd_stats->rate == INT_MAX) return; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4796 | |
| 4797 | tx_size_ctx = txsize_sqr_map[quarter_txsize]; |
| 4798 | coeff_ctx = get_entropy_context(quarter_txsize, pta, ptl); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 4799 | zero_blk_rate = |
| 4800 | x->token_head_costs[tx_size_ctx][pd->plane_type][1][0][coeff_ctx][0]; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4801 | if ((RDCOST(x->rdmult, rd_stats_qttx.rate, rd_stats_qttx.dist) >= |
| 4802 | RDCOST(x->rdmult, zero_blk_rate, rd_stats_qttx.sse) || |
| 4803 | rd_stats_qttx.skip == 1) && |
| 4804 | !xd->lossless[mbmi->segment_id]) { |
| 4805 | #if CONFIG_RD_DEBUG |
| 4806 | av1_update_txb_coeff_cost(&rd_stats_qttx, plane, quarter_txsize, 0, 0, |
| 4807 | zero_blk_rate - rd_stats_qttx.rate); |
| 4808 | #endif // CONFIG_RD_DEBUG |
| 4809 | rd_stats_qttx.rate = zero_blk_rate; |
| 4810 | rd_stats_qttx.dist = rd_stats_qttx.sse; |
| 4811 | rd_stats_qttx.skip = 1; |
| 4812 | x->blk_skip[plane][blk_row * bw + blk_col] = 1; |
| 4813 | skip_qttx[0] = 1; |
| 4814 | p->eobs[block] = 0; |
| 4815 | } else { |
| 4816 | x->blk_skip[plane][blk_row * bw + blk_col] = 0; |
| 4817 | skip_qttx[0] = 0; |
| 4818 | rd_stats->skip = 0; |
| 4819 | } |
| 4820 | |
| 4821 | // Second tx block |
| 4822 | av1_tx_block_rd_b(cpi, x, quarter_txsize, blk_row_offset, blk_col_offset, |
| 4823 | plane, block_offset_qttx, plane_bsize, pta, ptl, |
| 4824 | &rd_stats_tmp); |
| 4825 | |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4826 | if (rd_stats->rate == INT_MAX) return; |
| 4827 | |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 4828 | #if !CONFIG_PVQ |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4829 | av1_set_txb_context(x, plane, 0, quarter_txsize, pta, ptl); |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 4830 | #endif // !CONFIG_PVQ |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4831 | coeff_ctx = get_entropy_context(quarter_txsize, pta + blk_col_offset, |
| 4832 | ptl + blk_row_offset); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 4833 | zero_blk_rate = |
| 4834 | x->token_head_costs[tx_size_ctx][pd->plane_type][1][0][coeff_ctx][0]; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4835 | if ((RDCOST(x->rdmult, rd_stats_tmp.rate, rd_stats_tmp.dist) >= |
| 4836 | RDCOST(x->rdmult, zero_blk_rate, rd_stats_tmp.sse) || |
| 4837 | rd_stats_tmp.skip == 1) && |
| 4838 | !xd->lossless[mbmi->segment_id]) { |
| 4839 | #if CONFIG_RD_DEBUG |
| 4840 | av1_update_txb_coeff_cost(&rd_stats_tmp, plane, quarter_txsize, 0, 0, |
| 4841 | zero_blk_rate - rd_stats_tmp.rate); |
| 4842 | #endif // CONFIG_RD_DEBUG |
| 4843 | rd_stats_tmp.rate = zero_blk_rate; |
| 4844 | rd_stats_tmp.dist = rd_stats_tmp.sse; |
| 4845 | rd_stats_tmp.skip = 1; |
| 4846 | x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = 1; |
| 4847 | skip_qttx[1] = 1; |
| 4848 | p->eobs[block_offset_qttx] = 0; |
| 4849 | } else { |
| 4850 | x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = 0; |
| 4851 | skip_qttx[1] = 0; |
| 4852 | rd_stats_tmp.skip = 0; |
| 4853 | } |
| 4854 | |
| 4855 | av1_merge_rd_stats(&rd_stats_qttx, &rd_stats_tmp); |
| 4856 | |
| 4857 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) { |
| 4858 | rd_stats_qttx.rate += |
| 4859 | av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 0); |
| 4860 | } |
| 4861 | rd_stats_qttx.rate += |
| 4862 | av1_cost_bit(cpi->common.fc->quarter_tx_size_prob, 1); |
| 4863 | rd_qttx = RDCOST(x->rdmult, rd_stats_qttx.rate, rd_stats_qttx.dist); |
| 4864 | #if CONFIG_LV_MAP |
| 4865 | eobs_qttx[0] = p->txb_entropy_ctx[0]; |
| 4866 | eobs_qttx[1] = p->txb_entropy_ctx[block_offset_qttx]; |
| 4867 | #else |
| 4868 | eobs_qttx[0] = p->eobs[0]; |
| 4869 | eobs_qttx[1] = p->eobs[block_offset_qttx]; |
| 4870 | #endif |
| 4871 | if (rd_qttx < this_rd) { |
| 4872 | is_qttx_picked = 1; |
| 4873 | this_rd = rd_qttx; |
| 4874 | rd_stats->rate = rd_stats_qttx.rate; |
| 4875 | rd_stats->dist = rd_stats_qttx.dist; |
| 4876 | rd_stats->sse = rd_stats_qttx.sse; |
| 4877 | rd_stats->skip = rd_stats_qttx.skip; |
| 4878 | rd_stats->rdcost = rd_stats_qttx.rdcost; |
| 4879 | } |
| 4880 | av1_get_entropy_contexts(plane_bsize, 0, pd, ta, tl); |
| 4881 | } |
| 4882 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4883 | } |
| 4884 | |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 4885 | #if CONFIG_MRC_TX |
| 4886 | // If the tx type we are trying is MRC_DCT, we cannot partition the transform |
| 4887 | // into anything smaller than TX_32X32 |
| 4888 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH && mbmi->tx_type != MRC_DCT) { |
| 4889 | #else |
Jingning Han | 571189c | 2016-10-24 10:38:43 -0700 | [diff] [blame] | 4890 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) { |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 4891 | #endif |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 4892 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 4893 | const int bsl = tx_size_wide_unit[sub_txs]; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 4894 | int sub_step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs]; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4895 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4896 | int this_cost_valid = 1; |
| 4897 | int64_t tmp_rd = 0; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4898 | #if CONFIG_DIST_8X8 |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4899 | int sub8x8_eob[4]; |
| 4900 | #endif |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 4901 | sum_rd_stats.rate = |
| 4902 | av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 1); |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 4903 | |
| 4904 | assert(tx_size < TX_SIZES_ALL); |
| 4905 | |
Jingning Han | 16a9df7 | 2017-07-26 15:27:43 -0700 | [diff] [blame] | 4906 | ref_best_rd = AOMMIN(this_rd, ref_best_rd); |
| 4907 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4908 | for (i = 0; i < 4 && this_cost_valid; ++i) { |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 4909 | int offsetr = blk_row + (i >> 1) * bsl; |
| 4910 | int offsetc = blk_col + (i & 0x01) * bsl; |
| 4911 | |
| 4912 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 4913 | |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4914 | select_tx_block(cpi, x, offsetr, offsetc, plane, block, block32, sub_txs, |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 4915 | depth + 1, plane_bsize, ta, tl, tx_above, tx_left, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4916 | &this_rd_stats, ref_best_rd - tmp_rd, &this_cost_valid, |
| 4917 | rd_stats_stack); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4918 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4919 | if (x->using_dist_8x8 && plane == 0 && tx_size == TX_8X8) { |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4920 | sub8x8_eob[i] = p->eobs[block]; |
| 4921 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4922 | #endif // CONFIG_DIST_8X8 |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4923 | av1_merge_rd_stats(&sum_rd_stats, &this_rd_stats); |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 4924 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4925 | tmp_rd = RDCOST(x->rdmult, sum_rd_stats.rate, sum_rd_stats.dist); |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4926 | #if CONFIG_DIST_8X8 |
| 4927 | if (!x->using_dist_8x8) |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4928 | #endif |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4929 | if (this_rd < tmp_rd) break; |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 4930 | block += sub_step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4931 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4932 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4933 | if (x->using_dist_8x8 && this_cost_valid && plane == 0 && |
| 4934 | tx_size == TX_8X8) { |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4935 | const int src_stride = p->src.stride; |
| 4936 | const int dst_stride = pd->dst.stride; |
| 4937 | |
| 4938 | const uint8_t *src = |
| 4939 | &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]]; |
| 4940 | const uint8_t *dst = |
| 4941 | &pd->dst |
| 4942 | .buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]]; |
| 4943 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4944 | int64_t dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4945 | int qindex = x->qindex; |
| 4946 | const int pred_stride = block_size_wide[plane_bsize]; |
| 4947 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 4948 | << tx_size_wide_log2[0]; |
| 4949 | int16_t *pred = &pd->pred[pred_idx]; |
| 4950 | int j; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4951 | int row, col; |
| 4952 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4953 | #if CONFIG_HIGHBITDEPTH |
| 4954 | uint8_t *pred8; |
| 4955 | DECLARE_ALIGNED(16, uint16_t, pred8_16[8 * 8]); |
| 4956 | #else |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4957 | DECLARE_ALIGNED(16, uint8_t, pred8[8 * 8]); |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4958 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4959 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 4960 | 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] | 4961 | BLOCK_8X8, 8, 8, 8, 8, qindex) * |
| 4962 | 16; |
| 4963 | sum_rd_stats.sse = dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4964 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4965 | #if CONFIG_HIGHBITDEPTH |
| 4966 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 4967 | pred8 = CONVERT_TO_BYTEPTR(pred8_16); |
| 4968 | else |
| 4969 | pred8 = (uint8_t *)pred8_16; |
| 4970 | #endif |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4971 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4972 | #if CONFIG_HIGHBITDEPTH |
| 4973 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 4974 | for (row = 0; row < 2; ++row) { |
| 4975 | for (col = 0; col < 2; ++col) { |
| 4976 | int idx = row * 2 + col; |
| 4977 | int eob = sub8x8_eob[idx]; |
| 4978 | |
| 4979 | if (eob > 0) { |
| 4980 | for (j = 0; j < 4; j++) |
| 4981 | for (i = 0; i < 4; i++) |
| 4982 | CONVERT_TO_SHORTPTR(pred8) |
| 4983 | [(row * 4 + j) * 8 + 4 * col + i] = |
| 4984 | pred[(row * 4 + j) * pred_stride + 4 * col + i]; |
| 4985 | } else { |
| 4986 | for (j = 0; j < 4; j++) |
| 4987 | for (i = 0; i < 4; i++) |
| 4988 | CONVERT_TO_SHORTPTR(pred8) |
| 4989 | [(row * 4 + j) * 8 + 4 * col + i] = CONVERT_TO_SHORTPTR( |
| 4990 | dst)[(row * 4 + j) * dst_stride + 4 * col + i]; |
| 4991 | } |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4992 | } |
| 4993 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4994 | } else { |
| 4995 | #endif |
| 4996 | for (row = 0; row < 2; ++row) { |
| 4997 | for (col = 0; col < 2; ++col) { |
| 4998 | int idx = row * 2 + col; |
| 4999 | int eob = sub8x8_eob[idx]; |
| 5000 | |
| 5001 | if (eob > 0) { |
| 5002 | for (j = 0; j < 4; j++) |
| 5003 | for (i = 0; i < 4; i++) |
| 5004 | pred8[(row * 4 + j) * 8 + 4 * col + i] = |
Yaowu Xu | 7a47170 | 2017-09-29 08:38:37 -0700 | [diff] [blame] | 5005 | (uint8_t)pred[(row * 4 + j) * pred_stride + 4 * col + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 5006 | } else { |
| 5007 | for (j = 0; j < 4; j++) |
| 5008 | for (i = 0; i < 4; i++) |
| 5009 | pred8[(row * 4 + j) * 8 + 4 * col + i] = |
| 5010 | dst[(row * 4 + j) * dst_stride + 4 * col + i]; |
| 5011 | } |
| 5012 | } |
| 5013 | } |
| 5014 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 5015 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 5016 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 5017 | 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] | 5018 | 8, 8, 8, qindex) * |
| 5019 | 16; |
| 5020 | sum_rd_stats.dist = dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 5021 | tmp_rd = RDCOST(x->rdmult, sum_rd_stats.rate, sum_rd_stats.dist); |
| 5022 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 5023 | #endif // CONFIG_DIST_8X8 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5024 | if (this_cost_valid) sum_rd = tmp_rd; |
| 5025 | } |
| 5026 | |
| 5027 | if (this_rd < sum_rd) { |
| 5028 | int idx, idy; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 5029 | #if CONFIG_RECT_TX_EXT |
| 5030 | TX_SIZE tx_size_selected = is_qttx_picked ? quarter_txsize : tx_size; |
| 5031 | #else |
| 5032 | TX_SIZE tx_size_selected = tx_size; |
| 5033 | #endif |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 5034 | |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 5035 | #if CONFIG_RECT_TX_EXT |
| 5036 | if (is_qttx_picked) { |
| 5037 | assert(blk_row == 0 && blk_col == 0 && plane == 0); |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 5038 | #if CONFIG_LV_MAP |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 5039 | p->txb_entropy_ctx[0] = eobs_qttx[0]; |
| 5040 | p->txb_entropy_ctx[block_offset_qttx] = eobs_qttx[1]; |
| 5041 | #else |
| 5042 | p->eobs[0] = eobs_qttx[0]; |
| 5043 | p->eobs[block_offset_qttx] = eobs_qttx[1]; |
| 5044 | #endif |
| 5045 | } else { |
| 5046 | #endif |
| 5047 | #if CONFIG_LV_MAP |
| 5048 | p->txb_entropy_ctx[block] = tmp_eob; |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 5049 | #else |
| 5050 | p->eobs[block] = tmp_eob; |
| 5051 | #endif |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 5052 | #if CONFIG_RECT_TX_EXT |
| 5053 | } |
| 5054 | #endif |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 5055 | |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 5056 | #if !CONFIG_PVQ |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 5057 | av1_set_txb_context(x, plane, block, tx_size_selected, pta, ptl); |
| 5058 | #if CONFIG_RECT_TX_EXT |
| 5059 | if (is_qttx_picked) |
| 5060 | av1_set_txb_context(x, plane, block_offset_qttx, tx_size_selected, |
| 5061 | pta + blk_col_offset, ptl + blk_row_offset); |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 5062 | #endif // CONFIG_RECT_TX_EXT |
| 5063 | #endif // !CONFIG_PVQ |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 5064 | |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 5065 | txfm_partition_update(tx_above + blk_col, tx_left + blk_row, tx_size, |
| 5066 | tx_size); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 5067 | inter_tx_size[0][0] = tx_size_selected; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 5068 | for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy) |
| 5069 | for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx) |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 5070 | inter_tx_size[idy][idx] = tx_size_selected; |
| 5071 | mbmi->tx_size = tx_size_selected; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 5072 | #if CONFIG_TXK_SEL |
| 5073 | mbmi->txk_type[txk_idx] = best_tx_type; |
| 5074 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5075 | if (this_rd == INT64_MAX) *is_cost_valid = 0; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 5076 | #if CONFIG_RECT_TX_EXT |
| 5077 | if (is_qttx_picked) { |
| 5078 | x->blk_skip[plane][0] = skip_qttx[0]; |
| 5079 | x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = skip_qttx[1]; |
| 5080 | } else { |
| 5081 | #endif |
| 5082 | x->blk_skip[plane][blk_row * bw + blk_col] = rd_stats->skip; |
| 5083 | #if CONFIG_RECT_TX_EXT |
| 5084 | } |
| 5085 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5086 | } else { |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 5087 | *rd_stats = sum_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5088 | if (sum_rd == INT64_MAX) *is_cost_valid = 0; |
| 5089 | } |
| 5090 | } |
| 5091 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5092 | static void inter_block_yrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 5093 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5094 | int64_t ref_best_rd, RD_STATS *rd_stats_stack) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5095 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5096 | int is_cost_valid = 1; |
| 5097 | int64_t this_rd = 0; |
| 5098 | |
| 5099 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 5100 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5101 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5102 | |
| 5103 | if (is_cost_valid) { |
| 5104 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 5105 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5106 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 5107 | const int mi_height = block_size_high[plane_bsize] >> tx_size_high_log2[0]; |
Jingning Han | 70e5f3f | 2016-11-09 17:03:07 -0800 | [diff] [blame] | 5108 | const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize]; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5109 | const int bh = tx_size_high_unit[max_tx_size]; |
| 5110 | const int bw = tx_size_wide_unit[max_tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5111 | int idx, idy; |
| 5112 | int block = 0; |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5113 | int block32 = 0; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5114 | 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] | 5115 | ENTROPY_CONTEXT ctxa[2 * MAX_MIB_SIZE]; |
| 5116 | ENTROPY_CONTEXT ctxl[2 * MAX_MIB_SIZE]; |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 5117 | TXFM_CONTEXT tx_above[MAX_MIB_SIZE * 2]; |
| 5118 | TXFM_CONTEXT tx_left[MAX_MIB_SIZE * 2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5119 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5120 | RD_STATS pn_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5121 | av1_init_rd_stats(&pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5122 | |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5123 | av1_get_entropy_contexts(bsize, 0, pd, ctxa, ctxl); |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 5124 | memcpy(tx_above, xd->above_txfm_context, sizeof(TXFM_CONTEXT) * mi_width); |
| 5125 | memcpy(tx_left, xd->left_txfm_context, sizeof(TXFM_CONTEXT) * mi_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5126 | |
| 5127 | for (idy = 0; idy < mi_height; idy += bh) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5128 | for (idx = 0; idx < mi_width; idx += bw) { |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5129 | select_tx_block(cpi, x, idy, idx, 0, block, block32, max_tx_size, |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5130 | mi_height != mi_width, plane_bsize, ctxa, ctxl, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5131 | tx_above, tx_left, &pn_rd_stats, ref_best_rd - this_rd, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5132 | &is_cost_valid, rd_stats_stack); |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 5133 | if (pn_rd_stats.rate == INT_MAX) { |
| 5134 | av1_invalid_rd_stats(rd_stats); |
| 5135 | return; |
| 5136 | } |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5137 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5138 | this_rd += AOMMIN(RDCOST(x->rdmult, pn_rd_stats.rate, pn_rd_stats.dist), |
| 5139 | RDCOST(x->rdmult, 0, pn_rd_stats.sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5140 | block += step; |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5141 | ++block32; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5142 | } |
| 5143 | } |
| 5144 | } |
| 5145 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5146 | this_rd = AOMMIN(RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist), |
| 5147 | RDCOST(x->rdmult, 0, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5148 | if (this_rd > ref_best_rd) is_cost_valid = 0; |
| 5149 | |
| 5150 | if (!is_cost_valid) { |
| 5151 | // reset cost value |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5152 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5156 | 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] | 5157 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5158 | int64_t ref_best_rd, TX_TYPE tx_type, |
| 5159 | RD_STATS *rd_stats_stack) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5160 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5161 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5162 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5163 | const int is_inter = is_inter_block(mbmi); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5164 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
| 5165 | int s0 = av1_cost_bit(skip_prob, 0); |
| 5166 | int s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5167 | int64_t rd; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5168 | int row, col; |
| 5169 | const int max_blocks_high = max_block_high(xd, bsize, 0); |
| 5170 | const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5171 | |
| 5172 | mbmi->tx_type = tx_type; |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5173 | inter_block_yrd(cpi, x, rd_stats, bsize, ref_best_rd, rd_stats_stack); |
Jingning Han | 70bd76e | 2017-05-30 10:53:00 -0700 | [diff] [blame] | 5174 | mbmi->min_tx_size = get_min_tx_size(mbmi->inter_tx_size[0][0]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5175 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5176 | if (rd_stats->rate == INT_MAX) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5177 | |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5178 | for (row = 0; row < max_blocks_high / 2; ++row) |
| 5179 | for (col = 0; col < max_blocks_wide / 2; ++col) |
| 5180 | mbmi->min_tx_size = AOMMIN( |
| 5181 | mbmi->min_tx_size, get_min_tx_size(mbmi->inter_tx_size[row][col])); |
| 5182 | |
Jingning Han | 1643a0a | 2017-07-05 15:48:25 -0700 | [diff] [blame] | 5183 | #if !CONFIG_TXK_SEL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5184 | #if CONFIG_EXT_TX |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 5185 | if (get_ext_tx_types(mbmi->min_tx_size, bsize, is_inter, |
| 5186 | cm->reduced_tx_set_used) > 1 && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5187 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 5188 | const int ext_tx_set = get_ext_tx_set(mbmi->min_tx_size, bsize, is_inter, |
| 5189 | cm->reduced_tx_set_used); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5190 | if (is_inter) { |
| 5191 | if (ext_tx_set > 0) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5192 | rd_stats->rate += |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5193 | x->inter_tx_type_costs[ext_tx_set] |
| 5194 | [txsize_sqr_map[mbmi->min_tx_size]] |
| 5195 | [mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5196 | } else { |
| 5197 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5198 | rd_stats->rate += x->intra_tx_type_costs[ext_tx_set][mbmi->min_tx_size] |
| 5199 | [mbmi->mode][mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5200 | } |
| 5201 | } |
Jingning Han | 1643a0a | 2017-07-05 15:48:25 -0700 | [diff] [blame] | 5202 | #else |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5203 | if (mbmi->min_tx_size < TX_32X32 && !xd->lossless[xd->mi[0]->mbmi.segment_id]) |
Zoe Liu | 4508d88 | 2017-07-31 15:36:55 -0700 | [diff] [blame] | 5204 | rd_stats->rate += x->inter_tx_type_costs[mbmi->min_tx_size][mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5205 | #endif // CONFIG_EXT_TX |
Jingning Han | 1643a0a | 2017-07-05 15:48:25 -0700 | [diff] [blame] | 5206 | #endif // CONFIG_TXK_SEL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5207 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5208 | if (rd_stats->skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5209 | rd = RDCOST(x->rdmult, s1, rd_stats->sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5210 | else |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5211 | rd = RDCOST(x->rdmult, rd_stats->rate + s0, rd_stats->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5212 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5213 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 5214 | !(rd_stats->skip)) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5215 | rd = AOMMIN(rd, RDCOST(x->rdmult, s1, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5216 | |
| 5217 | return rd; |
| 5218 | } |
| 5219 | |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 5220 | static uint32_t get_block_residue_hash(MACROBLOCK *x, BLOCK_SIZE bsize) { |
| 5221 | const int rows = block_size_high[bsize]; |
| 5222 | const int cols = block_size_wide[bsize]; |
| 5223 | const int diff_stride = cols; |
| 5224 | const struct macroblock_plane *const p = &x->plane[0]; |
| 5225 | const int16_t *diff = &p->src_diff[0]; |
| 5226 | uint8_t hash_data[MAX_SB_SQUARE]; |
| 5227 | for (int r = 0; r < rows; ++r) { |
| 5228 | for (int c = 0; c < cols; ++c) { |
| 5229 | hash_data[cols * r + c] = clip_pixel(diff[c] + 128); |
| 5230 | } |
| 5231 | diff += diff_stride; |
| 5232 | } |
| 5233 | return (av1_get_crc_value(&x->tx_rd_record.crc_calculator, hash_data, |
| 5234 | rows * cols) |
| 5235 | << 7) + |
| 5236 | bsize; |
| 5237 | } |
| 5238 | |
| 5239 | static void save_tx_rd_info(int n4, uint32_t hash, const MACROBLOCK *const x, |
| 5240 | const RD_STATS *const rd_stats, |
| 5241 | TX_RD_INFO *const tx_rd_info) { |
| 5242 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 5243 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5244 | tx_rd_info->hash_value = hash; |
| 5245 | tx_rd_info->tx_type = mbmi->tx_type; |
| 5246 | tx_rd_info->tx_size = mbmi->tx_size; |
| 5247 | #if CONFIG_VAR_TX |
| 5248 | tx_rd_info->min_tx_size = mbmi->min_tx_size; |
| 5249 | memcpy(tx_rd_info->blk_skip, x->blk_skip[0], |
| 5250 | sizeof(tx_rd_info->blk_skip[0]) * n4); |
| 5251 | for (int idy = 0; idy < xd->n8_h; ++idy) |
| 5252 | for (int idx = 0; idx < xd->n8_w; ++idx) |
| 5253 | tx_rd_info->inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 5254 | #endif // CONFIG_VAR_TX |
| 5255 | #if CONFIG_TXK_SEL |
| 5256 | av1_copy(tx_rd_info->txk_type, mbmi->txk_type); |
| 5257 | #endif // CONFIG_TXK_SEL |
| 5258 | tx_rd_info->rd_stats = *rd_stats; |
| 5259 | } |
| 5260 | |
| 5261 | static void fetch_tx_rd_info(int n4, const TX_RD_INFO *const tx_rd_info, |
| 5262 | RD_STATS *const rd_stats, MACROBLOCK *const x) { |
| 5263 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5264 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5265 | mbmi->tx_type = tx_rd_info->tx_type; |
| 5266 | mbmi->tx_size = tx_rd_info->tx_size; |
| 5267 | #if CONFIG_VAR_TX |
| 5268 | mbmi->min_tx_size = tx_rd_info->min_tx_size; |
| 5269 | memcpy(x->blk_skip[0], tx_rd_info->blk_skip, |
| 5270 | sizeof(tx_rd_info->blk_skip[0]) * n4); |
| 5271 | for (int idy = 0; idy < xd->n8_h; ++idy) |
| 5272 | for (int idx = 0; idx < xd->n8_w; ++idx) |
| 5273 | mbmi->inter_tx_size[idy][idx] = tx_rd_info->inter_tx_size[idy][idx]; |
| 5274 | #endif // CONFIG_VAR_TX |
| 5275 | #if CONFIG_TXK_SEL |
| 5276 | av1_copy(mbmi->txk_type, tx_rd_info->txk_type); |
| 5277 | #endif // CONFIG_TXK_SEL |
| 5278 | *rd_stats = tx_rd_info->rd_stats; |
| 5279 | } |
| 5280 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5281 | static void select_tx_type_yrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 5282 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5283 | int64_t ref_best_rd) { |
Jingning Han | 2b0eeb1 | 2017-02-23 15:55:37 -0800 | [diff] [blame] | 5284 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5285 | const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; |
| 5286 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5287 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5288 | int64_t rd = INT64_MAX; |
| 5289 | int64_t best_rd = INT64_MAX; |
| 5290 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
| 5291 | const int is_inter = is_inter_block(mbmi); |
| 5292 | TX_SIZE best_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE]; |
Debargha Mukherjee | 28d924b | 2016-10-05 00:48:28 -0700 | [diff] [blame] | 5293 | TX_SIZE best_tx = max_txsize_lookup[bsize]; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5294 | TX_SIZE best_min_tx_size = TX_SIZES_ALL; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5295 | uint8_t best_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 5296 | TX_TYPE txk_start = DCT_DCT; |
| 5297 | #if CONFIG_TXK_SEL |
| 5298 | TX_TYPE txk_end = DCT_DCT + 1; |
| 5299 | #else |
| 5300 | TX_TYPE txk_end = TX_TYPES; |
| 5301 | #endif |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 5302 | const int n4 = bsize_to_num_blk(bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5303 | int idx, idy; |
| 5304 | int prune = 0; |
Jingning Han | 2b0eeb1 | 2017-02-23 15:55:37 -0800 | [diff] [blame] | 5305 | const int count32 = |
| 5306 | 1 << (2 * (cm->mib_size_log2 - mi_width_log2_lookup[BLOCK_32X32])); |
Jingning Han | 89d648b | 2016-11-22 11:22:08 -0800 | [diff] [blame] | 5307 | #if CONFIG_EXT_PARTITION |
| 5308 | RD_STATS rd_stats_stack[16]; |
| 5309 | #else |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5310 | RD_STATS rd_stats_stack[4]; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5311 | #endif // CONFIG_EXT_PARTITION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5312 | #if CONFIG_EXT_TX |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 5313 | const TxSetType tx_set_type = get_ext_tx_set_type( |
| 5314 | max_tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 5315 | const int ext_tx_set = |
| 5316 | get_ext_tx_set(max_tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5317 | #endif // CONFIG_EXT_TX |
| 5318 | |
| 5319 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 5320 | #if CONFIG_EXT_TX |
| 5321 | prune = prune_tx_types(cpi, bsize, x, xd, ext_tx_set); |
| 5322 | #else |
| 5323 | prune = prune_tx_types(cpi, bsize, x, xd, 0); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5324 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5325 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5326 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5327 | |
Jingning Han | 89d648b | 2016-11-22 11:22:08 -0800 | [diff] [blame] | 5328 | for (idx = 0; idx < count32; ++idx) |
| 5329 | av1_invalid_rd_stats(&rd_stats_stack[idx]); |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5330 | |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 5331 | const uint32_t hash = get_block_residue_hash(x, bsize); |
| 5332 | TX_RD_RECORD *tx_rd_record = &x->tx_rd_record; |
| 5333 | |
| 5334 | if (ref_best_rd != INT64_MAX) { |
| 5335 | for (int i = 0; i < tx_rd_record->num; ++i) { |
| 5336 | const int index = (tx_rd_record->index_start + i) % RD_RECORD_BUFFER_LEN; |
| 5337 | // If there is a match in the tx_rd_record, fetch the RD decision and |
| 5338 | // terminate early. |
| 5339 | if (tx_rd_record->tx_rd_info[index].hash_value == hash) { |
| 5340 | TX_RD_INFO *tx_rd_info = &tx_rd_record->tx_rd_info[index]; |
| 5341 | fetch_tx_rd_info(n4, tx_rd_info, rd_stats, x); |
| 5342 | return; |
| 5343 | } |
| 5344 | } |
| 5345 | } |
| 5346 | |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 5347 | for (tx_type = txk_start; tx_type < txk_end; ++tx_type) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5348 | RD_STATS this_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5349 | av1_init_rd_stats(&this_rd_stats); |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 5350 | #if CONFIG_MRC_TX |
| 5351 | // MRC_DCT only implemented for TX_32X32 so only include this tx in |
| 5352 | // the search for TX_32X32 |
Sarah Parker | 2e08d96 | 2017-08-01 19:51:20 -0700 | [diff] [blame] | 5353 | if (tx_type == MRC_DCT && |
| 5354 | (max_tx_size != TX_32X32 || (is_inter && !USE_MRC_INTER) || |
| 5355 | (!is_inter && !USE_MRC_INTRA))) |
| 5356 | continue; |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 5357 | #endif // CONFIG_MRC_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5358 | #if CONFIG_EXT_TX |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 5359 | if (!av1_ext_tx_used[tx_set_type][tx_type]) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5360 | if (is_inter) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5361 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 5362 | if (!do_tx_type_search(tx_type, prune)) continue; |
| 5363 | } |
| 5364 | } else { |
| 5365 | if (!ALLOW_INTRA_EXT_TX && bsize >= BLOCK_8X8) { |
| 5366 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 5367 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5368 | } |
| 5369 | #else // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5370 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 5371 | !do_tx_type_search(tx_type, prune)) |
| 5372 | continue; |
| 5373 | #endif // CONFIG_EXT_TX |
| 5374 | if (is_inter && x->use_default_inter_tx_type && |
| 5375 | tx_type != get_default_tx_type(0, xd, 0, max_tx_size)) |
| 5376 | continue; |
| 5377 | |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5378 | if (xd->lossless[mbmi->segment_id]) |
| 5379 | if (tx_type != DCT_DCT) continue; |
| 5380 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5381 | rd = select_tx_size_fix_type(cpi, x, &this_rd_stats, bsize, ref_best_rd, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5382 | tx_type, rd_stats_stack); |
Hui Su | da816a1 | 2017-08-18 14:46:02 -0700 | [diff] [blame] | 5383 | ref_best_rd = AOMMIN(rd, ref_best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5384 | if (rd < best_rd) { |
| 5385 | best_rd = rd; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5386 | *rd_stats = this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5387 | best_tx_type = mbmi->tx_type; |
| 5388 | best_tx = mbmi->tx_size; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5389 | best_min_tx_size = mbmi->min_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5390 | memcpy(best_blk_skip, x->blk_skip[0], sizeof(best_blk_skip[0]) * n4); |
| 5391 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 5392 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 5393 | best_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 5394 | } |
| 5395 | } |
| 5396 | |
| 5397 | mbmi->tx_type = best_tx_type; |
| 5398 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 5399 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 5400 | mbmi->inter_tx_size[idy][idx] = best_tx_size[idy][idx]; |
| 5401 | mbmi->tx_size = best_tx; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5402 | mbmi->min_tx_size = best_min_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5403 | 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] | 5404 | |
| 5405 | // Save the RD search results into tx_rd_record. |
| 5406 | int index; |
| 5407 | if (tx_rd_record->num < RD_RECORD_BUFFER_LEN) { |
| 5408 | index = |
| 5409 | (tx_rd_record->index_start + tx_rd_record->num) % RD_RECORD_BUFFER_LEN; |
| 5410 | ++tx_rd_record->num; |
| 5411 | } else { |
| 5412 | index = tx_rd_record->index_start; |
| 5413 | tx_rd_record->index_start = |
| 5414 | (tx_rd_record->index_start + 1) % RD_RECORD_BUFFER_LEN; |
| 5415 | } |
| 5416 | save_tx_rd_info(n4, hash, x, rd_stats, &tx_rd_record->tx_rd_info[index]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5417 | } |
| 5418 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5419 | 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] | 5420 | int blk_col, int plane, int block, TX_SIZE tx_size, |
| 5421 | BLOCK_SIZE plane_bsize, ENTROPY_CONTEXT *above_ctx, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5422 | ENTROPY_CONTEXT *left_ctx, RD_STATS *rd_stats) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5423 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5424 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5425 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 5426 | BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
| 5427 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 5428 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
| 5429 | TX_SIZE plane_tx_size; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5430 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 5431 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5432 | |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 5433 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5434 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5435 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 5436 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 5437 | plane_tx_size = |
| 5438 | plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0] |
| 5439 | : mbmi->inter_tx_size[tx_row][tx_col]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5440 | |
| 5441 | if (tx_size == plane_tx_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5442 | ENTROPY_CONTEXT *ta = above_ctx + blk_col; |
| 5443 | ENTROPY_CONTEXT *tl = left_ctx + blk_row; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5444 | av1_tx_block_rd_b(cpi, x, tx_size, blk_row, blk_col, plane, block, |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 5445 | plane_bsize, ta, tl, rd_stats); |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 5446 | #if !CONFIG_PVQ |
Jingning Han | 328d57b | 2017-07-07 14:40:17 -0700 | [diff] [blame] | 5447 | av1_set_txb_context(x, plane, block, tx_size, ta, tl); |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 5448 | #endif // !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5449 | } else { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5450 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 5451 | const int bsl = tx_size_wide_unit[sub_txs]; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 5452 | int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5453 | int i; |
| 5454 | |
| 5455 | assert(bsl > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5456 | |
| 5457 | for (i = 0; i < 4; ++i) { |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 5458 | int offsetr = blk_row + (i >> 1) * bsl; |
| 5459 | int offsetc = blk_col + (i & 0x01) * bsl; |
| 5460 | |
| 5461 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 5462 | |
| 5463 | tx_block_rd(cpi, x, offsetr, offsetc, plane, block, sub_txs, plane_bsize, |
| 5464 | above_ctx, left_ctx, rd_stats); |
| 5465 | block += step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5466 | } |
| 5467 | } |
| 5468 | } |
| 5469 | |
| 5470 | // Return value 0: early termination triggered, no valid rd cost available; |
| 5471 | // 1: rd cost values are valid. |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5472 | static int inter_block_uvrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 5473 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
| 5474 | int64_t ref_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5475 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5476 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5477 | int plane; |
| 5478 | int is_cost_valid = 1; |
| 5479 | int64_t this_rd; |
| 5480 | |
| 5481 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 5482 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5483 | av1_init_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 5484 | |
Jingning Han | 31b6a4f | 2017-02-23 11:05:53 -0800 | [diff] [blame] | 5485 | #if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 5486 | if (x->skip_chroma_rd) return is_cost_valid; |
Jingning Han | 52d2b40 | 2017-05-19 09:31:17 -0700 | [diff] [blame] | 5487 | bsize = scale_chroma_bsize(mbmi->sb_type, xd->plane[1].subsampling_x, |
| 5488 | xd->plane[1].subsampling_y); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5489 | #endif // CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 5490 | |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 5491 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 5492 | if (is_rect_tx(mbmi->tx_size)) { |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5493 | return super_block_uvrd(cpi, x, rd_stats, bsize, ref_best_rd); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 5494 | } |
| 5495 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 5496 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5497 | if (is_inter_block(mbmi) && is_cost_valid) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5498 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5499 | av1_subtract_plane(x, bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5500 | } |
| 5501 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5502 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) { |
| 5503 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 5504 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5505 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 5506 | const int mi_height = block_size_high[plane_bsize] >> tx_size_high_log2[0]; |
Jingning Han | 70e5f3f | 2016-11-09 17:03:07 -0800 | [diff] [blame] | 5507 | const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize]; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5508 | const int bh = tx_size_high_unit[max_tx_size]; |
| 5509 | const int bw = tx_size_wide_unit[max_tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5510 | int idx, idy; |
| 5511 | int block = 0; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5512 | const int step = bh * bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5513 | ENTROPY_CONTEXT ta[2 * MAX_MIB_SIZE]; |
| 5514 | ENTROPY_CONTEXT tl[2 * MAX_MIB_SIZE]; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5515 | RD_STATS pn_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5516 | av1_init_rd_stats(&pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5517 | |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5518 | av1_get_entropy_contexts(bsize, 0, pd, ta, tl); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5519 | |
| 5520 | for (idy = 0; idy < mi_height; idy += bh) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5521 | for (idx = 0; idx < mi_width; idx += bw) { |
| 5522 | tx_block_rd(cpi, x, idy, idx, plane, block, max_tx_size, plane_bsize, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5523 | ta, tl, &pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5524 | block += step; |
| 5525 | } |
| 5526 | } |
| 5527 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5528 | if (pn_rd_stats.rate == INT_MAX) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5529 | is_cost_valid = 0; |
| 5530 | break; |
| 5531 | } |
| 5532 | |
Angie Chiang | 628d7c9 | 2016-11-03 16:24:56 -0700 | [diff] [blame] | 5533 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5534 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5535 | this_rd = AOMMIN(RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist), |
| 5536 | RDCOST(x->rdmult, 0, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5537 | |
| 5538 | if (this_rd > ref_best_rd) { |
| 5539 | is_cost_valid = 0; |
| 5540 | break; |
| 5541 | } |
| 5542 | } |
| 5543 | |
| 5544 | if (!is_cost_valid) { |
| 5545 | // reset cost value |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5546 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5547 | } |
| 5548 | |
| 5549 | return is_cost_valid; |
| 5550 | } |
| 5551 | #endif // CONFIG_VAR_TX |
| 5552 | |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5553 | static void rd_pick_palette_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 5554 | int dc_mode_cost, |
| 5555 | uint8_t *best_palette_color_map, |
| 5556 | MB_MODE_INFO *const best_mbmi, |
| 5557 | int64_t *best_rd, int *rate, |
| 5558 | int *rate_tokenonly, int64_t *distortion, |
| 5559 | int *skippable) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5560 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5561 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5562 | assert(!is_inter_block(mbmi)); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5563 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5564 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 5565 | assert(bsize >= BLOCK_8X8); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5566 | int this_rate; |
| 5567 | int64_t this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5568 | int colors_u, colors_v, colors; |
| 5569 | const int src_stride = x->plane[1].src.stride; |
| 5570 | const uint8_t *const src_u = x->plane[1].src.buf; |
| 5571 | const uint8_t *const src_v = x->plane[2].src.buf; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5572 | uint8_t *const color_map = xd->plane[1].color_index_map; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5573 | RD_STATS tokenonly_rd_stats; |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5574 | int plane_block_width, plane_block_height, rows, cols; |
| 5575 | av1_get_block_dimensions(bsize, 1, xd, &plane_block_width, |
| 5576 | &plane_block_height, &rows, &cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5577 | if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return; |
| 5578 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5579 | mbmi->uv_mode = UV_DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5580 | #if CONFIG_FILTER_INTRA |
| 5581 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 5582 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5583 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5584 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5585 | if (cpi->common.use_highbitdepth) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5586 | colors_u = av1_count_colors_highbd(src_u, src_stride, rows, cols, |
| 5587 | cpi->common.bit_depth); |
| 5588 | colors_v = av1_count_colors_highbd(src_v, src_stride, rows, cols, |
| 5589 | cpi->common.bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5590 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5591 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5592 | colors_u = av1_count_colors(src_u, src_stride, rows, cols); |
| 5593 | colors_v = av1_count_colors(src_v, src_stride, rows, cols); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5594 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5595 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5596 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5597 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5598 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5599 | uint16_t color_cache[2 * PALETTE_MAX_SIZE]; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 5600 | const int n_cache = av1_get_palette_cache(xd, 1, color_cache); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5601 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 5602 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5603 | colors = colors_u > colors_v ? colors_u : colors_v; |
| 5604 | if (colors > 1 && colors <= 64) { |
| 5605 | int r, c, n, i, j; |
| 5606 | const int max_itr = 50; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5607 | float lb_u, ub_u, val_u; |
| 5608 | float lb_v, ub_v, val_v; |
| 5609 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 5610 | float centroids[2 * PALETTE_MAX_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5611 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5612 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5613 | uint16_t *src_u16 = CONVERT_TO_SHORTPTR(src_u); |
| 5614 | uint16_t *src_v16 = CONVERT_TO_SHORTPTR(src_v); |
| 5615 | if (cpi->common.use_highbitdepth) { |
| 5616 | lb_u = src_u16[0]; |
| 5617 | ub_u = src_u16[0]; |
| 5618 | lb_v = src_v16[0]; |
| 5619 | ub_v = src_v16[0]; |
| 5620 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5621 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5622 | lb_u = src_u[0]; |
| 5623 | ub_u = src_u[0]; |
| 5624 | lb_v = src_v[0]; |
| 5625 | ub_v = src_v[0]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5626 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5627 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5628 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5629 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5630 | for (r = 0; r < rows; ++r) { |
| 5631 | for (c = 0; c < cols; ++c) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5632 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5633 | if (cpi->common.use_highbitdepth) { |
| 5634 | val_u = src_u16[r * src_stride + c]; |
| 5635 | val_v = src_v16[r * src_stride + c]; |
| 5636 | data[(r * cols + c) * 2] = val_u; |
| 5637 | data[(r * cols + c) * 2 + 1] = val_v; |
| 5638 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5639 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5640 | val_u = src_u[r * src_stride + c]; |
| 5641 | val_v = src_v[r * src_stride + c]; |
| 5642 | data[(r * cols + c) * 2] = val_u; |
| 5643 | data[(r * cols + c) * 2 + 1] = val_v; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5644 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5645 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5646 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5647 | if (val_u < lb_u) |
| 5648 | lb_u = val_u; |
| 5649 | else if (val_u > ub_u) |
| 5650 | ub_u = val_u; |
| 5651 | if (val_v < lb_v) |
| 5652 | lb_v = val_v; |
| 5653 | else if (val_v > ub_v) |
| 5654 | ub_v = val_v; |
| 5655 | } |
| 5656 | } |
| 5657 | |
| 5658 | for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2; |
| 5659 | --n) { |
| 5660 | for (i = 0; i < n; ++i) { |
| 5661 | centroids[i * 2] = lb_u + (2 * i + 1) * (ub_u - lb_u) / n / 2; |
| 5662 | centroids[i * 2 + 1] = lb_v + (2 * i + 1) * (ub_v - lb_v) / n / 2; |
| 5663 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5664 | 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] | 5665 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5666 | optimize_palette_colors(color_cache, n_cache, n, 2, centroids); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 5667 | // Sort the U channel colors in ascending order. |
| 5668 | for (i = 0; i < 2 * (n - 1); i += 2) { |
| 5669 | int min_idx = i; |
| 5670 | float min_val = centroids[i]; |
| 5671 | for (j = i + 2; j < 2 * n; j += 2) |
| 5672 | if (centroids[j] < min_val) min_val = centroids[j], min_idx = j; |
| 5673 | if (min_idx != i) { |
| 5674 | float temp_u = centroids[i], temp_v = centroids[i + 1]; |
| 5675 | centroids[i] = centroids[min_idx]; |
| 5676 | centroids[i + 1] = centroids[min_idx + 1]; |
| 5677 | centroids[min_idx] = temp_u, centroids[min_idx + 1] = temp_v; |
| 5678 | } |
| 5679 | } |
| 5680 | av1_calc_indices(data, centroids, color_map, rows * cols, n, 2); |
| 5681 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5682 | extend_palette_color_map(color_map, cols, rows, plane_block_width, |
| 5683 | plane_block_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5684 | pmi->palette_size[1] = n; |
| 5685 | for (i = 1; i < 3; ++i) { |
| 5686 | for (j = 0; j < n; ++j) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5687 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5688 | if (cpi->common.use_highbitdepth) |
| 5689 | pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = clip_pixel_highbd( |
| 5690 | (int)centroids[j * 2 + i - 1], cpi->common.bit_depth); |
| 5691 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5692 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5693 | pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = |
| 5694 | clip_pixel((int)centroids[j * 2 + i - 1]); |
| 5695 | } |
| 5696 | } |
| 5697 | |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5698 | super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 5699 | if (tokenonly_rd_stats.rate == INT_MAX) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5700 | this_rate = |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5701 | tokenonly_rd_stats.rate + dc_mode_cost + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5702 | x->palette_uv_size_cost[bsize - BLOCK_8X8][n - PALETTE_MIN_SIZE] + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5703 | write_uniform_cost(n, color_map[0]) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5704 | av1_cost_bit( |
| 5705 | av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 1); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5706 | this_rate += av1_palette_color_cost_uv(pmi, |
| 5707 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 5708 | color_cache, n_cache, |
| 5709 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 5710 | cpi->common.bit_depth); |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 5711 | this_rate += |
| 5712 | 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] | 5713 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5714 | if (this_rd < *best_rd) { |
| 5715 | *best_rd = this_rd; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5716 | *best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5717 | memcpy(best_palette_color_map, color_map, |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5718 | plane_block_width * plane_block_height * |
| 5719 | sizeof(best_palette_color_map[0])); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5720 | *rate = this_rate; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5721 | *distortion = tokenonly_rd_stats.dist; |
| 5722 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 5723 | *skippable = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5724 | } |
| 5725 | } |
| 5726 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5727 | if (best_mbmi->palette_mode_info.palette_size[1] > 0) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5728 | memcpy(color_map, best_palette_color_map, |
Luc Trudeau | 0401e89 | 2017-08-31 00:37:11 -0400 | [diff] [blame] | 5729 | plane_block_width * plane_block_height * |
| 5730 | sizeof(best_palette_color_map[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5731 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5732 | } |
| 5733 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5734 | #if CONFIG_FILTER_INTRA |
| 5735 | // Return 1 if an filter intra mode is selected; return 0 otherwise. |
| 5736 | static int rd_pick_filter_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 5737 | int *rate, int *rate_tokenonly, |
| 5738 | int64_t *distortion, int *skippable, |
| 5739 | BLOCK_SIZE bsize, int64_t *best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5740 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5741 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5742 | int filter_intra_selected_flag = 0; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5743 | int this_rate; |
| 5744 | int64_t this_rd; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5745 | FILTER_INTRA_MODE mode; |
| 5746 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5747 | RD_STATS tokenonly_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5748 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5749 | av1_zero(filter_intra_mode_info); |
| 5750 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 1; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5751 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5752 | mbmi->palette_mode_info.palette_size[1] = 0; |
| 5753 | |
| 5754 | for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5755 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = mode; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5756 | if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5757 | continue; |
| 5758 | |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5759 | this_rate = tokenonly_rd_stats.rate + |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5760 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 1) + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5761 | x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode] + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5762 | write_uniform_cost(FILTER_INTRA_MODES, mode); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5763 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5764 | if (this_rd < *best_rd) { |
| 5765 | *best_rd = this_rd; |
| 5766 | *rate = this_rate; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5767 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 5768 | *distortion = tokenonly_rd_stats.dist; |
| 5769 | *skippable = tokenonly_rd_stats.skip; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5770 | filter_intra_mode_info = mbmi->filter_intra_mode_info; |
| 5771 | filter_intra_selected_flag = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5772 | } |
| 5773 | } |
| 5774 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5775 | if (filter_intra_selected_flag) { |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5776 | mbmi->uv_mode = UV_DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5777 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 5778 | filter_intra_mode_info.use_filter_intra_mode[1]; |
| 5779 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 5780 | filter_intra_mode_info.filter_intra_mode[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5781 | return 1; |
| 5782 | } else { |
| 5783 | return 0; |
| 5784 | } |
| 5785 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5786 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5787 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5788 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5789 | // Run RD calculation with given chroma intra prediction angle., and return |
| 5790 | // the RD cost. Update the best mode info. if the RD cost is the best so far. |
| 5791 | static int64_t pick_intra_angle_routine_sbuv( |
| 5792 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, |
| 5793 | int rate_overhead, int64_t best_rd_in, int *rate, RD_STATS *rd_stats, |
| 5794 | int *best_angle_delta, int64_t *best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5795 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5796 | assert(!is_inter_block(mbmi)); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5797 | int this_rate; |
| 5798 | int64_t this_rd; |
| 5799 | RD_STATS tokenonly_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5800 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5801 | if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in)) |
| 5802 | return INT64_MAX; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5803 | this_rate = tokenonly_rd_stats.rate + rate_overhead; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5804 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5805 | if (this_rd < *best_rd) { |
| 5806 | *best_rd = this_rd; |
| 5807 | *best_angle_delta = mbmi->angle_delta[1]; |
| 5808 | *rate = this_rate; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5809 | rd_stats->rate = tokenonly_rd_stats.rate; |
| 5810 | rd_stats->dist = tokenonly_rd_stats.dist; |
| 5811 | rd_stats->skip = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5812 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5813 | return this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5814 | } |
| 5815 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5816 | // With given chroma directional intra prediction mode, pick the best angle |
| 5817 | // 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] | 5818 | 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] | 5819 | BLOCK_SIZE bsize, int rate_overhead, |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5820 | int64_t best_rd, int *rate, |
| 5821 | RD_STATS *rd_stats) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5822 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5823 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5824 | assert(!is_inter_block(mbmi)); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5825 | int i, angle_delta, best_angle_delta = 0; |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5826 | 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] | 5827 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5828 | rd_stats->rate = INT_MAX; |
| 5829 | rd_stats->skip = 0; |
| 5830 | rd_stats->dist = INT64_MAX; |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5831 | 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] | 5832 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5833 | for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5834 | for (i = 0; i < 2; ++i) { |
| 5835 | best_rd_in = (best_rd == INT64_MAX) |
| 5836 | ? INT64_MAX |
| 5837 | : (best_rd + (best_rd >> ((angle_delta == 0) ? 3 : 5))); |
| 5838 | mbmi->angle_delta[1] = (1 - 2 * i) * angle_delta; |
| 5839 | this_rd = pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, |
| 5840 | best_rd_in, rate, rd_stats, |
| 5841 | &best_angle_delta, &best_rd); |
| 5842 | rd_cost[2 * angle_delta + i] = this_rd; |
| 5843 | if (angle_delta == 0) { |
| 5844 | if (this_rd == INT64_MAX) return 0; |
| 5845 | rd_cost[1] = this_rd; |
| 5846 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5847 | } |
| 5848 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5849 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5850 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5851 | assert(best_rd != INT64_MAX); |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5852 | for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5853 | int64_t rd_thresh; |
| 5854 | for (i = 0; i < 2; ++i) { |
| 5855 | int skip_search = 0; |
| 5856 | rd_thresh = best_rd + (best_rd >> 5); |
| 5857 | if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh && |
| 5858 | rd_cost[2 * (angle_delta - 1) + i] > rd_thresh) |
| 5859 | skip_search = 1; |
| 5860 | if (!skip_search) { |
| 5861 | mbmi->angle_delta[1] = (1 - 2 * i) * angle_delta; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 5862 | pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, best_rd, |
| 5863 | rate, rd_stats, &best_angle_delta, |
| 5864 | &best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5865 | } |
| 5866 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | mbmi->angle_delta[1] = best_angle_delta; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5870 | return rd_stats->rate != INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5871 | } |
| 5872 | #endif // CONFIG_EXT_INTRA |
| 5873 | |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5874 | #if CONFIG_CFL |
Luc Trudeau | 6066913 | 2017-09-28 16:49:49 -0400 | [diff] [blame] | 5875 | // TODO(ltrudeau) add support for HBD. |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5876 | static int64_t cfl_alpha_dist(const int16_t *pred_buf_q3, const uint8_t *src, |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5877 | int src_stride, int width, int height, |
| 5878 | int dc_pred, int alpha_q3, |
| 5879 | int64_t *dist_neg_out) { |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5880 | int64_t dist = 0; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5881 | int diff; |
| 5882 | |
| 5883 | if (alpha_q3 == 0) { |
| 5884 | for (int j = 0; j < height; j++) { |
| 5885 | for (int i = 0; i < width; i++) { |
| 5886 | diff = src[i] - dc_pred; |
| 5887 | dist += diff * diff; |
| 5888 | } |
| 5889 | src += src_stride; |
| 5890 | } |
| 5891 | |
| 5892 | if (dist_neg_out) *dist_neg_out = dist; |
| 5893 | |
| 5894 | return dist; |
| 5895 | } |
| 5896 | |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5897 | int64_t dist_neg = 0; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5898 | for (int j = 0; j < height; j++) { |
| 5899 | for (int i = 0; i < width; i++) { |
| 5900 | const int uv = src[i]; |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5901 | const int scaled_luma = get_scaled_luma_q0(alpha_q3, pred_buf_q3[i]); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5902 | |
Luc Trudeau | 6066913 | 2017-09-28 16:49:49 -0400 | [diff] [blame] | 5903 | diff = uv - clip_pixel(scaled_luma + dc_pred); |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5904 | dist += diff * diff; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5905 | |
Luc Trudeau | 6066913 | 2017-09-28 16:49:49 -0400 | [diff] [blame] | 5906 | diff = uv - clip_pixel(-scaled_luma + dc_pred); |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5907 | dist_neg += diff * diff; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5908 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5909 | pred_buf_q3 += MAX_SB_SIZE; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5910 | src += src_stride; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | if (dist_neg_out) *dist_neg_out = dist_neg; |
| 5914 | |
| 5915 | return dist; |
| 5916 | } |
| 5917 | |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5918 | static int cfl_rd_pick_alpha(MACROBLOCK *const x, TX_SIZE tx_size) { |
| 5919 | const struct macroblock_plane *const p_u = &x->plane[AOM_PLANE_U]; |
| 5920 | const struct macroblock_plane *const p_v = &x->plane[AOM_PLANE_V]; |
| 5921 | const uint8_t *const src_u = p_u->src.buf; |
| 5922 | const uint8_t *const src_v = p_v->src.buf; |
| 5923 | const int src_stride_u = p_u->src.stride; |
| 5924 | const int src_stride_v = p_v->src.stride; |
| 5925 | |
| 5926 | MACROBLOCKD *const xd = &x->e_mbd; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5927 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 5928 | |
| 5929 | CFL_CTX *const cfl = xd->cfl; |
| 5930 | cfl_compute_parameters(xd, tx_size); |
| 5931 | const int width = cfl->uv_width; |
| 5932 | const int height = cfl->uv_height; |
| 5933 | const int dc_pred_u = cfl->dc_pred[CFL_PRED_U]; |
| 5934 | const int dc_pred_v = cfl->dc_pred[CFL_PRED_V]; |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5935 | const int16_t *pred_buf_q3 = cfl->pred_buf_q3; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5936 | |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5937 | int64_t sse[CFL_PRED_PLANES][CFL_MAGS_SIZE]; |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5938 | sse[CFL_PRED_U][0] = cfl_alpha_dist(pred_buf_q3, src_u, src_stride_u, width, |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5939 | height, dc_pred_u, 0, NULL); |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5940 | sse[CFL_PRED_V][0] = cfl_alpha_dist(pred_buf_q3, src_v, src_stride_v, width, |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5941 | height, dc_pred_v, 0, NULL); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5942 | |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5943 | for (int c = 0; c < CFL_ALPHABET_SIZE; c++) { |
| 5944 | const int m = c * 2 + 1; |
| 5945 | const int abs_alpha_q3 = c + 1; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5946 | sse[CFL_PRED_U][m] = |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5947 | cfl_alpha_dist(pred_buf_q3, src_u, src_stride_u, width, height, |
| 5948 | dc_pred_u, abs_alpha_q3, &sse[CFL_PRED_U][m + 1]); |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5949 | sse[CFL_PRED_V][m] = |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5950 | cfl_alpha_dist(pred_buf_q3, src_v, src_stride_v, width, height, |
| 5951 | dc_pred_v, abs_alpha_q3, &sse[CFL_PRED_V][m + 1]); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5952 | } |
| 5953 | |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5954 | int64_t dist; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5955 | int64_t cost; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5956 | int64_t best_cost = INT64_MAX; |
| 5957 | int best_rate = 0; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5958 | |
| 5959 | // Compute least squares parameter of the entire block |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5960 | int ind = 0; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5961 | int signs = 0; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5962 | |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5963 | for (int joint_sign = 0; joint_sign < CFL_JOINT_SIGNS; joint_sign++) { |
| 5964 | const int sign_u = CFL_SIGN_U(joint_sign); |
| 5965 | const int sign_v = CFL_SIGN_V(joint_sign); |
| 5966 | const int size_u = (sign_u == CFL_SIGN_ZERO) ? 1 : CFL_ALPHABET_SIZE; |
| 5967 | const int size_v = (sign_v == CFL_SIGN_ZERO) ? 1 : CFL_ALPHABET_SIZE; |
| 5968 | for (int u = 0; u < size_u; u++) { |
| 5969 | const int idx_u = (sign_u == CFL_SIGN_ZERO) ? 0 : u * 2 + 1; |
| 5970 | for (int v = 0; v < size_v; v++) { |
| 5971 | const int idx_v = (sign_v == CFL_SIGN_ZERO) ? 0 : v * 2 + 1; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5972 | dist = sse[CFL_PRED_U][idx_u + (sign_u == CFL_SIGN_NEG)] + |
| 5973 | sse[CFL_PRED_V][idx_v + (sign_v == CFL_SIGN_NEG)]; |
| 5974 | dist *= 16; |
David Michael Barr | 38e560c | 2017-08-16 21:46:37 +0900 | [diff] [blame] | 5975 | const int rate = x->cfl_cost[joint_sign][CFL_PRED_U][u] + |
| 5976 | x->cfl_cost[joint_sign][CFL_PRED_V][v]; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5977 | cost = RDCOST(x->rdmult, rate, dist); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5978 | if (cost < best_cost) { |
| 5979 | best_cost = cost; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5980 | best_rate = rate; |
| 5981 | ind = (u << CFL_ALPHABET_SIZE_LOG2) + v; |
| 5982 | signs = joint_sign; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5983 | } |
| 5984 | } |
| 5985 | } |
| 5986 | } |
| 5987 | |
| 5988 | mbmi->cfl_alpha_idx = ind; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5989 | mbmi->cfl_alpha_signs = signs; |
| 5990 | return best_rate; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5991 | } |
| 5992 | #endif // CONFIG_CFL |
| 5993 | |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5994 | static void init_sbuv_mode(MB_MODE_INFO *const mbmi) { |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5995 | mbmi->uv_mode = UV_DC_PRED; |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5996 | mbmi->palette_mode_info.palette_size[1] = 0; |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5997 | #if CONFIG_FILTER_INTRA |
| 5998 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 5999 | #endif // CONFIG_FILTER_INTRA |
| 6000 | } |
| 6001 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6002 | static int64_t rd_pick_intra_sbuv_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 6003 | int *rate, int *rate_tokenonly, |
| 6004 | int64_t *distortion, int *skippable, |
| 6005 | BLOCK_SIZE bsize, TX_SIZE max_tx_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6006 | MACROBLOCKD *xd = &x->e_mbd; |
| 6007 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 6008 | assert(!is_inter_block(mbmi)); |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6009 | MB_MODE_INFO best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6010 | int64_t best_rd = INT64_MAX, this_rd; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 6011 | #if CONFIG_PVQ |
| 6012 | od_rollback_buffer buf; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 6013 | od_encode_checkpoint(&x->daala_enc, &buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6014 | #endif // CONFIG_PVQ |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 6015 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 6016 | const int try_palette = can_use_palette(cpi, mbmi); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 6017 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 6018 | for (int mode_idx = 0; mode_idx < UV_INTRA_MODES; ++mode_idx) { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 6019 | int this_rate; |
| 6020 | RD_STATS tokenonly_rd_stats; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 6021 | UV_PREDICTION_MODE mode = uv_rd_search_mode_order[mode_idx]; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6022 | #if CONFIG_EXT_INTRA |
| 6023 | const int is_directional_mode = |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 6024 | av1_is_directional_mode(get_uv_mode(mode), mbmi->sb_type); |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6025 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 6026 | if (!(cpi->sf.intra_uv_mode_mask[txsize_sqr_up_map[max_tx_size]] & |
| 6027 | (1 << mode))) |
| 6028 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6029 | |
| 6030 | mbmi->uv_mode = mode; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 6031 | #if CONFIG_CFL |
| 6032 | int cfl_alpha_rate = 0; |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 6033 | if (mode == UV_CFL_PRED) { |
| 6034 | assert(!is_directional_mode); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6035 | const TX_SIZE uv_tx_size = av1_get_uv_tx_size(mbmi, &xd->plane[1]); |
| 6036 | cfl_alpha_rate = cfl_rd_pick_alpha(x, uv_tx_size); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 6037 | } |
| 6038 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6039 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6040 | mbmi->angle_delta[1] = 0; |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 6041 | if (is_directional_mode && av1_use_angle_delta(mbmi->sb_type)) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6042 | const int rate_overhead = x->intra_uv_mode_cost[mbmi->mode][mode] + |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 6043 | write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, 0); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 6044 | if (!rd_pick_intra_angle_sbuv(cpi, x, bsize, rate_overhead, best_rd, |
| 6045 | &this_rate, &tokenonly_rd_stats)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6046 | continue; |
| 6047 | } else { |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6048 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 6049 | if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd)) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 6050 | #if CONFIG_PVQ |
| 6051 | od_encode_rollback(&x->daala_enc, &buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6052 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6053 | continue; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 6054 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6055 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6056 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6057 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 6058 | this_rate = |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6059 | tokenonly_rd_stats.rate + x->intra_uv_mode_cost[mbmi->mode][mode]; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6060 | |
Luc Trudeau | dff4192 | 2017-07-07 09:47:58 -0400 | [diff] [blame] | 6061 | #if CONFIG_CFL |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 6062 | if (mode == UV_CFL_PRED) { |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 6063 | this_rate += cfl_alpha_rate; |
Luc Trudeau | dff4192 | 2017-07-07 09:47:58 -0400 | [diff] [blame] | 6064 | } |
| 6065 | #endif |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6066 | #if CONFIG_EXT_INTRA |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 6067 | if (is_directional_mode && av1_use_angle_delta(mbmi->sb_type)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 6068 | this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 6069 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 6070 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6071 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 6072 | #if CONFIG_FILTER_INTRA |
Luc Trudeau | 91357ee | 2017-09-27 13:40:37 -0400 | [diff] [blame] | 6073 | if (mbmi->sb_type >= BLOCK_8X8 && mode == UV_DC_PRED) |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 6074 | this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 0); |
| 6075 | #endif // CONFIG_FILTER_INTRA |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 6076 | if (try_palette && mode == UV_DC_PRED) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6077 | this_rate += av1_cost_bit( |
| 6078 | av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6079 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 6080 | #if CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 6081 | od_encode_rollback(&x->daala_enc, &buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6082 | #endif // CONFIG_PVQ |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 6083 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6084 | |
| 6085 | if (this_rd < best_rd) { |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6086 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6087 | best_rd = this_rd; |
| 6088 | *rate = this_rate; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 6089 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 6090 | *distortion = tokenonly_rd_stats.dist; |
| 6091 | *skippable = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6092 | } |
| 6093 | } |
| 6094 | |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 6095 | if (try_palette) { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 6096 | 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] | 6097 | rd_pick_palette_intra_sbuv(cpi, x, |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6098 | x->intra_uv_mode_cost[mbmi->mode][UV_DC_PRED], |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6099 | best_palette_color_map, &best_mbmi, &best_rd, |
| 6100 | rate, rate_tokenonly, distortion, skippable); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6101 | } |
| 6102 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 6103 | #if CONFIG_FILTER_INTRA |
| 6104 | if (mbmi->sb_type >= BLOCK_8X8) { |
| 6105 | if (rd_pick_filter_intra_sbuv(cpi, x, rate, rate_tokenonly, distortion, |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6106 | skippable, bsize, &best_rd)) |
| 6107 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6108 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 6109 | #endif // CONFIG_FILTER_INTRA |
| 6110 | |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6111 | *mbmi = best_mbmi; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 6112 | // Make sure we actually chose a mode |
| 6113 | assert(best_rd < INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6114 | return best_rd; |
| 6115 | } |
| 6116 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6117 | 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] | 6118 | BLOCK_SIZE bsize, TX_SIZE max_tx_size, |
| 6119 | int *rate_uv, int *rate_uv_tokenonly, |
| 6120 | int64_t *dist_uv, int *skip_uv, |
| 6121 | UV_PREDICTION_MODE *mode_uv) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6122 | MACROBLOCKD *xd = &x->e_mbd; |
| 6123 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6124 | // Use an estimated rd for uv_intra based on DC_PRED if the |
| 6125 | // appropriate speed flag is set. |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6126 | init_sbuv_mode(mbmi); |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 6127 | #if CONFIG_CB4X4 |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 6128 | #if !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 6129 | if (x->skip_chroma_rd) { |
| 6130 | *rate_uv = 0; |
| 6131 | *rate_uv_tokenonly = 0; |
| 6132 | *dist_uv = 0; |
| 6133 | *skip_uv = 1; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 6134 | *mode_uv = UV_DC_PRED; |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 6135 | return; |
| 6136 | } |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6137 | bsize = scale_chroma_bsize(bsize, xd->plane[AOM_PLANE_U].subsampling_x, |
| 6138 | xd->plane[AOM_PLANE_U].subsampling_y); |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 6139 | #endif // !CONFIG_CHROMA_2X2 |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6140 | #if CONFIG_CFL |
| 6141 | // Only store reconstructed luma when there's chroma RDO. When there's no |
| 6142 | // chroma RDO, the reconstructed luma will be stored in encode_superblock(). |
| 6143 | xd->cfl->store_y = !x->skip_chroma_rd; |
| 6144 | #endif // CONFIG_CFL |
Jingning Han | 31b6a4f | 2017-02-23 11:05:53 -0800 | [diff] [blame] | 6145 | #else |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 6146 | bsize = bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize; |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6147 | #if CONFIG_CFL |
| 6148 | xd->cfl->store_y = 1; |
| 6149 | #endif // CONFIG_CFL |
Jingning Han | 31b6a4f | 2017-02-23 11:05:53 -0800 | [diff] [blame] | 6150 | #endif // CONFIG_CB4X4 |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6151 | #if CONFIG_CFL |
| 6152 | if (xd->cfl->store_y) { |
| 6153 | // Perform one extra call to txfm_rd_in_plane(), with the values chosen |
| 6154 | // during luma RDO, so we can store reconstructed luma values |
| 6155 | RD_STATS this_rd_stats; |
| 6156 | txfm_rd_in_plane(x, cpi, &this_rd_stats, INT64_MAX, AOM_PLANE_Y, |
| 6157 | mbmi->sb_type, mbmi->tx_size, |
| 6158 | cpi->sf.use_fast_coef_costing); |
| 6159 | xd->cfl->store_y = 0; |
| 6160 | } |
| 6161 | #endif // CONFIG_CFL |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 6162 | rd_pick_intra_sbuv_mode(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv, skip_uv, |
| 6163 | bsize, max_tx_size); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6164 | *mode_uv = mbmi->uv_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6165 | } |
| 6166 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6167 | static int cost_mv_ref(const MACROBLOCK *const x, PREDICTION_MODE mode, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6168 | int16_t mode_context) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6169 | #if CONFIG_EXT_INTER |
| 6170 | if (is_inter_compound_mode(mode)) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6171 | return x |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 6172 | ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6173 | #if CONFIG_COMPOUND_SINGLEREF |
| 6174 | } else if (is_inter_singleref_comp_mode(mode)) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6175 | return x->inter_singleref_comp_mode_cost[mode_context] |
| 6176 | [INTER_SINGLEREF_COMP_OFFSET(mode)]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6177 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6178 | } |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6179 | #endif |
| 6180 | |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6181 | int mode_cost = 0; |
| 6182 | int16_t mode_ctx = mode_context & NEWMV_CTX_MASK; |
| 6183 | int16_t is_all_zero_mv = mode_context & (1 << ALL_ZERO_FLAG_OFFSET); |
| 6184 | |
| 6185 | assert(is_inter_mode(mode)); |
| 6186 | |
| 6187 | if (mode == NEWMV) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6188 | mode_cost = x->newmv_mode_cost[mode_ctx][0]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6189 | return mode_cost; |
| 6190 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6191 | mode_cost = x->newmv_mode_cost[mode_ctx][1]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6192 | mode_ctx = (mode_context >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK; |
| 6193 | |
| 6194 | if (is_all_zero_mv) return mode_cost; |
| 6195 | |
| 6196 | if (mode == ZEROMV) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6197 | mode_cost += x->zeromv_mode_cost[mode_ctx][0]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6198 | return mode_cost; |
| 6199 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6200 | mode_cost += x->zeromv_mode_cost[mode_ctx][1]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6201 | mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK; |
| 6202 | |
| 6203 | if (mode_context & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6; |
| 6204 | if (mode_context & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7; |
| 6205 | if (mode_context & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8; |
| 6206 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6207 | mode_cost += x->refmv_mode_cost[mode_ctx][mode != NEARESTMV]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6208 | return mode_cost; |
| 6209 | } |
| 6210 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6211 | } |
| 6212 | |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 6213 | #if CONFIG_EXT_INTER && (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT) |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6214 | static int get_interinter_compound_type_bits(BLOCK_SIZE bsize, |
| 6215 | COMPOUND_TYPE comp_type) { |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 6216 | (void)bsize; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6217 | switch (comp_type) { |
| 6218 | case COMPOUND_AVERAGE: return 0; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 6219 | #if CONFIG_WEDGE |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6220 | case COMPOUND_WEDGE: return get_interinter_wedge_bits(bsize); |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 6221 | #endif // CONFIG_WEDGE |
Sarah Parker | 2f6ce75 | 2016-12-08 15:26:46 -0800 | [diff] [blame] | 6222 | #if CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 6223 | case COMPOUND_SEG: return 1; |
Sarah Parker | 2f6ce75 | 2016-12-08 15:26:46 -0800 | [diff] [blame] | 6224 | #endif // CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6225 | default: assert(0); return 0; |
| 6226 | } |
| 6227 | } |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 6228 | #endif // CONFIG_EXT_INTER && (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT) |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6229 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6230 | typedef struct { |
| 6231 | int eobs; |
| 6232 | int brate; |
| 6233 | int byrate; |
| 6234 | int64_t bdist; |
| 6235 | int64_t bsse; |
| 6236 | int64_t brdcost; |
| 6237 | int_mv mvs[2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6238 | int_mv pred_mv[2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6239 | #if CONFIG_EXT_INTER |
| 6240 | int_mv ref_mv[2]; |
| 6241 | #endif // CONFIG_EXT_INTER |
Jingning Han | 276c294 | 2016-12-05 12:37:02 -0800 | [diff] [blame] | 6242 | |
Timothy B. Terriberry | fe67ed6 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 6243 | #if CONFIG_CHROMA_2X2 |
Jingning Han | 276c294 | 2016-12-05 12:37:02 -0800 | [diff] [blame] | 6244 | ENTROPY_CONTEXT ta[4]; |
| 6245 | ENTROPY_CONTEXT tl[4]; |
| 6246 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6247 | ENTROPY_CONTEXT ta[2]; |
| 6248 | ENTROPY_CONTEXT tl[2]; |
Timothy B. Terriberry | fe67ed6 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 6249 | #endif // CONFIG_CHROMA_2X2 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6250 | } SEG_RDSTAT; |
| 6251 | |
| 6252 | typedef struct { |
| 6253 | int_mv *ref_mv[2]; |
| 6254 | int_mv mvp; |
| 6255 | |
| 6256 | int64_t segment_rd; |
| 6257 | int r; |
| 6258 | int64_t d; |
| 6259 | int64_t sse; |
| 6260 | int segment_yrate; |
| 6261 | PREDICTION_MODE modes[4]; |
| 6262 | #if CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6263 | #if CONFIG_COMPOUND_SINGLEREF |
| 6264 | SEG_RDSTAT rdstat[4][INTER_MODES + INTER_SINGLEREF_COMP_MODES + |
| 6265 | INTER_COMPOUND_MODES]; |
| 6266 | #else // !CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6267 | SEG_RDSTAT rdstat[4][INTER_MODES + INTER_COMPOUND_MODES]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6268 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 6269 | #else // !CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6270 | SEG_RDSTAT rdstat[4][INTER_MODES]; |
| 6271 | #endif // CONFIG_EXT_INTER |
| 6272 | int mvthresh; |
| 6273 | } BEST_SEG_INFO; |
| 6274 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6275 | static INLINE int mv_check_bounds(const MvLimits *mv_limits, const MV *mv) { |
| 6276 | return (mv->row >> 3) < mv_limits->row_min || |
| 6277 | (mv->row >> 3) > mv_limits->row_max || |
| 6278 | (mv->col >> 3) < mv_limits->col_min || |
| 6279 | (mv->col >> 3) > mv_limits->col_max; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6280 | } |
| 6281 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6282 | // Check if NEARESTMV/NEARMV/ZEROMV is the cheapest way encode zero motion. |
| 6283 | // TODO(aconverse): Find out if this is still productive then clean up or remove |
| 6284 | static int check_best_zero_mv( |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6285 | const AV1_COMP *const cpi, const MACROBLOCK *const x, |
| 6286 | const int16_t mode_context[TOTAL_REFS_PER_FRAME], |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 6287 | #if CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6288 | const int16_t compound_mode_context[TOTAL_REFS_PER_FRAME], |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 6289 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6290 | 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] | 6291 | const MV_REFERENCE_FRAME ref_frames[2], const BLOCK_SIZE bsize, int block, |
| 6292 | int mi_row, int mi_col) { |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6293 | int_mv zeromv[2] = { {.as_int = 0 } }; |
Yushin Cho | c9751c5 | 2017-06-12 10:38:29 -0700 | [diff] [blame] | 6294 | #if CONFIG_GLOBAL_MOTION |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6295 | int comp_pred_mode = ref_frames[1] > INTRA_FRAME; |
Yushin Cho | c9751c5 | 2017-06-12 10:38:29 -0700 | [diff] [blame] | 6296 | #endif |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 6297 | (void)mi_row; |
| 6298 | (void)mi_col; |
Zoe Liu | bc030ee | 2017-07-31 15:20:46 -0700 | [diff] [blame] | 6299 | (void)cpi; |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6300 | #if CONFIG_GLOBAL_MOTION |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6301 | if (this_mode == ZEROMV |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6302 | #if CONFIG_EXT_INTER |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6303 | || this_mode == ZERO_ZEROMV |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6304 | #endif // CONFIG_EXT_INTER |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6305 | ) { |
| 6306 | 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] | 6307 | zeromv[cur_frm].as_int = |
| 6308 | gm_get_motion_vector(&cpi->common.global_motion[ref_frames[cur_frm]], |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 6309 | cpi->common.allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6310 | mi_col, mi_row, block |
| 6311 | #if CONFIG_AMVR |
| 6312 | , |
| 6313 | cpi->common.cur_frame_mv_precision_level |
| 6314 | #endif |
| 6315 | ) |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6316 | .as_int; |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6317 | } |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6318 | } |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6319 | #endif // CONFIG_GLOBAL_MOTION |
| 6320 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6321 | #if !CONFIG_EXT_INTER |
| 6322 | assert(ref_frames[1] != INTRA_FRAME); // Just sanity check |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6323 | #endif // !CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6324 | if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) && |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6325 | 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] | 6326 | (ref_frames[1] <= INTRA_FRAME || |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6327 | 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] | 6328 | int16_t rfc = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6329 | av1_mode_context_analyzer(mode_context, ref_frames, bsize, block); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6330 | int c1 = cost_mv_ref(x, NEARMV, rfc); |
| 6331 | int c2 = cost_mv_ref(x, NEARESTMV, rfc); |
| 6332 | int c3 = cost_mv_ref(x, ZEROMV, rfc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6333 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6334 | if (this_mode == NEARMV) { |
| 6335 | if (c1 > c3) return 0; |
| 6336 | } else if (this_mode == NEARESTMV) { |
| 6337 | if (c2 > c3) return 0; |
| 6338 | } else { |
| 6339 | assert(this_mode == ZEROMV); |
| 6340 | if (ref_frames[1] <= INTRA_FRAME) { |
| 6341 | if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0) || |
| 6342 | (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0)) |
| 6343 | return 0; |
| 6344 | } else { |
| 6345 | if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0 && |
| 6346 | frame_mv[NEARESTMV][ref_frames[1]].as_int == 0) || |
| 6347 | (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0 && |
| 6348 | frame_mv[NEARMV][ref_frames[1]].as_int == 0)) |
| 6349 | return 0; |
| 6350 | } |
| 6351 | } |
| 6352 | } |
| 6353 | #if CONFIG_EXT_INTER |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 6354 | else if ((this_mode == NEAREST_NEARESTMV || this_mode == NEAR_NEARMV || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6355 | this_mode == ZERO_ZEROMV) && |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6356 | frame_mv[this_mode][ref_frames[0]].as_int == zeromv[0].as_int && |
| 6357 | 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] | 6358 | int16_t rfc = compound_mode_context[ref_frames[0]]; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6359 | int c2 = cost_mv_ref(x, NEAREST_NEARESTMV, rfc); |
| 6360 | int c3 = cost_mv_ref(x, ZERO_ZEROMV, rfc); |
| 6361 | int c5 = cost_mv_ref(x, NEAR_NEARMV, rfc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6362 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 6363 | if (this_mode == NEAREST_NEARESTMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6364 | if (c2 > c3) return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6365 | } else if (this_mode == NEAR_NEARMV) { |
| 6366 | if (c5 > c3) return 0; |
| 6367 | } else { |
| 6368 | assert(this_mode == ZERO_ZEROMV); |
| 6369 | if ((c3 >= c2 && frame_mv[NEAREST_NEARESTMV][ref_frames[0]].as_int == 0 && |
| 6370 | frame_mv[NEAREST_NEARESTMV][ref_frames[1]].as_int == 0) || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6371 | (c3 >= c5 && frame_mv[NEAR_NEARMV][ref_frames[0]].as_int == 0 && |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 6372 | frame_mv[NEAR_NEARMV][ref_frames[1]].as_int == 0)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6373 | return 0; |
| 6374 | } |
| 6375 | } |
| 6376 | #endif // CONFIG_EXT_INTER |
| 6377 | return 1; |
| 6378 | } |
| 6379 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6380 | static void joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x, |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6381 | BLOCK_SIZE bsize, int_mv *frame_mv, |
| 6382 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6383 | int_mv *frame_comp_mv, |
| 6384 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6385 | int mi_row, int mi_col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6386 | #if CONFIG_EXT_INTER |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6387 | int_mv *ref_mv_sub8x8[2], const uint8_t *mask, |
| 6388 | int mask_stride, |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6389 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6390 | int *rate_mv, const int block) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6391 | const AV1_COMMON *const cm = &cpi->common; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 6392 | const int pw = block_size_wide[bsize]; |
| 6393 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6394 | MACROBLOCKD *xd = &x->e_mbd; |
| 6395 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6396 | // This function should only ever be called for compound modes |
| 6397 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6398 | if (!has_second_ref(mbmi)) { |
| 6399 | assert(is_inter_singleref_comp_mode(mbmi->mode)); |
| 6400 | assert(frame_comp_mv); |
| 6401 | } |
| 6402 | assert(has_second_ref(mbmi) || is_inter_singleref_comp_mode(mbmi->mode)); |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 6403 | const int refs[2] = { mbmi->ref_frame[0], |
| 6404 | has_second_ref(mbmi) ? mbmi->ref_frame[1] |
| 6405 | : mbmi->ref_frame[0] }; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6406 | #else |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6407 | assert(has_second_ref(mbmi)); |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 6408 | const int refs[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] }; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6409 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6410 | int_mv ref_mv[2]; |
| 6411 | int ite, ref; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6412 | struct scale_factors sf; |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6413 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 6414 | // 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] | 6415 | const int ic = block & 1; |
| 6416 | const int ir = (block - ic) >> 1; |
Jingning Han | cb63767 | 2017-06-22 09:14:40 -0700 | [diff] [blame] | 6417 | struct macroblockd_plane *const pd = &xd->plane[0]; |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6418 | const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x) + 4 * ic; |
| 6419 | const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y) + 4 * ir; |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6420 | #if CONFIG_GLOBAL_MOTION |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6421 | int is_global[2]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6422 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6423 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 6424 | #else |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6425 | for (ref = 0; ref < 2; ++ref) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6426 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6427 | WarpedMotionParams *const wm = |
| 6428 | &xd->global_motion[xd->mi[0]->mbmi.ref_frame[ref]]; |
| 6429 | is_global[ref] = is_global_mv_block(xd->mi[0], block, wm->wmtype); |
| 6430 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6431 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6432 | if (!has_second_ref(mbmi)) is_global[1] = is_global[0]; |
| 6433 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6434 | #endif // CONFIG_GLOBAL_MOTION |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6435 | #else // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 6436 | (void)block; |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6437 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6438 | |
| 6439 | // Do joint motion search in compound mode to get more accurate mv. |
| 6440 | struct buf_2d backup_yv12[2][MAX_MB_PLANE]; |
| 6441 | int last_besterr[2] = { INT_MAX, INT_MAX }; |
| 6442 | const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = { |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 6443 | av1_get_scaled_ref_frame(cpi, refs[0]), |
| 6444 | av1_get_scaled_ref_frame(cpi, refs[1]) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6445 | }; |
| 6446 | |
| 6447 | // Prediction buffer from second frame. |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6448 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6449 | DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]); |
| 6450 | uint8_t *second_pred; |
| 6451 | #else |
| 6452 | DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6453 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6454 | |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6455 | #if CONFIG_EXT_INTER && CONFIG_CB4X4 |
| 6456 | (void)ref_mv_sub8x8; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6457 | #endif // CONFIG_EXT_INTER && CONFIG_CB4X4 |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6458 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6459 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6460 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 6461 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6462 | for (ref = 0; ref < 2; ++ref) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6463 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6464 | #if CONFIG_EXT_INTER && !CONFIG_CB4X4 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6465 | if (bsize < BLOCK_8X8 && ref_mv_sub8x8 != NULL) |
| 6466 | ref_mv[ref].as_int = ref_mv_sub8x8[ref]->as_int; |
| 6467 | else |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6468 | #endif // CONFIG_EXT_INTER && !CONFIG_CB4X4 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6469 | ref_mv[ref] = x->mbmi_ext->ref_mvs[refs[ref]][0]; |
| 6470 | |
| 6471 | if (scaled_ref_frame[ref]) { |
| 6472 | int i; |
| 6473 | // Swap out the reference frame for a version that's been scaled to |
| 6474 | // match the resolution of the current frame, allowing the existing |
| 6475 | // motion search code to be used without additional modifications. |
| 6476 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6477 | backup_yv12[ref][i] = xd->plane[i].pre[ref]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6478 | av1_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col, |
| 6479 | NULL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6480 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6481 | } |
| 6482 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6483 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6484 | if (!has_second_ref(mbmi)) { |
| 6485 | assert(is_inter_singleref_comp_mode(mbmi->mode)); |
| 6486 | // NOTE: For single ref comp mode, set up the 2nd set of ref_mv/pre_planes |
| 6487 | // all from the 1st reference frame, i.e. refs[0]. |
| 6488 | ref_mv[1] = x->mbmi_ext->ref_mvs[refs[0]][0]; |
| 6489 | if (scaled_ref_frame[0]) { |
| 6490 | int i; |
| 6491 | // Swap out the reference frame for a version that's been scaled to |
| 6492 | // match the resolution of the current frame, allowing the existing |
| 6493 | // motion search code to be used without additional modifications. |
| 6494 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6495 | backup_yv12[1][i] = xd->plane[i].pre[1]; |
| 6496 | av1_setup_pre_planes(xd, 1, scaled_ref_frame[0], mi_row, mi_col, NULL); |
| 6497 | } |
| 6498 | } |
| 6499 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6500 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6501 | // Since we have scaled the reference frames to match the size of the current |
| 6502 | // frame we must use a unit scaling factor during mode selection. |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6503 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6504 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 6505 | cm->height, cm->use_highbitdepth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6506 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6507 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 6508 | cm->height); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6509 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6510 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6511 | // Allow joint search multiple times iteratively for each reference frame |
| 6512 | // and break out of the search loop if it couldn't find a better mv. |
| 6513 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6514 | const int num_ites = |
| 6515 | (has_second_ref(mbmi) || mbmi->mode == SR_NEW_NEWMV) ? 4 : 1; |
| 6516 | const int start_ite = has_second_ref(mbmi) ? 0 : 1; |
| 6517 | for (ite = start_ite; ite < (start_ite + num_ites); ite++) { |
| 6518 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6519 | for (ite = 0; ite < 4; ite++) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6520 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6521 | struct buf_2d ref_yv12[2]; |
| 6522 | int bestsme = INT_MAX; |
| 6523 | int sadpb = x->sadperbit16; |
| 6524 | MV *const best_mv = &x->best_mv.as_mv; |
| 6525 | int search_range = 3; |
| 6526 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6527 | MvLimits tmp_mv_limits = x->mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6528 | int id = ite % 2; // Even iterations search in the first reference frame, |
| 6529 | // odd iterations search in the second. The predictor |
| 6530 | // found for the 'other' reference frame is factored in. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 6531 | const int plane = 0; |
David Barker | e64d51a | 2017-06-09 14:52:42 +0100 | [diff] [blame] | 6532 | ConvolveParams conv_params = get_conv_params(!id, 0, plane); |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6533 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 6534 | WarpTypesAllowed warp_types; |
| 6535 | #if CONFIG_GLOBAL_MOTION |
| 6536 | warp_types.global_warp_allowed = is_global[!id]; |
| 6537 | #endif // CONFIG_GLOBAL_MOTION |
| 6538 | #if CONFIG_WARPED_MOTION |
| 6539 | warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL; |
| 6540 | #endif // CONFIG_WARPED_MOTION |
| 6541 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6542 | |
| 6543 | // Initialized here because of compiler problem in Visual Studio. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 6544 | ref_yv12[0] = xd->plane[plane].pre[0]; |
| 6545 | ref_yv12[1] = xd->plane[plane].pre[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6546 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6547 | // Get the prediction block from the 'other' reference frame. |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6548 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6549 | MV *const the_other_mv = (has_second_ref(mbmi) || id) |
| 6550 | ? &frame_mv[refs[!id]].as_mv |
| 6551 | : &frame_comp_mv[refs[0]].as_mv; |
| 6552 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6553 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6554 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6555 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 6556 | second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6557 | av1_highbd_build_inter_predictor( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6558 | ref_yv12[!id].buf, ref_yv12[!id].stride, second_pred, pw, |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6559 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6560 | the_other_mv, |
| 6561 | #else // !(CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF) |
| 6562 | &frame_mv[refs[!id]].as_mv, |
| 6563 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6564 | &sf, pw, ph, 0, mbmi->interp_filter, |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6565 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 6566 | &warp_types, p_col, p_row, |
| 6567 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6568 | plane, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6569 | } else { |
| 6570 | second_pred = (uint8_t *)second_pred_alloc_16; |
Zoe Liu | 76fcff7 | 2017-04-24 17:50:53 -0700 | [diff] [blame] | 6571 | #endif // CONFIG_HIGHBITDEPTH |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6572 | av1_build_inter_predictor( |
| 6573 | ref_yv12[!id].buf, ref_yv12[!id].stride, second_pred, pw, |
| 6574 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6575 | the_other_mv, |
| 6576 | #else // !(CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF) |
| 6577 | &frame_mv[refs[!id]].as_mv, |
| 6578 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6579 | &sf, pw, ph, &conv_params, mbmi->interp_filter, |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6580 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6581 | &warp_types, p_col, p_row, plane, !id, |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6582 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6583 | MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
Zoe Liu | 76fcff7 | 2017-04-24 17:50:53 -0700 | [diff] [blame] | 6584 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6585 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6586 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6587 | |
| 6588 | // Do compound motion search on the current reference frame. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 6589 | if (id) xd->plane[plane].pre[0] = ref_yv12[id]; |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6590 | 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] | 6591 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6592 | // Use the mv result from the single mode as mv predictor. |
| 6593 | // Use the mv result from the single mode as mv predictor. |
| 6594 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6595 | if (!has_second_ref(mbmi) && id) |
| 6596 | *best_mv = frame_comp_mv[refs[0]].as_mv; |
| 6597 | else |
| 6598 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6599 | *best_mv = frame_mv[refs[id]].as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6600 | |
| 6601 | best_mv->col >>= 3; |
| 6602 | best_mv->row >>= 3; |
| 6603 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6604 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6605 | if (!has_second_ref(mbmi)) |
| 6606 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
| 6607 | else |
| 6608 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6609 | av1_set_mvcost(x, refs[id], id, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6610 | |
| 6611 | // Small-range full-pixel motion search. |
| 6612 | bestsme = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6613 | av1_refining_search_8p_c(x, sadpb, search_range, &cpi->fn_ptr[bsize], |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6614 | #if CONFIG_EXT_INTER |
| 6615 | mask, mask_stride, id, |
| 6616 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6617 | &ref_mv[id].as_mv, second_pred); |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6618 | if (bestsme < INT_MAX) { |
| 6619 | #if CONFIG_EXT_INTER |
| 6620 | if (mask) |
| 6621 | bestsme = av1_get_mvpred_mask_var(x, best_mv, &ref_mv[id].as_mv, |
| 6622 | second_pred, mask, mask_stride, id, |
| 6623 | &cpi->fn_ptr[bsize], 1); |
| 6624 | else |
| 6625 | #endif |
| 6626 | bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv[id].as_mv, |
| 6627 | second_pred, &cpi->fn_ptr[bsize], 1); |
| 6628 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6629 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6630 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6631 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6632 | #if CONFIG_AMVR |
| 6633 | if (cpi->common.cur_frame_mv_precision_level) { |
| 6634 | x->best_mv.as_mv.row *= 8; |
| 6635 | x->best_mv.as_mv.col *= 8; |
| 6636 | } |
| 6637 | if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) { |
| 6638 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6639 | if (bestsme < INT_MAX) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6640 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6641 | int dis; /* TODO: use dis in distortion calculation later. */ |
| 6642 | unsigned int sse; |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6643 | bestsme = cpi->find_fractional_mv_step( |
| 6644 | x, &ref_mv[id].as_mv, cpi->common.allow_high_precision_mv, |
| 6645 | x->errorperbit, &cpi->fn_ptr[bsize], 0, |
| 6646 | cpi->sf.mv.subpel_iters_per_step, NULL, x->nmvjointcost, x->mvcost, |
| 6647 | &dis, &sse, second_pred, |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6648 | #if CONFIG_EXT_INTER |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6649 | mask, mask_stride, id, |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6650 | #endif |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6651 | pw, ph, cpi->sf.use_upsampled_references); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6652 | } |
| 6653 | |
| 6654 | // Restore the pointer to the first (possibly scaled) prediction buffer. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 6655 | if (id) xd->plane[plane].pre[0] = ref_yv12[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6656 | |
| 6657 | if (bestsme < last_besterr[id]) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6658 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6659 | // NOTE: For single ref comp mode, frame_mv stores the first mv and |
| 6660 | // frame_comp_mv stores the second mv. |
| 6661 | if (!has_second_ref(mbmi) && id) |
| 6662 | frame_comp_mv[refs[0]].as_mv = *best_mv; |
| 6663 | else |
| 6664 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6665 | frame_mv[refs[id]].as_mv = *best_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6666 | last_besterr[id] = bestsme; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6667 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6668 | if (!has_second_ref(mbmi)) last_besterr[!id] = last_besterr[id]; |
| 6669 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6670 | } else { |
| 6671 | break; |
| 6672 | } |
| 6673 | } |
| 6674 | |
| 6675 | *rate_mv = 0; |
| 6676 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6677 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6678 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 6679 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6680 | for (ref = 0; ref < 2; ++ref) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6681 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6682 | if (scaled_ref_frame[ref]) { |
| 6683 | // Restore the prediction frame pointers to their unscaled versions. |
| 6684 | int i; |
| 6685 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6686 | xd->plane[i].pre[ref] = backup_yv12[ref][i]; |
| 6687 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6688 | |
| 6689 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6690 | if (!has_second_ref(mbmi)) |
| 6691 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6692 | else |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6693 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6694 | av1_set_mvcost(x, refs[ref], ref, mbmi->ref_mv_idx); |
| 6695 | |
| 6696 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6697 | if (!has_second_ref(mbmi)) { |
| 6698 | // NOTE: For single ref comp mode, i.e. !has_second_ref(mbmi) is true, the |
| 6699 | // first mv is stored in frame_mv[] and the second mv is stored in |
| 6700 | // frame_comp_mv[]. |
| 6701 | if (compound_ref0_mode(mbmi->mode) == NEWMV) // SR_NEW_NEWMV |
| 6702 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
| 6703 | &x->mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 6704 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 6705 | assert(compound_ref1_mode(mbmi->mode) == NEWMV); |
| 6706 | *rate_mv += av1_mv_bit_cost(&frame_comp_mv[refs[0]].as_mv, |
| 6707 | &x->mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 6708 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 6709 | } else { |
| 6710 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6711 | #if CONFIG_EXT_INTER && !CONFIG_CB4X4 |
| 6712 | if (bsize >= BLOCK_8X8) |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6713 | #endif // CONFIG_EXT_INTER && !CONFIG_CB4X4 |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6714 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv, |
| 6715 | &x->mbmi_ext->ref_mvs[refs[ref]][0].as_mv, |
| 6716 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 6717 | #if CONFIG_EXT_INTER && !CONFIG_CB4X4 |
| 6718 | else |
| 6719 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv, |
| 6720 | &ref_mv_sub8x8[ref]->as_mv, x->nmvjointcost, |
| 6721 | x->mvcost, MV_COST_WEIGHT); |
| 6722 | #endif // CONFIG_EXT_INTER && !CONFIG_CB4X4 |
| 6723 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6724 | } |
| 6725 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6726 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6727 | |
| 6728 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 6729 | if (!has_second_ref(mbmi)) { |
| 6730 | if (scaled_ref_frame[0]) { |
| 6731 | // Restore the prediction frame pointers to their unscaled versions. |
| 6732 | int i; |
| 6733 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6734 | xd->plane[i].pre[1] = backup_yv12[1][i]; |
| 6735 | } |
| 6736 | } |
| 6737 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6738 | } |
| 6739 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6740 | static void estimate_ref_frame_costs( |
| 6741 | const AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id, |
| 6742 | unsigned int *ref_costs_single, |
| 6743 | #if CONFIG_EXT_COMP_REFS |
| 6744 | unsigned int (*ref_costs_comp)[TOTAL_REFS_PER_FRAME], |
| 6745 | #else |
| 6746 | unsigned int *ref_costs_comp, |
| 6747 | #endif // CONFIG_EXT_COMP_REFS |
| 6748 | aom_prob *comp_mode_p) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6749 | int seg_ref_active = |
| 6750 | segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME); |
| 6751 | if (seg_ref_active) { |
| 6752 | memset(ref_costs_single, 0, |
| 6753 | TOTAL_REFS_PER_FRAME * sizeof(*ref_costs_single)); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6754 | #if CONFIG_EXT_COMP_REFS |
| 6755 | int ref_frame; |
| 6756 | for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame) |
| 6757 | memset(ref_costs_comp[ref_frame], 0, |
| 6758 | TOTAL_REFS_PER_FRAME * sizeof((*ref_costs_comp)[0])); |
| 6759 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6760 | 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] | 6761 | #endif // CONFIG_EXT_COMP_REFS |
| 6762 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6763 | *comp_mode_p = 128; |
| 6764 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6765 | aom_prob intra_inter_p = av1_get_intra_inter_prob(cm, xd); |
| 6766 | aom_prob comp_inter_p = 128; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6767 | |
| 6768 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6769 | comp_inter_p = av1_get_reference_mode_prob(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6770 | *comp_mode_p = comp_inter_p; |
| 6771 | } else { |
| 6772 | *comp_mode_p = 128; |
| 6773 | } |
| 6774 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6775 | ref_costs_single[INTRA_FRAME] = av1_cost_bit(intra_inter_p, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6776 | |
| 6777 | if (cm->reference_mode != COMPOUND_REFERENCE) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6778 | aom_prob ref_single_p1 = av1_get_pred_prob_single_ref_p1(cm, xd); |
| 6779 | aom_prob ref_single_p2 = av1_get_pred_prob_single_ref_p2(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6780 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6781 | aom_prob ref_single_p3 = av1_get_pred_prob_single_ref_p3(cm, xd); |
| 6782 | aom_prob ref_single_p4 = av1_get_pred_prob_single_ref_p4(cm, xd); |
| 6783 | 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] | 6784 | 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] | 6785 | #endif // CONFIG_EXT_REFS |
| 6786 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6787 | unsigned int base_cost = av1_cost_bit(intra_inter_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6788 | |
| 6789 | ref_costs_single[LAST_FRAME] = |
| 6790 | #if CONFIG_EXT_REFS |
| 6791 | ref_costs_single[LAST2_FRAME] = ref_costs_single[LAST3_FRAME] = |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6792 | ref_costs_single[BWDREF_FRAME] = ref_costs_single[ALTREF2_FRAME] = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6793 | #endif // CONFIG_EXT_REFS |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6794 | ref_costs_single[GOLDEN_FRAME] = |
| 6795 | ref_costs_single[ALTREF_FRAME] = base_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6796 | |
| 6797 | #if CONFIG_EXT_REFS |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6798 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6799 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6800 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6801 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6802 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6803 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6804 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6805 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6806 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p3, 0); |
| 6807 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p3, 0); |
| 6808 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p3, 1); |
| 6809 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p3, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6810 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6811 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p2, 0); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6812 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p2, 0); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6813 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6814 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6815 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p4, 0); |
| 6816 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p4, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6817 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6818 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p5, 0); |
| 6819 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p5, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6820 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6821 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p6, 0); |
| 6822 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p6, 1); |
Zoe Liu | 7b1ec7a | 2017-05-24 22:28:24 -0700 | [diff] [blame] | 6823 | #else // !CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6824 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6825 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 1); |
| 6826 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6827 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6828 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p2, 0); |
| 6829 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6830 | #endif // CONFIG_EXT_REFS |
| 6831 | } else { |
| 6832 | ref_costs_single[LAST_FRAME] = 512; |
| 6833 | #if CONFIG_EXT_REFS |
| 6834 | ref_costs_single[LAST2_FRAME] = 512; |
| 6835 | ref_costs_single[LAST3_FRAME] = 512; |
| 6836 | ref_costs_single[BWDREF_FRAME] = 512; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6837 | ref_costs_single[ALTREF2_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6838 | #endif // CONFIG_EXT_REFS |
| 6839 | ref_costs_single[GOLDEN_FRAME] = 512; |
| 6840 | ref_costs_single[ALTREF_FRAME] = 512; |
| 6841 | } |
| 6842 | |
| 6843 | if (cm->reference_mode != SINGLE_REFERENCE) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6844 | aom_prob ref_comp_p = av1_get_pred_prob_comp_ref_p(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6845 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6846 | aom_prob ref_comp_p1 = av1_get_pred_prob_comp_ref_p1(cm, xd); |
| 6847 | aom_prob ref_comp_p2 = av1_get_pred_prob_comp_ref_p2(cm, xd); |
| 6848 | 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] | 6849 | 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] | 6850 | #endif // CONFIG_EXT_REFS |
| 6851 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6852 | unsigned int base_cost = av1_cost_bit(intra_inter_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6853 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6854 | #if CONFIG_EXT_COMP_REFS |
| 6855 | aom_prob comp_ref_type_p = av1_get_comp_reference_type_prob(cm, xd); |
| 6856 | unsigned int ref_bicomp_costs[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 6857 | |
| 6858 | ref_bicomp_costs[LAST_FRAME] = ref_bicomp_costs[LAST2_FRAME] = |
| 6859 | ref_bicomp_costs[LAST3_FRAME] = ref_bicomp_costs[GOLDEN_FRAME] = |
| 6860 | #if USE_UNI_COMP_REFS |
| 6861 | base_cost + av1_cost_bit(comp_ref_type_p, 1); |
| 6862 | #else |
| 6863 | base_cost; |
| 6864 | #endif // USE_UNI_COMP_REFS |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6865 | ref_bicomp_costs[BWDREF_FRAME] = ref_bicomp_costs[ALTREF2_FRAME] = 0; |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6866 | ref_bicomp_costs[ALTREF_FRAME] = 0; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6867 | |
| 6868 | ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6869 | ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6870 | ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6871 | ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6872 | |
| 6873 | ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1); |
| 6874 | ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0); |
| 6875 | |
| 6876 | ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0); |
| 6877 | ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1); |
| 6878 | |
| 6879 | ref_bicomp_costs[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6880 | ref_bicomp_costs[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6881 | ref_bicomp_costs[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1); |
| 6882 | |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6883 | ref_bicomp_costs[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p1, 0); |
| 6884 | ref_bicomp_costs[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p1, 1); |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6885 | |
| 6886 | int ref0, ref1; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6887 | for (ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) { |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6888 | for (ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) { |
| 6889 | ref_costs_comp[ref0][ref1] = |
| 6890 | ref_bicomp_costs[ref0] + ref_bicomp_costs[ref1]; |
| 6891 | } |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6892 | } |
| 6893 | |
| 6894 | aom_prob uni_comp_ref_p = av1_get_pred_prob_uni_comp_ref_p(cm, xd); |
| 6895 | 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] | 6896 | 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] | 6897 | |
| 6898 | ref_costs_comp[LAST_FRAME][LAST2_FRAME] = |
| 6899 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6900 | 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] | 6901 | ref_costs_comp[LAST_FRAME][LAST3_FRAME] = |
| 6902 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6903 | av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 1) + |
| 6904 | av1_cost_bit(uni_comp_ref_p2, 0); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6905 | ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = |
| 6906 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6907 | av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 1) + |
| 6908 | av1_cost_bit(uni_comp_ref_p2, 1); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6909 | |
| 6910 | ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = |
| 6911 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6912 | av1_cost_bit(uni_comp_ref_p, 1); |
| 6913 | |
| 6914 | #else // !CONFIG_EXT_COMP_REFS |
| 6915 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6916 | ref_costs_comp[LAST_FRAME] = |
| 6917 | #if CONFIG_EXT_REFS |
| 6918 | ref_costs_comp[LAST2_FRAME] = ref_costs_comp[LAST3_FRAME] = |
| 6919 | #endif // CONFIG_EXT_REFS |
| 6920 | ref_costs_comp[GOLDEN_FRAME] = base_cost; |
| 6921 | |
| 6922 | #if CONFIG_EXT_REFS |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6923 | ref_costs_comp[BWDREF_FRAME] = ref_costs_comp[ALTREF2_FRAME] = |
| 6924 | ref_costs_comp[ALTREF_FRAME] = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6925 | #endif // CONFIG_EXT_REFS |
| 6926 | |
| 6927 | #if CONFIG_EXT_REFS |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6928 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6929 | ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6930 | ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6931 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6932 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6933 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1); |
| 6934 | ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6935 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6936 | ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0); |
| 6937 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6938 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6939 | // NOTE(zoeliu): BWDREF and ALTREF each add an extra cost by coding 1 |
| 6940 | // more bit. |
| 6941 | ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6942 | ref_costs_comp[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6943 | ref_costs_comp[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6944 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6945 | ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p1, 0); |
| 6946 | ref_costs_comp[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p1, 1); |
Zoe Liu | 7b1ec7a | 2017-05-24 22:28:24 -0700 | [diff] [blame] | 6947 | #else // !CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6948 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6949 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6950 | #endif // CONFIG_EXT_REFS |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6951 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6952 | } else { |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6953 | #if CONFIG_EXT_COMP_REFS |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6954 | int ref0, ref1; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6955 | for (ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) { |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6956 | for (ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) |
| 6957 | ref_costs_comp[ref0][ref1] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6958 | } |
| 6959 | ref_costs_comp[LAST_FRAME][LAST2_FRAME] = 512; |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6960 | ref_costs_comp[LAST_FRAME][LAST3_FRAME] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6961 | ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = 512; |
| 6962 | ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = 512; |
| 6963 | #else // !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6964 | ref_costs_comp[LAST_FRAME] = 512; |
| 6965 | #if CONFIG_EXT_REFS |
| 6966 | ref_costs_comp[LAST2_FRAME] = 512; |
| 6967 | ref_costs_comp[LAST3_FRAME] = 512; |
| 6968 | ref_costs_comp[BWDREF_FRAME] = 512; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6969 | ref_costs_comp[ALTREF2_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6970 | ref_costs_comp[ALTREF_FRAME] = 512; |
| 6971 | #endif // CONFIG_EXT_REFS |
| 6972 | ref_costs_comp[GOLDEN_FRAME] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6973 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6974 | } |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, |
| 6979 | int mode_index, |
| 6980 | int64_t comp_pred_diff[REFERENCE_MODES], |
| 6981 | int skippable) { |
| 6982 | MACROBLOCKD *const xd = &x->e_mbd; |
| 6983 | |
| 6984 | // Take a snapshot of the coding context so it can be |
| 6985 | // restored if we decide to encode this way |
| 6986 | ctx->skip = x->skip; |
| 6987 | ctx->skippable = skippable; |
| 6988 | ctx->best_mode_index = mode_index; |
| 6989 | ctx->mic = *xd->mi[0]; |
| 6990 | ctx->mbmi_ext = *x->mbmi_ext; |
| 6991 | ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE]; |
| 6992 | ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE]; |
| 6993 | ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT]; |
| 6994 | } |
| 6995 | |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 6996 | static void setup_buffer_inter( |
| 6997 | const AV1_COMP *const cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame, |
| 6998 | BLOCK_SIZE block_size, int mi_row, int mi_col, |
| 6999 | int_mv frame_nearest_mv[TOTAL_REFS_PER_FRAME], |
| 7000 | int_mv frame_near_mv[TOTAL_REFS_PER_FRAME], |
| 7001 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7002 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7003 | const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame); |
| 7004 | MACROBLOCKD *const xd = &x->e_mbd; |
| 7005 | MODE_INFO *const mi = xd->mi[0]; |
| 7006 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 7007 | const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf; |
| 7008 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 7009 | |
| 7010 | assert(yv12 != NULL); |
| 7011 | |
| 7012 | // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this |
| 7013 | // use the UV scaling factors. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7014 | 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] | 7015 | |
| 7016 | // Gets an initial list of candidate vectors from neighbours and orders them |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 7017 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 7018 | mbmi_ext->ref_mv_stack[ref_frame], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7019 | #if CONFIG_EXT_INTER |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 7020 | mbmi_ext->compound_mode_context, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7021 | #endif // CONFIG_EXT_INTER |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 7022 | candidates, mi_row, mi_col, NULL, NULL, |
| 7023 | mbmi_ext->mode_context); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7024 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7025 | // Candidate refinement carried out at encoder and decoder |
| 7026 | #if CONFIG_AMVR |
| 7027 | av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates, |
| 7028 | &frame_nearest_mv[ref_frame], &frame_near_mv[ref_frame], |
| 7029 | cm->cur_frame_mv_precision_level); |
| 7030 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7031 | av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates, |
| 7032 | &frame_nearest_mv[ref_frame], |
| 7033 | &frame_near_mv[ref_frame]); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7034 | #endif |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 7035 | // Further refinement that is encode side only to test the top few candidates |
| 7036 | // in full and choose the best as the centre point for subsequent searches. |
| 7037 | // The current implementation doesn't support scaling. |
| 7038 | #if CONFIG_CB4X4 |
| 7039 | av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame, |
| 7040 | block_size); |
| 7041 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7042 | if (!av1_is_scaled(sf) && block_size >= BLOCK_8X8) |
| 7043 | av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame, |
| 7044 | block_size); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 7045 | #endif // CONFIG_CB4X4 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7046 | } |
| 7047 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 7048 | static void single_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 7049 | BLOCK_SIZE bsize, int mi_row, int mi_col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7050 | #if CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7051 | int ref_idx, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7052 | #endif // CONFIG_EXT_INTER |
| 7053 | int *rate_mv) { |
| 7054 | MACROBLOCKD *xd = &x->e_mbd; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7055 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7056 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 7057 | struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } }; |
| 7058 | int bestsme = INT_MAX; |
| 7059 | int step_param; |
| 7060 | int sadpb = x->sadperbit16; |
| 7061 | MV mvp_full; |
| 7062 | #if CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7063 | #if CONFIG_COMPOUND_SINGLEREF |
| 7064 | int ref = |
| 7065 | has_second_ref(mbmi) ? mbmi->ref_frame[ref_idx] : mbmi->ref_frame[0]; |
| 7066 | #else // !CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7067 | int ref = mbmi->ref_frame[ref_idx]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7068 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7069 | #else // !CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7070 | int ref = mbmi->ref_frame[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7071 | int ref_idx = 0; |
| 7072 | #endif // CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7073 | MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7074 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7075 | MvLimits tmp_mv_limits = x->mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7076 | int cost_list[5]; |
| 7077 | |
| 7078 | const YV12_BUFFER_CONFIG *scaled_ref_frame = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7079 | av1_get_scaled_ref_frame(cpi, ref); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7080 | |
| 7081 | MV pred_mv[3]; |
| 7082 | pred_mv[0] = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 7083 | pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv; |
| 7084 | pred_mv[2] = x->pred_mv[ref]; |
| 7085 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7086 | if (scaled_ref_frame) { |
| 7087 | int i; |
| 7088 | // Swap out the reference frame for a version that's been scaled to |
| 7089 | // match the resolution of the current frame, allowing the existing |
| 7090 | // motion search code to be used without additional modifications. |
| 7091 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7092 | backup_yv12[i] = xd->plane[i].pre[ref_idx]; |
| 7093 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7094 | 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] | 7095 | } |
| 7096 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7097 | av1_set_mv_search_range(&x->mv_limits, &ref_mv); |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 7098 | |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 7099 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 7100 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7101 | // 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] | 7102 | // 0 here is maximum length first step. 1 is AOMMAX >> 1 etc. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7103 | if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) { |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 7104 | // 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] | 7105 | // max mv magnitude and that based on the best ref mvs of the current |
| 7106 | // block for the given reference. |
| 7107 | step_param = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7108 | (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] | 7109 | 2; |
| 7110 | } else { |
| 7111 | step_param = cpi->mv_step_param; |
| 7112 | } |
| 7113 | |
| 7114 | if (cpi->sf.adaptive_motion_search && bsize < cm->sb_size) { |
| 7115 | int boffset = |
| 7116 | 2 * (b_width_log2_lookup[cm->sb_size] - |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7117 | AOMMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize])); |
| 7118 | step_param = AOMMAX(step_param, boffset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7119 | } |
| 7120 | |
| 7121 | if (cpi->sf.adaptive_motion_search) { |
| 7122 | int bwl = b_width_log2_lookup[bsize]; |
| 7123 | int bhl = b_height_log2_lookup[bsize]; |
| 7124 | int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4); |
| 7125 | |
| 7126 | if (tlevel < 5) step_param += 2; |
| 7127 | |
| 7128 | // prev_mv_sad is not setup for dynamically scaled frames. |
Debargha Mukherjee | 7166f22 | 2017-09-05 21:32:42 -0700 | [diff] [blame] | 7129 | if (cpi->oxcf.resize_mode != RESIZE_RANDOM) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7130 | int i; |
| 7131 | for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) { |
| 7132 | if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) { |
| 7133 | x->pred_mv[ref].row = 0; |
| 7134 | x->pred_mv[ref].col = 0; |
| 7135 | x->best_mv.as_int = INVALID_MV; |
| 7136 | |
| 7137 | if (scaled_ref_frame) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 7138 | int j; |
| 7139 | for (j = 0; j < MAX_MB_PLANE; ++j) |
| 7140 | xd->plane[j].pre[ref_idx] = backup_yv12[j]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7141 | } |
| 7142 | return; |
| 7143 | } |
| 7144 | } |
| 7145 | } |
| 7146 | } |
| 7147 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7148 | av1_set_mv_search_range(&x->mv_limits, &ref_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7149 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7150 | #if CONFIG_MOTION_VAR |
| 7151 | if (mbmi->motion_mode != SIMPLE_TRANSLATION) |
| 7152 | mvp_full = mbmi->mv[0].as_mv; |
| 7153 | else |
| 7154 | #endif // CONFIG_MOTION_VAR |
| 7155 | mvp_full = pred_mv[x->mv_best_ref_index[ref]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7156 | |
| 7157 | mvp_full.col >>= 3; |
| 7158 | mvp_full.row >>= 3; |
| 7159 | |
| 7160 | x->best_mv.as_int = x->second_best_mv.as_int = INVALID_MV; |
| 7161 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7162 | #if CONFIG_MOTION_VAR |
| 7163 | switch (mbmi->motion_mode) { |
| 7164 | case SIMPLE_TRANSLATION: |
| 7165 | #endif // CONFIG_MOTION_VAR |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 7166 | #if CONFIG_HASH_ME |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7167 | bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, |
| 7168 | sadpb, cond_cost_list(cpi, cost_list), |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 7169 | &ref_mv, INT_MAX, 1, (MI_SIZE * mi_col), |
RogerZhou | d15e7c1 | 2017-09-26 08:49:28 -0700 | [diff] [blame] | 7170 | (MI_SIZE * mi_row), 0); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 7171 | #else |
| 7172 | bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb, |
| 7173 | cond_cost_list(cpi, cost_list), &ref_mv, |
| 7174 | INT_MAX, 1); |
| 7175 | #endif |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7176 | #if CONFIG_MOTION_VAR |
| 7177 | break; |
| 7178 | case OBMC_CAUSAL: |
| 7179 | bestsme = av1_obmc_full_pixel_diamond( |
| 7180 | cpi, x, &mvp_full, step_param, sadpb, |
| 7181 | MAX_MVSEARCH_STEPS - 1 - step_param, 1, &cpi->fn_ptr[bsize], &ref_mv, |
| 7182 | &(x->best_mv.as_mv), 0); |
| 7183 | break; |
James Zern | 8889673 | 2017-06-23 15:55:09 -0700 | [diff] [blame] | 7184 | default: assert(0 && "Invalid motion mode!\n"); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7185 | } |
| 7186 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7187 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7188 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7189 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7190 | #if CONFIG_AMVR |
| 7191 | if (cpi->common.cur_frame_mv_precision_level) { |
| 7192 | x->best_mv.as_mv.row *= 8; |
| 7193 | x->best_mv.as_mv.col *= 8; |
| 7194 | } |
| 7195 | if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) { |
| 7196 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7197 | if (bestsme < INT_MAX) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7198 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7199 | int dis; /* TODO: use dis in distortion calculation later. */ |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7200 | #if CONFIG_MOTION_VAR |
| 7201 | switch (mbmi->motion_mode) { |
| 7202 | case SIMPLE_TRANSLATION: |
| 7203 | #endif // CONFIG_MOTION_VAR |
| 7204 | if (cpi->sf.use_upsampled_references) { |
| 7205 | int best_mv_var; |
| 7206 | const int try_second = x->second_best_mv.as_int != INVALID_MV && |
| 7207 | x->second_best_mv.as_int != x->best_mv.as_int; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7208 | const int pw = block_size_wide[bsize]; |
| 7209 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7210 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7211 | best_mv_var = cpi->find_fractional_mv_step( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7212 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 7213 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 7214 | cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list), |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7215 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, |
| 7216 | #if CONFIG_EXT_INTER |
| 7217 | NULL, 0, 0, |
| 7218 | #endif |
| 7219 | pw, ph, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7220 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7221 | if (try_second) { |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7222 | const int minc = |
| 7223 | AOMMAX(x->mv_limits.col_min * 8, ref_mv.col - MV_MAX); |
| 7224 | const int maxc = |
| 7225 | AOMMIN(x->mv_limits.col_max * 8, ref_mv.col + MV_MAX); |
| 7226 | const int minr = |
| 7227 | AOMMAX(x->mv_limits.row_min * 8, ref_mv.row - MV_MAX); |
| 7228 | const int maxr = |
| 7229 | AOMMIN(x->mv_limits.row_max * 8, ref_mv.row + MV_MAX); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7230 | int this_var; |
| 7231 | MV best_mv = x->best_mv.as_mv; |
| 7232 | |
| 7233 | x->best_mv = x->second_best_mv; |
| 7234 | if (x->best_mv.as_mv.row * 8 <= maxr && |
| 7235 | x->best_mv.as_mv.row * 8 >= minr && |
| 7236 | x->best_mv.as_mv.col * 8 <= maxc && |
| 7237 | x->best_mv.as_mv.col * 8 >= minc) { |
| 7238 | this_var = cpi->find_fractional_mv_step( |
| 7239 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 7240 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 7241 | cpi->sf.mv.subpel_iters_per_step, |
| 7242 | cond_cost_list(cpi, cost_list), x->nmvjointcost, x->mvcost, |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7243 | &dis, &x->pred_sse[ref], NULL, |
| 7244 | #if CONFIG_EXT_INTER |
| 7245 | NULL, 0, 0, |
| 7246 | #endif |
| 7247 | pw, ph, 1); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7248 | if (this_var < best_mv_var) best_mv = x->best_mv.as_mv; |
| 7249 | x->best_mv.as_mv = best_mv; |
| 7250 | } |
| 7251 | } |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7252 | } else { |
| 7253 | cpi->find_fractional_mv_step( |
| 7254 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 7255 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 7256 | cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list), |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7257 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, |
| 7258 | #if CONFIG_EXT_INTER |
| 7259 | NULL, 0, 0, |
| 7260 | #endif |
| 7261 | 0, 0, 0); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7262 | } |
| 7263 | #if CONFIG_MOTION_VAR |
| 7264 | break; |
| 7265 | case OBMC_CAUSAL: |
| 7266 | av1_find_best_obmc_sub_pixel_tree_up( |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 7267 | x, &x->best_mv.as_mv, &ref_mv, cm->allow_high_precision_mv, |
| 7268 | x->errorperbit, &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 7269 | cpi->sf.mv.subpel_iters_per_step, x->nmvjointcost, x->mvcost, &dis, |
| 7270 | &x->pred_sse[ref], 0, cpi->sf.use_upsampled_references); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7271 | break; |
James Zern | 8889673 | 2017-06-23 15:55:09 -0700 | [diff] [blame] | 7272 | default: assert(0 && "Invalid motion mode!\n"); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7273 | } |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7274 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7275 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7276 | *rate_mv = av1_mv_bit_cost(&x->best_mv.as_mv, &ref_mv, x->nmvjointcost, |
| 7277 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7278 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7279 | #if CONFIG_MOTION_VAR |
| 7280 | if (cpi->sf.adaptive_motion_search && mbmi->motion_mode == SIMPLE_TRANSLATION) |
| 7281 | #else |
| 7282 | if (cpi->sf.adaptive_motion_search) |
| 7283 | #endif // CONFIG_MOTION_VAR |
| 7284 | x->pred_mv[ref] = x->best_mv.as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7285 | |
| 7286 | if (scaled_ref_frame) { |
| 7287 | int i; |
| 7288 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7289 | xd->plane[i].pre[ref_idx] = backup_yv12[i]; |
| 7290 | } |
| 7291 | } |
| 7292 | |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 7293 | static INLINE void restore_dst_buf(MACROBLOCKD *xd, BUFFER_SET dst) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7294 | int i; |
| 7295 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 7296 | xd->plane[i].dst.buf = dst.plane[i]; |
| 7297 | xd->plane[i].dst.stride = dst.stride[i]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7298 | } |
| 7299 | } |
| 7300 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7301 | #if CONFIG_EXT_INTER |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7302 | static void build_second_inter_pred(const AV1_COMP *cpi, MACROBLOCK *x, |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7303 | BLOCK_SIZE bsize, const MV *other_mv, |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7304 | int mi_row, int mi_col, const int block, |
| 7305 | int ref_idx, uint8_t *second_pred) { |
| 7306 | const AV1_COMMON *const cm = &cpi->common; |
| 7307 | const int pw = block_size_wide[bsize]; |
| 7308 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7309 | MACROBLOCKD *xd = &x->e_mbd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7310 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7311 | #if CONFIG_COMPOUND_SINGLEREF |
| 7312 | const int other_ref = |
| 7313 | has_second_ref(mbmi) ? mbmi->ref_frame[!ref_idx] : mbmi->ref_frame[0]; |
| 7314 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7315 | const int other_ref = mbmi->ref_frame[!ref_idx]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7316 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7317 | struct scale_factors sf; |
| 7318 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7319 | struct macroblockd_plane *const pd = &xd->plane[0]; |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 7320 | // 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] | 7321 | const int ic = block & 1; |
| 7322 | const int ir = (block - ic) >> 1; |
| 7323 | const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x) + 4 * ic; |
| 7324 | const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y) + 4 * ir; |
| 7325 | #if CONFIG_GLOBAL_MOTION |
| 7326 | WarpedMotionParams *const wm = &xd->global_motion[other_ref]; |
| 7327 | int is_global = is_global_mv_block(xd->mi[0], block, wm->wmtype); |
| 7328 | #endif // CONFIG_GLOBAL_MOTION |
| 7329 | #else |
| 7330 | (void)block; |
| 7331 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7332 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7333 | // This function should only ever be called for compound modes |
| 7334 | #if CONFIG_COMPOUND_SINGLEREF |
| 7335 | assert(has_second_ref(mbmi) || is_inter_singleref_comp_mode(mbmi->mode)); |
| 7336 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7337 | assert(has_second_ref(mbmi)); |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7338 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7339 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7340 | struct buf_2d backup_yv12[MAX_MB_PLANE]; |
| 7341 | const YV12_BUFFER_CONFIG *const scaled_ref_frame = |
| 7342 | av1_get_scaled_ref_frame(cpi, other_ref); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7343 | |
| 7344 | if (scaled_ref_frame) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7345 | int i; |
| 7346 | // Swap out the reference frame for a version that's been scaled to |
| 7347 | // match the resolution of the current frame, allowing the existing |
| 7348 | // motion search code to be used without additional modifications. |
| 7349 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7350 | backup_yv12[i] = xd->plane[i].pre[!ref_idx]; |
| 7351 | av1_setup_pre_planes(xd, !ref_idx, scaled_ref_frame, mi_row, mi_col, NULL); |
| 7352 | } |
| 7353 | |
| 7354 | // Since we have scaled the reference frames to match the size of the current |
| 7355 | // frame we must use a unit scaling factor during mode selection. |
| 7356 | #if CONFIG_HIGHBITDEPTH |
| 7357 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 7358 | cm->height, cm->use_highbitdepth); |
| 7359 | #else |
| 7360 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 7361 | cm->height); |
| 7362 | #endif // CONFIG_HIGHBITDEPTH |
| 7363 | |
| 7364 | struct buf_2d ref_yv12; |
| 7365 | |
| 7366 | const int plane = 0; |
David Barker | e64d51a | 2017-06-09 14:52:42 +0100 | [diff] [blame] | 7367 | ConvolveParams conv_params = get_conv_params(!ref_idx, 0, plane); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7368 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7369 | WarpTypesAllowed warp_types; |
| 7370 | #if CONFIG_GLOBAL_MOTION |
| 7371 | warp_types.global_warp_allowed = is_global; |
| 7372 | #endif // CONFIG_GLOBAL_MOTION |
| 7373 | #if CONFIG_WARPED_MOTION |
| 7374 | warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL; |
| 7375 | #endif // CONFIG_WARPED_MOTION |
| 7376 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7377 | |
| 7378 | // Initialized here because of compiler problem in Visual Studio. |
| 7379 | ref_yv12 = xd->plane[plane].pre[!ref_idx]; |
| 7380 | |
| 7381 | // Get the prediction block from the 'other' reference frame. |
| 7382 | #if CONFIG_HIGHBITDEPTH |
| 7383 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 7384 | av1_highbd_build_inter_predictor( |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7385 | ref_yv12.buf, ref_yv12.stride, second_pred, pw, other_mv, &sf, pw, ph, |
David Barker | e64d51a | 2017-06-09 14:52:42 +0100 | [diff] [blame] | 7386 | 0, mbmi->interp_filter, |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7387 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7388 | &warp_types, p_col, p_row, |
| 7389 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7390 | plane, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
| 7391 | } else { |
| 7392 | #endif // CONFIG_HIGHBITDEPTH |
| 7393 | av1_build_inter_predictor( |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7394 | ref_yv12.buf, ref_yv12.stride, second_pred, pw, other_mv, &sf, pw, ph, |
David Barker | e64d51a | 2017-06-09 14:52:42 +0100 | [diff] [blame] | 7395 | &conv_params, mbmi->interp_filter, |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7396 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7397 | &warp_types, p_col, p_row, plane, !ref_idx, |
| 7398 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7399 | MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
| 7400 | #if CONFIG_HIGHBITDEPTH |
| 7401 | } |
| 7402 | #endif // CONFIG_HIGHBITDEPTH |
| 7403 | |
| 7404 | if (scaled_ref_frame) { |
| 7405 | // Restore the prediction frame pointers to their unscaled versions. |
| 7406 | int i; |
| 7407 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7408 | xd->plane[i].pre[!ref_idx] = backup_yv12[i]; |
| 7409 | } |
| 7410 | } |
| 7411 | |
| 7412 | // Search for the best mv for one component of a compound, |
| 7413 | // given that the other component is fixed. |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 7414 | static void compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x, |
| 7415 | BLOCK_SIZE bsize, MV *this_mv, |
| 7416 | int mi_row, int mi_col, |
| 7417 | const uint8_t *second_pred, |
| 7418 | const uint8_t *mask, int mask_stride, |
| 7419 | int *rate_mv, int ref_idx) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7420 | const int pw = block_size_wide[bsize]; |
| 7421 | const int ph = block_size_high[bsize]; |
| 7422 | MACROBLOCKD *xd = &x->e_mbd; |
| 7423 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7424 | #if CONFIG_COMPOUND_SINGLEREF |
| 7425 | const int ref = |
| 7426 | has_second_ref(mbmi) ? mbmi->ref_frame[ref_idx] : mbmi->ref_frame[0]; |
| 7427 | #else |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7428 | const int ref = mbmi->ref_frame[ref_idx]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7429 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7430 | int_mv ref_mv = x->mbmi_ext->ref_mvs[ref][0]; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7431 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 7432 | |
| 7433 | struct buf_2d backup_yv12[MAX_MB_PLANE]; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7434 | const YV12_BUFFER_CONFIG *const scaled_ref_frame = |
| 7435 | av1_get_scaled_ref_frame(cpi, ref); |
| 7436 | |
Ryan Lei | 1d1df18 | 2017-06-15 11:38:59 -0700 | [diff] [blame] | 7437 | // Check that this is either an interinter or an interintra block |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7438 | #if CONFIG_COMPOUND_SINGLEREF |
Ryan Lei | 1d1df18 | 2017-06-15 11:38:59 -0700 | [diff] [blame] | 7439 | assert(has_second_ref(mbmi) || |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7440 | // or a single ref comp pred mode |
| 7441 | is_inter_singleref_comp_mode(mbmi->mode) || |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7442 | (ref_idx == 0 && mbmi->ref_frame[1] == INTRA_FRAME)); |
Ryan Lei | 1d1df18 | 2017-06-15 11:38:59 -0700 | [diff] [blame] | 7443 | #else |
| 7444 | assert(has_second_ref(mbmi) || |
| 7445 | (ref_idx == 0 && mbmi->ref_frame[1] == INTRA_FRAME)); |
| 7446 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7447 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7448 | if (scaled_ref_frame) { |
| 7449 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7450 | // Swap out the reference frame for a version that's been scaled to |
| 7451 | // match the resolution of the current frame, allowing the existing |
| 7452 | // motion search code to be used without additional modifications. |
| 7453 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7454 | backup_yv12[i] = xd->plane[i].pre[ref_idx]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7455 | 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] | 7456 | } |
| 7457 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7458 | struct buf_2d orig_yv12; |
| 7459 | int bestsme = INT_MAX; |
| 7460 | int sadpb = x->sadperbit16; |
| 7461 | MV *const best_mv = &x->best_mv.as_mv; |
| 7462 | int search_range = 3; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7463 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7464 | MvLimits tmp_mv_limits = x->mv_limits; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7465 | |
| 7466 | // Initialized here because of compiler problem in Visual Studio. |
| 7467 | if (ref_idx) { |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7468 | orig_yv12 = pd->pre[0]; |
| 7469 | pd->pre[0] = pd->pre[ref_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7470 | } |
| 7471 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7472 | // Do compound motion search on the current reference frame. |
| 7473 | av1_set_mv_search_range(&x->mv_limits, &ref_mv.as_mv); |
| 7474 | |
| 7475 | // Use the mv result from the single mode as mv predictor. |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7476 | *best_mv = *this_mv; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7477 | |
| 7478 | best_mv->col >>= 3; |
| 7479 | best_mv->row >>= 3; |
| 7480 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7481 | #if CONFIG_COMPOUND_SINGLEREF |
| 7482 | if (!has_second_ref(mbmi)) |
| 7483 | av1_set_mvcost(x, ref, 0, mbmi->ref_mv_idx); |
| 7484 | else |
| 7485 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7486 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7487 | |
| 7488 | // Small-range full-pixel motion search. |
| 7489 | bestsme = av1_refining_search_8p_c(x, sadpb, search_range, |
| 7490 | &cpi->fn_ptr[bsize], mask, mask_stride, |
| 7491 | ref_idx, &ref_mv.as_mv, second_pred); |
| 7492 | if (bestsme < INT_MAX) { |
| 7493 | if (mask) |
| 7494 | bestsme = |
| 7495 | av1_get_mvpred_mask_var(x, best_mv, &ref_mv.as_mv, second_pred, mask, |
| 7496 | mask_stride, ref_idx, &cpi->fn_ptr[bsize], 1); |
| 7497 | else |
| 7498 | bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv.as_mv, second_pred, |
| 7499 | &cpi->fn_ptr[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7500 | } |
| 7501 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7502 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7503 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7504 | #if CONFIG_AMVR |
| 7505 | if (cpi->common.cur_frame_mv_precision_level) { |
| 7506 | x->best_mv.as_mv.row *= 8; |
| 7507 | x->best_mv.as_mv.col *= 8; |
| 7508 | } |
| 7509 | if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) { |
| 7510 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7511 | if (bestsme < INT_MAX) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7512 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7513 | int dis; /* TODO: use dis in distortion calculation later. */ |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7514 | unsigned int sse; |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 7515 | bestsme = cpi->find_fractional_mv_step( |
| 7516 | x, &ref_mv.as_mv, cpi->common.allow_high_precision_mv, x->errorperbit, |
| 7517 | &cpi->fn_ptr[bsize], 0, cpi->sf.mv.subpel_iters_per_step, NULL, |
| 7518 | x->nmvjointcost, x->mvcost, &dis, &sse, second_pred, mask, mask_stride, |
| 7519 | ref_idx, pw, ph, cpi->sf.use_upsampled_references); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7520 | } |
| 7521 | |
| 7522 | // Restore the pointer to the first (possibly scaled) prediction buffer. |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7523 | if (ref_idx) pd->pre[0] = orig_yv12; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7524 | |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 7525 | if (bestsme < INT_MAX) *this_mv = *best_mv; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7526 | |
| 7527 | *rate_mv = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7528 | |
| 7529 | if (scaled_ref_frame) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7530 | // Restore the prediction frame pointers to their unscaled versions. |
| 7531 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7532 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7533 | xd->plane[i].pre[ref_idx] = backup_yv12[i]; |
| 7534 | } |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7535 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7536 | #if CONFIG_COMPOUND_SINGLEREF |
| 7537 | if (!has_second_ref(mbmi)) |
| 7538 | av1_set_mvcost(x, ref, 0, mbmi->ref_mv_idx); |
| 7539 | else |
| 7540 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7541 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7542 | *rate_mv += av1_mv_bit_cost(this_mv, &ref_mv.as_mv, x->nmvjointcost, |
| 7543 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7544 | } |
| 7545 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7546 | // Wrapper for compound_single_motion_search, for the common case |
| 7547 | // where the second prediction is also an inter mode. |
| 7548 | static void compound_single_motion_search_interinter( |
| 7549 | const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int_mv *frame_mv, |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7550 | #if CONFIG_COMPOUND_SINGLEREF |
| 7551 | int_mv *frame_comp_mv, |
| 7552 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7553 | int mi_row, int mi_col, const uint8_t *mask, int mask_stride, int *rate_mv, |
| 7554 | const int block, int ref_idx) { |
| 7555 | MACROBLOCKD *xd = &x->e_mbd; |
| 7556 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 7557 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7558 | // This function should only ever be called for compound modes |
| 7559 | #if CONFIG_COMPOUND_SINGLEREF |
| 7560 | int is_singleref_comp_mode = |
| 7561 | !has_second_ref(mbmi) && is_inter_singleref_comp_mode(mbmi->mode); |
| 7562 | assert(has_second_ref(mbmi) || is_singleref_comp_mode); |
| 7563 | if (is_singleref_comp_mode && ref_idx) assert(frame_comp_mv); |
| 7564 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7565 | assert(has_second_ref(mbmi)); |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7566 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7567 | |
| 7568 | // Prediction buffer from second frame. |
| 7569 | #if CONFIG_HIGHBITDEPTH |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7570 | DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]); |
| 7571 | uint8_t *second_pred; |
| 7572 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 7573 | second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16); |
| 7574 | else |
| 7575 | second_pred = (uint8_t *)second_pred_alloc_16; |
| 7576 | #else |
| 7577 | DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]); |
| 7578 | #endif // CONFIG_HIGHBITDEPTH |
| 7579 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7580 | #if CONFIG_COMPOUND_SINGLEREF |
| 7581 | MV *this_mv = has_second_ref(mbmi) |
| 7582 | ? &frame_mv[mbmi->ref_frame[ref_idx]].as_mv |
| 7583 | : (ref_idx ? &frame_comp_mv[mbmi->ref_frame[0]].as_mv |
| 7584 | : &frame_mv[mbmi->ref_frame[0]].as_mv); |
| 7585 | const MV *other_mv = |
| 7586 | has_second_ref(mbmi) |
| 7587 | ? &frame_mv[mbmi->ref_frame[!ref_idx]].as_mv |
| 7588 | : (ref_idx ? &frame_mv[mbmi->ref_frame[0]].as_mv |
| 7589 | : &frame_comp_mv[mbmi->ref_frame[0]].as_mv); |
| 7590 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7591 | MV *this_mv = &frame_mv[mbmi->ref_frame[ref_idx]].as_mv; |
| 7592 | const MV *other_mv = &frame_mv[mbmi->ref_frame[!ref_idx]].as_mv; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7593 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7594 | |
| 7595 | 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] | 7596 | ref_idx, second_pred); |
| 7597 | |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7598 | 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] | 7599 | second_pred, mask, mask_stride, rate_mv, |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7600 | ref_idx); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7601 | } |
| 7602 | |
| 7603 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7604 | static void do_masked_motion_search_indexed( |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7605 | 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] | 7606 | const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE bsize, |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7607 | 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] | 7608 | // NOTE: which values: 0 - 0 only, 1 - 1 only, 2 - both |
| 7609 | MACROBLOCKD *xd = &x->e_mbd; |
| 7610 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 7611 | BLOCK_SIZE sb_type = mbmi->sb_type; |
| 7612 | const uint8_t *mask; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7613 | const int mask_stride = block_size_wide[bsize]; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7614 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7615 | mask = av1_get_compound_type_mask(comp_data, sb_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7616 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7617 | int_mv frame_mv[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7618 | #if CONFIG_COMPOUND_SINGLEREF |
| 7619 | int_mv frame_comp_mv[TOTAL_REFS_PER_FRAME]; |
| 7620 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7621 | MV_REFERENCE_FRAME rf[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] }; |
| 7622 | assert(bsize >= BLOCK_8X8 || CONFIG_CB4X4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7623 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7624 | frame_mv[rf[0]].as_int = cur_mv[0].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7625 | #if CONFIG_COMPOUND_SINGLEREF |
| 7626 | if (!has_second_ref(mbmi)) |
| 7627 | frame_comp_mv[rf[0]].as_int = cur_mv[1].as_int; |
| 7628 | else |
| 7629 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7630 | frame_mv[rf[1]].as_int = cur_mv[1].as_int; |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7631 | if (which == 0 || which == 1) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7632 | compound_single_motion_search_interinter( |
| 7633 | cpi, x, bsize, frame_mv, |
| 7634 | #if CONFIG_COMPOUND_SINGLEREF |
| 7635 | has_second_ref(mbmi) ? NULL : frame_comp_mv, |
| 7636 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7637 | mi_row, mi_col, mask, mask_stride, rate_mv, 0, which); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7638 | } else if (which == 2) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7639 | joint_motion_search(cpi, x, bsize, frame_mv, |
| 7640 | #if CONFIG_COMPOUND_SINGLEREF |
| 7641 | has_second_ref(mbmi) ? NULL : frame_comp_mv, |
| 7642 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7643 | mi_row, mi_col, NULL, mask, mask_stride, rate_mv, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7644 | } |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7645 | tmp_mv[0].as_int = frame_mv[rf[0]].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7646 | #if CONFIG_COMPOUND_SINGLEREF |
| 7647 | if (!has_second_ref(mbmi)) |
| 7648 | tmp_mv[1].as_int = frame_comp_mv[rf[0]].as_int; |
| 7649 | else // comp ref |
| 7650 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7651 | tmp_mv[1].as_int = frame_mv[rf[1]].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7652 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7653 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7654 | #endif // CONFIG_EXT_INTER |
| 7655 | |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 7656 | // 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] | 7657 | // vector. Where there is a subtle motion field and especially where there is |
| 7658 | // low spatial complexity then it can be hard to cover the cost of a new motion |
| 7659 | // vector in a single block, even if that motion vector reduces distortion. |
| 7660 | // However, once established that vector may be usable through the nearest and |
| 7661 | // near mv modes to reduce distortion in subsequent blocks and also improve |
| 7662 | // visual quality. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 7663 | 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] | 7664 | int_mv this_mv, |
| 7665 | int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], |
| 7666 | int ref_frame) { |
| 7667 | return (!cpi->rc.is_src_frame_alt_ref && (this_mode == NEWMV) && |
| 7668 | (this_mv.as_int != 0) && |
| 7669 | ((mode_mv[NEARESTMV][ref_frame].as_int == 0) || |
| 7670 | (mode_mv[NEARESTMV][ref_frame].as_int == INVALID_MV)) && |
| 7671 | ((mode_mv[NEARMV][ref_frame].as_int == 0) || |
| 7672 | (mode_mv[NEARMV][ref_frame].as_int == INVALID_MV))); |
| 7673 | } |
| 7674 | |
Yaowu Xu | 671f2bd | 2016-09-30 15:07:57 -0700 | [diff] [blame] | 7675 | #define LEFT_TOP_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3) |
| 7676 | #define RIGHT_BOTTOM_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7677 | |
| 7678 | // TODO(jingning): this mv clamping function should be block size dependent. |
| 7679 | static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) { |
| 7680 | clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN, |
| 7681 | xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN, |
| 7682 | xd->mb_to_top_edge - LEFT_TOP_MARGIN, |
| 7683 | xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN); |
| 7684 | } |
| 7685 | |
| 7686 | #if CONFIG_EXT_INTER |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7687 | #if CONFIG_WEDGE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7688 | static int estimate_wedge_sign(const AV1_COMP *cpi, const MACROBLOCK *x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7689 | const BLOCK_SIZE bsize, const uint8_t *pred0, |
| 7690 | int stride0, const uint8_t *pred1, int stride1) { |
| 7691 | const struct macroblock_plane *const p = &x->plane[0]; |
| 7692 | const uint8_t *src = p->src.buf; |
| 7693 | int src_stride = p->src.stride; |
| 7694 | const int f_index = bsize - BLOCK_8X8; |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7695 | const int bw = block_size_wide[bsize]; |
| 7696 | const int bh = block_size_high[bsize]; |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 7697 | uint32_t esq[2][4]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7698 | int64_t tl, br; |
| 7699 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7700 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7701 | if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 7702 | pred0 = CONVERT_TO_BYTEPTR(pred0); |
| 7703 | pred1 = CONVERT_TO_BYTEPTR(pred1); |
| 7704 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7705 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7706 | |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 7707 | cpi->fn_ptr[f_index].vf(src, src_stride, pred0, stride0, &esq[0][0]); |
| 7708 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred0 + bw / 2, stride0, |
| 7709 | &esq[0][1]); |
| 7710 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride, |
| 7711 | pred0 + bh / 2 * stride0, stride0, &esq[0][2]); |
| 7712 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride, |
| 7713 | pred0 + bh / 2 * stride0 + bw / 2, stride0, |
| 7714 | &esq[0][3]); |
| 7715 | cpi->fn_ptr[f_index].vf(src, src_stride, pred1, stride1, &esq[1][0]); |
| 7716 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred1 + bw / 2, stride1, |
| 7717 | &esq[1][1]); |
| 7718 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride, |
| 7719 | pred1 + bh / 2 * stride1, stride0, &esq[1][2]); |
| 7720 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride, |
| 7721 | pred1 + bh / 2 * stride1 + bw / 2, stride0, |
| 7722 | &esq[1][3]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7723 | |
| 7724 | tl = (int64_t)(esq[0][0] + esq[0][1] + esq[0][2]) - |
| 7725 | (int64_t)(esq[1][0] + esq[1][1] + esq[1][2]); |
| 7726 | br = (int64_t)(esq[1][3] + esq[1][1] + esq[1][2]) - |
| 7727 | (int64_t)(esq[0][3] + esq[0][1] + esq[0][2]); |
| 7728 | return (tl + br > 0); |
| 7729 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7730 | #endif // CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7731 | #endif // CONFIG_EXT_INTER |
| 7732 | |
| 7733 | #if !CONFIG_DUAL_FILTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 7734 | static InterpFilter predict_interp_filter( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7735 | const AV1_COMP *cpi, const MACROBLOCK *x, const BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7736 | const int mi_row, const int mi_col, |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 7737 | InterpFilter (*single_filter)[TOTAL_REFS_PER_FRAME]) { |
| 7738 | InterpFilter best_filter = SWITCHABLE; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7739 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7740 | const MACROBLOCKD *xd = &x->e_mbd; |
| 7741 | int bsl = mi_width_log2_lookup[bsize]; |
| 7742 | int pred_filter_search = |
| 7743 | cpi->sf.cb_pred_filter_search |
| 7744 | ? (((mi_row + mi_col) >> bsl) + |
| 7745 | get_chessboard_index(cm->current_video_frame)) & |
| 7746 | 0x1 |
| 7747 | : 0; |
| 7748 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 7749 | const int is_comp_pred = has_second_ref(mbmi); |
| 7750 | const int this_mode = mbmi->mode; |
| 7751 | int refs[2] = { mbmi->ref_frame[0], |
| 7752 | (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7753 | if (pred_filter_search) { |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 7754 | InterpFilter af = SWITCHABLE, lf = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7755 | if (xd->up_available) af = xd->mi[-xd->mi_stride]->mbmi.interp_filter; |
| 7756 | if (xd->left_available) lf = xd->mi[-1]->mbmi.interp_filter; |
| 7757 | |
| 7758 | #if CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7759 | if ((this_mode != NEWMV && this_mode != NEW_NEWMV) || (af == lf)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7760 | #else |
| 7761 | if ((this_mode != NEWMV) || (af == lf)) |
| 7762 | #endif // CONFIG_EXT_INTER |
| 7763 | best_filter = af; |
| 7764 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7765 | if (is_comp_pred) { |
| 7766 | if (cpi->sf.adaptive_mode_search) { |
| 7767 | #if CONFIG_EXT_INTER |
| 7768 | switch (this_mode) { |
| 7769 | case NEAREST_NEARESTMV: |
| 7770 | if (single_filter[NEARESTMV][refs[0]] == |
| 7771 | single_filter[NEARESTMV][refs[1]]) |
| 7772 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 7773 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7774 | case NEAR_NEARMV: |
| 7775 | if (single_filter[NEARMV][refs[0]] == single_filter[NEARMV][refs[1]]) |
| 7776 | best_filter = single_filter[NEARMV][refs[0]]; |
| 7777 | break; |
| 7778 | case ZERO_ZEROMV: |
| 7779 | if (single_filter[ZEROMV][refs[0]] == single_filter[ZEROMV][refs[1]]) |
| 7780 | best_filter = single_filter[ZEROMV][refs[0]]; |
| 7781 | break; |
| 7782 | case NEW_NEWMV: |
| 7783 | if (single_filter[NEWMV][refs[0]] == single_filter[NEWMV][refs[1]]) |
| 7784 | best_filter = single_filter[NEWMV][refs[0]]; |
| 7785 | break; |
| 7786 | case NEAREST_NEWMV: |
| 7787 | if (single_filter[NEARESTMV][refs[0]] == |
| 7788 | single_filter[NEWMV][refs[1]]) |
| 7789 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 7790 | break; |
| 7791 | case NEAR_NEWMV: |
| 7792 | if (single_filter[NEARMV][refs[0]] == single_filter[NEWMV][refs[1]]) |
| 7793 | best_filter = single_filter[NEARMV][refs[0]]; |
| 7794 | break; |
| 7795 | case NEW_NEARESTMV: |
| 7796 | if (single_filter[NEWMV][refs[0]] == |
| 7797 | single_filter[NEARESTMV][refs[1]]) |
| 7798 | best_filter = single_filter[NEWMV][refs[0]]; |
| 7799 | break; |
| 7800 | case NEW_NEARMV: |
| 7801 | if (single_filter[NEWMV][refs[0]] == single_filter[NEARMV][refs[1]]) |
| 7802 | best_filter = single_filter[NEWMV][refs[0]]; |
| 7803 | break; |
| 7804 | default: |
| 7805 | if (single_filter[this_mode][refs[0]] == |
| 7806 | single_filter[this_mode][refs[1]]) |
| 7807 | best_filter = single_filter[this_mode][refs[0]]; |
| 7808 | break; |
| 7809 | } |
| 7810 | #else |
| 7811 | if (single_filter[this_mode][refs[0]] == |
| 7812 | single_filter[this_mode][refs[1]]) |
| 7813 | best_filter = single_filter[this_mode][refs[0]]; |
| 7814 | #endif // CONFIG_EXT_INTER |
| 7815 | } |
| 7816 | } |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7817 | if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) { |
| 7818 | best_filter = EIGHTTAP_REGULAR; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7819 | } |
| 7820 | return best_filter; |
| 7821 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 7822 | #endif // !CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7823 | |
| 7824 | #if CONFIG_EXT_INTER |
| 7825 | // Choose the best wedge index and sign |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7826 | #if CONFIG_WEDGE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7827 | 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] | 7828 | const BLOCK_SIZE bsize, const uint8_t *const p0, |
| 7829 | const uint8_t *const p1, int *const best_wedge_sign, |
| 7830 | int *const best_wedge_index) { |
| 7831 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 7832 | const struct buf_2d *const src = &x->plane[0].src; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7833 | const int bw = block_size_wide[bsize]; |
| 7834 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7835 | const int N = bw * bh; |
| 7836 | int rate; |
| 7837 | int64_t dist; |
| 7838 | int64_t rd, best_rd = INT64_MAX; |
| 7839 | int wedge_index; |
| 7840 | int wedge_sign; |
| 7841 | int wedge_types = (1 << get_wedge_bits_lookup(bsize)); |
| 7842 | const uint8_t *mask; |
| 7843 | uint64_t sse; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7844 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7845 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 7846 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 7847 | #else |
| 7848 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7849 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7850 | |
| 7851 | DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]); |
| 7852 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 7853 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 7854 | DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]); |
| 7855 | |
| 7856 | int64_t sign_limit; |
| 7857 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7858 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7859 | if (hbd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7860 | aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7861 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7862 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7863 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7864 | 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] | 7865 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7866 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7867 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7868 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7869 | aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw); |
| 7870 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 7871 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7872 | } |
| 7873 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7874 | sign_limit = ((int64_t)aom_sum_squares_i16(r0, N) - |
| 7875 | (int64_t)aom_sum_squares_i16(r1, N)) * |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7876 | (1 << WEDGE_WEIGHT_BITS) / 2; |
| 7877 | |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7878 | if (N < 64) |
| 7879 | av1_wedge_compute_delta_squares_c(ds, r0, r1, N); |
| 7880 | else |
| 7881 | av1_wedge_compute_delta_squares(ds, r0, r1, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7882 | |
| 7883 | for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7884 | mask = av1_get_contiguous_soft_mask(wedge_index, 0, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7885 | |
| 7886 | // TODO(jingning): Make sse2 functions support N = 16 case |
| 7887 | if (N < 64) |
| 7888 | wedge_sign = av1_wedge_sign_from_residuals_c(ds, mask, N, sign_limit); |
| 7889 | else |
| 7890 | wedge_sign = av1_wedge_sign_from_residuals(ds, mask, N, sign_limit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7891 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7892 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7893 | if (N < 64) |
| 7894 | sse = av1_wedge_sse_from_residuals_c(r1, d10, mask, N); |
| 7895 | else |
| 7896 | sse = av1_wedge_sse_from_residuals(r1, d10, mask, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7897 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
| 7898 | |
| 7899 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7900 | rd = RDCOST(x->rdmult, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7901 | |
| 7902 | if (rd < best_rd) { |
| 7903 | *best_wedge_index = wedge_index; |
| 7904 | *best_wedge_sign = wedge_sign; |
| 7905 | best_rd = rd; |
| 7906 | } |
| 7907 | } |
| 7908 | |
| 7909 | return best_rd; |
| 7910 | } |
| 7911 | |
| 7912 | // Choose the best wedge index the specified sign |
| 7913 | static int64_t pick_wedge_fixed_sign( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7914 | const AV1_COMP *const cpi, const MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7915 | const BLOCK_SIZE bsize, const uint8_t *const p0, const uint8_t *const p1, |
| 7916 | const int wedge_sign, int *const best_wedge_index) { |
| 7917 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 7918 | const struct buf_2d *const src = &x->plane[0].src; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7919 | const int bw = block_size_wide[bsize]; |
| 7920 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7921 | const int N = bw * bh; |
| 7922 | int rate; |
| 7923 | int64_t dist; |
| 7924 | int64_t rd, best_rd = INT64_MAX; |
| 7925 | int wedge_index; |
| 7926 | int wedge_types = (1 << get_wedge_bits_lookup(bsize)); |
| 7927 | const uint8_t *mask; |
| 7928 | uint64_t sse; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7929 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7930 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 7931 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 7932 | #else |
| 7933 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7934 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7935 | |
| 7936 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 7937 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 7938 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7939 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7940 | if (hbd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7941 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7942 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7943 | 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] | 7944 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7945 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7946 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7947 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7948 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 7949 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7950 | } |
| 7951 | |
| 7952 | for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7953 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7954 | if (N < 64) |
| 7955 | sse = av1_wedge_sse_from_residuals_c(r1, d10, mask, N); |
| 7956 | else |
| 7957 | sse = av1_wedge_sse_from_residuals(r1, d10, mask, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7958 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
| 7959 | |
| 7960 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7961 | rd = RDCOST(x->rdmult, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7962 | |
| 7963 | if (rd < best_rd) { |
| 7964 | *best_wedge_index = wedge_index; |
| 7965 | best_rd = rd; |
| 7966 | } |
| 7967 | } |
| 7968 | |
| 7969 | return best_rd; |
| 7970 | } |
| 7971 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7972 | static int64_t pick_interinter_wedge(const AV1_COMP *const cpi, |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7973 | MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7974 | const BLOCK_SIZE bsize, |
| 7975 | const uint8_t *const p0, |
| 7976 | const uint8_t *const p1) { |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7977 | MACROBLOCKD *const xd = &x->e_mbd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7978 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7979 | const int bw = block_size_wide[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7980 | |
| 7981 | int64_t rd; |
| 7982 | int wedge_index = -1; |
| 7983 | int wedge_sign = 0; |
| 7984 | |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 7985 | assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize)); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 7986 | assert(cpi->common.allow_masked_compound); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7987 | |
| 7988 | if (cpi->sf.fast_wedge_sign_estimate) { |
| 7989 | wedge_sign = estimate_wedge_sign(cpi, x, bsize, p0, bw, p1, bw); |
| 7990 | rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, wedge_sign, &wedge_index); |
| 7991 | } else { |
| 7992 | rd = pick_wedge(cpi, x, bsize, p0, p1, &wedge_sign, &wedge_index); |
| 7993 | } |
| 7994 | |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7995 | mbmi->wedge_sign = wedge_sign; |
| 7996 | mbmi->wedge_index = wedge_index; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7997 | return rd; |
| 7998 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7999 | #endif // CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8000 | |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8001 | #if CONFIG_COMPOUND_SEGMENT |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8002 | static int64_t pick_interinter_seg(const AV1_COMP *const cpi, |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8003 | MACROBLOCK *const x, const BLOCK_SIZE bsize, |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8004 | const uint8_t *const p0, |
| 8005 | const uint8_t *const p1) { |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8006 | MACROBLOCKD *const xd = &x->e_mbd; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8007 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8008 | const struct buf_2d *const src = &x->plane[0].src; |
| 8009 | const int bw = block_size_wide[bsize]; |
| 8010 | const int bh = block_size_high[bsize]; |
| 8011 | const int N = bw * bh; |
| 8012 | int rate; |
| 8013 | uint64_t sse; |
| 8014 | int64_t dist; |
Debargha Mukherjee | c30934b | 2017-04-25 01:23:51 -0700 | [diff] [blame] | 8015 | int64_t rd0; |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 8016 | SEG_MASK_TYPE cur_mask_type; |
| 8017 | int64_t best_rd = INT64_MAX; |
| 8018 | SEG_MASK_TYPE best_mask_type = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8019 | #if CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8020 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 8021 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 8022 | #else |
| 8023 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8024 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8025 | DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]); |
| 8026 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 8027 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 8028 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8029 | #if CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8030 | if (hbd) { |
| 8031 | aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride, |
| 8032 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 8033 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
| 8034 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
| 8035 | aom_highbd_subtract_block(bh, bw, d10, bw, CONVERT_TO_BYTEPTR(p1), bw, |
| 8036 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 8037 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8038 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8039 | { |
| 8040 | aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw); |
| 8041 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 8042 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
| 8043 | } |
| 8044 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 8045 | // try each mask type and its inverse |
| 8046 | 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] | 8047 | // build mask and inverse |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8048 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 8049 | if (hbd) |
| 8050 | build_compound_seg_mask_highbd( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8051 | xd->seg_mask, cur_mask_type, CONVERT_TO_BYTEPTR(p0), bw, |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 8052 | CONVERT_TO_BYTEPTR(p1), bw, bsize, bh, bw, xd->bd); |
| 8053 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8054 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8055 | build_compound_seg_mask(xd->seg_mask, cur_mask_type, p0, bw, p1, bw, |
| 8056 | bsize, bh, bw); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8057 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 8058 | // compute rd for mask |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8059 | sse = av1_wedge_sse_from_residuals(r1, d10, xd->seg_mask, N); |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 8060 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8061 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 8062 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8063 | rd0 = RDCOST(x->rdmult, rate, dist); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8064 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 8065 | if (rd0 < best_rd) { |
| 8066 | best_mask_type = cur_mask_type; |
| 8067 | best_rd = rd0; |
| 8068 | } |
| 8069 | } |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8070 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 8071 | // make final mask |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8072 | mbmi->mask_type = best_mask_type; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8073 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 8074 | if (hbd) |
| 8075 | build_compound_seg_mask_highbd( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8076 | xd->seg_mask, mbmi->mask_type, CONVERT_TO_BYTEPTR(p0), bw, |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 8077 | CONVERT_TO_BYTEPTR(p1), bw, bsize, bh, bw, xd->bd); |
| 8078 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8079 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8080 | build_compound_seg_mask(xd->seg_mask, mbmi->mask_type, p0, bw, p1, bw, |
| 8081 | bsize, bh, bw); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8082 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 8083 | return best_rd; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8084 | } |
| 8085 | #endif // CONFIG_COMPOUND_SEGMENT |
| 8086 | |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 8087 | #if CONFIG_WEDGE && CONFIG_INTERINTRA |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8088 | static int64_t pick_interintra_wedge(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8089 | const MACROBLOCK *const x, |
| 8090 | const BLOCK_SIZE bsize, |
| 8091 | const uint8_t *const p0, |
| 8092 | const uint8_t *const p1) { |
| 8093 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 8094 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8095 | |
| 8096 | int64_t rd; |
| 8097 | int wedge_index = -1; |
| 8098 | |
| 8099 | assert(is_interintra_wedge_used(bsize)); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 8100 | assert(cpi->common.allow_interintra_compound); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8101 | |
| 8102 | rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, 0, &wedge_index); |
| 8103 | |
| 8104 | mbmi->interintra_wedge_sign = 0; |
| 8105 | mbmi->interintra_wedge_index = wedge_index; |
| 8106 | return rd; |
| 8107 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 8108 | #endif // CONFIG_WEDGE && CONFIG_INTERINTRA |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8109 | |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 8110 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8111 | 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] | 8112 | const BLOCK_SIZE bsize, |
| 8113 | const uint8_t *const p0, |
| 8114 | const uint8_t *const p1) { |
| 8115 | const COMPOUND_TYPE compound_type = |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8116 | x->e_mbd.mi[0]->mbmi.interinter_compound_type; |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8117 | switch (compound_type) { |
| 8118 | #if CONFIG_WEDGE |
| 8119 | case COMPOUND_WEDGE: return pick_interinter_wedge(cpi, x, bsize, p0, p1); |
| 8120 | #endif // CONFIG_WEDGE |
| 8121 | #if CONFIG_COMPOUND_SEGMENT |
| 8122 | case COMPOUND_SEG: return pick_interinter_seg(cpi, x, bsize, p0, p1); |
| 8123 | #endif // CONFIG_COMPOUND_SEGMENT |
| 8124 | default: assert(0); return 0; |
| 8125 | } |
| 8126 | } |
| 8127 | |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 8128 | static int interinter_compound_motion_search( |
| 8129 | const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv, |
| 8130 | 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] | 8131 | MACROBLOCKD *const xd = &x->e_mbd; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8132 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8133 | int_mv tmp_mv[2]; |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 8134 | int tmp_rate_mv = 0; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8135 | const INTERINTER_COMPOUND_DATA compound_data = { |
| 8136 | #if CONFIG_WEDGE |
| 8137 | mbmi->wedge_index, |
| 8138 | mbmi->wedge_sign, |
| 8139 | #endif // CONFIG_WEDGE |
| 8140 | #if CONFIG_COMPOUND_SEGMENT |
| 8141 | mbmi->mask_type, |
| 8142 | xd->seg_mask, |
| 8143 | #endif // CONFIG_COMPOUND_SEGMENT |
| 8144 | mbmi->interinter_compound_type |
| 8145 | }; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8146 | #if CONFIG_COMPOUND_SINGLEREF |
| 8147 | // NOTE: Mode is needed to identify the compound mode prediction, regardless |
| 8148 | // of comp refs or single ref. |
| 8149 | mbmi->mode = this_mode; |
| 8150 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8151 | |
| 8152 | if (this_mode == NEW_NEWMV |
| 8153 | #if CONFIG_COMPOUND_SINGLEREF |
| 8154 | || this_mode == SR_NEW_NEWMV |
| 8155 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8156 | ) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 8157 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 8158 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 2); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8159 | mbmi->mv[0].as_int = tmp_mv[0].as_int; |
| 8160 | mbmi->mv[1].as_int = tmp_mv[1].as_int; |
| 8161 | } else if (this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 8162 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 8163 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 0); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8164 | mbmi->mv[0].as_int = tmp_mv[0].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8165 | } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV |
| 8166 | #if CONFIG_COMPOUND_SINGLEREF |
| 8167 | // || this_mode == SR_NEAREST_NEWMV |
| 8168 | || this_mode == SR_NEAR_NEWMV || this_mode == SR_ZERO_NEWMV |
| 8169 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8170 | ) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 8171 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 8172 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 1); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8173 | mbmi->mv[1].as_int = tmp_mv[1].as_int; |
| 8174 | } |
| 8175 | return tmp_rate_mv; |
| 8176 | } |
| 8177 | |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8178 | static int64_t build_and_cost_compound_type( |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8179 | const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv, |
| 8180 | const BLOCK_SIZE bsize, const int this_mode, int rs2, int rate_mv, |
| 8181 | BUFFER_SET *ctx, int *out_rate_mv, uint8_t **preds0, uint8_t **preds1, |
| 8182 | int *strides, int mi_row, int mi_col) { |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 8183 | const AV1_COMMON *const cm = &cpi->common; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8184 | MACROBLOCKD *xd = &x->e_mbd; |
| 8185 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8186 | int rate_sum; |
| 8187 | int64_t dist_sum; |
| 8188 | int64_t best_rd_cur = INT64_MAX; |
| 8189 | int64_t rd = INT64_MAX; |
| 8190 | int tmp_skip_txfm_sb; |
| 8191 | int64_t tmp_skip_sse_sb; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8192 | const COMPOUND_TYPE compound_type = mbmi->interinter_compound_type; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8193 | |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8194 | best_rd_cur = pick_interinter_mask(cpi, x, bsize, *preds0, *preds1); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8195 | best_rd_cur += RDCOST(x->rdmult, rs2 + rate_mv, 0); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8196 | |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 8197 | if (have_newmv_in_inter_mode(this_mode) && |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8198 | use_masked_motion_search(compound_type)) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 8199 | *out_rate_mv = interinter_compound_motion_search(cpi, x, cur_mv, bsize, |
| 8200 | this_mode, mi_row, mi_col); |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 8201 | 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] | 8202 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 8203 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8204 | rd = RDCOST(x->rdmult, rs2 + *out_rate_mv + rate_sum, dist_sum); |
Zoe Liu | 4d44f5a | 2016-12-14 17:46:19 -0800 | [diff] [blame] | 8205 | if (rd >= best_rd_cur) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8206 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 8207 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 8208 | *out_rate_mv = rate_mv; |
David Barker | 426a997 | 2017-01-27 11:03:11 +0000 | [diff] [blame] | 8209 | av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, |
| 8210 | #if CONFIG_SUPERTX |
| 8211 | 0, 0, |
| 8212 | #endif // CONFIG_SUPERTX |
| 8213 | preds0, strides, preds1, |
| 8214 | strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8215 | } |
| 8216 | av1_subtract_plane(x, bsize, 0); |
| 8217 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 8218 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 8219 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8220 | rd = RDCOST(x->rdmult, rs2 + *out_rate_mv + rate_sum, dist_sum); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8221 | best_rd_cur = rd; |
| 8222 | |
| 8223 | } else { |
David Barker | 426a997 | 2017-01-27 11:03:11 +0000 | [diff] [blame] | 8224 | av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, |
| 8225 | #if CONFIG_SUPERTX |
| 8226 | 0, 0, |
| 8227 | #endif // CONFIG_SUPERTX |
| 8228 | preds0, strides, preds1, strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8229 | av1_subtract_plane(x, bsize, 0); |
| 8230 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 8231 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 8232 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8233 | rd = RDCOST(x->rdmult, rs2 + rate_mv + rate_sum, dist_sum); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8234 | best_rd_cur = rd; |
| 8235 | } |
| 8236 | return best_rd_cur; |
| 8237 | } |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8238 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8239 | #endif // CONFIG_EXT_INTER |
| 8240 | |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8241 | typedef struct { |
| 8242 | #if CONFIG_MOTION_VAR |
| 8243 | // Inter prediction buffers and respective strides |
| 8244 | uint8_t *above_pred_buf[MAX_MB_PLANE]; |
| 8245 | int above_pred_stride[MAX_MB_PLANE]; |
| 8246 | uint8_t *left_pred_buf[MAX_MB_PLANE]; |
| 8247 | int left_pred_stride[MAX_MB_PLANE]; |
| 8248 | #endif // CONFIG_MOTION_VAR |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8249 | int_mv *single_newmv; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8250 | #if CONFIG_EXT_INTER |
| 8251 | // Pointer to array of motion vectors to use for each ref and their rates |
| 8252 | // Should point to first of 2 arrays in 2D array |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8253 | int *single_newmv_rate; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8254 | // Pointer to array of predicted rate-distortion |
| 8255 | // Should point to first of 2 arrays in 2D array |
| 8256 | int64_t (*modelled_rd)[TOTAL_REFS_PER_FRAME]; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8257 | #endif // CONFIG_EXT_INTER |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 8258 | InterpFilter single_filter[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8259 | } HandleInterModeArgs; |
| 8260 | |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8261 | static int64_t handle_newmv(const AV1_COMP *const cpi, MACROBLOCK *const x, |
| 8262 | const BLOCK_SIZE bsize, |
| 8263 | int_mv (*const mode_mv)[TOTAL_REFS_PER_FRAME], |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8264 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 8265 | int_mv (*const mode_comp_mv)[TOTAL_REFS_PER_FRAME], |
| 8266 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8267 | const int mi_row, const int mi_col, |
| 8268 | int *const rate_mv, int_mv *const single_newmv, |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 8269 | HandleInterModeArgs *const args) { |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8270 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 8271 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8272 | const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 8273 | const int is_comp_pred = has_second_ref(mbmi); |
| 8274 | const PREDICTION_MODE this_mode = mbmi->mode; |
| 8275 | #if CONFIG_EXT_INTER |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8276 | const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME); |
| 8277 | #endif // CONFIG_EXT_INTER |
| 8278 | int_mv *const frame_mv = mode_mv[this_mode]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8279 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 8280 | int_mv *const frame_comp_mv = mode_comp_mv[this_mode]; |
| 8281 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8282 | const int refs[2] = { mbmi->ref_frame[0], |
| 8283 | mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1] }; |
| 8284 | int i; |
| 8285 | |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 8286 | (void)args; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8287 | |
| 8288 | if (is_comp_pred) { |
| 8289 | #if CONFIG_EXT_INTER |
| 8290 | for (i = 0; i < 2; ++i) { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8291 | 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] | 8292 | } |
| 8293 | |
| 8294 | if (this_mode == NEW_NEWMV) { |
| 8295 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
| 8296 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
| 8297 | |
| 8298 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8299 | joint_motion_search(cpi, x, bsize, frame_mv, |
| 8300 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 8301 | NULL, // int_mv *frame_comp_mv |
| 8302 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 8303 | mi_row, mi_col, NULL, NULL, 0, rate_mv, 0); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8304 | } else { |
| 8305 | *rate_mv = 0; |
| 8306 | for (i = 0; i < 2; ++i) { |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 8307 | av1_set_mvcost(x, refs[i], i, mbmi->ref_mv_idx); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8308 | *rate_mv += av1_mv_bit_cost( |
| 8309 | &frame_mv[refs[i]].as_mv, &mbmi_ext->ref_mvs[refs[i]][0].as_mv, |
| 8310 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8311 | } |
| 8312 | } |
| 8313 | } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV) { |
| 8314 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 8315 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 8316 | frame_mv[refs[0]].as_int = |
| 8317 | mode_mv[compound_ref0_mode(this_mode)][refs[0]].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8318 | compound_single_motion_search_interinter(cpi, x, bsize, frame_mv, |
| 8319 | #if CONFIG_COMPOUND_SINGLEREF |
| 8320 | NULL, |
| 8321 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8322 | mi_row, mi_col, NULL, 0, |
| 8323 | rate_mv, 0, 1); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 8324 | } else { |
| 8325 | av1_set_mvcost(x, refs[1], 1, mbmi->ref_mv_idx); |
| 8326 | *rate_mv = av1_mv_bit_cost(&frame_mv[refs[1]].as_mv, |
| 8327 | &mbmi_ext->ref_mvs[refs[1]][0].as_mv, |
| 8328 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8329 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8330 | } else { |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 8331 | assert(this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8332 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 8333 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 8334 | frame_mv[refs[1]].as_int = |
| 8335 | mode_mv[compound_ref1_mode(this_mode)][refs[1]].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8336 | compound_single_motion_search_interinter(cpi, x, bsize, frame_mv, |
| 8337 | #if CONFIG_COMPOUND_SINGLEREF |
| 8338 | NULL, |
| 8339 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8340 | mi_row, mi_col, NULL, 0, |
| 8341 | rate_mv, 0, 0); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 8342 | } else { |
| 8343 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
| 8344 | *rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
| 8345 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 8346 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8347 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8348 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8349 | #else // !CONFIG_EXT_INTER |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8350 | // Initialize mv using single prediction mode result. |
| 8351 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
| 8352 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
| 8353 | |
| 8354 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 8355 | joint_motion_search(cpi, x, bsize, frame_mv, mi_row, mi_col, rate_mv, 0); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8356 | } else { |
| 8357 | *rate_mv = 0; |
| 8358 | for (i = 0; i < 2; ++i) { |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 8359 | av1_set_mvcost(x, refs[i], i, mbmi->ref_mv_idx); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8360 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[i]].as_mv, |
| 8361 | &mbmi_ext->ref_mvs[refs[i]][0].as_mv, |
| 8362 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8363 | } |
| 8364 | } |
| 8365 | #endif // CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8366 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 8367 | } else if (is_inter_singleref_comp_mode(this_mode)) { |
| 8368 | // Single ref comp mode |
| 8369 | const int mode0 = compound_ref0_mode(this_mode); |
| 8370 | |
| 8371 | single_newmv[refs[0]].as_int = args->single_newmv[refs[0]].as_int; |
| 8372 | frame_mv[refs[0]].as_int = (mode0 == NEWMV) |
| 8373 | ? single_newmv[refs[0]].as_int |
| 8374 | : mode_mv[mode0][refs[0]].as_int; |
| 8375 | assert(compound_ref1_mode(this_mode) == NEWMV); |
| 8376 | frame_comp_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
| 8377 | |
| 8378 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 8379 | if (this_mode == SR_NEW_NEWMV) { |
| 8380 | joint_motion_search(cpi, x, bsize, frame_mv, frame_comp_mv, mi_row, |
| 8381 | mi_col, NULL, NULL, 0, rate_mv, 0); |
| 8382 | } else { |
| 8383 | assert( // this_mode == SR_NEAREST_NEWMV || |
| 8384 | this_mode == SR_NEAR_NEWMV || this_mode == SR_ZERO_NEWMV); |
| 8385 | compound_single_motion_search_interinter(cpi, x, bsize, frame_mv, |
| 8386 | frame_comp_mv, mi_row, mi_col, |
| 8387 | NULL, 0, rate_mv, 0, 1); |
| 8388 | } |
| 8389 | } else { |
| 8390 | *rate_mv = 0; |
| 8391 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
| 8392 | if (mode0 == NEWMV) |
| 8393 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
| 8394 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 8395 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8396 | *rate_mv += av1_mv_bit_cost(&frame_comp_mv[refs[0]].as_mv, |
| 8397 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 8398 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8399 | } |
| 8400 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8401 | } else { |
| 8402 | #if CONFIG_EXT_INTER |
| 8403 | if (is_comp_interintra_pred) { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8404 | x->best_mv = args->single_newmv[refs[0]]; |
| 8405 | *rate_mv = args->single_newmv_rate[refs[0]]; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8406 | } else { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8407 | single_motion_search(cpi, x, bsize, mi_row, mi_col, 0, rate_mv); |
| 8408 | args->single_newmv[refs[0]] = x->best_mv; |
| 8409 | args->single_newmv_rate[refs[0]] = *rate_mv; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8410 | } |
| 8411 | #else |
| 8412 | single_motion_search(cpi, x, bsize, mi_row, mi_col, rate_mv); |
| 8413 | single_newmv[refs[0]] = x->best_mv; |
| 8414 | #endif // CONFIG_EXT_INTER |
| 8415 | |
| 8416 | if (x->best_mv.as_int == INVALID_MV) return INT64_MAX; |
| 8417 | |
| 8418 | frame_mv[refs[0]] = x->best_mv; |
| 8419 | xd->mi[0]->bmi[0].as_mv[0] = x->best_mv; |
| 8420 | |
| 8421 | // Estimate the rate implications of a new mv but discount this |
| 8422 | // under certain circumstances where we want to help initiate a weak |
| 8423 | // motion field, where the distortion gain for a single block may not |
| 8424 | // be enough to overcome the cost of a new mv. |
| 8425 | if (discount_newmv_test(cpi, this_mode, x->best_mv, mode_mv, refs[0])) { |
| 8426 | *rate_mv = AOMMAX(*rate_mv / NEW_MV_DISCOUNT_FACTOR, 1); |
| 8427 | } |
| 8428 | } |
| 8429 | |
| 8430 | return 0; |
| 8431 | } |
| 8432 | |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8433 | int64_t interpolation_filter_search( |
| 8434 | MACROBLOCK *const x, const AV1_COMP *const cpi, BLOCK_SIZE bsize, |
| 8435 | int mi_row, int mi_col, const BUFFER_SET *const tmp_dst, |
| 8436 | BUFFER_SET *const orig_dst, |
| 8437 | InterpFilter (*const single_filter)[TOTAL_REFS_PER_FRAME], |
| 8438 | int64_t *const rd, int *const switchable_rate, int *const skip_txfm_sb, |
| 8439 | int64_t *const skip_sse_sb) { |
| 8440 | const AV1_COMMON *cm = &cpi->common; |
| 8441 | MACROBLOCKD *const xd = &x->e_mbd; |
| 8442 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8443 | int i; |
| 8444 | int tmp_rate; |
| 8445 | int64_t tmp_dist; |
| 8446 | |
| 8447 | (void)single_filter; |
| 8448 | |
| 8449 | InterpFilter assign_filter = SWITCHABLE; |
| 8450 | |
| 8451 | if (cm->interp_filter == SWITCHABLE) { |
| 8452 | #if !CONFIG_DUAL_FILTER |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 8453 | assign_filter = av1_is_interp_needed(xd) |
| 8454 | ? predict_interp_filter(cpi, x, bsize, mi_row, mi_col, |
| 8455 | single_filter) |
| 8456 | : cm->interp_filter; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8457 | #endif // !CONFIG_DUAL_FILTER |
| 8458 | } else { |
| 8459 | assign_filter = cm->interp_filter; |
| 8460 | } |
| 8461 | |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 8462 | set_default_interp_filters(mbmi, assign_filter); |
| 8463 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8464 | *switchable_rate = av1_get_switchable_rate(cm, x, xd); |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8465 | 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] | 8466 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, &tmp_dist, |
| 8467 | skip_txfm_sb, skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8468 | *rd = RDCOST(x->rdmult, *switchable_rate + tmp_rate, tmp_dist); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8469 | |
| 8470 | if (assign_filter == SWITCHABLE) { |
| 8471 | // do interp_filter search |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 8472 | if (av1_is_interp_needed(xd) && av1_is_interp_search_needed(xd)) { |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8473 | #if CONFIG_DUAL_FILTER |
| 8474 | const int filter_set_size = DUAL_FILTER_SET_SIZE; |
| 8475 | #else |
| 8476 | const int filter_set_size = SWITCHABLE_FILTERS; |
| 8477 | #endif // CONFIG_DUAL_FILTER |
| 8478 | int best_in_temp = 0; |
| 8479 | #if CONFIG_DUAL_FILTER |
| 8480 | InterpFilter best_filter[4]; |
| 8481 | av1_copy(best_filter, mbmi->interp_filter); |
| 8482 | #else |
| 8483 | InterpFilter best_filter = mbmi->interp_filter; |
| 8484 | #endif // CONFIG_DUAL_FILTER |
| 8485 | restore_dst_buf(xd, *tmp_dst); |
| 8486 | // EIGHTTAP_REGULAR mode is calculated beforehand |
| 8487 | for (i = 1; i < filter_set_size; ++i) { |
| 8488 | int tmp_skip_sb = 0; |
| 8489 | int64_t tmp_skip_sse = INT64_MAX; |
| 8490 | int tmp_rs; |
| 8491 | int64_t tmp_rd; |
| 8492 | #if CONFIG_DUAL_FILTER |
| 8493 | mbmi->interp_filter[0] = filter_sets[i][0]; |
| 8494 | mbmi->interp_filter[1] = filter_sets[i][1]; |
| 8495 | mbmi->interp_filter[2] = filter_sets[i][0]; |
| 8496 | mbmi->interp_filter[3] = filter_sets[i][1]; |
| 8497 | #else |
| 8498 | mbmi->interp_filter = (InterpFilter)i; |
| 8499 | #endif // CONFIG_DUAL_FILTER |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8500 | tmp_rs = av1_get_switchable_rate(cm, x, xd); |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8501 | 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] | 8502 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 8503 | &tmp_dist, &tmp_skip_sb, &tmp_skip_sse); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8504 | tmp_rd = RDCOST(x->rdmult, tmp_rs + tmp_rate, tmp_dist); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8505 | |
| 8506 | if (tmp_rd < *rd) { |
| 8507 | *rd = tmp_rd; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8508 | *switchable_rate = av1_get_switchable_rate(cm, x, xd); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8509 | #if CONFIG_DUAL_FILTER |
| 8510 | av1_copy(best_filter, mbmi->interp_filter); |
| 8511 | #else |
| 8512 | best_filter = mbmi->interp_filter; |
| 8513 | #endif // CONFIG_DUAL_FILTER |
| 8514 | *skip_txfm_sb = tmp_skip_sb; |
| 8515 | *skip_sse_sb = tmp_skip_sse; |
| 8516 | best_in_temp = !best_in_temp; |
| 8517 | if (best_in_temp) { |
| 8518 | restore_dst_buf(xd, *orig_dst); |
| 8519 | } else { |
| 8520 | restore_dst_buf(xd, *tmp_dst); |
| 8521 | } |
| 8522 | } |
| 8523 | } |
| 8524 | if (best_in_temp) { |
| 8525 | restore_dst_buf(xd, *tmp_dst); |
| 8526 | } else { |
| 8527 | restore_dst_buf(xd, *orig_dst); |
| 8528 | } |
| 8529 | #if CONFIG_DUAL_FILTER |
| 8530 | av1_copy(mbmi->interp_filter, best_filter); |
| 8531 | #else |
| 8532 | mbmi->interp_filter = best_filter; |
| 8533 | #endif // CONFIG_DUAL_FILTER |
| 8534 | } else { |
| 8535 | #if CONFIG_DUAL_FILTER |
| 8536 | for (i = 0; i < 4; ++i) |
| 8537 | assert(mbmi->interp_filter[i] == EIGHTTAP_REGULAR); |
| 8538 | #else |
| 8539 | assert(mbmi->interp_filter == EIGHTTAP_REGULAR); |
| 8540 | #endif // CONFIG_DUAL_FILTER |
| 8541 | } |
| 8542 | } |
| 8543 | |
| 8544 | return 0; |
| 8545 | } |
| 8546 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8547 | // TODO(afergs): Refactor the MBMI references in here - there's four |
| 8548 | // TODO(afergs): Refactor optional args - add them to a struct or remove |
| 8549 | static int64_t motion_mode_rd( |
| 8550 | const AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize, |
| 8551 | RD_STATS *rd_stats, RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv, |
| 8552 | int *disable_skip, int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], int mi_row, |
| 8553 | int mi_col, HandleInterModeArgs *const args, const int64_t ref_best_rd, |
| 8554 | const int *refs, int rate_mv, |
| 8555 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Wei-Ting Lin | 85a8f70 | 2017-06-22 13:55:15 -0700 | [diff] [blame] | 8556 | // only used when WARPED_MOTION is on? |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8557 | int_mv *const single_newmv, |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8558 | #if CONFIG_EXT_INTER |
Yunqing Wang | 562a393 | 2017-06-20 12:20:45 -0700 | [diff] [blame] | 8559 | int rate2_bmc_nocoeff, MB_MODE_INFO *best_bmc_mbmi, int rate_mv_bmc, |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8560 | #endif // CONFIG_EXT_INTER |
| 8561 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8562 | int rs, int *skip_txfm_sb, int64_t *skip_sse_sb, BUFFER_SET *orig_dst) { |
| 8563 | const AV1_COMMON *const cm = &cpi->common; |
| 8564 | MACROBLOCKD *xd = &x->e_mbd; |
| 8565 | MODE_INFO *mi = xd->mi[0]; |
| 8566 | MB_MODE_INFO *mbmi = &mi->mbmi; |
| 8567 | const int is_comp_pred = has_second_ref(mbmi); |
| 8568 | const PREDICTION_MODE this_mode = mbmi->mode; |
| 8569 | |
| 8570 | (void)mode_mv; |
| 8571 | (void)mi_row; |
| 8572 | (void)mi_col; |
| 8573 | (void)args; |
| 8574 | (void)refs; |
| 8575 | (void)rate_mv; |
| 8576 | (void)is_comp_pred; |
| 8577 | (void)this_mode; |
Angie Chiang | cf85d5c | 2017-08-20 12:56:19 -0700 | [diff] [blame] | 8578 | #if !CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
Cheng Chen | 811bba7 | 2017-08-21 17:39:31 -0700 | [diff] [blame] | 8579 | (void)single_newmv; |
| 8580 | #endif |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8581 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8582 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8583 | MOTION_MODE motion_mode, last_motion_mode_allowed; |
| 8584 | int rate2_nocoeff = 0, best_xskip, best_disable_skip = 0; |
| 8585 | RD_STATS best_rd_stats, best_rd_stats_y, best_rd_stats_uv; |
| 8586 | MB_MODE_INFO base_mbmi, best_mbmi; |
| 8587 | #if CONFIG_VAR_TX |
| 8588 | uint8_t best_blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 4]; |
| 8589 | #endif // CONFIG_VAR_TX |
| 8590 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8591 | |
| 8592 | #if CONFIG_WARPED_MOTION |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8593 | #if WARPED_MOTION_SORT_SAMPLES |
| 8594 | int pts0[SAMPLES_ARRAY_SIZE], pts_inref0[SAMPLES_ARRAY_SIZE]; |
| 8595 | int pts_mv0[SAMPLES_ARRAY_SIZE]; |
| 8596 | int total_samples; |
| 8597 | #else |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8598 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8599 | #endif // WARPED_MOTION_SORT_SAMPLES |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8600 | #endif // CONFIG_WARPED_MOTION |
| 8601 | |
| 8602 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8603 | av1_invalid_rd_stats(&best_rd_stats); |
| 8604 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8605 | |
| 8606 | if (cm->interp_filter == SWITCHABLE) rd_stats->rate += rs; |
| 8607 | #if CONFIG_WARPED_MOTION |
| 8608 | aom_clear_system_state(); |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8609 | #if WARPED_MOTION_SORT_SAMPLES |
| 8610 | mbmi->num_proj_ref[0] = |
| 8611 | findSamples(cm, xd, mi_row, mi_col, pts0, pts_inref0, pts_mv0); |
| 8612 | total_samples = mbmi->num_proj_ref[0]; |
| 8613 | #else |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8614 | mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref); |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8615 | #endif // WARPED_MOTION_SORT_SAMPLES |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8616 | #if CONFIG_EXT_INTER |
| 8617 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
| 8618 | #endif // CONFIG_EXT_INTER |
| 8619 | #endif // CONFIG_WARPED_MOTION |
| 8620 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8621 | rate2_nocoeff = rd_stats->rate; |
| 8622 | last_motion_mode_allowed = motion_mode_allowed( |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 8623 | #if CONFIG_GLOBAL_MOTION |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8624 | 0, xd->global_motion, |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 8625 | #endif // CONFIG_GLOBAL_MOTION |
Yue Chen | 52c5173 | 2017-07-11 15:08:30 -0700 | [diff] [blame] | 8626 | #if CONFIG_WARPED_MOTION |
| 8627 | xd, |
| 8628 | #endif |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8629 | mi); |
| 8630 | base_mbmi = *mbmi; |
| 8631 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8632 | |
| 8633 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8634 | int64_t best_rd = INT64_MAX; |
| 8635 | for (motion_mode = SIMPLE_TRANSLATION; |
| 8636 | motion_mode <= last_motion_mode_allowed; motion_mode++) { |
| 8637 | int64_t tmp_rd = INT64_MAX; |
| 8638 | int tmp_rate; |
| 8639 | int64_t tmp_dist; |
| 8640 | #if CONFIG_EXT_INTER |
| 8641 | int tmp_rate2 = |
| 8642 | motion_mode != SIMPLE_TRANSLATION ? rate2_bmc_nocoeff : rate2_nocoeff; |
| 8643 | #else |
| 8644 | int tmp_rate2 = rate2_nocoeff; |
| 8645 | #endif // CONFIG_EXT_INTER |
| 8646 | |
Wei-Ting Lin | 2088528 | 2017-08-28 17:18:18 -0700 | [diff] [blame] | 8647 | #if CONFIG_NCOBMC_ADAPT_WEIGHT |
| 8648 | // We cannot estimate the rd cost for the motion mode NCOBMC_ADAPT_WEIGHT |
| 8649 | // right now since it requires mvs from all neighboring blocks. We will |
| 8650 | // check if this mode is beneficial after all the mv's in the current |
| 8651 | // superblock are selected. |
| 8652 | if (motion_mode == NCOBMC_ADAPT_WEIGHT) continue; |
| 8653 | #endif |
| 8654 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8655 | *mbmi = base_mbmi; |
| 8656 | mbmi->motion_mode = motion_mode; |
| 8657 | #if CONFIG_MOTION_VAR |
| 8658 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8659 | #if CONFIG_EXT_INTER |
| 8660 | *mbmi = *best_bmc_mbmi; |
| 8661 | mbmi->motion_mode = OBMC_CAUSAL; |
| 8662 | #endif // CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8663 | if (!is_comp_pred && |
| 8664 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 8665 | !is_inter_singleref_comp_mode(this_mode) && |
| 8666 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 8667 | have_newmv_in_inter_mode(this_mode)) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8668 | int tmp_rate_mv = 0; |
| 8669 | |
| 8670 | single_motion_search(cpi, x, bsize, mi_row, mi_col, |
| 8671 | #if CONFIG_EXT_INTER |
Yaowu Xu | a631722 | 2017-04-17 11:29:51 -0700 | [diff] [blame] | 8672 | 0, |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8673 | #endif // CONFIG_EXT_INTER |
| 8674 | &tmp_rate_mv); |
| 8675 | mbmi->mv[0].as_int = x->best_mv.as_int; |
| 8676 | if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv, |
| 8677 | refs[0])) { |
| 8678 | tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
| 8679 | } |
| 8680 | #if CONFIG_EXT_INTER |
| 8681 | tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv; |
| 8682 | #else |
| 8683 | tmp_rate2 = rate2_nocoeff - rate_mv + tmp_rate_mv; |
| 8684 | #endif // CONFIG_EXT_INTER |
| 8685 | #if CONFIG_DUAL_FILTER |
| 8686 | if (!has_subpel_mv_component(xd->mi[0], xd, 0)) |
| 8687 | mbmi->interp_filter[0] = EIGHTTAP_REGULAR; |
| 8688 | if (!has_subpel_mv_component(xd->mi[0], xd, 1)) |
| 8689 | mbmi->interp_filter[1] = EIGHTTAP_REGULAR; |
| 8690 | #endif // CONFIG_DUAL_FILTER |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8691 | 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] | 8692 | #if CONFIG_EXT_INTER |
| 8693 | } else { |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8694 | 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] | 8695 | #endif // CONFIG_EXT_INTER |
| 8696 | } |
| 8697 | av1_build_obmc_inter_prediction( |
| 8698 | cm, xd, mi_row, mi_col, args->above_pred_buf, args->above_pred_stride, |
| 8699 | args->left_pred_buf, args->left_pred_stride); |
| 8700 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 8701 | &tmp_dist, skip_txfm_sb, skip_sse_sb); |
| 8702 | } |
| 8703 | #endif // CONFIG_MOTION_VAR |
| 8704 | |
| 8705 | #if CONFIG_WARPED_MOTION |
| 8706 | if (mbmi->motion_mode == WARPED_CAUSAL) { |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8707 | #if WARPED_MOTION_SORT_SAMPLES |
| 8708 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
| 8709 | #endif // WARPED_MOTION_SORT_SAMPLES |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8710 | #if CONFIG_EXT_INTER |
| 8711 | *mbmi = *best_bmc_mbmi; |
| 8712 | mbmi->motion_mode = WARPED_CAUSAL; |
| 8713 | #endif // CONFIG_EXT_INTER |
| 8714 | mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE; |
| 8715 | #if CONFIG_DUAL_FILTER |
Yue Chen | 05bbf9b | 2017-04-26 21:58:01 -0700 | [diff] [blame] | 8716 | for (int dir = 0; dir < 4; ++dir) |
| 8717 | mbmi->interp_filter[dir] = cm->interp_filter == SWITCHABLE |
| 8718 | ? EIGHTTAP_REGULAR |
| 8719 | : cm->interp_filter; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8720 | #else |
| 8721 | mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP_REGULAR |
| 8722 | : cm->interp_filter; |
| 8723 | #endif // CONFIG_DUAL_FILTER |
| 8724 | |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8725 | #if WARPED_MOTION_SORT_SAMPLES |
| 8726 | memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0)); |
| 8727 | memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0)); |
| 8728 | // Rank the samples by motion vector difference |
| 8729 | if (mbmi->num_proj_ref[0] > 1) { |
| 8730 | mbmi->num_proj_ref[0] = sortSamples(pts_mv0, &mbmi->mv[0].as_mv, pts, |
| 8731 | pts_inref, mbmi->num_proj_ref[0]); |
| 8732 | #if CONFIG_EXT_INTER |
| 8733 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
| 8734 | #endif // CONFIG_EXT_INTER |
| 8735 | } |
| 8736 | #endif // WARPED_MOTION_SORT_SAMPLES |
| 8737 | |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8738 | if (!find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize, |
| 8739 | mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col, |
| 8740 | &mbmi->wm_params[0], mi_row, mi_col)) { |
| 8741 | // Refine MV for NEWMV mode |
| 8742 | if (!is_comp_pred && have_newmv_in_inter_mode(this_mode)) { |
| 8743 | int tmp_rate_mv = 0; |
| 8744 | const int_mv mv0 = mbmi->mv[0]; |
| 8745 | WarpedMotionParams wm_params0 = mbmi->wm_params[0]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8746 | #if WARPED_MOTION_SORT_SAMPLES |
| 8747 | int num_proj_ref0 = mbmi->num_proj_ref[0]; |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8748 | |
| 8749 | // Refine MV in a small range. |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8750 | av1_refine_warped_mv(cpi, x, bsize, mi_row, mi_col, pts0, pts_inref0, |
| 8751 | pts_mv0, total_samples); |
| 8752 | #else |
| 8753 | // Refine MV in a small range. |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8754 | av1_refine_warped_mv(cpi, x, bsize, mi_row, mi_col, pts, pts_inref); |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8755 | #endif // WARPED_MOTION_SORT_SAMPLES |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8756 | |
| 8757 | // Keep the refined MV and WM parameters. |
| 8758 | if (mv0.as_int != mbmi->mv[0].as_int) { |
| 8759 | const int ref = refs[0]; |
| 8760 | const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 8761 | |
| 8762 | tmp_rate_mv = |
| 8763 | av1_mv_bit_cost(&mbmi->mv[0].as_mv, &ref_mv, x->nmvjointcost, |
| 8764 | x->mvcost, MV_COST_WEIGHT); |
| 8765 | |
| 8766 | if (cpi->sf.adaptive_motion_search) |
| 8767 | x->pred_mv[ref] = mbmi->mv[0].as_mv; |
| 8768 | |
| 8769 | single_newmv[ref] = mbmi->mv[0]; |
| 8770 | |
| 8771 | if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv, |
| 8772 | refs[0])) { |
| 8773 | tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
| 8774 | } |
| 8775 | #if CONFIG_EXT_INTER |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8776 | #if WARPED_MOTION_SORT_SAMPLES |
| 8777 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
| 8778 | #endif // WARPED_MOTION_SORT_SAMPLES |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8779 | tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv; |
| 8780 | #else |
| 8781 | tmp_rate2 = rate2_nocoeff - rate_mv + tmp_rate_mv; |
| 8782 | #endif // CONFIG_EXT_INTER |
| 8783 | #if CONFIG_DUAL_FILTER |
| 8784 | if (!has_subpel_mv_component(xd->mi[0], xd, 0)) |
| 8785 | mbmi->interp_filter[0] = EIGHTTAP_REGULAR; |
| 8786 | if (!has_subpel_mv_component(xd->mi[0], xd, 1)) |
| 8787 | mbmi->interp_filter[1] = EIGHTTAP_REGULAR; |
| 8788 | #endif // CONFIG_DUAL_FILTER |
| 8789 | } else { |
| 8790 | // Restore the old MV and WM parameters. |
| 8791 | mbmi->mv[0] = mv0; |
| 8792 | mbmi->wm_params[0] = wm_params0; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8793 | #if WARPED_MOTION_SORT_SAMPLES |
| 8794 | mbmi->num_proj_ref[0] = num_proj_ref0; |
| 8795 | #endif // WARPED_MOTION_SORT_SAMPLES |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8796 | } |
| 8797 | } |
| 8798 | |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8799 | 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] | 8800 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 8801 | &tmp_dist, skip_txfm_sb, skip_sse_sb); |
| 8802 | } else { |
| 8803 | continue; |
| 8804 | } |
| 8805 | } |
| 8806 | #endif // CONFIG_WARPED_MOTION |
| 8807 | x->skip = 0; |
| 8808 | |
| 8809 | rd_stats->dist = 0; |
| 8810 | rd_stats->sse = 0; |
| 8811 | rd_stats->skip = 1; |
| 8812 | rd_stats->rate = tmp_rate2; |
| 8813 | if (last_motion_mode_allowed > SIMPLE_TRANSLATION) { |
| 8814 | #if CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
| 8815 | if (last_motion_mode_allowed == WARPED_CAUSAL) |
| 8816 | #endif // CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
Yue Chen | bdc8dab | 2017-07-26 12:05:47 -0700 | [diff] [blame] | 8817 | rd_stats->rate += x->motion_mode_cost[bsize][mbmi->motion_mode]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8818 | #if CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
| 8819 | else |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8820 | rd_stats->rate += x->motion_mode_cost1[bsize][mbmi->motion_mode]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8821 | #endif // CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
| 8822 | } |
| 8823 | #if CONFIG_WARPED_MOTION |
| 8824 | if (mbmi->motion_mode == WARPED_CAUSAL) { |
| 8825 | rd_stats->rate -= rs; |
| 8826 | } |
| 8827 | #endif // CONFIG_WARPED_MOTION |
| 8828 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8829 | if (!*skip_txfm_sb) { |
| 8830 | int64_t rdcosty = INT64_MAX; |
| 8831 | int is_cost_valid_uv = 0; |
| 8832 | |
| 8833 | // cost and distortion |
| 8834 | av1_subtract_plane(x, bsize, 0); |
| 8835 | #if CONFIG_VAR_TX |
| 8836 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
| 8837 | select_tx_type_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd); |
| 8838 | } else { |
| 8839 | int idx, idy; |
| 8840 | super_block_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd); |
| 8841 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 8842 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 8843 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 8844 | memset(x->blk_skip[0], rd_stats_y->skip, |
| 8845 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 8846 | } |
| 8847 | #else |
| 8848 | /* clang-format off */ |
| 8849 | super_block_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd); |
| 8850 | /* clang-format on */ |
| 8851 | #endif // CONFIG_VAR_TX |
| 8852 | |
| 8853 | if (rd_stats_y->rate == INT_MAX) { |
| 8854 | av1_invalid_rd_stats(rd_stats); |
| 8855 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8856 | if (mbmi->motion_mode != SIMPLE_TRANSLATION) { |
| 8857 | continue; |
| 8858 | } else { |
| 8859 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8860 | restore_dst_buf(xd, *orig_dst); |
| 8861 | return INT64_MAX; |
| 8862 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8863 | } |
| 8864 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8865 | } |
| 8866 | |
| 8867 | av1_merge_rd_stats(rd_stats, rd_stats_y); |
| 8868 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8869 | rdcosty = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
| 8870 | rdcosty = AOMMIN(rdcosty, RDCOST(x->rdmult, 0, rd_stats->sse)); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8871 | /* clang-format off */ |
| 8872 | #if CONFIG_VAR_TX |
| 8873 | is_cost_valid_uv = |
| 8874 | inter_block_uvrd(cpi, x, rd_stats_uv, bsize, ref_best_rd - rdcosty); |
| 8875 | #else |
| 8876 | is_cost_valid_uv = |
| 8877 | super_block_uvrd(cpi, x, rd_stats_uv, bsize, ref_best_rd - rdcosty); |
| 8878 | #endif // CONFIG_VAR_TX |
| 8879 | if (!is_cost_valid_uv) { |
| 8880 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8881 | continue; |
| 8882 | #else |
| 8883 | restore_dst_buf(xd, *orig_dst); |
| 8884 | return INT64_MAX; |
| 8885 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8886 | } |
| 8887 | /* clang-format on */ |
| 8888 | av1_merge_rd_stats(rd_stats, rd_stats_uv); |
| 8889 | #if CONFIG_RD_DEBUG |
| 8890 | // record transform block coefficient cost |
| 8891 | // TODO(angiebird): So far rd_debug tool only detects discrepancy of |
| 8892 | // coefficient cost. Therefore, it is fine to copy rd_stats into mbmi |
| 8893 | // here because we already collect the coefficient cost. Move this part to |
| 8894 | // other place when we need to compare non-coefficient cost. |
| 8895 | mbmi->rd_stats = *rd_stats; |
| 8896 | #endif // CONFIG_RD_DEBUG |
| 8897 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8898 | if (rd_stats->skip) { |
| 8899 | rd_stats->rate -= rd_stats_uv->rate + rd_stats_y->rate; |
| 8900 | rd_stats_y->rate = 0; |
| 8901 | rd_stats_uv->rate = 0; |
| 8902 | rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 8903 | mbmi->skip = 0; |
| 8904 | // here mbmi->skip temporarily plays a role as what this_skip2 does |
| 8905 | } else if (!xd->lossless[mbmi->segment_id] && |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8906 | (RDCOST(x->rdmult, |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8907 | rd_stats_y->rate + rd_stats_uv->rate + |
| 8908 | av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
| 8909 | rd_stats->dist) >= |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8910 | RDCOST(x->rdmult, av1_cost_bit(av1_get_skip_prob(cm, xd), 1), |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8911 | rd_stats->sse))) { |
| 8912 | rd_stats->rate -= rd_stats_uv->rate + rd_stats_y->rate; |
| 8913 | rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 8914 | rd_stats->dist = rd_stats->sse; |
| 8915 | rd_stats_y->rate = 0; |
| 8916 | rd_stats_uv->rate = 0; |
| 8917 | mbmi->skip = 1; |
| 8918 | } else { |
| 8919 | rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 8920 | mbmi->skip = 0; |
| 8921 | } |
| 8922 | *disable_skip = 0; |
| 8923 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8924 | } else { |
| 8925 | x->skip = 1; |
| 8926 | *disable_skip = 1; |
| 8927 | mbmi->tx_size = tx_size_from_tx_mode(bsize, cm->tx_mode, 1); |
| 8928 | |
| 8929 | // The cost of skip bit needs to be added. |
| 8930 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8931 | mbmi->skip = 0; |
| 8932 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8933 | rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 8934 | |
| 8935 | rd_stats->dist = *skip_sse_sb; |
| 8936 | rd_stats->sse = *skip_sse_sb; |
| 8937 | rd_stats_y->rate = 0; |
| 8938 | rd_stats_uv->rate = 0; |
| 8939 | rd_stats->skip = 1; |
| 8940 | } |
| 8941 | |
| 8942 | #if CONFIG_GLOBAL_MOTION |
| 8943 | if (this_mode == ZEROMV |
| 8944 | #if CONFIG_EXT_INTER |
| 8945 | || this_mode == ZERO_ZEROMV |
| 8946 | #endif // CONFIG_EXT_INTER |
| 8947 | ) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8948 | if (is_nontrans_global_motion(xd)) { |
| 8949 | rd_stats->rate -= rs; |
| 8950 | #if CONFIG_DUAL_FILTER |
| 8951 | mbmi->interp_filter[0] = cm->interp_filter == SWITCHABLE |
| 8952 | ? EIGHTTAP_REGULAR |
| 8953 | : cm->interp_filter; |
| 8954 | mbmi->interp_filter[1] = cm->interp_filter == SWITCHABLE |
| 8955 | ? EIGHTTAP_REGULAR |
| 8956 | : cm->interp_filter; |
| 8957 | #else |
| 8958 | mbmi->interp_filter = cm->interp_filter == SWITCHABLE |
| 8959 | ? EIGHTTAP_REGULAR |
| 8960 | : cm->interp_filter; |
| 8961 | #endif // CONFIG_DUAL_FILTER |
| 8962 | } |
| 8963 | } |
| 8964 | #endif // CONFIG_GLOBAL_MOTION |
| 8965 | |
| 8966 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8967 | tmp_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8968 | if (mbmi->motion_mode == SIMPLE_TRANSLATION || (tmp_rd < best_rd)) { |
| 8969 | best_mbmi = *mbmi; |
| 8970 | best_rd = tmp_rd; |
| 8971 | best_rd_stats = *rd_stats; |
| 8972 | best_rd_stats_y = *rd_stats_y; |
| 8973 | best_rd_stats_uv = *rd_stats_uv; |
| 8974 | #if CONFIG_VAR_TX |
| 8975 | for (int i = 0; i < MAX_MB_PLANE; ++i) |
| 8976 | memcpy(best_blk_skip[i], x->blk_skip[i], |
| 8977 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 8978 | #endif // CONFIG_VAR_TX |
| 8979 | best_xskip = x->skip; |
| 8980 | best_disable_skip = *disable_skip; |
| 8981 | } |
| 8982 | } |
| 8983 | |
| 8984 | if (best_rd == INT64_MAX) { |
| 8985 | av1_invalid_rd_stats(rd_stats); |
| 8986 | restore_dst_buf(xd, *orig_dst); |
| 8987 | return INT64_MAX; |
| 8988 | } |
| 8989 | *mbmi = best_mbmi; |
| 8990 | *rd_stats = best_rd_stats; |
| 8991 | *rd_stats_y = best_rd_stats_y; |
| 8992 | *rd_stats_uv = best_rd_stats_uv; |
| 8993 | #if CONFIG_VAR_TX |
| 8994 | for (int i = 0; i < MAX_MB_PLANE; ++i) |
| 8995 | memcpy(x->blk_skip[i], best_blk_skip[i], |
| 8996 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 8997 | #endif // CONFIG_VAR_TX |
| 8998 | x->skip = best_xskip; |
| 8999 | *disable_skip = best_disable_skip; |
| 9000 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 9001 | |
| 9002 | restore_dst_buf(xd, *orig_dst); |
| 9003 | return 0; |
| 9004 | } |
| 9005 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9006 | static int64_t handle_inter_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 9007 | BLOCK_SIZE bsize, RD_STATS *rd_stats, |
| 9008 | RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv, |
| 9009 | int *disable_skip, |
| 9010 | int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], |
| 9011 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9012 | int_mv (*mode_comp_mv)[TOTAL_REFS_PER_FRAME], |
| 9013 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9014 | int mi_row, int mi_col, |
| 9015 | HandleInterModeArgs *args, |
| 9016 | const int64_t ref_best_rd) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9017 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9018 | MACROBLOCKD *xd = &x->e_mbd; |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 9019 | MODE_INFO *mi = xd->mi[0]; |
| 9020 | MB_MODE_INFO *mbmi = &mi->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9021 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 9022 | const int is_comp_pred = has_second_ref(mbmi); |
| 9023 | const int this_mode = mbmi->mode; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9024 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9025 | const int is_singleref_comp_mode = is_inter_singleref_comp_mode(this_mode); |
| 9026 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9027 | int_mv *frame_mv = mode_mv[this_mode]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9028 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9029 | // The comp mv for the compound mode in single ref |
| 9030 | int_mv *frame_comp_mv = mode_comp_mv[this_mode]; |
| 9031 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9032 | int i; |
| 9033 | int refs[2] = { mbmi->ref_frame[0], |
| 9034 | (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; |
| 9035 | int_mv cur_mv[2]; |
| 9036 | int rate_mv = 0; |
| 9037 | #if CONFIG_EXT_INTER |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 9038 | int pred_exists = 1; |
Angie Chiang | 64a2fb8 | 2017-07-06 10:15:16 -0700 | [diff] [blame] | 9039 | #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT || CONFIG_INTERINTRA |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 9040 | const int bw = block_size_wide[bsize]; |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 9041 | #endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9042 | int_mv single_newmv[TOTAL_REFS_PER_FRAME]; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9043 | #if CONFIG_INTERINTRA |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9044 | const int *const interintra_mode_cost = |
| 9045 | x->interintra_mode_cost[size_group_lookup[bsize]]; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9046 | #endif // CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9047 | const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9048 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 9049 | #else |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9050 | int_mv *const single_newmv = args->single_newmv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9051 | #endif // CONFIG_EXT_INTER |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9052 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9053 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 9054 | #else |
| 9055 | 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] | 9056 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9057 | uint8_t *tmp_buf; |
| 9058 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9059 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9060 | #if CONFIG_EXT_INTER |
| 9061 | int rate2_bmc_nocoeff; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9062 | MB_MODE_INFO best_bmc_mbmi; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9063 | int rate_mv_bmc; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9064 | #endif // CONFIG_EXT_INTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9065 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 9066 | int64_t rd = INT64_MAX; |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9067 | BUFFER_SET orig_dst, tmp_dst; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9068 | int rs = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9069 | |
| 9070 | int skip_txfm_sb = 0; |
| 9071 | int64_t skip_sse_sb = INT64_MAX; |
Yaowu Xu | b0d0d00 | 2016-11-22 09:26:43 -0800 | [diff] [blame] | 9072 | int16_t mode_ctx; |
Wei-Ting Lin | 85a8f70 | 2017-06-22 13:55:15 -0700 | [diff] [blame] | 9073 | #if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_MOTION_VAR |
| 9074 | // dummy fillers |
| 9075 | mbmi->ncobmc_mode[0] = NO_OVERLAP; |
| 9076 | mbmi->ncobmc_mode[1] = NO_OVERLAP; |
| 9077 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9078 | |
| 9079 | #if CONFIG_EXT_INTER |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9080 | #if CONFIG_INTERINTRA |
| 9081 | int compmode_interintra_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9082 | mbmi->use_wedge_interintra = 0; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9083 | #endif |
| 9084 | #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
| 9085 | int compmode_interinter_cost = 0; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9086 | mbmi->interinter_compound_type = COMPOUND_AVERAGE; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9087 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9088 | |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 9089 | #if CONFIG_INTERINTRA |
| 9090 | if (!cm->allow_interintra_compound && is_comp_interintra_pred) |
| 9091 | return INT64_MAX; |
| 9092 | #endif // CONFIG_INTERINTRA |
| 9093 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9094 | // is_comp_interintra_pred implies !is_comp_pred |
| 9095 | assert(!is_comp_interintra_pred || (!is_comp_pred)); |
| 9096 | // is_comp_interintra_pred implies is_interintra_allowed(mbmi->sb_type) |
| 9097 | assert(!is_comp_interintra_pred || is_interintra_allowed(mbmi)); |
| 9098 | #endif // CONFIG_EXT_INTER |
| 9099 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9100 | #if CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9101 | #if CONFIG_COMPOUND_SINGLEREF |
| 9102 | if (is_comp_pred || is_singleref_comp_mode) |
| 9103 | #else // !CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9104 | if (is_comp_pred) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9105 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9106 | mode_ctx = mbmi_ext->compound_mode_context[refs[0]]; |
| 9107 | else |
| 9108 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9109 | mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context, |
| 9110 | mbmi->ref_frame, bsize, -1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9111 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9112 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9113 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 9114 | tmp_buf = CONVERT_TO_BYTEPTR(tmp_buf_); |
| 9115 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9116 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9117 | tmp_buf = tmp_buf_; |
David Barker | b8069f9 | 2016-11-18 14:49:56 +0000 | [diff] [blame] | 9118 | // Make sure that we didn't leave the plane destination buffers set |
| 9119 | // to tmp_buf at the end of the last iteration |
| 9120 | assert(xd->plane[0].dst.buf != tmp_buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9121 | |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9122 | #if CONFIG_WARPED_MOTION |
| 9123 | mbmi->num_proj_ref[0] = 0; |
| 9124 | mbmi->num_proj_ref[1] = 0; |
| 9125 | #endif // CONFIG_WARPED_MOTION |
| 9126 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9127 | if (is_comp_pred) { |
| 9128 | if (frame_mv[refs[0]].as_int == INVALID_MV || |
| 9129 | frame_mv[refs[1]].as_int == INVALID_MV) |
| 9130 | return INT64_MAX; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9131 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9132 | } else if (is_singleref_comp_mode) { |
| 9133 | if (frame_mv[refs[0]].as_int == INVALID_MV || |
| 9134 | frame_comp_mv[refs[0]].as_int == INVALID_MV) |
| 9135 | return INT64_MAX; |
| 9136 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9137 | } |
| 9138 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 9139 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9140 | if (have_newmv_in_inter_mode(this_mode)) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9141 | const int64_t ret_val = |
| 9142 | handle_newmv(cpi, x, bsize, mode_mv, |
| 9143 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9144 | mode_comp_mv, |
| 9145 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9146 | mi_row, mi_col, &rate_mv, single_newmv, args); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 9147 | if (ret_val != 0) |
| 9148 | return ret_val; |
| 9149 | else |
| 9150 | rd_stats->rate += rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9151 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9152 | for (i = 0; i < is_comp_pred + 1; ++i) { |
| 9153 | cur_mv[i] = frame_mv[refs[i]]; |
Zoe Liu | ed29ea2 | 2017-04-20 16:48:15 -0700 | [diff] [blame] | 9154 | // Clip "next_nearest" so that it does not extend to far out of image |
| 9155 | if (this_mode != NEWMV) clamp_mv2(&cur_mv[i].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 9156 | 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] | 9157 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 9158 | } |
| 9159 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9160 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9161 | if (!is_comp_pred && is_singleref_comp_mode) { |
| 9162 | cur_mv[1] = frame_comp_mv[refs[0]]; |
| 9163 | // Clip "next_nearest" so that it does not extend to far out of image |
| 9164 | if (this_mode != NEWMV) clamp_mv2(&cur_mv[1].as_mv, xd); |
| 9165 | if (mv_check_bounds(&x->mv_limits, &cur_mv[1].as_mv)) return INT64_MAX; |
| 9166 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 9167 | } |
| 9168 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9169 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9170 | #if CONFIG_EXT_INTER |
Angie Chiang | 78a3bc1 | 2016-11-06 12:55:46 -0800 | [diff] [blame] | 9171 | if (this_mode == NEAREST_NEARESTMV) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9172 | #else |
Angie Chiang | 78a3bc1 | 2016-11-06 12:55:46 -0800 | [diff] [blame] | 9173 | if (this_mode == NEARESTMV && is_comp_pred) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9174 | #endif // CONFIG_EXT_INTER |
Angie Chiang | 78a3bc1 | 2016-11-06 12:55:46 -0800 | [diff] [blame] | 9175 | { |
| 9176 | #if !CONFIG_EXT_INTER |
| 9177 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 9178 | #endif // !CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9179 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
| 9180 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 9181 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 9182 | |
| 9183 | for (i = 0; i < 2; ++i) { |
| 9184 | clamp_mv2(&cur_mv[i].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 9185 | 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] | 9186 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 9187 | } |
| 9188 | } |
| 9189 | } |
| 9190 | |
| 9191 | #if CONFIG_EXT_INTER |
| 9192 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9193 | #if CONFIG_COMPOUND_SINGLEREF |
| 9194 | if (this_mode == NEAREST_NEWMV || // this_mode == SR_NEAREST_NEWMV || |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9195 | this_mode == SR_NEAREST_NEARMV) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9196 | #else // !CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9197 | if (this_mode == NEAREST_NEWMV) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9198 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9199 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9200 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 9201 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9202 | #if CONFIG_AMVR |
| 9203 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv, |
| 9204 | cm->cur_frame_mv_precision_level); |
| 9205 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9206 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9207 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9208 | clamp_mv2(&cur_mv[0].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 9209 | 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] | 9210 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 9211 | } |
| 9212 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 9213 | if (this_mode == NEW_NEARESTMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9214 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 9215 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9216 | #if CONFIG_AMVR |
| 9217 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv, |
| 9218 | cm->cur_frame_mv_precision_level); |
| 9219 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9220 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9221 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9222 | clamp_mv2(&cur_mv[1].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 9223 | 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] | 9224 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 9225 | } |
| 9226 | } |
| 9227 | |
| 9228 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 9229 | int ref_mv_idx = mbmi->ref_mv_idx + 1; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9230 | if (this_mode == NEAR_NEWMV || |
| 9231 | #if CONFIG_COMPOUND_SINGLEREF |
| 9232 | this_mode == SR_NEAR_NEWMV || |
| 9233 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 9234 | this_mode == NEAR_NEARMV) { |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 9235 | 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] | 9236 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9237 | #if CONFIG_AMVR |
| 9238 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv, |
| 9239 | cm->cur_frame_mv_precision_level); |
| 9240 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9241 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9242 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9243 | clamp_mv2(&cur_mv[0].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 9244 | 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] | 9245 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 9246 | } |
| 9247 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9248 | if (this_mode == NEW_NEARMV || |
| 9249 | #if CONFIG_COMPOUND_SINGLEREF |
| 9250 | this_mode == SR_NEAREST_NEARMV || |
| 9251 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 9252 | this_mode == NEAR_NEARMV) { |
| 9253 | #if CONFIG_COMPOUND_SINGLEREF |
| 9254 | if (this_mode == SR_NEAREST_NEARMV) |
| 9255 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 9256 | else |
| 9257 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 9258 | 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] | 9259 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9260 | #if CONFIG_AMVR |
| 9261 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv, |
| 9262 | cm->cur_frame_mv_precision_level); |
| 9263 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9264 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 9265 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9266 | clamp_mv2(&cur_mv[1].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 9267 | 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] | 9268 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 9269 | } |
| 9270 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9271 | #else // !CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9272 | if (this_mode == NEARMV && is_comp_pred) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9273 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9274 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
| 9275 | int ref_mv_idx = mbmi->ref_mv_idx + 1; |
| 9276 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 9277 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv; |
| 9278 | |
| 9279 | for (i = 0; i < 2; ++i) { |
| 9280 | clamp_mv2(&cur_mv[i].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 9281 | 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] | 9282 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 9283 | } |
| 9284 | } |
| 9285 | } |
| 9286 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9287 | |
| 9288 | // do first prediction into the destination buffer. Do the next |
| 9289 | // prediction into a temporary buffer. Then keep track of which one |
| 9290 | // of these currently holds the best predictor, and use the other |
| 9291 | // one for future predictions. In the end, copy from tmp_buf to |
| 9292 | // dst if necessary. |
| 9293 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9294 | tmp_dst.plane[i] = tmp_buf + i * MAX_SB_SQUARE; |
| 9295 | tmp_dst.stride[i] = MAX_SB_SIZE; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 9296 | } |
| 9297 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9298 | orig_dst.plane[i] = xd->plane[i].dst.buf; |
| 9299 | orig_dst.stride[i] = xd->plane[i].dst.stride; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9300 | } |
| 9301 | |
| 9302 | // We don't include the cost of the second reference here, because there |
| 9303 | // are only three options: Last/Golden, ARF/Last or Golden/ARF, or in other |
| 9304 | // words if you present them in that order, the second one is always known |
| 9305 | // if the first is known. |
| 9306 | // |
| 9307 | // Under some circumstances we discount the cost of new mv mode to encourage |
| 9308 | // initiation of a motion field. |
| 9309 | if (discount_newmv_test(cpi, this_mode, frame_mv[refs[0]], mode_mv, |
| 9310 | refs[0])) { |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 9311 | #if CONFIG_EXT_INTER |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9312 | rd_stats->rate += AOMMIN( |
| 9313 | cost_mv_ref(x, this_mode, mode_ctx), |
| 9314 | cost_mv_ref(x, is_comp_pred ? NEAREST_NEARESTMV : NEARESTMV, mode_ctx)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9315 | #else |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9316 | rd_stats->rate += AOMMIN(cost_mv_ref(x, this_mode, mode_ctx), |
| 9317 | cost_mv_ref(x, NEARESTMV, mode_ctx)); |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 9318 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9319 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9320 | rd_stats->rate += cost_mv_ref(x, this_mode, mode_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9321 | } |
| 9322 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9323 | if (RDCOST(x->rdmult, rd_stats->rate, 0) > ref_best_rd && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9324 | #if CONFIG_EXT_INTER |
| 9325 | mbmi->mode != NEARESTMV && mbmi->mode != NEAREST_NEARESTMV |
| 9326 | #else |
| 9327 | mbmi->mode != NEARESTMV |
| 9328 | #endif // CONFIG_EXT_INTER |
| 9329 | ) |
| 9330 | return INT64_MAX; |
| 9331 | |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 9332 | int64_t ret_val = interpolation_filter_search( |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9333 | x, cpi, bsize, mi_row, mi_col, &tmp_dst, &orig_dst, args->single_filter, |
| 9334 | &rd, &rs, &skip_txfm_sb, &skip_sse_sb); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 9335 | if (ret_val != 0) return ret_val; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9336 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9337 | #if CONFIG_EXT_INTER |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9338 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9339 | best_bmc_mbmi = *mbmi; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 9340 | rate2_bmc_nocoeff = rd_stats->rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9341 | if (cm->interp_filter == SWITCHABLE) rate2_bmc_nocoeff += rs; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9342 | rate_mv_bmc = rate_mv; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9343 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9344 | |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9345 | #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9346 | #if CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9347 | if (is_comp_pred || is_singleref_comp_mode) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9348 | #else |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9349 | if (is_comp_pred) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9350 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9351 | { |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 9352 | int rate_sum, rs2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9353 | int64_t dist_sum; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9354 | int64_t best_rd_compound = INT64_MAX, best_rd_cur = INT64_MAX; |
| 9355 | INTERINTER_COMPOUND_DATA best_compound_data; |
| 9356 | int_mv best_mv[2]; |
| 9357 | int best_tmp_rate_mv = rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9358 | int tmp_skip_txfm_sb; |
| 9359 | int64_t tmp_skip_sse_sb; |
Yaowu Xu | 5e8007f | 2017-06-28 12:39:18 -0700 | [diff] [blame] | 9360 | DECLARE_ALIGNED(16, uint8_t, pred0[2 * MAX_SB_SQUARE]); |
| 9361 | DECLARE_ALIGNED(16, uint8_t, pred1[2 * MAX_SB_SQUARE]); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9362 | uint8_t *preds0[1] = { pred0 }; |
| 9363 | uint8_t *preds1[1] = { pred1 }; |
| 9364 | int strides[1] = { bw }; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 9365 | int tmp_rate_mv; |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 9366 | int masked_compound_used = is_any_masked_compound_used(bsize); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 9367 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
| 9368 | masked_compound_used = masked_compound_used && cm->allow_masked_compound; |
| 9369 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9370 | COMPOUND_TYPE cur_type; |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 9371 | int best_compmode_interinter_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9372 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9373 | best_mv[0].as_int = cur_mv[0].as_int; |
| 9374 | best_mv[1].as_int = cur_mv[1].as_int; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9375 | memset(&best_compound_data, 0, sizeof(best_compound_data)); |
| 9376 | #if CONFIG_COMPOUND_SEGMENT |
| 9377 | uint8_t tmp_mask_buf[2 * MAX_SB_SQUARE]; |
| 9378 | best_compound_data.seg_mask = tmp_mask_buf; |
| 9379 | #endif // CONFIG_COMPOUND_SEGMENT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9380 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9381 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9382 | // TODO(zoeliu): To further check whether the following setups are needed. |
| 9383 | // Single ref compound mode: Prepare the 2nd ref frame predictor the same as |
| 9384 | // the 1st one. |
| 9385 | if (!is_comp_pred && is_singleref_comp_mode) { |
| 9386 | xd->block_refs[1] = xd->block_refs[0]; |
| 9387 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 9388 | xd->plane[i].pre[1] = xd->plane[i].pre[0]; |
| 9389 | } |
| 9390 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 9391 | |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 9392 | if (masked_compound_used) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9393 | // get inter predictors to use for masked compound modes |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9394 | av1_build_inter_predictors_for_planes_single_buf( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9395 | xd, bsize, 0, 0, mi_row, mi_col, 0, preds0, strides); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9396 | av1_build_inter_predictors_for_planes_single_buf( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9397 | xd, bsize, 0, 0, mi_row, mi_col, 1, preds1, strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9398 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9399 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9400 | for (cur_type = COMPOUND_AVERAGE; cur_type < COMPOUND_TYPES; cur_type++) { |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 9401 | if (cur_type != COMPOUND_AVERAGE && !masked_compound_used) break; |
Yue Chen | e251825 | 2017-06-05 12:36:46 -0700 | [diff] [blame] | 9402 | if (!is_interinter_compound_used(cur_type, bsize)) continue; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 9403 | tmp_rate_mv = rate_mv; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9404 | best_rd_cur = INT64_MAX; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9405 | mbmi->interinter_compound_type = cur_type; |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 9406 | int masked_type_cost = 0; |
| 9407 | if (masked_compound_used) { |
| 9408 | #if CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT |
| 9409 | if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize)) |
| 9410 | masked_type_cost += av1_cost_literal(1); |
| 9411 | else |
| 9412 | #endif // CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT |
| 9413 | masked_type_cost += |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 9414 | x->compound_type_cost[bsize][mbmi->interinter_compound_type]; |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 9415 | } |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9416 | rs2 = av1_cost_literal(get_interinter_compound_type_bits( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9417 | bsize, mbmi->interinter_compound_type)) + |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 9418 | masked_type_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9419 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9420 | switch (cur_type) { |
| 9421 | case COMPOUND_AVERAGE: |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 9422 | av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst, |
| 9423 | bsize); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9424 | av1_subtract_plane(x, bsize, 0); |
| 9425 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 9426 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, |
| 9427 | INT64_MAX); |
| 9428 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9429 | 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] | 9430 | best_rd_compound = best_rd_cur; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9431 | break; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9432 | #if CONFIG_WEDGE |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9433 | case COMPOUND_WEDGE: |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9434 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh && |
| 9435 | best_rd_compound / 3 < ref_best_rd) { |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 9436 | best_rd_cur = build_and_cost_compound_type( |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9437 | cpi, x, cur_mv, bsize, this_mode, rs2, rate_mv, &orig_dst, |
| 9438 | &tmp_rate_mv, preds0, preds1, strides, mi_row, mi_col); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9439 | } |
| 9440 | break; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9441 | #endif // CONFIG_WEDGE |
Sarah Parker | 2f6ce75 | 2016-12-08 15:26:46 -0800 | [diff] [blame] | 9442 | #if CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 9443 | case COMPOUND_SEG: |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 9444 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh && |
| 9445 | best_rd_compound / 3 < ref_best_rd) { |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 9446 | best_rd_cur = build_and_cost_compound_type( |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 9447 | cpi, x, cur_mv, bsize, this_mode, rs2, rate_mv, &orig_dst, |
| 9448 | &tmp_rate_mv, preds0, preds1, strides, mi_row, mi_col); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 9449 | } |
| 9450 | break; |
Sarah Parker | 2f6ce75 | 2016-12-08 15:26:46 -0800 | [diff] [blame] | 9451 | #endif // CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9452 | default: assert(0); return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9453 | } |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 9454 | |
| 9455 | if (best_rd_cur < best_rd_compound) { |
| 9456 | best_rd_compound = best_rd_cur; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9457 | #if CONFIG_WEDGE |
| 9458 | best_compound_data.wedge_index = mbmi->wedge_index; |
| 9459 | best_compound_data.wedge_sign = mbmi->wedge_sign; |
| 9460 | #endif // CONFIG_WEDGE |
| 9461 | #if CONFIG_COMPOUND_SEGMENT |
| 9462 | best_compound_data.mask_type = mbmi->mask_type; |
| 9463 | memcpy(best_compound_data.seg_mask, xd->seg_mask, |
| 9464 | 2 * MAX_SB_SQUARE * sizeof(uint8_t)); |
| 9465 | #endif // CONFIG_COMPOUND_SEGMENT |
| 9466 | best_compound_data.interinter_compound_type = |
| 9467 | mbmi->interinter_compound_type; |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 9468 | best_compmode_interinter_cost = rs2; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 9469 | if (have_newmv_in_inter_mode(this_mode)) { |
| 9470 | if (use_masked_motion_search(cur_type)) { |
| 9471 | best_tmp_rate_mv = tmp_rate_mv; |
| 9472 | best_mv[0].as_int = mbmi->mv[0].as_int; |
| 9473 | best_mv[1].as_int = mbmi->mv[1].as_int; |
| 9474 | } else { |
| 9475 | best_mv[0].as_int = cur_mv[0].as_int; |
| 9476 | best_mv[1].as_int = cur_mv[1].as_int; |
| 9477 | } |
| 9478 | } |
| 9479 | } |
| 9480 | // reset to original mvs for next iteration |
| 9481 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 9482 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9483 | } |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9484 | #if CONFIG_WEDGE |
| 9485 | mbmi->wedge_index = best_compound_data.wedge_index; |
| 9486 | mbmi->wedge_sign = best_compound_data.wedge_sign; |
| 9487 | #endif // CONFIG_WEDGE |
| 9488 | #if CONFIG_COMPOUND_SEGMENT |
| 9489 | mbmi->mask_type = best_compound_data.mask_type; |
| 9490 | memcpy(xd->seg_mask, best_compound_data.seg_mask, |
| 9491 | 2 * MAX_SB_SQUARE * sizeof(uint8_t)); |
| 9492 | #endif // CONFIG_COMPOUND_SEGMENT |
| 9493 | mbmi->interinter_compound_type = |
| 9494 | best_compound_data.interinter_compound_type; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9495 | if (have_newmv_in_inter_mode(this_mode)) { |
| 9496 | mbmi->mv[0].as_int = best_mv[0].as_int; |
| 9497 | mbmi->mv[1].as_int = best_mv[1].as_int; |
| 9498 | xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int; |
| 9499 | 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] | 9500 | if (use_masked_motion_search(mbmi->interinter_compound_type)) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9501 | rd_stats->rate += best_tmp_rate_mv - rate_mv; |
| 9502 | rate_mv = best_tmp_rate_mv; |
| 9503 | } |
| 9504 | } |
| 9505 | |
| 9506 | 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] | 9507 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9508 | return INT64_MAX; |
David Barker | b8069f9 | 2016-11-18 14:49:56 +0000 | [diff] [blame] | 9509 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9510 | |
| 9511 | pred_exists = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9512 | |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 9513 | compmode_interinter_cost = best_compmode_interinter_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9514 | } |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9515 | #endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9516 | |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9517 | #if CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9518 | if (is_comp_interintra_pred) { |
| 9519 | INTERINTRA_MODE best_interintra_mode = II_DC_PRED; |
| 9520 | int64_t best_interintra_rd = INT64_MAX; |
| 9521 | int rmode, rate_sum; |
| 9522 | int64_t dist_sum; |
| 9523 | int j; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9524 | int tmp_rate_mv = 0; |
| 9525 | int tmp_skip_txfm_sb; |
| 9526 | int64_t tmp_skip_sse_sb; |
| 9527 | DECLARE_ALIGNED(16, uint8_t, intrapred_[2 * MAX_SB_SQUARE]); |
| 9528 | uint8_t *intrapred; |
| 9529 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9530 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9531 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 9532 | intrapred = CONVERT_TO_BYTEPTR(intrapred_); |
| 9533 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9534 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9535 | intrapred = intrapred_; |
| 9536 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 9537 | mbmi->ref_frame[1] = NONE_FRAME; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9538 | for (j = 0; j < MAX_MB_PLANE; j++) { |
| 9539 | xd->plane[j].dst.buf = tmp_buf + j * MAX_SB_SQUARE; |
| 9540 | xd->plane[j].dst.stride = bw; |
| 9541 | } |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 9542 | 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] | 9543 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9544 | mbmi->ref_frame[1] = INTRA_FRAME; |
| 9545 | mbmi->use_wedge_interintra = 0; |
| 9546 | |
| 9547 | for (j = 0; j < INTERINTRA_MODES; ++j) { |
| 9548 | mbmi->interintra_mode = (INTERINTRA_MODE)j; |
| 9549 | rmode = interintra_mode_cost[mbmi->interintra_mode]; |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 9550 | av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, &orig_dst, |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9551 | intrapred, bw); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9552 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9553 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 9554 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9555 | rd = RDCOST(x->rdmult, tmp_rate_mv + rate_sum + rmode, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9556 | if (rd < best_interintra_rd) { |
| 9557 | best_interintra_rd = rd; |
| 9558 | best_interintra_mode = mbmi->interintra_mode; |
| 9559 | } |
| 9560 | } |
| 9561 | mbmi->interintra_mode = best_interintra_mode; |
| 9562 | rmode = interintra_mode_cost[mbmi->interintra_mode]; |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 9563 | av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, &orig_dst, |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9564 | intrapred, bw); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9565 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
| 9566 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9567 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 9568 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 9569 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9570 | rd = RDCOST(x->rdmult, rate_mv + rmode + rate_sum, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9571 | best_interintra_rd = rd; |
| 9572 | |
| 9573 | 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] | 9574 | // Don't need to call restore_dst_buf here |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9575 | return INT64_MAX; |
| 9576 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9577 | #if CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9578 | if (is_interintra_wedge_used(bsize)) { |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9579 | int64_t best_interintra_rd_nowedge = INT64_MAX; |
| 9580 | int64_t best_interintra_rd_wedge = INT64_MAX; |
| 9581 | int_mv tmp_mv; |
| 9582 | int rwedge = av1_cost_bit(cm->fc->wedge_interintra_prob[bsize], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9583 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9584 | rd = RDCOST(x->rdmult, rmode + rate_mv + rwedge + rate_sum, dist_sum); |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 9585 | best_interintra_rd_nowedge = best_interintra_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9586 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 9587 | // Disable wedge search if source variance is small |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9588 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh) { |
| 9589 | mbmi->use_wedge_interintra = 1; |
| 9590 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9591 | rwedge = av1_cost_literal(get_interintra_wedge_bits(bsize)) + |
| 9592 | av1_cost_bit(cm->fc->wedge_interintra_prob[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9593 | |
| 9594 | best_interintra_rd_wedge = |
| 9595 | pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_); |
| 9596 | |
| 9597 | best_interintra_rd_wedge += |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9598 | RDCOST(x->rdmult, rmode + rate_mv + rwedge, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9599 | // Refine motion vector. |
| 9600 | if (have_newmv_in_inter_mode(this_mode)) { |
| 9601 | // get negative of mask |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9602 | const uint8_t *mask = av1_get_contiguous_soft_mask( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9603 | mbmi->interintra_wedge_index, 1, bsize); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 9604 | tmp_mv.as_int = x->mbmi_ext->ref_mvs[refs[0]][0].as_int; |
| 9605 | compound_single_motion_search(cpi, x, bsize, &tmp_mv.as_mv, mi_row, |
| 9606 | mi_col, intrapred, mask, bw, |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 9607 | &tmp_rate_mv, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9608 | mbmi->mv[0].as_int = tmp_mv.as_int; |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 9609 | av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst, |
| 9610 | bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9611 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 9612 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9613 | rd = RDCOST(x->rdmult, rmode + tmp_rate_mv + rwedge + rate_sum, |
| 9614 | dist_sum); |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 9615 | if (rd >= best_interintra_rd_wedge) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9616 | tmp_mv.as_int = cur_mv[0].as_int; |
| 9617 | tmp_rate_mv = rate_mv; |
| 9618 | } |
| 9619 | } else { |
| 9620 | tmp_mv.as_int = cur_mv[0].as_int; |
| 9621 | tmp_rate_mv = rate_mv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9622 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9623 | } |
| 9624 | // Evaluate closer to true rd |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9625 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9626 | rd = |
| 9627 | estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 9628 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 9629 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9630 | rd = RDCOST(x->rdmult, rmode + tmp_rate_mv + rwedge + rate_sum, |
| 9631 | dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9632 | best_interintra_rd_wedge = rd; |
| 9633 | if (best_interintra_rd_wedge < best_interintra_rd_nowedge) { |
| 9634 | mbmi->use_wedge_interintra = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9635 | mbmi->mv[0].as_int = tmp_mv.as_int; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 9636 | rd_stats->rate += tmp_rate_mv - rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9637 | rate_mv = tmp_rate_mv; |
| 9638 | } else { |
| 9639 | mbmi->use_wedge_interintra = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9640 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 9641 | } |
| 9642 | } else { |
| 9643 | mbmi->use_wedge_interintra = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9644 | } |
| 9645 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9646 | #endif // CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9647 | |
| 9648 | pred_exists = 0; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9649 | compmode_interintra_cost = |
| 9650 | av1_cost_bit(cm->fc->interintra_prob[size_group_lookup[bsize]], 1) + |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 9651 | interintra_mode_cost[mbmi->interintra_mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9652 | if (is_interintra_wedge_used(bsize)) { |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9653 | compmode_interintra_cost += av1_cost_bit( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9654 | cm->fc->wedge_interintra_prob[bsize], mbmi->use_wedge_interintra); |
| 9655 | if (mbmi->use_wedge_interintra) { |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9656 | compmode_interintra_cost += |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9657 | av1_cost_literal(get_interintra_wedge_bits(bsize)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9658 | } |
| 9659 | } |
| 9660 | } else if (is_interintra_allowed(mbmi)) { |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9661 | compmode_interintra_cost = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9662 | av1_cost_bit(cm->fc->interintra_prob[size_group_lookup[bsize]], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9663 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9664 | #endif // CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9665 | |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 9666 | if (pred_exists == 0) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9667 | int tmp_rate; |
| 9668 | int64_t tmp_dist; |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 9669 | 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] | 9670 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 9671 | &tmp_dist, &skip_txfm_sb, &skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9672 | rd = RDCOST(x->rdmult, rs + tmp_rate, tmp_dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9673 | } |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 9674 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9675 | |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 9676 | if (!is_comp_pred) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9677 | #if CONFIG_DUAL_FILTER |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9678 | args->single_filter[this_mode][refs[0]] = mbmi->interp_filter[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9679 | #else |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9680 | args->single_filter[this_mode][refs[0]] = mbmi->interp_filter; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 9681 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9682 | |
| 9683 | #if CONFIG_EXT_INTER |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9684 | if (args->modelled_rd != NULL) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9685 | if (is_comp_pred) { |
| 9686 | const int mode0 = compound_ref0_mode(this_mode); |
| 9687 | const int mode1 = compound_ref1_mode(this_mode); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9688 | const int64_t mrd = AOMMIN(args->modelled_rd[mode0][refs[0]], |
| 9689 | args->modelled_rd[mode1][refs[1]]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9690 | if (rd / 4 * 3 > mrd && ref_best_rd < INT64_MAX) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9691 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9692 | return INT64_MAX; |
| 9693 | } |
| 9694 | } else if (!is_comp_interintra_pred) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9695 | args->modelled_rd[this_mode][refs[0]] = rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9696 | } |
| 9697 | } |
| 9698 | #endif // CONFIG_EXT_INTER |
| 9699 | |
| 9700 | if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) { |
| 9701 | // if current pred_error modeled rd is substantially more than the best |
| 9702 | // so far, do not bother doing full rd |
| 9703 | if (rd / 2 > ref_best_rd) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9704 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9705 | return INT64_MAX; |
| 9706 | } |
| 9707 | } |
| 9708 | |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9709 | #if CONFIG_EXT_INTER |
| 9710 | #if CONFIG_INTERINTRA |
| 9711 | rd_stats->rate += compmode_interintra_cost; |
| 9712 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 9713 | rate2_bmc_nocoeff += compmode_interintra_cost; |
| 9714 | #endif |
| 9715 | #endif |
| 9716 | #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
| 9717 | rd_stats->rate += compmode_interinter_cost; |
| 9718 | #endif |
| 9719 | #endif |
| 9720 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 9721 | ret_val = motion_mode_rd(cpi, x, bsize, rd_stats, rd_stats_y, rd_stats_uv, |
| 9722 | disable_skip, mode_mv, mi_row, mi_col, args, |
| 9723 | ref_best_rd, refs, rate_mv, |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9724 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 9725 | single_newmv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9726 | #if CONFIG_EXT_INTER |
Yunqing Wang | 562a393 | 2017-06-20 12:20:45 -0700 | [diff] [blame] | 9727 | rate2_bmc_nocoeff, &best_bmc_mbmi, rate_mv_bmc, |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9728 | #endif // CONFIG_EXT_INTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9729 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 9730 | rs, &skip_txfm_sb, &skip_sse_sb, &orig_dst); |
| 9731 | if (ret_val != 0) return ret_val; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 9732 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9733 | return 0; // The rate-distortion cost will be re-calculated by caller. |
| 9734 | } |
| 9735 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9736 | #if CONFIG_INTRABC |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9737 | 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] | 9738 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9739 | int64_t best_rd) { |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9740 | const AV1_COMMON *const cm = &cpi->common; |
| 9741 | if (bsize < BLOCK_8X8 || !cm->allow_screen_content_tools) return INT64_MAX; |
| 9742 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9743 | MACROBLOCKD *const xd = &x->e_mbd; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9744 | const TileInfo *tile = &xd->tile; |
| 9745 | MODE_INFO *const mi = xd->mi[0]; |
| 9746 | const int mi_row = -xd->mb_to_top_edge / (8 * MI_SIZE); |
| 9747 | const int mi_col = -xd->mb_to_left_edge / (8 * MI_SIZE); |
| 9748 | const int w = block_size_wide[bsize]; |
| 9749 | const int h = block_size_high[bsize]; |
| 9750 | const int sb_row = mi_row / MAX_MIB_SIZE; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9751 | const int sb_col = mi_col / MAX_MIB_SIZE; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9752 | |
Alex Converse | 44c2bad | 2017-05-11 09:36:10 -0700 | [diff] [blame] | 9753 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 9754 | MV_REFERENCE_FRAME ref_frame = INTRA_FRAME; |
| 9755 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 9756 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 9757 | mbmi_ext->ref_mv_stack[ref_frame], |
| 9758 | #if CONFIG_EXT_INTER |
| 9759 | mbmi_ext->compound_mode_context, |
| 9760 | #endif // CONFIG_EXT_INTER |
| 9761 | candidates, mi_row, mi_col, NULL, NULL, |
| 9762 | mbmi_ext->mode_context); |
| 9763 | |
| 9764 | int_mv nearestmv, nearmv; |
| 9765 | av1_find_best_ref_mvs(0, candidates, &nearestmv, &nearmv); |
| 9766 | |
| 9767 | int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv; |
| 9768 | if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col); |
| 9769 | mbmi_ext->ref_mvs[INTRA_FRAME][0] = dv_ref; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9770 | |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9771 | struct buf_2d yv12_mb[MAX_MB_PLANE]; |
| 9772 | av1_setup_pred_block(xd, yv12_mb, xd->cur_buf, mi_row, mi_col, NULL, NULL); |
| 9773 | for (int i = 0; i < MAX_MB_PLANE; ++i) { |
| 9774 | xd->plane[i].pre[0] = yv12_mb[i]; |
| 9775 | } |
| 9776 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9777 | enum IntrabcMotionDirection { |
| 9778 | IBC_MOTION_ABOVE, |
| 9779 | IBC_MOTION_LEFT, |
| 9780 | IBC_MOTION_DIRECTIONS |
| 9781 | }; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9782 | |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9783 | MB_MODE_INFO *mbmi = &mi->mbmi; |
| 9784 | MB_MODE_INFO best_mbmi = *mbmi; |
| 9785 | RD_STATS best_rdcost = *rd_cost; |
| 9786 | int best_skip = x->skip; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9787 | |
| 9788 | for (enum IntrabcMotionDirection dir = IBC_MOTION_ABOVE; |
| 9789 | dir < IBC_MOTION_DIRECTIONS; ++dir) { |
| 9790 | const MvLimits tmp_mv_limits = x->mv_limits; |
| 9791 | switch (dir) { |
| 9792 | case IBC_MOTION_ABOVE: |
| 9793 | x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE; |
| 9794 | x->mv_limits.col_max = (tile->mi_col_end - mi_col) * MI_SIZE - w; |
| 9795 | x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE; |
| 9796 | x->mv_limits.row_max = (sb_row * MAX_MIB_SIZE - mi_row) * MI_SIZE - h; |
| 9797 | break; |
| 9798 | case IBC_MOTION_LEFT: |
| 9799 | x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE; |
| 9800 | x->mv_limits.col_max = (sb_col * MAX_MIB_SIZE - mi_col) * MI_SIZE - w; |
| 9801 | // TODO(aconverse@google.com): Minimize the overlap between above and |
| 9802 | // left areas. |
| 9803 | x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE; |
| 9804 | int bottom_coded_mi_edge = |
| 9805 | AOMMIN((sb_row + 1) * MAX_MIB_SIZE, tile->mi_row_end); |
| 9806 | x->mv_limits.row_max = (bottom_coded_mi_edge - mi_row) * MI_SIZE - h; |
| 9807 | break; |
| 9808 | default: assert(0); |
| 9809 | } |
| 9810 | assert(x->mv_limits.col_min >= tmp_mv_limits.col_min); |
| 9811 | assert(x->mv_limits.col_max <= tmp_mv_limits.col_max); |
| 9812 | assert(x->mv_limits.row_min >= tmp_mv_limits.row_min); |
| 9813 | assert(x->mv_limits.row_max <= tmp_mv_limits.row_max); |
| 9814 | av1_set_mv_search_range(&x->mv_limits, &dv_ref.as_mv); |
| 9815 | |
| 9816 | if (x->mv_limits.col_max < x->mv_limits.col_min || |
| 9817 | x->mv_limits.row_max < x->mv_limits.row_min) { |
| 9818 | x->mv_limits = tmp_mv_limits; |
| 9819 | continue; |
| 9820 | } |
| 9821 | |
| 9822 | int step_param = cpi->mv_step_param; |
| 9823 | MV mvp_full = dv_ref.as_mv; |
| 9824 | mvp_full.col >>= 3; |
| 9825 | mvp_full.row >>= 3; |
| 9826 | int sadpb = x->sadperbit16; |
| 9827 | int cost_list[5]; |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 9828 | #if CONFIG_HASH_ME |
RogerZhou | d15e7c1 | 2017-09-26 08:49:28 -0700 | [diff] [blame] | 9829 | int bestsme = av1_full_pixel_search( |
| 9830 | cpi, x, bsize, &mvp_full, step_param, sadpb, |
| 9831 | cond_cost_list(cpi, cost_list), &dv_ref.as_mv, INT_MAX, 1, |
| 9832 | (MI_SIZE * mi_col), (MI_SIZE * mi_row), 1); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 9833 | #else |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9834 | int bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, |
| 9835 | sadpb, cond_cost_list(cpi, cost_list), |
| 9836 | &dv_ref.as_mv, INT_MAX, 1); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 9837 | #endif |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9838 | |
| 9839 | x->mv_limits = tmp_mv_limits; |
| 9840 | if (bestsme == INT_MAX) continue; |
| 9841 | mvp_full = x->best_mv.as_mv; |
| 9842 | MV dv = {.row = mvp_full.row * 8, .col = mvp_full.col * 8 }; |
| 9843 | if (mv_check_bounds(&x->mv_limits, &dv)) continue; |
| 9844 | if (!is_dv_valid(dv, tile, mi_row, mi_col, bsize)) continue; |
| 9845 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9846 | memset(&mbmi->palette_mode_info, 0, sizeof(mbmi->palette_mode_info)); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9847 | mbmi->use_intrabc = 1; |
| 9848 | mbmi->mode = DC_PRED; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 9849 | mbmi->uv_mode = UV_DC_PRED; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9850 | mbmi->mv[0].as_mv = dv; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9851 | #if CONFIG_DUAL_FILTER |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9852 | for (int idx = 0; idx < 4; ++idx) mbmi->interp_filter[idx] = BILINEAR; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9853 | #else |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9854 | mbmi->interp_filter = BILINEAR; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9855 | #endif |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9856 | mbmi->skip = 0; |
| 9857 | x->skip = 0; |
| 9858 | 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] | 9859 | |
Alex Converse | d5d9b6c | 2017-05-23 15:23:45 -0700 | [diff] [blame] | 9860 | assert(x->mvcost == x->mv_cost_stack[0]); |
| 9861 | // TODO(aconverse@google.com): The full motion field defining discount |
| 9862 | // in MV_COST_WEIGHT is too large. Explore other values. |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9863 | int rate_mv = av1_mv_bit_cost(&dv, &dv_ref.as_mv, x->nmvjointcost, |
Alex Converse | d5d9b6c | 2017-05-23 15:23:45 -0700 | [diff] [blame] | 9864 | x->mvcost, MV_COST_WEIGHT_SUB); |
Hui Su | 6c8584f | 2017-09-14 15:37:02 -0700 | [diff] [blame] | 9865 | const int rate_mode = x->intrabc_cost[1]; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9866 | RD_STATS rd_stats, rd_stats_uv; |
| 9867 | av1_subtract_plane(x, bsize, 0); |
| 9868 | super_block_yrd(cpi, x, &rd_stats, bsize, INT64_MAX); |
| 9869 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 9870 | av1_merge_rd_stats(&rd_stats, &rd_stats_uv); |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9871 | #if CONFIG_RD_DEBUG |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9872 | mbmi->rd_stats = rd_stats; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9873 | #endif |
| 9874 | |
Alex Converse | e16b266 | 2017-05-24 14:00:00 -0700 | [diff] [blame] | 9875 | #if CONFIG_VAR_TX |
| 9876 | // TODO(aconverse@google.com): Evaluate allowing VAR TX on intrabc blocks |
| 9877 | const int width = block_size_wide[bsize] >> tx_size_wide_log2[0]; |
| 9878 | const int height = block_size_high[bsize] >> tx_size_high_log2[0]; |
| 9879 | int idx, idy; |
| 9880 | for (idy = 0; idy < height; ++idy) |
| 9881 | for (idx = 0; idx < width; ++idx) |
| 9882 | mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size; |
| 9883 | mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size); |
| 9884 | #endif // CONFIG_VAR_TX |
| 9885 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9886 | const aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9887 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9888 | RD_STATS rdc_noskip; |
| 9889 | av1_init_rd_stats(&rdc_noskip); |
| 9890 | rdc_noskip.rate = |
| 9891 | rate_mode + rate_mv + rd_stats.rate + av1_cost_bit(skip_prob, 0); |
| 9892 | rdc_noskip.dist = rd_stats.dist; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9893 | rdc_noskip.rdcost = RDCOST(x->rdmult, rdc_noskip.rate, rdc_noskip.dist); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9894 | if (rdc_noskip.rdcost < best_rd) { |
| 9895 | best_rd = rdc_noskip.rdcost; |
| 9896 | best_mbmi = *mbmi; |
| 9897 | best_skip = x->skip; |
| 9898 | best_rdcost = rdc_noskip; |
| 9899 | } |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9900 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9901 | x->skip = 1; |
| 9902 | mbmi->skip = 1; |
| 9903 | RD_STATS rdc_skip; |
| 9904 | av1_init_rd_stats(&rdc_skip); |
| 9905 | rdc_skip.rate = rate_mode + rate_mv + av1_cost_bit(skip_prob, 1); |
| 9906 | rdc_skip.dist = rd_stats.sse; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9907 | rdc_skip.rdcost = RDCOST(x->rdmult, rdc_skip.rate, rdc_skip.dist); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9908 | if (rdc_skip.rdcost < best_rd) { |
| 9909 | best_rd = rdc_skip.rdcost; |
| 9910 | best_mbmi = *mbmi; |
| 9911 | best_skip = x->skip; |
| 9912 | best_rdcost = rdc_skip; |
| 9913 | } |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9914 | } |
| 9915 | *mbmi = best_mbmi; |
| 9916 | *rd_cost = best_rdcost; |
| 9917 | x->skip = best_skip; |
| 9918 | return best_rd; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9919 | } |
| 9920 | #endif // CONFIG_INTRABC |
| 9921 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9922 | 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] | 9923 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9924 | PICK_MODE_CONTEXT *ctx, int64_t best_rd) { |
| 9925 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9926 | MACROBLOCKD *const xd = &x->e_mbd; |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9927 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9928 | struct macroblockd_plane *const pd = xd->plane; |
| 9929 | int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0; |
| 9930 | int y_skip = 0, uv_skip = 0; |
| 9931 | int64_t dist_y = 0, dist_uv = 0; |
| 9932 | TX_SIZE max_uv_tx_size; |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 9933 | const int unify_bsize = CONFIG_CB4X4; |
| 9934 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9935 | ctx->skip = 0; |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9936 | mbmi->ref_frame[0] = INTRA_FRAME; |
| 9937 | mbmi->ref_frame[1] = NONE_FRAME; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9938 | #if CONFIG_INTRABC |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9939 | mbmi->use_intrabc = 0; |
| 9940 | mbmi->mv[0].as_int = 0; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9941 | #endif // CONFIG_INTRABC |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9942 | |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9943 | const int64_t intra_yrd = |
| 9944 | (bsize >= BLOCK_8X8 || unify_bsize) |
| 9945 | ? rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly, &dist_y, |
| 9946 | &y_skip, bsize, best_rd) |
| 9947 | : rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly, |
| 9948 | &dist_y, &y_skip, best_rd); |
| 9949 | |
| 9950 | if (intra_yrd < best_rd) { |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9951 | #if CONFIG_CFL |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 9952 | #if CONFIG_CB4X4 |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 9953 | // Only store reconstructed luma when there's chroma RDO. When there's no |
| 9954 | // chroma RDO, the reconstructed luma will be stored in encode_superblock(). |
| 9955 | xd->cfl->store_y = !x->skip_chroma_rd; |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 9956 | #else |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 9957 | xd->cfl->store_y = 1; |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 9958 | #endif // CONFIG_CB4X4 |
Luc Trudeau | fcca37a | 2017-08-14 15:05:07 -0400 | [diff] [blame] | 9959 | if (xd->cfl->store_y) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 9960 | // Perform one extra call to txfm_rd_in_plane(), with the values chosen |
| 9961 | // during luma RDO, so we can store reconstructed luma values |
| 9962 | RD_STATS this_rd_stats; |
Luc Trudeau | 32306c2 | 2017-08-14 14:44:26 -0400 | [diff] [blame] | 9963 | txfm_rd_in_plane(x, cpi, &this_rd_stats, INT64_MAX, AOM_PLANE_Y, |
| 9964 | mbmi->sb_type, mbmi->tx_size, |
| 9965 | cpi->sf.use_fast_coef_costing); |
Luc Trudeau | fcca37a | 2017-08-14 15:05:07 -0400 | [diff] [blame] | 9966 | xd->cfl->store_y = 0; |
Luc Trudeau | 32306c2 | 2017-08-14 14:44:26 -0400 | [diff] [blame] | 9967 | } |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 9968 | #endif // CONFIG_CFL |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9969 | max_uv_tx_size = uv_txsize_lookup[bsize][mbmi->tx_size][pd[1].subsampling_x] |
| 9970 | [pd[1].subsampling_y]; |
| 9971 | init_sbuv_mode(mbmi); |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 9972 | #if CONFIG_CB4X4 |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9973 | if (!x->skip_chroma_rd) |
| 9974 | rd_pick_intra_sbuv_mode(cpi, x, &rate_uv, &rate_uv_tokenonly, &dist_uv, |
| 9975 | &uv_skip, bsize, max_uv_tx_size); |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 9976 | #else |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9977 | rd_pick_intra_sbuv_mode(cpi, x, &rate_uv, &rate_uv_tokenonly, &dist_uv, |
| 9978 | &uv_skip, AOMMAX(BLOCK_8X8, bsize), max_uv_tx_size); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 9979 | #endif // CONFIG_CB4X4 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9980 | |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9981 | if (y_skip && uv_skip) { |
| 9982 | rd_cost->rate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly + |
| 9983 | av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 9984 | rd_cost->dist = dist_y + dist_uv; |
| 9985 | } else { |
| 9986 | rd_cost->rate = |
| 9987 | rate_y + rate_uv + av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 9988 | rd_cost->dist = dist_y + dist_uv; |
| 9989 | } |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9990 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9991 | } else { |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9992 | rd_cost->rate = INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9993 | } |
| 9994 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9995 | #if CONFIG_INTRABC |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9996 | if (rd_cost->rate != INT_MAX && rd_cost->rdcost < best_rd) |
| 9997 | best_rd = rd_cost->rdcost; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9998 | if (rd_pick_intrabc_mode_sb(cpi, x, rd_cost, bsize, best_rd) < best_rd) { |
| 9999 | 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] | 10000 | assert(rd_cost->rate != INT_MAX); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10001 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 10002 | } |
| 10003 | #endif |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 10004 | if (rd_cost->rate == INT_MAX) return; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 10005 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10006 | ctx->mic = *xd->mi[0]; |
| 10007 | ctx->mbmi_ext = *x->mbmi_ext; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10008 | } |
| 10009 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10010 | // Do we have an internal image edge (e.g. formatting bars). |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10011 | int av1_internal_image_edge(const AV1_COMP *cpi) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10012 | return (cpi->oxcf.pass == 2) && |
| 10013 | ((cpi->twopass.this_frame_stats.inactive_zone_rows > 0) || |
| 10014 | (cpi->twopass.this_frame_stats.inactive_zone_cols > 0)); |
| 10015 | } |
| 10016 | |
| 10017 | // Checks to see if a super block is on a horizontal image edge. |
| 10018 | // In most cases this is the "real" edge unless there are formatting |
| 10019 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10020 | 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] | 10021 | int top_edge = 0; |
| 10022 | int bottom_edge = cpi->common.mi_rows; |
| 10023 | int is_active_h_edge = 0; |
| 10024 | |
| 10025 | // For two pass account for any formatting bars detected. |
| 10026 | if (cpi->oxcf.pass == 2) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10027 | const TWO_PASS *const twopass = &cpi->twopass; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10028 | |
| 10029 | // The inactive region is specified in MBs not mi units. |
| 10030 | // The image edge is in the following MB row. |
| 10031 | top_edge += (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
| 10032 | |
| 10033 | bottom_edge -= (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10034 | bottom_edge = AOMMAX(top_edge, bottom_edge); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10035 | } |
| 10036 | |
| 10037 | if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) || |
| 10038 | ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) { |
| 10039 | is_active_h_edge = 1; |
| 10040 | } |
| 10041 | return is_active_h_edge; |
| 10042 | } |
| 10043 | |
| 10044 | // Checks to see if a super block is on a vertical image edge. |
| 10045 | // In most cases this is the "real" edge unless there are formatting |
| 10046 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10047 | 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] | 10048 | int left_edge = 0; |
| 10049 | int right_edge = cpi->common.mi_cols; |
| 10050 | int is_active_v_edge = 0; |
| 10051 | |
| 10052 | // For two pass account for any formatting bars detected. |
| 10053 | if (cpi->oxcf.pass == 2) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10054 | const TWO_PASS *const twopass = &cpi->twopass; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10055 | |
| 10056 | // The inactive region is specified in MBs not mi units. |
| 10057 | // The image edge is in the following MB row. |
| 10058 | left_edge += (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
| 10059 | |
| 10060 | right_edge -= (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10061 | right_edge = AOMMAX(left_edge, right_edge); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10062 | } |
| 10063 | |
| 10064 | if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) || |
| 10065 | ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) { |
| 10066 | is_active_v_edge = 1; |
| 10067 | } |
| 10068 | return is_active_v_edge; |
| 10069 | } |
| 10070 | |
| 10071 | // Checks to see if a super block is at the edge of the active image. |
| 10072 | // In most cases this is the "real" edge unless there are formatting |
| 10073 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10074 | 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] | 10075 | return av1_active_h_edge(cpi, mi_row, cpi->common.mib_size) || |
| 10076 | av1_active_v_edge(cpi, mi_col, cpi->common.mib_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10077 | } |
| 10078 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10079 | 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] | 10080 | MACROBLOCKD *const xd = &x->e_mbd; |
| 10081 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 10082 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 10083 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 10084 | assert(bsize >= BLOCK_8X8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10085 | int src_stride = x->plane[1].src.stride; |
| 10086 | const uint8_t *const src_u = x->plane[1].src.buf; |
| 10087 | const uint8_t *const src_v = x->plane[2].src.buf; |
| 10088 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 10089 | float centroids[2 * PALETTE_MAX_SIZE]; |
| 10090 | uint8_t *const color_map = xd->plane[1].color_index_map; |
| 10091 | int r, c; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10092 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10093 | const uint16_t *const src_u16 = CONVERT_TO_SHORTPTR(src_u); |
| 10094 | const uint16_t *const src_v16 = CONVERT_TO_SHORTPTR(src_v); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10095 | #endif // CONFIG_HIGHBITDEPTH |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 10096 | int plane_block_width, plane_block_height, rows, cols; |
| 10097 | av1_get_block_dimensions(bsize, 1, xd, &plane_block_width, |
| 10098 | &plane_block_height, &rows, &cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10099 | (void)cpi; |
| 10100 | |
| 10101 | for (r = 0; r < rows; ++r) { |
| 10102 | for (c = 0; c < cols; ++c) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10103 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10104 | if (cpi->common.use_highbitdepth) { |
| 10105 | data[(r * cols + c) * 2] = src_u16[r * src_stride + c]; |
| 10106 | data[(r * cols + c) * 2 + 1] = src_v16[r * src_stride + c]; |
| 10107 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10108 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10109 | data[(r * cols + c) * 2] = src_u[r * src_stride + c]; |
| 10110 | data[(r * cols + c) * 2 + 1] = src_v[r * src_stride + c]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10111 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10112 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10113 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10114 | } |
| 10115 | } |
| 10116 | |
| 10117 | for (r = 1; r < 3; ++r) { |
| 10118 | for (c = 0; c < pmi->palette_size[1]; ++c) { |
| 10119 | centroids[c * 2 + r - 1] = pmi->palette_colors[r * PALETTE_MAX_SIZE + c]; |
| 10120 | } |
| 10121 | } |
| 10122 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10123 | av1_calc_indices(data, centroids, color_map, rows * cols, |
| 10124 | pmi->palette_size[1], 2); |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 10125 | extend_palette_color_map(color_map, cols, rows, plane_block_width, |
| 10126 | plane_block_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10127 | } |
| 10128 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10129 | #if CONFIG_FILTER_INTRA |
| 10130 | static void pick_filter_intra_interframe( |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 10131 | const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row, |
| 10132 | int mi_col, int *rate_uv_intra, int *rate_uv_tokenonly, int64_t *dist_uv, |
| 10133 | int *skip_uv, UV_PREDICTION_MODE *mode_uv, |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10134 | FILTER_INTRA_MODE_INFO *filter_intra_mode_info_uv, |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10135 | #if CONFIG_EXT_INTRA |
| 10136 | int8_t *uv_angle_delta, |
| 10137 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | c6300aa | 2017-06-01 14:46:23 -0700 | [diff] [blame] | 10138 | PALETTE_MODE_INFO *pmi_uv, int palette_ctx, int skip_mask, |
| 10139 | unsigned int *ref_costs_single, int64_t *best_rd, int64_t *best_intra_rd, |
| 10140 | PREDICTION_MODE *best_intra_mode, int *best_mode_index, int *best_skip2, |
| 10141 | int *best_mode_skippable, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10142 | #if CONFIG_SUPERTX |
| 10143 | int *returnrate_nocoef, |
| 10144 | #endif // CONFIG_SUPERTX |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 10145 | int64_t *best_pred_rd, MB_MODE_INFO *best_mbmode, RD_STATS *rd_cost) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10146 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10147 | MACROBLOCKD *const xd = &x->e_mbd; |
| 10148 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 10149 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 10150 | const int try_palette = can_use_palette(cpi, mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10151 | int rate2 = 0, rate_y = INT_MAX, skippable = 0, rate_uv, rate_dummy, i; |
| 10152 | int dc_mode_index; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10153 | const int *const intra_mode_cost = x->mbmode_cost[size_group_lookup[bsize]]; |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 10154 | int64_t distortion2 = 0, distortion_y = 0, this_rd = *best_rd; |
| 10155 | int64_t distortion_uv, model_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10156 | TX_SIZE uv_tx; |
| 10157 | |
| 10158 | for (i = 0; i < MAX_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10159 | if (av1_mode_order[i].mode == DC_PRED && |
| 10160 | av1_mode_order[i].ref_frame[0] == INTRA_FRAME) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10161 | break; |
| 10162 | dc_mode_index = i; |
| 10163 | assert(i < MAX_MODES); |
| 10164 | |
| 10165 | // TODO(huisu): use skip_mask for further speedup. |
| 10166 | (void)skip_mask; |
| 10167 | mbmi->mode = DC_PRED; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10168 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10169 | mbmi->ref_frame[0] = INTRA_FRAME; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 10170 | mbmi->ref_frame[1] = NONE_FRAME; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10171 | if (!rd_pick_filter_intra_sby(cpi, x, &rate_dummy, &rate_y, &distortion_y, |
| 10172 | &skippable, bsize, intra_mode_cost[mbmi->mode], |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 10173 | &this_rd, &model_rd, 0)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10174 | return; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10175 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10176 | if (rate_y == INT_MAX) return; |
| 10177 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 10178 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x] |
| 10179 | [xd->plane[1].subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10180 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 10181 | choose_intra_uv_mode(cpi, x, bsize, uv_tx, &rate_uv_intra[uv_tx], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10182 | &rate_uv_tokenonly[uv_tx], &dist_uv[uv_tx], |
| 10183 | &skip_uv[uv_tx], &mode_uv[uv_tx]); |
| 10184 | if (cm->allow_screen_content_tools) pmi_uv[uv_tx] = *pmi; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10185 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 10186 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10187 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10188 | #endif // CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10189 | } |
| 10190 | |
| 10191 | rate_uv = rate_uv_tokenonly[uv_tx]; |
| 10192 | distortion_uv = dist_uv[uv_tx]; |
| 10193 | skippable = skippable && skip_uv[uv_tx]; |
| 10194 | mbmi->uv_mode = mode_uv[uv_tx]; |
| 10195 | if (cm->allow_screen_content_tools) { |
| 10196 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
| 10197 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 10198 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 10199 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
| 10200 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10201 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10202 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10203 | #endif // CONFIG_EXT_INTRA |
| 10204 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 10205 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 10206 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 10207 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 10208 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10209 | } |
| 10210 | |
| 10211 | rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10212 | x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode]; |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 10213 | if (try_palette && mbmi->mode == DC_PRED) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10214 | rate2 += av1_cost_bit( |
| 10215 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10216 | |
| 10217 | if (!xd->lossless[mbmi->segment_id]) { |
| 10218 | // super_block_yrd above includes the cost of the tx_size in the |
| 10219 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 10220 | // (prediction granularity), so we account for it in the full rate, |
| 10221 | // not the tokenonly rate. |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10222 | rate_y -= tx_size_cost(cpi, x, bsize, mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10223 | } |
| 10224 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10225 | rate2 += av1_cost_bit(cm->fc->filter_intra_probs[0], |
| 10226 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0]); |
| 10227 | rate2 += write_uniform_cost( |
| 10228 | FILTER_INTRA_MODES, mbmi->filter_intra_mode_info.filter_intra_mode[0]); |
| 10229 | #if CONFIG_EXT_INTRA |
Luc Trudeau | f24a35f | 2017-09-11 11:56:33 -0400 | [diff] [blame] | 10230 | if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize) && |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 10231 | av1_use_angle_delta(bsize)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 10232 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 10233 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10234 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10235 | #endif // CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10236 | if (mbmi->mode == DC_PRED) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10237 | rate2 += |
| 10238 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], |
| 10239 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1]); |
| 10240 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1]) |
| 10241 | rate2 += |
| 10242 | write_uniform_cost(FILTER_INTRA_MODES, |
| 10243 | mbmi->filter_intra_mode_info.filter_intra_mode[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10244 | } |
| 10245 | distortion2 = distortion_y + distortion_uv; |
Jingning Han | 18c53c8 | 2017-02-17 14:49:57 -0800 | [diff] [blame] | 10246 | av1_encode_intra_block_plane((AV1_COMMON *)cm, x, bsize, 0, 0, mi_row, |
| 10247 | mi_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10248 | |
| 10249 | rate2 += ref_costs_single[INTRA_FRAME]; |
| 10250 | |
| 10251 | if (skippable) { |
| 10252 | rate2 -= (rate_y + rate_uv); |
| 10253 | rate_y = 0; |
| 10254 | rate_uv = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10255 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10256 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10257 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10258 | } |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10259 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10260 | |
| 10261 | if (this_rd < *best_intra_rd) { |
| 10262 | *best_intra_rd = this_rd; |
| 10263 | *best_intra_mode = mbmi->mode; |
| 10264 | } |
| 10265 | for (i = 0; i < REFERENCE_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10266 | best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10267 | |
| 10268 | if (this_rd < *best_rd) { |
| 10269 | *best_mode_index = dc_mode_index; |
| 10270 | mbmi->mv[0].as_int = 0; |
| 10271 | rd_cost->rate = rate2; |
| 10272 | #if CONFIG_SUPERTX |
| 10273 | if (x->skip) |
| 10274 | *returnrate_nocoef = rate2; |
| 10275 | else |
| 10276 | *returnrate_nocoef = rate2 - rate_y - rate_uv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10277 | *returnrate_nocoef -= av1_cost_bit(av1_get_skip_prob(cm, xd), skippable); |
| 10278 | *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd), |
| 10279 | mbmi->ref_frame[0] != INTRA_FRAME); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10280 | #endif // CONFIG_SUPERTX |
| 10281 | rd_cost->dist = distortion2; |
| 10282 | rd_cost->rdcost = this_rd; |
| 10283 | *best_rd = this_rd; |
| 10284 | *best_mbmode = *mbmi; |
| 10285 | *best_skip2 = 0; |
| 10286 | *best_mode_skippable = skippable; |
| 10287 | } |
| 10288 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10289 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10290 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10291 | #if CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10292 | static void calc_target_weighted_pred(const AV1_COMMON *cm, const MACROBLOCK *x, |
| 10293 | const MACROBLOCKD *xd, int mi_row, |
| 10294 | int mi_col, const uint8_t *above, |
| 10295 | int above_stride, const uint8_t *left, |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 10296 | int left_stride); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10297 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10298 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10299 | 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] | 10300 | MACROBLOCK *x, int mi_row, int mi_col, |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 10301 | RD_STATS *rd_cost, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10302 | #if CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10303 | int *returnrate_nocoef, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10304 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10305 | BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, |
| 10306 | int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 10307 | const AV1_COMMON *const cm = &cpi->common; |
| 10308 | const RD_OPT *const rd_opt = &cpi->rd; |
| 10309 | const SPEED_FEATURES *const sf = &cpi->sf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10310 | MACROBLOCKD *const xd = &x->e_mbd; |
| 10311 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 10312 | const int try_palette = can_use_palette(cpi, mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10313 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 10314 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 10315 | const struct segmentation *const seg = &cm->seg; |
| 10316 | PREDICTION_MODE this_mode; |
| 10317 | MV_REFERENCE_FRAME ref_frame, second_ref_frame; |
| 10318 | unsigned char segment_id = mbmi->segment_id; |
| 10319 | int comp_pred, i, k; |
| 10320 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10321 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 10322 | int_mv frame_comp_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
| 10323 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10324 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10325 | int_mv single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } }; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10326 | #if CONFIG_EXT_INTER |
| 10327 | int single_newmv_rate[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 10328 | int64_t modelled_rd[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10329 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10330 | static const int flag_list[TOTAL_REFS_PER_FRAME] = { |
| 10331 | 0, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10332 | AOM_LAST_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10333 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10334 | AOM_LAST2_FLAG, |
| 10335 | AOM_LAST3_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10336 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10337 | AOM_GOLD_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10338 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10339 | AOM_BWD_FLAG, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 10340 | AOM_ALT2_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10341 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10342 | AOM_ALT_FLAG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10343 | }; |
| 10344 | int64_t best_rd = best_rd_so_far; |
| 10345 | int best_rate_y = INT_MAX, best_rate_uv = INT_MAX; |
| 10346 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 10347 | int64_t best_pred_rd[REFERENCE_MODES]; |
| 10348 | MB_MODE_INFO best_mbmode; |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 10349 | int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 10350 | int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10351 | int best_mode_skippable = 0; |
| 10352 | int midx, best_mode_index = -1; |
| 10353 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10354 | #if CONFIG_EXT_COMP_REFS |
| 10355 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME][TOTAL_REFS_PER_FRAME]; |
| 10356 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10357 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10358 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10359 | aom_prob comp_mode_p; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10360 | int64_t best_intra_rd = INT64_MAX; |
| 10361 | unsigned int best_pred_sse = UINT_MAX; |
| 10362 | PREDICTION_MODE best_intra_mode = DC_PRED; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10363 | int rate_uv_intra[TX_SIZES_ALL], rate_uv_tokenonly[TX_SIZES_ALL]; |
| 10364 | int64_t dist_uvs[TX_SIZES_ALL]; |
| 10365 | int skip_uvs[TX_SIZES_ALL]; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10366 | UV_PREDICTION_MODE mode_uv[TX_SIZES_ALL]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10367 | PALETTE_MODE_INFO pmi_uv[TX_SIZES_ALL]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10368 | #if CONFIG_EXT_INTRA |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10369 | int8_t uv_angle_delta[TX_SIZES_ALL]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10370 | int is_directional_mode, angle_stats_ready = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10371 | uint8_t directional_mode_skip_mask[INTRA_MODES]; |
| 10372 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10373 | #if CONFIG_FILTER_INTRA |
| 10374 | int8_t dc_skipped = 1; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10375 | FILTER_INTRA_MODE_INFO filter_intra_mode_info_uv[TX_SIZES_ALL]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10376 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10377 | const int intra_cost_penalty = av1_get_intra_cost_penalty( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10378 | cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10379 | 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] | 10380 | int best_skip2 = 0; |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 10381 | uint16_t ref_frame_skip_mask[2] = { 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10382 | uint32_t mode_skip_mask[TOTAL_REFS_PER_FRAME] = { 0 }; |
Yue Chen | 4d26acb | 2017-05-01 12:28:34 -0700 | [diff] [blame] | 10383 | #if CONFIG_EXT_INTER && CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10384 | MV_REFERENCE_FRAME best_single_inter_ref = LAST_FRAME; |
| 10385 | int64_t best_single_inter_rd = INT64_MAX; |
Yue Chen | 4d26acb | 2017-05-01 12:28:34 -0700 | [diff] [blame] | 10386 | #endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10387 | int mode_skip_start = sf->mode_skip_start + 1; |
| 10388 | const int *const rd_threshes = rd_opt->threshes[segment_id][bsize]; |
| 10389 | const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize]; |
| 10390 | int64_t mode_threshold[MAX_MODES]; |
| 10391 | int *mode_map = tile_data->mode_map[bsize]; |
| 10392 | const int mode_search_skip_flags = sf->mode_search_skip_flags; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10393 | #if CONFIG_PVQ |
| 10394 | od_rollback_buffer pre_buf; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10395 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10396 | |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10397 | HandleInterModeArgs args = { |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10398 | #if CONFIG_MOTION_VAR |
| 10399 | { NULL }, |
| 10400 | { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }, |
| 10401 | { NULL }, |
| 10402 | { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }, |
| 10403 | #endif // CONFIG_MOTION_VAR |
| 10404 | #if CONFIG_EXT_INTER |
| 10405 | NULL, |
| 10406 | NULL, |
| 10407 | NULL, |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10408 | #else // CONFIG_EXT_INTER |
| 10409 | NULL, |
| 10410 | #endif // CONFIG_EXT_INTER |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 10411 | { { 0 } }, |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10412 | }; |
| 10413 | |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 10414 | const int rows = block_size_high[bsize]; |
| 10415 | const int cols = block_size_wide[bsize]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10416 | int palette_ctx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10417 | const MODE_INFO *above_mi = xd->above_mi; |
| 10418 | const MODE_INFO *left_mi = xd->left_mi; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10419 | #if CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10420 | int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 10421 | int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 10422 | int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 10423 | 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] | 10424 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10425 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10426 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 10427 | int len = sizeof(uint16_t); |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 10428 | args.above_pred_buf[0] = CONVERT_TO_BYTEPTR(x->above_pred_buf); |
| 10429 | args.above_pred_buf[1] = |
| 10430 | CONVERT_TO_BYTEPTR(x->above_pred_buf + MAX_SB_SQUARE * len); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10431 | args.above_pred_buf[2] = |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 10432 | CONVERT_TO_BYTEPTR(x->above_pred_buf + 2 * MAX_SB_SQUARE * len); |
| 10433 | args.left_pred_buf[0] = CONVERT_TO_BYTEPTR(x->left_pred_buf); |
| 10434 | args.left_pred_buf[1] = |
| 10435 | CONVERT_TO_BYTEPTR(x->left_pred_buf + MAX_SB_SQUARE * len); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10436 | args.left_pred_buf[2] = |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 10437 | CONVERT_TO_BYTEPTR(x->left_pred_buf + 2 * MAX_SB_SQUARE * len); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10438 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10439 | #endif // CONFIG_HIGHBITDEPTH |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 10440 | args.above_pred_buf[0] = x->above_pred_buf; |
| 10441 | args.above_pred_buf[1] = x->above_pred_buf + MAX_SB_SQUARE; |
| 10442 | args.above_pred_buf[2] = x->above_pred_buf + 2 * MAX_SB_SQUARE; |
| 10443 | args.left_pred_buf[0] = x->left_pred_buf; |
| 10444 | args.left_pred_buf[1] = x->left_pred_buf + MAX_SB_SQUARE; |
| 10445 | 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] | 10446 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10447 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10448 | #endif // CONFIG_HIGHBITDEPTH |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10449 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10450 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10451 | av1_zero(best_mbmode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10452 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10453 | av1_zero(pmi_uv); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 10454 | if (try_palette) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10455 | if (above_mi) |
| 10456 | palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 10457 | if (left_mi) |
| 10458 | palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 10459 | } |
| 10460 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10461 | estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp, |
| 10462 | &comp_mode_p); |
| 10463 | |
| 10464 | 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] | 10465 | 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] | 10466 | for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX; |
| 10467 | for (i = 0; i < MB_MODE_COUNT; ++i) { |
| 10468 | for (k = 0; k < TOTAL_REFS_PER_FRAME; ++k) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10469 | args.single_filter[i][k] = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10470 | } |
| 10471 | } |
| 10472 | |
| 10473 | rd_cost->rate = INT_MAX; |
| 10474 | #if CONFIG_SUPERTX |
| 10475 | *returnrate_nocoef = INT_MAX; |
| 10476 | #endif // CONFIG_SUPERTX |
| 10477 | |
| 10478 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 10479 | x->pred_mv_sad[ref_frame] = INT_MAX; |
| 10480 | x->mbmi_ext->mode_context[ref_frame] = 0; |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 10481 | #if CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10482 | x->mbmi_ext->compound_mode_context[ref_frame] = 0; |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 10483 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10484 | if (cpi->ref_frame_flags & flag_list[ref_frame]) { |
| 10485 | assert(get_ref_frame_buffer(cpi, ref_frame) != NULL); |
| 10486 | setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col, |
| 10487 | frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb); |
| 10488 | } |
| 10489 | frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10490 | #if CONFIG_GLOBAL_MOTION |
| 10491 | frame_mv[ZEROMV][ref_frame].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 10492 | gm_get_motion_vector(&cm->global_motion[ref_frame], |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 10493 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10494 | 0 |
| 10495 | #if CONFIG_AMVR |
| 10496 | , |
| 10497 | cm->cur_frame_mv_precision_level |
| 10498 | #endif |
| 10499 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10500 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10501 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10502 | frame_mv[ZEROMV][ref_frame].as_int = 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10503 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10504 | #if CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10505 | frame_mv[NEW_NEWMV][ref_frame].as_int = INVALID_MV; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10506 | #if CONFIG_COMPOUND_SINGLEREF |
| 10507 | frame_mv[SR_NEW_NEWMV][ref_frame].as_int = INVALID_MV; |
| 10508 | frame_comp_mv[SR_NEW_NEWMV][ref_frame].as_int = INVALID_MV; |
| 10509 | #endif // CONFIG_COMPOUND_SINGLEREF |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 10510 | #if CONFIG_GLOBAL_MOTION |
| 10511 | frame_mv[ZERO_ZEROMV][ref_frame].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 10512 | gm_get_motion_vector(&cm->global_motion[ref_frame], |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 10513 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10514 | 0 |
| 10515 | #if CONFIG_AMVR |
| 10516 | , |
| 10517 | cm->cur_frame_mv_precision_level |
| 10518 | #endif |
| 10519 | ) |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 10520 | .as_int; |
| 10521 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10522 | frame_mv[ZERO_ZEROMV][ref_frame].as_int = 0; |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 10523 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10524 | #endif // CONFIG_EXT_INTER |
| 10525 | } |
| 10526 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10527 | for (; ref_frame < MODE_CTX_REF_FRAMES; ++ref_frame) { |
| 10528 | MODE_INFO *const mi = xd->mi[0]; |
| 10529 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 10530 | x->mbmi_ext->mode_context[ref_frame] = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10531 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 10532 | mbmi_ext->ref_mv_stack[ref_frame], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10533 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10534 | mbmi_ext->compound_mode_context, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10535 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10536 | candidates, mi_row, mi_col, NULL, NULL, |
| 10537 | mbmi_ext->mode_context); |
Jingning Han | 731af49 | 2016-11-17 11:53:23 -0800 | [diff] [blame] | 10538 | if (mbmi_ext->ref_mv_count[ref_frame] < 2) { |
| 10539 | MV_REFERENCE_FRAME rf[2]; |
| 10540 | av1_set_ref_frame(rf, ref_frame); |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10541 | if (mbmi_ext->ref_mvs[rf[0]][0].as_int != |
| 10542 | frame_mv[ZEROMV][rf[0]].as_int || |
| 10543 | mbmi_ext->ref_mvs[rf[0]][1].as_int != |
| 10544 | frame_mv[ZEROMV][rf[0]].as_int || |
| 10545 | mbmi_ext->ref_mvs[rf[1]][0].as_int != |
| 10546 | frame_mv[ZEROMV][rf[1]].as_int || |
| 10547 | mbmi_ext->ref_mvs[rf[1]][1].as_int != frame_mv[ZEROMV][rf[1]].as_int) |
Jingning Han | 731af49 | 2016-11-17 11:53:23 -0800 | [diff] [blame] | 10548 | mbmi_ext->mode_context[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET); |
| 10549 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10550 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10551 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10552 | #if CONFIG_MOTION_VAR |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 10553 | av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col); |
Jingning Han | ad586b9 | 2017-05-23 10:24:57 -0700 | [diff] [blame] | 10554 | |
Yue Chen | f7ba647 | 2017-04-19 11:08:58 -0700 | [diff] [blame] | 10555 | if (check_num_overlappable_neighbors(mbmi) && |
| 10556 | is_motion_variation_allowed_bsize(bsize)) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10557 | av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col, |
| 10558 | args.above_pred_buf, dst_width1, |
| 10559 | dst_height1, args.above_pred_stride); |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 10560 | av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10561 | args.left_pred_buf, dst_width2, |
| 10562 | dst_height2, args.left_pred_stride); |
Jingning Han | 91d9a79 | 2017-04-18 12:01:52 -0700 | [diff] [blame] | 10563 | av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row, |
| 10564 | mi_col); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10565 | calc_target_weighted_pred(cm, x, xd, mi_row, mi_col, args.above_pred_buf[0], |
| 10566 | args.above_pred_stride[0], args.left_pred_buf[0], |
| 10567 | args.left_pred_stride[0]); |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 10568 | } |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10569 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10570 | |
| 10571 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 10572 | if (!(cpi->ref_frame_flags & flag_list[ref_frame])) { |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 10573 | // Skip checking missing references in both single and compound reference |
| 10574 | // modes. Note that a mode will be skipped iff both reference frames |
| 10575 | // are masked out. |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10576 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 10577 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10578 | } else { |
| 10579 | for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) { |
| 10580 | // Skip fixed mv modes for poor references |
| 10581 | if ((x->pred_mv_sad[ref_frame] >> 2) > x->pred_mv_sad[i]) { |
| 10582 | mode_skip_mask[ref_frame] |= INTER_NEAREST_NEAR_ZERO; |
| 10583 | break; |
| 10584 | } |
| 10585 | } |
| 10586 | } |
| 10587 | // If the segment reference frame feature is enabled.... |
| 10588 | // then do nothing if the current ref frame is not allowed.. |
| 10589 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && |
| 10590 | get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) { |
| 10591 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 10592 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10593 | } |
| 10594 | } |
| 10595 | |
| 10596 | // Disable this drop out case if the ref frame |
| 10597 | // segment level feature is enabled for this segment. This is to |
| 10598 | // prevent the possibility that we end up unable to pick any mode. |
| 10599 | if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) { |
| 10600 | // Only consider ZEROMV/ALTREF_FRAME for alt ref frame, |
| 10601 | // unless ARNR filtering is enabled in which case we want |
| 10602 | // an unfiltered alternative. We allow near/nearest as well |
| 10603 | // because they may result in zero-zero MVs but be cheaper. |
| 10604 | 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] | 10605 | int_mv zeromv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10606 | ref_frame_skip_mask[0] = (1 << LAST_FRAME) | |
| 10607 | #if CONFIG_EXT_REFS |
| 10608 | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 10609 | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10610 | #endif // CONFIG_EXT_REFS |
| 10611 | (1 << GOLDEN_FRAME); |
| 10612 | ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK; |
| 10613 | // TODO(zoeliu): To further explore whether following needs to be done for |
| 10614 | // BWDREF_FRAME as well. |
| 10615 | mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10616 | #if CONFIG_GLOBAL_MOTION |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10617 | zeromv.as_int = gm_get_motion_vector(&cm->global_motion[ALTREF_FRAME], |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 10618 | cm->allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10619 | mi_col, mi_row, 0 |
| 10620 | #if CONFIG_AMVR |
| 10621 | , |
| 10622 | cm->cur_frame_mv_precision_level |
| 10623 | #endif |
| 10624 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10625 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10626 | #else |
| 10627 | zeromv.as_int = 0; |
| 10628 | #endif // CONFIG_GLOBAL_MOTION |
| 10629 | if (frame_mv[NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10630 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEARMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10631 | if (frame_mv[NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10632 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEARESTMV); |
| 10633 | #if CONFIG_EXT_INTER |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10634 | if (frame_mv[NEAREST_NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10635 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAREST_NEARESTMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10636 | if (frame_mv[NEAR_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10637 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAR_NEARMV); |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10638 | #if CONFIG_COMPOUND_SINGLEREF |
| 10639 | if (frame_mv[SR_NEAREST_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int || |
| 10640 | frame_comp_mv[SR_NEAREST_NEARMV][ALTREF_FRAME].as_int != |
| 10641 | zeromv.as_int) |
| 10642 | mode_skip_mask[ALTREF_FRAME] |= (1 << SR_NEAREST_NEARMV); |
| 10643 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10644 | #endif // CONFIG_EXT_INTER |
| 10645 | } |
| 10646 | } |
| 10647 | |
| 10648 | if (cpi->rc.is_src_frame_alt_ref) { |
| 10649 | if (sf->alt_ref_search_fp) { |
| 10650 | assert(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]); |
| 10651 | mode_skip_mask[ALTREF_FRAME] = 0; |
| 10652 | ref_frame_skip_mask[0] = ~(1 << ALTREF_FRAME); |
| 10653 | ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK; |
| 10654 | } |
| 10655 | } |
| 10656 | |
| 10657 | if (sf->alt_ref_search_fp) |
| 10658 | if (!cm->show_frame && x->pred_mv_sad[GOLDEN_FRAME] < INT_MAX) |
| 10659 | if (x->pred_mv_sad[ALTREF_FRAME] > (x->pred_mv_sad[GOLDEN_FRAME] << 1)) |
| 10660 | mode_skip_mask[ALTREF_FRAME] |= INTER_ALL; |
| 10661 | |
| 10662 | if (sf->adaptive_mode_search) { |
| 10663 | if (cm->show_frame && !cpi->rc.is_src_frame_alt_ref && |
| 10664 | cpi->rc.frames_since_golden >= 3) |
Yaowu Xu | 36bad47 | 2017-05-16 18:29:53 -0700 | [diff] [blame] | 10665 | 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] | 10666 | mode_skip_mask[GOLDEN_FRAME] |= INTER_ALL; |
| 10667 | } |
| 10668 | |
| 10669 | if (bsize > sf->max_intra_bsize) { |
| 10670 | ref_frame_skip_mask[0] |= (1 << INTRA_FRAME); |
| 10671 | ref_frame_skip_mask[1] |= (1 << INTRA_FRAME); |
| 10672 | } |
| 10673 | |
| 10674 | mode_skip_mask[INTRA_FRAME] |= |
| 10675 | ~(sf->intra_y_mode_mask[max_txsize_lookup[bsize]]); |
| 10676 | |
| 10677 | for (i = 0; i <= LAST_NEW_MV_INDEX; ++i) mode_threshold[i] = 0; |
| 10678 | for (i = LAST_NEW_MV_INDEX + 1; i < MAX_MODES; ++i) |
| 10679 | mode_threshold[i] = ((int64_t)rd_threshes[i] * rd_thresh_freq_fact[i]) >> 5; |
| 10680 | |
| 10681 | midx = sf->schedule_mode_search ? mode_skip_start : 0; |
| 10682 | while (midx > 4) { |
| 10683 | uint8_t end_pos = 0; |
| 10684 | for (i = 5; i < midx; ++i) { |
| 10685 | if (mode_threshold[mode_map[i - 1]] > mode_threshold[mode_map[i]]) { |
| 10686 | uint8_t tmp = mode_map[i]; |
| 10687 | mode_map[i] = mode_map[i - 1]; |
| 10688 | mode_map[i - 1] = tmp; |
| 10689 | end_pos = i; |
| 10690 | } |
| 10691 | } |
| 10692 | midx = end_pos; |
| 10693 | } |
| 10694 | |
| 10695 | if (cpi->sf.tx_type_search.fast_intra_tx_type_search) |
| 10696 | x->use_default_intra_tx_type = 1; |
| 10697 | else |
| 10698 | x->use_default_intra_tx_type = 0; |
| 10699 | |
| 10700 | if (cpi->sf.tx_type_search.fast_inter_tx_type_search) |
| 10701 | x->use_default_inter_tx_type = 1; |
| 10702 | else |
| 10703 | x->use_default_inter_tx_type = 0; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10704 | #if CONFIG_PVQ |
| 10705 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10706 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10707 | #if CONFIG_EXT_INTER |
| 10708 | for (i = 0; i < MB_MODE_COUNT; ++i) |
| 10709 | for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame) |
| 10710 | modelled_rd[i][ref_frame] = INT64_MAX; |
| 10711 | #endif // CONFIG_EXT_INTER |
| 10712 | |
| 10713 | for (midx = 0; midx < MAX_MODES; ++midx) { |
| 10714 | int mode_index; |
| 10715 | int mode_excluded = 0; |
| 10716 | int64_t this_rd = INT64_MAX; |
| 10717 | int disable_skip = 0; |
| 10718 | int compmode_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10719 | int rate2 = 0, rate_y = 0, rate_uv = 0; |
| 10720 | int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0; |
| 10721 | int skippable = 0; |
| 10722 | int this_skip2 = 0; |
| 10723 | int64_t total_sse = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10724 | uint8_t ref_frame_type; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10725 | #if CONFIG_PVQ |
| 10726 | od_encode_rollback(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10727 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10728 | mode_index = mode_map[midx]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10729 | this_mode = av1_mode_order[mode_index].mode; |
| 10730 | ref_frame = av1_mode_order[mode_index].ref_frame[0]; |
| 10731 | second_ref_frame = av1_mode_order[mode_index].ref_frame[1]; |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 10732 | mbmi->ref_mv_idx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10733 | |
| 10734 | #if CONFIG_EXT_INTER |
| 10735 | if (ref_frame > INTRA_FRAME && second_ref_frame == INTRA_FRAME) { |
| 10736 | // Mode must by compatible |
Debargha Mukherjee | 37f6fe6 | 2017-02-10 21:44:13 -0800 | [diff] [blame] | 10737 | if (!is_interintra_allowed_mode(this_mode)) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10738 | if (!is_interintra_allowed_bsize(bsize)) continue; |
| 10739 | } |
| 10740 | |
| 10741 | if (is_inter_compound_mode(this_mode)) { |
| 10742 | frame_mv[this_mode][ref_frame].as_int = |
| 10743 | frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int; |
| 10744 | frame_mv[this_mode][second_ref_frame].as_int = |
| 10745 | frame_mv[compound_ref1_mode(this_mode)][second_ref_frame].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10746 | #if CONFIG_COMPOUND_SINGLEREF |
| 10747 | } else if (is_inter_singleref_comp_mode(this_mode)) { |
| 10748 | frame_mv[this_mode][ref_frame].as_int = |
| 10749 | frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int; |
| 10750 | frame_comp_mv[this_mode][ref_frame].as_int = |
| 10751 | frame_mv[compound_ref1_mode(this_mode)][ref_frame].as_int; |
| 10752 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10753 | } |
| 10754 | #endif // CONFIG_EXT_INTER |
| 10755 | |
| 10756 | // Look at the reference frame of the best mode so far and set the |
| 10757 | // skip mask to look at a subset of the remaining modes. |
| 10758 | if (midx == mode_skip_start && best_mode_index >= 0) { |
| 10759 | switch (best_mbmode.ref_frame[0]) { |
| 10760 | case INTRA_FRAME: break; |
| 10761 | case LAST_FRAME: |
| 10762 | ref_frame_skip_mask[0] |= LAST_FRAME_MODE_MASK; |
| 10763 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10764 | break; |
| 10765 | #if CONFIG_EXT_REFS |
| 10766 | case LAST2_FRAME: |
| 10767 | ref_frame_skip_mask[0] |= LAST2_FRAME_MODE_MASK; |
| 10768 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10769 | break; |
| 10770 | case LAST3_FRAME: |
| 10771 | ref_frame_skip_mask[0] |= LAST3_FRAME_MODE_MASK; |
| 10772 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10773 | break; |
| 10774 | #endif // CONFIG_EXT_REFS |
| 10775 | case GOLDEN_FRAME: |
| 10776 | ref_frame_skip_mask[0] |= GOLDEN_FRAME_MODE_MASK; |
| 10777 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10778 | break; |
| 10779 | #if CONFIG_EXT_REFS |
| 10780 | case BWDREF_FRAME: |
| 10781 | ref_frame_skip_mask[0] |= BWDREF_FRAME_MODE_MASK; |
| 10782 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10783 | break; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 10784 | case ALTREF2_FRAME: |
| 10785 | ref_frame_skip_mask[0] |= ALTREF2_FRAME_MODE_MASK; |
| 10786 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10787 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10788 | #endif // CONFIG_EXT_REFS |
| 10789 | case ALTREF_FRAME: ref_frame_skip_mask[0] |= ALTREF_FRAME_MODE_MASK; |
| 10790 | #if CONFIG_EXT_REFS |
| 10791 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10792 | #endif // CONFIG_EXT_REFS |
| 10793 | break; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 10794 | case NONE_FRAME: |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10795 | case TOTAL_REFS_PER_FRAME: |
| 10796 | assert(0 && "Invalid Reference frame"); |
| 10797 | break; |
| 10798 | } |
| 10799 | } |
| 10800 | |
| 10801 | if ((ref_frame_skip_mask[0] & (1 << ref_frame)) && |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10802 | (ref_frame_skip_mask[1] & (1 << AOMMAX(0, second_ref_frame)))) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10803 | continue; |
| 10804 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10805 | #if CONFIG_EXT_COMP_REFS |
| 10806 | // TODO(zoeliu): Following toggle between #if 0/1 and the bug will manifest |
| 10807 | // itself. |
| 10808 | #if 0 |
| 10809 | if (!(cpi->ref_frame_flags & flag_list[ref_frame]) || |
| 10810 | (second_ref_frame > INTRA_FRAME && |
| 10811 | (!(cpi->ref_frame_flags & flag_list[second_ref_frame])))) |
| 10812 | printf("Frame=%d, bsize=%d, (mi_row,mi_col)=(%d,%d), ref_frame=%d, " |
| 10813 | "second_ref_frame=%d\n", cm->current_video_frame, bsize, mi_row, |
| 10814 | mi_col, ref_frame, second_ref_frame); |
| 10815 | |
| 10816 | if (!(cpi->ref_frame_flags & flag_list[ref_frame])) continue; |
| 10817 | if (second_ref_frame > INTRA_FRAME && |
| 10818 | (!(cpi->ref_frame_flags & flag_list[second_ref_frame]))) |
| 10819 | continue; |
| 10820 | #endif // 0 |
| 10821 | |
| 10822 | #if !USE_UNI_COMP_REFS |
| 10823 | // NOTE(zoeliu): Temporarily disable uni-directional comp refs |
| 10824 | if (second_ref_frame > INTRA_FRAME) { |
| 10825 | if (!((ref_frame < BWDREF_FRAME) ^ (second_ref_frame < BWDREF_FRAME))) |
| 10826 | continue; |
| 10827 | } |
| 10828 | assert(second_ref_frame <= INTRA_FRAME || |
| 10829 | ((ref_frame < BWDREF_FRAME) ^ (second_ref_frame < BWDREF_FRAME))); |
| 10830 | #endif // !USE_UNI_COMP_REFS |
| 10831 | #endif // CONFIG_EXT_COMP_REFS |
| 10832 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10833 | if (mode_skip_mask[ref_frame] & (1 << this_mode)) continue; |
| 10834 | |
| 10835 | // Test best rd so far against threshold for trying this mode. |
| 10836 | if (best_mode_skippable && sf->schedule_mode_search) |
| 10837 | mode_threshold[mode_index] <<= 1; |
| 10838 | |
| 10839 | if (best_rd < mode_threshold[mode_index]) continue; |
| 10840 | |
Yunqing Wang | ff4fa06 | 2017-04-21 10:56:08 -0700 | [diff] [blame] | 10841 | // This is only used in motion vector unit test. |
| 10842 | if (cpi->oxcf.motion_vector_unit_test && ref_frame == INTRA_FRAME) continue; |
| 10843 | |
Zoe Liu | 5a97883 | 2017-08-15 16:33:34 -0700 | [diff] [blame] | 10844 | #if CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS // Changes LL bitstream |
Arild Fuldseth (arilfuld) | 3f42908 | 2017-04-28 15:54:28 +0200 | [diff] [blame] | 10845 | #if CONFIG_EXT_REFS |
| 10846 | if (cpi->oxcf.pass == 0) { |
| 10847 | // Complexity-compression trade-offs |
| 10848 | // if (ref_frame == ALTREF_FRAME) continue; |
| 10849 | // if (ref_frame == BWDREF_FRAME) continue; |
| 10850 | if (second_ref_frame == ALTREF_FRAME) continue; |
| 10851 | // if (second_ref_frame == BWDREF_FRAME) continue; |
| 10852 | } |
Zoe Liu | 5a97883 | 2017-08-15 16:33:34 -0700 | [diff] [blame] | 10853 | #endif // CONFIG_EXT_REFS |
| 10854 | #endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10855 | comp_pred = second_ref_frame > INTRA_FRAME; |
| 10856 | if (comp_pred) { |
| 10857 | if (!cpi->allow_comp_inter_inter) continue; |
| 10858 | |
| 10859 | // Skip compound inter modes if ARF is not available. |
| 10860 | if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue; |
| 10861 | |
| 10862 | // Do not allow compound prediction if the segment level reference frame |
| 10863 | // feature is in use as in this case there can only be one reference. |
| 10864 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue; |
| 10865 | |
| 10866 | if ((mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) && |
| 10867 | best_mode_index >= 0 && best_mbmode.ref_frame[0] == INTRA_FRAME) |
| 10868 | continue; |
| 10869 | |
| 10870 | mode_excluded = cm->reference_mode == SINGLE_REFERENCE; |
| 10871 | } else { |
| 10872 | if (ref_frame != INTRA_FRAME) |
| 10873 | mode_excluded = cm->reference_mode == COMPOUND_REFERENCE; |
| 10874 | } |
| 10875 | |
| 10876 | if (ref_frame == INTRA_FRAME) { |
| 10877 | if (sf->adaptive_mode_search) |
| 10878 | if ((x->source_variance << num_pels_log2_lookup[bsize]) > best_pred_sse) |
| 10879 | continue; |
| 10880 | |
| 10881 | if (this_mode != DC_PRED) { |
| 10882 | // Disable intra modes other than DC_PRED for blocks with low variance |
| 10883 | // Threshold for intra skipping based on source variance |
| 10884 | // TODO(debargha): Specialize the threshold for super block sizes |
| 10885 | const unsigned int skip_intra_var_thresh = 64; |
| 10886 | if ((mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) && |
| 10887 | x->source_variance < skip_intra_var_thresh) |
| 10888 | continue; |
| 10889 | // Only search the oblique modes if the best so far is |
| 10890 | // one of the neighboring directional modes |
| 10891 | if ((mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) && |
| 10892 | (this_mode >= D45_PRED && this_mode <= TM_PRED)) { |
| 10893 | if (best_mode_index >= 0 && best_mbmode.ref_frame[0] > INTRA_FRAME) |
| 10894 | continue; |
| 10895 | } |
| 10896 | if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 10897 | if (conditional_skipintra(this_mode, best_intra_mode)) continue; |
| 10898 | } |
| 10899 | } |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10900 | #if CONFIG_GLOBAL_MOTION |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 10901 | } else if (cm->global_motion[ref_frame].wmtype == IDENTITY && |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10902 | (!comp_pred || |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 10903 | cm->global_motion[second_ref_frame].wmtype == IDENTITY)) { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10904 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10905 | } else { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10906 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10907 | const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, second_ref_frame }; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10908 | if (!check_best_zero_mv(cpi, x, mbmi_ext->mode_context, |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 10909 | #if CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10910 | mbmi_ext->compound_mode_context, |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 10911 | #endif // CONFIG_EXT_INTER |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 10912 | frame_mv, this_mode, ref_frames, bsize, -1, |
| 10913 | mi_row, mi_col)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10914 | continue; |
| 10915 | } |
| 10916 | |
| 10917 | mbmi->mode = this_mode; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10918 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10919 | mbmi->ref_frame[0] = ref_frame; |
| 10920 | mbmi->ref_frame[1] = second_ref_frame; |
| 10921 | pmi->palette_size[0] = 0; |
| 10922 | pmi->palette_size[1] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10923 | #if CONFIG_FILTER_INTRA |
| 10924 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 10925 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 10926 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10927 | // Evaluate all sub-pel filters irrespective of whether we can use |
| 10928 | // them for this frame. |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 10929 | |
| 10930 | set_default_interp_filters(mbmi, cm->interp_filter); |
| 10931 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10932 | mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10933 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10934 | |
| 10935 | x->skip = 0; |
| 10936 | set_ref_ptrs(cm, xd, ref_frame, second_ref_frame); |
| 10937 | |
| 10938 | // Select prediction reference frames. |
| 10939 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 10940 | xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; |
| 10941 | if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i]; |
| 10942 | } |
| 10943 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10944 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 10945 | // Single ref compound mode |
| 10946 | if (!comp_pred && is_inter_singleref_comp_mode(mbmi->mode)) { |
| 10947 | xd->block_refs[1] = xd->block_refs[0]; |
| 10948 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 10949 | xd->plane[i].pre[1] = xd->plane[i].pre[0]; |
| 10950 | } |
| 10951 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 10952 | |
Yue Chen | 4d26acb | 2017-05-01 12:28:34 -0700 | [diff] [blame] | 10953 | #if CONFIG_EXT_INTER && CONFIG_INTERINTRA |
Debargha Mukherjee | cb60379 | 2016-10-04 13:10:23 -0700 | [diff] [blame] | 10954 | mbmi->interintra_mode = (INTERINTRA_MODE)(II_DC_PRED - 1); |
Yue Chen | 4d26acb | 2017-05-01 12:28:34 -0700 | [diff] [blame] | 10955 | #endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10956 | |
| 10957 | if (ref_frame == INTRA_FRAME) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10958 | RD_STATS rd_stats_y; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10959 | TX_SIZE uv_tx; |
| 10960 | struct macroblockd_plane *const pd = &xd->plane[1]; |
| 10961 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 10962 | is_directional_mode = av1_is_directional_mode(mbmi->mode, bsize); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 10963 | if (is_directional_mode && av1_use_angle_delta(bsize)) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 10964 | int rate_dummy; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 10965 | int64_t model_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10966 | if (!angle_stats_ready) { |
| 10967 | const int src_stride = x->plane[0].src.stride; |
| 10968 | const uint8_t *src = x->plane[0].src.buf; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10969 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10970 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 10971 | highbd_angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10972 | directional_mode_skip_mask); |
| 10973 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10974 | #endif // CONFIG_HIGHBITDEPTH |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 10975 | angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10976 | directional_mode_skip_mask); |
| 10977 | angle_stats_ready = 1; |
| 10978 | } |
| 10979 | if (directional_mode_skip_mask[mbmi->mode]) continue; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 10980 | rd_stats_y.rate = INT_MAX; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 10981 | rd_pick_intra_angle_sby(cpi, x, &rate_dummy, &rd_stats_y, bsize, |
| 10982 | intra_mode_cost[mbmi->mode], best_rd, |
| 10983 | &model_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10984 | } else { |
| 10985 | mbmi->angle_delta[0] = 0; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10986 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10987 | } |
| 10988 | #else |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10989 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 10990 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10991 | rate_y = rd_stats_y.rate; |
| 10992 | distortion_y = rd_stats_y.dist; |
| 10993 | skippable = rd_stats_y.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10994 | |
| 10995 | if (rate_y == INT_MAX) continue; |
| 10996 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10997 | #if CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10998 | if (mbmi->mode == DC_PRED) dc_skipped = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10999 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11000 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 11001 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][pd->subsampling_x] |
| 11002 | [pd->subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11003 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 11004 | 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] | 11005 | &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx], |
| 11006 | &skip_uvs[uv_tx], &mode_uv[uv_tx]); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 11007 | if (try_palette) pmi_uv[uv_tx] = *pmi; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 11008 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11009 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11010 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
| 11011 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11012 | #if CONFIG_FILTER_INTRA |
| 11013 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 11014 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11015 | } |
| 11016 | |
| 11017 | rate_uv = rate_uv_tokenonly[uv_tx]; |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 11018 | distortion_uv = dist_uvs[uv_tx]; |
| 11019 | skippable = skippable && skip_uvs[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11020 | mbmi->uv_mode = mode_uv[uv_tx]; |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 11021 | if (try_palette) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11022 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
| 11023 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 11024 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 11025 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
| 11026 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 11027 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11028 | #if CONFIG_EXT_INTRA |
| 11029 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11030 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11031 | #if CONFIG_FILTER_INTRA |
| 11032 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 11033 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 11034 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 11035 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 11036 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
| 11037 | } |
| 11038 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11039 | |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 11040 | #if CONFIG_CB4X4 |
| 11041 | rate2 = rate_y + intra_mode_cost[mbmi->mode]; |
| 11042 | if (!x->skip_chroma_rd) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11043 | 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] | 11044 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11045 | rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11046 | x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode]; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11047 | #endif // CONFIG_CB4X4 |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 11048 | |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 11049 | if (try_palette && mbmi->mode == DC_PRED) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11050 | rate2 += av1_cost_bit( |
| 11051 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 11052 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11053 | |
Jingning Han | bf9c6b7 | 2016-12-14 14:50:45 -0800 | [diff] [blame] | 11054 | if (!xd->lossless[mbmi->segment_id] && bsize >= BLOCK_8X8) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11055 | // super_block_yrd above includes the cost of the tx_size in the |
| 11056 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 11057 | // (prediction granularity), so we account for it in the full rate, |
| 11058 | // not the tokenonly rate. |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 11059 | rate_y -= tx_size_cost(cpi, x, bsize, mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11060 | } |
| 11061 | #if CONFIG_EXT_INTRA |
| 11062 | if (is_directional_mode) { |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 11063 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11064 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 11065 | const int p_angle = |
| 11066 | mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11067 | if (av1_is_intra_filter_switchable(p_angle)) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11068 | rate2 += x->intra_filter_cost[intra_filter_ctx][mbmi->intra_filter]; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 11069 | #endif // CONFIG_INTRA_INTERP |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 11070 | if (av1_use_angle_delta(bsize)) { |
| 11071 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 11072 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
| 11073 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11074 | } |
Luc Trudeau | f24a35f | 2017-09-11 11:56:33 -0400 | [diff] [blame] | 11075 | if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize) && |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 11076 | av1_use_angle_delta(bsize)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 11077 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 11078 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11079 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11080 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11081 | #if CONFIG_FILTER_INTRA |
| 11082 | if (mbmi->mode == DC_PRED) { |
| 11083 | rate2 += |
| 11084 | av1_cost_bit(cm->fc->filter_intra_probs[0], |
| 11085 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0]); |
| 11086 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) { |
| 11087 | rate2 += write_uniform_cost( |
| 11088 | FILTER_INTRA_MODES, |
| 11089 | mbmi->filter_intra_mode_info.filter_intra_mode[0]); |
| 11090 | } |
| 11091 | } |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 11092 | if (mbmi->uv_mode == UV_DC_PRED) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11093 | rate2 += |
| 11094 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], |
| 11095 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1]); |
| 11096 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1]) |
| 11097 | rate2 += write_uniform_cost( |
| 11098 | FILTER_INTRA_MODES, |
| 11099 | mbmi->filter_intra_mode_info.filter_intra_mode[1]); |
| 11100 | } |
| 11101 | #endif // CONFIG_FILTER_INTRA |
Zoe Liu | 1157d50 | 2017-04-30 07:57:14 -0700 | [diff] [blame] | 11102 | if (mbmi->mode != DC_PRED && mbmi->mode != TM_PRED) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11103 | rate2 += intra_cost_penalty; |
| 11104 | distortion2 = distortion_y + distortion_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11105 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11106 | int_mv backup_ref_mv[2]; |
| 11107 | |
Debargha Mukherjee | 0f248c4 | 2017-09-07 12:40:18 -0700 | [diff] [blame] | 11108 | if (!is_comp_ref_allowed(bsize) && mbmi->ref_frame[1] > INTRA_FRAME) |
| 11109 | continue; |
Jingning Han | c41a549 | 2017-02-24 11:18:52 -0800 | [diff] [blame] | 11110 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11111 | backup_ref_mv[0] = mbmi_ext->ref_mvs[ref_frame][0]; |
| 11112 | if (comp_pred) backup_ref_mv[1] = mbmi_ext->ref_mvs[second_ref_frame][0]; |
Yue Chen | 4d26acb | 2017-05-01 12:28:34 -0700 | [diff] [blame] | 11113 | #if CONFIG_EXT_INTER && CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11114 | if (second_ref_frame == INTRA_FRAME) { |
| 11115 | if (best_single_inter_ref != ref_frame) continue; |
Debargha Mukherjee | cb60379 | 2016-10-04 13:10:23 -0700 | [diff] [blame] | 11116 | mbmi->interintra_mode = intra_to_interintra_mode[best_intra_mode]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11117 | // TODO(debargha|geza.lore): |
| 11118 | // Should we use ext_intra modes for interintra? |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11119 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11120 | mbmi->angle_delta[0] = 0; |
| 11121 | mbmi->angle_delta[1] = 0; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 11122 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11123 | mbmi->intra_filter = INTRA_FILTER_LINEAR; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 11124 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11125 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11126 | #if CONFIG_FILTER_INTRA |
| 11127 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 11128 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 11129 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11130 | } |
Yue Chen | 4d26acb | 2017-05-01 12:28:34 -0700 | [diff] [blame] | 11131 | #endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11132 | mbmi->ref_mv_idx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11133 | ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11134 | |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 11135 | #if CONFIG_EXT_INTER |
| 11136 | if (comp_pred) { |
| 11137 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11138 | int ref_mv_idx = 0; |
| 11139 | // Special case: NEAR_NEWMV and NEW_NEARMV modes use |
| 11140 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 11141 | // mbmi->ref_mv_idx (like NEWMV) |
| 11142 | if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) |
| 11143 | ref_mv_idx = 1; |
| 11144 | |
| 11145 | if (compound_ref0_mode(mbmi->mode) == NEWMV) { |
| 11146 | int_mv this_mv = |
| 11147 | 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] | 11148 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11149 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11150 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 11151 | } |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11152 | if (compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 11153 | int_mv this_mv = |
| 11154 | 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] | 11155 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11156 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11157 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 11158 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11159 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11160 | #if CONFIG_COMPOUND_SINGLEREF |
| 11161 | } else if (is_inter_singleref_comp_mode(mbmi->mode)) { |
| 11162 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
| 11163 | // TODO(zoeliu): To further investigate which ref_mv_idx should be |
| 11164 | // chosen for the mode of SR_NEAR_NEWMV. |
| 11165 | int ref_mv_idx = 0; |
| 11166 | // Special case: SR_NEAR_NEWMV mode use |
| 11167 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 11168 | // mbmi->ref_mv_idx (like NEWMV) |
| 11169 | if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1; |
| 11170 | |
| 11171 | if (compound_ref0_mode(mbmi->mode) == NEWMV || |
| 11172 | compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 11173 | int_mv this_mv = |
| 11174 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 11175 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11176 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11177 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 11178 | } |
| 11179 | } |
| 11180 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 11181 | } else { |
| 11182 | #endif // CONFIG_EXT_INTER |
Zoe Liu | 1157d50 | 2017-04-30 07:57:14 -0700 | [diff] [blame] | 11183 | 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] | 11184 | int ref; |
| 11185 | for (ref = 0; ref < 1 + comp_pred; ++ref) { |
| 11186 | int_mv this_mv = |
| 11187 | (ref == 0) ? mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv |
| 11188 | : mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 11189 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11190 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11191 | mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv; |
| 11192 | } |
| 11193 | } |
| 11194 | #if CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11195 | } |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 11196 | #endif // CONFIG_EXT_INTER |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11197 | { |
| 11198 | RD_STATS rd_stats, rd_stats_y, rd_stats_uv; |
| 11199 | av1_init_rd_stats(&rd_stats); |
| 11200 | rd_stats.rate = rate2; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11201 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11202 | // Point to variables that are maintained between loop iterations |
| 11203 | args.single_newmv = single_newmv; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11204 | #if CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11205 | args.single_newmv_rate = single_newmv_rate; |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 11206 | args.modelled_rd = modelled_rd; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11207 | #endif // CONFIG_EXT_INTER |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 11208 | this_rd = handle_inter_mode(cpi, x, bsize, &rd_stats, &rd_stats_y, |
| 11209 | &rd_stats_uv, &disable_skip, frame_mv, |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11210 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 11211 | frame_comp_mv, |
| 11212 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 11213 | mi_row, mi_col, &args, best_rd); |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11214 | |
| 11215 | rate2 = rd_stats.rate; |
| 11216 | skippable = rd_stats.skip; |
| 11217 | distortion2 = rd_stats.dist; |
| 11218 | total_sse = rd_stats.sse; |
| 11219 | rate_y = rd_stats_y.rate; |
| 11220 | rate_uv = rd_stats_uv.rate; |
| 11221 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11222 | |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 11223 | // TODO(jingning): This needs some refactoring to improve code quality |
| 11224 | // and reduce redundant steps. |
| 11225 | #if CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11226 | #if CONFIG_COMPOUND_SINGLEREF |
| 11227 | if ((have_nearmv_in_inter_mode(mbmi->mode) && |
| 11228 | mbmi_ext->ref_mv_count[ref_frame_type] > 2) || |
| 11229 | ((mbmi->mode == NEWMV || mbmi->mode == SR_NEW_NEWMV || |
| 11230 | mbmi->mode == NEW_NEWMV) && |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11231 | mbmi_ext->ref_mv_count[ref_frame_type] > 1)) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11232 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11233 | if ((have_nearmv_in_inter_mode(mbmi->mode) && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 11234 | mbmi_ext->ref_mv_count[ref_frame_type] > 2) || |
| 11235 | ((mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) && |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11236 | mbmi_ext->ref_mv_count[ref_frame_type] > 1)) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11237 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 11238 | #else // !CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11239 | if ((mbmi->mode == NEARMV && |
| 11240 | mbmi_ext->ref_mv_count[ref_frame_type] > 2) || |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11241 | (mbmi->mode == NEWMV && mbmi_ext->ref_mv_count[ref_frame_type] > 1)) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11242 | #endif // CONFIG_EXT_INTER |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11243 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11244 | int_mv backup_mv = frame_mv[NEARMV][ref_frame]; |
| 11245 | MB_MODE_INFO backup_mbmi = *mbmi; |
| 11246 | int backup_skip = x->skip; |
| 11247 | int64_t tmp_ref_rd = this_rd; |
| 11248 | int ref_idx; |
| 11249 | |
Yue Chen | 6e601e9 | 2016-12-05 18:19:00 -0800 | [diff] [blame] | 11250 | // TODO(jingning): This should be deprecated shortly. |
| 11251 | #if CONFIG_EXT_INTER |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11252 | int idx_offset = have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0; |
Yue Chen | 6e601e9 | 2016-12-05 18:19:00 -0800 | [diff] [blame] | 11253 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11254 | int idx_offset = (mbmi->mode == NEARMV) ? 1 : 0; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11255 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11256 | int ref_set = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11257 | 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] | 11258 | |
| 11259 | uint8_t drl_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11260 | 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] | 11261 | // Dummy |
| 11262 | int_mv backup_fmv[2]; |
| 11263 | backup_fmv[0] = frame_mv[NEWMV][ref_frame]; |
| 11264 | if (comp_pred) backup_fmv[1] = frame_mv[NEWMV][second_ref_frame]; |
| 11265 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11266 | rate2 += (rate2 < INT_MAX ? x->drl_mode_cost0[drl_ctx][0] : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11267 | |
| 11268 | if (this_rd < INT64_MAX) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11269 | if (RDCOST(x->rdmult, rate_y + rate_uv, distortion2) < |
| 11270 | RDCOST(x->rdmult, 0, total_sse)) |
| 11271 | tmp_ref_rd = RDCOST( |
| 11272 | x->rdmult, rate2 + av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
| 11273 | distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11274 | else |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 11275 | tmp_ref_rd = |
| 11276 | RDCOST(x->rdmult, |
| 11277 | rate2 + av1_cost_bit(av1_get_skip_prob(cm, xd), 1) - |
| 11278 | rate_y - rate_uv, |
| 11279 | total_sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11280 | } |
| 11281 | #if CONFIG_VAR_TX |
| 11282 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11283 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 11284 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11285 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11286 | |
| 11287 | for (ref_idx = 0; ref_idx < ref_set; ++ref_idx) { |
| 11288 | int64_t tmp_alt_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11289 | int dummy_disable_skip = 0; |
| 11290 | int ref; |
| 11291 | int_mv cur_mv; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11292 | RD_STATS tmp_rd_stats, tmp_rd_stats_y, tmp_rd_stats_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11293 | |
Yaowu Xu | 5bfbfdf | 2016-11-22 16:43:34 -0800 | [diff] [blame] | 11294 | av1_invalid_rd_stats(&tmp_rd_stats); |
Yushin Cho | c0f6bf2 | 2017-06-09 16:08:02 -0700 | [diff] [blame] | 11295 | |
Jingning Han | 52617b2 | 2017-04-11 12:50:08 -0700 | [diff] [blame] | 11296 | x->skip = 0; |
Yaowu Xu | 5bfbfdf | 2016-11-22 16:43:34 -0800 | [diff] [blame] | 11297 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11298 | mbmi->ref_mv_idx = 1 + ref_idx; |
| 11299 | |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11300 | #if CONFIG_EXT_INTER |
| 11301 | if (comp_pred) { |
| 11302 | int ref_mv_idx = mbmi->ref_mv_idx; |
| 11303 | // Special case: NEAR_NEWMV and NEW_NEARMV modes use |
| 11304 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 11305 | // mbmi->ref_mv_idx (like NEWMV) |
| 11306 | if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) |
| 11307 | ref_mv_idx = 1 + mbmi->ref_mv_idx; |
| 11308 | |
| 11309 | if (compound_ref0_mode(mbmi->mode) == NEWMV) { |
| 11310 | int_mv this_mv = |
| 11311 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 11312 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11313 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11314 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 11315 | } else if (compound_ref0_mode(mbmi->mode) == NEARESTMV) { |
| 11316 | int_mv this_mv = |
| 11317 | mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 11318 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11319 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11320 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 11321 | } |
| 11322 | |
| 11323 | if (compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 11324 | int_mv this_mv = |
| 11325 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv; |
| 11326 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11327 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11328 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 11329 | } else if (compound_ref1_mode(mbmi->mode) == NEARESTMV) { |
| 11330 | int_mv this_mv = |
| 11331 | mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 11332 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11333 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11334 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 11335 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11336 | #if CONFIG_COMPOUND_SINGLEREF |
| 11337 | } else if (is_inter_singleref_comp_mode(mbmi->mode)) { |
| 11338 | int ref_mv_idx = mbmi->ref_mv_idx; |
| 11339 | // Special case: SR_NEAR_NEWMV mode use |
| 11340 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 11341 | // mbmi->ref_mv_idx (like NEWMV) |
| 11342 | if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx; |
| 11343 | |
| 11344 | // TODO(zoeliu): For the mode of SR_NEAREST_NEWMV, as it only runs |
| 11345 | // the "if", not the "else if", |
| 11346 | // mbmi_ext->ref_mvs[mbmi->ref_frame[0]] takes the |
| 11347 | // value for "NEWMV", instead of "NEARESTMV". |
| 11348 | if (compound_ref0_mode(mbmi->mode) == NEWMV || |
| 11349 | compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 11350 | int_mv this_mv = |
| 11351 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 11352 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11353 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11354 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 11355 | } else if (compound_ref0_mode(mbmi->mode) == NEARESTMV || |
| 11356 | compound_ref1_mode(mbmi->mode) == NEARESTMV) { |
| 11357 | int_mv this_mv = |
| 11358 | mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 11359 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11360 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11361 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 11362 | } |
| 11363 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11364 | } else { |
| 11365 | #endif // CONFIG_EXT_INTER |
| 11366 | for (ref = 0; ref < 1 + comp_pred; ++ref) { |
| 11367 | int_mv this_mv = |
| 11368 | (ref == 0) |
| 11369 | ? mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx] |
| 11370 | .this_mv |
| 11371 | : mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx] |
| 11372 | .comp_mv; |
| 11373 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11374 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11375 | mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv; |
| 11376 | } |
| 11377 | #if CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11378 | } |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11379 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11380 | |
| 11381 | cur_mv = |
| 11382 | mbmi_ext->ref_mv_stack[ref_frame][mbmi->ref_mv_idx + idx_offset] |
| 11383 | .this_mv; |
| 11384 | clamp_mv2(&cur_mv.as_mv, xd); |
| 11385 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 11386 | if (!mv_check_bounds(&x->mv_limits, &cur_mv.as_mv)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11387 | int_mv dummy_single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } }; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11388 | #if CONFIG_EXT_INTER |
| 11389 | int dummy_single_newmv_rate[TOTAL_REFS_PER_FRAME] = { 0 }; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11390 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11391 | |
| 11392 | frame_mv[NEARMV][ref_frame] = cur_mv; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11393 | av1_init_rd_stats(&tmp_rd_stats); |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 11394 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11395 | // Point to variables that are not maintained between iterations |
| 11396 | args.single_newmv = dummy_single_newmv; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11397 | #if CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11398 | args.single_newmv_rate = dummy_single_newmv_rate; |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 11399 | args.modelled_rd = NULL; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11400 | #endif // CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11401 | tmp_alt_rd = handle_inter_mode(cpi, x, bsize, &tmp_rd_stats, |
| 11402 | &tmp_rd_stats_y, &tmp_rd_stats_uv, |
| 11403 | &dummy_disable_skip, frame_mv, |
| 11404 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 11405 | frame_comp_mv, |
| 11406 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 11407 | mi_row, mi_col, &args, best_rd); |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11408 | // Prevent pointers from escaping local scope |
| 11409 | args.single_newmv = NULL; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11410 | #if CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11411 | args.single_newmv_rate = NULL; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11412 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11413 | } |
| 11414 | |
| 11415 | for (i = 0; i < mbmi->ref_mv_idx; ++i) { |
| 11416 | uint8_t drl1_ctx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11417 | drl1_ctx = av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], |
| 11418 | i + idx_offset); |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11419 | tmp_rd_stats.rate += |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11420 | (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] | 11421 | : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11422 | } |
| 11423 | |
| 11424 | if (mbmi_ext->ref_mv_count[ref_frame_type] > |
| 11425 | mbmi->ref_mv_idx + idx_offset + 1 && |
| 11426 | ref_idx < ref_set - 1) { |
| 11427 | uint8_t drl1_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11428 | av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], |
| 11429 | mbmi->ref_mv_idx + idx_offset); |
Yaowu Xu | 83ed6fe | 2016-11-22 11:15:29 -0800 | [diff] [blame] | 11430 | tmp_rd_stats.rate += |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11431 | (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] | 11432 | : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11433 | } |
| 11434 | |
| 11435 | if (tmp_alt_rd < INT64_MAX) { |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11436 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11437 | tmp_alt_rd = |
| 11438 | RDCOST(x->rdmult, tmp_rd_stats.rate, tmp_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11439 | #else |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11440 | if (RDCOST(x->rdmult, tmp_rd_stats_y.rate + tmp_rd_stats_uv.rate, |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11441 | tmp_rd_stats.dist) < |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11442 | RDCOST(x->rdmult, 0, tmp_rd_stats.sse)) |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 11443 | tmp_alt_rd = |
| 11444 | RDCOST(x->rdmult, |
| 11445 | tmp_rd_stats.rate + |
| 11446 | av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
| 11447 | tmp_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11448 | else |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 11449 | tmp_alt_rd = |
| 11450 | RDCOST(x->rdmult, |
| 11451 | tmp_rd_stats.rate + |
| 11452 | av1_cost_bit(av1_get_skip_prob(cm, xd), 1) - |
| 11453 | tmp_rd_stats_y.rate - tmp_rd_stats_uv.rate, |
| 11454 | tmp_rd_stats.sse); |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11455 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11456 | } |
| 11457 | |
| 11458 | if (tmp_ref_rd > tmp_alt_rd) { |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11459 | rate2 = tmp_rd_stats.rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11460 | disable_skip = dummy_disable_skip; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11461 | distortion2 = tmp_rd_stats.dist; |
| 11462 | skippable = tmp_rd_stats.skip; |
| 11463 | rate_y = tmp_rd_stats_y.rate; |
| 11464 | rate_uv = tmp_rd_stats_uv.rate; |
| 11465 | total_sse = tmp_rd_stats.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11466 | this_rd = tmp_alt_rd; |
| 11467 | tmp_ref_rd = tmp_alt_rd; |
| 11468 | backup_mbmi = *mbmi; |
| 11469 | backup_skip = x->skip; |
| 11470 | #if CONFIG_VAR_TX |
| 11471 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11472 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 11473 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11474 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11475 | } else { |
| 11476 | *mbmi = backup_mbmi; |
| 11477 | x->skip = backup_skip; |
| 11478 | } |
| 11479 | } |
| 11480 | |
| 11481 | frame_mv[NEARMV][ref_frame] = backup_mv; |
| 11482 | frame_mv[NEWMV][ref_frame] = backup_fmv[0]; |
| 11483 | if (comp_pred) frame_mv[NEWMV][second_ref_frame] = backup_fmv[1]; |
| 11484 | #if CONFIG_VAR_TX |
| 11485 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11486 | memcpy(x->blk_skip[i], x->blk_skip_drl[i], |
| 11487 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11488 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11489 | } |
| 11490 | mbmi_ext->ref_mvs[ref_frame][0] = backup_ref_mv[0]; |
| 11491 | 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] | 11492 | |
| 11493 | if (this_rd == INT64_MAX) continue; |
| 11494 | |
Debargha Mukherjee | 0f248c4 | 2017-09-07 12:40:18 -0700 | [diff] [blame] | 11495 | if (is_comp_ref_allowed(mbmi->sb_type)) |
Jingning Han | c41a549 | 2017-02-24 11:18:52 -0800 | [diff] [blame] | 11496 | compmode_cost = av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11497 | |
| 11498 | if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost; |
| 11499 | } |
| 11500 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11501 | // Estimate the reference frame signaling cost and add it |
| 11502 | // to the rolling cost variable. |
| 11503 | if (comp_pred) { |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 11504 | #if CONFIG_EXT_COMP_REFS |
| 11505 | rate2 += ref_costs_comp[ref_frame][second_ref_frame]; |
| 11506 | #else // !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11507 | rate2 += ref_costs_comp[ref_frame]; |
| 11508 | #if CONFIG_EXT_REFS |
| 11509 | rate2 += ref_costs_comp[second_ref_frame]; |
| 11510 | #endif // CONFIG_EXT_REFS |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 11511 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11512 | } else { |
| 11513 | rate2 += ref_costs_single[ref_frame]; |
| 11514 | } |
| 11515 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11516 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 11517 | // Add the cost to signal single/comp mode in single ref. |
| 11518 | if (!comp_pred && cm->reference_mode != COMPOUND_REFERENCE) { |
| 11519 | aom_prob singleref_comp_mode_p = av1_get_inter_mode_prob(cm, xd); |
| 11520 | rate2 += av1_cost_bit(singleref_comp_mode_p, |
| 11521 | is_inter_singleref_comp_mode(mbmi->mode)); |
| 11522 | } |
| 11523 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 11524 | |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11525 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11526 | if (ref_frame == INTRA_FRAME) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11527 | #else |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11528 | if (!disable_skip) |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11529 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11530 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11531 | if (skippable) { |
| 11532 | // Back out the coefficient coding costs |
| 11533 | rate2 -= (rate_y + rate_uv); |
| 11534 | rate_y = 0; |
| 11535 | rate_uv = 0; |
| 11536 | // Cost the skip mb case |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11537 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11538 | } else if (ref_frame != INTRA_FRAME && !xd->lossless[mbmi->segment_id]) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11539 | if (RDCOST(x->rdmult, rate_y + rate_uv + rate_skip0, distortion2) < |
| 11540 | RDCOST(x->rdmult, rate_skip1, total_sse)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11541 | // Add in the cost of the no skip flag. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11542 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11543 | } else { |
| 11544 | // FIXME(rbultje) make this work for splitmv also |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11545 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11546 | distortion2 = total_sse; |
| 11547 | assert(total_sse >= 0); |
| 11548 | rate2 -= (rate_y + rate_uv); |
| 11549 | this_skip2 = 1; |
| 11550 | rate_y = 0; |
| 11551 | rate_uv = 0; |
| 11552 | } |
| 11553 | } else { |
| 11554 | // Add in the cost of the no skip flag. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11555 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11556 | } |
| 11557 | |
| 11558 | // Calculate the final RD estimate for this mode. |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11559 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11560 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11561 | } else { |
| 11562 | this_skip2 = mbmi->skip; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11563 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11564 | if (this_skip2) { |
| 11565 | rate_y = 0; |
| 11566 | rate_uv = 0; |
| 11567 | } |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11568 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11569 | } |
| 11570 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11571 | if (ref_frame == INTRA_FRAME) { |
| 11572 | // Keep record of best intra rd |
| 11573 | if (this_rd < best_intra_rd) { |
| 11574 | best_intra_rd = this_rd; |
| 11575 | best_intra_mode = mbmi->mode; |
| 11576 | } |
Yue Chen | 4d26acb | 2017-05-01 12:28:34 -0700 | [diff] [blame] | 11577 | #if CONFIG_EXT_INTER && CONFIG_INTERINTRA |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 11578 | } else if (second_ref_frame == NONE_FRAME) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11579 | if (this_rd < best_single_inter_rd) { |
| 11580 | best_single_inter_rd = this_rd; |
| 11581 | best_single_inter_ref = mbmi->ref_frame[0]; |
| 11582 | } |
Yue Chen | 4d26acb | 2017-05-01 12:28:34 -0700 | [diff] [blame] | 11583 | #endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11584 | } |
| 11585 | |
| 11586 | if (!disable_skip && ref_frame == INTRA_FRAME) { |
| 11587 | for (i = 0; i < REFERENCE_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11588 | best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11589 | } |
| 11590 | |
| 11591 | // Did this mode help.. i.e. is it the new best mode |
| 11592 | if (this_rd < best_rd || x->skip) { |
| 11593 | if (!mode_excluded) { |
| 11594 | // Note index of best mode so far |
| 11595 | best_mode_index = mode_index; |
| 11596 | |
| 11597 | if (ref_frame == INTRA_FRAME) { |
| 11598 | /* required for left and above block mv */ |
| 11599 | mbmi->mv[0].as_int = 0; |
| 11600 | } else { |
| 11601 | best_pred_sse = x->pred_sse[ref_frame]; |
| 11602 | } |
| 11603 | |
| 11604 | rd_cost->rate = rate2; |
| 11605 | #if CONFIG_SUPERTX |
| 11606 | if (x->skip) |
| 11607 | *returnrate_nocoef = rate2; |
| 11608 | else |
| 11609 | *returnrate_nocoef = rate2 - rate_y - rate_uv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11610 | *returnrate_nocoef -= av1_cost_bit( |
| 11611 | av1_get_skip_prob(cm, xd), disable_skip || skippable || this_skip2); |
| 11612 | *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd), |
| 11613 | mbmi->ref_frame[0] != INTRA_FRAME); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11614 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yue Chen | 52c5173 | 2017-07-11 15:08:30 -0700 | [diff] [blame] | 11615 | #if CONFIG_WARPED_MOTION |
| 11616 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 11617 | #endif |
Yaowu Xu | 41a36de | 2017-03-23 15:55:03 -0700 | [diff] [blame] | 11618 | #if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION |
| 11619 | MODE_INFO *const mi = xd->mi[0]; |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11620 | const MOTION_MODE motion_allowed = motion_mode_allowed( |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 11621 | #if CONFIG_GLOBAL_MOTION |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11622 | 0, xd->global_motion, |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 11623 | #endif // CONFIG_GLOBAL_MOTION |
Yue Chen | 52c5173 | 2017-07-11 15:08:30 -0700 | [diff] [blame] | 11624 | #if CONFIG_WARPED_MOTION |
| 11625 | xd, |
| 11626 | #endif |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11627 | mi); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11628 | if (motion_allowed == WARPED_CAUSAL) |
Yue Chen | bdc8dab | 2017-07-26 12:05:47 -0700 | [diff] [blame] | 11629 | *returnrate_nocoef -= x->motion_mode_cost[bsize][mbmi->motion_mode]; |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11630 | else if (motion_allowed == OBMC_CAUSAL) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11631 | *returnrate_nocoef -= x->motion_mode_cost1[bsize][mbmi->motion_mode]; |
Yaowu Xu | 41a36de | 2017-03-23 15:55:03 -0700 | [diff] [blame] | 11632 | #else |
Yue Chen | bdc8dab | 2017-07-26 12:05:47 -0700 | [diff] [blame] | 11633 | *returnrate_nocoef -= x->motion_mode_cost[bsize][mbmi->motion_mode]; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11634 | #endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11635 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11636 | #endif // CONFIG_SUPERTX |
| 11637 | rd_cost->dist = distortion2; |
| 11638 | rd_cost->rdcost = this_rd; |
| 11639 | best_rd = this_rd; |
| 11640 | best_mbmode = *mbmi; |
| 11641 | best_skip2 = this_skip2; |
| 11642 | best_mode_skippable = skippable; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11643 | best_rate_y = rate_y + av1_cost_bit(av1_get_skip_prob(cm, xd), |
| 11644 | this_skip2 || skippable); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11645 | best_rate_uv = rate_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11646 | #if CONFIG_VAR_TX |
| 11647 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11648 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
| 11649 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11650 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11651 | } |
| 11652 | } |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 11653 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11654 | /* keep record of best compound/single-only prediction */ |
| 11655 | if (!disable_skip && ref_frame != INTRA_FRAME) { |
| 11656 | int64_t single_rd, hybrid_rd, single_rate, hybrid_rate; |
| 11657 | |
| 11658 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
| 11659 | single_rate = rate2 - compmode_cost; |
| 11660 | hybrid_rate = rate2; |
| 11661 | } else { |
| 11662 | single_rate = rate2; |
| 11663 | hybrid_rate = rate2 + compmode_cost; |
| 11664 | } |
| 11665 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11666 | single_rd = RDCOST(x->rdmult, single_rate, distortion2); |
| 11667 | hybrid_rd = RDCOST(x->rdmult, hybrid_rate, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11668 | |
| 11669 | if (!comp_pred) { |
| 11670 | if (single_rd < best_pred_rd[SINGLE_REFERENCE]) |
| 11671 | best_pred_rd[SINGLE_REFERENCE] = single_rd; |
| 11672 | } else { |
| 11673 | if (single_rd < best_pred_rd[COMPOUND_REFERENCE]) |
| 11674 | best_pred_rd[COMPOUND_REFERENCE] = single_rd; |
| 11675 | } |
| 11676 | if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT]) |
| 11677 | best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd; |
| 11678 | } |
| 11679 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11680 | if (x->skip && !comp_pred) break; |
| 11681 | } |
| 11682 | |
| 11683 | if (xd->lossless[mbmi->segment_id] == 0 && best_mode_index >= 0 && |
| 11684 | ((sf->tx_type_search.fast_inter_tx_type_search == 1 && |
| 11685 | is_inter_mode(best_mbmode.mode)) || |
| 11686 | (sf->tx_type_search.fast_intra_tx_type_search == 1 && |
| 11687 | !is_inter_mode(best_mbmode.mode)))) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11688 | int skip_blk = 0; |
| 11689 | RD_STATS rd_stats_y, rd_stats_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11690 | |
| 11691 | x->use_default_inter_tx_type = 0; |
| 11692 | x->use_default_intra_tx_type = 0; |
| 11693 | |
| 11694 | *mbmi = best_mbmode; |
| 11695 | |
| 11696 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 11697 | |
| 11698 | // Select prediction reference frames. |
| 11699 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 11700 | xd->plane[i].pre[0] = yv12_mb[mbmi->ref_frame[0]][i]; |
| 11701 | if (has_second_ref(mbmi)) |
| 11702 | xd->plane[i].pre[1] = yv12_mb[mbmi->ref_frame[1]][i]; |
| 11703 | } |
| 11704 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11705 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 11706 | // Single ref compound mode |
| 11707 | if (!has_second_ref(mbmi) && is_inter_singleref_comp_mode(mbmi->mode)) { |
| 11708 | xd->block_refs[1] = xd->block_refs[0]; |
| 11709 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 11710 | xd->plane[i].pre[1] = xd->plane[i].pre[0]; |
| 11711 | } |
| 11712 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 11713 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11714 | if (is_inter_mode(mbmi->mode)) { |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 11715 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11716 | #if CONFIG_MOTION_VAR |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11717 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11718 | av1_build_obmc_inter_prediction( |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 11719 | cm, xd, mi_row, mi_col, args.above_pred_buf, args.above_pred_stride, |
| 11720 | args.left_pred_buf, args.left_pred_stride); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11721 | } |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11722 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11723 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11724 | #if CONFIG_VAR_TX |
| 11725 | if (cm->tx_mode == TX_MODE_SELECT || xd->lossless[mbmi->segment_id]) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 11726 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 11727 | assert(rd_stats_y.rate != INT_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11728 | } else { |
| 11729 | int idx, idy; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11730 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11731 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 11732 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 11733 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11734 | memset(x->blk_skip[0], rd_stats_y.skip, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11735 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 11736 | } |
| 11737 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 11738 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11739 | #else |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11740 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 11741 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11742 | #endif // CONFIG_VAR_TX |
| 11743 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11744 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 11745 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11746 | } |
| 11747 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11748 | if (RDCOST(x->rdmult, rd_stats_y.rate + rd_stats_uv.rate, |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11749 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11750 | RDCOST(x->rdmult, 0, (rd_stats_y.sse + rd_stats_uv.sse))) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11751 | skip_blk = 1; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11752 | rd_stats_y.rate = av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 11753 | rd_stats_uv.rate = 0; |
| 11754 | rd_stats_y.dist = rd_stats_y.sse; |
| 11755 | rd_stats_uv.dist = rd_stats_uv.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11756 | } else { |
| 11757 | skip_blk = 0; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11758 | rd_stats_y.rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11759 | } |
| 11760 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11761 | if (RDCOST(x->rdmult, best_rate_y + best_rate_uv, rd_cost->dist) > |
| 11762 | RDCOST(x->rdmult, rd_stats_y.rate + rd_stats_uv.rate, |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11763 | (rd_stats_y.dist + rd_stats_uv.dist))) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11764 | #if CONFIG_VAR_TX |
| 11765 | int idx, idy; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11766 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11767 | best_mbmode.tx_type = mbmi->tx_type; |
| 11768 | best_mbmode.tx_size = mbmi->tx_size; |
| 11769 | #if CONFIG_VAR_TX |
| 11770 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 11771 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 11772 | best_mbmode.inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 11773 | |
| 11774 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11775 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
| 11776 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 11777 | |
| 11778 | best_mbmode.min_tx_size = mbmi->min_tx_size; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11779 | #endif // CONFIG_VAR_TX |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11780 | rd_cost->rate += |
| 11781 | (rd_stats_y.rate + rd_stats_uv.rate - best_rate_y - best_rate_uv); |
| 11782 | rd_cost->dist = rd_stats_y.dist + rd_stats_uv.dist; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11783 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11784 | best_skip2 = skip_blk; |
| 11785 | } |
| 11786 | } |
| 11787 | |
| 11788 | // 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] | 11789 | if (try_palette && !is_inter_mode(best_mbmode.mode)) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11790 | int rate2 = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11791 | #if CONFIG_SUPERTX |
| 11792 | int best_rate_nocoef; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11793 | #endif // CONFIG_SUPERTX |
Urvang Joshi | 451e0f2 | 2017-01-31 11:18:31 -0800 | [diff] [blame] | 11794 | int64_t distortion2 = 0, best_rd_palette = best_rd, this_rd, |
| 11795 | best_model_rd_palette = INT64_MAX; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 11796 | int skippable = 0, rate_overhead_palette = 0; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11797 | RD_STATS rd_stats_y; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 11798 | TX_SIZE uv_tx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11799 | uint8_t *const best_palette_color_map = |
| 11800 | x->palette_buffer->best_palette_color_map; |
| 11801 | uint8_t *const color_map = xd->plane[0].color_index_map; |
Urvang Joshi | 451e0f2 | 2017-01-31 11:18:31 -0800 | [diff] [blame] | 11802 | MB_MODE_INFO best_mbmi_palette = best_mbmode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11803 | |
| 11804 | mbmi->mode = DC_PRED; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 11805 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11806 | mbmi->ref_frame[0] = INTRA_FRAME; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 11807 | mbmi->ref_frame[1] = NONE_FRAME; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 11808 | rate_overhead_palette = rd_pick_palette_intra_sby( |
Urvang Joshi | 451e0f2 | 2017-01-31 11:18:31 -0800 | [diff] [blame] | 11809 | cpi, x, bsize, palette_ctx, intra_mode_cost[DC_PRED], |
| 11810 | &best_mbmi_palette, best_palette_color_map, &best_rd_palette, |
| 11811 | &best_model_rd_palette, NULL, NULL, NULL, NULL); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 11812 | if (pmi->palette_size[0] == 0) goto PALETTE_EXIT; |
| 11813 | memcpy(color_map, best_palette_color_map, |
| 11814 | rows * cols * sizeof(best_palette_color_map[0])); |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11815 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
| 11816 | if (rd_stats_y.rate == INT_MAX) goto PALETTE_EXIT; |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 11817 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x] |
| 11818 | [xd->plane[1].subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11819 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 11820 | 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] | 11821 | &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx], |
| 11822 | &skip_uvs[uv_tx], &mode_uv[uv_tx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11823 | pmi_uv[uv_tx] = *pmi; |
| 11824 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11825 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
| 11826 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11827 | #if CONFIG_FILTER_INTRA |
| 11828 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 11829 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11830 | } |
| 11831 | mbmi->uv_mode = mode_uv[uv_tx]; |
| 11832 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 11833 | if (pmi->palette_size[1] > 0) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11834 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 11835 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 11836 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 11837 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11838 | #if CONFIG_EXT_INTRA |
| 11839 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11840 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11841 | #if CONFIG_FILTER_INTRA |
| 11842 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 11843 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 11844 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 11845 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 11846 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
| 11847 | } |
| 11848 | #endif // CONFIG_FILTER_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11849 | skippable = rd_stats_y.skip && skip_uvs[uv_tx]; |
| 11850 | distortion2 = rd_stats_y.dist + dist_uvs[uv_tx]; |
| 11851 | 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] | 11852 | rate2 += ref_costs_single[INTRA_FRAME]; |
| 11853 | |
| 11854 | if (skippable) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11855 | rate2 -= (rd_stats_y.rate + rate_uv_tokenonly[uv_tx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11856 | #if CONFIG_SUPERTX |
| 11857 | best_rate_nocoef = rate2; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11858 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11859 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11860 | } else { |
| 11861 | #if CONFIG_SUPERTX |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11862 | best_rate_nocoef = rate2 - (rd_stats_y.rate + rate_uv_tokenonly[uv_tx]); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11863 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11864 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11865 | } |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11866 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11867 | if (this_rd < best_rd) { |
| 11868 | best_mode_index = 3; |
| 11869 | mbmi->mv[0].as_int = 0; |
| 11870 | rd_cost->rate = rate2; |
| 11871 | #if CONFIG_SUPERTX |
| 11872 | *returnrate_nocoef = best_rate_nocoef; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11873 | #endif // CONFIG_SUPERTX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11874 | rd_cost->dist = distortion2; |
| 11875 | rd_cost->rdcost = this_rd; |
| 11876 | best_rd = this_rd; |
| 11877 | best_mbmode = *mbmi; |
| 11878 | best_skip2 = 0; |
| 11879 | best_mode_skippable = skippable; |
| 11880 | } |
| 11881 | } |
| 11882 | PALETTE_EXIT: |
| 11883 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11884 | #if CONFIG_FILTER_INTRA |
| 11885 | // TODO(huisu): filter-intra is turned off in lossless mode for now to |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11886 | // avoid a unit test failure |
Urvang Joshi | c6300aa | 2017-06-01 14:46:23 -0700 | [diff] [blame] | 11887 | if (!xd->lossless[mbmi->segment_id] && pmi->palette_size[0] == 0 && |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 11888 | !dc_skipped && best_mode_index >= 0 && |
| 11889 | best_intra_rd < (best_rd + (best_rd >> 3))) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11890 | pick_filter_intra_interframe( |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 11891 | cpi, x, bsize, mi_row, mi_col, rate_uv_intra, rate_uv_tokenonly, |
Jingning Han | 18c53c8 | 2017-02-17 14:49:57 -0800 | [diff] [blame] | 11892 | dist_uvs, skip_uvs, mode_uv, filter_intra_mode_info_uv, |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11893 | #if CONFIG_EXT_INTRA |
| 11894 | uv_angle_delta, |
| 11895 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | c6300aa | 2017-06-01 14:46:23 -0700 | [diff] [blame] | 11896 | pmi_uv, palette_ctx, 0, ref_costs_single, &best_rd, &best_intra_rd, |
| 11897 | &best_intra_mode, &best_mode_index, &best_skip2, &best_mode_skippable, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11898 | #if CONFIG_SUPERTX |
| 11899 | returnrate_nocoef, |
| 11900 | #endif // CONFIG_SUPERTX |
| 11901 | best_pred_rd, &best_mbmode, rd_cost); |
| 11902 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11903 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11904 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11905 | // The inter modes' rate costs are not calculated precisely in some cases. |
| 11906 | // Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and |
| 11907 | // ZEROMV. Here, checks are added for those cases, and the mode decisions |
| 11908 | // are corrected. |
| 11909 | #if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
| 11910 | // NOTE: For SR_NEW_NEWMV, no need to check as the two mvs from the same ref |
| 11911 | // are surely different from each other. |
| 11912 | #endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11913 | if (best_mbmode.mode == NEWMV |
| 11914 | #if CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11915 | || best_mbmode.mode == NEW_NEWMV |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11916 | #endif // CONFIG_EXT_INTER |
| 11917 | ) { |
| 11918 | const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0], |
| 11919 | best_mbmode.ref_frame[1] }; |
| 11920 | int comp_pred_mode = refs[1] > INTRA_FRAME; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11921 | int_mv zeromv[2]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11922 | const uint8_t rf_type = av1_ref_frame_type(best_mbmode.ref_frame); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11923 | #if CONFIG_GLOBAL_MOTION |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 11924 | zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]], |
| 11925 | cm->allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 11926 | mi_col, mi_row, 0 |
| 11927 | #if CONFIG_AMVR |
| 11928 | , |
| 11929 | cm->cur_frame_mv_precision_level |
| 11930 | #endif |
| 11931 | ) |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 11932 | .as_int; |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 11933 | zeromv[1].as_int = |
| 11934 | comp_pred_mode |
| 11935 | ? gm_get_motion_vector(&cm->global_motion[refs[1]], |
| 11936 | cm->allow_high_precision_mv, bsize, mi_col, |
| 11937 | mi_row, 0 |
| 11938 | #if CONFIG_AMVR |
| 11939 | , |
| 11940 | cm->cur_frame_mv_precision_level |
| 11941 | #endif |
| 11942 | ) |
| 11943 | .as_int |
| 11944 | : 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11945 | #else |
| 11946 | zeromv[0].as_int = 0; |
| 11947 | zeromv[1].as_int = 0; |
| 11948 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11949 | if (!comp_pred_mode) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11950 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11951 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11952 | : INT_MAX; |
| 11953 | |
| 11954 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 11955 | int_mv cur_mv = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 11956 | if (cur_mv.as_int == best_mbmode.mv[0].as_int) { |
| 11957 | best_mbmode.mode = NEARMV; |
| 11958 | best_mbmode.ref_mv_idx = i; |
| 11959 | } |
| 11960 | } |
| 11961 | |
| 11962 | if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int) |
| 11963 | best_mbmode.mode = NEARESTMV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11964 | else if (best_mbmode.mv[0].as_int == zeromv[0].as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11965 | best_mbmode.mode = ZEROMV; |
| 11966 | } else { |
| 11967 | int_mv nearestmv[2]; |
| 11968 | int_mv nearmv[2]; |
| 11969 | |
| 11970 | #if CONFIG_EXT_INTER |
| 11971 | if (mbmi_ext->ref_mv_count[rf_type] > 1) { |
| 11972 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][1].this_mv; |
| 11973 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][1].comp_mv; |
| 11974 | } else { |
| 11975 | nearmv[0] = frame_mv[NEARMV][refs[0]]; |
| 11976 | nearmv[1] = frame_mv[NEARMV][refs[1]]; |
| 11977 | } |
| 11978 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11979 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11980 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11981 | : INT_MAX; |
| 11982 | |
| 11983 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 11984 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 11985 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][i + 1].comp_mv; |
| 11986 | |
| 11987 | if (nearmv[0].as_int == best_mbmode.mv[0].as_int && |
| 11988 | nearmv[1].as_int == best_mbmode.mv[1].as_int) { |
| 11989 | best_mbmode.mode = NEARMV; |
| 11990 | best_mbmode.ref_mv_idx = i; |
| 11991 | } |
| 11992 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11993 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11994 | if (mbmi_ext->ref_mv_count[rf_type] >= 1) { |
| 11995 | nearestmv[0] = mbmi_ext->ref_mv_stack[rf_type][0].this_mv; |
| 11996 | nearestmv[1] = mbmi_ext->ref_mv_stack[rf_type][0].comp_mv; |
| 11997 | } else { |
| 11998 | nearestmv[0] = frame_mv[NEARESTMV][refs[0]]; |
| 11999 | nearestmv[1] = frame_mv[NEARESTMV][refs[1]]; |
| 12000 | } |
| 12001 | |
| 12002 | if (nearestmv[0].as_int == best_mbmode.mv[0].as_int && |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 12003 | nearestmv[1].as_int == best_mbmode.mv[1].as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12004 | #if CONFIG_EXT_INTER |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 12005 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12006 | best_mbmode.mode = NEAREST_NEARESTMV; |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 12007 | } else { |
| 12008 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
| 12009 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
| 12010 | : INT_MAX; |
| 12011 | |
| 12012 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 12013 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 12014 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][i + 1].comp_mv; |
| 12015 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 12016 | // Try switching to the NEAR_NEARMV mode |
| 12017 | if (nearmv[0].as_int == best_mbmode.mv[0].as_int && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 12018 | nearmv[1].as_int == best_mbmode.mv[1].as_int) { |
| 12019 | best_mbmode.mode = NEAR_NEARMV; |
| 12020 | best_mbmode.ref_mv_idx = i; |
| 12021 | } |
| 12022 | } |
| 12023 | |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 12024 | if (best_mbmode.mode == NEW_NEWMV && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 12025 | best_mbmode.mv[0].as_int == zeromv[0].as_int && |
| 12026 | best_mbmode.mv[1].as_int == zeromv[1].as_int) |
| 12027 | best_mbmode.mode = ZERO_ZEROMV; |
| 12028 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12029 | #else |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 12030 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12031 | best_mbmode.mode = NEARESTMV; |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 12032 | } else if (best_mbmode.mv[0].as_int == zeromv[0].as_int && |
| 12033 | best_mbmode.mv[1].as_int == zeromv[1].as_int) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12034 | best_mbmode.mode = ZEROMV; |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 12035 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12036 | #endif // CONFIG_EXT_INTER |
| 12037 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12038 | } |
| 12039 | |
David Barker | 9620bcd | 2017-03-22 14:46:42 +0000 | [diff] [blame] | 12040 | // Make sure that the ref_mv_idx is only nonzero when we're |
| 12041 | // using a mode which can support ref_mv_idx |
| 12042 | if (best_mbmode.ref_mv_idx != 0 && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 12043 | #if CONFIG_EXT_INTER |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 12044 | #if CONFIG_COMPOUND_SINGLEREF |
| 12045 | !(best_mbmode.mode == NEWMV || best_mbmode.mode == SR_NEW_NEWMV || |
| 12046 | best_mbmode.mode == NEW_NEWMV || |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 12047 | have_nearmv_in_inter_mode(best_mbmode.mode))) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 12048 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 12049 | !(best_mbmode.mode == NEWMV || best_mbmode.mode == NEW_NEWMV || |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 12050 | have_nearmv_in_inter_mode(best_mbmode.mode))) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 12051 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 12052 | #else // !CONFIG_EXT_INTER |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 12053 | !(best_mbmode.mode == NEARMV || best_mbmode.mode == NEWMV)) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 12054 | #endif // CONFIG_EXT_INTER |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 12055 | { |
David Barker | 9620bcd | 2017-03-22 14:46:42 +0000 | [diff] [blame] | 12056 | best_mbmode.ref_mv_idx = 0; |
| 12057 | } |
| 12058 | |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 12059 | { |
Jingning Han | 731af49 | 2016-11-17 11:53:23 -0800 | [diff] [blame] | 12060 | int8_t ref_frame_type = av1_ref_frame_type(best_mbmode.ref_frame); |
| 12061 | int16_t mode_ctx = mbmi_ext->mode_context[ref_frame_type]; |
David Barker | 68e6e86 | 2016-11-24 15:10:15 +0000 | [diff] [blame] | 12062 | if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) { |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 12063 | int_mv zeromv[2]; |
David Barker | 68e6e86 | 2016-11-24 15:10:15 +0000 | [diff] [blame] | 12064 | #if CONFIG_GLOBAL_MOTION |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 12065 | const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0], |
| 12066 | best_mbmode.ref_frame[1] }; |
| 12067 | zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]], |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 12068 | cm->allow_high_precision_mv, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 12069 | bsize, mi_col, mi_row, 0 |
| 12070 | #if CONFIG_AMVR |
| 12071 | , |
| 12072 | cm->cur_frame_mv_precision_level |
| 12073 | #endif |
| 12074 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 12075 | .as_int; |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 12076 | zeromv[1].as_int = |
| 12077 | (refs[1] != NONE_FRAME) |
| 12078 | ? |
| 12079 | #if CONFIG_AMVR |
| 12080 | gm_get_motion_vector(&cm->global_motion[refs[1]], |
| 12081 | cm->allow_high_precision_mv, bsize, mi_col, |
| 12082 | mi_row, 0, cm->cur_frame_mv_precision_level) |
| 12083 | .as_int |
| 12084 | : 0; |
| 12085 | #else |
| 12086 | gm_get_motion_vector(&cm->global_motion[refs[1]], |
| 12087 | cm->allow_high_precision_mv, bsize, mi_col, |
| 12088 | mi_row, 0) |
| 12089 | .as_int |
| 12090 | : 0; |
| 12091 | #endif |
| 12092 | |
| 12093 | #if CONFIG_AMVR |
| 12094 | lower_mv_precision(&zeromv[0].as_mv, cm->allow_high_precision_mv, |
| 12095 | cm->cur_frame_mv_precision_level); |
| 12096 | lower_mv_precision(&zeromv[1].as_mv, cm->allow_high_precision_mv, |
| 12097 | cm->cur_frame_mv_precision_level); |
| 12098 | #else |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 12099 | lower_mv_precision(&zeromv[0].as_mv, cm->allow_high_precision_mv); |
| 12100 | lower_mv_precision(&zeromv[1].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 12101 | #endif |
| 12102 | |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 12103 | #else |
| 12104 | zeromv[0].as_int = zeromv[1].as_int = 0; |
| 12105 | #endif // CONFIG_GLOBAL_MOTION |
| 12106 | if (best_mbmode.ref_frame[0] > INTRA_FRAME && |
| 12107 | best_mbmode.mv[0].as_int == zeromv[0].as_int && |
| 12108 | #if CONFIG_EXT_INTER |
| 12109 | (best_mbmode.ref_frame[1] <= INTRA_FRAME) |
| 12110 | #else |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 12111 | (best_mbmode.ref_frame[1] == NONE_FRAME || |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 12112 | best_mbmode.mv[1].as_int == zeromv[1].as_int) |
| 12113 | #endif // CONFIG_EXT_INTER |
| 12114 | ) { |
| 12115 | best_mbmode.mode = ZEROMV; |
| 12116 | } |
David Barker | 68e6e86 | 2016-11-24 15:10:15 +0000 | [diff] [blame] | 12117 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12118 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12119 | |
| 12120 | if (best_mode_index < 0 || best_rd >= best_rd_so_far) { |
| 12121 | rd_cost->rate = INT_MAX; |
| 12122 | rd_cost->rdcost = INT64_MAX; |
| 12123 | return; |
| 12124 | } |
| 12125 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12126 | #if CONFIG_DUAL_FILTER |
| 12127 | assert((cm->interp_filter == SWITCHABLE) || |
| 12128 | (cm->interp_filter == best_mbmode.interp_filter[0]) || |
| 12129 | !is_inter_block(&best_mbmode)); |
| 12130 | assert((cm->interp_filter == SWITCHABLE) || |
| 12131 | (cm->interp_filter == best_mbmode.interp_filter[1]) || |
| 12132 | !is_inter_block(&best_mbmode)); |
| 12133 | if (best_mbmode.ref_frame[1] > INTRA_FRAME) { |
| 12134 | assert((cm->interp_filter == SWITCHABLE) || |
| 12135 | (cm->interp_filter == best_mbmode.interp_filter[2]) || |
| 12136 | !is_inter_block(&best_mbmode)); |
| 12137 | assert((cm->interp_filter == SWITCHABLE) || |
| 12138 | (cm->interp_filter == best_mbmode.interp_filter[3]) || |
| 12139 | !is_inter_block(&best_mbmode)); |
| 12140 | } |
| 12141 | #else |
| 12142 | assert((cm->interp_filter == SWITCHABLE) || |
| 12143 | (cm->interp_filter == best_mbmode.interp_filter) || |
| 12144 | !is_inter_block(&best_mbmode)); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 12145 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12146 | |
| 12147 | if (!cpi->rc.is_src_frame_alt_ref) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12148 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
| 12149 | sf->adaptive_rd_thresh, bsize, best_mode_index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12150 | |
| 12151 | // macroblock modes |
| 12152 | *mbmi = best_mbmode; |
| 12153 | x->skip |= best_skip2; |
| 12154 | |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 12155 | // Note: this section is needed since the mode may have been forced to |
| 12156 | // ZEROMV by the all-zero mode handling of ref-mv. |
| 12157 | #if CONFIG_GLOBAL_MOTION |
| 12158 | if (mbmi->mode == ZEROMV |
| 12159 | #if CONFIG_EXT_INTER |
| 12160 | || mbmi->mode == ZERO_ZEROMV |
| 12161 | #endif // CONFIG_EXT_INTER |
| 12162 | ) { |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 12163 | #if CONFIG_WARPED_MOTION || CONFIG_MOTION_VAR |
| 12164 | // Correct the motion mode for ZEROMV |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 12165 | const MOTION_MODE last_motion_mode_allowed = |
Yue Chen | 52c5173 | 2017-07-11 15:08:30 -0700 | [diff] [blame] | 12166 | motion_mode_allowed(0, xd->global_motion, |
| 12167 | #if CONFIG_WARPED_MOTION |
| 12168 | xd, |
| 12169 | #endif |
| 12170 | xd->mi[0]); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 12171 | if (mbmi->motion_mode > last_motion_mode_allowed) |
| 12172 | mbmi->motion_mode = last_motion_mode_allowed; |
| 12173 | #endif // CONFIG_WARPED_MOTION || CONFIG_MOTION_VAR |
| 12174 | |
| 12175 | // Correct the interpolation filter for ZEROMV |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 12176 | if (is_nontrans_global_motion(xd)) { |
| 12177 | #if CONFIG_DUAL_FILTER |
| 12178 | mbmi->interp_filter[0] = cm->interp_filter == SWITCHABLE |
| 12179 | ? EIGHTTAP_REGULAR |
| 12180 | : cm->interp_filter; |
| 12181 | mbmi->interp_filter[1] = cm->interp_filter == SWITCHABLE |
| 12182 | ? EIGHTTAP_REGULAR |
| 12183 | : cm->interp_filter; |
| 12184 | #else |
| 12185 | mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP_REGULAR |
| 12186 | : cm->interp_filter; |
| 12187 | #endif // CONFIG_DUAL_FILTER |
| 12188 | } |
| 12189 | } |
| 12190 | #endif // CONFIG_GLOBAL_MOTION |
| 12191 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12192 | for (i = 0; i < 1 + has_second_ref(mbmi); ++i) { |
| 12193 | if (mbmi->mode != NEWMV) |
| 12194 | mbmi->pred_mv[i].as_int = mbmi->mv[i].as_int; |
| 12195 | else |
| 12196 | mbmi->pred_mv[i].as_int = mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_int; |
| 12197 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12198 | |
| 12199 | for (i = 0; i < REFERENCE_MODES; ++i) { |
| 12200 | if (best_pred_rd[i] == INT64_MAX) |
| 12201 | best_pred_diff[i] = INT_MIN; |
| 12202 | else |
| 12203 | best_pred_diff[i] = best_rd - best_pred_rd[i]; |
| 12204 | } |
| 12205 | |
| 12206 | x->skip |= best_mode_skippable; |
| 12207 | |
| 12208 | assert(best_mode_index >= 0); |
| 12209 | |
| 12210 | store_coding_context(x, ctx, best_mode_index, best_pred_diff, |
| 12211 | best_mode_skippable); |
| 12212 | |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 12213 | if (pmi->palette_size[1] > 0) { |
| 12214 | assert(try_palette); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12215 | restore_uv_color_map(cpi, x); |
| 12216 | } |
| 12217 | } |
| 12218 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 12219 | void av1_rd_pick_inter_mode_sb_seg_skip(const AV1_COMP *cpi, |
| 12220 | TileDataEnc *tile_data, MACROBLOCK *x, |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 12221 | int mi_row, int mi_col, |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 12222 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12223 | PICK_MODE_CONTEXT *ctx, |
| 12224 | int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 12225 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12226 | MACROBLOCKD *const xd = &x->e_mbd; |
| 12227 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 12228 | unsigned char segment_id = mbmi->segment_id; |
| 12229 | const int comp_pred = 0; |
| 12230 | int i; |
| 12231 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 12232 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 12233 | #if CONFIG_EXT_COMP_REFS |
| 12234 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME][TOTAL_REFS_PER_FRAME]; |
| 12235 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12236 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 12237 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12238 | aom_prob comp_mode_p; |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 12239 | InterpFilter best_filter = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12240 | int64_t this_rd = INT64_MAX; |
| 12241 | int rate2 = 0; |
| 12242 | const int64_t distortion2 = 0; |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 12243 | (void)mi_row; |
| 12244 | (void)mi_col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12245 | |
| 12246 | estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp, |
| 12247 | &comp_mode_p); |
| 12248 | |
| 12249 | for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX; |
| 12250 | for (i = LAST_FRAME; i < TOTAL_REFS_PER_FRAME; ++i) |
| 12251 | x->pred_mv_sad[i] = INT_MAX; |
| 12252 | |
| 12253 | rd_cost->rate = INT_MAX; |
| 12254 | |
| 12255 | assert(segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)); |
| 12256 | |
| 12257 | mbmi->palette_mode_info.palette_size[0] = 0; |
| 12258 | mbmi->palette_mode_info.palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 12259 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 12260 | #if CONFIG_FILTER_INTRA |
| 12261 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 12262 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 12263 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12264 | mbmi->mode = ZEROMV; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 12265 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 12266 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12267 | mbmi->ref_frame[0] = LAST_FRAME; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 12268 | mbmi->ref_frame[1] = NONE_FRAME; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 12269 | #if CONFIG_GLOBAL_MOTION |
| 12270 | mbmi->mv[0].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 12271 | gm_get_motion_vector(&cm->global_motion[mbmi->ref_frame[0]], |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 12272 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, 0 |
| 12273 | #if CONFIG_AMVR |
| 12274 | , |
| 12275 | cm->cur_frame_mv_precision_level |
| 12276 | #endif |
| 12277 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 12278 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 12279 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12280 | mbmi->mv[0].as_int = 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 12281 | #endif // CONFIG_GLOBAL_MOTION |
Jingning Han | 6408895 | 2016-07-11 11:24:24 -0700 | [diff] [blame] | 12282 | mbmi->tx_size = max_txsize_lookup[bsize]; |
Yaowu Xu | ee775b1 | 2016-10-18 10:00:21 -0700 | [diff] [blame] | 12283 | x->skip = 1; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 12284 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12285 | mbmi->ref_mv_idx = 0; |
| 12286 | mbmi->pred_mv[0].as_int = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12287 | |
Yue Chen | dead17d | 2017-04-20 16:19:39 -0700 | [diff] [blame] | 12288 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 12289 | #if CONFIG_MOTION_VAR |
| 12290 | av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col); |
| 12291 | #endif |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 12292 | #if CONFIG_WARPED_MOTION |
| 12293 | if (is_motion_variation_allowed_bsize(bsize) && !has_second_ref(mbmi)) { |
| 12294 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 12295 | #if WARPED_MOTION_SORT_SAMPLES |
| 12296 | int pts_mv[SAMPLES_ARRAY_SIZE]; |
| 12297 | mbmi->num_proj_ref[0] = |
| 12298 | findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv); |
| 12299 | // Rank the samples by motion vector difference |
| 12300 | if (mbmi->num_proj_ref[0] > 1) |
| 12301 | mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts, |
| 12302 | pts_inref, mbmi->num_proj_ref[0]); |
| 12303 | #else |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 12304 | mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref); |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 12305 | #endif // WARPED_MOTION_SORT_SAMPLES |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 12306 | } |
| 12307 | #endif |
Yue Chen | dead17d | 2017-04-20 16:19:39 -0700 | [diff] [blame] | 12308 | |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 12309 | set_default_interp_filters(mbmi, cm->interp_filter); |
| 12310 | |
| 12311 | if (cm->interp_filter != SWITCHABLE) { |
| 12312 | best_filter = cm->interp_filter; |
| 12313 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12314 | best_filter = EIGHTTAP_REGULAR; |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 12315 | if (av1_is_interp_needed(xd) && av1_is_interp_search_needed(xd) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12316 | x->source_variance >= cpi->sf.disable_filter_search_var_thresh) { |
| 12317 | int rs; |
| 12318 | int best_rs = INT_MAX; |
| 12319 | for (i = 0; i < SWITCHABLE_FILTERS; ++i) { |
| 12320 | #if CONFIG_DUAL_FILTER |
| 12321 | int k; |
| 12322 | for (k = 0; k < 4; ++k) mbmi->interp_filter[k] = i; |
| 12323 | #else |
| 12324 | mbmi->interp_filter = i; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 12325 | #endif // CONFIG_DUAL_FILTER |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 12326 | rs = av1_get_switchable_rate(cm, x, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12327 | if (rs < best_rs) { |
| 12328 | best_rs = rs; |
| 12329 | #if CONFIG_DUAL_FILTER |
| 12330 | best_filter = mbmi->interp_filter[0]; |
| 12331 | #else |
| 12332 | best_filter = mbmi->interp_filter; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 12333 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12334 | } |
| 12335 | } |
| 12336 | } |
| 12337 | } |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 12338 | // Set the appropriate filter |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12339 | #if CONFIG_DUAL_FILTER |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 12340 | for (i = 0; i < 4; ++i) mbmi->interp_filter[i] = best_filter; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12341 | #else |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 12342 | mbmi->interp_filter = best_filter; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 12343 | #endif // CONFIG_DUAL_FILTER |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 12344 | rate2 += av1_get_switchable_rate(cm, x, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12345 | |
| 12346 | if (cm->reference_mode == REFERENCE_MODE_SELECT) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12347 | rate2 += av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12348 | |
| 12349 | // Estimate the reference frame signaling cost and add it |
| 12350 | // to the rolling cost variable. |
| 12351 | rate2 += ref_costs_single[LAST_FRAME]; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12352 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12353 | |
| 12354 | rd_cost->rate = rate2; |
| 12355 | rd_cost->dist = distortion2; |
| 12356 | rd_cost->rdcost = this_rd; |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 12357 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12358 | if (this_rd >= best_rd_so_far) { |
| 12359 | rd_cost->rate = INT_MAX; |
| 12360 | rd_cost->rdcost = INT64_MAX; |
| 12361 | return; |
| 12362 | } |
| 12363 | |
| 12364 | #if CONFIG_DUAL_FILTER |
| 12365 | assert((cm->interp_filter == SWITCHABLE) || |
| 12366 | (cm->interp_filter == mbmi->interp_filter[0])); |
| 12367 | #else |
| 12368 | assert((cm->interp_filter == SWITCHABLE) || |
| 12369 | (cm->interp_filter == mbmi->interp_filter)); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 12370 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12371 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12372 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
| 12373 | cpi->sf.adaptive_rd_thresh, bsize, THR_ZEROMV); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12374 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12375 | av1_zero(best_pred_diff); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12376 | |
| 12377 | store_coding_context(x, ctx, THR_ZEROMV, best_pred_diff, 0); |
| 12378 | } |
| 12379 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 12380 | #if CONFIG_MOTION_VAR |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12381 | |
| 12382 | struct calc_target_weighted_pred_ctxt { |
| 12383 | const MACROBLOCK *x; |
| 12384 | const uint8_t *tmp; |
| 12385 | int tmp_stride; |
| 12386 | int overlap; |
| 12387 | }; |
| 12388 | |
| 12389 | static INLINE void calc_target_weighted_pred_above(MACROBLOCKD *xd, |
| 12390 | int rel_mi_col, |
| 12391 | uint8_t nb_mi_width, |
| 12392 | MODE_INFO *nb_mi, |
| 12393 | void *fun_ctxt) { |
| 12394 | (void)nb_mi; |
| 12395 | |
| 12396 | struct calc_target_weighted_pred_ctxt *ctxt = |
| 12397 | (struct calc_target_weighted_pred_ctxt *)fun_ctxt; |
| 12398 | |
| 12399 | #if CONFIG_HIGHBITDEPTH |
| 12400 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 12401 | #else |
| 12402 | const int is_hbd = 0; |
| 12403 | #endif // CONFIG_HIGHBITDEPTH |
| 12404 | |
| 12405 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 12406 | const uint8_t *const mask1d = av1_get_obmc_mask(ctxt->overlap); |
| 12407 | |
| 12408 | int32_t *wsrc = ctxt->x->wsrc_buf + (rel_mi_col * MI_SIZE); |
| 12409 | int32_t *mask = ctxt->x->mask_buf + (rel_mi_col * MI_SIZE); |
| 12410 | const uint8_t *tmp = ctxt->tmp + rel_mi_col * MI_SIZE; |
| 12411 | |
| 12412 | if (!is_hbd) { |
| 12413 | for (int row = 0; row < ctxt->overlap; ++row) { |
| 12414 | const uint8_t m0 = mask1d[row]; |
| 12415 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 12416 | for (int col = 0; col < nb_mi_width * MI_SIZE; ++col) { |
| 12417 | wsrc[col] = m1 * tmp[col]; |
| 12418 | mask[col] = m0; |
| 12419 | } |
| 12420 | wsrc += bw; |
| 12421 | mask += bw; |
| 12422 | tmp += ctxt->tmp_stride; |
| 12423 | } |
| 12424 | #if CONFIG_HIGHBITDEPTH |
| 12425 | } else { |
| 12426 | const uint16_t *tmp16 = CONVERT_TO_SHORTPTR(tmp); |
| 12427 | |
| 12428 | for (int row = 0; row < ctxt->overlap; ++row) { |
| 12429 | const uint8_t m0 = mask1d[row]; |
| 12430 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 12431 | for (int col = 0; col < nb_mi_width * MI_SIZE; ++col) { |
| 12432 | wsrc[col] = m1 * tmp16[col]; |
| 12433 | mask[col] = m0; |
| 12434 | } |
| 12435 | wsrc += bw; |
| 12436 | mask += bw; |
| 12437 | tmp16 += ctxt->tmp_stride; |
| 12438 | } |
| 12439 | #endif // CONFIG_HIGHBITDEPTH |
| 12440 | } |
| 12441 | } |
| 12442 | |
| 12443 | static INLINE void calc_target_weighted_pred_left(MACROBLOCKD *xd, |
| 12444 | int rel_mi_row, |
| 12445 | uint8_t nb_mi_height, |
| 12446 | MODE_INFO *nb_mi, |
| 12447 | void *fun_ctxt) { |
| 12448 | (void)nb_mi; |
| 12449 | |
| 12450 | struct calc_target_weighted_pred_ctxt *ctxt = |
| 12451 | (struct calc_target_weighted_pred_ctxt *)fun_ctxt; |
| 12452 | |
| 12453 | #if CONFIG_HIGHBITDEPTH |
| 12454 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 12455 | #else |
| 12456 | const int is_hbd = 0; |
| 12457 | #endif // CONFIG_HIGHBITDEPTH |
| 12458 | |
| 12459 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 12460 | const uint8_t *const mask1d = av1_get_obmc_mask(ctxt->overlap); |
| 12461 | |
| 12462 | int32_t *wsrc = ctxt->x->wsrc_buf + (rel_mi_row * MI_SIZE * bw); |
| 12463 | int32_t *mask = ctxt->x->mask_buf + (rel_mi_row * MI_SIZE * bw); |
| 12464 | const uint8_t *tmp = ctxt->tmp + (rel_mi_row * MI_SIZE * ctxt->tmp_stride); |
| 12465 | |
| 12466 | if (!is_hbd) { |
| 12467 | for (int row = 0; row < nb_mi_height * MI_SIZE; ++row) { |
| 12468 | for (int col = 0; col < ctxt->overlap; ++col) { |
| 12469 | const uint8_t m0 = mask1d[col]; |
| 12470 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 12471 | wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 + |
| 12472 | (tmp[col] << AOM_BLEND_A64_ROUND_BITS) * m1; |
| 12473 | mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0; |
| 12474 | } |
| 12475 | wsrc += bw; |
| 12476 | mask += bw; |
| 12477 | tmp += ctxt->tmp_stride; |
| 12478 | } |
| 12479 | #if CONFIG_HIGHBITDEPTH |
| 12480 | } else { |
| 12481 | const uint16_t *tmp16 = CONVERT_TO_SHORTPTR(tmp); |
| 12482 | |
| 12483 | for (int row = 0; row < nb_mi_height * MI_SIZE; ++row) { |
| 12484 | for (int col = 0; col < ctxt->overlap; ++col) { |
| 12485 | const uint8_t m0 = mask1d[col]; |
| 12486 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 12487 | wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 + |
| 12488 | (tmp16[col] << AOM_BLEND_A64_ROUND_BITS) * m1; |
| 12489 | mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0; |
| 12490 | } |
| 12491 | wsrc += bw; |
| 12492 | mask += bw; |
| 12493 | tmp16 += ctxt->tmp_stride; |
| 12494 | } |
| 12495 | #endif // CONFIG_HIGHBITDEPTH |
| 12496 | } |
| 12497 | } |
| 12498 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12499 | // This function has a structure similar to av1_build_obmc_inter_prediction |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12500 | // |
| 12501 | // The OBMC predictor is computed as: |
| 12502 | // |
| 12503 | // PObmc(x,y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12504 | // AOM_BLEND_A64(Mh(x), |
| 12505 | // AOM_BLEND_A64(Mv(y), P(x,y), PAbove(x,y)), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12506 | // PLeft(x, y)) |
| 12507 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12508 | // 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] | 12509 | // rounding, this can be written as: |
| 12510 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12511 | // 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] | 12512 | // Mh(x) * Mv(y) * P(x,y) + |
| 12513 | // Mh(x) * Cv(y) * Pabove(x,y) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12514 | // AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12515 | // |
| 12516 | // Where : |
| 12517 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12518 | // Cv(y) = AOM_BLEND_A64_MAX_ALPHA - Mv(y) |
| 12519 | // Ch(y) = AOM_BLEND_A64_MAX_ALPHA - Mh(y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12520 | // |
| 12521 | // This function computes 'wsrc' and 'mask' as: |
| 12522 | // |
| 12523 | // wsrc(x, y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12524 | // 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] | 12525 | // Mh(x) * Cv(y) * Pabove(x,y) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12526 | // AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12527 | // |
| 12528 | // mask(x, y) = Mh(x) * Mv(y) |
| 12529 | // |
| 12530 | // These can then be used to efficiently approximate the error for any |
| 12531 | // predictor P in the context of the provided neighbouring predictors by |
| 12532 | // computing: |
| 12533 | // |
| 12534 | // error(x, y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12535 | // 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] | 12536 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12537 | 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] | 12538 | const MACROBLOCKD *xd, int mi_row, |
| 12539 | int mi_col, const uint8_t *above, |
| 12540 | int above_stride, const uint8_t *left, |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 12541 | int left_stride) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12542 | const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type; |
Jingning Han | ff6ee6a | 2016-12-07 09:55:21 -0800 | [diff] [blame] | 12543 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 12544 | const int bh = xd->n8_h << MI_SIZE_LOG2; |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 12545 | int32_t *mask_buf = x->mask_buf; |
| 12546 | int32_t *wsrc_buf = x->wsrc_buf; |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12547 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12548 | 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] | 12549 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12550 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 12551 | #else |
| 12552 | const int is_hbd = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 12553 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12554 | |
| 12555 | // plane 0 should not be subsampled |
| 12556 | assert(xd->plane[0].subsampling_x == 0); |
| 12557 | assert(xd->plane[0].subsampling_y == 0); |
| 12558 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12559 | av1_zero_array(wsrc_buf, bw * bh); |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12560 | 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] | 12561 | |
| 12562 | // handle above row |
| 12563 | if (xd->up_available) { |
Jingning Han | 440d425 | 2017-07-24 14:07:34 -0700 | [diff] [blame] | 12564 | const int overlap = |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12565 | AOMMIN(block_size_high[bsize], block_size_high[BLOCK_64X64]) >> 1; |
| 12566 | struct calc_target_weighted_pred_ctxt ctxt = { x, above, above_stride, |
| 12567 | overlap }; |
| 12568 | foreach_overlappable_nb_above(cm, (MACROBLOCKD *)xd, mi_col, |
| 12569 | max_neighbor_obmc[b_width_log2_lookup[bsize]], |
| 12570 | calc_target_weighted_pred_above, &ctxt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12571 | } |
| 12572 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12573 | for (int i = 0; i < bw * bh; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12574 | wsrc_buf[i] *= AOM_BLEND_A64_MAX_ALPHA; |
| 12575 | mask_buf[i] *= AOM_BLEND_A64_MAX_ALPHA; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12576 | } |
| 12577 | |
| 12578 | // handle left column |
| 12579 | if (xd->left_available) { |
Jingning Han | 440d425 | 2017-07-24 14:07:34 -0700 | [diff] [blame] | 12580 | const int overlap = |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12581 | AOMMIN(block_size_wide[bsize], block_size_wide[BLOCK_64X64]) >> 1; |
| 12582 | struct calc_target_weighted_pred_ctxt ctxt = { x, left, left_stride, |
| 12583 | overlap }; |
| 12584 | foreach_overlappable_nb_left(cm, (MACROBLOCKD *)xd, mi_row, |
| 12585 | max_neighbor_obmc[b_height_log2_lookup[bsize]], |
| 12586 | calc_target_weighted_pred_left, &ctxt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12587 | } |
| 12588 | |
| 12589 | if (!is_hbd) { |
| 12590 | const uint8_t *src = x->plane[0].src.buf; |
| 12591 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12592 | for (int row = 0; row < bh; ++row) { |
| 12593 | for (int col = 0; col < bw; ++col) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12594 | wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col]; |
| 12595 | } |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12596 | wsrc_buf += bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12597 | src += x->plane[0].src.stride; |
| 12598 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 12599 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12600 | } else { |
| 12601 | const uint16_t *src = CONVERT_TO_SHORTPTR(x->plane[0].src.buf); |
| 12602 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12603 | for (int row = 0; row < bh; ++row) { |
| 12604 | for (int col = 0; col < bw; ++col) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12605 | wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col]; |
| 12606 | } |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12607 | wsrc_buf += bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12608 | src += x->plane[0].src.stride; |
| 12609 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 12610 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12611 | } |
| 12612 | } |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12613 | |
| 12614 | #if CONFIG_NCOBMC |
| 12615 | void av1_check_ncobmc_rd(const struct AV1_COMP *cpi, struct macroblock *x, |
| 12616 | int mi_row, int mi_col) { |
| 12617 | const AV1_COMMON *const cm = &cpi->common; |
| 12618 | MACROBLOCKD *const xd = &x->e_mbd; |
| 12619 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 12620 | MB_MODE_INFO backup_mbmi; |
| 12621 | BLOCK_SIZE bsize = mbmi->sb_type; |
| 12622 | int ref, skip_blk, backup_skip = x->skip; |
| 12623 | int64_t rd_causal; |
| 12624 | RD_STATS rd_stats_y, rd_stats_uv; |
| 12625 | int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 12626 | int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 12627 | |
| 12628 | // Recompute the best causal predictor and rd |
| 12629 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 12630 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 12631 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 12632 | YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mbmi->ref_frame[ref]); |
| 12633 | assert(cfg != NULL); |
| 12634 | av1_setup_pre_planes(xd, ref, cfg, mi_row, mi_col, |
| 12635 | &xd->block_refs[ref]->sf); |
| 12636 | } |
Jingning Han | 91d9a79 | 2017-04-18 12:01:52 -0700 | [diff] [blame] | 12637 | av1_setup_dst_planes(x->e_mbd.plane, bsize, |
| 12638 | get_frame_new_buffer(&cpi->common), mi_row, mi_col); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12639 | |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 12640 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12641 | |
| 12642 | av1_subtract_plane(x, bsize, 0); |
Wei-Ting Lin | ba5f187 | 2017-07-06 12:26:09 -0700 | [diff] [blame] | 12643 | #if CONFIG_VAR_TX |
| 12644 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
| 12645 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12646 | } else { |
| 12647 | int idx, idy; |
| 12648 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12649 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 12650 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 12651 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 12652 | memset(x->blk_skip[0], rd_stats_y.skip, |
| 12653 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 12654 | } |
| 12655 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 12656 | #else |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12657 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12658 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Wei-Ting Lin | ba5f187 | 2017-07-06 12:26:09 -0700 | [diff] [blame] | 12659 | #endif |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12660 | assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX); |
| 12661 | if (rd_stats_y.skip && rd_stats_uv.skip) { |
| 12662 | rd_stats_y.rate = rate_skip1; |
| 12663 | rd_stats_uv.rate = 0; |
| 12664 | rd_stats_y.dist = rd_stats_y.sse; |
| 12665 | rd_stats_uv.dist = rd_stats_uv.sse; |
| 12666 | skip_blk = 0; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12667 | } else if (RDCOST(x->rdmult, |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12668 | (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0), |
| 12669 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12670 | RDCOST(x->rdmult, rate_skip1, |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12671 | (rd_stats_y.sse + rd_stats_uv.sse))) { |
| 12672 | rd_stats_y.rate = rate_skip1; |
| 12673 | rd_stats_uv.rate = 0; |
| 12674 | rd_stats_y.dist = rd_stats_y.sse; |
| 12675 | rd_stats_uv.dist = rd_stats_uv.sse; |
| 12676 | skip_blk = 1; |
| 12677 | } else { |
| 12678 | rd_stats_y.rate += rate_skip0; |
| 12679 | skip_blk = 0; |
| 12680 | } |
| 12681 | backup_skip = skip_blk; |
| 12682 | backup_mbmi = *mbmi; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12683 | rd_causal = RDCOST(x->rdmult, (rd_stats_y.rate + rd_stats_uv.rate), |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12684 | (rd_stats_y.dist + rd_stats_uv.dist)); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12685 | rd_causal += |
| 12686 | RDCOST(x->rdmult, av1_cost_bit(cm->fc->motion_mode_prob[bsize][0], 0), 0); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12687 | |
| 12688 | // Check non-causal mode |
| 12689 | mbmi->motion_mode = OBMC_CAUSAL; |
| 12690 | av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col); |
| 12691 | |
| 12692 | av1_subtract_plane(x, bsize, 0); |
Wei-Ting Lin | ba5f187 | 2017-07-06 12:26:09 -0700 | [diff] [blame] | 12693 | #if CONFIG_VAR_TX |
| 12694 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
| 12695 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12696 | } else { |
| 12697 | int idx, idy; |
| 12698 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12699 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 12700 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 12701 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 12702 | memset(x->blk_skip[0], rd_stats_y.skip, |
| 12703 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 12704 | } |
| 12705 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 12706 | #else |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12707 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12708 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Wei-Ting Lin | ba5f187 | 2017-07-06 12:26:09 -0700 | [diff] [blame] | 12709 | #endif |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12710 | assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX); |
| 12711 | if (rd_stats_y.skip && rd_stats_uv.skip) { |
| 12712 | rd_stats_y.rate = rate_skip1; |
| 12713 | rd_stats_uv.rate = 0; |
| 12714 | rd_stats_y.dist = rd_stats_y.sse; |
| 12715 | rd_stats_uv.dist = rd_stats_uv.sse; |
| 12716 | skip_blk = 0; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12717 | } else if (RDCOST(x->rdmult, |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12718 | (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0), |
| 12719 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12720 | RDCOST(x->rdmult, rate_skip1, |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12721 | (rd_stats_y.sse + rd_stats_uv.sse))) { |
| 12722 | rd_stats_y.rate = rate_skip1; |
| 12723 | rd_stats_uv.rate = 0; |
| 12724 | rd_stats_y.dist = rd_stats_y.sse; |
| 12725 | rd_stats_uv.dist = rd_stats_uv.sse; |
| 12726 | skip_blk = 1; |
| 12727 | } else { |
| 12728 | rd_stats_y.rate += rate_skip0; |
| 12729 | skip_blk = 0; |
| 12730 | } |
| 12731 | |
| 12732 | if (rd_causal > |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 12733 | RDCOST(x->rdmult, |
| 12734 | rd_stats_y.rate + rd_stats_uv.rate + |
| 12735 | av1_cost_bit(cm->fc->motion_mode_prob[bsize][0], 1), |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12736 | (rd_stats_y.dist + rd_stats_uv.dist))) { |
| 12737 | x->skip = skip_blk; |
| 12738 | } else { |
| 12739 | *mbmi = backup_mbmi; |
| 12740 | x->skip = backup_skip; |
| 12741 | } |
| 12742 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 12743 | #endif // CONFIG_NCOBMC |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12744 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12745 | int64_t get_prediction_rd_cost(const struct AV1_COMP *cpi, struct macroblock *x, |
| 12746 | int mi_row, int mi_col, int *skip_blk, |
| 12747 | MB_MODE_INFO *backup_mbmi) { |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12748 | const AV1_COMMON *const cm = &cpi->common; |
| 12749 | MACROBLOCKD *const xd = &x->e_mbd; |
| 12750 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 12751 | BLOCK_SIZE bsize = mbmi->sb_type; |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12752 | #if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
| 12753 | const MOTION_MODE motion_allowed = motion_mode_allowed( |
| 12754 | #if CONFIG_GLOBAL_MOTION |
| 12755 | 0, xd->global_motion, |
| 12756 | #endif // CONFIG_GLOBAL_MOTION |
| 12757 | #if CONFIG_WARPED_MOTION |
| 12758 | xd, |
| 12759 | #endif |
| 12760 | xd->mi[0]); |
| 12761 | #endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12762 | RD_STATS rd_stats_y, rd_stats_uv; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12763 | int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 12764 | int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12765 | int64_t this_rd; |
| 12766 | int ref; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12767 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12768 | #if CONFIG_CB4X4 |
| 12769 | x->skip_chroma_rd = |
| 12770 | !is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x, |
| 12771 | xd->plane[1].subsampling_y); |
| 12772 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12773 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12774 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 12775 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 12776 | YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mbmi->ref_frame[ref]); |
| 12777 | assert(cfg != NULL); |
| 12778 | av1_setup_pre_planes(xd, ref, cfg, mi_row, mi_col, |
| 12779 | &xd->block_refs[ref]->sf); |
| 12780 | } |
| 12781 | av1_setup_dst_planes(x->e_mbd.plane, bsize, |
| 12782 | get_frame_new_buffer(&cpi->common), mi_row, mi_col); |
| 12783 | |
| 12784 | #if CONFIG_NCOBMC_ADAPT_WEIGHT |
| 12785 | if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT) |
| 12786 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12787 | av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize); |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12788 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12789 | #if CONFIG_MOTION_VAR |
| 12790 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
| 12791 | #if CONFIG_NCOBMC |
| 12792 | av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col); |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12793 | #else |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12794 | av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col); |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12795 | #endif |
| 12796 | } |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12797 | #endif // CONFIG_MOTION_VAR |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12798 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12799 | #if CONFIG_NCOBMC_ADAPT_WEIGHT |
| 12800 | if (mbmi->motion_mode == NCOBMC_ADAPT_WEIGHT) |
| 12801 | for (int plane = 0; plane < MAX_MB_PLANE; ++plane) |
| 12802 | get_pred_from_intrpl_buf(xd, mi_row, mi_col, bsize, plane); |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12803 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12804 | av1_subtract_plane(x, bsize, 0); |
| 12805 | |
| 12806 | #if CONFIG_VAR_TX |
| 12807 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
| 12808 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12809 | } else { |
| 12810 | int idx, idy; |
| 12811 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12812 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 12813 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 12814 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 12815 | memset(x->blk_skip[0], rd_stats_y.skip, |
| 12816 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 12817 | } |
| 12818 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 12819 | #else |
| 12820 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12821 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 12822 | #endif |
| 12823 | assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX); |
| 12824 | |
| 12825 | if (rd_stats_y.skip && rd_stats_uv.skip) { |
| 12826 | rd_stats_y.rate = rate_skip1; |
| 12827 | rd_stats_uv.rate = 0; |
| 12828 | rd_stats_y.dist = rd_stats_y.sse; |
| 12829 | rd_stats_uv.dist = rd_stats_uv.sse; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12830 | *skip_blk = 1; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12831 | } else if (RDCOST(x->rdmult, |
| 12832 | (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0), |
| 12833 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
| 12834 | RDCOST(x->rdmult, rate_skip1, |
| 12835 | (rd_stats_y.sse + rd_stats_uv.sse))) { |
| 12836 | rd_stats_y.rate = rate_skip1; |
| 12837 | rd_stats_uv.rate = 0; |
| 12838 | rd_stats_y.dist = rd_stats_y.sse; |
| 12839 | rd_stats_uv.dist = rd_stats_uv.sse; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12840 | *skip_blk = 1; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12841 | } else { |
| 12842 | rd_stats_y.rate += rate_skip0; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12843 | *skip_blk = 0; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12844 | } |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12845 | |
| 12846 | if (backup_mbmi) *backup_mbmi = *mbmi; |
| 12847 | |
| 12848 | this_rd = RDCOST(x->rdmult, (rd_stats_y.rate + rd_stats_uv.rate), |
| 12849 | (rd_stats_y.dist + rd_stats_uv.dist)); |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12850 | #if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
| 12851 | if (motion_allowed == NCOBMC_ADAPT_WEIGHT) { |
| 12852 | assert(mbmi->motion_mode <= NCOBMC_ADAPT_WEIGHT); |
| 12853 | this_rd += |
| 12854 | RDCOST(x->rdmult, x->motion_mode_cost2[bsize][mbmi->motion_mode], 0); |
| 12855 | } else if (motion_allowed == OBMC_CAUSAL) { |
| 12856 | assert(mbmi->motion_mode <= OBMC_CAUSAL); |
| 12857 | this_rd += |
| 12858 | RDCOST(x->rdmult, x->motion_mode_cost1[bsize][mbmi->motion_mode], 0); |
| 12859 | } else { |
| 12860 | #endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
| 12861 | this_rd += |
| 12862 | RDCOST(x->rdmult, x->motion_mode_cost[bsize][mbmi->motion_mode], 0); |
| 12863 | #if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
| 12864 | } |
| 12865 | #endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12866 | return this_rd; |
| 12867 | } |
| 12868 | |
| 12869 | #if CONFIG_NCOBMC_ADAPT_WEIGHT |
| 12870 | void av1_check_ncobmc_adapt_weight_rd(const struct AV1_COMP *cpi, |
| 12871 | struct macroblock *x, int mi_row, |
| 12872 | int mi_col) { |
| 12873 | MACROBLOCKD *const xd = &x->e_mbd; |
| 12874 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 12875 | BLOCK_SIZE bsize = mbmi->sb_type; |
| 12876 | #if CONFIG_VAR_TX |
| 12877 | const int n4 = bsize_to_num_blk(bsize); |
| 12878 | uint8_t st_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
| 12879 | uint8_t obmc_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12880 | uint8_t ncobmc_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12881 | #endif |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12882 | MB_MODE_INFO st_mbmi, obmc_mbmi, ncobmc_mbmi; |
| 12883 | int st_skip, obmc_skip, ncobmc_skip; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12884 | int64_t st_rd, obmc_rd, ncobmc_rd; |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12885 | #if CONFIG_WARPED_MOTION |
| 12886 | const AV1_COMMON *const cm = &cpi->common; |
| 12887 | const int is_warp_motion = mbmi->motion_mode == WARPED_CAUSAL; |
| 12888 | const int rs = RDCOST(x->rdmult, av1_get_switchable_rate(cm, x, xd), 0); |
| 12889 | MB_MODE_INFO warp_mbmi; |
| 12890 | int64_t warp_rd; |
| 12891 | int warp_skip; |
| 12892 | #endif |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12893 | |
| 12894 | // Recompute the rd for the motion mode decided in rd loop |
| 12895 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 12896 | st_rd = get_prediction_rd_cost(cpi, x, mi_row, mi_col, &st_skip, &st_mbmi); |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12897 | #if CONFIG_WARPED_MOTION |
| 12898 | st_rd += rs; |
| 12899 | #endif |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12900 | #if CONFIG_VAR_TX |
| 12901 | memcpy(st_blk_skip, x->blk_skip[0], sizeof(st_blk_skip[0]) * n4); |
| 12902 | #endif |
| 12903 | |
| 12904 | mbmi->motion_mode = OBMC_CAUSAL; |
| 12905 | obmc_rd = |
| 12906 | get_prediction_rd_cost(cpi, x, mi_row, mi_col, &obmc_skip, &obmc_mbmi); |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12907 | #if CONFIG_WARPED_MOTION |
| 12908 | obmc_rd += rs; |
| 12909 | #endif |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12910 | #if CONFIG_VAR_TX |
| 12911 | memcpy(obmc_blk_skip, x->blk_skip[0], sizeof(obmc_blk_skip[0]) * n4); |
| 12912 | #endif |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12913 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12914 | // Compute the rd cost for ncobmc adaptive weight |
| 12915 | mbmi->motion_mode = NCOBMC_ADAPT_WEIGHT; |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12916 | ncobmc_rd = get_prediction_rd_cost(cpi, x, mi_row, mi_col, &ncobmc_skip, |
| 12917 | &ncobmc_mbmi); |
| 12918 | #if CONFIG_WARPED_MOTION |
| 12919 | ncobmc_rd += rs; |
| 12920 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12921 | // Calculate the ncobmc mode costs |
| 12922 | { |
| 12923 | ADAPT_OVERLAP_BLOCK aob = adapt_overlap_block_lookup[bsize]; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12924 | ncobmc_rd += |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12925 | RDCOST(x->rdmult, x->ncobmc_mode_cost[aob][mbmi->ncobmc_mode[0]], 0); |
| 12926 | if (mi_size_wide[bsize] != mi_size_high[bsize]) |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12927 | ncobmc_rd += |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12928 | RDCOST(x->rdmult, x->ncobmc_mode_cost[aob][mbmi->ncobmc_mode[1]], 0); |
| 12929 | } |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12930 | #if CONFIG_VAR_TX |
| 12931 | memcpy(ncobmc_blk_skip, x->blk_skip[0], sizeof(ncobmc_blk_skip[0]) * n4); |
| 12932 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12933 | |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12934 | #if CONFIG_WARPED_MOTION |
| 12935 | if (is_warp_motion) { |
| 12936 | mbmi->motion_mode = WARPED_CAUSAL; |
| 12937 | warp_rd = |
| 12938 | get_prediction_rd_cost(cpi, x, mi_row, mi_col, &warp_skip, &warp_mbmi); |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12939 | } else { |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12940 | warp_rd = INT64_MAX; |
| 12941 | } |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12942 | #endif |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12943 | |
| 12944 | #if CONFIG_WARPED_MOTION |
| 12945 | if (AOMMIN(ncobmc_rd, warp_rd) < AOMMIN(st_rd, obmc_rd)) { |
| 12946 | if (ncobmc_rd < warp_rd) { |
| 12947 | x->skip = ncobmc_skip; |
| 12948 | *mbmi = ncobmc_mbmi; |
| 12949 | #if CONFIG_VAR_TX |
| 12950 | memcpy(x->blk_skip[0], ncobmc_blk_skip, sizeof(ncobmc_blk_skip[0]) * n4); |
| 12951 | #endif |
| 12952 | } else { |
| 12953 | x->skip = warp_skip; |
| 12954 | *mbmi = warp_mbmi; |
| 12955 | } |
| 12956 | #else |
| 12957 | if (ncobmc_rd < AOMMIN(st_rd, obmc_rd)) { |
| 12958 | x->skip = ncobmc_skip; |
| 12959 | *mbmi = ncobmc_mbmi; |
| 12960 | #if CONFIG_VAR_TX |
| 12961 | memcpy(x->blk_skip[0], ncobmc_blk_skip, sizeof(ncobmc_blk_skip[0]) * n4); |
| 12962 | #endif |
| 12963 | #endif // CONFIG_WARPED_MOTION |
| 12964 | } else { |
| 12965 | if (obmc_rd < st_rd) { |
| 12966 | *mbmi = obmc_mbmi; |
| 12967 | x->skip = obmc_skip; |
| 12968 | #if CONFIG_VAR_TX |
| 12969 | memcpy(x->blk_skip[0], obmc_blk_skip, sizeof(obmc_blk_skip[0]) * n4); |
| 12970 | #endif |
| 12971 | } else { |
| 12972 | *mbmi = st_mbmi; |
| 12973 | x->skip = st_skip; |
| 12974 | #if CONFIG_VAR_TX |
| 12975 | memcpy(x->blk_skip[0], st_blk_skip, sizeof(st_blk_skip[0]) * n4); |
| 12976 | #endif |
| 12977 | } |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12978 | } |
| 12979 | } |
| 12980 | |
| 12981 | int64_t get_ncobmc_error(MACROBLOCKD *xd, int pxl_row, int pxl_col, |
| 12982 | BLOCK_SIZE bsize, int plane, struct buf_2d *src) { |
| 12983 | const int wide = AOMMIN(mi_size_wide[bsize] * MI_SIZE, |
| 12984 | (xd->sb_mi_bd.mi_col_end + 1) * MI_SIZE - pxl_col); |
| 12985 | const int high = AOMMIN(mi_size_high[bsize] * MI_SIZE, |
| 12986 | (xd->sb_mi_bd.mi_row_end + 1) * MI_SIZE - pxl_row); |
| 12987 | const int ss_x = xd->plane[plane].subsampling_x; |
| 12988 | const int ss_y = xd->plane[plane].subsampling_y; |
| 12989 | int row_offset = (pxl_row - xd->sb_mi_bd.mi_row_begin * MI_SIZE) >> ss_y; |
| 12990 | int col_offset = (pxl_col - xd->sb_mi_bd.mi_col_begin * MI_SIZE) >> ss_x; |
| 12991 | int dst_stride = xd->ncobmc_pred_buf_stride[plane]; |
| 12992 | int dst_offset = row_offset * dst_stride + col_offset; |
| 12993 | int src_stride = src->stride; |
| 12994 | |
| 12995 | int r, c; |
| 12996 | int64_t tmp, error = 0; |
| 12997 | |
| 12998 | for (r = 0; r < (high >> ss_y); ++r) { |
| 12999 | for (c = 0; c < (wide >> ss_x); ++c) { |
| 13000 | tmp = xd->ncobmc_pred_buf[plane][r * dst_stride + c + dst_offset] - |
| 13001 | src->buf[r * src_stride + c]; |
| 13002 | error += tmp * tmp; |
| 13003 | } |
| 13004 | } |
| 13005 | return error; |
| 13006 | } |
| 13007 | |
| 13008 | int get_ncobmc_mode(const AV1_COMP *const cpi, MACROBLOCK *const x, |
| 13009 | MACROBLOCKD *xd, int mi_row, int mi_col, int bsize) { |
| 13010 | const AV1_COMMON *const cm = &cpi->common; |
| 13011 | #if CONFIG_HIGHBITDEPTH |
| 13012 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_0[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 13013 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 13014 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_2[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 13015 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_3[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 13016 | #else |
| 13017 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_0[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 13018 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_1[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 13019 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_2[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 13020 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_3[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 13021 | #endif |
| 13022 | uint8_t *pred_buf[4][MAX_MB_PLANE]; |
| 13023 | |
| 13024 | // TODO(weitinglin): stride size needs to be fixed for high-bit depth |
| 13025 | int pred_stride[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 13026 | |
| 13027 | // target block in pxl |
| 13028 | int pxl_row = mi_row << MI_SIZE_LOG2; |
| 13029 | int pxl_col = mi_col << MI_SIZE_LOG2; |
| 13030 | int64_t error, best_error = INT64_MAX; |
| 13031 | int plane, tmp_mode, best_mode = 0; |
| 13032 | #if CONFIG_HIGHBITDEPTH |
| 13033 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 13034 | int len = sizeof(uint16_t); |
| 13035 | ASSIGN_ALIGNED_PTRS_HBD(pred_buf[0], tmp_buf_0, MAX_SB_SQUARE, len); |
| 13036 | ASSIGN_ALIGNED_PTRS_HBD(pred_buf[1], tmp_buf_0, MAX_SB_SQUARE, len); |
| 13037 | ASSIGN_ALIGNED_PTRS_HBD(pred_buf[2], tmp_buf_0, MAX_SB_SQUARE, len); |
| 13038 | ASSIGN_ALIGNED_PTRS_HBD(pred_buf[3], tmp_buf_0, MAX_SB_SQUARE, len); |
| 13039 | } else { |
| 13040 | #endif // CONFIG_HIGHBITDEPTH |
| 13041 | ASSIGN_ALIGNED_PTRS(pred_buf[0], tmp_buf_0, MAX_SB_SQUARE); |
| 13042 | ASSIGN_ALIGNED_PTRS(pred_buf[1], tmp_buf_1, MAX_SB_SQUARE); |
| 13043 | ASSIGN_ALIGNED_PTRS(pred_buf[2], tmp_buf_2, MAX_SB_SQUARE); |
| 13044 | ASSIGN_ALIGNED_PTRS(pred_buf[3], tmp_buf_3, MAX_SB_SQUARE); |
| 13045 | #if CONFIG_HIGHBITDEPTH |
| 13046 | } |
| 13047 | #endif |
| 13048 | |
| 13049 | av1_get_ext_blk_preds(cm, xd, bsize, mi_row, mi_col, pred_buf, pred_stride); |
| 13050 | av1_get_ori_blk_pred(cm, xd, bsize, mi_row, mi_col, pred_buf[3], pred_stride); |
| 13051 | |
| 13052 | for (tmp_mode = 0; tmp_mode < MAX_NCOBMC_MODES; ++tmp_mode) { |
| 13053 | error = 0; |
| 13054 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 13055 | build_ncobmc_intrpl_pred(cm, xd, plane, pxl_row, pxl_col, bsize, pred_buf, |
| 13056 | pred_stride, tmp_mode); |
| 13057 | error += get_ncobmc_error(xd, pxl_row, pxl_col, bsize, plane, |
| 13058 | &x->plane[plane].src); |
| 13059 | } |
| 13060 | if (error < best_error) { |
| 13061 | best_mode = tmp_mode; |
| 13062 | best_error = error; |
| 13063 | } |
| 13064 | } |
| 13065 | |
| 13066 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 13067 | build_ncobmc_intrpl_pred(cm, xd, plane, pxl_row, pxl_col, bsize, pred_buf, |
| 13068 | pred_stride, best_mode); |
| 13069 | } |
| 13070 | |
| 13071 | return best_mode; |
| 13072 | } |
| 13073 | |
| 13074 | #endif // CONFIG_NCOBMC_ADAPT_WEIGHT |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 13075 | #endif // CONFIG_MOTION_VAR |