Coverage Report

Created: 2025-06-22 08:04

/src/aom/av1/encoder/reconinter_enc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016, 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 <assert.h>
13
#include <stdio.h>
14
#include <limits.h>
15
16
#include "config/aom_config.h"
17
#include "config/aom_dsp_rtcd.h"
18
#include "config/aom_scale_rtcd.h"
19
20
#include "aom/aom_integer.h"
21
#include "aom_dsp/blend.h"
22
23
#include "av1/common/av1_common_int.h"
24
#include "av1/common/blockd.h"
25
#include "av1/common/mvref_common.h"
26
#include "av1/common/obmc.h"
27
#include "av1/common/reconinter.h"
28
#include "av1/common/reconintra.h"
29
#include "av1/encoder/reconinter_enc.h"
30
31
static inline void enc_calc_subpel_params(
32
    const MV *const src_mv, InterPredParams *const inter_pred_params,
33
0
    uint8_t **pre, SubpelParams *subpel_params, int *src_stride) {
34
0
  struct buf_2d *pre_buf = &inter_pred_params->ref_frame_buf;
35
0
  init_subpel_params(src_mv, inter_pred_params, subpel_params, pre_buf->width,
36
0
                     pre_buf->height);
37
0
  *pre = pre_buf->buf0 +
38
0
         (subpel_params->pos_y >> SCALE_SUBPEL_BITS) * pre_buf->stride +
39
0
         (subpel_params->pos_x >> SCALE_SUBPEL_BITS);
40
0
  *src_stride = pre_buf->stride;
41
0
}
42
43
#define IS_DEC 0
44
#include "av1/common/reconinter_template.inc"
45
#undef IS_DEC
46
47
void av1_enc_build_one_inter_predictor(uint8_t *dst, int dst_stride,
48
                                       const MV *src_mv,
49
0
                                       InterPredParams *inter_pred_params) {
50
0
  build_one_inter_predictor(dst, dst_stride, src_mv, inter_pred_params);
51
0
}
52
53
static void enc_build_inter_predictors(const AV1_COMMON *cm, MACROBLOCKD *xd,
54
                                       int plane, const MB_MODE_INFO *mi,
55
0
                                       int bw, int bh, int mi_x, int mi_y) {
56
0
  build_inter_predictors(cm, xd, plane, mi, /*build_for_obmc=*/0, bw, bh, mi_x,
57
0
                         mi_y);
58
0
}
59
60
0
void av1_enc_build_inter_predictor_y(MACROBLOCKD *xd, int mi_row, int mi_col) {
61
0
  const int mi_x = mi_col * MI_SIZE;
62
0
  const int mi_y = mi_row * MI_SIZE;
63
0
  struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
64
0
  InterPredParams inter_pred_params;
65
66
0
  struct buf_2d *const dst_buf = &pd->dst;
67
0
  uint8_t *const dst = dst_buf->buf;
68
0
  const MV mv = xd->mi[0]->mv[0].as_mv;
69
0
  const struct scale_factors *const sf = xd->block_ref_scale_factors[0];
70
71
0
  av1_init_inter_params(&inter_pred_params, pd->width, pd->height, mi_y, mi_x,
72
0
                        pd->subsampling_x, pd->subsampling_y, xd->bd,
73
0
                        is_cur_buf_hbd(xd), false, sf, pd->pre,
74
0
                        xd->mi[0]->interp_filters);
75
76
0
  inter_pred_params.conv_params = get_conv_params_no_round(
77
0
      0, AOM_PLANE_Y, xd->tmp_conv_dst, MAX_SB_SIZE, false, xd->bd);
78
79
0
  inter_pred_params.conv_params.use_dist_wtd_comp_avg = 0;
80
0
  av1_enc_build_one_inter_predictor(dst, dst_buf->stride, &mv,
81
0
                                    &inter_pred_params);
82
0
}
83
84
void av1_enc_build_inter_predictor_y_nonrd(MACROBLOCKD *xd,
85
                                           InterPredParams *inter_pred_params,
86
0
                                           const SubpelParams *subpel_params) {
87
0
  struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
88
89
0
  const MB_MODE_INFO *mbmi = xd->mi[0];
90
0
  struct buf_2d *const dst_buf = &pd->dst;
91
0
  const struct buf_2d *pre_buf = &pd->pre[0];
92
0
  const uint8_t *src =
93
0
      pre_buf->buf0 +
94
0
      (subpel_params->pos_y >> SCALE_SUBPEL_BITS) * pre_buf->stride +
95
0
      (subpel_params->pos_x >> SCALE_SUBPEL_BITS);
96
0
  uint8_t *const dst = dst_buf->buf;
97
0
  int src_stride = pre_buf->stride;
98
0
  int dst_stride = dst_buf->stride;
99
0
  inter_pred_params->ref_frame_buf = *pre_buf;
100
101
  // Initialize interp filter for single reference mode.
102
0
  init_interp_filter_params(inter_pred_params->interp_filter_params,
103
0
                            &mbmi->interp_filters.as_filters, pd->width,
104
0
                            pd->height, /*is_intrabc=*/0);
105
106
0
  av1_make_inter_predictor(src, src_stride, dst, dst_stride, inter_pred_params,
107
0
                           subpel_params);
108
0
}
109
110
void av1_enc_build_inter_predictor(const AV1_COMMON *cm, MACROBLOCKD *xd,
111
                                   int mi_row, int mi_col,
112
                                   const BUFFER_SET *ctx, BLOCK_SIZE bsize,
113
0
                                   int plane_from, int plane_to) {
114
0
  for (int plane = plane_from; plane <= plane_to; ++plane) {
115
0
    if (plane && !xd->is_chroma_ref) break;
116
0
    const int mi_x = mi_col * MI_SIZE;
117
0
    const int mi_y = mi_row * MI_SIZE;
118
0
    enc_build_inter_predictors(cm, xd, plane, xd->mi[0], xd->plane[plane].width,
119
0
                               xd->plane[plane].height, mi_x, mi_y);
120
121
0
    if (is_interintra_pred(xd->mi[0])) {
122
0
      BUFFER_SET default_ctx = {
123
0
        { xd->plane[0].dst.buf, xd->plane[1].dst.buf, xd->plane[2].dst.buf },
124
0
        { xd->plane[0].dst.stride, xd->plane[1].dst.stride,
125
0
          xd->plane[2].dst.stride }
126
0
      };
127
0
      if (!ctx) {
128
0
        ctx = &default_ctx;
129
0
      }
130
0
      av1_build_interintra_predictor(cm, xd, xd->plane[plane].dst.buf,
131
0
                                     xd->plane[plane].dst.stride, ctx, plane,
132
0
                                     bsize);
133
0
    }
134
0
  }
135
0
}
136
137
static void setup_address_for_obmc(MACROBLOCKD *xd, int mi_row_offset,
138
                                   int mi_col_offset, MB_MODE_INFO *ref_mbmi,
139
                                   struct build_prediction_ctxt *ctxt,
140
0
                                   const int num_planes) {
141
0
  const BLOCK_SIZE ref_bsize = AOMMAX(BLOCK_8X8, ref_mbmi->bsize);
142
0
  const int ref_mi_row = xd->mi_row + mi_row_offset;
143
0
  const int ref_mi_col = xd->mi_col + mi_col_offset;
144
145
0
  for (int plane = 0; plane < num_planes; ++plane) {
146
0
    struct macroblockd_plane *const pd = &xd->plane[plane];
147
0
    setup_pred_plane(&pd->dst, ref_bsize, ctxt->tmp_buf[plane],
148
0
                     ctxt->tmp_width[plane], ctxt->tmp_height[plane],
149
0
                     ctxt->tmp_stride[plane], mi_row_offset, mi_col_offset,
150
0
                     NULL, pd->subsampling_x, pd->subsampling_y);
151
0
  }
152
153
0
  const MV_REFERENCE_FRAME frame = ref_mbmi->ref_frame[0];
154
155
0
  const RefCntBuffer *const ref_buf = get_ref_frame_buf(ctxt->cm, frame);
156
0
  const struct scale_factors *const sf =
157
0
      get_ref_scale_factors_const(ctxt->cm, frame);
158
159
0
  xd->block_ref_scale_factors[0] = sf;
160
0
  if (!av1_is_valid_scale(sf))
161
0
    aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
162
0
                       "Reference frame has invalid dimensions");
