vp9_bitstream.c 51.4 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
#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"
35
#include "vp9/encoder/vp9_subexp.h"
36
37
#include "vp9/encoder/vp9_write_bit_buffer.h"

Paul Wilkins's avatar
Paul Wilkins committed
38

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

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

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

52

53
#ifdef MODE_STATS
54
55
56
int64_t tx_count_32x32p_stats[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB];
int64_t tx_count_16x16p_stats[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB - 1];
int64_t tx_count_8x8p_stats[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB - 2];
57
58
int64_t switchable_interp_stats[VP9_SWITCHABLE_FILTERS+1]
                               [VP9_SWITCHABLE_FILTERS];
59
60
61
62
63
64
65

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

66
67
68
69
void init_switchable_interp_stats() {
  vp9_zero(switchable_interp_stats);
}

70
71
static void update_tx_count_stats(VP9_COMMON *cm) {
  int i, j;
72
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
73
74
75
76
    for (j = 0; j < TX_SIZE_MAX_SB; j++) {
      tx_count_32x32p_stats[i][j] += cm->fc.tx_count_32x32p[i][j];
    }
  }
77
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
78
79
80
81
    for (j = 0; j < TX_SIZE_MAX_SB - 1; j++) {
      tx_count_16x16p_stats[i][j] += cm->fc.tx_count_16x16p[i][j];
    }
  }
82
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
83
84
85
86
87
88
    for (j = 0; j < TX_SIZE_MAX_SB - 2; j++) {
      tx_count_8x8p_stats[i][j] += cm->fc.tx_count_8x8p[i][j];
    }
  }
}

89
90
91
92
93
94
95
96
static void update_switchable_interp_stats(VP9_COMMON *cm) {
  int i, j;
  for (i = 0; i < VP9_SWITCHABLE_FILTERS+1; ++i)
    for (j = 0; j < VP9_SWITCHABLE_FILTERS; ++j) {
      switchable_interp_stats[i][j] += cm->fc.switchable_interp_count[i][j];
    }
}

97
98
99
100
101
102
103
104
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);

105
106
107
108
  printf(
      "vp9_default_tx_count_32x32p[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB] = {\n");
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
    printf("  { ");
109
110
111
112
113
114
    for (j = 0; j < TX_SIZE_MAX_SB; j++) {
      printf("%"PRId64", ", tx_count_32x32p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
115
116
117
118
  printf(
      "vp9_default_tx_count_16x16p[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB-1] = {\n");
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
    printf("  { ");
119
120
121
122
123
124
    for (j = 0; j < TX_SIZE_MAX_SB - 1; j++) {
      printf("%"PRId64", ", tx_count_16x16p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
125
126
127
128
  printf(
      "vp9_default_tx_count_8x8p[TX_SIZE_CONTEXTS][TX_SIZE_MAX_SB-2] = {\n");
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
    printf("  { ");
129
130
131
132
133
134
135
    for (j = 0; j < TX_SIZE_MAX_SB - 2; j++) {
      printf("%"PRId64", ", tx_count_8x8p_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

void write_switchable_interp_stats() {
  int i, j;
  FILE *fp = fopen("switchable_interp.bin", "wb");
  fwrite(switchable_interp_stats, sizeof(switchable_interp_stats), 1, fp);
  fclose(fp);

  printf(
      "vp9_default_switchable_filter_count[VP9_SWITCHABLE_FILTERS+1]"
      "[VP9_SWITCHABLE_FILTERS] = {\n");
  for (i = 0; i < VP9_SWITCHABLE_FILTERS+1; i++) {
    printf("  { ");
    for (j = 0; j < VP9_SWITCHABLE_FILTERS; j++) {
      printf("%"PRId64", ", switchable_interp_stats[i][j]);
    }
    printf("},\n");
  }
  printf("};\n");
}
155
156
#endif

157
158
159
160
161
static INLINE void write_be32(uint8_t *p, int value) {
  p[0] = value >> 24;
  p[1] = value >> 16;
  p[2] = value >> 8;
  p[3] = value;
162
163
}

164
165
166
167
168
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));
}

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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) {
185
    vp9_cond_prob_diff_update(w, &Pcur[i], VP9_MODE_UPDATE_PROB, bct[i]);
186
187
188
189
190
191
  }
}

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

196
197
198
199
200
  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]);
