vp9_bitstream.c 63.4 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
#include "vp9/common/vp9_header.h"
16
#include "vp9/encoder/vp9_encodemv.h"
17
#include "vp9/common/vp9_entropymode.h"
18
#include "vp9/common/vp9_entropymv.h"
19
#include "vp9/common/vp9_findnearmv.h"
20
#include "vp9/common/vp9_tile_common.h"
21
#include "vp9/encoder/vp9_mcomp.h"
22
23
#include "vp9/common/vp9_systemdependent.h"
#include "vp9/common/vp9_pragmas.h"
24
#include "vpx/vpx_encoder.h"
John Koleszar's avatar
John Koleszar committed
25
#include "vpx_mem/vpx_mem.h"
26
27
#include "vp9/encoder/vp9_bitstream.h"
#include "vp9/encoder/vp9_segmentation.h"
28

29
30
31
32
33
34
#include "vp9/common/vp9_seg_common.h"
#include "vp9/common/vp9_pred_common.h"
#include "vp9/common/vp9_entropy.h"
#include "vp9/encoder/vp9_encodemv.h"
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_mvref_common.h"
35
#include "vp9/common/vp9_treecoder.h"
Paul Wilkins's avatar
Paul Wilkins committed
36

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

#ifdef ENTROPY_STATS
42
43
44
int intra_mode_stats[VP9_KF_BINTRAMODES]
                    [VP9_KF_BINTRAMODES]
                    [VP9_KF_BINTRAMODES];
45
46
47
vp9_coeff_stats tree_update_hist_4x4[BLOCK_TYPES];
vp9_coeff_stats tree_update_hist_8x8[BLOCK_TYPES];
vp9_coeff_stats tree_update_hist_16x16[BLOCK_TYPES];
48
vp9_coeff_stats tree_update_hist_32x32[BLOCK_TYPES];
49

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

53
54
#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)))
55
56
57

static int update_bits[255];

58
59
60
61
62
63
64
65
66
67
68
69
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;
}

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
169
170
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
171
172
173
static void compute_update_table() {
  int i;
  for (i = 0; i < 255; i++)
174
    update_bits[i] = count_term_subexp(i, SUBEXP_PARAM, 255);
175
176
}

John Koleszar's avatar
John Koleszar committed
177
178
179
180
181
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;
182
183
}

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

  i = split_index(i, n - 1, modulus);
  return i;
195
}
196

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

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

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

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

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

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

232
    vp9_write_bit(w, 1);
John Koleszar's avatar
John Koleszar committed
233

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

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

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

247
248
249
250
251
252
253
254
255
  vp9_prob pnew[VP9_YMODES - 1];
  unsigned int bct[VP9_YMODES - 1][2];

  update_mode(bc, VP9_YMODES, vp9_ymode_encodings, vp9_ymode_tree, pnew,
              cm->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count);

  update_mode(bc, VP9_I32X32_MODES, vp9_sb_ymode_encodings,
              vp9_sb_ymode_tree, pnew, cm->fc.sb_ymode_prob, bct,
              (unsigned int *)cpi->sb_ymode_count);
John Koleszar's avatar
John Koleszar committed
256
257
}

258
259
void vp9_update_skip_probs(VP9_COMP *cpi) {
  VP9_COMMON *const pc = &cpi->common;
John Koleszar's avatar
John Koleszar committed
260
  int k;
Paul Wilkins's avatar
Paul Wilkins committed
261

262
  for (k = 0; k < MBSKIP_CONTEXTS; ++k)
John Koleszar's avatar
John Koleszar committed
263
264
    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
265
266
}

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

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

  // Set the prediction probability structures to defaults
293
  if (cm->frame_type != KEY_FRAME) {
John Koleszar's avatar
John Koleszar committed
294
295
    // From the prediction counts set the probabilities for each context
    for (i = 0; i < PREDICTION_PROBS; i++) {
296
297
298
299
      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
300
301
302

      // Decide whether or not to update the reference frame probs.
      // Returned costs are in 1/256 bit units.
303
304
      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
305

306
307
      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
308
309
310
311
312
313
314

      // 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;
315
    }
John Koleszar's avatar
John Koleszar committed
316
  }
317
318
}

