vp9_bitstream.c 81 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
55
56
57
58
59
60
61
62
63
#if CONFIG_CODE_ZEROGROUP
#ifdef ZPC_STATS
vp9_zpc_count zpc_stats_4x4;
vp9_zpc_count zpc_stats_8x8;
vp9_zpc_count zpc_stats_16x16;
vp9_zpc_count zpc_stats_32x32;
void init_zpcstats();
void update_zpcstats(VP9_COMMON *const cm);
void print_zpcstats();
#endif
#endif
64

John Koleszar's avatar
John Koleszar committed
65
66
67
68
#ifdef MODE_STATS
int count_mb_seg[4] = { 0, 0, 0, 0 };
#endif

69
70
#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)))
71
72
73
74

#define SEARCH_NEWP
static int update_bits[255];

75
76
77
78
79
80
81
82
83
84
85
86
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;
}

John Koleszar's avatar
John Koleszar committed
87
88
89
static void compute_update_table() {
  int i;
  for (i = 0; i < 255; i++)
90
    update_bits[i] = vp9_count_term_subexp(i, SUBEXP_PARAM, 255);
91
92
}

John Koleszar's avatar
John Koleszar committed
93
94
95
96
97
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;
98
99
}

John Koleszar's avatar
John Koleszar committed
100
101
102
103
104
static int remap_prob(int v, int m) {
  const int n = 256;
  const int modulus = MODULUS_PARAM;
  int i;
  if ((m << 1) <= n)
105
    i = vp9_recenter_nonneg(v, m) - 1;
John Koleszar's avatar
John Koleszar committed
106
  else
107
    i = vp9_recenter_nonneg(n - 1 - v, n - 1 - m) - 1;
John Koleszar's avatar
John Koleszar committed
108
109
110

  i = split_index(i, n - 1, modulus);
  return i;
111
}
112

113
114
static void write_prob_diff_update(vp9_writer *const bc,
                                   vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
115
  int delp = remap_prob(newp, oldp);
116
  vp9_encode_term_subexp(bc, delp, SUBEXP_PARAM, 255);
117
118
}

119
static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
120
121
  int delp = remap_prob(newp, oldp);
  return update_bits[delp] * 256;
122
}
123

John Koleszar's avatar
John Koleszar committed
124
static void update_mode(
125
  vp9_writer *const bc,
John Koleszar's avatar
John Koleszar committed
126
  int n,
127
  const struct vp9_token tok[/* n */],
128
129
130
  vp9_tree tree,
  vp9_prob Pnew               [/* n-1 */],
  vp9_prob Pcur               [/* n-1 */],
John Koleszar's avatar
John Koleszar committed
131
132
133
134
135
136
  unsigned int bct            [/* n-1 */] [2],
  const unsigned int num_events[/* n */]
) {
  unsigned int new_b = 0, old_b = 0;
  int i = 0;

137
138
  vp9_tree_probs_from_distribution(tree, Pnew, bct, num_events, 0);
  n--;
John Koleszar's avatar
John Koleszar committed
139
140

  do {
141
142
    new_b += cost_branch(bct[i], Pnew[i]);
    old_b += cost_branch(bct[i], Pcur[i]);
John Koleszar's avatar
John Koleszar committed
143
144
145
  } while (++i < n);

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

148
    vp9_write_bit(bc, 1);
John Koleszar's avatar
John Koleszar committed
149

John Koleszar's avatar
John Koleszar committed
150
    do {
151
      const vp9_prob p = Pnew[i];
John Koleszar's avatar
John Koleszar committed
152

153
      vp9_write_literal(bc, Pcur[i] = p ? p : 1, 8);
John Koleszar's avatar
John Koleszar committed
154
155
    } while (++i < n);
  } else
156
    vp9_write_bit(bc, 0);
John Koleszar's avatar
John Koleszar committed
157
158
}