201
202
}

Deb Mukherjee's avatar
Deb Mukherjee committed
203
void vp9_update_skip_probs(VP9_COMP *cpi, vp9_writer *bc) {
204
205
206
  VP9_COMMON *const pc = &cpi->common;
  int k;

Deb Mukherjee's avatar
Deb Mukherjee committed
207
208
  for (k = 0; k < MBSKIP_CONTEXTS; ++k) {
    vp9_cond_prob_diff_update(bc, &pc->fc.mbskip_probs[k],
209
                              VP9_MODE_UPDATE_PROB, pc->fc.mbskip_count[k]);
Deb Mukherjee's avatar
Deb Mukherjee committed
210
  }
211
212
213
214
215
216
}

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);
}

217
static void update_switchable_interp_probs(VP9_COMP *const cpi,
218
                                           vp9_writer* const bc) {
219
  VP9_COMMON *const pc = &cpi->common;
220
221
222
223
224
225
226
227
228
229
230
231
232
  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_diff_update(bc, &pc->fc.switchable_interp_prob[j][i],
233
                                VP9_MODE_UPDATE_PROB, branch_ct[j][i]);
234
235
    }
  }
236
237
238
239
#ifdef MODE_STATS
  if (!cpi->dummy_packing)
    update_switchable_interp_stats(pc);
#endif
240
241
242
243
244
245
}

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

  for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
246
    for (j = 0; j < VP9_INTER_MODES - 1; j++) {
247
      vp9_cond_prob_diff_update(bc, &pc->fc.inter_mode_probs[i][j],
248
                                VP9_MODE_UPDATE_PROB,
249
250
251
252
253
                                pc->fc.inter_mode_counts[i][j]);
    }
  }
}

254
static void pack_mb_tokens(vp9_writer* const bc,
255
256
257
                           TOKENEXTRA **tp,
                           const TOKENEXTRA *const stop) {
  TOKENEXTRA *p = *tp;
John Koleszar's avatar
John Koleszar committed
258

John Koleszar's avatar
John Koleszar committed
259
  while (p < stop) {
260
    const int t = p->token;
261
    const struct vp9_token *const a = vp9_coef_encodings + t;
262
    const vp9_extra_bit *const b = vp9_extra_bits + t;
John Koleszar's avatar
John Koleszar committed
263
    int i = 0;
264
    const vp9_prob *pp;
John Koleszar's avatar
John Koleszar committed
265
    int v = a->value;
266
    int n = a->len;
267
    vp9_prob probs[ENTROPY_NODES];
John Koleszar's avatar
John Koleszar committed
268

269
    if (t == EOSB_TOKEN) {
270
271
272
      ++p;
      break;
    }
273
    if (t >= TWO_TOKEN) {
274
      vp9_model_to_full_probs(p->context_tree, probs);
275
276
277
278
      pp = probs;
    } else {
      pp = p->context_tree;
    }
279
    assert(pp != 0);
280

John Koleszar's avatar
John Koleszar committed
281
    /* skip one or two nodes */
282
#if !CONFIG_BALANCED_COEFTREE
John Koleszar's avatar
John Koleszar committed
283
284
285
286
    if (p->skip_eob_node) {
      n -= p->skip_eob_node;
      i = 2 * p->skip_eob_node;
    }
287
#endif
John Koleszar's avatar
John Koleszar committed
288

John Koleszar's avatar
John Koleszar committed
289
290
    do {
      const int bb = (v >> --n) & 1;
291
292
293
294
295
296
297
#if CONFIG_BALANCED_COEFTREE
      if (i == 2 && p->skip_eob_node) {
        i += 2;
        assert(bb == 1);
        continue;
      }
#endif
298
      vp9_write(bc, bb, pp[i >> 1]);
299
      i = vp9_coef_tree[i + bb];
300
    } while (n);
John Koleszar's avatar
John Koleszar committed
301

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

305
      if (l) {
306
        const unsigned char *pb = b->prob;
John Koleszar's avatar
John Koleszar committed
307
        int v = e >> 1;
308
        int n = l;              /* number of bits in v, assumed nonzero */
John Koleszar's avatar
John Koleszar committed
309
        int i = 0;
John Koleszar's avatar
John Koleszar committed
310

John Koleszar's avatar
John Koleszar committed
311
312
        do {
          const int bb = (v >> --n) & 1;
313
          vp9_write(bc, bb, pb[i >> 1]);
John Koleszar's avatar
John Koleszar committed
314
315
316
          i = b->tree[i + bb];
        } while (n);
      }
John Koleszar's avatar
John Koleszar committed
317

318
      vp9_write_bit(bc, e & 1);
John Koleszar's avatar
John Koleszar committed
319
    }
John Koleszar's avatar
John Koleszar committed
320
321
322
    ++p;
  }

