vp9_bitstream.c 51.6 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 13
#include <assert.h>
#include <stdio.h>
#include <limits.h>
John Koleszar's avatar
John Koleszar committed
14

15 16 17
#include "vpx/vpx_encoder.h"
#include "vpx_mem/vpx_mem.h"

18
#include "vp9/common/vp9_entropymode.h"
19
#include "vp9/common/vp9_entropymv.h"
20
#include "vp9/common/vp9_findnearmv.h"
21
#include "vp9/common/vp9_tile_common.h"
22 23 24 25 26
#include "vp9/common/vp9_seg_common.h"
#include "vp9/common/vp9_pred_common.h"
#include "vp9/common/vp9_entropy.h"
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_mvref_common.h"
27
#include "vp9/common/vp9_treecoder.h"
28 29 30 31 32 33 34
#include "vp9/common/vp9_systemdependent.h"
#include "vp9/common/vp9_pragmas.h"

#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/encoder/vp9_encodemv.h"
#include "vp9/encoder/vp9_bitstream.h"
#include "vp9/encoder/vp9_segmentation.h"
35
#include "vp9/encoder/vp9_subexp.h"
36 37
#include "vp9/encoder/vp9_write_bit_buffer.h"

Paul Wilkins's avatar
Paul Wilkins committed
38

John Koleszar's avatar
John Koleszar committed
39 40 41 42 43
#if defined(SECTIONBITS_OUTPUT)
unsigned __int64 Sectionbits[500];
#endif

#ifdef ENTROPY_STATS
44 45 46
int intra_mode_stats[INTRA_MODES]
                    [INTRA_MODES]
                    [INTRA_MODES];
47
vp9_coeff_stats tree_update_hist[TX_SIZES][BLOCK_TYPES];
48

John Koleszar's avatar
John Koleszar committed
49 50 51
extern unsigned int active_section;
#endif

52

53
#ifdef MODE_STATS
54 55 56
int64_t tx_count_32x32p_stats[TX_SIZE_CONTEXTS][TX_SIZES];
int64_t tx_count_16x16p_stats[TX_SIZE_CONTEXTS][TX_SIZES - 1];
int64_t tx_count_8x8p_stats[TX_SIZE_CONTEXTS][TX_SIZES - 2];
57 58
int64_t switchable_interp_stats[SWITCHABLE_FILTERS+1]
                               [SWITCHABLE_FILTERS];
59 60 61 62 63 64 65

void init_tx_count_stats() {
  vp9_zero(tx_count_32x32p_stats);
  vp9_zero(tx_count_16x16p_stats);
  vp9_zero(tx_count_8x8p_stats);
}

66 67 68 69
void init_switchable_interp_stats() {
  vp9_zero(switchable_interp_stats);
}

70 71
static void update_tx_count_stats(VP9_COMMON *cm) {
  int i, j;
72
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
73
    for (j = 0; j < TX_SIZES; j++) {
74 75 76
      tx_count_32x32p_stats[i][j] += cm->fc.tx_count_32x32p[i][j];
    }
  }
77
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
78
    for (j = 0; j < TX_SIZES - 1; j++) {
79 80 81
      tx_count_16x16p_stats[i][j] += cm->fc.tx_count_16x16p[i][j];
    }
  }
82
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
83
    for (j = 0; j < TX_SIZES - 2; j++) {
84 85 86 87 88
      tx_count_8x8p_stats[i][j] += cm->fc.tx_count_8x8p[i][j];
    }
  }
}

89 90
static void update_switchable_interp_stats(VP9_COMMON *cm) {
  int i, j;
91 92
  for (i = 0; i < SWITCHABLE_FILTERS+1; ++i)
    for (j = 0; j < SWITCHABLE_FILTERS; ++j) {
93 94 95 96
      switchable_interp_stats[i][j] += cm->fc.switchable_interp_count[i][j];
    }
}

