xref: /aosp_15_r20/external/libvpx/vp9/encoder/vp9_mcomp.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef VPX_VP9_ENCODER_VP9_MCOMP_H_
12 #define VPX_VP9_ENCODER_VP9_MCOMP_H_
13 
14 #include "vp9/encoder/vp9_block.h"
15 #if CONFIG_NON_GREEDY_MV
16 #include "vp9/encoder/vp9_non_greedy_mv.h"
17 #endif  // CONFIG_NON_GREEDY_MV
18 #include "vpx_dsp/variance.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 // The maximum number of steps in a step search given the largest
25 // allowed initial step
26 #define MAX_MVSEARCH_STEPS 11
27 // Max full pel mv specified in the unit of full pixel
28 // Enable the use of motion vector in range [-1023, 1023].
29 #define MAX_FULL_PEL_VAL ((1 << (MAX_MVSEARCH_STEPS - 1)) - 1)
30 // Maximum size of the first step in full pel units
31 #define MAX_FIRST_STEP (1 << (MAX_MVSEARCH_STEPS - 1))
32 // Allowed motion vector pixel distance outside image border
33 // for Block_16x16
34 #define BORDER_MV_PIXELS_B16 (16 + VP9_INTERP_EXTEND)
35 
36 typedef struct search_site_config {
37   // motion search sites
38   MV ss_mv[8 * MAX_MVSEARCH_STEPS];        // Motion vector
39   intptr_t ss_os[8 * MAX_MVSEARCH_STEPS];  // Offset
40   int searches_per_step;
41   int total_steps;
42 } search_site_config;
43 
44 typedef struct vp9_sad_table {
45   vpx_sad_fn_t sdf;
46   vpx_sad_multi_d_fn_t sdx4df;
47 } vp9_sad_fn_ptr_t;
48 
get_buf_from_mv(const struct buf_2d * buf,const MV * mv)49 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
50                                              const MV *mv) {
51   return &buf->buf[mv->row * buf->stride + mv->col];
52 }
53 
54 void vp9_init_dsmotion_compensation(search_site_config *cfg, int stride);
55 void vp9_init3smotion_compensation(search_site_config *cfg, int stride);
56 
57 void vp9_set_mv_search_range(MvLimits *mv_limits, const MV *mv);
58 int vp9_mv_bit_cost(const MV *mv, const MV *ref, const int *mvjcost,
59                     int *mvcost[2], int weight);
60 
61 // Utility to compute variance + MV rate cost for a given MV
62 int vp9_get_mvpred_var(const MACROBLOCK *x, const MV *best_mv,
63                        const MV *center_mv, const vp9_variance_fn_ptr_t *vfp,
64                        int use_mvcost);
65 int vp9_get_mvpred_av_var(const MACROBLOCK *x, const MV *best_mv,
66                           const MV *center_mv, const uint8_t *second_pred,
67                           const vp9_variance_fn_ptr_t *vfp, int use_mvcost);
68 
69 struct VP9_COMP;
70 struct SPEED_FEATURES;
71 struct vp9_sad_table;
72 
73 int vp9_init_search_range(int size);
74 
75 int vp9_refining_search_sad(const struct macroblock *x, struct mv *ref_mv,
76                             int error_per_bit, int search_range,
77                             const struct vp9_sad_table *sad_fn_ptr,
78                             const struct mv *center_mv);
79 
80 // Perform integral projection based motion estimation.
81 unsigned int vp9_int_pro_motion_estimation(const struct VP9_COMP *cpi,
82                                            MACROBLOCK *x, BLOCK_SIZE bsize,
83                                            int mi_row, int mi_col,
84                                            const MV *ref_mv);
85 
86 typedef uint32_t(fractional_mv_step_fp)(
87     const MACROBLOCK *x, MV *bestmv, const MV *ref_mv, int allow_hp,
88     int error_per_bit, const vp9_variance_fn_ptr_t *vfp,
89     int forced_stop,  // 0 - full, 1 - qtr only, 2 - half only
90     int iters_per_step, int *cost_list, int *mvjcost, int *mvcost[2],
91     uint32_t *distortion, uint32_t *sse1, const uint8_t *second_pred, int w,
92     int h, int use_accurate_subpel_search);
93 
94 extern fractional_mv_step_fp vp9_find_best_sub_pixel_tree;
95 extern fractional_mv_step_fp vp9_find_best_sub_pixel_tree_pruned;
96 extern fractional_mv_step_fp vp9_find_best_sub_pixel_tree_pruned_more;
97 extern fractional_mv_step_fp vp9_find_best_sub_pixel_tree_pruned_evenmore;
98 extern fractional_mv_step_fp vp9_skip_sub_pixel_tree;
99 extern fractional_mv_step_fp vp9_return_max_sub_pixel_mv;
100 extern fractional_mv_step_fp vp9_return_min_sub_pixel_mv;
101 
102 typedef int (*vp9_diamond_search_fn_t)(
103     const MACROBLOCK *x, const search_site_config *cfg, MV *ref_mv,
104     uint32_t start_mv_sad, MV *best_mv, int search_param, int sad_per_bit,
105     int *num00, const vp9_sad_fn_ptr_t *sad_fn_ptr, const MV *center_mv);
106 
107 int vp9_refining_search_8p_c(const MACROBLOCK *x, MV *ref_mv, int error_per_bit,
108                              int search_range,
109                              const vp9_variance_fn_ptr_t *fn_ptr,
110                              const MV *center_mv, const uint8_t *second_pred);
111 
112 struct VP9_COMP;
113 
114 // "mvp_full" is the MV search starting point;
115 // "ref_mv" is the context reference MV;
116 // "tmp_mv" is the searched best MV.
117 int vp9_full_pixel_search(const struct VP9_COMP *const cpi,
118                           const MACROBLOCK *const x, BLOCK_SIZE bsize,
119                           MV *mvp_full, int step_param, int search_method,
120                           int error_per_bit, int *cost_list, const MV *ref_mv,
121                           MV *tmp_mv, int var_max, int rd);
122 
123 void vp9_set_subpel_mv_search_range(MvLimits *subpel_mv_limits,
124                                     const MvLimits *umv_window_limits,
125                                     const MV *ref_mv);
126 
127 #if CONFIG_NON_GREEDY_MV
128 struct TplDepStats;
129 int64_t vp9_refining_search_sad_new(const MACROBLOCK *x, MV *best_full_mv,
130                                     int lambda, int search_range,
131                                     const vp9_variance_fn_ptr_t *fn_ptr,
132                                     const int_mv *nb_full_mvs, int full_mv_num);
133 
134 int vp9_full_pixel_diamond_new(const struct VP9_COMP *cpi, MACROBLOCK *x,
135                                BLOCK_SIZE bsize, MV *mvp_full, int step_param,
136                                int lambda, int do_refine,
137                                const int_mv *nb_full_mvs, int full_mv_num,
138                                MV *best_mv);
139 
get_full_mv(const MV * mv)140 static INLINE MV get_full_mv(const MV *mv) {
141   MV out_mv;
142   out_mv.row = mv->row >> 3;
143   out_mv.col = mv->col >> 3;
144   return out_mv;
145 }
146 struct TplDepFrame;
147 int vp9_prepare_nb_full_mvs(const struct MotionField *motion_field, int mi_row,
148                             int mi_col, int_mv *nb_full_mvs);
149 
get_square_block_size(BLOCK_SIZE bsize)150 static INLINE BLOCK_SIZE get_square_block_size(BLOCK_SIZE bsize) {
151   BLOCK_SIZE square_bsize;
152   switch (bsize) {
153     case BLOCK_4X4:
154     case BLOCK_4X8:
155     case BLOCK_8X4: square_bsize = BLOCK_4X4; break;
156     case BLOCK_8X8:
157     case BLOCK_8X16:
158     case BLOCK_16X8: square_bsize = BLOCK_8X8; break;
159     case BLOCK_16X16:
160     case BLOCK_16X32:
161     case BLOCK_32X16: square_bsize = BLOCK_16X16; break;
162     case BLOCK_32X32:
163     case BLOCK_32X64:
164     case BLOCK_64X32:
165     case BLOCK_64X64: square_bsize = BLOCK_32X32; break;
166     default:
167       square_bsize = BLOCK_INVALID;
168       assert(0 && "ERROR: invalid block size");
169       break;
170   }
171   return square_bsize;
172 }
173 #endif  // CONFIG_NON_GREEDY_MV
174 #ifdef __cplusplus
175 }  // extern "C"
176 #endif
177 
178 #endif  // VPX_VP9_ENCODER_VP9_MCOMP_H_
179