159
static void update_mbintra_mode_probs(VP9_COMP* const cpi,
160
                                      vp9_writer* const bc) {
161
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
162

163
164
165
166
167
168
169
170
171
  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
172
173
}

174
175
void vp9_update_skip_probs(VP9_COMP *cpi) {
  VP9_COMMON *const pc = &cpi->common;
John Koleszar's avatar
John Koleszar committed
176
  int k;
Paul Wilkins's avatar
Paul Wilkins committed
177

178
  for (k = 0; k < MBSKIP_CONTEXTS; ++k)
John Koleszar's avatar
John Koleszar committed
179
180
    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
181
182
}

183
static void update_switchable_interp_probs(VP9_COMP *cpi,
184
                                           vp9_writer* const bc) {
185
  VP9_COMMON *const pc = &cpi->common;
186
187
  unsigned int branch_ct[32][2];
  int i, j;
188
  for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
189
    vp9_tree_probs_from_distribution(
190
        vp9_switchable_interp_tree,
191
        pc->fc.switchable_interp_prob[j], branch_ct,
192
        cpi->switchable_interp_count[j], 0);
193
    for (i = 0; i < VP9_SWITCHABLE_FILTERS - 1; ++i) {
194
195
      if (pc->fc.switchable_interp_prob[j][i] < 1)
        pc->fc.switchable_interp_prob[j][i] = 1;
196
      vp9_write_prob(bc, pc->fc.switchable_interp_prob[j][i]);
197
198
199
200
    }
  }
}

201
// This function updates the reference frame prediction stats
202
203
static void update_refpred_stats(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
204
  int i;
205
  vp9_prob new_pred_probs[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
206
207
208
  int old_cost, new_cost;

  // Set the prediction probability structures to defaults
209
  if (cm->frame_type != KEY_FRAME) {
John Koleszar's avatar
John Koleszar committed
210
211
    // From the prediction counts set the probabilities for each context
    for (i = 0; i < PREDICTION_PROBS; i++) {
212
213
214
215
      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
216
217
218

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

222
223
      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
224
225
226
227
228
229
230

      // 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;
231
    }
John Koleszar's avatar
John Koleszar committed
232
  }
233
234
}

235
236
237
238
239
240
// 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.
241
242
static void update_inter_mode_probs(VP9_COMMON *cm,
                                    int mode_context[INTER_MODE_CONTEXTS][4]) {
243
  int i, j;
244
  unsigned int (*mv_ref_ct)[4][2] = cm->fc.mv_ref_ct;
245
246
247
248
249
250

  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++) {
251
      int new_prob, old_cost, new_cost;
252
253
254

      // 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]);
255
      new_prob = get_binary_prob(mv_ref_ct[i][j][0], mv_ref_ct[i][j][1]);
256
257
258
259
260
261
262
263
264
265
266
      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;
      }
    }
  }
}
267

268
269
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
270
271
}

272
273
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
274
275
}

276
277
278
279
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);
}

280
281
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
282
283
}

284
#if !CONFIG_SB8X8
285
286
static void write_i8x8_mode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_i8x8_mode_tree, p, vp9_i8x8_mode_encodings + m);
Yaowu Xu's avatar
Yaowu Xu committed
287
}
288
#endif
Yaowu Xu's avatar
Yaowu Xu committed
289

290
291
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
292
293
294
}


295
static void write_bmode(vp9_writer *bc, int m, const vp9_prob *p) {
296
297
298
299
#if CONFIG_NEWBINTRAMODES
  assert(m < B_CONTEXT_PRED - CONTEXT_PRED_REPLACEMENTS || m == B_CONTEXT_PRED);
  if (m == B_CONTEXT_PRED) m -= CONTEXT_PRED_REPLACEMENTS;
#endif
300
  write_token(bc, vp9_bmode_tree, p, vp9_bmode_encodings + m);
John Koleszar's avatar
John Koleszar committed
301
302
}

303
304
305
306
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);
}

