vp9_bitstream.c 97.2 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
64
65
66
67
68
69
70
#if CONFIG_CODE_NONZEROCOUNT
#ifdef NZC_STATS
unsigned int nzc_stats_4x4[MAX_NZC_CONTEXTS][REF_TYPES][BLOCK_TYPES]
                          [NZC4X4_TOKENS];
unsigned int nzc_stats_8x8[MAX_NZC_CONTEXTS][REF_TYPES][BLOCK_TYPES]
                          [NZC8X8_TOKENS];
unsigned int nzc_stats_16x16[MAX_NZC_CONTEXTS][REF_TYPES][BLOCK_TYPES]
                          [NZC16X16_TOKENS];
unsigned int nzc_stats_32x32[MAX_NZC_CONTEXTS][REF_TYPES][BLOCK_TYPES]
                          [NZC32X32_TOKENS];
unsigned int nzc_pcat_stats[MAX_NZC_CONTEXTS][NZC_TOKENS_EXTRA]
                          [NZC_BITS_EXTRA][2];
void init_nzcstats();
void update_nzcstats(VP9_COMMON *const cm);
void print_nzcstats();
#endif
#endif

John Koleszar's avatar
John Koleszar committed
71
72
73
74
#ifdef MODE_STATS
int count_mb_seg[4] = { 0, 0, 0, 0 };
#endif

75
76
#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)))
77
78
79
80

#define SEARCH_NEWP
static int update_bits[255];

81
82
83
84
85
86
87
88
89
90
91
92
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
93
94
95
static void compute_update_table() {
  int i;
  for (i = 0; i < 255; i++)
96
    update_bits[i] = vp9_count_term_subexp(i, SUBEXP_PARAM, 255);
97
98
}

John Koleszar's avatar
John Koleszar committed
99
100
101
102
103
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;
104
105
}

John Koleszar's avatar
John Koleszar committed
106
107
108
109
110
static int remap_prob(int v, int m) {
  const int n = 256;
  const int modulus = MODULUS_PARAM;
  int i;
  if ((m << 1) <= n)
111
    i = vp9_recenter_nonneg(v, m) - 1;
John Koleszar's avatar
John Koleszar committed
112
  else
113
    i = vp9_recenter_nonneg(n - 1 - v, n - 1 - m) - 1;
John Koleszar's avatar
John Koleszar committed
114
115
116

  i = split_index(i, n - 1, modulus);
  return i;
117
}
118

119
120
static void write_prob_diff_update(vp9_writer *const bc,
                                   vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
121
  int delp = remap_prob(newp, oldp);
122
  vp9_encode_term_subexp(bc, delp, SUBEXP_PARAM, 255);
123
124
}

125
static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
126
127
  int delp = remap_prob(newp, oldp);
  return update_bits[delp] * 256;
128
}
129

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

143
144
  vp9_tree_probs_from_distribution(tree, Pnew, bct, num_events, 0);
  n--;
John Koleszar's avatar
John Koleszar committed
145
146

  do {
147
148
    new_b += cost_branch(bct[i], Pnew[i]);
    old_b += cost_branch(bct[i], Pcur[i]);
John Koleszar's avatar
John Koleszar committed
149
150
151
  } while (++i < n);

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

154
    vp9_write_bit(bc, 1);
John Koleszar's avatar
John Koleszar committed
155

John Koleszar's avatar
John Koleszar committed
156
    do {
157
      const vp9_prob p = Pnew[i];
John Koleszar's avatar
John Koleszar committed
158

159
      vp9_write_literal(bc, Pcur[i] = p ? p : 1, 8);
John Koleszar's avatar
John Koleszar committed
160
161
    } while (++i < n);
  } else
162
    vp9_write_bit(bc, 0);
John Koleszar's avatar
John Koleszar committed
163
164
}

165
static void update_mbintra_mode_probs(VP9_COMP* const cpi,
166
                                      vp9_writer* const bc) {
167
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
168

John Koleszar's avatar
John Koleszar committed
169
  {
170
171
    vp9_prob Pnew   [VP9_YMODES - 1];
    unsigned int bct [VP9_YMODES - 1] [2];
John Koleszar's avatar
John Koleszar committed
172

John Koleszar's avatar
John Koleszar committed
173
    update_mode(
174
      bc, VP9_YMODES, vp9_ymode_encodings, vp9_ymode_tree,
Paul Wilkins's avatar
Paul Wilkins committed
175
      Pnew, cm->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
John Koleszar's avatar
John Koleszar committed
176
    );
177
178
179
    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
180
  }
John Koleszar's avatar
John Koleszar committed
181
182
}

