vp9_bitstream.c 56.9 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
35
36
#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"
#include "vp9/encoder/vp9_write_bit_buffer.h"

Paul Wilkins's avatar
Paul Wilkins committed
37

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

#ifdef ENTROPY_STATS
43
44
45
int intra_mode_stats[VP9_INTRA_MODES]
                    [VP9_INTRA_MODES]
                    [VP9_INTRA_MODES];
46
vp9_coeff_stats tree_update_hist[TX_SIZE_MAX_SB][BLOCK_TYPES];
47

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

51
52
#define vp9_cost_upd  ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)) >> 8)
#define vp9_cost_upd256  ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)))
53
54
55

static int update_bits[255];

56
57
58
59
60
61
62
63
64
65
66
67
static INLINE void write_le16(uint8_t *p, int value) {
  p[0] = value;
  p[1] = value >> 8;
}

static INLINE void write_le32(uint8_t *p, int value) {
  p[0] = value;
  p[1] = value >> 8;
  p[2] = value >> 16;
  p[3] = value >> 24;
}

68
69
70
71
72
73
74
75
76
77
78
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
void vp9_encode_unsigned_max(vp9_writer *br, int data, int max) {
  assert(data <= max);
  while (max) {
    vp9_write_bit(br, data & 1);
    data >>= 1;
    max >>= 1;
  }
}

int recenter_nonneg(int v, int m) {
  if (v > (m << 1))
    return v;
  else if (v >= m)
    return ((v - m) << 1);
  else
    return ((m - v) << 1) - 1;
}

static int get_unsigned_bits(unsigned num_values) {
  int cat = 0;
  if ((num_values--) <= 1) return 0;
  while (num_values > 0) {
    cat++;
    num_values >>= 1;
  }
  return cat;
}

void encode_uniform(vp9_writer *w, int v, int n) {
  int l = get_unsigned_bits(n);
  int m;
  if (l == 0)
    return;
  m = (1 << l) - n;
  if (v < m) {
    vp9_write_literal(w, v, l - 1);
  } else {
    vp9_write_literal(w, m + ((v - m) >> 1), l - 1);
    vp9_write_literal(w, (v - m) & 1, 1);
  }
}

int count_uniform(int v, int n) {
  int l = get_unsigned_bits(n);
  int m;
  if (l == 0) return 0;
  m = (1 << l) - n;
  if (v < m)
    return l - 1;
  else
    return l;
}

void encode_term_subexp(vp9_writer *w, int word, int k, int num_syms) {
  int i = 0;
  int mk = 0;
  while (1) {
    int b = (i ? k + i - 1 : k);
    int a = (1 << b);
    if (num_syms <= mk + 3 * a) {
      encode_uniform(w, word - mk, num_syms - mk);
      break;
    } else {
      int t = (word >= mk + a);
      vp9_write_literal(w, t, 1);
      if (t) {
        i = i + 1;
        mk += a;
      } else {
        vp9_write_literal(w, word - mk, b);
        break;
      }
    }
  }
}

int count_term_subexp(int word, int k, int num_syms) {
  int count = 0;
  int i = 0;
  int mk = 0;
  while (1) {
    int b = (i ? k + i - 1 : k);
    int a = (1 << b);
    if (num_syms <= mk + 3 * a) {
      count += count_uniform(word - mk, num_syms - mk);
      break;
    } else {
      int t = (word >= mk + a);
      count++;
      if (t) {
        i = i + 1;
        mk += a;
      } else {
        count += b;
        break;
      }
    }
  }
  return count;
}

John Koleszar's avatar
John Koleszar committed
169
170
171
static void compute_update_table() {
  int i;
  for (i = 0; i < 255; i++)
172
    update_bits[i] = count_term_subexp(i, SUBEXP_PARAM, 255);
173
174
}

John Koleszar's avatar
John Koleszar committed
175
176
177
178
179
static int split_index(int i, int n, int modulus) {
  int max1 = (n - 1 - modulus / 2) / modulus + 1;
  if (i % modulus == modulus / 2) i = i / modulus;
  else i = max1 + i - (i + modulus - modulus / 2) / modulus;
  return i;
180
181
}

John Koleszar's avatar
John Koleszar committed
182
183
184
185
186
static int remap_prob(int v, int m) {
  const int n = 256;
  const int modulus = MODULUS_PARAM;
  int i;
  if ((m << 1) <= n)
187
    i = recenter_nonneg(v, m) - 1;
John Koleszar's avatar
John Koleszar committed
188
  else
189
    i = recenter_nonneg(n - 1 - v, n - 1 - m) - 1;
John Koleszar's avatar
John Koleszar committed
190
191
192

  i = split_index(i, n - 1, modulus);
  return i;
193
}
194

195
static void write_prob_diff_update(vp9_writer *w,
196
                                   vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
197
  int delp = remap_prob(newp, oldp);
198
  encode_term_subexp(w, delp, SUBEXP_PARAM, 255);
199
200
}

201
static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
202
203
  int delp = remap_prob(newp, oldp);
  return update_bits[delp] * 256;
204
}
205

