bitstream.c 91.6 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
52
53
#if CONFIG_HYBRIDTRANSFORM
unsigned int hybrid_tree_update_hist [BLOCK_TYPES]
                                     [COEF_BANDS]
                                     [PREV_COEF_CONTEXTS]
                                     [ENTROPY_NODES][2];
#endif
54
unsigned int tree_update_hist_8x8 [BLOCK_TYPES_8X8]
55
56
57
                                  [COEF_BANDS]
                                  [PREV_COEF_CONTEXTS]
                                  [ENTROPY_NODES] [2];
58
59
60
61
62
63
#if CONFIG_HYBRIDTRANSFORM8X8
unsigned int hybrid_tree_update_hist_8x8 [BLOCK_TYPES_8X8]
                                         [COEF_BANDS]
                                         [PREV_COEF_CONTEXTS]
                                         [ENTROPY_NODES] [2];
#endif
Daniel Kang's avatar
Daniel Kang committed
64
65
66
67
unsigned int tree_update_hist_16x16 [BLOCK_TYPES_16X16]
                                    [COEF_BANDS]
                                    [PREV_COEF_CONTEXTS]
                                    [ENTROPY_NODES] [2];
68
69
70
71
72
73
#if CONFIG_HYBRIDTRANSFORM16X16
unsigned int hybrid_tree_update_hist_16x16 [BLOCK_TYPES_16X16]
                                           [COEF_BANDS]
                                           [PREV_COEF_CONTEXTS]
                                           [ENTROPY_NODES] [2];
#endif
74

John Koleszar's avatar
John Koleszar committed
75
76
77
78
79
80
81
extern unsigned int active_section;
#endif

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

82
#define vp8_cost_upd  ((int)(vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8)
83
84
85
86
87
#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
88
89
90
91
static void compute_update_table() {
  int i;
  for (i = 0; i < 255; i++)
    update_bits[i] = vp8_count_term_subexp(i, SUBEXP_PARAM, 255);
92
93
}

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

John Koleszar's avatar
John Koleszar committed
101
102
103
104
105
106
107
108
109
110
111
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;
112
}
113

John Koleszar's avatar
John Koleszar committed
114
static void write_prob_diff_update(vp8_writer *const bc,
John Koleszar's avatar
John Koleszar committed
115
116
                                   vp8_prob newp, vp8_prob oldp) {
  int delp = remap_prob(newp, oldp);
John Koleszar's avatar
John Koleszar committed
117
  vp8_encode_term_subexp(bc, delp, SUBEXP_PARAM, 255);
118
119
}

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

Paul Wilkins's avatar
Paul Wilkins committed
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
182
183
184
185
186
187
#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
188
static void update_mode(
John Koleszar's avatar
John Koleszar committed
189
  vp8_writer *const bc,
John Koleszar's avatar
John Koleszar committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  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
213
214
    int i = 0;

John Koleszar's avatar
John Koleszar committed
215
    vp8_write_bit(bc, 1);
John Koleszar's avatar
John Koleszar committed
216

John Koleszar's avatar
John Koleszar committed
217
218
    do {
      const vp8_prob p = Pnew[i];
John Koleszar's avatar
John Koleszar committed
219

John Koleszar's avatar
John Koleszar committed
220
      vp8_write_literal(bc, Pcur[i] = p ? p : 1, 8);
John Koleszar's avatar
John Koleszar committed
221
222
    } while (++i < n);
  } else
John Koleszar's avatar
John Koleszar committed
223
    vp8_write_bit(bc, 0);
John Koleszar's avatar
John Koleszar committed
224
225
}

John Koleszar's avatar
John Koleszar committed
226
227
static void update_mbintra_mode_probs(VP8_COMP* const cpi,
                                      vp8_writer* const bc) {
John Koleszar's avatar
John Koleszar committed
228
  VP8_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
229

John Koleszar's avatar
John Koleszar committed
230
231
232
  {
    vp8_prob Pnew   [VP8_YMODES - 1];
    unsigned int bct [VP8_YMODES - 1] [2];
John Koleszar's avatar
John Koleszar committed
233

John Koleszar's avatar
John Koleszar committed
234
    update_mode(
John Koleszar's avatar
John Koleszar committed
235
      bc, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
Paul Wilkins's avatar
Paul Wilkins committed
236
      Pnew, cm->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
John Koleszar's avatar
John Koleszar committed
237
238
    );
  }
John Koleszar's avatar
John Koleszar committed
239
240
}