319
320
321
322
323
324
// 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.
325
326
static void update_inter_mode_probs(VP9_COMMON *cm,
                                    int mode_context[INTER_MODE_CONTEXTS][4]) {
327
  int i, j;
328
  unsigned int (*mv_ref_ct)[4][2] = cm->fc.mv_ref_ct;
329
330
331
332
333
334

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

  for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
    for (j = 0; j < 4; j++) {
335
      int new_prob, old_cost, new_cost;
336
337
338

      // 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]);
339
      new_prob = get_binary_prob(mv_ref_ct[i][j][0], mv_ref_ct[i][j][1]);
340
341
342
343
344
345
346
347
348
349
350
      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;
      }
    }
  }
}
351

352
353
static void write_ymode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_ymode_tree, p, vp9_ymode_encodings + m);
John Koleszar's avatar
John Koleszar committed
354
355
}

356
357
static void kfwrite_ymode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_kf_ymode_tree, p, vp9_kf_ymode_encodings + m);
John Koleszar's avatar
John Koleszar committed
358
359
}

360
361
362
363
static void write_sb_ymode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_sb_ymode_tree, p, vp9_sb_ymode_encodings + m);
}

364
365
static void sb_kfwrite_ymode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_uv_mode_tree, p, vp9_sb_kf_ymode_encodings + m);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
366
367
}

368
369
static void write_uv_mode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_uv_mode_tree, p, vp9_uv_mode_encodings + m);
John Koleszar's avatar
John Koleszar committed
370
371
}

372
373
374
375
static void write_kf_bmode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_kf_bmode_tree, p, vp9_kf_bmode_encodings + m);
}

376
static int prob_update_savings(const unsigned int *ct,
377
378
379
380
381
                               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;
382
  return old_b - new_b - update_b;
383
384
385
}

static int prob_diff_update_savings_search(const unsigned int *ct,
386
387
388
                                           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
389
  int new_b, update_b, savings, bestsavings, step;
390
  vp9_prob newp, bestnewp;
John Koleszar's avatar
John Koleszar committed
391
392
393
394
395
396

  bestsavings = 0;
  bestnewp = oldp;

  step = (*bestp > oldp ? -1 : 1);
  for (newp = *bestp; newp != oldp; newp += step) {
397
398
    new_b = cost_branch256(ct, newp);
    update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
John Koleszar's avatar
John Koleszar committed
399
400
401
402
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
403
    }
John Koleszar's avatar
John Koleszar committed
404
405
406
  }
  *bestp = bestnewp;
  return bestsavings;
407
408
}

409
410
411
412
static int prob_diff_update_savings_search_model(const unsigned int *ct,
                                                 const vp9_prob *oldp,
                                                 vp9_prob *bestp,
                                                 const vp9_prob upd,
413
                                                 int b, int r) {
414
415
  int i, old_b, new_b, update_b, savings, bestsavings, step;
  int newp;
416
  vp9_prob bestnewp, newplist[ENTROPY_NODES], oldplist[ENTROPY_NODES];
417
418
  vp9_model_to_full_probs(oldp, oldplist);
  vpx_memcpy(newplist, oldp, sizeof(vp9_prob) * UNCONSTRAINED_NODES);
419
420
421
  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]);
422
423

  bestsavings = 0;
424
  bestnewp = oldp[PIVOT_NODE];
425

426
  step = (*bestp > oldp[PIVOT_NODE] ? -1 : 1);
427
  newp = *bestp;
428
  for (; newp != oldp[PIVOT_NODE]; newp += step) {
429
    if (newp < 1 || newp > 255) continue;
430
    newplist[PIVOT_NODE] = newp;
431
    vp9_model_to_full_probs(newplist, newplist);
432
    for (i = UNCONSTRAINED_NODES, new_b = 0; i < ENTROPY_NODES; ++i)
433
      new_b += cost_branch256(ct + 2 * i, newplist[i]);
434
435
    new_b += cost_branch256(ct + 2 * PIVOT_NODE, newplist[PIVOT_NODE]);
    update_b = prob_diff_update_cost(newp, oldp[PIVOT_NODE]) +
436
437
438
439
440
441
442
443
444
445
446
        vp9_cost_upd256;
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
    }
  }
  *bestp = bestnewp;
  return bestsavings;
}

447
448
449
450
451
452
453
454
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);
455
    vp9_write_prob(bc, newp);
456
457
458
459
460
461
    *oldp = newp;
  } else {
    vp9_write(bc, 0, upd);
  }
}

