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 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 225 | #if CONFIG_COMPOUND_SINGLEREF |
| 226 | // Single ref comp mode |
| 227 | { SR_NEAREST_NEARMV, { LAST_FRAME, NONE_FRAME } }, |
| 228 | #if CONFIG_EXT_REFS |
| 229 | { SR_NEAREST_NEARMV, { LAST2_FRAME, NONE_FRAME } }, |
| 230 | { SR_NEAREST_NEARMV, { LAST3_FRAME, NONE_FRAME } }, |
| 231 | { SR_NEAREST_NEARMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 232 | #endif // CONFIG_EXT_REFS |
| 233 | { SR_NEAREST_NEARMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 234 | { SR_NEAREST_NEARMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 235 | |
| 236 | /* |
| 237 | { SR_NEAREST_NEWMV, { LAST_FRAME, NONE_FRAME } }, |
| 238 | #if CONFIG_EXT_REFS |
| 239 | { SR_NEAREST_NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 240 | { SR_NEAREST_NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 241 | { SR_NEAREST_NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 242 | #endif // CONFIG_EXT_REFS |
| 243 | { SR_NEAREST_NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 244 | { SR_NEAREST_NEWMV, { ALTREF_FRAME, NONE_FRAME } },*/ |
| 245 | |
| 246 | { SR_NEAR_NEWMV, { LAST_FRAME, NONE_FRAME } }, |
| 247 | #if CONFIG_EXT_REFS |
| 248 | { SR_NEAR_NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 249 | { SR_NEAR_NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 250 | { SR_NEAR_NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 251 | #endif // CONFIG_EXT_REFS |
| 252 | { SR_NEAR_NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 253 | { SR_NEAR_NEWMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 254 | |
| 255 | { SR_ZERO_NEWMV, { LAST_FRAME, NONE_FRAME } }, |
| 256 | #if CONFIG_EXT_REFS |
| 257 | { SR_ZERO_NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 258 | { SR_ZERO_NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 259 | { SR_ZERO_NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 260 | #endif // CONFIG_EXT_REFS |
| 261 | { SR_ZERO_NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 262 | { SR_ZERO_NEWMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 263 | |
| 264 | { SR_NEW_NEWMV, { LAST_FRAME, NONE_FRAME } }, |
| 265 | #if CONFIG_EXT_REFS |
| 266 | { SR_NEW_NEWMV, { LAST2_FRAME, NONE_FRAME } }, |
| 267 | { SR_NEW_NEWMV, { LAST3_FRAME, NONE_FRAME } }, |
| 268 | { SR_NEW_NEWMV, { BWDREF_FRAME, NONE_FRAME } }, |
| 269 | #endif // CONFIG_EXT_REFS |
| 270 | { SR_NEW_NEWMV, { GOLDEN_FRAME, NONE_FRAME } }, |
| 271 | { SR_NEW_NEWMV, { ALTREF_FRAME, NONE_FRAME } }, |
| 272 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 273 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 274 | { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 275 | #if CONFIG_EXT_REFS |
| 276 | { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 277 | { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 278 | #endif // CONFIG_EXT_REFS |
| 279 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 280 | #if CONFIG_EXT_REFS |
| 281 | { NEAREST_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 282 | { NEAREST_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 283 | { NEAREST_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 284 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 285 | { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 286 | { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 287 | { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 288 | { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 289 | |
| 290 | #if CONFIG_EXT_COMP_REFS |
| 291 | { NEAREST_NEARESTMV, { LAST_FRAME, LAST2_FRAME } }, |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 292 | { NEAREST_NEARESTMV, { LAST_FRAME, LAST3_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 293 | { NEAREST_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 294 | { NEAREST_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 295 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 296 | #endif // CONFIG_EXT_REFS |
| 297 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 298 | { TM_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 299 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 300 | { SMOOTH_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Urvang Joshi | e6ca8e8 | 2017-03-15 14:57:41 -0700 | [diff] [blame] | 301 | #if CONFIG_SMOOTH_HV |
| 302 | { SMOOTH_V_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 303 | { SMOOTH_H_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 304 | #endif // CONFIG_SMOOTH_HV |
Urvang Joshi | 6be4a54 | 2016-11-03 15:24:05 -0700 | [diff] [blame] | 305 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 306 | { NEAR_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 307 | { NEW_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 308 | { NEAREST_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 309 | { NEW_NEARMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 310 | { NEAR_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 311 | { NEW_NEWMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 312 | { ZERO_ZEROMV, { LAST_FRAME, ALTREF_FRAME } }, |
| 313 | |
| 314 | #if CONFIG_EXT_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 315 | { NEAR_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 316 | { NEW_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 317 | { NEAREST_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 318 | { NEW_NEARMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 319 | { NEAR_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 320 | { NEW_NEWMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 321 | { ZERO_ZEROMV, { LAST2_FRAME, ALTREF_FRAME } }, |
| 322 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 323 | { NEAR_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 324 | { NEW_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 325 | { NEAREST_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 326 | { NEW_NEARMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 327 | { NEAR_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 328 | { NEW_NEWMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 329 | { ZERO_ZEROMV, { LAST3_FRAME, ALTREF_FRAME } }, |
| 330 | #endif // CONFIG_EXT_REFS |
| 331 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 332 | { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 333 | { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 334 | { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 335 | { NEW_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 336 | { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 337 | { NEW_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 338 | { ZERO_ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } }, |
| 339 | |
| 340 | #if CONFIG_EXT_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 341 | { NEAR_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 342 | { NEW_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 343 | { NEAREST_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 344 | { NEW_NEARMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 345 | { NEAR_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 346 | { NEW_NEWMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 347 | { ZERO_ZEROMV, { LAST_FRAME, BWDREF_FRAME } }, |
| 348 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 349 | { NEAR_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 350 | { NEW_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 351 | { NEAREST_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 352 | { NEW_NEARMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 353 | { NEAR_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 354 | { NEW_NEWMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 355 | { ZERO_ZEROMV, { LAST2_FRAME, BWDREF_FRAME } }, |
| 356 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 357 | { NEAR_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 358 | { NEW_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 359 | { NEAREST_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 360 | { NEW_NEARMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 361 | { NEAR_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 362 | { NEW_NEWMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 363 | { ZERO_ZEROMV, { LAST3_FRAME, BWDREF_FRAME } }, |
| 364 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 365 | { NEAR_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 366 | { NEW_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 367 | { NEAREST_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 368 | { NEW_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 369 | { NEAR_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 370 | { NEW_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
| 371 | { ZERO_ZEROMV, { GOLDEN_FRAME, BWDREF_FRAME } }, |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 372 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 373 | { NEAR_NEARMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 374 | { NEW_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 375 | { NEAREST_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 376 | { NEW_NEARMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 377 | { NEAR_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 378 | { NEW_NEWMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 379 | { ZERO_ZEROMV, { LAST_FRAME, ALTREF2_FRAME } }, |
| 380 | |
| 381 | { NEAR_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 382 | { NEW_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 383 | { NEAREST_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 384 | { NEW_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 385 | { NEAR_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 386 | { NEW_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 387 | { ZERO_ZEROMV, { LAST2_FRAME, ALTREF2_FRAME } }, |
| 388 | |
| 389 | { NEAR_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 390 | { NEW_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 391 | { NEAREST_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 392 | { NEW_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 393 | { NEAR_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 394 | { NEW_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 395 | { ZERO_ZEROMV, { LAST3_FRAME, ALTREF2_FRAME } }, |
| 396 | |
| 397 | { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 398 | { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 399 | { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 400 | { NEW_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 401 | { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 402 | { NEW_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
| 403 | { ZERO_ZEROMV, { GOLDEN_FRAME, ALTREF2_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 404 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 405 | #if CONFIG_EXT_COMP_REFS |
| 406 | { NEAR_NEARMV, { LAST_FRAME, LAST2_FRAME } }, |
| 407 | { NEW_NEARESTMV, { LAST_FRAME, LAST2_FRAME } }, |
| 408 | { NEAREST_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 409 | { NEW_NEARMV, { LAST_FRAME, LAST2_FRAME } }, |
| 410 | { NEAR_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 411 | { NEW_NEWMV, { LAST_FRAME, LAST2_FRAME } }, |
| 412 | { ZERO_ZEROMV, { LAST_FRAME, LAST2_FRAME } }, |
| 413 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 414 | { NEAR_NEARMV, { LAST_FRAME, LAST3_FRAME } }, |
| 415 | { NEW_NEARESTMV, { LAST_FRAME, LAST3_FRAME } }, |
| 416 | { NEAREST_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 417 | { NEW_NEARMV, { LAST_FRAME, LAST3_FRAME } }, |
| 418 | { NEAR_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 419 | { NEW_NEWMV, { LAST_FRAME, LAST3_FRAME } }, |
| 420 | { ZERO_ZEROMV, { LAST_FRAME, LAST3_FRAME } }, |
| 421 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 422 | { NEAR_NEARMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 423 | { NEW_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 424 | { NEAREST_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 425 | { NEW_NEARMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 426 | { NEAR_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 427 | { NEW_NEWMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 428 | { ZERO_ZEROMV, { LAST_FRAME, GOLDEN_FRAME } }, |
| 429 | |
| 430 | { NEAR_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 431 | { NEW_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 432 | { NEAREST_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 433 | { NEW_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 434 | { NEAR_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 435 | { NEW_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 436 | { ZERO_ZEROMV, { BWDREF_FRAME, ALTREF_FRAME } }, |
| 437 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 438 | #endif // CONFIG_EXT_REFS |
| 439 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 440 | { H_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 441 | { V_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 442 | { D135_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 443 | { D207_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 444 | { D153_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 445 | { D63_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 446 | { D117_PRED, { INTRA_FRAME, NONE_FRAME } }, |
| 447 | { D45_PRED, { INTRA_FRAME, NONE_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 448 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 449 | { ZEROMV, { LAST_FRAME, INTRA_FRAME } }, |
| 450 | { NEARESTMV, { LAST_FRAME, INTRA_FRAME } }, |
| 451 | { NEARMV, { LAST_FRAME, INTRA_FRAME } }, |
| 452 | { NEWMV, { LAST_FRAME, INTRA_FRAME } }, |
| 453 | |
| 454 | #if CONFIG_EXT_REFS |
| 455 | { ZEROMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 456 | { NEARESTMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 457 | { NEARMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 458 | { NEWMV, { LAST2_FRAME, INTRA_FRAME } }, |
| 459 | |
| 460 | { ZEROMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 461 | { NEARESTMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 462 | { NEARMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 463 | { NEWMV, { LAST3_FRAME, INTRA_FRAME } }, |
| 464 | #endif // CONFIG_EXT_REFS |
| 465 | |
| 466 | { ZEROMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 467 | { NEARESTMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 468 | { NEARMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 469 | { NEWMV, { GOLDEN_FRAME, INTRA_FRAME } }, |
| 470 | |
| 471 | #if CONFIG_EXT_REFS |
| 472 | { ZEROMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 473 | { NEARESTMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 474 | { NEARMV, { BWDREF_FRAME, INTRA_FRAME } }, |
| 475 | { NEWMV, { BWDREF_FRAME, INTRA_FRAME } }, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 476 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 477 | { ZEROMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
| 478 | { NEARESTMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
| 479 | { NEARMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
| 480 | { NEWMV, { ALTREF2_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 481 | #endif // CONFIG_EXT_REFS |
| 482 | |
| 483 | { ZEROMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 484 | { NEARESTMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 485 | { NEARMV, { ALTREF_FRAME, INTRA_FRAME } }, |
| 486 | { NEWMV, { ALTREF_FRAME, INTRA_FRAME } }, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 487 | }; |
| 488 | |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 489 | static const PREDICTION_MODE intra_rd_search_mode_order[INTRA_MODES] = { |
Urvang Joshi | 93b543a | 2017-06-01 17:32:41 -0700 | [diff] [blame] | 490 | DC_PRED, H_PRED, V_PRED, SMOOTH_PRED, TM_PRED, |
| 491 | #if CONFIG_SMOOTH_HV |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 492 | SMOOTH_V_PRED, SMOOTH_H_PRED, |
Urvang Joshi | 93b543a | 2017-06-01 17:32:41 -0700 | [diff] [blame] | 493 | #endif // CONFIG_SMOOTH_HV |
| 494 | D135_PRED, D207_PRED, D153_PRED, D63_PRED, D117_PRED, D45_PRED, |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 497 | #if CONFIG_CFL |
| 498 | 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] | 499 | UV_DC_PRED, UV_CFL_PRED, UV_H_PRED, |
| 500 | UV_V_PRED, UV_SMOOTH_PRED, UV_TM_PRED, |
| 501 | #if CONFIG_SMOOTH_HV |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 502 | UV_SMOOTH_V_PRED, UV_SMOOTH_H_PRED, |
Urvang Joshi | 93b543a | 2017-06-01 17:32:41 -0700 | [diff] [blame] | 503 | #endif // CONFIG_SMOOTH_HV |
| 504 | UV_D135_PRED, UV_D207_PRED, UV_D153_PRED, |
| 505 | UV_D63_PRED, UV_D117_PRED, UV_D45_PRED, |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 506 | }; |
| 507 | #else |
| 508 | #define uv_rd_search_mode_order intra_rd_search_mode_order |
| 509 | #endif // CONFIG_CFL |
| 510 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 511 | static INLINE int write_uniform_cost(int n, int v) { |
hui su | 3749929 | 2017-04-26 09:49:53 -0700 | [diff] [blame] | 512 | const int l = get_unsigned_bits(n); |
| 513 | const int m = (1 << l) - n; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 514 | if (l == 0) return 0; |
| 515 | if (v < m) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 516 | return (l - 1) * av1_cost_bit(128, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 517 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 518 | return l * av1_cost_bit(128, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // constants for prune 1 and prune 2 decision boundaries |
| 522 | #define FAST_EXT_TX_CORR_MID 0.0 |
| 523 | #define FAST_EXT_TX_EDST_MID 0.1 |
| 524 | #define FAST_EXT_TX_CORR_MARGIN 0.5 |
| 525 | #define FAST_EXT_TX_EDST_MARGIN 0.3 |
| 526 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 527 | static unsigned pixel_dist_visible_only( |
| 528 | const AV1_COMP *const cpi, const MACROBLOCK *x, const uint8_t *src, |
| 529 | const int src_stride, const uint8_t *dst, const int dst_stride, |
| 530 | const BLOCK_SIZE tx_bsize, int txb_rows, int txb_cols, int visible_rows, |
| 531 | int visible_cols) { |
| 532 | unsigned sse; |
| 533 | |
| 534 | if (txb_rows == visible_rows && txb_cols == visible_cols |
| 535 | #if CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX) |
| 536 | && tx_bsize < BLOCK_SIZES |
| 537 | #endif |
| 538 | ) { |
| 539 | cpi->fn_ptr[tx_bsize].vf(src, src_stride, dst, dst_stride, &sse); |
| 540 | return sse; |
| 541 | } |
| 542 | #if CONFIG_HIGHBITDEPTH |
| 543 | const MACROBLOCKD *xd = &x->e_mbd; |
| 544 | |
| 545 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 546 | uint64_t sse64 = aom_highbd_sse_odd_size(src, src_stride, dst, dst_stride, |
| 547 | visible_cols, visible_rows); |
| 548 | return (unsigned int)ROUND_POWER_OF_TWO(sse64, (xd->bd - 8) * 2); |
| 549 | } |
| 550 | #else |
| 551 | (void)x; |
| 552 | #endif // CONFIG_HIGHBITDEPTH |
| 553 | sse = aom_sse_odd_size(src, src_stride, dst, dst_stride, visible_cols, |
| 554 | visible_rows); |
| 555 | return sse; |
| 556 | } |
| 557 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 558 | #if CONFIG_DIST_8X8 |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 559 | static uint64_t cdef_dist_8x8_16bit(uint16_t *dst, int dstride, uint16_t *src, |
| 560 | int sstride, int coeff_shift) { |
| 561 | uint64_t svar = 0; |
| 562 | uint64_t dvar = 0; |
| 563 | uint64_t sum_s = 0; |
| 564 | uint64_t sum_d = 0; |
| 565 | uint64_t sum_s2 = 0; |
| 566 | uint64_t sum_d2 = 0; |
| 567 | uint64_t sum_sd = 0; |
| 568 | uint64_t dist = 0; |
| 569 | |
| 570 | int i, j; |
| 571 | for (i = 0; i < 8; i++) { |
| 572 | for (j = 0; j < 8; j++) { |
| 573 | sum_s += src[i * sstride + j]; |
| 574 | sum_d += dst[i * dstride + j]; |
| 575 | sum_s2 += src[i * sstride + j] * src[i * sstride + j]; |
| 576 | sum_d2 += dst[i * dstride + j] * dst[i * dstride + j]; |
| 577 | sum_sd += src[i * sstride + j] * dst[i * dstride + j]; |
| 578 | } |
| 579 | } |
| 580 | /* Compute the variance -- the calculation cannot go negative. */ |
| 581 | svar = sum_s2 - ((sum_s * sum_s + 32) >> 6); |
| 582 | dvar = sum_d2 - ((sum_d * sum_d + 32) >> 6); |
| 583 | |
| 584 | // Tuning of jm's original dering distortion metric used in CDEF tool, |
| 585 | // suggested by jm |
| 586 | const uint64_t a = 4; |
| 587 | const uint64_t b = 2; |
| 588 | const uint64_t c1 = (400 * a << 2 * coeff_shift); |
| 589 | const uint64_t c2 = (b * 20000 * a * a << 4 * coeff_shift); |
| 590 | |
| 591 | dist = |
| 592 | (uint64_t)floor(.5 + |
| 593 | (sum_d2 + sum_s2 - 2 * sum_sd) * .5 * (svar + dvar + c1) / |
| 594 | (sqrt(svar * (double)dvar + c2))); |
| 595 | |
| 596 | // Calibrate dist to have similar rate for the same QP with MSE only |
| 597 | // distortion (as in master branch) |
| 598 | dist = (uint64_t)((float)dist * 0.75); |
| 599 | |
| 600 | return dist; |
| 601 | } |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 602 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 603 | static int od_compute_var_4x4(uint16_t *x, int stride) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 604 | int sum; |
| 605 | int s2; |
| 606 | int i; |
| 607 | sum = 0; |
| 608 | s2 = 0; |
| 609 | for (i = 0; i < 4; i++) { |
| 610 | int j; |
| 611 | for (j = 0; j < 4; j++) { |
| 612 | int t; |
| 613 | |
| 614 | t = x[i * stride + j]; |
| 615 | sum += t; |
| 616 | s2 += t * t; |
| 617 | } |
| 618 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 619 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 620 | return (s2 - (sum * sum >> 4)) >> 4; |
| 621 | } |
| 622 | |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 623 | /* OD_DIST_LP_MID controls the frequency weighting filter used for computing |
| 624 | the distortion. For a value X, the filter is [1 X 1]/(X + 2) and |
| 625 | is applied both horizontally and vertically. For X=5, the filter is |
| 626 | a good approximation for the OD_QM8_Q4_HVS quantization matrix. */ |
| 627 | #define OD_DIST_LP_MID (5) |
| 628 | #define OD_DIST_LP_NORM (OD_DIST_LP_MID + 2) |
| 629 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 630 | static double od_compute_dist_8x8(int use_activity_masking, uint16_t *x, |
| 631 | uint16_t *y, od_coeff *e_lp, int stride) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 632 | double sum; |
| 633 | int min_var; |
| 634 | double mean_var; |
| 635 | double var_stat; |
| 636 | double activity; |
| 637 | double calibration; |
| 638 | int i; |
| 639 | int j; |
| 640 | double vardist; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 641 | |
| 642 | vardist = 0; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 643 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 644 | #if 1 |
| 645 | min_var = INT_MAX; |
| 646 | mean_var = 0; |
| 647 | for (i = 0; i < 3; i++) { |
| 648 | for (j = 0; j < 3; j++) { |
| 649 | int varx; |
| 650 | int vary; |
| 651 | varx = od_compute_var_4x4(x + 2 * i * stride + 2 * j, stride); |
| 652 | vary = od_compute_var_4x4(y + 2 * i * stride + 2 * j, stride); |
| 653 | min_var = OD_MINI(min_var, varx); |
| 654 | mean_var += 1. / (1 + varx); |
| 655 | /* The cast to (double) is to avoid an overflow before the sqrt.*/ |
| 656 | vardist += varx - 2 * sqrt(varx * (double)vary) + vary; |
| 657 | } |
| 658 | } |
| 659 | /* We use a different variance statistic depending on whether activity |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 660 | masking is used, since the harmonic mean appeared slightly worse with |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 661 | masking off. The calibration constant just ensures that we preserve the |
| 662 | rate compared to activity=1. */ |
| 663 | if (use_activity_masking) { |
| 664 | calibration = 1.95; |
| 665 | var_stat = 9. / mean_var; |
| 666 | } else { |
| 667 | calibration = 1.62; |
| 668 | var_stat = min_var; |
| 669 | } |
| 670 | /* 1.62 is a calibration constant, 0.25 is a noise floor and 1/6 is the |
| 671 | activity masking constant. */ |
| 672 | activity = calibration * pow(.25 + var_stat, -1. / 6); |
| 673 | #else |
| 674 | activity = 1; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 675 | #endif // 1 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 676 | sum = 0; |
| 677 | for (i = 0; i < 8; i++) { |
| 678 | for (j = 0; j < 8; j++) |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 679 | sum += e_lp[i * stride + j] * (double)e_lp[i * stride + j]; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 680 | } |
Jean-Marc Valin | 79c0f32 | 2017-01-18 01:58:33 -0500 | [diff] [blame] | 681 | /* Normalize the filter to unit DC response. */ |
| 682 | sum *= 1. / (OD_DIST_LP_NORM * OD_DIST_LP_NORM * OD_DIST_LP_NORM * |
| 683 | OD_DIST_LP_NORM); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 684 | return activity * activity * (sum + vardist); |
| 685 | } |
| 686 | |
| 687 | // Note : Inputs x and y are in a pixel domain |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 688 | static double od_compute_dist_common(int activity_masking, uint16_t *x, |
| 689 | uint16_t *y, int bsize_w, int bsize_h, |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 690 | int qindex, od_coeff *tmp, |
| 691 | od_coeff *e_lp) { |
| 692 | int i, j; |
| 693 | double sum = 0; |
| 694 | const int mid = OD_DIST_LP_MID; |
| 695 | |
| 696 | for (j = 0; j < bsize_w; j++) { |
| 697 | e_lp[j] = mid * tmp[j] + 2 * tmp[bsize_w + j]; |
| 698 | e_lp[(bsize_h - 1) * bsize_w + j] = mid * tmp[(bsize_h - 1) * bsize_w + j] + |
| 699 | 2 * tmp[(bsize_h - 2) * bsize_w + j]; |
| 700 | } |
| 701 | for (i = 1; i < bsize_h - 1; i++) { |
| 702 | for (j = 0; j < bsize_w; j++) { |
| 703 | e_lp[i * bsize_w + j] = mid * tmp[i * bsize_w + j] + |
| 704 | tmp[(i - 1) * bsize_w + j] + |
| 705 | tmp[(i + 1) * bsize_w + j]; |
| 706 | } |
| 707 | } |
| 708 | for (i = 0; i < bsize_h; i += 8) { |
| 709 | for (j = 0; j < bsize_w; j += 8) { |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 710 | sum += od_compute_dist_8x8(activity_masking, &x[i * bsize_w + j], |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 711 | &y[i * bsize_w + j], &e_lp[i * bsize_w + j], |
| 712 | bsize_w); |
| 713 | } |
| 714 | } |
| 715 | /* Scale according to linear regression against SSE, for 8x8 blocks. */ |
| 716 | if (activity_masking) { |
| 717 | sum *= 2.2 + (1.7 - 2.2) * (qindex - 99) / (210 - 99) + |
| 718 | (qindex < 99 ? 2.5 * (qindex - 99) / 99 * (qindex - 99) / 99 : 0); |
| 719 | } else { |
| 720 | sum *= qindex >= 128 |
| 721 | ? 1.4 + (0.9 - 1.4) * (qindex - 128) / (209 - 128) |
| 722 | : qindex <= 43 ? 1.5 + (2.0 - 1.5) * (qindex - 43) / (16 - 43) |
| 723 | : 1.5 + (1.4 - 1.5) * (qindex - 43) / (128 - 43); |
| 724 | } |
| 725 | |
| 726 | return sum; |
| 727 | } |
| 728 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 729 | static double od_compute_dist(uint16_t *x, uint16_t *y, int bsize_w, |
| 730 | int bsize_h, int qindex) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 731 | assert(bsize_w >= 8 && bsize_h >= 8); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 732 | #if CONFIG_PVQ |
| 733 | int activity_masking = 1; |
| 734 | #else |
| 735 | int activity_masking = 0; |
| 736 | #endif |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 737 | int i, j; |
| 738 | DECLARE_ALIGNED(16, od_coeff, e[MAX_TX_SQUARE]); |
| 739 | DECLARE_ALIGNED(16, od_coeff, tmp[MAX_TX_SQUARE]); |
| 740 | DECLARE_ALIGNED(16, od_coeff, e_lp[MAX_TX_SQUARE]); |
| 741 | for (i = 0; i < bsize_h; i++) { |
| 742 | for (j = 0; j < bsize_w; j++) { |
| 743 | 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] | 744 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 745 | } |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 746 | int mid = OD_DIST_LP_MID; |
| 747 | for (i = 0; i < bsize_h; i++) { |
| 748 | tmp[i * bsize_w] = mid * e[i * bsize_w] + 2 * e[i * bsize_w + 1]; |
| 749 | tmp[i * bsize_w + bsize_w - 1] = |
| 750 | mid * e[i * bsize_w + bsize_w - 1] + 2 * e[i * bsize_w + bsize_w - 2]; |
| 751 | for (j = 1; j < bsize_w - 1; j++) { |
| 752 | tmp[i * bsize_w + j] = mid * e[i * bsize_w + j] + e[i * bsize_w + j - 1] + |
| 753 | e[i * bsize_w + j + 1]; |
| 754 | } |
| 755 | } |
| 756 | return od_compute_dist_common(activity_masking, x, y, bsize_w, bsize_h, |
| 757 | qindex, tmp, e_lp); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 760 | static double od_compute_dist_diff(uint16_t *x, int16_t *e, int bsize_w, |
| 761 | int bsize_h, int qindex) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 762 | assert(bsize_w >= 8 && bsize_h >= 8); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 763 | #if CONFIG_PVQ |
| 764 | int activity_masking = 1; |
| 765 | #else |
| 766 | int activity_masking = 0; |
| 767 | #endif |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 768 | DECLARE_ALIGNED(16, uint16_t, y[MAX_TX_SQUARE]); |
| 769 | DECLARE_ALIGNED(16, od_coeff, tmp[MAX_TX_SQUARE]); |
| 770 | DECLARE_ALIGNED(16, od_coeff, e_lp[MAX_TX_SQUARE]); |
| 771 | int i, j; |
| 772 | for (i = 0; i < bsize_h; i++) { |
| 773 | for (j = 0; j < bsize_w; j++) { |
| 774 | 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] | 775 | } |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 776 | } |
Luc Trudeau | 1f7c411 | 2017-09-13 15:10:08 -0400 | [diff] [blame] | 777 | int mid = OD_DIST_LP_MID; |
| 778 | for (i = 0; i < bsize_h; i++) { |
| 779 | tmp[i * bsize_w] = mid * e[i * bsize_w] + 2 * e[i * bsize_w + 1]; |
| 780 | tmp[i * bsize_w + bsize_w - 1] = |
| 781 | mid * e[i * bsize_w + bsize_w - 1] + 2 * e[i * bsize_w + bsize_w - 2]; |
| 782 | for (j = 1; j < bsize_w - 1; j++) { |
| 783 | tmp[i * bsize_w + j] = mid * e[i * bsize_w + j] + e[i * bsize_w + j - 1] + |
| 784 | e[i * bsize_w + j + 1]; |
| 785 | } |
| 786 | } |
| 787 | return od_compute_dist_common(activity_masking, x, y, bsize_w, bsize_h, |
| 788 | qindex, tmp, e_lp); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 789 | } |
| 790 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 791 | 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] | 792 | const uint8_t *src, int src_stride, const uint8_t *dst, |
| 793 | int dst_stride, const BLOCK_SIZE tx_bsize, int bsw, |
| 794 | int bsh, int visible_w, int visible_h, int qindex) { |
| 795 | int64_t d = 0; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 796 | int i, j; |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 797 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 798 | |
| 799 | DECLARE_ALIGNED(16, uint16_t, orig[MAX_TX_SQUARE]); |
| 800 | DECLARE_ALIGNED(16, uint16_t, rec[MAX_TX_SQUARE]); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 801 | |
Yushin Cho | ee81027 | 2017-09-13 17:30:25 -0700 | [diff] [blame] | 802 | assert(bsw >= 8); |
| 803 | assert(bsh >= 8); |
| 804 | assert((bsw & 0x07) == 0); |
| 805 | assert((bsh & 0x07) == 0); |
| 806 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 807 | if (x->tune_metric == AOM_TUNE_CDEF_DIST || |
| 808 | x->tune_metric == AOM_TUNE_DAALA_DIST) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 809 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 810 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 811 | for (j = 0; j < bsh; j++) |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 812 | for (i = 0; i < bsw; i++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 813 | orig[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 814 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 815 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 816 | for (j = 0; j < bsh; j++) |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 817 | for (i = 0; i < bsw; i++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 818 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(dst)[j * dst_stride + i]; |
| 819 | } else { |
| 820 | for (j = 0; j < visible_h; j++) |
| 821 | for (i = 0; i < visible_w; i++) |
| 822 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(dst)[j * dst_stride + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 823 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 824 | if (visible_w < bsw) { |
| 825 | for (j = 0; j < bsh; j++) |
| 826 | for (i = visible_w; i < bsw; i++) |
| 827 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 828 | } |
| 829 | |
| 830 | if (visible_h < bsh) { |
| 831 | for (j = visible_h; j < bsh; j++) |
| 832 | for (i = 0; i < bsw; i++) |
| 833 | rec[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 834 | } |
| 835 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 836 | } else { |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 837 | #endif |
| 838 | for (j = 0; j < bsh; j++) |
| 839 | 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] | 840 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 841 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 842 | for (j = 0; j < bsh; j++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 843 | for (i = 0; i < bsw; i++) rec[j * bsw + i] = dst[j * dst_stride + i]; |
| 844 | } else { |
| 845 | for (j = 0; j < visible_h; j++) |
| 846 | for (i = 0; i < visible_w; i++) |
| 847 | rec[j * bsw + i] = dst[j * dst_stride + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 848 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 849 | if (visible_w < bsw) { |
| 850 | for (j = 0; j < bsh; j++) |
| 851 | for (i = visible_w; i < bsw; i++) |
| 852 | rec[j * bsw + i] = src[j * src_stride + i]; |
| 853 | } |
| 854 | |
| 855 | if (visible_h < bsh) { |
| 856 | for (j = visible_h; j < bsh; j++) |
| 857 | for (i = 0; i < bsw; i++) |
| 858 | rec[j * bsw + i] = src[j * src_stride + i]; |
| 859 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 860 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 861 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 862 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 863 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 864 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 865 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 866 | if (x->tune_metric == AOM_TUNE_DAALA_DIST) { |
| 867 | d = (int64_t)od_compute_dist(orig, rec, bsw, bsh, qindex); |
| 868 | } else if (x->tune_metric == AOM_TUNE_CDEF_DIST) { |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 869 | int coeff_shift = AOMMAX(xd->bd - 8, 0); |
| 870 | |
| 871 | for (i = 0; i < bsh; i += 8) { |
| 872 | for (j = 0; j < bsw; j += 8) { |
| 873 | d += cdef_dist_8x8_16bit(&rec[i * bsw + j], bsw, &orig[i * bsw + j], |
| 874 | bsw, coeff_shift); |
| 875 | } |
| 876 | } |
| 877 | #if CONFIG_HIGHBITDEPTH |
| 878 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 879 | d = ((uint64_t)d) >> 2 * coeff_shift; |
| 880 | #endif |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 881 | } else { |
| 882 | // Otherwise, MSE by default |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 883 | d = pixel_dist_visible_only(cpi, x, src, src_stride, dst, dst_stride, |
| 884 | tx_bsize, bsh, bsw, visible_h, visible_w); |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 885 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 886 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 887 | return d; |
| 888 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 889 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 890 | 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] | 891 | int src_stride, const int16_t *diff, |
| 892 | int diff_stride, int bsw, int bsh, |
| 893 | int visible_w, int visible_h, int qindex) { |
| 894 | int64_t d = 0; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 895 | int i, j; |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 896 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 897 | |
| 898 | DECLARE_ALIGNED(16, uint16_t, orig[MAX_TX_SQUARE]); |
| 899 | DECLARE_ALIGNED(16, int16_t, diff16[MAX_TX_SQUARE]); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 900 | |
Yushin Cho | ee81027 | 2017-09-13 17:30:25 -0700 | [diff] [blame] | 901 | assert(bsw >= 8); |
| 902 | assert(bsh >= 8); |
| 903 | assert((bsw & 0x07) == 0); |
| 904 | assert((bsh & 0x07) == 0); |
| 905 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 906 | if (x->tune_metric == AOM_TUNE_CDEF_DIST || |
| 907 | x->tune_metric == AOM_TUNE_DAALA_DIST) { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 908 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 909 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 910 | for (j = 0; j < bsh; j++) |
| 911 | for (i = 0; i < bsw; i++) |
| 912 | orig[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i]; |
| 913 | } else { |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 914 | #endif |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 915 | for (j = 0; j < bsh; j++) |
| 916 | 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] | 917 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 918 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 919 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 920 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 921 | if ((bsw == visible_w) && (bsh == visible_h)) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 922 | for (j = 0; j < bsh; j++) |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 923 | for (i = 0; i < bsw; i++) |
| 924 | diff16[j * bsw + i] = diff[j * diff_stride + i]; |
| 925 | } else { |
| 926 | for (j = 0; j < visible_h; j++) |
| 927 | for (i = 0; i < visible_w; i++) |
| 928 | diff16[j * bsw + i] = diff[j * diff_stride + i]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 929 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 930 | if (visible_w < bsw) { |
| 931 | for (j = 0; j < bsh; j++) |
| 932 | for (i = visible_w; i < bsw; i++) diff16[j * bsw + i] = 0; |
| 933 | } |
| 934 | |
| 935 | if (visible_h < bsh) { |
| 936 | for (j = visible_h; j < bsh; j++) |
| 937 | for (i = 0; i < bsw; i++) diff16[j * bsw + i] = 0; |
| 938 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 941 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 942 | if (x->tune_metric == AOM_TUNE_DAALA_DIST) { |
| 943 | d = (int64_t)od_compute_dist_diff(orig, diff16, bsw, bsh, qindex); |
| 944 | } else if (x->tune_metric == AOM_TUNE_CDEF_DIST) { |
Yushin Cho | c49177e | 2017-07-18 17:18:09 -0700 | [diff] [blame] | 945 | int coeff_shift = AOMMAX(xd->bd - 8, 0); |
| 946 | DECLARE_ALIGNED(16, uint16_t, dst16[MAX_TX_SQUARE]); |
| 947 | |
| 948 | for (i = 0; i < bsh; i++) { |
| 949 | for (j = 0; j < bsw; j++) { |
| 950 | dst16[i * bsw + j] = orig[i * bsw + j] - diff16[i * bsw + j]; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | for (i = 0; i < bsh; i += 8) { |
| 955 | for (j = 0; j < bsw; j += 8) { |
| 956 | d += cdef_dist_8x8_16bit(&dst16[i * bsw + j], bsw, &orig[i * bsw + j], |
| 957 | bsw, coeff_shift); |
| 958 | } |
| 959 | } |
| 960 | // Don't scale 'd' for HBD since it will be done by caller side for diff |
| 961 | // input |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 962 | } else { |
| 963 | // Otherwise, MSE by default |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 964 | 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] | 965 | } |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 966 | |
| 967 | return d; |
| 968 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 969 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 970 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 971 | 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] | 972 | const uint8_t *src, int src_stride, |
| 973 | const uint8_t *dst, int dst_stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 974 | double *hordist, double *verdist) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 975 | const int bw = block_size_wide[bsize]; |
| 976 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 977 | 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] | 978 | |
| 979 | const int f_index = bsize - BLOCK_16X16; |
| 980 | if (f_index < 0) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 981 | const int w_shift = bw == 8 ? 1 : 2; |
| 982 | const int h_shift = bh == 8 ? 1 : 2; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 983 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 984 | if (cpi->common.use_highbitdepth) { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 985 | const uint16_t *src16 = CONVERT_TO_SHORTPTR(src); |
| 986 | const uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst); |
| 987 | for (int i = 0; i < bh; ++i) |
| 988 | for (int j = 0; j < bw; ++j) { |
| 989 | const int index = (j >> w_shift) + ((i >> h_shift) << 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 990 | esq[index] += |
| 991 | (src16[j + i * src_stride] - dst16[j + i * dst_stride]) * |
| 992 | (src16[j + i * src_stride] - dst16[j + i * dst_stride]); |
| 993 | } |
| 994 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 995 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 996 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 997 | for (int i = 0; i < bh; ++i) |
| 998 | for (int j = 0; j < bw; ++j) { |
| 999 | const int index = (j >> w_shift) + ((i >> h_shift) << 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1000 | esq[index] += (src[j + i * src_stride] - dst[j + i * dst_stride]) * |
| 1001 | (src[j + i * src_stride] - dst[j + i * dst_stride]); |
| 1002 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1003 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1004 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1005 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1006 | } else { |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1007 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[0]); |
| 1008 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 1009 | &esq[1]); |
| 1010 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 1011 | &esq[2]); |
| 1012 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 1013 | dst_stride, &esq[3]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1014 | src += bh / 4 * src_stride; |
| 1015 | dst += bh / 4 * dst_stride; |
| 1016 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1017 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[4]); |
| 1018 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 1019 | &esq[5]); |
| 1020 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 1021 | &esq[6]); |
| 1022 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 1023 | dst_stride, &esq[7]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1024 | src += bh / 4 * src_stride; |
| 1025 | dst += bh / 4 * dst_stride; |
| 1026 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1027 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[8]); |
| 1028 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 1029 | &esq[9]); |
| 1030 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 1031 | &esq[10]); |
| 1032 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 1033 | dst_stride, &esq[11]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1034 | src += bh / 4 * src_stride; |
| 1035 | dst += bh / 4 * dst_stride; |
| 1036 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1037 | cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[12]); |
| 1038 | cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride, |
| 1039 | &esq[13]); |
| 1040 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride, |
| 1041 | &esq[14]); |
| 1042 | cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4, |
| 1043 | dst_stride, &esq[15]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1046 | double total = (double)esq[0] + esq[1] + esq[2] + esq[3] + esq[4] + esq[5] + |
| 1047 | esq[6] + esq[7] + esq[8] + esq[9] + esq[10] + esq[11] + |
| 1048 | esq[12] + esq[13] + esq[14] + esq[15]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1049 | if (total > 0) { |
| 1050 | const double e_recip = 1.0 / total; |
Alex Converse | 9f21776 | 2017-04-20 15:34:54 -0700 | [diff] [blame] | 1051 | hordist[0] = ((double)esq[0] + esq[4] + esq[8] + esq[12]) * e_recip; |
| 1052 | hordist[1] = ((double)esq[1] + esq[5] + esq[9] + esq[13]) * e_recip; |
| 1053 | hordist[2] = ((double)esq[2] + esq[6] + esq[10] + esq[14]) * e_recip; |
| 1054 | verdist[0] = ((double)esq[0] + esq[1] + esq[2] + esq[3]) * e_recip; |
| 1055 | verdist[1] = ((double)esq[4] + esq[5] + esq[6] + esq[7]) * e_recip; |
| 1056 | 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] | 1057 | } else { |
| 1058 | hordist[0] = verdist[0] = 0.25; |
| 1059 | hordist[1] = verdist[1] = 0.25; |
| 1060 | hordist[2] = verdist[2] = 0.25; |
| 1061 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 1064 | static int adst_vs_flipadst(const AV1_COMP *cpi, BLOCK_SIZE bsize, |
| 1065 | const uint8_t *src, int src_stride, |
| 1066 | const uint8_t *dst, int dst_stride) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1067 | int prune_bitmask = 0; |
| 1068 | double svm_proj_h = 0, svm_proj_v = 0; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1069 | double hdist[3] = { 0, 0, 0 }, vdist[3] = { 0, 0, 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1070 | get_energy_distribution_fine(cpi, bsize, src, src_stride, dst, dst_stride, |
| 1071 | hdist, vdist); |
| 1072 | |
| 1073 | svm_proj_v = vdist[0] * ADST_FLIP_SVM[0] + vdist[1] * ADST_FLIP_SVM[1] + |
| 1074 | vdist[2] * ADST_FLIP_SVM[2] + ADST_FLIP_SVM[3]; |
| 1075 | svm_proj_h = hdist[0] * ADST_FLIP_SVM[4] + hdist[1] * ADST_FLIP_SVM[5] + |
| 1076 | hdist[2] * ADST_FLIP_SVM[6] + ADST_FLIP_SVM[7]; |
| 1077 | if (svm_proj_v > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN) |
| 1078 | prune_bitmask |= 1 << FLIPADST_1D; |
| 1079 | else if (svm_proj_v < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN) |
| 1080 | prune_bitmask |= 1 << ADST_1D; |
| 1081 | |
| 1082 | if (svm_proj_h > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN) |
| 1083 | prune_bitmask |= 1 << (FLIPADST_1D + 8); |
| 1084 | else if (svm_proj_h < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN) |
| 1085 | prune_bitmask |= 1 << (ADST_1D + 8); |
| 1086 | |
| 1087 | return prune_bitmask; |
| 1088 | } |
| 1089 | |
| 1090 | #if CONFIG_EXT_TX |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1091 | static void get_horver_correlation(const int16_t *diff, int stride, int w, |
| 1092 | int h, double *hcorr, double *vcorr) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1093 | // Returns hor/ver correlation coefficient |
| 1094 | const int num = (h - 1) * (w - 1); |
| 1095 | double num_r; |
| 1096 | int i, j; |
| 1097 | int64_t xy_sum = 0, xz_sum = 0; |
| 1098 | int64_t x_sum = 0, y_sum = 0, z_sum = 0; |
| 1099 | int64_t x2_sum = 0, y2_sum = 0, z2_sum = 0; |
| 1100 | double x_var_n, y_var_n, z_var_n, xy_var_n, xz_var_n; |
| 1101 | *hcorr = *vcorr = 1; |
| 1102 | |
| 1103 | assert(num > 0); |
| 1104 | num_r = 1.0 / num; |
| 1105 | for (i = 1; i < h; ++i) { |
| 1106 | for (j = 1; j < w; ++j) { |
| 1107 | const int16_t x = diff[i * stride + j]; |
| 1108 | const int16_t y = diff[i * stride + j - 1]; |
| 1109 | const int16_t z = diff[(i - 1) * stride + j]; |
| 1110 | xy_sum += x * y; |
| 1111 | xz_sum += x * z; |
| 1112 | x_sum += x; |
| 1113 | y_sum += y; |
| 1114 | z_sum += z; |
| 1115 | x2_sum += x * x; |
| 1116 | y2_sum += y * y; |
| 1117 | z2_sum += z * z; |
| 1118 | } |
| 1119 | } |
| 1120 | x_var_n = x2_sum - (x_sum * x_sum) * num_r; |
| 1121 | y_var_n = y2_sum - (y_sum * y_sum) * num_r; |
| 1122 | z_var_n = z2_sum - (z_sum * z_sum) * num_r; |
| 1123 | xy_var_n = xy_sum - (x_sum * y_sum) * num_r; |
| 1124 | xz_var_n = xz_sum - (x_sum * z_sum) * num_r; |
| 1125 | if (x_var_n > 0 && y_var_n > 0) { |
| 1126 | *hcorr = xy_var_n / sqrt(x_var_n * y_var_n); |
| 1127 | *hcorr = *hcorr < 0 ? 0 : *hcorr; |
| 1128 | } |
| 1129 | if (x_var_n > 0 && z_var_n > 0) { |
| 1130 | *vcorr = xz_var_n / sqrt(x_var_n * z_var_n); |
| 1131 | *vcorr = *vcorr < 0 ? 0 : *vcorr; |
| 1132 | } |
| 1133 | } |
| 1134 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1135 | int dct_vs_idtx(const int16_t *diff, int stride, int w, int h) { |
| 1136 | double hcorr, vcorr; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1137 | int prune_bitmask = 0; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1138 | get_horver_correlation(diff, stride, w, h, &hcorr, &vcorr); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1139 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1140 | if (vcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1141 | prune_bitmask |= 1 << IDTX_1D; |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1142 | 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] | 1143 | prune_bitmask |= 1 << DCT_1D; |
| 1144 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1145 | if (hcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1146 | prune_bitmask |= 1 << (IDTX_1D + 8); |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1147 | 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] | 1148 | prune_bitmask |= 1 << (DCT_1D + 8); |
| 1149 | return prune_bitmask; |
| 1150 | } |
| 1151 | |
| 1152 | // Performance drop: 0.5%, Speed improvement: 24% |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1153 | 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] | 1154 | MACROBLOCK *x, const MACROBLOCKD *xd, |
| 1155 | int adst_flipadst, int dct_idtx) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1156 | int prune = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1157 | |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1158 | if (adst_flipadst) { |
| 1159 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1160 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1161 | prune |= adst_vs_flipadst(cpi, bsize, p->src.buf, p->src.stride, |
Alex Converse | 89912f9 | 2017-04-21 13:28:50 -0700 | [diff] [blame] | 1162 | pd->dst.buf, pd->dst.stride); |
| 1163 | } |
| 1164 | if (dct_idtx) { |
| 1165 | av1_subtract_plane(x, bsize, 0); |
| 1166 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1167 | const int bw = 4 << (b_width_log2_lookup[bsize]); |
| 1168 | const int bh = 4 << (b_height_log2_lookup[bsize]); |
| 1169 | prune |= dct_vs_idtx(p->src_diff, bw, bw, bh); |
| 1170 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1171 | |
| 1172 | return prune; |
| 1173 | } |
| 1174 | #endif // CONFIG_EXT_TX |
| 1175 | |
| 1176 | // Performance drop: 0.3%, Speed improvement: 5% |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1177 | 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] | 1178 | const MACROBLOCK *x, const MACROBLOCKD *xd) { |
| 1179 | const struct macroblock_plane *const p = &x->plane[0]; |
| 1180 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1181 | 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] | 1182 | pd->dst.stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Hui Su | 032ab8b | 2017-09-19 14:53:40 -0700 | [diff] [blame] | 1185 | #if CONFIG_EXT_TX |
| 1186 | // 1D Transforms used in inter set, this needs to be changed if |
| 1187 | // ext_tx_used_inter is changed |
| 1188 | static const int ext_tx_used_inter_1D[EXT_TX_SETS_INTER][TX_TYPES_1D] = { |
| 1189 | { 1, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, |
| 1190 | #if CONFIG_MRC_TX |
| 1191 | { 1, 0, 0, 1 }, |
| 1192 | #endif // CONFIG_MRC_TX |
| 1193 | }; |
| 1194 | #endif // CONFIG_EXT_TX |
| 1195 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1196 | 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] | 1197 | const MACROBLOCKD *const xd, int tx_set) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1198 | #if CONFIG_EXT_TX |
Yaowu Xu | 37fe5fb | 2017-05-09 11:43:45 -0700 | [diff] [blame] | 1199 | 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] | 1200 | #else |
| 1201 | const int tx_set_1D[TX_TYPES_1D] = { 0 }; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 1202 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1203 | |
| 1204 | switch (cpi->sf.tx_type_search.prune_mode) { |
| 1205 | case NO_PRUNE: return 0; break; |
| 1206 | case PRUNE_ONE: |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 1207 | 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] | 1208 | return 0; |
| 1209 | return prune_one_for_sby(cpi, bsize, x, xd); |
| 1210 | break; |
| 1211 | #if CONFIG_EXT_TX |
| 1212 | case PRUNE_TWO: |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 1213 | 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] | 1214 | if (!(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D])) return 0; |
| 1215 | return prune_two_for_sby(cpi, bsize, x, xd, 0, 1); |
| 1216 | } |
Sarah Parker | 68a26b6 | 2016-10-28 13:19:33 -0700 | [diff] [blame] | 1217 | 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] | 1218 | return prune_two_for_sby(cpi, bsize, x, xd, 1, 0); |
| 1219 | return prune_two_for_sby(cpi, bsize, x, xd, 1, 1); |
| 1220 | break; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 1221 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1222 | } |
| 1223 | assert(0); |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | static int do_tx_type_search(TX_TYPE tx_type, int prune) { |
| 1228 | // TODO(sarahparker) implement for non ext tx |
| 1229 | #if CONFIG_EXT_TX |
| 1230 | return !(((prune >> vtx_tab[tx_type]) & 1) | |
| 1231 | ((prune >> (htx_tab[tx_type] + 8)) & 1)); |
| 1232 | #else |
| 1233 | // temporary to avoid compiler warnings |
| 1234 | (void)vtx_tab; |
| 1235 | (void)htx_tab; |
| 1236 | (void)tx_type; |
| 1237 | (void)prune; |
| 1238 | return 1; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 1239 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1242 | static void model_rd_from_sse(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1243 | const MACROBLOCKD *const xd, BLOCK_SIZE bsize, |
| 1244 | int plane, int64_t sse, int *rate, |
| 1245 | int64_t *dist) { |
| 1246 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1247 | const int dequant_shift = |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1248 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1249 | (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd - 5 : |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1250 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1251 | 3; |
| 1252 | |
| 1253 | // Fast approximate the modelling function. |
| 1254 | if (cpi->sf.simple_model_rd_from_var) { |
| 1255 | const int64_t square_error = sse; |
| 1256 | int quantizer = (pd->dequant[1] >> dequant_shift); |
| 1257 | |
| 1258 | if (quantizer < 120) |
| 1259 | *rate = (int)((square_error * (280 - quantizer)) >> |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1260 | (16 - AV1_PROB_COST_SHIFT)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1261 | else |
| 1262 | *rate = 0; |
| 1263 | *dist = (square_error * quantizer) >> 8; |
| 1264 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1265 | av1_model_rd_from_var_lapndz(sse, num_pels_log2_lookup[bsize], |
| 1266 | pd->dequant[1] >> dequant_shift, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | *dist <<= 4; |
| 1270 | } |
| 1271 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1272 | 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] | 1273 | MACROBLOCK *x, MACROBLOCKD *xd, int plane_from, |
| 1274 | int plane_to, int *out_rate_sum, |
| 1275 | int64_t *out_dist_sum, int *skip_txfm_sb, |
| 1276 | int64_t *skip_sse_sb) { |
| 1277 | // Note our transform coeffs are 8 times an orthogonal transform. |
| 1278 | // Hence quantizer step is also 8 times. To get effective quantizer |
| 1279 | // we need to divide by 8 before sending to modeling function. |
| 1280 | int plane; |
| 1281 | const int ref = xd->mi[0]->mbmi.ref_frame[0]; |
| 1282 | |
| 1283 | int64_t rate_sum = 0; |
| 1284 | int64_t dist_sum = 0; |
| 1285 | int64_t total_sse = 0; |
| 1286 | |
| 1287 | x->pred_sse[ref] = 0; |
| 1288 | |
| 1289 | for (plane = plane_from; plane <= plane_to; ++plane) { |
| 1290 | struct macroblock_plane *const p = &x->plane[plane]; |
| 1291 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Timothy B. Terriberry | 81ec261 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 1292 | #if CONFIG_CHROMA_SUB8X8 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1293 | const BLOCK_SIZE bs = AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd)); |
| 1294 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1295 | const BLOCK_SIZE bs = get_plane_block_size(bsize, pd); |
Timothy B. Terriberry | 81ec261 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 1296 | #endif // CONFIG_CHROMA_SUB8X8 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1297 | |
| 1298 | unsigned int sse; |
| 1299 | int rate; |
| 1300 | int64_t dist; |
| 1301 | |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1302 | #if CONFIG_CB4X4 |
| 1303 | if (x->skip_chroma_rd && plane) continue; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 1304 | #endif // CONFIG_CB4X4 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 1305 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1306 | // TODO(geza): Write direct sse functions that do not compute |
| 1307 | // variance as well. |
| 1308 | cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride, |
| 1309 | &sse); |
| 1310 | |
| 1311 | if (plane == 0) x->pred_sse[ref] = sse; |
| 1312 | |
| 1313 | total_sse += sse; |
| 1314 | |
| 1315 | model_rd_from_sse(cpi, xd, bs, plane, sse, &rate, &dist); |
| 1316 | |
| 1317 | rate_sum += rate; |
| 1318 | dist_sum += dist; |
| 1319 | } |
| 1320 | |
| 1321 | *skip_txfm_sb = total_sse == 0; |
| 1322 | *skip_sse_sb = total_sse << 4; |
| 1323 | *out_rate_sum = (int)rate_sum; |
| 1324 | *out_dist_sum = dist_sum; |
| 1325 | } |
| 1326 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1327 | int64_t av1_block_error_c(const tran_low_t *coeff, const tran_low_t *dqcoeff, |
| 1328 | intptr_t block_size, int64_t *ssz) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1329 | int i; |
| 1330 | int64_t error = 0, sqcoeff = 0; |
| 1331 | |
| 1332 | for (i = 0; i < block_size; i++) { |
| 1333 | const int diff = coeff[i] - dqcoeff[i]; |
| 1334 | error += diff * diff; |
| 1335 | sqcoeff += coeff[i] * coeff[i]; |
| 1336 | } |
| 1337 | |
| 1338 | *ssz = sqcoeff; |
| 1339 | return error; |
| 1340 | } |
| 1341 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1342 | int64_t av1_block_error_fp_c(const int16_t *coeff, const int16_t *dqcoeff, |
| 1343 | int block_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1344 | int i; |
| 1345 | int64_t error = 0; |
| 1346 | |
| 1347 | for (i = 0; i < block_size; i++) { |
| 1348 | const int diff = coeff[i] - dqcoeff[i]; |
| 1349 | error += diff * diff; |
| 1350 | } |
| 1351 | |
| 1352 | return error; |
| 1353 | } |
| 1354 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1355 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1356 | int64_t av1_highbd_block_error_c(const tran_low_t *coeff, |
| 1357 | const tran_low_t *dqcoeff, intptr_t block_size, |
| 1358 | int64_t *ssz, int bd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1359 | int i; |
| 1360 | int64_t error = 0, sqcoeff = 0; |
| 1361 | int shift = 2 * (bd - 8); |
| 1362 | int rounding = shift > 0 ? 1 << (shift - 1) : 0; |
| 1363 | |
| 1364 | for (i = 0; i < block_size; i++) { |
| 1365 | const int64_t diff = coeff[i] - dqcoeff[i]; |
| 1366 | error += diff * diff; |
| 1367 | sqcoeff += (int64_t)coeff[i] * (int64_t)coeff[i]; |
| 1368 | } |
| 1369 | assert(error >= 0 && sqcoeff >= 0); |
| 1370 | error = (error + rounding) >> shift; |
| 1371 | sqcoeff = (sqcoeff + rounding) >> shift; |
| 1372 | |
| 1373 | *ssz = sqcoeff; |
| 1374 | return error; |
| 1375 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1376 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1377 | |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1378 | #if CONFIG_PVQ |
| 1379 | // Without PVQ, av1_block_error_c() return two kind of errors, |
| 1380 | // 1) reconstruction (i.e. decoded) error and |
| 1381 | // 2) Squared sum of transformed residue (i.e. 'coeff') |
| 1382 | // However, if PVQ is enabled, coeff does not keep the transformed residue |
| 1383 | // but instead a transformed original is kept. |
| 1384 | // Hence, new parameter ref vector (i.e. transformed predicted signal) |
| 1385 | // is required to derive the residue signal, |
| 1386 | // i.e. coeff - ref = residue (all transformed). |
| 1387 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1388 | #if CONFIG_HIGHBITDEPTH |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1389 | static int64_t av1_highbd_block_error2_c(const tran_low_t *coeff, |
| 1390 | const tran_low_t *dqcoeff, |
| 1391 | const tran_low_t *ref, |
| 1392 | intptr_t block_size, int64_t *ssz, |
| 1393 | int bd) { |
| 1394 | int64_t error; |
| 1395 | int64_t sqcoeff; |
| 1396 | int shift = 2 * (bd - 8); |
| 1397 | int rounding = shift > 0 ? 1 << (shift - 1) : 0; |
| 1398 | // Use the existing sse codes for calculating distortion of decoded signal: |
| 1399 | // i.e. (orig - decoded)^2 |
| 1400 | // For high bit depth, throw away ssz until a 32-bit version of |
| 1401 | // av1_block_error_fp is written. |
| 1402 | int64_t ssz_trash; |
| 1403 | error = av1_block_error(coeff, dqcoeff, block_size, &ssz_trash); |
| 1404 | // prediction residue^2 = (orig - ref)^2 |
| 1405 | sqcoeff = av1_block_error(coeff, ref, block_size, &ssz_trash); |
| 1406 | error = (error + rounding) >> shift; |
| 1407 | sqcoeff = (sqcoeff + rounding) >> shift; |
| 1408 | *ssz = sqcoeff; |
| 1409 | return error; |
| 1410 | } |
| 1411 | #else |
| 1412 | // TODO(yushin) : Since 4x4 case does not need ssz, better to refactor into |
| 1413 | // a separate function that does not do the extra computations for ssz. |
| 1414 | static int64_t av1_block_error2_c(const tran_low_t *coeff, |
| 1415 | const tran_low_t *dqcoeff, |
| 1416 | const tran_low_t *ref, intptr_t block_size, |
| 1417 | int64_t *ssz) { |
| 1418 | int64_t error; |
Yushin Cho | ead552a | 2017-09-25 15:10:25 -0700 | [diff] [blame] | 1419 | int64_t ssz_trash; |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1420 | // Use the existing sse codes for calculating distortion of decoded signal: |
| 1421 | // i.e. (orig - decoded)^2 |
Yushin Cho | ead552a | 2017-09-25 15:10:25 -0700 | [diff] [blame] | 1422 | error = av1_block_error(coeff, dqcoeff, block_size, &ssz_trash); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1423 | // prediction residue^2 = (orig - ref)^2 |
Yushin Cho | ead552a | 2017-09-25 15:10:25 -0700 | [diff] [blame] | 1424 | *ssz = av1_block_error(coeff, ref, block_size, &ssz_trash); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1425 | return error; |
| 1426 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1427 | #endif // CONFIG_HIGHBITDEPTH |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1428 | #endif // CONFIG_PVQ |
| 1429 | |
Jingning Han | ab77e73 | 2017-02-28 15:20:59 -0800 | [diff] [blame] | 1430 | #if !CONFIG_PVQ || CONFIG_VAR_TX |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1431 | #if !CONFIG_LV_MAP |
| 1432 | static int cost_coeffs(const AV1_COMMON *const cm, MACROBLOCK *x, int plane, |
| 1433 | int block, TX_SIZE tx_size, const SCAN_ORDER *scan_order, |
| 1434 | const ENTROPY_CONTEXT *a, const ENTROPY_CONTEXT *l, |
| 1435 | int use_fast_coef_costing) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1436 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1437 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 1438 | const struct macroblock_plane *p = &x->plane[plane]; |
| 1439 | const struct macroblockd_plane *pd = &xd->plane[plane]; |
| 1440 | const PLANE_TYPE type = pd->plane_type; |
| 1441 | const uint16_t *band_count = &band_count_table[tx_size][1]; |
| 1442 | const int eob = p->eobs[block]; |
| 1443 | const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block); |
Urvang Joshi | 9752a2e | 2017-10-02 17:32:27 -0700 | [diff] [blame] | 1444 | const TX_SIZE tx_size_ctx = txsize_sqr_map[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1445 | uint8_t token_cache[MAX_TX_SQUARE]; |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 1446 | int pt = combine_entropy_contexts(*a, *l); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1447 | int c, cost; |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 1448 | const int16_t *scan = scan_order->scan; |
| 1449 | const int16_t *nb = scan_order->neighbors; |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 1450 | const int ref = is_inter_block(mbmi); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1451 | int(*head_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] = |
| 1452 | x->token_head_costs[tx_size_ctx][type][ref]; |
| 1453 | int(*tail_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] = |
| 1454 | x->token_tail_costs[tx_size_ctx][type][ref]; |
| 1455 | const int seg_eob = av1_get_tx_eob(&cm->seg, mbmi->segment_id, tx_size); |
| 1456 | int eob_val; |
Thomas Davies | 1052575 | 2017-03-06 12:10:46 +0000 | [diff] [blame] | 1457 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1458 | #if CONFIG_HIGHBITDEPTH |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1459 | 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] | 1460 | #else |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1461 | const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, 8); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1462 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1463 | |
| 1464 | #if !CONFIG_VAR_TX && !CONFIG_SUPERTX |
| 1465 | // Check for consistency of tx_size with mode info |
hui su | 0c6244b | 2017-07-12 17:11:43 -0700 | [diff] [blame] | 1466 | assert(tx_size == av1_get_tx_size(plane, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1467 | #endif // !CONFIG_VAR_TX && !CONFIG_SUPERTX |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 1468 | (void)cm; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1469 | |
| 1470 | if (eob == 0) { |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1471 | // block zero |
| 1472 | cost = (*head_token_costs)[pt][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1473 | } else { |
| 1474 | if (use_fast_coef_costing) { |
| 1475 | int band_left = *band_count++; |
| 1476 | |
| 1477 | // dc token |
| 1478 | int v = qcoeff[0]; |
| 1479 | int16_t prev_t; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1480 | cost = av1_get_token_cost(v, &prev_t, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1481 | eob_val = (eob == 1) ? EARLY_EOB : NO_EOB; |
| 1482 | cost += av1_get_coeff_token_cost( |
| 1483 | 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] | 1484 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1485 | token_cache[0] = av1_pt_energy_class[prev_t]; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1486 | ++head_token_costs; |
| 1487 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1488 | |
| 1489 | // ac tokens |
| 1490 | for (c = 1; c < eob; c++) { |
| 1491 | const int rc = scan[c]; |
| 1492 | int16_t t; |
| 1493 | |
| 1494 | v = qcoeff[rc]; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1495 | cost += av1_get_token_cost(v, &t, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1496 | eob_val = |
| 1497 | (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB; |
| 1498 | cost += av1_get_coeff_token_cost(t, eob_val, 0, |
| 1499 | (*head_token_costs)[!prev_t], |
| 1500 | (*tail_token_costs)[!prev_t]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1501 | prev_t = t; |
| 1502 | if (!--band_left) { |
| 1503 | band_left = *band_count++; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1504 | ++head_token_costs; |
| 1505 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1506 | } |
| 1507 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1508 | } else { // !use_fast_coef_costing |
| 1509 | int band_left = *band_count++; |
| 1510 | |
| 1511 | // dc token |
| 1512 | int v = qcoeff[0]; |
| 1513 | int16_t tok; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1514 | cost = av1_get_token_cost(v, &tok, cat6_bits); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1515 | eob_val = (eob == 1) ? EARLY_EOB : NO_EOB; |
| 1516 | cost += av1_get_coeff_token_cost(tok, eob_val, 1, (*head_token_costs)[pt], |
| 1517 | (*tail_token_costs)[pt]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1518 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1519 | token_cache[0] = av1_pt_energy_class[tok]; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1520 | ++head_token_costs; |
| 1521 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1522 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1523 | // ac tokens |
| 1524 | for (c = 1; c < eob; c++) { |
| 1525 | const int rc = scan[c]; |
| 1526 | |
| 1527 | v = qcoeff[rc]; |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1528 | cost += av1_get_token_cost(v, &tok, cat6_bits); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1529 | pt = get_coef_context(nb, token_cache, c); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1530 | eob_val = |
| 1531 | (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB; |
| 1532 | cost += av1_get_coeff_token_cost( |
| 1533 | tok, eob_val, 0, (*head_token_costs)[pt], (*tail_token_costs)[pt]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1534 | token_cache[rc] = av1_pt_energy_class[tok]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1535 | if (!--band_left) { |
| 1536 | band_left = *band_count++; |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 1537 | ++head_token_costs; |
| 1538 | ++tail_token_costs; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1539 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1540 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1544 | return cost; |
| 1545 | } |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1546 | #endif // !CONFIG_LV_MAP |
| 1547 | |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 1548 | 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] | 1549 | int blk_row, int blk_col, int block, TX_SIZE tx_size, |
| 1550 | const SCAN_ORDER *scan_order, const ENTROPY_CONTEXT *a, |
| 1551 | const ENTROPY_CONTEXT *l, int use_fast_coef_costing) { |
Angie Chiang | 3627de2 | 2017-08-18 20:15:59 -0700 | [diff] [blame] | 1552 | const AV1_COMMON *const cm = &cpi->common; |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1553 | #if !CONFIG_LV_MAP |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 1554 | (void)blk_row; |
| 1555 | (void)blk_col; |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 1556 | #if CONFIG_MRC_TX |
| 1557 | const MACROBLOCKD *xd = &x->e_mbd; |
| 1558 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 1559 | const TX_TYPE tx_type = av1_get_tx_type(xd->plane[plane].plane_type, xd, |
| 1560 | blk_row, blk_col, block, tx_size); |
| 1561 | const int is_inter = is_inter_block(mbmi); |
| 1562 | if (tx_type == MRC_DCT && ((is_inter && SIGNAL_MRC_MASK_INTER) || |
| 1563 | (!is_inter && SIGNAL_MRC_MASK_INTRA))) { |
| 1564 | const int mrc_mask_cost = |
| 1565 | av1_cost_color_map(x, plane, block, mbmi->sb_type, tx_size, MRC_MAP); |
| 1566 | return cost_coeffs(cm, x, plane, block, tx_size, scan_order, a, l, |
| 1567 | use_fast_coef_costing) + |
| 1568 | mrc_mask_cost; |
| 1569 | } |
| 1570 | #endif |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1571 | return cost_coeffs(cm, x, plane, block, tx_size, scan_order, a, l, |
| 1572 | use_fast_coef_costing); |
| 1573 | #else // !CONFIG_LV_MAP |
| 1574 | (void)scan_order; |
| 1575 | (void)use_fast_coef_costing; |
| 1576 | const MACROBLOCKD *xd = &x->e_mbd; |
| 1577 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 1578 | const struct macroblockd_plane *pd = &xd->plane[plane]; |
| 1579 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Timothy B. Terriberry | 81ec261 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 1580 | #if CONFIG_CHROMA_SUB8X8 |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1581 | const BLOCK_SIZE plane_bsize = |
| 1582 | AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd)); |
Timothy B. Terriberry | 81ec261 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 1583 | #elif CONFIG_CB4X4 |
| 1584 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1585 | #else // CONFIG_CB4X4 |
| 1586 | const BLOCK_SIZE plane_bsize = |
| 1587 | get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd); |
| 1588 | #endif // CONFIG_CB4X4 |
| 1589 | |
| 1590 | TXB_CTX txb_ctx; |
| 1591 | get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx); |
Angie Chiang | 3627de2 | 2017-08-18 20:15:59 -0700 | [diff] [blame] | 1592 | 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] | 1593 | &txb_ctx); |
Angie Chiang | 47e4b36 | 2017-03-24 11:25:10 -0700 | [diff] [blame] | 1594 | #endif // !CONFIG_LV_MAP |
| 1595 | } |
Fergus Simpson | 0b96b47 | 2017-03-07 15:20:28 -0800 | [diff] [blame] | 1596 | #endif // !CONFIG_PVQ || CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1597 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1598 | // Get transform block visible dimensions cropped to the MI units. |
| 1599 | static void get_txb_dimensions(const MACROBLOCKD *xd, int plane, |
| 1600 | BLOCK_SIZE plane_bsize, int blk_row, int blk_col, |
| 1601 | BLOCK_SIZE tx_bsize, int *width, int *height, |
| 1602 | int *visible_width, int *visible_height) { |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 1603 | #if !(CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX)) |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1604 | assert(tx_bsize <= plane_bsize); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 1605 | #endif |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1606 | int txb_height = block_size_high[tx_bsize]; |
| 1607 | int txb_width = block_size_wide[tx_bsize]; |
| 1608 | const int block_height = block_size_high[plane_bsize]; |
| 1609 | const int block_width = block_size_wide[plane_bsize]; |
| 1610 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1611 | // TODO(aconverse@google.com): Investigate using crop_width/height here rather |
| 1612 | // than the MI size |
| 1613 | const int block_rows = |
| 1614 | (xd->mb_to_bottom_edge >= 0) |
| 1615 | ? block_height |
| 1616 | : (xd->mb_to_bottom_edge >> (3 + pd->subsampling_y)) + block_height; |
| 1617 | const int block_cols = |
| 1618 | (xd->mb_to_right_edge >= 0) |
| 1619 | ? block_width |
| 1620 | : (xd->mb_to_right_edge >> (3 + pd->subsampling_x)) + block_width; |
| 1621 | const int tx_unit_size = tx_size_wide_log2[0]; |
| 1622 | if (width) *width = txb_width; |
| 1623 | if (height) *height = txb_height; |
| 1624 | *visible_width = clamp(block_cols - (blk_col << tx_unit_size), 0, txb_width); |
| 1625 | *visible_height = |
| 1626 | clamp(block_rows - (blk_row << tx_unit_size), 0, txb_height); |
| 1627 | } |
| 1628 | |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1629 | // Compute the pixel domain distortion from src and dst on all visible 4x4s in |
| 1630 | // the |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1631 | // transform block. |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1632 | static unsigned pixel_dist(const AV1_COMP *const cpi, const MACROBLOCK *x, |
| 1633 | int plane, const uint8_t *src, const int src_stride, |
| 1634 | const uint8_t *dst, const int dst_stride, |
| 1635 | int blk_row, int blk_col, |
| 1636 | const BLOCK_SIZE plane_bsize, |
| 1637 | const BLOCK_SIZE tx_bsize) { |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1638 | int txb_rows, txb_cols, visible_rows, visible_cols; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1639 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1640 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1641 | get_txb_dimensions(xd, plane, plane_bsize, blk_row, blk_col, tx_bsize, |
| 1642 | &txb_cols, &txb_rows, &visible_cols, &visible_rows); |
| 1643 | assert(visible_rows > 0); |
| 1644 | assert(visible_cols > 0); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1645 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1646 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1647 | 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] | 1648 | return (unsigned)av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride, |
| 1649 | tx_bsize, txb_cols, txb_rows, visible_cols, |
| 1650 | visible_rows, x->qindex); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1651 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1652 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 1653 | unsigned sse = pixel_dist_visible_only(cpi, x, src, src_stride, dst, |
| 1654 | dst_stride, tx_bsize, txb_rows, |
| 1655 | txb_cols, visible_rows, visible_cols); |
| 1656 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1657 | return sse; |
| 1658 | } |
| 1659 | |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1660 | // Compute the pixel domain distortion from diff on all visible 4x4s in the |
| 1661 | // transform block. |
| 1662 | static int64_t pixel_diff_dist(const MACROBLOCK *x, int plane, |
| 1663 | const int16_t *diff, const int diff_stride, |
| 1664 | int blk_row, int blk_col, |
| 1665 | const BLOCK_SIZE plane_bsize, |
| 1666 | const BLOCK_SIZE tx_bsize) { |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1667 | int visible_rows, visible_cols; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1668 | const MACROBLOCKD *xd = &x->e_mbd; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1669 | #if CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1670 | int txb_height = block_size_high[tx_bsize]; |
| 1671 | int txb_width = block_size_wide[tx_bsize]; |
| 1672 | const int src_stride = x->plane[plane].src.stride; |
| 1673 | const int src_idx = (blk_row * src_stride + blk_col) << tx_size_wide_log2[0]; |
| 1674 | const uint8_t *src = &x->plane[plane].src.buf[src_idx]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1675 | #endif |
| 1676 | |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1677 | get_txb_dimensions(xd, plane, plane_bsize, blk_row, blk_col, tx_bsize, NULL, |
| 1678 | NULL, &visible_cols, &visible_rows); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1679 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1680 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1681 | 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] | 1682 | 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] | 1683 | txb_height, visible_cols, visible_rows, x->qindex); |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1684 | else |
| 1685 | #endif |
| 1686 | return aom_sum_squares_2d_i16(diff, diff_stride, visible_cols, |
| 1687 | visible_rows); |
Alex Converse | 61f37b8 | 2017-03-29 15:26:03 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1690 | int av1_count_colors(const uint8_t *src, int stride, int rows, int cols) { |
| 1691 | int val_count[256]; |
| 1692 | memset(val_count, 0, sizeof(val_count)); |
| 1693 | for (int r = 0; r < rows; ++r) { |
| 1694 | for (int c = 0; c < cols; ++c) { |
| 1695 | ++val_count[src[r * stride + c]]; |
| 1696 | } |
| 1697 | } |
| 1698 | int n = 0; |
| 1699 | for (int i = 0; i < 256; ++i) { |
| 1700 | if (val_count[i]) ++n; |
| 1701 | } |
| 1702 | return n; |
| 1703 | } |
| 1704 | |
| 1705 | #if CONFIG_HIGHBITDEPTH |
| 1706 | int av1_count_colors_highbd(const uint8_t *src8, int stride, int rows, int cols, |
| 1707 | int bit_depth) { |
| 1708 | assert(bit_depth <= 12); |
| 1709 | const uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
| 1710 | int val_count[1 << 12]; |
| 1711 | memset(val_count, 0, (1 << 12) * sizeof(val_count[0])); |
| 1712 | for (int r = 0; r < rows; ++r) { |
| 1713 | for (int c = 0; c < cols; ++c) { |
| 1714 | ++val_count[src[r * stride + c]]; |
| 1715 | } |
| 1716 | } |
| 1717 | int n = 0; |
| 1718 | for (int i = 0; i < (1 << bit_depth); ++i) { |
| 1719 | if (val_count[i]) ++n; |
| 1720 | } |
| 1721 | return n; |
| 1722 | } |
| 1723 | #endif // CONFIG_HIGHBITDEPTH |
hui su | d9a812b | 2017-07-06 14:34:37 -0700 | [diff] [blame] | 1724 | |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 1725 | void av1_dist_block(const AV1_COMP *cpi, MACROBLOCK *x, int plane, |
| 1726 | BLOCK_SIZE plane_bsize, int block, int blk_row, int blk_col, |
| 1727 | TX_SIZE tx_size, int64_t *out_dist, int64_t *out_sse, |
| 1728 | OUTPUT_STATUS output_status) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1729 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1730 | const struct macroblock_plane *const p = &x->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1731 | #if CONFIG_DIST_8X8 |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 1732 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1733 | #else // CONFIG_DIST_8X8 |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 1734 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1735 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 1736 | |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1737 | if (cpi->sf.use_transform_domain_distortion |
| 1738 | #if CONFIG_DIST_8X8 |
| 1739 | && !x->using_dist_8x8 |
| 1740 | #endif |
| 1741 | ) { |
hui su | d2f12ba | 2017-04-12 10:08:43 -0700 | [diff] [blame] | 1742 | // Transform domain distortion computation is more efficient as it does |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1743 | // not involve an inverse transform, but it is less accurate. |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1744 | const int buffer_length = tx_size_2d[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1745 | int64_t this_sse; |
Jingning Han | ff70545 | 2017-04-27 11:32:15 -0700 | [diff] [blame] | 1746 | int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1747 | tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
| 1748 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1749 | #if CONFIG_PVQ |
Yaowu Xu | d6ea71c | 2016-11-07 10:24:14 -0800 | [diff] [blame] | 1750 | tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1751 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1752 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1753 | 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] | 1754 | *out_dist = av1_highbd_block_error2_c(coeff, dqcoeff, ref_coeff, |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1755 | buffer_length, &this_sse, bd); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1756 | #else |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1757 | *out_dist = |
| 1758 | av1_block_error2_c(coeff, dqcoeff, ref_coeff, buffer_length, &this_sse); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1759 | #endif // CONFIG_HIGHBITDEPTH |
Yi Luo | d61e608 | 2017-05-26 16:14:39 -0700 | [diff] [blame] | 1760 | #else // !CONFIG_PVQ |
| 1761 | #if CONFIG_HIGHBITDEPTH |
| 1762 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 1763 | *out_dist = av1_highbd_block_error(coeff, dqcoeff, buffer_length, |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1764 | &this_sse, xd->bd); |
Yi Luo | d61e608 | 2017-05-26 16:14:39 -0700 | [diff] [blame] | 1765 | else |
| 1766 | #endif |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1767 | *out_dist = av1_block_error(coeff, dqcoeff, buffer_length, &this_sse); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 1768 | #endif // CONFIG_PVQ |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1769 | *out_dist = RIGHT_SIGNED_SHIFT(*out_dist, shift); |
| 1770 | *out_sse = RIGHT_SIGNED_SHIFT(this_sse, shift); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1771 | } else { |
| 1772 | const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size]; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1773 | #if !CONFIG_PVQ || CONFIG_DIST_8X8 |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1774 | const int bsw = block_size_wide[tx_bsize]; |
| 1775 | const int bsh = block_size_high[tx_bsize]; |
Angie Chiang | 50910f6 | 2017-04-03 12:31:34 -0700 | [diff] [blame] | 1776 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1777 | const int src_stride = x->plane[plane].src.stride; |
| 1778 | const int dst_stride = xd->plane[plane].dst.stride; |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1779 | // Scale the transform block index to pixel unit. |
| 1780 | const int src_idx = (blk_row * src_stride + blk_col) |
| 1781 | << tx_size_wide_log2[0]; |
| 1782 | const int dst_idx = (blk_row * dst_stride + blk_col) |
| 1783 | << tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1784 | const uint8_t *src = &x->plane[plane].src.buf[src_idx]; |
| 1785 | const uint8_t *dst = &xd->plane[plane].dst.buf[dst_idx]; |
| 1786 | const tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 1787 | const uint16_t eob = p->eobs[block]; |
| 1788 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1789 | assert(cpi != NULL); |
Jingning Han | b9c5727 | 2016-10-25 10:15:39 -0700 | [diff] [blame] | 1790 | assert(tx_size_wide_log2[0] == tx_size_high_log2[0]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1791 | |
Angie Chiang | c0cf6c0 | 2017-04-04 13:47:27 -0700 | [diff] [blame] | 1792 | { |
| 1793 | const int diff_stride = block_size_wide[plane_bsize]; |
| 1794 | const int diff_idx = (blk_row * diff_stride + blk_col) |
| 1795 | << tx_size_wide_log2[0]; |
| 1796 | const int16_t *diff = &p->src_diff[diff_idx]; |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1797 | *out_sse = pixel_diff_dist(x, plane, diff, diff_stride, blk_row, blk_col, |
| 1798 | plane_bsize, tx_bsize); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1799 | #if CONFIG_HIGHBITDEPTH |
hui su | b1cc1f9 | 2017-04-11 17:41:29 -0700 | [diff] [blame] | 1800 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 1801 | *out_sse = ROUND_POWER_OF_TWO(*out_sse, (xd->bd - 8) * 2); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1802 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | c0cf6c0 | 2017-04-04 13:47:27 -0700 | [diff] [blame] | 1803 | } |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 1804 | *out_sse *= 16; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1805 | |
| 1806 | if (eob) { |
Angie Chiang | 228cc18 | 2017-04-07 15:22:16 -0700 | [diff] [blame] | 1807 | if (output_status == OUTPUT_HAS_DECODED_PIXELS) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1808 | *out_dist = pixel_dist(cpi, x, plane, src, src_stride, dst, dst_stride, |
| 1809 | blk_row, blk_col, plane_bsize, tx_bsize); |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1810 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1811 | #if CONFIG_HIGHBITDEPTH |
Jingning Han | 6a9dbef | 2017-04-10 10:25:14 -0700 | [diff] [blame] | 1812 | uint8_t *recon; |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1813 | DECLARE_ALIGNED(16, uint16_t, recon16[MAX_TX_SQUARE]); |
Jingning Han | 6a9dbef | 2017-04-10 10:25:14 -0700 | [diff] [blame] | 1814 | |
| 1815 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 1816 | recon = CONVERT_TO_BYTEPTR(recon16); |
| 1817 | else |
| 1818 | recon = (uint8_t *)recon16; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1819 | #else |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1820 | DECLARE_ALIGNED(16, uint8_t, recon[MAX_TX_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1821 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1822 | |
Angie Chiang | 50910f6 | 2017-04-03 12:31:34 -0700 | [diff] [blame] | 1823 | #if !CONFIG_PVQ |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1824 | #if CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1825 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1826 | aom_highbd_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, |
| 1827 | NULL, 0, bsw, bsh, xd->bd); |
| 1828 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1829 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1830 | aom_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, NULL, |
| 1831 | 0, bsw, bsh); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1832 | #if CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1833 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 1834 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | c0cf6c0 | 2017-04-04 13:47:27 -0700 | [diff] [blame] | 1835 | #else |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1836 | (void)dst; |
Angie Chiang | 50910f6 | 2017-04-03 12:31:34 -0700 | [diff] [blame] | 1837 | #endif // !CONFIG_PVQ |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 1838 | |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 1839 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 1840 | uint8_t *mrc_mask = BLOCK_OFFSET(xd->mrc_mask, block); |
| 1841 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1842 | const PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 1843 | TX_TYPE tx_type = |
| 1844 | 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] | 1845 | av1_inverse_transform_block(xd, dqcoeff, |
| 1846 | #if CONFIG_LGT |
| 1847 | xd->mi[0]->mbmi.mode, |
| 1848 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 1849 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 1850 | mrc_mask, |
| 1851 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 1852 | tx_type, tx_size, recon, MAX_TX_SIZE, eob); |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 1853 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1854 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 1855 | if (x->using_dist_8x8 && plane == 0 && (bsw < 8 || bsh < 8)) { |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1856 | // Save decoded pixels for inter block in pd->pred to avoid |
| 1857 | // block_8x8_rd_txfm_daala_dist() need to produce them |
| 1858 | // by calling av1_inverse_transform_block() again. |
| 1859 | const int pred_stride = block_size_wide[plane_bsize]; |
| 1860 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 1861 | << tx_size_wide_log2[0]; |
| 1862 | int16_t *pred = &pd->pred[pred_idx]; |
| 1863 | int i, j; |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 1864 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 1865 | #if CONFIG_HIGHBITDEPTH |
| 1866 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1867 | for (j = 0; j < bsh; j++) |
| 1868 | for (i = 0; i < bsw; i++) |
| 1869 | pred[j * pred_stride + i] = |
| 1870 | CONVERT_TO_SHORTPTR(recon)[j * MAX_TX_SIZE + i]; |
| 1871 | } else { |
| 1872 | #endif |
| 1873 | for (j = 0; j < bsh; j++) |
| 1874 | for (i = 0; i < bsw; i++) |
| 1875 | pred[j * pred_stride + i] = recon[j * MAX_TX_SIZE + i]; |
| 1876 | #if CONFIG_HIGHBITDEPTH |
| 1877 | } |
| 1878 | #endif // CONFIG_HIGHBITDEPTH |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1879 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 1880 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 1881 | *out_dist = |
| 1882 | pixel_dist(cpi, x, plane, src, src_stride, recon, MAX_TX_SIZE, |
| 1883 | blk_row, blk_col, plane_bsize, tx_bsize); |
Angie Chiang | 8f6ddec | 2017-04-04 17:07:00 -0700 | [diff] [blame] | 1884 | } |
Alex Converse | f323f01 | 2017-04-24 09:26:33 -0700 | [diff] [blame] | 1885 | *out_dist *= 16; |
| 1886 | } else { |
| 1887 | *out_dist = *out_sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1888 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1889 | } |
| 1890 | } |
| 1891 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1892 | static void block_rd_txfm(int plane, int block, int blk_row, int blk_col, |
| 1893 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) { |
| 1894 | struct rdcost_block_args *args = arg; |
| 1895 | MACROBLOCK *const x = args->x; |
| 1896 | MACROBLOCKD *const xd = &x->e_mbd; |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 1897 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | e50f3ec | 2017-04-10 15:50:33 -0700 | [diff] [blame] | 1898 | const AV1_COMP *cpi = args->cpi; |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 1899 | ENTROPY_CONTEXT *a = args->t_above + blk_col; |
| 1900 | ENTROPY_CONTEXT *l = args->t_left + blk_row; |
Angie Chiang | 18ad894 | 2017-04-11 12:37:07 -0700 | [diff] [blame] | 1901 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1902 | int64_t rd1, rd2, rd; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 1903 | RD_STATS this_rd_stats; |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 1904 | |
| 1905 | #if CONFIG_DIST_8X8 |
| 1906 | // If sub8x8 tx, 8x8 or larger partition, and luma channel, |
| 1907 | // dist-8x8 disables early skip, because the distortion metrics for |
| 1908 | // sub8x8 tx (MSE) and reference distortion from 8x8 or larger partition |
| 1909 | // (new distortion metric) are different. |
| 1910 | // Exception is: dist-8x8 is enabled but still MSE is used, |
| 1911 | // i.e. "--tune=" encoder option is not used. |
| 1912 | int disable_early_skip = |
| 1913 | x->using_dist_8x8 && plane == 0 && plane_bsize >= BLOCK_8X8 && |
| 1914 | (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4) && |
| 1915 | x->tune_metric != AOM_TUNE_PSNR; |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 1916 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 6341f5c | 2017-03-24 14:36:28 -0700 | [diff] [blame] | 1917 | |
Jingning Han | e65436b | 2017-06-14 09:26:11 -0700 | [diff] [blame] | 1918 | #if !CONFIG_SUPERTX && !CONFIG_VAR_TX |
hui su | 0c6244b | 2017-07-12 17:11:43 -0700 | [diff] [blame] | 1919 | assert(tx_size == av1_get_tx_size(plane, xd)); |
Yue Chen | 8e689e4 | 2017-06-02 10:56:10 -0700 | [diff] [blame] | 1920 | #endif // !CONFIG_SUPERTX |
Angie Chiang | bc2288c | 2017-04-09 15:41:17 -0700 | [diff] [blame] | 1921 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 1922 | av1_init_rd_stats(&this_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1923 | |
| 1924 | if (args->exit_early) return; |
| 1925 | |
| 1926 | if (!is_inter_block(mbmi)) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 1927 | av1_predict_intra_block_facade(cm, xd, plane, block, blk_col, blk_row, |
| 1928 | tx_size); |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 1929 | av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size); |
| 1930 | } |
| 1931 | |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 1932 | #if !CONFIG_TXK_SEL |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 1933 | // full forward transform and quantization |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 1934 | const int coeff_ctx = combine_entropy_contexts(*a, *l); |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 1935 | #if DISABLE_TRELLISQ_SEARCH |
| 1936 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 1937 | coeff_ctx, AV1_XFORM_QUANT_B); |
| 1938 | #else |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 1939 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 1940 | coeff_ctx, AV1_XFORM_QUANT_FP); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 1941 | |
| 1942 | const int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
| 1943 | tran_low_t *const coeff = BLOCK_OFFSET(x->plane[plane].coeff, block); |
| 1944 | tran_low_t *const dqcoeff = BLOCK_OFFSET(xd->plane[plane].dqcoeff, block); |
| 1945 | const int buffer_length = tx_size_2d[tx_size]; |
| 1946 | int64_t tmp_dist; |
| 1947 | int64_t tmp; |
| 1948 | #if CONFIG_HIGHBITDEPTH |
| 1949 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 1950 | tmp_dist = |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1951 | av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp, xd->bd); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 1952 | else |
| 1953 | #endif |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 1954 | tmp_dist = av1_block_error(coeff, dqcoeff, buffer_length, &tmp); |
| 1955 | tmp_dist = RIGHT_SIGNED_SHIFT(tmp_dist, shift); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 1956 | |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 1957 | if ( |
| 1958 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 1959 | disable_early_skip || |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 1960 | #endif |
| 1961 | RDCOST(x->rdmult, 0, tmp_dist) + args->this_rd < args->best_rd) { |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 1962 | 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] | 1963 | a, l, 1); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 1964 | } else { |
| 1965 | args->exit_early = 1; |
| 1966 | return; |
| 1967 | } |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 1968 | #endif // DISABLE_TRELLISQ_SEARCH |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 1969 | |
Sarah Parker | c5ccd4c | 2017-08-03 11:27:50 -0700 | [diff] [blame] | 1970 | #if CONFIG_MRC_TX |
| 1971 | if (mbmi->tx_type == MRC_DCT && !mbmi->valid_mrc_mask) { |
| 1972 | args->exit_early = 1; |
| 1973 | return; |
| 1974 | } |
| 1975 | #endif // CONFIG_MRC_TX |
| 1976 | |
Angie Chiang | 62e54cd | 2017-04-06 10:45:56 -0700 | [diff] [blame] | 1977 | if (!is_inter_block(mbmi)) { |
| 1978 | struct macroblock_plane *const p = &x->plane[plane]; |
Angie Chiang | bc2288c | 2017-04-09 15:41:17 -0700 | [diff] [blame] | 1979 | av1_inverse_transform_block_facade(xd, plane, block, blk_row, blk_col, |
| 1980 | p->eobs[block]); |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 1981 | av1_dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col, |
| 1982 | tx_size, &this_rd_stats.dist, &this_rd_stats.sse, |
| 1983 | OUTPUT_HAS_DECODED_PIXELS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1984 | } else { |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 1985 | av1_dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col, |
| 1986 | tx_size, &this_rd_stats.dist, &this_rd_stats.sse, |
| 1987 | OUTPUT_HAS_PREDICTED_PIXELS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1988 | } |
Luc Trudeau | aa94baa | 2017-04-27 10:52:21 -0400 | [diff] [blame] | 1989 | #if CONFIG_CFL |
Luc Trudeau | fcca37a | 2017-08-14 15:05:07 -0400 | [diff] [blame] | 1990 | if (plane == AOM_PLANE_Y && xd->cfl->store_y) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 1991 | #if CONFIG_CHROMA_SUB8X8 |
| 1992 | assert(!is_inter_block(mbmi) || plane_bsize < BLOCK_8X8); |
| 1993 | #else |
| 1994 | assert(!is_inter_block(mbmi)); |
| 1995 | #endif // CONFIG_CHROMA_SUB8X8 |
| 1996 | cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize); |
Luc Trudeau | aa94baa | 2017-04-27 10:52:21 -0400 | [diff] [blame] | 1997 | } |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 1998 | #endif // CONFIG_CFL |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 1999 | rd = RDCOST(x->rdmult, 0, this_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2000 | if (args->this_rd + rd > args->best_rd) { |
| 2001 | args->exit_early = 1; |
| 2002 | return; |
| 2003 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2004 | #if !CONFIG_PVQ |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2005 | const PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2006 | const TX_TYPE tx_type = |
| 2007 | 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] | 2008 | 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] | 2009 | this_rd_stats.rate = |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 2010 | av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block, tx_size, |
| 2011 | scan_order, a, l, args->use_fast_coef_costing); |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2012 | #else // !CONFIG_PVQ |
| 2013 | this_rd_stats.rate = x->rate; |
| 2014 | #endif // !CONFIG_PVQ |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2015 | #else // !CONFIG_TXK_SEL |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2016 | av1_search_txk_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize, |
| 2017 | tx_size, a, l, args->use_fast_coef_costing, |
| 2018 | &this_rd_stats); |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2019 | #endif // !CONFIG_TXK_SEL |
Angie Chiang | 65a39bb | 2017-04-11 16:50:04 -0700 | [diff] [blame] | 2020 | |
| 2021 | #if !CONFIG_PVQ |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2022 | #if CONFIG_RD_DEBUG |
Angie Chiang | e94556b | 2016-11-09 10:59:30 -0800 | [diff] [blame] | 2023 | av1_update_txb_coeff_cost(&this_rd_stats, plane, tx_size, blk_row, blk_col, |
| 2024 | this_rd_stats.rate); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2025 | #endif // CONFIG_RD_DEBUG |
Yushin Cho | 6341f5c | 2017-03-24 14:36:28 -0700 | [diff] [blame] | 2026 | av1_set_txb_context(x, plane, block, tx_size, a, l); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2027 | #endif // !CONFIG_PVQ |
Angie Chiang | b3a12b5 | 2017-03-23 14:53:10 -0700 | [diff] [blame] | 2028 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2029 | rd1 = RDCOST(x->rdmult, this_rd_stats.rate, this_rd_stats.dist); |
| 2030 | rd2 = RDCOST(x->rdmult, 0, this_rd_stats.sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2031 | |
| 2032 | // TODO(jingning): temporarily enabled only for luma component |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2033 | rd = AOMMIN(rd1, rd2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2034 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2035 | #if !CONFIG_PVQ |
| 2036 | this_rd_stats.skip &= !x->plane[plane].eobs[block]; |
| 2037 | #else |
| 2038 | this_rd_stats.skip &= x->pvq_skip[plane]; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2039 | #endif // !CONFIG_PVQ |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2040 | av1_merge_rd_stats(&args->rd_stats, &this_rd_stats); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2041 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2042 | args->this_rd += rd; |
| 2043 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2044 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 2045 | if (!disable_early_skip) |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2046 | #endif |
| 2047 | if (args->this_rd > args->best_rd) { |
| 2048 | args->exit_early = 1; |
| 2049 | return; |
| 2050 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2053 | #if CONFIG_DIST_8X8 |
| 2054 | static void dist_8x8_sub8x8_txfm_rd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2055 | BLOCK_SIZE bsize, |
| 2056 | struct rdcost_block_args *args) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2057 | MACROBLOCKD *const xd = &x->e_mbd; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2058 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 2059 | const struct macroblock_plane *const p = &x->plane[0]; |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 2060 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2061 | const int src_stride = p->src.stride; |
| 2062 | const int dst_stride = pd->dst.stride; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2063 | const uint8_t *src = &p->src.buf[0]; |
| 2064 | const uint8_t *dst = &pd->dst.buf[0]; |
| 2065 | const int16_t *pred = &pd->pred[0]; |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 2066 | int bw = block_size_wide[bsize]; |
| 2067 | int bh = block_size_high[bsize]; |
| 2068 | int visible_w = bw; |
| 2069 | int visible_h = bh; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2070 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2071 | int i, j; |
| 2072 | int64_t rd, rd1, rd2; |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2073 | unsigned int tmp1, tmp2; |
| 2074 | int qindex = x->qindex; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2075 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2076 | assert((bw & 0x07) == 0); |
| 2077 | assert((bh & 0x07) == 0); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2078 | |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 2079 | get_txb_dimensions(xd, 0, bsize, 0, 0, bsize, &bw, &bh, &visible_w, |
| 2080 | &visible_h); |
| 2081 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 2082 | #if CONFIG_HIGHBITDEPTH |
| 2083 | uint8_t *pred8; |
| 2084 | DECLARE_ALIGNED(16, uint16_t, pred16[MAX_TX_SQUARE]); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2085 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 2086 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 2087 | pred8 = CONVERT_TO_BYTEPTR(pred16); |
| 2088 | else |
| 2089 | pred8 = (uint8_t *)pred16; |
| 2090 | #else |
| 2091 | DECLARE_ALIGNED(16, uint8_t, pred8[MAX_TX_SQUARE]); |
| 2092 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2093 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 2094 | #if CONFIG_HIGHBITDEPTH |
| 2095 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 2096 | for (j = 0; j < bh; j++) |
| 2097 | for (i = 0; i < bw; i++) |
| 2098 | CONVERT_TO_SHORTPTR(pred8)[j * bw + i] = pred[j * bw + i]; |
| 2099 | } else { |
| 2100 | #endif |
| 2101 | for (j = 0; j < bh; j++) |
Yaowu Xu | 7a47170 | 2017-09-29 08:38:37 -0700 | [diff] [blame] | 2102 | 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] | 2103 | #if CONFIG_HIGHBITDEPTH |
| 2104 | } |
| 2105 | #endif // CONFIG_HIGHBITDEPTH |
| 2106 | |
Yushin Cho | fcddadf | 2017-08-30 13:49:38 -0700 | [diff] [blame] | 2107 | 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] | 2108 | bh, visible_w, visible_h, qindex); |
Yushin Cho | fcddadf | 2017-08-30 13:49:38 -0700 | [diff] [blame] | 2109 | 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] | 2110 | bw, bh, visible_w, visible_h, qindex); |
Yushin Cho | 4483e3d | 2017-04-18 19:41:20 -0700 | [diff] [blame] | 2111 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2112 | if (!is_inter_block(mbmi)) { |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 2113 | if (x->tune_metric == AOM_TUNE_PSNR) { |
| 2114 | assert(args->rd_stats.sse == tmp1 * 16); |
| 2115 | assert(args->rd_stats.dist == tmp2 * 16); |
| 2116 | } |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2117 | args->rd_stats.sse = (int64_t)tmp1 * 16; |
| 2118 | args->rd_stats.dist = (int64_t)tmp2 * 16; |
| 2119 | } else { |
| 2120 | // For inter mode, the decoded pixels are provided in pd->pred, |
| 2121 | // while the predicted pixels are in dst. |
Yushin Cho | 2f025aa | 2017-09-28 17:39:21 -0700 | [diff] [blame] | 2122 | if (x->tune_metric == AOM_TUNE_PSNR) { |
| 2123 | assert(args->rd_stats.sse == tmp2 * 16); |
| 2124 | assert(args->rd_stats.dist == tmp1 * 16); |
| 2125 | } |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2126 | args->rd_stats.sse = (int64_t)tmp2 * 16; |
| 2127 | args->rd_stats.dist = (int64_t)tmp1 * 16; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2128 | } |
| 2129 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2130 | rd1 = RDCOST(x->rdmult, args->rd_stats.rate, args->rd_stats.dist); |
| 2131 | rd2 = RDCOST(x->rdmult, 0, args->rd_stats.sse); |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2132 | rd = AOMMIN(rd1, rd2); |
| 2133 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2134 | args->rd_stats.rdcost = rd; |
| 2135 | args->this_rd = rd; |
Yushin Cho | 04eb959 | 2017-06-21 17:35:06 -0700 | [diff] [blame] | 2136 | |
| 2137 | if (args->this_rd > args->best_rd) args->exit_early = 1; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2138 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2139 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2140 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2141 | static void txfm_rd_in_plane(MACROBLOCK *x, const AV1_COMP *cpi, |
| 2142 | RD_STATS *rd_stats, int64_t ref_best_rd, int plane, |
| 2143 | BLOCK_SIZE bsize, TX_SIZE tx_size, |
| 2144 | int use_fast_coef_casting) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2145 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2146 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2147 | struct rdcost_block_args args; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2148 | av1_zero(args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2149 | args.x = x; |
| 2150 | args.cpi = cpi; |
| 2151 | args.best_rd = ref_best_rd; |
| 2152 | args.use_fast_coef_costing = use_fast_coef_casting; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2153 | av1_init_rd_stats(&args.rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2154 | |
| 2155 | if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size; |
| 2156 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2157 | 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] | 2158 | |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2159 | av1_foreach_transformed_block_in_plane(xd, bsize, plane, block_rd_txfm, |
| 2160 | &args); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2161 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 2162 | if (x->using_dist_8x8 && !args.exit_early && plane == 0 && |
| 2163 | bsize >= BLOCK_8X8 && |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2164 | (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4)) |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 2165 | dist_8x8_sub8x8_txfm_rd(cpi, x, bsize, &args); |
Yushin Cho | 30a2c5f | 2017-06-07 14:18:54 -0700 | [diff] [blame] | 2166 | #endif |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 2167 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2168 | if (args.exit_early) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2169 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2170 | } else { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2171 | *rd_stats = args.rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | #if CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2176 | void av1_txfm_rd_in_plane_supertx(MACROBLOCK *x, const AV1_COMP *cpi, int *rate, |
| 2177 | int64_t *distortion, int *skippable, |
| 2178 | int64_t *sse, int64_t ref_best_rd, int plane, |
| 2179 | BLOCK_SIZE bsize, TX_SIZE tx_size, |
| 2180 | int use_fast_coef_casting) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2181 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2182 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 2183 | struct rdcost_block_args args; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2184 | av1_zero(args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2185 | args.cpi = cpi; |
| 2186 | args.x = x; |
| 2187 | args.best_rd = ref_best_rd; |
| 2188 | args.use_fast_coef_costing = use_fast_coef_casting; |
| 2189 | |
| 2190 | #if CONFIG_EXT_TX |
| 2191 | assert(tx_size < TX_SIZES); |
| 2192 | #endif // CONFIG_EXT_TX |
| 2193 | |
| 2194 | if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size; |
| 2195 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2196 | 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] | 2197 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2198 | block_rd_txfm(plane, 0, 0, 0, get_plane_block_size(bsize, pd), tx_size, |
| 2199 | &args); |
| 2200 | |
| 2201 | if (args.exit_early) { |
| 2202 | *rate = INT_MAX; |
| 2203 | *distortion = INT64_MAX; |
| 2204 | *sse = INT64_MAX; |
| 2205 | *skippable = 0; |
| 2206 | } else { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2207 | *distortion = args.rd_stats.dist; |
| 2208 | *rate = args.rd_stats.rate; |
| 2209 | *sse = args.rd_stats.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2210 | *skippable = !x->plane[plane].eobs[0]; |
| 2211 | } |
| 2212 | } |
| 2213 | #endif // CONFIG_SUPERTX |
| 2214 | |
Urvang Joshi | 9752a2e | 2017-10-02 17:32:27 -0700 | [diff] [blame] | 2215 | static TX_SIZE tx_size_cost(const AV1_COMP *const cpi, |
| 2216 | const MACROBLOCK *const x, BLOCK_SIZE bsize, |
| 2217 | TX_SIZE tx_size) { |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2218 | const AV1_COMMON *const cm = &cpi->common; |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2219 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 2220 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2221 | |
Rupert Swarbrick | 93c39e9 | 2017-07-12 11:11:02 +0100 | [diff] [blame] | 2222 | const int tx_select = cm->tx_mode == TX_MODE_SELECT && |
| 2223 | #if CONFIG_EXT_PARTITION_TYPES |
| 2224 | // Currently these block shapes can only use 4x4 |
| 2225 | // transforms |
| 2226 | mbmi->sb_type != BLOCK_4X16 && |
| 2227 | mbmi->sb_type != BLOCK_16X4 && |
| 2228 | #endif |
| 2229 | mbmi->sb_type >= BLOCK_8X8; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2230 | |
| 2231 | if (tx_select) { |
| 2232 | const int is_inter = is_inter_block(mbmi); |
Urvang Joshi | 9752a2e | 2017-10-02 17:32:27 -0700 | [diff] [blame] | 2233 | const TX_SIZE tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize] |
| 2234 | : intra_tx_size_cat_lookup[bsize]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2235 | const TX_SIZE coded_tx_size = txsize_sqr_up_map[tx_size]; |
| 2236 | const int depth = tx_size_to_depth(coded_tx_size); |
Urvang Joshi | 9752a2e | 2017-10-02 17:32:27 -0700 | [diff] [blame] | 2237 | const TX_SIZE tx_size_ctx = get_tx_size_context(xd); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2238 | 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] | 2239 | #if CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX) |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 2240 | if (is_quarter_tx_allowed(xd, mbmi, is_inter) && tx_size != coded_tx_size) |
| 2241 | r_tx_size += av1_cost_bit(cm->fc->quarter_tx_size_prob, |
| 2242 | tx_size == quarter_txsize_lookup[bsize]); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 2243 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2244 | return r_tx_size; |
| 2245 | } else { |
| 2246 | return 0; |
| 2247 | } |
| 2248 | } |
| 2249 | |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2250 | // TODO(angiebird): use this function whenever it's possible |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2251 | int av1_tx_type_cost(const AV1_COMMON *cm, const MACROBLOCK *x, |
| 2252 | const MACROBLOCKD *xd, BLOCK_SIZE bsize, int plane, |
| 2253 | TX_SIZE tx_size, TX_TYPE tx_type) { |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 2254 | if (plane > 0) return 0; |
| 2255 | |
Jingning Han | 243b66b | 2017-06-23 12:11:47 -0700 | [diff] [blame] | 2256 | #if CONFIG_VAR_TX |
| 2257 | tx_size = get_min_tx_size(tx_size); |
| 2258 | #endif |
| 2259 | |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2260 | const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 2261 | const int is_inter = is_inter_block(mbmi); |
| 2262 | #if CONFIG_EXT_TX |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2263 | if (get_ext_tx_types(tx_size, bsize, is_inter, cm->reduced_tx_set_used) > 1 && |
| 2264 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
| 2265 | const int ext_tx_set = |
| 2266 | get_ext_tx_set(tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
| 2267 | if (is_inter) { |
| 2268 | if (ext_tx_set > 0) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2269 | return x |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2270 | ->inter_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]][tx_type]; |
| 2271 | } else { |
| 2272 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2273 | return x->intra_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]] |
| 2274 | [mbmi->mode][tx_type]; |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2275 | } |
| 2276 | } |
| 2277 | #else |
| 2278 | (void)bsize; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2279 | (void)cm; |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2280 | if (tx_size < TX_32X32 && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 2281 | !FIXED_TX_TYPE) { |
| 2282 | if (is_inter) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2283 | return x->inter_tx_type_costs[tx_size][tx_type]; |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2284 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2285 | return x->intra_tx_type_costs[tx_size] |
| 2286 | [intra_mode_to_tx_type_context[mbmi->mode]] |
| 2287 | [tx_type]; |
Angie Chiang | 6520156 | 2017-04-10 15:23:28 -0700 | [diff] [blame] | 2288 | } |
| 2289 | } |
| 2290 | #endif // CONFIG_EXT_TX |
| 2291 | return 0; |
| 2292 | } |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2293 | static int64_t txfm_yrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2294 | RD_STATS *rd_stats, int64_t ref_best_rd, BLOCK_SIZE bs, |
Urvang Joshi | 9752a2e | 2017-10-02 17:32:27 -0700 | [diff] [blame] | 2295 | TX_TYPE tx_type, TX_SIZE tx_size) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2296 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2297 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2298 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2299 | int64_t rd = INT64_MAX; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2300 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2301 | int s0, s1; |
| 2302 | const int is_inter = is_inter_block(mbmi); |
Jingning Han | bf9c6b7 | 2016-12-14 14:50:45 -0800 | [diff] [blame] | 2303 | const int tx_select = |
| 2304 | cm->tx_mode == TX_MODE_SELECT && mbmi->sb_type >= BLOCK_8X8; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 2305 | |
| 2306 | 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] | 2307 | |
Yushin Cho | cd4f4a2 | 2017-07-10 18:19:05 -0700 | [diff] [blame] | 2308 | #if CONFIG_PVQ |
| 2309 | assert(tx_size >= TX_4X4); |
| 2310 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2311 | assert(skip_prob > 0); |
| 2312 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2313 | assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed_bsize(bs))); |
| 2314 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2315 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2316 | s0 = av1_cost_bit(skip_prob, 0); |
| 2317 | s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2318 | |
| 2319 | mbmi->tx_type = tx_type; |
| 2320 | mbmi->tx_size = tx_size; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2321 | 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] | 2322 | cpi->sf.use_fast_coef_costing); |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2323 | if (rd_stats->rate == INT_MAX) return INT64_MAX; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2324 | #if !CONFIG_TXK_SEL |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 2325 | int plane = 0; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2326 | 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] | 2327 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2328 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2329 | if (rd_stats->skip) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2330 | if (is_inter) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2331 | rd = RDCOST(x->rdmult, s1, rd_stats->sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2332 | } else { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2333 | 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] | 2334 | } |
| 2335 | } else { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2336 | rd = RDCOST(x->rdmult, rd_stats->rate + s0 + r_tx_size * tx_select, |
| 2337 | rd_stats->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2338 | } |
| 2339 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2340 | if (tx_select) rd_stats->rate += r_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2341 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2342 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 2343 | !(rd_stats->skip)) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2344 | rd = AOMMIN(rd, RDCOST(x->rdmult, s1, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2345 | |
| 2346 | return rd; |
| 2347 | } |
| 2348 | |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2349 | static int skip_txfm_search(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs, |
| 2350 | TX_TYPE tx_type, TX_SIZE tx_size) { |
Urvang Joshi | 0d1e4ff | 2017-04-27 16:17:25 -0700 | [diff] [blame] | 2351 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 2352 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2353 | const TX_SIZE max_tx_size = max_txsize_lookup[bs]; |
| 2354 | const int is_inter = is_inter_block(mbmi); |
| 2355 | int prune = 0; |
| 2356 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 2357 | // passing -1 in for tx_type indicates that all 1D |
| 2358 | // transforms should be considered for pruning |
| 2359 | prune = prune_tx_types(cpi, bs, x, xd, -1); |
| 2360 | |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 2361 | #if CONFIG_MRC_TX |
| 2362 | // MRC_DCT only implemented for TX_32X32 so only include this tx in |
| 2363 | // the search for TX_32X32 |
Sarah Parker | 2e08d96 | 2017-08-01 19:51:20 -0700 | [diff] [blame] | 2364 | if (tx_type == MRC_DCT && |
| 2365 | ((is_inter && !USE_MRC_INTER) || (!is_inter && !USE_MRC_INTRA) || |
| 2366 | tx_size != TX_32X32)) |
| 2367 | return 1; |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 2368 | #endif // CONFIG_MRC_TX |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2369 | if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) return 1; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2370 | if (FIXED_TX_TYPE && tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2371 | return 1; |
| 2372 | if (!is_inter && x->use_default_intra_tx_type && |
| 2373 | tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2374 | return 1; |
| 2375 | if (is_inter && x->use_default_inter_tx_type && |
| 2376 | tx_type != get_default_tx_type(0, xd, 0, tx_size)) |
| 2377 | return 1; |
| 2378 | if (max_tx_size >= TX_32X32 && tx_size == TX_4X4) return 1; |
| 2379 | #if CONFIG_EXT_TX |
| 2380 | const AV1_COMMON *const cm = &cpi->common; |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2381 | const TxSetType tx_set_type = |
| 2382 | get_ext_tx_set_type(tx_size, bs, is_inter, cm->reduced_tx_set_used); |
| 2383 | if (!av1_ext_tx_used[tx_set_type][tx_type]) return 1; |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2384 | if (is_inter) { |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2385 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 2386 | if (!do_tx_type_search(tx_type, prune)) return 1; |
| 2387 | } |
| 2388 | } else { |
| 2389 | if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) { |
| 2390 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) return 1; |
| 2391 | } |
Angie Chiang | 2d147c1 | 2017-04-05 11:23:59 -0700 | [diff] [blame] | 2392 | } |
| 2393 | #else // CONFIG_EXT_TX |
| 2394 | if (tx_size >= TX_32X32 && tx_type != DCT_DCT) return 1; |
| 2395 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 2396 | !do_tx_type_search(tx_type, prune)) |
| 2397 | return 1; |
| 2398 | #endif // CONFIG_EXT_TX |
| 2399 | return 0; |
| 2400 | } |
| 2401 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 2402 | #if (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT || CONFIG_INTERINTRA) |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2403 | static int64_t estimate_yrd_for_sb(const AV1_COMP *const cpi, BLOCK_SIZE bs, |
| 2404 | MACROBLOCK *x, int *r, int64_t *d, int *s, |
| 2405 | int64_t *sse, int64_t ref_best_rd) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2406 | RD_STATS rd_stats; |
| 2407 | int64_t rd = txfm_yrd(cpi, x, &rd_stats, ref_best_rd, bs, DCT_DCT, |
| 2408 | max_txsize_lookup[bs]); |
| 2409 | *r = rd_stats.rate; |
| 2410 | *d = rd_stats.dist; |
| 2411 | *s = rd_stats.skip; |
| 2412 | *sse = rd_stats.sse; |
| 2413 | return rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2414 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 2415 | #endif // (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2416 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2417 | 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] | 2418 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2419 | BLOCK_SIZE bs) { |
| 2420 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2421 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2422 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2423 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2424 | int64_t this_rd, best_rd = INT64_MAX; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2425 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
| 2426 | int s0 = av1_cost_bit(skip_prob, 0); |
| 2427 | int s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2428 | const int is_inter = is_inter_block(mbmi); |
| 2429 | int prune = 0; |
Angie Chiang | aa0c34b | 2017-04-25 12:25:38 -0700 | [diff] [blame] | 2430 | const int plane = 0; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2431 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2432 | |
| 2433 | 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] | 2434 | #if CONFIG_VAR_TX |
| 2435 | mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2436 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2437 | #if CONFIG_EXT_TX |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2438 | int ext_tx_set = |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 2439 | 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] | 2440 | const TxSetType tx_set_type = |
| 2441 | 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] | 2442 | #endif // CONFIG_EXT_TX |
| 2443 | |
| 2444 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 2445 | #if CONFIG_EXT_TX |
| 2446 | prune = prune_tx_types(cpi, bs, x, xd, ext_tx_set); |
| 2447 | #else |
| 2448 | prune = prune_tx_types(cpi, bs, x, xd, 0); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2449 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2450 | #if CONFIG_EXT_TX |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 2451 | if (get_ext_tx_types(mbmi->tx_size, bs, is_inter, cm->reduced_tx_set_used) > |
| 2452 | 1 && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2453 | !xd->lossless[mbmi->segment_id]) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2454 | #if CONFIG_PVQ |
| 2455 | od_rollback_buffer pre_buf, post_buf; |
| 2456 | |
| 2457 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 2458 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2459 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2460 | |
| 2461 | for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) { |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2462 | if (!av1_ext_tx_used[tx_set_type][tx_type]) continue; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2463 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2464 | if (is_inter) { |
| 2465 | if (x->use_default_inter_tx_type && |
| 2466 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2467 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2468 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 2469 | if (!do_tx_type_search(tx_type, prune)) continue; |
| 2470 | } |
| 2471 | } else { |
| 2472 | if (x->use_default_intra_tx_type && |
| 2473 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2474 | continue; |
| 2475 | if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) { |
| 2476 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 2477 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | mbmi->tx_type = tx_type; |
| 2481 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2482 | 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] | 2483 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2484 | #if CONFIG_PVQ |
| 2485 | od_encode_rollback(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2486 | #endif // CONFIG_PVQ |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2487 | if (this_rd_stats.rate == INT_MAX) continue; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2488 | 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] | 2489 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2490 | if (this_rd_stats.skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2491 | this_rd = RDCOST(x->rdmult, s1, this_rd_stats.sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2492 | else |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2493 | this_rd = |
| 2494 | RDCOST(x->rdmult, this_rd_stats.rate + s0, this_rd_stats.dist); |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2495 | if (is_inter_block(mbmi) && !xd->lossless[mbmi->segment_id] && |
| 2496 | !this_rd_stats.skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2497 | 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] | 2498 | |
| 2499 | if (this_rd < best_rd) { |
| 2500 | best_rd = this_rd; |
| 2501 | best_tx_type = mbmi->tx_type; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2502 | *rd_stats = this_rd_stats; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2503 | #if CONFIG_PVQ |
| 2504 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2505 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2506 | } |
| 2507 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2508 | #if CONFIG_PVQ |
| 2509 | od_encode_rollback(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2510 | #endif // CONFIG_PVQ |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 2511 | } else { |
| 2512 | mbmi->tx_type = DCT_DCT; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2513 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size, |
| 2514 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2515 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2516 | #else // CONFIG_EXT_TX |
| 2517 | if (mbmi->tx_size < TX_32X32 && !xd->lossless[mbmi->segment_id]) { |
| 2518 | for (tx_type = 0; tx_type < TX_TYPES; ++tx_type) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2519 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2520 | if (!is_inter && x->use_default_intra_tx_type && |
| 2521 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2522 | continue; |
| 2523 | if (is_inter && x->use_default_inter_tx_type && |
| 2524 | tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size)) |
| 2525 | continue; |
| 2526 | mbmi->tx_type = tx_type; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2527 | 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] | 2528 | mbmi->tx_size, cpi->sf.use_fast_coef_costing); |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2529 | if (this_rd_stats.rate == INT_MAX) continue; |
Angie Chiang | aa0c34b | 2017-04-25 12:25:38 -0700 | [diff] [blame] | 2530 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 2531 | 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] | 2532 | if (is_inter) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2533 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 2534 | !do_tx_type_search(tx_type, prune)) |
| 2535 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2536 | } |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2537 | if (this_rd_stats.skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2538 | this_rd = RDCOST(x->rdmult, s1, this_rd_stats.sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2539 | else |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2540 | this_rd = |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2541 | RDCOST(x->rdmult, this_rd_stats.rate + s0, this_rd_stats.dist); |
| 2542 | if (is_inter && !xd->lossless[mbmi->segment_id] && !this_rd_stats.skip) |
| 2543 | 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] | 2544 | |
| 2545 | if (this_rd < best_rd) { |
| 2546 | best_rd = this_rd; |
| 2547 | best_tx_type = mbmi->tx_type; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2548 | *rd_stats = this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2549 | } |
| 2550 | } |
Guillaume Martres | 4e4d3a0 | 2016-08-21 19:02:33 -0700 | [diff] [blame] | 2551 | } else { |
| 2552 | mbmi->tx_type = DCT_DCT; |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2553 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size, |
| 2554 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2555 | } |
| 2556 | #endif // CONFIG_EXT_TX |
| 2557 | mbmi->tx_type = best_tx_type; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2560 | 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] | 2561 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2562 | BLOCK_SIZE bs) { |
| 2563 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2564 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 2565 | |
| 2566 | mbmi->tx_size = TX_4X4; |
| 2567 | mbmi->tx_type = DCT_DCT; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2568 | #if CONFIG_VAR_TX |
| 2569 | mbmi->min_tx_size = get_min_tx_size(TX_4X4); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2570 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2571 | |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 2572 | txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size, |
| 2573 | cpi->sf.use_fast_coef_costing); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2576 | #if CONFIG_TXK_SEL || CONFIG_VAR_TX |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2577 | static INLINE int bsize_to_num_blk(BLOCK_SIZE bsize) { |
| 2578 | int num_blk = 1 << (num_pels_log2_lookup[bsize] - 2 * tx_size_wide_log2[0]); |
| 2579 | return num_blk; |
| 2580 | } |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2581 | #endif // CONFIG_TXK_SEL || CONFIG_VAR_TX |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2582 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 2583 | 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] | 2584 | MACROBLOCK *x, RD_STATS *rd_stats, |
| 2585 | int64_t ref_best_rd, BLOCK_SIZE bs) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2586 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2587 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2588 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2589 | int64_t rd = INT64_MAX; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2590 | int n; |
| 2591 | int start_tx, end_tx; |
| 2592 | int64_t best_rd = INT64_MAX, last_rd = INT64_MAX; |
| 2593 | const TX_SIZE max_tx_size = max_txsize_lookup[bs]; |
| 2594 | TX_SIZE best_tx_size = max_tx_size; |
| 2595 | TX_TYPE best_tx_type = DCT_DCT; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2596 | #if CONFIG_TXK_SEL |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2597 | 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] | 2598 | #endif // CONFIG_TXK_SEL |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2599 | const int tx_select = cm->tx_mode == TX_MODE_SELECT; |
| 2600 | const int is_inter = is_inter_block(mbmi); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2601 | #if CONFIG_PVQ |
| 2602 | od_rollback_buffer buf; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2603 | od_encode_checkpoint(&x->daala_enc, &buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2604 | #endif // CONFIG_PVQ |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2605 | |
| 2606 | av1_invalid_rd_stats(rd_stats); |
| 2607 | |
| 2608 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2609 | int evaluate_rect_tx = 0; |
| 2610 | if (tx_select) { |
| 2611 | evaluate_rect_tx = is_rect_tx_allowed(xd, mbmi); |
| 2612 | } else { |
| 2613 | const TX_SIZE chosen_tx_size = |
| 2614 | tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 2615 | evaluate_rect_tx = is_rect_tx(chosen_tx_size); |
| 2616 | assert(IMPLIES(evaluate_rect_tx, is_rect_tx_allowed(xd, mbmi))); |
| 2617 | } |
| 2618 | if (evaluate_rect_tx) { |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2619 | TX_TYPE tx_start = DCT_DCT; |
| 2620 | TX_TYPE tx_end = TX_TYPES; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2621 | #if CONFIG_TXK_SEL |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2622 | // The tx_type becomes dummy when lv_map is on. The tx_type search will be |
| 2623 | // performed in av1_search_txk_type() |
| 2624 | tx_end = DCT_DCT + 1; |
| 2625 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2626 | TX_TYPE tx_type; |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2627 | for (tx_type = tx_start; tx_type < tx_end; ++tx_type) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2628 | if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) continue; |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2629 | const TX_SIZE rect_tx_size = max_txsize_rect_lookup[bs]; |
| 2630 | RD_STATS this_rd_stats; |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2631 | const TxSetType tx_set_type = get_ext_tx_set_type( |
| 2632 | rect_tx_size, bs, is_inter, cm->reduced_tx_set_used); |
| 2633 | if (av1_ext_tx_used[tx_set_type][tx_type]) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2634 | rd = txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, |
| 2635 | rect_tx_size); |
Hui Su | da816a1 | 2017-08-18 14:46:02 -0700 | [diff] [blame] | 2636 | ref_best_rd = AOMMIN(rd, ref_best_rd); |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2637 | if (rd < best_rd) { |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2638 | #if CONFIG_TXK_SEL |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2639 | 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] | 2640 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2641 | best_tx_type = tx_type; |
| 2642 | best_tx_size = rect_tx_size; |
| 2643 | best_rd = rd; |
| 2644 | *rd_stats = this_rd_stats; |
| 2645 | } |
| 2646 | } |
Debargha Mukherjee | 094c943 | 2017-02-22 10:31:25 -0800 | [diff] [blame] | 2647 | #if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2648 | const int is_inter = is_inter_block(mbmi); |
| 2649 | if (mbmi->sb_type < BLOCK_8X8 && is_inter) break; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2650 | #endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2651 | } |
| 2652 | } |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 2653 | |
| 2654 | #if CONFIG_RECT_TX_EXT |
| 2655 | // test 1:4/4:1 tx |
| 2656 | int evaluate_quarter_tx = 0; |
| 2657 | if (is_quarter_tx_allowed(xd, mbmi, is_inter)) { |
| 2658 | if (tx_select) { |
| 2659 | evaluate_quarter_tx = 1; |
| 2660 | } else { |
| 2661 | const TX_SIZE chosen_tx_size = |
| 2662 | tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 2663 | evaluate_quarter_tx = chosen_tx_size == quarter_txsize_lookup[bs]; |
| 2664 | } |
| 2665 | } |
| 2666 | if (evaluate_quarter_tx) { |
| 2667 | TX_TYPE tx_start = DCT_DCT; |
| 2668 | TX_TYPE tx_end = TX_TYPES; |
| 2669 | #if CONFIG_TXK_SEL |
| 2670 | // The tx_type becomes dummy when lv_map is on. The tx_type search will be |
| 2671 | // performed in av1_search_txk_type() |
| 2672 | tx_end = DCT_DCT + 1; |
| 2673 | #endif |
| 2674 | TX_TYPE tx_type; |
| 2675 | for (tx_type = tx_start; tx_type < tx_end; ++tx_type) { |
| 2676 | if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) continue; |
| 2677 | const TX_SIZE tx_size = quarter_txsize_lookup[bs]; |
| 2678 | RD_STATS this_rd_stats; |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 2679 | const TxSetType tx_set_type = |
| 2680 | get_ext_tx_set_type(tx_size, bs, is_inter, cm->reduced_tx_set_used); |
| 2681 | if (av1_ext_tx_used[tx_set_type][tx_type]) { |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 2682 | rd = |
| 2683 | txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, tx_size); |
| 2684 | if (rd < best_rd) { |
| 2685 | #if CONFIG_TXK_SEL |
| 2686 | memcpy(best_txk_type, mbmi->txk_type, |
| 2687 | sizeof(best_txk_type[0]) * num_blk); |
| 2688 | #endif |
| 2689 | best_tx_type = tx_type; |
| 2690 | best_tx_size = tx_size; |
| 2691 | best_rd = rd; |
| 2692 | *rd_stats = this_rd_stats; |
| 2693 | } |
| 2694 | } |
| 2695 | #if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
| 2696 | const int is_inter = is_inter_block(mbmi); |
| 2697 | if (mbmi->sb_type < BLOCK_8X8 && is_inter) break; |
| 2698 | #endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
| 2699 | } |
| 2700 | } |
| 2701 | #endif // CONFIG_RECT_TX_EXT |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2702 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2703 | |
| 2704 | if (tx_select) { |
| 2705 | start_tx = max_tx_size; |
| 2706 | end_tx = (max_tx_size >= TX_32X32) ? TX_8X8 : TX_4X4; |
| 2707 | } else { |
| 2708 | const TX_SIZE chosen_tx_size = |
| 2709 | tx_size_from_tx_mode(bs, cm->tx_mode, is_inter); |
| 2710 | start_tx = chosen_tx_size; |
| 2711 | end_tx = chosen_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2712 | } |
| 2713 | |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2714 | last_rd = INT64_MAX; |
| 2715 | for (n = start_tx; n >= end_tx; --n) { |
Sarah Parker | 3666192 | 2017-04-13 14:26:49 -0700 | [diff] [blame] | 2716 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 2717 | if (is_rect_tx(n)) break; |
| 2718 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2719 | TX_TYPE tx_start = DCT_DCT; |
| 2720 | TX_TYPE tx_end = TX_TYPES; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2721 | #if CONFIG_TXK_SEL |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2722 | // The tx_type becomes dummy when lv_map is on. The tx_type search will be |
| 2723 | // performed in av1_search_txk_type() |
| 2724 | tx_end = DCT_DCT + 1; |
| 2725 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2726 | TX_TYPE tx_type; |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 2727 | for (tx_type = tx_start; tx_type < tx_end; ++tx_type) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2728 | RD_STATS this_rd_stats; |
| 2729 | if (skip_txfm_search(cpi, x, bs, tx_type, n)) continue; |
| 2730 | rd = txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, n); |
| 2731 | #if CONFIG_PVQ |
| 2732 | od_encode_rollback(&x->daala_enc, &buf); |
| 2733 | #endif // CONFIG_PVQ |
| 2734 | // Early termination in transform size search. |
| 2735 | if (cpi->sf.tx_size_search_breakout && |
| 2736 | (rd == INT64_MAX || |
| 2737 | (this_rd_stats.skip == 1 && tx_type != DCT_DCT && n < start_tx) || |
| 2738 | (n < (int)max_tx_size && rd > last_rd))) |
| 2739 | break; |
| 2740 | |
| 2741 | last_rd = rd; |
Hui Su | da816a1 | 2017-08-18 14:46:02 -0700 | [diff] [blame] | 2742 | ref_best_rd = AOMMIN(rd, ref_best_rd); |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2743 | if (rd < best_rd) { |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2744 | #if CONFIG_TXK_SEL |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2745 | 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] | 2746 | #endif |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2747 | best_tx_type = tx_type; |
| 2748 | best_tx_size = n; |
| 2749 | best_rd = rd; |
| 2750 | *rd_stats = this_rd_stats; |
| 2751 | } |
| 2752 | #if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
| 2753 | const int is_inter = is_inter_block(mbmi); |
| 2754 | if (mbmi->sb_type < BLOCK_8X8 && is_inter) break; |
| 2755 | #endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 |
| 2756 | } |
| 2757 | } |
| 2758 | mbmi->tx_size = best_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2759 | mbmi->tx_type = best_tx_type; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2760 | #if CONFIG_TXK_SEL |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 2761 | 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] | 2762 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2763 | |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2764 | #if CONFIG_VAR_TX |
| 2765 | mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2766 | #endif // CONFIG_VAR_TX |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 2767 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2768 | #if !CONFIG_EXT_TX |
| 2769 | if (mbmi->tx_size >= TX_32X32) assert(mbmi->tx_type == DCT_DCT); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2770 | #endif // !CONFIG_EXT_TX |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2771 | #if CONFIG_PVQ |
Yushin Cho | 403618e | 2016-11-09 10:45:32 -0800 | [diff] [blame] | 2772 | if (best_rd != INT64_MAX) { |
Angie Chiang | a4fa190 | 2017-04-05 15:26:09 -0700 | [diff] [blame] | 2773 | 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] | 2774 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 2775 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2778 | static void super_block_yrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2779 | RD_STATS *rd_stats, BLOCK_SIZE bs, |
| 2780 | int64_t ref_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2781 | MACROBLOCKD *xd = &x->e_mbd; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2782 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2783 | |
| 2784 | assert(bs == xd->mi[0]->mbmi.sb_type); |
| 2785 | |
Yaowu Xu | 1e2aae1 | 2017-02-27 16:33:14 -0800 | [diff] [blame] | 2786 | if (xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2787 | choose_smallest_tx_size(cpi, x, rd_stats, ref_best_rd, bs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2788 | } else if (cpi->sf.tx_size_search_method == USE_LARGESTALL) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2789 | choose_largest_tx_size(cpi, x, rd_stats, ref_best_rd, bs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2790 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2791 | 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] | 2792 | } |
| 2793 | } |
| 2794 | |
| 2795 | static int conditional_skipintra(PREDICTION_MODE mode, |
| 2796 | PREDICTION_MODE best_intra_mode) { |
| 2797 | if (mode == D117_PRED && best_intra_mode != V_PRED && |
| 2798 | best_intra_mode != D135_PRED) |
| 2799 | return 1; |
| 2800 | if (mode == D63_PRED && best_intra_mode != V_PRED && |
| 2801 | best_intra_mode != D45_PRED) |
| 2802 | return 1; |
| 2803 | if (mode == D207_PRED && best_intra_mode != H_PRED && |
| 2804 | best_intra_mode != D45_PRED) |
| 2805 | return 1; |
| 2806 | if (mode == D153_PRED && best_intra_mode != H_PRED && |
| 2807 | best_intra_mode != D135_PRED) |
| 2808 | return 1; |
| 2809 | return 0; |
| 2810 | } |
| 2811 | |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2812 | // Model based RD estimation for luma intra blocks. |
| 2813 | 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] | 2814 | BLOCK_SIZE bsize, int mode_cost) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 2815 | const AV1_COMMON *cm = &cpi->common; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2816 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2817 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 2818 | assert(!is_inter_block(mbmi)); |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2819 | RD_STATS this_rd_stats; |
| 2820 | int row, col; |
| 2821 | int64_t temp_sse, this_rd; |
| 2822 | const TX_SIZE tx_size = tx_size_from_tx_mode(bsize, cpi->common.tx_mode, 0); |
| 2823 | const int stepr = tx_size_high_unit[tx_size]; |
| 2824 | const int stepc = tx_size_wide_unit[tx_size]; |
| 2825 | const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
| 2826 | const int max_blocks_high = max_block_high(xd, bsize, 0); |
| 2827 | mbmi->tx_size = tx_size; |
| 2828 | // Prediction. |
Angie Chiang | 3d005e4 | 2017-04-02 16:31:35 -0700 | [diff] [blame] | 2829 | const int step = stepr * stepc; |
| 2830 | int block = 0; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2831 | for (row = 0; row < max_blocks_high; row += stepr) { |
| 2832 | for (col = 0; col < max_blocks_wide; col += stepc) { |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 2833 | 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] | 2834 | block += step; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | // RD estimation. |
| 2838 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &this_rd_stats.rate, |
| 2839 | &this_rd_stats.dist, &this_rd_stats.skip, &temp_sse); |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 2840 | #if CONFIG_EXT_INTRA |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 2841 | if (av1_is_directional_mode(mbmi->mode, bsize) && |
| 2842 | av1_use_angle_delta(bsize)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 2843 | mode_cost += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 2844 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 2845 | } |
| 2846 | #endif // CONFIG_EXT_INTRA |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 2847 | #if CONFIG_FILTER_INTRA |
| 2848 | if (mbmi->mode == DC_PRED) { |
| 2849 | const aom_prob prob = cpi->common.fc->filter_intra_probs[0]; |
| 2850 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) { |
| 2851 | const int mode = mbmi->filter_intra_mode_info.filter_intra_mode[0]; |
| 2852 | mode_cost += (av1_cost_bit(prob, 1) + |
| 2853 | write_uniform_cost(FILTER_INTRA_MODES, mode)); |
| 2854 | } else { |
| 2855 | mode_cost += av1_cost_bit(prob, 0); |
| 2856 | } |
| 2857 | } |
| 2858 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 2859 | this_rd = |
| 2860 | 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] | 2861 | return this_rd; |
| 2862 | } |
| 2863 | |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 2864 | // Extends 'color_map' array from 'orig_width x orig_height' to 'new_width x |
| 2865 | // new_height'. Extra rows and columns are filled in by copying last valid |
| 2866 | // row/column. |
| 2867 | static void extend_palette_color_map(uint8_t *const color_map, int orig_width, |
| 2868 | int orig_height, int new_width, |
| 2869 | int new_height) { |
| 2870 | int j; |
| 2871 | assert(new_width >= orig_width); |
| 2872 | assert(new_height >= orig_height); |
| 2873 | if (new_width == orig_width && new_height == orig_height) return; |
| 2874 | |
| 2875 | for (j = orig_height - 1; j >= 0; --j) { |
| 2876 | memmove(color_map + j * new_width, color_map + j * orig_width, orig_width); |
| 2877 | // Copy last column to extra columns. |
| 2878 | memset(color_map + j * new_width + orig_width, |
| 2879 | color_map[j * new_width + orig_width - 1], new_width - orig_width); |
| 2880 | } |
| 2881 | // Copy last row to extra rows. |
| 2882 | for (j = orig_height; j < new_height; ++j) { |
| 2883 | memcpy(color_map + j * new_width, color_map + (orig_height - 1) * new_width, |
| 2884 | new_width); |
| 2885 | } |
| 2886 | } |
| 2887 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2888 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 2889 | // Bias toward using colors in the cache. |
| 2890 | // TODO(huisu): Try other schemes to improve compression. |
| 2891 | static void optimize_palette_colors(uint16_t *color_cache, int n_cache, |
| 2892 | int n_colors, int stride, |
| 2893 | float *centroids) { |
| 2894 | if (n_cache <= 0) return; |
| 2895 | for (int i = 0; i < n_colors * stride; i += stride) { |
| 2896 | float min_diff = fabsf(centroids[i] - color_cache[0]); |
| 2897 | int idx = 0; |
| 2898 | for (int j = 1; j < n_cache; ++j) { |
| 2899 | float this_diff = fabsf(centroids[i] - color_cache[j]); |
| 2900 | if (this_diff < min_diff) { |
| 2901 | min_diff = this_diff; |
| 2902 | idx = j; |
| 2903 | } |
| 2904 | } |
| 2905 | if (min_diff < 1.5) centroids[i] = color_cache[idx]; |
| 2906 | } |
| 2907 | } |
| 2908 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 2909 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 2910 | static int rd_pick_palette_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 2911 | BLOCK_SIZE bsize, int palette_ctx, |
| 2912 | int dc_mode_cost, MB_MODE_INFO *best_mbmi, |
| 2913 | uint8_t *best_palette_color_map, |
hui su | 78c611a | 2017-01-13 17:06:04 -0800 | [diff] [blame] | 2914 | int64_t *best_rd, int64_t *best_model_rd, |
| 2915 | int *rate, int *rate_tokenonly, |
| 2916 | int64_t *distortion, int *skippable) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2917 | int rate_overhead = 0; |
| 2918 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2919 | MODE_INFO *const mic = xd->mi[0]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 2920 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 2921 | assert(!is_inter_block(mbmi)); |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 2922 | assert(bsize >= BLOCK_8X8); |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 2923 | int this_rate, colors, n; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2924 | const int src_stride = x->plane[0].src.stride; |
| 2925 | const uint8_t *const src = x->plane[0].src.buf; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 2926 | uint8_t *const color_map = xd->plane[0].color_index_map; |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 2927 | int block_width, block_height, rows, cols; |
| 2928 | av1_get_block_dimensions(bsize, 0, xd, &block_width, &block_height, &rows, |
| 2929 | &cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2930 | |
| 2931 | assert(cpi->common.allow_screen_content_tools); |
| 2932 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2933 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2934 | if (cpi->common.use_highbitdepth) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2935 | colors = av1_count_colors_highbd(src, src_stride, rows, cols, |
| 2936 | cpi->common.bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2937 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2938 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2939 | colors = av1_count_colors(src, src_stride, rows, cols); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2940 | #if CONFIG_FILTER_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 2941 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2942 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2943 | |
| 2944 | if (colors > 1 && colors <= 64) { |
Sarah Parker | 9c0e451 | 2017-08-15 16:23:53 -0700 | [diff] [blame] | 2945 | int r, c, i, k, palette_mode_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2946 | const int max_itr = 50; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2947 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 2948 | float centroids[PALETTE_MAX_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2949 | float lb, ub, val; |
hui su | 78c611a | 2017-01-13 17:06:04 -0800 | [diff] [blame] | 2950 | RD_STATS tokenonly_rd_stats; |
| 2951 | int64_t this_rd, this_model_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2952 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2953 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2954 | uint16_t *src16 = CONVERT_TO_SHORTPTR(src); |
| 2955 | if (cpi->common.use_highbitdepth) |
| 2956 | lb = ub = src16[0]; |
| 2957 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2958 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2959 | lb = ub = src[0]; |
| 2960 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2961 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2962 | if (cpi->common.use_highbitdepth) { |
| 2963 | for (r = 0; r < rows; ++r) { |
| 2964 | for (c = 0; c < cols; ++c) { |
| 2965 | val = src16[r * src_stride + c]; |
| 2966 | data[r * cols + c] = val; |
| 2967 | if (val < lb) |
| 2968 | lb = val; |
| 2969 | else if (val > ub) |
| 2970 | ub = val; |
| 2971 | } |
| 2972 | } |
| 2973 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2974 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2975 | for (r = 0; r < rows; ++r) { |
| 2976 | for (c = 0; c < cols; ++c) { |
| 2977 | val = src[r * src_stride + c]; |
| 2978 | data[r * cols + c] = val; |
| 2979 | if (val < lb) |
| 2980 | lb = val; |
| 2981 | else if (val > ub) |
| 2982 | ub = val; |
| 2983 | } |
| 2984 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2985 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2986 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 2987 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2988 | |
| 2989 | mbmi->mode = DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2990 | #if CONFIG_FILTER_INTRA |
| 2991 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 2992 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2993 | |
| 2994 | if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return 0; |
| 2995 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2996 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2997 | uint16_t color_cache[2 * PALETTE_MAX_SIZE]; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 2998 | const int n_cache = av1_get_palette_cache(xd, 0, color_cache); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 2999 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 3000 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3001 | for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2; |
| 3002 | --n) { |
Urvang Joshi | 773e354 | 2017-05-05 18:09:42 -0700 | [diff] [blame] | 3003 | if (colors == PALETTE_MIN_SIZE) { |
| 3004 | // Special case: These colors automatically become the centroids. |
| 3005 | assert(colors == n); |
| 3006 | assert(colors == 2); |
| 3007 | centroids[0] = lb; |
| 3008 | centroids[1] = ub; |
| 3009 | k = 2; |
| 3010 | } else { |
| 3011 | for (i = 0; i < n; ++i) { |
| 3012 | centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2; |
| 3013 | } |
| 3014 | 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] | 3015 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 3016 | optimize_palette_colors(color_cache, n_cache, n, 1, centroids); |
| 3017 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
Urvang Joshi | 773e354 | 2017-05-05 18:09:42 -0700 | [diff] [blame] | 3018 | k = av1_remove_duplicates(centroids, n); |
| 3019 | if (k < PALETTE_MIN_SIZE) { |
| 3020 | // Too few unique colors to create a palette. And DC_PRED will work |
| 3021 | // well for that case anyway. So skip. |
| 3022 | continue; |
| 3023 | } |
Urvang Joshi | 5fb95f7 | 2017-05-05 17:36:16 -0700 | [diff] [blame] | 3024 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3025 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3026 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3027 | if (cpi->common.use_highbitdepth) |
| 3028 | for (i = 0; i < k; ++i) |
| 3029 | pmi->palette_colors[i] = |
| 3030 | clip_pixel_highbd((int)centroids[i], cpi->common.bit_depth); |
| 3031 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3032 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3033 | for (i = 0; i < k; ++i) |
| 3034 | pmi->palette_colors[i] = clip_pixel((int)centroids[i]); |
| 3035 | pmi->palette_size[0] = k; |
| 3036 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3037 | av1_calc_indices(data, centroids, color_map, rows * cols, k, 1); |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 3038 | extend_palette_color_map(color_map, cols, rows, block_width, |
| 3039 | block_height); |
hui su | 78c611a | 2017-01-13 17:06:04 -0800 | [diff] [blame] | 3040 | palette_mode_cost = |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 3041 | dc_mode_cost + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3042 | x->palette_y_size_cost[bsize - BLOCK_8X8][k - PALETTE_MIN_SIZE] + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3043 | write_uniform_cost(k, color_map[0]) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3044 | av1_cost_bit( |
| 3045 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3046 | 1); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 3047 | palette_mode_cost += av1_palette_color_cost_y(pmi, |
| 3048 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 3049 | color_cache, n_cache, |
| 3050 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 3051 | cpi->common.bit_depth); |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3052 | palette_mode_cost += |
| 3053 | 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] | 3054 | this_model_rd = intra_model_yrd(cpi, x, bsize, palette_mode_cost); |
| 3055 | if (*best_model_rd != INT64_MAX && |
| 3056 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 3057 | continue; |
| 3058 | if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd; |
| 3059 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 3060 | if (tokenonly_rd_stats.rate == INT_MAX) continue; |
| 3061 | this_rate = tokenonly_rd_stats.rate + palette_mode_cost; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3062 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
hui su | 8a63049 | 2017-01-10 18:22:41 -0800 | [diff] [blame] | 3063 | if (!xd->lossless[mbmi->segment_id] && mbmi->sb_type >= BLOCK_8X8) { |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3064 | 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] | 3065 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3066 | if (this_rd < *best_rd) { |
| 3067 | *best_rd = this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3068 | memcpy(best_palette_color_map, color_map, |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 3069 | block_width * block_height * sizeof(color_map[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3070 | *best_mbmi = *mbmi; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3071 | rate_overhead = this_rate - tokenonly_rd_stats.rate; |
hui su | 8a63049 | 2017-01-10 18:22:41 -0800 | [diff] [blame] | 3072 | if (rate) *rate = this_rate; |
| 3073 | if (rate_tokenonly) *rate_tokenonly = tokenonly_rd_stats.rate; |
| 3074 | if (distortion) *distortion = tokenonly_rd_stats.dist; |
| 3075 | if (skippable) *skippable = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | } |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3079 | |
| 3080 | if (best_mbmi->palette_mode_info.palette_size[0] > 0) { |
| 3081 | memcpy(color_map, best_palette_color_map, |
Luc Trudeau | 0401e89 | 2017-08-31 00:37:11 -0400 | [diff] [blame] | 3082 | block_width * block_height * sizeof(best_palette_color_map[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 3083 | } |
| 3084 | *mbmi = *best_mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3085 | return rate_overhead; |
| 3086 | } |
| 3087 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3088 | static int64_t rd_pick_intra_sub_8x8_y_subblock_mode( |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3089 | const AV1_COMP *const cpi, MACROBLOCK *x, int row, int col, |
| 3090 | PREDICTION_MODE *best_mode, const int *bmode_costs, ENTROPY_CONTEXT *a, |
| 3091 | ENTROPY_CONTEXT *l, int *bestrate, int *bestratey, int64_t *bestdistortion, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3092 | 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] | 3093 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3094 | PREDICTION_MODE mode; |
| 3095 | MACROBLOCKD *const xd = &x->e_mbd; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3096 | assert(!is_inter_block(&xd->mi[0]->mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3097 | int64_t best_rd = rd_thresh; |
| 3098 | struct macroblock_plane *p = &x->plane[0]; |
| 3099 | struct macroblockd_plane *pd = &xd->plane[0]; |
| 3100 | const int src_stride = p->src.stride; |
| 3101 | const int dst_stride = pd->dst.stride; |
| 3102 | 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] | 3103 | 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] | 3104 | #if CONFIG_CHROMA_2X2 |
Jingning Han | 276c294 | 2016-12-05 12:37:02 -0800 | [diff] [blame] | 3105 | // TODO(jingning): This is a temporal change. The whole function should be |
| 3106 | // out when cb4x4 is enabled. |
| 3107 | ENTROPY_CONTEXT ta[4], tempa[4]; |
| 3108 | ENTROPY_CONTEXT tl[4], templ[4]; |
| 3109 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3110 | ENTROPY_CONTEXT ta[2], tempa[2]; |
| 3111 | ENTROPY_CONTEXT tl[2], templ[2]; |
Timothy B. Terriberry | fe67ed6 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 3112 | #endif // CONFIG_CHROMA_2X2 |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3113 | |
| 3114 | const int pred_width_in_4x4_blocks = num_4x4_blocks_wide_lookup[bsize]; |
| 3115 | const int pred_height_in_4x4_blocks = num_4x4_blocks_high_lookup[bsize]; |
| 3116 | const int tx_width_unit = tx_size_wide_unit[tx_size]; |
| 3117 | const int tx_height_unit = tx_size_high_unit[tx_size]; |
| 3118 | const int pred_block_width = block_size_wide[bsize]; |
| 3119 | const int pred_block_height = block_size_high[bsize]; |
| 3120 | const int tx_width = tx_size_wide[tx_size]; |
| 3121 | const int tx_height = tx_size_high[tx_size]; |
| 3122 | const int pred_width_in_transform_blocks = pred_block_width / tx_width; |
| 3123 | const int pred_height_in_transform_blocks = pred_block_height / tx_height; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3124 | int idx, idy; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3125 | int best_can_skip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3126 | uint8_t best_dst[8 * 8]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3127 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3128 | uint16_t best_dst16[8 * 8]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3129 | #endif // CONFIG_HIGHBITDEPTH |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3130 | const int is_lossless = xd->lossless[xd->mi[0]->mbmi.segment_id]; |
| 3131 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 3132 | const int sub_bsize = bsize; |
| 3133 | #else |
| 3134 | const int sub_bsize = BLOCK_4X4; |
| 3135 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3136 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3137 | #if CONFIG_PVQ |
| 3138 | od_rollback_buffer pre_buf, post_buf; |
| 3139 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 3140 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3141 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3142 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3143 | assert(bsize < BLOCK_8X8); |
| 3144 | assert(tx_width < 8 || tx_height < 8); |
| 3145 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
hui su | ff5e209 | 2017-03-27 10:07:59 -0700 | [diff] [blame] | 3146 | if (is_lossless) |
| 3147 | assert(tx_width == 4 && tx_height == 4); |
| 3148 | else |
| 3149 | assert(tx_width == pred_block_width && tx_height == pred_block_height); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3150 | #else |
| 3151 | assert(tx_width == 4 && tx_height == 4); |
| 3152 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 3153 | |
| 3154 | memcpy(ta, a, pred_width_in_transform_blocks * sizeof(a[0])); |
| 3155 | memcpy(tl, l, pred_height_in_transform_blocks * sizeof(l[0])); |
| 3156 | |
| 3157 | xd->mi[0]->mbmi.tx_size = tx_size; |
| 3158 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3159 | xd->mi[0]->mbmi.palette_mode_info.palette_size[0] = 0; |
| 3160 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3161 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3162 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3163 | #if CONFIG_PVQ |
| 3164 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 3165 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3166 | for (mode = DC_PRED; mode <= TM_PRED; ++mode) { |
| 3167 | int64_t this_rd; |
| 3168 | int ratey = 0; |
| 3169 | int64_t distortion = 0; |
| 3170 | int rate = bmode_costs[mode]; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3171 | int can_skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3172 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3173 | if (!(cpi->sf.intra_y_mode_mask[txsize_sqr_up_map[tx_size]] & |
| 3174 | (1 << mode))) |
| 3175 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3176 | |
| 3177 | // Only do the oblique modes if the best so far is |
| 3178 | // one of the neighboring directional modes |
| 3179 | if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 3180 | if (conditional_skipintra(mode, *best_mode)) continue; |
| 3181 | } |
| 3182 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3183 | memcpy(tempa, ta, pred_width_in_transform_blocks * sizeof(ta[0])); |
| 3184 | memcpy(templ, tl, pred_height_in_transform_blocks * sizeof(tl[0])); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3185 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3186 | for (idy = 0; idy < pred_height_in_transform_blocks; ++idy) { |
| 3187 | for (idx = 0; idx < pred_width_in_transform_blocks; ++idx) { |
| 3188 | const int block_raster_idx = (row + idy) * 2 + (col + idx); |
| 3189 | const int block = |
| 3190 | av1_raster_order_to_block_index(tx_size, block_raster_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3191 | const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride]; |
| 3192 | uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride]; |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3193 | #if !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3194 | int16_t *const src_diff = av1_raster_block_offset_int16( |
| 3195 | BLOCK_8X8, block_raster_idx, p->src_diff); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3196 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3197 | int skip; |
| 3198 | assert(block < 4); |
| 3199 | assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4, |
| 3200 | idx == 0 && idy == 0)); |
| 3201 | assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4, |
| 3202 | block == 0 || block == 2)); |
| 3203 | xd->mi[0]->bmi[block_raster_idx].as_mode = mode; |
David Barker | 839467f | 2017-01-19 11:06:15 +0000 | [diff] [blame] | 3204 | av1_predict_intra_block( |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 3205 | cm, xd, pd->width, pd->height, txsize_to_bsize[tx_size], mode, |
| 3206 | dst, dst_stride, dst, dst_stride, col + idx, row + idy, 0); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3207 | #if !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3208 | aom_highbd_subtract_block(tx_height, tx_width, src_diff, 8, src, |
| 3209 | src_stride, dst, dst_stride, xd->bd); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3210 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3211 | if (is_lossless) { |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 3212 | TX_TYPE tx_type = |
| 3213 | 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] | 3214 | const SCAN_ORDER *scan_order = |
| 3215 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3216 | const int coeff_ctx = |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3217 | combine_entropy_contexts(tempa[idx], templ[idy]); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3218 | #if !CONFIG_PVQ |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 3219 | 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] | 3220 | tx_size, coeff_ctx, AV1_XFORM_QUANT_FP); |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3221 | ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size, |
| 3222 | scan_order, tempa + idx, templ + idy, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 3223 | cpi->sf.use_fast_coef_costing); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3224 | skip = (p->eobs[block] == 0); |
| 3225 | can_skip &= skip; |
| 3226 | tempa[idx] = !skip; |
| 3227 | templ[idy] = !skip; |
| 3228 | #if CONFIG_EXT_TX |
| 3229 | if (tx_size == TX_8X4) { |
| 3230 | tempa[idx + 1] = tempa[idx]; |
| 3231 | } else if (tx_size == TX_4X8) { |
| 3232 | templ[idy + 1] = templ[idy]; |
| 3233 | } |
| 3234 | #endif // CONFIG_EXT_TX |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3235 | #else |
| 3236 | (void)scan_order; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3237 | |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3238 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
| 3239 | tx_size, coeff_ctx, AV1_XFORM_QUANT_B); |
| 3240 | |
| 3241 | ratey += x->rate; |
| 3242 | skip = x->pvq_skip[0]; |
| 3243 | tempa[idx] = !skip; |
| 3244 | templ[idy] = !skip; |
| 3245 | can_skip &= skip; |
| 3246 | #endif |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3247 | if (RDCOST(x->rdmult, ratey, distortion) >= best_rd) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3248 | goto next_highbd; |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3249 | #if CONFIG_PVQ |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3250 | if (!skip) |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3251 | #endif |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3252 | av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block), |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 3253 | #if CONFIG_LGT |
| 3254 | mode, |
| 3255 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3256 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 3257 | BLOCK_OFFSET(xd->mrc_mask, block), |
| 3258 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3259 | DCT_DCT, tx_size, dst, dst_stride, |
| 3260 | p->eobs[block]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3261 | } else { |
| 3262 | int64_t dist; |
| 3263 | unsigned int tmp; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 3264 | TX_TYPE tx_type = |
| 3265 | 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] | 3266 | const SCAN_ORDER *scan_order = |
| 3267 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3268 | const int coeff_ctx = |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3269 | combine_entropy_contexts(tempa[idx], templ[idy]); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3270 | #if !CONFIG_PVQ |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 3271 | #if DISABLE_TRELLISQ_SEARCH |
| 3272 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
| 3273 | tx_size, coeff_ctx, AV1_XFORM_QUANT_B); |
| 3274 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 3275 | 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] | 3276 | tx_size, coeff_ctx, AV1_XFORM_QUANT_FP); |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3277 | 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] | 3278 | tempa + idx, templ + idy, 1); |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 3279 | #endif // DISABLE_TRELLISQ_SEARCH |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3280 | ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size, |
| 3281 | scan_order, tempa + idx, templ + idy, |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 3282 | cpi->sf.use_fast_coef_costing); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3283 | skip = (p->eobs[block] == 0); |
| 3284 | can_skip &= skip; |
| 3285 | tempa[idx] = !skip; |
| 3286 | templ[idy] = !skip; |
| 3287 | #if CONFIG_EXT_TX |
| 3288 | if (tx_size == TX_8X4) { |
| 3289 | tempa[idx + 1] = tempa[idx]; |
| 3290 | } else if (tx_size == TX_4X8) { |
| 3291 | templ[idy + 1] = templ[idy]; |
| 3292 | } |
| 3293 | #endif // CONFIG_EXT_TX |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3294 | #else |
| 3295 | (void)scan_order; |
| 3296 | |
| 3297 | av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8, |
| 3298 | tx_size, coeff_ctx, AV1_XFORM_QUANT_FP); |
| 3299 | ratey += x->rate; |
| 3300 | skip = x->pvq_skip[0]; |
| 3301 | tempa[idx] = !skip; |
| 3302 | templ[idy] = !skip; |
| 3303 | can_skip &= skip; |
| 3304 | #endif |
| 3305 | #if CONFIG_PVQ |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3306 | if (!skip) |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3307 | #endif |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3308 | av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block), |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 3309 | #if CONFIG_LGT |
| 3310 | mode, |
| 3311 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3312 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 3313 | BLOCK_OFFSET(xd->mrc_mask, block), |
| 3314 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3315 | tx_type, tx_size, dst, dst_stride, |
| 3316 | p->eobs[block]); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3317 | 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] | 3318 | dist = (int64_t)tmp << 4; |
| 3319 | distortion += dist; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3320 | if (RDCOST(x->rdmult, ratey, distortion) >= best_rd) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3321 | goto next_highbd; |
| 3322 | } |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | rate += ratey; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3327 | this_rd = RDCOST(x->rdmult, rate, distortion); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3328 | |
| 3329 | if (this_rd < best_rd) { |
| 3330 | *bestrate = rate; |
| 3331 | *bestratey = ratey; |
| 3332 | *bestdistortion = distortion; |
| 3333 | best_rd = this_rd; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3334 | best_can_skip = can_skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3335 | *best_mode = mode; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3336 | memcpy(a, tempa, pred_width_in_transform_blocks * sizeof(tempa[0])); |
| 3337 | memcpy(l, templ, pred_height_in_transform_blocks * sizeof(templ[0])); |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3338 | #if CONFIG_PVQ |
| 3339 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
| 3340 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3341 | for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3342 | memcpy(best_dst16 + idy * 8, |
| 3343 | CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride), |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3344 | pred_width_in_transform_blocks * 4 * sizeof(uint16_t)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3345 | } |
| 3346 | } |
| 3347 | next_highbd : {} |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3348 | #if CONFIG_PVQ |
| 3349 | od_encode_rollback(&x->daala_enc, &pre_buf); |
| 3350 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | if (best_rd >= rd_thresh) return best_rd; |
| 3354 | |
Thomas Daede | 6ff6af6 | 2017-02-03 16:29:24 -0800 | [diff] [blame] | 3355 | #if CONFIG_PVQ |
| 3356 | od_encode_rollback(&x->daala_enc, &post_buf); |
| 3357 | #endif |
| 3358 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3359 | if (y_skip) *y_skip &= best_can_skip; |
| 3360 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3361 | for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3362 | memcpy(CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride), |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3363 | best_dst16 + idy * 8, |
| 3364 | pred_width_in_transform_blocks * 4 * sizeof(uint16_t)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3365 | } |
| 3366 | |
| 3367 | return best_rd; |
| 3368 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 3369 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3370 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3371 | #if CONFIG_PVQ |
| 3372 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3373 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3374 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3375 | for (mode = DC_PRED; mode <= TM_PRED; ++mode) { |
| 3376 | int64_t this_rd; |
| 3377 | int ratey = 0; |
| 3378 | int64_t distortion = 0; |
| 3379 | int rate = bmode_costs[mode]; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3380 | int can_skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3381 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3382 | if (!(cpi->sf.intra_y_mode_mask[txsize_sqr_up_map[tx_size]] & |
| 3383 | (1 << mode))) { |
| 3384 | continue; |
| 3385 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3386 | |
| 3387 | // Only do the oblique modes if the best so far is |
| 3388 | // one of the neighboring directional modes |
| 3389 | if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 3390 | if (conditional_skipintra(mode, *best_mode)) continue; |
| 3391 | } |
| 3392 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3393 | memcpy(tempa, ta, pred_width_in_transform_blocks * sizeof(ta[0])); |
| 3394 | memcpy(templ, tl, pred_height_in_transform_blocks * sizeof(tl[0])); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3395 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3396 | for (idy = 0; idy < pred_height_in_4x4_blocks; idy += tx_height_unit) { |
| 3397 | for (idx = 0; idx < pred_width_in_4x4_blocks; idx += tx_width_unit) { |
| 3398 | const int block_raster_idx = (row + idy) * 2 + (col + idx); |
| 3399 | 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] | 3400 | const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride]; |
| 3401 | uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3402 | #if !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3403 | int16_t *const src_diff = av1_raster_block_offset_int16( |
| 3404 | BLOCK_8X8, block_raster_idx, p->src_diff); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3405 | #endif // !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3406 | int skip; |
| 3407 | assert(block < 4); |
| 3408 | assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4, |
| 3409 | idx == 0 && idy == 0)); |
| 3410 | assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4, |
| 3411 | block == 0 || block == 2)); |
| 3412 | xd->mi[0]->bmi[block_raster_idx].as_mode = mode; |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 3413 | av1_predict_intra_block(cm, xd, pd->width, pd->height, |
David Barker | 839467f | 2017-01-19 11:06:15 +0000 | [diff] [blame] | 3414 | txsize_to_bsize[tx_size], mode, dst, dst_stride, |
| 3415 | dst, dst_stride, |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3416 | #if CONFIG_CB4X4 |
| 3417 | 2 * (col + idx), 2 * (row + idy), |
| 3418 | #else |
| 3419 | col + idx, row + idy, |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3420 | #endif // CONFIG_CB4X4 |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3421 | 0); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3422 | #if !CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3423 | aom_subtract_block(tx_height, tx_width, src_diff, 8, src, src_stride, |
| 3424 | dst, dst_stride); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3425 | #endif // !CONFIG_PVQ |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 3426 | TX_TYPE tx_type = |
| 3427 | 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] | 3428 | const SCAN_ORDER *scan_order = |
| 3429 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3430 | const int coeff_ctx = combine_entropy_contexts(tempa[idx], templ[idy]); |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3431 | #if CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3432 | block = 4 * block; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3433 | #endif // CONFIG_CB4X4 |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3434 | #if !CONFIG_PVQ |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 3435 | #if DISABLE_TRELLISQ_SEARCH |
| 3436 | av1_xform_quant(cm, x, 0, block, |
| 3437 | #if CONFIG_CB4X4 |
| 3438 | 2 * (row + idy), 2 * (col + idx), |
| 3439 | #else |
| 3440 | row + idy, col + idx, |
| 3441 | #endif // CONFIG_CB4X4 |
| 3442 | BLOCK_8X8, tx_size, coeff_ctx, AV1_XFORM_QUANT_B); |
| 3443 | #else |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3444 | const AV1_XFORM_QUANT xform_quant = |
| 3445 | is_lossless ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP; |
| 3446 | av1_xform_quant(cm, x, 0, block, |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3447 | #if CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3448 | 2 * (row + idy), 2 * (col + idx), |
Jingning Han | d1097fc | 2016-12-06 10:55:34 -0800 | [diff] [blame] | 3449 | #else |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3450 | row + idy, col + idx, |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3451 | #endif // CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3452 | BLOCK_8X8, tx_size, coeff_ctx, xform_quant); |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3453 | |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3454 | 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] | 3455 | templ + idy, 1); |
Sarah Parker | 345366a | 2017-06-15 12:13:01 -0700 | [diff] [blame] | 3456 | #endif // DISABLE_TRELLISQ_SEARCH |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 3457 | ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size, scan_order, |
| 3458 | tempa + idx, templ + idy, |
| 3459 | cpi->sf.use_fast_coef_costing); |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3460 | skip = (p->eobs[block] == 0); |
| 3461 | can_skip &= skip; |
| 3462 | tempa[idx] = !skip; |
| 3463 | templ[idy] = !skip; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3464 | #if CONFIG_EXT_TX |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3465 | if (tx_size == TX_8X4) { |
| 3466 | tempa[idx + 1] = tempa[idx]; |
| 3467 | } else if (tx_size == TX_4X8) { |
| 3468 | templ[idy + 1] = templ[idy]; |
| 3469 | } |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3470 | #endif // CONFIG_EXT_TX |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3471 | #else |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3472 | (void)scan_order; |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3473 | |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3474 | av1_xform_quant(cm, x, 0, block, |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3475 | #if CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3476 | 2 * (row + idy), 2 * (col + idx), |
Yushin Cho | 900243b | 2017-01-03 11:02:38 -0800 | [diff] [blame] | 3477 | #else |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3478 | row + idy, col + idx, |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3479 | #endif // CONFIG_CB4X4 |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3480 | BLOCK_8X8, tx_size, coeff_ctx, AV1_XFORM_QUANT_FP); |
| 3481 | |
| 3482 | ratey += x->rate; |
| 3483 | skip = x->pvq_skip[0]; |
| 3484 | tempa[idx] = !skip; |
| 3485 | templ[idy] = !skip; |
| 3486 | can_skip &= skip; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3487 | #endif // !CONFIG_PVQ |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3488 | |
| 3489 | if (!is_lossless) { // To use the pixel domain distortion, we need to |
| 3490 | // calculate inverse txfm *before* calculating RD |
| 3491 | // cost. Compared to calculating the distortion in |
| 3492 | // the frequency domain, the overhead of encoding |
| 3493 | // effort is low. |
Yushin Cho | b27a17f | 2016-12-23 14:33:02 -0800 | [diff] [blame] | 3494 | #if CONFIG_PVQ |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3495 | if (!skip) |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3496 | #endif // CONFIG_PVQ |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3497 | av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block), |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 3498 | #if CONFIG_LGT |
| 3499 | mode, |
| 3500 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3501 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 3502 | BLOCK_OFFSET(xd->mrc_mask, block), |
| 3503 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Yushin Cho | 08195cc | 2017-04-05 16:37:01 -0700 | [diff] [blame] | 3504 | tx_type, tx_size, dst, dst_stride, |
| 3505 | p->eobs[block]); |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3506 | unsigned int tmp; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3507 | 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] | 3508 | const int64_t dist = (int64_t)tmp << 4; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3509 | distortion += dist; |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3512 | if (RDCOST(x->rdmult, ratey, distortion) >= best_rd) goto next; |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3513 | |
| 3514 | if (is_lossless) { // Calculate inverse txfm *after* RD cost. |
| 3515 | #if CONFIG_PVQ |
| 3516 | if (!skip) |
| 3517 | #endif // CONFIG_PVQ |
| 3518 | av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block), |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 3519 | #if CONFIG_LGT |
| 3520 | mode, |
| 3521 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 3522 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 3523 | BLOCK_OFFSET(xd->mrc_mask, block), |
| 3524 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Urvang Joshi | 1473173 | 2017-04-27 18:40:49 -0700 | [diff] [blame] | 3525 | DCT_DCT, tx_size, dst, dst_stride, |
| 3526 | p->eobs[block]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | rate += ratey; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3532 | this_rd = RDCOST(x->rdmult, rate, distortion); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3533 | |
| 3534 | if (this_rd < best_rd) { |
| 3535 | *bestrate = rate; |
| 3536 | *bestratey = ratey; |
| 3537 | *bestdistortion = distortion; |
| 3538 | best_rd = this_rd; |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3539 | best_can_skip = can_skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3540 | *best_mode = mode; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3541 | memcpy(a, tempa, pred_width_in_transform_blocks * sizeof(tempa[0])); |
| 3542 | memcpy(l, templ, pred_height_in_transform_blocks * sizeof(templ[0])); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3543 | #if CONFIG_PVQ |
| 3544 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3545 | #endif // CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3546 | for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3547 | memcpy(best_dst + idy * 8, dst_init + idy * dst_stride, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3548 | pred_width_in_transform_blocks * 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3549 | } |
| 3550 | next : {} |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3551 | #if CONFIG_PVQ |
| 3552 | od_encode_rollback(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3553 | #endif // CONFIG_PVQ |
| 3554 | } // mode decision loop |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3555 | |
| 3556 | if (best_rd >= rd_thresh) return best_rd; |
| 3557 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3558 | #if CONFIG_PVQ |
| 3559 | od_encode_rollback(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3560 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3561 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3562 | if (y_skip) *y_skip &= best_can_skip; |
| 3563 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3564 | for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3565 | memcpy(dst_init + idy * dst_stride, best_dst + idy * 8, |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3566 | pred_width_in_transform_blocks * 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3567 | |
| 3568 | return best_rd; |
| 3569 | } |
| 3570 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3571 | static int64_t rd_pick_intra_sub_8x8_y_mode(const AV1_COMP *const cpi, |
| 3572 | MACROBLOCK *mb, int *rate, |
| 3573 | int *rate_y, int64_t *distortion, |
| 3574 | int *y_skip, int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3575 | const MACROBLOCKD *const xd = &mb->e_mbd; |
| 3576 | MODE_INFO *const mic = xd->mi[0]; |
| 3577 | const MODE_INFO *above_mi = xd->above_mi; |
| 3578 | const MODE_INFO *left_mi = xd->left_mi; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3579 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3580 | assert(!is_inter_block(mbmi)); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3581 | const BLOCK_SIZE bsize = mbmi->sb_type; |
| 3582 | const int pred_width_in_4x4_blocks = num_4x4_blocks_wide_lookup[bsize]; |
| 3583 | 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] | 3584 | int idx, idy; |
| 3585 | int cost = 0; |
| 3586 | int64_t total_distortion = 0; |
| 3587 | int tot_rate_y = 0; |
| 3588 | int64_t total_rd = 0; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3589 | const int *bmode_costs = mb->mbmode_cost[0]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3590 | const int is_lossless = xd->lossless[mbmi->segment_id]; |
| 3591 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 3592 | const TX_SIZE tx_size = is_lossless ? TX_4X4 : max_txsize_rect_lookup[bsize]; |
| 3593 | #else |
| 3594 | const TX_SIZE tx_size = TX_4X4; |
| 3595 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3596 | |
| 3597 | #if CONFIG_EXT_INTRA |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3598 | #if CONFIG_INTRA_INTERP |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3599 | mbmi->intra_filter = INTRA_FILTER_LINEAR; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3600 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3601 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3602 | #if CONFIG_FILTER_INTRA |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3603 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3604 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3605 | |
| 3606 | // TODO(any): Add search of the tx_type to improve rd performance at the |
| 3607 | // expense of speed. |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3608 | mbmi->tx_type = DCT_DCT; |
| 3609 | mbmi->tx_size = tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3610 | |
Debargha Mukherjee | 096ae4c | 2016-09-07 10:08:13 -0700 | [diff] [blame] | 3611 | if (y_skip) *y_skip = 1; |
| 3612 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3613 | // Pick modes for each prediction sub-block (of size 4x4, 4x8, or 8x4) in this |
| 3614 | // 8x8 coding block. |
| 3615 | for (idy = 0; idy < 2; idy += pred_height_in_4x4_blocks) { |
| 3616 | for (idx = 0; idx < 2; idx += pred_width_in_4x4_blocks) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3617 | PREDICTION_MODE best_mode = DC_PRED; |
| 3618 | int r = INT_MAX, ry = INT_MAX; |
| 3619 | int64_t d = INT64_MAX, this_rd = INT64_MAX; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3620 | int j; |
| 3621 | const int pred_block_idx = idy * 2 + idx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3622 | if (cpi->common.frame_type == KEY_FRAME) { |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3623 | const PREDICTION_MODE A = |
| 3624 | av1_above_block_mode(mic, above_mi, pred_block_idx); |
| 3625 | const PREDICTION_MODE L = |
| 3626 | av1_left_block_mode(mic, left_mi, pred_block_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3627 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3628 | bmode_costs = mb->y_mode_costs[A][L]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3629 | } |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3630 | this_rd = rd_pick_intra_sub_8x8_y_subblock_mode( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3631 | cpi, mb, idy, idx, &best_mode, bmode_costs, |
| 3632 | 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] | 3633 | &ry, &d, bsize, tx_size, y_skip, best_rd - total_rd); |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 3634 | #if CONFIG_DIST_8X8 |
| 3635 | if (!cpi->oxcf.using_dist_8x8) |
| 3636 | #endif |
| 3637 | if (this_rd >= best_rd - total_rd) return INT64_MAX; |
| 3638 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3639 | total_rd += this_rd; |
| 3640 | cost += r; |
| 3641 | total_distortion += d; |
| 3642 | tot_rate_y += ry; |
| 3643 | |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3644 | mic->bmi[pred_block_idx].as_mode = best_mode; |
| 3645 | for (j = 1; j < pred_height_in_4x4_blocks; ++j) |
| 3646 | mic->bmi[pred_block_idx + j * 2].as_mode = best_mode; |
| 3647 | for (j = 1; j < pred_width_in_4x4_blocks; ++j) |
| 3648 | mic->bmi[pred_block_idx + j].as_mode = best_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3649 | |
| 3650 | if (total_rd >= best_rd) return INT64_MAX; |
| 3651 | } |
| 3652 | } |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3653 | mbmi->mode = mic->bmi[3].as_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3654 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3655 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 3656 | if (cpi->oxcf.using_dist_8x8) { |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 3657 | const struct macroblock_plane *p = &mb->plane[0]; |
| 3658 | const struct macroblockd_plane *pd = &xd->plane[0]; |
| 3659 | const int src_stride = p->src.stride; |
| 3660 | const int dst_stride = pd->dst.stride; |
| 3661 | uint8_t *src = p->src.buf; |
| 3662 | uint8_t *dst = pd->dst.buf; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 3663 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 3664 | // Daala-defined distortion computed for the block of 8x8 pixels |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 3665 | 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] | 3666 | BLOCK_8X8, 8, 8, 8, 8, mb->qindex) |
| 3667 | << 4; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 3668 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 3669 | #endif // CONFIG_DIST_8X8 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3670 | // Add in the cost of the transform type |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3671 | if (!is_lossless) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3672 | int rate_tx_type = 0; |
| 3673 | #if CONFIG_EXT_TX |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 3674 | if (get_ext_tx_types(tx_size, bsize, 0, cpi->common.reduced_tx_set_used) > |
| 3675 | 1) { |
| 3676 | const int eset = |
| 3677 | 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] | 3678 | rate_tx_type = mb->intra_tx_type_costs[eset][txsize_sqr_map[tx_size]] |
| 3679 | [mbmi->mode][mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3680 | } |
| 3681 | #else |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 3682 | rate_tx_type = |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3683 | mb->intra_tx_type_costs[txsize_sqr_map[tx_size]] |
| 3684 | [intra_mode_to_tx_type_context[mbmi->mode]] |
| 3685 | [mbmi->tx_type]; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 3686 | #endif // CONFIG_EXT_TX |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3687 | assert(mbmi->tx_size == tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3688 | cost += rate_tx_type; |
| 3689 | tot_rate_y += rate_tx_type; |
| 3690 | } |
| 3691 | |
| 3692 | *rate = cost; |
| 3693 | *rate_y = tot_rate_y; |
| 3694 | *distortion = total_distortion; |
| 3695 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3696 | return RDCOST(mb->rdmult, cost, total_distortion); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3699 | #if CONFIG_FILTER_INTRA |
| 3700 | // Return 1 if an filter intra mode is selected; return 0 otherwise. |
| 3701 | static int rd_pick_filter_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 3702 | int *rate, int *rate_tokenonly, |
| 3703 | int64_t *distortion, int *skippable, |
| 3704 | BLOCK_SIZE bsize, int mode_cost, |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 3705 | int64_t *best_rd, int64_t *best_model_rd, |
| 3706 | uint16_t skip_mask) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3707 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3708 | MODE_INFO *const mic = xd->mi[0]; |
| 3709 | MB_MODE_INFO *mbmi = &mic->mbmi; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3710 | int filter_intra_selected_flag = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3711 | FILTER_INTRA_MODE mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3712 | TX_SIZE best_tx_size = TX_4X4; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3713 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3714 | TX_TYPE best_tx_type; |
| 3715 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3716 | av1_zero(filter_intra_mode_info); |
| 3717 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3718 | mbmi->mode = DC_PRED; |
| 3719 | mbmi->palette_mode_info.palette_size[0] = 0; |
| 3720 | |
| 3721 | for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) { |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 3722 | int this_rate; |
| 3723 | int64_t this_rd, this_model_rd; |
| 3724 | RD_STATS tokenonly_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3725 | if (skip_mask & (1 << mode)) continue; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3726 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = mode; |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 3727 | this_model_rd = intra_model_yrd(cpi, x, bsize, mode_cost); |
| 3728 | if (*best_model_rd != INT64_MAX && |
| 3729 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 3730 | continue; |
| 3731 | 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] | 3732 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 3733 | if (tokenonly_rd_stats.rate == INT_MAX) continue; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3734 | this_rate = tokenonly_rd_stats.rate + |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3735 | av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 1) + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3736 | write_uniform_cost(FILTER_INTRA_MODES, mode) + mode_cost; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3737 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3738 | |
| 3739 | if (this_rd < *best_rd) { |
| 3740 | *best_rd = this_rd; |
| 3741 | best_tx_size = mic->mbmi.tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3742 | filter_intra_mode_info = mbmi->filter_intra_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3743 | best_tx_type = mic->mbmi.tx_type; |
| 3744 | *rate = this_rate; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3745 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 3746 | *distortion = tokenonly_rd_stats.dist; |
| 3747 | *skippable = tokenonly_rd_stats.skip; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3748 | filter_intra_selected_flag = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3749 | } |
| 3750 | } |
| 3751 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3752 | if (filter_intra_selected_flag) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3753 | mbmi->mode = DC_PRED; |
| 3754 | mbmi->tx_size = best_tx_size; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3755 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = |
| 3756 | filter_intra_mode_info.use_filter_intra_mode[0]; |
| 3757 | mbmi->filter_intra_mode_info.filter_intra_mode[0] = |
| 3758 | filter_intra_mode_info.filter_intra_mode[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3759 | mbmi->tx_type = best_tx_type; |
| 3760 | return 1; |
| 3761 | } else { |
| 3762 | return 0; |
| 3763 | } |
| 3764 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3765 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3766 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 3767 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3768 | // Run RD calculation with given luma intra prediction angle., and return |
| 3769 | // the RD cost. Update the best mode info. if the RD cost is the best so far. |
| 3770 | static int64_t calc_rd_given_intra_angle( |
| 3771 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mode_cost, |
| 3772 | int64_t best_rd_in, int8_t angle_delta, int max_angle_delta, int *rate, |
| 3773 | RD_STATS *rd_stats, int *best_angle_delta, TX_SIZE *best_tx_size, |
| 3774 | TX_TYPE *best_tx_type, |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3775 | #if CONFIG_INTRA_INTERP |
| 3776 | INTRA_FILTER *best_filter, |
| 3777 | #endif // CONFIG_INTRA_INTERP |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3778 | int64_t *best_rd, int64_t *best_model_rd) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 3779 | int this_rate; |
| 3780 | RD_STATS tokenonly_rd_stats; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3781 | int64_t this_rd, this_model_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3782 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3783 | assert(!is_inter_block(mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3784 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3785 | mbmi->angle_delta[0] = angle_delta; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3786 | this_model_rd = intra_model_yrd(cpi, x, bsize, mode_cost); |
| 3787 | if (*best_model_rd != INT64_MAX && |
| 3788 | this_model_rd > *best_model_rd + (*best_model_rd >> 1)) |
| 3789 | return INT64_MAX; |
| 3790 | 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] | 3791 | super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in); |
| 3792 | if (tokenonly_rd_stats.rate == INT_MAX) return INT64_MAX; |
| 3793 | |
| 3794 | this_rate = tokenonly_rd_stats.rate + mode_cost + |
| 3795 | write_uniform_cost(2 * max_angle_delta + 1, |
| 3796 | mbmi->angle_delta[0] + max_angle_delta); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 3797 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3798 | |
| 3799 | if (this_rd < *best_rd) { |
| 3800 | *best_rd = this_rd; |
| 3801 | *best_angle_delta = mbmi->angle_delta[0]; |
| 3802 | *best_tx_size = mbmi->tx_size; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3803 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3804 | *best_filter = mbmi->intra_filter; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3805 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3806 | *best_tx_type = mbmi->tx_type; |
| 3807 | *rate = this_rate; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3808 | rd_stats->rate = tokenonly_rd_stats.rate; |
| 3809 | rd_stats->dist = tokenonly_rd_stats.dist; |
| 3810 | rd_stats->skip = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3811 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3812 | return this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3813 | } |
| 3814 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3815 | // With given luma directional intra prediction mode, pick the best angle delta |
| 3816 | // Return the RD cost corresponding to the best angle delta. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 3817 | 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] | 3818 | int *rate, RD_STATS *rd_stats, |
| 3819 | BLOCK_SIZE bsize, int mode_cost, |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3820 | int64_t best_rd, |
| 3821 | int64_t *best_model_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3822 | MACROBLOCKD *const xd = &x->e_mbd; |
| 3823 | MODE_INFO *const mic = xd->mi[0]; |
| 3824 | MB_MODE_INFO *mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 3825 | assert(!is_inter_block(mbmi)); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3826 | int i, angle_delta, best_angle_delta = 0; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3827 | int first_try = 1; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3828 | #if CONFIG_INTRA_INTERP |
| 3829 | int p_angle; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3830 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3831 | INTRA_FILTER filter, best_filter = INTRA_FILTER_LINEAR; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3832 | #endif // CONFIG_INTRA_INTERP |
Debargha Mukherjee | df0e0d7 | 2017-04-27 15:16:53 -0700 | [diff] [blame] | 3833 | 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] | 3834 | TX_SIZE best_tx_size = mic->mbmi.tx_size; |
| 3835 | TX_TYPE best_tx_type = mbmi->tx_type; |
| 3836 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3837 | 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] | 3838 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3839 | for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3840 | #if CONFIG_INTRA_INTERP |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3841 | for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) { |
| 3842 | if (FILTER_FAST_SEARCH && filter != INTRA_FILTER_LINEAR) continue; |
| 3843 | mic->mbmi.intra_filter = filter; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3844 | #endif // CONFIG_INTRA_INTERP |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3845 | for (i = 0; i < 2; ++i) { |
| 3846 | best_rd_in = (best_rd == INT64_MAX) |
| 3847 | ? INT64_MAX |
| 3848 | : (best_rd + (best_rd >> (first_try ? 3 : 5))); |
| 3849 | this_rd = calc_rd_given_intra_angle( |
| 3850 | cpi, x, bsize, |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3851 | #if CONFIG_INTRA_INTERP |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3852 | mode_cost + x->intra_filter_cost[intra_filter_ctx][filter], |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3853 | #else |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3854 | mode_cost, |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3855 | #endif // CONFIG_INTRA_INTERP |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3856 | best_rd_in, (1 - 2 * i) * angle_delta, MAX_ANGLE_DELTA, rate, |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3857 | rd_stats, &best_angle_delta, &best_tx_size, &best_tx_type, |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3858 | #if CONFIG_INTRA_INTERP |
| 3859 | &best_filter, |
| 3860 | #endif // CONFIG_INTRA_INTERP |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3861 | &best_rd, best_model_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3862 | rd_cost[2 * angle_delta + i] = this_rd; |
| 3863 | if (first_try && this_rd == INT64_MAX) return best_rd; |
| 3864 | first_try = 0; |
| 3865 | if (angle_delta == 0) { |
| 3866 | rd_cost[1] = this_rd; |
| 3867 | break; |
| 3868 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3869 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3870 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3871 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3872 | #endif // CONFIG_INTRA_INTERP |
| 3873 | } |
| 3874 | |
| 3875 | assert(best_rd != INT64_MAX); |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3876 | for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3877 | int64_t rd_thresh; |
| 3878 | #if CONFIG_INTRA_INTERP |
| 3879 | for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) { |
| 3880 | if (FILTER_FAST_SEARCH && filter != INTRA_FILTER_LINEAR) continue; |
| 3881 | mic->mbmi.intra_filter = filter; |
| 3882 | #endif // CONFIG_INTRA_INTERP |
| 3883 | for (i = 0; i < 2; ++i) { |
| 3884 | int skip_search = 0; |
| 3885 | rd_thresh = best_rd + (best_rd >> 5); |
| 3886 | if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh && |
| 3887 | rd_cost[2 * (angle_delta - 1) + i] > rd_thresh) |
| 3888 | skip_search = 1; |
| 3889 | if (!skip_search) { |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 3890 | calc_rd_given_intra_angle( |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3891 | cpi, x, bsize, |
| 3892 | #if CONFIG_INTRA_INTERP |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3893 | mode_cost + x->intra_filter_cost[intra_filter_ctx][filter], |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3894 | #else |
| 3895 | mode_cost, |
| 3896 | #endif // CONFIG_INTRA_INTERP |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3897 | best_rd, (1 - 2 * i) * angle_delta, MAX_ANGLE_DELTA, rate, |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3898 | rd_stats, &best_angle_delta, &best_tx_size, &best_tx_type, |
| 3899 | #if CONFIG_INTRA_INTERP |
| 3900 | &best_filter, |
| 3901 | #endif // CONFIG_INTRA_INTERP |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3902 | &best_rd, best_model_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3903 | } |
| 3904 | } |
| 3905 | #if CONFIG_INTRA_INTERP |
| 3906 | } |
| 3907 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3908 | } |
| 3909 | |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3910 | #if CONFIG_INTRA_INTERP |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3911 | if (FILTER_FAST_SEARCH && rd_stats->rate < INT_MAX) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 3912 | 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] | 3913 | if (av1_is_intra_filter_switchable(p_angle)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3914 | for (filter = INTRA_FILTER_LINEAR + 1; filter < INTRA_FILTERS; ++filter) { |
| 3915 | mic->mbmi.intra_filter = filter; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 3916 | this_rd = calc_rd_given_intra_angle( |
| 3917 | cpi, x, bsize, |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 3918 | mode_cost + x->intra_filter_cost[intra_filter_ctx][filter], best_rd, |
| 3919 | best_angle_delta, MAX_ANGLE_DELTA, rate, rd_stats, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3920 | &best_angle_delta, &best_tx_size, &best_tx_type, &best_filter, |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 3921 | &best_rd, best_model_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | } |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3925 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3926 | |
| 3927 | mbmi->tx_size = best_tx_size; |
| 3928 | mbmi->angle_delta[0] = best_angle_delta; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3929 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3930 | mic->mbmi.intra_filter = best_filter; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 3931 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3932 | mbmi->tx_type = best_tx_type; |
| 3933 | return best_rd; |
| 3934 | } |
| 3935 | |
| 3936 | // Indices are sign, integer, and fractional part of the gradient value |
| 3937 | static const uint8_t gradient_to_angle_bin[2][7][16] = { |
| 3938 | { |
| 3939 | { 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0 }, |
| 3940 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, |
| 3941 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 3942 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 3943 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
| 3944 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 3945 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 3946 | }, |
| 3947 | { |
| 3948 | { 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4 }, |
| 3949 | { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3 }, |
| 3950 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 3951 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 3952 | { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, |
| 3953 | { 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 3954 | { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 3955 | }, |
| 3956 | }; |
| 3957 | |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 3958 | /* clang-format off */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3959 | static const uint8_t mode_to_angle_bin[INTRA_MODES] = { |
| 3960 | 0, 2, 6, 0, 4, 3, 5, 7, 1, 0, |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3961 | 0, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3962 | }; |
Yue Chen | 56e226e | 2017-05-02 16:21:40 -0700 | [diff] [blame] | 3963 | /* clang-format on */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3964 | |
| 3965 | 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] | 3966 | int cols, BLOCK_SIZE bsize, |
| 3967 | uint8_t *directional_mode_skip_mask) { |
| 3968 | memset(directional_mode_skip_mask, 0, |
| 3969 | INTRA_MODES * sizeof(*directional_mode_skip_mask)); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 3970 | // Check if angle_delta is used |
| 3971 | if (!av1_use_angle_delta(bsize)) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3972 | uint64_t hist[DIRECTIONAL_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3973 | memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0])); |
| 3974 | src += src_stride; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3975 | int r, c, dx, dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3976 | for (r = 1; r < rows; ++r) { |
| 3977 | for (c = 1; c < cols; ++c) { |
| 3978 | dx = src[c] - src[c - 1]; |
| 3979 | dy = src[c] - src[c - src_stride]; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3980 | int index; |
| 3981 | const int temp = dx * dx + dy * dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3982 | if (dy == 0) { |
| 3983 | index = 2; |
| 3984 | } else { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3985 | const int sn = (dx > 0) ^ (dy > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3986 | dx = abs(dx); |
| 3987 | dy = abs(dy); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3988 | const int remd = (dx % dy) * 16 / dy; |
| 3989 | const int quot = dx / dy; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3990 | index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3991 | } |
| 3992 | hist[index] += temp; |
| 3993 | } |
| 3994 | src += src_stride; |
| 3995 | } |
| 3996 | |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 3997 | int i; |
| 3998 | uint64_t hist_sum = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3999 | for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i]; |
| 4000 | for (i = 0; i < INTRA_MODES; ++i) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4001 | if (av1_is_directional_mode(i, bsize)) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4002 | const uint8_t angle_bin = mode_to_angle_bin[i]; |
| 4003 | uint64_t score = 2 * hist[angle_bin]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4004 | int weight = 2; |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4005 | if (angle_bin > 0) { |
| 4006 | score += hist[angle_bin - 1]; |
| 4007 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4008 | } |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4009 | if (angle_bin < DIRECTIONAL_MODES - 1) { |
| 4010 | score += hist[angle_bin + 1]; |
| 4011 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4012 | } |
| 4013 | if (score * ANGLE_SKIP_THRESH < hist_sum * weight) |
| 4014 | directional_mode_skip_mask[i] = 1; |
| 4015 | } |
| 4016 | } |
| 4017 | } |
| 4018 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4019 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4020 | static void highbd_angle_estimation(const uint8_t *src8, int src_stride, |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4021 | int rows, int cols, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4022 | uint8_t *directional_mode_skip_mask) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4023 | memset(directional_mode_skip_mask, 0, |
| 4024 | INTRA_MODES * sizeof(*directional_mode_skip_mask)); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 4025 | // Check if angle_delta is used |
| 4026 | if (!av1_use_angle_delta(bsize)) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4027 | uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4028 | uint64_t hist[DIRECTIONAL_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4029 | memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0])); |
| 4030 | src += src_stride; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4031 | int r, c, dx, dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4032 | for (r = 1; r < rows; ++r) { |
| 4033 | for (c = 1; c < cols; ++c) { |
| 4034 | dx = src[c] - src[c - 1]; |
| 4035 | dy = src[c] - src[c - src_stride]; |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4036 | int index; |
| 4037 | const int temp = dx * dx + dy * dy; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4038 | if (dy == 0) { |
| 4039 | index = 2; |
| 4040 | } else { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4041 | const int sn = (dx > 0) ^ (dy > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4042 | dx = abs(dx); |
| 4043 | dy = abs(dy); |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4044 | const int remd = (dx % dy) * 16 / dy; |
| 4045 | const int quot = dx / dy; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4046 | index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4047 | } |
| 4048 | hist[index] += temp; |
| 4049 | } |
| 4050 | src += src_stride; |
| 4051 | } |
| 4052 | |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4053 | int i; |
| 4054 | uint64_t hist_sum = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4055 | for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i]; |
| 4056 | for (i = 0; i < INTRA_MODES; ++i) { |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4057 | if (av1_is_directional_mode(i, bsize)) { |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4058 | const uint8_t angle_bin = mode_to_angle_bin[i]; |
| 4059 | uint64_t score = 2 * hist[angle_bin]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4060 | int weight = 2; |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4061 | if (angle_bin > 0) { |
| 4062 | score += hist[angle_bin - 1]; |
| 4063 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4064 | } |
Urvang Joshi | da70e7b | 2016-10-19 11:48:54 -0700 | [diff] [blame] | 4065 | if (angle_bin < DIRECTIONAL_MODES - 1) { |
| 4066 | score += hist[angle_bin + 1]; |
| 4067 | ++weight; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4068 | } |
| 4069 | if (score * ANGLE_SKIP_THRESH < hist_sum * weight) |
| 4070 | directional_mode_skip_mask[i] = 1; |
| 4071 | } |
| 4072 | } |
| 4073 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4074 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4075 | #endif // CONFIG_EXT_INTRA |
| 4076 | |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 4077 | // Returns true if palette can be used for this block. |
| 4078 | static int can_use_palette(const AV1_COMP *const cpi, |
| 4079 | const MB_MODE_INFO *const mbmi) { |
| 4080 | return cpi->common.allow_screen_content_tools && mbmi->sb_type >= BLOCK_8X8 && |
| 4081 | mbmi->sb_type <= BLOCK_LARGEST; |
| 4082 | } |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 4083 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4084 | // This function is used only for intra_only frames |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4085 | static int64_t rd_pick_intra_sby_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 4086 | int *rate, int *rate_tokenonly, |
| 4087 | int64_t *distortion, int *skippable, |
| 4088 | BLOCK_SIZE bsize, int64_t best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4089 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4090 | MODE_INFO *const mic = xd->mi[0]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4091 | MB_MODE_INFO *const mbmi = &mic->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 4092 | assert(!is_inter_block(mbmi)); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4093 | MB_MODE_INFO best_mbmi = *mbmi; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 4094 | int64_t best_model_rd = INT64_MAX; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4095 | #if CONFIG_EXT_INTRA |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 4096 | const int rows = block_size_high[bsize]; |
| 4097 | const int cols = block_size_wide[bsize]; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4098 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4099 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4100 | #endif // CONFIG_INTRA_INTERP |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4101 | int is_directional_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4102 | uint8_t directional_mode_skip_mask[INTRA_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4103 | const int src_stride = x->plane[0].src.stride; |
| 4104 | const uint8_t *src = x->plane[0].src.buf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4105 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4106 | #if CONFIG_FILTER_INTRA |
| 4107 | int beat_best_rd = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4108 | uint16_t filter_intra_mode_skip_mask = (1 << FILTER_INTRA_MODES) - 1; |
| 4109 | #endif // CONFIG_FILTER_INTRA |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 4110 | const int *bmode_costs; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4111 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4112 | uint8_t *best_palette_color_map = |
| 4113 | cpi->common.allow_screen_content_tools |
| 4114 | ? x->palette_buffer->best_palette_color_map |
| 4115 | : NULL; |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4116 | int palette_y_mode_ctx = 0; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 4117 | const int try_palette = can_use_palette(cpi, mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4118 | const MODE_INFO *above_mi = xd->above_mi; |
| 4119 | const MODE_INFO *left_mi = xd->left_mi; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4120 | const PREDICTION_MODE A = av1_above_block_mode(mic, above_mi, 0); |
| 4121 | const PREDICTION_MODE L = av1_left_block_mode(mic, left_mi, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4122 | const PREDICTION_MODE FINAL_MODE_SEARCH = TM_PRED + 1; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4123 | #if CONFIG_PVQ |
| 4124 | od_rollback_buffer pre_buf, post_buf; |
| 4125 | |
| 4126 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
| 4127 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4128 | #endif // CONFIG_PVQ |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 4129 | bmode_costs = x->y_mode_costs[A][L]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4130 | |
| 4131 | #if CONFIG_EXT_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4132 | mbmi->angle_delta[0] = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4133 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4134 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4135 | highbd_angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4136 | directional_mode_skip_mask); |
| 4137 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4138 | #endif // CONFIG_HIGHBITDEPTH |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 4139 | angle_estimation(src, src_stride, rows, cols, bsize, |
| 4140 | directional_mode_skip_mask); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4141 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4142 | #if CONFIG_FILTER_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4143 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4144 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4145 | pmi->palette_size[0] = 0; |
| 4146 | if (above_mi) |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4147 | palette_y_mode_ctx += |
| 4148 | (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4149 | if (left_mi) |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4150 | 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] | 4151 | |
| 4152 | if (cpi->sf.tx_type_search.fast_intra_tx_type_search) |
| 4153 | x->use_default_intra_tx_type = 1; |
| 4154 | else |
| 4155 | x->use_default_intra_tx_type = 0; |
| 4156 | |
| 4157 | /* Y Search for intra prediction mode */ |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 4158 | 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] | 4159 | RD_STATS this_rd_stats; |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 4160 | int this_rate, this_rate_tokenonly, s; |
| 4161 | int64_t this_distortion, this_rd, this_model_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4162 | if (mode_idx == FINAL_MODE_SEARCH) { |
| 4163 | if (x->use_default_intra_tx_type == 0) break; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4164 | mbmi->mode = best_mbmi.mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4165 | x->use_default_intra_tx_type = 0; |
| 4166 | } else { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 4167 | assert(mode_idx < INTRA_MODES); |
| 4168 | mbmi->mode = intra_rd_search_mode_order[mode_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4169 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4170 | #if CONFIG_PVQ |
| 4171 | od_encode_rollback(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4172 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4173 | #if CONFIG_EXT_INTRA |
hui su | 308a639 | 2017-01-12 14:49:57 -0800 | [diff] [blame] | 4174 | mbmi->angle_delta[0] = 0; |
| 4175 | #endif // CONFIG_EXT_INTRA |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 4176 | 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] | 4177 | if (best_model_rd != INT64_MAX && |
| 4178 | this_model_rd > best_model_rd + (best_model_rd >> 1)) |
| 4179 | continue; |
| 4180 | if (this_model_rd < best_model_rd) best_model_rd = this_model_rd; |
| 4181 | #if CONFIG_EXT_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4182 | is_directional_mode = av1_is_directional_mode(mbmi->mode, bsize); |
| 4183 | if (is_directional_mode && directional_mode_skip_mask[mbmi->mode]) continue; |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 4184 | if (is_directional_mode && av1_use_angle_delta(bsize)) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4185 | this_rd_stats.rate = INT_MAX; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 4186 | rd_pick_intra_angle_sby(cpi, x, &this_rate, &this_rd_stats, bsize, |
| 4187 | bmode_costs[mbmi->mode], best_rd, &best_model_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4188 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 4189 | super_block_yrd(cpi, x, &this_rd_stats, bsize, best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4190 | } |
| 4191 | #else |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 4192 | super_block_yrd(cpi, x, &this_rd_stats, bsize, best_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 4193 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 4194 | this_rate_tokenonly = this_rd_stats.rate; |
| 4195 | this_distortion = this_rd_stats.dist; |
| 4196 | s = this_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4197 | |
| 4198 | if (this_rate_tokenonly == INT_MAX) continue; |
| 4199 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4200 | this_rate = this_rate_tokenonly + bmode_costs[mbmi->mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4201 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4202 | if (!xd->lossless[mbmi->segment_id] && mbmi->sb_type >= BLOCK_8X8) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4203 | // super_block_yrd above includes the cost of the tx_size in the |
| 4204 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 4205 | // (prediction granularity), so we account for it in the full rate, |
| 4206 | // not the tokenonly rate. |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 4207 | this_rate_tokenonly -= tx_size_cost(cpi, x, bsize, mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4208 | } |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 4209 | if (try_palette && mbmi->mode == DC_PRED) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4210 | this_rate += |
| 4211 | av1_cost_bit(av1_default_palette_y_mode_prob[bsize - BLOCK_8X8] |
| 4212 | [palette_y_mode_ctx], |
| 4213 | 0); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 4214 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4215 | #if CONFIG_FILTER_INTRA |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4216 | if (mbmi->mode == DC_PRED) |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4217 | this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 0); |
| 4218 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4219 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4220 | if (is_directional_mode) { |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4221 | #if CONFIG_INTRA_INTERP |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 4222 | const int p_angle = |
| 4223 | mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4224 | if (av1_is_intra_filter_switchable(p_angle)) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 4225 | this_rate += x->intra_filter_cost[intra_filter_ctx][mbmi->intra_filter]; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 4226 | #endif // CONFIG_INTRA_INTERP |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 4227 | if (av1_use_angle_delta(bsize)) { |
| 4228 | this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 4229 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
| 4230 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4231 | } |
| 4232 | #endif // CONFIG_EXT_INTRA |
Hui Su | 8dc0092 | 2017-09-14 16:15:55 -0700 | [diff] [blame] | 4233 | #if CONFIG_INTRABC |
| 4234 | if (bsize >= BLOCK_8X8 && cpi->common.allow_screen_content_tools) |
| 4235 | this_rate += x->intrabc_cost[0]; |
| 4236 | #endif // CONFIG_INTRABC |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4237 | this_rd = RDCOST(x->rdmult, this_rate, this_distortion); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4238 | #if CONFIG_FILTER_INTRA |
Debargha Mukherjee | 1ae9f2c | 2016-10-04 14:30:16 -0700 | [diff] [blame] | 4239 | if (best_rd == INT64_MAX || this_rd - best_rd < (best_rd >> 4)) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4240 | filter_intra_mode_skip_mask ^= (1 << mbmi->mode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4241 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4242 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4243 | |
| 4244 | if (this_rd < best_rd) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4245 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4246 | best_rd = this_rd; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4247 | #if CONFIG_FILTER_INTRA |
| 4248 | beat_best_rd = 1; |
| 4249 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4250 | *rate = this_rate; |
| 4251 | *rate_tokenonly = this_rate_tokenonly; |
| 4252 | *distortion = this_distortion; |
| 4253 | *skippable = s; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4254 | #if CONFIG_PVQ |
| 4255 | od_encode_checkpoint(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4256 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4260 | #if CONFIG_PVQ |
| 4261 | od_encode_rollback(&x->daala_enc, &post_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4262 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4263 | |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 4264 | if (try_palette) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 4265 | rd_pick_palette_intra_sby(cpi, x, bsize, palette_y_mode_ctx, |
| 4266 | bmode_costs[DC_PRED], &best_mbmi, |
| 4267 | best_palette_color_map, &best_rd, &best_model_rd, |
| 4268 | rate, rate_tokenonly, distortion, skippable); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4269 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4270 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4271 | #if CONFIG_FILTER_INTRA |
| 4272 | if (beat_best_rd) { |
| 4273 | if (rd_pick_filter_intra_sby(cpi, x, rate, rate_tokenonly, distortion, |
| 4274 | skippable, bsize, bmode_costs[DC_PRED], |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 4275 | &best_rd, &best_model_rd, |
| 4276 | filter_intra_mode_skip_mask)) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4277 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4278 | } |
| 4279 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 4280 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4281 | |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 4282 | *mbmi = best_mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4283 | return best_rd; |
| 4284 | } |
| 4285 | |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4286 | // Return value 0: early termination triggered, no valid rd cost available; |
| 4287 | // 1: rd cost values are valid. |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 4288 | static int super_block_uvrd(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 4289 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
| 4290 | int64_t ref_best_rd) { |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4291 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4292 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
hui su | 0c6244b | 2017-07-12 17:11:43 -0700 | [diff] [blame] | 4293 | 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] | 4294 | int plane; |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4295 | int is_cost_valid = 1; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 4296 | av1_init_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4297 | |
| 4298 | if (ref_best_rd < 0) is_cost_valid = 0; |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 4299 | |
Jingning Han | 31b6a4f | 2017-02-23 11:05:53 -0800 | [diff] [blame] | 4300 | #if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 4301 | if (x->skip_chroma_rd) return is_cost_valid; |
Jingning Han | 2d2dac2 | 2017-04-11 09:41:10 -0700 | [diff] [blame] | 4302 | |
| 4303 | bsize = scale_chroma_bsize(bsize, xd->plane[1].subsampling_x, |
| 4304 | xd->plane[1].subsampling_y); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4305 | #endif // CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 4306 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4307 | #if !CONFIG_PVQ |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4308 | if (is_inter_block(mbmi) && is_cost_valid) { |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4309 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) |
| 4310 | av1_subtract_plane(x, bsize, plane); |
| 4311 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4312 | #endif // !CONFIG_PVQ |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4313 | |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 4314 | if (is_cost_valid) { |
| 4315 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) { |
Angie Chiang | 7c2b7f2 | 2016-11-07 16:00:00 -0800 | [diff] [blame] | 4316 | RD_STATS pn_rd_stats; |
| 4317 | txfm_rd_in_plane(x, cpi, &pn_rd_stats, ref_best_rd, plane, bsize, |
| 4318 | uv_tx_size, cpi->sf.use_fast_coef_costing); |
| 4319 | if (pn_rd_stats.rate == INT_MAX) { |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 4320 | is_cost_valid = 0; |
| 4321 | break; |
| 4322 | } |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 4323 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4324 | if (RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) > ref_best_rd && |
| 4325 | RDCOST(x->rdmult, 0, rd_stats->sse) > ref_best_rd) { |
Yushin Cho | 09de28b | 2016-06-21 14:51:23 -0700 | [diff] [blame] | 4326 | is_cost_valid = 0; |
| 4327 | break; |
| 4328 | } |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4329 | } |
| 4330 | } |
| 4331 | |
| 4332 | if (!is_cost_valid) { |
| 4333 | // reset cost value |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 4334 | av1_invalid_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | return is_cost_valid; |
| 4338 | } |
| 4339 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4340 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4341 | void av1_tx_block_rd_b(const AV1_COMP *cpi, MACROBLOCK *x, TX_SIZE tx_size, |
| 4342 | int blk_row, int blk_col, int plane, int block, |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 4343 | int plane_bsize, const ENTROPY_CONTEXT *a, |
| 4344 | const ENTROPY_CONTEXT *l, RD_STATS *rd_stats) { |
Angie Chiang | 22ba751 | 2016-10-20 17:10:33 -0700 | [diff] [blame] | 4345 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4346 | MACROBLOCKD *xd = &x->e_mbd; |
| 4347 | const struct macroblock_plane *const p = &x->plane[plane]; |
| 4348 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Jingning Han | 243b66b | 2017-06-23 12:11:47 -0700 | [diff] [blame] | 4349 | |
| 4350 | #if CONFIG_TXK_SEL |
| 4351 | av1_search_txk_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize, |
| 4352 | tx_size, a, l, 0, rd_stats); |
| 4353 | return; |
| 4354 | #endif |
| 4355 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4356 | int64_t tmp; |
| 4357 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 4358 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 4359 | uint8_t *mrc_mask = BLOCK_OFFSET(xd->mrc_mask, block); |
| 4360 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Luc Trudeau | 005feb6 | 2017-02-22 13:34:01 -0500 | [diff] [blame] | 4361 | PLANE_TYPE plane_type = get_plane_type(plane); |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 4362 | TX_TYPE tx_type = |
| 4363 | 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] | 4364 | const SCAN_ORDER *const scan_order = |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 4365 | get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4366 | BLOCK_SIZE txm_bsize = txsize_to_bsize[tx_size]; |
Jingning Han | 9fdc422 | 2016-10-27 21:32:19 -0700 | [diff] [blame] | 4367 | int bh = block_size_high[txm_bsize]; |
| 4368 | int bw = block_size_wide[txm_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4369 | int src_stride = p->src.stride; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 4370 | uint8_t *src = |
| 4371 | &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]]; |
| 4372 | uint8_t *dst = |
| 4373 | &pd->dst |
| 4374 | .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] | 4375 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4376 | DECLARE_ALIGNED(16, uint16_t, rec_buffer16[MAX_TX_SQUARE]); |
| 4377 | uint8_t *rec_buffer; |
| 4378 | #else |
| 4379 | DECLARE_ALIGNED(16, uint8_t, rec_buffer[MAX_TX_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4380 | #endif // CONFIG_HIGHBITDEPTH |
Timothy B. Terriberry | d62e2a3 | 2017-06-10 16:04:21 -0700 | [diff] [blame] | 4381 | const int diff_stride = block_size_wide[plane_bsize]; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 4382 | const int16_t *diff = |
| 4383 | &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] | 4384 | int txb_coeff_cost; |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 4385 | |
| 4386 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4387 | |
Angie Chiang | 77368af | 2017-03-23 16:22:07 -0700 | [diff] [blame] | 4388 | int coeff_ctx = get_entropy_context(tx_size, a, l); |
| 4389 | |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4390 | tmp = pixel_diff_dist(x, plane, diff, diff_stride, blk_row, blk_col, |
| 4391 | plane_bsize, txm_bsize); |
| 4392 | |
| 4393 | #if CONFIG_HIGHBITDEPTH |
| 4394 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 4395 | tmp = ROUND_POWER_OF_TWO(tmp, (xd->bd - 8) * 2); |
| 4396 | #endif // CONFIG_HIGHBITDEPTH |
| 4397 | rd_stats->sse += tmp << 4; |
| 4398 | |
| 4399 | if (rd_stats->invalid_rate) { |
| 4400 | rd_stats->dist += tmp << 4; |
| 4401 | rd_stats->rate += rd_stats->zero_rate; |
| 4402 | rd_stats->skip = 1; |
| 4403 | return; |
| 4404 | } |
| 4405 | |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 4406 | // TODO(any): Use av1_dist_block to compute distortion |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4407 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4408 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 4409 | rec_buffer = CONVERT_TO_BYTEPTR(rec_buffer16); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4410 | 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] | 4411 | 0, NULL, 0, bw, bh, xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4412 | } else { |
| 4413 | rec_buffer = (uint8_t *)rec_buffer16; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4414 | 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] | 4415 | NULL, 0, bw, bh); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4416 | } |
| 4417 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4418 | 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] | 4419 | 0, bw, bh); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 4420 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4421 | |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4422 | #if DISABLE_TRELLISQ_SEARCH |
| 4423 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 4424 | coeff_ctx, AV1_XFORM_QUANT_B); |
| 4425 | |
| 4426 | #else |
| 4427 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 4428 | coeff_ctx, AV1_XFORM_QUANT_FP); |
| 4429 | |
| 4430 | const int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2; |
| 4431 | tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
| 4432 | const int buffer_length = tx_size_2d[tx_size]; |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4433 | int64_t tmp_dist, tmp_sse; |
Yunqing Wang | 24d2d5d | 2017-09-20 09:45:13 -0700 | [diff] [blame] | 4434 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 4435 | int disable_early_skip = |
| 4436 | x->using_dist_8x8 && plane == 0 && plane_bsize >= BLOCK_8X8 && |
| 4437 | (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4) && |
| 4438 | x->tune_metric != AOM_TUNE_PSNR; |
Yunqing Wang | 24d2d5d | 2017-09-20 09:45:13 -0700 | [diff] [blame] | 4439 | #endif // CONFIG_DIST_8X8 |
| 4440 | |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4441 | #if CONFIG_HIGHBITDEPTH |
| 4442 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 4443 | tmp_dist = |
| 4444 | av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp_sse, xd->bd); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4445 | else |
| 4446 | #endif |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 4447 | tmp_dist = av1_block_error(coeff, dqcoeff, buffer_length, &tmp_sse); |
| 4448 | |
| 4449 | tmp_dist = RIGHT_SIGNED_SHIFT(tmp_dist, shift); |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4450 | |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4451 | #if CONFIG_MRC_TX |
| 4452 | if (tx_type == MRC_DCT && !xd->mi[0]->mbmi.valid_mrc_mask) { |
| 4453 | av1_invalid_rd_stats(rd_stats); |
| 4454 | return; |
| 4455 | } |
| 4456 | #endif // CONFIG_MRC_TX |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 4457 | if ( |
| 4458 | #if CONFIG_DIST_8X8 |
Yushin Cho | c00769a | 2017-09-14 14:44:30 -0700 | [diff] [blame] | 4459 | disable_early_skip || |
Yushin Cho | a4817a6 | 2017-07-27 13:09:43 -0700 | [diff] [blame] | 4460 | #endif |
| 4461 | RDCOST(x->rdmult, 0, tmp_dist) < rd_stats->ref_rdcost) { |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4462 | 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] | 4463 | a, l, 1); |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4464 | } else { |
| 4465 | rd_stats->rate += rd_stats->zero_rate; |
Debargha Mukherjee | b02d2f3 | 2017-10-03 11:06:40 -0700 | [diff] [blame] | 4466 | rd_stats->dist += RIGHT_SIGNED_SHIFT(tmp_sse, shift); |
| 4467 | rd_stats->sse += RIGHT_SIGNED_SHIFT(tmp_sse, shift); |
Jingning Han | 1a7f0a8 | 2017-07-27 09:48:05 -0700 | [diff] [blame] | 4468 | rd_stats->skip = 1; |
| 4469 | rd_stats->invalid_rate = 1; |
| 4470 | return; |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4471 | } |
| 4472 | #endif // DISABLE_TRELLISQ_SEARCH |
| 4473 | |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 4474 | const int eob = p->eobs[block]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4475 | |
Lester Lu | 918fe69 | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 4476 | av1_inverse_transform_block(xd, dqcoeff, |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 4477 | #if CONFIG_LGT |
Lester Lu | 918fe69 | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 4478 | xd->mi[0]->mbmi.mode, |
Lester Lu | 708c1ec | 2017-06-14 14:54:49 -0700 | [diff] [blame] | 4479 | #endif |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 4480 | #if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
| 4481 | mrc_mask, |
| 4482 | #endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK |
Lester Lu | 918fe69 | 2017-08-17 14:39:29 -0700 | [diff] [blame] | 4483 | tx_type, tx_size, rec_buffer, MAX_TX_SIZE, eob); |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 4484 | if (eob > 0) { |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4485 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4486 | if (x->using_dist_8x8 && plane == 0 && (bw < 8 && bh < 8)) { |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4487 | // Save sub8x8 luma decoded pixels |
| 4488 | // since 8x8 luma decoded pixels are not available for daala-dist |
| 4489 | // after recursive split of BLOCK_8x8 is done. |
| 4490 | const int pred_stride = block_size_wide[plane_bsize]; |
| 4491 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 4492 | << tx_size_wide_log2[0]; |
| 4493 | int16_t *decoded = &pd->pred[pred_idx]; |
| 4494 | int i, j; |
| 4495 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4496 | #if CONFIG_HIGHBITDEPTH |
| 4497 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 4498 | for (j = 0; j < bh; j++) |
| 4499 | for (i = 0; i < bw; i++) |
| 4500 | decoded[j * pred_stride + i] = |
| 4501 | CONVERT_TO_SHORTPTR(rec_buffer)[j * MAX_TX_SIZE + i]; |
| 4502 | } else { |
| 4503 | #endif |
| 4504 | for (j = 0; j < bh; j++) |
| 4505 | for (i = 0; i < bw; i++) |
| 4506 | decoded[j * pred_stride + i] = rec_buffer[j * MAX_TX_SIZE + i]; |
| 4507 | #if CONFIG_HIGHBITDEPTH |
| 4508 | } |
| 4509 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4510 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4511 | #endif // CONFIG_DIST_8X8 |
Yushin Cho | 75b0100 | 2017-06-21 13:43:57 -0700 | [diff] [blame] | 4512 | tmp = pixel_dist(cpi, x, plane, src, src_stride, rec_buffer, MAX_TX_SIZE, |
| 4513 | blk_row, blk_col, plane_bsize, txm_bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4514 | } |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4515 | rd_stats->dist += tmp * 16; |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 4516 | txb_coeff_cost = av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block, |
| 4517 | tx_size, scan_order, a, l, 0); |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 4518 | rd_stats->rate += txb_coeff_cost; |
Angie Chiang | 41fffae | 2017-04-03 10:33:18 -0700 | [diff] [blame] | 4519 | rd_stats->skip &= (eob == 0); |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4520 | |
Angie Chiang | d81fdb4 | 2016-11-03 12:20:58 -0700 | [diff] [blame] | 4521 | #if CONFIG_RD_DEBUG |
Angie Chiang | e94556b | 2016-11-09 10:59:30 -0800 | [diff] [blame] | 4522 | av1_update_txb_coeff_cost(rd_stats, plane, tx_size, blk_row, blk_col, |
| 4523 | txb_coeff_cost); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4524 | #endif // CONFIG_RD_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4525 | } |
| 4526 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4527 | 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] | 4528 | int blk_col, int plane, int block, int block32, |
| 4529 | TX_SIZE tx_size, int depth, BLOCK_SIZE plane_bsize, |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 4530 | ENTROPY_CONTEXT *ta, ENTROPY_CONTEXT *tl, |
| 4531 | TXFM_CONTEXT *tx_above, TXFM_CONTEXT *tx_left, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4532 | RD_STATS *rd_stats, int64_t ref_best_rd, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4533 | int *is_cost_valid, RD_STATS *rd_stats_stack) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4534 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4535 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 4536 | struct macroblock_plane *const p = &x->plane[plane]; |
| 4537 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 4538 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 4539 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 4540 | TX_SIZE(*const inter_tx_size) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4541 | [MAX_MIB_SIZE] = |
| 4542 | (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] | 4543 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 4544 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 4545 | 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] | 4546 | int64_t this_rd = INT64_MAX; |
| 4547 | ENTROPY_CONTEXT *pta = ta + blk_col; |
| 4548 | ENTROPY_CONTEXT *ptl = tl + blk_row; |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4549 | int i; |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 4550 | int ctx = txfm_partition_context(tx_above + blk_col, tx_left + blk_row, |
| 4551 | mbmi->sb_type, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4552 | int64_t sum_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4553 | int tmp_eob = 0; |
| 4554 | int zero_blk_rate; |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 4555 | RD_STATS sum_rd_stats; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4556 | #if CONFIG_TXK_SEL |
| 4557 | TX_TYPE best_tx_type = TX_TYPES; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 4558 | int txk_idx = (blk_row << 4) + blk_col; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4559 | #endif |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4560 | #if CONFIG_RECT_TX_EXT |
| 4561 | TX_SIZE quarter_txsize = quarter_txsize_lookup[mbmi->sb_type]; |
| 4562 | int check_qttx = is_quarter_tx_allowed(xd, mbmi, is_inter_block(mbmi)) && |
| 4563 | tx_size == max_txsize_rect_lookup[mbmi->sb_type] && |
| 4564 | quarter_txsize != tx_size; |
| 4565 | int is_qttx_picked = 0; |
| 4566 | int eobs_qttx[2] = { 0, 0 }; |
| 4567 | int skip_qttx[2] = { 0, 0 }; |
| 4568 | int block_offset_qttx = check_qttx |
| 4569 | ? tx_size_wide_unit[quarter_txsize] * |
| 4570 | tx_size_high_unit[quarter_txsize] |
| 4571 | : 0; |
| 4572 | int blk_row_offset, blk_col_offset; |
| 4573 | int is_wide_qttx = |
| 4574 | tx_size_wide_unit[quarter_txsize] > tx_size_high_unit[quarter_txsize]; |
| 4575 | blk_row_offset = is_wide_qttx ? tx_size_high_unit[quarter_txsize] : 0; |
| 4576 | blk_col_offset = is_wide_qttx ? 0 : tx_size_wide_unit[quarter_txsize]; |
| 4577 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4578 | |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4579 | av1_init_rd_stats(&sum_rd_stats); |
Jingning Han | fe45b21 | 2016-11-22 10:30:23 -0800 | [diff] [blame] | 4580 | |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 4581 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4582 | |
| 4583 | if (ref_best_rd < 0) { |
| 4584 | *is_cost_valid = 0; |
| 4585 | return; |
| 4586 | } |
| 4587 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4588 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4589 | |
| 4590 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 4591 | |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4592 | #if CONFIG_LV_MAP |
| 4593 | TX_SIZE txs_ctx = get_txsize_context(tx_size); |
| 4594 | TXB_CTX txb_ctx; |
| 4595 | get_txb_ctx(plane_bsize, tx_size, plane, pta, ptl, &txb_ctx); |
Jingning Han | e981491 | 2017-08-31 16:38:59 -0700 | [diff] [blame] | 4596 | |
| 4597 | #if LV_MAP_PROB |
| 4598 | zero_blk_rate = x->coeff_costs[txs_ctx][get_plane_type(plane)] |
| 4599 | .txb_skip_cost[txb_ctx.txb_skip_ctx][1]; |
| 4600 | #else |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4601 | zero_blk_rate = |
| 4602 | 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] | 4603 | #endif // LV_MAP_PROB |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4604 | #else |
Urvang Joshi | 9752a2e | 2017-10-02 17:32:27 -0700 | [diff] [blame] | 4605 | TX_SIZE tx_size_ctx = txsize_sqr_map[tx_size]; |
Jingning Han | 5a995d7 | 2017-07-02 15:20:54 -0700 | [diff] [blame] | 4606 | int coeff_ctx = get_entropy_context(tx_size, pta, ptl); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 4607 | zero_blk_rate = |
| 4608 | 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] | 4609 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4610 | |
Jingning Han | 3bce754 | 2017-07-25 10:53:57 -0700 | [diff] [blame] | 4611 | rd_stats->ref_rdcost = ref_best_rd; |
| 4612 | rd_stats->zero_rate = zero_blk_rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4613 | if (cpi->common.tx_mode == TX_MODE_SELECT || tx_size == TX_4X4) { |
| 4614 | inter_tx_size[0][0] = tx_size; |
Yushin Cho | c5a1f26 | 2017-10-02 17:17:19 -0700 | [diff] [blame] | 4615 | if (tx_size == TX_32X32 && mbmi->tx_type != DCT_DCT && |
| 4616 | rd_stats_stack[block32].rate != INT_MAX |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4617 | #if CONFIG_MRC_TX |
Yushin Cho | c5a1f26 | 2017-10-02 17:17:19 -0700 | [diff] [blame] | 4618 | && !USE_MRC_INTER |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4619 | #endif // CONFIG_MRC_TX |
Yushin Cho | c5a1f26 | 2017-10-02 17:17:19 -0700 | [diff] [blame] | 4620 | ) { |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4621 | *rd_stats = rd_stats_stack[block32]; |
| 4622 | p->eobs[block] = !rd_stats->skip; |
| 4623 | x->blk_skip[plane][blk_row * bw + blk_col] = rd_stats->skip; |
| 4624 | } else { |
| 4625 | 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] | 4626 | plane_bsize, pta, ptl, rd_stats); |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4627 | if (tx_size == TX_32X32) { |
| 4628 | rd_stats_stack[block32] = *rd_stats; |
| 4629 | } |
| 4630 | } |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4631 | if (rd_stats->rate == INT_MAX) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4632 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4633 | if ((RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) >= |
| 4634 | RDCOST(x->rdmult, zero_blk_rate, rd_stats->sse) || |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4635 | rd_stats->skip == 1) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4636 | !xd->lossless[mbmi->segment_id]) { |
Jingning Han | c7ea761 | 2017-01-11 15:01:30 -0800 | [diff] [blame] | 4637 | #if CONFIG_RD_DEBUG |
| 4638 | av1_update_txb_coeff_cost(rd_stats, plane, tx_size, blk_row, blk_col, |
| 4639 | zero_blk_rate - rd_stats->rate); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 4640 | #endif // CONFIG_RD_DEBUG |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4641 | rd_stats->rate = zero_blk_rate; |
| 4642 | rd_stats->dist = rd_stats->sse; |
| 4643 | rd_stats->skip = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4644 | x->blk_skip[plane][blk_row * bw + blk_col] = 1; |
| 4645 | p->eobs[block] = 0; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 4646 | #if CONFIG_TXK_SEL |
| 4647 | mbmi->txk_type[txk_idx] = DCT_DCT; |
| 4648 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4649 | } else { |
| 4650 | x->blk_skip[plane][blk_row * bw + blk_col] = 0; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4651 | rd_stats->skip = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4652 | } |
| 4653 | |
Jingning Han | 571189c | 2016-10-24 10:38:43 -0700 | [diff] [blame] | 4654 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4655 | rd_stats->rate += |
| 4656 | av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 0); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4657 | #if CONFIG_RECT_TX_EXT |
| 4658 | if (check_qttx) { |
| 4659 | assert(blk_row == 0 && blk_col == 0); |
| 4660 | rd_stats->rate += av1_cost_bit(cpi->common.fc->quarter_tx_size_prob, 0); |
| 4661 | } |
| 4662 | #endif |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4663 | this_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4664 | #if CONFIG_LV_MAP |
| 4665 | tmp_eob = p->txb_entropy_ctx[block]; |
| 4666 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4667 | tmp_eob = p->eobs[block]; |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4668 | #endif |
| 4669 | |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4670 | #if CONFIG_TXK_SEL |
| 4671 | best_tx_type = mbmi->txk_type[txk_idx]; |
| 4672 | #endif |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4673 | |
| 4674 | #if CONFIG_RECT_TX_EXT |
| 4675 | if (check_qttx) { |
| 4676 | assert(blk_row == 0 && blk_col == 0 && block == 0 && plane == 0); |
| 4677 | |
| 4678 | RD_STATS rd_stats_tmp, rd_stats_qttx; |
| 4679 | int64_t rd_qttx; |
| 4680 | |
| 4681 | av1_init_rd_stats(&rd_stats_qttx); |
| 4682 | av1_init_rd_stats(&rd_stats_tmp); |
| 4683 | |
| 4684 | av1_tx_block_rd_b(cpi, x, quarter_txsize, 0, 0, plane, 0, plane_bsize, |
| 4685 | pta, ptl, &rd_stats_qttx); |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4686 | if (rd_stats->rate == INT_MAX) return; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4687 | |
| 4688 | tx_size_ctx = txsize_sqr_map[quarter_txsize]; |
| 4689 | coeff_ctx = get_entropy_context(quarter_txsize, pta, ptl); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 4690 | zero_blk_rate = |
| 4691 | 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] | 4692 | if ((RDCOST(x->rdmult, rd_stats_qttx.rate, rd_stats_qttx.dist) >= |
| 4693 | RDCOST(x->rdmult, zero_blk_rate, rd_stats_qttx.sse) || |
| 4694 | rd_stats_qttx.skip == 1) && |
| 4695 | !xd->lossless[mbmi->segment_id]) { |
| 4696 | #if CONFIG_RD_DEBUG |
| 4697 | av1_update_txb_coeff_cost(&rd_stats_qttx, plane, quarter_txsize, 0, 0, |
| 4698 | zero_blk_rate - rd_stats_qttx.rate); |
| 4699 | #endif // CONFIG_RD_DEBUG |
| 4700 | rd_stats_qttx.rate = zero_blk_rate; |
| 4701 | rd_stats_qttx.dist = rd_stats_qttx.sse; |
| 4702 | rd_stats_qttx.skip = 1; |
| 4703 | x->blk_skip[plane][blk_row * bw + blk_col] = 1; |
| 4704 | skip_qttx[0] = 1; |
| 4705 | p->eobs[block] = 0; |
| 4706 | } else { |
| 4707 | x->blk_skip[plane][blk_row * bw + blk_col] = 0; |
| 4708 | skip_qttx[0] = 0; |
| 4709 | rd_stats->skip = 0; |
| 4710 | } |
| 4711 | |
| 4712 | // Second tx block |
| 4713 | av1_tx_block_rd_b(cpi, x, quarter_txsize, blk_row_offset, blk_col_offset, |
| 4714 | plane, block_offset_qttx, plane_bsize, pta, ptl, |
| 4715 | &rd_stats_tmp); |
| 4716 | |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 4717 | if (rd_stats->rate == INT_MAX) return; |
| 4718 | |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 4719 | #if !CONFIG_PVQ |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4720 | av1_set_txb_context(x, plane, 0, quarter_txsize, pta, ptl); |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 4721 | #endif // !CONFIG_PVQ |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4722 | coeff_ctx = get_entropy_context(quarter_txsize, pta + blk_col_offset, |
| 4723 | ptl + blk_row_offset); |
hui su | c0cf71d | 2017-07-20 16:38:50 -0700 | [diff] [blame] | 4724 | zero_blk_rate = |
| 4725 | 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] | 4726 | if ((RDCOST(x->rdmult, rd_stats_tmp.rate, rd_stats_tmp.dist) >= |
| 4727 | RDCOST(x->rdmult, zero_blk_rate, rd_stats_tmp.sse) || |
| 4728 | rd_stats_tmp.skip == 1) && |
| 4729 | !xd->lossless[mbmi->segment_id]) { |
| 4730 | #if CONFIG_RD_DEBUG |
| 4731 | av1_update_txb_coeff_cost(&rd_stats_tmp, plane, quarter_txsize, 0, 0, |
| 4732 | zero_blk_rate - rd_stats_tmp.rate); |
| 4733 | #endif // CONFIG_RD_DEBUG |
| 4734 | rd_stats_tmp.rate = zero_blk_rate; |
| 4735 | rd_stats_tmp.dist = rd_stats_tmp.sse; |
| 4736 | rd_stats_tmp.skip = 1; |
| 4737 | x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = 1; |
| 4738 | skip_qttx[1] = 1; |
| 4739 | p->eobs[block_offset_qttx] = 0; |
| 4740 | } else { |
| 4741 | x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = 0; |
| 4742 | skip_qttx[1] = 0; |
| 4743 | rd_stats_tmp.skip = 0; |
| 4744 | } |
| 4745 | |
| 4746 | av1_merge_rd_stats(&rd_stats_qttx, &rd_stats_tmp); |
| 4747 | |
| 4748 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) { |
| 4749 | rd_stats_qttx.rate += |
| 4750 | av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 0); |
| 4751 | } |
| 4752 | rd_stats_qttx.rate += |
| 4753 | av1_cost_bit(cpi->common.fc->quarter_tx_size_prob, 1); |
| 4754 | rd_qttx = RDCOST(x->rdmult, rd_stats_qttx.rate, rd_stats_qttx.dist); |
| 4755 | #if CONFIG_LV_MAP |
| 4756 | eobs_qttx[0] = p->txb_entropy_ctx[0]; |
| 4757 | eobs_qttx[1] = p->txb_entropy_ctx[block_offset_qttx]; |
| 4758 | #else |
| 4759 | eobs_qttx[0] = p->eobs[0]; |
| 4760 | eobs_qttx[1] = p->eobs[block_offset_qttx]; |
| 4761 | #endif |
| 4762 | if (rd_qttx < this_rd) { |
| 4763 | is_qttx_picked = 1; |
| 4764 | this_rd = rd_qttx; |
| 4765 | rd_stats->rate = rd_stats_qttx.rate; |
| 4766 | rd_stats->dist = rd_stats_qttx.dist; |
| 4767 | rd_stats->sse = rd_stats_qttx.sse; |
| 4768 | rd_stats->skip = rd_stats_qttx.skip; |
| 4769 | rd_stats->rdcost = rd_stats_qttx.rdcost; |
| 4770 | } |
| 4771 | av1_get_entropy_contexts(plane_bsize, 0, pd, ta, tl); |
| 4772 | } |
| 4773 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4774 | } |
| 4775 | |
Yushin Cho | c5a1f26 | 2017-10-02 17:17:19 -0700 | [diff] [blame] | 4776 | if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 4777 | #if CONFIG_MRC_TX |
Yushin Cho | c5a1f26 | 2017-10-02 17:17:19 -0700 | [diff] [blame] | 4778 | // If the tx type we are trying is MRC_DCT, we cannot partition the |
| 4779 | // transform into anything smaller than TX_32X32 |
| 4780 | && mbmi->tx_type != MRC_DCT |
| 4781 | #endif // CONFIG_MRC_TX |
| 4782 | ) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 4783 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 4784 | const int bsl = tx_size_wide_unit[sub_txs]; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 4785 | 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] | 4786 | RD_STATS this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4787 | int this_cost_valid = 1; |
| 4788 | int64_t tmp_rd = 0; |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4789 | #if CONFIG_DIST_8X8 |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4790 | int sub8x8_eob[4]; |
| 4791 | #endif |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 4792 | sum_rd_stats.rate = |
| 4793 | av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 1); |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 4794 | |
| 4795 | assert(tx_size < TX_SIZES_ALL); |
| 4796 | |
Jingning Han | 16a9df7 | 2017-07-26 15:27:43 -0700 | [diff] [blame] | 4797 | ref_best_rd = AOMMIN(this_rd, ref_best_rd); |
| 4798 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4799 | for (i = 0; i < 4 && this_cost_valid; ++i) { |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 4800 | int offsetr = blk_row + (i >> 1) * bsl; |
| 4801 | int offsetc = blk_col + (i & 0x01) * bsl; |
| 4802 | |
| 4803 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 4804 | |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4805 | select_tx_block(cpi, x, offsetr, offsetc, plane, block, block32, sub_txs, |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 4806 | depth + 1, plane_bsize, ta, tl, tx_above, tx_left, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4807 | &this_rd_stats, ref_best_rd - tmp_rd, &this_cost_valid, |
| 4808 | rd_stats_stack); |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4809 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4810 | if (x->using_dist_8x8 && plane == 0 && tx_size == TX_8X8) { |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4811 | sub8x8_eob[i] = p->eobs[block]; |
| 4812 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4813 | #endif // CONFIG_DIST_8X8 |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4814 | av1_merge_rd_stats(&sum_rd_stats, &this_rd_stats); |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 4815 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 4816 | 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] | 4817 | #if CONFIG_DIST_8X8 |
| 4818 | if (!x->using_dist_8x8) |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4819 | #endif |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4820 | if (this_rd < tmp_rd) break; |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 4821 | block += sub_step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4822 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4823 | #if CONFIG_DIST_8X8 |
Yushin Cho | 5510433 | 2017-08-14 16:15:43 -0700 | [diff] [blame] | 4824 | if (x->using_dist_8x8 && this_cost_valid && plane == 0 && |
| 4825 | tx_size == TX_8X8) { |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4826 | const int src_stride = p->src.stride; |
| 4827 | const int dst_stride = pd->dst.stride; |
| 4828 | |
| 4829 | const uint8_t *src = |
| 4830 | &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]]; |
| 4831 | const uint8_t *dst = |
| 4832 | &pd->dst |
| 4833 | .buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]]; |
| 4834 | |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4835 | int64_t dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4836 | int qindex = x->qindex; |
| 4837 | const int pred_stride = block_size_wide[plane_bsize]; |
| 4838 | const int pred_idx = (blk_row * pred_stride + blk_col) |
| 4839 | << tx_size_wide_log2[0]; |
| 4840 | int16_t *pred = &pd->pred[pred_idx]; |
| 4841 | int j; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4842 | int row, col; |
| 4843 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4844 | #if CONFIG_HIGHBITDEPTH |
| 4845 | uint8_t *pred8; |
| 4846 | DECLARE_ALIGNED(16, uint16_t, pred8_16[8 * 8]); |
| 4847 | #else |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4848 | DECLARE_ALIGNED(16, uint8_t, pred8[8 * 8]); |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4849 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4850 | |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 4851 | 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] | 4852 | BLOCK_8X8, 8, 8, 8, 8, qindex) * |
| 4853 | 16; |
| 4854 | sum_rd_stats.sse = dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4855 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4856 | #if CONFIG_HIGHBITDEPTH |
| 4857 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 4858 | pred8 = CONVERT_TO_BYTEPTR(pred8_16); |
| 4859 | else |
| 4860 | pred8 = (uint8_t *)pred8_16; |
| 4861 | #endif |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4862 | |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4863 | #if CONFIG_HIGHBITDEPTH |
| 4864 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 4865 | for (row = 0; row < 2; ++row) { |
| 4866 | for (col = 0; col < 2; ++col) { |
| 4867 | int idx = row * 2 + col; |
| 4868 | int eob = sub8x8_eob[idx]; |
| 4869 | |
| 4870 | if (eob > 0) { |
| 4871 | for (j = 0; j < 4; j++) |
| 4872 | for (i = 0; i < 4; i++) |
| 4873 | CONVERT_TO_SHORTPTR(pred8) |
| 4874 | [(row * 4 + j) * 8 + 4 * col + i] = |
| 4875 | pred[(row * 4 + j) * pred_stride + 4 * col + i]; |
| 4876 | } else { |
| 4877 | for (j = 0; j < 4; j++) |
| 4878 | for (i = 0; i < 4; i++) |
| 4879 | CONVERT_TO_SHORTPTR(pred8) |
| 4880 | [(row * 4 + j) * 8 + 4 * col + i] = CONVERT_TO_SHORTPTR( |
| 4881 | dst)[(row * 4 + j) * dst_stride + 4 * col + i]; |
| 4882 | } |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4883 | } |
| 4884 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4885 | } else { |
| 4886 | #endif |
| 4887 | for (row = 0; row < 2; ++row) { |
| 4888 | for (col = 0; col < 2; ++col) { |
| 4889 | int idx = row * 2 + col; |
| 4890 | int eob = sub8x8_eob[idx]; |
| 4891 | |
| 4892 | if (eob > 0) { |
| 4893 | for (j = 0; j < 4; j++) |
| 4894 | for (i = 0; i < 4; i++) |
| 4895 | pred8[(row * 4 + j) * 8 + 4 * col + i] = |
Yaowu Xu | 7a47170 | 2017-09-29 08:38:37 -0700 | [diff] [blame] | 4896 | (uint8_t)pred[(row * 4 + j) * pred_stride + 4 * col + i]; |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4897 | } else { |
| 4898 | for (j = 0; j < 4; j++) |
| 4899 | for (i = 0; i < 4; i++) |
| 4900 | pred8[(row * 4 + j) * 8 + 4 * col + i] = |
| 4901 | dst[(row * 4 + j) * dst_stride + 4 * col + i]; |
| 4902 | } |
| 4903 | } |
| 4904 | } |
| 4905 | #if CONFIG_HIGHBITDEPTH |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4906 | } |
Yushin Cho | 8ab875d | 2017-06-23 14:47:21 -0700 | [diff] [blame] | 4907 | #endif // CONFIG_HIGHBITDEPTH |
Yushin Cho | e30a47c | 2017-08-15 13:08:30 -0700 | [diff] [blame] | 4908 | 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] | 4909 | 8, 8, 8, qindex) * |
| 4910 | 16; |
| 4911 | sum_rd_stats.dist = dist_8x8; |
Yushin Cho | 0474912 | 2017-05-25 14:19:07 -0700 | [diff] [blame] | 4912 | tmp_rd = RDCOST(x->rdmult, sum_rd_stats.rate, sum_rd_stats.dist); |
| 4913 | } |
Yushin Cho | b7b60c5 | 2017-07-14 16:18:52 -0700 | [diff] [blame] | 4914 | #endif // CONFIG_DIST_8X8 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4915 | if (this_cost_valid) sum_rd = tmp_rd; |
| 4916 | } |
| 4917 | |
| 4918 | if (this_rd < sum_rd) { |
| 4919 | int idx, idy; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4920 | #if CONFIG_RECT_TX_EXT |
| 4921 | TX_SIZE tx_size_selected = is_qttx_picked ? quarter_txsize : tx_size; |
| 4922 | #else |
| 4923 | TX_SIZE tx_size_selected = tx_size; |
| 4924 | #endif |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4925 | |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4926 | #if CONFIG_RECT_TX_EXT |
| 4927 | if (is_qttx_picked) { |
| 4928 | assert(blk_row == 0 && blk_col == 0 && plane == 0); |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4929 | #if CONFIG_LV_MAP |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4930 | p->txb_entropy_ctx[0] = eobs_qttx[0]; |
| 4931 | p->txb_entropy_ctx[block_offset_qttx] = eobs_qttx[1]; |
| 4932 | #else |
| 4933 | p->eobs[0] = eobs_qttx[0]; |
| 4934 | p->eobs[block_offset_qttx] = eobs_qttx[1]; |
| 4935 | #endif |
| 4936 | } else { |
| 4937 | #endif |
| 4938 | #if CONFIG_LV_MAP |
| 4939 | p->txb_entropy_ctx[block] = tmp_eob; |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4940 | #else |
| 4941 | p->eobs[block] = tmp_eob; |
| 4942 | #endif |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4943 | #if CONFIG_RECT_TX_EXT |
| 4944 | } |
| 4945 | #endif |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4946 | |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 4947 | #if !CONFIG_PVQ |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4948 | av1_set_txb_context(x, plane, block, tx_size_selected, pta, ptl); |
| 4949 | #if CONFIG_RECT_TX_EXT |
| 4950 | if (is_qttx_picked) |
| 4951 | av1_set_txb_context(x, plane, block_offset_qttx, tx_size_selected, |
| 4952 | pta + blk_col_offset, ptl + blk_row_offset); |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 4953 | #endif // CONFIG_RECT_TX_EXT |
| 4954 | #endif // !CONFIG_PVQ |
Jingning Han | 2f42d77 | 2017-07-05 16:28:18 -0700 | [diff] [blame] | 4955 | |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 4956 | txfm_partition_update(tx_above + blk_col, tx_left + blk_row, tx_size, |
| 4957 | tx_size); |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4958 | inter_tx_size[0][0] = tx_size_selected; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 4959 | for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy) |
| 4960 | for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx) |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4961 | inter_tx_size[idy][idx] = tx_size_selected; |
| 4962 | mbmi->tx_size = tx_size_selected; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 4963 | #if CONFIG_TXK_SEL |
| 4964 | mbmi->txk_type[txk_idx] = best_tx_type; |
| 4965 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4966 | if (this_rd == INT64_MAX) *is_cost_valid = 0; |
Yue Chen | d6bdd46 | 2017-07-19 16:05:43 -0700 | [diff] [blame] | 4967 | #if CONFIG_RECT_TX_EXT |
| 4968 | if (is_qttx_picked) { |
| 4969 | x->blk_skip[plane][0] = skip_qttx[0]; |
| 4970 | x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = skip_qttx[1]; |
| 4971 | } else { |
| 4972 | #endif |
| 4973 | x->blk_skip[plane][blk_row * bw + blk_col] = rd_stats->skip; |
| 4974 | #if CONFIG_RECT_TX_EXT |
| 4975 | } |
| 4976 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4977 | } else { |
Angie Chiang | d724617 | 2016-11-03 11:49:15 -0700 | [diff] [blame] | 4978 | *rd_stats = sum_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4979 | if (sum_rd == INT64_MAX) *is_cost_valid = 0; |
| 4980 | } |
| 4981 | } |
| 4982 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 4983 | static void inter_block_yrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 4984 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 4985 | int64_t ref_best_rd, RD_STATS *rd_stats_stack) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4986 | MACROBLOCKD *const xd = &x->e_mbd; |
| 4987 | int is_cost_valid = 1; |
| 4988 | int64_t this_rd = 0; |
| 4989 | |
| 4990 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 4991 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 4992 | av1_init_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4993 | |
| 4994 | if (is_cost_valid) { |
| 4995 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 4996 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 4997 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 4998 | 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] | 4999 | const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize]; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5000 | const int bh = tx_size_high_unit[max_tx_size]; |
| 5001 | const int bw = tx_size_wide_unit[max_tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5002 | int idx, idy; |
| 5003 | int block = 0; |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5004 | int block32 = 0; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5005 | 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] | 5006 | ENTROPY_CONTEXT ctxa[2 * MAX_MIB_SIZE]; |
| 5007 | ENTROPY_CONTEXT ctxl[2 * MAX_MIB_SIZE]; |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 5008 | TXFM_CONTEXT tx_above[MAX_MIB_SIZE * 2]; |
| 5009 | TXFM_CONTEXT tx_left[MAX_MIB_SIZE * 2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5010 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5011 | RD_STATS pn_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5012 | av1_init_rd_stats(&pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5013 | |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5014 | av1_get_entropy_contexts(bsize, 0, pd, ctxa, ctxl); |
Jingning Han | 331662e | 2017-05-30 17:03:32 -0700 | [diff] [blame] | 5015 | memcpy(tx_above, xd->above_txfm_context, sizeof(TXFM_CONTEXT) * mi_width); |
| 5016 | memcpy(tx_left, xd->left_txfm_context, sizeof(TXFM_CONTEXT) * mi_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5017 | |
| 5018 | for (idy = 0; idy < mi_height; idy += bh) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5019 | for (idx = 0; idx < mi_width; idx += bw) { |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5020 | 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] | 5021 | mi_height != mi_width, plane_bsize, ctxa, ctxl, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5022 | tx_above, tx_left, &pn_rd_stats, ref_best_rd - this_rd, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5023 | &is_cost_valid, rd_stats_stack); |
Sarah Parker | de6f072 | 2017-08-07 20:23:46 -0700 | [diff] [blame] | 5024 | if (pn_rd_stats.rate == INT_MAX) { |
| 5025 | av1_invalid_rd_stats(rd_stats); |
| 5026 | return; |
| 5027 | } |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5028 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5029 | this_rd += AOMMIN(RDCOST(x->rdmult, pn_rd_stats.rate, pn_rd_stats.dist), |
| 5030 | RDCOST(x->rdmult, 0, pn_rd_stats.sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5031 | block += step; |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5032 | ++block32; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5037 | this_rd = AOMMIN(RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist), |
| 5038 | RDCOST(x->rdmult, 0, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5039 | if (this_rd > ref_best_rd) is_cost_valid = 0; |
| 5040 | |
| 5041 | if (!is_cost_valid) { |
| 5042 | // reset cost value |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5043 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5044 | } |
| 5045 | } |
| 5046 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5047 | 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] | 5048 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5049 | int64_t ref_best_rd, TX_TYPE tx_type, |
| 5050 | RD_STATS *rd_stats_stack) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5051 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5052 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5053 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5054 | const int is_inter = is_inter_block(mbmi); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5055 | aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
| 5056 | int s0 = av1_cost_bit(skip_prob, 0); |
| 5057 | int s1 = av1_cost_bit(skip_prob, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5058 | int64_t rd; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5059 | int row, col; |
| 5060 | const int max_blocks_high = max_block_high(xd, bsize, 0); |
| 5061 | const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5062 | |
| 5063 | mbmi->tx_type = tx_type; |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5064 | 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] | 5065 | 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] | 5066 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5067 | if (rd_stats->rate == INT_MAX) return INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5068 | |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5069 | for (row = 0; row < max_blocks_high / 2; ++row) |
| 5070 | for (col = 0; col < max_blocks_wide / 2; ++col) |
| 5071 | mbmi->min_tx_size = AOMMIN( |
| 5072 | mbmi->min_tx_size, get_min_tx_size(mbmi->inter_tx_size[row][col])); |
| 5073 | |
Jingning Han | 1643a0a | 2017-07-05 15:48:25 -0700 | [diff] [blame] | 5074 | #if !CONFIG_TXK_SEL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5075 | #if CONFIG_EXT_TX |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 5076 | if (get_ext_tx_types(mbmi->min_tx_size, bsize, is_inter, |
| 5077 | cm->reduced_tx_set_used) > 1 && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5078 | !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 5079 | const int ext_tx_set = get_ext_tx_set(mbmi->min_tx_size, bsize, is_inter, |
| 5080 | cm->reduced_tx_set_used); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5081 | if (is_inter) { |
| 5082 | if (ext_tx_set > 0) |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5083 | rd_stats->rate += |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5084 | x->inter_tx_type_costs[ext_tx_set] |
| 5085 | [txsize_sqr_map[mbmi->min_tx_size]] |
| 5086 | [mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5087 | } else { |
| 5088 | if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5089 | rd_stats->rate += x->intra_tx_type_costs[ext_tx_set][mbmi->min_tx_size] |
| 5090 | [mbmi->mode][mbmi->tx_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5091 | } |
| 5092 | } |
Jingning Han | 1643a0a | 2017-07-05 15:48:25 -0700 | [diff] [blame] | 5093 | #else |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5094 | 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] | 5095 | 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] | 5096 | #endif // CONFIG_EXT_TX |
Jingning Han | 1643a0a | 2017-07-05 15:48:25 -0700 | [diff] [blame] | 5097 | #endif // CONFIG_TXK_SEL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5098 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5099 | if (rd_stats->skip) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5100 | rd = RDCOST(x->rdmult, s1, rd_stats->sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5101 | else |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5102 | rd = RDCOST(x->rdmult, rd_stats->rate + s0, rd_stats->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5103 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5104 | if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] && |
| 5105 | !(rd_stats->skip)) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5106 | rd = AOMMIN(rd, RDCOST(x->rdmult, s1, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5107 | |
| 5108 | return rd; |
| 5109 | } |
| 5110 | |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 5111 | static uint32_t get_block_residue_hash(MACROBLOCK *x, BLOCK_SIZE bsize) { |
| 5112 | const int rows = block_size_high[bsize]; |
| 5113 | const int cols = block_size_wide[bsize]; |
| 5114 | const int diff_stride = cols; |
| 5115 | const struct macroblock_plane *const p = &x->plane[0]; |
| 5116 | const int16_t *diff = &p->src_diff[0]; |
| 5117 | uint8_t hash_data[MAX_SB_SQUARE]; |
| 5118 | for (int r = 0; r < rows; ++r) { |
| 5119 | for (int c = 0; c < cols; ++c) { |
| 5120 | hash_data[cols * r + c] = clip_pixel(diff[c] + 128); |
| 5121 | } |
| 5122 | diff += diff_stride; |
| 5123 | } |
| 5124 | return (av1_get_crc_value(&x->tx_rd_record.crc_calculator, hash_data, |
| 5125 | rows * cols) |
| 5126 | << 7) + |
| 5127 | bsize; |
| 5128 | } |
| 5129 | |
| 5130 | static void save_tx_rd_info(int n4, uint32_t hash, const MACROBLOCK *const x, |
| 5131 | const RD_STATS *const rd_stats, |
| 5132 | TX_RD_INFO *const tx_rd_info) { |
| 5133 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 5134 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5135 | tx_rd_info->hash_value = hash; |
| 5136 | tx_rd_info->tx_type = mbmi->tx_type; |
| 5137 | tx_rd_info->tx_size = mbmi->tx_size; |
| 5138 | #if CONFIG_VAR_TX |
| 5139 | tx_rd_info->min_tx_size = mbmi->min_tx_size; |
| 5140 | memcpy(tx_rd_info->blk_skip, x->blk_skip[0], |
| 5141 | sizeof(tx_rd_info->blk_skip[0]) * n4); |
| 5142 | for (int idy = 0; idy < xd->n8_h; ++idy) |
| 5143 | for (int idx = 0; idx < xd->n8_w; ++idx) |
| 5144 | tx_rd_info->inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 5145 | #endif // CONFIG_VAR_TX |
| 5146 | #if CONFIG_TXK_SEL |
| 5147 | av1_copy(tx_rd_info->txk_type, mbmi->txk_type); |
| 5148 | #endif // CONFIG_TXK_SEL |
| 5149 | tx_rd_info->rd_stats = *rd_stats; |
| 5150 | } |
| 5151 | |
| 5152 | static void fetch_tx_rd_info(int n4, const TX_RD_INFO *const tx_rd_info, |
| 5153 | RD_STATS *const rd_stats, MACROBLOCK *const x) { |
| 5154 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5155 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5156 | mbmi->tx_type = tx_rd_info->tx_type; |
| 5157 | mbmi->tx_size = tx_rd_info->tx_size; |
| 5158 | #if CONFIG_VAR_TX |
| 5159 | mbmi->min_tx_size = tx_rd_info->min_tx_size; |
| 5160 | memcpy(x->blk_skip[0], tx_rd_info->blk_skip, |
| 5161 | sizeof(tx_rd_info->blk_skip[0]) * n4); |
| 5162 | for (int idy = 0; idy < xd->n8_h; ++idy) |
| 5163 | for (int idx = 0; idx < xd->n8_w; ++idx) |
| 5164 | mbmi->inter_tx_size[idy][idx] = tx_rd_info->inter_tx_size[idy][idx]; |
| 5165 | #endif // CONFIG_VAR_TX |
| 5166 | #if CONFIG_TXK_SEL |
| 5167 | av1_copy(mbmi->txk_type, tx_rd_info->txk_type); |
| 5168 | #endif // CONFIG_TXK_SEL |
| 5169 | *rd_stats = tx_rd_info->rd_stats; |
| 5170 | } |
| 5171 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5172 | static void select_tx_type_yrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 5173 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5174 | int64_t ref_best_rd) { |
Jingning Han | 2b0eeb1 | 2017-02-23 15:55:37 -0800 | [diff] [blame] | 5175 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5176 | const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; |
| 5177 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5178 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5179 | int64_t rd = INT64_MAX; |
| 5180 | int64_t best_rd = INT64_MAX; |
| 5181 | TX_TYPE tx_type, best_tx_type = DCT_DCT; |
| 5182 | const int is_inter = is_inter_block(mbmi); |
| 5183 | TX_SIZE best_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE]; |
Debargha Mukherjee | 28d924b | 2016-10-05 00:48:28 -0700 | [diff] [blame] | 5184 | TX_SIZE best_tx = max_txsize_lookup[bsize]; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5185 | TX_SIZE best_min_tx_size = TX_SIZES_ALL; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5186 | uint8_t best_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 5187 | TX_TYPE txk_start = DCT_DCT; |
| 5188 | #if CONFIG_TXK_SEL |
| 5189 | TX_TYPE txk_end = DCT_DCT + 1; |
| 5190 | #else |
| 5191 | TX_TYPE txk_end = TX_TYPES; |
| 5192 | #endif |
Angie Chiang | f1cb075 | 2017-04-10 16:01:20 -0700 | [diff] [blame] | 5193 | const int n4 = bsize_to_num_blk(bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5194 | int idx, idy; |
| 5195 | int prune = 0; |
Jingning Han | 2b0eeb1 | 2017-02-23 15:55:37 -0800 | [diff] [blame] | 5196 | const int count32 = |
| 5197 | 1 << (2 * (cm->mib_size_log2 - mi_width_log2_lookup[BLOCK_32X32])); |
Jingning Han | 89d648b | 2016-11-22 11:22:08 -0800 | [diff] [blame] | 5198 | #if CONFIG_EXT_PARTITION |
Debargha Mukherjee | 2555065 | 2017-10-04 20:22:28 -0700 | [diff] [blame] | 5199 | RD_STATS *rd_stats_stack = aom_malloc(16 * sizeof(*rd_stats_stack)); |
Jingning Han | 89d648b | 2016-11-22 11:22:08 -0800 | [diff] [blame] | 5200 | #else |
Debargha Mukherjee | 2555065 | 2017-10-04 20:22:28 -0700 | [diff] [blame] | 5201 | RD_STATS *rd_stats_stack = aom_malloc(4 * sizeof(*rd_stats_stack)); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5202 | #endif // CONFIG_EXT_PARTITION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5203 | #if CONFIG_EXT_TX |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 5204 | const TxSetType tx_set_type = get_ext_tx_set_type( |
| 5205 | max_tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 5206 | const int ext_tx_set = |
| 5207 | 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] | 5208 | #endif // CONFIG_EXT_TX |
| 5209 | |
| 5210 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE) |
| 5211 | #if CONFIG_EXT_TX |
| 5212 | prune = prune_tx_types(cpi, bsize, x, xd, ext_tx_set); |
| 5213 | #else |
| 5214 | prune = prune_tx_types(cpi, bsize, x, xd, 0); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5215 | #endif // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5216 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5217 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5218 | |
Jingning Han | 89d648b | 2016-11-22 11:22:08 -0800 | [diff] [blame] | 5219 | for (idx = 0; idx < count32; ++idx) |
| 5220 | av1_invalid_rd_stats(&rd_stats_stack[idx]); |
Jingning Han | 63cbf34 | 2016-11-09 15:37:48 -0800 | [diff] [blame] | 5221 | |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 5222 | const uint32_t hash = get_block_residue_hash(x, bsize); |
| 5223 | TX_RD_RECORD *tx_rd_record = &x->tx_rd_record; |
| 5224 | |
| 5225 | if (ref_best_rd != INT64_MAX) { |
| 5226 | for (int i = 0; i < tx_rd_record->num; ++i) { |
| 5227 | const int index = (tx_rd_record->index_start + i) % RD_RECORD_BUFFER_LEN; |
| 5228 | // If there is a match in the tx_rd_record, fetch the RD decision and |
| 5229 | // terminate early. |
| 5230 | if (tx_rd_record->tx_rd_info[index].hash_value == hash) { |
| 5231 | TX_RD_INFO *tx_rd_info = &tx_rd_record->tx_rd_info[index]; |
| 5232 | fetch_tx_rd_info(n4, tx_rd_info, rd_stats, x); |
Debargha Mukherjee | 2555065 | 2017-10-04 20:22:28 -0700 | [diff] [blame] | 5233 | aom_free(rd_stats_stack); |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 5234 | return; |
| 5235 | } |
| 5236 | } |
| 5237 | } |
| 5238 | |
Jingning Han | e3b81bc | 2017-06-23 11:43:52 -0700 | [diff] [blame] | 5239 | for (tx_type = txk_start; tx_type < txk_end; ++tx_type) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5240 | RD_STATS this_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5241 | av1_init_rd_stats(&this_rd_stats); |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 5242 | #if CONFIG_MRC_TX |
| 5243 | // MRC_DCT only implemented for TX_32X32 so only include this tx in |
| 5244 | // the search for TX_32X32 |
Sarah Parker | 2e08d96 | 2017-08-01 19:51:20 -0700 | [diff] [blame] | 5245 | if (tx_type == MRC_DCT && |
| 5246 | (max_tx_size != TX_32X32 || (is_inter && !USE_MRC_INTER) || |
| 5247 | (!is_inter && !USE_MRC_INTRA))) |
| 5248 | continue; |
Sarah Parker | 53f93db | 2017-07-11 17:20:04 -0700 | [diff] [blame] | 5249 | #endif // CONFIG_MRC_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5250 | #if CONFIG_EXT_TX |
Hui Su | ddbcde2 | 2017-09-18 17:22:02 -0700 | [diff] [blame] | 5251 | if (!av1_ext_tx_used[tx_set_type][tx_type]) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5252 | if (is_inter) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5253 | if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) { |
| 5254 | if (!do_tx_type_search(tx_type, prune)) continue; |
| 5255 | } |
| 5256 | } else { |
| 5257 | if (!ALLOW_INTRA_EXT_TX && bsize >= BLOCK_8X8) { |
| 5258 | if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue; |
| 5259 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5260 | } |
| 5261 | #else // CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5262 | if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE && |
| 5263 | !do_tx_type_search(tx_type, prune)) |
| 5264 | continue; |
| 5265 | #endif // CONFIG_EXT_TX |
| 5266 | if (is_inter && x->use_default_inter_tx_type && |
| 5267 | tx_type != get_default_tx_type(0, xd, 0, max_tx_size)) |
| 5268 | continue; |
| 5269 | |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5270 | if (xd->lossless[mbmi->segment_id]) |
| 5271 | if (tx_type != DCT_DCT) continue; |
| 5272 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5273 | 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] | 5274 | tx_type, rd_stats_stack); |
Hui Su | da816a1 | 2017-08-18 14:46:02 -0700 | [diff] [blame] | 5275 | ref_best_rd = AOMMIN(rd, ref_best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5276 | if (rd < best_rd) { |
| 5277 | best_rd = rd; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5278 | *rd_stats = this_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5279 | best_tx_type = mbmi->tx_type; |
| 5280 | best_tx = mbmi->tx_size; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5281 | best_min_tx_size = mbmi->min_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5282 | memcpy(best_blk_skip, x->blk_skip[0], sizeof(best_blk_skip[0]) * n4); |
| 5283 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 5284 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 5285 | best_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 5286 | } |
| 5287 | } |
| 5288 | |
| 5289 | mbmi->tx_type = best_tx_type; |
| 5290 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 5291 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 5292 | mbmi->inter_tx_size[idy][idx] = best_tx_size[idy][idx]; |
| 5293 | mbmi->tx_size = best_tx; |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 5294 | mbmi->min_tx_size = best_min_tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5295 | 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] | 5296 | |
| 5297 | // Save the RD search results into tx_rd_record. |
| 5298 | int index; |
| 5299 | if (tx_rd_record->num < RD_RECORD_BUFFER_LEN) { |
| 5300 | index = |
| 5301 | (tx_rd_record->index_start + tx_rd_record->num) % RD_RECORD_BUFFER_LEN; |
| 5302 | ++tx_rd_record->num; |
| 5303 | } else { |
| 5304 | index = tx_rd_record->index_start; |
| 5305 | tx_rd_record->index_start = |
| 5306 | (tx_rd_record->index_start + 1) % RD_RECORD_BUFFER_LEN; |
| 5307 | } |
| 5308 | save_tx_rd_info(n4, hash, x, rd_stats, &tx_rd_record->tx_rd_info[index]); |
Debargha Mukherjee | 2555065 | 2017-10-04 20:22:28 -0700 | [diff] [blame] | 5309 | aom_free(rd_stats_stack); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5310 | } |
| 5311 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5312 | 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] | 5313 | int blk_col, int plane, int block, TX_SIZE tx_size, |
| 5314 | BLOCK_SIZE plane_bsize, ENTROPY_CONTEXT *above_ctx, |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5315 | ENTROPY_CONTEXT *left_ctx, RD_STATS *rd_stats) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5316 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5317 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5318 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 5319 | BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
| 5320 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 5321 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
| 5322 | TX_SIZE plane_tx_size; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5323 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 5324 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5325 | |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 5326 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5327 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5328 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 5329 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 5330 | plane_tx_size = |
| 5331 | plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0] |
| 5332 | : mbmi->inter_tx_size[tx_row][tx_col]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5333 | |
| 5334 | if (tx_size == plane_tx_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5335 | ENTROPY_CONTEXT *ta = above_ctx + blk_col; |
| 5336 | ENTROPY_CONTEXT *tl = left_ctx + blk_row; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5337 | 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] | 5338 | plane_bsize, ta, tl, rd_stats); |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 5339 | #if !CONFIG_PVQ |
Jingning Han | 328d57b | 2017-07-07 14:40:17 -0700 | [diff] [blame] | 5340 | av1_set_txb_context(x, plane, block, tx_size, ta, tl); |
Tom Finegan | ac87049 | 2017-08-15 16:19:13 -0700 | [diff] [blame] | 5341 | #endif // !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5342 | } else { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5343 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 5344 | const int bsl = tx_size_wide_unit[sub_txs]; |
Jingning Han | 5822404 | 2016-10-27 16:35:32 -0700 | [diff] [blame] | 5345 | 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] | 5346 | int i; |
| 5347 | |
| 5348 | assert(bsl > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5349 | |
| 5350 | for (i = 0; i < 4; ++i) { |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 5351 | int offsetr = blk_row + (i >> 1) * bsl; |
| 5352 | int offsetc = blk_col + (i & 0x01) * bsl; |
| 5353 | |
| 5354 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 5355 | |
| 5356 | tx_block_rd(cpi, x, offsetr, offsetc, plane, block, sub_txs, plane_bsize, |
| 5357 | above_ctx, left_ctx, rd_stats); |
| 5358 | block += step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5359 | } |
| 5360 | } |
| 5361 | } |
| 5362 | |
| 5363 | // Return value 0: early termination triggered, no valid rd cost available; |
| 5364 | // 1: rd cost values are valid. |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5365 | static int inter_block_uvrd(const AV1_COMP *cpi, MACROBLOCK *x, |
| 5366 | RD_STATS *rd_stats, BLOCK_SIZE bsize, |
| 5367 | int64_t ref_best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5368 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5369 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 5370 | int plane; |
| 5371 | int is_cost_valid = 1; |
| 5372 | int64_t this_rd; |
| 5373 | |
| 5374 | if (ref_best_rd < 0) is_cost_valid = 0; |
| 5375 | |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5376 | av1_init_rd_stats(rd_stats); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 5377 | |
Jingning Han | 31b6a4f | 2017-02-23 11:05:53 -0800 | [diff] [blame] | 5378 | #if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 5379 | if (x->skip_chroma_rd) return is_cost_valid; |
Jingning Han | 52d2b40 | 2017-05-19 09:31:17 -0700 | [diff] [blame] | 5380 | bsize = scale_chroma_bsize(mbmi->sb_type, xd->plane[1].subsampling_x, |
| 5381 | xd->plane[1].subsampling_y); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5382 | #endif // CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 5383 | |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 5384 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
| 5385 | if (is_rect_tx(mbmi->tx_size)) { |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5386 | return super_block_uvrd(cpi, x, rd_stats, bsize, ref_best_rd); |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 5387 | } |
| 5388 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 5389 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5390 | if (is_inter_block(mbmi) && is_cost_valid) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5391 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5392 | av1_subtract_plane(x, bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5393 | } |
| 5394 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5395 | for (plane = 1; plane < MAX_MB_PLANE; ++plane) { |
| 5396 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 5397 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5398 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 5399 | 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] | 5400 | const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize]; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5401 | const int bh = tx_size_high_unit[max_tx_size]; |
| 5402 | const int bw = tx_size_wide_unit[max_tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5403 | int idx, idy; |
| 5404 | int block = 0; |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5405 | const int step = bh * bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5406 | ENTROPY_CONTEXT ta[2 * MAX_MIB_SIZE]; |
| 5407 | ENTROPY_CONTEXT tl[2 * MAX_MIB_SIZE]; |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5408 | RD_STATS pn_rd_stats; |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5409 | av1_init_rd_stats(&pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5410 | |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 5411 | av1_get_entropy_contexts(bsize, 0, pd, ta, tl); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5412 | |
| 5413 | for (idy = 0; idy < mi_height; idy += bh) { |
Jingning Han | 18482fe | 2016-11-02 17:01:58 -0700 | [diff] [blame] | 5414 | for (idx = 0; idx < mi_width; idx += bw) { |
| 5415 | 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] | 5416 | ta, tl, &pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5417 | block += step; |
| 5418 | } |
| 5419 | } |
| 5420 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 5421 | if (pn_rd_stats.rate == INT_MAX) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5422 | is_cost_valid = 0; |
| 5423 | break; |
| 5424 | } |
| 5425 | |
Angie Chiang | 628d7c9 | 2016-11-03 16:24:56 -0700 | [diff] [blame] | 5426 | av1_merge_rd_stats(rd_stats, &pn_rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5427 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5428 | this_rd = AOMMIN(RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist), |
| 5429 | RDCOST(x->rdmult, 0, rd_stats->sse)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5430 | |
| 5431 | if (this_rd > ref_best_rd) { |
| 5432 | is_cost_valid = 0; |
| 5433 | break; |
| 5434 | } |
| 5435 | } |
| 5436 | |
| 5437 | if (!is_cost_valid) { |
| 5438 | // reset cost value |
Angie Chiang | c0feea8 | 2016-11-03 15:36:18 -0700 | [diff] [blame] | 5439 | av1_invalid_rd_stats(rd_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | return is_cost_valid; |
| 5443 | } |
| 5444 | #endif // CONFIG_VAR_TX |
| 5445 | |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5446 | static void rd_pick_palette_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 5447 | int dc_mode_cost, |
| 5448 | uint8_t *best_palette_color_map, |
| 5449 | MB_MODE_INFO *const best_mbmi, |
| 5450 | int64_t *best_rd, int *rate, |
| 5451 | int *rate_tokenonly, int64_t *distortion, |
| 5452 | int *skippable) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5453 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5454 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5455 | assert(!is_inter_block(mbmi)); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5456 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5457 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 5458 | assert(bsize >= BLOCK_8X8); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5459 | int this_rate; |
| 5460 | int64_t this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5461 | int colors_u, colors_v, colors; |
| 5462 | const int src_stride = x->plane[1].src.stride; |
| 5463 | const uint8_t *const src_u = x->plane[1].src.buf; |
| 5464 | const uint8_t *const src_v = x->plane[2].src.buf; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5465 | uint8_t *const color_map = xd->plane[1].color_index_map; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5466 | RD_STATS tokenonly_rd_stats; |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5467 | int plane_block_width, plane_block_height, rows, cols; |
| 5468 | av1_get_block_dimensions(bsize, 1, xd, &plane_block_width, |
| 5469 | &plane_block_height, &rows, &cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5470 | if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return; |
| 5471 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5472 | mbmi->uv_mode = UV_DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5473 | #if CONFIG_FILTER_INTRA |
| 5474 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 5475 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5476 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5477 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5478 | if (cpi->common.use_highbitdepth) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5479 | colors_u = av1_count_colors_highbd(src_u, src_stride, rows, cols, |
| 5480 | cpi->common.bit_depth); |
| 5481 | colors_v = av1_count_colors_highbd(src_v, src_stride, rows, cols, |
| 5482 | cpi->common.bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5483 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5484 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5485 | colors_u = av1_count_colors(src_u, src_stride, rows, cols); |
| 5486 | colors_v = av1_count_colors(src_v, src_stride, rows, cols); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5487 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5488 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5489 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5490 | |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5491 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5492 | uint16_t color_cache[2 * PALETTE_MAX_SIZE]; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 5493 | const int n_cache = av1_get_palette_cache(xd, 1, color_cache); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5494 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 5495 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5496 | colors = colors_u > colors_v ? colors_u : colors_v; |
| 5497 | if (colors > 1 && colors <= 64) { |
| 5498 | int r, c, n, i, j; |
| 5499 | const int max_itr = 50; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5500 | float lb_u, ub_u, val_u; |
| 5501 | float lb_v, ub_v, val_v; |
| 5502 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 5503 | float centroids[2 * PALETTE_MAX_SIZE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5504 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5505 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5506 | uint16_t *src_u16 = CONVERT_TO_SHORTPTR(src_u); |
| 5507 | uint16_t *src_v16 = CONVERT_TO_SHORTPTR(src_v); |
| 5508 | if (cpi->common.use_highbitdepth) { |
| 5509 | lb_u = src_u16[0]; |
| 5510 | ub_u = src_u16[0]; |
| 5511 | lb_v = src_v16[0]; |
| 5512 | ub_v = src_v16[0]; |
| 5513 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5514 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5515 | lb_u = src_u[0]; |
| 5516 | ub_u = src_u[0]; |
| 5517 | lb_v = src_v[0]; |
| 5518 | ub_v = src_v[0]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5519 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5520 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5521 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5522 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5523 | for (r = 0; r < rows; ++r) { |
| 5524 | for (c = 0; c < cols; ++c) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5525 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5526 | if (cpi->common.use_highbitdepth) { |
| 5527 | val_u = src_u16[r * src_stride + c]; |
| 5528 | val_v = src_v16[r * src_stride + c]; |
| 5529 | data[(r * cols + c) * 2] = val_u; |
| 5530 | data[(r * cols + c) * 2 + 1] = val_v; |
| 5531 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5532 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5533 | val_u = src_u[r * src_stride + c]; |
| 5534 | val_v = src_v[r * src_stride + c]; |
| 5535 | data[(r * cols + c) * 2] = val_u; |
| 5536 | data[(r * cols + c) * 2 + 1] = val_v; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5537 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5538 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5539 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5540 | if (val_u < lb_u) |
| 5541 | lb_u = val_u; |
| 5542 | else if (val_u > ub_u) |
| 5543 | ub_u = val_u; |
| 5544 | if (val_v < lb_v) |
| 5545 | lb_v = val_v; |
| 5546 | else if (val_v > ub_v) |
| 5547 | ub_v = val_v; |
| 5548 | } |
| 5549 | } |
| 5550 | |
| 5551 | for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2; |
| 5552 | --n) { |
| 5553 | for (i = 0; i < n; ++i) { |
| 5554 | centroids[i * 2] = lb_u + (2 * i + 1) * (ub_u - lb_u) / n / 2; |
| 5555 | centroids[i * 2 + 1] = lb_v + (2 * i + 1) * (ub_v - lb_v) / n / 2; |
| 5556 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5557 | 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] | 5558 | #if CONFIG_PALETTE_DELTA_ENCODING |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5559 | optimize_palette_colors(color_cache, n_cache, n, 2, centroids); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 5560 | // Sort the U channel colors in ascending order. |
| 5561 | for (i = 0; i < 2 * (n - 1); i += 2) { |
| 5562 | int min_idx = i; |
| 5563 | float min_val = centroids[i]; |
| 5564 | for (j = i + 2; j < 2 * n; j += 2) |
| 5565 | if (centroids[j] < min_val) min_val = centroids[j], min_idx = j; |
| 5566 | if (min_idx != i) { |
| 5567 | float temp_u = centroids[i], temp_v = centroids[i + 1]; |
| 5568 | centroids[i] = centroids[min_idx]; |
| 5569 | centroids[i + 1] = centroids[min_idx + 1]; |
| 5570 | centroids[min_idx] = temp_u, centroids[min_idx + 1] = temp_v; |
| 5571 | } |
| 5572 | } |
| 5573 | av1_calc_indices(data, centroids, color_map, rows * cols, n, 2); |
| 5574 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5575 | extend_palette_color_map(color_map, cols, rows, plane_block_width, |
| 5576 | plane_block_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5577 | pmi->palette_size[1] = n; |
| 5578 | for (i = 1; i < 3; ++i) { |
| 5579 | for (j = 0; j < n; ++j) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5580 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5581 | if (cpi->common.use_highbitdepth) |
| 5582 | pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = clip_pixel_highbd( |
| 5583 | (int)centroids[j * 2 + i - 1], cpi->common.bit_depth); |
| 5584 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 5585 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5586 | pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = |
| 5587 | clip_pixel((int)centroids[j * 2 + i - 1]); |
| 5588 | } |
| 5589 | } |
| 5590 | |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5591 | super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd); |
| 5592 | if (tokenonly_rd_stats.rate == INT_MAX) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5593 | this_rate = |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5594 | tokenonly_rd_stats.rate + dc_mode_cost + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5595 | x->palette_uv_size_cost[bsize - BLOCK_8X8][n - PALETTE_MIN_SIZE] + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5596 | write_uniform_cost(n, color_map[0]) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5597 | av1_cost_bit( |
| 5598 | av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 1); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 5599 | this_rate += av1_palette_color_cost_uv(pmi, |
| 5600 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 5601 | color_cache, n_cache, |
| 5602 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 5603 | cpi->common.bit_depth); |
Sarah Parker | 99e7daa | 2017-08-29 10:30:13 -0700 | [diff] [blame] | 5604 | this_rate += |
| 5605 | 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] | 5606 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5607 | if (this_rd < *best_rd) { |
| 5608 | *best_rd = this_rd; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5609 | *best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5610 | memcpy(best_palette_color_map, color_map, |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 5611 | plane_block_width * plane_block_height * |
| 5612 | sizeof(best_palette_color_map[0])); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5613 | *rate = this_rate; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5614 | *distortion = tokenonly_rd_stats.dist; |
| 5615 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 5616 | *skippable = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5617 | } |
| 5618 | } |
| 5619 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5620 | if (best_mbmi->palette_mode_info.palette_size[1] > 0) { |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5621 | memcpy(color_map, best_palette_color_map, |
Luc Trudeau | 0401e89 | 2017-08-31 00:37:11 -0400 | [diff] [blame] | 5622 | plane_block_width * plane_block_height * |
| 5623 | sizeof(best_palette_color_map[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5624 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5625 | } |
| 5626 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5627 | #if CONFIG_FILTER_INTRA |
| 5628 | // Return 1 if an filter intra mode is selected; return 0 otherwise. |
| 5629 | static int rd_pick_filter_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 5630 | int *rate, int *rate_tokenonly, |
| 5631 | int64_t *distortion, int *skippable, |
| 5632 | BLOCK_SIZE bsize, int64_t *best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5633 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5634 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5635 | int filter_intra_selected_flag = 0; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5636 | int this_rate; |
| 5637 | int64_t this_rd; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5638 | FILTER_INTRA_MODE mode; |
| 5639 | FILTER_INTRA_MODE_INFO filter_intra_mode_info; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5640 | RD_STATS tokenonly_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5641 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5642 | av1_zero(filter_intra_mode_info); |
| 5643 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 1; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5644 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5645 | mbmi->palette_mode_info.palette_size[1] = 0; |
| 5646 | |
| 5647 | for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5648 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = mode; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5649 | if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5650 | continue; |
| 5651 | |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5652 | this_rate = tokenonly_rd_stats.rate + |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5653 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 1) + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5654 | x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode] + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5655 | write_uniform_cost(FILTER_INTRA_MODES, mode); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5656 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5657 | if (this_rd < *best_rd) { |
| 5658 | *best_rd = this_rd; |
| 5659 | *rate = this_rate; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5660 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 5661 | *distortion = tokenonly_rd_stats.dist; |
| 5662 | *skippable = tokenonly_rd_stats.skip; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5663 | filter_intra_mode_info = mbmi->filter_intra_mode_info; |
| 5664 | filter_intra_selected_flag = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5665 | } |
| 5666 | } |
| 5667 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5668 | if (filter_intra_selected_flag) { |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5669 | mbmi->uv_mode = UV_DC_PRED; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5670 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 5671 | filter_intra_mode_info.use_filter_intra_mode[1]; |
| 5672 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 5673 | filter_intra_mode_info.filter_intra_mode[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5674 | return 1; |
| 5675 | } else { |
| 5676 | return 0; |
| 5677 | } |
| 5678 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5679 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5680 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5681 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5682 | // Run RD calculation with given chroma intra prediction angle., and return |
| 5683 | // the RD cost. Update the best mode info. if the RD cost is the best so far. |
| 5684 | static int64_t pick_intra_angle_routine_sbuv( |
| 5685 | const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, |
| 5686 | int rate_overhead, int64_t best_rd_in, int *rate, RD_STATS *rd_stats, |
| 5687 | int *best_angle_delta, int64_t *best_rd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5688 | MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5689 | assert(!is_inter_block(mbmi)); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5690 | int this_rate; |
| 5691 | int64_t this_rd; |
| 5692 | RD_STATS tokenonly_rd_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5693 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5694 | if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in)) |
| 5695 | return INT64_MAX; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5696 | this_rate = tokenonly_rd_stats.rate + rate_overhead; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5697 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5698 | if (this_rd < *best_rd) { |
| 5699 | *best_rd = this_rd; |
| 5700 | *best_angle_delta = mbmi->angle_delta[1]; |
| 5701 | *rate = this_rate; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5702 | rd_stats->rate = tokenonly_rd_stats.rate; |
| 5703 | rd_stats->dist = tokenonly_rd_stats.dist; |
| 5704 | rd_stats->skip = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5705 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5706 | return this_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5707 | } |
| 5708 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5709 | // With given chroma directional intra prediction mode, pick the best angle |
| 5710 | // 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] | 5711 | 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] | 5712 | BLOCK_SIZE bsize, int rate_overhead, |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5713 | int64_t best_rd, int *rate, |
| 5714 | RD_STATS *rd_stats) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5715 | MACROBLOCKD *const xd = &x->e_mbd; |
| 5716 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5717 | assert(!is_inter_block(mbmi)); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5718 | int i, angle_delta, best_angle_delta = 0; |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5719 | 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] | 5720 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5721 | rd_stats->rate = INT_MAX; |
| 5722 | rd_stats->skip = 0; |
| 5723 | rd_stats->dist = INT64_MAX; |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5724 | 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] | 5725 | |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5726 | for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5727 | for (i = 0; i < 2; ++i) { |
| 5728 | best_rd_in = (best_rd == INT64_MAX) |
| 5729 | ? INT64_MAX |
| 5730 | : (best_rd + (best_rd >> ((angle_delta == 0) ? 3 : 5))); |
| 5731 | mbmi->angle_delta[1] = (1 - 2 * i) * angle_delta; |
| 5732 | this_rd = pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, |
| 5733 | best_rd_in, rate, rd_stats, |
| 5734 | &best_angle_delta, &best_rd); |
| 5735 | rd_cost[2 * angle_delta + i] = this_rd; |
| 5736 | if (angle_delta == 0) { |
| 5737 | if (this_rd == INT64_MAX) return 0; |
| 5738 | rd_cost[1] = this_rd; |
| 5739 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5740 | } |
| 5741 | } |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5742 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5743 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5744 | assert(best_rd != INT64_MAX); |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5745 | for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5746 | int64_t rd_thresh; |
| 5747 | for (i = 0; i < 2; ++i) { |
| 5748 | int skip_search = 0; |
| 5749 | rd_thresh = best_rd + (best_rd >> 5); |
| 5750 | if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh && |
| 5751 | rd_cost[2 * (angle_delta - 1) + i] > rd_thresh) |
| 5752 | skip_search = 1; |
| 5753 | if (!skip_search) { |
| 5754 | mbmi->angle_delta[1] = (1 - 2 * i) * angle_delta; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 5755 | pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, best_rd, |
| 5756 | rate, rd_stats, &best_angle_delta, |
| 5757 | &best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5758 | } |
| 5759 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | mbmi->angle_delta[1] = best_angle_delta; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5763 | return rd_stats->rate != INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5764 | } |
| 5765 | #endif // CONFIG_EXT_INTRA |
| 5766 | |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5767 | #if CONFIG_CFL |
Luc Trudeau | 6066913 | 2017-09-28 16:49:49 -0400 | [diff] [blame] | 5768 | // TODO(ltrudeau) add support for HBD. |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5769 | 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] | 5770 | int src_stride, int width, int height, |
| 5771 | int dc_pred, int alpha_q3, |
| 5772 | int64_t *dist_neg_out) { |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5773 | int64_t dist = 0; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5774 | int diff; |
| 5775 | |
| 5776 | if (alpha_q3 == 0) { |
| 5777 | for (int j = 0; j < height; j++) { |
| 5778 | for (int i = 0; i < width; i++) { |
| 5779 | diff = src[i] - dc_pred; |
| 5780 | dist += diff * diff; |
| 5781 | } |
| 5782 | src += src_stride; |
| 5783 | } |
| 5784 | |
| 5785 | if (dist_neg_out) *dist_neg_out = dist; |
| 5786 | |
| 5787 | return dist; |
| 5788 | } |
| 5789 | |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5790 | int64_t dist_neg = 0; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5791 | for (int j = 0; j < height; j++) { |
| 5792 | for (int i = 0; i < width; i++) { |
| 5793 | const int uv = src[i]; |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5794 | 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] | 5795 | |
Luc Trudeau | 6066913 | 2017-09-28 16:49:49 -0400 | [diff] [blame] | 5796 | diff = uv - clip_pixel(scaled_luma + dc_pred); |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5797 | dist += diff * diff; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5798 | |
Luc Trudeau | 6066913 | 2017-09-28 16:49:49 -0400 | [diff] [blame] | 5799 | diff = uv - clip_pixel(-scaled_luma + dc_pred); |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5800 | dist_neg += diff * diff; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5801 | } |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5802 | pred_buf_q3 += MAX_SB_SIZE; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5803 | src += src_stride; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5804 | } |
| 5805 | |
| 5806 | if (dist_neg_out) *dist_neg_out = dist_neg; |
| 5807 | |
| 5808 | return dist; |
| 5809 | } |
| 5810 | |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5811 | static int cfl_rd_pick_alpha(MACROBLOCK *const x, TX_SIZE tx_size) { |
| 5812 | const struct macroblock_plane *const p_u = &x->plane[AOM_PLANE_U]; |
| 5813 | const struct macroblock_plane *const p_v = &x->plane[AOM_PLANE_V]; |
| 5814 | const uint8_t *const src_u = p_u->src.buf; |
| 5815 | const uint8_t *const src_v = p_v->src.buf; |
| 5816 | const int src_stride_u = p_u->src.stride; |
| 5817 | const int src_stride_v = p_v->src.stride; |
| 5818 | |
| 5819 | MACROBLOCKD *const xd = &x->e_mbd; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5820 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 5821 | |
| 5822 | CFL_CTX *const cfl = xd->cfl; |
| 5823 | cfl_compute_parameters(xd, tx_size); |
| 5824 | const int width = cfl->uv_width; |
| 5825 | const int height = cfl->uv_height; |
| 5826 | const int dc_pred_u = cfl->dc_pred[CFL_PRED_U]; |
| 5827 | const int dc_pred_v = cfl->dc_pred[CFL_PRED_V]; |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5828 | const int16_t *pred_buf_q3 = cfl->pred_buf_q3; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5829 | |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5830 | int64_t sse[CFL_PRED_PLANES][CFL_MAGS_SIZE]; |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5831 | 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] | 5832 | height, dc_pred_u, 0, NULL); |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5833 | 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] | 5834 | height, dc_pred_v, 0, NULL); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5835 | |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5836 | for (int c = 0; c < CFL_ALPHABET_SIZE; c++) { |
| 5837 | const int m = c * 2 + 1; |
| 5838 | const int abs_alpha_q3 = c + 1; |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5839 | sse[CFL_PRED_U][m] = |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5840 | cfl_alpha_dist(pred_buf_q3, src_u, src_stride_u, width, height, |
| 5841 | dc_pred_u, abs_alpha_q3, &sse[CFL_PRED_U][m + 1]); |
Luc Trudeau | 593d02c | 2017-09-08 11:29:37 -0400 | [diff] [blame] | 5842 | sse[CFL_PRED_V][m] = |
Luc Trudeau | 4e26d66 | 2017-09-11 13:08:40 -0400 | [diff] [blame] | 5843 | cfl_alpha_dist(pred_buf_q3, src_v, src_stride_v, width, height, |
| 5844 | 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] | 5845 | } |
| 5846 | |
Luc Trudeau | 4c5df10 | 2017-07-08 14:43:27 -0400 | [diff] [blame] | 5847 | int64_t dist; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5848 | int64_t cost; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5849 | int64_t best_cost = INT64_MAX; |
| 5850 | int best_rate = 0; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5851 | |
| 5852 | // Compute least squares parameter of the entire block |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5853 | int ind = 0; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5854 | int signs = 0; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5855 | |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5856 | for (int joint_sign = 0; joint_sign < CFL_JOINT_SIGNS; joint_sign++) { |
| 5857 | const int sign_u = CFL_SIGN_U(joint_sign); |
| 5858 | const int sign_v = CFL_SIGN_V(joint_sign); |
| 5859 | const int size_u = (sign_u == CFL_SIGN_ZERO) ? 1 : CFL_ALPHABET_SIZE; |
| 5860 | const int size_v = (sign_v == CFL_SIGN_ZERO) ? 1 : CFL_ALPHABET_SIZE; |
| 5861 | for (int u = 0; u < size_u; u++) { |
| 5862 | const int idx_u = (sign_u == CFL_SIGN_ZERO) ? 0 : u * 2 + 1; |
| 5863 | for (int v = 0; v < size_v; v++) { |
| 5864 | 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] | 5865 | dist = sse[CFL_PRED_U][idx_u + (sign_u == CFL_SIGN_NEG)] + |
| 5866 | sse[CFL_PRED_V][idx_v + (sign_v == CFL_SIGN_NEG)]; |
| 5867 | dist *= 16; |
David Michael Barr | 38e560c | 2017-08-16 21:46:37 +0900 | [diff] [blame] | 5868 | const int rate = x->cfl_cost[joint_sign][CFL_PRED_U][u] + |
| 5869 | x->cfl_cost[joint_sign][CFL_PRED_V][v]; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5870 | cost = RDCOST(x->rdmult, rate, dist); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5871 | if (cost < best_cost) { |
| 5872 | best_cost = cost; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5873 | best_rate = rate; |
| 5874 | ind = (u << CFL_ALPHABET_SIZE_LOG2) + v; |
| 5875 | signs = joint_sign; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5876 | } |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | mbmi->cfl_alpha_idx = ind; |
David Michael Barr | f6eaa15 | 2017-07-19 19:42:28 +0900 | [diff] [blame] | 5882 | mbmi->cfl_alpha_signs = signs; |
| 5883 | return best_rate; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5884 | } |
| 5885 | #endif // CONFIG_CFL |
| 5886 | |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5887 | static void init_sbuv_mode(MB_MODE_INFO *const mbmi) { |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5888 | mbmi->uv_mode = UV_DC_PRED; |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5889 | mbmi->palette_mode_info.palette_size[1] = 0; |
hui su | eaddeee | 2017-05-30 12:19:38 -0700 | [diff] [blame] | 5890 | #if CONFIG_FILTER_INTRA |
| 5891 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 5892 | #endif // CONFIG_FILTER_INTRA |
| 5893 | } |
| 5894 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 5895 | static int64_t rd_pick_intra_sbuv_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 5896 | int *rate, int *rate_tokenonly, |
| 5897 | int64_t *distortion, int *skippable, |
| 5898 | BLOCK_SIZE bsize, TX_SIZE max_tx_size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5899 | MACROBLOCKD *xd = &x->e_mbd; |
| 5900 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 330aec8 | 2017-05-08 15:37:42 -0700 | [diff] [blame] | 5901 | assert(!is_inter_block(mbmi)); |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5902 | MB_MODE_INFO best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5903 | int64_t best_rd = INT64_MAX, this_rd; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5904 | #if CONFIG_PVQ |
| 5905 | od_rollback_buffer buf; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5906 | od_encode_checkpoint(&x->daala_enc, &buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5907 | #endif // CONFIG_PVQ |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 5908 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 5909 | const int try_palette = can_use_palette(cpi, mbmi); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5910 | |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5911 | for (int mode_idx = 0; mode_idx < UV_INTRA_MODES; ++mode_idx) { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 5912 | int this_rate; |
| 5913 | RD_STATS tokenonly_rd_stats; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 5914 | UV_PREDICTION_MODE mode = uv_rd_search_mode_order[mode_idx]; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5915 | #if CONFIG_EXT_INTRA |
| 5916 | const int is_directional_mode = |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 5917 | av1_is_directional_mode(get_uv_mode(mode), mbmi->sb_type); |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5918 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 5919 | if (!(cpi->sf.intra_uv_mode_mask[txsize_sqr_up_map[max_tx_size]] & |
| 5920 | (1 << mode))) |
| 5921 | continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5922 | |
| 5923 | mbmi->uv_mode = mode; |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5924 | #if CONFIG_CFL |
| 5925 | int cfl_alpha_rate = 0; |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 5926 | if (mode == UV_CFL_PRED) { |
| 5927 | assert(!is_directional_mode); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 5928 | const TX_SIZE uv_tx_size = av1_get_uv_tx_size(mbmi, &xd->plane[1]); |
| 5929 | cfl_alpha_rate = cfl_rd_pick_alpha(x, uv_tx_size); |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5930 | } |
| 5931 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5932 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5933 | mbmi->angle_delta[1] = 0; |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 5934 | if (is_directional_mode && av1_use_angle_delta(mbmi->sb_type)) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5935 | const int rate_overhead = x->intra_uv_mode_cost[mbmi->mode][mode] + |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5936 | write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, 0); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5937 | if (!rd_pick_intra_angle_sbuv(cpi, x, bsize, rate_overhead, best_rd, |
| 5938 | &this_rate, &tokenonly_rd_stats)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5939 | continue; |
| 5940 | } else { |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5941 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5942 | if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd)) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5943 | #if CONFIG_PVQ |
| 5944 | od_encode_rollback(&x->daala_enc, &buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5945 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5946 | continue; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5947 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5948 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5949 | } |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5950 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5951 | this_rate = |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5952 | tokenonly_rd_stats.rate + x->intra_uv_mode_cost[mbmi->mode][mode]; |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5953 | |
Luc Trudeau | dff4192 | 2017-07-07 09:47:58 -0400 | [diff] [blame] | 5954 | #if CONFIG_CFL |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 5955 | if (mode == UV_CFL_PRED) { |
David Michael Barr | 2510f64 | 2017-07-11 23:39:20 +0900 | [diff] [blame] | 5956 | this_rate += cfl_alpha_rate; |
Luc Trudeau | dff4192 | 2017-07-07 09:47:58 -0400 | [diff] [blame] | 5957 | } |
| 5958 | #endif |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5959 | #if CONFIG_EXT_INTRA |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 5960 | if (is_directional_mode && av1_use_angle_delta(mbmi->sb_type)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 5961 | this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 5962 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 5963 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5964 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5965 | #if CONFIG_FILTER_INTRA |
Luc Trudeau | 91357ee | 2017-09-27 13:40:37 -0400 | [diff] [blame] | 5966 | if (mbmi->sb_type >= BLOCK_8X8 && mode == UV_DC_PRED) |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5967 | this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 0); |
| 5968 | #endif // CONFIG_FILTER_INTRA |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 5969 | if (try_palette && mode == UV_DC_PRED) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5970 | this_rate += av1_cost_bit( |
| 5971 | av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5972 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5973 | #if CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 5974 | od_encode_rollback(&x->daala_enc, &buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 5975 | #endif // CONFIG_PVQ |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 5976 | this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5977 | |
| 5978 | if (this_rd < best_rd) { |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5979 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5980 | best_rd = this_rd; |
| 5981 | *rate = this_rate; |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 5982 | *rate_tokenonly = tokenonly_rd_stats.rate; |
| 5983 | *distortion = tokenonly_rd_stats.dist; |
| 5984 | *skippable = tokenonly_rd_stats.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5985 | } |
| 5986 | } |
| 5987 | |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 5988 | if (try_palette) { |
hui su | 8a516a8 | 2017-07-06 10:00:36 -0700 | [diff] [blame] | 5989 | 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] | 5990 | rd_pick_palette_intra_sbuv(cpi, x, |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 5991 | x->intra_uv_mode_cost[mbmi->mode][UV_DC_PRED], |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5992 | best_palette_color_map, &best_mbmi, &best_rd, |
| 5993 | rate, rate_tokenonly, distortion, skippable); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5994 | } |
| 5995 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 5996 | #if CONFIG_FILTER_INTRA |
| 5997 | if (mbmi->sb_type >= BLOCK_8X8) { |
| 5998 | if (rd_pick_filter_intra_sbuv(cpi, x, rate, rate_tokenonly, distortion, |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 5999 | skippable, bsize, &best_rd)) |
| 6000 | best_mbmi = *mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6001 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 6002 | #endif // CONFIG_FILTER_INTRA |
| 6003 | |
hui su | 83c2663 | 2017-01-24 17:19:06 -0800 | [diff] [blame] | 6004 | *mbmi = best_mbmi; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 6005 | // Make sure we actually chose a mode |
| 6006 | assert(best_rd < INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6007 | return best_rd; |
| 6008 | } |
| 6009 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6010 | 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] | 6011 | BLOCK_SIZE bsize, TX_SIZE max_tx_size, |
| 6012 | int *rate_uv, int *rate_uv_tokenonly, |
| 6013 | int64_t *dist_uv, int *skip_uv, |
| 6014 | UV_PREDICTION_MODE *mode_uv) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6015 | MACROBLOCKD *xd = &x->e_mbd; |
| 6016 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6017 | // Use an estimated rd for uv_intra based on DC_PRED if the |
| 6018 | // appropriate speed flag is set. |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6019 | init_sbuv_mode(mbmi); |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 6020 | #if CONFIG_CB4X4 |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 6021 | #if !CONFIG_CHROMA_2X2 |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 6022 | if (x->skip_chroma_rd) { |
| 6023 | *rate_uv = 0; |
| 6024 | *rate_uv_tokenonly = 0; |
| 6025 | *dist_uv = 0; |
| 6026 | *skip_uv = 1; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 6027 | *mode_uv = UV_DC_PRED; |
Jingning Han | 9ce464c | 2017-02-20 15:36:30 -0800 | [diff] [blame] | 6028 | return; |
| 6029 | } |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6030 | bsize = scale_chroma_bsize(bsize, xd->plane[AOM_PLANE_U].subsampling_x, |
| 6031 | xd->plane[AOM_PLANE_U].subsampling_y); |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 6032 | #endif // !CONFIG_CHROMA_2X2 |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6033 | #if CONFIG_CFL |
| 6034 | // Only store reconstructed luma when there's chroma RDO. When there's no |
| 6035 | // chroma RDO, the reconstructed luma will be stored in encode_superblock(). |
| 6036 | xd->cfl->store_y = !x->skip_chroma_rd; |
| 6037 | #endif // CONFIG_CFL |
Jingning Han | 31b6a4f | 2017-02-23 11:05:53 -0800 | [diff] [blame] | 6038 | #else |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 6039 | bsize = bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize; |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6040 | #if CONFIG_CFL |
| 6041 | xd->cfl->store_y = 1; |
| 6042 | #endif // CONFIG_CFL |
Jingning Han | 31b6a4f | 2017-02-23 11:05:53 -0800 | [diff] [blame] | 6043 | #endif // CONFIG_CB4X4 |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6044 | #if CONFIG_CFL |
| 6045 | if (xd->cfl->store_y) { |
| 6046 | // Perform one extra call to txfm_rd_in_plane(), with the values chosen |
| 6047 | // during luma RDO, so we can store reconstructed luma values |
| 6048 | RD_STATS this_rd_stats; |
| 6049 | txfm_rd_in_plane(x, cpi, &this_rd_stats, INT64_MAX, AOM_PLANE_Y, |
| 6050 | mbmi->sb_type, mbmi->tx_size, |
| 6051 | cpi->sf.use_fast_coef_costing); |
| 6052 | xd->cfl->store_y = 0; |
| 6053 | } |
| 6054 | #endif // CONFIG_CFL |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 6055 | rd_pick_intra_sbuv_mode(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv, skip_uv, |
| 6056 | bsize, max_tx_size); |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 6057 | *mode_uv = mbmi->uv_mode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6058 | } |
| 6059 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6060 | static int cost_mv_ref(const MACROBLOCK *const x, PREDICTION_MODE mode, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6061 | int16_t mode_context) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6062 | if (is_inter_compound_mode(mode)) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6063 | return x |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 6064 | ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6065 | #if CONFIG_COMPOUND_SINGLEREF |
| 6066 | } else if (is_inter_singleref_comp_mode(mode)) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6067 | return x->inter_singleref_comp_mode_cost[mode_context] |
| 6068 | [INTER_SINGLEREF_COMP_OFFSET(mode)]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6069 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6070 | } |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6071 | |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6072 | int mode_cost = 0; |
| 6073 | int16_t mode_ctx = mode_context & NEWMV_CTX_MASK; |
| 6074 | int16_t is_all_zero_mv = mode_context & (1 << ALL_ZERO_FLAG_OFFSET); |
| 6075 | |
| 6076 | assert(is_inter_mode(mode)); |
| 6077 | |
| 6078 | if (mode == NEWMV) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6079 | mode_cost = x->newmv_mode_cost[mode_ctx][0]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6080 | return mode_cost; |
| 6081 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6082 | mode_cost = x->newmv_mode_cost[mode_ctx][1]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6083 | mode_ctx = (mode_context >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK; |
| 6084 | |
| 6085 | if (is_all_zero_mv) return mode_cost; |
| 6086 | |
| 6087 | if (mode == ZEROMV) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6088 | mode_cost += x->zeromv_mode_cost[mode_ctx][0]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6089 | return mode_cost; |
| 6090 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6091 | mode_cost += x->zeromv_mode_cost[mode_ctx][1]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6092 | mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK; |
| 6093 | |
| 6094 | if (mode_context & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6; |
| 6095 | if (mode_context & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7; |
| 6096 | if (mode_context & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8; |
| 6097 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6098 | mode_cost += x->refmv_mode_cost[mode_ctx][mode != NEARESTMV]; |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 6099 | return mode_cost; |
| 6100 | } |
| 6101 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6102 | } |
| 6103 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6104 | #if (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT) |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6105 | static int get_interinter_compound_type_bits(BLOCK_SIZE bsize, |
| 6106 | COMPOUND_TYPE comp_type) { |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 6107 | (void)bsize; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6108 | switch (comp_type) { |
| 6109 | case COMPOUND_AVERAGE: return 0; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 6110 | #if CONFIG_WEDGE |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6111 | case COMPOUND_WEDGE: return get_interinter_wedge_bits(bsize); |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 6112 | #endif // CONFIG_WEDGE |
Sarah Parker | 2f6ce75 | 2016-12-08 15:26:46 -0800 | [diff] [blame] | 6113 | #if CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 6114 | case COMPOUND_SEG: return 1; |
Sarah Parker | 2f6ce75 | 2016-12-08 15:26:46 -0800 | [diff] [blame] | 6115 | #endif // CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6116 | default: assert(0); return 0; |
| 6117 | } |
| 6118 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6119 | #endif // (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT) |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 6120 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6121 | typedef struct { |
| 6122 | int eobs; |
| 6123 | int brate; |
| 6124 | int byrate; |
| 6125 | int64_t bdist; |
| 6126 | int64_t bsse; |
| 6127 | int64_t brdcost; |
| 6128 | int_mv mvs[2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6129 | int_mv pred_mv[2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6130 | int_mv ref_mv[2]; |
Jingning Han | 276c294 | 2016-12-05 12:37:02 -0800 | [diff] [blame] | 6131 | |
Timothy B. Terriberry | fe67ed6 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 6132 | #if CONFIG_CHROMA_2X2 |
Jingning Han | 276c294 | 2016-12-05 12:37:02 -0800 | [diff] [blame] | 6133 | ENTROPY_CONTEXT ta[4]; |
| 6134 | ENTROPY_CONTEXT tl[4]; |
| 6135 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6136 | ENTROPY_CONTEXT ta[2]; |
| 6137 | ENTROPY_CONTEXT tl[2]; |
Timothy B. Terriberry | fe67ed6 | 2017-04-26 16:53:47 -0700 | [diff] [blame] | 6138 | #endif // CONFIG_CHROMA_2X2 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6139 | } SEG_RDSTAT; |
| 6140 | |
| 6141 | typedef struct { |
| 6142 | int_mv *ref_mv[2]; |
| 6143 | int_mv mvp; |
| 6144 | |
| 6145 | int64_t segment_rd; |
| 6146 | int r; |
| 6147 | int64_t d; |
| 6148 | int64_t sse; |
| 6149 | int segment_yrate; |
| 6150 | PREDICTION_MODE modes[4]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6151 | #if CONFIG_COMPOUND_SINGLEREF |
| 6152 | SEG_RDSTAT rdstat[4][INTER_MODES + INTER_SINGLEREF_COMP_MODES + |
| 6153 | INTER_COMPOUND_MODES]; |
| 6154 | #else // !CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6155 | SEG_RDSTAT rdstat[4][INTER_MODES + INTER_COMPOUND_MODES]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6156 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6157 | int mvthresh; |
| 6158 | } BEST_SEG_INFO; |
| 6159 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6160 | static INLINE int mv_check_bounds(const MvLimits *mv_limits, const MV *mv) { |
| 6161 | return (mv->row >> 3) < mv_limits->row_min || |
| 6162 | (mv->row >> 3) > mv_limits->row_max || |
| 6163 | (mv->col >> 3) < mv_limits->col_min || |
| 6164 | (mv->col >> 3) > mv_limits->col_max; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6165 | } |
| 6166 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6167 | // Check if NEARESTMV/NEARMV/ZEROMV is the cheapest way encode zero motion. |
| 6168 | // TODO(aconverse): Find out if this is still productive then clean up or remove |
| 6169 | static int check_best_zero_mv( |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6170 | const AV1_COMP *const cpi, const MACROBLOCK *const x, |
| 6171 | const int16_t mode_context[TOTAL_REFS_PER_FRAME], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6172 | const int16_t compound_mode_context[TOTAL_REFS_PER_FRAME], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6173 | 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] | 6174 | const MV_REFERENCE_FRAME ref_frames[2], const BLOCK_SIZE bsize, int block, |
| 6175 | int mi_row, int mi_col) { |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6176 | int_mv zeromv[2] = { {.as_int = 0 } }; |
Yushin Cho | c9751c5 | 2017-06-12 10:38:29 -0700 | [diff] [blame] | 6177 | #if CONFIG_GLOBAL_MOTION |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6178 | int comp_pred_mode = ref_frames[1] > INTRA_FRAME; |
Yushin Cho | c9751c5 | 2017-06-12 10:38:29 -0700 | [diff] [blame] | 6179 | #endif |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 6180 | (void)mi_row; |
| 6181 | (void)mi_col; |
Zoe Liu | bc030ee | 2017-07-31 15:20:46 -0700 | [diff] [blame] | 6182 | (void)cpi; |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6183 | #if CONFIG_GLOBAL_MOTION |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6184 | if (this_mode == ZEROMV || this_mode == ZERO_ZEROMV) { |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6185 | 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] | 6186 | zeromv[cur_frm].as_int = |
| 6187 | gm_get_motion_vector(&cpi->common.global_motion[ref_frames[cur_frm]], |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 6188 | cpi->common.allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6189 | mi_col, mi_row, block |
| 6190 | #if CONFIG_AMVR |
| 6191 | , |
| 6192 | cpi->common.cur_frame_mv_precision_level |
| 6193 | #endif |
| 6194 | ) |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6195 | .as_int; |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6196 | } |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6197 | } |
Luc Trudeau | d28e91d | 2017-06-05 14:42:26 -0400 | [diff] [blame] | 6198 | #endif // CONFIG_GLOBAL_MOTION |
| 6199 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6200 | if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) && |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6201 | 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] | 6202 | (ref_frames[1] <= INTRA_FRAME || |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 6203 | 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] | 6204 | int16_t rfc = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6205 | av1_mode_context_analyzer(mode_context, ref_frames, bsize, block); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6206 | int c1 = cost_mv_ref(x, NEARMV, rfc); |
| 6207 | int c2 = cost_mv_ref(x, NEARESTMV, rfc); |
| 6208 | int c3 = cost_mv_ref(x, ZEROMV, rfc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6209 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6210 | if (this_mode == NEARMV) { |
| 6211 | if (c1 > c3) return 0; |
| 6212 | } else if (this_mode == NEARESTMV) { |
| 6213 | if (c2 > c3) return 0; |
| 6214 | } else { |
| 6215 | assert(this_mode == ZEROMV); |
| 6216 | if (ref_frames[1] <= INTRA_FRAME) { |
| 6217 | if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0) || |
| 6218 | (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0)) |
| 6219 | return 0; |
| 6220 | } else { |
| 6221 | if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0 && |
| 6222 | frame_mv[NEARESTMV][ref_frames[1]].as_int == 0) || |
| 6223 | (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0 && |
| 6224 | frame_mv[NEARMV][ref_frames[1]].as_int == 0)) |
| 6225 | return 0; |
| 6226 | } |
| 6227 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6228 | } else if ((this_mode == NEAREST_NEARESTMV || this_mode == NEAR_NEARMV || |
| 6229 | this_mode == ZERO_ZEROMV) && |
| 6230 | frame_mv[this_mode][ref_frames[0]].as_int == zeromv[0].as_int && |
| 6231 | 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] | 6232 | int16_t rfc = compound_mode_context[ref_frames[0]]; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 6233 | int c2 = cost_mv_ref(x, NEAREST_NEARESTMV, rfc); |
| 6234 | int c3 = cost_mv_ref(x, ZERO_ZEROMV, rfc); |
| 6235 | int c5 = cost_mv_ref(x, NEAR_NEARMV, rfc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6236 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 6237 | if (this_mode == NEAREST_NEARESTMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6238 | if (c2 > c3) return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6239 | } else if (this_mode == NEAR_NEARMV) { |
| 6240 | if (c5 > c3) return 0; |
| 6241 | } else { |
| 6242 | assert(this_mode == ZERO_ZEROMV); |
| 6243 | if ((c3 >= c2 && frame_mv[NEAREST_NEARESTMV][ref_frames[0]].as_int == 0 && |
| 6244 | frame_mv[NEAREST_NEARESTMV][ref_frames[1]].as_int == 0) || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6245 | (c3 >= c5 && frame_mv[NEAR_NEARMV][ref_frames[0]].as_int == 0 && |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 6246 | frame_mv[NEAR_NEARMV][ref_frames[1]].as_int == 0)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6247 | return 0; |
| 6248 | } |
| 6249 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6250 | return 1; |
| 6251 | } |
| 6252 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6253 | static void joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x, |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6254 | BLOCK_SIZE bsize, int_mv *frame_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6255 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6256 | int_mv *frame_comp_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6257 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6258 | int mi_row, int mi_col, |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6259 | int_mv *ref_mv_sub8x8[2], const uint8_t *mask, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6260 | int mask_stride, int *rate_mv, |
| 6261 | const int block) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6262 | const AV1_COMMON *const cm = &cpi->common; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 6263 | const int pw = block_size_wide[bsize]; |
| 6264 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6265 | MACROBLOCKD *xd = &x->e_mbd; |
| 6266 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6267 | // This function should only ever be called for compound modes |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6268 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6269 | if (!has_second_ref(mbmi)) { |
| 6270 | assert(is_inter_singleref_comp_mode(mbmi->mode)); |
| 6271 | assert(frame_comp_mv); |
| 6272 | } |
| 6273 | assert(has_second_ref(mbmi) || is_inter_singleref_comp_mode(mbmi->mode)); |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 6274 | const int refs[2] = { mbmi->ref_frame[0], |
| 6275 | has_second_ref(mbmi) ? mbmi->ref_frame[1] |
| 6276 | : mbmi->ref_frame[0] }; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6277 | #else |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6278 | assert(has_second_ref(mbmi)); |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 6279 | const int refs[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] }; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6280 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6281 | int_mv ref_mv[2]; |
| 6282 | int ite, ref; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6283 | struct scale_factors sf; |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6284 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 6285 | // 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] | 6286 | const int ic = block & 1; |
| 6287 | const int ir = (block - ic) >> 1; |
Jingning Han | cb63767 | 2017-06-22 09:14:40 -0700 | [diff] [blame] | 6288 | struct macroblockd_plane *const pd = &xd->plane[0]; |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6289 | const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x) + 4 * ic; |
| 6290 | 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] | 6291 | #if CONFIG_GLOBAL_MOTION |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6292 | int is_global[2]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6293 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6294 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 6295 | #else |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6296 | for (ref = 0; ref < 2; ++ref) { |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6297 | #endif // CONFIG_COMPOUND_SINGLEREF |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6298 | WarpedMotionParams *const wm = |
| 6299 | &xd->global_motion[xd->mi[0]->mbmi.ref_frame[ref]]; |
| 6300 | is_global[ref] = is_global_mv_block(xd->mi[0], block, wm->wmtype); |
| 6301 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6302 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6303 | if (!has_second_ref(mbmi)) is_global[1] = is_global[0]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6304 | #endif // CONFIG_COMPOUND_SINGLEREF |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6305 | #endif // CONFIG_GLOBAL_MOTION |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6306 | #else // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 6307 | (void)block; |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6308 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6309 | |
| 6310 | // Do joint motion search in compound mode to get more accurate mv. |
| 6311 | struct buf_2d backup_yv12[2][MAX_MB_PLANE]; |
| 6312 | int last_besterr[2] = { INT_MAX, INT_MAX }; |
| 6313 | const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = { |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 6314 | av1_get_scaled_ref_frame(cpi, refs[0]), |
| 6315 | av1_get_scaled_ref_frame(cpi, refs[1]) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6316 | }; |
| 6317 | |
| 6318 | // Prediction buffer from second frame. |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6319 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6320 | DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]); |
| 6321 | uint8_t *second_pred; |
| 6322 | #else |
| 6323 | DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6324 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6325 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6326 | #if CONFIG_CB4X4 |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6327 | (void)ref_mv_sub8x8; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6328 | #endif // CONFIG_CB4X4 |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 6329 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6330 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6331 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 6332 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6333 | for (ref = 0; ref < 2; ++ref) { |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6334 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 6335 | #if !CONFIG_CB4X4 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6336 | if (bsize < BLOCK_8X8 && ref_mv_sub8x8 != NULL) |
| 6337 | ref_mv[ref].as_int = ref_mv_sub8x8[ref]->as_int; |
| 6338 | else |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6339 | #endif // !CONFIG_CB4X4 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6340 | ref_mv[ref] = x->mbmi_ext->ref_mvs[refs[ref]][0]; |
| 6341 | |
| 6342 | if (scaled_ref_frame[ref]) { |
| 6343 | int i; |
| 6344 | // Swap out the reference frame for a version that's been scaled to |
| 6345 | // match the resolution of the current frame, allowing the existing |
| 6346 | // motion search code to be used without additional modifications. |
| 6347 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6348 | backup_yv12[ref][i] = xd->plane[i].pre[ref]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6349 | av1_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col, |
| 6350 | NULL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6351 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6352 | } |
| 6353 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6354 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6355 | if (!has_second_ref(mbmi)) { |
| 6356 | assert(is_inter_singleref_comp_mode(mbmi->mode)); |
| 6357 | // NOTE: For single ref comp mode, set up the 2nd set of ref_mv/pre_planes |
| 6358 | // all from the 1st reference frame, i.e. refs[0]. |
| 6359 | ref_mv[1] = x->mbmi_ext->ref_mvs[refs[0]][0]; |
| 6360 | if (scaled_ref_frame[0]) { |
| 6361 | int i; |
| 6362 | // Swap out the reference frame for a version that's been scaled to |
| 6363 | // match the resolution of the current frame, allowing the existing |
| 6364 | // motion search code to be used without additional modifications. |
| 6365 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6366 | backup_yv12[1][i] = xd->plane[i].pre[1]; |
| 6367 | av1_setup_pre_planes(xd, 1, scaled_ref_frame[0], mi_row, mi_col, NULL); |
| 6368 | } |
| 6369 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6370 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6371 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6372 | // Since we have scaled the reference frames to match the size of the current |
| 6373 | // frame we must use a unit scaling factor during mode selection. |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6374 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6375 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 6376 | cm->height, cm->use_highbitdepth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6377 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6378 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 6379 | cm->height); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6380 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6381 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6382 | // Allow joint search multiple times iteratively for each reference frame |
| 6383 | // and break out of the search loop if it couldn't find a better mv. |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6384 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6385 | const int num_ites = |
| 6386 | (has_second_ref(mbmi) || mbmi->mode == SR_NEW_NEWMV) ? 4 : 1; |
| 6387 | const int start_ite = has_second_ref(mbmi) ? 0 : 1; |
| 6388 | for (ite = start_ite; ite < (start_ite + num_ites); ite++) { |
| 6389 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6390 | for (ite = 0; ite < 4; ite++) { |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6391 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6392 | struct buf_2d ref_yv12[2]; |
| 6393 | int bestsme = INT_MAX; |
| 6394 | int sadpb = x->sadperbit16; |
| 6395 | MV *const best_mv = &x->best_mv.as_mv; |
| 6396 | int search_range = 3; |
| 6397 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6398 | MvLimits tmp_mv_limits = x->mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6399 | int id = ite % 2; // Even iterations search in the first reference frame, |
| 6400 | // odd iterations search in the second. The predictor |
| 6401 | // found for the 'other' reference frame is factored in. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 6402 | const int plane = 0; |
David Barker | e64d51a | 2017-06-09 14:52:42 +0100 | [diff] [blame] | 6403 | ConvolveParams conv_params = get_conv_params(!id, 0, plane); |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6404 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 6405 | WarpTypesAllowed warp_types; |
| 6406 | #if CONFIG_GLOBAL_MOTION |
| 6407 | warp_types.global_warp_allowed = is_global[!id]; |
| 6408 | #endif // CONFIG_GLOBAL_MOTION |
| 6409 | #if CONFIG_WARPED_MOTION |
| 6410 | warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL; |
| 6411 | #endif // CONFIG_WARPED_MOTION |
| 6412 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6413 | |
| 6414 | // Initialized here because of compiler problem in Visual Studio. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 6415 | ref_yv12[0] = xd->plane[plane].pre[0]; |
| 6416 | ref_yv12[1] = xd->plane[plane].pre[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6417 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6418 | // Get the prediction block from the 'other' reference frame. |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6419 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6420 | MV *const the_other_mv = (has_second_ref(mbmi) || id) |
| 6421 | ? &frame_mv[refs[!id]].as_mv |
| 6422 | : &frame_comp_mv[refs[0]].as_mv; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6423 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6424 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6425 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6426 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 6427 | second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6428 | av1_highbd_build_inter_predictor( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6429 | ref_yv12[!id].buf, ref_yv12[!id].stride, second_pred, pw, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6430 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6431 | the_other_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6432 | #else // !(CONFIG_COMPOUND_SINGLEREF) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6433 | &frame_mv[refs[!id]].as_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6434 | #endif // CONFIG_COMPOUND_SINGLEREF |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 6435 | &sf, pw, ph, 0, mbmi->interp_filters, |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6436 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 6437 | &warp_types, p_col, p_row, |
| 6438 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Sarah Parker | b3ebed1 | 2017-03-09 10:52:03 -0800 | [diff] [blame] | 6439 | 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] | 6440 | } else { |
| 6441 | second_pred = (uint8_t *)second_pred_alloc_16; |
Zoe Liu | 76fcff7 | 2017-04-24 17:50:53 -0700 | [diff] [blame] | 6442 | #endif // CONFIG_HIGHBITDEPTH |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6443 | av1_build_inter_predictor( |
| 6444 | ref_yv12[!id].buf, ref_yv12[!id].stride, second_pred, pw, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6445 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6446 | the_other_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6447 | #else // !(CONFIG_COMPOUND_SINGLEREF) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6448 | &frame_mv[refs[!id]].as_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6449 | #endif // CONFIG_COMPOUND_SINGLEREF |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 6450 | &sf, pw, ph, &conv_params, mbmi->interp_filters, |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6451 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6452 | &warp_types, p_col, p_row, plane, !id, |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 6453 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6454 | MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
Zoe Liu | 76fcff7 | 2017-04-24 17:50:53 -0700 | [diff] [blame] | 6455 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6456 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 6457 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6458 | |
| 6459 | // Do compound motion search on the current reference frame. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 6460 | if (id) xd->plane[plane].pre[0] = ref_yv12[id]; |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6461 | 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] | 6462 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6463 | // Use the mv result from the single mode as mv predictor. |
| 6464 | // Use the mv result from the single mode as mv predictor. |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6465 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6466 | if (!has_second_ref(mbmi) && id) |
| 6467 | *best_mv = frame_comp_mv[refs[0]].as_mv; |
| 6468 | else |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6469 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6470 | *best_mv = frame_mv[refs[id]].as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6471 | |
| 6472 | best_mv->col >>= 3; |
| 6473 | best_mv->row >>= 3; |
| 6474 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6475 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6476 | if (!has_second_ref(mbmi)) |
| 6477 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
| 6478 | else |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6479 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6480 | av1_set_mvcost(x, refs[id], id, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6481 | |
| 6482 | // Small-range full-pixel motion search. |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6483 | bestsme = av1_refining_search_8p_c(x, sadpb, search_range, |
| 6484 | &cpi->fn_ptr[bsize], mask, mask_stride, |
| 6485 | id, &ref_mv[id].as_mv, second_pred); |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6486 | if (bestsme < INT_MAX) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6487 | if (mask) |
| 6488 | bestsme = av1_get_mvpred_mask_var(x, best_mv, &ref_mv[id].as_mv, |
| 6489 | second_pred, mask, mask_stride, id, |
| 6490 | &cpi->fn_ptr[bsize], 1); |
| 6491 | else |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 6492 | bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv[id].as_mv, |
| 6493 | second_pred, &cpi->fn_ptr[bsize], 1); |
| 6494 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6495 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6496 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6497 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6498 | #if CONFIG_AMVR |
| 6499 | if (cpi->common.cur_frame_mv_precision_level) { |
| 6500 | x->best_mv.as_mv.row *= 8; |
| 6501 | x->best_mv.as_mv.col *= 8; |
| 6502 | } |
| 6503 | if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) { |
| 6504 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6505 | if (bestsme < INT_MAX) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6506 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6507 | int dis; /* TODO: use dis in distortion calculation later. */ |
| 6508 | unsigned int sse; |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 6509 | bestsme = cpi->find_fractional_mv_step( |
| 6510 | x, &ref_mv[id].as_mv, cpi->common.allow_high_precision_mv, |
| 6511 | x->errorperbit, &cpi->fn_ptr[bsize], 0, |
| 6512 | cpi->sf.mv.subpel_iters_per_step, NULL, x->nmvjointcost, x->mvcost, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6513 | &dis, &sse, second_pred, mask, mask_stride, id, pw, ph, |
| 6514 | cpi->sf.use_upsampled_references); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | // Restore the pointer to the first (possibly scaled) prediction buffer. |
Angie Chiang | e3a4c1c | 2017-02-10 16:26:49 -0800 | [diff] [blame] | 6518 | if (id) xd->plane[plane].pre[0] = ref_yv12[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6519 | |
| 6520 | if (bestsme < last_besterr[id]) { |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6521 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6522 | // NOTE: For single ref comp mode, frame_mv stores the first mv and |
| 6523 | // frame_comp_mv stores the second mv. |
| 6524 | if (!has_second_ref(mbmi) && id) |
| 6525 | frame_comp_mv[refs[0]].as_mv = *best_mv; |
| 6526 | else |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6527 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6528 | frame_mv[refs[id]].as_mv = *best_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6529 | last_besterr[id] = bestsme; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6530 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6531 | if (!has_second_ref(mbmi)) last_besterr[!id] = last_besterr[id]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6532 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6533 | } else { |
| 6534 | break; |
| 6535 | } |
| 6536 | } |
| 6537 | |
| 6538 | *rate_mv = 0; |
| 6539 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6540 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6541 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 6542 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6543 | for (ref = 0; ref < 2; ++ref) { |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6544 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6545 | if (scaled_ref_frame[ref]) { |
| 6546 | // Restore the prediction frame pointers to their unscaled versions. |
| 6547 | int i; |
| 6548 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6549 | xd->plane[i].pre[ref] = backup_yv12[ref][i]; |
| 6550 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6551 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6552 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6553 | if (!has_second_ref(mbmi)) |
| 6554 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6555 | else |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6556 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6557 | av1_set_mvcost(x, refs[ref], ref, mbmi->ref_mv_idx); |
| 6558 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6559 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6560 | if (!has_second_ref(mbmi)) { |
| 6561 | // NOTE: For single ref comp mode, i.e. !has_second_ref(mbmi) is true, the |
| 6562 | // first mv is stored in frame_mv[] and the second mv is stored in |
| 6563 | // frame_comp_mv[]. |
| 6564 | if (compound_ref0_mode(mbmi->mode) == NEWMV) // SR_NEW_NEWMV |
| 6565 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
| 6566 | &x->mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 6567 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 6568 | assert(compound_ref1_mode(mbmi->mode) == NEWMV); |
| 6569 | *rate_mv += av1_mv_bit_cost(&frame_comp_mv[refs[0]].as_mv, |
| 6570 | &x->mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 6571 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 6572 | } else { |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6573 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 6574 | #if !CONFIG_CB4X4 |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6575 | if (bsize >= BLOCK_8X8) |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6576 | #endif // !CONFIG_CB4X4 |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6577 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv, |
| 6578 | &x->mbmi_ext->ref_mvs[refs[ref]][0].as_mv, |
| 6579 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6580 | #if !CONFIG_CB4X4 |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6581 | else |
| 6582 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv, |
| 6583 | &ref_mv_sub8x8[ref]->as_mv, x->nmvjointcost, |
| 6584 | x->mvcost, MV_COST_WEIGHT); |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6585 | #endif // !CONFIG_CB4X4 |
| 6586 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6587 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6588 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6589 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6590 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6591 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6592 | if (!has_second_ref(mbmi)) { |
| 6593 | if (scaled_ref_frame[0]) { |
| 6594 | // Restore the prediction frame pointers to their unscaled versions. |
| 6595 | int i; |
| 6596 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6597 | xd->plane[i].pre[1] = backup_yv12[1][i]; |
| 6598 | } |
| 6599 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6600 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6601 | } |
| 6602 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6603 | static void estimate_ref_frame_costs( |
| 6604 | const AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id, |
| 6605 | unsigned int *ref_costs_single, |
| 6606 | #if CONFIG_EXT_COMP_REFS |
| 6607 | unsigned int (*ref_costs_comp)[TOTAL_REFS_PER_FRAME], |
| 6608 | #else |
| 6609 | unsigned int *ref_costs_comp, |
| 6610 | #endif // CONFIG_EXT_COMP_REFS |
| 6611 | aom_prob *comp_mode_p) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6612 | int seg_ref_active = |
| 6613 | segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME); |
| 6614 | if (seg_ref_active) { |
| 6615 | memset(ref_costs_single, 0, |
| 6616 | TOTAL_REFS_PER_FRAME * sizeof(*ref_costs_single)); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6617 | #if CONFIG_EXT_COMP_REFS |
| 6618 | int ref_frame; |
| 6619 | for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame) |
| 6620 | memset(ref_costs_comp[ref_frame], 0, |
| 6621 | TOTAL_REFS_PER_FRAME * sizeof((*ref_costs_comp)[0])); |
| 6622 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6623 | 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] | 6624 | #endif // CONFIG_EXT_COMP_REFS |
| 6625 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6626 | *comp_mode_p = 128; |
| 6627 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6628 | aom_prob intra_inter_p = av1_get_intra_inter_prob(cm, xd); |
| 6629 | aom_prob comp_inter_p = 128; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6630 | |
| 6631 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6632 | comp_inter_p = av1_get_reference_mode_prob(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6633 | *comp_mode_p = comp_inter_p; |
| 6634 | } else { |
| 6635 | *comp_mode_p = 128; |
| 6636 | } |
| 6637 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6638 | ref_costs_single[INTRA_FRAME] = av1_cost_bit(intra_inter_p, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6639 | |
| 6640 | if (cm->reference_mode != COMPOUND_REFERENCE) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6641 | aom_prob ref_single_p1 = av1_get_pred_prob_single_ref_p1(cm, xd); |
| 6642 | 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] | 6643 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6644 | aom_prob ref_single_p3 = av1_get_pred_prob_single_ref_p3(cm, xd); |
| 6645 | aom_prob ref_single_p4 = av1_get_pred_prob_single_ref_p4(cm, xd); |
| 6646 | 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] | 6647 | 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] | 6648 | #endif // CONFIG_EXT_REFS |
| 6649 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6650 | unsigned int base_cost = av1_cost_bit(intra_inter_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6651 | |
| 6652 | ref_costs_single[LAST_FRAME] = |
| 6653 | #if CONFIG_EXT_REFS |
| 6654 | ref_costs_single[LAST2_FRAME] = ref_costs_single[LAST3_FRAME] = |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6655 | ref_costs_single[BWDREF_FRAME] = ref_costs_single[ALTREF2_FRAME] = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6656 | #endif // CONFIG_EXT_REFS |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6657 | ref_costs_single[GOLDEN_FRAME] = |
| 6658 | ref_costs_single[ALTREF_FRAME] = base_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6659 | |
| 6660 | #if CONFIG_EXT_REFS |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6661 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6662 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6663 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6664 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6665 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6666 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6667 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6668 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6669 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p3, 0); |
| 6670 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p3, 0); |
| 6671 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p3, 1); |
| 6672 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p3, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6673 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6674 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p2, 0); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6675 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p2, 0); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6676 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6677 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6678 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p4, 0); |
| 6679 | ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p4, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6680 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6681 | ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p5, 0); |
| 6682 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p5, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6683 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6684 | ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p6, 0); |
| 6685 | ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p6, 1); |
Zoe Liu | 7b1ec7a | 2017-05-24 22:28:24 -0700 | [diff] [blame] | 6686 | #else // !CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6687 | ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0); |
| 6688 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 1); |
| 6689 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6690 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6691 | ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p2, 0); |
| 6692 | ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6693 | #endif // CONFIG_EXT_REFS |
| 6694 | } else { |
| 6695 | ref_costs_single[LAST_FRAME] = 512; |
| 6696 | #if CONFIG_EXT_REFS |
| 6697 | ref_costs_single[LAST2_FRAME] = 512; |
| 6698 | ref_costs_single[LAST3_FRAME] = 512; |
| 6699 | ref_costs_single[BWDREF_FRAME] = 512; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6700 | ref_costs_single[ALTREF2_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6701 | #endif // CONFIG_EXT_REFS |
| 6702 | ref_costs_single[GOLDEN_FRAME] = 512; |
| 6703 | ref_costs_single[ALTREF_FRAME] = 512; |
| 6704 | } |
| 6705 | |
| 6706 | if (cm->reference_mode != SINGLE_REFERENCE) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6707 | 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] | 6708 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6709 | aom_prob ref_comp_p1 = av1_get_pred_prob_comp_ref_p1(cm, xd); |
| 6710 | aom_prob ref_comp_p2 = av1_get_pred_prob_comp_ref_p2(cm, xd); |
| 6711 | 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] | 6712 | 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] | 6713 | #endif // CONFIG_EXT_REFS |
| 6714 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6715 | unsigned int base_cost = av1_cost_bit(intra_inter_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6716 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6717 | #if CONFIG_EXT_COMP_REFS |
| 6718 | aom_prob comp_ref_type_p = av1_get_comp_reference_type_prob(cm, xd); |
| 6719 | unsigned int ref_bicomp_costs[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 6720 | |
| 6721 | ref_bicomp_costs[LAST_FRAME] = ref_bicomp_costs[LAST2_FRAME] = |
| 6722 | ref_bicomp_costs[LAST3_FRAME] = ref_bicomp_costs[GOLDEN_FRAME] = |
| 6723 | #if USE_UNI_COMP_REFS |
| 6724 | base_cost + av1_cost_bit(comp_ref_type_p, 1); |
| 6725 | #else |
| 6726 | base_cost; |
| 6727 | #endif // USE_UNI_COMP_REFS |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6728 | ref_bicomp_costs[BWDREF_FRAME] = ref_bicomp_costs[ALTREF2_FRAME] = 0; |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6729 | ref_bicomp_costs[ALTREF_FRAME] = 0; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6730 | |
| 6731 | ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6732 | ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6733 | ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6734 | ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6735 | |
| 6736 | ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1); |
| 6737 | ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0); |
| 6738 | |
| 6739 | ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0); |
| 6740 | ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1); |
| 6741 | |
| 6742 | ref_bicomp_costs[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6743 | ref_bicomp_costs[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6744 | ref_bicomp_costs[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1); |
| 6745 | |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6746 | ref_bicomp_costs[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p1, 0); |
| 6747 | ref_bicomp_costs[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p1, 1); |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6748 | |
| 6749 | int ref0, ref1; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6750 | for (ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) { |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6751 | for (ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) { |
| 6752 | ref_costs_comp[ref0][ref1] = |
| 6753 | ref_bicomp_costs[ref0] + ref_bicomp_costs[ref1]; |
| 6754 | } |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6755 | } |
| 6756 | |
| 6757 | aom_prob uni_comp_ref_p = av1_get_pred_prob_uni_comp_ref_p(cm, xd); |
| 6758 | 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] | 6759 | 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] | 6760 | |
| 6761 | ref_costs_comp[LAST_FRAME][LAST2_FRAME] = |
| 6762 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6763 | 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] | 6764 | ref_costs_comp[LAST_FRAME][LAST3_FRAME] = |
| 6765 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6766 | av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 1) + |
| 6767 | av1_cost_bit(uni_comp_ref_p2, 0); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6768 | ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = |
| 6769 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6770 | av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 1) + |
| 6771 | av1_cost_bit(uni_comp_ref_p2, 1); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6772 | |
| 6773 | ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = |
| 6774 | base_cost + av1_cost_bit(comp_ref_type_p, 0) + |
| 6775 | av1_cost_bit(uni_comp_ref_p, 1); |
| 6776 | |
| 6777 | #else // !CONFIG_EXT_COMP_REFS |
| 6778 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6779 | ref_costs_comp[LAST_FRAME] = |
| 6780 | #if CONFIG_EXT_REFS |
| 6781 | ref_costs_comp[LAST2_FRAME] = ref_costs_comp[LAST3_FRAME] = |
| 6782 | #endif // CONFIG_EXT_REFS |
| 6783 | ref_costs_comp[GOLDEN_FRAME] = base_cost; |
| 6784 | |
| 6785 | #if CONFIG_EXT_REFS |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 6786 | ref_costs_comp[BWDREF_FRAME] = ref_costs_comp[ALTREF2_FRAME] = |
| 6787 | ref_costs_comp[ALTREF_FRAME] = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6788 | #endif // CONFIG_EXT_REFS |
| 6789 | |
| 6790 | #if CONFIG_EXT_REFS |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6791 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6792 | ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6793 | ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1); |
| 6794 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6795 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6796 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1); |
| 6797 | ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6798 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6799 | ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0); |
| 6800 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6801 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6802 | // NOTE(zoeliu): BWDREF and ALTREF each add an extra cost by coding 1 |
| 6803 | // more bit. |
| 6804 | ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6805 | ref_costs_comp[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p, 0); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6806 | ref_costs_comp[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1); |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6807 | |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6808 | ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p1, 0); |
| 6809 | ref_costs_comp[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p1, 1); |
Zoe Liu | 7b1ec7a | 2017-05-24 22:28:24 -0700 | [diff] [blame] | 6810 | #else // !CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6811 | ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0); |
| 6812 | ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6813 | #endif // CONFIG_EXT_REFS |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6814 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6815 | } else { |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6816 | #if CONFIG_EXT_COMP_REFS |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6817 | int ref0, ref1; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6818 | for (ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) { |
Zoe Liu | ac88970 | 2017-08-23 14:22:58 -0700 | [diff] [blame] | 6819 | for (ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) |
| 6820 | ref_costs_comp[ref0][ref1] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6821 | } |
| 6822 | ref_costs_comp[LAST_FRAME][LAST2_FRAME] = 512; |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 6823 | ref_costs_comp[LAST_FRAME][LAST3_FRAME] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6824 | ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = 512; |
| 6825 | ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = 512; |
| 6826 | #else // !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6827 | ref_costs_comp[LAST_FRAME] = 512; |
| 6828 | #if CONFIG_EXT_REFS |
| 6829 | ref_costs_comp[LAST2_FRAME] = 512; |
| 6830 | ref_costs_comp[LAST3_FRAME] = 512; |
| 6831 | ref_costs_comp[BWDREF_FRAME] = 512; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 6832 | ref_costs_comp[ALTREF2_FRAME] = 512; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6833 | ref_costs_comp[ALTREF_FRAME] = 512; |
| 6834 | #endif // CONFIG_EXT_REFS |
| 6835 | ref_costs_comp[GOLDEN_FRAME] = 512; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 6836 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6837 | } |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, |
| 6842 | int mode_index, |
| 6843 | int64_t comp_pred_diff[REFERENCE_MODES], |
| 6844 | int skippable) { |
| 6845 | MACROBLOCKD *const xd = &x->e_mbd; |
| 6846 | |
| 6847 | // Take a snapshot of the coding context so it can be |
| 6848 | // restored if we decide to encode this way |
| 6849 | ctx->skip = x->skip; |
| 6850 | ctx->skippable = skippable; |
| 6851 | ctx->best_mode_index = mode_index; |
| 6852 | ctx->mic = *xd->mi[0]; |
| 6853 | ctx->mbmi_ext = *x->mbmi_ext; |
| 6854 | ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE]; |
| 6855 | ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE]; |
| 6856 | ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT]; |
| 6857 | } |
| 6858 | |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 6859 | static void setup_buffer_inter( |
| 6860 | const AV1_COMP *const cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame, |
| 6861 | BLOCK_SIZE block_size, int mi_row, int mi_col, |
| 6862 | int_mv frame_nearest_mv[TOTAL_REFS_PER_FRAME], |
| 6863 | int_mv frame_near_mv[TOTAL_REFS_PER_FRAME], |
| 6864 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6865 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6866 | const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame); |
| 6867 | MACROBLOCKD *const xd = &x->e_mbd; |
| 6868 | MODE_INFO *const mi = xd->mi[0]; |
| 6869 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 6870 | const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf; |
| 6871 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 6872 | |
| 6873 | assert(yv12 != NULL); |
| 6874 | |
| 6875 | // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this |
| 6876 | // use the UV scaling factors. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6877 | 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] | 6878 | |
| 6879 | // Gets an initial list of candidate vectors from neighbours and orders them |
Sebastien Alaiwan | e140c50 | 2017-04-27 09:52:34 +0200 | [diff] [blame] | 6880 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 6881 | mbmi_ext->ref_mv_stack[ref_frame], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6882 | mbmi_ext->compound_mode_context, candidates, mi_row, mi_col, |
| 6883 | NULL, NULL, mbmi_ext->mode_context); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6884 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6885 | // Candidate refinement carried out at encoder and decoder |
| 6886 | #if CONFIG_AMVR |
| 6887 | av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates, |
| 6888 | &frame_nearest_mv[ref_frame], &frame_near_mv[ref_frame], |
| 6889 | cm->cur_frame_mv_precision_level); |
| 6890 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6891 | av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates, |
| 6892 | &frame_nearest_mv[ref_frame], |
| 6893 | &frame_near_mv[ref_frame]); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 6894 | #endif |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 6895 | // Further refinement that is encode side only to test the top few candidates |
| 6896 | // in full and choose the best as the centre point for subsequent searches. |
| 6897 | // The current implementation doesn't support scaling. |
| 6898 | #if CONFIG_CB4X4 |
| 6899 | av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame, |
| 6900 | block_size); |
| 6901 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6902 | if (!av1_is_scaled(sf) && block_size >= BLOCK_8X8) |
| 6903 | av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame, |
| 6904 | block_size); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 6905 | #endif // CONFIG_CB4X4 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6906 | } |
| 6907 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 6908 | static void single_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 6909 | BLOCK_SIZE bsize, int mi_row, int mi_col, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 6910 | int ref_idx, int *rate_mv) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6911 | MACROBLOCKD *xd = &x->e_mbd; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6912 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6913 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 6914 | struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } }; |
| 6915 | int bestsme = INT_MAX; |
| 6916 | int step_param; |
| 6917 | int sadpb = x->sadperbit16; |
| 6918 | MV mvp_full; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6919 | #if CONFIG_COMPOUND_SINGLEREF |
| 6920 | int ref = |
| 6921 | has_second_ref(mbmi) ? mbmi->ref_frame[ref_idx] : mbmi->ref_frame[0]; |
| 6922 | #else // !CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6923 | int ref = mbmi->ref_frame[ref_idx]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 6924 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 6925 | MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6926 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6927 | MvLimits tmp_mv_limits = x->mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6928 | int cost_list[5]; |
| 6929 | |
| 6930 | const YV12_BUFFER_CONFIG *scaled_ref_frame = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6931 | av1_get_scaled_ref_frame(cpi, ref); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6932 | |
| 6933 | MV pred_mv[3]; |
| 6934 | pred_mv[0] = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 6935 | pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv; |
| 6936 | pred_mv[2] = x->pred_mv[ref]; |
| 6937 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6938 | if (scaled_ref_frame) { |
| 6939 | int i; |
| 6940 | // Swap out the reference frame for a version that's been scaled to |
| 6941 | // match the resolution of the current frame, allowing the existing |
| 6942 | // motion search code to be used without additional modifications. |
| 6943 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 6944 | backup_yv12[i] = xd->plane[i].pre[ref_idx]; |
| 6945 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6946 | 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] | 6947 | } |
| 6948 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 6949 | av1_set_mv_search_range(&x->mv_limits, &ref_mv); |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6950 | |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6951 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 6952 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6953 | // 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] | 6954 | // 0 here is maximum length first step. 1 is AOMMAX >> 1 etc. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6955 | if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) { |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 6956 | // 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] | 6957 | // max mv magnitude and that based on the best ref mvs of the current |
| 6958 | // block for the given reference. |
| 6959 | step_param = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6960 | (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] | 6961 | 2; |
| 6962 | } else { |
| 6963 | step_param = cpi->mv_step_param; |
| 6964 | } |
| 6965 | |
| 6966 | if (cpi->sf.adaptive_motion_search && bsize < cm->sb_size) { |
| 6967 | int boffset = |
| 6968 | 2 * (b_width_log2_lookup[cm->sb_size] - |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 6969 | AOMMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize])); |
| 6970 | step_param = AOMMAX(step_param, boffset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6971 | } |
| 6972 | |
| 6973 | if (cpi->sf.adaptive_motion_search) { |
| 6974 | int bwl = b_width_log2_lookup[bsize]; |
| 6975 | int bhl = b_height_log2_lookup[bsize]; |
| 6976 | int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4); |
| 6977 | |
| 6978 | if (tlevel < 5) step_param += 2; |
| 6979 | |
| 6980 | // prev_mv_sad is not setup for dynamically scaled frames. |
Debargha Mukherjee | 7166f22 | 2017-09-05 21:32:42 -0700 | [diff] [blame] | 6981 | if (cpi->oxcf.resize_mode != RESIZE_RANDOM) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6982 | int i; |
| 6983 | for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) { |
| 6984 | if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) { |
| 6985 | x->pred_mv[ref].row = 0; |
| 6986 | x->pred_mv[ref].col = 0; |
| 6987 | x->best_mv.as_int = INVALID_MV; |
| 6988 | |
| 6989 | if (scaled_ref_frame) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 6990 | int j; |
| 6991 | for (j = 0; j < MAX_MB_PLANE; ++j) |
| 6992 | xd->plane[j].pre[ref_idx] = backup_yv12[j]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 6993 | } |
| 6994 | return; |
| 6995 | } |
| 6996 | } |
| 6997 | } |
| 6998 | } |
| 6999 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7000 | av1_set_mv_search_range(&x->mv_limits, &ref_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7001 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7002 | #if CONFIG_MOTION_VAR |
| 7003 | if (mbmi->motion_mode != SIMPLE_TRANSLATION) |
| 7004 | mvp_full = mbmi->mv[0].as_mv; |
| 7005 | else |
| 7006 | #endif // CONFIG_MOTION_VAR |
| 7007 | mvp_full = pred_mv[x->mv_best_ref_index[ref]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7008 | |
| 7009 | mvp_full.col >>= 3; |
| 7010 | mvp_full.row >>= 3; |
| 7011 | |
| 7012 | x->best_mv.as_int = x->second_best_mv.as_int = INVALID_MV; |
| 7013 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7014 | #if CONFIG_MOTION_VAR |
| 7015 | switch (mbmi->motion_mode) { |
| 7016 | case SIMPLE_TRANSLATION: |
| 7017 | #endif // CONFIG_MOTION_VAR |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 7018 | #if CONFIG_HASH_ME |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7019 | bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, |
| 7020 | sadpb, cond_cost_list(cpi, cost_list), |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 7021 | &ref_mv, INT_MAX, 1, (MI_SIZE * mi_col), |
RogerZhou | d15e7c1 | 2017-09-26 08:49:28 -0700 | [diff] [blame] | 7022 | (MI_SIZE * mi_row), 0); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 7023 | #else |
| 7024 | bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb, |
| 7025 | cond_cost_list(cpi, cost_list), &ref_mv, |
| 7026 | INT_MAX, 1); |
| 7027 | #endif |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7028 | #if CONFIG_MOTION_VAR |
| 7029 | break; |
| 7030 | case OBMC_CAUSAL: |
| 7031 | bestsme = av1_obmc_full_pixel_diamond( |
| 7032 | cpi, x, &mvp_full, step_param, sadpb, |
| 7033 | MAX_MVSEARCH_STEPS - 1 - step_param, 1, &cpi->fn_ptr[bsize], &ref_mv, |
| 7034 | &(x->best_mv.as_mv), 0); |
| 7035 | break; |
James Zern | 8889673 | 2017-06-23 15:55:09 -0700 | [diff] [blame] | 7036 | default: assert(0 && "Invalid motion mode!\n"); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7037 | } |
| 7038 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7039 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7040 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7041 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7042 | #if CONFIG_AMVR |
| 7043 | if (cpi->common.cur_frame_mv_precision_level) { |
| 7044 | x->best_mv.as_mv.row *= 8; |
| 7045 | x->best_mv.as_mv.col *= 8; |
| 7046 | } |
| 7047 | if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) { |
| 7048 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7049 | if (bestsme < INT_MAX) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7050 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7051 | int dis; /* TODO: use dis in distortion calculation later. */ |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7052 | #if CONFIG_MOTION_VAR |
| 7053 | switch (mbmi->motion_mode) { |
| 7054 | case SIMPLE_TRANSLATION: |
| 7055 | #endif // CONFIG_MOTION_VAR |
| 7056 | if (cpi->sf.use_upsampled_references) { |
| 7057 | int best_mv_var; |
| 7058 | const int try_second = x->second_best_mv.as_int != INVALID_MV && |
| 7059 | x->second_best_mv.as_int != x->best_mv.as_int; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7060 | const int pw = block_size_wide[bsize]; |
| 7061 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7062 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7063 | best_mv_var = cpi->find_fractional_mv_step( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7064 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 7065 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 7066 | cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list), |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 7067 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, NULL, |
| 7068 | 0, 0, pw, ph, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7069 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7070 | if (try_second) { |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7071 | const int minc = |
| 7072 | AOMMAX(x->mv_limits.col_min * 8, ref_mv.col - MV_MAX); |
| 7073 | const int maxc = |
| 7074 | AOMMIN(x->mv_limits.col_max * 8, ref_mv.col + MV_MAX); |
| 7075 | const int minr = |
| 7076 | AOMMAX(x->mv_limits.row_min * 8, ref_mv.row - MV_MAX); |
| 7077 | const int maxr = |
| 7078 | AOMMIN(x->mv_limits.row_max * 8, ref_mv.row + MV_MAX); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7079 | int this_var; |
| 7080 | MV best_mv = x->best_mv.as_mv; |
| 7081 | |
| 7082 | x->best_mv = x->second_best_mv; |
| 7083 | if (x->best_mv.as_mv.row * 8 <= maxr && |
| 7084 | x->best_mv.as_mv.row * 8 >= minr && |
| 7085 | x->best_mv.as_mv.col * 8 <= maxc && |
| 7086 | x->best_mv.as_mv.col * 8 >= minc) { |
| 7087 | this_var = cpi->find_fractional_mv_step( |
| 7088 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 7089 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 7090 | cpi->sf.mv.subpel_iters_per_step, |
| 7091 | cond_cost_list(cpi, cost_list), x->nmvjointcost, x->mvcost, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 7092 | &dis, &x->pred_sse[ref], NULL, NULL, 0, 0, pw, ph, 1); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7093 | if (this_var < best_mv_var) best_mv = x->best_mv.as_mv; |
| 7094 | x->best_mv.as_mv = best_mv; |
| 7095 | } |
| 7096 | } |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7097 | } else { |
| 7098 | cpi->find_fractional_mv_step( |
| 7099 | x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit, |
| 7100 | &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 7101 | cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list), |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 7102 | x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL, NULL, |
| 7103 | 0, 0, 0, 0, 0); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7104 | } |
| 7105 | #if CONFIG_MOTION_VAR |
| 7106 | break; |
| 7107 | case OBMC_CAUSAL: |
| 7108 | av1_find_best_obmc_sub_pixel_tree_up( |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 7109 | x, &x->best_mv.as_mv, &ref_mv, cm->allow_high_precision_mv, |
| 7110 | x->errorperbit, &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop, |
| 7111 | cpi->sf.mv.subpel_iters_per_step, x->nmvjointcost, x->mvcost, &dis, |
| 7112 | &x->pred_sse[ref], 0, cpi->sf.use_upsampled_references); |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7113 | break; |
James Zern | 8889673 | 2017-06-23 15:55:09 -0700 | [diff] [blame] | 7114 | default: assert(0 && "Invalid motion mode!\n"); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7115 | } |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7116 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7117 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7118 | *rate_mv = av1_mv_bit_cost(&x->best_mv.as_mv, &ref_mv, x->nmvjointcost, |
| 7119 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7120 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 7121 | #if CONFIG_MOTION_VAR |
| 7122 | if (cpi->sf.adaptive_motion_search && mbmi->motion_mode == SIMPLE_TRANSLATION) |
| 7123 | #else |
| 7124 | if (cpi->sf.adaptive_motion_search) |
| 7125 | #endif // CONFIG_MOTION_VAR |
| 7126 | x->pred_mv[ref] = x->best_mv.as_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7127 | |
| 7128 | if (scaled_ref_frame) { |
| 7129 | int i; |
| 7130 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7131 | xd->plane[i].pre[ref_idx] = backup_yv12[i]; |
| 7132 | } |
| 7133 | } |
| 7134 | |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 7135 | static INLINE void restore_dst_buf(MACROBLOCKD *xd, BUFFER_SET dst) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7136 | int i; |
| 7137 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 7138 | xd->plane[i].dst.buf = dst.plane[i]; |
| 7139 | xd->plane[i].dst.stride = dst.stride[i]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7140 | } |
| 7141 | } |
| 7142 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7143 | static void build_second_inter_pred(const AV1_COMP *cpi, MACROBLOCK *x, |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7144 | BLOCK_SIZE bsize, const MV *other_mv, |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7145 | int mi_row, int mi_col, const int block, |
| 7146 | int ref_idx, uint8_t *second_pred) { |
| 7147 | const AV1_COMMON *const cm = &cpi->common; |
| 7148 | const int pw = block_size_wide[bsize]; |
| 7149 | const int ph = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7150 | MACROBLOCKD *xd = &x->e_mbd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7151 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7152 | #if CONFIG_COMPOUND_SINGLEREF |
| 7153 | const int other_ref = |
| 7154 | has_second_ref(mbmi) ? mbmi->ref_frame[!ref_idx] : mbmi->ref_frame[0]; |
| 7155 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7156 | const int other_ref = mbmi->ref_frame[!ref_idx]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7157 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7158 | struct scale_factors sf; |
| 7159 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7160 | struct macroblockd_plane *const pd = &xd->plane[0]; |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 7161 | // 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] | 7162 | const int ic = block & 1; |
| 7163 | const int ir = (block - ic) >> 1; |
| 7164 | const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x) + 4 * ic; |
| 7165 | const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y) + 4 * ir; |
| 7166 | #if CONFIG_GLOBAL_MOTION |
| 7167 | WarpedMotionParams *const wm = &xd->global_motion[other_ref]; |
| 7168 | int is_global = is_global_mv_block(xd->mi[0], block, wm->wmtype); |
| 7169 | #endif // CONFIG_GLOBAL_MOTION |
| 7170 | #else |
| 7171 | (void)block; |
| 7172 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7173 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7174 | // This function should only ever be called for compound modes |
| 7175 | #if CONFIG_COMPOUND_SINGLEREF |
| 7176 | assert(has_second_ref(mbmi) || is_inter_singleref_comp_mode(mbmi->mode)); |
| 7177 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7178 | assert(has_second_ref(mbmi)); |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7179 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7180 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7181 | struct buf_2d backup_yv12[MAX_MB_PLANE]; |
| 7182 | const YV12_BUFFER_CONFIG *const scaled_ref_frame = |
| 7183 | av1_get_scaled_ref_frame(cpi, other_ref); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7184 | |
| 7185 | if (scaled_ref_frame) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7186 | int i; |
| 7187 | // Swap out the reference frame for a version that's been scaled to |
| 7188 | // match the resolution of the current frame, allowing the existing |
| 7189 | // motion search code to be used without additional modifications. |
| 7190 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7191 | backup_yv12[i] = xd->plane[i].pre[!ref_idx]; |
| 7192 | av1_setup_pre_planes(xd, !ref_idx, scaled_ref_frame, mi_row, mi_col, NULL); |
| 7193 | } |
| 7194 | |
| 7195 | // Since we have scaled the reference frames to match the size of the current |
| 7196 | // frame we must use a unit scaling factor during mode selection. |
| 7197 | #if CONFIG_HIGHBITDEPTH |
| 7198 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 7199 | cm->height, cm->use_highbitdepth); |
| 7200 | #else |
| 7201 | av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width, |
| 7202 | cm->height); |
| 7203 | #endif // CONFIG_HIGHBITDEPTH |
| 7204 | |
| 7205 | struct buf_2d ref_yv12; |
| 7206 | |
| 7207 | const int plane = 0; |
David Barker | e64d51a | 2017-06-09 14:52:42 +0100 | [diff] [blame] | 7208 | ConvolveParams conv_params = get_conv_params(!ref_idx, 0, plane); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7209 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7210 | WarpTypesAllowed warp_types; |
| 7211 | #if CONFIG_GLOBAL_MOTION |
| 7212 | warp_types.global_warp_allowed = is_global; |
| 7213 | #endif // CONFIG_GLOBAL_MOTION |
| 7214 | #if CONFIG_WARPED_MOTION |
| 7215 | warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL; |
| 7216 | #endif // CONFIG_WARPED_MOTION |
| 7217 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7218 | |
| 7219 | // Initialized here because of compiler problem in Visual Studio. |
| 7220 | ref_yv12 = xd->plane[plane].pre[!ref_idx]; |
| 7221 | |
| 7222 | // Get the prediction block from the 'other' reference frame. |
| 7223 | #if CONFIG_HIGHBITDEPTH |
| 7224 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 7225 | av1_highbd_build_inter_predictor( |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7226 | ref_yv12.buf, ref_yv12.stride, second_pred, pw, other_mv, &sf, pw, ph, |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7227 | 0, mbmi->interp_filters, |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7228 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7229 | &warp_types, p_col, p_row, |
| 7230 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7231 | plane, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
| 7232 | } else { |
| 7233 | #endif // CONFIG_HIGHBITDEPTH |
| 7234 | av1_build_inter_predictor( |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7235 | ref_yv12.buf, ref_yv12.stride, second_pred, pw, other_mv, &sf, pw, ph, |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7236 | &conv_params, mbmi->interp_filters, |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7237 | #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7238 | &warp_types, p_col, p_row, plane, !ref_idx, |
| 7239 | #endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION |
| 7240 | MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd); |
| 7241 | #if CONFIG_HIGHBITDEPTH |
| 7242 | } |
| 7243 | #endif // CONFIG_HIGHBITDEPTH |
| 7244 | |
| 7245 | if (scaled_ref_frame) { |
| 7246 | // Restore the prediction frame pointers to their unscaled versions. |
| 7247 | int i; |
| 7248 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7249 | xd->plane[i].pre[!ref_idx] = backup_yv12[i]; |
| 7250 | } |
| 7251 | } |
| 7252 | |
| 7253 | // Search for the best mv for one component of a compound, |
| 7254 | // given that the other component is fixed. |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 7255 | static void compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x, |
| 7256 | BLOCK_SIZE bsize, MV *this_mv, |
| 7257 | int mi_row, int mi_col, |
| 7258 | const uint8_t *second_pred, |
| 7259 | const uint8_t *mask, int mask_stride, |
| 7260 | int *rate_mv, int ref_idx) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7261 | const int pw = block_size_wide[bsize]; |
| 7262 | const int ph = block_size_high[bsize]; |
| 7263 | MACROBLOCKD *xd = &x->e_mbd; |
| 7264 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7265 | #if CONFIG_COMPOUND_SINGLEREF |
| 7266 | const int ref = |
| 7267 | has_second_ref(mbmi) ? mbmi->ref_frame[ref_idx] : mbmi->ref_frame[0]; |
| 7268 | #else |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7269 | const int ref = mbmi->ref_frame[ref_idx]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7270 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7271 | int_mv ref_mv = x->mbmi_ext->ref_mvs[ref][0]; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7272 | struct macroblockd_plane *const pd = &xd->plane[0]; |
| 7273 | |
| 7274 | struct buf_2d backup_yv12[MAX_MB_PLANE]; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7275 | const YV12_BUFFER_CONFIG *const scaled_ref_frame = |
| 7276 | av1_get_scaled_ref_frame(cpi, ref); |
| 7277 | |
Ryan Lei | 1d1df18 | 2017-06-15 11:38:59 -0700 | [diff] [blame] | 7278 | // Check that this is either an interinter or an interintra block |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7279 | #if CONFIG_COMPOUND_SINGLEREF |
Ryan Lei | 1d1df18 | 2017-06-15 11:38:59 -0700 | [diff] [blame] | 7280 | assert(has_second_ref(mbmi) || |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7281 | // or a single ref comp pred mode |
| 7282 | is_inter_singleref_comp_mode(mbmi->mode) || |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7283 | (ref_idx == 0 && mbmi->ref_frame[1] == INTRA_FRAME)); |
Ryan Lei | 1d1df18 | 2017-06-15 11:38:59 -0700 | [diff] [blame] | 7284 | #else |
| 7285 | assert(has_second_ref(mbmi) || |
| 7286 | (ref_idx == 0 && mbmi->ref_frame[1] == INTRA_FRAME)); |
| 7287 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7288 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7289 | if (scaled_ref_frame) { |
| 7290 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7291 | // Swap out the reference frame for a version that's been scaled to |
| 7292 | // match the resolution of the current frame, allowing the existing |
| 7293 | // motion search code to be used without additional modifications. |
| 7294 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7295 | backup_yv12[i] = xd->plane[i].pre[ref_idx]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7296 | 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] | 7297 | } |
| 7298 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7299 | struct buf_2d orig_yv12; |
| 7300 | int bestsme = INT_MAX; |
| 7301 | int sadpb = x->sadperbit16; |
| 7302 | MV *const best_mv = &x->best_mv.as_mv; |
| 7303 | int search_range = 3; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7304 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7305 | MvLimits tmp_mv_limits = x->mv_limits; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7306 | |
| 7307 | // Initialized here because of compiler problem in Visual Studio. |
| 7308 | if (ref_idx) { |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7309 | orig_yv12 = pd->pre[0]; |
| 7310 | pd->pre[0] = pd->pre[ref_idx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7311 | } |
| 7312 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7313 | // Do compound motion search on the current reference frame. |
| 7314 | av1_set_mv_search_range(&x->mv_limits, &ref_mv.as_mv); |
| 7315 | |
| 7316 | // Use the mv result from the single mode as mv predictor. |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7317 | *best_mv = *this_mv; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7318 | |
| 7319 | best_mv->col >>= 3; |
| 7320 | best_mv->row >>= 3; |
| 7321 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7322 | #if CONFIG_COMPOUND_SINGLEREF |
| 7323 | if (!has_second_ref(mbmi)) |
| 7324 | av1_set_mvcost(x, ref, 0, mbmi->ref_mv_idx); |
| 7325 | else |
| 7326 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7327 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7328 | |
| 7329 | // Small-range full-pixel motion search. |
| 7330 | bestsme = av1_refining_search_8p_c(x, sadpb, search_range, |
| 7331 | &cpi->fn_ptr[bsize], mask, mask_stride, |
| 7332 | ref_idx, &ref_mv.as_mv, second_pred); |
| 7333 | if (bestsme < INT_MAX) { |
| 7334 | if (mask) |
| 7335 | bestsme = |
| 7336 | av1_get_mvpred_mask_var(x, best_mv, &ref_mv.as_mv, second_pred, mask, |
| 7337 | mask_stride, ref_idx, &cpi->fn_ptr[bsize], 1); |
| 7338 | else |
| 7339 | bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv.as_mv, second_pred, |
| 7340 | &cpi->fn_ptr[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7341 | } |
| 7342 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 7343 | x->mv_limits = tmp_mv_limits; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7344 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7345 | #if CONFIG_AMVR |
| 7346 | if (cpi->common.cur_frame_mv_precision_level) { |
| 7347 | x->best_mv.as_mv.row *= 8; |
| 7348 | x->best_mv.as_mv.col *= 8; |
| 7349 | } |
| 7350 | if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) { |
| 7351 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7352 | if (bestsme < INT_MAX) { |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 7353 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7354 | int dis; /* TODO: use dis in distortion calculation later. */ |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7355 | unsigned int sse; |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 7356 | bestsme = cpi->find_fractional_mv_step( |
| 7357 | x, &ref_mv.as_mv, cpi->common.allow_high_precision_mv, x->errorperbit, |
| 7358 | &cpi->fn_ptr[bsize], 0, cpi->sf.mv.subpel_iters_per_step, NULL, |
| 7359 | x->nmvjointcost, x->mvcost, &dis, &sse, second_pred, mask, mask_stride, |
| 7360 | ref_idx, pw, ph, cpi->sf.use_upsampled_references); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7361 | } |
| 7362 | |
| 7363 | // Restore the pointer to the first (possibly scaled) prediction buffer. |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7364 | if (ref_idx) pd->pre[0] = orig_yv12; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7365 | |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 7366 | if (bestsme < INT_MAX) *this_mv = *best_mv; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7367 | |
| 7368 | *rate_mv = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7369 | |
| 7370 | if (scaled_ref_frame) { |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7371 | // Restore the prediction frame pointers to their unscaled versions. |
| 7372 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7373 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 7374 | xd->plane[i].pre[ref_idx] = backup_yv12[i]; |
| 7375 | } |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7376 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7377 | #if CONFIG_COMPOUND_SINGLEREF |
| 7378 | if (!has_second_ref(mbmi)) |
| 7379 | av1_set_mvcost(x, ref, 0, mbmi->ref_mv_idx); |
| 7380 | else |
| 7381 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7382 | av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7383 | *rate_mv += av1_mv_bit_cost(this_mv, &ref_mv.as_mv, x->nmvjointcost, |
| 7384 | x->mvcost, MV_COST_WEIGHT); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7385 | } |
| 7386 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7387 | // Wrapper for compound_single_motion_search, for the common case |
| 7388 | // where the second prediction is also an inter mode. |
| 7389 | static void compound_single_motion_search_interinter( |
| 7390 | 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] | 7391 | #if CONFIG_COMPOUND_SINGLEREF |
| 7392 | int_mv *frame_comp_mv, |
| 7393 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7394 | int mi_row, int mi_col, const uint8_t *mask, int mask_stride, int *rate_mv, |
| 7395 | const int block, int ref_idx) { |
| 7396 | MACROBLOCKD *xd = &x->e_mbd; |
| 7397 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 7398 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7399 | // This function should only ever be called for compound modes |
| 7400 | #if CONFIG_COMPOUND_SINGLEREF |
| 7401 | int is_singleref_comp_mode = |
| 7402 | !has_second_ref(mbmi) && is_inter_singleref_comp_mode(mbmi->mode); |
| 7403 | assert(has_second_ref(mbmi) || is_singleref_comp_mode); |
| 7404 | if (is_singleref_comp_mode && ref_idx) assert(frame_comp_mv); |
| 7405 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7406 | assert(has_second_ref(mbmi)); |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7407 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7408 | |
| 7409 | // Prediction buffer from second frame. |
| 7410 | #if CONFIG_HIGHBITDEPTH |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7411 | DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]); |
| 7412 | uint8_t *second_pred; |
| 7413 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 7414 | second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16); |
| 7415 | else |
| 7416 | second_pred = (uint8_t *)second_pred_alloc_16; |
| 7417 | #else |
| 7418 | DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]); |
| 7419 | #endif // CONFIG_HIGHBITDEPTH |
| 7420 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7421 | #if CONFIG_COMPOUND_SINGLEREF |
| 7422 | MV *this_mv = has_second_ref(mbmi) |
| 7423 | ? &frame_mv[mbmi->ref_frame[ref_idx]].as_mv |
| 7424 | : (ref_idx ? &frame_comp_mv[mbmi->ref_frame[0]].as_mv |
| 7425 | : &frame_mv[mbmi->ref_frame[0]].as_mv); |
| 7426 | const MV *other_mv = |
| 7427 | has_second_ref(mbmi) |
| 7428 | ? &frame_mv[mbmi->ref_frame[!ref_idx]].as_mv |
| 7429 | : (ref_idx ? &frame_mv[mbmi->ref_frame[0]].as_mv |
| 7430 | : &frame_comp_mv[mbmi->ref_frame[0]].as_mv); |
| 7431 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7432 | MV *this_mv = &frame_mv[mbmi->ref_frame[ref_idx]].as_mv; |
| 7433 | 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] | 7434 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7435 | |
| 7436 | 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] | 7437 | ref_idx, second_pred); |
| 7438 | |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7439 | 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] | 7440 | second_pred, mask, mask_stride, rate_mv, |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7441 | ref_idx); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7442 | } |
| 7443 | |
| 7444 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7445 | static void do_masked_motion_search_indexed( |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7446 | 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] | 7447 | const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE bsize, |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7448 | 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] | 7449 | // NOTE: which values: 0 - 0 only, 1 - 1 only, 2 - both |
| 7450 | MACROBLOCKD *xd = &x->e_mbd; |
| 7451 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 7452 | BLOCK_SIZE sb_type = mbmi->sb_type; |
| 7453 | const uint8_t *mask; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7454 | const int mask_stride = block_size_wide[bsize]; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7455 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7456 | mask = av1_get_compound_type_mask(comp_data, sb_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7457 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7458 | int_mv frame_mv[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7459 | #if CONFIG_COMPOUND_SINGLEREF |
| 7460 | int_mv frame_comp_mv[TOTAL_REFS_PER_FRAME]; |
| 7461 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7462 | MV_REFERENCE_FRAME rf[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] }; |
| 7463 | assert(bsize >= BLOCK_8X8 || CONFIG_CB4X4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7464 | |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7465 | frame_mv[rf[0]].as_int = cur_mv[0].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7466 | #if CONFIG_COMPOUND_SINGLEREF |
| 7467 | if (!has_second_ref(mbmi)) |
| 7468 | frame_comp_mv[rf[0]].as_int = cur_mv[1].as_int; |
| 7469 | else |
| 7470 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7471 | frame_mv[rf[1]].as_int = cur_mv[1].as_int; |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7472 | if (which == 0 || which == 1) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7473 | compound_single_motion_search_interinter( |
| 7474 | cpi, x, bsize, frame_mv, |
| 7475 | #if CONFIG_COMPOUND_SINGLEREF |
| 7476 | has_second_ref(mbmi) ? NULL : frame_comp_mv, |
| 7477 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7478 | mi_row, mi_col, mask, mask_stride, rate_mv, 0, which); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 7479 | } else if (which == 2) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7480 | joint_motion_search(cpi, x, bsize, frame_mv, |
| 7481 | #if CONFIG_COMPOUND_SINGLEREF |
| 7482 | has_second_ref(mbmi) ? NULL : frame_comp_mv, |
| 7483 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7484 | mi_row, mi_col, NULL, mask, mask_stride, rate_mv, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7485 | } |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 7486 | tmp_mv[0].as_int = frame_mv[rf[0]].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7487 | #if CONFIG_COMPOUND_SINGLEREF |
| 7488 | if (!has_second_ref(mbmi)) |
| 7489 | tmp_mv[1].as_int = frame_comp_mv[rf[0]].as_int; |
| 7490 | else // comp ref |
| 7491 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7492 | tmp_mv[1].as_int = frame_mv[rf[1]].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7493 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7494 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7495 | |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 7496 | // 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] | 7497 | // vector. Where there is a subtle motion field and especially where there is |
| 7498 | // low spatial complexity then it can be hard to cover the cost of a new motion |
| 7499 | // vector in a single block, even if that motion vector reduces distortion. |
| 7500 | // However, once established that vector may be usable through the nearest and |
| 7501 | // near mv modes to reduce distortion in subsequent blocks and also improve |
| 7502 | // visual quality. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 7503 | 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] | 7504 | int_mv this_mv, |
| 7505 | int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], |
| 7506 | int ref_frame) { |
| 7507 | return (!cpi->rc.is_src_frame_alt_ref && (this_mode == NEWMV) && |
| 7508 | (this_mv.as_int != 0) && |
| 7509 | ((mode_mv[NEARESTMV][ref_frame].as_int == 0) || |
| 7510 | (mode_mv[NEARESTMV][ref_frame].as_int == INVALID_MV)) && |
| 7511 | ((mode_mv[NEARMV][ref_frame].as_int == 0) || |
| 7512 | (mode_mv[NEARMV][ref_frame].as_int == INVALID_MV))); |
| 7513 | } |
| 7514 | |
Yaowu Xu | 671f2bd | 2016-09-30 15:07:57 -0700 | [diff] [blame] | 7515 | #define LEFT_TOP_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3) |
| 7516 | #define RIGHT_BOTTOM_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7517 | |
| 7518 | // TODO(jingning): this mv clamping function should be block size dependent. |
| 7519 | static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) { |
| 7520 | clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN, |
| 7521 | xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN, |
| 7522 | xd->mb_to_top_edge - LEFT_TOP_MARGIN, |
| 7523 | xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN); |
| 7524 | } |
| 7525 | |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7526 | #if CONFIG_WEDGE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7527 | static int estimate_wedge_sign(const AV1_COMP *cpi, const MACROBLOCK *x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7528 | const BLOCK_SIZE bsize, const uint8_t *pred0, |
| 7529 | int stride0, const uint8_t *pred1, int stride1) { |
| 7530 | const struct macroblock_plane *const p = &x->plane[0]; |
| 7531 | const uint8_t *src = p->src.buf; |
| 7532 | int src_stride = p->src.stride; |
| 7533 | const int f_index = bsize - BLOCK_8X8; |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7534 | const int bw = block_size_wide[bsize]; |
| 7535 | const int bh = block_size_high[bsize]; |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 7536 | uint32_t esq[2][4]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7537 | int64_t tl, br; |
| 7538 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7539 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7540 | if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 7541 | pred0 = CONVERT_TO_BYTEPTR(pred0); |
| 7542 | pred1 = CONVERT_TO_BYTEPTR(pred1); |
| 7543 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7544 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7545 | |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 7546 | cpi->fn_ptr[f_index].vf(src, src_stride, pred0, stride0, &esq[0][0]); |
| 7547 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred0 + bw / 2, stride0, |
| 7548 | &esq[0][1]); |
| 7549 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride, |
| 7550 | pred0 + bh / 2 * stride0, stride0, &esq[0][2]); |
| 7551 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride, |
| 7552 | pred0 + bh / 2 * stride0 + bw / 2, stride0, |
| 7553 | &esq[0][3]); |
| 7554 | cpi->fn_ptr[f_index].vf(src, src_stride, pred1, stride1, &esq[1][0]); |
| 7555 | cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred1 + bw / 2, stride1, |
| 7556 | &esq[1][1]); |
| 7557 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride, |
| 7558 | pred1 + bh / 2 * stride1, stride0, &esq[1][2]); |
| 7559 | cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride, |
| 7560 | pred1 + bh / 2 * stride1 + bw / 2, stride0, |
| 7561 | &esq[1][3]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7562 | |
| 7563 | tl = (int64_t)(esq[0][0] + esq[0][1] + esq[0][2]) - |
| 7564 | (int64_t)(esq[1][0] + esq[1][1] + esq[1][2]); |
| 7565 | br = (int64_t)(esq[1][3] + esq[1][1] + esq[1][2]) - |
| 7566 | (int64_t)(esq[0][3] + esq[0][1] + esq[0][2]); |
| 7567 | return (tl + br > 0); |
| 7568 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7569 | #endif // CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7570 | |
| 7571 | #if !CONFIG_DUAL_FILTER |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 7572 | static InterpFilter predict_interp_filter( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7573 | const AV1_COMP *cpi, const MACROBLOCK *x, const BLOCK_SIZE bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7574 | const int mi_row, const int mi_col, |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 7575 | InterpFilter (*single_filter)[TOTAL_REFS_PER_FRAME]) { |
| 7576 | InterpFilter best_filter = SWITCHABLE; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7577 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7578 | const MACROBLOCKD *xd = &x->e_mbd; |
| 7579 | int bsl = mi_width_log2_lookup[bsize]; |
| 7580 | int pred_filter_search = |
| 7581 | cpi->sf.cb_pred_filter_search |
| 7582 | ? (((mi_row + mi_col) >> bsl) + |
| 7583 | get_chessboard_index(cm->current_video_frame)) & |
| 7584 | 0x1 |
| 7585 | : 0; |
| 7586 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 7587 | const int is_comp_pred = has_second_ref(mbmi); |
| 7588 | const int this_mode = mbmi->mode; |
| 7589 | int refs[2] = { mbmi->ref_frame[0], |
| 7590 | (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7591 | if (pred_filter_search) { |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 7592 | InterpFilter af = SWITCHABLE, lf = SWITCHABLE; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 7593 | if (xd->up_available) |
| 7594 | af = av1_extract_interp_filter( |
| 7595 | xd->mi[-xd->mi_stride]->mbmi.interp_filters, 0); |
| 7596 | if (xd->left_available) |
| 7597 | lf = av1_extract_interp_filter(xd->mi[-1]->mbmi.interp_filters, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7598 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 7599 | if ((this_mode != NEWMV && this_mode != NEW_NEWMV) || (af == lf)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7600 | best_filter = af; |
| 7601 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7602 | if (is_comp_pred) { |
| 7603 | if (cpi->sf.adaptive_mode_search) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7604 | switch (this_mode) { |
| 7605 | case NEAREST_NEARESTMV: |
| 7606 | if (single_filter[NEARESTMV][refs[0]] == |
| 7607 | single_filter[NEARESTMV][refs[1]]) |
| 7608 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 7609 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7610 | case NEAR_NEARMV: |
| 7611 | if (single_filter[NEARMV][refs[0]] == single_filter[NEARMV][refs[1]]) |
| 7612 | best_filter = single_filter[NEARMV][refs[0]]; |
| 7613 | break; |
| 7614 | case ZERO_ZEROMV: |
| 7615 | if (single_filter[ZEROMV][refs[0]] == single_filter[ZEROMV][refs[1]]) |
| 7616 | best_filter = single_filter[ZEROMV][refs[0]]; |
| 7617 | break; |
| 7618 | case NEW_NEWMV: |
| 7619 | if (single_filter[NEWMV][refs[0]] == single_filter[NEWMV][refs[1]]) |
| 7620 | best_filter = single_filter[NEWMV][refs[0]]; |
| 7621 | break; |
| 7622 | case NEAREST_NEWMV: |
| 7623 | if (single_filter[NEARESTMV][refs[0]] == |
| 7624 | single_filter[NEWMV][refs[1]]) |
| 7625 | best_filter = single_filter[NEARESTMV][refs[0]]; |
| 7626 | break; |
| 7627 | case NEAR_NEWMV: |
| 7628 | if (single_filter[NEARMV][refs[0]] == single_filter[NEWMV][refs[1]]) |
| 7629 | best_filter = single_filter[NEARMV][refs[0]]; |
| 7630 | break; |
| 7631 | case NEW_NEARESTMV: |
| 7632 | if (single_filter[NEWMV][refs[0]] == |
| 7633 | single_filter[NEARESTMV][refs[1]]) |
| 7634 | best_filter = single_filter[NEWMV][refs[0]]; |
| 7635 | break; |
| 7636 | case NEW_NEARMV: |
| 7637 | if (single_filter[NEWMV][refs[0]] == single_filter[NEARMV][refs[1]]) |
| 7638 | best_filter = single_filter[NEWMV][refs[0]]; |
| 7639 | break; |
| 7640 | default: |
| 7641 | if (single_filter[this_mode][refs[0]] == |
| 7642 | single_filter[this_mode][refs[1]]) |
| 7643 | best_filter = single_filter[this_mode][refs[0]]; |
| 7644 | break; |
| 7645 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7646 | } |
| 7647 | } |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 7648 | if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) { |
| 7649 | best_filter = EIGHTTAP_REGULAR; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7650 | } |
| 7651 | return best_filter; |
| 7652 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 7653 | #endif // !CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7654 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7655 | // Choose the best wedge index and sign |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7656 | #if CONFIG_WEDGE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7657 | 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] | 7658 | const BLOCK_SIZE bsize, const uint8_t *const p0, |
| 7659 | const uint8_t *const p1, int *const best_wedge_sign, |
| 7660 | int *const best_wedge_index) { |
| 7661 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 7662 | const struct buf_2d *const src = &x->plane[0].src; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7663 | const int bw = block_size_wide[bsize]; |
| 7664 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7665 | const int N = bw * bh; |
| 7666 | int rate; |
| 7667 | int64_t dist; |
| 7668 | int64_t rd, best_rd = INT64_MAX; |
| 7669 | int wedge_index; |
| 7670 | int wedge_sign; |
| 7671 | int wedge_types = (1 << get_wedge_bits_lookup(bsize)); |
| 7672 | const uint8_t *mask; |
| 7673 | uint64_t sse; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7674 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7675 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 7676 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 7677 | #else |
| 7678 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7679 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7680 | |
| 7681 | DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]); |
| 7682 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 7683 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 7684 | DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]); |
| 7685 | |
| 7686 | int64_t sign_limit; |
| 7687 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7688 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7689 | if (hbd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7690 | aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7691 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7692 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7693 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7694 | 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] | 7695 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7696 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7697 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7698 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7699 | aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw); |
| 7700 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 7701 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7702 | } |
| 7703 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7704 | sign_limit = ((int64_t)aom_sum_squares_i16(r0, N) - |
| 7705 | (int64_t)aom_sum_squares_i16(r1, N)) * |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7706 | (1 << WEDGE_WEIGHT_BITS) / 2; |
| 7707 | |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7708 | if (N < 64) |
| 7709 | av1_wedge_compute_delta_squares_c(ds, r0, r1, N); |
| 7710 | else |
| 7711 | av1_wedge_compute_delta_squares(ds, r0, r1, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7712 | |
| 7713 | for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7714 | mask = av1_get_contiguous_soft_mask(wedge_index, 0, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7715 | |
| 7716 | // TODO(jingning): Make sse2 functions support N = 16 case |
| 7717 | if (N < 64) |
| 7718 | wedge_sign = av1_wedge_sign_from_residuals_c(ds, mask, N, sign_limit); |
| 7719 | else |
| 7720 | wedge_sign = av1_wedge_sign_from_residuals(ds, mask, N, sign_limit); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7721 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7722 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7723 | if (N < 64) |
| 7724 | sse = av1_wedge_sse_from_residuals_c(r1, d10, mask, N); |
| 7725 | else |
| 7726 | sse = av1_wedge_sse_from_residuals(r1, d10, mask, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7727 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
| 7728 | |
| 7729 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7730 | rd = RDCOST(x->rdmult, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7731 | |
| 7732 | if (rd < best_rd) { |
| 7733 | *best_wedge_index = wedge_index; |
| 7734 | *best_wedge_sign = wedge_sign; |
| 7735 | best_rd = rd; |
| 7736 | } |
| 7737 | } |
| 7738 | |
| 7739 | return best_rd; |
| 7740 | } |
| 7741 | |
| 7742 | // Choose the best wedge index the specified sign |
| 7743 | static int64_t pick_wedge_fixed_sign( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7744 | const AV1_COMP *const cpi, const MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7745 | const BLOCK_SIZE bsize, const uint8_t *const p0, const uint8_t *const p1, |
| 7746 | const int wedge_sign, int *const best_wedge_index) { |
| 7747 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 7748 | const struct buf_2d *const src = &x->plane[0].src; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7749 | const int bw = block_size_wide[bsize]; |
| 7750 | const int bh = block_size_high[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7751 | const int N = bw * bh; |
| 7752 | int rate; |
| 7753 | int64_t dist; |
| 7754 | int64_t rd, best_rd = INT64_MAX; |
| 7755 | int wedge_index; |
| 7756 | int wedge_types = (1 << get_wedge_bits_lookup(bsize)); |
| 7757 | const uint8_t *mask; |
| 7758 | uint64_t sse; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7759 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7760 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 7761 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 7762 | #else |
| 7763 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7764 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7765 | |
| 7766 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 7767 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 7768 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7769 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7770 | if (hbd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7771 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7772 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7773 | 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] | 7774 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7775 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7776 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7777 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7778 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 7779 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7780 | } |
| 7781 | |
| 7782 | for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7783 | mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize); |
Jingning Han | 61418bb | 2017-01-23 17:12:48 -0800 | [diff] [blame] | 7784 | if (N < 64) |
| 7785 | sse = av1_wedge_sse_from_residuals_c(r1, d10, mask, N); |
| 7786 | else |
| 7787 | sse = av1_wedge_sse_from_residuals(r1, d10, mask, N); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7788 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
| 7789 | |
| 7790 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7791 | rd = RDCOST(x->rdmult, rate, dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7792 | |
| 7793 | if (rd < best_rd) { |
| 7794 | *best_wedge_index = wedge_index; |
| 7795 | best_rd = rd; |
| 7796 | } |
| 7797 | } |
| 7798 | |
| 7799 | return best_rd; |
| 7800 | } |
| 7801 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7802 | static int64_t pick_interinter_wedge(const AV1_COMP *const cpi, |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7803 | MACROBLOCK *const x, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7804 | const BLOCK_SIZE bsize, |
| 7805 | const uint8_t *const p0, |
| 7806 | const uint8_t *const p1) { |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7807 | MACROBLOCKD *const xd = &x->e_mbd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7808 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 7809 | const int bw = block_size_wide[bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7810 | |
| 7811 | int64_t rd; |
| 7812 | int wedge_index = -1; |
| 7813 | int wedge_sign = 0; |
| 7814 | |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 7815 | assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize)); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 7816 | assert(cpi->common.allow_masked_compound); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7817 | |
| 7818 | if (cpi->sf.fast_wedge_sign_estimate) { |
| 7819 | wedge_sign = estimate_wedge_sign(cpi, x, bsize, p0, bw, p1, bw); |
| 7820 | rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, wedge_sign, &wedge_index); |
| 7821 | } else { |
| 7822 | rd = pick_wedge(cpi, x, bsize, p0, p1, &wedge_sign, &wedge_index); |
| 7823 | } |
| 7824 | |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7825 | mbmi->wedge_sign = wedge_sign; |
| 7826 | mbmi->wedge_index = wedge_index; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7827 | return rd; |
| 7828 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7829 | #endif // CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7830 | |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7831 | #if CONFIG_COMPOUND_SEGMENT |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7832 | static int64_t pick_interinter_seg(const AV1_COMP *const cpi, |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7833 | MACROBLOCK *const x, const BLOCK_SIZE bsize, |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7834 | const uint8_t *const p0, |
| 7835 | const uint8_t *const p1) { |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7836 | MACROBLOCKD *const xd = &x->e_mbd; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7837 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7838 | const struct buf_2d *const src = &x->plane[0].src; |
| 7839 | const int bw = block_size_wide[bsize]; |
| 7840 | const int bh = block_size_high[bsize]; |
| 7841 | const int N = bw * bh; |
| 7842 | int rate; |
| 7843 | uint64_t sse; |
| 7844 | int64_t dist; |
Debargha Mukherjee | c30934b | 2017-04-25 01:23:51 -0700 | [diff] [blame] | 7845 | int64_t rd0; |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7846 | SEG_MASK_TYPE cur_mask_type; |
| 7847 | int64_t best_rd = INT64_MAX; |
| 7848 | SEG_MASK_TYPE best_mask_type = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7849 | #if CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7850 | const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH; |
| 7851 | const int bd_round = hbd ? (xd->bd - 8) * 2 : 0; |
| 7852 | #else |
| 7853 | const int bd_round = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7854 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7855 | DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]); |
| 7856 | DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]); |
| 7857 | DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]); |
| 7858 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7859 | #if CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7860 | if (hbd) { |
| 7861 | aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride, |
| 7862 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7863 | aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride, |
| 7864 | CONVERT_TO_BYTEPTR(p1), bw, xd->bd); |
| 7865 | aom_highbd_subtract_block(bh, bw, d10, bw, CONVERT_TO_BYTEPTR(p1), bw, |
| 7866 | CONVERT_TO_BYTEPTR(p0), bw, xd->bd); |
| 7867 | } else // NOLINT |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7868 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7869 | { |
| 7870 | aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw); |
| 7871 | aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw); |
| 7872 | aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw); |
| 7873 | } |
| 7874 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7875 | // try each mask type and its inverse |
| 7876 | 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] | 7877 | // build mask and inverse |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7878 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7879 | if (hbd) |
| 7880 | build_compound_seg_mask_highbd( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7881 | xd->seg_mask, cur_mask_type, CONVERT_TO_BYTEPTR(p0), bw, |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7882 | CONVERT_TO_BYTEPTR(p1), bw, bsize, bh, bw, xd->bd); |
| 7883 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7884 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7885 | build_compound_seg_mask(xd->seg_mask, cur_mask_type, p0, bw, p1, bw, |
| 7886 | bsize, bh, bw); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7887 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7888 | // compute rd for mask |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7889 | sse = av1_wedge_sse_from_residuals(r1, d10, xd->seg_mask, N); |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7890 | sse = ROUND_POWER_OF_TWO(sse, bd_round); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7891 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7892 | model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 7893 | rd0 = RDCOST(x->rdmult, rate, dist); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7894 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7895 | if (rd0 < best_rd) { |
| 7896 | best_mask_type = cur_mask_type; |
| 7897 | best_rd = rd0; |
| 7898 | } |
| 7899 | } |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7900 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7901 | // make final mask |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7902 | mbmi->mask_type = best_mask_type; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7903 | #if CONFIG_HIGHBITDEPTH |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7904 | if (hbd) |
| 7905 | build_compound_seg_mask_highbd( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7906 | xd->seg_mask, mbmi->mask_type, CONVERT_TO_BYTEPTR(p0), bw, |
Debargha Mukherjee | 1edf9a3 | 2017-01-07 18:54:20 -0800 | [diff] [blame] | 7907 | CONVERT_TO_BYTEPTR(p1), bw, bsize, bh, bw, xd->bd); |
| 7908 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 7909 | #endif // CONFIG_HIGHBITDEPTH |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7910 | build_compound_seg_mask(xd->seg_mask, mbmi->mask_type, p0, bw, p1, bw, |
| 7911 | bsize, bh, bw); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7912 | |
Sarah Parker | b9f757c | 2017-01-06 17:12:24 -0800 | [diff] [blame] | 7913 | return best_rd; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 7914 | } |
| 7915 | #endif // CONFIG_COMPOUND_SEGMENT |
| 7916 | |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7917 | #if CONFIG_WEDGE && CONFIG_INTERINTRA |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 7918 | static int64_t pick_interintra_wedge(const AV1_COMP *const cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7919 | const MACROBLOCK *const x, |
| 7920 | const BLOCK_SIZE bsize, |
| 7921 | const uint8_t *const p0, |
| 7922 | const uint8_t *const p1) { |
| 7923 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 7924 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7925 | |
| 7926 | int64_t rd; |
| 7927 | int wedge_index = -1; |
| 7928 | |
| 7929 | assert(is_interintra_wedge_used(bsize)); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 7930 | assert(cpi->common.allow_interintra_compound); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 7931 | |
| 7932 | rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, 0, &wedge_index); |
| 7933 | |
| 7934 | mbmi->interintra_wedge_sign = 0; |
| 7935 | mbmi->interintra_wedge_index = wedge_index; |
| 7936 | return rd; |
| 7937 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7938 | #endif // CONFIG_WEDGE && CONFIG_INTERINTRA |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7939 | |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 7940 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7941 | 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] | 7942 | const BLOCK_SIZE bsize, |
| 7943 | const uint8_t *const p0, |
| 7944 | const uint8_t *const p1) { |
| 7945 | const COMPOUND_TYPE compound_type = |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7946 | x->e_mbd.mi[0]->mbmi.interinter_compound_type; |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 7947 | switch (compound_type) { |
| 7948 | #if CONFIG_WEDGE |
| 7949 | case COMPOUND_WEDGE: return pick_interinter_wedge(cpi, x, bsize, p0, p1); |
| 7950 | #endif // CONFIG_WEDGE |
| 7951 | #if CONFIG_COMPOUND_SEGMENT |
| 7952 | case COMPOUND_SEG: return pick_interinter_seg(cpi, x, bsize, p0, p1); |
| 7953 | #endif // CONFIG_COMPOUND_SEGMENT |
| 7954 | default: assert(0); return 0; |
| 7955 | } |
| 7956 | } |
| 7957 | |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7958 | static int interinter_compound_motion_search( |
| 7959 | const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv, |
| 7960 | 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] | 7961 | MACROBLOCKD *const xd = &x->e_mbd; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7962 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 7963 | int_mv tmp_mv[2]; |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7964 | int tmp_rate_mv = 0; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 7965 | const INTERINTER_COMPOUND_DATA compound_data = { |
| 7966 | #if CONFIG_WEDGE |
| 7967 | mbmi->wedge_index, |
| 7968 | mbmi->wedge_sign, |
| 7969 | #endif // CONFIG_WEDGE |
| 7970 | #if CONFIG_COMPOUND_SEGMENT |
| 7971 | mbmi->mask_type, |
| 7972 | xd->seg_mask, |
| 7973 | #endif // CONFIG_COMPOUND_SEGMENT |
| 7974 | mbmi->interinter_compound_type |
| 7975 | }; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7976 | #if CONFIG_COMPOUND_SINGLEREF |
| 7977 | // NOTE: Mode is needed to identify the compound mode prediction, regardless |
| 7978 | // of comp refs or single ref. |
| 7979 | mbmi->mode = this_mode; |
| 7980 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7981 | |
| 7982 | if (this_mode == NEW_NEWMV |
| 7983 | #if CONFIG_COMPOUND_SINGLEREF |
| 7984 | || this_mode == SR_NEW_NEWMV |
| 7985 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 7986 | ) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7987 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 7988 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 2); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7989 | mbmi->mv[0].as_int = tmp_mv[0].as_int; |
| 7990 | mbmi->mv[1].as_int = tmp_mv[1].as_int; |
| 7991 | } else if (this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 7992 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 7993 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 0); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 7994 | mbmi->mv[0].as_int = tmp_mv[0].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 7995 | } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV |
| 7996 | #if CONFIG_COMPOUND_SINGLEREF |
| 7997 | // || this_mode == SR_NEAREST_NEWMV |
| 7998 | || this_mode == SR_NEAR_NEWMV || this_mode == SR_ZERO_NEWMV |
| 7999 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8000 | ) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 8001 | do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize, |
| 8002 | mi_row, mi_col, tmp_mv, &tmp_rate_mv, 1); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8003 | mbmi->mv[1].as_int = tmp_mv[1].as_int; |
| 8004 | } |
| 8005 | return tmp_rate_mv; |
| 8006 | } |
| 8007 | |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8008 | static int64_t build_and_cost_compound_type( |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8009 | const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv, |
| 8010 | const BLOCK_SIZE bsize, const int this_mode, int rs2, int rate_mv, |
| 8011 | BUFFER_SET *ctx, int *out_rate_mv, uint8_t **preds0, uint8_t **preds1, |
| 8012 | int *strides, int mi_row, int mi_col) { |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 8013 | const AV1_COMMON *const cm = &cpi->common; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8014 | MACROBLOCKD *xd = &x->e_mbd; |
| 8015 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8016 | int rate_sum; |
| 8017 | int64_t dist_sum; |
| 8018 | int64_t best_rd_cur = INT64_MAX; |
| 8019 | int64_t rd = INT64_MAX; |
| 8020 | int tmp_skip_txfm_sb; |
| 8021 | int64_t tmp_skip_sse_sb; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8022 | const COMPOUND_TYPE compound_type = mbmi->interinter_compound_type; |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8023 | |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8024 | best_rd_cur = pick_interinter_mask(cpi, x, bsize, *preds0, *preds1); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8025 | best_rd_cur += RDCOST(x->rdmult, rs2 + rate_mv, 0); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 8026 | |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 8027 | if (have_newmv_in_inter_mode(this_mode) && |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8028 | use_masked_motion_search(compound_type)) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 8029 | *out_rate_mv = interinter_compound_motion_search(cpi, x, cur_mv, bsize, |
| 8030 | this_mode, mi_row, mi_col); |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 8031 | 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] | 8032 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 8033 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8034 | rd = RDCOST(x->rdmult, rs2 + *out_rate_mv + rate_sum, dist_sum); |
Zoe Liu | 4d44f5a | 2016-12-14 17:46:19 -0800 | [diff] [blame] | 8035 | if (rd >= best_rd_cur) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8036 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 8037 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 8038 | *out_rate_mv = rate_mv; |
David Barker | 426a997 | 2017-01-27 11:03:11 +0000 | [diff] [blame] | 8039 | av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, |
| 8040 | #if CONFIG_SUPERTX |
| 8041 | 0, 0, |
| 8042 | #endif // CONFIG_SUPERTX |
| 8043 | preds0, strides, preds1, |
| 8044 | strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8045 | } |
| 8046 | av1_subtract_plane(x, bsize, 0); |
| 8047 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 8048 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 8049 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8050 | rd = RDCOST(x->rdmult, rs2 + *out_rate_mv + rate_sum, dist_sum); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8051 | best_rd_cur = rd; |
| 8052 | |
| 8053 | } else { |
David Barker | 426a997 | 2017-01-27 11:03:11 +0000 | [diff] [blame] | 8054 | av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, |
| 8055 | #if CONFIG_SUPERTX |
| 8056 | 0, 0, |
| 8057 | #endif // CONFIG_SUPERTX |
| 8058 | preds0, strides, preds1, strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8059 | av1_subtract_plane(x, bsize, 0); |
| 8060 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 8061 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 8062 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8063 | rd = RDCOST(x->rdmult, rs2 + rate_mv + rate_sum, dist_sum); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 8064 | best_rd_cur = rd; |
| 8065 | } |
| 8066 | return best_rd_cur; |
| 8067 | } |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 8068 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8069 | |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8070 | typedef struct { |
| 8071 | #if CONFIG_MOTION_VAR |
| 8072 | // Inter prediction buffers and respective strides |
| 8073 | uint8_t *above_pred_buf[MAX_MB_PLANE]; |
| 8074 | int above_pred_stride[MAX_MB_PLANE]; |
| 8075 | uint8_t *left_pred_buf[MAX_MB_PLANE]; |
| 8076 | int left_pred_stride[MAX_MB_PLANE]; |
| 8077 | #endif // CONFIG_MOTION_VAR |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8078 | int_mv *single_newmv; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8079 | // Pointer to array of motion vectors to use for each ref and their rates |
| 8080 | // Should point to first of 2 arrays in 2D array |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8081 | int *single_newmv_rate; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8082 | // Pointer to array of predicted rate-distortion |
| 8083 | // Should point to first of 2 arrays in 2D array |
| 8084 | int64_t (*modelled_rd)[TOTAL_REFS_PER_FRAME]; |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 8085 | InterpFilter single_filter[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 8086 | } HandleInterModeArgs; |
| 8087 | |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8088 | static int64_t handle_newmv(const AV1_COMP *const cpi, MACROBLOCK *const x, |
| 8089 | const BLOCK_SIZE bsize, |
| 8090 | int_mv (*const mode_mv)[TOTAL_REFS_PER_FRAME], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8091 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8092 | int_mv (*const mode_comp_mv)[TOTAL_REFS_PER_FRAME], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8093 | #endif // CONFIG_COMPOUND_SINGLEREF |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8094 | const int mi_row, const int mi_col, |
| 8095 | int *const rate_mv, int_mv *const single_newmv, |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 8096 | HandleInterModeArgs *const args) { |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8097 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 8098 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8099 | const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 8100 | const int is_comp_pred = has_second_ref(mbmi); |
| 8101 | const PREDICTION_MODE this_mode = mbmi->mode; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8102 | const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8103 | int_mv *const frame_mv = mode_mv[this_mode]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8104 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8105 | int_mv *const frame_comp_mv = mode_comp_mv[this_mode]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8106 | #endif // CONFIG_COMPOUND_SINGLEREF |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8107 | const int refs[2] = { mbmi->ref_frame[0], |
| 8108 | mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1] }; |
| 8109 | int i; |
| 8110 | |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 8111 | (void)args; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8112 | |
| 8113 | if (is_comp_pred) { |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8114 | for (i = 0; i < 2; ++i) { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8115 | 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] | 8116 | } |
| 8117 | |
| 8118 | if (this_mode == NEW_NEWMV) { |
| 8119 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
| 8120 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
| 8121 | |
| 8122 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8123 | joint_motion_search(cpi, x, bsize, frame_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8124 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8125 | NULL, // int_mv *frame_comp_mv |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8126 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8127 | mi_row, mi_col, NULL, NULL, 0, rate_mv, 0); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8128 | } else { |
| 8129 | *rate_mv = 0; |
| 8130 | for (i = 0; i < 2; ++i) { |
Zoe Liu | 122f394 | 2017-04-25 11:18:38 -0700 | [diff] [blame] | 8131 | av1_set_mvcost(x, refs[i], i, mbmi->ref_mv_idx); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8132 | *rate_mv += av1_mv_bit_cost( |
| 8133 | &frame_mv[refs[i]].as_mv, &mbmi_ext->ref_mvs[refs[i]][0].as_mv, |
| 8134 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8135 | } |
| 8136 | } |
| 8137 | } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV) { |
| 8138 | frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 8139 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 8140 | frame_mv[refs[0]].as_int = |
| 8141 | mode_mv[compound_ref0_mode(this_mode)][refs[0]].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8142 | compound_single_motion_search_interinter(cpi, x, bsize, frame_mv, |
| 8143 | #if CONFIG_COMPOUND_SINGLEREF |
| 8144 | NULL, |
| 8145 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8146 | mi_row, mi_col, NULL, 0, |
| 8147 | rate_mv, 0, 1); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 8148 | } else { |
| 8149 | av1_set_mvcost(x, refs[1], 1, mbmi->ref_mv_idx); |
| 8150 | *rate_mv = av1_mv_bit_cost(&frame_mv[refs[1]].as_mv, |
| 8151 | &mbmi_ext->ref_mvs[refs[1]][0].as_mv, |
| 8152 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8153 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8154 | } else { |
David Barker | cb03dc3 | 2017-04-07 13:05:09 +0100 | [diff] [blame] | 8155 | assert(this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8156 | frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 8157 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 8158 | frame_mv[refs[1]].as_int = |
| 8159 | mode_mv[compound_ref1_mode(this_mode)][refs[1]].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8160 | compound_single_motion_search_interinter(cpi, x, bsize, frame_mv, |
| 8161 | #if CONFIG_COMPOUND_SINGLEREF |
| 8162 | NULL, |
| 8163 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8164 | mi_row, mi_col, NULL, 0, |
| 8165 | rate_mv, 0, 0); |
David Barker | 8dd9b57 | 2017-05-12 16:31:38 +0100 | [diff] [blame] | 8166 | } else { |
| 8167 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
| 8168 | *rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
| 8169 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 8170 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8171 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8172 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8173 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8174 | } else if (is_inter_singleref_comp_mode(this_mode)) { |
| 8175 | // Single ref comp mode |
| 8176 | const int mode0 = compound_ref0_mode(this_mode); |
| 8177 | |
| 8178 | single_newmv[refs[0]].as_int = args->single_newmv[refs[0]].as_int; |
| 8179 | frame_mv[refs[0]].as_int = (mode0 == NEWMV) |
| 8180 | ? single_newmv[refs[0]].as_int |
| 8181 | : mode_mv[mode0][refs[0]].as_int; |
| 8182 | assert(compound_ref1_mode(this_mode) == NEWMV); |
| 8183 | frame_comp_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; |
| 8184 | |
| 8185 | if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { |
| 8186 | if (this_mode == SR_NEW_NEWMV) { |
| 8187 | joint_motion_search(cpi, x, bsize, frame_mv, frame_comp_mv, mi_row, |
| 8188 | mi_col, NULL, NULL, 0, rate_mv, 0); |
| 8189 | } else { |
| 8190 | assert( // this_mode == SR_NEAREST_NEWMV || |
| 8191 | this_mode == SR_NEAR_NEWMV || this_mode == SR_ZERO_NEWMV); |
| 8192 | compound_single_motion_search_interinter(cpi, x, bsize, frame_mv, |
| 8193 | frame_comp_mv, mi_row, mi_col, |
| 8194 | NULL, 0, rate_mv, 0, 1); |
| 8195 | } |
| 8196 | } else { |
| 8197 | *rate_mv = 0; |
| 8198 | av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx); |
| 8199 | if (mode0 == NEWMV) |
| 8200 | *rate_mv += av1_mv_bit_cost(&frame_mv[refs[0]].as_mv, |
| 8201 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 8202 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8203 | *rate_mv += av1_mv_bit_cost(&frame_comp_mv[refs[0]].as_mv, |
| 8204 | &mbmi_ext->ref_mvs[refs[0]][0].as_mv, |
| 8205 | x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); |
| 8206 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8207 | #endif // CONFIG_COMPOUND_SINGLEREF |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8208 | } else { |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8209 | if (is_comp_interintra_pred) { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8210 | x->best_mv = args->single_newmv[refs[0]]; |
| 8211 | *rate_mv = args->single_newmv_rate[refs[0]]; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8212 | } else { |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 8213 | single_motion_search(cpi, x, bsize, mi_row, mi_col, 0, rate_mv); |
| 8214 | args->single_newmv[refs[0]] = x->best_mv; |
| 8215 | args->single_newmv_rate[refs[0]] = *rate_mv; |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8216 | } |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8217 | |
| 8218 | if (x->best_mv.as_int == INVALID_MV) return INT64_MAX; |
| 8219 | |
| 8220 | frame_mv[refs[0]] = x->best_mv; |
| 8221 | xd->mi[0]->bmi[0].as_mv[0] = x->best_mv; |
| 8222 | |
| 8223 | // Estimate the rate implications of a new mv but discount this |
| 8224 | // under certain circumstances where we want to help initiate a weak |
| 8225 | // motion field, where the distortion gain for a single block may not |
| 8226 | // be enough to overcome the cost of a new mv. |
| 8227 | if (discount_newmv_test(cpi, this_mode, x->best_mv, mode_mv, refs[0])) { |
| 8228 | *rate_mv = AOMMAX(*rate_mv / NEW_MV_DISCOUNT_FACTOR, 1); |
| 8229 | } |
| 8230 | } |
| 8231 | |
| 8232 | return 0; |
| 8233 | } |
| 8234 | |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8235 | int64_t interpolation_filter_search( |
| 8236 | MACROBLOCK *const x, const AV1_COMP *const cpi, BLOCK_SIZE bsize, |
| 8237 | int mi_row, int mi_col, const BUFFER_SET *const tmp_dst, |
| 8238 | BUFFER_SET *const orig_dst, |
| 8239 | InterpFilter (*const single_filter)[TOTAL_REFS_PER_FRAME], |
| 8240 | int64_t *const rd, int *const switchable_rate, int *const skip_txfm_sb, |
| 8241 | int64_t *const skip_sse_sb) { |
| 8242 | const AV1_COMMON *cm = &cpi->common; |
| 8243 | MACROBLOCKD *const xd = &x->e_mbd; |
| 8244 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 8245 | int i; |
| 8246 | int tmp_rate; |
| 8247 | int64_t tmp_dist; |
| 8248 | |
| 8249 | (void)single_filter; |
| 8250 | |
| 8251 | InterpFilter assign_filter = SWITCHABLE; |
| 8252 | |
| 8253 | if (cm->interp_filter == SWITCHABLE) { |
| 8254 | #if !CONFIG_DUAL_FILTER |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 8255 | assign_filter = av1_is_interp_needed(xd) |
| 8256 | ? predict_interp_filter(cpi, x, bsize, mi_row, mi_col, |
| 8257 | single_filter) |
| 8258 | : cm->interp_filter; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8259 | #endif // !CONFIG_DUAL_FILTER |
| 8260 | } else { |
| 8261 | assign_filter = cm->interp_filter; |
| 8262 | } |
| 8263 | |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 8264 | set_default_interp_filters(mbmi, assign_filter); |
| 8265 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8266 | *switchable_rate = av1_get_switchable_rate(cm, x, xd); |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8267 | 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] | 8268 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, &tmp_dist, |
| 8269 | skip_txfm_sb, skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8270 | *rd = RDCOST(x->rdmult, *switchable_rate + tmp_rate, tmp_dist); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8271 | |
| 8272 | if (assign_filter == SWITCHABLE) { |
| 8273 | // do interp_filter search |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 8274 | if (av1_is_interp_needed(xd) && av1_is_interp_search_needed(xd)) { |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8275 | #if CONFIG_DUAL_FILTER |
| 8276 | const int filter_set_size = DUAL_FILTER_SET_SIZE; |
| 8277 | #else |
| 8278 | const int filter_set_size = SWITCHABLE_FILTERS; |
| 8279 | #endif // CONFIG_DUAL_FILTER |
| 8280 | int best_in_temp = 0; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8281 | InterpFilters best_filters = mbmi->interp_filters; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8282 | restore_dst_buf(xd, *tmp_dst); |
| 8283 | // EIGHTTAP_REGULAR mode is calculated beforehand |
| 8284 | for (i = 1; i < filter_set_size; ++i) { |
| 8285 | int tmp_skip_sb = 0; |
| 8286 | int64_t tmp_skip_sse = INT64_MAX; |
| 8287 | int tmp_rs; |
| 8288 | int64_t tmp_rd; |
| 8289 | #if CONFIG_DUAL_FILTER |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8290 | mbmi->interp_filters = |
| 8291 | av1_make_interp_filters(filter_sets[i][0], filter_sets[i][1]); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8292 | #else |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8293 | mbmi->interp_filters = av1_broadcast_interp_filter((InterpFilter)i); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8294 | #endif // CONFIG_DUAL_FILTER |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8295 | tmp_rs = av1_get_switchable_rate(cm, x, xd); |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8296 | 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] | 8297 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 8298 | &tmp_dist, &tmp_skip_sb, &tmp_skip_sse); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8299 | tmp_rd = RDCOST(x->rdmult, tmp_rs + tmp_rate, tmp_dist); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8300 | |
| 8301 | if (tmp_rd < *rd) { |
| 8302 | *rd = tmp_rd; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8303 | *switchable_rate = av1_get_switchable_rate(cm, x, xd); |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8304 | best_filters = mbmi->interp_filters; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8305 | *skip_txfm_sb = tmp_skip_sb; |
| 8306 | *skip_sse_sb = tmp_skip_sse; |
| 8307 | best_in_temp = !best_in_temp; |
| 8308 | if (best_in_temp) { |
| 8309 | restore_dst_buf(xd, *orig_dst); |
| 8310 | } else { |
| 8311 | restore_dst_buf(xd, *tmp_dst); |
| 8312 | } |
| 8313 | } |
| 8314 | } |
| 8315 | if (best_in_temp) { |
| 8316 | restore_dst_buf(xd, *tmp_dst); |
| 8317 | } else { |
| 8318 | restore_dst_buf(xd, *orig_dst); |
| 8319 | } |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8320 | mbmi->interp_filters = best_filters; |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8321 | } else { |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8322 | assert(mbmi->interp_filters == |
| 8323 | av1_broadcast_interp_filter(EIGHTTAP_REGULAR)); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 8324 | } |
| 8325 | } |
| 8326 | |
| 8327 | return 0; |
| 8328 | } |
| 8329 | |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8330 | #if CONFIG_DUAL_FILTER |
| 8331 | static InterpFilters condition_interp_filters_on_mv( |
| 8332 | InterpFilters interp_filters, const MACROBLOCKD *xd) { |
| 8333 | InterpFilter filters[2]; |
| 8334 | for (int i = 0; i < 2; ++i) |
| 8335 | filters[i] = (has_subpel_mv_component(xd->mi[0], xd, i)) |
| 8336 | ? av1_extract_interp_filter(interp_filters, i) |
| 8337 | : EIGHTTAP_REGULAR; |
| 8338 | |
| 8339 | return av1_make_interp_filters(filters[0], filters[1]); |
| 8340 | } |
| 8341 | #endif |
| 8342 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8343 | // TODO(afergs): Refactor the MBMI references in here - there's four |
| 8344 | // TODO(afergs): Refactor optional args - add them to a struct or remove |
| 8345 | static int64_t motion_mode_rd( |
| 8346 | const AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize, |
| 8347 | RD_STATS *rd_stats, RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv, |
| 8348 | int *disable_skip, int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], int mi_row, |
| 8349 | int mi_col, HandleInterModeArgs *const args, const int64_t ref_best_rd, |
| 8350 | const int *refs, int rate_mv, |
| 8351 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Wei-Ting Lin | 85a8f70 | 2017-06-22 13:55:15 -0700 | [diff] [blame] | 8352 | // only used when WARPED_MOTION is on? |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8353 | int_mv *const single_newmv, int rate2_bmc_nocoeff, |
| 8354 | MB_MODE_INFO *best_bmc_mbmi, int rate_mv_bmc, |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8355 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8356 | int rs, int *skip_txfm_sb, int64_t *skip_sse_sb, BUFFER_SET *orig_dst) { |
| 8357 | const AV1_COMMON *const cm = &cpi->common; |
| 8358 | MACROBLOCKD *xd = &x->e_mbd; |
| 8359 | MODE_INFO *mi = xd->mi[0]; |
| 8360 | MB_MODE_INFO *mbmi = &mi->mbmi; |
| 8361 | const int is_comp_pred = has_second_ref(mbmi); |
| 8362 | const PREDICTION_MODE this_mode = mbmi->mode; |
| 8363 | |
| 8364 | (void)mode_mv; |
| 8365 | (void)mi_row; |
| 8366 | (void)mi_col; |
| 8367 | (void)args; |
| 8368 | (void)refs; |
| 8369 | (void)rate_mv; |
| 8370 | (void)is_comp_pred; |
| 8371 | (void)this_mode; |
Angie Chiang | cf85d5c | 2017-08-20 12:56:19 -0700 | [diff] [blame] | 8372 | #if !CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
Cheng Chen | 811bba7 | 2017-08-21 17:39:31 -0700 | [diff] [blame] | 8373 | (void)single_newmv; |
| 8374 | #endif |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8375 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8376 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8377 | MOTION_MODE motion_mode, last_motion_mode_allowed; |
| 8378 | int rate2_nocoeff = 0, best_xskip, best_disable_skip = 0; |
| 8379 | RD_STATS best_rd_stats, best_rd_stats_y, best_rd_stats_uv; |
| 8380 | MB_MODE_INFO base_mbmi, best_mbmi; |
| 8381 | #if CONFIG_VAR_TX |
| 8382 | uint8_t best_blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 4]; |
| 8383 | #endif // CONFIG_VAR_TX |
| 8384 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8385 | |
| 8386 | #if CONFIG_WARPED_MOTION |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8387 | #if WARPED_MOTION_SORT_SAMPLES |
| 8388 | int pts0[SAMPLES_ARRAY_SIZE], pts_inref0[SAMPLES_ARRAY_SIZE]; |
| 8389 | int pts_mv0[SAMPLES_ARRAY_SIZE]; |
| 8390 | int total_samples; |
| 8391 | #else |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8392 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8393 | #endif // WARPED_MOTION_SORT_SAMPLES |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8394 | #endif // CONFIG_WARPED_MOTION |
| 8395 | |
| 8396 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8397 | av1_invalid_rd_stats(&best_rd_stats); |
| 8398 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8399 | |
| 8400 | if (cm->interp_filter == SWITCHABLE) rd_stats->rate += rs; |
| 8401 | #if CONFIG_WARPED_MOTION |
| 8402 | aom_clear_system_state(); |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8403 | #if WARPED_MOTION_SORT_SAMPLES |
| 8404 | mbmi->num_proj_ref[0] = |
| 8405 | findSamples(cm, xd, mi_row, mi_col, pts0, pts_inref0, pts_mv0); |
| 8406 | total_samples = mbmi->num_proj_ref[0]; |
| 8407 | #else |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8408 | 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] | 8409 | #endif // WARPED_MOTION_SORT_SAMPLES |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8410 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8411 | #endif // CONFIG_WARPED_MOTION |
| 8412 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8413 | rate2_nocoeff = rd_stats->rate; |
| 8414 | last_motion_mode_allowed = motion_mode_allowed( |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 8415 | #if CONFIG_GLOBAL_MOTION |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8416 | 0, xd->global_motion, |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 8417 | #endif // CONFIG_GLOBAL_MOTION |
Yue Chen | 52c5173 | 2017-07-11 15:08:30 -0700 | [diff] [blame] | 8418 | #if CONFIG_WARPED_MOTION |
| 8419 | xd, |
| 8420 | #endif |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8421 | mi); |
| 8422 | base_mbmi = *mbmi; |
| 8423 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8424 | |
| 8425 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8426 | int64_t best_rd = INT64_MAX; |
| 8427 | for (motion_mode = SIMPLE_TRANSLATION; |
| 8428 | motion_mode <= last_motion_mode_allowed; motion_mode++) { |
| 8429 | int64_t tmp_rd = INT64_MAX; |
| 8430 | int tmp_rate; |
| 8431 | int64_t tmp_dist; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8432 | int tmp_rate2 = |
| 8433 | motion_mode != SIMPLE_TRANSLATION ? rate2_bmc_nocoeff : rate2_nocoeff; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8434 | |
Wei-Ting Lin | 2088528 | 2017-08-28 17:18:18 -0700 | [diff] [blame] | 8435 | #if CONFIG_NCOBMC_ADAPT_WEIGHT |
| 8436 | // We cannot estimate the rd cost for the motion mode NCOBMC_ADAPT_WEIGHT |
| 8437 | // right now since it requires mvs from all neighboring blocks. We will |
| 8438 | // check if this mode is beneficial after all the mv's in the current |
| 8439 | // superblock are selected. |
| 8440 | if (motion_mode == NCOBMC_ADAPT_WEIGHT) continue; |
| 8441 | #endif |
| 8442 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8443 | *mbmi = base_mbmi; |
| 8444 | mbmi->motion_mode = motion_mode; |
| 8445 | #if CONFIG_MOTION_VAR |
| 8446 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8447 | *mbmi = *best_bmc_mbmi; |
| 8448 | mbmi->motion_mode = OBMC_CAUSAL; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8449 | if (!is_comp_pred && |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8450 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8451 | !is_inter_singleref_comp_mode(this_mode) && |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8452 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8453 | have_newmv_in_inter_mode(this_mode)) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8454 | int tmp_rate_mv = 0; |
| 8455 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8456 | single_motion_search(cpi, x, bsize, mi_row, mi_col, 0, &tmp_rate_mv); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8457 | mbmi->mv[0].as_int = x->best_mv.as_int; |
| 8458 | if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv, |
| 8459 | refs[0])) { |
| 8460 | tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
| 8461 | } |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8462 | tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8463 | #if CONFIG_DUAL_FILTER |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8464 | mbmi->interp_filters = |
| 8465 | condition_interp_filters_on_mv(mbmi->interp_filters, xd); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8466 | #endif // CONFIG_DUAL_FILTER |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8467 | 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] | 8468 | } else { |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8469 | 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] | 8470 | } |
| 8471 | av1_build_obmc_inter_prediction( |
| 8472 | cm, xd, mi_row, mi_col, args->above_pred_buf, args->above_pred_stride, |
| 8473 | args->left_pred_buf, args->left_pred_stride); |
| 8474 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 8475 | &tmp_dist, skip_txfm_sb, skip_sse_sb); |
| 8476 | } |
| 8477 | #endif // CONFIG_MOTION_VAR |
| 8478 | |
| 8479 | #if CONFIG_WARPED_MOTION |
| 8480 | if (mbmi->motion_mode == WARPED_CAUSAL) { |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8481 | #if WARPED_MOTION_SORT_SAMPLES |
| 8482 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
| 8483 | #endif // WARPED_MOTION_SORT_SAMPLES |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8484 | *mbmi = *best_bmc_mbmi; |
| 8485 | mbmi->motion_mode = WARPED_CAUSAL; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8486 | mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8487 | mbmi->interp_filters = av1_broadcast_interp_filter( |
| 8488 | av1_unswitchable_filter(cm->interp_filter)); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8489 | |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8490 | #if WARPED_MOTION_SORT_SAMPLES |
| 8491 | memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0)); |
| 8492 | memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0)); |
| 8493 | // Rank the samples by motion vector difference |
| 8494 | if (mbmi->num_proj_ref[0] > 1) { |
| 8495 | mbmi->num_proj_ref[0] = sortSamples(pts_mv0, &mbmi->mv[0].as_mv, pts, |
| 8496 | pts_inref, mbmi->num_proj_ref[0]); |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8497 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8498 | } |
| 8499 | #endif // WARPED_MOTION_SORT_SAMPLES |
| 8500 | |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8501 | if (!find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize, |
| 8502 | mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col, |
| 8503 | &mbmi->wm_params[0], mi_row, mi_col)) { |
| 8504 | // Refine MV for NEWMV mode |
| 8505 | if (!is_comp_pred && have_newmv_in_inter_mode(this_mode)) { |
| 8506 | int tmp_rate_mv = 0; |
| 8507 | const int_mv mv0 = mbmi->mv[0]; |
| 8508 | WarpedMotionParams wm_params0 = mbmi->wm_params[0]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8509 | #if WARPED_MOTION_SORT_SAMPLES |
| 8510 | int num_proj_ref0 = mbmi->num_proj_ref[0]; |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8511 | |
| 8512 | // Refine MV in a small range. |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8513 | av1_refine_warped_mv(cpi, x, bsize, mi_row, mi_col, pts0, pts_inref0, |
| 8514 | pts_mv0, total_samples); |
| 8515 | #else |
| 8516 | // Refine MV in a small range. |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8517 | 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] | 8518 | #endif // WARPED_MOTION_SORT_SAMPLES |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8519 | |
| 8520 | // Keep the refined MV and WM parameters. |
| 8521 | if (mv0.as_int != mbmi->mv[0].as_int) { |
| 8522 | const int ref = refs[0]; |
| 8523 | const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv; |
| 8524 | |
| 8525 | tmp_rate_mv = |
| 8526 | av1_mv_bit_cost(&mbmi->mv[0].as_mv, &ref_mv, x->nmvjointcost, |
| 8527 | x->mvcost, MV_COST_WEIGHT); |
| 8528 | |
| 8529 | if (cpi->sf.adaptive_motion_search) |
| 8530 | x->pred_mv[ref] = mbmi->mv[0].as_mv; |
| 8531 | |
| 8532 | single_newmv[ref] = mbmi->mv[0]; |
| 8533 | |
| 8534 | if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv, |
| 8535 | refs[0])) { |
| 8536 | tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
| 8537 | } |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8538 | #if WARPED_MOTION_SORT_SAMPLES |
| 8539 | best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0]; |
| 8540 | #endif // WARPED_MOTION_SORT_SAMPLES |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8541 | tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv; |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8542 | #if CONFIG_DUAL_FILTER |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8543 | mbmi->interp_filters = |
| 8544 | condition_interp_filters_on_mv(mbmi->interp_filters, xd); |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8545 | #endif // CONFIG_DUAL_FILTER |
| 8546 | } else { |
| 8547 | // Restore the old MV and WM parameters. |
| 8548 | mbmi->mv[0] = mv0; |
| 8549 | mbmi->wm_params[0] = wm_params0; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 8550 | #if WARPED_MOTION_SORT_SAMPLES |
| 8551 | mbmi->num_proj_ref[0] = num_proj_ref0; |
| 8552 | #endif // WARPED_MOTION_SORT_SAMPLES |
Yunqing Wang | 68f3ccd | 2017-05-23 14:43:54 -0700 | [diff] [blame] | 8553 | } |
| 8554 | } |
| 8555 | |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 8556 | 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] | 8557 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 8558 | &tmp_dist, skip_txfm_sb, skip_sse_sb); |
| 8559 | } else { |
| 8560 | continue; |
| 8561 | } |
| 8562 | } |
| 8563 | #endif // CONFIG_WARPED_MOTION |
| 8564 | x->skip = 0; |
| 8565 | |
| 8566 | rd_stats->dist = 0; |
| 8567 | rd_stats->sse = 0; |
| 8568 | rd_stats->skip = 1; |
| 8569 | rd_stats->rate = tmp_rate2; |
| 8570 | if (last_motion_mode_allowed > SIMPLE_TRANSLATION) { |
| 8571 | #if CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
| 8572 | if (last_motion_mode_allowed == WARPED_CAUSAL) |
| 8573 | #endif // CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
Yue Chen | bdc8dab | 2017-07-26 12:05:47 -0700 | [diff] [blame] | 8574 | rd_stats->rate += x->motion_mode_cost[bsize][mbmi->motion_mode]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8575 | #if CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
| 8576 | else |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8577 | rd_stats->rate += x->motion_mode_cost1[bsize][mbmi->motion_mode]; |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8578 | #endif // CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR |
| 8579 | } |
| 8580 | #if CONFIG_WARPED_MOTION |
| 8581 | if (mbmi->motion_mode == WARPED_CAUSAL) { |
| 8582 | rd_stats->rate -= rs; |
| 8583 | } |
| 8584 | #endif // CONFIG_WARPED_MOTION |
| 8585 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8586 | if (!*skip_txfm_sb) { |
| 8587 | int64_t rdcosty = INT64_MAX; |
| 8588 | int is_cost_valid_uv = 0; |
| 8589 | |
| 8590 | // cost and distortion |
| 8591 | av1_subtract_plane(x, bsize, 0); |
| 8592 | #if CONFIG_VAR_TX |
| 8593 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
| 8594 | select_tx_type_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd); |
| 8595 | } else { |
| 8596 | int idx, idy; |
| 8597 | super_block_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd); |
| 8598 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 8599 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 8600 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 8601 | memset(x->blk_skip[0], rd_stats_y->skip, |
| 8602 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 8603 | } |
| 8604 | #else |
| 8605 | /* clang-format off */ |
| 8606 | super_block_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd); |
| 8607 | /* clang-format on */ |
| 8608 | #endif // CONFIG_VAR_TX |
| 8609 | |
| 8610 | if (rd_stats_y->rate == INT_MAX) { |
| 8611 | av1_invalid_rd_stats(rd_stats); |
| 8612 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8613 | if (mbmi->motion_mode != SIMPLE_TRANSLATION) { |
| 8614 | continue; |
| 8615 | } else { |
| 8616 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8617 | restore_dst_buf(xd, *orig_dst); |
| 8618 | return INT64_MAX; |
| 8619 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8620 | } |
| 8621 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8622 | } |
| 8623 | |
| 8624 | av1_merge_rd_stats(rd_stats, rd_stats_y); |
| 8625 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8626 | rdcosty = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
| 8627 | rdcosty = AOMMIN(rdcosty, RDCOST(x->rdmult, 0, rd_stats->sse)); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8628 | /* clang-format off */ |
| 8629 | #if CONFIG_VAR_TX |
| 8630 | is_cost_valid_uv = |
| 8631 | inter_block_uvrd(cpi, x, rd_stats_uv, bsize, ref_best_rd - rdcosty); |
| 8632 | #else |
| 8633 | is_cost_valid_uv = |
| 8634 | super_block_uvrd(cpi, x, rd_stats_uv, bsize, ref_best_rd - rdcosty); |
| 8635 | #endif // CONFIG_VAR_TX |
| 8636 | if (!is_cost_valid_uv) { |
| 8637 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8638 | continue; |
| 8639 | #else |
| 8640 | restore_dst_buf(xd, *orig_dst); |
| 8641 | return INT64_MAX; |
| 8642 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8643 | } |
| 8644 | /* clang-format on */ |
| 8645 | av1_merge_rd_stats(rd_stats, rd_stats_uv); |
| 8646 | #if CONFIG_RD_DEBUG |
| 8647 | // record transform block coefficient cost |
| 8648 | // TODO(angiebird): So far rd_debug tool only detects discrepancy of |
| 8649 | // coefficient cost. Therefore, it is fine to copy rd_stats into mbmi |
| 8650 | // here because we already collect the coefficient cost. Move this part to |
| 8651 | // other place when we need to compare non-coefficient cost. |
| 8652 | mbmi->rd_stats = *rd_stats; |
| 8653 | #endif // CONFIG_RD_DEBUG |
| 8654 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8655 | if (rd_stats->skip) { |
| 8656 | rd_stats->rate -= rd_stats_uv->rate + rd_stats_y->rate; |
| 8657 | rd_stats_y->rate = 0; |
| 8658 | rd_stats_uv->rate = 0; |
| 8659 | rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 8660 | mbmi->skip = 0; |
| 8661 | // here mbmi->skip temporarily plays a role as what this_skip2 does |
| 8662 | } else if (!xd->lossless[mbmi->segment_id] && |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8663 | (RDCOST(x->rdmult, |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8664 | rd_stats_y->rate + rd_stats_uv->rate + |
| 8665 | av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
| 8666 | rd_stats->dist) >= |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8667 | 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] | 8668 | rd_stats->sse))) { |
| 8669 | rd_stats->rate -= rd_stats_uv->rate + rd_stats_y->rate; |
| 8670 | rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 8671 | rd_stats->dist = rd_stats->sse; |
| 8672 | rd_stats_y->rate = 0; |
| 8673 | rd_stats_uv->rate = 0; |
| 8674 | mbmi->skip = 1; |
| 8675 | } else { |
| 8676 | rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 8677 | mbmi->skip = 0; |
| 8678 | } |
| 8679 | *disable_skip = 0; |
| 8680 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8681 | } else { |
| 8682 | x->skip = 1; |
| 8683 | *disable_skip = 1; |
| 8684 | mbmi->tx_size = tx_size_from_tx_mode(bsize, cm->tx_mode, 1); |
| 8685 | |
| 8686 | // The cost of skip bit needs to be added. |
| 8687 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8688 | mbmi->skip = 0; |
| 8689 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8690 | rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 8691 | |
| 8692 | rd_stats->dist = *skip_sse_sb; |
| 8693 | rd_stats->sse = *skip_sse_sb; |
| 8694 | rd_stats_y->rate = 0; |
| 8695 | rd_stats_uv->rate = 0; |
| 8696 | rd_stats->skip = 1; |
| 8697 | } |
| 8698 | |
| 8699 | #if CONFIG_GLOBAL_MOTION |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8700 | if (this_mode == ZEROMV || this_mode == ZERO_ZEROMV) { |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8701 | if (is_nontrans_global_motion(xd)) { |
| 8702 | rd_stats->rate -= rs; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 8703 | mbmi->interp_filters = av1_broadcast_interp_filter( |
| 8704 | av1_unswitchable_filter(cm->interp_filter)); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8705 | } |
| 8706 | } |
| 8707 | #endif // CONFIG_GLOBAL_MOTION |
| 8708 | |
| 8709 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 8710 | tmp_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 8711 | if (mbmi->motion_mode == SIMPLE_TRANSLATION || (tmp_rd < best_rd)) { |
| 8712 | best_mbmi = *mbmi; |
| 8713 | best_rd = tmp_rd; |
| 8714 | best_rd_stats = *rd_stats; |
| 8715 | best_rd_stats_y = *rd_stats_y; |
| 8716 | best_rd_stats_uv = *rd_stats_uv; |
| 8717 | #if CONFIG_VAR_TX |
| 8718 | for (int i = 0; i < MAX_MB_PLANE; ++i) |
| 8719 | memcpy(best_blk_skip[i], x->blk_skip[i], |
| 8720 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 8721 | #endif // CONFIG_VAR_TX |
| 8722 | best_xskip = x->skip; |
| 8723 | best_disable_skip = *disable_skip; |
| 8724 | } |
| 8725 | } |
| 8726 | |
| 8727 | if (best_rd == INT64_MAX) { |
| 8728 | av1_invalid_rd_stats(rd_stats); |
| 8729 | restore_dst_buf(xd, *orig_dst); |
| 8730 | return INT64_MAX; |
| 8731 | } |
| 8732 | *mbmi = best_mbmi; |
| 8733 | *rd_stats = best_rd_stats; |
| 8734 | *rd_stats_y = best_rd_stats_y; |
| 8735 | *rd_stats_uv = best_rd_stats_uv; |
| 8736 | #if CONFIG_VAR_TX |
| 8737 | for (int i = 0; i < MAX_MB_PLANE; ++i) |
| 8738 | memcpy(x->blk_skip[i], best_blk_skip[i], |
| 8739 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 8740 | #endif // CONFIG_VAR_TX |
| 8741 | x->skip = best_xskip; |
| 8742 | *disable_skip = best_disable_skip; |
| 8743 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 8744 | |
| 8745 | restore_dst_buf(xd, *orig_dst); |
| 8746 | return 0; |
| 8747 | } |
| 8748 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8749 | static int64_t handle_inter_mode(const AV1_COMP *const cpi, MACROBLOCK *x, |
| 8750 | BLOCK_SIZE bsize, RD_STATS *rd_stats, |
| 8751 | RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv, |
| 8752 | int *disable_skip, |
| 8753 | int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8754 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8755 | int_mv (*mode_comp_mv)[TOTAL_REFS_PER_FRAME], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8756 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8757 | int mi_row, int mi_col, |
| 8758 | HandleInterModeArgs *args, |
| 8759 | const int64_t ref_best_rd) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 8760 | const AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8761 | MACROBLOCKD *xd = &x->e_mbd; |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 8762 | MODE_INFO *mi = xd->mi[0]; |
| 8763 | MB_MODE_INFO *mbmi = &mi->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8764 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 8765 | const int is_comp_pred = has_second_ref(mbmi); |
| 8766 | const int this_mode = mbmi->mode; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8767 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8768 | const int is_singleref_comp_mode = is_inter_singleref_comp_mode(this_mode); |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8769 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8770 | int_mv *frame_mv = mode_mv[this_mode]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8771 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8772 | // The comp mv for the compound mode in single ref |
| 8773 | int_mv *frame_comp_mv = mode_comp_mv[this_mode]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8774 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8775 | int i; |
| 8776 | int refs[2] = { mbmi->ref_frame[0], |
| 8777 | (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; |
| 8778 | int_mv cur_mv[2]; |
| 8779 | int rate_mv = 0; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 8780 | int pred_exists = 1; |
Angie Chiang | 64a2fb8 | 2017-07-06 10:15:16 -0700 | [diff] [blame] | 8781 | #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT || CONFIG_INTERINTRA |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 8782 | const int bw = block_size_wide[bsize]; |
James Zern | 89a015b | 2017-08-08 12:39:00 -0400 | [diff] [blame] | 8783 | #endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8784 | int_mv single_newmv[TOTAL_REFS_PER_FRAME]; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 8785 | #if CONFIG_INTERINTRA |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 8786 | const int *const interintra_mode_cost = |
| 8787 | x->interintra_mode_cost[size_group_lookup[bsize]]; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 8788 | #endif // CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8789 | const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8790 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8791 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8792 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 8793 | #else |
| 8794 | 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] | 8795 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8796 | uint8_t *tmp_buf; |
| 8797 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8798 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8799 | int rate2_bmc_nocoeff; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8800 | MB_MODE_INFO best_bmc_mbmi; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 8801 | int rate_mv_bmc; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 8802 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 8803 | int64_t rd = INT64_MAX; |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 8804 | BUFFER_SET orig_dst, tmp_dst; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8805 | int rs = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8806 | |
| 8807 | int skip_txfm_sb = 0; |
| 8808 | int64_t skip_sse_sb = INT64_MAX; |
Yaowu Xu | b0d0d00 | 2016-11-22 09:26:43 -0800 | [diff] [blame] | 8809 | int16_t mode_ctx; |
Wei-Ting Lin | 85a8f70 | 2017-06-22 13:55:15 -0700 | [diff] [blame] | 8810 | #if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_MOTION_VAR |
| 8811 | // dummy fillers |
| 8812 | mbmi->ncobmc_mode[0] = NO_OVERLAP; |
| 8813 | mbmi->ncobmc_mode[1] = NO_OVERLAP; |
| 8814 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8815 | |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8816 | #if CONFIG_INTERINTRA |
| 8817 | int compmode_interintra_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8818 | mbmi->use_wedge_interintra = 0; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8819 | #endif |
| 8820 | #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
| 8821 | int compmode_interinter_cost = 0; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 8822 | mbmi->interinter_compound_type = COMPOUND_AVERAGE; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 8823 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8824 | |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 8825 | #if CONFIG_INTERINTRA |
| 8826 | if (!cm->allow_interintra_compound && is_comp_interintra_pred) |
| 8827 | return INT64_MAX; |
| 8828 | #endif // CONFIG_INTERINTRA |
| 8829 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8830 | // is_comp_interintra_pred implies !is_comp_pred |
| 8831 | assert(!is_comp_interintra_pred || (!is_comp_pred)); |
| 8832 | // is_comp_interintra_pred implies is_interintra_allowed(mbmi->sb_type) |
| 8833 | assert(!is_comp_interintra_pred || is_interintra_allowed(mbmi)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8834 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8835 | #if CONFIG_COMPOUND_SINGLEREF |
| 8836 | if (is_comp_pred || is_singleref_comp_mode) |
| 8837 | #else // !CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8838 | if (is_comp_pred) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8839 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8840 | mode_ctx = mbmi_ext->compound_mode_context[refs[0]]; |
| 8841 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 8842 | mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context, |
| 8843 | mbmi->ref_frame, bsize, -1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8844 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8845 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8846 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 8847 | tmp_buf = CONVERT_TO_BYTEPTR(tmp_buf_); |
| 8848 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 8849 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8850 | tmp_buf = tmp_buf_; |
David Barker | b8069f9 | 2016-11-18 14:49:56 +0000 | [diff] [blame] | 8851 | // Make sure that we didn't leave the plane destination buffers set |
| 8852 | // to tmp_buf at the end of the last iteration |
| 8853 | assert(xd->plane[0].dst.buf != tmp_buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8854 | |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 8855 | #if CONFIG_WARPED_MOTION |
| 8856 | mbmi->num_proj_ref[0] = 0; |
| 8857 | mbmi->num_proj_ref[1] = 0; |
| 8858 | #endif // CONFIG_WARPED_MOTION |
| 8859 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8860 | if (is_comp_pred) { |
| 8861 | if (frame_mv[refs[0]].as_int == INVALID_MV || |
| 8862 | frame_mv[refs[1]].as_int == INVALID_MV) |
| 8863 | return INT64_MAX; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8864 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8865 | } else if (is_singleref_comp_mode) { |
| 8866 | if (frame_mv[refs[0]].as_int == INVALID_MV || |
| 8867 | frame_comp_mv[refs[0]].as_int == INVALID_MV) |
| 8868 | return INT64_MAX; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8869 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8870 | } |
| 8871 | |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 8872 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8873 | if (have_newmv_in_inter_mode(this_mode)) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8874 | const int64_t ret_val = |
| 8875 | handle_newmv(cpi, x, bsize, mode_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8876 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8877 | mode_comp_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8878 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8879 | mi_row, mi_col, &rate_mv, single_newmv, args); |
Fergus Simpson | 4550963 | 2017-02-22 15:30:50 -0800 | [diff] [blame] | 8880 | if (ret_val != 0) |
| 8881 | return ret_val; |
| 8882 | else |
| 8883 | rd_stats->rate += rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8884 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8885 | for (i = 0; i < is_comp_pred + 1; ++i) { |
| 8886 | cur_mv[i] = frame_mv[refs[i]]; |
Zoe Liu | ed29ea2 | 2017-04-20 16:48:15 -0700 | [diff] [blame] | 8887 | // Clip "next_nearest" so that it does not extend to far out of image |
| 8888 | if (this_mode != NEWMV) clamp_mv2(&cur_mv[i].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8889 | 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] | 8890 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 8891 | } |
| 8892 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8893 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8894 | if (!is_comp_pred && is_singleref_comp_mode) { |
| 8895 | cur_mv[1] = frame_comp_mv[refs[0]]; |
| 8896 | // Clip "next_nearest" so that it does not extend to far out of image |
| 8897 | if (this_mode != NEWMV) clamp_mv2(&cur_mv[1].as_mv, xd); |
| 8898 | if (mv_check_bounds(&x->mv_limits, &cur_mv[1].as_mv)) return INT64_MAX; |
| 8899 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 8900 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8901 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8902 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 8903 | if (this_mode == NEAREST_NEARESTMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8904 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
| 8905 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 8906 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 8907 | |
| 8908 | for (i = 0; i < 2; ++i) { |
| 8909 | clamp_mv2(&cur_mv[i].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8910 | 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] | 8911 | mbmi->mv[i].as_int = cur_mv[i].as_int; |
| 8912 | } |
| 8913 | } |
| 8914 | } |
| 8915 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8916 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) { |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8917 | #if CONFIG_COMPOUND_SINGLEREF |
| 8918 | if (this_mode == NEAREST_NEWMV || // this_mode == SR_NEAREST_NEWMV || |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 8919 | this_mode == SR_NEAREST_NEARMV) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8920 | #else // !CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 8921 | if (this_mode == NEAREST_NEWMV) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8922 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 8923 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8924 | cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 8925 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8926 | #if CONFIG_AMVR |
| 8927 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv, |
| 8928 | cm->cur_frame_mv_precision_level); |
| 8929 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8930 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8931 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8932 | clamp_mv2(&cur_mv[0].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8933 | 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] | 8934 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 8935 | } |
| 8936 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 8937 | if (this_mode == NEW_NEARESTMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8938 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 8939 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8940 | #if CONFIG_AMVR |
| 8941 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv, |
| 8942 | cm->cur_frame_mv_precision_level); |
| 8943 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8944 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8945 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8946 | clamp_mv2(&cur_mv[1].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8947 | 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] | 8948 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 8949 | } |
| 8950 | } |
| 8951 | |
| 8952 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 8953 | int ref_mv_idx = mbmi->ref_mv_idx + 1; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8954 | if (this_mode == NEAR_NEWMV || |
| 8955 | #if CONFIG_COMPOUND_SINGLEREF |
| 8956 | this_mode == SR_NEAR_NEWMV || |
| 8957 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8958 | this_mode == NEAR_NEARMV) { |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 8959 | 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] | 8960 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8961 | #if CONFIG_AMVR |
| 8962 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv, |
| 8963 | cm->cur_frame_mv_precision_level); |
| 8964 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8965 | lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8966 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8967 | clamp_mv2(&cur_mv[0].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8968 | 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] | 8969 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 8970 | } |
| 8971 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 8972 | if (this_mode == NEW_NEARMV || |
| 8973 | #if CONFIG_COMPOUND_SINGLEREF |
| 8974 | this_mode == SR_NEAREST_NEARMV || |
| 8975 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8976 | this_mode == NEAR_NEARMV) { |
| 8977 | #if CONFIG_COMPOUND_SINGLEREF |
| 8978 | if (this_mode == SR_NEAREST_NEARMV) |
| 8979 | cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 8980 | else |
| 8981 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 8982 | 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] | 8983 | |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8984 | #if CONFIG_AMVR |
| 8985 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv, |
| 8986 | cm->cur_frame_mv_precision_level); |
| 8987 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8988 | lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv); |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 8989 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8990 | clamp_mv2(&cur_mv[1].as_mv, xd); |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 8991 | 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] | 8992 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
| 8993 | } |
| 8994 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 8995 | |
| 8996 | // do first prediction into the destination buffer. Do the next |
| 8997 | // prediction into a temporary buffer. Then keep track of which one |
| 8998 | // of these currently holds the best predictor, and use the other |
| 8999 | // one for future predictions. In the end, copy from tmp_buf to |
| 9000 | // dst if necessary. |
| 9001 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9002 | tmp_dst.plane[i] = tmp_buf + i * MAX_SB_SQUARE; |
| 9003 | tmp_dst.stride[i] = MAX_SB_SIZE; |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 9004 | } |
| 9005 | for (i = 0; i < MAX_MB_PLANE; i++) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9006 | orig_dst.plane[i] = xd->plane[i].dst.buf; |
| 9007 | orig_dst.stride[i] = xd->plane[i].dst.stride; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9008 | } |
| 9009 | |
| 9010 | // We don't include the cost of the second reference here, because there |
| 9011 | // are only three options: Last/Golden, ARF/Last or Golden/ARF, or in other |
| 9012 | // words if you present them in that order, the second one is always known |
| 9013 | // if the first is known. |
| 9014 | // |
| 9015 | // Under some circumstances we discount the cost of new mv mode to encourage |
| 9016 | // initiation of a motion field. |
| 9017 | if (discount_newmv_test(cpi, this_mode, frame_mv[refs[0]], mode_mv, |
| 9018 | refs[0])) { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9019 | rd_stats->rate += AOMMIN( |
| 9020 | cost_mv_ref(x, this_mode, mode_ctx), |
| 9021 | cost_mv_ref(x, is_comp_pred ? NEAREST_NEARESTMV : NEARESTMV, mode_ctx)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9022 | } else { |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9023 | rd_stats->rate += cost_mv_ref(x, this_mode, mode_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9024 | } |
| 9025 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9026 | if (RDCOST(x->rdmult, rd_stats->rate, 0) > ref_best_rd && |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9027 | mbmi->mode != NEARESTMV && mbmi->mode != NEAREST_NEARESTMV) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9028 | return INT64_MAX; |
| 9029 | |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 9030 | int64_t ret_val = interpolation_filter_search( |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9031 | x, cpi, bsize, mi_row, mi_col, &tmp_dst, &orig_dst, args->single_filter, |
| 9032 | &rd, &rs, &skip_txfm_sb, &skip_sse_sb); |
Fergus Simpson | de18e2b | 2017-03-01 20:12:34 -0800 | [diff] [blame] | 9033 | if (ret_val != 0) return ret_val; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9034 | |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9035 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9036 | best_bmc_mbmi = *mbmi; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 9037 | rate2_bmc_nocoeff = rd_stats->rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9038 | if (cm->interp_filter == SWITCHABLE) rate2_bmc_nocoeff += rs; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9039 | rate_mv_bmc = rate_mv; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9040 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9041 | |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9042 | #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9043 | #if CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9044 | if (is_comp_pred || is_singleref_comp_mode) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9045 | #else |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9046 | if (is_comp_pred) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9047 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 9048 | { |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 9049 | int rate_sum, rs2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9050 | int64_t dist_sum; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9051 | int64_t best_rd_compound = INT64_MAX, best_rd_cur = INT64_MAX; |
| 9052 | INTERINTER_COMPOUND_DATA best_compound_data; |
| 9053 | int_mv best_mv[2]; |
| 9054 | int best_tmp_rate_mv = rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9055 | int tmp_skip_txfm_sb; |
| 9056 | int64_t tmp_skip_sse_sb; |
Yaowu Xu | 5e8007f | 2017-06-28 12:39:18 -0700 | [diff] [blame] | 9057 | DECLARE_ALIGNED(16, uint8_t, pred0[2 * MAX_SB_SQUARE]); |
| 9058 | DECLARE_ALIGNED(16, uint8_t, pred1[2 * MAX_SB_SQUARE]); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9059 | uint8_t *preds0[1] = { pred0 }; |
| 9060 | uint8_t *preds1[1] = { pred1 }; |
| 9061 | int strides[1] = { bw }; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 9062 | int tmp_rate_mv; |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 9063 | int masked_compound_used = is_any_masked_compound_used(bsize); |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 9064 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
| 9065 | masked_compound_used = masked_compound_used && cm->allow_masked_compound; |
| 9066 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9067 | COMPOUND_TYPE cur_type; |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 9068 | int best_compmode_interinter_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9069 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9070 | best_mv[0].as_int = cur_mv[0].as_int; |
| 9071 | best_mv[1].as_int = cur_mv[1].as_int; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9072 | memset(&best_compound_data, 0, sizeof(best_compound_data)); |
| 9073 | #if CONFIG_COMPOUND_SEGMENT |
| 9074 | uint8_t tmp_mask_buf[2 * MAX_SB_SQUARE]; |
| 9075 | best_compound_data.seg_mask = tmp_mask_buf; |
| 9076 | #endif // CONFIG_COMPOUND_SEGMENT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9077 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9078 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9079 | // TODO(zoeliu): To further check whether the following setups are needed. |
| 9080 | // Single ref compound mode: Prepare the 2nd ref frame predictor the same as |
| 9081 | // the 1st one. |
| 9082 | if (!is_comp_pred && is_singleref_comp_mode) { |
| 9083 | xd->block_refs[1] = xd->block_refs[0]; |
| 9084 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 9085 | xd->plane[i].pre[1] = xd->plane[i].pre[0]; |
| 9086 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9087 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 9088 | |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 9089 | if (masked_compound_used) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9090 | // get inter predictors to use for masked compound modes |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9091 | av1_build_inter_predictors_for_planes_single_buf( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9092 | xd, bsize, 0, 0, mi_row, mi_col, 0, preds0, strides); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9093 | av1_build_inter_predictors_for_planes_single_buf( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9094 | xd, bsize, 0, 0, mi_row, mi_col, 1, preds1, strides); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9095 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9096 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9097 | for (cur_type = COMPOUND_AVERAGE; cur_type < COMPOUND_TYPES; cur_type++) { |
Debargha Mukherjee | 9e2c7a6 | 2017-05-23 21:18:42 -0700 | [diff] [blame] | 9098 | if (cur_type != COMPOUND_AVERAGE && !masked_compound_used) break; |
Yue Chen | e251825 | 2017-06-05 12:36:46 -0700 | [diff] [blame] | 9099 | if (!is_interinter_compound_used(cur_type, bsize)) continue; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 9100 | tmp_rate_mv = rate_mv; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9101 | best_rd_cur = INT64_MAX; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9102 | mbmi->interinter_compound_type = cur_type; |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 9103 | int masked_type_cost = 0; |
| 9104 | if (masked_compound_used) { |
| 9105 | #if CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT |
| 9106 | if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize)) |
| 9107 | masked_type_cost += av1_cost_literal(1); |
| 9108 | else |
| 9109 | #endif // CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT |
| 9110 | masked_type_cost += |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 9111 | x->compound_type_cost[bsize][mbmi->interinter_compound_type]; |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 9112 | } |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9113 | rs2 = av1_cost_literal(get_interinter_compound_type_bits( |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9114 | bsize, mbmi->interinter_compound_type)) + |
Sarah Parker | 680b9b1 | 2017-08-16 18:55:34 -0700 | [diff] [blame] | 9115 | masked_type_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9116 | |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9117 | switch (cur_type) { |
| 9118 | case COMPOUND_AVERAGE: |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 9119 | av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst, |
| 9120 | bsize); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9121 | av1_subtract_plane(x, bsize, 0); |
| 9122 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 9123 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, |
| 9124 | INT64_MAX); |
| 9125 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9126 | 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] | 9127 | best_rd_compound = best_rd_cur; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9128 | break; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9129 | #if CONFIG_WEDGE |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9130 | case COMPOUND_WEDGE: |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9131 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh && |
| 9132 | best_rd_compound / 3 < ref_best_rd) { |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 9133 | best_rd_cur = build_and_cost_compound_type( |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9134 | cpi, x, cur_mv, bsize, this_mode, rs2, rate_mv, &orig_dst, |
| 9135 | &tmp_rate_mv, preds0, preds1, strides, mi_row, mi_col); |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9136 | } |
| 9137 | break; |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9138 | #endif // CONFIG_WEDGE |
Sarah Parker | 2f6ce75 | 2016-12-08 15:26:46 -0800 | [diff] [blame] | 9139 | #if CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 9140 | case COMPOUND_SEG: |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 9141 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh && |
| 9142 | best_rd_compound / 3 < ref_best_rd) { |
Sarah Parker | ddcea39 | 2017-04-25 15:57:22 -0700 | [diff] [blame] | 9143 | best_rd_cur = build_and_cost_compound_type( |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 9144 | cpi, x, cur_mv, bsize, this_mode, rs2, rate_mv, &orig_dst, |
| 9145 | &tmp_rate_mv, preds0, preds1, strides, mi_row, mi_col); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 9146 | } |
| 9147 | break; |
Sarah Parker | 2f6ce75 | 2016-12-08 15:26:46 -0800 | [diff] [blame] | 9148 | #endif // CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9149 | default: assert(0); return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9150 | } |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 9151 | |
| 9152 | if (best_rd_cur < best_rd_compound) { |
| 9153 | best_rd_compound = best_rd_cur; |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9154 | #if CONFIG_WEDGE |
| 9155 | best_compound_data.wedge_index = mbmi->wedge_index; |
| 9156 | best_compound_data.wedge_sign = mbmi->wedge_sign; |
| 9157 | #endif // CONFIG_WEDGE |
| 9158 | #if CONFIG_COMPOUND_SEGMENT |
| 9159 | best_compound_data.mask_type = mbmi->mask_type; |
| 9160 | memcpy(best_compound_data.seg_mask, xd->seg_mask, |
| 9161 | 2 * MAX_SB_SQUARE * sizeof(uint8_t)); |
| 9162 | #endif // CONFIG_COMPOUND_SEGMENT |
| 9163 | best_compound_data.interinter_compound_type = |
| 9164 | mbmi->interinter_compound_type; |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 9165 | best_compmode_interinter_cost = rs2; |
Sarah Parker | 2e60488 | 2017-01-17 17:31:25 -0800 | [diff] [blame] | 9166 | if (have_newmv_in_inter_mode(this_mode)) { |
| 9167 | if (use_masked_motion_search(cur_type)) { |
| 9168 | best_tmp_rate_mv = tmp_rate_mv; |
| 9169 | best_mv[0].as_int = mbmi->mv[0].as_int; |
| 9170 | best_mv[1].as_int = mbmi->mv[1].as_int; |
| 9171 | } else { |
| 9172 | best_mv[0].as_int = cur_mv[0].as_int; |
| 9173 | best_mv[1].as_int = cur_mv[1].as_int; |
| 9174 | } |
| 9175 | } |
| 9176 | } |
| 9177 | // reset to original mvs for next iteration |
| 9178 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 9179 | mbmi->mv[1].as_int = cur_mv[1].as_int; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9180 | } |
Sarah Parker | 2d0e9b7 | 2017-05-04 01:34:16 +0000 | [diff] [blame] | 9181 | #if CONFIG_WEDGE |
| 9182 | mbmi->wedge_index = best_compound_data.wedge_index; |
| 9183 | mbmi->wedge_sign = best_compound_data.wedge_sign; |
| 9184 | #endif // CONFIG_WEDGE |
| 9185 | #if CONFIG_COMPOUND_SEGMENT |
| 9186 | mbmi->mask_type = best_compound_data.mask_type; |
| 9187 | memcpy(xd->seg_mask, best_compound_data.seg_mask, |
| 9188 | 2 * MAX_SB_SQUARE * sizeof(uint8_t)); |
| 9189 | #endif // CONFIG_COMPOUND_SEGMENT |
| 9190 | mbmi->interinter_compound_type = |
| 9191 | best_compound_data.interinter_compound_type; |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9192 | if (have_newmv_in_inter_mode(this_mode)) { |
| 9193 | mbmi->mv[0].as_int = best_mv[0].as_int; |
| 9194 | mbmi->mv[1].as_int = best_mv[1].as_int; |
| 9195 | xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int; |
| 9196 | 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] | 9197 | if (use_masked_motion_search(mbmi->interinter_compound_type)) { |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 9198 | rd_stats->rate += best_tmp_rate_mv - rate_mv; |
| 9199 | rate_mv = best_tmp_rate_mv; |
| 9200 | } |
| 9201 | } |
| 9202 | |
| 9203 | 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] | 9204 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9205 | return INT64_MAX; |
David Barker | b8069f9 | 2016-11-18 14:49:56 +0000 | [diff] [blame] | 9206 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9207 | |
| 9208 | pred_exists = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9209 | |
Yue Chen | a424551 | 2017-08-31 11:58:08 -0700 | [diff] [blame] | 9210 | compmode_interinter_cost = best_compmode_interinter_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9211 | } |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9212 | #endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9213 | |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9214 | #if CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9215 | if (is_comp_interintra_pred) { |
| 9216 | INTERINTRA_MODE best_interintra_mode = II_DC_PRED; |
| 9217 | int64_t best_interintra_rd = INT64_MAX; |
| 9218 | int rmode, rate_sum; |
| 9219 | int64_t dist_sum; |
| 9220 | int j; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9221 | int tmp_rate_mv = 0; |
| 9222 | int tmp_skip_txfm_sb; |
| 9223 | int64_t tmp_skip_sse_sb; |
| 9224 | DECLARE_ALIGNED(16, uint8_t, intrapred_[2 * MAX_SB_SQUARE]); |
| 9225 | uint8_t *intrapred; |
| 9226 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9227 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9228 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
| 9229 | intrapred = CONVERT_TO_BYTEPTR(intrapred_); |
| 9230 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9231 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9232 | intrapred = intrapred_; |
| 9233 | |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 9234 | mbmi->ref_frame[1] = NONE_FRAME; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9235 | for (j = 0; j < MAX_MB_PLANE; j++) { |
| 9236 | xd->plane[j].dst.buf = tmp_buf + j * MAX_SB_SQUARE; |
| 9237 | xd->plane[j].dst.stride = bw; |
| 9238 | } |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 9239 | 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] | 9240 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9241 | mbmi->ref_frame[1] = INTRA_FRAME; |
| 9242 | mbmi->use_wedge_interintra = 0; |
| 9243 | |
| 9244 | for (j = 0; j < INTERINTRA_MODES; ++j) { |
| 9245 | mbmi->interintra_mode = (INTERINTRA_MODE)j; |
| 9246 | rmode = interintra_mode_cost[mbmi->interintra_mode]; |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 9247 | av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, &orig_dst, |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9248 | intrapred, bw); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9249 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9250 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 9251 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9252 | rd = RDCOST(x->rdmult, tmp_rate_mv + rate_sum + rmode, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9253 | if (rd < best_interintra_rd) { |
| 9254 | best_interintra_rd = rd; |
| 9255 | best_interintra_mode = mbmi->interintra_mode; |
| 9256 | } |
| 9257 | } |
| 9258 | mbmi->interintra_mode = best_interintra_mode; |
| 9259 | rmode = interintra_mode_cost[mbmi->interintra_mode]; |
David Barker | 761b1ac | 2017-09-25 11:23:03 +0100 | [diff] [blame] | 9260 | av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, &orig_dst, |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9261 | intrapred, bw); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9262 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
| 9263 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9264 | rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 9265 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 9266 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9267 | rd = RDCOST(x->rdmult, rate_mv + rmode + rate_sum, dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9268 | best_interintra_rd = rd; |
| 9269 | |
| 9270 | 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] | 9271 | // Don't need to call restore_dst_buf here |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9272 | return INT64_MAX; |
| 9273 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9274 | #if CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9275 | if (is_interintra_wedge_used(bsize)) { |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9276 | int64_t best_interintra_rd_nowedge = INT64_MAX; |
| 9277 | int64_t best_interintra_rd_wedge = INT64_MAX; |
| 9278 | int_mv tmp_mv; |
| 9279 | int rwedge = av1_cost_bit(cm->fc->wedge_interintra_prob[bsize], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9280 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9281 | rd = RDCOST(x->rdmult, rmode + rate_mv + rwedge + rate_sum, dist_sum); |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 9282 | best_interintra_rd_nowedge = best_interintra_rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9283 | |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 9284 | // Disable wedge search if source variance is small |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9285 | if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh) { |
| 9286 | mbmi->use_wedge_interintra = 1; |
| 9287 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9288 | rwedge = av1_cost_literal(get_interintra_wedge_bits(bsize)) + |
| 9289 | av1_cost_bit(cm->fc->wedge_interintra_prob[bsize], 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9290 | |
| 9291 | best_interintra_rd_wedge = |
| 9292 | pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_); |
| 9293 | |
| 9294 | best_interintra_rd_wedge += |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9295 | RDCOST(x->rdmult, rmode + rate_mv + rwedge, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9296 | // Refine motion vector. |
| 9297 | if (have_newmv_in_inter_mode(this_mode)) { |
| 9298 | // get negative of mask |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9299 | const uint8_t *mask = av1_get_contiguous_soft_mask( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9300 | mbmi->interintra_wedge_index, 1, bsize); |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 9301 | tmp_mv.as_int = x->mbmi_ext->ref_mvs[refs[0]][0].as_int; |
| 9302 | compound_single_motion_search(cpi, x, bsize, &tmp_mv.as_mv, mi_row, |
| 9303 | mi_col, intrapred, mask, bw, |
Timothy B. Terriberry | 5d24b6f | 2017-06-15 13:39:35 -0700 | [diff] [blame] | 9304 | &tmp_rate_mv, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9305 | mbmi->mv[0].as_int = tmp_mv.as_int; |
Debargha Mukherjee | ad8be03 | 2017-05-09 15:28:45 -0700 | [diff] [blame] | 9306 | av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst, |
| 9307 | bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9308 | model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, |
| 9309 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9310 | rd = RDCOST(x->rdmult, rmode + tmp_rate_mv + rwedge + rate_sum, |
| 9311 | dist_sum); |
Yue Chen | f03907a | 2017-05-31 12:04:04 -0700 | [diff] [blame] | 9312 | if (rd >= best_interintra_rd_wedge) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9313 | tmp_mv.as_int = cur_mv[0].as_int; |
| 9314 | tmp_rate_mv = rate_mv; |
| 9315 | } |
| 9316 | } else { |
| 9317 | tmp_mv.as_int = cur_mv[0].as_int; |
| 9318 | tmp_rate_mv = rate_mv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9319 | av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9320 | } |
| 9321 | // Evaluate closer to true rd |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9322 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9323 | rd = |
| 9324 | estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum, |
| 9325 | &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX); |
| 9326 | if (rd != INT64_MAX) |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9327 | rd = RDCOST(x->rdmult, rmode + tmp_rate_mv + rwedge + rate_sum, |
| 9328 | dist_sum); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9329 | best_interintra_rd_wedge = rd; |
| 9330 | if (best_interintra_rd_wedge < best_interintra_rd_nowedge) { |
| 9331 | mbmi->use_wedge_interintra = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9332 | mbmi->mv[0].as_int = tmp_mv.as_int; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 9333 | rd_stats->rate += tmp_rate_mv - rate_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9334 | rate_mv = tmp_rate_mv; |
| 9335 | } else { |
| 9336 | mbmi->use_wedge_interintra = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9337 | mbmi->mv[0].as_int = cur_mv[0].as_int; |
| 9338 | } |
| 9339 | } else { |
| 9340 | mbmi->use_wedge_interintra = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9341 | } |
| 9342 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9343 | #endif // CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9344 | |
| 9345 | pred_exists = 0; |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9346 | compmode_interintra_cost = |
| 9347 | av1_cost_bit(cm->fc->interintra_prob[size_group_lookup[bsize]], 1) + |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 9348 | interintra_mode_cost[mbmi->interintra_mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9349 | if (is_interintra_wedge_used(bsize)) { |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9350 | compmode_interintra_cost += av1_cost_bit( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9351 | cm->fc->wedge_interintra_prob[bsize], mbmi->use_wedge_interintra); |
| 9352 | if (mbmi->use_wedge_interintra) { |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9353 | compmode_interintra_cost += |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9354 | av1_cost_literal(get_interintra_wedge_bits(bsize)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9355 | } |
| 9356 | } |
| 9357 | } else if (is_interintra_allowed(mbmi)) { |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9358 | compmode_interintra_cost = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9359 | av1_cost_bit(cm->fc->interintra_prob[size_group_lookup[bsize]], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9360 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 9361 | #endif // CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9362 | |
Angie Chiang | 75c2209 | 2016-10-25 12:19:16 -0700 | [diff] [blame] | 9363 | if (pred_exists == 0) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9364 | int tmp_rate; |
| 9365 | int64_t tmp_dist; |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 9366 | 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] | 9367 | model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, |
| 9368 | &tmp_dist, &skip_txfm_sb, &skip_sse_sb); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9369 | rd = RDCOST(x->rdmult, rs + tmp_rate, tmp_dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9370 | } |
| 9371 | |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 9372 | if (!is_comp_pred) |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 9373 | args->single_filter[this_mode][refs[0]] = |
| 9374 | av1_extract_interp_filter(mbmi->interp_filters, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9375 | |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9376 | if (args->modelled_rd != NULL) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9377 | if (is_comp_pred) { |
| 9378 | const int mode0 = compound_ref0_mode(this_mode); |
| 9379 | const int mode1 = compound_ref1_mode(this_mode); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9380 | const int64_t mrd = AOMMIN(args->modelled_rd[mode0][refs[0]], |
| 9381 | args->modelled_rd[mode1][refs[1]]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9382 | if (rd / 4 * 3 > mrd && ref_best_rd < INT64_MAX) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9383 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9384 | return INT64_MAX; |
| 9385 | } |
| 9386 | } else if (!is_comp_interintra_pred) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 9387 | args->modelled_rd[this_mode][refs[0]] = rd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9388 | } |
| 9389 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9390 | |
| 9391 | if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) { |
| 9392 | // if current pred_error modeled rd is substantially more than the best |
| 9393 | // so far, do not bother doing full rd |
| 9394 | if (rd / 2 > ref_best_rd) { |
David Barker | ac37fa3 | 2016-12-02 12:30:21 +0000 | [diff] [blame] | 9395 | restore_dst_buf(xd, orig_dst); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9396 | return INT64_MAX; |
| 9397 | } |
| 9398 | } |
| 9399 | |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9400 | #if CONFIG_INTERINTRA |
| 9401 | rd_stats->rate += compmode_interintra_cost; |
| 9402 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 9403 | rate2_bmc_nocoeff += compmode_interintra_cost; |
| 9404 | #endif |
| 9405 | #endif |
| 9406 | #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT |
| 9407 | rd_stats->rate += compmode_interinter_cost; |
| 9408 | #endif |
Yue Chen | 5e60654 | 2017-05-24 17:03:17 -0700 | [diff] [blame] | 9409 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9410 | ret_val = motion_mode_rd( |
| 9411 | cpi, x, bsize, rd_stats, rd_stats_y, rd_stats_uv, disable_skip, mode_mv, |
| 9412 | mi_row, mi_col, args, ref_best_rd, refs, rate_mv, |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 9413 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9414 | single_newmv, rate2_bmc_nocoeff, &best_bmc_mbmi, rate_mv_bmc, |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9415 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9416 | rs, &skip_txfm_sb, &skip_sse_sb, &orig_dst); |
Fergus Simpson | 10fb9fb | 2017-03-09 16:48:02 -0800 | [diff] [blame] | 9417 | if (ret_val != 0) return ret_val; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 9418 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9419 | return 0; // The rate-distortion cost will be re-calculated by caller. |
| 9420 | } |
| 9421 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9422 | #if CONFIG_INTRABC |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9423 | 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] | 9424 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9425 | int64_t best_rd) { |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9426 | const AV1_COMMON *const cm = &cpi->common; |
RogerZhou | ca86546 | 2017-10-05 15:06:27 -0700 | [diff] [blame] | 9427 | if (!av1_allow_intrabc(bsize, cm)) return INT64_MAX; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9428 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9429 | MACROBLOCKD *const xd = &x->e_mbd; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9430 | const TileInfo *tile = &xd->tile; |
| 9431 | MODE_INFO *const mi = xd->mi[0]; |
| 9432 | const int mi_row = -xd->mb_to_top_edge / (8 * MI_SIZE); |
| 9433 | const int mi_col = -xd->mb_to_left_edge / (8 * MI_SIZE); |
| 9434 | const int w = block_size_wide[bsize]; |
| 9435 | const int h = block_size_high[bsize]; |
| 9436 | const int sb_row = mi_row / MAX_MIB_SIZE; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9437 | const int sb_col = mi_col / MAX_MIB_SIZE; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9438 | |
Alex Converse | 44c2bad | 2017-05-11 09:36:10 -0700 | [diff] [blame] | 9439 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 9440 | MV_REFERENCE_FRAME ref_frame = INTRA_FRAME; |
| 9441 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 9442 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 9443 | mbmi_ext->ref_mv_stack[ref_frame], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 9444 | mbmi_ext->compound_mode_context, candidates, mi_row, mi_col, |
| 9445 | NULL, NULL, mbmi_ext->mode_context); |
Alex Converse | 44c2bad | 2017-05-11 09:36:10 -0700 | [diff] [blame] | 9446 | |
| 9447 | int_mv nearestmv, nearmv; |
| 9448 | av1_find_best_ref_mvs(0, candidates, &nearestmv, &nearmv); |
| 9449 | |
| 9450 | int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv; |
| 9451 | if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col); |
| 9452 | mbmi_ext->ref_mvs[INTRA_FRAME][0] = dv_ref; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9453 | |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9454 | struct buf_2d yv12_mb[MAX_MB_PLANE]; |
| 9455 | av1_setup_pred_block(xd, yv12_mb, xd->cur_buf, mi_row, mi_col, NULL, NULL); |
| 9456 | for (int i = 0; i < MAX_MB_PLANE; ++i) { |
| 9457 | xd->plane[i].pre[0] = yv12_mb[i]; |
| 9458 | } |
| 9459 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9460 | enum IntrabcMotionDirection { |
| 9461 | IBC_MOTION_ABOVE, |
| 9462 | IBC_MOTION_LEFT, |
| 9463 | IBC_MOTION_DIRECTIONS |
| 9464 | }; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9465 | |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9466 | MB_MODE_INFO *mbmi = &mi->mbmi; |
| 9467 | MB_MODE_INFO best_mbmi = *mbmi; |
| 9468 | RD_STATS best_rdcost = *rd_cost; |
| 9469 | int best_skip = x->skip; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9470 | |
| 9471 | for (enum IntrabcMotionDirection dir = IBC_MOTION_ABOVE; |
| 9472 | dir < IBC_MOTION_DIRECTIONS; ++dir) { |
| 9473 | const MvLimits tmp_mv_limits = x->mv_limits; |
| 9474 | switch (dir) { |
| 9475 | case IBC_MOTION_ABOVE: |
| 9476 | x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE; |
| 9477 | x->mv_limits.col_max = (tile->mi_col_end - mi_col) * MI_SIZE - w; |
| 9478 | x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE; |
| 9479 | x->mv_limits.row_max = (sb_row * MAX_MIB_SIZE - mi_row) * MI_SIZE - h; |
| 9480 | break; |
| 9481 | case IBC_MOTION_LEFT: |
| 9482 | x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE; |
| 9483 | x->mv_limits.col_max = (sb_col * MAX_MIB_SIZE - mi_col) * MI_SIZE - w; |
| 9484 | // TODO(aconverse@google.com): Minimize the overlap between above and |
| 9485 | // left areas. |
| 9486 | x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE; |
| 9487 | int bottom_coded_mi_edge = |
| 9488 | AOMMIN((sb_row + 1) * MAX_MIB_SIZE, tile->mi_row_end); |
| 9489 | x->mv_limits.row_max = (bottom_coded_mi_edge - mi_row) * MI_SIZE - h; |
| 9490 | break; |
| 9491 | default: assert(0); |
| 9492 | } |
| 9493 | assert(x->mv_limits.col_min >= tmp_mv_limits.col_min); |
| 9494 | assert(x->mv_limits.col_max <= tmp_mv_limits.col_max); |
| 9495 | assert(x->mv_limits.row_min >= tmp_mv_limits.row_min); |
| 9496 | assert(x->mv_limits.row_max <= tmp_mv_limits.row_max); |
| 9497 | av1_set_mv_search_range(&x->mv_limits, &dv_ref.as_mv); |
| 9498 | |
| 9499 | if (x->mv_limits.col_max < x->mv_limits.col_min || |
| 9500 | x->mv_limits.row_max < x->mv_limits.row_min) { |
| 9501 | x->mv_limits = tmp_mv_limits; |
| 9502 | continue; |
| 9503 | } |
| 9504 | |
| 9505 | int step_param = cpi->mv_step_param; |
| 9506 | MV mvp_full = dv_ref.as_mv; |
| 9507 | mvp_full.col >>= 3; |
| 9508 | mvp_full.row >>= 3; |
| 9509 | int sadpb = x->sadperbit16; |
| 9510 | int cost_list[5]; |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 9511 | #if CONFIG_HASH_ME |
RogerZhou | d15e7c1 | 2017-09-26 08:49:28 -0700 | [diff] [blame] | 9512 | int bestsme = av1_full_pixel_search( |
| 9513 | cpi, x, bsize, &mvp_full, step_param, sadpb, |
| 9514 | cond_cost_list(cpi, cost_list), &dv_ref.as_mv, INT_MAX, 1, |
| 9515 | (MI_SIZE * mi_col), (MI_SIZE * mi_row), 1); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 9516 | #else |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9517 | int bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, |
| 9518 | sadpb, cond_cost_list(cpi, cost_list), |
| 9519 | &dv_ref.as_mv, INT_MAX, 1); |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 9520 | #endif |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9521 | |
| 9522 | x->mv_limits = tmp_mv_limits; |
| 9523 | if (bestsme == INT_MAX) continue; |
| 9524 | mvp_full = x->best_mv.as_mv; |
| 9525 | MV dv = {.row = mvp_full.row * 8, .col = mvp_full.col * 8 }; |
| 9526 | if (mv_check_bounds(&x->mv_limits, &dv)) continue; |
| 9527 | if (!is_dv_valid(dv, tile, mi_row, mi_col, bsize)) continue; |
| 9528 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9529 | memset(&mbmi->palette_mode_info, 0, sizeof(mbmi->palette_mode_info)); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9530 | mbmi->use_intrabc = 1; |
| 9531 | mbmi->mode = DC_PRED; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 9532 | mbmi->uv_mode = UV_DC_PRED; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9533 | mbmi->mv[0].as_mv = dv; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 9534 | mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9535 | mbmi->skip = 0; |
| 9536 | x->skip = 0; |
| 9537 | 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] | 9538 | |
Alex Converse | d5d9b6c | 2017-05-23 15:23:45 -0700 | [diff] [blame] | 9539 | assert(x->mvcost == x->mv_cost_stack[0]); |
| 9540 | // TODO(aconverse@google.com): The full motion field defining discount |
| 9541 | // in MV_COST_WEIGHT is too large. Explore other values. |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9542 | 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] | 9543 | x->mvcost, MV_COST_WEIGHT_SUB); |
Hui Su | 6c8584f | 2017-09-14 15:37:02 -0700 | [diff] [blame] | 9544 | const int rate_mode = x->intrabc_cost[1]; |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9545 | RD_STATS rd_stats, rd_stats_uv; |
| 9546 | av1_subtract_plane(x, bsize, 0); |
| 9547 | super_block_yrd(cpi, x, &rd_stats, bsize, INT64_MAX); |
| 9548 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 9549 | av1_merge_rd_stats(&rd_stats, &rd_stats_uv); |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9550 | #if CONFIG_RD_DEBUG |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9551 | mbmi->rd_stats = rd_stats; |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9552 | #endif |
| 9553 | |
Alex Converse | e16b266 | 2017-05-24 14:00:00 -0700 | [diff] [blame] | 9554 | #if CONFIG_VAR_TX |
| 9555 | // TODO(aconverse@google.com): Evaluate allowing VAR TX on intrabc blocks |
| 9556 | const int width = block_size_wide[bsize] >> tx_size_wide_log2[0]; |
| 9557 | const int height = block_size_high[bsize] >> tx_size_high_log2[0]; |
| 9558 | int idx, idy; |
| 9559 | for (idy = 0; idy < height; ++idy) |
| 9560 | for (idx = 0; idx < width; ++idx) |
| 9561 | mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size; |
| 9562 | mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size); |
| 9563 | #endif // CONFIG_VAR_TX |
| 9564 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9565 | const aom_prob skip_prob = av1_get_skip_prob(cm, xd); |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9566 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9567 | RD_STATS rdc_noskip; |
| 9568 | av1_init_rd_stats(&rdc_noskip); |
| 9569 | rdc_noskip.rate = |
| 9570 | rate_mode + rate_mv + rd_stats.rate + av1_cost_bit(skip_prob, 0); |
| 9571 | rdc_noskip.dist = rd_stats.dist; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9572 | rdc_noskip.rdcost = RDCOST(x->rdmult, rdc_noskip.rate, rdc_noskip.dist); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9573 | if (rdc_noskip.rdcost < best_rd) { |
| 9574 | best_rd = rdc_noskip.rdcost; |
| 9575 | best_mbmi = *mbmi; |
| 9576 | best_skip = x->skip; |
| 9577 | best_rdcost = rdc_noskip; |
| 9578 | } |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9579 | |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9580 | x->skip = 1; |
| 9581 | mbmi->skip = 1; |
| 9582 | RD_STATS rdc_skip; |
| 9583 | av1_init_rd_stats(&rdc_skip); |
| 9584 | rdc_skip.rate = rate_mode + rate_mv + av1_cost_bit(skip_prob, 1); |
| 9585 | rdc_skip.dist = rd_stats.sse; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9586 | rdc_skip.rdcost = RDCOST(x->rdmult, rdc_skip.rate, rdc_skip.dist); |
Alex Converse | 861d707 | 2017-05-15 14:19:53 -0700 | [diff] [blame] | 9587 | if (rdc_skip.rdcost < best_rd) { |
| 9588 | best_rd = rdc_skip.rdcost; |
| 9589 | best_mbmi = *mbmi; |
| 9590 | best_skip = x->skip; |
| 9591 | best_rdcost = rdc_skip; |
| 9592 | } |
Alex Converse | 3d8adf3 | 2017-04-24 12:35:42 -0700 | [diff] [blame] | 9593 | } |
| 9594 | *mbmi = best_mbmi; |
| 9595 | *rd_cost = best_rdcost; |
| 9596 | x->skip = best_skip; |
| 9597 | return best_rd; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9598 | } |
| 9599 | #endif // CONFIG_INTRABC |
| 9600 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9601 | 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] | 9602 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9603 | PICK_MODE_CONTEXT *ctx, int64_t best_rd) { |
| 9604 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9605 | MACROBLOCKD *const xd = &x->e_mbd; |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9606 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9607 | struct macroblockd_plane *const pd = xd->plane; |
| 9608 | int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0; |
| 9609 | int y_skip = 0, uv_skip = 0; |
| 9610 | int64_t dist_y = 0, dist_uv = 0; |
| 9611 | TX_SIZE max_uv_tx_size; |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 9612 | const int unify_bsize = CONFIG_CB4X4; |
| 9613 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9614 | ctx->skip = 0; |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9615 | mbmi->ref_frame[0] = INTRA_FRAME; |
| 9616 | mbmi->ref_frame[1] = NONE_FRAME; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9617 | #if CONFIG_INTRABC |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9618 | mbmi->use_intrabc = 0; |
| 9619 | mbmi->mv[0].as_int = 0; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9620 | #endif // CONFIG_INTRABC |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9621 | |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9622 | const int64_t intra_yrd = |
| 9623 | (bsize >= BLOCK_8X8 || unify_bsize) |
| 9624 | ? rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly, &dist_y, |
| 9625 | &y_skip, bsize, best_rd) |
| 9626 | : rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly, |
| 9627 | &dist_y, &y_skip, best_rd); |
| 9628 | |
| 9629 | if (intra_yrd < best_rd) { |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9630 | #if CONFIG_CFL |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 9631 | #if CONFIG_CB4X4 |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 9632 | // Only store reconstructed luma when there's chroma RDO. When there's no |
| 9633 | // chroma RDO, the reconstructed luma will be stored in encode_superblock(). |
| 9634 | xd->cfl->store_y = !x->skip_chroma_rd; |
Luc Trudeau | 780d249 | 2017-06-15 22:26:41 -0400 | [diff] [blame] | 9635 | #else |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 9636 | xd->cfl->store_y = 1; |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 9637 | #endif // CONFIG_CB4X4 |
Luc Trudeau | fcca37a | 2017-08-14 15:05:07 -0400 | [diff] [blame] | 9638 | if (xd->cfl->store_y) { |
Luc Trudeau | b05eeae | 2017-08-18 15:14:30 -0400 | [diff] [blame] | 9639 | // Perform one extra call to txfm_rd_in_plane(), with the values chosen |
| 9640 | // during luma RDO, so we can store reconstructed luma values |
| 9641 | RD_STATS this_rd_stats; |
Luc Trudeau | 32306c2 | 2017-08-14 14:44:26 -0400 | [diff] [blame] | 9642 | txfm_rd_in_plane(x, cpi, &this_rd_stats, INT64_MAX, AOM_PLANE_Y, |
| 9643 | mbmi->sb_type, mbmi->tx_size, |
| 9644 | cpi->sf.use_fast_coef_costing); |
Luc Trudeau | fcca37a | 2017-08-14 15:05:07 -0400 | [diff] [blame] | 9645 | xd->cfl->store_y = 0; |
Luc Trudeau | 32306c2 | 2017-08-14 14:44:26 -0400 | [diff] [blame] | 9646 | } |
Luc Trudeau | 6e1cd78 | 2017-06-21 13:52:36 -0400 | [diff] [blame] | 9647 | #endif // CONFIG_CFL |
Luc Trudeau | 14fc504 | 2017-06-16 12:40:29 -0400 | [diff] [blame] | 9648 | max_uv_tx_size = uv_txsize_lookup[bsize][mbmi->tx_size][pd[1].subsampling_x] |
| 9649 | [pd[1].subsampling_y]; |
| 9650 | init_sbuv_mode(mbmi); |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 9651 | #if CONFIG_CB4X4 |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9652 | if (!x->skip_chroma_rd) |
| 9653 | rd_pick_intra_sbuv_mode(cpi, x, &rate_uv, &rate_uv_tokenonly, &dist_uv, |
| 9654 | &uv_skip, bsize, max_uv_tx_size); |
Jingning Han | 271bb2c | 2016-12-14 12:34:46 -0800 | [diff] [blame] | 9655 | #else |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9656 | rd_pick_intra_sbuv_mode(cpi, x, &rate_uv, &rate_uv_tokenonly, &dist_uv, |
| 9657 | &uv_skip, AOMMAX(BLOCK_8X8, bsize), max_uv_tx_size); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 9658 | #endif // CONFIG_CB4X4 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9659 | |
Rupert Swarbrick | c6cc1f5 | 2017-10-04 11:52:12 +0100 | [diff] [blame] | 9660 | if (y_skip && (uv_skip || x->skip_chroma_rd)) { |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9661 | rd_cost->rate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly + |
| 9662 | av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 9663 | rd_cost->dist = dist_y + dist_uv; |
| 9664 | } else { |
| 9665 | rd_cost->rate = |
| 9666 | rate_y + rate_uv + av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 9667 | rd_cost->dist = dist_y + dist_uv; |
| 9668 | } |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9669 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9670 | } else { |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9671 | rd_cost->rate = INT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9672 | } |
| 9673 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9674 | #if CONFIG_INTRABC |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9675 | if (rd_cost->rate != INT_MAX && rd_cost->rdcost < best_rd) |
| 9676 | best_rd = rd_cost->rdcost; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9677 | if (rd_pick_intrabc_mode_sb(cpi, x, rd_cost, bsize, best_rd) < best_rd) { |
| 9678 | 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] | 9679 | assert(rd_cost->rate != INT_MAX); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9680 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9681 | } |
| 9682 | #endif |
Alex Converse | d1b6fad | 2017-04-26 15:39:37 -0700 | [diff] [blame] | 9683 | if (rd_cost->rate == INT_MAX) return; |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 9684 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9685 | ctx->mic = *xd->mi[0]; |
| 9686 | ctx->mbmi_ext = *x->mbmi_ext; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9687 | } |
| 9688 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9689 | // Do we have an internal image edge (e.g. formatting bars). |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9690 | int av1_internal_image_edge(const AV1_COMP *cpi) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9691 | return (cpi->oxcf.pass == 2) && |
| 9692 | ((cpi->twopass.this_frame_stats.inactive_zone_rows > 0) || |
| 9693 | (cpi->twopass.this_frame_stats.inactive_zone_cols > 0)); |
| 9694 | } |
| 9695 | |
| 9696 | // Checks to see if a super block is on a horizontal image edge. |
| 9697 | // In most cases this is the "real" edge unless there are formatting |
| 9698 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9699 | 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] | 9700 | int top_edge = 0; |
| 9701 | int bottom_edge = cpi->common.mi_rows; |
| 9702 | int is_active_h_edge = 0; |
| 9703 | |
| 9704 | // For two pass account for any formatting bars detected. |
| 9705 | if (cpi->oxcf.pass == 2) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9706 | const TWO_PASS *const twopass = &cpi->twopass; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9707 | |
| 9708 | // The inactive region is specified in MBs not mi units. |
| 9709 | // The image edge is in the following MB row. |
| 9710 | top_edge += (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
| 9711 | |
| 9712 | bottom_edge -= (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9713 | bottom_edge = AOMMAX(top_edge, bottom_edge); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9714 | } |
| 9715 | |
| 9716 | if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) || |
| 9717 | ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) { |
| 9718 | is_active_h_edge = 1; |
| 9719 | } |
| 9720 | return is_active_h_edge; |
| 9721 | } |
| 9722 | |
| 9723 | // Checks to see if a super block is on a vertical image edge. |
| 9724 | // In most cases this is the "real" edge unless there are formatting |
| 9725 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9726 | 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] | 9727 | int left_edge = 0; |
| 9728 | int right_edge = cpi->common.mi_cols; |
| 9729 | int is_active_v_edge = 0; |
| 9730 | |
| 9731 | // For two pass account for any formatting bars detected. |
| 9732 | if (cpi->oxcf.pass == 2) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9733 | const TWO_PASS *const twopass = &cpi->twopass; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9734 | |
| 9735 | // The inactive region is specified in MBs not mi units. |
| 9736 | // The image edge is in the following MB row. |
| 9737 | left_edge += (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
| 9738 | |
| 9739 | right_edge -= (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9740 | right_edge = AOMMAX(left_edge, right_edge); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9741 | } |
| 9742 | |
| 9743 | if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) || |
| 9744 | ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) { |
| 9745 | is_active_v_edge = 1; |
| 9746 | } |
| 9747 | return is_active_v_edge; |
| 9748 | } |
| 9749 | |
| 9750 | // Checks to see if a super block is at the edge of the active image. |
| 9751 | // In most cases this is the "real" edge unless there are formatting |
| 9752 | // bars embedded in the stream. |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9753 | 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] | 9754 | return av1_active_h_edge(cpi, mi_row, cpi->common.mib_size) || |
| 9755 | av1_active_v_edge(cpi, mi_col, cpi->common.mib_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9756 | } |
| 9757 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9758 | 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] | 9759 | MACROBLOCKD *const xd = &x->e_mbd; |
| 9760 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 9761 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 9762 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 9763 | assert(bsize >= BLOCK_8X8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9764 | int src_stride = x->plane[1].src.stride; |
| 9765 | const uint8_t *const src_u = x->plane[1].src.buf; |
| 9766 | const uint8_t *const src_v = x->plane[2].src.buf; |
| 9767 | float *const data = x->palette_buffer->kmeans_data_buf; |
| 9768 | float centroids[2 * PALETTE_MAX_SIZE]; |
| 9769 | uint8_t *const color_map = xd->plane[1].color_index_map; |
| 9770 | int r, c; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9771 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9772 | const uint16_t *const src_u16 = CONVERT_TO_SHORTPTR(src_u); |
| 9773 | const uint16_t *const src_v16 = CONVERT_TO_SHORTPTR(src_v); |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9774 | #endif // CONFIG_HIGHBITDEPTH |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 9775 | int plane_block_width, plane_block_height, rows, cols; |
| 9776 | av1_get_block_dimensions(bsize, 1, xd, &plane_block_width, |
| 9777 | &plane_block_height, &rows, &cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9778 | (void)cpi; |
| 9779 | |
| 9780 | for (r = 0; r < rows; ++r) { |
| 9781 | for (c = 0; c < cols; ++c) { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9782 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9783 | if (cpi->common.use_highbitdepth) { |
| 9784 | data[(r * cols + c) * 2] = src_u16[r * src_stride + c]; |
| 9785 | data[(r * cols + c) * 2 + 1] = src_v16[r * src_stride + c]; |
| 9786 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9787 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9788 | data[(r * cols + c) * 2] = src_u[r * src_stride + c]; |
| 9789 | data[(r * cols + c) * 2 + 1] = src_v[r * src_stride + c]; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9790 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9791 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 9792 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9793 | } |
| 9794 | } |
| 9795 | |
| 9796 | for (r = 1; r < 3; ++r) { |
| 9797 | for (c = 0; c < pmi->palette_size[1]; ++c) { |
| 9798 | centroids[c * 2 + r - 1] = pmi->palette_colors[r * PALETTE_MAX_SIZE + c]; |
| 9799 | } |
| 9800 | } |
| 9801 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9802 | av1_calc_indices(data, centroids, color_map, rows * cols, |
| 9803 | pmi->palette_size[1], 2); |
Urvang Joshi | 56ba91b | 2017-01-10 13:22:09 -0800 | [diff] [blame] | 9804 | extend_palette_color_map(color_map, cols, rows, plane_block_width, |
| 9805 | plane_block_height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9806 | } |
| 9807 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9808 | #if CONFIG_FILTER_INTRA |
| 9809 | static void pick_filter_intra_interframe( |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 9810 | const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row, |
| 9811 | int mi_col, int *rate_uv_intra, int *rate_uv_tokenonly, int64_t *dist_uv, |
| 9812 | int *skip_uv, UV_PREDICTION_MODE *mode_uv, |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 9813 | FILTER_INTRA_MODE_INFO *filter_intra_mode_info_uv, |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9814 | #if CONFIG_EXT_INTRA |
| 9815 | int8_t *uv_angle_delta, |
| 9816 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | c6300aa | 2017-06-01 14:46:23 -0700 | [diff] [blame] | 9817 | PALETTE_MODE_INFO *pmi_uv, int palette_ctx, int skip_mask, |
| 9818 | unsigned int *ref_costs_single, int64_t *best_rd, int64_t *best_intra_rd, |
| 9819 | PREDICTION_MODE *best_intra_mode, int *best_mode_index, int *best_skip2, |
| 9820 | int *best_mode_skippable, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9821 | #if CONFIG_SUPERTX |
| 9822 | int *returnrate_nocoef, |
| 9823 | #endif // CONFIG_SUPERTX |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 9824 | 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] | 9825 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9826 | MACROBLOCKD *const xd = &x->e_mbd; |
| 9827 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 9828 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 9829 | const int try_palette = can_use_palette(cpi, mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9830 | int rate2 = 0, rate_y = INT_MAX, skippable = 0, rate_uv, rate_dummy, i; |
| 9831 | int dc_mode_index; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9832 | 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] | 9833 | int64_t distortion2 = 0, distortion_y = 0, this_rd = *best_rd; |
| 9834 | int64_t distortion_uv, model_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9835 | TX_SIZE uv_tx; |
| 9836 | |
| 9837 | for (i = 0; i < MAX_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9838 | if (av1_mode_order[i].mode == DC_PRED && |
| 9839 | av1_mode_order[i].ref_frame[0] == INTRA_FRAME) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9840 | break; |
| 9841 | dc_mode_index = i; |
| 9842 | assert(i < MAX_MODES); |
| 9843 | |
| 9844 | // TODO(huisu): use skip_mask for further speedup. |
| 9845 | (void)skip_mask; |
| 9846 | mbmi->mode = DC_PRED; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 9847 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9848 | mbmi->ref_frame[0] = INTRA_FRAME; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 9849 | mbmi->ref_frame[1] = NONE_FRAME; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9850 | if (!rd_pick_filter_intra_sby(cpi, x, &rate_dummy, &rate_y, &distortion_y, |
| 9851 | &skippable, bsize, intra_mode_cost[mbmi->mode], |
hui su | 8f4cc0a | 2017-01-13 15:14:49 -0800 | [diff] [blame] | 9852 | &this_rd, &model_rd, 0)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9853 | return; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9854 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9855 | if (rate_y == INT_MAX) return; |
| 9856 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 9857 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x] |
| 9858 | [xd->plane[1].subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9859 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 9860 | 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] | 9861 | &rate_uv_tokenonly[uv_tx], &dist_uv[uv_tx], |
| 9862 | &skip_uv[uv_tx], &mode_uv[uv_tx]); |
| 9863 | if (cm->allow_screen_content_tools) pmi_uv[uv_tx] = *pmi; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9864 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 9865 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9866 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9867 | #endif // CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9868 | } |
| 9869 | |
| 9870 | rate_uv = rate_uv_tokenonly[uv_tx]; |
| 9871 | distortion_uv = dist_uv[uv_tx]; |
| 9872 | skippable = skippable && skip_uv[uv_tx]; |
| 9873 | mbmi->uv_mode = mode_uv[uv_tx]; |
| 9874 | if (cm->allow_screen_content_tools) { |
| 9875 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
| 9876 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 9877 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 9878 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
| 9879 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9880 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9881 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9882 | #endif // CONFIG_EXT_INTRA |
| 9883 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 9884 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 9885 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 9886 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 9887 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9888 | } |
| 9889 | |
| 9890 | rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 9891 | x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode]; |
Rupert Swarbrick | 6f9cd94 | 2017-08-02 15:57:18 +0100 | [diff] [blame] | 9892 | if (try_palette && mbmi->mode == DC_PRED) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9893 | rate2 += av1_cost_bit( |
| 9894 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9895 | |
| 9896 | if (!xd->lossless[mbmi->segment_id]) { |
| 9897 | // super_block_yrd above includes the cost of the tx_size in the |
| 9898 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 9899 | // (prediction granularity), so we account for it in the full rate, |
| 9900 | // not the tokenonly rate. |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 9901 | rate_y -= tx_size_cost(cpi, x, bsize, mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9902 | } |
| 9903 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9904 | rate2 += av1_cost_bit(cm->fc->filter_intra_probs[0], |
| 9905 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0]); |
| 9906 | rate2 += write_uniform_cost( |
| 9907 | FILTER_INTRA_MODES, mbmi->filter_intra_mode_info.filter_intra_mode[0]); |
| 9908 | #if CONFIG_EXT_INTRA |
Luc Trudeau | f24a35f | 2017-09-11 11:56:33 -0400 | [diff] [blame] | 9909 | if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize) && |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 9910 | av1_use_angle_delta(bsize)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 9911 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 9912 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9913 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9914 | #endif // CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9915 | if (mbmi->mode == DC_PRED) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9916 | rate2 += |
| 9917 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], |
| 9918 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1]); |
| 9919 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1]) |
| 9920 | rate2 += |
| 9921 | write_uniform_cost(FILTER_INTRA_MODES, |
| 9922 | mbmi->filter_intra_mode_info.filter_intra_mode[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9923 | } |
| 9924 | distortion2 = distortion_y + distortion_uv; |
Jingning Han | 18c53c8 | 2017-02-17 14:49:57 -0800 | [diff] [blame] | 9925 | av1_encode_intra_block_plane((AV1_COMMON *)cm, x, bsize, 0, 0, mi_row, |
| 9926 | mi_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9927 | |
| 9928 | rate2 += ref_costs_single[INTRA_FRAME]; |
| 9929 | |
| 9930 | if (skippable) { |
| 9931 | rate2 -= (rate_y + rate_uv); |
| 9932 | rate_y = 0; |
| 9933 | rate_uv = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9934 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9935 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9936 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9937 | } |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 9938 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9939 | |
| 9940 | if (this_rd < *best_intra_rd) { |
| 9941 | *best_intra_rd = this_rd; |
| 9942 | *best_intra_mode = mbmi->mode; |
| 9943 | } |
| 9944 | for (i = 0; i < REFERENCE_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9945 | best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9946 | |
| 9947 | if (this_rd < *best_rd) { |
| 9948 | *best_mode_index = dc_mode_index; |
| 9949 | mbmi->mv[0].as_int = 0; |
| 9950 | rd_cost->rate = rate2; |
| 9951 | #if CONFIG_SUPERTX |
| 9952 | if (x->skip) |
| 9953 | *returnrate_nocoef = rate2; |
| 9954 | else |
| 9955 | *returnrate_nocoef = rate2 - rate_y - rate_uv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9956 | *returnrate_nocoef -= av1_cost_bit(av1_get_skip_prob(cm, xd), skippable); |
| 9957 | *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd), |
| 9958 | mbmi->ref_frame[0] != INTRA_FRAME); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9959 | #endif // CONFIG_SUPERTX |
| 9960 | rd_cost->dist = distortion2; |
| 9961 | rd_cost->rdcost = this_rd; |
| 9962 | *best_rd = this_rd; |
| 9963 | *best_mbmode = *mbmi; |
| 9964 | *best_skip2 = 0; |
| 9965 | *best_mode_skippable = skippable; |
| 9966 | } |
| 9967 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 9968 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9969 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9970 | #if CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9971 | static void calc_target_weighted_pred(const AV1_COMMON *cm, const MACROBLOCK *x, |
| 9972 | const MACROBLOCKD *xd, int mi_row, |
| 9973 | int mi_col, const uint8_t *above, |
| 9974 | int above_stride, const uint8_t *left, |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 9975 | int left_stride); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 9976 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9977 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9978 | 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] | 9979 | MACROBLOCK *x, int mi_row, int mi_col, |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 9980 | RD_STATS *rd_cost, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9981 | #if CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9982 | int *returnrate_nocoef, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9983 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 9984 | BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, |
| 9985 | int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 9986 | const AV1_COMMON *const cm = &cpi->common; |
| 9987 | const RD_OPT *const rd_opt = &cpi->rd; |
| 9988 | const SPEED_FEATURES *const sf = &cpi->sf; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9989 | MACROBLOCKD *const xd = &x->e_mbd; |
| 9990 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 9991 | const int try_palette = can_use_palette(cpi, mbmi); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 9992 | PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
| 9993 | MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 9994 | const struct segmentation *const seg = &cm->seg; |
| 9995 | PREDICTION_MODE this_mode; |
| 9996 | MV_REFERENCE_FRAME ref_frame, second_ref_frame; |
| 9997 | unsigned char segment_id = mbmi->segment_id; |
| 9998 | int comp_pred, i, k; |
| 9999 | int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10000 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10001 | int_mv frame_comp_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10002 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10003 | struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10004 | int_mv single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } }; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10005 | int single_newmv_rate[TOTAL_REFS_PER_FRAME] = { 0 }; |
| 10006 | int64_t modelled_rd[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10007 | static const int flag_list[TOTAL_REFS_PER_FRAME] = { |
| 10008 | 0, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10009 | AOM_LAST_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10010 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10011 | AOM_LAST2_FLAG, |
| 10012 | AOM_LAST3_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10013 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10014 | AOM_GOLD_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10015 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10016 | AOM_BWD_FLAG, |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 10017 | AOM_ALT2_FLAG, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10018 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10019 | AOM_ALT_FLAG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10020 | }; |
| 10021 | int64_t best_rd = best_rd_so_far; |
| 10022 | int best_rate_y = INT_MAX, best_rate_uv = INT_MAX; |
| 10023 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 10024 | int64_t best_pred_rd[REFERENCE_MODES]; |
| 10025 | MB_MODE_INFO best_mbmode; |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 10026 | int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 10027 | 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] | 10028 | int best_mode_skippable = 0; |
| 10029 | int midx, best_mode_index = -1; |
| 10030 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10031 | #if CONFIG_EXT_COMP_REFS |
| 10032 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME][TOTAL_REFS_PER_FRAME]; |
| 10033 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10034 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10035 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10036 | aom_prob comp_mode_p; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10037 | int64_t best_intra_rd = INT64_MAX; |
| 10038 | unsigned int best_pred_sse = UINT_MAX; |
| 10039 | PREDICTION_MODE best_intra_mode = DC_PRED; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10040 | int rate_uv_intra[TX_SIZES_ALL], rate_uv_tokenonly[TX_SIZES_ALL]; |
| 10041 | int64_t dist_uvs[TX_SIZES_ALL]; |
| 10042 | int skip_uvs[TX_SIZES_ALL]; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10043 | UV_PREDICTION_MODE mode_uv[TX_SIZES_ALL]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10044 | PALETTE_MODE_INFO pmi_uv[TX_SIZES_ALL]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10045 | #if CONFIG_EXT_INTRA |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10046 | int8_t uv_angle_delta[TX_SIZES_ALL]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10047 | int is_directional_mode, angle_stats_ready = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10048 | uint8_t directional_mode_skip_mask[INTRA_MODES]; |
| 10049 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10050 | #if CONFIG_FILTER_INTRA |
| 10051 | int8_t dc_skipped = 1; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10052 | FILTER_INTRA_MODE_INFO filter_intra_mode_info_uv[TX_SIZES_ALL]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10053 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10054 | const int intra_cost_penalty = av1_get_intra_cost_penalty( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10055 | cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10056 | 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] | 10057 | int best_skip2 = 0; |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 10058 | uint16_t ref_frame_skip_mask[2] = { 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10059 | uint32_t mode_skip_mask[TOTAL_REFS_PER_FRAME] = { 0 }; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10060 | #if CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10061 | MV_REFERENCE_FRAME best_single_inter_ref = LAST_FRAME; |
| 10062 | int64_t best_single_inter_rd = INT64_MAX; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10063 | #endif // CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10064 | int mode_skip_start = sf->mode_skip_start + 1; |
| 10065 | const int *const rd_threshes = rd_opt->threshes[segment_id][bsize]; |
| 10066 | const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize]; |
| 10067 | int64_t mode_threshold[MAX_MODES]; |
| 10068 | int *mode_map = tile_data->mode_map[bsize]; |
| 10069 | const int mode_search_skip_flags = sf->mode_search_skip_flags; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10070 | #if CONFIG_PVQ |
| 10071 | od_rollback_buffer pre_buf; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10072 | #endif // CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10073 | |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10074 | HandleInterModeArgs args = { |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10075 | #if CONFIG_MOTION_VAR |
| 10076 | { NULL }, |
| 10077 | { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }, |
| 10078 | { NULL }, |
| 10079 | { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }, |
| 10080 | #endif // CONFIG_MOTION_VAR |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10081 | NULL, |
| 10082 | NULL, |
| 10083 | NULL, |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 10084 | { { 0 } }, |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10085 | }; |
| 10086 | |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 10087 | const int rows = block_size_high[bsize]; |
| 10088 | const int cols = block_size_wide[bsize]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10089 | int palette_ctx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10090 | const MODE_INFO *above_mi = xd->above_mi; |
| 10091 | const MODE_INFO *left_mi = xd->left_mi; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10092 | #if CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10093 | int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 10094 | int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 10095 | int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 10096 | 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] | 10097 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10098 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10099 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 10100 | int len = sizeof(uint16_t); |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 10101 | args.above_pred_buf[0] = CONVERT_TO_BYTEPTR(x->above_pred_buf); |
| 10102 | args.above_pred_buf[1] = |
| 10103 | CONVERT_TO_BYTEPTR(x->above_pred_buf + MAX_SB_SQUARE * len); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10104 | args.above_pred_buf[2] = |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 10105 | CONVERT_TO_BYTEPTR(x->above_pred_buf + 2 * MAX_SB_SQUARE * len); |
| 10106 | args.left_pred_buf[0] = CONVERT_TO_BYTEPTR(x->left_pred_buf); |
| 10107 | args.left_pred_buf[1] = |
| 10108 | CONVERT_TO_BYTEPTR(x->left_pred_buf + MAX_SB_SQUARE * len); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10109 | args.left_pred_buf[2] = |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 10110 | CONVERT_TO_BYTEPTR(x->left_pred_buf + 2 * MAX_SB_SQUARE * len); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10111 | } else { |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10112 | #endif // CONFIG_HIGHBITDEPTH |
Jingning Han | d064cf0 | 2017-06-01 10:00:39 -0700 | [diff] [blame] | 10113 | args.above_pred_buf[0] = x->above_pred_buf; |
| 10114 | args.above_pred_buf[1] = x->above_pred_buf + MAX_SB_SQUARE; |
| 10115 | args.above_pred_buf[2] = x->above_pred_buf + 2 * MAX_SB_SQUARE; |
| 10116 | args.left_pred_buf[0] = x->left_pred_buf; |
| 10117 | args.left_pred_buf[1] = x->left_pred_buf + MAX_SB_SQUARE; |
| 10118 | 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] | 10119 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10120 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10121 | #endif // CONFIG_HIGHBITDEPTH |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10122 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10123 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10124 | av1_zero(best_mbmode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10125 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10126 | av1_zero(pmi_uv); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 10127 | if (try_palette) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10128 | if (above_mi) |
| 10129 | palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 10130 | if (left_mi) |
| 10131 | palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
| 10132 | } |
| 10133 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10134 | estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp, |
| 10135 | &comp_mode_p); |
| 10136 | |
| 10137 | 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] | 10138 | 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] | 10139 | for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX; |
| 10140 | for (i = 0; i < MB_MODE_COUNT; ++i) { |
| 10141 | for (k = 0; k < TOTAL_REFS_PER_FRAME; ++k) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10142 | args.single_filter[i][k] = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10143 | } |
| 10144 | } |
| 10145 | |
| 10146 | rd_cost->rate = INT_MAX; |
| 10147 | #if CONFIG_SUPERTX |
| 10148 | *returnrate_nocoef = INT_MAX; |
| 10149 | #endif // CONFIG_SUPERTX |
| 10150 | |
| 10151 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 10152 | x->pred_mv_sad[ref_frame] = INT_MAX; |
| 10153 | x->mbmi_ext->mode_context[ref_frame] = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10154 | x->mbmi_ext->compound_mode_context[ref_frame] = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10155 | if (cpi->ref_frame_flags & flag_list[ref_frame]) { |
| 10156 | assert(get_ref_frame_buffer(cpi, ref_frame) != NULL); |
| 10157 | setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col, |
| 10158 | frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb); |
| 10159 | } |
| 10160 | frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10161 | #if CONFIG_GLOBAL_MOTION |
| 10162 | frame_mv[ZEROMV][ref_frame].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 10163 | gm_get_motion_vector(&cm->global_motion[ref_frame], |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 10164 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10165 | 0 |
| 10166 | #if CONFIG_AMVR |
| 10167 | , |
| 10168 | cm->cur_frame_mv_precision_level |
| 10169 | #endif |
| 10170 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10171 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10172 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10173 | frame_mv[ZEROMV][ref_frame].as_int = 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10174 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10175 | frame_mv[NEW_NEWMV][ref_frame].as_int = INVALID_MV; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10176 | #if CONFIG_COMPOUND_SINGLEREF |
| 10177 | frame_mv[SR_NEW_NEWMV][ref_frame].as_int = INVALID_MV; |
| 10178 | frame_comp_mv[SR_NEW_NEWMV][ref_frame].as_int = INVALID_MV; |
| 10179 | #endif // CONFIG_COMPOUND_SINGLEREF |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 10180 | #if CONFIG_GLOBAL_MOTION |
| 10181 | frame_mv[ZERO_ZEROMV][ref_frame].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 10182 | gm_get_motion_vector(&cm->global_motion[ref_frame], |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 10183 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10184 | 0 |
| 10185 | #if CONFIG_AMVR |
| 10186 | , |
| 10187 | cm->cur_frame_mv_precision_level |
| 10188 | #endif |
| 10189 | ) |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 10190 | .as_int; |
| 10191 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10192 | frame_mv[ZERO_ZEROMV][ref_frame].as_int = 0; |
Sarah Parker | c2d3871 | 2017-01-24 15:15:41 -0800 | [diff] [blame] | 10193 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10194 | } |
| 10195 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10196 | for (; ref_frame < MODE_CTX_REF_FRAMES; ++ref_frame) { |
| 10197 | MODE_INFO *const mi = xd->mi[0]; |
| 10198 | int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame]; |
| 10199 | x->mbmi_ext->mode_context[ref_frame] = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10200 | av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame], |
| 10201 | mbmi_ext->ref_mv_stack[ref_frame], |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10202 | mbmi_ext->compound_mode_context, candidates, mi_row, |
| 10203 | mi_col, NULL, NULL, mbmi_ext->mode_context); |
Jingning Han | 731af49 | 2016-11-17 11:53:23 -0800 | [diff] [blame] | 10204 | if (mbmi_ext->ref_mv_count[ref_frame] < 2) { |
| 10205 | MV_REFERENCE_FRAME rf[2]; |
| 10206 | av1_set_ref_frame(rf, ref_frame); |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10207 | if (mbmi_ext->ref_mvs[rf[0]][0].as_int != |
| 10208 | frame_mv[ZEROMV][rf[0]].as_int || |
| 10209 | mbmi_ext->ref_mvs[rf[0]][1].as_int != |
| 10210 | frame_mv[ZEROMV][rf[0]].as_int || |
| 10211 | mbmi_ext->ref_mvs[rf[1]][0].as_int != |
| 10212 | frame_mv[ZEROMV][rf[1]].as_int || |
| 10213 | 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] | 10214 | mbmi_ext->mode_context[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET); |
| 10215 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10216 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10217 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10218 | #if CONFIG_MOTION_VAR |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 10219 | av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col); |
Jingning Han | ad586b9 | 2017-05-23 10:24:57 -0700 | [diff] [blame] | 10220 | |
Yue Chen | f7ba647 | 2017-04-19 11:08:58 -0700 | [diff] [blame] | 10221 | if (check_num_overlappable_neighbors(mbmi) && |
| 10222 | is_motion_variation_allowed_bsize(bsize)) { |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10223 | av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col, |
| 10224 | args.above_pred_buf, dst_width1, |
| 10225 | dst_height1, args.above_pred_stride); |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 10226 | av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10227 | args.left_pred_buf, dst_width2, |
| 10228 | dst_height2, args.left_pred_stride); |
Jingning Han | 91d9a79 | 2017-04-18 12:01:52 -0700 | [diff] [blame] | 10229 | av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row, |
| 10230 | mi_col); |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10231 | calc_target_weighted_pred(cm, x, xd, mi_row, mi_col, args.above_pred_buf[0], |
| 10232 | args.above_pred_stride[0], args.left_pred_buf[0], |
| 10233 | args.left_pred_stride[0]); |
Yue Chen | 5329a2b | 2017-02-28 17:33:00 +0800 | [diff] [blame] | 10234 | } |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10235 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10236 | |
| 10237 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 10238 | if (!(cpi->ref_frame_flags & flag_list[ref_frame])) { |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 10239 | // Skip checking missing references in both single and compound reference |
| 10240 | // modes. Note that a mode will be skipped iff both reference frames |
| 10241 | // are masked out. |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10242 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 10243 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10244 | } else { |
| 10245 | for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) { |
| 10246 | // Skip fixed mv modes for poor references |
| 10247 | if ((x->pred_mv_sad[ref_frame] >> 2) > x->pred_mv_sad[i]) { |
| 10248 | mode_skip_mask[ref_frame] |= INTER_NEAREST_NEAR_ZERO; |
| 10249 | break; |
| 10250 | } |
| 10251 | } |
| 10252 | } |
| 10253 | // If the segment reference frame feature is enabled.... |
| 10254 | // then do nothing if the current ref frame is not allowed.. |
| 10255 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && |
| 10256 | get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) { |
| 10257 | ref_frame_skip_mask[0] |= (1 << ref_frame); |
| 10258 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10259 | } |
| 10260 | } |
| 10261 | |
| 10262 | // Disable this drop out case if the ref frame |
| 10263 | // segment level feature is enabled for this segment. This is to |
| 10264 | // prevent the possibility that we end up unable to pick any mode. |
| 10265 | if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) { |
| 10266 | // Only consider ZEROMV/ALTREF_FRAME for alt ref frame, |
| 10267 | // unless ARNR filtering is enabled in which case we want |
| 10268 | // an unfiltered alternative. We allow near/nearest as well |
| 10269 | // because they may result in zero-zero MVs but be cheaper. |
| 10270 | 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] | 10271 | int_mv zeromv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10272 | ref_frame_skip_mask[0] = (1 << LAST_FRAME) | |
| 10273 | #if CONFIG_EXT_REFS |
| 10274 | (1 << LAST2_FRAME) | (1 << LAST3_FRAME) | |
Zoe Liu | 3ac2093 | 2017-08-30 16:35:55 -0700 | [diff] [blame] | 10275 | (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10276 | #endif // CONFIG_EXT_REFS |
| 10277 | (1 << GOLDEN_FRAME); |
| 10278 | ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK; |
| 10279 | // TODO(zoeliu): To further explore whether following needs to be done for |
| 10280 | // BWDREF_FRAME as well. |
| 10281 | mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10282 | #if CONFIG_GLOBAL_MOTION |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10283 | zeromv.as_int = gm_get_motion_vector(&cm->global_motion[ALTREF_FRAME], |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 10284 | cm->allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 10285 | mi_col, mi_row, 0 |
| 10286 | #if CONFIG_AMVR |
| 10287 | , |
| 10288 | cm->cur_frame_mv_precision_level |
| 10289 | #endif |
| 10290 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 10291 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10292 | #else |
| 10293 | zeromv.as_int = 0; |
| 10294 | #endif // CONFIG_GLOBAL_MOTION |
| 10295 | if (frame_mv[NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10296 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEARMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10297 | if (frame_mv[NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10298 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEARESTMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10299 | if (frame_mv[NEAREST_NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10300 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAREST_NEARESTMV); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10301 | if (frame_mv[NEAR_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10302 | mode_skip_mask[ALTREF_FRAME] |= (1 << NEAR_NEARMV); |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10303 | #if CONFIG_COMPOUND_SINGLEREF |
| 10304 | if (frame_mv[SR_NEAREST_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int || |
| 10305 | frame_comp_mv[SR_NEAREST_NEARMV][ALTREF_FRAME].as_int != |
| 10306 | zeromv.as_int) |
| 10307 | mode_skip_mask[ALTREF_FRAME] |= (1 << SR_NEAREST_NEARMV); |
| 10308 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10309 | } |
| 10310 | } |
| 10311 | |
| 10312 | if (cpi->rc.is_src_frame_alt_ref) { |
| 10313 | if (sf->alt_ref_search_fp) { |
| 10314 | assert(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]); |
| 10315 | mode_skip_mask[ALTREF_FRAME] = 0; |
| 10316 | ref_frame_skip_mask[0] = ~(1 << ALTREF_FRAME); |
| 10317 | ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK; |
| 10318 | } |
| 10319 | } |
| 10320 | |
| 10321 | if (sf->alt_ref_search_fp) |
| 10322 | if (!cm->show_frame && x->pred_mv_sad[GOLDEN_FRAME] < INT_MAX) |
| 10323 | if (x->pred_mv_sad[ALTREF_FRAME] > (x->pred_mv_sad[GOLDEN_FRAME] << 1)) |
| 10324 | mode_skip_mask[ALTREF_FRAME] |= INTER_ALL; |
| 10325 | |
| 10326 | if (sf->adaptive_mode_search) { |
| 10327 | if (cm->show_frame && !cpi->rc.is_src_frame_alt_ref && |
| 10328 | cpi->rc.frames_since_golden >= 3) |
Yaowu Xu | 36bad47 | 2017-05-16 18:29:53 -0700 | [diff] [blame] | 10329 | 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] | 10330 | mode_skip_mask[GOLDEN_FRAME] |= INTER_ALL; |
| 10331 | } |
| 10332 | |
| 10333 | if (bsize > sf->max_intra_bsize) { |
| 10334 | ref_frame_skip_mask[0] |= (1 << INTRA_FRAME); |
| 10335 | ref_frame_skip_mask[1] |= (1 << INTRA_FRAME); |
| 10336 | } |
| 10337 | |
| 10338 | mode_skip_mask[INTRA_FRAME] |= |
| 10339 | ~(sf->intra_y_mode_mask[max_txsize_lookup[bsize]]); |
| 10340 | |
| 10341 | for (i = 0; i <= LAST_NEW_MV_INDEX; ++i) mode_threshold[i] = 0; |
| 10342 | for (i = LAST_NEW_MV_INDEX + 1; i < MAX_MODES; ++i) |
| 10343 | mode_threshold[i] = ((int64_t)rd_threshes[i] * rd_thresh_freq_fact[i]) >> 5; |
| 10344 | |
| 10345 | midx = sf->schedule_mode_search ? mode_skip_start : 0; |
| 10346 | while (midx > 4) { |
| 10347 | uint8_t end_pos = 0; |
| 10348 | for (i = 5; i < midx; ++i) { |
| 10349 | if (mode_threshold[mode_map[i - 1]] > mode_threshold[mode_map[i]]) { |
| 10350 | uint8_t tmp = mode_map[i]; |
| 10351 | mode_map[i] = mode_map[i - 1]; |
| 10352 | mode_map[i - 1] = tmp; |
| 10353 | end_pos = i; |
| 10354 | } |
| 10355 | } |
| 10356 | midx = end_pos; |
| 10357 | } |
| 10358 | |
| 10359 | if (cpi->sf.tx_type_search.fast_intra_tx_type_search) |
| 10360 | x->use_default_intra_tx_type = 1; |
| 10361 | else |
| 10362 | x->use_default_intra_tx_type = 0; |
| 10363 | |
| 10364 | if (cpi->sf.tx_type_search.fast_inter_tx_type_search) |
| 10365 | x->use_default_inter_tx_type = 1; |
| 10366 | else |
| 10367 | x->use_default_inter_tx_type = 0; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10368 | #if CONFIG_PVQ |
| 10369 | od_encode_checkpoint(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10370 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10371 | for (i = 0; i < MB_MODE_COUNT; ++i) |
| 10372 | for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame) |
| 10373 | modelled_rd[i][ref_frame] = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10374 | |
| 10375 | for (midx = 0; midx < MAX_MODES; ++midx) { |
| 10376 | int mode_index; |
| 10377 | int mode_excluded = 0; |
| 10378 | int64_t this_rd = INT64_MAX; |
| 10379 | int disable_skip = 0; |
| 10380 | int compmode_cost = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10381 | int rate2 = 0, rate_y = 0, rate_uv = 0; |
| 10382 | int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0; |
| 10383 | int skippable = 0; |
| 10384 | int this_skip2 = 0; |
| 10385 | int64_t total_sse = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10386 | uint8_t ref_frame_type; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 10387 | #if CONFIG_PVQ |
| 10388 | od_encode_rollback(&x->daala_enc, &pre_buf); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10389 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10390 | mode_index = mode_map[midx]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10391 | this_mode = av1_mode_order[mode_index].mode; |
| 10392 | ref_frame = av1_mode_order[mode_index].ref_frame[0]; |
| 10393 | second_ref_frame = av1_mode_order[mode_index].ref_frame[1]; |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 10394 | mbmi->ref_mv_idx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10395 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10396 | if (ref_frame > INTRA_FRAME && second_ref_frame == INTRA_FRAME) { |
| 10397 | // Mode must by compatible |
Debargha Mukherjee | 37f6fe6 | 2017-02-10 21:44:13 -0800 | [diff] [blame] | 10398 | if (!is_interintra_allowed_mode(this_mode)) continue; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10399 | if (!is_interintra_allowed_bsize(bsize)) continue; |
| 10400 | } |
| 10401 | |
| 10402 | if (is_inter_compound_mode(this_mode)) { |
| 10403 | frame_mv[this_mode][ref_frame].as_int = |
| 10404 | frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int; |
| 10405 | frame_mv[this_mode][second_ref_frame].as_int = |
| 10406 | frame_mv[compound_ref1_mode(this_mode)][second_ref_frame].as_int; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10407 | #if CONFIG_COMPOUND_SINGLEREF |
| 10408 | } else if (is_inter_singleref_comp_mode(this_mode)) { |
| 10409 | frame_mv[this_mode][ref_frame].as_int = |
| 10410 | frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int; |
| 10411 | frame_comp_mv[this_mode][ref_frame].as_int = |
| 10412 | frame_mv[compound_ref1_mode(this_mode)][ref_frame].as_int; |
| 10413 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10414 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10415 | |
| 10416 | // Look at the reference frame of the best mode so far and set the |
| 10417 | // skip mask to look at a subset of the remaining modes. |
| 10418 | if (midx == mode_skip_start && best_mode_index >= 0) { |
| 10419 | switch (best_mbmode.ref_frame[0]) { |
| 10420 | case INTRA_FRAME: break; |
| 10421 | case LAST_FRAME: |
| 10422 | ref_frame_skip_mask[0] |= LAST_FRAME_MODE_MASK; |
| 10423 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10424 | break; |
| 10425 | #if CONFIG_EXT_REFS |
| 10426 | case LAST2_FRAME: |
| 10427 | ref_frame_skip_mask[0] |= LAST2_FRAME_MODE_MASK; |
| 10428 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10429 | break; |
| 10430 | case LAST3_FRAME: |
| 10431 | ref_frame_skip_mask[0] |= LAST3_FRAME_MODE_MASK; |
| 10432 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10433 | break; |
| 10434 | #endif // CONFIG_EXT_REFS |
| 10435 | case GOLDEN_FRAME: |
| 10436 | ref_frame_skip_mask[0] |= GOLDEN_FRAME_MODE_MASK; |
| 10437 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10438 | break; |
| 10439 | #if CONFIG_EXT_REFS |
| 10440 | case BWDREF_FRAME: |
| 10441 | ref_frame_skip_mask[0] |= BWDREF_FRAME_MODE_MASK; |
| 10442 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10443 | break; |
Zoe Liu | e9b15e2 | 2017-07-19 15:53:01 -0700 | [diff] [blame] | 10444 | case ALTREF2_FRAME: |
| 10445 | ref_frame_skip_mask[0] |= ALTREF2_FRAME_MODE_MASK; |
| 10446 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10447 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10448 | #endif // CONFIG_EXT_REFS |
| 10449 | case ALTREF_FRAME: ref_frame_skip_mask[0] |= ALTREF_FRAME_MODE_MASK; |
| 10450 | #if CONFIG_EXT_REFS |
| 10451 | ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK; |
| 10452 | #endif // CONFIG_EXT_REFS |
| 10453 | break; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 10454 | case NONE_FRAME: |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10455 | case TOTAL_REFS_PER_FRAME: |
| 10456 | assert(0 && "Invalid Reference frame"); |
| 10457 | break; |
| 10458 | } |
| 10459 | } |
| 10460 | |
| 10461 | if ((ref_frame_skip_mask[0] & (1 << ref_frame)) && |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10462 | (ref_frame_skip_mask[1] & (1 << AOMMAX(0, second_ref_frame)))) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10463 | continue; |
| 10464 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 10465 | #if CONFIG_EXT_COMP_REFS |
| 10466 | // TODO(zoeliu): Following toggle between #if 0/1 and the bug will manifest |
| 10467 | // itself. |
| 10468 | #if 0 |
| 10469 | if (!(cpi->ref_frame_flags & flag_list[ref_frame]) || |
| 10470 | (second_ref_frame > INTRA_FRAME && |
| 10471 | (!(cpi->ref_frame_flags & flag_list[second_ref_frame])))) |
| 10472 | printf("Frame=%d, bsize=%d, (mi_row,mi_col)=(%d,%d), ref_frame=%d, " |
| 10473 | "second_ref_frame=%d\n", cm->current_video_frame, bsize, mi_row, |
| 10474 | mi_col, ref_frame, second_ref_frame); |
| 10475 | |
| 10476 | if (!(cpi->ref_frame_flags & flag_list[ref_frame])) continue; |
| 10477 | if (second_ref_frame > INTRA_FRAME && |
| 10478 | (!(cpi->ref_frame_flags & flag_list[second_ref_frame]))) |
| 10479 | continue; |
| 10480 | #endif // 0 |
| 10481 | |
| 10482 | #if !USE_UNI_COMP_REFS |
| 10483 | // NOTE(zoeliu): Temporarily disable uni-directional comp refs |
| 10484 | if (second_ref_frame > INTRA_FRAME) { |
| 10485 | if (!((ref_frame < BWDREF_FRAME) ^ (second_ref_frame < BWDREF_FRAME))) |
| 10486 | continue; |
| 10487 | } |
| 10488 | assert(second_ref_frame <= INTRA_FRAME || |
| 10489 | ((ref_frame < BWDREF_FRAME) ^ (second_ref_frame < BWDREF_FRAME))); |
| 10490 | #endif // !USE_UNI_COMP_REFS |
| 10491 | #endif // CONFIG_EXT_COMP_REFS |
| 10492 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10493 | if (mode_skip_mask[ref_frame] & (1 << this_mode)) continue; |
| 10494 | |
| 10495 | // Test best rd so far against threshold for trying this mode. |
| 10496 | if (best_mode_skippable && sf->schedule_mode_search) |
| 10497 | mode_threshold[mode_index] <<= 1; |
| 10498 | |
| 10499 | if (best_rd < mode_threshold[mode_index]) continue; |
| 10500 | |
Yunqing Wang | ff4fa06 | 2017-04-21 10:56:08 -0700 | [diff] [blame] | 10501 | // This is only used in motion vector unit test. |
| 10502 | if (cpi->oxcf.motion_vector_unit_test && ref_frame == INTRA_FRAME) continue; |
| 10503 | |
Zoe Liu | 5a97883 | 2017-08-15 16:33:34 -0700 | [diff] [blame] | 10504 | #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] | 10505 | #if CONFIG_EXT_REFS |
| 10506 | if (cpi->oxcf.pass == 0) { |
| 10507 | // Complexity-compression trade-offs |
| 10508 | // if (ref_frame == ALTREF_FRAME) continue; |
| 10509 | // if (ref_frame == BWDREF_FRAME) continue; |
| 10510 | if (second_ref_frame == ALTREF_FRAME) continue; |
| 10511 | // if (second_ref_frame == BWDREF_FRAME) continue; |
| 10512 | } |
Zoe Liu | 5a97883 | 2017-08-15 16:33:34 -0700 | [diff] [blame] | 10513 | #endif // CONFIG_EXT_REFS |
| 10514 | #endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10515 | comp_pred = second_ref_frame > INTRA_FRAME; |
| 10516 | if (comp_pred) { |
| 10517 | if (!cpi->allow_comp_inter_inter) continue; |
| 10518 | |
| 10519 | // Skip compound inter modes if ARF is not available. |
| 10520 | if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue; |
| 10521 | |
| 10522 | // Do not allow compound prediction if the segment level reference frame |
| 10523 | // feature is in use as in this case there can only be one reference. |
| 10524 | if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue; |
| 10525 | |
| 10526 | if ((mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) && |
| 10527 | best_mode_index >= 0 && best_mbmode.ref_frame[0] == INTRA_FRAME) |
| 10528 | continue; |
| 10529 | |
| 10530 | mode_excluded = cm->reference_mode == SINGLE_REFERENCE; |
| 10531 | } else { |
| 10532 | if (ref_frame != INTRA_FRAME) |
| 10533 | mode_excluded = cm->reference_mode == COMPOUND_REFERENCE; |
| 10534 | } |
| 10535 | |
| 10536 | if (ref_frame == INTRA_FRAME) { |
| 10537 | if (sf->adaptive_mode_search) |
| 10538 | if ((x->source_variance << num_pels_log2_lookup[bsize]) > best_pred_sse) |
| 10539 | continue; |
| 10540 | |
| 10541 | if (this_mode != DC_PRED) { |
| 10542 | // Disable intra modes other than DC_PRED for blocks with low variance |
| 10543 | // Threshold for intra skipping based on source variance |
| 10544 | // TODO(debargha): Specialize the threshold for super block sizes |
| 10545 | const unsigned int skip_intra_var_thresh = 64; |
| 10546 | if ((mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) && |
| 10547 | x->source_variance < skip_intra_var_thresh) |
| 10548 | continue; |
| 10549 | // Only search the oblique modes if the best so far is |
| 10550 | // one of the neighboring directional modes |
| 10551 | if ((mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) && |
| 10552 | (this_mode >= D45_PRED && this_mode <= TM_PRED)) { |
| 10553 | if (best_mode_index >= 0 && best_mbmode.ref_frame[0] > INTRA_FRAME) |
| 10554 | continue; |
| 10555 | } |
| 10556 | if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { |
| 10557 | if (conditional_skipintra(this_mode, best_intra_mode)) continue; |
| 10558 | } |
| 10559 | } |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10560 | #if CONFIG_GLOBAL_MOTION |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 10561 | } else if (cm->global_motion[ref_frame].wmtype == IDENTITY && |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10562 | (!comp_pred || |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 10563 | cm->global_motion[second_ref_frame].wmtype == IDENTITY)) { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10564 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10565 | } else { |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 10566 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10567 | const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, second_ref_frame }; |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10568 | if (!check_best_zero_mv(cpi, x, mbmi_ext->mode_context, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10569 | mbmi_ext->compound_mode_context, frame_mv, |
| 10570 | this_mode, ref_frames, bsize, -1, mi_row, mi_col)) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10571 | continue; |
| 10572 | } |
| 10573 | |
| 10574 | mbmi->mode = this_mode; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10575 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10576 | mbmi->ref_frame[0] = ref_frame; |
| 10577 | mbmi->ref_frame[1] = second_ref_frame; |
| 10578 | pmi->palette_size[0] = 0; |
| 10579 | pmi->palette_size[1] = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10580 | #if CONFIG_FILTER_INTRA |
| 10581 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 10582 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 10583 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10584 | // Evaluate all sub-pel filters irrespective of whether we can use |
| 10585 | // them for this frame. |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 10586 | |
| 10587 | set_default_interp_filters(mbmi, cm->interp_filter); |
| 10588 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10589 | mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 10590 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10591 | |
| 10592 | x->skip = 0; |
| 10593 | set_ref_ptrs(cm, xd, ref_frame, second_ref_frame); |
| 10594 | |
| 10595 | // Select prediction reference frames. |
| 10596 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 10597 | xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; |
| 10598 | if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i]; |
| 10599 | } |
| 10600 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10601 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10602 | // Single ref compound mode |
| 10603 | if (!comp_pred && is_inter_singleref_comp_mode(mbmi->mode)) { |
| 10604 | xd->block_refs[1] = xd->block_refs[0]; |
| 10605 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 10606 | xd->plane[i].pre[1] = xd->plane[i].pre[0]; |
| 10607 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10608 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10609 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10610 | #if CONFIG_INTERINTRA |
Debargha Mukherjee | cb60379 | 2016-10-04 13:10:23 -0700 | [diff] [blame] | 10611 | mbmi->interintra_mode = (INTERINTRA_MODE)(II_DC_PRED - 1); |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10612 | #endif // CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10613 | |
| 10614 | if (ref_frame == INTRA_FRAME) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10615 | RD_STATS rd_stats_y; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10616 | TX_SIZE uv_tx; |
| 10617 | struct macroblockd_plane *const pd = &xd->plane[1]; |
| 10618 | #if CONFIG_EXT_INTRA |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 10619 | is_directional_mode = av1_is_directional_mode(mbmi->mode, bsize); |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 10620 | if (is_directional_mode && av1_use_angle_delta(bsize)) { |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 10621 | int rate_dummy; |
hui su | 9a416f5 | 2017-01-13 11:37:53 -0800 | [diff] [blame] | 10622 | int64_t model_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10623 | if (!angle_stats_ready) { |
| 10624 | const int src_stride = x->plane[0].src.stride; |
| 10625 | const uint8_t *src = x->plane[0].src.buf; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10626 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10627 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 10628 | highbd_angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10629 | directional_mode_skip_mask); |
| 10630 | else |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 10631 | #endif // CONFIG_HIGHBITDEPTH |
hui su | 9cc1065 | 2017-04-27 17:22:07 -0700 | [diff] [blame] | 10632 | angle_estimation(src, src_stride, rows, cols, bsize, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10633 | directional_mode_skip_mask); |
| 10634 | angle_stats_ready = 1; |
| 10635 | } |
| 10636 | if (directional_mode_skip_mask[mbmi->mode]) continue; |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 10637 | rd_stats_y.rate = INT_MAX; |
Yue Chen | b0f808b | 2017-04-26 11:55:14 -0700 | [diff] [blame] | 10638 | rd_pick_intra_angle_sby(cpi, x, &rate_dummy, &rd_stats_y, bsize, |
| 10639 | intra_mode_cost[mbmi->mode], best_rd, |
| 10640 | &model_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10641 | } else { |
| 10642 | mbmi->angle_delta[0] = 0; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10643 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10644 | } |
| 10645 | #else |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10646 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 10647 | #endif // CONFIG_EXT_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 10648 | rate_y = rd_stats_y.rate; |
| 10649 | distortion_y = rd_stats_y.dist; |
| 10650 | skippable = rd_stats_y.skip; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10651 | |
| 10652 | if (rate_y == INT_MAX) continue; |
| 10653 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10654 | #if CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10655 | if (mbmi->mode == DC_PRED) dc_skipped = 0; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10656 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10657 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 10658 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][pd->subsampling_x] |
| 10659 | [pd->subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10660 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 10661 | 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] | 10662 | &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx], |
| 10663 | &skip_uvs[uv_tx], &mode_uv[uv_tx]); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 10664 | if (try_palette) pmi_uv[uv_tx] = *pmi; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10665 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10666 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10667 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
| 10668 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10669 | #if CONFIG_FILTER_INTRA |
| 10670 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 10671 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10672 | } |
| 10673 | |
| 10674 | rate_uv = rate_uv_tokenonly[uv_tx]; |
Urvang Joshi | 368fbc9 | 2016-10-17 16:31:34 -0700 | [diff] [blame] | 10675 | distortion_uv = dist_uvs[uv_tx]; |
| 10676 | skippable = skippable && skip_uvs[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10677 | mbmi->uv_mode = mode_uv[uv_tx]; |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 10678 | if (try_palette) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10679 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
| 10680 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 10681 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 10682 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
| 10683 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 10684 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10685 | #if CONFIG_EXT_INTRA |
| 10686 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10687 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10688 | #if CONFIG_FILTER_INTRA |
| 10689 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 10690 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 10691 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 10692 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 10693 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
| 10694 | } |
| 10695 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10696 | |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 10697 | #if CONFIG_CB4X4 |
| 10698 | rate2 = rate_y + intra_mode_cost[mbmi->mode]; |
| 10699 | if (!x->skip_chroma_rd) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10700 | 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] | 10701 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10702 | rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv + |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10703 | x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode]; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10704 | #endif // CONFIG_CB4X4 |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 10705 | |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 10706 | if (try_palette && mbmi->mode == DC_PRED) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10707 | rate2 += av1_cost_bit( |
| 10708 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0); |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 10709 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10710 | |
Jingning Han | bf9c6b7 | 2016-12-14 14:50:45 -0800 | [diff] [blame] | 10711 | if (!xd->lossless[mbmi->segment_id] && bsize >= BLOCK_8X8) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10712 | // super_block_yrd above includes the cost of the tx_size in the |
| 10713 | // tokenonly rate, but for intra blocks, tx_size is always coded |
| 10714 | // (prediction granularity), so we account for it in the full rate, |
| 10715 | // not the tokenonly rate. |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 10716 | rate_y -= tx_size_cost(cpi, x, bsize, mbmi->tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10717 | } |
| 10718 | #if CONFIG_EXT_INTRA |
| 10719 | if (is_directional_mode) { |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 10720 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10721 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 10722 | const int p_angle = |
| 10723 | mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10724 | if (av1_is_intra_filter_switchable(p_angle)) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10725 | rate2 += x->intra_filter_cost[intra_filter_ctx][mbmi->intra_filter]; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 10726 | #endif // CONFIG_INTRA_INTERP |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 10727 | if (av1_use_angle_delta(bsize)) { |
| 10728 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 10729 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
| 10730 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10731 | } |
Luc Trudeau | f24a35f | 2017-09-11 11:56:33 -0400 | [diff] [blame] | 10732 | if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize) && |
Joe Young | 830d4ce | 2017-05-30 17:48:13 -0700 | [diff] [blame] | 10733 | av1_use_angle_delta(bsize)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 10734 | rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, |
| 10735 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10736 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10737 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10738 | #if CONFIG_FILTER_INTRA |
| 10739 | if (mbmi->mode == DC_PRED) { |
| 10740 | rate2 += |
| 10741 | av1_cost_bit(cm->fc->filter_intra_probs[0], |
| 10742 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0]); |
| 10743 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) { |
| 10744 | rate2 += write_uniform_cost( |
| 10745 | FILTER_INTRA_MODES, |
| 10746 | mbmi->filter_intra_mode_info.filter_intra_mode[0]); |
| 10747 | } |
| 10748 | } |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 10749 | if (mbmi->uv_mode == UV_DC_PRED) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10750 | rate2 += |
| 10751 | av1_cost_bit(cpi->common.fc->filter_intra_probs[1], |
| 10752 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1]); |
| 10753 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1]) |
| 10754 | rate2 += write_uniform_cost( |
| 10755 | FILTER_INTRA_MODES, |
| 10756 | mbmi->filter_intra_mode_info.filter_intra_mode[1]); |
| 10757 | } |
| 10758 | #endif // CONFIG_FILTER_INTRA |
Zoe Liu | 1157d50 | 2017-04-30 07:57:14 -0700 | [diff] [blame] | 10759 | if (mbmi->mode != DC_PRED && mbmi->mode != TM_PRED) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10760 | rate2 += intra_cost_penalty; |
| 10761 | distortion2 = distortion_y + distortion_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10762 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10763 | int_mv backup_ref_mv[2]; |
| 10764 | |
Debargha Mukherjee | 0f248c4 | 2017-09-07 12:40:18 -0700 | [diff] [blame] | 10765 | if (!is_comp_ref_allowed(bsize) && mbmi->ref_frame[1] > INTRA_FRAME) |
| 10766 | continue; |
Jingning Han | c41a549 | 2017-02-24 11:18:52 -0800 | [diff] [blame] | 10767 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10768 | backup_ref_mv[0] = mbmi_ext->ref_mvs[ref_frame][0]; |
| 10769 | if (comp_pred) backup_ref_mv[1] = mbmi_ext->ref_mvs[second_ref_frame][0]; |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10770 | #if CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10771 | if (second_ref_frame == INTRA_FRAME) { |
| 10772 | if (best_single_inter_ref != ref_frame) continue; |
Debargha Mukherjee | cb60379 | 2016-10-04 13:10:23 -0700 | [diff] [blame] | 10773 | mbmi->interintra_mode = intra_to_interintra_mode[best_intra_mode]; |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10774 | // TODO(debargha|geza.lore): |
| 10775 | // Should we use ext_intra modes for interintra? |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10776 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10777 | mbmi->angle_delta[0] = 0; |
| 10778 | mbmi->angle_delta[1] = 0; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 10779 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10780 | mbmi->intra_filter = INTRA_FILTER_LINEAR; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 10781 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10782 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 10783 | #if CONFIG_FILTER_INTRA |
| 10784 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 10785 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 10786 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10787 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10788 | #endif // CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10789 | mbmi->ref_mv_idx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10790 | ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10791 | |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10792 | if (comp_pred) { |
| 10793 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10794 | int ref_mv_idx = 0; |
| 10795 | // Special case: NEAR_NEWMV and NEW_NEARMV modes use |
| 10796 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 10797 | // mbmi->ref_mv_idx (like NEWMV) |
| 10798 | if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) |
| 10799 | ref_mv_idx = 1; |
| 10800 | |
| 10801 | if (compound_ref0_mode(mbmi->mode) == NEWMV) { |
| 10802 | int_mv this_mv = |
| 10803 | 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] | 10804 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10805 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10806 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 10807 | } |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10808 | if (compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 10809 | int_mv this_mv = |
| 10810 | 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] | 10811 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10812 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10813 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 10814 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10815 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10816 | #if CONFIG_COMPOUND_SINGLEREF |
| 10817 | } else if (is_inter_singleref_comp_mode(mbmi->mode)) { |
| 10818 | if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) { |
| 10819 | // TODO(zoeliu): To further investigate which ref_mv_idx should be |
| 10820 | // chosen for the mode of SR_NEAR_NEWMV. |
| 10821 | int ref_mv_idx = 0; |
| 10822 | // Special case: SR_NEAR_NEWMV mode use |
| 10823 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 10824 | // mbmi->ref_mv_idx (like NEWMV) |
| 10825 | if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1; |
| 10826 | |
| 10827 | if (compound_ref0_mode(mbmi->mode) == NEWMV || |
| 10828 | compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 10829 | int_mv this_mv = |
| 10830 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 10831 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10832 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10833 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 10834 | } |
| 10835 | } |
| 10836 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10837 | } else { |
Zoe Liu | 1157d50 | 2017-04-30 07:57:14 -0700 | [diff] [blame] | 10838 | 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] | 10839 | int ref; |
| 10840 | for (ref = 0; ref < 1 + comp_pred; ++ref) { |
| 10841 | int_mv this_mv = |
| 10842 | (ref == 0) ? mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv |
| 10843 | : mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 10844 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10845 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10846 | mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv; |
| 10847 | } |
| 10848 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10849 | } |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10850 | { |
| 10851 | RD_STATS rd_stats, rd_stats_y, rd_stats_uv; |
| 10852 | av1_init_rd_stats(&rd_stats); |
| 10853 | rd_stats.rate = rate2; |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 10854 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10855 | // Point to variables that are maintained between loop iterations |
| 10856 | args.single_newmv = single_newmv; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 10857 | args.single_newmv_rate = single_newmv_rate; |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10858 | args.modelled_rd = modelled_rd; |
Fergus Simpson | 3424c2d | 2017-03-09 11:48:15 -0800 | [diff] [blame] | 10859 | this_rd = handle_inter_mode(cpi, x, bsize, &rd_stats, &rd_stats_y, |
| 10860 | &rd_stats_uv, &disable_skip, frame_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10861 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10862 | frame_comp_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10863 | #endif // CONFIG_COMPOUND_SINGLEREF |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 10864 | mi_row, mi_col, &args, best_rd); |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10865 | |
| 10866 | rate2 = rd_stats.rate; |
| 10867 | skippable = rd_stats.skip; |
| 10868 | distortion2 = rd_stats.dist; |
| 10869 | total_sse = rd_stats.sse; |
| 10870 | rate_y = rd_stats_y.rate; |
| 10871 | rate_uv = rd_stats_uv.rate; |
| 10872 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10873 | |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10874 | // TODO(jingning): This needs some refactoring to improve code quality |
| 10875 | // and reduce redundant steps. |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10876 | #if CONFIG_COMPOUND_SINGLEREF |
| 10877 | if ((have_nearmv_in_inter_mode(mbmi->mode) && |
| 10878 | mbmi_ext->ref_mv_count[ref_frame_type] > 2) || |
| 10879 | ((mbmi->mode == NEWMV || mbmi->mode == SR_NEW_NEWMV || |
| 10880 | mbmi->mode == NEW_NEWMV) && |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 10881 | mbmi_ext->ref_mv_count[ref_frame_type] > 1)) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10882 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10883 | if ((have_nearmv_in_inter_mode(mbmi->mode) && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 10884 | mbmi_ext->ref_mv_count[ref_frame_type] > 2) || |
| 10885 | ((mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) && |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 10886 | mbmi_ext->ref_mv_count[ref_frame_type] > 1)) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10887 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 10888 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10889 | int_mv backup_mv = frame_mv[NEARMV][ref_frame]; |
| 10890 | MB_MODE_INFO backup_mbmi = *mbmi; |
| 10891 | int backup_skip = x->skip; |
| 10892 | int64_t tmp_ref_rd = this_rd; |
| 10893 | int ref_idx; |
| 10894 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 10895 | // TODO(jingning): This should be deprecated shortly. |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10896 | int idx_offset = have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10897 | int ref_set = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10898 | 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] | 10899 | |
| 10900 | uint8_t drl_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10901 | 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] | 10902 | // Dummy |
| 10903 | int_mv backup_fmv[2]; |
| 10904 | backup_fmv[0] = frame_mv[NEWMV][ref_frame]; |
| 10905 | if (comp_pred) backup_fmv[1] = frame_mv[NEWMV][second_ref_frame]; |
| 10906 | |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 10907 | rate2 += (rate2 < INT_MAX ? x->drl_mode_cost0[drl_ctx][0] : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10908 | |
| 10909 | if (this_rd < INT64_MAX) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 10910 | if (RDCOST(x->rdmult, rate_y + rate_uv, distortion2) < |
| 10911 | RDCOST(x->rdmult, 0, total_sse)) |
| 10912 | tmp_ref_rd = RDCOST( |
| 10913 | x->rdmult, rate2 + av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
| 10914 | distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10915 | else |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 10916 | tmp_ref_rd = |
| 10917 | RDCOST(x->rdmult, |
| 10918 | rate2 + av1_cost_bit(av1_get_skip_prob(cm, xd), 1) - |
| 10919 | rate_y - rate_uv, |
| 10920 | total_sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10921 | } |
| 10922 | #if CONFIG_VAR_TX |
| 10923 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 10924 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 10925 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 10926 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10927 | |
| 10928 | for (ref_idx = 0; ref_idx < ref_set; ++ref_idx) { |
| 10929 | int64_t tmp_alt_rd = INT64_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10930 | int dummy_disable_skip = 0; |
| 10931 | int ref; |
| 10932 | int_mv cur_mv; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 10933 | 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] | 10934 | |
Yaowu Xu | 5bfbfdf | 2016-11-22 16:43:34 -0800 | [diff] [blame] | 10935 | av1_invalid_rd_stats(&tmp_rd_stats); |
Yushin Cho | c0f6bf2 | 2017-06-09 16:08:02 -0700 | [diff] [blame] | 10936 | |
Jingning Han | 52617b2 | 2017-04-11 12:50:08 -0700 | [diff] [blame] | 10937 | x->skip = 0; |
Yaowu Xu | 5bfbfdf | 2016-11-22 16:43:34 -0800 | [diff] [blame] | 10938 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10939 | mbmi->ref_mv_idx = 1 + ref_idx; |
| 10940 | |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 10941 | if (comp_pred) { |
| 10942 | int ref_mv_idx = mbmi->ref_mv_idx; |
| 10943 | // Special case: NEAR_NEWMV and NEW_NEARMV modes use |
| 10944 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 10945 | // mbmi->ref_mv_idx (like NEWMV) |
| 10946 | if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) |
| 10947 | ref_mv_idx = 1 + mbmi->ref_mv_idx; |
| 10948 | |
| 10949 | if (compound_ref0_mode(mbmi->mode) == NEWMV) { |
| 10950 | int_mv this_mv = |
| 10951 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 10952 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10953 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10954 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 10955 | } else if (compound_ref0_mode(mbmi->mode) == NEARESTMV) { |
| 10956 | int_mv this_mv = |
| 10957 | mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 10958 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10959 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10960 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 10961 | } |
| 10962 | |
| 10963 | if (compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 10964 | int_mv this_mv = |
| 10965 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv; |
| 10966 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10967 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10968 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 10969 | } else if (compound_ref1_mode(mbmi->mode) == NEARESTMV) { |
| 10970 | int_mv this_mv = |
| 10971 | mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv; |
| 10972 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10973 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10974 | mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv; |
| 10975 | } |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 10976 | #if CONFIG_COMPOUND_SINGLEREF |
| 10977 | } else if (is_inter_singleref_comp_mode(mbmi->mode)) { |
| 10978 | int ref_mv_idx = mbmi->ref_mv_idx; |
| 10979 | // Special case: SR_NEAR_NEWMV mode use |
| 10980 | // 1 + mbmi->ref_mv_idx (like NEARMV) instead of |
| 10981 | // mbmi->ref_mv_idx (like NEWMV) |
| 10982 | if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx; |
| 10983 | |
| 10984 | // TODO(zoeliu): For the mode of SR_NEAREST_NEWMV, as it only runs |
| 10985 | // the "if", not the "else if", |
| 10986 | // mbmi_ext->ref_mvs[mbmi->ref_frame[0]] takes the |
| 10987 | // value for "NEWMV", instead of "NEARESTMV". |
| 10988 | if (compound_ref0_mode(mbmi->mode) == NEWMV || |
| 10989 | compound_ref1_mode(mbmi->mode) == NEWMV) { |
| 10990 | int_mv this_mv = |
| 10991 | mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv; |
| 10992 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 10993 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 10994 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 10995 | } else if (compound_ref0_mode(mbmi->mode) == NEARESTMV || |
| 10996 | compound_ref1_mode(mbmi->mode) == NEARESTMV) { |
| 10997 | int_mv this_mv = |
| 10998 | mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv; |
| 10999 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11000 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11001 | mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv; |
| 11002 | } |
| 11003 | #endif // CONFIG_COMPOUND_SINGLEREF |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11004 | } else { |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11005 | for (ref = 0; ref < 1 + comp_pred; ++ref) { |
| 11006 | int_mv this_mv = |
| 11007 | (ref == 0) |
| 11008 | ? mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx] |
| 11009 | .this_mv |
| 11010 | : mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx] |
| 11011 | .comp_mv; |
| 11012 | clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2, |
| 11013 | xd->n8_h << MI_SIZE_LOG2, xd); |
| 11014 | mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv; |
| 11015 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11016 | } |
| 11017 | |
| 11018 | cur_mv = |
| 11019 | mbmi_ext->ref_mv_stack[ref_frame][mbmi->ref_mv_idx + idx_offset] |
| 11020 | .this_mv; |
| 11021 | clamp_mv2(&cur_mv.as_mv, xd); |
| 11022 | |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 11023 | if (!mv_check_bounds(&x->mv_limits, &cur_mv.as_mv)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11024 | int_mv dummy_single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } }; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11025 | int dummy_single_newmv_rate[TOTAL_REFS_PER_FRAME] = { 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11026 | |
| 11027 | frame_mv[NEARMV][ref_frame] = cur_mv; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11028 | av1_init_rd_stats(&tmp_rd_stats); |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 11029 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11030 | // Point to variables that are not maintained between iterations |
| 11031 | args.single_newmv = dummy_single_newmv; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11032 | args.single_newmv_rate = dummy_single_newmv_rate; |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 11033 | args.modelled_rd = NULL; |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11034 | tmp_alt_rd = handle_inter_mode(cpi, x, bsize, &tmp_rd_stats, |
| 11035 | &tmp_rd_stats_y, &tmp_rd_stats_uv, |
| 11036 | &dummy_disable_skip, frame_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11037 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11038 | frame_comp_mv, |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11039 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11040 | mi_row, mi_col, &args, best_rd); |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11041 | // Prevent pointers from escaping local scope |
| 11042 | args.single_newmv = NULL; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 11043 | args.single_newmv_rate = NULL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11044 | } |
| 11045 | |
| 11046 | for (i = 0; i < mbmi->ref_mv_idx; ++i) { |
| 11047 | uint8_t drl1_ctx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11048 | drl1_ctx = av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], |
| 11049 | i + idx_offset); |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11050 | tmp_rd_stats.rate += |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11051 | (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] | 11052 | : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11053 | } |
| 11054 | |
| 11055 | if (mbmi_ext->ref_mv_count[ref_frame_type] > |
| 11056 | mbmi->ref_mv_idx + idx_offset + 1 && |
| 11057 | ref_idx < ref_set - 1) { |
| 11058 | uint8_t drl1_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11059 | av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], |
| 11060 | mbmi->ref_mv_idx + idx_offset); |
Yaowu Xu | 83ed6fe | 2016-11-22 11:15:29 -0800 | [diff] [blame] | 11061 | tmp_rd_stats.rate += |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11062 | (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] | 11063 | : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11064 | } |
| 11065 | |
| 11066 | if (tmp_alt_rd < INT64_MAX) { |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11067 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11068 | tmp_alt_rd = |
| 11069 | RDCOST(x->rdmult, tmp_rd_stats.rate, tmp_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11070 | #else |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11071 | 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] | 11072 | tmp_rd_stats.dist) < |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11073 | RDCOST(x->rdmult, 0, tmp_rd_stats.sse)) |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 11074 | tmp_alt_rd = |
| 11075 | RDCOST(x->rdmult, |
| 11076 | tmp_rd_stats.rate + |
| 11077 | av1_cost_bit(av1_get_skip_prob(cm, xd), 0), |
| 11078 | tmp_rd_stats.dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11079 | else |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 11080 | tmp_alt_rd = |
| 11081 | RDCOST(x->rdmult, |
| 11082 | tmp_rd_stats.rate + |
| 11083 | av1_cost_bit(av1_get_skip_prob(cm, xd), 1) - |
| 11084 | tmp_rd_stats_y.rate - tmp_rd_stats_uv.rate, |
| 11085 | tmp_rd_stats.sse); |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11086 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11087 | } |
| 11088 | |
| 11089 | if (tmp_ref_rd > tmp_alt_rd) { |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11090 | rate2 = tmp_rd_stats.rate; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11091 | disable_skip = dummy_disable_skip; |
Angie Chiang | 7615912 | 2016-11-09 12:13:22 -0800 | [diff] [blame] | 11092 | distortion2 = tmp_rd_stats.dist; |
| 11093 | skippable = tmp_rd_stats.skip; |
| 11094 | rate_y = tmp_rd_stats_y.rate; |
| 11095 | rate_uv = tmp_rd_stats_uv.rate; |
| 11096 | total_sse = tmp_rd_stats.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11097 | this_rd = tmp_alt_rd; |
| 11098 | tmp_ref_rd = tmp_alt_rd; |
| 11099 | backup_mbmi = *mbmi; |
| 11100 | backup_skip = x->skip; |
| 11101 | #if CONFIG_VAR_TX |
| 11102 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11103 | memcpy(x->blk_skip_drl[i], x->blk_skip[i], |
| 11104 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11105 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11106 | } else { |
| 11107 | *mbmi = backup_mbmi; |
| 11108 | x->skip = backup_skip; |
| 11109 | } |
| 11110 | } |
| 11111 | |
| 11112 | frame_mv[NEARMV][ref_frame] = backup_mv; |
| 11113 | frame_mv[NEWMV][ref_frame] = backup_fmv[0]; |
| 11114 | if (comp_pred) frame_mv[NEWMV][second_ref_frame] = backup_fmv[1]; |
| 11115 | #if CONFIG_VAR_TX |
| 11116 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11117 | memcpy(x->blk_skip[i], x->blk_skip_drl[i], |
| 11118 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11119 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11120 | } |
| 11121 | mbmi_ext->ref_mvs[ref_frame][0] = backup_ref_mv[0]; |
| 11122 | 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] | 11123 | |
| 11124 | if (this_rd == INT64_MAX) continue; |
| 11125 | |
Debargha Mukherjee | 0f248c4 | 2017-09-07 12:40:18 -0700 | [diff] [blame] | 11126 | if (is_comp_ref_allowed(mbmi->sb_type)) |
Jingning Han | c41a549 | 2017-02-24 11:18:52 -0800 | [diff] [blame] | 11127 | compmode_cost = av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11128 | |
| 11129 | if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost; |
| 11130 | } |
| 11131 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11132 | // Estimate the reference frame signaling cost and add it |
| 11133 | // to the rolling cost variable. |
| 11134 | if (comp_pred) { |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 11135 | #if CONFIG_EXT_COMP_REFS |
| 11136 | rate2 += ref_costs_comp[ref_frame][second_ref_frame]; |
| 11137 | #else // !CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11138 | rate2 += ref_costs_comp[ref_frame]; |
| 11139 | #if CONFIG_EXT_REFS |
| 11140 | rate2 += ref_costs_comp[second_ref_frame]; |
| 11141 | #endif // CONFIG_EXT_REFS |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 11142 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11143 | } else { |
| 11144 | rate2 += ref_costs_single[ref_frame]; |
| 11145 | } |
| 11146 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11147 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11148 | // Add the cost to signal single/comp mode in single ref. |
| 11149 | if (!comp_pred && cm->reference_mode != COMPOUND_REFERENCE) { |
| 11150 | aom_prob singleref_comp_mode_p = av1_get_inter_mode_prob(cm, xd); |
| 11151 | rate2 += av1_cost_bit(singleref_comp_mode_p, |
| 11152 | is_inter_singleref_comp_mode(mbmi->mode)); |
| 11153 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11154 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11155 | |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11156 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11157 | if (ref_frame == INTRA_FRAME) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11158 | #else |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11159 | if (!disable_skip) |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11160 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11161 | { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11162 | if (skippable) { |
| 11163 | // Back out the coefficient coding costs |
| 11164 | rate2 -= (rate_y + rate_uv); |
| 11165 | rate_y = 0; |
| 11166 | rate_uv = 0; |
| 11167 | // Cost the skip mb case |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11168 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11169 | } else if (ref_frame != INTRA_FRAME && !xd->lossless[mbmi->segment_id]) { |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11170 | if (RDCOST(x->rdmult, rate_y + rate_uv + rate_skip0, distortion2) < |
| 11171 | RDCOST(x->rdmult, rate_skip1, total_sse)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11172 | // Add in the cost of the no skip flag. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11173 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11174 | } else { |
| 11175 | // FIXME(rbultje) make this work for splitmv also |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11176 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11177 | distortion2 = total_sse; |
| 11178 | assert(total_sse >= 0); |
| 11179 | rate2 -= (rate_y + rate_uv); |
| 11180 | this_skip2 = 1; |
| 11181 | rate_y = 0; |
| 11182 | rate_uv = 0; |
| 11183 | } |
| 11184 | } else { |
| 11185 | // Add in the cost of the no skip flag. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11186 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11187 | } |
| 11188 | |
| 11189 | // Calculate the final RD estimate for this mode. |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11190 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11191 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11192 | } else { |
| 11193 | this_skip2 = mbmi->skip; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11194 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11195 | if (this_skip2) { |
| 11196 | rate_y = 0; |
| 11197 | rate_uv = 0; |
| 11198 | } |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11199 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11200 | } |
| 11201 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11202 | if (ref_frame == INTRA_FRAME) { |
| 11203 | // Keep record of best intra rd |
| 11204 | if (this_rd < best_intra_rd) { |
| 11205 | best_intra_rd = this_rd; |
| 11206 | best_intra_mode = mbmi->mode; |
| 11207 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11208 | #if CONFIG_INTERINTRA |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 11209 | } else if (second_ref_frame == NONE_FRAME) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11210 | if (this_rd < best_single_inter_rd) { |
| 11211 | best_single_inter_rd = this_rd; |
| 11212 | best_single_inter_ref = mbmi->ref_frame[0]; |
| 11213 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11214 | #endif // CONFIG_INTERINTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11215 | } |
| 11216 | |
| 11217 | if (!disable_skip && ref_frame == INTRA_FRAME) { |
| 11218 | for (i = 0; i < REFERENCE_MODES; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11219 | best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11220 | } |
| 11221 | |
| 11222 | // Did this mode help.. i.e. is it the new best mode |
| 11223 | if (this_rd < best_rd || x->skip) { |
| 11224 | if (!mode_excluded) { |
| 11225 | // Note index of best mode so far |
| 11226 | best_mode_index = mode_index; |
| 11227 | |
| 11228 | if (ref_frame == INTRA_FRAME) { |
| 11229 | /* required for left and above block mv */ |
| 11230 | mbmi->mv[0].as_int = 0; |
| 11231 | } else { |
| 11232 | best_pred_sse = x->pred_sse[ref_frame]; |
| 11233 | } |
| 11234 | |
| 11235 | rd_cost->rate = rate2; |
| 11236 | #if CONFIG_SUPERTX |
| 11237 | if (x->skip) |
| 11238 | *returnrate_nocoef = rate2; |
| 11239 | else |
| 11240 | *returnrate_nocoef = rate2 - rate_y - rate_uv; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11241 | *returnrate_nocoef -= av1_cost_bit( |
| 11242 | av1_get_skip_prob(cm, xd), disable_skip || skippable || this_skip2); |
| 11243 | *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd), |
| 11244 | mbmi->ref_frame[0] != INTRA_FRAME); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11245 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yue Chen | 52c5173 | 2017-07-11 15:08:30 -0700 | [diff] [blame] | 11246 | #if CONFIG_WARPED_MOTION |
| 11247 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 11248 | #endif |
Yaowu Xu | 41a36de | 2017-03-23 15:55:03 -0700 | [diff] [blame] | 11249 | #if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION |
| 11250 | MODE_INFO *const mi = xd->mi[0]; |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11251 | const MOTION_MODE motion_allowed = motion_mode_allowed( |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 11252 | #if CONFIG_GLOBAL_MOTION |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11253 | 0, xd->global_motion, |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 11254 | #endif // CONFIG_GLOBAL_MOTION |
Yue Chen | 52c5173 | 2017-07-11 15:08:30 -0700 | [diff] [blame] | 11255 | #if CONFIG_WARPED_MOTION |
| 11256 | xd, |
| 11257 | #endif |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11258 | mi); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11259 | if (motion_allowed == WARPED_CAUSAL) |
Yue Chen | bdc8dab | 2017-07-26 12:05:47 -0700 | [diff] [blame] | 11260 | *returnrate_nocoef -= x->motion_mode_cost[bsize][mbmi->motion_mode]; |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11261 | else if (motion_allowed == OBMC_CAUSAL) |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11262 | *returnrate_nocoef -= x->motion_mode_cost1[bsize][mbmi->motion_mode]; |
Yaowu Xu | 41a36de | 2017-03-23 15:55:03 -0700 | [diff] [blame] | 11263 | #else |
Yue Chen | bdc8dab | 2017-07-26 12:05:47 -0700 | [diff] [blame] | 11264 | *returnrate_nocoef -= x->motion_mode_cost[bsize][mbmi->motion_mode]; |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 11265 | #endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11266 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11267 | #endif // CONFIG_SUPERTX |
| 11268 | rd_cost->dist = distortion2; |
| 11269 | rd_cost->rdcost = this_rd; |
| 11270 | best_rd = this_rd; |
| 11271 | best_mbmode = *mbmi; |
| 11272 | best_skip2 = this_skip2; |
| 11273 | best_mode_skippable = skippable; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11274 | best_rate_y = rate_y + av1_cost_bit(av1_get_skip_prob(cm, xd), |
| 11275 | this_skip2 || skippable); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11276 | best_rate_uv = rate_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11277 | #if CONFIG_VAR_TX |
| 11278 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11279 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
| 11280 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11281 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11282 | } |
| 11283 | } |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 11284 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11285 | /* keep record of best compound/single-only prediction */ |
| 11286 | if (!disable_skip && ref_frame != INTRA_FRAME) { |
| 11287 | int64_t single_rd, hybrid_rd, single_rate, hybrid_rate; |
| 11288 | |
| 11289 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
| 11290 | single_rate = rate2 - compmode_cost; |
| 11291 | hybrid_rate = rate2; |
| 11292 | } else { |
| 11293 | single_rate = rate2; |
| 11294 | hybrid_rate = rate2 + compmode_cost; |
| 11295 | } |
| 11296 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11297 | single_rd = RDCOST(x->rdmult, single_rate, distortion2); |
| 11298 | hybrid_rd = RDCOST(x->rdmult, hybrid_rate, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11299 | |
| 11300 | if (!comp_pred) { |
| 11301 | if (single_rd < best_pred_rd[SINGLE_REFERENCE]) |
| 11302 | best_pred_rd[SINGLE_REFERENCE] = single_rd; |
| 11303 | } else { |
| 11304 | if (single_rd < best_pred_rd[COMPOUND_REFERENCE]) |
| 11305 | best_pred_rd[COMPOUND_REFERENCE] = single_rd; |
| 11306 | } |
| 11307 | if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT]) |
| 11308 | best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd; |
| 11309 | } |
| 11310 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11311 | if (x->skip && !comp_pred) break; |
| 11312 | } |
| 11313 | |
| 11314 | if (xd->lossless[mbmi->segment_id] == 0 && best_mode_index >= 0 && |
| 11315 | ((sf->tx_type_search.fast_inter_tx_type_search == 1 && |
| 11316 | is_inter_mode(best_mbmode.mode)) || |
| 11317 | (sf->tx_type_search.fast_intra_tx_type_search == 1 && |
| 11318 | !is_inter_mode(best_mbmode.mode)))) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11319 | int skip_blk = 0; |
| 11320 | RD_STATS rd_stats_y, rd_stats_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11321 | |
| 11322 | x->use_default_inter_tx_type = 0; |
| 11323 | x->use_default_intra_tx_type = 0; |
| 11324 | |
| 11325 | *mbmi = best_mbmode; |
| 11326 | |
| 11327 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 11328 | |
| 11329 | // Select prediction reference frames. |
| 11330 | for (i = 0; i < MAX_MB_PLANE; i++) { |
| 11331 | xd->plane[i].pre[0] = yv12_mb[mbmi->ref_frame[0]][i]; |
| 11332 | if (has_second_ref(mbmi)) |
| 11333 | xd->plane[i].pre[1] = yv12_mb[mbmi->ref_frame[1]][i]; |
| 11334 | } |
| 11335 | |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11336 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11337 | // Single ref compound mode |
| 11338 | if (!has_second_ref(mbmi) && is_inter_singleref_comp_mode(mbmi->mode)) { |
| 11339 | xd->block_refs[1] = xd->block_refs[0]; |
| 11340 | for (i = 0; i < MAX_MB_PLANE; i++) |
| 11341 | xd->plane[i].pre[1] = xd->plane[i].pre[0]; |
| 11342 | } |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11343 | #endif // CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11344 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11345 | if (is_inter_mode(mbmi->mode)) { |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 11346 | 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] | 11347 | #if CONFIG_MOTION_VAR |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11348 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
Fergus Simpson | 073c6f3 | 2017-02-17 12:13:48 -0800 | [diff] [blame] | 11349 | av1_build_obmc_inter_prediction( |
Fergus Simpson | 9f7ca0b | 2017-03-10 10:46:46 -0800 | [diff] [blame] | 11350 | cm, xd, mi_row, mi_col, args.above_pred_buf, args.above_pred_stride, |
| 11351 | args.left_pred_buf, args.left_pred_stride); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11352 | } |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11353 | #endif // CONFIG_MOTION_VAR |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11354 | av1_subtract_plane(x, bsize, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11355 | #if CONFIG_VAR_TX |
| 11356 | if (cm->tx_mode == TX_MODE_SELECT || xd->lossless[mbmi->segment_id]) { |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 11357 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Hui Su | 1ddf231 | 2017-08-19 15:21:34 -0700 | [diff] [blame] | 11358 | assert(rd_stats_y.rate != INT_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11359 | } else { |
| 11360 | int idx, idy; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11361 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11362 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 11363 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 11364 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11365 | memset(x->blk_skip[0], rd_stats_y.skip, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11366 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 11367 | } |
| 11368 | |
Angie Chiang | b5dda48 | 2016-11-02 16:19:58 -0700 | [diff] [blame] | 11369 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11370 | #else |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11371 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 11372 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11373 | #endif // CONFIG_VAR_TX |
| 11374 | } else { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11375 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
Angie Chiang | 284d777 | 2016-11-08 11:06:45 -0800 | [diff] [blame] | 11376 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11377 | } |
| 11378 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11379 | if (RDCOST(x->rdmult, rd_stats_y.rate + rd_stats_uv.rate, |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11380 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11381 | RDCOST(x->rdmult, 0, (rd_stats_y.sse + rd_stats_uv.sse))) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11382 | skip_blk = 1; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11383 | rd_stats_y.rate = av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 11384 | rd_stats_uv.rate = 0; |
| 11385 | rd_stats_y.dist = rd_stats_y.sse; |
| 11386 | rd_stats_uv.dist = rd_stats_uv.sse; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11387 | } else { |
| 11388 | skip_blk = 0; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11389 | 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] | 11390 | } |
| 11391 | |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11392 | if (RDCOST(x->rdmult, best_rate_y + best_rate_uv, rd_cost->dist) > |
| 11393 | RDCOST(x->rdmult, rd_stats_y.rate + rd_stats_uv.rate, |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11394 | (rd_stats_y.dist + rd_stats_uv.dist))) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11395 | #if CONFIG_VAR_TX |
| 11396 | int idx, idy; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11397 | #endif // CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11398 | best_mbmode.tx_type = mbmi->tx_type; |
| 11399 | best_mbmode.tx_size = mbmi->tx_size; |
| 11400 | #if CONFIG_VAR_TX |
| 11401 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 11402 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 11403 | best_mbmode.inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx]; |
| 11404 | |
| 11405 | for (i = 0; i < MAX_MB_PLANE; ++i) |
| 11406 | memcpy(ctx->blk_skip[i], x->blk_skip[i], |
| 11407 | sizeof(uint8_t) * ctx->num_4x4_blk); |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 11408 | |
| 11409 | best_mbmode.min_tx_size = mbmi->min_tx_size; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11410 | #endif // CONFIG_VAR_TX |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11411 | rd_cost->rate += |
| 11412 | (rd_stats_y.rate + rd_stats_uv.rate - best_rate_y - best_rate_uv); |
| 11413 | rd_cost->dist = rd_stats_y.dist + rd_stats_uv.dist; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11414 | rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11415 | best_skip2 = skip_blk; |
| 11416 | } |
| 11417 | } |
| 11418 | |
| 11419 | // 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] | 11420 | if (try_palette && !is_inter_mode(best_mbmode.mode)) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11421 | int rate2 = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11422 | #if CONFIG_SUPERTX |
| 11423 | int best_rate_nocoef; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11424 | #endif // CONFIG_SUPERTX |
Urvang Joshi | 451e0f2 | 2017-01-31 11:18:31 -0800 | [diff] [blame] | 11425 | int64_t distortion2 = 0, best_rd_palette = best_rd, this_rd, |
| 11426 | best_model_rd_palette = INT64_MAX; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 11427 | int skippable = 0, rate_overhead_palette = 0; |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11428 | RD_STATS rd_stats_y; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 11429 | TX_SIZE uv_tx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11430 | uint8_t *const best_palette_color_map = |
| 11431 | x->palette_buffer->best_palette_color_map; |
| 11432 | uint8_t *const color_map = xd->plane[0].color_index_map; |
Urvang Joshi | 451e0f2 | 2017-01-31 11:18:31 -0800 | [diff] [blame] | 11433 | MB_MODE_INFO best_mbmi_palette = best_mbmode; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11434 | |
| 11435 | mbmi->mode = DC_PRED; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 11436 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11437 | mbmi->ref_frame[0] = INTRA_FRAME; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 11438 | mbmi->ref_frame[1] = NONE_FRAME; |
Urvang Joshi | 626591d | 2016-10-24 14:13:55 -0700 | [diff] [blame] | 11439 | rate_overhead_palette = rd_pick_palette_intra_sby( |
Urvang Joshi | 451e0f2 | 2017-01-31 11:18:31 -0800 | [diff] [blame] | 11440 | cpi, x, bsize, palette_ctx, intra_mode_cost[DC_PRED], |
| 11441 | &best_mbmi_palette, best_palette_color_map, &best_rd_palette, |
| 11442 | &best_model_rd_palette, NULL, NULL, NULL, NULL); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 11443 | if (pmi->palette_size[0] == 0) goto PALETTE_EXIT; |
| 11444 | memcpy(color_map, best_palette_color_map, |
| 11445 | rows * cols * sizeof(best_palette_color_map[0])); |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11446 | super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd); |
| 11447 | if (rd_stats_y.rate == INT_MAX) goto PALETTE_EXIT; |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 11448 | uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x] |
| 11449 | [xd->plane[1].subsampling_y]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11450 | if (rate_uv_intra[uv_tx] == INT_MAX) { |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 11451 | 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] | 11452 | &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx], |
| 11453 | &skip_uvs[uv_tx], &mode_uv[uv_tx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11454 | pmi_uv[uv_tx] = *pmi; |
| 11455 | #if CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11456 | uv_angle_delta[uv_tx] = mbmi->angle_delta[1]; |
| 11457 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11458 | #if CONFIG_FILTER_INTRA |
| 11459 | filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info; |
| 11460 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11461 | } |
| 11462 | mbmi->uv_mode = mode_uv[uv_tx]; |
| 11463 | pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1]; |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 11464 | if (pmi->palette_size[1] > 0) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11465 | memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, |
| 11466 | pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE, |
| 11467 | 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0])); |
hui su | de0c70a | 2017-01-09 17:12:17 -0800 | [diff] [blame] | 11468 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11469 | #if CONFIG_EXT_INTRA |
| 11470 | mbmi->angle_delta[1] = uv_angle_delta[uv_tx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11471 | #endif // CONFIG_EXT_INTRA |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11472 | #if CONFIG_FILTER_INTRA |
| 11473 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = |
| 11474 | filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]; |
| 11475 | if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) { |
| 11476 | mbmi->filter_intra_mode_info.filter_intra_mode[1] = |
| 11477 | filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1]; |
| 11478 | } |
| 11479 | #endif // CONFIG_FILTER_INTRA |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11480 | skippable = rd_stats_y.skip && skip_uvs[uv_tx]; |
| 11481 | distortion2 = rd_stats_y.dist + dist_uvs[uv_tx]; |
| 11482 | 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] | 11483 | rate2 += ref_costs_single[INTRA_FRAME]; |
| 11484 | |
| 11485 | if (skippable) { |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11486 | rate2 -= (rd_stats_y.rate + rate_uv_tokenonly[uv_tx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11487 | #if CONFIG_SUPERTX |
| 11488 | best_rate_nocoef = rate2; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11489 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11490 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11491 | } else { |
| 11492 | #if CONFIG_SUPERTX |
Angie Chiang | 0e9a2e9 | 2016-11-08 09:45:40 -0800 | [diff] [blame] | 11493 | 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] | 11494 | #endif // CONFIG_SUPERTX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11495 | rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11496 | } |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11497 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11498 | if (this_rd < best_rd) { |
| 11499 | best_mode_index = 3; |
| 11500 | mbmi->mv[0].as_int = 0; |
| 11501 | rd_cost->rate = rate2; |
| 11502 | #if CONFIG_SUPERTX |
| 11503 | *returnrate_nocoef = best_rate_nocoef; |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11504 | #endif // CONFIG_SUPERTX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11505 | rd_cost->dist = distortion2; |
| 11506 | rd_cost->rdcost = this_rd; |
| 11507 | best_rd = this_rd; |
| 11508 | best_mbmode = *mbmi; |
| 11509 | best_skip2 = 0; |
| 11510 | best_mode_skippable = skippable; |
| 11511 | } |
| 11512 | } |
| 11513 | PALETTE_EXIT: |
| 11514 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11515 | #if CONFIG_FILTER_INTRA |
| 11516 | // 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] | 11517 | // avoid a unit test failure |
Urvang Joshi | c6300aa | 2017-06-01 14:46:23 -0700 | [diff] [blame] | 11518 | if (!xd->lossless[mbmi->segment_id] && pmi->palette_size[0] == 0 && |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 11519 | !dc_skipped && best_mode_index >= 0 && |
| 11520 | best_intra_rd < (best_rd + (best_rd >> 3))) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11521 | pick_filter_intra_interframe( |
Luc Trudeau | 9d4cbb8 | 2017-07-27 17:01:32 -0400 | [diff] [blame] | 11522 | 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] | 11523 | dist_uvs, skip_uvs, mode_uv, filter_intra_mode_info_uv, |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11524 | #if CONFIG_EXT_INTRA |
| 11525 | uv_angle_delta, |
| 11526 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | c6300aa | 2017-06-01 14:46:23 -0700 | [diff] [blame] | 11527 | pmi_uv, palette_ctx, 0, ref_costs_single, &best_rd, &best_intra_rd, |
| 11528 | &best_intra_mode, &best_mode_index, &best_skip2, &best_mode_skippable, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11529 | #if CONFIG_SUPERTX |
| 11530 | returnrate_nocoef, |
| 11531 | #endif // CONFIG_SUPERTX |
| 11532 | best_pred_rd, &best_mbmode, rd_cost); |
| 11533 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11534 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11535 | |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11536 | // The inter modes' rate costs are not calculated precisely in some cases. |
| 11537 | // Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and |
| 11538 | // ZEROMV. Here, checks are added for those cases, and the mode decisions |
| 11539 | // are corrected. |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11540 | #if CONFIG_COMPOUND_SINGLEREF |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11541 | // NOTE: For SR_NEW_NEWMV, no need to check as the two mvs from the same ref |
| 11542 | // are surely different from each other. |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11543 | #endif // CONFIG_COMPOUND_SINGLEREF |
| 11544 | if (best_mbmode.mode == NEWMV || best_mbmode.mode == NEW_NEWMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11545 | const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0], |
| 11546 | best_mbmode.ref_frame[1] }; |
| 11547 | int comp_pred_mode = refs[1] > INTRA_FRAME; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11548 | int_mv zeromv[2]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11549 | 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] | 11550 | #if CONFIG_GLOBAL_MOTION |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 11551 | zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]], |
| 11552 | cm->allow_high_precision_mv, bsize, |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 11553 | mi_col, mi_row, 0 |
| 11554 | #if CONFIG_AMVR |
| 11555 | , |
| 11556 | cm->cur_frame_mv_precision_level |
| 11557 | #endif |
| 11558 | ) |
Debargha Mukherjee | febb59c | 2017-03-02 12:23:45 -0800 | [diff] [blame] | 11559 | .as_int; |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 11560 | zeromv[1].as_int = |
| 11561 | comp_pred_mode |
| 11562 | ? gm_get_motion_vector(&cm->global_motion[refs[1]], |
| 11563 | cm->allow_high_precision_mv, bsize, mi_col, |
| 11564 | mi_row, 0 |
| 11565 | #if CONFIG_AMVR |
| 11566 | , |
| 11567 | cm->cur_frame_mv_precision_level |
| 11568 | #endif |
| 11569 | ) |
| 11570 | .as_int |
| 11571 | : 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11572 | #else |
| 11573 | zeromv[0].as_int = 0; |
| 11574 | zeromv[1].as_int = 0; |
| 11575 | #endif // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11576 | if (!comp_pred_mode) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11577 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11578 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11579 | : INT_MAX; |
| 11580 | |
| 11581 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 11582 | int_mv cur_mv = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 11583 | if (cur_mv.as_int == best_mbmode.mv[0].as_int) { |
| 11584 | best_mbmode.mode = NEARMV; |
| 11585 | best_mbmode.ref_mv_idx = i; |
| 11586 | } |
| 11587 | } |
| 11588 | |
| 11589 | if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int) |
| 11590 | best_mbmode.mode = NEARESTMV; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11591 | else if (best_mbmode.mv[0].as_int == zeromv[0].as_int) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11592 | best_mbmode.mode = ZEROMV; |
| 11593 | } else { |
| 11594 | int_mv nearestmv[2]; |
| 11595 | int_mv nearmv[2]; |
| 11596 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11597 | if (mbmi_ext->ref_mv_count[rf_type] > 1) { |
| 11598 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][1].this_mv; |
| 11599 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][1].comp_mv; |
| 11600 | } else { |
| 11601 | nearmv[0] = frame_mv[NEARMV][refs[0]]; |
| 11602 | nearmv[1] = frame_mv[NEARMV][refs[1]]; |
| 11603 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11604 | if (mbmi_ext->ref_mv_count[rf_type] >= 1) { |
| 11605 | nearestmv[0] = mbmi_ext->ref_mv_stack[rf_type][0].this_mv; |
| 11606 | nearestmv[1] = mbmi_ext->ref_mv_stack[rf_type][0].comp_mv; |
| 11607 | } else { |
| 11608 | nearestmv[0] = frame_mv[NEARESTMV][refs[0]]; |
| 11609 | nearestmv[1] = frame_mv[NEARESTMV][refs[1]]; |
| 11610 | } |
| 11611 | |
| 11612 | if (nearestmv[0].as_int == best_mbmode.mv[0].as_int && |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11613 | nearestmv[1].as_int == best_mbmode.mv[1].as_int) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11614 | best_mbmode.mode = NEAREST_NEARESTMV; |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 11615 | } else { |
| 11616 | int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2) |
| 11617 | ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2) |
| 11618 | : INT_MAX; |
| 11619 | |
| 11620 | for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) { |
| 11621 | nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv; |
| 11622 | nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][i + 1].comp_mv; |
| 11623 | |
Debargha Mukherjee | bb6e134 | 2017-04-17 16:05:04 -0700 | [diff] [blame] | 11624 | // Try switching to the NEAR_NEARMV mode |
| 11625 | if (nearmv[0].as_int == best_mbmode.mv[0].as_int && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 11626 | nearmv[1].as_int == best_mbmode.mv[1].as_int) { |
| 11627 | best_mbmode.mode = NEAR_NEARMV; |
| 11628 | best_mbmode.ref_mv_idx = i; |
| 11629 | } |
| 11630 | } |
| 11631 | |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11632 | if (best_mbmode.mode == NEW_NEWMV && |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 11633 | best_mbmode.mv[0].as_int == zeromv[0].as_int && |
| 11634 | best_mbmode.mv[1].as_int == zeromv[1].as_int) |
| 11635 | best_mbmode.mode = ZERO_ZEROMV; |
| 11636 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11637 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11638 | } |
| 11639 | |
David Barker | 9620bcd | 2017-03-22 14:46:42 +0000 | [diff] [blame] | 11640 | // Make sure that the ref_mv_idx is only nonzero when we're |
| 11641 | // using a mode which can support ref_mv_idx |
| 11642 | if (best_mbmode.ref_mv_idx != 0 && |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11643 | #if CONFIG_COMPOUND_SINGLEREF |
| 11644 | !(best_mbmode.mode == NEWMV || best_mbmode.mode == SR_NEW_NEWMV || |
| 11645 | best_mbmode.mode == NEW_NEWMV || |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11646 | have_nearmv_in_inter_mode(best_mbmode.mode))) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11647 | #else // !CONFIG_COMPOUND_SINGLEREF |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 11648 | !(best_mbmode.mode == NEWMV || best_mbmode.mode == NEW_NEWMV || |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11649 | have_nearmv_in_inter_mode(best_mbmode.mode))) |
Zoe Liu | 85b6646 | 2017-04-20 14:28:19 -0700 | [diff] [blame] | 11650 | #endif // CONFIG_COMPOUND_SINGLEREF |
Yushin Cho | 67dda51 | 2017-07-25 14:58:22 -0700 | [diff] [blame] | 11651 | { |
David Barker | 9620bcd | 2017-03-22 14:46:42 +0000 | [diff] [blame] | 11652 | best_mbmode.ref_mv_idx = 0; |
| 11653 | } |
| 11654 | |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame^] | 11655 | if (best_mbmode.ref_frame[0] > INTRA_FRAME && |
| 11656 | best_mbmode.ref_frame[1] <= INTRA_FRAME) { |
Jingning Han | 731af49 | 2016-11-17 11:53:23 -0800 | [diff] [blame] | 11657 | int8_t ref_frame_type = av1_ref_frame_type(best_mbmode.ref_frame); |
| 11658 | int16_t mode_ctx = mbmi_ext->mode_context[ref_frame_type]; |
David Barker | 68e6e86 | 2016-11-24 15:10:15 +0000 | [diff] [blame] | 11659 | if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) { |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame^] | 11660 | int_mv zeromv; |
David Barker | 68e6e86 | 2016-11-24 15:10:15 +0000 | [diff] [blame] | 11661 | #if CONFIG_GLOBAL_MOTION |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame^] | 11662 | const MV_REFERENCE_FRAME ref = best_mbmode.ref_frame[0]; |
| 11663 | zeromv.as_int = gm_get_motion_vector(&cm->global_motion[ref], |
| 11664 | cm->allow_high_precision_mv, bsize, |
| 11665 | mi_col, mi_row, 0 |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 11666 | #if CONFIG_AMVR |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame^] | 11667 | , |
| 11668 | cm->cur_frame_mv_precision_level |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 11669 | #endif |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame^] | 11670 | ) |
| 11671 | .as_int; |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 11672 | #else |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame^] | 11673 | zeromv.as_int = 0; |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 11674 | #endif // CONFIG_GLOBAL_MOTION |
Rupert Swarbrick | 799ff70 | 2017-10-04 17:37:52 +0100 | [diff] [blame^] | 11675 | if (best_mbmode.mv[0].as_int == zeromv.as_int) { |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 11676 | best_mbmode.mode = ZEROMV; |
| 11677 | } |
David Barker | 68e6e86 | 2016-11-24 15:10:15 +0000 | [diff] [blame] | 11678 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11679 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11680 | |
| 11681 | if (best_mode_index < 0 || best_rd >= best_rd_so_far) { |
| 11682 | rd_cost->rate = INT_MAX; |
| 11683 | rd_cost->rdcost = INT64_MAX; |
| 11684 | return; |
| 11685 | } |
| 11686 | |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 11687 | assert((cm->interp_filter == SWITCHABLE) || |
| 11688 | (cm->interp_filter == |
| 11689 | av1_extract_interp_filter(best_mbmode.interp_filters, 0)) || |
| 11690 | !is_inter_block(&best_mbmode)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11691 | #if CONFIG_DUAL_FILTER |
| 11692 | assert((cm->interp_filter == SWITCHABLE) || |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 11693 | (cm->interp_filter == |
| 11694 | av1_extract_interp_filter(best_mbmode.interp_filters, 1)) || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11695 | !is_inter_block(&best_mbmode)); |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 11696 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11697 | |
| 11698 | if (!cpi->rc.is_src_frame_alt_ref) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11699 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
| 11700 | sf->adaptive_rd_thresh, bsize, best_mode_index); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11701 | |
| 11702 | // macroblock modes |
| 11703 | *mbmi = best_mbmode; |
| 11704 | x->skip |= best_skip2; |
| 11705 | |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 11706 | // Note: this section is needed since the mode may have been forced to |
| 11707 | // ZEROMV by the all-zero mode handling of ref-mv. |
| 11708 | #if CONFIG_GLOBAL_MOTION |
Sebastien Alaiwan | 0bdea0d | 2017-10-02 15:15:05 +0200 | [diff] [blame] | 11709 | if (mbmi->mode == ZEROMV || mbmi->mode == ZERO_ZEROMV) { |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11710 | #if CONFIG_WARPED_MOTION || CONFIG_MOTION_VAR |
| 11711 | // Correct the motion mode for ZEROMV |
Sarah Parker | 0eea89f | 2017-07-11 11:56:36 -0700 | [diff] [blame] | 11712 | const MOTION_MODE last_motion_mode_allowed = |
Yue Chen | 52c5173 | 2017-07-11 15:08:30 -0700 | [diff] [blame] | 11713 | motion_mode_allowed(0, xd->global_motion, |
| 11714 | #if CONFIG_WARPED_MOTION |
| 11715 | xd, |
| 11716 | #endif |
| 11717 | xd->mi[0]); |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 11718 | if (mbmi->motion_mode > last_motion_mode_allowed) |
| 11719 | mbmi->motion_mode = last_motion_mode_allowed; |
| 11720 | #endif // CONFIG_WARPED_MOTION || CONFIG_MOTION_VAR |
| 11721 | |
| 11722 | // Correct the interpolation filter for ZEROMV |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 11723 | if (is_nontrans_global_motion(xd)) { |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 11724 | mbmi->interp_filters = av1_broadcast_interp_filter( |
| 11725 | av1_unswitchable_filter(cm->interp_filter)); |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 11726 | } |
| 11727 | } |
| 11728 | #endif // CONFIG_GLOBAL_MOTION |
| 11729 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11730 | for (i = 0; i < 1 + has_second_ref(mbmi); ++i) { |
| 11731 | if (mbmi->mode != NEWMV) |
| 11732 | mbmi->pred_mv[i].as_int = mbmi->mv[i].as_int; |
| 11733 | else |
| 11734 | mbmi->pred_mv[i].as_int = mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_int; |
| 11735 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11736 | |
| 11737 | for (i = 0; i < REFERENCE_MODES; ++i) { |
| 11738 | if (best_pred_rd[i] == INT64_MAX) |
| 11739 | best_pred_diff[i] = INT_MIN; |
| 11740 | else |
| 11741 | best_pred_diff[i] = best_rd - best_pred_rd[i]; |
| 11742 | } |
| 11743 | |
| 11744 | x->skip |= best_mode_skippable; |
| 11745 | |
| 11746 | assert(best_mode_index >= 0); |
| 11747 | |
| 11748 | store_coding_context(x, ctx, best_mode_index, best_pred_diff, |
| 11749 | best_mode_skippable); |
| 11750 | |
Urvang Joshi | c9e71d4 | 2017-08-09 18:58:33 -0700 | [diff] [blame] | 11751 | if (pmi->palette_size[1] > 0) { |
| 11752 | assert(try_palette); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11753 | restore_uv_color_map(cpi, x); |
| 11754 | } |
| 11755 | } |
| 11756 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 11757 | void av1_rd_pick_inter_mode_sb_seg_skip(const AV1_COMP *cpi, |
| 11758 | TileDataEnc *tile_data, MACROBLOCK *x, |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 11759 | int mi_row, int mi_col, |
Angie Chiang | 2a2a7dd | 2017-04-25 16:08:47 -0700 | [diff] [blame] | 11760 | RD_STATS *rd_cost, BLOCK_SIZE bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11761 | PICK_MODE_CONTEXT *ctx, |
| 11762 | int64_t best_rd_so_far) { |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 11763 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11764 | MACROBLOCKD *const xd = &x->e_mbd; |
| 11765 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 11766 | unsigned char segment_id = mbmi->segment_id; |
| 11767 | const int comp_pred = 0; |
| 11768 | int i; |
| 11769 | int64_t best_pred_diff[REFERENCE_MODES]; |
| 11770 | unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 11771 | #if CONFIG_EXT_COMP_REFS |
| 11772 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME][TOTAL_REFS_PER_FRAME]; |
| 11773 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11774 | unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 11775 | #endif // CONFIG_EXT_COMP_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11776 | aom_prob comp_mode_p; |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 11777 | InterpFilter best_filter = SWITCHABLE; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11778 | int64_t this_rd = INT64_MAX; |
| 11779 | int rate2 = 0; |
| 11780 | const int64_t distortion2 = 0; |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 11781 | (void)mi_row; |
| 11782 | (void)mi_col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11783 | |
| 11784 | estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp, |
| 11785 | &comp_mode_p); |
| 11786 | |
| 11787 | for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX; |
| 11788 | for (i = LAST_FRAME; i < TOTAL_REFS_PER_FRAME; ++i) |
| 11789 | x->pred_mv_sad[i] = INT_MAX; |
| 11790 | |
| 11791 | rd_cost->rate = INT_MAX; |
| 11792 | |
| 11793 | assert(segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)); |
| 11794 | |
| 11795 | mbmi->palette_mode_info.palette_size[0] = 0; |
| 11796 | mbmi->palette_mode_info.palette_size[1] = 0; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 11797 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 11798 | #if CONFIG_FILTER_INTRA |
| 11799 | mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0; |
| 11800 | mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0; |
| 11801 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11802 | mbmi->mode = ZEROMV; |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11803 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
Luc Trudeau | d6d9eee | 2017-07-12 12:36:50 -0400 | [diff] [blame] | 11804 | mbmi->uv_mode = UV_DC_PRED; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11805 | mbmi->ref_frame[0] = LAST_FRAME; |
Emil Keyder | 01770b3 | 2017-01-20 18:03:11 -0500 | [diff] [blame] | 11806 | mbmi->ref_frame[1] = NONE_FRAME; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11807 | #if CONFIG_GLOBAL_MOTION |
| 11808 | mbmi->mv[0].as_int = |
Sarah Parker | ae7c458 | 2017-02-28 16:30:30 -0800 | [diff] [blame] | 11809 | gm_get_motion_vector(&cm->global_motion[mbmi->ref_frame[0]], |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 11810 | cm->allow_high_precision_mv, bsize, mi_col, mi_row, 0 |
| 11811 | #if CONFIG_AMVR |
| 11812 | , |
| 11813 | cm->cur_frame_mv_precision_level |
| 11814 | #endif |
| 11815 | ) |
David Barker | cdcac6d | 2016-12-01 17:04:16 +0000 | [diff] [blame] | 11816 | .as_int; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11817 | #else // CONFIG_GLOBAL_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11818 | mbmi->mv[0].as_int = 0; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11819 | #endif // CONFIG_GLOBAL_MOTION |
Jingning Han | 6408895 | 2016-07-11 11:24:24 -0700 | [diff] [blame] | 11820 | mbmi->tx_size = max_txsize_lookup[bsize]; |
Yaowu Xu | ee775b1 | 2016-10-18 10:00:21 -0700 | [diff] [blame] | 11821 | x->skip = 1; |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 11822 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11823 | mbmi->ref_mv_idx = 0; |
| 11824 | mbmi->pred_mv[0].as_int = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11825 | |
Yue Chen | dead17d | 2017-04-20 16:19:39 -0700 | [diff] [blame] | 11826 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 11827 | #if CONFIG_MOTION_VAR |
| 11828 | av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col); |
| 11829 | #endif |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 11830 | #if CONFIG_WARPED_MOTION |
| 11831 | if (is_motion_variation_allowed_bsize(bsize) && !has_second_ref(mbmi)) { |
| 11832 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
Yunqing Wang | 1bc8286 | 2017-06-28 15:49:48 -0700 | [diff] [blame] | 11833 | #if WARPED_MOTION_SORT_SAMPLES |
| 11834 | int pts_mv[SAMPLES_ARRAY_SIZE]; |
| 11835 | mbmi->num_proj_ref[0] = |
| 11836 | findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv); |
| 11837 | // Rank the samples by motion vector difference |
| 11838 | if (mbmi->num_proj_ref[0] > 1) |
| 11839 | mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts, |
| 11840 | pts_inref, mbmi->num_proj_ref[0]); |
| 11841 | #else |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 11842 | 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] | 11843 | #endif // WARPED_MOTION_SORT_SAMPLES |
Yue Chen | f3e1ead | 2017-04-21 14:05:51 -0700 | [diff] [blame] | 11844 | } |
| 11845 | #endif |
Yue Chen | dead17d | 2017-04-20 16:19:39 -0700 | [diff] [blame] | 11846 | |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 11847 | set_default_interp_filters(mbmi, cm->interp_filter); |
| 11848 | |
| 11849 | if (cm->interp_filter != SWITCHABLE) { |
| 11850 | best_filter = cm->interp_filter; |
| 11851 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11852 | best_filter = EIGHTTAP_REGULAR; |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 11853 | if (av1_is_interp_needed(xd) && av1_is_interp_search_needed(xd) && |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11854 | x->source_variance >= cpi->sf.disable_filter_search_var_thresh) { |
| 11855 | int rs; |
| 11856 | int best_rs = INT_MAX; |
| 11857 | for (i = 0; i < SWITCHABLE_FILTERS; ++i) { |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 11858 | mbmi->interp_filters = av1_broadcast_interp_filter(i); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11859 | rs = av1_get_switchable_rate(cm, x, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11860 | if (rs < best_rs) { |
| 11861 | best_rs = rs; |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 11862 | best_filter = av1_extract_interp_filter(mbmi->interp_filters, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11863 | } |
| 11864 | } |
| 11865 | } |
| 11866 | } |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 11867 | // Set the appropriate filter |
| 11868 | mbmi->interp_filters = av1_broadcast_interp_filter(best_filter); |
Yue Chen | b23d00a | 2017-07-28 17:01:21 -0700 | [diff] [blame] | 11869 | rate2 += av1_get_switchable_rate(cm, x, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11870 | |
| 11871 | if (cm->reference_mode == REFERENCE_MODE_SELECT) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11872 | rate2 += av1_cost_bit(comp_mode_p, comp_pred); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11873 | |
| 11874 | // Estimate the reference frame signaling cost and add it |
| 11875 | // to the rolling cost variable. |
| 11876 | rate2 += ref_costs_single[LAST_FRAME]; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 11877 | this_rd = RDCOST(x->rdmult, rate2, distortion2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11878 | |
| 11879 | rd_cost->rate = rate2; |
| 11880 | rd_cost->dist = distortion2; |
| 11881 | rd_cost->rdcost = this_rd; |
Yushin Cho | 8e75e8b | 2017-09-12 16:33:28 -0700 | [diff] [blame] | 11882 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11883 | if (this_rd >= best_rd_so_far) { |
| 11884 | rd_cost->rate = INT_MAX; |
| 11885 | rd_cost->rdcost = INT64_MAX; |
| 11886 | return; |
| 11887 | } |
| 11888 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11889 | assert((cm->interp_filter == SWITCHABLE) || |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 11890 | (cm->interp_filter == |
| 11891 | av1_extract_interp_filter(mbmi->interp_filters, 0))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11892 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11893 | av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact, |
| 11894 | cpi->sf.adaptive_rd_thresh, bsize, THR_ZEROMV); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11895 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 11896 | av1_zero(best_pred_diff); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11897 | |
| 11898 | store_coding_context(x, ctx, THR_ZEROMV, best_pred_diff, 0); |
| 11899 | } |
| 11900 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 11901 | #if CONFIG_MOTION_VAR |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 11902 | |
| 11903 | struct calc_target_weighted_pred_ctxt { |
| 11904 | const MACROBLOCK *x; |
| 11905 | const uint8_t *tmp; |
| 11906 | int tmp_stride; |
| 11907 | int overlap; |
| 11908 | }; |
| 11909 | |
| 11910 | static INLINE void calc_target_weighted_pred_above(MACROBLOCKD *xd, |
| 11911 | int rel_mi_col, |
| 11912 | uint8_t nb_mi_width, |
| 11913 | MODE_INFO *nb_mi, |
| 11914 | void *fun_ctxt) { |
| 11915 | (void)nb_mi; |
| 11916 | |
| 11917 | struct calc_target_weighted_pred_ctxt *ctxt = |
| 11918 | (struct calc_target_weighted_pred_ctxt *)fun_ctxt; |
| 11919 | |
| 11920 | #if CONFIG_HIGHBITDEPTH |
| 11921 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 11922 | #else |
| 11923 | const int is_hbd = 0; |
| 11924 | #endif // CONFIG_HIGHBITDEPTH |
| 11925 | |
| 11926 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 11927 | const uint8_t *const mask1d = av1_get_obmc_mask(ctxt->overlap); |
| 11928 | |
| 11929 | int32_t *wsrc = ctxt->x->wsrc_buf + (rel_mi_col * MI_SIZE); |
| 11930 | int32_t *mask = ctxt->x->mask_buf + (rel_mi_col * MI_SIZE); |
| 11931 | const uint8_t *tmp = ctxt->tmp + rel_mi_col * MI_SIZE; |
| 11932 | |
| 11933 | if (!is_hbd) { |
| 11934 | for (int row = 0; row < ctxt->overlap; ++row) { |
| 11935 | const uint8_t m0 = mask1d[row]; |
| 11936 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11937 | for (int col = 0; col < nb_mi_width * MI_SIZE; ++col) { |
| 11938 | wsrc[col] = m1 * tmp[col]; |
| 11939 | mask[col] = m0; |
| 11940 | } |
| 11941 | wsrc += bw; |
| 11942 | mask += bw; |
| 11943 | tmp += ctxt->tmp_stride; |
| 11944 | } |
| 11945 | #if CONFIG_HIGHBITDEPTH |
| 11946 | } else { |
| 11947 | const uint16_t *tmp16 = CONVERT_TO_SHORTPTR(tmp); |
| 11948 | |
| 11949 | for (int row = 0; row < ctxt->overlap; ++row) { |
| 11950 | const uint8_t m0 = mask1d[row]; |
| 11951 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11952 | for (int col = 0; col < nb_mi_width * MI_SIZE; ++col) { |
| 11953 | wsrc[col] = m1 * tmp16[col]; |
| 11954 | mask[col] = m0; |
| 11955 | } |
| 11956 | wsrc += bw; |
| 11957 | mask += bw; |
| 11958 | tmp16 += ctxt->tmp_stride; |
| 11959 | } |
| 11960 | #endif // CONFIG_HIGHBITDEPTH |
| 11961 | } |
| 11962 | } |
| 11963 | |
| 11964 | static INLINE void calc_target_weighted_pred_left(MACROBLOCKD *xd, |
| 11965 | int rel_mi_row, |
| 11966 | uint8_t nb_mi_height, |
| 11967 | MODE_INFO *nb_mi, |
| 11968 | void *fun_ctxt) { |
| 11969 | (void)nb_mi; |
| 11970 | |
| 11971 | struct calc_target_weighted_pred_ctxt *ctxt = |
| 11972 | (struct calc_target_weighted_pred_ctxt *)fun_ctxt; |
| 11973 | |
| 11974 | #if CONFIG_HIGHBITDEPTH |
| 11975 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 11976 | #else |
| 11977 | const int is_hbd = 0; |
| 11978 | #endif // CONFIG_HIGHBITDEPTH |
| 11979 | |
| 11980 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 11981 | const uint8_t *const mask1d = av1_get_obmc_mask(ctxt->overlap); |
| 11982 | |
| 11983 | int32_t *wsrc = ctxt->x->wsrc_buf + (rel_mi_row * MI_SIZE * bw); |
| 11984 | int32_t *mask = ctxt->x->mask_buf + (rel_mi_row * MI_SIZE * bw); |
| 11985 | const uint8_t *tmp = ctxt->tmp + (rel_mi_row * MI_SIZE * ctxt->tmp_stride); |
| 11986 | |
| 11987 | if (!is_hbd) { |
| 11988 | for (int row = 0; row < nb_mi_height * MI_SIZE; ++row) { |
| 11989 | for (int col = 0; col < ctxt->overlap; ++col) { |
| 11990 | const uint8_t m0 = mask1d[col]; |
| 11991 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 11992 | wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 + |
| 11993 | (tmp[col] << AOM_BLEND_A64_ROUND_BITS) * m1; |
| 11994 | mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0; |
| 11995 | } |
| 11996 | wsrc += bw; |
| 11997 | mask += bw; |
| 11998 | tmp += ctxt->tmp_stride; |
| 11999 | } |
| 12000 | #if CONFIG_HIGHBITDEPTH |
| 12001 | } else { |
| 12002 | const uint16_t *tmp16 = CONVERT_TO_SHORTPTR(tmp); |
| 12003 | |
| 12004 | for (int row = 0; row < nb_mi_height * MI_SIZE; ++row) { |
| 12005 | for (int col = 0; col < ctxt->overlap; ++col) { |
| 12006 | const uint8_t m0 = mask1d[col]; |
| 12007 | const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0; |
| 12008 | wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 + |
| 12009 | (tmp16[col] << AOM_BLEND_A64_ROUND_BITS) * m1; |
| 12010 | mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0; |
| 12011 | } |
| 12012 | wsrc += bw; |
| 12013 | mask += bw; |
| 12014 | tmp16 += ctxt->tmp_stride; |
| 12015 | } |
| 12016 | #endif // CONFIG_HIGHBITDEPTH |
| 12017 | } |
| 12018 | } |
| 12019 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12020 | // This function has a structure similar to av1_build_obmc_inter_prediction |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12021 | // |
| 12022 | // The OBMC predictor is computed as: |
| 12023 | // |
| 12024 | // PObmc(x,y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12025 | // AOM_BLEND_A64(Mh(x), |
| 12026 | // AOM_BLEND_A64(Mv(y), P(x,y), PAbove(x,y)), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12027 | // PLeft(x, y)) |
| 12028 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12029 | // 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] | 12030 | // rounding, this can be written as: |
| 12031 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12032 | // 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] | 12033 | // Mh(x) * Mv(y) * P(x,y) + |
| 12034 | // Mh(x) * Cv(y) * Pabove(x,y) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12035 | // AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12036 | // |
| 12037 | // Where : |
| 12038 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12039 | // Cv(y) = AOM_BLEND_A64_MAX_ALPHA - Mv(y) |
| 12040 | // Ch(y) = AOM_BLEND_A64_MAX_ALPHA - Mh(y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12041 | // |
| 12042 | // This function computes 'wsrc' and 'mask' as: |
| 12043 | // |
| 12044 | // wsrc(x, y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12045 | // 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] | 12046 | // Mh(x) * Cv(y) * Pabove(x,y) + |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12047 | // AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12048 | // |
| 12049 | // mask(x, y) = Mh(x) * Mv(y) |
| 12050 | // |
| 12051 | // These can then be used to efficiently approximate the error for any |
| 12052 | // predictor P in the context of the provided neighbouring predictors by |
| 12053 | // computing: |
| 12054 | // |
| 12055 | // error(x, y) = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12056 | // 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] | 12057 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12058 | 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] | 12059 | const MACROBLOCKD *xd, int mi_row, |
| 12060 | int mi_col, const uint8_t *above, |
| 12061 | int above_stride, const uint8_t *left, |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 12062 | int left_stride) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12063 | const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type; |
Jingning Han | ff6ee6a | 2016-12-07 09:55:21 -0800 | [diff] [blame] | 12064 | const int bw = xd->n8_w << MI_SIZE_LOG2; |
| 12065 | const int bh = xd->n8_h << MI_SIZE_LOG2; |
Yue Chen | e9638cc | 2016-10-10 12:37:54 -0700 | [diff] [blame] | 12066 | int32_t *mask_buf = x->mask_buf; |
| 12067 | int32_t *wsrc_buf = x->wsrc_buf; |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12068 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12069 | 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] | 12070 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12071 | const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
| 12072 | #else |
| 12073 | const int is_hbd = 0; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 12074 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12075 | |
| 12076 | // plane 0 should not be subsampled |
| 12077 | assert(xd->plane[0].subsampling_x == 0); |
| 12078 | assert(xd->plane[0].subsampling_y == 0); |
| 12079 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12080 | av1_zero_array(wsrc_buf, bw * bh); |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12081 | 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] | 12082 | |
| 12083 | // handle above row |
| 12084 | if (xd->up_available) { |
Jingning Han | 440d425 | 2017-07-24 14:07:34 -0700 | [diff] [blame] | 12085 | const int overlap = |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12086 | AOMMIN(block_size_high[bsize], block_size_high[BLOCK_64X64]) >> 1; |
| 12087 | struct calc_target_weighted_pred_ctxt ctxt = { x, above, above_stride, |
| 12088 | overlap }; |
| 12089 | foreach_overlappable_nb_above(cm, (MACROBLOCKD *)xd, mi_col, |
| 12090 | max_neighbor_obmc[b_width_log2_lookup[bsize]], |
| 12091 | calc_target_weighted_pred_above, &ctxt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12092 | } |
| 12093 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12094 | for (int i = 0; i < bw * bh; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12095 | wsrc_buf[i] *= AOM_BLEND_A64_MAX_ALPHA; |
| 12096 | mask_buf[i] *= AOM_BLEND_A64_MAX_ALPHA; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12097 | } |
| 12098 | |
| 12099 | // handle left column |
| 12100 | if (xd->left_available) { |
Jingning Han | 440d425 | 2017-07-24 14:07:34 -0700 | [diff] [blame] | 12101 | const int overlap = |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12102 | AOMMIN(block_size_wide[bsize], block_size_wide[BLOCK_64X64]) >> 1; |
| 12103 | struct calc_target_weighted_pred_ctxt ctxt = { x, left, left_stride, |
| 12104 | overlap }; |
| 12105 | foreach_overlappable_nb_left(cm, (MACROBLOCKD *)xd, mi_row, |
| 12106 | max_neighbor_obmc[b_height_log2_lookup[bsize]], |
| 12107 | calc_target_weighted_pred_left, &ctxt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12108 | } |
| 12109 | |
| 12110 | if (!is_hbd) { |
| 12111 | const uint8_t *src = x->plane[0].src.buf; |
| 12112 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12113 | for (int row = 0; row < bh; ++row) { |
| 12114 | for (int col = 0; col < bw; ++col) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12115 | wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col]; |
| 12116 | } |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12117 | wsrc_buf += bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12118 | src += x->plane[0].src.stride; |
| 12119 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 12120 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12121 | } else { |
| 12122 | const uint16_t *src = CONVERT_TO_SHORTPTR(x->plane[0].src.buf); |
| 12123 | |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12124 | for (int row = 0; row < bh; ++row) { |
| 12125 | for (int col = 0; col < bw; ++col) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12126 | wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col]; |
| 12127 | } |
Rupert Swarbrick | c0cea7f | 2017-08-22 14:06:56 +0100 | [diff] [blame] | 12128 | wsrc_buf += bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12129 | src += x->plane[0].src.stride; |
| 12130 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 12131 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12132 | } |
| 12133 | } |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12134 | |
| 12135 | #if CONFIG_NCOBMC |
| 12136 | void av1_check_ncobmc_rd(const struct AV1_COMP *cpi, struct macroblock *x, |
| 12137 | int mi_row, int mi_col) { |
| 12138 | const AV1_COMMON *const cm = &cpi->common; |
| 12139 | MACROBLOCKD *const xd = &x->e_mbd; |
| 12140 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 12141 | MB_MODE_INFO backup_mbmi; |
| 12142 | BLOCK_SIZE bsize = mbmi->sb_type; |
| 12143 | int ref, skip_blk, backup_skip = x->skip; |
| 12144 | int64_t rd_causal; |
| 12145 | RD_STATS rd_stats_y, rd_stats_uv; |
| 12146 | int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 12147 | int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1); |
| 12148 | |
| 12149 | // Recompute the best causal predictor and rd |
| 12150 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 12151 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 12152 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 12153 | YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mbmi->ref_frame[ref]); |
| 12154 | assert(cfg != NULL); |
| 12155 | av1_setup_pre_planes(xd, ref, cfg, mi_row, mi_col, |
| 12156 | &xd->block_refs[ref]->sf); |
| 12157 | } |
Jingning Han | 91d9a79 | 2017-04-18 12:01:52 -0700 | [diff] [blame] | 12158 | av1_setup_dst_planes(x->e_mbd.plane, bsize, |
| 12159 | get_frame_new_buffer(&cpi->common), mi_row, mi_col); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12160 | |
Jingning Han | c44009c | 2017-05-06 11:36:49 -0700 | [diff] [blame] | 12161 | 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] | 12162 | |
| 12163 | av1_subtract_plane(x, bsize, 0); |
Wei-Ting Lin | ba5f187 | 2017-07-06 12:26:09 -0700 | [diff] [blame] | 12164 | #if CONFIG_VAR_TX |
| 12165 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
| 12166 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12167 | } else { |
| 12168 | int idx, idy; |
| 12169 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12170 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 12171 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 12172 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 12173 | memset(x->blk_skip[0], rd_stats_y.skip, |
| 12174 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 12175 | } |
| 12176 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 12177 | #else |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12178 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12179 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Wei-Ting Lin | ba5f187 | 2017-07-06 12:26:09 -0700 | [diff] [blame] | 12180 | #endif |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12181 | assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX); |
| 12182 | if (rd_stats_y.skip && rd_stats_uv.skip) { |
| 12183 | rd_stats_y.rate = rate_skip1; |
| 12184 | rd_stats_uv.rate = 0; |
| 12185 | rd_stats_y.dist = rd_stats_y.sse; |
| 12186 | rd_stats_uv.dist = rd_stats_uv.sse; |
| 12187 | skip_blk = 0; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12188 | } else if (RDCOST(x->rdmult, |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12189 | (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0), |
| 12190 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12191 | RDCOST(x->rdmult, rate_skip1, |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12192 | (rd_stats_y.sse + rd_stats_uv.sse))) { |
| 12193 | rd_stats_y.rate = rate_skip1; |
| 12194 | rd_stats_uv.rate = 0; |
| 12195 | rd_stats_y.dist = rd_stats_y.sse; |
| 12196 | rd_stats_uv.dist = rd_stats_uv.sse; |
| 12197 | skip_blk = 1; |
| 12198 | } else { |
| 12199 | rd_stats_y.rate += rate_skip0; |
| 12200 | skip_blk = 0; |
| 12201 | } |
| 12202 | backup_skip = skip_blk; |
| 12203 | backup_mbmi = *mbmi; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12204 | 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] | 12205 | (rd_stats_y.dist + rd_stats_uv.dist)); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12206 | rd_causal += |
| 12207 | 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] | 12208 | |
| 12209 | // Check non-causal mode |
| 12210 | mbmi->motion_mode = OBMC_CAUSAL; |
| 12211 | av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col); |
| 12212 | |
| 12213 | av1_subtract_plane(x, bsize, 0); |
Wei-Ting Lin | ba5f187 | 2017-07-06 12:26:09 -0700 | [diff] [blame] | 12214 | #if CONFIG_VAR_TX |
| 12215 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
| 12216 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12217 | } else { |
| 12218 | int idx, idy; |
| 12219 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12220 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 12221 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 12222 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 12223 | memset(x->blk_skip[0], rd_stats_y.skip, |
| 12224 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 12225 | } |
| 12226 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 12227 | #else |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12228 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12229 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
Wei-Ting Lin | ba5f187 | 2017-07-06 12:26:09 -0700 | [diff] [blame] | 12230 | #endif |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12231 | assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX); |
| 12232 | if (rd_stats_y.skip && rd_stats_uv.skip) { |
| 12233 | rd_stats_y.rate = rate_skip1; |
| 12234 | rd_stats_uv.rate = 0; |
| 12235 | rd_stats_y.dist = rd_stats_y.sse; |
| 12236 | rd_stats_uv.dist = rd_stats_uv.sse; |
| 12237 | skip_blk = 0; |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12238 | } else if (RDCOST(x->rdmult, |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12239 | (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0), |
| 12240 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 12241 | RDCOST(x->rdmult, rate_skip1, |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12242 | (rd_stats_y.sse + rd_stats_uv.sse))) { |
| 12243 | rd_stats_y.rate = rate_skip1; |
| 12244 | rd_stats_uv.rate = 0; |
| 12245 | rd_stats_y.dist = rd_stats_y.sse; |
| 12246 | rd_stats_uv.dist = rd_stats_uv.sse; |
| 12247 | skip_blk = 1; |
| 12248 | } else { |
| 12249 | rd_stats_y.rate += rate_skip0; |
| 12250 | skip_blk = 0; |
| 12251 | } |
| 12252 | |
| 12253 | if (rd_causal > |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 12254 | RDCOST(x->rdmult, |
| 12255 | rd_stats_y.rate + rd_stats_uv.rate + |
| 12256 | av1_cost_bit(cm->fc->motion_mode_prob[bsize][0], 1), |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 12257 | (rd_stats_y.dist + rd_stats_uv.dist))) { |
| 12258 | x->skip = skip_blk; |
| 12259 | } else { |
| 12260 | *mbmi = backup_mbmi; |
| 12261 | x->skip = backup_skip; |
| 12262 | } |
| 12263 | } |
Fergus Simpson | 4063a68 | 2017-02-28 16:52:22 -0800 | [diff] [blame] | 12264 | #endif // CONFIG_NCOBMC |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12265 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12266 | int64_t get_prediction_rd_cost(const struct AV1_COMP *cpi, struct macroblock *x, |
| 12267 | int mi_row, int mi_col, int *skip_blk, |
| 12268 | MB_MODE_INFO *backup_mbmi) { |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12269 | const AV1_COMMON *const cm = &cpi->common; |
| 12270 | MACROBLOCKD *const xd = &x->e_mbd; |
| 12271 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 12272 | BLOCK_SIZE bsize = mbmi->sb_type; |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12273 | #if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
| 12274 | const MOTION_MODE motion_allowed = motion_mode_allowed( |
| 12275 | #if CONFIG_GLOBAL_MOTION |
| 12276 | 0, xd->global_motion, |
| 12277 | #endif // CONFIG_GLOBAL_MOTION |
| 12278 | #if CONFIG_WARPED_MOTION |
| 12279 | xd, |
| 12280 | #endif |
| 12281 | xd->mi[0]); |
| 12282 | #endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12283 | RD_STATS rd_stats_y, rd_stats_uv; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12284 | int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0); |
| 12285 | 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] | 12286 | int64_t this_rd; |
| 12287 | int ref; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12288 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12289 | #if CONFIG_CB4X4 |
| 12290 | x->skip_chroma_rd = |
| 12291 | !is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x, |
| 12292 | xd->plane[1].subsampling_y); |
| 12293 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12294 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12295 | set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]); |
| 12296 | for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) { |
| 12297 | YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mbmi->ref_frame[ref]); |
| 12298 | assert(cfg != NULL); |
| 12299 | av1_setup_pre_planes(xd, ref, cfg, mi_row, mi_col, |
| 12300 | &xd->block_refs[ref]->sf); |
| 12301 | } |
| 12302 | av1_setup_dst_planes(x->e_mbd.plane, bsize, |
| 12303 | get_frame_new_buffer(&cpi->common), mi_row, mi_col); |
| 12304 | |
| 12305 | #if CONFIG_NCOBMC_ADAPT_WEIGHT |
| 12306 | if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT) |
| 12307 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12308 | 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] | 12309 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12310 | #if CONFIG_MOTION_VAR |
| 12311 | if (mbmi->motion_mode == OBMC_CAUSAL) { |
| 12312 | #if CONFIG_NCOBMC |
| 12313 | 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] | 12314 | #else |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12315 | 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] | 12316 | #endif |
| 12317 | } |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12318 | #endif // CONFIG_MOTION_VAR |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12319 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12320 | #if CONFIG_NCOBMC_ADAPT_WEIGHT |
| 12321 | if (mbmi->motion_mode == NCOBMC_ADAPT_WEIGHT) |
| 12322 | for (int plane = 0; plane < MAX_MB_PLANE; ++plane) |
| 12323 | 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] | 12324 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12325 | av1_subtract_plane(x, bsize, 0); |
| 12326 | |
| 12327 | #if CONFIG_VAR_TX |
| 12328 | if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) { |
| 12329 | select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12330 | } else { |
| 12331 | int idx, idy; |
| 12332 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12333 | for (idy = 0; idy < xd->n8_h; ++idy) |
| 12334 | for (idx = 0; idx < xd->n8_w; ++idx) |
| 12335 | mbmi->inter_tx_size[idy][idx] = mbmi->tx_size; |
| 12336 | memset(x->blk_skip[0], rd_stats_y.skip, |
| 12337 | sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4); |
| 12338 | } |
| 12339 | inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 12340 | #else |
| 12341 | super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX); |
| 12342 | super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX); |
| 12343 | #endif |
| 12344 | assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX); |
| 12345 | |
| 12346 | if (rd_stats_y.skip && rd_stats_uv.skip) { |
| 12347 | rd_stats_y.rate = rate_skip1; |
| 12348 | rd_stats_uv.rate = 0; |
| 12349 | rd_stats_y.dist = rd_stats_y.sse; |
| 12350 | rd_stats_uv.dist = rd_stats_uv.sse; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12351 | *skip_blk = 1; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12352 | } else if (RDCOST(x->rdmult, |
| 12353 | (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0), |
| 12354 | (rd_stats_y.dist + rd_stats_uv.dist)) > |
| 12355 | RDCOST(x->rdmult, rate_skip1, |
| 12356 | (rd_stats_y.sse + rd_stats_uv.sse))) { |
| 12357 | rd_stats_y.rate = rate_skip1; |
| 12358 | rd_stats_uv.rate = 0; |
| 12359 | rd_stats_y.dist = rd_stats_y.sse; |
| 12360 | rd_stats_uv.dist = rd_stats_uv.sse; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12361 | *skip_blk = 1; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12362 | } else { |
| 12363 | rd_stats_y.rate += rate_skip0; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12364 | *skip_blk = 0; |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12365 | } |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12366 | |
| 12367 | if (backup_mbmi) *backup_mbmi = *mbmi; |
| 12368 | |
| 12369 | this_rd = RDCOST(x->rdmult, (rd_stats_y.rate + rd_stats_uv.rate), |
| 12370 | (rd_stats_y.dist + rd_stats_uv.dist)); |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12371 | #if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
| 12372 | if (motion_allowed == NCOBMC_ADAPT_WEIGHT) { |
| 12373 | assert(mbmi->motion_mode <= NCOBMC_ADAPT_WEIGHT); |
| 12374 | this_rd += |
| 12375 | RDCOST(x->rdmult, x->motion_mode_cost2[bsize][mbmi->motion_mode], 0); |
| 12376 | } else if (motion_allowed == OBMC_CAUSAL) { |
| 12377 | assert(mbmi->motion_mode <= OBMC_CAUSAL); |
| 12378 | this_rd += |
| 12379 | RDCOST(x->rdmult, x->motion_mode_cost1[bsize][mbmi->motion_mode], 0); |
| 12380 | } else { |
| 12381 | #endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
| 12382 | this_rd += |
| 12383 | RDCOST(x->rdmult, x->motion_mode_cost[bsize][mbmi->motion_mode], 0); |
| 12384 | #if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
| 12385 | } |
| 12386 | #endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12387 | return this_rd; |
| 12388 | } |
| 12389 | |
| 12390 | #if CONFIG_NCOBMC_ADAPT_WEIGHT |
| 12391 | void av1_check_ncobmc_adapt_weight_rd(const struct AV1_COMP *cpi, |
| 12392 | struct macroblock *x, int mi_row, |
| 12393 | int mi_col) { |
| 12394 | MACROBLOCKD *const xd = &x->e_mbd; |
| 12395 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 12396 | BLOCK_SIZE bsize = mbmi->sb_type; |
| 12397 | #if CONFIG_VAR_TX |
| 12398 | const int n4 = bsize_to_num_blk(bsize); |
| 12399 | uint8_t st_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8]; |
| 12400 | 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] | 12401 | 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] | 12402 | #endif |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12403 | MB_MODE_INFO st_mbmi, obmc_mbmi, ncobmc_mbmi; |
| 12404 | int st_skip, obmc_skip, ncobmc_skip; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12405 | int64_t st_rd, obmc_rd, ncobmc_rd; |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12406 | #if CONFIG_WARPED_MOTION |
| 12407 | const AV1_COMMON *const cm = &cpi->common; |
| 12408 | const int is_warp_motion = mbmi->motion_mode == WARPED_CAUSAL; |
| 12409 | const int rs = RDCOST(x->rdmult, av1_get_switchable_rate(cm, x, xd), 0); |
| 12410 | MB_MODE_INFO warp_mbmi; |
| 12411 | int64_t warp_rd; |
| 12412 | int warp_skip; |
| 12413 | #endif |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12414 | |
| 12415 | // Recompute the rd for the motion mode decided in rd loop |
| 12416 | mbmi->motion_mode = SIMPLE_TRANSLATION; |
| 12417 | 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] | 12418 | #if CONFIG_WARPED_MOTION |
| 12419 | st_rd += rs; |
| 12420 | #endif |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12421 | #if CONFIG_VAR_TX |
| 12422 | memcpy(st_blk_skip, x->blk_skip[0], sizeof(st_blk_skip[0]) * n4); |
| 12423 | #endif |
| 12424 | |
| 12425 | mbmi->motion_mode = OBMC_CAUSAL; |
| 12426 | obmc_rd = |
| 12427 | 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] | 12428 | #if CONFIG_WARPED_MOTION |
| 12429 | obmc_rd += rs; |
| 12430 | #endif |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12431 | #if CONFIG_VAR_TX |
| 12432 | memcpy(obmc_blk_skip, x->blk_skip[0], sizeof(obmc_blk_skip[0]) * n4); |
| 12433 | #endif |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12434 | |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12435 | // Compute the rd cost for ncobmc adaptive weight |
| 12436 | mbmi->motion_mode = NCOBMC_ADAPT_WEIGHT; |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12437 | ncobmc_rd = get_prediction_rd_cost(cpi, x, mi_row, mi_col, &ncobmc_skip, |
| 12438 | &ncobmc_mbmi); |
| 12439 | #if CONFIG_WARPED_MOTION |
| 12440 | ncobmc_rd += rs; |
| 12441 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12442 | // Calculate the ncobmc mode costs |
| 12443 | { |
| 12444 | ADAPT_OVERLAP_BLOCK aob = adapt_overlap_block_lookup[bsize]; |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12445 | ncobmc_rd += |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12446 | RDCOST(x->rdmult, x->ncobmc_mode_cost[aob][mbmi->ncobmc_mode[0]], 0); |
| 12447 | if (mi_size_wide[bsize] != mi_size_high[bsize]) |
Wei-Ting Lin | 3122b7d | 2017-08-30 17:26:58 -0700 | [diff] [blame] | 12448 | ncobmc_rd += |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12449 | RDCOST(x->rdmult, x->ncobmc_mode_cost[aob][mbmi->ncobmc_mode[1]], 0); |
| 12450 | } |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12451 | #if CONFIG_VAR_TX |
| 12452 | memcpy(ncobmc_blk_skip, x->blk_skip[0], sizeof(ncobmc_blk_skip[0]) * n4); |
| 12453 | #endif |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12454 | |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12455 | #if CONFIG_WARPED_MOTION |
| 12456 | if (is_warp_motion) { |
| 12457 | mbmi->motion_mode = WARPED_CAUSAL; |
| 12458 | warp_rd = |
| 12459 | 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] | 12460 | } else { |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12461 | warp_rd = INT64_MAX; |
| 12462 | } |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12463 | #endif |
Wei-Ting Lin | 5f8f7a1 | 2017-08-31 14:39:52 -0700 | [diff] [blame] | 12464 | |
| 12465 | #if CONFIG_WARPED_MOTION |
| 12466 | if (AOMMIN(ncobmc_rd, warp_rd) < AOMMIN(st_rd, obmc_rd)) { |
| 12467 | if (ncobmc_rd < warp_rd) { |
| 12468 | x->skip = ncobmc_skip; |
| 12469 | *mbmi = ncobmc_mbmi; |
| 12470 | #if CONFIG_VAR_TX |
| 12471 | memcpy(x->blk_skip[0], ncobmc_blk_skip, sizeof(ncobmc_blk_skip[0]) * n4); |
| 12472 | #endif |
| 12473 | } else { |
| 12474 | x->skip = warp_skip; |
| 12475 | *mbmi = warp_mbmi; |
| 12476 | } |
| 12477 | #else |
| 12478 | if (ncobmc_rd < AOMMIN(st_rd, obmc_rd)) { |
| 12479 | x->skip = ncobmc_skip; |
| 12480 | *mbmi = ncobmc_mbmi; |
| 12481 | #if CONFIG_VAR_TX |
| 12482 | memcpy(x->blk_skip[0], ncobmc_blk_skip, sizeof(ncobmc_blk_skip[0]) * n4); |
| 12483 | #endif |
| 12484 | #endif // CONFIG_WARPED_MOTION |
| 12485 | } else { |
| 12486 | if (obmc_rd < st_rd) { |
| 12487 | *mbmi = obmc_mbmi; |
| 12488 | x->skip = obmc_skip; |
| 12489 | #if CONFIG_VAR_TX |
| 12490 | memcpy(x->blk_skip[0], obmc_blk_skip, sizeof(obmc_blk_skip[0]) * n4); |
| 12491 | #endif |
| 12492 | } else { |
| 12493 | *mbmi = st_mbmi; |
| 12494 | x->skip = st_skip; |
| 12495 | #if CONFIG_VAR_TX |
| 12496 | memcpy(x->blk_skip[0], st_blk_skip, sizeof(st_blk_skip[0]) * n4); |
| 12497 | #endif |
| 12498 | } |
Wei-Ting Lin | c5563a4 | 2017-08-03 12:45:43 -0700 | [diff] [blame] | 12499 | } |
| 12500 | } |
| 12501 | |
| 12502 | int64_t get_ncobmc_error(MACROBLOCKD *xd, int pxl_row, int pxl_col, |
| 12503 | BLOCK_SIZE bsize, int plane, struct buf_2d *src) { |
| 12504 | const int wide = AOMMIN(mi_size_wide[bsize] * MI_SIZE, |
| 12505 | (xd->sb_mi_bd.mi_col_end + 1) * MI_SIZE - pxl_col); |
| 12506 | const int high = AOMMIN(mi_size_high[bsize] * MI_SIZE, |
| 12507 | (xd->sb_mi_bd.mi_row_end + 1) * MI_SIZE - pxl_row); |
| 12508 | const int ss_x = xd->plane[plane].subsampling_x; |
| 12509 | const int ss_y = xd->plane[plane].subsampling_y; |
| 12510 | int row_offset = (pxl_row - xd->sb_mi_bd.mi_row_begin * MI_SIZE) >> ss_y; |
| 12511 | int col_offset = (pxl_col - xd->sb_mi_bd.mi_col_begin * MI_SIZE) >> ss_x; |
| 12512 | int dst_stride = xd->ncobmc_pred_buf_stride[plane]; |
| 12513 | int dst_offset = row_offset * dst_stride + col_offset; |
| 12514 | int src_stride = src->stride; |
| 12515 | |
| 12516 | int r, c; |
| 12517 | int64_t tmp, error = 0; |
| 12518 | |
| 12519 | for (r = 0; r < (high >> ss_y); ++r) { |
| 12520 | for (c = 0; c < (wide >> ss_x); ++c) { |
| 12521 | tmp = xd->ncobmc_pred_buf[plane][r * dst_stride + c + dst_offset] - |
| 12522 | src->buf[r * src_stride + c]; |
| 12523 | error += tmp * tmp; |
| 12524 | } |
| 12525 | } |
| 12526 | return error; |
| 12527 | } |
| 12528 | |
| 12529 | int get_ncobmc_mode(const AV1_COMP *const cpi, MACROBLOCK *const x, |
| 12530 | MACROBLOCKD *xd, int mi_row, int mi_col, int bsize) { |
| 12531 | const AV1_COMMON *const cm = &cpi->common; |
| 12532 | #if CONFIG_HIGHBITDEPTH |
| 12533 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_0[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 12534 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 12535 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_2[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 12536 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_3[2 * MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 12537 | #else |
| 12538 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_0[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 12539 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_1[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 12540 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_2[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 12541 | DECLARE_ALIGNED(16, uint8_t, tmp_buf_3[MAX_MB_PLANE * MAX_SB_SQUARE]); |
| 12542 | #endif |
| 12543 | uint8_t *pred_buf[4][MAX_MB_PLANE]; |
| 12544 | |
| 12545 | // TODO(weitinglin): stride size needs to be fixed for high-bit depth |
| 12546 | int pred_stride[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE }; |
| 12547 | |
| 12548 | // target block in pxl |
| 12549 | int pxl_row = mi_row << MI_SIZE_LOG2; |
| 12550 | int pxl_col = mi_col << MI_SIZE_LOG2; |
| 12551 | int64_t error, best_error = INT64_MAX; |
| 12552 | int plane, tmp_mode, best_mode = 0; |
| 12553 | #if CONFIG_HIGHBITDEPTH |
| 12554 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 12555 | int len = sizeof(uint16_t); |
| 12556 | ASSIGN_ALIGNED_PTRS_HBD(pred_buf[0], tmp_buf_0, MAX_SB_SQUARE, len); |
| 12557 | ASSIGN_ALIGNED_PTRS_HBD(pred_buf[1], tmp_buf_0, MAX_SB_SQUARE, len); |
| 12558 | ASSIGN_ALIGNED_PTRS_HBD(pred_buf[2], tmp_buf_0, MAX_SB_SQUARE, len); |
| 12559 | ASSIGN_ALIGNED_PTRS_HBD(pred_buf[3], tmp_buf_0, MAX_SB_SQUARE, len); |
| 12560 | } else { |
| 12561 | #endif // CONFIG_HIGHBITDEPTH |
| 12562 | ASSIGN_ALIGNED_PTRS(pred_buf[0], tmp_buf_0, MAX_SB_SQUARE); |
| 12563 | ASSIGN_ALIGNED_PTRS(pred_buf[1], tmp_buf_1, MAX_SB_SQUARE); |
| 12564 | ASSIGN_ALIGNED_PTRS(pred_buf[2], tmp_buf_2, MAX_SB_SQUARE); |
| 12565 | ASSIGN_ALIGNED_PTRS(pred_buf[3], tmp_buf_3, MAX_SB_SQUARE); |
| 12566 | #if CONFIG_HIGHBITDEPTH |
| 12567 | } |
| 12568 | #endif |
| 12569 | |
| 12570 | av1_get_ext_blk_preds(cm, xd, bsize, mi_row, mi_col, pred_buf, pred_stride); |
| 12571 | av1_get_ori_blk_pred(cm, xd, bsize, mi_row, mi_col, pred_buf[3], pred_stride); |
| 12572 | |
| 12573 | for (tmp_mode = 0; tmp_mode < MAX_NCOBMC_MODES; ++tmp_mode) { |
| 12574 | error = 0; |
| 12575 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 12576 | build_ncobmc_intrpl_pred(cm, xd, plane, pxl_row, pxl_col, bsize, pred_buf, |
| 12577 | pred_stride, tmp_mode); |
| 12578 | error += get_ncobmc_error(xd, pxl_row, pxl_col, bsize, plane, |
| 12579 | &x->plane[plane].src); |
| 12580 | } |
| 12581 | if (error < best_error) { |
| 12582 | best_mode = tmp_mode; |
| 12583 | best_error = error; |
| 12584 | } |
| 12585 | } |
| 12586 | |
| 12587 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 12588 | build_ncobmc_intrpl_pred(cm, xd, plane, pxl_row, pxl_col, bsize, pred_buf, |
| 12589 | pred_stride, best_mode); |
| 12590 | } |
| 12591 | |
| 12592 | return best_mode; |
| 12593 | } |
| 12594 | |
| 12595 | #endif // CONFIG_NCOBMC_ADAPT_WEIGHT |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 12596 | #endif // CONFIG_MOTION_VAR |