97 98 99 100 101 102 103 104
void write_tx_count_stats() {
  int i, j;
  FILE *fp = fopen("tx_count.bin", "wb");
  fwrite(tx_count_32x32p_stats, sizeof(tx_count_32x32p_stats), 1, fp);
  fwrite(tx_count_16x16p_stats, sizeof(tx_count_16x16p_stats), 1, fp);
  fwrite(tx_count_8x8p_stats, sizeof(tx_count_8x8p_stats), 1, fp);
  fclose(fp);

105
  printf(
106
      "vp9_default_tx_count_32x32p[TX_SIZE_CONTEXTS][TX_SIZES] = {\n");
107 108
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
    printf("  { ");
109
    for (j = 0; j < TX_SIZES; j++) {
110 111 112 113 114
      printf("%"PRId64", ", tx_count_32x32p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
115
  printf(
116
      "vp9_default_tx_count_16x16p[TX_SIZE_CONTEXTS][TX_SIZES-1] = {\n");
117 118
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
    printf("  { ");
119
    for (j = 0; j < TX_SIZES - 1; j++) {
120 121 122 123 124
      printf("%"PRId64", ", tx_count_16x16p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
125
  printf(
126
      "vp9_default_tx_count_8x8p[TX_SIZE_CONTEXTS][TX_SIZES-2] = {\n");
127 128
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
    printf("  { ");
129
    for (j = 0; j < TX_SIZES - 2; j++) {
130 131 132 133 134 135
      printf("%"PRId64", ", tx_count_8x8p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
}
136 137 138 139 140 141 142 143

void write_switchable_interp_stats() {
  int i, j;
  FILE *fp = fopen("switchable_interp.bin", "wb");
  fwrite(switchable_interp_stats, sizeof(switchable_interp_stats), 1, fp);
  fclose(fp);

  printf(
144 145 146
      "vp9_default_switchable_filter_count[SWITCHABLE_FILTERS+1]"
      "[SWITCHABLE_FILTERS] = {\n");
  for (i = 0; i < SWITCHABLE_FILTERS+1; i++) {
147
    printf("  { ");
148
    for (j = 0; j < SWITCHABLE_FILTERS; j++) {
149 150 151 152 153 154
      printf("%"PRId64", ", switchable_interp_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
}
155 156
#endif

157 158 159 160 161
static INLINE void write_be32(uint8_t *p, int value) {
  p[0] = value >> 24;
  p[1] = value >> 16;
  p[2] = value >> 8;
  p[3] = value;
162 163
}

164 165 166 167 168
void vp9_encode_unsigned_max(struct vp9_write_bit_buffer *wb,
                             int data, int max) {
  vp9_wb_write_literal(wb, data, get_unsigned_bits(max));
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
static void update_mode(
  vp9_writer *w,
  int n,
  vp9_tree tree,
  vp9_prob Pnew[/* n-1 */],
  vp9_prob Pcur[/* n-1 */],
  unsigned int bct[/* n-1 */] [2],
  const unsigned int num_events[/* n */]
) {
  int i = 0;

  vp9_tree_probs_from_distribution(tree, Pnew, bct, num_events, 0);
  n--;

  for (i = 0; i < n; ++i) {
184
    vp9_cond_prob_diff_update(w, &Pcur[i], MODE_UPDATE_PROB, bct[i]);
185 186 187 188 189 190
  }
}

static void update_mbintra_mode_probs(VP9_COMP* const cpi,
                                      vp9_writer* const bc) {
  VP9_COMMON *const cm = &cpi->common;
191
  int j;
192 193
  vp9_prob pnew[INTRA_MODES - 1];
  unsigned int bct[INTRA_MODES - 1][2];
194

195
  for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
196
    update_mode(bc, INTRA_MODES, vp9_intra_mode_tree, pnew,
197 198
                cm->fc.y_mode_prob[j], bct,
                (unsigned int *)cpi->y_mode_count[j]);
199 200
}

201
static void write_selected_tx_size(const VP9_COMP *cpi, TX_SIZE tx_size,
202
                                   BLOCK_SIZE bsize, vp9_writer *w) {
203
  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
204
  const vp9_prob *tx_probs = get_tx_probs2(xd, &cpi->common.fc.tx_probs);
205
  vp9_write(w, tx_size != TX_4X4, tx_probs[0]);
206
  if (bsize >= BLOCK_16X16 && tx_size != TX_4X4) {
207
    vp9_write(w, tx_size != TX_8X8, tx_probs[1]);
208
    if (bsize >= BLOCK_32X32 && tx_size != TX_8X8)
209 210 211 212
      vp9_write(w, tx_size != TX_16X16, tx_probs[2]);
  }
}

213 214 215
static int write_skip_coeff(const VP9_COMP *cpi, int segment_id, MODE_INFO *m,
                            vp9_writer *w) {
  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
216
  if (vp9_segfeature_active(&cpi->common.seg, segment_id, SEG_LVL_SKIP)) {
217 218
    return 1;
  } else {
Paul Wilkins's avatar
Paul Wilkins committed
219
    const int skip_coeff = m->mbmi.skip_coeff;
220
    vp9_write(w, skip_coeff, vp9_get_pred_prob_mbskip(&cpi->common, xd));
221 222 223 224
    return skip_coeff;
  }
}

225
void vp9_update_skip_probs(VP9_COMP *cpi, vp9_writer *w) {
226
  VP9_COMMON *cm = &cpi->common;
227 228
  int k;

229
  for (k = 0; k < MBSKIP_CONTEXTS; ++k)
230
    vp9_cond_prob_diff_update(w, &cm->fc.mbskip_probs[k],
231
                              MODE_UPDATE_PROB, cm->counts.mbskip[k]);
232 233 234 235 236 237
}

static void write_intra_mode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_intra_mode_tree, p, vp9_intra_mode_encodings + m);
}

238
static void update_switchable_interp_probs(VP9_COMP *const cpi,
239
                                           vp9_writer* const bc) {
240
  VP9_COMMON *const cm = &cpi->common;
241 242 243
  unsigned int branch_ct[SWITCHABLE_FILTERS + 1]
                        [SWITCHABLE_FILTERS - 1][2];
  vp9_prob new_prob[SWITCHABLE_FILTERS + 1][SWITCHABLE_FILTERS - 1];
244
  int i, j;
245
  for (j = 0; j <= SWITCHABLE_FILTERS; ++j) {
246 247 248
    vp9_tree_probs_from_distribution(
        vp9_switchable_interp_tree,
        new_prob[j], branch_ct[j],
249
        cm->counts.switchable_interp[j], 0);
250
  }
251 252
  for (j = 0; j <= SWITCHABLE_FILTERS; ++j) {
    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i) {
253
      vp9_cond_prob_diff_update(bc, &cm->fc.switchable_interp_prob[j][i],
254
                                MODE_UPDATE_PROB, branch_ct[j][i]);
255 256
    }
  }
257 258
#ifdef MODE_STATS
  if (!cpi->dummy_packing)
259
    update_switchable_interp_stats(cm);
260
#endif
261 262
}

263
static void update_inter_mode_probs(VP9_COMMON *cm, vp9_writer* const bc) {
264 265
  int i, j;

266
  for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
267 268
    unsigned int branch_ct[INTER_MODES - 1][2];
    vp9_prob new_prob[INTER_MODES - 1];
269 270 271

    vp9_tree_probs_from_distribution(vp9_inter_mode_tree,
                                     new_prob, branch_ct,
272
                                     cm->counts.inter_mode[i], NEARESTMV);
273

274
    for (j = 0; j < INTER_MODES - 1; ++j)
275
      vp9_cond_prob_diff_update(bc, &cm->fc.inter_mode_probs[i][j],
276
                                MODE_UPDATE_PROB, branch_ct[j]);
277 278 279
  }
}

280
static void pack_mb_tokens(vp9_writer* const bc,
281 282 283
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
284

John Koleszar's avatar
John Koleszar committed
285
  while (p < stop) {
286
    const int t = p->token;
287
    const struct vp9_token *const a = vp9_coef_encodings + t;
288
    const vp9_extra_bit *const b = vp9_extra_bits + t;
John Koleszar's avatar
John Koleszar committed
289
    int i = 0;
290
    const vp9_prob *pp;
John Koleszar's avatar
John Koleszar committed
291
    int v = a->value;
292
    int n = a->len;
293
    vp9_prob probs[ENTROPY_NODES];
John Koleszar's avatar
John Koleszar committed
294

295
    if (t == EOSB_TOKEN) {
296 297 298
      ++p;
      break;
    }
299
    if (t >= TWO_TOKEN) {
300
      vp9_model_to_full_probs(p->context_tree, probs);
301 302 303 304
      pp = probs;
    } else {
      pp = p->context_tree;
    }
305
    assert(pp != 0);
306

John Koleszar's avatar
John Koleszar committed
307 308 309 310 311
    /* skip one or two nodes */
    if (p->skip_eob_node) {
      n -= p->skip_eob_node;
      i = 2 * p->skip_eob_node;
    }
John Koleszar's avatar
John Koleszar committed
312

John Koleszar's avatar
John Koleszar committed
313 314
    do {
      const int bb = (v >> --n) & 1;
315
      vp9_write(bc, bb, pp[i >> 1]);
316
      i = vp9_coef_tree[i + bb];
317
    } while (n);
John Koleszar's avatar
John Koleszar committed
318

John Koleszar's avatar
John Koleszar committed
319
    if (b->base_val) {
320
      const int e = p->extra, l = b->len;
John Koleszar's avatar
John Koleszar committed
321

322
      if (l) {
323
        const unsigned char *pb = b->prob;
John Koleszar's avatar
John Koleszar committed
324
        int v = e >> 1;
325
        int n = l;              /* number of bits in v, assumed nonzero */
John Koleszar's avatar
John Koleszar committed
326
        int i = 0;
John Koleszar's avatar
John Koleszar committed
327

John Koleszar's avatar
John Koleszar committed
328 329
        do {
          const int bb = (v >> --n) & 1;
330
          vp9_write(bc, bb, pb[i >> 1]);
John Koleszar's avatar
John Koleszar committed
331 332 333
          i = b->tree[i + bb];
        } while (n);
      }
John Koleszar's avatar
John Koleszar committed
334

335
      vp9_write_bit(bc, e & 1);
John Koleszar's avatar
John Koleszar committed
336
    }
John Koleszar's avatar
John Koleszar committed
337 338 339
    ++p;
  }

340
  *tp = p;
John Koleszar's avatar
John Koleszar committed
341 342
}

343
static void write_sb_mv_ref(vp9_writer *w, MB_PREDICTION_MODE mode,
344
                            const vp9_prob *p) {
345
  assert(is_inter_mode(mode));
346 347
  write_token(w, vp9_inter_mode_tree, p,
              &vp9_inter_mode_encodings[mode - NEARESTMV]);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
348 349
}

350

351
static void write_segment_id(vp9_writer *w, const struct segmentation *seg,
352
                             int segment_id) {
353 354
  if (seg->enabled && seg->update_map)
    treed_write(w, vp9_segment_tree, seg->tree_probs, segment_id, 3);
John Koleszar's avatar
John Koleszar committed
355 356
}

Paul Wilkins's avatar
Paul Wilkins committed
357
// This function encodes the reference frame
Ronald S. Bultje's avatar
Ronald S. Bultje committed
358
static void encode_ref_frame(VP9_COMP *cpi, vp9_writer *bc) {
359
  VP9_COMMON *const cm = &cpi->common;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
360 361 362 363
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *mi = &xd->mode_info_context->mbmi;
  const int segment_id = mi->segment_id;
364
  int seg_ref_active = vp9_segfeature_active(&cm->seg, segment_id,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
365
                                             SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
366 367
  // If segment level coding of this signal is disabled...
  // or the segment allows multiple reference frame options
368
  if (!seg_ref_active) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
369 370
    // does the feature use compound prediction or not
    // (if not specified at the frame/segment level)
371
    if (cm->comp_pred_mode == HYBRID_PREDICTION) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
372
      vp9_write(bc, mi->ref_frame[1] > INTRA_FRAME,
373
                vp9_get_pred_prob_comp_inter_inter(cm, xd));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
374 375
    } else {
      assert((mi->ref_frame[1] <= INTRA_FRAME) ==
376
                 (cm->comp_pred_mode == SINGLE_PREDICTION_ONLY));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
377
    }
378

Ronald S. Bultje's avatar
Ronald S. Bultje committed
379 380
    if (mi->ref_frame[1] > INTRA_FRAME) {
      vp9_write(bc, mi->ref_frame[0] == GOLDEN_FRAME,
381
                vp9_get_pred_prob_comp_ref_p(cm, xd));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
382 383
    } else {
      vp9_write(bc, mi->ref_frame[0] != LAST_FRAME,
384
                vp9_get_pred_prob_single_ref_p1(cm, xd));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
385 386
      if (mi->ref_frame[0] != LAST_FRAME)
        vp9_write(bc, mi->ref_frame[0] != GOLDEN_FRAME,
387
                  vp9_get_pred_prob_single_ref_p2(cm, xd));
Paul Wilkins's avatar
Paul Wilkins committed
388
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
389 390
  } else {
    assert(mi->ref_frame[1] <= INTRA_FRAME);
391
    assert(vp9_get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) ==
392
           mi->ref_frame[0]);
John Koleszar's avatar
John Koleszar committed
393
  }
Paul Wilkins's avatar
Paul Wilkins committed
394

John Koleszar's avatar
John Koleszar committed
395 396
  // if using the prediction mdoel we have nothing further to do because
  // the reference frame is fully coded by the segment
Paul Wilkins's avatar
Paul Wilkins committed
397
}
John Koleszar's avatar
John Koleszar committed
398

399
static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc) {
400 401
  VP9_COMMON *const cm = &cpi->common;
  const nmv_context *nmvc = &cm->fc.nmvc;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
402 403
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
404
  struct segmentation *seg = &cm->seg;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
405
  MB_MODE_INFO *const mi = &m->mbmi;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
406
  const MV_REFERENCE_FRAME rf = mi->ref_frame[0];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
407 408 409
  const MB_PREDICTION_MODE mode = mi->mode;
  const int segment_id = mi->segment_id;
  int skip_coeff;
410
  const BLOCK_SIZE bsize = mi->sb_type;
411
  const int allow_hp = xd->allow_high_precision_mv;
Adrian Grange's avatar
Adrian Grange committed
412

413
  x->partition_info = x->pi + (m - cm->mi);
414

Ronald S. Bultje's avatar
Ronald S. Bultje committed
415 416
#ifdef ENTROPY_STATS
  active_section = 9;
417
#endif
418

419 420
  if (seg->update_map) {
    if (seg->temporal_update) {
Scott LaVarnway's avatar
Scott LaVarnway committed
421
      const int pred_flag = mi->seg_id_predicted;
422
      vp9_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
423 424 425
      vp9_write(bc, pred_flag, pred_prob);
      if (!pred_flag)
        write_segment_id(bc, seg, segment_id);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
426
    } else {
427
      write_segment_id(bc, seg, segment_id);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
428 429
    }
  }
430

431
  skip_coeff = write_skip_coeff(cpi, segment_id, m, bc);
John Koleszar's avatar
John Koleszar committed
432

433
  if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME))
434
    vp9_write(bc, rf != INTRA_FRAME,
435
              vp9_get_pred_prob_intra_inter(cm, xd));
Paul Wilkins's avatar
Paul Wilkins committed
436

437
  if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT &&
438
      !(rf != INTRA_FRAME &&
439
        (skip_coeff || vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)))) {
440
    write_selected_tx_size(cpi, mi->tx_size, bsize, bc);
441 442
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
443
  if (rf == INTRA_FRAME) {
444
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
445
    active_section = 6;
446
#endif
Paul Wilkins's avatar
Paul Wilkins committed
447

448
    if (bsize >= BLOCK_8X8) {
449
      write_intra_mode(bc, mode, cm->fc.y_mode_prob[size_group_lookup[bsize]]);
450
    } else {
451
      int idx, idy;
Jim Bankoski's avatar
Jim Bankoski committed
452 453 454
      const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
      const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
      for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
Jim Bankoski's avatar
Jim Bankoski committed
455
        for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
456
          const MB_PREDICTION_MODE bm = m->bmi[idy * 2 + idx].as_mode;
457
          write_intra_mode(bc, bm, cm->fc.y_mode_prob[0]);
458
        }
Jim Bankoski's avatar
Jim Bankoski committed
459
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
460
    }
461
    write_intra_mode(bc, mi->uv_mode, cm->fc.uv_mode_prob[mode]);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
462
  } else {
463
    vp9_prob *mv_ref_p;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
464
    encode_ref_frame(cpi, bc);
Paul Wilkins's avatar
Paul Wilkins committed
465
    mv_ref_p = cpi->common.fc.inter_mode_probs[mi->mode_context[rf]];
Yaowu Xu's avatar
Yaowu Xu committed
466

John Koleszar's avatar
John Koleszar committed
467
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
468
    active_section = 3;
John Koleszar's avatar
John Koleszar committed
469 470
#endif

471
    // If segment skip is not enabled code the mode.
472
    if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)) {
473
      if (bsize >= BLOCK_8X8) {
474
        write_sb_mv_ref(bc, mode, mv_ref_p);
475
        ++cm->counts.inter_mode[mi->mode_context[rf]]
476
                               [inter_mode_offset(mode)];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
477
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
478
    }
479

480
    if (cm->mcomp_filter_type == SWITCHABLE) {
481
      const int ctx = vp9_get_pred_context_switchable_interp(xd);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
482
      write_token(bc, vp9_switchable_interp_tree,
483
                  cm->fc.switchable_interp_prob[ctx],
484
                  &vp9_switchable_interp_encodings[mi->interp_filter]);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
485
    } else {
486
      assert(mi->interp_filter == cm->mcomp_filter_type);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
487
    }
488

489
    if (bsize < BLOCK_8X8) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
490 491 492
      int j;
      MB_PREDICTION_MODE blockmode;
      int_mv blockmv;
Jim Bankoski's avatar
Jim Bankoski committed
493 494
      const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
      const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
495
      int idx, idy;
Jim Bankoski's avatar
Jim Bankoski committed
496 497
      for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
        for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
498
          j = idy * 2 + idx;
Scott LaVarnway's avatar
Scott LaVarnway committed
499
          blockmode = x->partition_info->bmi[j].mode;
500
          blockmv = m->bmi[j].as_mv[0];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
501
          write_sb_mv_ref(bc, blockmode, mv_ref_p);
502
          ++cm->counts.inter_mode[mi->mode_context[rf]]
503 504
                                 [inter_mode_offset(blockmode)];

Ronald S. Bultje's avatar
Ronald S. Bultje committed
505
          if (blockmode == NEWMV) {
506
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
507
            active_section = 11;
508
#endif
Paul Wilkins's avatar
Paul Wilkins committed
509
            vp9_encode_mv(cpi, bc, &blockmv.as_mv, &mi->best_mv.as_mv,
510
                          nmvc, allow_hp);
511

Ronald S. Bultje's avatar
Ronald S. Bultje committed
512
            if (mi->ref_frame[1] > INTRA_FRAME)
Paul Wilkins's avatar
Paul Wilkins committed
513
              vp9_encode_mv(cpi, bc,
514
                            &m->bmi[j].as_mv[1].as_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
515
                            &mi->best_second_mv.as_mv,
516
                            nmvc, allow_hp);
John Koleszar's avatar
John Koleszar committed
517
          }
518
        }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