163
164
0
  av1_setup_pre_planes(xd, 0, &ref_buf->buf, ref_mi_row, ref_mi_col, sf,
165
0
                       num_planes);
166
0
}
167
168
static inline void build_obmc_prediction(MACROBLOCKD *xd, int rel_mi_row,
169
                                         int rel_mi_col, uint8_t op_mi_size,
170
                                         int dir, MB_MODE_INFO *above_mbmi,
171
0
                                         void *fun_ctxt, const int num_planes) {
172
0
  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
173
0
  setup_address_for_obmc(xd, rel_mi_row, rel_mi_col, above_mbmi, ctxt,
174
0
                         num_planes);
175
176
0
  const int mi_x = (xd->mi_col + rel_mi_col) << MI_SIZE_LOG2;
177
0
  const int mi_y = (xd->mi_row + rel_mi_row) << MI_SIZE_LOG2;
178
179
0
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
180
181
0
  InterPredParams inter_pred_params;
182
183
0
  for (int j = 0; j < num_planes; ++j) {
184
0
    const struct macroblockd_plane *pd = &xd->plane[j];
185
0
    int bw = 0, bh = 0;
186
187
0
    if (dir) {
188
      // prepare left reference block size
189
0
      bw = clamp(block_size_wide[bsize] >> (pd->subsampling_x + 1), 4,
190
0
                 block_size_wide[BLOCK_64X64] >> (pd->subsampling_x + 1));
191
0
      bh = (op_mi_size << MI_SIZE_LOG2) >> pd->subsampling_y;
192
0
    } else {
193
      // prepare above reference block size
194
0
      bw = (op_mi_size * MI_SIZE) >> pd->subsampling_x;
195
0
      bh = clamp(block_size_high[bsize] >> (pd->subsampling_y + 1), 4,
196
0
                 block_size_high[BLOCK_64X64] >> (pd->subsampling_y + 1));
197
0
    }
198
199
0
    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, dir)) continue;
