blob: 77823b7f44f29336418f706afb8d6fd21331e3dc [file] [log] [blame] [edit]
/*
* Copyright (c) 2021, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* aomedia.org/license/patent-license/.
*/
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include "config/aom_config.h"
#include "config/aom_dsp_rtcd.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_mem/aom_mem.h"
#include "aom_ports/mem.h"
#include "av1/common/av1_common_int.h"
#include "av1/common/common.h"
#include "av1/common/filter.h"
#include "av1/common/mvref_common.h"
#include "av1/common/reconinter.h"
#include "av1/encoder/cost.h"
#include "av1/encoder/encoder.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/mcomp.h"
#include "av1/encoder/rdopt.h"
#include "av1/encoder/reconinter_enc.h"
static INLINE void init_mv_cost_params(MV_COST_PARAMS *mv_cost_params,
const MvCosts *mv_costs,
int is_adaptive_mvd, const MV *ref_mv,
MvSubpelPrecision pb_mv_precision
#if CONFIG_IBC_BV_IMPROVEMENT
,
const int is_ibc_cost
#endif
) {
mv_cost_params->ref_mv = ref_mv;
mv_cost_params->full_ref_mv = get_fullmv_from_mv(ref_mv);
mv_cost_params->mv_cost_type = MV_COST_ENTROPY;
mv_cost_params->mv_costs = mv_costs;
mv_cost_params->pb_mv_precision = pb_mv_precision;
mv_cost_params->is_adaptive_mvd = is_adaptive_mvd;
#if CONFIG_IBC_BV_IMPROVEMENT
mv_cost_params->is_ibc_cost = is_ibc_cost;
#endif
}
static INLINE void init_ms_buffers(MSBuffers *ms_buffers, const MACROBLOCK *x) {
ms_buffers->ref = &x->e_mbd.plane[0].pre[0];
ms_buffers->src = &x->plane[0].src;
av1_set_ms_compound_refs(ms_buffers, NULL, NULL, 0, 0);
ms_buffers->wsrc = x->obmc_buffer.wsrc;
ms_buffers->obmc_mask = x->obmc_buffer.mask;
}
static AOM_INLINE SEARCH_METHODS
get_faster_search_method(SEARCH_METHODS search_method) {
// Note on search method's accuracy:
// 1. NSTEP
// 2. DIAMOND
// 3. BIGDIA \approx SQUARE
// 4. HEX.
// 5. FAST_HEX \approx FAST_DIAMOND
switch (search_method) {
case NSTEP: return DIAMOND;
case DIAMOND: return BIGDIA;
case BIGDIA: return HEX;
case SQUARE: return HEX;
case HEX: return FAST_HEX;
case FAST_HEX: return FAST_HEX;
case FAST_DIAMOND: return FAST_DIAMOND;
case FAST_BIGDIA: return FAST_BIGDIA;
default: assert(0 && "Invalid search method!"); return DIAMOND;
}
}
void av1_make_default_fullpel_ms_params(
FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const struct AV1_COMP *cpi,
const MACROBLOCK *x, BLOCK_SIZE bsize, const MV *ref_mv,
const MvSubpelPrecision pb_mv_precision,
#if CONFIG_IBC_BV_IMPROVEMENT
const int is_ibc_cost,
#endif
const search_site_config search_sites[NUM_DISTINCT_SEARCH_METHODS],
int fine_search_interval) {
const MV_SPEED_FEATURES *mv_sf = &cpi->sf.mv_sf;
const MACROBLOCKD *const xd = &x->e_mbd;
MB_MODE_INFO *mbmi = xd->mi[0];
const int is_adaptive_mvd =
enable_adaptive_mvd_resolution(&cpi->common, mbmi);
ms_params->xd = xd;
// High level params
ms_params->bsize = bsize;
ms_params->vfp = &cpi->fn_ptr[bsize];
init_ms_buffers(&ms_params->ms_buffers, x);
SEARCH_METHODS search_method = mv_sf->search_method;
const int min_dim = AOMMIN(block_size_wide[bsize], block_size_high[bsize]);
if (mv_sf->use_bsize_dependent_search_method) {
if (min_dim >= 32) {
search_method = get_faster_search_method(search_method);
}
}
#if CONFIG_BLOCK_256
const int max_dim = AOMMAX(block_size_wide[bsize], block_size_high[bsize]);
if (cpi->sf.mv_sf.fast_motion_estimation_on_block_256 && max_dim >= 256) {
search_method = get_faster_search_method(search_method);
}
#endif // CONFIG_BLOCK_256
// MV search of flex MV precision is supported only for NSTEP or DIAMOND
// search
if (cpi->common.seq_params.enable_flex_mvres &&
(search_method != NSTEP && search_method != DIAMOND))
search_method = NSTEP;
av1_set_mv_search_method(ms_params, search_sites, search_method);
const int use_downsampled_sad =
mv_sf->use_downsampled_sad && block_size_high[bsize] >= 16;
if (use_downsampled_sad) {
ms_params->sdf = ms_params->vfp->sdsf;
ms_params->sdx4df = ms_params->vfp->sdsx4df;
} else {
ms_params->sdf = ms_params->vfp->sdf;
ms_params->sdx4df = ms_params->vfp->sdx4df;
}
ms_params->mesh_patterns[0] = mv_sf->mesh_patterns;
ms_params->mesh_patterns[1] = mv_sf->intrabc_mesh_patterns;
ms_params->force_mesh_thresh = mv_sf->exhaustive_searches_thresh;
ms_params->prune_mesh_search = mv_sf->prune_mesh_search;
ms_params->run_mesh_search = 0;
ms_params->fine_search_interval = fine_search_interval;
ms_params->is_intra_mode = 0;
ms_params->fast_obmc_search =
(pb_mv_precision == mbmi->max_mv_precision)
? mv_sf->obmc_full_pixel_search_level
: cpi->sf.flexmv_sf.low_prec_obmc_full_pixel_search_level;
ms_params->mv_limits = x->mv_limits;
if (is_tip_ref_frame(mbmi->ref_frame[0])) {
av1_set_tip_mv_search_range(&ms_params->mv_limits);
} else {
av1_set_mv_search_range(&ms_params->mv_limits, ref_mv, pb_mv_precision
);
}
// Mvcost params
init_mv_cost_params(&ms_params->mv_cost_params, &x->mv_costs, is_adaptive_mvd,
ref_mv, pb_mv_precision
#if CONFIG_IBC_BV_IMPROVEMENT
,
is_ibc_cost
#endif
);
}
void av1_make_default_subpel_ms_params(SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
const struct AV1_COMP *cpi,
const MACROBLOCK *x, BLOCK_SIZE bsize,
const MV *ref_mv,
const MvSubpelPrecision pb_mv_precision,
const int *cost_list) {
const AV1_COMMON *cm = &cpi->common;
#if CONFIG_IBC_BV_IMPROVEMENT
const int is_ibc_cost = 0;
#endif
const MACROBLOCKD *const xd = &x->e_mbd;
MB_MODE_INFO *mbmi = xd->mi[0];
const int is_adaptive_mvd = enable_adaptive_mvd_resolution(cm, mbmi);
#if !BUGFIX_AMVD_AMVR
assert(
!(is_adaptive_mvd && (mbmi->pb_mv_precision != mbmi->max_mv_precision)));
#endif // BUGFIX_AMVD_AMVR
// High level params
ms_params->forced_stop = cpi->sf.mv_sf.subpel_force_stop;
ms_params->iters_per_step = cpi->sf.mv_sf.subpel_iters_per_step;
ms_params->cost_list = cond_cost_list_const(cpi, cost_list);
if (is_tip_ref_frame(mbmi->ref_frame[0])) {
av1_set_tip_subpel_mv_search_range(&ms_params->mv_limits, &x->mv_limits);
} else {
av1_set_subpel_mv_search_range(
&ms_params->mv_limits, &x->mv_limits, ref_mv, pb_mv_precision
);
}
// Mvcost params
init_mv_cost_params(&ms_params->mv_cost_params, &x->mv_costs, is_adaptive_mvd,
ref_mv, pb_mv_precision
#if CONFIG_IBC_BV_IMPROVEMENT
,
is_ibc_cost
#endif
);
// Subpel variance params
ms_params->var_params.vfp = &cpi->fn_ptr[bsize];
ms_params->var_params.subpel_search_type = cpi->sf.mv_sf.subpel_search_type;
#if CONFIG_BLOCK_256
if (cpi->sf.mv_sf.fast_motion_estimation_on_block_256 &&
AOMMAX(block_size_wide[bsize], block_size_high[bsize]) >= 256) {
ms_params->var_params.subpel_search_type =
AOMMIN(ms_params->var_params.subpel_search_type, USE_2_TAPS);
}
#endif // CONFIG_BLOCK_256
ms_params->var_params.w = block_size_wide[bsize];
ms_params->var_params.h = block_size_high[bsize];
// Ref and src buffers
MSBuffers *ms_buffers = &ms_params->var_params.ms_buffers;
init_ms_buffers(ms_buffers, x);
assert(ms_params->var_params.subpel_search_type &&
"Subpel type 2_TAPS_ORIG is no longer supported!");
}
static INLINE int get_offset_from_fullmv(const FULLPEL_MV *mv, int stride) {
return mv->row * stride + mv->col;
}
static INLINE const uint16_t *get_buf_from_fullmv(const struct buf_2d *buf,
const FULLPEL_MV *mv) {
return &buf->buf[get_offset_from_fullmv(mv, buf->stride)];
}
void av1_set_mv_search_range(FullMvLimits *mv_limits, const MV *mv,
MvSubpelPrecision pb_mv_precision
) {
// We have to make sure the generated mv_limits
// are compatible with target precision.
// prec_shift is the number of LSBs need to be 0 to make the mv/mv_limit
// compatible
const int prec_shift = (pb_mv_precision < MV_PRECISION_ONE_PEL)
? (MV_PRECISION_ONE_PEL - pb_mv_precision)
: 0;
const int max_full_mv = av1_lower_mv_limit(MAX_FULL_PEL_VAL, prec_shift);
// Producing the reference mv value to the target precision
FULLPEL_MV full_ref_mv = get_fullmv_from_mv(mv);
MV low_prec_mv = { GET_MV_SUBPEL(full_ref_mv.row),
GET_MV_SUBPEL(full_ref_mv.col) };
lower_mv_precision(&low_prec_mv, pb_mv_precision);
// Calculate the outermost full-pixel MVs which are inside the limits set by
// av1_set_subpel_mv_search_range().
//
// The subpel limits are simply mv->col +/- 8*MAX_FULL_PEL_VAL, and similar
// for mv->row. We can then divide by 8 to find the fullpel MV limits. But
// we have to be careful about the rounding. We want these bounds to be
// at least as tight as the subpel limits, which means that we must round
// the minimum values up and the maximum values down when dividing.
int col_min = ((low_prec_mv.col + 7) >> 3) - max_full_mv;
int row_min = ((low_prec_mv.row + 7) >> 3) - max_full_mv;
int col_max = (low_prec_mv.col >> 3) + max_full_mv;
int row_max = (low_prec_mv.row >> 3) + max_full_mv;
col_min = AOMMAX(col_min, (MV_LOW >> 3) + (1 << prec_shift));
row_min = AOMMAX(row_min, (MV_LOW >> 3) + (1 << prec_shift));
col_max = AOMMIN(col_max, (MV_UPP >> 3) - (1 << prec_shift));
row_max = AOMMIN(row_max, (MV_UPP >> 3) - (1 << prec_shift));
full_pel_lower_mv_precision_one_comp(&mv_limits->col_min, pb_mv_precision, 0);
full_pel_lower_mv_precision_one_comp(&mv_limits->row_min, pb_mv_precision, 0);
full_pel_lower_mv_precision_one_comp(&mv_limits->col_max, pb_mv_precision, 1);
full_pel_lower_mv_precision_one_comp(&mv_limits->row_max, pb_mv_precision, 1);
// Get intersection of UMV window and valid MV window to reduce # of checks
// in diamond search.
if (mv_limits->col_min < col_min) mv_limits->col_min = col_min;
if (mv_limits->col_max > col_max) mv_limits->col_max = col_max;
if (mv_limits->row_min < row_min) mv_limits->row_min = row_min;
if (mv_limits->row_max > row_max) mv_limits->row_max = row_max;
mv_limits->col_max = AOMMAX(mv_limits->col_min, mv_limits->col_max);
mv_limits->row_max = AOMMAX(mv_limits->row_min, mv_limits->row_max);
}
#if CONFIG_OPFL_MV_SEARCH
// Obtain number of iterations for optical flow based MV search.
int get_opfl_mv_iterations(const AV1_COMP *cpi, const MB_MODE_INFO *mbmi) {
// Allowed only for screen content
const AV1_COMMON *cm = &cpi->common;
if (!cm->features.allow_screen_content_tools) return 0;
if (mbmi->ref_frame[0] == NONE_FRAME) return 0;
// Optical flow MV search is allowed for NEWMV and WARPMV only, since it
// shows little improvements in compound modes.
if (mbmi->mode == NEWMV
#if CONFIG_EXTENDED_WARP_PREDICTION
|| mbmi->mode == WARPMV
#endif // CONFIG_EXTENDED_WARP_PREDICTION
)
return 3;
return 0;
}
// Derive a MVD based on optical flow method. In the two sided optical flow
// refinement implemented in av1_get_optflow_based_mv_highbd, two predicted
// blocks (P0, P1) are used to solve a MV delta, which is scaled based on d0
// and d1 to derive MVs of src relative to P0 and P1. Alternatively, this
// routine is a one sided optical flow solver, which uses the source block (src)
// and one predicted block (P0) to derives an MV delta, which is by itself
// relative to P0.
int opfl_refine_fullpel_mv_one_sided(
const AV1_COMMON *cm, MACROBLOCKD *xd,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, MB_MODE_INFO *mbmi,
const FULLPEL_MV *const smv, int_mv *mv_refined, BLOCK_SIZE bsize) {
(void)cm;
(void)xd;
(void)mbmi;
int bw = block_size_wide[bsize];
int bh = block_size_high[bsize];
const struct buf_2d *const pred = ms_params->ms_buffers.ref;
const struct buf_2d *const src = ms_params->ms_buffers.src;
uint16_t *pred_ptr = &pred->buf[smv->row * pred->stride + smv->col];
#if OMVS_EARLY_TERM
// Early termination based on SAD
// int sad = ms_params->vfp->sdf(dst0, bw, dst1, bw);
int sad = ms_params->vfp->sdf(src->buf, src->stride, pred_ptr, pred->stride);
if (sad < bw * bh * OMVS_SAD_THR) return 1;
#endif
int vx0, vx1, vy0, vy1;
int16_t *gx0, *gy0;
uint16_t *dst0 = NULL, *dst1 = NULL;
gx0 = (int16_t *)aom_memalign(16, bw * bh * sizeof(int16_t));
gy0 = (int16_t *)aom_memalign(16, bw * bh * sizeof(int16_t));
dst0 = (uint16_t *)aom_memalign(16, bw * bh * sizeof(uint16_t));
dst1 = (uint16_t *)aom_memalign(16, bw * bh * sizeof(uint16_t));
// Obrain Pred as dst0 and Cur as dst1
aom_highbd_convolve_copy(pred_ptr, pred->stride, dst0, bw, bw, bh);
aom_highbd_convolve_copy(src->buf, src->stride, dst1, bw, bw, bh);
int grad_prec_bits;
int16_t *tmp0 =
(int16_t *)aom_memalign(16, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(int16_t));
int16_t *tmp1 =
(int16_t *)aom_memalign(16, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(int16_t));
// tmp0 = (P0 + Cur) / 2, tmp1 = P0 - Cur
if (bw < 8)
av1_copy_pred_array_highbd_c(dst0, dst1, tmp0, tmp1, bw, bh, 1, -1, 1);
else
av1_copy_pred_array_highbd(dst0, dst1, tmp0, tmp1, bw, bh, 1, -1, 1);
// Buffers gx0 and gy0 are used to store the gradients of tmp0
av1_compute_subpel_gradients_interp(tmp0, bw, bh, &grad_prec_bits, gx0, gy0);
int bits = 3 + get_opfl_mv_upshift_bits(mbmi);
#if OMVS_AVG_POOLING
int n = AOMMIN(8, AOMMIN(bw, bh));
avg_pooling_pdiff_gradients(tmp1, bw, gx0, gy0, bw, bw, bh, n);
// The SIMD version performs refinement for every 4x8 or 8x8 region. It is
// only applicable when n == 8 in optical flow based MV search
if (n == 8)
av1_opfl_mv_refinement_nxn_interp_grad(tmp1, bw, gx0, gy0, bw, n, n, n, 1,
0, grad_prec_bits, bits, &vx0, &vy0,
&vx1, &vy1);
else
av1_opfl_mv_refinement_interp_grad(tmp1, bw, gx0, gy0, bw, n, n, 1, 0,
grad_prec_bits, bits, &vx0, &vy0, &vx1,
&vy1);
#else
av1_opfl_mv_refinement_interp_grad(tmp1, bw, gx0, gy0, bw, bw, bh, 1, 0,
grad_prec_bits, bits, &vx0, &vy0, &vx1,
&vy1);
#endif
aom_free(tmp0);
aom_free(tmp1);
aom_free(dst0);
aom_free(dst1);
aom_free(gx0);
aom_free(gy0);
mv_refined[0].as_mv.row += vy0;
mv_refined[0].as_mv.col += vx0;
return 0;
}
#endif // CONFIG_OPFL_MV_SEARCH
void av1_set_tip_mv_search_range(FullMvLimits *mv_limits) {
const int tmvp_mv = (TIP_MV_SEARCH_RANGE << TMVP_MI_SZ_LOG2);
const int col_min = -tmvp_mv;
const int row_min = -tmvp_mv;
const int col_max = tmvp_mv;
const int row_max = tmvp_mv;
// Get intersection of UMV window and valid MV window to reduce # of checks
// in diamond search.
if (mv_limits->col_min < col_min) mv_limits->col_min = col_min;
if (mv_limits->col_max > col_max) mv_limits->col_max = col_max;
if (mv_limits->row_min < row_min) mv_limits->row_min = row_min;
if (mv_limits->row_max > row_max) mv_limits->row_max = row_max;
}
int av1_init_search_range(int size) {
int sr = 0;
// Minimum search size no matter what the passed in value.
size = AOMMAX(16, size);
while ((size << sr) < MAX_FULL_PEL_VAL) sr++;
sr = AOMMIN(sr, MAX_MVSEARCH_STEPS - 2);
return sr;
}
// ============================================================================
// Cost of motion vectors
// ============================================================================
// TODO(any): Adaptively adjust the regularization strength based on image size
// and motion activity instead of using hard-coded values. It seems like we
// roughly half the lambda for each increase in resolution
// These are multiplier used to perform regularization in motion compensation
// when x->mv_cost_type is set to MV_COST_L1.
// LOWRES
#define SSE_LAMBDA_LOWRES 2 // Used by mv_cost_err_fn
#define SAD_LAMBDA_LOWRES 32 // Used by mvsad_err_cost during full pixel search
// MIDRES
#define SSE_LAMBDA_MIDRES 0 // Used by mv_cost_err_fn
#define SAD_LAMBDA_MIDRES 15 // Used by mvsad_err_cost during full pixel search
// HDRES
#define SSE_LAMBDA_HDRES 1 // Used by mv_cost_err_fn
#define SAD_LAMBDA_HDRES 8 // Used by mvsad_err_cost during full pixel search
// Returns the rate of encoding the current motion vector based on the
// joint_cost and comp_cost. joint_costs covers the cost of transmitting
// JOINT_MV, and comp_cost covers the cost of transmitting the actual motion
// vector.
static INLINE int mv_cost(const MV *mv, const int *joint_cost,
const int *const comp_cost[2]) {
return joint_cost[av1_get_mv_joint(mv)] + comp_cost[0][mv->row] +
comp_cost[1][mv->col];
}
#define CONVERT_TO_CONST_MVCOST(ptr) ((const int *const *)(ptr))
static INLINE int get_mv_cost_with_precision(
const MV mv, const MV ref_mv, const MvSubpelPrecision pb_mv_precision,
const int is_adaptive_mvd,
#if CONFIG_IBC_BV_IMPROVEMENT
const int is_ibc_cost,
#endif
const MvCosts *mv_costs, int weight, int round_bits) {
const int *mvjcost =
is_adaptive_mvd
? mv_costs->amvd_nmv_joint_cost
#if CONFIG_IBC_BV_IMPROVEMENT
: (is_ibc_cost ? mv_costs->dv_joint_cost : mv_costs->nmv_joint_cost);
#else
: mv_costs->nmv_joint_cost;
#endif
const int *const *mvcost =
is_adaptive_mvd
? CONVERT_TO_CONST_MVCOST(mv_costs->amvd_nmv_cost)
#if CONFIG_IBC_BV_IMPROVEMENT
: (is_ibc_cost ? CONVERT_TO_CONST_MVCOST(mv_costs->dv_nmv_cost)
: CONVERT_TO_CONST_MVCOST(
mv_costs->nmv_costs[pb_mv_precision]));
#else
: CONVERT_TO_CONST_MVCOST(mv_costs->nmv_costs[pb_mv_precision]);
#endif
MV low_prec_ref_mv = ref_mv;
#if BUGFIX_AMVD_AMVR
if (!is_adaptive_mvd)
#endif
#if CONFIG_C071_SUBBLK_WARPMV
if (pb_mv_precision < MV_PRECISION_HALF_PEL)
#endif // CONFIG_C071_SUBBLK_WARPMV
lower_mv_precision(&low_prec_ref_mv, pb_mv_precision);
const MV diff = { mv.row - low_prec_ref_mv.row,
mv.col - low_prec_ref_mv.col };
#if CONFIG_C071_SUBBLK_WARPMV
assert(is_this_mv_precision_compliant(diff, pb_mv_precision));
#endif // CONFIG_C071_SUBBLK_WARPMV
if (mvcost) {
return (int)ROUND_POWER_OF_TWO_64(
(int64_t)mv_cost(&diff, mvjcost, mvcost) * weight, round_bits);
}
return 0;
}
static INLINE int get_intrabc_mv_cost_with_precision(
const MV diff, const IntraBCMvCosts *dv_costs, int weight, int round_bits) {
const int *dvjcost = dv_costs->joint_mv;
const int *const *dvcost = CONVERT_TO_CONST_MVCOST(dv_costs->dv_costs);
if (dv_costs) {
return (int)ROUND_POWER_OF_TWO_64(
(int64_t)mv_cost(&diff, dvjcost, dvcost) * weight, round_bits);
}
return 0;
}
// Returns the cost of encoding the motion vector diff := *mv - *ref. The cost
// is defined as the rate required to encode diff * weight, rounded to the
// nearest 2 ** 7.
// This is NOT used during motion compensation.
int av1_mv_bit_cost(const MV *mv, const MV *ref_mv,
const MvSubpelPrecision pb_mv_precision,
const MvCosts *mv_costs, int weight,
const int is_adaptive_mvd) {
#if CONFIG_IBC_BV_IMPROVEMENT
// For ibc block this function should not be called
const int is_ibc_cost = 0;
#endif
return get_mv_cost_with_precision(*mv, *ref_mv, pb_mv_precision,
is_adaptive_mvd,
#if CONFIG_IBC_BV_IMPROVEMENT
is_ibc_cost,
#endif
mv_costs, weight, 7);
}
int av1_intrabc_mv_bit_cost(const MV *mv, const MV *ref_mv,
const IntraBCMvCosts *mv_costs, int weight) {
const MV diff = { mv->row - ref_mv->row, mv->col - ref_mv->col };
return get_intrabc_mv_cost_with_precision(diff, mv_costs, weight, 7);
}
// Returns the cost of using the current mv during the motion search. This is
// used when var is used as the error metric.
#define PIXEL_TRANSFORM_ERROR_SCALE 4
static INLINE int mv_err_cost(const MV mv,
const MV_COST_PARAMS *mv_cost_params) {
const MV ref_mv = *mv_cost_params->ref_mv;
const MvSubpelPrecision pb_mv_precision = mv_cost_params->pb_mv_precision;
const MV_COST_TYPE mv_cost_type = mv_cost_params->mv_cost_type;
const MvCosts *mv_costs = mv_cost_params->mv_costs;
MV low_prec_ref_mv = ref_mv;
#if BUGFIX_AMVD_AMVR
if (!mv_cost_params->is_adaptive_mvd)
#endif
#if CONFIG_C071_SUBBLK_WARPMV
if (pb_mv_precision < MV_PRECISION_HALF_PEL)
#endif // CONFIG_C071_SUBBLK_WARPMV
lower_mv_precision(&low_prec_ref_mv, pb_mv_precision);
const MV diff = { mv.row - low_prec_ref_mv.row,
mv.col - low_prec_ref_mv.col };
#if CONFIG_C071_SUBBLK_WARPMV
assert(is_this_mv_precision_compliant(diff, pb_mv_precision));
#endif // CONFIG_C071_SUBBLK_WARPMV
const MV abs_diff = { abs(diff.row), abs(diff.col) };
switch (mv_cost_type) {
case MV_COST_ENTROPY:
return get_mv_cost_with_precision(
mv, ref_mv, mv_cost_params->pb_mv_precision,
mv_cost_params->is_adaptive_mvd,
#if CONFIG_IBC_BV_IMPROVEMENT
mv_cost_params->is_ibc_cost,
#endif
mv_costs, mv_costs->errorperbit,
RDDIV_BITS + AV1_PROB_COST_SHIFT - RD_EPB_SHIFT +
PIXEL_TRANSFORM_ERROR_SCALE);
case MV_COST_L1_LOWRES:
return (SSE_LAMBDA_LOWRES * (abs_diff.row + abs_diff.col)) >> 3;
case MV_COST_L1_MIDRES:
return (SSE_LAMBDA_MIDRES * (abs_diff.row + abs_diff.col)) >> 3;
case MV_COST_L1_HDRES:
return (SSE_LAMBDA_HDRES * (abs_diff.row + abs_diff.col)) >> 3;
case MV_COST_NONE: return 0;
default: assert(0 && "Invalid rd_cost_type"); return 0;
}
}
// Returns the cost of using the current mv during the motion search. This is
// only used during full pixel motion search when sad is used as the error
// metric
static INLINE int mvsad_err_cost(const FULLPEL_MV mv,
const MV_COST_PARAMS *mv_cost_params) {
MV this_mv = { GET_MV_SUBPEL(mv.row), GET_MV_SUBPEL(mv.col) };
const MvSubpelPrecision pb_mv_precision = mv_cost_params->pb_mv_precision;
MV ref_mv = { GET_MV_SUBPEL(mv_cost_params->full_ref_mv.row),
GET_MV_SUBPEL(mv_cost_params->full_ref_mv.col) };
#if BUGFIX_AMVD_AMVR
if (!mv_cost_params->is_adaptive_mvd)
#endif
lower_mv_precision(&ref_mv, pb_mv_precision);
const MV diff = { (this_mv.row - ref_mv.row), (this_mv.col - ref_mv.col) };
const MV abs_diff = { abs(diff.row), abs(diff.col) };
const MvCosts *mv_costs = mv_cost_params->mv_costs;
#if CONFIG_IBC_BV_IMPROVEMENT
const int *mvjcost =
mv_cost_params->is_ibc_cost
? mv_costs->dv_joint_cost
: (mv_cost_params->is_adaptive_mvd ? mv_costs->amvd_nmv_joint_cost
: mv_costs->nmv_joint_cost);
const int *const *mvcost =
mv_cost_params->is_ibc_cost
? CONVERT_TO_CONST_MVCOST(mv_costs->dv_nmv_cost)
: (mv_cost_params->is_adaptive_mvd
? CONVERT_TO_CONST_MVCOST(mv_costs->amvd_nmv_cost)
: CONVERT_TO_CONST_MVCOST(
mv_costs->nmv_costs[pb_mv_precision]));
#else
const int *mvjcost = mv_cost_params->is_adaptive_mvd
? mv_costs->amvd_nmv_joint_cost
: mv_costs->nmv_joint_cost;
const int *const *mvcost =
mv_cost_params->is_adaptive_mvd
? CONVERT_TO_CONST_MVCOST(mv_costs->amvd_nmv_cost)
: CONVERT_TO_CONST_MVCOST(mv_costs->nmv_costs[pb_mv_precision]);
#endif
const int sad_per_bit = mv_costs->sadperbit;
const MV_COST_TYPE mv_cost_type = mv_cost_params->mv_cost_type;
switch (mv_cost_type) {
case MV_COST_ENTROPY:
return ROUND_POWER_OF_TWO((unsigned)mv_cost(&diff, mvjcost, mvcost
) *
sad_per_bit,
AV1_PROB_COST_SHIFT);
case MV_COST_L1_LOWRES:
return (SAD_LAMBDA_LOWRES * (abs_diff.row + abs_diff.col)) >> 3;
case MV_COST_L1_MIDRES:
return (SAD_LAMBDA_MIDRES * (abs_diff.row + abs_diff.col)) >> 3;
case MV_COST_L1_HDRES:
return (SAD_LAMBDA_HDRES * (abs_diff.row + abs_diff.col)) >> 3;
case MV_COST_NONE: return 0;
default: assert(0 && "Invalid rd_cost_type"); return 0;
}
}
// =============================================================================
// Fullpixel Motion Search: Translational
// =============================================================================
#define MAX_PATTERN_SCALES 11
#define MAX_PATTERN_CANDIDATES 8 // max number of candidates per scale
#define PATTERN_CANDIDATES_REF 3 // number of refinement candidates
void av1_init_dsmotion_compensation(search_site_config *cfg, int stride) {
int num_search_steps = 0;
int stage_index = MAX_MVSEARCH_STEPS - 1;
cfg->site[stage_index][0].mv.col = cfg->site[stage_index][0].mv.row = 0;
cfg->site[stage_index][0].offset = 0;
cfg->stride = stride;
for (int radius = MAX_FIRST_STEP; radius > 0; radius /= 2) {
int num_search_pts = 8;
const FULLPEL_MV search_site_mvs[13] = {
{ 0, 0 }, { -radius, 0 }, { radius, 0 },
{ 0, -radius }, { 0, radius }, { -radius, -radius },
{ radius, radius }, { -radius, radius }, { radius, -radius },
};
int i;
for (i = 0; i <= num_search_pts; ++i) {
search_site *const site = &cfg->site[stage_index][i];
site->mv = search_site_mvs[i];
site->offset = get_offset_from_fullmv(&site->mv, stride);
}
cfg->searches_per_step[stage_index] = num_search_pts;
cfg->radius[stage_index] = radius;
--stage_index;
++num_search_steps;
}
cfg->num_search_steps = num_search_steps;
}
void av1_init_motion_fpf(search_site_config *cfg, int stride) {
int num_search_steps = 0;
int stage_index = MAX_MVSEARCH_STEPS - 1;
cfg->site[stage_index][0].mv.col = cfg->site[stage_index][0].mv.row = 0;
cfg->site[stage_index][0].offset = 0;
cfg->stride = stride;
for (int radius = MAX_FIRST_STEP; radius > 0; radius /= 2) {
// Generate offsets for 8 search sites per step.
int tan_radius = AOMMAX((int)(0.41 * radius), 1);
int num_search_pts = 12;
if (radius == 1) num_search_pts = 8;
const FULLPEL_MV search_site_mvs[13] = {
{ 0, 0 },
{ -radius, 0 },
{ radius, 0 },
{ 0, -radius },
{ 0, radius },
{ -radius, -tan_radius },
{ radius, tan_radius },
{ -tan_radius, radius },
{ tan_radius, -radius },
{ -radius, tan_radius },
{ radius, -tan_radius },
{ tan_radius, radius },
{ -tan_radius, -radius },
};
int i;
for (i = 0; i <= num_search_pts; ++i) {
search_site *const site = &cfg->site[stage_index][i];
site->mv = search_site_mvs[i];
site->offset = get_offset_from_fullmv(&site->mv, stride);
}
cfg->searches_per_step[stage_index] = num_search_pts;
cfg->radius[stage_index] = radius;
--stage_index;
++num_search_steps;
}
cfg->num_search_steps = num_search_steps;
}
// Search site initialization for NSTEP search method.
void av1_init_motion_compensation_nstep(search_site_config *cfg, int stride) {
int num_search_steps = 0;
int stage_index = 0;
cfg->stride = stride;
int radius = 1;
#if CONFIG_MV_SEARCH_RANGE
for (stage_index = 0; stage_index < 16; ++stage_index) {
#else
for (stage_index = 0; stage_index < 15; ++stage_index) {
#endif // CONFIG_MV_SEARCH_RANGE
int tan_radius = AOMMAX((int)(0.41 * radius), 1);
int num_search_pts = 12;
if (radius <= 5) {
tan_radius = radius;
num_search_pts = 8;
}
const FULLPEL_MV search_site_mvs[13] = {
{ 0, 0 },
{ -radius, 0 },
{ radius, 0 },
{ 0, -radius },
{ 0, radius },
{ -radius, -tan_radius },
{ radius, tan_radius },
{ -tan_radius, radius },
{ tan_radius, -radius },
{ -radius, tan_radius },
{ radius, -tan_radius },
{ tan_radius, radius },
{ -tan_radius, -radius },
};
for (int i = 0; i <= num_search_pts; ++i) {
search_site *const site = &cfg->site[stage_index][i];
site->mv = search_site_mvs[i];
site->offset = get_offset_from_fullmv(&site->mv, stride);
}
cfg->searches_per_step[stage_index] = num_search_pts;
cfg->radius[stage_index] = radius;
++num_search_steps;
#if !CONFIG_MV_SEARCH_RANGE
if (stage_index < 12)
#endif // CONFIG_MV_SEARCH_RANGE
radius = (int)AOMMAX((radius * 1.5 + 0.5), radius + 1);
}
cfg->num_search_steps = num_search_steps;
}
// Search site initialization for BIGDIA / FAST_BIGDIA / FAST_DIAMOND
// search methods.
void av1_init_motion_compensation_bigdia(search_site_config *cfg, int stride) {
cfg->stride = stride;
// First scale has 4-closest points, the rest have 8 points in diamond
// shape at increasing scales
static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
};
// BIGDIA search method candidates.
// Note that the largest candidate step at each scale is 2^scale
/* clang-format off */
static const FULLPEL_MV
site_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
{ { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 0 }, { 0, 0 },
{ 0, 0 }, { 0, 0 } },
{ { -1, -1 }, { 0, -2 }, { 1, -1 }, { 2, 0 }, { 1, 1 }, { 0, 2 },
{ -1, 1 }, { -2, 0 } },
{ { -2, -2 }, { 0, -4 }, { 2, -2 }, { 4, 0 }, { 2, 2 }, { 0, 4 },
{ -2, 2 }, { -4, 0 } },
{ { -4, -4 }, { 0, -8 }, { 4, -4 }, { 8, 0 }, { 4, 4 }, { 0, 8 },
{ -4, 4 }, { -8, 0 } },
{ { -8, -8 }, { 0, -16 }, { 8, -8 }, { 16, 0 }, { 8, 8 }, { 0, 16 },
{ -8, 8 }, { -16, 0 } },
{ { -16, -16 }, { 0, -32 }, { 16, -16 }, { 32, 0 }, { 16, 16 },
{ 0, 32 }, { -16, 16 }, { -32, 0 } },
{ { -32, -32 }, { 0, -64 }, { 32, -32 }, { 64, 0 }, { 32, 32 },
{ 0, 64 }, { -32, 32 }, { -64, 0 } },
{ { -64, -64 }, { 0, -128 }, { 64, -64 }, { 128, 0 }, { 64, 64 },
{ 0, 128 }, { -64, 64 }, { -128, 0 } },
{ { -128, -128 }, { 0, -256 }, { 128, -128 }, { 256, 0 },
{ 128, 128 }, { 0, 256 }, { -128, 128 }, { -256, 0 } },
{ { -256, -256 }, { 0, -512 }, { 256, -256 }, { 512, 0 },
{ 256, 256 }, { 0, 512 }, { -256, 256 }, { -512, 0 } },
{ { -512, -512 }, { 0, -1024 }, { 512, -512 }, { 1024, 0 },
{ 512, 512 }, { 0, 1024 }, { -512, 512 }, { -1024, 0 } },
};
/* clang-format on */
int radius = 1;
for (int i = 0; i < MAX_PATTERN_SCALES; ++i) {
cfg->searches_per_step[i] = bigdia_num_candidates[i];
cfg->radius[i] = radius;
for (int j = 0; j < MAX_PATTERN_CANDIDATES; ++j) {
search_site *const site = &cfg->site[i][j];
site->mv = site_candidates[i][j];
site->offset = get_offset_from_fullmv(&site->mv, stride);
}
radius *= 2;
}
cfg->num_search_steps = MAX_PATTERN_SCALES;
}
// Search site initialization for SQUARE search method.
void av1_init_motion_compensation_square(search_site_config *cfg, int stride) {
cfg->stride = stride;
// All scales have 8 closest points in square shape.
static const int square_num_candidates[MAX_PATTERN_SCALES] = {
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
};
// Square search method candidates.
// Note that the largest candidate step at each scale is 2^scale.
/* clang-format off */
static const FULLPEL_MV
square_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
{ { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
{ -1, 1 }, { -1, 0 } },
{ { -2, -2 }, { 0, -2 }, { 2, -2 }, { 2, 0 }, { 2, 2 }, { 0, 2 },
{ -2, 2 }, { -2, 0 } },
{ { -4, -4 }, { 0, -4 }, { 4, -4 }, { 4, 0 }, { 4, 4 }, { 0, 4 },
{ -4, 4 }, { -4, 0 } },
{ { -8, -8 }, { 0, -8 }, { 8, -8 }, { 8, 0 }, { 8, 8 }, { 0, 8 },
{ -8, 8 }, { -8, 0 } },
{ { -16, -16 }, { 0, -16 }, { 16, -16 }, { 16, 0 }, { 16, 16 },
{ 0, 16 }, { -16, 16 }, { -16, 0 } },
{ { -32, -32 }, { 0, -32 }, { 32, -32 }, { 32, 0 }, { 32, 32 },
{ 0, 32 }, { -32, 32 }, { -32, 0 } },
{ { -64, -64 }, { 0, -64 }, { 64, -64 }, { 64, 0 }, { 64, 64 },
{ 0, 64 }, { -64, 64 }, { -64, 0 } },
{ { -128, -128 }, { 0, -128 }, { 128, -128 }, { 128, 0 },
{ 128, 128 }, { 0, 128 }, { -128, 128 }, { -128, 0 } },
{ { -256, -256 }, { 0, -256 }, { 256, -256 }, { 256, 0 },
{ 256, 256 }, { 0, 256 }, { -256, 256 }, { -256, 0 } },
{ { -512, -512 }, { 0, -512 }, { 512, -512 }, { 512, 0 },
{ 512, 512 }, { 0, 512 }, { -512, 512 }, { -512, 0 } },
{ { -1024, -1024 }, { 0, -1024 }, { 1024, -1024 }, { 1024, 0 },
{ 1024, 1024 }, { 0, 1024 }, { -1024, 1024 }, { -1024, 0 } },
};
/* clang-format on */
int radius = 1;
for (int i = 0; i < MAX_PATTERN_SCALES; ++i) {
cfg->searches_per_step[i] = square_num_candidates[i];
cfg->radius[i] = radius;
for (int j = 0; j < MAX_PATTERN_CANDIDATES; ++j) {
search_site *const site = &cfg->site[i][j];
site->mv = square_candidates[i][j];
site->offset = get_offset_from_fullmv(&site->mv, stride);
}
radius *= 2;
}
cfg->num_search_steps = MAX_PATTERN_SCALES;
}
// Search site initialization for HEX / FAST_HEX search methods.
void av1_init_motion_compensation_hex(search_site_config *cfg, int stride) {
cfg->stride = stride;
// First scale has 8-closest points, the rest have 6 points in hex shape
// at increasing scales.
static const int hex_num_candidates[MAX_PATTERN_SCALES] = { 8, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6 };
// Note that the largest candidate step at each scale is 2^scale.
/* clang-format off */
static const FULLPEL_MV
hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
{ { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
{ -1, 1 }, { -1, 0 } },
{ { -1, -2 }, { 1, -2 }, { 2, 0 }, { 1, 2 }, { -1, 2 }, { -2, 0 } },
{ { -2, -4 }, { 2, -4 }, { 4, 0 }, { 2, 4 }, { -2, 4 }, { -4, 0 } },
{ { -4, -8 }, { 4, -8 }, { 8, 0 }, { 4, 8 }, { -4, 8 }, { -8, 0 } },
{ { -8, -16 }, { 8, -16 }, { 16, 0 }, { 8, 16 },
{ -8, 16 }, { -16, 0 } },
{ { -16, -32 }, { 16, -32 }, { 32, 0 }, { 16, 32 }, { -16, 32 },
{ -32, 0 } },
{ { -32, -64 }, { 32, -64 }, { 64, 0 }, { 32, 64 }, { -32, 64 },
{ -64, 0 } },
{ { -64, -128 }, { 64, -128 }, { 128, 0 }, { 64, 128 },
{ -64, 128 }, { -128, 0 } },
{ { -128, -256 }, { 128, -256 }, { 256, 0 }, { 128, 256 },
{ -128, 256 }, { -256, 0 } },
{ { -256, -512 }, { 256, -512 }, { 512, 0 }, { 256, 512 },
{ -256, 512 }, { -512, 0 } },
{ { -512, -1024 }, { 512, -1024 }, { 1024, 0 }, { 512, 1024 },
{ -512, 1024 }, { -1024, 0 } },
};
/* clang-format on */
int radius = 1;
for (int i = 0; i < MAX_PATTERN_SCALES; ++i) {
cfg->searches_per_step[i] = hex_num_candidates[i];
cfg->radius[i] = radius;
for (int j = 0; j < hex_num_candidates[i]; ++j) {
search_site *const site = &cfg->site[i][j];
site->mv = hex_candidates[i][j];
site->offset = get_offset_from_fullmv(&site->mv, stride);
}
radius *= 2;
}
cfg->num_search_steps = MAX_PATTERN_SCALES;
}
// Checks whether the mv is within range of the mv_limits
static INLINE int check_bounds(const FullMvLimits *mv_limits, int row, int col,
int range) {
return ((row - range) >= mv_limits->row_min) &
((row + range) <= mv_limits->row_max) &
((col - range) >= mv_limits->col_min) &
((col + range) <= mv_limits->col_max);
}
#if CONFIG_IBC_BV_IMPROVEMENT
int av1_get_mv_err_cost(const MV *mv, const MV_COST_PARAMS *mv_cost_params) {
return mv_err_cost(*mv, mv_cost_params);
}
#endif // CONFIG_IBC_BV_IMPROVEMENT
static INLINE int get_mvpred_var_cost(
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) {
const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
#if !CONFIG_C071_SUBBLK_WARPMV
const
#endif // !CONFIG_C071_SUBBLK_WARPMV
MV sub_this_mv = get_mv_from_fullmv(this_mv);
const struct buf_2d *const src = ms_params->ms_buffers.src;
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const uint16_t *src_buf = src->buf;
const int src_stride = src->stride;
const int ref_stride = ref->stride;
unsigned unused;
int bestsme;
bestsme = vfp->vf(src_buf, src_stride, get_buf_from_fullmv(ref, this_mv),
ref_stride, &unused);
#if CONFIG_C071_SUBBLK_WARPMV
MV sub_mv_offset = { 0, 0 };
get_phase_from_mv(*ms_params->mv_cost_params.ref_mv, &sub_mv_offset,
ms_params->mv_cost_params.pb_mv_precision);
if (ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_HALF_PEL) {
sub_this_mv.col += sub_mv_offset.col;
sub_this_mv.row += sub_mv_offset.row;
}
#endif // CONFIG_C071_SUBBLK_WARPMV
bestsme += mv_err_cost(sub_this_mv, &ms_params->mv_cost_params);
return bestsme;
}
static INLINE int get_mvpred_sad(const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const struct buf_2d *const src,
const uint16_t *const ref_address,
const int ref_stride) {
const uint16_t *src_buf = src->buf;
const int src_stride = src->stride;
return ms_params->sdf(src_buf, src_stride, ref_address, ref_stride);
}
static INLINE int get_mvpred_compound_var_cost(
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) {
const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
const struct buf_2d *const src = ms_params->ms_buffers.src;
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const uint16_t *src_buf = src->buf;
const int src_stride = src->stride;
const int ref_stride = ref->stride;
const uint8_t *mask = ms_params->ms_buffers.mask;
const uint16_t *second_pred = ms_params->ms_buffers.second_pred;
const int mask_stride = ms_params->ms_buffers.mask_stride;
const int invert_mask = ms_params->ms_buffers.inv_mask;
unsigned unused;
int bestsme;
if (mask) {
bestsme = vfp->msvf(get_buf_from_fullmv(ref, this_mv), ref_stride, 0, 0,
src_buf, src_stride, second_pred, mask, mask_stride,
invert_mask, &unused);
} else if (second_pred) {
bestsme = vfp->svaf(get_buf_from_fullmv(ref, this_mv), ref_stride, 0, 0,
src_buf, src_stride, &unused, second_pred);
} else {
bestsme = vfp->vf(src_buf, src_stride, get_buf_from_fullmv(ref, this_mv),
ref_stride, &unused);
}
#if !CONFIG_C071_SUBBLK_WARPMV
const
#endif // !CONFIG_C071_SUBBLK_WARPMV
MV sub_this_mv = get_mv_from_fullmv(this_mv);
#if CONFIG_C071_SUBBLK_WARPMV
MV sub_mv_offset = { 0, 0 };
get_phase_from_mv(*ms_params->mv_cost_params.ref_mv, &sub_mv_offset,
ms_params->mv_cost_params.pb_mv_precision);
if (ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_HALF_PEL) {
sub_this_mv.col += sub_mv_offset.col;
sub_this_mv.row += sub_mv_offset.row;
}
#endif // CONFIG_C071_SUBBLK_WARPMV
bestsme += mv_err_cost(sub_this_mv, &ms_params->mv_cost_params);
return bestsme;
}
// Set weighting factor for two reference frames
static INLINE void set_cmp_weight(const MB_MODE_INFO *mi, int invert_mask,
DIST_WTD_COMP_PARAMS *jcp_param) {
int weight = get_cwp_idx(mi);
weight = invert_mask ? (1 << CWP_WEIGHT_BITS) - weight : weight;
jcp_param->fwd_offset = weight;
jcp_param->bck_offset = (1 << CWP_WEIGHT_BITS) - weight;
}
static INLINE int get_mvpred_compound_sad(
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const struct buf_2d *const src, const uint16_t *const ref_address,
const int ref_stride) {
const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
const uint16_t *src_buf = src->buf;
const int src_stride = src->stride;
const uint8_t *mask = ms_params->ms_buffers.mask;
const uint16_t *second_pred = ms_params->ms_buffers.second_pred;
const int mask_stride = ms_params->ms_buffers.mask_stride;
const int invert_mask = ms_params->ms_buffers.inv_mask;
if (mask) {
return vfp->msdf(src_buf, src_stride, ref_address, ref_stride, second_pred,
mask, mask_stride, invert_mask);
} else if (second_pred) {
const MB_MODE_INFO *mi = ms_params->xd->mi[0];
if (get_cwp_idx(mi) != CWP_EQUAL) {
DIST_WTD_COMP_PARAMS jcp_param;
set_cmp_weight(mi, invert_mask, &jcp_param);
return vfp->jsdaf(src_buf, src_stride, ref_address, ref_stride,
second_pred, &jcp_param);
}
return vfp->sdaf(src_buf, src_stride, ref_address, ref_stride, second_pred);
} else {
return ms_params->sdf(src_buf, src_stride, ref_address, ref_stride);
}
}
// Calculates and returns a sad+mvcost list around an integer best pel during
// fullpixel motion search. The resulting list can be used to speed up subpel
// motion search later.
#define USE_SAD_COSTLIST 1
// calc_int_cost_list uses var to populate the costlist, which is more accurate
// than sad but slightly slower.
static AOM_FORCE_INLINE void calc_int_cost_list(
const FULLPEL_MV best_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
int *cost_list) {
static const FULLPEL_MV neighbors[4] = {
{ 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
};
const int br = best_mv.row;
const int bc = best_mv.col;
// costlist is not supported for the 2/4 MV precision
assert(ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_ONE_PEL);
cost_list[0] = get_mvpred_var_cost(ms_params, &best_mv);
if (check_bounds(&ms_params->mv_limits, br, bc, 1)) {
for (int i = 0; i < 4; i++) {
const FULLPEL_MV neighbor_mv = { br + neighbors[i].row,
bc + neighbors[i].col };
cost_list[i + 1] = get_mvpred_var_cost(ms_params, &neighbor_mv);
}
} else {
for (int i = 0; i < 4; i++) {
const FULLPEL_MV neighbor_mv = { br + neighbors[i].row,
bc + neighbors[i].col };
if (!av1_is_fullmv_in_range(&ms_params->mv_limits, neighbor_mv,
ms_params->mv_cost_params.pb_mv_precision
)) {
cost_list[i + 1] = INT_MAX;
} else {
cost_list[i + 1] = get_mvpred_var_cost(ms_params, &neighbor_mv);
}
}
}
}
// calc_int_sad_list uses sad to populate the costlist, which is less accurate
// than var but faster.
static AOM_FORCE_INLINE void calc_int_sad_list(
const FULLPEL_MV best_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
int *cost_list, int costlist_has_sad) {
static const FULLPEL_MV neighbors[4] = {
{ 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
};
const struct buf_2d *const src = ms_params->ms_buffers.src;
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const int ref_stride = ref->stride;
const int br = best_mv.row;
const int bc = best_mv.col;
assert(av1_is_fullmv_in_range(&ms_params->mv_limits, best_mv,
ms_params->mv_cost_params.pb_mv_precision));
// costlist is not supported for the 2/4 MV precision
assert(ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_ONE_PEL);
// Refresh the costlist it does not contain valid sad
if (!costlist_has_sad) {
#if CONFIG_IBC_SR_EXT
if (ms_params->is_intra_mode &&
ms_params->cm->features.allow_local_intrabc) {
MV sub_mv = { (int16_t)GET_MV_SUBPEL(best_mv.row),
(int16_t)GET_MV_SUBPEL(best_mv.col) };
int flag = av1_is_dv_valid(sub_mv, ms_params->cm, ms_params->xd,
ms_params->mi_row, ms_params->mi_col,
ms_params->bsize, ms_params->mib_size_log2);
if (flag) {
cost_list[0] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &best_mv), ref_stride);
} else {
cost_list[0] = INT_MAX;
}
} else {
cost_list[0] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &best_mv), ref_stride);
}
#else
cost_list[0] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &best_mv), ref_stride);
#endif // CONFIG_IBC_SR_EXT
if (check_bounds(&ms_params->mv_limits, br, bc, 1)) {
for (int i = 0; i < 4; i++) {
const FULLPEL_MV this_mv = { br + neighbors[i].row,
bc + neighbors[i].col };
#if CONFIG_IBC_SR_EXT
if (ms_params->is_intra_mode &&
ms_params->cm->features.allow_local_intrabc) {
MV sub_mv = { (int16_t)GET_MV_SUBPEL(this_mv.row),
(int16_t)GET_MV_SUBPEL(this_mv.col) };
int flag = av1_is_dv_valid(
sub_mv, ms_params->cm, ms_params->xd, ms_params->mi_row,
ms_params->mi_col, ms_params->bsize, ms_params->mib_size_log2);
if (flag) {
cost_list[i + 1] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
} else {
cost_list[i + 1] = INT_MAX;
}
} else {
cost_list[i + 1] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
}
#else
cost_list[i + 1] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
#endif // CONFIG_IBC_SR_EXT
}
} else {
for (int i = 0; i < 4; i++) {
const FULLPEL_MV this_mv = { br + neighbors[i].row,
bc + neighbors[i].col };
if (!av1_is_fullmv_in_range(
&ms_params->mv_limits, this_mv,
ms_params->mv_cost_params.pb_mv_precision)) {
cost_list[i + 1] = INT_MAX;
} else {
#if CONFIG_IBC_SR_EXT
if (ms_params->is_intra_mode &&
ms_params->cm->features.allow_local_intrabc) {
MV sub_mv = { (int16_t)GET_MV_SUBPEL(this_mv.row),
(int16_t)GET_MV_SUBPEL(this_mv.col) };
int flag = av1_is_dv_valid(
sub_mv, ms_params->cm, ms_params->xd, ms_params->mi_row,
ms_params->mi_col, ms_params->bsize, ms_params->mib_size_log2);
if (flag) {
cost_list[i + 1] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv),
ref_stride);
} else {
cost_list[i + 1] = INT_MAX;
}
} else {
cost_list[i + 1] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
}
#else
cost_list[i + 1] = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
#endif // CONFIG_IBC_SR_EXT
}
}
}
}
const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
cost_list[0] += mvsad_err_cost(best_mv, mv_cost_params);
for (int idx = 0; idx < 4; idx++) {
if (cost_list[idx + 1] != INT_MAX) {
const FULLPEL_MV this_mv = { br + neighbors[idx].row,
bc + neighbors[idx].col };
cost_list[idx + 1] += mvsad_err_cost(this_mv, mv_cost_params);
}
}
}
// Computes motion vector cost and adds to the sad cost.
// Then updates the best sad and motion vectors.
// Inputs:
// this_sad: the sad to be evaluated.
// mv: the current motion vector.
// mv_cost_params: a structure containing information to compute mv cost.
// best_sad: the current best sad.
// raw_best_sad (optional): the current best sad without calculating mv cost.
// best_mv: the current best motion vector.
// second_best_mv (optional): the second best motion vector up to now.
// Modifies:
// best_sad, raw_best_sad, best_mv, second_best_mv
// If the current sad is lower than the current best sad.
// Returns:
// Whether the input sad (mv) is better than the current best.
static int update_mvs_and_sad(const unsigned int this_sad, const FULLPEL_MV *mv,
const MV_COST_PARAMS *mv_cost_params,
unsigned int *best_sad,
unsigned int *raw_best_sad, FULLPEL_MV *best_mv,
FULLPEL_MV *second_best_mv) {
if (this_sad >= *best_sad) return 0;
// Add the motion vector cost.
const unsigned int sad = this_sad + mvsad_err_cost(*mv, mv_cost_params);
if (sad < *best_sad) {
if (raw_best_sad) *raw_best_sad = this_sad;
*best_sad = sad;
if (second_best_mv) *second_best_mv = *best_mv;
*best_mv = *mv;
return 1;
}
return 0;
}
// Calculate sad4 and update the bestmv information
// in FAST_DIAMOND search method.
static void calc_sad4_update_bestmv(
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const MV_COST_PARAMS *mv_cost_params, FULLPEL_MV *best_mv,
FULLPEL_MV *temp_best_mv, unsigned int *bestsad, unsigned int *raw_bestsad,
int search_step, int *best_site, int cand_start) {
const struct buf_2d *const src = ms_params->ms_buffers.src;
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const search_site *site = ms_params->search_sites->site[search_step];
uint16_t const *block_offset[4];
unsigned int sads[4];
const uint16_t *best_address;
const uint16_t *src_buf = src->buf;
const int src_stride = src->stride;
best_address = get_buf_from_fullmv(ref, temp_best_mv);
// Loop over number of candidates.
for (int j = 0; j < 4; j++)
block_offset[j] = site[cand_start + j].offset + best_address;
// 4-point sad calculation.
ms_params->sdx4df(src_buf, src_stride, block_offset, ref->stride, sads);
assert(ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_ONE_PEL);
for (int j = 0; j < 4; j++) {
const FULLPEL_MV this_mv = {
temp_best_mv->row + site[cand_start + j].mv.row,
temp_best_mv->col + site[cand_start + j].mv.col
};
const int found_better_mv = update_mvs_and_sad(
sads[j], &this_mv, mv_cost_params, bestsad, raw_bestsad, best_mv,
/*second_best_mv=*/NULL);
if (found_better_mv) *best_site = cand_start + j;
}
}
// Calculate sad and update the bestmv information
// in FAST_DIAMOND search method.
static void calc_sad_update_bestmv(
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const MV_COST_PARAMS *mv_cost_params, FULLPEL_MV *best_mv,
FULLPEL_MV *temp_best_mv, unsigned int *bestsad, unsigned int *raw_bestsad,
int search_step, int *best_site, const int num_candidates, int cand_start) {
const struct buf_2d *const src = ms_params->ms_buffers.src;
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const search_site *site = ms_params->search_sites->site[search_step];
assert(ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_ONE_PEL);
// Loop over number of candidates.
for (int i = cand_start; i < num_candidates; i++) {
const FULLPEL_MV this_mv = { temp_best_mv->row + site[i].mv.row,
temp_best_mv->col + site[i].mv.col };
if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv,
ms_params->mv_cost_params.pb_mv_precision))
continue;
int thissad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref->stride);
const int found_better_mv = update_mvs_and_sad(
thissad, &this_mv, mv_cost_params, bestsad, raw_bestsad, best_mv,
/*second_best_mv=*/NULL);
if (found_better_mv) *best_site = i;
}
}
// Generic pattern search function that searches over multiple scales.
// Each scale can have a different number of candidates and shape of
// candidates as indicated in the num_candidates and candidates arrays
// passed into this function
static int pattern_search(FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
int search_step, const int do_init_search,
int *cost_list, FULLPEL_MV *best_mv) {
static const int search_steps[MAX_MVSEARCH_STEPS] = {
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
};
int i, s, t;
const struct buf_2d *const src = ms_params->ms_buffers.src;
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const search_site_config *search_sites = ms_params->search_sites;
const int *num_candidates = search_sites->searches_per_step;
const int ref_stride = ref->stride;
const int last_is_4 = num_candidates[0] == 4;
int br, bc;
unsigned int bestsad = UINT_MAX, raw_bestsad = UINT_MAX;
int thissad;
int k = -1;
const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
search_step = AOMMIN(search_step, MAX_MVSEARCH_STEPS - 1);
assert(search_step >= 0);
assert(ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_ONE_PEL);
int best_init_s = search_steps[search_step];
// adjust ref_mv to make sure it is within MV range
clamp_fullmv(&start_mv, &ms_params->mv_limits);
br = start_mv.row;
bc = start_mv.col;
if (cost_list != NULL) {
cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
INT_MAX;
}
int costlist_has_sad = 0;
// Work out the start point for the search
raw_bestsad = get_mvpred_sad(ms_params, src,
get_buf_from_fullmv(ref, &start_mv), ref_stride);
bestsad = raw_bestsad + mvsad_err_cost(start_mv, mv_cost_params);
// Search all possible scales up to the search param around the center point
// pick the scale of the point that is best as the starting scale of
// further steps around it.
if (do_init_search) {
s = best_init_s;
best_init_s = -1;
for (t = 0; t <= s; ++t) {
int best_site = -1;
FULLPEL_MV temp_best_mv;
temp_best_mv.row = br;
temp_best_mv.col = bc;
if (check_bounds(&ms_params->mv_limits, br, bc, 1 << t)) {
// Call 4-point sad for multiples of 4 candidates.
const int no_of_4_cand_loops = num_candidates[t] >> 2;
for (i = 0; i < no_of_4_cand_loops; i++) {
calc_sad4_update_bestmv(ms_params, mv_cost_params, best_mv,
&temp_best_mv, &bestsad, &raw_bestsad, t,
&best_site, i * 4);
}
// Rest of the candidates
const int remaining_cand = num_candidates[t] % 4;
calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv,
&temp_best_mv, &bestsad, &raw_bestsad, t,
&best_site, remaining_cand,
no_of_4_cand_loops * 4);
} else {
calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv,
&temp_best_mv, &bestsad, &raw_bestsad, t,
&best_site, num_candidates[t], 0);
}
if (best_site == -1) {
continue;
} else {
best_init_s = t;
k = best_site;
}
}
if (best_init_s != -1) {
br += search_sites->site[best_init_s][k].mv.row;
bc += search_sites->site[best_init_s][k].mv.col;
}
}
// If the center point is still the best, just skip this and move to
// the refinement step.
if (best_init_s != -1) {
const int last_s = (last_is_4 && cost_list != NULL);
int best_site = -1;
s = best_init_s;
for (; s >= last_s; s--) {
// No need to search all points the 1st time if initial search was used
if (!do_init_search || s != best_init_s) {
FULLPEL_MV temp_best_mv;
temp_best_mv.row = br;
temp_best_mv.col = bc;
if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
// Call 4-point sad for multiples of 4 candidates.
const int no_of_4_cand_loops = num_candidates[s] >> 2;
for (i = 0; i < no_of_4_cand_loops; i++) {
calc_sad4_update_bestmv(ms_params, mv_cost_params, best_mv,
&temp_best_mv, &bestsad, &raw_bestsad, s,
&best_site, i * 4);
}
// Rest of the candidates
const int remaining_cand = num_candidates[s] % 4;
calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv,
&temp_best_mv, &bestsad, &raw_bestsad, s,
&best_site, remaining_cand,
no_of_4_cand_loops * 4);
} else {
calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv,
&temp_best_mv, &bestsad, &raw_bestsad, s,
&best_site, num_candidates[s], 0);
}
if (best_site == -1) {
continue;
} else {
br += search_sites->site[s][best_site].mv.row;
bc += search_sites->site[s][best_site].mv.col;
k = best_site;
}
}
do {
int next_chkpts_indices[PATTERN_CANDIDATES_REF];
best_site = -1;
next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
next_chkpts_indices[1] = k;
next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
const FULLPEL_MV this_mv = {
br + search_sites->site[s][next_chkpts_indices[i]].mv.row,
bc + search_sites->site[s][next_chkpts_indices[i]].mv.col
};
thissad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
const int found_better_mv =
update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
&raw_bestsad, best_mv,
/*second_best_mv=*/NULL);
if (found_better_mv) best_site = i;
}
} else {
for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
const FULLPEL_MV this_mv = {
br + search_sites->site[s][next_chkpts_indices[i]].mv.row,
bc + search_sites->site[s][next_chkpts_indices[i]].mv.col
};
if (!av1_is_fullmv_in_range(
&ms_params->mv_limits, this_mv,
ms_params->mv_cost_params.pb_mv_precision))
continue;
thissad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
const int found_better_mv =
update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
&raw_bestsad, best_mv,
/*second_best_mv=*/NULL);
if (found_better_mv) best_site = i;
}
}
if (best_site != -1) {
k = next_chkpts_indices[best_site];
br += search_sites->site[s][k].mv.row;
bc += search_sites->site[s][k].mv.col;
}
} while (best_site != -1);
}
// Note: If we enter the if below, then cost_list must be non-NULL.
if (s == 0) {
cost_list[0] = raw_bestsad;
costlist_has_sad = 1;
if (!do_init_search || s != best_init_s) {
if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
for (i = 0; i < num_candidates[s]; i++) {
const FULLPEL_MV this_mv = { br + search_sites->site[s][i].mv.row,
bc + search_sites->site[s][i].mv.col };
cost_list[i + 1] = thissad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
const int found_better_mv =
update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
&raw_bestsad, best_mv,
/*second_best_mv=*/NULL);
if (found_better_mv) best_site = i;
}
} else {
for (i = 0; i < num_candidates[s]; i++) {
const FULLPEL_MV this_mv = { br + search_sites->site[s][i].mv.row,
bc + search_sites->site[s][i].mv.col };
if (!av1_is_fullmv_in_range(
&ms_params->mv_limits, this_mv,
ms_params->mv_cost_params.pb_mv_precision))
continue;
cost_list[i + 1] = thissad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
const int found_better_mv =
update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
&raw_bestsad, best_mv,
/*second_best_mv=*/NULL);
if (found_better_mv) best_site = i;
}
}
if (best_site != -1) {
br += search_sites->site[s][best_site].mv.row;
bc += search_sites->site[s][best_site].mv.col;
k = best_site;
}
}
while (best_site != -1) {
int next_chkpts_indices[PATTERN_CANDIDATES_REF];
best_site = -1;
next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
next_chkpts_indices[1] = k;
next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
cost_list[((k + 2) % 4) + 1] = cost_list[0];
cost_list[0] = raw_bestsad;
if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
const FULLPEL_MV this_mv = {
br + search_sites->site[s][next_chkpts_indices[i]].mv.row,
bc + search_sites->site[s][next_chkpts_indices[i]].mv.col
};
cost_list[next_chkpts_indices[i] + 1] = thissad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
const int found_better_mv =
update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
&raw_bestsad, best_mv,
/*second_best_mv=*/NULL);
if (found_better_mv) best_site = i;
}
} else {
for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
const FULLPEL_MV this_mv = {
br + search_sites->site[s][next_chkpts_indices[i]].mv.row,
bc + search_sites->site[s][next_chkpts_indices[i]].mv.col
};
if (!av1_is_fullmv_in_range(
&ms_params->mv_limits, this_mv,
ms_params->mv_cost_params.pb_mv_precision)) {
cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
continue;
}
cost_list[next_chkpts_indices[i] + 1] = thissad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
const int found_better_mv =
update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
&raw_bestsad, best_mv,
/*second_best_mv=*/NULL);
if (found_better_mv) best_site = i;
}
}
if (best_site != -1) {
k = next_chkpts_indices[best_site];
br += search_sites->site[s][k].mv.row;
bc += search_sites->site[s][k].mv.col;
}
}
}
}
best_mv->row = br;
best_mv->col = bc;
// Returns the one-away integer pel cost/sad around the best as follows:
// cost_list[0]: cost/sad at the best integer pel
// cost_list[1]: cost/sad at delta {0, -1} (left) from the best integer pel
// cost_list[2]: cost/sad at delta { 1, 0} (bottom) from the best integer pel
// cost_list[3]: cost/sad at delta { 0, 1} (right) from the best integer pel
// cost_list[4]: cost/sad at delta {-1, 0} (top) from the best integer pel
if (cost_list) {
if (USE_SAD_COSTLIST) {
calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
} else {
calc_int_cost_list(*best_mv, ms_params, cost_list);
}
}
best_mv->row = br;
best_mv->col = bc;
const int var_cost = get_mvpred_var_cost(ms_params, best_mv);
return var_cost;
}
// For the following foo_search, the input arguments are:
// start_mv: where we are starting our motion search
// ms_params: a collection of motion search parameters
// search_step: how many steps to skip in our motion search. For example,
// a value 3 suggests that 3 search steps have already taken place prior to
// this function call, so we jump directly to step 4 of the search process
// do_init_search: if on, do an initial search of all possible scales around the
// start_mv, and then pick the best scale.
// cond_list: used to hold the cost around the best full mv so we can use it to
// speed up subpel search later.
// best_mv: the best mv found in the motion search
static int hex_search(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int search_step, const int do_init_search,
int *cost_list, FULLPEL_MV *best_mv) {
return pattern_search(start_mv, ms_params, search_step, do_init_search,
cost_list, best_mv);
}
static int bigdia_search(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int search_step, const int do_init_search,
int *cost_list, FULLPEL_MV *best_mv) {
return pattern_search(start_mv, ms_params, search_step, do_init_search,
cost_list, best_mv);
}
static int square_search(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int search_step, const int do_init_search,
int *cost_list, FULLPEL_MV *best_mv) {
return pattern_search(start_mv, ms_params, search_step, do_init_search,
cost_list, best_mv);
}
static int fast_hex_search(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int search_step, const int do_init_search,
int *cost_list, FULLPEL_MV *best_mv) {
return hex_search(start_mv, ms_params,
AOMMAX(MAX_MVSEARCH_STEPS - 2, search_step), do_init_search,
cost_list, best_mv);
}
static int fast_dia_search(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int search_step, const int do_init_search,
int *cost_list, FULLPEL_MV *best_mv) {
return bigdia_search(start_mv, ms_params,
AOMMAX(MAX_MVSEARCH_STEPS - 2, search_step),
do_init_search, cost_list, best_mv);
}
static int fast_bigdia_search(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int search_step, const int do_init_search,
int *cost_list, FULLPEL_MV *best_mv) {
return bigdia_search(start_mv, ms_params,
AOMMAX(MAX_MVSEARCH_STEPS - 3, search_step),
do_init_search, cost_list, best_mv);
}
static int diamond_search_sad(FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int search_step, int *num00,
FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
const struct buf_2d *const src = ms_params->ms_buffers.src;
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const int ref_stride = ref->stride;
const uint16_t *best_address;
const uint8_t *mask = ms_params->ms_buffers.mask;
const uint16_t *second_pred = ms_params->ms_buffers.second_pred;
const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
const search_site_config *cfg = ms_params->search_sites;
const int prec_shift =
(ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_ONE_PEL)
? 0
: (MV_PRECISION_ONE_PEL - ms_params->mv_cost_params.pb_mv_precision);
const int prec_multiplier = (1 << prec_shift);
assert(is_this_mv_precision_compliant(
get_mv_from_fullmv(&start_mv),
ms_params->mv_cost_params.pb_mv_precision));
unsigned int bestsad = INT_MAX;
int best_site = 0;
int is_off_center = 0;
clamp_fullmv(&start_mv, &ms_params->mv_limits);
// search_step determines the length of the initial step and hence the number
// of iterations.
const int tot_steps = cfg->num_search_steps - search_step;
*num00 = 0;
*best_mv = start_mv;
// Check the starting position
#if CONFIG_IBC_SR_EXT
if (ms_params->is_intra_mode && ms_params->cm->features.allow_local_intrabc) {
MV sub_mv = { (int16_t)GET_MV_SUBPEL(start_mv.row),
(int16_t)GET_MV_SUBPEL(start_mv.col) };
if (av1_is_dv_valid(sub_mv, ms_params->cm, ms_params->xd, ms_params->mi_row,
ms_params->mi_col, ms_params->bsize,
ms_params->mib_size_log2)) {
best_address = get_buf_from_fullmv(ref, &start_mv);
bestsad =
get_mvpred_compound_sad(ms_params, src, best_address, ref_stride);
bestsad += mvsad_err_cost(*best_mv, &ms_params->mv_cost_params);
} else {
best_address = get_buf_from_fullmv(ref, &start_mv);
bestsad = INT_MAX;
}
} else {
best_address = get_buf_from_fullmv(ref, &start_mv);
bestsad = get_mvpred_compound_sad(ms_params, src, best_address, ref_stride);
bestsad += mvsad_err_cost(*best_mv, &ms_params->mv_cost_params);
}
#else
best_address = get_buf_from_fullmv(ref, &start_mv);
bestsad = get_mvpred_compound_sad(ms_params, src, best_address, ref_stride);
bestsad += mvsad_err_cost(*best_mv, &ms_params->mv_cost_params);
#endif // CONFIG_IBC_SR_EXT
int next_step_size = tot_steps > 2 ? cfg->radius[tot_steps - 2] : 1;
for (int step = tot_steps - 1; step >= 0; --step) {
const search_site *site = cfg->site[step];
best_site = 0;
if (step > 0) next_step_size = cfg->radius[step - 1];
int all_in = 1, j;
// Trap illegal vectors
all_in &= best_mv->row + (site[1].mv.row * prec_multiplier) >=
ms_params->mv_limits.row_min;
all_in &= best_mv->row + (site[2].mv.row * prec_multiplier) <=
ms_params->mv_limits.row_max;
all_in &= best_mv->col + (site[3].mv.col * prec_multiplier) >=
ms_params->mv_limits.col_min;
all_in &= best_mv->col + (site[4].mv.col * prec_multiplier) <=
ms_params->mv_limits.col_max;
#if CONFIG_IBC_SR_EXT
if (ms_params->is_intra_mode &&
ms_params->cm->features.allow_local_intrabc) {
for (j = 0; j < 4; j++) {
MV sub_mv = { (int16_t)GET_MV_SUBPEL(best_mv->row + site[1 + j].mv.row),
(int16_t)GET_MV_SUBPEL(best_mv->col +
site[1 + j].mv.col) };
all_in &= av1_is_dv_valid(sub_mv, ms_params->cm, ms_params->xd,
ms_params->mi_row, ms_params->mi_col,
ms_params->bsize, ms_params->mib_size_log2);
}
}
#endif // CONFIG_IBC_SR_EXT
// TODO(anyone): Implement 4 points search for msdf&sdaf
if (all_in && !mask && !second_pred) {
const uint16_t *src_buf = src->buf;
const int src_stride = src->stride;
for (int idx = 1; idx <= cfg->searches_per_step[step]; idx += 4) {
uint16_t const *block_offset[4];
unsigned int sads[4];
#if CONFIG_IBC_SR_EXT
int valid = 1;
for (j = 0; j < 4; j++) {
if (ms_params->is_intra_mode &&
ms_params->cm->features.allow_local_intrabc) {
MV sub_mv = {
(int16_t)GET_MV_SUBPEL(best_mv->row + site[idx + j].mv.row),
(int16_t)GET_MV_SUBPEL(best_mv->col + site[idx + j].mv.col)
};
valid &= av1_is_dv_valid(
sub_mv, ms_params->cm, ms_params->xd, ms_params->mi_row,
ms_params->mi_col, ms_params->bsize, ms_params->mib_size_log2);
}
}
if (!valid) continue;
#endif // CONFIG_IBC_SR_EXT
for (j = 0; j < 4; j++) {
int row = (site[idx + j].mv.row * prec_multiplier);
int col = (site[idx + j].mv.col * prec_multiplier);
block_offset[j] = (row * ref_stride + col) + best_address;
}
ms_params->sdx4df(src_buf, src_stride, block_offset, ref_stride, sads);
for (j = 0; j < 4; j++) {
if (sads[j] < bestsad) {
const FULLPEL_MV this_mv = {
best_mv->row + (site[idx + j].mv.row * prec_multiplier),
best_mv->col + (site[idx + j].mv.col * prec_multiplier)
};
unsigned int thissad =
sads[j] + mvsad_err_cost(this_mv, mv_cost_params);
if (thissad < bestsad) {
bestsad = thissad;
best_site = idx + j;
}
}
}
}
} else {
for (int idx = 1; idx <= cfg->searches_per_step[step]; idx++) {
const FULLPEL_MV this_mv = {
best_mv->row + (site[idx].mv.row * prec_multiplier),
best_mv->col + (site[idx].mv.col * prec_multiplier)
};
if (av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv,
ms_params->mv_cost_params.pb_mv_precision)) {
#if CONFIG_IBC_SR_EXT
if (ms_params->is_intra_mode &&
ms_params->cm->features.allow_local_intrabc) {
MV sub_mv = { (int16_t)GET_MV_SUBPEL(this_mv.row),
(int16_t)GET_MV_SUBPEL(this_mv.col) };
int valid = av1_is_dv_valid(
sub_mv, ms_params->cm, ms_params->xd, ms_params->mi_row,
ms_params->mi_col, ms_params->bsize, ms_params->mib_size_log2);
if (!valid) continue;
}
#endif // CONFIG_IBC_SR_EXT
int r = (site[idx].mv.row * prec_multiplier);
int c = (site[idx].mv.col * prec_multiplier);
const uint16_t *const check_here =
(r * ref_stride + c) + best_address;
unsigned int thissad;
thissad =
get_mvpred_compound_sad(ms_params, src, check_here, ref_stride);
assert(is_this_mv_precision_compliant(
get_mv_from_fullmv(&this_mv),
ms_params->mv_cost_params.pb_mv_precision));
if (thissad < bestsad) {
thissad += mvsad_err_cost(this_mv, mv_cost_params);
if (thissad < bestsad) {
bestsad = thissad;
best_site = idx;
}
}
}
}
}
if (best_site != 0) {
if (second_best_mv) {
*second_best_mv = *best_mv;
}
best_mv->row += (site[best_site].mv.row * prec_multiplier);
best_mv->col += (site[best_site].mv.col * prec_multiplier);
best_address += (site[best_site].mv.row * prec_multiplier) * ref_stride +
(site[best_site].mv.col * prec_multiplier);
is_off_center = 1;
}
if (is_off_center == 0) (*num00)++;
if (best_site == 0) {
while (next_step_size == cfg->radius[step] && step > 2) {
++(*num00);
--step;
next_step_size = cfg->radius[step - 1];
}
}
}
return bestsad;
}
/* do_refine: If last step (1-away) of n-step search doesn't pick the center
point as the best match, we will do a final 1-away diamond
refining search */
static int full_pixel_diamond(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int step_param, int *cost_list,
FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
const search_site_config *cfg = ms_params->search_sites;
int thissme, n, num00 = 0;
int bestsme = diamond_search_sad(start_mv, ms_params, step_param, &n, best_mv,
second_best_mv);
if (bestsme < INT_MAX) {
bestsme = get_mvpred_compound_var_cost(ms_params, best_mv);
}
// If there won't be more n-step search, check to see if refining search is
// needed.
const int further_steps = cfg->num_search_steps - 1 - step_param;
while (n < further_steps) {
++n;
if (num00) {
num00--;
} else {
// TODO(chiyotsai@google.com): There is another bug here where the second
// best mv gets incorrectly overwritten. Fix it later.
FULLPEL_MV tmp_best_mv;
thissme = diamond_search_sad(start_mv, ms_params, step_param + n, &num00,
&tmp_best_mv, second_best_mv);
if (thissme < INT_MAX) {
thissme = get_mvpred_compound_var_cost(ms_params, &tmp_best_mv);
}
if (thissme < bestsme) {
bestsme = thissme;
*best_mv = tmp_best_mv;
}
}
}
// Return cost list.
if (cost_list) {
if (USE_SAD_COSTLIST) {
const int costlist_has_sad = 0;
calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
} else {
calc_int_cost_list(*best_mv, ms_params, cost_list);
}
}
return bestsme;
}
// Exhaustive motion search around a given centre position with a given
// step size.
static int exhaustive_mesh_search(FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int range, const int step,
FULLPEL_MV *best_mv,
FULLPEL_MV *second_best_mv) {
const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
const struct buf_2d *const src = ms_params->ms_buffers.src;
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const int ref_stride = ref->stride;
unsigned int best_sad = INT_MAX;
int r, c, i;
int start_col, end_col, start_row, end_row;
const int col_step = (step > 1) ? step : 4;
assert(step >= 1);
clamp_fullmv(&start_mv, &ms_params->mv_limits);
*best_mv = start_mv;
#if CONFIG_IBC_SR_EXT
if (ms_params->is_intra_mode && ms_params->cm->features.allow_local_intrabc) {
const MV sub_mv = { (int16_t)GET_MV_SUBPEL(start_mv.row),
(int16_t)GET_MV_SUBPEL(start_mv.col) };
if (av1_is_dv_valid(sub_mv, ms_params->cm, ms_params->xd, ms_params->mi_row,
ms_params->mi_col, ms_params->bsize,
ms_params->mib_size_log2)) {
best_sad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &start_mv), ref_stride);
best_sad += mvsad_err_cost(start_mv, mv_cost_params);
} else {
best_sad = INT_MAX;
}
} else {
best_sad = get_mvpred_sad(ms_params, src,
get_buf_from_fullmv(ref, &start_mv), ref_stride);
best_sad += mvsad_err_cost(start_mv, mv_cost_params);
}
#else
best_sad = get_mvpred_sad(ms_params, src, get_buf_from_fullmv(ref, &start_mv),
ref_stride);
best_sad += mvsad_err_cost(start_mv, mv_cost_params);
#endif // CONFIG_IBC_SR_EXT
start_row = AOMMAX(-range, ms_params->mv_limits.row_min - start_mv.row);
start_col = AOMMAX(-range, ms_params->mv_limits.col_min - start_mv.col);
end_row = AOMMIN(range, ms_params->mv_limits.row_max - start_mv.row);
end_col = AOMMIN(range, ms_params->mv_limits.col_max - start_mv.col);
#if CONFIG_IBC_SR_EXT
if (ms_params->is_intra_mode && ms_params->cm->features.allow_local_intrabc) {
int part_size = 65;
int part_start_row;
int part_start_col;
int part_end_row;
int part_end_col;
FULLPEL_MV best_valid_mv = start_mv;
unsigned int best_valid_sad = best_sad;
for (part_start_row = start_row; part_start_row <= end_row;
part_start_row += part_size) {
part_end_row = AOMMIN(part_start_row + part_size - 1, end_row);
for (part_start_col = start_col; part_start_col <= end_col;
part_start_col += part_size) {
part_end_col = AOMMIN(part_start_col + part_size - 1, end_col);
for (r = part_start_row; r <= part_end_row; r += step) {
for (c = part_start_col; c <= part_end_col; c += col_step) {
// Step > 1 means we are not checking every location in this pass.
if (step > 1) {
const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c };
unsigned int sad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad,
/*raw_best_sad=*/NULL, best_mv,
second_best_mv);
} else {
// 4 sads in a single call if we are checking every location
if (c + 3 <= part_end_col) {
unsigned int sads[4];
const uint16_t *addrs[4];
for (i = 0; i < 4; ++i) {
const FULLPEL_MV mv = { start_mv.row + r,
start_mv.col + c + i };
addrs[i] = get_buf_from_fullmv(ref, &mv);
}
ms_params->sdx4df(src->buf, src->stride, addrs, ref_stride,
sads);
for (i = 0; i < 4; ++i) {
if (sads[i] < best_sad) {
const FULLPEL_MV mv = { start_mv.row + r,
start_mv.col + c + i };
update_mvs_and_sad(sads[i], &mv, mv_cost_params, &best_sad,
/*raw_best_sad=*/NULL, best_mv,
second_best_mv);
}
}
} else {
for (i = 0; i < part_end_col - c; ++i) {
const FULLPEL_MV mv = { start_mv.row + r,
start_mv.col + c + i };
unsigned int sad =
get_mvpred_sad(ms_params, src,
get_buf_from_fullmv(ref, &mv), ref_stride);
update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad,
/*raw_best_sad=*/NULL, best_mv,
second_best_mv);
}
}
}
}
}
// stores the best valid mv
if (best_valid_mv.row != best_mv->row ||
best_valid_mv.col != best_mv->col) {
const MV sub_mv = { (int16_t)GET_MV_SUBPEL(best_mv->row),
(int16_t)GET_MV_SUBPEL(best_mv->col) };
if (av1_is_dv_valid(sub_mv, ms_params->cm, ms_params->xd,
ms_params->mi_row, ms_params->mi_col,
ms_params->bsize, ms_params->mib_size_log2)) {
best_valid_mv = *best_mv;
best_valid_sad = best_sad;
}
}
*best_mv = best_valid_mv;
best_sad = best_valid_sad;
}
}
return best_sad;
}
#endif // CONFIG_IBC_SR_EXT
for (r = start_row; r <= end_row; r += step) {
for (c = start_col; c <= end_col; c += col_step) {
// Step > 1 means we are not checking every location in this pass.
if (step > 1) {
const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c };
unsigned int sad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad,
/*raw_best_sad=*/NULL, best_mv, second_best_mv);
} else {
// 4 sads in a single call if we are checking every location
if (c + 3 <= end_col) {
unsigned int sads[4];
const uint16_t *addrs[4];
for (i = 0; i < 4; ++i) {
const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
addrs[i] = get_buf_from_fullmv(ref, &mv);
}
ms_params->sdx4df(src->buf, src->stride, addrs, ref_stride, sads);
for (i = 0; i < 4; ++i) {
if (sads[i] < best_sad) {
const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
update_mvs_and_sad(sads[i], &mv, mv_cost_params, &best_sad,
/*raw_best_sad=*/NULL, best_mv,
second_best_mv);
}
}
} else {
for (i = 0; i < end_col - c; ++i) {
const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
unsigned int sad = get_mvpred_sad(
ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad,
/*raw_best_sad=*/NULL, best_mv, second_best_mv);
}
}
}
}
}
return best_sad;
}
// Runs an limited range exhaustive mesh search using a pattern set
// according to the encode speed profile.
static int full_pixel_exhaustive(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const struct MESH_PATTERN *const mesh_patterns,
int *cost_list, FULLPEL_MV *best_mv,
FULLPEL_MV *second_best_mv) {
const int kMinRange = 7;
const int kMaxRange = 256;
const int kMinInterval = 1;
int bestsme;
int i;
int interval = mesh_patterns[0].interval;
int range = mesh_patterns[0].range;
int baseline_interval_divisor;
*best_mv = start_mv;
// Trap illegal values for interval and range for this function.
if ((range < kMinRange) || (range > kMaxRange) || (interval < kMinInterval) ||
(interval > range))
return INT_MAX;
baseline_interval_divisor = range / interval;
// Check size of proposed first range against magnitude of the centre
// value used as a starting point.
range = AOMMAX(range, (5 * AOMMAX(abs(best_mv->row), abs(best_mv->col))) / 4);
range = AOMMIN(range, kMaxRange);
interval = AOMMAX(interval, range / baseline_interval_divisor);
// Use a small search step/interval for certain kind of clips.
// For example, screen content clips with a lot of texts.
// Large interval could lead to a false matching position, and it can't find
// the best global candidate in following iterations due to reduced search
// range. The solution here is to use a small search iterval in the beginning
// and thus reduces the chance of missing the best candidate.
if (ms_params->fine_search_interval) {
interval = AOMMIN(interval, 4);
}
// initial search
bestsme = exhaustive_mesh_search(*best_mv, ms_params, range, interval,
best_mv, second_best_mv);
if ((interval > kMinInterval) && (range > kMinRange)) {
// Progressive searches with range and step size decreasing each time
// till we reach a step size of 1. Then break out.
for (i = 1; i < MAX_MESH_STEP; ++i) {
// First pass with coarser step and longer range
bestsme = exhaustive_mesh_search(
*best_mv, ms_params, mesh_patterns[i].range,
mesh_patterns[i].interval, best_mv, second_best_mv);
if (mesh_patterns[i].interval == 1) break;
}
}
if (bestsme < INT_MAX) {
bestsme = get_mvpred_var_cost(ms_params, best_mv);
}
// Return cost list.
if (cost_list) {
if (USE_SAD_COSTLIST) {
const int costlist_has_sad = 0;
calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
} else {
calc_int_cost_list(*best_mv, ms_params, cost_list);
}
}
return bestsme;
}
// This function is called when we do joint motion search in comp_inter_inter
// mode, or when searching for one component of an ext-inter compound mode.
int av1_refining_search_8p_c(const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const FULLPEL_MV start_mv, FULLPEL_MV *best_mv) {
static const search_neighbors neighbors[8] = {
{ { -1, 0 }, -1 * SEARCH_GRID_STRIDE_8P + 0 },
{ { 0, -1 }, 0 * SEARCH_GRID_STRIDE_8P - 1 },
{ { 0, 1 }, 0 * SEARCH_GRID_STRIDE_8P + 1 },
{ { 1, 0 }, 1 * SEARCH_GRID_STRIDE_8P + 0 },
{ { -1, -1 }, -1 * SEARCH_GRID_STRIDE_8P - 1 },
{ { 1, -1 }, 1 * SEARCH_GRID_STRIDE_8P - 1 },
{ { -1, 1 }, -1 * SEARCH_GRID_STRIDE_8P + 1 },
{ { 1, 1 }, 1 * SEARCH_GRID_STRIDE_8P + 1 }
};
uint8_t do_refine_search_grid[SEARCH_GRID_STRIDE_8P *
SEARCH_GRID_STRIDE_8P] = { 0 };
int grid_center = SEARCH_GRID_CENTER_8P;
int grid_coord = grid_center;
assert(ms_params->mv_cost_params.pb_mv_precision >= MV_PRECISION_ONE_PEL);
const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
const FullMvLimits *mv_limits = &ms_params->mv_limits;
const MSBuffers *ms_buffers = &ms_params->ms_buffers;
const struct buf_2d *src = ms_buffers->src;
const struct buf_2d *ref = ms_buffers->ref;
const int ref_stride = ref->stride;
*best_mv = start_mv;
clamp_fullmv(best_mv, mv_limits);
unsigned int best_sad = get_mvpred_compound_sad(
ms_params, src, get_buf_from_fullmv(ref, best_mv), ref_stride);
best_sad += mvsad_err_cost(*best_mv, mv_cost_params);
do_refine_search_grid[grid_coord] = 1;
for (int i = 0; i < SEARCH_RANGE_8P; ++i) {
int best_site = -1;
for (int j = 0; j < 8; ++j) {
grid_coord = grid_center + neighbors[j].coord_offset;
if (do_refine_search_grid[grid_coord] == 1) {
continue;
}
const FULLPEL_MV mv = { best_mv->row + neighbors[j].coord.row,
best_mv->col + neighbors[j].coord.col };
do_refine_search_grid[grid_coord] = 1;
if (av1_is_fullmv_in_range(mv_limits, mv
,
ms_params->mv_cost_params.pb_mv_precision
)) {
unsigned int sad;
sad = get_mvpred_compound_sad(
ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
if (sad < best_sad) {
sad += mvsad_err_cost(mv, mv_cost_params);
if (sad < best_sad) {
best_sad = sad;
best_site = j;
}
}
}
}
if (best_site == -1) {
break;
} else {
best_mv->row += neighbors[best_site].coord.row;
best_mv->col += neighbors[best_site].coord.col;
grid_center += neighbors[best_site].coord_offset;
}
}
return best_sad;
}
// This function is called when precision of motion vector is lower than inter
// pel. This function is called when we do joint motion search in
// comp_inter_inter mode, or when searching for one component of an ext-inter
// compound mode.
int av1_refining_search_8p_c_low_precision(
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV start_mv,
FULLPEL_MV *best_mv, int fast_mv_refinement) {
assert(ms_params->mv_cost_params.pb_mv_precision < MV_PRECISION_ONE_PEL);
const int search_range =
1 << (MV_PRECISION_ONE_PEL - ms_params->mv_cost_params.pb_mv_precision);
const int search_grid_stride = (2 * search_range + 1);
const search_neighbors neighbors[8] = {
{ { -search_range, 0 }, -search_range * search_grid_stride + 0 },
{ { 0, -search_range }, 0 * search_grid_stride - search_range },
{ { 0, search_range }, 0 * search_grid_stride + search_range },
{ { search_range, 0 }, search_range * search_grid_stride + 0 },
{ { -search_range, -search_range },
-search_range * search_grid_stride - search_range },
{ { search_range, -search_range },
search_range * search_grid_stride - search_range },
{ { -search_range, search_range },
-search_range * search_grid_stride + search_range },
{ { search_range, search_range },
search_range * search_grid_stride + search_range }
};
const int num_of_search_steps = fast_mv_refinement ? 1 : 3;
assert(ms_params->mv_cost_params.pb_mv_precision < MV_PRECISION_ONE_PEL);
const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
const FullMvLimits *mv_limits = &ms_params->mv_limits;
const MSBuffers *ms_buffers = &ms_params->ms_buffers;
const struct buf_2d *src = ms_buffers->src;
const struct buf_2d *ref = ms_buffers->ref;
const int ref_stride = ref->stride;
*best_mv = start_mv;
clamp_fullmv(best_mv, mv_limits);
unsigned int best_sad = get_mvpred_compound_sad(
ms_params, src, get_buf_from_fullmv(ref, best_mv), ref_stride);
best_sad += mvsad_err_cost(*best_mv, mv_cost_params);
for (int step = 0; step < num_of_search_steps; step++) {
int best_site = -1;
// TODO(Mohammed): remove retundant search points to reduce complexity
for (int j = 0; j < 8; ++j) {
const FULLPEL_MV mv = { best_mv->row + neighbors[j].coord.row,
best_mv->col + neighbors[j].coord.col };
if (av1_is_fullmv_in_range(mv_limits, mv,
ms_params->mv_cost_params.pb_mv_precision)) {
unsigned int sad;
sad = get_mvpred_compound_sad(
ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
if (sad < best_sad) {
sad += mvsad_err_cost(mv, mv_cost_params);
if (sad < best_sad) {
best_sad = sad;
best_site = j;
}
}
}
}
if (best_site == -1) {
break;
} else {
best_mv->row += neighbors[best_site].coord.row;
best_mv->col += neighbors[best_site].coord.col;
}
}
return best_sad;
}
int av1_full_pixel_search(const FULLPEL_MV start_mv,
const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
const int step_param, int *cost_list,
FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
const BLOCK_SIZE bsize = ms_params->bsize;
const SEARCH_METHODS search_method = ms_params->search_method;
const int is_intra_mode = ms_params->is_intra_mode;
int run_mesh_search = ms_params->run_mesh_search;
assert(is_this_mv_precision_compliant(
get_mv_from_fullmv(&start_mv),
ms_params->mv_cost_params.pb_mv_precision));
int var = 0;
MARK_MV_INVALID(best_mv);
if (second_best_mv) {
MARK_MV_INVALID(second_best_mv);
}
assert(ms_params->ms_buffers.second_pred == NULL &&
ms_params->ms_buffers.mask == NULL &&
"av1_full_pixel_search does not support compound pred");
if (cost_list) {
cost_list[0] = INT_MAX;
cost_list[1] = INT_MAX;
cost_list[2] = INT_MAX;
cost_list[3] = INT_MAX;
cost_list[4] = INT_MAX;
}
switch (search_method) {
case FAST_BIGDIA:
var = fast_bigdia_search(start_mv, ms_params, step_param, 0, cost_list,
best_mv);
break;
case FAST_DIAMOND:
var = fast_dia_search(start_mv, ms_params, step_param, 0, cost_list,
best_mv);
break