vp9_bitstream.c 69.3 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
11
 */


12
#include "vp9/common/vp9_header.h"
13
#include "vp9/encoder/vp9_encodemv.h"
14
#include "vp9/common/vp9_entropymode.h"
15
#include "vp9/common/vp9_entropymv.h"
16
#include "vp9/common/vp9_findnearmv.h"
17
#include "vp9/common/vp9_tile_common.h"
18
#include "vp9/encoder/vp9_mcomp.h"
19
#include "vp9/common/vp9_systemdependent.h"
John Koleszar's avatar
John Koleszar committed
20
21
#include <assert.h>
#include <stdio.h>
22
#include <limits.h>
23
#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
53
54
55
56
extern unsigned int active_section;
#endif

#ifdef MODE_STATS
int count_mb_seg[4] = { 0, 0, 0, 0 };
#endif

57
58
#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)))
59
60
61
62

#define SEARCH_NEWP
static int update_bits[255];

John Koleszar's avatar
John Koleszar committed
63
64
65
static void compute_update_table() {
  int i;
  for (i = 0; i < 255; i++)
66
    update_bits[i] = vp9_count_term_subexp(i, SUBEXP_PARAM, 255);
67
68
}

John Koleszar's avatar
John Koleszar committed
69
70
71
72
73
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;
74
75
}

John Koleszar's avatar
John Koleszar committed
76
77
78
79
80
static int remap_prob(int v, int m) {
  const int n = 256;
  const int modulus = MODULUS_PARAM;
  int i;
  if ((m << 1) <= n)
81
    i = vp9_recenter_nonneg(v, m) - 1;
John Koleszar's avatar
John Koleszar committed
82
  else
83
    i = vp9_recenter_nonneg(n - 1 - v, n - 1 - m) - 1;
John Koleszar's avatar
John Koleszar committed
84
85
86

  i = split_index(i, n - 1, modulus);
  return i;
87
}
88

89
90
static void write_prob_diff_update(vp9_writer *const bc,
                                   vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
91
  int delp = remap_prob(newp, oldp);
92
  vp9_encode_term_subexp(bc, delp, SUBEXP_PARAM, 255);
93
94
}

95
static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
96
97
  int delp = remap_prob(newp, oldp);
  return update_bits[delp] * 256;
98
}
99

John Koleszar's avatar
John Koleszar committed
100
static void update_mode(
101
  vp9_writer *const bc,
John Koleszar's avatar
John Koleszar committed
102
  int n,
103
104
105
106
  vp9_token tok               [/* n */],
  vp9_tree tree,
  vp9_prob Pnew               [/* n-1 */],
  vp9_prob Pcur               [/* n-1 */],
John Koleszar's avatar
John Koleszar committed
107
108
109
110
111
112
  unsigned int bct            [/* n-1 */] [2],
  const unsigned int num_events[/* n */]
) {
  unsigned int new_b = 0, old_b = 0;
  int i = 0;

113
114
  vp9_tree_probs_from_distribution(n--, tok, tree,
                                   Pnew, bct, num_events);
John Koleszar's avatar
John Koleszar committed
115
116

  do {
117
118
    new_b += cost_branch(bct[i], Pnew[i]);
    old_b += cost_branch(bct[i], Pcur[i]);
John Koleszar's avatar
John Koleszar committed
119
120
121
  } while (++i < n);

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

124
    vp9_write_bit(bc, 1);
John Koleszar's avatar
John Koleszar committed
125

John Koleszar's avatar
John Koleszar committed
126
    do {
127
      const vp9_prob p = Pnew[i];
John Koleszar's avatar
John Koleszar committed
128

129
      vp9_write_literal(bc, Pcur[i] = p ? p : 1, 8);
John Koleszar's avatar
John Koleszar committed
130
131
    } while (++i < n);
  } else
132
    vp9_write_bit(bc, 0);
John Koleszar's avatar
John Koleszar committed
133
134
}

