vp9_bitstream.c revision 1184aebb761cbeac9124c37189a80a1a58f04b6b
1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  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.
9 */
10
11#include <assert.h>
12#include <stdio.h>
13#include <limits.h>
14
15#include "vpx/vpx_encoder.h"
16#include "vpx_mem/vpx_mem.h"
17
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#include "vp9/common/vp9_seg_common.h"
23#include "vp9/common/vp9_pred_common.h"
24#include "vp9/common/vp9_entropy.h"
25#include "vp9/common/vp9_entropymv.h"
26#include "vp9/common/vp9_mvref_common.h"
27#include "vp9/common/vp9_treecoder.h"
28#include "vp9/common/vp9_systemdependent.h"
29#include "vp9/common/vp9_pragmas.h"
30
31#include "vp9/encoder/vp9_mcomp.h"
32#include "vp9/encoder/vp9_encodemv.h"
33#include "vp9/encoder/vp9_bitstream.h"
34#include "vp9/encoder/vp9_segmentation.h"
35#include "vp9/encoder/vp9_subexp.h"
36#include "vp9/encoder/vp9_write_bit_buffer.h"
37
38
39#if defined(SECTIONBITS_OUTPUT)
40unsigned __int64 Sectionbits[500];
41#endif
42
43#ifdef ENTROPY_STATS
44int intra_mode_stats[INTRA_MODES]
45                    [INTRA_MODES]
46                    [INTRA_MODES];
47vp9_coeff_stats tree_update_hist[TX_SIZES][BLOCK_TYPES];
48
49extern unsigned int active_section;
50#endif
51
52
53#ifdef MODE_STATS
54int64_t tx_count_32x32p_stats[TX_SIZE_CONTEXTS][TX_SIZES];
55int64_t tx_count_16x16p_stats[TX_SIZE_CONTEXTS][TX_SIZES - 1];
56int64_t tx_count_8x8p_stats[TX_SIZE_CONTEXTS][TX_SIZES - 2];
57int64_t switchable_interp_stats[SWITCHABLE_FILTERS+1]
58                               [SWITCHABLE_FILTERS];
59
60void init_tx_count_stats() {
61  vp9_zero(tx_count_32x32p_stats);
62  vp9_zero(tx_count_16x16p_stats);
63  vp9_zero(tx_count_8x8p_stats);
64}
65
66void init_switchable_interp_stats() {
67  vp9_zero(switchable_interp_stats);
68}
69
70static void update_tx_count_stats(VP9_COMMON *cm) {
71  int i, j;
72  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
73    for (j = 0; j < TX_SIZES; j++) {
74      tx_count_32x32p_stats[i][j] += cm->fc.tx_count_32x32p[i][j];
75    }
76  }
77  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
78    for (j = 0; j < TX_SIZES - 1; j++) {
79      tx_count_16x16p_stats[i][j] += cm->fc.tx_count_16x16p[i][j];
80    }
81  }
82  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
83    for (j = 0; j < TX_SIZES - 2; j++) {
84      tx_count_8x8p_stats[i][j] += cm->fc.tx_count_8x8p[i][j];
85    }
86  }
87}
88
89static void update_switchable_interp_stats(VP9_COMMON *cm) {
90  int i, j;
91  for (i = 0; i < SWITCHABLE_FILTERS+1; ++i)
92    for (j = 0; j < SWITCHABLE_FILTERS; ++j) {
93      switchable_interp_stats[i][j] += cm->fc.switchable_interp_count[i][j];
94    }
95}
96
97void write_tx_count_stats() {
98  int i, j;
99  FILE *fp = fopen("tx_count.bin", "wb");
100  fwrite(tx_count_32x32p_stats, sizeof(tx_count_32x32p_stats), 1, fp);
101  fwrite(tx_count_16x16p_stats, sizeof(tx_count_16x16p_stats), 1, fp);
102  fwrite(tx_count_8x8p_stats, sizeof(tx_count_8x8p_stats), 1, fp);
103  fclose(fp);
104
105  printf(
106      "vp9_default_tx_count_32x32p[TX_SIZE_CONTEXTS][TX_SIZES] = {\n");
107  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
108    printf("  { ");
109    for (j = 0; j < TX_SIZES; j++) {
110      printf("%"PRId64", ", tx_count_32x32p_stats[i][j]);
111    }
112    printf("},\n");
113  }
114  printf("};\n");
115  printf(
116      "vp9_default_tx_count_16x16p[TX_SIZE_CONTEXTS][TX_SIZES-1] = {\n");
117  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
118    printf("  { ");
119    for (j = 0; j < TX_SIZES - 1; j++) {
120      printf("%"PRId64", ", tx_count_16x16p_stats[i][j]);
121    }
122    printf("},\n");
123  }
124  printf("};\n");
125  printf(
126      "vp9_default_tx_count_8x8p[TX_SIZE_CONTEXTS][TX_SIZES-2] = {\n");
127  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
128    printf("  { ");
129    for (j = 0; j < TX_SIZES - 2; j++) {
130      printf("%"PRId64", ", tx_count_8x8p_stats[i][j]);
131    }
132    printf("},\n");
133  }
134  printf("};\n");
135}
136
137void write_switchable_interp_stats() {
138  int i, j;
139  FILE *fp = fopen("switchable_interp.bin", "wb");
140  fwrite(switchable_interp_stats, sizeof(switchable_interp_stats), 1, fp);
141  fclose(fp);
142
143  printf(
144      "vp9_default_switchable_filter_count[SWITCHABLE_FILTERS+1]"
145      "[SWITCHABLE_FILTERS] = {\n");
146  for (i = 0; i < SWITCHABLE_FILTERS+1; i++) {
147    printf("  { ");
148    for (j = 0; j < SWITCHABLE_FILTERS; j++) {
149      printf("%"PRId64", ", switchable_interp_stats[i][j]);
150    }
151    printf("},\n");
152  }
153  printf("};\n");
154}
155#endif
156
157static INLINE void write_be32(uint8_t *p, int value) {
158  p[0] = value >> 24;
159  p[1] = value >> 16;
160  p[2] = value >> 8;
161  p[3] = value;
162}
163
164void vp9_encode_unsigned_max(struct vp9_write_bit_buffer *wb,
165                             int data, int max) {
166  vp9_wb_write_literal(wb, data, get_unsigned_bits(max));
167}
168
169static void update_mode(
170  vp9_writer *w,
171  int n,
172  vp9_tree tree,
173  vp9_prob Pnew[/* n-1 */],
174  vp9_prob Pcur[/* n-1 */],
175  unsigned int bct[/* n-1 */] [2],
176  const unsigned int num_events[/* n */]
177) {
178  int i = 0;
179
180  vp9_tree_probs_from_distribution(tree, Pnew, bct, num_events, 0);
181  n--;
182
183  for (i = 0; i < n; ++i) {
184    vp9_cond_prob_diff_update(w, &Pcur[i], MODE_UPDATE_PROB, bct[i]);
185  }
186}
187
188static void update_mbintra_mode_probs(VP9_COMP* const cpi,
189                                      vp9_writer* const bc) {
190  VP9_COMMON *const cm = &cpi->common;
191  int j;
192  vp9_prob pnew[INTRA_MODES - 1];
193  unsigned int bct[INTRA_MODES - 1][2];
194
195  for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
196    update_mode(bc, INTRA_MODES, vp9_intra_mode_tree, pnew,
197                cm->fc.y_mode_prob[j], bct,
198                (unsigned int *)cpi->y_mode_count[j]);
199}
200
201static void write_selected_tx_size(const VP9_COMP *cpi, MODE_INFO *m,
202                                   TX_SIZE tx_size, BLOCK_SIZE bsize,
203                                   vp9_writer *w) {
204  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
205  const vp9_prob *tx_probs = get_tx_probs2(xd, &cpi->common.fc.tx_probs, m);
206  vp9_write(w, tx_size != TX_4X4, tx_probs[0]);
207  if (bsize >= BLOCK_16X16 && tx_size != TX_4X4) {
208    vp9_write(w, tx_size != TX_8X8, tx_probs[1]);
209    if (bsize >= BLOCK_32X32 && tx_size != TX_8X8)
210      vp9_write(w, tx_size != TX_16X16, tx_probs[2]);
211  }
212}
213
214static int write_skip_coeff(const VP9_COMP *cpi, int segment_id, MODE_INFO *m,
215                            vp9_writer *w) {
216  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
217  if (vp9_segfeature_active(&cpi->common.seg, segment_id, SEG_LVL_SKIP)) {
218    return 1;
219  } else {
220    const int skip_coeff = m->mbmi.skip_coeff;
221    vp9_write(w, skip_coeff, vp9_get_pred_prob_mbskip(&cpi->common, xd));
222    return skip_coeff;
223  }
224}
225
226void vp9_update_skip_probs(VP9_COMP *cpi, vp9_writer *w) {
227  VP9_COMMON *cm = &cpi->common;
228  int k;
229
230  for (k = 0; k < MBSKIP_CONTEXTS; ++k)
231    vp9_cond_prob_diff_update(w, &cm->fc.mbskip_probs[k],
232                              MODE_UPDATE_PROB, cm->counts.mbskip[k]);
233}
234
235static void write_intra_mode(vp9_writer *bc, int m, const vp9_prob *p) {
236  write_token(bc, vp9_intra_mode_tree, p, vp9_intra_mode_encodings + m);
237}
238
239static void update_switchable_interp_probs(VP9_COMP *const cpi,
240                                           vp9_writer* const bc) {
241  VP9_COMMON *const cm = &cpi->common;
242  unsigned int branch_ct[SWITCHABLE_FILTERS + 1]
243                        [SWITCHABLE_FILTERS - 1][2];
244  vp9_prob new_prob[SWITCHABLE_FILTERS + 1][SWITCHABLE_FILTERS - 1];
245  int i, j;
246  for (j = 0; j <= SWITCHABLE_FILTERS; ++j) {
247    vp9_tree_probs_from_distribution(
248        vp9_switchable_interp_tree,
249        new_prob[j], branch_ct[j],
250        cm->counts.switchable_interp[j], 0);
251  }
252  for (j = 0; j <= SWITCHABLE_FILTERS; ++j) {
253    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i) {
254      vp9_cond_prob_diff_update(bc, &cm->fc.switchable_interp_prob[j][i],
255                                MODE_UPDATE_PROB, branch_ct[j][i]);
256    }
257  }
258#ifdef MODE_STATS
259  if (!cpi->dummy_packing)
260    update_switchable_interp_stats(cm);
261#endif
262}
263
264static void update_inter_mode_probs(VP9_COMMON *cm, vp9_writer* const bc) {
265  int i, j;
266
267  for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
268    unsigned int branch_ct[INTER_MODES - 1][2];
269    vp9_prob new_prob[INTER_MODES - 1];
270
271    vp9_tree_probs_from_distribution(vp9_inter_mode_tree,
272                                     new_prob, branch_ct,
273                                     cm->counts.inter_mode[i], NEARESTMV);
274
275    for (j = 0; j < INTER_MODES - 1; ++j)
276      vp9_cond_prob_diff_update(bc, &cm->fc.inter_mode_probs[i][j],
277                                MODE_UPDATE_PROB, branch_ct[j]);
278  }
279}
280
281static void pack_mb_tokens(vp9_writer* const bc,
282                           TOKENEXTRA **tp,
283                           const TOKENEXTRA *const stop) {
284  TOKENEXTRA *p = *tp;
285
286  while (p < stop) {
287    const int t = p->token;
288    const struct vp9_token *const a = vp9_coef_encodings + t;
289    const vp9_extra_bit *const b = vp9_extra_bits + t;
290    int i = 0;
291    const vp9_prob *pp;
292    int v = a->value;
293    int n = a->len;
294    vp9_prob probs[ENTROPY_NODES];
295
296    if (t == EOSB_TOKEN) {
297      ++p;
298      break;
299    }
300    if (t >= TWO_TOKEN) {
301      vp9_model_to_full_probs(p->context_tree, probs);
302      pp = probs;
303    } else {
304      pp = p->context_tree;
305    }
306    assert(pp != 0);
307
308    /* skip one or two nodes */
309    if (p->skip_eob_node) {
310      n -= p->skip_eob_node;
311      i = 2 * p->skip_eob_node;
312    }
313
314    do {
315      const int bb = (v >> --n) & 1;
316      vp9_write(bc, bb, pp[i >> 1]);
317      i = vp9_coef_tree[i + bb];
318    } while (n);
319
320    if (b->base_val) {
321      const int e = p->extra, l = b->len;
322
323      if (l) {
324        const unsigned char *pb = b->prob;
325        int v = e >> 1;
326        int n = l;              /* number of bits in v, assumed nonzero */
327        int i = 0;
328
329        do {
330          const int bb = (v >> --n) & 1;
331          vp9_write(bc, bb, pb[i >> 1]);
332          i = b->tree[i + bb];
333        } while (n);
334      }
335
336      vp9_write_bit(bc, e & 1);
337    }
338    ++p;
339  }
340
341  *tp = p;
342}
343
344static void write_sb_mv_ref(vp9_writer *w, MB_PREDICTION_MODE mode,
345                            const vp9_prob *p) {
346  assert(is_inter_mode(mode));
347  write_token(w, vp9_inter_mode_tree, p,
348              &vp9_inter_mode_encodings[mode - NEARESTMV]);
349}
350
351
352static void write_segment_id(vp9_writer *w, const struct segmentation *seg,
353                             int segment_id) {
354  if (seg->enabled && seg->update_map)
355    treed_write(w, vp9_segment_tree, seg->tree_probs, segment_id, 3);
356}
357
358// This function encodes the reference frame
359static void encode_ref_frame(VP9_COMP *cpi, vp9_writer *bc) {
360  VP9_COMMON *const cm = &cpi->common;
361  MACROBLOCK *const x = &cpi->mb;
362  MACROBLOCKD *const xd = &x->e_mbd;
363  MB_MODE_INFO *mi = &xd->this_mi->mbmi;
364  const int segment_id = mi->segment_id;
365  int seg_ref_active = vp9_segfeature_active(&cm->seg, segment_id,
366                                             SEG_LVL_REF_FRAME);
367  // If segment level coding of this signal is disabled...
368  // or the segment allows multiple reference frame options
369  if (!seg_ref_active) {
370    // does the feature use compound prediction or not
371    // (if not specified at the frame/segment level)
372    if (cm->comp_pred_mode == HYBRID_PREDICTION) {
373      vp9_write(bc, mi->ref_frame[1] > INTRA_FRAME,
374                vp9_get_pred_prob_comp_inter_inter(cm, xd));
375    } else {
376      assert((mi->ref_frame[1] <= INTRA_FRAME) ==
377                 (cm->comp_pred_mode == SINGLE_PREDICTION_ONLY));
378    }
379
380    if (mi->ref_frame[1] > INTRA_FRAME) {
381      vp9_write(bc, mi->ref_frame[0] == GOLDEN_FRAME,
382                vp9_get_pred_prob_comp_ref_p(cm, xd));
383    } else {
384      vp9_write(bc, mi->ref_frame[0] != LAST_FRAME,
385                vp9_get_pred_prob_single_ref_p1(cm, xd));
386      if (mi->ref_frame[0] != LAST_FRAME)
387        vp9_write(bc, mi->ref_frame[0] != GOLDEN_FRAME,
388                  vp9_get_pred_prob_single_ref_p2(cm, xd));
389    }
390  } else {
391    assert(mi->ref_frame[1] <= INTRA_FRAME);
392    assert(vp9_get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) ==
393           mi->ref_frame[0]);
394  }
395
396  // if using the prediction mdoel we have nothing further to do because
397  // the reference frame is fully coded by the segment
398}
399
400static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc) {
401  VP9_COMMON *const cm = &cpi->common;
402  const nmv_context *nmvc = &cm->fc.nmvc;
403  MACROBLOCK *const x = &cpi->mb;
404  MACROBLOCKD *const xd = &x->e_mbd;
405  struct segmentation *seg = &cm->seg;
406  MB_MODE_INFO *const mi = &m->mbmi;
407  const MV_REFERENCE_FRAME rf = mi->ref_frame[0];
408  const MB_PREDICTION_MODE mode = mi->mode;
409  const int segment_id = mi->segment_id;
410  int skip_coeff;
411  const BLOCK_SIZE bsize = mi->sb_type;
412  const int allow_hp = xd->allow_high_precision_mv;
413
414  x->partition_info = x->pi + (m - cm->mi);
415
416#ifdef ENTROPY_STATS
417  active_section = 9;
418#endif
419
420  if (seg->update_map) {
421    if (seg->temporal_update) {
422      const int pred_flag = mi->seg_id_predicted;
423      vp9_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
424      vp9_write(bc, pred_flag, pred_prob);
425      if (!pred_flag)
426        write_segment_id(bc, seg, segment_id);
427    } else {
428      write_segment_id(bc, seg, segment_id);
429    }
430  }
431
432  skip_coeff = write_skip_coeff(cpi, segment_id, m, bc);
433
434  if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME))
435    vp9_write(bc, rf != INTRA_FRAME,
436              vp9_get_pred_prob_intra_inter(cm, xd));
437
438  if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT &&
439      !(rf != INTRA_FRAME &&
440        (skip_coeff || vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)))) {
441    write_selected_tx_size(cpi, m, mi->tx_size, bsize, bc);
442  }
443
444  if (rf == INTRA_FRAME) {
445#ifdef ENTROPY_STATS
446    active_section = 6;
447#endif
448
449    if (bsize >= BLOCK_8X8) {
450      write_intra_mode(bc, mode, cm->fc.y_mode_prob[size_group_lookup[bsize]]);
451    } else {
452      int idx, idy;
453      const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
454      const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
455      for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
456        for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
457          const MB_PREDICTION_MODE bm = m->bmi[idy * 2 + idx].as_mode;
458          write_intra_mode(bc, bm, cm->fc.y_mode_prob[0]);
459        }
460      }
461    }
462    write_intra_mode(bc, mi->uv_mode, cm->fc.uv_mode_prob[mode]);
463  } else {
464    vp9_prob *mv_ref_p;
465    encode_ref_frame(cpi, bc);
466    mv_ref_p = cpi->common.fc.inter_mode_probs[mi->mode_context[rf]];
467
468#ifdef ENTROPY_STATS
469    active_section = 3;
470#endif
471
472    // If segment skip is not enabled code the mode.
473    if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)) {
474      if (bsize >= BLOCK_8X8) {
475        write_sb_mv_ref(bc, mode, mv_ref_p);
476        ++cm->counts.inter_mode[mi->mode_context[rf]]
477                               [inter_mode_offset(mode)];
478      }
479    }
480
481    if (cm->mcomp_filter_type == SWITCHABLE) {
482      const int ctx = vp9_get_pred_context_switchable_interp(xd);
483      write_token(bc, vp9_switchable_interp_tree,
484                  cm->fc.switchable_interp_prob[ctx],
485                  &vp9_switchable_interp_encodings[mi->interp_filter]);
486    } else {
487      assert(mi->interp_filter == cm->mcomp_filter_type);
488    }
489
490    if (bsize < BLOCK_8X8) {
491      int j;
492      MB_PREDICTION_MODE blockmode;
493      int_mv blockmv;
494      const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
495      const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
496      int idx, idy;
497      for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
498        for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
499          j = idy * 2 + idx;
500          blockmode = x->partition_info->bmi[j].mode;
501          blockmv = m->bmi[j].as_mv[0];
502          write_sb_mv_ref(bc, blockmode, mv_ref_p);
503          ++cm->counts.inter_mode[mi->mode_context[rf]]
504                                 [inter_mode_offset(blockmode)];
505
506          if (blockmode == NEWMV) {
507#ifdef ENTROPY_STATS
508            active_section = 11;
509#endif
510            vp9_encode_mv(cpi, bc, &blockmv.as_mv, &mi->best_mv.as_mv,
511                          nmvc, allow_hp);
512
513            if (mi->ref_frame[1] > INTRA_FRAME)
514              vp9_encode_mv(cpi, bc,
515                            &m->bmi[j].as_mv[1].as_mv,
516                            &mi->best_second_mv.as_mv,
517                            nmvc, allow_hp);
518          }
519        }
520      }
521    } else if (mode == NEWMV) {
522#ifdef ENTROPY_STATS
523      active_section = 5;
524#endif
525      vp9_encode_mv(cpi, bc, &mi->mv[0].as_mv, &mi->best_mv.as_mv,
526                    nmvc, allow_hp);
527
528      if (mi->ref_frame[1] > INTRA_FRAME)
529        vp9_encode_mv(cpi, bc, &mi->mv[1].as_mv, &mi->best_second_mv.as_mv,
530                      nmvc, allow_hp);
531    }
532  }
533}
534
535static void write_mb_modes_kf(const VP9_COMP *cpi, MODE_INFO **mi_8x8,
536                              vp9_writer *bc) {
537  const VP9_COMMON *const cm = &cpi->common;
538  const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
539  const struct segmentation *const seg = &cm->seg;
540  MODE_INFO *m = mi_8x8[0];
541  const int ym = m->mbmi.mode;
542  const int segment_id = m->mbmi.segment_id;
543  MODE_INFO *above_mi = mi_8x8[-xd->mode_info_stride];
544  MODE_INFO *left_mi = mi_8x8[-1];
545
546  if (seg->update_map)
547    write_segment_id(bc, seg, m->mbmi.segment_id);
548
549  write_skip_coeff(cpi, segment_id, m, bc);
550
551  if (m->mbmi.sb_type >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT)
552    write_selected_tx_size(cpi, m, m->mbmi.tx_size, m->mbmi.sb_type, bc);
553
554  if (m->mbmi.sb_type >= BLOCK_8X8) {
555    const MB_PREDICTION_MODE A = above_block_mode(m, above_mi, 0);
556    const MB_PREDICTION_MODE L = xd->left_available ?
557                                 left_block_mode(m, left_mi, 0) : DC_PRED;
558    write_intra_mode(bc, ym, vp9_kf_y_mode_prob[A][L]);
559  } else {
560    int idx, idy;
561    const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[m->mbmi.sb_type];
562    const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[m->mbmi.sb_type];
563    for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
564      for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
565        int i = idy * 2 + idx;
566        const MB_PREDICTION_MODE A = above_block_mode(m, above_mi, i);
567        const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
568                                     left_block_mode(m, left_mi, i) : DC_PRED;
569        const int bm = m->bmi[i].as_mode;
570#ifdef ENTROPY_STATS
571        ++intra_mode_stats[A][L][bm];
572#endif
573        write_intra_mode(bc, bm, vp9_kf_y_mode_prob[A][L]);
574      }
575    }
576  }
577
578  write_intra_mode(bc, m->mbmi.uv_mode, vp9_kf_uv_mode_prob[ym]);
579}
580
581static void write_modes_b(VP9_COMP *cpi, MODE_INFO **mi_8x8, vp9_writer *bc,
582                          TOKENEXTRA **tok, TOKENEXTRA *tok_end,
583                          int mi_row, int mi_col) {
584  VP9_COMMON *const cm = &cpi->common;
585  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
586  MODE_INFO *m = mi_8x8[0];
587
588  if (m->mbmi.sb_type < BLOCK_8X8)
589    if (xd->ab_index > 0)
590      return;
591
592  xd->this_mi = mi_8x8[0];
593  xd->mi_8x8 = mi_8x8;
594
595  set_mi_row_col(&cpi->common, xd,
596                 mi_row, num_8x8_blocks_high_lookup[m->mbmi.sb_type],
597                 mi_col, num_8x8_blocks_wide_lookup[m->mbmi.sb_type]);
598  if ((cm->frame_type == KEY_FRAME) || cm->intra_only) {
599    write_mb_modes_kf(cpi, mi_8x8, bc);
600#ifdef ENTROPY_STATS
601    active_section = 8;
602#endif
603  } else {
604    pack_inter_mode_mvs(cpi, m, bc);
605#ifdef ENTROPY_STATS
606    active_section = 1;
607#endif
608  }
609
610  assert(*tok < tok_end);
611  pack_mb_tokens(bc, tok, tok_end);
612}
613
614static void write_modes_sb(VP9_COMP *cpi, MODE_INFO **mi_8x8, vp9_writer *bc,
615                           TOKENEXTRA **tok, TOKENEXTRA *tok_end,
616                           int mi_row, int mi_col, BLOCK_SIZE bsize) {
617  VP9_COMMON *const cm = &cpi->common;
618  MACROBLOCKD *xd = &cpi->mb.e_mbd;
619  const int mis = cm->mode_info_stride;
620  int bsl = b_width_log2(bsize);
621  int bs = (1 << bsl) / 4;  // mode_info step for subsize
622  int n;
623  PARTITION_TYPE partition = PARTITION_NONE;
624  BLOCK_SIZE subsize;
625  MODE_INFO *m = mi_8x8[0];
626
627  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
628    return;
629
630  partition = partition_lookup[bsl][m->mbmi.sb_type];
631
632  if (bsize < BLOCK_8X8)
633    if (xd->ab_index > 0)
634      return;
635
636  if (bsize >= BLOCK_8X8) {
637    int pl;
638    const int idx = check_bsize_coverage(bs, cm->mi_rows, cm->mi_cols,
639                                         mi_row, mi_col);
640    set_partition_seg_context(cm, xd, mi_row, mi_col);
641    pl = partition_plane_context(xd, bsize);
642    // encode the partition information
643    if (idx == 0)
644      write_token(bc, vp9_partition_tree,
645                  cm->fc.partition_prob[cm->frame_type][pl],
646                  vp9_partition_encodings + partition);
647    else if (idx > 0)
648      vp9_write(bc, partition == PARTITION_SPLIT,
649                cm->fc.partition_prob[cm->frame_type][pl][idx]);
650  }
651
652  subsize = get_subsize(bsize, partition);
653  *(get_sb_index(xd, subsize)) = 0;
654
655  switch (partition) {
656    case PARTITION_NONE:
657      write_modes_b(cpi, mi_8x8, bc, tok, tok_end, mi_row, mi_col);
658      break;
659    case PARTITION_HORZ:
660      write_modes_b(cpi, mi_8x8, bc, tok, tok_end, mi_row, mi_col);
661      *(get_sb_index(xd, subsize)) = 1;
662      if ((mi_row + bs) < cm->mi_rows)
663        write_modes_b(cpi, mi_8x8 + bs * mis, bc, tok, tok_end, mi_row + bs,
664                      mi_col);
665      break;
666    case PARTITION_VERT:
667      write_modes_b(cpi, mi_8x8, bc, tok, tok_end, mi_row, mi_col);
668      *(get_sb_index(xd, subsize)) = 1;
669      if ((mi_col + bs) < cm->mi_cols)
670        write_modes_b(cpi, mi_8x8 + bs, bc, tok, tok_end, mi_row, mi_col + bs);
671      break;
672    case PARTITION_SPLIT:
673      for (n = 0; n < 4; n++) {
674        int j = n >> 1, i = n & 0x01;
675        *(get_sb_index(xd, subsize)) = n;
676        write_modes_sb(cpi, mi_8x8 + j * bs * mis + i * bs, bc, tok, tok_end,
677                       mi_row + j * bs, mi_col + i * bs, subsize);
678      }
679      break;
680    default:
681      assert(0);
682  }
683
684  // update partition context
685  if (bsize >= BLOCK_8X8 &&
686      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) {
687    set_partition_seg_context(cm, xd, mi_row, mi_col);
688    update_partition_context(xd, subsize, bsize);
689  }
690}
691
692static void write_modes(VP9_COMP *cpi, vp9_writer* const bc,
693                        TOKENEXTRA **tok, TOKENEXTRA *tok_end) {
694  VP9_COMMON *const cm = &cpi->common;
695  const int mis = cm->mode_info_stride;
696  int mi_row, mi_col;
697  MODE_INFO **mi_8x8 = cm->mi_grid_visible;
698  MODE_INFO **m_8x8;
699
700  mi_8x8 += cm->cur_tile_mi_col_start + cm->cur_tile_mi_row_start * mis;
701
702  for (mi_row = cm->cur_tile_mi_row_start; mi_row < cm->cur_tile_mi_row_end;
703       mi_row += 8, mi_8x8 += 8 * mis) {
704    m_8x8 = mi_8x8;
705    vp9_zero(cm->left_seg_context);
706    for (mi_col = cm->cur_tile_mi_col_start; mi_col < cm->cur_tile_mi_col_end;
707         mi_col += MI_BLOCK_SIZE, m_8x8 += MI_BLOCK_SIZE) {
708      write_modes_sb(cpi, m_8x8, bc, tok, tok_end, mi_row, mi_col,
709                     BLOCK_64X64);
710    }
711  }
712}
713
714/* This function is used for debugging probability trees. */
715static void print_prob_tree(vp9_coeff_probs *coef_probs, int block_types) {
716  /* print coef probability tree */
717  int i, j, k, l, m;
718  FILE *f = fopen("enc_tree_probs.txt", "a");
719  fprintf(f, "{\n");
720  for (i = 0; i < block_types; i++) {
721    fprintf(f, "  {\n");
722    for (j = 0; j < REF_TYPES; ++j) {
723      fprintf(f, "  {\n");
724      for (k = 0; k < COEF_BANDS; k++) {
725        fprintf(f, "    {\n");
726        for (l = 0; l < PREV_COEF_CONTEXTS; l++) {
727          fprintf(f, "      {");
728          for (m = 0; m < ENTROPY_NODES; m++) {
729            fprintf(f, "%3u, ",
730                    (unsigned int)(coef_probs[i][j][k][l][m]));
731          }
732        }
733        fprintf(f, " }\n");
734      }
735      fprintf(f, "    }\n");
736    }
737    fprintf(f, "  }\n");
738  }
739  fprintf(f, "}\n");
740  fclose(f);
741}
742
743static void build_tree_distribution(VP9_COMP *cpi, TX_SIZE tx_size) {
744  vp9_coeff_probs_model *coef_probs = cpi->frame_coef_probs[tx_size];
745  vp9_coeff_count *coef_counts = cpi->coef_counts[tx_size];
746  unsigned int (*eob_branch_ct)[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] =
747      cpi->common.counts.eob_branch[tx_size];
748  vp9_coeff_stats *coef_branch_ct = cpi->frame_branch_ct[tx_size];
749  vp9_prob full_probs[ENTROPY_NODES];
750  int i, j, k, l;
751
752  for (i = 0; i < BLOCK_TYPES; ++i) {
753    for (j = 0; j < REF_TYPES; ++j) {
754      for (k = 0; k < COEF_BANDS; ++k) {
755        for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
756          if (l >= 3 && k == 0)
757            continue;
758          vp9_tree_probs_from_distribution(vp9_coef_tree,
759                                           full_probs,
760                                           coef_branch_ct[i][j][k][l],
761                                           coef_counts[i][j][k][l], 0);
762          vpx_memcpy(coef_probs[i][j][k][l], full_probs,
763                     sizeof(vp9_prob) * UNCONSTRAINED_NODES);
764          coef_branch_ct[i][j][k][l][0][1] = eob_branch_ct[i][j][k][l] -
765                                             coef_branch_ct[i][j][k][l][0][0];
766          coef_probs[i][j][k][l][0] =
767              get_binary_prob(coef_branch_ct[i][j][k][l][0][0],
768                              coef_branch_ct[i][j][k][l][0][1]);
769#ifdef ENTROPY_STATS
770          if (!cpi->dummy_packing) {
771            int t;
772            for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
773              context_counters[tx_size][i][j][k][l][t] +=
774                  coef_counts[i][j][k][l][t];
775            context_counters[tx_size][i][j][k][l][MAX_ENTROPY_TOKENS] +=
776                eob_branch_ct[i][j][k][l];
777          }
778#endif
779        }
780      }
781    }
782  }
783}
784
785static void build_coeff_contexts(VP9_COMP *cpi) {
786  TX_SIZE t;
787  for (t = TX_4X4; t <= TX_32X32; t++)
788    build_tree_distribution(cpi, t);
789}
790
791static void update_coef_probs_common(vp9_writer* const bc, VP9_COMP *cpi,
792                                     TX_SIZE tx_size) {
793  vp9_coeff_probs_model *new_frame_coef_probs = cpi->frame_coef_probs[tx_size];
794  vp9_coeff_probs_model *old_frame_coef_probs =
795      cpi->common.fc.coef_probs[tx_size];
796  vp9_coeff_stats *frame_branch_ct = cpi->frame_branch_ct[tx_size];
797  const vp9_prob upd = VP9_COEF_UPDATE_PROB;
798  const int entropy_nodes_update = UNCONSTRAINED_NODES;
799  int i, j, k, l, t;
800  switch (cpi->sf.use_fast_coef_updates) {
801    case 0: {
802      /* dry run to see if there is any udpate at all needed */
803      int savings = 0;
804      int update[2] = {0, 0};
805      for (i = 0; i < BLOCK_TYPES; ++i) {
806        for (j = 0; j < REF_TYPES; ++j) {
807          for (k = 0; k < COEF_BANDS; ++k) {
808            for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
809              for (t = 0; t < entropy_nodes_update; ++t) {
810                vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
811                const vp9_prob oldp = old_frame_coef_probs[i][j][k][l][t];
812                int s;
813                int u = 0;
814
815                if (l >= 3 && k == 0)
816                  continue;
817                if (t == PIVOT_NODE)
818                  s = vp9_prob_diff_update_savings_search_model(
819                      frame_branch_ct[i][j][k][l][0],
820                      old_frame_coef_probs[i][j][k][l], &newp, upd, i, j);
821                else
822                  s = vp9_prob_diff_update_savings_search(
823                      frame_branch_ct[i][j][k][l][t], oldp, &newp, upd);
824                if (s > 0 && newp != oldp)
825                  u = 1;
826                if (u)
827                  savings += s - (int)(vp9_cost_zero(upd));
828                else
829                  savings -= (int)(vp9_cost_zero(upd));
830                update[u]++;
831              }
832            }
833          }
834        }
835      }
836
837      // printf("Update %d %d, savings %d\n", update[0], update[1], savings);
838      /* Is coef updated at all */
839      if (update[1] == 0 || savings < 0) {
840        vp9_write_bit(bc, 0);
841        return;
842      }
843      vp9_write_bit(bc, 1);
844      for (i = 0; i < BLOCK_TYPES; ++i) {
845        for (j = 0; j < REF_TYPES; ++j) {
846          for (k = 0; k < COEF_BANDS; ++k) {
847            for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
848              // calc probs and branch cts for this frame only
849              for (t = 0; t < entropy_nodes_update; ++t) {
850                vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
851                vp9_prob *oldp = old_frame_coef_probs[i][j][k][l] + t;
852                const vp9_prob upd = VP9_COEF_UPDATE_PROB;
853                int s;
854                int u = 0;
855                if (l >= 3 && k == 0)
856                  continue;
857                if (t == PIVOT_NODE)
858                  s = vp9_prob_diff_update_savings_search_model(
859                      frame_branch_ct[i][j][k][l][0],
860                      old_frame_coef_probs[i][j][k][l], &newp, upd, i, j);
861                else
862                  s = vp9_prob_diff_update_savings_search(
863                      frame_branch_ct[i][j][k][l][t],
864                      *oldp, &newp, upd);
865                if (s > 0 && newp != *oldp)
866                  u = 1;
867                vp9_write(bc, u, upd);
868#ifdef ENTROPY_STATS
869                if (!cpi->dummy_packing)
870                  ++tree_update_hist[tx_size][i][j][k][l][t][u];
871#endif
872                if (u) {
873                  /* send/use new probability */
874                  vp9_write_prob_diff_update(bc, newp, *oldp);
875                  *oldp = newp;
876                }
877              }
878            }
879          }
880        }
881      }
882      return;
883    }
884
885    case 1:
886    case 2: {
887      const int prev_coef_contexts_to_update =
888          (cpi->sf.use_fast_coef_updates == 2 ?
889           PREV_COEF_CONTEXTS >> 1 : PREV_COEF_CONTEXTS);
890      const int coef_band_to_update =
891          (cpi->sf.use_fast_coef_updates == 2 ?
892           COEF_BANDS >> 1 : COEF_BANDS);
893      int updates = 0;
894      int noupdates_before_first = 0;
895      for (i = 0; i < BLOCK_TYPES; ++i) {
896        for (j = 0; j < REF_TYPES; ++j) {
897          for (k = 0; k < COEF_BANDS; ++k) {
898            for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
899              // calc probs and branch cts for this frame only
900              for (t = 0; t < entropy_nodes_update; ++t) {
901                vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
902                vp9_prob *oldp = old_frame_coef_probs[i][j][k][l] + t;
903                int s;
904                int u = 0;
905                if (l >= 3 && k == 0)
906                  continue;
907                if (l >= prev_coef_contexts_to_update ||
908                    k >= coef_band_to_update) {
909                  u = 0;
910                } else {
911                  if (t == PIVOT_NODE)
912                    s = vp9_prob_diff_update_savings_search_model(
913                        frame_branch_ct[i][j][k][l][0],
914                        old_frame_coef_probs[i][j][k][l], &newp, upd, i, j);
915                  else
916                    s = vp9_prob_diff_update_savings_search(
917                        frame_branch_ct[i][j][k][l][t],
918                        *oldp, &newp, upd);
919                  if (s > 0 && newp != *oldp)
920                    u = 1;
921                }
922                updates += u;
923                if (u == 0 && updates == 0) {
924                  noupdates_before_first++;
925#ifdef ENTROPY_STATS
926                  if (!cpi->dummy_packing)
927                    ++tree_update_hist[tx_size][i][j][k][l][t][u];
928#endif
929                  continue;
930                }
931                if (u == 1 && updates == 1) {
932                  int v;
933                  // first update
934                  vp9_write_bit(bc, 1);
935                  for (v = 0; v < noupdates_before_first; ++v)
936                    vp9_write(bc, 0, upd);
937                }
938                vp9_write(bc, u, upd);
939#ifdef ENTROPY_STATS
940                if (!cpi->dummy_packing)
941                  ++tree_update_hist[tx_size][i][j][k][l][t][u];
942#endif
943                if (u) {
944                  /* send/use new probability */
945                  vp9_write_prob_diff_update(bc, newp, *oldp);
946                  *oldp = newp;
947                }
948              }
949            }
950          }
951        }
952      }
953      if (updates == 0) {
954        vp9_write_bit(bc, 0);  // no updates
955      }
956      return;
957    }
958
959    default:
960      assert(0);
961  }
962}
963
964static void update_coef_probs(VP9_COMP* const cpi, vp9_writer* const bc) {
965  const TX_MODE tx_mode = cpi->common.tx_mode;
966
967  vp9_clear_system_state();
968
969  // Build the cofficient contexts based on counts collected in encode loop
970  build_coeff_contexts(cpi);
971
972  update_coef_probs_common(bc, cpi, TX_4X4);
973
974  // do not do this if not even allowed
975  if (tx_mode > ONLY_4X4)
976    update_coef_probs_common(bc, cpi, TX_8X8);
977
978  if (tx_mode > ALLOW_8X8)
979    update_coef_probs_common(bc, cpi, TX_16X16);
980
981  if (tx_mode > ALLOW_16X16)
982    update_coef_probs_common(bc, cpi, TX_32X32);
983}
984
985static void encode_loopfilter(struct loopfilter *lf,
986                              struct vp9_write_bit_buffer *wb) {
987  int i;
988
989  // Encode the loop filter level and type
990  vp9_wb_write_literal(wb, lf->filter_level, 6);
991  vp9_wb_write_literal(wb, lf->sharpness_level, 3);
992
993  // Write out loop filter deltas applied at the MB level based on mode or
994  // ref frame (if they are enabled).
995  vp9_wb_write_bit(wb, lf->mode_ref_delta_enabled);
996
997  if (lf->mode_ref_delta_enabled) {
998    // Do the deltas need to be updated
999    vp9_wb_write_bit(wb, lf->mode_ref_delta_update);
1000    if (lf->mode_ref_delta_update) {
1001      // Send update
1002      for (i = 0; i < MAX_REF_LF_DELTAS; i++) {
1003        const int delta = lf->ref_deltas[i];
1004
1005        // Frame level data
1006        if (delta != lf->last_ref_deltas[i]) {
1007          lf->last_ref_deltas[i] = delta;
1008          vp9_wb_write_bit(wb, 1);
1009
1010          assert(delta != 0);
1011          vp9_wb_write_literal(wb, abs(delta) & 0x3F, 6);
1012          vp9_wb_write_bit(wb, delta < 0);
1013        } else {
1014          vp9_wb_write_bit(wb, 0);
1015        }
1016      }
1017
1018      // Send update
1019      for (i = 0; i < MAX_MODE_LF_DELTAS; i++) {
1020        const int delta = lf->mode_deltas[i];
1021        if (delta != lf->last_mode_deltas[i]) {
1022          lf->last_mode_deltas[i] = delta;
1023          vp9_wb_write_bit(wb, 1);
1024
1025          assert(delta != 0);
1026          vp9_wb_write_literal(wb, abs(delta) & 0x3F, 6);
1027          vp9_wb_write_bit(wb, delta < 0);
1028        } else {
1029          vp9_wb_write_bit(wb, 0);
1030        }
1031      }
1032    }
1033  }
1034}
1035
1036static void write_delta_q(struct vp9_write_bit_buffer *wb, int delta_q) {
1037  if (delta_q != 0) {
1038    vp9_wb_write_bit(wb, 1);
1039    vp9_wb_write_literal(wb, abs(delta_q), 4);
1040    vp9_wb_write_bit(wb, delta_q < 0);
1041  } else {
1042    vp9_wb_write_bit(wb, 0);
1043  }
1044}
1045
1046static void encode_quantization(VP9_COMMON *cm,
1047                                struct vp9_write_bit_buffer *wb) {
1048  vp9_wb_write_literal(wb, cm->base_qindex, QINDEX_BITS);
1049  write_delta_q(wb, cm->y_dc_delta_q);
1050  write_delta_q(wb, cm->uv_dc_delta_q);
1051  write_delta_q(wb, cm->uv_ac_delta_q);
1052}
1053
1054
1055static void encode_segmentation(VP9_COMP *cpi,
1056                                struct vp9_write_bit_buffer *wb) {
1057  int i, j;
1058
1059  struct segmentation *seg = &cpi->common.seg;
1060
1061  vp9_wb_write_bit(wb, seg->enabled);
1062  if (!seg->enabled)
1063    return;
1064
1065  // Segmentation map
1066  vp9_wb_write_bit(wb, seg->update_map);
1067  if (seg->update_map) {
1068    // Select the coding strategy (temporal or spatial)
1069    vp9_choose_segmap_coding_method(cpi);
1070    // Write out probabilities used to decode unpredicted  macro-block segments
1071    for (i = 0; i < SEG_TREE_PROBS; i++) {
1072      const int prob = seg->tree_probs[i];
1073      const int update = prob != MAX_PROB;
1074      vp9_wb_write_bit(wb, update);
1075      if (update)
1076        vp9_wb_write_literal(wb, prob, 8);
1077    }
1078
1079    // Write out the chosen coding method.
1080    vp9_wb_write_bit(wb, seg->temporal_update);
1081    if (seg->temporal_update) {
1082      for (i = 0; i < PREDICTION_PROBS; i++) {
1083        const int prob = seg->pred_probs[i];
1084        const int update = prob != MAX_PROB;
1085        vp9_wb_write_bit(wb, update);
1086        if (update)
1087          vp9_wb_write_literal(wb, prob, 8);
1088      }
1089    }
1090  }
1091
1092  // Segmentation data
1093  vp9_wb_write_bit(wb, seg->update_data);
1094  if (seg->update_data) {
1095    vp9_wb_write_bit(wb, seg->abs_delta);
1096
1097    for (i = 0; i < MAX_SEGMENTS; i++) {
1098      for (j = 0; j < SEG_LVL_MAX; j++) {
1099        const int active = vp9_segfeature_active(seg, i, j);
1100        vp9_wb_write_bit(wb, active);
1101        if (active) {
1102          const int data = vp9_get_segdata(seg, i, j);
1103          const int data_max = vp9_seg_feature_data_max(j);
1104
1105          if (vp9_is_segfeature_signed(j)) {
1106            vp9_encode_unsigned_max(wb, abs(data), data_max);
1107            vp9_wb_write_bit(wb, data < 0);
1108          } else {
1109            vp9_encode_unsigned_max(wb, data, data_max);
1110          }
1111        }
1112      }
1113    }
1114  }
1115}
1116
1117
1118static void encode_txfm_probs(VP9_COMP *cpi, vp9_writer *w) {
1119  VP9_COMMON *const cm = &cpi->common;
1120
1121  // Mode
1122  vp9_write_literal(w, MIN(cm->tx_mode, ALLOW_32X32), 2);
1123  if (cm->tx_mode >= ALLOW_32X32)
1124    vp9_write_bit(w, cm->tx_mode == TX_MODE_SELECT);
1125
1126  // Probabilities
1127  if (cm->tx_mode == TX_MODE_SELECT) {
1128    int i, j;
1129    unsigned int ct_8x8p[TX_SIZES - 3][2];
1130    unsigned int ct_16x16p[TX_SIZES - 2][2];
1131    unsigned int ct_32x32p[TX_SIZES - 1][2];
1132
1133
1134    for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
1135      tx_counts_to_branch_counts_8x8(cm->counts.tx.p8x8[i],
1136                                     ct_8x8p);
1137      for (j = 0; j < TX_SIZES - 3; j++)
1138        vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p8x8[i][j],
1139                                  MODE_UPDATE_PROB, ct_8x8p[j]);
1140    }
1141
1142    for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
1143      tx_counts_to_branch_counts_16x16(cm->counts.tx.p16x16[i],
1144                                       ct_16x16p);
1145      for (j = 0; j < TX_SIZES - 2; j++)
1146        vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p16x16[i][j],
1147                                  MODE_UPDATE_PROB, ct_16x16p[j]);
1148    }
1149
1150    for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
1151      tx_counts_to_branch_counts_32x32(cm->counts.tx.p32x32[i], ct_32x32p);
1152      for (j = 0; j < TX_SIZES - 1; j++)
1153        vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p32x32[i][j],
1154                                  MODE_UPDATE_PROB, ct_32x32p[j]);
1155    }
1156#ifdef MODE_STATS
1157    if (!cpi->dummy_packing)
1158      update_tx_count_stats(cm);
1159#endif
1160  }
1161}
1162
1163static void write_interp_filter_type(INTERPOLATIONFILTERTYPE type,
1164                                     struct vp9_write_bit_buffer *wb) {
1165  const int type_to_literal[] = { 1, 0, 2 };
1166
1167  vp9_wb_write_bit(wb, type == SWITCHABLE);
1168  if (type != SWITCHABLE)
1169    vp9_wb_write_literal(wb, type_to_literal[type], 2);
1170}
1171
1172static void fix_mcomp_filter_type(VP9_COMP *cpi) {
1173  VP9_COMMON *const cm = &cpi->common;
1174
1175  if (cm->mcomp_filter_type == SWITCHABLE) {
1176    // Check to see if only one of the filters is actually used
1177    int count[SWITCHABLE_FILTERS];
1178    int i, j, c = 0;
1179    for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
1180      count[i] = 0;
1181      for (j = 0; j <= SWITCHABLE_FILTERS; ++j)
1182        count[i] += cm->counts.switchable_interp[j][i];
1183      c += (count[i] > 0);
1184    }
1185    if (c == 1) {
1186      // Only one filter is used. So set the filter at frame level
1187      for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
1188        if (count[i]) {
1189          cm->mcomp_filter_type = i;
1190          break;
1191        }
1192      }
1193    }
1194  }
1195}
1196
1197static void write_tile_info(VP9_COMMON *cm, struct vp9_write_bit_buffer *wb) {
1198  int min_log2_tile_cols, max_log2_tile_cols, ones;
1199  vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1200
1201  // columns
1202  ones = cm->log2_tile_cols - min_log2_tile_cols;
1203  while (ones--)
1204    vp9_wb_write_bit(wb, 1);
1205
1206  if (cm->log2_tile_cols < max_log2_tile_cols)
1207    vp9_wb_write_bit(wb, 0);
1208
1209  // rows
1210  vp9_wb_write_bit(wb, cm->log2_tile_rows != 0);
1211  if (cm->log2_tile_rows != 0)
1212    vp9_wb_write_bit(wb, cm->log2_tile_rows != 1);
1213}
1214
1215static int get_refresh_mask(VP9_COMP *cpi) {
1216    // Should the GF or ARF be updated using the transmitted frame or buffer
1217#if CONFIG_MULTIPLE_ARF
1218    if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame &&
1219        !cpi->refresh_alt_ref_frame) {
1220#else
1221    if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame &&
1222        !cpi->use_svc) {
1223#endif
1224      // Preserve the previously existing golden frame and update the frame in
1225      // the alt ref slot instead. This is highly specific to the use of
1226      // alt-ref as a forward reference, and this needs to be generalized as
1227      // other uses are implemented (like RTC/temporal scaling)
1228      //
1229      // gld_fb_idx and alt_fb_idx need to be swapped for future frames, but
1230      // that happens in vp9_onyx_if.c:update_reference_frames() so that it can
1231      // be done outside of the recode loop.
1232      return (cpi->refresh_last_frame << cpi->lst_fb_idx) |
1233             (cpi->refresh_golden_frame << cpi->alt_fb_idx);
1234    } else {
1235      int arf_idx = cpi->alt_fb_idx;
1236#if CONFIG_MULTIPLE_ARF
1237      // Determine which ARF buffer to use to encode this ARF frame.
1238      if (cpi->multi_arf_enabled) {
1239        int sn = cpi->sequence_number;
1240        arf_idx = (cpi->frame_coding_order[sn] < 0) ?
1241            cpi->arf_buffer_idx[sn + 1] :
1242            cpi->arf_buffer_idx[sn];
1243      }
1244#endif
1245      return (cpi->refresh_last_frame << cpi->lst_fb_idx) |
1246             (cpi->refresh_golden_frame << cpi->gld_fb_idx) |
1247             (cpi->refresh_alt_ref_frame << arf_idx);
1248    }
1249}
1250
1251static size_t encode_tiles(VP9_COMP *cpi, uint8_t *data_ptr) {
1252  VP9_COMMON *const cm = &cpi->common;
1253  vp9_writer residual_bc;
1254
1255  int tile_row, tile_col;
1256  TOKENEXTRA *tok[4][1 << 6], *tok_end;
1257  size_t total_size = 0;
1258  const int tile_cols = 1 << cm->log2_tile_cols;
1259  const int tile_rows = 1 << cm->log2_tile_rows;
1260
1261  vpx_memset(cm->above_seg_context, 0, sizeof(PARTITION_CONTEXT) *
1262             mi_cols_aligned_to_sb(cm->mi_cols));
1263
1264  tok[0][0] = cpi->tok;
1265  for (tile_row = 0; tile_row < tile_rows; tile_row++) {
1266    if (tile_row)
1267      tok[tile_row][0] = tok[tile_row - 1][tile_cols - 1] +
1268                         cpi->tok_count[tile_row - 1][tile_cols - 1];
1269
1270    for (tile_col = 1; tile_col < tile_cols; tile_col++)
1271      tok[tile_row][tile_col] = tok[tile_row][tile_col - 1] +
1272                                cpi->tok_count[tile_row][tile_col - 1];
1273  }
1274
1275  for (tile_row = 0; tile_row < tile_rows; tile_row++) {
1276    vp9_get_tile_row_offsets(cm, tile_row);
1277    for (tile_col = 0; tile_col < tile_cols; tile_col++) {
1278      vp9_get_tile_col_offsets(cm, tile_col);
1279      tok_end = tok[tile_row][tile_col] + cpi->tok_count[tile_row][tile_col];
1280
1281      if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1)
1282        vp9_start_encode(&residual_bc, data_ptr + total_size + 4);
1283      else
1284        vp9_start_encode(&residual_bc, data_ptr + total_size);
1285
1286      write_modes(cpi, &residual_bc, &tok[tile_row][tile_col], tok_end);
1287      assert(tok[tile_row][tile_col] == tok_end);
1288      vp9_stop_encode(&residual_bc);
1289      if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) {
1290        // size of this tile
1291        write_be32(data_ptr + total_size, residual_bc.pos);
1292        total_size += 4;
1293      }
1294
1295      total_size += residual_bc.pos;
1296    }
1297  }
1298
1299  return total_size;
1300}
1301
1302static void write_display_size(VP9_COMP *cpi, struct vp9_write_bit_buffer *wb) {
1303  VP9_COMMON *const cm = &cpi->common;
1304
1305  const int scaling_active = cm->width != cm->display_width ||
1306                             cm->height != cm->display_height;
1307  vp9_wb_write_bit(wb, scaling_active);
1308  if (scaling_active) {
1309    vp9_wb_write_literal(wb, cm->display_width - 1, 16);
1310    vp9_wb_write_literal(wb, cm->display_height - 1, 16);
1311  }
1312}
1313
1314static void write_frame_size(VP9_COMP *cpi,
1315                             struct vp9_write_bit_buffer *wb) {
1316  VP9_COMMON *const cm = &cpi->common;
1317  vp9_wb_write_literal(wb, cm->width - 1, 16);
1318  vp9_wb_write_literal(wb, cm->height - 1, 16);
1319
1320  write_display_size(cpi, wb);
1321}
1322
1323static void write_frame_size_with_refs(VP9_COMP *cpi,
1324                                       struct vp9_write_bit_buffer *wb) {
1325  VP9_COMMON *const cm = &cpi->common;
1326  int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx,
1327                                      cpi->alt_fb_idx};
1328  int i, found = 0;
1329
1330  for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
1331    YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->ref_frame_map[refs[i]]];
1332    found = cm->width == cfg->y_crop_width &&
1333            cm->height == cfg->y_crop_height;
1334
1335    // TODO(ivan): This prevents a bug while more than 3 buffers are used. Do it
1336    // in a better way.
1337    if (cpi->use_svc) {
1338      found = 0;
1339    }
1340    vp9_wb_write_bit(wb, found);
1341    if (found) {
1342      break;
1343    }
1344  }
1345
1346  if (!found) {
1347    vp9_wb_write_literal(wb, cm->width - 1, 16);
1348    vp9_wb_write_literal(wb, cm->height - 1, 16);
1349  }
1350
1351  write_display_size(cpi, wb);
1352}
1353
1354static void write_sync_code(struct vp9_write_bit_buffer *wb) {
1355  vp9_wb_write_literal(wb, SYNC_CODE_0, 8);
1356  vp9_wb_write_literal(wb, SYNC_CODE_1, 8);
1357  vp9_wb_write_literal(wb, SYNC_CODE_2, 8);
1358}
1359
1360static void write_uncompressed_header(VP9_COMP *cpi,
1361                                      struct vp9_write_bit_buffer *wb) {
1362  VP9_COMMON *const cm = &cpi->common;
1363  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1364
1365  // frame marker bits
1366  vp9_wb_write_literal(wb, 0x2, 2);
1367
1368  // bitstream version.
1369  // 00 - profile 0. 4:2:0 only
1370  // 10 - profile 1. adds 4:4:4, 4:2:2, alpha
1371  vp9_wb_write_bit(wb, cm->version);
1372  vp9_wb_write_bit(wb, 0);
1373
1374  vp9_wb_write_bit(wb, 0);
1375  vp9_wb_write_bit(wb, cm->frame_type);
1376  vp9_wb_write_bit(wb, cm->show_frame);
1377  vp9_wb_write_bit(wb, cm->error_resilient_mode);
1378
1379  if (cm->frame_type == KEY_FRAME) {
1380    write_sync_code(wb);
1381    // colorspaces
1382    // 000 - Unknown
1383    // 001 - BT.601
1384    // 010 - BT.709
1385    // 011 - SMPTE-170
1386    // 100 - SMPTE-240
1387    // 101 - Reserved
1388    // 110 - Reserved
1389    // 111 - sRGB (RGB)
1390    vp9_wb_write_literal(wb, 0, 3);
1391    if (1 /* colorspace != sRGB */) {
1392      vp9_wb_write_bit(wb, 0);  // 0: [16, 235] (i.e. xvYCC), 1: [0, 255]
1393      if (cm->version == 1) {
1394        vp9_wb_write_bit(wb, cm->subsampling_x);
1395        vp9_wb_write_bit(wb, cm->subsampling_y);
1396        vp9_wb_write_bit(wb, 0);  // has extra plane
1397      }
1398    } else {
1399      assert(cm->version == 1);
1400      vp9_wb_write_bit(wb, 0);  // has extra plane
1401    }
1402
1403    write_frame_size(cpi, wb);
1404  } else {
1405    const int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx,
1406                                              cpi->alt_fb_idx};
1407    if (!cm->show_frame)
1408      vp9_wb_write_bit(wb, cm->intra_only);
1409
1410    if (!cm->error_resilient_mode)
1411      vp9_wb_write_literal(wb, cm->reset_frame_context, 2);
1412
1413    if (cm->intra_only) {
1414      write_sync_code(wb);
1415
1416      vp9_wb_write_literal(wb, get_refresh_mask(cpi), NUM_REF_FRAMES);
1417      write_frame_size(cpi, wb);
1418    } else {
1419      int i;
1420      vp9_wb_write_literal(wb, get_refresh_mask(cpi), NUM_REF_FRAMES);
1421      for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
1422        vp9_wb_write_literal(wb, refs[i], NUM_REF_FRAMES_LOG2);
1423        vp9_wb_write_bit(wb, cm->ref_frame_sign_bias[LAST_FRAME + i]);
1424      }
1425
1426      write_frame_size_with_refs(cpi, wb);
1427
1428      vp9_wb_write_bit(wb, xd->allow_high_precision_mv);
1429
1430      fix_mcomp_filter_type(cpi);
1431      write_interp_filter_type(cm->mcomp_filter_type, wb);
1432    }
1433  }
1434
1435  if (!cm->error_resilient_mode) {
1436    vp9_wb_write_bit(wb, cm->refresh_frame_context);
1437    vp9_wb_write_bit(wb, cm->frame_parallel_decoding_mode);
1438  }
1439
1440  vp9_wb_write_literal(wb, cm->frame_context_idx, NUM_FRAME_CONTEXTS_LOG2);
1441
1442  encode_loopfilter(&cm->lf, wb);
1443  encode_quantization(cm, wb);
1444  encode_segmentation(cpi, wb);
1445
1446  write_tile_info(cm, wb);
1447}
1448
1449static size_t write_compressed_header(VP9_COMP *cpi, uint8_t *data) {
1450  VP9_COMMON *const cm = &cpi->common;
1451  MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1452  FRAME_CONTEXT *const fc = &cm->fc;
1453  vp9_writer header_bc;
1454
1455  vp9_start_encode(&header_bc, data);
1456
1457  if (xd->lossless)
1458    cm->tx_mode = ONLY_4X4;
1459  else
1460    encode_txfm_probs(cpi, &header_bc);
1461
1462  update_coef_probs(cpi, &header_bc);
1463
1464#ifdef ENTROPY_STATS
1465  active_section = 2;
1466#endif
1467
1468  vp9_update_skip_probs(cpi, &header_bc);
1469
1470  if (cm->frame_type != KEY_FRAME) {
1471    int i;
1472#ifdef ENTROPY_STATS
1473    active_section = 1;
1474#endif
1475
1476    update_inter_mode_probs(cm, &header_bc);
1477    vp9_zero(cm->counts.inter_mode);
1478
1479    if (cm->mcomp_filter_type == SWITCHABLE)
1480      update_switchable_interp_probs(cpi, &header_bc);
1481
1482    for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
1483      vp9_cond_prob_diff_update(&header_bc, &fc->intra_inter_prob[i],
1484                                MODE_UPDATE_PROB,
1485                                cpi->intra_inter_count[i]);
1486
1487    if (cm->allow_comp_inter_inter) {
1488      const int comp_pred_mode = cpi->common.comp_pred_mode;
1489      const int use_compound_pred = comp_pred_mode != SINGLE_PREDICTION_ONLY;
1490      const int use_hybrid_pred = comp_pred_mode == HYBRID_PREDICTION;
1491
1492      vp9_write_bit(&header_bc, use_compound_pred);
1493      if (use_compound_pred) {
1494        vp9_write_bit(&header_bc, use_hybrid_pred);
1495        if (use_hybrid_pred)
1496          for (i = 0; i < COMP_INTER_CONTEXTS; i++)
1497            vp9_cond_prob_diff_update(&header_bc, &fc->comp_inter_prob[i],
1498                                      MODE_UPDATE_PROB,
1499                                      cpi->comp_inter_count[i]);
1500      }
1501    }
1502
1503    if (cm->comp_pred_mode != COMP_PREDICTION_ONLY) {
1504      for (i = 0; i < REF_CONTEXTS; i++) {
1505        vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][0],
1506                                  MODE_UPDATE_PROB,
1507                                  cpi->single_ref_count[i][0]);
1508        vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][1],
1509                                  MODE_UPDATE_PROB,
1510                                  cpi->single_ref_count[i][1]);
1511      }
1512    }
1513
1514    if (cm->comp_pred_mode != SINGLE_PREDICTION_ONLY)
1515      for (i = 0; i < REF_CONTEXTS; i++)
1516        vp9_cond_prob_diff_update(&header_bc, &fc->comp_ref_prob[i],
1517                                  MODE_UPDATE_PROB,
1518                                  cpi->comp_ref_count[i]);
1519
1520    update_mbintra_mode_probs(cpi, &header_bc);
1521
1522    for (i = 0; i < NUM_PARTITION_CONTEXTS; ++i) {
1523      vp9_prob pnew[PARTITION_TYPES - 1];
1524      unsigned int bct[PARTITION_TYPES - 1][2];
1525      update_mode(&header_bc, PARTITION_TYPES,
1526                  vp9_partition_tree, pnew,
1527                  fc->partition_prob[cm->frame_type][i], bct,
1528                  (unsigned int *)cpi->partition_count[i]);
1529    }
1530
1531    vp9_write_nmv_probs(cpi, xd->allow_high_precision_mv, &header_bc);
1532  }
1533
1534  vp9_stop_encode(&header_bc);
1535  assert(header_bc.pos <= 0xffff);
1536
1537  return header_bc.pos;
1538}
1539
1540void vp9_pack_bitstream(VP9_COMP *cpi, uint8_t *dest, unsigned long *size) {
1541  uint8_t *data = dest;
1542  size_t first_part_size;
1543  struct vp9_write_bit_buffer wb = {data, 0};
1544  struct vp9_write_bit_buffer saved_wb;
1545
1546  write_uncompressed_header(cpi, &wb);
1547  saved_wb = wb;
1548  vp9_wb_write_literal(&wb, 0, 16);  // don't know in advance first part. size
1549
1550  data += vp9_rb_bytes_written(&wb);
1551
1552  vp9_compute_update_table();
1553
1554#ifdef ENTROPY_STATS
1555  if (cm->frame_type == INTER_FRAME)
1556    active_section = 0;
1557  else
1558    active_section = 7;
1559#endif
1560
1561  vp9_clear_system_state();  // __asm emms;
1562
1563  first_part_size = write_compressed_header(cpi, data);
1564  data += first_part_size;
1565  vp9_wb_write_literal(&saved_wb, first_part_size, 16);
1566
1567  data += encode_tiles(cpi, data);
1568
1569  *size = data - dest;
1570}
1571
1572#ifdef ENTROPY_STATS
1573static void print_tree_update_for_type(FILE *f,
1574                                       vp9_coeff_stats *tree_update_hist,
1575                                       int block_types, const char *header) {
1576  int i, j, k, l, m;
1577
1578  fprintf(f, "const vp9_coeff_prob %s = {\n", header);
1579  for (i = 0; i < block_types; i++) {
1580    fprintf(f, "  { \n");
1581    for (j = 0; j < REF_TYPES; j++) {
1582      fprintf(f, "  { \n");
1583      for (k = 0; k < COEF_BANDS; k++) {
1584        fprintf(f, "    {\n");
1585        for (l = 0; l < PREV_COEF_CONTEXTS; l++) {
1586          fprintf(f, "      {");
1587          for (m = 0; m < ENTROPY_NODES; m++) {
1588            fprintf(f, "%3d, ",
1589                    get_binary_prob(tree_update_hist[i][j][k][l][m][0],
1590                                    tree_update_hist[i][j][k][l][m][1]));
1591          }
1592          fprintf(f, "},\n");
1593        }
1594        fprintf(f, "},\n");
1595      }
1596      fprintf(f, "    },\n");
1597    }
1598    fprintf(f, "  },\n");
1599  }
1600  fprintf(f, "};\n");
1601}
1602
1603void print_tree_update_probs() {
1604  FILE *f = fopen("coefupdprob.h", "w");
1605  fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
1606
1607  print_tree_update_for_type(f, tree_update_hist[TX_4X4],   BLOCK_TYPES,
1608                             "vp9_coef_update_probs_4x4[BLOCK_TYPES]");
1609  print_tree_update_for_type(f, tree_update_hist[TX_8X8],   BLOCK_TYPES,
1610                             "vp9_coef_update_probs_8x8[BLOCK_TYPES]");
1611  print_tree_update_for_type(f, tree_update_hist[TX_16X16], BLOCK_TYPES,
1612                             "vp9_coef_update_probs_16x16[BLOCK_TYPES]");
1613  print_tree_update_for_type(f, tree_update_hist[TX_32X32], BLOCK_TYPES,
1614                             "vp9_coef_update_probs_32x32[BLOCK_TYPES]");
1615
1616  fclose(f);
1617  f = fopen("treeupdate.bin", "wb");
1618  fwrite(tree_update_hist, sizeof(tree_update_hist), 1, f);
1619  fclose(f);
1620}
1621#endif
1622