John Koleszar's avatar
John Koleszar committed
206
static void update_mode(
207
  vp9_writer *w,
John Koleszar's avatar
John Koleszar committed
208
  int n,
209
  const struct vp9_token tok[/* n */],
210
211
212
  vp9_tree tree,
  vp9_prob Pnew               [/* n-1 */],
  vp9_prob Pcur               [/* n-1 */],
John Koleszar's avatar
John Koleszar committed
213
214
215
216
217
218
  unsigned int bct            [/* n-1 */] [2],
  const unsigned int num_events[/* n */]
) {
  unsigned int new_b = 0, old_b = 0;
  int i = 0;

219
220
  vp9_tree_probs_from_distribution(tree, Pnew, bct, num_events, 0);
  n--;
John Koleszar's avatar
John Koleszar committed
221
222

  do {
223
224
    new_b += cost_branch(bct[i], Pnew[i]);
    old_b += cost_branch(bct[i], Pcur[i]);
John Koleszar's avatar
John Koleszar committed
225
226
227
  } while (++i < n);

  if (new_b + (n << 8) < old_b) {
John Koleszar's avatar
John Koleszar committed
228
229
    int i = 0;

230
    vp9_write_bit(w, 1);
John Koleszar's avatar
John Koleszar committed
231

John Koleszar's avatar
John Koleszar committed
232
    do {
233
      const vp9_prob p = Pnew[i];
John Koleszar's avatar
John Koleszar committed
234

235
      vp9_write_literal(w, Pcur[i] = p ? p : 1, 8);
John Koleszar's avatar
John Koleszar committed
236
237
    } while (++i < n);
  } else
238
    vp9_write_bit(w, 0);
John Koleszar's avatar
John Koleszar committed
239
240
}

241
static void update_mbintra_mode_probs(VP9_COMP* const cpi,
242
                                      vp9_writer* const bc) {
243
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
244

245
246
  vp9_prob pnew[VP9_INTRA_MODES - 1];
  unsigned int bct[VP9_INTRA_MODES - 1][2];
247

248
  update_mode(bc, VP9_INTRA_MODES, vp9_intra_mode_encodings,
249
250
              vp9_intra_mode_tree, pnew,
              cm->fc.y_mode_prob, bct, (unsigned int *)cpi->y_mode_count);
John Koleszar's avatar
John Koleszar committed
251
252
}

253
254
void vp9_update_skip_probs(VP9_COMP *cpi) {
  VP9_COMMON *const pc = &cpi->common;
John Koleszar's avatar
John Koleszar committed
255
  int k;
Paul Wilkins's avatar
Paul Wilkins committed
256

257
  for (k = 0; k < MBSKIP_CONTEXTS; ++k)
John Koleszar's avatar
John Koleszar committed
258
259
    pc->mbskip_pred_probs[k] = get_binary_prob(cpi->skip_false_count[k],
                                               cpi->skip_true_count[k]);
Paul Wilkins's avatar
Paul Wilkins committed
260
261
}

262
static void update_switchable_interp_probs(VP9_COMP *cpi,
263
                                           vp9_writer* const bc) {
264
  VP9_COMMON *const pc = &cpi->common;
265
266
  unsigned int branch_ct[32][2];
  int i, j;
267
  for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
268
    vp9_tree_probs_from_distribution(
269
        vp9_switchable_interp_tree,
270
        pc->fc.switchable_interp_prob[j], branch_ct,
271
        cpi->switchable_interp_count[j], 0);
272
    for (i = 0; i < VP9_SWITCHABLE_FILTERS - 1; ++i) {
273
274
      if (pc->fc.switchable_interp_prob[j][i] < 1)
        pc->fc.switchable_interp_prob[j][i] = 1;
275
      vp9_write_prob(bc, pc->fc.switchable_interp_prob[j][i]);
276
277
278
279
    }
  }
}

280
// This function updates the reference frame prediction stats
281
282
static void update_refpred_stats(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
283
  int i;
284
  vp9_prob new_pred_probs[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
285
286
287
  int old_cost, new_cost;

  // Set the prediction probability structures to defaults
288
  if (cm->frame_type != KEY_FRAME) {
John Koleszar's avatar
John Koleszar committed
289
290
    // From the prediction counts set the probabilities for each context
    for (i = 0; i < PREDICTION_PROBS; i++) {
291
292
293
294
      const int c0 = cpi->ref_pred_count[i][0];
      const int c1 = cpi->ref_pred_count[i][1];

      new_pred_probs[i] = get_binary_prob(c0, c1);
John Koleszar's avatar
John Koleszar committed
295
296
297

      // Decide whether or not to update the reference frame probs.
      // Returned costs are in 1/256 bit units.
298
299
      old_cost = c0 * vp9_cost_zero(cm->ref_pred_probs[i]) +
                 c1 * vp9_cost_one(cm->ref_pred_probs[i]);
John Koleszar's avatar
John Koleszar committed
300

301
302
      new_cost = c0 * vp9_cost_zero(new_pred_probs[i]) +
                 c1 * vp9_cost_one(new_pred_probs[i]);
John Koleszar's avatar
John Koleszar committed
303
304
305
306
307
308
309

      // Cost saving must be >= 8 bits (2048 in these units)
      if ((old_cost - new_cost) >= 2048) {
        cpi->ref_pred_probs_update[i] = 1;
        cm->ref_pred_probs[i] = new_pred_probs[i];
      } else
        cpi->ref_pred_probs_update[i] = 0;
310
    }
John Koleszar's avatar
John Koleszar committed
311
  }
312
313
}

314
315
316
317
318
319
// This function is called to update the mode probability context used to encode
// inter modes. It assumes the branch counts table has already been populated
// prior to the actual packing of the bitstream (in rd stage or dummy pack)
//
// The branch counts table is re-populated during the actual pack stage and in
// the decoder to facilitate backwards update of the context.
320
static void update_inter_mode_probs(VP9_COMMON *cm,
321
    int mode_context[INTER_MODE_CONTEXTS][VP9_MVREFS - 1]) {
322
  int i, j;
323
  unsigned int (*mv_ref_ct)[VP9_MVREFS - 1][2] = cm->fc.mv_ref_ct;
324
325
326
327
328

  vpx_memcpy(mode_context, cm->fc.vp9_mode_contexts,
             sizeof(cm->fc.vp9_mode_contexts));

  for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
329
    for (j = 0; j < VP9_MVREFS - 1; j++) {
330
      int new_prob, old_cost, new_cost;
331
332
333

      // Work out cost of coding branches with the old and optimal probability
      old_cost = cost_branch256(mv_ref_ct[i][j], mode_context[i][j]);
334
      new_prob = get_binary_prob(mv_ref_ct[i][j][0], mv_ref_ct[i][j][1]);
335
336
337
338
339
340
341
342
343
344
345
      new_cost = cost_branch256(mv_ref_ct[i][j], new_prob);

      // If cost saving is >= 14 bits then update the mode probability.
      // This is the approximate net cost of updating one probability given
      // that the no update case ismuch more common than the update case.
      if (new_cost <= (old_cost - (14 << 8))) {
        mode_context[i][j] = new_prob;
      }
    }
  }
}
346

