2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
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.
12 #include "treereader.h"
13 #include "entropymv.h"
14 #include "entropymode.h"
15 #include "onyxd_int.h"
16 #include "findnearmv.h"
22 static int read_mvcomponent(vp8_reader *r, const MV_CONTEXT *mvc)
24 const vp8_prob *const p = (const vp8_prob *) mvc;
27 if (vp8_read(r, p [mvpis_short])) /* Large */
33 x += vp8_read(r, p [MVPbits + i]) << i;
37 i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
41 x += vp8_read(r, p [MVPbits + i]) << i;
45 if (!(x & 0xFFF0) || vp8_read(r, p [MVPbits + 3]))
49 x = vp8_treed_read(r, vp8_small_mvtree, p + MVPshort);
51 if (x && vp8_read(r, p [MVPsign]))
57 static void read_mv(vp8_reader *r, MV *mv, const MV_CONTEXT *mvc)
59 mv->row = (short)(read_mvcomponent(r, mvc) << 1);
60 mv->col = (short)(read_mvcomponent(r, ++mvc) << 1);
64 static void read_mvcontexts(vp8_reader *bc, MV_CONTEXT *mvc)
70 const vp8_prob *up = vp8_mv_update_probs[i].prob;
71 vp8_prob *p = (vp8_prob *)(mvc + i);
72 vp8_prob *const pstop = p + MVPcount;
76 if (vp8_read(bc, *up++))
78 const vp8_prob x = (vp8_prob)vp8_read_literal(bc, 7);
89 static MB_PREDICTION_MODE read_mv_ref(vp8_reader *bc, const vp8_prob *p)
91 const int i = vp8_treed_read(bc, vp8_mv_ref_tree, p);
93 return (MB_PREDICTION_MODE)i;
96 static MB_PREDICTION_MODE sub_mv_ref(vp8_reader *bc, const vp8_prob *p)
98 const int i = vp8_treed_read(bc, vp8_sub_mv_ref_tree, p);
100 return (MB_PREDICTION_MODE)i;
102 unsigned int vp8_mv_cont_count[5][4] =
111 void vp8_decode_mode_mvs(VP8D_COMP *pbi)
113 const MV Zero = { 0, 0};
115 VP8_COMMON *const pc = & pbi->common;
116 vp8_reader *const bc = & pbi->bc;
118 MODE_INFO *mi = pc->mi, *ms;
119 const int mis = pc->mode_info_stride;
121 MV_CONTEXT *const mvc = pc->fc.mvc;
128 vp8_prob prob_skip_false = 0;
130 if (pc->mb_no_coeff_skip)
131 prob_skip_false = (vp8_prob)vp8_read_literal(bc, 8);
133 prob_intra = (vp8_prob)vp8_read_literal(bc, 8);
134 prob_last = (vp8_prob)vp8_read_literal(bc, 8);
135 prob_gf = (vp8_prob)vp8_read_literal(bc, 8);
139 if (vp8_read_bit(bc))
145 pc->fc.ymode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8);
150 if (vp8_read_bit(bc))
156 pc->fc.uv_mode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8);
161 read_mvcontexts(bc, mvc);
163 while (++mb_row < pc->mb_rows)
167 while (++mb_col < pc->mb_cols)
169 MB_MODE_INFO *const mbmi = & mi->mbmi;
170 MV *const mv = & mbmi->mv.as_mv;
171 VP8_COMMON *const pc = &pbi->common;
172 MACROBLOCKD *xd = &pbi->mb;
174 mbmi->need_to_clamp_mvs = 0;
175 vp8dx_bool_decoder_fill(bc);
177 // Distance of Mb to the various image edges.
178 // These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
179 xd->mb_to_left_edge = -((mb_col * 16) << 3);
180 xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
181 xd->mb_to_top_edge = -((mb_row * 16)) << 3;
182 xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
184 // If required read in new segmentation data for this MB
185 if (pbi->mb.update_mb_segmentation_map)
186 vp8_read_mb_features(bc, mbmi, &pbi->mb);
188 // Read the macroblock coeff skip flag if this feature is in use, else default to 0
189 if (pc->mb_no_coeff_skip)
190 mbmi->mb_skip_coeff = vp8_read(bc, prob_skip_false);
192 mbmi->mb_skip_coeff = 0;
194 mbmi->uv_mode = DC_PRED;
196 if ((mbmi->ref_frame = (MV_REFERENCE_FRAME) vp8_read(bc, prob_intra))) /* inter MB */
199 vp8_prob mv_ref_p [VP8_MVREFS-1];
200 MV nearest, nearby, best_mv;
202 if (vp8_read(bc, prob_last))
204 mbmi->ref_frame = (MV_REFERENCE_FRAME)((int)mbmi->ref_frame + (int)(1 + vp8_read(bc, prob_gf)));
207 vp8_find_near_mvs(xd, mi, &nearest, &nearby, &best_mv, rct, mbmi->ref_frame, pbi->common.ref_frame_sign_bias);
209 vp8_mv_ref_probs(mv_ref_p, rct);
211 switch (mbmi->mode = read_mv_ref(bc, mv_ref_p))
215 const int s = mbmi->partitioning = vp8_treed_read(
216 bc, vp8_mbsplit_tree, vp8_mbsplit_probs
218 const int num_p = vp8_mbsplit_count [s];
219 const int *const L = vp8_mbsplits [s];
222 do /* for each subset j */
224 B_MODE_INFO *const bmi = mbmi->partition_bmi + j;
225 MV *const mv = & bmi->mv.as_mv;
227 int k = -1; /* first block in subset j */
239 mv_contz = vp8_mv_cont(&(vp8_left_bmi(mi, k)->mv.as_mv), &(vp8_above_bmi(mi, k, mis)->mv.as_mv));
241 switch (bmi->mode = (B_PREDICTION_MODE) sub_mv_ref(bc, vp8_sub_mv_ref_prob2 [mv_contz])) //pc->fc.sub_mv_ref_prob))
244 read_mv(bc, mv, (const MV_CONTEXT *) mvc);
245 mv->row += best_mv.row;
246 mv->col += best_mv.col;
247 #ifdef VPX_MODE_COUNT
248 vp8_mv_cont_count[mv_contz][3]++;
252 *mv = vp8_left_bmi(mi, k)->mv.as_mv;
253 #ifdef VPX_MODE_COUNT
254 vp8_mv_cont_count[mv_contz][0]++;
258 *mv = vp8_above_bmi(mi, k, mis)->mv.as_mv;
259 #ifdef VPX_MODE_COUNT
260 vp8_mv_cont_count[mv_contz][1]++;
265 #ifdef VPX_MODE_COUNT
266 vp8_mv_cont_count[mv_contz][2]++;
273 if (mv->col < xd->mb_to_left_edge
275 || mv->col > xd->mb_to_right_edge
276 + RIGHT_BOTTOM_MARGIN
277 || mv->row < xd->mb_to_top_edge
279 || mv->row > xd->mb_to_bottom_edge
280 + RIGHT_BOTTOM_MARGIN
282 mbmi->need_to_clamp_mvs = 1;
284 /* Fill (uniform) modes, mvs of jth subset.
285 Must do it here because ensuing subsets can
286 refer back to us via "left" or "above". */
296 *mv = mi->bmi[15].mv.as_mv;
298 break; /* done with SPLITMV */
303 // Clip "next_nearest" so that it does not extend to far out of image
304 if (mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
305 mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
306 else if (mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
307 mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
309 if (mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
310 mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
311 else if (mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
312 mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
319 // Clip "next_nearest" so that it does not extend to far out of image
320 if (mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
321 mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
322 else if (mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
323 mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
325 if (mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
326 mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
327 else if (mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
328 mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
337 read_mv(bc, mv, (const MV_CONTEXT *) mvc);
338 mv->row += best_mv.row;
339 mv->col += best_mv.col;
341 /* Don't need to check this on NEARMV and NEARESTMV modes
342 * since those modes clamp the MV. The NEWMV mode does not,
343 * so signal to the prediction stage whether special
344 * handling may be required.
346 if (mv->col < xd->mb_to_left_edge - LEFT_TOP_MARGIN
347 || mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN
348 || mv->row < xd->mb_to_top_edge - LEFT_TOP_MARGIN
349 || mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN
351 mbmi->need_to_clamp_mvs = 1;
353 propagate_mv: /* same MV throughout */
358 // mi->bmi[i].mv.as_mv = *mv;
362 mi->bmi[0].mv.as_mv = *mv;
363 mi->bmi[1].mv.as_mv = *mv;
364 mi->bmi[2].mv.as_mv = *mv;
365 mi->bmi[3].mv.as_mv = *mv;
366 mi->bmi[4].mv.as_mv = *mv;
367 mi->bmi[5].mv.as_mv = *mv;
368 mi->bmi[6].mv.as_mv = *mv;
369 mi->bmi[7].mv.as_mv = *mv;
370 mi->bmi[8].mv.as_mv = *mv;
371 mi->bmi[9].mv.as_mv = *mv;
372 mi->bmi[10].mv.as_mv = *mv;
373 mi->bmi[11].mv.as_mv = *mv;
374 mi->bmi[12].mv.as_mv = *mv;
375 mi->bmi[13].mv.as_mv = *mv;
376 mi->bmi[14].mv.as_mv = *mv;
377 mi->bmi[15].mv.as_mv = *mv;
390 /* MB is intra coded */
396 mi->bmi[j].mv.as_mv = Zero;
402 if ((mbmi->mode = (MB_PREDICTION_MODE) vp8_read_ymode(bc, pc->fc.ymode_prob)) == B_PRED)
408 mi->bmi[j].mode = (B_PREDICTION_MODE)vp8_read_bmode(bc, pc->fc.bmode_prob);
413 mbmi->uv_mode = (MB_PREDICTION_MODE)vp8_read_uv_mode(bc, pc->fc.uv_mode_prob);
416 mi++; // next macroblock
419 mi++; // skip left predictor each row