323
  *tp = p;
John Koleszar's avatar
John Koleszar committed
324
325
}

326
static void write_sb_mv_ref(vp9_writer *w, MB_PREDICTION_MODE m,
327
                            const vp9_prob *p) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
328
  assert(NEARESTMV <= m && m <= NEWMV);
329
  write_token(w, vp9_sb_mv_ref_tree, p,
330
              vp9_sb_mv_ref_encoding_array - NEARESTMV + m);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
331
332
}

333
334
335

static void write_segment_id(vp9_writer *w, const MACROBLOCKD *xd,
                             int segment_id) {
336
  if (xd->segmentation_enabled && xd->update_mb_segmentation_map)
337
    treed_write(w, vp9_segment_tree, xd->mb_segment_tree_probs, segment_id, 3);
John Koleszar's avatar
John Koleszar committed
338
339
}

Paul Wilkins's avatar
Paul Wilkins committed
340
// This function encodes the reference frame
Ronald S. Bultje's avatar
Ronald S. Bultje committed
341
342
343
344
345
346
347
348
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
349
350
  // If segment level coding of this signal is disabled...
  // or the segment allows multiple reference frame options
351
  if (!seg_ref_active) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
352
353
354
355
356
357
358
359
360
    // 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));
    }
361

Ronald S. Bultje's avatar
Ronald S. Bultje committed
362
363
364
365
366
367
368
369
370
    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
371
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
372
373
  } else {
    assert(mi->ref_frame[1] <= INTRA_FRAME);
374
375
    assert(vp9_get_segdata(xd, segment_id, SEG_LVL_REF_FRAME) ==
           mi->ref_frame[0]);
John Koleszar's avatar
John Koleszar committed
376
  }
Paul Wilkins's avatar
Paul Wilkins committed
377

John Koleszar's avatar
John Koleszar committed
378
379
  // 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
380
}
John Koleszar's avatar
John Koleszar committed
381

Ronald S. Bultje's avatar
Ronald S. Bultje committed
382
static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m,
383
                                vp9_writer *bc, int mi_row, int mi_col) {
384
  VP9_COMMON *const pc = &cpi->common;
385
  const nmv_context *nmvc = &pc->fc.nmvc;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
386
387
388
  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
389
  const MV_REFERENCE_FRAME rf = mi->ref_frame[0];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
390
391
392
  const MB_PREDICTION_MODE mode = mi->mode;
  const int segment_id = mi->segment_id;
  int skip_coeff;
Adrian Grange's avatar
Adrian Grange committed
393

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
397
398
#ifdef ENTROPY_STATS
  active_section = 9;
399
#endif
400

Ronald S. Bultje's avatar
Ronald S. Bultje committed
401
402
403
404
405
  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
406

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
410
411
      // If the mb segment id wasn't predicted code explicitly
      if (!prediction_flag)
412
        write_segment_id(bc, xd, mi->segment_id);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
413
414
    } else {
      // Normal unpredicted coding
415
        write_segment_id(bc, xd, mi->segment_id);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
416
417
    }
  }
