blob: cdd92a7f332aebe7b2761e511d07750c9d374e21 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
James Zerne1cbb132018-08-22 14:10:36 -070012#ifndef AOM_AV1_ENCODER_MCOMP_H_
13#define AOM_AV1_ENCODER_MCOMP_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include "av1/encoder/block.h"
chiyotsai19a58ee2019-03-18 18:01:05 -070016
Yaowu Xuc27fc142016-08-22 16:08:15 -070017#include "aom_dsp/variance.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
chiyotsaie46cff72020-02-05 15:03:34 -080023// In this file, the following variables always have the same meaning:
24// start_mv: the motion vector where we start the motion search
25// ref_mv: the motion vector with respect to which we calculate the mv_cost
26// best_mv: when it is not const, it is the destination where to store the
27// best motion vector
28// full_*: a prefix of full indicates that the mv is a FULLPEL_MV
29//
30// When a mv needs to both act as a fullpel_mv and subpel_mv, it is stored as an
31// int_mv, which is a union of int, FULLPEL_MV, and MV
32
Yaowu Xuc27fc142016-08-22 16:08:15 -070033// The maximum number of steps in a step search given the largest
34// allowed initial step
Yunqing Wange25fdbb2018-03-16 16:36:32 -070035#define MAX_MVSEARCH_STEPS 11
Yaowu Xuc27fc142016-08-22 16:08:15 -070036// Max full pel mv specified in the unit of full pixel
Yunqing Wange25fdbb2018-03-16 16:36:32 -070037// Enable the use of motion vector in range [-1023, 1023].
Yaowu Xuc27fc142016-08-22 16:08:15 -070038#define MAX_FULL_PEL_VAL ((1 << (MAX_MVSEARCH_STEPS - 1)) - 1)
39// Maximum size of the first step in full pel units
40#define MAX_FIRST_STEP (1 << (MAX_MVSEARCH_STEPS - 1))
41// Allowed motion vector pixel distance outside image border
42// for Block_16x16
Yaowu Xuf883b422016-08-30 14:01:10 -070043#define BORDER_MV_PIXELS_B16 (16 + AOM_INTERP_EXTEND)
Yaowu Xuc27fc142016-08-22 16:08:15 -070044
Deepa K Gd4febfb2018-08-14 11:33:51 +053045#define SEARCH_RANGE_8P 3
46#define SEARCH_GRID_STRIDE_8P (2 * SEARCH_RANGE_8P + 1)
47#define SEARCH_GRID_CENTER_8P \
48 (SEARCH_RANGE_8P * SEARCH_GRID_STRIDE_8P + SEARCH_RANGE_8P)
49
Yaowu Xuc27fc142016-08-22 16:08:15 -070050// motion search site
51typedef struct search_site {
chiyotsaie46cff72020-02-05 15:03:34 -080052 FULLPEL_MV mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070053 int offset;
54} search_site;
55
56typedef struct search_site_config {
Jingning Han969a8d42019-12-16 19:40:14 -080057 search_site ss[MAX_MVSEARCH_STEPS * 2][16 + 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -070058 int ss_count;
Jingning Han969a8d42019-12-16 19:40:14 -080059 int searches_per_step[MAX_MVSEARCH_STEPS * 2];
60 int radius[MAX_MVSEARCH_STEPS * 2];
chiyotsai836b69b2019-04-09 13:41:24 -070061 int stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -070062} search_site_config;
63
Deepa K Gd4febfb2018-08-14 11:33:51 +053064typedef struct {
65 MV coord;
66 int coord_offset;
67} search_neighbors;
68
Yaowu Xuf883b422016-08-30 14:01:10 -070069void av1_init_dsmotion_compensation(search_site_config *cfg, int stride);
Jingning Han969a8d42019-12-16 19:40:14 -080070void av1_init_motion_fpf(search_site_config *cfg, int stride);
Yaowu Xuf883b422016-08-30 14:01:10 -070071void av1_init3smotion_compensation(search_site_config *cfg, int stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -070072
Alex Converse0fa0f422017-04-24 12:51:14 -070073void av1_set_mv_search_range(MvLimits *mv_limits, const MV *mv);
Yunqing Wang8e173422017-04-21 09:27:55 -070074
chiyotsaie46cff72020-02-05 15:03:34 -080075int av1_mv_bit_cost(const MV *mv, const MV *ref_mv, const int *mvjcost,
Yaowu Xuf883b422016-08-30 14:01:10 -070076 int *mvcost[2], int weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -070077
78// Utility to compute variance + MV rate cost for a given MV
chiyotsaie46cff72020-02-05 15:03:34 -080079int av1_get_mvpred_var(const MACROBLOCK *x, const FULLPEL_MV *best_mv,
80 const MV *ref_mv, const aom_variance_fn_ptr_t *vfp,
Jingning Hand58f3772019-12-05 15:52:36 -080081 int use_var);
chiyotsaie46cff72020-02-05 15:03:34 -080082int av1_get_mvpred_av_var(const MACROBLOCK *x, const FULLPEL_MV *best_mv,
83 const MV *ref_mv, const uint8_t *second_pred,
Jingning Han7fb39672019-11-20 22:46:43 -080084 const aom_variance_fn_ptr_t *vfp,
85 const struct buf_2d *src, const struct buf_2d *pre,
86 int use_mvcost);
chiyotsaie46cff72020-02-05 15:03:34 -080087int av1_get_mvpred_mask_var(const MACROBLOCK *x, const FULLPEL_MV *best_mv,
88 const MV *ref_mv, const uint8_t *second_pred,
David Barkerc155e012017-05-11 13:54:54 +010089 const uint8_t *mask, int mask_stride,
90 int invert_mask, const aom_variance_fn_ptr_t *vfp,
Jingning Han7fb39672019-11-20 22:46:43 -080091 const struct buf_2d *src, const struct buf_2d *pre,
David Barkerc155e012017-05-11 13:54:54 +010092 int use_mvcost);
Yaowu Xuc27fc142016-08-22 16:08:15 -070093
Yaowu Xuf883b422016-08-30 14:01:10 -070094struct AV1_COMP;
Yaowu Xuc27fc142016-08-22 16:08:15 -070095struct SPEED_FEATURES;
96
Yaowu Xuf883b422016-08-30 14:01:10 -070097int av1_init_search_range(int size);
Yaowu Xuc27fc142016-08-22 16:08:15 -070098
kyslov9de0d282019-02-27 16:14:08 -080099unsigned int av1_int_pro_motion_estimation(const struct AV1_COMP *cpi,
100 MACROBLOCK *x, BLOCK_SIZE bsize,
101 int mi_row, int mi_col,
102 const MV *ref_mv);
103
Yunqing Wange25fdbb2018-03-16 16:36:32 -0700104// Runs sequence of diamond searches in smaller steps for RD.
chiyotsaie46cff72020-02-05 15:03:34 -0800105int av1_hex_search(MACROBLOCK *x, FULLPEL_MV *start_mv, int search_param,
Yaowu Xuf883b422016-08-30 14:01:10 -0700106 int sad_per_bit, int do_init_search, int *cost_list,
107 const aom_variance_fn_ptr_t *vfp, int use_mvcost,
chiyotsaie46cff72020-02-05 15:03:34 -0800108 const MV *ref_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700109
110typedef int(fractional_mv_step_fp)(
Urvang Joshi52b62992018-02-02 14:43:07 -0800111 MACROBLOCK *x, const AV1_COMMON *const cm, int mi_row, int mi_col,
112 const MV *ref_mv, int allow_hp, int error_per_bit,
Yaowu Xuf883b422016-08-30 14:01:10 -0700113 const aom_variance_fn_ptr_t *vfp,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114 int forced_stop, // 0 - full, 1 - qtr only, 2 - half only
115 int iters_per_step, int *cost_list, int *mvjcost, int *mvcost[2],
David Barkerc155e012017-05-11 13:54:54 +0100116 int *distortion, unsigned int *sse1, const uint8_t *second_pred,
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200117 const uint8_t *mask, int mask_stride, int invert_mask, int w, int h,
Venkate846e2b2018-09-21 07:31:14 +0530118 int use_accurate_subpel_search, const int do_reset_fractional_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119
Yaowu Xuf883b422016-08-30 14:01:10 -0700120extern fractional_mv_step_fp av1_find_best_sub_pixel_tree;
121extern fractional_mv_step_fp av1_find_best_sub_pixel_tree_pruned;
122extern fractional_mv_step_fp av1_find_best_sub_pixel_tree_pruned_more;
123extern fractional_mv_step_fp av1_find_best_sub_pixel_tree_pruned_evenmore;
Yunqing Wangff4fa062017-04-21 10:56:08 -0700124extern fractional_mv_step_fp av1_return_max_sub_pixel_mv;
125extern fractional_mv_step_fp av1_return_min_sub_pixel_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126
Yaowu Xuf883b422016-08-30 14:01:10 -0700127int av1_refining_search_8p_c(MACROBLOCK *x, int error_per_bit, int search_range,
128 const aom_variance_fn_ptr_t *fn_ptr,
David Barkerc155e012017-05-11 13:54:54 +0100129 const uint8_t *mask, int mask_stride,
chiyotsaie46cff72020-02-05 15:03:34 -0800130 int invert_mask, const MV *ref_mv,
Jingning Han03cbd6d2019-11-20 16:42:01 -0800131 const uint8_t *second_pred,
132 const struct buf_2d *src,
133 const struct buf_2d *pre);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134
Jingning Hanaaa34672019-11-25 14:29:26 -0800135int av1_diamond_search_sad_c(MACROBLOCK *x, const search_site_config *cfg,
chiyotsaie46cff72020-02-05 15:03:34 -0800136 FULLPEL_MV *start_mv, FULLPEL_MV *best_mv,
137 int search_param, int sad_per_bit, int *num00,
Jingning Hanaaa34672019-11-25 14:29:26 -0800138 const aom_variance_fn_ptr_t *fn_ptr,
chiyotsaie46cff72020-02-05 15:03:34 -0800139 const MV *ref_mv, uint8_t *second_pred,
Jingning Hanaaa34672019-11-25 14:29:26 -0800140 uint8_t *mask, int mask_stride, int inv_mask);
141
RogerZhoucc5d35d2017-08-07 22:20:15 -0700142int av1_full_pixel_search(const struct AV1_COMP *cpi, MACROBLOCK *x,
chiyotsaie46cff72020-02-05 15:03:34 -0800143 BLOCK_SIZE bsize, FULLPEL_MV *start_mv,
144 int step_param, int use_var, int method,
145 int run_mesh_search, int error_per_bit,
146 int *cost_list, const MV *ref_mv, int var_max, int rd,
147 int x_pos, int y_pos, int intra,
Ranjit Kumar Tulabandu5e1f7012019-10-18 19:01:46 +0530148 const search_site_config *cfg,
149 int use_intrabc_mesh_pattern);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700150
Peng Bin235d7152018-09-03 16:40:46 +0800151int av1_obmc_full_pixel_search(const struct AV1_COMP *cpi, MACROBLOCK *x,
chiyotsaie46cff72020-02-05 15:03:34 -0800152 FULLPEL_MV *start_mv, int step_param, int sadpb,
Peng Bin235d7152018-09-03 16:40:46 +0800153 int further_steps, int do_refine,
154 const aom_variance_fn_ptr_t *fn_ptr,
chiyotsaie46cff72020-02-05 15:03:34 -0800155 const MV *ref_mv, FULLPEL_MV *dst_mv,
156 int is_second, const search_site_config *cfg);
Yaowu Xuf883b422016-08-30 14:01:10 -0700157int av1_find_best_obmc_sub_pixel_tree_up(
Urvang Joshi52b62992018-02-02 14:43:07 -0800158 MACROBLOCK *x, const AV1_COMMON *const cm, int mi_row, int mi_col,
159 MV *bestmv, const MV *ref_mv, int allow_hp, int error_per_bit,
160 const aom_variance_fn_ptr_t *vfp, int forced_stop, int iters_per_step,
161 int *mvjcost, int *mvcost[2], int *distortion, unsigned int *sse1,
162 int is_second, int use_accurate_subpel_search);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700163
Yunqing Wang68f3ccd2017-05-23 14:43:54 -0700164unsigned int av1_compute_motion_cost(const struct AV1_COMP *cpi,
165 MACROBLOCK *const x, BLOCK_SIZE bsize,
Hui Sub94cd5e2019-11-06 12:05:47 -0800166 const MV *this_mv);
Yunqing Wang1bc82862017-06-28 15:49:48 -0700167unsigned int av1_refine_warped_mv(const struct AV1_COMP *cpi,
168 MACROBLOCK *const x, BLOCK_SIZE bsize,
Hui Sub94cd5e2019-11-06 12:05:47 -0800169 int *pts0, int *pts_inref0,
170 int total_samples);
Peng Bin6e2ced62018-05-08 11:34:36 +0800171
chiyotsai19a58ee2019-03-18 18:01:05 -0700172// Performs a motion search in SIMPLE_TRANSLATION mode using reference frame
173// ref. Note that this sets the offset of mbmi, so we will need to reset it
174// after calling this function.
175void av1_simple_motion_search(struct AV1_COMP *const cpi, MACROBLOCK *x,
176 int mi_row, int mi_col, BLOCK_SIZE bsize, int ref,
chiyotsaie46cff72020-02-05 15:03:34 -0800177 FULLPEL_MV start_mv, int num_planes,
178 int use_subpixel);
chiyotsai19a58ee2019-03-18 18:01:05 -0700179
180// Performs a simple motion search to calculate the sse and var of the residue
181void av1_simple_motion_sse_var(struct AV1_COMP *cpi, MACROBLOCK *x, int mi_row,
182 int mi_col, BLOCK_SIZE bsize,
chiyotsaie46cff72020-02-05 15:03:34 -0800183 const FULLPEL_MV start_mv, int use_subpixel,
chiyotsai19a58ee2019-03-18 18:01:05 -0700184 unsigned int *sse, unsigned int *var);
185
Venkate846e2b2018-09-21 07:31:14 +0530186static INLINE void av1_set_fractional_mv(int_mv *fractional_best_mv) {
187 for (int z = 0; z < 3; z++) {
188 fractional_best_mv[z].as_int = INVALID_MV;
189 }
190}
191
Yunqing Wangda213612019-06-12 08:47:03 -0700192static INLINE void set_subpel_mv_search_range(const MvLimits *mv_limits,
193 int *col_min, int *col_max,
194 int *row_min, int *row_max,
195 const MV *ref_mv) {
196 const int max_mv = MAX_FULL_PEL_VAL * 8;
197 const int minc = AOMMAX(mv_limits->col_min * 8, ref_mv->col - max_mv);
198 const int maxc = AOMMIN(mv_limits->col_max * 8, ref_mv->col + max_mv);
199 const int minr = AOMMAX(mv_limits->row_min * 8, ref_mv->row - max_mv);
200 const int maxr = AOMMIN(mv_limits->row_max * 8, ref_mv->row + max_mv);
201
202 *col_min = AOMMAX(MV_LOW + 1, minc);
203 *col_max = AOMMIN(MV_UPP - 1, maxc);
204 *row_min = AOMMAX(MV_LOW + 1, minr);
205 *row_max = AOMMIN(MV_UPP - 1, maxr);
206}
207
Peng Bin6e2ced62018-05-08 11:34:36 +0800208#ifdef __cplusplus
209} // extern "C"
210#endif
211
James Zerne1cbb132018-08-22 14:10:36 -0700212#endif // AOM_AV1_ENCODER_MCOMP_H_