bitstream.c 91.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
 */


John Koleszar's avatar
John Koleszar committed
12
#include "vp8/common/header.h"
John Koleszar's avatar
John Koleszar committed
13
#include "encodemv.h"
John Koleszar's avatar
John Koleszar committed
14
15
#include "vp8/common/entropymode.h"
#include "vp8/common/findnearmv.h"
John Koleszar's avatar
John Koleszar committed
16
#include "mcomp.h"
John Koleszar's avatar
John Koleszar committed
17
#include "vp8/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
18
19
#include <assert.h>
#include <stdio.h>
20
#include <limits.h>
John Koleszar's avatar
John Koleszar committed
21
#include "vp8/common/pragmas.h"
22
#include "vpx/vpx_encoder.h"
John Koleszar's avatar
John Koleszar committed
23
24
#include "vpx_mem/vpx_mem.h"
#include "bitstream.h"
Christian Duvivier's avatar
Christian Duvivier committed
25
#include "segmentation.h"
26

27
#include "vp8/common/seg_common.h"
28
#include "vp8/common/pred_common.h"
29
#include "vp8/common/entropy.h"
30
#include "vp8/encoder/encodemv.h"
31

32
#if CONFIG_NEWBESTREFMV
Paul Wilkins's avatar
Paul Wilkins committed
33
34
35
#include "vp8/common/mvref_common.h"
#endif

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

40
41
//int final_packing = 0;

John Koleszar's avatar
John Koleszar committed
42
#ifdef ENTROPY_STATS
43
int intra_mode_stats [VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES];
44
unsigned int tree_update_hist [BLOCK_TYPES]
45
46
47
                              [COEF_BANDS]
                              [PREV_COEF_CONTEXTS]
                              [ENTROPY_NODES][2];
48
49
50
51
unsigned int hybrid_tree_update_hist [BLOCK_TYPES]
                                     [COEF_BANDS]
                                     [PREV_COEF_CONTEXTS]
                                     [ENTROPY_NODES][2];
52
unsigned int tree_update_hist_8x8 [BLOCK_TYPES_8X8]
53
54
55
                                  [COEF_BANDS]
                                  [PREV_COEF_CONTEXTS]
                                  [ENTROPY_NODES] [2];
56
57
58
59
unsigned int hybrid_tree_update_hist_8x8 [BLOCK_TYPES_8X8]
                                         [COEF_BANDS]
                                         [PREV_COEF_CONTEXTS]
                                         [ENTROPY_NODES] [2];
Daniel Kang's avatar
Daniel Kang committed
60
61
62
63
unsigned int tree_update_hist_16x16 [BLOCK_TYPES_16X16]
                                    [COEF_BANDS]
                                    [PREV_COEF_CONTEXTS]
                                    [ENTROPY_NODES] [2];
64
65
66
67
unsigned int hybrid_tree_update_hist_16x16 [BLOCK_TYPES_16X16]
                                           [COEF_BANDS]
                                           [PREV_COEF_CONTEXTS]
                                           [ENTROPY_NODES] [2];
68

John Koleszar's avatar
John Koleszar committed
69
70
71
72
73
74
75
extern unsigned int active_section;
#endif

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

76
#define vp8_cost_upd  ((int)(vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8)
77
78
79
80
81
#define vp8_cost_upd256  ((int)(vp8_cost_one(upd) - vp8_cost_zero(upd)))

#define SEARCH_NEWP
static int update_bits[255];

John Koleszar's avatar
John Koleszar committed
82
83
84
85
static void compute_update_table() {
  int i;
  for (i = 0; i < 255; i++)
    update_bits[i] = vp8_count_term_subexp(i, SUBEXP_PARAM, 255);
86
87
}

John Koleszar's avatar
John Koleszar committed
88
89
90
91
92
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;
93
94
}

John Koleszar's avatar
John Koleszar committed
95
96
97
98
99
100
101
102
103
104
105
static int remap_prob(int v, int m) {
  const int n = 256;
  const int modulus = MODULUS_PARAM;
  int i;
  if ((m << 1) <= n)
    i = recenter_nonneg(v, m) - 1;
  else
    i = recenter_nonneg(n - 1 - v, n - 1 - m) - 1;

  i = split_index(i, n - 1, modulus);
  return i;
106
}
107

John Koleszar's avatar
John Koleszar committed
108
static void write_prob_diff_update(vp8_writer *const bc,
John Koleszar's avatar
John Koleszar committed
109
110
                                   vp8_prob newp, vp8_prob oldp) {
  int delp = remap_prob(newp, oldp);
John Koleszar's avatar
John Koleszar committed
111
  vp8_encode_term_subexp(bc, delp, SUBEXP_PARAM, 255);
112
113
}

John Koleszar's avatar
John Koleszar committed
114
115
116
static int prob_diff_update_cost(vp8_prob newp, vp8_prob oldp) {
  int delp = remap_prob(newp, oldp);
  return update_bits[delp] * 256;
117
}
118

Paul Wilkins's avatar
Paul Wilkins committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#if CONFIG_NEW_MVREF
// Estimate the cost of each coding the vector using each reference candidate
unsigned int pick_best_mv_ref( MACROBLOCK *x,
                               int_mv target_mv,
                               int_mv * mv_ref_list,
                               int_mv * best_ref ) {

  int i;
  int best_index = 0;
  int cost, cost2;
  int index_cost[MAX_MV_REFS];
  MACROBLOCKD *xd = &x->e_mbd;

  /*unsigned int distance, distance2;

  distance = mv_distance(&target_mv, &mv_ref_list[0]);

  for (i = 1; i < MAX_MV_REFS; ++i ) {
    distance2 =
      mv_distance(&target_mv, &mv_ref_list[i]);
    if (distance2 < distance) {
      distance = distance2;
      best_index = i;
    }
  }*/

  // For now estimate the cost of selecting a given ref index
  // as index * 1 bits (but here 1 bit is scaled to 256)
  for (i = 0; i < MAX_MV_REFS; ++i ) {
    index_cost[i] = i << 8;
  }
  index_cost[0] = vp8_cost_zero(205);
  index_cost[1] = vp8_cost_zero(40);
  index_cost[2] = vp8_cost_zero(8);
  index_cost[3] = vp8_cost_zero(2);

  cost = index_cost[0] +
         vp8_mv_bit_cost(&target_mv,
                         &mv_ref_list[0],
                         XMVCOST, 96,
                         xd->allow_high_precision_mv);


  //for (i = 1; i < MAX_MV_REFS; ++i ) {
  for (i = 1; i < 4; ++i ) {
    cost2 = index_cost[i] +
            vp8_mv_bit_cost(&target_mv,
                            &mv_ref_list[i],
                            XMVCOST, 96,
                            xd->allow_high_precision_mv);

    if (cost2 < cost) {
      cost = cost2;
      best_index = i;
    }
  }

  (*best_ref).as_int = mv_ref_list[best_index].as_int;

  return best_index;
}
#endif