135
static void update_mbintra_mode_probs(VP9_COMP* const cpi,
136
                                      vp9_writer* const bc) {
137
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
138

John Koleszar's avatar
John Koleszar committed
139
  {
140
141
    vp9_prob Pnew   [VP9_YMODES - 1];
    unsigned int bct [VP9_YMODES - 1] [2];
John Koleszar's avatar
John Koleszar committed
142

John Koleszar's avatar
John Koleszar committed
143
    update_mode(
144
      bc, VP9_YMODES, vp9_ymode_encodings, vp9_ymode_tree,
Paul Wilkins's avatar
Paul Wilkins committed
145
      Pnew, cm->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
John Koleszar's avatar
John Koleszar committed
146
    );
147
148
149
    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
150
  }
John Koleszar's avatar
John Koleszar committed
151
152
}

153
154
void vp9_update_skip_probs(VP9_COMP *cpi) {
  VP9_COMMON *const pc = &cpi->common;
John Koleszar's avatar
John Koleszar committed
155
  int k;
Paul Wilkins's avatar
Paul Wilkins committed
156

John Koleszar's avatar
John Koleszar committed
157
  for (k = 0; k < MBSKIP_CONTEXTS; ++k) {
John Koleszar's avatar
John Koleszar committed
158
159
    pc->mbskip_pred_probs[k] = get_binary_prob(cpi->skip_false_count[k],
                                               cpi->skip_true_count[k]);
John Koleszar's avatar
John Koleszar committed
160
  }
Paul Wilkins's avatar
Paul Wilkins committed
161
162
}

163
static void update_switchable_interp_probs(VP9_COMP *cpi,
164
                                           vp9_writer* const bc) {
165
  VP9_COMMON *const pc = &cpi->common;
166
167
  unsigned int branch_ct[32][2];
  int i, j;
168
  for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
169
    vp9_tree_probs_from_distribution(
170
        VP9_SWITCHABLE_FILTERS,
171
        vp9_switchable_interp_encodings, vp9_switchable_interp_tree,
172
        pc->fc.switchable_interp_prob[j], branch_ct,
173
        cpi->switchable_interp_count[j]);
174
    for (i = 0; i < VP9_SWITCHABLE_FILTERS - 1; ++i) {
175
176
      if (pc->fc.switchable_interp_prob[j][i] < 1)
        pc->fc.switchable_interp_prob[j][i] = 1;
177
      vp9_write_literal(bc, pc->fc.switchable_interp_prob[j][i], 8);
178
179
180
181
    }
  }
}

182
// This function updates the reference frame prediction stats
183
184
static void update_refpred_stats(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
185
  int i;
186
  vp9_prob new_pred_probs[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
187
188
189
  int old_cost, new_cost;

  // Set the prediction probability structures to defaults
190
  if (cm->frame_type != KEY_FRAME) {
John Koleszar's avatar
John Koleszar committed
191
192
    // From the prediction counts set the probabilities for each context
    for (i = 0; i < PREDICTION_PROBS; i++) {
John Koleszar's avatar
John Koleszar committed
193
194
      new_pred_probs[i] = get_binary_prob(cpi->ref_pred_count[i][0],
                                          cpi->ref_pred_count[i][1]);
John Koleszar's avatar
John Koleszar committed
195
196
197
198

      // Decide whether or not to update the reference frame probs.
      // Returned costs are in 1/256 bit units.
      old_cost =
199
200
        (cpi->ref_pred_count[i][0] * vp9_cost_zero(cm->ref_pred_probs[i])) +
        (cpi->ref_pred_count[i][1] * vp9_cost_one(cm->ref_pred_probs[i]));
John Koleszar's avatar
John Koleszar committed
201
202

      new_cost =
203
204
        (cpi->ref_pred_count[i][0] * vp9_cost_zero(new_pred_probs[i])) +
        (cpi->ref_pred_count[i][1] * vp9_cost_one(new_pred_probs[i]));
John Koleszar's avatar
John Koleszar committed
205
206
207
208
209
210
211

      // 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;
212
    }
John Koleszar's avatar
John Koleszar committed
213
  }
214
215
}

216
217
218
219
220
221
// 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.
222
223
static void update_inter_mode_probs(VP9_COMMON *cm,
                                    int mode_context[INTER_MODE_CONTEXTS][4]) {
224
  int i, j;
Jim Bankoski's avatar
Jim Bankoski committed
225
  unsigned int (*mv_ref_ct)[4][2];
226
227
228
229
230
231
232
233

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

  mv_ref_ct = cm->fc.mv_ref_ct;

  for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
    for (j = 0; j < 4; j++) {
234
      int new_prob, old_cost, new_cost;
235
236
237

      // 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]);
238
      new_prob = get_binary_prob(mv_ref_ct[i][j][0], mv_ref_ct[i][j][1]);
239
240
241
242
243
244
245
246
247
248
249
      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;
      }
    }
  }
}
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

