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

11
12
13
#include <assert.h>
#include <stdio.h>
#include <limits.h>
John Koleszar's avatar
John Koleszar committed
14

15
16
17
#include "vpx/vpx_encoder.h"
#include "vpx_mem/vpx_mem.h"

18
#include "vp9/common/vp9_entropymode.h"
19
#include "vp9/common/vp9_entropymv.h"
20
#include "vp9/common/vp9_findnearmv.h"
21
#include "vp9/common/vp9_tile_common.h"
22
23
24
25
26
#include "vp9/common/vp9_seg_common.h"
#include "vp9/common/vp9_pred_common.h"
#include "vp9/common/vp9_entropy.h"
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_mvref_common.h"
27
#include "vp9/common/vp9_treecoder.h"
28
29
30
31
32
33
34
35
36
#include "vp9/common/vp9_systemdependent.h"
#include "vp9/common/vp9_pragmas.h"

#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/encoder/vp9_encodemv.h"
#include "vp9/encoder/vp9_bitstream.h"
#include "vp9/encoder/vp9_segmentation.h"
#include "vp9/encoder/vp9_write_bit_buffer.h"

Paul Wilkins's avatar
Paul Wilkins committed
37

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

#ifdef ENTROPY_STATS
43
44
45
int intra_mode_stats[VP9_INTRA_MODES]
                    [VP9_INTRA_MODES]
                    [VP9_INTRA_MODES];
46
vp9_coeff_stats tree_update_hist[TX_SIZE_MAX_SB][BLOCK_TYPES];
47

John Koleszar's avatar
John Koleszar committed
48
49
50
extern unsigned int active_section;
#endif

51
52
#define vp9_cost_upd  ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)) >> 8)
#define vp9_cost_upd256  ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)))
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifdef MODE_STATS
int64_t tx_count_32x32p_stats[TX_SIZE_MAX_SB][TX_SIZE_MAX_SB];
int64_t tx_count_16x16p_stats[TX_SIZE_MAX_SB - 1][TX_SIZE_MAX_SB - 1];
int64_t tx_count_8x8p_stats[TX_SIZE_MAX_SB - 2][TX_SIZE_MAX_SB - 2];

void init_tx_count_stats() {
  vp9_zero(tx_count_32x32p_stats);
  vp9_zero(tx_count_16x16p_stats);
  vp9_zero(tx_count_8x8p_stats);
}

static void update_tx_count_stats(VP9_COMMON *cm) {
  int i, j;
  for (i = 0; i < TX_SIZE_MAX_SB; i++) {
    for (j = 0; j < TX_SIZE_MAX_SB; j++) {
      tx_count_32x32p_stats[i][j] += cm->fc.tx_count_32x32p[i][j];
    }
  }
  for (i = 0; i < TX_SIZE_MAX_SB - 1; i++) {
    for (j = 0; j < TX_SIZE_MAX_SB - 1; j++) {
      tx_count_16x16p_stats[i][j] += cm->fc.tx_count_16x16p[i][j];
    }
  }
  for (i = 0; i < TX_SIZE_MAX_SB - 2; i++) {
    for (j = 0; j < TX_SIZE_MAX_SB - 2; j++) {
      tx_count_8x8p_stats[i][j] += cm->fc.tx_count_8x8p[i][j];
    }
  }
}