347
348
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);
349
350
}

351
static int prob_update_savings(const unsigned int *ct,
352
353
354
355
356
                               const vp9_prob oldp, const vp9_prob newp,
                               const vp9_prob upd) {
  const int old_b = cost_branch256(ct, oldp);
  const int new_b = cost_branch256(ct, newp);
  const int update_b = 2048 + vp9_cost_upd256;
357
  return old_b - new_b - update_b;
358
359
360
}

static int prob_diff_update_savings_search(const unsigned int *ct,
361
362
363
                                           const vp9_prob oldp, vp9_prob *bestp,
                                           const vp9_prob upd) {
  const int old_b = cost_branch256(ct, oldp);
John Koleszar's avatar
John Koleszar committed
364
  int new_b, update_b, savings, bestsavings, step;
365
  vp9_prob newp, bestnewp;
John Koleszar's avatar
John Koleszar committed
366
367
368
369
370
371

  bestsavings = 0;
  bestnewp = oldp;

  step = (*bestp > oldp ? -1 : 1);
  for (newp = *bestp; newp != oldp; newp += step) {
372
373
    new_b = cost_branch256(ct, newp);
    update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
John Koleszar's avatar
John Koleszar committed
374
375
376
377
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
378
    }
John Koleszar's avatar
John Koleszar committed
379
380
381
  }
  *bestp = bestnewp;
  return bestsavings;
382
383
}

384
385
386
387
static int prob_diff_update_savings_search_model(const unsigned int *ct,
                                                 const vp9_prob *oldp,
                                                 vp9_prob *bestp,
                                                 const vp9_prob upd,
388
                                                 int b, int r) {
389
390
  int i, old_b, new_b, update_b, savings, bestsavings, step;
  int newp;
391
  vp9_prob bestnewp, newplist[ENTROPY_NODES], oldplist[ENTROPY_NODES];
392
393
  vp9_model_to_full_probs(oldp, oldplist);
  vpx_memcpy(newplist, oldp, sizeof(vp9_prob) * UNCONSTRAINED_NODES);
394
395
396
  for (i = UNCONSTRAINED_NODES, old_b = 0; i < ENTROPY_NODES; ++i)
    old_b += cost_branch256(ct + 2 * i, oldplist[i]);
  old_b += cost_branch256(ct + 2 * PIVOT_NODE, oldplist[PIVOT_NODE]);
397
398

  bestsavings = 0;
399
  bestnewp = oldp[PIVOT_NODE];
400

401
  step = (*bestp > oldp[PIVOT_NODE] ? -1 : 1);
402
  newp = *bestp;
403
  for (; newp != oldp[PIVOT_NODE]; newp += step) {
404
    if (newp < 1 || newp > 255) continue;
405
    newplist[PIVOT_NODE] = newp;
406
    vp9_model_to_full_probs(newplist, newplist);
407
    for (i = UNCONSTRAINED_NODES, new_b = 0; i < ENTROPY_NODES; ++i)
408
      new_b += cost_branch256(ct + 2 * i, newplist[i]);
409
410
    new_b += cost_branch256(ct + 2 * PIVOT_NODE, newplist[PIVOT_NODE]);
    update_b = prob_diff_update_cost(newp, oldp[PIVOT_NODE]) +
411
412
413
414
415
416
417
418
419
420
421
        vp9_cost_upd256;
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
    }
  }
  *bestp = bestnewp;
  return bestsavings;
}

422
423
424
425
426
427
428
429
static void vp9_cond_prob_update(vp9_writer *bc, vp9_prob *oldp, vp9_prob upd,
                                 unsigned int *ct) {
  vp9_prob newp;
  int savings;
  newp = get_binary_prob(ct[0], ct[1]);
  savings = prob_update_savings(ct, *oldp, newp, upd);
  if (savings > 0) {
    vp9_write(bc, 1, upd);
430
    vp9_write_prob(bc, newp);
431
432
433
434
435
436
    *oldp = newp;
  } else {
    vp9_write(bc, 0, upd);
  }
}