John Koleszar's avatar
John Koleszar committed
241
static int get_prob(int num, int den) {
242
243
244
245
246
247
248
249
250
251
252
  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
253
254
255
256
static int get_binary_prob(int n0, int n1) {
  return get_prob(n0, n0 + n1);
}

John Koleszar's avatar
John Koleszar committed
257
void update_skip_probs(VP8_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
258
  VP8_COMMON *const pc = &cpi->common;
John Koleszar's avatar
John Koleszar committed
259
260
  int prob_skip_false[3] = {0, 0, 0};
  int k;
Paul Wilkins's avatar
Paul Wilkins committed
261

John Koleszar's avatar
John Koleszar committed
262
  for (k = 0; k < MBSKIP_CONTEXTS; ++k) {
John Koleszar's avatar
John Koleszar committed
263
264
    pc->mbskip_pred_probs[k] = get_binary_prob(cpi->skip_false_count[k],
                                               cpi->skip_true_count[k]);
John Koleszar's avatar
John Koleszar committed
265
  }
Paul Wilkins's avatar
Paul Wilkins committed
266
267
}

268
#if CONFIG_SWITCHABLE_INTERP
John Koleszar's avatar
John Koleszar committed
269
void update_switchable_interp_probs(VP8_COMP *cpi, vp8_writer* const bc) {
John Koleszar's avatar
John Koleszar committed
270
  VP8_COMMON *const pc = &cpi->common;
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  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
294
      vp8_write_literal(bc, pc->fc.switchable_interp_prob[j][i], 8);
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
      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

320
// This function updates the reference frame prediction stats
John Koleszar's avatar
John Koleszar committed
321
static void update_refpred_stats(VP8_COMP *cpi) {
John Koleszar's avatar
John Koleszar committed
322
  VP8_COMMON *const cm = &cpi->common;
John Koleszar's avatar
John Koleszar committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  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
340
341
      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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

      // 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;
359
360

    }
John Koleszar's avatar
John Koleszar committed
361
  }
362
363
}

John Koleszar's avatar
John Koleszar committed
364
365
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
366
367
}

John Koleszar's avatar
John Koleszar committed
368
369
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
370
371
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
372
373
374
375
376
377
#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
378
379
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
380
}
Yaowu Xu's avatar
Yaowu Xu committed
381

John Koleszar's avatar
John Koleszar committed
382
383
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
384
385
386
}


John Koleszar's avatar
John Koleszar committed
387
388
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
389
390
}

John Koleszar's avatar
John Koleszar committed
391
392
393
394
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
395
396
}

397
398
static int prob_update_savings(const unsigned int *ct,
                               const vp8_prob oldp, const vp8_prob newp,
John Koleszar's avatar
John Koleszar committed
399
400
401
402
403
                               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);
404
405
}

406
static int prob_diff_update_savings(const unsigned int *ct,
John Koleszar's avatar
John Koleszar committed
407
408
409
410
411
412
413
                                    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);
414
415
416
}

static int prob_diff_update_savings_search(const unsigned int *ct,
John Koleszar's avatar
John Koleszar committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
                                           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;
434
    }
John Koleszar's avatar
John Koleszar committed
435
436
437
  }
  *bestp = bestnewp;
  return bestsavings;
438
439
}