void write_tx_count_stats() {
  int i, j;
  FILE *fp = fopen("tx_count.bin", "wb");
  fwrite(tx_count_32x32p_stats, sizeof(tx_count_32x32p_stats), 1, fp);
  fwrite(tx_count_16x16p_stats, sizeof(tx_count_16x16p_stats), 1, fp);
  fwrite(tx_count_8x8p_stats, sizeof(tx_count_8x8p_stats), 1, fp);
  fclose(fp);

  printf("vp9_default_tx_count_32x32p[TX_SIZE_MAX_SB][TX_SIZE_MAX_SB] = {\n");
  for (i = 0; i < TX_SIZE_MAX_SB; i++) {
    printf("{ ");
    for (j = 0; j < TX_SIZE_MAX_SB; j++) {
      printf("%"PRId64", ", tx_count_32x32p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
  printf("vp9_default_tx_count_16x16p[TX_SIZE_MAX_SB-1][TX_SIZE_MAX_SB-1] = {\n");
  for (i = 0; i < TX_SIZE_MAX_SB - 1; i++) {
    printf("{ ");
    for (j = 0; j < TX_SIZE_MAX_SB - 1; j++) {
      printf("%"PRId64", ", tx_count_16x16p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
  printf("vp9_default_tx_count_8x8p[TX_SIZE_MAX_SB-2][TX_SIZE_MAX_SB-2] = {\n");
  for (i = 0; i < TX_SIZE_MAX_SB - 2; i++) {
    printf("{ ");
    for (j = 0; j < TX_SIZE_MAX_SB - 2; j++) {
      printf("%"PRId64", ", tx_count_8x8p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
}
#endif

122
123
static int update_bits[255];

124
125
126
127
128
129
130
static INLINE void write_le32(uint8_t *p, int value) {
  p[0] = value;
  p[1] = value >> 8;
  p[2] = value >> 16;
  p[3] = value >> 24;
}

131

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

int recenter_nonneg(int v, int m) {
  if (v > (m << 1))
    return v;
  else if (v >= m)
    return ((v - m) << 1);
  else
    return ((m - v) << 1) - 1;
}

static int get_unsigned_bits(unsigned num_values) {
  int cat = 0;
  if ((num_values--) <= 1) return 0;
  while (num_values > 0) {
    cat++;
    num_values >>= 1;
  }
  return cat;
}

152
153
154
155
156
void vp9_encode_unsigned_max(struct vp9_write_bit_buffer *wb,
                             int data, int max) {
  vp9_wb_write_literal(wb, data, get_unsigned_bits(max));
}

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
void encode_uniform(vp9_writer *w, int v, int n) {
  int l = get_unsigned_bits(n);
  int m;
  if (l == 0)
    return;
  m = (1 << l) - n;
  if (v < m) {
    vp9_write_literal(w, v, l - 1);
  } else {
    vp9_write_literal(w, m + ((v - m) >> 1), l - 1);
    vp9_write_literal(w, (v - m) & 1, 1);
  }
}

int count_uniform(int v, int n) {
  int l = get_unsigned_bits(n);
  int m;
  if (l == 0) return 0;
  m = (1 << l) - n;
  if (v < m)
    return l - 1;
  else
    return l;
}

void encode_term_subexp(vp9_writer *w, int word, int k, int num_syms) {
  int i = 0;
  int mk = 0;
  while (1) {
    int b = (i ? k + i - 1 : k);
    int a = (1 << b);
    if (num_syms <= mk + 3 * a) {
      encode_uniform(w, word - mk, num_syms - mk);
      break;
    } else {
      int t = (word >= mk + a);
      vp9_write_literal(w, t, 1);
      if (t) {
        i = i + 1;
        mk += a;
      } else {
        vp9_write_literal(w, word - mk, b);
        break;
      }
    }
  }
}

int count_term_subexp(int word, int k, int num_syms) {
  int count = 0;
  int i = 0;
  int mk = 0;
  while (1) {
    int b = (i ? k + i - 1 : k);
    int a = (1 << b);
    if (num_syms <= mk + 3 * a) {
      count += count_uniform(word - mk, num_syms - mk);
      break;
    } else {
      int t = (word >= mk + a);
      count++;
      if (t) {
        i = i + 1;
        mk += a;
      } else {
        count += b;
        break;
      }
    }
  }
  return count;
}

John Koleszar's avatar
John Koleszar committed
230
231
232
static void compute_update_table() {
  int i;
  for (i = 0; i < 255; i++)
233
    update_bits[i] = count_term_subexp(i, SUBEXP_PARAM, 255);
234
235
}

John Koleszar's avatar
John Koleszar committed
236
237
238
239
240
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;
241
242
}

John Koleszar's avatar
John Koleszar committed
243
244
245
246
247
static int remap_prob(int v, int m) {
  const int n = 256;
  const int modulus = MODULUS_PARAM;
  int i;
  if ((m << 1) <= n)
248
    i = recenter_nonneg(v, m) - 1;
John Koleszar's avatar
John Koleszar committed
249
  else
250
    i = recenter_nonneg(n - 1 - v, n - 1 - m) - 1;
John Koleszar's avatar
John Koleszar committed
251
252
253

  i = split_index(i, n - 1, modulus);
  return i;
254
}
255

256
static void write_prob_diff_update(vp9_writer *w,
257
                                   vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
258
  int delp = remap_prob(newp, oldp);
259
  encode_term_subexp(w, delp, SUBEXP_PARAM, 255);
260
261
}

262
static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
John Koleszar's avatar
John Koleszar committed
263
264
  int delp = remap_prob(newp, oldp);
  return update_bits[delp] * 256;
265
}
266

267
static int prob_update_savings(const unsigned int *ct,
268
269
270
271
272
                               const vp9_prob oldp, const vp9_prob newp,
                               const vp9_prob upd) {
  const int old_b = cost_branch256(ct, oldp);
  const int new_b = cost_branch256(ct, newp);
  const int update_b = 2048 + vp9_cost_upd256;
273
  return old_b - new_b - update_b;
274
275
276
}

static int prob_diff_update_savings_search(const unsigned int *ct,
277
278
279
                                           const vp9_prob oldp, vp9_prob *bestp,
                                           const vp9_prob upd) {
  const int old_b = cost_branch256(ct, oldp);
John Koleszar's avatar
John Koleszar committed
280
  int new_b, update_b, savings, bestsavings, step;
281
  vp9_prob newp, bestnewp;
John Koleszar's avatar
John Koleszar committed
282
283
284
285
286
287

  bestsavings = 0;
  bestnewp = oldp;

  step = (*bestp > oldp ? -1 : 1);
  for (newp = *bestp; newp != oldp; newp += step) {
288
289
    new_b = cost_branch256(ct, newp);
    update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
John Koleszar's avatar
John Koleszar committed
290
291
292
293
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
294
    }
John Koleszar's avatar
John Koleszar committed
295
296
297
  }
  *bestp = bestnewp;
  return bestsavings;
298
299
}

300
301
302
303
static int prob_diff_update_savings_search_model(const unsigned int *ct,
                                                 const vp9_prob *oldp,
                                                 vp9_prob *bestp,
                                                 const vp9_prob upd,
304
                                                 int b, int r) {
305
306
  int i, old_b, new_b, update_b, savings, bestsavings, step;
  int newp;
307
  vp9_prob bestnewp, newplist[ENTROPY_NODES], oldplist[ENTROPY_NODES];
308
309
  vp9_model_to_full_probs(oldp, oldplist);
  vpx_memcpy(newplist, oldp, sizeof(vp9_prob) * UNCONSTRAINED_NODES);
310
311
312
  for (i = UNCONSTRAINED_NODES, old_b = 0; i < ENTROPY_NODES; ++i)
    old_b += cost_branch256(ct + 2 * i, oldplist[i]);
  old_b += cost_branch256(ct + 2 * PIVOT_NODE, oldplist[PIVOT_NODE]);
313
314

  bestsavings = 0;
315
  bestnewp = oldp[PIVOT_NODE];
316

317
  step = (*bestp > oldp[PIVOT_NODE] ? -1 : 1);
318
  newp = *bestp;
319
  for (; newp != oldp[PIVOT_NODE]; newp += step) {
320
    if (newp < 1 || newp > 255) continue;
321
    newplist[PIVOT_NODE] = newp;
322
    vp9_model_to_full_probs(newplist, newplist);
323
    for (i = UNCONSTRAINED_NODES, new_b = 0; i < ENTROPY_NODES; ++i)
324
      new_b += cost_branch256(ct + 2 * i, newplist[i]);
325
326
    new_b += cost_branch256(ct + 2 * PIVOT_NODE, newplist[PIVOT_NODE]);
    update_b = prob_diff_update_cost(newp, oldp[PIVOT_NODE]) +
327
328
329
330
331
332
333
334
335
336
337
        vp9_cost_upd256;
    savings = old_b - new_b - update_b;
    if (savings > bestsavings) {
      bestsavings = savings;
      bestnewp = newp;
    }
  }
  *bestp = bestnewp;
  return bestsavings;
}

338
339
340
341
342
static void vp9_cond_prob_update(vp9_writer *bc, vp9_prob *oldp, vp9_prob upd,
                                 unsigned int *ct) {
  vp9_prob newp;
  int savings;
  newp = get_binary_prob(ct[0], ct[1]);
343
  assert(newp >= 1);
344
345
346
  savings = prob_update_savings(ct, *oldp, newp, upd);
  if (savings > 0) {
    vp9_write(bc, 1, upd);
347
    vp9_write_prob(bc, newp);
348
349
350
351
352
353
    *oldp = newp;
  } else {
    vp9_write(bc, 0, upd);
  }
}

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
static void vp9_cond_prob_diff_update(vp9_writer *bc, vp9_prob *oldp,
                                      vp9_prob upd,
                                      unsigned int *ct) {
  vp9_prob newp;
  int savings;
  newp = get_binary_prob(ct[0], ct[1]);
  assert(newp >= 1);
  savings = prob_diff_update_savings_search(ct, *oldp, &newp, upd);
  if (savings > 0) {
    vp9_write(bc, 1, upd);
    write_prob_diff_update(bc, newp, *oldp);
    *oldp = newp;
  } else {
    vp9_write(bc, 0, upd);
  }
}

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
static void update_mode(
  vp9_writer *w,
  int n,
  const struct vp9_token tok[/* n */],
  vp9_tree tree,
  vp9_prob Pnew[/* n-1 */],
  vp9_prob Pcur[/* n-1 */],
  unsigned int bct[/* n-1 */] [2],
  const unsigned int num_events[/* n */]
) {
  int i = 0;

  vp9_tree_probs_from_distribution(tree, Pnew, bct, num_events, 0);
  n--;

  for (i = 0; i < n; ++i) {
    vp9_cond_prob_diff_update(w, &Pcur[i], VP9_DEF_UPDATE_PROB, bct[i]);
  }
}

static void update_mbintra_mode_probs(VP9_COMP* const cpi,
                                      vp9_writer* const bc) {
  VP9_COMMON *const cm = &cpi->common;
394
  int j;
395
396
397
  vp9_prob pnew[VP9_INTRA_MODES - 1];
  unsigned int bct[VP9_INTRA_MODES - 1][2];

398
399
400
401
402
  for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
    update_mode(bc, VP9_INTRA_MODES, vp9_intra_mode_encodings,
                vp9_intra_mode_tree, pnew,
                cm->fc.y_mode_prob[j], bct,
                (unsigned int *)cpi->y_mode_count[j]);
403
404
}

Deb Mukherjee's avatar
Deb Mukherjee committed
405
void vp9_update_skip_probs(VP9_COMP *cpi, vp9_writer *bc) {
406
407
408
  VP9_COMMON *const pc = &cpi->common;
  int k;

Deb Mukherjee's avatar
Deb Mukherjee committed
409
410
411
412
413
414
415
416
  for (k = 0; k < MBSKIP_CONTEXTS; ++k) {
    vp9_cond_prob_diff_update(bc, &pc->fc.mbskip_probs[k],
                              VP9_DEF_UPDATE_PROB, pc->fc.mbskip_count[k]);
    /*
    pc->fc.mbskip_probs[k] = get_binary_prob(pc->fc.mbskip_count[k][0],
                                                  pc->fc.mbskip_count[k][1]);
                                                  */
  }
417
418
419
420
421
422
}

static void write_intra_mode(vp9_writer *bc, int m, const vp9_prob *p) {
  write_token(bc, vp9_intra_mode_tree, p, vp9_intra_mode_encodings + m);
}

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
static void update_switchable_interp_probs(VP9_COMMON *const pc,
                                           vp9_writer* const bc) {
  unsigned int branch_ct[VP9_SWITCHABLE_FILTERS + 1]
                        [VP9_SWITCHABLE_FILTERS - 1][2];
  vp9_prob new_prob[VP9_SWITCHABLE_FILTERS + 1][VP9_SWITCHABLE_FILTERS - 1];
  int i, j;
  for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
    vp9_tree_probs_from_distribution(
        vp9_switchable_interp_tree,
        new_prob[j], branch_ct[j],
        pc->fc.switchable_interp_count[j], 0);
  }
  for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
    for (i = 0; i < VP9_SWITCHABLE_FILTERS - 1; ++i) {
      // vp9_cond_prob_update(bc, &pc->fc.switchable_interp_prob[j][i],
      //                      VP9_DEF_UPDATE_PROB, branch_ct[j][i]);
      vp9_cond_prob_diff_update(bc, &pc->fc.switchable_interp_prob[j][i],
                                VP9_DEF_UPDATE_PROB, branch_ct[j][i]);
    }
  }
}

static void update_inter_mode_probs(VP9_COMMON *pc, vp9_writer* const bc) {
  int i, j;

  for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
449
    for (j = 0; j < VP9_INTER_MODES - 1; j++) {
450
451
452
453
454
455
456
457
458
459
      vp9_cond_prob_diff_update(bc, &pc->fc.inter_mode_probs[i][j],
                                VP9_DEF_UPDATE_PROB,
                                pc->fc.inter_mode_counts[i][j]);
      // vp9_cond_prob_update(
      //     bc, &pc->fc.inter_mode_probs[i][j],
      //     VP9_DEF_UPDATE_PROB, pc->fc.inter_mode_counts[i][j]);
    }
  }
}

460
static void pack_mb_tokens(vp9_writer* const bc,
461
462
463
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
464

John Koleszar's avatar
John Koleszar committed
465
  while (p < stop) {
466
    const int t = p->token;
467
    const struct vp9_token *const a = vp9_coef_encodings + t;
468
    const vp9_extra_bit *const b = vp9_extra_bits + t;
John Koleszar's avatar
John Koleszar committed
469
    int i = 0;
470
    const vp9_prob *pp;
John Koleszar's avatar
John Koleszar committed
471
    int v = a->value;
472
    int n = a->len;
473
    vp9_prob probs[ENTROPY_NODES];
John Koleszar's avatar
John Koleszar committed
474

475
    if (t == EOSB_TOKEN) {
476
477
478
      ++p;
      break;
    }
479
    if (t >= TWO_TOKEN) {
480
      vp9_model_to_full_probs(p->context_tree, probs);
481
482
483
484
      pp = probs;
    } else {
      pp = p->context_tree;
    }
485
    assert(pp != 0);
486

John Koleszar's avatar
John Koleszar committed
487
    /* skip one or two nodes */
488
#if !CONFIG_BALANCED_COEFTREE
John Koleszar's avatar
John Koleszar committed
489
490
491
492
    if (p->skip_eob_node) {
      n -= p->skip_eob_node;
      i = 2 * p->skip_eob_node;
    }
493
#endif
John Koleszar's avatar
John Koleszar committed
494

John Koleszar's avatar
John Koleszar committed
495
496
    do {
      const int bb = (v >> --n) & 1;
497
498
499
500
501
502
503
#if CONFIG_BALANCED_COEFTREE
      if (i == 2 && p->skip_eob_node) {
        i += 2;
        assert(bb == 1);
        continue;
      }
#endif
504
      vp9_write(bc, bb, pp[i >> 1]);
505
      i = vp9_coef_tree[i + bb];
506
    } while (n);
John Koleszar's avatar
John Koleszar committed
507

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

511
      if (l) {
512
        const unsigned char *pb = b->prob;
John Koleszar's avatar
John Koleszar committed
513
        int v = e >> 1;
514
        int n = l;              /* number of bits in v, assumed nonzero */
John Koleszar's avatar
John Koleszar committed
515
        int i = 0;
John Koleszar's avatar
John Koleszar committed
516

John Koleszar's avatar
John Koleszar committed
517
518
        do {
          const int bb = (v >> --n) & 1;
519
          vp9_write(bc, bb, pb[i >> 1]);
John Koleszar's avatar
John Koleszar committed
520
521
522
          i = b->tree[i + bb];
        } while (n);
      }
John Koleszar's avatar
John Koleszar committed
523

524
      vp9_write_bit(bc, e & 1);
John Koleszar's avatar
John Koleszar committed
525
    }
John Koleszar's avatar
John Koleszar committed
526
527
528
    ++p;
  }

529
  *tp = p;
John Koleszar's avatar
John Koleszar committed
530
531
}

532
533
static void write_sb_mv_ref(vp9_writer *bc, MB_PREDICTION_MODE m,
                            const vp9_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
534
#if CONFIG_DEBUG
Ronald S. Bultje's avatar
Ronald S. Bultje committed
535
  assert(NEARESTMV <= m && m <= NEWMV);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
536
#endif
537
538
  write_token(bc, vp9_sb_mv_ref_tree, p,
              vp9_sb_mv_ref_encoding_array - NEARESTMV + m);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
539
540
}

Paul Wilkins's avatar
Paul Wilkins committed
541
542
// 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.
543
static void write_mb_segid(vp9_writer *bc,
Paul Wilkins's avatar
Paul Wilkins committed
544
                           const MB_MODE_INFO *mi, const MACROBLOCKD *xd) {
545
546
547
  if (xd->segmentation_enabled && xd->update_mb_segmentation_map)
    treed_write(bc, vp9_segment_tree, xd->mb_segment_tree_probs,
                mi->segment_id, 3);
John Koleszar's avatar
John Koleszar committed
548
549
}

Paul Wilkins's avatar
Paul Wilkins committed
550
// This function encodes the reference frame
Ronald S. Bultje's avatar
Ronald S. Bultje committed
551
552
553
554
555
556
557
558
static void encode_ref_frame(VP9_COMP *cpi, vp9_writer *bc) {
  VP9_COMMON *const pc = &cpi->common;
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *mi = &xd->mode_info_context->mbmi;
  const int segment_id = mi->segment_id;
  int seg_ref_active = vp9_segfeature_active(xd, segment_id,
                                             SEG_LVL_REF_FRAME);
John Koleszar's avatar
John Koleszar committed
559
560
  // If segment level coding of this signal is disabled...
  // or the segment allows multiple reference frame options
561
  if (!seg_ref_active) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
562
563
564
565
566
567
568
569
570
    // does the feature use compound prediction or not
    // (if not specified at the frame/segment level)
    if (pc->comp_pred_mode == HYBRID_PREDICTION) {
      vp9_write(bc, mi->ref_frame[1] > INTRA_FRAME,
                vp9_get_pred_prob(pc, xd, PRED_COMP_INTER_INTER));
    } else {
      assert((mi->ref_frame[1] <= INTRA_FRAME) ==
                 (pc->comp_pred_mode == SINGLE_PREDICTION_ONLY));
    }
571

Ronald S. Bultje's avatar
Ronald S. Bultje committed
572
573
574
575
576
577
578
579
580
    if (mi->ref_frame[1] > INTRA_FRAME) {
      vp9_write(bc, mi->ref_frame[0] == GOLDEN_FRAME,
                vp9_get_pred_prob(pc, xd, PRED_COMP_REF_P));
    } else {
      vp9_write(bc, mi->ref_frame[0] != LAST_FRAME,
                vp9_get_pred_prob(pc, xd, PRED_SINGLE_REF_P1));
      if (mi->ref_frame[0] != LAST_FRAME)
        vp9_write(bc, mi->ref_frame[0] != GOLDEN_FRAME,
                  vp9_get_pred_prob(pc, xd, PRED_SINGLE_REF_P2));
Paul Wilkins's avatar
Paul Wilkins committed
581
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
582
583
  } else {
    assert(mi->ref_frame[1] <= INTRA_FRAME);
584
585
    assert(vp9_get_segdata(xd, segment_id, SEG_LVL_REF_FRAME) ==
           mi->ref_frame[0]);
John Koleszar's avatar
John Koleszar committed
586
  }
Paul Wilkins's avatar
Paul Wilkins committed
587

John Koleszar's avatar
John Koleszar committed
588
589
  // 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
590
}
John Koleszar's avatar
John Koleszar committed
591

Ronald S. Bultje's avatar
Ronald S. Bultje committed
592
static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m,
593
                                vp9_writer *bc, int mi_row, int mi_col) {
594
  VP9_COMMON *const pc = &cpi->common;
595
  const nmv_context *nmvc = &pc->fc.nmvc;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
596
597
598
  MACROBLOCK *const x = &cpi->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *const mi = &m->mbmi;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
599
  const MV_REFERENCE_FRAME rf = mi->ref_frame[0];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
600
601
602
  const MB_PREDICTION_MODE mode = mi->mode;
  const int segment_id = mi->segment_id;
  int skip_coeff;
Adrian Grange's avatar
Adrian Grange committed
603

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
607
608
#ifdef ENTROPY_STATS
  active_section = 9;
609
#endif
610

Ronald S. Bultje's avatar
Ronald S. Bultje committed
611
612
613
614
615
  if (cpi->mb.e_mbd.update_mb_segmentation_map) {
    // Is temporal coding of the segment map enabled
    if (pc->temporal_update) {
      unsigned char prediction_flag = vp9_get_pred_flag(xd, PRED_SEG_ID);
      vp9_prob pred_prob = vp9_get_pred_prob(pc, xd, PRED_SEG_ID);
Paul Wilkins's avatar
Paul Wilkins committed
616

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
620
621
      // If the mb segment id wasn't predicted code explicitly
      if (!prediction_flag)
Paul Wilkins's avatar
Paul Wilkins committed
622
        write_mb_segid(bc, mi, &cpi->mb.e_mbd);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
623
624
625
626
627
    } else {
      // Normal unpredicted coding
      write_mb_segid(bc, mi, &cpi->mb.e_mbd);
    }
  }
628

629
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
630
631
    skip_coeff = 1;
  } else {
632
    skip_coeff = m->mbmi.mb_skip_coeff;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
633
634
635
    vp9_write(bc, skip_coeff,
              vp9_get_pred_prob(pc, xd, PRED_MBSKIP));
  }
John Koleszar's avatar
John Koleszar committed
636

637
638
639
  if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_REF_FRAME))
    vp9_write(bc, rf != INTRA_FRAME,
              vp9_get_pred_prob(pc, xd, PRED_INTRA_INTER));
