blob: dabfc0eade1d20fd2c89990a419efe4ddf68c760 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AV1_COMMON_MV_H_
13#define AV1_COMMON_MV_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include "av1/common/common.h"
Sarah Parkerae7c4582017-02-28 16:30:30 -080016#include "av1/common/common_data.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "aom_dsp/aom_filter.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23typedef struct mv {
24 int16_t row;
25 int16_t col;
26} MV;
27
28typedef union int_mv {
29 uint32_t as_int;
30 MV as_mv;
31} int_mv; /* facilitates faster equality tests and copies */
32
33typedef struct mv32 {
34 int32_t row;
35 int32_t col;
36} MV32;
37
Yunqing Wang1bc82862017-06-28 15:49:48 -070038#if CONFIG_WARPED_MOTION
39#define WARPED_MOTION_SORT_SAMPLES 1
40#endif // CONFIG_WARPED_MOTION
41
Sarah Parkere5299862016-08-16 14:57:37 -070042#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
43// Bits of precision used for the model
Debargha Mukherjee3a50db42016-12-06 15:20:03 -080044#define WARPEDMODEL_PREC_BITS 16
45#define WARPEDMODEL_ROW3HOMO_PREC_BITS 16
Sarah Parkere5299862016-08-16 14:57:37 -070046
Debargha Mukherjee1e6e1302017-04-07 15:27:53 -070047#define WARPEDMODEL_TRANS_CLAMP (128 << WARPEDMODEL_PREC_BITS)
Debargha Mukherjeef053cba2017-06-21 19:04:00 -070048#define WARPEDMODEL_NONDIAGAFFINE_CLAMP (1 << (WARPEDMODEL_PREC_BITS - 3))
49#define WARPEDMODEL_ROW3HOMO_CLAMP (1 << (WARPEDMODEL_PREC_BITS - 2))
Debargha Mukherjee1e6e1302017-04-07 15:27:53 -070050
Sarah Parkere5299862016-08-16 14:57:37 -070051// Bits of subpel precision for warped interpolation
52#define WARPEDPIXEL_PREC_BITS 6
53#define WARPEDPIXEL_PREC_SHIFTS (1 << WARPEDPIXEL_PREC_BITS)
54
55// Taps for ntap filter
56#define WARPEDPIXEL_FILTER_TAPS 6
57
58// Precision of filter taps
59#define WARPEDPIXEL_FILTER_BITS 7
60
Sean Purser-Haskell14b81122017-05-03 16:50:07 -070061#define WARP_PARAM_REDUCE_BITS 6
62
Debargha Mukherjee1d184602017-04-04 04:54:07 -070063// Precision bits reduction after horizontal shear
64#define HORSHEAR_REDUCE_PREC_BITS 5
65#define VERSHEAR_REDUCE_PREC_BITS \
66 (2 * WARPEDPIXEL_FILTER_BITS - HORSHEAR_REDUCE_PREC_BITS)
67
Sarah Parkere5299862016-08-16 14:57:37 -070068#define WARPEDDIFF_PREC_BITS (WARPEDMODEL_PREC_BITS - WARPEDPIXEL_PREC_BITS)
69
David Barkercf3d0b02016-11-10 10:14:49 +000070/* clang-format off */
Sarah Parkere5299862016-08-16 14:57:37 -070071typedef enum {
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -080072 IDENTITY = 0, // identity transformation, 0-parameter
73 TRANSLATION = 1, // translational motion 2-parameter
74 ROTZOOM = 2, // simplified affine with rotation + zoom only, 4-parameter
75 AFFINE = 3, // affine, 6-parameter
76 HORTRAPEZOID = 4, // constrained homography, hor trapezoid, 6-parameter
77 VERTRAPEZOID = 5, // constrained homography, ver trapezoid, 6-parameter
78 HOMOGRAPHY = 6, // homography, 8-parameter
79 TRANS_TYPES = 7,
Sarah Parkere5299862016-08-16 14:57:37 -070080} TransformationType;
David Barkercf3d0b02016-11-10 10:14:49 +000081/* clang-format on */
82
Debargha Mukherjeeb0f6bd42016-12-02 09:19:39 -080083// Number of types used for global motion (must be >= 3 and <= TRANS_TYPES)
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -080084// The following can be useful:
85// GLOBAL_TRANS_TYPES 3 - up to rotation-zoom
86// GLOBAL_TRANS_TYPES 4 - up to affine
87// GLOBAL_TRANS_TYPES 6 - up to hor/ver trapezoids
88// GLOBAL_TRANS_TYPES 7 - up to full homography
Debargha Mukherjee5f35e582017-04-25 18:05:18 -070089#define GLOBAL_TRANS_TYPES 4
Sarah Parkere5299862016-08-16 14:57:37 -070090
Thomas Daviesb732c1e2017-06-09 14:13:43 +010091// First bit indicates whether using identity or not
92// GLOBAL_TYPE_BITS=ceiling(log2(GLOBAL_TRANS_TYPES-1)) is the
93// number of bits needed to cover the remaining possibilities
94#define GLOBAL_TYPE_BITS (get_msb(2 * GLOBAL_TRANS_TYPES - 3))
95
Sarah Parker4c10a3c2017-04-10 19:37:59 -070096typedef struct {
97#if CONFIG_GLOBAL_MOTION
98 int global_warp_allowed;
99#endif // CONFIG_GLOBAL_MOTION
100#if CONFIG_WARPED_MOTION
101 int local_warp_allowed;
102#endif // CONFIG_WARPED_MOTION
103} WarpTypesAllowed;
104
Sarah Parkere5299862016-08-16 14:57:37 -0700105// number of parameters used by each transformation in TransformationTypes
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -0800106static const int trans_model_params[TRANS_TYPES] = { 0, 2, 4, 6, 6, 6, 8 };
Sarah Parkere5299862016-08-16 14:57:37 -0700107
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800108// The order of values in the wmmat matrix below is best described
109// by the homography:
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -0800110// [x' (m2 m3 m0 [x
111// z . y' = m4 m5 m1 * y
112// 1] m6 m7 1) 1]
Sarah Parkere5299862016-08-16 14:57:37 -0700113typedef struct {
114 TransformationType wmtype;
David Barkercf3d0b02016-11-10 10:14:49 +0000115 int32_t wmmat[8];
Debargha Mukherjee27f6e662017-04-10 11:17:16 -0700116 int16_t alpha, beta, gamma, delta;
Sarah Parkere5299862016-08-16 14:57:37 -0700117} WarpedMotionParams;
Sarah Parker4c10a3c2017-04-10 19:37:59 -0700118
119static INLINE void set_default_warp_params(WarpedMotionParams *wm) {
120 static const int32_t default_wm_mat[8] = {
121 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0
122 };
123 memset(wm, 0, sizeof(*wm));
124 memcpy(wm->wmmat, default_wm_mat, sizeof(wm->wmmat));
125 wm->wmtype = IDENTITY;
126}
Sarah Parkere5299862016-08-16 14:57:37 -0700127#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
128
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129#if CONFIG_GLOBAL_MOTION
Sarah Parkerf7ab7e32017-04-26 12:23:56 -0700130// The following constants describe the various precisions
131// of different parameters in the global motion experiment.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132//
Sarah Parkerf7ab7e32017-04-26 12:23:56 -0700133// Given the general homography:
134// [x' (a b c [x
135// z . y' = d e f * y
136// 1] g h i) 1]
137//
138// Constants using the name ALPHA here are related to parameters
139// a, b, d, e. Constants using the name TRANS are related
140// to parameters c and f.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700141//
142// Anything ending in PREC_BITS is the number of bits of precision
143// to maintain when converting from double to integer.
144//
145// The ABS parameters are used to create an upper and lower bound
146// for each parameter. In other words, after a parameter is integerized
147// it is clamped between -(1 << ABS_XXX_BITS) and (1 << ABS_XXX_BITS).
148//
149// XXX_PREC_DIFF and XXX_DECODE_FACTOR
150// are computed once here to prevent repetitive
151// computation on the decoder side. These are
152// to allow the global motion parameters to be encoded in a lower
153// precision than the warped model precision. This means that they
154// need to be changed to warped precision when they are decoded.
155//
156// XX_MIN, XX_MAX are also computed to avoid repeated computation
157
Sarah Parkerf1783292017-04-05 11:55:27 -0700158#define SUBEXPFIN_K 3
David Barker6c652782017-02-13 13:46:40 +0000159#define GM_TRANS_PREC_BITS 6
160#define GM_ABS_TRANS_BITS 12
Sarah Parker13d06622017-03-10 17:03:28 -0800161#define GM_ABS_TRANS_ONLY_BITS (GM_ABS_TRANS_BITS - GM_TRANS_PREC_BITS + 3)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162#define GM_TRANS_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_TRANS_PREC_BITS)
Sarah Parker13d06622017-03-10 17:03:28 -0800163#define GM_TRANS_ONLY_PREC_DIFF (WARPEDMODEL_PREC_BITS - 3)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164#define GM_TRANS_DECODE_FACTOR (1 << GM_TRANS_PREC_DIFF)
Sarah Parker13d06622017-03-10 17:03:28 -0800165#define GM_TRANS_ONLY_DECODE_FACTOR (1 << GM_TRANS_ONLY_PREC_DIFF)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166
Debargha Mukherjee3a50db42016-12-06 15:20:03 -0800167#define GM_ALPHA_PREC_BITS 15
168#define GM_ABS_ALPHA_BITS 12
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169#define GM_ALPHA_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_ALPHA_PREC_BITS)
170#define GM_ALPHA_DECODE_FACTOR (1 << GM_ALPHA_PREC_DIFF)
171
Debargha Mukherjee3a50db42016-12-06 15:20:03 -0800172#define GM_ROW3HOMO_PREC_BITS 16
173#define GM_ABS_ROW3HOMO_BITS 11
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800174#define GM_ROW3HOMO_PREC_DIFF \
175 (WARPEDMODEL_ROW3HOMO_PREC_BITS - GM_ROW3HOMO_PREC_BITS)
176#define GM_ROW3HOMO_DECODE_FACTOR (1 << GM_ROW3HOMO_PREC_DIFF)
177
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178#define GM_TRANS_MAX (1 << GM_ABS_TRANS_BITS)
179#define GM_ALPHA_MAX (1 << GM_ABS_ALPHA_BITS)
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800180#define GM_ROW3HOMO_MAX (1 << GM_ABS_ROW3HOMO_BITS)
181
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182#define GM_TRANS_MIN -GM_TRANS_MAX
183#define GM_ALPHA_MIN -GM_ALPHA_MAX
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800184#define GM_ROW3HOMO_MIN -GM_ROW3HOMO_MAX
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185
Sarah Parkerae7c4582017-02-28 16:30:30 -0800186// Use global motion parameters for sub8x8 blocks
187#define GLOBAL_SUB8X8_USED 0
188
189static INLINE int block_center_x(int mi_col, BLOCK_SIZE bs) {
190 const int bw = block_size_wide[bs];
Debargha Mukherjee07b1d382017-03-24 08:54:26 -0700191 return mi_col * MI_SIZE + bw / 2 - 1;
Sarah Parkerae7c4582017-02-28 16:30:30 -0800192}
193
194static INLINE int block_center_y(int mi_row, BLOCK_SIZE bs) {
195 const int bh = block_size_high[bs];
Debargha Mukherjee07b1d382017-03-24 08:54:26 -0700196 return mi_row * MI_SIZE + bh / 2 - 1;
Sarah Parkerae7c4582017-02-28 16:30:30 -0800197}
198
Sarah Parker13d06622017-03-10 17:03:28 -0800199static INLINE int convert_to_trans_prec(int allow_hp, int coor) {
Yaowu Xuf9d15b62017-05-01 15:14:10 -0700200 if (allow_hp)
201 return ROUND_POWER_OF_TWO_SIGNED(coor, WARPEDMODEL_PREC_BITS - 3);
202 else
203 return ROUND_POWER_OF_TWO_SIGNED(coor, WARPEDMODEL_PREC_BITS - 2) * 2;
Sarah Parker13d06622017-03-10 17:03:28 -0800204}
205
Debargha Mukherjee5f305852016-11-03 15:47:21 -0700206// Convert a global motion translation vector (which may have more bits than a
207// regular motion vector) into a motion vector
David Barkercdcac6d2016-12-01 17:04:16 +0000208static INLINE int_mv gm_get_motion_vector(const WarpedMotionParams *gm,
Sarah Parkerae7c4582017-02-28 16:30:30 -0800209 int allow_hp, BLOCK_SIZE bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -0800210 int mi_col, int mi_row,
211 int block_idx) {
212 const int unify_bsize = CONFIG_CB4X4;
Debargha Mukherjee5f305852016-11-03 15:47:21 -0700213 int_mv res;
David Barker45390c12017-02-20 14:44:40 +0000214 const int32_t *mat = gm->wmmat;
Debargha Mukherjee552b0b22017-05-01 15:29:16 -0700215 int x, y, tx, ty;
Sarah Parker13d06622017-03-10 17:03:28 -0800216
217 if (gm->wmtype == TRANSLATION) {
218 res.as_mv.row = gm->wmmat[0] >> GM_TRANS_ONLY_PREC_DIFF;
219 res.as_mv.col = gm->wmmat[1] >> GM_TRANS_ONLY_PREC_DIFF;
220 return res;
221 }
222
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -0800223 if (bsize >= BLOCK_8X8 || unify_bsize) {
224 x = block_center_x(mi_col, bsize);
225 y = block_center_y(mi_row, bsize);
226 } else {
227 x = block_center_x(mi_col, bsize);
228 y = block_center_y(mi_row, bsize);
229 x += (block_idx & 1) * MI_SIZE / 2;
230 y += (block_idx & 2) * MI_SIZE / 4;
231 }
David Barker45390c12017-02-20 14:44:40 +0000232
233 if (gm->wmtype == ROTZOOM) {
234 assert(gm->wmmat[5] == gm->wmmat[2]);
235 assert(gm->wmmat[4] == -gm->wmmat[3]);
236 }
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -0800237 if (gm->wmtype > AFFINE) {
Debargha Mukherjee552b0b22017-05-01 15:29:16 -0700238 int xc = (int)((int64_t)mat[2] * x + (int64_t)mat[3] * y + mat[0]);
239 int yc = (int)((int64_t)mat[4] * x + (int64_t)mat[5] * y + mat[1]);
240 const int Z = (int)((int64_t)mat[6] * x + (int64_t)mat[7] * y +
241 (1 << WARPEDMODEL_ROW3HOMO_PREC_BITS));
242 xc *= 1 << (WARPEDMODEL_ROW3HOMO_PREC_BITS - WARPEDMODEL_PREC_BITS);
243 yc *= 1 << (WARPEDMODEL_ROW3HOMO_PREC_BITS - WARPEDMODEL_PREC_BITS);
244 xc = (int)(xc > 0 ? ((int64_t)xc + Z / 2) / Z : ((int64_t)xc - Z / 2) / Z);
245 yc = (int)(yc > 0 ? ((int64_t)yc + Z / 2) / Z : ((int64_t)yc - Z / 2) / Z);
246 tx = convert_to_trans_prec(allow_hp, xc) - (x << 3);
247 ty = convert_to_trans_prec(allow_hp, yc) - (y << 3);
248 } else {
249 const int xc =
250 (mat[2] - (1 << WARPEDMODEL_PREC_BITS)) * x + mat[3] * y + mat[0];
251 const int yc =
252 mat[4] * x + (mat[5] - (1 << WARPEDMODEL_PREC_BITS)) * y + mat[1];
253 tx = convert_to_trans_prec(allow_hp, xc);
254 ty = convert_to_trans_prec(allow_hp, yc);
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -0800255 }
256
David Barker45390c12017-02-20 14:44:40 +0000257 res.as_mv.row = ty;
258 res.as_mv.col = tx;
Debargha Mukherjee5f305852016-11-03 15:47:21 -0700259 return res;
260}
261
David Barkercf3d0b02016-11-10 10:14:49 +0000262static INLINE TransformationType get_gmtype(const WarpedMotionParams *gm) {
Debargha Mukherjee5dfa9302017-02-10 05:00:08 -0800263 if (gm->wmmat[6] != 0 || gm->wmmat[7] != 0) {
264 if (!gm->wmmat[6] && !gm->wmmat[4]) return HORTRAPEZOID;
265 if (!gm->wmmat[7] && !gm->wmmat[3]) return VERTRAPEZOID;
266 return HOMOGRAPHY;
267 }
David Barkercf3d0b02016-11-10 10:14:49 +0000268 if (gm->wmmat[5] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[4] &&
269 gm->wmmat[2] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[3]) {
270 return ((!gm->wmmat[1] && !gm->wmmat[0]) ? IDENTITY : TRANSLATION);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700271 }
David Barkercf3d0b02016-11-10 10:14:49 +0000272 if (gm->wmmat[2] == gm->wmmat[5] && gm->wmmat[3] == -gm->wmmat[4])
273 return ROTZOOM;
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800274 else
David Barkercf3d0b02016-11-10 10:14:49 +0000275 return AFFINE;
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800276}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700277#endif // CONFIG_GLOBAL_MOTION
278
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279typedef struct candidate_mv {
280 int_mv this_mv;
281 int_mv comp_mv;
Jingning Han3f338832016-11-18 11:01:48 -0800282 uint8_t pred_diff[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283 int weight;
284} CANDIDATE_MV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700285
286static INLINE int is_zero_mv(const MV *mv) {
287 return *((const uint32_t *)mv) == 0;
288}
289
290static INLINE int is_equal_mv(const MV *a, const MV *b) {
291 return *((const uint32_t *)a) == *((const uint32_t *)b);
292}
293
294static INLINE void clamp_mv(MV *mv, int min_col, int max_col, int min_row,
295 int max_row) {
296 mv->col = clamp(mv->col, min_col, max_col);
297 mv->row = clamp(mv->row, min_row, max_row);
298}
299
300static INLINE int mv_has_subpel(const MV *mv) {
301 return (mv->row & SUBPEL_MASK) || (mv->col & SUBPEL_MASK);
302}
303#ifdef __cplusplus
304} // extern "C"
305#endif
306
Yaowu Xuf883b422016-08-30 14:01:10 -0700307#endif // AV1_COMMON_MV_H_