307
#if !CONFIG_SB8X8
308
static void write_split(vp9_writer *bc, int x, const vp9_prob *p) {
309
  write_token(bc, vp9_mbsplit_tree, p, vp9_mbsplit_encodings + x);
John Koleszar's avatar
John Koleszar committed
310
}
311
#endif
John Koleszar's avatar
John Koleszar committed
312

313
static int prob_update_savings(const unsigned int *ct,
314
315
316
317
318
                               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;
319
  return old_b - new_b - update_b;
320
321
322
}

static int prob_diff_update_savings_search(const unsigned int *ct,
323
324
325
                                           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
326
  int new_b, update_b, savings, bestsavings, step;
327
  vp9_prob newp, bestnewp;
John Koleszar's avatar
John Koleszar committed
328
329
330
331
332
333

  bestsavings = 0;
  bestnewp = oldp;

  step = (*bestp > oldp ? -1 : 1);
  for (newp = *bestp; newp != oldp; newp += step) {
334
335
    new_b = cost_branch256(ct, newp);
    update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
John Koleszar's avatar
John Koleszar committed
336
337
338
339
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
340
    }
John Koleszar's avatar
John Koleszar committed
341
342
343
  }
  *bestp = bestnewp;
  return bestsavings;
344
345
}

346
347
348
349
350
#if CONFIG_MODELCOEFPROB && MODEL_BASED_UPDATE
static int prob_diff_update_savings_search_model(const unsigned int *ct,
                                                 const vp9_prob *oldp,
                                                 vp9_prob *bestp,
                                                 const vp9_prob upd,
351
                                                 int b, int r, int q) {
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  int i, old_b, new_b, update_b, savings, bestsavings, step;
  int newp;
  vp9_prob bestnewp, newplist[ENTROPY_NODES];
  for (i = UNCONSTRAINED_NODES - 1, old_b = 0; i < ENTROPY_NODES; ++i)
    old_b += cost_branch256(ct + 2 * i, oldp[i]);

  bestsavings = 0;
  bestnewp = oldp[UNCONSTRAINED_NODES - 1];

  step = (*bestp > oldp[UNCONSTRAINED_NODES - 1] ? -1 : 1);
  newp = *bestp;
  // newp = *bestp - step * (abs(*bestp - oldp[UNCONSTRAINED_NODES - 1]) >> 1);
  for (; newp != oldp[UNCONSTRAINED_NODES - 1]; newp += step) {
    if (newp < 1 || newp > 255) continue;
    newplist[UNCONSTRAINED_NODES - 1] = newp;
    vp9_get_model_distribution(newp, newplist, b, r);
    for (i = UNCONSTRAINED_NODES - 1, new_b = 0; i < ENTROPY_NODES; ++i)
      new_b += cost_branch256(ct + 2 * i, newplist[i]);
    update_b = prob_diff_update_cost(newp, oldp[UNCONSTRAINED_NODES - 1]) +
        vp9_cost_upd256;
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
    }
  }
  *bestp = bestnewp;
  return bestsavings;
}
#endif

383
384
385
386
387
388
389
390
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);
391
    vp9_write_prob(bc, newp);
392
393
394
395
396
397
    *oldp = newp;
  } else {
    vp9_write(bc, 0, upd);
  }
}