John Koleszar's avatar
John Koleszar committed
440
static void pack_mb_tokens(vp8_writer* const bc,
441
442
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
John Koleszar's avatar
John Koleszar committed
443
444
  unsigned int split;
  unsigned int shift;
John Koleszar's avatar
John Koleszar committed
445
446
447
  int count = bc->count;
  unsigned int range = bc->range;
  unsigned int lowvalue = bc->lowvalue;
448
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
449

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

459
460
461
462
463
464
    if (t == EOSB_TOKEN)
    {
      ++p;
      break;
    }

John Koleszar's avatar
John Koleszar committed
465
466
467
468
469
    /* 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
470

John Koleszar's avatar
John Koleszar committed
471
472
473
474
    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
475

John Koleszar's avatar
John Koleszar committed
476
477
478
479
480
481
      if (bb) {
        lowvalue += split;
        range = range - split;
      } else {
        range = split;
      }
John Koleszar's avatar
John Koleszar committed
482

John Koleszar's avatar
John Koleszar committed
483
484
485
      shift = vp8_norm[range];
      range <<= shift;
      count += shift;
John Koleszar's avatar
John Koleszar committed
486

John Koleszar's avatar
John Koleszar committed
487
488
      if (count >= 0) {
        int offset = shift - count;
John Koleszar's avatar
John Koleszar committed
489

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

John Koleszar's avatar
John Koleszar committed
493
494
          while (x >= 0 && bc->buffer[x] == 0xff) {
            bc->buffer[x] = (unsigned char)0;
John Koleszar's avatar
John Koleszar committed
495
496
            x--;
          }
John Koleszar's avatar
John Koleszar committed
497

John Koleszar's avatar
John Koleszar committed
498
          bc->buffer[x] += 1;
John Koleszar's avatar
John Koleszar committed
499
        }
John Koleszar's avatar
John Koleszar committed
500

John Koleszar's avatar
John Koleszar committed
501
        bc->buffer[bc->pos++] = (lowvalue >> (24 - offset));
John Koleszar's avatar
John Koleszar committed
502
503
504
505
506
        lowvalue <<= offset;
        shift = count;
        lowvalue &= 0xffffff;
        count -= 8;
      }
John Koleszar's avatar
John Koleszar committed
507

John Koleszar's avatar
John Koleszar committed
508
509
      lowvalue <<= shift;
    } while (n);
John Koleszar's avatar
John Koleszar committed
510
511


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

John Koleszar's avatar
John Koleszar committed
515
516
517
518
519
      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
520

John Koleszar's avatar
John Koleszar committed
521
522
523
524
        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
525

John Koleszar's avatar
John Koleszar committed
526
527
528
529
530
531
          if (bb) {
            lowvalue += split;
            range = range - split;
          } else {
            range = split;
          }
John Koleszar's avatar
John Koleszar committed
532

John Koleszar's avatar
John Koleszar committed
533
534
535
          shift = vp8_norm[range];
          range <<= shift;
          count += shift;
John Koleszar's avatar
John Koleszar committed
536

John Koleszar's avatar
John Koleszar committed
537
538
          if (count >= 0) {
            int offset = shift - count;
John Koleszar's avatar
John Koleszar committed
539

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

John Koleszar's avatar
John Koleszar committed
543
544
              while (x >= 0 && bc->buffer[x] == 0xff) {
                bc->buffer[x] = (unsigned char)0;
John Koleszar's avatar
John Koleszar committed
545
546
547
                x--;
              }

John Koleszar's avatar
John Koleszar committed
548
              bc->buffer[x] += 1;
John Koleszar's avatar
John Koleszar committed
549
550
            }

John Koleszar's avatar
John Koleszar committed
551
            bc->buffer[bc->pos++] = (lowvalue >> (24 - offset));
John Koleszar's avatar
John Koleszar committed
552
553
554
555
556
            lowvalue <<= offset;
            shift = count;
            lowvalue &= 0xffffff;
            count -= 8;
          }
John Koleszar's avatar
John Koleszar committed
557

John Koleszar's avatar
John Koleszar committed
558
559
560
          lowvalue <<= shift;
        } while (n);
      }
John Koleszar's avatar
John Koleszar committed
561
562


John Koleszar's avatar
John Koleszar committed
563
      {
John Koleszar's avatar
John Koleszar committed
564

John Koleszar's avatar
John Koleszar committed
565
        split = (range + 1) >> 1;
John Koleszar's avatar
John Koleszar committed
566

John Koleszar's avatar
John Koleszar committed
567
568
569
570
571
572
        if (e & 1) {
          lowvalue += split;
          range = range - split;
        } else {
          range = split;
        }
John Koleszar's avatar
John Koleszar committed
573

John Koleszar's avatar
John Koleszar committed
574
        range <<= 1;
John Koleszar's avatar
John Koleszar committed
575

John Koleszar's avatar
John Koleszar committed
576
        if ((lowvalue & 0x80000000)) {
John Koleszar's avatar
John Koleszar committed
577
          int x = bc->pos - 1;
John Koleszar's avatar
John Koleszar committed
578

John Koleszar's avatar
John Koleszar committed
579
580
          while (x >= 0 && bc->buffer[x] == 0xff) {
            bc->buffer[x] = (unsigned char)0;
John Koleszar's avatar
John Koleszar committed
581
582
            x--;
          }
John Koleszar's avatar
John Koleszar committed
583

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

John Koleszar's avatar
John Koleszar committed
586
        }
John Koleszar's avatar
John Koleszar committed
587

John Koleszar's avatar
John Koleszar committed
588
589
590
591
        lowvalue  <<= 1;

        if (!++count) {
          count = -8;
John Koleszar's avatar
John Koleszar committed
592
          bc->buffer[bc->pos++] = (lowvalue >> 24);
John Koleszar's avatar
John Koleszar committed
593
          lowvalue &= 0xffffff;
John Koleszar's avatar
John Koleszar committed
594
        }
John Koleszar's avatar
John Koleszar committed
595
      }
John Koleszar's avatar
John Koleszar committed
596
597

    }
John Koleszar's avatar
John Koleszar committed
598
599
600
    ++p;
  }

John Koleszar's avatar
John Koleszar committed
601
602
603
  bc->count = count;
  bc->lowvalue = lowvalue;
  bc->range = range;
604
  *tp = p;
John Koleszar's avatar
John Koleszar committed
605
606
}

John Koleszar's avatar
John Koleszar committed
607
608
static void write_partition_size(unsigned char *cx_data, int size) {
  signed char csize;
John Koleszar's avatar
John Koleszar committed
609

John Koleszar's avatar
John Koleszar committed
610
611
612
613
614
615
  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
616
617
618
619
620

}

static void write_mv_ref
(
John Koleszar's avatar
John Koleszar committed
621
  vp8_writer *bc, MB_PREDICTION_MODE m, const vp8_prob *p
John Koleszar's avatar
John Koleszar committed
622
) {
623
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
624
  assert(NEARESTMV <= m  &&  m <= SPLITMV);
625
#endif
John Koleszar's avatar
John Koleszar committed
626
  vp8_write_token(bc, vp8_mv_ref_tree, p,
John Koleszar's avatar
John Koleszar committed
627
                  vp8_mv_ref_encoding_array - NEARESTMV + m);
John Koleszar's avatar
John Koleszar committed
628
629
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
630
#if CONFIG_SUPERBLOCKS
John Koleszar's avatar
John Koleszar committed
631
632
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
633
634
635
#if CONFIG_DEBUG
  assert(NEARESTMV <= m  &&  m < SPLITMV);
#endif
John Koleszar's avatar
John Koleszar committed
636
  vp8_write_token(bc, vp8_sb_mv_ref_tree, p,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
637
638
639
640
                  vp8_sb_mv_ref_encoding_array - NEARESTMV + m);
}
#endif

John Koleszar's avatar
John Koleszar committed
641
642
static void write_sub_mv_ref
(
John Koleszar's avatar
John Koleszar committed
643
  vp8_writer *bc, B_PREDICTION_MODE m, const vp8_prob *p
John Koleszar's avatar
John Koleszar committed
644
) {
645
#if CONFIG_DEBUG
John Koleszar's avatar
John Koleszar committed
646
  assert(LEFT4X4 <= m  &&  m <= NEW4X4);
647
#endif
John Koleszar's avatar
John Koleszar committed
648
  vp8_write_token(bc, vp8_sub_mv_ref_tree, p,
John Koleszar's avatar
John Koleszar committed
649
                  vp8_sub_mv_ref_encoding_array - LEFT4X4 + m);
John Koleszar's avatar
John Koleszar committed
650
651
}

652
#if CONFIG_NEWMVENTROPY
John Koleszar's avatar
John Koleszar committed
653
654
static void write_nmv(vp8_writer *bc, const MV *mv, const int_mv *ref,
                      const nmv_context *nmvc, int usehp) {
655
656
657
658
  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
659
660
  vp8_encode_nmv(bc, &e, &ref->as_mv, nmvc);
  vp8_encode_nmv_fp(bc, &e, &ref->as_mv, nmvc, usehp);
661
662
663
664
}

#else

John Koleszar's avatar
John Koleszar committed
665
666
static void write_mv
(
John Koleszar's avatar
John Koleszar committed
667
  vp8_writer *bc, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc
John Koleszar's avatar
John Koleszar committed
668
669
670
671
672
) {
  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
673
  vp8_encode_motion_vector(bc, &e, mvc);
John Koleszar's avatar
John Koleszar committed
674
675
}

676
677
static void write_mv_hp
(
John Koleszar's avatar
John Koleszar committed
678
  vp8_writer *bc, const MV *mv, const int_mv *ref, const MV_CONTEXT_HP *mvc
John Koleszar's avatar
John Koleszar committed
679
680
681
682
683
) {
  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
684
  vp8_encode_motion_vector_hp(bc, &e, mvc);
685
}
686
#endif  /* CONFIG_NEWMVENTROPY */
687

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

        // TRAP.. This should not happen
      default:
John Koleszar's avatar
John Koleszar committed
714
715
        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
716
        break;
John Koleszar's avatar
John Koleszar committed
717
    }
