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