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