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