462
static void pack_mb_tokens(vp9_writer* const bc,
463
464
465
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
466

John Koleszar's avatar
John Koleszar committed
467
  while (p < stop) {
468
    const int t = p->token;
469
    const struct vp9_token *const a = vp9_coef_encodings + t;
470
    const vp9_extra_bit *const b = vp9_extra_bits + t;
John Koleszar's avatar
John Koleszar committed
471
    int i = 0;
472
    const vp9_prob *pp;
John Koleszar's avatar
John Koleszar committed
473
    int v = a->value;
474
    int n = a->len;
475
    int ncount = n;
476
    vp9_prob probs[ENTROPY_NODES];
John Koleszar's avatar
John Koleszar committed
477

478
    if (t == EOSB_TOKEN) {
479
480
481
      ++p;
      break;
    }
482
    if (t >= TWO_TOKEN) {
483
      vp9_model_to_full_probs(p->context_tree, probs);
484
485
486
487
      pp = probs;
    } else {
      pp = p->context_tree;
    }
488
    assert(pp != 0);
489

John Koleszar's avatar
John Koleszar committed
490
491
492
493
    /* skip one or two nodes */
    if (p->skip_eob_node) {
      n -= p->skip_eob_node;
      i = 2 * p->skip_eob_node;
494
      ncount -= p->skip_eob_node;
John Koleszar's avatar
John Koleszar committed
495
    }
John Koleszar's avatar
John Koleszar committed
496

John Koleszar's avatar
John Koleszar committed
497
498
    do {
      const int bb = (v >> --n) & 1;
499
      vp9_write(bc, bb, pp[i >> 1]);
500
      i = vp9_coef_tree[i + bb];
501
502
      ncount--;
    } while (n && ncount);
John Koleszar's avatar
John Koleszar committed
503

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

507
      if (l) {
508
        const unsigned char *pb = b->prob;
John Koleszar's avatar
John Koleszar committed
509
        int v = e >> 1;
510
        int n = l;              /* number of bits in v, assumed nonzero */
John Koleszar's avatar
John Koleszar committed
511
        int i = 0;
John Koleszar's avatar
John Koleszar committed
512

John Koleszar's avatar
John Koleszar committed
513
514
        do {
          const int bb = (v >> --n) & 1;
515
          vp9_write(bc, bb, pb[i >> 1]);
John Koleszar's avatar
John Koleszar committed
516
517
518
          i = b->tree[i + bb];
        } while (n);
      }
John Koleszar's avatar
John Koleszar committed
519

520
      vp9_write_bit(bc, e & 1);
John Koleszar's avatar
John Koleszar committed
521
    }
John Koleszar's avatar
John Koleszar committed
522
523
524
    ++p;
  }

525
  *tp = p;
John Koleszar's avatar
John Koleszar committed
526
527
}

528
529
static void write_mv_ref(vp9_writer *bc, MB_PREDICTION_MODE m,
                         const vp9_prob *p) {
530
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
531
  assert(NEARESTMV <= m  &&  m <= SPLITMV);
532
#endif
533
534
  write_token(bc, vp9_mv_ref_tree, p,
              vp9_mv_ref_encoding_array - NEARESTMV + m);
John Koleszar's avatar
John Koleszar committed
535
536
}

537
538
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
539
540
541
#if CONFIG_DEBUG
  assert(NEARESTMV <= m  &&  m < SPLITMV);
#endif
542
543
  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
544
545
}

546
547
static void write_sub_mv_ref(vp9_writer *bc, B_PREDICTION_MODE m,
                             const vp9_prob *p) {
548
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
549
  assert(LEFT4X4 <= m  &&  m <= NEW4X4);
550
#endif
551
552
  write_token(bc, vp9_sub_mv_ref_tree, p,
              vp9_sub_mv_ref_encoding_array - LEFT4X4 + m);
John Koleszar's avatar
John Koleszar committed
553
554
}

555

Paul Wilkins's avatar
Paul Wilkins committed
556
557
// 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.
558
static void write_mb_segid(vp9_writer *bc,
Paul Wilkins's avatar
Paul Wilkins committed
559
                           const MB_MODE_INFO *mi, const MACROBLOCKD *xd) {
560
561
562
  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
563
564
}