John Koleszar's avatar
John Koleszar committed
718
  }
John Koleszar's avatar
John Koleszar committed
719
720
}

Paul Wilkins's avatar
Paul Wilkins committed
721
// This function encodes the reference frame
John Koleszar's avatar
John Koleszar committed
722
static void encode_ref_frame(vp8_writer *const bc,
John Koleszar's avatar
John Koleszar committed
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
753
754
755
756
757
758
                             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
759
    vp8_write(bc, prediction_flag, pred_prob);
John Koleszar's avatar
John Koleszar committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780

    // 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
781
        vp8_write(bc, (rf != INTRA_FRAME), mod_refprobs[0]);
John Koleszar's avatar
John Koleszar committed
782
783
784
785
786
      }

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

John Koleszar's avatar
John Koleszar committed
790
791
        if (rf != LAST_FRAME) {
          if (mod_refprobs[2]) {
John Koleszar's avatar
John Koleszar committed
792
            vp8_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]);
John Koleszar's avatar
John Koleszar committed
793
          }
794
        }
John Koleszar's avatar
John Koleszar committed
795
      }
Paul Wilkins's avatar
Paul Wilkins committed
796
    }
John Koleszar's avatar
John Koleszar committed
797
  }
Paul Wilkins's avatar
Paul Wilkins committed
798

