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 #ifndef AOM_AV1_COMMON_MVREF_COMMON_H_
12 #define AOM_AV1_COMMON_MVREF_COMMON_H_
13
14 #include "av1/common/av1_common_int.h"
15 #include "av1/common/blockd.h"
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #define MVREF_ROW_COLS 3
22
23 // Set the upper limit of the motion vector component magnitude.
24 // This would make a motion vector fit in 26 bits. Plus 3 bits for the
25 // reference frame index. A tuple of motion vector can hence be stored within
26 // 32 bit range for efficient load/store operations.
27 #define REFMVS_LIMIT ((1 << 12) - 1)
28
29 typedef struct position {
30 int row;
31 int col;
32 } POSITION;
33
34 // clamp_mv_ref
35 #define MV_BORDER (16 << 3) // Allow 16 pels in 1/8th pel units
36
get_relative_dist(const OrderHintInfo * oh,int a,int b)37 static INLINE int get_relative_dist(const OrderHintInfo *oh, int a, int b) {
38 if (!oh->enable_order_hint) return 0;
39
40 const int bits = oh->order_hint_bits_minus_1 + 1;
41
42 assert(bits >= 1);
43 assert(a >= 0 && a < (1 << bits));
44 assert(b >= 0 && b < (1 << bits));
45
46 int diff = a - b;
47 const int m = 1 << (bits - 1);
48 diff = (diff & (m - 1)) - (diff & m);
49 return diff;
50 }
51
clamp_mv_ref(MV * mv,int bw,int bh,const MACROBLOCKD * xd)52 static INLINE void clamp_mv_ref(MV *mv, int bw, int bh, const MACROBLOCKD *xd) {
53 const SubpelMvLimits mv_limits = {
54 xd->mb_to_left_edge - GET_MV_SUBPEL(bw) - MV_BORDER,
55 xd->mb_to_right_edge + GET_MV_SUBPEL(bw) + MV_BORDER,
56 xd->mb_to_top_edge - GET_MV_SUBPEL(bh) - MV_BORDER,
57 xd->mb_to_bottom_edge + GET_MV_SUBPEL(bh) + MV_BORDER
58 };
59 clamp_mv(mv, &mv_limits);
60 }
61
get_block_mv(const MB_MODE_INFO * candidate,int which_mv)62 static INLINE int_mv get_block_mv(const MB_MODE_INFO *candidate, int which_mv) {
63 return candidate->mv[which_mv];
64 }
65
66 // Checks that the given mi_row, mi_col and search point
67 // are inside the borders of the tile.
is_inside(const TileInfo * const tile,int mi_col,int mi_row,const POSITION * mi_pos)68 static INLINE int is_inside(const TileInfo *const tile, int mi_col, int mi_row,
69 const POSITION *mi_pos) {
70 return !(mi_row + mi_pos->row < tile->mi_row_start ||
71 mi_col + mi_pos->col < tile->mi_col_start ||
72 mi_row + mi_pos->row >= tile->mi_row_end ||
73 mi_col + mi_pos->col >= tile->mi_col_end);
74 }
75
find_valid_row_offset(const TileInfo * const tile,int mi_row,int row_offset)76 static INLINE int find_valid_row_offset(const TileInfo *const tile, int mi_row,
77 int row_offset) {
78 return clamp(row_offset, tile->mi_row_start - mi_row,
79 tile->mi_row_end - mi_row - 1);
80 }
81
find_valid_col_offset(const TileInfo * const tile,int mi_col,int col_offset)82 static INLINE int find_valid_col_offset(const TileInfo *const tile, int mi_col,
83 int col_offset) {
84 return clamp(col_offset, tile->mi_col_start - mi_col,
85 tile->mi_col_end - mi_col - 1);
86 }
87
lower_mv_precision(MV * mv,int allow_hp,int is_integer)88 static INLINE void lower_mv_precision(MV *mv, int allow_hp, int is_integer) {
89 if (is_integer) {
90 integer_mv_precision(mv);
91 } else {
92 if (!allow_hp) {
93 if (mv->row & 1) mv->row += (mv->row > 0 ? -1 : 1);
94 if (mv->col & 1) mv->col += (mv->col > 0 ? -1 : 1);
95 }
96 }
97 }
98
get_uni_comp_ref_idx(const MV_REFERENCE_FRAME * const rf)99 static INLINE int8_t get_uni_comp_ref_idx(const MV_REFERENCE_FRAME *const rf) {
100 // Single ref pred
101 if (rf[1] <= INTRA_FRAME) return -1;
102
103 // Bi-directional comp ref pred
104 if ((rf[0] < BWDREF_FRAME) && (rf[1] >= BWDREF_FRAME)) return -1;
105
106 for (int8_t ref_idx = 0; ref_idx < TOTAL_UNIDIR_COMP_REFS; ++ref_idx) {
107 if (rf[0] == comp_ref0(ref_idx) && rf[1] == comp_ref1(ref_idx))
108 return ref_idx;
109 }
110 return -1;
111 }
112
av1_ref_frame_type(const MV_REFERENCE_FRAME * const rf)113 static INLINE int8_t av1_ref_frame_type(const MV_REFERENCE_FRAME *const rf) {
114 if (rf[1] > INTRA_FRAME) {
115 const int8_t uni_comp_ref_idx = get_uni_comp_ref_idx(rf);
116 if (uni_comp_ref_idx >= 0) {
117 assert((REF_FRAMES + FWD_REFS * BWD_REFS + uni_comp_ref_idx) <
118 MODE_CTX_REF_FRAMES);
119 return REF_FRAMES + FWD_REFS * BWD_REFS + uni_comp_ref_idx;
120 } else {
121 return REF_FRAMES + FWD_RF_OFFSET(rf[0]) +
122 BWD_RF_OFFSET(rf[1]) * FWD_REFS;
123 }
124 }
125
126 return rf[0];
127 }
128
129 // clang-format off
130 static MV_REFERENCE_FRAME ref_frame_map[TOTAL_COMP_REFS][2] = {
131 { LAST_FRAME, BWDREF_FRAME }, { LAST2_FRAME, BWDREF_FRAME },
132 { LAST3_FRAME, BWDREF_FRAME }, { GOLDEN_FRAME, BWDREF_FRAME },
133
134 { LAST_FRAME, ALTREF2_FRAME }, { LAST2_FRAME, ALTREF2_FRAME },
135 { LAST3_FRAME, ALTREF2_FRAME }, { GOLDEN_FRAME, ALTREF2_FRAME },
136
137 { LAST_FRAME, ALTREF_FRAME }, { LAST2_FRAME, ALTREF_FRAME },
138 { LAST3_FRAME, ALTREF_FRAME }, { GOLDEN_FRAME, ALTREF_FRAME },
139
140 { LAST_FRAME, LAST2_FRAME }, { LAST_FRAME, LAST3_FRAME },
141 { LAST_FRAME, GOLDEN_FRAME }, { BWDREF_FRAME, ALTREF_FRAME },
142
143 // NOTE: Following reference frame pairs are not supported to be explicitly
144 // signalled, but they are possibly chosen by the use of skip_mode,
145 // which may use the most recent one-sided reference frame pair.
146 { LAST2_FRAME, LAST3_FRAME }, { LAST2_FRAME, GOLDEN_FRAME },
147 { LAST3_FRAME, GOLDEN_FRAME }, {BWDREF_FRAME, ALTREF2_FRAME},
148 { ALTREF2_FRAME, ALTREF_FRAME }
149 };
150 // clang-format on
151
av1_set_ref_frame(MV_REFERENCE_FRAME * rf,MV_REFERENCE_FRAME ref_frame_type)152 static INLINE void av1_set_ref_frame(MV_REFERENCE_FRAME *rf,
153 MV_REFERENCE_FRAME ref_frame_type) {
154 if (ref_frame_type >= REF_FRAMES) {
155 rf[0] = ref_frame_map[ref_frame_type - REF_FRAMES][0];
156 rf[1] = ref_frame_map[ref_frame_type - REF_FRAMES][1];
157 } else {
158 assert(ref_frame_type > NONE_FRAME);
159 rf[0] = ref_frame_type;
160 rf[1] = NONE_FRAME;
161 }
162 }
163
164 static uint16_t compound_mode_ctx_map[3][COMP_NEWMV_CTXS] = {
165 { 0, 1, 1, 1, 1 },
166 { 1, 2, 3, 4, 4 },
167 { 4, 4, 5, 6, 7 },
168 };
169
av1_mode_context_analyzer(const int16_t * const mode_context,const MV_REFERENCE_FRAME * const rf)170 static INLINE int16_t av1_mode_context_analyzer(
171 const int16_t *const mode_context, const MV_REFERENCE_FRAME *const rf) {
172 const int8_t ref_frame = av1_ref_frame_type(rf);
173
174 if (rf[1] <= INTRA_FRAME) return mode_context[ref_frame];
175
176 const int16_t newmv_ctx = mode_context[ref_frame] & NEWMV_CTX_MASK;
177 const int16_t refmv_ctx =
178 (mode_context[ref_frame] >> REFMV_OFFSET) & REFMV_CTX_MASK;
179
180 const int16_t comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][AOMMIN(
181 newmv_ctx, COMP_NEWMV_CTXS - 1)];
182 return comp_ctx;
183 }
184
av1_drl_ctx(const uint16_t * ref_mv_weight,int ref_idx)185 static INLINE uint8_t av1_drl_ctx(const uint16_t *ref_mv_weight, int ref_idx) {
186 if (ref_mv_weight[ref_idx] >= REF_CAT_LEVEL &&
187 ref_mv_weight[ref_idx + 1] >= REF_CAT_LEVEL)
188 return 0;
189
190 if (ref_mv_weight[ref_idx] >= REF_CAT_LEVEL &&
191 ref_mv_weight[ref_idx + 1] < REF_CAT_LEVEL)
192 return 1;
193
194 if (ref_mv_weight[ref_idx] < REF_CAT_LEVEL &&
195 ref_mv_weight[ref_idx + 1] < REF_CAT_LEVEL)
196 return 2;
197
198 return 0;
199 }
200
201 void av1_setup_frame_buf_refs(AV1_COMMON *cm);
202 void av1_setup_frame_sign_bias(AV1_COMMON *cm);
203 void av1_setup_skip_mode_allowed(AV1_COMMON *cm);
204 void av1_setup_motion_field(AV1_COMMON *cm);
205 void av1_set_frame_refs(AV1_COMMON *const cm, int *remapped_ref_idx,
206 int lst_map_idx, int gld_map_idx);
207
av1_collect_neighbors_ref_counts(MACROBLOCKD * const xd)208 static INLINE void av1_collect_neighbors_ref_counts(MACROBLOCKD *const xd) {
209 av1_zero(xd->neighbors_ref_counts);
210
211 uint8_t *const ref_counts = xd->neighbors_ref_counts;
212
213 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
214 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
215 const int above_in_image = xd->up_available;
216 const int left_in_image = xd->left_available;
217
218 // Above neighbor
219 if (above_in_image && is_inter_block(above_mbmi)) {
220 ref_counts[above_mbmi->ref_frame[0]]++;
221 if (has_second_ref(above_mbmi)) {
222 ref_counts[above_mbmi->ref_frame[1]]++;
223 }
224 }
225
226 // Left neighbor
227 if (left_in_image && is_inter_block(left_mbmi)) {
228 ref_counts[left_mbmi->ref_frame[0]]++;
229 if (has_second_ref(left_mbmi)) {
230 ref_counts[left_mbmi->ref_frame[1]]++;
231 }
232 }
233 }
234
235 void av1_copy_frame_mvs(const AV1_COMMON *const cm,
236 const MB_MODE_INFO *const mi, int mi_row, int mi_col,
237 int x_mis, int y_mis);
238
239 // The global_mvs output parameter points to an array of REF_FRAMES elements.
240 // The caller may pass a null global_mvs if it does not need the global_mvs
241 // output.
242 void av1_find_mv_refs(const AV1_COMMON *cm, const MACROBLOCKD *xd,
243 MB_MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
244 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES],
245 CANDIDATE_MV ref_mv_stack[][MAX_REF_MV_STACK_SIZE],
246 uint16_t ref_mv_weight[][MAX_REF_MV_STACK_SIZE],
247 int_mv mv_ref_list[][MAX_MV_REF_CANDIDATES],
248 int_mv *global_mvs, int16_t *mode_context);
249
250 // check a list of motion vectors by sad score using a number rows of pixels
251 // above and a number cols of pixels in the left to select the one with best
252 // score to use as ref motion vector
253 void av1_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *nearest_mv,
254 int_mv *near_mv, int is_integer);
255
256 uint8_t av1_selectSamples(MV *mv, int *pts, int *pts_inref, int len,
257 BLOCK_SIZE bsize);
258 uint8_t av1_findSamples(const AV1_COMMON *cm, MACROBLOCKD *xd, int *pts,
259 int *pts_inref);
260
261 #define INTRABC_DELAY_PIXELS 256 // Delay of 256 pixels
262 #define INTRABC_DELAY_SB64 (INTRABC_DELAY_PIXELS / 64)
263
av1_find_ref_dv(int_mv * ref_dv,const TileInfo * const tile,int mib_size,int mi_row)264 static INLINE void av1_find_ref_dv(int_mv *ref_dv, const TileInfo *const tile,
265 int mib_size, int mi_row) {
266 if (mi_row - mib_size < tile->mi_row_start) {
267 ref_dv->as_fullmv.row = 0;
268 ref_dv->as_fullmv.col = -MI_SIZE * mib_size - INTRABC_DELAY_PIXELS;
269 } else {
270 ref_dv->as_fullmv.row = -MI_SIZE * mib_size;
271 ref_dv->as_fullmv.col = 0;
272 }
273 convert_fullmv_to_mv(ref_dv);
274 }
275
av1_is_dv_valid(const MV dv,const AV1_COMMON * cm,const MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize,int mib_size_log2)276 static INLINE int av1_is_dv_valid(const MV dv, const AV1_COMMON *cm,
277 const MACROBLOCKD *xd, int mi_row, int mi_col,
278 BLOCK_SIZE bsize, int mib_size_log2) {
279 const int bw = block_size_wide[bsize];
280 const int bh = block_size_high[bsize];
281 const int SCALE_PX_TO_MV = 8;
282 // Disallow subpixel for now
283 // SUBPEL_MASK is not the correct scale
284 if (((dv.row & (SCALE_PX_TO_MV - 1)) || (dv.col & (SCALE_PX_TO_MV - 1))))
285 return 0;
286
287 const TileInfo *const tile = &xd->tile;
288 // Is the source top-left inside the current tile?
289 const int src_top_edge = mi_row * MI_SIZE * SCALE_PX_TO_MV + dv.row;
290 const int tile_top_edge = tile->mi_row_start * MI_SIZE * SCALE_PX_TO_MV;
291 if (src_top_edge < tile_top_edge) return 0;
292 const int src_left_edge = mi_col * MI_SIZE * SCALE_PX_TO_MV + dv.col;
293 const int tile_left_edge = tile->mi_col_start * MI_SIZE * SCALE_PX_TO_MV;
294 if (src_left_edge < tile_left_edge) return 0;
295 // Is the bottom right inside the current tile?
296 const int src_bottom_edge = (mi_row * MI_SIZE + bh) * SCALE_PX_TO_MV + dv.row;
297 const int tile_bottom_edge = tile->mi_row_end * MI_SIZE * SCALE_PX_TO_MV;
298 if (src_bottom_edge > tile_bottom_edge) return 0;
299 const int src_right_edge = (mi_col * MI_SIZE + bw) * SCALE_PX_TO_MV + dv.col;
300 const int tile_right_edge = tile->mi_col_end * MI_SIZE * SCALE_PX_TO_MV;
301 if (src_right_edge > tile_right_edge) return 0;
302
303 // Special case for sub 8x8 chroma cases, to prevent referring to chroma
304 // pixels outside current tile.
305 if (xd->is_chroma_ref && av1_num_planes(cm) > 1) {
306 const struct macroblockd_plane *const pd = &xd->plane[1];
307 if (bw < 8 && pd->subsampling_x)
308 if (src_left_edge < tile_left_edge + 4 * SCALE_PX_TO_MV) return 0;
309 if (bh < 8 && pd->subsampling_y)
310 if (src_top_edge < tile_top_edge + 4 * SCALE_PX_TO_MV) return 0;
311 }
312
313 // Is the bottom right within an already coded SB? Also consider additional
314 // constraints to facilitate HW decoder.
315 const int max_mib_size = 1 << mib_size_log2;
316 const int active_sb_row = mi_row >> mib_size_log2;
317 const int active_sb64_col = (mi_col * MI_SIZE) >> 6;
318 const int sb_size = max_mib_size * MI_SIZE;
319 const int src_sb_row = ((src_bottom_edge >> 3) - 1) / sb_size;
320 const int src_sb64_col = ((src_right_edge >> 3) - 1) >> 6;
321 const int total_sb64_per_row =
322 ((tile->mi_col_end - tile->mi_col_start - 1) >> 4) + 1;
323 const int active_sb64 = active_sb_row * total_sb64_per_row + active_sb64_col;
324 const int src_sb64 = src_sb_row * total_sb64_per_row + src_sb64_col;
325 if (src_sb64 >= active_sb64 - INTRABC_DELAY_SB64) return 0;
326
327 // Wavefront constraint: use only top left area of frame for reference.
328 const int gradient = 1 + INTRABC_DELAY_SB64 + (sb_size > 64);
329 const int wf_offset = gradient * (active_sb_row - src_sb_row);
330 if (src_sb_row > active_sb_row ||
331 src_sb64_col >= active_sb64_col - INTRABC_DELAY_SB64 + wf_offset)
332 return 0;
333
334 return 1;
335 }
336
337 #ifdef __cplusplus
338 } // extern "C"
339 #endif
340
341 #endif // AOM_AV1_COMMON_MVREF_COMMON_H_
342