418

419
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
420
421
    skip_coeff = 1;
  } else {
422
    skip_coeff = m->mbmi.mb_skip_coeff;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
423
424
425
    vp9_write(bc, skip_coeff,
              vp9_get_pred_prob(pc, xd, PRED_MBSKIP));
  }
John Koleszar's avatar
John Koleszar committed
426

427
428
429
  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
430

431
432
433
434
  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;
435
436
    const vp9_prob *tx_probs = vp9_get_pred_probs(pc, xd, PRED_TX_SIZE);
    vp9_write(bc, sz != TX_4X4, tx_probs[0]);
437
    if (mi->sb_type >= BLOCK_SIZE_MB16X16 && sz != TX_4X4) {
438
      vp9_write(bc, sz != TX_8X8, tx_probs[1]);
439
      if (mi->sb_type >= BLOCK_SIZE_SB32X32 && sz != TX_8X8)
440
        vp9_write(bc, sz != TX_16X16, tx_probs[2]);
441
442
443
    }
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
444
  if (rf == INTRA_FRAME) {
445
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
446
    active_section = 6;
447
#endif
Paul Wilkins's avatar
Paul Wilkins committed
448

449
    if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8) {
450
451
452
453
      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)]);
454
    } else {
455
456
457
458
      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)
459
460
461
462
        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
463
    }
464
465
    write_intra_mode(bc, mi->uv_mode,
                     pc->fc.uv_mode_prob[mode]);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
466
  } else {
467
    vp9_prob *mv_ref_p;
468

Ronald S. Bultje's avatar
Ronald S. Bultje committed
469
470
    encode_ref_frame(cpi, bc);

471
    mv_ref_p = cpi->common.fc.inter_mode_probs[mi->mb_mode_context[rf]];
Yaowu Xu's avatar
Yaowu Xu committed
472

John Koleszar's avatar
John Koleszar committed
473
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
474
    active_section = 3;
John Koleszar's avatar
John Koleszar committed
475
476
#endif

477
    // If segment skip is not enabled code the mode.
Paul Wilkins's avatar
Paul Wilkins committed
478
    if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
479
      if (mi->sb_type >= BLOCK_SIZE_SB8X8) {
480
        write_sb_mv_ref(bc, mode, mv_ref_p);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
481
482
        vp9_accum_mv_refs(&cpi->common, mode, mi->mb_mode_context[rf]);
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
483
    }
484

Ronald S. Bultje's avatar
Ronald S. Bultje committed
485
486
487
488
489
490
491
492
    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
493
    }
494

Ronald S. Bultje's avatar
Ronald S. Bultje committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
    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) {
510
#ifdef ENTROPY_STATS
Ronald S. Bultje's avatar
Ronald S. Bultje committed
511
            active_section = 11;
512
#endif
Paul Wilkins's avatar
Paul Wilkins committed
513
            vp9_encode_mv(cpi, bc, &blockmv.as_mv, &mi->best_mv.as_mv,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
514
                          nmvc, xd->allow_high_precision_mv);
515

Ronald S. Bultje's avatar
Ronald S. Bultje committed
516
            if (mi->ref_frame[1] > INTRA_FRAME)
Paul Wilkins's avatar
Paul Wilkins committed
517
              vp9_encode_mv(cpi, bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
518
519
                            &cpi->mb.partition_info->bmi[j].second_mv.as_mv,
                            &mi->best_second_mv.as_mv,
520
                            nmvc, xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
521
          }
522
        }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
523
524
525
526
527
      }
    } else if (mode == NEWMV) {
#ifdef ENTROPY_STATS
      active_section = 5;
#endif
Paul Wilkins's avatar
Paul Wilkins committed
528
      vp9_encode_mv(cpi, bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
529
530
531
                    &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
532
      if (mi->ref_frame[1] > INTRA_FRAME)
Paul Wilkins's avatar
Paul Wilkins committed
533
        vp9_encode_mv(cpi, bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
534
535
                      &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
536
537
    }
  }
John Koleszar's avatar
John Koleszar committed
538
}
539

