1 /*
2 * Copyright (c) 2020, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include "av1/common/pred_common.h"
13 #include "av1/encoder/compound_type.h"
14 #include "av1/encoder/encoder_alloc.h"
15 #include "av1/encoder/model_rd.h"
16 #include "av1/encoder/motion_search_facade.h"
17 #include "av1/encoder/rdopt_utils.h"
18 #include "av1/encoder/reconinter_enc.h"
19 #include "av1/encoder/tx_search.h"
20
21 typedef int64_t (*pick_interinter_mask_type)(
22 const AV1_COMP *const cpi, MACROBLOCK *x, const BLOCK_SIZE bsize,
23 const uint8_t *const p0, const uint8_t *const p1,
24 const int16_t *const residual1, const int16_t *const diff10,
25 uint64_t *best_sse);
26
27 // Checks if characteristics of search match
is_comp_rd_match(const AV1_COMP * const cpi,const MACROBLOCK * const x,const COMP_RD_STATS * st,const MB_MODE_INFO * const mi,int32_t * comp_rate,int64_t * comp_dist,int32_t * comp_model_rate,int64_t * comp_model_dist,int * comp_rs2)28 static INLINE int is_comp_rd_match(const AV1_COMP *const cpi,
29 const MACROBLOCK *const x,
30 const COMP_RD_STATS *st,
31 const MB_MODE_INFO *const mi,
32 int32_t *comp_rate, int64_t *comp_dist,
33 int32_t *comp_model_rate,
34 int64_t *comp_model_dist, int *comp_rs2) {
35 // TODO(ranjit): Ensure that compound type search use regular filter always
36 // and check if following check can be removed
37 // Check if interp filter matches with previous case
38 if (st->filter.as_int != mi->interp_filters.as_int) return 0;
39
40 const MACROBLOCKD *const xd = &x->e_mbd;
41 // Match MV and reference indices
42 for (int i = 0; i < 2; ++i) {
43 if ((st->ref_frames[i] != mi->ref_frame[i]) ||
44 (st->mv[i].as_int != mi->mv[i].as_int)) {
45 return 0;
46 }
47 const WarpedMotionParams *const wm = &xd->global_motion[mi->ref_frame[i]];
48 if (is_global_mv_block(mi, wm->wmtype) != st->is_global[i]) return 0;
49 }
50
51 int reuse_data[COMPOUND_TYPES] = { 1, 1, 0, 0 };
52 // For compound wedge, reuse data if newmv search is disabled when NEWMV is
53 // present or if NEWMV is not present in either of the directions
54 if ((!have_newmv_in_inter_mode(mi->mode) &&
55 !have_newmv_in_inter_mode(st->mode)) ||
56 (cpi->sf.inter_sf.disable_interinter_wedge_newmv_search))
57 reuse_data[COMPOUND_WEDGE] = 1;
58 // For compound diffwtd, reuse data if fast search is enabled (no newmv search
59 // when NEWMV is present) or if NEWMV is not present in either of the
60 // directions
61 if (cpi->sf.inter_sf.enable_fast_compound_mode_search ||
62 (!have_newmv_in_inter_mode(mi->mode) &&
63 !have_newmv_in_inter_mode(st->mode)))
64 reuse_data[COMPOUND_DIFFWTD] = 1;
65
66 // Store the stats for the different compound types
67 for (int comp_type = COMPOUND_AVERAGE; comp_type < COMPOUND_TYPES;
68 comp_type++) {
69 if (reuse_data[comp_type]) {
70 comp_rate[comp_type] = st->rate[comp_type];
71 comp_dist[comp_type] = st->dist[comp_type];
72 comp_model_rate[comp_type] = st->model_rate[comp_type];
73 comp_model_dist[comp_type] = st->model_dist[comp_type];
74 comp_rs2[comp_type] = st->comp_rs2[comp_type];
75 }
76 }
77 return 1;
78 }
79
80 // Checks if similar compound type search case is accounted earlier
81 // If found, returns relevant rd data
find_comp_rd_in_stats(const AV1_COMP * const cpi,const MACROBLOCK * x,const MB_MODE_INFO * const mbmi,int32_t * comp_rate,int64_t * comp_dist,int32_t * comp_model_rate,int64_t * comp_model_dist,int * comp_rs2,int * match_index)82 static INLINE int find_comp_rd_in_stats(const AV1_COMP *const cpi,
83 const MACROBLOCK *x,
84 const MB_MODE_INFO *const mbmi,
85 int32_t *comp_rate, int64_t *comp_dist,
86 int32_t *comp_model_rate,
87 int64_t *comp_model_dist, int *comp_rs2,
88 int *match_index) {
89 for (int j = 0; j < x->comp_rd_stats_idx; ++j) {
90 if (is_comp_rd_match(cpi, x, &x->comp_rd_stats[j], mbmi, comp_rate,
91 comp_dist, comp_model_rate, comp_model_dist,
92 comp_rs2)) {
93 *match_index = j;
94 return 1;
95 }
96 }
97 return 0; // no match result found
98 }
99
enable_wedge_search(MACROBLOCK * const x,const unsigned int disable_wedge_var_thresh)100 static INLINE bool enable_wedge_search(
101 MACROBLOCK *const x, const unsigned int disable_wedge_var_thresh) {
102 // Enable wedge search if source variance and edge strength are above
103 // the thresholds.
104 return x->source_variance > disable_wedge_var_thresh;
105 }
106
enable_wedge_interinter_search(MACROBLOCK * const x,const AV1_COMP * const cpi)107 static INLINE bool enable_wedge_interinter_search(MACROBLOCK *const x,
108 const AV1_COMP *const cpi) {
109 return enable_wedge_search(
110 x, cpi->sf.inter_sf.disable_interinter_wedge_var_thresh) &&
111 cpi->oxcf.comp_type_cfg.enable_interinter_wedge;
112 }
113
enable_wedge_interintra_search(MACROBLOCK * const x,const AV1_COMP * const cpi)114 static INLINE bool enable_wedge_interintra_search(MACROBLOCK *const x,
115 const AV1_COMP *const cpi) {
116 return enable_wedge_search(
117 x, cpi->sf.inter_sf.disable_interintra_wedge_var_thresh) &&
118 cpi->oxcf.comp_type_cfg.enable_interintra_wedge;
119 }
120
estimate_wedge_sign(const AV1_COMP * cpi,const MACROBLOCK * x,const BLOCK_SIZE bsize,const uint8_t * pred0,int stride0,const uint8_t * pred1,int stride1)121 static int8_t estimate_wedge_sign(const AV1_COMP *cpi, const MACROBLOCK *x,
122 const BLOCK_SIZE bsize, const uint8_t *pred0,
123 int stride0, const uint8_t *pred1,
124 int stride1) {
125 static const BLOCK_SIZE split_qtr[BLOCK_SIZES_ALL] = {
126 // 4X4
127 BLOCK_INVALID,
128 // 4X8, 8X4, 8X8
129 BLOCK_INVALID, BLOCK_INVALID, BLOCK_4X4,
130 // 8X16, 16X8, 16X16
131 BLOCK_4X8, BLOCK_8X4, BLOCK_8X8,
132 // 16X32, 32X16, 32X32
133 BLOCK_8X16, BLOCK_16X8, BLOCK_16X16,
134 // 32X64, 64X32, 64X64
135 BLOCK_16X32, BLOCK_32X16, BLOCK_32X32,
136 // 64x128, 128x64, 128x128
137 BLOCK_32X64, BLOCK_64X32, BLOCK_64X64,
138 // 4X16, 16X4, 8X32
139 BLOCK_INVALID, BLOCK_INVALID, BLOCK_4X16,
140 // 32X8, 16X64, 64X16
141 BLOCK_16X4, BLOCK_8X32, BLOCK_32X8
142 };
143 const struct macroblock_plane *const p = &x->plane[0];
144 const uint8_t *src = p->src.buf;
145 int src_stride = p->src.stride;
146 const int bw = block_size_wide[bsize];
147 const int bh = block_size_high[bsize];
148 const int bw_by2 = bw >> 1;
149 const int bh_by2 = bh >> 1;
150 uint32_t esq[2][2];
151 int64_t tl, br;
152
153 const BLOCK_SIZE f_index = split_qtr[bsize];
154 assert(f_index != BLOCK_INVALID);
155
156 if (is_cur_buf_hbd(&x->e_mbd)) {
157 pred0 = CONVERT_TO_BYTEPTR(pred0);
158 pred1 = CONVERT_TO_BYTEPTR(pred1);
159 }
160
161 // Residual variance computation over relevant quandrants in order to
162 // find TL + BR, TL = sum(1st,2nd,3rd) quadrants of (pred0 - pred1),
163 // BR = sum(2nd,3rd,4th) quadrants of (pred1 - pred0)
164 // The 2nd and 3rd quadrants cancel out in TL + BR
165 // Hence TL + BR = 1st quadrant of (pred0-pred1) + 4th of (pred1-pred0)
166 // TODO(nithya): Sign estimation assumes 45 degrees (1st and 4th quadrants)
167 // for all codebooks; experiment with other quadrant combinations for
168 // 0, 90 and 135 degrees also.
169 cpi->ppi->fn_ptr[f_index].vf(src, src_stride, pred0, stride0, &esq[0][0]);
170 cpi->ppi->fn_ptr[f_index].vf(src + bh_by2 * src_stride + bw_by2, src_stride,
171 pred0 + bh_by2 * stride0 + bw_by2, stride0,
172 &esq[0][1]);
173 cpi->ppi->fn_ptr[f_index].vf(src, src_stride, pred1, stride1, &esq[1][0]);
174 cpi->ppi->fn_ptr[f_index].vf(src + bh_by2 * src_stride + bw_by2, src_stride,
175 pred1 + bh_by2 * stride1 + bw_by2, stride0,
176 &esq[1][1]);
177
178 tl = ((int64_t)esq[0][0]) - ((int64_t)esq[1][0]);
179 br = ((int64_t)esq[1][1]) - ((int64_t)esq[0][1]);
180 return (tl + br > 0);
181 }
182
183 // Choose the best wedge index and sign
pick_wedge(const AV1_COMP * const cpi,const MACROBLOCK * const x,const BLOCK_SIZE bsize,const uint8_t * const p0,const int16_t * const residual1,const int16_t * const diff10,int8_t * const best_wedge_sign,int8_t * const best_wedge_index,uint64_t * best_sse)184 static int64_t pick_wedge(const AV1_COMP *const cpi, const MACROBLOCK *const x,
185 const BLOCK_SIZE bsize, const uint8_t *const p0,
186 const int16_t *const residual1,
187 const int16_t *const diff10,
188 int8_t *const best_wedge_sign,
189 int8_t *const best_wedge_index, uint64_t *best_sse) {
190 const MACROBLOCKD *const xd = &x->e_mbd;
191 const struct buf_2d *const src = &x->plane[0].src;
192 const int bw = block_size_wide[bsize];
193 const int bh = block_size_high[bsize];
194 const int N = bw * bh;
195 assert(N >= 64);
196 int rate;
197 int64_t dist;
198 int64_t rd, best_rd = INT64_MAX;
199 int8_t wedge_index;
200 int8_t wedge_sign;
201 const int8_t wedge_types = get_wedge_types_lookup(bsize);
202 const uint8_t *mask;
203 uint64_t sse;
204 const int hbd = is_cur_buf_hbd(xd);
205 const int bd_round = hbd ? (xd->bd - 8) * 2 : 0;
206
207 DECLARE_ALIGNED(32, int16_t, residual0[MAX_SB_SQUARE]); // src - pred0
208 #if CONFIG_AV1_HIGHBITDEPTH
209 if (hbd) {
210 aom_highbd_subtract_block(bh, bw, residual0, bw, src->buf, src->stride,
211 CONVERT_TO_BYTEPTR(p0), bw, xd->bd);
212 } else {
213 aom_subtract_block(bh, bw, residual0, bw, src->buf, src->stride, p0, bw);
214 }
215 #else
216 (void)hbd;
217 aom_subtract_block(bh, bw, residual0, bw, src->buf, src->stride, p0, bw);
218 #endif
219
220 int64_t sign_limit = ((int64_t)aom_sum_squares_i16(residual0, N) -
221 (int64_t)aom_sum_squares_i16(residual1, N)) *
222 (1 << WEDGE_WEIGHT_BITS) / 2;
223 int16_t *ds = residual0;
224
225 av1_wedge_compute_delta_squares(ds, residual0, residual1, N);
226
227 for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) {
228 mask = av1_get_contiguous_soft_mask(wedge_index, 0, bsize);
229
230 wedge_sign = av1_wedge_sign_from_residuals(ds, mask, N, sign_limit);
231
232 mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize);
233 sse = av1_wedge_sse_from_residuals(residual1, diff10, mask, N);
234 sse = ROUND_POWER_OF_TWO(sse, bd_round);
235
236 model_rd_sse_fn[MODELRD_TYPE_MASKED_COMPOUND](cpi, x, bsize, 0, sse, N,
237 &rate, &dist);
238 // int rate2;
239 // int64_t dist2;
240 // model_rd_with_curvfit(cpi, x, bsize, 0, sse, N, &rate2, &dist2);
241 // printf("sse %"PRId64": leagacy: %d %"PRId64", curvfit %d %"PRId64"\n",
242 // sse, rate, dist, rate2, dist2); dist = dist2;
243 // rate = rate2;
244
245 rate += x->mode_costs.wedge_idx_cost[bsize][wedge_index];
246 rd = RDCOST(x->rdmult, rate, dist);
247
248 if (rd < best_rd) {
249 *best_wedge_index = wedge_index;
250 *best_wedge_sign = wedge_sign;
251 best_rd = rd;
252 *best_sse = sse;
253 }
254 }
255
256 return best_rd -
257 RDCOST(x->rdmult,
258 x->mode_costs.wedge_idx_cost[bsize][*best_wedge_index], 0);
259 }
260
261 // Choose the best wedge index the specified sign
pick_wedge_fixed_sign(const AV1_COMP * const cpi,const MACROBLOCK * const x,const BLOCK_SIZE bsize,const int16_t * const residual1,const int16_t * const diff10,const int8_t wedge_sign,int8_t * const best_wedge_index,uint64_t * best_sse)262 static int64_t pick_wedge_fixed_sign(
263 const AV1_COMP *const cpi, const MACROBLOCK *const x,
264 const BLOCK_SIZE bsize, const int16_t *const residual1,
265 const int16_t *const diff10, const int8_t wedge_sign,
266 int8_t *const best_wedge_index, uint64_t *best_sse) {
267 const MACROBLOCKD *const xd = &x->e_mbd;
268
269 const int bw = block_size_wide[bsize];
270 const int bh = block_size_high[bsize];
271 const int N = bw * bh;
272 assert(N >= 64);
273 int rate;
274 int64_t dist;
275 int64_t rd, best_rd = INT64_MAX;
276 int8_t wedge_index;
277 const int8_t wedge_types = get_wedge_types_lookup(bsize);
278 const uint8_t *mask;
279 uint64_t sse;
280 const int hbd = is_cur_buf_hbd(xd);
281 const int bd_round = hbd ? (xd->bd - 8) * 2 : 0;
282 for (wedge_index = 0; wedge_index < wedge_types; ++wedge_index) {
283 mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize);
284 sse = av1_wedge_sse_from_residuals(residual1, diff10, mask, N);
285 sse = ROUND_POWER_OF_TWO(sse, bd_round);
286
287 model_rd_sse_fn[MODELRD_TYPE_MASKED_COMPOUND](cpi, x, bsize, 0, sse, N,
288 &rate, &dist);
289 rate += x->mode_costs.wedge_idx_cost[bsize][wedge_index];
290 rd = RDCOST(x->rdmult, rate, dist);
291
292 if (rd < best_rd) {
293 *best_wedge_index = wedge_index;
294 best_rd = rd;
295 *best_sse = sse;
296 }
297 }
298 return best_rd -
299 RDCOST(x->rdmult,
300 x->mode_costs.wedge_idx_cost[bsize][*best_wedge_index], 0);
301 }
302
pick_interinter_wedge(const AV1_COMP * const cpi,MACROBLOCK * const x,const BLOCK_SIZE bsize,const uint8_t * const p0,const uint8_t * const p1,const int16_t * const residual1,const int16_t * const diff10,uint64_t * best_sse)303 static int64_t pick_interinter_wedge(
304 const AV1_COMP *const cpi, MACROBLOCK *const x, const BLOCK_SIZE bsize,
305 const uint8_t *const p0, const uint8_t *const p1,
306 const int16_t *const residual1, const int16_t *const diff10,
307 uint64_t *best_sse) {
308 MACROBLOCKD *const xd = &x->e_mbd;
309 MB_MODE_INFO *const mbmi = xd->mi[0];
310 const int bw = block_size_wide[bsize];
311
312 int64_t rd;
313 int8_t wedge_index = -1;
314 int8_t wedge_sign = 0;
315
316 assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
317 assert(cpi->common.seq_params->enable_masked_compound);
318
319 if (cpi->sf.inter_sf.fast_wedge_sign_estimate) {
320 wedge_sign = estimate_wedge_sign(cpi, x, bsize, p0, bw, p1, bw);
321 rd = pick_wedge_fixed_sign(cpi, x, bsize, residual1, diff10, wedge_sign,
322 &wedge_index, best_sse);
323 } else {
324 rd = pick_wedge(cpi, x, bsize, p0, residual1, diff10, &wedge_sign,
325 &wedge_index, best_sse);
326 }
327
328 mbmi->interinter_comp.wedge_sign = wedge_sign;
329 mbmi->interinter_comp.wedge_index = wedge_index;
330 return rd;
331 }
332
pick_interinter_seg(const AV1_COMP * const cpi,MACROBLOCK * const x,const BLOCK_SIZE bsize,const uint8_t * const p0,const uint8_t * const p1,const int16_t * const residual1,const int16_t * const diff10,uint64_t * best_sse)333 static int64_t pick_interinter_seg(const AV1_COMP *const cpi,
334 MACROBLOCK *const x, const BLOCK_SIZE bsize,
335 const uint8_t *const p0,
336 const uint8_t *const p1,
337 const int16_t *const residual1,
338 const int16_t *const diff10,
339 uint64_t *best_sse) {
340 MACROBLOCKD *const xd = &x->e_mbd;
341 MB_MODE_INFO *const mbmi = xd->mi[0];
342 const int bw = block_size_wide[bsize];
343 const int bh = block_size_high[bsize];
344 const int N = 1 << num_pels_log2_lookup[bsize];
345 int rate;
346 int64_t dist;
347 DIFFWTD_MASK_TYPE cur_mask_type;
348 int64_t best_rd = INT64_MAX;
349 DIFFWTD_MASK_TYPE best_mask_type = 0;
350 const int hbd = is_cur_buf_hbd(xd);
351 const int bd_round = hbd ? (xd->bd - 8) * 2 : 0;
352 DECLARE_ALIGNED(16, uint8_t, seg_mask[2 * MAX_SB_SQUARE]);
353 uint8_t *tmp_mask[2] = { xd->seg_mask, seg_mask };
354 // try each mask type and its inverse
355 for (cur_mask_type = 0; cur_mask_type < DIFFWTD_MASK_TYPES; cur_mask_type++) {
356 // build mask and inverse
357 if (hbd)
358 av1_build_compound_diffwtd_mask_highbd(
359 tmp_mask[cur_mask_type], cur_mask_type, CONVERT_TO_BYTEPTR(p0), bw,
360 CONVERT_TO_BYTEPTR(p1), bw, bh, bw, xd->bd);
361 else
362 av1_build_compound_diffwtd_mask(tmp_mask[cur_mask_type], cur_mask_type,
363 p0, bw, p1, bw, bh, bw);
364
365 // compute rd for mask
366 uint64_t sse = av1_wedge_sse_from_residuals(residual1, diff10,
367 tmp_mask[cur_mask_type], N);
368 sse = ROUND_POWER_OF_TWO(sse, bd_round);
369
370 model_rd_sse_fn[MODELRD_TYPE_MASKED_COMPOUND](cpi, x, bsize, 0, sse, N,
371 &rate, &dist);
372 const int64_t rd0 = RDCOST(x->rdmult, rate, dist);
373
374 if (rd0 < best_rd) {
375 best_mask_type = cur_mask_type;
376 best_rd = rd0;
377 *best_sse = sse;
378 }
379 }
380 mbmi->interinter_comp.mask_type = best_mask_type;
381 if (best_mask_type == DIFFWTD_38_INV) {
382 memcpy(xd->seg_mask, seg_mask, N * 2);
383 }
384 return best_rd;
385 }
386
pick_interintra_wedge(const AV1_COMP * const cpi,const MACROBLOCK * const x,const BLOCK_SIZE bsize,const uint8_t * const p0,const uint8_t * const p1)387 static int64_t pick_interintra_wedge(const AV1_COMP *const cpi,
388 const MACROBLOCK *const x,
389 const BLOCK_SIZE bsize,
390 const uint8_t *const p0,
391 const uint8_t *const p1) {
392 const MACROBLOCKD *const xd = &x->e_mbd;
393 MB_MODE_INFO *const mbmi = xd->mi[0];
394 assert(av1_is_wedge_used(bsize));
395 assert(cpi->common.seq_params->enable_interintra_compound);
396
397 const struct buf_2d *const src = &x->plane[0].src;
398 const int bw = block_size_wide[bsize];
399 const int bh = block_size_high[bsize];
400 DECLARE_ALIGNED(32, int16_t, residual1[MAX_SB_SQUARE]); // src - pred1
401 DECLARE_ALIGNED(32, int16_t, diff10[MAX_SB_SQUARE]); // pred1 - pred0
402 #if CONFIG_AV1_HIGHBITDEPTH
403 if (is_cur_buf_hbd(xd)) {
404 aom_highbd_subtract_block(bh, bw, residual1, bw, src->buf, src->stride,
405 CONVERT_TO_BYTEPTR(p1), bw, xd->bd);
406 aom_highbd_subtract_block(bh, bw, diff10, bw, CONVERT_TO_BYTEPTR(p1), bw,
407 CONVERT_TO_BYTEPTR(p0), bw, xd->bd);
408 } else {
409 aom_subtract_block(bh, bw, residual1, bw, src->buf, src->stride, p1, bw);
410 aom_subtract_block(bh, bw, diff10, bw, p1, bw, p0, bw);
411 }
412 #else
413 aom_subtract_block(bh, bw, residual1, bw, src->buf, src->stride, p1, bw);
414 aom_subtract_block(bh, bw, diff10, bw, p1, bw, p0, bw);
415 #endif
416 int8_t wedge_index = -1;
417 uint64_t sse;
418 int64_t rd = pick_wedge_fixed_sign(cpi, x, bsize, residual1, diff10, 0,
419 &wedge_index, &sse);
420
421 mbmi->interintra_wedge_index = wedge_index;
422 return rd;
423 }
424
get_inter_predictors_masked_compound(MACROBLOCK * x,const BLOCK_SIZE bsize,uint8_t ** preds0,uint8_t ** preds1,int16_t * residual1,int16_t * diff10,int * strides)425 static AOM_INLINE void get_inter_predictors_masked_compound(
426 MACROBLOCK *x, const BLOCK_SIZE bsize, uint8_t **preds0, uint8_t **preds1,
427 int16_t *residual1, int16_t *diff10, int *strides) {
428 MACROBLOCKD *xd = &x->e_mbd;
429 const int bw = block_size_wide[bsize];
430 const int bh = block_size_high[bsize];
431 // get inter predictors to use for masked compound modes
432 av1_build_inter_predictors_for_planes_single_buf(xd, bsize, 0, 0, 0, preds0,
433 strides);
434 av1_build_inter_predictors_for_planes_single_buf(xd, bsize, 0, 0, 1, preds1,
435 strides);
436 const struct buf_2d *const src = &x->plane[0].src;
437 #if CONFIG_AV1_HIGHBITDEPTH
438 if (is_cur_buf_hbd(xd)) {
439 aom_highbd_subtract_block(bh, bw, residual1, bw, src->buf, src->stride,
440 CONVERT_TO_BYTEPTR(*preds1), bw, xd->bd);
441 aom_highbd_subtract_block(bh, bw, diff10, bw, CONVERT_TO_BYTEPTR(*preds1),
442 bw, CONVERT_TO_BYTEPTR(*preds0), bw, xd->bd);
443 } else {
444 aom_subtract_block(bh, bw, residual1, bw, src->buf, src->stride, *preds1,
445 bw);
446 aom_subtract_block(bh, bw, diff10, bw, *preds1, bw, *preds0, bw);
447 }
448 #else
449 aom_subtract_block(bh, bw, residual1, bw, src->buf, src->stride, *preds1, bw);
450 aom_subtract_block(bh, bw, diff10, bw, *preds1, bw, *preds0, bw);
451 #endif
452 }
453
454 // Computes the rd cost for the given interintra mode and updates the best
compute_best_interintra_mode(const AV1_COMP * const cpi,MB_MODE_INFO * mbmi,MACROBLOCKD * xd,MACROBLOCK * const x,const int * const interintra_mode_cost,const BUFFER_SET * orig_dst,uint8_t * intrapred,const uint8_t * tmp_buf,INTERINTRA_MODE * best_interintra_mode,int64_t * best_interintra_rd,INTERINTRA_MODE interintra_mode,BLOCK_SIZE bsize)455 static INLINE void compute_best_interintra_mode(
456 const AV1_COMP *const cpi, MB_MODE_INFO *mbmi, MACROBLOCKD *xd,
457 MACROBLOCK *const x, const int *const interintra_mode_cost,
458 const BUFFER_SET *orig_dst, uint8_t *intrapred, const uint8_t *tmp_buf,
459 INTERINTRA_MODE *best_interintra_mode, int64_t *best_interintra_rd,
460 INTERINTRA_MODE interintra_mode, BLOCK_SIZE bsize) {
461 const AV1_COMMON *const cm = &cpi->common;
462 int rate, skip_txfm_sb;
463 int64_t dist, skip_sse_sb;
464 const int bw = block_size_wide[bsize];
465 mbmi->interintra_mode = interintra_mode;
466 int rmode = interintra_mode_cost[interintra_mode];
467 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, orig_dst,
468 intrapred, bw);
469 av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw);
470 model_rd_sb_fn[MODELRD_TYPE_INTERINTRA](cpi, bsize, x, xd, 0, 0, &rate, &dist,
471 &skip_txfm_sb, &skip_sse_sb, NULL,
472 NULL, NULL);
473 int64_t rd = RDCOST(x->rdmult, rate + rmode, dist);
474 if (rd < *best_interintra_rd) {
475 *best_interintra_rd = rd;
476 *best_interintra_mode = mbmi->interintra_mode;
477 }
478 }
479
estimate_yrd_for_sb(const AV1_COMP * const cpi,BLOCK_SIZE bs,MACROBLOCK * x,int64_t ref_best_rd,RD_STATS * rd_stats)480 static int64_t estimate_yrd_for_sb(const AV1_COMP *const cpi, BLOCK_SIZE bs,
481 MACROBLOCK *x, int64_t ref_best_rd,
482 RD_STATS *rd_stats) {
483 MACROBLOCKD *const xd = &x->e_mbd;
484 if (ref_best_rd < 0) return INT64_MAX;
485 av1_subtract_plane(x, bs, 0);
486 const int64_t rd = av1_estimate_txfm_yrd(cpi, x, rd_stats, ref_best_rd, bs,
487 max_txsize_rect_lookup[bs]);
488 if (rd != INT64_MAX) {
489 const int skip_ctx = av1_get_skip_txfm_context(xd);
490 if (rd_stats->skip_txfm) {
491 const int s1 = x->mode_costs.skip_txfm_cost[skip_ctx][1];
492 rd_stats->rate = s1;
493 } else {
494 const int s0 = x->mode_costs.skip_txfm_cost[skip_ctx][0];
495 rd_stats->rate += s0;
496 }
497 }
498 return rd;
499 }
500
501 // Computes the rd_threshold for smooth interintra rd search.
compute_rd_thresh(MACROBLOCK * const x,int total_mode_rate,int64_t ref_best_rd)502 static AOM_INLINE int64_t compute_rd_thresh(MACROBLOCK *const x,
503 int total_mode_rate,
504 int64_t ref_best_rd) {
505 const int64_t rd_thresh = get_rd_thresh_from_best_rd(
506 ref_best_rd, (1 << INTER_INTRA_RD_THRESH_SHIFT),
507 INTER_INTRA_RD_THRESH_SCALE);
508 const int64_t mode_rd = RDCOST(x->rdmult, total_mode_rate, 0);
509 return (rd_thresh - mode_rd);
510 }
511
512 // Computes the best wedge interintra mode
compute_best_wedge_interintra(const AV1_COMP * const cpi,MB_MODE_INFO * mbmi,MACROBLOCKD * xd,MACROBLOCK * const x,const int * const interintra_mode_cost,const BUFFER_SET * orig_dst,uint8_t * intrapred_,uint8_t * tmp_buf_,int * best_mode,int * best_wedge_index,BLOCK_SIZE bsize)513 static AOM_INLINE int64_t compute_best_wedge_interintra(
514 const AV1_COMP *const cpi, MB_MODE_INFO *mbmi, MACROBLOCKD *xd,
515 MACROBLOCK *const x, const int *const interintra_mode_cost,
516 const BUFFER_SET *orig_dst, uint8_t *intrapred_, uint8_t *tmp_buf_,
517 int *best_mode, int *best_wedge_index, BLOCK_SIZE bsize) {
518 const AV1_COMMON *const cm = &cpi->common;
519 const int bw = block_size_wide[bsize];
520 int64_t best_interintra_rd_wedge = INT64_MAX;
521 int64_t best_total_rd = INT64_MAX;
522 uint8_t *intrapred = get_buf_by_bd(xd, intrapred_);
523 for (INTERINTRA_MODE mode = 0; mode < INTERINTRA_MODES; ++mode) {
524 mbmi->interintra_mode = mode;
525 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, orig_dst,
526 intrapred, bw);
527 int64_t rd = pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_);
528 const int rate_overhead =
529 interintra_mode_cost[mode] +
530 x->mode_costs.wedge_idx_cost[bsize][mbmi->interintra_wedge_index];
531 const int64_t total_rd = rd + RDCOST(x->rdmult, rate_overhead, 0);
532 if (total_rd < best_total_rd) {
533 best_total_rd = total_rd;
534 best_interintra_rd_wedge = rd;
535 *best_mode = mbmi->interintra_mode;
536 *best_wedge_index = mbmi->interintra_wedge_index;
537 }
538 }
539 return best_interintra_rd_wedge;
540 }
541
handle_smooth_inter_intra_mode(const AV1_COMP * const cpi,MACROBLOCK * const x,BLOCK_SIZE bsize,MB_MODE_INFO * mbmi,int64_t ref_best_rd,int * rate_mv,INTERINTRA_MODE * best_interintra_mode,int64_t * best_rd,int * best_mode_rate,const BUFFER_SET * orig_dst,uint8_t * tmp_buf,uint8_t * intrapred,HandleInterModeArgs * args)542 static int handle_smooth_inter_intra_mode(
543 const AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize,
544 MB_MODE_INFO *mbmi, int64_t ref_best_rd, int *rate_mv,
545 INTERINTRA_MODE *best_interintra_mode, int64_t *best_rd,
546 int *best_mode_rate, const BUFFER_SET *orig_dst, uint8_t *tmp_buf,
547 uint8_t *intrapred, HandleInterModeArgs *args) {
548 MACROBLOCKD *xd = &x->e_mbd;
549 const ModeCosts *mode_costs = &x->mode_costs;
550 const int *const interintra_mode_cost =
551 mode_costs->interintra_mode_cost[size_group_lookup[bsize]];
552 const AV1_COMMON *const cm = &cpi->common;
553 const int bw = block_size_wide[bsize];
554
555 mbmi->use_wedge_interintra = 0;
556
557 if (cpi->sf.inter_sf.reuse_inter_intra_mode == 0 ||
558 *best_interintra_mode == INTERINTRA_MODES) {
559 int64_t best_interintra_rd = INT64_MAX;
560 for (INTERINTRA_MODE cur_mode = 0; cur_mode < INTERINTRA_MODES;
561 ++cur_mode) {
562 if ((!cpi->oxcf.intra_mode_cfg.enable_smooth_intra ||
563 cpi->sf.intra_sf.disable_smooth_intra) &&
564 cur_mode == II_SMOOTH_PRED)
565 continue;
566 compute_best_interintra_mode(
567 cpi, mbmi, xd, x, interintra_mode_cost, orig_dst, intrapred, tmp_buf,
568 best_interintra_mode, &best_interintra_rd, cur_mode, bsize);
569 }
570 args->inter_intra_mode[mbmi->ref_frame[0]] = *best_interintra_mode;
571 }
572 assert(IMPLIES(!cpi->oxcf.comp_type_cfg.enable_smooth_interintra,
573 *best_interintra_mode != II_SMOOTH_PRED));
574 // Recompute prediction if required
575 bool interintra_mode_reuse = cpi->sf.inter_sf.reuse_inter_intra_mode ||
576 *best_interintra_mode != INTERINTRA_MODES;
577 if (interintra_mode_reuse || *best_interintra_mode != INTERINTRA_MODES - 1) {
578 mbmi->interintra_mode = *best_interintra_mode;
579 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, orig_dst,
580 intrapred, bw);
581 av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw);
582 }
583
584 // Compute rd cost for best smooth_interintra
585 RD_STATS rd_stats;
586 const int is_wedge_used = av1_is_wedge_used(bsize);
587 const int rmode =
588 interintra_mode_cost[*best_interintra_mode] +
589 (is_wedge_used ? mode_costs->wedge_interintra_cost[bsize][0] : 0);
590 const int total_mode_rate = rmode + *rate_mv;
591 const int64_t rd_thresh = compute_rd_thresh(x, total_mode_rate, ref_best_rd);
592 int64_t rd = estimate_yrd_for_sb(cpi, bsize, x, rd_thresh, &rd_stats);
593 if (rd != INT64_MAX) {
594 rd = RDCOST(x->rdmult, total_mode_rate + rd_stats.rate, rd_stats.dist);
595 } else {
596 return IGNORE_MODE;
597 }
598 *best_rd = rd;
599 *best_mode_rate = rmode;
600 // Return early if best rd not good enough
601 if (ref_best_rd < INT64_MAX &&
602 (*best_rd >> INTER_INTRA_RD_THRESH_SHIFT) * INTER_INTRA_RD_THRESH_SCALE >
603 ref_best_rd) {
604 return IGNORE_MODE;
605 }
606 return 0;
607 }
608
handle_wedge_inter_intra_mode(const AV1_COMP * const cpi,MACROBLOCK * const x,BLOCK_SIZE bsize,MB_MODE_INFO * mbmi,int * rate_mv,INTERINTRA_MODE * best_interintra_mode,int64_t * best_rd,const BUFFER_SET * orig_dst,uint8_t * tmp_buf_,uint8_t * tmp_buf,uint8_t * intrapred_,uint8_t * intrapred,HandleInterModeArgs * args,int * tmp_rate_mv,int * rate_overhead,int_mv * tmp_mv,int64_t best_rd_no_wedge)609 static int handle_wedge_inter_intra_mode(
610 const AV1_COMP *const cpi, MACROBLOCK *const x, BLOCK_SIZE bsize,
611 MB_MODE_INFO *mbmi, int *rate_mv, INTERINTRA_MODE *best_interintra_mode,
612 int64_t *best_rd, const BUFFER_SET *orig_dst, uint8_t *tmp_buf_,
613 uint8_t *tmp_buf, uint8_t *intrapred_, uint8_t *intrapred,
614 HandleInterModeArgs *args, int *tmp_rate_mv, int *rate_overhead,
615 int_mv *tmp_mv, int64_t best_rd_no_wedge) {
616 MACROBLOCKD *xd = &x->e_mbd;
617 const ModeCosts *mode_costs = &x->mode_costs;
618 const int *const interintra_mode_cost =
619 mode_costs->interintra_mode_cost[size_group_lookup[bsize]];
620 const AV1_COMMON *const cm = &cpi->common;
621 const int bw = block_size_wide[bsize];
622 const int try_smooth_interintra =
623 cpi->oxcf.comp_type_cfg.enable_smooth_interintra;
624
625 mbmi->use_wedge_interintra = 1;
626
627 if (!cpi->sf.inter_sf.fast_interintra_wedge_search) {
628 // Exhaustive search of all wedge and mode combinations.
629 int best_mode = 0;
630 int best_wedge_index = 0;
631 *best_rd = compute_best_wedge_interintra(
632 cpi, mbmi, xd, x, interintra_mode_cost, orig_dst, intrapred_, tmp_buf_,
633 &best_mode, &best_wedge_index, bsize);
634 mbmi->interintra_mode = best_mode;
635 mbmi->interintra_wedge_index = best_wedge_index;
636 if (best_mode != INTERINTRA_MODES - 1) {
637 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, orig_dst,
638 intrapred, bw);
639 }
640 } else if (!try_smooth_interintra) {
641 if (*best_interintra_mode == INTERINTRA_MODES) {
642 mbmi->interintra_mode = INTERINTRA_MODES - 1;
643 *best_interintra_mode = INTERINTRA_MODES - 1;
644 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, orig_dst,
645 intrapred, bw);
646 // Pick wedge mask based on INTERINTRA_MODES - 1
647 *best_rd = pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_);
648 // Find the best interintra mode for the chosen wedge mask
649 for (INTERINTRA_MODE cur_mode = 0; cur_mode < INTERINTRA_MODES;
650 ++cur_mode) {
651 compute_best_interintra_mode(
652 cpi, mbmi, xd, x, interintra_mode_cost, orig_dst, intrapred,
653 tmp_buf, best_interintra_mode, best_rd, cur_mode, bsize);
654 }
655 args->inter_intra_mode[mbmi->ref_frame[0]] = *best_interintra_mode;
656 mbmi->interintra_mode = *best_interintra_mode;
657
658 // Recompute prediction if required
659 if (*best_interintra_mode != INTERINTRA_MODES - 1) {
660 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, orig_dst,
661 intrapred, bw);
662 }
663 } else {
664 // Pick wedge mask for the best interintra mode (reused)
665 mbmi->interintra_mode = *best_interintra_mode;
666 av1_build_intra_predictors_for_interintra(cm, xd, bsize, 0, orig_dst,
667 intrapred, bw);
668 *best_rd = pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_);
669 }
670 } else {
671 // Pick wedge mask for the best interintra mode from smooth_interintra
672 *best_rd = pick_interintra_wedge(cpi, x, bsize, intrapred_, tmp_buf_);
673 }
674
675 *rate_overhead =
676 interintra_mode_cost[mbmi->interintra_mode] +
677 mode_costs->wedge_idx_cost[bsize][mbmi->interintra_wedge_index] +
678 mode_costs->wedge_interintra_cost[bsize][1];
679 *best_rd += RDCOST(x->rdmult, *rate_overhead + *rate_mv, 0);
680
681 int64_t rd = INT64_MAX;
682 const int_mv mv0 = mbmi->mv[0];
683 // Refine motion vector for NEWMV case.
684 if (have_newmv_in_inter_mode(mbmi->mode)) {
685 int rate_sum, skip_txfm_sb;
686 int64_t dist_sum, skip_sse_sb;
687 // get negative of mask
688 const uint8_t *mask =
689 av1_get_contiguous_soft_mask(mbmi->interintra_wedge_index, 1, bsize);
690 av1_compound_single_motion_search(cpi, x, bsize, &tmp_mv->as_mv, intrapred,
691 mask, bw, tmp_rate_mv, 0);
692 if (mbmi->mv[0].as_int != tmp_mv->as_int) {
693 mbmi->mv[0].as_int = tmp_mv->as_int;
694 // Set ref_frame[1] to NONE_FRAME temporarily so that the intra
695 // predictor is not calculated again in av1_enc_build_inter_predictor().
696 mbmi->ref_frame[1] = NONE_FRAME;
697 const int mi_row = xd->mi_row;
698 const int mi_col = xd->mi_col;
699 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
700 AOM_PLANE_Y, AOM_PLANE_Y);
701 mbmi->ref_frame[1] = INTRA_FRAME;
702 av1_combine_interintra(xd, bsize, 0, xd->plane[AOM_PLANE_Y].dst.buf,
703 xd->plane[AOM_PLANE_Y].dst.stride, intrapred, bw);
704 model_rd_sb_fn[MODELRD_TYPE_MASKED_COMPOUND](
705 cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, &skip_txfm_sb,
706 &skip_sse_sb, NULL, NULL, NULL);
707 rd =
708 RDCOST(x->rdmult, *tmp_rate_mv + *rate_overhead + rate_sum, dist_sum);
709 }
710 }
711 if (rd >= *best_rd) {
712 tmp_mv->as_int = mv0.as_int;
713 *tmp_rate_mv = *rate_mv;
714 av1_combine_interintra(xd, bsize, 0, tmp_buf, bw, intrapred, bw);
715 }
716 // Evaluate closer to true rd
717 RD_STATS rd_stats;
718 const int64_t mode_rd = RDCOST(x->rdmult, *rate_overhead + *tmp_rate_mv, 0);
719 const int64_t tmp_rd_thresh = best_rd_no_wedge - mode_rd;
720 rd = estimate_yrd_for_sb(cpi, bsize, x, tmp_rd_thresh, &rd_stats);
721 if (rd != INT64_MAX) {
722 rd = RDCOST(x->rdmult, *rate_overhead + *tmp_rate_mv + rd_stats.rate,
723 rd_stats.dist);
724 } else {
725 if (*best_rd == INT64_MAX) return IGNORE_MODE;
726 }
727 *best_rd = rd;
728 return 0;
729 }
730
av1_handle_inter_intra_mode(const AV1_COMP * const cpi,MACROBLOCK * const x,BLOCK_SIZE bsize,MB_MODE_INFO * mbmi,HandleInterModeArgs * args,int64_t ref_best_rd,int * rate_mv,int * tmp_rate2,const BUFFER_SET * orig_dst)731 int av1_handle_inter_intra_mode(const AV1_COMP *const cpi, MACROBLOCK *const x,
732 BLOCK_SIZE bsize, MB_MODE_INFO *mbmi,
733 HandleInterModeArgs *args, int64_t ref_best_rd,
734 int *rate_mv, int *tmp_rate2,
735 const BUFFER_SET *orig_dst) {
736 const int try_smooth_interintra =
737 cpi->oxcf.comp_type_cfg.enable_smooth_interintra;
738
739 const int is_wedge_used = av1_is_wedge_used(bsize);
740 const int try_wedge_interintra =
741 is_wedge_used && enable_wedge_interintra_search(x, cpi);
742
743 const AV1_COMMON *const cm = &cpi->common;
744 MACROBLOCKD *xd = &x->e_mbd;
745 const int bw = block_size_wide[bsize];
746 DECLARE_ALIGNED(16, uint8_t, tmp_buf_[2 * MAX_INTERINTRA_SB_SQUARE]);
747 DECLARE_ALIGNED(16, uint8_t, intrapred_[2 * MAX_INTERINTRA_SB_SQUARE]);
748 uint8_t *tmp_buf = get_buf_by_bd(xd, tmp_buf_);
749 uint8_t *intrapred = get_buf_by_bd(xd, intrapred_);
750 const int mi_row = xd->mi_row;
751 const int mi_col = xd->mi_col;
752
753 // Single reference inter prediction
754 mbmi->ref_frame[1] = NONE_FRAME;
755 xd->plane[0].dst.buf = tmp_buf;
756 xd->plane[0].dst.stride = bw;
757 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
758 AOM_PLANE_Y, AOM_PLANE_Y);
759 const int num_planes = av1_num_planes(cm);
760
761 // Restore the buffers for intra prediction
762 restore_dst_buf(xd, *orig_dst, num_planes);
763 mbmi->ref_frame[1] = INTRA_FRAME;
764 INTERINTRA_MODE best_interintra_mode =
765 args->inter_intra_mode[mbmi->ref_frame[0]];
766
767 // Compute smooth_interintra
768 int64_t best_interintra_rd_nowedge = INT64_MAX;
769 int best_mode_rate = INT_MAX;
770 if (try_smooth_interintra) {
771 int ret = handle_smooth_inter_intra_mode(
772 cpi, x, bsize, mbmi, ref_best_rd, rate_mv, &best_interintra_mode,
773 &best_interintra_rd_nowedge, &best_mode_rate, orig_dst, tmp_buf,
774 intrapred, args);
775 if (ret == IGNORE_MODE) {
776 return IGNORE_MODE;
777 }
778 }
779
780 // Compute wedge interintra
781 int64_t best_interintra_rd_wedge = INT64_MAX;
782 const int_mv mv0 = mbmi->mv[0];
783 int_mv tmp_mv = mv0;
784 int tmp_rate_mv = 0;
785 int rate_overhead = 0;
786 if (try_wedge_interintra) {
787 int ret = handle_wedge_inter_intra_mode(
788 cpi, x, bsize, mbmi, rate_mv, &best_interintra_mode,
789 &best_interintra_rd_wedge, orig_dst, tmp_buf_, tmp_buf, intrapred_,
790 intrapred, args, &tmp_rate_mv, &rate_overhead, &tmp_mv,
791 best_interintra_rd_nowedge);
792 if (ret == IGNORE_MODE) {
793 return IGNORE_MODE;
794 }
795 }
796
797 if (best_interintra_rd_nowedge == INT64_MAX &&
798 best_interintra_rd_wedge == INT64_MAX) {
799 return IGNORE_MODE;
800 }
801 if (best_interintra_rd_wedge < best_interintra_rd_nowedge) {
802 mbmi->mv[0].as_int = tmp_mv.as_int;
803 *tmp_rate2 += tmp_rate_mv - *rate_mv;
804 *rate_mv = tmp_rate_mv;
805 best_mode_rate = rate_overhead;
806 } else if (try_smooth_interintra && try_wedge_interintra) {
807 // If smooth was best, but we over-wrote the values when evaluating the
808 // wedge mode, we need to recompute the smooth values.
809 mbmi->use_wedge_interintra = 0;
810 mbmi->interintra_mode = best_interintra_mode;
811 mbmi->mv[0].as_int = mv0.as_int;
812 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
813 AOM_PLANE_Y, AOM_PLANE_Y);
814 }
815 *tmp_rate2 += best_mode_rate;
816
817 if (num_planes > 1) {
818 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
819 AOM_PLANE_U, num_planes - 1);
820 }
821 return 0;
822 }
823
824 // Computes the valid compound_types to be evaluated
compute_valid_comp_types(MACROBLOCK * x,const AV1_COMP * const cpi,BLOCK_SIZE bsize,int masked_compound_used,int mode_search_mask,COMPOUND_TYPE * valid_comp_types)825 static INLINE int compute_valid_comp_types(MACROBLOCK *x,
826 const AV1_COMP *const cpi,
827 BLOCK_SIZE bsize,
828 int masked_compound_used,
829 int mode_search_mask,
830 COMPOUND_TYPE *valid_comp_types) {
831 const AV1_COMMON *cm = &cpi->common;
832 int valid_type_count = 0;
833 int comp_type, valid_check;
834 int8_t enable_masked_type[MASKED_COMPOUND_TYPES] = { 0, 0 };
835
836 const int try_average_comp = (mode_search_mask & (1 << COMPOUND_AVERAGE));
837 const int try_distwtd_comp =
838 ((mode_search_mask & (1 << COMPOUND_DISTWTD)) &&
839 cm->seq_params->order_hint_info.enable_dist_wtd_comp == 1 &&
840 cpi->sf.inter_sf.use_dist_wtd_comp_flag != DIST_WTD_COMP_DISABLED);
841
842 // Check if COMPOUND_AVERAGE and COMPOUND_DISTWTD are valid cases
843 for (comp_type = COMPOUND_AVERAGE; comp_type <= COMPOUND_DISTWTD;
844 comp_type++) {
845 valid_check =
846 (comp_type == COMPOUND_AVERAGE) ? try_average_comp : try_distwtd_comp;
847 if (valid_check && is_interinter_compound_used(comp_type, bsize))
848 valid_comp_types[valid_type_count++] = comp_type;
849 }
850 // Check if COMPOUND_WEDGE and COMPOUND_DIFFWTD are valid cases
851 if (masked_compound_used) {
852 // enable_masked_type[0] corresponds to COMPOUND_WEDGE
853 // enable_masked_type[1] corresponds to COMPOUND_DIFFWTD
854 enable_masked_type[0] = enable_wedge_interinter_search(x, cpi);
855 enable_masked_type[1] = cpi->oxcf.comp_type_cfg.enable_diff_wtd_comp;
856 for (comp_type = COMPOUND_WEDGE; comp_type <= COMPOUND_DIFFWTD;
857 comp_type++) {
858 if ((mode_search_mask & (1 << comp_type)) &&
859 is_interinter_compound_used(comp_type, bsize) &&
860 enable_masked_type[comp_type - COMPOUND_WEDGE])
861 valid_comp_types[valid_type_count++] = comp_type;
862 }
863 }
864 return valid_type_count;
865 }
866
867 // Calculates the cost for compound type mask
calc_masked_type_cost(const ModeCosts * mode_costs,BLOCK_SIZE bsize,int comp_group_idx_ctx,int comp_index_ctx,int masked_compound_used,int * masked_type_cost)868 static INLINE void calc_masked_type_cost(
869 const ModeCosts *mode_costs, BLOCK_SIZE bsize, int comp_group_idx_ctx,
870 int comp_index_ctx, int masked_compound_used, int *masked_type_cost) {
871 av1_zero_array(masked_type_cost, COMPOUND_TYPES);
872 // Account for group index cost when wedge and/or diffwtd prediction are
873 // enabled
874 if (masked_compound_used) {
875 // Compound group index of average and distwtd is 0
876 // Compound group index of wedge and diffwtd is 1
877 masked_type_cost[COMPOUND_AVERAGE] +=
878 mode_costs->comp_group_idx_cost[comp_group_idx_ctx][0];
879 masked_type_cost[COMPOUND_DISTWTD] += masked_type_cost[COMPOUND_AVERAGE];
880 masked_type_cost[COMPOUND_WEDGE] +=
881 mode_costs->comp_group_idx_cost[comp_group_idx_ctx][1];
882 masked_type_cost[COMPOUND_DIFFWTD] += masked_type_cost[COMPOUND_WEDGE];
883 }
884
885 // Compute the cost to signal compound index/type
886 masked_type_cost[COMPOUND_AVERAGE] +=
887 mode_costs->comp_idx_cost[comp_index_ctx][1];
888 masked_type_cost[COMPOUND_DISTWTD] +=
889 mode_costs->comp_idx_cost[comp_index_ctx][0];
890 masked_type_cost[COMPOUND_WEDGE] += mode_costs->compound_type_cost[bsize][0];
891 masked_type_cost[COMPOUND_DIFFWTD] +=
892 mode_costs->compound_type_cost[bsize][1];
893 }
894
895 // Updates mbmi structure with the relevant compound type info
update_mbmi_for_compound_type(MB_MODE_INFO * mbmi,COMPOUND_TYPE cur_type)896 static INLINE void update_mbmi_for_compound_type(MB_MODE_INFO *mbmi,
897 COMPOUND_TYPE cur_type) {
898 mbmi->interinter_comp.type = cur_type;
899 mbmi->comp_group_idx = (cur_type >= COMPOUND_WEDGE);
900 mbmi->compound_idx = (cur_type != COMPOUND_DISTWTD);
901 }
902
903 // When match is found, populate the compound type data
904 // and calculate the rd cost using the stored stats and
905 // update the mbmi appropriately.
populate_reuse_comp_type_data(const MACROBLOCK * x,MB_MODE_INFO * mbmi,BEST_COMP_TYPE_STATS * best_type_stats,int_mv * cur_mv,int32_t * comp_rate,int64_t * comp_dist,int * comp_rs2,int * rate_mv,int64_t * rd,int match_index)906 static INLINE int populate_reuse_comp_type_data(
907 const MACROBLOCK *x, MB_MODE_INFO *mbmi,
908 BEST_COMP_TYPE_STATS *best_type_stats, int_mv *cur_mv, int32_t *comp_rate,
909 int64_t *comp_dist, int *comp_rs2, int *rate_mv, int64_t *rd,
910 int match_index) {
911 const int winner_comp_type =
912 x->comp_rd_stats[match_index].interinter_comp.type;
913 if (comp_rate[winner_comp_type] == INT_MAX)
914 return best_type_stats->best_compmode_interinter_cost;
915 update_mbmi_for_compound_type(mbmi, winner_comp_type);
916 mbmi->interinter_comp = x->comp_rd_stats[match_index].interinter_comp;
917 *rd = RDCOST(
918 x->rdmult,
919 comp_rs2[winner_comp_type] + *rate_mv + comp_rate[winner_comp_type],
920 comp_dist[winner_comp_type]);
921 mbmi->mv[0].as_int = cur_mv[0].as_int;
922 mbmi->mv[1].as_int = cur_mv[1].as_int;
923 return comp_rs2[winner_comp_type];
924 }
925
926 // Updates rd cost and relevant compound type data for the best compound type
update_best_info(const MB_MODE_INFO * const mbmi,int64_t * rd,BEST_COMP_TYPE_STATS * best_type_stats,int64_t best_rd_cur,int64_t comp_model_rd_cur,int rs2)927 static INLINE void update_best_info(const MB_MODE_INFO *const mbmi, int64_t *rd,
928 BEST_COMP_TYPE_STATS *best_type_stats,
929 int64_t best_rd_cur,
930 int64_t comp_model_rd_cur, int rs2) {
931 *rd = best_rd_cur;
932 best_type_stats->comp_best_model_rd = comp_model_rd_cur;
933 best_type_stats->best_compound_data = mbmi->interinter_comp;
934 best_type_stats->best_compmode_interinter_cost = rs2;
935 }
936
937 // Updates best_mv for masked compound types
update_mask_best_mv(const MB_MODE_INFO * const mbmi,int_mv * best_mv,int * best_tmp_rate_mv,int tmp_rate_mv)938 static INLINE void update_mask_best_mv(const MB_MODE_INFO *const mbmi,
939 int_mv *best_mv, int *best_tmp_rate_mv,
940 int tmp_rate_mv) {
941 *best_tmp_rate_mv = tmp_rate_mv;
942 best_mv[0].as_int = mbmi->mv[0].as_int;
943 best_mv[1].as_int = mbmi->mv[1].as_int;
944 }
945
save_comp_rd_search_stat(MACROBLOCK * x,const MB_MODE_INFO * const mbmi,const int32_t * comp_rate,const int64_t * comp_dist,const int32_t * comp_model_rate,const int64_t * comp_model_dist,const int_mv * cur_mv,const int * comp_rs2)946 static INLINE void save_comp_rd_search_stat(
947 MACROBLOCK *x, const MB_MODE_INFO *const mbmi, const int32_t *comp_rate,
948 const int64_t *comp_dist, const int32_t *comp_model_rate,
949 const int64_t *comp_model_dist, const int_mv *cur_mv, const int *comp_rs2) {
950 const int offset = x->comp_rd_stats_idx;
951 if (offset < MAX_COMP_RD_STATS) {
952 COMP_RD_STATS *const rd_stats = x->comp_rd_stats + offset;
953 memcpy(rd_stats->rate, comp_rate, sizeof(rd_stats->rate));
954 memcpy(rd_stats->dist, comp_dist, sizeof(rd_stats->dist));
955 memcpy(rd_stats->model_rate, comp_model_rate, sizeof(rd_stats->model_rate));
956 memcpy(rd_stats->model_dist, comp_model_dist, sizeof(rd_stats->model_dist));
957 memcpy(rd_stats->comp_rs2, comp_rs2, sizeof(rd_stats->comp_rs2));
958 memcpy(rd_stats->mv, cur_mv, sizeof(rd_stats->mv));
959 memcpy(rd_stats->ref_frames, mbmi->ref_frame, sizeof(rd_stats->ref_frames));
960 rd_stats->mode = mbmi->mode;
961 rd_stats->filter = mbmi->interp_filters;
962 rd_stats->ref_mv_idx = mbmi->ref_mv_idx;
963 const MACROBLOCKD *const xd = &x->e_mbd;
964 for (int i = 0; i < 2; ++i) {
965 const WarpedMotionParams *const wm =
966 &xd->global_motion[mbmi->ref_frame[i]];
967 rd_stats->is_global[i] = is_global_mv_block(mbmi, wm->wmtype);
968 }
969 memcpy(&rd_stats->interinter_comp, &mbmi->interinter_comp,
970 sizeof(rd_stats->interinter_comp));
971 ++x->comp_rd_stats_idx;
972 }
973 }
974
get_interinter_compound_mask_rate(const ModeCosts * const mode_costs,const MB_MODE_INFO * const mbmi)975 static INLINE int get_interinter_compound_mask_rate(
976 const ModeCosts *const mode_costs, const MB_MODE_INFO *const mbmi) {
977 const COMPOUND_TYPE compound_type = mbmi->interinter_comp.type;
978 // This function will be called only for COMPOUND_WEDGE and COMPOUND_DIFFWTD
979 if (compound_type == COMPOUND_WEDGE) {
980 return av1_is_wedge_used(mbmi->bsize)
981 ? av1_cost_literal(1) +
982 mode_costs
983 ->wedge_idx_cost[mbmi->bsize]
984 [mbmi->interinter_comp.wedge_index]
985 : 0;
986 } else {
987 assert(compound_type == COMPOUND_DIFFWTD);
988 return av1_cost_literal(1);
989 }
990 }
991
992 // Takes a backup of rate, distortion and model_rd for future reuse
backup_stats(COMPOUND_TYPE cur_type,int32_t * comp_rate,int64_t * comp_dist,int32_t * comp_model_rate,int64_t * comp_model_dist,int rate_sum,int64_t dist_sum,RD_STATS * rd_stats,int * comp_rs2,int rs2)993 static INLINE void backup_stats(COMPOUND_TYPE cur_type, int32_t *comp_rate,
994 int64_t *comp_dist, int32_t *comp_model_rate,
995 int64_t *comp_model_dist, int rate_sum,
996 int64_t dist_sum, RD_STATS *rd_stats,
997 int *comp_rs2, int rs2) {
998 comp_rate[cur_type] = rd_stats->rate;
999 comp_dist[cur_type] = rd_stats->dist;
1000 comp_model_rate[cur_type] = rate_sum;
1001 comp_model_dist[cur_type] = dist_sum;
1002 comp_rs2[cur_type] = rs2;
1003 }
1004
save_mask_search_results(const PREDICTION_MODE this_mode,const int reuse_level)1005 static INLINE int save_mask_search_results(const PREDICTION_MODE this_mode,
1006 const int reuse_level) {
1007 if (reuse_level || (this_mode == NEW_NEWMV))
1008 return 1;
1009 else
1010 return 0;
1011 }
1012
prune_mode_by_skip_rd(const AV1_COMP * const cpi,MACROBLOCK * x,MACROBLOCKD * xd,const BLOCK_SIZE bsize,int64_t ref_skip_rd,int mode_rate)1013 static INLINE int prune_mode_by_skip_rd(const AV1_COMP *const cpi,
1014 MACROBLOCK *x, MACROBLOCKD *xd,
1015 const BLOCK_SIZE bsize,
1016 int64_t ref_skip_rd, int mode_rate) {
1017 int eval_txfm = 1;
1018 // Check if the mode is good enough based on skip rd
1019 if (cpi->sf.inter_sf.txfm_rd_gate_level) {
1020 int64_t sse_y = compute_sse_plane(x, xd, PLANE_TYPE_Y, bsize);
1021 int64_t skip_rd = RDCOST(x->rdmult, mode_rate, (sse_y << 4));
1022 eval_txfm = check_txfm_eval(x, bsize, ref_skip_rd, skip_rd,
1023 cpi->sf.inter_sf.txfm_rd_gate_level, 1);
1024 }
1025 return eval_txfm;
1026 }
1027
masked_compound_type_rd(const AV1_COMP * const cpi,MACROBLOCK * x,const int_mv * const cur_mv,const BLOCK_SIZE bsize,const PREDICTION_MODE this_mode,int * rs2,int rate_mv,const BUFFER_SET * ctx,int * out_rate_mv,uint8_t ** preds0,uint8_t ** preds1,int16_t * residual1,int16_t * diff10,int * strides,int mode_rate,int64_t rd_thresh,int * calc_pred_masked_compound,int32_t * comp_rate,int64_t * comp_dist,int32_t * comp_model_rate,int64_t * comp_model_dist,const int64_t comp_best_model_rd,int64_t * const comp_model_rd_cur,int * comp_rs2,int64_t ref_skip_rd)1028 static int64_t masked_compound_type_rd(
1029 const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv,
1030 const BLOCK_SIZE bsize, const PREDICTION_MODE this_mode, int *rs2,
1031 int rate_mv, const BUFFER_SET *ctx, int *out_rate_mv, uint8_t **preds0,
1032 uint8_t **preds1, int16_t *residual1, int16_t *diff10, int *strides,
1033 int mode_rate, int64_t rd_thresh, int *calc_pred_masked_compound,
1034 int32_t *comp_rate, int64_t *comp_dist, int32_t *comp_model_rate,
1035 int64_t *comp_model_dist, const int64_t comp_best_model_rd,
1036 int64_t *const comp_model_rd_cur, int *comp_rs2, int64_t ref_skip_rd) {
1037 const AV1_COMMON *const cm = &cpi->common;
1038 MACROBLOCKD *xd = &x->e_mbd;
1039 MB_MODE_INFO *const mbmi = xd->mi[0];
1040 int64_t best_rd_cur = INT64_MAX;
1041 int64_t rd = INT64_MAX;
1042 const COMPOUND_TYPE compound_type = mbmi->interinter_comp.type;
1043 // This function will be called only for COMPOUND_WEDGE and COMPOUND_DIFFWTD
1044 assert(compound_type == COMPOUND_WEDGE || compound_type == COMPOUND_DIFFWTD);
1045 int rate_sum, tmp_skip_txfm_sb;
1046 int64_t dist_sum, tmp_skip_sse_sb;
1047 pick_interinter_mask_type pick_interinter_mask[2] = { pick_interinter_wedge,
1048 pick_interinter_seg };
1049
1050 // TODO(any): Save pred and mask calculation as well into records. However
1051 // this may increase memory requirements as compound segment mask needs to be
1052 // stored in each record.
1053 if (*calc_pred_masked_compound) {
1054 get_inter_predictors_masked_compound(x, bsize, preds0, preds1, residual1,
1055 diff10, strides);
1056 *calc_pred_masked_compound = 0;
1057 }
1058 if (compound_type == COMPOUND_WEDGE) {
1059 unsigned int sse;
1060 if (is_cur_buf_hbd(xd))
1061 (void)cpi->ppi->fn_ptr[bsize].vf(CONVERT_TO_BYTEPTR(*preds0), *strides,
1062 CONVERT_TO_BYTEPTR(*preds1), *strides,
1063 &sse);
1064 else
1065 (void)cpi->ppi->fn_ptr[bsize].vf(*preds0, *strides, *preds1, *strides,
1066 &sse);
1067 const unsigned int mse =
1068 ROUND_POWER_OF_TWO(sse, num_pels_log2_lookup[bsize]);
1069 // If two predictors are very similar, skip wedge compound mode search
1070 if (mse < 8 || (!have_newmv_in_inter_mode(this_mode) && mse < 64)) {
1071 *comp_model_rd_cur = INT64_MAX;
1072 return INT64_MAX;
1073 }
1074 }
1075 // Function pointer to pick the appropriate mask
1076 // compound_type == COMPOUND_WEDGE, calls pick_interinter_wedge()
1077 // compound_type == COMPOUND_DIFFWTD, calls pick_interinter_seg()
1078 uint64_t cur_sse = UINT64_MAX;
1079 best_rd_cur = pick_interinter_mask[compound_type - COMPOUND_WEDGE](
1080 cpi, x, bsize, *preds0, *preds1, residual1, diff10, &cur_sse);
1081 *rs2 += get_interinter_compound_mask_rate(&x->mode_costs, mbmi);
1082 best_rd_cur += RDCOST(x->rdmult, *rs2 + rate_mv, 0);
1083 assert(cur_sse != UINT64_MAX);
1084 int64_t skip_rd_cur = RDCOST(x->rdmult, *rs2 + rate_mv, (cur_sse << 4));
1085
1086 // Although the true rate_mv might be different after motion search, but it
1087 // is unlikely to be the best mode considering the transform rd cost and other
1088 // mode overhead cost
1089 int64_t mode_rd = RDCOST(x->rdmult, *rs2 + mode_rate, 0);
1090 if (mode_rd > rd_thresh) {
1091 *comp_model_rd_cur = INT64_MAX;
1092 return INT64_MAX;
1093 }
1094
1095 // Check if the mode is good enough based on skip rd
1096 // TODO(nithya): Handle wedge_newmv_search if extending for lower speed
1097 // setting
1098 if (cpi->sf.inter_sf.txfm_rd_gate_level) {
1099 int eval_txfm = check_txfm_eval(x, bsize, ref_skip_rd, skip_rd_cur,
1100 cpi->sf.inter_sf.txfm_rd_gate_level, 1);
1101 if (!eval_txfm) {
1102 *comp_model_rd_cur = INT64_MAX;
1103 return INT64_MAX;
1104 }
1105 }
1106
1107 // Compute cost if matching record not found, else, reuse data
1108 if (comp_rate[compound_type] == INT_MAX) {
1109 // Check whether new MV search for wedge is to be done
1110 int wedge_newmv_search =
1111 have_newmv_in_inter_mode(this_mode) &&
1112 (compound_type == COMPOUND_WEDGE) &&
1113 (!cpi->sf.inter_sf.disable_interinter_wedge_newmv_search);
1114
1115 // Search for new MV if needed and build predictor
1116 if (wedge_newmv_search) {
1117 *out_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
1118 bsize, this_mode);
1119 const int mi_row = xd->mi_row;
1120 const int mi_col = xd->mi_col;
1121 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, ctx, bsize,
1122 AOM_PLANE_Y, AOM_PLANE_Y);
1123 } else {
1124 *out_rate_mv = rate_mv;
1125 av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0, strides,
1126 preds1, strides);
1127 }
1128 // Get the RD cost from model RD
1129 model_rd_sb_fn[MODELRD_TYPE_MASKED_COMPOUND](
1130 cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, &tmp_skip_txfm_sb,
1131 &tmp_skip_sse_sb, NULL, NULL, NULL);
1132 rd = RDCOST(x->rdmult, *rs2 + *out_rate_mv + rate_sum, dist_sum);
1133 *comp_model_rd_cur = rd;
1134 // Override with best if current is worse than best for new MV
1135 if (wedge_newmv_search) {
1136 if (rd >= best_rd_cur) {
1137 mbmi->mv[0].as_int = cur_mv[0].as_int;
1138 mbmi->mv[1].as_int = cur_mv[1].as_int;
1139 *out_rate_mv = rate_mv;
1140 av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0,
1141 strides, preds1, strides);
1142 *comp_model_rd_cur = best_rd_cur;
1143 }
1144 }
1145 if (cpi->sf.inter_sf.prune_comp_type_by_model_rd &&
1146 (*comp_model_rd_cur > comp_best_model_rd) &&
1147 comp_best_model_rd != INT64_MAX) {
1148 *comp_model_rd_cur = INT64_MAX;
1149 return INT64_MAX;
1150 }
1151 // Compute RD cost for the current type
1152 RD_STATS rd_stats;
1153 const int64_t tmp_mode_rd = RDCOST(x->rdmult, *rs2 + *out_rate_mv, 0);
1154 const int64_t tmp_rd_thresh = rd_thresh - tmp_mode_rd;
1155 rd = estimate_yrd_for_sb(cpi, bsize, x, tmp_rd_thresh, &rd_stats);
1156 if (rd != INT64_MAX) {
1157 rd =
1158 RDCOST(x->rdmult, *rs2 + *out_rate_mv + rd_stats.rate, rd_stats.dist);
1159 // Backup rate and distortion for future reuse
1160 backup_stats(compound_type, comp_rate, comp_dist, comp_model_rate,
1161 comp_model_dist, rate_sum, dist_sum, &rd_stats, comp_rs2,
1162 *rs2);
1163 }
1164 } else {
1165 // Reuse data as matching record is found
1166 assert(comp_dist[compound_type] != INT64_MAX);
1167 // When disable_interinter_wedge_newmv_search is set, motion refinement is
1168 // disabled. Hence rate and distortion can be reused in this case as well
1169 assert(IMPLIES((have_newmv_in_inter_mode(this_mode) &&
1170 (compound_type == COMPOUND_WEDGE)),
1171 cpi->sf.inter_sf.disable_interinter_wedge_newmv_search));
1172 assert(mbmi->mv[0].as_int == cur_mv[0].as_int);
1173 assert(mbmi->mv[1].as_int == cur_mv[1].as_int);
1174 *out_rate_mv = rate_mv;
1175 // Calculate RD cost based on stored stats
1176 rd = RDCOST(x->rdmult, *rs2 + *out_rate_mv + comp_rate[compound_type],
1177 comp_dist[compound_type]);
1178 // Recalculate model rdcost with the updated rate
1179 *comp_model_rd_cur =
1180 RDCOST(x->rdmult, *rs2 + *out_rate_mv + comp_model_rate[compound_type],
1181 comp_model_dist[compound_type]);
1182 }
1183 return rd;
1184 }
1185
1186 // scaling values to be used for gating wedge/compound segment based on best
1187 // approximate rd
1188 static int comp_type_rd_threshold_mul[3] = { 1, 11, 12 };
1189 static int comp_type_rd_threshold_div[3] = { 3, 16, 16 };
1190
av1_compound_type_rd(const AV1_COMP * const cpi,MACROBLOCK * x,HandleInterModeArgs * args,BLOCK_SIZE bsize,int_mv * cur_mv,int mode_search_mask,int masked_compound_used,const BUFFER_SET * orig_dst,const BUFFER_SET * tmp_dst,const CompoundTypeRdBuffers * buffers,int * rate_mv,int64_t * rd,RD_STATS * rd_stats,int64_t ref_best_rd,int64_t ref_skip_rd,int * is_luma_interp_done,int64_t rd_thresh)1191 int av1_compound_type_rd(const AV1_COMP *const cpi, MACROBLOCK *x,
1192 HandleInterModeArgs *args, BLOCK_SIZE bsize,
1193 int_mv *cur_mv, int mode_search_mask,
1194 int masked_compound_used, const BUFFER_SET *orig_dst,
1195 const BUFFER_SET *tmp_dst,
1196 const CompoundTypeRdBuffers *buffers, int *rate_mv,
1197 int64_t *rd, RD_STATS *rd_stats, int64_t ref_best_rd,
1198 int64_t ref_skip_rd, int *is_luma_interp_done,
1199 int64_t rd_thresh) {
1200 const AV1_COMMON *cm = &cpi->common;
1201 MACROBLOCKD *xd = &x->e_mbd;
1202 MB_MODE_INFO *mbmi = xd->mi[0];
1203 const PREDICTION_MODE this_mode = mbmi->mode;
1204 int ref_frame = av1_ref_frame_type(mbmi->ref_frame);
1205 const int bw = block_size_wide[bsize];
1206 int rs2;
1207 int_mv best_mv[2];
1208 int best_tmp_rate_mv = *rate_mv;
1209 BEST_COMP_TYPE_STATS best_type_stats;
1210 // Initializing BEST_COMP_TYPE_STATS
1211 best_type_stats.best_compound_data.type = COMPOUND_AVERAGE;
1212 best_type_stats.best_compmode_interinter_cost = 0;
1213 best_type_stats.comp_best_model_rd = INT64_MAX;
1214
1215 uint8_t *preds0[1] = { buffers->pred0 };
1216 uint8_t *preds1[1] = { buffers->pred1 };
1217 int strides[1] = { bw };
1218 int tmp_rate_mv;
1219 COMPOUND_TYPE cur_type;
1220 // Local array to store the mask cost for different compound types
1221 int masked_type_cost[COMPOUND_TYPES];
1222
1223 int calc_pred_masked_compound = 1;
1224 int64_t comp_dist[COMPOUND_TYPES] = { INT64_MAX, INT64_MAX, INT64_MAX,
1225 INT64_MAX };
1226 int32_t comp_rate[COMPOUND_TYPES] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
1227 int comp_rs2[COMPOUND_TYPES] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
1228 int32_t comp_model_rate[COMPOUND_TYPES] = { INT_MAX, INT_MAX, INT_MAX,
1229 INT_MAX };
1230 int64_t comp_model_dist[COMPOUND_TYPES] = { INT64_MAX, INT64_MAX, INT64_MAX,
1231 INT64_MAX };
1232 int match_index = 0;
1233 const int match_found =
1234 find_comp_rd_in_stats(cpi, x, mbmi, comp_rate, comp_dist, comp_model_rate,
1235 comp_model_dist, comp_rs2, &match_index);
1236 best_mv[0].as_int = cur_mv[0].as_int;
1237 best_mv[1].as_int = cur_mv[1].as_int;
1238 *rd = INT64_MAX;
1239
1240 // Local array to store the valid compound types to be evaluated in the core
1241 // loop
1242 COMPOUND_TYPE valid_comp_types[COMPOUND_TYPES] = {
1243 COMPOUND_AVERAGE, COMPOUND_DISTWTD, COMPOUND_WEDGE, COMPOUND_DIFFWTD
1244 };
1245 int valid_type_count = 0;
1246 // compute_valid_comp_types() returns the number of valid compound types to be
1247 // evaluated and populates the same in the local array valid_comp_types[].
1248 // It also sets the flag 'try_average_and_distwtd_comp'
1249 valid_type_count = compute_valid_comp_types(
1250 x, cpi, bsize, masked_compound_used, mode_search_mask, valid_comp_types);
1251
1252 // The following context indices are independent of compound type
1253 const int comp_group_idx_ctx = get_comp_group_idx_context(xd);
1254 const int comp_index_ctx = get_comp_index_context(cm, xd);
1255
1256 // Populates masked_type_cost local array for the 4 compound types
1257 calc_masked_type_cost(&x->mode_costs, bsize, comp_group_idx_ctx,
1258 comp_index_ctx, masked_compound_used, masked_type_cost);
1259
1260 int64_t comp_model_rd_cur = INT64_MAX;
1261 int64_t best_rd_cur = ref_best_rd;
1262 const int mi_row = xd->mi_row;
1263 const int mi_col = xd->mi_col;
1264
1265 // If the match is found, calculate the rd cost using the
1266 // stored stats and update the mbmi appropriately.
1267 if (match_found && cpi->sf.inter_sf.reuse_compound_type_decision) {
1268 return populate_reuse_comp_type_data(x, mbmi, &best_type_stats, cur_mv,
1269 comp_rate, comp_dist, comp_rs2,
1270 rate_mv, rd, match_index);
1271 }
1272
1273 // If COMPOUND_AVERAGE is not valid, use the spare buffer
1274 if (valid_comp_types[0] != COMPOUND_AVERAGE) restore_dst_buf(xd, *tmp_dst, 1);
1275
1276 // Loop over valid compound types
1277 for (int i = 0; i < valid_type_count; i++) {
1278 cur_type = valid_comp_types[i];
1279
1280 if (args->cmp_mode[ref_frame] == COMPOUND_AVERAGE) {
1281 if (cur_type == COMPOUND_WEDGE) continue;
1282 }
1283
1284 comp_model_rd_cur = INT64_MAX;
1285 tmp_rate_mv = *rate_mv;
1286 best_rd_cur = INT64_MAX;
1287 ref_best_rd = AOMMIN(ref_best_rd, *rd);
1288 update_mbmi_for_compound_type(mbmi, cur_type);
1289 rs2 = masked_type_cost[cur_type];
1290
1291 int64_t mode_rd = RDCOST(x->rdmult, rs2 + rd_stats->rate, 0);
1292 if (mode_rd >= ref_best_rd) continue;
1293
1294 // Case COMPOUND_AVERAGE and COMPOUND_DISTWTD
1295 if (cur_type < COMPOUND_WEDGE) {
1296 if (cpi->sf.inter_sf.enable_fast_compound_mode_search == 2) {
1297 int rate_sum, tmp_skip_txfm_sb;
1298 int64_t dist_sum, tmp_skip_sse_sb;
1299
1300 // Reuse data if matching record is found
1301 if (comp_rate[cur_type] == INT_MAX) {
1302 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
1303 AOM_PLANE_Y, AOM_PLANE_Y);
1304 if (cur_type == COMPOUND_AVERAGE) *is_luma_interp_done = 1;
1305 // Compute RD cost for the current type
1306 RD_STATS est_rd_stats;
1307 const int64_t tmp_rd_thresh = AOMMIN(*rd, rd_thresh) - mode_rd;
1308 int64_t est_rd = INT64_MAX;
1309 int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
1310 rs2 + *rate_mv);
1311 // Evaluate further if skip rd is low enough
1312 if (eval_txfm) {
1313 est_rd = estimate_yrd_for_sb(cpi, bsize, x, tmp_rd_thresh,
1314 &est_rd_stats);
1315 }
1316 if (est_rd != INT64_MAX) {
1317 best_rd_cur = RDCOST(x->rdmult, rs2 + *rate_mv + est_rd_stats.rate,
1318 est_rd_stats.dist);
1319 model_rd_sb_fn[MODELRD_TYPE_MASKED_COMPOUND](
1320 cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum,
1321 &tmp_skip_txfm_sb, &tmp_skip_sse_sb, NULL, NULL, NULL);
1322 comp_model_rd_cur =
1323 RDCOST(x->rdmult, rs2 + *rate_mv + rate_sum, dist_sum);
1324 // Backup rate and distortion for future reuse
1325 backup_stats(cur_type, comp_rate, comp_dist, comp_model_rate,
1326 comp_model_dist, rate_sum, dist_sum, &est_rd_stats,
1327 comp_rs2, rs2);
1328 }
1329 } else {
1330 // Calculate RD cost based on stored stats
1331 assert(comp_dist[cur_type] != INT64_MAX);
1332 best_rd_cur = RDCOST(x->rdmult, rs2 + *rate_mv + comp_rate[cur_type],
1333 comp_dist[cur_type]);
1334 // Recalculate model rdcost with the updated rate
1335 comp_model_rd_cur =
1336 RDCOST(x->rdmult, rs2 + *rate_mv + comp_model_rate[cur_type],
1337 comp_model_dist[cur_type]);
1338 }
1339 } else {
1340 tmp_rate_mv = *rate_mv;
1341 if (have_newmv_in_inter_mode(this_mode)) {
1342 InterPredParams inter_pred_params;
1343 av1_dist_wtd_comp_weight_assign(
1344 &cpi->common, mbmi, &inter_pred_params.conv_params.fwd_offset,
1345 &inter_pred_params.conv_params.bck_offset,
1346 &inter_pred_params.conv_params.use_dist_wtd_comp_avg, 1);
1347 int mask_value = inter_pred_params.conv_params.fwd_offset * 4;
1348 memset(xd->seg_mask, mask_value,
1349 sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
1350 tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
1351 bsize, this_mode);
1352 }
1353 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
1354 AOM_PLANE_Y, AOM_PLANE_Y);
1355 if (cur_type == COMPOUND_AVERAGE) *is_luma_interp_done = 1;
1356
1357 int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
1358 rs2 + *rate_mv);
1359 if (eval_txfm) {
1360 RD_STATS est_rd_stats;
1361 estimate_yrd_for_sb(cpi, bsize, x, INT64_MAX, &est_rd_stats);
1362
1363 best_rd_cur = RDCOST(x->rdmult, rs2 + tmp_rate_mv + est_rd_stats.rate,
1364 est_rd_stats.dist);
1365 }
1366 }
1367
1368 // use spare buffer for following compound type try
1369 if (cur_type == COMPOUND_AVERAGE) restore_dst_buf(xd, *tmp_dst, 1);
1370 } else if (cur_type == COMPOUND_WEDGE) {
1371 int best_mask_index = 0;
1372 int best_wedge_sign = 0;
1373 int_mv tmp_mv[2] = { mbmi->mv[0], mbmi->mv[1] };
1374 int best_rs2 = 0;
1375 int best_rate_mv = *rate_mv;
1376 int wedge_mask_size = get_wedge_types_lookup(bsize);
1377 int need_mask_search = args->wedge_index == -1;
1378
1379 if (need_mask_search && !have_newmv_in_inter_mode(this_mode)) {
1380 // short cut repeated single reference block build
1381 av1_build_inter_predictors_for_planes_single_buf(xd, bsize, 0, 0, 0,
1382 preds0, strides);
1383 av1_build_inter_predictors_for_planes_single_buf(xd, bsize, 0, 0, 1,
1384 preds1, strides);
1385 }
1386
1387 for (int wedge_mask = 0; wedge_mask < wedge_mask_size && need_mask_search;
1388 ++wedge_mask) {
1389 for (int wedge_sign = 0; wedge_sign < 2; ++wedge_sign) {
1390 tmp_rate_mv = *rate_mv;
1391 mbmi->interinter_comp.wedge_index = wedge_mask;
1392 mbmi->interinter_comp.wedge_sign = wedge_sign;
1393 rs2 = masked_type_cost[cur_type];
1394 rs2 += get_interinter_compound_mask_rate(&x->mode_costs, mbmi);
1395
1396 mode_rd = RDCOST(x->rdmult, rs2 + rd_stats->rate, 0);
1397 if (mode_rd >= ref_best_rd / 2) continue;
1398
1399 if (have_newmv_in_inter_mode(this_mode) &&
1400 !cpi->sf.inter_sf.disable_interinter_wedge_newmv_search) {
1401 tmp_rate_mv = av1_interinter_compound_motion_search(
1402 cpi, x, cur_mv, bsize, this_mode);
1403 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst,
1404 bsize, AOM_PLANE_Y, AOM_PLANE_Y);
1405 } else {
1406 av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0,
1407 strides, preds1, strides);
1408 }
1409
1410 RD_STATS est_rd_stats;
1411 int64_t this_rd_cur = INT64_MAX;
1412 int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
1413 rs2 + *rate_mv);
1414 if (eval_txfm) {
1415 this_rd_cur = estimate_yrd_for_sb(
1416 cpi, bsize, x, AOMMIN(best_rd_cur, ref_best_rd), &est_rd_stats);
1417 }
1418 if (this_rd_cur < INT64_MAX) {
1419 this_rd_cur =
1420 RDCOST(x->rdmult, rs2 + tmp_rate_mv + est_rd_stats.rate,
1421 est_rd_stats.dist);
1422 }
1423 if (this_rd_cur < best_rd_cur) {
1424 best_mask_index = wedge_mask;
1425 best_wedge_sign = wedge_sign;
1426 best_rd_cur = this_rd_cur;
1427 tmp_mv[0] = mbmi->mv[0];
1428 tmp_mv[1] = mbmi->mv[1];
1429 best_rate_mv = tmp_rate_mv;
1430 best_rs2 = rs2;
1431 }
1432 }
1433 // Consider the asymmetric partitions for oblique angle only if the
1434 // corresponding symmetric partition is the best so far.
1435 // Note: For horizontal and vertical types, both symmetric and
1436 // asymmetric partitions are always considered.
1437 if (cpi->sf.inter_sf.enable_fast_wedge_mask_search) {
1438 // The first 4 entries in wedge_codebook_16_heqw/hltw/hgtw[16]
1439 // correspond to symmetric partitions of the 4 oblique angles, the
1440 // next 4 entries correspond to the vertical/horizontal
1441 // symmetric/asymmetric partitions and the last 8 entries correspond
1442 // to the asymmetric partitions of oblique types.
1443 const int idx_before_asym_oblique = 7;
1444 const int last_oblique_sym_idx = 3;
1445 if (wedge_mask == idx_before_asym_oblique) {
1446 if (best_mask_index > last_oblique_sym_idx) {
1447 break;
1448 } else {
1449 // Asymmetric (Index-1) map for the corresponding oblique masks.
1450 // WEDGE_OBLIQUE27: sym - 0, asym - 8, 9
1451 // WEDGE_OBLIQUE63: sym - 1, asym - 12, 13
1452 // WEDGE_OBLIQUE117: sym - 2, asym - 14, 15
1453 // WEDGE_OBLIQUE153: sym - 3, asym - 10, 11
1454 const int asym_mask_idx[4] = { 7, 11, 13, 9 };
1455 wedge_mask = asym_mask_idx[best_mask_index];
1456 wedge_mask_size = wedge_mask + 3;
1457 }
1458 }
1459 }
1460 }
1461
1462 if (need_mask_search) {
1463 if (save_mask_search_results(
1464 this_mode, cpi->sf.inter_sf.reuse_mask_search_results)) {
1465 args->wedge_index = best_mask_index;
1466 args->wedge_sign = best_wedge_sign;
1467 }
1468 } else {
1469 mbmi->interinter_comp.wedge_index = args->wedge_index;
1470 mbmi->interinter_comp.wedge_sign = args->wedge_sign;
1471 rs2 = masked_type_cost[cur_type];
1472 rs2 += get_interinter_compound_mask_rate(&x->mode_costs, mbmi);
1473
1474 if (have_newmv_in_inter_mode(this_mode) &&
1475 !cpi->sf.inter_sf.disable_interinter_wedge_newmv_search) {
1476 tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
1477 bsize, this_mode);
1478 }
1479
1480 best_mask_index = args->wedge_index;
1481 best_wedge_sign = args->wedge_sign;
1482 tmp_mv[0] = mbmi->mv[0];
1483 tmp_mv[1] = mbmi->mv[1];
1484 best_rate_mv = tmp_rate_mv;
1485 best_rs2 = masked_type_cost[cur_type];
1486 best_rs2 += get_interinter_compound_mask_rate(&x->mode_costs, mbmi);
1487 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
1488 AOM_PLANE_Y, AOM_PLANE_Y);
1489 int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
1490 best_rs2 + *rate_mv);
1491 if (eval_txfm) {
1492 RD_STATS est_rd_stats;
1493 estimate_yrd_for_sb(cpi, bsize, x, INT64_MAX, &est_rd_stats);
1494 best_rd_cur =
1495 RDCOST(x->rdmult, best_rs2 + tmp_rate_mv + est_rd_stats.rate,
1496 est_rd_stats.dist);
1497 }
1498 }
1499
1500 mbmi->interinter_comp.wedge_index = best_mask_index;
1501 mbmi->interinter_comp.wedge_sign = best_wedge_sign;
1502 mbmi->mv[0] = tmp_mv[0];
1503 mbmi->mv[1] = tmp_mv[1];
1504 tmp_rate_mv = best_rate_mv;
1505 rs2 = best_rs2;
1506 } else if (!cpi->sf.inter_sf.enable_fast_compound_mode_search &&
1507 cur_type == COMPOUND_DIFFWTD) {
1508 int_mv tmp_mv[2];
1509 int best_mask_index = 0;
1510 rs2 += get_interinter_compound_mask_rate(&x->mode_costs, mbmi);
1511
1512 int need_mask_search = args->diffwtd_index == -1;
1513
1514 for (int mask_index = 0; mask_index < 2 && need_mask_search;
1515 ++mask_index) {
1516 tmp_rate_mv = *rate_mv;
1517 mbmi->interinter_comp.mask_type = mask_index;
1518 if (have_newmv_in_inter_mode(this_mode)) {
1519 // hard coded number for diff wtd
1520 int mask_value = mask_index == 0 ? 38 : 26;
1521 memset(xd->seg_mask, mask_value,
1522 sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
1523 tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
1524 bsize, this_mode);
1525 }
1526 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
1527 AOM_PLANE_Y, AOM_PLANE_Y);
1528 RD_STATS est_rd_stats;
1529 int64_t this_rd_cur = INT64_MAX;
1530 int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
1531 rs2 + *rate_mv);
1532 if (eval_txfm) {
1533 this_rd_cur =
1534 estimate_yrd_for_sb(cpi, bsize, x, ref_best_rd, &est_rd_stats);
1535 }
1536 if (this_rd_cur < INT64_MAX) {
1537 this_rd_cur = RDCOST(x->rdmult, rs2 + tmp_rate_mv + est_rd_stats.rate,
1538 est_rd_stats.dist);
1539 }
1540
1541 if (this_rd_cur < best_rd_cur) {
1542 best_rd_cur = this_rd_cur;
1543 best_mask_index = mbmi->interinter_comp.mask_type;
1544 tmp_mv[0] = mbmi->mv[0];
1545 tmp_mv[1] = mbmi->mv[1];
1546 }
1547 }
1548
1549 if (need_mask_search) {
1550 if (save_mask_search_results(
1551 this_mode, cpi->sf.inter_sf.reuse_mask_search_results))
1552 args->diffwtd_index = best_mask_index;
1553 } else {
1554 mbmi->interinter_comp.mask_type = args->diffwtd_index;
1555 rs2 = masked_type_cost[cur_type];
1556 rs2 += get_interinter_compound_mask_rate(&x->mode_costs, mbmi);
1557
1558 int mask_value = mbmi->interinter_comp.mask_type == 0 ? 38 : 26;
1559 memset(xd->seg_mask, mask_value,
1560 sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
1561
1562 if (have_newmv_in_inter_mode(this_mode)) {
1563 tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
1564 bsize, this_mode);
1565 }
1566 best_mask_index = mbmi->interinter_comp.mask_type;
1567 tmp_mv[0] = mbmi->mv[0];
1568 tmp_mv[1] = mbmi->mv[1];
1569 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
1570 AOM_PLANE_Y, AOM_PLANE_Y);
1571 RD_STATS est_rd_stats;
1572 int64_t this_rd_cur = INT64_MAX;
1573 int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
1574 rs2 + *rate_mv);
1575 if (eval_txfm) {
1576 this_rd_cur =
1577 estimate_yrd_for_sb(cpi, bsize, x, ref_best_rd, &est_rd_stats);
1578 }
1579 if (this_rd_cur < INT64_MAX) {
1580 best_rd_cur = RDCOST(x->rdmult, rs2 + tmp_rate_mv + est_rd_stats.rate,
1581 est_rd_stats.dist);
1582 }
1583 }
1584
1585 mbmi->interinter_comp.mask_type = best_mask_index;
1586 mbmi->mv[0] = tmp_mv[0];
1587 mbmi->mv[1] = tmp_mv[1];
1588 } else {
1589 // Handle masked compound types
1590 // Factors to control gating of compound type selection based on best
1591 // approximate rd so far
1592 const int max_comp_type_rd_threshold_mul =
1593 comp_type_rd_threshold_mul[cpi->sf.inter_sf
1594 .prune_comp_type_by_comp_avg];
1595 const int max_comp_type_rd_threshold_div =
1596 comp_type_rd_threshold_div[cpi->sf.inter_sf
1597 .prune_comp_type_by_comp_avg];
1598 // Evaluate COMPOUND_WEDGE / COMPOUND_DIFFWTD if approximated cost is
1599 // within threshold
1600 int64_t approx_rd = ((*rd / max_comp_type_rd_threshold_div) *
1601 max_comp_type_rd_threshold_mul);
1602
1603 if (approx_rd < ref_best_rd) {
1604 const int64_t tmp_rd_thresh = AOMMIN(*rd, rd_thresh);
1605 best_rd_cur = masked_compound_type_rd(
1606 cpi, x, cur_mv, bsize, this_mode, &rs2, *rate_mv, orig_dst,
1607 &tmp_rate_mv, preds0, preds1, buffers->residual1, buffers->diff10,
1608 strides, rd_stats->rate, tmp_rd_thresh, &calc_pred_masked_compound,
1609 comp_rate, comp_dist, comp_model_rate, comp_model_dist,
1610 best_type_stats.comp_best_model_rd, &comp_model_rd_cur, comp_rs2,
1611 ref_skip_rd);
1612 }
1613 }
1614
1615 // Update stats for best compound type
1616 if (best_rd_cur < *rd) {
1617 update_best_info(mbmi, rd, &best_type_stats, best_rd_cur,
1618 comp_model_rd_cur, rs2);
1619 if (have_newmv_in_inter_mode(this_mode))
1620 update_mask_best_mv(mbmi, best_mv, &best_tmp_rate_mv, tmp_rate_mv);
1621 }
1622 // reset to original mvs for next iteration
1623 mbmi->mv[0].as_int = cur_mv[0].as_int;
1624 mbmi->mv[1].as_int = cur_mv[1].as_int;
1625 }
1626
1627 mbmi->comp_group_idx =
1628 (best_type_stats.best_compound_data.type < COMPOUND_WEDGE) ? 0 : 1;
1629 mbmi->compound_idx =
1630 !(best_type_stats.best_compound_data.type == COMPOUND_DISTWTD);
1631 mbmi->interinter_comp = best_type_stats.best_compound_data;
1632
1633 if (have_newmv_in_inter_mode(this_mode)) {
1634 mbmi->mv[0].as_int = best_mv[0].as_int;
1635 mbmi->mv[1].as_int = best_mv[1].as_int;
1636 rd_stats->rate += best_tmp_rate_mv - *rate_mv;
1637 *rate_mv = best_tmp_rate_mv;
1638 }
1639
1640 if (this_mode == NEW_NEWMV)
1641 args->cmp_mode[ref_frame] = mbmi->interinter_comp.type;
1642
1643 restore_dst_buf(xd, *orig_dst, 1);
1644 if (!match_found)
1645 save_comp_rd_search_stat(x, mbmi, comp_rate, comp_dist, comp_model_rate,
1646 comp_model_dist, cur_mv, comp_rs2);
1647 return best_type_stats.best_compmode_interinter_cost;
1648 }
1649