John Koleszar's avatar
John Koleszar committed
182
static void update_mode(
John Koleszar's avatar
John Koleszar committed
183
  vp8_writer *const bc,
John Koleszar's avatar
John Koleszar committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  int n,
  vp8_token tok               [/* n */],
  vp8_tree tree,
  vp8_prob Pnew               [/* n-1 */],
  vp8_prob Pcur               [/* n-1 */],
  unsigned int bct            [/* n-1 */] [2],
  const unsigned int num_events[/* n */]
) {
  unsigned int new_b = 0, old_b = 0;
  int i = 0;

  vp8_tree_probs_from_distribution(
    n--, tok, tree,
    Pnew, bct, num_events,
    256, 1
  );

  do {
    new_b += vp8_cost_branch(bct[i], Pnew[i]);
    old_b += vp8_cost_branch(bct[i], Pcur[i]);
  } while (++i < n);

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

John Koleszar's avatar
John Koleszar committed
209
    vp8_write_bit(bc, 1);
John Koleszar's avatar
John Koleszar committed
210

John Koleszar's avatar
John Koleszar committed
211
212
    do {
      const vp8_prob p = Pnew[i];
John Koleszar's avatar
John Koleszar committed
213

John Koleszar's avatar
John Koleszar committed
214
      vp8_write_literal(bc, Pcur[i] = p ? p : 1, 8);
John Koleszar's avatar
John Koleszar committed
215
216
    } while (++i < n);
  } else
John Koleszar's avatar
John Koleszar committed
217
    vp8_write_bit(bc, 0);
John Koleszar's avatar
John Koleszar committed
218
219
}

John Koleszar's avatar
John Koleszar committed
220
221
static void update_mbintra_mode_probs(VP8_COMP* const cpi,
                                      vp8_writer* const bc) {
John Koleszar's avatar
John Koleszar committed
222
  VP8_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
223

John Koleszar's avatar
John Koleszar committed
224
225
226
  {
    vp8_prob Pnew   [VP8_YMODES - 1];
    unsigned int bct [VP8_YMODES - 1] [2];
John Koleszar's avatar
John Koleszar committed
227

John Koleszar's avatar
John Koleszar committed
228
    update_mode(
John Koleszar's avatar
John Koleszar committed
229
      bc, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
Paul Wilkins's avatar
Paul Wilkins committed
230
      Pnew, cm->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
John Koleszar's avatar
John Koleszar committed
231
232
    );
  }
John Koleszar's avatar
John Koleszar committed
233
234
}

John Koleszar's avatar
John Koleszar committed
235
static int get_prob(int num, int den) {
236
237
238
239
240
241
242
243
244
245
246
  int p;
  if (den <= 0)
    return 128;
  p = (num * 255 + (den >> 1)) / den;
  if (p > 255)
    return 255;
  else if (p < 1)
    return 1;
  return p;
}

John Koleszar's avatar
John Koleszar committed
247
248
249
250
static int get_binary_prob(int n0, int n1) {
  return get_prob(n0, n0 + n1);
}

John Koleszar's avatar
John Koleszar committed
251
void update_skip_probs(VP8_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
252
  VP8_COMMON *const pc = &cpi->common;
John Koleszar's avatar
John Koleszar committed
253
254
  int prob_skip_false[3] = {0, 0, 0};
  int k;
Paul Wilkins's avatar
Paul Wilkins committed
255

John Koleszar's avatar
John Koleszar committed
256
  for (k = 0; k < MBSKIP_CONTEXTS; ++k) {
John Koleszar's avatar
John Koleszar committed
257
258
    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
259
  }
Paul Wilkins's avatar
Paul Wilkins committed
260
261
}

262
#if CONFIG_SWITCHABLE_INTERP
John Koleszar's avatar
John Koleszar committed
263
void update_switchable_interp_probs(VP8_COMP *cpi, vp8_writer* const bc) {
John Koleszar's avatar
John Koleszar committed
264
  VP8_COMMON *const pc = &cpi->common;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  unsigned int branch_ct[32][2];
  int i, j;
  for (j = 0; j <= VP8_SWITCHABLE_FILTERS; ++j) {
  //for (j = 0; j <= 0; ++j) {
/*
    if (!cpi->dummy_packing)
#if VP8_SWITCHABLE_FILTERS == 3
      printf("HELLO %d %d %d\n", cpi->switchable_interp_count[j][0],
             cpi->switchable_interp_count[j][1], cpi->switchable_interp_count[j][2]);
#else
      printf("HELLO %d %d\n", cpi->switchable_interp_count[j][0],
             cpi->switchable_interp_count[j][1]);
#endif
*/
    vp8_tree_probs_from_distribution(
        VP8_SWITCHABLE_FILTERS,
        vp8_switchable_interp_encodings, vp8_switchable_interp_tree,
        pc->fc.switchable_interp_prob[j], branch_ct, cpi->switchable_interp_count[j],
        256, 1
        );
    for (i = 0; i < VP8_SWITCHABLE_FILTERS - 1; ++i) {
      if (pc->fc.switchable_interp_prob[j][i] < 1)
        pc->fc.switchable_interp_prob[j][i] = 1;
John Koleszar's avatar
John Koleszar committed
288
      vp8_write_literal(bc, pc->fc.switchable_interp_prob[j][i], 8);
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
      if (!cpi->dummy_packing)
#if VP8_SWITCHABLE_FILTERS == 3
        printf("Probs %d %d [%d]\n",
               pc->fc.switchable_interp_prob[j][0],
               pc->fc.switchable_interp_prob[j][1], pc->frame_type);
#else
        printf("Probs %d [%d]\n", pc->fc.switchable_interp_prob[j][0],
               pc->frame_type);
#endif
*/
    }
  }
  /*
  if (!cpi->dummy_packing)
#if VP8_SWITCHABLE_FILTERS == 3
    printf("Probs %d %d [%d]\n",
           pc->fc.switchable_interp_prob[0], pc->fc.switchable_interp_prob[1], pc->frame_type);
#else
    printf("Probs %d [%d]\n", pc->fc.switchable_interp_prob[0], pc->frame_type);
#endif
  */
}
#endif