Ronald S. Bultje's avatar
Ronald S. Bultje committed
540
static void write_mb_modes_kf(const VP9_COMP *cpi,
541
                              MODE_INFO *m,
542
                              vp9_writer *bc, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
543
544
545
  const VP9_COMMON *const c = &cpi->common;
  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  const int ym = m->mbmi.mode;
546
  const int mis = c->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
547
548
  const int segment_id = m->mbmi.segment_id;
  int skip_coeff;
549

550
  if (xd->update_mb_segmentation_map)
551
    write_segment_id(bc, xd, m->mbmi.segment_id);
552

553
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_SKIP)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
554
555
    skip_coeff = 1;
  } else {
556
    skip_coeff = m->mbmi.mb_skip_coeff;
557
    vp9_write(bc, skip_coeff, vp9_get_pred_prob(c, xd, PRED_MBSKIP));
558
559
  }

560
561
  if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8 && c->txfm_mode == TX_MODE_SELECT) {
    TX_SIZE sz = m->mbmi.txfm_size;
562
563
    const vp9_prob *tx_probs = vp9_get_pred_probs(c, xd, PRED_TX_SIZE);
    vp9_write(bc, sz != TX_4X4, tx_probs[0]);
564
    if (m->mbmi.sb_type >= BLOCK_SIZE_MB16X16 && sz != TX_4X4) {
565
      vp9_write(bc, sz != TX_8X8, tx_probs[1]);
566
      if (m->mbmi.sb_type >= BLOCK_SIZE_SB32X32 && sz != TX_8X8)
567
        vp9_write(bc, sz != TX_16X16, tx_probs[2]);
568
569
570
    }
  }

571
  if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8) {
Yaowu Xu's avatar
Yaowu Xu committed
572
573
    const MB_PREDICTION_MODE A = above_block_mode(m, 0, mis);
    const MB_PREDICTION_MODE L = xd->left_available ?
574
                                 left_block_mode(m, 0) : DC_PRED;
575
576
    write_intra_mode(bc, ym, c->kf_y_mode_prob[A][L]);
  } else {
577
578
579
    int idx, idy;
    int bw = 1 << b_width_log2(m->mbmi.sb_type);
    int bh = 1 << b_height_log2(m->mbmi.sb_type);
580
581
582
    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
583
584
        const MB_PREDICTION_MODE A = above_block_mode(m, i, mis);
        const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
585
                                     left_block_mode(m, i) : DC_PRED;
586
587
588
589
        const int bm = m->bmi[i].as_mode.first;
#ifdef ENTROPY_STATS
        ++intra_mode_stats[A][L][bm];
#endif
590
        write_intra_mode(bc, bm, c->kf_y_mode_prob[A][L]);
591
592
      }
    }
593
594
  }

595
  write_intra_mode(bc, m->mbmi.uv_mode, c->kf_uv_mode_prob[ym]);
596
597
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
598
599
static void write_modes_b(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                          TOKENEXTRA **tok, TOKENEXTRA *tok_end,
600
                          int mi_row, int mi_col) {
601
  VP9_COMMON *const cm = &cpi->common;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
602
603
  MACROBLOCKD *const xd = &cpi->mb.e_mbd;

604
605
606
  if (m->mbmi.sb_type < BLOCK_SIZE_SB8X8)
    if (xd->ab_index > 0)
      return;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
607
  xd->mode_info_context = m;
608
609
610
  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));
