blob: b65509ac36bd6f5902be7b21a9ebfa50827b1a36 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Yaowu Xuf883b422016-08-30 14:01:10 -070010#ifndef AV1_COMMON_MVREF_COMMON_H_
11#define AV1_COMMON_MVREF_COMMON_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070012
13#include "av1/common/onyxc_int.h"
14#include "av1/common/blockd.h"
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20#define MVREF_NEIGHBOURS 8
21
22typedef struct position {
23 int row;
24 int col;
25} POSITION;
26
27typedef enum {
28 BOTH_ZERO = 0,
29 ZERO_PLUS_PREDICTED = 1,
30 BOTH_PREDICTED = 2,
31 NEW_PLUS_NON_INTRA = 3,
32 BOTH_NEW = 4,
33 INTRA_PLUS_NON_INTRA = 5,
34 BOTH_INTRA = 6,
35 INVALID_CASE = 9
36} motion_vector_context;
37
38// This is used to figure out a context for the ref blocks. The code flattens
39// an array that would have 3 possible counts (0, 1 & 2) for 3 choices by
40// adding 9 for each intra block, 3 for each zero mv and 1 for each new
41// motion vector. This single number is then converted into a context
42// with a single lookup ( counter_to_context ).
43static const int mode_2_counter[MB_MODE_COUNT] = {
44 9, // DC_PRED
45 9, // V_PRED
46 9, // H_PRED
47 9, // D45_PRED
48 9, // D135_PRED
49 9, // D117_PRED
50 9, // D153_PRED
51 9, // D207_PRED
52 9, // D63_PRED
53 9, // TM_PRED
54 0, // NEARESTMV
55 0, // NEARMV
56 3, // ZEROMV
57 1, // NEWMV
58#if CONFIG_EXT_INTER
59 1, // NEWFROMNEARMV
60 0, // NEAREST_NEARESTMV
61 0, // NEAREST_NEARMV
62 0, // NEAR_NEARESTMV
63 0, // NEAR_NEARMV
64 1, // NEAREST_NEWMV
65 1, // NEW_NEARESTMV
66 1, // NEAR_NEWMV
67 1, // NEW_NEARMV
68 3, // ZERO_ZEROMV
69 1, // NEW_NEWMV
70#endif // CONFIG_EXT_INTER
71};
72
73// There are 3^3 different combinations of 3 counts that can be either 0,1 or
74// 2. However the actual count can never be greater than 2 so the highest
75// counter we need is 18. 9 is an invalid counter that's never used.
76static const int counter_to_context[19] = {
77 BOTH_PREDICTED, // 0
78 NEW_PLUS_NON_INTRA, // 1
79 BOTH_NEW, // 2
80 ZERO_PLUS_PREDICTED, // 3
81 NEW_PLUS_NON_INTRA, // 4
82 INVALID_CASE, // 5
83 BOTH_ZERO, // 6
84 INVALID_CASE, // 7
85 INVALID_CASE, // 8
86 INTRA_PLUS_NON_INTRA, // 9
87 INTRA_PLUS_NON_INTRA, // 10
88 INVALID_CASE, // 11
89 INTRA_PLUS_NON_INTRA, // 12
90 INVALID_CASE, // 13
91 INVALID_CASE, // 14
92 INVALID_CASE, // 15
93 INVALID_CASE, // 16
94 INVALID_CASE, // 17
95 BOTH_INTRA // 18
96};
97
98static const POSITION mv_ref_blocks[BLOCK_SIZES][MVREF_NEIGHBOURS] = {
99 // 4X4
100 { { -1, 0 },
101 { 0, -1 },
102 { -1, -1 },
103 { -2, 0 },
104 { 0, -2 },
105 { -2, -1 },
106 { -1, -2 },
107 { -2, -2 } },
108 // 4X8
109 { { -1, 0 },
110 { 0, -1 },
111 { -1, -1 },
112 { -2, 0 },
113 { 0, -2 },
114 { -2, -1 },
115 { -1, -2 },
116 { -2, -2 } },
117 // 8X4
118 { { -1, 0 },
119 { 0, -1 },
120 { -1, -1 },
121 { -2, 0 },
122 { 0, -2 },
123 { -2, -1 },
124 { -1, -2 },
125 { -2, -2 } },
126 // 8X8
127 { { -1, 0 },
128 { 0, -1 },
129 { -1, -1 },
130 { -2, 0 },
131 { 0, -2 },
132 { -2, -1 },
133 { -1, -2 },
134 { -2, -2 } },
135 // 8X16
136 { { 0, -1 },
137 { -1, 0 },
138 { 1, -1 },
139 { -1, -1 },
140 { 0, -2 },
141 { -2, 0 },
142 { -2, -1 },
143 { -1, -2 } },
144 // 16X8
145 { { -1, 0 },
146 { 0, -1 },
147 { -1, 1 },
148 { -1, -1 },
149 { -2, 0 },
150 { 0, -2 },
151 { -1, -2 },
152 { -2, -1 } },
153 // 16X16
154 { { -1, 0 },
155 { 0, -1 },
156 { -1, 1 },
157 { 1, -1 },
158 { -1, -1 },
159 { -3, 0 },
160 { 0, -3 },
161 { -3, -3 } },
162 // 16X32
163 { { 0, -1 },
164 { -1, 0 },
165 { 2, -1 },
166 { -1, -1 },
167 { -1, 1 },
168 { 0, -3 },
169 { -3, 0 },
170 { -3, -3 } },
171 // 32X16
172 { { -1, 0 },
173 { 0, -1 },
174 { -1, 2 },
175 { -1, -1 },
176 { 1, -1 },
177 { -3, 0 },
178 { 0, -3 },
179 { -3, -3 } },
180 // 32X32
181 { { -1, 1 },
182 { 1, -1 },
183 { -1, 2 },
184 { 2, -1 },
185 { -1, -1 },
186 { -3, 0 },
187 { 0, -3 },
188 { -3, -3 } },
189 // 32X64
190 { { 0, -1 },
191 { -1, 0 },
192 { 4, -1 },
193 { -1, 2 },
194 { -1, -1 },
195 { 0, -3 },
196 { -3, 0 },
197 { 2, -1 } },
198 // 64X32
199 { { -1, 0 },
200 { 0, -1 },
201 { -1, 4 },
202 { 2, -1 },
203 { -1, -1 },
204 { -3, 0 },
205 { 0, -3 },
206 { -1, 2 } },
207 // 64X64
208 { { -1, 3 },
209 { 3, -1 },
210 { -1, 4 },
211 { 4, -1 },
212 { -1, -1 },
213 { -1, 0 },
214 { 0, -1 },
215 { -1, 6 } },
216#if CONFIG_EXT_PARTITION
217 // TODO(debargha/jingning) Making them twice the 32x64, .. ones above
218 // 64x128
219 { { 0, -2 },
220 { -2, 0 },
221 { 8, -2 },
222 { -2, 4 },
223 { -2, -2 },
224 { 0, -6 },
225 { -6, 0 },
226 { 4, -2 } },
227 // 128x64
228 { { -2, 0 },
229 { 0, -2 },
230 { -2, 8 },
231 { 4, -2 },
232 { -2, -2 },
233 { -6, 0 },
234 { 0, -6 },
235 { -2, 4 } },
236 // 128x128
237 { { -2, 6 },
238 { 6, -2 },
239 { -2, 8 },
240 { 8, -2 },
241 { -2, -2 },
242 { -2, 0 },
243 { 0, -2 },
244 { -2, 12 } },
245#endif // CONFIG_EXT_PARTITION
246};
247
248static const int idx_n_column_to_subblock[4][2] = {
249 { 1, 2 }, { 1, 3 }, { 3, 2 }, { 3, 3 }
250};
251
252// clamp_mv_ref
253#if CONFIG_EXT_PARTITION
254#define MV_BORDER (16 << 3) // Allow 16 pels in 1/8th pel units
255#else
256#define MV_BORDER (8 << 3) // Allow 8 pels in 1/8th pel units
257#endif // CONFIG_EXT_PARTITION
258
259static INLINE void clamp_mv_ref(MV *mv, int bw, int bh, const MACROBLOCKD *xd) {
260 clamp_mv(mv, xd->mb_to_left_edge - bw * 8 - MV_BORDER,
261 xd->mb_to_right_edge + bw * 8 + MV_BORDER,
262 xd->mb_to_top_edge - bh * 8 - MV_BORDER,
263 xd->mb_to_bottom_edge + bh * 8 + MV_BORDER);
264}
265
266// This function returns either the appropriate sub block or block's mv
267// on whether the block_size < 8x8 and we have check_sub_blocks set.
268static INLINE int_mv get_sub_block_mv(const MODE_INFO *candidate, int which_mv,
269 int search_col, int block_idx) {
270 return block_idx >= 0 && candidate->mbmi.sb_type < BLOCK_8X8
271 ? candidate
272 ->bmi[idx_n_column_to_subblock[block_idx][search_col == 0]]
273 .as_mv[which_mv]
274 : candidate->mbmi.mv[which_mv];
275}
276
277#if CONFIG_REF_MV
278static INLINE int_mv get_sub_block_pred_mv(const MODE_INFO *candidate,
279 int which_mv, int search_col,
280 int block_idx) {
281 return block_idx >= 0 && candidate->mbmi.sb_type < BLOCK_8X8
282 ? candidate
283 ->bmi[idx_n_column_to_subblock[block_idx][search_col == 0]]
284 .pred_mv_s8[which_mv]
285 : candidate->mbmi.pred_mv[which_mv];
286}
287#endif
288
289// Performs mv sign inversion if indicated by the reference frame combination.
290static INLINE int_mv scale_mv(const MB_MODE_INFO *mbmi, int ref,
291 const MV_REFERENCE_FRAME this_ref_frame,
292 const int *ref_sign_bias) {
293 int_mv mv = mbmi->mv[ref];
294 if (ref_sign_bias[mbmi->ref_frame[ref]] != ref_sign_bias[this_ref_frame]) {
295 mv.as_mv.row *= -1;
296 mv.as_mv.col *= -1;
297 }
298 return mv;
299}
300
301#define CLIP_IN_ADD(mv, bw, bh, xd) clamp_mv_ref(mv, bw, bh, xd)
302
303// This macro is used to add a motion vector mv_ref list if it isn't
304// already in the list. If it's the second motion vector it will also
305// skip all additional processing and jump to done!
306#define ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, bw, bh, xd, Done) \
307 do { \
308 (mv_ref_list)[(refmv_count)] = (mv); \
309 CLIP_IN_ADD(&(mv_ref_list)[(refmv_count)].as_mv, (bw), (bh), (xd)); \
310 if (refmv_count && (mv_ref_list)[1].as_int != (mv_ref_list)[0].as_int) { \
311 (refmv_count) = 2; \
312 goto Done; \
313 } \
314 (refmv_count) = 1; \
315 } while (0)
316
317// If either reference frame is different, not INTRA, and they
318// are different from each other scale and add the mv to our list.
319#define IF_DIFF_REF_FRAME_ADD_MV(mbmi, ref_frame, ref_sign_bias, refmv_count, \
320 mv_ref_list, bw, bh, xd, Done) \
321 do { \
322 if (is_inter_block(mbmi)) { \
323 if ((mbmi)->ref_frame[0] != ref_frame) \
324 ADD_MV_REF_LIST(scale_mv((mbmi), 0, ref_frame, ref_sign_bias), \
325 refmv_count, mv_ref_list, bw, bh, xd, Done); \
326 if (has_second_ref(mbmi) && (mbmi)->ref_frame[1] != ref_frame) \
327 ADD_MV_REF_LIST(scale_mv((mbmi), 1, ref_frame, ref_sign_bias), \
328 refmv_count, mv_ref_list, bw, bh, xd, Done); \
329 } \
330 } while (0)
331
332// Checks that the given mi_row, mi_col and search point
333// are inside the borders of the tile.
334static INLINE int is_inside(const TileInfo *const tile, int mi_col, int mi_row,
335 const POSITION *mi_pos) {
336 return !(mi_row + mi_pos->row < tile->mi_row_start ||
337 mi_col + mi_pos->col < tile->mi_col_start ||
338 mi_row + mi_pos->row >= tile->mi_row_end ||
339 mi_col + mi_pos->col >= tile->mi_col_end);
340}
341
342static INLINE void lower_mv_precision(MV *mv, int allow_hp) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700343 const int use_hp = allow_hp && av1_use_mv_hp(mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344 if (!use_hp) {
345 if (mv->row & 1) mv->row += (mv->row > 0 ? -1 : 1);
346 if (mv->col & 1) mv->col += (mv->col > 0 ? -1 : 1);
347 }
348}
349
350#if CONFIG_REF_MV
Yaowu Xuf883b422016-08-30 14:01:10 -0700351static INLINE int av1_nmv_ctx(const uint8_t ref_mv_count,
352 const CANDIDATE_MV *ref_mv_stack) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700353#if CONFIG_EXT_INTER
354 return 0;
355#endif
356 if (ref_mv_stack[0].weight > REF_CAT_LEVEL && ref_mv_count > 0) {
357 if (abs(ref_mv_stack[0].this_mv.as_mv.row -
358 ref_mv_stack[0].pred_mv.as_mv.row) <= 4 &&
359 abs(ref_mv_stack[0].this_mv.as_mv.col -
360 ref_mv_stack[0].pred_mv.as_mv.col) <= 4)
361 return 2;
362 else
363 return 1;
364 }
365 return 0;
366}
367
Yaowu Xuf883b422016-08-30 14:01:10 -0700368static INLINE int8_t av1_ref_frame_type(const MV_REFERENCE_FRAME *const rf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700369 if (rf[1] > INTRA_FRAME) {
370 return TOTAL_REFS_PER_FRAME + FWD_RF_OFFSET(rf[0]) +
371 BWD_RF_OFFSET(rf[1]) * FWD_REFS;
372 }
373
374 return rf[0];
375}
376
377static MV_REFERENCE_FRAME ref_frame_map[COMP_REFS][2] = {
378#if CONFIG_EXT_REFS
379 { LAST_FRAME, BWDREF_FRAME }, { LAST2_FRAME, BWDREF_FRAME },
380 { LAST3_FRAME, BWDREF_FRAME }, { GOLDEN_FRAME, BWDREF_FRAME },
381
382 { LAST_FRAME, ALTREF_FRAME }, { LAST2_FRAME, ALTREF_FRAME },
383 { LAST3_FRAME, ALTREF_FRAME }, { GOLDEN_FRAME, ALTREF_FRAME }
384#else
385 { LAST_FRAME, ALTREF_FRAME }, { GOLDEN_FRAME, ALTREF_FRAME }
386#endif
387};
388
Yaowu Xuf883b422016-08-30 14:01:10 -0700389static INLINE void av1_set_ref_frame(MV_REFERENCE_FRAME *rf,
390 int8_t ref_frame_type) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700391 if (ref_frame_type >= TOTAL_REFS_PER_FRAME) {
392 rf[0] = ref_frame_map[ref_frame_type - TOTAL_REFS_PER_FRAME][0];
393 rf[1] = ref_frame_map[ref_frame_type - TOTAL_REFS_PER_FRAME][1];
394 } else {
395 rf[0] = ref_frame_type;
396 rf[1] = NONE;
397 assert(ref_frame_type > INTRA_FRAME &&
398 ref_frame_type < TOTAL_REFS_PER_FRAME);
399 }
400}
401
Yaowu Xuf883b422016-08-30 14:01:10 -0700402static INLINE int16_t av1_mode_context_analyzer(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700403 const int16_t *const mode_context, const MV_REFERENCE_FRAME *const rf,
404 BLOCK_SIZE bsize, int block) {
405 int16_t mode_ctx = 0;
406 if (block >= 0) {
407 mode_ctx = mode_context[rf[0]] & 0x00ff;
408
409 if (block > 0 && bsize < BLOCK_8X8 && bsize > BLOCK_4X4)
410 mode_ctx |= (1 << SKIP_NEARESTMV_SUB8X8_OFFSET);
411
412 return mode_ctx;
413 }
414
415 if (rf[1] > INTRA_FRAME)
416 return mode_context[rf[0]] & (mode_context[rf[1]] | 0x00ff);
417 else if (rf[0] != ALTREF_FRAME)
418 return mode_context[rf[0]] & ~(mode_context[ALTREF_FRAME] & 0xfe00);
419 else
420 return mode_context[rf[0]];
421}
422
Yaowu Xuf883b422016-08-30 14:01:10 -0700423static INLINE uint8_t av1_drl_ctx(const CANDIDATE_MV *ref_mv_stack,
424 int ref_idx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700425 if (ref_mv_stack[ref_idx].weight >= REF_CAT_LEVEL &&
426 ref_mv_stack[ref_idx + 1].weight >= REF_CAT_LEVEL) {
427 if (ref_mv_stack[ref_idx].weight == ref_mv_stack[ref_idx + 1].weight)
428 return 0;
429 else
430 return 1;
431 }
432
433 if (ref_mv_stack[ref_idx].weight >= REF_CAT_LEVEL &&
434 ref_mv_stack[ref_idx + 1].weight < REF_CAT_LEVEL)
435 return 2;
436
437 if (ref_mv_stack[ref_idx].weight < REF_CAT_LEVEL &&
438 ref_mv_stack[ref_idx + 1].weight < REF_CAT_LEVEL) {
439 if (ref_mv_stack[ref_idx].weight == ref_mv_stack[ref_idx + 1].weight)
440 return 3;
441 else
442 return 4;
443 }
444
445 return 0;
446}
447#endif
448
449typedef void (*find_mv_refs_sync)(void *const data, int mi_row);
Yaowu Xuf883b422016-08-30 14:01:10 -0700450void av1_find_mv_refs(const AV1_COMMON *cm, const MACROBLOCKD *xd,
451 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700452#if CONFIG_REF_MV
Yaowu Xuf883b422016-08-30 14:01:10 -0700453 uint8_t *ref_mv_count, CANDIDATE_MV *ref_mv_stack,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700454#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700455 int16_t *compound_mode_context,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700456#endif // CONFIG_EXT_INTER
457#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700458 int_mv *mv_ref_list, int mi_row, int mi_col,
459 find_mv_refs_sync sync, void *const data,
460 int16_t *mode_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461
462// check a list of motion vectors by sad score using a number rows of pixels
463// above and a number cols of pixels in the left to select the one with best
464// score to use as ref motion vector
Yaowu Xuf883b422016-08-30 14:01:10 -0700465void av1_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *nearest_mv,
466 int_mv *near_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700467
Yaowu Xuf883b422016-08-30 14:01:10 -0700468void av1_append_sub8x8_mvs_for_idx(AV1_COMMON *cm, MACROBLOCKD *xd, int block,
469 int ref, int mi_row, int mi_col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700470#if CONFIG_REF_MV
Yaowu Xuf883b422016-08-30 14:01:10 -0700471 CANDIDATE_MV *ref_mv_stack,
472 uint8_t *ref_mv_count,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473#endif
474#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700475 int_mv *mv_list,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700476#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700477 int_mv *nearest_mv, int_mv *near_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700478
479#if CONFIG_EXT_INTER
480// This function keeps a mode count for a given MB/SB
Yaowu Xuf883b422016-08-30 14:01:10 -0700481void av1_update_mv_context(const MACROBLOCKD *xd, MODE_INFO *mi,
482 MV_REFERENCE_FRAME ref_frame, int_mv *mv_ref_list,
483 int block, int mi_row, int mi_col,
484 int16_t *mode_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700485#endif // CONFIG_EXT_INTER
486
487#ifdef __cplusplus
488} // extern "C"
489#endif
490
Yaowu Xuf883b422016-08-30 14:01:10 -0700491#endif // AV1_COMMON_MVREF_COMMON_H_