#if CONFIG_NEW_MVREF
static void update_mv_ref_probs(VP9_COMP *cpi,
                                int mvref_probs[MAX_REF_FRAMES]
                                               [MAX_MV_REF_CANDIDATES-1]) {
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
  int rf;     // Reference frame
  int ref_c;  // Motion reference candidate
  int node;   // Probability node index

  for (rf = 0; rf < MAX_REF_FRAMES; ++rf) {
    int count = 0;

    // Skip the dummy entry for intra ref frame.
    if (rf == INTRA_FRAME) {
      continue;
    }

    // Sum the counts for all candidates
    for (ref_c = 0; ref_c < MAX_MV_REF_CANDIDATES; ++ref_c) {
      count += cpi->mb_mv_ref_count[rf][ref_c];
    }

    // Calculate the tree node probabilities
    for (node = 0; node < MAX_MV_REF_CANDIDATES-1; ++node) {
      int new_prob, old_cost, new_cost;
      unsigned int branch_cnts[2];

      // How many hits on each branch at this node
      branch_cnts[0] = cpi->mb_mv_ref_count[rf][node];
      branch_cnts[1] = count - cpi->mb_mv_ref_count[rf][node];

      // Work out cost of coding branches with the old and optimal probability
      old_cost = cost_branch256(branch_cnts, xd->mb_mv_ref_probs[rf][node]);
      new_prob = get_prob(branch_cnts[0], count);
      new_cost = cost_branch256(branch_cnts, new_prob);

      // Take current 0 branch cases out of residual count
      count -= cpi->mb_mv_ref_count[rf][node];

      if ((new_cost + VP9_MV_REF_UPDATE_COST) <= old_cost) {
        mvref_probs[rf][node] = new_prob;
      } else {
        mvref_probs[rf][node] = xd->mb_mv_ref_probs[rf][node];
      }
    }
  }
}
#endif

300
301
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
302
303
}

304
305
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
306
307
}

308
309
310
311
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);
}

312
313
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
314
315
}

316
317
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
318
}
Yaowu Xu's avatar
Yaowu Xu committed
319

320
321
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
322
323
324
}


325
static void write_bmode(vp9_writer *bc, int m, const vp9_prob *p) {
326
327
328
329
#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
330
  write_token(bc, vp9_bmode_tree, p, vp9_bmode_encodings + m);
John Koleszar's avatar
John Koleszar committed
331
332
}

333
334
335
336
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);
}

337
static void write_split(vp9_writer *bc, int x, const vp9_prob *p) {
338
339
  write_token(
    bc, vp9_mbsplit_tree, p, vp9_mbsplit_encodings + x);
John Koleszar's avatar
John Koleszar committed
340
341
}

342
static int prob_update_savings(const unsigned int *ct,
343
344
345
346
347
                               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;
John Koleszar's avatar
John Koleszar committed
348
  return (old_b - new_b - update_b);
349
350
}

351
static int prob_diff_update_savings(const unsigned int *ct,
352
353
354
355
                                    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);
John Koleszar's avatar
John Koleszar committed
356
  const int update_b = (newp == oldp ? 0 :
357
                        prob_diff_update_cost(newp, oldp) + vp9_cost_upd256);
John Koleszar's avatar
John Koleszar committed
358
  return (old_b - new_b - update_b);
359
360
361
}