Paul Wilkins's avatar
Paul Wilkins committed
565
// This function encodes the reference frame
566
static void encode_ref_frame(vp9_writer *const bc,
567
                             VP9_COMMON *const cm,
John Koleszar's avatar
John Koleszar committed
568
569
570
571
572
                             MACROBLOCKD *xd,
                             int segment_id,
                             MV_REFERENCE_FRAME rf) {
  int seg_ref_active;
  int seg_ref_count = 0;
573
574
575
  seg_ref_active = vp9_segfeature_active(xd,
                                         segment_id,
                                         SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
576
577

  if (seg_ref_active) {
578
579
580
581
    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
582
583
584
585
586
587
588
  }

  // 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;
589
    vp9_prob pred_prob;
John Koleszar's avatar
John Koleszar committed
590
591
592
    MV_REFERENCE_FRAME pred_rf;

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

    // Get the predicted value.
Paul Wilkins's avatar
Paul Wilkins committed
596
    pred_rf = vp9_get_pred_ref(cm, xd);
John Koleszar's avatar
John Koleszar committed
597
598
599
600
601

    // 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
602
    vp9_set_pred_flag(xd, PRED_REF, prediction_flag);
603
    vp9_write(bc, prediction_flag, pred_prob);
John Koleszar's avatar
John Koleszar committed
604
605
606

    // If not predicted correctly then code value explicitly
    if (!prediction_flag) {
607
      vp9_prob mod_refprobs[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
608
609
610
611
612
613
614
615

      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] *=
616
          vp9_check_segref(xd, segment_id, INTRA_FRAME);
John Koleszar's avatar
John Koleszar committed
617
        mod_refprobs[LAST_FRAME] *=
618
          vp9_check_segref(xd, segment_id, LAST_FRAME);
John Koleszar's avatar
John Koleszar committed
619
        mod_refprobs[GOLDEN_FRAME] *=
620
621
          (vp9_check_segref(xd, segment_id, GOLDEN_FRAME) *
           vp9_check_segref(xd, segment_id, ALTREF_FRAME));
John Koleszar's avatar
John Koleszar committed
622
623
624
      }

      if (mod_refprobs[0]) {
625
        vp9_write(bc, (rf != INTRA_FRAME), mod_refprobs[0]);
John Koleszar's avatar
John Koleszar committed
626
627
628
629
630
      }

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

John Koleszar's avatar
John Koleszar committed
634
635
        if (rf != LAST_FRAME) {
          if (mod_refprobs[2]) {
636
            vp9_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]);
John Koleszar's avatar
John Koleszar committed
637
          }
638
        }
John Koleszar's avatar
John Koleszar committed
639
      }
Paul Wilkins's avatar
Paul Wilkins committed
640
    }
John Koleszar's avatar
John Koleszar committed
641
  }
Paul Wilkins's avatar
Paul Wilkins committed
642

John Koleszar's avatar
John Koleszar committed
643
644
  // 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
645
}
John Koleszar's avatar
John Koleszar committed
646

647
// Update the probabilities used to encode reference frame data
648
649
static void update_ref_probs(VP9_COMP *const cpi) {
  VP9_COMMON *const cm = &cpi->common;
650

John Koleszar's avatar
John Koleszar committed
651
652
653
654
  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];
655

John Koleszar's avatar
John Koleszar committed
656
657
658
  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]);
659

John Koleszar's avatar
John Koleszar committed
660
661
  // Compute a modified set of probabilities to use when prediction of the
  // reference frame fails
Paul Wilkins's avatar
Paul Wilkins committed
662
  vp9_compute_mod_refprobs(cm);
663
664
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
665
static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m,
666
                                vp9_writer *bc, int mi_row, int mi_col) {
667
  VP9_COMMON *const pc = &cpi->common;
668
  const nmv_context *nmvc = &pc->fc.nmvc;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
669
670
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
671
  const int mis = pc->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
672
673
674
675
676
  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
677

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
681
682
#ifdef ENTROPY_STATS
  active_section = 9;
683
#endif
684

Ronald S. Bultje's avatar
Ronald S. Bultje committed
685
686
687
688
689
  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
690

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
694
695
      // If the mb segment id wasn't predicted code explicitly
      if (!prediction_flag)
Paul Wilkins's avatar
Paul Wilkins committed
696
        write_mb_segid(bc, mi, &cpi->mb.e_mbd);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
697
698
699
700
701
    } else {
      // Normal unpredicted coding
      write_mb_segid(bc, mi, &cpi->mb.e_mbd);
    }
  }