Adrian Grange's avatar
Adrian Grange committed
611
  if ((cm->frame_type == KEY_FRAME) || cm->intra_only) {
612
    write_mb_modes_kf(cpi, m, bc, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
613
614
615
616
#ifdef ENTROPY_STATS
    active_section = 8;
#endif
  } else {
617
    pack_inter_mode_mvs(cpi, m, bc, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
618
619
620
621
622
623
624
625
626
#ifdef ENTROPY_STATS
    active_section = 1;
#endif
  }

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

627
628
static void write_modes_sb(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
                           TOKENEXTRA **tok, TOKENEXTRA *tok_end,
629
                           int mi_row, int mi_col,
630
631
                           BLOCK_SIZE_TYPE bsize) {
  VP9_COMMON *const cm = &cpi->common;
632
  MACROBLOCKD *xd = &cpi->mb.e_mbd;
633
634
  const int mis = cm->mode_info_stride;
  int bwl, bhl;
635
636
  int bsl = b_width_log2(bsize);
  int bs = (1 << bsl) / 4;  // mode_info step for subsize
637
638
639
640
  int n;
  PARTITION_TYPE partition;
  BLOCK_SIZE_TYPE subsize;

641
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
642
643
    return;

644
645
  bwl = b_width_log2(m->mbmi.sb_type);
  bhl = b_height_log2(m->mbmi.sb_type);
646
647
648
649
650
651
652
653
654
655
656
657
658

  // 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);

659
  if (bsize < BLOCK_SIZE_SB8X8)
660
    if (xd->ab_index > 0)
661
662
663
      return;

  if (bsize >= BLOCK_SIZE_SB8X8) {
664
    int pl;
665
    int idx = check_bsize_coverage(cm, xd, mi_row, mi_col, bsize);
666
667
    xd->left_seg_context = cm->left_seg_context + (mi_row & MI_MASK);
    xd->above_seg_context = cm->above_seg_context + mi_col;
668
    pl = partition_plane_context(xd, bsize);
669
    // encode the partition information
670
671
672
673
674
675
676
    if (idx == 0)
      write_token(bc, vp9_partition_tree,
                  cm->fc.partition_prob[cm->frame_type][pl],
                  vp9_partition_encodings + partition);
    else if (idx > 0)
      vp9_write(bc, partition == PARTITION_SPLIT,
                cm->fc.partition_prob[cm->frame_type][pl][idx]);
677
  }
678

679
  subsize = get_subsize(bsize, partition);
680
  *(get_sb_index(xd, subsize)) = 0;
681

682
683
  switch (partition) {
    case PARTITION_NONE:
684
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
685
686
      break;
    case PARTITION_HORZ:
687
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
688
      *(get_sb_index(xd, subsize)) = 1;
689
690
      if ((mi_row + bs) < cm->mi_rows)
        write_modes_b(cpi, m + bs * mis, bc, tok, tok_end, mi_row + bs, mi_col);
691
692
      break;
    case PARTITION_VERT:
693
      write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
694
      *(get_sb_index(xd, subsize)) = 1;
695
696
      if ((mi_col + bs) < cm->mi_cols)
        write_modes_b(cpi, m + bs, bc, tok, tok_end, mi_row, mi_col + bs);
697
698
699
700
      break;
    case PARTITION_SPLIT:
      for (n = 0; n < 4; n++) {
        int j = n >> 1, i = n & 0x01;
701
        *(get_sb_index(xd, subsize)) = n;
702
        write_modes_sb(cpi, m + j * bs * mis + i * bs, bc, tok, tok_end,
703
                       mi_row + j * bs, mi_col + i * bs, subsize);
704
705
706
707
708
      }
      break;
    default:
      assert(0);
  }
709
710

  // update partition context
711
712
  if (bsize >= BLOCK_SIZE_SB8X8 &&
      (bsize == BLOCK_SIZE_SB8X8 || partition != PARTITION_SPLIT)) {
713
714
715
    set_partition_seg_context(cm, xd, mi_row, mi_col);
    update_partition_context(xd, subsize, bsize);
  }
716
717
}

718
static void write_modes(VP9_COMP *cpi, vp9_writer* const bc,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
719
                        TOKENEXTRA **tok, TOKENEXTRA *tok_end) {
720
  VP9_COMMON *const c = &cpi->common;
John Koleszar's avatar
John Koleszar committed
721
  const int mis = c->mode_info_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
722
  MODE_INFO *m, *m_ptr = c->mi;
723
  int mi_row, mi_col;
John Koleszar's avatar
John Koleszar committed
724

725
  m_ptr += c->cur_tile_mi_col_start + c->cur_tile_mi_row_start * mis;
726

727
728
  for (mi_row = c->cur_tile_mi_row_start;
       mi_row < c->cur_tile_mi_row_end;
Jingning Han's avatar
Jingning Han committed
729
       mi_row += 8, m_ptr += 8 * mis) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
730
    m = m_ptr;
731
    vpx_memset(c->left_seg_context, 0, sizeof(c->left_seg_context));
732
733
    for (mi_col = c->cur_tile_mi_col_start;
         mi_col < c->cur_tile_mi_col_end;
734
         mi_col += 64 / MI_SIZE, m += 64 / MI_SIZE)
735
      write_modes_sb(cpi, m, bc, tok, tok_end, mi_row, mi_col,
736
                     BLOCK_SIZE_SB64X64);
John Koleszar's avatar
John Koleszar committed
737
  }
John Koleszar's avatar
John Koleszar committed
738
}
739

740
/* This function is used for debugging probability trees. */
741
static void print_prob_tree(vp9_coeff_probs *coef_probs, int block_types) {
John Koleszar's avatar
John Koleszar committed
742
  /* print coef probability tree */
743
  int i, j, k, l, m;
John Koleszar's avatar
John Koleszar committed
744
745
  FILE *f = fopen("enc_tree_probs.txt", "a");
  fprintf(f, "{\n");
746
  for (i = 0; i < block_types; i++) {
John Koleszar's avatar
John Koleszar committed
747
    fprintf(f, "  {\n");
748
749
750
751
752
753
754
755
756
757
    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]));
          }