John Koleszar's avatar
John Koleszar committed
799
800
  // 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
801
}
John Koleszar's avatar
John Koleszar committed
802

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

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

John Koleszar's avatar
John Koleszar committed
812
813
814
  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]);
815

John Koleszar's avatar
John Koleszar committed
816
817
818
  // Compute a modified set of probabilities to use when prediction of the
  // reference frame fails
  compute_mod_refprobs(cm);
819
820
}

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

John Koleszar's avatar
John Koleszar committed
837
838
839
  const int mis = pc->mode_info_stride;
  int mb_row, mb_col;
  int row, col;
John Koleszar's avatar
John Koleszar committed
840

John Koleszar's avatar
John Koleszar committed
841
842
843
  // Values used in prediction model coding
  vp8_prob pred_prob;
  unsigned char prediction_flag;
844

John Koleszar's avatar
John Koleszar committed
845
846
  int row_delta[4] = { 0, +1,  0, -1};
  int col_delta[4] = { +1, -1, +1, +1};
Adrian Grange's avatar
Adrian Grange committed
847

848
849
  //final_packing = !cpi->dummy_packing;

John Koleszar's avatar
John Koleszar committed
850
  cpi->mb.partition_info = cpi->mb.pi;
851

John Koleszar's avatar
John Koleszar committed
852
853
854
855
856
857
858
859
860
861
862
  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
863
#if CONFIG_SUPERBLOCKS
John Koleszar's avatar
John Koleszar committed
864
      vp8_write(bc, m->mbmi.encoded_as_sb, pc->sb_coded);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
