decodemv.c 45.5 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10 11 12
 */


#include "treereader.h"
John Koleszar's avatar
John Koleszar committed
13 14
#include "vp8/common/entropymv.h"
#include "vp8/common/entropymode.h"
John Koleszar's avatar
John Koleszar committed
15
#include "onyxd_int.h"
John Koleszar's avatar
John Koleszar committed
16
#include "vp8/common/findnearmv.h"
Scott LaVarnway's avatar
Scott LaVarnway committed
17

18
#include "vp8/common/seg_common.h"
19
#include "vp8/common/pred_common.h"
20
#include "vp8/common/entropy.h"
21

John Koleszar's avatar
John Koleszar committed
22 23 24
#if CONFIG_DEBUG
#include <assert.h>
#endif
25

John Koleszar's avatar
John Koleszar committed
26
// #define DEBUG_DEC_MV
27 28 29 30
#ifdef DEBUG_DEC_MV
int dec_mvcount = 0;
#endif

John Koleszar's avatar
John Koleszar committed
31
static int vp8_read_bmode(vp8_reader *bc, const vp8_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
32
  return vp8_treed_read(bc, vp8_bmode_tree, p);
Scott LaVarnway's avatar
Scott LaVarnway committed
33 34 35
}


John Koleszar's avatar
John Koleszar committed
36
static int vp8_read_ymode(vp8_reader *bc, const vp8_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
37 38
  return vp8_treed_read(bc, vp8_ymode_tree, p);
}
Scott LaVarnway's avatar
Scott LaVarnway committed
39

