2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
15 #include "vpx/vpx_encoder.h"
16 #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 #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"
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"
39 #if defined(SECTIONBITS_OUTPUT)
40 unsigned __int64 Sectionbits[500];
44 int intra_mode_stats[VP9_INTRA_MODES]
47 vp9_coeff_stats tree_update_hist[TX_SIZES][BLOCK_TYPES];
49 extern unsigned int active_section;
54 int64_t tx_count_32x32p_stats[TX_SIZE_CONTEXTS][TX_SIZES];
55 int64_t tx_count_16x16p_stats[TX_SIZE_CONTEXTS][TX_SIZES - 1];
56 int64_t tx_count_8x8p_stats[TX_SIZE_CONTEXTS][TX_SIZES - 2];
57 int64_t switchable_interp_stats[VP9_SWITCHABLE_FILTERS+1]
58 [VP9_SWITCHABLE_FILTERS];
60 void 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);
66 void init_switchable_interp_stats() {
67 vp9_zero(switchable_interp_stats);
70 static void update_tx_count_stats(VP9_COMMON *cm) {
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];
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];
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];
89 static void update_switchable_interp_stats(VP9_COMMON *cm) {
91 for (i = 0; i < VP9_SWITCHABLE_FILTERS+1; ++i)
92 for (j = 0; j < VP9_SWITCHABLE_FILTERS; ++j) {
93 switchable_interp_stats[i][j] += cm->fc.switchable_interp_count[i][j];
97 void write_tx_count_stats() {
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);
106 "vp9_default_tx_count_32x32p[TX_SIZE_CONTEXTS][TX_SIZES] = {\n");
107 for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
109 for (j = 0; j < TX_SIZES; j++) {
110 printf("%"PRId64", ", tx_count_32x32p_stats[i][j]);
116 "vp9_default_tx_count_16x16p[TX_SIZE_CONTEXTS][TX_SIZES-1] = {\n");
117 for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
119 for (j = 0; j < TX_SIZES - 1; j++) {
120 printf("%"PRId64", ", tx_count_16x16p_stats[i][j]);
126 "vp9_default_tx_count_8x8p[TX_SIZE_CONTEXTS][TX_SIZES-2] = {\n");
127 for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
129 for (j = 0; j < TX_SIZES - 2; j++) {
130 printf("%"PRId64", ", tx_count_8x8p_stats[i][j]);
137 void write_switchable_interp_stats() {
139 FILE *fp = fopen("switchable_interp.bin", "wb");
140 fwrite(switchable_interp_stats, sizeof(switchable_interp_stats), 1, fp);
144 "vp9_default_switchable_filter_count[VP9_SWITCHABLE_FILTERS+1]"
145 "[VP9_SWITCHABLE_FILTERS] = {\n");
146 for (i = 0; i < VP9_SWITCHABLE_FILTERS+1; i++) {
148 for (j = 0; j < VP9_SWITCHABLE_FILTERS; j++) {
149 printf("%"PRId64", ", switchable_interp_stats[i][j]);
157 static INLINE void write_be32(uint8_t *p, int value) {
164 void vp9_encode_unsigned_max(struct vp9_write_bit_buffer *wb,
166 vp9_wb_write_literal(wb, data, get_unsigned_bits(max));
169 static void update_mode(
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 */]
180 vp9_tree_probs_from_distribution(tree, Pnew, bct, num_events, 0);
183 for (i = 0; i < n; ++i) {
184 vp9_cond_prob_diff_update(w, &Pcur[i], VP9_MODE_UPDATE_PROB, bct[i]);
188 static void update_mbintra_mode_probs(VP9_COMP* const cpi,
189 vp9_writer* const bc) {
190 VP9_COMMON *const cm = &cpi->common;
192 vp9_prob pnew[VP9_INTRA_MODES - 1];
193 unsigned int bct[VP9_INTRA_MODES - 1][2];
195 for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
196 update_mode(bc, VP9_INTRA_MODES, vp9_intra_mode_tree, pnew,
197 cm->fc.y_mode_prob[j], bct,
198 (unsigned int *)cpi->y_mode_count[j]);
201 static void write_selected_tx_size(const VP9_COMP *cpi, TX_SIZE tx_size,
202 BLOCK_SIZE_TYPE bsize, vp9_writer *w) {
203 const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
204 const vp9_prob *tx_probs = get_tx_probs2(xd, &cpi->common.fc.tx_probs);
205 vp9_write(w, tx_size != TX_4X4, tx_probs[0]);
206 if (bsize >= BLOCK_16X16 && tx_size != TX_4X4) {
207 vp9_write(w, tx_size != TX_8X8, tx_probs[1]);
208 if (bsize >= BLOCK_32X32 && tx_size != TX_8X8)
209 vp9_write(w, tx_size != TX_16X16, tx_probs[2]);
213 static int write_skip_coeff(const VP9_COMP *cpi, int segment_id, MODE_INFO *m,
215 const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
216 if (vp9_segfeature_active(&xd->seg, segment_id, SEG_LVL_SKIP)) {
219 const int skip_coeff = m->mbmi.mb_skip_coeff;
220 vp9_write(w, skip_coeff, vp9_get_pred_prob_mbskip(&cpi->common, xd));
225 void vp9_update_skip_probs(VP9_COMP *cpi, vp9_writer *w) {
226 VP9_COMMON *cm = &cpi->common;
229 for (k = 0; k < MBSKIP_CONTEXTS; ++k)
230 vp9_cond_prob_diff_update(w, &cm->fc.mbskip_probs[k],
231 VP9_MODE_UPDATE_PROB, cm->counts.mbskip[k]);
234 static void write_intra_mode(vp9_writer *bc, int m, const vp9_prob *p) {
235 write_token(bc, vp9_intra_mode_tree, p, vp9_intra_mode_encodings + m);
238 static void update_switchable_interp_probs(VP9_COMP *const cpi,
239 vp9_writer* const bc) {
240 VP9_COMMON *const pc = &cpi->common;
241 unsigned int branch_ct[VP9_SWITCHABLE_FILTERS + 1]
242 [VP9_SWITCHABLE_FILTERS - 1][2];
243 vp9_prob new_prob[VP9_SWITCHABLE_FILTERS + 1][VP9_SWITCHABLE_FILTERS - 1];
245 for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
246 vp9_tree_probs_from_distribution(
247 vp9_switchable_interp_tree,
248 new_prob[j], branch_ct[j],
249 pc->counts.switchable_interp[j], 0);
251 for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j) {
252 for (i = 0; i < VP9_SWITCHABLE_FILTERS - 1; ++i) {
253 vp9_cond_prob_diff_update(bc, &pc->fc.switchable_interp_prob[j][i],
254 VP9_MODE_UPDATE_PROB, branch_ct[j][i]);
258 if (!cpi->dummy_packing)
259 update_switchable_interp_stats(pc);
263 static void update_inter_mode_probs(VP9_COMMON *pc, vp9_writer* const bc) {
266 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
267 unsigned int branch_ct[VP9_INTER_MODES - 1][2];
268 vp9_prob new_prob[VP9_INTER_MODES - 1];
270 vp9_tree_probs_from_distribution(vp9_inter_mode_tree,
272 pc->counts.inter_mode[i], NEARESTMV);
274 for (j = 0; j < VP9_INTER_MODES - 1; ++j)
275 vp9_cond_prob_diff_update(bc, &pc->fc.inter_mode_probs[i][j],
276 VP9_MODE_UPDATE_PROB, branch_ct[j]);
280 static void pack_mb_tokens(vp9_writer* const bc,
282 const TOKENEXTRA *const stop) {
286 const int t = p->token;
287 const struct vp9_token *const a = vp9_coef_encodings + t;
288 const vp9_extra_bit *const b = vp9_extra_bits + t;
293 vp9_prob probs[ENTROPY_NODES];
295 if (t == EOSB_TOKEN) {
299 if (t >= TWO_TOKEN) {
300 vp9_model_to_full_probs(p->context_tree, probs);
303 pp = p->context_tree;
307 /* skip one or two nodes */
308 if (p->skip_eob_node) {
309 n -= p->skip_eob_node;
310 i = 2 * p->skip_eob_node;
314 const int bb = (v >> --n) & 1;
315 vp9_write(bc, bb, pp[i >> 1]);
316 i = vp9_coef_tree[i + bb];
320 const int e = p->extra, l = b->len;
323 const unsigned char *pb = b->prob;
325 int n = l; /* number of bits in v, assumed nonzero */
329 const int bb = (v >> --n) & 1;
330 vp9_write(bc, bb, pb[i >> 1]);
335 vp9_write_bit(bc, e & 1);
343 static void write_sb_mv_ref(vp9_writer *w, MB_PREDICTION_MODE mode,
345 assert(is_inter_mode(mode));
346 write_token(w, vp9_inter_mode_tree, p,
347 &vp9_inter_mode_encodings[mode - NEARESTMV]);
351 static void write_segment_id(vp9_writer *w, const struct segmentation *seg,
353 if (seg->enabled && seg->update_map)
354 treed_write(w, vp9_segment_tree, seg->tree_probs, segment_id, 3);
357 // This function encodes the reference frame
358 static void encode_ref_frame(VP9_COMP *cpi, vp9_writer *bc) {
359 VP9_COMMON *const pc = &cpi->common;
360 MACROBLOCK *const x = &cpi->mb;
361 MACROBLOCKD *const xd = &x->e_mbd;
362 MB_MODE_INFO *mi = &xd->mode_info_context->mbmi;
363 const int segment_id = mi->segment_id;
364 int seg_ref_active = vp9_segfeature_active(&xd->seg, segment_id,
366 // If segment level coding of this signal is disabled...
367 // or the segment allows multiple reference frame options
368 if (!seg_ref_active) {
369 // does the feature use compound prediction or not
370 // (if not specified at the frame/segment level)
371 if (pc->comp_pred_mode == HYBRID_PREDICTION) {
372 vp9_write(bc, mi->ref_frame[1] > INTRA_FRAME,
373 vp9_get_pred_prob_comp_inter_inter(pc, xd));
375 assert((mi->ref_frame[1] <= INTRA_FRAME) ==
376 (pc->comp_pred_mode == SINGLE_PREDICTION_ONLY));
379 if (mi->ref_frame[1] > INTRA_FRAME) {
380 vp9_write(bc, mi->ref_frame[0] == GOLDEN_FRAME,
381 vp9_get_pred_prob_comp_ref_p(pc, xd));
383 vp9_write(bc, mi->ref_frame[0] != LAST_FRAME,
384 vp9_get_pred_prob_single_ref_p1(pc, xd));
385 if (mi->ref_frame[0] != LAST_FRAME)
386 vp9_write(bc, mi->ref_frame[0] != GOLDEN_FRAME,
387 vp9_get_pred_prob_single_ref_p2(pc, xd));
390 assert(mi->ref_frame[1] <= INTRA_FRAME);
391 assert(vp9_get_segdata(&xd->seg, segment_id, SEG_LVL_REF_FRAME) ==
395 // if using the prediction mdoel we have nothing further to do because
396 // the reference frame is fully coded by the segment
399 static void pack_inter_mode_mvs(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc) {
400 VP9_COMMON *const pc = &cpi->common;
401 const nmv_context *nmvc = &pc->fc.nmvc;
402 MACROBLOCK *const x = &cpi->mb;
403 MACROBLOCKD *const xd = &x->e_mbd;
404 struct segmentation *seg = &xd->seg;
405 MB_MODE_INFO *const mi = &m->mbmi;
406 const MV_REFERENCE_FRAME rf = mi->ref_frame[0];
407 const MB_PREDICTION_MODE mode = mi->mode;
408 const int segment_id = mi->segment_id;
410 const BLOCK_SIZE_TYPE bsize = mi->sb_type;
411 const int allow_hp = xd->allow_high_precision_mv;
413 x->partition_info = x->pi + (m - pc->mi);
419 if (seg->update_map) {
420 if (seg->temporal_update) {
421 const int pred_flag = mi->seg_id_predicted;
422 vp9_prob pred_prob = vp9_get_pred_prob_seg_id(xd);
423 vp9_write(bc, pred_flag, pred_prob);
425 write_segment_id(bc, seg, segment_id);
427 write_segment_id(bc, seg, segment_id);
431 skip_coeff = write_skip_coeff(cpi, segment_id, m, bc);
433 if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME))
434 vp9_write(bc, rf != INTRA_FRAME,
435 vp9_get_pred_prob_intra_inter(pc, xd));
437 if (bsize >= BLOCK_SIZE_SB8X8 && pc->tx_mode == TX_MODE_SELECT &&
438 !(rf != INTRA_FRAME &&
439 (skip_coeff || vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)))) {
440 write_selected_tx_size(cpi, mi->txfm_size, bsize, bc);
443 if (rf == INTRA_FRAME) {
448 if (bsize >= BLOCK_SIZE_SB8X8) {
449 write_intra_mode(bc, mode, pc->fc.y_mode_prob[size_group_lookup[bsize]]);
452 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
453 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
454 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
455 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
456 const MB_PREDICTION_MODE bm = m->bmi[idy * 2 + idx].as_mode;
457 write_intra_mode(bc, bm, pc->fc.y_mode_prob[0]);
461 write_intra_mode(bc, mi->uv_mode, pc->fc.uv_mode_prob[mode]);
464 encode_ref_frame(cpi, bc);
465 mv_ref_p = cpi->common.fc.inter_mode_probs[mi->mb_mode_context[rf]];
471 // If segment skip is not enabled code the mode.
472 if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)) {
473 if (bsize >= BLOCK_SIZE_SB8X8) {
474 write_sb_mv_ref(bc, mode, mv_ref_p);
475 ++pc->counts.inter_mode[mi->mb_mode_context[rf]]
476 [inter_mode_offset(mode)];
480 if (cpi->common.mcomp_filter_type == SWITCHABLE) {
481 write_token(bc, vp9_switchable_interp_tree,
482 vp9_get_pred_probs_switchable_interp(&cpi->common, xd),
483 vp9_switchable_interp_encodings +
484 vp9_switchable_interp_map[mi->interp_filter]);
486 assert(mi->interp_filter == cpi->common.mcomp_filter_type);
489 if (bsize < BLOCK_SIZE_SB8X8) {
491 MB_PREDICTION_MODE blockmode;
493 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
494 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
496 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
497 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
499 blockmode = x->partition_info->bmi[j].mode;
500 blockmv = m->bmi[j].as_mv[0];
501 write_sb_mv_ref(bc, blockmode, mv_ref_p);
502 ++pc->counts.inter_mode[mi->mb_mode_context[rf]]
503 [inter_mode_offset(blockmode)];
505 if (blockmode == NEWMV) {
509 vp9_encode_mv(cpi, bc, &blockmv.as_mv, &mi->best_mv.as_mv,
512 if (mi->ref_frame[1] > INTRA_FRAME)
513 vp9_encode_mv(cpi, bc,
514 &m->bmi[j].as_mv[1].as_mv,
515 &mi->best_second_mv.as_mv,
520 } else if (mode == NEWMV) {
524 vp9_encode_mv(cpi, bc, &mi->mv[0].as_mv, &mi->best_mv.as_mv,
527 if (mi->ref_frame[1] > INTRA_FRAME)
528 vp9_encode_mv(cpi, bc, &mi->mv[1].as_mv, &mi->best_second_mv.as_mv,
534 static void write_mb_modes_kf(const VP9_COMP *cpi, MODE_INFO *m,
536 const VP9_COMMON *const c = &cpi->common;
537 const MACROBLOCKD *const xd = &cpi->mb.e_mbd;
538 const int ym = m->mbmi.mode;
539 const int mis = c->mode_info_stride;
540 const int segment_id = m->mbmi.segment_id;
542 if (xd->seg.update_map)
543 write_segment_id(bc, &xd->seg, m->mbmi.segment_id);
545 write_skip_coeff(cpi, segment_id, m, bc);
547 if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8 && c->tx_mode == TX_MODE_SELECT)
548 write_selected_tx_size(cpi, m->mbmi.txfm_size, m->mbmi.sb_type, bc);
550 if (m->mbmi.sb_type >= BLOCK_SIZE_SB8X8) {
551 const MB_PREDICTION_MODE A = above_block_mode(m, 0, mis);
552 const MB_PREDICTION_MODE L = xd->left_available ?
553 left_block_mode(m, 0) : DC_PRED;
554 write_intra_mode(bc, ym, vp9_kf_y_mode_prob[A][L]);
557 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[m->mbmi.sb_type];
558 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[m->mbmi.sb_type];
559 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
560 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
561 const int i = idy * 2 + idx;
562 const MB_PREDICTION_MODE A = above_block_mode(m, i, mis);
563 const MB_PREDICTION_MODE L = (xd->left_available || idx) ?
564 left_block_mode(m, i) : DC_PRED;
565 const int bm = m->bmi[i].as_mode;
567 ++intra_mode_stats[A][L][bm];
569 write_intra_mode(bc, bm, vp9_kf_y_mode_prob[A][L]);
574 write_intra_mode(bc, m->mbmi.uv_mode, vp9_kf_uv_mode_prob[ym]);
577 static void write_modes_b(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
578 TOKENEXTRA **tok, TOKENEXTRA *tok_end,
579 int mi_row, int mi_col) {
580 VP9_COMMON *const cm = &cpi->common;
581 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
583 if (m->mbmi.sb_type < BLOCK_SIZE_SB8X8)
584 if (xd->ab_index > 0)
586 xd->mode_info_context = m;
587 set_mi_row_col(&cpi->common, xd, mi_row,
588 1 << mi_height_log2(m->mbmi.sb_type),
589 mi_col, 1 << mi_width_log2(m->mbmi.sb_type));
590 if ((cm->frame_type == KEY_FRAME) || cm->intra_only) {
591 write_mb_modes_kf(cpi, m, bc);
596 pack_inter_mode_mvs(cpi, m, bc);
602 assert(*tok < tok_end);
603 pack_mb_tokens(bc, tok, tok_end);
606 static void write_modes_sb(VP9_COMP *cpi, MODE_INFO *m, vp9_writer *bc,
607 TOKENEXTRA **tok, TOKENEXTRA *tok_end,
608 int mi_row, int mi_col,
609 BLOCK_SIZE_TYPE bsize) {
610 VP9_COMMON *const cm = &cpi->common;
611 MACROBLOCKD *xd = &cpi->mb.e_mbd;
612 const int mis = cm->mode_info_stride;
613 int bsl = b_width_log2(bsize);
614 int bs = (1 << bsl) / 4; // mode_info step for subsize
616 PARTITION_TYPE partition = PARTITION_NONE;
617 BLOCK_SIZE_TYPE subsize;
619 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
622 partition = partition_lookup[bsl][m->mbmi.sb_type];
624 if (bsize < BLOCK_SIZE_SB8X8)
625 if (xd->ab_index > 0)
628 if (bsize >= BLOCK_SIZE_SB8X8) {
630 const int idx = check_bsize_coverage(cm, mi_row, mi_col, bsize);
631 set_partition_seg_context(cm, xd, mi_row, mi_col);
632 pl = partition_plane_context(xd, bsize);
633 // encode the partition information
635 write_token(bc, vp9_partition_tree,
636 cm->fc.partition_prob[cm->frame_type][pl],
637 vp9_partition_encodings + partition);
639 vp9_write(bc, partition == PARTITION_SPLIT,
640 cm->fc.partition_prob[cm->frame_type][pl][idx]);
643 subsize = get_subsize(bsize, partition);
644 *(get_sb_index(xd, subsize)) = 0;
648 write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
651 write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
652 *(get_sb_index(xd, subsize)) = 1;
653 if ((mi_row + bs) < cm->mi_rows)
654 write_modes_b(cpi, m + bs * mis, bc, tok, tok_end, mi_row + bs, mi_col);
657 write_modes_b(cpi, m, bc, tok, tok_end, mi_row, mi_col);
658 *(get_sb_index(xd, subsize)) = 1;
659 if ((mi_col + bs) < cm->mi_cols)
660 write_modes_b(cpi, m + bs, bc, tok, tok_end, mi_row, mi_col + bs);
662 case PARTITION_SPLIT:
663 for (n = 0; n < 4; n++) {
664 int j = n >> 1, i = n & 0x01;
665 *(get_sb_index(xd, subsize)) = n;
666 write_modes_sb(cpi, m + j * bs * mis + i * bs, bc, tok, tok_end,
667 mi_row + j * bs, mi_col + i * bs, subsize);
674 // update partition context
675 if (bsize >= BLOCK_SIZE_SB8X8 &&
676 (bsize == BLOCK_SIZE_SB8X8 || partition != PARTITION_SPLIT)) {
677 set_partition_seg_context(cm, xd, mi_row, mi_col);
678 update_partition_context(xd, subsize, bsize);
682 static void write_modes(VP9_COMP *cpi, vp9_writer* const bc,
683 TOKENEXTRA **tok, TOKENEXTRA *tok_end) {
684 VP9_COMMON *const c = &cpi->common;
685 const int mis = c->mode_info_stride;
686 MODE_INFO *m, *m_ptr = c->mi;
689 m_ptr += c->cur_tile_mi_col_start + c->cur_tile_mi_row_start * mis;
691 for (mi_row = c->cur_tile_mi_row_start; mi_row < c->cur_tile_mi_row_end;
692 mi_row += 8, m_ptr += 8 * mis) {
694 vp9_zero(c->left_seg_context);
695 for (mi_col = c->cur_tile_mi_col_start; mi_col < c->cur_tile_mi_col_end;
696 mi_col += MI_BLOCK_SIZE, m += MI_BLOCK_SIZE)
697 write_modes_sb(cpi, m, bc, tok, tok_end, mi_row, mi_col, BLOCK_64X64);
701 /* This function is used for debugging probability trees. */
702 static void print_prob_tree(vp9_coeff_probs *coef_probs, int block_types) {
703 /* print coef probability tree */
705 FILE *f = fopen("enc_tree_probs.txt", "a");
707 for (i = 0; i < block_types; i++) {
709 for (j = 0; j < REF_TYPES; ++j) {
711 for (k = 0; k < COEF_BANDS; k++) {
713 for (l = 0; l < PREV_COEF_CONTEXTS; l++) {
715 for (m = 0; m < ENTROPY_NODES; m++) {
717 (unsigned int)(coef_probs[i][j][k][l][m]));
730 static void build_tree_distribution(VP9_COMP *cpi, TX_SIZE tx_size) {
731 vp9_coeff_probs_model *coef_probs = cpi->frame_coef_probs[tx_size];
732 vp9_coeff_count *coef_counts = cpi->coef_counts[tx_size];
733 unsigned int (*eob_branch_ct)[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] =
734 cpi->common.counts.eob_branch[tx_size];
735 vp9_coeff_stats *coef_branch_ct = cpi->frame_branch_ct[tx_size];
736 vp9_prob full_probs[ENTROPY_NODES];
739 for (i = 0; i < BLOCK_TYPES; ++i) {
740 for (j = 0; j < REF_TYPES; ++j) {
741 for (k = 0; k < COEF_BANDS; ++k) {
742 for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
743 if (l >= 3 && k == 0)
745 vp9_tree_probs_from_distribution(vp9_coef_tree,
747 coef_branch_ct[i][j][k][l],
748 coef_counts[i][j][k][l], 0);
749 vpx_memcpy(coef_probs[i][j][k][l], full_probs,
750 sizeof(vp9_prob) * UNCONSTRAINED_NODES);
751 coef_branch_ct[i][j][k][l][0][1] = eob_branch_ct[i][j][k][l] -
752 coef_branch_ct[i][j][k][l][0][0];
753 coef_probs[i][j][k][l][0] =
754 get_binary_prob(coef_branch_ct[i][j][k][l][0][0],
755 coef_branch_ct[i][j][k][l][0][1]);
757 if (!cpi->dummy_packing) {
759 for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
760 context_counters[tx_size][i][j][k][l][t] +=
761 coef_counts[i][j][k][l][t];
762 context_counters[tx_size][i][j][k][l][MAX_ENTROPY_TOKENS] +=
763 eob_branch_ct[i][j][k][l];
772 static void build_coeff_contexts(VP9_COMP *cpi) {
774 for (t = TX_4X4; t <= TX_32X32; t++)
775 build_tree_distribution(cpi, t);
778 static void update_coef_probs_common(vp9_writer* const bc, VP9_COMP *cpi,
780 vp9_coeff_probs_model *new_frame_coef_probs = cpi->frame_coef_probs[tx_size];
781 vp9_coeff_probs_model *old_frame_coef_probs =
782 cpi->common.fc.coef_probs[tx_size];
783 vp9_coeff_stats *frame_branch_ct = cpi->frame_branch_ct[tx_size];
785 int update[2] = {0, 0};
788 const int entropy_nodes_update = UNCONSTRAINED_NODES;
790 const int tstart = 0;
791 /* dry run to see if there is any udpate at all needed */
793 for (i = 0; i < BLOCK_TYPES; ++i) {
794 for (j = 0; j < REF_TYPES; ++j) {
795 for (k = 0; k < COEF_BANDS; ++k) {
796 // int prev_coef_savings[ENTROPY_NODES] = {0};
797 for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
798 for (t = tstart; t < entropy_nodes_update; ++t) {
799 vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
800 const vp9_prob oldp = old_frame_coef_probs[i][j][k][l][t];
801 const vp9_prob upd = VP9_COEF_UPDATE_PROB;
805 if (l >= 3 && k == 0)
808 s = vp9_prob_diff_update_savings_search_model(
809 frame_branch_ct[i][j][k][l][0],
810 old_frame_coef_probs[i][j][k][l], &newp, upd, i, j);
812 s = vp9_prob_diff_update_savings_search(
813 frame_branch_ct[i][j][k][l][t], oldp, &newp, upd);
814 if (s > 0 && newp != oldp)
817 savings += s - (int)(vp9_cost_zero(upd));
819 savings -= (int)(vp9_cost_zero(upd));
827 // printf("Update %d %d, savings %d\n", update[0], update[1], savings);
828 /* Is coef updated at all */
829 if (update[1] == 0 || savings < 0) {
830 vp9_write_bit(bc, 0);
833 vp9_write_bit(bc, 1);
834 for (i = 0; i < BLOCK_TYPES; ++i) {
835 for (j = 0; j < REF_TYPES; ++j) {
836 for (k = 0; k < COEF_BANDS; ++k) {
837 // int prev_coef_savings[ENTROPY_NODES] = {0};
838 for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
839 // calc probs and branch cts for this frame only
840 for (t = tstart; t < entropy_nodes_update; ++t) {
841 vp9_prob newp = new_frame_coef_probs[i][j][k][l][t];
842 vp9_prob *oldp = old_frame_coef_probs[i][j][k][l] + t;
843 const vp9_prob upd = VP9_COEF_UPDATE_PROB;
846 if (l >= 3 && k == 0)
849 s = vp9_prob_diff_update_savings_search_model(
850 frame_branch_ct[i][j][k][l][0],
851 old_frame_coef_probs[i][j][k][l], &newp, upd, i, j);
853 s = vp9_prob_diff_update_savings_search(
854 frame_branch_ct[i][j][k][l][t],
856 if (s > 0 && newp != *oldp)
858 vp9_write(bc, u, upd);
860 if (!cpi->dummy_packing)
861 ++tree_update_hist[tx_size][i][j][k][l][t][u];
864 /* send/use new probability */
865 vp9_write_prob_diff_update(bc, newp, *oldp);
875 static void update_coef_probs(VP9_COMP* const cpi, vp9_writer* const bc) {
876 const TX_MODE tx_mode = cpi->common.tx_mode;
878 vp9_clear_system_state();
880 // Build the cofficient contexts based on counts collected in encode loop
881 build_coeff_contexts(cpi);
883 update_coef_probs_common(bc, cpi, TX_4X4);
885 // do not do this if not even allowed
886 if (tx_mode > ONLY_4X4)
887 update_coef_probs_common(bc, cpi, TX_8X8);
889 if (tx_mode > ALLOW_8X8)
890 update_coef_probs_common(bc, cpi, TX_16X16);
892 if (tx_mode > ALLOW_16X16)
893 update_coef_probs_common(bc, cpi, TX_32X32);
896 static void encode_loopfilter(struct loopfilter *lf,
897 struct vp9_write_bit_buffer *wb) {
900 // Encode the loop filter level and type
901 vp9_wb_write_literal(wb, lf->filter_level, 6);
902 vp9_wb_write_literal(wb, lf->sharpness_level, 3);
904 // Write out loop filter deltas applied at the MB level based on mode or
905 // ref frame (if they are enabled).
906 vp9_wb_write_bit(wb, lf->mode_ref_delta_enabled);
908 if (lf->mode_ref_delta_enabled) {
909 // Do the deltas need to be updated
910 vp9_wb_write_bit(wb, lf->mode_ref_delta_update);
911 if (lf->mode_ref_delta_update) {
913 for (i = 0; i < MAX_REF_LF_DELTAS; i++) {
914 const int delta = lf->ref_deltas[i];
917 if (delta != lf->last_ref_deltas[i]) {
918 lf->last_ref_deltas[i] = delta;
919 vp9_wb_write_bit(wb, 1);
922 vp9_wb_write_literal(wb, abs(delta) & 0x3F, 6);
923 vp9_wb_write_bit(wb, delta < 0);
925 vp9_wb_write_bit(wb, 0);
930 for (i = 0; i < MAX_MODE_LF_DELTAS; i++) {
931 const int delta = lf->mode_deltas[i];
932 if (delta != lf->last_mode_deltas[i]) {
933 lf->last_mode_deltas[i] = delta;
934 vp9_wb_write_bit(wb, 1);
937 vp9_wb_write_literal(wb, abs(delta) & 0x3F, 6);
938 vp9_wb_write_bit(wb, delta < 0);
940 vp9_wb_write_bit(wb, 0);
947 static void write_delta_q(struct vp9_write_bit_buffer *wb, int delta_q) {
949 vp9_wb_write_bit(wb, 1);
950 vp9_wb_write_literal(wb, abs(delta_q), 4);
951 vp9_wb_write_bit(wb, delta_q < 0);
953 vp9_wb_write_bit(wb, 0);
957 static void encode_quantization(VP9_COMMON *cm,
958 struct vp9_write_bit_buffer *wb) {
959 vp9_wb_write_literal(wb, cm->base_qindex, QINDEX_BITS);
960 write_delta_q(wb, cm->y_dc_delta_q);
961 write_delta_q(wb, cm->uv_dc_delta_q);
962 write_delta_q(wb, cm->uv_ac_delta_q);
966 static void encode_segmentation(VP9_COMP *cpi,
967 struct vp9_write_bit_buffer *wb) {
970 struct segmentation *seg = &cpi->mb.e_mbd.seg;
972 vp9_wb_write_bit(wb, seg->enabled);
977 vp9_wb_write_bit(wb, seg->update_map);
978 if (seg->update_map) {
979 // Select the coding strategy (temporal or spatial)
980 vp9_choose_segmap_coding_method(cpi);
981 // Write out probabilities used to decode unpredicted macro-block segments
982 for (i = 0; i < SEG_TREE_PROBS; i++) {
983 const int prob = seg->tree_probs[i];
984 const int update = prob != MAX_PROB;
985 vp9_wb_write_bit(wb, update);
987 vp9_wb_write_literal(wb, prob, 8);
990 // Write out the chosen coding method.
991 vp9_wb_write_bit(wb, seg->temporal_update);
992 if (seg->temporal_update) {
993 for (i = 0; i < PREDICTION_PROBS; i++) {
994 const int prob = seg->pred_probs[i];
995 const int update = prob != MAX_PROB;
996 vp9_wb_write_bit(wb, update);
998 vp9_wb_write_literal(wb, prob, 8);
1003 // Segmentation data
1004 vp9_wb_write_bit(wb, seg->update_data);
1005 if (seg->update_data) {
1006 vp9_wb_write_bit(wb, seg->abs_delta);
1008 for (i = 0; i < MAX_SEGMENTS; i++) {
1009 for (j = 0; j < SEG_LVL_MAX; j++) {
1010 const int active = vp9_segfeature_active(seg, i, j);
1011 vp9_wb_write_bit(wb, active);
1013 const int data = vp9_get_segdata(seg, i, j);
1014 const int data_max = vp9_seg_feature_data_max(j);
1016 if (vp9_is_segfeature_signed(j)) {
1017 vp9_encode_unsigned_max(wb, abs(data), data_max);
1018 vp9_wb_write_bit(wb, data < 0);
1020 vp9_encode_unsigned_max(wb, data, data_max);
1029 static void encode_txfm_probs(VP9_COMP *cpi, vp9_writer *w) {
1030 VP9_COMMON *const cm = &cpi->common;
1033 vp9_write_literal(w, MIN(cm->tx_mode, ALLOW_32X32), 2);
1034 if (cm->tx_mode >= ALLOW_32X32)
1035 vp9_write_bit(w, cm->tx_mode == TX_MODE_SELECT);
1038 if (cm->tx_mode == TX_MODE_SELECT) {
1040 unsigned int ct_8x8p[TX_SIZES - 3][2];
1041 unsigned int ct_16x16p[TX_SIZES - 2][2];
1042 unsigned int ct_32x32p[TX_SIZES - 1][2];
1045 for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
1046 tx_counts_to_branch_counts_8x8(cm->counts.tx.p8x8[i],
1048 for (j = 0; j < TX_SIZES - 3; j++)
1049 vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p8x8[i][j],
1050 VP9_MODE_UPDATE_PROB, ct_8x8p[j]);
1053 for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
1054 tx_counts_to_branch_counts_16x16(cm->counts.tx.p16x16[i],
1056 for (j = 0; j < TX_SIZES - 2; j++)
1057 vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p16x16[i][j],
1058 VP9_MODE_UPDATE_PROB, ct_16x16p[j]);
1061 for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
1062 tx_counts_to_branch_counts_32x32(cm->counts.tx.p32x32[i], ct_32x32p);
1063 for (j = 0; j < TX_SIZES - 1; j++)
1064 vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p32x32[i][j],
1065 VP9_MODE_UPDATE_PROB, ct_32x32p[j]);
1068 if (!cpi->dummy_packing)
1069 update_tx_count_stats(cm);
1074 static void write_interp_filter_type(INTERPOLATIONFILTERTYPE type,
1075 struct vp9_write_bit_buffer *wb) {
1076 vp9_wb_write_bit(wb, type == SWITCHABLE);
1077 if (type != SWITCHABLE)
1078 vp9_wb_write_literal(wb, type, 2);
1081 static void fix_mcomp_filter_type(VP9_COMP *cpi) {
1082 VP9_COMMON *const cm = &cpi->common;
1084 if (cm->mcomp_filter_type == SWITCHABLE) {
1085 // Check to see if only one of the filters is actually used
1086 int count[VP9_SWITCHABLE_FILTERS];
1088 for (i = 0; i < VP9_SWITCHABLE_FILTERS; ++i) {
1090 for (j = 0; j <= VP9_SWITCHABLE_FILTERS; ++j)
1091 count[i] += cm->counts.switchable_interp[j][i];
1092 c += (count[i] > 0);
1095 // Only one filter is used. So set the filter at frame level
1096 for (i = 0; i < VP9_SWITCHABLE_FILTERS; ++i) {
1098 cm->mcomp_filter_type = vp9_switchable_interp[i];
1106 static void write_tile_info(VP9_COMMON *cm, struct vp9_write_bit_buffer *wb) {
1107 int min_log2_tile_cols, max_log2_tile_cols, ones;
1108 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1111 ones = cm->log2_tile_cols - min_log2_tile_cols;
1113 vp9_wb_write_bit(wb, 1);
1115 if (cm->log2_tile_cols < max_log2_tile_cols)
1116 vp9_wb_write_bit(wb, 0);
1119 vp9_wb_write_bit(wb, cm->log2_tile_rows != 0);
1120 if (cm->log2_tile_rows != 0)
1121 vp9_wb_write_bit(wb, cm->log2_tile_rows != 1);
1124 static int get_refresh_mask(VP9_COMP *cpi) {
1125 // Should the GF or ARF be updated using the transmitted frame or buffer
1126 #if CONFIG_MULTIPLE_ARF
1127 if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame &&
1128 !cpi->refresh_alt_ref_frame) {
1130 if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame) {
1132 // Preserve the previously existing golden frame and update the frame in
1133 // the alt ref slot instead. This is highly specific to the use of
1134 // alt-ref as a forward reference, and this needs to be generalized as
1135 // other uses are implemented (like RTC/temporal scaling)
1137 // gld_fb_idx and alt_fb_idx need to be swapped for future frames, but
1138 // that happens in vp9_onyx_if.c:update_reference_frames() so that it can
1139 // be done outside of the recode loop.
1140 return (cpi->refresh_last_frame << cpi->lst_fb_idx) |
1141 (cpi->refresh_golden_frame << cpi->alt_fb_idx);
1143 int arf_idx = cpi->alt_fb_idx;
1144 #if CONFIG_MULTIPLE_ARF
1145 // Determine which ARF buffer to use to encode this ARF frame.
1146 if (cpi->multi_arf_enabled) {
1147 int sn = cpi->sequence_number;
1148 arf_idx = (cpi->frame_coding_order[sn] < 0) ?
1149 cpi->arf_buffer_idx[sn + 1] :
1150 cpi->arf_buffer_idx[sn];
1153 return (cpi->refresh_last_frame << cpi->lst_fb_idx) |
1154 (cpi->refresh_golden_frame << cpi->gld_fb_idx) |
1155 (cpi->refresh_alt_ref_frame << arf_idx);
1159 static size_t encode_tiles(VP9_COMP *cpi, uint8_t *data_ptr) {
1160 VP9_COMMON *const cm = &cpi->common;
1161 vp9_writer residual_bc;
1163 int tile_row, tile_col;
1164 TOKENEXTRA *tok[4][1 << 6], *tok_end;
1165 size_t total_size = 0;
1166 const int tile_cols = 1 << cm->log2_tile_cols;
1167 const int tile_rows = 1 << cm->log2_tile_rows;
1169 vpx_memset(cm->above_seg_context, 0, sizeof(PARTITION_CONTEXT) *
1170 mi_cols_aligned_to_sb(cm->mi_cols));
1172 tok[0][0] = cpi->tok;
1173 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
1175 tok[tile_row][0] = tok[tile_row - 1][tile_cols - 1] +
1176 cpi->tok_count[tile_row - 1][tile_cols - 1];
1178 for (tile_col = 1; tile_col < tile_cols; tile_col++)
1179 tok[tile_row][tile_col] = tok[tile_row][tile_col - 1] +
1180 cpi->tok_count[tile_row][tile_col - 1];
1183 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
1184 vp9_get_tile_row_offsets(cm, tile_row);
1185 for (tile_col = 0; tile_col < tile_cols; tile_col++) {
1186 vp9_get_tile_col_offsets(cm, tile_col);
1187 tok_end = tok[tile_row][tile_col] + cpi->tok_count[tile_row][tile_col];
1189 if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1)
1190 vp9_start_encode(&residual_bc, data_ptr + total_size + 4);
1192 vp9_start_encode(&residual_bc, data_ptr + total_size);
1194 write_modes(cpi, &residual_bc, &tok[tile_row][tile_col], tok_end);
1195 assert(tok[tile_row][tile_col] == tok_end);
1196 vp9_stop_encode(&residual_bc);
1197 if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) {
1198 // size of this tile
1199 write_be32(data_ptr + total_size, residual_bc.pos);
1203 total_size += residual_bc.pos;
1210 static void write_display_size(VP9_COMP *cpi, struct vp9_write_bit_buffer *wb) {
1211 VP9_COMMON *const cm = &cpi->common;
1213 const int scaling_active = cm->width != cm->display_width ||
1214 cm->height != cm->display_height;
1215 vp9_wb_write_bit(wb, scaling_active);
1216 if (scaling_active) {
1217 vp9_wb_write_literal(wb, cm->display_width - 1, 16);
1218 vp9_wb_write_literal(wb, cm->display_height - 1, 16);
1222 static void write_frame_size(VP9_COMP *cpi,
1223 struct vp9_write_bit_buffer *wb) {
1224 VP9_COMMON *const cm = &cpi->common;
1225 vp9_wb_write_literal(wb, cm->width - 1, 16);
1226 vp9_wb_write_literal(wb, cm->height - 1, 16);
1228 write_display_size(cpi, wb);
1231 static void write_frame_size_with_refs(VP9_COMP *cpi,
1232 struct vp9_write_bit_buffer *wb) {
1233 VP9_COMMON *const cm = &cpi->common;
1234 int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx,
1238 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
1239 YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->ref_frame_map[refs[i]]];
1240 found = cm->width == cfg->y_crop_width &&
1241 cm->height == cfg->y_crop_height;
1242 vp9_wb_write_bit(wb, found);
1248 vp9_wb_write_literal(wb, cm->width - 1, 16);
1249 vp9_wb_write_literal(wb, cm->height - 1, 16);
1252 write_display_size(cpi, wb);
1255 static void write_sync_code(struct vp9_write_bit_buffer *wb) {
1256 vp9_wb_write_literal(wb, SYNC_CODE_0, 8);
1257 vp9_wb_write_literal(wb, SYNC_CODE_1, 8);
1258 vp9_wb_write_literal(wb, SYNC_CODE_2, 8);
1261 static void write_uncompressed_header(VP9_COMP *cpi,
1262 struct vp9_write_bit_buffer *wb) {
1263 VP9_COMMON *const cm = &cpi->common;
1264 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1266 // frame marker bits
1267 vp9_wb_write_literal(wb, 0x2, 2);
1269 // bitstream version.
1270 // 00 - profile 0. 4:2:0 only
1271 // 10 - profile 1. adds 4:4:4, 4:2:2, alpha
1272 vp9_wb_write_bit(wb, cm->version);
1273 vp9_wb_write_bit(wb, 0);
1275 vp9_wb_write_bit(wb, 0);
1276 vp9_wb_write_bit(wb, cm->frame_type);
1277 vp9_wb_write_bit(wb, cm->show_frame);
1278 vp9_wb_write_bit(wb, cm->error_resilient_mode);
1280 if (cm->frame_type == KEY_FRAME) {
1281 write_sync_code(wb);
1291 vp9_wb_write_literal(wb, 0, 3);
1292 if (1 /* colorspace != sRGB */) {
1293 vp9_wb_write_bit(wb, 0); // 0: [16, 235] (i.e. xvYCC), 1: [0, 255]
1294 if (cm->version == 1) {
1295 vp9_wb_write_bit(wb, cm->subsampling_x);
1296 vp9_wb_write_bit(wb, cm->subsampling_y);
1297 vp9_wb_write_bit(wb, 0); // has extra plane
1300 assert(cm->version == 1);
1301 vp9_wb_write_bit(wb, 0); // has extra plane
1304 write_frame_size(cpi, wb);
1306 const int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx,
1308 if (!cm->show_frame)
1309 vp9_wb_write_bit(wb, cm->intra_only);
1311 if (!cm->error_resilient_mode)
1312 vp9_wb_write_literal(wb, cm->reset_frame_context, 2);
1314 if (cm->intra_only) {
1315 write_sync_code(wb);
1317 vp9_wb_write_literal(wb, get_refresh_mask(cpi), NUM_REF_FRAMES);
1318 write_frame_size(cpi, wb);
1321 vp9_wb_write_literal(wb, get_refresh_mask(cpi), NUM_REF_FRAMES);
1322 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
1323 vp9_wb_write_literal(wb, refs[i], NUM_REF_FRAMES_LOG2);
1324 vp9_wb_write_bit(wb, cm->ref_frame_sign_bias[LAST_FRAME + i]);
1327 write_frame_size_with_refs(cpi, wb);
1329 vp9_wb_write_bit(wb, xd->allow_high_precision_mv);
1331 fix_mcomp_filter_type(cpi);
1332 write_interp_filter_type(cm->mcomp_filter_type, wb);
1336 if (!cm->error_resilient_mode) {
1337 vp9_wb_write_bit(wb, cm->refresh_frame_context);
1338 vp9_wb_write_bit(wb, cm->frame_parallel_decoding_mode);
1341 vp9_wb_write_literal(wb, cm->frame_context_idx, NUM_FRAME_CONTEXTS_LOG2);
1343 encode_loopfilter(&xd->lf, wb);
1344 encode_quantization(cm, wb);
1345 encode_segmentation(cpi, wb);
1347 write_tile_info(cm, wb);
1350 static size_t write_compressed_header(VP9_COMP *cpi, uint8_t *data) {
1351 VP9_COMMON *const cm = &cpi->common;
1352 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1353 FRAME_CONTEXT *const fc = &cm->fc;
1354 vp9_writer header_bc;
1356 vp9_start_encode(&header_bc, data);
1359 cm->tx_mode = ONLY_4X4;
1361 encode_txfm_probs(cpi, &header_bc);
1363 update_coef_probs(cpi, &header_bc);
1365 #ifdef ENTROPY_STATS
1369 vp9_update_skip_probs(cpi, &header_bc);
1371 if (cm->frame_type != KEY_FRAME) {
1373 #ifdef ENTROPY_STATS
1377 update_inter_mode_probs(cm, &header_bc);
1378 vp9_zero(cm->counts.inter_mode);
1380 if (cm->mcomp_filter_type == SWITCHABLE)
1381 update_switchable_interp_probs(cpi, &header_bc);
1383 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
1384 vp9_cond_prob_diff_update(&header_bc, &fc->intra_inter_prob[i],
1385 VP9_MODE_UPDATE_PROB,
1386 cpi->intra_inter_count[i]);
1388 if (cm->allow_comp_inter_inter) {
1389 const int comp_pred_mode = cpi->common.comp_pred_mode;
1390 const int use_compound_pred = comp_pred_mode != SINGLE_PREDICTION_ONLY;
1391 const int use_hybrid_pred = comp_pred_mode == HYBRID_PREDICTION;
1393 vp9_write_bit(&header_bc, use_compound_pred);
1394 if (use_compound_pred) {
1395 vp9_write_bit(&header_bc, use_hybrid_pred);
1396 if (use_hybrid_pred)
1397 for (i = 0; i < COMP_INTER_CONTEXTS; i++)
1398 vp9_cond_prob_diff_update(&header_bc, &fc->comp_inter_prob[i],
1399 VP9_MODE_UPDATE_PROB,
1400 cpi->comp_inter_count[i]);
1404 if (cm->comp_pred_mode != COMP_PREDICTION_ONLY) {
1405 for (i = 0; i < REF_CONTEXTS; i++) {
1406 vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][0],
1407 VP9_MODE_UPDATE_PROB,
1408 cpi->single_ref_count[i][0]);
1409 vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][1],
1410 VP9_MODE_UPDATE_PROB,
1411 cpi->single_ref_count[i][1]);
1415 if (cm->comp_pred_mode != SINGLE_PREDICTION_ONLY)
1416 for (i = 0; i < REF_CONTEXTS; i++)
1417 vp9_cond_prob_diff_update(&header_bc, &fc->comp_ref_prob[i],
1418 VP9_MODE_UPDATE_PROB,
1419 cpi->comp_ref_count[i]);
1421 update_mbintra_mode_probs(cpi, &header_bc);
1423 for (i = 0; i < NUM_PARTITION_CONTEXTS; ++i) {
1424 vp9_prob pnew[PARTITION_TYPES - 1];
1425 unsigned int bct[PARTITION_TYPES - 1][2];
1426 update_mode(&header_bc, PARTITION_TYPES,
1427 vp9_partition_tree, pnew,
1428 fc->partition_prob[cm->frame_type][i], bct,
1429 (unsigned int *)cpi->partition_count[i]);
1432 vp9_write_nmv_probs(cpi, xd->allow_high_precision_mv, &header_bc);
1435 vp9_stop_encode(&header_bc);
1436 assert(header_bc.pos <= 0xffff);
1438 return header_bc.pos;
1441 void vp9_pack_bitstream(VP9_COMP *cpi, uint8_t *dest, unsigned long *size) {
1442 uint8_t *data = dest;
1443 size_t first_part_size;
1444 struct vp9_write_bit_buffer wb = {data, 0};
1445 struct vp9_write_bit_buffer saved_wb;
1447 write_uncompressed_header(cpi, &wb);
1449 vp9_wb_write_literal(&wb, 0, 16); // don't know in advance first part. size
1451 data += vp9_rb_bytes_written(&wb);
1453 vp9_compute_update_table();
1455 #ifdef ENTROPY_STATS
1456 if (pc->frame_type == INTER_FRAME)
1462 vp9_clear_system_state(); // __asm emms;
1464 first_part_size = write_compressed_header(cpi, data);
1465 data += first_part_size;
1466 vp9_wb_write_literal(&saved_wb, first_part_size, 16);
1468 data += encode_tiles(cpi, data);
1470 *size = data - dest;
1473 #ifdef ENTROPY_STATS
1474 static void print_tree_update_for_type(FILE *f,
1475 vp9_coeff_stats *tree_update_hist,
1476 int block_types, const char *header) {
1479 fprintf(f, "const vp9_coeff_prob %s = {\n", header);
1480 for (i = 0; i < block_types; i++) {
1481 fprintf(f, " { \n");
1482 for (j = 0; j < REF_TYPES; j++) {
1483 fprintf(f, " { \n");
1484 for (k = 0; k < COEF_BANDS; k++) {
1486 for (l = 0; l < PREV_COEF_CONTEXTS; l++) {
1488 for (m = 0; m < ENTROPY_NODES; m++) {
1490 get_binary_prob(tree_update_hist[i][j][k][l][m][0],
1491 tree_update_hist[i][j][k][l][m][1]));
1497 fprintf(f, " },\n");
1499 fprintf(f, " },\n");
1504 void print_tree_update_probs() {
1505 FILE *f = fopen("coefupdprob.h", "w");
1506 fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
1508 print_tree_update_for_type(f, tree_update_hist[TX_4X4], BLOCK_TYPES,
1509 "vp9_coef_update_probs_4x4[BLOCK_TYPES]");
1510 print_tree_update_for_type(f, tree_update_hist[TX_8X8], BLOCK_TYPES,
1511 "vp9_coef_update_probs_8x8[BLOCK_TYPES]");
1512 print_tree_update_for_type(f, tree_update_hist[TX_16X16], BLOCK_TYPES,
1513 "vp9_coef_update_probs_16x16[BLOCK_TYPES]");
1514 print_tree_update_for_type(f, tree_update_hist[TX_32X32], BLOCK_TYPES,
1515 "vp9_coef_update_probs_32x32[BLOCK_TYPES]");
1518 f = fopen("treeupdate.bin", "wb");
1519 fwrite(tree_update_hist, sizeof(tree_update_hist), 1, f);