758
        }
John Koleszar's avatar
John Koleszar committed
759
760
761
        fprintf(f, " }\n");
      }
      fprintf(f, "    }\n");
762
    }
John Koleszar's avatar
John Koleszar committed
763
764
765
766
    fprintf(f, "  }\n");
  }
  fprintf(f, "}\n");
  fclose(f);
767
768
}

769
770
771
772
773
774
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];
775
  vp9_prob full_probs[ENTROPY_NODES];
776
  int i, j, k, l;
777

778
  for (i = 0; i < BLOCK_TYPES; ++i) {
779
780
781
782
783
    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;
784
785
          vp9_tree_probs_from_distribution(vp9_coef_tree,
                                           full_probs,
786
                                           coef_branch_ct[i][j][k][l],
787
788
789
                                           coef_counts[i][j][k][l], 0);
          vpx_memcpy(coef_probs[i][j][k][l], full_probs,
                     sizeof(vp9_prob) * UNCONSTRAINED_NODES);
790
791
792
793
794
795
796
#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
797
798
799
800
801
          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]);
802
#endif
803
#ifdef ENTROPY_STATS
804
          if (!cpi->dummy_packing) {
805
            int t;
806
            for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
807
808
809
              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] +=
810
811
                eob_branch_ct[i][j][k][l];
          }
John Koleszar's avatar
John Koleszar committed
812
#endif
813
        }
Daniel Kang's avatar
Daniel Kang committed
814
815
816
      }
    }
  }
817
818
819
}

static void build_coeff_contexts(VP9_COMP *cpi) {
820
821
822
  TX_SIZE t;
  for (t = TX_4X4; t <= TX_32X32; t++)
    build_tree_distribution(cpi, t);
John Koleszar's avatar
John Koleszar committed
823
824
}

825
826
827
828
829
830
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];
831
  int i, j, k, l, t;
John Koleszar's avatar
John Koleszar committed
832
833
  int update[2] = {0, 0};
  int savings;
834
835

  const int entropy_nodes_update = UNCONSTRAINED_NODES;
836

837
  const int tstart = 0;