Paul Wilkins's avatar
Paul Wilkins committed
640

641
642
643
644
  if (mi->sb_type >= BLOCK_SIZE_SB8X8 && pc->txfm_mode == TX_MODE_SELECT &&
      !(rf != INTRA_FRAME &&
        (skip_coeff || vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)))) {
    TX_SIZE sz = mi->txfm_size;
645
646
    const vp9_prob *tx_probs = vp9_get_pred_probs(pc, xd, PRED_TX_SIZE);
    vp9_write(bc, sz != TX_4X4, tx_probs[0]);
647
    if (mi->sb_type >= BLOCK_SIZE_MB16X16 && sz != TX_4X4) {
648
      vp9_write(bc, sz != TX_8X8, tx_probs[1]);
649
      if (mi->sb_type >= BLOCK_SIZE_SB32X32 && sz != TX_8X8)
650
        vp9_write(bc, sz != TX_16X16, tx_probs[2]);
651
652
653
    }
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
654
  if (rf == INTRA_FRAME) {
655
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
656
    active_section = 6;
657
#endif
Paul Wilkins's avatar
Paul Wilkins committed
658

659
    if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8) {
660
661
662
663
      const BLOCK_SIZE_TYPE bsize = xd->mode_info_context->mbmi.sb_type;
      const int bwl = b_width_log2(bsize), bhl = b_height_log2(bsize);
      const int bsl = MIN(bwl, bhl);
      write_intra_mode(bc, mode, pc->fc.y_mode_prob[MIN(3, bsl)]);
664
    } else {
665
666
667
668
      int idx, idy;
      int bw = 1 << b_width_log2(mi->sb_type);
      int bh = 1 << b_height_log2(mi->sb_type);
      for (idy = 0; idy < 2; idy += bh)
669
670
671
672
        for (idx = 0; idx < 2; idx += bw) {
          MB_PREDICTION_MODE bm = m->bmi[idy * 2 + idx].as_mode.first;
          write_intra_mode(bc, bm, pc->fc.y_mode_prob[0]);
        }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
673
    }
674
675
    write_intra_mode(bc, mi->uv_mode,
                     pc->fc.uv_mode_prob[mode]);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
676
  } else {
677
    vp9_prob mv_ref_p[VP9_INTER_MODES - 1];
678

Ronald S. Bultje's avatar
Ronald S. Bultje committed
679
680
    encode_ref_frame(cpi, bc);

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

John Koleszar's avatar
John Koleszar committed
683
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
684
    active_section = 3;
John Koleszar's avatar
John Koleszar committed
685
686
#endif

687
    // If segment skip is not enabled code the mode.
Paul Wilkins's avatar
Paul Wilkins committed
688
    if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
689
      if (mi->sb_type >= BLOCK_SIZE_SB8X8) {
690
        write_sb_mv_ref(bc, mode, mv_ref_p);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
691
692
        vp9_accum_mv_refs(&cpi->common, mode, mi->mb_mode_context[rf]);
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
693
    }
694

Ronald S. Bultje's avatar
Ronald S. Bultje committed
695
696
697
698
699
700
701
702
    if (cpi->common.mcomp_filter_type == SWITCHABLE) {
      write_token(bc, vp9_switchable_interp_tree,
                  vp9_get_pred_probs(&cpi->common, xd,
                                     PRED_SWITCHABLE_INTERP),
                  vp9_switchable_interp_encodings +
                  vp9_switchable_interp_map[mi->interp_filter]);
    } else {
      assert(mi->interp_filter == cpi->common.mcomp_filter_type);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
703
    }
704

Ronald S. Bultje's avatar
Ronald S. Bultje committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
    if (xd->mode_info_context->mbmi.sb_type < BLOCK_SIZE_SB8X8) {
      int j;
      MB_PREDICTION_MODE blockmode;
      int_mv blockmv;
      int bwl = b_width_log2(mi->sb_type), bw = 1 << bwl;
      int bhl = b_height_log2(mi->sb_type), bh = 1 << bhl;
      int idx, idy;
      for (idy = 0; idy < 2; idy += bh) {
        for (idx = 0; idx < 2; idx += bw) {
          j = idy * 2 + idx;
          blockmode = cpi->mb.partition_info->bmi[j].mode;
          blockmv = cpi->mb.partition_info->bmi[j].mv;
          write_sb_mv_ref(bc, blockmode, mv_ref_p);
          vp9_accum_mv_refs(&cpi->common, blockmode, mi->mb_mode_context[rf]);
          if (blockmode == NEWMV) {
720
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
721
            active_section = 11;
722
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
723
724
            vp9_encode_mv(bc, &blockmv.as_mv, &mi->best_mv.as_mv,
                          nmvc, xd->allow_high_precision_mv);
725

Ronald S. Bultje's avatar
Ronald S. Bultje committed
726
            if (mi->ref_frame[1] > INTRA_FRAME)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
727
728
729
              vp9_encode_mv(bc,
                            &cpi->mb.partition_info->bmi[j].second_mv.as_mv,
                            &mi->best_second_mv.as_mv,
730
                            nmvc, xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
731
          }
732
        }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
733
734
735
736
737
738
739
740
741
      }
    } else if (mode == NEWMV) {
#ifdef ENTROPY_STATS
      active_section = 5;
#endif
      vp9_encode_mv(bc,
                    &mi->mv[0].as_mv, &mi->best_mv.as_mv,
                    nmvc, xd->allow_high_precision_mv);

Ronald S. Bultje's avatar
Ronald S. Bultje committed
742
      if (mi->ref_frame[1] > INTRA_FRAME)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
743
744
745
        vp9_encode_mv(bc,
                      &mi->mv[1].as_mv, &mi->best_second_mv.as_mv,
                      nmvc, xd->allow_high_precision_mv);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
746
747
    }
  }
John Koleszar's avatar
John Koleszar committed
748
}
749