519 520 521 522 523
      }
    } else if (mode == NEWMV) {
#ifdef ENTROPY_STATS
      active_section = 5;
#endif
524 525
      vp9_encode_mv(cpi, bc, &mi->mv[0].as_mv, &mi->best_mv.as_mv,
                    nmvc, allow_hp);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
526

Ronald S. Bultje's avatar
Ronald S. Bultje committed
527
      if (mi->ref_frame[1] > INTRA_FRAME)
528 529
        vp9_encode_mv(cpi, bc, &mi->mv[1].as_mv, &mi->best_second_mv.as_mv,
                      nmvc, allow_hp);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
530 531
    }
  }
John Koleszar's avatar
John Koleszar committed
532
}
533

534 535
static void write_mb_modes_kf(const VP9_COMP *cpi, MODE_INFO *m,
                              vp9_writer *bc) {
536
  const VP9_COMMON *const cm = &cpi->common;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
537
  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
538
  const struct segmentation *const seg = &cm->seg;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
539
  const int ym = m->mbmi.mode;
540
  const int mis = cm->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
541
  const int segment_id = m->mbmi.segment_id;
542

543 544
  if (seg->update_map)
    write_segment_id(bc, seg, m->mbmi.segment_id);
545

546
  write_skip_coeff(cpi, segment_id, m, bc);
547

548
  if (m->mbmi.sb_type >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT)
549
    write_selected_tx_size(cpi, m->mbmi.tx_size, m->mbmi.sb_type, bc);
550

551
  if (m->mbmi.sb_type >= BLOCK_8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
552 553
    const MB_PREDICTION_MODE A = above_block_mode(m, 0, mis);
    const MB_PREDICTION_MODE L = xd->left_available ?
554
                                 left_block_mode(m, 0) : DC_PRED;
555
    write_intra_mode(bc, ym, vp9_kf_y_mode_prob[A][L]);
556
  } else {
557
    int idx, idy;
Jim Bankoski's avatar
Jim Bankoski committed
558 559
    const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[m->mbmi.sb_type];
    const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[m->mbmi.sb_type];
Jim Bankoski's avatar
Jim Bankoski committed
560 561
    for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
      for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
Jim Bankoski's avatar
Jim Bankoski committed
562
        const int i = idy * 2 + idx;
Yaowu Xu's avatar
Yaowu Xu committed
563 564
        const MB_PREDICTION_MODE A = above_block_mode(m, i, mis);
        const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
565
                                     left_block_mode(m, i) : DC_PRED;
566
        const int bm = m->bmi[i].as_mode;
567 568 569
#ifdef ENTROPY_STATS
        ++intra_mode_stats[A][L][bm];
#endif
570
        write_intra_mode(bc, bm, vp9_kf_y_mode_prob[A][L]);
571 572
      }
    }
