• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AOM_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 AOM_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 AOM_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 #define WARPEDMODEL_ROW3HOMO_PREC_BITS 16
98 
99 #define WARPEDMODEL_TRANS_CLAMP (128 << WARPEDMODEL_PREC_BITS)
100 #define WARPEDMODEL_NONDIAGAFFINE_CLAMP (1 << (WARPEDMODEL_PREC_BITS - 3))
101 #define WARPEDMODEL_ROW3HOMO_CLAMP (1 << (WARPEDMODEL_PREC_BITS - 2))
102 
103 // Bits of subpel precision for warped interpolation
104 #define WARPEDPIXEL_PREC_BITS 6
105 #define WARPEDPIXEL_PREC_SHIFTS (1 << WARPEDPIXEL_PREC_BITS)
106 
107 #define WARP_PARAM_REDUCE_BITS 6
108 
109 #define WARPEDDIFF_PREC_BITS (WARPEDMODEL_PREC_BITS - WARPEDPIXEL_PREC_BITS)
110 
111 // Number of types used for global motion (must be >= 3 and <= TRANS_TYPES)
112 // The following can be useful:
113 // GLOBAL_TRANS_TYPES 3 - up to rotation-zoom
114 // GLOBAL_TRANS_TYPES 4 - up to affine
115 // GLOBAL_TRANS_TYPES 6 - up to hor/ver trapezoids
116 // GLOBAL_TRANS_TYPES 7 - up to full homography
117 #define GLOBAL_TRANS_TYPES 4
118 
119 typedef struct {
120   int global_warp_allowed;
121   int local_warp_allowed;
122 } WarpTypesAllowed;
123 
124 // The order of values in the wmmat matrix below is best described
125 // by the homography:
126 //      [x'     (m2 m3 m0   [x
127 //  z .  y'  =   m4 m5 m1 *  y
128 //       1]      m6 m7 1)    1]
129 typedef struct {
130   int32_t wmmat[6];
131   int16_t alpha, beta, gamma, delta;
132   TransformationType wmtype;
133   int8_t invalid;
134 } WarpedMotionParams;
135 
136 /* clang-format off */
137 static const WarpedMotionParams default_warp_params = {
138   { 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0, (1 << WARPEDMODEL_PREC_BITS) },
139   0, 0, 0, 0,
140   IDENTITY,
141   0,
142 };
143 /* clang-format on */
144 
145 // The following constants describe the various precisions
146 // of different parameters in the global motion experiment.
147 //
148 // Given the general homography:
149 //      [x'     (a  b  c   [x
150 //  z .  y'  =   d  e  f *  y
151 //       1]      g  h  i)    1]
152 //
153 // Constants using the name ALPHA here are related to parameters
154 // a, b, d, e. Constants using the name TRANS are related
155 // to parameters c and f.
156 //
157 // Anything ending in PREC_BITS is the number of bits of precision
158 // to maintain when converting from double to integer.
159 //
160 // The ABS parameters are used to create an upper and lower bound
161 // for each parameter. In other words, after a parameter is integerized
162 // it is clamped between -(1 << ABS_XXX_BITS) and (1 << ABS_XXX_BITS).
163 //
164 // XXX_PREC_DIFF and XXX_DECODE_FACTOR
165 // are computed once here to prevent repetitive
166 // computation on the decoder side. These are
167 // to allow the global motion parameters to be encoded in a lower
168 // precision than the warped model precision. This means that they
169 // need to be changed to warped precision when they are decoded.
170 //
171 // XX_MIN, XX_MAX are also computed to avoid repeated computation
172 
173 #define SUBEXPFIN_K 3
174 #define GM_TRANS_PREC_BITS 6
175 #define GM_ABS_TRANS_BITS 12
176 #define GM_ABS_TRANS_ONLY_BITS (GM_ABS_TRANS_BITS - GM_TRANS_PREC_BITS + 3)
177 #define GM_TRANS_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_TRANS_PREC_BITS)
178 #define GM_TRANS_ONLY_PREC_DIFF (WARPEDMODEL_PREC_BITS - 3)
179 #define GM_TRANS_DECODE_FACTOR (1 << GM_TRANS_PREC_DIFF)
180 #define GM_TRANS_ONLY_DECODE_FACTOR (1 << GM_TRANS_ONLY_PREC_DIFF)
181 
182 #define GM_ALPHA_PREC_BITS 15
183 #define GM_ABS_ALPHA_BITS 12
184 #define GM_ALPHA_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_ALPHA_PREC_BITS)
185 #define GM_ALPHA_DECODE_FACTOR (1 << GM_ALPHA_PREC_DIFF)
186 
187 #define GM_ROW3HOMO_PREC_BITS 16
188 #define GM_ABS_ROW3HOMO_BITS 11
189 #define GM_ROW3HOMO_PREC_DIFF \
190   (WARPEDMODEL_ROW3HOMO_PREC_BITS - GM_ROW3HOMO_PREC_BITS)
191 #define GM_ROW3HOMO_DECODE_FACTOR (1 << GM_ROW3HOMO_PREC_DIFF)
192 
193 #define GM_TRANS_MAX (1 << GM_ABS_TRANS_BITS)
194 #define GM_ALPHA_MAX (1 << GM_ABS_ALPHA_BITS)
195 #define GM_ROW3HOMO_MAX (1 << GM_ABS_ROW3HOMO_BITS)
196 
197 #define GM_TRANS_MIN -GM_TRANS_MAX
198 #define GM_ALPHA_MIN -GM_ALPHA_MAX
199 #define GM_ROW3HOMO_MIN -GM_ROW3HOMO_MAX
200 
block_center_x(int mi_col,BLOCK_SIZE bs)201 static INLINE int block_center_x(int mi_col, BLOCK_SIZE bs) {
202   const int bw = block_size_wide[bs];
203   return mi_col * MI_SIZE + bw / 2 - 1;
204 }
205 
block_center_y(int mi_row,BLOCK_SIZE bs)206 static INLINE int block_center_y(int mi_row, BLOCK_SIZE bs) {
207   const int bh = block_size_high[bs];
208   return mi_row * MI_SIZE + bh / 2 - 1;
209 }
210 
convert_to_trans_prec(int allow_hp,int coor)211 static INLINE int convert_to_trans_prec(int allow_hp, int coor) {
212   if (allow_hp)
213     return ROUND_POWER_OF_TWO_SIGNED(coor, WARPEDMODEL_PREC_BITS - 3);
214   else
215     return ROUND_POWER_OF_TWO_SIGNED(coor, WARPEDMODEL_PREC_BITS - 2) * 2;
216 }
integer_mv_precision(MV * mv)217 static INLINE void integer_mv_precision(MV *mv) {
218   int mod = (mv->row % 8);
219   if (mod != 0) {
220     mv->row -= mod;
221     if (abs(mod) > 4) {
222       if (mod > 0) {
223         mv->row += 8;
224       } else {
225         mv->row -= 8;
226       }
227     }
228   }
229 
230   mod = (mv->col % 8);
231   if (mod != 0) {
232     mv->col -= mod;
233     if (abs(mod) > 4) {
234       if (mod > 0) {
235         mv->col += 8;
236       } else {
237         mv->col -= 8;
238       }
239     }
240   }
241 }
242 // Convert a global motion vector into a motion vector at the centre of the
243 // given block.
244 //
245 // The resulting motion vector will have three fractional bits of precision. If
246 // allow_hp is zero, the bottom bit will always be zero. If CONFIG_AMVR and
247 // is_integer is true, the bottom three bits will be zero (so the motion vector
248 // 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)249 static INLINE int_mv gm_get_motion_vector(const WarpedMotionParams *gm,
250                                           int allow_hp, BLOCK_SIZE bsize,
251                                           int mi_col, int mi_row,
252                                           int is_integer) {
253   int_mv res;
254 
255   if (gm->wmtype == IDENTITY) {
256     res.as_int = 0;
257     return res;
258   }
259 
260   const int32_t *mat = gm->wmmat;
261   int x, y, tx, ty;
262 
263   if (gm->wmtype == TRANSLATION) {
264     // All global motion vectors are stored with WARPEDMODEL_PREC_BITS (16)
265     // bits of fractional precision. The offset for a translation is stored in
266     // entries 0 and 1. For translations, all but the top three (two if
267     // cm->features.allow_high_precision_mv is false) fractional bits are always
268     // zero.
269     //
270     // After the right shifts, there are 3 fractional bits of precision. If
271     // allow_hp is false, the bottom bit is always zero (so we don't need a
272     // call to convert_to_trans_prec here)
273     //
274     // Note: There is an AV1 specification bug here:
275     //
276     // gm->wmmat[0] is supposed to be the horizontal translation, and so should
277     // go into res.as_mv.col, and gm->wmmat[1] is supposed to be the vertical
278     // translation and so should go into res.as_mv.row
279     //
280     // However, in the spec, these assignments are accidentally reversed, and so
281     // we must keep this incorrect logic to match the spec.
282     //
283     // See also: https://crbug.com/aomedia/3328
284     res.as_mv.row = gm->wmmat[0] >> GM_TRANS_ONLY_PREC_DIFF;
285     res.as_mv.col = gm->wmmat[1] >> GM_TRANS_ONLY_PREC_DIFF;
286     assert(IMPLIES(1 & (res.as_mv.row | res.as_mv.col), allow_hp));
287     if (is_integer) {
288       integer_mv_precision(&res.as_mv);
289     }
290     return res;
291   }
292 
293   x = block_center_x(mi_col, bsize);
294   y = block_center_y(mi_row, bsize);
295 
296   if (gm->wmtype == ROTZOOM) {
297     assert(gm->wmmat[5] == gm->wmmat[2]);
298     assert(gm->wmmat[4] == -gm->wmmat[3]);
299   }
300 
301   const int xc =
302       (mat[2] - (1 << WARPEDMODEL_PREC_BITS)) * x + mat[3] * y + mat[0];
303   const int yc =
304       mat[4] * x + (mat[5] - (1 << WARPEDMODEL_PREC_BITS)) * y + mat[1];
305   tx = convert_to_trans_prec(allow_hp, xc);
306   ty = convert_to_trans_prec(allow_hp, yc);
307 
308   res.as_mv.row = ty;
309   res.as_mv.col = tx;
310 
311   if (is_integer) {
312     integer_mv_precision(&res.as_mv);
313   }
314   return res;
315 }
316 
get_wmtype(const WarpedMotionParams * gm)317 static INLINE TransformationType get_wmtype(const WarpedMotionParams *gm) {
318   if (gm->wmmat[5] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[4] &&
319       gm->wmmat[2] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[3]) {
320     return ((!gm->wmmat[1] && !gm->wmmat[0]) ? IDENTITY : TRANSLATION);
321   }
322   if (gm->wmmat[2] == gm->wmmat[5] && gm->wmmat[3] == -gm->wmmat[4])
323     return ROTZOOM;
324   else
325     return AFFINE;
326 }
327 
328 typedef struct candidate_mv {
329   int_mv this_mv;
330   int_mv comp_mv;
331 } CANDIDATE_MV;
332 
is_zero_mv(const MV * mv)333 static INLINE int is_zero_mv(const MV *mv) {
334   return *((const uint32_t *)mv) == 0;
335 }
336 
is_equal_mv(const MV * a,const MV * b)337 static INLINE int is_equal_mv(const MV *a, const MV *b) {
338   return *((const uint32_t *)a) == *((const uint32_t *)b);
339 }
340 
clamp_mv(MV * mv,const SubpelMvLimits * mv_limits)341 static INLINE void clamp_mv(MV *mv, const SubpelMvLimits *mv_limits) {
342   mv->col = clamp(mv->col, mv_limits->col_min, mv_limits->col_max);
343   mv->row = clamp(mv->row, mv_limits->row_min, mv_limits->row_max);
344 }
345 
clamp_fullmv(FULLPEL_MV * mv,const FullMvLimits * mv_limits)346 static INLINE void clamp_fullmv(FULLPEL_MV *mv, const FullMvLimits *mv_limits) {
347   mv->col = clamp(mv->col, mv_limits->col_min, mv_limits->col_max);
348   mv->row = clamp(mv->row, mv_limits->row_min, mv_limits->row_max);
349 }
350 
351 #ifdef __cplusplus
352 }  // extern "C"
353 #endif
354 
355 #endif  // AOM_AV1_COMMON_MV_H_
356