Coverage Report

Created: 2025-06-22 08:04

/src/aom/av1/encoder/interp_search.c
Line
Count
Source (jump to first uncovered line)
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/interp_search.h"
14
#include "av1/encoder/model_rd.h"
15
#include "av1/encoder/rdopt_utils.h"
16
#include "av1/encoder/reconinter_enc.h"
17
18
// return mv_diff
19
static inline int is_interp_filter_good_match(
20
    const INTERPOLATION_FILTER_STATS *st, MB_MODE_INFO *const mi,
21
0
    int skip_level) {
22
0
  const int is_comp = has_second_ref(mi);
23
0
  int i;
24
25
0
  for (i = 0; i < 1 + is_comp; ++i) {
26
0
    if (st->ref_frames[i] != mi->ref_frame[i]) return INT_MAX;
27
0
  }
28
29
0
  if (skip_level == 1 && is_comp) {
30
0
    if (st->comp_type != mi->interinter_comp.type) return INT_MAX;
31
0
    if (st->compound_idx != mi->compound_idx) return INT_MAX;
32
0
  }
33
34
0
  int mv_diff = 0;
35
0
  for (i = 0; i < 1 + is_comp; ++i) {
36
0
    mv_diff += abs(st->mv[i].as_mv.row - mi->mv[i].as_mv.row) +
37
0
               abs(st->mv[i].as_mv.col - mi->mv[i].as_mv.col);
38
0
  }
39
0
  return mv_diff;
40
0
}
41
42
static inline int save_interp_filter_search_stat(
43
    MB_MODE_INFO *const mbmi, int64_t rd, unsigned int pred_sse,
44
    INTERPOLATION_FILTER_STATS *interp_filter_stats,
45
0
    int interp_filter_stats_idx) {
46
0
  if (interp_filter_stats_idx < MAX_INTERP_FILTER_STATS) {
47
0
    INTERPOLATION_FILTER_STATS stat = { mbmi->interp_filters,
48
0
                                        { mbmi->mv[0], mbmi->mv[1] },
49
0
                                        { mbmi->ref_frame[0],
50
0
                                          mbmi->ref_frame[1] },
51
0
                                        mbmi->interinter_comp.type,
52
0
                                        mbmi->compound_idx,
53
0
                                        rd,
54
0
                                        pred_sse };
55
0
    interp_filter_stats[interp_filter_stats_idx] = stat;
56
0
    interp_filter_stats_idx++;
57
0
  }
58
0
  return interp_filter_stats_idx;
59
0
}
60
61
static inline int find_interp_filter_in_stats(
62
    MB_MODE_INFO *const mbmi, INTERPOLATION_FILTER_STATS *interp_filter_stats,
63
0
    int interp_filter_stats_idx, int skip_level) {
64
  // [skip_levels][single or comp]
65
0
  const int thr[2][2] = { { 0, 0 }, { 3, 7 } };
66
0
  const int is_comp = has_second_ref(mbmi);
67
68
  // Find good enough match.
69
  // TODO(yunqing): Separate single-ref mode and comp mode stats for fast
70
  // search.
71
0
  int best = INT_MAX;
72
0
  int match = -1;
73
0
  for (int j = 0; j < interp_filter_stats_idx; ++j) {
74
0
    const INTERPOLATION_FILTER_STATS *st = &interp_filter_stats[j];
75
0
    const int mv_diff = is_interp_filter_good_match(st, mbmi, skip_level);
76
    // Exact match is found.
77
0
    if (mv_diff == 0) {
78
0
      match = j;
79
0
      break;
80
0
    } else if (mv_diff < best && mv_diff <= thr[skip_level - 1][is_comp]) {
81
0
      best = mv_diff;
82
0
      match = j;
83
0
    }
84
0
  }
85
86
0
  if (match != -1) {
87
0
    mbmi->interp_filters = interp_filter_stats[match].filters;
88
0
    return match;
89
0
  }
90
0
  return -1;  // no match result found
91
0
}
92
93
static int find_interp_filter_match(
94
    MB_MODE_INFO *const mbmi, const AV1_COMP *const cpi,
95
    const InterpFilter assign_filter, const int need_search,
96
    INTERPOLATION_FILTER_STATS *interp_filter_stats,
97
0
    int interp_filter_stats_idx) {
98
0
  int match_found_idx = -1;
99
0
  if (cpi->sf.interp_sf.use_interp_filter && need_search)
100
0
    match_found_idx = find_interp_filter_in_stats(
101
0
        mbmi, interp_filter_stats, interp_filter_stats_idx,
102
0
        cpi->sf.interp_sf.use_interp_filter);
103
104
0
  if (!need_search || match_found_idx == -1)
105
0
    set_default_interp_filters(mbmi, assign_filter);
106
0
  return match_found_idx;
107
0
}
108
109
static inline int get_switchable_rate(MACROBLOCK *const x,
110
                                      const int_interpfilters filters,
111
0
                                      const int ctx[2], int dual_filter) {
112
0
  const InterpFilter filter0 = filters.as_filters.y_filter;
113
0
  int inter_filter_cost =
114
0
      x->mode_costs.switchable_interp_costs[ctx[0]][filter0];
115
0
  if (dual_filter) {
116
0
    const InterpFilter filter1 = filters.as_filters.x_filter;
117
0
    inter_filter_cost += x->mode_costs.switchable_interp_costs[ctx[1]][filter1];
118
0
  }
119
0
  return SWITCHABLE_INTERP_RATE_FACTOR * inter_filter_cost;
120
0
}
121
122
// Build inter predictor and calculate model rd
123
// for a given plane.
124
static inline void interp_model_rd_eval(
125
    MACROBLOCK *const x, const AV1_COMP *const cpi, BLOCK_SIZE bsize,
126
    const BUFFER_SET *const orig_dst, int plane_from, int plane_to,
127
0
    RD_STATS *rd_stats, int is_skip_build_pred) {
128
0
  const AV1_COMMON *cm = &cpi->common;
129
0
  MACROBLOCKD *const xd = &x->e_mbd;
130
0
  RD_STATS tmp_rd_stats;
131
0
  av1_init_rd_stats(&tmp_rd_stats);
132
133
  // Skip inter predictor if the predictor is already available.
134
0
  if (!is_skip_build_pred) {
135
0
    const int mi_row = xd->mi_row;
136
0
    const int mi_col = xd->mi_col;
137
0
    av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
138
0
                                  plane_from, plane_to);
139
0
  }