static int prob_diff_update_savings_search(const unsigned int *ct,
362
363
364
                                           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
365
  int new_b, update_b, savings, bestsavings, step;
366
  vp9_prob newp, bestnewp;
John Koleszar's avatar
John Koleszar committed
367
368
369
370
371
372

  bestsavings = 0;
  bestnewp = oldp;

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

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

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

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

414
415
416
417
418
419
    if (t == EOSB_TOKEN)
    {
      ++p;
      break;
    }

John Koleszar's avatar
John Koleszar committed
420
421
422
423
424
    /* skip one or two nodes */
    if (p->skip_eob_node) {
      n -= p->skip_eob_node;
      i = 2 * p->skip_eob_node;
    }
John Koleszar's avatar
John Koleszar committed
425

John Koleszar's avatar
John Koleszar committed
426
427
    do {
      const int bb = (v >> --n) & 1;
428
      encode_bool(bc, bb, pp[i >> 1]);
429
      i = vp9_coef_tree[i + bb];
John Koleszar's avatar
John Koleszar committed
430
    } while (n);
John Koleszar's avatar
John Koleszar committed
431
432


John Koleszar's avatar
John Koleszar committed
433
434
    if (b->base_val) {
      const int e = p->Extra, L = b->Len;
John Koleszar's avatar
John Koleszar committed
435

John Koleszar's avatar
John Koleszar committed
436
437
438
439
440
      if (L) {
        const unsigned char *pp = b->prob;
        int v = e >> 1;
        int n = L;              /* number of bits in v, assumed nonzero */
        int i = 0;
John Koleszar's avatar
John Koleszar committed
441

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

449
      encode_bool(bc, e & 1, 128);
John Koleszar's avatar
John Koleszar committed
450
    }
John Koleszar's avatar
John Koleszar committed
451
452
453
    ++p;
  }

454
  *tp = p;
John Koleszar's avatar
John Koleszar committed
455
456
}

John Koleszar's avatar
John Koleszar committed
457
458
static void write_partition_size(unsigned char *cx_data, int size) {
  signed char csize;
John Koleszar's avatar
John Koleszar committed
459

John Koleszar's avatar
John Koleszar committed
460
461
462
463
464
465
  csize = size & 0xff;
  *cx_data = csize;
  csize = (size >> 8) & 0xff;
  *(cx_data + 1) = csize;
  csize = (size >> 16) & 0xff;
  *(cx_data + 2) = csize;
John Koleszar's avatar
John Koleszar committed
466
467
468
469
470

}

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

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

John Koleszar's avatar
John Koleszar committed
489
490
static void write_sub_mv_ref
(
491
  vp9_writer *bc, B_PREDICTION_MODE m, const vp9_prob *p
John Koleszar's avatar
John Koleszar committed
492
) {
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
}

511
#if CONFIG_NEW_MVREF
512
513
static void vp9_write_mv_ref_id(vp9_writer *w,
                                vp9_prob * ref_id_probs,
514
515
516
517
                                int mv_ref_id) {
  // Encode the index for the MV reference.
  switch (mv_ref_id) {
    case 0:
518
      vp9_write(w, 0, ref_id_probs[0]);
519
520
      break;
    case 1:
521
522
      vp9_write(w, 1, ref_id_probs[0]);
      vp9_write(w, 0, ref_id_probs[1]);
523
524
      break;
    case 2:
525
526
527
      vp9_write(w, 1, ref_id_probs[0]);
      vp9_write(w, 1, ref_id_probs[1]);
      vp9_write(w, 0, ref_id_probs[2]);
528
529
      break;
    case 3:
530
531
532
      vp9_write(w, 1, ref_id_probs[0]);
      vp9_write(w, 1, ref_id_probs[1]);
      vp9_write(w, 1, ref_id_probs[2]);
533
534
535
536
537
538
539
540
541
542
      break;

      // TRAP.. This should not happen
    default:
      assert(0);
      break;
  }
}
#endif

Paul Wilkins's avatar
Paul Wilkins committed
543
544
// 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.
545
static void write_mb_segid(vp9_writer *bc,
Paul Wilkins's avatar
Paul Wilkins committed
546
                           const MB_MODE_INFO *mi, const MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
547
  // Encode the MB segment id.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
548
  int seg_id = mi->segment_id;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
549

Paul Wilkins's avatar
Paul Wilkins committed
550
  if (xd->segmentation_enabled && xd->update_mb_segmentation_map) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
551
    switch (seg_id) {
John Koleszar's avatar
John Koleszar committed
552
      case 0:
553
554
        vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[1]);
John Koleszar's avatar
John Koleszar committed
555
556
        break;
      case 1:
557
558
        vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[1]);