Ronald S. Bultje's avatar
Ronald S. Bultje committed
750
static void write_mb_modes_kf(const VP9_COMP *cpi,
751
                              MODE_INFO *m,
752
                              vp9_writer *bc, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
753
754
755
  const VP9_COMMON *const c = &cpi->common;
  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  const int ym = m->mbmi.mode;
756
  const int mis = c->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
757
758
  const int segment_id = m->mbmi.segment_id;
  int skip_coeff;
759

760
  if (xd->update_mb_segmentation_map)
761
762
    write_mb_segid(bc, &m->mbmi, xd);

763
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
764
765
    skip_coeff = 1;
  } else {
766
    skip_coeff = m->mbmi.mb_skip_coeff;
767
    vp9_write(bc, skip_coeff, vp9_get_pred_prob(c, xd, PRED_MBSKIP));
768
769
  }

770
771
  if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8 && c->txfm_mode == TX_MODE_SELECT) {
    TX_SIZE sz = m->mbmi.txfm_size;
772
773
    const vp9_prob *tx_probs = vp9_get_pred_probs(c, xd, PRED_TX_SIZE);
    vp9_write(bc, sz != TX_4X4, tx_probs[0]);
774
    if (m->mbmi.sb_type >= BLOCK_SIZE_MB16X16 && sz != TX_4X4) {
775
      vp9_write(bc, sz != TX_8X8, tx_probs[1]);
776
      if (m->mbmi.sb_type >= BLOCK_SIZE_SB32X32 && sz != TX_8X8)
777
        vp9_write(bc, sz != TX_16X16, tx_probs[2]);
778
779
780
    }
  }