398
static void pack_mb_tokens(vp9_writer* const bc,
399
400
401
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
402

John Koleszar's avatar
John Koleszar committed
403
  while (p < stop) {
404
    const int t = p->token;
405
    const struct vp9_token *const a = vp9_coef_encodings + t;
406
    const vp9_extra_bit *const b = vp9_extra_bits + t;
John Koleszar's avatar
John Koleszar committed
407
408
409
    int i = 0;
    const unsigned char *pp = p->context_tree;
    int v = a->value;
410
    int n = a->len;
411
    int ncount = n;
John Koleszar's avatar
John Koleszar committed
412

413
414
415
416
417
    if (t == EOSB_TOKEN)
    {
      ++p;
      break;
    }
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
    assert(pp != 0);
#if CONFIG_CODE_ZEROGROUP
    if (t == ZPC_ISOLATED || t == ZPC_EOORIENT) {
      assert((p - 1)->token == ZERO_TOKEN);
      encode_bool(bc, t == ZPC_ISOLATED, *pp);
      ++p;
      continue;
    } else if (p->skip_coef_val) {
      assert(p->skip_eob_node == 0);
      assert(t == DCT_EOB_TOKEN || t == ZERO_TOKEN);
      encode_bool(bc, t == ZERO_TOKEN, *pp);
      ++p;
      continue;
    }
#endif
433

John Koleszar's avatar
John Koleszar committed
434
435
436
437
    /* skip one or two nodes */
    if (p->skip_eob_node) {
      n -= p->skip_eob_node;
      i = 2 * p->skip_eob_node;
438
      ncount -= p->skip_eob_node;
John Koleszar's avatar
John Koleszar committed
439
    }
John Koleszar's avatar
John Koleszar committed
440

John Koleszar's avatar
John Koleszar committed
441
442
    do {
      const int bb = (v >> --n) & 1;
443
      vp9_write(bc, bb, pp[i >> 1]);
444
      i = vp9_coef_tree[i + bb];
445
446
      ncount--;
    } while (n && ncount);
John Koleszar's avatar
John Koleszar committed
447
448


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

452
      if (l) {
John Koleszar's avatar
John Koleszar committed
453
454
        const unsigned char *pp = b->prob;
        int v = e >> 1;
455
        int n = l;              /* number of bits in v, assumed nonzero */
John Koleszar's avatar
John Koleszar committed
456
        int i = 0;
John Koleszar's avatar
John Koleszar committed
457

John Koleszar's avatar
John Koleszar committed
458
459
        do {
          const int bb = (v >> --n) & 1;
460
          vp9_write(bc, bb, pp[i >> 1]);
John Koleszar's avatar
John Koleszar committed
461
462
463
          i = b->tree[i + bb];
        } while (n);
      }
John Koleszar's avatar
John Koleszar committed
464

465
      vp9_write_bit(bc, e & 1);
John Koleszar's avatar
John Koleszar committed
466
    }
John Koleszar's avatar
John Koleszar committed
467
468
469
    ++p;
  }

470
  *tp = p;
John Koleszar's avatar
John Koleszar committed
471
472
}

473
474
static void write_mv_ref(vp9_writer *bc, MB_PREDICTION_MODE m,
                         const vp9_prob *p) {
475
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
476
  assert(NEARESTMV <= m  &&  m <= SPLITMV);
477
#endif
478
479
  write_token(bc, vp9_mv_ref_tree, p,
              vp9_mv_ref_encoding_array - NEARESTMV + m);
John Koleszar's avatar
John Koleszar committed
480
481
}

482
483
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
484
485
486
#if CONFIG_DEBUG
  assert(NEARESTMV <= m  &&  m < SPLITMV);
#endif
487
488
  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
489
490
}

491
492
static void write_sub_mv_ref(vp9_writer *bc, B_PREDICTION_MODE m,
                             const vp9_prob *p) {
493
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
494
  assert(LEFT4X4 <= m  &&  m <= NEW4X4);
495
#endif
496
497
  write_token(bc, vp9_sub_mv_ref_tree, p,
              vp9_sub_mv_ref_encoding_array - LEFT4X4 + m);
John Koleszar's avatar
John Koleszar committed
498
499
}

500
501
static void write_nmv(VP9_COMP *cpi, vp9_writer *bc,
                      const MV *mv, const int_mv *ref,
John Koleszar's avatar
John Koleszar committed
502
                      const nmv_context *nmvc, int usehp) {
503
504
505
506
  MV e;
  e.row = mv->row - ref->as_mv.row;
  e.col = mv->col - ref->as_mv.col;

507
508
  vp9_encode_nmv(bc, &e, &ref->as_mv, nmvc);
  vp9_encode_nmv_fp(bc, &e, &ref->as_mv, nmvc, usehp);
509
510
}