702

703
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
704
705
    skip_coeff = 1;
  } else {
706
    skip_coeff = m->mbmi.mb_skip_coeff;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
707
708
709
    vp9_write(bc, skip_coeff,
              vp9_get_pred_prob(pc, xd, PRED_MBSKIP));
  }
John Koleszar's avatar
John Koleszar committed
710

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
714
  if (rf == INTRA_FRAME) {
715
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
716
    active_section = 6;
717
#endif
Paul Wilkins's avatar
Paul Wilkins committed
718

719
720
    if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8)
      write_sb_ymode(bc, mode, pc->fc.sb_ymode_prob);
Paul Wilkins's avatar
Paul Wilkins committed
721

722
    if (m->mbmi.sb_type < BLOCK_SIZE_SB8X8) {
723
724
725
726
727
728
729
      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)
          write_sb_ymode(bc, m->bmi[idy * 2 + idx].as_mode.first,
                         pc->fc.sb_ymode_prob);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
730
    }
Jingning Han's avatar
Jingning Han committed
731
732
    write_uv_mode(bc, mi->uv_mode,
                  pc->fc.uv_mode_prob[mode]);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
733
734
  } else {
    vp9_prob mv_ref_p[VP9_MVREFS - 1];
735

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

John Koleszar's avatar
John Koleszar committed
738
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
739
    active_section = 3;
John Koleszar's avatar
John Koleszar committed
740
741
#endif

742
    // If segment skip is not enabled code the mode.
Paul Wilkins's avatar
Paul Wilkins committed
743
    if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
744
745
      if (mi->sb_type >= BLOCK_SIZE_SB8X8)
        write_sb_mv_ref(bc, mode, mv_ref_p);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
746
747
      vp9_accum_mv_refs(&cpi->common, mode, mi->mb_mode_context[rf]);
    }
748

749
    if (is_inter_mode(mode)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
750
751
752
753
754
755
756
757
758
759
      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);
      }
    }
760

Ronald S. Bultje's avatar
Ronald S. Bultje committed
761
762
763
764
765
766
    // 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
767

Ronald S. Bultje's avatar
Ronald S. Bultje committed
768
769
    switch (mode) { /* new, split require MVs */
      case NEWMV:
770
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
771
        active_section = 5;
772
#endif
773
774
775
776
777
778
779
780
        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
781
782
        break;
      case SPLITMV: {
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
        int j;
        B_PREDICTION_MODE blockmode;
        int_mv blockmv;
        int k = -1;  /* first block in subset j */
        int mv_contz;
        int_mv leftmv, abovemv;
        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;
            k = j;
            leftmv.as_int = left_block_mv(xd, m, k);
            abovemv.as_int = above_block_mv(m, k, mis);
            mv_contz = vp9_mv_cont(&leftmv, &abovemv);

            write_sub_mv_ref(bc, blockmode,
                             cpi->common.fc.sub_mv_ref_prob[mv_contz]);
            cpi->sub_mv_ref_count[mv_contz][blockmode - LEFT4X4]++;
            if (blockmode == NEW4X4) {
806
#ifdef ENTROPY_STATS
807
              active_section = 11;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
808
#endif
809
              vp9_encode_mv(bc, &blockmv.as_mv, &mi->best_mv.as_mv,
810
                            nmvc, xd->allow_high_precision_mv);
811
812
813
814
815
816
817

              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,
                              nmvc, xd->allow_high_precision_mv);
            }
John Koleszar's avatar
John Koleszar committed
818
          }
819
820
821
822
823
        }

#ifdef MODE_STATS
        ++count_mb_seg[mi->partitioning];
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
824
825
826
827
828
829
        break;
      }
      default:
        break;
    }
  }
John Koleszar's avatar
John Koleszar committed
830

831
  if (mi->sb_type >= BLOCK_SIZE_SB8X8 && pc->txfm_mode == TX_MODE_SELECT &&
832
      !(rf != INTRA_FRAME &&
833
        (skip_coeff || vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)))) {
834
835
836
837
838
839
840
841
842
    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]);
    }
  }
John Koleszar's avatar
John Koleszar committed
843
}
844