John Koleszar's avatar
John Koleszar committed
559
560
        break;
      case 2:
561
562
        vp9_write(bc, 1, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[2]);
John Koleszar's avatar
John Koleszar committed
563
564
        break;
      case 3:
565
566
        vp9_write(bc, 1, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 1, xd->mb_segment_tree_probs[2]);
John Koleszar's avatar
John Koleszar committed
567
568
569
570
        break;

        // TRAP.. This should not happen
      default:
571
572
        vp9_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp9_write(bc, 0, xd->mb_segment_tree_probs[1]);
John Koleszar's avatar
John Koleszar committed
573
        break;
John Koleszar's avatar
John Koleszar committed
574
    }
John Koleszar's avatar
John Koleszar committed
575
  }
John Koleszar's avatar
John Koleszar committed
576
577
}

578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
static void write_mb_segid_except(VP9_COMMON *cm,
                                  vp9_writer *bc,
                                  const MB_MODE_INFO *mi,
                                  const MACROBLOCKD *xd,
                                  int mb_row, int mb_col) {
  // Encode the MB segment id.
  int seg_id = mi->segment_id;
  int pred_seg_id = vp9_get_pred_mb_segid(cm, xd,
                                          mb_row * cm->mb_cols + mb_col);
  const vp9_prob *p = xd->mb_segment_tree_probs;
  const vp9_prob p1 = xd->mb_segment_mispred_tree_probs[pred_seg_id];

  if (xd->segmentation_enabled && xd->update_mb_segmentation_map) {
    vp9_write(bc, seg_id >= 2, p1);
    if (pred_seg_id >= 2 && seg_id < 2) {
      vp9_write(bc, seg_id == 1, p[1]);
    } else if (pred_seg_id < 2 && seg_id >= 2) {
      vp9_write(bc, seg_id == 3, p[2]);
    }
  }
}

Paul Wilkins's avatar
Paul Wilkins committed
600
// This function encodes the reference frame
601
static void encode_ref_frame(vp9_writer *const bc,
602
                             VP9_COMMON *const cm,
John Koleszar's avatar
John Koleszar committed
603
604
605
606
607
                             MACROBLOCKD *xd,
                             int segment_id,
                             MV_REFERENCE_FRAME rf) {
  int seg_ref_active;
  int seg_ref_count = 0;
608
609
610
  seg_ref_active = vp9_segfeature_active(xd,
                                         segment_id,
                                         SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
611
612

  if (seg_ref_active) {
613
614
615
616
    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
617
618
619
620
621
622
623
  }

  // 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;
624
    vp9_prob pred_prob;
John Koleszar's avatar
John Koleszar committed
625
626
627
    MV_REFERENCE_FRAME pred_rf;

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

    // Get the predicted value.
Paul Wilkins's avatar
Paul Wilkins committed
631
    pred_rf = vp9_get_pred_ref(cm, xd);
John Koleszar's avatar
John Koleszar committed
632
633
634
635
636

    // 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
637
    vp9_set_pred_flag(xd, PRED_REF, prediction_flag);
638
    vp9_write(bc, prediction_flag, pred_prob);
John Koleszar's avatar
John Koleszar committed
639
640
641

    // If not predicted correctly then code value explicitly
    if (!prediction_flag) {
642
      vp9_prob mod_refprobs[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
643
644
645
646
647
648
649
650

      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] *=
651
          vp9_check_segref(xd, segment_id, INTRA_FRAME);
John Koleszar's avatar
John Koleszar committed
652
        mod_refprobs[LAST_FRAME] *=
653
          vp9_check_segref(xd, segment_id, LAST_FRAME);
John Koleszar's avatar
John Koleszar committed
654
        mod_refprobs[GOLDEN_FRAME] *=
655
656
          (vp9_check_segref(xd, segment_id, GOLDEN_FRAME) *
           vp9_check_segref(xd, segment_id, ALTREF_FRAME));
John Koleszar's avatar
John Koleszar committed
657
658
659
      }

      if (mod_refprobs[0]) {
660
        vp9_write(bc, (rf != INTRA_FRAME), mod_refprobs[0]);
John Koleszar's avatar
John Koleszar committed
661
662
663
664
665
      }

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

John Koleszar's avatar
John Koleszar committed
669
670
        if (rf != LAST_FRAME) {
          if (mod_refprobs[2]) {
671
            vp9_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]);
John Koleszar's avatar
John Koleszar committed
672
          }
673
        }