314
// This function updates the reference frame prediction stats
John Koleszar's avatar
John Koleszar committed
315
static void update_refpred_stats(VP8_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
316
  VP8_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  int i;
  int tot_count;
  vp8_prob new_pred_probs[PREDICTION_PROBS];
  int old_cost, new_cost;

  // Set the prediction probability structures to defaults
  if (cm->frame_type == KEY_FRAME) {
    // Set the prediction probabilities to defaults
    cm->ref_pred_probs[0] = 120;
    cm->ref_pred_probs[1] = 80;
    cm->ref_pred_probs[2] = 40;

    vpx_memset(cpi->ref_pred_probs_update, 0,
               sizeof(cpi->ref_pred_probs_update));
  } else {
    // From the prediction counts set the probabilities for each context
    for (i = 0; i < PREDICTION_PROBS; i++) {
John Koleszar's avatar
John Koleszar committed
334
335
      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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352

      // Decide whether or not to update the reference frame probs.
      // Returned costs are in 1/256 bit units.
      old_cost =
        (cpi->ref_pred_count[i][0] * vp8_cost_zero(cm->ref_pred_probs[i])) +
        (cpi->ref_pred_count[i][1] * vp8_cost_one(cm->ref_pred_probs[i]));

      new_cost =
        (cpi->ref_pred_count[i][0] * vp8_cost_zero(new_pred_probs[i])) +
        (cpi->ref_pred_count[i][1] * vp8_cost_one(new_pred_probs[i]));

      // 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;
353
354

    }
John Koleszar's avatar
John Koleszar committed
355
  }
356
357
}

John Koleszar's avatar
John Koleszar committed
358
359
static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
  vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
John Koleszar's avatar
John Koleszar committed
360
361
}

John Koleszar's avatar
John Koleszar committed
362
363
static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
  vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
John Koleszar's avatar
John Koleszar committed
364
365
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
366
367
368
369
370
371
#if CONFIG_SUPERBLOCKS
static void sb_kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
  vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_sb_kf_ymode_encodings + m);
}
#endif

John Koleszar's avatar
John Koleszar committed
372
373
static void write_i8x8_mode(vp8_writer *bc, int m, const vp8_prob *p) {
  vp8_write_token(bc, vp8_i8x8_mode_tree, p, vp8_i8x8_mode_encodings + m);
Yaowu Xu's avatar
Yaowu Xu committed
374
}
Yaowu Xu's avatar
Yaowu Xu committed
375

John Koleszar's avatar
John Koleszar committed
376
377
static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p) {
  vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
John Koleszar's avatar
John Koleszar committed
378
379
380
}


John Koleszar's avatar
John Koleszar committed
381
382
static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p) {
  vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
John Koleszar's avatar
John Koleszar committed
383
384
}

John Koleszar's avatar
John Koleszar committed
385
386
387
388
static void write_split(vp8_writer *bc, int x, const vp8_prob *p) {
  vp8_write_token(
    bc, vp8_mbsplit_tree, p, vp8_mbsplit_encodings + x
  );
John Koleszar's avatar
John Koleszar committed
389
390
}

391
392
static int prob_update_savings(const unsigned int *ct,
                               const vp8_prob oldp, const vp8_prob newp,
John Koleszar's avatar
John Koleszar committed
393
394
395
396
397
                               const vp8_prob upd) {
  const int old_b = vp8_cost_branch256(ct, oldp);
  const int new_b = vp8_cost_branch256(ct, newp);
  const int update_b = 2048 + vp8_cost_upd256;
  return (old_b - new_b - update_b);
398
399
}

400
static int prob_diff_update_savings(const unsigned int *ct,
John Koleszar's avatar
John Koleszar committed
401
402
403
404
405
406
407
                                    const vp8_prob oldp, const vp8_prob newp,
                                    const vp8_prob upd) {
  const int old_b = vp8_cost_branch256(ct, oldp);
  const int new_b = vp8_cost_branch256(ct, newp);
  const int update_b = (newp == oldp ? 0 :
                        prob_diff_update_cost(newp, oldp) + vp8_cost_upd256);
  return (old_b - new_b - update_b);
408
409
410
}

static int prob_diff_update_savings_search(const unsigned int *ct,
John Koleszar's avatar
John Koleszar committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
                                           const vp8_prob oldp, vp8_prob *bestp,
                                           const vp8_prob upd) {
  const int old_b = vp8_cost_branch256(ct, oldp);
  int new_b, update_b, savings, bestsavings, step;
  vp8_prob newp, bestnewp;

  bestsavings = 0;
  bestnewp = oldp;

  step = (*bestp > oldp ? -1 : 1);
  for (newp = *bestp; newp != oldp; newp += step) {
    new_b = vp8_cost_branch256(ct, newp);
    update_b = prob_diff_update_cost(newp, oldp) + vp8_cost_upd256;
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
428
    }
John Koleszar's avatar
John Koleszar committed
429
430
431
  }
  *bestp = bestnewp;
  return bestsavings;
432
433
}