437
static void pack_mb_tokens(vp9_writer* const bc,
438
439
440
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
441

John Koleszar's avatar
John Koleszar committed
442
  while (p < stop) {
443
    const int t = p->token;
444
    const struct vp9_token *const a = vp9_coef_encodings + t;
445
    const vp9_extra_bit *const b = vp9_extra_bits + t;
John Koleszar's avatar
John Koleszar committed
446
    int i = 0;
447
    const vp9_prob *pp;
John Koleszar's avatar
John Koleszar committed
448
    int v = a->value;
449
    int n = a->len;
450
    vp9_prob probs[ENTROPY_NODES];
John Koleszar's avatar
John Koleszar committed
451

452
    if (t == EOSB_TOKEN) {
453
454
455
      ++p;
      break;
    }
456
    if (t >= TWO_TOKEN) {
457
      vp9_model_to_full_probs(p->context_tree, probs);
458
459
460
461
      pp = probs;
    } else {
      pp = p->context_tree;
    }
462
    assert(pp != 0);
463

John Koleszar's avatar
John Koleszar committed
464
    /* skip one or two nodes */
465
#if !CONFIG_BALANCED_COEFTREE
John Koleszar's avatar
John Koleszar committed
466
467
468
469
    if (p->skip_eob_node) {
      n -= p->skip_eob_node;
      i = 2 * p->skip_eob_node;
    }
470
#endif
John Koleszar's avatar
John Koleszar committed
471

John Koleszar's avatar
John Koleszar committed
472
473
    do {
      const int bb = (v >> --n) & 1;
474
475
476
477
478
479
480
#if CONFIG_BALANCED_COEFTREE
      if (i == 2 && p->skip_eob_node) {
        i += 2;
        assert(bb == 1);
        continue;
      }
#endif
481
      vp9_write(bc, bb, pp[i >> 1]);
482
      i = vp9_coef_tree[i + bb];
483
    } while (n);
John Koleszar's avatar
John Koleszar committed
484

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

488
      if (l) {
489
        const unsigned char *pb = b->prob;
John Koleszar's avatar
John Koleszar committed
490
        int v = e >> 1;
491
        int n = l;              /* number of bits in v, assumed nonzero */
John Koleszar's avatar
John Koleszar committed
492
        int i = 0;
John Koleszar's avatar
John Koleszar committed
493

John Koleszar's avatar
John Koleszar committed
494
495
        do {
          const int bb = (v >> --n) & 1;
496
          vp9_write(bc, bb, pb[i >> 1]);
John Koleszar's avatar
John Koleszar committed
497
498
499
          i = b->tree[i + bb];
        } while (n);
      }
John Koleszar's avatar
John Koleszar committed
500

501
      vp9_write_bit(bc, e & 1);
John Koleszar's avatar
John Koleszar committed
502
    }
John Koleszar's avatar
John Koleszar committed
503
504
505
    ++p;
  }

506
  *tp = p;
John Koleszar's avatar
John Koleszar committed
507
508
}

509
510
static void write_sb_mv_ref(vp9_writer *bc, MB_PREDICTION_MODE m,
                            const vp9_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
511
#if CONFIG_DEBUG
Ronald S. Bultje's avatar
Ronald S. Bultje committed
512
  assert(NEARESTMV <= m && m <= NEWMV);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
513
#endif
514
515
  write_token(bc, vp9_sb_mv_ref_tree, p,
              vp9_sb_mv_ref_encoding_array - NEARESTMV + m);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
516
517
}

Paul Wilkins's avatar
Paul Wilkins committed
518
519
// This function writes the current macro block's segnment id to the bitstream
// It should only be called if a segment map update is indicated.
520
static void write_mb_segid(vp9_writer *bc,
Paul Wilkins's avatar
Paul Wilkins committed
521
                           const MB_MODE_INFO *mi, const MACROBLOCKD *xd) {
522
523
524
  if (xd->segmentation_enabled && xd->update_mb_segmentation_map)
    treed_write(bc, vp9_segment_tree, xd->mb_segment_tree_probs,
                mi->segment_id, 3);
John Koleszar's avatar
John Koleszar committed
525
526
}