Paul Wilkins's avatar
Paul Wilkins committed
511
512
// 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.
513
static void write_mb_segid(vp9_writer *bc,
Paul Wilkins's avatar
Paul Wilkins committed
514
                           const MB_MODE_INFO *mi, const MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
515
  // Encode the MB segment id.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
516
  int seg_id = mi->segment_id;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
517

Paul Wilkins's avatar
Paul Wilkins committed
518
  if (xd->segmentation_enabled && xd->update_mb_segmentation_map) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
519
    switch (seg_id) {
John Koleszar's avatar
John Koleszar committed
520
      case 0:
521
522
        vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[1]);
523
        vp9_write(bc, 0, xd->mb_segment_tree_probs[2]);
John Koleszar's avatar
John Koleszar committed
524
525
        break;
      case 1:
526
        vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
527
528
        vp9_write(bc, 0, xd->mb_segment_tree_probs[1]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[2]);
John Koleszar's avatar
John Koleszar committed
529
530
        break;
      case 2:
531
532
533
        vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[1]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[3]);
John Koleszar's avatar
John Koleszar committed
534
535
        break;
      case 3:
536
537
538
539
540
        vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[1]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[3]);
        break;
      case 4:
541
        vp9_write(bc, 1, xd->mb_segment_tree_probs[0]);
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
        vp9_write(bc, 0, xd->mb_segment_tree_probs[4]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[5]);
        break;
      case 5:
        vp9_write(bc, 1, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[4]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[5]);
        break;
      case 6:
        vp9_write(bc, 1, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[4]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[6]);
        break;
      case 7:
        vp9_write(bc, 1, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[4]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[6]);
John Koleszar's avatar
John Koleszar committed
559
560
561
562
        break;

        // TRAP.. This should not happen
      default:
563
564
        vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[1]);
565
        vp9_write(bc, 0, xd->mb_segment_tree_probs[2]);
John Koleszar's avatar
John Koleszar committed
566
        break;
John Koleszar's avatar
John Koleszar committed
567
    }
John Koleszar's avatar
John Koleszar committed
568
  }
John Koleszar's avatar
John Koleszar committed
569
570
}

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

  if (seg_ref_active) {
584
585
586
587
    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
588
589
590
591
592
593
594
  }

  // 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;
595
    vp9_prob pred_prob;
John Koleszar's avatar
John Koleszar committed
596
597
598
    MV_REFERENCE_FRAME pred_rf;

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

    // Get the predicted value.
Paul Wilkins's avatar
Paul Wilkins committed
602
    pred_rf = vp9_get_pred_ref(cm, xd);
John Koleszar's avatar
John Koleszar committed
603
604
605
606
607

    // 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
608
    vp9_set_pred_flag(xd, PRED_REF, prediction_flag);
609
    vp9_write(bc, prediction_flag, pred_prob);
John Koleszar's avatar
John Koleszar committed
610
611
612

    // If not predicted correctly then code value explicitly
    if (!prediction_flag) {
613
      vp9_prob mod_refprobs[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
614
615
616
617
618
619
620
621

      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] *=
622
          vp9_check_segref(xd, segment_id, INTRA_FRAME);
John Koleszar's avatar
John Koleszar committed
623
        mod_refprobs[LAST_FRAME] *=
624
          vp9_check_segref(xd, segment_id, LAST_FRAME);
John Koleszar's avatar
John Koleszar committed
625
        mod_refprobs[GOLDEN_FRAME] *=
626
627
          (vp9_check_segref(xd, segment_id, GOLDEN_FRAME) *
           vp9_check_segref(xd, segment_id, ALTREF_FRAME));
John Koleszar's avatar
John Koleszar committed
628
629
630
      }

      if (mod_refprobs[0]) {
631
        vp9_write(bc, (rf != INTRA_FRAME), mod_refprobs[0]);
John Koleszar's avatar
John Koleszar committed
632
633
634
635
636
      }

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