140
141
0
  model_rd_sb_fn[cpi->sf.rt_sf.use_simple_rd_model
142
0
                     ? MODELRD_LEGACY
143
0
                     : MODELRD_TYPE_INTERP_FILTER](
144
0
      cpi, bsize, x, xd, plane_from, plane_to, &tmp_rd_stats.rate,
145
0
      &tmp_rd_stats.dist, &tmp_rd_stats.skip_txfm, &tmp_rd_stats.sse, NULL,
146
0
      NULL, NULL);
147
148
0
  av1_merge_rd_stats(rd_stats, &tmp_rd_stats);
149
0
}
150
151
// calculate the rdcost of given interpolation_filter
152
static inline int64_t interpolation_filter_rd(
153
    MACROBLOCK *const x, const AV1_COMP *const cpi,
154
    const TileDataEnc *tile_data, BLOCK_SIZE bsize,
155
    const BUFFER_SET *const orig_dst, int64_t *const rd,
156
    RD_STATS *rd_stats_luma, RD_STATS *rd_stats, int *const switchable_rate,
157
    const BUFFER_SET *dst_bufs[2], int filter_idx, const int switchable_ctx[2],
158
0
    const int skip_pred) {
159
0
  const AV1_COMMON *cm = &cpi->common;
160
0
  const InterpSearchFlags *interp_search_flags = &cpi->interp_search_flags;
161
0
  const int num_planes = av1_num_planes(cm);
162
0
  MACROBLOCKD *const xd = &x->e_mbd;
163
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
164
0
  RD_STATS this_rd_stats_luma, this_rd_stats;
165
166
  // Initialize rd_stats structures to default values.
167
0
  av1_init_rd_stats(&this_rd_stats_luma);
168
0
  this_rd_stats = *rd_stats_luma;
169
0
  const int_interpfilters last_best = mbmi->interp_filters;
170
0
  mbmi->interp_filters = filter_sets[filter_idx];
171
0
  const int tmp_rs =
172
0
      get_switchable_rate(x, mbmi->interp_filters, switchable_ctx,
173
0
                          cm->seq_params->enable_dual_filter);
174
175
0
  int64_t min_rd = RDCOST(x->rdmult, tmp_rs, 0);
176
0
  if (min_rd > *rd) {
177
0
    mbmi->interp_filters = last_best;
178
0
    return 0;
179
0
  }
180
181
0
  (void)tile_data;
182
183
0
  assert(skip_pred != 2);
184
0
  assert((rd_stats_luma->rate >= 0) && (rd_stats->rate >= 0));
185
0
  assert((rd_stats_luma->dist >= 0) && (rd_stats->dist >= 0));
186
0
  assert((rd_stats_luma->sse >= 0) && (rd_stats->sse >= 0));
187
0
  assert((rd_stats_luma->skip_txfm == 0) || (rd_stats_luma->skip_txfm == 1));
188
0
  assert((rd_stats->skip_txfm == 0) || (rd_stats->skip_txfm == 1));
189
0
  assert((skip_pred >= 0) &&
190
0
         (skip_pred <= interp_search_flags->default_interp_skip_flags));
191
192
  // When skip_txfm pred is equal to default_interp_skip_flags,
193
  // skip both luma and chroma MC.
194
  // For mono-chrome images:
195
  // num_planes = 1 and cpi->default_interp_skip_flags = 1,
196
  // skip_pred = 1: skip both luma and chroma
197
  // skip_pred = 0: Evaluate luma and as num_planes=1,
198
  // skip chroma evaluation
199
0
  int tmp_skip_pred =
200
0
      (skip_pred == interp_search_flags->default_interp_skip_flags)
201
0
          ? INTERP_SKIP_LUMA_SKIP_CHROMA
202
0
          : skip_pred;
203
204
0
  switch (tmp_skip_pred) {
205
0
    case INTERP_EVAL_LUMA_EVAL_CHROMA:
206
      // skip_pred = 0: Evaluate both luma and chroma.
207
      // Luma MC
208
0
      interp_model_rd_eval(x, cpi, bsize, orig_dst, AOM_PLANE_Y, AOM_PLANE_Y,
209
0
                           &this_rd_stats_luma, 0);
210
0
      this_rd_stats = this_rd_stats_luma;
211
#if CONFIG_COLLECT_RD_STATS == 3
212
      RD_STATS rd_stats_y;
213
      av1_pick_recursive_tx_size_type_yrd(cpi, x, &rd_stats_y, bsize,
214
                                          INT64_MAX);
215
      PrintPredictionUnitStats(cpi, tile_data, x, &rd_stats_y, bsize);
216
#endif  // CONFIG_COLLECT_RD_STATS == 3
217
0
      AOM_FALLTHROUGH_INTENDED;
218
0
    case INTERP_SKIP_LUMA_EVAL_CHROMA:
219
      // skip_pred = 1: skip luma evaluation (retain previous best luma stats)
220
      // and do chroma evaluation.
221
0
      for (int plane = 1; plane < num_planes; ++plane) {
222
0
        int64_t tmp_rd =
223
0
            RDCOST(x->rdmult, tmp_rs + this_rd_stats.rate, this_rd_stats.dist);
224
0
        if (tmp_rd >= *rd) {
225
0
          mbmi->interp_filters = last_best;
226
0
          return 0;
227
0
        }
228
0
        interp_model_rd_eval(x, cpi, bsize, orig_dst, plane, plane,
229
0
                             &this_rd_stats, 0);
230
0
      }
231
0
      break;
232
0
    case INTERP_SKIP_LUMA_SKIP_CHROMA:
233
      // both luma and chroma evaluation is skipped
234
0
      this_rd_stats = *rd_stats;
235
0
      break;
236
0
    case INTERP_EVAL_INVALID:
237
0
    default: assert(0); return 0;
238
0
  }
239
0
  int64_t tmp_rd =
240
0
      RDCOST(x->rdmult, tmp_rs + this_rd_stats.rate, this_rd_stats.dist);
241
242
0
  if (tmp_rd < *rd) {
243
0
    *rd = tmp_rd;
244
0
    *switchable_rate = tmp_rs;
245
0
    if (skip_pred != interp_search_flags->default_interp_skip_flags) {
246
0
      if (skip_pred == INTERP_EVAL_LUMA_EVAL_CHROMA) {
247
        // Overwrite the data as current filter is the best one
248
0
        *rd_stats_luma = this_rd_stats_luma;
249
0
        *rd_stats = this_rd_stats;
250
        // As luma MC data is computed, no need to recompute after the search
251
0
        x->recalc_luma_mc_data = 0;
252
0
      } else if (skip_pred == INTERP_SKIP_LUMA_EVAL_CHROMA) {
253
        // As luma MC data is not computed, update of luma data can be skipped
254
0
        *rd_stats = this_rd_stats;
255
        // As luma MC data is not recomputed and current filter is the best,
256
        // indicate the possibility of recomputing MC data
257
        // If current buffer contains valid MC data, toggle to indicate that
258
        // luma MC data needs to be recomputed
259
0
        x->recalc_luma_mc_data ^= 1;
260
0
      }
261
0
      swap_dst_buf(xd, dst_bufs, num_planes);
262
0
    }
263
0
    return 1;
264
0
  }
265
0
  mbmi->interp_filters = last_best;
266
0
  return 0;
267
0
}
268
269
static inline INTERP_PRED_TYPE is_pred_filter_search_allowed(
270
    const AV1_COMP *const cpi, MACROBLOCKD *xd, BLOCK_SIZE bsize,
271
0
    int_interpfilters *af, int_interpfilters *lf) {
272
0
  const AV1_COMMON *cm = &cpi->common;
273
0
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
274
0
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
275
0
  const int bsl = mi_size_wide_log2[bsize];
276
0
  int is_horiz_eq = 0, is_vert_eq = 0;
277
278
0
  if (above_mbmi && is_inter_block(above_mbmi))
279
0
    *af = above_mbmi->interp_filters;
280
281
0
  if (left_mbmi && is_inter_block(left_mbmi)) *lf = left_mbmi->interp_filters;
282
283
0
  if (af->as_filters.x_filter != INTERP_INVALID)
284
0
    is_horiz_eq = af->as_filters.x_filter == lf->as_filters.x_filter;
285
0
  if (af->as_filters.y_filter != INTERP_INVALID)
286
0
    is_vert_eq = af->as_filters.y_filter == lf->as_filters.y_filter;
287
288
0
  INTERP_PRED_TYPE pred_filter_type = (is_vert_eq << 1) + is_horiz_eq;
289
0
  const int mi_row = xd->mi_row;
290
0
  const int mi_col = xd->mi_col;
291
0
  int pred_filter_enable =
292
0
      cpi->sf.interp_sf.cb_pred_filter_search
293
0
          ? (((mi_row + mi_col) >> bsl) +
294
0
             get_chessboard_index(cm->current_frame.frame_number)) &
295
0
                0x1
296
0
          : 0;
297
0
  pred_filter_enable &= is_horiz_eq || is_vert_eq;
298
  // pred_filter_search = 0: pred_filter is disabled
299
  // pred_filter_search = 1: pred_filter is enabled and only horz pred matching
300
  // pred_filter_search = 2: pred_filter is enabled and only vert pred matching
301
  // pred_filter_search = 3: pred_filter is enabled and
302
  //                         both vert, horz pred matching
303
0
  return pred_filter_enable * pred_filter_type;
304
0
}
305
306
static DUAL_FILTER_TYPE find_best_interp_rd_facade(
307
    MACROBLOCK *const x, const AV1_COMP *const cpi,
308
    const TileDataEnc *tile_data, BLOCK_SIZE bsize,
309
    const BUFFER_SET *const orig_dst, int64_t *const rd, RD_STATS *rd_stats_y,
310
    RD_STATS *rd_stats, int *const switchable_rate,
311
    const BUFFER_SET *dst_bufs[2], const int switchable_ctx[2],
312
0
    const int skip_pred, uint16_t allow_interp_mask, int is_w4_or_h4) {
313
0
  int tmp_skip_pred = skip_pred;
314
0
  DUAL_FILTER_TYPE best_filt_type = REG_REG;
315
316
  // If no filter are set to be evaluated, return from function
317
0
  if (allow_interp_mask == 0x0) return best_filt_type;
318
  // For block width or height is 4, skip the pred evaluation of SHARP_SHARP
319
0
  tmp_skip_pred = is_w4_or_h4
320
0
                      ? cpi->interp_search_flags.default_interp_skip_flags
321
0
                      : skip_pred;
322
323
  // Loop over the all filter types and evaluate for only allowed filter types
324
0
  for (int filt_type = SHARP_SHARP; filt_type >= REG_REG; --filt_type) {
325
0
    const int is_filter_allowed =
326
0
        get_interp_filter_allowed_mask(allow_interp_mask, filt_type);
327
0
    if (is_filter_allowed)
328
0
      if (interpolation_filter_rd(x, cpi, tile_data, bsize, orig_dst, rd,
329
0
                                  rd_stats_y, rd_stats, switchable_rate,
330
0
                                  dst_bufs, filt_type, switchable_ctx,
331
0
                                  tmp_skip_pred))
332
0
        best_filt_type = filt_type;
333
0
    tmp_skip_pred = skip_pred;
334
0
  }
335
0
  return best_filt_type;
336
0
}
337
338
static inline void pred_dual_interp_filter_rd(
339
    MACROBLOCK *const x, const AV1_COMP *const cpi,
340
    const TileDataEnc *tile_data, BLOCK_SIZE bsize,
341
    const BUFFER_SET *const orig_dst, int64_t *const rd, RD_STATS *rd_stats_y,
342
    RD_STATS *rd_stats, int *const switchable_rate,
343
    const BUFFER_SET *dst_bufs[2], const int switchable_ctx[2],
344
    const int skip_pred, INTERP_PRED_TYPE pred_filt_type, int_interpfilters *af,
345
0
    int_interpfilters *lf) {
346
0
  (void)lf;
347
0
  assert(pred_filt_type > INTERP_HORZ_NEQ_VERT_NEQ);
348
0
  assert(pred_filt_type < INTERP_PRED_TYPE_ALL);
349
0
  uint16_t allowed_interp_mask = 0;
350
351
0
  if (pred_filt_type == INTERP_HORZ_EQ_VERT_NEQ) {
352
    // pred_filter_search = 1: Only horizontal filter is matching
353
0
    allowed_interp_mask =
354
0
        av1_interp_dual_filt_mask[pred_filt_type - 1][af->as_filters.x_filter];
355
0
  } else if (pred_filt_type == INTERP_HORZ_NEQ_VERT_EQ) {
356
    // pred_filter_search = 2: Only vertical filter is matching
357
0
    allowed_interp_mask =
358
0
        av1_interp_dual_filt_mask[pred_filt_type - 1][af->as_filters.y_filter];
359
0
  } else {
360
    // pred_filter_search = 3: Both horizontal and vertical filter are matching
361
0
    int filt_type =
362
0
        af->as_filters.x_filter + af->as_filters.y_filter * SWITCHABLE_FILTERS;
363
0
    set_interp_filter_allowed_mask(&allowed_interp_mask, filt_type);
364
0
  }
365
  // REG_REG is already been evaluated in the beginning
366
0
  reset_interp_filter_allowed_mask(&allowed_interp_mask, REG_REG);
367
0
  find_best_interp_rd_facade(x, cpi, tile_data, bsize, orig_dst, rd, rd_stats_y,
368
0
                             rd_stats, switchable_rate, dst_bufs,
369
0
                             switchable_ctx, skip_pred, allowed_interp_mask, 0);
370
0
}
371
// Evaluate dual filter type
372
// a) Using above, left block interp filter
373
// b) Find the best horizontal filter and
374
//    then evaluate corresponding vertical filters.
375
static inline void fast_dual_interp_filter_rd(
376
    MACROBLOCK *const x, const AV1_COMP *const cpi,
377
    const TileDataEnc *tile_data, BLOCK_SIZE bsize,
378
    const BUFFER_SET *const orig_dst, int64_t *const rd, RD_STATS *rd_stats_y,
379
    RD_STATS *rd_stats, int *const switchable_rate,
380
    const BUFFER_SET *dst_bufs[2], const int switchable_ctx[2],
381
0
    const int skip_hor, const int skip_ver) {
382
0
  const InterpSearchFlags *interp_search_flags = &cpi->interp_search_flags;
383
0
  MACROBLOCKD *const xd = &x->e_mbd;
384
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
385
0
  INTERP_PRED_TYPE pred_filter_type = INTERP_HORZ_NEQ_VERT_NEQ;
386
0
  int_interpfilters af = av1_broadcast_interp_filter(INTERP_INVALID);
387
0
  int_interpfilters lf = af;
388
389
0
  if (!have_newmv_in_inter_mode(mbmi->mode)) {
390
0
    pred_filter_type = is_pred_filter_search_allowed(cpi, xd, bsize, &af, &lf);
391
0
  }
392
393
0
  if (pred_filter_type) {
394
0
    pred_dual_interp_filter_rd(x, cpi, tile_data, bsize, orig_dst, rd,
395
0
                               rd_stats_y, rd_stats, switchable_rate, dst_bufs,
396
0
                               switchable_ctx, (skip_hor & skip_ver),
397
0
                               pred_filter_type, &af, &lf);
398
0
  } else {
399
0
    const int bw = block_size_wide[bsize];
400
0
    const int bh = block_size_high[bsize];
401
0
    int best_dual_mode = 0;
402
0
    int skip_pred =
403
0
        bw <= 4 ? interp_search_flags->default_interp_skip_flags : skip_hor;
404
    // TODO(any): Make use of find_best_interp_rd_facade()
405
    // if speed impact is negligible
406
0
    for (int i = (SWITCHABLE_FILTERS - 1); i >= 1; --i) {
407
0
      if (interpolation_filter_rd(x, cpi, tile_data, bsize, orig_dst, rd,
408
0
                                  rd_stats_y, rd_stats, switchable_rate,
409
0
                                  dst_bufs, i, switchable_ctx, skip_pred)) {
410
0
        best_dual_mode = i;
411
0
      }
412
0
      skip_pred = skip_hor;
413
0
    }
414
    // From best of horizontal EIGHTTAP_REGULAR modes, check vertical modes
415
0
    skip_pred =
416
0
        bh <= 4 ? interp_search_flags->default_interp_skip_flags : skip_ver;
417
0
    for (int i = (best_dual_mode + (SWITCHABLE_FILTERS * 2));
418
0
         i >= (best_dual_mode + SWITCHABLE_FILTERS); i -= SWITCHABLE_FILTERS) {
419
0
      interpolation_filter_rd(x, cpi, tile_data, bsize, orig_dst, rd,
420
0
                              rd_stats_y, rd_stats, switchable_rate, dst_bufs,
421
0
                              i, switchable_ctx, skip_pred);
422
0
      skip_pred = skip_ver;
423
0
    }
424
0
  }
425
0
}
426
427
// Find the best interp filter if dual_interp_filter = 0
428
static inline void find_best_non_dual_interp_filter(
429
    MACROBLOCK *const x, const AV1_COMP *const cpi,
430
    const TileDataEnc *tile_data, BLOCK_SIZE bsize,
431
    const BUFFER_SET *const orig_dst, int64_t *const rd, RD_STATS *rd_stats_y,
432
    RD_STATS *rd_stats, int *const switchable_rate,
433
    const BUFFER_SET *dst_bufs[2], const int switchable_ctx[2],
434
0
    const int skip_ver, const int skip_hor) {
435
0
  const InterpSearchFlags *interp_search_flags = &cpi->interp_search_flags;
436
0
  int8_t i;
437
0
  MACROBLOCKD *const xd = &x->e_mbd;
438
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
439
440
0
  uint16_t interp_filter_search_mask =
441
0
      interp_search_flags->interp_filter_search_mask;
442
443
0
  if (cpi->sf.interp_sf.adaptive_interp_filter_search == 2) {
444
0
    const FRAME_UPDATE_TYPE update_type =
445
0
        get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
446
0
    const int ctx0 = av1_get_pred_context_switchable_interp(xd, 0);
447
0
    const int ctx1 = av1_get_pred_context_switchable_interp(xd, 1);
448
0
    int use_actual_frame_probs = 1;
449
0
    const int *switchable_interp_p0;
450
0
    const int *switchable_interp_p1;
451
#if CONFIG_FPMT_TEST
452
    use_actual_frame_probs =
453
        (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 0 : 1;
454
    if (!use_actual_frame_probs) {
455
      switchable_interp_p0 = (int *)cpi->ppi->temp_frame_probs
456
                                 .switchable_interp_probs[update_type][ctx0];
457
      switchable_interp_p1 = (int *)cpi->ppi->temp_frame_probs
458
                                 .switchable_interp_probs[update_type][ctx1];
459
    }
460
#endif
461
0
    if (use_actual_frame_probs) {
462
0
      switchable_interp_p0 =
463
0
          cpi->ppi->frame_probs.switchable_interp_probs[update_type][ctx0];
464
0
      switchable_interp_p1 =
465
0
          cpi->ppi->frame_probs.switchable_interp_probs[update_type][ctx1];
466
0
    }
467
0
    static const int thr[7] = { 0, 8, 8, 8, 8, 0, 8 };
468
0
    const int thresh = thr[update_type];
469
0
    for (i = 0; i < SWITCHABLE_FILTERS; i++) {
470
      // For non-dual case, the 2 dir's prob should be identical.
471
0
      assert(switchable_interp_p0[i] == switchable_interp_p1[i]);
472
0
      if (switchable_interp_p0[i] < thresh &&
473
0
          switchable_interp_p1[i] < thresh) {
474
0
        DUAL_FILTER_TYPE filt_type = i + SWITCHABLE_FILTERS * i;
475
0
        reset_interp_filter_allowed_mask(&interp_filter_search_mask, filt_type);
476
0
      }
477
0
    }
478
0
  }
479
480
  // Regular filter evaluation should have been done and hence the same should
481
  // be the winner
482
0
  assert(x->e_mbd.mi[0]->interp_filters.as_int == filter_sets[0].as_int);
483
0
  if ((skip_hor & skip_ver) != interp_search_flags->default_interp_skip_flags) {
484
0
    INTERP_PRED_TYPE pred_filter_type = INTERP_HORZ_NEQ_VERT_NEQ;
485
0
    int_interpfilters af = av1_broadcast_interp_filter(INTERP_INVALID);
486
0
    int_interpfilters lf = af;
487
488
0
    pred_filter_type = is_pred_filter_search_allowed(cpi, xd, bsize, &af, &lf);
489
0
    if (pred_filter_type) {
490
0
      assert(af.as_filters.x_filter != INTERP_INVALID);
491
0
      int filter_idx = SWITCHABLE * af.as_filters.x_filter;
492
      // This assert tells that (filter_x == filter_y) for non-dual filter case
493
0
      assert(filter_sets[filter_idx].as_filters.x_filter ==
494
0
             filter_sets[filter_idx].as_filters.y_filter);
495
0
      if (cpi->sf.interp_sf.adaptive_interp_filter_search &&
496
0
          !(get_interp_filter_allowed_mask(interp_filter_search_mask,
497
0
                                           filter_idx))) {
498
0
        return;
499
0
      }
500
0
      if (filter_idx) {
501
0
        interpolation_filter_rd(x, cpi, tile_data, bsize, orig_dst, rd,
502
0
                                rd_stats_y, rd_stats, switchable_rate, dst_bufs,
503
0
                                filter_idx, switchable_ctx,
504
0
                                (skip_hor & skip_ver));
505
0
      }
506
0
      return;
507
0
    }
508
0
  }
509
  // Reuse regular filter's modeled rd data for sharp filter for following
510
  // cases
511
  // 1) When bsize is 4x4
512
  // 2) When block width is 4 (i.e. 4x8/4x16 blocks) and MV in vertical
513
  // direction is full-pel
514
  // 3) When block height is 4 (i.e. 8x4/16x4 blocks) and MV in horizontal
515
  // direction is full-pel
516
  // TODO(any): Optimize cases 2 and 3 further if luma MV in relavant direction
517
  // alone is full-pel
518
519
0
  if ((bsize == BLOCK_4X4) ||
520
0
      (block_size_wide[bsize] == 4 &&
521
0
       skip_ver == interp_search_flags->default_interp_skip_flags) ||
522
0
      (block_size_high[bsize] == 4 &&
523
0
       skip_hor == interp_search_flags->default_interp_skip_flags)) {
524
0
    int skip_pred = skip_hor & skip_ver;
525
0
    uint16_t allowed_interp_mask = 0;
526
527
    // REG_REG filter type is evaluated beforehand, hence skip it
528
0
    set_interp_filter_allowed_mask(&allowed_interp_mask, SHARP_SHARP);
529
0
    set_interp_filter_allowed_mask(&allowed_interp_mask, SMOOTH_SMOOTH);
530
0
    if (cpi->sf.interp_sf.adaptive_interp_filter_search)
531
0
      allowed_interp_mask &= interp_filter_search_mask;
532
533
0
    find_best_interp_rd_facade(x, cpi, tile_data, bsize, orig_dst, rd,
534
0
                               rd_stats_y, rd_stats, switchable_rate, dst_bufs,
535
0
                               switchable_ctx, skip_pred, allowed_interp_mask,
536
0
                               1);
537
0
  } else {
538
0
    int skip_pred = (skip_hor & skip_ver);
539
0
    for (i = (SWITCHABLE_FILTERS + 1); i < DUAL_FILTER_SET_SIZE;
540
0
         i += (SWITCHABLE_FILTERS + 1)) {
541
      // This assert tells that (filter_x == filter_y) for non-dual filter case
542
0
      assert(filter_sets[i].as_filters.x_filter ==
543
0
             filter_sets[i].as_filters.y_filter);
544
0
      if (cpi->sf.interp_sf.adaptive_interp_filter_search &&
545
0
          !(get_interp_filter_allowed_mask(interp_filter_search_mask, i))) {
546
0
        continue;
547
0
      }
548
0
      interpolation_filter_rd(x, cpi, tile_data, bsize, orig_dst, rd,
549
0
                              rd_stats_y, rd_stats, switchable_rate, dst_bufs,
550
0
                              i, switchable_ctx, skip_pred);
551
      // In first iteration, smooth filter is evaluated. If smooth filter
552
      // (which is less sharper) is the winner among regular and smooth filters,
553
      // sharp filter evaluation is skipped
554
      // TODO(any): Refine this gating based on modelled rd only (i.e., by not
555
      // accounting switchable filter rate)
556
0
      if (cpi->sf.interp_sf.skip_sharp_interp_filter_search &&
557
0
          skip_pred != interp_search_flags->default_interp_skip_flags) {
558
0
        if (mbmi->interp_filters.as_int == filter_sets[SMOOTH_SMOOTH].as_int)
559
0
          break;
560
0
      }
561
0
    }
562
0
  }
563
0
}
564
565
static inline void calc_interp_skip_pred_flag(MACROBLOCK *const x,
566
                                              const AV1_COMP *const cpi,
567
0
                                              int *skip_hor, int *skip_ver) {
568
0
  const AV1_COMMON *cm = &cpi->common;
569
0
  MACROBLOCKD *const xd = &x->e_mbd;
570
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
571
0
  const int num_planes = av1_num_planes(cm);
572
0
  const int is_compound = has_second_ref(mbmi);
573
0
  assert(is_intrabc_block(mbmi) == 0);
574
0
  for (int ref = 0; ref < 1 + is_compound; ++ref) {
575
0
    const struct scale_factors *const sf =
576
0
        get_ref_scale_factors_const(cm, mbmi->ref_frame[ref]);
577
    // TODO(any): Refine skip flag calculation considering scaling
578
0
    if (av1_is_scaled(sf)) {
579
0
      *skip_hor = 0;
580
0
      *skip_ver = 0;
581
0
      break;
582
0
    }
583
0
    const MV mv = mbmi->mv[ref].as_mv;
584
0
    int skip_hor_plane = 0;
585
0
    int skip_ver_plane = 0;
586
0
    for (int plane_idx = 0; plane_idx < AOMMAX(1, (num_planes - 1));
587
0
         ++plane_idx) {
588
0
      struct macroblockd_plane *const pd = &xd->plane[plane_idx];
589
0
      const int bw = pd->width;
590
0
      const int bh = pd->height;
591
0
      const MV mv_q4 = clamp_mv_to_umv_border_sb(
592
0
          xd, &mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
593
0
      const int sub_x = (mv_q4.col & SUBPEL_MASK) << SCALE_EXTRA_BITS;
594
0
      const int sub_y = (mv_q4.row & SUBPEL_MASK) << SCALE_EXTRA_BITS;
595
0
      skip_hor_plane |= ((sub_x == 0) << plane_idx);
596
0
      skip_ver_plane |= ((sub_y == 0) << plane_idx);
597
0
    }
598
0
    *skip_hor &= skip_hor_plane;
599
0
    *skip_ver &= skip_ver_plane;
600
    // It is not valid that "luma MV is sub-pel, whereas chroma MV is not"
601
0
    assert(*skip_hor != 2);
602
0
    assert(*skip_ver != 2);
603
0
  }
604
  // When compond prediction type is compound segment wedge, luma MC and chroma
605
  // MC need to go hand in hand as mask generated during luma MC is reuired for
606
  // chroma MC. If skip_hor = 0 and skip_ver = 1, mask used for chroma MC during
607
  // vertical filter decision may be incorrect as temporary MC evaluation
608
  // overwrites the mask. Make skip_ver as 0 for this case so that mask is
609
  // populated during luma MC
610
0
  if (is_compound && mbmi->compound_idx == 1 &&
611
0
      mbmi->interinter_comp.type == COMPOUND_DIFFWTD) {
612
0
    assert(mbmi->comp_group_idx == 1);
613
0
    if (*skip_hor == 0 && *skip_ver == 1) *skip_ver = 0;
614
0
  }
615
0
}
616
617
/*!\brief AV1 interpolation filter search
618
 *
619
 * \ingroup inter_mode_search
620
 *
621
 * \param[in]     cpi               Top-level encoder structure.
622
 * \param[in]     tile_data         Pointer to struct holding adaptive
623
 *                                  data/contexts/models for the tile during
624
 *                                  encoding.
625
 * \param[in]     x                 Pointer to struc holding all the data for
626
 *                                  the current macroblock.
627
 * \param[in]     bsize             Current block size.
628
 * \param[in]     tmp_dst           A temporary prediction buffer to hold a
629
 *                                  computed prediction.
630
 * \param[in,out] orig_dst          A prediction buffer to hold a computed
631
 *                                  prediction. This will eventually hold the
632
 *                                  final prediction, and the tmp_dst info will
633
 *                                  be copied here.
634
 * \param[in,out] rd                The RD cost associated with the selected
635
 *                                  interpolation filter parameters.
636
 * \param[in,out] switchable_rate   The rate associated with using a SWITCHABLE
637
 *                                  filter mode.
638
 * \param[in,out] skip_build_pred   Indicates whether or not to build the inter
639
 *                                  predictor. If this is 0, the inter predictor
640
 *                                  has already been built and thus we can avoid
641
 *                                  repeating computation.
642
 * \param[in]     args              HandleInterModeArgs struct holding
643
 *                                  miscellaneous arguments for inter mode
644
 *                                  search. See the documentation for this
645
 *                                  struct for a description of each member.
646
 * \param[in]     ref_best_rd       Best RD found so far for this block.
647
 *                                  It is used for early termination of this
648
 *                                  search if the RD exceeds this value.
649
 *
650
 * \return Returns INT64_MAX if the filter parameters are invalid and the
651
 * current motion mode being tested should be skipped. It returns 0 if the
652
 * parameter search is a success.
653
 */
654
int64_t av1_interpolation_filter_search(
655
    MACROBLOCK *const x, const AV1_COMP *const cpi,
656
    const TileDataEnc *tile_data, BLOCK_SIZE bsize,
657
    const BUFFER_SET *const tmp_dst, const BUFFER_SET *const orig_dst,
658
    int64_t *const rd, int *const switchable_rate, int *skip_build_pred,
659
0
    HandleInterModeArgs *args, int64_t ref_best_rd) {
660
0
  const AV1_COMMON *cm = &cpi->common;
661
0
  const InterpSearchFlags *interp_search_flags = &cpi->interp_search_flags;
662
0
  const int num_planes = av1_num_planes(cm);
663
0
  MACROBLOCKD *const xd = &x->e_mbd;
664
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
665
0
  const int need_search = av1_is_interp_needed(xd);
666
0
  const int ref_frame = xd->mi[0]->ref_frame[0];
667
0
  RD_STATS rd_stats_luma, rd_stats;
668
669
  // Initialization of rd_stats structures with default values
670
0
  av1_init_rd_stats(&rd_stats_luma);
671
0
  av1_init_rd_stats(&rd_stats);
672
673
0
  int match_found_idx = -1;
674
0
  const InterpFilter assign_filter = cm->features.interp_filter;
675
676
0
  match_found_idx = find_interp_filter_match(
677
0
      mbmi, cpi, assign_filter, need_search, args->interp_filter_stats,
678
0
      args->interp_filter_stats_idx);
679
680
0
  if (match_found_idx != -1) {
681
0
    *rd = args->interp_filter_stats[match_found_idx].rd;
682
0
    x->pred_sse[ref_frame] =
683
0
        args->interp_filter_stats[match_found_idx].pred_sse;
684
0
    *skip_build_pred = 0;
685
0
    return 0;
686
0
  }
687
688
0
  int switchable_ctx[2];
689
0
  switchable_ctx[0] = av1_get_pred_context_switchable_interp(xd, 0);
690
0
  switchable_ctx[1] = av1_get_pred_context_switchable_interp(xd, 1);
691
0
  *switchable_rate =
692
0
      get_switchable_rate(x, mbmi->interp_filters, switchable_ctx,
693
0
                          cm->seq_params->enable_dual_filter);
694
695
  // Do MC evaluation for default filter_type.
696
  // Luma MC
697
0
  interp_model_rd_eval(x, cpi, bsize, orig_dst, AOM_PLANE_Y, AOM_PLANE_Y,
698
0
                       &rd_stats_luma, *skip_build_pred);
699
700
#if CONFIG_COLLECT_RD_STATS == 3
701
  RD_STATS rd_stats_y;
702
  av1_pick_recursive_tx_size_type_yrd(cpi, x, &rd_stats_y, bsize, INT64_MAX);
703
  PrintPredictionUnitStats(cpi, tile_data, x, &rd_stats_y, bsize);
704
#endif  // CONFIG_COLLECT_RD_STATS == 3
705
  // Chroma MC
706
0
  if (num_planes > 1) {
707
0
    interp_model_rd_eval(x, cpi, bsize, orig_dst, AOM_PLANE_U, AOM_PLANE_V,
708
0
                         &rd_stats, *skip_build_pred);
709
0
  }
710
0
  *skip_build_pred = 1;
711
712
0
  av1_merge_rd_stats(&rd_stats, &rd_stats_luma);
713
714
0
  assert(rd_stats.rate >= 0);
715
716
0
  *rd = RDCOST(x->rdmult, *switchable_rate + rd_stats.rate, rd_stats.dist);
717
0
  x->pred_sse[ref_frame] = (unsigned int)(rd_stats_luma.sse >> 4);
718
719
0
  if (assign_filter != SWITCHABLE || match_found_idx != -1) {
720
0
    return 0;
721
0
  }
722
0
  if (!need_search) {
723
0
    int_interpfilters filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
724
0
    assert(mbmi->interp_filters.as_int == filters.as_int);
725
0
    (void)filters;
726
0
    return 0;
727
0
  }
728
0
  if (args->modelled_rd != NULL) {
729
0
    if (has_second_ref(mbmi)) {
730
0
      const int ref_mv_idx = mbmi->ref_mv_idx;
731
0
      MV_REFERENCE_FRAME *refs = mbmi->ref_frame;
732
0
      const int mode0 = compound_ref0_mode(mbmi->mode);
733
0
      const int mode1 = compound_ref1_mode(mbmi->mode);
734
0
      const int64_t mrd = AOMMIN(args->modelled_rd[mode0][ref_mv_idx][refs[0]],
735
0
                                 args->modelled_rd[mode1][ref_mv_idx][refs[1]]);
736
0
      if ((*rd >> 1) > mrd && ref_best_rd < INT64_MAX) {
737
0
        return INT64_MAX;
738
0
      }
739
0
    }
740
0
  }
741
742
0
  x->recalc_luma_mc_data = 0;
743
  // skip_flag=xx (in binary form)
744
  // Setting 0th flag corresonds to skipping luma MC and setting 1st bt
745
  // corresponds to skipping chroma MC  skip_flag=0 corresponds to "Don't skip
746
  // luma and chroma MC"  Skip flag=1 corresponds to "Skip Luma MC only"
747
  // Skip_flag=2 is not a valid case
748
  // skip_flag=3 corresponds to "Skip both luma and chroma MC"
749
0
  int skip_hor = interp_search_flags->default_interp_skip_flags;
750
0
  int skip_ver = interp_search_flags->default_interp_skip_flags;
751
0
  calc_interp_skip_pred_flag(x, cpi, &skip_hor, &skip_ver);
752
753
  // do interp_filter search
754
0
  restore_dst_buf(xd, *tmp_dst, num_planes);
755
0
  const BUFFER_SET *dst_bufs[2] = { tmp_dst, orig_dst };
756
  // Evaluate dual interp filters
757
0
  if (cm->seq_params->enable_dual_filter) {
758
0
    if (cpi->sf.interp_sf.use_fast_interpolation_filter_search) {
759
0
      fast_dual_interp_filter_rd(x, cpi, tile_data, bsize, orig_dst, rd,
760
0
                                 &rd_stats_luma, &rd_stats, switchable_rate,
761
0
                                 dst_bufs, switchable_ctx, skip_hor, skip_ver);
762
0
    } else {
763
      // Use full interpolation filter search
764
0
      uint16_t allowed_interp_mask = ALLOW_ALL_INTERP_FILT_MASK;
765
      // REG_REG filter type is evaluated beforehand, so loop is repeated over
766
      // REG_SMOOTH to SHARP_SHARP for full interpolation filter search
767
0
      reset_interp_filter_allowed_mask(&allowed_interp_mask, REG_REG);
768
0
      find_best_interp_rd_facade(x, cpi, tile_data, bsize, orig_dst, rd,
769
0
                                 &rd_stats_luma, &rd_stats, switchable_rate,
770
0
                                 dst_bufs, switchable_ctx,
771
0
                                 (skip_hor & skip_ver), allowed_interp_mask, 0);
772
0
    }
773
0
  } else {
774
    // Evaluate non-dual interp filters
775
0
    find_best_non_dual_interp_filter(
776
0
        x, cpi, tile_data, bsize, orig_dst, rd, &rd_stats_luma, &rd_stats,
777
0
        switchable_rate, dst_bufs, switchable_ctx, skip_ver, skip_hor);
778
0
  }
779
0
  swap_dst_buf(xd, dst_bufs, num_planes);
780
  // Recompute final MC data if required
781
0
  if (x->recalc_luma_mc_data == 1) {
782
    // Recomputing final luma MC data is required only if the same was skipped
783
    // in either of the directions  Condition below is necessary, but not
784
    // sufficient
785
0
    assert((skip_hor == 1) || (skip_ver == 1));
786
0
    const int mi_row = xd->mi_row;
787
0
    const int mi_col = xd->mi_col;
788
0
    av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
789
0
                                  AOM_PLANE_Y, AOM_PLANE_Y);
790
0
  }
791
0
  x->pred_sse[ref_frame] = (unsigned int)(rd_stats_luma.sse >> 4);
792
793
  // save search results
794
0
  if (cpi->sf.interp_sf.use_interp_filter) {
795
0
    assert(match_found_idx == -1);
796
0
    args->interp_filter_stats_idx = save_interp_filter_search_stat(
797
0
        mbmi, *rd, x->pred_sse[ref_frame], args->interp_filter_stats,
798
0
        args->interp_filter_stats_idx);
799
0
  }
800
0
  return 0;
801
0
}