183
184
void vp9_update_skip_probs(VP9_COMP *cpi) {
  VP9_COMMON *const pc = &cpi->common;
John Koleszar's avatar
John Koleszar committed
185
  int k;
Paul Wilkins's avatar
Paul Wilkins committed
186

John Koleszar's avatar
John Koleszar committed
187
  for (k = 0; k < MBSKIP_CONTEXTS; ++k) {
John Koleszar's avatar
John Koleszar committed
188
189
    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
190
  }
Paul Wilkins's avatar
Paul Wilkins committed
191
192
}

193
static void update_switchable_interp_probs(VP9_COMP *cpi,
194
                                           vp9_writer* const bc) {
195
  VP9_COMMON *const pc = &cpi->common;
196
197
  unsigned int branch_ct[32][2];
  int i, j;
198
  for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
199
    vp9_tree_probs_from_distribution(
200
        vp9_switchable_interp_tree,
201
        pc->fc.switchable_interp_prob[j], branch_ct,
202
        cpi->switchable_interp_count[j], 0);
203
    for (i = 0; i < VP9_SWITCHABLE_FILTERS - 1; ++i) {
204
205
      if (pc->fc.switchable_interp_prob[j][i] < 1)
        pc->fc.switchable_interp_prob[j][i] = 1;
206
      vp9_write_prob(bc, pc->fc.switchable_interp_prob[j][i]);
207
208
209
210
    }
  }
}

211
// This function updates the reference frame prediction stats
212
213
static void update_refpred_stats(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
214
  int i;
215
  vp9_prob new_pred_probs[PREDICTION_PROBS];
John Koleszar's avatar
John Koleszar committed
216
217
218
  int old_cost, new_cost;

  // Set the prediction probability structures to defaults
219
  if (cm->frame_type != KEY_FRAME) {
John Koleszar's avatar
John Koleszar committed
220
221
    // From the prediction counts set the probabilities for each context
    for (i = 0; i < PREDICTION_PROBS; i++) {
John Koleszar's avatar
John Koleszar committed
222
223
      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
224
225
226
227

      // Decide whether or not to update the reference frame probs.
      // Returned costs are in 1/256 bit units.
      old_cost =
228
229
        (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
230
231

      new_cost =
232
233
        (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
234
235
236
237
238
239
240

      // 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;
241
    }
John Koleszar's avatar
John Koleszar committed
242
  }
243
244
}

245
246
247
248
249
250
// 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.
251
252
static void update_inter_mode_probs(VP9_COMMON *cm,
                                    int mode_context[INTER_MODE_CONTEXTS][4]) {
253
  int i, j;
Jim Bankoski's avatar
Jim Bankoski committed
254
  unsigned int (*mv_ref_ct)[4][2];
255
256
257
258
259
260
261
262

  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++) {
263
      int new_prob, old_cost, new_cost;
264
265
266

      // 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]);
267
      new_prob = get_binary_prob(mv_ref_ct[i][j][0], mv_ref_ct[i][j][1]);
268
269
270
271
272
273
274
275
276
277
278
      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;
      }
    }
  }
}
279

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

284
285
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
286
287
}

288
289
290
291
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);
}

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

296
297
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
298
}
Yaowu Xu's avatar
Yaowu Xu committed
299

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


305
static void write_bmode(vp9_writer *bc, int m, const vp9_prob *p) {
306
307
308
309
#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
310
  write_token(bc, vp9_bmode_tree, p, vp9_bmode_encodings + m);
John Koleszar's avatar
John Koleszar committed
311
312
}

313
314
315
316
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);
}

317
static void write_split(vp9_writer *bc, int x, const vp9_prob *p) {
318
319
  write_token(
    bc, vp9_mbsplit_tree, p, vp9_mbsplit_encodings + x);
John Koleszar's avatar
John Koleszar committed
320
321
}