Ronald S. Bultje's avatar
Ronald S. Bultje committed
40 41 42
#if CONFIG_SUPERBLOCKS
static int vp8_sb_kfread_ymode(vp8_reader *bc, const vp8_prob *p) {
  return vp8_treed_read(bc, vp8_uv_mode_tree, p);
Scott LaVarnway's avatar
Scott LaVarnway committed
43
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
44
#endif
Scott LaVarnway's avatar
Scott LaVarnway committed
45

John Koleszar's avatar
John Koleszar committed
46
static int vp8_kfread_ymode(vp8_reader *bc, const vp8_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
47
  return vp8_treed_read(bc, vp8_kf_ymode_tree, p);
Scott LaVarnway's avatar
Scott LaVarnway committed
48 49
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
50 51
static int vp8_read_i8x8_mode(vp8_reader *bc, const vp8_prob *p) {
  return vp8_treed_read(bc, vp8_i8x8_mode_tree, p);
Yaowu Xu's avatar
Yaowu Xu committed
52
}
Scott LaVarnway's avatar
Scott LaVarnway committed
53 54


John Koleszar's avatar
John Koleszar committed
55
static int vp8_read_uv_mode(vp8_reader *bc, const vp8_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
56
  return vp8_treed_read(bc, vp8_uv_mode_tree, p);
Scott LaVarnway's avatar
Scott LaVarnway committed
57 58
}

59
// This function reads the current macro block's segnent id from the bitstream
Paul Wilkins's avatar
Paul Wilkins committed
60
// It should only be called if a segment map update is indicated.
Paul Wilkins's avatar
Paul Wilkins committed
61 62
static void vp8_read_mb_segid(vp8_reader *r, MB_MODE_INFO *mi,
                              MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
63
  /* Is segmentation enabled */
Paul Wilkins's avatar
Paul Wilkins committed
64
  if (xd->segmentation_enabled && xd->update_mb_segmentation_map) {
John Koleszar's avatar
John Koleszar committed
65
    /* If so then read the segment id. */
Paul Wilkins's avatar
Paul Wilkins committed
66 67 68
    if (vp8_read(r, xd->mb_segment_tree_probs[0]))
      mi->segment_id =
        (unsigned char)(2 + vp8_read(r, xd->mb_segment_tree_probs[2]));
John Koleszar's avatar
John Koleszar committed
69
    else
Paul Wilkins's avatar
Paul Wilkins committed
70 71
      mi->segment_id =
        (unsigned char)(vp8_read(r, xd->mb_segment_tree_probs[1]));
John Koleszar's avatar
John Koleszar committed
72
  }
Scott LaVarnway's avatar
Scott LaVarnway committed
73
}
74

Yaowu Xu's avatar
Yaowu Xu committed
75
extern const int vp8_i8x8_block[4];
Paul Wilkins's avatar
Paul Wilkins committed
76 77 78
static void vp8_kfread_modes(VP8D_COMP *pbi,
                             MODE_INFO *m,
                             int mb_row,
John Koleszar's avatar
John Koleszar committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                             int mb_col) {
  VP8_COMMON *const cm = & pbi->common;
  vp8_reader *const bc = & pbi->bc;
  const int mis = pbi->common.mode_info_stride;
  int map_index = mb_row * pbi->common.mb_cols + mb_col;
  MB_PREDICTION_MODE y_mode;

  // Read the Macroblock segmentation map if it is being updated explicitly
  // this frame (reset to 0 by default).
  m->mbmi.segment_id = 0;
  if (pbi->mb.update_mb_segmentation_map) {
    vp8_read_mb_segid(bc, &m->mbmi, &pbi->mb);
    pbi->common.last_frame_seg_map[map_index] = m->mbmi.segment_id;
  }

  m->mbmi.mb_skip_coeff = 0;
  if (pbi->common.mb_no_coeff_skip &&
      (!segfeature_active(&pbi->mb,
                          m->mbmi.segment_id, SEG_LVL_EOB) ||
       (get_segdata(&pbi->mb,
                    m->mbmi.segment_id, SEG_LVL_EOB) != 0))) {
    MACROBLOCKD *const xd  = & pbi->mb;
    m->mbmi.mb_skip_coeff = vp8_read(bc, get_pred_prob(cm, xd, PRED_MBSKIP));
  } else {
    if (segfeature_active(&pbi->mb,
                          m->mbmi.segment_id, SEG_LVL_EOB) &&
        (get_segdata(&pbi->mb,
                     m->mbmi.segment_id, SEG_LVL_EOB) == 0)) {
      m->mbmi.mb_skip_coeff = 1;
    } else
      m->mbmi.mb_skip_coeff = 0;
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
112 113 114 115 116 117
#if CONFIG_SUPERBLOCKS
  if (m->mbmi.encoded_as_sb) {
    y_mode = (MB_PREDICTION_MODE) vp8_sb_kfread_ymode(bc,
      pbi->common.sb_kf_ymode_prob[pbi->common.kf_ymode_probs_index]);
  } else
#endif
John Koleszar's avatar
John Koleszar committed
118
  y_mode = (MB_PREDICTION_MODE) vp8_kfread_ymode(bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
119
    pbi->common.kf_ymode_prob[pbi->common.kf_ymode_probs_index]);
120
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
121
  m->mbmi.second_mode = (MB_PREDICTION_MODE)(DC_PRED - 1);
122
#endif
123

John Koleszar's avatar
John Koleszar committed
124
  m->mbmi.ref_frame = INTRA_FRAME;
Paul Wilkins's avatar
Paul Wilkins committed
125

John Koleszar's avatar
John Koleszar committed
126 127
  if ((m->mbmi.mode = y_mode) == B_PRED) {
    int i = 0;
128
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
129
    int use_comp_pred = vp8_read(bc, 128);
130
#endif
John Koleszar's avatar
John Koleszar committed
131 132 133
    do {
      const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
      const B_PREDICTION_MODE L = left_block_mode(m, i);
Paul Wilkins's avatar
Paul Wilkins committed
134

John Koleszar's avatar
John Koleszar committed
135 136 137
      m->bmi[i].as_mode.first =
        (B_PREDICTION_MODE) vp8_read_bmode(
          bc, pbi->common.kf_bmode_prob [A] [L]);
138
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
139 140 141 142 143 144 145
      if (use_comp_pred) {
        m->bmi[i].as_mode.second =
          (B_PREDICTION_MODE) vp8_read_bmode(
            bc, pbi->common.kf_bmode_prob [A] [L]);
      } else {
        m->bmi[i].as_mode.second = (B_PREDICTION_MODE)(B_DC_PRED - 1);
      }
146
#endif
John Koleszar's avatar
John Koleszar committed
147 148 149 150 151 152 153 154 155 156 157 158
    } while (++i < 16);
  }
  if ((m->mbmi.mode = y_mode) == I8X8_PRED) {
    int i;
    int mode8x8;
    for (i = 0; i < 4; i++) {
      int ib = vp8_i8x8_block[i];
      mode8x8 = vp8_read_i8x8_mode(bc, pbi->common.fc.i8x8_mode_prob);
      m->bmi[ib + 0].as_mode.first = mode8x8;
      m->bmi[ib + 1].as_mode.first = mode8x8;
      m->bmi[ib + 4].as_mode.first = mode8x8;
      m->bmi[ib + 5].as_mode.first = mode8x8;
159
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
160 161 162 163
      m->bmi[ib + 0].as_mode.second = (MB_PREDICTION_MODE)(DC_PRED - 1);
      m->bmi[ib + 1].as_mode.second = (MB_PREDICTION_MODE)(DC_PRED - 1);
      m->bmi[ib + 4].as_mode.second = (MB_PREDICTION_MODE)(DC_PRED - 1);
      m->bmi[ib + 5].as_mode.second = (MB_PREDICTION_MODE)(DC_PRED - 1);
164
#endif
John Koleszar's avatar
John Koleszar committed
165 166 167 168
    }
  } else
    m->mbmi.uv_mode = (MB_PREDICTION_MODE)vp8_read_uv_mode(bc,
                                                           pbi->common.kf_uv_mode_prob[m->mbmi.mode]);
169
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
170
  m->mbmi.second_uv_mode = (MB_PREDICTION_MODE)(DC_PRED - 1);
171
#endif
172

Scott LaVarnway's avatar
Scott LaVarnway committed
173
}
John Koleszar's avatar
John Koleszar committed
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
#if CONFIG_NEWMVENTROPY
static int read_nmv_component(vp8_reader *r,
                              int rv,
                              const nmv_component *mvcomp) {
  int v, s, z, c, o, d;
  s = vp8_read(r, mvcomp->sign);
  c = vp8_treed_read(r, vp8_mv_class_tree, mvcomp->classes);
  if (c == MV_CLASS_0) {
    d = vp8_treed_read(r, vp8_mv_class0_tree, mvcomp->class0);
  } else {
    int i, b;
    d = 0;
    b = c + CLASS0_BITS - 1;  /* number of bits */
    for (i = 0; i < b; ++i)
      d |= (vp8_read(r, mvcomp->bits[i]) << i);
  }
  o = d << 3;

  z = vp8_get_mv_mag(c, o);
  v = (s ? -(z + 1) : (z + 1));
  return v;
}

static int read_nmv_component_fp(vp8_reader *r,
                                 int v,
                                 int rv,
                                 const nmv_component *mvcomp,
                                 int usehp) {
  int s, z, c, o, d, e, f;
  s = v < 0;
  z = (s ? -v : v) - 1;       /* magnitude - 1 */

  c = vp8_get_mv_class(z, &o);
  d = o >> 3;

  if (c == MV_CLASS_0) {
    f = vp8_treed_read(r, vp8_mv_fp_tree, mvcomp->class0_fp[d]);
  } else {
    f = vp8_treed_read(r, vp8_mv_fp_tree, mvcomp->fp);
  }
  o += (f << 1);

  if (usehp) {
    if (c == MV_CLASS_0) {
      e = vp8_read(r, mvcomp->class0_hp);
    } else {
      e = vp8_read(r, mvcomp->hp);
    }
    o += e;
  } else {
    ++o;  /* Note if hp is not used, the default value of the hp bit is 1 */
  }
  z = vp8_get_mv_mag(c, o);
  v = (s ? -(z + 1) : (z + 1));
  return v;
}

static void read_nmv(vp8_reader *r, MV *mv, const MV *ref,
                     const nmv_context *mvctx) {
  MV_JOINT_TYPE j = vp8_treed_read(r, vp8_mv_joint_tree, mvctx->joints);
  mv->row = mv-> col = 0;
  if (j == MV_JOINT_HZVNZ || j == MV_JOINT_HNZVNZ) {
    mv->row = read_nmv_component(r, ref->row, &mvctx->comps[0]);
  }
  if (j == MV_JOINT_HNZVZ || j == MV_JOINT_HNZVNZ) {
    mv->col = read_nmv_component(r, ref->col, &mvctx->comps[1]);
  }
}

static void read_nmv_fp(vp8_reader *r, MV *mv, const MV *ref,
                        const nmv_context *mvctx, int usehp) {
  MV_JOINT_TYPE j = vp8_get_mv_joint(*mv);
247
  usehp = usehp && vp8_use_nmv_hp(ref);
248 249 250 251 252 253 254 255
  if (j == MV_JOINT_HZVNZ || j == MV_JOINT_HNZVNZ) {
    mv->row = read_nmv_component_fp(r, mv->row, ref->row, &mvctx->comps[0],
                                    usehp);
  }
  if (j == MV_JOINT_HNZVZ || j == MV_JOINT_HNZVNZ) {
    mv->col = read_nmv_component_fp(r, mv->col, ref->col, &mvctx->comps[1],
                                    usehp);
  }
256
  //printf("  %d: %d %d ref: %d %d\n", usehp, mv->row, mv-> col, ref->row, ref->col);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
}

static void update_nmv(vp8_reader *bc, vp8_prob *const p,
                       const vp8_prob upd_p) {
  if (vp8_read(bc, upd_p)) {
#ifdef LOW_PRECISION_MV_UPDATE
    *p = (vp8_read_literal(bc, 7) << 1) | 1;
#else
    *p = (vp8_read_literal(bc, 8));
#endif
  }
}

static void read_nmvprobs(vp8_reader *bc, nmv_context *mvctx,
                          int usehp) {
  int i, j, k;
#ifdef MV_GROUP_UPDATE
  if (!vp8_read_bit(bc)) return;
#endif
  for (j = 0; j < MV_JOINTS - 1; ++j) {
    update_nmv(bc, &mvctx->joints[j],
               VP8_NMV_UPDATE_PROB);
  }
  for (i = 0; i < 2; ++i) {
    update_nmv(bc, &mvctx->comps[i].sign,
               VP8_NMV_UPDATE_PROB);
    for (j = 0; j < MV_CLASSES - 1; ++j) {
      update_nmv(bc, &mvctx->comps[i].classes[j],
                 VP8_NMV_UPDATE_PROB);
    }
    for (j = 0; j < CLASS0_SIZE - 1; ++j) {
      update_nmv(bc, &mvctx->comps[i].class0[j],
                 VP8_NMV_UPDATE_PROB);
    }
    for (j = 0; j < MV_OFFSET_BITS; ++j) {
      update_nmv(bc, &mvctx->comps[i].bits[j],
                 VP8_NMV_UPDATE_PROB);
    }
  }

  for (i = 0; i < 2; ++i) {
    for (j = 0; j < CLASS0_SIZE; ++j) {
      for (k = 0; k < 3; ++k)
        update_nmv(bc, &mvctx->comps[i].class0_fp[j][k],
                   VP8_NMV_UPDATE_PROB);
    }
    for (j = 0; j < 3; ++j) {
      update_nmv(bc, &mvctx->comps[i].fp[j],
                 VP8_NMV_UPDATE_PROB);
    }
  }

  if (usehp) {
    for (i = 0; i < 2; ++i) {
      update_nmv(bc, &mvctx->comps[i].class0_hp,
                 VP8_NMV_UPDATE_PROB);
      update_nmv(bc, &mvctx->comps[i].hp,
                 VP8_NMV_UPDATE_PROB);
    }
  }
}

#else

John Koleszar's avatar
John Koleszar committed
321 322 323
static int read_mvcomponent(vp8_reader *r, const MV_CONTEXT *mvc) {
  const vp8_prob *const p = (const vp8_prob *) mvc;
  int x = 0;
John Koleszar's avatar
John Koleszar committed
324

John Koleszar's avatar
John Koleszar committed
325 326
  if (vp8_read(r, p [mvpis_short])) { /* Large */
    int i = 0;
John Koleszar's avatar
John Koleszar committed
327

John Koleszar's avatar
John Koleszar committed
328 329 330
    do {
      x += vp8_read(r, p [MVPbits + i]) << i;
    } while (++i < mvnum_short_bits);
John Koleszar's avatar
John Koleszar committed
331

John Koleszar's avatar
John Koleszar committed
332
    i = mvlong_width - 1;  /* Skip bit 3, which is sometimes implicit */
John Koleszar's avatar
John Koleszar committed
333

John Koleszar's avatar
John Koleszar committed
334 335 336
    do {
      x += vp8_read(r, p [MVPbits + i]) << i;
    } while (--i > mvnum_short_bits);
John Koleszar's avatar
John Koleszar committed
337

John Koleszar's avatar
John Koleszar committed
338 339 340 341
    if (!(x & ~((2 << mvnum_short_bits) - 1))  ||  vp8_read(r, p [MVPbits + mvnum_short_bits]))
      x += (mvnum_short);
  } else /* small */
    x = vp8_treed_read(r, vp8_small_mvtree, p + MVPshort);
John Koleszar's avatar
John Koleszar committed
342

John Koleszar's avatar
John Koleszar committed
343 344
  if (x  &&  vp8_read(r, p [MVPsign]))
    x = -x;
John Koleszar's avatar
John Koleszar committed
345

John Koleszar's avatar
John Koleszar committed
346
  return x;
John Koleszar's avatar
John Koleszar committed
347 348
}

John Koleszar's avatar
John Koleszar committed
349 350 351
static void read_mv(vp8_reader *r, MV *mv, const MV_CONTEXT *mvc) {
  mv->row = (short)(read_mvcomponent(r,   mvc) << 1);
  mv->col = (short)(read_mvcomponent(r, ++mvc) << 1);
352
#ifdef DEBUG_DEC_MV
John Koleszar's avatar
John Koleszar committed
353 354 355 356
  int i;
  printf("%d (np): %d %d\n", dec_mvcount++, mv->row, mv->col);
  // for (i=0; i<MVPcount;++i) printf("  %d", (&mvc[-1])->prob[i]); printf("\n");
  // for (i=0; i<MVPcount;++i) printf("  %d", (&mvc[0])->prob[i]); printf("\n");
357
#endif
John Koleszar's avatar
John Koleszar committed
358 359
}

John Koleszar's avatar
John Koleszar committed
360 361
static void read_mvcontexts(vp8_reader *bc, MV_CONTEXT *mvc) {
  int i = 0;
John Koleszar's avatar
John Koleszar committed
362

John Koleszar's avatar
John Koleszar committed
363 364 365 366
  do {
    const vp8_prob *up = vp8_mv_update_probs[i].prob;
    vp8_prob *p = (vp8_prob *)(mvc + i);
    vp8_prob *const pstop = p + MVPcount;
John Koleszar's avatar
John Koleszar committed
367

John Koleszar's avatar
John Koleszar committed
368 369 370
    do {
      if (vp8_read(bc, *up++)) {
        const vp8_prob x = (vp8_prob)vp8_read_literal(bc, 7);
John Koleszar's avatar
John Koleszar committed
371

John Koleszar's avatar
John Koleszar committed
372 373 374 375
        *p = x ? x << 1 : 1;
      }
    } while (++p < pstop);
  } while (++i < 2);
John Koleszar's avatar
John Koleszar committed
376 377
}

John Koleszar's avatar
John Koleszar committed
378 379 380
static int read_mvcomponent_hp(vp8_reader *r, const MV_CONTEXT_HP *mvc) {
  const vp8_prob *const p = (const vp8_prob *) mvc;
  int x = 0;
381

John Koleszar's avatar
John Koleszar committed
382 383
  if (vp8_read(r, p [mvpis_short_hp])) { /* Large */
    int i = 0;
384

John Koleszar's avatar
John Koleszar committed
385 386 387
    do {
      x += vp8_read(r, p [MVPbits_hp + i]) << i;
    } while (++i < mvnum_short_bits_hp);
388

John Koleszar's avatar
John Koleszar committed
389
    i = mvlong_width_hp - 1;  /* Skip bit 3, which is sometimes implicit */
390

John Koleszar's avatar
John Koleszar committed
391 392 393
    do {
      x += vp8_read(r, p [MVPbits_hp + i]) << i;
    } while (--i > mvnum_short_bits_hp);
394

John Koleszar's avatar
John Koleszar committed
395 396 397 398
    if (!(x & ~((2 << mvnum_short_bits_hp) - 1))  ||  vp8_read(r, p [MVPbits_hp + mvnum_short_bits_hp]))
      x += (mvnum_short_hp);
  } else /* small */
    x = vp8_treed_read(r, vp8_small_mvtree_hp, p + MVPshort_hp);
399

John Koleszar's avatar
John Koleszar committed
400 401
  if (x  &&  vp8_read(r, p [MVPsign_hp]))
    x = -x;
402

John Koleszar's avatar
John Koleszar committed
403
  return x;
404 405
}

John Koleszar's avatar
John Koleszar committed
406 407 408
static void read_mv_hp(vp8_reader *r, MV *mv, const MV_CONTEXT_HP *mvc) {
  mv->row = (short)(read_mvcomponent_hp(r,   mvc));
  mv->col = (short)(read_mvcomponent_hp(r, ++mvc));
409
#ifdef DEBUG_DEC_MV
John Koleszar's avatar
John Koleszar committed
410 411 412 413
  int i;
  printf("%d (hp): %d %d\n", dec_mvcount++, mv->row, mv->col);
  // for (i=0; i<MVPcount_hp;++i) printf("  %d", (&mvc[-1])->prob[i]); printf("\n");
  // for (i=0; i<MVPcount_hp;++i) printf("  %d", (&mvc[0])->prob[i]); printf("\n");
414 415 416
#endif
}

John Koleszar's avatar
John Koleszar committed
417 418
static void read_mvcontexts_hp(vp8_reader *bc, MV_CONTEXT_HP *mvc) {
  int i = 0;
419

John Koleszar's avatar
John Koleszar committed
420 421 422 423
  do {
    const vp8_prob *up = vp8_mv_update_probs_hp[i].prob;
    vp8_prob *p = (vp8_prob *)(mvc + i);
    vp8_prob *const pstop = p + MVPcount_hp;
424

John Koleszar's avatar
John Koleszar committed
425 426 427
    do {
      if (vp8_read(bc, *up++)) {
        const vp8_prob x = (vp8_prob)vp8_read_literal(bc, 7);
428

John Koleszar's avatar
John Koleszar committed
429 430 431 432
        *p = x ? x << 1 : 1;
      }
    } while (++p < pstop);
  } while (++i < 2);
433 434
}

435 436
#endif  /* CONFIG_NEWMVENTROPY */

437
// Read the referncence frame
John Koleszar's avatar
John Koleszar committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
static MV_REFERENCE_FRAME read_ref_frame(VP8D_COMP *pbi,
                                         vp8_reader *const bc,
                                         unsigned char segment_id) {
  MV_REFERENCE_FRAME ref_frame;
  int seg_ref_active;
  int seg_ref_count = 0;

  VP8_COMMON *const cm = & pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;

  seg_ref_active = segfeature_active(xd,
                                     segment_id,
                                     SEG_LVL_REF_FRAME);

  // If segment coding enabled does the segment allow for more than one
  // possible reference frame
  if (seg_ref_active) {
    seg_ref_count = check_segref(xd, segment_id, INTRA_FRAME) +
                    check_segref(xd, segment_id, LAST_FRAME) +
                    check_segref(xd, segment_id, GOLDEN_FRAME) +
                    check_segref(xd, segment_id, ALTREF_FRAME);
  }

  // Segment reference frame features not available or allows for
  // multiple reference frame options
  if (!seg_ref_active || (seg_ref_count > 1)) {
    // Values used in prediction model coding
    unsigned char prediction_flag;
    vp8_prob pred_prob;
    MV_REFERENCE_FRAME pred_ref;

    // Get the context probability the prediction flag
    pred_prob = get_pred_prob(cm, xd, PRED_REF);

    // Read the prediction status flag
    prediction_flag = (unsigned char)vp8_read(bc, pred_prob);

    // Store the prediction flag.
    set_pred_flag(xd, PRED_REF, prediction_flag);

    // Get the predicted reference frame.
    pred_ref = get_pred_ref(cm, xd);

    // If correctly predicted then use the predicted value
    if (prediction_flag) {
      ref_frame = pred_ref;
Paul Wilkins's avatar
Paul Wilkins committed
484
    }
John Koleszar's avatar
John Koleszar committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    // else decode the explicitly coded value
    else {
      vp8_prob mod_refprobs[PREDICTION_PROBS];
      vpx_memcpy(mod_refprobs,
                 cm->mod_refprobs[pred_ref], sizeof(mod_refprobs));

      // If segment coding enabled blank out options that cant occur by
      // setting the branch probability to 0.
      if (seg_ref_active) {
        mod_refprobs[INTRA_FRAME] *=
          check_segref(xd, segment_id, INTRA_FRAME);
        mod_refprobs[LAST_FRAME] *=
          check_segref(xd, segment_id, LAST_FRAME);
        mod_refprobs[GOLDEN_FRAME] *=
          (check_segref(xd, segment_id, GOLDEN_FRAME) *
           check_segref(xd, segment_id, ALTREF_FRAME));
      }

      // Default to INTRA_FRAME (value 0)
      ref_frame = INTRA_FRAME;

      // Do we need to decode the Intra/Inter branch
      if (mod_refprobs[0])
        ref_frame = (MV_REFERENCE_FRAME) vp8_read(bc, mod_refprobs[0]);
      else
        ref_frame++;

      if (ref_frame) {
        // Do we need to decode the Last/Gf_Arf branch
        if (mod_refprobs[1])
          ref_frame += vp8_read(bc, mod_refprobs[1]);
516
        else
John Koleszar's avatar
John Koleszar committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
          ref_frame++;

        if (ref_frame > 1) {
          // Do we need to decode the GF/Arf branch
          if (mod_refprobs[2])
            ref_frame += vp8_read(bc, mod_refprobs[2]);
          else {
            if (seg_ref_active) {
              if ((pred_ref == GOLDEN_FRAME) ||
                  !check_segref(xd, segment_id, GOLDEN_FRAME)) {
                ref_frame = ALTREF_FRAME;
              } else
                ref_frame = GOLDEN_FRAME;
            } else
              ref_frame = (pred_ref == GOLDEN_FRAME)
                          ? ALTREF_FRAME : GOLDEN_FRAME;
          }
534
        }
John Koleszar's avatar
John Koleszar committed
535
      }
536
    }
John Koleszar's avatar
John Koleszar committed
537 538 539 540 541 542 543 544 545 546 547 548
  }

  // Segment reference frame features are enabled
  else {
    // The reference frame for the mb is considered as correclty predicted
    // if it is signaled at the segment level for the purposes of the
    // common prediction model
    set_pred_flag(xd, PRED_REF, 1);
    ref_frame = get_pred_ref(cm, xd);
  }

  return (MV_REFERENCE_FRAME)ref_frame;
549
}
John Koleszar's avatar
John Koleszar committed
550

Ronald S. Bultje's avatar
Ronald S. Bultje committed
551 552 553 554 555
#if CONFIG_SUPERBLOCKS
static MB_PREDICTION_MODE read_sb_mv_ref(vp8_reader *bc, const vp8_prob *p) {
  return (MB_PREDICTION_MODE) vp8_treed_read(bc, vp8_sb_mv_ref_tree, p);
}
#endif
John Koleszar's avatar
John Koleszar committed
556

Ronald S. Bultje's avatar
Ronald S. Bultje committed
557 558
static MB_PREDICTION_MODE read_mv_ref(vp8_reader *bc, const vp8_prob *p) {
  return (MB_PREDICTION_MODE) vp8_treed_read(bc, vp8_mv_ref_tree, p);
John Koleszar's avatar
John Koleszar committed
559 560
}

John Koleszar's avatar
John Koleszar committed
561
static B_PREDICTION_MODE sub_mv_ref(vp8_reader *bc, const vp8_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
562
  return (B_PREDICTION_MODE) vp8_treed_read(bc, vp8_sub_mv_ref_tree, p);
John Koleszar's avatar
John Koleszar committed
563
}
Scott LaVarnway's avatar
Scott LaVarnway committed
564 565

#ifdef VPX_MODE_COUNT
John Koleszar's avatar
John Koleszar committed
566 567 568 569 570 571
unsigned int vp8_mv_cont_count[5][4] = {
  { 0, 0, 0, 0 },
  { 0, 0, 0, 0 },
  { 0, 0, 0, 0 },
  { 0, 0, 0, 0 },
  { 0, 0, 0, 0 }
John Koleszar's avatar
John Koleszar committed
572
};
Scott LaVarnway's avatar
Scott LaVarnway committed
573
#endif
John Koleszar's avatar
John Koleszar committed
574

575 576
static const unsigned char mbsplit_fill_count[4] = {8, 8, 4, 1};
static const unsigned char mbsplit_fill_offset[4][16] = {
John Koleszar's avatar
John Koleszar committed
577 578 579 580
  { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15},
  { 0,  1,  4,  5,  8,  9, 12, 13,  2,  3,   6,  7, 10, 11, 14, 15},
  { 0,  1,  4,  5,  2,  3,  6,  7,  8,  9,  12, 13, 10, 11, 14, 15},
  { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15}
Scott LaVarnway's avatar
Scott LaVarnway committed
581
};
John Koleszar's avatar
John Koleszar committed
582

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
#if CONFIG_SWITCHABLE_INTERP
static void read_switchable_interp_probs(VP8D_COMP *pbi) {
  VP8_COMMON *const cm = & pbi->common;
  vp8_reader *const bc = & pbi->bc;
  int i, j;
  for (j = 0; j <= VP8_SWITCHABLE_FILTERS; ++j) {
  //for (j = 0; j <= 0; ++j) {
    for (i = 0; i < VP8_SWITCHABLE_FILTERS - 1; ++i) {
      cm->fc.switchable_interp_prob[j][i] = vp8_read_literal(bc, 8);
    }
  }
  //printf("DECODER: %d %d\n", cm->fc.switchable_interp_prob[0],
  //cm->fc.switchable_interp_prob[1]);
}
#endif
John Koleszar's avatar
John Koleszar committed
598

John Koleszar's avatar
John Koleszar committed
599 600 601
static void mb_mode_mv_init(VP8D_COMP *pbi) {
  VP8_COMMON *const cm = & pbi->common;
  vp8_reader *const bc = & pbi->bc;
602 603 604
#if CONFIG_NEWMVENTROPY
  nmv_context *const nmvc = &pbi->common.fc.nmvc;
#else
John Koleszar's avatar
John Koleszar committed
605 606
  MV_CONTEXT *const mvc = pbi->common.fc.mvc;
  MV_CONTEXT_HP *const mvc_hp = pbi->common.fc.mvc_hp;
607
#endif
John Koleszar's avatar
John Koleszar committed
608
  MACROBLOCKD *const xd  = & pbi->mb;
John Koleszar's avatar
John Koleszar committed
609

John Koleszar's avatar
John Koleszar committed
610 611 612 613 614 615
  vpx_memset(cm->mbskip_pred_probs, 0, sizeof(cm->mbskip_pred_probs));
  if (pbi->common.mb_no_coeff_skip) {
    int k;
    for (k = 0; k < MBSKIP_CONTEXTS; ++k)
      cm->mbskip_pred_probs[k] = (vp8_prob)vp8_read_literal(bc, 8);
  }
John Koleszar's avatar
John Koleszar committed
616

John Koleszar's avatar
John Koleszar committed
617
  if (cm->frame_type != KEY_FRAME) {
618
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
619
    cm->pred_filter_mode = (vp8_prob)vp8_read_literal(bc, 2);
620

John Koleszar's avatar
John Koleszar committed
621 622
    if (cm->pred_filter_mode == 2)
      cm->prob_pred_filter_off = (vp8_prob)vp8_read_literal(bc, 8);
623 624 625 626
#endif
#if CONFIG_SWITCHABLE_INTERP
    if (cm->mcomp_filter_type == SWITCHABLE)
      read_switchable_interp_probs(pbi);
627
#endif
John Koleszar's avatar
John Koleszar committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    // Decode the baseline probabilities for decoding reference frame
    cm->prob_intra_coded = (vp8_prob)vp8_read_literal(bc, 8);
    cm->prob_last_coded  = (vp8_prob)vp8_read_literal(bc, 8);
    cm->prob_gf_coded    = (vp8_prob)vp8_read_literal(bc, 8);

    // Computes a modified set of probabilities for use when reference
    // frame prediction fails.
    compute_mod_refprobs(cm);

    pbi->common.comp_pred_mode = vp8_read(bc, 128);
    if (cm->comp_pred_mode)
      cm->comp_pred_mode += vp8_read(bc, 128);
    if (cm->comp_pred_mode == HYBRID_PREDICTION) {
      int i;
      for (i = 0; i < COMP_PRED_CONTEXTS; i++)
        cm->prob_comppred[i] = (vp8_prob)vp8_read_literal(bc, 8);
    }
John Koleszar's avatar
John Koleszar committed
645

John Koleszar's avatar
John Koleszar committed
646 647
    if (vp8_read_bit(bc)) {
      int i = 0;
John Koleszar's avatar
John Koleszar committed
648

John Koleszar's avatar
John Koleszar committed
649 650 651 652
      do {
        cm->fc.ymode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8);
      } while (++i < VP8_YMODES - 1);
    }
653 654 655
#if CONFIG_NEWMVENTROPY
    read_nmvprobs(bc, nmvc, xd->allow_high_precision_mv);
#else
John Koleszar's avatar
John Koleszar committed
656 657 658 659
    if (xd->allow_high_precision_mv)
      read_mvcontexts_hp(bc, mvc_hp);
    else
      read_mvcontexts(bc, mvc);
660
#endif
John Koleszar's avatar
John Koleszar committed
661
  }
Scott LaVarnway's avatar
Scott LaVarnway committed
662
}
John Koleszar's avatar
John Koleszar committed
663

664 665 666
// This function either reads the segment id for the current macroblock from
// the bitstream or if the value is temporally predicted asserts the predicted
// value
John Koleszar's avatar
John Koleszar committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
static void read_mb_segment_id(VP8D_COMP *pbi,
                               int mb_row, int mb_col) {
  vp8_reader *const bc = & pbi->bc;
  VP8_COMMON *const cm = & pbi->common;
  MACROBLOCKD *const xd  = & pbi->mb;
  MODE_INFO *mi = xd->mode_info_context;
  MB_MODE_INFO *mbmi = &mi->mbmi;
  int index = mb_row * pbi->common.mb_cols + mb_col;

  if (xd->segmentation_enabled) {
    if (xd->update_mb_segmentation_map) {
      // Is temporal coding of the segment id for this mb enabled.
      if (cm->temporal_update) {
        // Get the context based probability for reading the
        // prediction status flag
        vp8_prob pred_prob =
          get_pred_prob(cm, xd, PRED_SEG_ID);
684

John Koleszar's avatar
John Koleszar committed
685 686 687
        // Read the prediction status flag
        unsigned char seg_pred_flag =
          (unsigned char)vp8_read(bc, pred_prob);
688

John Koleszar's avatar
John Koleszar committed
689 690 691 692 693 694 695
        // Store the prediction flag.
        set_pred_flag(xd, PRED_SEG_ID, seg_pred_flag);

        // If the value is flagged as correctly predicted
        // then use the predicted value
        if (seg_pred_flag) {
          mbmi->segment_id = get_pred_mb_segid(cm, index);
696
        }
John Koleszar's avatar
John Koleszar committed
697 698 699 700 701 702 703 704
        // Else .... decode it explicitly
        else {
          vp8_read_mb_segid(bc, mbmi, xd);
        }
      }
      // Normal unpredicted coding mode
      else {
        vp8_read_mb_segid(bc, mbmi, xd);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
705 706 707 708 709 710 711 712 713 714
      }
#if CONFIG_SUPERBLOCKS
      if (mbmi->encoded_as_sb) {
        cm->last_frame_seg_map[index] =
        cm->last_frame_seg_map[index + 1] =
        cm->last_frame_seg_map[index + cm->mb_cols] =
        cm->last_frame_seg_map[index + cm->mb_cols + 1] = mbmi->segment_id;
      } else
#endif
      {
John Koleszar's avatar
John Koleszar committed
715 716
        cm->last_frame_seg_map[index] = mbmi->segment_id;
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
717 718 719 720 721 722 723 724 725 726 727 728 729
    } else {
#if CONFIG_SUPERBLOCKS
      if (mbmi->encoded_as_sb) {
        mbmi->segment_id =
              cm->last_frame_seg_map[index] &&
              cm->last_frame_seg_map[index + 1] &&
              cm->last_frame_seg_map[index + cm->mb_cols] &&
              cm->last_frame_seg_map[index + cm->mb_cols + 1];
      } else
#endif
      {
        mbmi->segment_id = cm->last_frame_seg_map[index];
      }
730
    }
John Koleszar's avatar
John Koleszar committed
731 732 733 734 735
  } else {
    // The encoder explicitly sets the segment_id to 0
    // when segmentation is disabled
    mbmi->segment_id = 0;
  }
736
}
737

738
static void read_mb_modes_mv(VP8D_COMP *pbi, MODE_INFO *mi, MB_MODE_INFO *mbmi,
739
                             MODE_INFO *prev_mi,
John Koleszar's avatar
John Koleszar committed
740 741 742
                             int mb_row, int mb_col) {
  VP8_COMMON *const cm = & pbi->common;
  vp8_reader *const bc = & pbi->bc;
743 744 745
#if CONFIG_NEWMVENTROPY
  nmv_context *const nmvc = &pbi->common.fc.nmvc;
#else
John Koleszar's avatar
John Koleszar committed
746 747
  MV_CONTEXT *const mvc = pbi->common.fc.mvc;
  MV_CONTEXT_HP *const mvc_hp = pbi->common.fc.mvc_hp;
748
#endif
John Koleszar's avatar
John Koleszar committed
749 750 751
  const int mis = pbi->common.mode_info_stride;
  MACROBLOCKD *const xd  = & pbi->mb;

752
  int_mv *const mv = & mbmi->mv;
John Koleszar's avatar
John Koleszar committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
  int mb_to_left_edge;
  int mb_to_right_edge;
  int mb_to_top_edge;
  int mb_to_bottom_edge;

  mb_to_top_edge = xd->mb_to_top_edge;
  mb_to_bottom_edge = xd->mb_to_bottom_edge;
  mb_to_top_edge -= LEFT_TOP_MARGIN;
  mb_to_bottom_edge += RIGHT_BOTTOM_MARGIN;
  mbmi->need_to_clamp_mvs = 0;
  mbmi->need_to_clamp_secondmv = 0;
  mbmi->second_ref_frame = 0;
  /* Distance of Mb to the various image edges.
   * These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
   */
  xd->mb_to_left_edge =
Scott LaVarnway's avatar
Scott LaVarnway committed
769
    mb_to_left_edge = -((mb_col * 16) << 3);
John Koleszar's avatar
John Koleszar committed
770
  mb_to_left_edge -= LEFT_TOP_MARGIN;
Scott LaVarnway's avatar
Scott LaVarnway committed
771

John Koleszar's avatar
John Koleszar committed
772
  xd->mb_to_right_edge =
Scott LaVarnway's avatar
Scott LaVarnway committed
773
    mb_to_right_edge = ((pbi->common.mb_cols - 1 - mb_col) * 16) << 3;
John Koleszar's avatar
John Koleszar committed
774 775 776 777 778
  mb_to_right_edge += RIGHT_BOTTOM_MARGIN;

  // Make sure the MACROBLOCKD mode info pointer is pointed at the
  // correct entry for the current macroblock.
  xd->mode_info_context = mi;
779
  xd->prev_mode_info_context = prev_mi;
John Koleszar's avatar
John Koleszar committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809

  // Read the macroblock segment id.
  read_mb_segment_id(pbi, mb_row, mb_col);

  if (pbi->common.mb_no_coeff_skip &&
      (!segfeature_active(xd,
                          mbmi->segment_id, SEG_LVL_EOB) ||
       (get_segdata(xd, mbmi->segment_id, SEG_LVL_EOB) != 0))) {
    // Read the macroblock coeff skip flag if this feature is in use,
    // else default to 0
    mbmi->mb_skip_coeff = vp8_read(bc, get_pred_prob(cm, xd, PRED_MBSKIP));
  } else {
    if (segfeature_active(xd,
                          mbmi->segment_id, SEG_LVL_EOB) &&
        (get_segdata(xd, mbmi->segment_id, SEG_LVL_EOB) == 0)) {
      mbmi->mb_skip_coeff = 1;
    } else
      mbmi->mb_skip_coeff = 0;
  }

  // Read the reference frame
  mbmi->ref_frame = read_ref_frame(pbi, bc, mbmi->segment_id);

  // If reference frame is an Inter frame
  if (mbmi->ref_frame) {
    int rct[4];
    int_mv nearest, nearby, best_mv;
    int_mv nearest_second, nearby_second, best_mv_second;
    vp8_prob mv_ref_p [VP8_MVREFS - 1];

810 811 812 813 814
#if CONFIG_NEWBESTREFMV
    int recon_y_stride, recon_yoffset;
    int recon_uv_stride, recon_uvoffset;
#endif

John Koleszar's avatar
John Koleszar committed
815 816 817
    vp8_find_near_mvs(xd, mi,
                      prev_mi,
                      &nearest, &nearby, &best_mv, rct,
818
                      mbmi->ref_frame, cm->ref_frame_sign_bias);
Paul Wilkins's avatar
Paul Wilkins committed
819

820 821 822
#if CONFIG_NEWBESTREFMV
    {
      int ref_fb_idx;
Paul Wilkins's avatar
Paul Wilkins committed
823
      MV_REFERENCE_FRAME ref_frame = mbmi->ref_frame;
824 825

      /* Select the appropriate reference frame for this MB */
Paul Wilkins's avatar
Paul Wilkins committed
826
      if (ref_frame == LAST_FRAME)
827
        ref_fb_idx = cm->lst_fb_idx;
Paul Wilkins's avatar
Paul Wilkins committed
828
      else if (ref_frame == GOLDEN_FRAME)
829 830 831 832 833 834 835 836 837 838 839 840 841 842
        ref_fb_idx = cm->gld_fb_idx;
      else
        ref_fb_idx = cm->alt_fb_idx;

      recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride  ;
      recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;

      recon_yoffset = (mb_row * recon_y_stride * 16) + (mb_col * 16);
      recon_uvoffset = (mb_row * recon_uv_stride * 8) + (mb_col * 8);

      xd->pre.y_buffer = cm->yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
      xd->pre.u_buffer = cm->yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
      xd->pre.v_buffer = cm->yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;

Paul Wilkins's avatar
Paul Wilkins committed
843 844 845 846 847 848 849 850
      // Update stats on relative distance of chosen vector to the
      // possible best reference vectors.
      {
        find_mv_refs(xd, mi, prev_mi,
                     ref_frame, mbmi->ref_mvs[ref_frame],
                     cm->ref_frame_sign_bias );
      }

851 852 853
      vp8_find_best_ref_mvs(xd,
                            xd->pre.y_buffer,
                            recon_y_stride,
Paul Wilkins's avatar
Paul Wilkins committed
854
                            mbmi->ref_mvs[ref_frame],
855
                            &best_mv, &nearest, &nearby);
856 857 858
    }
#endif

John Koleszar's avatar
John Koleszar committed
859 860 861 862 863 864 865
    vp8_mv_ref_probs(&pbi->common, mv_ref_p, rct);

    // Is the segment level mode feature enabled for this segment
    if (segfeature_active(xd, mbmi->segment_id, SEG_LVL_MODE)) {
      mbmi->mode =
        get_segdata(xd, mbmi->segment_id, SEG_LVL_MODE);
    } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
866 867 868 869 870
#if CONFIG_SUPERBLOCKS
      if (mbmi->encoded_as_sb) {
        mbmi->mode = read_sb_mv_ref(bc, mv_ref_p);
      } else
#endif
John Koleszar's avatar
John Koleszar committed
871 872 873
      mbmi->mode = read_mv_ref(bc, mv_ref_p);

      vp8_accum_mv_refs(&pbi->common, mbmi->mode, rct);
874
    }
John Koleszar's avatar
John Koleszar committed
875

876
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
877 878 879 880 881 882 883 884
    if (mbmi->mode >= NEARESTMV && mbmi->mode < SPLITMV) {
      // Is the prediction filter enabled
      if (cm->pred_filter_mode == 2)
        mbmi->pred_filter_enabled =
          vp8_read(bc, cm->prob_pred_filter_off);
      else
        mbmi->pred_filter_enabled = cm->pred_filter_mode;
    }
885
#endif
886 887 888 889 890 891 892 893 894 895 896 897 898
#if CONFIG_SWITCHABLE_INTERP
    if (mbmi->mode >= NEARESTMV && mbmi->mode <= SPLITMV)
    {
      if (cm->mcomp_filter_type == SWITCHABLE) {
        mbmi->interp_filter = vp8_switchable_interp[
            vp8_treed_read(bc, vp8_switchable_interp_tree,
                           get_pred_probs(cm, xd, PRED_SWITCHABLE_INTERP))];
        //printf("Reading: %d\n", mbmi->interp_filter);
      } else {
        mbmi->interp_filter = cm->mcomp_filter_type;
      }
    }
#endif
899

John Koleszar's avatar
John Koleszar committed
900 901 902 903 904 905 906 907 908 909 910 911 912
    if (cm->comp_pred_mode == COMP_PREDICTION_ONLY ||
        (cm->comp_pred_mode == HYBRID_PREDICTION &&
         vp8_read(bc, get_pred_prob(cm, xd, PRED_COMP)))) {
      /* Since we have 3 reference frames, we can only have 3 unique
       * combinations of combinations of 2 different reference frames
       * (A-G, G-L or A-L). In the bitstream, we use this to simply
       * derive the second reference frame from the first reference
       * frame, by saying it's the next one in the enumerator, and
       * if that's > n_refs, then the second reference frame is the
       * first one in the enumerator. */
      mbmi->second_ref_frame = mbmi->ref_frame + 1;
      if (mbmi->second_ref_frame == 4)
        mbmi->second_ref_frame = 1;
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
#if CONFIG_NEWBESTREFMV
      if (mbmi->second_ref_frame) {
        int second_ref_fb_idx;
        /* Select the appropriate reference frame for this MB */
        if (mbmi->second_ref_frame == LAST_FRAME)
          second_ref_fb_idx = cm->lst_fb_idx;
        else if (mbmi->second_ref_frame ==
          GOLDEN_FRAME)
          second_ref_fb_idx = cm->gld_fb_idx;
        else
          second_ref_fb_idx = cm->alt_fb_idx;

        xd->second_pre.y_buffer =
          cm->yv12_fb[second_ref_fb_idx].y_buffer + recon_yoffset;
        xd->second_pre.u_buffer =
          cm->yv12_fb[second_ref_fb_idx].u_buffer + recon_uvoffset;
        xd->second_pre.v_buffer =
          cm->yv12_fb[second_ref_fb_idx].v_buffer + recon_uvoffset;
        vp8_find_near_mvs(xd, mi, prev_mi,
                          &nearest_second, &nearby_second, &best_mv_second,
                          rct,
                          mbmi->second_ref_frame,
                          cm->ref_frame_sign_bias);
Paul Wilkins's avatar
Paul Wilkins committed
936 937 938 939 940 941 942 943 944 945 946

        // Update stats on relative distance of chosen vector to the
        // possible best reference vectors.
        {
          MV_REFERENCE_FRAME ref_frame = mbmi->second_ref_frame;

          find_mv_refs(xd, mi, prev_mi,
                       ref_frame, mbmi->ref_mvs[ref_frame],
                       cm->ref_frame_sign_bias );
        }

947 948 949
        vp8_find_best_ref_mvs(xd,
                              xd->second_pre.y_buffer,
                              recon_y_stride,
Paul Wilkins's avatar
Paul Wilkins committed
950
                              mbmi->ref_mvs[mbmi->second_ref_frame],
951 952 953
                              &best_mv_second,
                              &nearest_second,
                              &nearby_second);
954 955 956 957 958 959 960 961
      }
#else
      vp8_find_near_mvs(xd, mi, prev_mi,
                        &nearest_second, &nearby_second, &best_mv_second,
                        rct,
                        mbmi->second_ref_frame,
                        pbi->common.ref_frame_sign_bias);
#endif
John Koleszar's avatar
John Koleszar committed
962 963 964
    } else {
      mbmi->second_ref_frame = 0;
    }
965

John Koleszar's avatar
John Koleszar committed
966 967 968 969 970 971 972 973
    mbmi->uv_mode = DC_PRED;
    switch (mbmi->mode) {
      case SPLITMV: {
        const int s = mbmi->partitioning =
                        vp8_treed_read(bc, vp8_mbsplit_tree, cm->fc.mbsplit_prob);
        const int num_p = vp8_mbsplit_count [s];
        int j = 0;
        cm->fc.mbsplit_counts[s]++;
John Koleszar's avatar
John Koleszar committed
974

John Koleszar's avatar
John Koleszar committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
        mbmi->need_to_clamp_mvs = 0;
        do { /* for each subset j */
          int_mv leftmv, abovemv, second_leftmv, second_abovemv;
          int_mv blockmv, secondmv;
          int k;  /* first block in subset j */
          int mv_contz;
          int blockmode;

          k = vp8_mbsplit_offset[s][j];

          leftmv.as_int = left_block_mv(mi, k);
          abovemv.as_int = above_block_mv(mi, k, mis);
          if (mbmi->second_ref_frame) {
            second_leftmv.as_int = left_block_second_mv(mi, k);
            second_abovemv.as_int = above_block_second_mv(mi, k, mis);
          }
          mv_contz = vp8_mv_cont(&leftmv, &abovemv);
          blockmode = sub_mv_ref(bc, cm->fc.sub_mv_ref_prob [mv_contz]);
          cm->fc.sub_mv_ref_counts[mv_contz][blockmode - LEFT4X4]++;
John Koleszar's avatar
John Koleszar committed
994

John Koleszar's avatar
John Koleszar committed
995 996
          switch (blockmode) {
            case NEW4X4:
997 998 999 1000 1001 1002 1003
#if CONFIG_NEWMVENTROPY
              read_nmv(bc, &blockmv.as_mv, &best_mv.as_mv, nmvc);
              read_nmv_fp(bc, &blockmv.as_mv, &best_mv.as_mv, nmvc,
                          xd->allow_high_precision_mv);
              vp8_increment_nmv(&blockmv.as_mv, &best_mv.as_mv,
                                &cm->fc.NMVcount, xd->allow_high_precision_mv);
#else
John Koleszar's avatar
John Koleszar committed
1004 1005 1006 1007
              if (xd->allow_high_precision_mv) {
                read_mv_hp(bc, &blockmv.as_mv, (const MV_CONTEXT_HP *) mvc_hp);
                cm->fc.MVcount_hp[0][mv_max_hp + (blockmv.as_mv.row)]++;
                cm->fc.MVcount_hp[1][mv_max_hp + (blockmv.as_mv.col)]++;
1008
              } else {
John Koleszar's avatar
John Koleszar committed
1009 1010 1011 1012
                read_mv(bc, &blockmv.as_mv, (const MV_CONTEXT *) mvc);
                cm->fc.MVcount[0][mv_max + (blockmv.as_mv.row >> 1)]++;
                cm->fc.MVcount[1][mv_max + (blockmv.as_mv.col >> 1)]++;
              }
1013
#endif  /* CONFIG_NEWMVENTROPY */
John Koleszar's avatar
John Koleszar committed
1014 1015
              blockmv.as_mv.row += best_mv.as_mv.row;
              blockmv.as_mv.col += best_mv.as_mv.col;
1016

John Koleszar's avatar
John Koleszar committed
1017
              if (mbmi->second_ref_frame) {
1018 1019 1020 1021 1022 1023 1024
#if CONFIG_NEWMVENTROPY
                read_nmv(bc, &secondmv.as_mv, &best_mv_second.as_mv, nmvc);
                read_nmv_fp(bc, &secondmv.as_mv, &best_mv_second.as_mv, nmvc,
                            xd->allow_high_precision_mv);
                vp8_increment_nmv(&secondmv.as_mv, &best_mv_second.as_mv,
                                  &cm->fc.NMVcount, xd->allow_high_precision_mv);
#else
John Koleszar's avatar
John Koleszar committed
1025 1026 1027 1028
                if (xd->allow_high_precision_mv) {
                  read_mv_hp(bc, &secondmv.as_mv, (const MV_CONTEXT_HP *) mvc_hp);
                  cm->fc.MVcount_hp[0][mv_max_hp + (secondmv.as_mv.row)]++;
                  cm->fc.MVcount_hp[1][mv_max_hp + (secondmv.as_mv.col)]++;
1029
                } else {
John Koleszar's avatar
John Koleszar committed
1030 1031 1032 1033
                  read_mv(bc, &secondmv.as_mv, (const MV_CONTEXT *) mvc);
                  cm->fc.MVcount[0][mv_max + (secondmv.as_mv.row >> 1)]++;
                  cm->fc.MVcount[1][mv_max + (secondmv.as_mv.col >> 1)]++;
                }
1034
#endif  /* CONFIG_NEWMVENTROPY */
John Koleszar's avatar
John Koleszar committed
1035 1036 1037
                secondmv.as_mv.row += best_mv_second.as_mv.row;
                secondmv.as_mv.col += best_mv_second.as_mv.col;
              }
1038
#ifdef VPX_MODE_COUNT
John Koleszar's avatar
John Koleszar committed
1039
              vp8_mv_cont_count[mv_contz][3]++;
1040
#endif
John Koleszar's avatar
John Koleszar committed
1041 1042 1043 1044 1045
              break;
            case LEFT4X4:
              blockmv.as_int = leftmv.as_int;
              if (mbmi->second_ref_frame)
                secondmv.as_int = second_leftmv.as_int;
1046
#ifdef VPX_MODE_COUNT
John Koleszar's avatar
John Koleszar committed
1047
              vp8_mv_cont_count[mv_contz][0]++;
1048
#endif
John Koleszar's avatar
John Koleszar committed
1049 1050 1051 1052 1053
              break;
            case ABOVE4X4:
              blockmv.as_int = abovemv.as_int;
              if (mbmi->second_ref_frame)
                secondmv.as_int = second_abovemv.as_int;
1054
#ifdef VPX_MODE_COUNT
John Koleszar's avatar
John Koleszar committed
1055
              vp8_mv_cont_count[mv_contz][1]++;
1056
#endif
John Koleszar's avatar
John Koleszar committed
1057 1058 1059 1060 1061
              break;
            case ZERO4X4:
              blockmv.as_int = 0;
              if (mbmi->second_ref_frame)
                secondmv.as_int = 0;
1062
#ifdef VPX_MODE_COUNT
John Koleszar's avatar
John Koleszar committed
1063
              vp8_mv_cont_count[mv_contz][2]++;
1064
#endif
John Koleszar's avatar
John Koleszar committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
              break;
            default:
              break;
          }

          mbmi->need_to_clamp_mvs |= vp8_check_mv_bounds(&blockmv,
                                                         mb_to_left_edge,
                                                         mb_to_right_edge,
                                                         mb_to_top_edge,
                                                         mb_to_bottom_edge);
          if (mbmi->second_ref_frame) {
            mbmi->need_to_clamp_mvs |= vp8_check_mv_bounds(&secondmv,
                                                           mb_to_left_edge,
                                                           mb_to_right_edge,
                                                           mb_to_top_edge,
                                                           mb_to_bottom_edge);
          }

          {
            /* Fill (uniform) modes, mvs of jth subset.
             Must do it here because ensuing subsets can
             refer back to us via "left" or "above". */
            const unsigned char *fill_offset;
            unsigned int fill_count = mbsplit_fill_count[s];

            fill_offset = &mbsplit_fill_offset[s][(unsigned char)j * mbsplit_fill_count[s]];

            do {
              mi->bmi[ *fill_offset].as_mv.first.as_int = blockmv.as_int;
              if (mbmi->second_ref_frame)
                mi->bmi[ *fill_offset].as_mv.second.as_int = secondmv.as_int;
              fill_offset++;
            } while (--fill_count);
          }

        } while (++j < num_p);
      }

      mv->as_int = mi->bmi[15].as_mv.first.as_int;
1104
      mbmi->mv[1].as_int = mi->bmi[15].as_mv.second.as_int;