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_diamond_search_fn_t)(
97 const MACROBLOCK *x, const search_site_config *cfg, MV *ref_mv, MV *best_mv,
98 int search_param, int sad_per_bit, int *num00,
99 const vp9_variance_fn_ptr_t *fn_ptr, const MV *center_mv);
100
101 int vp9_refining_search_8p_c(const MACROBLOCK *x, MV *ref_mv, int error_per_bit,
102 int search_range,
103 const vp9_variance_fn_ptr_t *fn_ptr,
104 const MV *center_mv, const uint8_t *second_pred);
105
106 struct VP9_COMP;
107
108 // "mvp_full" is the MV search starting point;
109 // "ref_mv" is the context reference MV;
110 // "tmp_mv" is the searched best MV.
111 int vp9_full_pixel_search(const struct VP9_COMP *const cpi,
112 const MACROBLOCK *const x, BLOCK_SIZE bsize,
113 MV *mvp_full, int step_param, int search_method,
114 int error_per_bit, int *cost_list, const MV *ref_mv,
115 MV *tmp_mv, int var_max, int rd);
116
117 void vp9_set_subpel_mv_search_range(MvLimits *subpel_mv_limits,
118 const MvLimits *umv_window_limits,
119 const MV *ref_mv);
120
121 #if CONFIG_NON_GREEDY_MV
122 struct TplDepStats;
123 int64_t vp9_refining_search_sad_new(const MACROBLOCK *x, MV *best_full_mv,
124 int lambda, int search_range,
125 const vp9_variance_fn_ptr_t *fn_ptr,
126 const int_mv *nb_full_mvs, int full_mv_num);
127
128 int vp9_full_pixel_diamond_new(const struct VP9_COMP *cpi, MACROBLOCK *x,
129 BLOCK_SIZE bsize, MV *mvp_full, int step_param,
130 int lambda, int do_refine,
131 const int_mv *nb_full_mvs, int full_mv_num,
132 MV *best_mv);
133
get_full_mv(const MV * mv)134 static INLINE MV get_full_mv(const MV *mv) {
135 MV out_mv;
136 out_mv.row = mv->row >> 3;
137 out_mv.col = mv->col >> 3;
138 return out_mv;
139 }
140 struct TplDepFrame;
141 int vp9_prepare_nb_full_mvs(const struct MotionField *motion_field, int mi_row,
142 int mi_col, int_mv *nb_full_mvs);
143
get_square_block_size(BLOCK_SIZE bsize)144 static INLINE BLOCK_SIZE get_square_block_size(BLOCK_SIZE bsize) {
145 BLOCK_SIZE square_bsize;
146 switch (bsize) {
147 case BLOCK_4X4:
148 case BLOCK_4X8:
149 case BLOCK_8X4: square_bsize = BLOCK_4X4; break;
150 case BLOCK_8X8:
151 case BLOCK_8X16:
152 case BLOCK_16X8: square_bsize = BLOCK_8X8; break;
153 case BLOCK_16X16:
154 case BLOCK_16X32:
155 case BLOCK_32X16: square_bsize = BLOCK_16X16; break;
156 case BLOCK_32X32:
157 case BLOCK_32X64:
158 case BLOCK_64X32:
159 case BLOCK_64X64: square_bsize = BLOCK_32X32; break;
160 default:
161 square_bsize = BLOCK_INVALID;
162 assert(0 && "ERROR: invalid block size");
163 break;
164 }
165 return square_bsize;
166 }
167 #endif // CONFIG_NON_GREEDY_MV
168 #ifdef __cplusplus
169 } // extern "C"
170 #endif
171
172 #endif // VPX_VP9_ENCODER_VP9_MCOMP_H_
173