322
static int prob_update_savings(const unsigned int *ct,
323
324
325
326
327
                               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
328
  return (old_b - new_b - update_b);
329
330
}

331
static int prob_diff_update_savings(const unsigned int *ct,
332
333
334
335
                                    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
336
  const int update_b = (newp == oldp ? 0 :
337
                        prob_diff_update_cost(newp, oldp) + vp9_cost_upd256);
John Koleszar's avatar
John Koleszar committed
338
  return (old_b - new_b - update_b);
339
340
341
}

static int prob_diff_update_savings_search(const unsigned int *ct,
342
343
344
                                           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
345
  int new_b, update_b, savings, bestsavings, step;
346
  vp9_prob newp, bestnewp;
John Koleszar's avatar
John Koleszar committed
347
348
349
350
351
352

  bestsavings = 0;
  bestnewp = oldp;

  step = (*bestp > oldp ? -1 : 1);
  for (newp = *bestp; newp != oldp; newp += step) {
353
354
    new_b = cost_branch256(ct, newp);
    update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
John Koleszar's avatar
John Koleszar committed
355
356
357
358
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
359
    }
John Koleszar's avatar
John Koleszar committed
360
361
362
  }
  *bestp = bestnewp;
  return bestsavings;
363
364
}

365
366
367
368
369
#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,
370
                                                 int b, int r, int q) {
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  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

402
403
404
405
406
407
408
409
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);
410
    vp9_write_prob(bc, newp);
411
412
413
414
415
416
    *oldp = newp;
  } else {
    vp9_write(bc, 0, upd);
  }
}