Paul Wilkins's avatar
Paul Wilkins committed
527
// This function encodes the reference frame
528
static void encode_ref_frame(vp9_writer *const bc,
529
                             VP9_COMMON *const cm,
John Koleszar's avatar
John Koleszar committed
530
531
532
533
534
                             MACROBLOCKD *xd,
                             int segment_id,
                             MV_REFERENCE_FRAME rf) {
  int seg_ref_active;
  int seg_ref_count = 0;
535
536
537
  seg_ref_active = vp9_segfeature_active(xd,
                                         segment_id,
                                         SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
538
539

  if (seg_ref_active) {
540
541
542
543
    seg_ref_count = vp9_check_segref(xd, segment_id, INTRA_FRAME) +
                    vp9_check_segref(xd, segment_id, LAST_FRAME) +
                    vp9_check_segref(xd, segment_id, GOLDEN_FRAME) +
                    vp9_check_segref(xd, segment_id, ALTREF_FRAME);
John Koleszar's avatar
John Koleszar committed
544
545
546
547
548
549
550
  }

  // If segment level coding of this signal is disabled...
  // or the segment allows multiple reference frame options
  if (!seg_ref_active || (seg_ref_count > 1)) {
    // Values used in prediction model coding
    unsigned char prediction_flag;
551
    vp9_prob pred_prob;
John Koleszar's avatar
John Koleszar committed
552
553
554
    MV_REFERENCE_FRAME pred_rf;

    // Get the context probability the prediction flag
Paul Wilkins's avatar
Paul Wilkins committed
555
    pred_prob = vp9_get_pred_prob(cm, xd, PRED_REF);
John Koleszar's avatar
John Koleszar committed
556
557

    // Get the predicted value.
Paul Wilkins's avatar
Paul Wilkins committed
558
    pred_rf = vp9_get_pred_ref(cm, xd);
John Koleszar's avatar
John Koleszar committed
559
560
561
562
563

    // Did the chosen reference frame match its predicted value.
    prediction_flag =
      (xd->mode_info_context->mbmi.ref_frame == pred_rf);

Paul Wilkins's avatar
Paul Wilkins committed
564
    vp9_set_pred_flag(xd, PRED_REF, prediction_flag);
565
    vp9_write(bc, prediction_flag, pred_prob);
John Koleszar's avatar
John Koleszar committed
566
567
568

    // If not predicted correctly then code value explicitly
    if (!prediction_flag) {
569
      vp9_prob mod_refprobs[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
570
571
572
573
574
575
576
577

      vpx_memcpy(mod_refprobs,
                 cm->mod_refprobs[pred_rf], 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] *=
578
          vp9_check_segref(xd, segment_id, INTRA_FRAME);
John Koleszar's avatar
John Koleszar committed
579
        mod_refprobs[LAST_FRAME] *=
580
          vp9_check_segref(xd, segment_id, LAST_FRAME);
John Koleszar's avatar
John Koleszar committed
581
        mod_refprobs[GOLDEN_FRAME] *=
582
583
          (vp9_check_segref(xd, segment_id, GOLDEN_FRAME) *
           vp9_check_segref(xd, segment_id, ALTREF_FRAME));
John Koleszar's avatar
John Koleszar committed
584
585
586
      }

      if (mod_refprobs[0]) {
587
        vp9_write(bc, (rf != INTRA_FRAME), mod_refprobs[0]);
John Koleszar's avatar
John Koleszar committed
588
589
590
591
592
      }

      // Inter coded
      if (rf != INTRA_FRAME) {
        if (mod_refprobs[1]) {
593
          vp9_write(bc, (rf != LAST_FRAME), mod_refprobs[1]);
John Koleszar's avatar
John Koleszar committed
594
        }
595

John Koleszar's avatar
John Koleszar committed
596
597
        if (rf != LAST_FRAME) {
          if (mod_refprobs[2]) {
598
            vp9_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]);
John Koleszar's avatar
John Koleszar committed
599
          }
600
        }
John Koleszar's avatar
John Koleszar committed
601
      }
Paul Wilkins's avatar
Paul Wilkins committed
602
    }
John Koleszar's avatar
John Koleszar committed
603
  }
Paul Wilkins's avatar
Paul Wilkins committed
604

John Koleszar's avatar
John Koleszar committed
605
606
  // 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
607
}
John Koleszar's avatar
John Koleszar committed
608

609
// Update the probabilities used to encode reference frame data
610
611
static void update_ref_probs(VP9_COMP *const cpi) {
  VP9_COMMON *const cm = &cpi->common;
612

John Koleszar's avatar
John Koleszar committed
613
614
615
616
  const int *const rfct = cpi->count_mb_ref_frame_usage;
  const int rf_intra = rfct[INTRA_FRAME];
  const int rf_inter = rfct[LAST_FRAME] +
                       rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
617

John Koleszar's avatar
John Koleszar committed
618
619
620
  cm->prob_intra_coded = get_binary_prob(rf_intra, rf_inter);
  cm->prob_last_coded = get_prob(rfct[LAST_FRAME], rf_inter);
  cm->prob_gf_coded = get_binary_prob(rfct[GOLDEN_FRAME], rfct[ALTREF_FRAME]);
621

John Koleszar's avatar
John Koleszar committed
622
623
  // Compute a modified set of probabilities to use when prediction of the
  // reference frame fails
Paul Wilkins's avatar
Paul Wilkins committed
624
  vp9_compute_mod_refprobs(cm);
625
626
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
627
static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m,
628
                                vp9_writer *bc, int mi_row, int mi_col) {
629
  VP9_COMMON *const pc = &cpi->common;
630
  const nmv_context *nmvc = &pc->fc.nmvc;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
631
632
633
634
635
636
637
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *const mi = &m->mbmi;
  const MV_REFERENCE_FRAME rf = mi->ref_frame;
  const MB_PREDICTION_MODE mode = mi->mode;
  const int segment_id = mi->segment_id;
  int skip_coeff;
Adrian Grange's avatar
Adrian Grange committed
638

Ronald S. Bultje's avatar
Ronald S. Bultje committed
639
640
  xd->prev_mode_info_context = pc->prev_mi + (m - pc->mi);
  x->partition_info = x->pi + (m - pc->mi);
641

Ronald S. Bultje's avatar
Ronald S. Bultje committed
642
643
#ifdef ENTROPY_STATS
  active_section = 9;
644
#endif
645

Ronald S. Bultje's avatar
Ronald S. Bultje committed
646
647
648
649
650
  if (cpi->mb.e_mbd.update_mb_segmentation_map) {
    // Is temporal coding of the segment map enabled
    if (pc->temporal_update) {
      unsigned char prediction_flag = vp9_get_pred_flag(xd, PRED_SEG_ID);
      vp9_prob pred_prob = vp9_get_pred_prob(pc, xd, PRED_SEG_ID);
Paul Wilkins's avatar
Paul Wilkins committed
651

Ronald S. Bultje's avatar
Ronald S. Bultje committed
652
653
      // Code the segment id prediction flag for this mb
      vp9_write(bc, prediction_flag, pred_prob);
654

Ronald S. Bultje's avatar
Ronald S. Bultje committed
655
656
      // If the mb segment id wasn't predicted code explicitly
      if (!prediction_flag)
Paul Wilkins's avatar
Paul Wilkins committed
657
        write_mb_segid(bc, mi, &cpi->mb.e_mbd);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
658
659
660
661
662
    } else {
      // Normal unpredicted coding
      write_mb_segid(bc, mi, &cpi->mb.e_mbd);
    }
  }
663

664
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
665
666
    skip_coeff = 1;
  } else {
667
    skip_coeff = m->mbmi.mb_skip_coeff;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
668
669
670
    vp9_write(bc, skip_coeff,
              vp9_get_pred_prob(pc, xd, PRED_MBSKIP));
  }