John Koleszar's avatar
John Koleszar committed
674
      }
Paul Wilkins's avatar
Paul Wilkins committed
675
    }
John Koleszar's avatar
John Koleszar committed
676
  }
Paul Wilkins's avatar
Paul Wilkins committed
677

John Koleszar's avatar
John Koleszar committed
678
679
  // 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
680
}
John Koleszar's avatar
John Koleszar committed
681

682
// Update the probabilities used to encode reference frame data
683
684
static void update_ref_probs(VP9_COMP *const cpi) {
  VP9_COMMON *const cm = &cpi->common;
685

John Koleszar's avatar
John Koleszar committed
686
687
688
689
  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];
690

John Koleszar's avatar
John Koleszar committed
691
692
693
  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]);
694

John Koleszar's avatar
John Koleszar committed
695
696
  // Compute a modified set of probabilities to use when prediction of the
  // reference frame fails
Paul Wilkins's avatar
Paul Wilkins committed
697
  vp9_compute_mod_refprobs(cm);
698
699
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
700
701
702
static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m,
                                vp9_writer *bc,
                                int mb_rows_left, int mb_cols_left) {
703
  VP9_COMMON *const pc = &cpi->common;
704
  const nmv_context *nmvc = &pc->fc.nmvc;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
705
706
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
707
  const int mis = pc->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
708
709
710
711
712
713
  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;
  const int mb_size = 1 << mi->sb_type;
  int skip_coeff;
Adrian Grange's avatar
Adrian Grange committed
714

Ronald S. Bultje's avatar
Ronald S. Bultje committed
715
716
717
718
  int mb_row = pc->mb_rows - mb_rows_left;
  int mb_col = pc->mb_cols - mb_cols_left;
  xd->prev_mode_info_context = pc->prev_mi + (m - pc->mi);
  x->partition_info = x->pi + (m - pc->mi);
719

Ronald S. Bultje's avatar
Ronald S. Bultje committed
720
721
722
723
724
725
726
  // Distance of Mb to the various image edges.
  // These specified to 8th pel as they are always compared to MV
  // values that are in 1/8th pel units
  xd->mb_to_left_edge = -((mb_col * 16) << 3);
  xd->mb_to_top_edge = -((mb_row * 16)) << 3;
  xd->mb_to_right_edge = ((pc->mb_cols - mb_size - mb_col) * 16) << 3;
  xd->mb_to_bottom_edge = ((pc->mb_rows - mb_size - mb_row) * 16) << 3;
727

Ronald S. Bultje's avatar
Ronald S. Bultje committed
728
729
#ifdef ENTROPY_STATS
  active_section = 9;
730
#endif
731

Ronald S. Bultje's avatar
Ronald S. Bultje committed
732
733
734
735
736
  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
737

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
741
742
      // If the mb segment id wasn't predicted code explicitly
      if (!prediction_flag)
743
        write_mb_segid_except(pc, bc, mi, &cpi->mb.e_mbd, mb_row, mb_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
744
745
746
747
748
    } else {
      // Normal unpredicted coding
      write_mb_segid(bc, mi, &cpi->mb.e_mbd);
    }
  }
749

Ronald S. Bultje's avatar
Ronald S. Bultje committed
750
751
  if (!pc->mb_no_coeff_skip) {
    skip_coeff = 0;
Paul Wilkins's avatar
Paul Wilkins committed
752
  } else if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
753
754
755
756
757
758
759
760
761
762
763
764
765
    skip_coeff = 1;
  } else {
    const int nmbs = mb_size;
    const int xmbs = MIN(nmbs, mb_cols_left);
    const int ymbs = MIN(nmbs, mb_rows_left);
    int x, y;

    skip_coeff = 1;
    for (y = 0; y < ymbs; y++) {
      for (x = 0; x < xmbs; x++) {
        skip_coeff = skip_coeff && m[y * mis + x].mbmi.mb_skip_coeff;
      }
    }
John Koleszar's avatar
John Koleszar committed
766

Ronald S. Bultje's avatar
Ronald S. Bultje committed
767
768
769
    vp9_write(bc, skip_coeff,
              vp9_get_pred_prob(pc, xd, PRED_MBSKIP));
  }