781
  if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
782
783
    const MB_PREDICTION_MODE A = above_block_mode(m, 0, mis);
    const MB_PREDICTION_MODE L = xd->left_available ?
784
                                 left_block_mode(m, 0) : DC_PRED;
785
786
    write_intra_mode(bc, ym, c->kf_y_mode_prob[A][L]);
  } else {
787
788
789
    int idx, idy;
    int bw = 1 << b_width_log2(m->mbmi.sb_type);
    int bh = 1 << b_height_log2(m->mbmi.sb_type);
790
791
792
    for (idy = 0; idy < 2; idy += bh) {
      for (idx = 0; idx < 2; idx += bw) {
        int i = idy * 2 + idx;
Yaowu Xu's avatar
Yaowu Xu committed
793
794
        const MB_PREDICTION_MODE A = above_block_mode(m, i, mis);
        const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
795
                                     left_block_mode(m, i) : DC_PRED;
796
797
798
799
        const int bm = m->bmi[i].as_mode.first;
#ifdef ENTROPY_STATS
        ++intra_mode_stats[A][L][bm];
#endif
800
        write_intra_mode(bc, bm, c->kf_y_mode_prob[A][L]);
801
802
      }
    }
803
804
  }

805
  write_intra_mode(bc, m->mbmi.uv_mode, c->kf_uv_mode_prob[ym]);