John Koleszar's avatar
John Koleszar committed
671

Ronald S. Bultje's avatar
Ronald S. Bultje committed
672
  // Encode the reference frame.
Paul Wilkins's avatar
Paul Wilkins committed
673
  encode_ref_frame(bc, pc, xd, segment_id, rf);
Paul Wilkins's avatar
Paul Wilkins committed
674

675
676
677
678
679
680
681
682
683
684
685
686
687
  if (mi->sb_type >= BLOCK_SIZE_SB8X8 && pc->txfm_mode == TX_MODE_SELECT &&
      !(rf != INTRA_FRAME &&
        (skip_coeff || vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)))) {
    TX_SIZE sz = mi->txfm_size;
    // FIXME(rbultje) code ternary symbol once all experiments are merged
    vp9_write(bc, sz != TX_4X4, pc->prob_tx[0]);
    if (mi->sb_type >= BLOCK_SIZE_MB16X16 && sz != TX_4X4) {
      vp9_write(bc, sz != TX_8X8, pc->prob_tx[1]);
      if (mi->sb_type >= BLOCK_SIZE_SB32X32 && sz != TX_8X8)
        vp9_write(bc, sz != TX_16X16, pc->prob_tx[2]);
    }
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
688
  if (rf == INTRA_FRAME) {
689
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
690
    active_section = 6;
691
#endif
Paul Wilkins's avatar
Paul Wilkins committed
692

693
694
695
    if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8) {
      write_intra_mode(bc, mode, pc->fc.y_mode_prob);
    } else {
696
697
698
699
700
      int idx, idy;
      int bw = 1 << b_width_log2(mi->sb_type);
      int bh = 1 << b_height_log2(mi->sb_type);
      for (idy = 0; idy < 2; idy += bh)
        for (idx = 0; idx < 2; idx += bw)
701
702
          write_intra_mode(bc, m->bmi[idy * 2 + idx].as_mode.first,
                           pc->fc.y_mode_prob);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
703
    }
704
705
    write_intra_mode(bc, mi->uv_mode,
                     pc->fc.uv_mode_prob[mode]);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
706
707
  } else {
    vp9_prob mv_ref_p[VP9_MVREFS - 1];
708

Ronald S. Bultje's avatar
Ronald S. Bultje committed
709
    vp9_mv_ref_probs(&cpi->common, mv_ref_p, mi->mb_mode_context[rf]);
Yaowu Xu's avatar
Yaowu Xu committed
710

John Koleszar's avatar
John Koleszar committed
711
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
712
    active_section = 3;
John Koleszar's avatar
John Koleszar committed
713
714
#endif

715
    // If segment skip is not enabled code the mode.
Paul Wilkins's avatar
Paul Wilkins committed
716
    if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
717
      if (mi->sb_type >= BLOCK_SIZE_SB8X8) {
718
        write_sb_mv_ref(bc, mode, mv_ref_p);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
719
720
        vp9_accum_mv_refs(&cpi->common, mode, mi->mb_mode_context[rf]);
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
721
    }
722

Ronald S. Bultje's avatar
Ronald S. Bultje committed
723
724
725
726
727
728
729
730
    if (cpi->common.mcomp_filter_type == SWITCHABLE) {
      write_token(bc, vp9_switchable_interp_tree,
                  vp9_get_pred_probs(&cpi->common, xd,
                                     PRED_SWITCHABLE_INTERP),
                  vp9_switchable_interp_encodings +
                  vp9_switchable_interp_map[mi->interp_filter]);
    } else {
      assert(mi->interp_filter == cpi->common.mcomp_filter_type);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
731
    }
732

Ronald S. Bultje's avatar
Ronald S. Bultje committed
733
734
735
736
737
738
    // does the feature use compound prediction or not
    // (if not specified at the frame/segment level)
    if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) {
      vp9_write(bc, mi->second_ref_frame > INTRA_FRAME,
                vp9_get_pred_prob(pc, xd, PRED_COMP));
    }