John Koleszar's avatar
John Koleszar committed
640
641
        if (rf != LAST_FRAME) {
          if (mod_refprobs[2]) {
642
            vp9_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]);
John Koleszar's avatar
John Koleszar committed
643
          }
644
        }
John Koleszar's avatar
John Koleszar committed
645
      }
Paul Wilkins's avatar
Paul Wilkins committed
646
    }
John Koleszar's avatar
John Koleszar committed
647
  }
Paul Wilkins's avatar
Paul Wilkins committed
648

John Koleszar's avatar
John Koleszar committed
649
650
  // 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
651
}
John Koleszar's avatar
John Koleszar committed
652

653
// Update the probabilities used to encode reference frame data
654
655
static void update_ref_probs(VP9_COMP *const cpi) {
  VP9_COMMON *const cm = &cpi->common;
656

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

John Koleszar's avatar
John Koleszar committed
662
663
664
  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]);
665

John Koleszar's avatar
John Koleszar committed
666
667
  // Compute a modified set of probabilities to use when prediction of the
  // reference frame fails
Paul Wilkins's avatar
Paul Wilkins committed
668
  vp9_compute_mod_refprobs(cm);
669
670
}

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

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
687
688
#ifdef ENTROPY_STATS
  active_section = 9;
689
#endif
690

Ronald S. Bultje's avatar
Ronald S. Bultje committed
691
692
693
694
695
  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
696

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
700
701
      // If the mb segment id wasn't predicted code explicitly
      if (!prediction_flag)
Paul Wilkins's avatar
Paul Wilkins committed
702
        write_mb_segid(bc, mi, &cpi->mb.e_mbd);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
703
704
705
706
707
    } else {
      // Normal unpredicted coding
      write_mb_segid(bc, mi, &cpi->mb.e_mbd);
    }
  }
708

709
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
710
711
    skip_coeff = 1;
  } else {
712
    skip_coeff = m->mbmi.mb_skip_coeff;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
713
714
715
    vp9_write(bc, skip_coeff,
              vp9_get_pred_prob(pc, xd, PRED_MBSKIP));
  }
John Koleszar's avatar
John Koleszar committed
716

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
720
  if (rf == INTRA_FRAME) {
721
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
722
    active_section = 6;
723
#endif
Paul Wilkins's avatar
Paul Wilkins committed
724

725
    if (m->mbmi.sb_type > BLOCK_SIZE_MB16X16)
Paul Wilkins's avatar
Paul Wilkins committed
726
727
728
729
      write_sb_ymode(bc, mode, pc->fc.sb_ymode_prob);
    else
      write_ymode(bc, mode, pc->fc.ymode_prob);

Yaowu Xu's avatar
Yaowu Xu committed
730
    if (mode == I4X4_PRED) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
731
732
733
734
      int j = 0;
      do {
        write_bmode(bc, m->bmi[j].as_mode.first,
                    pc->fc.bmode_prob);
735
      } while (++j < (16 >> (CONFIG_SB8X8 * 2)));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
736
    }
737
#if !CONFIG_SB8X8
Ronald S. Bultje's avatar
Ronald S. Bultje committed
738
739
740
741
742
743
744
745
746
    if (mode == I8X8_PRED) {
      write_i8x8_mode(bc, m->bmi[0].as_mode.first,
                      pc->fc.i8x8_mode_prob);
      write_i8x8_mode(bc, m->bmi[2].as_mode.first,
                      pc->fc.i8x8_mode_prob);
      write_i8x8_mode(bc, m->bmi[8].as_mode.first,
                      pc->fc.i8x8_mode_prob);
      write_i8x8_mode(bc, m->bmi[10].as_mode.first,
                      pc->fc.i8x8_mode_prob);
747
748
749
    } else