806
807
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
808
809
static void write_modes_b(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                          TOKENEXTRA **tok, TOKENEXTRA *tok_end,
810
                          int mi_row, int mi_col) {
811
  VP9_COMMON *const cm = &cpi->common;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
812
813
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;

814
815
816
  if (m->mbmi.sb_type < BLOCK_SIZE_SB8X8)
    if (xd->ab_index > 0)
      return;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
817
  xd->mode_info_context = m;
818
819
820
  set_mi_row_col(&cpi->common, xd, mi_row,
                 1 << mi_height_log2(m->mbmi.sb_type),
                 mi_col, 1 << mi_width_log2(m->mbmi.sb_type));
821
  if (cm->frame_type == KEY_FRAME) {
822
    write_mb_modes_kf(cpi, m, bc, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
823
824
825
826
#ifdef ENTROPY_STATS
    active_section = 8;
#endif
  } else {
827
    pack_inter_mode_mvs(cpi, m, bc, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
828
829
830
831
832
833
834
835
836
#ifdef ENTROPY_STATS
    active_section = 1;
#endif
  }

  assert(*tok < tok_end);
  pack_mb_tokens(bc, tok, tok_end);
}

837
838
static void write_modes_sb(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                           TOKENEXTRA **tok, TOKENEXTRA *tok_end,
839
                           int mi_row, int mi_col,
840
841
                           BLOCK_SIZE_TYPE bsize) {
  VP9_COMMON *const cm = &cpi->common;
842
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
843
844
  const int mis = cm->mode_info_stride;
  int bwl, bhl;
845
846
  int bsl = b_width_log2(bsize);
  int bs = (1 << bsl) / 4;  // mode_info step for subsize
847
848
849
850
  int n;
  PARTITION_TYPE partition;
  BLOCK_SIZE_TYPE subsize;

851
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
852
853
    return;

854
855
  bwl = b_width_log2(m->mbmi.sb_type);
  bhl = b_height_log2(m->mbmi.sb_type);
856
857
858
859
860
861
862
863
864
865
866
867
868

  // parse the partition type
  if ((bwl == bsl) && (bhl == bsl))
    partition = PARTITION_NONE;
  else if ((bwl == bsl) && (bhl < bsl))
    partition = PARTITION_HORZ;
  else if ((bwl < bsl) && (bhl == bsl))
    partition = PARTITION_VERT;
  else if ((bwl < bsl) && (bhl < bsl))
    partition = PARTITION_SPLIT;
  else
    assert(0);

869
  if (bsize < BLOCK_SIZE_SB8X8)
870
    if (xd->ab_index > 0)
871
872
873
      return;

  if (bsize >= BLOCK_SIZE_SB8X8) {
874
    int pl;
875
876
    xd->left_seg_context = cm->left_seg_context + (mi_row & MI_MASK);
    xd->above_seg_context = cm->above_seg_context + mi_col;
877
    pl = partition_plane_context(xd, bsize);
878
    // encode the partition information
879
880
    write_token(bc, vp9_partition_tree,
                cm->fc.partition_prob[cm->frame_type][pl],
881
                vp9_partition_encodings + partition);
882
  }
883

884
  subsize = get_subsize(bsize, partition);
885
  *(get_sb_index(xd, subsize)) = 0;
886

887
888
  switch (partition) {
    case PARTITION_NONE:
889
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
890
891
      break;
    case PARTITION_HORZ:
892
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
893
      *(get_sb_index(xd, subsize)) = 1;
894
895
      if ((mi_row + bs) < cm->mi_rows)
        write_modes_b(cpi, m + bs * mis, bc, tok, tok_end, mi_row + bs, mi_col);
896
897
      break;
    case PARTITION_VERT:
898
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
899
      *(get_sb_index(xd, subsize)) = 1;
900
901
      if ((mi_col + bs) < cm->mi_cols)
        write_modes_b(cpi, m + bs, bc, tok, tok_end, mi_row, mi_col + bs);
902
903
904
905
      break;
    case PARTITION_SPLIT:
      for (n = 0; n < 4; n++) {
        int j = n >> 1, i = n & 0x01;
906
        *(get_sb_index(xd, subsize)) = n;
907
        write_modes_sb(cpi, m + j * bs * mis + i * bs, bc, tok, tok_end,
908
                       mi_row + j * bs, mi_col + i * bs, subsize);
909
910
911
912
913
      }
      break;
    default:
      assert(0);
  }
914
915

  // update partition context
916
917
  if (bsize >= BLOCK_SIZE_SB8X8 &&
      (bsize == BLOCK_SIZE_SB8X8 || partition != PARTITION_SPLIT)) {
918
919
920
    set_partition_seg_context(cm, xd, mi_row, mi_col);
    update_partition_context(xd, subsize, bsize);
  }
921
922
}

923
static void write_modes(VP9_COMP *cpi, vp9_writer* const bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
924
                        TOKENEXTRA **tok, TOKENEXTRA *tok_end) {
925
  VP9_COMMON *const c = &cpi->common;
John Koleszar's avatar
John Koleszar committed
926
  const int mis = c->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
927
  MODE_INFO *m, *m_ptr = c->mi;
928
  int mi_row, mi_col;
John Koleszar's avatar
John Koleszar committed
929

930
  m_ptr += c->cur_tile_mi_col_start + c->cur_tile_mi_row_start * mis;
931
  vpx_memset(c->above_seg_context, 0, sizeof(PARTITION_CONTEXT) *
932
             mi_cols_aligned_to_sb(c));
933

934
935
  for (mi_row = c->cur_tile_mi_row_start;
       mi_row < c->cur_tile_mi_row_end;
Jingning Han's avatar
Jingning Han committed
936
       mi_row += 8, m_ptr += 8 * mis) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
937
    m = m_ptr;
938
    vpx_memset(c->left_seg_context, 0, sizeof(c->left_seg_context));
939
940
    for (mi_col = c->cur_tile_mi_col_start;
         mi_col < c->cur_tile_mi_col_end;
941
         mi_col += 64 / MI_SIZE, m += 64 / MI_SIZE)
942
      write_modes_sb(cpi, m, bc, tok, tok_end, mi_row, mi_col,
943
                     BLOCK_SIZE_SB64X64);
John Koleszar's avatar
John Koleszar committed
944
  }
John Koleszar's avatar
John Koleszar committed
945
}
946

947
/* This function is used for debugging probability trees. */
948
static void print_prob_tree(vp9_coeff_probs *coef_probs, int block_types) {
John Koleszar's avatar
John Koleszar committed
949
  /* print coef probability tree */
950
  int i, j, k, l, m;
John Koleszar's avatar
John Koleszar committed
951
952
  FILE *f = fopen("enc_tree_probs.txt", "a");
  fprintf(f, "{\n");
953
  for (i = 0; i < block_types; i++) {
John Koleszar's avatar
John Koleszar committed
954
    fprintf(f, "  {\n");
955
956
957
958
959
960
961
962
963
964
    for (j = 0; j < REF_TYPES; ++j) {
      fprintf(f, "  {\n");
      for (k = 0; k < COEF_BANDS; k++) {
        fprintf(f, "    {\n");
        for (l = 0; l < PREV_COEF_CONTEXTS; l++) {
          fprintf(f, "      {");
          for (m = 0; m < ENTROPY_NODES; m++) {
            fprintf(f, "%3u, ",
                    (unsigned int)(coef_probs[i][j][k][l][m]));
          }
965
        }
John Koleszar's avatar
John Koleszar committed
966
967
968
        fprintf(f, " }\n");
      }
      fprintf(f, "    }\n");
969
    }
John Koleszar's avatar
John Koleszar committed
970
971
972
973
    fprintf(f, "  }\n");
  }
  fprintf(f, "}\n");
  fclose(f);
974
975
}

976
977
978
979
980
981
static void build_tree_distribution(VP9_COMP *cpi, TX_SIZE txfm_size) {
  vp9_coeff_probs_model *coef_probs = cpi->frame_coef_probs[txfm_size];
  vp9_coeff_count *coef_counts = cpi->coef_counts[txfm_size];
  unsigned int (*eob_branch_ct)[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] =
      cpi->common.fc.eob_branch_counts[txfm_size];
  vp9_coeff_stats *coef_branch_ct = cpi->frame_branch_ct[txfm_size];
982
  vp9_prob full_probs[ENTROPY_NODES];
983
  int i, j, k, l;
984

985
  for (i = 0; i < BLOCK_TYPES; ++i) {
986
987
988
989
990
    for (j = 0; j < REF_TYPES; ++j) {
      for (k = 0; k < COEF_BANDS; ++k) {
        for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
          if (l >= 3 && k == 0)
            continue;
991
992
          vp9_tree_probs_from_distribution(vp9_coef_tree,
                                           full_probs,
993
                                           coef_branch_ct[i][j][k][l],
994
995
996
                                           coef_counts[i][j][k][l], 0);
          vpx_memcpy(coef_probs[i][j][k][l], full_probs,
                     sizeof(vp9_prob) * UNCONSTRAINED_NODES);
997
998
999
1000
1001
1002
1003
#if CONFIG_BALANCED_COEFTREE
          coef_branch_ct[i][j][k][l][1][1] = eob_branch_ct[i][j][k][l] -
                                             coef_branch_ct[i][j][k][l][1][0];
          coef_probs[i][j][k][l][1] =
              get_binary_prob(coef_branch_ct[i][j][k][l][1][0],
                              coef_branch_ct[i][j][k][l][1][1]);
#else
1004
1005
1006
1007
1008
          coef_branch_ct[i][j][k][l][0][1] = eob_branch_ct[i][j][k][l] -
                                             coef_branch_ct[i][j][k][l][0][0];
          coef_probs[i][j][k][l][0] =
              get_binary_prob(coef_branch_ct[i][j][k][l][0][0],
                              coef_branch_ct[i][j][k][l][0][1]);
1009
#endif
1010
#ifdef ENTROPY_STATS
1011
          if (!cpi->dummy_packing) {
1012
            int t;
1013
            for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
1014
1015
1016
              context_counters[txfm_size][i][j][k][l][t] +=
                  coef_counts[i][j][k][l][t];
            context_counters[txfm_size][i][j][k][l][MAX_ENTROPY_TOKENS] +=
1017
1018
                eob_branch_ct[i][j][k][l];
          }
John Koleszar's avatar
John Koleszar committed
1019
#endif
1020
        }
Daniel Kang's avatar
Daniel Kang committed
1021
1022
1023
      }
    }
  }
1024
1025
1026
}

static void build_coeff_contexts(VP9_COMP *cpi) {
1027
1028
1029
  TX_SIZE t;
  for (t = TX_4X4; t <= TX_32X32; t++)
    build_tree_distribution(cpi, t);
John Koleszar's avatar
John Koleszar committed
1030
1031
}

1032
1033
1034
1035
1036
1037
static void update_coef_probs_common(vp9_writer* const bc, VP9_COMP *cpi,
                                     TX_SIZE tx_size) {
  vp9_coeff_probs_model *new_frame_coef_probs = cpi->frame_coef_probs[tx_size];
  vp9_coeff_probs_model *old_frame_coef_probs =
      cpi->common.fc.coef_probs[tx_size];
  vp9_coeff_stats *frame_branch_ct = cpi->frame_branch_ct[tx_size];
1038
  int i, j, k, l, t;