John Koleszar's avatar
John Koleszar committed
770

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
774
  if (rf == INTRA_FRAME) {
775
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
776
    active_section = 6;
777
#endif
Paul Wilkins's avatar
Paul Wilkins committed
778

Paul Wilkins's avatar
Paul Wilkins committed
779
780
781
782
783
    if (m->mbmi.sb_type)
      write_sb_ymode(bc, mode, pc->fc.sb_ymode_prob);
    else
      write_ymode(bc, mode, pc->fc.ymode_prob);

Ronald S. Bultje's avatar
Ronald S. Bultje committed
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
    if (mode == B_PRED) {
      int j = 0;
      do {
        write_bmode(bc, m->bmi[j].as_mode.first,
                    pc->fc.bmode_prob);
      } while (++j < 16);
    }
    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);
    } else {
      write_uv_mode(bc, mi->uv_mode,
                    pc->fc.uv_mode_prob[mode]);
    }
  } else {
    vp9_prob mv_ref_p[VP9_MVREFS - 1];
806

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

John Koleszar's avatar
John Koleszar committed
809
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
810
    active_section = 3;
John Koleszar's avatar
John Koleszar committed
811
812
#endif

813
    // If segment skip is not enabled code the mode.
Paul Wilkins's avatar
Paul Wilkins committed
814
    if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
815
816
      if (mi->sb_type) {
        write_sb_mv_ref(bc, mode, mv_ref_p);
817
      } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
818
819
820
821
        write_mv_ref(bc, mode, mv_ref_p);
      }
      vp9_accum_mv_refs(&cpi->common, mode, mi->mb_mode_context[rf]);
    }
822

Ronald S. Bultje's avatar
Ronald S. Bultje committed
823
824
825
826
827
828
829
830
831
832
833
    if (mode >= NEARESTMV && mode <= SPLITMV) {
      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);
      }
    }
834

Ronald S. Bultje's avatar
Ronald S. Bultje committed
835
836
837
838
839
840
    // 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));
    }
841
#if CONFIG_COMP_INTERINTRA_PRED
Ronald S. Bultje's avatar
Ronald S. Bultje committed
842
843
844
845
846
847
848
849
850
851
852
853
854
    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);
855
#if SEPARATE_INTERINTRA_UV
Ronald S. Bultje's avatar
Ronald S. Bultje committed
856
857
        write_uv_mode(bc, mi->interintra_uv_mode,
                      pc->fc.uv_mode_prob[mi->interintra_mode]);
858
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
859
860
      }
    }
861
#endif
John Koleszar's avatar
John Koleszar committed
862

863
#if CONFIG_NEW_MVREF
Ronald S. Bultje's avatar
Ronald S. Bultje committed
864
865
866
867
868
869
870
871
872
873
874
875
876
    // if ((mode == NEWMV) || (mode == SPLITMV)) {
    if (mode == NEWMV) {
      // Encode the index of the choice.
      vp9_write_mv_ref_id(bc,
                          xd->mb_mv_ref_probs[rf], mi->best_index);

      if (mi->second_ref_frame > 0) {
        // Encode the index of the choice.
        vp9_write_mv_ref_id(
                            bc, xd->mb_mv_ref_probs[mi->second_ref_frame],
                            mi->best_second_index);
      }
    }