#endif
    {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
750
751
752
753
754
      write_uv_mode(bc, mi->uv_mode,
                    pc->fc.uv_mode_prob[mode]);
    }
  } else {
    vp9_prob mv_ref_p[VP9_MVREFS - 1];
755

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

John Koleszar's avatar
John Koleszar committed
758
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
759
    active_section = 3;
John Koleszar's avatar
John Koleszar committed
760
761
#endif

762
    // If segment skip is not enabled code the mode.
Paul Wilkins's avatar
Paul Wilkins committed
763
    if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
764
      if (mi->sb_type > BLOCK_SIZE_MB16X16) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
765
        write_sb_mv_ref(bc, mode, mv_ref_p);
766
      } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
767
768
769
770
        write_mv_ref(bc, mode, mv_ref_p);
      }
      vp9_accum_mv_refs(&cpi->common, mode, mi->mb_mode_context[rf]);
    }
771

772
    if (is_inter_mode(mode)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
773
774
775
776
777
778
779
780
781
782
      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);
      }
    }
783

Ronald S. Bultje's avatar
Ronald S. Bultje committed
784
785
786
787
788
789
    // 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));
    }
790
#if CONFIG_COMP_INTERINTRA_PRED
Ronald S. Bultje's avatar
Ronald S. Bultje committed
791
792
793
794
795
796
797
798
799
800
801
802
803
    if (cpi->common.use_interintra &&
        mode >= NEARESTMV && mode < SPLITMV &&
        mi->second_ref_frame <= INTRA_FRAME) {
      vp9_write(bc, mi->second_ref_frame == INTRA_FRAME,
                pc->fc.interintra_prob);
      // if (!cpi->dummy_packing)
      //   printf("-- %d (%d)\n", mi->second_ref_frame == INTRA_FRAME,
      //          pc->fc.interintra_prob);
      if (mi->second_ref_frame == INTRA_FRAME) {
        // if (!cpi->dummy_packing)
        //   printf("** %d %d\n", mi->interintra_mode,
        // mi->interintra_uv_mode);
        write_ymode(bc, mi->interintra_mode, pc->fc.ymode_prob);
804
#if SEPARATE_INTERINTRA_UV
Ronald S. Bultje's avatar
Ronald S. Bultje committed
805
806
        write_uv_mode(bc, mi->interintra_uv_mode,
                      pc->fc.uv_mode_prob[mi->interintra_mode]);
807
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
808
809
      }
    }
810
#endif
John Koleszar's avatar
John Koleszar committed
811