John Koleszar's avatar
John Koleszar committed
434
static void pack_mb_tokens(vp8_writer* const bc,
435
436
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
John Koleszar's avatar
John Koleszar committed
437
438
  unsigned int split;
  unsigned int shift;
John Koleszar's avatar
John Koleszar committed
439
440
441
  int count = bc->count;
  unsigned int range = bc->range;
  unsigned int lowvalue = bc->lowvalue;
442
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
443

John Koleszar's avatar
John Koleszar committed
444
445
446
447
448
449
450
451
452
  while (p < stop) {
    const int t = p->Token;
    vp8_token *const a = vp8_coef_encodings + t;
    const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
    int i = 0;
    const unsigned char *pp = p->context_tree;
    int v = a->value;
    int n = a->Len;

453
454
455
456
457
458
    if (t == EOSB_TOKEN)
    {
      ++p;
      break;
    }

John Koleszar's avatar
John Koleszar committed
459
460
461
462
463
    /* 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
464

John Koleszar's avatar
John Koleszar committed
465
466
467
468
    do {
      const int bb = (v >> --n) & 1;
      split = 1 + (((range - 1) * pp[i >> 1]) >> 8);
      i = vp8_coef_tree[i + bb];
John Koleszar's avatar
John Koleszar committed
469

John Koleszar's avatar
John Koleszar committed
470
471
472
473
474
475
      if (bb) {
        lowvalue += split;
        range = range - split;
      } else {
        range = split;
      }
John Koleszar's avatar
John Koleszar committed
476

John Koleszar's avatar
John Koleszar committed
477
478
479
      shift = vp8_norm[range];
      range <<= shift;
      count += shift;
John Koleszar's avatar
John Koleszar committed
480

John Koleszar's avatar
John Koleszar committed
481
482
      if (count >= 0) {
        int offset = shift - count;
John Koleszar's avatar
John Koleszar committed
483

John Koleszar's avatar
John Koleszar committed
484
        if ((lowvalue << (offset - 1)) & 0x80000000) {
John Koleszar's avatar
John Koleszar committed
485
          int x = bc->pos - 1;
John Koleszar's avatar
John Koleszar committed
486

John Koleszar's avatar
John Koleszar committed
487
488
          while (x >= 0 && bc->buffer[x] == 0xff) {
            bc->buffer[x] = (unsigned char)0;
John Koleszar's avatar
John Koleszar committed
489
490
            x--;
          }
John Koleszar's avatar
John Koleszar committed
491

John Koleszar's avatar
John Koleszar committed
492
          bc->buffer[x] += 1;
John Koleszar's avatar
John Koleszar committed
493
        }
John Koleszar's avatar
John Koleszar committed
494

John Koleszar's avatar
John Koleszar committed
495
        bc->buffer[bc->pos++] = (lowvalue >> (24 - offset));
John Koleszar's avatar
John Koleszar committed
496
497
498
499
500
        lowvalue <<= offset;
        shift = count;
        lowvalue &= 0xffffff;
        count -= 8;
      }
John Koleszar's avatar
John Koleszar committed
501

John Koleszar's avatar
John Koleszar committed
502
503
      lowvalue <<= shift;
    } while (n);
John Koleszar's avatar
John Koleszar committed
504
505


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

John Koleszar's avatar
John Koleszar committed
509
510
511
512
513
      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
514

John Koleszar's avatar
John Koleszar committed
515
516
517
518
        do {
          const int bb = (v >> --n) & 1;
          split = 1 + (((range - 1) * pp[i >> 1]) >> 8);
          i = b->tree[i + bb];
John Koleszar's avatar
John Koleszar committed
519

John Koleszar's avatar
John Koleszar committed
520
521
522
523
524
525
          if (bb) {
            lowvalue += split;
            range = range - split;
          } else {
            range = split;
          }
John Koleszar's avatar
John Koleszar committed
526

John Koleszar's avatar
John Koleszar committed
527
528
529
          shift = vp8_norm[range];
          range <<= shift;
          count += shift;
John Koleszar's avatar
John Koleszar committed
530

John Koleszar's avatar
John Koleszar committed
531
532
          if (count >= 0) {
            int offset = shift - count;
John Koleszar's avatar
John Koleszar committed
533

John Koleszar's avatar
John Koleszar committed
534
            if ((lowvalue << (offset - 1)) & 0x80000000) {
John Koleszar's avatar
John Koleszar committed
535
              int x = bc->pos - 1;
John Koleszar's avatar
John Koleszar committed
536

John Koleszar's avatar
John Koleszar committed
537
538
              while (x >= 0 && bc->buffer[x] == 0xff) {
                bc->buffer[x] = (unsigned char)0;
John Koleszar's avatar
John Koleszar committed
539
540
541
                x--;
              }

John Koleszar's avatar
John Koleszar committed
542
              bc->buffer[x] += 1;
John Koleszar's avatar
John Koleszar committed
543
544
            }

John Koleszar's avatar
John Koleszar committed
545
            bc->buffer[bc->pos++] = (lowvalue >> (24 - offset));
John Koleszar's avatar
John Koleszar committed
546
547
548
549
550
            lowvalue <<= offset;
            shift = count;
            lowvalue &= 0xffffff;
            count -= 8;
          }
John Koleszar's avatar
John Koleszar committed
551

John Koleszar's avatar
John Koleszar committed
552
553
554
          lowvalue <<= shift;
        } while (n);
      }
John Koleszar's avatar
John Koleszar committed
555
556


John Koleszar's avatar
John Koleszar committed
557
      {
John Koleszar's avatar
John Koleszar committed
558

John Koleszar's avatar
John Koleszar committed
559
        split = (range + 1) >> 1;
John Koleszar's avatar
John Koleszar committed
560

John Koleszar's avatar
John Koleszar committed
561
562
563
564
565
566
        if (e & 1) {
          lowvalue += split;
          range = range - split;
        } else {
          range = split;
        }
John Koleszar's avatar
John Koleszar committed
567

John Koleszar's avatar
John Koleszar committed
568
        range <<= 1;
John Koleszar's avatar
John Koleszar committed
569

John Koleszar's avatar
John Koleszar committed
570
        if ((lowvalue & 0x80000000)) {
John Koleszar's avatar
John Koleszar committed
571
          int x = bc->pos - 1;
John Koleszar's avatar
John Koleszar committed
572

John Koleszar's avatar
John Koleszar committed
573
574
          while (x >= 0 && bc->buffer[x] == 0xff) {
            bc->buffer[x] = (unsigned char)0;
John Koleszar's avatar
John Koleszar committed
575
576
            x--;
          }
John Koleszar's avatar
John Koleszar committed
577

John Koleszar's avatar
John Koleszar committed
578
          bc->buffer[x] += 1;
John Koleszar's avatar
John Koleszar committed
579

John Koleszar's avatar
John Koleszar committed
580
        }
John Koleszar's avatar
John Koleszar committed
581

John Koleszar's avatar
John Koleszar committed
582
583
584
585
        lowvalue  <<= 1;

        if (!++count) {
          count = -8;
John Koleszar's avatar
John Koleszar committed
586
          bc->buffer[bc->pos++] = (lowvalue >> 24);
John Koleszar's avatar
John Koleszar committed
587
          lowvalue &= 0xffffff;
John Koleszar's avatar
John Koleszar committed
588
        }
John Koleszar's avatar
John Koleszar committed
589
      }
John Koleszar's avatar
John Koleszar committed
590
591

    }
John Koleszar's avatar
John Koleszar committed
592
593
594
    ++p;
  }

John Koleszar's avatar
John Koleszar committed
595
596
597
  bc->count = count;
  bc->lowvalue = lowvalue;
  bc->range = range;
598
  *tp = p;
John Koleszar's avatar
John Koleszar committed
599
600
}

John Koleszar's avatar
John Koleszar committed
601
602
static void write_partition_size(unsigned char *cx_data, int size) {
  signed char csize;
John Koleszar's avatar
John Koleszar committed
603

John Koleszar's avatar
John Koleszar committed
604
605
606
607
608
609
  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
610
611
612
613
614

}

static void write_mv_ref
(
John Koleszar's avatar
John Koleszar committed
615
  vp8_writer *bc, MB_PREDICTION_MODE m, const vp8_prob *p
John Koleszar's avatar
John Koleszar committed
616
) {
617
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
618
  assert(NEARESTMV <= m  &&  m <= SPLITMV);
619
#endif
John Koleszar's avatar
John Koleszar committed
620
  vp8_write_token(bc, vp8_mv_ref_tree, p,
John Koleszar's avatar
John Koleszar committed
621
                  vp8_mv_ref_encoding_array - NEARESTMV + m);
John Koleszar's avatar
John Koleszar committed
622
623
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
624
#if CONFIG_SUPERBLOCKS
John Koleszar's avatar
John Koleszar committed
625
626
static void write_sb_mv_ref(vp8_writer *bc, MB_PREDICTION_MODE m,
                            const vp8_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
627
628
629
#if CONFIG_DEBUG
  assert(NEARESTMV <= m  &&  m < SPLITMV);
#endif
John Koleszar's avatar
John Koleszar committed
630
  vp8_write_token(bc, vp8_sb_mv_ref_tree, p,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
631
632
633
634
                  vp8_sb_mv_ref_encoding_array - NEARESTMV + m);
}
#endif

John Koleszar's avatar
John Koleszar committed
635
636
static void write_sub_mv_ref
(
John Koleszar's avatar
John Koleszar committed
637
  vp8_writer *bc, B_PREDICTION_MODE m, const vp8_prob *p
John Koleszar's avatar
John Koleszar committed
638
) {
639
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
640
  assert(LEFT4X4 <= m  &&  m <= NEW4X4);
641
#endif
John Koleszar's avatar
John Koleszar committed
642
  vp8_write_token(bc, vp8_sub_mv_ref_tree, p,
John Koleszar's avatar
John Koleszar committed
643
                  vp8_sub_mv_ref_encoding_array - LEFT4X4 + m);
John Koleszar's avatar
John Koleszar committed
644
645
}

646
#if CONFIG_NEWMVENTROPY
John Koleszar's avatar
John Koleszar committed
647
648
static void write_nmv(vp8_writer *bc, const MV *mv, const int_mv *ref,
                      const nmv_context *nmvc, int usehp) {
649
650
651
652
  MV e;
  e.row = mv->row - ref->as_mv.row;
  e.col = mv->col - ref->as_mv.col;

John Koleszar's avatar
John Koleszar committed
653
654
  vp8_encode_nmv(bc, &e, &ref->as_mv, nmvc);
  vp8_encode_nmv_fp(bc, &e, &ref->as_mv, nmvc, usehp);
655
656
657
658
}

#else

John Koleszar's avatar
John Koleszar committed
659
660
static void write_mv
(
John Koleszar's avatar
John Koleszar committed
661
  vp8_writer *bc, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc
John Koleszar's avatar
John Koleszar committed
662
663
664
665
666
) {
  MV e;
  e.row = mv->row - ref->as_mv.row;
  e.col = mv->col - ref->as_mv.col;

John Koleszar's avatar
John Koleszar committed
667
  vp8_encode_motion_vector(bc, &e, mvc);
John Koleszar's avatar
John Koleszar committed
668
669
}

670
671
static void write_mv_hp
(
John Koleszar's avatar
John Koleszar committed
672
  vp8_writer *bc, const MV *mv, const int_mv *ref, const MV_CONTEXT_HP *mvc
John Koleszar's avatar
John Koleszar committed
673
674
675
676
677
) {
  MV e;
  e.row = mv->row - ref->as_mv.row;
  e.col = mv->col - ref->as_mv.col;

John Koleszar's avatar
John Koleszar committed
678
  vp8_encode_motion_vector_hp(bc, &e, mvc);
679
}
680
#endif  /* CONFIG_NEWMVENTROPY */
681

Paul Wilkins's avatar
Paul Wilkins committed
682
683
// 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.
John Koleszar's avatar
John Koleszar committed
684
static void write_mb_segid(vp8_writer *bc,
Paul Wilkins's avatar
Paul Wilkins committed
685
                           const MB_MODE_INFO *mi, const MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
686
  // Encode the MB segment id.
Paul Wilkins's avatar
Paul Wilkins committed
687
  if (xd->segmentation_enabled && xd->update_mb_segmentation_map) {
John Koleszar's avatar
John Koleszar committed
688
689
    switch (mi->segment_id) {
      case 0:
John Koleszar's avatar
John Koleszar committed
690
691
        vp8_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp8_write(bc, 0, xd->mb_segment_tree_probs[1]);
John Koleszar's avatar
John Koleszar committed
692
693
        break;
      case 1:
John Koleszar's avatar
John Koleszar committed
694
695
        vp8_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp8_write(bc, 1, xd->mb_segment_tree_probs[1]);
John Koleszar's avatar
John Koleszar committed
696
697
        break;
      case 2:
John Koleszar's avatar
John Koleszar committed
698
699
        vp8_write(bc, 1, xd->mb_segment_tree_probs[0]);
        vp8_write(bc, 0, xd->mb_segment_tree_probs[2]);
John Koleszar's avatar
John Koleszar committed
700
701
        break;
      case 3:
John Koleszar's avatar
John Koleszar committed
702
703
        vp8_write(bc, 1, xd->mb_segment_tree_probs[0]);
        vp8_write(bc, 1, xd->mb_segment_tree_probs[2]);
John Koleszar's avatar
John Koleszar committed
704
705
706
707
        break;

        // TRAP.. This should not happen
      default:
John Koleszar's avatar
John Koleszar committed
708
709
        vp8_write(bc, 0, xd->mb_segment_tree_probs[0]);
        vp8_write(bc, 0, xd->mb_segment_tree_probs[1]);
John Koleszar's avatar
John Koleszar committed
710
        break;
John Koleszar's avatar
John Koleszar committed
711
    }
John Koleszar's avatar
John Koleszar committed
712
  }
John Koleszar's avatar
John Koleszar committed
713
714
}

Paul Wilkins's avatar
Paul Wilkins committed
715
// This function encodes the reference frame
John Koleszar's avatar
John Koleszar committed
716
static void encode_ref_frame(vp8_writer *const bc,
John Koleszar's avatar
John Koleszar committed
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
                             VP8_COMMON *const cm,
                             MACROBLOCKD *xd,
                             int segment_id,
                             MV_REFERENCE_FRAME rf) {
  int seg_ref_active;
  int seg_ref_count = 0;
  seg_ref_active = segfeature_active(xd,
                                     segment_id,
                                     SEG_LVL_REF_FRAME);

  if (seg_ref_active) {
    seg_ref_count = check_segref(xd, segment_id, INTRA_FRAME) +
                    check_segref(xd, segment_id, LAST_FRAME) +
                    check_segref(xd, segment_id, GOLDEN_FRAME) +
                    check_segref(xd, segment_id, ALTREF_FRAME);
  }

  // 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;
    vp8_prob pred_prob;
    MV_REFERENCE_FRAME pred_rf;

    // Get the context probability the prediction flag
    pred_prob = get_pred_prob(cm, xd, PRED_REF);

    // Get the predicted value.
    pred_rf = get_pred_ref(cm, xd);

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

    set_pred_flag(xd, PRED_REF, prediction_flag);
John Koleszar's avatar
John Koleszar committed
753
    vp8_write(bc, prediction_flag, pred_prob);
John Koleszar's avatar
John Koleszar committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774

    // If not predicted correctly then code value explicitly
    if (!prediction_flag) {
      vp8_prob mod_refprobs[PREDICTION_PROBS];

      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] *=
          check_segref(xd, segment_id, INTRA_FRAME);
        mod_refprobs[LAST_FRAME] *=
          check_segref(xd, segment_id, LAST_FRAME);
        mod_refprobs[GOLDEN_FRAME] *=
          (check_segref(xd, segment_id, GOLDEN_FRAME) *
           check_segref(xd, segment_id, ALTREF_FRAME));
      }

      if (mod_refprobs[0]) {
John Koleszar's avatar
John Koleszar committed
775
        vp8_write(bc, (rf != INTRA_FRAME), mod_refprobs[0]);
John Koleszar's avatar
John Koleszar committed
776
777
778
779
780
      }

      // Inter coded
      if (rf != INTRA_FRAME) {
        if (mod_refprobs[1]) {
John Koleszar's avatar
John Koleszar committed
781
          vp8_write(bc, (rf != LAST_FRAME), mod_refprobs[1]);
John Koleszar's avatar
John Koleszar committed
782
        }
783

John Koleszar's avatar
John Koleszar committed
784
785
        if (rf != LAST_FRAME) {
          if (mod_refprobs[2]) {
John Koleszar's avatar
John Koleszar committed
786
            vp8_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]);
John Koleszar's avatar
John Koleszar committed
787
          }
788
        }
John Koleszar's avatar
John Koleszar committed
789
      }