573 574
  }

575
  write_intra_mode(bc, m->mbmi.uv_mode, vp9_kf_uv_mode_prob[ym]);
576 577
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
578 579
static void write_modes_b(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                          TOKENEXTRA **tok, TOKENEXTRA *tok_end,
580
                          int mi_row, int mi_col) {
581
  VP9_COMMON *const cm = &cpi->common;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
582 583
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;

584
  if (m->mbmi.sb_type < BLOCK_8X8)
585 586
    if (xd->ab_index > 0)
      return;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
587

Ronald S. Bultje's avatar
Ronald S. Bultje committed
588
  xd->mode_info_context = m;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
589 590 591 592 593
  set_mi_row_col(&cpi->common, xd,
                 mi_row, num_8x8_blocks_high_lookup[m->mbmi.sb_type],
                 mi_col, num_8x8_blocks_wide_lookup[m->mbmi.sb_type]);

  if (cm->frame_type == KEY_FRAME || cm->intra_only) {
594
    write_mb_modes_kf(cpi, m, bc);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
595 596 597 598
#ifdef ENTROPY_STATS
    active_section = 8;
#endif
  } else {
599
    pack_inter_mode_mvs(cpi, m, bc);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
600 601 602 603 604 605 606 607 608
#ifdef ENTROPY_STATS
    active_section = 1;
#endif
  }

  assert(*tok < tok_end);
  pack_mb_tokens(bc, tok, tok_end);
}