John Koleszar's avatar
John Koleszar committed
739

Ronald S. Bultje's avatar
Ronald S. Bultje committed
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
    if (xd->mode_info_context->mbmi.sb_type < BLOCK_SIZE_SB8X8) {
      int j;
      MB_PREDICTION_MODE blockmode;
      int_mv blockmv;
      int bwl = b_width_log2(mi->sb_type), bw = 1 << bwl;
      int bhl = b_height_log2(mi->sb_type), bh = 1 << bhl;
      int idx, idy;
      for (idy = 0; idy < 2; idy += bh) {
        for (idx = 0; idx < 2; idx += bw) {
          j = idy * 2 + idx;
          blockmode = cpi->mb.partition_info->bmi[j].mode;
          blockmv = cpi->mb.partition_info->bmi[j].mv;
          write_sb_mv_ref(bc, blockmode, mv_ref_p);
          vp9_accum_mv_refs(&cpi->common, blockmode, mi->mb_mode_context[rf]);
          if (blockmode == NEWMV) {
755
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
756
            active_section = 11;
757
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
758
759
            vp9_encode_mv(bc, &blockmv.as_mv, &mi->best_mv.as_mv,
                          nmvc, xd->allow_high_precision_mv);
760

Ronald S. Bultje's avatar
Ronald S. Bultje committed
761
762
763
764
            if (mi->second_ref_frame > 0)
              vp9_encode_mv(bc,
                            &cpi->mb.partition_info->bmi[j].second_mv.as_mv,
                            &mi->best_second_mv.as_mv,
765
                            nmvc, xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
766
          }
767
        }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
768
      }
769
770

#ifdef MODE_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
771
      ++count_mb_seg[mi->partitioning];
772
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
773
774
775
776
777
778
779
780
781
782
783
784
    } else if (mode == NEWMV) {
#ifdef ENTROPY_STATS
      active_section = 5;
#endif
      vp9_encode_mv(bc,
                    &mi->mv[0].as_mv, &mi->best_mv.as_mv,
                    nmvc, xd->allow_high_precision_mv);

      if (mi->second_ref_frame > 0)
        vp9_encode_mv(bc,
                      &mi->mv[1].as_mv, &mi->best_second_mv.as_mv,
                      nmvc, xd->allow_high_precision_mv);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
785
786
    }
  }
John Koleszar's avatar
John Koleszar committed
787
}
788

Ronald S. Bultje's avatar
Ronald S. Bultje committed
789
static void write_mb_modes_kf(const VP9_COMP *cpi,
790
                              MODE_INFO *m,
791
                              vp9_writer *bc, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
792
793
794
  const VP9_COMMON *const c = &cpi->common;
  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  const int ym = m->mbmi.mode;
795
  const int mis = c->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
796
797
  const int segment_id = m->mbmi.segment_id;
  int skip_coeff;
798

799
  if (xd->update_mb_segmentation_map)
800
801
    write_mb_segid(bc, &m->mbmi, xd);

802
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
803
804
    skip_coeff = 1;
  } else {
805
    skip_coeff = m->mbmi.mb_skip_coeff;
806
    vp9_write(bc, skip_coeff, vp9_get_pred_prob(c, xd, PRED_MBSKIP));
807
808
  }

809
810
811
812
813
814
815
816
817
818
819
  if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8 && c->txfm_mode == TX_MODE_SELECT) {
    TX_SIZE sz = m->mbmi.txfm_size;
    // FIXME(rbultje) code ternary symbol once all experiments are merged
    vp9_write(bc, sz != TX_4X4, c->prob_tx[0]);
    if (m->mbmi.sb_type >= BLOCK_SIZE_MB16X16 && sz != TX_4X4) {
      vp9_write(bc, sz != TX_8X8, c->prob_tx[1]);
      if (m->mbmi.sb_type >= BLOCK_SIZE_SB32X32 && sz != TX_8X8)
        vp9_write(bc, sz != TX_16X16, c->prob_tx[2]);
    }
  }

820
  if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
821
822
    const MB_PREDICTION_MODE A = above_block_mode(m, 0, mis);
    const MB_PREDICTION_MODE L = xd->left_available ?
823
                                 left_block_mode(m, 0) : DC_PRED;
824
825
    write_intra_mode(bc, ym, c->kf_y_mode_prob[A][L]);
  } else {
826
827
828
    int idx, idy;
    int bw = 1 << b_width_log2(m->mbmi.sb_type);
    int bh = 1 << b_height_log2(m->mbmi.sb_type);
829
830
831
    for (idy = 0; idy < 2; idy += bh) {
      for (idx = 0; idx < 2; idx += bw) {
        int i = idy * 2 + idx;
Yaowu Xu's avatar
Yaowu Xu committed
832
833
        const MB_PREDICTION_MODE A = above_block_mode(m, i, mis);
        const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
834
                                     left_block_mode(m, i) : DC_PRED;
835
836
837
838
        const int bm = m->bmi[i].as_mode.first;
#ifdef ENTROPY_STATS
        ++intra_mode_stats[A][L][bm];
#endif
839
        write_intra_mode(bc, bm, c->kf_y_mode_prob[A][L]);
840
841
      }
    }
842
843
  }

844
  write_intra_mode(bc, m->mbmi.uv_mode, c->kf_uv_mode_prob[ym]);