877
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
878
879
880

    switch (mode) { /* new, split require MVs */
      case NEWMV:
881
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
882
        active_section = 5;
883
#endif
884
        write_nmv(cpi, bc, &mi->mv[0].as_mv, &mi->best_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
885
886
                  (const nmv_context*) nmvc,
                  xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
887

Ronald S. Bultje's avatar
Ronald S. Bultje committed
888
        if (mi->second_ref_frame > 0) {
889
          write_nmv(cpi, bc, &mi->mv[1].as_mv, &mi->best_second_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
890
891
892
893
894
895
                    (const nmv_context*) nmvc,
                    xd->allow_high_precision_mv);
        }
        break;
      case SPLITMV: {
        int j = 0;
John Koleszar's avatar
John Koleszar committed
896

897
#ifdef MODE_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
898
        ++count_mb_seg[mi->partitioning];
899
#endif
John Koleszar's avatar
John Koleszar committed
900

Ronald S. Bultje's avatar
Ronald S. Bultje committed
901
902
        write_split(bc, mi->partitioning, cpi->common.fc.mbsplit_prob);
        cpi->mbsplit_count[mi->partitioning]++;
Adrian Grange's avatar
Adrian Grange committed
903

Ronald S. Bultje's avatar
Ronald S. Bultje committed
904
905
906
907
908
909
910
911
912
913
        do {
          B_PREDICTION_MODE blockmode;
          int_mv blockmv;
          const int *const  L = vp9_mbsplits[mi->partitioning];
          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;
914
#if CONFIG_DEBUG
Ronald S. Bultje's avatar
Ronald S. Bultje committed
915
916
917
          while (j != L[++k])
            if (k >= 16)
              assert(0);
918
#else
Ronald S. Bultje's avatar
Ronald S. Bultje committed
919
          while (j != L[++k]);
920
#endif
921
          leftmv.as_int = left_block_mv(xd, m, k);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
922
923
          abovemv.as_int = above_block_mv(m, k, mis);
          mv_contz = vp9_mv_cont(&leftmv, &abovemv);
924

Ronald S. Bultje's avatar
Ronald S. Bultje committed
925
926
927
928
          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) {
929
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
930
931
            active_section = 11;
#endif
932
            write_nmv(cpi, bc, &blockmv.as_mv, &mi->best_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
933
934
935
936
                      (const nmv_context*) nmvc,
                      xd->allow_high_precision_mv);

            if (mi->second_ref_frame > 0) {
937
              write_nmv(cpi, bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
938
939
940
941
                        &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
942
            }
John Koleszar's avatar
John Koleszar committed
943
          }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
944
945
946
947
948
949
950
        } while (++j < cpi->mb.partition_info->count);
        break;
      }
      default:
        break;
    }
  }
John Koleszar's avatar
John Koleszar committed
951

Ronald S. Bultje's avatar
Ronald S. Bultje committed
952
953
954
955
956
  if (((rf == INTRA_FRAME && mode <= I8X8_PRED) ||
       (rf != INTRA_FRAME && !(mode == SPLITMV &&
                               mi->partitioning == PARTITIONING_4X4))) &&
      pc->txfm_mode == TX_MODE_SELECT &&
      !((pc->mb_no_coeff_skip && skip_coeff) ||
Paul Wilkins's avatar
Paul Wilkins committed
957
        (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)))) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
958
959
960
961
962
963
964
    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]);
      if (mi->sb_type && sz != TX_8X8)
        vp9_write(bc, sz != TX_16X16, pc->prob_tx[2]);
John Koleszar's avatar
John Koleszar committed
965
    }
John Koleszar's avatar
John Koleszar committed
966
  }
John Koleszar's avatar
John Koleszar committed
967
}
968

Ronald S. Bultje's avatar
Ronald S. Bultje committed
969
970
971
972
973
974
975
976
977
978
static void write_mb_modes_kf(const VP9_COMP *cpi,
                              const MODE_INFO *m,
                              vp9_writer *bc,
                              int mb_rows_left, int mb_cols_left) {
  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;
979
980
981
982
983

  if (xd->update_mb_segmentation_map) {
    write_mb_segid(bc, &m->mbmi, xd);
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
984
985
  if (!c->mb_no_coeff_skip) {
    skip_coeff = 0;
Paul Wilkins's avatar
Paul Wilkins committed
986
  } else if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
    skip_coeff = 1;
  } else {
    const int nmbs = 1 << m->mbmi.sb_type;
    const int xmbs = MIN(nmbs, mb_cols_left);
    const int ymbs = MIN(nmbs, mb_rows_left);
    int x, y;

    skip_coeff = 1;
    for (y = 0; y < ymbs; y++) {
      for (x = 0; x < xmbs; x++) {
        skip_coeff = skip_coeff &a