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 AV1_COMMON_MV_H_
13 #define AV1_COMMON_MV_H_
14 
15 #include "av1/common/common.h"
16 #include "av1/common/common_data.h"
17 #include "aom_dsp/aom_filter.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #define INVALID_MV 0x80008000
24 
25 typedef struct mv {
26   int16_t row;
27   int16_t col;
28 } MV;
29 
30 typedef union int_mv {
31   uint32_t as_int;
32   MV as_mv;
33 } int_mv; /* facilitates faster equality tests and copies */
34 
35 typedef struct mv32 {
36   int32_t row;
37   int32_t col;
38 } MV32;
39 
40 #if CONFIG_WARPED_MOTION
41 #define WARPED_MOTION_SORT_SAMPLES 1
42 #endif  // CONFIG_WARPED_MOTION
43 
44 #if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
45 // Bits of precision used for the model
46 #define WARPEDMODEL_PREC_BITS 16
47 #define WARPEDMODEL_ROW3HOMO_PREC_BITS 16
48 
49 #define WARPEDMODEL_TRANS_CLAMP (128 << WARPEDMODEL_PREC_BITS)
50 #define WARPEDMODEL_NONDIAGAFFINE_CLAMP (1 << (WARPEDMODEL_PREC_BITS - 3))
51 #define WARPEDMODEL_ROW3HOMO_CLAMP (1 << (WARPEDMODEL_PREC_BITS - 2))
52 
53 // Bits of subpel precision for warped interpolation
54 #define WARPEDPIXEL_PREC_BITS 6
55 #define WARPEDPIXEL_PREC_SHIFTS (1 << WARPEDPIXEL_PREC_BITS)
56 
57 // Taps for ntap filter
58 #define WARPEDPIXEL_FILTER_TAPS 6
59 
60 // Precision of filter taps
61 #define WARPEDPIXEL_FILTER_BITS 7
62 
63 #define WARP_PARAM_REDUCE_BITS 6
64 
65 // Precision bits reduction after horizontal shear
66 #define HORSHEAR_REDUCE_PREC_BITS 5
67 #define VERSHEAR_REDUCE_PREC_BITS \
68   (2 * WARPEDPIXEL_FILTER_BITS - HORSHEAR_REDUCE_PREC_BITS)
69 
70 #define WARPEDDIFF_PREC_BITS (WARPEDMODEL_PREC_BITS - WARPEDPIXEL_PREC_BITS)
71 
72 /* clang-format off */
73 typedef enum {
74   IDENTITY = 0,      // identity transformation, 0-parameter
75   TRANSLATION = 1,   // translational motion 2-parameter
76   ROTZOOM = 2,       // simplified affine with rotation + zoom only, 4-parameter
77   AFFINE = 3,        // affine, 6-parameter
78   HORTRAPEZOID = 4,  // constrained homography, hor trapezoid, 6-parameter
79   VERTRAPEZOID = 5,  // constrained homography, ver trapezoid, 6-parameter
80   HOMOGRAPHY = 6,    // homography, 8-parameter
81   TRANS_TYPES = 7,
82 } TransformationType;
83 /* clang-format on */
84 
85 // Number of types used for global motion (must be >= 3 and <= TRANS_TYPES)
86 // The following can be useful:
87 // GLOBAL_TRANS_TYPES 3 - up to rotation-zoom
88 // GLOBAL_TRANS_TYPES 4 - up to affine
89 // GLOBAL_TRANS_TYPES 6 - up to hor/ver trapezoids
90 // GLOBAL_TRANS_TYPES 7 - up to full homography
91 #define GLOBAL_TRANS_TYPES 4
92 
93 #if GLOBAL_TRANS_TYPES > 4
94 // First bit indicates whether using identity or not
95 // GLOBAL_TYPE_BITS=ceiling(log2(GLOBAL_TRANS_TYPES-1)) is the
96 // number of bits needed to cover the remaining possibilities
97 #define GLOBAL_TYPE_BITS (get_msb(2 * GLOBAL_TRANS_TYPES - 3))
98 #endif  // GLOBAL_TRANS_TYPES > 4
99 
100 typedef struct {
101 #if CONFIG_GLOBAL_MOTION
102   int global_warp_allowed;
103 #endif  // CONFIG_GLOBAL_MOTION
104 #if CONFIG_WARPED_MOTION
105   int local_warp_allowed;
106 #endif  // CONFIG_WARPED_MOTION
107 } WarpTypesAllowed;
108 
109 // number of parameters used by each transformation in TransformationTypes
110 static const int trans_model_params[TRANS_TYPES] = { 0, 2, 4, 6, 6, 6, 8 };
111 
112 // The order of values in the wmmat matrix below is best described
113 // by the homography:
114 //      [x'     (m2 m3 m0   [x
115 //  z .  y'  =   m4 m5 m1 *  y
116 //       1]      m6 m7 1)    1]
117 typedef struct {
118   TransformationType wmtype;
119   int32_t wmmat[8];
120   int16_t alpha, beta, gamma, delta;
121 } WarpedMotionParams;
122 
123 /* clang-format off */
124 static const WarpedMotionParams default_warp_params = {
125   IDENTITY,
126   { 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0,
127     0 },
128   0, 0, 0, 0
129 };
130 /* clang-format on */
131 #endif  // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
132 
133 #if CONFIG_GLOBAL_MOTION
134 // The following constants describe the various precisions
135 // of different parameters in the global motion experiment.
136 //
137 // Given the general homography:
138 //      [x'     (a  b  c   [x
139 //  z .  y'  =   d  e  f *  y
140 //       1]      g  h  i)    1]
141 //
142 // Constants using the name ALPHA here are related to parameters
143 // a, b, d, e. Constants using the name TRANS are related
144 // to parameters c and f.
145 //
146 // Anything ending in PREC_BITS is the number of bits of precision
147 // to maintain when converting from double to integer.
148 //
149 // The ABS parameters are used to create an upper and lower bound
150 // for each parameter. In other words, after a parameter is integerized
151 // it is clamped between -(1 << ABS_XXX_BITS) and (1 << ABS_XXX_BITS).
152 //
153 // XXX_PREC_DIFF and XXX_DECODE_FACTOR
154 // are computed once here to prevent repetitive
155 // computation on the decoder side. These are
156 // to allow the global motion parameters to be encoded in a lower
157 // precision than the warped model precision. This means that they
158 // need to be changed to warped precision when they are decoded.
159 //
160 // XX_MIN, XX_MAX are also computed to avoid repeated computation
161 
162 #define SUBEXPFIN_K 3
163 #define GM_TRANS_PREC_BITS 6
164 #define GM_ABS_TRANS_BITS 12
165 #define GM_ABS_TRANS_ONLY_BITS (GM_ABS_TRANS_BITS - GM_TRANS_PREC_BITS + 3)
166 #define GM_TRANS_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_TRANS_PREC_BITS)
167 #define GM_TRANS_ONLY_PREC_DIFF (WARPEDMODEL_PREC_BITS - 3)
168 #define GM_TRANS_DECODE_FACTOR (1 << GM_TRANS_PREC_DIFF)
169 #define GM_TRANS_ONLY_DECODE_FACTOR (1 << GM_TRANS_ONLY_PREC_DIFF)
170 
171 #define GM_ALPHA_PREC_BITS 15
172 #define GM_ABS_ALPHA_BITS 12
173 #define GM_ALPHA_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_ALPHA_PREC_BITS)
174 #define GM_ALPHA_DECODE_FACTOR (1 << GM_ALPHA_PREC_DIFF)
175 
176 #define GM_ROW3HOMO_PREC_BITS 16
177 #define GM_ABS_ROW3HOMO_BITS 11
178 #define GM_ROW3HOMO_PREC_DIFF \
179   (WARPEDMODEL_ROW3HOMO_PREC_BITS - GM_ROW3HOMO_PREC_BITS)
180 #define GM_ROW3HOMO_DECODE_FACTOR (1 << GM_ROW3HOMO_PREC_DIFF)
181 
182 #define GM_TRANS_MAX (1 << GM_ABS_TRANS_BITS)
183 #define GM_ALPHA_MAX (1 << GM_ABS_ALPHA_BITS)
184 #define GM_ROW3HOMO_MAX (1 << GM_ABS_ROW3HOMO_BITS)
185 
186 #define GM_TRANS_MIN -GM_TRANS_MAX
187 #define GM_ALPHA_MIN -GM_ALPHA_MAX
188 #define GM_ROW3HOMO_MIN -GM_ROW3HOMO_MAX
189 
190 // Use global motion parameters for sub8x8 blocks
191 #define GLOBAL_SUB8X8_USED 0
192 
block_center_x(int mi_col,BLOCK_SIZE bs)193 static INLINE int block_center_x(int mi_col, BLOCK_SIZE bs) {
194   const int bw = block_size_wide[bs];
195   return mi_col * MI_SIZE + bw / 2 - 1;
196 }
197 
block_center_y(int mi_row,BLOCK_SIZE bs)198 static INLINE int block_center_y(int mi_row, BLOCK_SIZE bs) {
199   const int bh = block_size_high[bs];
200   return mi_row * MI_SIZE + bh / 2 - 1;
201 }
202 
convert_to_trans_prec(int allow_hp,int coor)203 static INLINE int convert_to_trans_prec(int allow_hp, int coor) {
204   if (allow_hp)
205     return ROUND_POWER_OF_TWO_SIGNED(coor, WARPEDMODEL_PREC_BITS - 3);
206   else
207     return ROUND_POWER_OF_TWO_SIGNED(coor, WARPEDMODEL_PREC_BITS - 2) * 2;
208 }
209 #if CONFIG_AMVR
integer_mv_precision(MV * mv)210 static INLINE void integer_mv_precision(MV *mv) {
211   int mod = (mv->row % 8);
212   if (mod != 0) {
213     mv->row -= mod;
214     if (abs(mod) > 4) {
215       if (mod > 0) {
216         mv->row += 8;
217       } else {
218         mv->row -= 8;
219       }
220     }
221   }
222 
223   mod = (mv->col % 8);
224   if (mod != 0) {
225     mv->col -= mod;
226     if (abs(mod) > 4) {
227       if (mod > 0) {
228         mv->col += 8;
229       } else {
230         mv->col -= 8;
231       }
232     }
233   }
234 }
235 #endif
236 // Convert a global motion vector into a motion vector at the centre of the
237 // given block.
238 //
239 // The resulting motion vector will have three fractional bits of precision. If
240 // allow_hp is zero, the bottom bit will always be zero. If CONFIG_AMVR and
241 // is_integer is true, the bottom three bits will be zero (so the motion vector
242 // represents an integer)
gm_get_motion_vector(const WarpedMotionParams * gm,int allow_hp,BLOCK_SIZE bsize,int mi_col,int mi_row,int block_idx,int is_integer)243 static INLINE int_mv gm_get_motion_vector(const WarpedMotionParams *gm,
244                                           int allow_hp, BLOCK_SIZE bsize,
245                                           int mi_col, int mi_row, int block_idx
246 #if CONFIG_AMVR
247                                           ,
248                                           int is_integer
249 #endif
250                                           ) {
251   const int unify_bsize = CONFIG_CB4X4;
252   int_mv res;
253   const int32_t *mat = gm->wmmat;
254   int x, y, tx, ty;
255 
256   if (gm->wmtype == TRANSLATION) {
257     // All global motion vectors are stored with WARPEDMODEL_PREC_BITS (16)
258     // bits of fractional precision. The offset for a translation is stored in
259     // entries 0 and 1. For translations, all but the top three (two if
260     // cm->allow_high_precision_mv is false) fractional bits are always zero.
261     //
262     // After the right shifts, there are 3 fractional bits of precision. If
263     // allow_hp is false, the bottom bit is always zero (so we don't need a
264     // call to convert_to_trans_prec here)
265     res.as_mv.row = gm->wmmat[0] >> GM_TRANS_ONLY_PREC_DIFF;
266     res.as_mv.col = gm->wmmat[1] >> GM_TRANS_ONLY_PREC_DIFF;
267     assert(IMPLIES(1 & (res.as_mv.row | res.as_mv.col), allow_hp));
268 #if CONFIG_AMVR
269     if (is_integer) {
270       integer_mv_precision(&res.as_mv);
271     }
272 #endif
273     return res;
274   }
275 
276   if (bsize >= BLOCK_8X8 || unify_bsize) {
277     x = block_center_x(mi_col, bsize);
278     y = block_center_y(mi_row, bsize);
279   } else {
280     x = block_center_x(mi_col, bsize);
281     y = block_center_y(mi_row, bsize);
282     x += (block_idx & 1) * MI_SIZE / 2;
283     y += (block_idx & 2) * MI_SIZE / 4;
284   }
285 
286   if (gm->wmtype == ROTZOOM) {
287     assert(gm->wmmat[5] == gm->wmmat[2]);
288     assert(gm->wmmat[4] == -gm->wmmat[3]);
289   }
290   if (gm->wmtype > AFFINE) {
291     int xc = (int)((int64_t)mat[2] * x + (int64_t)mat[3] * y + mat[0]);
292     int yc = (int)((int64_t)mat[4] * x + (int64_t)mat[5] * y + mat[1]);
293     const int Z = (int)((int64_t)mat[6] * x + (int64_t)mat[7] * y +
294                         (1 << WARPEDMODEL_ROW3HOMO_PREC_BITS));
295     xc *= 1 << (WARPEDMODEL_ROW3HOMO_PREC_BITS - WARPEDMODEL_PREC_BITS);
296     yc *= 1 << (WARPEDMODEL_ROW3HOMO_PREC_BITS - WARPEDMODEL_PREC_BITS);
297     xc = (int)(xc > 0 ? ((int64_t)xc + Z / 2) / Z : ((int64_t)xc - Z / 2) / Z);
298     yc = (int)(yc > 0 ? ((int64_t)yc + Z / 2) / Z : ((int64_t)yc - Z / 2) / Z);
299     tx = convert_to_trans_prec(allow_hp, xc) - (x << 3);
300     ty = convert_to_trans_prec(allow_hp, yc) - (y << 3);
301   } else {
302     const int xc =
303         (mat[2] - (1 << WARPEDMODEL_PREC_BITS)) * x + mat[3] * y + mat[0];
304     const int yc =
305         mat[4] * x + (mat[5] - (1 << WARPEDMODEL_PREC_BITS)) * y + mat[1];
306     tx = convert_to_trans_prec(allow_hp, xc);
307     ty = convert_to_trans_prec(allow_hp, yc);
308   }
309 
310   res.as_mv.row = ty;
311   res.as_mv.col = tx;
312 
313 #if CONFIG_AMVR
314   if (is_integer) {
315     integer_mv_precision(&res.as_mv);
316   }
317 #endif
318   return res;
319 }
320 
get_gmtype(const WarpedMotionParams * gm)321 static INLINE TransformationType get_gmtype(const WarpedMotionParams *gm) {
322   if (gm->wmmat[6] != 0 || gm->wmmat[7] != 0) {
323     if (!gm->wmmat[6] && !gm->wmmat[4]) return HORTRAPEZOID;
324     if (!gm->wmmat[7] && !gm->wmmat[3]) return VERTRAPEZOID;
325     return HOMOGRAPHY;
326   }
327   if (gm->wmmat[5] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[4] &&
328       gm->wmmat[2] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[3]) {
329     return ((!gm->wmmat[1] && !gm->wmmat[0]) ? IDENTITY : TRANSLATION);
330   }
331   if (gm->wmmat[2] == gm->wmmat[5] && gm->wmmat[3] == -gm->wmmat[4])
332     return ROTZOOM;
333   else
334     return AFFINE;
335 }
336 #endif  // CONFIG_GLOBAL_MOTION
337 
338 typedef struct candidate_mv {
339   int_mv this_mv;
340   int_mv comp_mv;
341   uint8_t pred_diff[2];
342   int weight;
343 } CANDIDATE_MV;
344 
is_zero_mv(const MV * mv)345 static INLINE int is_zero_mv(const MV *mv) {
346   return *((const uint32_t *)mv) == 0;
347 }
348 
is_equal_mv(const MV * a,const MV * b)349 static INLINE int is_equal_mv(const MV *a, const MV *b) {
350   return *((const uint32_t *)a) == *((const uint32_t *)b);
351 }
352 
clamp_mv(MV * mv,int min_col,int max_col,int min_row,int max_row)353 static INLINE void clamp_mv(MV *mv, int min_col, int max_col, int min_row,
354                             int max_row) {
355   mv->col = clamp(mv->col, min_col, max_col);
356   mv->row = clamp(mv->row, min_row, max_row);
357 }
358 
mv_has_subpel(const MV * mv)359 static INLINE int mv_has_subpel(const MV *mv) {
360   return (mv->row & SUBPEL_MASK) || (mv->col & SUBPEL_MASK);
361 }
362 #ifdef __cplusplus
363 }  // extern "C"
364 #endif
365 
366 #endif  // AV1_COMMON_MV_H_
367