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_COMMON_MV_H_
13 #define AOM_AV1_COMMON_MV_H_
14
15 #include <stdlib.h>
16
17 #include "av1/common/common.h"
18 #include "av1/common/common_data.h"
19 #include "aom_dsp/aom_filter.h"
20 #include "aom_dsp/flow_estimation/flow_estimation.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 #define INVALID_MV 0x80008000
27 #define INVALID_MV_ROW_COL -32768
28 #define GET_MV_RAWPEL(x) (((x) + 3 + ((x) >= 0)) >> 3)
29 #define GET_MV_SUBPEL(x) ((x)*8)
30
31 #define MARK_MV_INVALID(mv) \
32 do { \
33 ((int_mv *)(mv))->as_int = INVALID_MV; \
34 } while (0)
35 #define CHECK_MV_EQUAL(x, y) (((x).row == (y).row) && ((x).col == (y).col))
36
37 // The motion vector in units of full pixel
38 typedef struct fullpel_mv {
39 int16_t row;
40 int16_t col;
41 } FULLPEL_MV;
42
43 // The motion vector in units of 1/8-pel
44 typedef struct mv {
45 int16_t row;
46 int16_t col;
47 } MV;
48
49 static const MV kZeroMv = { 0, 0 };
50 static const FULLPEL_MV kZeroFullMv = { 0, 0 };
51
52 typedef union int_mv {
53 uint32_t as_int;
54 MV as_mv;
55 FULLPEL_MV as_fullmv;
56 } int_mv; /* facilitates faster equality tests and copies */
57
58 typedef struct mv32 {
59 int32_t row;
60 int32_t col;
61 } MV32;
62
63 // The mv limit for fullpel mvs
64 typedef struct {
65 int col_min;
66 int col_max;
67 int row_min;
68 int row_max;
69 } FullMvLimits;
70
71 // The mv limit for subpel mvs
72 typedef struct {
73 int col_min;
74 int col_max;
75 int row_min;
76 int row_max;
77 } SubpelMvLimits;
78
get_fullmv_from_mv(const MV * subpel_mv)79 static inline FULLPEL_MV get_fullmv_from_mv(const MV *subpel_mv) {
80 const FULLPEL_MV full_mv = { (int16_t)GET_MV_RAWPEL(subpel_mv->row),
81 (int16_t)GET_MV_RAWPEL(subpel_mv->col) };
82 return full_mv;
83 }
84
get_mv_from_fullmv(const FULLPEL_MV * full_mv)85 static inline MV get_mv_from_fullmv(const FULLPEL_MV *full_mv) {
86 const MV subpel_mv = { (int16_t)GET_MV_SUBPEL(full_mv->row),
87 (int16_t)GET_MV_SUBPEL(full_mv->col) };
88 return subpel_mv;
89 }
90
convert_fullmv_to_mv(int_mv * mv)91 static inline void convert_fullmv_to_mv(int_mv *mv) {
92 mv->as_mv = get_mv_from_fullmv(&mv->as_fullmv);
93 }
94
95 // Bits of precision used for the model
96 #define WARPEDMODEL_PREC_BITS 16
97
98 #define WARPEDMODEL_TRANS_CLAMP (128 << WARPEDMODEL_PREC_BITS)
99 #define WARPEDMODEL_NONDIAGAFFINE_CLAMP (1 << (WARPEDMODEL_PREC_BITS - 3))
100
101 // Bits of subpel precision for warped interpolation
102 #define WARPEDPIXEL_PREC_BITS 6
103 #define WARPEDPIXEL_PREC_SHIFTS (1 << WARPEDPIXEL_PREC_BITS)
104
105 #define WARP_PARAM_REDUCE_BITS 6
106
107 #define WARPEDDIFF_PREC_BITS (WARPEDMODEL_PREC_BITS - WARPEDPIXEL_PREC_BITS)
108
109 typedef struct {
110 int global_warp_allowed;
111 int local_warp_allowed;
112 } WarpTypesAllowed;
113
114 // The order of values in the wmmat matrix below is best described
115 // by the affine transformation:
116 // [x' (m2 m3 m0 [x
117 // z . y' = m4 m5 m1 * y
118 // 1] 0 0 1) 1]
119 typedef struct {
120 int32_t wmmat[MAX_PARAMDIM];
121 int16_t alpha, beta, gamma, delta;
122 TransformationType wmtype;
123 int8_t invalid;
124 } WarpedMotionParams;
125
126 /* clang-format off */
127 static const WarpedMotionParams default_warp_params = {
128 { 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0, (1 << WARPEDMODEL_PREC_BITS) },
129 0, 0, 0, 0,
130 IDENTITY,
131 0,
132 };
133 /* clang-format on */
134
135 // The following constants describe the various precisions
136 // of different parameters in the global motion experiment.
137 //
138 // Given the general homography:
139 // [x' (a b c [x
140 // z . y' = d e f * y
141 // 1] g h i) 1]
142 //
143 // Constants using the name ALPHA here are related to parameters
144 // a, b, d, e. Constants using the name TRANS are related
145 // to parameters c and f.
146 //
147 // Anything ending in PREC_BITS is the number of bits of precision
148 // to maintain when converting from double to integer.
149 //
150 // The ABS parameters are used to create an upper and lower bound
151 // for each parameter. In other words, after a parameter is integerized
152 // it is clamped between -(1 << ABS_XXX_BITS) and (1 << ABS_XXX_BITS).
153 //
154 // XXX_PREC_DIFF and XXX_DECODE_FACTOR
155 // are computed once here to prevent repetitive
156 // computation on the decoder side. These are
157 // to allow the global motion parameters to be encoded in a lower
158 // precision than the warped model precision. This means that they
159 // need to be changed to warped precision when they are decoded.
160 //
161 // XX_MIN, XX_MAX are also computed to avoid repeated computation
162
163 #define SUBEXPFIN_K 3
164 #define GM_TRANS_PREC_BITS 6
165 #define GM_ABS_TRANS_BITS 12
166 #define GM_ABS_TRANS_ONLY_BITS (GM_ABS_TRANS_BITS - GM_TRANS_PREC_BITS + 3)
167 #define GM_TRANS_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_TRANS_PREC_BITS)
168 #define GM_TRANS_ONLY_PREC_DIFF (WARPEDMODEL_PREC_BITS - 3)
169 #define GM_TRANS_DECODE_FACTOR (1 << GM_TRANS_PREC_DIFF)
170 #define GM_TRANS_ONLY_DECODE_FACTOR (1 << GM_TRANS_ONLY_PREC_DIFF)
171
172 #define GM_ALPHA_PREC_BITS 15
173 #define GM_ABS_ALPHA_BITS 12
174 #define GM_ALPHA_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_ALPHA_PREC_BITS)
175 #define GM_ALPHA_DECODE_FACTOR (1 << GM_ALPHA_PREC_DIFF)
176
177 #define GM_TRANS_MAX (1 << GM_ABS_TRANS_BITS)
178 #define GM_ALPHA_MAX (1 << GM_ABS_ALPHA_BITS)
179
180 #define GM_TRANS_MIN -GM_TRANS_MAX
181 #define GM_ALPHA_MIN -GM_ALPHA_MAX
182
block_center_x(int mi_col,BLOCK_SIZE bs)183 static inline int block_center_x(int mi_col, BLOCK_SIZE bs) {
184 const int bw = block_size_wide[bs];
185 return mi_col * MI_SIZE + bw / 2 - 1;
186 }
187
block_center_y(int mi_row,BLOCK_SIZE bs)188 static inline int block_center_y(int mi_row, BLOCK_SIZE bs) {
189 const int bh = block_size_high[bs];
190 return mi_row * MI_SIZE + bh / 2 - 1;
191 }
192
convert_to_trans_prec(int allow_hp,int coor)193 static inline int convert_to_trans_prec(int allow_hp, int coor) {
194 if (allow_hp)
195 return ROUND_POWER_OF_TWO_SIGNED(coor, WARPEDMODEL_PREC_BITS - 3);
196 else
197 return ROUND_POWER_OF_TWO_SIGNED(coor, WARPEDMODEL_PREC_BITS - 2) * 2;
198 }
integer_mv_precision(MV * mv)199 static inline void integer_mv_precision(MV *mv) {
200 int mod = (mv->row % 8);
201 if (mod != 0) {
202 mv->row -= mod;
203 if (abs(mod) > 4) {
204 if (mod > 0) {
205 mv->row += 8;
206 } else {
207 mv->row -= 8;
208 }
209 }
210 }
211
212 mod = (mv->col % 8);
213 if (mod != 0) {
214 mv->col -= mod;
215 if (abs(mod) > 4) {
216 if (mod > 0) {
217 mv->col += 8;
218 } else {
219 mv->col -= 8;
220 }
221 }
222 }
223 }
224 // Convert a global motion vector into a motion vector at the centre of the
225 // given block.
226 //
227 // The resulting motion vector will have three fractional bits of precision. If
228 // allow_hp is zero, the bottom bit will always be zero. If CONFIG_AMVR and
229 // is_integer is true, the bottom three bits will be zero (so the motion vector
230 // represents an integer)
gm_get_motion_vector(const WarpedMotionParams * gm,int allow_hp,BLOCK_SIZE bsize,int mi_col,int mi_row,int is_integer)231 static inline int_mv gm_get_motion_vector(const WarpedMotionParams *gm,
232 int allow_hp, BLOCK_SIZE bsize,
233 int mi_col, int mi_row,
234 int is_integer) {
235 int_mv res;
236
237 if (gm->wmtype == IDENTITY) {
238 res.as_int = 0;
239 return res;
240 }
241
242 const int32_t *mat = gm->wmmat;
243 int x, y, tx, ty;
244
245 if (gm->wmtype == TRANSLATION) {
246 // All global motion vectors are stored with WARPEDMODEL_PREC_BITS (16)
247 // bits of fractional precision. The offset for a translation is stored in
248 // entries 0 and 1. For translations, all but the top three (two if
249 // cm->features.allow_high_precision_mv is false) fractional bits are always
250 // zero.
251 //
252 // After the right shifts, there are 3 fractional bits of precision. If
253 // allow_hp is false, the bottom bit is always zero (so we don't need a
254 // call to convert_to_trans_prec here)
255 //
256 // Note: There is an AV1 specification bug here:
257 //
258 // gm->wmmat[0] is supposed to be the horizontal translation, and so should
259 // go into res.as_mv.col, and gm->wmmat[1] is supposed to be the vertical
260 // translation and so should go into res.as_mv.row
261 //
262 // However, in the spec, these assignments are accidentally reversed, and so
263 // we must keep this incorrect logic to match the spec.
264 //
265 // See also: https://crbug.com/aomedia/3328
266 res.as_mv.row = gm->wmmat[0] >> GM_TRANS_ONLY_PREC_DIFF;
267 res.as_mv.col = gm->wmmat[1] >> GM_TRANS_ONLY_PREC_DIFF;
268 assert(IMPLIES(1 & (res.as_mv.row | res.as_mv.col), allow_hp));
269 if (is_integer) {
270 integer_mv_precision(&res.as_mv);
271 }
272 return res;
273 }
274
275 x = block_center_x(mi_col, bsize);
276 y = block_center_y(mi_row, bsize);
277
278 if (gm->wmtype == ROTZOOM) {
279 assert(gm->wmmat[5] == gm->wmmat[2]);
280 assert(gm->wmmat[4] == -gm->wmmat[3]);
281 }
282
283 const int xc =
284 (mat[2] - (1 << WARPEDMODEL_PREC_BITS)) * x + mat[3] * y + mat[0];
285 const int yc =
286 mat[4] * x + (mat[5] - (1 << WARPEDMODEL_PREC_BITS)) * y + mat[1];
287 tx = convert_to_trans_prec(allow_hp, xc);
288 ty = convert_to_trans_prec(allow_hp, yc);
289
290 res.as_mv.row = ty;
291 res.as_mv.col = tx;
292
293 if (is_integer) {
294 integer_mv_precision(&res.as_mv);
295 }
296 return res;
297 }
298
get_wmtype(const WarpedMotionParams * gm)299 static inline TransformationType get_wmtype(const WarpedMotionParams *gm) {
300 if (gm->wmmat[5] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[4] &&
301 gm->wmmat[2] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[3]) {
302 return ((!gm->wmmat[1] && !gm->wmmat[0]) ? IDENTITY : TRANSLATION);
303 }
304 if (gm->wmmat[2] == gm->wmmat[5] && gm->wmmat[3] == -gm->wmmat[4])
305 return ROTZOOM;
306 else
307 return AFFINE;
308 }
309
310 typedef struct candidate_mv {
311 int_mv this_mv;
312 int_mv comp_mv;
313 } CANDIDATE_MV;
314
is_zero_mv(const MV * mv)315 static inline int is_zero_mv(const MV *mv) {
316 return *((const uint32_t *)mv) == 0;
317 }
318
is_equal_mv(const MV * a,const MV * b)319 static inline int is_equal_mv(const MV *a, const MV *b) {
320 return *((const uint32_t *)a) == *((const uint32_t *)b);
321 }
322
clamp_mv(MV * mv,const SubpelMvLimits * mv_limits)323 static inline void clamp_mv(MV *mv, const SubpelMvLimits *mv_limits) {
324 mv->col = clamp(mv->col, mv_limits->col_min, mv_limits->col_max);
325 mv->row = clamp(mv->row, mv_limits->row_min, mv_limits->row_max);
326 }
327
clamp_fullmv(FULLPEL_MV * mv,const FullMvLimits * mv_limits)328 static inline void clamp_fullmv(FULLPEL_MV *mv, const FullMvLimits *mv_limits) {
329 mv->col = clamp(mv->col, mv_limits->col_min, mv_limits->col_max);
330 mv->row = clamp(mv->row, mv_limits->row_min, mv_limits->row_max);
331 }
332
333 #ifdef __cplusplus
334 } // extern "C"
335 #endif
336
337 #endif // AOM_AV1_COMMON_MV_H_
338