417
static void pack_mb_tokens(vp9_writer* const bc,
418
419
420
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
421

John Koleszar's avatar
John Koleszar committed
422
  while (p < stop) {
423
    const int t = p->token;
424
    const struct vp9_token *const a = vp9_coef_encodings + t;
425
    const vp9_extra_bit_struct *const b = vp9_extra_bits + t;
John Koleszar's avatar
John Koleszar committed
426
427
428
    int i = 0;
    const unsigned char *pp = p->context_tree;
    int v = a->value;
429
    int n = a->len;
John Koleszar's avatar
John Koleszar committed
430

431
432
433
434
435
436
    if (t == EOSB_TOKEN)
    {
      ++p;
      break;
    }

John Koleszar's avatar
John Koleszar committed
437
438
439
440
441
    /* 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
442

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


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

John Koleszar's avatar
John Koleszar committed
453
454
455
456
457
      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
458

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

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

471
  *tp = p;
John Koleszar's avatar
John Koleszar committed
472
473
474
475
}

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

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

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

505
506
static void write_nmv(VP9_COMP *cpi, vp9_writer *bc,
                      const MV *mv, const int_mv *ref,
John Koleszar's avatar
John Koleszar committed
507
                      const nmv_context *nmvc, int usehp) {
508
509
510
511
  MV e;
  e.row = mv->row - ref->as_mv.row;
  e.col = mv->col - ref->as_mv.col;

512
513
  vp9_encode_nmv(bc, &e, &ref->as_mv, nmvc);
  vp9_encode_nmv_fp(bc, &e, &ref->as_mv, nmvc, usehp);
514
515
}

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

Paul Wilkins's avatar
Paul Wilkins committed
523
  if (xd->segmentation_enabled && xd->update_mb_segmentation_map) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
524
    switch (seg_id) {
John Koleszar's avatar
John Koleszar committed
525
      case 0:
526
527
        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
528
529
        break;
      case 1:
530
531
        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
532
533
        break;
      case 2:
534
535
        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
536
537
        break;
      case 3:
538
539
        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
540
541
542
543
        break;

        // TRAP.. This should not happen
      default:
544
545
        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
546
        break;
John Koleszar's avatar
John Koleszar committed
547
    }
John Koleszar's avatar
John Koleszar committed
548
  }
John Koleszar's avatar
John Koleszar committed
549
550
}

551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
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
573
// This function encodes the reference frame
574
static void encode_ref_frame(vp9_writer *const bc,
575
                             VP9_COMMON *const cm,
John Koleszar's avatar
John Koleszar committed
576
577
578
579
580
                             MACROBLOCKD *xd,
                             int segment_id,
                             MV_REFERENCE_FRAME rf) {
  int seg_ref_active;
  int seg_ref_count = 0;
581
582
583
  seg_ref_active = vp9_segfeature_active(xd,
                                         segment_id,
                                         SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
584
585

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

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

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

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

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

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

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

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

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

John Koleszar's avatar
John Koleszar committed
642
643
        if (rf != LAST_FRAME) {
          if (mod_refprobs[2]) {
644
            vp9_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]);
John Koleszar's avatar
John Koleszar committed
645
          }
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
  }
Paul Wilkins's avatar
Paul Wilkins committed
650

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

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

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

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

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
673
674
675
static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m,
                                vp9_writer *bc,
                                int mb_rows_left, int mb_cols_left) {
676
  VP9_COMMON *const pc = &cpi->common;
677
  const nmv_context *nmvc = &pc->fc.nmvc;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
678
679
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
John Koleszar's avatar
John Koleszar committed
680
  const int mis = pc->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
681
682
683
684
  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;
685
686
  const int bw = 1 << mb_width_log2(mi->sb_type);
  const int bh = 1 << mb_height_log2(mi->sb_type);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
687
  int skip_coeff;
Adrian Grange's avatar
Adrian Grange committed
688

Ronald S. Bultje's avatar
Ronald S. Bultje committed
689
690
691
692
  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);
693

Ronald S. Bultje's avatar
Ronald S. Bultje committed
694
695
696
  // 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
697

698
699
  set_mb_row(pc, xd, mb_row, bh);
  set_mb_col(pc, xd, mb_col, bw);
700

Ronald S. Bultje's avatar
Ronald S. Bultje committed
701
702
#ifdef ENTROPY_STATS
  active_section = 9;
703
#endif
704

Ronald S. Bultje's avatar
Ronald S. Bultje committed
705
706
707
708
709
  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
710

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
714
715
      // If the mb segment id wasn't predicted code explicitly
      if (!prediction_flag)
716
        write_mb_segid_except(pc, bc, mi, &cpi->mb.e_mbd, mb_row, mb_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
717
718
719
720
721
    } else {
      // Normal unpredicted coding
      write_mb_segid(bc, mi, &cpi->mb.e_mbd);
    }
  }
722

723
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
724
725
    skip_coeff = 1;
  } else {
726
    skip_coeff = m->mbmi.mb_skip_coeff;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
727
728
729
    vp9_write(bc, skip_coeff,
              vp9_get_pred_prob(pc, xd, PRED_MBSKIP));
  }
John Koleszar's avatar
John Koleszar committed
730

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
734
  if (rf == INTRA_FRAME) {
735
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
736
    active_section = 6;
737
#endif
Paul Wilkins's avatar
Paul Wilkins committed
738

Paul Wilkins's avatar
Paul Wilkins committed
739
740
741
742
743
    if (m->mbmi.sb_type)
      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
744
    if (mode == I4X4_PRED) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
      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];
766

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

John Koleszar's avatar
John Koleszar committed
769
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
770
    active_section = 3;
John Koleszar's avatar
John Koleszar committed
771
772
#endif

773
    // If segment skip is not enabled code the mode.
Paul Wilkins's avatar
Paul Wilkins committed
774
    if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
775
776
      if (mi->sb_type) {
        write_sb_mv_ref(bc, mode, mv_ref_p);
777
      } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
778
779
780
781
        write_mv_ref(bc, mode, mv_ref_p);
      }
      vp9_accum_mv_refs(&cpi->common, mode, mi->mb_mode_context[rf]);
    }
782

Ronald S. Bultje's avatar
Ronald S. Bultje committed
783
784
785
786
787
788
789
790
791
792
793
    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);
      }
    }
794

Ronald S. Bultje's avatar
Ronald S. Bultje committed
795
796
797
798
799
800
    // 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));
    }
801
#if CONFIG_COMP_INTERINTRA_PRED
Ronald S. Bultje's avatar
Ronald S. Bultje committed
802
803
804
805
806
807
808
809
810
811
812
813
814
    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);
815
#if SEPARATE_INTERINTRA_UV
Ronald S. Bultje's avatar
Ronald S. Bultje committed
816
817
        write_uv_mode(bc, mi->interintra_uv_mode,
                      pc->fc.uv_mode_prob[mi->interintra_mode]);
818
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
819
820
      }
    }
821
#endif
John Koleszar's avatar
John Koleszar committed
822

Ronald S. Bultje's avatar
Ronald S. Bultje committed
823
824
    switch (mode) { /* new, split require MVs */
      case NEWMV:
825
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
826
        active_section = 5;
827
#endif
828
        write_nmv(cpi, bc, &mi->mv[0].as_mv, &mi->best_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
829
830
                  (const nmv_context*) nmvc,
                  xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
831

Ronald S. Bultje's avatar
Ronald S. Bultje committed
832
        if (mi->second_ref_frame > 0) {
833
          write_nmv(cpi, bc, &mi->mv[1].as_mv, &mi->best_second_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
834
835
836
837
838
839
                    (const nmv_context*) nmvc,
                    xd->allow_high_precision_mv);
        }
        break;
      case SPLITMV: {
        int j = 0;
John Koleszar's avatar
John Koleszar committed
840

841
#ifdef MODE_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
842
        ++count_mb_seg[mi->partitioning];
843
#endif
John Koleszar's avatar
John Koleszar committed
844

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
848
849
850
851
852
853
854
855
856
857
        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;
858
#if CONFIG_DEBUG
Ronald S. Bultje's avatar
Ronald S. Bultje committed
859
860
861
          while (j != L[++k])
            if (k >= 16)
              assert(0);
862
#else
Ronald S. Bultje's avatar
Ronald S. Bultje committed
863
          while (j != L[++k]);
864
#endif
865
          leftmv.as_int = left_block_mv(xd, m, k);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
866
867
          abovemv.as_int = above_block_mv(m, k, mis);
          mv_contz = vp9_mv_cont(&leftmv, &abovemv);
868

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

            if (mi->second_ref_frame > 0) {
881
              write_nmv(cpi, bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
882
883
884
885
                        &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
886
            }
John Koleszar's avatar
John Koleszar committed
887
          }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
888
889
890
891
892
893
894
        } while (++j < cpi->mb.partition_info->count);
        break;
      }
      default:
        break;
    }
  }