865
#endif
John Koleszar's avatar
John Koleszar committed
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
      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
885

John Koleszar's avatar
John Koleszar committed
886
        mi = &m->mbmi;
John Koleszar's avatar
John Koleszar committed
887
888
889
        rf = mi->ref_frame;
        mode = mi->mode;
        segment_id = mi->segment_id;
890

John Koleszar's avatar
John Koleszar committed
891
892
893
894
895
896
897
        // 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;
898

John Koleszar's avatar
John Koleszar committed
899
900
901
        // 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
902

John Koleszar's avatar
John Koleszar committed
903
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
904
        active_section = 9;
John Koleszar's avatar
John Koleszar committed
905
906
#endif

John Koleszar's avatar
John Koleszar committed
907
908
909
910
911
        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);
912

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

John Koleszar's avatar
John Koleszar committed
916
917
            // If the mb segment id wasn't predicted code explicitly
            if (!prediction_flag)
John Koleszar's avatar
John Koleszar committed
918
              write_mb_segid(bc, mi, &cpi->mb.e_mbd);
John Koleszar's avatar
John Koleszar committed
919
920
          } else {
            // Normal unpredicted coding
John Koleszar's avatar
John Koleszar committed
921
            write_mb_segid(bc, mi, &cpi->mb.e_mbd);
John Koleszar's avatar
John Koleszar committed
922
923
          }
        }
John Koleszar's avatar
John Koleszar committed
924

John Koleszar's avatar
John Koleszar committed
925
926
927
        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
928
929
930
931
932
933
934
935
          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
936
          vp8_encode_bool(bc, skip_coeff,
John Koleszar's avatar
John Koleszar committed
937
938
                          get_pred_prob(pc, xd, PRED_MBSKIP));
        }
John Koleszar's avatar
John Koleszar committed
939

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

John Koleszar's avatar
John Koleszar committed
943
        if (rf == INTRA_FRAME) {
944
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
945
          active_section = 6;
946
#endif
Paul Wilkins's avatar
Paul Wilkins committed
947

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

John Koleszar's avatar
John Koleszar committed
950
          if (!segfeature_active(xd, segment_id, SEG_LVL_MODE)) {
John Koleszar's avatar
John Koleszar committed
951
            write_ymode(bc, mode, pc->fc.ymode_prob);
John Koleszar's avatar
John Koleszar committed
952
          }
John Koleszar's avatar
John Koleszar committed
953

John Koleszar's avatar
John Koleszar committed
954
955
          if (mode == B_PRED) {
            int j = 0;
956
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
957
958
959
            int uses_second =
              m->bmi[0].as_mode.second !=
              (B_PREDICTION_MODE)(B_DC_PRED - 1);
John Koleszar's avatar
John Koleszar committed
960
            vp8_write(bc, uses_second, 128);
961
#endif
John Koleszar's avatar
John Koleszar committed
962
            do {
963
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
964
              B_PREDICTION_MODE mode2 = m->bmi[j].as_mode.second;
965
#endif
John Koleszar's avatar
John Koleszar committed
966
              write_bmode(bc, m->bmi[j].as_mode.first,
John Koleszar's avatar
John Koleszar committed
967
                          pc->fc.bmode_prob);
Deb Mukherjee's avatar
Deb Mukherjee committed
968
969
970
971
972
973
974
975
              /*
              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);
              }
              */
976
#if CONFIG_COMP_INTRA_PRED
John Koleszar's avatar
John Koleszar committed
977
              if (uses_second) {
John Koleszar's avatar
John Koleszar committed
978
                write_bmode(bc, mode2, pc->fc.bmode_prob);
John Koleszar's avatar
John Koleszar committed
979
980
981
982
983
              }
#endif
            } while (++j < 16);
          }
          if (mode == I8X8_PRED) {
John Koleszar's avatar
John Koleszar committed
984
            write_i8x8_mode(bc, m->bmi[0].as_mode.first,
John Koleszar's avatar
John Koleszar committed
985
                            pc->fc.i8x8_mode_prob);
John Koleszar's avatar
John Koleszar committed
986
            write_i8x8_mode(bc, m->bmi[2].as_mode.first,
John Koleszar's avatar
John Koleszar committed
987
                            pc->fc.i8x8_mode_prob);
John Koleszar's avatar
John Koleszar committed
988
            write_i8x8_mode(bc, m->bmi[8].as_mode.first,
John Koleszar's avatar
John Koleszar committed
989
                            pc->fc.i8x8_mode_prob);
John Koleszar's avatar
John Koleszar committed
990
            write_i8x8_mode(bc, m->bmi[10].as_mode.first,
John Koleszar's avatar
John Koleszar committed
991
992
                            pc->fc.i8x8_mode_prob);
          } else {
John Koleszar's avatar
John Koleszar committed
993
            write_uv_mode(bc, mi->uv_mode,
John Koleszar's avatar
John Koleszar committed
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
                          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);
1007
1008
1009
#if CONFIG_NEWBESTREFMV
            best_mv.as_int = mi->ref_mv.as_int;
#endif
John Koleszar's avatar
John Koleszar committed
1010
            vp8_mv_ref_probs(&cpi->common, mv_ref_p, ct);
Yaowu Xu's avatar
Yaowu Xu committed
1011

John Koleszar's avatar
John Koleszar committed
1012
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1013
            accum_mv_refs(mode, ct);
John Koleszar's avatar
John Koleszar committed
1014
#endif
John Koleszar's avatar
John Koleszar committed
1015
          }
John Koleszar's avatar
John Koleszar committed
1016
1017

#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1018
          active_section = 3;
John Koleszar's avatar
John Koleszar committed
1019
1020
#endif

John Koleszar's avatar
John Koleszar committed
1021
1022
          // 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
1023
1024
#if CONFIG_SUPERBLOCKS
            if (mi->encoded_as_sb) {
John Koleszar's avatar
John Koleszar committed
1025
              write_sb_mv_ref(bc, mode, mv_ref_p);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1026
1027
1028
            } else
#endif
            {
John Koleszar's avatar
John Koleszar committed
1029
              write_mv_ref(bc, mode, mv_ref_p);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1030
            }
John Koleszar's avatar
John Koleszar committed
1031
1032
            vp8_accum_mv_refs(&cpi->common, mode, ct);
          }
