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.
12 #include "vp8/common/header.h"
14 #include "vp8/common/entropymode.h"
15 #include "vp8/common/findnearmv.h"
17 #include "vp8/common/systemdependent.h"
21 #include "vpx/vpx_encoder.h"
22 #include "vpx_mem/vpx_mem.h"
23 #include "bitstream.h"
25 #include "defaultcoefcounts.h"
26 #include "vp8/common/common.h"
28 const int vp8cx_base_skip_false_prob[128] =
30 255, 255, 255, 255, 255, 255, 255, 255,
31 255, 255, 255, 255, 255, 255, 255, 255,
32 255, 255, 255, 255, 255, 255, 255, 255,
33 255, 255, 255, 255, 255, 255, 255, 255,
34 255, 255, 255, 255, 255, 255, 255, 255,
35 255, 255, 255, 255, 255, 255, 255, 255,
36 255, 255, 255, 255, 255, 255, 255, 255,
37 251, 248, 244, 240, 236, 232, 229, 225,
38 221, 217, 213, 208, 204, 199, 194, 190,
39 187, 183, 179, 175, 172, 168, 164, 160,
40 157, 153, 149, 145, 142, 138, 134, 130,
41 127, 124, 120, 117, 114, 110, 107, 104,
42 101, 98, 95, 92, 89, 86, 83, 80,
43 77, 74, 71, 68, 65, 62, 59, 56,
44 53, 50, 47, 44, 41, 38, 35, 32,
45 30, 28, 26, 24, 22, 20, 18, 16,
48 #if defined(SECTIONBITS_OUTPUT)
49 unsigned __int64 Sectionbits[500];
52 #ifdef VP8_ENTROPY_STATS
53 int intra_mode_stats[10][10][10];
54 static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2];
55 extern unsigned int active_section;
59 int count_mb_seg[4] = { 0, 0, 0, 0 };
63 static void update_mode(
66 vp8_token tok [/* n */],
68 vp8_prob Pnew [/* n-1 */],
69 vp8_prob Pcur [/* n-1 */],
70 unsigned int bct [/* n-1 */] [2],
71 const unsigned int num_events[/* n */]
74 unsigned int new_b = 0, old_b = 0;
77 vp8_tree_probs_from_distribution(
79 Pnew, bct, num_events,
85 new_b += vp8_cost_branch(bct[i], Pnew[i]);
86 old_b += vp8_cost_branch(bct[i], Pcur[i]);
90 if (new_b + (n << 8) < old_b)
98 const vp8_prob p = Pnew[j];
100 vp8_write_literal(w, Pcur[j] = p ? p : 1, 8);
108 static void update_mbintra_mode_probs(VP8_COMP *cpi)
110 VP8_COMMON *const x = & cpi->common;
112 vp8_writer *const w = cpi->bc;
115 vp8_prob Pnew [VP8_YMODES-1];
116 unsigned int bct [VP8_YMODES-1] [2];
119 w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
120 Pnew, x->fc.ymode_prob, bct, (unsigned int *)cpi->mb.ymode_count
124 vp8_prob Pnew [VP8_UV_MODES-1];
125 unsigned int bct [VP8_UV_MODES-1] [2];
128 w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
129 Pnew, x->fc.uv_mode_prob, bct, (unsigned int *)cpi->mb.uv_mode_count
134 static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p)
136 vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
139 static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p)
141 vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
144 static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p)
146 vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
150 static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p)
152 vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
155 static void write_split(vp8_writer *bc, int x)
158 bc, vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + x
162 void vp8_pack_tokens(vp8_writer *w, const TOKENEXTRA *p, int xcount)
164 const TOKENEXTRA *stop = p + xcount;
167 int count = w->count;
168 unsigned int range = w->range;
169 unsigned int lowvalue = w->lowvalue;
173 const int t = p->Token;
174 vp8_token *a = vp8_coef_encodings + t;
175 const vp8_extra_bit_struct *b = vp8_extra_bits + t;
177 const unsigned char *pp = p->context_tree;
181 if (p->skip_eob_node)
189 const int bb = (v >> --n) & 1;
190 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
191 i = vp8_coef_tree[i+bb];
196 range = range - split;
203 shift = vp8_norm[range];
209 int offset = shift - count;
211 if ((lowvalue << (offset - 1)) & 0x80000000)
215 while (x >= 0 && w->buffer[x] == 0xff)
217 w->buffer[x] = (unsigned char)0;
224 validate_buffer(w->buffer + w->pos,
229 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
232 lowvalue &= 0xffffff;
243 const int e = p->Extra, L = b->Len;
247 const unsigned char *proba = b->prob;
248 const int v2 = e >> 1;
249 int n2 = L; /* number of bits in v2, assumed nonzero */
254 const int bb = (v2 >> --n2) & 1;
255 split = 1 + (((range - 1) * proba[i>>1]) >> 8);
261 range = range - split;
268 shift = vp8_norm[range];
274 int offset = shift - count;
276 if ((lowvalue << (offset - 1)) & 0x80000000)
280 while (x >= 0 && w->buffer[x] == 0xff)
282 w->buffer[x] = (unsigned char)0;
289 validate_buffer(w->buffer + w->pos,
294 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
297 lowvalue &= 0xffffff;
309 split = (range + 1) >> 1;
314 range = range - split;
323 if ((lowvalue & 0x80000000))
327 while (x >= 0 && w->buffer[x] == 0xff)
329 w->buffer[x] = (unsigned char)0;
343 validate_buffer(w->buffer + w->pos,
348 w->buffer[w->pos++] = (lowvalue >> 24);
349 lowvalue &= 0xffffff;
359 w->lowvalue = lowvalue;
364 static void write_partition_size(unsigned char *cx_data, int size)
370 csize = (size >> 8) & 0xff;
371 *(cx_data + 1) = csize;
372 csize = (size >> 16) & 0xff;
373 *(cx_data + 2) = csize;
377 static void pack_tokens_into_partitions(VP8_COMP *cpi, unsigned char *cx_data,
378 unsigned char * cx_data_end,
383 unsigned char *ptr = cx_data;
384 unsigned char *ptr_end = cx_data_end;
387 for (i = 0; i < num_part; i++)
393 vp8_start_encode(w, ptr, ptr_end);
395 for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part)
397 const TOKENEXTRA *p = cpi->tplist[mb_row].start;
398 const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
399 int tokens = (int)(stop - p);
401 vp8_pack_tokens(w, p, tokens);
410 #if CONFIG_MULTITHREAD
411 static void pack_mb_row_tokens(VP8_COMP *cpi, vp8_writer *w)
415 for (mb_row = 0; mb_row < cpi->common.mb_rows; mb_row++)
417 const TOKENEXTRA *p = cpi->tplist[mb_row].start;
418 const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
419 int tokens = (int)(stop - p);
421 vp8_pack_tokens(w, p, tokens);
425 #endif // CONFIG_MULTITHREAD
427 static void write_mv_ref
429 vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p
433 assert(NEARESTMV <= m && m <= SPLITMV);
435 vp8_write_token(w, vp8_mv_ref_tree, p,
436 vp8_mv_ref_encoding_array + (m - NEARESTMV));
439 static void write_sub_mv_ref
441 vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p
445 assert(LEFT4X4 <= m && m <= NEW4X4);
447 vp8_write_token(w, vp8_sub_mv_ref_tree, p,
448 vp8_sub_mv_ref_encoding_array + (m - LEFT4X4));
453 vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc
457 e.row = mv->row - ref->as_mv.row;
458 e.col = mv->col - ref->as_mv.col;
460 vp8_encode_motion_vector(w, &e, mvc);
463 static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi, const MACROBLOCKD *x)
465 /* Encode the MB segment id. */
466 if (x->segmentation_enabled && x->update_mb_segmentation_map)
468 switch (mi->segment_id)
471 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
472 vp8_write(w, 0, x->mb_segment_tree_probs[1]);
475 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
476 vp8_write(w, 1, x->mb_segment_tree_probs[1]);
479 vp8_write(w, 1, x->mb_segment_tree_probs[0]);
480 vp8_write(w, 0, x->mb_segment_tree_probs[2]);
483 vp8_write(w, 1, x->mb_segment_tree_probs[0]);
484 vp8_write(w, 1, x->mb_segment_tree_probs[2]);
487 /* TRAP.. This should not happen */
489 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
490 vp8_write(w, 0, x->mb_segment_tree_probs[1]);
495 void vp8_convert_rfct_to_prob(VP8_COMP *const cpi)
497 const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
498 const int rf_intra = rfct[INTRA_FRAME];
499 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
501 /* Calculate the probabilities used to code the ref frame based on usage */
502 if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter)))
503 cpi->prob_intra_coded = 1;
505 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
507 if (!cpi->prob_last_coded)
508 cpi->prob_last_coded = 1;
510 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
511 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
513 if (!cpi->prob_gf_coded)
514 cpi->prob_gf_coded = 1;
518 static void pack_inter_mode_mvs(VP8_COMP *const cpi)
520 VP8_COMMON *const pc = & cpi->common;
521 vp8_writer *const w = cpi->bc;
522 const MV_CONTEXT *mvc = pc->fc.mvc;
525 MODE_INFO *m = pc->mi;
526 const int mis = pc->mode_info_stride;
529 int prob_skip_false = 0;
531 cpi->mb.partition_info = cpi->mb.pi;
533 vp8_convert_rfct_to_prob(cpi);
535 #ifdef VP8_ENTROPY_STATS
539 if (pc->mb_no_coeff_skip)
541 int total_mbs = pc->mb_rows * pc->mb_cols;
543 prob_skip_false = (total_mbs - cpi->mb.skip_true_count ) * 256 / total_mbs;
545 if (prob_skip_false <= 1)
548 if (prob_skip_false > 255)
549 prob_skip_false = 255;
551 cpi->prob_skip_false = prob_skip_false;
552 vp8_write_literal(w, prob_skip_false, 8);
555 vp8_write_literal(w, cpi->prob_intra_coded, 8);
556 vp8_write_literal(w, cpi->prob_last_coded, 8);
557 vp8_write_literal(w, cpi->prob_gf_coded, 8);
559 update_mbintra_mode_probs(cpi);
561 vp8_write_mvprobs(cpi);
563 while (++mb_row < pc->mb_rows)
567 while (++mb_col < pc->mb_cols)
569 const MB_MODE_INFO *const mi = & m->mbmi;
570 const MV_REFERENCE_FRAME rf = mi->ref_frame;
571 const MB_PREDICTION_MODE mode = mi->mode;
573 MACROBLOCKD *xd = &cpi->mb.e_mbd;
575 /* Distance of Mb to the various image edges.
576 * These specified to 8th pel as they are always compared to MV
577 * values that are in 1/8th pel units
579 xd->mb_to_left_edge = -((mb_col * 16) << 3);
580 xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
581 xd->mb_to_top_edge = -((mb_row * 16) << 3);
582 xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
584 #ifdef VP8_ENTROPY_STATS
588 if (cpi->mb.e_mbd.update_mb_segmentation_map)
589 write_mb_features(w, mi, &cpi->mb.e_mbd);
591 if (pc->mb_no_coeff_skip)
592 vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
594 if (rf == INTRA_FRAME)
596 vp8_write(w, 0, cpi->prob_intra_coded);
597 #ifdef VP8_ENTROPY_STATS
600 write_ymode(w, mode, pc->fc.ymode_prob);
607 write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
611 write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
613 else /* inter coded */
616 vp8_prob mv_ref_p [VP8_MVREFS-1];
618 vp8_write(w, 1, cpi->prob_intra_coded);
620 if (rf == LAST_FRAME)
621 vp8_write(w, 0, cpi->prob_last_coded);
624 vp8_write(w, 1, cpi->prob_last_coded);
625 vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded);
632 vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias);
633 vp8_clamp_mv2(&best_mv, xd);
635 vp8_mv_ref_probs(mv_ref_p, ct);
637 #ifdef VP8_ENTROPY_STATS
638 accum_mv_refs(mode, ct);
643 #ifdef VP8_ENTROPY_STATS
647 write_mv_ref(w, mode, mv_ref_p);
649 switch (mode) /* new, split require MVs */
653 #ifdef VP8_ENTROPY_STATS
657 write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
665 ++count_mb_seg [mi->partitioning];
668 write_split(w, mi->partitioning);
672 B_PREDICTION_MODE blockmode;
674 const int *const L = vp8_mbsplits [mi->partitioning];
675 int k = -1; /* first block in subset j */
677 int_mv leftmv, abovemv;
679 blockmode = cpi->mb.partition_info->bmi[j].mode;
680 blockmv = cpi->mb.partition_info->bmi[j].mv;
688 leftmv.as_int = left_block_mv(m, k);
689 abovemv.as_int = above_block_mv(m, k, mis);
690 mv_contz = vp8_mv_cont(&leftmv, &abovemv);
692 write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2 [mv_contz]);
694 if (blockmode == NEW4X4)
696 #ifdef VP8_ENTROPY_STATS
699 write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *) mvc);
702 while (++j < cpi->mb.partition_info->count);
711 cpi->mb.partition_info++;
714 ++m; /* skip L prediction border */
715 cpi->mb.partition_info++;
720 static void write_kfmodes(VP8_COMP *cpi)
722 vp8_writer *const bc = cpi->bc;
723 const VP8_COMMON *const c = & cpi->common;
725 MODE_INFO *m = c->mi;
728 int prob_skip_false = 0;
730 if (c->mb_no_coeff_skip)
732 int total_mbs = c->mb_rows * c->mb_cols;
734 prob_skip_false = (total_mbs - cpi->mb.skip_true_count ) * 256 / total_mbs;
736 if (prob_skip_false <= 1)
739 if (prob_skip_false >= 255)
740 prob_skip_false = 255;
742 cpi->prob_skip_false = prob_skip_false;
743 vp8_write_literal(bc, prob_skip_false, 8);
746 while (++mb_row < c->mb_rows)
750 while (++mb_col < c->mb_cols)
752 const int ym = m->mbmi.mode;
754 if (cpi->mb.e_mbd.update_mb_segmentation_map)
755 write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
757 if (c->mb_no_coeff_skip)
758 vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
760 kfwrite_ymode(bc, ym, vp8_kf_ymode_prob);
764 const int mis = c->mode_info_stride;
769 const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
770 const B_PREDICTION_MODE L = left_block_mode(m, i);
771 const int bm = m->bmi[i].as_mode;
773 #ifdef VP8_ENTROPY_STATS
774 ++intra_mode_stats [A] [L] [bm];
777 write_bmode(bc, bm, vp8_kf_bmode_prob [A] [L]);
782 write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob);
785 m++; /* skip L prediction border */
790 /* This function is used for debugging probability trees. */
791 static void print_prob_tree(vp8_prob
792 coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES])
794 /* print coef probability tree */
796 FILE* f = fopen("enc_tree_probs.txt", "a");
798 for (i = 0; i < BLOCK_TYPES; i++)
801 for (j = 0; j < COEF_BANDS; j++)
804 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
807 for (l = 0; l < ENTROPY_NODES; l++)
810 (unsigned int)(coef_probs [i][j][k][l]));
823 static void sum_probs_over_prev_coef_context(
824 const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
828 for (i=0; i < MAX_ENTROPY_TOKENS; ++i)
830 for (j=0; j < PREV_COEF_CONTEXTS; ++j)
832 const unsigned int tmp = out[i];
833 out[i] += probs[j][i];
841 static int prob_update_savings(const unsigned int *ct,
842 const vp8_prob oldp, const vp8_prob newp,
845 const int old_b = vp8_cost_branch(ct, oldp);
846 const int new_b = vp8_cost_branch(ct, newp);
847 const int update_b = 8 +
848 ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
850 return old_b - new_b - update_b;
853 static int independent_coef_context_savings(VP8_COMP *cpi)
855 MACROBLOCK *const x = & cpi->mb;
864 unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = {0};
865 int prev_coef_savings[MAX_ENTROPY_TOKENS] = {0};
866 const unsigned int (*probs)[MAX_ENTROPY_TOKENS];
867 /* Calculate new probabilities given the constraint that
868 * they must be equal over the prev coef contexts
871 probs = (const unsigned int (*)[MAX_ENTROPY_TOKENS])
872 x->coef_counts[i][j];
874 /* Reset to default probabilities at key frames */
875 if (cpi->common.frame_type == KEY_FRAME)
876 probs = default_coef_counts[i][j];
878 sum_probs_over_prev_coef_context(probs, prev_coef_count_sum);
882 /* at every context */
884 /* calc probs and branch cts for this frame only */
885 int t = 0; /* token/prob index */
887 vp8_tree_probs_from_distribution(
888 MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
889 cpi->frame_coef_probs[i][j][k],
890 cpi->frame_branch_ct [i][j][k],
896 const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t];
897 const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
898 const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
899 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
900 const int s = prob_update_savings(ct, oldp, newp, upd);
902 if (cpi->common.frame_type != KEY_FRAME ||
903 (cpi->common.frame_type == KEY_FRAME && newp != oldp))
904 prev_coef_savings[t] += s;
906 while (++t < ENTROPY_NODES);
908 while (++k < PREV_COEF_CONTEXTS);
912 /* We only update probabilities if we can save bits, except
913 * for key frames where we have to update all probabilities
914 * to get the equal probabilities across the prev coef
917 if (prev_coef_savings[k] > 0 ||
918 cpi->common.frame_type == KEY_FRAME)
919 savings += prev_coef_savings[k];
921 while (++k < ENTROPY_NODES);
923 while (++j < COEF_BANDS);
925 while (++i < BLOCK_TYPES);
929 static int default_coef_context_savings(VP8_COMP *cpi)
931 MACROBLOCK *const x = & cpi->mb;
942 /* at every context */
944 /* calc probs and branch cts for this frame only */
945 int t = 0; /* token/prob index */
947 vp8_tree_probs_from_distribution(
948 MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
949 cpi->frame_coef_probs [i][j][k],
950 cpi->frame_branch_ct [i][j][k],
951 x->coef_counts [i][j][k],
957 const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t];
958 const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
959 const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
960 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
961 const int s = prob_update_savings(ct, oldp, newp, upd);
968 while (++t < ENTROPY_NODES);
970 while (++k < PREV_COEF_CONTEXTS);
972 while (++j < COEF_BANDS);
974 while (++i < BLOCK_TYPES);
978 void vp8_calc_ref_frame_costs(int *ref_frame_cost,
984 assert(prob_intra >= 0);
985 assert(prob_intra <= 255);
986 assert(prob_last >= 0);
987 assert(prob_last <= 255);
988 assert(prob_garf >= 0);
989 assert(prob_garf <= 255);
990 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(prob_intra);
991 ref_frame_cost[LAST_FRAME] = vp8_cost_one(prob_intra)
992 + vp8_cost_zero(prob_last);
993 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(prob_intra)
994 + vp8_cost_one(prob_last)
995 + vp8_cost_zero(prob_garf);
996 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(prob_intra)
997 + vp8_cost_one(prob_last)
998 + vp8_cost_one(prob_garf);
1002 int vp8_estimate_entropy_savings(VP8_COMP *cpi)
1006 const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
1007 const int rf_intra = rfct[INTRA_FRAME];
1008 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
1009 int new_intra, new_last, new_garf, oldtotal, newtotal;
1010 int ref_frame_cost[MAX_REF_FRAMES];
1012 vp8_clear_system_state();
1014 if (cpi->common.frame_type != KEY_FRAME)
1016 if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter)))
1019 new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
1021 new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
1022 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
1025 vp8_calc_ref_frame_costs(ref_frame_cost,new_intra,new_last,new_garf);
1028 rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
1029 rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
1030 rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
1031 rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
1035 vp8_calc_ref_frame_costs(ref_frame_cost,cpi->prob_intra_coded,
1036 cpi->prob_last_coded,cpi->prob_gf_coded);
1039 rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
1040 rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
1041 rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
1042 rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
1044 savings += (oldtotal - newtotal) / 256;
1048 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
1049 savings += independent_coef_context_savings(cpi);
1051 savings += default_coef_context_savings(cpi);
1057 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1058 int vp8_update_coef_context(VP8_COMP *cpi)
1063 if (cpi->common.frame_type == KEY_FRAME)
1065 /* Reset to default counts/probabilities at key frames */
1066 vp8_copy(cpi->mb.coef_counts, default_coef_counts);
1069 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
1070 savings += independent_coef_context_savings(cpi);
1072 savings += default_coef_context_savings(cpi);
1078 void vp8_update_coef_probs(VP8_COMP *cpi)
1081 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1082 vp8_writer *const w = cpi->bc;
1086 vp8_clear_system_state();
1095 int prev_coef_savings[ENTROPY_NODES] = {0};
1096 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
1098 for (k = 0; k < PREV_COEF_CONTEXTS; ++k)
1100 int t; /* token/prob index */
1101 for (t = 0; t < ENTROPY_NODES; ++t)
1103 const unsigned int *ct = cpi->frame_branch_ct [i][j]
1105 const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
1106 const vp8_prob oldp = cpi->common.fc.coef_probs[i][j]
1108 const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
1110 prev_coef_savings[t] +=
1111 prob_update_savings(ct, oldp, newp, upd);
1118 /* note: use result from vp8_estimate_entropy_savings, so no
1119 * need to call vp8_tree_probs_from_distribution here.
1122 /* at every context */
1124 /* calc probs and branch cts for this frame only */
1125 int t = 0; /* token/prob index */
1129 const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
1131 vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
1132 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
1134 int s = prev_coef_savings[t];
1137 if (!(cpi->oxcf.error_resilient_mode &
1138 VPX_ERROR_RESILIENT_PARTITIONS))
1140 s = prob_update_savings(
1141 cpi->frame_branch_ct [i][j][k][t],
1148 /* Force updates on key frames if the new is different,
1149 * so that we can be sure we end up with equal probabilities
1150 * over the prev coef contexts.
1152 if ((cpi->oxcf.error_resilient_mode &
1153 VPX_ERROR_RESILIENT_PARTITIONS) &&
1154 cpi->common.frame_type == KEY_FRAME && newp != *Pold)
1157 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1158 cpi->update_probs[i][j][k][t] = u;
1160 vp8_write(w, u, upd);
1164 #ifdef VP8_ENTROPY_STATS
1165 ++ tree_update_hist [i][j][k][t] [u];
1170 /* send/use new probability */
1173 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1174 vp8_write_literal(w, newp, 8);
1182 while (++t < ENTROPY_NODES);
1184 /* Accum token counts for generation of default statistics */
1185 #ifdef VP8_ENTROPY_STATS
1190 context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t];
1192 while (++t < MAX_ENTROPY_TOKENS);
1197 while (++k < PREV_COEF_CONTEXTS);
1199 while (++j < COEF_BANDS);
1201 while (++i < BLOCK_TYPES);
1205 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1206 static void pack_coef_probs(VP8_COMP *cpi)
1209 vp8_writer *const w = cpi->bc;
1221 int t = 0; /* token/prob index */
1225 const vp8_prob newp = cpi->common.fc.coef_probs [i][j][k][t];
1226 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
1228 const char u = cpi->update_probs[i][j][k][t] ;
1230 vp8_write(w, u, upd);
1234 /* send/use new probability */
1235 vp8_write_literal(w, newp, 8);
1238 while (++t < ENTROPY_NODES);
1240 while (++k < PREV_COEF_CONTEXTS);
1242 while (++j < COEF_BANDS);
1244 while (++i < BLOCK_TYPES);
1248 #ifdef PACKET_TESTING
1252 static void put_delta_q(vp8_writer *bc, int delta_q)
1256 vp8_write_bit(bc, 1);
1257 vp8_write_literal(bc, abs(delta_q), 4);
1260 vp8_write_bit(bc, 1);
1262 vp8_write_bit(bc, 0);
1265 vp8_write_bit(bc, 0);
1268 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned char * dest_end, unsigned long *size)
1272 VP8_COMMON *const pc = & cpi->common;
1273 vp8_writer *const bc = cpi->bc;
1274 MACROBLOCKD *const xd = & cpi->mb.e_mbd;
1275 int extra_bytes_packed = 0;
1277 unsigned char *cx_data = dest;
1278 unsigned char *cx_data_end = dest_end;
1279 const int *mb_feature_data_bits;
1281 oh.show_frame = (int) pc->show_frame;
1282 oh.type = (int)pc->frame_type;
1283 oh.version = pc->version;
1284 oh.first_partition_length_in_bytes = 0;
1286 mb_feature_data_bits = vp8_mb_feature_data_bits;
1288 bc[0].error = &pc->error;
1290 validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error);
1293 #if defined(SECTIONBITS_OUTPUT)
1294 Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
1297 /* every keyframe send startcode, width, height, scale factor, clamp
1300 if (oh.type == KEY_FRAME)
1304 validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error);
1306 /* Start / synch code */
1311 v = (pc->horiz_scale << 14) | pc->Width;
1313 cx_data[4] = v >> 8;
1315 v = (pc->vert_scale << 14) | pc->Height;
1317 cx_data[6] = v >> 8;
1320 extra_bytes_packed = 7;
1321 cx_data += extra_bytes_packed ;
1323 vp8_start_encode(bc, cx_data, cx_data_end);
1325 /* signal clr type */
1326 vp8_write_bit(bc, 0);
1327 vp8_write_bit(bc, pc->clamp_type);
1331 vp8_start_encode(bc, cx_data, cx_data_end);
1334 /* Signal whether or not Segmentation is enabled */
1335 vp8_write_bit(bc, xd->segmentation_enabled);
1337 /* Indicate which features are enabled */
1338 if (xd->segmentation_enabled)
1340 /* Signal whether or not the segmentation map is being updated. */
1341 vp8_write_bit(bc, xd->update_mb_segmentation_map);
1342 vp8_write_bit(bc, xd->update_mb_segmentation_data);
1344 if (xd->update_mb_segmentation_data)
1348 vp8_write_bit(bc, xd->mb_segement_abs_delta);
1350 /* For each segmentation feature (Quant and loop filter level) */
1351 for (i = 0; i < MB_LVL_MAX; i++)
1353 /* For each of the segments */
1354 for (j = 0; j < MAX_MB_SEGMENTS; j++)
1356 Data = xd->segment_feature_data[i][j];
1358 /* Frame level data */
1361 vp8_write_bit(bc, 1);
1366 vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1367 vp8_write_bit(bc, 1);
1371 vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1372 vp8_write_bit(bc, 0);
1376 vp8_write_bit(bc, 0);
1381 if (xd->update_mb_segmentation_map)
1383 /* Write the probs used to decode the segment id for each mb */
1384 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
1386 int Data = xd->mb_segment_tree_probs[i];
1390 vp8_write_bit(bc, 1);
1391 vp8_write_literal(bc, Data, 8);
1394 vp8_write_bit(bc, 0);
1399 vp8_write_bit(bc, pc->filter_type);
1400 vp8_write_literal(bc, pc->filter_level, 6);
1401 vp8_write_literal(bc, pc->sharpness_level, 3);
1403 /* Write out loop filter deltas applied at the MB level based on mode
1404 * or ref frame (if they are enabled).
1406 vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled);
1408 if (xd->mode_ref_lf_delta_enabled)
1410 /* Do the deltas need to be updated */
1411 int send_update = xd->mode_ref_lf_delta_update
1412 || cpi->oxcf.error_resilient_mode;
1414 vp8_write_bit(bc, send_update);
1420 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1422 Data = xd->ref_lf_deltas[i];
1424 /* Frame level data */
1425 if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i]
1426 || cpi->oxcf.error_resilient_mode)
1428 xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
1429 vp8_write_bit(bc, 1);
1433 vp8_write_literal(bc, (Data & 0x3F), 6);
1434 vp8_write_bit(bc, 0); /* sign */
1439 vp8_write_literal(bc, (Data & 0x3F), 6);
1440 vp8_write_bit(bc, 1); /* sign */
1444 vp8_write_bit(bc, 0);
1448 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1450 Data = xd->mode_lf_deltas[i];
1452 if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i]
1453 || cpi->oxcf.error_resilient_mode)
1455 xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
1456 vp8_write_bit(bc, 1);
1460 vp8_write_literal(bc, (Data & 0x3F), 6);
1461 vp8_write_bit(bc, 0); /* sign */
1466 vp8_write_literal(bc, (Data & 0x3F), 6);
1467 vp8_write_bit(bc, 1); /* sign */
1471 vp8_write_bit(bc, 0);
1476 /* signal here is multi token partition is enabled */
1477 vp8_write_literal(bc, pc->multi_token_partition, 2);
1479 /* Frame Qbaseline quantizer index */
1480 vp8_write_literal(bc, pc->base_qindex, 7);
1482 /* Transmit Dc, Second order and Uv quantizer delta information */
1483 put_delta_q(bc, pc->y1dc_delta_q);
1484 put_delta_q(bc, pc->y2dc_delta_q);
1485 put_delta_q(bc, pc->y2ac_delta_q);
1486 put_delta_q(bc, pc->uvdc_delta_q);
1487 put_delta_q(bc, pc->uvac_delta_q);
1489 /* When there is a key frame all reference buffers are updated using
1492 if (pc->frame_type != KEY_FRAME)
1494 /* Should the GF or ARF be updated using the transmitted frame
1497 vp8_write_bit(bc, pc->refresh_golden_frame);
1498 vp8_write_bit(bc, pc->refresh_alt_ref_frame);
1500 /* If not being updated from current frame should either GF or ARF
1501 * be updated from another buffer
1503 if (!pc->refresh_golden_frame)
1504 vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
1506 if (!pc->refresh_alt_ref_frame)
1507 vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
1509 /* Indicate reference frame sign bias for Golden and ARF frames
1510 * (always 0 for last frame buffer)
1512 vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
1513 vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
1516 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1517 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
1519 if (pc->frame_type == KEY_FRAME)
1520 pc->refresh_entropy_probs = 1;
1522 pc->refresh_entropy_probs = 0;
1526 vp8_write_bit(bc, pc->refresh_entropy_probs);
1528 if (pc->frame_type != KEY_FRAME)
1529 vp8_write_bit(bc, pc->refresh_last_frame);
1531 #ifdef VP8_ENTROPY_STATS
1533 if (pc->frame_type == INTER_FRAME)
1540 vp8_clear_system_state();
1542 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1543 pack_coef_probs(cpi);
1545 if (pc->refresh_entropy_probs == 0)
1547 /* save a copy for later refresh */
1548 memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
1551 vp8_update_coef_probs(cpi);
1554 #ifdef VP8_ENTROPY_STATS
1558 /* Write out the mb_no_coeff_skip flag */
1559 vp8_write_bit(bc, pc->mb_no_coeff_skip);
1561 if (pc->frame_type == KEY_FRAME)
1565 #ifdef VP8_ENTROPY_STATS
1571 pack_inter_mode_mvs(cpi);
1573 #ifdef VP8_ENTROPY_STATS
1578 vp8_stop_encode(bc);
1582 oh.first_partition_length_in_bytes = cpi->bc->pos;
1584 /* update frame tag */
1586 int v = (oh.first_partition_length_in_bytes << 5) |
1587 (oh.show_frame << 4) |
1596 *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos;
1598 cpi->partition_sz[0] = *size;
1600 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1602 const int num_part = (1 << pc->multi_token_partition);
1603 unsigned char * dp = cpi->partition_d[0] + cpi->partition_sz[0];
1607 /* write token part sizes (all but last) if more than 1 */
1608 validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0],
1611 cpi->partition_sz[0] += 3*(num_part-1);
1613 for(i = 1; i < num_part; i++)
1615 write_partition_size(dp, cpi->partition_sz[i]);
1620 if (!cpi->output_partition)
1622 /* concatenate partition buffers */
1623 for(i = 0; i < num_part; i++)
1625 memmove(dp, cpi->partition_d[i+1], cpi->partition_sz[i+1]);
1626 cpi->partition_d[i+1] = dp;
1627 dp += cpi->partition_sz[i+1];
1631 /* update total size */
1633 for(i = 0; i < num_part+1; i++)
1635 *size += cpi->partition_sz[i];
1639 if (pc->multi_token_partition != ONE_PARTITION)
1641 int num_part = 1 << pc->multi_token_partition;
1643 /* partition size table at the end of first partition */
1644 cpi->partition_sz[0] += 3 * (num_part - 1);
1645 *size += 3 * (num_part - 1);
1647 validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end,
1650 for(i = 1; i < num_part + 1; i++)
1652 cpi->bc[i].error = &pc->error;
1655 pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1),
1656 cx_data_end, num_part);
1658 for(i = 1; i < num_part; i++)
1660 cpi->partition_sz[i] = cpi->bc[i].pos;
1661 write_partition_size(cx_data, cpi->partition_sz[i]);
1663 *size += cpi->partition_sz[i]; /* add to total */
1666 /* add last partition to total size */
1667 cpi->partition_sz[i] = cpi->bc[i].pos;
1668 *size += cpi->partition_sz[i];
1672 bc[1].error = &pc->error;
1674 vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end);
1676 #if CONFIG_MULTITHREAD
1677 if (cpi->b_multi_threaded)
1678 pack_mb_row_tokens(cpi, &cpi->bc[1]);
1680 #endif // CONFIG_MULTITHREAD
1681 vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
1683 vp8_stop_encode(&cpi->bc[1]);
1685 *size += cpi->bc[1].pos;
1686 cpi->partition_sz[1] = cpi->bc[1].pos;
1691 #ifdef VP8_ENTROPY_STATS
1692 void print_tree_update_probs()
1695 FILE *f = fopen("context.c", "a");
1697 fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
1698 fprintf(f, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
1700 for (i = 0; i < BLOCK_TYPES; i++)
1702 fprintf(f, " { \n");
1704 for (j = 0; j < COEF_BANDS; j++)
1708 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
1712 for (l = 0; l < ENTROPY_NODES; l++)
1714 Sum = tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
1718 if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
1719 fprintf(f, "%3ld, ", (tree_update_hist[i][j][k][l][0] * 255) / Sum);
1721 fprintf(f, "%3ld, ", 1);
1724 fprintf(f, "%3ld, ", 128);
1730 fprintf(f, " },\n");
1733 fprintf(f, " },\n");