1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #ifndef AOM_AV1_ENCODER_MCOMP_H_
13 #define AOM_AV1_ENCODER_MCOMP_H_
14 
15 #include "av1/encoder/block.h"
16 #include "aom_dsp/variance.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 // The maximum number of steps in a step search given the largest
23 // allowed initial step
24 #define MAX_MVSEARCH_STEPS 11
25 // Max full pel mv specified in the unit of full pixel
26 // Enable the use of motion vector in range [-1023, 1023].
27 #define MAX_FULL_PEL_VAL ((1 << (MAX_MVSEARCH_STEPS - 1)) - 1)
28 // Maximum size of the first step in full pel units
29 #define MAX_FIRST_STEP (1 << (MAX_MVSEARCH_STEPS - 1))
30 // Allowed motion vector pixel distance outside image border
31 // for Block_16x16
32 #define BORDER_MV_PIXELS_B16 (16 + AOM_INTERP_EXTEND)
33 
34 #define SEARCH_RANGE_8P 3
35 #define SEARCH_GRID_STRIDE_8P (2 * SEARCH_RANGE_8P + 1)
36 #define SEARCH_GRID_CENTER_8P \
37   (SEARCH_RANGE_8P * SEARCH_GRID_STRIDE_8P + SEARCH_RANGE_8P)
38 
39 // motion search site
40 typedef struct search_site {
41   MV mv;
42   int offset;
43 } search_site;
44 
45 typedef struct search_site_config {
46   search_site ss[8 * MAX_MVSEARCH_STEPS + 1];
47   int ss_count;
48   int searches_per_step;
49 } search_site_config;
50 
51 typedef struct {
52   MV coord;
53   int coord_offset;
54 } search_neighbors;
55 
56 void av1_init_dsmotion_compensation(search_site_config *cfg, int stride);
57 void av1_init3smotion_compensation(search_site_config *cfg, int stride);
58 
59 void av1_set_mv_search_range(MvLimits *mv_limits, const MV *mv);
60 
61 int av1_mv_bit_cost(const MV *mv, const MV *ref, const int *mvjcost,
62                     int *mvcost[2], int weight);
63 
64 // Utility to compute variance + MV rate cost for a given MV
65 int av1_get_mvpred_var(const MACROBLOCK *x, const MV *best_mv,
66                        const MV *center_mv, const aom_variance_fn_ptr_t *vfp,
67                        int use_mvcost);
68 int av1_get_mvpred_av_var(const MACROBLOCK *x, const MV *best_mv,
69                           const MV *center_mv, const uint8_t *second_pred,
70                           const aom_variance_fn_ptr_t *vfp, int use_mvcost);
71 int av1_get_mvpred_mask_var(const MACROBLOCK *x, const MV *best_mv,
72                             const MV *center_mv, const uint8_t *second_pred,
73                             const uint8_t *mask, int mask_stride,
74                             int invert_mask, const aom_variance_fn_ptr_t *vfp,
75                             int use_mvcost);
76 
77 struct AV1_COMP;
78 struct SPEED_FEATURES;
79 
80 int av1_init_search_range(int size);
81 
82 int av1_refining_search_sad(struct macroblock *x, MV *ref_mv, int sad_per_bit,
83                             int distance, const aom_variance_fn_ptr_t *fn_ptr,
84                             const MV *center_mv);
85 
86 // Runs sequence of diamond searches in smaller steps for RD.
87 int av1_full_pixel_diamond(const struct AV1_COMP *cpi, MACROBLOCK *x,
88                            MV *mvp_full, int step_param, int sadpb,
89                            int further_steps, int do_refine, int *cost_list,
90                            const aom_variance_fn_ptr_t *fn_ptr,
91                            const MV *ref_mv, MV *dst_mv);
92 
93 int av1_hex_search(MACROBLOCK *x, MV *start_mv, int search_param,
94                    int sad_per_bit, int do_init_search, int *cost_list,
95                    const aom_variance_fn_ptr_t *vfp, int use_mvcost,
96                    const MV *center_mv);
97 
98 typedef int(fractional_mv_step_fp)(
99     MACROBLOCK *x, const AV1_COMMON *const cm, int mi_row, int mi_col,
100     const MV *ref_mv, int allow_hp, int error_per_bit,
101     const aom_variance_fn_ptr_t *vfp,
102     int forced_stop,  // 0 - full, 1 - qtr only, 2 - half only
103     int iters_per_step, int *cost_list, int *mvjcost, int *mvcost[2],
104     int *distortion, unsigned int *sse1, const uint8_t *second_pred,
105     const uint8_t *mask, int mask_stride, int invert_mask, int w, int h,
106     int use_accurate_subpel_search);
107 
108 extern fractional_mv_step_fp av1_find_best_sub_pixel_tree;
109 extern fractional_mv_step_fp av1_find_best_sub_pixel_tree_pruned;
110 extern fractional_mv_step_fp av1_find_best_sub_pixel_tree_pruned_more;
111 extern fractional_mv_step_fp av1_find_best_sub_pixel_tree_pruned_evenmore;
112 extern fractional_mv_step_fp av1_return_max_sub_pixel_mv;
113 extern fractional_mv_step_fp av1_return_min_sub_pixel_mv;
114 
115 typedef int (*av1_full_search_fn_t)(const MACROBLOCK *x, const MV *ref_mv,
116                                     int sad_per_bit, int distance,
117                                     const aom_variance_fn_ptr_t *fn_ptr,
118                                     const MV *center_mv, MV *best_mv);
119 
120 typedef int (*av1_diamond_search_fn_t)(
121     MACROBLOCK *x, const search_site_config *cfg, MV *ref_mv, MV *best_mv,
122     int search_param, int sad_per_bit, int *num00,
123     const aom_variance_fn_ptr_t *fn_ptr, const MV *center_mv);
124 
125 int av1_refining_search_8p_c(MACROBLOCK *x, int error_per_bit, int search_range,
126                              const aom_variance_fn_ptr_t *fn_ptr,
127                              const uint8_t *mask, int mask_stride,
128                              int invert_mask, const MV *center_mv,
129                              const uint8_t *second_pred);
130 
131 int av1_full_pixel_search(const struct AV1_COMP *cpi, MACROBLOCK *x,
132                           BLOCK_SIZE bsize, MV *mvp_full, int step_param,
133                           int method, int run_mesh_search, int error_per_bit,
134                           int *cost_list, const MV *ref_mv, int var_max, int rd,
135                           int x_pos, int y_pos, int intra);
136 
137 int av1_obmc_full_pixel_search(const struct AV1_COMP *cpi, MACROBLOCK *x,
138                                MV *mvp_full, int step_param, int sadpb,
139                                int further_steps, int do_refine,
140                                const aom_variance_fn_ptr_t *fn_ptr,
141                                const MV *ref_mv, MV *dst_mv, int is_second);
142 int av1_find_best_obmc_sub_pixel_tree_up(
143     MACROBLOCK *x, const AV1_COMMON *const cm, int mi_row, int mi_col,
144     MV *bestmv, const MV *ref_mv, int allow_hp, int error_per_bit,
145     const aom_variance_fn_ptr_t *vfp, int forced_stop, int iters_per_step,
146     int *mvjcost, int *mvcost[2], int *distortion, unsigned int *sse1,
147     int is_second, int use_accurate_subpel_search);
148 
149 unsigned int av1_compute_motion_cost(const struct AV1_COMP *cpi,
150                                      MACROBLOCK *const x, BLOCK_SIZE bsize,
151                                      int mi_row, int mi_col, const MV *this_mv);
152 unsigned int av1_refine_warped_mv(const struct AV1_COMP *cpi,
153                                   MACROBLOCK *const x, BLOCK_SIZE bsize,
154                                   int mi_row, int mi_col, int *pts0,
155                                   int *pts_inref0, int total_samples);
156 
157 #ifdef __cplusplus
158 }  // extern "C"
159 #endif
160 
161 #endif  // AOM_AV1_ENCODER_MCOMP_H_
162