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 
get_buf_from_mv(const struct buf_2d * buf,const MV * mv)44 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
45                                              const MV *mv) {
46   return &buf->buf[mv->row * buf->stride + mv->col];
47 }
48 
49 void vp9_init_dsmotion_compensation(search_site_config *cfg, int stride);
50 void vp9_init3smotion_compensation(search_site_config *cfg, int stride);
51 
52 void vp9_set_mv_search_range(MvLimits *mv_limits, const MV *mv);
53 int vp9_mv_bit_cost(const MV *mv, const MV *ref, const int *mvjcost,
54                     int *mvcost[2], int weight);
55 
56 // Utility to compute variance + MV rate cost for a given MV
57 int vp9_get_mvpred_var(const MACROBLOCK *x, const MV *best_mv,
58                        const MV *center_mv, const vp9_variance_fn_ptr_t *vfp,
59                        int use_mvcost);
60 int vp9_get_mvpred_av_var(const MACROBLOCK *x, const MV *best_mv,
61                           const MV *center_mv, const uint8_t *second_pred,
62                           const vp9_variance_fn_ptr_t *vfp, int use_mvcost);
63 
64 struct VP9_COMP;
65 struct SPEED_FEATURES;
66 
67 int vp9_init_search_range(int size);
68 
69 int vp9_refining_search_sad(const struct macroblock *x, struct mv *ref_mv,
70                             int error_per_bit, int search_range,
71                             const struct vp9_variance_vtable *fn_ptr,
72                             const struct mv *center_mv);
73 
74 // Perform integral projection based motion estimation.
75 unsigned int vp9_int_pro_motion_estimation(const struct VP9_COMP *cpi,
76                                            MACROBLOCK *x, BLOCK_SIZE bsize,
77                                            int mi_row, int mi_col,
78                                            const MV *ref_mv);
79 
80 typedef uint32_t(fractional_mv_step_fp)(
81     const MACROBLOCK *x, MV *bestmv, const MV *ref_mv, int allow_hp,
82     int error_per_bit, const vp9_variance_fn_ptr_t *vfp,
83     int forced_stop,  // 0 - full, 1 - qtr only, 2 - half only
84     int iters_per_step, int *cost_list, int *mvjcost, int *mvcost[2],
85     uint32_t *distortion, uint32_t *sse1, const uint8_t *second_pred, int w,
86     int h, int use_accurate_subpel_search);
87 
88 extern fractional_mv_step_fp vp9_find_best_sub_pixel_tree;
89 extern fractional_mv_step_fp vp9_find_best_sub_pixel_tree_pruned;
90 extern fractional_mv_step_fp vp9_find_best_sub_pixel_tree_pruned_more;
91 extern fractional_mv_step_fp vp9_find_best_sub_pixel_tree_pruned_evenmore;
92 extern fractional_mv_step_fp vp9_skip_sub_pixel_tree;
93 extern fractional_mv_step_fp vp9_return_max_sub_pixel_mv;
94 extern fractional_mv_step_fp vp9_return_min_sub_pixel_mv;
95 
96 typedef int (*vp9_full_search_fn_t)(const MACROBLOCK *x, const MV *ref_mv,
97                                     int sad_per_bit, int distance,
98                                     const vp9_variance_fn_ptr_t *fn_ptr,
99                                     const MV *center_mv, MV *best_mv);
100 
101 typedef int (*vp9_refining_search_fn_t)(const MACROBLOCK *x, MV *ref_mv,
102                                         int sad_per_bit, int distance,
103                                         const vp9_variance_fn_ptr_t *fn_ptr,
104                                         const MV *center_mv);
105 
106 typedef int (*vp9_diamond_search_fn_t)(
107     const MACROBLOCK *x, const search_site_config *cfg, MV *ref_mv, MV *best_mv,
108     int search_param, int sad_per_bit, int *num00,
109     const vp9_variance_fn_ptr_t *fn_ptr, const MV *center_mv);
110 
111 int vp9_refining_search_8p_c(const MACROBLOCK *x, MV *ref_mv, int error_per_bit,
112                              int search_range,
113                              const vp9_variance_fn_ptr_t *fn_ptr,
114                              const MV *center_mv, const uint8_t *second_pred);
115 
116 struct VP9_COMP;
117 
118 // "mvp_full" is the MV search starting point;
119 // "ref_mv" is the context reference MV;
120 // "tmp_mv" is the searched best MV.
121 int vp9_full_pixel_search(const struct VP9_COMP *const cpi,
122                           const MACROBLOCK *const x, BLOCK_SIZE bsize,
123                           MV *mvp_full, int step_param, int search_method,
124                           int error_per_bit, int *cost_list, const MV *ref_mv,
125                           MV *tmp_mv, int var_max, int rd);
126 
127 void vp9_set_subpel_mv_search_range(MvLimits *subpel_mv_limits,
128                                     const MvLimits *umv_window_limits,
129                                     const MV *ref_mv);
130 
131 #if CONFIG_NON_GREEDY_MV
132 struct TplDepStats;
133 int64_t vp9_refining_search_sad_new(const MACROBLOCK *x, MV *best_full_mv,
134                                     int lambda, int search_range,
135                                     const vp9_variance_fn_ptr_t *fn_ptr,
136                                     const int_mv *nb_full_mvs, int full_mv_num);
137 
138 int vp9_full_pixel_diamond_new(const struct VP9_COMP *cpi, MACROBLOCK *x,
139                                BLOCK_SIZE bsize, MV *mvp_full, int step_param,
140                                int lambda, int do_refine,
141                                const int_mv *nb_full_mvs, int full_mv_num,
142                                MV *best_mv);
143 
get_full_mv(const MV * mv)144 static INLINE MV get_full_mv(const MV *mv) {
145   MV out_mv;
146   out_mv.row = mv->row >> 3;
147   out_mv.col = mv->col >> 3;
148   return out_mv;
149 }
150 struct TplDepFrame;
151 int vp9_prepare_nb_full_mvs(const struct MotionField *motion_field, int mi_row,
152                             int mi_col, int_mv *nb_full_mvs);
153 
get_square_block_size(BLOCK_SIZE bsize)154 static INLINE BLOCK_SIZE get_square_block_size(BLOCK_SIZE bsize) {
155   BLOCK_SIZE square_bsize;
156   switch (bsize) {
157     case BLOCK_4X4:
158     case BLOCK_4X8:
159     case BLOCK_8X4: square_bsize = BLOCK_4X4; break;
160     case BLOCK_8X8:
161     case BLOCK_8X16:
162     case BLOCK_16X8: square_bsize = BLOCK_8X8; break;
163     case BLOCK_16X16:
164     case BLOCK_16X32:
165     case BLOCK_32X16: square_bsize = BLOCK_16X16; break;
166     case BLOCK_32X32:
167     case BLOCK_32X64:
168     case BLOCK_64X32:
169     case BLOCK_64X64: square_bsize = BLOCK_32X32; break;
170     default:
171       square_bsize = BLOCK_INVALID;
172       assert(0 && "ERROR: invalid block size");
173       break;
174   }
175   return square_bsize;
176 }
177 #endif  // CONFIG_NON_GREEDY_MV
178 #ifdef __cplusplus
179 }  // extern "C"
180 #endif
181 
182 #endif  // VPX_VP9_ENCODER_VP9_MCOMP_H_
183