vp9_bitstream.c revision b08e2e23eec181e9951df33cd704ac294c5407b6
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#include "vpx_ports/mem_ops.h" 18 19#include "vp9/common/vp9_entropy.h" 20#include "vp9/common/vp9_entropymode.h" 21#include "vp9/common/vp9_entropymv.h" 22#include "vp9/common/vp9_mvref_common.h" 23#include "vp9/common/vp9_pragmas.h" 24#include "vp9/common/vp9_pred_common.h" 25#include "vp9/common/vp9_seg_common.h" 26#include "vp9/common/vp9_systemdependent.h" 27#include "vp9/common/vp9_tile_common.h" 28 29#include "vp9/encoder/vp9_cost.h" 30#include "vp9/encoder/vp9_bitstream.h" 31#include "vp9/encoder/vp9_encodemv.h" 32#include "vp9/encoder/vp9_mcomp.h" 33#include "vp9/encoder/vp9_segmentation.h" 34#include "vp9/encoder/vp9_subexp.h" 35#include "vp9/encoder/vp9_tokenize.h" 36#include "vp9/encoder/vp9_write_bit_buffer.h" 37 38static struct vp9_token intra_mode_encodings[INTRA_MODES]; 39static struct vp9_token switchable_interp_encodings[SWITCHABLE_FILTERS]; 40static struct vp9_token partition_encodings[PARTITION_TYPES]; 41static struct vp9_token inter_mode_encodings[INTER_MODES]; 42 43void vp9_entropy_mode_init() { 44 vp9_tokens_from_tree(intra_mode_encodings, vp9_intra_mode_tree); 45 vp9_tokens_from_tree(switchable_interp_encodings, vp9_switchable_interp_tree); 46 vp9_tokens_from_tree(partition_encodings, vp9_partition_tree); 47 vp9_tokens_from_tree(inter_mode_encodings, vp9_inter_mode_tree); 48} 49 50static void write_intra_mode(vp9_writer *w, MB_PREDICTION_MODE mode, 51 const vp9_prob *probs) { 52 vp9_write_token(w, vp9_intra_mode_tree, probs, &intra_mode_encodings[mode]); 53} 54 55static void write_inter_mode(vp9_writer *w, MB_PREDICTION_MODE mode, 56 const vp9_prob *probs) { 57 assert(is_inter_mode(mode)); 58 vp9_write_token(w, vp9_inter_mode_tree, probs, 59 &inter_mode_encodings[INTER_OFFSET(mode)]); 60} 61 62static void encode_unsigned_max(struct vp9_write_bit_buffer *wb, 63 int data, int max) { 64 vp9_wb_write_literal(wb, data, get_unsigned_bits(max)); 65} 66 67static void prob_diff_update(const vp9_tree_index *tree, 68 vp9_prob probs[/*n - 1*/], 69 const unsigned int counts[/*n - 1*/], 70 int n, vp9_writer *w) { 71 int i; 72 unsigned int branch_ct[32][2]; 73 74 // Assuming max number of probabilities <= 32 75 assert(n <= 32); 76 77 vp9_tree_probs_from_distribution(tree, branch_ct, counts); 78 for (i = 0; i < n - 1; ++i) 79 vp9_cond_prob_diff_update(w, &probs[i], branch_ct[i]); 80} 81 82static void write_selected_tx_size(const VP9_COMP *cpi, 83 TX_SIZE tx_size, BLOCK_SIZE bsize, 84 vp9_writer *w) { 85 const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; 86 const MACROBLOCKD *const xd = &cpi->mb.e_mbd; 87 const vp9_prob *const tx_probs = get_tx_probs2(max_tx_size, xd, 88 &cpi->common.fc.tx_probs); 89 vp9_write(w, tx_size != TX_4X4, tx_probs[0]); 90 if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) { 91 vp9_write(w, tx_size != TX_8X8, tx_probs[1]); 92 if (tx_size != TX_8X8 && max_tx_size >= TX_32X32) 93 vp9_write(w, tx_size != TX_16X16, tx_probs[2]); 94 } 95} 96 97static int write_skip(const VP9_COMP *cpi, int segment_id, const MODE_INFO *mi, 98 vp9_writer *w) { 99 const MACROBLOCKD *const xd = &cpi->mb.e_mbd; 100 if (vp9_segfeature_active(&cpi->common.seg, segment_id, SEG_LVL_SKIP)) { 101 return 1; 102 } else { 103 const int skip = mi->mbmi.skip; 104 vp9_write(w, skip, vp9_get_skip_prob(&cpi->common, xd)); 105 return skip; 106 } 107} 108 109static void update_skip_probs(VP9_COMMON *cm, vp9_writer *w) { 110 int k; 111 112 for (k = 0; k < SKIP_CONTEXTS; ++k) 113 vp9_cond_prob_diff_update(w, &cm->fc.skip_probs[k], cm->counts.skip[k]); 114} 115 116static void update_switchable_interp_probs(VP9_COMMON *cm, vp9_writer *w) { 117 int j; 118 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) 119 prob_diff_update(vp9_switchable_interp_tree, 120 cm->fc.switchable_interp_prob[j], 121 cm->counts.switchable_interp[j], SWITCHABLE_FILTERS, w); 122} 123 124static void pack_mb_tokens(vp9_writer *w, 125 TOKENEXTRA **tp, const TOKENEXTRA *stop) { 126 TOKENEXTRA *p = *tp; 127 128 while (p < stop && p->token != EOSB_TOKEN) { 129 const int t = p->token; 130 const struct vp9_token *const a = &vp9_coef_encodings[t]; 131 const vp9_extra_bit *const b = &vp9_extra_bits[t]; 132 int i = 0; 133 int v = a->value; 134 int n = a->len; 135 136 /* skip one or two nodes */ 137 if (p->skip_eob_node) { 138 n -= p->skip_eob_node; 139 i = 2 * p->skip_eob_node; 140 } 141 142 // TODO(jbb): expanding this can lead to big gains. It allows 143 // much better branch prediction and would enable us to avoid numerous 144 // lookups and compares. 145 146 // If we have a token that's in the constrained set, the coefficient tree 147 // is split into two treed writes. The first treed write takes care of the 148 // unconstrained nodes. The second treed write takes care of the 149 // constrained nodes. 150 if (t >= TWO_TOKEN && t < EOB_TOKEN) { 151 int len = UNCONSTRAINED_NODES - p->skip_eob_node; 152 int bits = v >> (n - len); 153 vp9_write_tree(w, vp9_coef_tree, p->context_tree, bits, len, i); 154 vp9_write_tree(w, vp9_coef_con_tree, 155 vp9_pareto8_full[p->context_tree[PIVOT_NODE] - 1], 156 v, n - len, 0); 157 } else { 158 vp9_write_tree(w, vp9_coef_tree, p->context_tree, v, n, i); 159 } 160 161 if (b->base_val) { 162 const int e = p->extra, l = b->len; 163 164 if (l) { 165 const unsigned char *pb = b->prob; 166 int v = e >> 1; 167 int n = l; /* number of bits in v, assumed nonzero */ 168 int i = 0; 169 170 do { 171 const int bb = (v >> --n) & 1; 172 vp9_write(w, bb, pb[i >> 1]); 173 i = b->tree[i + bb]; 174 } while (n); 175 } 176 177 vp9_write_bit(w, e & 1); 178 } 179 ++p; 180 } 181 182 *tp = p + (p->token == EOSB_TOKEN); 183} 184 185static void write_segment_id(vp9_writer *w, const struct segmentation *seg, 186 int segment_id) { 187 if (seg->enabled && seg->update_map) 188 vp9_write_tree(w, vp9_segment_tree, seg->tree_probs, segment_id, 3, 0); 189} 190 191// This function encodes the reference frame 192static void write_ref_frames(const VP9_COMP *cpi, vp9_writer *w) { 193 const VP9_COMMON *const cm = &cpi->common; 194 const MACROBLOCKD *const xd = &cpi->mb.e_mbd; 195 const MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; 196 const int is_compound = has_second_ref(mbmi); 197 const int segment_id = mbmi->segment_id; 198 199 // If segment level coding of this signal is disabled... 200 // or the segment allows multiple reference frame options 201 if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) { 202 assert(!is_compound); 203 assert(mbmi->ref_frame[0] == 204 vp9_get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME)); 205 } else { 206 // does the feature use compound prediction or not 207 // (if not specified at the frame/segment level) 208 if (cm->reference_mode == REFERENCE_MODE_SELECT) { 209 vp9_write(w, is_compound, vp9_get_reference_mode_prob(cm, xd)); 210 } else { 211 assert(!is_compound == (cm->reference_mode == SINGLE_REFERENCE)); 212 } 213 214 if (is_compound) { 215 vp9_write(w, mbmi->ref_frame[0] == GOLDEN_FRAME, 216 vp9_get_pred_prob_comp_ref_p(cm, xd)); 217 } else { 218 const int bit0 = mbmi->ref_frame[0] != LAST_FRAME; 219 vp9_write(w, bit0, vp9_get_pred_prob_single_ref_p1(cm, xd)); 220 if (bit0) { 221 const int bit1 = mbmi->ref_frame[0] != GOLDEN_FRAME; 222 vp9_write(w, bit1, vp9_get_pred_prob_single_ref_p2(cm, xd)); 223 } 224 } 225 } 226} 227 228static void pack_inter_mode_mvs(VP9_COMP *cpi, const MODE_INFO *mi, 229 vp9_writer *w) { 230 VP9_COMMON *const cm = &cpi->common; 231 const nmv_context *nmvc = &cm->fc.nmvc; 232 const MACROBLOCK *const x = &cpi->mb; 233 const MACROBLOCKD *const xd = &x->e_mbd; 234 const struct segmentation *const seg = &cm->seg; 235 const MB_MODE_INFO *const mbmi = &mi->mbmi; 236 const MB_PREDICTION_MODE mode = mbmi->mode; 237 const int segment_id = mbmi->segment_id; 238 const BLOCK_SIZE bsize = mbmi->sb_type; 239 const int allow_hp = cm->allow_high_precision_mv; 240 const int is_inter = is_inter_block(mbmi); 241 const int is_compound = has_second_ref(mbmi); 242 int skip, ref; 243 244 if (seg->update_map) { 245 if (seg->temporal_update) { 246 const int pred_flag = mbmi->seg_id_predicted; 247 vp9_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd); 248 vp9_write(w, pred_flag, pred_prob); 249 if (!pred_flag) 250 write_segment_id(w, seg, segment_id); 251 } else { 252 write_segment_id(w, seg, segment_id); 253 } 254 } 255 256 skip = write_skip(cpi, segment_id, mi, w); 257 258 if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) 259 vp9_write(w, is_inter, vp9_get_intra_inter_prob(cm, xd)); 260 261 if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT && 262 !(is_inter && 263 (skip || vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)))) { 264 write_selected_tx_size(cpi, mbmi->tx_size, bsize, w); 265 } 266 267 if (!is_inter) { 268 if (bsize >= BLOCK_8X8) { 269 write_intra_mode(w, mode, cm->fc.y_mode_prob[size_group_lookup[bsize]]); 270 } else { 271 int idx, idy; 272 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; 273 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; 274 for (idy = 0; idy < 2; idy += num_4x4_h) { 275 for (idx = 0; idx < 2; idx += num_4x4_w) { 276 const MB_PREDICTION_MODE b_mode = mi->bmi[idy * 2 + idx].as_mode; 277 write_intra_mode(w, b_mode, cm->fc.y_mode_prob[0]); 278 } 279 } 280 } 281 write_intra_mode(w, mbmi->uv_mode, cm->fc.uv_mode_prob[mode]); 282 } else { 283 const int mode_ctx = mbmi->mode_context[mbmi->ref_frame[0]]; 284 const vp9_prob *const inter_probs = cm->fc.inter_mode_probs[mode_ctx]; 285 write_ref_frames(cpi, w); 286 287 // If segment skip is not enabled code the mode. 288 if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)) { 289 if (bsize >= BLOCK_8X8) { 290 write_inter_mode(w, mode, inter_probs); 291 ++cm->counts.inter_mode[mode_ctx][INTER_OFFSET(mode)]; 292 } 293 } 294 295 if (cm->interp_filter == SWITCHABLE) { 296 const int ctx = vp9_get_pred_context_switchable_interp(xd); 297 vp9_write_token(w, vp9_switchable_interp_tree, 298 cm->fc.switchable_interp_prob[ctx], 299 &switchable_interp_encodings[mbmi->interp_filter]); 300 } else { 301 assert(mbmi->interp_filter == cm->interp_filter); 302 } 303 304 if (bsize < BLOCK_8X8) { 305 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; 306 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; 307 int idx, idy; 308 for (idy = 0; idy < 2; idy += num_4x4_h) { 309 for (idx = 0; idx < 2; idx += num_4x4_w) { 310 const int j = idy * 2 + idx; 311 const MB_PREDICTION_MODE b_mode = mi->bmi[j].as_mode; 312 write_inter_mode(w, b_mode, inter_probs); 313 ++cm->counts.inter_mode[mode_ctx][INTER_OFFSET(b_mode)]; 314 if (b_mode == NEWMV) { 315 for (ref = 0; ref < 1 + is_compound; ++ref) 316 vp9_encode_mv(cpi, w, &mi->bmi[j].as_mv[ref].as_mv, 317 &mbmi->ref_mvs[mbmi->ref_frame[ref]][0].as_mv, 318 nmvc, allow_hp); 319 } 320 } 321 } 322 } else { 323 if (mode == NEWMV) { 324 for (ref = 0; ref < 1 + is_compound; ++ref) 325 vp9_encode_mv(cpi, w, &mbmi->mv[ref].as_mv, 326 &mbmi->ref_mvs[mbmi->ref_frame[ref]][0].as_mv, nmvc, 327 allow_hp); 328 } 329 } 330 } 331} 332 333static void write_mb_modes_kf(const VP9_COMP *cpi, MODE_INFO **mi_8x8, 334 vp9_writer *w) { 335 const VP9_COMMON *const cm = &cpi->common; 336 const MACROBLOCKD *const xd = &cpi->mb.e_mbd; 337 const struct segmentation *const seg = &cm->seg; 338 const MODE_INFO *const mi = mi_8x8[0]; 339 const MODE_INFO *const above_mi = mi_8x8[-xd->mode_info_stride]; 340 const MODE_INFO *const left_mi = xd->left_available ? mi_8x8[-1] : NULL; 341 const MB_MODE_INFO *const mbmi = &mi->mbmi; 342 const BLOCK_SIZE bsize = mbmi->sb_type; 343 344 if (seg->update_map) 345 write_segment_id(w, seg, mbmi->segment_id); 346 347 write_skip(cpi, mbmi->segment_id, mi, w); 348 349 if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT) 350 write_selected_tx_size(cpi, mbmi->tx_size, bsize, w); 351 352 if (bsize >= BLOCK_8X8) { 353 write_intra_mode(w, mbmi->mode, get_y_mode_probs(mi, above_mi, left_mi, 0)); 354 } else { 355 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; 356 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; 357 int idx, idy; 358 359 for (idy = 0; idy < 2; idy += num_4x4_h) { 360 for (idx = 0; idx < 2; idx += num_4x4_w) { 361 const int block = idy * 2 + idx; 362 write_intra_mode(w, mi->bmi[block].as_mode, 363 get_y_mode_probs(mi, above_mi, left_mi, block)); 364 } 365 } 366 } 367 368 write_intra_mode(w, mbmi->uv_mode, vp9_kf_uv_mode_prob[mbmi->mode]); 369} 370 371static void write_modes_b(VP9_COMP *cpi, const TileInfo *const tile, 372 vp9_writer *w, TOKENEXTRA **tok, TOKENEXTRA *tok_end, 373 int mi_row, int mi_col) { 374 VP9_COMMON *const cm = &cpi->common; 375 MACROBLOCKD *const xd = &cpi->mb.e_mbd; 376 MODE_INFO *m; 377 378 xd->mi_8x8 = cm->mi_grid_visible + (mi_row * cm->mode_info_stride + mi_col); 379 m = xd->mi_8x8[0]; 380 381 set_mi_row_col(xd, tile, 382 mi_row, num_8x8_blocks_high_lookup[m->mbmi.sb_type], 383 mi_col, num_8x8_blocks_wide_lookup[m->mbmi.sb_type], 384 cm->mi_rows, cm->mi_cols); 385 if (frame_is_intra_only(cm)) { 386 write_mb_modes_kf(cpi, xd->mi_8x8, w); 387 } else { 388 pack_inter_mode_mvs(cpi, m, w); 389 } 390 391 assert(*tok < tok_end); 392 pack_mb_tokens(w, tok, tok_end); 393} 394 395static void write_partition(VP9_COMP *cpi, int hbs, int mi_row, int mi_col, 396 PARTITION_TYPE p, BLOCK_SIZE bsize, vp9_writer *w) { 397 VP9_COMMON *const cm = &cpi->common; 398 const int ctx = partition_plane_context(cpi->above_seg_context, 399 cpi->left_seg_context, 400 mi_row, mi_col, bsize); 401 const vp9_prob *const probs = get_partition_probs(cm, ctx); 402 const int has_rows = (mi_row + hbs) < cm->mi_rows; 403 const int has_cols = (mi_col + hbs) < cm->mi_cols; 404 405 if (has_rows && has_cols) { 406 vp9_write_token(w, vp9_partition_tree, probs, &partition_encodings[p]); 407 } else if (!has_rows && has_cols) { 408 assert(p == PARTITION_SPLIT || p == PARTITION_HORZ); 409 vp9_write(w, p == PARTITION_SPLIT, probs[1]); 410 } else if (has_rows && !has_cols) { 411 assert(p == PARTITION_SPLIT || p == PARTITION_VERT); 412 vp9_write(w, p == PARTITION_SPLIT, probs[2]); 413 } else { 414 assert(p == PARTITION_SPLIT); 415 } 416} 417 418static void write_modes_sb(VP9_COMP *cpi, const TileInfo *const tile, 419 vp9_writer *w, TOKENEXTRA **tok, TOKENEXTRA *tok_end, 420 int mi_row, int mi_col, BLOCK_SIZE bsize) { 421 VP9_COMMON *const cm = &cpi->common; 422 const int bsl = b_width_log2(bsize); 423 const int bs = (1 << bsl) / 4; 424 PARTITION_TYPE partition; 425 BLOCK_SIZE subsize; 426 MODE_INFO *m = cm->mi_grid_visible[mi_row * cm->mode_info_stride + mi_col]; 427 428 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) 429 return; 430 431 partition = partition_lookup[bsl][m->mbmi.sb_type]; 432 write_partition(cpi, bs, mi_row, mi_col, partition, bsize, w); 433 subsize = get_subsize(bsize, partition); 434 if (subsize < BLOCK_8X8) { 435 write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); 436 } else { 437 switch (partition) { 438 case PARTITION_NONE: 439 write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); 440 break; 441 case PARTITION_HORZ: 442 write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); 443 if (mi_row + bs < cm->mi_rows) 444 write_modes_b(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col); 445 break; 446 case PARTITION_VERT: 447 write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); 448 if (mi_col + bs < cm->mi_cols) 449 write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + bs); 450 break; 451 case PARTITION_SPLIT: 452 write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, subsize); 453 write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col + bs, 454 subsize); 455 write_modes_sb(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col, 456 subsize); 457 write_modes_sb(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col + bs, 458 subsize); 459 break; 460 default: 461 assert(0); 462 } 463 } 464 465 // update partition context 466 if (bsize >= BLOCK_8X8 && 467 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) 468 update_partition_context(cpi->above_seg_context, cpi->left_seg_context, 469 mi_row, mi_col, subsize, bsize); 470} 471 472static void write_modes(VP9_COMP *cpi, const TileInfo *const tile, 473 vp9_writer *w, TOKENEXTRA **tok, TOKENEXTRA *tok_end) { 474 int mi_row, mi_col; 475 476 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end; 477 mi_row += MI_BLOCK_SIZE) { 478 vp9_zero(cpi->left_seg_context); 479 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end; 480 mi_col += MI_BLOCK_SIZE) 481 write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, BLOCK_64X64); 482 } 483} 484 485static void build_tree_distribution(VP9_COMP *cpi, TX_SIZE tx_size) { 486 vp9_coeff_probs_model *coef_probs = cpi->frame_coef_probs[tx_size]; 487 vp9_coeff_count *coef_counts = cpi->coef_counts[tx_size]; 488 unsigned int (*eob_branch_ct)[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] = 489 cpi->common.counts.eob_branch[tx_size]; 490 vp9_coeff_stats *coef_branch_ct = cpi->frame_branch_ct[tx_size]; 491 int i, j, k, l, m; 492 493 for (i = 0; i < PLANE_TYPES; ++i) { 494 for (j = 0; j < REF_TYPES; ++j) { 495 for (k = 0; k < COEF_BANDS; ++k) { 496 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { 497 vp9_tree_probs_from_distribution(vp9_coef_tree, 498 coef_branch_ct[i][j][k][l], 499 coef_counts[i][j][k][l]); 500 coef_branch_ct[i][j][k][l][0][1] = eob_branch_ct[i][j][k][l] - 501 coef_branch_ct[i][j][k][l][0][0]; 502 for (m = 0; m < UNCONSTRAINED_NODES; ++m) 503 coef_probs[i][j][k][l][m] = get_binary_prob( 504 coef_branch_ct[i][j][k][l][m][0], 505 coef_branch_ct[i][j][k][l][m][1]); 506 } 507 } 508 } 509 } 510} 511 512static void update_coef_probs_common(vp9_writer* const bc, VP9_COMP *cpi, 513 TX_SIZE tx_size) { 514 vp9_coeff_probs_model *new_frame_coef_probs = cpi->frame_coef_probs[tx_size]; 515 vp9_coeff_probs_model *old_frame_coef_probs = 516 cpi->common.fc.coef_probs[tx_size]; 517 vp9_coeff_stats *frame_branch_ct = cpi->frame_branch_ct[tx_size]; 518 const vp9_prob upd = DIFF_UPDATE_PROB; 519 const int entropy_nodes_update = UNCONSTRAINED_NODES; 520 int i, j, k, l, t; 521 switch (cpi->sf.use_fast_coef_updates) { 522 case 0: { 523 /* dry run to see if there is any udpate at all needed */ 524 int savings = 0; 525 int update[2] = {0, 0}; 526 for (i = 0; i < PLANE_TYPES; ++i) { 527 for (j = 0; j < REF_TYPES; ++j) { 528 for (k = 0; k < COEF_BANDS; ++k) { 529 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { 530 for (t = 0; t < entropy_nodes_update; ++t) { 531 vp9_prob newp = new_frame_coef_probs[i][j][k][l][t]; 532 const vp9_prob oldp = old_frame_coef_probs[i][j][k][l][t]; 533 int s; 534 int u = 0; 535 if (t == PIVOT_NODE) 536 s = vp9_prob_diff_update_savings_search_model( 537 frame_branch_ct[i][j][k][l][0], 538 old_frame_coef_probs[i][j][k][l], &newp, upd); 539 else 540 s = vp9_prob_diff_update_savings_search( 541 frame_branch_ct[i][j][k][l][t], oldp, &newp, upd); 542 if (s > 0 && newp != oldp) 543 u = 1; 544 if (u) 545 savings += s - (int)(vp9_cost_zero(upd)); 546 else 547 savings -= (int)(vp9_cost_zero(upd)); 548 update[u]++; 549 } 550 } 551 } 552 } 553 } 554 555 // printf("Update %d %d, savings %d\n", update[0], update[1], savings); 556 /* Is coef updated at all */ 557 if (update[1] == 0 || savings < 0) { 558 vp9_write_bit(bc, 0); 559 return; 560 } 561 vp9_write_bit(bc, 1); 562 for (i = 0; i < PLANE_TYPES; ++i) { 563 for (j = 0; j < REF_TYPES; ++j) { 564 for (k = 0; k < COEF_BANDS; ++k) { 565 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { 566 // calc probs and branch cts for this frame only 567 for (t = 0; t < entropy_nodes_update; ++t) { 568 vp9_prob newp = new_frame_coef_probs[i][j][k][l][t]; 569 vp9_prob *oldp = old_frame_coef_probs[i][j][k][l] + t; 570 const vp9_prob upd = DIFF_UPDATE_PROB; 571 int s; 572 int u = 0; 573 if (t == PIVOT_NODE) 574 s = vp9_prob_diff_update_savings_search_model( 575 frame_branch_ct[i][j][k][l][0], 576 old_frame_coef_probs[i][j][k][l], &newp, upd); 577 else 578 s = vp9_prob_diff_update_savings_search( 579 frame_branch_ct[i][j][k][l][t], 580 *oldp, &newp, upd); 581 if (s > 0 && newp != *oldp) 582 u = 1; 583 vp9_write(bc, u, upd); 584 if (u) { 585 /* send/use new probability */ 586 vp9_write_prob_diff_update(bc, newp, *oldp); 587 *oldp = newp; 588 } 589 } 590 } 591 } 592 } 593 } 594 return; 595 } 596 597 case 1: 598 case 2: { 599 const int prev_coef_contexts_to_update = 600 cpi->sf.use_fast_coef_updates == 2 ? COEFF_CONTEXTS >> 1 601 : COEFF_CONTEXTS; 602 const int coef_band_to_update = 603 cpi->sf.use_fast_coef_updates == 2 ? COEF_BANDS >> 1 604 : COEF_BANDS; 605 int updates = 0; 606 int noupdates_before_first = 0; 607 for (i = 0; i < PLANE_TYPES; ++i) { 608 for (j = 0; j < REF_TYPES; ++j) { 609 for (k = 0; k < COEF_BANDS; ++k) { 610 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { 611 // calc probs and branch cts for this frame only 612 for (t = 0; t < entropy_nodes_update; ++t) { 613 vp9_prob newp = new_frame_coef_probs[i][j][k][l][t]; 614 vp9_prob *oldp = old_frame_coef_probs[i][j][k][l] + t; 615 int s; 616 int u = 0; 617 if (l >= prev_coef_contexts_to_update || 618 k >= coef_band_to_update) { 619 u = 0; 620 } else { 621 if (t == PIVOT_NODE) 622 s = vp9_prob_diff_update_savings_search_model( 623 frame_branch_ct[i][j][k][l][0], 624 old_frame_coef_probs[i][j][k][l], &newp, upd); 625 else 626 s = vp9_prob_diff_update_savings_search( 627 frame_branch_ct[i][j][k][l][t], 628 *oldp, &newp, upd); 629 if (s > 0 && newp != *oldp) 630 u = 1; 631 } 632 updates += u; 633 if (u == 0 && updates == 0) { 634 noupdates_before_first++; 635 continue; 636 } 637 if (u == 1 && updates == 1) { 638 int v; 639 // first update 640 vp9_write_bit(bc, 1); 641 for (v = 0; v < noupdates_before_first; ++v) 642 vp9_write(bc, 0, upd); 643 } 644 vp9_write(bc, u, upd); 645 if (u) { 646 /* send/use new probability */ 647 vp9_write_prob_diff_update(bc, newp, *oldp); 648 *oldp = newp; 649 } 650 } 651 } 652 } 653 } 654 } 655 if (updates == 0) { 656 vp9_write_bit(bc, 0); // no updates 657 } 658 return; 659 } 660 661 default: 662 assert(0); 663 } 664} 665 666static void update_coef_probs(VP9_COMP *cpi, vp9_writer* w) { 667 const TX_MODE tx_mode = cpi->common.tx_mode; 668 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode]; 669 TX_SIZE tx_size; 670 vp9_clear_system_state(); 671 672 for (tx_size = TX_4X4; tx_size <= TX_32X32; ++tx_size) 673 build_tree_distribution(cpi, tx_size); 674 675 for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size) 676 update_coef_probs_common(w, cpi, tx_size); 677} 678 679static void encode_loopfilter(struct loopfilter *lf, 680 struct vp9_write_bit_buffer *wb) { 681 int i; 682 683 // Encode the loop filter level and type 684 vp9_wb_write_literal(wb, lf->filter_level, 6); 685 vp9_wb_write_literal(wb, lf->sharpness_level, 3); 686 687 // Write out loop filter deltas applied at the MB level based on mode or 688 // ref frame (if they are enabled). 689 vp9_wb_write_bit(wb, lf->mode_ref_delta_enabled); 690 691 if (lf->mode_ref_delta_enabled) { 692 vp9_wb_write_bit(wb, lf->mode_ref_delta_update); 693 if (lf->mode_ref_delta_update) { 694 for (i = 0; i < MAX_REF_LF_DELTAS; i++) { 695 const int delta = lf->ref_deltas[i]; 696 const int changed = delta != lf->last_ref_deltas[i]; 697 vp9_wb_write_bit(wb, changed); 698 if (changed) { 699 lf->last_ref_deltas[i] = delta; 700 vp9_wb_write_literal(wb, abs(delta) & 0x3F, 6); 701 vp9_wb_write_bit(wb, delta < 0); 702 } 703 } 704 705 for (i = 0; i < MAX_MODE_LF_DELTAS; i++) { 706 const int delta = lf->mode_deltas[i]; 707 const int changed = delta != lf->last_mode_deltas[i]; 708 vp9_wb_write_bit(wb, changed); 709 if (changed) { 710 lf->last_mode_deltas[i] = delta; 711 vp9_wb_write_literal(wb, abs(delta) & 0x3F, 6); 712 vp9_wb_write_bit(wb, delta < 0); 713 } 714 } 715 } 716 } 717} 718 719static void write_delta_q(struct vp9_write_bit_buffer *wb, int delta_q) { 720 if (delta_q != 0) { 721 vp9_wb_write_bit(wb, 1); 722 vp9_wb_write_literal(wb, abs(delta_q), 4); 723 vp9_wb_write_bit(wb, delta_q < 0); 724 } else { 725 vp9_wb_write_bit(wb, 0); 726 } 727} 728 729static void encode_quantization(VP9_COMMON *cm, 730 struct vp9_write_bit_buffer *wb) { 731 vp9_wb_write_literal(wb, cm->base_qindex, QINDEX_BITS); 732 write_delta_q(wb, cm->y_dc_delta_q); 733 write_delta_q(wb, cm->uv_dc_delta_q); 734 write_delta_q(wb, cm->uv_ac_delta_q); 735} 736 737 738static void encode_segmentation(VP9_COMP *cpi, 739 struct vp9_write_bit_buffer *wb) { 740 int i, j; 741 742 struct segmentation *seg = &cpi->common.seg; 743 744 vp9_wb_write_bit(wb, seg->enabled); 745 if (!seg->enabled) 746 return; 747 748 // Segmentation map 749 vp9_wb_write_bit(wb, seg->update_map); 750 if (seg->update_map) { 751 // Select the coding strategy (temporal or spatial) 752 vp9_choose_segmap_coding_method(cpi); 753 // Write out probabilities used to decode unpredicted macro-block segments 754 for (i = 0; i < SEG_TREE_PROBS; i++) { 755 const int prob = seg->tree_probs[i]; 756 const int update = prob != MAX_PROB; 757 vp9_wb_write_bit(wb, update); 758 if (update) 759 vp9_wb_write_literal(wb, prob, 8); 760 } 761 762 // Write out the chosen coding method. 763 vp9_wb_write_bit(wb, seg->temporal_update); 764 if (seg->temporal_update) { 765 for (i = 0; i < PREDICTION_PROBS; i++) { 766 const int prob = seg->pred_probs[i]; 767 const int update = prob != MAX_PROB; 768 vp9_wb_write_bit(wb, update); 769 if (update) 770 vp9_wb_write_literal(wb, prob, 8); 771 } 772 } 773 } 774 775 // Segmentation data 776 vp9_wb_write_bit(wb, seg->update_data); 777 if (seg->update_data) { 778 vp9_wb_write_bit(wb, seg->abs_delta); 779 780 for (i = 0; i < MAX_SEGMENTS; i++) { 781 for (j = 0; j < SEG_LVL_MAX; j++) { 782 const int active = vp9_segfeature_active(seg, i, j); 783 vp9_wb_write_bit(wb, active); 784 if (active) { 785 const int data = vp9_get_segdata(seg, i, j); 786 const int data_max = vp9_seg_feature_data_max(j); 787 788 if (vp9_is_segfeature_signed(j)) { 789 encode_unsigned_max(wb, abs(data), data_max); 790 vp9_wb_write_bit(wb, data < 0); 791 } else { 792 encode_unsigned_max(wb, data, data_max); 793 } 794 } 795 } 796 } 797 } 798} 799 800 801static void encode_txfm_probs(VP9_COMMON *cm, vp9_writer *w) { 802 // Mode 803 vp9_write_literal(w, MIN(cm->tx_mode, ALLOW_32X32), 2); 804 if (cm->tx_mode >= ALLOW_32X32) 805 vp9_write_bit(w, cm->tx_mode == TX_MODE_SELECT); 806 807 // Probabilities 808 if (cm->tx_mode == TX_MODE_SELECT) { 809 int i, j; 810 unsigned int ct_8x8p[TX_SIZES - 3][2]; 811 unsigned int ct_16x16p[TX_SIZES - 2][2]; 812 unsigned int ct_32x32p[TX_SIZES - 1][2]; 813 814 815 for (i = 0; i < TX_SIZE_CONTEXTS; i++) { 816 tx_counts_to_branch_counts_8x8(cm->counts.tx.p8x8[i], ct_8x8p); 817 for (j = 0; j < TX_SIZES - 3; j++) 818 vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p8x8[i][j], ct_8x8p[j]); 819 } 820 821 for (i = 0; i < TX_SIZE_CONTEXTS; i++) { 822 tx_counts_to_branch_counts_16x16(cm->counts.tx.p16x16[i], ct_16x16p); 823 for (j = 0; j < TX_SIZES - 2; j++) 824 vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p16x16[i][j], 825 ct_16x16p[j]); 826 } 827 828 for (i = 0; i < TX_SIZE_CONTEXTS; i++) { 829 tx_counts_to_branch_counts_32x32(cm->counts.tx.p32x32[i], ct_32x32p); 830 for (j = 0; j < TX_SIZES - 1; j++) 831 vp9_cond_prob_diff_update(w, &cm->fc.tx_probs.p32x32[i][j], 832 ct_32x32p[j]); 833 } 834 } 835} 836 837static void write_interp_filter(INTERP_FILTER filter, 838 struct vp9_write_bit_buffer *wb) { 839 const int filter_to_literal[] = { 1, 0, 2, 3 }; 840 841 vp9_wb_write_bit(wb, filter == SWITCHABLE); 842 if (filter != SWITCHABLE) 843 vp9_wb_write_literal(wb, filter_to_literal[filter], 2); 844} 845 846static void fix_interp_filter(VP9_COMMON *cm) { 847 if (cm->interp_filter == SWITCHABLE) { 848 // Check to see if only one of the filters is actually used 849 int count[SWITCHABLE_FILTERS]; 850 int i, j, c = 0; 851 for (i = 0; i < SWITCHABLE_FILTERS; ++i) { 852 count[i] = 0; 853 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) 854 count[i] += cm->counts.switchable_interp[j][i]; 855 c += (count[i] > 0); 856 } 857 if (c == 1) { 858 // Only one filter is used. So set the filter at frame level 859 for (i = 0; i < SWITCHABLE_FILTERS; ++i) { 860 if (count[i]) { 861 cm->interp_filter = i; 862 break; 863 } 864 } 865 } 866 } 867} 868 869static void write_tile_info(VP9_COMMON *cm, struct vp9_write_bit_buffer *wb) { 870 int min_log2_tile_cols, max_log2_tile_cols, ones; 871 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); 872 873 // columns 874 ones = cm->log2_tile_cols - min_log2_tile_cols; 875 while (ones--) 876 vp9_wb_write_bit(wb, 1); 877 878 if (cm->log2_tile_cols < max_log2_tile_cols) 879 vp9_wb_write_bit(wb, 0); 880 881 // rows 882 vp9_wb_write_bit(wb, cm->log2_tile_rows != 0); 883 if (cm->log2_tile_rows != 0) 884 vp9_wb_write_bit(wb, cm->log2_tile_rows != 1); 885} 886 887static int get_refresh_mask(VP9_COMP *cpi) { 888 // Should the GF or ARF be updated using the transmitted frame or buffer 889#if CONFIG_MULTIPLE_ARF 890 if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame && 891 !cpi->refresh_alt_ref_frame) { 892#else 893 if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame && 894 !cpi->use_svc) { 895#endif 896 // Preserve the previously existing golden frame and update the frame in 897 // the alt ref slot instead. This is highly specific to the use of 898 // alt-ref as a forward reference, and this needs to be generalized as 899 // other uses are implemented (like RTC/temporal scaling) 900 // 901 // gld_fb_idx and alt_fb_idx need to be swapped for future frames, but 902 // that happens in vp9_onyx_if.c:update_reference_frames() so that it can 903 // be done outside of the recode loop. 904 return (cpi->refresh_last_frame << cpi->lst_fb_idx) | 905 (cpi->refresh_golden_frame << cpi->alt_fb_idx); 906 } else { 907 int arf_idx = cpi->alt_fb_idx; 908#if CONFIG_MULTIPLE_ARF 909 // Determine which ARF buffer to use to encode this ARF frame. 910 if (cpi->multi_arf_enabled) { 911 int sn = cpi->sequence_number; 912 arf_idx = (cpi->frame_coding_order[sn] < 0) ? 913 cpi->arf_buffer_idx[sn + 1] : 914 cpi->arf_buffer_idx[sn]; 915 } 916#endif 917 return (cpi->refresh_last_frame << cpi->lst_fb_idx) | 918 (cpi->refresh_golden_frame << cpi->gld_fb_idx) | 919 (cpi->refresh_alt_ref_frame << arf_idx); 920 } 921} 922 923static size_t encode_tiles(VP9_COMP *cpi, uint8_t *data_ptr) { 924 VP9_COMMON *const cm = &cpi->common; 925 vp9_writer residual_bc; 926 927 int tile_row, tile_col; 928 TOKENEXTRA *tok[4][1 << 6], *tok_end; 929 size_t total_size = 0; 930 const int tile_cols = 1 << cm->log2_tile_cols; 931 const int tile_rows = 1 << cm->log2_tile_rows; 932 933 vpx_memset(cpi->above_seg_context, 0, sizeof(*cpi->above_seg_context) * 934 mi_cols_aligned_to_sb(cm->mi_cols)); 935 936 tok[0][0] = cpi->tok; 937 for (tile_row = 0; tile_row < tile_rows; tile_row++) { 938 if (tile_row) 939 tok[tile_row][0] = tok[tile_row - 1][tile_cols - 1] + 940 cpi->tok_count[tile_row - 1][tile_cols - 1]; 941 942 for (tile_col = 1; tile_col < tile_cols; tile_col++) 943 tok[tile_row][tile_col] = tok[tile_row][tile_col - 1] + 944 cpi->tok_count[tile_row][tile_col - 1]; 945 } 946 947 for (tile_row = 0; tile_row < tile_rows; tile_row++) { 948 for (tile_col = 0; tile_col < tile_cols; tile_col++) { 949 TileInfo tile; 950 951 vp9_tile_init(&tile, cm, tile_row, tile_col); 952 tok_end = tok[tile_row][tile_col] + cpi->tok_count[tile_row][tile_col]; 953 954 if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) 955 vp9_start_encode(&residual_bc, data_ptr + total_size + 4); 956 else 957 vp9_start_encode(&residual_bc, data_ptr + total_size); 958 959 write_modes(cpi, &tile, &residual_bc, &tok[tile_row][tile_col], tok_end); 960 assert(tok[tile_row][tile_col] == tok_end); 961 vp9_stop_encode(&residual_bc); 962 if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) { 963 // size of this tile 964 mem_put_be32(data_ptr + total_size, residual_bc.pos); 965 total_size += 4; 966 } 967 968 total_size += residual_bc.pos; 969 } 970 } 971 972 return total_size; 973} 974 975static void write_display_size(const VP9_COMMON *cm, 976 struct vp9_write_bit_buffer *wb) { 977 const int scaling_active = cm->width != cm->display_width || 978 cm->height != cm->display_height; 979 vp9_wb_write_bit(wb, scaling_active); 980 if (scaling_active) { 981 vp9_wb_write_literal(wb, cm->display_width - 1, 16); 982 vp9_wb_write_literal(wb, cm->display_height - 1, 16); 983 } 984} 985 986static void write_frame_size(const VP9_COMMON *cm, 987 struct vp9_write_bit_buffer *wb) { 988 vp9_wb_write_literal(wb, cm->width - 1, 16); 989 vp9_wb_write_literal(wb, cm->height - 1, 16); 990 991 write_display_size(cm, wb); 992} 993 994static void write_frame_size_with_refs(VP9_COMP *cpi, 995 struct vp9_write_bit_buffer *wb) { 996 VP9_COMMON *const cm = &cpi->common; 997 int found = 0; 998 999 MV_REFERENCE_FRAME ref_frame; 1000 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { 1001 YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, ref_frame); 1002 found = cm->width == cfg->y_crop_width && 1003 cm->height == cfg->y_crop_height; 1004 1005 // TODO(ivan): This prevents a bug while more than 3 buffers are used. Do it 1006 // in a better way. 1007 if (cpi->use_svc) { 1008 found = 0; 1009 } 1010 vp9_wb_write_bit(wb, found); 1011 if (found) { 1012 break; 1013 } 1014 } 1015 1016 if (!found) { 1017 vp9_wb_write_literal(wb, cm->width - 1, 16); 1018 vp9_wb_write_literal(wb, cm->height - 1, 16); 1019 } 1020 1021 write_display_size(cm, wb); 1022} 1023 1024static void write_sync_code(struct vp9_write_bit_buffer *wb) { 1025 vp9_wb_write_literal(wb, VP9_SYNC_CODE_0, 8); 1026 vp9_wb_write_literal(wb, VP9_SYNC_CODE_1, 8); 1027 vp9_wb_write_literal(wb, VP9_SYNC_CODE_2, 8); 1028} 1029 1030static void write_uncompressed_header(VP9_COMP *cpi, 1031 struct vp9_write_bit_buffer *wb) { 1032 VP9_COMMON *const cm = &cpi->common; 1033 1034 vp9_wb_write_literal(wb, VP9_FRAME_MARKER, 2); 1035 1036 // bitstream version. 1037 // 00 - profile 0. 4:2:0 only 1038 // 10 - profile 1. adds 4:4:4, 4:2:2, alpha 1039 vp9_wb_write_bit(wb, cm->version); 1040 vp9_wb_write_bit(wb, 0); 1041 1042 vp9_wb_write_bit(wb, 0); 1043 vp9_wb_write_bit(wb, cm->frame_type); 1044 vp9_wb_write_bit(wb, cm->show_frame); 1045 vp9_wb_write_bit(wb, cm->error_resilient_mode); 1046 1047 if (cm->frame_type == KEY_FRAME) { 1048 const COLOR_SPACE cs = UNKNOWN; 1049 write_sync_code(wb); 1050 vp9_wb_write_literal(wb, cs, 3); 1051 if (cs != SRGB) { 1052 vp9_wb_write_bit(wb, 0); // 0: [16, 235] (i.e. xvYCC), 1: [0, 255] 1053 if (cm->version == 1) { 1054 vp9_wb_write_bit(wb, cm->subsampling_x); 1055 vp9_wb_write_bit(wb, cm->subsampling_y); 1056 vp9_wb_write_bit(wb, 0); // has extra plane 1057 } 1058 } else { 1059 assert(cm->version == 1); 1060 vp9_wb_write_bit(wb, 0); // has extra plane 1061 } 1062 1063 write_frame_size(cm, wb); 1064 } else { 1065 if (!cm->show_frame) 1066 vp9_wb_write_bit(wb, cm->intra_only); 1067 1068 if (!cm->error_resilient_mode) 1069 vp9_wb_write_literal(wb, cm->reset_frame_context, 2); 1070 1071 if (cm->intra_only) { 1072 write_sync_code(wb); 1073 1074 vp9_wb_write_literal(wb, get_refresh_mask(cpi), REF_FRAMES); 1075 write_frame_size(cm, wb); 1076 } else { 1077 MV_REFERENCE_FRAME ref_frame; 1078 vp9_wb_write_literal(wb, get_refresh_mask(cpi), REF_FRAMES); 1079 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { 1080 vp9_wb_write_literal(wb, get_ref_frame_idx(cpi, ref_frame), 1081 REF_FRAMES_LOG2); 1082 vp9_wb_write_bit(wb, cm->ref_frame_sign_bias[ref_frame]); 1083 } 1084 1085 write_frame_size_with_refs(cpi, wb); 1086 1087 vp9_wb_write_bit(wb, cm->allow_high_precision_mv); 1088 1089 fix_interp_filter(cm); 1090 write_interp_filter(cm->interp_filter, wb); 1091 } 1092 } 1093 1094 if (!cm->error_resilient_mode) { 1095 vp9_wb_write_bit(wb, cm->refresh_frame_context); 1096 vp9_wb_write_bit(wb, cm->frame_parallel_decoding_mode); 1097 } 1098 1099 vp9_wb_write_literal(wb, cm->frame_context_idx, FRAME_CONTEXTS_LOG2); 1100 1101 encode_loopfilter(&cm->lf, wb); 1102 encode_quantization(cm, wb); 1103 encode_segmentation(cpi, wb); 1104 1105 write_tile_info(cm, wb); 1106} 1107 1108static size_t write_compressed_header(VP9_COMP *cpi, uint8_t *data) { 1109 VP9_COMMON *const cm = &cpi->common; 1110 MACROBLOCKD *const xd = &cpi->mb.e_mbd; 1111 FRAME_CONTEXT *const fc = &cm->fc; 1112 vp9_writer header_bc; 1113 1114 vp9_start_encode(&header_bc, data); 1115 1116 if (xd->lossless) 1117 cm->tx_mode = ONLY_4X4; 1118 else 1119 encode_txfm_probs(cm, &header_bc); 1120 1121 update_coef_probs(cpi, &header_bc); 1122 update_skip_probs(cm, &header_bc); 1123 1124 if (!frame_is_intra_only(cm)) { 1125 int i; 1126 1127 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) 1128 prob_diff_update(vp9_inter_mode_tree, cm->fc.inter_mode_probs[i], 1129 cm->counts.inter_mode[i], INTER_MODES, &header_bc); 1130 1131 vp9_zero(cm->counts.inter_mode); 1132 1133 if (cm->interp_filter == SWITCHABLE) 1134 update_switchable_interp_probs(cm, &header_bc); 1135 1136 for (i = 0; i < INTRA_INTER_CONTEXTS; i++) 1137 vp9_cond_prob_diff_update(&header_bc, &fc->intra_inter_prob[i], 1138 cm->counts.intra_inter[i]); 1139 1140 if (cm->allow_comp_inter_inter) { 1141 const int use_compound_pred = cm->reference_mode != SINGLE_REFERENCE; 1142 const int use_hybrid_pred = cm->reference_mode == REFERENCE_MODE_SELECT; 1143 1144 vp9_write_bit(&header_bc, use_compound_pred); 1145 if (use_compound_pred) { 1146 vp9_write_bit(&header_bc, use_hybrid_pred); 1147 if (use_hybrid_pred) 1148 for (i = 0; i < COMP_INTER_CONTEXTS; i++) 1149 vp9_cond_prob_diff_update(&header_bc, &fc->comp_inter_prob[i], 1150 cm->counts.comp_inter[i]); 1151 } 1152 } 1153 1154 if (cm->reference_mode != COMPOUND_REFERENCE) { 1155 for (i = 0; i < REF_CONTEXTS; i++) { 1156 vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][0], 1157 cm->counts.single_ref[i][0]); 1158 vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][1], 1159 cm->counts.single_ref[i][1]); 1160 } 1161 } 1162 1163 if (cm->reference_mode != SINGLE_REFERENCE) 1164 for (i = 0; i < REF_CONTEXTS; i++) 1165 vp9_cond_prob_diff_update(&header_bc, &fc->comp_ref_prob[i], 1166 cm->counts.comp_ref[i]); 1167 1168 for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) 1169 prob_diff_update(vp9_intra_mode_tree, cm->fc.y_mode_prob[i], 1170 cm->counts.y_mode[i], INTRA_MODES, &header_bc); 1171 1172 for (i = 0; i < PARTITION_CONTEXTS; ++i) 1173 prob_diff_update(vp9_partition_tree, fc->partition_prob[i], 1174 cm->counts.partition[i], PARTITION_TYPES, &header_bc); 1175 1176 vp9_write_nmv_probs(cm, cm->allow_high_precision_mv, &header_bc); 1177 } 1178 1179 vp9_stop_encode(&header_bc); 1180 assert(header_bc.pos <= 0xffff); 1181 1182 return header_bc.pos; 1183} 1184 1185void vp9_pack_bitstream(VP9_COMP *cpi, uint8_t *dest, size_t *size) { 1186 uint8_t *data = dest; 1187 size_t first_part_size; 1188 struct vp9_write_bit_buffer wb = {data, 0}; 1189 struct vp9_write_bit_buffer saved_wb; 1190 1191 write_uncompressed_header(cpi, &wb); 1192 saved_wb = wb; 1193 vp9_wb_write_literal(&wb, 0, 16); // don't know in advance first part. size 1194 1195 data += vp9_rb_bytes_written(&wb); 1196 1197 vp9_compute_update_table(); 1198 1199 vp9_clear_system_state(); 1200 1201 first_part_size = write_compressed_header(cpi, data); 1202 data += first_part_size; 1203 // TODO(jbb): Figure out what to do if first_part_size > 16 bits. 1204 vp9_wb_write_literal(&saved_wb, (int)first_part_size, 16); 1205 1206 data += encode_tiles(cpi, data); 1207 1208 *size = data - dest; 1209} 1210 1211