200
201
0
    const struct buf_2d *const pre_buf = &pd->pre[0];
202
0
    const MV mv = above_mbmi->mv[0].as_mv;
203
204
0
    av1_init_inter_params(&inter_pred_params, bw, bh, mi_y >> pd->subsampling_y,
205
0
                          mi_x >> pd->subsampling_x, pd->subsampling_x,
206
0
                          pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd), 0,
207
0
                          xd->block_ref_scale_factors[0], pre_buf,
208
0
                          above_mbmi->interp_filters);
209
0
    inter_pred_params.conv_params = get_conv_params(0, j, xd->bd);
210
211
0
    av1_enc_build_one_inter_predictor(pd->dst.buf, pd->dst.stride, &mv,
212
0
                                      &inter_pred_params);
213
0
  }
214
0
}
215
216
void av1_build_prediction_by_above_preds(const AV1_COMMON *cm, MACROBLOCKD *xd,
217
                                         uint8_t *tmp_buf[MAX_MB_PLANE],
218
                                         int tmp_width[MAX_MB_PLANE],
219
                                         int tmp_height[MAX_MB_PLANE],
220
0
                                         int tmp_stride[MAX_MB_PLANE]) {
221
0
  if (!xd->up_available) return;
222
0
  struct build_prediction_ctxt ctxt = {
223
0
    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_right_edge, NULL
224
0
  };
225
0
  BLOCK_SIZE bsize = xd->mi[0]->bsize;
226
0
  foreach_overlappable_nb_above(cm, xd,
227
0
                                max_neighbor_obmc[mi_size_wide_log2[bsize]],
228
0
                                build_obmc_prediction, &ctxt);
229
0
}
230
231
void av1_build_prediction_by_left_preds(const AV1_COMMON *cm, MACROBLOCKD *xd,
232
                                        uint8_t *tmp_buf[MAX_MB_PLANE],
233
                                        int tmp_width[MAX_MB_PLANE],
234
                                        int tmp_height[MAX_MB_PLANE],
235
0
                                        int tmp_stride[MAX_MB_PLANE]) {
236
0
  if (!xd->left_available) return;
237
0
  struct build_prediction_ctxt ctxt = {
238
0
    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_bottom_edge, NULL
239
0
  };
240
0
  BLOCK_SIZE bsize = xd->mi[0]->bsize;
241
0
  foreach_overlappable_nb_left(cm, xd,
242
0
                               max_neighbor_obmc[mi_size_high_log2[bsize]],
243
0
                               build_obmc_prediction, &ctxt);
244
0
}
245
246
0
void av1_build_obmc_inter_predictors_sb(const AV1_COMMON *cm, MACROBLOCKD *xd) {
247
0
  const int num_planes = av1_num_planes(cm);
248
0
  uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
249
0
  int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
250
0
  int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
251
0
  int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
252
0
  int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
253
0
  int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
254
0
  int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
255
256
0
  av1_setup_obmc_dst_bufs(xd, dst_buf1, dst_buf2);
257
258
0
  const int mi_row = xd->mi_row;
259
0
  const int mi_col = xd->mi_col;
260
0
  av1_build_prediction_by_above_preds(cm, xd, dst_buf1, dst_width1, dst_height1,
261
0
                                      dst_stride1);
262
0
  av1_build_prediction_by_left_preds(cm, xd, dst_buf2, dst_width2, dst_height2,
263
0
                                     dst_stride2);
264
0
  av1_setup_dst_planes(xd->plane, xd->mi[0]->bsize, &cm->cur_frame->buf, mi_row,
265
0
                       mi_col, 0, num_planes);
266
0
  av1_build_obmc_inter_prediction(cm, xd, dst_buf1, dst_stride1, dst_buf2,
267
0
                                  dst_stride2);
268
0
}
269
270
void av1_build_inter_predictors_for_planes_single_buf(
271
    MACROBLOCKD *xd, BLOCK_SIZE bsize, int plane_from, int plane_to, int ref,
272
0
    uint8_t *ext_dst[], int ext_dst_stride[]) {
273
0
  assert(bsize < BLOCK_SIZES_ALL);
274
0
  const MB_MODE_INFO *mi = xd->mi[0];
275
0
  const int mi_row = xd->mi_row;
276
0
  const int mi_col = xd->mi_col;
277
0
  const int mi_x = mi_col * MI_SIZE;
278
0
  const int mi_y = mi_row * MI_SIZE;
279
0
  WarpTypesAllowed warp_types;
280
0
  const WarpedMotionParams *const wm = &xd->global_motion[mi->ref_frame[ref]];
281
0
  warp_types.global_warp_allowed = is_global_mv_block(mi, wm->wmtype);
282
0
  warp_types.local_warp_allowed = mi->motion_mode == WARPED_CAUSAL;
283
284
0
  for (int plane = plane_from; plane <= plane_to; ++plane) {
285
0
    const struct macroblockd_plane *pd = &xd->plane[plane];
286
0
    const BLOCK_SIZE plane_bsize =
287
0
        get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
288
0
    const int bw = block_size_wide[plane_bsize];
289
0
    const int bh = block_size_high[plane_bsize];
290
291
0
    InterPredParams inter_pred_params;
292
293
0
    av1_init_inter_params(&inter_pred_params, bw, bh, mi_y >> pd->subsampling_y,
294
0
                          mi_x >> pd->subsampling_x, pd->subsampling_x,
295
0
                          pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd), 0,
296
0
                          xd->block_ref_scale_factors[ref], &pd->pre[ref],
297
0
                          mi->interp_filters);
298
0
    inter_pred_params.conv_params = get_conv_params(0, plane, xd->bd);
299
0
    av1_init_warp_params(&inter_pred_params, &warp_types, ref, xd, mi);
300
301
0
    uint8_t *const dst = get_buf_by_bd(xd, ext_dst[plane]);
302
0
    const MV mv = mi->mv[ref].as_mv;
303
304
0
    av1_enc_build_one_inter_predictor(dst, ext_dst_stride[plane], &mv,
305
0
                                      &inter_pred_params);
306
0
  }