845
846
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
847
848
static void write_modes_b(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                          TOKENEXTRA **tok, TOKENEXTRA *tok_end,
849
                          int mi_row, int mi_col) {
850
  VP9_COMMON *const cm = &cpi->common;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
851
852
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;

853
854
855
  if (m->mbmi.sb_type < BLOCK_SIZE_SB8X8)
    if (xd->ab_index > 0)
      return;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
856
  xd->mode_info_context = m;
857
858
859
  set_mi_row_col(&cpi->common, xd, mi_row,
                 1 << mi_height_log2(m->mbmi.sb_type),
                 mi_col, 1 << mi_width_log2(m->mbmi.sb_type));
860
  if (cm->frame_type == KEY_FRAME) {
861
    write_mb_modes_kf(cpi, m, bc, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
862
863
864
865
#ifdef ENTROPY_STATS
    active_section = 8;
#endif
  } else {
866
    pack_inter_mode_mvs(cpi, m, bc, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
867
868
869
870
871
872
873
874
875
#ifdef ENTROPY_STATS
    active_section = 1;
#endif
  }

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

876
877
static void write_modes_sb(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                           TOKENEXTRA **tok, TOKENEXTRA *tok_end,
878
                           int mi_row, int mi_col,
879
880
                           BLOCK_SIZE_TYPE bsize) {
  VP9_COMMON *const cm = &cpi->common;
881
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
882
883
  const int mis = cm->mode_info_stride;
  int bwl, bhl;
884
885
  int bsl = b_width_log2(bsize);
  int bs = (1 << bsl) / 4;  // mode_info step for subsize
886
887
888
889
  int n;
  PARTITION_TYPE partition;
  BLOCK_SIZE_TYPE subsize;

890
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
891
892
    return;

893
894
  bwl = b_width_log2(m->mbmi.sb_type);
  bhl = b_height_log2(m->mbmi.sb_type);
895
896
897
898
899
900
901
902
903
904
905
906
907

  // parse the partition type
  if ((bwl == bsl) && (bhl == bsl))
    partition = PARTITION_NONE;
  else if ((bwl == bsl) && (bhl < bsl))
    partition = PARTITION_HORZ;
  else if ((bwl < bsl) && (bhl == bsl))
    partition = PARTITION_VERT;
  else if ((bwl < bsl) && (bhl < bsl))
    partition = PARTITION_SPLIT;
  else
    assert(0);

908
  if (bsize < BLOCK_SIZE_SB8X8)
909
    if (xd->ab_index > 0)
910
911
912
      return;

  if (bsize >= BLOCK_SIZE_SB8X8) {
913
    int pl;
914
915
    xd->left_seg_context = cm->left_seg_context + (mi_row & MI_MASK);
    xd->above_seg_context = cm->above_seg_context + mi_col;
916
    pl = partition_plane_context(xd, bsize);
917
    // encode the partition information
918
    write_token(bc, vp9_partition_tree, cm->fc.partition_prob[pl],
919
                vp9_partition_encodings + partition);
920
  }
921

922
  subsize = get_subsize(bsize, partition);
923
  *(get_sb_index(xd, subsize)) = 0;
924

925
926
  switch (partition) {
    case PARTITION_NONE:
927
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
928
929
      break;
    case PARTITION_HORZ:
930
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
931
      *(get_sb_index(xd, subsize)) = 1;
932
933
      if ((mi_row + bs) < cm->mi_rows)
        write_modes_b(cpi, m + bs * mis, bc, tok, tok_end, mi_row + bs, mi_col);
934
935
      break;
    case PARTITION_VERT:
936
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
937
      *(get_sb_index(xd, subsize)) = 1;
938
939
      if ((mi_col + bs) < cm->mi_cols)
        write_modes_b(cpi, m + bs, bc, tok, tok_end, mi_row, mi_col + bs);
940
941
942
943
      break;
    case PARTITION_SPLIT:
      for (n = 0; n < 4; n++) {
        int j = n >> 1, i = n & 0x01;
944
        *(get_sb_index(xd, subsize)) = n;
945
        write_modes_sb(cpi, m + j * bs * mis + i * bs, bc, tok, tok_end,
946
                       mi_row + j * bs, mi_col + i * bs, subsize);
947
948
949
950
951
      }
      break;
    default:
      assert(0);
  }
952
953

  // update partition context
954
955
  if (bsize >= BLOCK_SIZE_SB8X8 &&
      (bsize == BLOCK_SIZE_SB8X8 || partition != PARTITION_SPLIT)) {
956
957
958
    set_partition_seg_context(cm, xd, mi_row, mi_col);
    update_partition_context(xd, subsize, bsize);
  }
959
960
}

961
static void write_modes(VP9_COMP *cpi, vp9_writer* const bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
962
                        TOKENEXTRA **tok, TOKENEXTRA *tok_end) {
963
  VP9_COMMON *const c = &cpi->common;
John Koleszar's avatar
John Koleszar committed
964
  const int mis = c->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
965
  MODE_INFO *m, *m_ptr = c->mi;
966
  int mi_row, mi_col;
John Koleszar's avatar
John Koleszar committed
967

968
  m_ptr += c->cur_tile_mi_col_start + c->cur_tile_mi_row_start * mis;
969
  vpx_memset(c->above_seg_context, 0, sizeof(PARTITION_CONTEXT) *
970
             mi_cols_aligned_to_sb(c));
971

Ronald S. Bultje's avatar