John Koleszar's avatar
John Koleszar committed
895

Ronald S. Bultje's avatar
Ronald S. Bultje committed
896
897
898
899
  if (((rf == INTRA_FRAME && mode <= I8X8_PRED) ||
       (rf != INTRA_FRAME && !(mode == SPLITMV &&
                               mi->partitioning == PARTITIONING_4X4))) &&
      pc->txfm_mode == TX_MODE_SELECT &&
900
901
          !(skip_coeff || vp9_segfeature_active(xd, segment_id,
                                                SEG_LVL_SKIP))) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
902
903
904
905
906
    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]);
907
      if (mi->sb_type >= BLOCK_SIZE_SB32X32 && sz != TX_8X8)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
908
        vp9_write(bc, sz != TX_16X16, pc->prob_tx[2]);
John Koleszar's avatar
John Koleszar committed
909
    }
John Koleszar's avatar
John Koleszar committed
910
  }
John Koleszar's avatar
John Koleszar committed
911
}
912

Ronald S. Bultje's avatar
Ronald S. Bultje committed
913
static void write_mb_modes_kf(const VP9_COMP *cpi,
914
                              MODE_INFO *m,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
915
916
917
918
919
920
921
922
                              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;
923
924
925
926
927

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

928
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
929
930
    skip_coeff = 1;
  } else {
931
    skip_coeff = m->mbmi.mb_skip_coeff;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
932
933
    vp9_write(bc, skip_coeff,
              vp9_get_pred_prob(c, xd, PRED_MBSKIP));
934
935
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
936
  if (m->mbmi.sb_type) {
937
938
    sb_kfwrite_ymode(bc, ym,
                     c->sb_kf_ymode_prob[c->kf_ymode_probs_index]);
939
  } else {
940
941
942
943
    kfwrite_ymode(bc, ym,
                  c->kf_ymode_prob[c->kf_ymode_probs_index]);
  }

Yaowu Xu's avatar
Yaowu Xu committed
944
  if (ym == I4X4_PRED) {
945
946
947
    int i = 0;
    do {
      const B_PREDICTION_MODE A = above_block_mode(m, i, mis);