mv.h 8.38 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12 13
#ifndef AV1_COMMON_MV_H_
#define AV1_COMMON_MV_H_
Jingning Han's avatar
Jingning Han committed
14

15
#include "av1/common/common.h"
16
#include "av1/common/common_data.h"
Yaowu Xu's avatar
Yaowu Xu committed
17
#include "aom_dsp/aom_filter.h"
Jingning Han's avatar
Jingning Han committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#ifdef __cplusplus
extern "C" {
#endif

typedef struct mv {
  int16_t row;
  int16_t col;
} MV;

typedef union int_mv {
  uint32_t as_int;
  MV as_mv;
} int_mv; /* facilitates faster equality tests and copies */

typedef struct mv32 {
  int32_t row;
  int32_t col;
} MV32;

38 39 40
#if (CONFIG_WARPED_MOTION || CONFIG_MOTION_VAR) && CONFIG_GLOBAL_MOTION
#define SEPARATE_GLOBAL_MOTION 0
#endif  // (CONFIG_WARPED_MOTION || CONFIG_MOTION_VAR) && CONFIG_GLOBAL_MOTION
41 42
#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
// Bits of precision used for the model
43 44
#define WARPEDMODEL_PREC_BITS 16
#define WARPEDMODEL_ROW3HOMO_PREC_BITS 16
45 46 47 48 49 50 51 52 53 54 55 56 57

// Bits of subpel precision for warped interpolation
#define WARPEDPIXEL_PREC_BITS 6
#define WARPEDPIXEL_PREC_SHIFTS (1 << WARPEDPIXEL_PREC_BITS)

// Taps for ntap filter
#define WARPEDPIXEL_FILTER_TAPS 6

// Precision of filter taps
#define WARPEDPIXEL_FILTER_BITS 7

#define WARPEDDIFF_PREC_BITS (WARPEDMODEL_PREC_BITS - WARPEDPIXEL_PREC_BITS)

58
/* clang-format off */
59
typedef enum {
60 61 62 63 64 65 66 67
  IDENTITY = 0,      // identity transformation, 0-parameter
  TRANSLATION = 1,   // translational motion 2-parameter
  ROTZOOM = 2,       // simplified affine with rotation + zoom only, 4-parameter
  AFFINE = 3,        // affine, 6-parameter
  HORTRAPEZOID = 4,  // constrained homography, hor trapezoid, 6-parameter
  VERTRAPEZOID = 5,  // constrained homography, ver trapezoid, 6-parameter
  HOMOGRAPHY = 6,    // homography, 8-parameter
  TRANS_TYPES = 7,
68
} TransformationType;
69 70
/* clang-format on */

71
// Number of types used for global motion (must be >= 3 and <= TRANS_TYPES)
72 73 74 75 76
// The following can be useful:
// GLOBAL_TRANS_TYPES 3 - up to rotation-zoom
// GLOBAL_TRANS_TYPES 4 - up to affine
// GLOBAL_TRANS_TYPES 6 - up to hor/ver trapezoids
// GLOBAL_TRANS_TYPES 7 - up to full homography
77
#define GLOBAL_TRANS_TYPES 3
78 79

// number of parameters used by each transformation in TransformationTypes
80
static const int trans_model_params[TRANS_TYPES] = { 0, 2, 4, 6, 6, 6, 8 };
81

82 83
// The order of values in the wmmat matrix below is best described
// by the homography:
84 85 86
//      [x'     (m2 m3 m0   [x
//  z .  y'  =   m4 m5 m1 *  y
//       1]      m6 m7 1)    1]
87 88
typedef struct {
  TransformationType wmtype;
89
  int32_t wmmat[8];
90
  int32_t alpha, beta, gamma, delta;
91 92 93
} WarpedMotionParams;
#endif  // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION

94 95 96 97 98
#if CONFIG_GLOBAL_MOTION
// ALPHA here refers to parameters a and b in rotzoom model:
// | a   b|
// |-b   a|
//
99 100 101 102
// and a, b, c, d in affine model:
// | a   b|
// | c   d|
//
103 104 105 106 107
// Anything ending in PREC_BITS is the number of bits of precision
// to maintain when converting from double to integer.
//
// The ABS parameters are used to create an upper and lower bound
// for each parameter. In other words, after a parameter is integerized
108
// it is clamped between -(1 << ABS_XXX_BITS) and (1 << ABS_XXX_BITS).
109
//
110
// XXX_PREC_DIFF and XXX_DECODE_FACTOR
111 112
// are computed once here to prevent repetitive
// computation on the decoder side. These are
113 114 115 116 117 118
// to allow the global motion parameters to be encoded in a lower
// precision than the warped model precision. This means that they
// need to be changed to warped precision when they are decoded.
//
// XX_MIN, XX_MAX are also computed to avoid repeated computation

119 120
#define GM_TRANS_PREC_BITS 6
#define GM_ABS_TRANS_BITS 12
clang-format's avatar
clang-format committed
121 122
#define GM_TRANS_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_TRANS_PREC_BITS)
#define GM_TRANS_DECODE_FACTOR (1 << GM_TRANS_PREC_DIFF)
123

124 125
#define GM_ALPHA_PREC_BITS 15
#define GM_ABS_ALPHA_BITS 12
clang-format's avatar
clang-format committed
126 127
#define GM_ALPHA_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_ALPHA_PREC_BITS)
#define GM_ALPHA_DECODE_FACTOR (1 << GM_ALPHA_PREC_DIFF)
128

129 130
#define GM_ROW3HOMO_PREC_BITS 16
#define GM_ABS_ROW3HOMO_BITS 11
131 132 133 134
#define GM_ROW3HOMO_PREC_DIFF \
  (WARPEDMODEL_ROW3HOMO_PREC_BITS - GM_ROW3HOMO_PREC_BITS)
#define GM_ROW3HOMO_DECODE_FACTOR (1 << GM_ROW3HOMO_PREC_DIFF)

clang-format's avatar
clang-format committed
135 136
#define GM_TRANS_MAX (1 << GM_ABS_TRANS_BITS)
#define GM_ALPHA_MAX (1 << GM_ABS_ALPHA_BITS)
137 138
#define GM_ROW3HOMO_MAX (1 << GM_ABS_ROW3HOMO_BITS)

clang-format's avatar
clang-format committed
139 140
#define GM_TRANS_MIN -GM_TRANS_MAX
#define GM_ALPHA_MIN -GM_ALPHA_MAX
141
#define GM_ROW3HOMO_MIN -GM_ROW3HOMO_MAX
142

143 144 145 146 147
// Use global motion parameters for sub8x8 blocks
#define GLOBAL_SUB8X8_USED 0

static INLINE int block_center_x(int mi_col, BLOCK_SIZE bs) {
  const int bw = block_size_wide[bs];
Debargha Mukherjee's avatar
Debargha Mukherjee committed
148
  return mi_col * MI_SIZE + bw / 2 - 1;
149 150 151 152
}

static INLINE int block_center_y(int mi_row, BLOCK_SIZE bs) {
  const int bh = block_size_high[bs];
Debargha Mukherjee's avatar
Debargha Mukherjee committed
153
  return mi_row * MI_SIZE + bh / 2 - 1;
154 155
}

156 157
// Convert a global motion translation vector (which may have more bits than a
// regular motion vector) into a motion vector
158
static INLINE int_mv gm_get_motion_vector(const WarpedMotionParams *gm,
159
                                          int allow_hp, BLOCK_SIZE bsize,
160 161 162
                                          int mi_col, int mi_row,
                                          int block_idx) {
  const int unify_bsize = CONFIG_CB4X4;
163
  int_mv res;
David Barker's avatar
David Barker committed
164
  const int32_t *mat = gm->wmmat;
165 166 167 168 169 170 171 172 173 174
  int xc, yc, x, y;
  if (bsize >= BLOCK_8X8 || unify_bsize) {
    x = block_center_x(mi_col, bsize);
    y = block_center_y(mi_row, bsize);
  } else {
    x = block_center_x(mi_col, bsize);
    y = block_center_y(mi_row, bsize);
    x += (block_idx & 1) * MI_SIZE / 2;
    y += (block_idx & 2) * MI_SIZE / 4;
  }
David Barker's avatar
David Barker committed
175 176 177 178 179 180 181 182 183 184
  int shift = allow_hp ? WARPEDMODEL_PREC_BITS - 3 : WARPEDMODEL_PREC_BITS - 2;
  int scale = allow_hp ? 0 : 1;

  if (gm->wmtype == ROTZOOM) {
    assert(gm->wmmat[5] == gm->wmmat[2]);
    assert(gm->wmmat[4] == -gm->wmmat[3]);
  }
  xc = mat[2] * x + mat[3] * y + mat[0];
  yc = mat[4] * x + mat[5] * y + mat[1];

185 186 187 188 189 190 191 192 193
  if (gm->wmtype > AFFINE) {
    const int Z =
        mat[6] * x + mat[7] * y + (1 << WARPEDMODEL_ROW3HOMO_PREC_BITS);
    xc <<= (WARPEDMODEL_ROW3HOMO_PREC_BITS - WARPEDMODEL_PREC_BITS);
    yc <<= (WARPEDMODEL_ROW3HOMO_PREC_BITS - WARPEDMODEL_PREC_BITS);
    xc = xc > 0 ? (xc + Z / 2) / Z : (xc - Z / 2) / Z;
    yc = yc > 0 ? (yc + Z / 2) / Z : (yc - Z / 2) / Z;
  }

David Barker's avatar
David Barker committed
194 195 196 197 198
  int tx = (ROUND_POWER_OF_TWO_SIGNED(xc, shift) << scale) - (x << 3);
  int ty = (ROUND_POWER_OF_TWO_SIGNED(yc, shift) << scale) - (y << 3);

  res.as_mv.row = ty;
  res.as_mv.col = tx;
199 200 201
  return res;
}

202
static INLINE TransformationType get_gmtype(const WarpedMotionParams *gm) {
203 204 205 206 207
  if (gm->wmmat[6] != 0 || gm->wmmat[7] != 0) {
    if (!gm->wmmat[6] && !gm->wmmat[4]) return HORTRAPEZOID;
    if (!gm->wmmat[7] && !gm->wmmat[3]) return VERTRAPEZOID;
    return HOMOGRAPHY;
  }
208 209 210
  if (gm->wmmat[5] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[4] &&
      gm->wmmat[2] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[3]) {
    return ((!gm->wmmat[1] && !gm->wmmat[0]) ? IDENTITY : TRANSLATION);
211
  }
212 213
  if (gm->wmmat[2] == gm->wmmat[5] && gm->wmmat[3] == -gm->wmmat[4])
    return ROTZOOM;
214
  else
215
    return AFFINE;
216 217
}

218 219
static INLINE void set_default_gmparams(WarpedMotionParams *wm) {
  static const int32_t default_wm_mat[8] = {
220 221
    0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0
  };
222
  memcpy(wm->wmmat, default_wm_mat, sizeof(wm->wmmat));
223
  wm->alpha = wm->beta = wm->gamma = wm->delta = 0;
224
  wm->wmtype = IDENTITY;
225 226 227
}
#endif  // CONFIG_GLOBAL_MOTION

228 229 230
#if CONFIG_REF_MV
typedef struct candidate_mv {
  int_mv this_mv;
Jingning Han's avatar
Jingning Han committed
231
  int_mv comp_mv;
232
  uint8_t pred_diff[2];
233 234 235 236
  int weight;
} CANDIDATE_MV;
#endif

Jingning Han's avatar
Jingning Han committed
237 238 239 240 241
static INLINE int is_zero_mv(const MV *mv) {
  return *((const uint32_t *)mv) == 0;
}

static INLINE int is_equal_mv(const MV *a, const MV *b) {
clang-format's avatar
clang-format committed
242
  return *((const uint32_t *)a) == *((const uint32_t *)b);
Jingning Han's avatar
Jingning Han committed
243 244
}

clang-format's avatar
clang-format committed
245 246
static INLINE void clamp_mv(MV *mv, int min_col, int max_col, int min_row,
                            int max_row) {
Jingning Han's avatar
Jingning Han committed
247 248 249 250
  mv->col = clamp(mv->col, min_col, max_col);
  mv->row = clamp(mv->row, min_row, max_row);
}

251
static INLINE int mv_has_subpel(const MV *mv) {
252
  return (mv->row & SUBPEL_MASK) || (mv->col & SUBPEL_MASK);
253
}
Jingning Han's avatar
Jingning Han committed
254 255 256 257
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
258
#endif  // AV1_COMMON_MV_H_