Paul Wilkins's avatar
Paul Wilkins committed
790
    }
John Koleszar's avatar
John Koleszar committed
791
  }
Paul Wilkins's avatar
Paul Wilkins committed
792

John Koleszar's avatar
John Koleszar committed
793
794
  // 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
795
}
John Koleszar's avatar
John Koleszar committed
796

797
// Update the probabilities used to encode reference frame data
John Koleszar's avatar
John Koleszar committed
798
static void update_ref_probs(VP8_COMP *const cpi) {
John Koleszar's avatar
John Koleszar committed
799
  VP8_COMMON *const cm = &cpi->common;
800

John Koleszar's avatar
John Koleszar committed
801
802
803
804
  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];
805

John Koleszar's avatar
John Koleszar committed
806
807
808
  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]);
809

John Koleszar's avatar
John Koleszar committed
810
811
812
  // Compute a modified set of probabilities to use when prediction of the
  // reference frame fails
  compute_mod_refprobs(cm);
813
814
}

John Koleszar's avatar
John Koleszar committed
815
static void pack_inter_mode_mvs(VP8_COMP *const cpi, vp8_writer *const bc) {
John Koleszar's avatar
John Koleszar committed
816
  int i;
John Koleszar's avatar
John Koleszar committed
817
  VP8_COMMON *const pc = &cpi->common;
818
819
820
#if CONFIG_NEWMVENTROPY
  const nmv_context *nmvc = &pc->fc.nmvc;
#else
John Koleszar's avatar
John Koleszar committed
821
822
  const MV_CONTEXT *mvc = pc->fc.mvc;
  const MV_CONTEXT_HP *mvc_hp = pc->fc.mvc_hp;
823
#endif
Paul Wilkins's avatar
Paul Wilkins committed
824
  MACROBLOCK *x = &cpi->mb;
John Koleszar's avatar
John Koleszar committed
825
826
827
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
  MODE_INFO *m;
  MODE_INFO *prev_m;
828
829
  TOKENEXTRA *tok = cpi->tok;
  TOKENEXTRA *tok_end = tok + cpi->tok_count;
830

John Koleszar's avatar
John Koleszar committed
831
832
833
  const int mis = pc->mode_info_stride;
  int mb_row, mb_col;
  int row, col;
John Koleszar's avatar
John Koleszar committed
834

John Koleszar's avatar
John Koleszar committed
835
836
837
  // Values used in prediction model coding
  vp8_prob pred_prob;
  unsigned char prediction_flag;
838

John Koleszar's avatar
John Koleszar committed
839
840
  int row_delta[4] = { 0, +1,  0, -1};
  int col_delta[4] = { +1, -1, +1, +1};
Adrian Grange's avatar
Adrian Grange committed
841

842
843
  //final_packing = !cpi->dummy_packing;

John Koleszar's avatar
John Koleszar committed
844
  cpi->mb.partition_info = cpi->mb.pi;
845

John Koleszar's avatar
John Koleszar committed
846
847
848
849
850
851
852
853
854
855
856
  mb_row = 0;
  for (row = 0; row < pc->mb_rows; row += 2) {
    m = pc->mi + row * mis;
    prev_m = pc->prev_mi + row * mis;

    mb_col = 0;
    for (col = 0; col < pc->mb_cols; col += 2) {
      int i;

      // Process the 4 MBs in the order:
      // top-left, top-right, bottom-left, bottom-right
Ronald S. Bultje's avatar
Ronald S. Bultje committed
857
#if CONFIG_SUPERBLOCKS
John Koleszar's avatar
John Koleszar committed
858
      vp8_write(bc, m->mbmi.encoded_as_sb, pc->sb_coded);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
859
#endif
John Koleszar's avatar
John Koleszar committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
      for (i = 0; i < 4; i++) {
        MB_MODE_INFO *mi;
        MV_REFERENCE_FRAME rf;
        MB_PREDICTION_MODE mode;
        int segment_id;

        int dy = row_delta[i];
        int dx = col_delta[i];
        int offset_extended = dy * mis + dx;

        if ((mb_row >= pc->mb_rows) || (mb_col >= pc->mb_cols)) {
          // MB lies outside frame, move on
          mb_row += dy;
          mb_col += dx;
          m += offset_extended;
          prev_m += offset_extended;
          cpi->mb.partition_info += offset_extended;
          continue;
        }
Adrian Grange's avatar
Adrian Grange committed
879

John Koleszar's avatar
John Koleszar committed
880
        mi = &m->mbmi;
John Koleszar's avatar
John Koleszar committed
881
882
883
        rf = mi->ref_frame;
        mode = mi->mode;
        segment_id = mi->segment_id;
884

John Koleszar's avatar
John Koleszar committed
885
886
887
888
889
890
891
        // 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_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
        xd->mb_to_top_edge = -((mb_row * 16)) << 3;
        xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
892

John Koleszar's avatar
John Koleszar committed
893
894
895
        // Make sure the MacroBlockD mode info pointer is set correctly
        xd->mode_info_context = m;
        xd->prev_mode_info_context = prev_m;
Paul Wilkins's avatar
Paul Wilkins committed
896

John Koleszar's avatar
John Koleszar committed
897
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
898
        active_section = 9;
John Koleszar's avatar
John Koleszar committed
899
900
#endif

John Koleszar's avatar
John Koleszar committed
901
902
903
904
905
        if (cpi->mb.e_mbd.update_mb_segmentation_map) {
          // Is temporal coding of the segment map enabled
          if (pc->temporal_update) {
            prediction_flag = get_pred_flag(xd, PRED_SEG_ID);
            pred_prob = get_pred_prob(pc, xd, PRED_SEG_ID);
906

John Koleszar's avatar
John Koleszar committed
907
            // Code the segment id prediction flag for this mb
John Koleszar's avatar
John Koleszar committed
908
            vp8_write(bc, prediction_flag, pred_prob);
909

John Koleszar's avatar
John Koleszar committed
910
911
            // If the mb segment id wasn't predicted code explicitly
            if (!prediction_flag)
John Koleszar's avatar
John Koleszar committed
912
              write_mb_segid(bc, mi, &cpi->mb.e_mbd);
John Koleszar's avatar
John Koleszar committed
913
914
          } else {
            // Normal unpredicted coding
John Koleszar's avatar
John Koleszar committed
915
            write_mb_segid(bc, mi, &cpi->mb.e_mbd);
John Koleszar's avatar
John Koleszar committed
916
917
          }
        }
John Koleszar's avatar
John Koleszar committed
918

John Koleszar's avatar
John Koleszar committed
919
920
921
        if (pc->mb_no_coeff_skip &&
            (!segfeature_active(xd, segment_id, SEG_LVL_EOB) ||
             (get_segdata(xd, segment_id, SEG_LVL_EOB) != 0))) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
922
923
924
925
926
927
928
929
          int skip_coeff = mi->mb_skip_coeff;
#if CONFIG_SUPERBLOCKS
          if (mi->encoded_as_sb) {
            skip_coeff &= m[1].mbmi.mb_skip_coeff;
            skip_coeff &= m[mis].mbmi.mb_skip_coeff;
            skip_coeff &= m[mis + 1].mbmi.mb_skip_coeff;
          }
#endif
John Koleszar's avatar
John Koleszar committed
930
          vp8_encode_bool(bc, skip_coeff,
John Koleszar's avatar
John Koleszar committed
931
932
                          get_pred_prob(pc, xd, PRED_MBSKIP));
        }
John Koleszar's avatar
John Koleszar committed
933

John Koleszar's avatar
John Koleszar committed
934
        // Encode the reference frame.
John Koleszar's avatar
John Koleszar committed
935
        encode_ref_frame(bc, pc, xd, segment_id, rf);
Paul Wilkins's avatar
Paul Wilkins committed
936

John Koleszar's avatar
John Koleszar committed
937
        if (rf == INTRA_FRAME) {
938
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
939
          active_section = 6;
940
#endif
Paul Wilkins's avatar
Paul Wilkins committed
941

Ronald S. Bultje's avatar
Ronald S. Bultje committed
942
943
          // TODO(rbultje) write using SB tree structure

John Koleszar's avatar
John Koleszar committed
944
          if (!segfeature_active(xd, segment_id, SEG_LVL_MODE)) {
John Koleszar's avatar
John Koleszar committed
945
            write_ymode(bc, mode, pc->fc.ymode_prob);
John Koleszar's avatar
John Koleszar committed
946
          }
John Koleszar's avatar
John Koleszar committed
947

John Koleszar's avatar
John Koleszar committed
948
949
          if (mode == B_PRED) {
            int j = 0;
950
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
951
952
953
            int uses_second =
              m->bmi[0].as_mode.second !=
              (B_PREDICTION_MODE)(B_DC_PRED - 1);
John Koleszar's avatar
John Koleszar committed
954
            vp8_write(bc, uses_second, 128);
955
#endif
John Koleszar's avatar
John Koleszar committed
956
            do {
957
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
958
              B_PREDICTION_MODE mode2 = m->bmi[j].as_mode.second;
959
#endif
John Koleszar's avatar
John Koleszar committed
960
              write_bmode(bc, m->bmi[j].as_mode.first,
John Koleszar's avatar
John Koleszar committed
961
                          pc->fc.bmode_prob);
Deb Mukherjee's avatar
Deb Mukherjee committed
962
963
964
965
966
967
968
969
              /*
              if (!cpi->dummy_packing) {
                int p;
                for (p = 0; p < VP8_BINTRAMODES - 1; ++p)
                  printf(" %d", pc->fc.bmode_prob[p]);
                printf("\nbmode[%d][%d]: %d\n", pc->current_video_frame, j, m->bmi[j].as_mode.first);
              }
              */
970
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
971
              if (uses_second) {
John Koleszar's avatar
John Koleszar committed
972
                write_bmode(bc, mode2, pc->fc.bmode_prob);
John Koleszar's avatar
John Koleszar committed
973
974
975
976
977
              }
#endif
            } while (++j < 16);
          }
          if (mode == I8X8_PRED) {
John Koleszar's avatar
John Koleszar committed
978
            write_i8x8_mode(bc, m->bmi[0].as_mode.first,
John Koleszar's avatar
John Koleszar committed
979
                            pc->fc.i8x8_mode_prob);
John Koleszar's avatar
John Koleszar committed
980
            write_i8x8_mode(bc, m->bmi[2].as_mode.first,
John Koleszar's avatar
John Koleszar committed
981
                            pc->fc.i8x8_mode_prob);
John Koleszar's avatar
John Koleszar committed
982
            write_i8x8_mode(bc, m->bmi[8].as_mode.first,
John Koleszar's avatar
John Koleszar committed
983
                            pc->fc.i8x8_mode_prob);
John Koleszar's avatar
John Koleszar committed
984
            write_i8x8_mode(bc, m->bmi[10].as_mode.first,
John Koleszar's avatar
John Koleszar committed
985
986
                            pc->fc.i8x8_mode_prob);
          } else {
John Koleszar's avatar
John Koleszar committed
987
            write_uv_mode(bc, mi->uv_mode,
John Koleszar's avatar
John Koleszar committed
988
989
990
991
992
993
994
995
996
997
998
999
1000
                          pc->fc.uv_mode_prob[mode]);
          }
        } else {
          int_mv best_mv, best_second_mv;
          int ct[4];

          vp8_prob mv_ref_p [VP8_MVREFS - 1];

          {
            int_mv n1, n2;

            vp8_find_near_mvs(xd, m, prev_m, &n1, &n2, &best_mv, ct,
                              rf, cpi->common.ref_frame_sign_bias);
1001
1002
1003
#if CONFIG_NEWBESTREFMV
            best_mv.as_int = mi->ref_mv.as_int;
#endif
John Koleszar's avatar
John Koleszar committed
1004
            vp8_mv_ref_probs(&cpi->common, mv_ref_p, ct);
Yaowu Xu's avatar
Yaowu Xu committed
1005

John Koleszar's avatar
John Koleszar committed
1006
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1007
            accum_mv_refs(mode, ct);
John Koleszar's avatar
John Koleszar committed
1008
#endif
John Koleszar's avatar
John Koleszar committed
1009
          }
John Koleszar's avatar
John Koleszar committed
1010
1011

#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1012
          active_section = 3;
John Koleszar's avatar
John Koleszar committed
1013
1014
#endif

John Koleszar's avatar
John Koleszar committed
1015
1016
          // Is the segment coding of mode enabled
          if (!segfeature_active(xd, segment_id, SEG_LVL_MODE)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1017
1018
#if CONFIG_SUPERBLOCKS
            if (mi->encoded_as_sb) {
John Koleszar's avatar
John Koleszar committed
1019
              write_sb_mv_ref(bc, mode, mv_ref_p);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1020
1021
1022
            } else
#endif
            {
John Koleszar's avatar
John Koleszar committed
1023
              write_mv_ref(bc, mode, mv_ref_p);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1024
            }
John Koleszar's avatar
John Koleszar committed
1025
1026
            vp8_accum_mv_refs(&cpi->common, mode, ct);
          }
1027

1028
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
1029
1030
1031
          // Is the prediction filter enabled
          if (mode >= NEARESTMV && mode < SPLITMV) {
            if (cpi->common.pred_filter_mode == 2)
John Koleszar's avatar
John Koleszar committed
1032
              vp8_write(bc, mi->pred_filter_enabled,
John Koleszar's avatar
John Koleszar committed
1033
1034
1035
1036
1037
                        pc->prob_pred_filter_off);
            else
              assert(mi->pred_filter_enabled ==
                     cpi->common.pred_filter_mode);
          }
1038
1039
1040
1041
1042
#endif
#if CONFIG_SWITCHABLE_INTERP
          if (mode >= NEARESTMV && mode <= SPLITMV)
          {
            if (cpi->common.mcomp_filter_type == SWITCHABLE) {
John Koleszar's avatar
John Koleszar committed
1043
              vp8_write_token(bc, vp8_switchable_interp_tree,
1044
1045
1046
1047
1048
1049
1050
1051
1052
                              get_pred_probs(&cpi->common, xd, PRED_SWITCHABLE_INTERP),
                              vp8_switchable_interp_encodings +
                              vp8_switchable_interp_map[mi->interp_filter]);
              //if (!cpi->dummy_packing) printf("Reading: %d\n", mi->interp_filter);
            } else {
              assert (mi->interp_filter ==
                      cpi->common.mcomp_filter_type);
            }
          }
John Koleszar's avatar
John Koleszar committed
1053
1054
1055
1056
1057
1058
1059
1060
#endif
          if (mi->second_ref_frame &&
              (mode == NEWMV || mode == SPLITMV)) {
            int_mv n1, n2;

            vp8_find_near_mvs(xd, m,
                              prev_m,
                              &n1, &n2, &best_second_mv, ct,
1061
1062
1063
1064
1065
                              mi->second_ref_frame,
                              cpi->common.ref_frame_sign_bias);
#if CONFIG_NEWBESTREFMV
            best_second_mv.as_int = mi->second_ref_mv.as_int;
#endif
John Koleszar's avatar
John Koleszar committed
1066
1067
1068
1069
1070
          }

          // does the feature use compound prediction or not
          // (if not specified at the frame/segment level)
          if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) {
John Koleszar's avatar
John Koleszar committed
1071
            vp8_write(bc, mi->second_ref_frame != INTRA_FRAME,
John Koleszar's avatar
John Koleszar committed
1072
1073
1074
1075
1076
1077
                      get_pred_prob(pc, xd, PRED_COMP));
          }

          {
            switch (mode) { /* new, split require MVs */
              case NEWMV:
1078
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1079
                active_section = 5;
1080
#endif
John Koleszar's avatar
John Koleszar committed
1081

Paul Wilkins's avatar
Paul Wilkins committed
1082
#if 0 //CONFIG_NEW_MVREF
Paul Wilkins's avatar
Paul Wilkins committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
                {
                  unsigned int best_index;
                  /*find_mv_refs(xd, m, prev_m,
                               m->mbmi.ref_frame,
                               mi->ref_mvs[rf],
                               cpi->common.ref_frame_sign_bias );*/

                  best_index = pick_best_mv_ref(x, mi->mv[0],
                                                mi->ref_mvs[rf], &best_mv);
                  cpi->best_ref_index_counts[best_index]++;
Paul Wilkins's avatar
Paul Wilkins committed
1093

Paul Wilkins's avatar
Paul Wilkins committed
1094
                }
Paul Wilkins's avatar
Paul Wilkins committed
1095
#endif
1096
#if CONFIG_NEWMVENTROPY
John Koleszar's avatar
John Koleszar committed
1097
                write_nmv(bc, &mi->mv[0].as_mv, &best_mv,
1098
1099
1100
1101
                          (const nmv_context*) nmvc,
                          xd->allow_high_precision_mv);
#else
                if (xd->allow_high_precision_mv) {
John Koleszar's avatar
John Koleszar committed
1102
                  write_mv_hp(bc, &mi->mv[0].as_mv, &best_mv, mvc_hp);
1103
                } else {
John Koleszar's avatar
John Koleszar committed
1104
                  write_mv(bc, &mi->mv[0].as_mv, &best_mv, mvc);
1105
1106
                }
#endif
John Koleszar's avatar
John Koleszar committed
1107
1108

                if (mi->second_ref_frame) {