Ronald S. Bultje's avatar
Ronald S. Bultje committed
845
static void write_mb_modes_kf(const VP9_COMP *cpi,
846
                              MODE_INFO *m,
847
                              vp9_writer *bc, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
848
849
850
  const VP9_COMMON *const c = &cpi->common;
  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  const int ym = m->mbmi.mode;
851
  const int mis = c->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
852
853
  const int segment_id = m->mbmi.segment_id;
  int skip_coeff;
854

855
  if (xd->update_mb_segmentation_map)
856
857
    write_mb_segid(bc, &m->mbmi, xd);

858
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
859
860
    skip_coeff = 1;
  } else {
861
    skip_coeff = m->mbmi.mb_skip_coeff;
862
    vp9_write(bc, skip_coeff, vp9_get_pred_prob(c, xd, PRED_MBSKIP));
863
864
  }

865
866
867
868
869
870
  if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8) {
    const B_PREDICTION_MODE A = above_block_mode(m, 0, mis);
    const B_PREDICTION_MODE L = xd->left_available ?
                                 left_block_mode(m, 0) : DC_PRED;
    write_kf_bmode(bc, ym, c->kf_bmode_prob[A][L]);
  }
871

872
  if (m->mbmi.sb_type < BLOCK_SIZE_SB8X8) {
873
874
875
    int idx, idy;
    int bw = 1 << b_width_log2(m->mbmi.sb_type);
    int bh = 1 << b_height_log2(m->mbmi.sb_type);
876
877
878
879
880
881
882
883
884
885
    for (idy = 0; idy < 2; idy += bh) {
      for (idx = 0; idx < 2; idx += bw) {
        int i = idy * 2 + idx;
        const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
        const B_PREDICTION_MODE L = (xd->left_available || idx) ?
                                     left_block_mode(m, i) : DC_PRED;
        write_kf_bmode(bc, m->bmi[i].as_mode.first,
                       c->kf_bmode_prob[A][L]);
      }
    }
886
887
  }

Jingning Han's avatar
Jingning Han committed
888
889
  write_uv_mode(bc, m->mbmi.uv_mode, c->kf_uv_mode_prob[ym]);

890
  if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8 && c->txfm_mode == TX_MODE_SELECT &&
891
      !((skip_coeff || vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)))) {
892
893
894
895
896
897
898
899
900
    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]);
    }
  }
901
902
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
903
904
static void write_modes_b(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                          TOKENEXTRA **tok, TOKENEXTRA *tok_end,
905
                          int mi_row, int mi_col) {
906
  VP9_COMMON *const cm = &cpi->common;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
907
908
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;

909
910
911
  if (m->mbmi.sb_type < BLOCK_SIZE_SB8X8)
    if (xd->ab_index > 0)
      return;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
912
  xd->mode_info_context = m;
913
914
915
  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));
916
  if (cm->frame_type == KEY_FRAME) {
917
    write_mb_modes_kf(cpi, m, bc, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
918
919
920
921
#ifdef ENTROPY_STATS
    active_section = 8;
#endif
  } else {
922
    pack_inter_mode_mvs(cpi, m, bc, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
923
924
925
926
927
928
929
930
931
#ifdef ENTROPY_STATS
    active_section = 1;
#endif
  }

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

932
933
static void write_modes_sb(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                           TOKENEXTRA **tok, TOKENEXTRA *tok_end,
934
                           int mi_row, int mi_col,
935
936
                           BLOCK_SIZE_TYPE bsize) {
  VP9_COMMON *const cm = &cpi->common;
937
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
938
939
  const int mis = cm->mode_info_stride;
  int bwl, bhl;
940
941
  int bsl = b_width_log2(bsize);
  int bs = (1 << bsl) / 4;  // mode_info step for subsize
942
943
944
945
  int n;
  PARTITION_TYPE partition;
  BLOCK_SIZE_TYPE subsize;

946
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
947
948
    return;

949
950
  bwl = b_width_log2(m->mbmi.sb_type);
  bhl = b_height_log2(m->mbmi.sb_type);
951
952
953
954
955
956
957
958
959
960
961
962
963

  // 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);

964
  if (bsize < BLOCK_SIZE_SB8X8)
965
    if (xd->ab_index > 0)
966
967
968
      return;

  if (bsize >= BLOCK_SIZE_SB8X8) {
969
    int pl;
970
971
    xd->left_seg_context = cm->left_seg_context + (mi_row & MI_MASK);
    xd->above_seg_context = cm->above_seg_context + mi_col;