John Koleszar's avatar
John Koleszar committed
838
839
  /* dry run to see if there is any udpate at all needed */
  savings = 0;
840
  for (i = 0; i < BLOCK_TYPES; ++i) {
841
842
    for (j = 0; j < REF_TYPES; ++j) {
      for (k = 0; k < COEF_BANDS; ++k) {
843
        // int prev_coef_savings[ENTROPY_NODES] = {0};
844
        for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
845
          for (t = tstart; t < entropy_nodes_update; ++t) {
846
847
            vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
            const vp9_prob oldp = old_frame_coef_probs[i][j][k][l][t];
848
            const vp9_prob upd = VP9_COEF_UPDATE_PROB;
849
            int s;
850
851
852
853
            int u = 0;

            if (l >= 3 && k == 0)
              continue;
854
            if (t == PIVOT_NODE)
855
              s = vp9_prob_diff_update_savings_search_model(
856
                  frame_branch_ct[i][j][k][l][0],
857
                  old_frame_coef_probs[i][j][k][l], &newp, upd, i, j);
858
            else
859
              s = vp9_prob_diff_update_savings_search(
860
                  frame_branch_ct[i][j][k][l][t], oldp, &newp, upd);
861
862
863
864
865
866
867
868
            if (s > 0 && newp != oldp)
              u = 1;
            if (u)
              savings += s - (int)(vp9_cost_zero(upd));
            else
              savings -= (int)(vp9_cost_zero(upd));
            update[u]++;
          }
Daniel Kang's avatar
Daniel Kang committed
869
870
871
872
        }
      }
    }
  }
John Koleszar's avatar
John Koleszar committed
873
874
875

  // printf("Update %d %d, savings %d\n", update[0], update[1], savings);
  /* Is coef updated at all */
John Koleszar's avatar
John Koleszar committed
876
  if (update[1] == 0 || savings < 0) {
877
    vp9_write_bit(bc, 0);
878
879
880
    return;
  }
  vp9_write_bit(bc, 1);
881
  for (i = 0; i < BLOCK_TYPES; ++i) {
882
883
884
885
886
    for (j = 0; j < REF_TYPES; ++j) {
      for (k = 0; k < COEF_BANDS; ++k) {
        // int prev_coef_savings[ENTROPY_NODES] = {0};
        for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
          // calc probs and branch cts for this frame only
887
          for (t = tstart; t < entropy_nodes_update; ++t) {
888
889
            vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
            vp9_prob *oldp = old_frame_coef_probs[i][j][k][l] + t;
890
            const vp9_prob upd = VP9_COEF_UPDATE_PROB;
891
            int s;
892
893
894
            int u = 0;
            if (l >= 3 && k == 0)
              continue;
895
            if (t == PIVOT_NODE)
896
              s = vp9_prob_diff_update_savings_search_model(
897
                  frame_branch_ct[i][j][k][l][0],
898
                  old_frame_coef_probs[i][j][k][l], &newp, upd, i, j);
899
            else
900
              s = vp9_prob_diff_update_savings_search(
901
902
903
904
905
                  frame_branch_ct[i][j][k][l][t],
                  *oldp, &newp, upd);
            if (s > 0 && newp != *oldp)
              u = 1;
            vp9_write(bc, u, upd);
906
#ifdef ENTROPY_STATS
907
            if (!cpi->dummy_packing)
908
              ++tree_update_hist[tx_size][i][j][k][l][t][u];
909
910
911
#endif
            if (u) {
              /* send/use new probability */
912
              vp9_write_prob_diff_update(bc, newp, *oldp);
913
              *oldp = newp;
John Koleszar's avatar
John Koleszar committed
914
            }
Daniel Kang's avatar
Daniel Kang committed
915
916
917
918
          }
        }
      }
    }
John Koleszar's avatar
John Koleszar committed
919
  }
920
}
John Koleszar's avatar
John Koleszar committed
921

922
static void update_coef_probs(VP9_COMP* const cpi, vp9_writer* const bc) {
Dmitry Kovalev's avatar