blob: cd4b4e670079ccb1264a2b9b5442e3fc09486a3a [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"
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "aom_dsp/aom_filter.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22typedef struct mv {
23 int16_t row;
24 int16_t col;
25} MV;
26
27typedef union int_mv {
28 uint32_t as_int;
29 MV as_mv;
30} int_mv; /* facilitates faster equality tests and copies */
31
32typedef struct mv32 {
33 int32_t row;
34 int32_t col;
35} MV32;
36
Sarah Parkere5299862016-08-16 14:57:37 -070037#if CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
38// Bits of precision used for the model
Debargha Mukherjee3a50db42016-12-06 15:20:03 -080039#define WARPEDMODEL_PREC_BITS 16
40#define WARPEDMODEL_ROW3HOMO_PREC_BITS 16
Sarah Parkere5299862016-08-16 14:57:37 -070041
42// Bits of subpel precision for warped interpolation
43#define WARPEDPIXEL_PREC_BITS 6
44#define WARPEDPIXEL_PREC_SHIFTS (1 << WARPEDPIXEL_PREC_BITS)
45
46// Taps for ntap filter
47#define WARPEDPIXEL_FILTER_TAPS 6
48
49// Precision of filter taps
50#define WARPEDPIXEL_FILTER_BITS 7
51
52#define WARPEDDIFF_PREC_BITS (WARPEDMODEL_PREC_BITS - WARPEDPIXEL_PREC_BITS)
53
David Barkercf3d0b02016-11-10 10:14:49 +000054/* clang-format off */
Sarah Parkere5299862016-08-16 14:57:37 -070055typedef enum {
David Barkercf3d0b02016-11-10 10:14:49 +000056 IDENTITY = 0, // identity transformation, 0-parameter
57 TRANSLATION = 1, // translational motion 2-parameter
Debargha Mukherjeeb0f6bd42016-12-02 09:19:39 -080058 ROTZOOM = 2, // simplified affine with rotation + zoom only, 4-parameter
David Barkercf3d0b02016-11-10 10:14:49 +000059 AFFINE = 3, // affine, 6-parameter
60 HOMOGRAPHY = 4, // homography, 8-parameter
61 TRANS_TYPES = 5,
Sarah Parkere5299862016-08-16 14:57:37 -070062} TransformationType;
David Barkercf3d0b02016-11-10 10:14:49 +000063/* clang-format on */
64
Debargha Mukherjeeb0f6bd42016-12-02 09:19:39 -080065// Number of types used for global motion (must be >= 3 and <= TRANS_TYPES)
David Barkercf3d0b02016-11-10 10:14:49 +000066#define GLOBAL_TRANS_TYPES 3
Sarah Parkere5299862016-08-16 14:57:37 -070067
68// number of parameters used by each transformation in TransformationTypes
David Barkercf3d0b02016-11-10 10:14:49 +000069static const int n_trans_model_params[TRANS_TYPES] = { 0, 2, 4, 6, 8 };
Sarah Parkere5299862016-08-16 14:57:37 -070070
Debargha Mukherjee8db4c772016-11-07 12:54:21 -080071// The order of values in the wmmat matrix below is best described
72// by the homography:
73// [x' (m2 m3 m0 [x
David Barkercf3d0b02016-11-10 10:14:49 +000074// y' = m4 m5 m1 * y
Debargha Mukherjee8db4c772016-11-07 12:54:21 -080075// 1] m6 m7 1) 1]
Sarah Parkere5299862016-08-16 14:57:37 -070076typedef struct {
77 TransformationType wmtype;
David Barkercf3d0b02016-11-10 10:14:49 +000078 int32_t wmmat[8];
Sarah Parkere5299862016-08-16 14:57:37 -070079} WarpedMotionParams;
80#endif // CONFIG_GLOBAL_MOTION || CONFIG_WARPED_MOTION
81
Yaowu Xuc27fc142016-08-22 16:08:15 -070082#if CONFIG_GLOBAL_MOTION
83// ALPHA here refers to parameters a and b in rotzoom model:
84// | a b|
85// |-b a|
86//
87// and a, b, c, d in affine model:
88// | a b|
89// | c d|
90//
91// Anything ending in PREC_BITS is the number of bits of precision
92// to maintain when converting from double to integer.
93//
94// The ABS parameters are used to create an upper and lower bound
95// for each parameter. In other words, after a parameter is integerized
96// it is clamped between -(1 << ABS_XXX_BITS) and (1 << ABS_XXX_BITS).
97//
98// XXX_PREC_DIFF and XXX_DECODE_FACTOR
99// are computed once here to prevent repetitive
100// computation on the decoder side. These are
101// to allow the global motion parameters to be encoded in a lower
102// precision than the warped model precision. This means that they
103// need to be changed to warped precision when they are decoded.
104//
105// XX_MIN, XX_MAX are also computed to avoid repeated computation
106
Debargha Mukherjee5f305852016-11-03 15:47:21 -0700107#define GM_TRANS_PREC_BITS 3
Debargha Mukherjee3a50db42016-12-06 15:20:03 -0800108#define GM_ABS_TRANS_BITS 9
Yaowu Xuc27fc142016-08-22 16:08:15 -0700109#define GM_TRANS_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_TRANS_PREC_BITS)
110#define GM_TRANS_DECODE_FACTOR (1 << GM_TRANS_PREC_DIFF)
111
Debargha Mukherjee3a50db42016-12-06 15:20:03 -0800112#define GM_ALPHA_PREC_BITS 15
113#define GM_ABS_ALPHA_BITS 12
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114#define GM_ALPHA_PREC_DIFF (WARPEDMODEL_PREC_BITS - GM_ALPHA_PREC_BITS)
115#define GM_ALPHA_DECODE_FACTOR (1 << GM_ALPHA_PREC_DIFF)
116
Debargha Mukherjee3a50db42016-12-06 15:20:03 -0800117#define GM_ROW3HOMO_PREC_BITS 16
118#define GM_ABS_ROW3HOMO_BITS 11
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800119#define GM_ROW3HOMO_PREC_DIFF \
120 (WARPEDMODEL_ROW3HOMO_PREC_BITS - GM_ROW3HOMO_PREC_BITS)
121#define GM_ROW3HOMO_DECODE_FACTOR (1 << GM_ROW3HOMO_PREC_DIFF)
122
Yaowu Xuc27fc142016-08-22 16:08:15 -0700123#define GM_TRANS_MAX (1 << GM_ABS_TRANS_BITS)
124#define GM_ALPHA_MAX (1 << GM_ABS_ALPHA_BITS)
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800125#define GM_ROW3HOMO_MAX (1 << GM_ABS_ROW3HOMO_BITS)
126
Yaowu Xuc27fc142016-08-22 16:08:15 -0700127#define GM_TRANS_MIN -GM_TRANS_MAX
128#define GM_ALPHA_MIN -GM_ALPHA_MAX
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800129#define GM_ROW3HOMO_MIN -GM_ROW3HOMO_MAX
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130
Debargha Mukherjee9febfc12016-12-07 13:20:44 -0800131// Bits used for different models
132#define GM_IDENTITY_BITS 0
133#define GM_TRANSLATION_BITS ((GM_ABS_TRANS_BITS + 1) * 2)
134#define GM_ROTZOOM_BITS (GM_TRANSLATION_BITS + (GM_ABS_ALPHA_BITS + 1) * 2)
135#define GM_AFFINE_BITS (GM_ROTZOOM_BITS + (GM_ABS_ALPHA_BITS + 1) * 2)
136#define GM_HOMOGRAPHY_BITS (GM_AFFINE_BITS + (GM_ABS_ROW3HOMO_BITS + 1) * 2)
137
Debargha Mukherjee5f305852016-11-03 15:47:21 -0700138// Convert a global motion translation vector (which may have more bits than a
139// regular motion vector) into a motion vector
David Barkercdcac6d2016-12-01 17:04:16 +0000140static INLINE int_mv gm_get_motion_vector(const WarpedMotionParams *gm,
141 int allow_hp) {
Debargha Mukherjee5f305852016-11-03 15:47:21 -0700142 int_mv res;
David Barkercdcac6d2016-12-01 17:04:16 +0000143 res.as_mv.row = allow_hp ? (int16_t)ROUND_POWER_OF_TWO_SIGNED(
144 gm->wmmat[1], WARPEDMODEL_PREC_BITS - 3)
145 : (int16_t)ROUND_POWER_OF_TWO_SIGNED(
146 gm->wmmat[1], WARPEDMODEL_PREC_BITS - 2) *
147 2;
148 res.as_mv.col = allow_hp ? (int16_t)ROUND_POWER_OF_TWO_SIGNED(
149 gm->wmmat[0], WARPEDMODEL_PREC_BITS - 3)
150 : (int16_t)ROUND_POWER_OF_TWO_SIGNED(
151 gm->wmmat[0], WARPEDMODEL_PREC_BITS - 2) *
152 2;
Debargha Mukherjee5f305852016-11-03 15:47:21 -0700153 return res;
154}
155
David Barkercf3d0b02016-11-10 10:14:49 +0000156static INLINE TransformationType get_gmtype(const WarpedMotionParams *gm) {
157 // if (gm->wmmat[6] != 0 || gm->wmmat[7] != 0) return HOMOGRAPHY;
158 if (gm->wmmat[5] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[4] &&
159 gm->wmmat[2] == (1 << WARPEDMODEL_PREC_BITS) && !gm->wmmat[3]) {
160 return ((!gm->wmmat[1] && !gm->wmmat[0]) ? IDENTITY : TRANSLATION);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161 }
David Barkercf3d0b02016-11-10 10:14:49 +0000162 if (gm->wmmat[2] == gm->wmmat[5] && gm->wmmat[3] == -gm->wmmat[4])
163 return ROTZOOM;
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800164 else
David Barkercf3d0b02016-11-10 10:14:49 +0000165 return AFFINE;
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800166}
167
Debargha Mukherjeee3e00792016-11-13 11:35:44 -0800168static INLINE void set_default_gmparams(WarpedMotionParams *wm) {
169 static const int32_t default_wm_mat[8] = {
Debargha Mukherjee8db4c772016-11-07 12:54:21 -0800170 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0, (1 << WARPEDMODEL_PREC_BITS), 0, 0
171 };
Debargha Mukherjeee3e00792016-11-13 11:35:44 -0800172 memcpy(wm->wmmat, default_wm_mat, sizeof(wm->wmmat));
173 wm->wmtype = IDENTITY;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174}
175#endif // CONFIG_GLOBAL_MOTION
176
177#if CONFIG_REF_MV
178typedef struct candidate_mv {
179 int_mv this_mv;
180 int_mv comp_mv;
Jingning Han3f338832016-11-18 11:01:48 -0800181 uint8_t pred_diff[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 int weight;
183} CANDIDATE_MV;
184#endif
185
186static INLINE int is_zero_mv(const MV *mv) {
187 return *((const uint32_t *)mv) == 0;
188}
189
190static INLINE int is_equal_mv(const MV *a, const MV *b) {
191 return *((const uint32_t *)a) == *((const uint32_t *)b);
192}
193
194static INLINE void clamp_mv(MV *mv, int min_col, int max_col, int min_row,
195 int max_row) {
196 mv->col = clamp(mv->col, min_col, max_col);
197 mv->row = clamp(mv->row, min_row, max_row);
198}
199
200static INLINE int mv_has_subpel(const MV *mv) {
201 return (mv->row & SUBPEL_MASK) || (mv->col & SUBPEL_MASK);
202}
203#ifdef __cplusplus
204} // extern "C"
205#endif
206
Yaowu Xuf883b422016-08-30 14:01:10 -0700207#endif // AV1_COMMON_MV_H_