Ronald S. Bultje's avatar
Ronald S. Bultje committed
812
813
    switch (mode) { /* new, split require MVs */
      case NEWMV:
814
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
815
        active_section = 5;
816
#endif
817
        write_nmv(cpi, bc, &mi->mv[0].as_mv, &mi->best_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
818
819
                  (const nmv_context*) nmvc,
                  xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
820

Ronald S. Bultje's avatar
Ronald S. Bultje committed
821
        if (mi->second_ref_frame > 0) {
822
          write_nmv(cpi, bc, &mi->mv[1].as_mv, &mi->best_second_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
823
824
825
826
827
828
                    (const nmv_context*) nmvc,
                    xd->allow_high_precision_mv);
        }
        break;
      case SPLITMV: {
        int j = 0;
John Koleszar's avatar
John Koleszar committed
829

830
#ifdef MODE_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
831
        ++count_mb_seg[mi->partitioning];
832
#endif
John Koleszar's avatar
John Koleszar committed
833

834
#if !CONFIG_SB8X8
Ronald S. Bultje's avatar
Ronald S. Bultje committed
835
836
        write_split(bc, mi->partitioning, cpi->common.fc.mbsplit_prob);
        cpi->mbsplit_count[mi->partitioning]++;
837
#endif
Adrian Grange's avatar
Adrian Grange committed
838

Ronald S. Bultje's avatar
Ronald S. Bultje committed
839
840
841
        do {
          B_PREDICTION_MODE blockmode;
          int_mv blockmv;
842
#if !CONFIG_SB8X8
Ronald S. Bultje's avatar
Ronald S. Bultje committed
843
          const int *const  L = vp9_mbsplits[mi->partitioning];
844
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
845
846
847
848
849
850
          int k = -1;  /* first block in subset j */
          int mv_contz;
          int_mv leftmv, abovemv;

          blockmode = cpi->mb.partition_info->bmi[j].mode;
          blockmv = cpi->mb.partition_info->bmi[j].mv;
851
852
853
#if CONFIG_SB8X8
          k = j;
#else
854
#if CONFIG_DEBUG
Ronald S. Bultje's avatar
Ronald S. Bultje committed
855
856
857
          while (j != L[++k])
            if (k >= 16)
              assert(0);
858
#else
Ronald S. Bultje's avatar
Ronald S. Bultje committed
859
          while (j != L[++k]);
860
#endif
861
#endif
862
          leftmv.as_int = left_block_mv(xd, m, k);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
863
864
          abovemv.as_int = above_block_mv(m, k, mis);
          mv_contz = vp9_mv_cont(&leftmv, &abovemv);
865

Ronald S. Bultje's avatar
Ronald S. Bultje committed
866
867
868
869
          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) {
870
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
871
872
            active_section = 11;
#endif
873
            write_nmv(cpi, bc, &blockmv.as_mv, &mi->best_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
874
875
876
877
                      (const nmv_context*) nmvc,
                      xd->allow_high_precision_mv);

            if (mi->second_ref_frame > 0) {
878
              write_nmv(cpi, bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
879
880
881
882
                        &cpi->mb.partition_info->bmi[j].second_mv.as_mv,
                        &mi->best_second_mv,
                        (const nmv_context*) nmvc,
                        xd->allow_high_precision_mv);
Adrian Grange's avatar
Adrian Grange committed
883
            }
John Koleszar's avatar
John Koleszar committed
884
          }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
885
886
887
888
889
890
891
        } while (++j < cpi->mb.partition_info->count);
        break;
      }
      default:
        break;
    }
  }
John Koleszar's avatar
John Koleszar committed
892

893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#if CONFIG_SB8X8
  if (((rf == INTRA_FRAME && mode != I4X4_PRED) ||
       (rf != INTRA_FRAME && mode != SPLITMV)) &&
      pc->txfm_mode == TX_MODE_SELECT &&
      !(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]);
    }
  }
#else
Ronald S. Bultje's avatar
Ronald S. Bultje committed
909
910
911
912
  if (((rf == INTRA_FRAME && mode <= I8X8_PRED) ||
       (rf != INTRA_FRAME && !(mode == SPLITMV &&
                               mi->partitioning == PARTITIONING_4X4))) &&
      pc->txfm_mode == TX_MODE_SELECT &&
913
914
          !(skip_coeff || vp9_segfeature_active(xd, segment_id,
                                                SEG_LVL_SKIP))) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
915
916
917
918
919
    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 (sz != TX_4X4 && mode != I8X8_PRED && mode != SPLITMV) {
      vp9_write(bc, sz != TX_8X8, pc->prob_tx[1]);
920
      if (mi->sb_type >= BLOCK_SIZE_SB32X32 && sz != TX_8X8)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
921
        vp9_write(bc, sz != TX_16X16, pc->prob_tx[2]);
John Koleszar's avatar
John Koleszar committed
922
    }
John Koleszar's avatar
John Koleszar committed
923
  }
924
#endif
John Koleszar's avatar
John Koleszar committed
925
}
926

Ronald S. Bultje's avatar
Ronald S. Bultje committed
927
static void write_mb_modes_kf(const VP9_COMP *cpi,
928
                              MODE_INFO *m,
929
                              vp9_writer *bc, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
930
931
932
933
934
935
  const VP9_COMMON *const c = &cpi->common;
  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  const int mis = c->mode_info_stride;
  const int ym = m->mbmi.mode;
  const int segment_id = m->mbmi.segment_id;
  int skip_coeff;
936