blob: b1c0fb30a0a4bfdc6c7955758bdf0839cdd951b1 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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 Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <math.h>
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_dsp_rtcd.h"
Jingning Han1aab8182016-06-03 11:09:06 -070016#include "./av1_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
Yaowu Xuf883b422016-08-30 14:01:10 -070018#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019#include "aom_dsp/blend.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070020#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "aom_ports/mem.h"
22#include "aom_ports/system_state.h"
23
David Michael Barr5b2021e2017-08-17 18:12:39 +090024#if CONFIG_CFL
25#include "av1/common/cfl.h"
26#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#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 Swarbrickc0cea7f2017-08-22 14:06:56 +010033#include "av1/common/obmc.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070034#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 Chiang47e4b362017-03-24 11:25:10 -070040#if CONFIG_LV_MAP
41#include "av1/common/txb_common.h"
42#endif
Yue Chen69f18e12016-09-08 14:48:15 -070043#if CONFIG_WARPED_MOTION
44#include "av1/common/warped_motion.h"
45#endif // CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070046
Jingning Han1aab8182016-06-03 11:09:06 -070047#include "av1/encoder/aq_variance.h"
Tom Finegan17ce8b12017-02-08 12:46:31 -080048#include "av1/encoder/av1_quantize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070049#include "av1/encoder/cost.h"
50#include "av1/encoder/encodemb.h"
51#include "av1/encoder/encodemv.h"
52#include "av1/encoder/encoder.h"
Angie Chiang47e4b362017-03-24 11:25:10 -070053#if CONFIG_LV_MAP
54#include "av1/encoder/encodetxb.h"
55#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070056#include "av1/encoder/hybrid_fwd_txfm.h"
57#include "av1/encoder/mcomp.h"
58#include "av1/encoder/palette.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070059#include "av1/encoder/ratectrl.h"
60#include "av1/encoder/rd.h"
61#include "av1/encoder/rdopt.h"
Debargha Mukherjeeceebb702016-10-11 05:26:50 -070062#include "av1/encoder/tokenize.h"
Yushin Cho77bba8d2016-11-04 16:36:56 -070063#if CONFIG_PVQ
64#include "av1/encoder/pvq_encoder.h"
Yushin Cho7a428ba2017-01-12 16:28:49 -080065#include "av1/common/pvq.h"
Yushin Cho55104332017-08-14 16:15:43 -070066#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#if CONFIG_DUAL_FILTER
Angie Chiang5678ad92016-11-21 09:38:40 -080068#define DUAL_FILTER_SET_SIZE (SWITCHABLE_FILTERS * SWITCHABLE_FILTERS)
Angie Chiangaadbb022017-06-01 16:08:03 -070069#if USE_EXTRA_FILTER
Angie Chiang5678ad92016-11-21 09:38:40 -080070static const int filter_sets[DUAL_FILTER_SET_SIZE][2] = {
Angie Chiangd91ab372016-11-21 18:16:49 -080071 { 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 Xuc27fc142016-08-22 16:08:15 -070074};
Angie Chiangaadbb022017-06-01 16:08:03 -070075#else // USE_EXTRA_FILTER
76static 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 Chiang5678ad92016-11-21 09:38:40 -080081#endif // CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070082
83#if CONFIG_EXT_REFS
84
Zoe Liue9b15e22017-07-19 15:53:01 -070085#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 Liue9b15e22017-07-19 15:53:01 -0700114#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700115
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 Liuc082bbc2017-05-17 13:31:37 -0700126#if CONFIG_EXT_COMP_REFS
Zoe Liuac889702017-08-23 14:22:58 -0700127#define SECOND_REF_FRAME_MASK \
128 ((1 << ALTREF_FRAME) | (1 << ALTREF2_FRAME) | (1 << BWDREF_FRAME) | \
129 (1 << GOLDEN_FRAME) | (1 << LAST2_FRAME) | 0x01)
Zoe Liu3ac20932017-08-30 16:35:55 -0700130#else // !CONFIG_EXT_COMP_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -0700131#define SECOND_REF_FRAME_MASK \
132 ((1 << ALTREF_FRAME) | (1 << ALTREF2_FRAME) | (1 << BWDREF_FRAME) | 0x01)
Zoe Liuc082bbc2017-05-17 13:31:37 -0700133#endif // CONFIG_EXT_COMP_REFS
134#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700135#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 Xuc27fc142016-08-22 16:08:15 -0700142#define ANGLE_SKIP_THRESH 10
143#define FILTER_FAST_SEARCH 1
144#endif // CONFIG_EXT_INTRA
145
Sarah Parker345366a2017-06-15 12:13:01 -0700146// 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 Chiang91a1cf92017-08-20 10:50:31 -0700149#ifndef DISABLE_TRELLISQ_SEARCH
Yushin Choba40b522017-09-25 10:25:36 -0700150#define DISABLE_TRELLISQ_SEARCH 0
Angie Chiang91a1cf92017-08-20 10:50:31 -0700151#endif
Sarah Parker345366a2017-06-15 12:13:01 -0700152
James Zern67932792017-08-21 11:13:19 -0700153static 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 Xuc27fc142016-08-22 16:08:15 -0700159
160typedef struct {
161 PREDICTION_MODE mode;
162 MV_REFERENCE_FRAME ref_frame[2];
163} MODE_DEFINITION;
164
165typedef struct { MV_REFERENCE_FRAME ref_frame[2]; } REF_DEFINITION;
166
167struct rdcost_block_args {
Yaowu Xuf883b422016-08-30 14:01:10 -0700168 const AV1_COMP *cpi;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169 MACROBLOCK *x;
170 ENTROPY_CONTEXT t_above[2 * MAX_MIB_SIZE];
171 ENTROPY_CONTEXT t_left[2 * MAX_MIB_SIZE];
Angie Chiang7c2b7f22016-11-07 16:00:00 -0800172 RD_STATS rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173 int64_t this_rd;
174 int64_t best_rd;
175 int exit_early;
176 int use_fast_coef_costing;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177};
178
179#define LAST_NEW_MV_INDEX 6
Yaowu Xuf883b422016-08-30 14:01:10 -0700180static const MODE_DEFINITION av1_mode_order[MAX_MODES] = {
Emil Keyder01770b32017-01-20 18:03:11 -0500181 { NEARESTMV, { LAST_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182#if CONFIG_EXT_REFS
Emil Keyder01770b32017-01-20 18:03:11 -0500183 { NEARESTMV, { LAST2_FRAME, NONE_FRAME } },
184 { NEARESTMV, { LAST3_FRAME, NONE_FRAME } },
185 { NEARESTMV, { BWDREF_FRAME, NONE_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700186 { NEARESTMV, { ALTREF2_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187#endif // CONFIG_EXT_REFS
Emil Keyder01770b32017-01-20 18:03:11 -0500188 { NEARESTMV, { ALTREF_FRAME, NONE_FRAME } },
189 { NEARESTMV, { GOLDEN_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190
Emil Keyder01770b32017-01-20 18:03:11 -0500191 { DC_PRED, { INTRA_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192
Emil Keyder01770b32017-01-20 18:03:11 -0500193 { NEWMV, { LAST_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700194#if CONFIG_EXT_REFS
Emil Keyder01770b32017-01-20 18:03:11 -0500195 { NEWMV, { LAST2_FRAME, NONE_FRAME } },
196 { NEWMV, { LAST3_FRAME, NONE_FRAME } },
197 { NEWMV, { BWDREF_FRAME, NONE_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700198 { NEWMV, { ALTREF2_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199#endif // CONFIG_EXT_REFS
Emil Keyder01770b32017-01-20 18:03:11 -0500200 { NEWMV, { ALTREF_FRAME, NONE_FRAME } },
201 { NEWMV, { GOLDEN_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202
Emil Keyder01770b32017-01-20 18:03:11 -0500203 { NEARMV, { LAST_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204#if CONFIG_EXT_REFS
Emil Keyder01770b32017-01-20 18:03:11 -0500205 { NEARMV, { LAST2_FRAME, NONE_FRAME } },
206 { NEARMV, { LAST3_FRAME, NONE_FRAME } },
207 { NEARMV, { BWDREF_FRAME, NONE_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700208 { NEARMV, { ALTREF2_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209#endif // CONFIG_EXT_REFS
Emil Keyder01770b32017-01-20 18:03:11 -0500210 { NEARMV, { ALTREF_FRAME, NONE_FRAME } },
211 { NEARMV, { GOLDEN_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700212
Emil Keyder01770b32017-01-20 18:03:11 -0500213 { ZEROMV, { LAST_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700214#if CONFIG_EXT_REFS
Emil Keyder01770b32017-01-20 18:03:11 -0500215 { ZEROMV, { LAST2_FRAME, NONE_FRAME } },
216 { ZEROMV, { LAST3_FRAME, NONE_FRAME } },
217 { ZEROMV, { BWDREF_FRAME, NONE_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700218 { ZEROMV, { ALTREF2_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219#endif // CONFIG_EXT_REFS
Emil Keyder01770b32017-01-20 18:03:11 -0500220 { ZEROMV, { GOLDEN_FRAME, NONE_FRAME } },
221 { ZEROMV, { ALTREF_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222
223// TODO(zoeliu): May need to reconsider the order on the modes to check
224
225#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -0700226
227#if CONFIG_COMPOUND_SINGLEREF
228 // Single ref comp mode
229 { SR_NEAREST_NEARMV, { LAST_FRAME, NONE_FRAME } },
230#if CONFIG_EXT_REFS
231 { SR_NEAREST_NEARMV, { LAST2_FRAME, NONE_FRAME } },
232 { SR_NEAREST_NEARMV, { LAST3_FRAME, NONE_FRAME } },
233 { SR_NEAREST_NEARMV, { BWDREF_FRAME, NONE_FRAME } },
234#endif // CONFIG_EXT_REFS
235 { SR_NEAREST_NEARMV, { GOLDEN_FRAME, NONE_FRAME } },
236 { SR_NEAREST_NEARMV, { ALTREF_FRAME, NONE_FRAME } },
237
238 /*
239 { SR_NEAREST_NEWMV, { LAST_FRAME, NONE_FRAME } },
240#if CONFIG_EXT_REFS
241 { SR_NEAREST_NEWMV, { LAST2_FRAME, NONE_FRAME } },
242 { SR_NEAREST_NEWMV, { LAST3_FRAME, NONE_FRAME } },
243 { SR_NEAREST_NEWMV, { BWDREF_FRAME, NONE_FRAME } },
244#endif // CONFIG_EXT_REFS
245 { SR_NEAREST_NEWMV, { GOLDEN_FRAME, NONE_FRAME } },
246 { SR_NEAREST_NEWMV, { ALTREF_FRAME, NONE_FRAME } },*/
247
248 { SR_NEAR_NEWMV, { LAST_FRAME, NONE_FRAME } },
249#if CONFIG_EXT_REFS
250 { SR_NEAR_NEWMV, { LAST2_FRAME, NONE_FRAME } },
251 { SR_NEAR_NEWMV, { LAST3_FRAME, NONE_FRAME } },
252 { SR_NEAR_NEWMV, { BWDREF_FRAME, NONE_FRAME } },
253#endif // CONFIG_EXT_REFS
254 { SR_NEAR_NEWMV, { GOLDEN_FRAME, NONE_FRAME } },
255 { SR_NEAR_NEWMV, { ALTREF_FRAME, NONE_FRAME } },
256
257 { SR_ZERO_NEWMV, { LAST_FRAME, NONE_FRAME } },
258#if CONFIG_EXT_REFS
259 { SR_ZERO_NEWMV, { LAST2_FRAME, NONE_FRAME } },
260 { SR_ZERO_NEWMV, { LAST3_FRAME, NONE_FRAME } },
261 { SR_ZERO_NEWMV, { BWDREF_FRAME, NONE_FRAME } },
262#endif // CONFIG_EXT_REFS
263 { SR_ZERO_NEWMV, { GOLDEN_FRAME, NONE_FRAME } },
264 { SR_ZERO_NEWMV, { ALTREF_FRAME, NONE_FRAME } },
265
266 { SR_NEW_NEWMV, { LAST_FRAME, NONE_FRAME } },
267#if CONFIG_EXT_REFS
268 { SR_NEW_NEWMV, { LAST2_FRAME, NONE_FRAME } },
269 { SR_NEW_NEWMV, { LAST3_FRAME, NONE_FRAME } },
270 { SR_NEW_NEWMV, { BWDREF_FRAME, NONE_FRAME } },
271#endif // CONFIG_EXT_REFS
272 { SR_NEW_NEWMV, { GOLDEN_FRAME, NONE_FRAME } },
273 { SR_NEW_NEWMV, { ALTREF_FRAME, NONE_FRAME } },
274#endif // CONFIG_COMPOUND_SINGLEREF
275
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276 { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } },
277#if CONFIG_EXT_REFS
278 { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } },
279 { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } },
280#endif // CONFIG_EXT_REFS
281 { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } },
282#if CONFIG_EXT_REFS
283 { NEAREST_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } },
284 { NEAREST_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } },
285 { NEAREST_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } },
286 { NEAREST_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700287 { NEAREST_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } },
288 { NEAREST_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } },
289 { NEAREST_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } },
290 { NEAREST_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700291
292#if CONFIG_EXT_COMP_REFS
293 { NEAREST_NEARESTMV, { LAST_FRAME, LAST2_FRAME } },
Zoe Liufcf5fa22017-06-26 16:00:38 -0700294 { NEAREST_NEARESTMV, { LAST_FRAME, LAST3_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700295 { NEAREST_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } },
296 { NEAREST_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } },
297#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298#endif // CONFIG_EXT_REFS
299
300#else // CONFIG_EXT_INTER
301
302 { NEARESTMV, { LAST_FRAME, ALTREF_FRAME } },
303#if CONFIG_EXT_REFS
304 { NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } },
305 { NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } },
306#endif // CONFIG_EXT_REFS
307 { NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } },
308#if CONFIG_EXT_REFS
309 { NEARESTMV, { LAST_FRAME, BWDREF_FRAME } },
310 { NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } },
311 { NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } },
312 { NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700313 { NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } },
314 { NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } },
315 { NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } },
316 { NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700317
318#if CONFIG_EXT_COMP_REFS
319 { NEARESTMV, { LAST_FRAME, LAST2_FRAME } },
Zoe Liufcf5fa22017-06-26 16:00:38 -0700320 { NEARESTMV, { LAST_FRAME, LAST3_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700321 { NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } },
322 { NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } },
323#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324#endif // CONFIG_EXT_REFS
325#endif // CONFIG_EXT_INTER
326
Emil Keyder01770b32017-01-20 18:03:11 -0500327 { TM_PRED, { INTRA_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328
Emil Keyder01770b32017-01-20 18:03:11 -0500329 { SMOOTH_PRED, { INTRA_FRAME, NONE_FRAME } },
Urvang Joshie6ca8e82017-03-15 14:57:41 -0700330#if CONFIG_SMOOTH_HV
331 { SMOOTH_V_PRED, { INTRA_FRAME, NONE_FRAME } },
332 { SMOOTH_H_PRED, { INTRA_FRAME, NONE_FRAME } },
333#endif // CONFIG_SMOOTH_HV
Urvang Joshi6be4a542016-11-03 15:24:05 -0700334
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -0700336 { NEAR_NEARMV, { LAST_FRAME, ALTREF_FRAME } },
337 { NEW_NEARESTMV, { LAST_FRAME, ALTREF_FRAME } },
338 { NEAREST_NEWMV, { LAST_FRAME, ALTREF_FRAME } },
339 { NEW_NEARMV, { LAST_FRAME, ALTREF_FRAME } },
340 { NEAR_NEWMV, { LAST_FRAME, ALTREF_FRAME } },
341 { NEW_NEWMV, { LAST_FRAME, ALTREF_FRAME } },
342 { ZERO_ZEROMV, { LAST_FRAME, ALTREF_FRAME } },
343
344#if CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345 { NEAR_NEARMV, { LAST2_FRAME, ALTREF_FRAME } },
346 { NEW_NEARESTMV, { LAST2_FRAME, ALTREF_FRAME } },
347 { NEAREST_NEWMV, { LAST2_FRAME, ALTREF_FRAME } },
348 { NEW_NEARMV, { LAST2_FRAME, ALTREF_FRAME } },
349 { NEAR_NEWMV, { LAST2_FRAME, ALTREF_FRAME } },
350 { NEW_NEWMV, { LAST2_FRAME, ALTREF_FRAME } },
351 { ZERO_ZEROMV, { LAST2_FRAME, ALTREF_FRAME } },
352
Yaowu Xuc27fc142016-08-22 16:08:15 -0700353 { NEAR_NEARMV, { LAST3_FRAME, ALTREF_FRAME } },
354 { NEW_NEARESTMV, { LAST3_FRAME, ALTREF_FRAME } },
355 { NEAREST_NEWMV, { LAST3_FRAME, ALTREF_FRAME } },
356 { NEW_NEARMV, { LAST3_FRAME, ALTREF_FRAME } },
357 { NEAR_NEWMV, { LAST3_FRAME, ALTREF_FRAME } },
358 { NEW_NEWMV, { LAST3_FRAME, ALTREF_FRAME } },
359 { ZERO_ZEROMV, { LAST3_FRAME, ALTREF_FRAME } },
360#endif // CONFIG_EXT_REFS
361
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } },
363 { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF_FRAME } },
364 { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
365 { NEW_NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } },
366 { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
367 { NEW_NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
368 { ZERO_ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } },
369
370#if CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700371 { NEAR_NEARMV, { LAST_FRAME, BWDREF_FRAME } },
372 { NEW_NEARESTMV, { LAST_FRAME, BWDREF_FRAME } },
373 { NEAREST_NEWMV, { LAST_FRAME, BWDREF_FRAME } },
374 { NEW_NEARMV, { LAST_FRAME, BWDREF_FRAME } },
375 { NEAR_NEWMV, { LAST_FRAME, BWDREF_FRAME } },
376 { NEW_NEWMV, { LAST_FRAME, BWDREF_FRAME } },
377 { ZERO_ZEROMV, { LAST_FRAME, BWDREF_FRAME } },
378
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379 { NEAR_NEARMV, { LAST2_FRAME, BWDREF_FRAME } },
380 { NEW_NEARESTMV, { LAST2_FRAME, BWDREF_FRAME } },
381 { NEAREST_NEWMV, { LAST2_FRAME, BWDREF_FRAME } },
382 { NEW_NEARMV, { LAST2_FRAME, BWDREF_FRAME } },
383 { NEAR_NEWMV, { LAST2_FRAME, BWDREF_FRAME } },
384 { NEW_NEWMV, { LAST2_FRAME, BWDREF_FRAME } },
385 { ZERO_ZEROMV, { LAST2_FRAME, BWDREF_FRAME } },
386
Yaowu Xuc27fc142016-08-22 16:08:15 -0700387 { NEAR_NEARMV, { LAST3_FRAME, BWDREF_FRAME } },
388 { NEW_NEARESTMV, { LAST3_FRAME, BWDREF_FRAME } },
389 { NEAREST_NEWMV, { LAST3_FRAME, BWDREF_FRAME } },
390 { NEW_NEARMV, { LAST3_FRAME, BWDREF_FRAME } },
391 { NEAR_NEWMV, { LAST3_FRAME, BWDREF_FRAME } },
392 { NEW_NEWMV, { LAST3_FRAME, BWDREF_FRAME } },
393 { ZERO_ZEROMV, { LAST3_FRAME, BWDREF_FRAME } },
394
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395 { NEAR_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } },
396 { NEW_NEARESTMV, { GOLDEN_FRAME, BWDREF_FRAME } },
397 { NEAREST_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } },
398 { NEW_NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } },
399 { NEAR_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } },
400 { NEW_NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } },
401 { ZERO_ZEROMV, { GOLDEN_FRAME, BWDREF_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700402
Zoe Liue9b15e22017-07-19 15:53:01 -0700403 { NEAR_NEARMV, { LAST_FRAME, ALTREF2_FRAME } },
404 { NEW_NEARESTMV, { LAST_FRAME, ALTREF2_FRAME } },
405 { NEAREST_NEWMV, { LAST_FRAME, ALTREF2_FRAME } },
406 { NEW_NEARMV, { LAST_FRAME, ALTREF2_FRAME } },
407 { NEAR_NEWMV, { LAST_FRAME, ALTREF2_FRAME } },
408 { NEW_NEWMV, { LAST_FRAME, ALTREF2_FRAME } },
409 { ZERO_ZEROMV, { LAST_FRAME, ALTREF2_FRAME } },
410
411 { NEAR_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } },
412 { NEW_NEARESTMV, { LAST2_FRAME, ALTREF2_FRAME } },
413 { NEAREST_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } },
414 { NEW_NEARMV, { LAST2_FRAME, ALTREF2_FRAME } },
415 { NEAR_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } },
416 { NEW_NEWMV, { LAST2_FRAME, ALTREF2_FRAME } },
417 { ZERO_ZEROMV, { LAST2_FRAME, ALTREF2_FRAME } },
418
419 { NEAR_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } },
420 { NEW_NEARESTMV, { LAST3_FRAME, ALTREF2_FRAME } },
421 { NEAREST_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } },
422 { NEW_NEARMV, { LAST3_FRAME, ALTREF2_FRAME } },
423 { NEAR_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } },
424 { NEW_NEWMV, { LAST3_FRAME, ALTREF2_FRAME } },
425 { ZERO_ZEROMV, { LAST3_FRAME, ALTREF2_FRAME } },
426
427 { NEAR_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
428 { NEW_NEARESTMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
429 { NEAREST_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
430 { NEW_NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
431 { NEAR_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
432 { NEW_NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
433 { ZERO_ZEROMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700434
Zoe Liuc082bbc2017-05-17 13:31:37 -0700435#if CONFIG_EXT_COMP_REFS
436 { NEAR_NEARMV, { LAST_FRAME, LAST2_FRAME } },
437 { NEW_NEARESTMV, { LAST_FRAME, LAST2_FRAME } },
438 { NEAREST_NEWMV, { LAST_FRAME, LAST2_FRAME } },
439 { NEW_NEARMV, { LAST_FRAME, LAST2_FRAME } },
440 { NEAR_NEWMV, { LAST_FRAME, LAST2_FRAME } },
441 { NEW_NEWMV, { LAST_FRAME, LAST2_FRAME } },
442 { ZERO_ZEROMV, { LAST_FRAME, LAST2_FRAME } },
443
Zoe Liufcf5fa22017-06-26 16:00:38 -0700444 { NEAR_NEARMV, { LAST_FRAME, LAST3_FRAME } },
445 { NEW_NEARESTMV, { LAST_FRAME, LAST3_FRAME } },
446 { NEAREST_NEWMV, { LAST_FRAME, LAST3_FRAME } },
447 { NEW_NEARMV, { LAST_FRAME, LAST3_FRAME } },
448 { NEAR_NEWMV, { LAST_FRAME, LAST3_FRAME } },
449 { NEW_NEWMV, { LAST_FRAME, LAST3_FRAME } },
450 { ZERO_ZEROMV, { LAST_FRAME, LAST3_FRAME } },
451
Zoe Liuc082bbc2017-05-17 13:31:37 -0700452 { NEAR_NEARMV, { LAST_FRAME, GOLDEN_FRAME } },
453 { NEW_NEARESTMV, { LAST_FRAME, GOLDEN_FRAME } },
454 { NEAREST_NEWMV, { LAST_FRAME, GOLDEN_FRAME } },
455 { NEW_NEARMV, { LAST_FRAME, GOLDEN_FRAME } },
456 { NEAR_NEWMV, { LAST_FRAME, GOLDEN_FRAME } },
457 { NEW_NEWMV, { LAST_FRAME, GOLDEN_FRAME } },
458 { ZERO_ZEROMV, { LAST_FRAME, GOLDEN_FRAME } },
459
460 { NEAR_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } },
461 { NEW_NEARESTMV, { BWDREF_FRAME, ALTREF_FRAME } },
462 { NEAREST_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } },
463 { NEW_NEARMV, { BWDREF_FRAME, ALTREF_FRAME } },
464 { NEAR_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } },
465 { NEW_NEWMV, { BWDREF_FRAME, ALTREF_FRAME } },
466 { ZERO_ZEROMV, { BWDREF_FRAME, ALTREF_FRAME } },
467#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700468#endif // CONFIG_EXT_REFS
469
Zoe Liuc082bbc2017-05-17 13:31:37 -0700470#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -0700471
472 { NEARMV, { LAST_FRAME, ALTREF_FRAME } },
473 { NEWMV, { LAST_FRAME, ALTREF_FRAME } },
474#if CONFIG_EXT_REFS
475 { NEARMV, { LAST2_FRAME, ALTREF_FRAME } },
476 { NEWMV, { LAST2_FRAME, ALTREF_FRAME } },
477 { NEARMV, { LAST3_FRAME, ALTREF_FRAME } },
478 { NEWMV, { LAST3_FRAME, ALTREF_FRAME } },
479#endif // CONFIG_EXT_REFS
480 { NEARMV, { GOLDEN_FRAME, ALTREF_FRAME } },
481 { NEWMV, { GOLDEN_FRAME, ALTREF_FRAME } },
482
483#if CONFIG_EXT_REFS
484 { NEARMV, { LAST_FRAME, BWDREF_FRAME } },
485 { NEWMV, { LAST_FRAME, BWDREF_FRAME } },
486 { NEARMV, { LAST2_FRAME, BWDREF_FRAME } },
487 { NEWMV, { LAST2_FRAME, BWDREF_FRAME } },
488 { NEARMV, { LAST3_FRAME, BWDREF_FRAME } },
489 { NEWMV, { LAST3_FRAME, BWDREF_FRAME } },
490 { NEARMV, { GOLDEN_FRAME, BWDREF_FRAME } },
491 { NEWMV, { GOLDEN_FRAME, BWDREF_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700492
Zoe Liue9b15e22017-07-19 15:53:01 -0700493 { NEARMV, { LAST_FRAME, ALTREF2_FRAME } },
494 { NEWMV, { LAST_FRAME, ALTREF2_FRAME } },
495 { NEARMV, { LAST2_FRAME, ALTREF2_FRAME } },
496 { NEWMV, { LAST2_FRAME, ALTREF2_FRAME } },
497 { NEARMV, { LAST3_FRAME, ALTREF2_FRAME } },
498 { NEWMV, { LAST3_FRAME, ALTREF2_FRAME } },
499 { NEARMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
500 { NEWMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700501
Zoe Liuc082bbc2017-05-17 13:31:37 -0700502#if CONFIG_EXT_COMP_REFS
503 { NEARMV, { LAST_FRAME, LAST2_FRAME } },
504 { NEWMV, { LAST_FRAME, LAST2_FRAME } },
Zoe Liufcf5fa22017-06-26 16:00:38 -0700505 { NEARMV, { LAST_FRAME, LAST3_FRAME } },
506 { NEWMV, { LAST_FRAME, LAST3_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700507 { NEARMV, { LAST_FRAME, GOLDEN_FRAME } },
508 { NEWMV, { LAST_FRAME, GOLDEN_FRAME } },
509 { NEARMV, { BWDREF_FRAME, ALTREF_FRAME } },
510 { NEWMV, { BWDREF_FRAME, ALTREF_FRAME } },
511#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512#endif // CONFIG_EXT_REFS
513
514 { ZEROMV, { LAST_FRAME, ALTREF_FRAME } },
515#if CONFIG_EXT_REFS
516 { ZEROMV, { LAST2_FRAME, ALTREF_FRAME } },
517 { ZEROMV, { LAST3_FRAME, ALTREF_FRAME } },
518#endif // CONFIG_EXT_REFS
519 { ZEROMV, { GOLDEN_FRAME, ALTREF_FRAME } },
520
521#if CONFIG_EXT_REFS
522 { ZEROMV, { LAST_FRAME, BWDREF_FRAME } },
523 { ZEROMV, { LAST2_FRAME, BWDREF_FRAME } },
524 { ZEROMV, { LAST3_FRAME, BWDREF_FRAME } },
525 { ZEROMV, { GOLDEN_FRAME, BWDREF_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700526
Zoe Liue9b15e22017-07-19 15:53:01 -0700527 { ZEROMV, { LAST_FRAME, ALTREF2_FRAME } },
528 { ZEROMV, { LAST2_FRAME, ALTREF2_FRAME } },
529 { ZEROMV, { LAST3_FRAME, ALTREF2_FRAME } },
530 { ZEROMV, { GOLDEN_FRAME, ALTREF2_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700531
Zoe Liuc082bbc2017-05-17 13:31:37 -0700532#if CONFIG_EXT_COMP_REFS
533 { ZEROMV, { LAST_FRAME, LAST2_FRAME } },
Zoe Liufcf5fa22017-06-26 16:00:38 -0700534 { ZEROMV, { LAST_FRAME, LAST3_FRAME } },
Zoe Liuc082bbc2017-05-17 13:31:37 -0700535 { ZEROMV, { LAST_FRAME, GOLDEN_FRAME } },
536 { ZEROMV, { BWDREF_FRAME, ALTREF_FRAME } },
537#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538#endif // CONFIG_EXT_REFS
539
540#endif // CONFIG_EXT_INTER
541
Emil Keyder01770b32017-01-20 18:03:11 -0500542 { H_PRED, { INTRA_FRAME, NONE_FRAME } },
543 { V_PRED, { INTRA_FRAME, NONE_FRAME } },
544 { D135_PRED, { INTRA_FRAME, NONE_FRAME } },
545 { D207_PRED, { INTRA_FRAME, NONE_FRAME } },
546 { D153_PRED, { INTRA_FRAME, NONE_FRAME } },
547 { D63_PRED, { INTRA_FRAME, NONE_FRAME } },
548 { D117_PRED, { INTRA_FRAME, NONE_FRAME } },
549 { D45_PRED, { INTRA_FRAME, NONE_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700550
551#if CONFIG_EXT_INTER
552 { ZEROMV, { LAST_FRAME, INTRA_FRAME } },
553 { NEARESTMV, { LAST_FRAME, INTRA_FRAME } },
554 { NEARMV, { LAST_FRAME, INTRA_FRAME } },
555 { NEWMV, { LAST_FRAME, INTRA_FRAME } },
556
557#if CONFIG_EXT_REFS
558 { ZEROMV, { LAST2_FRAME, INTRA_FRAME } },
559 { NEARESTMV, { LAST2_FRAME, INTRA_FRAME } },
560 { NEARMV, { LAST2_FRAME, INTRA_FRAME } },
561 { NEWMV, { LAST2_FRAME, INTRA_FRAME } },
562
563 { ZEROMV, { LAST3_FRAME, INTRA_FRAME } },
564 { NEARESTMV, { LAST3_FRAME, INTRA_FRAME } },
565 { NEARMV, { LAST3_FRAME, INTRA_FRAME } },
566 { NEWMV, { LAST3_FRAME, INTRA_FRAME } },
567#endif // CONFIG_EXT_REFS
568
569 { ZEROMV, { GOLDEN_FRAME, INTRA_FRAME } },
570 { NEARESTMV, { GOLDEN_FRAME, INTRA_FRAME } },
571 { NEARMV, { GOLDEN_FRAME, INTRA_FRAME } },
572 { NEWMV, { GOLDEN_FRAME, INTRA_FRAME } },
573
574#if CONFIG_EXT_REFS
575 { ZEROMV, { BWDREF_FRAME, INTRA_FRAME } },
576 { NEARESTMV, { BWDREF_FRAME, INTRA_FRAME } },
577 { NEARMV, { BWDREF_FRAME, INTRA_FRAME } },
578 { NEWMV, { BWDREF_FRAME, INTRA_FRAME } },
Zoe Liue9b15e22017-07-19 15:53:01 -0700579
Zoe Liue9b15e22017-07-19 15:53:01 -0700580 { ZEROMV, { ALTREF2_FRAME, INTRA_FRAME } },
581 { NEARESTMV, { ALTREF2_FRAME, INTRA_FRAME } },
582 { NEARMV, { ALTREF2_FRAME, INTRA_FRAME } },
583 { NEWMV, { ALTREF2_FRAME, INTRA_FRAME } },
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584#endif // CONFIG_EXT_REFS
585
586 { ZEROMV, { ALTREF_FRAME, INTRA_FRAME } },
587 { NEARESTMV, { ALTREF_FRAME, INTRA_FRAME } },
588 { NEARMV, { ALTREF_FRAME, INTRA_FRAME } },
589 { NEWMV, { ALTREF_FRAME, INTRA_FRAME } },
590#endif // CONFIG_EXT_INTER
591};
592
hui su8a516a82017-07-06 10:00:36 -0700593static const PREDICTION_MODE intra_rd_search_mode_order[INTRA_MODES] = {
Urvang Joshi93b543a2017-06-01 17:32:41 -0700594 DC_PRED, H_PRED, V_PRED, SMOOTH_PRED, TM_PRED,
595#if CONFIG_SMOOTH_HV
hui su8a516a82017-07-06 10:00:36 -0700596 SMOOTH_V_PRED, SMOOTH_H_PRED,
Urvang Joshi93b543a2017-06-01 17:32:41 -0700597#endif // CONFIG_SMOOTH_HV
598 D135_PRED, D207_PRED, D153_PRED, D63_PRED, D117_PRED, D45_PRED,
hui su8a516a82017-07-06 10:00:36 -0700599};
600
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400601#if CONFIG_CFL
602static const UV_PREDICTION_MODE uv_rd_search_mode_order[UV_INTRA_MODES] = {
Urvang Joshi93b543a2017-06-01 17:32:41 -0700603 UV_DC_PRED, UV_CFL_PRED, UV_H_PRED,
604 UV_V_PRED, UV_SMOOTH_PRED, UV_TM_PRED,
605#if CONFIG_SMOOTH_HV
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400606 UV_SMOOTH_V_PRED, UV_SMOOTH_H_PRED,
Urvang Joshi93b543a2017-06-01 17:32:41 -0700607#endif // CONFIG_SMOOTH_HV
608 UV_D135_PRED, UV_D207_PRED, UV_D153_PRED,
609 UV_D63_PRED, UV_D117_PRED, UV_D45_PRED,
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400610};
611#else
612#define uv_rd_search_mode_order intra_rd_search_mode_order
613#endif // CONFIG_CFL
614
Yaowu Xuc27fc142016-08-22 16:08:15 -0700615static INLINE int write_uniform_cost(int n, int v) {
hui su37499292017-04-26 09:49:53 -0700616 const int l = get_unsigned_bits(n);
617 const int m = (1 << l) - n;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618 if (l == 0) return 0;
619 if (v < m)
Yaowu Xuf883b422016-08-30 14:01:10 -0700620 return (l - 1) * av1_cost_bit(128, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 else
Yaowu Xuf883b422016-08-30 14:01:10 -0700622 return l * av1_cost_bit(128, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623}
624
625// constants for prune 1 and prune 2 decision boundaries
626#define FAST_EXT_TX_CORR_MID 0.0
627#define FAST_EXT_TX_EDST_MID 0.1
628#define FAST_EXT_TX_CORR_MARGIN 0.5
629#define FAST_EXT_TX_EDST_MARGIN 0.3
630
Yushin Cho2f025aa2017-09-28 17:39:21 -0700631static unsigned pixel_dist_visible_only(
632 const AV1_COMP *const cpi, const MACROBLOCK *x, const uint8_t *src,
633 const int src_stride, const uint8_t *dst, const int dst_stride,
634 const BLOCK_SIZE tx_bsize, int txb_rows, int txb_cols, int visible_rows,
635 int visible_cols) {
636 unsigned sse;
637
638 if (txb_rows == visible_rows && txb_cols == visible_cols
639#if CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX)
640 && tx_bsize < BLOCK_SIZES
641#endif
642 ) {
643 cpi->fn_ptr[tx_bsize].vf(src, src_stride, dst, dst_stride, &sse);
644 return sse;
645 }
646#if CONFIG_HIGHBITDEPTH
647 const MACROBLOCKD *xd = &x->e_mbd;
648
649 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
650 uint64_t sse64 = aom_highbd_sse_odd_size(src, src_stride, dst, dst_stride,
651 visible_cols, visible_rows);
652 return (unsigned int)ROUND_POWER_OF_TWO(sse64, (xd->bd - 8) * 2);
653 }
654#else
655 (void)x;
656#endif // CONFIG_HIGHBITDEPTH
657 sse = aom_sse_odd_size(src, src_stride, dst, dst_stride, visible_cols,
658 visible_rows);
659 return sse;
660}
661
Yushin Choe30a47c2017-08-15 13:08:30 -0700662#if CONFIG_DIST_8X8
Yushin Choc49177e2017-07-18 17:18:09 -0700663static uint64_t cdef_dist_8x8_16bit(uint16_t *dst, int dstride, uint16_t *src,
664 int sstride, int coeff_shift) {
665 uint64_t svar = 0;
666 uint64_t dvar = 0;
667 uint64_t sum_s = 0;
668 uint64_t sum_d = 0;
669 uint64_t sum_s2 = 0;
670 uint64_t sum_d2 = 0;
671 uint64_t sum_sd = 0;
672 uint64_t dist = 0;
673
674 int i, j;
675 for (i = 0; i < 8; i++) {
676 for (j = 0; j < 8; j++) {
677 sum_s += src[i * sstride + j];
678 sum_d += dst[i * dstride + j];
679 sum_s2 += src[i * sstride + j] * src[i * sstride + j];
680 sum_d2 += dst[i * dstride + j] * dst[i * dstride + j];
681 sum_sd += src[i * sstride + j] * dst[i * dstride + j];
682 }
683 }
684 /* Compute the variance -- the calculation cannot go negative. */
685 svar = sum_s2 - ((sum_s * sum_s + 32) >> 6);
686 dvar = sum_d2 - ((sum_d * sum_d + 32) >> 6);
687
688 // Tuning of jm's original dering distortion metric used in CDEF tool,
689 // suggested by jm
690 const uint64_t a = 4;
691 const uint64_t b = 2;
692 const uint64_t c1 = (400 * a << 2 * coeff_shift);
693 const uint64_t c2 = (b * 20000 * a * a << 4 * coeff_shift);
694
695 dist =
696 (uint64_t)floor(.5 +
697 (sum_d2 + sum_s2 - 2 * sum_sd) * .5 * (svar + dvar + c1) /
698 (sqrt(svar * (double)dvar + c2)));
699
700 // Calibrate dist to have similar rate for the same QP with MSE only
701 // distortion (as in master branch)
702 dist = (uint64_t)((float)dist * 0.75);
703
704 return dist;
705}
Yushin Choc49177e2017-07-18 17:18:09 -0700706
Yushin Chob7b60c52017-07-14 16:18:52 -0700707static int od_compute_var_4x4(uint16_t *x, int stride) {
Yushin Cho7a428ba2017-01-12 16:28:49 -0800708 int sum;
709 int s2;
710 int i;
711 sum = 0;
712 s2 = 0;
713 for (i = 0; i < 4; i++) {
714 int j;
715 for (j = 0; j < 4; j++) {
716 int t;
717
718 t = x[i * stride + j];
719 sum += t;
720 s2 += t * t;
721 }
722 }
Yushin Chob7b60c52017-07-14 16:18:52 -0700723
Yushin Cho7a428ba2017-01-12 16:28:49 -0800724 return (s2 - (sum * sum >> 4)) >> 4;
725}
726
Jean-Marc Valin79c0f322017-01-18 01:58:33 -0500727/* OD_DIST_LP_MID controls the frequency weighting filter used for computing
728 the distortion. For a value X, the filter is [1 X 1]/(X + 2) and
729 is applied both horizontally and vertically. For X=5, the filter is
730 a good approximation for the OD_QM8_Q4_HVS quantization matrix. */
731#define OD_DIST_LP_MID (5)
732#define OD_DIST_LP_NORM (OD_DIST_LP_MID + 2)
733
Yushin Chob7b60c52017-07-14 16:18:52 -0700734static double od_compute_dist_8x8(int use_activity_masking, uint16_t *x,
735 uint16_t *y, od_coeff *e_lp, int stride) {
Yushin Cho7a428ba2017-01-12 16:28:49 -0800736 double sum;
737 int min_var;
738 double mean_var;
739 double var_stat;
740 double activity;
741 double calibration;
742 int i;
743 int j;
744 double vardist;
Yushin Cho7a428ba2017-01-12 16:28:49 -0800745
746 vardist = 0;
Yushin Chob7b60c52017-07-14 16:18:52 -0700747
Yushin Cho7a428ba2017-01-12 16:28:49 -0800748#if 1
749 min_var = INT_MAX;
750 mean_var = 0;
751 for (i = 0; i < 3; i++) {
752 for (j = 0; j < 3; j++) {
753 int varx;
754 int vary;
755 varx = od_compute_var_4x4(x + 2 * i * stride + 2 * j, stride);
756 vary = od_compute_var_4x4(y + 2 * i * stride + 2 * j, stride);
757 min_var = OD_MINI(min_var, varx);
758 mean_var += 1. / (1 + varx);
759 /* The cast to (double) is to avoid an overflow before the sqrt.*/
760 vardist += varx - 2 * sqrt(varx * (double)vary) + vary;
761 }
762 }
763 /* We use a different variance statistic depending on whether activity
James Zern89a015b2017-08-08 12:39:00 -0400764 masking is used, since the harmonic mean appeared slightly worse with
Yushin Cho7a428ba2017-01-12 16:28:49 -0800765 masking off. The calibration constant just ensures that we preserve the
766 rate compared to activity=1. */
767 if (use_activity_masking) {
768 calibration = 1.95;
769 var_stat = 9. / mean_var;
770 } else {
771 calibration = 1.62;
772 var_stat = min_var;
773 }
774 /* 1.62 is a calibration constant, 0.25 is a noise floor and 1/6 is the
775 activity masking constant. */
776 activity = calibration * pow(.25 + var_stat, -1. / 6);
777#else
778 activity = 1;
Fergus Simpson4063a682017-02-28 16:52:22 -0800779#endif // 1
Yushin Cho7a428ba2017-01-12 16:28:49 -0800780 sum = 0;
781 for (i = 0; i < 8; i++) {
782 for (j = 0; j < 8; j++)
Jean-Marc Valin79c0f322017-01-18 01:58:33 -0500783 sum += e_lp[i * stride + j] * (double)e_lp[i * stride + j];
Yushin Cho7a428ba2017-01-12 16:28:49 -0800784 }
Jean-Marc Valin79c0f322017-01-18 01:58:33 -0500785 /* Normalize the filter to unit DC response. */
786 sum *= 1. / (OD_DIST_LP_NORM * OD_DIST_LP_NORM * OD_DIST_LP_NORM *
787 OD_DIST_LP_NORM);
Yushin Cho7a428ba2017-01-12 16:28:49 -0800788 return activity * activity * (sum + vardist);
789}
790
791// Note : Inputs x and y are in a pixel domain
Yushin Chob7b60c52017-07-14 16:18:52 -0700792static double od_compute_dist_common(int activity_masking, uint16_t *x,
793 uint16_t *y, int bsize_w, int bsize_h,
Yushin Cho75b01002017-06-21 13:43:57 -0700794 int qindex, od_coeff *tmp,
795 od_coeff *e_lp) {
796 int i, j;
797 double sum = 0;
798 const int mid = OD_DIST_LP_MID;
799
800 for (j = 0; j < bsize_w; j++) {
801 e_lp[j] = mid * tmp[j] + 2 * tmp[bsize_w + j];
802 e_lp[(bsize_h - 1) * bsize_w + j] = mid * tmp[(bsize_h - 1) * bsize_w + j] +
803 2 * tmp[(bsize_h - 2) * bsize_w + j];
804 }
805 for (i = 1; i < bsize_h - 1; i++) {
806 for (j = 0; j < bsize_w; j++) {
807 e_lp[i * bsize_w + j] = mid * tmp[i * bsize_w + j] +
808 tmp[(i - 1) * bsize_w + j] +
809 tmp[(i + 1) * bsize_w + j];
810 }
811 }
812 for (i = 0; i < bsize_h; i += 8) {
813 for (j = 0; j < bsize_w; j += 8) {
Yushin Chob7b60c52017-07-14 16:18:52 -0700814 sum += od_compute_dist_8x8(activity_masking, &x[i * bsize_w + j],
Yushin Cho75b01002017-06-21 13:43:57 -0700815 &y[i * bsize_w + j], &e_lp[i * bsize_w + j],
816 bsize_w);
817 }
818 }
819 /* Scale according to linear regression against SSE, for 8x8 blocks. */
820 if (activity_masking) {
821 sum *= 2.2 + (1.7 - 2.2) * (qindex - 99) / (210 - 99) +
822 (qindex < 99 ? 2.5 * (qindex - 99) / 99 * (qindex - 99) / 99 : 0);
823 } else {
824 sum *= qindex >= 128
825 ? 1.4 + (0.9 - 1.4) * (qindex - 128) / (209 - 128)
826 : qindex <= 43 ? 1.5 + (2.0 - 1.5) * (qindex - 43) / (16 - 43)
827 : 1.5 + (1.4 - 1.5) * (qindex - 43) / (128 - 43);
828 }
829
830 return sum;
831}
832
Yushin Chob7b60c52017-07-14 16:18:52 -0700833static double od_compute_dist(uint16_t *x, uint16_t *y, int bsize_w,
834 int bsize_h, int qindex) {
Yushin Cho7a428ba2017-01-12 16:28:49 -0800835 assert(bsize_w >= 8 && bsize_h >= 8);
Yushin Chob7b60c52017-07-14 16:18:52 -0700836#if CONFIG_PVQ
837 int activity_masking = 1;
838#else
839 int activity_masking = 0;
840#endif
Luc Trudeau1f7c4112017-09-13 15:10:08 -0400841 int i, j;
842 DECLARE_ALIGNED(16, od_coeff, e[MAX_TX_SQUARE]);
843 DECLARE_ALIGNED(16, od_coeff, tmp[MAX_TX_SQUARE]);
844 DECLARE_ALIGNED(16, od_coeff, e_lp[MAX_TX_SQUARE]);
845 for (i = 0; i < bsize_h; i++) {
846 for (j = 0; j < bsize_w; j++) {
847 e[i * bsize_w + j] = x[i * bsize_w + j] - y[i * bsize_w + j];
Jean-Marc Valin79c0f322017-01-18 01:58:33 -0500848 }
Yushin Cho75b01002017-06-21 13:43:57 -0700849 }
Luc Trudeau1f7c4112017-09-13 15:10:08 -0400850 int mid = OD_DIST_LP_MID;
851 for (i = 0; i < bsize_h; i++) {
852 tmp[i * bsize_w] = mid * e[i * bsize_w] + 2 * e[i * bsize_w + 1];
853 tmp[i * bsize_w + bsize_w - 1] =
854 mid * e[i * bsize_w + bsize_w - 1] + 2 * e[i * bsize_w + bsize_w - 2];
855 for (j = 1; j < bsize_w - 1; j++) {
856 tmp[i * bsize_w + j] = mid * e[i * bsize_w + j] + e[i * bsize_w + j - 1] +
857 e[i * bsize_w + j + 1];
858 }
859 }
860 return od_compute_dist_common(activity_masking, x, y, bsize_w, bsize_h,
861 qindex, tmp, e_lp);
Yushin Cho75b01002017-06-21 13:43:57 -0700862}
863
Yushin Chob7b60c52017-07-14 16:18:52 -0700864static double od_compute_dist_diff(uint16_t *x, int16_t *e, int bsize_w,
865 int bsize_h, int qindex) {
Yushin Cho75b01002017-06-21 13:43:57 -0700866 assert(bsize_w >= 8 && bsize_h >= 8);
Yushin Chob7b60c52017-07-14 16:18:52 -0700867#if CONFIG_PVQ
868 int activity_masking = 1;
869#else
870 int activity_masking = 0;
871#endif
Luc Trudeau1f7c4112017-09-13 15:10:08 -0400872 DECLARE_ALIGNED(16, uint16_t, y[MAX_TX_SQUARE]);
873 DECLARE_ALIGNED(16, od_coeff, tmp[MAX_TX_SQUARE]);
874 DECLARE_ALIGNED(16, od_coeff, e_lp[MAX_TX_SQUARE]);
875 int i, j;
876 for (i = 0; i < bsize_h; i++) {
877 for (j = 0; j < bsize_w; j++) {
878 y[i * bsize_w + j] = x[i * bsize_w + j] - e[i * bsize_w + j];
Jean-Marc Valin79c0f322017-01-18 01:58:33 -0500879 }
Yushin Cho7a428ba2017-01-12 16:28:49 -0800880 }
Luc Trudeau1f7c4112017-09-13 15:10:08 -0400881 int mid = OD_DIST_LP_MID;
882 for (i = 0; i < bsize_h; i++) {
883 tmp[i * bsize_w] = mid * e[i * bsize_w] + 2 * e[i * bsize_w + 1];
884 tmp[i * bsize_w + bsize_w - 1] =
885 mid * e[i * bsize_w + bsize_w - 1] + 2 * e[i * bsize_w + bsize_w - 2];
886 for (j = 1; j < bsize_w - 1; j++) {
887 tmp[i * bsize_w + j] = mid * e[i * bsize_w + j] + e[i * bsize_w + j - 1] +
888 e[i * bsize_w + j + 1];
889 }
890 }
891 return od_compute_dist_common(activity_masking, x, y, bsize_w, bsize_h,
892 qindex, tmp, e_lp);
Yushin Cho7a428ba2017-01-12 16:28:49 -0800893}
894
Yushin Choe30a47c2017-08-15 13:08:30 -0700895int64_t av1_dist_8x8(const AV1_COMP *const cpi, const MACROBLOCK *x,
Yushin Chob7b60c52017-07-14 16:18:52 -0700896 const uint8_t *src, int src_stride, const uint8_t *dst,
897 int dst_stride, const BLOCK_SIZE tx_bsize, int bsw,
898 int bsh, int visible_w, int visible_h, int qindex) {
899 int64_t d = 0;
Yushin Cho7a428ba2017-01-12 16:28:49 -0800900 int i, j;
Yushin Choe30a47c2017-08-15 13:08:30 -0700901 const MACROBLOCKD *xd = &x->e_mbd;
Yushin Chob7b60c52017-07-14 16:18:52 -0700902
903 DECLARE_ALIGNED(16, uint16_t, orig[MAX_TX_SQUARE]);
904 DECLARE_ALIGNED(16, uint16_t, rec[MAX_TX_SQUARE]);
Yushin Chob7b60c52017-07-14 16:18:52 -0700905
Yushin Choee810272017-09-13 17:30:25 -0700906 assert(bsw >= 8);
907 assert(bsh >= 8);
908 assert((bsw & 0x07) == 0);
909 assert((bsh & 0x07) == 0);
910
Yushin Choe30a47c2017-08-15 13:08:30 -0700911 if (x->tune_metric == AOM_TUNE_CDEF_DIST ||
912 x->tune_metric == AOM_TUNE_DAALA_DIST) {
Yushin Cho8ab875d2017-06-23 14:47:21 -0700913#if CONFIG_HIGHBITDEPTH
Yushin Choe30a47c2017-08-15 13:08:30 -0700914 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Yushin Cho75b01002017-06-21 13:43:57 -0700915 for (j = 0; j < bsh; j++)
Yushin Cho8ab875d2017-06-23 14:47:21 -0700916 for (i = 0; i < bsw; i++)
Yushin Choe30a47c2017-08-15 13:08:30 -0700917 orig[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i];
Yushin Cho75b01002017-06-21 13:43:57 -0700918
Yushin Choe30a47c2017-08-15 13:08:30 -0700919 if ((bsw == visible_w) && (bsh == visible_h)) {
Yushin Cho8ab875d2017-06-23 14:47:21 -0700920 for (j = 0; j < bsh; j++)
Yushin Cho8ab875d2017-06-23 14:47:21 -0700921 for (i = 0; i < bsw; i++)
Yushin Choe30a47c2017-08-15 13:08:30 -0700922 rec[j * bsw + i] = CONVERT_TO_SHORTPTR(dst)[j * dst_stride + i];
923 } else {
924 for (j = 0; j < visible_h; j++)
925 for (i = 0; i < visible_w; i++)
926 rec[j * bsw + i] = CONVERT_TO_SHORTPTR(dst)[j * dst_stride + i];
Yushin Cho8ab875d2017-06-23 14:47:21 -0700927
Yushin Choe30a47c2017-08-15 13:08:30 -0700928 if (visible_w < bsw) {
929 for (j = 0; j < bsh; j++)
930 for (i = visible_w; i < bsw; i++)
931 rec[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i];
932 }
933
934 if (visible_h < bsh) {
935 for (j = visible_h; j < bsh; j++)
936 for (i = 0; i < bsw; i++)
937 rec[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i];
938 }
939 }
Yushin Cho8ab875d2017-06-23 14:47:21 -0700940 } else {
Yushin Choe30a47c2017-08-15 13:08:30 -0700941#endif
942 for (j = 0; j < bsh; j++)
943 for (i = 0; i < bsw; i++) orig[j * bsw + i] = src[j * src_stride + i];
Yushin Cho8ab875d2017-06-23 14:47:21 -0700944
Yushin Choe30a47c2017-08-15 13:08:30 -0700945 if ((bsw == visible_w) && (bsh == visible_h)) {
Yushin Cho8ab875d2017-06-23 14:47:21 -0700946 for (j = 0; j < bsh; j++)
Yushin Choe30a47c2017-08-15 13:08:30 -0700947 for (i = 0; i < bsw; i++) rec[j * bsw + i] = dst[j * dst_stride + i];
948 } else {
949 for (j = 0; j < visible_h; j++)
950 for (i = 0; i < visible_w; i++)
951 rec[j * bsw + i] = dst[j * dst_stride + i];
Yushin Cho8ab875d2017-06-23 14:47:21 -0700952
Yushin Choe30a47c2017-08-15 13:08:30 -0700953 if (visible_w < bsw) {
954 for (j = 0; j < bsh; j++)
955 for (i = visible_w; i < bsw; i++)
956 rec[j * bsw + i] = src[j * src_stride + i];
957 }
958
959 if (visible_h < bsh) {
960 for (j = visible_h; j < bsh; j++)
961 for (i = 0; i < bsw; i++)
962 rec[j * bsw + i] = src[j * src_stride + i];
963 }
Yushin Cho8ab875d2017-06-23 14:47:21 -0700964 }
Yushin Cho8ab875d2017-06-23 14:47:21 -0700965#if CONFIG_HIGHBITDEPTH
Yushin Choe30a47c2017-08-15 13:08:30 -0700966 }
Yushin Cho8ab875d2017-06-23 14:47:21 -0700967#endif // CONFIG_HIGHBITDEPTH
Yushin Choe30a47c2017-08-15 13:08:30 -0700968 }
Yushin Cho8ab875d2017-06-23 14:47:21 -0700969
Yushin Choe30a47c2017-08-15 13:08:30 -0700970 if (x->tune_metric == AOM_TUNE_DAALA_DIST) {
971 d = (int64_t)od_compute_dist(orig, rec, bsw, bsh, qindex);
972 } else if (x->tune_metric == AOM_TUNE_CDEF_DIST) {
Yushin Choc49177e2017-07-18 17:18:09 -0700973 int coeff_shift = AOMMAX(xd->bd - 8, 0);
974
975 for (i = 0; i < bsh; i += 8) {
976 for (j = 0; j < bsw; j += 8) {
977 d += cdef_dist_8x8_16bit(&rec[i * bsw + j], bsw, &orig[i * bsw + j],
978 bsw, coeff_shift);
979 }
980 }
981#if CONFIG_HIGHBITDEPTH
982 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
983 d = ((uint64_t)d) >> 2 * coeff_shift;
984#endif
Yushin Choe30a47c2017-08-15 13:08:30 -0700985 } else {
986 // Otherwise, MSE by default
Yushin Cho2f025aa2017-09-28 17:39:21 -0700987 d = pixel_dist_visible_only(cpi, x, src, src_stride, dst, dst_stride,
988 tx_bsize, bsh, bsw, visible_h, visible_w);
Yushin Choc49177e2017-07-18 17:18:09 -0700989 }
Yushin Chob7b60c52017-07-14 16:18:52 -0700990
Yushin Cho7a428ba2017-01-12 16:28:49 -0800991 return d;
992}
Yushin Cho75b01002017-06-21 13:43:57 -0700993
Yushin Choe30a47c2017-08-15 13:08:30 -0700994static int64_t av1_dist_8x8_diff(const MACROBLOCK *x, const uint8_t *src,
Yushin Chob7b60c52017-07-14 16:18:52 -0700995 int src_stride, const int16_t *diff,
996 int diff_stride, int bsw, int bsh,
997 int visible_w, int visible_h, int qindex) {
998 int64_t d = 0;
Yushin Cho75b01002017-06-21 13:43:57 -0700999 int i, j;
Yushin Choe30a47c2017-08-15 13:08:30 -07001000 const MACROBLOCKD *xd = &x->e_mbd;
Yushin Chob7b60c52017-07-14 16:18:52 -07001001
1002 DECLARE_ALIGNED(16, uint16_t, orig[MAX_TX_SQUARE]);
1003 DECLARE_ALIGNED(16, int16_t, diff16[MAX_TX_SQUARE]);
Yushin Chob7b60c52017-07-14 16:18:52 -07001004
Yushin Choee810272017-09-13 17:30:25 -07001005 assert(bsw >= 8);
1006 assert(bsh >= 8);
1007 assert((bsw & 0x07) == 0);
1008 assert((bsh & 0x07) == 0);
1009
Yushin Choe30a47c2017-08-15 13:08:30 -07001010 if (x->tune_metric == AOM_TUNE_CDEF_DIST ||
1011 x->tune_metric == AOM_TUNE_DAALA_DIST) {
Yushin Cho8ab875d2017-06-23 14:47:21 -07001012#if CONFIG_HIGHBITDEPTH
Yushin Choe30a47c2017-08-15 13:08:30 -07001013 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1014 for (j = 0; j < bsh; j++)
1015 for (i = 0; i < bsw; i++)
1016 orig[j * bsw + i] = CONVERT_TO_SHORTPTR(src)[j * src_stride + i];
1017 } else {
Yushin Cho8ab875d2017-06-23 14:47:21 -07001018#endif
Yushin Choe30a47c2017-08-15 13:08:30 -07001019 for (j = 0; j < bsh; j++)
1020 for (i = 0; i < bsw; i++) orig[j * bsw + i] = src[j * src_stride + i];
Yushin Cho8ab875d2017-06-23 14:47:21 -07001021#if CONFIG_HIGHBITDEPTH
Yushin Choe30a47c2017-08-15 13:08:30 -07001022 }
Yushin Cho8ab875d2017-06-23 14:47:21 -07001023#endif // CONFIG_HIGHBITDEPTH
Yushin Cho75b01002017-06-21 13:43:57 -07001024
Yushin Choe30a47c2017-08-15 13:08:30 -07001025 if ((bsw == visible_w) && (bsh == visible_h)) {
Yushin Cho75b01002017-06-21 13:43:57 -07001026 for (j = 0; j < bsh; j++)
Yushin Choe30a47c2017-08-15 13:08:30 -07001027 for (i = 0; i < bsw; i++)
1028 diff16[j * bsw + i] = diff[j * diff_stride + i];
1029 } else {
1030 for (j = 0; j < visible_h; j++)
1031 for (i = 0; i < visible_w; i++)
1032 diff16[j * bsw + i] = diff[j * diff_stride + i];
Yushin Cho75b01002017-06-21 13:43:57 -07001033
Yushin Choe30a47c2017-08-15 13:08:30 -07001034 if (visible_w < bsw) {
1035 for (j = 0; j < bsh; j++)
1036 for (i = visible_w; i < bsw; i++) diff16[j * bsw + i] = 0;
1037 }
1038
1039 if (visible_h < bsh) {
1040 for (j = visible_h; j < bsh; j++)
1041 for (i = 0; i < bsw; i++) diff16[j * bsw + i] = 0;
1042 }
Yushin Cho75b01002017-06-21 13:43:57 -07001043 }
1044 }
Yushin Cho8ab875d2017-06-23 14:47:21 -07001045
Yushin Choe30a47c2017-08-15 13:08:30 -07001046 if (x->tune_metric == AOM_TUNE_DAALA_DIST) {
1047 d = (int64_t)od_compute_dist_diff(orig, diff16, bsw, bsh, qindex);
1048 } else if (x->tune_metric == AOM_TUNE_CDEF_DIST) {
Yushin Choc49177e2017-07-18 17:18:09 -07001049 int coeff_shift = AOMMAX(xd->bd - 8, 0);
1050 DECLARE_ALIGNED(16, uint16_t, dst16[MAX_TX_SQUARE]);
1051
1052 for (i = 0; i < bsh; i++) {
1053 for (j = 0; j < bsw; j++) {
1054 dst16[i * bsw + j] = orig[i * bsw + j] - diff16[i * bsw + j];
1055 }
1056 }
1057
1058 for (i = 0; i < bsh; i += 8) {
1059 for (j = 0; j < bsw; j += 8) {
1060 d += cdef_dist_8x8_16bit(&dst16[i * bsw + j], bsw, &orig[i * bsw + j],
1061 bsw, coeff_shift);
1062 }
1063 }
1064 // Don't scale 'd' for HBD since it will be done by caller side for diff
1065 // input
Yushin Choe30a47c2017-08-15 13:08:30 -07001066 } else {
1067 // Otherwise, MSE by default
Yushin Cho2f025aa2017-09-28 17:39:21 -07001068 d = aom_sum_squares_2d_i16(diff, diff_stride, visible_w, visible_h);
Yushin Choc49177e2017-07-18 17:18:09 -07001069 }
Yushin Cho75b01002017-06-21 13:43:57 -07001070
1071 return d;
1072}
Yushin Chob7b60c52017-07-14 16:18:52 -07001073#endif // CONFIG_DIST_8X8
Yushin Cho7a428ba2017-01-12 16:28:49 -08001074
Yaowu Xuf883b422016-08-30 14:01:10 -07001075static void get_energy_distribution_fine(const AV1_COMP *cpi, BLOCK_SIZE bsize,
Alex Converse9f217762017-04-20 15:34:54 -07001076 const uint8_t *src, int src_stride,
1077 const uint8_t *dst, int dst_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001078 double *hordist, double *verdist) {
Alex Converse9f217762017-04-20 15:34:54 -07001079 const int bw = block_size_wide[bsize];
1080 const int bh = block_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001081 unsigned int esq[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -07001082
1083 const int f_index = bsize - BLOCK_16X16;
1084 if (f_index < 0) {
Alex Converse9f217762017-04-20 15:34:54 -07001085 const int w_shift = bw == 8 ? 1 : 2;
1086 const int h_shift = bh == 8 ? 1 : 2;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001087#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001088 if (cpi->common.use_highbitdepth) {
Alex Converse9f217762017-04-20 15:34:54 -07001089 const uint16_t *src16 = CONVERT_TO_SHORTPTR(src);
1090 const uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst);
1091 for (int i = 0; i < bh; ++i)
1092 for (int j = 0; j < bw; ++j) {
1093 const int index = (j >> w_shift) + ((i >> h_shift) << 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001094 esq[index] +=
1095 (src16[j + i * src_stride] - dst16[j + i * dst_stride]) *
1096 (src16[j + i * src_stride] - dst16[j + i * dst_stride]);
1097 }
1098 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001099#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001100
Alex Converse9f217762017-04-20 15:34:54 -07001101 for (int i = 0; i < bh; ++i)
1102 for (int j = 0; j < bw; ++j) {
1103 const int index = (j >> w_shift) + ((i >> h_shift) << 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104 esq[index] += (src[j + i * src_stride] - dst[j + i * dst_stride]) *
1105 (src[j + i * src_stride] - dst[j + i * dst_stride]);
1106 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001107#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001108 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001109#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001110 } else {
Alex Converse9f217762017-04-20 15:34:54 -07001111 cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[0]);
1112 cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride,
1113 &esq[1]);
1114 cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride,
1115 &esq[2]);
1116 cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4,
1117 dst_stride, &esq[3]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001118 src += bh / 4 * src_stride;
1119 dst += bh / 4 * dst_stride;
1120
Alex Converse9f217762017-04-20 15:34:54 -07001121 cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[4]);
1122 cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride,
1123 &esq[5]);
1124 cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride,
1125 &esq[6]);
1126 cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4,
1127 dst_stride, &esq[7]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001128 src += bh / 4 * src_stride;
1129 dst += bh / 4 * dst_stride;
1130
Alex Converse9f217762017-04-20 15:34:54 -07001131 cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[8]);
1132 cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride,
1133 &esq[9]);
1134 cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride,
1135 &esq[10]);
1136 cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4,
1137 dst_stride, &esq[11]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001138 src += bh / 4 * src_stride;
1139 dst += bh / 4 * dst_stride;
1140
Alex Converse9f217762017-04-20 15:34:54 -07001141 cpi->fn_ptr[f_index].vf(src, src_stride, dst, dst_stride, &esq[12]);
1142 cpi->fn_ptr[f_index].vf(src + bw / 4, src_stride, dst + bw / 4, dst_stride,
1143 &esq[13]);
1144 cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, dst + bw / 2, dst_stride,
1145 &esq[14]);
1146 cpi->fn_ptr[f_index].vf(src + 3 * bw / 4, src_stride, dst + 3 * bw / 4,
1147 dst_stride, &esq[15]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001148 }
1149
Alex Converse9f217762017-04-20 15:34:54 -07001150 double total = (double)esq[0] + esq[1] + esq[2] + esq[3] + esq[4] + esq[5] +
1151 esq[6] + esq[7] + esq[8] + esq[9] + esq[10] + esq[11] +
1152 esq[12] + esq[13] + esq[14] + esq[15];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001153 if (total > 0) {
1154 const double e_recip = 1.0 / total;
Alex Converse9f217762017-04-20 15:34:54 -07001155 hordist[0] = ((double)esq[0] + esq[4] + esq[8] + esq[12]) * e_recip;
1156 hordist[1] = ((double)esq[1] + esq[5] + esq[9] + esq[13]) * e_recip;
1157 hordist[2] = ((double)esq[2] + esq[6] + esq[10] + esq[14]) * e_recip;
1158 verdist[0] = ((double)esq[0] + esq[1] + esq[2] + esq[3]) * e_recip;
1159 verdist[1] = ((double)esq[4] + esq[5] + esq[6] + esq[7]) * e_recip;
1160 verdist[2] = ((double)esq[8] + esq[9] + esq[10] + esq[11]) * e_recip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001161 } else {
1162 hordist[0] = verdist[0] = 0.25;
1163 hordist[1] = verdist[1] = 0.25;
1164 hordist[2] = verdist[2] = 0.25;
1165 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166}
1167
Urvang Joshi0d1e4ff2017-04-27 16:17:25 -07001168static int adst_vs_flipadst(const AV1_COMP *cpi, BLOCK_SIZE bsize,
1169 const uint8_t *src, int src_stride,
1170 const uint8_t *dst, int dst_stride) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001171 int prune_bitmask = 0;
1172 double svm_proj_h = 0, svm_proj_v = 0;
Alex Converse89912f92017-04-21 13:28:50 -07001173 double hdist[3] = { 0, 0, 0 }, vdist[3] = { 0, 0, 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -07001174 get_energy_distribution_fine(cpi, bsize, src, src_stride, dst, dst_stride,
1175 hdist, vdist);
1176
1177 svm_proj_v = vdist[0] * ADST_FLIP_SVM[0] + vdist[1] * ADST_FLIP_SVM[1] +
1178 vdist[2] * ADST_FLIP_SVM[2] + ADST_FLIP_SVM[3];
1179 svm_proj_h = hdist[0] * ADST_FLIP_SVM[4] + hdist[1] * ADST_FLIP_SVM[5] +
1180 hdist[2] * ADST_FLIP_SVM[6] + ADST_FLIP_SVM[7];
1181 if (svm_proj_v > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN)
1182 prune_bitmask |= 1 << FLIPADST_1D;
1183 else if (svm_proj_v < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN)
1184 prune_bitmask |= 1 << ADST_1D;
1185
1186 if (svm_proj_h > FAST_EXT_TX_EDST_MID + FAST_EXT_TX_EDST_MARGIN)
1187 prune_bitmask |= 1 << (FLIPADST_1D + 8);
1188 else if (svm_proj_h < FAST_EXT_TX_EDST_MID - FAST_EXT_TX_EDST_MARGIN)
1189 prune_bitmask |= 1 << (ADST_1D + 8);
1190
1191 return prune_bitmask;
1192}
1193
1194#if CONFIG_EXT_TX
Alex Converse89912f92017-04-21 13:28:50 -07001195static void get_horver_correlation(const int16_t *diff, int stride, int w,
1196 int h, double *hcorr, double *vcorr) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197 // Returns hor/ver correlation coefficient
1198 const int num = (h - 1) * (w - 1);
1199 double num_r;
1200 int i, j;
1201 int64_t xy_sum = 0, xz_sum = 0;
1202 int64_t x_sum = 0, y_sum = 0, z_sum = 0;
1203 int64_t x2_sum = 0, y2_sum = 0, z2_sum = 0;
1204 double x_var_n, y_var_n, z_var_n, xy_var_n, xz_var_n;
1205 *hcorr = *vcorr = 1;
1206
1207 assert(num > 0);
1208 num_r = 1.0 / num;
1209 for (i = 1; i < h; ++i) {
1210 for (j = 1; j < w; ++j) {
1211 const int16_t x = diff[i * stride + j];
1212 const int16_t y = diff[i * stride + j - 1];
1213 const int16_t z = diff[(i - 1) * stride + j];
1214 xy_sum += x * y;
1215 xz_sum += x * z;
1216 x_sum += x;
1217 y_sum += y;
1218 z_sum += z;
1219 x2_sum += x * x;
1220 y2_sum += y * y;
1221 z2_sum += z * z;
1222 }
1223 }
1224 x_var_n = x2_sum - (x_sum * x_sum) * num_r;
1225 y_var_n = y2_sum - (y_sum * y_sum) * num_r;
1226 z_var_n = z2_sum - (z_sum * z_sum) * num_r;
1227 xy_var_n = xy_sum - (x_sum * y_sum) * num_r;
1228 xz_var_n = xz_sum - (x_sum * z_sum) * num_r;
1229 if (x_var_n > 0 && y_var_n > 0) {
1230 *hcorr = xy_var_n / sqrt(x_var_n * y_var_n);
1231 *hcorr = *hcorr < 0 ? 0 : *hcorr;
1232 }
1233 if (x_var_n > 0 && z_var_n > 0) {
1234 *vcorr = xz_var_n / sqrt(x_var_n * z_var_n);
1235 *vcorr = *vcorr < 0 ? 0 : *vcorr;
1236 }
1237}
1238
Alex Converse89912f92017-04-21 13:28:50 -07001239int dct_vs_idtx(const int16_t *diff, int stride, int w, int h) {
1240 double hcorr, vcorr;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001241 int prune_bitmask = 0;
Alex Converse89912f92017-04-21 13:28:50 -07001242 get_horver_correlation(diff, stride, w, h, &hcorr, &vcorr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001243
Alex Converse89912f92017-04-21 13:28:50 -07001244 if (vcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001245 prune_bitmask |= 1 << IDTX_1D;
Alex Converse89912f92017-04-21 13:28:50 -07001246 else if (vcorr < FAST_EXT_TX_CORR_MID - FAST_EXT_TX_CORR_MARGIN)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001247 prune_bitmask |= 1 << DCT_1D;
1248
Alex Converse89912f92017-04-21 13:28:50 -07001249 if (hcorr > FAST_EXT_TX_CORR_MID + FAST_EXT_TX_CORR_MARGIN)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001250 prune_bitmask |= 1 << (IDTX_1D + 8);
Alex Converse89912f92017-04-21 13:28:50 -07001251 else if (hcorr < FAST_EXT_TX_CORR_MID - FAST_EXT_TX_CORR_MARGIN)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001252 prune_bitmask |= 1 << (DCT_1D + 8);
1253 return prune_bitmask;
1254}
1255
1256// Performance drop: 0.5%, Speed improvement: 24%
Yaowu Xuf883b422016-08-30 14:01:10 -07001257static int prune_two_for_sby(const AV1_COMP *cpi, BLOCK_SIZE bsize,
Alex Converse89912f92017-04-21 13:28:50 -07001258 MACROBLOCK *x, const MACROBLOCKD *xd,
1259 int adst_flipadst, int dct_idtx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001260 int prune = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261
Alex Converse89912f92017-04-21 13:28:50 -07001262 if (adst_flipadst) {
1263 const struct macroblock_plane *const p = &x->plane[0];
1264 const struct macroblockd_plane *const pd = &xd->plane[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001265 prune |= adst_vs_flipadst(cpi, bsize, p->src.buf, p->src.stride,
Alex Converse89912f92017-04-21 13:28:50 -07001266 pd->dst.buf, pd->dst.stride);
1267 }
1268 if (dct_idtx) {
1269 av1_subtract_plane(x, bsize, 0);
1270 const struct macroblock_plane *const p = &x->plane[0];
1271 const int bw = 4 << (b_width_log2_lookup[bsize]);
1272 const int bh = 4 << (b_height_log2_lookup[bsize]);
1273 prune |= dct_vs_idtx(p->src_diff, bw, bw, bh);
1274 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001275
1276 return prune;
1277}
1278#endif // CONFIG_EXT_TX
1279
1280// Performance drop: 0.3%, Speed improvement: 5%
Yaowu Xuf883b422016-08-30 14:01:10 -07001281static int prune_one_for_sby(const AV1_COMP *cpi, BLOCK_SIZE bsize,
Alex Converse89912f92017-04-21 13:28:50 -07001282 const MACROBLOCK *x, const MACROBLOCKD *xd) {
1283 const struct macroblock_plane *const p = &x->plane[0];
1284 const struct macroblockd_plane *const pd = &xd->plane[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001285 return adst_vs_flipadst(cpi, bsize, p->src.buf, p->src.stride, pd->dst.buf,
Alex Converse89912f92017-04-21 13:28:50 -07001286 pd->dst.stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001287}
1288
Hui Su032ab8b2017-09-19 14:53:40 -07001289#if CONFIG_EXT_TX
1290// 1D Transforms used in inter set, this needs to be changed if
1291// ext_tx_used_inter is changed
1292static const int ext_tx_used_inter_1D[EXT_TX_SETS_INTER][TX_TYPES_1D] = {
1293 { 1, 0, 0, 0 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 0, 0, 1 },
1294#if CONFIG_MRC_TX
1295 { 1, 0, 0, 1 },
1296#endif // CONFIG_MRC_TX
1297};
1298#endif // CONFIG_EXT_TX
1299
Yaowu Xuf883b422016-08-30 14:01:10 -07001300static int prune_tx_types(const AV1_COMP *cpi, BLOCK_SIZE bsize, MACROBLOCK *x,
Urvang Joshi0d1e4ff2017-04-27 16:17:25 -07001301 const MACROBLOCKD *const xd, int tx_set) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001302#if CONFIG_EXT_TX
Yaowu Xu37fe5fb2017-05-09 11:43:45 -07001303 const int *tx_set_1D = tx_set >= 0 ? ext_tx_used_inter_1D[tx_set] : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001304#else
1305 const int tx_set_1D[TX_TYPES_1D] = { 0 };
Fergus Simpson4063a682017-02-28 16:52:22 -08001306#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001307
1308 switch (cpi->sf.tx_type_search.prune_mode) {
1309 case NO_PRUNE: return 0; break;
1310 case PRUNE_ONE:
Sarah Parker68a26b62016-10-28 13:19:33 -07001311 if ((tx_set >= 0) && !(tx_set_1D[FLIPADST_1D] & tx_set_1D[ADST_1D]))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312 return 0;
1313 return prune_one_for_sby(cpi, bsize, x, xd);
1314 break;
1315#if CONFIG_EXT_TX
1316 case PRUNE_TWO:
Sarah Parker68a26b62016-10-28 13:19:33 -07001317 if ((tx_set >= 0) && !(tx_set_1D[FLIPADST_1D] & tx_set_1D[ADST_1D])) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001318 if (!(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D])) return 0;
1319 return prune_two_for_sby(cpi, bsize, x, xd, 0, 1);
1320 }
Sarah Parker68a26b62016-10-28 13:19:33 -07001321 if ((tx_set >= 0) && !(tx_set_1D[DCT_1D] & tx_set_1D[IDTX_1D]))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001322 return prune_two_for_sby(cpi, bsize, x, xd, 1, 0);
1323 return prune_two_for_sby(cpi, bsize, x, xd, 1, 1);
1324 break;
Fergus Simpson4063a682017-02-28 16:52:22 -08001325#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001326 }
1327 assert(0);
1328 return 0;
1329}
1330
1331static int do_tx_type_search(TX_TYPE tx_type, int prune) {
1332// TODO(sarahparker) implement for non ext tx
1333#if CONFIG_EXT_TX
1334 return !(((prune >> vtx_tab[tx_type]) & 1) |
1335 ((prune >> (htx_tab[tx_type] + 8)) & 1));
1336#else
1337 // temporary to avoid compiler warnings
1338 (void)vtx_tab;
1339 (void)htx_tab;
1340 (void)tx_type;
1341 (void)prune;
1342 return 1;
Fergus Simpson4063a682017-02-28 16:52:22 -08001343#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001344}
1345
Yaowu Xuf883b422016-08-30 14:01:10 -07001346static void model_rd_from_sse(const AV1_COMP *const cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001347 const MACROBLOCKD *const xd, BLOCK_SIZE bsize,
1348 int plane, int64_t sse, int *rate,
1349 int64_t *dist) {
1350 const struct macroblockd_plane *const pd = &xd->plane[plane];
1351 const int dequant_shift =
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001352#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001353 (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd - 5 :
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001354#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001355 3;
1356
1357 // Fast approximate the modelling function.
1358 if (cpi->sf.simple_model_rd_from_var) {
1359 const int64_t square_error = sse;
1360 int quantizer = (pd->dequant[1] >> dequant_shift);
1361
1362 if (quantizer < 120)
1363 *rate = (int)((square_error * (280 - quantizer)) >>
Yaowu Xuf883b422016-08-30 14:01:10 -07001364 (16 - AV1_PROB_COST_SHIFT));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001365 else
1366 *rate = 0;
1367 *dist = (square_error * quantizer) >> 8;
1368 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001369 av1_model_rd_from_var_lapndz(sse, num_pels_log2_lookup[bsize],
1370 pd->dequant[1] >> dequant_shift, rate, dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001371 }
1372
1373 *dist <<= 4;
1374}
1375
Yaowu Xuf883b422016-08-30 14:01:10 -07001376static void model_rd_for_sb(const AV1_COMP *const cpi, BLOCK_SIZE bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001377 MACROBLOCK *x, MACROBLOCKD *xd, int plane_from,
1378 int plane_to, int *out_rate_sum,
1379 int64_t *out_dist_sum, int *skip_txfm_sb,
1380 int64_t *skip_sse_sb) {
1381 // Note our transform coeffs are 8 times an orthogonal transform.
1382 // Hence quantizer step is also 8 times. To get effective quantizer
1383 // we need to divide by 8 before sending to modeling function.
1384 int plane;
1385 const int ref = xd->mi[0]->mbmi.ref_frame[0];
1386
1387 int64_t rate_sum = 0;
1388 int64_t dist_sum = 0;
1389 int64_t total_sse = 0;
1390
1391 x->pred_sse[ref] = 0;
1392
1393 for (plane = plane_from; plane <= plane_to; ++plane) {
1394 struct macroblock_plane *const p = &x->plane[plane];
1395 struct macroblockd_plane *const pd = &xd->plane[plane];
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001396#if CONFIG_CHROMA_SUB8X8
Jingning Han9ce464c2017-02-20 15:36:30 -08001397 const BLOCK_SIZE bs = AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
1398#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001399 const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001400#endif // CONFIG_CHROMA_SUB8X8
Yaowu Xuc27fc142016-08-22 16:08:15 -07001401
1402 unsigned int sse;
1403 int rate;
1404 int64_t dist;
1405
Jingning Han9ce464c2017-02-20 15:36:30 -08001406#if CONFIG_CB4X4
1407 if (x->skip_chroma_rd && plane) continue;
Fergus Simpson4063a682017-02-28 16:52:22 -08001408#endif // CONFIG_CB4X4
Jingning Han9ce464c2017-02-20 15:36:30 -08001409
Yaowu Xuc27fc142016-08-22 16:08:15 -07001410 // TODO(geza): Write direct sse functions that do not compute
1411 // variance as well.
1412 cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
1413 &sse);
1414
1415 if (plane == 0) x->pred_sse[ref] = sse;
1416
1417 total_sse += sse;
1418
1419 model_rd_from_sse(cpi, xd, bs, plane, sse, &rate, &dist);
1420
1421 rate_sum += rate;
1422 dist_sum += dist;
1423 }
1424
1425 *skip_txfm_sb = total_sse == 0;
1426 *skip_sse_sb = total_sse << 4;
1427 *out_rate_sum = (int)rate_sum;
1428 *out_dist_sum = dist_sum;
1429}
1430
Yaowu Xuf883b422016-08-30 14:01:10 -07001431int64_t av1_block_error_c(const tran_low_t *coeff, const tran_low_t *dqcoeff,
1432 intptr_t block_size, int64_t *ssz) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001433 int i;
1434 int64_t error = 0, sqcoeff = 0;
1435
1436 for (i = 0; i < block_size; i++) {
1437 const int diff = coeff[i] - dqcoeff[i];
1438 error += diff * diff;
1439 sqcoeff += coeff[i] * coeff[i];
1440 }
1441
1442 *ssz = sqcoeff;
1443 return error;
1444}
1445
Yaowu Xuf883b422016-08-30 14:01:10 -07001446int64_t av1_block_error_fp_c(const int16_t *coeff, const int16_t *dqcoeff,
1447 int block_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001448 int i;
1449 int64_t error = 0;
1450
1451 for (i = 0; i < block_size; i++) {
1452 const int diff = coeff[i] - dqcoeff[i];
1453 error += diff * diff;
1454 }
1455
1456 return error;
1457}
1458
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001459#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001460int64_t av1_highbd_block_error_c(const tran_low_t *coeff,
1461 const tran_low_t *dqcoeff, intptr_t block_size,
1462 int64_t *ssz, int bd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001463 int i;
1464 int64_t error = 0, sqcoeff = 0;
1465 int shift = 2 * (bd - 8);
1466 int rounding = shift > 0 ? 1 << (shift - 1) : 0;
1467
1468 for (i = 0; i < block_size; i++) {
1469 const int64_t diff = coeff[i] - dqcoeff[i];
1470 error += diff * diff;
1471 sqcoeff += (int64_t)coeff[i] * (int64_t)coeff[i];
1472 }
1473 assert(error >= 0 && sqcoeff >= 0);
1474 error = (error + rounding) >> shift;
1475 sqcoeff = (sqcoeff + rounding) >> shift;
1476
1477 *ssz = sqcoeff;
1478 return error;
1479}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001480#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481
Thomas Daede6ff6af62017-02-03 16:29:24 -08001482#if CONFIG_PVQ
1483// Without PVQ, av1_block_error_c() return two kind of errors,
1484// 1) reconstruction (i.e. decoded) error and
1485// 2) Squared sum of transformed residue (i.e. 'coeff')
1486// However, if PVQ is enabled, coeff does not keep the transformed residue
1487// but instead a transformed original is kept.
1488// Hence, new parameter ref vector (i.e. transformed predicted signal)
1489// is required to derive the residue signal,
1490// i.e. coeff - ref = residue (all transformed).
1491
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001492#if CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -08001493static int64_t av1_highbd_block_error2_c(const tran_low_t *coeff,
1494 const tran_low_t *dqcoeff,
1495 const tran_low_t *ref,
1496 intptr_t block_size, int64_t *ssz,
1497 int bd) {
1498 int64_t error;
1499 int64_t sqcoeff;
1500 int shift = 2 * (bd - 8);
1501 int rounding = shift > 0 ? 1 << (shift - 1) : 0;
1502 // Use the existing sse codes for calculating distortion of decoded signal:
1503 // i.e. (orig - decoded)^2
1504 // For high bit depth, throw away ssz until a 32-bit version of
1505 // av1_block_error_fp is written.
1506 int64_t ssz_trash;
1507 error = av1_block_error(coeff, dqcoeff, block_size, &ssz_trash);
1508 // prediction residue^2 = (orig - ref)^2
1509 sqcoeff = av1_block_error(coeff, ref, block_size, &ssz_trash);
1510 error = (error + rounding) >> shift;
1511 sqcoeff = (sqcoeff + rounding) >> shift;
1512 *ssz = sqcoeff;
1513 return error;
1514}
1515#else
1516// TODO(yushin) : Since 4x4 case does not need ssz, better to refactor into
1517// a separate function that does not do the extra computations for ssz.
1518static int64_t av1_block_error2_c(const tran_low_t *coeff,
1519 const tran_low_t *dqcoeff,
1520 const tran_low_t *ref, intptr_t block_size,
1521 int64_t *ssz) {
1522 int64_t error;
Yushin Choead552a2017-09-25 15:10:25 -07001523 int64_t ssz_trash;
Thomas Daede6ff6af62017-02-03 16:29:24 -08001524 // Use the existing sse codes for calculating distortion of decoded signal:
1525 // i.e. (orig - decoded)^2
Yushin Choead552a2017-09-25 15:10:25 -07001526 error = av1_block_error(coeff, dqcoeff, block_size, &ssz_trash);
Thomas Daede6ff6af62017-02-03 16:29:24 -08001527 // prediction residue^2 = (orig - ref)^2
Yushin Choead552a2017-09-25 15:10:25 -07001528 *ssz = av1_block_error(coeff, ref, block_size, &ssz_trash);
Thomas Daede6ff6af62017-02-03 16:29:24 -08001529 return error;
1530}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001531#endif // CONFIG_HIGHBITDEPTH
Thomas Daede6ff6af62017-02-03 16:29:24 -08001532#endif // CONFIG_PVQ
1533
Jingning Hanab77e732017-02-28 15:20:59 -08001534#if !CONFIG_PVQ || CONFIG_VAR_TX
Angie Chiang47e4b362017-03-24 11:25:10 -07001535#if !CONFIG_LV_MAP
1536static int cost_coeffs(const AV1_COMMON *const cm, MACROBLOCK *x, int plane,
1537 int block, TX_SIZE tx_size, const SCAN_ORDER *scan_order,
1538 const ENTROPY_CONTEXT *a, const ENTROPY_CONTEXT *l,
1539 int use_fast_coef_costing) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001540 MACROBLOCKD *const xd = &x->e_mbd;
1541 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
1542 const struct macroblock_plane *p = &x->plane[plane];
1543 const struct macroblockd_plane *pd = &xd->plane[plane];
1544 const PLANE_TYPE type = pd->plane_type;
1545 const uint16_t *band_count = &band_count_table[tx_size][1];
1546 const int eob = p->eobs[block];
1547 const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
1548 const int tx_size_ctx = txsize_sqr_map[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001549 uint8_t token_cache[MAX_TX_SQUARE];
Angie Chiang77368af2017-03-23 16:22:07 -07001550 int pt = combine_entropy_contexts(*a, *l);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001551 int c, cost;
Angie Chiang77368af2017-03-23 16:22:07 -07001552 const int16_t *scan = scan_order->scan;
1553 const int16_t *nb = scan_order->neighbors;
Thomas Daviesed8e2d22017-01-04 16:42:09 +00001554 const int ref = is_inter_block(mbmi);
hui suc0cf71d2017-07-20 16:38:50 -07001555 int(*head_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] =
1556 x->token_head_costs[tx_size_ctx][type][ref];
1557 int(*tail_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] =
1558 x->token_tail_costs[tx_size_ctx][type][ref];
1559 const int seg_eob = av1_get_tx_eob(&cm->seg, mbmi->segment_id, tx_size);
1560 int eob_val;
Thomas Davies10525752017-03-06 12:10:46 +00001561
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001562#if CONFIG_HIGHBITDEPTH
Alex Converseda3d94f2017-03-15 14:54:29 -07001563 const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001564#else
Alex Converseda3d94f2017-03-15 14:54:29 -07001565 const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, 8);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001566#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001567
1568#if !CONFIG_VAR_TX && !CONFIG_SUPERTX
1569 // Check for consistency of tx_size with mode info
hui su0c6244b2017-07-12 17:11:43 -07001570 assert(tx_size == av1_get_tx_size(plane, xd));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001571#endif // !CONFIG_VAR_TX && !CONFIG_SUPERTX
Angie Chiang22ba7512016-10-20 17:10:33 -07001572 (void)cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001573
1574 if (eob == 0) {
hui suc0cf71d2017-07-20 16:38:50 -07001575 // block zero
1576 cost = (*head_token_costs)[pt][0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001577 } else {
1578 if (use_fast_coef_costing) {
1579 int band_left = *band_count++;
1580
1581 // dc token
1582 int v = qcoeff[0];
1583 int16_t prev_t;
Alex Converseda3d94f2017-03-15 14:54:29 -07001584 cost = av1_get_token_cost(v, &prev_t, cat6_bits);
hui suc0cf71d2017-07-20 16:38:50 -07001585 eob_val = (eob == 1) ? EARLY_EOB : NO_EOB;
1586 cost += av1_get_coeff_token_cost(
1587 prev_t, eob_val, 1, (*head_token_costs)[pt], (*tail_token_costs)[pt]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001588
Yaowu Xuf883b422016-08-30 14:01:10 -07001589 token_cache[0] = av1_pt_energy_class[prev_t];
hui suc0cf71d2017-07-20 16:38:50 -07001590 ++head_token_costs;
1591 ++tail_token_costs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001592
1593 // ac tokens
1594 for (c = 1; c < eob; c++) {
1595 const int rc = scan[c];
1596 int16_t t;
1597
1598 v = qcoeff[rc];
Alex Converseda3d94f2017-03-15 14:54:29 -07001599 cost += av1_get_token_cost(v, &t, cat6_bits);
hui suc0cf71d2017-07-20 16:38:50 -07001600 eob_val =
1601 (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
1602 cost += av1_get_coeff_token_cost(t, eob_val, 0,
1603 (*head_token_costs)[!prev_t],
1604 (*tail_token_costs)[!prev_t]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001605 prev_t = t;
1606 if (!--band_left) {
1607 band_left = *band_count++;
hui suc0cf71d2017-07-20 16:38:50 -07001608 ++head_token_costs;
1609 ++tail_token_costs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001610 }
1611 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001612 } else { // !use_fast_coef_costing
1613 int band_left = *band_count++;
1614
1615 // dc token
1616 int v = qcoeff[0];
1617 int16_t tok;
Alex Converseda3d94f2017-03-15 14:54:29 -07001618 cost = av1_get_token_cost(v, &tok, cat6_bits);
hui suc0cf71d2017-07-20 16:38:50 -07001619 eob_val = (eob == 1) ? EARLY_EOB : NO_EOB;
1620 cost += av1_get_coeff_token_cost(tok, eob_val, 1, (*head_token_costs)[pt],
1621 (*tail_token_costs)[pt]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001622
Yaowu Xuf883b422016-08-30 14:01:10 -07001623 token_cache[0] = av1_pt_energy_class[tok];
hui suc0cf71d2017-07-20 16:38:50 -07001624 ++head_token_costs;
1625 ++tail_token_costs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001626
Yaowu Xuc27fc142016-08-22 16:08:15 -07001627 // ac tokens
1628 for (c = 1; c < eob; c++) {
1629 const int rc = scan[c];
1630
1631 v = qcoeff[rc];
Alex Converseda3d94f2017-03-15 14:54:29 -07001632 cost += av1_get_token_cost(v, &tok, cat6_bits);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001633 pt = get_coef_context(nb, token_cache, c);
hui suc0cf71d2017-07-20 16:38:50 -07001634 eob_val =
1635 (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
1636 cost += av1_get_coeff_token_cost(
1637 tok, eob_val, 0, (*head_token_costs)[pt], (*tail_token_costs)[pt]);
Yaowu Xuf883b422016-08-30 14:01:10 -07001638 token_cache[rc] = av1_pt_energy_class[tok];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001639 if (!--band_left) {
1640 band_left = *band_count++;
hui suc0cf71d2017-07-20 16:38:50 -07001641 ++head_token_costs;
1642 ++tail_token_costs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001643 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001644 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001645 }
1646 }
1647
Yaowu Xuc27fc142016-08-22 16:08:15 -07001648 return cost;
1649}
Angie Chiang47e4b362017-03-24 11:25:10 -07001650#endif // !CONFIG_LV_MAP
1651
Angie Chiang05917872017-04-15 12:28:56 -07001652int av1_cost_coeffs(const AV1_COMP *const cpi, MACROBLOCK *x, int plane,
Jingning Han7eab9ff2017-07-06 10:12:54 -07001653 int blk_row, int blk_col, int block, TX_SIZE tx_size,
1654 const SCAN_ORDER *scan_order, const ENTROPY_CONTEXT *a,
1655 const ENTROPY_CONTEXT *l, int use_fast_coef_costing) {
Angie Chiang3627de22017-08-18 20:15:59 -07001656 const AV1_COMMON *const cm = &cpi->common;
Angie Chiang47e4b362017-03-24 11:25:10 -07001657#if !CONFIG_LV_MAP
Jingning Han7eab9ff2017-07-06 10:12:54 -07001658 (void)blk_row;
1659 (void)blk_col;
Sarah Parker99e7daa2017-08-29 10:30:13 -07001660#if CONFIG_MRC_TX
1661 const MACROBLOCKD *xd = &x->e_mbd;
1662 const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
1663 const TX_TYPE tx_type = av1_get_tx_type(xd->plane[plane].plane_type, xd,
1664 blk_row, blk_col, block, tx_size);
1665 const int is_inter = is_inter_block(mbmi);
1666 if (tx_type == MRC_DCT && ((is_inter && SIGNAL_MRC_MASK_INTER) ||
1667 (!is_inter && SIGNAL_MRC_MASK_INTRA))) {
1668 const int mrc_mask_cost =
1669 av1_cost_color_map(x, plane, block, mbmi->sb_type, tx_size, MRC_MAP);
1670 return cost_coeffs(cm, x, plane, block, tx_size, scan_order, a, l,
1671 use_fast_coef_costing) +
1672 mrc_mask_cost;
1673 }
1674#endif
Angie Chiang47e4b362017-03-24 11:25:10 -07001675 return cost_coeffs(cm, x, plane, block, tx_size, scan_order, a, l,
1676 use_fast_coef_costing);
1677#else // !CONFIG_LV_MAP
1678 (void)scan_order;
1679 (void)use_fast_coef_costing;
1680 const MACROBLOCKD *xd = &x->e_mbd;
1681 const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
1682 const struct macroblockd_plane *pd = &xd->plane[plane];
1683 const BLOCK_SIZE bsize = mbmi->sb_type;
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001684#if CONFIG_CHROMA_SUB8X8
Angie Chiang47e4b362017-03-24 11:25:10 -07001685 const BLOCK_SIZE plane_bsize =
1686 AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001687#elif CONFIG_CB4X4
1688 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Angie Chiang47e4b362017-03-24 11:25:10 -07001689#else // CONFIG_CB4X4
1690 const BLOCK_SIZE plane_bsize =
1691 get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
1692#endif // CONFIG_CB4X4
1693
1694 TXB_CTX txb_ctx;
1695 get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
Angie Chiang3627de22017-08-18 20:15:59 -07001696 return av1_cost_coeffs_txb(cm, x, plane, blk_row, blk_col, block, tx_size,
Jingning Han7eab9ff2017-07-06 10:12:54 -07001697 &txb_ctx);
Angie Chiang47e4b362017-03-24 11:25:10 -07001698#endif // !CONFIG_LV_MAP
1699}
Fergus Simpson0b96b472017-03-07 15:20:28 -08001700#endif // !CONFIG_PVQ || CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07001701
Alex Converse61f37b82017-03-29 15:26:03 -07001702// Get transform block visible dimensions cropped to the MI units.
1703static void get_txb_dimensions(const MACROBLOCKD *xd, int plane,
1704 BLOCK_SIZE plane_bsize, int blk_row, int blk_col,
1705 BLOCK_SIZE tx_bsize, int *width, int *height,
1706 int *visible_width, int *visible_height) {
Yue Chend6bdd462017-07-19 16:05:43 -07001707#if !(CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX))
Alex Converse61f37b82017-03-29 15:26:03 -07001708 assert(tx_bsize <= plane_bsize);
Yue Chend6bdd462017-07-19 16:05:43 -07001709#endif
Alex Converse61f37b82017-03-29 15:26:03 -07001710 int txb_height = block_size_high[tx_bsize];
1711 int txb_width = block_size_wide[tx_bsize];
1712 const int block_height = block_size_high[plane_bsize];
1713 const int block_width = block_size_wide[plane_bsize];
1714 const struct macroblockd_plane *const pd = &xd->plane[plane];
1715 // TODO(aconverse@google.com): Investigate using crop_width/height here rather
1716 // than the MI size
1717 const int block_rows =
1718 (xd->mb_to_bottom_edge >= 0)
1719 ? block_height
1720 : (xd->mb_to_bottom_edge >> (3 + pd->subsampling_y)) + block_height;
1721 const int block_cols =
1722 (xd->mb_to_right_edge >= 0)
1723 ? block_width
1724 : (xd->mb_to_right_edge >> (3 + pd->subsampling_x)) + block_width;
1725 const int tx_unit_size = tx_size_wide_log2[0];
1726 if (width) *width = txb_width;
1727 if (height) *height = txb_height;
1728 *visible_width = clamp(block_cols - (blk_col << tx_unit_size), 0, txb_width);
1729 *visible_height =
1730 clamp(block_rows - (blk_row << tx_unit_size), 0, txb_height);
1731}
1732
Yushin Cho75b01002017-06-21 13:43:57 -07001733// Compute the pixel domain distortion from src and dst on all visible 4x4s in
1734// the
Alex Converse61f37b82017-03-29 15:26:03 -07001735// transform block.
Yushin Cho75b01002017-06-21 13:43:57 -07001736static unsigned pixel_dist(const AV1_COMP *const cpi, const MACROBLOCK *x,
1737 int plane, const uint8_t *src, const int src_stride,
1738 const uint8_t *dst, const int dst_stride,
1739 int blk_row, int blk_col,
1740 const BLOCK_SIZE plane_bsize,
1741 const BLOCK_SIZE tx_bsize) {
Alex Converse61f37b82017-03-29 15:26:03 -07001742 int txb_rows, txb_cols, visible_rows, visible_cols;
Yushin Cho75b01002017-06-21 13:43:57 -07001743 const MACROBLOCKD *xd = &x->e_mbd;
Yushin Chob7b60c52017-07-14 16:18:52 -07001744
Alex Converse61f37b82017-03-29 15:26:03 -07001745 get_txb_dimensions(xd, plane, plane_bsize, blk_row, blk_col, tx_bsize,
1746 &txb_cols, &txb_rows, &visible_cols, &visible_rows);
1747 assert(visible_rows > 0);
1748 assert(visible_cols > 0);
Yushin Cho75b01002017-06-21 13:43:57 -07001749
Yushin Chob7b60c52017-07-14 16:18:52 -07001750#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -07001751 if (x->using_dist_8x8 && plane == 0 && txb_cols >= 8 && txb_rows >= 8)
Yushin Chofcddadf2017-08-30 13:49:38 -07001752 return (unsigned)av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride,
1753 tx_bsize, txb_cols, txb_rows, visible_cols,
1754 visible_rows, x->qindex);
Yushin Chob7b60c52017-07-14 16:18:52 -07001755#endif // CONFIG_DIST_8X8
Yushin Cho75b01002017-06-21 13:43:57 -07001756
Yushin Cho2f025aa2017-09-28 17:39:21 -07001757 unsigned sse = pixel_dist_visible_only(cpi, x, src, src_stride, dst,
1758 dst_stride, tx_bsize, txb_rows,
1759 txb_cols, visible_rows, visible_cols);
1760
Alex Converse61f37b82017-03-29 15:26:03 -07001761 return sse;
1762}
1763
Yushin Cho75b01002017-06-21 13:43:57 -07001764// Compute the pixel domain distortion from diff on all visible 4x4s in the
1765// transform block.
1766static int64_t pixel_diff_dist(const MACROBLOCK *x, int plane,
1767 const int16_t *diff, const int diff_stride,
1768 int blk_row, int blk_col,
1769 const BLOCK_SIZE plane_bsize,
1770 const BLOCK_SIZE tx_bsize) {
Alex Converse61f37b82017-03-29 15:26:03 -07001771 int visible_rows, visible_cols;
Yushin Cho75b01002017-06-21 13:43:57 -07001772 const MACROBLOCKD *xd = &x->e_mbd;
Yushin Chob7b60c52017-07-14 16:18:52 -07001773#if CONFIG_DIST_8X8
Yushin Cho75b01002017-06-21 13:43:57 -07001774 int txb_height = block_size_high[tx_bsize];
1775 int txb_width = block_size_wide[tx_bsize];
1776 const int src_stride = x->plane[plane].src.stride;
1777 const int src_idx = (blk_row * src_stride + blk_col) << tx_size_wide_log2[0];
1778 const uint8_t *src = &x->plane[plane].src.buf[src_idx];
Yushin Cho75b01002017-06-21 13:43:57 -07001779#endif
1780
Alex Converse61f37b82017-03-29 15:26:03 -07001781 get_txb_dimensions(xd, plane, plane_bsize, blk_row, blk_col, tx_bsize, NULL,
1782 NULL, &visible_cols, &visible_rows);
Yushin Cho75b01002017-06-21 13:43:57 -07001783
Yushin Chob7b60c52017-07-14 16:18:52 -07001784#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -07001785 if (x->using_dist_8x8 && plane == 0 && txb_width >= 8 && txb_height >= 8)
Yushin Choe30a47c2017-08-15 13:08:30 -07001786 return av1_dist_8x8_diff(x, src, src_stride, diff, diff_stride, txb_width,
Yushin Chob7b60c52017-07-14 16:18:52 -07001787 txb_height, visible_cols, visible_rows, x->qindex);
Yushin Cho75b01002017-06-21 13:43:57 -07001788 else
1789#endif
1790 return aom_sum_squares_2d_i16(diff, diff_stride, visible_cols,
1791 visible_rows);
Alex Converse61f37b82017-03-29 15:26:03 -07001792}
1793
hui sud9a812b2017-07-06 14:34:37 -07001794int av1_count_colors(const uint8_t *src, int stride, int rows, int cols) {
1795 int val_count[256];
1796 memset(val_count, 0, sizeof(val_count));
1797 for (int r = 0; r < rows; ++r) {
1798 for (int c = 0; c < cols; ++c) {
1799 ++val_count[src[r * stride + c]];
1800 }
1801 }
1802 int n = 0;
1803 for (int i = 0; i < 256; ++i) {
1804 if (val_count[i]) ++n;
1805 }
1806 return n;
1807}
1808
1809#if CONFIG_HIGHBITDEPTH
1810int av1_count_colors_highbd(const uint8_t *src8, int stride, int rows, int cols,
1811 int bit_depth) {
1812 assert(bit_depth <= 12);
1813 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1814 int val_count[1 << 12];
1815 memset(val_count, 0, (1 << 12) * sizeof(val_count[0]));
1816 for (int r = 0; r < rows; ++r) {
1817 for (int c = 0; c < cols; ++c) {
1818 ++val_count[src[r * stride + c]];
1819 }
1820 }
1821 int n = 0;
1822 for (int i = 0; i < (1 << bit_depth); ++i) {
1823 if (val_count[i]) ++n;
1824 }
1825 return n;
1826}
1827#endif // CONFIG_HIGHBITDEPTH
hui sud9a812b2017-07-06 14:34:37 -07001828
Angie Chiang808d8592017-04-06 18:36:55 -07001829void av1_dist_block(const AV1_COMP *cpi, MACROBLOCK *x, int plane,
1830 BLOCK_SIZE plane_bsize, int block, int blk_row, int blk_col,
1831 TX_SIZE tx_size, int64_t *out_dist, int64_t *out_sse,
1832 OUTPUT_STATUS output_status) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001833 MACROBLOCKD *const xd = &x->e_mbd;
1834 const struct macroblock_plane *const p = &x->plane[plane];
Yushin Chob7b60c52017-07-14 16:18:52 -07001835#if CONFIG_DIST_8X8
Yushin Cho4483e3d2017-04-18 19:41:20 -07001836 struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Chob7b60c52017-07-14 16:18:52 -07001837#else // CONFIG_DIST_8X8
Yushin Cho4483e3d2017-04-18 19:41:20 -07001838 const struct macroblockd_plane *const pd = &xd->plane[plane];
Yushin Chob7b60c52017-07-14 16:18:52 -07001839#endif // CONFIG_DIST_8X8
Yushin Cho7a428ba2017-01-12 16:28:49 -08001840
Yushin Cho55104332017-08-14 16:15:43 -07001841 if (cpi->sf.use_transform_domain_distortion
1842#if CONFIG_DIST_8X8
1843 && !x->using_dist_8x8
1844#endif
1845 ) {
hui sud2f12ba2017-04-12 10:08:43 -07001846 // Transform domain distortion computation is more efficient as it does
Yaowu Xuc27fc142016-08-22 16:08:15 -07001847 // not involve an inverse transform, but it is less accurate.
Jingning Hanb9c57272016-10-25 10:15:39 -07001848 const int buffer_length = tx_size_2d[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001849 int64_t this_sse;
Jingning Hanff705452017-04-27 11:32:15 -07001850 int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001851 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
1852 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001853#if CONFIG_PVQ
Yaowu Xud6ea71c2016-11-07 10:24:14 -08001854 tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block);
Thomas Daede6ff6af62017-02-03 16:29:24 -08001855
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001856#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001857 const int bd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? xd->bd : 8;
Thomas Daede6ff6af62017-02-03 16:29:24 -08001858 *out_dist = av1_highbd_block_error2_c(coeff, dqcoeff, ref_coeff,
1859 buffer_length, &this_sse, bd) >>
1860 shift;
1861#else
1862 *out_dist = av1_block_error2_c(coeff, dqcoeff, ref_coeff, buffer_length,
1863 &this_sse) >>
1864 shift;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001865#endif // CONFIG_HIGHBITDEPTH
Yi Luod61e6082017-05-26 16:14:39 -07001866#else // !CONFIG_PVQ
1867#if CONFIG_HIGHBITDEPTH
1868 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
1869 *out_dist = av1_highbd_block_error(coeff, dqcoeff, buffer_length,
1870 &this_sse, xd->bd) >>
1871 shift;
1872 else
1873#endif
1874 *out_dist =
1875 av1_block_error(coeff, dqcoeff, buffer_length, &this_sse) >> shift;
Thomas Daede6ff6af62017-02-03 16:29:24 -08001876#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07001877 *out_sse = this_sse >> shift;
1878 } else {
1879 const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
Yushin Chob7b60c52017-07-14 16:18:52 -07001880#if !CONFIG_PVQ || CONFIG_DIST_8X8
Jingning Hanb9c57272016-10-25 10:15:39 -07001881 const int bsw = block_size_wide[tx_bsize];
1882 const int bsh = block_size_high[tx_bsize];
Angie Chiang50910f62017-04-03 12:31:34 -07001883#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001884 const int src_stride = x->plane[plane].src.stride;
1885 const int dst_stride = xd->plane[plane].dst.stride;
Jingning Hanb9c57272016-10-25 10:15:39 -07001886 // Scale the transform block index to pixel unit.
1887 const int src_idx = (blk_row * src_stride + blk_col)
1888 << tx_size_wide_log2[0];
1889 const int dst_idx = (blk_row * dst_stride + blk_col)
1890 << tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001891 const uint8_t *src = &x->plane[plane].src.buf[src_idx];
1892 const uint8_t *dst = &xd->plane[plane].dst.buf[dst_idx];
1893 const tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
1894 const uint16_t eob = p->eobs[block];
1895
Yaowu Xuc27fc142016-08-22 16:08:15 -07001896 assert(cpi != NULL);
Jingning Hanb9c57272016-10-25 10:15:39 -07001897 assert(tx_size_wide_log2[0] == tx_size_high_log2[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001898
Angie Chiangc0cf6c02017-04-04 13:47:27 -07001899 {
1900 const int diff_stride = block_size_wide[plane_bsize];
1901 const int diff_idx = (blk_row * diff_stride + blk_col)
1902 << tx_size_wide_log2[0];
1903 const int16_t *diff = &p->src_diff[diff_idx];
Yushin Cho75b01002017-06-21 13:43:57 -07001904 *out_sse = pixel_diff_dist(x, plane, diff, diff_stride, blk_row, blk_col,
1905 plane_bsize, tx_bsize);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001906#if CONFIG_HIGHBITDEPTH
hui sub1cc1f92017-04-11 17:41:29 -07001907 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
Alex Conversef323f012017-04-24 09:26:33 -07001908 *out_sse = ROUND_POWER_OF_TWO(*out_sse, (xd->bd - 8) * 2);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001909#endif // CONFIG_HIGHBITDEPTH
Angie Chiangc0cf6c02017-04-04 13:47:27 -07001910 }
Alex Conversef323f012017-04-24 09:26:33 -07001911 *out_sse *= 16;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001912
1913 if (eob) {
Angie Chiang228cc182017-04-07 15:22:16 -07001914 if (output_status == OUTPUT_HAS_DECODED_PIXELS) {
Yushin Cho75b01002017-06-21 13:43:57 -07001915 *out_dist = pixel_dist(cpi, x, plane, src, src_stride, dst, dst_stride,
1916 blk_row, blk_col, plane_bsize, tx_bsize);
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001917 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001918#if CONFIG_HIGHBITDEPTH
Jingning Han6a9dbef2017-04-10 10:25:14 -07001919 uint8_t *recon;
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001920 DECLARE_ALIGNED(16, uint16_t, recon16[MAX_TX_SQUARE]);
Jingning Han6a9dbef2017-04-10 10:25:14 -07001921
1922 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
1923 recon = CONVERT_TO_BYTEPTR(recon16);
1924 else
1925 recon = (uint8_t *)recon16;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001926#else
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001927 DECLARE_ALIGNED(16, uint8_t, recon[MAX_TX_SQUARE]);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001928#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001929
Angie Chiang50910f62017-04-03 12:31:34 -07001930#if !CONFIG_PVQ
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001931#if CONFIG_HIGHBITDEPTH
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001932 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1933 aom_highbd_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0,
1934 NULL, 0, bsw, bsh, xd->bd);
1935 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001936#endif // CONFIG_HIGHBITDEPTH
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001937 aom_convolve_copy(dst, dst_stride, recon, MAX_TX_SIZE, NULL, 0, NULL,
1938 0, bsw, bsh);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001939#if CONFIG_HIGHBITDEPTH
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001940 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001941#endif // CONFIG_HIGHBITDEPTH
Angie Chiangc0cf6c02017-04-04 13:47:27 -07001942#else
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001943 (void)dst;
Angie Chiang50910f62017-04-03 12:31:34 -07001944#endif // !CONFIG_PVQ
Angie Chiang41fffae2017-04-03 10:33:18 -07001945
Sarah Parker99e7daa2017-08-29 10:30:13 -07001946#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
1947 uint8_t *mrc_mask = BLOCK_OFFSET(xd->mrc_mask, block);
1948#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001949 const PLANE_TYPE plane_type = get_plane_type(plane);
Jingning Han19b5c8f2017-07-06 15:10:12 -07001950 TX_TYPE tx_type =
1951 av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
Lester Lu708c1ec2017-06-14 14:54:49 -07001952 av1_inverse_transform_block(xd, dqcoeff,
1953#if CONFIG_LGT
1954 xd->mi[0]->mbmi.mode,
1955#endif
Sarah Parker99e7daa2017-08-29 10:30:13 -07001956#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
1957 mrc_mask,
1958#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Lester Lu708c1ec2017-06-14 14:54:49 -07001959 tx_type, tx_size, recon, MAX_TX_SIZE, eob);
Angie Chiang41fffae2017-04-03 10:33:18 -07001960
Yushin Chob7b60c52017-07-14 16:18:52 -07001961#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -07001962 if (x->using_dist_8x8 && plane == 0 && (bsw < 8 || bsh < 8)) {
Yushin Cho75b01002017-06-21 13:43:57 -07001963 // Save decoded pixels for inter block in pd->pred to avoid
1964 // block_8x8_rd_txfm_daala_dist() need to produce them
1965 // by calling av1_inverse_transform_block() again.
1966 const int pred_stride = block_size_wide[plane_bsize];
1967 const int pred_idx = (blk_row * pred_stride + blk_col)
1968 << tx_size_wide_log2[0];
1969 int16_t *pred = &pd->pred[pred_idx];
1970 int i, j;
Yushin Cho4483e3d2017-04-18 19:41:20 -07001971
Yushin Cho8ab875d2017-06-23 14:47:21 -07001972#if CONFIG_HIGHBITDEPTH
1973 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1974 for (j = 0; j < bsh; j++)
1975 for (i = 0; i < bsw; i++)
1976 pred[j * pred_stride + i] =
1977 CONVERT_TO_SHORTPTR(recon)[j * MAX_TX_SIZE + i];
1978 } else {
1979#endif
1980 for (j = 0; j < bsh; j++)
1981 for (i = 0; i < bsw; i++)
1982 pred[j * pred_stride + i] = recon[j * MAX_TX_SIZE + i];
1983#if CONFIG_HIGHBITDEPTH
1984 }
1985#endif // CONFIG_HIGHBITDEPTH
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001986 }
Yushin Chob7b60c52017-07-14 16:18:52 -07001987#endif // CONFIG_DIST_8X8
Yushin Cho75b01002017-06-21 13:43:57 -07001988 *out_dist =
1989 pixel_dist(cpi, x, plane, src, src_stride, recon, MAX_TX_SIZE,
1990 blk_row, blk_col, plane_bsize, tx_bsize);
Angie Chiang8f6ddec2017-04-04 17:07:00 -07001991 }
Alex Conversef323f012017-04-24 09:26:33 -07001992 *out_dist *= 16;
1993 } else {
1994 *out_dist = *out_sse;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001995 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001996 }
1997}
1998
Yaowu Xuc27fc142016-08-22 16:08:15 -07001999static void block_rd_txfm(int plane, int block, int blk_row, int blk_col,
2000 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
2001 struct rdcost_block_args *args = arg;
2002 MACROBLOCK *const x = args->x;
2003 MACROBLOCKD *const xd = &x->e_mbd;
Urvang Joshi0d1e4ff2017-04-27 16:17:25 -07002004 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Angie Chiange50f3ec2017-04-10 15:50:33 -07002005 const AV1_COMP *cpi = args->cpi;
Angie Chiang65a39bb2017-04-11 16:50:04 -07002006 ENTROPY_CONTEXT *a = args->t_above + blk_col;
2007 ENTROPY_CONTEXT *l = args->t_left + blk_row;
Angie Chiang18ad8942017-04-11 12:37:07 -07002008 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002009 int64_t rd1, rd2, rd;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002010 RD_STATS this_rd_stats;
Yushin Choc00769a2017-09-14 14:44:30 -07002011
2012#if CONFIG_DIST_8X8
2013 // If sub8x8 tx, 8x8 or larger partition, and luma channel,
2014 // dist-8x8 disables early skip, because the distortion metrics for
2015 // sub8x8 tx (MSE) and reference distortion from 8x8 or larger partition
2016 // (new distortion metric) are different.
2017 // Exception is: dist-8x8 is enabled but still MSE is used,
2018 // i.e. "--tune=" encoder option is not used.
2019 int disable_early_skip =
2020 x->using_dist_8x8 && plane == 0 && plane_bsize >= BLOCK_8X8 &&
2021 (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4) &&
2022 x->tune_metric != AOM_TUNE_PSNR;
Yushin Choa4817a62017-07-27 13:09:43 -07002023#endif // CONFIG_DIST_8X8
Yushin Cho6341f5c2017-03-24 14:36:28 -07002024
Jingning Hane65436b2017-06-14 09:26:11 -07002025#if !CONFIG_SUPERTX && !CONFIG_VAR_TX
hui su0c6244b2017-07-12 17:11:43 -07002026 assert(tx_size == av1_get_tx_size(plane, xd));
Yue Chen8e689e42017-06-02 10:56:10 -07002027#endif // !CONFIG_SUPERTX
Angie Chiangbc2288c2017-04-09 15:41:17 -07002028
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002029 av1_init_rd_stats(&this_rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002030
2031 if (args->exit_early) return;
2032
2033 if (!is_inter_block(mbmi)) {
David Barker761b1ac2017-09-25 11:23:03 +01002034 av1_predict_intra_block_facade(cm, xd, plane, block, blk_col, blk_row,
2035 tx_size);
Angie Chiang62e54cd2017-04-06 10:45:56 -07002036 av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
2037 }
2038
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002039#if !CONFIG_TXK_SEL
Angie Chiang62e54cd2017-04-06 10:45:56 -07002040 // full forward transform and quantization
Urvang Joshi0d1e4ff2017-04-27 16:17:25 -07002041 const int coeff_ctx = combine_entropy_contexts(*a, *l);
Sarah Parker345366a2017-06-15 12:13:01 -07002042#if DISABLE_TRELLISQ_SEARCH
2043 av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
2044 coeff_ctx, AV1_XFORM_QUANT_B);
2045#else
Angie Chiang62e54cd2017-04-06 10:45:56 -07002046 av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
2047 coeff_ctx, AV1_XFORM_QUANT_FP);
Jingning Han3bce7542017-07-25 10:53:57 -07002048
2049 const int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2;
2050 tran_low_t *const coeff = BLOCK_OFFSET(x->plane[plane].coeff, block);
2051 tran_low_t *const dqcoeff = BLOCK_OFFSET(xd->plane[plane].dqcoeff, block);
2052 const int buffer_length = tx_size_2d[tx_size];
2053 int64_t tmp_dist;
2054 int64_t tmp;
2055#if CONFIG_HIGHBITDEPTH
2056 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
2057 tmp_dist =
2058 av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp, xd->bd) >>
2059 shift;
2060 else
2061#endif
2062 tmp_dist = av1_block_error(coeff, dqcoeff, buffer_length, &tmp) >> shift;
2063
Yushin Choa4817a62017-07-27 13:09:43 -07002064 if (
2065#if CONFIG_DIST_8X8
Yushin Choc00769a2017-09-14 14:44:30 -07002066 disable_early_skip ||
Yushin Choa4817a62017-07-27 13:09:43 -07002067#endif
2068 RDCOST(x->rdmult, 0, tmp_dist) + args->this_rd < args->best_rd) {
Jingning Han3bce7542017-07-25 10:53:57 -07002069 av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size,
Angie Chiang25645b72017-09-24 14:28:49 -07002070 a, l, 1);
Jingning Han3bce7542017-07-25 10:53:57 -07002071 } else {
2072 args->exit_early = 1;
2073 return;
2074 }
Sarah Parker345366a2017-06-15 12:13:01 -07002075#endif // DISABLE_TRELLISQ_SEARCH
Angie Chiang62e54cd2017-04-06 10:45:56 -07002076
Sarah Parkerc5ccd4c2017-08-03 11:27:50 -07002077#if CONFIG_MRC_TX
2078 if (mbmi->tx_type == MRC_DCT && !mbmi->valid_mrc_mask) {
2079 args->exit_early = 1;
2080 return;
2081 }
2082#endif // CONFIG_MRC_TX
2083
Angie Chiang62e54cd2017-04-06 10:45:56 -07002084 if (!is_inter_block(mbmi)) {
2085 struct macroblock_plane *const p = &x->plane[plane];
Angie Chiangbc2288c2017-04-09 15:41:17 -07002086 av1_inverse_transform_block_facade(xd, plane, block, blk_row, blk_col,
2087 p->eobs[block]);
Angie Chiang808d8592017-04-06 18:36:55 -07002088 av1_dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col,
2089 tx_size, &this_rd_stats.dist, &this_rd_stats.sse,
2090 OUTPUT_HAS_DECODED_PIXELS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002091 } else {
Angie Chiang808d8592017-04-06 18:36:55 -07002092 av1_dist_block(args->cpi, x, plane, plane_bsize, block, blk_row, blk_col,
2093 tx_size, &this_rd_stats.dist, &this_rd_stats.sse,
2094 OUTPUT_HAS_PREDICTED_PIXELS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002095 }
Luc Trudeauaa94baa2017-04-27 10:52:21 -04002096#if CONFIG_CFL
Luc Trudeaufcca37a2017-08-14 15:05:07 -04002097 if (plane == AOM_PLANE_Y && xd->cfl->store_y) {
Luc Trudeaub05eeae2017-08-18 15:14:30 -04002098#if CONFIG_CHROMA_SUB8X8
2099 assert(!is_inter_block(mbmi) || plane_bsize < BLOCK_8X8);
2100#else
2101 assert(!is_inter_block(mbmi));
2102#endif // CONFIG_CHROMA_SUB8X8
2103 cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize);
Luc Trudeauaa94baa2017-04-27 10:52:21 -04002104 }
Luc Trudeaub05eeae2017-08-18 15:14:30 -04002105#endif // CONFIG_CFL
Urvang Joshi70006e42017-06-14 16:08:55 -07002106 rd = RDCOST(x->rdmult, 0, this_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002107 if (args->this_rd + rd > args->best_rd) {
2108 args->exit_early = 1;
2109 return;
2110 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07002111#if !CONFIG_PVQ
Urvang Joshi0d1e4ff2017-04-27 16:17:25 -07002112 const PLANE_TYPE plane_type = get_plane_type(plane);
Jingning Han19b5c8f2017-07-06 15:10:12 -07002113 const TX_TYPE tx_type =
2114 av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
Angie Chiangbd99b382017-06-20 15:11:16 -07002115 const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, mbmi);
Angie Chiang05917872017-04-15 12:28:56 -07002116 this_rd_stats.rate =
Jingning Han7eab9ff2017-07-06 10:12:54 -07002117 av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block, tx_size,
2118 scan_order, a, l, args->use_fast_coef_costing);
Angie Chiang65a39bb2017-04-11 16:50:04 -07002119#else // !CONFIG_PVQ
2120 this_rd_stats.rate = x->rate;
2121#endif // !CONFIG_PVQ
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002122#else // !CONFIG_TXK_SEL
Angie Chiang65a39bb2017-04-11 16:50:04 -07002123 av1_search_txk_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize,
2124 tx_size, a, l, args->use_fast_coef_costing,
2125 &this_rd_stats);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002126#endif // !CONFIG_TXK_SEL
Angie Chiang65a39bb2017-04-11 16:50:04 -07002127
2128#if !CONFIG_PVQ
Angie Chiang3963d632016-11-10 18:41:40 -08002129#if CONFIG_RD_DEBUG
Angie Chiange94556b2016-11-09 10:59:30 -08002130 av1_update_txb_coeff_cost(&this_rd_stats, plane, tx_size, blk_row, blk_col,
2131 this_rd_stats.rate);
Fergus Simpson4063a682017-02-28 16:52:22 -08002132#endif // CONFIG_RD_DEBUG
Yushin Cho6341f5c2017-03-24 14:36:28 -07002133 av1_set_txb_context(x, plane, block, tx_size, a, l);
Fergus Simpson4063a682017-02-28 16:52:22 -08002134#endif // !CONFIG_PVQ
Angie Chiangb3a12b52017-03-23 14:53:10 -07002135
Urvang Joshi70006e42017-06-14 16:08:55 -07002136 rd1 = RDCOST(x->rdmult, this_rd_stats.rate, this_rd_stats.dist);
2137 rd2 = RDCOST(x->rdmult, 0, this_rd_stats.sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002138
2139 // TODO(jingning): temporarily enabled only for luma component
Yaowu Xuf883b422016-08-30 14:01:10 -07002140 rd = AOMMIN(rd1, rd2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002141
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002142#if !CONFIG_PVQ
2143 this_rd_stats.skip &= !x->plane[plane].eobs[block];
2144#else
2145 this_rd_stats.skip &= x->pvq_skip[plane];
Fergus Simpson4063a682017-02-28 16:52:22 -08002146#endif // !CONFIG_PVQ
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002147 av1_merge_rd_stats(&args->rd_stats, &this_rd_stats);
Yushin Cho7a428ba2017-01-12 16:28:49 -08002148
Yaowu Xuc27fc142016-08-22 16:08:15 -07002149 args->this_rd += rd;
2150
Yushin Chob7b60c52017-07-14 16:18:52 -07002151#if CONFIG_DIST_8X8
Yushin Choc00769a2017-09-14 14:44:30 -07002152 if (!disable_early_skip)
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002153#endif
2154 if (args->this_rd > args->best_rd) {
2155 args->exit_early = 1;
2156 return;
2157 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002158}
2159
Yushin Chob7b60c52017-07-14 16:18:52 -07002160#if CONFIG_DIST_8X8
2161static void dist_8x8_sub8x8_txfm_rd(const AV1_COMP *const cpi, MACROBLOCK *x,
2162 BLOCK_SIZE bsize,
2163 struct rdcost_block_args *args) {
Yushin Cho7a428ba2017-01-12 16:28:49 -08002164 MACROBLOCKD *const xd = &x->e_mbd;
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002165 const struct macroblockd_plane *const pd = &xd->plane[0];
2166 const struct macroblock_plane *const p = &x->plane[0];
Yushin Cho4483e3d2017-04-18 19:41:20 -07002167 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002168 const int src_stride = p->src.stride;
2169 const int dst_stride = pd->dst.stride;
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002170 const uint8_t *src = &p->src.buf[0];
2171 const uint8_t *dst = &pd->dst.buf[0];
2172 const int16_t *pred = &pd->pred[0];
Yushin Cho2f025aa2017-09-28 17:39:21 -07002173 int bw = block_size_wide[bsize];
2174 int bh = block_size_high[bsize];
2175 int visible_w = bw;
2176 int visible_h = bh;
Yushin Cho7a428ba2017-01-12 16:28:49 -08002177
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002178 int i, j;
2179 int64_t rd, rd1, rd2;
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002180 unsigned int tmp1, tmp2;
2181 int qindex = x->qindex;
Yushin Cho7a428ba2017-01-12 16:28:49 -08002182
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002183 assert((bw & 0x07) == 0);
2184 assert((bh & 0x07) == 0);
Yushin Cho7a428ba2017-01-12 16:28:49 -08002185
Yushin Cho2f025aa2017-09-28 17:39:21 -07002186 get_txb_dimensions(xd, 0, bsize, 0, 0, bsize, &bw, &bh, &visible_w,
2187 &visible_h);
2188
Yushin Cho8ab875d2017-06-23 14:47:21 -07002189#if CONFIG_HIGHBITDEPTH
2190 uint8_t *pred8;
2191 DECLARE_ALIGNED(16, uint16_t, pred16[MAX_TX_SQUARE]);
Yushin Cho7a428ba2017-01-12 16:28:49 -08002192
Yushin Cho8ab875d2017-06-23 14:47:21 -07002193 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
2194 pred8 = CONVERT_TO_BYTEPTR(pred16);
2195 else
2196 pred8 = (uint8_t *)pred16;
2197#else
2198 DECLARE_ALIGNED(16, uint8_t, pred8[MAX_TX_SQUARE]);
2199#endif // CONFIG_HIGHBITDEPTH
Yushin Cho7a428ba2017-01-12 16:28:49 -08002200
Yushin Cho8ab875d2017-06-23 14:47:21 -07002201#if CONFIG_HIGHBITDEPTH
2202 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
2203 for (j = 0; j < bh; j++)
2204 for (i = 0; i < bw; i++)
2205 CONVERT_TO_SHORTPTR(pred8)[j * bw + i] = pred[j * bw + i];
2206 } else {
2207#endif
2208 for (j = 0; j < bh; j++)
Yaowu Xu7a471702017-09-29 08:38:37 -07002209 for (i = 0; i < bw; i++) pred8[j * bw + i] = (uint8_t)pred[j * bw + i];
Yushin Cho8ab875d2017-06-23 14:47:21 -07002210#if CONFIG_HIGHBITDEPTH
2211 }
2212#endif // CONFIG_HIGHBITDEPTH
2213
Yushin Chofcddadf2017-08-30 13:49:38 -07002214 tmp1 = (unsigned)av1_dist_8x8(cpi, x, src, src_stride, pred8, bw, bsize, bw,
Yushin Cho2f025aa2017-09-28 17:39:21 -07002215 bh, visible_w, visible_h, qindex);
Yushin Chofcddadf2017-08-30 13:49:38 -07002216 tmp2 = (unsigned)av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride, bsize,
Yushin Cho2f025aa2017-09-28 17:39:21 -07002217 bw, bh, visible_w, visible_h, qindex);
Yushin Cho4483e3d2017-04-18 19:41:20 -07002218
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002219 if (!is_inter_block(mbmi)) {
Yushin Cho2f025aa2017-09-28 17:39:21 -07002220 if (x->tune_metric == AOM_TUNE_PSNR) {
2221 assert(args->rd_stats.sse == tmp1 * 16);
2222 assert(args->rd_stats.dist == tmp2 * 16);
2223 }
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002224 args->rd_stats.sse = (int64_t)tmp1 * 16;
2225 args->rd_stats.dist = (int64_t)tmp2 * 16;
2226 } else {
2227 // For inter mode, the decoded pixels are provided in pd->pred,
2228 // while the predicted pixels are in dst.
Yushin Cho2f025aa2017-09-28 17:39:21 -07002229 if (x->tune_metric == AOM_TUNE_PSNR) {
2230 assert(args->rd_stats.sse == tmp2 * 16);
2231 assert(args->rd_stats.dist == tmp1 * 16);
2232 }
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002233 args->rd_stats.sse = (int64_t)tmp2 * 16;
2234 args->rd_stats.dist = (int64_t)tmp1 * 16;
Yushin Cho7a428ba2017-01-12 16:28:49 -08002235 }
2236
Urvang Joshi70006e42017-06-14 16:08:55 -07002237 rd1 = RDCOST(x->rdmult, args->rd_stats.rate, args->rd_stats.dist);
2238 rd2 = RDCOST(x->rdmult, 0, args->rd_stats.sse);
Yushin Cho7a428ba2017-01-12 16:28:49 -08002239 rd = AOMMIN(rd1, rd2);
2240
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002241 args->rd_stats.rdcost = rd;
2242 args->this_rd = rd;
Yushin Cho04eb9592017-06-21 17:35:06 -07002243
2244 if (args->this_rd > args->best_rd) args->exit_early = 1;
Yushin Cho7a428ba2017-01-12 16:28:49 -08002245}
Yushin Chob7b60c52017-07-14 16:18:52 -07002246#endif // CONFIG_DIST_8X8
Yushin Cho7a428ba2017-01-12 16:28:49 -08002247
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002248static void txfm_rd_in_plane(MACROBLOCK *x, const AV1_COMP *cpi,
2249 RD_STATS *rd_stats, int64_t ref_best_rd, int plane,
2250 BLOCK_SIZE bsize, TX_SIZE tx_size,
2251 int use_fast_coef_casting) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002252 MACROBLOCKD *const xd = &x->e_mbd;
2253 const struct macroblockd_plane *const pd = &xd->plane[plane];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002254 struct rdcost_block_args args;
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 av1_zero(args);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256 args.x = x;
2257 args.cpi = cpi;
2258 args.best_rd = ref_best_rd;
2259 args.use_fast_coef_costing = use_fast_coef_casting;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002260 av1_init_rd_stats(&args.rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002261
2262 if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size;
2263
Yaowu Xuf883b422016-08-30 14:01:10 -07002264 av1_get_entropy_contexts(bsize, tx_size, pd, args.t_above, args.t_left);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002266 av1_foreach_transformed_block_in_plane(xd, bsize, plane, block_rd_txfm,
2267 &args);
Yushin Chob7b60c52017-07-14 16:18:52 -07002268#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -07002269 if (x->using_dist_8x8 && !args.exit_early && plane == 0 &&
2270 bsize >= BLOCK_8X8 &&
Yushin Cho7a428ba2017-01-12 16:28:49 -08002271 (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4))
Yushin Chob7b60c52017-07-14 16:18:52 -07002272 dist_8x8_sub8x8_txfm_rd(cpi, x, bsize, &args);
Yushin Cho30a2c5f2017-06-07 14:18:54 -07002273#endif
Yushin Cho7a428ba2017-01-12 16:28:49 -08002274
Yaowu Xuc27fc142016-08-22 16:08:15 -07002275 if (args.exit_early) {
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002276 av1_invalid_rd_stats(rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002277 } else {
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002278 *rd_stats = args.rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002279 }
2280}
2281
2282#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002283void av1_txfm_rd_in_plane_supertx(MACROBLOCK *x, const AV1_COMP *cpi, int *rate,
2284 int64_t *distortion, int *skippable,
2285 int64_t *sse, int64_t ref_best_rd, int plane,
2286 BLOCK_SIZE bsize, TX_SIZE tx_size,
2287 int use_fast_coef_casting) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002288 MACROBLOCKD *const xd = &x->e_mbd;
2289 const struct macroblockd_plane *const pd = &xd->plane[plane];
2290 struct rdcost_block_args args;
Yaowu Xuf883b422016-08-30 14:01:10 -07002291 av1_zero(args);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002292 args.cpi = cpi;
2293 args.x = x;
2294 args.best_rd = ref_best_rd;
2295 args.use_fast_coef_costing = use_fast_coef_casting;
2296
2297#if CONFIG_EXT_TX
2298 assert(tx_size < TX_SIZES);
2299#endif // CONFIG_EXT_TX
2300
2301 if (plane == 0) xd->mi[0]->mbmi.tx_size = tx_size;
2302
Yaowu Xuf883b422016-08-30 14:01:10 -07002303 av1_get_entropy_contexts(bsize, tx_size, pd, args.t_above, args.t_left);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002304
Yaowu Xuc27fc142016-08-22 16:08:15 -07002305 block_rd_txfm(plane, 0, 0, 0, get_plane_block_size(bsize, pd), tx_size,
2306 &args);
2307
2308 if (args.exit_early) {
2309 *rate = INT_MAX;
2310 *distortion = INT64_MAX;
2311 *sse = INT64_MAX;
2312 *skippable = 0;
2313 } else {
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002314 *distortion = args.rd_stats.dist;
2315 *rate = args.rd_stats.rate;
2316 *sse = args.rd_stats.sse;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002317 *skippable = !x->plane[plane].eobs[0];
2318 }
2319}
2320#endif // CONFIG_SUPERTX
2321
Urvang Joshi0d1e4ff2017-04-27 16:17:25 -07002322static int tx_size_cost(const AV1_COMP *const cpi, const MACROBLOCK *const x,
Urvang Joshifeb925f2016-12-05 10:37:29 -08002323 BLOCK_SIZE bsize, TX_SIZE tx_size) {
2324 const AV1_COMMON *const cm = &cpi->common;
Urvang Joshi0d1e4ff2017-04-27 16:17:25 -07002325 const MACROBLOCKD *const xd = &x->e_mbd;
2326 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Urvang Joshifeb925f2016-12-05 10:37:29 -08002327
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002328 const int tx_select = cm->tx_mode == TX_MODE_SELECT &&
2329#if CONFIG_EXT_PARTITION_TYPES
2330 // Currently these block shapes can only use 4x4
2331 // transforms
2332 mbmi->sb_type != BLOCK_4X16 &&
2333 mbmi->sb_type != BLOCK_16X4 &&
2334#endif
2335 mbmi->sb_type >= BLOCK_8X8;
Urvang Joshifeb925f2016-12-05 10:37:29 -08002336
2337 if (tx_select) {
2338 const int is_inter = is_inter_block(mbmi);
2339 const int tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
2340 : intra_tx_size_cat_lookup[bsize];
2341 const TX_SIZE coded_tx_size = txsize_sqr_up_map[tx_size];
2342 const int depth = tx_size_to_depth(coded_tx_size);
2343 const int tx_size_ctx = get_tx_size_context(xd);
Yue Chenb23d00a2017-07-28 17:01:21 -07002344 int r_tx_size = x->tx_size_cost[tx_size_cat][tx_size_ctx][depth];
Yue Chend6bdd462017-07-19 16:05:43 -07002345#if CONFIG_RECT_TX_EXT && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yue Chen56e226e2017-05-02 16:21:40 -07002346 if (is_quarter_tx_allowed(xd, mbmi, is_inter) && tx_size != coded_tx_size)
2347 r_tx_size += av1_cost_bit(cm->fc->quarter_tx_size_prob,
2348 tx_size == quarter_txsize_lookup[bsize]);
Yue Chend6bdd462017-07-19 16:05:43 -07002349#endif
Urvang Joshifeb925f2016-12-05 10:37:29 -08002350 return r_tx_size;
2351 } else {
2352 return 0;
2353 }
2354}
2355
Hui Suddbcde22017-09-18 17:22:02 -07002356// TODO(angiebird): use this function whenever it's possible
Yue Chenb23d00a2017-07-28 17:01:21 -07002357int av1_tx_type_cost(const AV1_COMMON *cm, const MACROBLOCK *x,
2358 const MACROBLOCKD *xd, BLOCK_SIZE bsize, int plane,
2359 TX_SIZE tx_size, TX_TYPE tx_type) {
Angie Chiang05917872017-04-15 12:28:56 -07002360 if (plane > 0) return 0;
2361
Jingning Han243b66b2017-06-23 12:11:47 -07002362#if CONFIG_VAR_TX
2363 tx_size = get_min_tx_size(tx_size);
2364#endif
2365
Angie Chiang65201562017-04-10 15:23:28 -07002366 const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
2367 const int is_inter = is_inter_block(mbmi);
2368#if CONFIG_EXT_TX
Angie Chiang65201562017-04-10 15:23:28 -07002369 if (get_ext_tx_types(tx_size, bsize, is_inter, cm->reduced_tx_set_used) > 1 &&
2370 !xd->lossless[xd->mi[0]->mbmi.segment_id]) {
2371 const int ext_tx_set =
2372 get_ext_tx_set(tx_size, bsize, is_inter, cm->reduced_tx_set_used);
2373 if (is_inter) {
2374 if (ext_tx_set > 0)
Yue Chenb23d00a2017-07-28 17:01:21 -07002375 return x
Angie Chiang65201562017-04-10 15:23:28 -07002376 ->inter_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]][tx_type];
2377 } else {
2378 if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX)
Yue Chenb23d00a2017-07-28 17:01:21 -07002379 return x->intra_tx_type_costs[ext_tx_set][txsize_sqr_map[tx_size]]
2380 [mbmi->mode][tx_type];
Angie Chiang65201562017-04-10 15:23:28 -07002381 }
2382 }
2383#else
2384 (void)bsize;
Yue Chenb23d00a2017-07-28 17:01:21 -07002385 (void)cm;
Angie Chiang65201562017-04-10 15:23:28 -07002386 if (tx_size < TX_32X32 && !xd->lossless[xd->mi[0]->mbmi.segment_id] &&
2387 !FIXED_TX_TYPE) {
2388 if (is_inter) {
Yue Chenb23d00a2017-07-28 17:01:21 -07002389 return x->inter_tx_type_costs[tx_size][tx_type];
Angie Chiang65201562017-04-10 15:23:28 -07002390 } else {
Yue Chenb23d00a2017-07-28 17:01:21 -07002391 return x->intra_tx_type_costs[tx_size]
2392 [intra_mode_to_tx_type_context[mbmi->mode]]
2393 [tx_type];
Angie Chiang65201562017-04-10 15:23:28 -07002394 }
2395 }
2396#endif // CONFIG_EXT_TX
2397 return 0;
2398}
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002399static int64_t txfm_yrd(const AV1_COMP *const cpi, MACROBLOCK *x,
2400 RD_STATS *rd_stats, int64_t ref_best_rd, BLOCK_SIZE bs,
2401 TX_TYPE tx_type, int tx_size) {
Urvang Joshi52648442016-10-13 17:27:51 -07002402 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002403 MACROBLOCKD *const xd = &x->e_mbd;
2404 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
2405 int64_t rd = INT64_MAX;
Yaowu Xuf883b422016-08-30 14:01:10 -07002406 aom_prob skip_prob = av1_get_skip_prob(cm, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407 int s0, s1;
2408 const int is_inter = is_inter_block(mbmi);
Jingning Hanbf9c6b72016-12-14 14:50:45 -08002409 const int tx_select =
2410 cm->tx_mode == TX_MODE_SELECT && mbmi->sb_type >= BLOCK_8X8;
Urvang Joshifeb925f2016-12-05 10:37:29 -08002411
2412 const int r_tx_size = tx_size_cost(cpi, x, bs, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002413
Yushin Chocd4f4a22017-07-10 18:19:05 -07002414#if CONFIG_PVQ
2415 assert(tx_size >= TX_4X4);
2416#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07002417 assert(skip_prob > 0);
2418#if CONFIG_EXT_TX && CONFIG_RECT_TX
2419 assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed_bsize(bs)));
2420#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
2421
Yaowu Xuf883b422016-08-30 14:01:10 -07002422 s0 = av1_cost_bit(skip_prob, 0);
2423 s1 = av1_cost_bit(skip_prob, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002424
2425 mbmi->tx_type = tx_type;
2426 mbmi->tx_size = tx_size;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002427 txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, tx_size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002428 cpi->sf.use_fast_coef_costing);
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002429 if (rd_stats->rate == INT_MAX) return INT64_MAX;
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002430#if !CONFIG_TXK_SEL
Angie Chiang05917872017-04-15 12:28:56 -07002431 int plane = 0;
Yue Chenb23d00a2017-07-28 17:01:21 -07002432 rd_stats->rate += av1_tx_type_cost(cm, x, xd, bs, plane, tx_size, tx_type);
Angie Chiang05917872017-04-15 12:28:56 -07002433#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002434
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002435 if (rd_stats->skip) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002436 if (is_inter) {
Urvang Joshi70006e42017-06-14 16:08:55 -07002437 rd = RDCOST(x->rdmult, s1, rd_stats->sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002438 } else {
Urvang Joshi70006e42017-06-14 16:08:55 -07002439 rd = RDCOST(x->rdmult, s1 + r_tx_size * tx_select, rd_stats->sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002440 }
2441 } else {
Urvang Joshi70006e42017-06-14 16:08:55 -07002442 rd = RDCOST(x->rdmult, rd_stats->rate + s0 + r_tx_size * tx_select,
2443 rd_stats->dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444 }
2445
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002446 if (tx_select) rd_stats->rate += r_tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002447
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002448 if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] &&
2449 !(rd_stats->skip))
Urvang Joshi70006e42017-06-14 16:08:55 -07002450 rd = AOMMIN(rd, RDCOST(x->rdmult, s1, rd_stats->sse));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451
2452 return rd;
2453}
2454
Angie Chiang2d147c12017-04-05 11:23:59 -07002455static int skip_txfm_search(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs,
2456 TX_TYPE tx_type, TX_SIZE tx_size) {
Urvang Joshi0d1e4ff2017-04-27 16:17:25 -07002457 const MACROBLOCKD *const xd = &x->e_mbd;
2458 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Angie Chiang2d147c12017-04-05 11:23:59 -07002459 const TX_SIZE max_tx_size = max_txsize_lookup[bs];
2460 const int is_inter = is_inter_block(mbmi);
2461 int prune = 0;
2462 if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE)
2463 // passing -1 in for tx_type indicates that all 1D
2464 // transforms should be considered for pruning
2465 prune = prune_tx_types(cpi, bs, x, xd, -1);
2466
Sarah Parker53f93db2017-07-11 17:20:04 -07002467#if CONFIG_MRC_TX
2468 // MRC_DCT only implemented for TX_32X32 so only include this tx in
2469 // the search for TX_32X32
Sarah Parker2e08d962017-08-01 19:51:20 -07002470 if (tx_type == MRC_DCT &&
2471 ((is_inter && !USE_MRC_INTER) || (!is_inter && !USE_MRC_INTRA) ||
2472 tx_size != TX_32X32))
2473 return 1;
Sarah Parker53f93db2017-07-11 17:20:04 -07002474#endif // CONFIG_MRC_TX
Angie Chianga4fa1902017-04-05 15:26:09 -07002475 if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) return 1;
Angie Chiang2d147c12017-04-05 11:23:59 -07002476 if (FIXED_TX_TYPE && tx_type != get_default_tx_type(0, xd, 0, tx_size))
2477 return 1;
2478 if (!is_inter && x->use_default_intra_tx_type &&
2479 tx_type != get_default_tx_type(0, xd, 0, tx_size))
2480 return 1;
2481 if (is_inter && x->use_default_inter_tx_type &&
2482 tx_type != get_default_tx_type(0, xd, 0, tx_size))
2483 return 1;
2484 if (max_tx_size >= TX_32X32 && tx_size == TX_4X4) return 1;
2485#if CONFIG_EXT_TX
2486 const AV1_COMMON *const cm = &cpi->common;
Hui Suddbcde22017-09-18 17:22:02 -07002487 const TxSetType tx_set_type =
2488 get_ext_tx_set_type(tx_size, bs, is_inter, cm->reduced_tx_set_used);
2489 if (!av1_ext_tx_used[tx_set_type][tx_type]) return 1;
Angie Chiang2d147c12017-04-05 11:23:59 -07002490 if (is_inter) {
Angie Chiang2d147c12017-04-05 11:23:59 -07002491 if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) {
2492 if (!do_tx_type_search(tx_type, prune)) return 1;
2493 }
2494 } else {
2495 if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) {
2496 if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) return 1;
2497 }
Angie Chiang2d147c12017-04-05 11:23:59 -07002498 }
2499#else // CONFIG_EXT_TX
2500 if (tx_size >= TX_32X32 && tx_type != DCT_DCT) return 1;
2501 if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE &&
2502 !do_tx_type_search(tx_type, prune))
2503 return 1;
2504#endif // CONFIG_EXT_TX
2505 return 0;
2506}
2507
Angie Chiang64a2fb82017-07-06 10:15:16 -07002508#if CONFIG_EXT_INTER && \
2509 (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT || CONFIG_INTERINTRA)
Urvang Joshi52648442016-10-13 17:27:51 -07002510static int64_t estimate_yrd_for_sb(const AV1_COMP *const cpi, BLOCK_SIZE bs,
2511 MACROBLOCK *x, int *r, int64_t *d, int *s,
2512 int64_t *sse, int64_t ref_best_rd) {
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002513 RD_STATS rd_stats;
2514 int64_t rd = txfm_yrd(cpi, x, &rd_stats, ref_best_rd, bs, DCT_DCT,
2515 max_txsize_lookup[bs]);
2516 *r = rd_stats.rate;
2517 *d = rd_stats.dist;
2518 *s = rd_stats.skip;
2519 *sse = rd_stats.sse;
2520 return rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002521}
Yue Chen5e606542017-05-24 17:03:17 -07002522#endif // CONFIG_EXT_INTER && (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002523
Urvang Joshi52648442016-10-13 17:27:51 -07002524static void choose_largest_tx_size(const AV1_COMP *const cpi, MACROBLOCK *x,
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002525 RD_STATS *rd_stats, int64_t ref_best_rd,
Urvang Joshi52648442016-10-13 17:27:51 -07002526 BLOCK_SIZE bs) {
2527 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002528 MACROBLOCKD *const xd = &x->e_mbd;
2529 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
2530 TX_TYPE tx_type, best_tx_type = DCT_DCT;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002531 int64_t this_rd, best_rd = INT64_MAX;
Yaowu Xuf883b422016-08-30 14:01:10 -07002532 aom_prob skip_prob = av1_get_skip_prob(cm, xd);
2533 int s0 = av1_cost_bit(skip_prob, 0);
2534 int s1 = av1_cost_bit(skip_prob, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002535 const int is_inter = is_inter_block(mbmi);
2536 int prune = 0;
Angie Chiangaa0c34b2017-04-25 12:25:38 -07002537 const int plane = 0;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002538 av1_invalid_rd_stats(rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002539
2540 mbmi->tx_size = tx_size_from_tx_mode(bs, cm->tx_mode, is_inter);
Jingning Hane67b38a2016-11-04 10:30:00 -07002541#if CONFIG_VAR_TX
2542 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Fergus Simpson4063a682017-02-28 16:52:22 -08002543#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002544#if CONFIG_EXT_TX
Hui Suddbcde22017-09-18 17:22:02 -07002545 int ext_tx_set =
Sarah Parkere68a3e42017-02-16 14:03:24 -08002546 get_ext_tx_set(mbmi->tx_size, bs, is_inter, cm->reduced_tx_set_used);
Hui Suddbcde22017-09-18 17:22:02 -07002547 const TxSetType tx_set_type =
2548 get_ext_tx_set_type(mbmi->tx_size, bs, is_inter, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002549#endif // CONFIG_EXT_TX
2550
2551 if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE)
2552#if CONFIG_EXT_TX
2553 prune = prune_tx_types(cpi, bs, x, xd, ext_tx_set);
2554#else
2555 prune = prune_tx_types(cpi, bs, x, xd, 0);
Fergus Simpson4063a682017-02-28 16:52:22 -08002556#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002557#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08002558 if (get_ext_tx_types(mbmi->tx_size, bs, is_inter, cm->reduced_tx_set_used) >
2559 1 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07002560 !xd->lossless[mbmi->segment_id]) {
Yushin Cho77bba8d2016-11-04 16:36:56 -07002561#if CONFIG_PVQ
2562 od_rollback_buffer pre_buf, post_buf;
2563
2564 od_encode_checkpoint(&x->daala_enc, &pre_buf);
2565 od_encode_checkpoint(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08002566#endif // CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07002567
2568 for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) {
Hui Suddbcde22017-09-18 17:22:02 -07002569 if (!av1_ext_tx_used[tx_set_type][tx_type]) continue;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002570 RD_STATS this_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002571 if (is_inter) {
2572 if (x->use_default_inter_tx_type &&
2573 tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size))
2574 continue;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002575 if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) {
2576 if (!do_tx_type_search(tx_type, prune)) continue;
2577 }
2578 } else {
2579 if (x->use_default_intra_tx_type &&
2580 tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size))
2581 continue;
2582 if (!ALLOW_INTRA_EXT_TX && bs >= BLOCK_8X8) {
2583 if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue;
2584 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002585 }
2586
2587 mbmi->tx_type = tx_type;
2588
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002589 txfm_rd_in_plane(x, cpi, &this_rd_stats, ref_best_rd, 0, bs,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590 mbmi->tx_size, cpi->sf.use_fast_coef_costing);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002591#if CONFIG_PVQ
2592 od_encode_rollback(&x->daala_enc, &pre_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08002593#endif // CONFIG_PVQ
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002594 if (this_rd_stats.rate == INT_MAX) continue;
Yue Chenb23d00a2017-07-28 17:01:21 -07002595 av1_tx_type_cost(cm, x, xd, bs, plane, mbmi->tx_size, tx_type);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002596
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002597 if (this_rd_stats.skip)
Urvang Joshi70006e42017-06-14 16:08:55 -07002598 this_rd = RDCOST(x->rdmult, s1, this_rd_stats.sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002599 else
Urvang Joshi70006e42017-06-14 16:08:55 -07002600 this_rd =
2601 RDCOST(x->rdmult, this_rd_stats.rate + s0, this_rd_stats.dist);
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002602 if (is_inter_block(mbmi) && !xd->lossless[mbmi->segment_id] &&
2603 !this_rd_stats.skip)
Urvang Joshi70006e42017-06-14 16:08:55 -07002604 this_rd = AOMMIN(this_rd, RDCOST(x->rdmult, s1, this_rd_stats.sse));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002605
2606 if (this_rd < best_rd) {
2607 best_rd = this_rd;
2608 best_tx_type = mbmi->tx_type;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002609 *rd_stats = this_rd_stats;
Yushin Cho77bba8d2016-11-04 16:36:56 -07002610#if CONFIG_PVQ
2611 od_encode_checkpoint(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08002612#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07002613 }
2614 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07002615#if CONFIG_PVQ
2616 od_encode_rollback(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08002617#endif // CONFIG_PVQ
Guillaume Martres4e4d3a02016-08-21 19:02:33 -07002618 } else {
2619 mbmi->tx_type = DCT_DCT;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002620 txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size,
2621 cpi->sf.use_fast_coef_costing);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002623#else // CONFIG_EXT_TX
2624 if (mbmi->tx_size < TX_32X32 && !xd->lossless[mbmi->segment_id]) {
2625 for (tx_type = 0; tx_type < TX_TYPES; ++tx_type) {
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002626 RD_STATS this_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002627 if (!is_inter && x->use_default_intra_tx_type &&
2628 tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size))
2629 continue;
2630 if (is_inter && x->use_default_inter_tx_type &&
2631 tx_type != get_default_tx_type(0, xd, 0, mbmi->tx_size))
2632 continue;
2633 mbmi->tx_type = tx_type;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002634 txfm_rd_in_plane(x, cpi, &this_rd_stats, ref_best_rd, 0, bs,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002635 mbmi->tx_size, cpi->sf.use_fast_coef_costing);
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002636 if (this_rd_stats.rate == INT_MAX) continue;
Angie Chiangaa0c34b2017-04-25 12:25:38 -07002637
Yue Chenb23d00a2017-07-28 17:01:21 -07002638 av1_tx_type_cost(cm, x, xd, bs, plane, mbmi->tx_size, tx_type);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639 if (is_inter) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002640 if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE &&
2641 !do_tx_type_search(tx_type, prune))
2642 continue;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002643 }
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002644 if (this_rd_stats.skip)
Urvang Joshi70006e42017-06-14 16:08:55 -07002645 this_rd = RDCOST(x->rdmult, s1, this_rd_stats.sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002646 else
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002647 this_rd =
Urvang Joshi70006e42017-06-14 16:08:55 -07002648 RDCOST(x->rdmult, this_rd_stats.rate + s0, this_rd_stats.dist);
2649 if (is_inter && !xd->lossless[mbmi->segment_id] && !this_rd_stats.skip)
2650 this_rd = AOMMIN(this_rd, RDCOST(x->rdmult, s1, this_rd_stats.sse));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002651
2652 if (this_rd < best_rd) {
2653 best_rd = this_rd;
2654 best_tx_type = mbmi->tx_type;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002655 *rd_stats = this_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656 }
2657 }
Guillaume Martres4e4d3a02016-08-21 19:02:33 -07002658 } else {
2659 mbmi->tx_type = DCT_DCT;
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002660 txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size,
2661 cpi->sf.use_fast_coef_costing);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002662 }
2663#endif // CONFIG_EXT_TX
2664 mbmi->tx_type = best_tx_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665}
2666
Urvang Joshi52648442016-10-13 17:27:51 -07002667static void choose_smallest_tx_size(const AV1_COMP *const cpi, MACROBLOCK *x,
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002668 RD_STATS *rd_stats, int64_t ref_best_rd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002669 BLOCK_SIZE bs) {
2670 MACROBLOCKD *const xd = &x->e_mbd;
2671 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
2672
2673 mbmi->tx_size = TX_4X4;
2674 mbmi->tx_type = DCT_DCT;
Jingning Hane67b38a2016-11-04 10:30:00 -07002675#if CONFIG_VAR_TX
2676 mbmi->min_tx_size = get_min_tx_size(TX_4X4);
Fergus Simpson4063a682017-02-28 16:52:22 -08002677#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002679 txfm_rd_in_plane(x, cpi, rd_stats, ref_best_rd, 0, bs, mbmi->tx_size,
2680 cpi->sf.use_fast_coef_costing);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681}
2682
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002683#if CONFIG_TXK_SEL || CONFIG_VAR_TX
Angie Chiangf1cb0752017-04-10 16:01:20 -07002684static INLINE int bsize_to_num_blk(BLOCK_SIZE bsize) {
2685 int num_blk = 1 << (num_pels_log2_lookup[bsize] - 2 * tx_size_wide_log2[0]);
2686 return num_blk;
2687}
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002688#endif // CONFIG_TXK_SEL || CONFIG_VAR_TX
Angie Chiangf1cb0752017-04-10 16:01:20 -07002689
Urvang Joshi52648442016-10-13 17:27:51 -07002690static void choose_tx_size_type_from_rd(const AV1_COMP *const cpi,
Angie Chiang7c2b7f22016-11-07 16:00:00 -08002691 MACROBLOCK *x, RD_STATS *rd_stats,
2692 int64_t ref_best_rd, BLOCK_SIZE bs) {
Angie Chianga4fa1902017-04-05 15:26:09 -07002693 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002694 MACROBLOCKD *const xd = &x->e_mbd;
2695 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002696 int64_t rd = INT64_MAX;
Angie Chianga4fa1902017-04-05 15:26:09 -07002697 int n;
2698 int start_tx, end_tx;
2699 int64_t best_rd = INT64_MAX, last_rd = INT64_MAX;
2700 const TX_SIZE max_tx_size = max_txsize_lookup[bs];
2701 TX_SIZE best_tx_size = max_tx_size;
2702 TX_TYPE best_tx_type = DCT_DCT;
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002703#if CONFIG_TXK_SEL
Angie Chiangf1cb0752017-04-10 16:01:20 -07002704 TX_TYPE best_txk_type[MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002705#endif // CONFIG_TXK_SEL
Angie Chianga4fa1902017-04-05 15:26:09 -07002706 const int tx_select = cm->tx_mode == TX_MODE_SELECT;
2707 const int is_inter = is_inter_block(mbmi);
Yushin Cho77bba8d2016-11-04 16:36:56 -07002708#if CONFIG_PVQ
2709 od_rollback_buffer buf;
Yushin Cho77bba8d2016-11-04 16:36:56 -07002710 od_encode_checkpoint(&x->daala_enc, &buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08002711#endif // CONFIG_PVQ
Angie Chianga4fa1902017-04-05 15:26:09 -07002712
2713 av1_invalid_rd_stats(rd_stats);
2714
2715#if CONFIG_EXT_TX && CONFIG_RECT_TX
2716 int evaluate_rect_tx = 0;
2717 if (tx_select) {
2718 evaluate_rect_tx = is_rect_tx_allowed(xd, mbmi);
2719 } else {
2720 const TX_SIZE chosen_tx_size =
2721 tx_size_from_tx_mode(bs, cm->tx_mode, is_inter);
2722 evaluate_rect_tx = is_rect_tx(chosen_tx_size);
2723 assert(IMPLIES(evaluate_rect_tx, is_rect_tx_allowed(xd, mbmi)));
2724 }
2725 if (evaluate_rect_tx) {
Angie Chiangf1cb0752017-04-10 16:01:20 -07002726 TX_TYPE tx_start = DCT_DCT;
2727 TX_TYPE tx_end = TX_TYPES;
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002728#if CONFIG_TXK_SEL
Angie Chiangf1cb0752017-04-10 16:01:20 -07002729 // The tx_type becomes dummy when lv_map is on. The tx_type search will be
2730 // performed in av1_search_txk_type()
2731 tx_end = DCT_DCT + 1;
2732#endif
Angie Chianga4fa1902017-04-05 15:26:09 -07002733 TX_TYPE tx_type;
Angie Chiangf1cb0752017-04-10 16:01:20 -07002734 for (tx_type = tx_start; tx_type < tx_end; ++tx_type) {
Angie Chianga4fa1902017-04-05 15:26:09 -07002735 if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) continue;
Angie Chianga4fa1902017-04-05 15:26:09 -07002736 const TX_SIZE rect_tx_size = max_txsize_rect_lookup[bs];
2737 RD_STATS this_rd_stats;
Hui Suddbcde22017-09-18 17:22:02 -07002738 const TxSetType tx_set_type = get_ext_tx_set_type(
2739 rect_tx_size, bs, is_inter, cm->reduced_tx_set_used);
2740 if (av1_ext_tx_used[tx_set_type][tx_type]) {
Angie Chianga4fa1902017-04-05 15:26:09 -07002741 rd = txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type,
2742 rect_tx_size);
Hui Suda816a12017-08-18 14:46:02 -07002743 ref_best_rd = AOMMIN(rd, ref_best_rd);
Angie Chianga4fa1902017-04-05 15:26:09 -07002744 if (rd < best_rd) {
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002745#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -07002746 memcpy(best_txk_type, mbmi->txk_type, sizeof(best_txk_type[0]) * 256);
Angie Chiangf1cb0752017-04-10 16:01:20 -07002747#endif
Angie Chianga4fa1902017-04-05 15:26:09 -07002748 best_tx_type = tx_type;
2749 best_tx_size = rect_tx_size;
2750 best_rd = rd;
2751 *rd_stats = this_rd_stats;
2752 }
2753 }
Debargha Mukherjee094c9432017-02-22 10:31:25 -08002754#if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
Angie Chianga4fa1902017-04-05 15:26:09 -07002755 const int is_inter = is_inter_block(mbmi);
2756 if (mbmi->sb_type < BLOCK_8X8 && is_inter) break;
Fergus Simpson4063a682017-02-28 16:52:22 -08002757#endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
Angie Chianga4fa1902017-04-05 15:26:09 -07002758 }
2759 }
Yue Chen56e226e2017-05-02 16:21:40 -07002760
2761#if CONFIG_RECT_TX_EXT
2762 // test 1:4/4:1 tx
2763 int evaluate_quarter_tx = 0;
2764 if (is_quarter_tx_allowed(xd, mbmi, is_inter)) {
2765 if (tx_select) {
2766 evaluate_quarter_tx = 1;
2767 } else {
2768 const TX_SIZE chosen_tx_size =
2769 tx_size_from_tx_mode(bs, cm->tx_mode, is_inter);
2770 evaluate_quarter_tx = chosen_tx_size == quarter_txsize_lookup[bs];
2771 }
2772 }
2773 if (evaluate_quarter_tx) {
2774 TX_TYPE tx_start = DCT_DCT;
2775 TX_TYPE tx_end = TX_TYPES;
2776#if CONFIG_TXK_SEL
2777 // The tx_type becomes dummy when lv_map is on. The tx_type search will be
2778 // performed in av1_search_txk_type()
2779 tx_end = DCT_DCT + 1;
2780#endif
2781 TX_TYPE tx_type;
2782 for (tx_type = tx_start; tx_type < tx_end; ++tx_type) {
2783 if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) continue;
2784 const TX_SIZE tx_size = quarter_txsize_lookup[bs];
2785 RD_STATS this_rd_stats;
Hui Suddbcde22017-09-18 17:22:02 -07002786 const TxSetType tx_set_type =
2787 get_ext_tx_set_type(tx_size, bs, is_inter, cm->reduced_tx_set_used);
2788 if (av1_ext_tx_used[tx_set_type][tx_type]) {
Yue Chen56e226e2017-05-02 16:21:40 -07002789 rd =
2790 txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, tx_size);
2791 if (rd < best_rd) {
2792#if CONFIG_TXK_SEL
2793 memcpy(best_txk_type, mbmi->txk_type,
2794 sizeof(best_txk_type[0]) * num_blk);
2795#endif
2796 best_tx_type = tx_type;
2797 best_tx_size = tx_size;
2798 best_rd = rd;
2799 *rd_stats = this_rd_stats;
2800 }
2801 }
2802#if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
2803 const int is_inter = is_inter_block(mbmi);
2804 if (mbmi->sb_type < BLOCK_8X8 && is_inter) break;
2805#endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
2806 }
2807 }
2808#endif // CONFIG_RECT_TX_EXT
Angie Chianga4fa1902017-04-05 15:26:09 -07002809#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
2810
2811 if (tx_select) {
2812 start_tx = max_tx_size;
2813 end_tx = (max_tx_size >= TX_32X32) ? TX_8X8 : TX_4X4;
2814 } else {
2815 const TX_SIZE chosen_tx_size =
2816 tx_size_from_tx_mode(bs, cm->tx_mode, is_inter);
2817 start_tx = chosen_tx_size;
2818 end_tx = chosen_tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002819 }
2820
Angie Chianga4fa1902017-04-05 15:26:09 -07002821 last_rd = INT64_MAX;
2822 for (n = start_tx; n >= end_tx; --n) {
Sarah Parker36661922017-04-13 14:26:49 -07002823#if CONFIG_EXT_TX && CONFIG_RECT_TX
2824 if (is_rect_tx(n)) break;
2825#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Angie Chiangf1cb0752017-04-10 16:01:20 -07002826 TX_TYPE tx_start = DCT_DCT;
2827 TX_TYPE tx_end = TX_TYPES;
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002828#if CONFIG_TXK_SEL
Angie Chiangf1cb0752017-04-10 16:01:20 -07002829 // The tx_type becomes dummy when lv_map is on. The tx_type search will be
2830 // performed in av1_search_txk_type()
2831 tx_end = DCT_DCT + 1;
2832#endif
Angie Chianga4fa1902017-04-05 15:26:09 -07002833 TX_TYPE tx_type;
Angie Chiangf1cb0752017-04-10 16:01:20 -07002834 for (tx_type = tx_start; tx_type < tx_end; ++tx_type) {
Angie Chianga4fa1902017-04-05 15:26:09 -07002835 RD_STATS this_rd_stats;
2836 if (skip_txfm_search(cpi, x, bs, tx_type, n)) continue;
2837 rd = txfm_yrd(cpi, x, &this_rd_stats, ref_best_rd, bs, tx_type, n);
2838#if CONFIG_PVQ
2839 od_encode_rollback(&x->daala_enc, &buf);
2840#endif // CONFIG_PVQ
2841 // Early termination in transform size search.
2842 if (cpi->sf.tx_size_search_breakout &&
2843 (rd == INT64_MAX ||
2844 (this_rd_stats.skip == 1 && tx_type != DCT_DCT && n < start_tx) ||
2845 (n < (int)max_tx_size && rd > last_rd)))
2846 break;
2847
2848 last_rd = rd;
Hui Suda816a12017-08-18 14:46:02 -07002849 ref_best_rd = AOMMIN(rd, ref_best_rd);
Angie Chianga4fa1902017-04-05 15:26:09 -07002850 if (rd < best_rd) {
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002851#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -07002852 memcpy(best_txk_type, mbmi->txk_type, sizeof(best_txk_type[0]) * 256);
Angie Chiangf1cb0752017-04-10 16:01:20 -07002853#endif
Angie Chianga4fa1902017-04-05 15:26:09 -07002854 best_tx_type = tx_type;
2855 best_tx_size = n;
2856 best_rd = rd;
2857 *rd_stats = this_rd_stats;
2858 }
2859#if CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
2860 const int is_inter = is_inter_block(mbmi);
2861 if (mbmi->sb_type < BLOCK_8X8 && is_inter) break;
2862#endif // CONFIG_CB4X4 && !USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
2863 }
2864 }
2865 mbmi->tx_size = best_tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002866 mbmi->tx_type = best_tx_type;
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002867#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -07002868 memcpy(mbmi->txk_type, best_txk_type, sizeof(best_txk_type[0]) * 256);
Angie Chiangf1cb0752017-04-10 16:01:20 -07002869#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002870
Jingning Hane67b38a2016-11-04 10:30:00 -07002871#if CONFIG_VAR_TX
2872 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Fergus Simpson4063a682017-02-28 16:52:22 -08002873#endif // CONFIG_VAR_TX
Jingning Hane67b38a2016-11-04 10:30:00 -07002874
Yaowu Xuc27fc142016-08-22 16:08:15 -07002875#if !CONFIG_EXT_TX
2876 if (mbmi->tx_size >= TX_32X32) assert(mbmi->tx_type == DCT_DCT);
Fergus Simpson4063a682017-02-28 16:52:22 -08002877#endif // !CONFIG_EXT_TX
Yushin Cho77bba8d2016-11-04 16:36:56 -07002878#if CONFIG_PVQ
Yushin Cho403618e2016-11-09 10:45:32 -08002879 if (best_rd != INT64_MAX) {
Angie Chianga4fa1902017-04-05 15:26:09 -07002880 txfm_yrd(cpi, x, rd_stats, ref_best_rd, bs, best_tx_type, best_tx_size);
Yushin Cho05f540a2016-11-08 22:12:51 -08002881 }
Fergus Simpson4063a682017-02-28 16:52:22 -08002882#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07002883}
2884
Angie Chiang0e9a2e92016-11-08 09:45:40 -08002885static void super_block_yrd(const AV1_COMP *const cpi, MACROBLOCK *x,
2886 RD_STATS *rd_stats, BLOCK_SIZE bs,
2887 int64_t ref_best_rd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002888 MACROBLOCKD *xd = &x->e_mbd;
Angie Chiang0e9a2e92016-11-08 09:45:40 -08002889 av1_init_rd_stats(rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002890
2891 assert(bs == xd->mi[0]->mbmi.sb_type);
2892
Yaowu Xu1e2aae12017-02-27 16:33:14 -08002893 if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
Angie Chiang0e9a2e92016-11-08 09:45:40 -08002894 choose_smallest_tx_size(cpi, x, rd_stats, ref_best_rd, bs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002895 } else if (cpi->sf.tx_size_search_method == USE_LARGESTALL) {
Angie Chiang0e9a2e92016-11-08 09:45:40 -08002896 choose_largest_tx_size(cpi, x, rd_stats, ref_best_rd, bs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897 } else {
Angie Chiang0e9a2e92016-11-08 09:45:40 -08002898 choose_tx_size_type_from_rd(cpi, x, rd_stats, ref_best_rd, bs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002899 }
2900}
2901
2902static int conditional_skipintra(PREDICTION_MODE mode,
2903 PREDICTION_MODE best_intra_mode) {
2904 if (mode == D117_PRED && best_intra_mode != V_PRED &&
2905 best_intra_mode != D135_PRED)
2906 return 1;
2907 if (mode == D63_PRED && best_intra_mode != V_PRED &&
2908 best_intra_mode != D45_PRED)
2909 return 1;
2910 if (mode == D207_PRED && best_intra_mode != H_PRED &&
2911 best_intra_mode != D45_PRED)
2912 return 1;
2913 if (mode == D153_PRED && best_intra_mode != H_PRED &&
2914 best_intra_mode != D135_PRED)
2915 return 1;
2916 return 0;
2917}
2918
hui su308a6392017-01-12 14:49:57 -08002919// Model based RD estimation for luma intra blocks.
2920static int64_t intra_model_yrd(const AV1_COMP *const cpi, MACROBLOCK *const x,
hui su9a416f52017-01-13 11:37:53 -08002921 BLOCK_SIZE bsize, int mode_cost) {
David Barker761b1ac2017-09-25 11:23:03 +01002922 const AV1_COMMON *cm = &cpi->common;
hui su308a6392017-01-12 14:49:57 -08002923 MACROBLOCKD *const xd = &x->e_mbd;
2924 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07002925 assert(!is_inter_block(mbmi));
hui su308a6392017-01-12 14:49:57 -08002926 RD_STATS this_rd_stats;
2927 int row, col;
2928 int64_t temp_sse, this_rd;
2929 const TX_SIZE tx_size = tx_size_from_tx_mode(bsize, cpi->common.tx_mode, 0);
2930 const int stepr = tx_size_high_unit[tx_size];
2931 const int stepc = tx_size_wide_unit[tx_size];
2932 const int max_blocks_wide = max_block_wide(xd, bsize, 0);
2933 const int max_blocks_high = max_block_high(xd, bsize, 0);
2934 mbmi->tx_size = tx_size;
2935 // Prediction.
Angie Chiang3d005e42017-04-02 16:31:35 -07002936 const int step = stepr * stepc;
2937 int block = 0;
hui su308a6392017-01-12 14:49:57 -08002938 for (row = 0; row < max_blocks_high; row += stepr) {
2939 for (col = 0; col < max_blocks_wide; col += stepc) {
David Barker761b1ac2017-09-25 11:23:03 +01002940 av1_predict_intra_block_facade(cm, xd, 0, block, col, row, tx_size);
Angie Chiang3d005e42017-04-02 16:31:35 -07002941 block += step;
hui su308a6392017-01-12 14:49:57 -08002942 }
2943 }
2944 // RD estimation.
2945 model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &this_rd_stats.rate,
2946 &this_rd_stats.dist, &this_rd_stats.skip, &temp_sse);
hui su9a416f52017-01-13 11:37:53 -08002947#if CONFIG_EXT_INTRA
Joe Young830d4ce2017-05-30 17:48:13 -07002948 if (av1_is_directional_mode(mbmi->mode, bsize) &&
2949 av1_use_angle_delta(bsize)) {
hui su0a6731f2017-04-26 15:23:47 -07002950 mode_cost += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1,
2951 MAX_ANGLE_DELTA + mbmi->angle_delta[0]);
hui su9a416f52017-01-13 11:37:53 -08002952 }
2953#endif // CONFIG_EXT_INTRA
hui su8f4cc0a2017-01-13 15:14:49 -08002954#if CONFIG_FILTER_INTRA
2955 if (mbmi->mode == DC_PRED) {
2956 const aom_prob prob = cpi->common.fc->filter_intra_probs[0];
2957 if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) {
2958 const int mode = mbmi->filter_intra_mode_info.filter_intra_mode[0];
2959 mode_cost += (av1_cost_bit(prob, 1) +
2960 write_uniform_cost(FILTER_INTRA_MODES, mode));
2961 } else {
2962 mode_cost += av1_cost_bit(prob, 0);
2963 }
2964 }
2965#endif // CONFIG_FILTER_INTRA
Urvang Joshi70006e42017-06-14 16:08:55 -07002966 this_rd =
2967 RDCOST(x->rdmult, this_rd_stats.rate + mode_cost, this_rd_stats.dist);
hui su308a6392017-01-12 14:49:57 -08002968 return this_rd;
2969}
2970
Urvang Joshi56ba91b2017-01-10 13:22:09 -08002971// Extends 'color_map' array from 'orig_width x orig_height' to 'new_width x
2972// new_height'. Extra rows and columns are filled in by copying last valid
2973// row/column.
2974static void extend_palette_color_map(uint8_t *const color_map, int orig_width,
2975 int orig_height, int new_width,
2976 int new_height) {
2977 int j;
2978 assert(new_width >= orig_width);
2979 assert(new_height >= orig_height);
2980 if (new_width == orig_width && new_height == orig_height) return;
2981
2982 for (j = orig_height - 1; j >= 0; --j) {
2983 memmove(color_map + j * new_width, color_map + j * orig_width, orig_width);
2984 // Copy last column to extra columns.
2985 memset(color_map + j * new_width + orig_width,
2986 color_map[j * new_width + orig_width - 1], new_width - orig_width);
2987 }
2988 // Copy last row to extra rows.
2989 for (j = orig_height; j < new_height; ++j) {
2990 memcpy(color_map + j * new_width, color_map + (orig_height - 1) * new_width,
2991 new_width);
2992 }
2993}
2994
hui su33567b22017-04-30 16:40:19 -07002995#if CONFIG_PALETTE_DELTA_ENCODING
2996// Bias toward using colors in the cache.
2997// TODO(huisu): Try other schemes to improve compression.
2998static void optimize_palette_colors(uint16_t *color_cache, int n_cache,
2999 int n_colors, int stride,
3000 float *centroids) {
3001 if (n_cache <= 0) return;
3002 for (int i = 0; i < n_colors * stride; i += stride) {
3003 float min_diff = fabsf(centroids[i] - color_cache[0]);
3004 int idx = 0;
3005 for (int j = 1; j < n_cache; ++j) {
3006 float this_diff = fabsf(centroids[i] - color_cache[j]);
3007 if (this_diff < min_diff) {
3008 min_diff = this_diff;
3009 idx = j;
3010 }
3011 }
3012 if (min_diff < 1.5) centroids[i] = color_cache[idx];
3013 }
3014}
3015#endif // CONFIG_PALETTE_DELTA_ENCODING
3016
hui sude0c70a2017-01-09 17:12:17 -08003017static int rd_pick_palette_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x,
3018 BLOCK_SIZE bsize, int palette_ctx,
3019 int dc_mode_cost, MB_MODE_INFO *best_mbmi,
3020 uint8_t *best_palette_color_map,
hui su78c611a2017-01-13 17:06:04 -08003021 int64_t *best_rd, int64_t *best_model_rd,
3022 int *rate, int *rate_tokenonly,
3023 int64_t *distortion, int *skippable) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003024 int rate_overhead = 0;
3025 MACROBLOCKD *const xd = &x->e_mbd;
3026 MODE_INFO *const mic = xd->mi[0];
hui sude0c70a2017-01-09 17:12:17 -08003027 MB_MODE_INFO *const mbmi = &mic->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07003028 assert(!is_inter_block(mbmi));
Urvang Joshic9e71d42017-08-09 18:58:33 -07003029 assert(bsize >= BLOCK_8X8);
Angie Chiang0e9a2e92016-11-08 09:45:40 -08003030 int this_rate, colors, n;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003031 const int src_stride = x->plane[0].src.stride;
3032 const uint8_t *const src = x->plane[0].src.buf;
hui sude0c70a2017-01-09 17:12:17 -08003033 uint8_t *const color_map = xd->plane[0].color_index_map;
Urvang Joshi56ba91b2017-01-10 13:22:09 -08003034 int block_width, block_height, rows, cols;
3035 av1_get_block_dimensions(bsize, 0, xd, &block_width, &block_height, &rows,
3036 &cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003037
3038 assert(cpi->common.allow_screen_content_tools);
3039
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003040#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003041 if (cpi->common.use_highbitdepth)
Yaowu Xuf883b422016-08-30 14:01:10 -07003042 colors = av1_count_colors_highbd(src, src_stride, rows, cols,
3043 cpi->common.bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003045#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07003046 colors = av1_count_colors(src, src_stride, rows, cols);
hui su5db97432016-10-14 16:10:14 -07003047#if CONFIG_FILTER_INTRA
hui sude0c70a2017-01-09 17:12:17 -08003048 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
hui su5db97432016-10-14 16:10:14 -07003049#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07003050
3051 if (colors > 1 && colors <= 64) {
Sarah Parker9c0e4512017-08-15 16:23:53 -07003052 int r, c, i, k, palette_mode_cost;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003053 const int max_itr = 50;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003054 float *const data = x->palette_buffer->kmeans_data_buf;
3055 float centroids[PALETTE_MAX_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003056 float lb, ub, val;
hui su78c611a2017-01-13 17:06:04 -08003057 RD_STATS tokenonly_rd_stats;
3058 int64_t this_rd, this_model_rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003059 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003060#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003061 uint16_t *src16 = CONVERT_TO_SHORTPTR(src);
3062 if (cpi->common.use_highbitdepth)
3063 lb = ub = src16[0];
3064 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003065#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003066 lb = ub = src[0];
3067
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003068#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003069 if (cpi->common.use_highbitdepth) {
3070 for (r = 0; r < rows; ++r) {
3071 for (c = 0; c < cols; ++c) {
3072 val = src16[r * src_stride + c];
3073 data[r * cols + c] = val;
3074 if (val < lb)
3075 lb = val;
3076 else if (val > ub)
3077 ub = val;
3078 }
3079 }
3080 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003081#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003082 for (r = 0; r < rows; ++r) {
3083 for (c = 0; c < cols; ++c) {
3084 val = src[r * src_stride + c];
3085 data[r * cols + c] = val;
3086 if (val < lb)
3087 lb = val;
3088 else if (val > ub)
3089 ub = val;
3090 }
3091 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003092#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003093 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003094#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003095
3096 mbmi->mode = DC_PRED;
hui su5db97432016-10-14 16:10:14 -07003097#if CONFIG_FILTER_INTRA
3098 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
3099#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07003100
3101 if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return 0;
3102
hui su33567b22017-04-30 16:40:19 -07003103#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -07003104 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -07003105 const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
hui su33567b22017-04-30 16:40:19 -07003106#endif // CONFIG_PALETTE_DELTA_ENCODING
3107
Yaowu Xuc27fc142016-08-22 16:08:15 -07003108 for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2;
3109 --n) {
Urvang Joshi773e3542017-05-05 18:09:42 -07003110 if (colors == PALETTE_MIN_SIZE) {
3111 // Special case: These colors automatically become the centroids.
3112 assert(colors == n);
3113 assert(colors == 2);
3114 centroids[0] = lb;
3115 centroids[1] = ub;
3116 k = 2;
3117 } else {
3118 for (i = 0; i < n; ++i) {
3119 centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2;
3120 }
3121 av1_k_means(data, centroids, color_map, rows * cols, n, 1, max_itr);
hui su33567b22017-04-30 16:40:19 -07003122#if CONFIG_PALETTE_DELTA_ENCODING
3123 optimize_palette_colors(color_cache, n_cache, n, 1, centroids);
3124#endif // CONFIG_PALETTE_DELTA_ENCODING
Urvang Joshi773e3542017-05-05 18:09:42 -07003125 k = av1_remove_duplicates(centroids, n);
3126 if (k < PALETTE_MIN_SIZE) {
3127 // Too few unique colors to create a palette. And DC_PRED will work
3128 // well for that case anyway. So skip.
3129 continue;
3130 }
Urvang Joshi5fb95f72017-05-05 17:36:16 -07003131 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003132
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003133#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003134 if (cpi->common.use_highbitdepth)
3135 for (i = 0; i < k; ++i)
3136 pmi->palette_colors[i] =
3137 clip_pixel_highbd((int)centroids[i], cpi->common.bit_depth);
3138 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003139#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003140 for (i = 0; i < k; ++i)
3141 pmi->palette_colors[i] = clip_pixel((int)centroids[i]);
3142 pmi->palette_size[0] = k;
3143
Yaowu Xuf883b422016-08-30 14:01:10 -07003144 av1_calc_indices(data, centroids, color_map, rows * cols, k, 1);
Urvang Joshi56ba91b2017-01-10 13:22:09 -08003145 extend_palette_color_map(color_map, cols, rows, block_width,
3146 block_height);
hui su78c611a2017-01-13 17:06:04 -08003147 palette_mode_cost =
hui sud13c24a2017-04-07 16:13:07 -07003148 dc_mode_cost +
Yue Chenb23d00a2017-07-28 17:01:21 -07003149 x->palette_y_size_cost[bsize - BLOCK_8X8][k - PALETTE_MIN_SIZE] +
Yaowu Xuc27fc142016-08-22 16:08:15 -07003150 write_uniform_cost(k, color_map[0]) +
Yaowu Xuf883b422016-08-30 14:01:10 -07003151 av1_cost_bit(
3152 av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003153 1);
hui su33567b22017-04-30 16:40:19 -07003154 palette_mode_cost += av1_palette_color_cost_y(pmi,
3155#if CONFIG_PALETTE_DELTA_ENCODING
3156 color_cache, n_cache,
3157#endif // CONFIG_PALETTE_DELTA_ENCODING
3158 cpi->common.bit_depth);
Sarah Parker99e7daa2017-08-29 10:30:13 -07003159 palette_mode_cost +=
3160 av1_cost_color_map(x, 0, 0, bsize, mbmi->tx_size, PALETTE_MAP);
hui su78c611a2017-01-13 17:06:04 -08003161 this_model_rd = intra_model_yrd(cpi, x, bsize, palette_mode_cost);
3162 if (*best_model_rd != INT64_MAX &&
3163 this_model_rd > *best_model_rd + (*best_model_rd >> 1))
3164 continue;
3165 if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd;
3166 super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd);
3167 if (tokenonly_rd_stats.rate == INT_MAX) continue;
3168 this_rate = tokenonly_rd_stats.rate + palette_mode_cost;
Urvang Joshi70006e42017-06-14 16:08:55 -07003169 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
hui su8a630492017-01-10 18:22:41 -08003170 if (!xd->lossless[mbmi->segment_id] && mbmi->sb_type >= BLOCK_8X8) {
Urvang Joshifeb925f2016-12-05 10:37:29 -08003171 tokenonly_rd_stats.rate -= tx_size_cost(cpi, x, bsize, mbmi->tx_size);
hui su8a630492017-01-10 18:22:41 -08003172 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003173 if (this_rd < *best_rd) {
3174 *best_rd = this_rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003175 memcpy(best_palette_color_map, color_map,
Urvang Joshi56ba91b2017-01-10 13:22:09 -08003176 block_width * block_height * sizeof(color_map[0]));
hui sude0c70a2017-01-09 17:12:17 -08003177 *best_mbmi = *mbmi;
Angie Chiang0e9a2e92016-11-08 09:45:40 -08003178 rate_overhead = this_rate - tokenonly_rd_stats.rate;
hui su8a630492017-01-10 18:22:41 -08003179 if (rate) *rate = this_rate;
3180 if (rate_tokenonly) *rate_tokenonly = tokenonly_rd_stats.rate;
3181 if (distortion) *distortion = tokenonly_rd_stats.dist;
3182 if (skippable) *skippable = tokenonly_rd_stats.skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003183 }
3184 }
3185 }
hui sude0c70a2017-01-09 17:12:17 -08003186
3187 if (best_mbmi->palette_mode_info.palette_size[0] > 0) {
3188 memcpy(color_map, best_palette_color_map,
Luc Trudeau0401e892017-08-31 00:37:11 -04003189 block_width * block_height * sizeof(best_palette_color_map[0]));
hui sude0c70a2017-01-09 17:12:17 -08003190 }
3191 *mbmi = *best_mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003192 return rate_overhead;
3193}
3194
Urvang Joshifeb925f2016-12-05 10:37:29 -08003195static int64_t rd_pick_intra_sub_8x8_y_subblock_mode(
Urvang Joshi52648442016-10-13 17:27:51 -07003196 const AV1_COMP *const cpi, MACROBLOCK *x, int row, int col,
3197 PREDICTION_MODE *best_mode, const int *bmode_costs, ENTROPY_CONTEXT *a,
3198 ENTROPY_CONTEXT *l, int *bestrate, int *bestratey, int64_t *bestdistortion,
Urvang Joshifeb925f2016-12-05 10:37:29 -08003199 BLOCK_SIZE bsize, TX_SIZE tx_size, int *y_skip, int64_t rd_thresh) {
Angie Chiang22ba7512016-10-20 17:10:33 -07003200 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003201 PREDICTION_MODE mode;
3202 MACROBLOCKD *const xd = &x->e_mbd;
Urvang Joshi330aec82017-05-08 15:37:42 -07003203 assert(!is_inter_block(&xd->mi[0]->mbmi));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003204 int64_t best_rd = rd_thresh;
3205 struct macroblock_plane *p = &x->plane[0];
3206 struct macroblockd_plane *pd = &xd->plane[0];
3207 const int src_stride = p->src.stride;
3208 const int dst_stride = pd->dst.stride;
3209 const uint8_t *src_init = &p->src.buf[row * 4 * src_stride + col * 4];
Yushin Cho1a2df5e2017-01-09 13:36:13 -08003210 uint8_t *dst_init = &pd->dst.buf[row * 4 * dst_stride + col * 4];
Timothy B. Terriberryfe67ed62017-04-26 16:53:47 -07003211#if CONFIG_CHROMA_2X2
Jingning Han276c2942016-12-05 12:37:02 -08003212 // TODO(jingning): This is a temporal change. The whole function should be
3213 // out when cb4x4 is enabled.
3214 ENTROPY_CONTEXT ta[4], tempa[4];
3215 ENTROPY_CONTEXT tl[4], templ[4];
3216#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003217 ENTROPY_CONTEXT ta[2], tempa[2];
3218 ENTROPY_CONTEXT tl[2], templ[2];
Timothy B. Terriberryfe67ed62017-04-26 16:53:47 -07003219#endif // CONFIG_CHROMA_2X2
Urvang Joshifeb925f2016-12-05 10:37:29 -08003220
3221 const int pred_width_in_4x4_blocks = num_4x4_blocks_wide_lookup[bsize];
3222 const int pred_height_in_4x4_blocks = num_4x4_blocks_high_lookup[bsize];
3223 const int tx_width_unit = tx_size_wide_unit[tx_size];
3224 const int tx_height_unit = tx_size_high_unit[tx_size];
3225 const int pred_block_width = block_size_wide[bsize];
3226 const int pred_block_height = block_size_high[bsize];
3227 const int tx_width = tx_size_wide[tx_size];
3228 const int tx_height = tx_size_high[tx_size];
3229 const int pred_width_in_transform_blocks = pred_block_width / tx_width;
3230 const int pred_height_in_transform_blocks = pred_block_height / tx_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003231 int idx, idy;
Debargha Mukherjee096ae4c2016-09-07 10:08:13 -07003232 int best_can_skip = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003233 uint8_t best_dst[8 * 8];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003234#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003235 uint16_t best_dst16[8 * 8];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003236#endif // CONFIG_HIGHBITDEPTH
Urvang Joshifeb925f2016-12-05 10:37:29 -08003237 const int is_lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
3238#if CONFIG_EXT_TX && CONFIG_RECT_TX
3239 const int sub_bsize = bsize;
3240#else
3241 const int sub_bsize = BLOCK_4X4;
3242#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003243
Yushin Cho77bba8d2016-11-04 16:36:56 -07003244#if CONFIG_PVQ
3245 od_rollback_buffer pre_buf, post_buf;
3246 od_encode_checkpoint(&x->daala_enc, &pre_buf);
3247 od_encode_checkpoint(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08003248#endif // CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003249
Urvang Joshifeb925f2016-12-05 10:37:29 -08003250 assert(bsize < BLOCK_8X8);
3251 assert(tx_width < 8 || tx_height < 8);
3252#if CONFIG_EXT_TX && CONFIG_RECT_TX
hui suff5e2092017-03-27 10:07:59 -07003253 if (is_lossless)
3254 assert(tx_width == 4 && tx_height == 4);
3255 else
3256 assert(tx_width == pred_block_width && tx_height == pred_block_height);
Urvang Joshifeb925f2016-12-05 10:37:29 -08003257#else
3258 assert(tx_width == 4 && tx_height == 4);
3259#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
3260
3261 memcpy(ta, a, pred_width_in_transform_blocks * sizeof(a[0]));
3262 memcpy(tl, l, pred_height_in_transform_blocks * sizeof(l[0]));
3263
3264 xd->mi[0]->mbmi.tx_size = tx_size;
3265
Yaowu Xuc27fc142016-08-22 16:08:15 -07003266 xd->mi[0]->mbmi.palette_mode_info.palette_size[0] = 0;
3267
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003268#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003269 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Thomas Daede6ff6af62017-02-03 16:29:24 -08003270#if CONFIG_PVQ
3271 od_encode_checkpoint(&x->daala_enc, &pre_buf);
3272#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003273 for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
3274 int64_t this_rd;
3275 int ratey = 0;
3276 int64_t distortion = 0;
3277 int rate = bmode_costs[mode];
Debargha Mukherjee096ae4c2016-09-07 10:08:13 -07003278 int can_skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003279
Urvang Joshifeb925f2016-12-05 10:37:29 -08003280 if (!(cpi->sf.intra_y_mode_mask[txsize_sqr_up_map[tx_size]] &
3281 (1 << mode)))
3282 continue;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003283
3284 // Only do the oblique modes if the best so far is
3285 // one of the neighboring directional modes
3286 if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
3287 if (conditional_skipintra(mode, *best_mode)) continue;
3288 }
3289
Urvang Joshifeb925f2016-12-05 10:37:29 -08003290 memcpy(tempa, ta, pred_width_in_transform_blocks * sizeof(ta[0]));
3291 memcpy(templ, tl, pred_height_in_transform_blocks * sizeof(tl[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003292
Urvang Joshifeb925f2016-12-05 10:37:29 -08003293 for (idy = 0; idy < pred_height_in_transform_blocks; ++idy) {
3294 for (idx = 0; idx < pred_width_in_transform_blocks; ++idx) {
3295 const int block_raster_idx = (row + idy) * 2 + (col + idx);
3296 const int block =
3297 av1_raster_order_to_block_index(tx_size, block_raster_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003298 const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride];
3299 uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride];
Thomas Daede6ff6af62017-02-03 16:29:24 -08003300#if !CONFIG_PVQ
Urvang Joshifeb925f2016-12-05 10:37:29 -08003301 int16_t *const src_diff = av1_raster_block_offset_int16(
3302 BLOCK_8X8, block_raster_idx, p->src_diff);
Thomas Daede6ff6af62017-02-03 16:29:24 -08003303#endif
Urvang Joshifeb925f2016-12-05 10:37:29 -08003304 int skip;
3305 assert(block < 4);
3306 assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4,
3307 idx == 0 && idy == 0));
3308 assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4,
3309 block == 0 || block == 2));
3310 xd->mi[0]->bmi[block_raster_idx].as_mode = mode;
David Barker839467f2017-01-19 11:06:15 +00003311 av1_predict_intra_block(
David Barker761b1ac2017-09-25 11:23:03 +01003312 cm, xd, pd->width, pd->height, txsize_to_bsize[tx_size], mode,
3313 dst, dst_stride, dst, dst_stride, col + idx, row + idy, 0);
Thomas Daede6ff6af62017-02-03 16:29:24 -08003314#if !CONFIG_PVQ
Urvang Joshifeb925f2016-12-05 10:37:29 -08003315 aom_highbd_subtract_block(tx_height, tx_width, src_diff, 8, src,
3316 src_stride, dst, dst_stride, xd->bd);
Thomas Daede6ff6af62017-02-03 16:29:24 -08003317#endif
Urvang Joshifeb925f2016-12-05 10:37:29 -08003318 if (is_lossless) {
Jingning Han19b5c8f2017-07-06 15:10:12 -07003319 TX_TYPE tx_type =
3320 av1_get_tx_type(PLANE_TYPE_Y, xd, 0, 0, block, tx_size);
Angie Chiangbd99b382017-06-20 15:11:16 -07003321 const SCAN_ORDER *scan_order =
3322 get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003323 const int coeff_ctx =
Urvang Joshifeb925f2016-12-05 10:37:29 -08003324 combine_entropy_contexts(tempa[idx], templ[idy]);
Thomas Daede6ff6af62017-02-03 16:29:24 -08003325#if !CONFIG_PVQ
Angie Chiangff6d8902016-10-21 11:02:09 -07003326 av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8,
Urvang Joshifeb925f2016-12-05 10:37:29 -08003327 tx_size, coeff_ctx, AV1_XFORM_QUANT_FP);
Jingning Han7eab9ff2017-07-06 10:12:54 -07003328 ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size,
3329 scan_order, tempa + idx, templ + idy,
Urvang Joshi03f6fdc2016-10-14 15:53:39 -07003330 cpi->sf.use_fast_coef_costing);
Urvang Joshifeb925f2016-12-05 10:37:29 -08003331 skip = (p->eobs[block] == 0);
3332 can_skip &= skip;
3333 tempa[idx] = !skip;
3334 templ[idy] = !skip;
3335#if CONFIG_EXT_TX
3336 if (tx_size == TX_8X4) {
3337 tempa[idx + 1] = tempa[idx];
3338 } else if (tx_size == TX_4X8) {
3339 templ[idy + 1] = templ[idy];
3340 }
3341#endif // CONFIG_EXT_TX
Thomas Daede6ff6af62017-02-03 16:29:24 -08003342#else
3343 (void)scan_order;
Urvang Joshifeb925f2016-12-05 10:37:29 -08003344
Thomas Daede6ff6af62017-02-03 16:29:24 -08003345 av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8,
3346 tx_size, coeff_ctx, AV1_XFORM_QUANT_B);
3347
3348 ratey += x->rate;
3349 skip = x->pvq_skip[0];
3350 tempa[idx] = !skip;
3351 templ[idy] = !skip;
3352 can_skip &= skip;
3353#endif
Urvang Joshi70006e42017-06-14 16:08:55 -07003354 if (RDCOST(x->rdmult, ratey, distortion) >= best_rd)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003355 goto next_highbd;
Thomas Daede6ff6af62017-02-03 16:29:24 -08003356#if CONFIG_PVQ
Yushin Cho08195cc2017-04-05 16:37:01 -07003357 if (!skip)
Thomas Daede6ff6af62017-02-03 16:29:24 -08003358#endif
Yushin Cho08195cc2017-04-05 16:37:01 -07003359 av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block),
Lester Lu708c1ec2017-06-14 14:54:49 -07003360#if CONFIG_LGT
3361 mode,
3362#endif
Sarah Parker99e7daa2017-08-29 10:30:13 -07003363#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
3364 BLOCK_OFFSET(xd->mrc_mask, block),
3365#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Yushin Cho08195cc2017-04-05 16:37:01 -07003366 DCT_DCT, tx_size, dst, dst_stride,
3367 p->eobs[block]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368 } else {
3369 int64_t dist;
3370 unsigned int tmp;
Jingning Han19b5c8f2017-07-06 15:10:12 -07003371 TX_TYPE tx_type =
3372 av1_get_tx_type(PLANE_TYPE_Y, xd, 0, 0, block, tx_size);
Angie Chiangbd99b382017-06-20 15:11:16 -07003373 const SCAN_ORDER *scan_order =
3374 get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003375 const int coeff_ctx =
Urvang Joshifeb925f2016-12-05 10:37:29 -08003376 combine_entropy_contexts(tempa[idx], templ[idy]);
Thomas Daede6ff6af62017-02-03 16:29:24 -08003377#if !CONFIG_PVQ
Sarah Parker345366a2017-06-15 12:13:01 -07003378#if DISABLE_TRELLISQ_SEARCH
3379 av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8,
3380 tx_size, coeff_ctx, AV1_XFORM_QUANT_B);
3381#else
Angie Chiangff6d8902016-10-21 11:02:09 -07003382 av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8,
Urvang Joshifeb925f2016-12-05 10:37:29 -08003383 tx_size, coeff_ctx, AV1_XFORM_QUANT_FP);
Jingning Han7eab9ff2017-07-06 10:12:54 -07003384 av1_optimize_b(cm, x, 0, 0, 0, block, BLOCK_8X8, tx_size,
Angie Chiang25645b72017-09-24 14:28:49 -07003385 tempa + idx, templ + idy, 1);
Sarah Parker345366a2017-06-15 12:13:01 -07003386#endif // DISABLE_TRELLISQ_SEARCH
Jingning Han7eab9ff2017-07-06 10:12:54 -07003387 ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size,
3388 scan_order, tempa + idx, templ + idy,
Urvang Joshi03f6fdc2016-10-14 15:53:39 -07003389 cpi->sf.use_fast_coef_costing);
Urvang Joshifeb925f2016-12-05 10:37:29 -08003390 skip = (p->eobs[block] == 0);
3391 can_skip &= skip;
3392 tempa[idx] = !skip;
3393 templ[idy] = !skip;
3394#if CONFIG_EXT_TX
3395 if (tx_size == TX_8X4) {
3396 tempa[idx + 1] = tempa[idx];
3397 } else if (tx_size == TX_4X8) {
3398 templ[idy + 1] = templ[idy];
3399 }
3400#endif // CONFIG_EXT_TX
Thomas Daede6ff6af62017-02-03 16:29:24 -08003401#else
3402 (void)scan_order;
3403
3404 av1_xform_quant(cm, x, 0, block, row + idy, col + idx, BLOCK_8X8,
3405 tx_size, coeff_ctx, AV1_XFORM_QUANT_FP);
3406 ratey += x->rate;
3407 skip = x->pvq_skip[0];
3408 tempa[idx] = !skip;
3409 templ[idy] = !skip;
3410 can_skip &= skip;
3411#endif
3412#if CONFIG_PVQ
Yushin Cho08195cc2017-04-05 16:37:01 -07003413 if (!skip)
Thomas Daede6ff6af62017-02-03 16:29:24 -08003414#endif
Yushin Cho08195cc2017-04-05 16:37:01 -07003415 av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block),
Lester Lu708c1ec2017-06-14 14:54:49 -07003416#if CONFIG_LGT
3417 mode,
3418#endif
Sarah Parker99e7daa2017-08-29 10:30:13 -07003419#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
3420 BLOCK_OFFSET(xd->mrc_mask, block),
3421#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Yushin Cho08195cc2017-04-05 16:37:01 -07003422 tx_type, tx_size, dst, dst_stride,
3423 p->eobs[block]);
Urvang Joshifeb925f2016-12-05 10:37:29 -08003424 cpi->fn_ptr[sub_bsize].vf(src, src_stride, dst, dst_stride, &tmp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003425 dist = (int64_t)tmp << 4;
3426 distortion += dist;
Urvang Joshi70006e42017-06-14 16:08:55 -07003427 if (RDCOST(x->rdmult, ratey, distortion) >= best_rd)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003428 goto next_highbd;
3429 }
3430 }
3431 }
3432
3433 rate += ratey;
Urvang Joshi70006e42017-06-14 16:08:55 -07003434 this_rd = RDCOST(x->rdmult, rate, distortion);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003435
3436 if (this_rd < best_rd) {
3437 *bestrate = rate;
3438 *bestratey = ratey;
3439 *bestdistortion = distortion;
3440 best_rd = this_rd;
Debargha Mukherjee096ae4c2016-09-07 10:08:13 -07003441 best_can_skip = can_skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003442 *best_mode = mode;
Urvang Joshifeb925f2016-12-05 10:37:29 -08003443 memcpy(a, tempa, pred_width_in_transform_blocks * sizeof(tempa[0]));
3444 memcpy(l, templ, pred_height_in_transform_blocks * sizeof(templ[0]));
Thomas Daede6ff6af62017-02-03 16:29:24 -08003445#if CONFIG_PVQ
3446 od_encode_checkpoint(&x->daala_enc, &post_buf);
3447#endif
Urvang Joshifeb925f2016-12-05 10:37:29 -08003448 for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003449 memcpy(best_dst16 + idy * 8,
3450 CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride),
Urvang Joshifeb925f2016-12-05 10:37:29 -08003451 pred_width_in_transform_blocks * 4 * sizeof(uint16_t));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003452 }
3453 }
3454 next_highbd : {}
Thomas Daede6ff6af62017-02-03 16:29:24 -08003455#if CONFIG_PVQ
3456 od_encode_rollback(&x->daala_enc, &pre_buf);
3457#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003458 }
3459
3460 if (best_rd >= rd_thresh) return best_rd;
3461
Thomas Daede6ff6af62017-02-03 16:29:24 -08003462#if CONFIG_PVQ
3463 od_encode_rollback(&x->daala_enc, &post_buf);
3464#endif
3465
Debargha Mukherjee096ae4c2016-09-07 10:08:13 -07003466 if (y_skip) *y_skip &= best_can_skip;
3467
Urvang Joshifeb925f2016-12-05 10:37:29 -08003468 for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003469 memcpy(CONVERT_TO_SHORTPTR(dst_init + idy * dst_stride),
Urvang Joshifeb925f2016-12-05 10:37:29 -08003470 best_dst16 + idy * 8,
3471 pred_width_in_transform_blocks * 4 * sizeof(uint16_t));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472 }
3473
3474 return best_rd;
3475 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003476#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477
Yushin Cho77bba8d2016-11-04 16:36:56 -07003478#if CONFIG_PVQ
3479 od_encode_checkpoint(&x->daala_enc, &pre_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08003480#endif // CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003481
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482 for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
3483 int64_t this_rd;
3484 int ratey = 0;
3485 int64_t distortion = 0;
3486 int rate = bmode_costs[mode];
Debargha Mukherjee096ae4c2016-09-07 10:08:13 -07003487 int can_skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488
Urvang Joshifeb925f2016-12-05 10:37:29 -08003489 if (!(cpi->sf.intra_y_mode_mask[txsize_sqr_up_map[tx_size]] &
3490 (1 << mode))) {
3491 continue;
3492 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003493
3494 // Only do the oblique modes if the best so far is
3495 // one of the neighboring directional modes
3496 if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
3497 if (conditional_skipintra(mode, *best_mode)) continue;
3498 }
3499
Urvang Joshifeb925f2016-12-05 10:37:29 -08003500 memcpy(tempa, ta, pred_width_in_transform_blocks * sizeof(ta[0]));
3501 memcpy(templ, tl, pred_height_in_transform_blocks * sizeof(tl[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003502
Urvang Joshifeb925f2016-12-05 10:37:29 -08003503 for (idy = 0; idy < pred_height_in_4x4_blocks; idy += tx_height_unit) {
3504 for (idx = 0; idx < pred_width_in_4x4_blocks; idx += tx_width_unit) {
3505 const int block_raster_idx = (row + idy) * 2 + (col + idx);
3506 int block = av1_raster_order_to_block_index(tx_size, block_raster_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003507 const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride];
3508 uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride];
Yushin Cho77bba8d2016-11-04 16:36:56 -07003509#if !CONFIG_PVQ
Urvang Joshifeb925f2016-12-05 10:37:29 -08003510 int16_t *const src_diff = av1_raster_block_offset_int16(
3511 BLOCK_8X8, block_raster_idx, p->src_diff);
Fergus Simpson4063a682017-02-28 16:52:22 -08003512#endif // !CONFIG_PVQ
Urvang Joshifeb925f2016-12-05 10:37:29 -08003513 int skip;
3514 assert(block < 4);
3515 assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4,
3516 idx == 0 && idy == 0));
3517 assert(IMPLIES(tx_size == TX_4X8 || tx_size == TX_8X4,
3518 block == 0 || block == 2));
3519 xd->mi[0]->bmi[block_raster_idx].as_mode = mode;
David Barker761b1ac2017-09-25 11:23:03 +01003520 av1_predict_intra_block(cm, xd, pd->width, pd->height,
David Barker839467f2017-01-19 11:06:15 +00003521 txsize_to_bsize[tx_size], mode, dst, dst_stride,
3522 dst, dst_stride,
Jingning Hand1097fc2016-12-06 10:55:34 -08003523#if CONFIG_CB4X4
3524 2 * (col + idx), 2 * (row + idy),
3525#else
3526 col + idx, row + idy,
Fergus Simpson4063a682017-02-28 16:52:22 -08003527#endif // CONFIG_CB4X4
Jingning Hand1097fc2016-12-06 10:55:34 -08003528 0);
Yushin Cho77bba8d2016-11-04 16:36:56 -07003529#if !CONFIG_PVQ
Urvang Joshifeb925f2016-12-05 10:37:29 -08003530 aom_subtract_block(tx_height, tx_width, src_diff, 8, src, src_stride,
3531 dst, dst_stride);
Fergus Simpson4063a682017-02-28 16:52:22 -08003532#endif // !CONFIG_PVQ
Jingning Han19b5c8f2017-07-06 15:10:12 -07003533 TX_TYPE tx_type =
3534 av1_get_tx_type(PLANE_TYPE_Y, xd, 0, 0, block, tx_size);
Angie Chiangbd99b382017-06-20 15:11:16 -07003535 const SCAN_ORDER *scan_order =
3536 get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
Urvang Joshi14731732017-04-27 18:40:49 -07003537 const int coeff_ctx = combine_entropy_contexts(tempa[idx], templ[idy]);
Jingning Hand1097fc2016-12-06 10:55:34 -08003538#if CONFIG_CB4X4
Urvang Joshi14731732017-04-27 18:40:49 -07003539 block = 4 * block;
Fergus Simpson4063a682017-02-28 16:52:22 -08003540#endif // CONFIG_CB4X4
Yushin Cho900243b2017-01-03 11:02:38 -08003541#if !CONFIG_PVQ
Sarah Parker345366a2017-06-15 12:13:01 -07003542#if DISABLE_TRELLISQ_SEARCH
3543 av1_xform_quant(cm, x, 0, block,
3544#if CONFIG_CB4X4
3545 2 * (row + idy), 2 * (col + idx),
3546#else
3547 row + idy, col + idx,
3548#endif // CONFIG_CB4X4
3549 BLOCK_8X8, tx_size, coeff_ctx, AV1_XFORM_QUANT_B);
3550#else
Urvang Joshi14731732017-04-27 18:40:49 -07003551 const AV1_XFORM_QUANT xform_quant =
3552 is_lossless ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
3553 av1_xform_quant(cm, x, 0, block,
Jingning Hand1097fc2016-12-06 10:55:34 -08003554#if CONFIG_CB4X4
Urvang Joshi14731732017-04-27 18:40:49 -07003555 2 * (row + idy), 2 * (col + idx),
Jingning Hand1097fc2016-12-06 10:55:34 -08003556#else
Urvang Joshi14731732017-04-27 18:40:49 -07003557 row + idy, col + idx,
Fergus Simpson4063a682017-02-28 16:52:22 -08003558#endif // CONFIG_CB4X4
Urvang Joshi14731732017-04-27 18:40:49 -07003559 BLOCK_8X8, tx_size, coeff_ctx, xform_quant);
Yushin Cho900243b2017-01-03 11:02:38 -08003560
Jingning Han7eab9ff2017-07-06 10:12:54 -07003561 av1_optimize_b(cm, x, 0, 0, 0, block, BLOCK_8X8, tx_size, tempa + idx,
Angie Chiang25645b72017-09-24 14:28:49 -07003562 templ + idy, 1);
Sarah Parker345366a2017-06-15 12:13:01 -07003563#endif // DISABLE_TRELLISQ_SEARCH
Jingning Han7eab9ff2017-07-06 10:12:54 -07003564 ratey += av1_cost_coeffs(cpi, x, 0, 0, 0, block, tx_size, scan_order,
3565 tempa + idx, templ + idy,
3566 cpi->sf.use_fast_coef_costing);
Urvang Joshi14731732017-04-27 18:40:49 -07003567 skip = (p->eobs[block] == 0);
3568 can_skip &= skip;
3569 tempa[idx] = !skip;
3570 templ[idy] = !skip;
Urvang Joshifeb925f2016-12-05 10:37:29 -08003571#if CONFIG_EXT_TX
Urvang Joshi14731732017-04-27 18:40:49 -07003572 if (tx_size == TX_8X4) {
3573 tempa[idx + 1] = tempa[idx];
3574 } else if (tx_size == TX_4X8) {
3575 templ[idy + 1] = templ[idy];
3576 }
Urvang Joshifeb925f2016-12-05 10:37:29 -08003577#endif // CONFIG_EXT_TX
Yushin Cho77bba8d2016-11-04 16:36:56 -07003578#else
Urvang Joshi14731732017-04-27 18:40:49 -07003579 (void)scan_order;
Yushin Cho900243b2017-01-03 11:02:38 -08003580
Urvang Joshi14731732017-04-27 18:40:49 -07003581 av1_xform_quant(cm, x, 0, block,
Yushin Cho900243b2017-01-03 11:02:38 -08003582#if CONFIG_CB4X4
Urvang Joshi14731732017-04-27 18:40:49 -07003583 2 * (row + idy), 2 * (col + idx),
Yushin Cho900243b2017-01-03 11:02:38 -08003584#else
Urvang Joshi14731732017-04-27 18:40:49 -07003585 row + idy, col + idx,
Fergus Simpson4063a682017-02-28 16:52:22 -08003586#endif // CONFIG_CB4X4
Urvang Joshi14731732017-04-27 18:40:49 -07003587 BLOCK_8X8, tx_size, coeff_ctx, AV1_XFORM_QUANT_FP);
3588
3589 ratey += x->rate;
3590 skip = x->pvq_skip[0];
3591 tempa[idx] = !skip;
3592 templ[idy] = !skip;
3593 can_skip &= skip;
Fergus Simpson4063a682017-02-28 16:52:22 -08003594#endif // !CONFIG_PVQ
Urvang Joshi14731732017-04-27 18:40:49 -07003595
3596 if (!is_lossless) { // To use the pixel domain distortion, we need to
3597 // calculate inverse txfm *before* calculating RD
3598 // cost. Compared to calculating the distortion in
3599 // the frequency domain, the overhead of encoding
3600 // effort is low.
Yushin Chob27a17f2016-12-23 14:33:02 -08003601#if CONFIG_PVQ
Yushin Cho08195cc2017-04-05 16:37:01 -07003602 if (!skip)
Fergus Simpson4063a682017-02-28 16:52:22 -08003603#endif // CONFIG_PVQ
Yushin Cho08195cc2017-04-05 16:37:01 -07003604 av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block),
Lester Lu708c1ec2017-06-14 14:54:49 -07003605#if CONFIG_LGT
3606 mode,
3607#endif
Sarah Parker99e7daa2017-08-29 10:30:13 -07003608#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
3609 BLOCK_OFFSET(xd->mrc_mask, block),
3610#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Yushin Cho08195cc2017-04-05 16:37:01 -07003611 tx_type, tx_size, dst, dst_stride,
3612 p->eobs[block]);
Urvang Joshi14731732017-04-27 18:40:49 -07003613 unsigned int tmp;
Urvang Joshifeb925f2016-12-05 10:37:29 -08003614 cpi->fn_ptr[sub_bsize].vf(src, src_stride, dst, dst_stride, &tmp);
Urvang Joshi14731732017-04-27 18:40:49 -07003615 const int64_t dist = (int64_t)tmp << 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003616 distortion += dist;
Urvang Joshi14731732017-04-27 18:40:49 -07003617 }
3618
Urvang Joshi70006e42017-06-14 16:08:55 -07003619 if (RDCOST(x->rdmult, ratey, distortion) >= best_rd) goto next;
Urvang Joshi14731732017-04-27 18:40:49 -07003620
3621 if (is_lossless) { // Calculate inverse txfm *after* RD cost.
3622#if CONFIG_PVQ
3623 if (!skip)
3624#endif // CONFIG_PVQ
3625 av1_inverse_transform_block(xd, BLOCK_OFFSET(pd->dqcoeff, block),
Lester Lu708c1ec2017-06-14 14:54:49 -07003626#if CONFIG_LGT
3627 mode,
3628#endif
Sarah Parker99e7daa2017-08-29 10:30:13 -07003629#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
3630 BLOCK_OFFSET(xd->mrc_mask, block),
3631#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Urvang Joshi14731732017-04-27 18:40:49 -07003632 DCT_DCT, tx_size, dst, dst_stride,
3633 p->eobs[block]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003634 }
3635 }
3636 }
3637
3638 rate += ratey;
Urvang Joshi70006e42017-06-14 16:08:55 -07003639 this_rd = RDCOST(x->rdmult, rate, distortion);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003640
3641 if (this_rd < best_rd) {
3642 *bestrate = rate;
3643 *bestratey = ratey;
3644 *bestdistortion = distortion;
3645 best_rd = this_rd;
Debargha Mukherjee096ae4c2016-09-07 10:08:13 -07003646 best_can_skip = can_skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003647 *best_mode = mode;
Urvang Joshifeb925f2016-12-05 10:37:29 -08003648 memcpy(a, tempa, pred_width_in_transform_blocks * sizeof(tempa[0]));
3649 memcpy(l, templ, pred_height_in_transform_blocks * sizeof(templ[0]));
Yushin Cho77bba8d2016-11-04 16:36:56 -07003650#if CONFIG_PVQ
3651 od_encode_checkpoint(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08003652#endif // CONFIG_PVQ
Urvang Joshifeb925f2016-12-05 10:37:29 -08003653 for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003654 memcpy(best_dst + idy * 8, dst_init + idy * dst_stride,
Urvang Joshifeb925f2016-12-05 10:37:29 -08003655 pred_width_in_transform_blocks * 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003656 }
3657 next : {}
Yushin Cho77bba8d2016-11-04 16:36:56 -07003658#if CONFIG_PVQ
3659 od_encode_rollback(&x->daala_enc, &pre_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08003660#endif // CONFIG_PVQ
3661 } // mode decision loop
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662
3663 if (best_rd >= rd_thresh) return best_rd;
3664
Yushin Cho77bba8d2016-11-04 16:36:56 -07003665#if CONFIG_PVQ
3666 od_encode_rollback(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08003667#endif // CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07003668
Debargha Mukherjee096ae4c2016-09-07 10:08:13 -07003669 if (y_skip) *y_skip &= best_can_skip;
3670
Urvang Joshifeb925f2016-12-05 10:37:29 -08003671 for (idy = 0; idy < pred_height_in_transform_blocks * 4; ++idy)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003672 memcpy(dst_init + idy * dst_stride, best_dst + idy * 8,
Urvang Joshifeb925f2016-12-05 10:37:29 -08003673 pred_width_in_transform_blocks * 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003674
3675 return best_rd;
3676}
3677
Urvang Joshi52648442016-10-13 17:27:51 -07003678static int64_t rd_pick_intra_sub_8x8_y_mode(const AV1_COMP *const cpi,
3679 MACROBLOCK *mb, int *rate,
3680 int *rate_y, int64_t *distortion,
3681 int *y_skip, int64_t best_rd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682 const MACROBLOCKD *const xd = &mb->e_mbd;
3683 MODE_INFO *const mic = xd->mi[0];
3684 const MODE_INFO *above_mi = xd->above_mi;
3685 const MODE_INFO *left_mi = xd->left_mi;
Urvang Joshifeb925f2016-12-05 10:37:29 -08003686 MB_MODE_INFO *const mbmi = &mic->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07003687 assert(!is_inter_block(mbmi));
Urvang Joshifeb925f2016-12-05 10:37:29 -08003688 const BLOCK_SIZE bsize = mbmi->sb_type;
3689 const int pred_width_in_4x4_blocks = num_4x4_blocks_wide_lookup[bsize];
3690 const int pred_height_in_4x4_blocks = num_4x4_blocks_high_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003691 int idx, idy;
3692 int cost = 0;
3693 int64_t total_distortion = 0;
3694 int tot_rate_y = 0;
3695 int64_t total_rd = 0;
Yue Chenb23d00a2017-07-28 17:01:21 -07003696 const int *bmode_costs = mb->mbmode_cost[0];
Urvang Joshifeb925f2016-12-05 10:37:29 -08003697 const int is_lossless = xd->lossless[mbmi->segment_id];
3698#if CONFIG_EXT_TX && CONFIG_RECT_TX
3699 const TX_SIZE tx_size = is_lossless ? TX_4X4 : max_txsize_rect_lookup[bsize];
3700#else
3701 const TX_SIZE tx_size = TX_4X4;
3702#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703
3704#if CONFIG_EXT_INTRA
hui sueda3d762016-12-06 16:58:23 -08003705#if CONFIG_INTRA_INTERP
Urvang Joshifeb925f2016-12-05 10:37:29 -08003706 mbmi->intra_filter = INTRA_FILTER_LINEAR;
hui sueda3d762016-12-06 16:58:23 -08003707#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07003708#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07003709#if CONFIG_FILTER_INTRA
Urvang Joshifeb925f2016-12-05 10:37:29 -08003710 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
hui su5db97432016-10-14 16:10:14 -07003711#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712
3713 // TODO(any): Add search of the tx_type to improve rd performance at the
3714 // expense of speed.
Urvang Joshifeb925f2016-12-05 10:37:29 -08003715 mbmi->tx_type = DCT_DCT;
3716 mbmi->tx_size = tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003717
Debargha Mukherjee096ae4c2016-09-07 10:08:13 -07003718 if (y_skip) *y_skip = 1;
3719
Urvang Joshifeb925f2016-12-05 10:37:29 -08003720 // Pick modes for each prediction sub-block (of size 4x4, 4x8, or 8x4) in this
3721 // 8x8 coding block.
3722 for (idy = 0; idy < 2; idy += pred_height_in_4x4_blocks) {
3723 for (idx = 0; idx < 2; idx += pred_width_in_4x4_blocks) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003724 PREDICTION_MODE best_mode = DC_PRED;
3725 int r = INT_MAX, ry = INT_MAX;
3726 int64_t d = INT64_MAX, this_rd = INT64_MAX;
Urvang Joshifeb925f2016-12-05 10:37:29 -08003727 int j;
3728 const int pred_block_idx = idy * 2 + idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003729 if (cpi->common.frame_type == KEY_FRAME) {
Urvang Joshifeb925f2016-12-05 10:37:29 -08003730 const PREDICTION_MODE A =
3731 av1_above_block_mode(mic, above_mi, pred_block_idx);
3732 const PREDICTION_MODE L =
3733 av1_left_block_mode(mic, left_mi, pred_block_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003734
Yue Chenb23d00a2017-07-28 17:01:21 -07003735 bmode_costs = mb->y_mode_costs[A][L];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003736 }
Urvang Joshifeb925f2016-12-05 10:37:29 -08003737 this_rd = rd_pick_intra_sub_8x8_y_subblock_mode(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003738 cpi, mb, idy, idx, &best_mode, bmode_costs,
3739 xd->plane[0].above_context + idx, xd->plane[0].left_context + idy, &r,
Urvang Joshifeb925f2016-12-05 10:37:29 -08003740 &ry, &d, bsize, tx_size, y_skip, best_rd - total_rd);
Yushin Cho55104332017-08-14 16:15:43 -07003741#if CONFIG_DIST_8X8
3742 if (!cpi->oxcf.using_dist_8x8)
3743#endif
3744 if (this_rd >= best_rd - total_rd) return INT64_MAX;
3745
Yaowu Xuc27fc142016-08-22 16:08:15 -07003746 total_rd += this_rd;
3747 cost += r;
3748 total_distortion += d;
3749 tot_rate_y += ry;
3750
Urvang Joshifeb925f2016-12-05 10:37:29 -08003751 mic->bmi[pred_block_idx].as_mode = best_mode;
3752 for (j = 1; j < pred_height_in_4x4_blocks; ++j)
3753 mic->bmi[pred_block_idx + j * 2].as_mode = best_mode;
3754 for (j = 1; j < pred_width_in_4x4_blocks; ++j)
3755 mic->bmi[pred_block_idx + j].as_mode = best_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003756
3757 if (total_rd >= best_rd) return INT64_MAX;
3758 }
3759 }
Urvang Joshifeb925f2016-12-05 10:37:29 -08003760 mbmi->mode = mic->bmi[3].as_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003761
Yushin Chob7b60c52017-07-14 16:18:52 -07003762#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -07003763 if (cpi->oxcf.using_dist_8x8) {
Yushin Cho7a428ba2017-01-12 16:28:49 -08003764 const struct macroblock_plane *p = &mb->plane[0];
3765 const struct macroblockd_plane *pd = &xd->plane[0];
3766 const int src_stride = p->src.stride;
3767 const int dst_stride = pd->dst.stride;
3768 uint8_t *src = p->src.buf;
3769 uint8_t *dst = pd->dst.buf;
Yushin Cho7a428ba2017-01-12 16:28:49 -08003770
Yushin Cho7a428ba2017-01-12 16:28:49 -08003771 // Daala-defined distortion computed for the block of 8x8 pixels
Yushin Choe30a47c2017-08-15 13:08:30 -07003772 total_distortion = av1_dist_8x8(cpi, mb, src, src_stride, dst, dst_stride,
Yushin Chob7b60c52017-07-14 16:18:52 -07003773 BLOCK_8X8, 8, 8, 8, 8, mb->qindex)
3774 << 4;
Yushin Cho7a428ba2017-01-12 16:28:49 -08003775 }
Yushin Chob7b60c52017-07-14 16:18:52 -07003776#endif // CONFIG_DIST_8X8
Yaowu Xuc27fc142016-08-22 16:08:15 -07003777 // Add in the cost of the transform type
Urvang Joshifeb925f2016-12-05 10:37:29 -08003778 if (!is_lossless) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003779 int rate_tx_type = 0;
3780#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08003781 if (get_ext_tx_types(tx_size, bsize, 0, cpi->common.reduced_tx_set_used) >
3782 1) {
3783 const int eset =
3784 get_ext_tx_set(tx_size, bsize, 0, cpi->common.reduced_tx_set_used);
Yue Chenb23d00a2017-07-28 17:01:21 -07003785 rate_tx_type = mb->intra_tx_type_costs[eset][txsize_sqr_map[tx_size]]
3786 [mbmi->mode][mbmi->tx_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003787 }
3788#else
clang-format67948d32016-09-07 22:40:40 -07003789 rate_tx_type =
Yue Chenb23d00a2017-07-28 17:01:21 -07003790 mb->intra_tx_type_costs[txsize_sqr_map[tx_size]]
3791 [intra_mode_to_tx_type_context[mbmi->mode]]
3792 [mbmi->tx_type];
Fergus Simpson4063a682017-02-28 16:52:22 -08003793#endif // CONFIG_EXT_TX
Urvang Joshifeb925f2016-12-05 10:37:29 -08003794 assert(mbmi->tx_size == tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003795 cost += rate_tx_type;
3796 tot_rate_y += rate_tx_type;
3797 }
3798
3799 *rate = cost;
3800 *rate_y = tot_rate_y;
3801 *distortion = total_distortion;
3802
Urvang Joshi70006e42017-06-14 16:08:55 -07003803 return RDCOST(mb->rdmult, cost, total_distortion);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003804}
3805
hui su5db97432016-10-14 16:10:14 -07003806#if CONFIG_FILTER_INTRA
3807// Return 1 if an filter intra mode is selected; return 0 otherwise.
3808static int rd_pick_filter_intra_sby(const AV1_COMP *const cpi, MACROBLOCK *x,
3809 int *rate, int *rate_tokenonly,
3810 int64_t *distortion, int *skippable,
3811 BLOCK_SIZE bsize, int mode_cost,
hui su8f4cc0a2017-01-13 15:14:49 -08003812 int64_t *best_rd, int64_t *best_model_rd,
3813 uint16_t skip_mask) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003814 MACROBLOCKD *const xd = &x->e_mbd;
3815 MODE_INFO *const mic = xd->mi[0];
3816 MB_MODE_INFO *mbmi = &mic->mbmi;
hui su5db97432016-10-14 16:10:14 -07003817 int filter_intra_selected_flag = 0;
hui su5db97432016-10-14 16:10:14 -07003818 FILTER_INTRA_MODE mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003819 TX_SIZE best_tx_size = TX_4X4;
hui su5db97432016-10-14 16:10:14 -07003820 FILTER_INTRA_MODE_INFO filter_intra_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003821 TX_TYPE best_tx_type;
3822
hui su5db97432016-10-14 16:10:14 -07003823 av1_zero(filter_intra_mode_info);
3824 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003825 mbmi->mode = DC_PRED;
3826 mbmi->palette_mode_info.palette_size[0] = 0;
3827
3828 for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) {
hui su8f4cc0a2017-01-13 15:14:49 -08003829 int this_rate;
3830 int64_t this_rd, this_model_rd;
3831 RD_STATS tokenonly_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003832 if (skip_mask & (1 << mode)) continue;
hui su5db97432016-10-14 16:10:14 -07003833 mbmi->filter_intra_mode_info.filter_intra_mode[0] = mode;
hui su8f4cc0a2017-01-13 15:14:49 -08003834 this_model_rd = intra_model_yrd(cpi, x, bsize, mode_cost);
3835 if (*best_model_rd != INT64_MAX &&
3836 this_model_rd > *best_model_rd + (*best_model_rd >> 1))
3837 continue;
3838 if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd;
Angie Chiang0e9a2e92016-11-08 09:45:40 -08003839 super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd);
3840 if (tokenonly_rd_stats.rate == INT_MAX) continue;
Angie Chiang0e9a2e92016-11-08 09:45:40 -08003841 this_rate = tokenonly_rd_stats.rate +
hui su5db97432016-10-14 16:10:14 -07003842 av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 1) +
Yaowu Xuc27fc142016-08-22 16:08:15 -07003843 write_uniform_cost(FILTER_INTRA_MODES, mode) + mode_cost;
Urvang Joshi70006e42017-06-14 16:08:55 -07003844 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003845
3846 if (this_rd < *best_rd) {
3847 *best_rd = this_rd;
3848 best_tx_size = mic->mbmi.tx_size;
hui su5db97432016-10-14 16:10:14 -07003849 filter_intra_mode_info = mbmi->filter_intra_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003850 best_tx_type = mic->mbmi.tx_type;
3851 *rate = this_rate;
Angie Chiang0e9a2e92016-11-08 09:45:40 -08003852 *rate_tokenonly = tokenonly_rd_stats.rate;
3853 *distortion = tokenonly_rd_stats.dist;
3854 *skippable = tokenonly_rd_stats.skip;
hui su5db97432016-10-14 16:10:14 -07003855 filter_intra_selected_flag = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003856 }
3857 }
3858
hui su5db97432016-10-14 16:10:14 -07003859 if (filter_intra_selected_flag) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003860 mbmi->mode = DC_PRED;
3861 mbmi->tx_size = best_tx_size;
hui su5db97432016-10-14 16:10:14 -07003862 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] =
3863 filter_intra_mode_info.use_filter_intra_mode[0];
3864 mbmi->filter_intra_mode_info.filter_intra_mode[0] =
3865 filter_intra_mode_info.filter_intra_mode[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003866 mbmi->tx_type = best_tx_type;
3867 return 1;
3868 } else {
3869 return 0;
3870 }
3871}
hui su5db97432016-10-14 16:10:14 -07003872#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07003873
hui su5db97432016-10-14 16:10:14 -07003874#if CONFIG_EXT_INTRA
hui su45dc5972016-12-08 17:42:50 -08003875// Run RD calculation with given luma intra prediction angle., and return
3876// the RD cost. Update the best mode info. if the RD cost is the best so far.
3877static int64_t calc_rd_given_intra_angle(
3878 const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mode_cost,
3879 int64_t best_rd_in, int8_t angle_delta, int max_angle_delta, int *rate,
3880 RD_STATS *rd_stats, int *best_angle_delta, TX_SIZE *best_tx_size,
3881 TX_TYPE *best_tx_type,
hui sueda3d762016-12-06 16:58:23 -08003882#if CONFIG_INTRA_INTERP
3883 INTRA_FILTER *best_filter,
3884#endif // CONFIG_INTRA_INTERP
hui su9a416f52017-01-13 11:37:53 -08003885 int64_t *best_rd, int64_t *best_model_rd) {
Angie Chiang0e9a2e92016-11-08 09:45:40 -08003886 int this_rate;
3887 RD_STATS tokenonly_rd_stats;
hui su9a416f52017-01-13 11:37:53 -08003888 int64_t this_rd, this_model_rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003889 MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07003890 assert(!is_inter_block(mbmi));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003891
hui su45dc5972016-12-08 17:42:50 -08003892 mbmi->angle_delta[0] = angle_delta;
hui su9a416f52017-01-13 11:37:53 -08003893 this_model_rd = intra_model_yrd(cpi, x, bsize, mode_cost);
3894 if (*best_model_rd != INT64_MAX &&
3895 this_model_rd > *best_model_rd + (*best_model_rd >> 1))
3896 return INT64_MAX;
3897 if (this_model_rd < *best_model_rd) *best_model_rd = this_model_rd;
hui su45dc5972016-12-08 17:42:50 -08003898 super_block_yrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in);
3899 if (tokenonly_rd_stats.rate == INT_MAX) return INT64_MAX;
3900
3901 this_rate = tokenonly_rd_stats.rate + mode_cost +
3902 write_uniform_cost(2 * max_angle_delta + 1,
3903 mbmi->angle_delta[0] + max_angle_delta);
Urvang Joshi70006e42017-06-14 16:08:55 -07003904 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003905
3906 if (this_rd < *best_rd) {
3907 *best_rd = this_rd;
3908 *best_angle_delta = mbmi->angle_delta[0];
3909 *best_tx_size = mbmi->tx_size;
hui sueda3d762016-12-06 16:58:23 -08003910#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07003911 *best_filter = mbmi->intra_filter;
hui sueda3d762016-12-06 16:58:23 -08003912#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07003913 *best_tx_type = mbmi->tx_type;
3914 *rate = this_rate;
hui su45dc5972016-12-08 17:42:50 -08003915 rd_stats->rate = tokenonly_rd_stats.rate;
3916 rd_stats->dist = tokenonly_rd_stats.dist;
3917 rd_stats->skip = tokenonly_rd_stats.skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003918 }
hui su45dc5972016-12-08 17:42:50 -08003919 return this_rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003920}
3921
hui su45dc5972016-12-08 17:42:50 -08003922// With given luma directional intra prediction mode, pick the best angle delta
3923// Return the RD cost corresponding to the best angle delta.
Urvang Joshi52648442016-10-13 17:27:51 -07003924static int64_t rd_pick_intra_angle_sby(const AV1_COMP *const cpi, MACROBLOCK *x,
hui su45dc5972016-12-08 17:42:50 -08003925 int *rate, RD_STATS *rd_stats,
3926 BLOCK_SIZE bsize, int mode_cost,
hui su9a416f52017-01-13 11:37:53 -08003927 int64_t best_rd,
3928 int64_t *best_model_rd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003929 MACROBLOCKD *const xd = &x->e_mbd;
3930 MODE_INFO *const mic = xd->mi[0];
3931 MB_MODE_INFO *mbmi = &mic->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07003932 assert(!is_inter_block(mbmi));
hui su45dc5972016-12-08 17:42:50 -08003933 int i, angle_delta, best_angle_delta = 0;
hui su45dc5972016-12-08 17:42:50 -08003934 int first_try = 1;
hui sueda3d762016-12-06 16:58:23 -08003935#if CONFIG_INTRA_INTERP
3936 int p_angle;
Yaowu Xuf883b422016-08-30 14:01:10 -07003937 const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003938 INTRA_FILTER filter, best_filter = INTRA_FILTER_LINEAR;
hui sueda3d762016-12-06 16:58:23 -08003939#endif // CONFIG_INTRA_INTERP
Debargha Mukherjeedf0e0d72017-04-27 15:16:53 -07003940 int64_t this_rd, best_rd_in, rd_cost[2 * (MAX_ANGLE_DELTA + 2)];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003941 TX_SIZE best_tx_size = mic->mbmi.tx_size;
3942 TX_TYPE best_tx_type = mbmi->tx_type;
3943
hui su0a6731f2017-04-26 15:23:47 -07003944 for (i = 0; i < 2 * (MAX_ANGLE_DELTA + 2); ++i) rd_cost[i] = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003945
hui su0a6731f2017-04-26 15:23:47 -07003946 for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) {
hui sueda3d762016-12-06 16:58:23 -08003947#if CONFIG_INTRA_INTERP
hui su45dc5972016-12-08 17:42:50 -08003948 for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) {
3949 if (FILTER_FAST_SEARCH && filter != INTRA_FILTER_LINEAR) continue;
3950 mic->mbmi.intra_filter = filter;
hui sueda3d762016-12-06 16:58:23 -08003951#endif // CONFIG_INTRA_INTERP
hui su45dc5972016-12-08 17:42:50 -08003952 for (i = 0; i < 2; ++i) {
3953 best_rd_in = (best_rd == INT64_MAX)
3954 ? INT64_MAX
3955 : (best_rd + (best_rd >> (first_try ? 3 : 5)));
3956 this_rd = calc_rd_given_intra_angle(
3957 cpi, x, bsize,
hui sueda3d762016-12-06 16:58:23 -08003958#if CONFIG_INTRA_INTERP
Yue Chenb23d00a2017-07-28 17:01:21 -07003959 mode_cost + x->intra_filter_cost[intra_filter_ctx][filter],
hui sueda3d762016-12-06 16:58:23 -08003960#else
hui su45dc5972016-12-08 17:42:50 -08003961 mode_cost,
hui sueda3d762016-12-06 16:58:23 -08003962#endif // CONFIG_INTRA_INTERP
hui su0a6731f2017-04-26 15:23:47 -07003963 best_rd_in, (1 - 2 * i) * angle_delta, MAX_ANGLE_DELTA, rate,
hui su45dc5972016-12-08 17:42:50 -08003964 rd_stats, &best_angle_delta, &best_tx_size, &best_tx_type,
hui sueda3d762016-12-06 16:58:23 -08003965#if CONFIG_INTRA_INTERP
3966 &best_filter,
3967#endif // CONFIG_INTRA_INTERP
hui su9a416f52017-01-13 11:37:53 -08003968 &best_rd, best_model_rd);
hui su45dc5972016-12-08 17:42:50 -08003969 rd_cost[2 * angle_delta + i] = this_rd;
3970 if (first_try && this_rd == INT64_MAX) return best_rd;
3971 first_try = 0;
3972 if (angle_delta == 0) {
3973 rd_cost[1] = this_rd;
3974 break;
3975 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003976 }
hui su45dc5972016-12-08 17:42:50 -08003977#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07003978 }
hui su45dc5972016-12-08 17:42:50 -08003979#endif // CONFIG_INTRA_INTERP
3980 }
3981
3982 assert(best_rd != INT64_MAX);
hui su0a6731f2017-04-26 15:23:47 -07003983 for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) {
hui su45dc5972016-12-08 17:42:50 -08003984 int64_t rd_thresh;
3985#if CONFIG_INTRA_INTERP
3986 for (filter = INTRA_FILTER_LINEAR; filter < INTRA_FILTERS; ++filter) {
3987 if (FILTER_FAST_SEARCH && filter != INTRA_FILTER_LINEAR) continue;
3988 mic->mbmi.intra_filter = filter;
3989#endif // CONFIG_INTRA_INTERP
3990 for (i = 0; i < 2; ++i) {
3991 int skip_search = 0;
3992 rd_thresh = best_rd + (best_rd >> 5);
3993 if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh &&
3994 rd_cost[2 * (angle_delta - 1) + i] > rd_thresh)
3995 skip_search = 1;
3996 if (!skip_search) {
Yue Chenb0f808b2017-04-26 11:55:14 -07003997 calc_rd_given_intra_angle(
hui su45dc5972016-12-08 17:42:50 -08003998 cpi, x, bsize,
3999#if CONFIG_INTRA_INTERP
Yue Chenb23d00a2017-07-28 17:01:21 -07004000 mode_cost + x->intra_filter_cost[intra_filter_ctx][filter],
hui su45dc5972016-12-08 17:42:50 -08004001#else
4002 mode_cost,
4003#endif // CONFIG_INTRA_INTERP
hui su0a6731f2017-04-26 15:23:47 -07004004 best_rd, (1 - 2 * i) * angle_delta, MAX_ANGLE_DELTA, rate,
hui su45dc5972016-12-08 17:42:50 -08004005 rd_stats, &best_angle_delta, &best_tx_size, &best_tx_type,
4006#if CONFIG_INTRA_INTERP
4007 &best_filter,
4008#endif // CONFIG_INTRA_INTERP
hui su9a416f52017-01-13 11:37:53 -08004009 &best_rd, best_model_rd);
hui su45dc5972016-12-08 17:42:50 -08004010 }
4011 }
4012#if CONFIG_INTRA_INTERP
4013 }
4014#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004015 }
4016
hui sueda3d762016-12-06 16:58:23 -08004017#if CONFIG_INTRA_INTERP
hui su45dc5972016-12-08 17:42:50 -08004018 if (FILTER_FAST_SEARCH && rd_stats->rate < INT_MAX) {
hui su0a6731f2017-04-26 15:23:47 -07004019 p_angle = mode_to_angle_map[mbmi->mode] + best_angle_delta * ANGLE_STEP;
Yaowu Xuf883b422016-08-30 14:01:10 -07004020 if (av1_is_intra_filter_switchable(p_angle)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004021 for (filter = INTRA_FILTER_LINEAR + 1; filter < INTRA_FILTERS; ++filter) {
4022 mic->mbmi.intra_filter = filter;
hui su45dc5972016-12-08 17:42:50 -08004023 this_rd = calc_rd_given_intra_angle(
4024 cpi, x, bsize,
Yue Chenb23d00a2017-07-28 17:01:21 -07004025 mode_cost + x->intra_filter_cost[intra_filter_ctx][filter], best_rd,
4026 best_angle_delta, MAX_ANGLE_DELTA, rate, rd_stats,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004027 &best_angle_delta, &best_tx_size, &best_tx_type, &best_filter,
hui su9a416f52017-01-13 11:37:53 -08004028 &best_rd, best_model_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004029 }
4030 }
4031 }
hui sueda3d762016-12-06 16:58:23 -08004032#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004033
4034 mbmi->tx_size = best_tx_size;
4035 mbmi->angle_delta[0] = best_angle_delta;
hui sueda3d762016-12-06 16:58:23 -08004036#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004037 mic->mbmi.intra_filter = best_filter;
hui sueda3d762016-12-06 16:58:23 -08004038#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07004039 mbmi->tx_type = best_tx_type;
4040 return best_rd;
4041}
4042
4043// Indices are sign, integer, and fractional part of the gradient value
4044static const uint8_t gradient_to_angle_bin[2][7][16] = {
4045 {
4046 { 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0 },
4047 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
4048 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4049 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4050 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
4051 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
4052 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
4053 },
4054 {
4055 { 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4 },
4056 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3 },
4057 { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
4058 { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
4059 { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
4060 { 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
4061 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
4062 },
4063};
4064
Yue Chen56e226e2017-05-02 16:21:40 -07004065/* clang-format off */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004066static const uint8_t mode_to_angle_bin[INTRA_MODES] = {
4067 0, 2, 6, 0, 4, 3, 5, 7, 1, 0,
hui su9cc10652017-04-27 17:22:07 -07004068 0,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004069};
Yue Chen56e226e2017-05-02 16:21:40 -07004070/* clang-format on */
Yaowu Xuc27fc142016-08-22 16:08:15 -07004071
4072static void angle_estimation(const uint8_t *src, int src_stride, int rows,
hui su9cc10652017-04-27 17:22:07 -07004073 int cols, BLOCK_SIZE bsize,
4074 uint8_t *directional_mode_skip_mask) {
4075 memset(directional_mode_skip_mask, 0,
4076 INTRA_MODES * sizeof(*directional_mode_skip_mask));
Joe Young830d4ce2017-05-30 17:48:13 -07004077 // Check if angle_delta is used
4078 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004079 uint64_t hist[DIRECTIONAL_MODES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004080 memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0]));
4081 src += src_stride;
hui su9cc10652017-04-27 17:22:07 -07004082 int r, c, dx, dy;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004083 for (r = 1; r < rows; ++r) {
4084 for (c = 1; c < cols; ++c) {
4085 dx = src[c] - src[c - 1];
4086 dy = src[c] - src[c - src_stride];
hui su9cc10652017-04-27 17:22:07 -07004087 int index;
4088 const int temp = dx * dx + dy * dy;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004089 if (dy == 0) {
4090 index = 2;
4091 } else {
hui su9cc10652017-04-27 17:22:07 -07004092 const int sn = (dx > 0) ^ (dy > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004093 dx = abs(dx);
4094 dy = abs(dy);
hui su9cc10652017-04-27 17:22:07 -07004095 const int remd = (dx % dy) * 16 / dy;
4096 const int quot = dx / dy;
Yaowu Xuf883b422016-08-30 14:01:10 -07004097 index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004098 }
4099 hist[index] += temp;
4100 }
4101 src += src_stride;
4102 }
4103
hui su9cc10652017-04-27 17:22:07 -07004104 int i;
4105 uint64_t hist_sum = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004106 for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i];
4107 for (i = 0; i < INTRA_MODES; ++i) {
hui su9cc10652017-04-27 17:22:07 -07004108 if (av1_is_directional_mode(i, bsize)) {
Urvang Joshida70e7b2016-10-19 11:48:54 -07004109 const uint8_t angle_bin = mode_to_angle_bin[i];
4110 uint64_t score = 2 * hist[angle_bin];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004111 int weight = 2;
Urvang Joshida70e7b2016-10-19 11:48:54 -07004112 if (angle_bin > 0) {
4113 score += hist[angle_bin - 1];
4114 ++weight;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004115 }
Urvang Joshida70e7b2016-10-19 11:48:54 -07004116 if (angle_bin < DIRECTIONAL_MODES - 1) {
4117 score += hist[angle_bin + 1];
4118 ++weight;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004119 }
4120 if (score * ANGLE_SKIP_THRESH < hist_sum * weight)
4121 directional_mode_skip_mask[i] = 1;
4122 }
4123 }
4124}
4125
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004126#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004127static void highbd_angle_estimation(const uint8_t *src8, int src_stride,
hui su9cc10652017-04-27 17:22:07 -07004128 int rows, int cols, BLOCK_SIZE bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004129 uint8_t *directional_mode_skip_mask) {
hui su9cc10652017-04-27 17:22:07 -07004130 memset(directional_mode_skip_mask, 0,
4131 INTRA_MODES * sizeof(*directional_mode_skip_mask));
Joe Young830d4ce2017-05-30 17:48:13 -07004132 // Check if angle_delta is used
4133 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004134 uint16_t *src = CONVERT_TO_SHORTPTR(src8);
hui su9cc10652017-04-27 17:22:07 -07004135 uint64_t hist[DIRECTIONAL_MODES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004136 memset(hist, 0, DIRECTIONAL_MODES * sizeof(hist[0]));
4137 src += src_stride;
hui su9cc10652017-04-27 17:22:07 -07004138 int r, c, dx, dy;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004139 for (r = 1; r < rows; ++r) {
4140 for (c = 1; c < cols; ++c) {
4141 dx = src[c] - src[c - 1];
4142 dy = src[c] - src[c - src_stride];
hui su9cc10652017-04-27 17:22:07 -07004143 int index;
4144 const int temp = dx * dx + dy * dy;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004145 if (dy == 0) {
4146 index = 2;
4147 } else {
hui su9cc10652017-04-27 17:22:07 -07004148 const int sn = (dx > 0) ^ (dy > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004149 dx = abs(dx);
4150 dy = abs(dy);
hui su9cc10652017-04-27 17:22:07 -07004151 const int remd = (dx % dy) * 16 / dy;
4152 const int quot = dx / dy;
Yaowu Xuf883b422016-08-30 14:01:10 -07004153 index = gradient_to_angle_bin[sn][AOMMIN(quot, 6)][AOMMIN(remd, 15)];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004154 }
4155 hist[index] += temp;
4156 }
4157 src += src_stride;
4158 }
4159
hui su9cc10652017-04-27 17:22:07 -07004160 int i;
4161 uint64_t hist_sum = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004162 for (i = 0; i < DIRECTIONAL_MODES; ++i) hist_sum += hist[i];
4163 for (i = 0; i < INTRA_MODES; ++i) {
hui su9cc10652017-04-27 17:22:07 -07004164 if (av1_is_directional_mode(i, bsize)) {
Urvang Joshida70e7b2016-10-19 11:48:54 -07004165 const uint8_t angle_bin = mode_to_angle_bin[i];
4166 uint64_t score = 2 * hist[angle_bin];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004167 int weight = 2;
Urvang Joshida70e7b2016-10-19 11:48:54 -07004168 if (angle_bin > 0) {
4169 score += hist[angle_bin - 1];
4170 ++weight;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004171 }
Urvang Joshida70e7b2016-10-19 11:48:54 -07004172 if (angle_bin < DIRECTIONAL_MODES - 1) {
4173 score += hist[angle_bin + 1];
4174 ++weight;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004175 }
4176 if (score * ANGLE_SKIP_THRESH < hist_sum * weight)
4177 directional_mode_skip_mask[i] = 1;
4178 }
4179 }
4180}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004181#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004182#endif // CONFIG_EXT_INTRA
4183
Urvang Joshic9e71d42017-08-09 18:58:33 -07004184// Returns true if palette can be used for this block.
4185static int can_use_palette(const AV1_COMP *const cpi,
4186 const MB_MODE_INFO *const mbmi) {
4187 return cpi->common.allow_screen_content_tools && mbmi->sb_type >= BLOCK_8X8 &&
4188 mbmi->sb_type <= BLOCK_LARGEST;
4189}
Urvang Joshic9e71d42017-08-09 18:58:33 -07004190
Yaowu Xuc27fc142016-08-22 16:08:15 -07004191// This function is used only for intra_only frames
Urvang Joshi52648442016-10-13 17:27:51 -07004192static int64_t rd_pick_intra_sby_mode(const AV1_COMP *const cpi, MACROBLOCK *x,
4193 int *rate, int *rate_tokenonly,
4194 int64_t *distortion, int *skippable,
4195 BLOCK_SIZE bsize, int64_t best_rd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004196 MACROBLOCKD *const xd = &x->e_mbd;
4197 MODE_INFO *const mic = xd->mi[0];
hui sude0c70a2017-01-09 17:12:17 -08004198 MB_MODE_INFO *const mbmi = &mic->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07004199 assert(!is_inter_block(mbmi));
hui sude0c70a2017-01-09 17:12:17 -08004200 MB_MODE_INFO best_mbmi = *mbmi;
hui su308a6392017-01-12 14:49:57 -08004201 int64_t best_model_rd = INT64_MAX;
hui sude0c70a2017-01-09 17:12:17 -08004202#if CONFIG_EXT_INTRA
Jingning Hanae5cfde2016-11-30 12:01:44 -08004203 const int rows = block_size_high[bsize];
4204 const int cols = block_size_wide[bsize];
hui sueda3d762016-12-06 16:58:23 -08004205#if CONFIG_INTRA_INTERP
Yaowu Xuf883b422016-08-30 14:01:10 -07004206 const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd);
hui sueda3d762016-12-06 16:58:23 -08004207#endif // CONFIG_INTRA_INTERP
hui sude0c70a2017-01-09 17:12:17 -08004208 int is_directional_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004209 uint8_t directional_mode_skip_mask[INTRA_MODES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004210 const int src_stride = x->plane[0].src.stride;
4211 const uint8_t *src = x->plane[0].src.buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004212#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07004213#if CONFIG_FILTER_INTRA
4214 int beat_best_rd = 0;
hui su5db97432016-10-14 16:10:14 -07004215 uint16_t filter_intra_mode_skip_mask = (1 << FILTER_INTRA_MODES) - 1;
4216#endif // CONFIG_FILTER_INTRA
Urvang Joshi52648442016-10-13 17:27:51 -07004217 const int *bmode_costs;
hui sude0c70a2017-01-09 17:12:17 -08004218 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004219 uint8_t *best_palette_color_map =
4220 cpi->common.allow_screen_content_tools
4221 ? x->palette_buffer->best_palette_color_map
4222 : NULL;
Urvang Joshi23a61112017-01-30 14:59:27 -08004223 int palette_y_mode_ctx = 0;
Urvang Joshic9e71d42017-08-09 18:58:33 -07004224 const int try_palette = can_use_palette(cpi, mbmi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004225 const MODE_INFO *above_mi = xd->above_mi;
4226 const MODE_INFO *left_mi = xd->left_mi;
Yaowu Xuf883b422016-08-30 14:01:10 -07004227 const PREDICTION_MODE A = av1_above_block_mode(mic, above_mi, 0);
4228 const PREDICTION_MODE L = av1_left_block_mode(mic, left_mi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229 const PREDICTION_MODE FINAL_MODE_SEARCH = TM_PRED + 1;
Yushin Cho77bba8d2016-11-04 16:36:56 -07004230#if CONFIG_PVQ
4231 od_rollback_buffer pre_buf, post_buf;
4232
4233 od_encode_checkpoint(&x->daala_enc, &pre_buf);
4234 od_encode_checkpoint(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08004235#endif // CONFIG_PVQ
Yue Chenb23d00a2017-07-28 17:01:21 -07004236 bmode_costs = x->y_mode_costs[A][L];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004237
4238#if CONFIG_EXT_INTRA
hui sude0c70a2017-01-09 17:12:17 -08004239 mbmi->angle_delta[0] = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004240#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004241 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
hui su9cc10652017-04-27 17:22:07 -07004242 highbd_angle_estimation(src, src_stride, rows, cols, bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004243 directional_mode_skip_mask);
4244 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004245#endif // CONFIG_HIGHBITDEPTH
hui su9cc10652017-04-27 17:22:07 -07004246 angle_estimation(src, src_stride, rows, cols, bsize,
4247 directional_mode_skip_mask);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004248#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07004249#if CONFIG_FILTER_INTRA
hui sude0c70a2017-01-09 17:12:17 -08004250 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
hui su5db97432016-10-14 16:10:14 -07004251#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07004252 pmi->palette_size[0] = 0;
4253 if (above_mi)
Urvang Joshi23a61112017-01-30 14:59:27 -08004254 palette_y_mode_ctx +=
4255 (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004256 if (left_mi)
Urvang Joshi23a61112017-01-30 14:59:27 -08004257 palette_y_mode_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004258
4259 if (cpi->sf.tx_type_search.fast_intra_tx_type_search)
4260 x->use_default_intra_tx_type = 1;
4261 else
4262 x->use_default_intra_tx_type = 0;
4263
4264 /* Y Search for intra prediction mode */
hui su8a516a82017-07-06 10:00:36 -07004265 for (int mode_idx = DC_PRED; mode_idx <= FINAL_MODE_SEARCH; ++mode_idx) {
Angie Chiang0e9a2e92016-11-08 09:45:40 -08004266 RD_STATS this_rd_stats;
hui su308a6392017-01-12 14:49:57 -08004267 int this_rate, this_rate_tokenonly, s;
4268 int64_t this_distortion, this_rd, this_model_rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004269 if (mode_idx == FINAL_MODE_SEARCH) {
4270 if (x->use_default_intra_tx_type == 0) break;
hui sude0c70a2017-01-09 17:12:17 -08004271 mbmi->mode = best_mbmi.mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004272 x->use_default_intra_tx_type = 0;
4273 } else {
hui su8a516a82017-07-06 10:00:36 -07004274 assert(mode_idx < INTRA_MODES);
4275 mbmi->mode = intra_rd_search_mode_order[mode_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004276 }
Yushin Cho77bba8d2016-11-04 16:36:56 -07004277#if CONFIG_PVQ
4278 od_encode_rollback(&x->daala_enc, &pre_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08004279#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004280#if CONFIG_EXT_INTRA
hui su308a6392017-01-12 14:49:57 -08004281 mbmi->angle_delta[0] = 0;
4282#endif // CONFIG_EXT_INTRA
hui su9a416f52017-01-13 11:37:53 -08004283 this_model_rd = intra_model_yrd(cpi, x, bsize, bmode_costs[mbmi->mode]);
hui su308a6392017-01-12 14:49:57 -08004284 if (best_model_rd != INT64_MAX &&
4285 this_model_rd > best_model_rd + (best_model_rd >> 1))
4286 continue;
4287 if (this_model_rd < best_model_rd) best_model_rd = this_model_rd;
4288#if CONFIG_EXT_INTRA
hui sude0c70a2017-01-09 17:12:17 -08004289 is_directional_mode = av1_is_directional_mode(mbmi->mode, bsize);
4290 if (is_directional_mode && directional_mode_skip_mask[mbmi->mode]) continue;
Joe Young830d4ce2017-05-30 17:48:13 -07004291 if (is_directional_mode && av1_use_angle_delta(bsize)) {
hui su45dc5972016-12-08 17:42:50 -08004292 this_rd_stats.rate = INT_MAX;
Yue Chenb0f808b2017-04-26 11:55:14 -07004293 rd_pick_intra_angle_sby(cpi, x, &this_rate, &this_rd_stats, bsize,
4294 bmode_costs[mbmi->mode], best_rd, &best_model_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004295 } else {
Angie Chiang0e9a2e92016-11-08 09:45:40 -08004296 super_block_yrd(cpi, x, &this_rd_stats, bsize, best_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004297 }
4298#else
Angie Chiang0e9a2e92016-11-08 09:45:40 -08004299 super_block_yrd(cpi, x, &this_rd_stats, bsize, best_rd);
hui su45dc5972016-12-08 17:42:50 -08004300#endif // CONFIG_EXT_INTRA
Angie Chiang0e9a2e92016-11-08 09:45:40 -08004301 this_rate_tokenonly = this_rd_stats.rate;
4302 this_distortion = this_rd_stats.dist;
4303 s = this_rd_stats.skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004304
4305 if (this_rate_tokenonly == INT_MAX) continue;
4306
hui sude0c70a2017-01-09 17:12:17 -08004307 this_rate = this_rate_tokenonly + bmode_costs[mbmi->mode];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004308
hui sude0c70a2017-01-09 17:12:17 -08004309 if (!xd->lossless[mbmi->segment_id] && mbmi->sb_type >= BLOCK_8X8) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004310 // super_block_yrd above includes the cost of the tx_size in the
4311 // tokenonly rate, but for intra blocks, tx_size is always coded
4312 // (prediction granularity), so we account for it in the full rate,
4313 // not the tokenonly rate.
Urvang Joshifeb925f2016-12-05 10:37:29 -08004314 this_rate_tokenonly -= tx_size_cost(cpi, x, bsize, mbmi->tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004315 }
hui su9bc1d8d2017-03-24 12:36:03 -07004316 if (try_palette && mbmi->mode == DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -08004317 this_rate +=
4318 av1_cost_bit(av1_default_palette_y_mode_prob[bsize - BLOCK_8X8]
4319 [palette_y_mode_ctx],
4320 0);
hui su9bc1d8d2017-03-24 12:36:03 -07004321 }
hui su5db97432016-10-14 16:10:14 -07004322#if CONFIG_FILTER_INTRA
hui sude0c70a2017-01-09 17:12:17 -08004323 if (mbmi->mode == DC_PRED)
hui su5db97432016-10-14 16:10:14 -07004324 this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[0], 0);
4325#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07004326#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327 if (is_directional_mode) {
hui sueda3d762016-12-06 16:58:23 -08004328#if CONFIG_INTRA_INTERP
hui su0a6731f2017-04-26 15:23:47 -07004329 const int p_angle =
4330 mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xuf883b422016-08-30 14:01:10 -07004331 if (av1_is_intra_filter_switchable(p_angle))
Yue Chenb23d00a2017-07-28 17:01:21 -07004332 this_rate += x->intra_filter_cost[intra_filter_ctx][mbmi->intra_filter];
hui sueda3d762016-12-06 16:58:23 -08004333#endif // CONFIG_INTRA_INTERP
Joe Young830d4ce2017-05-30 17:48:13 -07004334 if (av1_use_angle_delta(bsize)) {
4335 this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1,
4336 MAX_ANGLE_DELTA + mbmi->angle_delta[0]);
4337 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004338 }
4339#endif // CONFIG_EXT_INTRA
Hui Su8dc00922017-09-14 16:15:55 -07004340#if CONFIG_INTRABC
4341 if (bsize >= BLOCK_8X8 && cpi->common.allow_screen_content_tools)
4342 this_rate += x->intrabc_cost[0];
4343#endif // CONFIG_INTRABC
Urvang Joshi70006e42017-06-14 16:08:55 -07004344 this_rd = RDCOST(x->rdmult, this_rate, this_distortion);
hui su5db97432016-10-14 16:10:14 -07004345#if CONFIG_FILTER_INTRA
Debargha Mukherjee1ae9f2c2016-10-04 14:30:16 -07004346 if (best_rd == INT64_MAX || this_rd - best_rd < (best_rd >> 4)) {
hui sude0c70a2017-01-09 17:12:17 -08004347 filter_intra_mode_skip_mask ^= (1 << mbmi->mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004348 }
hui su5db97432016-10-14 16:10:14 -07004349#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07004350
4351 if (this_rd < best_rd) {
hui sude0c70a2017-01-09 17:12:17 -08004352 best_mbmi = *mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004353 best_rd = this_rd;
hui su5db97432016-10-14 16:10:14 -07004354#if CONFIG_FILTER_INTRA
4355 beat_best_rd = 1;
4356#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07004357 *rate = this_rate;
4358 *rate_tokenonly = this_rate_tokenonly;
4359 *distortion = this_distortion;
4360 *skippable = s;
Yushin Cho77bba8d2016-11-04 16:36:56 -07004361#if CONFIG_PVQ
4362 od_encode_checkpoint(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08004363#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07004364 }
4365 }
4366
Yushin Cho77bba8d2016-11-04 16:36:56 -07004367#if CONFIG_PVQ
4368 od_encode_rollback(&x->daala_enc, &post_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08004369#endif // CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07004370
hui su9bc1d8d2017-03-24 12:36:03 -07004371 if (try_palette) {
Urvang Joshi23a61112017-01-30 14:59:27 -08004372 rd_pick_palette_intra_sby(cpi, x, bsize, palette_y_mode_ctx,
4373 bmode_costs[DC_PRED], &best_mbmi,
4374 best_palette_color_map, &best_rd, &best_model_rd,
4375 rate, rate_tokenonly, distortion, skippable);
hui sude0c70a2017-01-09 17:12:17 -08004376 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004377
hui su5db97432016-10-14 16:10:14 -07004378#if CONFIG_FILTER_INTRA
4379 if (beat_best_rd) {
4380 if (rd_pick_filter_intra_sby(cpi, x, rate, rate_tokenonly, distortion,
4381 skippable, bsize, bmode_costs[DC_PRED],
hui su8f4cc0a2017-01-13 15:14:49 -08004382 &best_rd, &best_model_rd,
4383 filter_intra_mode_skip_mask)) {
hui sude0c70a2017-01-09 17:12:17 -08004384 best_mbmi = *mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004385 }
4386 }
hui su5db97432016-10-14 16:10:14 -07004387#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07004388
hui sude0c70a2017-01-09 17:12:17 -08004389 *mbmi = best_mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004390 return best_rd;
4391}
4392
Yue Chena1e48dc2016-08-29 17:29:33 -07004393// Return value 0: early termination triggered, no valid rd cost available;
4394// 1: rd cost values are valid.
Angie Chiang284d7772016-11-08 11:06:45 -08004395static int super_block_uvrd(const AV1_COMP *const cpi, MACROBLOCK *x,
4396 RD_STATS *rd_stats, BLOCK_SIZE bsize,
4397 int64_t ref_best_rd) {
Yue Chena1e48dc2016-08-29 17:29:33 -07004398 MACROBLOCKD *const xd = &x->e_mbd;
4399 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
hui su0c6244b2017-07-12 17:11:43 -07004400 const TX_SIZE uv_tx_size = av1_get_uv_tx_size(mbmi, &xd->plane[1]);
Yue Chena1e48dc2016-08-29 17:29:33 -07004401 int plane;
Yue Chena1e48dc2016-08-29 17:29:33 -07004402 int is_cost_valid = 1;
Angie Chiang284d7772016-11-08 11:06:45 -08004403 av1_init_rd_stats(rd_stats);
Yue Chena1e48dc2016-08-29 17:29:33 -07004404
4405 if (ref_best_rd < 0) is_cost_valid = 0;
Jingning Han9ce464c2017-02-20 15:36:30 -08004406
Jingning Han31b6a4f2017-02-23 11:05:53 -08004407#if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
Jingning Han9ce464c2017-02-20 15:36:30 -08004408 if (x->skip_chroma_rd) return is_cost_valid;
Jingning Han2d2dac22017-04-11 09:41:10 -07004409
4410 bsize = scale_chroma_bsize(bsize, xd->plane[1].subsampling_x,
4411 xd->plane[1].subsampling_y);
Fergus Simpson4063a682017-02-28 16:52:22 -08004412#endif // CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
Jingning Han9ce464c2017-02-20 15:36:30 -08004413
Yushin Cho77bba8d2016-11-04 16:36:56 -07004414#if !CONFIG_PVQ
Yue Chena1e48dc2016-08-29 17:29:33 -07004415 if (is_inter_block(mbmi) && is_cost_valid) {
Yue Chena1e48dc2016-08-29 17:29:33 -07004416 for (plane = 1; plane < MAX_MB_PLANE; ++plane)
4417 av1_subtract_plane(x, bsize, plane);
4418 }
Fergus Simpson4063a682017-02-28 16:52:22 -08004419#endif // !CONFIG_PVQ
Yue Chena1e48dc2016-08-29 17:29:33 -07004420
Yushin Cho09de28b2016-06-21 14:51:23 -07004421 if (is_cost_valid) {
4422 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
Angie Chiang7c2b7f22016-11-07 16:00:00 -08004423 RD_STATS pn_rd_stats;
4424 txfm_rd_in_plane(x, cpi, &pn_rd_stats, ref_best_rd, plane, bsize,
4425 uv_tx_size, cpi->sf.use_fast_coef_costing);
4426 if (pn_rd_stats.rate == INT_MAX) {
Yushin Cho09de28b2016-06-21 14:51:23 -07004427 is_cost_valid = 0;
4428 break;
4429 }
Angie Chiang284d7772016-11-08 11:06:45 -08004430 av1_merge_rd_stats(rd_stats, &pn_rd_stats);
Urvang Joshi70006e42017-06-14 16:08:55 -07004431 if (RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) > ref_best_rd &&
4432 RDCOST(x->rdmult, 0, rd_stats->sse) > ref_best_rd) {
Yushin Cho09de28b2016-06-21 14:51:23 -07004433 is_cost_valid = 0;
4434 break;
4435 }
Yue Chena1e48dc2016-08-29 17:29:33 -07004436 }
4437 }
4438
4439 if (!is_cost_valid) {
4440 // reset cost value
Angie Chiang284d7772016-11-08 11:06:45 -08004441 av1_invalid_rd_stats(rd_stats);
Yue Chena1e48dc2016-08-29 17:29:33 -07004442 }
4443
4444 return is_cost_valid;
4445}
4446
Yaowu Xuc27fc142016-08-22 16:08:15 -07004447#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -07004448void av1_tx_block_rd_b(const AV1_COMP *cpi, MACROBLOCK *x, TX_SIZE tx_size,
4449 int blk_row, int blk_col, int plane, int block,
Angie Chiang77368af2017-03-23 16:22:07 -07004450 int plane_bsize, const ENTROPY_CONTEXT *a,
4451 const ENTROPY_CONTEXT *l, RD_STATS *rd_stats) {
Angie Chiang22ba7512016-10-20 17:10:33 -07004452 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004453 MACROBLOCKD *xd = &x->e_mbd;
4454 const struct macroblock_plane *const p = &x->plane[plane];
4455 struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Han243b66b2017-06-23 12:11:47 -07004456
4457#if CONFIG_TXK_SEL
4458 av1_search_txk_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize,
4459 tx_size, a, l, 0, rd_stats);
4460 return;
4461#endif
4462
Yaowu Xuc27fc142016-08-22 16:08:15 -07004463 int64_t tmp;
4464 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
Sarah Parker99e7daa2017-08-29 10:30:13 -07004465#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
4466 uint8_t *mrc_mask = BLOCK_OFFSET(xd->mrc_mask, block);
4467#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Luc Trudeau005feb62017-02-22 13:34:01 -05004468 PLANE_TYPE plane_type = get_plane_type(plane);
Jingning Han19b5c8f2017-07-06 15:10:12 -07004469 TX_TYPE tx_type =
4470 av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
Urvang Joshi03f6fdc2016-10-14 15:53:39 -07004471 const SCAN_ORDER *const scan_order =
Angie Chiangbd99b382017-06-20 15:11:16 -07004472 get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004473 BLOCK_SIZE txm_bsize = txsize_to_bsize[tx_size];
Jingning Han9fdc4222016-10-27 21:32:19 -07004474 int bh = block_size_high[txm_bsize];
4475 int bw = block_size_wide[txm_bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004476 int src_stride = p->src.stride;
Jingning Han9ca05b72017-01-03 14:41:36 -08004477 uint8_t *src =
4478 &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
4479 uint8_t *dst =
4480 &pd->dst
4481 .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004482#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004483 DECLARE_ALIGNED(16, uint16_t, rec_buffer16[MAX_TX_SQUARE]);
4484 uint8_t *rec_buffer;
4485#else
4486 DECLARE_ALIGNED(16, uint8_t, rec_buffer[MAX_TX_SQUARE]);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004487#endif // CONFIG_HIGHBITDEPTH
Timothy B. Terriberryd62e2a32017-06-10 16:04:21 -07004488 const int diff_stride = block_size_wide[plane_bsize];
Jingning Han9ca05b72017-01-03 14:41:36 -08004489 const int16_t *diff =
4490 &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
Angie Chiangd81fdb42016-11-03 12:20:58 -07004491 int txb_coeff_cost;
Jingning Hand3fada82016-11-22 10:46:55 -08004492
4493 assert(tx_size < TX_SIZES_ALL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004494
Angie Chiang77368af2017-03-23 16:22:07 -07004495 int coeff_ctx = get_entropy_context(tx_size, a, l);
4496
Jingning Han1a7f0a82017-07-27 09:48:05 -07004497 tmp = pixel_diff_dist(x, plane, diff, diff_stride, blk_row, blk_col,
4498 plane_bsize, txm_bsize);
4499
4500#if CONFIG_HIGHBITDEPTH
4501 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
4502 tmp = ROUND_POWER_OF_TWO(tmp, (xd->bd - 8) * 2);
4503#endif // CONFIG_HIGHBITDEPTH
4504 rd_stats->sse += tmp << 4;
4505
4506 if (rd_stats->invalid_rate) {
4507 rd_stats->dist += tmp << 4;
4508 rd_stats->rate += rd_stats->zero_rate;
4509 rd_stats->skip = 1;
4510 return;
4511 }
4512
Angie Chiang808d8592017-04-06 18:36:55 -07004513// TODO(any): Use av1_dist_block to compute distortion
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004514#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004515 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
4516 rec_buffer = CONVERT_TO_BYTEPTR(rec_buffer16);
Yaowu Xuf883b422016-08-30 14:01:10 -07004517 aom_highbd_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL,
Jingning Han9fdc4222016-10-27 21:32:19 -07004518 0, NULL, 0, bw, bh, xd->bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004519 } else {
4520 rec_buffer = (uint8_t *)rec_buffer16;
Yaowu Xuf883b422016-08-30 14:01:10 -07004521 aom_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL, 0,
Jingning Han9fdc4222016-10-27 21:32:19 -07004522 NULL, 0, bw, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004523 }
4524#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004525 aom_convolve_copy(dst, pd->dst.stride, rec_buffer, MAX_TX_SIZE, NULL, 0, NULL,
Jingning Han9fdc4222016-10-27 21:32:19 -07004526 0, bw, bh);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004527#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004528
Jingning Han3bce7542017-07-25 10:53:57 -07004529#if DISABLE_TRELLISQ_SEARCH
4530 av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
4531 coeff_ctx, AV1_XFORM_QUANT_B);
4532
4533#else
4534 av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
4535 coeff_ctx, AV1_XFORM_QUANT_FP);
4536
4537 const int shift = (MAX_TX_SCALE - av1_get_tx_scale(tx_size)) * 2;
4538 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
4539 const int buffer_length = tx_size_2d[tx_size];
Jingning Han1a7f0a82017-07-27 09:48:05 -07004540 int64_t tmp_dist, tmp_sse;
Yunqing Wang24d2d5d2017-09-20 09:45:13 -07004541#if CONFIG_DIST_8X8
Yushin Choc00769a2017-09-14 14:44:30 -07004542 int disable_early_skip =
4543 x->using_dist_8x8 && plane == 0 && plane_bsize >= BLOCK_8X8 &&
4544 (tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4) &&
4545 x->tune_metric != AOM_TUNE_PSNR;
Yunqing Wang24d2d5d2017-09-20 09:45:13 -07004546#endif // CONFIG_DIST_8X8
4547
Jingning Han3bce7542017-07-25 10:53:57 -07004548#if CONFIG_HIGHBITDEPTH
4549 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
Jingning Han1a7f0a82017-07-27 09:48:05 -07004550 tmp_dist = av1_highbd_block_error(coeff, dqcoeff, buffer_length, &tmp_sse,
4551 xd->bd) >>
4552 shift;
Jingning Han3bce7542017-07-25 10:53:57 -07004553 else
4554#endif
Jingning Han1a7f0a82017-07-27 09:48:05 -07004555 tmp_dist =
4556 av1_block_error(coeff, dqcoeff, buffer_length, &tmp_sse) >> shift;
Jingning Han3bce7542017-07-25 10:53:57 -07004557
Sarah Parkerde6f0722017-08-07 20:23:46 -07004558#if CONFIG_MRC_TX
4559 if (tx_type == MRC_DCT && !xd->mi[0]->mbmi.valid_mrc_mask) {
4560 av1_invalid_rd_stats(rd_stats);
4561 return;
4562 }
4563#endif // CONFIG_MRC_TX
Yushin Choa4817a62017-07-27 13:09:43 -07004564 if (
4565#if CONFIG_DIST_8X8
Yushin Choc00769a2017-09-14 14:44:30 -07004566 disable_early_skip ||
Yushin Choa4817a62017-07-27 13:09:43 -07004567#endif
4568 RDCOST(x->rdmult, 0, tmp_dist) < rd_stats->ref_rdcost) {
Jingning Han3bce7542017-07-25 10:53:57 -07004569 av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size,
Angie Chiang25645b72017-09-24 14:28:49 -07004570 a, l, 1);
Jingning Han1a7f0a82017-07-27 09:48:05 -07004571 } else {
4572 rd_stats->rate += rd_stats->zero_rate;
4573 rd_stats->dist += tmp_sse >> shift;
4574 rd_stats->sse += tmp_sse >> shift;
4575 rd_stats->skip = 1;
4576 rd_stats->invalid_rate = 1;
4577 return;
Jingning Han3bce7542017-07-25 10:53:57 -07004578 }
4579#endif // DISABLE_TRELLISQ_SEARCH
4580
Angie Chiang41fffae2017-04-03 10:33:18 -07004581 const int eob = p->eobs[block];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004582
Lester Lu918fe692017-08-17 14:39:29 -07004583 av1_inverse_transform_block(xd, dqcoeff,
Lester Lu708c1ec2017-06-14 14:54:49 -07004584#if CONFIG_LGT
Lester Lu918fe692017-08-17 14:39:29 -07004585 xd->mi[0]->mbmi.mode,
Lester Lu708c1ec2017-06-14 14:54:49 -07004586#endif
Sarah Parker99e7daa2017-08-29 10:30:13 -07004587#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
4588 mrc_mask,
4589#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Lester Lu918fe692017-08-17 14:39:29 -07004590 tx_type, tx_size, rec_buffer, MAX_TX_SIZE, eob);
Angie Chiang41fffae2017-04-03 10:33:18 -07004591 if (eob > 0) {
Yushin Chob7b60c52017-07-14 16:18:52 -07004592#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -07004593 if (x->using_dist_8x8 && plane == 0 && (bw < 8 && bh < 8)) {
Yushin Cho04749122017-05-25 14:19:07 -07004594 // Save sub8x8 luma decoded pixels
4595 // since 8x8 luma decoded pixels are not available for daala-dist
4596 // after recursive split of BLOCK_8x8 is done.
4597 const int pred_stride = block_size_wide[plane_bsize];
4598 const int pred_idx = (blk_row * pred_stride + blk_col)
4599 << tx_size_wide_log2[0];
4600 int16_t *decoded = &pd->pred[pred_idx];
4601 int i, j;
4602
Yushin Cho8ab875d2017-06-23 14:47:21 -07004603#if CONFIG_HIGHBITDEPTH
4604 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
4605 for (j = 0; j < bh; j++)
4606 for (i = 0; i < bw; i++)
4607 decoded[j * pred_stride + i] =
4608 CONVERT_TO_SHORTPTR(rec_buffer)[j * MAX_TX_SIZE + i];
4609 } else {
4610#endif
4611 for (j = 0; j < bh; j++)
4612 for (i = 0; i < bw; i++)
4613 decoded[j * pred_stride + i] = rec_buffer[j * MAX_TX_SIZE + i];
4614#if CONFIG_HIGHBITDEPTH
4615 }
4616#endif // CONFIG_HIGHBITDEPTH
Yushin Cho04749122017-05-25 14:19:07 -07004617 }
Yushin Chob7b60c52017-07-14 16:18:52 -07004618#endif // CONFIG_DIST_8X8
Yushin Cho75b01002017-06-21 13:43:57 -07004619 tmp = pixel_dist(cpi, x, plane, src, src_stride, rec_buffer, MAX_TX_SIZE,
4620 blk_row, blk_col, plane_bsize, txm_bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004621 }
Angie Chiangb5dda482016-11-02 16:19:58 -07004622 rd_stats->dist += tmp * 16;
Jingning Han7eab9ff2017-07-06 10:12:54 -07004623 txb_coeff_cost = av1_cost_coeffs(cpi, x, plane, blk_row, blk_col, block,
4624 tx_size, scan_order, a, l, 0);
Angie Chiangd81fdb42016-11-03 12:20:58 -07004625 rd_stats->rate += txb_coeff_cost;
Angie Chiang41fffae2017-04-03 10:33:18 -07004626 rd_stats->skip &= (eob == 0);
Jingning Han63cbf342016-11-09 15:37:48 -08004627
Angie Chiangd81fdb42016-11-03 12:20:58 -07004628#if CONFIG_RD_DEBUG
Angie Chiange94556b2016-11-09 10:59:30 -08004629 av1_update_txb_coeff_cost(rd_stats, plane, tx_size, blk_row, blk_col,
4630 txb_coeff_cost);
Fergus Simpson4063a682017-02-28 16:52:22 -08004631#endif // CONFIG_RD_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07004632}
4633
Yaowu Xuf883b422016-08-30 14:01:10 -07004634static void select_tx_block(const AV1_COMP *cpi, MACROBLOCK *x, int blk_row,
Jingning Han63cbf342016-11-09 15:37:48 -08004635 int blk_col, int plane, int block, int block32,
4636 TX_SIZE tx_size, int depth, BLOCK_SIZE plane_bsize,
Jingning Han94d5bfc2016-10-21 10:14:36 -07004637 ENTROPY_CONTEXT *ta, ENTROPY_CONTEXT *tl,
4638 TXFM_CONTEXT *tx_above, TXFM_CONTEXT *tx_left,
Angie Chiangb5dda482016-11-02 16:19:58 -07004639 RD_STATS *rd_stats, int64_t ref_best_rd,
Jingning Han63cbf342016-11-09 15:37:48 -08004640 int *is_cost_valid, RD_STATS *rd_stats_stack) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004641 MACROBLOCKD *const xd = &x->e_mbd;
4642 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
4643 struct macroblock_plane *const p = &x->plane[plane];
4644 struct macroblockd_plane *const pd = &xd->plane[plane];
4645 const int tx_row = blk_row >> (1 - pd->subsampling_y);
4646 const int tx_col = blk_col >> (1 - pd->subsampling_x);
clang-format67948d32016-09-07 22:40:40 -07004647 TX_SIZE(*const inter_tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004648 [MAX_MIB_SIZE] =
4649 (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
Jingning Hanf65b8702016-10-31 12:13:20 -07004650 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
4651 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Jingning Han58224042016-10-27 16:35:32 -07004652 const int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004653 int64_t this_rd = INT64_MAX;
4654 ENTROPY_CONTEXT *pta = ta + blk_col;
4655 ENTROPY_CONTEXT *ptl = tl + blk_row;
Jingning Han5a995d72017-07-02 15:20:54 -07004656 int i;
Jingning Han331662e2017-05-30 17:03:32 -07004657 int ctx = txfm_partition_context(tx_above + blk_col, tx_left + blk_row,
4658 mbmi->sb_type, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004659 int64_t sum_rd = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004660 int tmp_eob = 0;
4661 int zero_blk_rate;
Angie Chiangd7246172016-11-03 11:49:15 -07004662 RD_STATS sum_rd_stats;
Jingning Hane3b81bc2017-06-23 11:43:52 -07004663#if CONFIG_TXK_SEL
4664 TX_TYPE best_tx_type = TX_TYPES;
Jingning Han19b5c8f2017-07-06 15:10:12 -07004665 int txk_idx = (blk_row << 4) + blk_col;
Jingning Hane3b81bc2017-06-23 11:43:52 -07004666#endif
Yue Chend6bdd462017-07-19 16:05:43 -07004667#if CONFIG_RECT_TX_EXT
4668 TX_SIZE quarter_txsize = quarter_txsize_lookup[mbmi->sb_type];
4669 int check_qttx = is_quarter_tx_allowed(xd, mbmi, is_inter_block(mbmi)) &&
4670 tx_size == max_txsize_rect_lookup[mbmi->sb_type] &&
4671 quarter_txsize != tx_size;
4672 int is_qttx_picked = 0;
4673 int eobs_qttx[2] = { 0, 0 };
4674 int skip_qttx[2] = { 0, 0 };
4675 int block_offset_qttx = check_qttx
4676 ? tx_size_wide_unit[quarter_txsize] *
4677 tx_size_high_unit[quarter_txsize]
4678 : 0;
4679 int blk_row_offset, blk_col_offset;
4680 int is_wide_qttx =
4681 tx_size_wide_unit[quarter_txsize] > tx_size_high_unit[quarter_txsize];
4682 blk_row_offset = is_wide_qttx ? tx_size_high_unit[quarter_txsize] : 0;
4683 blk_col_offset = is_wide_qttx ? 0 : tx_size_wide_unit[quarter_txsize];
4684#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004685
Jingning Han63cbf342016-11-09 15:37:48 -08004686 av1_init_rd_stats(&sum_rd_stats);
Jingning Hanfe45b212016-11-22 10:30:23 -08004687
Jingning Hand3fada82016-11-22 10:46:55 -08004688 assert(tx_size < TX_SIZES_ALL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004689
4690 if (ref_best_rd < 0) {
4691 *is_cost_valid = 0;
4692 return;
4693 }
4694
Angie Chiangc0feea82016-11-03 15:36:18 -07004695 av1_init_rd_stats(rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004696
4697 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
4698
Jingning Han5a995d72017-07-02 15:20:54 -07004699#if CONFIG_LV_MAP
4700 TX_SIZE txs_ctx = get_txsize_context(tx_size);
4701 TXB_CTX txb_ctx;
4702 get_txb_ctx(plane_bsize, tx_size, plane, pta, ptl, &txb_ctx);
Jingning Hane9814912017-08-31 16:38:59 -07004703
4704#if LV_MAP_PROB
4705 zero_blk_rate = x->coeff_costs[txs_ctx][get_plane_type(plane)]
4706 .txb_skip_cost[txb_ctx.txb_skip_ctx][1];
4707#else
Jingning Han5a995d72017-07-02 15:20:54 -07004708 zero_blk_rate =
4709 av1_cost_bit(xd->fc->txb_skip[txs_ctx][txb_ctx.txb_skip_ctx], 1);
Jingning Hane9814912017-08-31 16:38:59 -07004710#endif // LV_MAP_PROB
Jingning Han5a995d72017-07-02 15:20:54 -07004711#else
Yue Chend6bdd462017-07-19 16:05:43 -07004712 int tx_size_ctx = txsize_sqr_map[tx_size];
Jingning Han5a995d72017-07-02 15:20:54 -07004713 int coeff_ctx = get_entropy_context(tx_size, pta, ptl);
hui suc0cf71d2017-07-20 16:38:50 -07004714 zero_blk_rate =
4715 x->token_head_costs[tx_size_ctx][pd->plane_type][1][0][coeff_ctx][0];
Jingning Han5a995d72017-07-02 15:20:54 -07004716#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004717
Jingning Han3bce7542017-07-25 10:53:57 -07004718 rd_stats->ref_rdcost = ref_best_rd;
4719 rd_stats->zero_rate = zero_blk_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004720 if (cpi->common.tx_mode == TX_MODE_SELECT || tx_size == TX_4X4) {
4721 inter_tx_size[0][0] = tx_size;
Jingning Han63cbf342016-11-09 15:37:48 -08004722
Sarah Parkerde6f0722017-08-07 20:23:46 -07004723#if CONFIG_MRC_TX
4724 if (tx_size == TX_32X32 && mbmi->tx_type != DCT_DCT &&
4725 rd_stats_stack[block32].rate != INT_MAX && !USE_MRC_INTER) {
4726#else
Jingning Han63cbf342016-11-09 15:37:48 -08004727 if (tx_size == TX_32X32 && mbmi->tx_type != DCT_DCT &&
4728 rd_stats_stack[block32].rate != INT_MAX) {
Sarah Parkerde6f0722017-08-07 20:23:46 -07004729#endif // CONFIG_MRC_TX
Jingning Han63cbf342016-11-09 15:37:48 -08004730 *rd_stats = rd_stats_stack[block32];
4731 p->eobs[block] = !rd_stats->skip;
4732 x->blk_skip[plane][blk_row * bw + blk_col] = rd_stats->skip;
4733 } else {
4734 av1_tx_block_rd_b(cpi, x, tx_size, blk_row, blk_col, plane, block,
Angie Chiang77368af2017-03-23 16:22:07 -07004735 plane_bsize, pta, ptl, rd_stats);
Jingning Han63cbf342016-11-09 15:37:48 -08004736 if (tx_size == TX_32X32) {
4737 rd_stats_stack[block32] = *rd_stats;
4738 }
4739 }
Sarah Parkerde6f0722017-08-07 20:23:46 -07004740 if (rd_stats->rate == INT_MAX) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004741
Urvang Joshi70006e42017-06-14 16:08:55 -07004742 if ((RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist) >=
4743 RDCOST(x->rdmult, zero_blk_rate, rd_stats->sse) ||
Angie Chiangb5dda482016-11-02 16:19:58 -07004744 rd_stats->skip == 1) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004745 !xd->lossless[mbmi->segment_id]) {
Jingning Hanc7ea7612017-01-11 15:01:30 -08004746#if CONFIG_RD_DEBUG
4747 av1_update_txb_coeff_cost(rd_stats, plane, tx_size, blk_row, blk_col,
4748 zero_blk_rate - rd_stats->rate);
Fergus Simpson4063a682017-02-28 16:52:22 -08004749#endif // CONFIG_RD_DEBUG
Angie Chiangb5dda482016-11-02 16:19:58 -07004750 rd_stats->rate = zero_blk_rate;
4751 rd_stats->dist = rd_stats->sse;
4752 rd_stats->skip = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004753 x->blk_skip[plane][blk_row * bw + blk_col] = 1;
4754 p->eobs[block] = 0;
Jingning Han19b5c8f2017-07-06 15:10:12 -07004755#if CONFIG_TXK_SEL
4756 mbmi->txk_type[txk_idx] = DCT_DCT;
4757#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004758 } else {
4759 x->blk_skip[plane][blk_row * bw + blk_col] = 0;
Angie Chiangb5dda482016-11-02 16:19:58 -07004760 rd_stats->skip = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004761 }
4762
Jingning Han571189c2016-10-24 10:38:43 -07004763 if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH)
Angie Chiangb5dda482016-11-02 16:19:58 -07004764 rd_stats->rate +=
4765 av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 0);
Yue Chend6bdd462017-07-19 16:05:43 -07004766#if CONFIG_RECT_TX_EXT
4767 if (check_qttx) {
4768 assert(blk_row == 0 && blk_col == 0);
4769 rd_stats->rate += av1_cost_bit(cpi->common.fc->quarter_tx_size_prob, 0);
4770 }
4771#endif
Urvang Joshi70006e42017-06-14 16:08:55 -07004772 this_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist);
Jingning Han2f42d772017-07-05 16:28:18 -07004773#if CONFIG_LV_MAP
4774 tmp_eob = p->txb_entropy_ctx[block];
4775#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004776 tmp_eob = p->eobs[block];
Jingning Han2f42d772017-07-05 16:28:18 -07004777#endif
4778
Jingning Hane3b81bc2017-06-23 11:43:52 -07004779#if CONFIG_TXK_SEL
4780 best_tx_type = mbmi->txk_type[txk_idx];
4781#endif
Yue Chend6bdd462017-07-19 16:05:43 -07004782
4783#if CONFIG_RECT_TX_EXT
4784 if (check_qttx) {
4785 assert(blk_row == 0 && blk_col == 0 && block == 0 && plane == 0);
4786
4787 RD_STATS rd_stats_tmp, rd_stats_qttx;
4788 int64_t rd_qttx;
4789
4790 av1_init_rd_stats(&rd_stats_qttx);
4791 av1_init_rd_stats(&rd_stats_tmp);
4792
4793 av1_tx_block_rd_b(cpi, x, quarter_txsize, 0, 0, plane, 0, plane_bsize,
4794 pta, ptl, &rd_stats_qttx);
Sarah Parkerde6f0722017-08-07 20:23:46 -07004795 if (rd_stats->rate == INT_MAX) return;
Yue Chend6bdd462017-07-19 16:05:43 -07004796
4797 tx_size_ctx = txsize_sqr_map[quarter_txsize];
4798 coeff_ctx = get_entropy_context(quarter_txsize, pta, ptl);
hui suc0cf71d2017-07-20 16:38:50 -07004799 zero_blk_rate =
4800 x->token_head_costs[tx_size_ctx][pd->plane_type][1][0][coeff_ctx][0];
Yue Chend6bdd462017-07-19 16:05:43 -07004801 if ((RDCOST(x->rdmult, rd_stats_qttx.rate, rd_stats_qttx.dist) >=
4802 RDCOST(x->rdmult, zero_blk_rate, rd_stats_qttx.sse) ||
4803 rd_stats_qttx.skip == 1) &&
4804 !xd->lossless[mbmi->segment_id]) {
4805#if CONFIG_RD_DEBUG
4806 av1_update_txb_coeff_cost(&rd_stats_qttx, plane, quarter_txsize, 0, 0,
4807 zero_blk_rate - rd_stats_qttx.rate);
4808#endif // CONFIG_RD_DEBUG
4809 rd_stats_qttx.rate = zero_blk_rate;
4810 rd_stats_qttx.dist = rd_stats_qttx.sse;
4811 rd_stats_qttx.skip = 1;
4812 x->blk_skip[plane][blk_row * bw + blk_col] = 1;
4813 skip_qttx[0] = 1;
4814 p->eobs[block] = 0;
4815 } else {
4816 x->blk_skip[plane][blk_row * bw + blk_col] = 0;
4817 skip_qttx[0] = 0;
4818 rd_stats->skip = 0;
4819 }
4820
4821 // Second tx block
4822 av1_tx_block_rd_b(cpi, x, quarter_txsize, blk_row_offset, blk_col_offset,
4823 plane, block_offset_qttx, plane_bsize, pta, ptl,
4824 &rd_stats_tmp);
4825
Sarah Parkerde6f0722017-08-07 20:23:46 -07004826 if (rd_stats->rate == INT_MAX) return;
4827
Tom Fineganac870492017-08-15 16:19:13 -07004828#if !CONFIG_PVQ
Yue Chend6bdd462017-07-19 16:05:43 -07004829 av1_set_txb_context(x, plane, 0, quarter_txsize, pta, ptl);
Tom Fineganac870492017-08-15 16:19:13 -07004830#endif // !CONFIG_PVQ
Yue Chend6bdd462017-07-19 16:05:43 -07004831 coeff_ctx = get_entropy_context(quarter_txsize, pta + blk_col_offset,
4832 ptl + blk_row_offset);
hui suc0cf71d2017-07-20 16:38:50 -07004833 zero_blk_rate =
4834 x->token_head_costs[tx_size_ctx][pd->plane_type][1][0][coeff_ctx][0];
Yue Chend6bdd462017-07-19 16:05:43 -07004835 if ((RDCOST(x->rdmult, rd_stats_tmp.rate, rd_stats_tmp.dist) >=
4836 RDCOST(x->rdmult, zero_blk_rate, rd_stats_tmp.sse) ||
4837 rd_stats_tmp.skip == 1) &&
4838 !xd->lossless[mbmi->segment_id]) {
4839#if CONFIG_RD_DEBUG
4840 av1_update_txb_coeff_cost(&rd_stats_tmp, plane, quarter_txsize, 0, 0,
4841 zero_blk_rate - rd_stats_tmp.rate);
4842#endif // CONFIG_RD_DEBUG
4843 rd_stats_tmp.rate = zero_blk_rate;
4844 rd_stats_tmp.dist = rd_stats_tmp.sse;
4845 rd_stats_tmp.skip = 1;
4846 x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = 1;
4847 skip_qttx[1] = 1;
4848 p->eobs[block_offset_qttx] = 0;
4849 } else {
4850 x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = 0;
4851 skip_qttx[1] = 0;
4852 rd_stats_tmp.skip = 0;
4853 }
4854
4855 av1_merge_rd_stats(&rd_stats_qttx, &rd_stats_tmp);
4856
4857 if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) {
4858 rd_stats_qttx.rate +=
4859 av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 0);
4860 }
4861 rd_stats_qttx.rate +=
4862 av1_cost_bit(cpi->common.fc->quarter_tx_size_prob, 1);
4863 rd_qttx = RDCOST(x->rdmult, rd_stats_qttx.rate, rd_stats_qttx.dist);
4864#if CONFIG_LV_MAP
4865 eobs_qttx[0] = p->txb_entropy_ctx[0];
4866 eobs_qttx[1] = p->txb_entropy_ctx[block_offset_qttx];
4867#else
4868 eobs_qttx[0] = p->eobs[0];
4869 eobs_qttx[1] = p->eobs[block_offset_qttx];
4870#endif
4871 if (rd_qttx < this_rd) {
4872 is_qttx_picked = 1;
4873 this_rd = rd_qttx;
4874 rd_stats->rate = rd_stats_qttx.rate;
4875 rd_stats->dist = rd_stats_qttx.dist;
4876 rd_stats->sse = rd_stats_qttx.sse;
4877 rd_stats->skip = rd_stats_qttx.skip;
4878 rd_stats->rdcost = rd_stats_qttx.rdcost;
4879 }
4880 av1_get_entropy_contexts(plane_bsize, 0, pd, ta, tl);
4881 }
4882#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004883 }
4884
Sarah Parker53f93db2017-07-11 17:20:04 -07004885#if CONFIG_MRC_TX
4886 // If the tx type we are trying is MRC_DCT, we cannot partition the transform
4887 // into anything smaller than TX_32X32
4888 if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH && mbmi->tx_type != MRC_DCT) {
4889#else
Jingning Han571189c2016-10-24 10:38:43 -07004890 if (tx_size > TX_4X4 && depth < MAX_VARTX_DEPTH) {
Sarah Parker53f93db2017-07-11 17:20:04 -07004891#endif
Jingning Han18482fe2016-11-02 17:01:58 -07004892 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
4893 const int bsl = tx_size_wide_unit[sub_txs];
Jingning Han58224042016-10-27 16:35:32 -07004894 int sub_step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
Angie Chiangb5dda482016-11-02 16:19:58 -07004895 RD_STATS this_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004896 int this_cost_valid = 1;
4897 int64_t tmp_rd = 0;
Yushin Chob7b60c52017-07-14 16:18:52 -07004898#if CONFIG_DIST_8X8
Yushin Cho04749122017-05-25 14:19:07 -07004899 int sub8x8_eob[4];
4900#endif
Angie Chiangd7246172016-11-03 11:49:15 -07004901 sum_rd_stats.rate =
4902 av1_cost_bit(cpi->common.fc->txfm_partition_prob[ctx], 1);
Jingning Hand3fada82016-11-22 10:46:55 -08004903
4904 assert(tx_size < TX_SIZES_ALL);
4905
Jingning Han16a9df72017-07-26 15:27:43 -07004906 ref_best_rd = AOMMIN(this_rd, ref_best_rd);
4907
Yaowu Xuc27fc142016-08-22 16:08:15 -07004908 for (i = 0; i < 4 && this_cost_valid; ++i) {
Jingning Han98d6a1f2016-11-03 12:47:47 -07004909 int offsetr = blk_row + (i >> 1) * bsl;
4910 int offsetc = blk_col + (i & 0x01) * bsl;
4911
4912 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
4913
Jingning Han63cbf342016-11-09 15:37:48 -08004914 select_tx_block(cpi, x, offsetr, offsetc, plane, block, block32, sub_txs,
Jingning Han98d6a1f2016-11-03 12:47:47 -07004915 depth + 1, plane_bsize, ta, tl, tx_above, tx_left,
Jingning Han63cbf342016-11-09 15:37:48 -08004916 &this_rd_stats, ref_best_rd - tmp_rd, &this_cost_valid,
4917 rd_stats_stack);
Yushin Chob7b60c52017-07-14 16:18:52 -07004918#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -07004919 if (x->using_dist_8x8 && plane == 0 && tx_size == TX_8X8) {
Yushin Cho04749122017-05-25 14:19:07 -07004920 sub8x8_eob[i] = p->eobs[block];
4921 }
Yushin Chob7b60c52017-07-14 16:18:52 -07004922#endif // CONFIG_DIST_8X8
Angie Chiangc0feea82016-11-03 15:36:18 -07004923 av1_merge_rd_stats(&sum_rd_stats, &this_rd_stats);
Jingning Han98d6a1f2016-11-03 12:47:47 -07004924
Urvang Joshi70006e42017-06-14 16:08:55 -07004925 tmp_rd = RDCOST(x->rdmult, sum_rd_stats.rate, sum_rd_stats.dist);
Yushin Cho55104332017-08-14 16:15:43 -07004926#if CONFIG_DIST_8X8
4927 if (!x->using_dist_8x8)
Yushin Cho04749122017-05-25 14:19:07 -07004928#endif
Yushin Cho55104332017-08-14 16:15:43 -07004929 if (this_rd < tmp_rd) break;
Jingning Han98d6a1f2016-11-03 12:47:47 -07004930 block += sub_step;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004931 }
Yushin Chob7b60c52017-07-14 16:18:52 -07004932#if CONFIG_DIST_8X8
Yushin Cho55104332017-08-14 16:15:43 -07004933 if (x->using_dist_8x8 && this_cost_valid && plane == 0 &&
4934 tx_size == TX_8X8) {
Yushin Cho04749122017-05-25 14:19:07 -07004935 const int src_stride = p->src.stride;
4936 const int dst_stride = pd->dst.stride;
4937
4938 const uint8_t *src =
4939 &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
4940 const uint8_t *dst =
4941 &pd->dst
4942 .buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
4943
Yushin Chob7b60c52017-07-14 16:18:52 -07004944 int64_t dist_8x8;
Yushin Cho04749122017-05-25 14:19:07 -07004945 int qindex = x->qindex;
4946 const int pred_stride = block_size_wide[plane_bsize];
4947 const int pred_idx = (blk_row * pred_stride + blk_col)
4948 << tx_size_wide_log2[0];
4949 int16_t *pred = &pd->pred[pred_idx];
4950 int j;
Yushin Cho04749122017-05-25 14:19:07 -07004951 int row, col;
4952
Yushin Cho8ab875d2017-06-23 14:47:21 -07004953#if CONFIG_HIGHBITDEPTH
4954 uint8_t *pred8;
4955 DECLARE_ALIGNED(16, uint16_t, pred8_16[8 * 8]);
4956#else
Yushin Cho04749122017-05-25 14:19:07 -07004957 DECLARE_ALIGNED(16, uint8_t, pred8[8 * 8]);
Yushin Cho8ab875d2017-06-23 14:47:21 -07004958#endif // CONFIG_HIGHBITDEPTH
Yushin Cho04749122017-05-25 14:19:07 -07004959
Yushin Choe30a47c2017-08-15 13:08:30 -07004960 dist_8x8 = av1_dist_8x8(cpi, x, src, src_stride, dst, dst_stride,
Yushin Chob7b60c52017-07-14 16:18:52 -07004961 BLOCK_8X8, 8, 8, 8, 8, qindex) *
4962 16;
4963 sum_rd_stats.sse = dist_8x8;
Yushin Cho04749122017-05-25 14:19:07 -07004964
Yushin Cho8ab875d2017-06-23 14:47:21 -07004965#if CONFIG_HIGHBITDEPTH
4966 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
4967 pred8 = CONVERT_TO_BYTEPTR(pred8_16);
4968 else
4969 pred8 = (uint8_t *)pred8_16;
4970#endif
Yushin Cho04749122017-05-25 14:19:07 -07004971
Yushin Cho8ab875d2017-06-23 14:47:21 -07004972#if CONFIG_HIGHBITDEPTH
4973 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
4974 for (row = 0; row < 2; ++row) {
4975 for (col = 0; col < 2; ++col) {
4976 int idx = row * 2 + col;
4977 int eob = sub8x8_eob[idx];
4978
4979 if (eob > 0) {
4980 for (j = 0; j < 4; j++)
4981 for (i = 0; i < 4; i++)
4982 CONVERT_TO_SHORTPTR(pred8)
4983 [(row * 4 + j) * 8 + 4 * col + i] =
4984 pred[(row * 4 + j) * pred_stride + 4 * col + i];
4985 } else {
4986 for (j = 0; j < 4; j++)
4987 for (i = 0; i < 4; i++)
4988 CONVERT_TO_SHORTPTR(pred8)
4989 [(row * 4 + j) * 8 + 4 * col + i] = CONVERT_TO_SHORTPTR(
4990 dst)[(row * 4 + j) * dst_stride + 4 * col + i];
4991 }
Yushin Cho04749122017-05-25 14:19:07 -07004992 }
4993 }
Yushin Cho8ab875d2017-06-23 14:47:21 -07004994 } else {
4995#endif
4996 for (row = 0; row < 2; ++row) {
4997 for (col = 0; col < 2; ++col) {
4998 int idx = row * 2 + col;
4999 int eob = sub8x8_eob[idx];
5000
5001 if (eob > 0) {
5002 for (j = 0; j < 4; j++)
5003 for (i = 0; i < 4; i++)
5004 pred8[(row * 4 + j) * 8 + 4 * col + i] =
Yaowu Xu7a471702017-09-29 08:38:37 -07005005 (uint8_t)pred[(row * 4 + j) * pred_stride + 4 * col + i];
Yushin Cho8ab875d2017-06-23 14:47:21 -07005006 } else {
5007 for (j = 0; j < 4; j++)
5008 for (i = 0; i < 4; i++)
5009 pred8[(row * 4 + j) * 8 + 4 * col + i] =
5010 dst[(row * 4 + j) * dst_stride + 4 * col + i];
5011 }
5012 }
5013 }
5014#if CONFIG_HIGHBITDEPTH
Yushin Cho04749122017-05-25 14:19:07 -07005015 }
Yushin Cho8ab875d2017-06-23 14:47:21 -07005016#endif // CONFIG_HIGHBITDEPTH
Yushin Choe30a47c2017-08-15 13:08:30 -07005017 dist_8x8 = av1_dist_8x8(cpi, x, src, src_stride, pred8, 8, BLOCK_8X8, 8,
Yushin Chob7b60c52017-07-14 16:18:52 -07005018 8, 8, 8, qindex) *
5019 16;
5020 sum_rd_stats.dist = dist_8x8;
Yushin Cho04749122017-05-25 14:19:07 -07005021 tmp_rd = RDCOST(x->rdmult, sum_rd_stats.rate, sum_rd_stats.dist);
5022 }
Yushin Chob7b60c52017-07-14 16:18:52 -07005023#endif // CONFIG_DIST_8X8
Yaowu Xuc27fc142016-08-22 16:08:15 -07005024 if (this_cost_valid) sum_rd = tmp_rd;
5025 }
5026
5027 if (this_rd < sum_rd) {
5028 int idx, idy;
Yue Chend6bdd462017-07-19 16:05:43 -07005029#if CONFIG_RECT_TX_EXT
5030 TX_SIZE tx_size_selected = is_qttx_picked ? quarter_txsize : tx_size;
5031#else
5032 TX_SIZE tx_size_selected = tx_size;
5033#endif
Jingning Han2f42d772017-07-05 16:28:18 -07005034
Yue Chend6bdd462017-07-19 16:05:43 -07005035#if CONFIG_RECT_TX_EXT
5036 if (is_qttx_picked) {
5037 assert(blk_row == 0 && blk_col == 0 && plane == 0);
Jingning Han2f42d772017-07-05 16:28:18 -07005038#if CONFIG_LV_MAP
Yue Chend6bdd462017-07-19 16:05:43 -07005039 p->txb_entropy_ctx[0] = eobs_qttx[0];
5040 p->txb_entropy_ctx[block_offset_qttx] = eobs_qttx[1];
5041#else
5042 p->eobs[0] = eobs_qttx[0];
5043 p->eobs[block_offset_qttx] = eobs_qttx[1];
5044#endif
5045 } else {
5046#endif
5047#if CONFIG_LV_MAP
5048 p->txb_entropy_ctx[block] = tmp_eob;
Jingning Han2f42d772017-07-05 16:28:18 -07005049#else
5050 p->eobs[block] = tmp_eob;
5051#endif
Yue Chend6bdd462017-07-19 16:05:43 -07005052#if CONFIG_RECT_TX_EXT
5053 }
5054#endif
Jingning Han2f42d772017-07-05 16:28:18 -07005055
Tom Fineganac870492017-08-15 16:19:13 -07005056#if !CONFIG_PVQ
Yue Chend6bdd462017-07-19 16:05:43 -07005057 av1_set_txb_context(x, plane, block, tx_size_selected, pta, ptl);
5058#if CONFIG_RECT_TX_EXT
5059 if (is_qttx_picked)
5060 av1_set_txb_context(x, plane, block_offset_qttx, tx_size_selected,
5061 pta + blk_col_offset, ptl + blk_row_offset);
Tom Fineganac870492017-08-15 16:19:13 -07005062#endif // CONFIG_RECT_TX_EXT
5063#endif // !CONFIG_PVQ
Jingning Han2f42d772017-07-05 16:28:18 -07005064
Jingning Han331662e2017-05-30 17:03:32 -07005065 txfm_partition_update(tx_above + blk_col, tx_left + blk_row, tx_size,
5066 tx_size);
Yue Chend6bdd462017-07-19 16:05:43 -07005067 inter_tx_size[0][0] = tx_size_selected;
Jingning Han58224042016-10-27 16:35:32 -07005068 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
5069 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Yue Chend6bdd462017-07-19 16:05:43 -07005070 inter_tx_size[idy][idx] = tx_size_selected;
5071 mbmi->tx_size = tx_size_selected;
Jingning Hane3b81bc2017-06-23 11:43:52 -07005072#if CONFIG_TXK_SEL
5073 mbmi->txk_type[txk_idx] = best_tx_type;
5074#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005075 if (this_rd == INT64_MAX) *is_cost_valid = 0;
Yue Chend6bdd462017-07-19 16:05:43 -07005076#if CONFIG_RECT_TX_EXT
5077 if (is_qttx_picked) {
5078 x->blk_skip[plane][0] = skip_qttx[0];
5079 x->blk_skip[plane][blk_row_offset * bw + blk_col_offset] = skip_qttx[1];
5080 } else {
5081#endif
5082 x->blk_skip[plane][blk_row * bw + blk_col] = rd_stats->skip;
5083#if CONFIG_RECT_TX_EXT
5084 }
5085#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005086 } else {
Angie Chiangd7246172016-11-03 11:49:15 -07005087 *rd_stats = sum_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005088 if (sum_rd == INT64_MAX) *is_cost_valid = 0;
5089 }
5090}
5091
Angie Chiangb5dda482016-11-02 16:19:58 -07005092static void inter_block_yrd(const AV1_COMP *cpi, MACROBLOCK *x,
5093 RD_STATS *rd_stats, BLOCK_SIZE bsize,
Jingning Han63cbf342016-11-09 15:37:48 -08005094 int64_t ref_best_rd, RD_STATS *rd_stats_stack) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005095 MACROBLOCKD *const xd = &x->e_mbd;
5096 int is_cost_valid = 1;
5097 int64_t this_rd = 0;
5098
5099 if (ref_best_rd < 0) is_cost_valid = 0;
5100
Angie Chiangc0feea82016-11-03 15:36:18 -07005101 av1_init_rd_stats(rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005102
5103 if (is_cost_valid) {
5104 const struct macroblockd_plane *const pd = &xd->plane[0];
5105 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han9ca05b72017-01-03 14:41:36 -08005106 const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
5107 const int mi_height = block_size_high[plane_bsize] >> tx_size_high_log2[0];
Jingning Han70e5f3f2016-11-09 17:03:07 -08005108 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Han18482fe2016-11-02 17:01:58 -07005109 const int bh = tx_size_high_unit[max_tx_size];
5110 const int bw = tx_size_wide_unit[max_tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005111 int idx, idy;
5112 int block = 0;
Jingning Han63cbf342016-11-09 15:37:48 -08005113 int block32 = 0;
Jingning Han18482fe2016-11-02 17:01:58 -07005114 int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005115 ENTROPY_CONTEXT ctxa[2 * MAX_MIB_SIZE];
5116 ENTROPY_CONTEXT ctxl[2 * MAX_MIB_SIZE];
Jingning Han331662e2017-05-30 17:03:32 -07005117 TXFM_CONTEXT tx_above[MAX_MIB_SIZE * 2];
5118 TXFM_CONTEXT tx_left[MAX_MIB_SIZE * 2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005119
Angie Chiangb5dda482016-11-02 16:19:58 -07005120 RD_STATS pn_rd_stats;
Angie Chiangc0feea82016-11-03 15:36:18 -07005121 av1_init_rd_stats(&pn_rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005122
Jingning Han9ca05b72017-01-03 14:41:36 -08005123 av1_get_entropy_contexts(bsize, 0, pd, ctxa, ctxl);
Jingning Han331662e2017-05-30 17:03:32 -07005124 memcpy(tx_above, xd->above_txfm_context, sizeof(TXFM_CONTEXT) * mi_width);
5125 memcpy(tx_left, xd->left_txfm_context, sizeof(TXFM_CONTEXT) * mi_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005126
5127 for (idy = 0; idy < mi_height; idy += bh) {
Jingning Han18482fe2016-11-02 17:01:58 -07005128 for (idx = 0; idx < mi_width; idx += bw) {
Jingning Han63cbf342016-11-09 15:37:48 -08005129 select_tx_block(cpi, x, idy, idx, 0, block, block32, max_tx_size,
Jingning Han18482fe2016-11-02 17:01:58 -07005130 mi_height != mi_width, plane_bsize, ctxa, ctxl,
Angie Chiangb5dda482016-11-02 16:19:58 -07005131 tx_above, tx_left, &pn_rd_stats, ref_best_rd - this_rd,
Jingning Han63cbf342016-11-09 15:37:48 -08005132 &is_cost_valid, rd_stats_stack);
Sarah Parkerde6f0722017-08-07 20:23:46 -07005133 if (pn_rd_stats.rate == INT_MAX) {
5134 av1_invalid_rd_stats(rd_stats);
5135 return;
5136 }
Angie Chiangc0feea82016-11-03 15:36:18 -07005137 av1_merge_rd_stats(rd_stats, &pn_rd_stats);
Urvang Joshi70006e42017-06-14 16:08:55 -07005138 this_rd += AOMMIN(RDCOST(x->rdmult, pn_rd_stats.rate, pn_rd_stats.dist),
5139 RDCOST(x->rdmult, 0, pn_rd_stats.sse));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005140 block += step;
Jingning Han63cbf342016-11-09 15:37:48 -08005141 ++block32;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005142 }
5143 }
5144 }
5145
Urvang Joshi70006e42017-06-14 16:08:55 -07005146 this_rd = AOMMIN(RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist),
5147 RDCOST(x->rdmult, 0, rd_stats->sse));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005148 if (this_rd > ref_best_rd) is_cost_valid = 0;
5149
5150 if (!is_cost_valid) {
5151 // reset cost value
Angie Chiangc0feea82016-11-03 15:36:18 -07005152 av1_invalid_rd_stats(rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005153 }
5154}
5155
Yaowu Xuf883b422016-08-30 14:01:10 -07005156static int64_t select_tx_size_fix_type(const AV1_COMP *cpi, MACROBLOCK *x,
Angie Chiangb5dda482016-11-02 16:19:58 -07005157 RD_STATS *rd_stats, BLOCK_SIZE bsize,
Jingning Han63cbf342016-11-09 15:37:48 -08005158 int64_t ref_best_rd, TX_TYPE tx_type,
5159 RD_STATS *rd_stats_stack) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005160 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005161 MACROBLOCKD *const xd = &x->e_mbd;
5162 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005163 const int is_inter = is_inter_block(mbmi);
Yaowu Xuf883b422016-08-30 14:01:10 -07005164 aom_prob skip_prob = av1_get_skip_prob(cm, xd);
5165 int s0 = av1_cost_bit(skip_prob, 0);
5166 int s1 = av1_cost_bit(skip_prob, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005167 int64_t rd;
Jingning Hane67b38a2016-11-04 10:30:00 -07005168 int row, col;
5169 const int max_blocks_high = max_block_high(xd, bsize, 0);
5170 const int max_blocks_wide = max_block_wide(xd, bsize, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005171
5172 mbmi->tx_type = tx_type;
Jingning Han63cbf342016-11-09 15:37:48 -08005173 inter_block_yrd(cpi, x, rd_stats, bsize, ref_best_rd, rd_stats_stack);
Jingning Han70bd76e2017-05-30 10:53:00 -07005174 mbmi->min_tx_size = get_min_tx_size(mbmi->inter_tx_size[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005175
Angie Chiangb5dda482016-11-02 16:19:58 -07005176 if (rd_stats->rate == INT_MAX) return INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005177
Jingning Hane67b38a2016-11-04 10:30:00 -07005178 for (row = 0; row < max_blocks_high / 2; ++row)
5179 for (col = 0; col < max_blocks_wide / 2; ++col)
5180 mbmi->min_tx_size = AOMMIN(
5181 mbmi->min_tx_size, get_min_tx_size(mbmi->inter_tx_size[row][col]));
5182
Jingning Han1643a0a2017-07-05 15:48:25 -07005183#if !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07005184#if CONFIG_EXT_TX
Sarah Parkere68a3e42017-02-16 14:03:24 -08005185 if (get_ext_tx_types(mbmi->min_tx_size, bsize, is_inter,
5186 cm->reduced_tx_set_used) > 1 &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005187 !xd->lossless[xd->mi[0]->mbmi.segment_id]) {
Sarah Parkere68a3e42017-02-16 14:03:24 -08005188 const int ext_tx_set = get_ext_tx_set(mbmi->min_tx_size, bsize, is_inter,
5189 cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005190 if (is_inter) {
5191 if (ext_tx_set > 0)
Angie Chiangb5dda482016-11-02 16:19:58 -07005192 rd_stats->rate +=
Yue Chenb23d00a2017-07-28 17:01:21 -07005193 x->inter_tx_type_costs[ext_tx_set]
5194 [txsize_sqr_map[mbmi->min_tx_size]]
5195 [mbmi->tx_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005196 } else {
5197 if (ext_tx_set > 0 && ALLOW_INTRA_EXT_TX)
Yue Chenb23d00a2017-07-28 17:01:21 -07005198 rd_stats->rate += x->intra_tx_type_costs[ext_tx_set][mbmi->min_tx_size]
5199 [mbmi->mode][mbmi->tx_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005200 }
5201 }
Jingning Han1643a0a2017-07-05 15:48:25 -07005202#else
Jingning Hane67b38a2016-11-04 10:30:00 -07005203 if (mbmi->min_tx_size < TX_32X32 && !xd->lossless[xd->mi[0]->mbmi.segment_id])
Zoe Liu4508d882017-07-31 15:36:55 -07005204 rd_stats->rate += x->inter_tx_type_costs[mbmi->min_tx_size][mbmi->tx_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005205#endif // CONFIG_EXT_TX
Jingning Han1643a0a2017-07-05 15:48:25 -07005206#endif // CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07005207
Angie Chiangb5dda482016-11-02 16:19:58 -07005208 if (rd_stats->skip)
Urvang Joshi70006e42017-06-14 16:08:55 -07005209 rd = RDCOST(x->rdmult, s1, rd_stats->sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005210 else
Urvang Joshi70006e42017-06-14 16:08:55 -07005211 rd = RDCOST(x->rdmult, rd_stats->rate + s0, rd_stats->dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005212
Angie Chiangb5dda482016-11-02 16:19:58 -07005213 if (is_inter && !xd->lossless[xd->mi[0]->mbmi.segment_id] &&
5214 !(rd_stats->skip))
Urvang Joshi70006e42017-06-14 16:08:55 -07005215 rd = AOMMIN(rd, RDCOST(x->rdmult, s1, rd_stats->sse));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005216
5217 return rd;
5218}
5219
Hui Su1ddf2312017-08-19 15:21:34 -07005220static uint32_t get_block_residue_hash(MACROBLOCK *x, BLOCK_SIZE bsize) {
5221 const int rows = block_size_high[bsize];
5222 const int cols = block_size_wide[bsize];
5223 const int diff_stride = cols;
5224 const struct macroblock_plane *const p = &x->plane[0];
5225 const int16_t *diff = &p->src_diff[0];
5226 uint8_t hash_data[MAX_SB_SQUARE];
5227 for (int r = 0; r < rows; ++r) {
5228 for (int c = 0; c < cols; ++c) {
5229 hash_data[cols * r + c] = clip_pixel(diff[c] + 128);
5230 }
5231 diff += diff_stride;
5232 }
5233 return (av1_get_crc_value(&x->tx_rd_record.crc_calculator, hash_data,
5234 rows * cols)
5235 << 7) +
5236 bsize;
5237}
5238
5239static void save_tx_rd_info(int n4, uint32_t hash, const MACROBLOCK *const x,
5240 const RD_STATS *const rd_stats,
5241 TX_RD_INFO *const tx_rd_info) {
5242 const MACROBLOCKD *const xd = &x->e_mbd;
5243 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
5244 tx_rd_info->hash_value = hash;
5245 tx_rd_info->tx_type = mbmi->tx_type;
5246 tx_rd_info->tx_size = mbmi->tx_size;
5247#if CONFIG_VAR_TX
5248 tx_rd_info->min_tx_size = mbmi->min_tx_size;
5249 memcpy(tx_rd_info->blk_skip, x->blk_skip[0],
5250 sizeof(tx_rd_info->blk_skip[0]) * n4);
5251 for (int idy = 0; idy < xd->n8_h; ++idy)
5252 for (int idx = 0; idx < xd->n8_w; ++idx)
5253 tx_rd_info->inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx];
5254#endif // CONFIG_VAR_TX
5255#if CONFIG_TXK_SEL
5256 av1_copy(tx_rd_info->txk_type, mbmi->txk_type);
5257#endif // CONFIG_TXK_SEL
5258 tx_rd_info->rd_stats = *rd_stats;
5259}
5260
5261static void fetch_tx_rd_info(int n4, const TX_RD_INFO *const tx_rd_info,
5262 RD_STATS *const rd_stats, MACROBLOCK *const x) {
5263 MACROBLOCKD *const xd = &x->e_mbd;
5264 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
5265 mbmi->tx_type = tx_rd_info->tx_type;
5266 mbmi->tx_size = tx_rd_info->tx_size;
5267#if CONFIG_VAR_TX
5268 mbmi->min_tx_size = tx_rd_info->min_tx_size;
5269 memcpy(x->blk_skip[0], tx_rd_info->blk_skip,
5270 sizeof(tx_rd_info->blk_skip[0]) * n4);
5271 for (int idy = 0; idy < xd->n8_h; ++idy)
5272 for (int idx = 0; idx < xd->n8_w; ++idx)
5273 mbmi->inter_tx_size[idy][idx] = tx_rd_info->inter_tx_size[idy][idx];
5274#endif // CONFIG_VAR_TX
5275#if CONFIG_TXK_SEL
5276 av1_copy(mbmi->txk_type, tx_rd_info->txk_type);
5277#endif // CONFIG_TXK_SEL
5278 *rd_stats = tx_rd_info->rd_stats;
5279}
5280
Angie Chiangb5dda482016-11-02 16:19:58 -07005281static void select_tx_type_yrd(const AV1_COMP *cpi, MACROBLOCK *x,
5282 RD_STATS *rd_stats, BLOCK_SIZE bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005283 int64_t ref_best_rd) {
Jingning Han2b0eeb12017-02-23 15:55:37 -08005284 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005285 const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
5286 MACROBLOCKD *const xd = &x->e_mbd;
5287 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
5288 int64_t rd = INT64_MAX;
5289 int64_t best_rd = INT64_MAX;
5290 TX_TYPE tx_type, best_tx_type = DCT_DCT;
5291 const int is_inter = is_inter_block(mbmi);
5292 TX_SIZE best_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE];
Debargha Mukherjee28d924b2016-10-05 00:48:28 -07005293 TX_SIZE best_tx = max_txsize_lookup[bsize];
Jingning Hane67b38a2016-11-04 10:30:00 -07005294 TX_SIZE best_min_tx_size = TX_SIZES_ALL;
Jingning Han9ca05b72017-01-03 14:41:36 -08005295 uint8_t best_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Jingning Hane3b81bc2017-06-23 11:43:52 -07005296 TX_TYPE txk_start = DCT_DCT;
5297#if CONFIG_TXK_SEL
5298 TX_TYPE txk_end = DCT_DCT + 1;
5299#else
5300 TX_TYPE txk_end = TX_TYPES;
5301#endif
Angie Chiangf1cb0752017-04-10 16:01:20 -07005302 const int n4 = bsize_to_num_blk(bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005303 int idx, idy;
5304 int prune = 0;
Jingning Han2b0eeb12017-02-23 15:55:37 -08005305 const int count32 =
5306 1 << (2 * (cm->mib_size_log2 - mi_width_log2_lookup[BLOCK_32X32]));
Jingning Han89d648b2016-11-22 11:22:08 -08005307#if CONFIG_EXT_PARTITION
5308 RD_STATS rd_stats_stack[16];
5309#else
Jingning Han63cbf342016-11-09 15:37:48 -08005310 RD_STATS rd_stats_stack[4];
Fergus Simpson4063a682017-02-28 16:52:22 -08005311#endif // CONFIG_EXT_PARTITION
Yaowu Xuc27fc142016-08-22 16:08:15 -07005312#if CONFIG_EXT_TX
Hui Suddbcde22017-09-18 17:22:02 -07005313 const TxSetType tx_set_type = get_ext_tx_set_type(
5314 max_tx_size, bsize, is_inter, cm->reduced_tx_set_used);
Sarah Parkere68a3e42017-02-16 14:03:24 -08005315 const int ext_tx_set =
5316 get_ext_tx_set(max_tx_size, bsize, is_inter, cm->reduced_tx_set_used);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005317#endif // CONFIG_EXT_TX
5318
5319 if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE)
5320#if CONFIG_EXT_TX
5321 prune = prune_tx_types(cpi, bsize, x, xd, ext_tx_set);
5322#else
5323 prune = prune_tx_types(cpi, bsize, x, xd, 0);
Fergus Simpson4063a682017-02-28 16:52:22 -08005324#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07005325
Angie Chiangc0feea82016-11-03 15:36:18 -07005326 av1_invalid_rd_stats(rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005327
Jingning Han89d648b2016-11-22 11:22:08 -08005328 for (idx = 0; idx < count32; ++idx)
5329 av1_invalid_rd_stats(&rd_stats_stack[idx]);
Jingning Han63cbf342016-11-09 15:37:48 -08005330
Hui Su1ddf2312017-08-19 15:21:34 -07005331 const uint32_t hash = get_block_residue_hash(x, bsize);
5332 TX_RD_RECORD *tx_rd_record = &x->tx_rd_record;
5333
5334 if (ref_best_rd != INT64_MAX) {
5335 for (int i = 0; i < tx_rd_record->num; ++i) {
5336 const int index = (tx_rd_record->index_start + i) % RD_RECORD_BUFFER_LEN;
5337 // If there is a match in the tx_rd_record, fetch the RD decision and
5338 // terminate early.
5339 if (tx_rd_record->tx_rd_info[index].hash_value == hash) {
5340 TX_RD_INFO *tx_rd_info = &tx_rd_record->tx_rd_info[index];
5341 fetch_tx_rd_info(n4, tx_rd_info, rd_stats, x);
5342 return;
5343 }
5344 }
5345 }
5346
Jingning Hane3b81bc2017-06-23 11:43:52 -07005347 for (tx_type = txk_start; tx_type < txk_end; ++tx_type) {
Angie Chiangb5dda482016-11-02 16:19:58 -07005348 RD_STATS this_rd_stats;
Angie Chiangc0feea82016-11-03 15:36:18 -07005349 av1_init_rd_stats(&this_rd_stats);
Sarah Parker53f93db2017-07-11 17:20:04 -07005350#if CONFIG_MRC_TX
5351 // MRC_DCT only implemented for TX_32X32 so only include this tx in
5352 // the search for TX_32X32
Sarah Parker2e08d962017-08-01 19:51:20 -07005353 if (tx_type == MRC_DCT &&
5354 (max_tx_size != TX_32X32 || (is_inter && !USE_MRC_INTER) ||
5355 (!is_inter && !USE_MRC_INTRA)))
5356 continue;
Sarah Parker53f93db2017-07-11 17:20:04 -07005357#endif // CONFIG_MRC_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07005358#if CONFIG_EXT_TX
Hui Suddbcde22017-09-18 17:22:02 -07005359 if (!av1_ext_tx_used[tx_set_type][tx_type]) continue;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005360 if (is_inter) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005361 if (cpi->sf.tx_type_search.prune_mode > NO_PRUNE) {
5362 if (!do_tx_type_search(tx_type, prune)) continue;
5363 }
5364 } else {
5365 if (!ALLOW_INTRA_EXT_TX && bsize >= BLOCK_8X8) {
5366 if (tx_type != intra_mode_to_tx_type_context[mbmi->mode]) continue;
5367 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005368 }
5369#else // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -07005370 if (is_inter && cpi->sf.tx_type_search.prune_mode > NO_PRUNE &&
5371 !do_tx_type_search(tx_type, prune))
5372 continue;
5373#endif // CONFIG_EXT_TX
5374 if (is_inter && x->use_default_inter_tx_type &&
5375 tx_type != get_default_tx_type(0, xd, 0, max_tx_size))
5376 continue;
5377
Jingning Hane67b38a2016-11-04 10:30:00 -07005378 if (xd->lossless[mbmi->segment_id])
5379 if (tx_type != DCT_DCT) continue;
5380
Angie Chiangb5dda482016-11-02 16:19:58 -07005381 rd = select_tx_size_fix_type(cpi, x, &this_rd_stats, bsize, ref_best_rd,
Jingning Han63cbf342016-11-09 15:37:48 -08005382 tx_type, rd_stats_stack);
Hui Suda816a12017-08-18 14:46:02 -07005383 ref_best_rd = AOMMIN(rd, ref_best_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005384 if (rd < best_rd) {
5385 best_rd = rd;
Angie Chiangb5dda482016-11-02 16:19:58 -07005386 *rd_stats = this_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005387 best_tx_type = mbmi->tx_type;
5388 best_tx = mbmi->tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -07005389 best_min_tx_size = mbmi->min_tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005390 memcpy(best_blk_skip, x->blk_skip[0], sizeof(best_blk_skip[0]) * n4);
5391 for (idy = 0; idy < xd->n8_h; ++idy)
5392 for (idx = 0; idx < xd->n8_w; ++idx)
5393 best_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx];
5394 }
5395 }
5396
5397 mbmi->tx_type = best_tx_type;
5398 for (idy = 0; idy < xd->n8_h; ++idy)
5399 for (idx = 0; idx < xd->n8_w; ++idx)
5400 mbmi->inter_tx_size[idy][idx] = best_tx_size[idy][idx];
5401 mbmi->tx_size = best_tx;
Jingning Hane67b38a2016-11-04 10:30:00 -07005402 mbmi->min_tx_size = best_min_tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005403 memcpy(x->blk_skip[0], best_blk_skip, sizeof(best_blk_skip[0]) * n4);
Hui Su1ddf2312017-08-19 15:21:34 -07005404
5405 // Save the RD search results into tx_rd_record.
5406 int index;
5407 if (tx_rd_record->num < RD_RECORD_BUFFER_LEN) {
5408 index =
5409 (tx_rd_record->index_start + tx_rd_record->num) % RD_RECORD_BUFFER_LEN;
5410 ++tx_rd_record->num;
5411 } else {
5412 index = tx_rd_record->index_start;
5413 tx_rd_record->index_start =
5414 (tx_rd_record->index_start + 1) % RD_RECORD_BUFFER_LEN;
5415 }
5416 save_tx_rd_info(n4, hash, x, rd_stats, &tx_rd_record->tx_rd_info[index]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005417}
5418
Yaowu Xuf883b422016-08-30 14:01:10 -07005419static void tx_block_rd(const AV1_COMP *cpi, MACROBLOCK *x, int blk_row,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005420 int blk_col, int plane, int block, TX_SIZE tx_size,
5421 BLOCK_SIZE plane_bsize, ENTROPY_CONTEXT *above_ctx,
Angie Chiangb5dda482016-11-02 16:19:58 -07005422 ENTROPY_CONTEXT *left_ctx, RD_STATS *rd_stats) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005423 MACROBLOCKD *const xd = &x->e_mbd;
5424 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005425 struct macroblockd_plane *const pd = &xd->plane[plane];
5426 BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
5427 const int tx_row = blk_row >> (1 - pd->subsampling_y);
5428 const int tx_col = blk_col >> (1 - pd->subsampling_x);
5429 TX_SIZE plane_tx_size;
Jingning Han18482fe2016-11-02 17:01:58 -07005430 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
5431 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005432
Jingning Hand3fada82016-11-22 10:46:55 -08005433 assert(tx_size < TX_SIZES_ALL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005434
Yaowu Xuc27fc142016-08-22 16:08:15 -07005435 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
5436
Debargha Mukherjee2f123402016-08-30 17:43:38 -07005437 plane_tx_size =
5438 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
5439 : mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005440
5441 if (tx_size == plane_tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005442 ENTROPY_CONTEXT *ta = above_ctx + blk_col;
5443 ENTROPY_CONTEXT *tl = left_ctx + blk_row;
Yaowu Xuf883b422016-08-30 14:01:10 -07005444 av1_tx_block_rd_b(cpi, x, tx_size, blk_row, blk_col, plane, block,
Angie Chiang77368af2017-03-23 16:22:07 -07005445 plane_bsize, ta, tl, rd_stats);
Tom Fineganac870492017-08-15 16:19:13 -07005446#if !CONFIG_PVQ
Jingning Han328d57b2017-07-07 14:40:17 -07005447 av1_set_txb_context(x, plane, block, tx_size, ta, tl);
Tom Fineganac870492017-08-15 16:19:13 -07005448#endif // !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07005449 } else {
Jingning Han18482fe2016-11-02 17:01:58 -07005450 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
5451 const int bsl = tx_size_wide_unit[sub_txs];
Jingning Han58224042016-10-27 16:35:32 -07005452 int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005453 int i;
5454
5455 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005456
5457 for (i = 0; i < 4; ++i) {
Jingning Han98d6a1f2016-11-03 12:47:47 -07005458 int offsetr = blk_row + (i >> 1) * bsl;
5459 int offsetc = blk_col + (i & 0x01) * bsl;
5460
5461 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
5462
5463 tx_block_rd(cpi, x, offsetr, offsetc, plane, block, sub_txs, plane_bsize,
5464 above_ctx, left_ctx, rd_stats);
5465 block += step;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005466 }
5467 }
5468}
5469
5470// Return value 0: early termination triggered, no valid rd cost available;
5471// 1: rd cost values are valid.
Angie Chiangb5dda482016-11-02 16:19:58 -07005472static int inter_block_uvrd(const AV1_COMP *cpi, MACROBLOCK *x,
5473 RD_STATS *rd_stats, BLOCK_SIZE bsize,
5474 int64_t ref_best_rd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005475 MACROBLOCKD *const xd = &x->e_mbd;
5476 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
5477 int plane;
5478 int is_cost_valid = 1;
5479 int64_t this_rd;
5480
5481 if (ref_best_rd < 0) is_cost_valid = 0;
5482
Angie Chiangc0feea82016-11-03 15:36:18 -07005483 av1_init_rd_stats(rd_stats);
Yue Chena1e48dc2016-08-29 17:29:33 -07005484
Jingning Han31b6a4f2017-02-23 11:05:53 -08005485#if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
Jingning Han9ce464c2017-02-20 15:36:30 -08005486 if (x->skip_chroma_rd) return is_cost_valid;
Jingning Han52d2b402017-05-19 09:31:17 -07005487 bsize = scale_chroma_bsize(mbmi->sb_type, xd->plane[1].subsampling_x,
5488 xd->plane[1].subsampling_y);
Fergus Simpson4063a682017-02-28 16:52:22 -08005489#endif // CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
Jingning Han9ce464c2017-02-20 15:36:30 -08005490
Yue Chena1e48dc2016-08-29 17:29:33 -07005491#if CONFIG_EXT_TX && CONFIG_RECT_TX
5492 if (is_rect_tx(mbmi->tx_size)) {
Angie Chiang284d7772016-11-08 11:06:45 -08005493 return super_block_uvrd(cpi, x, rd_stats, bsize, ref_best_rd);
Yue Chena1e48dc2016-08-29 17:29:33 -07005494 }
5495#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
5496
Yaowu Xuc27fc142016-08-22 16:08:15 -07005497 if (is_inter_block(mbmi) && is_cost_valid) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005498 for (plane = 1; plane < MAX_MB_PLANE; ++plane)
Yaowu Xuf883b422016-08-30 14:01:10 -07005499 av1_subtract_plane(x, bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005500 }
5501
Yaowu Xuc27fc142016-08-22 16:08:15 -07005502 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
5503 const struct macroblockd_plane *const pd = &xd->plane[plane];
5504 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Han9ca05b72017-01-03 14:41:36 -08005505 const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
5506 const int mi_height = block_size_high[plane_bsize] >> tx_size_high_log2[0];
Jingning Han70e5f3f2016-11-09 17:03:07 -08005507 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Jingning Han18482fe2016-11-02 17:01:58 -07005508 const int bh = tx_size_high_unit[max_tx_size];
5509 const int bw = tx_size_wide_unit[max_tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005510 int idx, idy;
5511 int block = 0;
Jingning Han18482fe2016-11-02 17:01:58 -07005512 const int step = bh * bw;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005513 ENTROPY_CONTEXT ta[2 * MAX_MIB_SIZE];
5514 ENTROPY_CONTEXT tl[2 * MAX_MIB_SIZE];
Angie Chiangb5dda482016-11-02 16:19:58 -07005515 RD_STATS pn_rd_stats;
Angie Chiangc0feea82016-11-03 15:36:18 -07005516 av1_init_rd_stats(&pn_rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005517
Jingning Han9ca05b72017-01-03 14:41:36 -08005518 av1_get_entropy_contexts(bsize, 0, pd, ta, tl);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005519
5520 for (idy = 0; idy < mi_height; idy += bh) {
Jingning Han18482fe2016-11-02 17:01:58 -07005521 for (idx = 0; idx < mi_width; idx += bw) {
5522 tx_block_rd(cpi, x, idy, idx, plane, block, max_tx_size, plane_bsize,
Angie Chiangb5dda482016-11-02 16:19:58 -07005523 ta, tl, &pn_rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005524 block += step;
5525 }
5526 }
5527
Angie Chiangb5dda482016-11-02 16:19:58 -07005528 if (pn_rd_stats.rate == INT_MAX) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005529 is_cost_valid = 0;
5530 break;
5531 }
5532
Angie Chiang628d7c92016-11-03 16:24:56 -07005533 av1_merge_rd_stats(rd_stats, &pn_rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005534
Urvang Joshi70006e42017-06-14 16:08:55 -07005535 this_rd = AOMMIN(RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist),
5536 RDCOST(x->rdmult, 0, rd_stats->sse));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005537
5538 if (this_rd > ref_best_rd) {
5539 is_cost_valid = 0;
5540 break;
5541 }
5542 }
5543
5544 if (!is_cost_valid) {
5545 // reset cost value
Angie Chiangc0feea82016-11-03 15:36:18 -07005546 av1_invalid_rd_stats(rd_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005547 }
5548
5549 return is_cost_valid;
5550}
5551#endif // CONFIG_VAR_TX
5552
hui su83c26632017-01-24 17:19:06 -08005553static void rd_pick_palette_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x,
5554 int dc_mode_cost,
5555 uint8_t *best_palette_color_map,
5556 MB_MODE_INFO *const best_mbmi,
5557 int64_t *best_rd, int *rate,
5558 int *rate_tokenonly, int64_t *distortion,
5559 int *skippable) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005560 MACROBLOCKD *const xd = &x->e_mbd;
5561 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07005562 assert(!is_inter_block(mbmi));
hui sude0c70a2017-01-09 17:12:17 -08005563 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005564 const BLOCK_SIZE bsize = mbmi->sb_type;
Urvang Joshic9e71d42017-08-09 18:58:33 -07005565 assert(bsize >= BLOCK_8X8);
Angie Chiang284d7772016-11-08 11:06:45 -08005566 int this_rate;
5567 int64_t this_rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005568 int colors_u, colors_v, colors;
5569 const int src_stride = x->plane[1].src.stride;
5570 const uint8_t *const src_u = x->plane[1].src.buf;
5571 const uint8_t *const src_v = x->plane[2].src.buf;
hui sude0c70a2017-01-09 17:12:17 -08005572 uint8_t *const color_map = xd->plane[1].color_index_map;
Angie Chiang284d7772016-11-08 11:06:45 -08005573 RD_STATS tokenonly_rd_stats;
Urvang Joshi56ba91b2017-01-10 13:22:09 -08005574 int plane_block_width, plane_block_height, rows, cols;
5575 av1_get_block_dimensions(bsize, 1, xd, &plane_block_width,
5576 &plane_block_height, &rows, &cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005577 if (rows * cols > PALETTE_MAX_BLOCK_SIZE) return;
5578
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04005579 mbmi->uv_mode = UV_DC_PRED;
hui su5db97432016-10-14 16:10:14 -07005580#if CONFIG_FILTER_INTRA
5581 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
5582#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07005583
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005584#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005585 if (cpi->common.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005586 colors_u = av1_count_colors_highbd(src_u, src_stride, rows, cols,
5587 cpi->common.bit_depth);
5588 colors_v = av1_count_colors_highbd(src_v, src_stride, rows, cols,
5589 cpi->common.bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005590 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005591#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005592 colors_u = av1_count_colors(src_u, src_stride, rows, cols);
5593 colors_v = av1_count_colors(src_v, src_stride, rows, cols);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005594#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005595 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005596#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005597
hui su33567b22017-04-30 16:40:19 -07005598#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -07005599 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -07005600 const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
hui su33567b22017-04-30 16:40:19 -07005601#endif // CONFIG_PALETTE_DELTA_ENCODING
5602
Yaowu Xuc27fc142016-08-22 16:08:15 -07005603 colors = colors_u > colors_v ? colors_u : colors_v;
5604 if (colors > 1 && colors <= 64) {
5605 int r, c, n, i, j;
5606 const int max_itr = 50;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005607 float lb_u, ub_u, val_u;
5608 float lb_v, ub_v, val_v;
5609 float *const data = x->palette_buffer->kmeans_data_buf;
5610 float centroids[2 * PALETTE_MAX_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005611
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005612#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005613 uint16_t *src_u16 = CONVERT_TO_SHORTPTR(src_u);
5614 uint16_t *src_v16 = CONVERT_TO_SHORTPTR(src_v);
5615 if (cpi->common.use_highbitdepth) {
5616 lb_u = src_u16[0];
5617 ub_u = src_u16[0];
5618 lb_v = src_v16[0];
5619 ub_v = src_v16[0];
5620 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005621#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005622 lb_u = src_u[0];
5623 ub_u = src_u[0];
5624 lb_v = src_v[0];
5625 ub_v = src_v[0];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005626#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005627 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005628#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005629
Yaowu Xuc27fc142016-08-22 16:08:15 -07005630 for (r = 0; r < rows; ++r) {
5631 for (c = 0; c < cols; ++c) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005632#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005633 if (cpi->common.use_highbitdepth) {
5634 val_u = src_u16[r * src_stride + c];
5635 val_v = src_v16[r * src_stride + c];
5636 data[(r * cols + c) * 2] = val_u;
5637 data[(r * cols + c) * 2 + 1] = val_v;
5638 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005639#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005640 val_u = src_u[r * src_stride + c];
5641 val_v = src_v[r * src_stride + c];
5642 data[(r * cols + c) * 2] = val_u;
5643 data[(r * cols + c) * 2 + 1] = val_v;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005644#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005645 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005646#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005647 if (val_u < lb_u)
5648 lb_u = val_u;
5649 else if (val_u > ub_u)
5650 ub_u = val_u;
5651 if (val_v < lb_v)
5652 lb_v = val_v;
5653 else if (val_v > ub_v)
5654 ub_v = val_v;
5655 }
5656 }
5657
5658 for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2;
5659 --n) {
5660 for (i = 0; i < n; ++i) {
5661 centroids[i * 2] = lb_u + (2 * i + 1) * (ub_u - lb_u) / n / 2;
5662 centroids[i * 2 + 1] = lb_v + (2 * i + 1) * (ub_v - lb_v) / n / 2;
5663 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005664 av1_k_means(data, centroids, color_map, rows * cols, n, 2, max_itr);
hui sud13c24a2017-04-07 16:13:07 -07005665#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -07005666 optimize_palette_colors(color_cache, n_cache, n, 2, centroids);
hui sud13c24a2017-04-07 16:13:07 -07005667 // Sort the U channel colors in ascending order.
5668 for (i = 0; i < 2 * (n - 1); i += 2) {
5669 int min_idx = i;
5670 float min_val = centroids[i];
5671 for (j = i + 2; j < 2 * n; j += 2)
5672 if (centroids[j] < min_val) min_val = centroids[j], min_idx = j;
5673 if (min_idx != i) {
5674 float temp_u = centroids[i], temp_v = centroids[i + 1];
5675 centroids[i] = centroids[min_idx];
5676 centroids[i + 1] = centroids[min_idx + 1];
5677 centroids[min_idx] = temp_u, centroids[min_idx + 1] = temp_v;
5678 }
5679 }
5680 av1_calc_indices(data, centroids, color_map, rows * cols, n, 2);
5681#endif // CONFIG_PALETTE_DELTA_ENCODING
Urvang Joshi56ba91b2017-01-10 13:22:09 -08005682 extend_palette_color_map(color_map, cols, rows, plane_block_width,
5683 plane_block_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005684 pmi->palette_size[1] = n;
5685 for (i = 1; i < 3; ++i) {
5686 for (j = 0; j < n; ++j) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005687#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005688 if (cpi->common.use_highbitdepth)
5689 pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = clip_pixel_highbd(
5690 (int)centroids[j * 2 + i - 1], cpi->common.bit_depth);
5691 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005692#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005693 pmi->palette_colors[i * PALETTE_MAX_SIZE + j] =
5694 clip_pixel((int)centroids[j * 2 + i - 1]);
5695 }
5696 }
5697
Angie Chiang284d7772016-11-08 11:06:45 -08005698 super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd);
5699 if (tokenonly_rd_stats.rate == INT_MAX) continue;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005700 this_rate =
Angie Chiang284d7772016-11-08 11:06:45 -08005701 tokenonly_rd_stats.rate + dc_mode_cost +
Yue Chenb23d00a2017-07-28 17:01:21 -07005702 x->palette_uv_size_cost[bsize - BLOCK_8X8][n - PALETTE_MIN_SIZE] +
Yaowu Xuc27fc142016-08-22 16:08:15 -07005703 write_uniform_cost(n, color_map[0]) +
Yaowu Xuf883b422016-08-30 14:01:10 -07005704 av1_cost_bit(
5705 av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 1);
hui su33567b22017-04-30 16:40:19 -07005706 this_rate += av1_palette_color_cost_uv(pmi,
5707#if CONFIG_PALETTE_DELTA_ENCODING
5708 color_cache, n_cache,
5709#endif // CONFIG_PALETTE_DELTA_ENCODING
5710 cpi->common.bit_depth);
Sarah Parker99e7daa2017-08-29 10:30:13 -07005711 this_rate +=
5712 av1_cost_color_map(x, 1, 0, bsize, mbmi->tx_size, PALETTE_MAP);
Urvang Joshi70006e42017-06-14 16:08:55 -07005713 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005714 if (this_rd < *best_rd) {
5715 *best_rd = this_rd;
hui su83c26632017-01-24 17:19:06 -08005716 *best_mbmi = *mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005717 memcpy(best_palette_color_map, color_map,
Urvang Joshi56ba91b2017-01-10 13:22:09 -08005718 plane_block_width * plane_block_height *
5719 sizeof(best_palette_color_map[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005720 *rate = this_rate;
Angie Chiang284d7772016-11-08 11:06:45 -08005721 *distortion = tokenonly_rd_stats.dist;
5722 *rate_tokenonly = tokenonly_rd_stats.rate;
5723 *skippable = tokenonly_rd_stats.skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005724 }
5725 }
5726 }
hui su83c26632017-01-24 17:19:06 -08005727 if (best_mbmi->palette_mode_info.palette_size[1] > 0) {
hui sude0c70a2017-01-09 17:12:17 -08005728 memcpy(color_map, best_palette_color_map,
Luc Trudeau0401e892017-08-31 00:37:11 -04005729 plane_block_width * plane_block_height *
5730 sizeof(best_palette_color_map[0]));
hui sude0c70a2017-01-09 17:12:17 -08005731 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005732}
5733
hui su5db97432016-10-14 16:10:14 -07005734#if CONFIG_FILTER_INTRA
5735// Return 1 if an filter intra mode is selected; return 0 otherwise.
5736static int rd_pick_filter_intra_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x,
5737 int *rate, int *rate_tokenonly,
5738 int64_t *distortion, int *skippable,
5739 BLOCK_SIZE bsize, int64_t *best_rd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005740 MACROBLOCKD *const xd = &x->e_mbd;
5741 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
hui su5db97432016-10-14 16:10:14 -07005742 int filter_intra_selected_flag = 0;
Angie Chiang284d7772016-11-08 11:06:45 -08005743 int this_rate;
5744 int64_t this_rd;
hui su5db97432016-10-14 16:10:14 -07005745 FILTER_INTRA_MODE mode;
5746 FILTER_INTRA_MODE_INFO filter_intra_mode_info;
Angie Chiang284d7772016-11-08 11:06:45 -08005747 RD_STATS tokenonly_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005748
hui su5db97432016-10-14 16:10:14 -07005749 av1_zero(filter_intra_mode_info);
5750 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 1;
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04005751 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005752 mbmi->palette_mode_info.palette_size[1] = 0;
5753
5754 for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) {
hui su5db97432016-10-14 16:10:14 -07005755 mbmi->filter_intra_mode_info.filter_intra_mode[1] = mode;
Angie Chiang284d7772016-11-08 11:06:45 -08005756 if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, *best_rd))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005757 continue;
5758
Angie Chiang284d7772016-11-08 11:06:45 -08005759 this_rate = tokenonly_rd_stats.rate +
hui su5db97432016-10-14 16:10:14 -07005760 av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 1) +
Yue Chenb23d00a2017-07-28 17:01:21 -07005761 x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode] +
Yaowu Xuc27fc142016-08-22 16:08:15 -07005762 write_uniform_cost(FILTER_INTRA_MODES, mode);
Urvang Joshi70006e42017-06-14 16:08:55 -07005763 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005764 if (this_rd < *best_rd) {
5765 *best_rd = this_rd;
5766 *rate = this_rate;
Angie Chiang284d7772016-11-08 11:06:45 -08005767 *rate_tokenonly = tokenonly_rd_stats.rate;
5768 *distortion = tokenonly_rd_stats.dist;
5769 *skippable = tokenonly_rd_stats.skip;
hui su5db97432016-10-14 16:10:14 -07005770 filter_intra_mode_info = mbmi->filter_intra_mode_info;
5771 filter_intra_selected_flag = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005772 }
5773 }
5774
hui su5db97432016-10-14 16:10:14 -07005775 if (filter_intra_selected_flag) {
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04005776 mbmi->uv_mode = UV_DC_PRED;
hui su5db97432016-10-14 16:10:14 -07005777 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
5778 filter_intra_mode_info.use_filter_intra_mode[1];
5779 mbmi->filter_intra_mode_info.filter_intra_mode[1] =
5780 filter_intra_mode_info.filter_intra_mode[1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005781 return 1;
5782 } else {
5783 return 0;
5784 }
5785}
hui su5db97432016-10-14 16:10:14 -07005786#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07005787
hui su5db97432016-10-14 16:10:14 -07005788#if CONFIG_EXT_INTRA
hui su45dc5972016-12-08 17:42:50 -08005789// Run RD calculation with given chroma intra prediction angle., and return
5790// the RD cost. Update the best mode info. if the RD cost is the best so far.
5791static int64_t pick_intra_angle_routine_sbuv(
5792 const AV1_COMP *const cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
5793 int rate_overhead, int64_t best_rd_in, int *rate, RD_STATS *rd_stats,
5794 int *best_angle_delta, int64_t *best_rd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005795 MB_MODE_INFO *mbmi = &x->e_mbd.mi[0]->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07005796 assert(!is_inter_block(mbmi));
Angie Chiang284d7772016-11-08 11:06:45 -08005797 int this_rate;
5798 int64_t this_rd;
5799 RD_STATS tokenonly_rd_stats;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005800
hui su45dc5972016-12-08 17:42:50 -08005801 if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd_in))
5802 return INT64_MAX;
Angie Chiang284d7772016-11-08 11:06:45 -08005803 this_rate = tokenonly_rd_stats.rate + rate_overhead;
Urvang Joshi70006e42017-06-14 16:08:55 -07005804 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005805 if (this_rd < *best_rd) {
5806 *best_rd = this_rd;
5807 *best_angle_delta = mbmi->angle_delta[1];
5808 *rate = this_rate;
hui su45dc5972016-12-08 17:42:50 -08005809 rd_stats->rate = tokenonly_rd_stats.rate;
5810 rd_stats->dist = tokenonly_rd_stats.dist;
5811 rd_stats->skip = tokenonly_rd_stats.skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005812 }
hui su45dc5972016-12-08 17:42:50 -08005813 return this_rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005814}
5815
hui su45dc5972016-12-08 17:42:50 -08005816// With given chroma directional intra prediction mode, pick the best angle
5817// delta. Return true if a RD cost that is smaller than the input one is found.
Urvang Joshi52648442016-10-13 17:27:51 -07005818static int rd_pick_intra_angle_sbuv(const AV1_COMP *const cpi, MACROBLOCK *x,
Urvang Joshi52648442016-10-13 17:27:51 -07005819 BLOCK_SIZE bsize, int rate_overhead,
hui su45dc5972016-12-08 17:42:50 -08005820 int64_t best_rd, int *rate,
5821 RD_STATS *rd_stats) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005822 MACROBLOCKD *const xd = &x->e_mbd;
5823 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07005824 assert(!is_inter_block(mbmi));
hui su45dc5972016-12-08 17:42:50 -08005825 int i, angle_delta, best_angle_delta = 0;
hui su0a6731f2017-04-26 15:23:47 -07005826 int64_t this_rd, best_rd_in, rd_cost[2 * (MAX_ANGLE_DELTA + 2)];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005827
hui su45dc5972016-12-08 17:42:50 -08005828 rd_stats->rate = INT_MAX;
5829 rd_stats->skip = 0;
5830 rd_stats->dist = INT64_MAX;
hui su0a6731f2017-04-26 15:23:47 -07005831 for (i = 0; i < 2 * (MAX_ANGLE_DELTA + 2); ++i) rd_cost[i] = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005832
hui su0a6731f2017-04-26 15:23:47 -07005833 for (angle_delta = 0; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) {
hui su45dc5972016-12-08 17:42:50 -08005834 for (i = 0; i < 2; ++i) {
5835 best_rd_in = (best_rd == INT64_MAX)
5836 ? INT64_MAX
5837 : (best_rd + (best_rd >> ((angle_delta == 0) ? 3 : 5)));
5838 mbmi->angle_delta[1] = (1 - 2 * i) * angle_delta;
5839 this_rd = pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead,
5840 best_rd_in, rate, rd_stats,
5841 &best_angle_delta, &best_rd);
5842 rd_cost[2 * angle_delta + i] = this_rd;
5843 if (angle_delta == 0) {
5844 if (this_rd == INT64_MAX) return 0;
5845 rd_cost[1] = this_rd;
5846 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005847 }
5848 }
hui su45dc5972016-12-08 17:42:50 -08005849 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005850
hui su45dc5972016-12-08 17:42:50 -08005851 assert(best_rd != INT64_MAX);
hui su0a6731f2017-04-26 15:23:47 -07005852 for (angle_delta = 1; angle_delta <= MAX_ANGLE_DELTA; angle_delta += 2) {
hui su45dc5972016-12-08 17:42:50 -08005853 int64_t rd_thresh;
5854 for (i = 0; i < 2; ++i) {
5855 int skip_search = 0;
5856 rd_thresh = best_rd + (best_rd >> 5);
5857 if (rd_cost[2 * (angle_delta + 1) + i] > rd_thresh &&
5858 rd_cost[2 * (angle_delta - 1) + i] > rd_thresh)
5859 skip_search = 1;
5860 if (!skip_search) {
5861 mbmi->angle_delta[1] = (1 - 2 * i) * angle_delta;
Yue Chenb0f808b2017-04-26 11:55:14 -07005862 pick_intra_angle_routine_sbuv(cpi, x, bsize, rate_overhead, best_rd,
5863 rate, rd_stats, &best_angle_delta,
5864 &best_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005865 }
5866 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005867 }
5868
5869 mbmi->angle_delta[1] = best_angle_delta;
hui su45dc5972016-12-08 17:42:50 -08005870 return rd_stats->rate != INT_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005871}
5872#endif // CONFIG_EXT_INTRA
5873
David Michael Barr2510f642017-07-11 23:39:20 +09005874#if CONFIG_CFL
Luc Trudeau60669132017-09-28 16:49:49 -04005875// TODO(ltrudeau) add support for HBD.
Luc Trudeau4e26d662017-09-11 13:08:40 -04005876static int64_t cfl_alpha_dist(const int16_t *pred_buf_q3, const uint8_t *src,
Luc Trudeau593d02c2017-09-08 11:29:37 -04005877 int src_stride, int width, int height,
5878 int dc_pred, int alpha_q3,
5879 int64_t *dist_neg_out) {
Luc Trudeau4c5df102017-07-08 14:43:27 -04005880 int64_t dist = 0;
David Michael Barr2510f642017-07-11 23:39:20 +09005881 int diff;
5882
5883 if (alpha_q3 == 0) {
5884 for (int j = 0; j < height; j++) {
5885 for (int i = 0; i < width; i++) {
5886 diff = src[i] - dc_pred;
5887 dist += diff * diff;
5888 }
5889 src += src_stride;
5890 }
5891
5892 if (dist_neg_out) *dist_neg_out = dist;
5893
5894 return dist;
5895 }
5896
Luc Trudeau4c5df102017-07-08 14:43:27 -04005897 int64_t dist_neg = 0;
Luc Trudeau593d02c2017-09-08 11:29:37 -04005898 for (int j = 0; j < height; j++) {
5899 for (int i = 0; i < width; i++) {
5900 const int uv = src[i];
Luc Trudeau4e26d662017-09-11 13:08:40 -04005901 const int scaled_luma = get_scaled_luma_q0(alpha_q3, pred_buf_q3[i]);
David Michael Barr2510f642017-07-11 23:39:20 +09005902
Luc Trudeau60669132017-09-28 16:49:49 -04005903 diff = uv - clip_pixel(scaled_luma + dc_pred);
Luc Trudeau593d02c2017-09-08 11:29:37 -04005904 dist += diff * diff;
David Michael Barr2510f642017-07-11 23:39:20 +09005905
Luc Trudeau60669132017-09-28 16:49:49 -04005906 diff = uv - clip_pixel(-scaled_luma + dc_pred);
Luc Trudeau593d02c2017-09-08 11:29:37 -04005907 dist_neg += diff * diff;
David Michael Barr2510f642017-07-11 23:39:20 +09005908 }
Luc Trudeau4e26d662017-09-11 13:08:40 -04005909 pred_buf_q3 += MAX_SB_SIZE;
Luc Trudeau593d02c2017-09-08 11:29:37 -04005910 src += src_stride;
David Michael Barr2510f642017-07-11 23:39:20 +09005911 }
5912
5913 if (dist_neg_out) *dist_neg_out = dist_neg;
5914
5915 return dist;
5916}
5917
David Michael Barr2510f642017-07-11 23:39:20 +09005918static int cfl_rd_pick_alpha(MACROBLOCK *const x, TX_SIZE tx_size) {
5919 const struct macroblock_plane *const p_u = &x->plane[AOM_PLANE_U];
5920 const struct macroblock_plane *const p_v = &x->plane[AOM_PLANE_V];
5921 const uint8_t *const src_u = p_u->src.buf;
5922 const uint8_t *const src_v = p_v->src.buf;
5923 const int src_stride_u = p_u->src.stride;
5924 const int src_stride_v = p_v->src.stride;
5925
5926 MACROBLOCKD *const xd = &x->e_mbd;
David Michael Barr2510f642017-07-11 23:39:20 +09005927 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
5928
5929 CFL_CTX *const cfl = xd->cfl;
5930 cfl_compute_parameters(xd, tx_size);
5931 const int width = cfl->uv_width;
5932 const int height = cfl->uv_height;
5933 const int dc_pred_u = cfl->dc_pred[CFL_PRED_U];
5934 const int dc_pred_v = cfl->dc_pred[CFL_PRED_V];
Luc Trudeau4e26d662017-09-11 13:08:40 -04005935 const int16_t *pred_buf_q3 = cfl->pred_buf_q3;
David Michael Barr2510f642017-07-11 23:39:20 +09005936
Luc Trudeau4c5df102017-07-08 14:43:27 -04005937 int64_t sse[CFL_PRED_PLANES][CFL_MAGS_SIZE];
Luc Trudeau4e26d662017-09-11 13:08:40 -04005938 sse[CFL_PRED_U][0] = cfl_alpha_dist(pred_buf_q3, src_u, src_stride_u, width,
Luc Trudeau593d02c2017-09-08 11:29:37 -04005939 height, dc_pred_u, 0, NULL);
Luc Trudeau4e26d662017-09-11 13:08:40 -04005940 sse[CFL_PRED_V][0] = cfl_alpha_dist(pred_buf_q3, src_v, src_stride_v, width,
Luc Trudeau593d02c2017-09-08 11:29:37 -04005941 height, dc_pred_v, 0, NULL);
David Michael Barr2510f642017-07-11 23:39:20 +09005942
David Michael Barrf6eaa152017-07-19 19:42:28 +09005943 for (int c = 0; c < CFL_ALPHABET_SIZE; c++) {
5944 const int m = c * 2 + 1;
5945 const int abs_alpha_q3 = c + 1;
Luc Trudeau593d02c2017-09-08 11:29:37 -04005946 sse[CFL_PRED_U][m] =
Luc Trudeau4e26d662017-09-11 13:08:40 -04005947 cfl_alpha_dist(pred_buf_q3, src_u, src_stride_u, width, height,
5948 dc_pred_u, abs_alpha_q3, &sse[CFL_PRED_U][m + 1]);
Luc Trudeau593d02c2017-09-08 11:29:37 -04005949 sse[CFL_PRED_V][m] =
Luc Trudeau4e26d662017-09-11 13:08:40 -04005950 cfl_alpha_dist(pred_buf_q3, src_v, src_stride_v, width, height,
5951 dc_pred_v, abs_alpha_q3, &sse[CFL_PRED_V][m + 1]);
David Michael Barr2510f642017-07-11 23:39:20 +09005952 }
5953
Luc Trudeau4c5df102017-07-08 14:43:27 -04005954 int64_t dist;
David Michael Barr2510f642017-07-11 23:39:20 +09005955 int64_t cost;
David Michael Barrf6eaa152017-07-19 19:42:28 +09005956 int64_t best_cost = INT64_MAX;
5957 int best_rate = 0;
David Michael Barr2510f642017-07-11 23:39:20 +09005958
5959 // Compute least squares parameter of the entire block
David Michael Barr2510f642017-07-11 23:39:20 +09005960 int ind = 0;
David Michael Barrf6eaa152017-07-19 19:42:28 +09005961 int signs = 0;
David Michael Barr2510f642017-07-11 23:39:20 +09005962
David Michael Barrf6eaa152017-07-19 19:42:28 +09005963 for (int joint_sign = 0; joint_sign < CFL_JOINT_SIGNS; joint_sign++) {
5964 const int sign_u = CFL_SIGN_U(joint_sign);
5965 const int sign_v = CFL_SIGN_V(joint_sign);
5966 const int size_u = (sign_u == CFL_SIGN_ZERO) ? 1 : CFL_ALPHABET_SIZE;
5967 const int size_v = (sign_v == CFL_SIGN_ZERO) ? 1 : CFL_ALPHABET_SIZE;
5968 for (int u = 0; u < size_u; u++) {
5969 const int idx_u = (sign_u == CFL_SIGN_ZERO) ? 0 : u * 2 + 1;
5970 for (int v = 0; v < size_v; v++) {
5971 const int idx_v = (sign_v == CFL_SIGN_ZERO) ? 0 : v * 2 + 1;
David Michael Barr2510f642017-07-11 23:39:20 +09005972 dist = sse[CFL_PRED_U][idx_u + (sign_u == CFL_SIGN_NEG)] +
5973 sse[CFL_PRED_V][idx_v + (sign_v == CFL_SIGN_NEG)];
5974 dist *= 16;
David Michael Barr38e560c2017-08-16 21:46:37 +09005975 const int rate = x->cfl_cost[joint_sign][CFL_PRED_U][u] +
5976 x->cfl_cost[joint_sign][CFL_PRED_V][v];
David Michael Barrf6eaa152017-07-19 19:42:28 +09005977 cost = RDCOST(x->rdmult, rate, dist);
David Michael Barr2510f642017-07-11 23:39:20 +09005978 if (cost < best_cost) {
5979 best_cost = cost;
David Michael Barrf6eaa152017-07-19 19:42:28 +09005980 best_rate = rate;
5981 ind = (u << CFL_ALPHABET_SIZE_LOG2) + v;
5982 signs = joint_sign;
David Michael Barr2510f642017-07-11 23:39:20 +09005983 }
5984 }
5985 }
5986 }
5987
5988 mbmi->cfl_alpha_idx = ind;
David Michael Barrf6eaa152017-07-19 19:42:28 +09005989 mbmi->cfl_alpha_signs = signs;
5990 return best_rate;
David Michael Barr2510f642017-07-11 23:39:20 +09005991}
5992#endif // CONFIG_CFL
5993
hui sueaddeee2017-05-30 12:19:38 -07005994static void init_sbuv_mode(MB_MODE_INFO *const mbmi) {
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04005995 mbmi->uv_mode = UV_DC_PRED;
hui sueaddeee2017-05-30 12:19:38 -07005996 mbmi->palette_mode_info.palette_size[1] = 0;
hui sueaddeee2017-05-30 12:19:38 -07005997#if CONFIG_FILTER_INTRA
5998 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
5999#endif // CONFIG_FILTER_INTRA
6000}
6001
Urvang Joshi52648442016-10-13 17:27:51 -07006002static int64_t rd_pick_intra_sbuv_mode(const AV1_COMP *const cpi, MACROBLOCK *x,
6003 int *rate, int *rate_tokenonly,
6004 int64_t *distortion, int *skippable,
6005 BLOCK_SIZE bsize, TX_SIZE max_tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006006 MACROBLOCKD *xd = &x->e_mbd;
6007 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Urvang Joshi330aec82017-05-08 15:37:42 -07006008 assert(!is_inter_block(mbmi));
hui su83c26632017-01-24 17:19:06 -08006009 MB_MODE_INFO best_mbmi = *mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006010 int64_t best_rd = INT64_MAX, this_rd;
Yushin Cho77bba8d2016-11-04 16:36:56 -07006011#if CONFIG_PVQ
6012 od_rollback_buffer buf;
Yushin Cho77bba8d2016-11-04 16:36:56 -07006013 od_encode_checkpoint(&x->daala_enc, &buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08006014#endif // CONFIG_PVQ
hui sude0c70a2017-01-09 17:12:17 -08006015 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
Urvang Joshic9e71d42017-08-09 18:58:33 -07006016 const int try_palette = can_use_palette(cpi, mbmi);
hui su5db97432016-10-14 16:10:14 -07006017
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04006018 for (int mode_idx = 0; mode_idx < UV_INTRA_MODES; ++mode_idx) {
hui su8a516a82017-07-06 10:00:36 -07006019 int this_rate;
6020 RD_STATS tokenonly_rd_stats;
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04006021 UV_PREDICTION_MODE mode = uv_rd_search_mode_order[mode_idx];
hui su83c26632017-01-24 17:19:06 -08006022#if CONFIG_EXT_INTRA
6023 const int is_directional_mode =
Luc Trudeau6e1cd782017-06-21 13:52:36 -04006024 av1_is_directional_mode(get_uv_mode(mode), mbmi->sb_type);
hui su83c26632017-01-24 17:19:06 -08006025#endif // CONFIG_EXT_INTRA
Urvang Joshifeb925f2016-12-05 10:37:29 -08006026 if (!(cpi->sf.intra_uv_mode_mask[txsize_sqr_up_map[max_tx_size]] &
6027 (1 << mode)))
6028 continue;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006029
6030 mbmi->uv_mode = mode;
David Michael Barr2510f642017-07-11 23:39:20 +09006031#if CONFIG_CFL
6032 int cfl_alpha_rate = 0;
Luc Trudeau6e1cd782017-06-21 13:52:36 -04006033 if (mode == UV_CFL_PRED) {
6034 assert(!is_directional_mode);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04006035 const TX_SIZE uv_tx_size = av1_get_uv_tx_size(mbmi, &xd->plane[1]);
6036 cfl_alpha_rate = cfl_rd_pick_alpha(x, uv_tx_size);
David Michael Barr2510f642017-07-11 23:39:20 +09006037 }
6038#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006039#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07006040 mbmi->angle_delta[1] = 0;
Joe Young830d4ce2017-05-30 17:48:13 -07006041 if (is_directional_mode && av1_use_angle_delta(mbmi->sb_type)) {
Yue Chenb23d00a2017-07-28 17:01:21 -07006042 const int rate_overhead = x->intra_uv_mode_cost[mbmi->mode][mode] +
hui su0a6731f2017-04-26 15:23:47 -07006043 write_uniform_cost(2 * MAX_ANGLE_DELTA + 1, 0);
hui su45dc5972016-12-08 17:42:50 -08006044 if (!rd_pick_intra_angle_sbuv(cpi, x, bsize, rate_overhead, best_rd,
6045 &this_rate, &tokenonly_rd_stats))
Yaowu Xuc27fc142016-08-22 16:08:15 -07006046 continue;
6047 } else {
hui su83c26632017-01-24 17:19:06 -08006048#endif // CONFIG_EXT_INTRA
Angie Chiang284d7772016-11-08 11:06:45 -08006049 if (!super_block_uvrd(cpi, x, &tokenonly_rd_stats, bsize, best_rd)) {
Yushin Cho77bba8d2016-11-04 16:36:56 -07006050#if CONFIG_PVQ
6051 od_encode_rollback(&x->daala_enc, &buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08006052#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -07006053 continue;
Yushin Cho77bba8d2016-11-04 16:36:56 -07006054 }
hui su83c26632017-01-24 17:19:06 -08006055#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07006056 }
hui su83c26632017-01-24 17:19:06 -08006057#endif // CONFIG_EXT_INTRA
Angie Chiang284d7772016-11-08 11:06:45 -08006058 this_rate =
Yue Chenb23d00a2017-07-28 17:01:21 -07006059 tokenonly_rd_stats.rate + x->intra_uv_mode_cost[mbmi->mode][mode];
hui su83c26632017-01-24 17:19:06 -08006060
Luc Trudeaudff41922017-07-07 09:47:58 -04006061#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04006062 if (mode == UV_CFL_PRED) {
David Michael Barr2510f642017-07-11 23:39:20 +09006063 this_rate += cfl_alpha_rate;
Luc Trudeaudff41922017-07-07 09:47:58 -04006064 }
6065#endif
hui su83c26632017-01-24 17:19:06 -08006066#if CONFIG_EXT_INTRA
Joe Young830d4ce2017-05-30 17:48:13 -07006067 if (is_directional_mode && av1_use_angle_delta(mbmi->sb_type)) {
hui su0a6731f2017-04-26 15:23:47 -07006068 this_rate += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1,
6069 MAX_ANGLE_DELTA + mbmi->angle_delta[1]);
hui su45dc5972016-12-08 17:42:50 -08006070 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006071#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07006072#if CONFIG_FILTER_INTRA
Luc Trudeau91357ee2017-09-27 13:40:37 -04006073 if (mbmi->sb_type >= BLOCK_8X8 && mode == UV_DC_PRED)
hui su5db97432016-10-14 16:10:14 -07006074 this_rate += av1_cost_bit(cpi->common.fc->filter_intra_probs[1], 0);
6075#endif // CONFIG_FILTER_INTRA
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +01006076 if (try_palette && mode == UV_DC_PRED)
Yaowu Xuf883b422016-08-30 14:01:10 -07006077 this_rate += av1_cost_bit(
6078 av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006079
Yushin Cho77bba8d2016-11-04 16:36:56 -07006080#if CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -07006081 od_encode_rollback(&x->daala_enc, &buf);
Fergus Simpson4063a682017-02-28 16:52:22 -08006082#endif // CONFIG_PVQ
Urvang Joshi70006e42017-06-14 16:08:55 -07006083 this_rd = RDCOST(x->rdmult, this_rate, tokenonly_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006084
6085 if (this_rd < best_rd) {
hui su83c26632017-01-24 17:19:06 -08006086 best_mbmi = *mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006087 best_rd = this_rd;
6088 *rate = this_rate;
Angie Chiang284d7772016-11-08 11:06:45 -08006089 *rate_tokenonly = tokenonly_rd_stats.rate;
6090 *distortion = tokenonly_rd_stats.dist;
6091 *skippable = tokenonly_rd_stats.skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006092 }
6093 }
6094
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +01006095 if (try_palette) {
hui su8a516a82017-07-06 10:00:36 -07006096 uint8_t *best_palette_color_map = x->palette_buffer->best_palette_color_map;
hui su83c26632017-01-24 17:19:06 -08006097 rd_pick_palette_intra_sbuv(cpi, x,
Yue Chenb23d00a2017-07-28 17:01:21 -07006098 x->intra_uv_mode_cost[mbmi->mode][UV_DC_PRED],
hui su83c26632017-01-24 17:19:06 -08006099 best_palette_color_map, &best_mbmi, &best_rd,
6100 rate, rate_tokenonly, distortion, skippable);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006101 }
6102
hui su5db97432016-10-14 16:10:14 -07006103#if CONFIG_FILTER_INTRA
6104 if (mbmi->sb_type >= BLOCK_8X8) {
6105 if (rd_pick_filter_intra_sbuv(cpi, x, rate, rate_tokenonly, distortion,
hui su83c26632017-01-24 17:19:06 -08006106 skippable, bsize, &best_rd))
6107 best_mbmi = *mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006108 }
hui su5db97432016-10-14 16:10:14 -07006109#endif // CONFIG_FILTER_INTRA
6110
hui su83c26632017-01-24 17:19:06 -08006111 *mbmi = best_mbmi;
Urvang Joshifeb925f2016-12-05 10:37:29 -08006112 // Make sure we actually chose a mode
6113 assert(best_rd < INT64_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006114 return best_rd;
6115}
6116
Urvang Joshi52648442016-10-13 17:27:51 -07006117static void choose_intra_uv_mode(const AV1_COMP *const cpi, MACROBLOCK *const x,
Luc Trudeau9d4cbb82017-07-27 17:01:32 -04006118 BLOCK_SIZE bsize, TX_SIZE max_tx_size,
6119 int *rate_uv, int *rate_uv_tokenonly,
6120 int64_t *dist_uv, int *skip_uv,
6121 UV_PREDICTION_MODE *mode_uv) {
Luc Trudeaub05eeae2017-08-18 15:14:30 -04006122 MACROBLOCKD *xd = &x->e_mbd;
6123 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006124 // Use an estimated rd for uv_intra based on DC_PRED if the
6125 // appropriate speed flag is set.
Luc Trudeaub05eeae2017-08-18 15:14:30 -04006126 init_sbuv_mode(mbmi);
Jingning Han271bb2c2016-12-14 12:34:46 -08006127#if CONFIG_CB4X4
Luc Trudeau9d4cbb82017-07-27 17:01:32 -04006128#if !CONFIG_CHROMA_2X2
Jingning Han9ce464c2017-02-20 15:36:30 -08006129 if (x->skip_chroma_rd) {
6130 *rate_uv = 0;
6131 *rate_uv_tokenonly = 0;
6132 *dist_uv = 0;
6133 *skip_uv = 1;
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04006134 *mode_uv = UV_DC_PRED;
Jingning Han9ce464c2017-02-20 15:36:30 -08006135 return;
6136 }
Luc Trudeaub05eeae2017-08-18 15:14:30 -04006137 bsize = scale_chroma_bsize(bsize, xd->plane[AOM_PLANE_U].subsampling_x,
6138 xd->plane[AOM_PLANE_U].subsampling_y);
Luc Trudeau9d4cbb82017-07-27 17:01:32 -04006139#endif // !CONFIG_CHROMA_2X2
Luc Trudeaub05eeae2017-08-18 15:14:30 -04006140#if CONFIG_CFL
6141 // Only store reconstructed luma when there's chroma RDO. When there's no
6142 // chroma RDO, the reconstructed luma will be stored in encode_superblock().
6143 xd->cfl->store_y = !x->skip_chroma_rd;
6144#endif // CONFIG_CFL
Jingning Han31b6a4f2017-02-23 11:05:53 -08006145#else
Luc Trudeau9d4cbb82017-07-27 17:01:32 -04006146 bsize = bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize;
Luc Trudeaub05eeae2017-08-18 15:14:30 -04006147#if CONFIG_CFL
6148 xd->cfl->store_y = 1;
6149#endif // CONFIG_CFL
Jingning Han31b6a4f2017-02-23 11:05:53 -08006150#endif // CONFIG_CB4X4
Luc Trudeaub05eeae2017-08-18 15:14:30 -04006151#if CONFIG_CFL
6152 if (xd->cfl->store_y) {
6153 // Perform one extra call to txfm_rd_in_plane(), with the values chosen
6154 // during luma RDO, so we can store reconstructed luma values
6155 RD_STATS this_rd_stats;
6156 txfm_rd_in_plane(x, cpi, &this_rd_stats, INT64_MAX, AOM_PLANE_Y,
6157 mbmi->sb_type, mbmi->tx_size,
6158 cpi->sf.use_fast_coef_costing);
6159 xd->cfl->store_y = 0;
6160 }
6161#endif // CONFIG_CFL
Luc Trudeau9d4cbb82017-07-27 17:01:32 -04006162 rd_pick_intra_sbuv_mode(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv, skip_uv,
6163 bsize, max_tx_size);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04006164 *mode_uv = mbmi->uv_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006165}
6166
Yue Chenb23d00a2017-07-28 17:01:21 -07006167static int cost_mv_ref(const MACROBLOCK *const x, PREDICTION_MODE mode,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006168 int16_t mode_context) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006169#if CONFIG_EXT_INTER
6170 if (is_inter_compound_mode(mode)) {
Yue Chenb23d00a2017-07-28 17:01:21 -07006171 return x
clang-format55ce9e02017-02-15 22:27:12 -08006172 ->inter_compound_mode_cost[mode_context][INTER_COMPOUND_OFFSET(mode)];
Zoe Liu85b66462017-04-20 14:28:19 -07006173#if CONFIG_COMPOUND_SINGLEREF
6174 } else if (is_inter_singleref_comp_mode(mode)) {
Yue Chenb23d00a2017-07-28 17:01:21 -07006175 return x->inter_singleref_comp_mode_cost[mode_context]
6176 [INTER_SINGLEREF_COMP_OFFSET(mode)];
Zoe Liu85b66462017-04-20 14:28:19 -07006177#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07006178 }
David Barkercb03dc32017-04-07 13:05:09 +01006179#endif
6180
David Barkercb03dc32017-04-07 13:05:09 +01006181 int mode_cost = 0;
6182 int16_t mode_ctx = mode_context & NEWMV_CTX_MASK;
6183 int16_t is_all_zero_mv = mode_context & (1 << ALL_ZERO_FLAG_OFFSET);
6184
6185 assert(is_inter_mode(mode));
6186
6187 if (mode == NEWMV) {
Yue Chenb23d00a2017-07-28 17:01:21 -07006188 mode_cost = x->newmv_mode_cost[mode_ctx][0];
David Barkercb03dc32017-04-07 13:05:09 +01006189 return mode_cost;
6190 } else {
Yue Chenb23d00a2017-07-28 17:01:21 -07006191 mode_cost = x->newmv_mode_cost[mode_ctx][1];
David Barkercb03dc32017-04-07 13:05:09 +01006192 mode_ctx = (mode_context >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
6193
6194 if (is_all_zero_mv) return mode_cost;
6195
6196 if (mode == ZEROMV) {
Yue Chenb23d00a2017-07-28 17:01:21 -07006197 mode_cost += x->zeromv_mode_cost[mode_ctx][0];
David Barkercb03dc32017-04-07 13:05:09 +01006198 return mode_cost;
6199 } else {
Yue Chenb23d00a2017-07-28 17:01:21 -07006200 mode_cost += x->zeromv_mode_cost[mode_ctx][1];
David Barkercb03dc32017-04-07 13:05:09 +01006201 mode_ctx = (mode_context >> REFMV_OFFSET) & REFMV_CTX_MASK;
6202
6203 if (mode_context & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
6204 if (mode_context & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
6205 if (mode_context & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
6206
Yue Chenb23d00a2017-07-28 17:01:21 -07006207 mode_cost += x->refmv_mode_cost[mode_ctx][mode != NEARESTMV];
David Barkercb03dc32017-04-07 13:05:09 +01006208 return mode_cost;
6209 }
6210 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006211}
6212
Yue Chen5e606542017-05-24 17:03:17 -07006213#if CONFIG_EXT_INTER && (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT)
Sarah Parker6fdc8532016-11-16 17:47:13 -08006214static int get_interinter_compound_type_bits(BLOCK_SIZE bsize,
6215 COMPOUND_TYPE comp_type) {
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00006216 (void)bsize;
Sarah Parker6fdc8532016-11-16 17:47:13 -08006217 switch (comp_type) {
6218 case COMPOUND_AVERAGE: return 0;
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00006219#if CONFIG_WEDGE
Sarah Parker6fdc8532016-11-16 17:47:13 -08006220 case COMPOUND_WEDGE: return get_interinter_wedge_bits(bsize);
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00006221#endif // CONFIG_WEDGE
Sarah Parker2f6ce752016-12-08 15:26:46 -08006222#if CONFIG_COMPOUND_SEGMENT
Sarah Parker569edda2016-12-14 14:57:38 -08006223 case COMPOUND_SEG: return 1;
Sarah Parker2f6ce752016-12-08 15:26:46 -08006224#endif // CONFIG_COMPOUND_SEGMENT
Sarah Parker6fdc8532016-11-16 17:47:13 -08006225 default: assert(0); return 0;
6226 }
6227}
Yue Chen5e606542017-05-24 17:03:17 -07006228#endif // CONFIG_EXT_INTER && (CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT)
Sarah Parker6fdc8532016-11-16 17:47:13 -08006229
Yaowu Xuc27fc142016-08-22 16:08:15 -07006230typedef struct {
6231 int eobs;
6232 int brate;
6233 int byrate;
6234 int64_t bdist;
6235 int64_t bsse;
6236 int64_t brdcost;
6237 int_mv mvs[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006238 int_mv pred_mv[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006239#if CONFIG_EXT_INTER
6240 int_mv ref_mv[2];
6241#endif // CONFIG_EXT_INTER
Jingning Han276c2942016-12-05 12:37:02 -08006242
Timothy B. Terriberryfe67ed62017-04-26 16:53:47 -07006243#if CONFIG_CHROMA_2X2
Jingning Han276c2942016-12-05 12:37:02 -08006244 ENTROPY_CONTEXT ta[4];
6245 ENTROPY_CONTEXT tl[4];
6246#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006247 ENTROPY_CONTEXT ta[2];
6248 ENTROPY_CONTEXT tl[2];
Timothy B. Terriberryfe67ed62017-04-26 16:53:47 -07006249#endif // CONFIG_CHROMA_2X2
Yaowu Xuc27fc142016-08-22 16:08:15 -07006250} SEG_RDSTAT;
6251
6252typedef struct {
6253 int_mv *ref_mv[2];
6254 int_mv mvp;
6255
6256 int64_t segment_rd;
6257 int r;
6258 int64_t d;
6259 int64_t sse;
6260 int segment_yrate;
6261 PREDICTION_MODE modes[4];
6262#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07006263#if CONFIG_COMPOUND_SINGLEREF
6264 SEG_RDSTAT rdstat[4][INTER_MODES + INTER_SINGLEREF_COMP_MODES +
6265 INTER_COMPOUND_MODES];
6266#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07006267 SEG_RDSTAT rdstat[4][INTER_MODES + INTER_COMPOUND_MODES];
Zoe Liu85b66462017-04-20 14:28:19 -07006268#endif // CONFIG_COMPOUND_SINGLEREF
6269#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07006270 SEG_RDSTAT rdstat[4][INTER_MODES];
6271#endif // CONFIG_EXT_INTER
6272 int mvthresh;
6273} BEST_SEG_INFO;
6274
Alex Converse0fa0f422017-04-24 12:51:14 -07006275static INLINE int mv_check_bounds(const MvLimits *mv_limits, const MV *mv) {
6276 return (mv->row >> 3) < mv_limits->row_min ||
6277 (mv->row >> 3) > mv_limits->row_max ||
6278 (mv->col >> 3) < mv_limits->col_min ||
6279 (mv->col >> 3) > mv_limits->col_max;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006280}
6281
Yaowu Xuc27fc142016-08-22 16:08:15 -07006282// Check if NEARESTMV/NEARMV/ZEROMV is the cheapest way encode zero motion.
6283// TODO(aconverse): Find out if this is still productive then clean up or remove
6284static int check_best_zero_mv(
Yue Chenb23d00a2017-07-28 17:01:21 -07006285 const AV1_COMP *const cpi, const MACROBLOCK *const x,
6286 const int16_t mode_context[TOTAL_REFS_PER_FRAME],
Sebastien Alaiwane140c502017-04-27 09:52:34 +02006287#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07006288 const int16_t compound_mode_context[TOTAL_REFS_PER_FRAME],
Sebastien Alaiwane140c502017-04-27 09:52:34 +02006289#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07006290 int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME], int this_mode,
David Barker45390c12017-02-20 14:44:40 +00006291 const MV_REFERENCE_FRAME ref_frames[2], const BLOCK_SIZE bsize, int block,
6292 int mi_row, int mi_col) {
Luc Trudeaud28e91d2017-06-05 14:42:26 -04006293 int_mv zeromv[2] = { {.as_int = 0 } };
Yushin Choc9751c52017-06-12 10:38:29 -07006294#if CONFIG_GLOBAL_MOTION
Sarah Parkerc2d38712017-01-24 15:15:41 -08006295 int comp_pred_mode = ref_frames[1] > INTRA_FRAME;
Yushin Choc9751c52017-06-12 10:38:29 -07006296#endif
David Barker45390c12017-02-20 14:44:40 +00006297 (void)mi_row;
6298 (void)mi_col;
Zoe Liubc030ee2017-07-31 15:20:46 -07006299 (void)cpi;
Sarah Parkerc2d38712017-01-24 15:15:41 -08006300#if CONFIG_GLOBAL_MOTION
Luc Trudeaud28e91d2017-06-05 14:42:26 -04006301 if (this_mode == ZEROMV
Sarah Parkerc2d38712017-01-24 15:15:41 -08006302#if CONFIG_EXT_INTER
Luc Trudeaud28e91d2017-06-05 14:42:26 -04006303 || this_mode == ZERO_ZEROMV
Sarah Parkerc2d38712017-01-24 15:15:41 -08006304#endif // CONFIG_EXT_INTER
Luc Trudeaud28e91d2017-06-05 14:42:26 -04006305 ) {
6306 for (int cur_frm = 0; cur_frm < 1 + comp_pred_mode; cur_frm++) {
Sarah Parkerc2d38712017-01-24 15:15:41 -08006307 zeromv[cur_frm].as_int =
6308 gm_get_motion_vector(&cpi->common.global_motion[ref_frames[cur_frm]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08006309 cpi->common.allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07006310 mi_col, mi_row, block
6311#if CONFIG_AMVR
6312 ,
6313 cpi->common.cur_frame_mv_precision_level
6314#endif
6315 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08006316 .as_int;
Luc Trudeaud28e91d2017-06-05 14:42:26 -04006317 }
Sarah Parkerc2d38712017-01-24 15:15:41 -08006318 }
Luc Trudeaud28e91d2017-06-05 14:42:26 -04006319#endif // CONFIG_GLOBAL_MOTION
6320
Yaowu Xuc27fc142016-08-22 16:08:15 -07006321#if !CONFIG_EXT_INTER
6322 assert(ref_frames[1] != INTRA_FRAME); // Just sanity check
Fergus Simpson4063a682017-02-28 16:52:22 -08006323#endif // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07006324 if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) &&
Sarah Parkerc2d38712017-01-24 15:15:41 -08006325 frame_mv[this_mode][ref_frames[0]].as_int == zeromv[0].as_int &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07006326 (ref_frames[1] <= INTRA_FRAME ||
Sarah Parkerc2d38712017-01-24 15:15:41 -08006327 frame_mv[this_mode][ref_frames[1]].as_int == zeromv[1].as_int)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006328 int16_t rfc =
Yaowu Xuf883b422016-08-30 14:01:10 -07006329 av1_mode_context_analyzer(mode_context, ref_frames, bsize, block);
Yue Chenb23d00a2017-07-28 17:01:21 -07006330 int c1 = cost_mv_ref(x, NEARMV, rfc);
6331 int c2 = cost_mv_ref(x, NEARESTMV, rfc);
6332 int c3 = cost_mv_ref(x, ZEROMV, rfc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006333
Yaowu Xuc27fc142016-08-22 16:08:15 -07006334 if (this_mode == NEARMV) {
6335 if (c1 > c3) return 0;
6336 } else if (this_mode == NEARESTMV) {
6337 if (c2 > c3) return 0;
6338 } else {
6339 assert(this_mode == ZEROMV);
6340 if (ref_frames[1] <= INTRA_FRAME) {
6341 if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0) ||
6342 (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0))
6343 return 0;
6344 } else {
6345 if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0 &&
6346 frame_mv[NEARESTMV][ref_frames[1]].as_int == 0) ||
6347 (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0 &&
6348 frame_mv[NEARMV][ref_frames[1]].as_int == 0))
6349 return 0;
6350 }
6351 }
6352 }
6353#if CONFIG_EXT_INTER
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07006354 else if ((this_mode == NEAREST_NEARESTMV || this_mode == NEAR_NEARMV ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07006355 this_mode == ZERO_ZEROMV) &&
Sarah Parkerc2d38712017-01-24 15:15:41 -08006356 frame_mv[this_mode][ref_frames[0]].as_int == zeromv[0].as_int &&
6357 frame_mv[this_mode][ref_frames[1]].as_int == zeromv[1].as_int) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006358 int16_t rfc = compound_mode_context[ref_frames[0]];
Yue Chenb23d00a2017-07-28 17:01:21 -07006359 int c2 = cost_mv_ref(x, NEAREST_NEARESTMV, rfc);
6360 int c3 = cost_mv_ref(x, ZERO_ZEROMV, rfc);
6361 int c5 = cost_mv_ref(x, NEAR_NEARMV, rfc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006362
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07006363 if (this_mode == NEAREST_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006364 if (c2 > c3) return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006365 } else if (this_mode == NEAR_NEARMV) {
6366 if (c5 > c3) return 0;
6367 } else {
6368 assert(this_mode == ZERO_ZEROMV);
6369 if ((c3 >= c2 && frame_mv[NEAREST_NEARESTMV][ref_frames[0]].as_int == 0 &&
6370 frame_mv[NEAREST_NEARESTMV][ref_frames[1]].as_int == 0) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07006371 (c3 >= c5 && frame_mv[NEAR_NEARMV][ref_frames[0]].as_int == 0 &&
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07006372 frame_mv[NEAR_NEARMV][ref_frames[1]].as_int == 0))
Yaowu Xuc27fc142016-08-22 16:08:15 -07006373 return 0;
6374 }
6375 }
6376#endif // CONFIG_EXT_INTER
6377 return 1;
6378}
6379
Urvang Joshi52648442016-10-13 17:27:51 -07006380static void joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
Zoe Liu85b66462017-04-20 14:28:19 -07006381 BLOCK_SIZE bsize, int_mv *frame_mv,
6382#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6383 int_mv *frame_comp_mv,
6384#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6385 int mi_row, int mi_col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006386#if CONFIG_EXT_INTER
David Barkerc155e012017-05-11 13:54:54 +01006387 int_mv *ref_mv_sub8x8[2], const uint8_t *mask,
6388 int mask_stride,
Fergus Simpson4063a682017-02-28 16:52:22 -08006389#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07006390 int *rate_mv, const int block) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006391 const AV1_COMMON *const cm = &cpi->common;
Jingning Hanae5cfde2016-11-30 12:01:44 -08006392 const int pw = block_size_wide[bsize];
6393 const int ph = block_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006394 MACROBLOCKD *xd = &x->e_mbd;
6395 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Zoe Liu85b66462017-04-20 14:28:19 -07006396// This function should only ever be called for compound modes
6397#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6398 if (!has_second_ref(mbmi)) {
6399 assert(is_inter_singleref_comp_mode(mbmi->mode));
6400 assert(frame_comp_mv);
6401 }
6402 assert(has_second_ref(mbmi) || is_inter_singleref_comp_mode(mbmi->mode));
clang-format4eafefe2017-09-04 12:51:20 -07006403 const int refs[2] = { mbmi->ref_frame[0],
6404 has_second_ref(mbmi) ? mbmi->ref_frame[1]
6405 : mbmi->ref_frame[0] };
Zoe Liu85b66462017-04-20 14:28:19 -07006406#else
Sarah Parkerb3ebed12017-03-09 10:52:03 -08006407 assert(has_second_ref(mbmi));
Zoe Liu122f3942017-04-25 11:18:38 -07006408 const int refs[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] };
Zoe Liu85b66462017-04-20 14:28:19 -07006409#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07006410 int_mv ref_mv[2];
6411 int ite, ref;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006412 struct scale_factors sf;
Sarah Parker4c10a3c2017-04-10 19:37:59 -07006413#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
James Zern89a015b2017-08-08 12:39:00 -04006414 // ic and ir are the 4x4 coordinates of the sub8x8 at index "block"
Sarah Parkerb3ebed12017-03-09 10:52:03 -08006415 const int ic = block & 1;
6416 const int ir = (block - ic) >> 1;
Jingning Hancb637672017-06-22 09:14:40 -07006417 struct macroblockd_plane *const pd = &xd->plane[0];
Sarah Parkerb3ebed12017-03-09 10:52:03 -08006418 const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x) + 4 * ic;
6419 const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y) + 4 * ir;
Sarah Parker4c10a3c2017-04-10 19:37:59 -07006420#if CONFIG_GLOBAL_MOTION
Sarah Parkerb3ebed12017-03-09 10:52:03 -08006421 int is_global[2];
Zoe Liu85b66462017-04-20 14:28:19 -07006422#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6423 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
6424#else
Sarah Parkerb3ebed12017-03-09 10:52:03 -08006425 for (ref = 0; ref < 2; ++ref) {
Zoe Liu85b66462017-04-20 14:28:19 -07006426#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Sarah Parkerb3ebed12017-03-09 10:52:03 -08006427 WarpedMotionParams *const wm =
6428 &xd->global_motion[xd->mi[0]->mbmi.ref_frame[ref]];
6429 is_global[ref] = is_global_mv_block(xd->mi[0], block, wm->wmtype);
6430 }
Zoe Liu85b66462017-04-20 14:28:19 -07006431#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6432 if (!has_second_ref(mbmi)) is_global[1] = is_global[0];
6433#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Sarah Parkerb3ebed12017-03-09 10:52:03 -08006434#endif // CONFIG_GLOBAL_MOTION
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07006435#else // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
6436 (void)block;
Sarah Parker4c10a3c2017-04-10 19:37:59 -07006437#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07006438
6439 // Do joint motion search in compound mode to get more accurate mv.
6440 struct buf_2d backup_yv12[2][MAX_MB_PLANE];
6441 int last_besterr[2] = { INT_MAX, INT_MAX };
6442 const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = {
Zoe Liu122f3942017-04-25 11:18:38 -07006443 av1_get_scaled_ref_frame(cpi, refs[0]),
6444 av1_get_scaled_ref_frame(cpi, refs[1])
Yaowu Xuc27fc142016-08-22 16:08:15 -07006445 };
6446
6447// Prediction buffer from second frame.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006448#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006449 DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]);
6450 uint8_t *second_pred;
6451#else
6452 DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006453#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006454
Jingning Han61418bb2017-01-23 17:12:48 -08006455#if CONFIG_EXT_INTER && CONFIG_CB4X4
6456 (void)ref_mv_sub8x8;
Fergus Simpson4063a682017-02-28 16:52:22 -08006457#endif // CONFIG_EXT_INTER && CONFIG_CB4X4
Jingning Han61418bb2017-01-23 17:12:48 -08006458
Zoe Liu85b66462017-04-20 14:28:19 -07006459#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6460 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
6461#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006462 for (ref = 0; ref < 2; ++ref) {
Zoe Liu85b66462017-04-20 14:28:19 -07006463#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Jingning Han61418bb2017-01-23 17:12:48 -08006464#if CONFIG_EXT_INTER && !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07006465 if (bsize < BLOCK_8X8 && ref_mv_sub8x8 != NULL)
6466 ref_mv[ref].as_int = ref_mv_sub8x8[ref]->as_int;
6467 else
Fergus Simpson4063a682017-02-28 16:52:22 -08006468#endif // CONFIG_EXT_INTER && !CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07006469 ref_mv[ref] = x->mbmi_ext->ref_mvs[refs[ref]][0];
6470
6471 if (scaled_ref_frame[ref]) {
6472 int i;
6473 // Swap out the reference frame for a version that's been scaled to
6474 // match the resolution of the current frame, allowing the existing
6475 // motion search code to be used without additional modifications.
6476 for (i = 0; i < MAX_MB_PLANE; i++)
6477 backup_yv12[ref][i] = xd->plane[i].pre[ref];
Yaowu Xuf883b422016-08-30 14:01:10 -07006478 av1_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
6479 NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006480 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006481 }
6482
Zoe Liu85b66462017-04-20 14:28:19 -07006483#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6484 if (!has_second_ref(mbmi)) {
6485 assert(is_inter_singleref_comp_mode(mbmi->mode));
6486 // NOTE: For single ref comp mode, set up the 2nd set of ref_mv/pre_planes
6487 // all from the 1st reference frame, i.e. refs[0].
6488 ref_mv[1] = x->mbmi_ext->ref_mvs[refs[0]][0];
6489 if (scaled_ref_frame[0]) {
6490 int i;
6491 // Swap out the reference frame for a version that's been scaled to
6492 // match the resolution of the current frame, allowing the existing
6493 // motion search code to be used without additional modifications.
6494 for (i = 0; i < MAX_MB_PLANE; i++)
6495 backup_yv12[1][i] = xd->plane[i].pre[1];
6496 av1_setup_pre_planes(xd, 1, scaled_ref_frame[0], mi_row, mi_col, NULL);
6497 }
6498 }
6499#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6500
Yaowu Xuc27fc142016-08-22 16:08:15 -07006501// Since we have scaled the reference frames to match the size of the current
6502// frame we must use a unit scaling factor during mode selection.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006503#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07006504 av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width,
6505 cm->height, cm->use_highbitdepth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006506#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006507 av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width,
6508 cm->height);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006509#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006510
Zoe Liu85b66462017-04-20 14:28:19 -07006511// Allow joint search multiple times iteratively for each reference frame
6512// and break out of the search loop if it couldn't find a better mv.
6513#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6514 const int num_ites =
6515 (has_second_ref(mbmi) || mbmi->mode == SR_NEW_NEWMV) ? 4 : 1;
6516 const int start_ite = has_second_ref(mbmi) ? 0 : 1;
6517 for (ite = start_ite; ite < (start_ite + num_ites); ite++) {
6518#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006519 for (ite = 0; ite < 4; ite++) {
Zoe Liu85b66462017-04-20 14:28:19 -07006520#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07006521 struct buf_2d ref_yv12[2];
6522 int bestsme = INT_MAX;
6523 int sadpb = x->sadperbit16;
6524 MV *const best_mv = &x->best_mv.as_mv;
6525 int search_range = 3;
6526
Alex Converse0fa0f422017-04-24 12:51:14 -07006527 MvLimits tmp_mv_limits = x->mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006528 int id = ite % 2; // Even iterations search in the first reference frame,
6529 // odd iterations search in the second. The predictor
6530 // found for the 'other' reference frame is factored in.
Angie Chiange3a4c1c2017-02-10 16:26:49 -08006531 const int plane = 0;
David Barkere64d51a2017-06-09 14:52:42 +01006532 ConvolveParams conv_params = get_conv_params(!id, 0, plane);
Sarah Parker4c10a3c2017-04-10 19:37:59 -07006533#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
6534 WarpTypesAllowed warp_types;
6535#if CONFIG_GLOBAL_MOTION
6536 warp_types.global_warp_allowed = is_global[!id];
6537#endif // CONFIG_GLOBAL_MOTION
6538#if CONFIG_WARPED_MOTION
6539 warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL;
6540#endif // CONFIG_WARPED_MOTION
6541#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07006542
6543 // Initialized here because of compiler problem in Visual Studio.
Angie Chiange3a4c1c2017-02-10 16:26:49 -08006544 ref_yv12[0] = xd->plane[plane].pre[0];
6545 ref_yv12[1] = xd->plane[plane].pre[1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006546
Yaowu Xuc27fc142016-08-22 16:08:15 -07006547// Get the prediction block from the 'other' reference frame.
Zoe Liu85b66462017-04-20 14:28:19 -07006548#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6549 MV *const the_other_mv = (has_second_ref(mbmi) || id)
6550 ? &frame_mv[refs[!id]].as_mv
6551 : &frame_comp_mv[refs[0]].as_mv;
6552#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6553
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006554#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006555 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
6556 second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16);
Yaowu Xuf883b422016-08-30 14:01:10 -07006557 av1_highbd_build_inter_predictor(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006558 ref_yv12[!id].buf, ref_yv12[!id].stride, second_pred, pw,
Zoe Liu85b66462017-04-20 14:28:19 -07006559#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6560 the_other_mv,
6561#else // !(CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF)
6562 &frame_mv[refs[!id]].as_mv,
6563#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6564 &sf, pw, ph, 0, mbmi->interp_filter,
Sarah Parker4c10a3c2017-04-10 19:37:59 -07006565#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
6566 &warp_types, p_col, p_row,
6567#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
Sarah Parkerb3ebed12017-03-09 10:52:03 -08006568 plane, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006569 } else {
6570 second_pred = (uint8_t *)second_pred_alloc_16;
Zoe Liu76fcff72017-04-24 17:50:53 -07006571#endif // CONFIG_HIGHBITDEPTH
Zoe Liu85b66462017-04-20 14:28:19 -07006572 av1_build_inter_predictor(
6573 ref_yv12[!id].buf, ref_yv12[!id].stride, second_pred, pw,
6574#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6575 the_other_mv,
6576#else // !(CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF)
6577 &frame_mv[refs[!id]].as_mv,
6578#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6579 &sf, pw, ph, &conv_params, mbmi->interp_filter,
Sarah Parker4c10a3c2017-04-10 19:37:59 -07006580#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
Zoe Liu85b66462017-04-20 14:28:19 -07006581 &warp_types, p_col, p_row, plane, !id,
Sarah Parker4c10a3c2017-04-10 19:37:59 -07006582#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
Zoe Liu85b66462017-04-20 14:28:19 -07006583 MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd);
Zoe Liu76fcff72017-04-24 17:50:53 -07006584#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006585 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006586#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006587
6588 // Do compound motion search on the current reference frame.
Angie Chiange3a4c1c2017-02-10 16:26:49 -08006589 if (id) xd->plane[plane].pre[0] = ref_yv12[id];
Alex Converse0fa0f422017-04-24 12:51:14 -07006590 av1_set_mv_search_range(&x->mv_limits, &ref_mv[id].as_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006591
Zoe Liu85b66462017-04-20 14:28:19 -07006592// Use the mv result from the single mode as mv predictor.
6593// Use the mv result from the single mode as mv predictor.
6594#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6595 if (!has_second_ref(mbmi) && id)
6596 *best_mv = frame_comp_mv[refs[0]].as_mv;
6597 else
6598#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6599 *best_mv = frame_mv[refs[id]].as_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006600
6601 best_mv->col >>= 3;
6602 best_mv->row >>= 3;
6603
Zoe Liu85b66462017-04-20 14:28:19 -07006604#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6605 if (!has_second_ref(mbmi))
6606 av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx);
6607 else
6608#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6609 av1_set_mvcost(x, refs[id], id, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006610
6611 // Small-range full-pixel motion search.
6612 bestsme =
Yaowu Xuf883b422016-08-30 14:01:10 -07006613 av1_refining_search_8p_c(x, sadpb, search_range, &cpi->fn_ptr[bsize],
David Barkerc155e012017-05-11 13:54:54 +01006614#if CONFIG_EXT_INTER
6615 mask, mask_stride, id,
6616#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07006617 &ref_mv[id].as_mv, second_pred);
David Barkerc155e012017-05-11 13:54:54 +01006618 if (bestsme < INT_MAX) {
6619#if CONFIG_EXT_INTER
6620 if (mask)
6621 bestsme = av1_get_mvpred_mask_var(x, best_mv, &ref_mv[id].as_mv,
6622 second_pred, mask, mask_stride, id,
6623 &cpi->fn_ptr[bsize], 1);
6624 else
6625#endif
6626 bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv[id].as_mv,
6627 second_pred, &cpi->fn_ptr[bsize], 1);
6628 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006629
Alex Converse0fa0f422017-04-24 12:51:14 -07006630 x->mv_limits = tmp_mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006631
RogerZhou3b635242017-09-19 10:06:46 -07006632#if CONFIG_AMVR
6633 if (cpi->common.cur_frame_mv_precision_level) {
6634 x->best_mv.as_mv.row *= 8;
6635 x->best_mv.as_mv.col *= 8;
6636 }
6637 if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) {
6638#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006639 if (bestsme < INT_MAX) {
RogerZhou3b635242017-09-19 10:06:46 -07006640#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006641 int dis; /* TODO: use dis in distortion calculation later. */
6642 unsigned int sse;
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07006643 bestsme = cpi->find_fractional_mv_step(
6644 x, &ref_mv[id].as_mv, cpi->common.allow_high_precision_mv,
6645 x->errorperbit, &cpi->fn_ptr[bsize], 0,
6646 cpi->sf.mv.subpel_iters_per_step, NULL, x->nmvjointcost, x->mvcost,
6647 &dis, &sse, second_pred,
David Barkerc155e012017-05-11 13:54:54 +01006648#if CONFIG_EXT_INTER
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07006649 mask, mask_stride, id,
David Barkerc155e012017-05-11 13:54:54 +01006650#endif
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07006651 pw, ph, cpi->sf.use_upsampled_references);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006652 }
6653
6654 // Restore the pointer to the first (possibly scaled) prediction buffer.
Angie Chiange3a4c1c2017-02-10 16:26:49 -08006655 if (id) xd->plane[plane].pre[0] = ref_yv12[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006656
6657 if (bestsme < last_besterr[id]) {
Zoe Liu85b66462017-04-20 14:28:19 -07006658#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6659 // NOTE: For single ref comp mode, frame_mv stores the first mv and
6660 // frame_comp_mv stores the second mv.
6661 if (!has_second_ref(mbmi) && id)
6662 frame_comp_mv[refs[0]].as_mv = *best_mv;
6663 else
6664#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6665 frame_mv[refs[id]].as_mv = *best_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006666 last_besterr[id] = bestsme;
Zoe Liu85b66462017-04-20 14:28:19 -07006667#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6668 if (!has_second_ref(mbmi)) last_besterr[!id] = last_besterr[id];
6669#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07006670 } else {
6671 break;
6672 }
6673 }
6674
6675 *rate_mv = 0;
6676
Zoe Liu85b66462017-04-20 14:28:19 -07006677#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6678 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
6679#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006680 for (ref = 0; ref < 2; ++ref) {
Zoe Liu85b66462017-04-20 14:28:19 -07006681#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07006682 if (scaled_ref_frame[ref]) {
6683 // Restore the prediction frame pointers to their unscaled versions.
6684 int i;
6685 for (i = 0; i < MAX_MB_PLANE; i++)
6686 xd->plane[i].pre[ref] = backup_yv12[ref][i];
6687 }
Zoe Liu85b66462017-04-20 14:28:19 -07006688
6689#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6690 if (!has_second_ref(mbmi))
6691 av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006692 else
Zoe Liu85b66462017-04-20 14:28:19 -07006693#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6694 av1_set_mvcost(x, refs[ref], ref, mbmi->ref_mv_idx);
6695
6696#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6697 if (!has_second_ref(mbmi)) {
6698 // NOTE: For single ref comp mode, i.e. !has_second_ref(mbmi) is true, the
6699 // first mv is stored in frame_mv[] and the second mv is stored in
6700 // frame_comp_mv[].
6701 if (compound_ref0_mode(mbmi->mode) == NEWMV) // SR_NEW_NEWMV
6702 *rate_mv += av1_mv_bit_cost(&frame_mv[refs[0]].as_mv,
6703 &x->mbmi_ext->ref_mvs[refs[0]][0].as_mv,
6704 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
6705 assert(compound_ref1_mode(mbmi->mode) == NEWMV);
6706 *rate_mv += av1_mv_bit_cost(&frame_comp_mv[refs[0]].as_mv,
6707 &x->mbmi_ext->ref_mvs[refs[0]][0].as_mv,
6708 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
6709 } else {
6710#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6711#if CONFIG_EXT_INTER && !CONFIG_CB4X4
6712 if (bsize >= BLOCK_8X8)
Fergus Simpson4063a682017-02-28 16:52:22 -08006713#endif // CONFIG_EXT_INTER && !CONFIG_CB4X4
Zoe Liu85b66462017-04-20 14:28:19 -07006714 *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv,
6715 &x->mbmi_ext->ref_mvs[refs[ref]][0].as_mv,
6716 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
6717#if CONFIG_EXT_INTER && !CONFIG_CB4X4
6718 else
6719 *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv,
6720 &ref_mv_sub8x8[ref]->as_mv, x->nmvjointcost,
6721 x->mvcost, MV_COST_WEIGHT);
6722#endif // CONFIG_EXT_INTER && !CONFIG_CB4X4
6723#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6724 }
6725#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07006726 }
Zoe Liu85b66462017-04-20 14:28:19 -07006727
6728#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
6729 if (!has_second_ref(mbmi)) {
6730 if (scaled_ref_frame[0]) {
6731 // Restore the prediction frame pointers to their unscaled versions.
6732 int i;
6733 for (i = 0; i < MAX_MB_PLANE; i++)
6734 xd->plane[i].pre[1] = backup_yv12[1][i];
6735 }
6736 }
6737#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07006738}
6739
Zoe Liuc082bbc2017-05-17 13:31:37 -07006740static void estimate_ref_frame_costs(
6741 const AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
6742 unsigned int *ref_costs_single,
6743#if CONFIG_EXT_COMP_REFS
6744 unsigned int (*ref_costs_comp)[TOTAL_REFS_PER_FRAME],
6745#else
6746 unsigned int *ref_costs_comp,
6747#endif // CONFIG_EXT_COMP_REFS
6748 aom_prob *comp_mode_p) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006749 int seg_ref_active =
6750 segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
6751 if (seg_ref_active) {
6752 memset(ref_costs_single, 0,
6753 TOTAL_REFS_PER_FRAME * sizeof(*ref_costs_single));
Zoe Liuc082bbc2017-05-17 13:31:37 -07006754#if CONFIG_EXT_COMP_REFS
6755 int ref_frame;
6756 for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame)
6757 memset(ref_costs_comp[ref_frame], 0,
6758 TOTAL_REFS_PER_FRAME * sizeof((*ref_costs_comp)[0]));
6759#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006760 memset(ref_costs_comp, 0, TOTAL_REFS_PER_FRAME * sizeof(*ref_costs_comp));
Zoe Liuc082bbc2017-05-17 13:31:37 -07006761#endif // CONFIG_EXT_COMP_REFS
6762
Yaowu Xuc27fc142016-08-22 16:08:15 -07006763 *comp_mode_p = 128;
6764 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07006765 aom_prob intra_inter_p = av1_get_intra_inter_prob(cm, xd);
6766 aom_prob comp_inter_p = 128;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006767
6768 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006769 comp_inter_p = av1_get_reference_mode_prob(cm, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006770 *comp_mode_p = comp_inter_p;
6771 } else {
6772 *comp_mode_p = 128;
6773 }
6774
Yaowu Xuf883b422016-08-30 14:01:10 -07006775 ref_costs_single[INTRA_FRAME] = av1_cost_bit(intra_inter_p, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006776
6777 if (cm->reference_mode != COMPOUND_REFERENCE) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006778 aom_prob ref_single_p1 = av1_get_pred_prob_single_ref_p1(cm, xd);
6779 aom_prob ref_single_p2 = av1_get_pred_prob_single_ref_p2(cm, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006780#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006781 aom_prob ref_single_p3 = av1_get_pred_prob_single_ref_p3(cm, xd);
6782 aom_prob ref_single_p4 = av1_get_pred_prob_single_ref_p4(cm, xd);
6783 aom_prob ref_single_p5 = av1_get_pred_prob_single_ref_p5(cm, xd);
Zoe Liue9b15e22017-07-19 15:53:01 -07006784 aom_prob ref_single_p6 = av1_get_pred_prob_single_ref_p6(cm, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006785#endif // CONFIG_EXT_REFS
6786
Yaowu Xuf883b422016-08-30 14:01:10 -07006787 unsigned int base_cost = av1_cost_bit(intra_inter_p, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006788
6789 ref_costs_single[LAST_FRAME] =
6790#if CONFIG_EXT_REFS
6791 ref_costs_single[LAST2_FRAME] = ref_costs_single[LAST3_FRAME] =
Zoe Liu3ac20932017-08-30 16:35:55 -07006792 ref_costs_single[BWDREF_FRAME] = ref_costs_single[ALTREF2_FRAME] =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006793#endif // CONFIG_EXT_REFS
Zoe Liu3ac20932017-08-30 16:35:55 -07006794 ref_costs_single[GOLDEN_FRAME] =
6795 ref_costs_single[ALTREF_FRAME] = base_cost;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006796
6797#if CONFIG_EXT_REFS
Zoe Liufcf5fa22017-06-26 16:00:38 -07006798 ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0);
6799 ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p1, 0);
6800 ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p1, 0);
6801 ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 0);
6802 ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p1, 1);
Zoe Liue9b15e22017-07-19 15:53:01 -07006803 ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p1, 1);
Zoe Liufcf5fa22017-06-26 16:00:38 -07006804 ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006805
Zoe Liufcf5fa22017-06-26 16:00:38 -07006806 ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p3, 0);
6807 ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p3, 0);
6808 ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p3, 1);
6809 ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p3, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006810
Zoe Liufcf5fa22017-06-26 16:00:38 -07006811 ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p2, 0);
Zoe Liue9b15e22017-07-19 15:53:01 -07006812 ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p2, 0);
Zoe Liufcf5fa22017-06-26 16:00:38 -07006813 ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006814
Zoe Liufcf5fa22017-06-26 16:00:38 -07006815 ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p4, 0);
6816 ref_costs_single[LAST2_FRAME] += av1_cost_bit(ref_single_p4, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006817
Zoe Liufcf5fa22017-06-26 16:00:38 -07006818 ref_costs_single[LAST3_FRAME] += av1_cost_bit(ref_single_p5, 0);
6819 ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p5, 1);
Zoe Liue9b15e22017-07-19 15:53:01 -07006820
Zoe Liue9b15e22017-07-19 15:53:01 -07006821 ref_costs_single[BWDREF_FRAME] += av1_cost_bit(ref_single_p6, 0);
6822 ref_costs_single[ALTREF2_FRAME] += av1_cost_bit(ref_single_p6, 1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07006823#else // !CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006824 ref_costs_single[LAST_FRAME] += av1_cost_bit(ref_single_p1, 0);
6825 ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p1, 1);
6826 ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p1, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006827
Yaowu Xuf883b422016-08-30 14:01:10 -07006828 ref_costs_single[GOLDEN_FRAME] += av1_cost_bit(ref_single_p2, 0);
6829 ref_costs_single[ALTREF_FRAME] += av1_cost_bit(ref_single_p2, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006830#endif // CONFIG_EXT_REFS
6831 } else {
6832 ref_costs_single[LAST_FRAME] = 512;
6833#if CONFIG_EXT_REFS
6834 ref_costs_single[LAST2_FRAME] = 512;
6835 ref_costs_single[LAST3_FRAME] = 512;
6836 ref_costs_single[BWDREF_FRAME] = 512;
Zoe Liue9b15e22017-07-19 15:53:01 -07006837 ref_costs_single[ALTREF2_FRAME] = 512;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006838#endif // CONFIG_EXT_REFS
6839 ref_costs_single[GOLDEN_FRAME] = 512;
6840 ref_costs_single[ALTREF_FRAME] = 512;
6841 }
6842
6843 if (cm->reference_mode != SINGLE_REFERENCE) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006844 aom_prob ref_comp_p = av1_get_pred_prob_comp_ref_p(cm, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006845#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006846 aom_prob ref_comp_p1 = av1_get_pred_prob_comp_ref_p1(cm, xd);
6847 aom_prob ref_comp_p2 = av1_get_pred_prob_comp_ref_p2(cm, xd);
6848 aom_prob bwdref_comp_p = av1_get_pred_prob_comp_bwdref_p(cm, xd);
Zoe Liue9b15e22017-07-19 15:53:01 -07006849 aom_prob bwdref_comp_p1 = av1_get_pred_prob_comp_bwdref_p1(cm, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006850#endif // CONFIG_EXT_REFS
6851
Yaowu Xuf883b422016-08-30 14:01:10 -07006852 unsigned int base_cost = av1_cost_bit(intra_inter_p, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006853
Zoe Liuc082bbc2017-05-17 13:31:37 -07006854#if CONFIG_EXT_COMP_REFS
6855 aom_prob comp_ref_type_p = av1_get_comp_reference_type_prob(cm, xd);
6856 unsigned int ref_bicomp_costs[TOTAL_REFS_PER_FRAME] = { 0 };
6857
6858 ref_bicomp_costs[LAST_FRAME] = ref_bicomp_costs[LAST2_FRAME] =
6859 ref_bicomp_costs[LAST3_FRAME] = ref_bicomp_costs[GOLDEN_FRAME] =
6860#if USE_UNI_COMP_REFS
6861 base_cost + av1_cost_bit(comp_ref_type_p, 1);
6862#else
6863 base_cost;
6864#endif // USE_UNI_COMP_REFS
Zoe Liu3ac20932017-08-30 16:35:55 -07006865 ref_bicomp_costs[BWDREF_FRAME] = ref_bicomp_costs[ALTREF2_FRAME] = 0;
Zoe Liuac889702017-08-23 14:22:58 -07006866 ref_bicomp_costs[ALTREF_FRAME] = 0;
Zoe Liuc082bbc2017-05-17 13:31:37 -07006867
6868 ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0);
6869 ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0);
6870 ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1);
6871 ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1);
6872
6873 ref_bicomp_costs[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1);
6874 ref_bicomp_costs[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0);
6875
6876 ref_bicomp_costs[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0);
6877 ref_bicomp_costs[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1);
6878
6879 ref_bicomp_costs[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0);
Zoe Liuac889702017-08-23 14:22:58 -07006880 ref_bicomp_costs[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p, 0);
Zoe Liuc082bbc2017-05-17 13:31:37 -07006881 ref_bicomp_costs[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1);
6882
Zoe Liuac889702017-08-23 14:22:58 -07006883 ref_bicomp_costs[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p1, 0);
6884 ref_bicomp_costs[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p1, 1);
Zoe Liuac889702017-08-23 14:22:58 -07006885
6886 int ref0, ref1;
Zoe Liuc082bbc2017-05-17 13:31:37 -07006887 for (ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) {
Zoe Liuac889702017-08-23 14:22:58 -07006888 for (ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1) {
6889 ref_costs_comp[ref0][ref1] =
6890 ref_bicomp_costs[ref0] + ref_bicomp_costs[ref1];
6891 }
Zoe Liuc082bbc2017-05-17 13:31:37 -07006892 }
6893
6894 aom_prob uni_comp_ref_p = av1_get_pred_prob_uni_comp_ref_p(cm, xd);
6895 aom_prob uni_comp_ref_p1 = av1_get_pred_prob_uni_comp_ref_p1(cm, xd);
Zoe Liufcf5fa22017-06-26 16:00:38 -07006896 aom_prob uni_comp_ref_p2 = av1_get_pred_prob_uni_comp_ref_p2(cm, xd);
Zoe Liuc082bbc2017-05-17 13:31:37 -07006897
6898 ref_costs_comp[LAST_FRAME][LAST2_FRAME] =
6899 base_cost + av1_cost_bit(comp_ref_type_p, 0) +
6900 av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 0);
Zoe Liufcf5fa22017-06-26 16:00:38 -07006901 ref_costs_comp[LAST_FRAME][LAST3_FRAME] =
6902 base_cost + av1_cost_bit(comp_ref_type_p, 0) +
6903 av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 1) +
6904 av1_cost_bit(uni_comp_ref_p2, 0);
Zoe Liuc082bbc2017-05-17 13:31:37 -07006905 ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] =
6906 base_cost + av1_cost_bit(comp_ref_type_p, 0) +
Zoe Liufcf5fa22017-06-26 16:00:38 -07006907 av1_cost_bit(uni_comp_ref_p, 0) + av1_cost_bit(uni_comp_ref_p1, 1) +
6908 av1_cost_bit(uni_comp_ref_p2, 1);
Zoe Liuc082bbc2017-05-17 13:31:37 -07006909
6910 ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] =
6911 base_cost + av1_cost_bit(comp_ref_type_p, 0) +
6912 av1_cost_bit(uni_comp_ref_p, 1);
6913
6914#else // !CONFIG_EXT_COMP_REFS
6915
Yaowu Xuc27fc142016-08-22 16:08:15 -07006916 ref_costs_comp[LAST_FRAME] =
6917#if CONFIG_EXT_REFS
6918 ref_costs_comp[LAST2_FRAME] = ref_costs_comp[LAST3_FRAME] =
6919#endif // CONFIG_EXT_REFS
6920 ref_costs_comp[GOLDEN_FRAME] = base_cost;
6921
6922#if CONFIG_EXT_REFS
Zoe Liu3ac20932017-08-30 16:35:55 -07006923 ref_costs_comp[BWDREF_FRAME] = ref_costs_comp[ALTREF2_FRAME] =
6924 ref_costs_comp[ALTREF_FRAME] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006925#endif // CONFIG_EXT_REFS
6926
6927#if CONFIG_EXT_REFS
Zoe Liufcf5fa22017-06-26 16:00:38 -07006928 ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0);
6929 ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p, 0);
6930 ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p, 1);
6931 ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006932
Zoe Liufcf5fa22017-06-26 16:00:38 -07006933 ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p1, 1);
6934 ref_costs_comp[LAST2_FRAME] += av1_cost_bit(ref_comp_p1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006935
Zoe Liufcf5fa22017-06-26 16:00:38 -07006936 ref_costs_comp[LAST3_FRAME] += av1_cost_bit(ref_comp_p2, 0);
6937 ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p2, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006938
Zoe Liufcf5fa22017-06-26 16:00:38 -07006939 // NOTE(zoeliu): BWDREF and ALTREF each add an extra cost by coding 1
6940 // more bit.
6941 ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p, 0);
Zoe Liue9b15e22017-07-19 15:53:01 -07006942 ref_costs_comp[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p, 0);
Zoe Liufcf5fa22017-06-26 16:00:38 -07006943 ref_costs_comp[ALTREF_FRAME] += av1_cost_bit(bwdref_comp_p, 1);
Zoe Liue9b15e22017-07-19 15:53:01 -07006944
Zoe Liue9b15e22017-07-19 15:53:01 -07006945 ref_costs_comp[BWDREF_FRAME] += av1_cost_bit(bwdref_comp_p1, 0);
6946 ref_costs_comp[ALTREF2_FRAME] += av1_cost_bit(bwdref_comp_p1, 1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07006947#else // !CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006948 ref_costs_comp[LAST_FRAME] += av1_cost_bit(ref_comp_p, 0);
6949 ref_costs_comp[GOLDEN_FRAME] += av1_cost_bit(ref_comp_p, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006950#endif // CONFIG_EXT_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07006951#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006952 } else {
Zoe Liuc082bbc2017-05-17 13:31:37 -07006953#if CONFIG_EXT_COMP_REFS
Zoe Liuac889702017-08-23 14:22:58 -07006954 int ref0, ref1;
Zoe Liuc082bbc2017-05-17 13:31:37 -07006955 for (ref0 = LAST_FRAME; ref0 <= GOLDEN_FRAME; ++ref0) {
Zoe Liuac889702017-08-23 14:22:58 -07006956 for (ref1 = BWDREF_FRAME; ref1 <= ALTREF_FRAME; ++ref1)
6957 ref_costs_comp[ref0][ref1] = 512;
Zoe Liuc082bbc2017-05-17 13:31:37 -07006958 }
6959 ref_costs_comp[LAST_FRAME][LAST2_FRAME] = 512;
Zoe Liufcf5fa22017-06-26 16:00:38 -07006960 ref_costs_comp[LAST_FRAME][LAST3_FRAME] = 512;
Zoe Liuc082bbc2017-05-17 13:31:37 -07006961 ref_costs_comp[LAST_FRAME][GOLDEN_FRAME] = 512;
6962 ref_costs_comp[BWDREF_FRAME][ALTREF_FRAME] = 512;
6963#else // !CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006964 ref_costs_comp[LAST_FRAME] = 512;
6965#if CONFIG_EXT_REFS
6966 ref_costs_comp[LAST2_FRAME] = 512;
6967 ref_costs_comp[LAST3_FRAME] = 512;
6968 ref_costs_comp[BWDREF_FRAME] = 512;
Zoe Liue9b15e22017-07-19 15:53:01 -07006969 ref_costs_comp[ALTREF2_FRAME] = 512;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006970 ref_costs_comp[ALTREF_FRAME] = 512;
6971#endif // CONFIG_EXT_REFS
6972 ref_costs_comp[GOLDEN_FRAME] = 512;
Zoe Liuc082bbc2017-05-17 13:31:37 -07006973#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006974 }
6975 }
6976}
6977
6978static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
6979 int mode_index,
6980 int64_t comp_pred_diff[REFERENCE_MODES],
6981 int skippable) {
6982 MACROBLOCKD *const xd = &x->e_mbd;
6983
6984 // Take a snapshot of the coding context so it can be
6985 // restored if we decide to encode this way
6986 ctx->skip = x->skip;
6987 ctx->skippable = skippable;
6988 ctx->best_mode_index = mode_index;
6989 ctx->mic = *xd->mi[0];
6990 ctx->mbmi_ext = *x->mbmi_ext;
6991 ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE];
6992 ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE];
6993 ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT];
6994}
6995
clang-format55ce9e02017-02-15 22:27:12 -08006996static void setup_buffer_inter(
6997 const AV1_COMP *const cpi, MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame,
6998 BLOCK_SIZE block_size, int mi_row, int mi_col,
6999 int_mv frame_nearest_mv[TOTAL_REFS_PER_FRAME],
7000 int_mv frame_near_mv[TOTAL_REFS_PER_FRAME],
7001 struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007002 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007003 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
7004 MACROBLOCKD *const xd = &x->e_mbd;
7005 MODE_INFO *const mi = xd->mi[0];
7006 int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
7007 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
7008 MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
7009
7010 assert(yv12 != NULL);
7011
7012 // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this
7013 // use the UV scaling factors.
Yaowu Xuf883b422016-08-30 14:01:10 -07007014 av1_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007015
7016 // Gets an initial list of candidate vectors from neighbours and orders them
Sebastien Alaiwane140c502017-04-27 09:52:34 +02007017 av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame],
7018 mbmi_ext->ref_mv_stack[ref_frame],
Yaowu Xuc27fc142016-08-22 16:08:15 -07007019#if CONFIG_EXT_INTER
Sebastien Alaiwane140c502017-04-27 09:52:34 +02007020 mbmi_ext->compound_mode_context,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007021#endif // CONFIG_EXT_INTER
Sebastien Alaiwane140c502017-04-27 09:52:34 +02007022 candidates, mi_row, mi_col, NULL, NULL,
7023 mbmi_ext->mode_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007024
RogerZhou3b635242017-09-19 10:06:46 -07007025// Candidate refinement carried out at encoder and decoder
7026#if CONFIG_AMVR
7027 av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates,
7028 &frame_nearest_mv[ref_frame], &frame_near_mv[ref_frame],
7029 cm->cur_frame_mv_precision_level);
7030#else
Yaowu Xuf883b422016-08-30 14:01:10 -07007031 av1_find_best_ref_mvs(cm->allow_high_precision_mv, candidates,
7032 &frame_nearest_mv[ref_frame],
7033 &frame_near_mv[ref_frame]);
RogerZhou3b635242017-09-19 10:06:46 -07007034#endif
Jingning Han271bb2c2016-12-14 12:34:46 -08007035// Further refinement that is encode side only to test the top few candidates
7036// in full and choose the best as the centre point for subsequent searches.
7037// The current implementation doesn't support scaling.
7038#if CONFIG_CB4X4
7039 av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
7040 block_size);
7041#else
Yaowu Xuf883b422016-08-30 14:01:10 -07007042 if (!av1_is_scaled(sf) && block_size >= BLOCK_8X8)
7043 av1_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride, ref_frame,
7044 block_size);
Fergus Simpson4063a682017-02-28 16:52:22 -08007045#endif // CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07007046}
7047
Urvang Joshi52648442016-10-13 17:27:51 -07007048static void single_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x,
7049 BLOCK_SIZE bsize, int mi_row, int mi_col,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007050#if CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07007051 int ref_idx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007052#endif // CONFIG_EXT_INTER
7053 int *rate_mv) {
7054 MACROBLOCKD *xd = &x->e_mbd;
Yaowu Xuf883b422016-08-30 14:01:10 -07007055 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007056 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
7057 struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } };
7058 int bestsme = INT_MAX;
7059 int step_param;
7060 int sadpb = x->sadperbit16;
7061 MV mvp_full;
7062#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07007063#if CONFIG_COMPOUND_SINGLEREF
7064 int ref =
7065 has_second_ref(mbmi) ? mbmi->ref_frame[ref_idx] : mbmi->ref_frame[0];
7066#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07007067 int ref = mbmi->ref_frame[ref_idx];
Zoe Liu85b66462017-04-20 14:28:19 -07007068#endif // CONFIG_COMPOUND_SINGLEREF
7069#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07007070 int ref = mbmi->ref_frame[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007071 int ref_idx = 0;
7072#endif // CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07007073 MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007074
Alex Converse0fa0f422017-04-24 12:51:14 -07007075 MvLimits tmp_mv_limits = x->mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007076 int cost_list[5];
7077
7078 const YV12_BUFFER_CONFIG *scaled_ref_frame =
Yaowu Xuf883b422016-08-30 14:01:10 -07007079 av1_get_scaled_ref_frame(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007080
7081 MV pred_mv[3];
7082 pred_mv[0] = x->mbmi_ext->ref_mvs[ref][0].as_mv;
7083 pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv;
7084 pred_mv[2] = x->pred_mv[ref];
7085
Yaowu Xuc27fc142016-08-22 16:08:15 -07007086 if (scaled_ref_frame) {
7087 int i;
7088 // Swap out the reference frame for a version that's been scaled to
7089 // match the resolution of the current frame, allowing the existing
7090 // motion search code to be used without additional modifications.
7091 for (i = 0; i < MAX_MB_PLANE; i++)
7092 backup_yv12[i] = xd->plane[i].pre[ref_idx];
7093
Yaowu Xuf883b422016-08-30 14:01:10 -07007094 av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007095 }
7096
Alex Converse0fa0f422017-04-24 12:51:14 -07007097 av1_set_mv_search_range(&x->mv_limits, &ref_mv);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07007098
Yaowu Xu4306b6e2016-09-27 12:55:32 -07007099 av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07007100
Yaowu Xuc27fc142016-08-22 16:08:15 -07007101 // Work out the size of the first step in the mv step search.
Yaowu Xuf883b422016-08-30 14:01:10 -07007102 // 0 here is maximum length first step. 1 is AOMMAX >> 1 etc.
Yaowu Xuc27fc142016-08-22 16:08:15 -07007103 if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) {
James Zern89a015b2017-08-08 12:39:00 -04007104 // Take the weighted average of the step_params based on the last frame's
Yaowu Xuc27fc142016-08-22 16:08:15 -07007105 // max mv magnitude and that based on the best ref mvs of the current
7106 // block for the given reference.
7107 step_param =
Yaowu Xuf883b422016-08-30 14:01:10 -07007108 (av1_init_search_range(x->max_mv_context[ref]) + cpi->mv_step_param) /
Yaowu Xuc27fc142016-08-22 16:08:15 -07007109 2;
7110 } else {
7111 step_param = cpi->mv_step_param;
7112 }
7113
7114 if (cpi->sf.adaptive_motion_search && bsize < cm->sb_size) {
7115 int boffset =
7116 2 * (b_width_log2_lookup[cm->sb_size] -
Yaowu Xuf883b422016-08-30 14:01:10 -07007117 AOMMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize]));
7118 step_param = AOMMAX(step_param, boffset);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007119 }
7120
7121 if (cpi->sf.adaptive_motion_search) {
7122 int bwl = b_width_log2_lookup[bsize];
7123 int bhl = b_height_log2_lookup[bsize];
7124 int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4);
7125
7126 if (tlevel < 5) step_param += 2;
7127
7128 // prev_mv_sad is not setup for dynamically scaled frames.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07007129 if (cpi->oxcf.resize_mode != RESIZE_RANDOM) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07007130 int i;
7131 for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) {
7132 if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) {
7133 x->pred_mv[ref].row = 0;
7134 x->pred_mv[ref].col = 0;
7135 x->best_mv.as_int = INVALID_MV;
7136
7137 if (scaled_ref_frame) {
Urvang Joshi454280d2016-10-14 16:51:44 -07007138 int j;
7139 for (j = 0; j < MAX_MB_PLANE; ++j)
7140 xd->plane[j].pre[ref_idx] = backup_yv12[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007141 }
7142 return;
7143 }
7144 }
7145 }
7146 }
7147
Alex Converse0fa0f422017-04-24 12:51:14 -07007148 av1_set_mv_search_range(&x->mv_limits, &ref_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007149
Yue Chene9638cc2016-10-10 12:37:54 -07007150#if CONFIG_MOTION_VAR
7151 if (mbmi->motion_mode != SIMPLE_TRANSLATION)
7152 mvp_full = mbmi->mv[0].as_mv;
7153 else
7154#endif // CONFIG_MOTION_VAR
7155 mvp_full = pred_mv[x->mv_best_ref_index[ref]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007156
7157 mvp_full.col >>= 3;
7158 mvp_full.row >>= 3;
7159
7160 x->best_mv.as_int = x->second_best_mv.as_int = INVALID_MV;
7161
Yue Chene9638cc2016-10-10 12:37:54 -07007162#if CONFIG_MOTION_VAR
7163 switch (mbmi->motion_mode) {
7164 case SIMPLE_TRANSLATION:
7165#endif // CONFIG_MOTION_VAR
RogerZhoucc5d35d2017-08-07 22:20:15 -07007166#if CONFIG_HASH_ME
Yue Chene9638cc2016-10-10 12:37:54 -07007167 bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param,
7168 sadpb, cond_cost_list(cpi, cost_list),
RogerZhoucc5d35d2017-08-07 22:20:15 -07007169 &ref_mv, INT_MAX, 1, (MI_SIZE * mi_col),
RogerZhoud15e7c12017-09-26 08:49:28 -07007170 (MI_SIZE * mi_row), 0);
RogerZhoucc5d35d2017-08-07 22:20:15 -07007171#else
7172 bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb,
7173 cond_cost_list(cpi, cost_list), &ref_mv,
7174 INT_MAX, 1);
7175#endif
Yue Chene9638cc2016-10-10 12:37:54 -07007176#if CONFIG_MOTION_VAR
7177 break;
7178 case OBMC_CAUSAL:
7179 bestsme = av1_obmc_full_pixel_diamond(
7180 cpi, x, &mvp_full, step_param, sadpb,
7181 MAX_MVSEARCH_STEPS - 1 - step_param, 1, &cpi->fn_ptr[bsize], &ref_mv,
7182 &(x->best_mv.as_mv), 0);
7183 break;
James Zern88896732017-06-23 15:55:09 -07007184 default: assert(0 && "Invalid motion mode!\n");
Yue Chene9638cc2016-10-10 12:37:54 -07007185 }
7186#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07007187
Alex Converse0fa0f422017-04-24 12:51:14 -07007188 x->mv_limits = tmp_mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007189
RogerZhou3b635242017-09-19 10:06:46 -07007190#if CONFIG_AMVR
7191 if (cpi->common.cur_frame_mv_precision_level) {
7192 x->best_mv.as_mv.row *= 8;
7193 x->best_mv.as_mv.col *= 8;
7194 }
7195 if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) {
7196#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07007197 if (bestsme < INT_MAX) {
RogerZhou3b635242017-09-19 10:06:46 -07007198#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07007199 int dis; /* TODO: use dis in distortion calculation later. */
Yue Chene9638cc2016-10-10 12:37:54 -07007200#if CONFIG_MOTION_VAR
7201 switch (mbmi->motion_mode) {
7202 case SIMPLE_TRANSLATION:
7203#endif // CONFIG_MOTION_VAR
7204 if (cpi->sf.use_upsampled_references) {
7205 int best_mv_var;
7206 const int try_second = x->second_best_mv.as_int != INVALID_MV &&
7207 x->second_best_mv.as_int != x->best_mv.as_int;
Jingning Hanae5cfde2016-11-30 12:01:44 -08007208 const int pw = block_size_wide[bsize];
7209 const int ph = block_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007210
Yue Chene9638cc2016-10-10 12:37:54 -07007211 best_mv_var = cpi->find_fractional_mv_step(
Yaowu Xuc27fc142016-08-22 16:08:15 -07007212 x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit,
7213 &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
7214 cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list),
David Barkerc155e012017-05-11 13:54:54 +01007215 x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL,
7216#if CONFIG_EXT_INTER
7217 NULL, 0, 0,
7218#endif
7219 pw, ph, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007220
Yue Chene9638cc2016-10-10 12:37:54 -07007221 if (try_second) {
Alex Converse0fa0f422017-04-24 12:51:14 -07007222 const int minc =
7223 AOMMAX(x->mv_limits.col_min * 8, ref_mv.col - MV_MAX);
7224 const int maxc =
7225 AOMMIN(x->mv_limits.col_max * 8, ref_mv.col + MV_MAX);
7226 const int minr =
7227 AOMMAX(x->mv_limits.row_min * 8, ref_mv.row - MV_MAX);
7228 const int maxr =
7229 AOMMIN(x->mv_limits.row_max * 8, ref_mv.row + MV_MAX);
Yue Chene9638cc2016-10-10 12:37:54 -07007230 int this_var;
7231 MV best_mv = x->best_mv.as_mv;
7232
7233 x->best_mv = x->second_best_mv;
7234 if (x->best_mv.as_mv.row * 8 <= maxr &&
7235 x->best_mv.as_mv.row * 8 >= minr &&
7236 x->best_mv.as_mv.col * 8 <= maxc &&
7237 x->best_mv.as_mv.col * 8 >= minc) {
7238 this_var = cpi->find_fractional_mv_step(
7239 x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit,
7240 &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
7241 cpi->sf.mv.subpel_iters_per_step,
7242 cond_cost_list(cpi, cost_list), x->nmvjointcost, x->mvcost,
David Barkerc155e012017-05-11 13:54:54 +01007243 &dis, &x->pred_sse[ref], NULL,
7244#if CONFIG_EXT_INTER
7245 NULL, 0, 0,
7246#endif
7247 pw, ph, 1);
Yue Chene9638cc2016-10-10 12:37:54 -07007248 if (this_var < best_mv_var) best_mv = x->best_mv.as_mv;
7249 x->best_mv.as_mv = best_mv;
7250 }
7251 }
Yue Chene9638cc2016-10-10 12:37:54 -07007252 } else {
7253 cpi->find_fractional_mv_step(
7254 x, &ref_mv, cm->allow_high_precision_mv, x->errorperbit,
7255 &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
7256 cpi->sf.mv.subpel_iters_per_step, cond_cost_list(cpi, cost_list),
David Barkerc155e012017-05-11 13:54:54 +01007257 x->nmvjointcost, x->mvcost, &dis, &x->pred_sse[ref], NULL,
7258#if CONFIG_EXT_INTER
7259 NULL, 0, 0,
7260#endif
7261 0, 0, 0);
Yue Chene9638cc2016-10-10 12:37:54 -07007262 }
7263#if CONFIG_MOTION_VAR
7264 break;
7265 case OBMC_CAUSAL:
7266 av1_find_best_obmc_sub_pixel_tree_up(
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07007267 x, &x->best_mv.as_mv, &ref_mv, cm->allow_high_precision_mv,
7268 x->errorperbit, &cpi->fn_ptr[bsize], cpi->sf.mv.subpel_force_stop,
7269 cpi->sf.mv.subpel_iters_per_step, x->nmvjointcost, x->mvcost, &dis,
7270 &x->pred_sse[ref], 0, cpi->sf.use_upsampled_references);
Yue Chene9638cc2016-10-10 12:37:54 -07007271 break;
James Zern88896732017-06-23 15:55:09 -07007272 default: assert(0 && "Invalid motion mode!\n");
Yaowu Xuc27fc142016-08-22 16:08:15 -07007273 }
Yue Chene9638cc2016-10-10 12:37:54 -07007274#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07007275 }
Yaowu Xuf883b422016-08-30 14:01:10 -07007276 *rate_mv = av1_mv_bit_cost(&x->best_mv.as_mv, &ref_mv, x->nmvjointcost,
7277 x->mvcost, MV_COST_WEIGHT);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007278
Yue Chene9638cc2016-10-10 12:37:54 -07007279#if CONFIG_MOTION_VAR
7280 if (cpi->sf.adaptive_motion_search && mbmi->motion_mode == SIMPLE_TRANSLATION)
7281#else
7282 if (cpi->sf.adaptive_motion_search)
7283#endif // CONFIG_MOTION_VAR
7284 x->pred_mv[ref] = x->best_mv.as_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007285
7286 if (scaled_ref_frame) {
7287 int i;
7288 for (i = 0; i < MAX_MB_PLANE; i++)
7289 xd->plane[i].pre[ref_idx] = backup_yv12[i];
7290 }
7291}
7292
David Barkerac37fa32016-12-02 12:30:21 +00007293static INLINE void restore_dst_buf(MACROBLOCKD *xd, BUFFER_SET dst) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07007294 int i;
7295 for (i = 0; i < MAX_MB_PLANE; i++) {
David Barkerac37fa32016-12-02 12:30:21 +00007296 xd->plane[i].dst.buf = dst.plane[i];
7297 xd->plane[i].dst.stride = dst.stride[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007298 }
7299}
7300
Yaowu Xuc27fc142016-08-22 16:08:15 -07007301#if CONFIG_EXT_INTER
David Barker8dd9b572017-05-12 16:31:38 +01007302static void build_second_inter_pred(const AV1_COMP *cpi, MACROBLOCK *x,
David Barkerf19f35f2017-05-22 16:33:22 +01007303 BLOCK_SIZE bsize, const MV *other_mv,
David Barker8dd9b572017-05-12 16:31:38 +01007304 int mi_row, int mi_col, const int block,
7305 int ref_idx, uint8_t *second_pred) {
7306 const AV1_COMMON *const cm = &cpi->common;
7307 const int pw = block_size_wide[bsize];
7308 const int ph = block_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007309 MACROBLOCKD *xd = &x->e_mbd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007310 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Zoe Liu85b66462017-04-20 14:28:19 -07007311#if CONFIG_COMPOUND_SINGLEREF
7312 const int other_ref =
7313 has_second_ref(mbmi) ? mbmi->ref_frame[!ref_idx] : mbmi->ref_frame[0];
7314#else // !CONFIG_COMPOUND_SINGLEREF
David Barker8dd9b572017-05-12 16:31:38 +01007315 const int other_ref = mbmi->ref_frame[!ref_idx];
Zoe Liu85b66462017-04-20 14:28:19 -07007316#endif // CONFIG_COMPOUND_SINGLEREF
David Barker8dd9b572017-05-12 16:31:38 +01007317 struct scale_factors sf;
7318#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
7319 struct macroblockd_plane *const pd = &xd->plane[0];
James Zern89a015b2017-08-08 12:39:00 -04007320 // ic and ir are the 4x4 coordinates of the sub8x8 at index "block"
David Barker8dd9b572017-05-12 16:31:38 +01007321 const int ic = block & 1;
7322 const int ir = (block - ic) >> 1;
7323 const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x) + 4 * ic;
7324 const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y) + 4 * ir;
7325#if CONFIG_GLOBAL_MOTION
7326 WarpedMotionParams *const wm = &xd->global_motion[other_ref];
7327 int is_global = is_global_mv_block(xd->mi[0], block, wm->wmtype);
7328#endif // CONFIG_GLOBAL_MOTION
7329#else
7330 (void)block;
7331#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07007332
Zoe Liu85b66462017-04-20 14:28:19 -07007333// This function should only ever be called for compound modes
7334#if CONFIG_COMPOUND_SINGLEREF
7335 assert(has_second_ref(mbmi) || is_inter_singleref_comp_mode(mbmi->mode));
7336#else // !CONFIG_COMPOUND_SINGLEREF
David Barker8dd9b572017-05-12 16:31:38 +01007337 assert(has_second_ref(mbmi));
Zoe Liu85b66462017-04-20 14:28:19 -07007338#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07007339
David Barker8dd9b572017-05-12 16:31:38 +01007340 struct buf_2d backup_yv12[MAX_MB_PLANE];
7341 const YV12_BUFFER_CONFIG *const scaled_ref_frame =
7342 av1_get_scaled_ref_frame(cpi, other_ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007343
7344 if (scaled_ref_frame) {
David Barker8dd9b572017-05-12 16:31:38 +01007345 int i;
7346 // Swap out the reference frame for a version that's been scaled to
7347 // match the resolution of the current frame, allowing the existing
7348 // motion search code to be used without additional modifications.
7349 for (i = 0; i < MAX_MB_PLANE; i++)
7350 backup_yv12[i] = xd->plane[i].pre[!ref_idx];
7351 av1_setup_pre_planes(xd, !ref_idx, scaled_ref_frame, mi_row, mi_col, NULL);
7352 }
7353
7354// Since we have scaled the reference frames to match the size of the current
7355// frame we must use a unit scaling factor during mode selection.
7356#if CONFIG_HIGHBITDEPTH
7357 av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width,
7358 cm->height, cm->use_highbitdepth);
7359#else
7360 av1_setup_scale_factors_for_frame(&sf, cm->width, cm->height, cm->width,
7361 cm->height);
7362#endif // CONFIG_HIGHBITDEPTH
7363
7364 struct buf_2d ref_yv12;
7365
7366 const int plane = 0;
David Barkere64d51a2017-06-09 14:52:42 +01007367 ConvolveParams conv_params = get_conv_params(!ref_idx, 0, plane);
David Barker8dd9b572017-05-12 16:31:38 +01007368#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
7369 WarpTypesAllowed warp_types;
7370#if CONFIG_GLOBAL_MOTION
7371 warp_types.global_warp_allowed = is_global;
7372#endif // CONFIG_GLOBAL_MOTION
7373#if CONFIG_WARPED_MOTION
7374 warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL;
7375#endif // CONFIG_WARPED_MOTION
7376#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
7377
7378 // Initialized here because of compiler problem in Visual Studio.
7379 ref_yv12 = xd->plane[plane].pre[!ref_idx];
7380
7381// Get the prediction block from the 'other' reference frame.
7382#if CONFIG_HIGHBITDEPTH
7383 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
7384 av1_highbd_build_inter_predictor(
David Barkerf19f35f2017-05-22 16:33:22 +01007385 ref_yv12.buf, ref_yv12.stride, second_pred, pw, other_mv, &sf, pw, ph,
David Barkere64d51a2017-06-09 14:52:42 +01007386 0, mbmi->interp_filter,
David Barker8dd9b572017-05-12 16:31:38 +01007387#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
7388 &warp_types, p_col, p_row,
7389#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
7390 plane, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd);
7391 } else {
7392#endif // CONFIG_HIGHBITDEPTH
7393 av1_build_inter_predictor(
David Barkerf19f35f2017-05-22 16:33:22 +01007394 ref_yv12.buf, ref_yv12.stride, second_pred, pw, other_mv, &sf, pw, ph,
David Barkere64d51a2017-06-09 14:52:42 +01007395 &conv_params, mbmi->interp_filter,
David Barker8dd9b572017-05-12 16:31:38 +01007396#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
7397 &warp_types, p_col, p_row, plane, !ref_idx,
7398#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
7399 MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd);
7400#if CONFIG_HIGHBITDEPTH
7401 }
7402#endif // CONFIG_HIGHBITDEPTH
7403
7404 if (scaled_ref_frame) {
7405 // Restore the prediction frame pointers to their unscaled versions.
7406 int i;
7407 for (i = 0; i < MAX_MB_PLANE; i++)
7408 xd->plane[i].pre[!ref_idx] = backup_yv12[i];
7409 }
7410}
7411
7412// Search for the best mv for one component of a compound,
7413// given that the other component is fixed.
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07007414static void compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
7415 BLOCK_SIZE bsize, MV *this_mv,
7416 int mi_row, int mi_col,
7417 const uint8_t *second_pred,
7418 const uint8_t *mask, int mask_stride,
7419 int *rate_mv, int ref_idx) {
David Barker8dd9b572017-05-12 16:31:38 +01007420 const int pw = block_size_wide[bsize];
7421 const int ph = block_size_high[bsize];
7422 MACROBLOCKD *xd = &x->e_mbd;
7423 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Zoe Liu85b66462017-04-20 14:28:19 -07007424#if CONFIG_COMPOUND_SINGLEREF
7425 const int ref =
7426 has_second_ref(mbmi) ? mbmi->ref_frame[ref_idx] : mbmi->ref_frame[0];
7427#else
David Barker8dd9b572017-05-12 16:31:38 +01007428 const int ref = mbmi->ref_frame[ref_idx];
Zoe Liu85b66462017-04-20 14:28:19 -07007429#endif // CONFIG_COMPOUND_SINGLEREF
David Barkerf19f35f2017-05-22 16:33:22 +01007430 int_mv ref_mv = x->mbmi_ext->ref_mvs[ref][0];
David Barker8dd9b572017-05-12 16:31:38 +01007431 struct macroblockd_plane *const pd = &xd->plane[0];
7432
7433 struct buf_2d backup_yv12[MAX_MB_PLANE];
David Barker8dd9b572017-05-12 16:31:38 +01007434 const YV12_BUFFER_CONFIG *const scaled_ref_frame =
7435 av1_get_scaled_ref_frame(cpi, ref);
7436
Ryan Lei1d1df182017-06-15 11:38:59 -07007437// Check that this is either an interinter or an interintra block
Zoe Liu85b66462017-04-20 14:28:19 -07007438#if CONFIG_COMPOUND_SINGLEREF
Ryan Lei1d1df182017-06-15 11:38:59 -07007439 assert(has_second_ref(mbmi) ||
Zoe Liu85b66462017-04-20 14:28:19 -07007440 // or a single ref comp pred mode
7441 is_inter_singleref_comp_mode(mbmi->mode) ||
David Barker8dd9b572017-05-12 16:31:38 +01007442 (ref_idx == 0 && mbmi->ref_frame[1] == INTRA_FRAME));
Ryan Lei1d1df182017-06-15 11:38:59 -07007443#else
7444 assert(has_second_ref(mbmi) ||
7445 (ref_idx == 0 && mbmi->ref_frame[1] == INTRA_FRAME));
7446#endif // CONFIG_COMPOUND_SINGLEREF
David Barker8dd9b572017-05-12 16:31:38 +01007447
David Barker8dd9b572017-05-12 16:31:38 +01007448 if (scaled_ref_frame) {
7449 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007450 // Swap out the reference frame for a version that's been scaled to
7451 // match the resolution of the current frame, allowing the existing
7452 // motion search code to be used without additional modifications.
7453 for (i = 0; i < MAX_MB_PLANE; i++)
7454 backup_yv12[i] = xd->plane[i].pre[ref_idx];
Yaowu Xuf883b422016-08-30 14:01:10 -07007455 av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007456 }
7457
David Barker8dd9b572017-05-12 16:31:38 +01007458 struct buf_2d orig_yv12;
7459 int bestsme = INT_MAX;
7460 int sadpb = x->sadperbit16;
7461 MV *const best_mv = &x->best_mv.as_mv;
7462 int search_range = 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007463
David Barker8dd9b572017-05-12 16:31:38 +01007464 MvLimits tmp_mv_limits = x->mv_limits;
David Barker8dd9b572017-05-12 16:31:38 +01007465
7466 // Initialized here because of compiler problem in Visual Studio.
7467 if (ref_idx) {
David Barkerf19f35f2017-05-22 16:33:22 +01007468 orig_yv12 = pd->pre[0];
7469 pd->pre[0] = pd->pre[ref_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007470 }
7471
David Barker8dd9b572017-05-12 16:31:38 +01007472 // Do compound motion search on the current reference frame.
7473 av1_set_mv_search_range(&x->mv_limits, &ref_mv.as_mv);
7474
7475 // Use the mv result from the single mode as mv predictor.
David Barkerf19f35f2017-05-22 16:33:22 +01007476 *best_mv = *this_mv;
David Barker8dd9b572017-05-12 16:31:38 +01007477
7478 best_mv->col >>= 3;
7479 best_mv->row >>= 3;
7480
Zoe Liu85b66462017-04-20 14:28:19 -07007481#if CONFIG_COMPOUND_SINGLEREF
7482 if (!has_second_ref(mbmi))
7483 av1_set_mvcost(x, ref, 0, mbmi->ref_mv_idx);
7484 else
7485#endif // CONFIG_COMPOUND_SINGLEREF
7486 av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
David Barker8dd9b572017-05-12 16:31:38 +01007487
7488 // Small-range full-pixel motion search.
7489 bestsme = av1_refining_search_8p_c(x, sadpb, search_range,
7490 &cpi->fn_ptr[bsize], mask, mask_stride,
7491 ref_idx, &ref_mv.as_mv, second_pred);
7492 if (bestsme < INT_MAX) {
7493 if (mask)
7494 bestsme =
7495 av1_get_mvpred_mask_var(x, best_mv, &ref_mv.as_mv, second_pred, mask,
7496 mask_stride, ref_idx, &cpi->fn_ptr[bsize], 1);
7497 else
7498 bestsme = av1_get_mvpred_av_var(x, best_mv, &ref_mv.as_mv, second_pred,
7499 &cpi->fn_ptr[bsize], 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007500 }
7501
Alex Converse0fa0f422017-04-24 12:51:14 -07007502 x->mv_limits = tmp_mv_limits;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007503
RogerZhou3b635242017-09-19 10:06:46 -07007504#if CONFIG_AMVR
7505 if (cpi->common.cur_frame_mv_precision_level) {
7506 x->best_mv.as_mv.row *= 8;
7507 x->best_mv.as_mv.col *= 8;
7508 }
7509 if (bestsme < INT_MAX && cpi->common.cur_frame_mv_precision_level == 0) {
7510#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07007511 if (bestsme < INT_MAX) {
RogerZhou3b635242017-09-19 10:06:46 -07007512#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07007513 int dis; /* TODO: use dis in distortion calculation later. */
David Barker8dd9b572017-05-12 16:31:38 +01007514 unsigned int sse;
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07007515 bestsme = cpi->find_fractional_mv_step(
7516 x, &ref_mv.as_mv, cpi->common.allow_high_precision_mv, x->errorperbit,
7517 &cpi->fn_ptr[bsize], 0, cpi->sf.mv.subpel_iters_per_step, NULL,
7518 x->nmvjointcost, x->mvcost, &dis, &sse, second_pred, mask, mask_stride,
7519 ref_idx, pw, ph, cpi->sf.use_upsampled_references);
David Barker8dd9b572017-05-12 16:31:38 +01007520 }
7521
7522 // Restore the pointer to the first (possibly scaled) prediction buffer.
David Barkerf19f35f2017-05-22 16:33:22 +01007523 if (ref_idx) pd->pre[0] = orig_yv12;
David Barker8dd9b572017-05-12 16:31:38 +01007524
Yue Chenf03907a2017-05-31 12:04:04 -07007525 if (bestsme < INT_MAX) *this_mv = *best_mv;
David Barker8dd9b572017-05-12 16:31:38 +01007526
7527 *rate_mv = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007528
7529 if (scaled_ref_frame) {
David Barker8dd9b572017-05-12 16:31:38 +01007530 // Restore the prediction frame pointers to their unscaled versions.
7531 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007532 for (i = 0; i < MAX_MB_PLANE; i++)
7533 xd->plane[i].pre[ref_idx] = backup_yv12[i];
7534 }
David Barker8dd9b572017-05-12 16:31:38 +01007535
Zoe Liu85b66462017-04-20 14:28:19 -07007536#if CONFIG_COMPOUND_SINGLEREF
7537 if (!has_second_ref(mbmi))
7538 av1_set_mvcost(x, ref, 0, mbmi->ref_mv_idx);
7539 else
7540#endif // CONFIG_COMPOUND_SINGLEREF
7541 av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
David Barkerf19f35f2017-05-22 16:33:22 +01007542 *rate_mv += av1_mv_bit_cost(this_mv, &ref_mv.as_mv, x->nmvjointcost,
7543 x->mvcost, MV_COST_WEIGHT);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007544}
7545
David Barker8dd9b572017-05-12 16:31:38 +01007546// Wrapper for compound_single_motion_search, for the common case
7547// where the second prediction is also an inter mode.
7548static void compound_single_motion_search_interinter(
7549 const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int_mv *frame_mv,
Zoe Liu85b66462017-04-20 14:28:19 -07007550#if CONFIG_COMPOUND_SINGLEREF
7551 int_mv *frame_comp_mv,
7552#endif // CONFIG_COMPOUND_SINGLEREF
David Barkerf19f35f2017-05-22 16:33:22 +01007553 int mi_row, int mi_col, const uint8_t *mask, int mask_stride, int *rate_mv,
7554 const int block, int ref_idx) {
7555 MACROBLOCKD *xd = &x->e_mbd;
7556 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
7557
Zoe Liu85b66462017-04-20 14:28:19 -07007558// This function should only ever be called for compound modes
7559#if CONFIG_COMPOUND_SINGLEREF
7560 int is_singleref_comp_mode =
7561 !has_second_ref(mbmi) && is_inter_singleref_comp_mode(mbmi->mode);
7562 assert(has_second_ref(mbmi) || is_singleref_comp_mode);
7563 if (is_singleref_comp_mode && ref_idx) assert(frame_comp_mv);
7564#else // !CONFIG_COMPOUND_SINGLEREF
David Barkerf19f35f2017-05-22 16:33:22 +01007565 assert(has_second_ref(mbmi));
Zoe Liu85b66462017-04-20 14:28:19 -07007566#endif // CONFIG_COMPOUND_SINGLEREF
David Barker8dd9b572017-05-12 16:31:38 +01007567
7568// Prediction buffer from second frame.
7569#if CONFIG_HIGHBITDEPTH
David Barker8dd9b572017-05-12 16:31:38 +01007570 DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]);
7571 uint8_t *second_pred;
7572 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
7573 second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16);
7574 else
7575 second_pred = (uint8_t *)second_pred_alloc_16;
7576#else
7577 DECLARE_ALIGNED(16, uint8_t, second_pred[MAX_SB_SQUARE]);
7578#endif // CONFIG_HIGHBITDEPTH
7579
Zoe Liu85b66462017-04-20 14:28:19 -07007580#if CONFIG_COMPOUND_SINGLEREF
7581 MV *this_mv = has_second_ref(mbmi)
7582 ? &frame_mv[mbmi->ref_frame[ref_idx]].as_mv
7583 : (ref_idx ? &frame_comp_mv[mbmi->ref_frame[0]].as_mv
7584 : &frame_mv[mbmi->ref_frame[0]].as_mv);
7585 const MV *other_mv =
7586 has_second_ref(mbmi)
7587 ? &frame_mv[mbmi->ref_frame[!ref_idx]].as_mv
7588 : (ref_idx ? &frame_mv[mbmi->ref_frame[0]].as_mv
7589 : &frame_comp_mv[mbmi->ref_frame[0]].as_mv);
7590#else // !CONFIG_COMPOUND_SINGLEREF
David Barkerf19f35f2017-05-22 16:33:22 +01007591 MV *this_mv = &frame_mv[mbmi->ref_frame[ref_idx]].as_mv;
7592 const MV *other_mv = &frame_mv[mbmi->ref_frame[!ref_idx]].as_mv;
Zoe Liu85b66462017-04-20 14:28:19 -07007593#endif // CONFIG_COMPOUND_SINGLEREF
David Barkerf19f35f2017-05-22 16:33:22 +01007594
7595 build_second_inter_pred(cpi, x, bsize, other_mv, mi_row, mi_col, block,
David Barker8dd9b572017-05-12 16:31:38 +01007596 ref_idx, second_pred);
7597
David Barkerf19f35f2017-05-22 16:33:22 +01007598 compound_single_motion_search(cpi, x, bsize, this_mv, mi_row, mi_col,
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07007599 second_pred, mask, mask_stride, rate_mv,
David Barkerf19f35f2017-05-22 16:33:22 +01007600 ref_idx);
David Barker8dd9b572017-05-12 16:31:38 +01007601}
7602
7603#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Sarah Parker6fdc8532016-11-16 17:47:13 -08007604static void do_masked_motion_search_indexed(
David Barkerc155e012017-05-11 13:54:54 +01007605 const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv,
Sarah Parkerb9f757c2017-01-06 17:12:24 -08007606 const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE bsize,
Zoe Liu7f24e1b2017-03-17 17:42:05 -07007607 int mi_row, int mi_col, int_mv *tmp_mv, int *rate_mv, int which) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07007608 // NOTE: which values: 0 - 0 only, 1 - 1 only, 2 - both
7609 MACROBLOCKD *xd = &x->e_mbd;
7610 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
7611 BLOCK_SIZE sb_type = mbmi->sb_type;
7612 const uint8_t *mask;
Jingning Hanae5cfde2016-11-30 12:01:44 -08007613 const int mask_stride = block_size_wide[bsize];
Sarah Parker569edda2016-12-14 14:57:38 -08007614
Sarah Parkerb9f757c2017-01-06 17:12:24 -08007615 mask = av1_get_compound_type_mask(comp_data, sb_type);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007616
David Barker8dd9b572017-05-12 16:31:38 +01007617 int_mv frame_mv[TOTAL_REFS_PER_FRAME];
Zoe Liu85b66462017-04-20 14:28:19 -07007618#if CONFIG_COMPOUND_SINGLEREF
7619 int_mv frame_comp_mv[TOTAL_REFS_PER_FRAME];
7620#endif // CONFIG_COMPOUND_SINGLEREF
David Barker8dd9b572017-05-12 16:31:38 +01007621 MV_REFERENCE_FRAME rf[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] };
7622 assert(bsize >= BLOCK_8X8 || CONFIG_CB4X4);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007623
David Barker8dd9b572017-05-12 16:31:38 +01007624 frame_mv[rf[0]].as_int = cur_mv[0].as_int;
Zoe Liu85b66462017-04-20 14:28:19 -07007625#if CONFIG_COMPOUND_SINGLEREF
7626 if (!has_second_ref(mbmi))
7627 frame_comp_mv[rf[0]].as_int = cur_mv[1].as_int;
7628 else
7629#endif // CONFIG_COMPOUND_SINGLEREF
7630 frame_mv[rf[1]].as_int = cur_mv[1].as_int;
David Barkerf19f35f2017-05-22 16:33:22 +01007631 if (which == 0 || which == 1) {
Zoe Liu85b66462017-04-20 14:28:19 -07007632 compound_single_motion_search_interinter(
7633 cpi, x, bsize, frame_mv,
7634#if CONFIG_COMPOUND_SINGLEREF
7635 has_second_ref(mbmi) ? NULL : frame_comp_mv,
7636#endif // CONFIG_COMPOUND_SINGLEREF
7637 mi_row, mi_col, mask, mask_stride, rate_mv, 0, which);
David Barkerf19f35f2017-05-22 16:33:22 +01007638 } else if (which == 2) {
Zoe Liu85b66462017-04-20 14:28:19 -07007639 joint_motion_search(cpi, x, bsize, frame_mv,
7640#if CONFIG_COMPOUND_SINGLEREF
7641 has_second_ref(mbmi) ? NULL : frame_comp_mv,
7642#endif // CONFIG_COMPOUND_SINGLEREF
7643 mi_row, mi_col, NULL, mask, mask_stride, rate_mv, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007644 }
David Barker8dd9b572017-05-12 16:31:38 +01007645 tmp_mv[0].as_int = frame_mv[rf[0]].as_int;
Zoe Liu85b66462017-04-20 14:28:19 -07007646#if CONFIG_COMPOUND_SINGLEREF
7647 if (!has_second_ref(mbmi))
7648 tmp_mv[1].as_int = frame_comp_mv[rf[0]].as_int;
7649 else // comp ref
7650#endif // CONFIG_COMPOUND_SINGLEREF
7651 tmp_mv[1].as_int = frame_mv[rf[1]].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007652}
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00007653#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07007654#endif // CONFIG_EXT_INTER
7655
James Zern89a015b2017-08-08 12:39:00 -04007656// In some situations we want to discount the apparent cost of a new motion
Yaowu Xuc27fc142016-08-22 16:08:15 -07007657// vector. Where there is a subtle motion field and especially where there is
7658// low spatial complexity then it can be hard to cover the cost of a new motion
7659// vector in a single block, even if that motion vector reduces distortion.
7660// However, once established that vector may be usable through the nearest and
7661// near mv modes to reduce distortion in subsequent blocks and also improve
7662// visual quality.
Urvang Joshi52648442016-10-13 17:27:51 -07007663static int discount_newmv_test(const AV1_COMP *const cpi, int this_mode,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007664 int_mv this_mv,
7665 int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME],
7666 int ref_frame) {
7667 return (!cpi->rc.is_src_frame_alt_ref && (this_mode == NEWMV) &&
7668 (this_mv.as_int != 0) &&
7669 ((mode_mv[NEARESTMV][ref_frame].as_int == 0) ||
7670 (mode_mv[NEARESTMV][ref_frame].as_int == INVALID_MV)) &&
7671 ((mode_mv[NEARMV][ref_frame].as_int == 0) ||
7672 (mode_mv[NEARMV][ref_frame].as_int == INVALID_MV)));
7673}
7674
Yaowu Xu671f2bd2016-09-30 15:07:57 -07007675#define LEFT_TOP_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3)
7676#define RIGHT_BOTTOM_MARGIN ((AOM_BORDER_IN_PIXELS - AOM_INTERP_EXTEND) << 3)
Yaowu Xuc27fc142016-08-22 16:08:15 -07007677
7678// TODO(jingning): this mv clamping function should be block size dependent.
7679static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) {
7680 clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN,
7681 xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN,
7682 xd->mb_to_top_edge - LEFT_TOP_MARGIN,
7683 xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN);
7684}
7685
7686#if CONFIG_EXT_INTER
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00007687#if CONFIG_WEDGE
Yaowu Xuf883b422016-08-30 14:01:10 -07007688static int estimate_wedge_sign(const AV1_COMP *cpi, const MACROBLOCK *x,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007689 const BLOCK_SIZE bsize, const uint8_t *pred0,
7690 int stride0, const uint8_t *pred1, int stride1) {
7691 const struct macroblock_plane *const p = &x->plane[0];
7692 const uint8_t *src = p->src.buf;
7693 int src_stride = p->src.stride;
7694 const int f_index = bsize - BLOCK_8X8;
Jingning Han61418bb2017-01-23 17:12:48 -08007695 const int bw = block_size_wide[bsize];
7696 const int bh = block_size_high[bsize];
Yue Chenf03907a2017-05-31 12:04:04 -07007697 uint32_t esq[2][4];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007698 int64_t tl, br;
7699
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007700#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007701 if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
7702 pred0 = CONVERT_TO_BYTEPTR(pred0);
7703 pred1 = CONVERT_TO_BYTEPTR(pred1);
7704 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007705#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007706
Yue Chenf03907a2017-05-31 12:04:04 -07007707 cpi->fn_ptr[f_index].vf(src, src_stride, pred0, stride0, &esq[0][0]);
7708 cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred0 + bw / 2, stride0,
7709 &esq[0][1]);
7710 cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride,
7711 pred0 + bh / 2 * stride0, stride0, &esq[0][2]);
7712 cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride,
7713 pred0 + bh / 2 * stride0 + bw / 2, stride0,
7714 &esq[0][3]);
7715 cpi->fn_ptr[f_index].vf(src, src_stride, pred1, stride1, &esq[1][0]);
7716 cpi->fn_ptr[f_index].vf(src + bw / 2, src_stride, pred1 + bw / 2, stride1,
7717 &esq[1][1]);
7718 cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride, src_stride,
7719 pred1 + bh / 2 * stride1, stride0, &esq[1][2]);
7720 cpi->fn_ptr[f_index].vf(src + bh / 2 * src_stride + bw / 2, src_stride,
7721 pred1 + bh / 2 * stride1 + bw / 2, stride0,
7722 &esq[1][3]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007723
7724 tl = (int64_t)(esq[0][0] + esq[0][1] + esq[0][2]) -
7725 (int64_t)(esq[1][0] + esq[1][1] + esq[1][2]);
7726 br = (int64_t)(esq[1][3] + esq[1][1] + esq[1][2]) -
7727 (int64_t)(esq[0][3] + esq[0][1] + esq[0][2]);
7728 return (tl + br > 0);
7729}
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00007730#endif // CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07007731#endif // CONFIG_EXT_INTER
7732
7733#if !CONFIG_DUAL_FILTER
James Zern7b9407a2016-05-18 23:48:05 -07007734static InterpFilter predict_interp_filter(
Yaowu Xuf883b422016-08-30 14:01:10 -07007735 const AV1_COMP *cpi, const MACROBLOCK *x, const BLOCK_SIZE bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007736 const int mi_row, const int mi_col,
James Zern7b9407a2016-05-18 23:48:05 -07007737 InterpFilter (*single_filter)[TOTAL_REFS_PER_FRAME]) {
7738 InterpFilter best_filter = SWITCHABLE;
Yaowu Xuf883b422016-08-30 14:01:10 -07007739 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007740 const MACROBLOCKD *xd = &x->e_mbd;
7741 int bsl = mi_width_log2_lookup[bsize];
7742 int pred_filter_search =
7743 cpi->sf.cb_pred_filter_search
7744 ? (((mi_row + mi_col) >> bsl) +
7745 get_chessboard_index(cm->current_video_frame)) &
7746 0x1
7747 : 0;
7748 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
7749 const int is_comp_pred = has_second_ref(mbmi);
7750 const int this_mode = mbmi->mode;
7751 int refs[2] = { mbmi->ref_frame[0],
7752 (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) };
Yaowu Xuc27fc142016-08-22 16:08:15 -07007753 if (pred_filter_search) {
James Zern7b9407a2016-05-18 23:48:05 -07007754 InterpFilter af = SWITCHABLE, lf = SWITCHABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007755 if (xd->up_available) af = xd->mi[-xd->mi_stride]->mbmi.interp_filter;
7756 if (xd->left_available) lf = xd->mi[-1]->mbmi.interp_filter;
7757
7758#if CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07007759 if ((this_mode != NEWMV && this_mode != NEW_NEWMV) || (af == lf))
Yaowu Xuc27fc142016-08-22 16:08:15 -07007760#else
7761 if ((this_mode != NEWMV) || (af == lf))
7762#endif // CONFIG_EXT_INTER
7763 best_filter = af;
7764 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007765 if (is_comp_pred) {
7766 if (cpi->sf.adaptive_mode_search) {
7767#if CONFIG_EXT_INTER
7768 switch (this_mode) {
7769 case NEAREST_NEARESTMV:
7770 if (single_filter[NEARESTMV][refs[0]] ==
7771 single_filter[NEARESTMV][refs[1]])
7772 best_filter = single_filter[NEARESTMV][refs[0]];
7773 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007774 case NEAR_NEARMV:
7775 if (single_filter[NEARMV][refs[0]] == single_filter[NEARMV][refs[1]])
7776 best_filter = single_filter[NEARMV][refs[0]];
7777 break;
7778 case ZERO_ZEROMV:
7779 if (single_filter[ZEROMV][refs[0]] == single_filter[ZEROMV][refs[1]])
7780 best_filter = single_filter[ZEROMV][refs[0]];
7781 break;
7782 case NEW_NEWMV:
7783 if (single_filter[NEWMV][refs[0]] == single_filter[NEWMV][refs[1]])
7784 best_filter = single_filter[NEWMV][refs[0]];
7785 break;
7786 case NEAREST_NEWMV:
7787 if (single_filter[NEARESTMV][refs[0]] ==
7788 single_filter[NEWMV][refs[1]])
7789 best_filter = single_filter[NEARESTMV][refs[0]];
7790 break;
7791 case NEAR_NEWMV:
7792 if (single_filter[NEARMV][refs[0]] == single_filter[NEWMV][refs[1]])
7793 best_filter = single_filter[NEARMV][refs[0]];
7794 break;
7795 case NEW_NEARESTMV:
7796 if (single_filter[NEWMV][refs[0]] ==
7797 single_filter[NEARESTMV][refs[1]])
7798 best_filter = single_filter[NEWMV][refs[0]];
7799 break;
7800 case NEW_NEARMV:
7801 if (single_filter[NEWMV][refs[0]] == single_filter[NEARMV][refs[1]])
7802 best_filter = single_filter[NEWMV][refs[0]];
7803 break;
7804 default:
7805 if (single_filter[this_mode][refs[0]] ==
7806 single_filter[this_mode][refs[1]])
7807 best_filter = single_filter[this_mode][refs[0]];
7808 break;
7809 }
7810#else
7811 if (single_filter[this_mode][refs[0]] ==
7812 single_filter[this_mode][refs[1]])
7813 best_filter = single_filter[this_mode][refs[0]];
7814#endif // CONFIG_EXT_INTER
7815 }
7816 }
Angie Chiang75c22092016-10-25 12:19:16 -07007817 if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) {
7818 best_filter = EIGHTTAP_REGULAR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007819 }
7820 return best_filter;
7821}
Fergus Simpson4063a682017-02-28 16:52:22 -08007822#endif // !CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07007823
7824#if CONFIG_EXT_INTER
7825// Choose the best wedge index and sign
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00007826#if CONFIG_WEDGE
Yaowu Xuf883b422016-08-30 14:01:10 -07007827static int64_t pick_wedge(const AV1_COMP *const cpi, const MACROBLOCK *const x,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007828 const BLOCK_SIZE bsize, const uint8_t *const p0,
7829 const uint8_t *const p1, int *const best_wedge_sign,
7830 int *const best_wedge_index) {
7831 const MACROBLOCKD *const xd = &x->e_mbd;
7832 const struct buf_2d *const src = &x->plane[0].src;
Jingning Hanae5cfde2016-11-30 12:01:44 -08007833 const int bw = block_size_wide[bsize];
7834 const int bh = block_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007835 const int N = bw * bh;
7836 int rate;
7837 int64_t dist;
7838 int64_t rd, best_rd = INT64_MAX;
7839 int wedge_index;
7840 int wedge_sign;
7841 int wedge_types = (1 << get_wedge_bits_lookup(bsize));
7842 const uint8_t *mask;
7843 uint64_t sse;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007844#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007845 const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
7846 const int bd_round = hbd ? (xd->bd - 8) * 2 : 0;
7847#else
7848 const int bd_round = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007849#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007850
7851 DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
7852 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
7853 DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]);
7854 DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]);
7855
7856 int64_t sign_limit;
7857
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007858#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007859 if (hbd) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007860 aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007861 CONVERT_TO_BYTEPTR(p0), bw, xd->bd);
Yaowu Xuf883b422016-08-30 14:01:10 -07007862 aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007863 CONVERT_TO_BYTEPTR(p1), bw, xd->bd);
Yaowu Xuf883b422016-08-30 14:01:10 -07007864 aom_highbd_subtract_block(bh, bw, d10, bw, CONVERT_TO_BYTEPTR(p1), bw,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007865 CONVERT_TO_BYTEPTR(p0), bw, xd->bd);
7866 } else // NOLINT
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007867#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007868 {
Yaowu Xuf883b422016-08-30 14:01:10 -07007869 aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw);
7870 aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw);
7871 aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007872 }
7873
Yaowu Xuf883b422016-08-30 14:01:10 -07007874 sign_limit = ((int64_t)aom_sum_squares_i16(r0, N) -
7875 (int64_t)aom_sum_squares_i16(r1, N)) *
Yaowu Xuc27fc142016-08-22 16:08:15 -07007876 (1 << WEDGE_WEIGHT_BITS) / 2;
7877
Jingning Han61418bb2017-01-23 17:12:48 -08007878 if (N < 64)
7879 av1_wedge_compute_delta_squares_c(ds, r0, r1, N);
7880 else
7881 av1_wedge_compute_delta_squares(ds, r0, r1, N);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007882
7883 for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007884 mask = av1_get_contiguous_soft_mask(wedge_index, 0, bsize);
Jingning Han61418bb2017-01-23 17:12:48 -08007885
7886 // TODO(jingning): Make sse2 functions support N = 16 case
7887 if (N < 64)
7888 wedge_sign = av1_wedge_sign_from_residuals_c(ds, mask, N, sign_limit);
7889 else
7890 wedge_sign = av1_wedge_sign_from_residuals(ds, mask, N, sign_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007891
Yaowu Xuf883b422016-08-30 14:01:10 -07007892 mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize);
Jingning Han61418bb2017-01-23 17:12:48 -08007893 if (N < 64)
7894 sse = av1_wedge_sse_from_residuals_c(r1, d10, mask, N);
7895 else
7896 sse = av1_wedge_sse_from_residuals(r1, d10, mask, N);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007897 sse = ROUND_POWER_OF_TWO(sse, bd_round);
7898
7899 model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist);
Urvang Joshi70006e42017-06-14 16:08:55 -07007900 rd = RDCOST(x->rdmult, rate, dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007901
7902 if (rd < best_rd) {
7903 *best_wedge_index = wedge_index;
7904 *best_wedge_sign = wedge_sign;
7905 best_rd = rd;
7906 }
7907 }
7908
7909 return best_rd;
7910}
7911
7912// Choose the best wedge index the specified sign
7913static int64_t pick_wedge_fixed_sign(
Yaowu Xuf883b422016-08-30 14:01:10 -07007914 const AV1_COMP *const cpi, const MACROBLOCK *const x,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007915 const BLOCK_SIZE bsize, const uint8_t *const p0, const uint8_t *const p1,
7916 const int wedge_sign, int *const best_wedge_index) {
7917 const MACROBLOCKD *const xd = &x->e_mbd;
7918 const struct buf_2d *const src = &x->plane[0].src;
Jingning Hanae5cfde2016-11-30 12:01:44 -08007919 const int bw = block_size_wide[bsize];
7920 const int bh = block_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007921 const int N = bw * bh;
7922 int rate;
7923 int64_t dist;
7924 int64_t rd, best_rd = INT64_MAX;
7925 int wedge_index;
7926 int wedge_types = (1 << get_wedge_bits_lookup(bsize));
7927 const uint8_t *mask;
7928 uint64_t sse;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007929#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007930 const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
7931 const int bd_round = hbd ? (xd->bd - 8) * 2 : 0;
7932#else
7933 const int bd_round = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007934#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007935
7936 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
7937 DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]);
7938
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007939#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007940 if (hbd) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007941 aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007942 CONVERT_TO_BYTEPTR(p1), bw, xd->bd);
Yaowu Xuf883b422016-08-30 14:01:10 -07007943 aom_highbd_subtract_block(bh, bw, d10, bw, CONVERT_TO_BYTEPTR(p1), bw,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007944 CONVERT_TO_BYTEPTR(p0), bw, xd->bd);
7945 } else // NOLINT
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02007946#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07007947 {
Yaowu Xuf883b422016-08-30 14:01:10 -07007948 aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw);
7949 aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007950 }
7951
7952 for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007953 mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize);
Jingning Han61418bb2017-01-23 17:12:48 -08007954 if (N < 64)
7955 sse = av1_wedge_sse_from_residuals_c(r1, d10, mask, N);
7956 else
7957 sse = av1_wedge_sse_from_residuals(r1, d10, mask, N);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007958 sse = ROUND_POWER_OF_TWO(sse, bd_round);
7959
7960 model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist);
Urvang Joshi70006e42017-06-14 16:08:55 -07007961 rd = RDCOST(x->rdmult, rate, dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007962
7963 if (rd < best_rd) {
7964 *best_wedge_index = wedge_index;
7965 best_rd = rd;
7966 }
7967 }
7968
7969 return best_rd;
7970}
7971
Yaowu Xuf883b422016-08-30 14:01:10 -07007972static int64_t pick_interinter_wedge(const AV1_COMP *const cpi,
Sarah Parker2d0e9b72017-05-04 01:34:16 +00007973 MACROBLOCK *const x,
Yaowu Xuc27fc142016-08-22 16:08:15 -07007974 const BLOCK_SIZE bsize,
7975 const uint8_t *const p0,
7976 const uint8_t *const p1) {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00007977 MACROBLOCKD *const xd = &x->e_mbd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007978 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Jingning Hanae5cfde2016-11-30 12:01:44 -08007979 const int bw = block_size_wide[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07007980
7981 int64_t rd;
7982 int wedge_index = -1;
7983 int wedge_sign = 0;
7984
Sarah Parker42d96102017-01-31 21:05:27 -08007985 assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07007986 assert(cpi->common.allow_masked_compound);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007987
7988 if (cpi->sf.fast_wedge_sign_estimate) {
7989 wedge_sign = estimate_wedge_sign(cpi, x, bsize, p0, bw, p1, bw);
7990 rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, wedge_sign, &wedge_index);
7991 } else {
7992 rd = pick_wedge(cpi, x, bsize, p0, p1, &wedge_sign, &wedge_index);
7993 }
7994
Sarah Parker2d0e9b72017-05-04 01:34:16 +00007995 mbmi->wedge_sign = wedge_sign;
7996 mbmi->wedge_index = wedge_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007997 return rd;
7998}
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00007999#endif // CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07008000
Sarah Parker569edda2016-12-14 14:57:38 -08008001#if CONFIG_COMPOUND_SEGMENT
Sarah Parkerddcea392017-04-25 15:57:22 -07008002static int64_t pick_interinter_seg(const AV1_COMP *const cpi,
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008003 MACROBLOCK *const x, const BLOCK_SIZE bsize,
Sarah Parkerddcea392017-04-25 15:57:22 -07008004 const uint8_t *const p0,
8005 const uint8_t *const p1) {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008006 MACROBLOCKD *const xd = &x->e_mbd;
Sarah Parker569edda2016-12-14 14:57:38 -08008007 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
8008 const struct buf_2d *const src = &x->plane[0].src;
8009 const int bw = block_size_wide[bsize];
8010 const int bh = block_size_high[bsize];
8011 const int N = bw * bh;
8012 int rate;
8013 uint64_t sse;
8014 int64_t dist;
Debargha Mukherjeec30934b2017-04-25 01:23:51 -07008015 int64_t rd0;
Sarah Parkerb9f757c2017-01-06 17:12:24 -08008016 SEG_MASK_TYPE cur_mask_type;
8017 int64_t best_rd = INT64_MAX;
8018 SEG_MASK_TYPE best_mask_type = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02008019#if CONFIG_HIGHBITDEPTH
Sarah Parker569edda2016-12-14 14:57:38 -08008020 const int hbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
8021 const int bd_round = hbd ? (xd->bd - 8) * 2 : 0;
8022#else
8023 const int bd_round = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02008024#endif // CONFIG_HIGHBITDEPTH
Sarah Parker569edda2016-12-14 14:57:38 -08008025 DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
8026 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
8027 DECLARE_ALIGNED(32, int16_t, d10[MAX_SB_SQUARE]);
8028
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02008029#if CONFIG_HIGHBITDEPTH
Sarah Parker569edda2016-12-14 14:57:38 -08008030 if (hbd) {
8031 aom_highbd_subtract_block(bh, bw, r0, bw, src->buf, src->stride,
8032 CONVERT_TO_BYTEPTR(p0), bw, xd->bd);
8033 aom_highbd_subtract_block(bh, bw, r1, bw, src->buf, src->stride,
8034 CONVERT_TO_BYTEPTR(p1), bw, xd->bd);
8035 aom_highbd_subtract_block(bh, bw, d10, bw, CONVERT_TO_BYTEPTR(p1), bw,
8036 CONVERT_TO_BYTEPTR(p0), bw, xd->bd);
8037 } else // NOLINT
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02008038#endif // CONFIG_HIGHBITDEPTH
Sarah Parker569edda2016-12-14 14:57:38 -08008039 {
8040 aom_subtract_block(bh, bw, r0, bw, src->buf, src->stride, p0, bw);
8041 aom_subtract_block(bh, bw, r1, bw, src->buf, src->stride, p1, bw);
8042 aom_subtract_block(bh, bw, d10, bw, p1, bw, p0, bw);
8043 }
8044
Sarah Parkerb9f757c2017-01-06 17:12:24 -08008045 // try each mask type and its inverse
8046 for (cur_mask_type = 0; cur_mask_type < SEG_MASK_TYPES; cur_mask_type++) {
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -08008047// build mask and inverse
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02008048#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -08008049 if (hbd)
8050 build_compound_seg_mask_highbd(
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008051 xd->seg_mask, cur_mask_type, CONVERT_TO_BYTEPTR(p0), bw,
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -08008052 CONVERT_TO_BYTEPTR(p1), bw, bsize, bh, bw, xd->bd);
8053 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02008054#endif // CONFIG_HIGHBITDEPTH
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008055 build_compound_seg_mask(xd->seg_mask, cur_mask_type, p0, bw, p1, bw,
8056 bsize, bh, bw);
Sarah Parker569edda2016-12-14 14:57:38 -08008057
Sarah Parkerb9f757c2017-01-06 17:12:24 -08008058 // compute rd for mask
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008059 sse = av1_wedge_sse_from_residuals(r1, d10, xd->seg_mask, N);
Sarah Parkerb9f757c2017-01-06 17:12:24 -08008060 sse = ROUND_POWER_OF_TWO(sse, bd_round);
Sarah Parker569edda2016-12-14 14:57:38 -08008061
Sarah Parkerb9f757c2017-01-06 17:12:24 -08008062 model_rd_from_sse(cpi, xd, bsize, 0, sse, &rate, &dist);
Urvang Joshi70006e42017-06-14 16:08:55 -07008063 rd0 = RDCOST(x->rdmult, rate, dist);
Sarah Parker569edda2016-12-14 14:57:38 -08008064
Sarah Parkerb9f757c2017-01-06 17:12:24 -08008065 if (rd0 < best_rd) {
8066 best_mask_type = cur_mask_type;
8067 best_rd = rd0;
8068 }
8069 }
Sarah Parker569edda2016-12-14 14:57:38 -08008070
Sarah Parkerb9f757c2017-01-06 17:12:24 -08008071 // make final mask
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008072 mbmi->mask_type = best_mask_type;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02008073#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -08008074 if (hbd)
8075 build_compound_seg_mask_highbd(
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008076 xd->seg_mask, mbmi->mask_type, CONVERT_TO_BYTEPTR(p0), bw,
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -08008077 CONVERT_TO_BYTEPTR(p1), bw, bsize, bh, bw, xd->bd);
8078 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02008079#endif // CONFIG_HIGHBITDEPTH
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008080 build_compound_seg_mask(xd->seg_mask, mbmi->mask_type, p0, bw, p1, bw,
8081 bsize, bh, bw);
Sarah Parker569edda2016-12-14 14:57:38 -08008082
Sarah Parkerb9f757c2017-01-06 17:12:24 -08008083 return best_rd;
Sarah Parker569edda2016-12-14 14:57:38 -08008084}
8085#endif // CONFIG_COMPOUND_SEGMENT
8086
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00008087#if CONFIG_WEDGE && CONFIG_INTERINTRA
Yaowu Xuf883b422016-08-30 14:01:10 -07008088static int64_t pick_interintra_wedge(const AV1_COMP *const cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07008089 const MACROBLOCK *const x,
8090 const BLOCK_SIZE bsize,
8091 const uint8_t *const p0,
8092 const uint8_t *const p1) {
8093 const MACROBLOCKD *const xd = &x->e_mbd;
8094 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
8095
8096 int64_t rd;
8097 int wedge_index = -1;
8098
8099 assert(is_interintra_wedge_used(bsize));
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07008100 assert(cpi->common.allow_interintra_compound);
Yaowu Xuc27fc142016-08-22 16:08:15 -07008101
8102 rd = pick_wedge_fixed_sign(cpi, x, bsize, p0, p1, 0, &wedge_index);
8103
8104 mbmi->interintra_wedge_sign = 0;
8105 mbmi->interintra_wedge_index = wedge_index;
8106 return rd;
8107}
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00008108#endif // CONFIG_WEDGE && CONFIG_INTERINTRA
Sarah Parker6fdc8532016-11-16 17:47:13 -08008109
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00008110#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008111static int64_t pick_interinter_mask(const AV1_COMP *const cpi, MACROBLOCK *x,
Sarah Parkerddcea392017-04-25 15:57:22 -07008112 const BLOCK_SIZE bsize,
8113 const uint8_t *const p0,
8114 const uint8_t *const p1) {
8115 const COMPOUND_TYPE compound_type =
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008116 x->e_mbd.mi[0]->mbmi.interinter_compound_type;
Sarah Parkerddcea392017-04-25 15:57:22 -07008117 switch (compound_type) {
8118#if CONFIG_WEDGE
8119 case COMPOUND_WEDGE: return pick_interinter_wedge(cpi, x, bsize, p0, p1);
8120#endif // CONFIG_WEDGE
8121#if CONFIG_COMPOUND_SEGMENT
8122 case COMPOUND_SEG: return pick_interinter_seg(cpi, x, bsize, p0, p1);
8123#endif // CONFIG_COMPOUND_SEGMENT
8124 default: assert(0); return 0;
8125 }
8126}
8127
David Barkerc155e012017-05-11 13:54:54 +01008128static int interinter_compound_motion_search(
8129 const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv,
8130 const BLOCK_SIZE bsize, const int this_mode, int mi_row, int mi_col) {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008131 MACROBLOCKD *const xd = &x->e_mbd;
Sarah Parker6fdc8532016-11-16 17:47:13 -08008132 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
8133 int_mv tmp_mv[2];
David Barkerc155e012017-05-11 13:54:54 +01008134 int tmp_rate_mv = 0;
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008135 const INTERINTER_COMPOUND_DATA compound_data = {
8136#if CONFIG_WEDGE
8137 mbmi->wedge_index,
8138 mbmi->wedge_sign,
8139#endif // CONFIG_WEDGE
8140#if CONFIG_COMPOUND_SEGMENT
8141 mbmi->mask_type,
8142 xd->seg_mask,
8143#endif // CONFIG_COMPOUND_SEGMENT
8144 mbmi->interinter_compound_type
8145 };
Zoe Liu85b66462017-04-20 14:28:19 -07008146#if CONFIG_COMPOUND_SINGLEREF
8147 // NOTE: Mode is needed to identify the compound mode prediction, regardless
8148 // of comp refs or single ref.
8149 mbmi->mode = this_mode;
8150#endif // CONFIG_COMPOUND_SINGLEREF
8151
8152 if (this_mode == NEW_NEWMV
8153#if CONFIG_COMPOUND_SINGLEREF
8154 || this_mode == SR_NEW_NEWMV
8155#endif // CONFIG_COMPOUND_SINGLEREF
8156 ) {
David Barkerc155e012017-05-11 13:54:54 +01008157 do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize,
8158 mi_row, mi_col, tmp_mv, &tmp_rate_mv, 2);
Sarah Parker6fdc8532016-11-16 17:47:13 -08008159 mbmi->mv[0].as_int = tmp_mv[0].as_int;
8160 mbmi->mv[1].as_int = tmp_mv[1].as_int;
8161 } else if (this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV) {
David Barkerc155e012017-05-11 13:54:54 +01008162 do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize,
8163 mi_row, mi_col, tmp_mv, &tmp_rate_mv, 0);
Sarah Parker6fdc8532016-11-16 17:47:13 -08008164 mbmi->mv[0].as_int = tmp_mv[0].as_int;
Zoe Liu85b66462017-04-20 14:28:19 -07008165 } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV
8166#if CONFIG_COMPOUND_SINGLEREF
8167 // || this_mode == SR_NEAREST_NEWMV
8168 || this_mode == SR_NEAR_NEWMV || this_mode == SR_ZERO_NEWMV
8169#endif // CONFIG_COMPOUND_SINGLEREF
8170 ) {
David Barkerc155e012017-05-11 13:54:54 +01008171 do_masked_motion_search_indexed(cpi, x, cur_mv, &compound_data, bsize,
8172 mi_row, mi_col, tmp_mv, &tmp_rate_mv, 1);
Sarah Parker6fdc8532016-11-16 17:47:13 -08008173 mbmi->mv[1].as_int = tmp_mv[1].as_int;
8174 }
8175 return tmp_rate_mv;
8176}
8177
Sarah Parkerddcea392017-04-25 15:57:22 -07008178static int64_t build_and_cost_compound_type(
Sarah Parker569edda2016-12-14 14:57:38 -08008179 const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv,
8180 const BLOCK_SIZE bsize, const int this_mode, int rs2, int rate_mv,
8181 BUFFER_SET *ctx, int *out_rate_mv, uint8_t **preds0, uint8_t **preds1,
8182 int *strides, int mi_row, int mi_col) {
Debargha Mukherjeead8be032017-05-09 15:28:45 -07008183 const AV1_COMMON *const cm = &cpi->common;
Sarah Parker569edda2016-12-14 14:57:38 -08008184 MACROBLOCKD *xd = &x->e_mbd;
8185 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
8186 int rate_sum;
8187 int64_t dist_sum;
8188 int64_t best_rd_cur = INT64_MAX;
8189 int64_t rd = INT64_MAX;
8190 int tmp_skip_txfm_sb;
8191 int64_t tmp_skip_sse_sb;
Sarah Parker2d0e9b72017-05-04 01:34:16 +00008192 const COMPOUND_TYPE compound_type = mbmi->interinter_compound_type;
Sarah Parker569edda2016-12-14 14:57:38 -08008193
Sarah Parkerddcea392017-04-25 15:57:22 -07008194 best_rd_cur = pick_interinter_mask(cpi, x, bsize, *preds0, *preds1);
Urvang Joshi70006e42017-06-14 16:08:55 -07008195 best_rd_cur += RDCOST(x->rdmult, rs2 + rate_mv, 0);
Sarah Parker569edda2016-12-14 14:57:38 -08008196
Sarah Parker2e604882017-01-17 17:31:25 -08008197 if (have_newmv_in_inter_mode(this_mode) &&
Sarah Parkerddcea392017-04-25 15:57:22 -07008198 use_masked_motion_search(compound_type)) {
David Barkerc155e012017-05-11 13:54:54 +01008199 *out_rate_mv = interinter_compound_motion_search(cpi, x, cur_mv, bsize,
8200 this_mode, mi_row, mi_col);
Debargha Mukherjeead8be032017-05-09 15:28:45 -07008201 av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, ctx, bsize);
Sarah Parker6fdc8532016-11-16 17:47:13 -08008202 model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum,
8203 &tmp_skip_txfm_sb, &tmp_skip_sse_sb);
Urvang Joshi70006e42017-06-14 16:08:55 -07008204 rd = RDCOST(x->rdmult, rs2 + *out_rate_mv + rate_sum, dist_sum);
Zoe Liu4d44f5a2016-12-14 17:46:19 -08008205 if (rd >= best_rd_cur) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08008206 mbmi->mv[0].as_int = cur_mv[0].as_int;
8207 mbmi->mv[1].as_int = cur_mv[1].as_int;
8208 *out_rate_mv = rate_mv;
David Barker426a9972017-01-27 11:03:11 +00008209 av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0,
8210#if CONFIG_SUPERTX
8211 0, 0,
8212#endif // CONFIG_SUPERTX
8213 preds0, strides, preds1,
8214 strides);
Sarah Parker6fdc8532016-11-16 17:47:13 -08008215 }
8216 av1_subtract_plane(x, bsize, 0);
8217 rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum,
8218 &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX);
8219 if (rd != INT64_MAX)
Urvang Joshi70006e42017-06-14 16:08:55 -07008220 rd = RDCOST(x->rdmult, rs2 + *out_rate_mv + rate_sum, dist_sum);
Sarah Parker6fdc8532016-11-16 17:47:13 -08008221 best_rd_cur = rd;
8222
8223 } else {
David Barker426a9972017-01-27 11:03:11 +00008224 av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0,
8225#if CONFIG_SUPERTX
8226 0, 0,
8227#endif // CONFIG_SUPERTX
8228 preds0, strides, preds1, strides);
Sarah Parker6fdc8532016-11-16 17:47:13 -08008229 av1_subtract_plane(x, bsize, 0);
8230 rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum,
8231 &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX);
8232 if (rd != INT64_MAX)
Urvang Joshi70006e42017-06-14 16:08:55 -07008233 rd = RDCOST(x->rdmult, rs2 + rate_mv + rate_sum, dist_sum);
Sarah Parker6fdc8532016-11-16 17:47:13 -08008234 best_rd_cur = rd;
8235 }
8236 return best_rd_cur;
8237}
Sarah Parkerddcea392017-04-25 15:57:22 -07008238#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07008239#endif // CONFIG_EXT_INTER
8240
Fergus Simpson073c6f32017-02-17 12:13:48 -08008241typedef struct {
8242#if CONFIG_MOTION_VAR
8243 // Inter prediction buffers and respective strides
8244 uint8_t *above_pred_buf[MAX_MB_PLANE];
8245 int above_pred_stride[MAX_MB_PLANE];
8246 uint8_t *left_pred_buf[MAX_MB_PLANE];
8247 int left_pred_stride[MAX_MB_PLANE];
8248#endif // CONFIG_MOTION_VAR
Zoe Liu7f24e1b2017-03-17 17:42:05 -07008249 int_mv *single_newmv;
Fergus Simpson073c6f32017-02-17 12:13:48 -08008250#if CONFIG_EXT_INTER
8251 // Pointer to array of motion vectors to use for each ref and their rates
8252 // Should point to first of 2 arrays in 2D array
Zoe Liu7f24e1b2017-03-17 17:42:05 -07008253 int *single_newmv_rate;
Fergus Simpson073c6f32017-02-17 12:13:48 -08008254 // Pointer to array of predicted rate-distortion
8255 // Should point to first of 2 arrays in 2D array
8256 int64_t (*modelled_rd)[TOTAL_REFS_PER_FRAME];
Fergus Simpson073c6f32017-02-17 12:13:48 -08008257#endif // CONFIG_EXT_INTER
Fergus Simpson3424c2d2017-03-09 11:48:15 -08008258 InterpFilter single_filter[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME];
Fergus Simpson073c6f32017-02-17 12:13:48 -08008259} HandleInterModeArgs;
8260
Fergus Simpson45509632017-02-22 15:30:50 -08008261static int64_t handle_newmv(const AV1_COMP *const cpi, MACROBLOCK *const x,
8262 const BLOCK_SIZE bsize,
8263 int_mv (*const mode_mv)[TOTAL_REFS_PER_FRAME],
Zoe Liu85b66462017-04-20 14:28:19 -07008264#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
8265 int_mv (*const mode_comp_mv)[TOTAL_REFS_PER_FRAME],
8266#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Fergus Simpson45509632017-02-22 15:30:50 -08008267 const int mi_row, const int mi_col,
8268 int *const rate_mv, int_mv *const single_newmv,
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08008269 HandleInterModeArgs *const args) {
Fergus Simpson45509632017-02-22 15:30:50 -08008270 const MACROBLOCKD *const xd = &x->e_mbd;
8271 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
8272 const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
8273 const int is_comp_pred = has_second_ref(mbmi);
8274 const PREDICTION_MODE this_mode = mbmi->mode;
8275#if CONFIG_EXT_INTER
Fergus Simpson45509632017-02-22 15:30:50 -08008276 const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME);
8277#endif // CONFIG_EXT_INTER
8278 int_mv *const frame_mv = mode_mv[this_mode];
Zoe Liu85b66462017-04-20 14:28:19 -07008279#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
8280 int_mv *const frame_comp_mv = mode_comp_mv[this_mode];
8281#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Fergus Simpson45509632017-02-22 15:30:50 -08008282 const int refs[2] = { mbmi->ref_frame[0],
8283 mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1] };
8284 int i;
8285
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08008286 (void)args;
Fergus Simpson45509632017-02-22 15:30:50 -08008287
8288 if (is_comp_pred) {
8289#if CONFIG_EXT_INTER
8290 for (i = 0; i < 2; ++i) {
Zoe Liu7f24e1b2017-03-17 17:42:05 -07008291 single_newmv[refs[i]].as_int = args->single_newmv[refs[i]].as_int;
Fergus Simpson45509632017-02-22 15:30:50 -08008292 }
8293
8294 if (this_mode == NEW_NEWMV) {
8295 frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int;
8296 frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int;
8297
8298 if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
Zoe Liu85b66462017-04-20 14:28:19 -07008299 joint_motion_search(cpi, x, bsize, frame_mv,
8300#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
8301 NULL, // int_mv *frame_comp_mv
8302#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
8303 mi_row, mi_col, NULL, NULL, 0, rate_mv, 0);
Fergus Simpson45509632017-02-22 15:30:50 -08008304 } else {
8305 *rate_mv = 0;
8306 for (i = 0; i < 2; ++i) {
Zoe Liu122f3942017-04-25 11:18:38 -07008307 av1_set_mvcost(x, refs[i], i, mbmi->ref_mv_idx);
Fergus Simpson45509632017-02-22 15:30:50 -08008308 *rate_mv += av1_mv_bit_cost(
8309 &frame_mv[refs[i]].as_mv, &mbmi_ext->ref_mvs[refs[i]][0].as_mv,
8310 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
8311 }
8312 }
8313 } else if (this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV) {
8314 frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int;
David Barker8dd9b572017-05-12 16:31:38 +01008315 if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
8316 frame_mv[refs[0]].as_int =
8317 mode_mv[compound_ref0_mode(this_mode)][refs[0]].as_int;
Zoe Liu85b66462017-04-20 14:28:19 -07008318 compound_single_motion_search_interinter(cpi, x, bsize, frame_mv,
8319#if CONFIG_COMPOUND_SINGLEREF
8320 NULL,
8321#endif // CONFIG_COMPOUND_SINGLEREF
8322 mi_row, mi_col, NULL, 0,
8323 rate_mv, 0, 1);
David Barker8dd9b572017-05-12 16:31:38 +01008324 } else {
8325 av1_set_mvcost(x, refs[1], 1, mbmi->ref_mv_idx);
8326 *rate_mv = av1_mv_bit_cost(&frame_mv[refs[1]].as_mv,
8327 &mbmi_ext->ref_mvs[refs[1]][0].as_mv,
8328 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
8329 }
Fergus Simpson45509632017-02-22 15:30:50 -08008330 } else {
David Barkercb03dc32017-04-07 13:05:09 +01008331 assert(this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV);
Fergus Simpson45509632017-02-22 15:30:50 -08008332 frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int;
David Barker8dd9b572017-05-12 16:31:38 +01008333 if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
8334 frame_mv[refs[1]].as_int =
8335 mode_mv[compound_ref1_mode(this_mode)][refs[1]].as_int;
Zoe Liu85b66462017-04-20 14:28:19 -07008336 compound_single_motion_search_interinter(cpi, x, bsize, frame_mv,
8337#if CONFIG_COMPOUND_SINGLEREF
8338 NULL,
8339#endif // CONFIG_COMPOUND_SINGLEREF
8340 mi_row, mi_col, NULL, 0,
8341 rate_mv, 0, 0);
David Barker8dd9b572017-05-12 16:31:38 +01008342 } else {
8343 av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx);
8344 *rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv,
8345 &mbmi_ext->ref_mvs[refs[0]][0].as_mv,
8346 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
8347 }
Fergus Simpson45509632017-02-22 15:30:50 -08008348 }
Zoe Liu85b66462017-04-20 14:28:19 -07008349#else // !CONFIG_EXT_INTER
Fergus Simpson45509632017-02-22 15:30:50 -08008350 // Initialize mv using single prediction mode result.
8351 frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int;
8352 frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int;
8353
8354 if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
Zoe Liu122f3942017-04-25 11:18:38 -07008355 joint_motion_search(cpi, x, bsize, frame_mv, mi_row, mi_col, rate_mv, 0);
Fergus Simpson45509632017-02-22 15:30:50 -08008356 } else {
8357 *rate_mv = 0;
8358 for (i = 0; i < 2; ++i) {
Zoe Liu122f3942017-04-25 11:18:38 -07008359 av1_set_mvcost(x, refs[i], i, mbmi->ref_mv_idx);
Fergus Simpson45509632017-02-22 15:30:50 -08008360 *rate_mv += av1_mv_bit_cost(&frame_mv[refs[i]].as_mv,
8361 &mbmi_ext->ref_mvs[refs[i]][0].as_mv,
8362 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
8363 }
8364 }
8365#endif // CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07008366#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
8367 } else if (is_inter_singleref_comp_mode(this_mode)) {
8368 // Single ref comp mode
8369 const int mode0 = compound_ref0_mode(this_mode);
8370
8371 single_newmv[refs[0]].as_int = args->single_newmv[refs[0]].as_int;
8372 frame_mv[refs[0]].as_int = (mode0 == NEWMV)
8373 ? single_newmv[refs[0]].as_int
8374 : mode_mv[mode0][refs[0]].as_int;
8375 assert(compound_ref1_mode(this_mode) == NEWMV);
8376 frame_comp_mv[refs[0]].as_int = single_newmv[refs[0]].as_int;
8377
8378 if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
8379 if (this_mode == SR_NEW_NEWMV) {
8380 joint_motion_search(cpi, x, bsize, frame_mv, frame_comp_mv, mi_row,
8381 mi_col, NULL, NULL, 0, rate_mv, 0);
8382 } else {
8383 assert( // this_mode == SR_NEAREST_NEWMV ||
8384 this_mode == SR_NEAR_NEWMV || this_mode == SR_ZERO_NEWMV);
8385 compound_single_motion_search_interinter(cpi, x, bsize, frame_mv,
8386 frame_comp_mv, mi_row, mi_col,
8387 NULL, 0, rate_mv, 0, 1);
8388 }
8389 } else {
8390 *rate_mv = 0;
8391 av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx);
8392 if (mode0 == NEWMV)
8393 *rate_mv += av1_mv_bit_cost(&frame_mv[refs[0]].as_mv,
8394 &mbmi_ext->ref_mvs[refs[0]][0].as_mv,
8395 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
8396 *rate_mv += av1_mv_bit_cost(&frame_comp_mv[refs[0]].as_mv,
8397 &mbmi_ext->ref_mvs[refs[0]][0].as_mv,
8398 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
8399 }
8400#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Fergus Simpson45509632017-02-22 15:30:50 -08008401 } else {
8402#if CONFIG_EXT_INTER
8403 if (is_comp_interintra_pred) {
Zoe Liu7f24e1b2017-03-17 17:42:05 -07008404 x->best_mv = args->single_newmv[refs[0]];
8405 *rate_mv = args->single_newmv_rate[refs[0]];
Fergus Simpson45509632017-02-22 15:30:50 -08008406 } else {
Zoe Liu7f24e1b2017-03-17 17:42:05 -07008407 single_motion_search(cpi, x, bsize, mi_row, mi_col, 0, rate_mv);
8408 args->single_newmv[refs[0]] = x->best_mv;
8409 args->single_newmv_rate[refs[0]] = *rate_mv;
Fergus Simpson45509632017-02-22 15:30:50 -08008410 }
8411#else
8412 single_motion_search(cpi, x, bsize, mi_row, mi_col, rate_mv);
8413 single_newmv[refs[0]] = x->best_mv;
8414#endif // CONFIG_EXT_INTER
8415
8416 if (x->best_mv.as_int == INVALID_MV) return INT64_MAX;
8417
8418 frame_mv[refs[0]] = x->best_mv;
8419 xd->mi[0]->bmi[0].as_mv[0] = x->best_mv;
8420
8421 // Estimate the rate implications of a new mv but discount this
8422 // under certain circumstances where we want to help initiate a weak
8423 // motion field, where the distortion gain for a single block may not
8424 // be enough to overcome the cost of a new mv.
8425 if (discount_newmv_test(cpi, this_mode, x->best_mv, mode_mv, refs[0])) {
8426 *rate_mv = AOMMAX(*rate_mv / NEW_MV_DISCOUNT_FACTOR, 1);
8427 }
8428 }
8429
8430 return 0;
8431}
8432
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08008433int64_t interpolation_filter_search(
8434 MACROBLOCK *const x, const AV1_COMP *const cpi, BLOCK_SIZE bsize,
8435 int mi_row, int mi_col, const BUFFER_SET *const tmp_dst,
8436 BUFFER_SET *const orig_dst,
8437 InterpFilter (*const single_filter)[TOTAL_REFS_PER_FRAME],
8438 int64_t *const rd, int *const switchable_rate, int *const skip_txfm_sb,
8439 int64_t *const skip_sse_sb) {
8440 const AV1_COMMON *cm = &cpi->common;
8441 MACROBLOCKD *const xd = &x->e_mbd;
8442 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
8443 int i;
8444 int tmp_rate;
8445 int64_t tmp_dist;
8446
8447 (void)single_filter;
8448
8449 InterpFilter assign_filter = SWITCHABLE;
8450
8451 if (cm->interp_filter == SWITCHABLE) {
8452#if !CONFIG_DUAL_FILTER
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07008453 assign_filter = av1_is_interp_needed(xd)
8454 ? predict_interp_filter(cpi, x, bsize, mi_row, mi_col,
8455 single_filter)
8456 : cm->interp_filter;
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08008457#endif // !CONFIG_DUAL_FILTER
8458 } else {
8459 assign_filter = cm->interp_filter;
8460 }
8461
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07008462 set_default_interp_filters(mbmi, assign_filter);
8463
Yue Chenb23d00a2017-07-28 17:01:21 -07008464 *switchable_rate = av1_get_switchable_rate(cm, x, xd);
Jingning Hanc44009c2017-05-06 11:36:49 -07008465 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, bsize);
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08008466 model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate, &tmp_dist,
8467 skip_txfm_sb, skip_sse_sb);
Urvang Joshi70006e42017-06-14 16:08:55 -07008468 *rd = RDCOST(x->rdmult, *switchable_rate + tmp_rate, tmp_dist);
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08008469
8470 if (assign_filter == SWITCHABLE) {
8471 // do interp_filter search
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07008472 if (av1_is_interp_needed(xd) && av1_is_interp_search_needed(xd)) {
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08008473#if CONFIG_DUAL_FILTER
8474 const int filter_set_size = DUAL_FILTER_SET_SIZE;
8475#else
8476 const int filter_set_size = SWITCHABLE_FILTERS;
8477#endif // CONFIG_DUAL_FILTER
8478 int best_in_temp = 0;
8479#if CONFIG_DUAL_FILTER
8480 InterpFilter best_filter[4];
8481 av1_copy(best_filter, mbmi->interp_filter);
8482#else
8483 InterpFilter best_filter = mbmi->interp_filter;
8484#endif // CONFIG_DUAL_FILTER
8485 restore_dst_buf(xd, *tmp_dst);
8486 // EIGHTTAP_REGULAR mode is calculated beforehand
8487 for (i = 1; i < filter_set_size; ++i) {
8488 int tmp_skip_sb = 0;
8489 int64_t tmp_skip_sse = INT64_MAX;
8490 int tmp_rs;
8491 int64_t tmp_rd;
8492#if CONFIG_DUAL_FILTER
8493 mbmi->interp_filter[0] = filter_sets[i][0];
8494 mbmi->interp_filter[1] = filter_sets[i][1];
8495 mbmi->interp_filter[2] = filter_sets[i][0];
8496 mbmi->interp_filter[3] = filter_sets[i][1];
8497#else
8498 mbmi->interp_filter = (InterpFilter)i;
8499#endif // CONFIG_DUAL_FILTER
Yue Chenb23d00a2017-07-28 17:01:21 -07008500 tmp_rs = av1_get_switchable_rate(cm, x, xd);
Jingning Hanc44009c2017-05-06 11:36:49 -07008501 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, bsize);
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08008502 model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate,
8503 &tmp_dist, &tmp_skip_sb, &tmp_skip_sse);
Urvang Joshi70006e42017-06-14 16:08:55 -07008504 tmp_rd = RDCOST(x->rdmult, tmp_rs + tmp_rate, tmp_dist);
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08008505
8506 if (tmp_rd < *rd) {
8507 *rd = tmp_rd;
Yue Chenb23d00a2017-07-28 17:01:21 -07008508 *switchable_rate = av1_get_switchable_rate(cm, x, xd);
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08008509#if CONFIG_DUAL_FILTER
8510 av1_copy(best_filter, mbmi->interp_filter);
8511#else
8512 best_filter = mbmi->interp_filter;
8513#endif // CONFIG_DUAL_FILTER
8514 *skip_txfm_sb = tmp_skip_sb;
8515 *skip_sse_sb = tmp_skip_sse;
8516 best_in_temp = !best_in_temp;
8517 if (best_in_temp) {
8518 restore_dst_buf(xd, *orig_dst);
8519 } else {
8520 restore_dst_buf(xd, *tmp_dst);
8521 }
8522 }
8523 }
8524 if (best_in_temp) {
8525 restore_dst_buf(xd, *tmp_dst);
8526 } else {
8527 restore_dst_buf(xd, *orig_dst);
8528 }
8529#if CONFIG_DUAL_FILTER
8530 av1_copy(mbmi->interp_filter, best_filter);
8531#else
8532 mbmi->interp_filter = best_filter;
8533#endif // CONFIG_DUAL_FILTER
8534 } else {
8535#if CONFIG_DUAL_FILTER
8536 for (i = 0; i < 4; ++i)
8537 assert(mbmi->interp_filter[i] == EIGHTTAP_REGULAR);
8538#else
8539 assert(mbmi->interp_filter == EIGHTTAP_REGULAR);
8540#endif // CONFIG_DUAL_FILTER
8541 }
8542 }
8543
8544 return 0;
8545}
8546
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008547// TODO(afergs): Refactor the MBMI references in here - there's four
8548// TODO(afergs): Refactor optional args - add them to a struct or remove
8549static int64_t motion_mode_rd(
8550 const AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize,
8551 RD_STATS *rd_stats, RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv,
8552 int *disable_skip, int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME], int mi_row,
8553 int mi_col, HandleInterModeArgs *const args, const int64_t ref_best_rd,
8554 const int *refs, int rate_mv,
8555#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07008556 // only used when WARPED_MOTION is on?
Yunqing Wang68f3ccd2017-05-23 14:43:54 -07008557 int_mv *const single_newmv,
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008558#if CONFIG_EXT_INTER
Yunqing Wang562a3932017-06-20 12:20:45 -07008559 int rate2_bmc_nocoeff, MB_MODE_INFO *best_bmc_mbmi, int rate_mv_bmc,
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008560#endif // CONFIG_EXT_INTER
8561#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8562 int rs, int *skip_txfm_sb, int64_t *skip_sse_sb, BUFFER_SET *orig_dst) {
8563 const AV1_COMMON *const cm = &cpi->common;
8564 MACROBLOCKD *xd = &x->e_mbd;
8565 MODE_INFO *mi = xd->mi[0];
8566 MB_MODE_INFO *mbmi = &mi->mbmi;
8567 const int is_comp_pred = has_second_ref(mbmi);
8568 const PREDICTION_MODE this_mode = mbmi->mode;
8569
8570 (void)mode_mv;
8571 (void)mi_row;
8572 (void)mi_col;
8573 (void)args;
8574 (void)refs;
8575 (void)rate_mv;
8576 (void)is_comp_pred;
8577 (void)this_mode;
Angie Chiangcf85d5c2017-08-20 12:56:19 -07008578#if !CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR
Cheng Chen811bba72017-08-21 17:39:31 -07008579 (void)single_newmv;
8580#endif
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008581
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008582#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8583 MOTION_MODE motion_mode, last_motion_mode_allowed;
8584 int rate2_nocoeff = 0, best_xskip, best_disable_skip = 0;
8585 RD_STATS best_rd_stats, best_rd_stats_y, best_rd_stats_uv;
8586 MB_MODE_INFO base_mbmi, best_mbmi;
8587#if CONFIG_VAR_TX
8588 uint8_t best_blk_skip[MAX_MB_PLANE][MAX_MIB_SIZE * MAX_MIB_SIZE * 4];
8589#endif // CONFIG_VAR_TX
8590#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8591
8592#if CONFIG_WARPED_MOTION
Yunqing Wang1bc82862017-06-28 15:49:48 -07008593#if WARPED_MOTION_SORT_SAMPLES
8594 int pts0[SAMPLES_ARRAY_SIZE], pts_inref0[SAMPLES_ARRAY_SIZE];
8595 int pts_mv0[SAMPLES_ARRAY_SIZE];
8596 int total_samples;
8597#else
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008598 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang1bc82862017-06-28 15:49:48 -07008599#endif // WARPED_MOTION_SORT_SAMPLES
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008600#endif // CONFIG_WARPED_MOTION
8601
8602#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8603 av1_invalid_rd_stats(&best_rd_stats);
8604#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8605
8606 if (cm->interp_filter == SWITCHABLE) rd_stats->rate += rs;
8607#if CONFIG_WARPED_MOTION
8608 aom_clear_system_state();
Yunqing Wang1bc82862017-06-28 15:49:48 -07008609#if WARPED_MOTION_SORT_SAMPLES
8610 mbmi->num_proj_ref[0] =
8611 findSamples(cm, xd, mi_row, mi_col, pts0, pts_inref0, pts_mv0);
8612 total_samples = mbmi->num_proj_ref[0];
8613#else
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008614 mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
Yunqing Wang1bc82862017-06-28 15:49:48 -07008615#endif // WARPED_MOTION_SORT_SAMPLES
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008616#if CONFIG_EXT_INTER
8617 best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0];
8618#endif // CONFIG_EXT_INTER
8619#endif // CONFIG_WARPED_MOTION
8620#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8621 rate2_nocoeff = rd_stats->rate;
8622 last_motion_mode_allowed = motion_mode_allowed(
Sarah Parker0eea89f2017-07-11 11:56:36 -07008623#if CONFIG_GLOBAL_MOTION
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008624 0, xd->global_motion,
Sarah Parker0eea89f2017-07-11 11:56:36 -07008625#endif // CONFIG_GLOBAL_MOTION
Yue Chen52c51732017-07-11 15:08:30 -07008626#if CONFIG_WARPED_MOTION
8627 xd,
8628#endif
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008629 mi);
8630 base_mbmi = *mbmi;
8631#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8632
8633#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8634 int64_t best_rd = INT64_MAX;
8635 for (motion_mode = SIMPLE_TRANSLATION;
8636 motion_mode <= last_motion_mode_allowed; motion_mode++) {
8637 int64_t tmp_rd = INT64_MAX;
8638 int tmp_rate;
8639 int64_t tmp_dist;
8640#if CONFIG_EXT_INTER
8641 int tmp_rate2 =
8642 motion_mode != SIMPLE_TRANSLATION ? rate2_bmc_nocoeff : rate2_nocoeff;
8643#else
8644 int tmp_rate2 = rate2_nocoeff;
8645#endif // CONFIG_EXT_INTER
8646
Wei-Ting Lin20885282017-08-28 17:18:18 -07008647#if CONFIG_NCOBMC_ADAPT_WEIGHT
8648 // We cannot estimate the rd cost for the motion mode NCOBMC_ADAPT_WEIGHT
8649 // right now since it requires mvs from all neighboring blocks. We will
8650 // check if this mode is beneficial after all the mv's in the current
8651 // superblock are selected.
8652 if (motion_mode == NCOBMC_ADAPT_WEIGHT) continue;
8653#endif
8654
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008655 *mbmi = base_mbmi;
8656 mbmi->motion_mode = motion_mode;
8657#if CONFIG_MOTION_VAR
8658 if (mbmi->motion_mode == OBMC_CAUSAL) {
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008659#if CONFIG_EXT_INTER
8660 *mbmi = *best_bmc_mbmi;
8661 mbmi->motion_mode = OBMC_CAUSAL;
8662#endif // CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07008663 if (!is_comp_pred &&
8664#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
8665 !is_inter_singleref_comp_mode(this_mode) &&
8666#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
8667 have_newmv_in_inter_mode(this_mode)) {
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008668 int tmp_rate_mv = 0;
8669
8670 single_motion_search(cpi, x, bsize, mi_row, mi_col,
8671#if CONFIG_EXT_INTER
Yaowu Xua6317222017-04-17 11:29:51 -07008672 0,
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008673#endif // CONFIG_EXT_INTER
8674 &tmp_rate_mv);
8675 mbmi->mv[0].as_int = x->best_mv.as_int;
8676 if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv,
8677 refs[0])) {
8678 tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1);
8679 }
8680#if CONFIG_EXT_INTER
8681 tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv;
8682#else
8683 tmp_rate2 = rate2_nocoeff - rate_mv + tmp_rate_mv;
8684#endif // CONFIG_EXT_INTER
8685#if CONFIG_DUAL_FILTER
8686 if (!has_subpel_mv_component(xd->mi[0], xd, 0))
8687 mbmi->interp_filter[0] = EIGHTTAP_REGULAR;
8688 if (!has_subpel_mv_component(xd->mi[0], xd, 1))
8689 mbmi->interp_filter[1] = EIGHTTAP_REGULAR;
8690#endif // CONFIG_DUAL_FILTER
Jingning Hanc44009c2017-05-06 11:36:49 -07008691 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, bsize);
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008692#if CONFIG_EXT_INTER
8693 } else {
Jingning Hanc44009c2017-05-06 11:36:49 -07008694 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, orig_dst, bsize);
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008695#endif // CONFIG_EXT_INTER
8696 }
8697 av1_build_obmc_inter_prediction(
8698 cm, xd, mi_row, mi_col, args->above_pred_buf, args->above_pred_stride,
8699 args->left_pred_buf, args->left_pred_stride);
8700 model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate,
8701 &tmp_dist, skip_txfm_sb, skip_sse_sb);
8702 }
8703#endif // CONFIG_MOTION_VAR
8704
8705#if CONFIG_WARPED_MOTION
8706 if (mbmi->motion_mode == WARPED_CAUSAL) {
Yunqing Wang1bc82862017-06-28 15:49:48 -07008707#if WARPED_MOTION_SORT_SAMPLES
8708 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
8709#endif // WARPED_MOTION_SORT_SAMPLES
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008710#if CONFIG_EXT_INTER
8711 *mbmi = *best_bmc_mbmi;
8712 mbmi->motion_mode = WARPED_CAUSAL;
8713#endif // CONFIG_EXT_INTER
8714 mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE;
8715#if CONFIG_DUAL_FILTER
Yue Chen05bbf9b2017-04-26 21:58:01 -07008716 for (int dir = 0; dir < 4; ++dir)
8717 mbmi->interp_filter[dir] = cm->interp_filter == SWITCHABLE
8718 ? EIGHTTAP_REGULAR
8719 : cm->interp_filter;
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008720#else
8721 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP_REGULAR
8722 : cm->interp_filter;
8723#endif // CONFIG_DUAL_FILTER
8724
Yunqing Wang1bc82862017-06-28 15:49:48 -07008725#if WARPED_MOTION_SORT_SAMPLES
8726 memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0));
8727 memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0));
8728 // Rank the samples by motion vector difference
8729 if (mbmi->num_proj_ref[0] > 1) {
8730 mbmi->num_proj_ref[0] = sortSamples(pts_mv0, &mbmi->mv[0].as_mv, pts,
8731 pts_inref, mbmi->num_proj_ref[0]);
8732#if CONFIG_EXT_INTER
8733 best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0];
8734#endif // CONFIG_EXT_INTER
8735 }
8736#endif // WARPED_MOTION_SORT_SAMPLES
8737
Yunqing Wang68f3ccd2017-05-23 14:43:54 -07008738 if (!find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize,
8739 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
8740 &mbmi->wm_params[0], mi_row, mi_col)) {
8741 // Refine MV for NEWMV mode
8742 if (!is_comp_pred && have_newmv_in_inter_mode(this_mode)) {
8743 int tmp_rate_mv = 0;
8744 const int_mv mv0 = mbmi->mv[0];
8745 WarpedMotionParams wm_params0 = mbmi->wm_params[0];
Yunqing Wang1bc82862017-06-28 15:49:48 -07008746#if WARPED_MOTION_SORT_SAMPLES
8747 int num_proj_ref0 = mbmi->num_proj_ref[0];
Yunqing Wang68f3ccd2017-05-23 14:43:54 -07008748
8749 // Refine MV in a small range.
Yunqing Wang1bc82862017-06-28 15:49:48 -07008750 av1_refine_warped_mv(cpi, x, bsize, mi_row, mi_col, pts0, pts_inref0,
8751 pts_mv0, total_samples);
8752#else
8753 // Refine MV in a small range.
Yunqing Wang68f3ccd2017-05-23 14:43:54 -07008754 av1_refine_warped_mv(cpi, x, bsize, mi_row, mi_col, pts, pts_inref);
Yunqing Wang1bc82862017-06-28 15:49:48 -07008755#endif // WARPED_MOTION_SORT_SAMPLES
Yunqing Wang68f3ccd2017-05-23 14:43:54 -07008756
8757 // Keep the refined MV and WM parameters.
8758 if (mv0.as_int != mbmi->mv[0].as_int) {
8759 const int ref = refs[0];
8760 const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
8761
8762 tmp_rate_mv =
8763 av1_mv_bit_cost(&mbmi->mv[0].as_mv, &ref_mv, x->nmvjointcost,
8764 x->mvcost, MV_COST_WEIGHT);
8765
8766 if (cpi->sf.adaptive_motion_search)
8767 x->pred_mv[ref] = mbmi->mv[0].as_mv;
8768
8769 single_newmv[ref] = mbmi->mv[0];
8770
8771 if (discount_newmv_test(cpi, this_mode, mbmi->mv[0], mode_mv,
8772 refs[0])) {
8773 tmp_rate_mv = AOMMAX((tmp_rate_mv / NEW_MV_DISCOUNT_FACTOR), 1);
8774 }
8775#if CONFIG_EXT_INTER
Yunqing Wang1bc82862017-06-28 15:49:48 -07008776#if WARPED_MOTION_SORT_SAMPLES
8777 best_bmc_mbmi->num_proj_ref[0] = mbmi->num_proj_ref[0];
8778#endif // WARPED_MOTION_SORT_SAMPLES
Yunqing Wang68f3ccd2017-05-23 14:43:54 -07008779 tmp_rate2 = rate2_bmc_nocoeff - rate_mv_bmc + tmp_rate_mv;
8780#else
8781 tmp_rate2 = rate2_nocoeff - rate_mv + tmp_rate_mv;
8782#endif // CONFIG_EXT_INTER
8783#if CONFIG_DUAL_FILTER
8784 if (!has_subpel_mv_component(xd->mi[0], xd, 0))
8785 mbmi->interp_filter[0] = EIGHTTAP_REGULAR;
8786 if (!has_subpel_mv_component(xd->mi[0], xd, 1))
8787 mbmi->interp_filter[1] = EIGHTTAP_REGULAR;
8788#endif // CONFIG_DUAL_FILTER
8789 } else {
8790 // Restore the old MV and WM parameters.
8791 mbmi->mv[0] = mv0;
8792 mbmi->wm_params[0] = wm_params0;
Yunqing Wang1bc82862017-06-28 15:49:48 -07008793#if WARPED_MOTION_SORT_SAMPLES
8794 mbmi->num_proj_ref[0] = num_proj_ref0;
8795#endif // WARPED_MOTION_SORT_SAMPLES
Yunqing Wang68f3ccd2017-05-23 14:43:54 -07008796 }
8797 }
8798
Jingning Hanc44009c2017-05-06 11:36:49 -07008799 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008800 model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate,
8801 &tmp_dist, skip_txfm_sb, skip_sse_sb);
8802 } else {
8803 continue;
8804 }
8805 }
8806#endif // CONFIG_WARPED_MOTION
8807 x->skip = 0;
8808
8809 rd_stats->dist = 0;
8810 rd_stats->sse = 0;
8811 rd_stats->skip = 1;
8812 rd_stats->rate = tmp_rate2;
8813 if (last_motion_mode_allowed > SIMPLE_TRANSLATION) {
8814#if CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR
8815 if (last_motion_mode_allowed == WARPED_CAUSAL)
8816#endif // CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR
Yue Chenbdc8dab2017-07-26 12:05:47 -07008817 rd_stats->rate += x->motion_mode_cost[bsize][mbmi->motion_mode];
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008818#if CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR
8819 else
Yue Chenb23d00a2017-07-28 17:01:21 -07008820 rd_stats->rate += x->motion_mode_cost1[bsize][mbmi->motion_mode];
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008821#endif // CONFIG_WARPED_MOTION && CONFIG_MOTION_VAR
8822 }
8823#if CONFIG_WARPED_MOTION
8824 if (mbmi->motion_mode == WARPED_CAUSAL) {
8825 rd_stats->rate -= rs;
8826 }
8827#endif // CONFIG_WARPED_MOTION
8828#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8829 if (!*skip_txfm_sb) {
8830 int64_t rdcosty = INT64_MAX;
8831 int is_cost_valid_uv = 0;
8832
8833 // cost and distortion
8834 av1_subtract_plane(x, bsize, 0);
8835#if CONFIG_VAR_TX
8836 if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) {
8837 select_tx_type_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd);
8838 } else {
8839 int idx, idy;
8840 super_block_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd);
8841 for (idy = 0; idy < xd->n8_h; ++idy)
8842 for (idx = 0; idx < xd->n8_w; ++idx)
8843 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
8844 memset(x->blk_skip[0], rd_stats_y->skip,
8845 sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4);
8846 }
8847#else
8848 /* clang-format off */
8849 super_block_yrd(cpi, x, rd_stats_y, bsize, ref_best_rd);
8850/* clang-format on */
8851#endif // CONFIG_VAR_TX
8852
8853 if (rd_stats_y->rate == INT_MAX) {
8854 av1_invalid_rd_stats(rd_stats);
8855#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8856 if (mbmi->motion_mode != SIMPLE_TRANSLATION) {
8857 continue;
8858 } else {
8859#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8860 restore_dst_buf(xd, *orig_dst);
8861 return INT64_MAX;
8862#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8863 }
8864#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8865 }
8866
8867 av1_merge_rd_stats(rd_stats, rd_stats_y);
8868
Urvang Joshi70006e42017-06-14 16:08:55 -07008869 rdcosty = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist);
8870 rdcosty = AOMMIN(rdcosty, RDCOST(x->rdmult, 0, rd_stats->sse));
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008871/* clang-format off */
8872#if CONFIG_VAR_TX
8873 is_cost_valid_uv =
8874 inter_block_uvrd(cpi, x, rd_stats_uv, bsize, ref_best_rd - rdcosty);
8875#else
8876 is_cost_valid_uv =
8877 super_block_uvrd(cpi, x, rd_stats_uv, bsize, ref_best_rd - rdcosty);
8878#endif // CONFIG_VAR_TX
8879 if (!is_cost_valid_uv) {
8880#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8881 continue;
8882#else
8883 restore_dst_buf(xd, *orig_dst);
8884 return INT64_MAX;
8885#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8886 }
8887 /* clang-format on */
8888 av1_merge_rd_stats(rd_stats, rd_stats_uv);
8889#if CONFIG_RD_DEBUG
8890 // record transform block coefficient cost
8891 // TODO(angiebird): So far rd_debug tool only detects discrepancy of
8892 // coefficient cost. Therefore, it is fine to copy rd_stats into mbmi
8893 // here because we already collect the coefficient cost. Move this part to
8894 // other place when we need to compare non-coefficient cost.
8895 mbmi->rd_stats = *rd_stats;
8896#endif // CONFIG_RD_DEBUG
8897#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8898 if (rd_stats->skip) {
8899 rd_stats->rate -= rd_stats_uv->rate + rd_stats_y->rate;
8900 rd_stats_y->rate = 0;
8901 rd_stats_uv->rate = 0;
8902 rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
8903 mbmi->skip = 0;
8904 // here mbmi->skip temporarily plays a role as what this_skip2 does
8905 } else if (!xd->lossless[mbmi->segment_id] &&
Urvang Joshi70006e42017-06-14 16:08:55 -07008906 (RDCOST(x->rdmult,
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008907 rd_stats_y->rate + rd_stats_uv->rate +
8908 av1_cost_bit(av1_get_skip_prob(cm, xd), 0),
8909 rd_stats->dist) >=
Urvang Joshi70006e42017-06-14 16:08:55 -07008910 RDCOST(x->rdmult, av1_cost_bit(av1_get_skip_prob(cm, xd), 1),
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008911 rd_stats->sse))) {
8912 rd_stats->rate -= rd_stats_uv->rate + rd_stats_y->rate;
8913 rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
8914 rd_stats->dist = rd_stats->sse;
8915 rd_stats_y->rate = 0;
8916 rd_stats_uv->rate = 0;
8917 mbmi->skip = 1;
8918 } else {
8919 rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
8920 mbmi->skip = 0;
8921 }
8922 *disable_skip = 0;
8923#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8924 } else {
8925 x->skip = 1;
8926 *disable_skip = 1;
8927 mbmi->tx_size = tx_size_from_tx_mode(bsize, cm->tx_mode, 1);
8928
8929// The cost of skip bit needs to be added.
8930#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8931 mbmi->skip = 0;
8932#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
8933 rd_stats->rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
8934
8935 rd_stats->dist = *skip_sse_sb;
8936 rd_stats->sse = *skip_sse_sb;
8937 rd_stats_y->rate = 0;
8938 rd_stats_uv->rate = 0;
8939 rd_stats->skip = 1;
8940 }
8941
8942#if CONFIG_GLOBAL_MOTION
8943 if (this_mode == ZEROMV
8944#if CONFIG_EXT_INTER
8945 || this_mode == ZERO_ZEROMV
8946#endif // CONFIG_EXT_INTER
8947 ) {
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008948 if (is_nontrans_global_motion(xd)) {
8949 rd_stats->rate -= rs;
8950#if CONFIG_DUAL_FILTER
8951 mbmi->interp_filter[0] = cm->interp_filter == SWITCHABLE
8952 ? EIGHTTAP_REGULAR
8953 : cm->interp_filter;
8954 mbmi->interp_filter[1] = cm->interp_filter == SWITCHABLE
8955 ? EIGHTTAP_REGULAR
8956 : cm->interp_filter;
8957#else
8958 mbmi->interp_filter = cm->interp_filter == SWITCHABLE
8959 ? EIGHTTAP_REGULAR
8960 : cm->interp_filter;
8961#endif // CONFIG_DUAL_FILTER
8962 }
8963 }
8964#endif // CONFIG_GLOBAL_MOTION
8965
8966#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Urvang Joshi70006e42017-06-14 16:08:55 -07008967 tmp_rd = RDCOST(x->rdmult, rd_stats->rate, rd_stats->dist);
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08008968 if (mbmi->motion_mode == SIMPLE_TRANSLATION || (tmp_rd < best_rd)) {
8969 best_mbmi = *mbmi;
8970 best_rd = tmp_rd;
8971 best_rd_stats = *rd_stats;
8972 best_rd_stats_y = *rd_stats_y;
8973 best_rd_stats_uv = *rd_stats_uv;
8974#if CONFIG_VAR_TX
8975 for (int i = 0; i < MAX_MB_PLANE; ++i)
8976 memcpy(best_blk_skip[i], x->blk_skip[i],
8977 sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4);
8978#endif // CONFIG_VAR_TX
8979 best_xskip = x->skip;
8980 best_disable_skip = *disable_skip;
8981 }
8982 }
8983
8984 if (best_rd == INT64_MAX) {
8985 av1_invalid_rd_stats(rd_stats);
8986 restore_dst_buf(xd, *orig_dst);
8987 return INT64_MAX;
8988 }
8989 *mbmi = best_mbmi;
8990 *rd_stats = best_rd_stats;
8991 *rd_stats_y = best_rd_stats_y;
8992 *rd_stats_uv = best_rd_stats_uv;
8993#if CONFIG_VAR_TX
8994 for (int i = 0; i < MAX_MB_PLANE; ++i)
8995 memcpy(x->blk_skip[i], best_blk_skip[i],
8996 sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4);
8997#endif // CONFIG_VAR_TX
8998 x->skip = best_xskip;
8999 *disable_skip = best_disable_skip;
9000#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
9001
9002 restore_dst_buf(xd, *orig_dst);
9003 return 0;
9004}
9005
Zoe Liu85b66462017-04-20 14:28:19 -07009006static int64_t handle_inter_mode(const AV1_COMP *const cpi, MACROBLOCK *x,
9007 BLOCK_SIZE bsize, RD_STATS *rd_stats,
9008 RD_STATS *rd_stats_y, RD_STATS *rd_stats_uv,
9009 int *disable_skip,
9010 int_mv (*mode_mv)[TOTAL_REFS_PER_FRAME],
9011#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9012 int_mv (*mode_comp_mv)[TOTAL_REFS_PER_FRAME],
9013#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9014 int mi_row, int mi_col,
9015 HandleInterModeArgs *args,
9016 const int64_t ref_best_rd) {
Urvang Joshi52648442016-10-13 17:27:51 -07009017 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009018 MACROBLOCKD *xd = &x->e_mbd;
Sarah Parker19234cc2017-03-10 16:43:25 -08009019 MODE_INFO *mi = xd->mi[0];
9020 MB_MODE_INFO *mbmi = &mi->mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009021 MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
9022 const int is_comp_pred = has_second_ref(mbmi);
9023 const int this_mode = mbmi->mode;
Zoe Liu85b66462017-04-20 14:28:19 -07009024#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9025 const int is_singleref_comp_mode = is_inter_singleref_comp_mode(this_mode);
9026#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07009027 int_mv *frame_mv = mode_mv[this_mode];
Zoe Liu85b66462017-04-20 14:28:19 -07009028#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9029 // The comp mv for the compound mode in single ref
9030 int_mv *frame_comp_mv = mode_comp_mv[this_mode];
9031#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07009032 int i;
9033 int refs[2] = { mbmi->ref_frame[0],
9034 (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) };
9035 int_mv cur_mv[2];
9036 int rate_mv = 0;
9037#if CONFIG_EXT_INTER
Angie Chiang75c22092016-10-25 12:19:16 -07009038 int pred_exists = 1;
Angie Chiang64a2fb82017-07-06 10:15:16 -07009039#if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT || CONFIG_INTERINTRA
Jingning Hanae5cfde2016-11-30 12:01:44 -08009040 const int bw = block_size_wide[bsize];
James Zern89a015b2017-08-08 12:39:00 -04009041#endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
Yaowu Xuc27fc142016-08-22 16:08:15 -07009042 int_mv single_newmv[TOTAL_REFS_PER_FRAME];
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009043#if CONFIG_INTERINTRA
Yue Chenb23d00a2017-07-28 17:01:21 -07009044 const int *const interintra_mode_cost =
9045 x->interintra_mode_cost[size_group_lookup[bsize]];
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009046#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07009047 const int is_comp_interintra_pred = (mbmi->ref_frame[1] == INTRA_FRAME);
Yaowu Xuf883b422016-08-30 14:01:10 -07009048 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Fergus Simpson073c6f32017-02-17 12:13:48 -08009049#else
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08009050 int_mv *const single_newmv = args->single_newmv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009051#endif // CONFIG_EXT_INTER
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02009052#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07009053 DECLARE_ALIGNED(16, uint8_t, tmp_buf_[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
9054#else
9055 DECLARE_ALIGNED(16, uint8_t, tmp_buf_[MAX_MB_PLANE * MAX_SB_SQUARE]);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02009056#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07009057 uint8_t *tmp_buf;
9058
Yue Chencb60b182016-10-13 15:18:22 -07009059#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07009060#if CONFIG_EXT_INTER
9061 int rate2_bmc_nocoeff;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009062 MB_MODE_INFO best_bmc_mbmi;
Yue Chen69f18e12016-09-08 14:48:15 -07009063 int rate_mv_bmc;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009064#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07009065#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Angie Chiang75c22092016-10-25 12:19:16 -07009066 int64_t rd = INT64_MAX;
David Barkerac37fa32016-12-02 12:30:21 +00009067 BUFFER_SET orig_dst, tmp_dst;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009068 int rs = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009069
9070 int skip_txfm_sb = 0;
9071 int64_t skip_sse_sb = INT64_MAX;
Yaowu Xub0d0d002016-11-22 09:26:43 -08009072 int16_t mode_ctx;
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07009073#if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_MOTION_VAR
9074 // dummy fillers
9075 mbmi->ncobmc_mode[0] = NO_OVERLAP;
9076 mbmi->ncobmc_mode[1] = NO_OVERLAP;
9077#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07009078
9079#if CONFIG_EXT_INTER
Yue Chen5e606542017-05-24 17:03:17 -07009080#if CONFIG_INTERINTRA
9081 int compmode_interintra_cost = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009082 mbmi->use_wedge_interintra = 0;
Yue Chen5e606542017-05-24 17:03:17 -07009083#endif
9084#if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
9085 int compmode_interinter_cost = 0;
Sarah Parker2d0e9b72017-05-04 01:34:16 +00009086 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Yue Chen5e606542017-05-24 17:03:17 -07009087#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07009088
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07009089#if CONFIG_INTERINTRA
9090 if (!cm->allow_interintra_compound && is_comp_interintra_pred)
9091 return INT64_MAX;
9092#endif // CONFIG_INTERINTRA
9093
Yaowu Xuc27fc142016-08-22 16:08:15 -07009094 // is_comp_interintra_pred implies !is_comp_pred
9095 assert(!is_comp_interintra_pred || (!is_comp_pred));
9096 // is_comp_interintra_pred implies is_interintra_allowed(mbmi->sb_type)
9097 assert(!is_comp_interintra_pred || is_interintra_allowed(mbmi));
9098#endif // CONFIG_EXT_INTER
9099
Yaowu Xuc27fc142016-08-22 16:08:15 -07009100#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07009101#if CONFIG_COMPOUND_SINGLEREF
9102 if (is_comp_pred || is_singleref_comp_mode)
9103#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07009104 if (is_comp_pred)
Zoe Liu85b66462017-04-20 14:28:19 -07009105#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07009106 mode_ctx = mbmi_ext->compound_mode_context[refs[0]];
9107 else
9108#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07009109 mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context,
9110 mbmi->ref_frame, bsize, -1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009111
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02009112#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07009113 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
9114 tmp_buf = CONVERT_TO_BYTEPTR(tmp_buf_);
9115 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02009116#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07009117 tmp_buf = tmp_buf_;
David Barkerb8069f92016-11-18 14:49:56 +00009118 // Make sure that we didn't leave the plane destination buffers set
9119 // to tmp_buf at the end of the last iteration
9120 assert(xd->plane[0].dst.buf != tmp_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009121
Yue Chen69f18e12016-09-08 14:48:15 -07009122#if CONFIG_WARPED_MOTION
9123 mbmi->num_proj_ref[0] = 0;
9124 mbmi->num_proj_ref[1] = 0;
9125#endif // CONFIG_WARPED_MOTION
9126
Yaowu Xuc27fc142016-08-22 16:08:15 -07009127 if (is_comp_pred) {
9128 if (frame_mv[refs[0]].as_int == INVALID_MV ||
9129 frame_mv[refs[1]].as_int == INVALID_MV)
9130 return INT64_MAX;
Zoe Liu85b66462017-04-20 14:28:19 -07009131#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9132 } else if (is_singleref_comp_mode) {
9133 if (frame_mv[refs[0]].as_int == INVALID_MV ||
9134 frame_comp_mv[refs[0]].as_int == INVALID_MV)
9135 return INT64_MAX;
9136#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07009137 }
9138
Yue Chene9638cc2016-10-10 12:37:54 -07009139 mbmi->motion_mode = SIMPLE_TRANSLATION;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009140 if (have_newmv_in_inter_mode(this_mode)) {
Zoe Liu85b66462017-04-20 14:28:19 -07009141 const int64_t ret_val =
9142 handle_newmv(cpi, x, bsize, mode_mv,
9143#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9144 mode_comp_mv,
9145#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9146 mi_row, mi_col, &rate_mv, single_newmv, args);
Fergus Simpson45509632017-02-22 15:30:50 -08009147 if (ret_val != 0)
9148 return ret_val;
9149 else
9150 rd_stats->rate += rate_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009151 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07009152 for (i = 0; i < is_comp_pred + 1; ++i) {
9153 cur_mv[i] = frame_mv[refs[i]];
Zoe Liued29ea22017-04-20 16:48:15 -07009154 // Clip "next_nearest" so that it does not extend to far out of image
9155 if (this_mode != NEWMV) clamp_mv2(&cur_mv[i].as_mv, xd);
Alex Converse0fa0f422017-04-24 12:51:14 -07009156 if (mv_check_bounds(&x->mv_limits, &cur_mv[i].as_mv)) return INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009157 mbmi->mv[i].as_int = cur_mv[i].as_int;
9158 }
9159
Zoe Liu85b66462017-04-20 14:28:19 -07009160#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9161 if (!is_comp_pred && is_singleref_comp_mode) {
9162 cur_mv[1] = frame_comp_mv[refs[0]];
9163 // Clip "next_nearest" so that it does not extend to far out of image
9164 if (this_mode != NEWMV) clamp_mv2(&cur_mv[1].as_mv, xd);
9165 if (mv_check_bounds(&x->mv_limits, &cur_mv[1].as_mv)) return INT64_MAX;
9166 mbmi->mv[1].as_int = cur_mv[1].as_int;
9167 }
9168#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9169
Yaowu Xuc27fc142016-08-22 16:08:15 -07009170#if CONFIG_EXT_INTER
Angie Chiang78a3bc12016-11-06 12:55:46 -08009171 if (this_mode == NEAREST_NEARESTMV)
Yaowu Xuc27fc142016-08-22 16:08:15 -07009172#else
Angie Chiang78a3bc12016-11-06 12:55:46 -08009173 if (this_mode == NEARESTMV && is_comp_pred)
Yaowu Xuc27fc142016-08-22 16:08:15 -07009174#endif // CONFIG_EXT_INTER
Angie Chiang78a3bc12016-11-06 12:55:46 -08009175 {
9176#if !CONFIG_EXT_INTER
9177 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Fergus Simpson4063a682017-02-28 16:52:22 -08009178#endif // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07009179 if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) {
9180 cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv;
9181 cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv;
9182
9183 for (i = 0; i < 2; ++i) {
9184 clamp_mv2(&cur_mv[i].as_mv, xd);
Alex Converse0fa0f422017-04-24 12:51:14 -07009185 if (mv_check_bounds(&x->mv_limits, &cur_mv[i].as_mv)) return INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009186 mbmi->mv[i].as_int = cur_mv[i].as_int;
9187 }
9188 }
9189 }
9190
9191#if CONFIG_EXT_INTER
9192 if (mbmi_ext->ref_mv_count[ref_frame_type] > 0) {
Zoe Liu85b66462017-04-20 14:28:19 -07009193#if CONFIG_COMPOUND_SINGLEREF
9194 if (this_mode == NEAREST_NEWMV || // this_mode == SR_NEAREST_NEWMV ||
Yushin Cho67dda512017-07-25 14:58:22 -07009195 this_mode == SR_NEAREST_NEARMV)
Zoe Liu85b66462017-04-20 14:28:19 -07009196#else // !CONFIG_COMPOUND_SINGLEREF
Yushin Cho67dda512017-07-25 14:58:22 -07009197 if (this_mode == NEAREST_NEWMV)
Zoe Liu85b66462017-04-20 14:28:19 -07009198#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho67dda512017-07-25 14:58:22 -07009199 {
Yaowu Xuc27fc142016-08-22 16:08:15 -07009200 cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv;
9201
RogerZhou3b635242017-09-19 10:06:46 -07009202#if CONFIG_AMVR
9203 lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv,
9204 cm->cur_frame_mv_precision_level);
9205#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07009206 lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv);
RogerZhou3b635242017-09-19 10:06:46 -07009207#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07009208 clamp_mv2(&cur_mv[0].as_mv, xd);
Alex Converse0fa0f422017-04-24 12:51:14 -07009209 if (mv_check_bounds(&x->mv_limits, &cur_mv[0].as_mv)) return INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009210 mbmi->mv[0].as_int = cur_mv[0].as_int;
9211 }
9212
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07009213 if (this_mode == NEW_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07009214 cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv;
9215
RogerZhou3b635242017-09-19 10:06:46 -07009216#if CONFIG_AMVR
9217 lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv,
9218 cm->cur_frame_mv_precision_level);
9219#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07009220 lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv);
RogerZhou3b635242017-09-19 10:06:46 -07009221#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07009222 clamp_mv2(&cur_mv[1].as_mv, xd);
Alex Converse0fa0f422017-04-24 12:51:14 -07009223 if (mv_check_bounds(&x->mv_limits, &cur_mv[1].as_mv)) return INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009224 mbmi->mv[1].as_int = cur_mv[1].as_int;
9225 }
9226 }
9227
9228 if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) {
David Barker404b2e82017-03-27 13:07:47 +01009229 int ref_mv_idx = mbmi->ref_mv_idx + 1;
Zoe Liu85b66462017-04-20 14:28:19 -07009230 if (this_mode == NEAR_NEWMV ||
9231#if CONFIG_COMPOUND_SINGLEREF
9232 this_mode == SR_NEAR_NEWMV ||
9233#endif // CONFIG_COMPOUND_SINGLEREF
9234 this_mode == NEAR_NEARMV) {
David Barker404b2e82017-03-27 13:07:47 +01009235 cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009236
RogerZhou3b635242017-09-19 10:06:46 -07009237#if CONFIG_AMVR
9238 lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv,
9239 cm->cur_frame_mv_precision_level);
9240#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07009241 lower_mv_precision(&cur_mv[0].as_mv, cm->allow_high_precision_mv);
RogerZhou3b635242017-09-19 10:06:46 -07009242#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07009243 clamp_mv2(&cur_mv[0].as_mv, xd);
Alex Converse0fa0f422017-04-24 12:51:14 -07009244 if (mv_check_bounds(&x->mv_limits, &cur_mv[0].as_mv)) return INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009245 mbmi->mv[0].as_int = cur_mv[0].as_int;
9246 }
9247
Zoe Liu85b66462017-04-20 14:28:19 -07009248 if (this_mode == NEW_NEARMV ||
9249#if CONFIG_COMPOUND_SINGLEREF
9250 this_mode == SR_NEAREST_NEARMV ||
9251#endif // CONFIG_COMPOUND_SINGLEREF
9252 this_mode == NEAR_NEARMV) {
9253#if CONFIG_COMPOUND_SINGLEREF
9254 if (this_mode == SR_NEAREST_NEARMV)
9255 cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
9256 else
9257#endif // CONFIG_COMPOUND_SINGLEREF
9258 cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009259
RogerZhou3b635242017-09-19 10:06:46 -07009260#if CONFIG_AMVR
9261 lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv,
9262 cm->cur_frame_mv_precision_level);
9263#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07009264 lower_mv_precision(&cur_mv[1].as_mv, cm->allow_high_precision_mv);
RogerZhou3b635242017-09-19 10:06:46 -07009265#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07009266 clamp_mv2(&cur_mv[1].as_mv, xd);
Alex Converse0fa0f422017-04-24 12:51:14 -07009267 if (mv_check_bounds(&x->mv_limits, &cur_mv[1].as_mv)) return INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009268 mbmi->mv[1].as_int = cur_mv[1].as_int;
9269 }
9270 }
Zoe Liu85b66462017-04-20 14:28:19 -07009271#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07009272 if (this_mode == NEARMV && is_comp_pred) {
Yaowu Xuf883b422016-08-30 14:01:10 -07009273 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009274 if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) {
9275 int ref_mv_idx = mbmi->ref_mv_idx + 1;
9276 cur_mv[0] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
9277 cur_mv[1] = mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
9278
9279 for (i = 0; i < 2; ++i) {
9280 clamp_mv2(&cur_mv[i].as_mv, xd);
Alex Converse0fa0f422017-04-24 12:51:14 -07009281 if (mv_check_bounds(&x->mv_limits, &cur_mv[i].as_mv)) return INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009282 mbmi->mv[i].as_int = cur_mv[i].as_int;
9283 }
9284 }
9285 }
9286#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07009287
9288 // do first prediction into the destination buffer. Do the next
9289 // prediction into a temporary buffer. Then keep track of which one
9290 // of these currently holds the best predictor, and use the other
9291 // one for future predictions. In the end, copy from tmp_buf to
9292 // dst if necessary.
9293 for (i = 0; i < MAX_MB_PLANE; i++) {
David Barkerac37fa32016-12-02 12:30:21 +00009294 tmp_dst.plane[i] = tmp_buf + i * MAX_SB_SQUARE;
9295 tmp_dst.stride[i] = MAX_SB_SIZE;
Angie Chiang75c22092016-10-25 12:19:16 -07009296 }
9297 for (i = 0; i < MAX_MB_PLANE; i++) {
David Barkerac37fa32016-12-02 12:30:21 +00009298 orig_dst.plane[i] = xd->plane[i].dst.buf;
9299 orig_dst.stride[i] = xd->plane[i].dst.stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009300 }
9301
9302 // We don't include the cost of the second reference here, because there
9303 // are only three options: Last/Golden, ARF/Last or Golden/ARF, or in other
9304 // words if you present them in that order, the second one is always known
9305 // if the first is known.
9306 //
9307 // Under some circumstances we discount the cost of new mv mode to encourage
9308 // initiation of a motion field.
9309 if (discount_newmv_test(cpi, this_mode, frame_mv[refs[0]], mode_mv,
9310 refs[0])) {
David Barkercb03dc32017-04-07 13:05:09 +01009311#if CONFIG_EXT_INTER
Yue Chenb23d00a2017-07-28 17:01:21 -07009312 rd_stats->rate += AOMMIN(
9313 cost_mv_ref(x, this_mode, mode_ctx),
9314 cost_mv_ref(x, is_comp_pred ? NEAREST_NEARESTMV : NEARESTMV, mode_ctx));
Yaowu Xuc27fc142016-08-22 16:08:15 -07009315#else
Yue Chenb23d00a2017-07-28 17:01:21 -07009316 rd_stats->rate += AOMMIN(cost_mv_ref(x, this_mode, mode_ctx),
9317 cost_mv_ref(x, NEARESTMV, mode_ctx));
Sebastien Alaiwane140c502017-04-27 09:52:34 +02009318#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07009319 } else {
Yue Chenb23d00a2017-07-28 17:01:21 -07009320 rd_stats->rate += cost_mv_ref(x, this_mode, mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009321 }
9322
Urvang Joshi70006e42017-06-14 16:08:55 -07009323 if (RDCOST(x->rdmult, rd_stats->rate, 0) > ref_best_rd &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07009324#if CONFIG_EXT_INTER
9325 mbmi->mode != NEARESTMV && mbmi->mode != NEAREST_NEARESTMV
9326#else
9327 mbmi->mode != NEARESTMV
9328#endif // CONFIG_EXT_INTER
9329 )
9330 return INT64_MAX;
9331
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08009332 int64_t ret_val = interpolation_filter_search(
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08009333 x, cpi, bsize, mi_row, mi_col, &tmp_dst, &orig_dst, args->single_filter,
9334 &rd, &rs, &skip_txfm_sb, &skip_sse_sb);
Fergus Simpsonde18e2b2017-03-01 20:12:34 -08009335 if (ret_val != 0) return ret_val;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009336
Yaowu Xuc27fc142016-08-22 16:08:15 -07009337#if CONFIG_EXT_INTER
Yue Chen69f18e12016-09-08 14:48:15 -07009338#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07009339 best_bmc_mbmi = *mbmi;
Angie Chiang76159122016-11-09 12:13:22 -08009340 rate2_bmc_nocoeff = rd_stats->rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009341 if (cm->interp_filter == SWITCHABLE) rate2_bmc_nocoeff += rs;
Yue Chen69f18e12016-09-08 14:48:15 -07009342 rate_mv_bmc = rate_mv;
Yue Chen69f18e12016-09-08 14:48:15 -07009343#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07009344
Yue Chen5e606542017-05-24 17:03:17 -07009345#if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
Zoe Liu85b66462017-04-20 14:28:19 -07009346#if CONFIG_COMPOUND_SINGLEREF
Yushin Cho67dda512017-07-25 14:58:22 -07009347 if (is_comp_pred || is_singleref_comp_mode)
Zoe Liu85b66462017-04-20 14:28:19 -07009348#else
Yushin Cho67dda512017-07-25 14:58:22 -07009349 if (is_comp_pred)
Zoe Liu85b66462017-04-20 14:28:19 -07009350#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho67dda512017-07-25 14:58:22 -07009351 {
Urvang Joshi368fbc92016-10-17 16:31:34 -07009352 int rate_sum, rs2;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009353 int64_t dist_sum;
Sarah Parker6fdc8532016-11-16 17:47:13 -08009354 int64_t best_rd_compound = INT64_MAX, best_rd_cur = INT64_MAX;
9355 INTERINTER_COMPOUND_DATA best_compound_data;
9356 int_mv best_mv[2];
9357 int best_tmp_rate_mv = rate_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009358 int tmp_skip_txfm_sb;
9359 int64_t tmp_skip_sse_sb;
Yaowu Xu5e8007f2017-06-28 12:39:18 -07009360 DECLARE_ALIGNED(16, uint8_t, pred0[2 * MAX_SB_SQUARE]);
9361 DECLARE_ALIGNED(16, uint8_t, pred1[2 * MAX_SB_SQUARE]);
Sarah Parker6fdc8532016-11-16 17:47:13 -08009362 uint8_t *preds0[1] = { pred0 };
9363 uint8_t *preds1[1] = { pred1 };
9364 int strides[1] = { bw };
Sarah Parker2e604882017-01-17 17:31:25 -08009365 int tmp_rate_mv;
Sarah Parker42d96102017-01-31 21:05:27 -08009366 int masked_compound_used = is_any_masked_compound_used(bsize);
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07009367#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
9368 masked_compound_used = masked_compound_used && cm->allow_masked_compound;
9369#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Sarah Parker6fdc8532016-11-16 17:47:13 -08009370 COMPOUND_TYPE cur_type;
Yue Chena4245512017-08-31 11:58:08 -07009371 int best_compmode_interinter_cost = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009372
Sarah Parker6fdc8532016-11-16 17:47:13 -08009373 best_mv[0].as_int = cur_mv[0].as_int;
9374 best_mv[1].as_int = cur_mv[1].as_int;
Sarah Parker2d0e9b72017-05-04 01:34:16 +00009375 memset(&best_compound_data, 0, sizeof(best_compound_data));
9376#if CONFIG_COMPOUND_SEGMENT
9377 uint8_t tmp_mask_buf[2 * MAX_SB_SQUARE];
9378 best_compound_data.seg_mask = tmp_mask_buf;
9379#endif // CONFIG_COMPOUND_SEGMENT
Yaowu Xuc27fc142016-08-22 16:08:15 -07009380
Zoe Liu85b66462017-04-20 14:28:19 -07009381#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9382 // TODO(zoeliu): To further check whether the following setups are needed.
9383 // Single ref compound mode: Prepare the 2nd ref frame predictor the same as
9384 // the 1st one.
9385 if (!is_comp_pred && is_singleref_comp_mode) {
9386 xd->block_refs[1] = xd->block_refs[0];
9387 for (i = 0; i < MAX_MB_PLANE; i++)
9388 xd->plane[i].pre[1] = xd->plane[i].pre[0];
9389 }
9390#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
9391
Sarah Parker42d96102017-01-31 21:05:27 -08009392 if (masked_compound_used) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08009393 // get inter predictors to use for masked compound modes
Yaowu Xuf883b422016-08-30 14:01:10 -07009394 av1_build_inter_predictors_for_planes_single_buf(
Yaowu Xuc27fc142016-08-22 16:08:15 -07009395 xd, bsize, 0, 0, mi_row, mi_col, 0, preds0, strides);
Yaowu Xuf883b422016-08-30 14:01:10 -07009396 av1_build_inter_predictors_for_planes_single_buf(
Yaowu Xuc27fc142016-08-22 16:08:15 -07009397 xd, bsize, 0, 0, mi_row, mi_col, 1, preds1, strides);
Sarah Parker6fdc8532016-11-16 17:47:13 -08009398 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07009399
Sarah Parker6fdc8532016-11-16 17:47:13 -08009400 for (cur_type = COMPOUND_AVERAGE; cur_type < COMPOUND_TYPES; cur_type++) {
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07009401 if (cur_type != COMPOUND_AVERAGE && !masked_compound_used) break;
Yue Chene2518252017-06-05 12:36:46 -07009402 if (!is_interinter_compound_used(cur_type, bsize)) continue;
Sarah Parker2e604882017-01-17 17:31:25 -08009403 tmp_rate_mv = rate_mv;
Sarah Parker6fdc8532016-11-16 17:47:13 -08009404 best_rd_cur = INT64_MAX;
Sarah Parker2d0e9b72017-05-04 01:34:16 +00009405 mbmi->interinter_compound_type = cur_type;
Sarah Parker680b9b12017-08-16 18:55:34 -07009406 int masked_type_cost = 0;
9407 if (masked_compound_used) {
9408#if CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
9409 if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize))
9410 masked_type_cost += av1_cost_literal(1);
9411 else
9412#endif // CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
9413 masked_type_cost +=
Yue Chena4245512017-08-31 11:58:08 -07009414 x->compound_type_cost[bsize][mbmi->interinter_compound_type];
Sarah Parker680b9b12017-08-16 18:55:34 -07009415 }
Sarah Parker6fdc8532016-11-16 17:47:13 -08009416 rs2 = av1_cost_literal(get_interinter_compound_type_bits(
Sarah Parker2d0e9b72017-05-04 01:34:16 +00009417 bsize, mbmi->interinter_compound_type)) +
Sarah Parker680b9b12017-08-16 18:55:34 -07009418 masked_type_cost;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009419
Sarah Parker6fdc8532016-11-16 17:47:13 -08009420 switch (cur_type) {
9421 case COMPOUND_AVERAGE:
Jingning Hanc44009c2017-05-06 11:36:49 -07009422 av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst,
9423 bsize);
Sarah Parker6fdc8532016-11-16 17:47:13 -08009424 av1_subtract_plane(x, bsize, 0);
9425 rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum,
9426 &tmp_skip_txfm_sb, &tmp_skip_sse_sb,
9427 INT64_MAX);
9428 if (rd != INT64_MAX)
Urvang Joshi70006e42017-06-14 16:08:55 -07009429 best_rd_cur = RDCOST(x->rdmult, rs2 + rate_mv + rate_sum, dist_sum);
Sarah Parker2e604882017-01-17 17:31:25 -08009430 best_rd_compound = best_rd_cur;
Sarah Parker6fdc8532016-11-16 17:47:13 -08009431 break;
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009432#if CONFIG_WEDGE
Sarah Parker6fdc8532016-11-16 17:47:13 -08009433 case COMPOUND_WEDGE:
Sarah Parker6fdc8532016-11-16 17:47:13 -08009434 if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh &&
9435 best_rd_compound / 3 < ref_best_rd) {
Sarah Parkerddcea392017-04-25 15:57:22 -07009436 best_rd_cur = build_and_cost_compound_type(
David Barkerac37fa32016-12-02 12:30:21 +00009437 cpi, x, cur_mv, bsize, this_mode, rs2, rate_mv, &orig_dst,
9438 &tmp_rate_mv, preds0, preds1, strides, mi_row, mi_col);
Sarah Parker6fdc8532016-11-16 17:47:13 -08009439 }
9440 break;
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009441#endif // CONFIG_WEDGE
Sarah Parker2f6ce752016-12-08 15:26:46 -08009442#if CONFIG_COMPOUND_SEGMENT
Sarah Parker569edda2016-12-14 14:57:38 -08009443 case COMPOUND_SEG:
Sarah Parker569edda2016-12-14 14:57:38 -08009444 if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh &&
9445 best_rd_compound / 3 < ref_best_rd) {
Sarah Parkerddcea392017-04-25 15:57:22 -07009446 best_rd_cur = build_and_cost_compound_type(
Sarah Parker569edda2016-12-14 14:57:38 -08009447 cpi, x, cur_mv, bsize, this_mode, rs2, rate_mv, &orig_dst,
9448 &tmp_rate_mv, preds0, preds1, strides, mi_row, mi_col);
Sarah Parker569edda2016-12-14 14:57:38 -08009449 }
9450 break;
Sarah Parker2f6ce752016-12-08 15:26:46 -08009451#endif // CONFIG_COMPOUND_SEGMENT
Sarah Parker6fdc8532016-11-16 17:47:13 -08009452 default: assert(0); return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009453 }
Sarah Parker2e604882017-01-17 17:31:25 -08009454
9455 if (best_rd_cur < best_rd_compound) {
9456 best_rd_compound = best_rd_cur;
Sarah Parker2d0e9b72017-05-04 01:34:16 +00009457#if CONFIG_WEDGE
9458 best_compound_data.wedge_index = mbmi->wedge_index;
9459 best_compound_data.wedge_sign = mbmi->wedge_sign;
9460#endif // CONFIG_WEDGE
9461#if CONFIG_COMPOUND_SEGMENT
9462 best_compound_data.mask_type = mbmi->mask_type;
9463 memcpy(best_compound_data.seg_mask, xd->seg_mask,
9464 2 * MAX_SB_SQUARE * sizeof(uint8_t));
9465#endif // CONFIG_COMPOUND_SEGMENT
9466 best_compound_data.interinter_compound_type =
9467 mbmi->interinter_compound_type;
Yue Chena4245512017-08-31 11:58:08 -07009468 best_compmode_interinter_cost = rs2;
Sarah Parker2e604882017-01-17 17:31:25 -08009469 if (have_newmv_in_inter_mode(this_mode)) {
9470 if (use_masked_motion_search(cur_type)) {
9471 best_tmp_rate_mv = tmp_rate_mv;
9472 best_mv[0].as_int = mbmi->mv[0].as_int;
9473 best_mv[1].as_int = mbmi->mv[1].as_int;
9474 } else {
9475 best_mv[0].as_int = cur_mv[0].as_int;
9476 best_mv[1].as_int = cur_mv[1].as_int;
9477 }
9478 }
9479 }
9480 // reset to original mvs for next iteration
9481 mbmi->mv[0].as_int = cur_mv[0].as_int;
9482 mbmi->mv[1].as_int = cur_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009483 }
Sarah Parker2d0e9b72017-05-04 01:34:16 +00009484#if CONFIG_WEDGE
9485 mbmi->wedge_index = best_compound_data.wedge_index;
9486 mbmi->wedge_sign = best_compound_data.wedge_sign;
9487#endif // CONFIG_WEDGE
9488#if CONFIG_COMPOUND_SEGMENT
9489 mbmi->mask_type = best_compound_data.mask_type;
9490 memcpy(xd->seg_mask, best_compound_data.seg_mask,
9491 2 * MAX_SB_SQUARE * sizeof(uint8_t));
9492#endif // CONFIG_COMPOUND_SEGMENT
9493 mbmi->interinter_compound_type =
9494 best_compound_data.interinter_compound_type;
Sarah Parker6fdc8532016-11-16 17:47:13 -08009495 if (have_newmv_in_inter_mode(this_mode)) {
9496 mbmi->mv[0].as_int = best_mv[0].as_int;
9497 mbmi->mv[1].as_int = best_mv[1].as_int;
9498 xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
9499 xd->mi[0]->bmi[0].as_mv[1].as_int = mbmi->mv[1].as_int;
Sarah Parker2d0e9b72017-05-04 01:34:16 +00009500 if (use_masked_motion_search(mbmi->interinter_compound_type)) {
Sarah Parker6fdc8532016-11-16 17:47:13 -08009501 rd_stats->rate += best_tmp_rate_mv - rate_mv;
9502 rate_mv = best_tmp_rate_mv;
9503 }
9504 }
9505
9506 if (ref_best_rd < INT64_MAX && best_rd_compound / 3 > ref_best_rd) {
David Barkerac37fa32016-12-02 12:30:21 +00009507 restore_dst_buf(xd, orig_dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009508 return INT64_MAX;
David Barkerb8069f92016-11-18 14:49:56 +00009509 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07009510
9511 pred_exists = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009512
Yue Chena4245512017-08-31 11:58:08 -07009513 compmode_interinter_cost = best_compmode_interinter_cost;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009514 }
Yue Chen5e606542017-05-24 17:03:17 -07009515#endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
Yaowu Xuc27fc142016-08-22 16:08:15 -07009516
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009517#if CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07009518 if (is_comp_interintra_pred) {
9519 INTERINTRA_MODE best_interintra_mode = II_DC_PRED;
9520 int64_t best_interintra_rd = INT64_MAX;
9521 int rmode, rate_sum;
9522 int64_t dist_sum;
9523 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009524 int tmp_rate_mv = 0;
9525 int tmp_skip_txfm_sb;
9526 int64_t tmp_skip_sse_sb;
9527 DECLARE_ALIGNED(16, uint8_t, intrapred_[2 * MAX_SB_SQUARE]);
9528 uint8_t *intrapred;
9529
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02009530#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07009531 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
9532 intrapred = CONVERT_TO_BYTEPTR(intrapred_);
9533 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02009534#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07009535 intrapred = intrapred_;
9536
Emil Keyder01770b32017-01-20 18:03:11 -05009537 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009538 for (j = 0; j < MAX_MB_PLANE; j++) {
9539 xd->plane[j].dst.buf = tmp_buf + j * MAX_SB_SQUARE;
9540 xd->plane[j].dst.stride = bw;
9541 }
Debargha Mukherjeead8be032017-05-09 15:28:45 -07009542 av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst, bsize);
David Barkerac37fa32016-12-02 12:30:21 +00009543 restore_dst_buf(xd, orig_dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009544 mbmi->ref_frame[1] = INTRA_FRAME;
9545 mbmi->use_wedge_interintra = 0;
9546
9547 for (j = 0; j < INTERINTRA_MODES; ++j) {
9548 mbmi->interintra_mode = (INTERINTRA_MODE)j;
9549 rmode = interintra_mode_cost[mbmi->interintra_mode];
David Barker761b1ac2017-09-25 11:23:03 +01009550 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, &orig_dst,
David Barkerac37fa32016-12-02 12:30:21 +00009551 intrapred, bw);
Yaowu Xuf883b422016-08-30 14:01:10 -07009552 av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009553 model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum,
9554 &tmp_skip_txfm_sb, &tmp_skip_sse_sb);
Urvang Joshi70006e42017-06-14 16:08:55 -07009555 rd = RDCOST(x->rdmult, tmp_rate_mv + rate_sum + rmode, dist_sum);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009556 if (rd < best_interintra_rd) {
9557 best_interintra_rd = rd;
9558 best_interintra_mode = mbmi->interintra_mode;
9559 }
9560 }
9561 mbmi->interintra_mode = best_interintra_mode;
9562 rmode = interintra_mode_cost[mbmi->interintra_mode];
David Barker761b1ac2017-09-25 11:23:03 +01009563 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, &orig_dst,
David Barkerac37fa32016-12-02 12:30:21 +00009564 intrapred, bw);
Yaowu Xuf883b422016-08-30 14:01:10 -07009565 av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw);
9566 av1_subtract_plane(x, bsize, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009567 rd = estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum,
9568 &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX);
9569 if (rd != INT64_MAX)
Urvang Joshi70006e42017-06-14 16:08:55 -07009570 rd = RDCOST(x->rdmult, rate_mv + rmode + rate_sum, dist_sum);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009571 best_interintra_rd = rd;
9572
9573 if (ref_best_rd < INT64_MAX && best_interintra_rd > 2 * ref_best_rd) {
David Barkerb8069f92016-11-18 14:49:56 +00009574 // Don't need to call restore_dst_buf here
Yaowu Xuc27fc142016-08-22 16:08:15 -07009575 return INT64_MAX;
9576 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009577#if CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07009578 if (is_interintra_wedge_used(bsize)) {
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009579 int64_t best_interintra_rd_nowedge = INT64_MAX;
9580 int64_t best_interintra_rd_wedge = INT64_MAX;
9581 int_mv tmp_mv;
9582 int rwedge = av1_cost_bit(cm->fc->wedge_interintra_prob[bsize], 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009583 if (rd != INT64_MAX)
Urvang Joshi70006e42017-06-14 16:08:55 -07009584 rd = RDCOST(x->rdmult, rmode + rate_mv + rwedge + rate_sum, dist_sum);
Yue Chenf03907a2017-05-31 12:04:04 -07009585 best_interintra_rd_nowedge = best_interintra_rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009586
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08009587 // Disable wedge search if source variance is small
Yaowu Xuc27fc142016-08-22 16:08:15 -07009588 if (x->source_variance > cpi->sf.disable_wedge_search_var_thresh) {
9589 mbmi->use_wedge_interintra = 1;
9590
Yaowu Xuf883b422016-08-30 14:01:10 -07009591 rwedge = av1_cost_literal(get_interintra_wedge_bits(bsize)) +
9592 av1_cost_bit(cm->fc->wedge_interintra_prob[bsize], 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009593
9594 best_interintra_rd_wedge =
9595 pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_);
9596
9597 best_interintra_rd_wedge +=
Urvang Joshi70006e42017-06-14 16:08:55 -07009598 RDCOST(x->rdmult, rmode + rate_mv + rwedge, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009599 // Refine motion vector.
9600 if (have_newmv_in_inter_mode(this_mode)) {
9601 // get negative of mask
Yaowu Xuf883b422016-08-30 14:01:10 -07009602 const uint8_t *mask = av1_get_contiguous_soft_mask(
Yaowu Xuc27fc142016-08-22 16:08:15 -07009603 mbmi->interintra_wedge_index, 1, bsize);
David Barkerf19f35f2017-05-22 16:33:22 +01009604 tmp_mv.as_int = x->mbmi_ext->ref_mvs[refs[0]][0].as_int;
9605 compound_single_motion_search(cpi, x, bsize, &tmp_mv.as_mv, mi_row,
9606 mi_col, intrapred, mask, bw,
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -07009607 &tmp_rate_mv, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009608 mbmi->mv[0].as_int = tmp_mv.as_int;
Debargha Mukherjeead8be032017-05-09 15:28:45 -07009609 av1_build_inter_predictors_sby(cm, xd, mi_row, mi_col, &orig_dst,
9610 bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009611 model_rd_for_sb(cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum,
9612 &tmp_skip_txfm_sb, &tmp_skip_sse_sb);
Urvang Joshi70006e42017-06-14 16:08:55 -07009613 rd = RDCOST(x->rdmult, rmode + tmp_rate_mv + rwedge + rate_sum,
9614 dist_sum);
Yue Chenf03907a2017-05-31 12:04:04 -07009615 if (rd >= best_interintra_rd_wedge) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07009616 tmp_mv.as_int = cur_mv[0].as_int;
9617 tmp_rate_mv = rate_mv;
9618 }
9619 } else {
9620 tmp_mv.as_int = cur_mv[0].as_int;
9621 tmp_rate_mv = rate_mv;
Yaowu Xuf883b422016-08-30 14:01:10 -07009622 av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009623 }
9624 // Evaluate closer to true rd
Yaowu Xuf883b422016-08-30 14:01:10 -07009625 av1_subtract_plane(x, bsize, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009626 rd =
9627 estimate_yrd_for_sb(cpi, bsize, x, &rate_sum, &dist_sum,
9628 &tmp_skip_txfm_sb, &tmp_skip_sse_sb, INT64_MAX);
9629 if (rd != INT64_MAX)
Urvang Joshi70006e42017-06-14 16:08:55 -07009630 rd = RDCOST(x->rdmult, rmode + tmp_rate_mv + rwedge + rate_sum,
9631 dist_sum);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009632 best_interintra_rd_wedge = rd;
9633 if (best_interintra_rd_wedge < best_interintra_rd_nowedge) {
9634 mbmi->use_wedge_interintra = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009635 mbmi->mv[0].as_int = tmp_mv.as_int;
Angie Chiang76159122016-11-09 12:13:22 -08009636 rd_stats->rate += tmp_rate_mv - rate_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009637 rate_mv = tmp_rate_mv;
9638 } else {
9639 mbmi->use_wedge_interintra = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009640 mbmi->mv[0].as_int = cur_mv[0].as_int;
9641 }
9642 } else {
9643 mbmi->use_wedge_interintra = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009644 }
9645 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009646#endif // CONFIG_WEDGE
Yaowu Xuc27fc142016-08-22 16:08:15 -07009647
9648 pred_exists = 0;
Yue Chen5e606542017-05-24 17:03:17 -07009649 compmode_interintra_cost =
9650 av1_cost_bit(cm->fc->interintra_prob[size_group_lookup[bsize]], 1) +
Fergus Simpson073c6f32017-02-17 12:13:48 -08009651 interintra_mode_cost[mbmi->interintra_mode];
Yaowu Xuc27fc142016-08-22 16:08:15 -07009652 if (is_interintra_wedge_used(bsize)) {
Yue Chen5e606542017-05-24 17:03:17 -07009653 compmode_interintra_cost += av1_cost_bit(
Yaowu Xuc27fc142016-08-22 16:08:15 -07009654 cm->fc->wedge_interintra_prob[bsize], mbmi->use_wedge_interintra);
9655 if (mbmi->use_wedge_interintra) {
Yue Chen5e606542017-05-24 17:03:17 -07009656 compmode_interintra_cost +=
Yaowu Xuf883b422016-08-30 14:01:10 -07009657 av1_cost_literal(get_interintra_wedge_bits(bsize));
Yaowu Xuc27fc142016-08-22 16:08:15 -07009658 }
9659 }
9660 } else if (is_interintra_allowed(mbmi)) {
Yue Chen5e606542017-05-24 17:03:17 -07009661 compmode_interintra_cost =
Yaowu Xuf883b422016-08-30 14:01:10 -07009662 av1_cost_bit(cm->fc->interintra_prob[size_group_lookup[bsize]], 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009663 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00009664#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07009665
Angie Chiang75c22092016-10-25 12:19:16 -07009666 if (pred_exists == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07009667 int tmp_rate;
9668 int64_t tmp_dist;
Jingning Hanc44009c2017-05-06 11:36:49 -07009669 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, &orig_dst, bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009670 model_rd_for_sb(cpi, bsize, x, xd, 0, MAX_MB_PLANE - 1, &tmp_rate,
9671 &tmp_dist, &skip_txfm_sb, &skip_sse_sb);
Urvang Joshi70006e42017-06-14 16:08:55 -07009672 rd = RDCOST(x->rdmult, rs + tmp_rate, tmp_dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009673 }
Angie Chiang75c22092016-10-25 12:19:16 -07009674#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07009675
Fergus Simpson3424c2d2017-03-09 11:48:15 -08009676 if (!is_comp_pred)
Yaowu Xuc27fc142016-08-22 16:08:15 -07009677#if CONFIG_DUAL_FILTER
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08009678 args->single_filter[this_mode][refs[0]] = mbmi->interp_filter[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07009679#else
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08009680 args->single_filter[this_mode][refs[0]] = mbmi->interp_filter;
Fergus Simpson4063a682017-02-28 16:52:22 -08009681#endif // CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07009682
9683#if CONFIG_EXT_INTER
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08009684 if (args->modelled_rd != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07009685 if (is_comp_pred) {
9686 const int mode0 = compound_ref0_mode(this_mode);
9687 const int mode1 = compound_ref1_mode(this_mode);
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08009688 const int64_t mrd = AOMMIN(args->modelled_rd[mode0][refs[0]],
9689 args->modelled_rd[mode1][refs[1]]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009690 if (rd / 4 * 3 > mrd && ref_best_rd < INT64_MAX) {
David Barkerac37fa32016-12-02 12:30:21 +00009691 restore_dst_buf(xd, orig_dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009692 return INT64_MAX;
9693 }
9694 } else if (!is_comp_interintra_pred) {
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -08009695 args->modelled_rd[this_mode][refs[0]] = rd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009696 }
9697 }
9698#endif // CONFIG_EXT_INTER
9699
9700 if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) {
9701 // if current pred_error modeled rd is substantially more than the best
9702 // so far, do not bother doing full rd
9703 if (rd / 2 > ref_best_rd) {
David Barkerac37fa32016-12-02 12:30:21 +00009704 restore_dst_buf(xd, orig_dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009705 return INT64_MAX;
9706 }
9707 }
9708
Yue Chen5e606542017-05-24 17:03:17 -07009709#if CONFIG_EXT_INTER
9710#if CONFIG_INTERINTRA
9711 rd_stats->rate += compmode_interintra_cost;
9712#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
9713 rate2_bmc_nocoeff += compmode_interintra_cost;
9714#endif
9715#endif
9716#if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
9717 rd_stats->rate += compmode_interinter_cost;
9718#endif
9719#endif
9720
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08009721 ret_val = motion_mode_rd(cpi, x, bsize, rd_stats, rd_stats_y, rd_stats_uv,
9722 disable_skip, mode_mv, mi_row, mi_col, args,
9723 ref_best_rd, refs, rate_mv,
Yue Chen69f18e12016-09-08 14:48:15 -07009724#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yunqing Wang68f3ccd2017-05-23 14:43:54 -07009725 single_newmv,
Yaowu Xuc27fc142016-08-22 16:08:15 -07009726#if CONFIG_EXT_INTER
Yunqing Wang562a3932017-06-20 12:20:45 -07009727 rate2_bmc_nocoeff, &best_bmc_mbmi, rate_mv_bmc,
Yue Chen69f18e12016-09-08 14:48:15 -07009728#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07009729#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Fergus Simpson10fb9fb2017-03-09 16:48:02 -08009730 rs, &skip_txfm_sb, &skip_sse_sb, &orig_dst);
9731 if (ret_val != 0) return ret_val;
Angie Chiang76159122016-11-09 12:13:22 -08009732
Yaowu Xuc27fc142016-08-22 16:08:15 -07009733 return 0; // The rate-distortion cost will be re-calculated by caller.
9734}
9735
Alex Converse28744302017-04-13 14:46:22 -07009736#if CONFIG_INTRABC
Alex Converse28744302017-04-13 14:46:22 -07009737static int64_t rd_pick_intrabc_mode_sb(const AV1_COMP *cpi, MACROBLOCK *x,
Angie Chiang2a2a7dd2017-04-25 16:08:47 -07009738 RD_STATS *rd_cost, BLOCK_SIZE bsize,
Alex Converse28744302017-04-13 14:46:22 -07009739 int64_t best_rd) {
Alex Converse3d8adf32017-04-24 12:35:42 -07009740 const AV1_COMMON *const cm = &cpi->common;
9741 if (bsize < BLOCK_8X8 || !cm->allow_screen_content_tools) return INT64_MAX;
9742
Alex Converse28744302017-04-13 14:46:22 -07009743 MACROBLOCKD *const xd = &x->e_mbd;
Alex Converse3d8adf32017-04-24 12:35:42 -07009744 const TileInfo *tile = &xd->tile;
9745 MODE_INFO *const mi = xd->mi[0];
9746 const int mi_row = -xd->mb_to_top_edge / (8 * MI_SIZE);
9747 const int mi_col = -xd->mb_to_left_edge / (8 * MI_SIZE);
9748 const int w = block_size_wide[bsize];
9749 const int h = block_size_high[bsize];
9750 const int sb_row = mi_row / MAX_MIB_SIZE;
Alex Converse861d7072017-05-15 14:19:53 -07009751 const int sb_col = mi_col / MAX_MIB_SIZE;
Alex Converse3d8adf32017-04-24 12:35:42 -07009752
Alex Converse44c2bad2017-05-11 09:36:10 -07009753 MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
9754 MV_REFERENCE_FRAME ref_frame = INTRA_FRAME;
9755 int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
9756 av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame],
9757 mbmi_ext->ref_mv_stack[ref_frame],
9758#if CONFIG_EXT_INTER
9759 mbmi_ext->compound_mode_context,
9760#endif // CONFIG_EXT_INTER
9761 candidates, mi_row, mi_col, NULL, NULL,
9762 mbmi_ext->mode_context);
9763
9764 int_mv nearestmv, nearmv;
9765 av1_find_best_ref_mvs(0, candidates, &nearestmv, &nearmv);
9766
9767 int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
9768 if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
9769 mbmi_ext->ref_mvs[INTRA_FRAME][0] = dv_ref;
Alex Converse3d8adf32017-04-24 12:35:42 -07009770
Alex Converse3d8adf32017-04-24 12:35:42 -07009771 struct buf_2d yv12_mb[MAX_MB_PLANE];
9772 av1_setup_pred_block(xd, yv12_mb, xd->cur_buf, mi_row, mi_col, NULL, NULL);
9773 for (int i = 0; i < MAX_MB_PLANE; ++i) {
9774 xd->plane[i].pre[0] = yv12_mb[i];
9775 }
9776
Alex Converse861d7072017-05-15 14:19:53 -07009777 enum IntrabcMotionDirection {
9778 IBC_MOTION_ABOVE,
9779 IBC_MOTION_LEFT,
9780 IBC_MOTION_DIRECTIONS
9781 };
Alex Converse3d8adf32017-04-24 12:35:42 -07009782
Alex Converse3d8adf32017-04-24 12:35:42 -07009783 MB_MODE_INFO *mbmi = &mi->mbmi;
9784 MB_MODE_INFO best_mbmi = *mbmi;
9785 RD_STATS best_rdcost = *rd_cost;
9786 int best_skip = x->skip;
Alex Converse861d7072017-05-15 14:19:53 -07009787
9788 for (enum IntrabcMotionDirection dir = IBC_MOTION_ABOVE;
9789 dir < IBC_MOTION_DIRECTIONS; ++dir) {
9790 const MvLimits tmp_mv_limits = x->mv_limits;
9791 switch (dir) {
9792 case IBC_MOTION_ABOVE:
9793 x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE;
9794 x->mv_limits.col_max = (tile->mi_col_end - mi_col) * MI_SIZE - w;
9795 x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE;
9796 x->mv_limits.row_max = (sb_row * MAX_MIB_SIZE - mi_row) * MI_SIZE - h;
9797 break;
9798 case IBC_MOTION_LEFT:
9799 x->mv_limits.col_min = (tile->mi_col_start - mi_col) * MI_SIZE;
9800 x->mv_limits.col_max = (sb_col * MAX_MIB_SIZE - mi_col) * MI_SIZE - w;
9801 // TODO(aconverse@google.com): Minimize the overlap between above and
9802 // left areas.
9803 x->mv_limits.row_min = (tile->mi_row_start - mi_row) * MI_SIZE;
9804 int bottom_coded_mi_edge =
9805 AOMMIN((sb_row + 1) * MAX_MIB_SIZE, tile->mi_row_end);
9806 x->mv_limits.row_max = (bottom_coded_mi_edge - mi_row) * MI_SIZE - h;
9807 break;
9808 default: assert(0);
9809 }
9810 assert(x->mv_limits.col_min >= tmp_mv_limits.col_min);
9811 assert(x->mv_limits.col_max <= tmp_mv_limits.col_max);
9812 assert(x->mv_limits.row_min >= tmp_mv_limits.row_min);
9813 assert(x->mv_limits.row_max <= tmp_mv_limits.row_max);
9814 av1_set_mv_search_range(&x->mv_limits, &dv_ref.as_mv);
9815
9816 if (x->mv_limits.col_max < x->mv_limits.col_min ||
9817 x->mv_limits.row_max < x->mv_limits.row_min) {
9818 x->mv_limits = tmp_mv_limits;
9819 continue;
9820 }
9821
9822 int step_param = cpi->mv_step_param;
9823 MV mvp_full = dv_ref.as_mv;
9824 mvp_full.col >>= 3;
9825 mvp_full.row >>= 3;
9826 int sadpb = x->sadperbit16;
9827 int cost_list[5];
RogerZhoucc5d35d2017-08-07 22:20:15 -07009828#if CONFIG_HASH_ME
RogerZhoud15e7c12017-09-26 08:49:28 -07009829 int bestsme = av1_full_pixel_search(
9830 cpi, x, bsize, &mvp_full, step_param, sadpb,
9831 cond_cost_list(cpi, cost_list), &dv_ref.as_mv, INT_MAX, 1,
9832 (MI_SIZE * mi_col), (MI_SIZE * mi_row), 1);
RogerZhoucc5d35d2017-08-07 22:20:15 -07009833#else
Alex Converse861d7072017-05-15 14:19:53 -07009834 int bestsme = av1_full_pixel_search(cpi, x, bsize, &mvp_full, step_param,
9835 sadpb, cond_cost_list(cpi, cost_list),
9836 &dv_ref.as_mv, INT_MAX, 1);
RogerZhoucc5d35d2017-08-07 22:20:15 -07009837#endif
Alex Converse861d7072017-05-15 14:19:53 -07009838
9839 x->mv_limits = tmp_mv_limits;
9840 if (bestsme == INT_MAX) continue;
9841 mvp_full = x->best_mv.as_mv;
9842 MV dv = {.row = mvp_full.row * 8, .col = mvp_full.col * 8 };
9843 if (mv_check_bounds(&x->mv_limits, &dv)) continue;
9844 if (!is_dv_valid(dv, tile, mi_row, mi_col, bsize)) continue;
9845
Alex Converse861d7072017-05-15 14:19:53 -07009846 memset(&mbmi->palette_mode_info, 0, sizeof(mbmi->palette_mode_info));
Alex Converse861d7072017-05-15 14:19:53 -07009847 mbmi->use_intrabc = 1;
9848 mbmi->mode = DC_PRED;
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04009849 mbmi->uv_mode = UV_DC_PRED;
Alex Converse861d7072017-05-15 14:19:53 -07009850 mbmi->mv[0].as_mv = dv;
Alex Converse3d8adf32017-04-24 12:35:42 -07009851#if CONFIG_DUAL_FILTER
Alex Converse861d7072017-05-15 14:19:53 -07009852 for (int idx = 0; idx < 4; ++idx) mbmi->interp_filter[idx] = BILINEAR;
Alex Converse3d8adf32017-04-24 12:35:42 -07009853#else
Alex Converse861d7072017-05-15 14:19:53 -07009854 mbmi->interp_filter = BILINEAR;
Alex Converse3d8adf32017-04-24 12:35:42 -07009855#endif
Alex Converse861d7072017-05-15 14:19:53 -07009856 mbmi->skip = 0;
9857 x->skip = 0;
9858 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
Alex Converse3d8adf32017-04-24 12:35:42 -07009859
Alex Conversed5d9b6c2017-05-23 15:23:45 -07009860 assert(x->mvcost == x->mv_cost_stack[0]);
9861 // TODO(aconverse@google.com): The full motion field defining discount
9862 // in MV_COST_WEIGHT is too large. Explore other values.
Alex Converse861d7072017-05-15 14:19:53 -07009863 int rate_mv = av1_mv_bit_cost(&dv, &dv_ref.as_mv, x->nmvjointcost,
Alex Conversed5d9b6c2017-05-23 15:23:45 -07009864 x->mvcost, MV_COST_WEIGHT_SUB);
Hui Su6c8584f2017-09-14 15:37:02 -07009865 const int rate_mode = x->intrabc_cost[1];
Alex Converse861d7072017-05-15 14:19:53 -07009866 RD_STATS rd_stats, rd_stats_uv;
9867 av1_subtract_plane(x, bsize, 0);
9868 super_block_yrd(cpi, x, &rd_stats, bsize, INT64_MAX);
9869 super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
9870 av1_merge_rd_stats(&rd_stats, &rd_stats_uv);
Alex Converse3d8adf32017-04-24 12:35:42 -07009871#if CONFIG_RD_DEBUG
Alex Converse861d7072017-05-15 14:19:53 -07009872 mbmi->rd_stats = rd_stats;
Alex Converse3d8adf32017-04-24 12:35:42 -07009873#endif
9874
Alex Conversee16b2662017-05-24 14:00:00 -07009875#if CONFIG_VAR_TX
9876 // TODO(aconverse@google.com): Evaluate allowing VAR TX on intrabc blocks
9877 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
9878 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
9879 int idx, idy;
9880 for (idy = 0; idy < height; ++idy)
9881 for (idx = 0; idx < width; ++idx)
9882 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
9883 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
9884#endif // CONFIG_VAR_TX
9885
Alex Converse861d7072017-05-15 14:19:53 -07009886 const aom_prob skip_prob = av1_get_skip_prob(cm, xd);
Alex Converse3d8adf32017-04-24 12:35:42 -07009887
Alex Converse861d7072017-05-15 14:19:53 -07009888 RD_STATS rdc_noskip;
9889 av1_init_rd_stats(&rdc_noskip);
9890 rdc_noskip.rate =
9891 rate_mode + rate_mv + rd_stats.rate + av1_cost_bit(skip_prob, 0);
9892 rdc_noskip.dist = rd_stats.dist;
Urvang Joshi70006e42017-06-14 16:08:55 -07009893 rdc_noskip.rdcost = RDCOST(x->rdmult, rdc_noskip.rate, rdc_noskip.dist);
Alex Converse861d7072017-05-15 14:19:53 -07009894 if (rdc_noskip.rdcost < best_rd) {
9895 best_rd = rdc_noskip.rdcost;
9896 best_mbmi = *mbmi;
9897 best_skip = x->skip;
9898 best_rdcost = rdc_noskip;
9899 }
Alex Converse3d8adf32017-04-24 12:35:42 -07009900
Alex Converse861d7072017-05-15 14:19:53 -07009901 x->skip = 1;
9902 mbmi->skip = 1;
9903 RD_STATS rdc_skip;
9904 av1_init_rd_stats(&rdc_skip);
9905 rdc_skip.rate = rate_mode + rate_mv + av1_cost_bit(skip_prob, 1);
9906 rdc_skip.dist = rd_stats.sse;
Urvang Joshi70006e42017-06-14 16:08:55 -07009907 rdc_skip.rdcost = RDCOST(x->rdmult, rdc_skip.rate, rdc_skip.dist);
Alex Converse861d7072017-05-15 14:19:53 -07009908 if (rdc_skip.rdcost < best_rd) {
9909 best_rd = rdc_skip.rdcost;
9910 best_mbmi = *mbmi;
9911 best_skip = x->skip;
9912 best_rdcost = rdc_skip;
9913 }
Alex Converse3d8adf32017-04-24 12:35:42 -07009914 }
9915 *mbmi = best_mbmi;
9916 *rd_cost = best_rdcost;
9917 x->skip = best_skip;
9918 return best_rd;
Alex Converse28744302017-04-13 14:46:22 -07009919}
9920#endif // CONFIG_INTRABC
9921
Urvang Joshi52648442016-10-13 17:27:51 -07009922void av1_rd_pick_intra_mode_sb(const AV1_COMP *cpi, MACROBLOCK *x,
Angie Chiang2a2a7dd2017-04-25 16:08:47 -07009923 RD_STATS *rd_cost, BLOCK_SIZE bsize,
Urvang Joshi52648442016-10-13 17:27:51 -07009924 PICK_MODE_CONTEXT *ctx, int64_t best_rd) {
9925 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009926 MACROBLOCKD *const xd = &x->e_mbd;
Luc Trudeau14fc5042017-06-16 12:40:29 -04009927 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009928 struct macroblockd_plane *const pd = xd->plane;
9929 int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0;
9930 int y_skip = 0, uv_skip = 0;
9931 int64_t dist_y = 0, dist_uv = 0;
9932 TX_SIZE max_uv_tx_size;
Jingning Han271bb2c2016-12-14 12:34:46 -08009933 const int unify_bsize = CONFIG_CB4X4;
9934
Yaowu Xuc27fc142016-08-22 16:08:15 -07009935 ctx->skip = 0;
Luc Trudeau14fc5042017-06-16 12:40:29 -04009936 mbmi->ref_frame[0] = INTRA_FRAME;
9937 mbmi->ref_frame[1] = NONE_FRAME;
Alex Converse28744302017-04-13 14:46:22 -07009938#if CONFIG_INTRABC
Luc Trudeau14fc5042017-06-16 12:40:29 -04009939 mbmi->use_intrabc = 0;
9940 mbmi->mv[0].as_int = 0;
Alex Converse28744302017-04-13 14:46:22 -07009941#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07009942
Alex Conversed1b6fad2017-04-26 15:39:37 -07009943 const int64_t intra_yrd =
9944 (bsize >= BLOCK_8X8 || unify_bsize)
9945 ? rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly, &dist_y,
9946 &y_skip, bsize, best_rd)
9947 : rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly,
9948 &dist_y, &y_skip, best_rd);
9949
9950 if (intra_yrd < best_rd) {
Luc Trudeau14fc5042017-06-16 12:40:29 -04009951#if CONFIG_CFL
Luc Trudeau780d2492017-06-15 22:26:41 -04009952#if CONFIG_CB4X4
Luc Trudeaub05eeae2017-08-18 15:14:30 -04009953 // Only store reconstructed luma when there's chroma RDO. When there's no
9954 // chroma RDO, the reconstructed luma will be stored in encode_superblock().
9955 xd->cfl->store_y = !x->skip_chroma_rd;
Luc Trudeau780d2492017-06-15 22:26:41 -04009956#else
Luc Trudeaub05eeae2017-08-18 15:14:30 -04009957 xd->cfl->store_y = 1;
Luc Trudeau6e1cd782017-06-21 13:52:36 -04009958#endif // CONFIG_CB4X4
Luc Trudeaufcca37a2017-08-14 15:05:07 -04009959 if (xd->cfl->store_y) {
Luc Trudeaub05eeae2017-08-18 15:14:30 -04009960 // Perform one extra call to txfm_rd_in_plane(), with the values chosen
9961 // during luma RDO, so we can store reconstructed luma values
9962 RD_STATS this_rd_stats;
Luc Trudeau32306c22017-08-14 14:44:26 -04009963 txfm_rd_in_plane(x, cpi, &this_rd_stats, INT64_MAX, AOM_PLANE_Y,
9964 mbmi->sb_type, mbmi->tx_size,
9965 cpi->sf.use_fast_coef_costing);
Luc Trudeaufcca37a2017-08-14 15:05:07 -04009966 xd->cfl->store_y = 0;
Luc Trudeau32306c22017-08-14 14:44:26 -04009967 }
Luc Trudeau6e1cd782017-06-21 13:52:36 -04009968#endif // CONFIG_CFL
Luc Trudeau14fc5042017-06-16 12:40:29 -04009969 max_uv_tx_size = uv_txsize_lookup[bsize][mbmi->tx_size][pd[1].subsampling_x]
9970 [pd[1].subsampling_y];
9971 init_sbuv_mode(mbmi);
Jingning Han271bb2c2016-12-14 12:34:46 -08009972#if CONFIG_CB4X4
Alex Conversed1b6fad2017-04-26 15:39:37 -07009973 if (!x->skip_chroma_rd)
9974 rd_pick_intra_sbuv_mode(cpi, x, &rate_uv, &rate_uv_tokenonly, &dist_uv,
9975 &uv_skip, bsize, max_uv_tx_size);
Jingning Han271bb2c2016-12-14 12:34:46 -08009976#else
Alex Conversed1b6fad2017-04-26 15:39:37 -07009977 rd_pick_intra_sbuv_mode(cpi, x, &rate_uv, &rate_uv_tokenonly, &dist_uv,
9978 &uv_skip, AOMMAX(BLOCK_8X8, bsize), max_uv_tx_size);
Fergus Simpson4063a682017-02-28 16:52:22 -08009979#endif // CONFIG_CB4X4
Yaowu Xuc27fc142016-08-22 16:08:15 -07009980
Alex Conversed1b6fad2017-04-26 15:39:37 -07009981 if (y_skip && uv_skip) {
9982 rd_cost->rate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly +
9983 av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
9984 rd_cost->dist = dist_y + dist_uv;
9985 } else {
9986 rd_cost->rate =
9987 rate_y + rate_uv + av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
9988 rd_cost->dist = dist_y + dist_uv;
9989 }
Urvang Joshi70006e42017-06-14 16:08:55 -07009990 rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -07009991 } else {
Alex Conversed1b6fad2017-04-26 15:39:37 -07009992 rd_cost->rate = INT_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07009993 }
9994
Alex Converse28744302017-04-13 14:46:22 -07009995#if CONFIG_INTRABC
Alex Conversed1b6fad2017-04-26 15:39:37 -07009996 if (rd_cost->rate != INT_MAX && rd_cost->rdcost < best_rd)
9997 best_rd = rd_cost->rdcost;
Alex Converse28744302017-04-13 14:46:22 -07009998 if (rd_pick_intrabc_mode_sb(cpi, x, rd_cost, bsize, best_rd) < best_rd) {
9999 ctx->skip = x->skip; // FIXME where is the proper place to set this?!
Alex Conversed1b6fad2017-04-26 15:39:37 -070010000 assert(rd_cost->rate != INT_MAX);
Urvang Joshi70006e42017-06-14 16:08:55 -070010001 rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist);
Alex Converse28744302017-04-13 14:46:22 -070010002 }
10003#endif
Alex Conversed1b6fad2017-04-26 15:39:37 -070010004 if (rd_cost->rate == INT_MAX) return;
Alex Converse28744302017-04-13 14:46:22 -070010005
Yaowu Xuc27fc142016-08-22 16:08:15 -070010006 ctx->mic = *xd->mi[0];
10007 ctx->mbmi_ext = *x->mbmi_ext;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010008}
10009
Yaowu Xuc27fc142016-08-22 16:08:15 -070010010// Do we have an internal image edge (e.g. formatting bars).
Urvang Joshi52648442016-10-13 17:27:51 -070010011int av1_internal_image_edge(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070010012 return (cpi->oxcf.pass == 2) &&
10013 ((cpi->twopass.this_frame_stats.inactive_zone_rows > 0) ||
10014 (cpi->twopass.this_frame_stats.inactive_zone_cols > 0));
10015}
10016
10017// Checks to see if a super block is on a horizontal image edge.
10018// In most cases this is the "real" edge unless there are formatting
10019// bars embedded in the stream.
Urvang Joshi52648442016-10-13 17:27:51 -070010020int av1_active_h_edge(const AV1_COMP *cpi, int mi_row, int mi_step) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070010021 int top_edge = 0;
10022 int bottom_edge = cpi->common.mi_rows;
10023 int is_active_h_edge = 0;
10024
10025 // For two pass account for any formatting bars detected.
10026 if (cpi->oxcf.pass == 2) {
Urvang Joshi52648442016-10-13 17:27:51 -070010027 const TWO_PASS *const twopass = &cpi->twopass;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010028
10029 // The inactive region is specified in MBs not mi units.
10030 // The image edge is in the following MB row.
10031 top_edge += (int)(twopass->this_frame_stats.inactive_zone_rows * 2);
10032
10033 bottom_edge -= (int)(twopass->this_frame_stats.inactive_zone_rows * 2);
Yaowu Xuf883b422016-08-30 14:01:10 -070010034 bottom_edge = AOMMAX(top_edge, bottom_edge);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010035 }
10036
10037 if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) ||
10038 ((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) {
10039 is_active_h_edge = 1;
10040 }
10041 return is_active_h_edge;
10042}
10043
10044// Checks to see if a super block is on a vertical image edge.
10045// In most cases this is the "real" edge unless there are formatting
10046// bars embedded in the stream.
Urvang Joshi52648442016-10-13 17:27:51 -070010047int av1_active_v_edge(const AV1_COMP *cpi, int mi_col, int mi_step) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070010048 int left_edge = 0;
10049 int right_edge = cpi->common.mi_cols;
10050 int is_active_v_edge = 0;
10051
10052 // For two pass account for any formatting bars detected.
10053 if (cpi->oxcf.pass == 2) {
Urvang Joshi52648442016-10-13 17:27:51 -070010054 const TWO_PASS *const twopass = &cpi->twopass;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010055
10056 // The inactive region is specified in MBs not mi units.
10057 // The image edge is in the following MB row.
10058 left_edge += (int)(twopass->this_frame_stats.inactive_zone_cols * 2);
10059
10060 right_edge -= (int)(twopass->this_frame_stats.inactive_zone_cols * 2);
Yaowu Xuf883b422016-08-30 14:01:10 -070010061 right_edge = AOMMAX(left_edge, right_edge);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010062 }
10063
10064 if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) ||
10065 ((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) {
10066 is_active_v_edge = 1;
10067 }
10068 return is_active_v_edge;
10069}
10070
10071// Checks to see if a super block is at the edge of the active image.
10072// In most cases this is the "real" edge unless there are formatting
10073// bars embedded in the stream.
Urvang Joshi52648442016-10-13 17:27:51 -070010074int av1_active_edge_sb(const AV1_COMP *cpi, int mi_row, int mi_col) {
Yaowu Xuf883b422016-08-30 14:01:10 -070010075 return av1_active_h_edge(cpi, mi_row, cpi->common.mib_size) ||
10076 av1_active_v_edge(cpi, mi_col, cpi->common.mib_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010077}
10078
Urvang Joshi52648442016-10-13 17:27:51 -070010079static void restore_uv_color_map(const AV1_COMP *const cpi, MACROBLOCK *x) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070010080 MACROBLOCKD *const xd = &x->e_mbd;
10081 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
10082 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
10083 const BLOCK_SIZE bsize = mbmi->sb_type;
Urvang Joshic9e71d42017-08-09 18:58:33 -070010084 assert(bsize >= BLOCK_8X8);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010085 int src_stride = x->plane[1].src.stride;
10086 const uint8_t *const src_u = x->plane[1].src.buf;
10087 const uint8_t *const src_v = x->plane[2].src.buf;
10088 float *const data = x->palette_buffer->kmeans_data_buf;
10089 float centroids[2 * PALETTE_MAX_SIZE];
10090 uint8_t *const color_map = xd->plane[1].color_index_map;
10091 int r, c;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010092#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070010093 const uint16_t *const src_u16 = CONVERT_TO_SHORTPTR(src_u);
10094 const uint16_t *const src_v16 = CONVERT_TO_SHORTPTR(src_v);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010095#endif // CONFIG_HIGHBITDEPTH
Urvang Joshi56ba91b2017-01-10 13:22:09 -080010096 int plane_block_width, plane_block_height, rows, cols;
10097 av1_get_block_dimensions(bsize, 1, xd, &plane_block_width,
10098 &plane_block_height, &rows, &cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010099 (void)cpi;
10100
10101 for (r = 0; r < rows; ++r) {
10102 for (c = 0; c < cols; ++c) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010103#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070010104 if (cpi->common.use_highbitdepth) {
10105 data[(r * cols + c) * 2] = src_u16[r * src_stride + c];
10106 data[(r * cols + c) * 2 + 1] = src_v16[r * src_stride + c];
10107 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010108#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070010109 data[(r * cols + c) * 2] = src_u[r * src_stride + c];
10110 data[(r * cols + c) * 2 + 1] = src_v[r * src_stride + c];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010111#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070010112 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010113#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070010114 }
10115 }
10116
10117 for (r = 1; r < 3; ++r) {
10118 for (c = 0; c < pmi->palette_size[1]; ++c) {
10119 centroids[c * 2 + r - 1] = pmi->palette_colors[r * PALETTE_MAX_SIZE + c];
10120 }
10121 }
10122
Yaowu Xuf883b422016-08-30 14:01:10 -070010123 av1_calc_indices(data, centroids, color_map, rows * cols,
10124 pmi->palette_size[1], 2);
Urvang Joshi56ba91b2017-01-10 13:22:09 -080010125 extend_palette_color_map(color_map, cols, rows, plane_block_width,
10126 plane_block_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010127}
10128
hui su5db97432016-10-14 16:10:14 -070010129#if CONFIG_FILTER_INTRA
10130static void pick_filter_intra_interframe(
Luc Trudeau9d4cbb82017-07-27 17:01:32 -040010131 const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row,
10132 int mi_col, int *rate_uv_intra, int *rate_uv_tokenonly, int64_t *dist_uv,
10133 int *skip_uv, UV_PREDICTION_MODE *mode_uv,
Luc Trudeaud6d9eee2017-07-12 12:36:50 -040010134 FILTER_INTRA_MODE_INFO *filter_intra_mode_info_uv,
hui su5db97432016-10-14 16:10:14 -070010135#if CONFIG_EXT_INTRA
10136 int8_t *uv_angle_delta,
10137#endif // CONFIG_EXT_INTRA
Urvang Joshic6300aa2017-06-01 14:46:23 -070010138 PALETTE_MODE_INFO *pmi_uv, int palette_ctx, int skip_mask,
10139 unsigned int *ref_costs_single, int64_t *best_rd, int64_t *best_intra_rd,
10140 PREDICTION_MODE *best_intra_mode, int *best_mode_index, int *best_skip2,
10141 int *best_mode_skippable,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010142#if CONFIG_SUPERTX
10143 int *returnrate_nocoef,
10144#endif // CONFIG_SUPERTX
Angie Chiang2a2a7dd2017-04-25 16:08:47 -070010145 int64_t *best_pred_rd, MB_MODE_INFO *best_mbmode, RD_STATS *rd_cost) {
Urvang Joshi52648442016-10-13 17:27:51 -070010146 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010147 MACROBLOCKD *const xd = &x->e_mbd;
10148 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
10149 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
Urvang Joshic9e71d42017-08-09 18:58:33 -070010150 const int try_palette = can_use_palette(cpi, mbmi);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010151 int rate2 = 0, rate_y = INT_MAX, skippable = 0, rate_uv, rate_dummy, i;
10152 int dc_mode_index;
Yue Chenb23d00a2017-07-28 17:01:21 -070010153 const int *const intra_mode_cost = x->mbmode_cost[size_group_lookup[bsize]];
hui su8f4cc0a2017-01-13 15:14:49 -080010154 int64_t distortion2 = 0, distortion_y = 0, this_rd = *best_rd;
10155 int64_t distortion_uv, model_rd = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010156 TX_SIZE uv_tx;
10157
10158 for (i = 0; i < MAX_MODES; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -070010159 if (av1_mode_order[i].mode == DC_PRED &&
10160 av1_mode_order[i].ref_frame[0] == INTRA_FRAME)
Yaowu Xuc27fc142016-08-22 16:08:15 -070010161 break;
10162 dc_mode_index = i;
10163 assert(i < MAX_MODES);
10164
10165 // TODO(huisu): use skip_mask for further speedup.
10166 (void)skip_mask;
10167 mbmi->mode = DC_PRED;
Luc Trudeaud6d9eee2017-07-12 12:36:50 -040010168 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010169 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -050010170 mbmi->ref_frame[1] = NONE_FRAME;
hui su5db97432016-10-14 16:10:14 -070010171 if (!rd_pick_filter_intra_sby(cpi, x, &rate_dummy, &rate_y, &distortion_y,
10172 &skippable, bsize, intra_mode_cost[mbmi->mode],
hui su8f4cc0a2017-01-13 15:14:49 -080010173 &this_rd, &model_rd, 0)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070010174 return;
hui su5db97432016-10-14 16:10:14 -070010175 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070010176 if (rate_y == INT_MAX) return;
10177
Debargha Mukherjee2f123402016-08-30 17:43:38 -070010178 uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x]
10179 [xd->plane[1].subsampling_y];
Yaowu Xuc27fc142016-08-22 16:08:15 -070010180 if (rate_uv_intra[uv_tx] == INT_MAX) {
Luc Trudeau9d4cbb82017-07-27 17:01:32 -040010181 choose_intra_uv_mode(cpi, x, bsize, uv_tx, &rate_uv_intra[uv_tx],
Yaowu Xuc27fc142016-08-22 16:08:15 -070010182 &rate_uv_tokenonly[uv_tx], &dist_uv[uv_tx],
10183 &skip_uv[uv_tx], &mode_uv[uv_tx]);
10184 if (cm->allow_screen_content_tools) pmi_uv[uv_tx] = *pmi;
hui su5db97432016-10-14 16:10:14 -070010185 filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info;
10186#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010187 uv_angle_delta[uv_tx] = mbmi->angle_delta[1];
hui su5db97432016-10-14 16:10:14 -070010188#endif // CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010189 }
10190
10191 rate_uv = rate_uv_tokenonly[uv_tx];
10192 distortion_uv = dist_uv[uv_tx];
10193 skippable = skippable && skip_uv[uv_tx];
10194 mbmi->uv_mode = mode_uv[uv_tx];
10195 if (cm->allow_screen_content_tools) {
10196 pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1];
10197 memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
10198 pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE,
10199 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
10200 }
hui su5db97432016-10-14 16:10:14 -070010201#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010202 mbmi->angle_delta[1] = uv_angle_delta[uv_tx];
hui su5db97432016-10-14 16:10:14 -070010203#endif // CONFIG_EXT_INTRA
10204 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
10205 filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1];
10206 if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) {
10207 mbmi->filter_intra_mode_info.filter_intra_mode[1] =
10208 filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1];
Yaowu Xuc27fc142016-08-22 16:08:15 -070010209 }
10210
10211 rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv +
Yue Chenb23d00a2017-07-28 17:01:21 -070010212 x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode];
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +010010213 if (try_palette && mbmi->mode == DC_PRED)
Yaowu Xuf883b422016-08-30 14:01:10 -070010214 rate2 += av1_cost_bit(
10215 av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010216
10217 if (!xd->lossless[mbmi->segment_id]) {
10218 // super_block_yrd above includes the cost of the tx_size in the
10219 // tokenonly rate, but for intra blocks, tx_size is always coded
10220 // (prediction granularity), so we account for it in the full rate,
10221 // not the tokenonly rate.
Urvang Joshifeb925f2016-12-05 10:37:29 -080010222 rate_y -= tx_size_cost(cpi, x, bsize, mbmi->tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010223 }
10224
hui su5db97432016-10-14 16:10:14 -070010225 rate2 += av1_cost_bit(cm->fc->filter_intra_probs[0],
10226 mbmi->filter_intra_mode_info.use_filter_intra_mode[0]);
10227 rate2 += write_uniform_cost(
10228 FILTER_INTRA_MODES, mbmi->filter_intra_mode_info.filter_intra_mode[0]);
10229#if CONFIG_EXT_INTRA
Luc Trudeauf24a35f2017-09-11 11:56:33 -040010230 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize) &&
Joe Young830d4ce2017-05-30 17:48:13 -070010231 av1_use_angle_delta(bsize)) {
hui su0a6731f2017-04-26 15:23:47 -070010232 rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1,
10233 MAX_ANGLE_DELTA + mbmi->angle_delta[1]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010234 }
hui su5db97432016-10-14 16:10:14 -070010235#endif // CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010236 if (mbmi->mode == DC_PRED) {
hui su5db97432016-10-14 16:10:14 -070010237 rate2 +=
10238 av1_cost_bit(cpi->common.fc->filter_intra_probs[1],
10239 mbmi->filter_intra_mode_info.use_filter_intra_mode[1]);
10240 if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1])
10241 rate2 +=
10242 write_uniform_cost(FILTER_INTRA_MODES,
10243 mbmi->filter_intra_mode_info.filter_intra_mode[1]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010244 }
10245 distortion2 = distortion_y + distortion_uv;
Jingning Han18c53c82017-02-17 14:49:57 -080010246 av1_encode_intra_block_plane((AV1_COMMON *)cm, x, bsize, 0, 0, mi_row,
10247 mi_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010248
10249 rate2 += ref_costs_single[INTRA_FRAME];
10250
10251 if (skippable) {
10252 rate2 -= (rate_y + rate_uv);
10253 rate_y = 0;
10254 rate_uv = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070010255 rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010256 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -070010257 rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010258 }
Urvang Joshi70006e42017-06-14 16:08:55 -070010259 this_rd = RDCOST(x->rdmult, rate2, distortion2);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010260
10261 if (this_rd < *best_intra_rd) {
10262 *best_intra_rd = this_rd;
10263 *best_intra_mode = mbmi->mode;
10264 }
10265 for (i = 0; i < REFERENCE_MODES; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -070010266 best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010267
10268 if (this_rd < *best_rd) {
10269 *best_mode_index = dc_mode_index;
10270 mbmi->mv[0].as_int = 0;
10271 rd_cost->rate = rate2;
10272#if CONFIG_SUPERTX
10273 if (x->skip)
10274 *returnrate_nocoef = rate2;
10275 else
10276 *returnrate_nocoef = rate2 - rate_y - rate_uv;
Yaowu Xuf883b422016-08-30 14:01:10 -070010277 *returnrate_nocoef -= av1_cost_bit(av1_get_skip_prob(cm, xd), skippable);
10278 *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd),
10279 mbmi->ref_frame[0] != INTRA_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010280#endif // CONFIG_SUPERTX
10281 rd_cost->dist = distortion2;
10282 rd_cost->rdcost = this_rd;
10283 *best_rd = this_rd;
10284 *best_mbmode = *mbmi;
10285 *best_skip2 = 0;
10286 *best_mode_skippable = skippable;
10287 }
10288}
hui su5db97432016-10-14 16:10:14 -070010289#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010290
Yue Chencb60b182016-10-13 15:18:22 -070010291#if CONFIG_MOTION_VAR
Yaowu Xuf883b422016-08-30 14:01:10 -070010292static void calc_target_weighted_pred(const AV1_COMMON *cm, const MACROBLOCK *x,
10293 const MACROBLOCKD *xd, int mi_row,
10294 int mi_col, const uint8_t *above,
10295 int above_stride, const uint8_t *left,
Yue Chene9638cc2016-10-10 12:37:54 -070010296 int left_stride);
Yue Chencb60b182016-10-13 15:18:22 -070010297#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -070010298
Urvang Joshi52648442016-10-13 17:27:51 -070010299void av1_rd_pick_inter_mode_sb(const AV1_COMP *cpi, TileDataEnc *tile_data,
Yaowu Xuf883b422016-08-30 14:01:10 -070010300 MACROBLOCK *x, int mi_row, int mi_col,
Angie Chiang2a2a7dd2017-04-25 16:08:47 -070010301 RD_STATS *rd_cost,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010302#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -070010303 int *returnrate_nocoef,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010304#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -070010305 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
10306 int64_t best_rd_so_far) {
Urvang Joshi52648442016-10-13 17:27:51 -070010307 const AV1_COMMON *const cm = &cpi->common;
10308 const RD_OPT *const rd_opt = &cpi->rd;
10309 const SPEED_FEATURES *const sf = &cpi->sf;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010310 MACROBLOCKD *const xd = &x->e_mbd;
10311 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Urvang Joshic9e71d42017-08-09 18:58:33 -070010312 const int try_palette = can_use_palette(cpi, mbmi);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010313 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
10314 MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
10315 const struct segmentation *const seg = &cm->seg;
10316 PREDICTION_MODE this_mode;
10317 MV_REFERENCE_FRAME ref_frame, second_ref_frame;
10318 unsigned char segment_id = mbmi->segment_id;
10319 int comp_pred, i, k;
10320 int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME];
Zoe Liu85b66462017-04-20 14:28:19 -070010321#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
10322 int_mv frame_comp_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME];
10323#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -070010324 struct buf_2d yv12_mb[TOTAL_REFS_PER_FRAME][MAX_MB_PLANE];
Yaowu Xuc27fc142016-08-22 16:08:15 -070010325 int_mv single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } };
Zoe Liu7f24e1b2017-03-17 17:42:05 -070010326#if CONFIG_EXT_INTER
10327 int single_newmv_rate[TOTAL_REFS_PER_FRAME] = { 0 };
10328 int64_t modelled_rd[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME];
Yaowu Xuc27fc142016-08-22 16:08:15 -070010329#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070010330 static const int flag_list[TOTAL_REFS_PER_FRAME] = {
10331 0,
Yaowu Xuf883b422016-08-30 14:01:10 -070010332 AOM_LAST_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010333#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -070010334 AOM_LAST2_FLAG,
10335 AOM_LAST3_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010336#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -070010337 AOM_GOLD_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010338#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -070010339 AOM_BWD_FLAG,
Zoe Liue9b15e22017-07-19 15:53:01 -070010340 AOM_ALT2_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010341#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -070010342 AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -070010343 };
10344 int64_t best_rd = best_rd_so_far;
10345 int best_rate_y = INT_MAX, best_rate_uv = INT_MAX;
10346 int64_t best_pred_diff[REFERENCE_MODES];
10347 int64_t best_pred_rd[REFERENCE_MODES];
10348 MB_MODE_INFO best_mbmode;
Yaowu Xu4306b6e2016-09-27 12:55:32 -070010349 int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
10350 int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010351 int best_mode_skippable = 0;
10352 int midx, best_mode_index = -1;
10353 unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME];
Zoe Liuc082bbc2017-05-17 13:31:37 -070010354#if CONFIG_EXT_COMP_REFS
10355 unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME][TOTAL_REFS_PER_FRAME];
10356#else
Yaowu Xuc27fc142016-08-22 16:08:15 -070010357 unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME];
Zoe Liuc082bbc2017-05-17 13:31:37 -070010358#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -070010359 aom_prob comp_mode_p;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010360 int64_t best_intra_rd = INT64_MAX;
10361 unsigned int best_pred_sse = UINT_MAX;
10362 PREDICTION_MODE best_intra_mode = DC_PRED;
Urvang Joshifeb925f2016-12-05 10:37:29 -080010363 int rate_uv_intra[TX_SIZES_ALL], rate_uv_tokenonly[TX_SIZES_ALL];
10364 int64_t dist_uvs[TX_SIZES_ALL];
10365 int skip_uvs[TX_SIZES_ALL];
Luc Trudeaud6d9eee2017-07-12 12:36:50 -040010366 UV_PREDICTION_MODE mode_uv[TX_SIZES_ALL];
Urvang Joshifeb925f2016-12-05 10:37:29 -080010367 PALETTE_MODE_INFO pmi_uv[TX_SIZES_ALL];
Yaowu Xuc27fc142016-08-22 16:08:15 -070010368#if CONFIG_EXT_INTRA
Urvang Joshifeb925f2016-12-05 10:37:29 -080010369 int8_t uv_angle_delta[TX_SIZES_ALL];
Yaowu Xuc27fc142016-08-22 16:08:15 -070010370 int is_directional_mode, angle_stats_ready = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010371 uint8_t directional_mode_skip_mask[INTRA_MODES];
10372#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -070010373#if CONFIG_FILTER_INTRA
10374 int8_t dc_skipped = 1;
Urvang Joshifeb925f2016-12-05 10:37:29 -080010375 FILTER_INTRA_MODE_INFO filter_intra_mode_info_uv[TX_SIZES_ALL];
hui su5db97432016-10-14 16:10:14 -070010376#endif // CONFIG_FILTER_INTRA
Yaowu Xuf883b422016-08-30 14:01:10 -070010377 const int intra_cost_penalty = av1_get_intra_cost_penalty(
Yaowu Xuc27fc142016-08-22 16:08:15 -070010378 cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth);
Yue Chenb23d00a2017-07-28 17:01:21 -070010379 const int *const intra_mode_cost = x->mbmode_cost[size_group_lookup[bsize]];
Yaowu Xuc27fc142016-08-22 16:08:15 -070010380 int best_skip2 = 0;
Zoe Liu97ad0582017-02-09 10:51:00 -080010381 uint16_t ref_frame_skip_mask[2] = { 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -070010382 uint32_t mode_skip_mask[TOTAL_REFS_PER_FRAME] = { 0 };
Yue Chen4d26acb2017-05-01 12:28:34 -070010383#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010384 MV_REFERENCE_FRAME best_single_inter_ref = LAST_FRAME;
10385 int64_t best_single_inter_rd = INT64_MAX;
Yue Chen4d26acb2017-05-01 12:28:34 -070010386#endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010387 int mode_skip_start = sf->mode_skip_start + 1;
10388 const int *const rd_threshes = rd_opt->threshes[segment_id][bsize];
10389 const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
10390 int64_t mode_threshold[MAX_MODES];
10391 int *mode_map = tile_data->mode_map[bsize];
10392 const int mode_search_skip_flags = sf->mode_search_skip_flags;
Yushin Cho77bba8d2016-11-04 16:36:56 -070010393#if CONFIG_PVQ
10394 od_rollback_buffer pre_buf;
Fergus Simpson4063a682017-02-28 16:52:22 -080010395#endif // CONFIG_PVQ
Yushin Cho77bba8d2016-11-04 16:36:56 -070010396
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080010397 HandleInterModeArgs args = {
Fergus Simpson073c6f32017-02-17 12:13:48 -080010398#if CONFIG_MOTION_VAR
10399 { NULL },
10400 { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE },
10401 { NULL },
10402 { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE },
10403#endif // CONFIG_MOTION_VAR
10404#if CONFIG_EXT_INTER
10405 NULL,
10406 NULL,
10407 NULL,
Fergus Simpson073c6f32017-02-17 12:13:48 -080010408#else // CONFIG_EXT_INTER
10409 NULL,
10410#endif // CONFIG_EXT_INTER
Fergus Simpson3424c2d2017-03-09 11:48:15 -080010411 { { 0 } },
Fergus Simpson073c6f32017-02-17 12:13:48 -080010412 };
10413
Jingning Hanae5cfde2016-11-30 12:01:44 -080010414 const int rows = block_size_high[bsize];
10415 const int cols = block_size_wide[bsize];
Urvang Joshib100db72016-10-12 16:28:56 -070010416 int palette_ctx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010417 const MODE_INFO *above_mi = xd->above_mi;
10418 const MODE_INFO *left_mi = xd->left_mi;
Yue Chencb60b182016-10-13 15:18:22 -070010419#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -070010420 int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
10421 int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
10422 int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
10423 int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
Yaowu Xuc27fc142016-08-22 16:08:15 -070010424
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010425#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070010426 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
10427 int len = sizeof(uint16_t);
Jingning Hand064cf02017-06-01 10:00:39 -070010428 args.above_pred_buf[0] = CONVERT_TO_BYTEPTR(x->above_pred_buf);
10429 args.above_pred_buf[1] =
10430 CONVERT_TO_BYTEPTR(x->above_pred_buf + MAX_SB_SQUARE * len);
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080010431 args.above_pred_buf[2] =
Jingning Hand064cf02017-06-01 10:00:39 -070010432 CONVERT_TO_BYTEPTR(x->above_pred_buf + 2 * MAX_SB_SQUARE * len);
10433 args.left_pred_buf[0] = CONVERT_TO_BYTEPTR(x->left_pred_buf);
10434 args.left_pred_buf[1] =
10435 CONVERT_TO_BYTEPTR(x->left_pred_buf + MAX_SB_SQUARE * len);
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080010436 args.left_pred_buf[2] =
Jingning Hand064cf02017-06-01 10:00:39 -070010437 CONVERT_TO_BYTEPTR(x->left_pred_buf + 2 * MAX_SB_SQUARE * len);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010438 } else {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010439#endif // CONFIG_HIGHBITDEPTH
Jingning Hand064cf02017-06-01 10:00:39 -070010440 args.above_pred_buf[0] = x->above_pred_buf;
10441 args.above_pred_buf[1] = x->above_pred_buf + MAX_SB_SQUARE;
10442 args.above_pred_buf[2] = x->above_pred_buf + 2 * MAX_SB_SQUARE;
10443 args.left_pred_buf[0] = x->left_pred_buf;
10444 args.left_pred_buf[1] = x->left_pred_buf + MAX_SB_SQUARE;
10445 args.left_pred_buf[2] = x->left_pred_buf + 2 * MAX_SB_SQUARE;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010446#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070010447 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010448#endif // CONFIG_HIGHBITDEPTH
Yue Chencb60b182016-10-13 15:18:22 -070010449#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -070010450
Yaowu Xuf883b422016-08-30 14:01:10 -070010451 av1_zero(best_mbmode);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010452
Urvang Joshib100db72016-10-12 16:28:56 -070010453 av1_zero(pmi_uv);
hui su9bc1d8d2017-03-24 12:36:03 -070010454 if (try_palette) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070010455 if (above_mi)
10456 palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
10457 if (left_mi)
10458 palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
10459 }
10460
Yaowu Xuc27fc142016-08-22 16:08:15 -070010461 estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
10462 &comp_mode_p);
10463
10464 for (i = 0; i < REFERENCE_MODES; ++i) best_pred_rd[i] = INT64_MAX;
Urvang Joshifeb925f2016-12-05 10:37:29 -080010465 for (i = 0; i < TX_SIZES_ALL; i++) rate_uv_intra[i] = INT_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010466 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX;
10467 for (i = 0; i < MB_MODE_COUNT; ++i) {
10468 for (k = 0; k < TOTAL_REFS_PER_FRAME; ++k) {
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080010469 args.single_filter[i][k] = SWITCHABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010470 }
10471 }
10472
10473 rd_cost->rate = INT_MAX;
10474#if CONFIG_SUPERTX
10475 *returnrate_nocoef = INT_MAX;
10476#endif // CONFIG_SUPERTX
10477
10478 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
10479 x->pred_mv_sad[ref_frame] = INT_MAX;
10480 x->mbmi_ext->mode_context[ref_frame] = 0;
Sebastien Alaiwane140c502017-04-27 09:52:34 +020010481#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070010482 x->mbmi_ext->compound_mode_context[ref_frame] = 0;
Sebastien Alaiwane140c502017-04-27 09:52:34 +020010483#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070010484 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
10485 assert(get_ref_frame_buffer(cpi, ref_frame) != NULL);
10486 setup_buffer_inter(cpi, x, ref_frame, bsize, mi_row, mi_col,
10487 frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
10488 }
10489 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
Sarah Parkere5299862016-08-16 14:57:37 -070010490#if CONFIG_GLOBAL_MOTION
10491 frame_mv[ZEROMV][ref_frame].as_int =
Sarah Parkerae7c4582017-02-28 16:30:30 -080010492 gm_get_motion_vector(&cm->global_motion[ref_frame],
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -080010493 cm->allow_high_precision_mv, bsize, mi_col, mi_row,
RogerZhou3b635242017-09-19 10:06:46 -070010494 0
10495#if CONFIG_AMVR
10496 ,
10497 cm->cur_frame_mv_precision_level
10498#endif
10499 )
David Barkercdcac6d2016-12-01 17:04:16 +000010500 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -070010501#else // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070010502 frame_mv[ZEROMV][ref_frame].as_int = 0;
Sarah Parkere5299862016-08-16 14:57:37 -070010503#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070010504#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070010505 frame_mv[NEW_NEWMV][ref_frame].as_int = INVALID_MV;
Zoe Liu85b66462017-04-20 14:28:19 -070010506#if CONFIG_COMPOUND_SINGLEREF
10507 frame_mv[SR_NEW_NEWMV][ref_frame].as_int = INVALID_MV;
10508 frame_comp_mv[SR_NEW_NEWMV][ref_frame].as_int = INVALID_MV;
10509#endif // CONFIG_COMPOUND_SINGLEREF
Sarah Parkerc2d38712017-01-24 15:15:41 -080010510#if CONFIG_GLOBAL_MOTION
10511 frame_mv[ZERO_ZEROMV][ref_frame].as_int =
Sarah Parkerae7c4582017-02-28 16:30:30 -080010512 gm_get_motion_vector(&cm->global_motion[ref_frame],
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -080010513 cm->allow_high_precision_mv, bsize, mi_col, mi_row,
RogerZhou3b635242017-09-19 10:06:46 -070010514 0
10515#if CONFIG_AMVR
10516 ,
10517 cm->cur_frame_mv_precision_level
10518#endif
10519 )
Sarah Parkerc2d38712017-01-24 15:15:41 -080010520 .as_int;
10521#else // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070010522 frame_mv[ZERO_ZEROMV][ref_frame].as_int = 0;
Sarah Parkerc2d38712017-01-24 15:15:41 -080010523#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070010524#endif // CONFIG_EXT_INTER
10525 }
10526
Yaowu Xuc27fc142016-08-22 16:08:15 -070010527 for (; ref_frame < MODE_CTX_REF_FRAMES; ++ref_frame) {
10528 MODE_INFO *const mi = xd->mi[0];
10529 int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
10530 x->mbmi_ext->mode_context[ref_frame] = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070010531 av1_find_mv_refs(cm, xd, mi, ref_frame, &mbmi_ext->ref_mv_count[ref_frame],
10532 mbmi_ext->ref_mv_stack[ref_frame],
Yaowu Xuc27fc142016-08-22 16:08:15 -070010533#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -070010534 mbmi_ext->compound_mode_context,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010535#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -070010536 candidates, mi_row, mi_col, NULL, NULL,
10537 mbmi_ext->mode_context);
Jingning Han731af492016-11-17 11:53:23 -080010538 if (mbmi_ext->ref_mv_count[ref_frame] < 2) {
10539 MV_REFERENCE_FRAME rf[2];
10540 av1_set_ref_frame(rf, ref_frame);
David Barkercdcac6d2016-12-01 17:04:16 +000010541 if (mbmi_ext->ref_mvs[rf[0]][0].as_int !=
10542 frame_mv[ZEROMV][rf[0]].as_int ||
10543 mbmi_ext->ref_mvs[rf[0]][1].as_int !=
10544 frame_mv[ZEROMV][rf[0]].as_int ||
10545 mbmi_ext->ref_mvs[rf[1]][0].as_int !=
10546 frame_mv[ZEROMV][rf[1]].as_int ||
10547 mbmi_ext->ref_mvs[rf[1]][1].as_int != frame_mv[ZEROMV][rf[1]].as_int)
Jingning Han731af492016-11-17 11:53:23 -080010548 mbmi_ext->mode_context[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
10549 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070010550 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070010551
Yue Chencb60b182016-10-13 15:18:22 -070010552#if CONFIG_MOTION_VAR
Yue Chen5329a2b2017-02-28 17:33:00 +080010553 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
Jingning Hanad586b92017-05-23 10:24:57 -070010554
Yue Chenf7ba6472017-04-19 11:08:58 -070010555 if (check_num_overlappable_neighbors(mbmi) &&
10556 is_motion_variation_allowed_bsize(bsize)) {
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080010557 av1_build_prediction_by_above_preds(cm, xd, mi_row, mi_col,
10558 args.above_pred_buf, dst_width1,
10559 dst_height1, args.above_pred_stride);
Yue Chen5329a2b2017-02-28 17:33:00 +080010560 av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col,
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080010561 args.left_pred_buf, dst_width2,
10562 dst_height2, args.left_pred_stride);
Jingning Han91d9a792017-04-18 12:01:52 -070010563 av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
10564 mi_col);
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080010565 calc_target_weighted_pred(cm, x, xd, mi_row, mi_col, args.above_pred_buf[0],
10566 args.above_pred_stride[0], args.left_pred_buf[0],
10567 args.left_pred_stride[0]);
Yue Chen5329a2b2017-02-28 17:33:00 +080010568 }
Yue Chencb60b182016-10-13 15:18:22 -070010569#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -070010570
10571 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
10572 if (!(cpi->ref_frame_flags & flag_list[ref_frame])) {
Zoe Liue9b15e22017-07-19 15:53:01 -070010573 // Skip checking missing references in both single and compound reference
10574 // modes. Note that a mode will be skipped iff both reference frames
10575 // are masked out.
Zoe Liuc082bbc2017-05-17 13:31:37 -070010576 ref_frame_skip_mask[0] |= (1 << ref_frame);
10577 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010578 } else {
10579 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
10580 // Skip fixed mv modes for poor references
10581 if ((x->pred_mv_sad[ref_frame] >> 2) > x->pred_mv_sad[i]) {
10582 mode_skip_mask[ref_frame] |= INTER_NEAREST_NEAR_ZERO;
10583 break;
10584 }
10585 }
10586 }
10587 // If the segment reference frame feature is enabled....
10588 // then do nothing if the current ref frame is not allowed..
10589 if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
10590 get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) {
10591 ref_frame_skip_mask[0] |= (1 << ref_frame);
10592 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
10593 }
10594 }
10595
10596 // Disable this drop out case if the ref frame
10597 // segment level feature is enabled for this segment. This is to
10598 // prevent the possibility that we end up unable to pick any mode.
10599 if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
10600 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
10601 // unless ARNR filtering is enabled in which case we want
10602 // an unfiltered alternative. We allow near/nearest as well
10603 // because they may result in zero-zero MVs but be cheaper.
10604 if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) {
Sarah Parkere5299862016-08-16 14:57:37 -070010605 int_mv zeromv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010606 ref_frame_skip_mask[0] = (1 << LAST_FRAME) |
10607#if CONFIG_EXT_REFS
10608 (1 << LAST2_FRAME) | (1 << LAST3_FRAME) |
Zoe Liu3ac20932017-08-30 16:35:55 -070010609 (1 << BWDREF_FRAME) | (1 << ALTREF2_FRAME) |
Yaowu Xuc27fc142016-08-22 16:08:15 -070010610#endif // CONFIG_EXT_REFS
10611 (1 << GOLDEN_FRAME);
10612 ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK;
10613 // TODO(zoeliu): To further explore whether following needs to be done for
10614 // BWDREF_FRAME as well.
10615 mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO;
Sarah Parkere5299862016-08-16 14:57:37 -070010616#if CONFIG_GLOBAL_MOTION
David Barkercdcac6d2016-12-01 17:04:16 +000010617 zeromv.as_int = gm_get_motion_vector(&cm->global_motion[ALTREF_FRAME],
Sarah Parkerae7c4582017-02-28 16:30:30 -080010618 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -070010619 mi_col, mi_row, 0
10620#if CONFIG_AMVR
10621 ,
10622 cm->cur_frame_mv_precision_level
10623#endif
10624 )
David Barkercdcac6d2016-12-01 17:04:16 +000010625 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -070010626#else
10627 zeromv.as_int = 0;
10628#endif // CONFIG_GLOBAL_MOTION
10629 if (frame_mv[NEARMV][ALTREF_FRAME].as_int != zeromv.as_int)
Yaowu Xuc27fc142016-08-22 16:08:15 -070010630 mode_skip_mask[ALTREF_FRAME] |= (1 << NEARMV);
Sarah Parkere5299862016-08-16 14:57:37 -070010631 if (frame_mv[NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int)
Yaowu Xuc27fc142016-08-22 16:08:15 -070010632 mode_skip_mask[ALTREF_FRAME] |= (1 << NEARESTMV);
10633#if CONFIG_EXT_INTER
Sarah Parkere5299862016-08-16 14:57:37 -070010634 if (frame_mv[NEAREST_NEARESTMV][ALTREF_FRAME].as_int != zeromv.as_int)
Yaowu Xuc27fc142016-08-22 16:08:15 -070010635 mode_skip_mask[ALTREF_FRAME] |= (1 << NEAREST_NEARESTMV);
Sarah Parkere5299862016-08-16 14:57:37 -070010636 if (frame_mv[NEAR_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int)
Yaowu Xuc27fc142016-08-22 16:08:15 -070010637 mode_skip_mask[ALTREF_FRAME] |= (1 << NEAR_NEARMV);
Zoe Liu85b66462017-04-20 14:28:19 -070010638#if CONFIG_COMPOUND_SINGLEREF
10639 if (frame_mv[SR_NEAREST_NEARMV][ALTREF_FRAME].as_int != zeromv.as_int ||
10640 frame_comp_mv[SR_NEAREST_NEARMV][ALTREF_FRAME].as_int !=
10641 zeromv.as_int)
10642 mode_skip_mask[ALTREF_FRAME] |= (1 << SR_NEAREST_NEARMV);
10643#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -070010644#endif // CONFIG_EXT_INTER
10645 }
10646 }
10647
10648 if (cpi->rc.is_src_frame_alt_ref) {
10649 if (sf->alt_ref_search_fp) {
10650 assert(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]);
10651 mode_skip_mask[ALTREF_FRAME] = 0;
10652 ref_frame_skip_mask[0] = ~(1 << ALTREF_FRAME);
10653 ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK;
10654 }
10655 }
10656
10657 if (sf->alt_ref_search_fp)
10658 if (!cm->show_frame && x->pred_mv_sad[GOLDEN_FRAME] < INT_MAX)
10659 if (x->pred_mv_sad[ALTREF_FRAME] > (x->pred_mv_sad[GOLDEN_FRAME] << 1))
10660 mode_skip_mask[ALTREF_FRAME] |= INTER_ALL;
10661
10662 if (sf->adaptive_mode_search) {
10663 if (cm->show_frame && !cpi->rc.is_src_frame_alt_ref &&
10664 cpi->rc.frames_since_golden >= 3)
Yaowu Xu36bad472017-05-16 18:29:53 -070010665 if ((x->pred_mv_sad[GOLDEN_FRAME] >> 1) > x->pred_mv_sad[LAST_FRAME])
Yaowu Xuc27fc142016-08-22 16:08:15 -070010666 mode_skip_mask[GOLDEN_FRAME] |= INTER_ALL;
10667 }
10668
10669 if (bsize > sf->max_intra_bsize) {
10670 ref_frame_skip_mask[0] |= (1 << INTRA_FRAME);
10671 ref_frame_skip_mask[1] |= (1 << INTRA_FRAME);
10672 }
10673
10674 mode_skip_mask[INTRA_FRAME] |=
10675 ~(sf->intra_y_mode_mask[max_txsize_lookup[bsize]]);
10676
10677 for (i = 0; i <= LAST_NEW_MV_INDEX; ++i) mode_threshold[i] = 0;
10678 for (i = LAST_NEW_MV_INDEX + 1; i < MAX_MODES; ++i)
10679 mode_threshold[i] = ((int64_t)rd_threshes[i] * rd_thresh_freq_fact[i]) >> 5;
10680
10681 midx = sf->schedule_mode_search ? mode_skip_start : 0;
10682 while (midx > 4) {
10683 uint8_t end_pos = 0;
10684 for (i = 5; i < midx; ++i) {
10685 if (mode_threshold[mode_map[i - 1]] > mode_threshold[mode_map[i]]) {
10686 uint8_t tmp = mode_map[i];
10687 mode_map[i] = mode_map[i - 1];
10688 mode_map[i - 1] = tmp;
10689 end_pos = i;
10690 }
10691 }
10692 midx = end_pos;
10693 }
10694
10695 if (cpi->sf.tx_type_search.fast_intra_tx_type_search)
10696 x->use_default_intra_tx_type = 1;
10697 else
10698 x->use_default_intra_tx_type = 0;
10699
10700 if (cpi->sf.tx_type_search.fast_inter_tx_type_search)
10701 x->use_default_inter_tx_type = 1;
10702 else
10703 x->use_default_inter_tx_type = 0;
Yushin Cho77bba8d2016-11-04 16:36:56 -070010704#if CONFIG_PVQ
10705 od_encode_checkpoint(&x->daala_enc, &pre_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -080010706#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -070010707#if CONFIG_EXT_INTER
10708 for (i = 0; i < MB_MODE_COUNT; ++i)
10709 for (ref_frame = 0; ref_frame < TOTAL_REFS_PER_FRAME; ++ref_frame)
10710 modelled_rd[i][ref_frame] = INT64_MAX;
10711#endif // CONFIG_EXT_INTER
10712
10713 for (midx = 0; midx < MAX_MODES; ++midx) {
10714 int mode_index;
10715 int mode_excluded = 0;
10716 int64_t this_rd = INT64_MAX;
10717 int disable_skip = 0;
10718 int compmode_cost = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010719 int rate2 = 0, rate_y = 0, rate_uv = 0;
10720 int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0;
10721 int skippable = 0;
10722 int this_skip2 = 0;
10723 int64_t total_sse = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010724 uint8_t ref_frame_type;
Yushin Cho77bba8d2016-11-04 16:36:56 -070010725#if CONFIG_PVQ
10726 od_encode_rollback(&x->daala_enc, &pre_buf);
Fergus Simpson4063a682017-02-28 16:52:22 -080010727#endif // CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -070010728 mode_index = mode_map[midx];
Yaowu Xuf883b422016-08-30 14:01:10 -070010729 this_mode = av1_mode_order[mode_index].mode;
10730 ref_frame = av1_mode_order[mode_index].ref_frame[0];
10731 second_ref_frame = av1_mode_order[mode_index].ref_frame[1];
Yaowu Xu4306b6e2016-09-27 12:55:32 -070010732 mbmi->ref_mv_idx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010733
10734#if CONFIG_EXT_INTER
10735 if (ref_frame > INTRA_FRAME && second_ref_frame == INTRA_FRAME) {
10736 // Mode must by compatible
Debargha Mukherjee37f6fe62017-02-10 21:44:13 -080010737 if (!is_interintra_allowed_mode(this_mode)) continue;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010738 if (!is_interintra_allowed_bsize(bsize)) continue;
10739 }
10740
10741 if (is_inter_compound_mode(this_mode)) {
10742 frame_mv[this_mode][ref_frame].as_int =
10743 frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int;
10744 frame_mv[this_mode][second_ref_frame].as_int =
10745 frame_mv[compound_ref1_mode(this_mode)][second_ref_frame].as_int;
Zoe Liu85b66462017-04-20 14:28:19 -070010746#if CONFIG_COMPOUND_SINGLEREF
10747 } else if (is_inter_singleref_comp_mode(this_mode)) {
10748 frame_mv[this_mode][ref_frame].as_int =
10749 frame_mv[compound_ref0_mode(this_mode)][ref_frame].as_int;
10750 frame_comp_mv[this_mode][ref_frame].as_int =
10751 frame_mv[compound_ref1_mode(this_mode)][ref_frame].as_int;
10752#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -070010753 }
10754#endif // CONFIG_EXT_INTER
10755
10756 // Look at the reference frame of the best mode so far and set the
10757 // skip mask to look at a subset of the remaining modes.
10758 if (midx == mode_skip_start && best_mode_index >= 0) {
10759 switch (best_mbmode.ref_frame[0]) {
10760 case INTRA_FRAME: break;
10761 case LAST_FRAME:
10762 ref_frame_skip_mask[0] |= LAST_FRAME_MODE_MASK;
10763 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
10764 break;
10765#if CONFIG_EXT_REFS
10766 case LAST2_FRAME:
10767 ref_frame_skip_mask[0] |= LAST2_FRAME_MODE_MASK;
10768 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
10769 break;
10770 case LAST3_FRAME:
10771 ref_frame_skip_mask[0] |= LAST3_FRAME_MODE_MASK;
10772 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
10773 break;
10774#endif // CONFIG_EXT_REFS
10775 case GOLDEN_FRAME:
10776 ref_frame_skip_mask[0] |= GOLDEN_FRAME_MODE_MASK;
10777 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
10778 break;
10779#if CONFIG_EXT_REFS
10780 case BWDREF_FRAME:
10781 ref_frame_skip_mask[0] |= BWDREF_FRAME_MODE_MASK;
10782 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
10783 break;
Zoe Liue9b15e22017-07-19 15:53:01 -070010784 case ALTREF2_FRAME:
10785 ref_frame_skip_mask[0] |= ALTREF2_FRAME_MODE_MASK;
10786 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
10787 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010788#endif // CONFIG_EXT_REFS
10789 case ALTREF_FRAME: ref_frame_skip_mask[0] |= ALTREF_FRAME_MODE_MASK;
10790#if CONFIG_EXT_REFS
10791 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
10792#endif // CONFIG_EXT_REFS
10793 break;
Emil Keyder01770b32017-01-20 18:03:11 -050010794 case NONE_FRAME:
Yaowu Xuc27fc142016-08-22 16:08:15 -070010795 case TOTAL_REFS_PER_FRAME:
10796 assert(0 && "Invalid Reference frame");
10797 break;
10798 }
10799 }
10800
10801 if ((ref_frame_skip_mask[0] & (1 << ref_frame)) &&
Yaowu Xuf883b422016-08-30 14:01:10 -070010802 (ref_frame_skip_mask[1] & (1 << AOMMAX(0, second_ref_frame))))
Yaowu Xuc27fc142016-08-22 16:08:15 -070010803 continue;
10804
Zoe Liuc082bbc2017-05-17 13:31:37 -070010805#if CONFIG_EXT_COMP_REFS
10806// TODO(zoeliu): Following toggle between #if 0/1 and the bug will manifest
10807// itself.
10808#if 0
10809 if (!(cpi->ref_frame_flags & flag_list[ref_frame]) ||
10810 (second_ref_frame > INTRA_FRAME &&
10811 (!(cpi->ref_frame_flags & flag_list[second_ref_frame]))))
10812 printf("Frame=%d, bsize=%d, (mi_row,mi_col)=(%d,%d), ref_frame=%d, "
10813 "second_ref_frame=%d\n", cm->current_video_frame, bsize, mi_row,
10814 mi_col, ref_frame, second_ref_frame);
10815
10816 if (!(cpi->ref_frame_flags & flag_list[ref_frame])) continue;
10817 if (second_ref_frame > INTRA_FRAME &&
10818 (!(cpi->ref_frame_flags & flag_list[second_ref_frame])))
10819 continue;
10820#endif // 0
10821
10822#if !USE_UNI_COMP_REFS
10823 // NOTE(zoeliu): Temporarily disable uni-directional comp refs
10824 if (second_ref_frame > INTRA_FRAME) {
10825 if (!((ref_frame < BWDREF_FRAME) ^ (second_ref_frame < BWDREF_FRAME)))
10826 continue;
10827 }
10828 assert(second_ref_frame <= INTRA_FRAME ||
10829 ((ref_frame < BWDREF_FRAME) ^ (second_ref_frame < BWDREF_FRAME)));
10830#endif // !USE_UNI_COMP_REFS
10831#endif // CONFIG_EXT_COMP_REFS
10832
Yaowu Xuc27fc142016-08-22 16:08:15 -070010833 if (mode_skip_mask[ref_frame] & (1 << this_mode)) continue;
10834
10835 // Test best rd so far against threshold for trying this mode.
10836 if (best_mode_skippable && sf->schedule_mode_search)
10837 mode_threshold[mode_index] <<= 1;
10838
10839 if (best_rd < mode_threshold[mode_index]) continue;
10840
Yunqing Wangff4fa062017-04-21 10:56:08 -070010841 // This is only used in motion vector unit test.
10842 if (cpi->oxcf.motion_vector_unit_test && ref_frame == INTRA_FRAME) continue;
10843
Zoe Liu5a978832017-08-15 16:33:34 -070010844#if CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS // Changes LL bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +020010845#if CONFIG_EXT_REFS
10846 if (cpi->oxcf.pass == 0) {
10847 // Complexity-compression trade-offs
10848 // if (ref_frame == ALTREF_FRAME) continue;
10849 // if (ref_frame == BWDREF_FRAME) continue;
10850 if (second_ref_frame == ALTREF_FRAME) continue;
10851 // if (second_ref_frame == BWDREF_FRAME) continue;
10852 }
Zoe Liu5a978832017-08-15 16:33:34 -070010853#endif // CONFIG_EXT_REFS
10854#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -070010855 comp_pred = second_ref_frame > INTRA_FRAME;
10856 if (comp_pred) {
10857 if (!cpi->allow_comp_inter_inter) continue;
10858
10859 // Skip compound inter modes if ARF is not available.
10860 if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) continue;
10861
10862 // Do not allow compound prediction if the segment level reference frame
10863 // feature is in use as in this case there can only be one reference.
10864 if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) continue;
10865
10866 if ((mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) &&
10867 best_mode_index >= 0 && best_mbmode.ref_frame[0] == INTRA_FRAME)
10868 continue;
10869
10870 mode_excluded = cm->reference_mode == SINGLE_REFERENCE;
10871 } else {
10872 if (ref_frame != INTRA_FRAME)
10873 mode_excluded = cm->reference_mode == COMPOUND_REFERENCE;
10874 }
10875
10876 if (ref_frame == INTRA_FRAME) {
10877 if (sf->adaptive_mode_search)
10878 if ((x->source_variance << num_pels_log2_lookup[bsize]) > best_pred_sse)
10879 continue;
10880
10881 if (this_mode != DC_PRED) {
10882 // Disable intra modes other than DC_PRED for blocks with low variance
10883 // Threshold for intra skipping based on source variance
10884 // TODO(debargha): Specialize the threshold for super block sizes
10885 const unsigned int skip_intra_var_thresh = 64;
10886 if ((mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) &&
10887 x->source_variance < skip_intra_var_thresh)
10888 continue;
10889 // Only search the oblique modes if the best so far is
10890 // one of the neighboring directional modes
10891 if ((mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) &&
10892 (this_mode >= D45_PRED && this_mode <= TM_PRED)) {
10893 if (best_mode_index >= 0 && best_mbmode.ref_frame[0] > INTRA_FRAME)
10894 continue;
10895 }
10896 if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
10897 if (conditional_skipintra(this_mode, best_intra_mode)) continue;
10898 }
10899 }
Sarah Parkere5299862016-08-16 14:57:37 -070010900#if CONFIG_GLOBAL_MOTION
David Barkercf3d0b02016-11-10 10:14:49 +000010901 } else if (cm->global_motion[ref_frame].wmtype == IDENTITY &&
Sarah Parkere5299862016-08-16 14:57:37 -070010902 (!comp_pred ||
David Barkercf3d0b02016-11-10 10:14:49 +000010903 cm->global_motion[second_ref_frame].wmtype == IDENTITY)) {
Sarah Parkere5299862016-08-16 14:57:37 -070010904#else // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070010905 } else {
Sarah Parkere5299862016-08-16 14:57:37 -070010906#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070010907 const MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, second_ref_frame };
Yue Chenb23d00a2017-07-28 17:01:21 -070010908 if (!check_best_zero_mv(cpi, x, mbmi_ext->mode_context,
Sebastien Alaiwane140c502017-04-27 09:52:34 +020010909#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070010910 mbmi_ext->compound_mode_context,
Sebastien Alaiwane140c502017-04-27 09:52:34 +020010911#endif // CONFIG_EXT_INTER
David Barker45390c12017-02-20 14:44:40 +000010912 frame_mv, this_mode, ref_frames, bsize, -1,
10913 mi_row, mi_col))
Yaowu Xuc27fc142016-08-22 16:08:15 -070010914 continue;
10915 }
10916
10917 mbmi->mode = this_mode;
Luc Trudeaud6d9eee2017-07-12 12:36:50 -040010918 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010919 mbmi->ref_frame[0] = ref_frame;
10920 mbmi->ref_frame[1] = second_ref_frame;
10921 pmi->palette_size[0] = 0;
10922 pmi->palette_size[1] = 0;
hui su5db97432016-10-14 16:10:14 -070010923#if CONFIG_FILTER_INTRA
10924 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
10925 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
10926#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010927 // Evaluate all sub-pel filters irrespective of whether we can use
10928 // them for this frame.
Debargha Mukherjee0df711f2017-05-02 16:00:20 -070010929
10930 set_default_interp_filters(mbmi, cm->interp_filter);
10931
Yaowu Xuc27fc142016-08-22 16:08:15 -070010932 mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0;
Yue Chencb60b182016-10-13 15:18:22 -070010933 mbmi->motion_mode = SIMPLE_TRANSLATION;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010934
10935 x->skip = 0;
10936 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
10937
10938 // Select prediction reference frames.
10939 for (i = 0; i < MAX_MB_PLANE; i++) {
10940 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
10941 if (comp_pred) xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
10942 }
10943
Zoe Liu85b66462017-04-20 14:28:19 -070010944#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
10945 // Single ref compound mode
10946 if (!comp_pred && is_inter_singleref_comp_mode(mbmi->mode)) {
10947 xd->block_refs[1] = xd->block_refs[0];
10948 for (i = 0; i < MAX_MB_PLANE; i++)
10949 xd->plane[i].pre[1] = xd->plane[i].pre[0];
10950 }
10951#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
10952
Yue Chen4d26acb2017-05-01 12:28:34 -070010953#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Debargha Mukherjeecb603792016-10-04 13:10:23 -070010954 mbmi->interintra_mode = (INTERINTRA_MODE)(II_DC_PRED - 1);
Yue Chen4d26acb2017-05-01 12:28:34 -070010955#endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010956
10957 if (ref_frame == INTRA_FRAME) {
Angie Chiang0e9a2e92016-11-08 09:45:40 -080010958 RD_STATS rd_stats_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010959 TX_SIZE uv_tx;
10960 struct macroblockd_plane *const pd = &xd->plane[1];
10961#if CONFIG_EXT_INTRA
hui su45dc5972016-12-08 17:42:50 -080010962 is_directional_mode = av1_is_directional_mode(mbmi->mode, bsize);
Joe Young830d4ce2017-05-30 17:48:13 -070010963 if (is_directional_mode && av1_use_angle_delta(bsize)) {
hui su45dc5972016-12-08 17:42:50 -080010964 int rate_dummy;
hui su9a416f52017-01-13 11:37:53 -080010965 int64_t model_rd = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010966 if (!angle_stats_ready) {
10967 const int src_stride = x->plane[0].src.stride;
10968 const uint8_t *src = x->plane[0].src.buf;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010969#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070010970 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
hui su9cc10652017-04-27 17:22:07 -070010971 highbd_angle_estimation(src, src_stride, rows, cols, bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010972 directional_mode_skip_mask);
10973 else
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020010974#endif // CONFIG_HIGHBITDEPTH
hui su9cc10652017-04-27 17:22:07 -070010975 angle_estimation(src, src_stride, rows, cols, bsize,
Yaowu Xuc27fc142016-08-22 16:08:15 -070010976 directional_mode_skip_mask);
10977 angle_stats_ready = 1;
10978 }
10979 if (directional_mode_skip_mask[mbmi->mode]) continue;
hui su45dc5972016-12-08 17:42:50 -080010980 rd_stats_y.rate = INT_MAX;
Yue Chenb0f808b2017-04-26 11:55:14 -070010981 rd_pick_intra_angle_sby(cpi, x, &rate_dummy, &rd_stats_y, bsize,
10982 intra_mode_cost[mbmi->mode], best_rd,
10983 &model_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010984 } else {
10985 mbmi->angle_delta[0] = 0;
Angie Chiang0e9a2e92016-11-08 09:45:40 -080010986 super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -070010987 }
10988#else
Angie Chiang0e9a2e92016-11-08 09:45:40 -080010989 super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd);
hui su45dc5972016-12-08 17:42:50 -080010990#endif // CONFIG_EXT_INTRA
Angie Chiang0e9a2e92016-11-08 09:45:40 -080010991 rate_y = rd_stats_y.rate;
10992 distortion_y = rd_stats_y.dist;
10993 skippable = rd_stats_y.skip;
Yaowu Xuc27fc142016-08-22 16:08:15 -070010994
10995 if (rate_y == INT_MAX) continue;
10996
hui su5db97432016-10-14 16:10:14 -070010997#if CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070010998 if (mbmi->mode == DC_PRED) dc_skipped = 0;
hui su5db97432016-10-14 16:10:14 -070010999#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011000
Debargha Mukherjee2f123402016-08-30 17:43:38 -070011001 uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][pd->subsampling_x]
11002 [pd->subsampling_y];
Yaowu Xuc27fc142016-08-22 16:08:15 -070011003 if (rate_uv_intra[uv_tx] == INT_MAX) {
Luc Trudeau9d4cbb82017-07-27 17:01:32 -040011004 choose_intra_uv_mode(cpi, x, bsize, uv_tx, &rate_uv_intra[uv_tx],
Urvang Joshi368fbc92016-10-17 16:31:34 -070011005 &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx],
11006 &skip_uvs[uv_tx], &mode_uv[uv_tx]);
hui su9bc1d8d2017-03-24 12:36:03 -070011007 if (try_palette) pmi_uv[uv_tx] = *pmi;
Urvang Joshib100db72016-10-12 16:28:56 -070011008
Yaowu Xuc27fc142016-08-22 16:08:15 -070011009#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011010 uv_angle_delta[uv_tx] = mbmi->angle_delta[1];
11011#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -070011012#if CONFIG_FILTER_INTRA
11013 filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info;
11014#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011015 }
11016
11017 rate_uv = rate_uv_tokenonly[uv_tx];
Urvang Joshi368fbc92016-10-17 16:31:34 -070011018 distortion_uv = dist_uvs[uv_tx];
11019 skippable = skippable && skip_uvs[uv_tx];
Yaowu Xuc27fc142016-08-22 16:08:15 -070011020 mbmi->uv_mode = mode_uv[uv_tx];
hui su9bc1d8d2017-03-24 12:36:03 -070011021 if (try_palette) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011022 pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1];
11023 memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
11024 pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE,
11025 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
11026 }
Urvang Joshib100db72016-10-12 16:28:56 -070011027
Yaowu Xuc27fc142016-08-22 16:08:15 -070011028#if CONFIG_EXT_INTRA
11029 mbmi->angle_delta[1] = uv_angle_delta[uv_tx];
Yaowu Xuc27fc142016-08-22 16:08:15 -070011030#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -070011031#if CONFIG_FILTER_INTRA
11032 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
11033 filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1];
11034 if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) {
11035 mbmi->filter_intra_mode_info.filter_intra_mode[1] =
11036 filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1];
11037 }
11038#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011039
Jingning Han36fe3202017-02-20 22:31:49 -080011040#if CONFIG_CB4X4
11041 rate2 = rate_y + intra_mode_cost[mbmi->mode];
11042 if (!x->skip_chroma_rd)
Yue Chenb23d00a2017-07-28 17:01:21 -070011043 rate2 += rate_uv + x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode];
Jingning Han36fe3202017-02-20 22:31:49 -080011044#else
Yaowu Xuc27fc142016-08-22 16:08:15 -070011045 rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv +
Yue Chenb23d00a2017-07-28 17:01:21 -070011046 x->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode];
Fergus Simpson4063a682017-02-28 16:52:22 -080011047#endif // CONFIG_CB4X4
Jingning Han36fe3202017-02-20 22:31:49 -080011048
hui su9bc1d8d2017-03-24 12:36:03 -070011049 if (try_palette && mbmi->mode == DC_PRED) {
Yaowu Xuf883b422016-08-30 14:01:10 -070011050 rate2 += av1_cost_bit(
11051 av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0);
hui su9bc1d8d2017-03-24 12:36:03 -070011052 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070011053
Jingning Hanbf9c6b72016-12-14 14:50:45 -080011054 if (!xd->lossless[mbmi->segment_id] && bsize >= BLOCK_8X8) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011055 // super_block_yrd above includes the cost of the tx_size in the
11056 // tokenonly rate, but for intra blocks, tx_size is always coded
11057 // (prediction granularity), so we account for it in the full rate,
11058 // not the tokenonly rate.
Urvang Joshifeb925f2016-12-05 10:37:29 -080011059 rate_y -= tx_size_cost(cpi, x, bsize, mbmi->tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011060 }
11061#if CONFIG_EXT_INTRA
11062 if (is_directional_mode) {
hui sueda3d762016-12-06 16:58:23 -080011063#if CONFIG_INTRA_INTERP
Yaowu Xuf883b422016-08-30 14:01:10 -070011064 const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd);
hui su0a6731f2017-04-26 15:23:47 -070011065 const int p_angle =
11066 mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xuf883b422016-08-30 14:01:10 -070011067 if (av1_is_intra_filter_switchable(p_angle))
Yue Chenb23d00a2017-07-28 17:01:21 -070011068 rate2 += x->intra_filter_cost[intra_filter_ctx][mbmi->intra_filter];
hui sueda3d762016-12-06 16:58:23 -080011069#endif // CONFIG_INTRA_INTERP
Joe Young830d4ce2017-05-30 17:48:13 -070011070 if (av1_use_angle_delta(bsize)) {
11071 rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1,
11072 MAX_ANGLE_DELTA + mbmi->angle_delta[0]);
11073 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070011074 }
Luc Trudeauf24a35f2017-09-11 11:56:33 -040011075 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize) &&
Joe Young830d4ce2017-05-30 17:48:13 -070011076 av1_use_angle_delta(bsize)) {
hui su0a6731f2017-04-26 15:23:47 -070011077 rate2 += write_uniform_cost(2 * MAX_ANGLE_DELTA + 1,
11078 MAX_ANGLE_DELTA + mbmi->angle_delta[1]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011079 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070011080#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -070011081#if CONFIG_FILTER_INTRA
11082 if (mbmi->mode == DC_PRED) {
11083 rate2 +=
11084 av1_cost_bit(cm->fc->filter_intra_probs[0],
11085 mbmi->filter_intra_mode_info.use_filter_intra_mode[0]);
11086 if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) {
11087 rate2 += write_uniform_cost(
11088 FILTER_INTRA_MODES,
11089 mbmi->filter_intra_mode_info.filter_intra_mode[0]);
11090 }
11091 }
Luc Trudeaud6d9eee2017-07-12 12:36:50 -040011092 if (mbmi->uv_mode == UV_DC_PRED) {
hui su5db97432016-10-14 16:10:14 -070011093 rate2 +=
11094 av1_cost_bit(cpi->common.fc->filter_intra_probs[1],
11095 mbmi->filter_intra_mode_info.use_filter_intra_mode[1]);
11096 if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1])
11097 rate2 += write_uniform_cost(
11098 FILTER_INTRA_MODES,
11099 mbmi->filter_intra_mode_info.filter_intra_mode[1]);
11100 }
11101#endif // CONFIG_FILTER_INTRA
Zoe Liu1157d502017-04-30 07:57:14 -070011102 if (mbmi->mode != DC_PRED && mbmi->mode != TM_PRED)
Yaowu Xuc27fc142016-08-22 16:08:15 -070011103 rate2 += intra_cost_penalty;
11104 distortion2 = distortion_y + distortion_uv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011105 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011106 int_mv backup_ref_mv[2];
11107
Debargha Mukherjee0f248c42017-09-07 12:40:18 -070011108 if (!is_comp_ref_allowed(bsize) && mbmi->ref_frame[1] > INTRA_FRAME)
11109 continue;
Jingning Hanc41a5492017-02-24 11:18:52 -080011110
Yaowu Xuc27fc142016-08-22 16:08:15 -070011111 backup_ref_mv[0] = mbmi_ext->ref_mvs[ref_frame][0];
11112 if (comp_pred) backup_ref_mv[1] = mbmi_ext->ref_mvs[second_ref_frame][0];
Yue Chen4d26acb2017-05-01 12:28:34 -070011113#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011114 if (second_ref_frame == INTRA_FRAME) {
11115 if (best_single_inter_ref != ref_frame) continue;
Debargha Mukherjeecb603792016-10-04 13:10:23 -070011116 mbmi->interintra_mode = intra_to_interintra_mode[best_intra_mode];
hui su5db97432016-10-14 16:10:14 -070011117// TODO(debargha|geza.lore):
11118// Should we use ext_intra modes for interintra?
Yaowu Xuc27fc142016-08-22 16:08:15 -070011119#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011120 mbmi->angle_delta[0] = 0;
11121 mbmi->angle_delta[1] = 0;
hui sueda3d762016-12-06 16:58:23 -080011122#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -070011123 mbmi->intra_filter = INTRA_FILTER_LINEAR;
hui sueda3d762016-12-06 16:58:23 -080011124#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -070011125#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -070011126#if CONFIG_FILTER_INTRA
11127 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
11128 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
11129#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011130 }
Yue Chen4d26acb2017-05-01 12:28:34 -070011131#endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011132 mbmi->ref_mv_idx = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070011133 ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011134
David Barker404b2e82017-03-27 13:07:47 +010011135#if CONFIG_EXT_INTER
11136 if (comp_pred) {
11137 if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) {
David Barker3dfba992017-04-03 16:10:09 +010011138 int ref_mv_idx = 0;
11139 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
11140 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
11141 // mbmi->ref_mv_idx (like NEWMV)
11142 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
11143 ref_mv_idx = 1;
11144
11145 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
11146 int_mv this_mv =
11147 mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
David Barker404b2e82017-03-27 13:07:47 +010011148 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11149 xd->n8_h << MI_SIZE_LOG2, xd);
11150 mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
11151 }
David Barker3dfba992017-04-03 16:10:09 +010011152 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
11153 int_mv this_mv =
11154 mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
David Barker404b2e82017-03-27 13:07:47 +010011155 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11156 xd->n8_h << MI_SIZE_LOG2, xd);
11157 mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv;
11158 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070011159 }
Zoe Liu85b66462017-04-20 14:28:19 -070011160#if CONFIG_COMPOUND_SINGLEREF
11161 } else if (is_inter_singleref_comp_mode(mbmi->mode)) {
11162 if (mbmi_ext->ref_mv_count[ref_frame_type] > 1) {
11163 // TODO(zoeliu): To further investigate which ref_mv_idx should be
11164 // chosen for the mode of SR_NEAR_NEWMV.
11165 int ref_mv_idx = 0;
11166 // Special case: SR_NEAR_NEWMV mode use
11167 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
11168 // mbmi->ref_mv_idx (like NEWMV)
11169 if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1;
11170
11171 if (compound_ref0_mode(mbmi->mode) == NEWMV ||
11172 compound_ref1_mode(mbmi->mode) == NEWMV) {
11173 int_mv this_mv =
11174 mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
11175 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11176 xd->n8_h << MI_SIZE_LOG2, xd);
11177 mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
11178 }
11179 }
11180#endif // CONFIG_COMPOUND_SINGLEREF
David Barker404b2e82017-03-27 13:07:47 +010011181 } else {
11182#endif // CONFIG_EXT_INTER
Zoe Liu1157d502017-04-30 07:57:14 -070011183 if (mbmi->mode == NEWMV && mbmi_ext->ref_mv_count[ref_frame_type] > 1) {
David Barker404b2e82017-03-27 13:07:47 +010011184 int ref;
11185 for (ref = 0; ref < 1 + comp_pred; ++ref) {
11186 int_mv this_mv =
11187 (ref == 0) ? mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv
11188 : mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv;
11189 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11190 xd->n8_h << MI_SIZE_LOG2, xd);
11191 mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv;
11192 }
11193 }
11194#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070011195 }
David Barker404b2e82017-03-27 13:07:47 +010011196#endif // CONFIG_EXT_INTER
Angie Chiang76159122016-11-09 12:13:22 -080011197 {
11198 RD_STATS rd_stats, rd_stats_y, rd_stats_uv;
11199 av1_init_rd_stats(&rd_stats);
11200 rd_stats.rate = rate2;
Fergus Simpson073c6f32017-02-17 12:13:48 -080011201
Zoe Liu7f24e1b2017-03-17 17:42:05 -070011202 // Point to variables that are maintained between loop iterations
11203 args.single_newmv = single_newmv;
Fergus Simpson073c6f32017-02-17 12:13:48 -080011204#if CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -070011205 args.single_newmv_rate = single_newmv_rate;
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080011206 args.modelled_rd = modelled_rd;
Fergus Simpson073c6f32017-02-17 12:13:48 -080011207#endif // CONFIG_EXT_INTER
Fergus Simpson3424c2d2017-03-09 11:48:15 -080011208 this_rd = handle_inter_mode(cpi, x, bsize, &rd_stats, &rd_stats_y,
11209 &rd_stats_uv, &disable_skip, frame_mv,
Zoe Liu85b66462017-04-20 14:28:19 -070011210#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
11211 frame_comp_mv,
11212#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080011213 mi_row, mi_col, &args, best_rd);
Angie Chiang76159122016-11-09 12:13:22 -080011214
11215 rate2 = rd_stats.rate;
11216 skippable = rd_stats.skip;
11217 distortion2 = rd_stats.dist;
11218 total_sse = rd_stats.sse;
11219 rate_y = rd_stats_y.rate;
11220 rate_uv = rd_stats_uv.rate;
11221 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070011222
David Barker404b2e82017-03-27 13:07:47 +010011223// TODO(jingning): This needs some refactoring to improve code quality
11224// and reduce redundant steps.
11225#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -070011226#if CONFIG_COMPOUND_SINGLEREF
11227 if ((have_nearmv_in_inter_mode(mbmi->mode) &&
11228 mbmi_ext->ref_mv_count[ref_frame_type] > 2) ||
11229 ((mbmi->mode == NEWMV || mbmi->mode == SR_NEW_NEWMV ||
11230 mbmi->mode == NEW_NEWMV) &&
Yushin Cho67dda512017-07-25 14:58:22 -070011231 mbmi_ext->ref_mv_count[ref_frame_type] > 1))
Zoe Liu85b66462017-04-20 14:28:19 -070011232#else // !CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +010011233 if ((have_nearmv_in_inter_mode(mbmi->mode) &&
David Barker404b2e82017-03-27 13:07:47 +010011234 mbmi_ext->ref_mv_count[ref_frame_type] > 2) ||
11235 ((mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) &&
Yushin Cho67dda512017-07-25 14:58:22 -070011236 mbmi_ext->ref_mv_count[ref_frame_type] > 1))
Zoe Liu85b66462017-04-20 14:28:19 -070011237#endif // CONFIG_COMPOUND_SINGLEREF
11238#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070011239 if ((mbmi->mode == NEARMV &&
11240 mbmi_ext->ref_mv_count[ref_frame_type] > 2) ||
Yushin Cho67dda512017-07-25 14:58:22 -070011241 (mbmi->mode == NEWMV && mbmi_ext->ref_mv_count[ref_frame_type] > 1))
Zoe Liu85b66462017-04-20 14:28:19 -070011242#endif // CONFIG_EXT_INTER
Yushin Cho67dda512017-07-25 14:58:22 -070011243 {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011244 int_mv backup_mv = frame_mv[NEARMV][ref_frame];
11245 MB_MODE_INFO backup_mbmi = *mbmi;
11246 int backup_skip = x->skip;
11247 int64_t tmp_ref_rd = this_rd;
11248 int ref_idx;
11249
Yue Chen6e601e92016-12-05 18:19:00 -080011250// TODO(jingning): This should be deprecated shortly.
11251#if CONFIG_EXT_INTER
David Barker3dfba992017-04-03 16:10:09 +010011252 int idx_offset = have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0;
Yue Chen6e601e92016-12-05 18:19:00 -080011253#else
Yaowu Xuc27fc142016-08-22 16:08:15 -070011254 int idx_offset = (mbmi->mode == NEARMV) ? 1 : 0;
Fergus Simpson4063a682017-02-28 16:52:22 -080011255#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070011256 int ref_set =
Yaowu Xuf883b422016-08-30 14:01:10 -070011257 AOMMIN(2, mbmi_ext->ref_mv_count[ref_frame_type] - 1 - idx_offset);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011258
11259 uint8_t drl_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -070011260 av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx_offset);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011261 // Dummy
11262 int_mv backup_fmv[2];
11263 backup_fmv[0] = frame_mv[NEWMV][ref_frame];
11264 if (comp_pred) backup_fmv[1] = frame_mv[NEWMV][second_ref_frame];
11265
Yue Chenb23d00a2017-07-28 17:01:21 -070011266 rate2 += (rate2 < INT_MAX ? x->drl_mode_cost0[drl_ctx][0] : 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011267
11268 if (this_rd < INT64_MAX) {
Urvang Joshi70006e42017-06-14 16:08:55 -070011269 if (RDCOST(x->rdmult, rate_y + rate_uv, distortion2) <
11270 RDCOST(x->rdmult, 0, total_sse))
11271 tmp_ref_rd = RDCOST(
11272 x->rdmult, rate2 + av1_cost_bit(av1_get_skip_prob(cm, xd), 0),
11273 distortion2);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011274 else
clang-format4eafefe2017-09-04 12:51:20 -070011275 tmp_ref_rd =
11276 RDCOST(x->rdmult,
11277 rate2 + av1_cost_bit(av1_get_skip_prob(cm, xd), 1) -
11278 rate_y - rate_uv,
11279 total_sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011280 }
11281#if CONFIG_VAR_TX
11282 for (i = 0; i < MAX_MB_PLANE; ++i)
11283 memcpy(x->blk_skip_drl[i], x->blk_skip[i],
11284 sizeof(uint8_t) * ctx->num_4x4_blk);
Fergus Simpson4063a682017-02-28 16:52:22 -080011285#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -070011286
11287 for (ref_idx = 0; ref_idx < ref_set; ++ref_idx) {
11288 int64_t tmp_alt_rd = INT64_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011289 int dummy_disable_skip = 0;
11290 int ref;
11291 int_mv cur_mv;
Angie Chiang76159122016-11-09 12:13:22 -080011292 RD_STATS tmp_rd_stats, tmp_rd_stats_y, tmp_rd_stats_uv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011293
Yaowu Xu5bfbfdf2016-11-22 16:43:34 -080011294 av1_invalid_rd_stats(&tmp_rd_stats);
Yushin Choc0f6bf22017-06-09 16:08:02 -070011295
Jingning Han52617b22017-04-11 12:50:08 -070011296 x->skip = 0;
Yaowu Xu5bfbfdf2016-11-22 16:43:34 -080011297
Yaowu Xuc27fc142016-08-22 16:08:15 -070011298 mbmi->ref_mv_idx = 1 + ref_idx;
11299
David Barker3dfba992017-04-03 16:10:09 +010011300#if CONFIG_EXT_INTER
11301 if (comp_pred) {
11302 int ref_mv_idx = mbmi->ref_mv_idx;
11303 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
11304 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
11305 // mbmi->ref_mv_idx (like NEWMV)
11306 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
11307 ref_mv_idx = 1 + mbmi->ref_mv_idx;
11308
11309 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
11310 int_mv this_mv =
11311 mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
11312 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11313 xd->n8_h << MI_SIZE_LOG2, xd);
11314 mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
11315 } else if (compound_ref0_mode(mbmi->mode) == NEARESTMV) {
11316 int_mv this_mv =
11317 mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv;
11318 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11319 xd->n8_h << MI_SIZE_LOG2, xd);
11320 mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
11321 }
11322
11323 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
11324 int_mv this_mv =
11325 mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
11326 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11327 xd->n8_h << MI_SIZE_LOG2, xd);
11328 mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv;
11329 } else if (compound_ref1_mode(mbmi->mode) == NEARESTMV) {
11330 int_mv this_mv =
11331 mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv;
11332 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11333 xd->n8_h << MI_SIZE_LOG2, xd);
11334 mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv;
11335 }
Zoe Liu85b66462017-04-20 14:28:19 -070011336#if CONFIG_COMPOUND_SINGLEREF
11337 } else if (is_inter_singleref_comp_mode(mbmi->mode)) {
11338 int ref_mv_idx = mbmi->ref_mv_idx;
11339 // Special case: SR_NEAR_NEWMV mode use
11340 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
11341 // mbmi->ref_mv_idx (like NEWMV)
11342 if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx;
11343
11344 // TODO(zoeliu): For the mode of SR_NEAREST_NEWMV, as it only runs
11345 // the "if", not the "else if",
11346 // mbmi_ext->ref_mvs[mbmi->ref_frame[0]] takes the
11347 // value for "NEWMV", instead of "NEARESTMV".
11348 if (compound_ref0_mode(mbmi->mode) == NEWMV ||
11349 compound_ref1_mode(mbmi->mode) == NEWMV) {
11350 int_mv this_mv =
11351 mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
11352 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11353 xd->n8_h << MI_SIZE_LOG2, xd);
11354 mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
11355 } else if (compound_ref0_mode(mbmi->mode) == NEARESTMV ||
11356 compound_ref1_mode(mbmi->mode) == NEARESTMV) {
11357 int_mv this_mv =
11358 mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv;
11359 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11360 xd->n8_h << MI_SIZE_LOG2, xd);
11361 mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
11362 }
11363#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +010011364 } else {
11365#endif // CONFIG_EXT_INTER
11366 for (ref = 0; ref < 1 + comp_pred; ++ref) {
11367 int_mv this_mv =
11368 (ref == 0)
11369 ? mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
11370 .this_mv
11371 : mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
11372 .comp_mv;
11373 clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
11374 xd->n8_h << MI_SIZE_LOG2, xd);
11375 mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv;
11376 }
11377#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070011378 }
David Barker3dfba992017-04-03 16:10:09 +010011379#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070011380
11381 cur_mv =
11382 mbmi_ext->ref_mv_stack[ref_frame][mbmi->ref_mv_idx + idx_offset]
11383 .this_mv;
11384 clamp_mv2(&cur_mv.as_mv, xd);
11385
Alex Converse0fa0f422017-04-24 12:51:14 -070011386 if (!mv_check_bounds(&x->mv_limits, &cur_mv.as_mv)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011387 int_mv dummy_single_newmv[TOTAL_REFS_PER_FRAME] = { { 0 } };
Zoe Liu7f24e1b2017-03-17 17:42:05 -070011388#if CONFIG_EXT_INTER
11389 int dummy_single_newmv_rate[TOTAL_REFS_PER_FRAME] = { 0 };
Fergus Simpson4063a682017-02-28 16:52:22 -080011390#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070011391
11392 frame_mv[NEARMV][ref_frame] = cur_mv;
Angie Chiang76159122016-11-09 12:13:22 -080011393 av1_init_rd_stats(&tmp_rd_stats);
Yushin Cho8e75e8b2017-09-12 16:33:28 -070011394
Zoe Liu7f24e1b2017-03-17 17:42:05 -070011395 // Point to variables that are not maintained between iterations
11396 args.single_newmv = dummy_single_newmv;
Fergus Simpson073c6f32017-02-17 12:13:48 -080011397#if CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -070011398 args.single_newmv_rate = dummy_single_newmv_rate;
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080011399 args.modelled_rd = NULL;
Fergus Simpson073c6f32017-02-17 12:13:48 -080011400#endif // CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -070011401 tmp_alt_rd = handle_inter_mode(cpi, x, bsize, &tmp_rd_stats,
11402 &tmp_rd_stats_y, &tmp_rd_stats_uv,
11403 &dummy_disable_skip, frame_mv,
11404#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
11405 frame_comp_mv,
11406#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
11407 mi_row, mi_col, &args, best_rd);
Zoe Liu7f24e1b2017-03-17 17:42:05 -070011408 // Prevent pointers from escaping local scope
11409 args.single_newmv = NULL;
Fergus Simpson073c6f32017-02-17 12:13:48 -080011410#if CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -070011411 args.single_newmv_rate = NULL;
Fergus Simpson073c6f32017-02-17 12:13:48 -080011412#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070011413 }
11414
11415 for (i = 0; i < mbmi->ref_mv_idx; ++i) {
11416 uint8_t drl1_ctx = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070011417 drl1_ctx = av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type],
11418 i + idx_offset);
Angie Chiang76159122016-11-09 12:13:22 -080011419 tmp_rd_stats.rate +=
Yue Chenb23d00a2017-07-28 17:01:21 -070011420 (tmp_rd_stats.rate < INT_MAX ? x->drl_mode_cost0[drl1_ctx][1]
Angie Chiang76159122016-11-09 12:13:22 -080011421 : 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011422 }
11423
11424 if (mbmi_ext->ref_mv_count[ref_frame_type] >
11425 mbmi->ref_mv_idx + idx_offset + 1 &&
11426 ref_idx < ref_set - 1) {
11427 uint8_t drl1_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -070011428 av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type],
11429 mbmi->ref_mv_idx + idx_offset);
Yaowu Xu83ed6fe2016-11-22 11:15:29 -080011430 tmp_rd_stats.rate +=
Yue Chenb23d00a2017-07-28 17:01:21 -070011431 (tmp_rd_stats.rate < INT_MAX ? x->drl_mode_cost0[drl1_ctx][0]
Yaowu Xu83ed6fe2016-11-22 11:15:29 -080011432 : 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011433 }
11434
11435 if (tmp_alt_rd < INT64_MAX) {
Yue Chen69f18e12016-09-08 14:48:15 -070011436#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Urvang Joshi70006e42017-06-14 16:08:55 -070011437 tmp_alt_rd =
11438 RDCOST(x->rdmult, tmp_rd_stats.rate, tmp_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011439#else
Urvang Joshi70006e42017-06-14 16:08:55 -070011440 if (RDCOST(x->rdmult, tmp_rd_stats_y.rate + tmp_rd_stats_uv.rate,
Angie Chiang76159122016-11-09 12:13:22 -080011441 tmp_rd_stats.dist) <
Urvang Joshi70006e42017-06-14 16:08:55 -070011442 RDCOST(x->rdmult, 0, tmp_rd_stats.sse))
clang-format4eafefe2017-09-04 12:51:20 -070011443 tmp_alt_rd =
11444 RDCOST(x->rdmult,
11445 tmp_rd_stats.rate +
11446 av1_cost_bit(av1_get_skip_prob(cm, xd), 0),
11447 tmp_rd_stats.dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011448 else
clang-format4eafefe2017-09-04 12:51:20 -070011449 tmp_alt_rd =
11450 RDCOST(x->rdmult,
11451 tmp_rd_stats.rate +
11452 av1_cost_bit(av1_get_skip_prob(cm, xd), 1) -
11453 tmp_rd_stats_y.rate - tmp_rd_stats_uv.rate,
11454 tmp_rd_stats.sse);
Yue Chen69f18e12016-09-08 14:48:15 -070011455#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070011456 }
11457
11458 if (tmp_ref_rd > tmp_alt_rd) {
Angie Chiang76159122016-11-09 12:13:22 -080011459 rate2 = tmp_rd_stats.rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011460 disable_skip = dummy_disable_skip;
Angie Chiang76159122016-11-09 12:13:22 -080011461 distortion2 = tmp_rd_stats.dist;
11462 skippable = tmp_rd_stats.skip;
11463 rate_y = tmp_rd_stats_y.rate;
11464 rate_uv = tmp_rd_stats_uv.rate;
11465 total_sse = tmp_rd_stats.sse;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011466 this_rd = tmp_alt_rd;
11467 tmp_ref_rd = tmp_alt_rd;
11468 backup_mbmi = *mbmi;
11469 backup_skip = x->skip;
11470#if CONFIG_VAR_TX
11471 for (i = 0; i < MAX_MB_PLANE; ++i)
11472 memcpy(x->blk_skip_drl[i], x->blk_skip[i],
11473 sizeof(uint8_t) * ctx->num_4x4_blk);
Fergus Simpson4063a682017-02-28 16:52:22 -080011474#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -070011475 } else {
11476 *mbmi = backup_mbmi;
11477 x->skip = backup_skip;
11478 }
11479 }
11480
11481 frame_mv[NEARMV][ref_frame] = backup_mv;
11482 frame_mv[NEWMV][ref_frame] = backup_fmv[0];
11483 if (comp_pred) frame_mv[NEWMV][second_ref_frame] = backup_fmv[1];
11484#if CONFIG_VAR_TX
11485 for (i = 0; i < MAX_MB_PLANE; ++i)
11486 memcpy(x->blk_skip[i], x->blk_skip_drl[i],
11487 sizeof(uint8_t) * ctx->num_4x4_blk);
Fergus Simpson4063a682017-02-28 16:52:22 -080011488#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -070011489 }
11490 mbmi_ext->ref_mvs[ref_frame][0] = backup_ref_mv[0];
11491 if (comp_pred) mbmi_ext->ref_mvs[second_ref_frame][0] = backup_ref_mv[1];
Yaowu Xuc27fc142016-08-22 16:08:15 -070011492
11493 if (this_rd == INT64_MAX) continue;
11494
Debargha Mukherjee0f248c42017-09-07 12:40:18 -070011495 if (is_comp_ref_allowed(mbmi->sb_type))
Jingning Hanc41a5492017-02-24 11:18:52 -080011496 compmode_cost = av1_cost_bit(comp_mode_p, comp_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011497
11498 if (cm->reference_mode == REFERENCE_MODE_SELECT) rate2 += compmode_cost;
11499 }
11500
Yaowu Xuc27fc142016-08-22 16:08:15 -070011501 // Estimate the reference frame signaling cost and add it
11502 // to the rolling cost variable.
11503 if (comp_pred) {
Zoe Liuc082bbc2017-05-17 13:31:37 -070011504#if CONFIG_EXT_COMP_REFS
11505 rate2 += ref_costs_comp[ref_frame][second_ref_frame];
11506#else // !CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -070011507 rate2 += ref_costs_comp[ref_frame];
11508#if CONFIG_EXT_REFS
11509 rate2 += ref_costs_comp[second_ref_frame];
11510#endif // CONFIG_EXT_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -070011511#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -070011512 } else {
11513 rate2 += ref_costs_single[ref_frame];
11514 }
11515
Zoe Liu85b66462017-04-20 14:28:19 -070011516#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
11517 // Add the cost to signal single/comp mode in single ref.
11518 if (!comp_pred && cm->reference_mode != COMPOUND_REFERENCE) {
11519 aom_prob singleref_comp_mode_p = av1_get_inter_mode_prob(cm, xd);
11520 rate2 += av1_cost_bit(singleref_comp_mode_p,
11521 is_inter_singleref_comp_mode(mbmi->mode));
11522 }
11523#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
11524
Yue Chen69f18e12016-09-08 14:48:15 -070011525#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yushin Cho67dda512017-07-25 14:58:22 -070011526 if (ref_frame == INTRA_FRAME)
Yaowu Xuc27fc142016-08-22 16:08:15 -070011527#else
Yushin Cho67dda512017-07-25 14:58:22 -070011528 if (!disable_skip)
Yue Chen69f18e12016-09-08 14:48:15 -070011529#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yushin Cho67dda512017-07-25 14:58:22 -070011530 {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011531 if (skippable) {
11532 // Back out the coefficient coding costs
11533 rate2 -= (rate_y + rate_uv);
11534 rate_y = 0;
11535 rate_uv = 0;
11536 // Cost the skip mb case
Yaowu Xuf883b422016-08-30 14:01:10 -070011537 rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011538 } else if (ref_frame != INTRA_FRAME && !xd->lossless[mbmi->segment_id]) {
Urvang Joshi70006e42017-06-14 16:08:55 -070011539 if (RDCOST(x->rdmult, rate_y + rate_uv + rate_skip0, distortion2) <
11540 RDCOST(x->rdmult, rate_skip1, total_sse)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011541 // Add in the cost of the no skip flag.
Yaowu Xuf883b422016-08-30 14:01:10 -070011542 rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011543 } else {
11544 // FIXME(rbultje) make this work for splitmv also
Yaowu Xuf883b422016-08-30 14:01:10 -070011545 rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011546 distortion2 = total_sse;
11547 assert(total_sse >= 0);
11548 rate2 -= (rate_y + rate_uv);
11549 this_skip2 = 1;
11550 rate_y = 0;
11551 rate_uv = 0;
11552 }
11553 } else {
11554 // Add in the cost of the no skip flag.
Yaowu Xuf883b422016-08-30 14:01:10 -070011555 rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011556 }
11557
11558 // Calculate the final RD estimate for this mode.
Urvang Joshi70006e42017-06-14 16:08:55 -070011559 this_rd = RDCOST(x->rdmult, rate2, distortion2);
Yue Chen69f18e12016-09-08 14:48:15 -070011560#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070011561 } else {
11562 this_skip2 = mbmi->skip;
Urvang Joshi70006e42017-06-14 16:08:55 -070011563 this_rd = RDCOST(x->rdmult, rate2, distortion2);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011564 if (this_skip2) {
11565 rate_y = 0;
11566 rate_uv = 0;
11567 }
Yue Chen69f18e12016-09-08 14:48:15 -070011568#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070011569 }
11570
Yaowu Xuc27fc142016-08-22 16:08:15 -070011571 if (ref_frame == INTRA_FRAME) {
11572 // Keep record of best intra rd
11573 if (this_rd < best_intra_rd) {
11574 best_intra_rd = this_rd;
11575 best_intra_mode = mbmi->mode;
11576 }
Yue Chen4d26acb2017-05-01 12:28:34 -070011577#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Emil Keyder01770b32017-01-20 18:03:11 -050011578 } else if (second_ref_frame == NONE_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011579 if (this_rd < best_single_inter_rd) {
11580 best_single_inter_rd = this_rd;
11581 best_single_inter_ref = mbmi->ref_frame[0];
11582 }
Yue Chen4d26acb2017-05-01 12:28:34 -070011583#endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011584 }
11585
11586 if (!disable_skip && ref_frame == INTRA_FRAME) {
11587 for (i = 0; i < REFERENCE_MODES; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -070011588 best_pred_rd[i] = AOMMIN(best_pred_rd[i], this_rd);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011589 }
11590
11591 // Did this mode help.. i.e. is it the new best mode
11592 if (this_rd < best_rd || x->skip) {
11593 if (!mode_excluded) {
11594 // Note index of best mode so far
11595 best_mode_index = mode_index;
11596
11597 if (ref_frame == INTRA_FRAME) {
11598 /* required for left and above block mv */
11599 mbmi->mv[0].as_int = 0;
11600 } else {
11601 best_pred_sse = x->pred_sse[ref_frame];
11602 }
11603
11604 rd_cost->rate = rate2;
11605#if CONFIG_SUPERTX
11606 if (x->skip)
11607 *returnrate_nocoef = rate2;
11608 else
11609 *returnrate_nocoef = rate2 - rate_y - rate_uv;
Yaowu Xuf883b422016-08-30 14:01:10 -070011610 *returnrate_nocoef -= av1_cost_bit(
11611 av1_get_skip_prob(cm, xd), disable_skip || skippable || this_skip2);
11612 *returnrate_nocoef -= av1_cost_bit(av1_get_intra_inter_prob(cm, xd),
11613 mbmi->ref_frame[0] != INTRA_FRAME);
Yue Chencb60b182016-10-13 15:18:22 -070011614#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yue Chen52c51732017-07-11 15:08:30 -070011615#if CONFIG_WARPED_MOTION
11616 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
11617#endif
Yaowu Xu41a36de2017-03-23 15:55:03 -070011618#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
11619 MODE_INFO *const mi = xd->mi[0];
Sarah Parker19234cc2017-03-10 16:43:25 -080011620 const MOTION_MODE motion_allowed = motion_mode_allowed(
Sarah Parker0eea89f2017-07-11 11:56:36 -070011621#if CONFIG_GLOBAL_MOTION
Sarah Parker19234cc2017-03-10 16:43:25 -080011622 0, xd->global_motion,
Sarah Parker0eea89f2017-07-11 11:56:36 -070011623#endif // CONFIG_GLOBAL_MOTION
Yue Chen52c51732017-07-11 15:08:30 -070011624#if CONFIG_WARPED_MOTION
11625 xd,
11626#endif
Sarah Parker19234cc2017-03-10 16:43:25 -080011627 mi);
Sarah Parker19234cc2017-03-10 16:43:25 -080011628 if (motion_allowed == WARPED_CAUSAL)
Yue Chenbdc8dab2017-07-26 12:05:47 -070011629 *returnrate_nocoef -= x->motion_mode_cost[bsize][mbmi->motion_mode];
Sarah Parker19234cc2017-03-10 16:43:25 -080011630 else if (motion_allowed == OBMC_CAUSAL)
Yue Chenb23d00a2017-07-28 17:01:21 -070011631 *returnrate_nocoef -= x->motion_mode_cost1[bsize][mbmi->motion_mode];
Yaowu Xu41a36de2017-03-23 15:55:03 -070011632#else
Yue Chenbdc8dab2017-07-26 12:05:47 -070011633 *returnrate_nocoef -= x->motion_mode_cost[bsize][mbmi->motion_mode];
Yue Chen69f18e12016-09-08 14:48:15 -070011634#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -070011635#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070011636#endif // CONFIG_SUPERTX
11637 rd_cost->dist = distortion2;
11638 rd_cost->rdcost = this_rd;
11639 best_rd = this_rd;
11640 best_mbmode = *mbmi;
11641 best_skip2 = this_skip2;
11642 best_mode_skippable = skippable;
Yaowu Xuf883b422016-08-30 14:01:10 -070011643 best_rate_y = rate_y + av1_cost_bit(av1_get_skip_prob(cm, xd),
11644 this_skip2 || skippable);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011645 best_rate_uv = rate_uv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011646#if CONFIG_VAR_TX
11647 for (i = 0; i < MAX_MB_PLANE; ++i)
11648 memcpy(ctx->blk_skip[i], x->blk_skip[i],
11649 sizeof(uint8_t) * ctx->num_4x4_blk);
Fergus Simpson4063a682017-02-28 16:52:22 -080011650#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -070011651 }
11652 }
Yushin Cho8e75e8b2017-09-12 16:33:28 -070011653
Yaowu Xuc27fc142016-08-22 16:08:15 -070011654 /* keep record of best compound/single-only prediction */
11655 if (!disable_skip && ref_frame != INTRA_FRAME) {
11656 int64_t single_rd, hybrid_rd, single_rate, hybrid_rate;
11657
11658 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
11659 single_rate = rate2 - compmode_cost;
11660 hybrid_rate = rate2;
11661 } else {
11662 single_rate = rate2;
11663 hybrid_rate = rate2 + compmode_cost;
11664 }
11665
Urvang Joshi70006e42017-06-14 16:08:55 -070011666 single_rd = RDCOST(x->rdmult, single_rate, distortion2);
11667 hybrid_rd = RDCOST(x->rdmult, hybrid_rate, distortion2);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011668
11669 if (!comp_pred) {
11670 if (single_rd < best_pred_rd[SINGLE_REFERENCE])
11671 best_pred_rd[SINGLE_REFERENCE] = single_rd;
11672 } else {
11673 if (single_rd < best_pred_rd[COMPOUND_REFERENCE])
11674 best_pred_rd[COMPOUND_REFERENCE] = single_rd;
11675 }
11676 if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT])
11677 best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd;
11678 }
11679
Yaowu Xuc27fc142016-08-22 16:08:15 -070011680 if (x->skip && !comp_pred) break;
11681 }
11682
11683 if (xd->lossless[mbmi->segment_id] == 0 && best_mode_index >= 0 &&
11684 ((sf->tx_type_search.fast_inter_tx_type_search == 1 &&
11685 is_inter_mode(best_mbmode.mode)) ||
11686 (sf->tx_type_search.fast_intra_tx_type_search == 1 &&
11687 !is_inter_mode(best_mbmode.mode)))) {
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011688 int skip_blk = 0;
11689 RD_STATS rd_stats_y, rd_stats_uv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011690
11691 x->use_default_inter_tx_type = 0;
11692 x->use_default_intra_tx_type = 0;
11693
11694 *mbmi = best_mbmode;
11695
11696 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
11697
11698 // Select prediction reference frames.
11699 for (i = 0; i < MAX_MB_PLANE; i++) {
11700 xd->plane[i].pre[0] = yv12_mb[mbmi->ref_frame[0]][i];
11701 if (has_second_ref(mbmi))
11702 xd->plane[i].pre[1] = yv12_mb[mbmi->ref_frame[1]][i];
11703 }
11704
Zoe Liu85b66462017-04-20 14:28:19 -070011705#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
11706 // Single ref compound mode
11707 if (!has_second_ref(mbmi) && is_inter_singleref_comp_mode(mbmi->mode)) {
11708 xd->block_refs[1] = xd->block_refs[0];
11709 for (i = 0; i < MAX_MB_PLANE; i++)
11710 xd->plane[i].pre[1] = xd->plane[i].pre[0];
11711 }
11712#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
11713
Yaowu Xuc27fc142016-08-22 16:08:15 -070011714 if (is_inter_mode(mbmi->mode)) {
Jingning Hanc44009c2017-05-06 11:36:49 -070011715 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
Yue Chencb60b182016-10-13 15:18:22 -070011716#if CONFIG_MOTION_VAR
Sarah Parker19234cc2017-03-10 16:43:25 -080011717 if (mbmi->motion_mode == OBMC_CAUSAL) {
Fergus Simpson073c6f32017-02-17 12:13:48 -080011718 av1_build_obmc_inter_prediction(
Fergus Simpson9f7ca0b2017-03-10 10:46:46 -080011719 cm, xd, mi_row, mi_col, args.above_pred_buf, args.above_pred_stride,
11720 args.left_pred_buf, args.left_pred_stride);
Sarah Parker19234cc2017-03-10 16:43:25 -080011721 }
Yue Chencb60b182016-10-13 15:18:22 -070011722#endif // CONFIG_MOTION_VAR
Yaowu Xuf883b422016-08-30 14:01:10 -070011723 av1_subtract_plane(x, bsize, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011724#if CONFIG_VAR_TX
11725 if (cm->tx_mode == TX_MODE_SELECT || xd->lossless[mbmi->segment_id]) {
Angie Chiangb5dda482016-11-02 16:19:58 -070011726 select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
Hui Su1ddf2312017-08-19 15:21:34 -070011727 assert(rd_stats_y.rate != INT_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011728 } else {
11729 int idx, idy;
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011730 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011731 for (idy = 0; idy < xd->n8_h; ++idy)
11732 for (idx = 0; idx < xd->n8_w; ++idx)
11733 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011734 memset(x->blk_skip[0], rd_stats_y.skip,
Yaowu Xuc27fc142016-08-22 16:08:15 -070011735 sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4);
11736 }
11737
Angie Chiangb5dda482016-11-02 16:19:58 -070011738 inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011739#else
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011740 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
Angie Chiang284d7772016-11-08 11:06:45 -080011741 super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011742#endif // CONFIG_VAR_TX
11743 } else {
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011744 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
Angie Chiang284d7772016-11-08 11:06:45 -080011745 super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011746 }
11747
Urvang Joshi70006e42017-06-14 16:08:55 -070011748 if (RDCOST(x->rdmult, rd_stats_y.rate + rd_stats_uv.rate,
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011749 (rd_stats_y.dist + rd_stats_uv.dist)) >
Urvang Joshi70006e42017-06-14 16:08:55 -070011750 RDCOST(x->rdmult, 0, (rd_stats_y.sse + rd_stats_uv.sse))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011751 skip_blk = 1;
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011752 rd_stats_y.rate = av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
11753 rd_stats_uv.rate = 0;
11754 rd_stats_y.dist = rd_stats_y.sse;
11755 rd_stats_uv.dist = rd_stats_uv.sse;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011756 } else {
11757 skip_blk = 0;
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011758 rd_stats_y.rate += av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011759 }
11760
Urvang Joshi70006e42017-06-14 16:08:55 -070011761 if (RDCOST(x->rdmult, best_rate_y + best_rate_uv, rd_cost->dist) >
11762 RDCOST(x->rdmult, rd_stats_y.rate + rd_stats_uv.rate,
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011763 (rd_stats_y.dist + rd_stats_uv.dist))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011764#if CONFIG_VAR_TX
11765 int idx, idy;
Fergus Simpson4063a682017-02-28 16:52:22 -080011766#endif // CONFIG_VAR_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -070011767 best_mbmode.tx_type = mbmi->tx_type;
11768 best_mbmode.tx_size = mbmi->tx_size;
11769#if CONFIG_VAR_TX
11770 for (idy = 0; idy < xd->n8_h; ++idy)
11771 for (idx = 0; idx < xd->n8_w; ++idx)
11772 best_mbmode.inter_tx_size[idy][idx] = mbmi->inter_tx_size[idy][idx];
11773
11774 for (i = 0; i < MAX_MB_PLANE; ++i)
11775 memcpy(ctx->blk_skip[i], x->blk_skip[i],
11776 sizeof(uint8_t) * ctx->num_4x4_blk);
Jingning Hane67b38a2016-11-04 10:30:00 -070011777
11778 best_mbmode.min_tx_size = mbmi->min_tx_size;
Fergus Simpson4063a682017-02-28 16:52:22 -080011779#endif // CONFIG_VAR_TX
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011780 rd_cost->rate +=
11781 (rd_stats_y.rate + rd_stats_uv.rate - best_rate_y - best_rate_uv);
11782 rd_cost->dist = rd_stats_y.dist + rd_stats_uv.dist;
Urvang Joshi70006e42017-06-14 16:08:55 -070011783 rd_cost->rdcost = RDCOST(x->rdmult, rd_cost->rate, rd_cost->dist);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011784 best_skip2 = skip_blk;
11785 }
11786 }
11787
11788 // Only try palette mode when the best mode so far is an intra mode.
hui su9bc1d8d2017-03-24 12:36:03 -070011789 if (try_palette && !is_inter_mode(best_mbmode.mode)) {
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011790 int rate2 = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011791#if CONFIG_SUPERTX
11792 int best_rate_nocoef;
Fergus Simpson4063a682017-02-28 16:52:22 -080011793#endif // CONFIG_SUPERTX
Urvang Joshi451e0f22017-01-31 11:18:31 -080011794 int64_t distortion2 = 0, best_rd_palette = best_rd, this_rd,
11795 best_model_rd_palette = INT64_MAX;
Urvang Joshi626591d2016-10-24 14:13:55 -070011796 int skippable = 0, rate_overhead_palette = 0;
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011797 RD_STATS rd_stats_y;
hui sude0c70a2017-01-09 17:12:17 -080011798 TX_SIZE uv_tx;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011799 uint8_t *const best_palette_color_map =
11800 x->palette_buffer->best_palette_color_map;
11801 uint8_t *const color_map = xd->plane[0].color_index_map;
Urvang Joshi451e0f22017-01-31 11:18:31 -080011802 MB_MODE_INFO best_mbmi_palette = best_mbmode;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011803
11804 mbmi->mode = DC_PRED;
Luc Trudeaud6d9eee2017-07-12 12:36:50 -040011805 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -070011806 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -050011807 mbmi->ref_frame[1] = NONE_FRAME;
Urvang Joshi626591d2016-10-24 14:13:55 -070011808 rate_overhead_palette = rd_pick_palette_intra_sby(
Urvang Joshi451e0f22017-01-31 11:18:31 -080011809 cpi, x, bsize, palette_ctx, intra_mode_cost[DC_PRED],
11810 &best_mbmi_palette, best_palette_color_map, &best_rd_palette,
11811 &best_model_rd_palette, NULL, NULL, NULL, NULL);
hui sude0c70a2017-01-09 17:12:17 -080011812 if (pmi->palette_size[0] == 0) goto PALETTE_EXIT;
11813 memcpy(color_map, best_palette_color_map,
11814 rows * cols * sizeof(best_palette_color_map[0]));
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011815 super_block_yrd(cpi, x, &rd_stats_y, bsize, best_rd);
11816 if (rd_stats_y.rate == INT_MAX) goto PALETTE_EXIT;
Debargha Mukherjee2f123402016-08-30 17:43:38 -070011817 uv_tx = uv_txsize_lookup[bsize][mbmi->tx_size][xd->plane[1].subsampling_x]
11818 [xd->plane[1].subsampling_y];
Yaowu Xuc27fc142016-08-22 16:08:15 -070011819 if (rate_uv_intra[uv_tx] == INT_MAX) {
Luc Trudeau9d4cbb82017-07-27 17:01:32 -040011820 choose_intra_uv_mode(cpi, x, bsize, uv_tx, &rate_uv_intra[uv_tx],
Urvang Joshi368fbc92016-10-17 16:31:34 -070011821 &rate_uv_tokenonly[uv_tx], &dist_uvs[uv_tx],
11822 &skip_uvs[uv_tx], &mode_uv[uv_tx]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011823 pmi_uv[uv_tx] = *pmi;
11824#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011825 uv_angle_delta[uv_tx] = mbmi->angle_delta[1];
11826#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -070011827#if CONFIG_FILTER_INTRA
11828 filter_intra_mode_info_uv[uv_tx] = mbmi->filter_intra_mode_info;
11829#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011830 }
11831 mbmi->uv_mode = mode_uv[uv_tx];
11832 pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1];
hui sude0c70a2017-01-09 17:12:17 -080011833 if (pmi->palette_size[1] > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011834 memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
11835 pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE,
11836 2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
hui sude0c70a2017-01-09 17:12:17 -080011837 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070011838#if CONFIG_EXT_INTRA
11839 mbmi->angle_delta[1] = uv_angle_delta[uv_tx];
Yaowu Xuc27fc142016-08-22 16:08:15 -070011840#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -070011841#if CONFIG_FILTER_INTRA
11842 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] =
11843 filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1];
11844 if (filter_intra_mode_info_uv[uv_tx].use_filter_intra_mode[1]) {
11845 mbmi->filter_intra_mode_info.filter_intra_mode[1] =
11846 filter_intra_mode_info_uv[uv_tx].filter_intra_mode[1];
11847 }
11848#endif // CONFIG_FILTER_INTRA
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011849 skippable = rd_stats_y.skip && skip_uvs[uv_tx];
11850 distortion2 = rd_stats_y.dist + dist_uvs[uv_tx];
11851 rate2 = rd_stats_y.rate + rate_overhead_palette + rate_uv_intra[uv_tx];
Yaowu Xuc27fc142016-08-22 16:08:15 -070011852 rate2 += ref_costs_single[INTRA_FRAME];
11853
11854 if (skippable) {
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011855 rate2 -= (rd_stats_y.rate + rate_uv_tokenonly[uv_tx]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011856#if CONFIG_SUPERTX
11857 best_rate_nocoef = rate2;
Fergus Simpson4063a682017-02-28 16:52:22 -080011858#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -070011859 rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011860 } else {
11861#if CONFIG_SUPERTX
Angie Chiang0e9a2e92016-11-08 09:45:40 -080011862 best_rate_nocoef = rate2 - (rd_stats_y.rate + rate_uv_tokenonly[uv_tx]);
Fergus Simpson4063a682017-02-28 16:52:22 -080011863#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -070011864 rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011865 }
Urvang Joshi70006e42017-06-14 16:08:55 -070011866 this_rd = RDCOST(x->rdmult, rate2, distortion2);
Yaowu Xuc27fc142016-08-22 16:08:15 -070011867 if (this_rd < best_rd) {
11868 best_mode_index = 3;
11869 mbmi->mv[0].as_int = 0;
11870 rd_cost->rate = rate2;
11871#if CONFIG_SUPERTX
11872 *returnrate_nocoef = best_rate_nocoef;
Fergus Simpson4063a682017-02-28 16:52:22 -080011873#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -070011874 rd_cost->dist = distortion2;
11875 rd_cost->rdcost = this_rd;
11876 best_rd = this_rd;
11877 best_mbmode = *mbmi;
11878 best_skip2 = 0;
11879 best_mode_skippable = skippable;
11880 }
11881 }
11882PALETTE_EXIT:
11883
hui su5db97432016-10-14 16:10:14 -070011884#if CONFIG_FILTER_INTRA
11885 // TODO(huisu): filter-intra is turned off in lossless mode for now to
Yaowu Xuc27fc142016-08-22 16:08:15 -070011886 // avoid a unit test failure
Urvang Joshic6300aa2017-06-01 14:46:23 -070011887 if (!xd->lossless[mbmi->segment_id] && pmi->palette_size[0] == 0 &&
Urvang Joshib100db72016-10-12 16:28:56 -070011888 !dc_skipped && best_mode_index >= 0 &&
11889 best_intra_rd < (best_rd + (best_rd >> 3))) {
hui su5db97432016-10-14 16:10:14 -070011890 pick_filter_intra_interframe(
Luc Trudeau9d4cbb82017-07-27 17:01:32 -040011891 cpi, x, bsize, mi_row, mi_col, rate_uv_intra, rate_uv_tokenonly,
Jingning Han18c53c82017-02-17 14:49:57 -080011892 dist_uvs, skip_uvs, mode_uv, filter_intra_mode_info_uv,
hui su5db97432016-10-14 16:10:14 -070011893#if CONFIG_EXT_INTRA
11894 uv_angle_delta,
11895#endif // CONFIG_EXT_INTRA
Urvang Joshic6300aa2017-06-01 14:46:23 -070011896 pmi_uv, palette_ctx, 0, ref_costs_single, &best_rd, &best_intra_rd,
11897 &best_intra_mode, &best_mode_index, &best_skip2, &best_mode_skippable,
Yaowu Xuc27fc142016-08-22 16:08:15 -070011898#if CONFIG_SUPERTX
11899 returnrate_nocoef,
11900#endif // CONFIG_SUPERTX
11901 best_pred_rd, &best_mbmode, rd_cost);
11902 }
hui su5db97432016-10-14 16:10:14 -070011903#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070011904
Zoe Liu85b66462017-04-20 14:28:19 -070011905// The inter modes' rate costs are not calculated precisely in some cases.
11906// Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and
11907// ZEROMV. Here, checks are added for those cases, and the mode decisions
11908// are corrected.
11909#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
11910// NOTE: For SR_NEW_NEWMV, no need to check as the two mvs from the same ref
11911// are surely different from each other.
11912#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -070011913 if (best_mbmode.mode == NEWMV
11914#if CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -070011915 || best_mbmode.mode == NEW_NEWMV
Yaowu Xuc27fc142016-08-22 16:08:15 -070011916#endif // CONFIG_EXT_INTER
11917 ) {
11918 const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0],
11919 best_mbmode.ref_frame[1] };
11920 int comp_pred_mode = refs[1] > INTRA_FRAME;
Sarah Parkere5299862016-08-16 14:57:37 -070011921 int_mv zeromv[2];
Yaowu Xuf883b422016-08-30 14:01:10 -070011922 const uint8_t rf_type = av1_ref_frame_type(best_mbmode.ref_frame);
Sarah Parkere5299862016-08-16 14:57:37 -070011923#if CONFIG_GLOBAL_MOTION
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -080011924 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]],
11925 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -070011926 mi_col, mi_row, 0
11927#if CONFIG_AMVR
11928 ,
11929 cm->cur_frame_mv_precision_level
11930#endif
11931 )
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -080011932 .as_int;
RogerZhou3b635242017-09-19 10:06:46 -070011933 zeromv[1].as_int =
11934 comp_pred_mode
11935 ? gm_get_motion_vector(&cm->global_motion[refs[1]],
11936 cm->allow_high_precision_mv, bsize, mi_col,
11937 mi_row, 0
11938#if CONFIG_AMVR
11939 ,
11940 cm->cur_frame_mv_precision_level
11941#endif
11942 )
11943 .as_int
11944 : 0;
Sarah Parkere5299862016-08-16 14:57:37 -070011945#else
11946 zeromv[0].as_int = 0;
11947 zeromv[1].as_int = 0;
11948#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070011949 if (!comp_pred_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070011950 int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2)
Yaowu Xuf883b422016-08-30 14:01:10 -070011951 ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2)
Yaowu Xuc27fc142016-08-22 16:08:15 -070011952 : INT_MAX;
11953
11954 for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) {
11955 int_mv cur_mv = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv;
11956 if (cur_mv.as_int == best_mbmode.mv[0].as_int) {
11957 best_mbmode.mode = NEARMV;
11958 best_mbmode.ref_mv_idx = i;
11959 }
11960 }
11961
11962 if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int)
11963 best_mbmode.mode = NEARESTMV;
Sarah Parkere5299862016-08-16 14:57:37 -070011964 else if (best_mbmode.mv[0].as_int == zeromv[0].as_int)
Yaowu Xuc27fc142016-08-22 16:08:15 -070011965 best_mbmode.mode = ZEROMV;
11966 } else {
11967 int_mv nearestmv[2];
11968 int_mv nearmv[2];
11969
11970#if CONFIG_EXT_INTER
11971 if (mbmi_ext->ref_mv_count[rf_type] > 1) {
11972 nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][1].this_mv;
11973 nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][1].comp_mv;
11974 } else {
11975 nearmv[0] = frame_mv[NEARMV][refs[0]];
11976 nearmv[1] = frame_mv[NEARMV][refs[1]];
11977 }
11978#else
Yaowu Xuc27fc142016-08-22 16:08:15 -070011979 int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2)
Yaowu Xuf883b422016-08-30 14:01:10 -070011980 ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2)
Yaowu Xuc27fc142016-08-22 16:08:15 -070011981 : INT_MAX;
11982
11983 for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) {
11984 nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv;
11985 nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][i + 1].comp_mv;
11986
11987 if (nearmv[0].as_int == best_mbmode.mv[0].as_int &&
11988 nearmv[1].as_int == best_mbmode.mv[1].as_int) {
11989 best_mbmode.mode = NEARMV;
11990 best_mbmode.ref_mv_idx = i;
11991 }
11992 }
Fergus Simpson4063a682017-02-28 16:52:22 -080011993#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070011994 if (mbmi_ext->ref_mv_count[rf_type] >= 1) {
11995 nearestmv[0] = mbmi_ext->ref_mv_stack[rf_type][0].this_mv;
11996 nearestmv[1] = mbmi_ext->ref_mv_stack[rf_type][0].comp_mv;
11997 } else {
11998 nearestmv[0] = frame_mv[NEARESTMV][refs[0]];
11999 nearestmv[1] = frame_mv[NEARESTMV][refs[1]];
12000 }
12001
12002 if (nearestmv[0].as_int == best_mbmode.mv[0].as_int &&
Yushin Cho67dda512017-07-25 14:58:22 -070012003 nearestmv[1].as_int == best_mbmode.mv[1].as_int)
Yaowu Xuc27fc142016-08-22 16:08:15 -070012004#if CONFIG_EXT_INTER
Yushin Cho67dda512017-07-25 14:58:22 -070012005 {
Yaowu Xuc27fc142016-08-22 16:08:15 -070012006 best_mbmode.mode = NEAREST_NEARESTMV;
David Barker404b2e82017-03-27 13:07:47 +010012007 } else {
12008 int ref_set = (mbmi_ext->ref_mv_count[rf_type] >= 2)
12009 ? AOMMIN(2, mbmi_ext->ref_mv_count[rf_type] - 2)
12010 : INT_MAX;
12011
12012 for (i = 0; i <= ref_set && ref_set != INT_MAX; ++i) {
12013 nearmv[0] = mbmi_ext->ref_mv_stack[rf_type][i + 1].this_mv;
12014 nearmv[1] = mbmi_ext->ref_mv_stack[rf_type][i + 1].comp_mv;
12015
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -070012016 // Try switching to the NEAR_NEARMV mode
12017 if (nearmv[0].as_int == best_mbmode.mv[0].as_int &&
David Barker404b2e82017-03-27 13:07:47 +010012018 nearmv[1].as_int == best_mbmode.mv[1].as_int) {
12019 best_mbmode.mode = NEAR_NEARMV;
12020 best_mbmode.ref_mv_idx = i;
12021 }
12022 }
12023
David Barker3dfba992017-04-03 16:10:09 +010012024 if (best_mbmode.mode == NEW_NEWMV &&
David Barker404b2e82017-03-27 13:07:47 +010012025 best_mbmode.mv[0].as_int == zeromv[0].as_int &&
12026 best_mbmode.mv[1].as_int == zeromv[1].as_int)
12027 best_mbmode.mode = ZERO_ZEROMV;
12028 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070012029#else
Yushin Cho67dda512017-07-25 14:58:22 -070012030 {
Yaowu Xuc27fc142016-08-22 16:08:15 -070012031 best_mbmode.mode = NEARESTMV;
David Barker404b2e82017-03-27 13:07:47 +010012032 } else if (best_mbmode.mv[0].as_int == zeromv[0].as_int &&
12033 best_mbmode.mv[1].as_int == zeromv[1].as_int) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070012034 best_mbmode.mode = ZEROMV;
David Barker404b2e82017-03-27 13:07:47 +010012035 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070012036#endif // CONFIG_EXT_INTER
12037 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070012038 }
12039
David Barker9620bcd2017-03-22 14:46:42 +000012040 // Make sure that the ref_mv_idx is only nonzero when we're
12041 // using a mode which can support ref_mv_idx
12042 if (best_mbmode.ref_mv_idx != 0 &&
David Barker404b2e82017-03-27 13:07:47 +010012043#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -070012044#if CONFIG_COMPOUND_SINGLEREF
12045 !(best_mbmode.mode == NEWMV || best_mbmode.mode == SR_NEW_NEWMV ||
12046 best_mbmode.mode == NEW_NEWMV ||
Yushin Cho67dda512017-07-25 14:58:22 -070012047 have_nearmv_in_inter_mode(best_mbmode.mode)))
Zoe Liu85b66462017-04-20 14:28:19 -070012048#else // !CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +010012049 !(best_mbmode.mode == NEWMV || best_mbmode.mode == NEW_NEWMV ||
Yushin Cho67dda512017-07-25 14:58:22 -070012050 have_nearmv_in_inter_mode(best_mbmode.mode)))
Zoe Liu85b66462017-04-20 14:28:19 -070012051#endif // CONFIG_COMPOUND_SINGLEREF
12052#else // !CONFIG_EXT_INTER
Yushin Cho67dda512017-07-25 14:58:22 -070012053 !(best_mbmode.mode == NEARMV || best_mbmode.mode == NEWMV))
Zoe Liu85b66462017-04-20 14:28:19 -070012054#endif // CONFIG_EXT_INTER
Yushin Cho67dda512017-07-25 14:58:22 -070012055 {
David Barker9620bcd2017-03-22 14:46:42 +000012056 best_mbmode.ref_mv_idx = 0;
12057 }
12058
David Barkercdcac6d2016-12-01 17:04:16 +000012059 {
Jingning Han731af492016-11-17 11:53:23 -080012060 int8_t ref_frame_type = av1_ref_frame_type(best_mbmode.ref_frame);
12061 int16_t mode_ctx = mbmi_ext->mode_context[ref_frame_type];
David Barker68e6e862016-11-24 15:10:15 +000012062 if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) {
David Barkercdcac6d2016-12-01 17:04:16 +000012063 int_mv zeromv[2];
David Barker68e6e862016-11-24 15:10:15 +000012064#if CONFIG_GLOBAL_MOTION
David Barkercdcac6d2016-12-01 17:04:16 +000012065 const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0],
12066 best_mbmode.ref_frame[1] };
12067 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]],
David Barker45390c12017-02-20 14:44:40 +000012068 cm->allow_high_precision_mv,
RogerZhou3b635242017-09-19 10:06:46 -070012069 bsize, mi_col, mi_row, 0
12070#if CONFIG_AMVR
12071 ,
12072 cm->cur_frame_mv_precision_level
12073#endif
12074 )
David Barkercdcac6d2016-12-01 17:04:16 +000012075 .as_int;
RogerZhou3b635242017-09-19 10:06:46 -070012076 zeromv[1].as_int =
12077 (refs[1] != NONE_FRAME)
12078 ?
12079#if CONFIG_AMVR
12080 gm_get_motion_vector(&cm->global_motion[refs[1]],
12081 cm->allow_high_precision_mv, bsize, mi_col,
12082 mi_row, 0, cm->cur_frame_mv_precision_level)
12083 .as_int
12084 : 0;
12085#else
12086 gm_get_motion_vector(&cm->global_motion[refs[1]],
12087 cm->allow_high_precision_mv, bsize, mi_col,
12088 mi_row, 0)
12089 .as_int
12090 : 0;
12091#endif
12092
12093#if CONFIG_AMVR
12094 lower_mv_precision(&zeromv[0].as_mv, cm->allow_high_precision_mv,
12095 cm->cur_frame_mv_precision_level);
12096 lower_mv_precision(&zeromv[1].as_mv, cm->allow_high_precision_mv,
12097 cm->cur_frame_mv_precision_level);
12098#else
David Barkercdcac6d2016-12-01 17:04:16 +000012099 lower_mv_precision(&zeromv[0].as_mv, cm->allow_high_precision_mv);
12100 lower_mv_precision(&zeromv[1].as_mv, cm->allow_high_precision_mv);
RogerZhou3b635242017-09-19 10:06:46 -070012101#endif
12102
David Barkercdcac6d2016-12-01 17:04:16 +000012103#else
12104 zeromv[0].as_int = zeromv[1].as_int = 0;
12105#endif // CONFIG_GLOBAL_MOTION
12106 if (best_mbmode.ref_frame[0] > INTRA_FRAME &&
12107 best_mbmode.mv[0].as_int == zeromv[0].as_int &&
12108#if CONFIG_EXT_INTER
12109 (best_mbmode.ref_frame[1] <= INTRA_FRAME)
12110#else
Emil Keyder01770b32017-01-20 18:03:11 -050012111 (best_mbmode.ref_frame[1] == NONE_FRAME ||
David Barkercdcac6d2016-12-01 17:04:16 +000012112 best_mbmode.mv[1].as_int == zeromv[1].as_int)
12113#endif // CONFIG_EXT_INTER
12114 ) {
12115 best_mbmode.mode = ZEROMV;
12116 }
David Barker68e6e862016-11-24 15:10:15 +000012117 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070012118 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070012119
12120 if (best_mode_index < 0 || best_rd >= best_rd_so_far) {
12121 rd_cost->rate = INT_MAX;
12122 rd_cost->rdcost = INT64_MAX;
12123 return;
12124 }
12125
Yaowu Xuc27fc142016-08-22 16:08:15 -070012126#if CONFIG_DUAL_FILTER
12127 assert((cm->interp_filter == SWITCHABLE) ||
12128 (cm->interp_filter == best_mbmode.interp_filter[0]) ||
12129 !is_inter_block(&best_mbmode));
12130 assert((cm->interp_filter == SWITCHABLE) ||
12131 (cm->interp_filter == best_mbmode.interp_filter[1]) ||
12132 !is_inter_block(&best_mbmode));
12133 if (best_mbmode.ref_frame[1] > INTRA_FRAME) {
12134 assert((cm->interp_filter == SWITCHABLE) ||
12135 (cm->interp_filter == best_mbmode.interp_filter[2]) ||
12136 !is_inter_block(&best_mbmode));
12137 assert((cm->interp_filter == SWITCHABLE) ||
12138 (cm->interp_filter == best_mbmode.interp_filter[3]) ||
12139 !is_inter_block(&best_mbmode));
12140 }
12141#else
12142 assert((cm->interp_filter == SWITCHABLE) ||
12143 (cm->interp_filter == best_mbmode.interp_filter) ||
12144 !is_inter_block(&best_mbmode));
Fergus Simpson4063a682017-02-28 16:52:22 -080012145#endif // CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070012146
12147 if (!cpi->rc.is_src_frame_alt_ref)
Yaowu Xuf883b422016-08-30 14:01:10 -070012148 av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact,
12149 sf->adaptive_rd_thresh, bsize, best_mode_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012150
12151 // macroblock modes
12152 *mbmi = best_mbmode;
12153 x->skip |= best_skip2;
12154
Yue Chen19e7aa82016-11-30 14:05:39 -080012155// Note: this section is needed since the mode may have been forced to
12156// ZEROMV by the all-zero mode handling of ref-mv.
12157#if CONFIG_GLOBAL_MOTION
12158 if (mbmi->mode == ZEROMV
12159#if CONFIG_EXT_INTER
12160 || mbmi->mode == ZERO_ZEROMV
12161#endif // CONFIG_EXT_INTER
12162 ) {
Sarah Parker19234cc2017-03-10 16:43:25 -080012163#if CONFIG_WARPED_MOTION || CONFIG_MOTION_VAR
12164 // Correct the motion mode for ZEROMV
Sarah Parker0eea89f2017-07-11 11:56:36 -070012165 const MOTION_MODE last_motion_mode_allowed =
Yue Chen52c51732017-07-11 15:08:30 -070012166 motion_mode_allowed(0, xd->global_motion,
12167#if CONFIG_WARPED_MOTION
12168 xd,
12169#endif
12170 xd->mi[0]);
Sarah Parker19234cc2017-03-10 16:43:25 -080012171 if (mbmi->motion_mode > last_motion_mode_allowed)
12172 mbmi->motion_mode = last_motion_mode_allowed;
12173#endif // CONFIG_WARPED_MOTION || CONFIG_MOTION_VAR
12174
12175 // Correct the interpolation filter for ZEROMV
Yue Chen19e7aa82016-11-30 14:05:39 -080012176 if (is_nontrans_global_motion(xd)) {
12177#if CONFIG_DUAL_FILTER
12178 mbmi->interp_filter[0] = cm->interp_filter == SWITCHABLE
12179 ? EIGHTTAP_REGULAR
12180 : cm->interp_filter;
12181 mbmi->interp_filter[1] = cm->interp_filter == SWITCHABLE
12182 ? EIGHTTAP_REGULAR
12183 : cm->interp_filter;
12184#else
12185 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP_REGULAR
12186 : cm->interp_filter;
12187#endif // CONFIG_DUAL_FILTER
12188 }
12189 }
12190#endif // CONFIG_GLOBAL_MOTION
12191
Yaowu Xuc27fc142016-08-22 16:08:15 -070012192 for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
12193 if (mbmi->mode != NEWMV)
12194 mbmi->pred_mv[i].as_int = mbmi->mv[i].as_int;
12195 else
12196 mbmi->pred_mv[i].as_int = mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_int;
12197 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070012198
12199 for (i = 0; i < REFERENCE_MODES; ++i) {
12200 if (best_pred_rd[i] == INT64_MAX)
12201 best_pred_diff[i] = INT_MIN;
12202 else
12203 best_pred_diff[i] = best_rd - best_pred_rd[i];
12204 }
12205
12206 x->skip |= best_mode_skippable;
12207
12208 assert(best_mode_index >= 0);
12209
12210 store_coding_context(x, ctx, best_mode_index, best_pred_diff,
12211 best_mode_skippable);
12212
Urvang Joshic9e71d42017-08-09 18:58:33 -070012213 if (pmi->palette_size[1] > 0) {
12214 assert(try_palette);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012215 restore_uv_color_map(cpi, x);
12216 }
12217}
12218
Urvang Joshi52648442016-10-13 17:27:51 -070012219void av1_rd_pick_inter_mode_sb_seg_skip(const AV1_COMP *cpi,
12220 TileDataEnc *tile_data, MACROBLOCK *x,
David Barker45390c12017-02-20 14:44:40 +000012221 int mi_row, int mi_col,
Angie Chiang2a2a7dd2017-04-25 16:08:47 -070012222 RD_STATS *rd_cost, BLOCK_SIZE bsize,
Yaowu Xuf883b422016-08-30 14:01:10 -070012223 PICK_MODE_CONTEXT *ctx,
12224 int64_t best_rd_so_far) {
Urvang Joshi52648442016-10-13 17:27:51 -070012225 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012226 MACROBLOCKD *const xd = &x->e_mbd;
12227 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
12228 unsigned char segment_id = mbmi->segment_id;
12229 const int comp_pred = 0;
12230 int i;
12231 int64_t best_pred_diff[REFERENCE_MODES];
12232 unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME];
Zoe Liuc082bbc2017-05-17 13:31:37 -070012233#if CONFIG_EXT_COMP_REFS
12234 unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME][TOTAL_REFS_PER_FRAME];
12235#else
Yaowu Xuc27fc142016-08-22 16:08:15 -070012236 unsigned int ref_costs_comp[TOTAL_REFS_PER_FRAME];
Zoe Liuc082bbc2017-05-17 13:31:37 -070012237#endif // CONFIG_EXT_COMP_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -070012238 aom_prob comp_mode_p;
James Zern7b9407a2016-05-18 23:48:05 -070012239 InterpFilter best_filter = SWITCHABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012240 int64_t this_rd = INT64_MAX;
12241 int rate2 = 0;
12242 const int64_t distortion2 = 0;
David Barker45390c12017-02-20 14:44:40 +000012243 (void)mi_row;
12244 (void)mi_col;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012245
12246 estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
12247 &comp_mode_p);
12248
12249 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) x->pred_sse[i] = INT_MAX;
12250 for (i = LAST_FRAME; i < TOTAL_REFS_PER_FRAME; ++i)
12251 x->pred_mv_sad[i] = INT_MAX;
12252
12253 rd_cost->rate = INT_MAX;
12254
12255 assert(segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP));
12256
12257 mbmi->palette_mode_info.palette_size[0] = 0;
12258 mbmi->palette_mode_info.palette_size[1] = 0;
Urvang Joshib100db72016-10-12 16:28:56 -070012259
hui su5db97432016-10-14 16:10:14 -070012260#if CONFIG_FILTER_INTRA
12261 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
12262 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
12263#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070012264 mbmi->mode = ZEROMV;
Yue Chencb60b182016-10-13 15:18:22 -070012265 mbmi->motion_mode = SIMPLE_TRANSLATION;
Luc Trudeaud6d9eee2017-07-12 12:36:50 -040012266 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012267 mbmi->ref_frame[0] = LAST_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -050012268 mbmi->ref_frame[1] = NONE_FRAME;
Sarah Parkere5299862016-08-16 14:57:37 -070012269#if CONFIG_GLOBAL_MOTION
12270 mbmi->mv[0].as_int =
Sarah Parkerae7c4582017-02-28 16:30:30 -080012271 gm_get_motion_vector(&cm->global_motion[mbmi->ref_frame[0]],
RogerZhou3b635242017-09-19 10:06:46 -070012272 cm->allow_high_precision_mv, bsize, mi_col, mi_row, 0
12273#if CONFIG_AMVR
12274 ,
12275 cm->cur_frame_mv_precision_level
12276#endif
12277 )
David Barkercdcac6d2016-12-01 17:04:16 +000012278 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -070012279#else // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070012280 mbmi->mv[0].as_int = 0;
Sarah Parkere5299862016-08-16 14:57:37 -070012281#endif // CONFIG_GLOBAL_MOTION
Jingning Han64088952016-07-11 11:24:24 -070012282 mbmi->tx_size = max_txsize_lookup[bsize];
Yaowu Xuee775b12016-10-18 10:00:21 -070012283 x->skip = 1;
Sarah Parkere5299862016-08-16 14:57:37 -070012284
Yaowu Xuc27fc142016-08-22 16:08:15 -070012285 mbmi->ref_mv_idx = 0;
12286 mbmi->pred_mv[0].as_int = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012287
Yue Chendead17d2017-04-20 16:19:39 -070012288 mbmi->motion_mode = SIMPLE_TRANSLATION;
12289#if CONFIG_MOTION_VAR
12290 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
12291#endif
Yue Chenf3e1ead2017-04-21 14:05:51 -070012292#if CONFIG_WARPED_MOTION
12293 if (is_motion_variation_allowed_bsize(bsize) && !has_second_ref(mbmi)) {
12294 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang1bc82862017-06-28 15:49:48 -070012295#if WARPED_MOTION_SORT_SAMPLES
12296 int pts_mv[SAMPLES_ARRAY_SIZE];
12297 mbmi->num_proj_ref[0] =
12298 findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv);
12299 // Rank the samples by motion vector difference
12300 if (mbmi->num_proj_ref[0] > 1)
12301 mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts,
12302 pts_inref, mbmi->num_proj_ref[0]);
12303#else
Yue Chenf3e1ead2017-04-21 14:05:51 -070012304 mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
Yunqing Wang1bc82862017-06-28 15:49:48 -070012305#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chenf3e1ead2017-04-21 14:05:51 -070012306 }
12307#endif
Yue Chendead17d2017-04-20 16:19:39 -070012308
Debargha Mukherjee0df711f2017-05-02 16:00:20 -070012309 set_default_interp_filters(mbmi, cm->interp_filter);
12310
12311 if (cm->interp_filter != SWITCHABLE) {
12312 best_filter = cm->interp_filter;
12313 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -070012314 best_filter = EIGHTTAP_REGULAR;
Debargha Mukherjee0df711f2017-05-02 16:00:20 -070012315 if (av1_is_interp_needed(xd) && av1_is_interp_search_needed(xd) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -070012316 x->source_variance >= cpi->sf.disable_filter_search_var_thresh) {
12317 int rs;
12318 int best_rs = INT_MAX;
12319 for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
12320#if CONFIG_DUAL_FILTER
12321 int k;
12322 for (k = 0; k < 4; ++k) mbmi->interp_filter[k] = i;
12323#else
12324 mbmi->interp_filter = i;
Fergus Simpson4063a682017-02-28 16:52:22 -080012325#endif // CONFIG_DUAL_FILTER
Yue Chenb23d00a2017-07-28 17:01:21 -070012326 rs = av1_get_switchable_rate(cm, x, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012327 if (rs < best_rs) {
12328 best_rs = rs;
12329#if CONFIG_DUAL_FILTER
12330 best_filter = mbmi->interp_filter[0];
12331#else
12332 best_filter = mbmi->interp_filter;
Fergus Simpson4063a682017-02-28 16:52:22 -080012333#endif // CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070012334 }
12335 }
12336 }
12337 }
Debargha Mukherjee0df711f2017-05-02 16:00:20 -070012338// Set the appropriate filter
Yaowu Xuc27fc142016-08-22 16:08:15 -070012339#if CONFIG_DUAL_FILTER
Debargha Mukherjee0df711f2017-05-02 16:00:20 -070012340 for (i = 0; i < 4; ++i) mbmi->interp_filter[i] = best_filter;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012341#else
Debargha Mukherjee0df711f2017-05-02 16:00:20 -070012342 mbmi->interp_filter = best_filter;
Fergus Simpson4063a682017-02-28 16:52:22 -080012343#endif // CONFIG_DUAL_FILTER
Yue Chenb23d00a2017-07-28 17:01:21 -070012344 rate2 += av1_get_switchable_rate(cm, x, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012345
12346 if (cm->reference_mode == REFERENCE_MODE_SELECT)
Yaowu Xuf883b422016-08-30 14:01:10 -070012347 rate2 += av1_cost_bit(comp_mode_p, comp_pred);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012348
12349 // Estimate the reference frame signaling cost and add it
12350 // to the rolling cost variable.
12351 rate2 += ref_costs_single[LAST_FRAME];
Urvang Joshi70006e42017-06-14 16:08:55 -070012352 this_rd = RDCOST(x->rdmult, rate2, distortion2);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012353
12354 rd_cost->rate = rate2;
12355 rd_cost->dist = distortion2;
12356 rd_cost->rdcost = this_rd;
Yushin Cho8e75e8b2017-09-12 16:33:28 -070012357
Yaowu Xuc27fc142016-08-22 16:08:15 -070012358 if (this_rd >= best_rd_so_far) {
12359 rd_cost->rate = INT_MAX;
12360 rd_cost->rdcost = INT64_MAX;
12361 return;
12362 }
12363
12364#if CONFIG_DUAL_FILTER
12365 assert((cm->interp_filter == SWITCHABLE) ||
12366 (cm->interp_filter == mbmi->interp_filter[0]));
12367#else
12368 assert((cm->interp_filter == SWITCHABLE) ||
12369 (cm->interp_filter == mbmi->interp_filter));
Fergus Simpson4063a682017-02-28 16:52:22 -080012370#endif // CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -070012371
Yaowu Xuf883b422016-08-30 14:01:10 -070012372 av1_update_rd_thresh_fact(cm, tile_data->thresh_freq_fact,
12373 cpi->sf.adaptive_rd_thresh, bsize, THR_ZEROMV);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012374
Yaowu Xuf883b422016-08-30 14:01:10 -070012375 av1_zero(best_pred_diff);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012376
12377 store_coding_context(x, ctx, THR_ZEROMV, best_pred_diff, 0);
12378}
12379
Yue Chencb60b182016-10-13 15:18:22 -070012380#if CONFIG_MOTION_VAR
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012381
12382struct calc_target_weighted_pred_ctxt {
12383 const MACROBLOCK *x;
12384 const uint8_t *tmp;
12385 int tmp_stride;
12386 int overlap;
12387};
12388
12389static INLINE void calc_target_weighted_pred_above(MACROBLOCKD *xd,
12390 int rel_mi_col,
12391 uint8_t nb_mi_width,
12392 MODE_INFO *nb_mi,
12393 void *fun_ctxt) {
12394 (void)nb_mi;
12395
12396 struct calc_target_weighted_pred_ctxt *ctxt =
12397 (struct calc_target_weighted_pred_ctxt *)fun_ctxt;
12398
12399#if CONFIG_HIGHBITDEPTH
12400 const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0;
12401#else
12402 const int is_hbd = 0;
12403#endif // CONFIG_HIGHBITDEPTH
12404
12405 const int bw = xd->n8_w << MI_SIZE_LOG2;
12406 const uint8_t *const mask1d = av1_get_obmc_mask(ctxt->overlap);
12407
12408 int32_t *wsrc = ctxt->x->wsrc_buf + (rel_mi_col * MI_SIZE);
12409 int32_t *mask = ctxt->x->mask_buf + (rel_mi_col * MI_SIZE);
12410 const uint8_t *tmp = ctxt->tmp + rel_mi_col * MI_SIZE;
12411
12412 if (!is_hbd) {
12413 for (int row = 0; row < ctxt->overlap; ++row) {
12414 const uint8_t m0 = mask1d[row];
12415 const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0;
12416 for (int col = 0; col < nb_mi_width * MI_SIZE; ++col) {
12417 wsrc[col] = m1 * tmp[col];
12418 mask[col] = m0;
12419 }
12420 wsrc += bw;
12421 mask += bw;
12422 tmp += ctxt->tmp_stride;
12423 }
12424#if CONFIG_HIGHBITDEPTH
12425 } else {
12426 const uint16_t *tmp16 = CONVERT_TO_SHORTPTR(tmp);
12427
12428 for (int row = 0; row < ctxt->overlap; ++row) {
12429 const uint8_t m0 = mask1d[row];
12430 const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0;
12431 for (int col = 0; col < nb_mi_width * MI_SIZE; ++col) {
12432 wsrc[col] = m1 * tmp16[col];
12433 mask[col] = m0;
12434 }
12435 wsrc += bw;
12436 mask += bw;
12437 tmp16 += ctxt->tmp_stride;
12438 }
12439#endif // CONFIG_HIGHBITDEPTH
12440 }
12441}
12442
12443static INLINE void calc_target_weighted_pred_left(MACROBLOCKD *xd,
12444 int rel_mi_row,
12445 uint8_t nb_mi_height,
12446 MODE_INFO *nb_mi,
12447 void *fun_ctxt) {
12448 (void)nb_mi;
12449
12450 struct calc_target_weighted_pred_ctxt *ctxt =
12451 (struct calc_target_weighted_pred_ctxt *)fun_ctxt;
12452
12453#if CONFIG_HIGHBITDEPTH
12454 const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0;
12455#else
12456 const int is_hbd = 0;
12457#endif // CONFIG_HIGHBITDEPTH
12458
12459 const int bw = xd->n8_w << MI_SIZE_LOG2;
12460 const uint8_t *const mask1d = av1_get_obmc_mask(ctxt->overlap);
12461
12462 int32_t *wsrc = ctxt->x->wsrc_buf + (rel_mi_row * MI_SIZE * bw);
12463 int32_t *mask = ctxt->x->mask_buf + (rel_mi_row * MI_SIZE * bw);
12464 const uint8_t *tmp = ctxt->tmp + (rel_mi_row * MI_SIZE * ctxt->tmp_stride);
12465
12466 if (!is_hbd) {
12467 for (int row = 0; row < nb_mi_height * MI_SIZE; ++row) {
12468 for (int col = 0; col < ctxt->overlap; ++col) {
12469 const uint8_t m0 = mask1d[col];
12470 const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0;
12471 wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 +
12472 (tmp[col] << AOM_BLEND_A64_ROUND_BITS) * m1;
12473 mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0;
12474 }
12475 wsrc += bw;
12476 mask += bw;
12477 tmp += ctxt->tmp_stride;
12478 }
12479#if CONFIG_HIGHBITDEPTH
12480 } else {
12481 const uint16_t *tmp16 = CONVERT_TO_SHORTPTR(tmp);
12482
12483 for (int row = 0; row < nb_mi_height * MI_SIZE; ++row) {
12484 for (int col = 0; col < ctxt->overlap; ++col) {
12485 const uint8_t m0 = mask1d[col];
12486 const uint8_t m1 = AOM_BLEND_A64_MAX_ALPHA - m0;
12487 wsrc[col] = (wsrc[col] >> AOM_BLEND_A64_ROUND_BITS) * m0 +
12488 (tmp16[col] << AOM_BLEND_A64_ROUND_BITS) * m1;
12489 mask[col] = (mask[col] >> AOM_BLEND_A64_ROUND_BITS) * m0;
12490 }
12491 wsrc += bw;
12492 mask += bw;
12493 tmp16 += ctxt->tmp_stride;
12494 }
12495#endif // CONFIG_HIGHBITDEPTH
12496 }
12497}
12498
Yaowu Xuf883b422016-08-30 14:01:10 -070012499// This function has a structure similar to av1_build_obmc_inter_prediction
Yaowu Xuc27fc142016-08-22 16:08:15 -070012500//
12501// The OBMC predictor is computed as:
12502//
12503// PObmc(x,y) =
Yaowu Xuf883b422016-08-30 14:01:10 -070012504// AOM_BLEND_A64(Mh(x),
12505// AOM_BLEND_A64(Mv(y), P(x,y), PAbove(x,y)),
Yaowu Xuc27fc142016-08-22 16:08:15 -070012506// PLeft(x, y))
12507//
Yaowu Xuf883b422016-08-30 14:01:10 -070012508// Scaling up by AOM_BLEND_A64_MAX_ALPHA ** 2 and omitting the intermediate
Yaowu Xuc27fc142016-08-22 16:08:15 -070012509// rounding, this can be written as:
12510//
Yaowu Xuf883b422016-08-30 14:01:10 -070012511// AOM_BLEND_A64_MAX_ALPHA * AOM_BLEND_A64_MAX_ALPHA * Pobmc(x,y) =
Yaowu Xuc27fc142016-08-22 16:08:15 -070012512// Mh(x) * Mv(y) * P(x,y) +
12513// Mh(x) * Cv(y) * Pabove(x,y) +
Yaowu Xuf883b422016-08-30 14:01:10 -070012514// AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y)
Yaowu Xuc27fc142016-08-22 16:08:15 -070012515//
12516// Where :
12517//
Yaowu Xuf883b422016-08-30 14:01:10 -070012518// Cv(y) = AOM_BLEND_A64_MAX_ALPHA - Mv(y)
12519// Ch(y) = AOM_BLEND_A64_MAX_ALPHA - Mh(y)
Yaowu Xuc27fc142016-08-22 16:08:15 -070012520//
12521// This function computes 'wsrc' and 'mask' as:
12522//
12523// wsrc(x, y) =
Yaowu Xuf883b422016-08-30 14:01:10 -070012524// AOM_BLEND_A64_MAX_ALPHA * AOM_BLEND_A64_MAX_ALPHA * src(x, y) -
Yaowu Xuc27fc142016-08-22 16:08:15 -070012525// Mh(x) * Cv(y) * Pabove(x,y) +
Yaowu Xuf883b422016-08-30 14:01:10 -070012526// AOM_BLEND_A64_MAX_ALPHA * Ch(x) * PLeft(x, y)
Yaowu Xuc27fc142016-08-22 16:08:15 -070012527//
12528// mask(x, y) = Mh(x) * Mv(y)
12529//
12530// These can then be used to efficiently approximate the error for any
12531// predictor P in the context of the provided neighbouring predictors by
12532// computing:
12533//
12534// error(x, y) =
Yaowu Xuf883b422016-08-30 14:01:10 -070012535// wsrc(x, y) - mask(x, y) * P(x, y) / (AOM_BLEND_A64_MAX_ALPHA ** 2)
Yaowu Xuc27fc142016-08-22 16:08:15 -070012536//
Yaowu Xuf883b422016-08-30 14:01:10 -070012537static void calc_target_weighted_pred(const AV1_COMMON *cm, const MACROBLOCK *x,
Yaowu Xuc27fc142016-08-22 16:08:15 -070012538 const MACROBLOCKD *xd, int mi_row,
12539 int mi_col, const uint8_t *above,
12540 int above_stride, const uint8_t *left,
Yue Chene9638cc2016-10-10 12:37:54 -070012541 int left_stride) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070012542 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Jingning Hanff6ee6a2016-12-07 09:55:21 -080012543 const int bw = xd->n8_w << MI_SIZE_LOG2;
12544 const int bh = xd->n8_h << MI_SIZE_LOG2;
Yue Chene9638cc2016-10-10 12:37:54 -070012545 int32_t *mask_buf = x->mask_buf;
12546 int32_t *wsrc_buf = x->wsrc_buf;
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012547
Yaowu Xuf883b422016-08-30 14:01:10 -070012548 const int src_scale = AOM_BLEND_A64_MAX_ALPHA * AOM_BLEND_A64_MAX_ALPHA;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020012549#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070012550 const int is_hbd = (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0;
12551#else
12552 const int is_hbd = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020012553#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070012554
12555 // plane 0 should not be subsampled
12556 assert(xd->plane[0].subsampling_x == 0);
12557 assert(xd->plane[0].subsampling_y == 0);
12558
Yaowu Xuf883b422016-08-30 14:01:10 -070012559 av1_zero_array(wsrc_buf, bw * bh);
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012560 for (int i = 0; i < bw * bh; ++i) mask_buf[i] = AOM_BLEND_A64_MAX_ALPHA;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012561
12562 // handle above row
12563 if (xd->up_available) {
Jingning Han440d4252017-07-24 14:07:34 -070012564 const int overlap =
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012565 AOMMIN(block_size_high[bsize], block_size_high[BLOCK_64X64]) >> 1;
12566 struct calc_target_weighted_pred_ctxt ctxt = { x, above, above_stride,
12567 overlap };
12568 foreach_overlappable_nb_above(cm, (MACROBLOCKD *)xd, mi_col,
12569 max_neighbor_obmc[b_width_log2_lookup[bsize]],
12570 calc_target_weighted_pred_above, &ctxt);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012571 }
12572
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012573 for (int i = 0; i < bw * bh; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -070012574 wsrc_buf[i] *= AOM_BLEND_A64_MAX_ALPHA;
12575 mask_buf[i] *= AOM_BLEND_A64_MAX_ALPHA;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012576 }
12577
12578 // handle left column
12579 if (xd->left_available) {
Jingning Han440d4252017-07-24 14:07:34 -070012580 const int overlap =
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012581 AOMMIN(block_size_wide[bsize], block_size_wide[BLOCK_64X64]) >> 1;
12582 struct calc_target_weighted_pred_ctxt ctxt = { x, left, left_stride,
12583 overlap };
12584 foreach_overlappable_nb_left(cm, (MACROBLOCKD *)xd, mi_row,
12585 max_neighbor_obmc[b_height_log2_lookup[bsize]],
12586 calc_target_weighted_pred_left, &ctxt);
Yaowu Xuc27fc142016-08-22 16:08:15 -070012587 }
12588
12589 if (!is_hbd) {
12590 const uint8_t *src = x->plane[0].src.buf;
12591
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012592 for (int row = 0; row < bh; ++row) {
12593 for (int col = 0; col < bw; ++col) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070012594 wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col];
12595 }
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012596 wsrc_buf += bw;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012597 src += x->plane[0].src.stride;
12598 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020012599#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070012600 } else {
12601 const uint16_t *src = CONVERT_TO_SHORTPTR(x->plane[0].src.buf);
12602
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012603 for (int row = 0; row < bh; ++row) {
12604 for (int col = 0; col < bw; ++col) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070012605 wsrc_buf[col] = src[col] * src_scale - wsrc_buf[col];
12606 }
Rupert Swarbrickc0cea7f2017-08-22 14:06:56 +010012607 wsrc_buf += bw;
Yaowu Xuc27fc142016-08-22 16:08:15 -070012608 src += x->plane[0].src.stride;
12609 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020012610#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070012611 }
12612}
Yue Chenf27b1602017-01-13 11:11:43 -080012613
12614#if CONFIG_NCOBMC
12615void av1_check_ncobmc_rd(const struct AV1_COMP *cpi, struct macroblock *x,
12616 int mi_row, int mi_col) {
12617 const AV1_COMMON *const cm = &cpi->common;
12618 MACROBLOCKD *const xd = &x->e_mbd;
12619 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
12620 MB_MODE_INFO backup_mbmi;
12621 BLOCK_SIZE bsize = mbmi->sb_type;
12622 int ref, skip_blk, backup_skip = x->skip;
12623 int64_t rd_causal;
12624 RD_STATS rd_stats_y, rd_stats_uv;
12625 int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
12626 int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
12627
12628 // Recompute the best causal predictor and rd
12629 mbmi->motion_mode = SIMPLE_TRANSLATION;
12630 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
12631 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
12632 YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mbmi->ref_frame[ref]);
12633 assert(cfg != NULL);
12634 av1_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
12635 &xd->block_refs[ref]->sf);
12636 }
Jingning Han91d9a792017-04-18 12:01:52 -070012637 av1_setup_dst_planes(x->e_mbd.plane, bsize,
12638 get_frame_new_buffer(&cpi->common), mi_row, mi_col);
Yue Chenf27b1602017-01-13 11:11:43 -080012639
Jingning Hanc44009c2017-05-06 11:36:49 -070012640 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
Yue Chenf27b1602017-01-13 11:11:43 -080012641
12642 av1_subtract_plane(x, bsize, 0);
Wei-Ting Linba5f1872017-07-06 12:26:09 -070012643#if CONFIG_VAR_TX
12644 if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) {
12645 select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12646 } else {
12647 int idx, idy;
12648 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12649 for (idy = 0; idy < xd->n8_h; ++idy)
12650 for (idx = 0; idx < xd->n8_w; ++idx)
12651 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
12652 memset(x->blk_skip[0], rd_stats_y.skip,
12653 sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4);
12654 }
12655 inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
12656#else
Yue Chenf27b1602017-01-13 11:11:43 -080012657 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12658 super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
Wei-Ting Linba5f1872017-07-06 12:26:09 -070012659#endif
Yue Chenf27b1602017-01-13 11:11:43 -080012660 assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX);
12661 if (rd_stats_y.skip && rd_stats_uv.skip) {
12662 rd_stats_y.rate = rate_skip1;
12663 rd_stats_uv.rate = 0;
12664 rd_stats_y.dist = rd_stats_y.sse;
12665 rd_stats_uv.dist = rd_stats_uv.sse;
12666 skip_blk = 0;
Urvang Joshi70006e42017-06-14 16:08:55 -070012667 } else if (RDCOST(x->rdmult,
Yue Chenf27b1602017-01-13 11:11:43 -080012668 (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0),
12669 (rd_stats_y.dist + rd_stats_uv.dist)) >
Urvang Joshi70006e42017-06-14 16:08:55 -070012670 RDCOST(x->rdmult, rate_skip1,
Yue Chenf27b1602017-01-13 11:11:43 -080012671 (rd_stats_y.sse + rd_stats_uv.sse))) {
12672 rd_stats_y.rate = rate_skip1;
12673 rd_stats_uv.rate = 0;
12674 rd_stats_y.dist = rd_stats_y.sse;
12675 rd_stats_uv.dist = rd_stats_uv.sse;
12676 skip_blk = 1;
12677 } else {
12678 rd_stats_y.rate += rate_skip0;
12679 skip_blk = 0;
12680 }
12681 backup_skip = skip_blk;
12682 backup_mbmi = *mbmi;
Urvang Joshi70006e42017-06-14 16:08:55 -070012683 rd_causal = RDCOST(x->rdmult, (rd_stats_y.rate + rd_stats_uv.rate),
Yue Chenf27b1602017-01-13 11:11:43 -080012684 (rd_stats_y.dist + rd_stats_uv.dist));
Urvang Joshi70006e42017-06-14 16:08:55 -070012685 rd_causal +=
12686 RDCOST(x->rdmult, av1_cost_bit(cm->fc->motion_mode_prob[bsize][0], 0), 0);
Yue Chenf27b1602017-01-13 11:11:43 -080012687
12688 // Check non-causal mode
12689 mbmi->motion_mode = OBMC_CAUSAL;
12690 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
12691
12692 av1_subtract_plane(x, bsize, 0);
Wei-Ting Linba5f1872017-07-06 12:26:09 -070012693#if CONFIG_VAR_TX
12694 if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) {
12695 select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12696 } else {
12697 int idx, idy;
12698 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12699 for (idy = 0; idy < xd->n8_h; ++idy)
12700 for (idx = 0; idx < xd->n8_w; ++idx)
12701 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
12702 memset(x->blk_skip[0], rd_stats_y.skip,
12703 sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4);
12704 }
12705 inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
12706#else
Yue Chenf27b1602017-01-13 11:11:43 -080012707 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12708 super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
Wei-Ting Linba5f1872017-07-06 12:26:09 -070012709#endif
Yue Chenf27b1602017-01-13 11:11:43 -080012710 assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX);
12711 if (rd_stats_y.skip && rd_stats_uv.skip) {
12712 rd_stats_y.rate = rate_skip1;
12713 rd_stats_uv.rate = 0;
12714 rd_stats_y.dist = rd_stats_y.sse;
12715 rd_stats_uv.dist = rd_stats_uv.sse;
12716 skip_blk = 0;
Urvang Joshi70006e42017-06-14 16:08:55 -070012717 } else if (RDCOST(x->rdmult,
Yue Chenf27b1602017-01-13 11:11:43 -080012718 (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0),
12719 (rd_stats_y.dist + rd_stats_uv.dist)) >
Urvang Joshi70006e42017-06-14 16:08:55 -070012720 RDCOST(x->rdmult, rate_skip1,
Yue Chenf27b1602017-01-13 11:11:43 -080012721 (rd_stats_y.sse + rd_stats_uv.sse))) {
12722 rd_stats_y.rate = rate_skip1;
12723 rd_stats_uv.rate = 0;
12724 rd_stats_y.dist = rd_stats_y.sse;
12725 rd_stats_uv.dist = rd_stats_uv.sse;
12726 skip_blk = 1;
12727 } else {
12728 rd_stats_y.rate += rate_skip0;
12729 skip_blk = 0;
12730 }
12731
12732 if (rd_causal >
clang-format4eafefe2017-09-04 12:51:20 -070012733 RDCOST(x->rdmult,
12734 rd_stats_y.rate + rd_stats_uv.rate +
12735 av1_cost_bit(cm->fc->motion_mode_prob[bsize][0], 1),
Yue Chenf27b1602017-01-13 11:11:43 -080012736 (rd_stats_y.dist + rd_stats_uv.dist))) {
12737 x->skip = skip_blk;
12738 } else {
12739 *mbmi = backup_mbmi;
12740 x->skip = backup_skip;
12741 }
12742}
Fergus Simpson4063a682017-02-28 16:52:22 -080012743#endif // CONFIG_NCOBMC
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012744
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012745int64_t get_prediction_rd_cost(const struct AV1_COMP *cpi, struct macroblock *x,
12746 int mi_row, int mi_col, int *skip_blk,
12747 MB_MODE_INFO *backup_mbmi) {
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012748 const AV1_COMMON *const cm = &cpi->common;
12749 MACROBLOCKD *const xd = &x->e_mbd;
12750 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
12751 BLOCK_SIZE bsize = mbmi->sb_type;
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012752#if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION
12753 const MOTION_MODE motion_allowed = motion_mode_allowed(
12754#if CONFIG_GLOBAL_MOTION
12755 0, xd->global_motion,
12756#endif // CONFIG_GLOBAL_MOTION
12757#if CONFIG_WARPED_MOTION
12758 xd,
12759#endif
12760 xd->mi[0]);
12761#endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012762 RD_STATS rd_stats_y, rd_stats_uv;
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012763 int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
12764 int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012765 int64_t this_rd;
12766 int ref;
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012767
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012768#if CONFIG_CB4X4
12769 x->skip_chroma_rd =
12770 !is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
12771 xd->plane[1].subsampling_y);
12772#endif
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012773
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012774 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
12775 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
12776 YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mbmi->ref_frame[ref]);
12777 assert(cfg != NULL);
12778 av1_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
12779 &xd->block_refs[ref]->sf);
12780 }
12781 av1_setup_dst_planes(x->e_mbd.plane, bsize,
12782 get_frame_new_buffer(&cpi->common), mi_row, mi_col);
12783
12784#if CONFIG_NCOBMC_ADAPT_WEIGHT
12785 if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT)
12786#endif
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012787 av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012788
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012789#if CONFIG_MOTION_VAR
12790 if (mbmi->motion_mode == OBMC_CAUSAL) {
12791#if CONFIG_NCOBMC
12792 av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012793#else
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012794 av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012795#endif
12796 }
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012797#endif // CONFIG_MOTION_VAR
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012798
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012799#if CONFIG_NCOBMC_ADAPT_WEIGHT
12800 if (mbmi->motion_mode == NCOBMC_ADAPT_WEIGHT)
12801 for (int plane = 0; plane < MAX_MB_PLANE; ++plane)
12802 get_pred_from_intrpl_buf(xd, mi_row, mi_col, bsize, plane);
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012803#endif
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012804 av1_subtract_plane(x, bsize, 0);
12805
12806#if CONFIG_VAR_TX
12807 if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id]) {
12808 select_tx_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12809 } else {
12810 int idx, idy;
12811 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12812 for (idy = 0; idy < xd->n8_h; ++idy)
12813 for (idx = 0; idx < xd->n8_w; ++idx)
12814 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
12815 memset(x->blk_skip[0], rd_stats_y.skip,
12816 sizeof(uint8_t) * xd->n8_h * xd->n8_w * 4);
12817 }
12818 inter_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
12819#else
12820 super_block_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
12821 super_block_uvrd(cpi, x, &rd_stats_uv, bsize, INT64_MAX);
12822#endif
12823 assert(rd_stats_y.rate != INT_MAX && rd_stats_uv.rate != INT_MAX);
12824
12825 if (rd_stats_y.skip && rd_stats_uv.skip) {
12826 rd_stats_y.rate = rate_skip1;
12827 rd_stats_uv.rate = 0;
12828 rd_stats_y.dist = rd_stats_y.sse;
12829 rd_stats_uv.dist = rd_stats_uv.sse;
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012830 *skip_blk = 1;
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012831 } else if (RDCOST(x->rdmult,
12832 (rd_stats_y.rate + rd_stats_uv.rate + rate_skip0),
12833 (rd_stats_y.dist + rd_stats_uv.dist)) >
12834 RDCOST(x->rdmult, rate_skip1,
12835 (rd_stats_y.sse + rd_stats_uv.sse))) {
12836 rd_stats_y.rate = rate_skip1;
12837 rd_stats_uv.rate = 0;
12838 rd_stats_y.dist = rd_stats_y.sse;
12839 rd_stats_uv.dist = rd_stats_uv.sse;
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012840 *skip_blk = 1;
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012841 } else {
12842 rd_stats_y.rate += rate_skip0;
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012843 *skip_blk = 0;
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012844 }
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012845
12846 if (backup_mbmi) *backup_mbmi = *mbmi;
12847
12848 this_rd = RDCOST(x->rdmult, (rd_stats_y.rate + rd_stats_uv.rate),
12849 (rd_stats_y.dist + rd_stats_uv.dist));
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012850#if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION
12851 if (motion_allowed == NCOBMC_ADAPT_WEIGHT) {
12852 assert(mbmi->motion_mode <= NCOBMC_ADAPT_WEIGHT);
12853 this_rd +=
12854 RDCOST(x->rdmult, x->motion_mode_cost2[bsize][mbmi->motion_mode], 0);
12855 } else if (motion_allowed == OBMC_CAUSAL) {
12856 assert(mbmi->motion_mode <= OBMC_CAUSAL);
12857 this_rd +=
12858 RDCOST(x->rdmult, x->motion_mode_cost1[bsize][mbmi->motion_mode], 0);
12859 } else {
12860#endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION
12861 this_rd +=
12862 RDCOST(x->rdmult, x->motion_mode_cost[bsize][mbmi->motion_mode], 0);
12863#if CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION
12864 }
12865#endif // CONFIG_NCOBMC_ADAPT_WEIGHT && CONFIG_WARPED_MOTION
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012866 return this_rd;
12867}
12868
12869#if CONFIG_NCOBMC_ADAPT_WEIGHT
12870void av1_check_ncobmc_adapt_weight_rd(const struct AV1_COMP *cpi,
12871 struct macroblock *x, int mi_row,
12872 int mi_col) {
12873 MACROBLOCKD *const xd = &x->e_mbd;
12874 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
12875 BLOCK_SIZE bsize = mbmi->sb_type;
12876#if CONFIG_VAR_TX
12877 const int n4 = bsize_to_num_blk(bsize);
12878 uint8_t st_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
12879 uint8_t obmc_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012880 uint8_t ncobmc_blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE * 8];
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012881#endif
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012882 MB_MODE_INFO st_mbmi, obmc_mbmi, ncobmc_mbmi;
12883 int st_skip, obmc_skip, ncobmc_skip;
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012884 int64_t st_rd, obmc_rd, ncobmc_rd;
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012885#if CONFIG_WARPED_MOTION
12886 const AV1_COMMON *const cm = &cpi->common;
12887 const int is_warp_motion = mbmi->motion_mode == WARPED_CAUSAL;
12888 const int rs = RDCOST(x->rdmult, av1_get_switchable_rate(cm, x, xd), 0);
12889 MB_MODE_INFO warp_mbmi;
12890 int64_t warp_rd;
12891 int warp_skip;
12892#endif
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012893
12894 // Recompute the rd for the motion mode decided in rd loop
12895 mbmi->motion_mode = SIMPLE_TRANSLATION;
12896 st_rd = get_prediction_rd_cost(cpi, x, mi_row, mi_col, &st_skip, &st_mbmi);
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012897#if CONFIG_WARPED_MOTION
12898 st_rd += rs;
12899#endif
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012900#if CONFIG_VAR_TX
12901 memcpy(st_blk_skip, x->blk_skip[0], sizeof(st_blk_skip[0]) * n4);
12902#endif
12903
12904 mbmi->motion_mode = OBMC_CAUSAL;
12905 obmc_rd =
12906 get_prediction_rd_cost(cpi, x, mi_row, mi_col, &obmc_skip, &obmc_mbmi);
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012907#if CONFIG_WARPED_MOTION
12908 obmc_rd += rs;
12909#endif
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012910#if CONFIG_VAR_TX
12911 memcpy(obmc_blk_skip, x->blk_skip[0], sizeof(obmc_blk_skip[0]) * n4);
12912#endif
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012913
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012914 // Compute the rd cost for ncobmc adaptive weight
12915 mbmi->motion_mode = NCOBMC_ADAPT_WEIGHT;
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012916 ncobmc_rd = get_prediction_rd_cost(cpi, x, mi_row, mi_col, &ncobmc_skip,
12917 &ncobmc_mbmi);
12918#if CONFIG_WARPED_MOTION
12919 ncobmc_rd += rs;
12920#endif
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012921 // Calculate the ncobmc mode costs
12922 {
12923 ADAPT_OVERLAP_BLOCK aob = adapt_overlap_block_lookup[bsize];
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012924 ncobmc_rd +=
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012925 RDCOST(x->rdmult, x->ncobmc_mode_cost[aob][mbmi->ncobmc_mode[0]], 0);
12926 if (mi_size_wide[bsize] != mi_size_high[bsize])
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012927 ncobmc_rd +=
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012928 RDCOST(x->rdmult, x->ncobmc_mode_cost[aob][mbmi->ncobmc_mode[1]], 0);
12929 }
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012930#if CONFIG_VAR_TX
12931 memcpy(ncobmc_blk_skip, x->blk_skip[0], sizeof(ncobmc_blk_skip[0]) * n4);
12932#endif
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012933
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012934#if CONFIG_WARPED_MOTION
12935 if (is_warp_motion) {
12936 mbmi->motion_mode = WARPED_CAUSAL;
12937 warp_rd =
12938 get_prediction_rd_cost(cpi, x, mi_row, mi_col, &warp_skip, &warp_mbmi);
Wei-Ting Lin3122b7d2017-08-30 17:26:58 -070012939 } else {
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012940 warp_rd = INT64_MAX;
12941 }
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012942#endif
Wei-Ting Lin5f8f7a12017-08-31 14:39:52 -070012943
12944#if CONFIG_WARPED_MOTION
12945 if (AOMMIN(ncobmc_rd, warp_rd) < AOMMIN(st_rd, obmc_rd)) {
12946 if (ncobmc_rd < warp_rd) {
12947 x->skip = ncobmc_skip;
12948 *mbmi = ncobmc_mbmi;
12949#if CONFIG_VAR_TX
12950 memcpy(x->blk_skip[0], ncobmc_blk_skip, sizeof(ncobmc_blk_skip[0]) * n4);
12951#endif
12952 } else {
12953 x->skip = warp_skip;
12954 *mbmi = warp_mbmi;
12955 }
12956#else
12957 if (ncobmc_rd < AOMMIN(st_rd, obmc_rd)) {
12958 x->skip = ncobmc_skip;
12959 *mbmi = ncobmc_mbmi;
12960#if CONFIG_VAR_TX
12961 memcpy(x->blk_skip[0], ncobmc_blk_skip, sizeof(ncobmc_blk_skip[0]) * n4);
12962#endif
12963#endif // CONFIG_WARPED_MOTION
12964 } else {
12965 if (obmc_rd < st_rd) {
12966 *mbmi = obmc_mbmi;
12967 x->skip = obmc_skip;
12968#if CONFIG_VAR_TX
12969 memcpy(x->blk_skip[0], obmc_blk_skip, sizeof(obmc_blk_skip[0]) * n4);
12970#endif
12971 } else {
12972 *mbmi = st_mbmi;
12973 x->skip = st_skip;
12974#if CONFIG_VAR_TX
12975 memcpy(x->blk_skip[0], st_blk_skip, sizeof(st_blk_skip[0]) * n4);
12976#endif
12977 }
Wei-Ting Linc5563a42017-08-03 12:45:43 -070012978 }
12979}
12980
12981int64_t get_ncobmc_error(MACROBLOCKD *xd, int pxl_row, int pxl_col,
12982 BLOCK_SIZE bsize, int plane, struct buf_2d *src) {
12983 const int wide = AOMMIN(mi_size_wide[bsize] * MI_SIZE,
12984 (xd->sb_mi_bd.mi_col_end + 1) * MI_SIZE - pxl_col);
12985 const int high = AOMMIN(mi_size_high[bsize] * MI_SIZE,
12986 (xd->sb_mi_bd.mi_row_end + 1) * MI_SIZE - pxl_row);
12987 const int ss_x = xd->plane[plane].subsampling_x;
12988 const int ss_y = xd->plane[plane].subsampling_y;
12989 int row_offset = (pxl_row - xd->sb_mi_bd.mi_row_begin * MI_SIZE) >> ss_y;
12990 int col_offset = (pxl_col - xd->sb_mi_bd.mi_col_begin * MI_SIZE) >> ss_x;
12991 int dst_stride = xd->ncobmc_pred_buf_stride[plane];
12992 int dst_offset = row_offset * dst_stride + col_offset;
12993 int src_stride = src->stride;
12994
12995 int r, c;
12996 int64_t tmp, error = 0;
12997
12998 for (r = 0; r < (high >> ss_y); ++r) {
12999 for (c = 0; c < (wide >> ss_x); ++c) {
13000 tmp = xd->ncobmc_pred_buf[plane][r * dst_stride + c + dst_offset] -
13001 src->buf[r * src_stride + c];
13002 error += tmp * tmp;
13003 }
13004 }
13005 return error;
13006}
13007
13008int get_ncobmc_mode(const AV1_COMP *const cpi, MACROBLOCK *const x,
13009 MACROBLOCKD *xd, int mi_row, int mi_col, int bsize) {
13010 const AV1_COMMON *const cm = &cpi->common;
13011#if CONFIG_HIGHBITDEPTH
13012 DECLARE_ALIGNED(16, uint8_t, tmp_buf_0[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
13013 DECLARE_ALIGNED(16, uint8_t, tmp_buf_1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
13014 DECLARE_ALIGNED(16, uint8_t, tmp_buf_2[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
13015 DECLARE_ALIGNED(16, uint8_t, tmp_buf_3[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
13016#else
13017 DECLARE_ALIGNED(16, uint8_t, tmp_buf_0[MAX_MB_PLANE * MAX_SB_SQUARE]);
13018 DECLARE_ALIGNED(16, uint8_t, tmp_buf_1[MAX_MB_PLANE * MAX_SB_SQUARE]);
13019 DECLARE_ALIGNED(16, uint8_t, tmp_buf_2[MAX_MB_PLANE * MAX_SB_SQUARE]);
13020 DECLARE_ALIGNED(16, uint8_t, tmp_buf_3[MAX_MB_PLANE * MAX_SB_SQUARE]);
13021#endif
13022 uint8_t *pred_buf[4][MAX_MB_PLANE];
13023
13024 // TODO(weitinglin): stride size needs to be fixed for high-bit depth
13025 int pred_stride[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
13026
13027 // target block in pxl
13028 int pxl_row = mi_row << MI_SIZE_LOG2;
13029 int pxl_col = mi_col << MI_SIZE_LOG2;
13030 int64_t error, best_error = INT64_MAX;
13031 int plane, tmp_mode, best_mode = 0;
13032#if CONFIG_HIGHBITDEPTH
13033 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
13034 int len = sizeof(uint16_t);
13035 ASSIGN_ALIGNED_PTRS_HBD(pred_buf[0], tmp_buf_0, MAX_SB_SQUARE, len);
13036 ASSIGN_ALIGNED_PTRS_HBD(pred_buf[1], tmp_buf_0, MAX_SB_SQUARE, len);
13037 ASSIGN_ALIGNED_PTRS_HBD(pred_buf[2], tmp_buf_0, MAX_SB_SQUARE, len);
13038 ASSIGN_ALIGNED_PTRS_HBD(pred_buf[3], tmp_buf_0, MAX_SB_SQUARE, len);
13039 } else {
13040#endif // CONFIG_HIGHBITDEPTH
13041 ASSIGN_ALIGNED_PTRS(pred_buf[0], tmp_buf_0, MAX_SB_SQUARE);
13042 ASSIGN_ALIGNED_PTRS(pred_buf[1], tmp_buf_1, MAX_SB_SQUARE);
13043 ASSIGN_ALIGNED_PTRS(pred_buf[2], tmp_buf_2, MAX_SB_SQUARE);
13044 ASSIGN_ALIGNED_PTRS(pred_buf[3], tmp_buf_3, MAX_SB_SQUARE);
13045#if CONFIG_HIGHBITDEPTH
13046 }
13047#endif
13048
13049 av1_get_ext_blk_preds(cm, xd, bsize, mi_row, mi_col, pred_buf, pred_stride);
13050 av1_get_ori_blk_pred(cm, xd, bsize, mi_row, mi_col, pred_buf[3], pred_stride);
13051
13052 for (tmp_mode = 0; tmp_mode < MAX_NCOBMC_MODES; ++tmp_mode) {
13053 error = 0;
13054 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
13055 build_ncobmc_intrpl_pred(cm, xd, plane, pxl_row, pxl_col, bsize, pred_buf,
13056 pred_stride, tmp_mode);
13057 error += get_ncobmc_error(xd, pxl_row, pxl_col, bsize, plane,
13058 &x->plane[plane].src);
13059 }
13060 if (error < best_error) {
13061 best_mode = tmp_mode;
13062 best_error = error;
13063 }
13064 }
13065
13066 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
13067 build_ncobmc_intrpl_pred(cm, xd, plane, pxl_row, pxl_col, bsize, pred_buf,
13068 pred_stride, best_mode);
13069 }
13070
13071 return best_mode;
13072}
13073
13074#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yue Chencb60b182016-10-13 15:18:22 -070013075#endif // CONFIG_MOTION_VAR