307
0
}
308
309
static void build_masked_compound(
310
    uint8_t *dst, int dst_stride, const uint8_t *src0, int src0_stride,
311
    const uint8_t *src1, int src1_stride,
312
    const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE sb_type, int h,
313
0
    int w) {
314
  // Derive subsampling from h and w passed in. May be refactored to
315
  // pass in subsampling factors directly.
316
0
  const int subh = (2 << mi_size_high_log2[sb_type]) == h;
317
0
  const int subw = (2 << mi_size_wide_log2[sb_type]) == w;
318
0
  const uint8_t *mask = av1_get_compound_type_mask(comp_data, sb_type);
319
0
  aom_blend_a64_mask(dst, dst_stride, src0, src0_stride, src1, src1_stride,
320
0
                     mask, block_size_wide[sb_type], w, h, subw, subh);
321
0
}
322
323
#if CONFIG_AV1_HIGHBITDEPTH
324
static void build_masked_compound_highbd(
325
    uint8_t *dst_8, int dst_stride, const uint8_t *src0_8, int src0_stride,
326
    const uint8_t *src1_8, int src1_stride,
327
    const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE sb_type, int h,
328
0
    int w, int bd) {
329
  // Derive subsampling from h and w passed in. May be refactored to
330
  // pass in subsampling factors directly.
331
0
  const int subh = (2 << mi_size_high_log2[sb_type]) == h;
332
0
  const int subw = (2 << mi_size_wide_log2[sb_type]) == w;
333
0
  const uint8_t *mask = av1_get_compound_type_mask(comp_data, sb_type);
334
  // const uint8_t *mask =
335
  //     av1_get_contiguous_soft_mask(wedge_index, wedge_sign, sb_type);
336
0
  aom_highbd_blend_a64_mask(dst_8, dst_stride, src0_8, src0_stride, src1_8,
337
0
                            src1_stride, mask, block_size_wide[sb_type], w, h,
338
0
                            subw, subh, bd);
339
0
}
340
#endif
341
342
static void build_wedge_inter_predictor_from_buf(
343
    MACROBLOCKD *xd, int plane, int x, int y, int w, int h, uint8_t *ext_dst0,
344
0
    int ext_dst_stride0, uint8_t *ext_dst1, int ext_dst_stride1) {
345
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
346
0
  const int is_compound = has_second_ref(mbmi);
347
0
  MACROBLOCKD_PLANE *const pd = &xd->plane[plane];
348
0
  struct buf_2d *const dst_buf = &pd->dst;
349
0
  uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
350
0
  mbmi->interinter_comp.seg_mask = xd->seg_mask;
351
0
  const INTERINTER_COMPOUND_DATA *comp_data = &mbmi->interinter_comp;
352
0
  const int is_hbd = is_cur_buf_hbd(xd);
353
354
0
  if (is_compound && is_masked_compound_type(comp_data->type)) {
355
0
    if (!plane && comp_data->type == COMPOUND_DIFFWTD) {
356
0
#if CONFIG_AV1_HIGHBITDEPTH
357
0
      if (is_hbd) {
358
0
        av1_build_compound_diffwtd_mask_highbd(
359
0
            comp_data->seg_mask, comp_data->mask_type,
360
0
            CONVERT_TO_BYTEPTR(ext_dst0), ext_dst_stride0,
361
0
            CONVERT_TO_BYTEPTR(ext_dst1), ext_dst_stride1, h, w, xd->bd);
362
0
      } else {
363
0
        av1_build_compound_diffwtd_mask(
364
0
            comp_data->seg_mask, comp_data->mask_type, ext_dst0,
365
0
            ext_dst_stride0, ext_dst1, ext_dst_stride1, h, w);
366
0
      }
367
#else
368
      (void)is_hbd;
369
      av1_build_compound_diffwtd_mask(comp_data->seg_mask, comp_data->mask_type,
370
                                      ext_dst0, ext_dst_stride0, ext_dst1,
371
                                      ext_dst_stride1, h, w);
372
#endif  // CONFIG_AV1_HIGHBITDEPTH
373
0
    }
374
0
#if CONFIG_AV1_HIGHBITDEPTH
375
0
    if (is_hbd) {
376
0
      build_masked_compound_highbd(
377
0
          dst, dst_buf->stride, CONVERT_TO_BYTEPTR(ext_dst0), ext_dst_stride0,
378
0
          CONVERT_TO_BYTEPTR(ext_dst1), ext_dst_stride1, comp_data, mbmi->bsize,
379
0
          h, w, xd->bd);
380
0
    } else {
381
0
      build_masked_compound(dst, dst_buf->stride, ext_dst0, ext_dst_stride0,
382
0
                            ext_dst1, ext_dst_stride1, comp_data, mbmi->bsize,
383
0
                            h, w);
384
0
    }
385
#else
386
    build_masked_compound(dst, dst_buf->stride, ext_dst0, ext_dst_stride0,
387
                          ext_dst1, ext_dst_stride1, comp_data, mbmi->bsize, h,
388
                          w);
389
#endif
390
0
  } else {
391
0
#if CONFIG_AV1_HIGHBITDEPTH
392
0
    if (is_hbd) {
393
0
      aom_highbd_convolve_copy(CONVERT_TO_SHORTPTR(ext_dst0), ext_dst_stride0,
394
0
                               CONVERT_TO_SHORTPTR(dst), dst_buf->stride, w, h);
395
0
    } else {
396
0
      aom_convolve_copy(ext_dst0, ext_dst_stride0, dst, dst_buf->stride, w, h);
397
0
    }
398
#else
399
    aom_convolve_copy(ext_dst0, ext_dst_stride0, dst, dst_buf->stride, w, h);
400
#endif
401
0
  }
402
0
}
403
404
void av1_build_wedge_inter_predictor_from_buf(MACROBLOCKD *xd, BLOCK_SIZE bsize,
405
                                              int plane_from, int plane_to,
406
                                              uint8_t *ext_dst0[],
407
                                              int ext_dst_stride0[],
408
                                              uint8_t *ext_dst1[],
409
0
                                              int ext_dst_stride1[]) {
410
0
  int plane;
411
0
  assert(bsize < BLOCK_SIZES_ALL);
412
0
  for (plane = plane_from; plane <= plane_to; ++plane) {
413
0
    const BLOCK_SIZE plane_bsize = get_plane_block_size(
414
0
        bsize, xd->plane[plane].subsampling_x, xd->plane[plane].subsampling_y);
415
0
    const int bw = block_size_wide[plane_bsize];
416
0
    const int bh = block_size_high[plane_bsize];
417
0
    build_wedge_inter_predictor_from_buf(
418
0
        xd, plane, 0, 0, bw, bh, ext_dst0[plane], ext_dst_stride0[plane],
419
0
        ext_dst1[plane], ext_dst_stride1[plane]);
420
0
  }
421
0
}
422
423
// Get pred block from up-sampled reference.
424
void aom_upsampled_pred_c(MACROBLOCKD *xd, const AV1_COMMON *const cm,
425
                          int mi_row, int mi_col, const MV *const mv,
426
                          uint8_t *comp_pred, int width, int height,
427
                          int subpel_x_q3, int subpel_y_q3, const uint8_t *ref,
428
0
                          int ref_stride, int subpel_search) {
429
  // expect xd == NULL only in tests
430
0
  if (xd != NULL) {
431
0
    const MB_MODE_INFO *mi = xd->mi[0];
432
0
    const int ref_num = 0;
433
0
    const int is_intrabc = is_intrabc_block(mi);
434
0
    const struct scale_factors *const sf =
435
0
        is_intrabc ? &cm->sf_identity : xd->block_ref_scale_factors[ref_num];
436
0
    const int is_scaled = av1_is_scaled(sf);
437
438
0
    if (is_scaled) {
439
0
      int plane = 0;
440
0
      const int mi_x = mi_col * MI_SIZE;
441
0
      const int mi_y = mi_row * MI_SIZE;
442
0
      const struct macroblockd_plane *const pd = &xd->plane[plane];
443
0
      const struct buf_2d *const dst_buf = &pd->dst;
444
0
      const struct buf_2d *const pre_buf =
445
0
          is_intrabc ? dst_buf : &pd->pre[ref_num];
446
447
0
      InterPredParams inter_pred_params;
448
0
      inter_pred_params.conv_params = get_conv_params(0, plane, xd->bd);
449
0
      const int_interpfilters filters =
450
0
          av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
451
0
      av1_init_inter_params(
452
0
          &inter_pred_params, width, height, mi_y >> pd->subsampling_y,
453
0
          mi_x >> pd->subsampling_x, pd->subsampling_x, pd->subsampling_y,
454
0
          xd->bd, is_cur_buf_hbd(xd), is_intrabc, sf, pre_buf, filters);
455
0
      av1_enc_build_one_inter_predictor(comp_pred, width, mv,
456
0
                                        &inter_pred_params);
457
0
      return;
458
0
    }
459
0
  }
460
461
0
  const InterpFilterParams *filter = av1_get_filter(subpel_search);
462
463
0
  if (!subpel_x_q3 && !subpel_y_q3) {
464
0
    for (int i = 0; i < height; i++) {
465
0
      memcpy(comp_pred, ref, width * sizeof(*comp_pred));
466
0
      comp_pred += width;
467
0
      ref += ref_stride;
468
0
    }
469
0
  } else if (!subpel_y_q3) {
470
0
    const int16_t *const kernel =
471
0
        av1_get_interp_filter_subpel_kernel(filter, subpel_x_q3 << 1);
472
0
    aom_convolve8_horiz_c(ref, ref_stride, comp_pred, width, kernel, 16, NULL,
473
0
                          -1, width, height);
474
0
  } else if (!subpel_x_q3) {
475
0
    const int16_t *const kernel =
476
0
        av1_get_interp_filter_subpel_kernel(filter, subpel_y_q3 << 1);
477
0
    aom_convolve8_vert_c(ref, ref_stride, comp_pred, width, NULL, -1, kernel,
478
0
                         16, width, height);
479
0
  } else {
480
0
    DECLARE_ALIGNED(16, uint8_t,
481
0
                    temp[((MAX_SB_SIZE * 2 + 16) + 16) * MAX_SB_SIZE]);
482
0
    const int16_t *const kernel_x =
483
0
        av1_get_interp_filter_subpel_kernel(filter, subpel_x_q3 << 1);
484
0
    const int16_t *const kernel_y =
485
0
        av1_get_interp_filter_subpel_kernel(filter, subpel_y_q3 << 1);
486
0
    const int intermediate_height =
487
0
        (((height - 1) * 8 + subpel_y_q3) >> 3) + filter->taps;
488
0
    assert(intermediate_height <= (MAX_SB_SIZE * 2 + 16) + 16);
489
0
    aom_convolve8_horiz_c(ref - ref_stride * ((filter->taps >> 1) - 1),
490
0
                          ref_stride, temp, MAX_SB_SIZE, kernel_x, 16, NULL, -1,
491
0
                          width, intermediate_height);
492
0
    aom_convolve8_vert_c(temp + MAX_SB_SIZE * ((filter->taps >> 1) - 1),
493
0
                         MAX_SB_SIZE, comp_pred, width, NULL, -1, kernel_y, 16,
494
0
                         width, height);
495
0
  }
496
0
}
497
498
void aom_comp_avg_upsampled_pred_c(MACROBLOCKD *xd, const AV1_COMMON *const cm,
499
                                   int mi_row, int mi_col, const MV *const mv,
500
                                   uint8_t *comp_pred, const uint8_t *pred,
501
                                   int width, int height, int subpel_x_q3,
502
                                   int subpel_y_q3, const uint8_t *ref,
503
0
                                   int ref_stride, int subpel_search) {
504
0
  int i, j;
505
506
0
  aom_upsampled_pred_c(xd, cm, mi_row, mi_col, mv, comp_pred, width, height,
507
0
                       subpel_x_q3, subpel_y_q3, ref, ref_stride,
508
0
                       subpel_search);
509
0
  for (i = 0; i < height; i++) {
510
0
    for (j = 0; j < width; j++) {
511
0
      comp_pred[j] = ROUND_POWER_OF_TWO(comp_pred[j] + pred[j], 1);
512
0
    }
513
0
    comp_pred += width;
514
0
    pred += width;
515
0
  }
516
0
}
517
518
void aom_comp_mask_upsampled_pred(MACROBLOCKD *xd, const AV1_COMMON *const cm,
519
                                  int mi_row, int mi_col, const MV *const mv,
520
                                  uint8_t *comp_pred, const uint8_t *pred,
521
                                  int width, int height, int subpel_x_q3,
522
                                  int subpel_y_q3, const uint8_t *ref,
523
                                  int ref_stride, const uint8_t *mask,
524
                                  int mask_stride, int invert_mask,
525
0
                                  int subpel_search) {
526
0
  if (subpel_x_q3 | subpel_y_q3) {
527
0
    aom_upsampled_pred(xd, cm, mi_row, mi_col, mv, comp_pred, width, height,
528
0
                       subpel_x_q3, subpel_y_q3, ref, ref_stride,
529
0
                       subpel_search);
530
0
    ref = comp_pred;
531
0
    ref_stride = width;
532
0
  }
533
0
  aom_comp_mask_pred(comp_pred, pred, width, height, ref, ref_stride, mask,
534
0
                     mask_stride, invert_mask);
535
0
}
536
537
#if CONFIG_AV1_HIGHBITDEPTH
538
void aom_highbd_upsampled_pred_c(MACROBLOCKD *xd,
539
                                 const struct AV1Common *const cm, int mi_row,
540
                                 int mi_col, const MV *const mv,
541
                                 uint8_t *comp_pred8, int width, int height,
542
                                 int subpel_x_q3, int subpel_y_q3,
543
                                 const uint8_t *ref8, int ref_stride, int bd,
544
0
                                 int subpel_search) {
545
  // expect xd == NULL only in tests
546
0
  if (xd != NULL) {
547
0
    const MB_MODE_INFO *mi = xd->mi[0];
548
0
    const int ref_num = 0;
549
0
    const int is_intrabc = is_intrabc_block(mi);
550
0
    const struct scale_factors *const sf =
551
0
        is_intrabc ? &cm->sf_identity : xd->block_ref_scale_factors[ref_num];
552
0
    const int is_scaled = av1_is_scaled(sf);
553
554
0
    if (is_scaled) {
555
0
      int plane = 0;
556
0
      const int mi_x = mi_col * MI_SIZE;
557
0
      const int mi_y = mi_row * MI_SIZE;
558
0
      const struct macroblockd_plane *const pd = &xd->plane[plane];
559
0
      const struct buf_2d *const dst_buf = &pd->dst;
560
0
      const struct buf_2d *const pre_buf =
561
0
          is_intrabc ? dst_buf : &pd->pre[ref_num];
562
563
0
      InterPredParams inter_pred_params;
564
0
      inter_pred_params.conv_params = get_conv_params(0, plane, xd->bd);
565
0
      const int_interpfilters filters =
566
0
          av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
567
0
      av1_init_inter_params(
568
0
          &inter_pred_params, width, height, mi_y >> pd->subsampling_y,
569
0
          mi_x >> pd->subsampling_x, pd->subsampling_x, pd->subsampling_y,
570
0
          xd->bd, is_cur_buf_hbd(xd), is_intrabc, sf, pre_buf, filters);
571
0
      av1_enc_build_one_inter_predictor(comp_pred8, width, mv,
572
0
                                        &inter_pred_params);
573
0
      return;
574
0
    }
575
0
  }
576
577
0
  const InterpFilterParams *filter = av1_get_filter(subpel_search);
578
579
0
  if (!subpel_x_q3 && !subpel_y_q3) {
580
0
    const uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
581
0
    uint16_t *comp_pred = CONVERT_TO_SHORTPTR(comp_pred8);
582
0
    for (int i = 0; i < height; i++) {
583
0
      memcpy(comp_pred, ref, width * sizeof(*comp_pred));
584
0
      comp_pred += width;
585
0
      ref += ref_stride;
586
0
    }
587
0
  } else if (!subpel_y_q3) {
588
0
    const int16_t *const kernel =
589
0
        av1_get_interp_filter_subpel_kernel(filter, subpel_x_q3 << 1);
590
0
    aom_highbd_convolve8_horiz_c(ref8, ref_stride, comp_pred8, width, kernel,
591
0
                                 16, NULL, -1, width, height, bd);
592
0
  } else if (!subpel_x_q3) {
593
0
    const int16_t *const kernel =
594
0
        av1_get_interp_filter_subpel_kernel(filter, subpel_y_q3 << 1);
595
0
    aom_highbd_convolve8_vert_c(ref8, ref_stride, comp_pred8, width, NULL, -1,
596
0
                                kernel, 16, width, height, bd);
597
0
  } else {
598
0
    DECLARE_ALIGNED(16, uint16_t,
599
0
                    temp[((MAX_SB_SIZE + 16) + 16) * MAX_SB_SIZE]);
600
0
    const int16_t *const kernel_x =
601
0
        av1_get_interp_filter_subpel_kernel(filter, subpel_x_q3 << 1);
602
0
    const int16_t *const kernel_y =
603
0
        av1_get_interp_filter_subpel_kernel(filter, subpel_y_q3 << 1);
604
0
    const int intermediate_height =
605
0
        (((height - 1) * 8 + subpel_y_q3) >> 3) + filter->taps;
606
0
    assert(intermediate_height <= (MAX_SB_SIZE * 2 + 16) + 16);
607
0
    aom_highbd_convolve8_horiz_c(ref8 - ref_stride * ((filter->taps >> 1) - 1),
608
0
                                 ref_stride, CONVERT_TO_BYTEPTR(temp),
609
0
                                 MAX_SB_SIZE, kernel_x, 16, NULL, -1, width,
610
0
                                 intermediate_height, bd);
611
0
    aom_highbd_convolve8_vert_c(
612
0
        CONVERT_TO_BYTEPTR(temp + MAX_SB_SIZE * ((filter->taps >> 1) - 1)),
613
0
        MAX_SB_SIZE, comp_pred8, width, NULL, -1, kernel_y, 16, width, height,
614
0
        bd);
615
0
  }
616
0
}
617
618
void aom_highbd_comp_avg_upsampled_pred_c(
619
    MACROBLOCKD *xd, const struct AV1Common *const cm, int mi_row, int mi_col,
620
    const MV *const mv, uint8_t *comp_pred8, const uint8_t *pred8, int width,
621
    int height, int subpel_x_q3, int subpel_y_q3, const uint8_t *ref8,
622
0
    int ref_stride, int bd, int subpel_search) {
623
0
  int i, j;
624
625
0
  const uint16_t *pred = CONVERT_TO_SHORTPTR(pred8);
626
0
  uint16_t *comp_pred = CONVERT_TO_SHORTPTR(comp_pred8);
627
0
  aom_highbd_upsampled_pred(xd, cm, mi_row, mi_col, mv, comp_pred8, width,
628
0
                            height, subpel_x_q3, subpel_y_q3, ref8, ref_stride,
629
0
                            bd, subpel_search);
630
0
  for (i = 0; i < height; ++i) {
631
0
    for (j = 0; j < width; ++j) {
632
0
      comp_pred[j] = ROUND_POWER_OF_TWO(pred[j] + comp_pred[j], 1);
633
0
    }
634
0
    comp_pred += width;
635
0
    pred += width;
636
0
  }
637
0
}
638
639
void aom_highbd_comp_mask_upsampled_pred(
640
    MACROBLOCKD *xd, const struct AV1Common *const cm, int mi_row, int mi_col,
641
    const MV *const mv, uint8_t *comp_pred8, const uint8_t *pred8, int width,
642
    int height, int subpel_x_q3, int subpel_y_q3, const uint8_t *ref8,
643
    int ref_stride, const uint8_t *mask, int mask_stride, int invert_mask,
644
0
    int bd, int subpel_search) {
645
0
  aom_highbd_upsampled_pred(xd, cm, mi_row, mi_col, mv, comp_pred8, width,
646
0
                            height, subpel_x_q3, subpel_y_q3, ref8, ref_stride,
647
0
                            bd, subpel_search);
648
0
  aom_highbd_comp_mask_pred(comp_pred8, pred8, width, height, comp_pred8, width,
649
0
                            mask, mask_stride, invert_mask);
650
0
}
651
#endif  // CONFIG_AV1_HIGHBITDEPTH