Line data Source code
1 : /*
2 : Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #include <assert.h>
12 :
13 : #include "vp9/common/vp9_common.h"
14 : #include "vp9/common/vp9_entropy.h"
15 : #include "vp9/common/vp9_entropymode.h"
16 : #include "vp9/common/vp9_entropymv.h"
17 : #include "vp9/common/vp9_mvref_common.h"
18 : #include "vp9/common/vp9_pred_common.h"
19 : #include "vp9/common/vp9_reconinter.h"
20 : #include "vp9/common/vp9_seg_common.h"
21 :
22 : #include "vp9/decoder/vp9_decodemv.h"
23 : #include "vp9/decoder/vp9_decodeframe.h"
24 :
25 : #include "vpx_dsp/vpx_dsp_common.h"
26 :
27 0 : static PREDICTION_MODE read_intra_mode(vpx_reader *r, const vpx_prob *p) {
28 0 : return (PREDICTION_MODE)vpx_read_tree(r, vp9_intra_mode_tree, p);
29 : }
30 :
31 0 : static PREDICTION_MODE read_intra_mode_y(VP9_COMMON *cm, MACROBLOCKD *xd,
32 : vpx_reader *r, int size_group) {
33 0 : const PREDICTION_MODE y_mode =
34 0 : read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
35 0 : FRAME_COUNTS *counts = xd->counts;
36 0 : if (counts) ++counts->y_mode[size_group][y_mode];
37 0 : return y_mode;
38 : }
39 :
40 0 : static PREDICTION_MODE read_intra_mode_uv(VP9_COMMON *cm, MACROBLOCKD *xd,
41 : vpx_reader *r,
42 : PREDICTION_MODE y_mode) {
43 0 : const PREDICTION_MODE uv_mode =
44 0 : read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
45 0 : FRAME_COUNTS *counts = xd->counts;
46 0 : if (counts) ++counts->uv_mode[y_mode][uv_mode];
47 0 : return uv_mode;
48 : }
49 :
50 0 : static PREDICTION_MODE read_inter_mode(VP9_COMMON *cm, MACROBLOCKD *xd,
51 : vpx_reader *r, int ctx) {
52 0 : const int mode =
53 0 : vpx_read_tree(r, vp9_inter_mode_tree, cm->fc->inter_mode_probs[ctx]);
54 0 : FRAME_COUNTS *counts = xd->counts;
55 0 : if (counts) ++counts->inter_mode[ctx][mode];
56 :
57 0 : return NEARESTMV + mode;
58 : }
59 :
60 0 : static int read_segment_id(vpx_reader *r, const struct segmentation *seg) {
61 0 : return vpx_read_tree(r, vp9_segment_tree, seg->tree_probs);
62 : }
63 :
64 0 : static TX_SIZE read_selected_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
65 : TX_SIZE max_tx_size, vpx_reader *r) {
66 0 : FRAME_COUNTS *counts = xd->counts;
67 0 : const int ctx = get_tx_size_context(xd);
68 0 : const vpx_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
69 0 : int tx_size = vpx_read(r, tx_probs[0]);
70 0 : if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
71 0 : tx_size += vpx_read(r, tx_probs[1]);
72 0 : if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
73 0 : tx_size += vpx_read(r, tx_probs[2]);
74 : }
75 :
76 0 : if (counts) ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
77 0 : return (TX_SIZE)tx_size;
78 : }
79 :
80 0 : static INLINE TX_SIZE read_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
81 : int allow_select, vpx_reader *r) {
82 0 : TX_MODE tx_mode = cm->tx_mode;
83 0 : BLOCK_SIZE bsize = xd->mi[0]->sb_type;
84 0 : const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
85 0 : if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
86 0 : return read_selected_tx_size(cm, xd, max_tx_size, r);
87 : else
88 0 : return VPXMIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
89 : }
90 :
91 0 : static int dec_get_segment_id(const VP9_COMMON *cm, const uint8_t *segment_ids,
92 : int mi_offset, int x_mis, int y_mis) {
93 0 : int x, y, segment_id = INT_MAX;
94 :
95 0 : for (y = 0; y < y_mis; y++)
96 0 : for (x = 0; x < x_mis; x++)
97 0 : segment_id =
98 0 : VPXMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
99 :
100 0 : assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
101 0 : return segment_id;
102 : }
103 :
104 0 : static void set_segment_id(VP9_COMMON *cm, int mi_offset, int x_mis, int y_mis,
105 : int segment_id) {
106 : int x, y;
107 :
108 0 : assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
109 :
110 0 : for (y = 0; y < y_mis; y++)
111 0 : for (x = 0; x < x_mis; x++)
112 0 : cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
113 0 : }
114 :
115 0 : static void copy_segment_id(const VP9_COMMON *cm,
116 : const uint8_t *last_segment_ids,
117 : uint8_t *current_segment_ids, int mi_offset,
118 : int x_mis, int y_mis) {
119 : int x, y;
120 :
121 0 : for (y = 0; y < y_mis; y++)
122 0 : for (x = 0; x < x_mis; x++)
123 0 : current_segment_ids[mi_offset + y * cm->mi_cols + x] =
124 0 : last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
125 : : 0;
126 0 : }
127 :
128 0 : static int read_intra_segment_id(VP9_COMMON *const cm, int mi_offset, int x_mis,
129 : int y_mis, vpx_reader *r) {
130 0 : struct segmentation *const seg = &cm->seg;
131 : int segment_id;
132 :
133 0 : if (!seg->enabled) return 0; // Default for disabled segmentation
134 :
135 0 : if (!seg->update_map) {
136 0 : copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
137 : mi_offset, x_mis, y_mis);
138 0 : return 0;
139 : }
140 :
141 0 : segment_id = read_segment_id(r, seg);
142 0 : set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
143 0 : return segment_id;
144 : }
145 :
146 0 : static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
147 : int mi_row, int mi_col, vpx_reader *r,
148 : int x_mis, int y_mis) {
149 0 : struct segmentation *const seg = &cm->seg;
150 0 : MODE_INFO *const mi = xd->mi[0];
151 : int predicted_segment_id, segment_id;
152 0 : const int mi_offset = mi_row * cm->mi_cols + mi_col;
153 :
154 0 : if (!seg->enabled) return 0; // Default for disabled segmentation
155 :
156 0 : predicted_segment_id = cm->last_frame_seg_map
157 0 : ? dec_get_segment_id(cm, cm->last_frame_seg_map,
158 : mi_offset, x_mis, y_mis)
159 0 : : 0;
160 :
161 0 : if (!seg->update_map) {
162 0 : copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
163 : mi_offset, x_mis, y_mis);
164 0 : return predicted_segment_id;
165 : }
166 :
167 0 : if (seg->temporal_update) {
168 0 : const vpx_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
169 0 : mi->seg_id_predicted = vpx_read(r, pred_prob);
170 0 : segment_id =
171 0 : mi->seg_id_predicted ? predicted_segment_id : read_segment_id(r, seg);
172 : } else {
173 0 : segment_id = read_segment_id(r, seg);
174 : }
175 0 : set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
176 0 : return segment_id;
177 : }
178 :
179 0 : static int read_skip(VP9_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
180 : vpx_reader *r) {
181 0 : if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
182 0 : return 1;
183 : } else {
184 0 : const int ctx = vp9_get_skip_context(xd);
185 0 : const int skip = vpx_read(r, cm->fc->skip_probs[ctx]);
186 0 : FRAME_COUNTS *counts = xd->counts;
187 0 : if (counts) ++counts->skip[ctx][skip];
188 0 : return skip;
189 : }
190 : }
191 :
192 0 : static void read_intra_frame_mode_info(VP9_COMMON *const cm,
193 : MACROBLOCKD *const xd, int mi_row,
194 : int mi_col, vpx_reader *r, int x_mis,
195 : int y_mis) {
196 0 : MODE_INFO *const mi = xd->mi[0];
197 0 : const MODE_INFO *above_mi = xd->above_mi;
198 0 : const MODE_INFO *left_mi = xd->left_mi;
199 0 : const BLOCK_SIZE bsize = mi->sb_type;
200 : int i;
201 0 : const int mi_offset = mi_row * cm->mi_cols + mi_col;
202 :
203 0 : mi->segment_id = read_intra_segment_id(cm, mi_offset, x_mis, y_mis, r);
204 0 : mi->skip = read_skip(cm, xd, mi->segment_id, r);
205 0 : mi->tx_size = read_tx_size(cm, xd, 1, r);
206 0 : mi->ref_frame[0] = INTRA_FRAME;
207 0 : mi->ref_frame[1] = NONE;
208 :
209 0 : switch (bsize) {
210 : case BLOCK_4X4:
211 0 : for (i = 0; i < 4; ++i)
212 0 : mi->bmi[i].as_mode =
213 0 : read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, i));
214 0 : mi->mode = mi->bmi[3].as_mode;
215 0 : break;
216 : case BLOCK_4X8:
217 0 : mi->bmi[0].as_mode = mi->bmi[2].as_mode =
218 0 : read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
219 0 : mi->bmi[1].as_mode = mi->bmi[3].as_mode = mi->mode =
220 0 : read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 1));
221 0 : break;
222 : case BLOCK_8X4:
223 0 : mi->bmi[0].as_mode = mi->bmi[1].as_mode =
224 0 : read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
225 0 : mi->bmi[2].as_mode = mi->bmi[3].as_mode = mi->mode =
226 0 : read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 2));
227 0 : break;
228 : default:
229 0 : mi->mode = read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
230 : }
231 :
232 0 : mi->uv_mode = read_intra_mode(r, vp9_kf_uv_mode_prob[mi->mode]);
233 0 : }
234 :
235 0 : static int read_mv_component(vpx_reader *r, const nmv_component *mvcomp,
236 : int usehp) {
237 : int mag, d, fr, hp;
238 0 : const int sign = vpx_read(r, mvcomp->sign);
239 0 : const int mv_class = vpx_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
240 0 : const int class0 = mv_class == MV_CLASS_0;
241 :
242 : // Integer part
243 0 : if (class0) {
244 0 : d = vpx_read(r, mvcomp->class0[0]);
245 0 : mag = 0;
246 : } else {
247 : int i;
248 0 : const int n = mv_class + CLASS0_BITS - 1; // number of bits
249 :
250 0 : d = 0;
251 0 : for (i = 0; i < n; ++i) d |= vpx_read(r, mvcomp->bits[i]) << i;
252 0 : mag = CLASS0_SIZE << (mv_class + 2);
253 : }
254 :
255 : // Fractional part
256 0 : fr = vpx_read_tree(r, vp9_mv_fp_tree,
257 : class0 ? mvcomp->class0_fp[d] : mvcomp->fp);
258 :
259 : // High precision part (if hp is not used, the default value of the hp is 1)
260 0 : hp = usehp ? vpx_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp) : 1;
261 :
262 : // Result
263 0 : mag += ((d << 3) | (fr << 1) | hp) + 1;
264 0 : return sign ? -mag : mag;
265 : }
266 :
267 0 : static INLINE void read_mv(vpx_reader *r, MV *mv, const MV *ref,
268 : const nmv_context *ctx, nmv_context_counts *counts,
269 : int allow_hp) {
270 0 : const MV_JOINT_TYPE joint_type =
271 0 : (MV_JOINT_TYPE)vpx_read_tree(r, vp9_mv_joint_tree, ctx->joints);
272 0 : const int use_hp = allow_hp && use_mv_hp(ref);
273 0 : MV diff = { 0, 0 };
274 :
275 0 : if (mv_joint_vertical(joint_type))
276 0 : diff.row = read_mv_component(r, &ctx->comps[0], use_hp);
277 :
278 0 : if (mv_joint_horizontal(joint_type))
279 0 : diff.col = read_mv_component(r, &ctx->comps[1], use_hp);
280 :
281 0 : vp9_inc_mv(&diff, counts);
282 :
283 0 : mv->row = ref->row + diff.row;
284 0 : mv->col = ref->col + diff.col;
285 0 : }
286 :
287 0 : static REFERENCE_MODE read_block_reference_mode(VP9_COMMON *cm,
288 : const MACROBLOCKD *xd,
289 : vpx_reader *r) {
290 0 : if (cm->reference_mode == REFERENCE_MODE_SELECT) {
291 0 : const int ctx = vp9_get_reference_mode_context(cm, xd);
292 0 : const REFERENCE_MODE mode =
293 0 : (REFERENCE_MODE)vpx_read(r, cm->fc->comp_inter_prob[ctx]);
294 0 : FRAME_COUNTS *counts = xd->counts;
295 0 : if (counts) ++counts->comp_inter[ctx][mode];
296 0 : return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
297 : } else {
298 0 : return cm->reference_mode;
299 : }
300 : }
301 :
302 : // Read the referncence frame
303 0 : static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
304 : vpx_reader *r, int segment_id,
305 : MV_REFERENCE_FRAME ref_frame[2]) {
306 0 : FRAME_CONTEXT *const fc = cm->fc;
307 0 : FRAME_COUNTS *counts = xd->counts;
308 :
309 0 : if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
310 0 : ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
311 : SEG_LVL_REF_FRAME);
312 0 : ref_frame[1] = NONE;
313 : } else {
314 0 : const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
315 : // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
316 0 : if (mode == COMPOUND_REFERENCE) {
317 0 : const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
318 0 : const int ctx = vp9_get_pred_context_comp_ref_p(cm, xd);
319 0 : const int bit = vpx_read(r, fc->comp_ref_prob[ctx]);
320 0 : if (counts) ++counts->comp_ref[ctx][bit];
321 0 : ref_frame[idx] = cm->comp_fixed_ref;
322 0 : ref_frame[!idx] = cm->comp_var_ref[bit];
323 0 : } else if (mode == SINGLE_REFERENCE) {
324 0 : const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
325 0 : const int bit0 = vpx_read(r, fc->single_ref_prob[ctx0][0]);
326 0 : if (counts) ++counts->single_ref[ctx0][0][bit0];
327 0 : if (bit0) {
328 0 : const int ctx1 = vp9_get_pred_context_single_ref_p2(xd);
329 0 : const int bit1 = vpx_read(r, fc->single_ref_prob[ctx1][1]);
330 0 : if (counts) ++counts->single_ref[ctx1][1][bit1];
331 0 : ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
332 : } else {
333 0 : ref_frame[0] = LAST_FRAME;
334 : }
335 :
336 0 : ref_frame[1] = NONE;
337 : } else {
338 0 : assert(0 && "Invalid prediction mode.");
339 : }
340 : }
341 0 : }
342 :
343 0 : static INLINE INTERP_FILTER read_switchable_interp_filter(VP9_COMMON *const cm,
344 : MACROBLOCKD *const xd,
345 : vpx_reader *r) {
346 0 : const int ctx = get_pred_context_switchable_interp(xd);
347 0 : const INTERP_FILTER type = (INTERP_FILTER)vpx_read_tree(
348 0 : r, vp9_switchable_interp_tree, cm->fc->switchable_interp_prob[ctx]);
349 0 : FRAME_COUNTS *counts = xd->counts;
350 0 : if (counts) ++counts->switchable_interp[ctx][type];
351 0 : return type;
352 : }
353 :
354 0 : static void read_intra_block_mode_info(VP9_COMMON *const cm,
355 : MACROBLOCKD *const xd, MODE_INFO *mi,
356 : vpx_reader *r) {
357 0 : const BLOCK_SIZE bsize = mi->sb_type;
358 : int i;
359 :
360 0 : switch (bsize) {
361 : case BLOCK_4X4:
362 0 : for (i = 0; i < 4; ++i)
363 0 : mi->bmi[i].as_mode = read_intra_mode_y(cm, xd, r, 0);
364 0 : mi->mode = mi->bmi[3].as_mode;
365 0 : break;
366 : case BLOCK_4X8:
367 0 : mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd, r, 0);
368 0 : mi->bmi[1].as_mode = mi->bmi[3].as_mode = mi->mode =
369 0 : read_intra_mode_y(cm, xd, r, 0);
370 0 : break;
371 : case BLOCK_8X4:
372 0 : mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd, r, 0);
373 0 : mi->bmi[2].as_mode = mi->bmi[3].as_mode = mi->mode =
374 0 : read_intra_mode_y(cm, xd, r, 0);
375 0 : break;
376 0 : default: mi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
377 : }
378 :
379 0 : mi->uv_mode = read_intra_mode_uv(cm, xd, r, mi->mode);
380 :
381 : // Initialize interp_filter here so we do not have to check for inter block
382 : // modes in get_pred_context_switchable_interp()
383 0 : mi->interp_filter = SWITCHABLE_FILTERS;
384 :
385 0 : mi->ref_frame[0] = INTRA_FRAME;
386 0 : mi->ref_frame[1] = NONE;
387 0 : }
388 :
389 0 : static INLINE int is_mv_valid(const MV *mv) {
390 0 : return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
391 0 : mv->col < MV_UPP;
392 : }
393 :
394 0 : static INLINE void copy_mv_pair(int_mv *dst, const int_mv *src) {
395 0 : memcpy(dst, src, sizeof(*dst) * 2);
396 0 : }
397 :
398 0 : static INLINE void zero_mv_pair(int_mv *dst) {
399 0 : memset(dst, 0, sizeof(*dst) * 2);
400 0 : }
401 :
402 0 : static INLINE int assign_mv(VP9_COMMON *cm, MACROBLOCKD *xd,
403 : PREDICTION_MODE mode, int_mv mv[2],
404 : int_mv ref_mv[2], int_mv near_nearest_mv[2],
405 : int is_compound, int allow_hp, vpx_reader *r) {
406 : int i;
407 0 : int ret = 1;
408 :
409 0 : switch (mode) {
410 : case NEWMV: {
411 0 : FRAME_COUNTS *counts = xd->counts;
412 0 : nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
413 0 : for (i = 0; i < 1 + is_compound; ++i) {
414 0 : read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
415 : allow_hp);
416 0 : ret = ret && is_mv_valid(&mv[i].as_mv);
417 : }
418 0 : break;
419 : }
420 : case NEARMV:
421 : case NEARESTMV: {
422 0 : copy_mv_pair(mv, near_nearest_mv);
423 0 : break;
424 : }
425 : case ZEROMV: {
426 0 : zero_mv_pair(mv);
427 0 : break;
428 : }
429 0 : default: { return 0; }
430 : }
431 0 : return ret;
432 : }
433 :
434 0 : static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
435 : int segment_id, vpx_reader *r) {
436 0 : if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
437 0 : return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
438 : } else {
439 0 : const int ctx = get_intra_inter_context(xd);
440 0 : const int is_inter = vpx_read(r, cm->fc->intra_inter_prob[ctx]);
441 0 : FRAME_COUNTS *counts = xd->counts;
442 0 : if (counts) ++counts->intra_inter[ctx][is_inter];
443 0 : return is_inter;
444 : }
445 : }
446 :
447 0 : static void dec_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *best_mv,
448 : int refmv_count) {
449 : int i;
450 :
451 : // Make sure all the candidates are properly clamped etc
452 0 : for (i = 0; i < refmv_count; ++i) {
453 0 : lower_mv_precision(&mvlist[i].as_mv, allow_hp);
454 0 : *best_mv = mvlist[i];
455 : }
456 0 : }
457 :
458 0 : static void fpm_sync(void *const data, int mi_row) {
459 0 : VP9Decoder *const pbi = (VP9Decoder *)data;
460 0 : vp9_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
461 : mi_row << MI_BLOCK_SIZE_LOG2);
462 0 : }
463 :
464 : // This macro is used to add a motion vector mv_ref list if it isn't
465 : // already in the list. If it's the second motion vector or early_break
466 : // it will also skip all additional processing and jump to Done!
467 : #define ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done) \
468 : do { \
469 : if (refmv_count) { \
470 : if ((mv).as_int != (mv_ref_list)[0].as_int) { \
471 : (mv_ref_list)[(refmv_count)] = (mv); \
472 : refmv_count++; \
473 : goto Done; \
474 : } \
475 : } else { \
476 : (mv_ref_list)[(refmv_count)++] = (mv); \
477 : if (early_break) goto Done; \
478 : } \
479 : } while (0)
480 :
481 : // If either reference frame is different, not INTRA, and they
482 : // are different from each other scale and add the mv to our list.
483 : #define IF_DIFF_REF_FRAME_ADD_MV_EB(mbmi, ref_frame, ref_sign_bias, \
484 : refmv_count, mv_ref_list, Done) \
485 : do { \
486 : if (is_inter_block(mbmi)) { \
487 : if ((mbmi)->ref_frame[0] != ref_frame) \
488 : ADD_MV_REF_LIST_EB(scale_mv((mbmi), 0, ref_frame, ref_sign_bias), \
489 : refmv_count, mv_ref_list, Done); \
490 : if (has_second_ref(mbmi) && (mbmi)->ref_frame[1] != ref_frame && \
491 : (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int) \
492 : ADD_MV_REF_LIST_EB(scale_mv((mbmi), 1, ref_frame, ref_sign_bias), \
493 : refmv_count, mv_ref_list, Done); \
494 : } \
495 : } while (0)
496 :
497 : // This function searches the neighborhood of a given MB/SB
498 : // to try and find candidate reference vectors.
499 0 : static int dec_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
500 : PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame,
501 : const POSITION *const mv_ref_search,
502 : int_mv *mv_ref_list, int mi_row, int mi_col,
503 : int block, int is_sub8x8, find_mv_refs_sync sync,
504 : void *const data) {
505 0 : const int *ref_sign_bias = cm->ref_frame_sign_bias;
506 0 : int i, refmv_count = 0;
507 0 : int different_ref_found = 0;
508 0 : const MV_REF *const prev_frame_mvs =
509 0 : cm->use_prev_frame_mvs
510 0 : ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
511 0 : : NULL;
512 0 : const TileInfo *const tile = &xd->tile;
513 : // If mode is nearestmv or newmv (uses nearestmv as a reference) then stop
514 : // searching after the first mv is found.
515 0 : const int early_break = (mode != NEARMV);
516 :
517 : // Blank the reference vector list
518 0 : memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
519 :
520 0 : i = 0;
521 0 : if (is_sub8x8) {
522 : // If the size < 8x8 we get the mv from the bmi substructure for the
523 : // nearest two blocks.
524 0 : for (i = 0; i < 2; ++i) {
525 0 : const POSITION *const mv_ref = &mv_ref_search[i];
526 0 : if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
527 0 : const MODE_INFO *const candidate_mi =
528 0 : xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
529 0 : different_ref_found = 1;
530 :
531 0 : if (candidate_mi->ref_frame[0] == ref_frame)
532 0 : ADD_MV_REF_LIST_EB(
533 : get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
534 : refmv_count, mv_ref_list, Done);
535 0 : else if (candidate_mi->ref_frame[1] == ref_frame)
536 0 : ADD_MV_REF_LIST_EB(
537 : get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
538 : refmv_count, mv_ref_list, Done);
539 : }
540 : }
541 : }
542 :
543 : // Check the rest of the neighbors in much the same way
544 : // as before except we don't need to keep track of sub blocks or
545 : // mode counts.
546 0 : for (; i < MVREF_NEIGHBOURS; ++i) {
547 0 : const POSITION *const mv_ref = &mv_ref_search[i];
548 0 : if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
549 0 : const MODE_INFO *const candidate =
550 0 : xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
551 0 : different_ref_found = 1;
552 :
553 0 : if (candidate->ref_frame[0] == ref_frame)
554 0 : ADD_MV_REF_LIST_EB(candidate->mv[0], refmv_count, mv_ref_list, Done);
555 0 : else if (candidate->ref_frame[1] == ref_frame)
556 0 : ADD_MV_REF_LIST_EB(candidate->mv[1], refmv_count, mv_ref_list, Done);
557 : }
558 : }
559 :
560 : // TODO(hkuang): Remove this sync after fixing pthread_cond_broadcast
561 : // on windows platform. The sync here is unnecessary if use_prev_frame_mvs
562 : // is 0. But after removing it, there will be hang in the unit test on windows
563 : // due to several threads waiting for a thread's signal.
564 : #if defined(_WIN32) && !HAVE_PTHREAD_H
565 : if (cm->frame_parallel_decode && sync != NULL) {
566 : sync(data, mi_row);
567 : }
568 : #endif
569 :
570 : // Check the last frame's mode and mv info.
571 0 : if (prev_frame_mvs) {
572 : // Synchronize here for frame parallel decode if sync function is provided.
573 0 : if (cm->frame_parallel_decode && sync != NULL) {
574 0 : sync(data, mi_row);
575 : }
576 :
577 0 : if (prev_frame_mvs->ref_frame[0] == ref_frame) {
578 0 : ADD_MV_REF_LIST_EB(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
579 0 : } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
580 0 : ADD_MV_REF_LIST_EB(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
581 : }
582 : }
583 :
584 : // Since we couldn't find 2 mvs from the same reference frame
585 : // go back through the neighbors and find motion vectors from
586 : // different reference frames.
587 0 : if (different_ref_found) {
588 0 : for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
589 0 : const POSITION *mv_ref = &mv_ref_search[i];
590 0 : if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
591 0 : const MODE_INFO *const candidate =
592 0 : xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
593 :
594 : // If the candidate is INTRA we don't want to consider its mv.
595 0 : IF_DIFF_REF_FRAME_ADD_MV_EB(candidate, ref_frame, ref_sign_bias,
596 : refmv_count, mv_ref_list, Done);
597 : }
598 : }
599 : }
600 :
601 : // Since we still don't have a candidate we'll try the last frame.
602 0 : if (prev_frame_mvs) {
603 0 : if (prev_frame_mvs->ref_frame[0] != ref_frame &&
604 0 : prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
605 0 : int_mv mv = prev_frame_mvs->mv[0];
606 0 : if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
607 0 : ref_sign_bias[ref_frame]) {
608 0 : mv.as_mv.row *= -1;
609 0 : mv.as_mv.col *= -1;
610 : }
611 0 : ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done);
612 : }
613 :
614 0 : if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
615 0 : prev_frame_mvs->ref_frame[1] != ref_frame &&
616 0 : prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
617 0 : int_mv mv = prev_frame_mvs->mv[1];
618 0 : if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
619 0 : ref_sign_bias[ref_frame]) {
620 0 : mv.as_mv.row *= -1;
621 0 : mv.as_mv.col *= -1;
622 : }
623 0 : ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done);
624 : }
625 : }
626 :
627 0 : if (mode == NEARMV)
628 0 : refmv_count = MAX_MV_REF_CANDIDATES;
629 : else
630 : // we only care about the nearestmv for the remaining modes
631 0 : refmv_count = 1;
632 :
633 : Done:
634 : // Clamp vectors
635 0 : for (i = 0; i < refmv_count; ++i) clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
636 :
637 0 : return refmv_count;
638 : }
639 :
640 0 : static void append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
641 : const POSITION *const mv_ref_search,
642 : PREDICTION_MODE b_mode, int block,
643 : int ref, int mi_row, int mi_col,
644 : int_mv *best_sub8x8) {
645 : int_mv mv_list[MAX_MV_REF_CANDIDATES];
646 0 : MODE_INFO *const mi = xd->mi[0];
647 0 : b_mode_info *bmi = mi->bmi;
648 : int n;
649 : int refmv_count;
650 :
651 : assert(MAX_MV_REF_CANDIDATES == 2);
652 :
653 0 : refmv_count =
654 0 : dec_find_mv_refs(cm, xd, b_mode, mi->ref_frame[ref], mv_ref_search,
655 : mv_list, mi_row, mi_col, block, 1, NULL, NULL);
656 :
657 0 : switch (block) {
658 0 : case 0: best_sub8x8->as_int = mv_list[refmv_count - 1].as_int; break;
659 : case 1:
660 : case 2:
661 0 : if (b_mode == NEARESTMV) {
662 0 : best_sub8x8->as_int = bmi[0].as_mv[ref].as_int;
663 : } else {
664 0 : best_sub8x8->as_int = 0;
665 0 : for (n = 0; n < refmv_count; ++n)
666 0 : if (bmi[0].as_mv[ref].as_int != mv_list[n].as_int) {
667 0 : best_sub8x8->as_int = mv_list[n].as_int;
668 0 : break;
669 : }
670 : }
671 0 : break;
672 : case 3:
673 0 : if (b_mode == NEARESTMV) {
674 0 : best_sub8x8->as_int = bmi[2].as_mv[ref].as_int;
675 : } else {
676 : int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
677 0 : candidates[0] = bmi[1].as_mv[ref];
678 0 : candidates[1] = bmi[0].as_mv[ref];
679 0 : candidates[2] = mv_list[0];
680 0 : candidates[3] = mv_list[1];
681 0 : best_sub8x8->as_int = 0;
682 0 : for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
683 0 : if (bmi[2].as_mv[ref].as_int != candidates[n].as_int) {
684 0 : best_sub8x8->as_int = candidates[n].as_int;
685 0 : break;
686 : }
687 : }
688 0 : break;
689 0 : default: assert(0 && "Invalid block index.");
690 : }
691 0 : }
692 :
693 0 : static uint8_t get_mode_context(const VP9_COMMON *cm, const MACROBLOCKD *xd,
694 : const POSITION *const mv_ref_search, int mi_row,
695 : int mi_col) {
696 : int i;
697 0 : int context_counter = 0;
698 0 : const TileInfo *const tile = &xd->tile;
699 :
700 : // Get mode count from nearest 2 blocks
701 0 : for (i = 0; i < 2; ++i) {
702 0 : const POSITION *const mv_ref = &mv_ref_search[i];
703 0 : if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
704 0 : const MODE_INFO *const candidate =
705 0 : xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
706 : // Keep counts for entropy encoding.
707 0 : context_counter += mode_2_counter[candidate->mode];
708 : }
709 : }
710 :
711 0 : return counter_to_context[context_counter];
712 : }
713 :
714 0 : static void read_inter_block_mode_info(VP9Decoder *const pbi,
715 : MACROBLOCKD *const xd,
716 : MODE_INFO *const mi, int mi_row,
717 : int mi_col, vpx_reader *r) {
718 0 : VP9_COMMON *const cm = &pbi->common;
719 0 : const BLOCK_SIZE bsize = mi->sb_type;
720 0 : const int allow_hp = cm->allow_high_precision_mv;
721 : int_mv best_ref_mvs[2];
722 : int ref, is_compound;
723 : uint8_t inter_mode_ctx;
724 0 : const POSITION *const mv_ref_search = mv_ref_blocks[bsize];
725 :
726 0 : read_ref_frames(cm, xd, r, mi->segment_id, mi->ref_frame);
727 0 : is_compound = has_second_ref(mi);
728 0 : inter_mode_ctx = get_mode_context(cm, xd, mv_ref_search, mi_row, mi_col);
729 :
730 0 : if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP)) {
731 0 : mi->mode = ZEROMV;
732 0 : if (bsize < BLOCK_8X8) {
733 0 : vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
734 : "Invalid usage of segement feature on small blocks");
735 0 : return;
736 : }
737 : } else {
738 0 : if (bsize >= BLOCK_8X8)
739 0 : mi->mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
740 : else
741 : // Sub 8x8 blocks use the nearestmv as a ref_mv if the b_mode is NEWMV.
742 : // Setting mode to NEARESTMV forces the search to stop after the nearestmv
743 : // has been found. After b_modes have been read, mode will be overwritten
744 : // by the last b_mode.
745 0 : mi->mode = NEARESTMV;
746 :
747 0 : if (mi->mode != ZEROMV) {
748 0 : for (ref = 0; ref < 1 + is_compound; ++ref) {
749 : int_mv tmp_mvs[MAX_MV_REF_CANDIDATES];
750 0 : const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
751 : int refmv_count;
752 :
753 0 : refmv_count =
754 0 : dec_find_mv_refs(cm, xd, mi->mode, frame, mv_ref_search, tmp_mvs,
755 : mi_row, mi_col, -1, 0, fpm_sync, (void *)pbi);
756 :
757 0 : dec_find_best_ref_mvs(allow_hp, tmp_mvs, &best_ref_mvs[ref],
758 : refmv_count);
759 : }
760 : }
761 : }
762 :
763 0 : mi->interp_filter = (cm->interp_filter == SWITCHABLE)
764 : ? read_switchable_interp_filter(cm, xd, r)
765 : : cm->interp_filter;
766 :
767 0 : if (bsize < BLOCK_8X8) {
768 0 : const int num_4x4_w = 1 << xd->bmode_blocks_wl;
769 0 : const int num_4x4_h = 1 << xd->bmode_blocks_hl;
770 : int idx, idy;
771 : PREDICTION_MODE b_mode;
772 : int_mv best_sub8x8[2];
773 0 : const uint32_t invalid_mv = 0x80008000;
774 : // Initialize the 2nd element as even though it won't be used meaningfully
775 : // if is_compound is false, copying/clamping it may trigger a MSan warning.
776 0 : best_sub8x8[1].as_int = invalid_mv;
777 0 : for (idy = 0; idy < 2; idy += num_4x4_h) {
778 0 : for (idx = 0; idx < 2; idx += num_4x4_w) {
779 0 : const int j = idy * 2 + idx;
780 0 : b_mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
781 :
782 0 : if (b_mode == NEARESTMV || b_mode == NEARMV) {
783 0 : for (ref = 0; ref < 1 + is_compound; ++ref)
784 0 : append_sub8x8_mvs_for_idx(cm, xd, mv_ref_search, b_mode, j, ref,
785 : mi_row, mi_col, &best_sub8x8[ref]);
786 : }
787 :
788 0 : if (!assign_mv(cm, xd, b_mode, mi->bmi[j].as_mv, best_ref_mvs,
789 : best_sub8x8, is_compound, allow_hp, r)) {
790 0 : xd->corrupted |= 1;
791 0 : break;
792 : }
793 :
794 0 : if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
795 0 : if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
796 : }
797 : }
798 :
799 0 : mi->mode = b_mode;
800 :
801 0 : copy_mv_pair(mi->mv, mi->bmi[3].as_mv);
802 : } else {
803 0 : xd->corrupted |= !assign_mv(cm, xd, mi->mode, mi->mv, best_ref_mvs,
804 : best_ref_mvs, is_compound, allow_hp, r);
805 : }
806 : }
807 :
808 0 : static void read_inter_frame_mode_info(VP9Decoder *const pbi,
809 : MACROBLOCKD *const xd, int mi_row,
810 : int mi_col, vpx_reader *r, int x_mis,
811 : int y_mis) {
812 0 : VP9_COMMON *const cm = &pbi->common;
813 0 : MODE_INFO *const mi = xd->mi[0];
814 : int inter_block;
815 :
816 0 : mi->segment_id =
817 0 : read_inter_segment_id(cm, xd, mi_row, mi_col, r, x_mis, y_mis);
818 0 : mi->skip = read_skip(cm, xd, mi->segment_id, r);
819 0 : inter_block = read_is_inter_block(cm, xd, mi->segment_id, r);
820 0 : mi->tx_size = read_tx_size(cm, xd, !mi->skip || !inter_block, r);
821 :
822 0 : if (inter_block)
823 0 : read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
824 : else
825 0 : read_intra_block_mode_info(cm, xd, mi, r);
826 0 : }
827 :
828 0 : static INLINE void copy_ref_frame_pair(MV_REFERENCE_FRAME *dst,
829 : const MV_REFERENCE_FRAME *src) {
830 0 : memcpy(dst, src, sizeof(*dst) * 2);
831 0 : }
832 :
833 0 : void vp9_read_mode_info(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
834 : int mi_col, int x_mis, int y_mis) {
835 0 : vpx_reader *r = &twd->bit_reader;
836 0 : MACROBLOCKD *const xd = &twd->xd;
837 0 : VP9_COMMON *const cm = &pbi->common;
838 0 : MODE_INFO *const mi = xd->mi[0];
839 0 : MV_REF *frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
840 : int w, h;
841 :
842 0 : if (frame_is_intra_only(cm)) {
843 0 : read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r, x_mis, y_mis);
844 : } else {
845 0 : read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
846 :
847 0 : for (h = 0; h < y_mis; ++h) {
848 0 : for (w = 0; w < x_mis; ++w) {
849 0 : MV_REF *const mv = frame_mvs + w;
850 0 : copy_ref_frame_pair(mv->ref_frame, mi->ref_frame);
851 0 : copy_mv_pair(mv->mv, mi->mv);
852 : }
853 0 : frame_mvs += cm->mi_cols;
854 : }
855 : }
856 : #if 0 // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
857 : if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
858 : (xd->above_mi == NULL || xd->left_mi == NULL) &&
859 : !is_inter_block(mi) && need_top_left[mi->uv_mode])
860 : assert(0);
861 : #endif // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
862 0 : }
|