/src/aom/av1/common/mvref_common.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
12 | | #include <stdlib.h> |
13 | | |
14 | | #include "av1/common/mvref_common.h" |
15 | | #include "av1/common/warped_motion.h" |
16 | | |
17 | | // Although we assign 32 bit integers, all the values are strictly under 14 |
18 | | // bits. |
19 | | static const int div_mult[32] = { 0, 16384, 8192, 5461, 4096, 3276, 2730, |
20 | | 2340, 2048, 1820, 1638, 1489, 1365, 1260, |
21 | | 1170, 1092, 1024, 963, 910, 862, 819, |
22 | | 780, 744, 712, 682, 655, 630, 606, |
23 | | 585, 564, 546, 528 }; |
24 | | |
25 | | // TODO(jingning): Consider the use of lookup table for (num / den) |
26 | | // altogether. |
27 | 16.5k | static inline void get_mv_projection(MV *output, MV ref, int num, int den) { |
28 | 16.5k | den = AOMMIN(den, MAX_FRAME_DISTANCE); |
29 | 16.5k | num = num > 0 ? AOMMIN(num, MAX_FRAME_DISTANCE) |
30 | 16.5k | : AOMMAX(num, -MAX_FRAME_DISTANCE); |
31 | 16.5k | const int mv_row = |
32 | 16.5k | ROUND_POWER_OF_TWO_SIGNED(ref.row * num * div_mult[den], 14); |
33 | 16.5k | const int mv_col = |
34 | 16.5k | ROUND_POWER_OF_TWO_SIGNED(ref.col * num * div_mult[den], 14); |
35 | 16.5k | const int clamp_max = MV_UPP - 1; |
36 | 16.5k | const int clamp_min = MV_LOW + 1; |
37 | 16.5k | output->row = (int16_t)clamp(mv_row, clamp_min, clamp_max); |
38 | 16.5k | output->col = (int16_t)clamp(mv_col, clamp_min, clamp_max); |
39 | 16.5k | } |
40 | | |
41 | | void av1_copy_frame_mvs(const AV1_COMMON *const cm, |
42 | | const MB_MODE_INFO *const mi, int mi_row, int mi_col, |
43 | 39.0k | int x_mis, int y_mis) { |
44 | 39.0k | const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_params.mi_cols, 1); |
45 | 39.0k | MV_REF *frame_mvs = |
46 | 39.0k | cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1); |
47 | 39.0k | x_mis = ROUND_POWER_OF_TWO(x_mis, 1); |
48 | 39.0k | y_mis = ROUND_POWER_OF_TWO(y_mis, 1); |
49 | 39.0k | int w, h; |
50 | | |
51 | 79.2k | for (h = 0; h < y_mis; h++) { |
52 | 40.1k | MV_REF *mv = frame_mvs; |
53 | 97.2k | for (w = 0; w < x_mis; w++) { |
54 | 57.0k | mv->ref_frame = NONE_FRAME; |
55 | 57.0k | mv->mv.as_int = 0; |
56 | | |
57 | 171k | for (int idx = 0; idx < 2; ++idx) { |
58 | 114k | MV_REFERENCE_FRAME ref_frame = mi->ref_frame[idx]; |
59 | 114k | if (ref_frame > INTRA_FRAME) { |
60 | 68.1k | int8_t ref_idx = cm->ref_frame_side[ref_frame]; |
61 | 68.1k | if (ref_idx) continue; |
62 | 33.0k | if ((abs(mi->mv[idx].as_mv.row) > REFMVS_LIMIT) || |
63 | 33.0k | (abs(mi->mv[idx].as_mv.col) > REFMVS_LIMIT)) |
64 | 238 | continue; |
65 | 32.7k | mv->ref_frame = ref_frame; |
66 | 32.7k | mv->mv.as_int = mi->mv[idx].as_int; |
67 | 32.7k | } |
68 | 114k | } |
69 | 57.0k | mv++; |
70 | 57.0k | } |
71 | 40.1k | frame_mvs += frame_mvs_stride; |
72 | 40.1k | } |
73 | 39.0k | } |
74 | | |
75 | | static inline void add_ref_mv_candidate( |
76 | | const MB_MODE_INFO *const candidate, const MV_REFERENCE_FRAME rf[2], |
77 | | uint8_t *refmv_count, uint8_t *ref_match_count, uint8_t *newmv_count, |
78 | | CANDIDATE_MV *ref_mv_stack, uint16_t *ref_mv_weight, |
79 | | int_mv *gm_mv_candidates, const WarpedMotionParams *gm_params, |
80 | 1.33M | uint16_t weight) { |
81 | 1.33M | if (!is_inter_block(candidate)) return; |
82 | 833k | assert(weight % 2 == 0); |
83 | 833k | int index, ref; |
84 | | |
85 | 833k | if (rf[1] == NONE_FRAME) { |
86 | | // single reference frame |
87 | 2.42M | for (ref = 0; ref < 2; ++ref) { |
88 | 1.61M | if (candidate->ref_frame[ref] == rf[0]) { |
89 | 776k | const int is_gm_block = |
90 | 776k | is_global_mv_block(candidate, gm_params[rf[0]].wmtype); |
91 | 776k | const int_mv this_refmv = |
92 | 776k | is_gm_block ? gm_mv_candidates[0] : get_block_mv(candidate, ref); |
93 | 1.77M | for (index = 0; index < *refmv_count; ++index) { |
94 | 1.22M | if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int) { |
95 | 221k | ref_mv_weight[index] += weight; |
96 | 221k | break; |
97 | 221k | } |
98 | 1.22M | } |
99 | | |
100 | | // Add a new item to the list. |
101 | 776k | if (index == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) { |
102 | 550k | ref_mv_stack[index].this_mv = this_refmv; |
103 | 550k | ref_mv_weight[index] = weight; |
104 | 550k | ++(*refmv_count); |
105 | 550k | } |
106 | 776k | if (have_newmv_in_inter_mode(candidate->mode)) ++*newmv_count; |
107 | 776k | ++*ref_match_count; |
108 | 776k | } |
109 | 1.61M | } |
110 | 808k | } else { |
111 | | // compound reference frame |
112 | 24.7k | if (candidate->ref_frame[0] == rf[0] && candidate->ref_frame[1] == rf[1]) { |
113 | 8.04k | int_mv this_refmv[2]; |
114 | | |
115 | 24.1k | for (ref = 0; ref < 2; ++ref) { |
116 | 16.0k | if (is_global_mv_block(candidate, gm_params[rf[ref]].wmtype)) |
117 | 1.84k | this_refmv[ref] = gm_mv_candidates[ref]; |
118 | 14.2k | else |
119 | 14.2k | this_refmv[ref] = get_block_mv(candidate, ref); |
120 | 16.0k | } |
121 | | |
122 | 10.3k | for (index = 0; index < *refmv_count; ++index) { |
123 | 4.23k | if ((ref_mv_stack[index].this_mv.as_int == this_refmv[0].as_int) && |
124 | 4.23k | (ref_mv_stack[index].comp_mv.as_int == this_refmv[1].as_int)) { |
125 | 1.88k | ref_mv_weight[index] += weight; |
126 | 1.88k | break; |
127 | 1.88k | } |
128 | 4.23k | } |
129 | | |
130 | | // Add a new item to the list. |
131 | 8.04k | if (index == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) { |
132 | 6.15k | ref_mv_stack[index].this_mv = this_refmv[0]; |
133 | 6.15k | ref_mv_stack[index].comp_mv = this_refmv[1]; |
134 | 6.15k | ref_mv_weight[index] = weight; |
135 | 6.15k | ++(*refmv_count); |
136 | 6.15k | } |
137 | 8.04k | if (have_newmv_in_inter_mode(candidate->mode)) ++*newmv_count; |
138 | 8.04k | ++*ref_match_count; |
139 | 8.04k | } |
140 | 24.7k | } |
141 | 833k | } |
142 | | |
143 | | static inline void scan_row_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
144 | | int mi_col, const MV_REFERENCE_FRAME rf[2], |
145 | | int row_offset, CANDIDATE_MV *ref_mv_stack, |
146 | | uint16_t *ref_mv_weight, uint8_t *refmv_count, |
147 | | uint8_t *ref_match_count, uint8_t *newmv_count, |
148 | | int_mv *gm_mv_candidates, int max_row_offset, |
149 | 438k | int *processed_rows) { |
150 | 438k | int end_mi = AOMMIN(xd->width, cm->mi_params.mi_cols - mi_col); |
151 | 438k | end_mi = AOMMIN(end_mi, mi_size_wide[BLOCK_64X64]); |
152 | 438k | const int width_8x8 = mi_size_wide[BLOCK_8X8]; |
153 | 438k | const int width_16x16 = mi_size_wide[BLOCK_16X16]; |
154 | 438k | int col_offset = 0; |
155 | | // TODO(jingning): Revisit this part after cb4x4 is stable. |
156 | 438k | if (abs(row_offset) > 1) { |
157 | 234k | col_offset = 1; |
158 | 234k | if ((mi_col & 0x01) && xd->width < width_8x8) --col_offset; |
159 | 234k | } |
160 | 438k | const int use_step_16 = (xd->width >= 16); |
161 | 438k | MB_MODE_INFO **const candidate_mi0 = xd->mi + row_offset * xd->mi_stride; |
162 | | |
163 | 927k | for (int i = 0; i < end_mi;) { |
164 | 488k | const MB_MODE_INFO *const candidate = candidate_mi0[col_offset + i]; |
165 | 488k | const int candidate_bsize = candidate->bsize; |
166 | 488k | const int n4_w = mi_size_wide[candidate_bsize]; |
167 | 488k | int len = AOMMIN(xd->width, n4_w); |
168 | 488k | if (use_step_16) |
169 | 7.37k | len = AOMMAX(width_16x16, len); |
170 | 481k | else if (abs(row_offset) > 1) |
171 | 259k | len = AOMMAX(len, width_8x8); |
172 | | |
173 | 488k | uint16_t weight = 2; |
174 | 488k | if (xd->width >= width_8x8 && xd->width <= n4_w) { |
175 | 260k | uint16_t inc = AOMMIN(-max_row_offset + row_offset + 1, |
176 | 260k | mi_size_high[candidate_bsize]); |
177 | | // Obtain range used in weight calculation. |
178 | 260k | weight = AOMMAX(weight, inc); |
179 | | // Update processed rows. |
180 | 260k | *processed_rows = inc - row_offset - 1; |
181 | 260k | } |
182 | | |
183 | 488k | add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count, |
184 | 488k | newmv_count, ref_mv_stack, ref_mv_weight, |
185 | 488k | gm_mv_candidates, cm->global_motion, len * weight); |
186 | | |
187 | 488k | i += len; |
188 | 488k | } |
189 | 438k | } |
190 | | |
191 | | static inline void scan_col_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
192 | | int mi_row, const MV_REFERENCE_FRAME rf[2], |
193 | | int col_offset, CANDIDATE_MV *ref_mv_stack, |
194 | | uint16_t *ref_mv_weight, uint8_t *refmv_count, |
195 | | uint8_t *ref_match_count, uint8_t *newmv_count, |
196 | | int_mv *gm_mv_candidates, int max_col_offset, |
197 | 486k | int *processed_cols) { |
198 | 486k | int end_mi = AOMMIN(xd->height, cm->mi_params.mi_rows - mi_row); |
199 | 486k | end_mi = AOMMIN(end_mi, mi_size_high[BLOCK_64X64]); |
200 | 486k | const int n8_h_8 = mi_size_high[BLOCK_8X8]; |
201 | 486k | const int n8_h_16 = mi_size_high[BLOCK_16X16]; |
202 | 486k | int i; |
203 | 486k | int row_offset = 0; |
204 | 486k | if (abs(col_offset) > 1) { |
205 | 268k | row_offset = 1; |
206 | 268k | if ((mi_row & 0x01) && xd->height < n8_h_8) --row_offset; |
207 | 268k | } |
208 | 486k | const int use_step_16 = (xd->height >= 16); |
209 | | |
210 | 1.03M | for (i = 0; i < end_mi;) { |
211 | 545k | const MB_MODE_INFO *const candidate = |
212 | 545k | xd->mi[(row_offset + i) * xd->mi_stride + col_offset]; |
213 | 545k | const int candidate_bsize = candidate->bsize; |
214 | 545k | const int n4_h = mi_size_high[candidate_bsize]; |
215 | 545k | int len = AOMMIN(xd->height, n4_h); |
216 | 545k | if (use_step_16) |
217 | 8.44k | len = AOMMAX(n8_h_16, len); |
218 | 537k | else if (abs(col_offset) > 1) |
219 | 298k | len = AOMMAX(len, n8_h_8); |
220 | | |
221 | 545k | int weight = 2; |
222 | 545k | if (xd->height >= n8_h_8 && xd->height <= n4_h) { |
223 | 233k | int inc = AOMMIN(-max_col_offset + col_offset + 1, |
224 | 233k | mi_size_wide[candidate_bsize]); |
225 | | // Obtain range used in weight calculation. |
226 | 233k | weight = AOMMAX(weight, inc); |
227 | | // Update processed cols. |
228 | 233k | *processed_cols = inc - col_offset - 1; |
229 | 233k | } |
230 | | |
231 | 545k | add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count, |
232 | 545k | newmv_count, ref_mv_stack, ref_mv_weight, |
233 | 545k | gm_mv_candidates, cm->global_motion, len * weight); |
234 | | |
235 | 545k | i += len; |
236 | 545k | } |
237 | 486k | } |
238 | | |
239 | | static inline void scan_blk_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
240 | | const int mi_row, const int mi_col, |
241 | | const MV_REFERENCE_FRAME rf[2], int row_offset, |
242 | | int col_offset, CANDIDATE_MV *ref_mv_stack, |
243 | | uint16_t *ref_mv_weight, |
244 | | uint8_t *ref_match_count, uint8_t *newmv_count, |
245 | | int_mv *gm_mv_candidates, |
246 | 417k | uint8_t *refmv_count) { |
247 | 417k | const TileInfo *const tile = &xd->tile; |
248 | 417k | POSITION mi_pos; |
249 | | |
250 | 417k | mi_pos.row = row_offset; |
251 | 417k | mi_pos.col = col_offset; |
252 | | |
253 | 417k | if (is_inside(tile, mi_col, mi_row, &mi_pos)) { |
254 | 299k | const MB_MODE_INFO *const candidate = |
255 | 299k | xd->mi[mi_pos.row * xd->mi_stride + mi_pos.col]; |
256 | 299k | const int len = mi_size_wide[BLOCK_8X8]; |
257 | | |
258 | 299k | add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count, |
259 | 299k | newmv_count, ref_mv_stack, ref_mv_weight, |
260 | 299k | gm_mv_candidates, cm->global_motion, 2 * len); |
261 | 299k | } // Analyze a single 8x8 block motion information. |
262 | 417k | } |
263 | | |
264 | | static int has_top_right(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
265 | 270k | int mi_row, int mi_col, int bs) { |
266 | 270k | const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size]; |
267 | 270k | const int mask_row = mi_row & (sb_mi_size - 1); |
268 | 270k | const int mask_col = mi_col & (sb_mi_size - 1); |
269 | | |
270 | 270k | if (bs > mi_size_wide[BLOCK_64X64]) return 0; |
271 | | |
272 | | // In a split partition all apart from the bottom right has a top right |
273 | 268k | int has_tr = !((mask_row & bs) && (mask_col & bs)); |
274 | | |
275 | | // bs > 0 and bs is a power of 2 |
276 | 268k | assert(bs > 0 && !(bs & (bs - 1))); |
277 | | |
278 | | // For each 4x4 group of blocks, when the bottom right is decoded the blocks |
279 | | // to the right have not been decoded therefore the bottom right does |
280 | | // not have a top right |
281 | 404k | while (bs < sb_mi_size) { |
282 | 397k | if (mask_col & bs) { |
283 | 160k | if ((mask_col & (2 * bs)) && (mask_row & (2 * bs))) { |
284 | 24.6k | has_tr = 0; |
285 | 24.6k | break; |
286 | 24.6k | } |
287 | 236k | } else { |
288 | 236k | break; |
289 | 236k | } |
290 | 135k | bs <<= 1; |
291 | 135k | } |
292 | | |
293 | | // In a VERTICAL or VERTICAL_4 partition, all partition before the last one |
294 | | // always have a top right (as the block above will have been decoded). |
295 | 268k | if (xd->width < xd->height) { |
296 | 47.5k | if (!xd->is_last_vertical_rect) has_tr = 1; |
297 | 47.5k | } |
298 | | |
299 | | // In a HORIZONTAL or HORIZONTAL_4 partition, partitions after the first one |
300 | | // never have a top right (as the block to the right won't have been decoded). |
301 | 268k | if (xd->width > xd->height) { |
302 | 64.6k | if (!xd->is_first_horizontal_rect) has_tr = 0; |
303 | 64.6k | } |
304 | | |
305 | | // The bottom left square of a Vertical A (in the old format) does |
306 | | // not have a top right as it is decoded before the right hand |
307 | | // rectangle of the partition |
308 | 268k | if (xd->mi[0]->partition == PARTITION_VERT_A) { |
309 | 4.67k | if (xd->width == xd->height) |
310 | 3.12k | if (mask_row & bs) has_tr = 0; |
311 | 4.67k | } |
312 | | |
313 | 268k | return has_tr; |
314 | 270k | } |
315 | | |
316 | | static int check_sb_border(const int mi_row, const int mi_col, |
317 | 25.9k | const int row_offset, const int col_offset) { |
318 | 25.9k | const int sb_mi_size = mi_size_wide[BLOCK_64X64]; |
319 | 25.9k | const int row = mi_row & (sb_mi_size - 1); |
320 | 25.9k | const int col = mi_col & (sb_mi_size - 1); |
321 | | |
322 | 25.9k | if (row + row_offset < 0 || row + row_offset >= sb_mi_size || |
323 | 25.9k | col + col_offset < 0 || col + col_offset >= sb_mi_size) |
324 | 7.03k | return 0; |
325 | | |
326 | 18.9k | return 1; |
327 | 25.9k | } |
328 | | |
329 | | static int add_tpl_ref_mv(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
330 | | int mi_row, int mi_col, MV_REFERENCE_FRAME ref_frame, |
331 | | int blk_row, int blk_col, int_mv *gm_mv_candidates, |
332 | | uint8_t *const refmv_count, |
333 | | CANDIDATE_MV ref_mv_stack[MAX_REF_MV_STACK_SIZE], |
334 | | uint16_t ref_mv_weight[MAX_REF_MV_STACK_SIZE], |
335 | 39.3k | int16_t *mode_context) { |
336 | 39.3k | POSITION mi_pos; |
337 | 39.3k | mi_pos.row = (mi_row & 0x01) ? blk_row : blk_row + 1; |
338 | 39.3k | mi_pos.col = (mi_col & 0x01) ? blk_col : blk_col + 1; |
339 | | |
340 | 39.3k | if (!is_inside(&xd->tile, mi_col, mi_row, &mi_pos)) return 0; |
341 | | |
342 | 22.0k | const TPL_MV_REF *prev_frame_mvs = |
343 | 22.0k | cm->tpl_mvs + |
344 | 22.0k | ((mi_row + mi_pos.row) >> 1) * (cm->mi_params.mi_stride >> 1) + |
345 | 22.0k | ((mi_col + mi_pos.col) >> 1); |
346 | 22.0k | if (prev_frame_mvs->mfmv0.as_int == INVALID_MV) return 0; |
347 | | |
348 | 5.11k | MV_REFERENCE_FRAME rf[2]; |
349 | 5.11k | av1_set_ref_frame(rf, ref_frame); |
350 | | |
351 | 5.11k | const uint16_t weight_unit = 1; // mi_size_wide[BLOCK_8X8]; |
352 | 5.11k | const int cur_frame_index = cm->cur_frame->order_hint; |
353 | 5.11k | const RefCntBuffer *const buf_0 = get_ref_frame_buf(cm, rf[0]); |
354 | 5.11k | const int frame0_index = buf_0->order_hint; |
355 | 5.11k | const int cur_offset_0 = get_relative_dist(&cm->seq_params->order_hint_info, |
356 | 5.11k | cur_frame_index, frame0_index); |
357 | 5.11k | int idx; |
358 | 5.11k | const int allow_high_precision_mv = cm->features.allow_high_precision_mv; |
359 | 5.11k | const int force_integer_mv = cm->features.cur_frame_force_integer_mv; |
360 | | |
361 | 5.11k | int_mv this_refmv; |
362 | 5.11k | get_mv_projection(&this_refmv.as_mv, prev_frame_mvs->mfmv0.as_mv, |
363 | 5.11k | cur_offset_0, prev_frame_mvs->ref_frame_offset); |
364 | 5.11k | lower_mv_precision(&this_refmv.as_mv, allow_high_precision_mv, |
365 | 5.11k | force_integer_mv); |
366 | | |
367 | 5.11k | if (rf[1] == NONE_FRAME) { |
368 | 1.40k | if (blk_row == 0 && blk_col == 0) { |
369 | 737 | if (abs(this_refmv.as_mv.row - gm_mv_candidates[0].as_mv.row) >= 16 || |
370 | 737 | abs(this_refmv.as_mv.col - gm_mv_candidates[0].as_mv.col) >= 16) |
371 | 442 | mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET); |
372 | 737 | } |
373 | | |
374 | 2.94k | for (idx = 0; idx < *refmv_count; ++idx) |
375 | 1.94k | if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int) break; |
376 | | |
377 | 1.40k | if (idx < *refmv_count) ref_mv_weight[idx] += 2 * weight_unit; |
378 | | |
379 | 1.40k | if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) { |
380 | 998 | ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int; |
381 | 998 | ref_mv_weight[idx] = 2 * weight_unit; |
382 | 998 | ++(*refmv_count); |
383 | 998 | } |
384 | 3.70k | } else { |
385 | | // Process compound inter mode |
386 | 3.70k | const RefCntBuffer *const buf_1 = get_ref_frame_buf(cm, rf[1]); |
387 | 3.70k | const int frame1_index = buf_1->order_hint; |
388 | 3.70k | const int cur_offset_1 = get_relative_dist(&cm->seq_params->order_hint_info, |
389 | 3.70k | cur_frame_index, frame1_index); |
390 | 3.70k | int_mv comp_refmv; |
391 | 3.70k | get_mv_projection(&comp_refmv.as_mv, prev_frame_mvs->mfmv0.as_mv, |
392 | 3.70k | cur_offset_1, prev_frame_mvs->ref_frame_offset); |
393 | 3.70k | lower_mv_precision(&comp_refmv.as_mv, allow_high_precision_mv, |
394 | 3.70k | force_integer_mv); |
395 | | |
396 | 3.70k | if (blk_row == 0 && blk_col == 0) { |
397 | 2.83k | if (abs(this_refmv.as_mv.row - gm_mv_candidates[0].as_mv.row) >= 16 || |
398 | 2.83k | abs(this_refmv.as_mv.col - gm_mv_candidates[0].as_mv.col) >= 16 || |
399 | 2.83k | abs(comp_refmv.as_mv.row - gm_mv_candidates[1].as_mv.row) >= 16 || |
400 | 2.83k | abs(comp_refmv.as_mv.col - gm_mv_candidates[1].as_mv.col) >= 16) |
401 | 1.69k | mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET); |
402 | 2.83k | } |
403 | | |
404 | 5.00k | for (idx = 0; idx < *refmv_count; ++idx) { |
405 | 1.85k | if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int && |
406 | 1.85k | comp_refmv.as_int == ref_mv_stack[idx].comp_mv.as_int) |
407 | 556 | break; |
408 | 1.85k | } |
409 | | |
410 | 3.70k | if (idx < *refmv_count) ref_mv_weight[idx] += 2 * weight_unit; |
411 | | |
412 | 3.70k | if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) { |
413 | 3.14k | ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int; |
414 | 3.14k | ref_mv_stack[idx].comp_mv.as_int = comp_refmv.as_int; |
415 | 3.14k | ref_mv_weight[idx] = 2 * weight_unit; |
416 | 3.14k | ++(*refmv_count); |
417 | 3.14k | } |
418 | 3.70k | } |
419 | | |
420 | 5.11k | return 1; |
421 | 22.0k | } |
422 | | |
423 | | static inline void process_compound_ref_mv_candidate( |
424 | | const MB_MODE_INFO *const candidate, const AV1_COMMON *const cm, |
425 | | const MV_REFERENCE_FRAME *const rf, int_mv ref_id[2][2], |
426 | 10.6k | int ref_id_count[2], int_mv ref_diff[2][2], int ref_diff_count[2]) { |
427 | 31.8k | for (int rf_idx = 0; rf_idx < 2; ++rf_idx) { |
428 | 21.2k | MV_REFERENCE_FRAME can_rf = candidate->ref_frame[rf_idx]; |
429 | | |
430 | 63.7k | for (int cmp_idx = 0; cmp_idx < 2; ++cmp_idx) { |
431 | 42.4k | if (can_rf == rf[cmp_idx] && ref_id_count[cmp_idx] < 2) { |
432 | 12.2k | ref_id[cmp_idx][ref_id_count[cmp_idx]] = candidate->mv[rf_idx]; |
433 | 12.2k | ++ref_id_count[cmp_idx]; |
434 | 30.2k | } else if (can_rf > INTRA_FRAME && ref_diff_count[cmp_idx] < 2) { |
435 | 20.2k | int_mv this_mv = candidate->mv[rf_idx]; |
436 | 20.2k | if (cm->ref_frame_sign_bias[can_rf] != |
437 | 20.2k | cm->ref_frame_sign_bias[rf[cmp_idx]]) { |
438 | 709 | this_mv.as_mv.row = -this_mv.as_mv.row; |
439 | 709 | this_mv.as_mv.col = -this_mv.as_mv.col; |
440 | 709 | } |
441 | 20.2k | ref_diff[cmp_idx][ref_diff_count[cmp_idx]] = this_mv; |
442 | 20.2k | ++ref_diff_count[cmp_idx]; |
443 | 20.2k | } |
444 | 42.4k | } |
445 | 21.2k | } |
446 | 10.6k | } |
447 | | |
448 | | static inline void process_single_ref_mv_candidate( |
449 | | const MB_MODE_INFO *const candidate, const AV1_COMMON *const cm, |
450 | | MV_REFERENCE_FRAME ref_frame, uint8_t *const refmv_count, |
451 | | CANDIDATE_MV ref_mv_stack[MAX_REF_MV_STACK_SIZE], |
452 | 162k | uint16_t ref_mv_weight[MAX_REF_MV_STACK_SIZE]) { |
453 | 488k | for (int rf_idx = 0; rf_idx < 2; ++rf_idx) { |
454 | 325k | if (candidate->ref_frame[rf_idx] > INTRA_FRAME) { |
455 | 58.5k | int_mv this_mv = candidate->mv[rf_idx]; |
456 | 58.5k | if (cm->ref_frame_sign_bias[candidate->ref_frame[rf_idx]] != |
457 | 58.5k | cm->ref_frame_sign_bias[ref_frame]) { |
458 | 500 | this_mv.as_mv.row = -this_mv.as_mv.row; |
459 | 500 | this_mv.as_mv.col = -this_mv.as_mv.col; |
460 | 500 | } |
461 | 58.5k | int stack_idx; |
462 | 65.2k | for (stack_idx = 0; stack_idx < *refmv_count; ++stack_idx) { |
463 | 51.7k | const int_mv stack_mv = ref_mv_stack[stack_idx].this_mv; |
464 | 51.7k | if (this_mv.as_int == stack_mv.as_int) break; |
465 | 51.7k | } |
466 | | |
467 | 58.5k | if (stack_idx == *refmv_count) { |
468 | 13.4k | ref_mv_stack[stack_idx].this_mv = this_mv; |
469 | | |
470 | | // TODO(jingning): Set an arbitrary small number here. The weight |
471 | | // doesn't matter as long as it is properly initialized. |
472 | 13.4k | ref_mv_weight[stack_idx] = 2; |
473 | 13.4k | ++(*refmv_count); |
474 | 13.4k | } |
475 | 58.5k | } |
476 | 325k | } |
477 | 162k | } |
478 | | |
479 | | static inline void setup_ref_mv_list( |
480 | | const AV1_COMMON *cm, const MACROBLOCKD *xd, MV_REFERENCE_FRAME ref_frame, |
481 | | uint8_t *const refmv_count, |
482 | | CANDIDATE_MV ref_mv_stack[MAX_REF_MV_STACK_SIZE], |
483 | | uint16_t ref_mv_weight[MAX_REF_MV_STACK_SIZE], |
484 | | int_mv mv_ref_list[MAX_MV_REF_CANDIDATES], int_mv *gm_mv_candidates, |
485 | 251k | int mi_row, int mi_col, int16_t *mode_context) { |
486 | 251k | const int bs = AOMMAX(xd->width, xd->height); |
487 | 251k | const int has_tr = has_top_right(cm, xd, mi_row, mi_col, bs); |
488 | 251k | MV_REFERENCE_FRAME rf[2]; |
489 | | |
490 | 251k | const TileInfo *const tile = &xd->tile; |
491 | 251k | int max_row_offset = 0, max_col_offset = 0; |
492 | 251k | const int row_adj = (xd->height < mi_size_high[BLOCK_8X8]) && (mi_row & 0x01); |
493 | 251k | const int col_adj = (xd->width < mi_size_wide[BLOCK_8X8]) && (mi_col & 0x01); |
494 | 251k | int processed_rows = 0; |
495 | 251k | int processed_cols = 0; |
496 | | |
497 | 251k | av1_set_ref_frame(rf, ref_frame); |
498 | 251k | mode_context[ref_frame] = 0; |
499 | 251k | *refmv_count = 0; |
500 | | |
501 | | // Find valid maximum row/col offset. |
502 | 251k | if (xd->up_available) { |
503 | 204k | max_row_offset = -(MVREF_ROW_COLS << 1) + row_adj; |
504 | | |
505 | 204k | if (xd->height < mi_size_high[BLOCK_8X8]) |
506 | 77.7k | max_row_offset = -(2 << 1) + row_adj; |
507 | | |
508 | 204k | max_row_offset = find_valid_row_offset(tile, mi_row, max_row_offset); |
509 | 204k | } |
510 | | |
511 | 251k | if (xd->left_available) { |
512 | 218k | max_col_offset = -(MVREF_ROW_COLS << 1) + col_adj; |
513 | | |
514 | 218k | if (xd->width < mi_size_wide[BLOCK_8X8]) |
515 | 72.8k | max_col_offset = -(2 << 1) + col_adj; |
516 | | |
517 | 218k | max_col_offset = find_valid_col_offset(tile, mi_col, max_col_offset); |
518 | 218k | } |
519 | | |
520 | 251k | uint8_t col_match_count = 0; |
521 | 251k | uint8_t row_match_count = 0; |
522 | 251k | uint8_t newmv_count = 0; |
523 | | |
524 | | // Scan the first above row mode info. row_offset = -1; |
525 | 251k | if (abs(max_row_offset) >= 1) |
526 | 204k | scan_row_mbmi(cm, xd, mi_col, rf, -1, ref_mv_stack, ref_mv_weight, |
527 | 204k | refmv_count, &row_match_count, &newmv_count, gm_mv_candidates, |
528 | 204k | max_row_offset, &processed_rows); |
529 | | // Scan the first left column mode info. col_offset = -1; |
530 | 251k | if (abs(max_col_offset) >= 1) |
531 | 218k | scan_col_mbmi(cm, xd, mi_row, rf, -1, ref_mv_stack, ref_mv_weight, |
532 | 218k | refmv_count, &col_match_count, &newmv_count, gm_mv_candidates, |
533 | 218k | max_col_offset, &processed_cols); |
534 | | // Check top-right boundary |
535 | 251k | if (has_tr) |
536 | 166k | scan_blk_mbmi(cm, xd, mi_row, mi_col, rf, -1, xd->width, ref_mv_stack, |
537 | 166k | ref_mv_weight, &row_match_count, &newmv_count, |
538 | 166k | gm_mv_candidates, refmv_count); |
539 | | |
540 | 251k | const uint8_t nearest_match = (row_match_count > 0) + (col_match_count > 0); |
541 | 251k | const uint8_t nearest_refmv_count = *refmv_count; |
542 | | |
543 | | // TODO(yunqing): for comp_search, do it for all 3 cases. |
544 | 550k | for (int idx = 0; idx < nearest_refmv_count; ++idx) |
545 | 298k | ref_mv_weight[idx] += REF_CAT_LEVEL; |
546 | | |
547 | 251k | if (cm->features.allow_ref_frame_mvs) { |
548 | 14.5k | int is_available = 0; |
549 | 14.5k | const int voffset = AOMMAX(mi_size_high[BLOCK_8X8], xd->height); |
550 | 14.5k | const int hoffset = AOMMAX(mi_size_wide[BLOCK_8X8], xd->width); |
551 | 14.5k | const int blk_row_end = AOMMIN(xd->height, mi_size_high[BLOCK_64X64]); |
552 | 14.5k | const int blk_col_end = AOMMIN(xd->width, mi_size_wide[BLOCK_64X64]); |
553 | | |
554 | 14.5k | const int tpl_sample_pos[3][2] = { |
555 | 14.5k | { voffset, -2 }, |
556 | 14.5k | { voffset, hoffset }, |
557 | 14.5k | { voffset - 2, hoffset }, |
558 | 14.5k | }; |
559 | 14.5k | const int allow_extension = (xd->height >= mi_size_high[BLOCK_8X8]) && |
560 | 14.5k | (xd->height < mi_size_high[BLOCK_64X64]) && |
561 | 14.5k | (xd->width >= mi_size_wide[BLOCK_8X8]) && |
562 | 14.5k | (xd->width < mi_size_wide[BLOCK_64X64]); |
563 | | |
564 | 14.5k | const int step_h = (xd->height >= mi_size_high[BLOCK_64X64]) |
565 | 14.5k | ? mi_size_high[BLOCK_16X16] |
566 | 14.5k | : mi_size_high[BLOCK_8X8]; |
567 | 14.5k | const int step_w = (xd->width >= mi_size_wide[BLOCK_64X64]) |
568 | 14.5k | ? mi_size_wide[BLOCK_16X16] |
569 | 14.5k | : mi_size_wide[BLOCK_8X8]; |
570 | | |
571 | 29.9k | for (int blk_row = 0; blk_row < blk_row_end; blk_row += step_h) { |
572 | 35.8k | for (int blk_col = 0; blk_col < blk_col_end; blk_col += step_w) { |
573 | 20.4k | int ret = add_tpl_ref_mv(cm, xd, mi_row, mi_col, ref_frame, blk_row, |
574 | 20.4k | blk_col, gm_mv_candidates, refmv_count, |
575 | 20.4k | ref_mv_stack, ref_mv_weight, mode_context); |
576 | 20.4k | if (blk_row == 0 && blk_col == 0) is_available = ret; |
577 | 20.4k | } |
578 | 15.4k | } |
579 | | |
580 | 14.5k | if (is_available == 0) mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET); |
581 | | |
582 | 40.5k | for (int i = 0; i < 3 && allow_extension; ++i) { |
583 | 25.9k | const int blk_row = tpl_sample_pos[i][0]; |
584 | 25.9k | const int blk_col = tpl_sample_pos[i][1]; |
585 | | |
586 | 25.9k | if (!check_sb_border(mi_row, mi_col, blk_row, blk_col)) continue; |
587 | 18.9k | add_tpl_ref_mv(cm, xd, mi_row, mi_col, ref_frame, blk_row, blk_col, |
588 | 18.9k | gm_mv_candidates, refmv_count, ref_mv_stack, ref_mv_weight, |
589 | 18.9k | mode_context); |
590 | 18.9k | } |
591 | 14.5k | } |
592 | | |
593 | 251k | uint8_t dummy_newmv_count = 0; |
594 | | |
595 | | // Scan the second outer area. |
596 | 251k | scan_blk_mbmi(cm, xd, mi_row, mi_col, rf, -1, -1, ref_mv_stack, ref_mv_weight, |
597 | 251k | &row_match_count, &dummy_newmv_count, gm_mv_candidates, |
598 | 251k | refmv_count); |
599 | | |
600 | 754k | for (int idx = 2; idx <= MVREF_ROW_COLS; ++idx) { |
601 | 502k | const int row_offset = -(idx << 1) + 1 + row_adj; |
602 | 502k | const int col_offset = -(idx << 1) + 1 + col_adj; |
603 | | |
604 | 502k | if (abs(row_offset) <= abs(max_row_offset) && |
605 | 502k | abs(row_offset) > processed_rows) |
606 | 234k | scan_row_mbmi(cm, xd, mi_col, rf, row_offset, ref_mv_stack, ref_mv_weight, |
607 | 234k | refmv_count, &row_match_count, &dummy_newmv_count, |
608 | 234k | gm_mv_candidates, max_row_offset, &processed_rows); |
609 | | |
610 | 502k | if (abs(col_offset) <= abs(max_col_offset) && |
611 | 502k | abs(col_offset) > processed_cols) |
612 | 268k | scan_col_mbmi(cm, xd, mi_row, rf, col_offset, ref_mv_stack, ref_mv_weight, |
613 | 268k | refmv_count, &col_match_count, &dummy_newmv_count, |
614 | 268k | gm_mv_candidates, max_col_offset, &processed_cols); |
615 | 502k | } |
616 | | |
617 | 251k | const uint8_t ref_match_count = (row_match_count > 0) + (col_match_count > 0); |
618 | | |
619 | 251k | switch (nearest_match) { |
620 | 63.4k | case 0: |
621 | 63.4k | if (ref_match_count >= 1) mode_context[ref_frame] |= 1; |
622 | 63.4k | if (ref_match_count == 1) |
623 | 13.0k | mode_context[ref_frame] |= (1 << REFMV_OFFSET); |
624 | 50.3k | else if (ref_match_count >= 2) |
625 | 2.61k | mode_context[ref_frame] |= (2 << REFMV_OFFSET); |
626 | 63.4k | break; |
627 | 104k | case 1: |
628 | 104k | mode_context[ref_frame] |= (newmv_count > 0) ? 2 : 3; |
629 | 104k | if (ref_match_count == 1) |
630 | 71.9k | mode_context[ref_frame] |= (3 << REFMV_OFFSET); |
631 | 32.6k | else if (ref_match_count >= 2) |
632 | 32.6k | mode_context[ref_frame] |= (4 << REFMV_OFFSET); |
633 | 104k | break; |
634 | 83.4k | case 2: |
635 | 83.4k | default: |
636 | 83.4k | if (newmv_count >= 1) |
637 | 2.72k | mode_context[ref_frame] |= 4; |
638 | 80.6k | else |
639 | 80.6k | mode_context[ref_frame] |= 5; |
640 | | |
641 | 83.4k | mode_context[ref_frame] |= (5 << REFMV_OFFSET); |
642 | 83.4k | break; |
643 | 251k | } |
644 | | |
645 | | // Rank the likelihood and assign nearest and near mvs. |
646 | 251k | int len = nearest_refmv_count; |
647 | 478k | while (len > 0) { |
648 | 227k | int nr_len = 0; |
649 | 361k | for (int idx = 1; idx < len; ++idx) { |
650 | 134k | if (ref_mv_weight[idx - 1] < ref_mv_weight[idx]) { |
651 | 48.2k | const CANDIDATE_MV tmp_mv = ref_mv_stack[idx - 1]; |
652 | 48.2k | const uint16_t tmp_ref_mv_weight = ref_mv_weight[idx - 1]; |
653 | 48.2k | ref_mv_stack[idx - 1] = ref_mv_stack[idx]; |
654 | 48.2k | ref_mv_stack[idx] = tmp_mv; |
655 | 48.2k | ref_mv_weight[idx - 1] = ref_mv_weight[idx]; |
656 | 48.2k | ref_mv_weight[idx] = tmp_ref_mv_weight; |
657 | 48.2k | nr_len = idx; |
658 | 48.2k | } |
659 | 134k | } |
660 | 227k | len = nr_len; |
661 | 227k | } |
662 | | |
663 | 251k | len = *refmv_count; |
664 | 418k | while (len > nearest_refmv_count) { |
665 | 167k | int nr_len = nearest_refmv_count; |
666 | 308k | for (int idx = nearest_refmv_count + 1; idx < len; ++idx) { |
667 | 140k | if (ref_mv_weight[idx - 1] < ref_mv_weight[idx]) { |
668 | 36.0k | const CANDIDATE_MV tmp_mv = ref_mv_stack[idx - 1]; |
669 | 36.0k | const uint16_t tmp_ref_mv_weight = ref_mv_weight[idx - 1]; |
670 | 36.0k | ref_mv_stack[idx - 1] = ref_mv_stack[idx]; |
671 | 36.0k | ref_mv_stack[idx] = tmp_mv; |
672 | 36.0k | ref_mv_weight[idx - 1] = ref_mv_weight[idx]; |
673 | 36.0k | ref_mv_weight[idx] = tmp_ref_mv_weight; |
674 | 36.0k | nr_len = idx; |
675 | 36.0k | } |
676 | 140k | } |
677 | 167k | len = nr_len; |
678 | 167k | } |
679 | | |
680 | 251k | int mi_width = AOMMIN(mi_size_wide[BLOCK_64X64], xd->width); |
681 | 251k | mi_width = AOMMIN(mi_width, cm->mi_params.mi_cols - mi_col); |
682 | 251k | int mi_height = AOMMIN(mi_size_high[BLOCK_64X64], xd->height); |
683 | 251k | mi_height = AOMMIN(mi_height, cm->mi_params.mi_rows - mi_row); |
684 | 251k | const int mi_size = AOMMIN(mi_width, mi_height); |
685 | 251k | if (rf[1] > NONE_FRAME) { |
686 | | // TODO(jingning, yunqing): Refactor and consolidate the compound and |
687 | | // single reference frame modes. Reduce unnecessary redundancy. |
688 | 17.2k | if (*refmv_count < MAX_MV_REF_CANDIDATES) { |
689 | 16.0k | int_mv ref_id[2][2], ref_diff[2][2]; |
690 | 16.0k | int ref_id_count[2] = { 0 }, ref_diff_count[2] = { 0 }; |
691 | | |
692 | 21.3k | for (int idx = 0; abs(max_row_offset) >= 1 && idx < mi_size;) { |
693 | 5.34k | const MB_MODE_INFO *const candidate = xd->mi[-xd->mi_stride + idx]; |
694 | 5.34k | process_compound_ref_mv_candidate( |
695 | 5.34k | candidate, cm, rf, ref_id, ref_id_count, ref_diff, ref_diff_count); |
696 | 5.34k | idx += mi_size_wide[candidate->bsize]; |
697 | 5.34k | } |
698 | | |
699 | 21.2k | for (int idx = 0; abs(max_col_offset) >= 1 && idx < mi_size;) { |
700 | 5.27k | const MB_MODE_INFO *const candidate = xd->mi[idx * xd->mi_stride - 1]; |
701 | 5.27k | process_compound_ref_mv_candidate( |
702 | 5.27k | candidate, cm, rf, ref_id, ref_id_count, ref_diff, ref_diff_count); |
703 | 5.27k | idx += mi_size_high[candidate->bsize]; |
704 | 5.27k | } |
705 | | |
706 | | // Build up the compound mv predictor |
707 | 16.0k | int_mv comp_list[MAX_MV_REF_CANDIDATES][2]; |
708 | | |
709 | 48.0k | for (int idx = 0; idx < 2; ++idx) { |
710 | 32.0k | int comp_idx = 0; |
711 | 32.0k | for (int list_idx = 0; |
712 | 44.2k | list_idx < ref_id_count[idx] && comp_idx < MAX_MV_REF_CANDIDATES; |
713 | 32.0k | ++list_idx, ++comp_idx) |
714 | 12.2k | comp_list[comp_idx][idx] = ref_id[idx][list_idx]; |
715 | 32.0k | for (int list_idx = 0; |
716 | 49.0k | list_idx < ref_diff_count[idx] && comp_idx < MAX_MV_REF_CANDIDATES; |
717 | 32.0k | ++list_idx, ++comp_idx) |
718 | 17.0k | comp_list[comp_idx][idx] = ref_diff[idx][list_idx]; |
719 | 66.6k | for (; comp_idx < MAX_MV_REF_CANDIDATES; ++comp_idx) |
720 | 34.6k | comp_list[comp_idx][idx] = gm_mv_candidates[idx]; |
721 | 32.0k | } |
722 | | |
723 | 16.0k | if (*refmv_count) { |
724 | 6.10k | assert(*refmv_count == 1); |
725 | 6.10k | if (comp_list[0][0].as_int == ref_mv_stack[0].this_mv.as_int && |
726 | 6.10k | comp_list[0][1].as_int == ref_mv_stack[0].comp_mv.as_int) { |
727 | 3.12k | ref_mv_stack[*refmv_count].this_mv = comp_list[1][0]; |
728 | 3.12k | ref_mv_stack[*refmv_count].comp_mv = comp_list[1][1]; |
729 | 3.12k | } else { |
730 | 2.97k | ref_mv_stack[*refmv_count].this_mv = comp_list[0][0]; |
731 | 2.97k | ref_mv_stack[*refmv_count].comp_mv = comp_list[0][1]; |
732 | 2.97k | } |
733 | 6.10k | ref_mv_weight[*refmv_count] = 2; |
734 | 6.10k | ++*refmv_count; |
735 | 9.89k | } else { |
736 | 29.6k | for (int idx = 0; idx < MAX_MV_REF_CANDIDATES; ++idx) { |
737 | 19.7k | ref_mv_stack[*refmv_count].this_mv = comp_list[idx][0]; |
738 | 19.7k | ref_mv_stack[*refmv_count].comp_mv = comp_list[idx][1]; |
739 | 19.7k | ref_mv_weight[*refmv_count] = 2; |
740 | 19.7k | ++*refmv_count; |
741 | 19.7k | } |
742 | 9.89k | } |
743 | 16.0k | } |
744 | | |
745 | 17.2k | assert(*refmv_count >= 2); |
746 | | |
747 | 52.4k | for (int idx = 0; idx < *refmv_count; ++idx) { |
748 | 35.2k | clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->width << MI_SIZE_LOG2, |
749 | 35.2k | xd->height << MI_SIZE_LOG2, xd); |
750 | 35.2k | clamp_mv_ref(&ref_mv_stack[idx].comp_mv.as_mv, xd->width << MI_SIZE_LOG2, |
751 | 35.2k | xd->height << MI_SIZE_LOG2, xd); |
752 | 35.2k | } |
753 | 234k | } else { |
754 | | // Handle single reference frame extension |
755 | 307k | for (int idx = 0; abs(max_row_offset) >= 1 && idx < mi_size && |
756 | 307k | *refmv_count < MAX_MV_REF_CANDIDATES;) { |
757 | 72.9k | const MB_MODE_INFO *const candidate = xd->mi[-xd->mi_stride + idx]; |
758 | 72.9k | process_single_ref_mv_candidate(candidate, cm, ref_frame, refmv_count, |
759 | 72.9k | ref_mv_stack, ref_mv_weight); |
760 | 72.9k | idx += mi_size_wide[candidate->bsize]; |
761 | 72.9k | } |
762 | | |
763 | 324k | for (int idx = 0; abs(max_col_offset) >= 1 && idx < mi_size && |
764 | 324k | *refmv_count < MAX_MV_REF_CANDIDATES;) { |
765 | 89.9k | const MB_MODE_INFO *const candidate = xd->mi[idx * xd->mi_stride - 1]; |
766 | 89.9k | process_single_ref_mv_candidate(candidate, cm, ref_frame, refmv_count, |
767 | 89.9k | ref_mv_stack, ref_mv_weight); |
768 | 89.9k | idx += mi_size_high[candidate->bsize]; |
769 | 89.9k | } |
770 | | |
771 | 798k | for (int idx = 0; idx < *refmv_count; ++idx) { |
772 | 564k | clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->width << MI_SIZE_LOG2, |
773 | 564k | xd->height << MI_SIZE_LOG2, xd); |
774 | 564k | } |
775 | | |
776 | 234k | if (mv_ref_list != NULL) { |
777 | 359k | for (int idx = *refmv_count; idx < MAX_MV_REF_CANDIDATES; ++idx) |
778 | 125k | mv_ref_list[idx].as_int = gm_mv_candidates[0].as_int; |
779 | | |
780 | 577k | for (int idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, *refmv_count); |
781 | 343k | ++idx) { |
782 | 343k | mv_ref_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int; |
783 | 343k | } |
784 | 234k | } |
785 | 234k | } |
786 | 251k | } |
787 | | |
788 | | void av1_find_mv_refs(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
789 | | MB_MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame, |
790 | | uint8_t ref_mv_count[MODE_CTX_REF_FRAMES], |
791 | | CANDIDATE_MV ref_mv_stack[][MAX_REF_MV_STACK_SIZE], |
792 | | uint16_t ref_mv_weight[][MAX_REF_MV_STACK_SIZE], |
793 | | int_mv mv_ref_list[][MAX_MV_REF_CANDIDATES], |
794 | 251k | int_mv *global_mvs, int16_t *mode_context) { |
795 | 251k | const int mi_row = xd->mi_row; |
796 | 251k | const int mi_col = xd->mi_col; |
797 | 251k | int_mv gm_mv[2]; |
798 | | |
799 | 251k | if (ref_frame == INTRA_FRAME) { |
800 | 169k | gm_mv[0].as_int = gm_mv[1].as_int = 0; |
801 | 169k | if (global_mvs != NULL) { |
802 | 0 | global_mvs[ref_frame].as_int = INVALID_MV; |
803 | 0 | } |
804 | 169k | } else { |
805 | 82.3k | const BLOCK_SIZE bsize = mi->bsize; |
806 | 82.3k | const int allow_high_precision_mv = cm->features.allow_high_precision_mv; |
807 | 82.3k | const int force_integer_mv = cm->features.cur_frame_force_integer_mv; |
808 | 82.3k | if (ref_frame < REF_FRAMES) { |
809 | 65.0k | gm_mv[0] = gm_get_motion_vector(&cm->global_motion[ref_frame], |
810 | 65.0k | allow_high_precision_mv, bsize, mi_col, |
811 | 65.0k | mi_row, force_integer_mv); |
812 | 65.0k | gm_mv[1].as_int = 0; |
813 | 65.0k | if (global_mvs != NULL) global_mvs[ref_frame] = gm_mv[0]; |
814 | 65.0k | } else { |
815 | 17.2k | MV_REFERENCE_FRAME rf[2]; |
816 | 17.2k | av1_set_ref_frame(rf, ref_frame); |
817 | 17.2k | gm_mv[0] = gm_get_motion_vector(&cm->global_motion[rf[0]], |
818 | 17.2k | allow_high_precision_mv, bsize, mi_col, |
819 | 17.2k | mi_row, force_integer_mv); |
820 | 17.2k | gm_mv[1] = gm_get_motion_vector(&cm->global_motion[rf[1]], |
821 | 17.2k | allow_high_precision_mv, bsize, mi_col, |
822 | 17.2k | mi_row, force_integer_mv); |
823 | 17.2k | } |
824 | 82.3k | } |
825 | | |
826 | 251k | setup_ref_mv_list(cm, xd, ref_frame, &ref_mv_count[ref_frame], |
827 | 251k | ref_mv_stack[ref_frame], ref_mv_weight[ref_frame], |
828 | 251k | mv_ref_list ? mv_ref_list[ref_frame] : NULL, gm_mv, mi_row, |
829 | 251k | mi_col, mode_context); |
830 | 251k | } |
831 | | |
832 | | void av1_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *nearest_mv, |
833 | 214k | int_mv *near_mv, int is_integer) { |
834 | 214k | int i; |
835 | | // Make sure all the candidates are properly clamped etc |
836 | 642k | for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) { |
837 | 428k | lower_mv_precision(&mvlist[i].as_mv, allow_hp, is_integer); |
838 | 428k | } |
839 | 214k | *nearest_mv = mvlist[0]; |
840 | 214k | *near_mv = mvlist[1]; |
841 | 214k | } |
842 | | |
843 | 54.9k | void av1_setup_frame_buf_refs(AV1_COMMON *cm) { |
844 | 54.9k | cm->cur_frame->order_hint = cm->current_frame.order_hint; |
845 | 54.9k | cm->cur_frame->display_order_hint = cm->current_frame.display_order_hint; |
846 | 54.9k | cm->cur_frame->pyramid_level = cm->current_frame.pyramid_level; |
847 | 54.9k | MV_REFERENCE_FRAME ref_frame; |
848 | 439k | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
849 | 384k | const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame); |
850 | 384k | if (buf != NULL) { |
851 | 140k | cm->cur_frame->ref_order_hints[ref_frame - LAST_FRAME] = buf->order_hint; |
852 | 140k | cm->cur_frame->ref_display_order_hint[ref_frame - LAST_FRAME] = |
853 | 140k | buf->display_order_hint; |
854 | 140k | } |
855 | 384k | } |
856 | 54.9k | } |
857 | | |
858 | 54.9k | void av1_setup_frame_sign_bias(AV1_COMMON *cm) { |
859 | 54.9k | MV_REFERENCE_FRAME ref_frame; |
860 | 439k | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
861 | 384k | const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame); |
862 | 384k | if (cm->seq_params->order_hint_info.enable_order_hint && buf != NULL) { |
863 | 81.6k | const int ref_order_hint = buf->order_hint; |
864 | 81.6k | cm->ref_frame_sign_bias[ref_frame] = |
865 | 81.6k | (get_relative_dist(&cm->seq_params->order_hint_info, ref_order_hint, |
866 | 81.6k | (int)cm->current_frame.order_hint) <= 0) |
867 | 81.6k | ? 0 |
868 | 81.6k | : 1; |
869 | 302k | } else { |
870 | 302k | cm->ref_frame_sign_bias[ref_frame] = 0; |
871 | 302k | } |
872 | 384k | } |
873 | 54.9k | } |
874 | | |
875 | 13.4k | #define MAX_OFFSET_WIDTH 64 |
876 | 13.4k | #define MAX_OFFSET_HEIGHT 0 |
877 | | |
878 | | static int get_block_position(AV1_COMMON *cm, int *mi_r, int *mi_c, int blk_row, |
879 | 7.77k | int blk_col, MV mv, int sign_bias) { |
880 | 7.77k | const int base_blk_row = (blk_row >> 3) << 3; |
881 | 7.77k | const int base_blk_col = (blk_col >> 3) << 3; |
882 | | |
883 | 7.77k | const int row_offset = (mv.row >= 0) ? (mv.row >> (4 + MI_SIZE_LOG2)) |
884 | 7.77k | : -((-mv.row) >> (4 + MI_SIZE_LOG2)); |
885 | | |
886 | 7.77k | const int col_offset = (mv.col >= 0) ? (mv.col >> (4 + MI_SIZE_LOG2)) |
887 | 7.77k | : -((-mv.col) >> (4 + MI_SIZE_LOG2)); |
888 | | |
889 | 7.77k | const int row = |
890 | 7.77k | (sign_bias == 1) ? blk_row - row_offset : blk_row + row_offset; |
891 | 7.77k | const int col = |
892 | 7.77k | (sign_bias == 1) ? blk_col - col_offset : blk_col + col_offset; |
893 | | |
894 | 7.77k | if (row < 0 || row >= (cm->mi_params.mi_rows >> 1) || col < 0 || |
895 | 7.77k | col >= (cm->mi_params.mi_cols >> 1)) |
896 | 1.03k | return 0; |
897 | | |
898 | 6.74k | if (row < base_blk_row - (MAX_OFFSET_HEIGHT >> 3) || |
899 | 6.74k | row >= base_blk_row + 8 + (MAX_OFFSET_HEIGHT >> 3) || |
900 | 6.74k | col < base_blk_col - (MAX_OFFSET_WIDTH >> 3) || |
901 | 6.74k | col >= base_blk_col + 8 + (MAX_OFFSET_WIDTH >> 3)) |
902 | 0 | return 0; |
903 | | |
904 | 6.74k | *mi_r = row; |
905 | 6.74k | *mi_c = col; |
906 | | |
907 | 6.74k | return 1; |
908 | 6.74k | } |
909 | | |
910 | | // Note: motion_filed_projection finds motion vectors of current frame's |
911 | | // reference frame, and projects them to current frame. To make it clear, |
912 | | // let's call current frame's reference frame as start frame. |
913 | | // Call Start frame's reference frames as reference frames. |
914 | | // Call ref_offset as frame distances between start frame and its reference |
915 | | // frames. |
916 | | static int motion_field_projection(AV1_COMMON *cm, |
917 | 15.3k | MV_REFERENCE_FRAME start_frame, int dir) { |
918 | 15.3k | TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs; |
919 | 15.3k | int ref_offset[REF_FRAMES] = { 0 }; |
920 | | |
921 | 15.3k | const RefCntBuffer *const start_frame_buf = |
922 | 15.3k | get_ref_frame_buf(cm, start_frame); |
923 | 15.3k | if (start_frame_buf == NULL) return 0; |
924 | | |
925 | 15.3k | if (start_frame_buf->frame_type == KEY_FRAME || |
926 | 15.3k | start_frame_buf->frame_type == INTRA_ONLY_FRAME) |
927 | 4.08k | return 0; |
928 | | |
929 | 11.3k | if (start_frame_buf->mi_rows != cm->mi_params.mi_rows || |
930 | 11.3k | start_frame_buf->mi_cols != cm->mi_params.mi_cols) |
931 | 21 | return 0; |
932 | | |
933 | 11.2k | const int start_frame_order_hint = start_frame_buf->order_hint; |
934 | 11.2k | const unsigned int *const ref_order_hints = |
935 | 11.2k | &start_frame_buf->ref_order_hints[0]; |
936 | 11.2k | const int cur_order_hint = cm->cur_frame->order_hint; |
937 | 11.2k | int start_to_current_frame_offset = get_relative_dist( |
938 | 11.2k | &cm->seq_params->order_hint_info, start_frame_order_hint, cur_order_hint); |
939 | | |
940 | 90.2k | for (MV_REFERENCE_FRAME rf = LAST_FRAME; rf <= INTER_REFS_PER_FRAME; ++rf) { |
941 | 79.0k | ref_offset[rf] = get_relative_dist(&cm->seq_params->order_hint_info, |
942 | 79.0k | start_frame_order_hint, |
943 | 79.0k | ref_order_hints[rf - LAST_FRAME]); |
944 | 79.0k | } |
945 | | |
946 | 11.2k | if (dir == 2) start_to_current_frame_offset = -start_to_current_frame_offset; |
947 | | |
948 | 11.2k | MV_REF *mv_ref_base = start_frame_buf->mvs; |
949 | 11.2k | const int mvs_rows = (cm->mi_params.mi_rows + 1) >> 1; |
950 | 11.2k | const int mvs_cols = (cm->mi_params.mi_cols + 1) >> 1; |
951 | | |
952 | 23.2k | for (int blk_row = 0; blk_row < mvs_rows; ++blk_row) { |
953 | 26.7k | for (int blk_col = 0; blk_col < mvs_cols; ++blk_col) { |
954 | 14.7k | MV_REF *mv_ref = &mv_ref_base[blk_row * mvs_cols + blk_col]; |
955 | 14.7k | MV fwd_mv = mv_ref->mv.as_mv; |
956 | | |
957 | 14.7k | if (mv_ref->ref_frame > INTRA_FRAME) { |
958 | 10.9k | int_mv this_mv; |
959 | 10.9k | int mi_r, mi_c; |
960 | 10.9k | const int ref_frame_offset = ref_offset[mv_ref->ref_frame]; |
961 | | |
962 | 10.9k | int pos_valid = |
963 | 10.9k | abs(ref_frame_offset) <= MAX_FRAME_DISTANCE && |
964 | 10.9k | ref_frame_offset > 0 && |
965 | 10.9k | abs(start_to_current_frame_offset) <= MAX_FRAME_DISTANCE; |
966 | | |
967 | 10.9k | if (pos_valid) { |
968 | 7.77k | get_mv_projection(&this_mv.as_mv, fwd_mv, |
969 | 7.77k | start_to_current_frame_offset, ref_frame_offset); |
970 | 7.77k | pos_valid = get_block_position(cm, &mi_r, &mi_c, blk_row, blk_col, |
971 | 7.77k | this_mv.as_mv, dir >> 1); |
972 | 7.77k | } |
973 | | |
974 | 10.9k | if (pos_valid) { |
975 | 6.74k | const int mi_offset = mi_r * (cm->mi_params.mi_stride >> 1) + mi_c; |
976 | | |
977 | 6.74k | tpl_mvs_base[mi_offset].mfmv0.as_mv.row = fwd_mv.row; |
978 | 6.74k | tpl_mvs_base[mi_offset].mfmv0.as_mv.col = fwd_mv.col; |
979 | 6.74k | tpl_mvs_base[mi_offset].ref_frame_offset = ref_frame_offset; |
980 | 6.74k | } |
981 | 10.9k | } |
982 | 14.7k | } |
983 | 11.9k | } |
984 | | |
985 | 11.2k | return 1; |
986 | 11.3k | } |
987 | | |
988 | | // cm->ref_frame_side is calculated here, and will be used in |
989 | | // av1_copy_frame_mvs() to affect how mvs are copied. |
990 | 54.5k | void av1_calculate_ref_frame_side(AV1_COMMON *cm) { |
991 | 54.5k | const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info; |
992 | | |
993 | 54.5k | memset(cm->ref_frame_side, 0, sizeof(cm->ref_frame_side)); |
994 | 54.5k | if (!order_hint_info->enable_order_hint) return; |
995 | | |
996 | 21.9k | const int cur_order_hint = cm->cur_frame->order_hint; |
997 | | |
998 | 175k | for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { |
999 | 153k | const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame); |
1000 | 153k | int order_hint = 0; |
1001 | | |
1002 | 153k | if (buf != NULL) order_hint = buf->order_hint; |
1003 | | |
1004 | 153k | if (get_relative_dist(order_hint_info, order_hint, cur_order_hint) > 0) |
1005 | 62.0k | cm->ref_frame_side[ref_frame] = 1; |
1006 | 91.6k | else if (order_hint == cur_order_hint) |
1007 | 34.8k | cm->ref_frame_side[ref_frame] = -1; |
1008 | 153k | } |
1009 | 21.9k | } |
1010 | | |
1011 | 6.33k | void av1_setup_motion_field(AV1_COMMON *cm) { |
1012 | 6.33k | const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info; |
1013 | | |
1014 | 6.33k | if (!order_hint_info->enable_order_hint) return; |
1015 | | |
1016 | 6.33k | TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs; |
1017 | 6.33k | int size = ((cm->mi_params.mi_rows + MAX_MIB_SIZE) >> 1) * |
1018 | 6.33k | (cm->mi_params.mi_stride >> 1); |
1019 | 1.80M | for (int idx = 0; idx < size; ++idx) { |
1020 | 1.79M | tpl_mvs_base[idx].mfmv0.as_int = INVALID_MV; |
1021 | 1.79M | tpl_mvs_base[idx].ref_frame_offset = 0; |
1022 | 1.79M | } |
1023 | | |
1024 | 6.33k | const int cur_order_hint = cm->cur_frame->order_hint; |
1025 | 6.33k | const RefCntBuffer *ref_buf[INTER_REFS_PER_FRAME]; |
1026 | 6.33k | int ref_order_hint[INTER_REFS_PER_FRAME]; |
1027 | | |
1028 | 50.7k | for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { |
1029 | 44.3k | const int ref_idx = ref_frame - LAST_FRAME; |
1030 | 44.3k | const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame); |
1031 | 44.3k | int order_hint = 0; |
1032 | | |
1033 | 44.3k | if (buf != NULL) order_hint = buf->order_hint; |
1034 | | |
1035 | 44.3k | ref_buf[ref_idx] = buf; |
1036 | 44.3k | ref_order_hint[ref_idx] = order_hint; |
1037 | 44.3k | } |
1038 | | |
1039 | 6.33k | int ref_stamp = MFMV_STACK_SIZE - 1; |
1040 | | |
1041 | 6.33k | if (ref_buf[LAST_FRAME - LAST_FRAME] != NULL) { |
1042 | 6.33k | const int alt_of_lst_order_hint = |
1043 | 6.33k | ref_buf[LAST_FRAME - LAST_FRAME] |
1044 | 6.33k | ->ref_order_hints[ALTREF_FRAME - LAST_FRAME]; |
1045 | | |
1046 | 6.33k | const int is_lst_overlay = |
1047 | 6.33k | (alt_of_lst_order_hint == ref_order_hint[GOLDEN_FRAME - LAST_FRAME]); |
1048 | 6.33k | if (!is_lst_overlay) motion_field_projection(cm, LAST_FRAME, 2); |
1049 | 6.33k | --ref_stamp; |
1050 | 6.33k | } |
1051 | | |
1052 | 6.33k | if (get_relative_dist(order_hint_info, |
1053 | 6.33k | ref_order_hint[BWDREF_FRAME - LAST_FRAME], |
1054 | 6.33k | cur_order_hint) > 0) { |
1055 | 2.20k | if (motion_field_projection(cm, BWDREF_FRAME, 0)) --ref_stamp; |
1056 | 2.20k | } |
1057 | | |
1058 | 6.33k | if (get_relative_dist(order_hint_info, |
1059 | 6.33k | ref_order_hint[ALTREF2_FRAME - LAST_FRAME], |
1060 | 6.33k | cur_order_hint) > 0) { |
1061 | 1.87k | if (motion_field_projection(cm, ALTREF2_FRAME, 0)) --ref_stamp; |
1062 | 1.87k | } |
1063 | | |
1064 | 6.33k | if (get_relative_dist(order_hint_info, |
1065 | 6.33k | ref_order_hint[ALTREF_FRAME - LAST_FRAME], |
1066 | 6.33k | cur_order_hint) > 0 && |
1067 | 6.33k | ref_stamp >= 0) |
1068 | 1.55k | if (motion_field_projection(cm, ALTREF_FRAME, 0)) --ref_stamp; |
1069 | | |
1070 | 6.33k | if (ref_stamp >= 0) motion_field_projection(cm, LAST2_FRAME, 2); |
1071 | 6.33k | } |
1072 | | |
1073 | | static inline void record_samples(const MB_MODE_INFO *mbmi, int *pts, |
1074 | | int *pts_inref, int row_offset, int sign_r, |
1075 | 11.0k | int col_offset, int sign_c) { |
1076 | 11.0k | const int bw = block_size_wide[mbmi->bsize]; |
1077 | 11.0k | const int bh = block_size_high[mbmi->bsize]; |
1078 | 11.0k | const int x = col_offset * MI_SIZE + sign_c * bw / 2 - 1; |
1079 | 11.0k | const int y = row_offset * MI_SIZE + sign_r * bh / 2 - 1; |
1080 | | |
1081 | 11.0k | pts[0] = GET_MV_SUBPEL(x); |
1082 | 11.0k | pts[1] = GET_MV_SUBPEL(y); |
1083 | 11.0k | pts_inref[0] = pts[0] + mbmi->mv[0].as_mv.col; |
1084 | 11.0k | pts_inref[1] = pts[1] + mbmi->mv[0].as_mv.row; |
1085 | 11.0k | } |
1086 | | |
1087 | | // Select samples according to the motion vector difference. |
1088 | | uint8_t av1_selectSamples(MV *mv, int *pts, int *pts_inref, int len, |
1089 | 390 | BLOCK_SIZE bsize) { |
1090 | 390 | const int bw = block_size_wide[bsize]; |
1091 | 390 | const int bh = block_size_high[bsize]; |
1092 | 390 | const int thresh = clamp(AOMMAX(bw, bh), 16, 112); |
1093 | 390 | uint8_t ret = 0; |
1094 | 390 | assert(len <= LEAST_SQUARES_SAMPLES_MAX); |
1095 | | |
1096 | | // Only keep the samples with MV differences within threshold. |
1097 | 1.33k | for (int i = 0; i < len; ++i) { |
1098 | 941 | const int diff = abs(pts_inref[2 * i] - pts[2 * i] - mv->col) + |
1099 | 941 | abs(pts_inref[2 * i + 1] - pts[2 * i + 1] - mv->row); |
1100 | 941 | if (diff > thresh) continue; |
1101 | 401 | if (ret != i) { |
1102 | 26 | memcpy(pts + 2 * ret, pts + 2 * i, 2 * sizeof(pts[0])); |
1103 | 26 | memcpy(pts_inref + 2 * ret, pts_inref + 2 * i, 2 * sizeof(pts_inref[0])); |
1104 | 26 | } |
1105 | 401 | ++ret; |
1106 | 401 | } |
1107 | | // Keep at least 1 sample. |
1108 | 390 | return AOMMAX(ret, 1); |
1109 | 390 | } |
1110 | | |
1111 | | // Note: Samples returned are at 1/8-pel precision |
1112 | | // Sample are the neighbor block center point's coordinates relative to the |
1113 | | // left-top pixel of current block. |
1114 | | uint8_t av1_findSamples(const AV1_COMMON *cm, MACROBLOCKD *xd, int *pts, |
1115 | 19.5k | int *pts_inref) { |
1116 | 19.5k | const MB_MODE_INFO *const mbmi0 = xd->mi[0]; |
1117 | 19.5k | const int ref_frame = mbmi0->ref_frame[0]; |
1118 | 19.5k | const int up_available = xd->up_available; |
1119 | 19.5k | const int left_available = xd->left_available; |
1120 | 19.5k | uint8_t np = 0; |
1121 | 19.5k | int do_tl = 1; |
1122 | 19.5k | int do_tr = 1; |
1123 | 19.5k | const int mi_stride = xd->mi_stride; |
1124 | 19.5k | const int mi_row = xd->mi_row; |
1125 | 19.5k | const int mi_col = xd->mi_col; |
1126 | | |
1127 | | // scan the nearest above rows |
1128 | 19.5k | if (up_available) { |
1129 | 5.88k | const int mi_row_offset = -1; |
1130 | 5.88k | const MB_MODE_INFO *mbmi = xd->mi[mi_row_offset * mi_stride]; |
1131 | 5.88k | uint8_t superblock_width = mi_size_wide[mbmi->bsize]; |
1132 | | |
1133 | 5.88k | if (xd->width <= superblock_width) { |
1134 | | // Handle "current block width <= above block width" case. |
1135 | 4.92k | const int col_offset = -mi_col % superblock_width; |
1136 | | |
1137 | 4.92k | if (col_offset < 0) do_tl = 0; |
1138 | 4.92k | if (col_offset + superblock_width > xd->width) do_tr = 0; |
1139 | | |
1140 | 4.92k | if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) { |
1141 | 2.44k | record_samples(mbmi, pts, pts_inref, 0, -1, col_offset, 1); |
1142 | 2.44k | pts += 2; |
1143 | 2.44k | pts_inref += 2; |
1144 | 2.44k | if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX; |
1145 | 2.44k | } |
1146 | 4.92k | } else { |
1147 | | // Handle "current block width > above block width" case. |
1148 | 2.80k | for (int i = 0; i < AOMMIN(xd->width, cm->mi_params.mi_cols - mi_col); |
1149 | 1.83k | i += superblock_width) { |
1150 | 1.83k | mbmi = xd->mi[i + mi_row_offset * mi_stride]; |
1151 | 1.83k | superblock_width = mi_size_wide[mbmi->bsize]; |
1152 | | |
1153 | 1.83k | if (mbmi->ref_frame[0] == ref_frame && |
1154 | 1.83k | mbmi->ref_frame[1] == NONE_FRAME) { |
1155 | 1.00k | record_samples(mbmi, pts, pts_inref, 0, -1, i, 1); |
1156 | 1.00k | pts += 2; |
1157 | 1.00k | pts_inref += 2; |
1158 | 1.00k | if (++np >= LEAST_SQUARES_SAMPLES_MAX) |
1159 | 0 | return LEAST_SQUARES_SAMPLES_MAX; |
1160 | 1.00k | } |
1161 | 1.83k | } |
1162 | 968 | } |
1163 | 5.88k | } |
1164 | 19.5k | assert(np <= LEAST_SQUARES_SAMPLES_MAX); |
1165 | | |
1166 | | // scan the nearest left columns |
1167 | 19.5k | if (left_available) { |
1168 | 10.4k | const int mi_col_offset = -1; |
1169 | 10.4k | const MB_MODE_INFO *mbmi = xd->mi[mi_col_offset]; |
1170 | 10.4k | uint8_t superblock_height = mi_size_high[mbmi->bsize]; |
1171 | | |
1172 | 10.4k | if (xd->height <= superblock_height) { |
1173 | | // Handle "current block height <= above block height" case. |
1174 | 8.07k | const int row_offset = -mi_row % superblock_height; |
1175 | | |
1176 | 8.07k | if (row_offset < 0) do_tl = 0; |
1177 | | |
1178 | 8.07k | if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) { |
1179 | 4.66k | record_samples(mbmi, pts, pts_inref, row_offset, 1, 0, -1); |
1180 | 4.66k | pts += 2; |
1181 | 4.66k | pts_inref += 2; |
1182 | 4.66k | np++; |
1183 | 4.66k | if (np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX; |
1184 | 4.66k | } |
1185 | 8.07k | } else { |
1186 | | // Handle "current block height > above block height" case. |
1187 | 6.85k | for (int i = 0; i < AOMMIN(xd->height, cm->mi_params.mi_rows - mi_row); |
1188 | 4.47k | i += superblock_height) { |
1189 | 4.47k | mbmi = xd->mi[mi_col_offset + i * mi_stride]; |
1190 | 4.47k | superblock_height = mi_size_high[mbmi->bsize]; |
1191 | | |
1192 | 4.47k | if (mbmi->ref_frame[0] == ref_frame && |
1193 | 4.47k | mbmi->ref_frame[1] == NONE_FRAME) { |
1194 | 2.22k | record_samples(mbmi, pts, pts_inref, i, 1, 0, -1); |
1195 | 2.22k | pts += 2; |
1196 | 2.22k | pts_inref += 2; |
1197 | 2.22k | if (++np >= LEAST_SQUARES_SAMPLES_MAX) |
1198 | 1 | return LEAST_SQUARES_SAMPLES_MAX; |
1199 | 2.22k | } |
1200 | 4.47k | } |
1201 | 2.37k | } |
1202 | 10.4k | } |
1203 | 19.5k | assert(np <= LEAST_SQUARES_SAMPLES_MAX); |
1204 | | |
1205 | | // Top-left block |
1206 | 19.5k | if (do_tl && left_available && up_available) { |
1207 | 1.33k | const int mi_row_offset = -1; |
1208 | 1.33k | const int mi_col_offset = -1; |
1209 | 1.33k | MB_MODE_INFO *mbmi = xd->mi[mi_col_offset + mi_row_offset * mi_stride]; |
1210 | | |
1211 | 1.33k | if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) { |
1212 | 476 | record_samples(mbmi, pts, pts_inref, 0, -1, 0, -1); |
1213 | 476 | pts += 2; |
1214 | 476 | pts_inref += 2; |
1215 | 476 | if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX; |
1216 | 476 | } |
1217 | 1.33k | } |
1218 | 19.5k | assert(np <= LEAST_SQUARES_SAMPLES_MAX); |
1219 | | |
1220 | | // Top-right block |
1221 | 19.5k | if (do_tr && |
1222 | 19.5k | has_top_right(cm, xd, mi_row, mi_col, AOMMAX(xd->width, xd->height))) { |
1223 | 16.7k | const POSITION trb_pos = { -1, xd->width }; |
1224 | 16.7k | const TileInfo *const tile = &xd->tile; |
1225 | 16.7k | if (is_inside(tile, mi_col, mi_row, &trb_pos)) { |
1226 | 759 | const int mi_row_offset = -1; |
1227 | 759 | const int mi_col_offset = xd->width; |
1228 | 759 | const MB_MODE_INFO *mbmi = |
1229 | 759 | xd->mi[mi_col_offset + mi_row_offset * mi_stride]; |
1230 | | |
1231 | 759 | if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) { |
1232 | 269 | record_samples(mbmi, pts, pts_inref, 0, -1, xd->width, 1); |
1233 | 269 | if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX; |
1234 | 269 | } |
1235 | 759 | } |
1236 | 16.7k | } |
1237 | 19.5k | assert(np <= LEAST_SQUARES_SAMPLES_MAX); |
1238 | | |
1239 | 19.5k | return np; |
1240 | 19.5k | } |
1241 | | |
1242 | 54.7k | void av1_setup_skip_mode_allowed(AV1_COMMON *cm) { |
1243 | 54.7k | const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info; |
1244 | 54.7k | SkipModeInfo *const skip_mode_info = &cm->current_frame.skip_mode_info; |
1245 | | |
1246 | 54.7k | skip_mode_info->skip_mode_allowed = 0; |
1247 | 54.7k | skip_mode_info->ref_frame_idx_0 = INVALID_IDX; |
1248 | 54.7k | skip_mode_info->ref_frame_idx_1 = INVALID_IDX; |
1249 | | |
1250 | 54.7k | if (!order_hint_info->enable_order_hint || frame_is_intra_only(cm) || |
1251 | 54.7k | cm->current_frame.reference_mode == SINGLE_REFERENCE) |
1252 | 46.0k | return; |
1253 | | |
1254 | 8.75k | const int cur_order_hint = cm->current_frame.order_hint; |
1255 | 8.75k | int ref_order_hints[2] = { -1, INT_MAX }; |
1256 | 8.75k | int ref_idx[2] = { INVALID_IDX, INVALID_IDX }; |
1257 | | |
1258 | | // Identify the nearest forward and backward references. |
1259 | 70.0k | for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) { |
1260 | 61.2k | const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i); |
1261 | 61.2k | if (buf == NULL) continue; |
1262 | | |
1263 | 61.2k | const int ref_order_hint = buf->order_hint; |
1264 | 61.2k | if (get_relative_dist(order_hint_info, ref_order_hint, cur_order_hint) < |
1265 | 61.2k | 0) { |
1266 | | // Forward reference |
1267 | 33.4k | if (ref_order_hints[0] == -1 || |
1268 | 33.4k | get_relative_dist(order_hint_info, ref_order_hint, |
1269 | 25.6k | ref_order_hints[0]) > 0) { |
1270 | 10.2k | ref_order_hints[0] = ref_order_hint; |
1271 | 10.2k | ref_idx[0] = i; |
1272 | 10.2k | } |
1273 | 33.4k | } else if (get_relative_dist(order_hint_info, ref_order_hint, |
1274 | 27.8k | cur_order_hint) > 0) { |
1275 | | // Backward reference |
1276 | 15.2k | if (ref_order_hints[1] == INT_MAX || |
1277 | 15.2k | get_relative_dist(order_hint_info, ref_order_hint, |
1278 | 10.8k | ref_order_hints[1]) < 0) { |
1279 | 5.23k | ref_order_hints[1] = ref_order_hint; |
1280 | 5.23k | ref_idx[1] = i; |
1281 | 5.23k | } |
1282 | 15.2k | } |
1283 | 61.2k | } |
1284 | | |
1285 | 8.75k | if (ref_idx[0] != INVALID_IDX && ref_idx[1] != INVALID_IDX) { |
1286 | | // == Bi-directional prediction == |
1287 | 3.54k | skip_mode_info->skip_mode_allowed = 1; |
1288 | 3.54k | skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]); |
1289 | 3.54k | skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]); |
1290 | 5.21k | } else if (ref_idx[0] != INVALID_IDX && ref_idx[1] == INVALID_IDX) { |
1291 | | // == Forward prediction only == |
1292 | | // Identify the second nearest forward reference. |
1293 | 4.25k | ref_order_hints[1] = -1; |
1294 | 34.0k | for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) { |
1295 | 29.7k | const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i); |
1296 | 29.7k | if (buf == NULL) continue; |
1297 | | |
1298 | 29.7k | const int ref_order_hint = buf->order_hint; |
1299 | 29.7k | if ((ref_order_hints[0] != -1 && |
1300 | 29.7k | get_relative_dist(order_hint_info, ref_order_hint, |
1301 | 29.7k | ref_order_hints[0]) < 0) && |
1302 | 29.7k | (ref_order_hints[1] == -1 || |
1303 | 6.79k | get_relative_dist(order_hint_info, ref_order_hint, |
1304 | 4.40k | ref_order_hints[1]) > 0)) { |
1305 | | // Second closest forward reference |
1306 | 3.01k | ref_order_hints[1] = ref_order_hint; |
1307 | 3.01k | ref_idx[1] = i; |
1308 | 3.01k | } |
1309 | 29.7k | } |
1310 | 4.25k | if (ref_order_hints[1] != -1) { |
1311 | 2.38k | skip_mode_info->skip_mode_allowed = 1; |
1312 | 2.38k | skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]); |
1313 | 2.38k | skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]); |
1314 | 2.38k | } |
1315 | 4.25k | } |
1316 | 8.75k | } |
1317 | | |
1318 | | typedef struct { |
1319 | | int map_idx; // frame map index |
1320 | | RefCntBuffer *buf; // frame buffer |
1321 | | int sort_idx; // index based on the offset to be used for sorting |
1322 | | } REF_FRAME_INFO; |
1323 | | |
1324 | | // Compares the sort_idx fields. If they are equal, then compares the map_idx |
1325 | | // fields to break the tie. This ensures a stable sort. |
1326 | 50.8k | static int compare_ref_frame_info(const void *arg_a, const void *arg_b) { |
1327 | 50.8k | const REF_FRAME_INFO *info_a = (REF_FRAME_INFO *)arg_a; |
1328 | 50.8k | const REF_FRAME_INFO *info_b = (REF_FRAME_INFO *)arg_b; |
1329 | | |
1330 | 50.8k | const int sort_idx_diff = info_a->sort_idx - info_b->sort_idx; |
1331 | 50.8k | if (sort_idx_diff != 0) return sort_idx_diff; |
1332 | 28.3k | return info_a->map_idx - info_b->map_idx; |
1333 | 50.8k | } |
1334 | | |
1335 | | static inline void set_ref_frame_info(int *remapped_ref_idx, int frame_idx, |
1336 | 24.9k | REF_FRAME_INFO *ref_info) { |
1337 | 24.9k | assert(frame_idx >= 0 && frame_idx < INTER_REFS_PER_FRAME); |
1338 | | |
1339 | 24.9k | remapped_ref_idx[frame_idx] = ref_info->map_idx; |
1340 | 24.9k | } |
1341 | | |
1342 | | void av1_set_frame_refs(AV1_COMMON *const cm, int *remapped_ref_idx, |
1343 | 3.59k | int lst_map_idx, int gld_map_idx) { |
1344 | 3.59k | int lst_frame_sort_idx = -1; |
1345 | 3.59k | int gld_frame_sort_idx = -1; |
1346 | | |
1347 | 3.59k | assert(cm->seq_params->order_hint_info.enable_order_hint); |
1348 | 3.59k | assert(cm->seq_params->order_hint_info.order_hint_bits_minus_1 >= 0); |
1349 | 3.59k | const int cur_order_hint = (int)cm->current_frame.order_hint; |
1350 | 3.59k | const int cur_frame_sort_idx = |
1351 | 3.59k | 1 << cm->seq_params->order_hint_info.order_hint_bits_minus_1; |
1352 | | |
1353 | 3.59k | REF_FRAME_INFO ref_frame_info[REF_FRAMES]; |
1354 | 3.59k | int ref_flag_list[INTER_REFS_PER_FRAME] = { 0, 0, 0, 0, 0, 0, 0 }; |
1355 | | |
1356 | 32.3k | for (int i = 0; i < REF_FRAMES; ++i) { |
1357 | 28.7k | const int map_idx = i; |
1358 | | |
1359 | 28.7k | ref_frame_info[i].map_idx = map_idx; |
1360 | 28.7k | ref_frame_info[i].sort_idx = -1; |
1361 | | |
1362 | 28.7k | RefCntBuffer *const buf = cm->ref_frame_map[map_idx]; |
1363 | 28.7k | ref_frame_info[i].buf = buf; |
1364 | | |
1365 | 28.7k | if (buf == NULL) continue; |
1366 | | // If this assertion fails, there is a reference leak. |
1367 | 23.5k | assert(buf->ref_count > 0); |
1368 | | |
1369 | 23.5k | const int offset = (int)buf->order_hint; |
1370 | 23.5k | ref_frame_info[i].sort_idx = |
1371 | 23.5k | (offset == -1) ? -1 |
1372 | 23.5k | : cur_frame_sort_idx + |
1373 | 23.5k | get_relative_dist(&cm->seq_params->order_hint_info, |
1374 | 23.5k | offset, cur_order_hint); |
1375 | 23.5k | assert(ref_frame_info[i].sort_idx >= -1); |
1376 | | |
1377 | 23.5k | if (map_idx == lst_map_idx) lst_frame_sort_idx = ref_frame_info[i].sort_idx; |
1378 | 23.5k | if (map_idx == gld_map_idx) gld_frame_sort_idx = ref_frame_info[i].sort_idx; |
1379 | 23.5k | } |
1380 | | |
1381 | | // Confirm both LAST_FRAME and GOLDEN_FRAME are valid forward reference |
1382 | | // frames. |
1383 | 3.59k | if (lst_frame_sort_idx == -1 || lst_frame_sort_idx >= cur_frame_sort_idx) { |
1384 | 26 | aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME, |
1385 | 26 | "Inter frame requests a look-ahead frame as LAST"); |
1386 | 26 | } |
1387 | 3.59k | if (gld_frame_sort_idx == -1 || gld_frame_sort_idx >= cur_frame_sort_idx) { |
1388 | 8 | aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME, |
1389 | 8 | "Inter frame requests a look-ahead frame as GOLDEN"); |
1390 | 8 | } |
1391 | | |
1392 | | // Sort ref frames based on their frame_offset values. |
1393 | 3.59k | qsort(ref_frame_info, REF_FRAMES, sizeof(REF_FRAME_INFO), |
1394 | 3.59k | compare_ref_frame_info); |
1395 | | |
1396 | | // Identify forward and backward reference frames. |
1397 | | // Forward reference: offset < order_hint |
1398 | | // Backward reference: offset >= order_hint |
1399 | 3.59k | int fwd_start_idx = 0, fwd_end_idx = REF_FRAMES - 1; |
1400 | | |
1401 | 26.3k | for (int i = 0; i < REF_FRAMES; i++) { |
1402 | 24.4k | if (ref_frame_info[i].sort_idx == -1) { |
1403 | 5.23k | fwd_start_idx++; |
1404 | 5.23k | continue; |
1405 | 5.23k | } |
1406 | | |
1407 | 19.1k | if (ref_frame_info[i].sort_idx >= cur_frame_sort_idx) { |
1408 | 1.64k | fwd_end_idx = i - 1; |
1409 | 1.64k | break; |
1410 | 1.64k | } |
1411 | 19.1k | } |
1412 | | |
1413 | 3.59k | int bwd_start_idx = fwd_end_idx + 1; |
1414 | 3.59k | int bwd_end_idx = REF_FRAMES - 1; |
1415 | | |
1416 | | // === Backward Reference Frames === |
1417 | | |
1418 | | // == ALTREF_FRAME == |
1419 | 3.59k | if (bwd_start_idx <= bwd_end_idx) { |
1420 | 1.64k | set_ref_frame_info(remapped_ref_idx, ALTREF_FRAME - LAST_FRAME, |
1421 | 1.64k | &ref_frame_info[bwd_end_idx]); |
1422 | 1.64k | ref_flag_list[ALTREF_FRAME - LAST_FRAME] = 1; |
1423 | 1.64k | bwd_end_idx--; |
1424 | 1.64k | } |
1425 | | |
1426 | | // == BWDREF_FRAME == |
1427 | 3.59k | if (bwd_start_idx <= bwd_end_idx) { |
1428 | 1.29k | set_ref_frame_info(remapped_ref_idx, BWDREF_FRAME - LAST_FRAME, |
1429 | 1.29k | &ref_frame_info[bwd_start_idx]); |
1430 | 1.29k | ref_flag_list[BWDREF_FRAME - LAST_FRAME] = 1; |
1431 | 1.29k | bwd_start_idx++; |
1432 | 1.29k | } |
1433 | | |
1434 | | // == ALTREF2_FRAME == |
1435 | 3.59k | if (bwd_start_idx <= bwd_end_idx) { |
1436 | 944 | set_ref_frame_info(remapped_ref_idx, ALTREF2_FRAME - LAST_FRAME, |
1437 | 944 | &ref_frame_info[bwd_start_idx]); |
1438 | 944 | ref_flag_list[ALTREF2_FRAME - LAST_FRAME] = 1; |
1439 | 944 | } |
1440 | | |
1441 | | // === Forward Reference Frames === |
1442 | | |
1443 | 21.1k | for (int i = fwd_start_idx; i <= fwd_end_idx; ++i) { |
1444 | | // == LAST_FRAME == |
1445 | 17.5k | if (ref_frame_info[i].map_idx == lst_map_idx) { |
1446 | 3.56k | set_ref_frame_info(remapped_ref_idx, LAST_FRAME - LAST_FRAME, |
1447 | 3.56k | &ref_frame_info[i]); |
1448 | 3.56k | ref_flag_list[LAST_FRAME - LAST_FRAME] = 1; |
1449 | 3.56k | } |
1450 | | |
1451 | | // == GOLDEN_FRAME == |
1452 | 17.5k | if (ref_frame_info[i].map_idx == gld_map_idx) { |
1453 | 3.56k | set_ref_frame_info(remapped_ref_idx, GOLDEN_FRAME - LAST_FRAME, |
1454 | 3.56k | &ref_frame_info[i]); |
1455 | 3.56k | ref_flag_list[GOLDEN_FRAME - LAST_FRAME] = 1; |
1456 | 3.56k | } |
1457 | 17.5k | } |
1458 | | |
1459 | 3.59k | assert(ref_flag_list[LAST_FRAME - LAST_FRAME] == 1 && |
1460 | 3.59k | ref_flag_list[GOLDEN_FRAME - LAST_FRAME] == 1); |
1461 | | |
1462 | | // == LAST2_FRAME == |
1463 | | // == LAST3_FRAME == |
1464 | | // == BWDREF_FRAME == |
1465 | | // == ALTREF2_FRAME == |
1466 | | // == ALTREF_FRAME == |
1467 | | |
1468 | | // Set up the reference frames in the anti-chronological order. |
1469 | 3.59k | static const MV_REFERENCE_FRAME ref_frame_list[INTER_REFS_PER_FRAME - 2] = { |
1470 | 3.59k | LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME |
1471 | 3.59k | }; |
1472 | | |
1473 | 3.59k | int ref_idx; |
1474 | 16.4k | for (ref_idx = 0; ref_idx < (INTER_REFS_PER_FRAME - 2); ref_idx++) { |
1475 | 14.7k | const MV_REFERENCE_FRAME ref_frame = ref_frame_list[ref_idx]; |
1476 | | |
1477 | 14.7k | if (ref_flag_list[ref_frame - LAST_FRAME] == 1) continue; |
1478 | | |
1479 | 18.2k | while (fwd_start_idx <= fwd_end_idx && |
1480 | 18.2k | (ref_frame_info[fwd_end_idx].map_idx == lst_map_idx || |
1481 | 16.3k | ref_frame_info[fwd_end_idx].map_idx == gld_map_idx)) { |
1482 | 5.25k | fwd_end_idx--; |
1483 | 5.25k | } |
1484 | 12.9k | if (fwd_start_idx > fwd_end_idx) break; |
1485 | | |
1486 | 11.0k | set_ref_frame_info(remapped_ref_idx, ref_frame - LAST_FRAME, |
1487 | 11.0k | &ref_frame_info[fwd_end_idx]); |
1488 | 11.0k | ref_flag_list[ref_frame - LAST_FRAME] = 1; |
1489 | | |
1490 | 11.0k | fwd_end_idx--; |
1491 | 11.0k | } |
1492 | | |
1493 | | // Assign all the remaining frame(s), if any, to the earliest reference |
1494 | | // frame. |
1495 | 8.59k | for (; ref_idx < (INTER_REFS_PER_FRAME - 2); ref_idx++) { |
1496 | 4.99k | const MV_REFERENCE_FRAME ref_frame = ref_frame_list[ref_idx]; |
1497 | 4.99k | if (ref_flag_list[ref_frame - LAST_FRAME] == 1) continue; |
1498 | 2.86k | set_ref_frame_info(remapped_ref_idx, ref_frame - LAST_FRAME, |
1499 | 2.86k | &ref_frame_info[fwd_start_idx]); |
1500 | 2.86k | ref_flag_list[ref_frame - LAST_FRAME] = 1; |
1501 | 2.86k | } |
1502 | | |
1503 | 28.5k | for (int i = 0; i < INTER_REFS_PER_FRAME; i++) { |
1504 | 24.9k | assert(ref_flag_list[i] == 1); |
1505 | 24.9k | } |
1506 | 3.59k | } |