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
read_intra_mode(vpx_reader * r,const vpx_prob * p)27 static PREDICTION_MODE read_intra_mode(vpx_reader *r, const vpx_prob *p) {
28 return (PREDICTION_MODE)vpx_read_tree(r, vp9_intra_mode_tree, p);
29 }
30
read_intra_mode_y(VP9_COMMON * cm,MACROBLOCKD * xd,vpx_reader * r,int size_group)31 static PREDICTION_MODE read_intra_mode_y(VP9_COMMON *cm, MACROBLOCKD *xd,
32 vpx_reader *r, int size_group) {
33 const PREDICTION_MODE y_mode =
34 read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
35 FRAME_COUNTS *counts = xd->counts;
36 if (counts)
37 ++counts->y_mode[size_group][y_mode];
38 return y_mode;
39 }
40
read_intra_mode_uv(VP9_COMMON * cm,MACROBLOCKD * xd,vpx_reader * r,PREDICTION_MODE y_mode)41 static PREDICTION_MODE read_intra_mode_uv(VP9_COMMON *cm, MACROBLOCKD *xd,
42 vpx_reader *r,
43 PREDICTION_MODE y_mode) {
44 const PREDICTION_MODE uv_mode = read_intra_mode(r,
45 cm->fc->uv_mode_prob[y_mode]);
46 FRAME_COUNTS *counts = xd->counts;
47 if (counts)
48 ++counts->uv_mode[y_mode][uv_mode];
49 return uv_mode;
50 }
51
read_inter_mode(VP9_COMMON * cm,MACROBLOCKD * xd,vpx_reader * r,int ctx)52 static PREDICTION_MODE read_inter_mode(VP9_COMMON *cm, MACROBLOCKD *xd,
53 vpx_reader *r, int ctx) {
54 const int mode = vpx_read_tree(r, vp9_inter_mode_tree,
55 cm->fc->inter_mode_probs[ctx]);
56 FRAME_COUNTS *counts = xd->counts;
57 if (counts)
58 ++counts->inter_mode[ctx][mode];
59
60 return NEARESTMV + mode;
61 }
62
read_segment_id(vpx_reader * r,const struct segmentation * seg)63 static int read_segment_id(vpx_reader *r, const struct segmentation *seg) {
64 return vpx_read_tree(r, vp9_segment_tree, seg->tree_probs);
65 }
66
read_selected_tx_size(VP9_COMMON * cm,MACROBLOCKD * xd,TX_SIZE max_tx_size,vpx_reader * r)67 static TX_SIZE read_selected_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
68 TX_SIZE max_tx_size, vpx_reader *r) {
69 FRAME_COUNTS *counts = xd->counts;
70 const int ctx = get_tx_size_context(xd);
71 const vpx_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
72 int tx_size = vpx_read(r, tx_probs[0]);
73 if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
74 tx_size += vpx_read(r, tx_probs[1]);
75 if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
76 tx_size += vpx_read(r, tx_probs[2]);
77 }
78
79 if (counts)
80 ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
81 return (TX_SIZE)tx_size;
82 }
83
read_tx_size(VP9_COMMON * cm,MACROBLOCKD * xd,int allow_select,vpx_reader * r)84 static TX_SIZE read_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
85 int allow_select, vpx_reader *r) {
86 TX_MODE tx_mode = cm->tx_mode;
87 BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
88 const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
89 if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
90 return read_selected_tx_size(cm, xd, max_tx_size, r);
91 else
92 return VPXMIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
93 }
94
dec_get_segment_id(const VP9_COMMON * cm,const uint8_t * segment_ids,int mi_offset,int x_mis,int y_mis)95 static int dec_get_segment_id(const VP9_COMMON *cm, const uint8_t *segment_ids,
96 int mi_offset, int x_mis, int y_mis) {
97 int x, y, segment_id = INT_MAX;
98
99 for (y = 0; y < y_mis; y++)
100 for (x = 0; x < x_mis; x++)
101 segment_id =
102 VPXMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
103
104 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
105 return segment_id;
106 }
107
set_segment_id(VP9_COMMON * cm,int mi_offset,int x_mis,int y_mis,int segment_id)108 static void set_segment_id(VP9_COMMON *cm, int mi_offset,
109 int x_mis, int y_mis, int segment_id) {
110 int x, y;
111
112 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
113
114 for (y = 0; y < y_mis; y++)
115 for (x = 0; x < x_mis; x++)
116 cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
117 }
118
copy_segment_id(const VP9_COMMON * cm,const uint8_t * last_segment_ids,uint8_t * current_segment_ids,int mi_offset,int x_mis,int y_mis)119 static void copy_segment_id(const VP9_COMMON *cm,
120 const uint8_t *last_segment_ids,
121 uint8_t *current_segment_ids,
122 int mi_offset, int x_mis, int y_mis) {
123 int x, y;
124
125 for (y = 0; y < y_mis; y++)
126 for (x = 0; x < x_mis; x++)
127 current_segment_ids[mi_offset + y * cm->mi_cols + x] = last_segment_ids ?
128 last_segment_ids[mi_offset + y * cm->mi_cols + x] : 0;
129 }
130
read_intra_segment_id(VP9_COMMON * const cm,int mi_offset,int x_mis,int y_mis,vpx_reader * r)131 static int read_intra_segment_id(VP9_COMMON *const cm, int mi_offset,
132 int x_mis, int y_mis,
133 vpx_reader *r) {
134 struct segmentation *const seg = &cm->seg;
135 int segment_id;
136
137 if (!seg->enabled)
138 return 0; // Default for disabled segmentation
139
140 if (!seg->update_map) {
141 copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
142 mi_offset, x_mis, y_mis);
143 return 0;
144 }
145
146 segment_id = read_segment_id(r, seg);
147 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
148 return segment_id;
149 }
150
read_inter_segment_id(VP9_COMMON * const cm,MACROBLOCKD * const xd,int mi_row,int mi_col,vpx_reader * r)151 static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
152 int mi_row, int mi_col, vpx_reader *r) {
153 struct segmentation *const seg = &cm->seg;
154 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
155 int predicted_segment_id, segment_id;
156 const int mi_offset = mi_row * cm->mi_cols + mi_col;
157 const int bw = xd->plane[0].n4_w >> 1;
158 const int bh = xd->plane[0].n4_h >> 1;
159
160 // TODO(slavarnway): move x_mis, y_mis into xd ?????
161 const int x_mis = VPXMIN(cm->mi_cols - mi_col, bw);
162 const int y_mis = VPXMIN(cm->mi_rows - mi_row, bh);
163
164 if (!seg->enabled)
165 return 0; // Default for disabled segmentation
166
167 predicted_segment_id = cm->last_frame_seg_map ?
168 dec_get_segment_id(cm, cm->last_frame_seg_map, mi_offset, x_mis, y_mis) :
169 0;
170
171 if (!seg->update_map) {
172 copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
173 mi_offset, x_mis, y_mis);
174 return predicted_segment_id;
175 }
176
177 if (seg->temporal_update) {
178 const vpx_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
179 mbmi->seg_id_predicted = vpx_read(r, pred_prob);
180 segment_id = mbmi->seg_id_predicted ? predicted_segment_id
181 : read_segment_id(r, seg);
182 } else {
183 segment_id = read_segment_id(r, seg);
184 }
185 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
186 return segment_id;
187 }
188
read_skip(VP9_COMMON * cm,const MACROBLOCKD * xd,int segment_id,vpx_reader * r)189 static int read_skip(VP9_COMMON *cm, const MACROBLOCKD *xd,
190 int segment_id, vpx_reader *r) {
191 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
192 return 1;
193 } else {
194 const int ctx = vp9_get_skip_context(xd);
195 const int skip = vpx_read(r, cm->fc->skip_probs[ctx]);
196 FRAME_COUNTS *counts = xd->counts;
197 if (counts)
198 ++counts->skip[ctx][skip];
199 return skip;
200 }
201 }
202
read_intra_frame_mode_info(VP9_COMMON * const cm,MACROBLOCKD * const xd,int mi_row,int mi_col,vpx_reader * r)203 static void read_intra_frame_mode_info(VP9_COMMON *const cm,
204 MACROBLOCKD *const xd,
205 int mi_row, int mi_col, vpx_reader *r) {
206 MODE_INFO *const mi = xd->mi[0];
207 MB_MODE_INFO *const mbmi = &mi->mbmi;
208 const MODE_INFO *above_mi = xd->above_mi;
209 const MODE_INFO *left_mi = xd->left_mi;
210 const BLOCK_SIZE bsize = mbmi->sb_type;
211 int i;
212 const int mi_offset = mi_row * cm->mi_cols + mi_col;
213 const int bw = xd->plane[0].n4_w >> 1;
214 const int bh = xd->plane[0].n4_h >> 1;
215
216 // TODO(slavarnway): move x_mis, y_mis into xd ?????
217 const int x_mis = VPXMIN(cm->mi_cols - mi_col, bw);
218 const int y_mis = VPXMIN(cm->mi_rows - mi_row, bh);
219
220 mbmi->segment_id = read_intra_segment_id(cm, mi_offset, x_mis, y_mis, r);
221 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
222 mbmi->tx_size = read_tx_size(cm, xd, 1, r);
223 mbmi->ref_frame[0] = INTRA_FRAME;
224 mbmi->ref_frame[1] = NONE;
225
226 switch (bsize) {
227 case BLOCK_4X4:
228 for (i = 0; i < 4; ++i)
229 mi->bmi[i].as_mode =
230 read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, i));
231 mbmi->mode = mi->bmi[3].as_mode;
232 break;
233 case BLOCK_4X8:
234 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
235 read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
236 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
237 read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 1));
238 break;
239 case BLOCK_8X4:
240 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
241 read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
242 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
243 read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 2));
244 break;
245 default:
246 mbmi->mode = read_intra_mode(r,
247 get_y_mode_probs(mi, above_mi, left_mi, 0));
248 }
249
250 mbmi->uv_mode = read_intra_mode(r, vp9_kf_uv_mode_prob[mbmi->mode]);
251 }
252
read_mv_component(vpx_reader * r,const nmv_component * mvcomp,int usehp)253 static int read_mv_component(vpx_reader *r,
254 const nmv_component *mvcomp, int usehp) {
255 int mag, d, fr, hp;
256 const int sign = vpx_read(r, mvcomp->sign);
257 const int mv_class = vpx_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
258 const int class0 = mv_class == MV_CLASS_0;
259
260 // Integer part
261 if (class0) {
262 d = vpx_read_tree(r, vp9_mv_class0_tree, mvcomp->class0);
263 mag = 0;
264 } else {
265 int i;
266 const int n = mv_class + CLASS0_BITS - 1; // number of bits
267
268 d = 0;
269 for (i = 0; i < n; ++i)
270 d |= vpx_read(r, mvcomp->bits[i]) << i;
271 mag = CLASS0_SIZE << (mv_class + 2);
272 }
273
274 // Fractional part
275 fr = vpx_read_tree(r, vp9_mv_fp_tree, class0 ? mvcomp->class0_fp[d]
276 : mvcomp->fp);
277
278 // High precision part (if hp is not used, the default value of the hp is 1)
279 hp = usehp ? vpx_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp)
280 : 1;
281
282 // Result
283 mag += ((d << 3) | (fr << 1) | hp) + 1;
284 return sign ? -mag : mag;
285 }
286
read_mv(vpx_reader * r,MV * mv,const MV * ref,const nmv_context * ctx,nmv_context_counts * counts,int allow_hp)287 static INLINE void read_mv(vpx_reader *r, MV *mv, const MV *ref,
288 const nmv_context *ctx,
289 nmv_context_counts *counts, int allow_hp) {
290 const MV_JOINT_TYPE joint_type =
291 (MV_JOINT_TYPE)vpx_read_tree(r, vp9_mv_joint_tree, ctx->joints);
292 const int use_hp = allow_hp && vp9_use_mv_hp(ref);
293 MV diff = {0, 0};
294
295 if (mv_joint_vertical(joint_type))
296 diff.row = read_mv_component(r, &ctx->comps[0], use_hp);
297
298 if (mv_joint_horizontal(joint_type))
299 diff.col = read_mv_component(r, &ctx->comps[1], use_hp);
300
301 vp9_inc_mv(&diff, counts);
302
303 mv->row = ref->row + diff.row;
304 mv->col = ref->col + diff.col;
305 }
306
read_block_reference_mode(VP9_COMMON * cm,const MACROBLOCKD * xd,vpx_reader * r)307 static REFERENCE_MODE read_block_reference_mode(VP9_COMMON *cm,
308 const MACROBLOCKD *xd,
309 vpx_reader *r) {
310 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
311 const int ctx = vp9_get_reference_mode_context(cm, xd);
312 const REFERENCE_MODE mode =
313 (REFERENCE_MODE)vpx_read(r, cm->fc->comp_inter_prob[ctx]);
314 FRAME_COUNTS *counts = xd->counts;
315 if (counts)
316 ++counts->comp_inter[ctx][mode];
317 return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
318 } else {
319 return cm->reference_mode;
320 }
321 }
322
323 // Read the referncence frame
read_ref_frames(VP9_COMMON * const cm,MACROBLOCKD * const xd,vpx_reader * r,int segment_id,MV_REFERENCE_FRAME ref_frame[2])324 static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
325 vpx_reader *r,
326 int segment_id, MV_REFERENCE_FRAME ref_frame[2]) {
327 FRAME_CONTEXT *const fc = cm->fc;
328 FRAME_COUNTS *counts = xd->counts;
329
330 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
331 ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
332 SEG_LVL_REF_FRAME);
333 ref_frame[1] = NONE;
334 } else {
335 const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
336 // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
337 if (mode == COMPOUND_REFERENCE) {
338 const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
339 const int ctx = vp9_get_pred_context_comp_ref_p(cm, xd);
340 const int bit = vpx_read(r, fc->comp_ref_prob[ctx]);
341 if (counts)
342 ++counts->comp_ref[ctx][bit];
343 ref_frame[idx] = cm->comp_fixed_ref;
344 ref_frame[!idx] = cm->comp_var_ref[bit];
345 } else if (mode == SINGLE_REFERENCE) {
346 const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
347 const int bit0 = vpx_read(r, fc->single_ref_prob[ctx0][0]);
348 if (counts)
349 ++counts->single_ref[ctx0][0][bit0];
350 if (bit0) {
351 const int ctx1 = vp9_get_pred_context_single_ref_p2(xd);
352 const int bit1 = vpx_read(r, fc->single_ref_prob[ctx1][1]);
353 if (counts)
354 ++counts->single_ref[ctx1][1][bit1];
355 ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
356 } else {
357 ref_frame[0] = LAST_FRAME;
358 }
359
360 ref_frame[1] = NONE;
361 } else {
362 assert(0 && "Invalid prediction mode.");
363 }
364 }
365 }
366
367
read_switchable_interp_filter(VP9_COMMON * const cm,MACROBLOCKD * const xd,vpx_reader * r)368 static INLINE INTERP_FILTER read_switchable_interp_filter(
369 VP9_COMMON *const cm, MACROBLOCKD *const xd,
370 vpx_reader *r) {
371 const int ctx = vp9_get_pred_context_switchable_interp(xd);
372 const INTERP_FILTER type =
373 (INTERP_FILTER)vpx_read_tree(r, vp9_switchable_interp_tree,
374 cm->fc->switchable_interp_prob[ctx]);
375 FRAME_COUNTS *counts = xd->counts;
376 if (counts)
377 ++counts->switchable_interp[ctx][type];
378 return type;
379 }
380
read_intra_block_mode_info(VP9_COMMON * const cm,MACROBLOCKD * const xd,MODE_INFO * mi,vpx_reader * r)381 static void read_intra_block_mode_info(VP9_COMMON *const cm,
382 MACROBLOCKD *const xd, MODE_INFO *mi,
383 vpx_reader *r) {
384 MB_MODE_INFO *const mbmi = &mi->mbmi;
385 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
386 int i;
387
388 mbmi->ref_frame[0] = INTRA_FRAME;
389 mbmi->ref_frame[1] = NONE;
390
391 switch (bsize) {
392 case BLOCK_4X4:
393 for (i = 0; i < 4; ++i)
394 mi->bmi[i].as_mode = read_intra_mode_y(cm, xd, r, 0);
395 mbmi->mode = mi->bmi[3].as_mode;
396 break;
397 case BLOCK_4X8:
398 mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd,
399 r, 0);
400 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
401 read_intra_mode_y(cm, xd, r, 0);
402 break;
403 case BLOCK_8X4:
404 mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd,
405 r, 0);
406 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
407 read_intra_mode_y(cm, xd, r, 0);
408 break;
409 default:
410 mbmi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
411 }
412
413 mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
414 }
415
is_mv_valid(const MV * mv)416 static INLINE int is_mv_valid(const MV *mv) {
417 return mv->row > MV_LOW && mv->row < MV_UPP &&
418 mv->col > MV_LOW && mv->col < MV_UPP;
419 }
420
assign_mv(VP9_COMMON * cm,MACROBLOCKD * xd,PREDICTION_MODE mode,int_mv mv[2],int_mv ref_mv[2],int_mv nearest_mv[2],int_mv near_mv[2],int is_compound,int allow_hp,vpx_reader * r)421 static INLINE int assign_mv(VP9_COMMON *cm, MACROBLOCKD *xd,
422 PREDICTION_MODE mode,
423 int_mv mv[2], int_mv ref_mv[2],
424 int_mv nearest_mv[2], int_mv near_mv[2],
425 int is_compound, int allow_hp, vpx_reader *r) {
426 int i;
427 int ret = 1;
428
429 switch (mode) {
430 case NEWMV: {
431 FRAME_COUNTS *counts = xd->counts;
432 nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
433 for (i = 0; i < 1 + is_compound; ++i) {
434 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
435 allow_hp);
436 ret = ret && is_mv_valid(&mv[i].as_mv);
437 }
438 break;
439 }
440 case NEARESTMV: {
441 mv[0].as_int = nearest_mv[0].as_int;
442 if (is_compound)
443 mv[1].as_int = nearest_mv[1].as_int;
444 break;
445 }
446 case NEARMV: {
447 mv[0].as_int = near_mv[0].as_int;
448 if (is_compound)
449 mv[1].as_int = near_mv[1].as_int;
450 break;
451 }
452 case ZEROMV: {
453 mv[0].as_int = 0;
454 if (is_compound)
455 mv[1].as_int = 0;
456 break;
457 }
458 default: {
459 return 0;
460 }
461 }
462 return ret;
463 }
464
read_is_inter_block(VP9_COMMON * const cm,MACROBLOCKD * const xd,int segment_id,vpx_reader * r)465 static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
466 int segment_id, vpx_reader *r) {
467 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
468 return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
469 } else {
470 const int ctx = vp9_get_intra_inter_context(xd);
471 const int is_inter = vpx_read(r, cm->fc->intra_inter_prob[ctx]);
472 FRAME_COUNTS *counts = xd->counts;
473 if (counts)
474 ++counts->intra_inter[ctx][is_inter];
475 return is_inter;
476 }
477 }
478
fpm_sync(void * const data,int mi_row)479 static void fpm_sync(void *const data, int mi_row) {
480 VP9Decoder *const pbi = (VP9Decoder *)data;
481 vp9_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
482 mi_row << MI_BLOCK_SIZE_LOG2);
483 }
484
read_inter_block_mode_info(VP9Decoder * const pbi,MACROBLOCKD * const xd,MODE_INFO * const mi,int mi_row,int mi_col,vpx_reader * r)485 static void read_inter_block_mode_info(VP9Decoder *const pbi,
486 MACROBLOCKD *const xd,
487 MODE_INFO *const mi,
488 int mi_row, int mi_col, vpx_reader *r) {
489 VP9_COMMON *const cm = &pbi->common;
490 MB_MODE_INFO *const mbmi = &mi->mbmi;
491 const BLOCK_SIZE bsize = mbmi->sb_type;
492 const int allow_hp = cm->allow_high_precision_mv;
493 int_mv nearestmv[2], nearmv[2];
494 int_mv ref_mvs[MAX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
495 int ref, is_compound;
496 uint8_t inter_mode_ctx[MAX_REF_FRAMES];
497
498 read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
499 is_compound = has_second_ref(mbmi);
500
501 for (ref = 0; ref < 1 + is_compound; ++ref) {
502 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
503 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
504
505 xd->block_refs[ref] = ref_buf;
506 if ((!vp9_is_valid_scale(&ref_buf->sf)))
507 vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
508 "Reference frame has invalid dimensions");
509 vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
510 &ref_buf->sf);
511 vp9_find_mv_refs(cm, xd, mi, frame, ref_mvs[frame],
512 mi_row, mi_col, fpm_sync, (void *)pbi, inter_mode_ctx);
513 }
514
515 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
516 mbmi->mode = ZEROMV;
517 if (bsize < BLOCK_8X8) {
518 vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
519 "Invalid usage of segement feature on small blocks");
520 return;
521 }
522 } else {
523 if (bsize >= BLOCK_8X8)
524 mbmi->mode = read_inter_mode(cm, xd, r,
525 inter_mode_ctx[mbmi->ref_frame[0]]);
526 }
527
528 if (bsize < BLOCK_8X8 || mbmi->mode != ZEROMV) {
529 for (ref = 0; ref < 1 + is_compound; ++ref) {
530 vp9_find_best_ref_mvs(xd, allow_hp, ref_mvs[mbmi->ref_frame[ref]],
531 &nearestmv[ref], &nearmv[ref]);
532 }
533 }
534
535 mbmi->interp_filter = (cm->interp_filter == SWITCHABLE)
536 ? read_switchable_interp_filter(cm, xd, r)
537 : cm->interp_filter;
538
539 if (bsize < BLOCK_8X8) {
540 const int num_4x4_w = 1 << xd->bmode_blocks_wl;
541 const int num_4x4_h = 1 << xd->bmode_blocks_hl;
542 int idx, idy;
543 PREDICTION_MODE b_mode;
544 int_mv nearest_sub8x8[2], near_sub8x8[2];
545 for (idy = 0; idy < 2; idy += num_4x4_h) {
546 for (idx = 0; idx < 2; idx += num_4x4_w) {
547 int_mv block[2];
548 const int j = idy * 2 + idx;
549 b_mode = read_inter_mode(cm, xd, r, inter_mode_ctx[mbmi->ref_frame[0]]);
550
551 if (b_mode == NEARESTMV || b_mode == NEARMV) {
552 uint8_t dummy_mode_ctx[MAX_REF_FRAMES];
553 for (ref = 0; ref < 1 + is_compound; ++ref)
554 vp9_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
555 &nearest_sub8x8[ref],
556 &near_sub8x8[ref],
557 dummy_mode_ctx);
558 }
559
560 if (!assign_mv(cm, xd, b_mode, block, nearestmv,
561 nearest_sub8x8, near_sub8x8,
562 is_compound, allow_hp, r)) {
563 xd->corrupted |= 1;
564 break;
565 }
566
567 mi->bmi[j].as_mv[0].as_int = block[0].as_int;
568 if (is_compound)
569 mi->bmi[j].as_mv[1].as_int = block[1].as_int;
570
571 if (num_4x4_h == 2)
572 mi->bmi[j + 2] = mi->bmi[j];
573 if (num_4x4_w == 2)
574 mi->bmi[j + 1] = mi->bmi[j];
575 }
576 }
577
578 mi->mbmi.mode = b_mode;
579
580 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
581 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
582 } else {
583 xd->corrupted |= !assign_mv(cm, xd, mbmi->mode, mbmi->mv, nearestmv,
584 nearestmv, nearmv, is_compound, allow_hp, r);
585 }
586 }
587
read_inter_frame_mode_info(VP9Decoder * const pbi,MACROBLOCKD * const xd,int mi_row,int mi_col,vpx_reader * r)588 static void read_inter_frame_mode_info(VP9Decoder *const pbi,
589 MACROBLOCKD *const xd,
590 int mi_row, int mi_col, vpx_reader *r) {
591 VP9_COMMON *const cm = &pbi->common;
592 MODE_INFO *const mi = xd->mi[0];
593 MB_MODE_INFO *const mbmi = &mi->mbmi;
594 int inter_block;
595
596 mbmi->mv[0].as_int = 0;
597 mbmi->mv[1].as_int = 0;
598 mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
599 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
600 inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
601 mbmi->tx_size = read_tx_size(cm, xd, !mbmi->skip || !inter_block, r);
602
603 if (inter_block)
604 read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
605 else
606 read_intra_block_mode_info(cm, xd, mi, r);
607 }
608
vpx_read_mode_info(VP9Decoder * const pbi,MACROBLOCKD * xd,int mi_row,int mi_col,vpx_reader * r,int x_mis,int y_mis)609 void vpx_read_mode_info(VP9Decoder *const pbi, MACROBLOCKD *xd,
610 int mi_row, int mi_col, vpx_reader *r,
611 int x_mis, int y_mis) {
612 VP9_COMMON *const cm = &pbi->common;
613 MODE_INFO *const mi = xd->mi[0];
614 MV_REF* frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
615 int w, h;
616
617 if (frame_is_intra_only(cm)) {
618 read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
619 } else {
620 read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r);
621
622 for (h = 0; h < y_mis; ++h) {
623 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
624 for (w = 0; w < x_mis; ++w) {
625 MV_REF *const mv = frame_mv + w;
626 mv->ref_frame[0] = mi->mbmi.ref_frame[0];
627 mv->ref_frame[1] = mi->mbmi.ref_frame[1];
628 mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
629 mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
630 }
631 }
632 }
633 }
634