609 610
static void write_modes_sb(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                           TOKENEXTRA **tok, TOKENEXTRA *tok_end,
611
                           int mi_row, int mi_col, BLOCK_SIZE bsize) {
612
  VP9_COMMON *const cm = &cpi->common;
613
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
614
  const int mis = cm->mode_info_stride;
615 616
  int bsl = b_width_log2(bsize);
  int bs = (1 << bsl) / 4;  // mode_info step for subsize
617
  int n;
618
  PARTITION_TYPE partition = PARTITION_NONE;
619
  BLOCK_SIZE subsize;
620

621
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
622 623
    return;

Jim Bankoski's avatar
Jim Bankoski committed
624
  partition = partition_lookup[bsl][m->mbmi.sb_type];
625

626
  if (bsize < BLOCK_8X8)
627
    if (xd->ab_index > 0)
628 629
      return;

630
  if (bsize >= BLOCK_8X8) {
631
    int pl;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
632 633
    const int idx = check_bsize_coverage(bs, cm->mi_rows, cm->mi_cols,
                                         mi_row, mi_col);
634
    set_partition_seg_context(cm, xd, mi_row, mi_col);
635
    pl = partition_plane_context(xd, bsize);
636
    // encode the partition information
637 638 639 640 641 642 643
    if (idx == 0)
      write_token(bc, vp9_partition_tree,
                  cm->fc.partition_prob[cm->frame_type][pl],
                  vp9_partition_encodings + partition);
    else if (idx > 0)
      vp9_write(bc, partition == PARTITION_SPLIT,
                cm->fc.partition_prob[cm->frame_type][pl][idx]);
644
  }
645

646
  subsize = get_subsize(bsize, partition);
647
  *(get_sb_index(xd, subsize)) = 0;
648

649 650
  switch (partition) {
    case PARTITION_NONE:
651
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
652 653
      break;
    case PARTITION_HORZ:
654
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
655
      *(get_sb_index(xd, subsize)) = 1;
656 657
      if ((mi_row + bs) < cm->mi_rows)
        write_modes_b(cpi, m + bs * mis, bc, tok, tok_end, mi_row + bs, mi_col);
658 659
      break;
    case PARTITION_VERT:
660
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
661
      *(get_sb_index(xd, subsize)) = 1;
662 663
      if ((mi_col + bs) < cm->mi_cols)
        write_modes_b(cpi, m + bs, bc, tok, tok_end, mi_row, mi_col + bs);
664 665 666 667
      break;
    case PARTITION_SPLIT:
      for (n = 0; n < 4; n++) {
        int j = n >> 1, i = n & 0x01;
668
        *(get_sb_index(xd, subsize)) = n;
669
        write_modes_sb(cpi, m + j * bs * mis + i * bs, bc, tok, tok_end,
670
                       mi_row + j * bs, mi_col + i * bs, subsize);
671 672 673 674 675
      }
      break;
    default:
      assert(0);
  }
676 677

  // update partition context
678 679
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) {
680 681 682
    set_partition_seg_context(cm, xd, mi_row, mi_col);
    update_partition_context(xd, subsize, bsize);
  }
683 684
}

685
static void write_modes(VP9_COMP *cpi, vp9_writer* const bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
686
                        TOKENEXTRA **tok, TOKENEXTRA *tok_end) {
687 688 689
  VP9_COMMON *const cm = &cpi->common;
  const int mis = cm->mode_info_stride;
  MODE_INFO *m, *m_ptr = cm->mi;
690
  int mi_row, mi_col;