1033

1034
#if CONFIG_PRED_FILTER
John Koleszar's avatar
John Koleszar committed
1035
1036
1037
          // Is the prediction filter enabled
          if (mode >= NEARESTMV && mode < SPLITMV) {
            if (cpi->common.pred_filter_mode == 2)
John Koleszar's avatar
John Koleszar committed
1038
              vp8_write(bc, mi->pred_filter_enabled,
John Koleszar's avatar
John Koleszar committed
1039
1040
1041
1042
1043
                        pc->prob_pred_filter_off);
            else
              assert(mi->pred_filter_enabled ==
                     cpi->common.pred_filter_mode);
          }
1044
1045
1046
1047
1048
#endif
#if CONFIG_SWITCHABLE_INTERP
          if (mode >= NEARESTMV && mode <= SPLITMV)
          {
            if (cpi->common.mcomp_filter_type == SWITCHABLE) {
John Koleszar's avatar
John Koleszar committed
1049
              vp8_write_token(bc, vp8_switchable_interp_tree,
1050
1051
1052
1053
1054
1055
1056
1057
1058
                              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
1059
1060
1061
1062
1063
1064
1065
1066
#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,
1067
1068
1069
1070
1071
                              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
1072
1073
1074
1075
1076
          }

          // 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
1077
            vp8_write(bc, mi->second_ref_frame != INTRA_FRAME,
John Koleszar's avatar
John Koleszar committed
1078
1079
1080
1081
1082
1083
                      get_pred_prob(pc, xd, PRED_COMP));
          }

          {
            switch (mode) { /* new, split require MVs */
              case NEWMV:
1084
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1085
                active_section = 5;
1086
#endif
John Koleszar's avatar
John Koleszar committed
1087

Paul Wilkins's avatar
Paul Wilkins committed
1088
#if 0 //CONFIG_NEW_MVREF
Paul Wilkins's avatar
Paul Wilkins committed
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
                {
                  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
1099

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