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.
11 #include "vp8/common/header.h"
13 #include "vp8/common/entropymode.h"
14 #include "vp8/common/findnearmv.h"
16 #include "vp8/common/systemdependent.h"
20 #include "vpx/vpx_encoder.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "vpx_ports/system_state.h"
23 #include "bitstream.h"
25 #include "defaultcoefcounts.h"
26 #include "vp8/common/common.h"
28 const int vp8cx_base_skip_false_prob[128] = {
29 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
30 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
31 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
32 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251, 248, 244, 240,
33 236, 232, 229, 225, 221, 217, 213, 208, 204, 199, 194, 190, 187, 183, 179,
34 175, 172, 168, 164, 160, 157, 153, 149, 145, 142, 138, 134, 130, 127, 124,
35 120, 117, 114, 110, 107, 104, 101, 98, 95, 92, 89, 86, 83, 80, 77,
36 74, 71, 68, 65, 62, 59, 56, 53, 50, 47, 44, 41, 38, 35, 32,
37 30, 28, 26, 24, 22, 20, 18, 16,
40 #if defined(SECTIONBITS_OUTPUT)
41 unsigned __int64 Sectionbits[500];
44 #ifdef VP8_ENTROPY_STATS
45 int intra_mode_stats[10][10][10];
46 static unsigned int tree_update_hist[BLOCK_TYPES][COEF_BANDS]
47 [PREV_COEF_CONTEXTS][ENTROPY_NODES][2];
48 extern unsigned int active_section;
52 int count_mb_seg[4] = { 0, 0, 0, 0 };
55 static void update_mode(vp8_writer *const w, int n, vp8_token tok[/* n */],
56 vp8_tree tree, vp8_prob Pnew[/* n-1 */],
57 vp8_prob Pcur[/* n-1 */],
58 unsigned int bct[/* n-1 */][2],
59 const unsigned int num_events[/* n */]) {
60 unsigned int new_b = 0, old_b = 0;
63 vp8_tree_probs_from_distribution(n--, tok, tree, Pnew, bct, num_events, 256,
67 new_b += vp8_cost_branch(bct[i], Pnew[i]);
68 old_b += vp8_cost_branch(bct[i], Pcur[i]);
71 if (new_b + (n << 8) < old_b) {
77 const vp8_prob p = Pnew[j];
79 vp8_write_literal(w, Pcur[j] = p ? p : 1, 8);
85 static void update_mbintra_mode_probs(VP8_COMP *cpi) {
86 VP8_COMMON *const x = &cpi->common;
88 vp8_writer *const w = cpi->bc;
91 vp8_prob Pnew[VP8_YMODES - 1];
92 unsigned int bct[VP8_YMODES - 1][2];
94 update_mode(w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, Pnew,
95 x->fc.ymode_prob, bct, (unsigned int *)cpi->mb.ymode_count);
98 vp8_prob Pnew[VP8_UV_MODES - 1];
99 unsigned int bct[VP8_UV_MODES - 1][2];
101 update_mode(w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree, Pnew,
102 x->fc.uv_mode_prob, bct, (unsigned int *)cpi->mb.uv_mode_count);
106 static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
107 vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
110 static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
111 vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
114 static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p) {
115 vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
118 static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p) {
119 vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
122 static void write_split(vp8_writer *bc, int x) {
123 vp8_write_token(bc, vp8_mbsplit_tree, vp8_mbsplit_probs,
124 vp8_mbsplit_encodings + x);
127 void vp8_pack_tokens(vp8_writer *w, const TOKENEXTRA *p, int xcount) {
128 const TOKENEXTRA *stop = p + xcount;
131 int count = w->count;
132 unsigned int range = w->range;
133 unsigned int lowvalue = w->lowvalue;
136 const int t = p->Token;
137 vp8_token *a = vp8_coef_encodings + t;
138 const vp8_extra_bit_struct *b = vp8_extra_bits + t;
140 const unsigned char *pp = p->context_tree;
144 if (p->skip_eob_node) {
150 const int bb = (v >> --n) & 1;
151 split = 1 + (((range - 1) * pp[i >> 1]) >> 8);
152 i = vp8_coef_tree[i + bb];
156 range = range - split;
161 shift = vp8_norm[range];
166 int offset = shift - count;
168 if ((lowvalue << (offset - 1)) & 0x80000000) {
171 while (x >= 0 && w->buffer[x] == 0xff) {
172 w->buffer[x] = (unsigned char)0;
179 validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
181 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
184 lowvalue &= 0xffffff;
192 const int e = p->Extra, L = b->Len;
195 const unsigned char *proba = b->prob;
196 const int v2 = e >> 1;
197 int n2 = L; /* number of bits in v2, assumed nonzero */
201 const int bb = (v2 >> --n2) & 1;
202 split = 1 + (((range - 1) * proba[i >> 1]) >> 8);
207 range = range - split;
212 shift = vp8_norm[range];
217 int offset = shift - count;
219 if ((lowvalue << (offset - 1)) & 0x80000000) {
222 while (x >= 0 && w->buffer[x] == 0xff) {
223 w->buffer[x] = (unsigned char)0;
230 validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
232 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
235 lowvalue &= 0xffffff;
244 split = (range + 1) >> 1;
248 range = range - split;
255 if ((lowvalue & 0x80000000)) {
258 while (x >= 0 && w->buffer[x] == 0xff) {
259 w->buffer[x] = (unsigned char)0;
271 validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
273 w->buffer[w->pos++] = (lowvalue >> 24);
274 lowvalue &= 0xffffff;
283 w->lowvalue = lowvalue;
287 static void write_partition_size(unsigned char *cx_data, int size) {
292 csize = (size >> 8) & 0xff;
293 *(cx_data + 1) = csize;
294 csize = (size >> 16) & 0xff;
295 *(cx_data + 2) = csize;
298 static void pack_tokens_into_partitions(VP8_COMP *cpi, unsigned char *cx_data,
299 unsigned char *cx_data_end,
302 unsigned char *ptr = cx_data;
303 unsigned char *ptr_end = cx_data_end;
306 for (i = 0; i < num_part; ++i) {
311 vp8_start_encode(w, ptr, ptr_end);
313 for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part) {
314 const TOKENEXTRA *p = cpi->tplist[mb_row].start;
315 const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
316 int tokens = (int)(stop - p);
318 vp8_pack_tokens(w, p, tokens);
326 #if CONFIG_MULTITHREAD
327 static void pack_mb_row_tokens(VP8_COMP *cpi, vp8_writer *w) {
330 for (mb_row = 0; mb_row < cpi->common.mb_rows; ++mb_row) {
331 const TOKENEXTRA *p = cpi->tplist[mb_row].start;
332 const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
333 int tokens = (int)(stop - p);
335 vp8_pack_tokens(w, p, tokens);
338 #endif // CONFIG_MULTITHREAD
340 static void write_mv_ref(vp8_writer *w, MB_PREDICTION_MODE m,
342 assert(NEARESTMV <= m && m <= SPLITMV);
343 vp8_write_token(w, vp8_mv_ref_tree, p,
344 vp8_mv_ref_encoding_array + (m - NEARESTMV));
347 static void write_sub_mv_ref(vp8_writer *w, B_PREDICTION_MODE m,
349 assert(LEFT4X4 <= m && m <= NEW4X4);
350 vp8_write_token(w, vp8_sub_mv_ref_tree, p,
351 vp8_sub_mv_ref_encoding_array + (m - LEFT4X4));
354 static void write_mv(vp8_writer *w, const MV *mv, const int_mv *ref,
355 const MV_CONTEXT *mvc) {
357 e.row = mv->row - ref->as_mv.row;
358 e.col = mv->col - ref->as_mv.col;
360 vp8_encode_motion_vector(w, &e, mvc);
363 static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi,
364 const MACROBLOCKD *x) {
365 /* Encode the MB segment id. */
366 if (x->segmentation_enabled && x->update_mb_segmentation_map) {
367 switch (mi->segment_id) {
369 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
370 vp8_write(w, 0, x->mb_segment_tree_probs[1]);
373 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
374 vp8_write(w, 1, x->mb_segment_tree_probs[1]);
377 vp8_write(w, 1, x->mb_segment_tree_probs[0]);
378 vp8_write(w, 0, x->mb_segment_tree_probs[2]);
381 vp8_write(w, 1, x->mb_segment_tree_probs[0]);
382 vp8_write(w, 1, x->mb_segment_tree_probs[2]);
385 /* TRAP.. This should not happen */
387 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
388 vp8_write(w, 0, x->mb_segment_tree_probs[1]);
393 void vp8_convert_rfct_to_prob(VP8_COMP *const cpi) {
394 const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
395 const int rf_intra = rfct[INTRA_FRAME];
397 rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
399 /* Calculate the probabilities used to code the ref frame based on usage */
400 if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter))) {
401 cpi->prob_intra_coded = 1;
404 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
406 if (!cpi->prob_last_coded) cpi->prob_last_coded = 1;
408 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
409 ? (rfct[GOLDEN_FRAME] * 255) /
410 (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
413 if (!cpi->prob_gf_coded) cpi->prob_gf_coded = 1;
416 static void pack_inter_mode_mvs(VP8_COMP *const cpi) {
417 VP8_COMMON *const pc = &cpi->common;
418 vp8_writer *const w = cpi->bc;
419 const MV_CONTEXT *mvc = pc->fc.mvc;
421 MODE_INFO *m = pc->mi;
422 const int mis = pc->mode_info_stride;
425 int prob_skip_false = 0;
427 cpi->mb.partition_info = cpi->mb.pi;
429 vp8_convert_rfct_to_prob(cpi);
431 #ifdef VP8_ENTROPY_STATS
435 if (pc->mb_no_coeff_skip) {
436 int total_mbs = pc->mb_rows * pc->mb_cols;
438 prob_skip_false = (total_mbs - cpi->mb.skip_true_count) * 256 / total_mbs;
440 if (prob_skip_false <= 1) prob_skip_false = 1;
442 if (prob_skip_false > 255) prob_skip_false = 255;
444 cpi->prob_skip_false = prob_skip_false;
445 vp8_write_literal(w, prob_skip_false, 8);
448 vp8_write_literal(w, cpi->prob_intra_coded, 8);
449 vp8_write_literal(w, cpi->prob_last_coded, 8);
450 vp8_write_literal(w, cpi->prob_gf_coded, 8);
452 update_mbintra_mode_probs(cpi);
454 vp8_write_mvprobs(cpi);
456 while (++mb_row < pc->mb_rows) {
459 while (++mb_col < pc->mb_cols) {
460 const MB_MODE_INFO *const mi = &m->mbmi;
461 const MV_REFERENCE_FRAME rf = mi->ref_frame;
462 const MB_PREDICTION_MODE mode = mi->mode;
464 MACROBLOCKD *xd = &cpi->mb.e_mbd;
466 /* Distance of Mb to the various image edges.
467 * These specified to 8th pel as they are always compared to MV
468 * values that are in 1/8th pel units
470 xd->mb_to_left_edge = -((mb_col * 16) << 3);
471 xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
472 xd->mb_to_top_edge = -((mb_row * 16) << 3);
473 xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
475 #ifdef VP8_ENTROPY_STATS
479 if (cpi->mb.e_mbd.update_mb_segmentation_map) {
480 write_mb_features(w, mi, &cpi->mb.e_mbd);
483 if (pc->mb_no_coeff_skip) {
484 vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
487 if (rf == INTRA_FRAME) {
488 vp8_write(w, 0, cpi->prob_intra_coded);
489 #ifdef VP8_ENTROPY_STATS
492 write_ymode(w, mode, pc->fc.ymode_prob);
494 if (mode == B_PRED) {
498 write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
502 write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
503 } else /* inter coded */
506 vp8_prob mv_ref_p[VP8_MVREFS - 1];
508 vp8_write(w, 1, cpi->prob_intra_coded);
510 if (rf == LAST_FRAME)
511 vp8_write(w, 0, cpi->prob_last_coded);
513 vp8_write(w, 1, cpi->prob_last_coded);
514 vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded);
521 vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf,
522 cpi->common.ref_frame_sign_bias);
523 vp8_clamp_mv2(&best_mv, xd);
525 vp8_mv_ref_probs(mv_ref_p, ct);
527 #ifdef VP8_ENTROPY_STATS
528 accum_mv_refs(mode, ct);
532 #ifdef VP8_ENTROPY_STATS
536 write_mv_ref(w, mode, mv_ref_p);
538 switch (mode) /* new, split require MVs */
542 #ifdef VP8_ENTROPY_STATS
546 write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
553 ++count_mb_seg[mi->partitioning];
556 write_split(w, mi->partitioning);
559 B_PREDICTION_MODE blockmode;
561 const int *const L = vp8_mbsplits[mi->partitioning];
562 int k = -1; /* first block in subset j */
564 int_mv leftmv, abovemv;
566 blockmode = cpi->mb.partition_info->bmi[j].mode;
567 blockmv = cpi->mb.partition_info->bmi[j].mv;
568 while (j != L[++k]) {
571 leftmv.as_int = left_block_mv(m, k);
572 abovemv.as_int = above_block_mv(m, k, mis);
573 mv_contz = vp8_mv_cont(&leftmv, &abovemv);
575 write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2[mv_contz]);
577 if (blockmode == NEW4X4) {
578 #ifdef VP8_ENTROPY_STATS
581 write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *)mvc);
583 } while (++j < cpi->mb.partition_info->count);
591 cpi->mb.partition_info++;
594 ++m; /* skip L prediction border */
595 cpi->mb.partition_info++;
599 static void write_kfmodes(VP8_COMP *cpi) {
600 vp8_writer *const bc = cpi->bc;
601 const VP8_COMMON *const c = &cpi->common;
603 MODE_INFO *m = c->mi;
606 int prob_skip_false = 0;
608 if (c->mb_no_coeff_skip) {
609 int total_mbs = c->mb_rows * c->mb_cols;
611 prob_skip_false = (total_mbs - cpi->mb.skip_true_count) * 256 / total_mbs;
613 if (prob_skip_false <= 1) prob_skip_false = 1;
615 if (prob_skip_false >= 255) prob_skip_false = 255;
617 cpi->prob_skip_false = prob_skip_false;
618 vp8_write_literal(bc, prob_skip_false, 8);
621 while (++mb_row < c->mb_rows) {
624 while (++mb_col < c->mb_cols) {
625 const int ym = m->mbmi.mode;
627 if (cpi->mb.e_mbd.update_mb_segmentation_map) {
628 write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
631 if (c->mb_no_coeff_skip) {
632 vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
635 kfwrite_ymode(bc, ym, vp8_kf_ymode_prob);
638 const int mis = c->mode_info_stride;
642 const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
643 const B_PREDICTION_MODE L = left_block_mode(m, i);
644 const int bm = m->bmi[i].as_mode;
646 #ifdef VP8_ENTROPY_STATS
647 ++intra_mode_stats[A][L][bm];
650 write_bmode(bc, bm, vp8_kf_bmode_prob[A][L]);
654 write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob);
657 m++; /* skip L prediction border */
662 /* This function is used for debugging probability trees. */
663 static void print_prob_tree(vp8_prob
664 coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES])
666 /* print coef probability tree */
668 FILE* f = fopen("enc_tree_probs.txt", "a");
670 for (i = 0; i < BLOCK_TYPES; ++i)
673 for (j = 0; j < COEF_BANDS; ++j)
676 for (k = 0; k < PREV_COEF_CONTEXTS; ++k)
679 for (l = 0; l < ENTROPY_NODES; ++l)
682 (unsigned int)(coef_probs [i][j][k][l]));
695 static void sum_probs_over_prev_coef_context(
696 const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
699 for (i = 0; i < MAX_ENTROPY_TOKENS; ++i) {
700 for (j = 0; j < PREV_COEF_CONTEXTS; ++j) {
701 const unsigned int tmp = out[i];
702 out[i] += probs[j][i];
704 if (out[i] < tmp) out[i] = UINT_MAX;
709 static int prob_update_savings(const unsigned int *ct, const vp8_prob oldp,
710 const vp8_prob newp, const vp8_prob upd) {
711 const int old_b = vp8_cost_branch(ct, oldp);
712 const int new_b = vp8_cost_branch(ct, newp);
713 const int update_b = 8 + ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
715 return old_b - new_b - update_b;
718 static int independent_coef_context_savings(VP8_COMP *cpi) {
719 MACROBLOCK *const x = &cpi->mb;
726 unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = { 0 };
727 int prev_coef_savings[MAX_ENTROPY_TOKENS] = { 0 };
728 const unsigned int(*probs)[MAX_ENTROPY_TOKENS];
729 /* Calculate new probabilities given the constraint that
730 * they must be equal over the prev coef contexts
733 probs = (const unsigned int(*)[MAX_ENTROPY_TOKENS])x->coef_counts[i][j];
735 /* Reset to default probabilities at key frames */
736 if (cpi->common.frame_type == KEY_FRAME) {
737 probs = default_coef_counts[i][j];
740 sum_probs_over_prev_coef_context(probs, prev_coef_count_sum);
743 /* at every context */
745 /* calc probs and branch cts for this frame only */
746 int t = 0; /* token/prob index */
748 vp8_tree_probs_from_distribution(
749 MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
750 cpi->frame_coef_probs[i][j][k], cpi->frame_branch_ct[i][j][k],
751 prev_coef_count_sum, 256, 1);
754 const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
755 const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
756 const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
757 const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
758 const int s = prob_update_savings(ct, oldp, newp, upd);
760 if (cpi->common.frame_type != KEY_FRAME ||
761 (cpi->common.frame_type == KEY_FRAME && newp != oldp)) {
762 prev_coef_savings[t] += s;
764 } while (++t < ENTROPY_NODES);
765 } while (++k < PREV_COEF_CONTEXTS);
768 /* We only update probabilities if we can save bits, except
769 * for key frames where we have to update all probabilities
770 * to get the equal probabilities across the prev coef
773 if (prev_coef_savings[k] > 0 || cpi->common.frame_type == KEY_FRAME) {
774 savings += prev_coef_savings[k];
776 } while (++k < ENTROPY_NODES);
777 } while (++j < COEF_BANDS);
778 } while (++i < BLOCK_TYPES);
782 static int default_coef_context_savings(VP8_COMP *cpi) {
783 MACROBLOCK *const x = &cpi->mb;
791 /* at every context */
793 /* calc probs and branch cts for this frame only */
794 int t = 0; /* token/prob index */
796 vp8_tree_probs_from_distribution(
797 MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
798 cpi->frame_coef_probs[i][j][k], cpi->frame_branch_ct[i][j][k],
799 x->coef_counts[i][j][k], 256, 1);
802 const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
803 const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
804 const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
805 const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
806 const int s = prob_update_savings(ct, oldp, newp, upd);
811 } while (++t < ENTROPY_NODES);
812 } while (++k < PREV_COEF_CONTEXTS);
813 } while (++j < COEF_BANDS);
814 } while (++i < BLOCK_TYPES);
818 void vp8_calc_ref_frame_costs(int *ref_frame_cost, int prob_intra,
819 int prob_last, int prob_garf) {
820 assert(prob_intra >= 0);
821 assert(prob_intra <= 255);
822 assert(prob_last >= 0);
823 assert(prob_last <= 255);
824 assert(prob_garf >= 0);
825 assert(prob_garf <= 255);
826 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(prob_intra);
827 ref_frame_cost[LAST_FRAME] =
828 vp8_cost_one(prob_intra) + vp8_cost_zero(prob_last);
829 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(prob_intra) +
830 vp8_cost_one(prob_last) +
831 vp8_cost_zero(prob_garf);
832 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(prob_intra) +
833 vp8_cost_one(prob_last) +
834 vp8_cost_one(prob_garf);
837 int vp8_estimate_entropy_savings(VP8_COMP *cpi) {
840 const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
841 const int rf_intra = rfct[INTRA_FRAME];
843 rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
844 int new_intra, new_last, new_garf, oldtotal, newtotal;
845 int ref_frame_cost[MAX_REF_FRAMES];
847 vpx_clear_system_state();
849 if (cpi->common.frame_type != KEY_FRAME) {
850 if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter))) new_intra = 1;
852 new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
854 new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
855 ? (rfct[GOLDEN_FRAME] * 255) /
856 (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
859 vp8_calc_ref_frame_costs(ref_frame_cost, new_intra, new_last, new_garf);
861 newtotal = rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
862 rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
863 rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
864 rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
867 vp8_calc_ref_frame_costs(ref_frame_cost, cpi->prob_intra_coded,
868 cpi->prob_last_coded, cpi->prob_gf_coded);
870 oldtotal = rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
871 rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
872 rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
873 rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
875 savings += (oldtotal - newtotal) / 256;
878 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
879 savings += independent_coef_context_savings(cpi);
881 savings += default_coef_context_savings(cpi);
887 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
888 int vp8_update_coef_context(VP8_COMP *cpi) {
891 if (cpi->common.frame_type == KEY_FRAME) {
892 /* Reset to default counts/probabilities at key frames */
893 vp8_copy(cpi->mb.coef_counts, default_coef_counts);
896 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
897 savings += independent_coef_context_savings(cpi);
899 savings += default_coef_context_savings(cpi);
905 void vp8_update_coef_probs(VP8_COMP *cpi) {
907 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
908 vp8_writer *const w = cpi->bc;
912 vpx_clear_system_state();
919 int prev_coef_savings[ENTROPY_NODES] = { 0 };
920 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
921 for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
922 int t; /* token/prob index */
923 for (t = 0; t < ENTROPY_NODES; ++t) {
924 const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
925 const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
926 const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
927 const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
929 prev_coef_savings[t] += prob_update_savings(ct, oldp, newp, upd);
935 /* note: use result from vp8_estimate_entropy_savings, so no
936 * need to call vp8_tree_probs_from_distribution here.
939 /* at every context */
941 /* calc probs and branch cts for this frame only */
942 int t = 0; /* token/prob index */
945 const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
947 vp8_prob *Pold = cpi->common.fc.coef_probs[i][j][k] + t;
948 const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
950 int s = prev_coef_savings[t];
953 if (!(cpi->oxcf.error_resilient_mode &
954 VPX_ERROR_RESILIENT_PARTITIONS)) {
955 s = prob_update_savings(cpi->frame_branch_ct[i][j][k][t], *Pold,
961 /* Force updates on key frames if the new is different,
962 * so that we can be sure we end up with equal probabilities
963 * over the prev coef contexts.
965 if ((cpi->oxcf.error_resilient_mode &
966 VPX_ERROR_RESILIENT_PARTITIONS) &&
967 cpi->common.frame_type == KEY_FRAME && newp != *Pold) {
971 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
972 cpi->update_probs[i][j][k][t] = u;
974 vp8_write(w, u, upd);
977 #ifdef VP8_ENTROPY_STATS
978 ++tree_update_hist[i][j][k][t][u];
982 /* send/use new probability */
985 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
986 vp8_write_literal(w, newp, 8);
992 } while (++t < ENTROPY_NODES);
994 /* Accum token counts for generation of default statistics */
995 #ifdef VP8_ENTROPY_STATS
999 context_counters[i][j][k][t] += cpi->coef_counts[i][j][k][t];
1000 } while (++t < MAX_ENTROPY_TOKENS);
1004 } while (++k < PREV_COEF_CONTEXTS);
1005 } while (++j < COEF_BANDS);
1006 } while (++i < BLOCK_TYPES);
1009 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1010 static void pack_coef_probs(VP8_COMP *cpi) {
1012 vp8_writer *const w = cpi->bc;
1021 int t = 0; /* token/prob index */
1024 const vp8_prob newp = cpi->common.fc.coef_probs[i][j][k][t];
1025 const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
1027 const char u = cpi->update_probs[i][j][k][t];
1029 vp8_write(w, u, upd);
1032 /* send/use new probability */
1033 vp8_write_literal(w, newp, 8);
1035 } while (++t < ENTROPY_NODES);
1036 } while (++k < PREV_COEF_CONTEXTS);
1037 } while (++j < COEF_BANDS);
1038 } while (++i < BLOCK_TYPES);
1042 #ifdef PACKET_TESTING
1046 static void put_delta_q(vp8_writer *bc, int delta_q) {
1048 vp8_write_bit(bc, 1);
1049 vp8_write_literal(bc, abs(delta_q), 4);
1052 vp8_write_bit(bc, 1);
1054 vp8_write_bit(bc, 0);
1056 vp8_write_bit(bc, 0);
1059 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest,
1060 unsigned char *dest_end, size_t *size) {
1063 VP8_COMMON *const pc = &cpi->common;
1064 vp8_writer *const bc = cpi->bc;
1065 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1066 int extra_bytes_packed = 0;
1068 unsigned char *cx_data = dest;
1069 unsigned char *cx_data_end = dest_end;
1070 const int *mb_feature_data_bits;
1072 oh.show_frame = (int)pc->show_frame;
1073 oh.type = (int)pc->frame_type;
1074 oh.version = pc->version;
1075 oh.first_partition_length_in_bytes = 0;
1077 mb_feature_data_bits = vp8_mb_feature_data_bits;
1079 bc[0].error = &pc->error;
1081 validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error);
1084 #if defined(SECTIONBITS_OUTPUT)
1085 Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
1088 /* every keyframe send startcode, width, height, scale factor, clamp
1091 if (oh.type == KEY_FRAME) {
1094 validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error);
1096 /* Start / synch code */
1101 v = (pc->horiz_scale << 14) | pc->Width;
1103 cx_data[4] = v >> 8;
1105 v = (pc->vert_scale << 14) | pc->Height;
1107 cx_data[6] = v >> 8;
1109 extra_bytes_packed = 7;
1110 cx_data += extra_bytes_packed;
1112 vp8_start_encode(bc, cx_data, cx_data_end);
1114 /* signal clr type */
1115 vp8_write_bit(bc, 0);
1116 vp8_write_bit(bc, pc->clamp_type);
1119 vp8_start_encode(bc, cx_data, cx_data_end);
1122 /* Signal whether or not Segmentation is enabled */
1123 vp8_write_bit(bc, xd->segmentation_enabled);
1125 /* Indicate which features are enabled */
1126 if (xd->segmentation_enabled) {
1127 /* Signal whether or not the segmentation map is being updated. */
1128 vp8_write_bit(bc, xd->update_mb_segmentation_map);
1129 vp8_write_bit(bc, xd->update_mb_segmentation_data);
1131 if (xd->update_mb_segmentation_data) {
1134 vp8_write_bit(bc, xd->mb_segement_abs_delta);
1136 /* For each segmentation feature (Quant and loop filter level) */
1137 for (i = 0; i < MB_LVL_MAX; ++i) {
1138 /* For each of the segments */
1139 for (j = 0; j < MAX_MB_SEGMENTS; ++j) {
1140 Data = xd->segment_feature_data[i][j];
1142 /* Frame level data */
1144 vp8_write_bit(bc, 1);
1148 vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1149 vp8_write_bit(bc, 1);
1151 vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1152 vp8_write_bit(bc, 0);
1155 vp8_write_bit(bc, 0);
1160 if (xd->update_mb_segmentation_map) {
1161 /* Write the probs used to decode the segment id for each mb */
1162 for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) {
1163 int Data = xd->mb_segment_tree_probs[i];
1166 vp8_write_bit(bc, 1);
1167 vp8_write_literal(bc, Data, 8);
1169 vp8_write_bit(bc, 0);
1174 vp8_write_bit(bc, pc->filter_type);
1175 vp8_write_literal(bc, pc->filter_level, 6);
1176 vp8_write_literal(bc, pc->sharpness_level, 3);
1178 /* Write out loop filter deltas applied at the MB level based on mode
1179 * or ref frame (if they are enabled).
1181 vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled);
1183 if (xd->mode_ref_lf_delta_enabled) {
1184 /* Do the deltas need to be updated */
1186 xd->mode_ref_lf_delta_update || cpi->oxcf.error_resilient_mode;
1188 vp8_write_bit(bc, send_update);
1193 for (i = 0; i < MAX_REF_LF_DELTAS; ++i) {
1194 Data = xd->ref_lf_deltas[i];
1196 /* Frame level data */
1197 if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i] ||
1198 cpi->oxcf.error_resilient_mode) {
1199 xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
1200 vp8_write_bit(bc, 1);
1203 vp8_write_literal(bc, (Data & 0x3F), 6);
1204 vp8_write_bit(bc, 0); /* sign */
1207 vp8_write_literal(bc, (Data & 0x3F), 6);
1208 vp8_write_bit(bc, 1); /* sign */
1211 vp8_write_bit(bc, 0);
1215 for (i = 0; i < MAX_MODE_LF_DELTAS; ++i) {
1216 Data = xd->mode_lf_deltas[i];
1218 if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i] ||
1219 cpi->oxcf.error_resilient_mode) {
1220 xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
1221 vp8_write_bit(bc, 1);
1224 vp8_write_literal(bc, (Data & 0x3F), 6);
1225 vp8_write_bit(bc, 0); /* sign */
1228 vp8_write_literal(bc, (Data & 0x3F), 6);
1229 vp8_write_bit(bc, 1); /* sign */
1232 vp8_write_bit(bc, 0);
1237 /* signal here is multi token partition is enabled */
1238 vp8_write_literal(bc, pc->multi_token_partition, 2);
1240 /* Frame Qbaseline quantizer index */
1241 vp8_write_literal(bc, pc->base_qindex, 7);
1243 /* Transmit Dc, Second order and Uv quantizer delta information */
1244 put_delta_q(bc, pc->y1dc_delta_q);
1245 put_delta_q(bc, pc->y2dc_delta_q);
1246 put_delta_q(bc, pc->y2ac_delta_q);
1247 put_delta_q(bc, pc->uvdc_delta_q);
1248 put_delta_q(bc, pc->uvac_delta_q);
1250 /* When there is a key frame all reference buffers are updated using
1253 if (pc->frame_type != KEY_FRAME) {
1254 /* Should the GF or ARF be updated using the transmitted frame
1257 vp8_write_bit(bc, pc->refresh_golden_frame);
1258 vp8_write_bit(bc, pc->refresh_alt_ref_frame);
1260 /* If not being updated from current frame should either GF or ARF
1261 * be updated from another buffer
1263 if (!pc->refresh_golden_frame)
1264 vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
1266 if (!pc->refresh_alt_ref_frame)
1267 vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
1269 /* Indicate reference frame sign bias for Golden and ARF frames
1270 * (always 0 for last frame buffer)
1272 vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
1273 vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
1276 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1277 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
1278 if (pc->frame_type == KEY_FRAME) {
1279 pc->refresh_entropy_probs = 1;
1281 pc->refresh_entropy_probs = 0;
1286 vp8_write_bit(bc, pc->refresh_entropy_probs);
1288 if (pc->frame_type != KEY_FRAME) vp8_write_bit(bc, pc->refresh_last_frame);
1290 #ifdef VP8_ENTROPY_STATS
1292 if (pc->frame_type == INTER_FRAME)
1299 vpx_clear_system_state();
1301 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1302 pack_coef_probs(cpi);
1304 if (pc->refresh_entropy_probs == 0) {
1305 /* save a copy for later refresh */
1306 memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
1309 vp8_update_coef_probs(cpi);
1312 #ifdef VP8_ENTROPY_STATS
1316 /* Write out the mb_no_coeff_skip flag */
1317 vp8_write_bit(bc, pc->mb_no_coeff_skip);
1319 if (pc->frame_type == KEY_FRAME) {
1322 #ifdef VP8_ENTROPY_STATS
1326 pack_inter_mode_mvs(cpi);
1328 #ifdef VP8_ENTROPY_STATS
1333 vp8_stop_encode(bc);
1337 oh.first_partition_length_in_bytes = cpi->bc->pos;
1339 /* update frame tag */
1341 int v = (oh.first_partition_length_in_bytes << 5) | (oh.show_frame << 4) |
1342 (oh.version << 1) | oh.type;
1349 *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos;
1351 cpi->partition_sz[0] = (unsigned int)*size;
1353 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1355 const int num_part = (1 << pc->multi_token_partition);
1356 unsigned char *dp = cpi->partition_d[0] + cpi->partition_sz[0];
1359 /* write token part sizes (all but last) if more than 1 */
1360 validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0],
1363 cpi->partition_sz[0] += 3 * (num_part - 1);
1365 for (i = 1; i < num_part; ++i) {
1366 write_partition_size(dp, cpi->partition_sz[i]);
1371 if (!cpi->output_partition) {
1372 /* concatenate partition buffers */
1373 for (i = 0; i < num_part; ++i) {
1374 memmove(dp, cpi->partition_d[i + 1], cpi->partition_sz[i + 1]);
1375 cpi->partition_d[i + 1] = dp;
1376 dp += cpi->partition_sz[i + 1];
1380 /* update total size */
1382 for (i = 0; i < num_part + 1; ++i) {
1383 *size += cpi->partition_sz[i];
1387 if (pc->multi_token_partition != ONE_PARTITION) {
1388 int num_part = 1 << pc->multi_token_partition;
1390 /* partition size table at the end of first partition */
1391 cpi->partition_sz[0] += 3 * (num_part - 1);
1392 *size += 3 * (num_part - 1);
1394 validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end, &pc->error);
1396 for (i = 1; i < num_part + 1; ++i) {
1397 cpi->bc[i].error = &pc->error;
1400 pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1), cx_data_end,
1403 for (i = 1; i < num_part; ++i) {
1404 cpi->partition_sz[i] = cpi->bc[i].pos;
1405 write_partition_size(cx_data, cpi->partition_sz[i]);
1407 *size += cpi->partition_sz[i]; /* add to total */
1410 /* add last partition to total size */
1411 cpi->partition_sz[i] = cpi->bc[i].pos;
1412 *size += cpi->partition_sz[i];
1414 bc[1].error = &pc->error;
1416 vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end);
1418 #if CONFIG_MULTITHREAD
1419 if (cpi->b_multi_threaded) {
1420 pack_mb_row_tokens(cpi, &cpi->bc[1]);
1422 vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
1425 vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
1426 #endif // CONFIG_MULTITHREAD
1428 vp8_stop_encode(&cpi->bc[1]);
1430 *size += cpi->bc[1].pos;
1431 cpi->partition_sz[1] = cpi->bc[1].pos;
1436 #ifdef VP8_ENTROPY_STATS
1437 void print_tree_update_probs() {
1439 FILE *f = fopen("context.c", "a");
1441 fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
1443 "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] "
1444 "[PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
1446 for (i = 0; i < BLOCK_TYPES; ++i) {
1447 fprintf(f, " { \n");
1449 for (j = 0; j < COEF_BANDS; ++j) {
1452 for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
1455 for (l = 0; l < ENTROPY_NODES; ++l) {
1457 tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
1460 if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
1461 fprintf(f, "%3ld, ",
1462 (tree_update_hist[i][j][k][l][0] * 255) / Sum);
1464 fprintf(f, "%3ld, ", 1);
1466 fprintf(f, "%3ld, ", 128);
1472 fprintf(f, " },\n");
1475 fprintf(f, " },\n");