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 "vp8/common/pragmas.h"
22 #include "vpx/vpx_encoder.h"
23 #include "vpx_mem/vpx_mem.h"
24 #include "bitstream.h"
26 #include "defaultcoefcounts.h"
27 #include "vp8/common/common.h"
29 const int vp8cx_base_skip_false_prob[128] =
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 255, 255, 255, 255, 255, 255, 255, 255,
38 251, 248, 244, 240, 236, 232, 229, 225,
39 221, 217, 213, 208, 204, 199, 194, 190,
40 187, 183, 179, 175, 172, 168, 164, 160,
41 157, 153, 149, 145, 142, 138, 134, 130,
42 127, 124, 120, 117, 114, 110, 107, 104,
43 101, 98, 95, 92, 89, 86, 83, 80,
44 77, 74, 71, 68, 65, 62, 59, 56,
45 53, 50, 47, 44, 41, 38, 35, 32,
46 30, 28, 26, 24, 22, 20, 18, 16,
49 #if defined(SECTIONBITS_OUTPUT)
50 unsigned __int64 Sectionbits[500];
54 int intra_mode_stats[10][10][10];
55 static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2];
56 extern unsigned int active_section;
60 int count_mb_seg[4] = { 0, 0, 0, 0 };
64 static void update_mode(
67 vp8_token tok [/* n */],
69 vp8_prob Pnew [/* n-1 */],
70 vp8_prob Pcur [/* n-1 */],
71 unsigned int bct [/* n-1 */] [2],
72 const unsigned int num_events[/* n */]
75 unsigned int new_b = 0, old_b = 0;
78 vp8_tree_probs_from_distribution(
80 Pnew, bct, num_events,
86 new_b += vp8_cost_branch(bct[i], Pnew[i]);
87 old_b += vp8_cost_branch(bct[i], Pcur[i]);
91 if (new_b + (n << 8) < old_b)
99 const vp8_prob p = Pnew[i];
101 vp8_write_literal(w, Pcur[i] = p ? p : 1, 8);
109 static void update_mbintra_mode_probs(VP8_COMP *cpi)
111 VP8_COMMON *const x = & cpi->common;
113 vp8_writer *const w = cpi->bc;
116 vp8_prob Pnew [VP8_YMODES-1];
117 unsigned int bct [VP8_YMODES-1] [2];
120 w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
121 Pnew, x->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
125 vp8_prob Pnew [VP8_UV_MODES-1];
126 unsigned int bct [VP8_UV_MODES-1] [2];
129 w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
130 Pnew, x->fc.uv_mode_prob, bct, (unsigned int *)cpi->uv_mode_count
135 static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p)
137 vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
140 static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p)
142 vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
145 static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p)
147 vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
151 static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p)
153 vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
156 static void write_split(vp8_writer *bc, int x)
159 bc, vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + x
163 void vp8_pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount)
165 const TOKENEXTRA *stop = p + xcount;
168 int count = w->count;
169 unsigned int range = w->range;
170 unsigned int lowvalue = w->lowvalue;
174 const int t = p->Token;
175 const vp8_token *a = vp8_coef_encodings + t;
176 const vp8_extra_bit_struct *b = vp8_extra_bits + t;
178 const unsigned char *pp = p->context_tree;
182 if (p->skip_eob_node)
190 const int bb = (v >> --n) & 1;
191 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
192 i = vp8_coef_tree[i+bb];
197 range = range - split;
204 shift = vp8_norm[range];
210 int offset = shift - count;
212 if ((lowvalue << (offset - 1)) & 0x80000000)
216 while (x >= 0 && w->buffer[x] == 0xff)
218 w->buffer[x] = (unsigned char)0;
225 validate_buffer(w->buffer + w->pos,
230 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
233 lowvalue &= 0xffffff;
244 const int e = p->Extra, L = b->Len;
248 const unsigned char *pp = b->prob;
250 int n = L; /* number of bits in v, assumed nonzero */
255 const int bb = (v >> --n) & 1;
256 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
262 range = range - split;
269 shift = vp8_norm[range];
275 int offset = shift - count;
277 if ((lowvalue << (offset - 1)) & 0x80000000)
281 while (x >= 0 && w->buffer[x] == 0xff)
283 w->buffer[x] = (unsigned char)0;
290 validate_buffer(w->buffer + w->pos,
295 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
298 lowvalue &= 0xffffff;
310 split = (range + 1) >> 1;
315 range = range - split;
324 if ((lowvalue & 0x80000000))
328 while (x >= 0 && w->buffer[x] == 0xff)
330 w->buffer[x] = (unsigned char)0;
344 validate_buffer(w->buffer + w->pos,
349 w->buffer[w->pos++] = (lowvalue >> 24);
350 lowvalue &= 0xffffff;
360 w->lowvalue = lowvalue;
365 static void write_partition_size(unsigned char *cx_data, int size)
371 csize = (size >> 8) & 0xff;
372 *(cx_data + 1) = csize;
373 csize = (size >> 16) & 0xff;
374 *(cx_data + 2) = csize;
378 static void pack_tokens_into_partitions_c(VP8_COMP *cpi, unsigned char *cx_data,
379 unsigned char * cx_data_end,
384 unsigned char *ptr = cx_data;
385 unsigned char *ptr_end = cx_data_end;
388 for (i = 0; i < num_part; i++)
394 vp8_start_encode(w, ptr, ptr_end);
396 for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part)
398 const TOKENEXTRA *p = cpi->tplist[mb_row].start;
399 const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
400 int tokens = stop - p;
402 vp8_pack_tokens_c(w, p, tokens);
411 static void pack_mb_row_tokens_c(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 = stop - p;
421 vp8_pack_tokens_c(w, p, tokens);
426 static void write_mv_ref
428 vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p
432 assert(NEARESTMV <= m && m <= SPLITMV);
434 vp8_write_token(w, vp8_mv_ref_tree, p,
435 vp8_mv_ref_encoding_array - NEARESTMV + m);
438 static void write_sub_mv_ref
440 vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p
444 assert(LEFT4X4 <= m && m <= NEW4X4);
446 vp8_write_token(w, vp8_sub_mv_ref_tree, p,
447 vp8_sub_mv_ref_encoding_array - LEFT4X4 + m);
452 vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc
456 e.row = mv->row - ref->as_mv.row;
457 e.col = mv->col - ref->as_mv.col;
459 vp8_encode_motion_vector(w, &e, mvc);
462 static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi, const MACROBLOCKD *x)
464 // Encode the MB segment id.
465 if (x->segmentation_enabled && x->update_mb_segmentation_map)
467 switch (mi->segment_id)
470 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
471 vp8_write(w, 0, x->mb_segment_tree_probs[1]);
474 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
475 vp8_write(w, 1, x->mb_segment_tree_probs[1]);
478 vp8_write(w, 1, x->mb_segment_tree_probs[0]);
479 vp8_write(w, 0, x->mb_segment_tree_probs[2]);
482 vp8_write(w, 1, x->mb_segment_tree_probs[0]);
483 vp8_write(w, 1, x->mb_segment_tree_probs[2]);
486 // TRAP.. This should not happen
488 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
489 vp8_write(w, 0, x->mb_segment_tree_probs[1]);
494 void vp8_convert_rfct_to_prob(VP8_COMP *const cpi)
496 const int *const rfct = cpi->count_mb_ref_frame_usage;
497 const int rf_intra = rfct[INTRA_FRAME];
498 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
500 // Calculate the probabilities used to code the ref frame based on useage
501 if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter)))
502 cpi->prob_intra_coded = 1;
504 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
506 if (!cpi->prob_last_coded)
507 cpi->prob_last_coded = 1;
509 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
510 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
512 if (!cpi->prob_gf_coded)
513 cpi->prob_gf_coded = 1;
517 static void pack_inter_mode_mvs(VP8_COMP *const cpi)
519 VP8_COMMON *const pc = & cpi->common;
520 vp8_writer *const w = cpi->bc;
521 const MV_CONTEXT *mvc = pc->fc.mvc;
524 MODE_INFO *m = pc->mi;
525 const int mis = pc->mode_info_stride;
528 int prob_skip_false = 0;
530 cpi->mb.partition_info = cpi->mb.pi;
532 vp8_convert_rfct_to_prob(cpi);
538 if (pc->mb_no_coeff_skip)
540 int total_mbs = pc->mb_rows * pc->mb_cols;
542 prob_skip_false = (total_mbs - cpi->skip_true_count ) * 256 / total_mbs;
544 if (prob_skip_false <= 1)
547 if (prob_skip_false > 255)
548 prob_skip_false = 255;
550 cpi->prob_skip_false = prob_skip_false;
551 vp8_write_literal(w, prob_skip_false, 8);
554 vp8_write_literal(w, cpi->prob_intra_coded, 8);
555 vp8_write_literal(w, cpi->prob_last_coded, 8);
556 vp8_write_literal(w, cpi->prob_gf_coded, 8);
558 update_mbintra_mode_probs(cpi);
560 vp8_write_mvprobs(cpi);
562 while (++mb_row < pc->mb_rows)
566 while (++mb_col < pc->mb_cols)
568 const MB_MODE_INFO *const mi = & m->mbmi;
569 const MV_REFERENCE_FRAME rf = mi->ref_frame;
570 const MB_PREDICTION_MODE mode = mi->mode;
572 MACROBLOCKD *xd = &cpi->mb.e_mbd;
574 // Distance of Mb to the various image edges.
575 // These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
576 xd->mb_to_left_edge = -((mb_col * 16) << 3);
577 xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
578 xd->mb_to_top_edge = -((mb_row * 16)) << 3;
579 xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
585 if (cpi->mb.e_mbd.update_mb_segmentation_map)
586 write_mb_features(w, mi, &cpi->mb.e_mbd);
588 if (pc->mb_no_coeff_skip)
589 vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
591 if (rf == INTRA_FRAME)
593 vp8_write(w, 0, cpi->prob_intra_coded);
597 write_ymode(w, mode, pc->fc.ymode_prob);
604 write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
608 write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
610 else /* inter coded */
613 vp8_prob mv_ref_p [VP8_MVREFS-1];
615 vp8_write(w, 1, cpi->prob_intra_coded);
617 if (rf == LAST_FRAME)
618 vp8_write(w, 0, cpi->prob_last_coded);
621 vp8_write(w, 1, cpi->prob_last_coded);
622 vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded);
629 vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias);
630 vp8_clamp_mv2(&best_mv, xd);
632 vp8_mv_ref_probs(mv_ref_p, ct);
635 accum_mv_refs(mode, ct);
644 write_mv_ref(w, mode, mv_ref_p);
646 switch (mode) /* new, split require MVs */
654 write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
662 ++count_mb_seg [mi->partitioning];
665 write_split(w, mi->partitioning);
669 B_PREDICTION_MODE blockmode;
671 const int *const L = vp8_mbsplits [mi->partitioning];
672 int k = -1; /* first block in subset j */
674 int_mv leftmv, abovemv;
676 blockmode = cpi->mb.partition_info->bmi[j].mode;
677 blockmv = cpi->mb.partition_info->bmi[j].mv;
685 leftmv.as_int = left_block_mv(m, k);
686 abovemv.as_int = above_block_mv(m, k, mis);
687 mv_contz = vp8_mv_cont(&leftmv, &abovemv);
689 write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2 [mv_contz]);
691 if (blockmode == NEW4X4)
696 write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *) mvc);
699 while (++j < cpi->mb.partition_info->count);
708 cpi->mb.partition_info++;
711 ++m; /* skip L prediction border */
712 cpi->mb.partition_info++;
717 static void write_kfmodes(VP8_COMP *cpi)
719 vp8_writer *const bc = cpi->bc;
720 const VP8_COMMON *const c = & cpi->common;
722 MODE_INFO *m = c->mi;
725 int prob_skip_false = 0;
727 if (c->mb_no_coeff_skip)
729 int total_mbs = c->mb_rows * c->mb_cols;
731 prob_skip_false = (total_mbs - cpi->skip_true_count ) * 256 / total_mbs;
733 if (prob_skip_false <= 1)
736 if (prob_skip_false >= 255)
737 prob_skip_false = 255;
739 cpi->prob_skip_false = prob_skip_false;
740 vp8_write_literal(bc, prob_skip_false, 8);
743 while (++mb_row < c->mb_rows)
747 while (++mb_col < c->mb_cols)
749 const int ym = m->mbmi.mode;
751 if (cpi->mb.e_mbd.update_mb_segmentation_map)
752 write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
754 if (c->mb_no_coeff_skip)
755 vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
757 kfwrite_ymode(bc, ym, vp8_kf_ymode_prob);
761 const int mis = c->mode_info_stride;
766 const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
767 const B_PREDICTION_MODE L = left_block_mode(m, i);
768 const int bm = m->bmi[i].as_mode;
771 ++intra_mode_stats [A] [L] [bm];
774 write_bmode(bc, bm, vp8_kf_bmode_prob [A] [L]);
779 write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob);
782 m++; // skip L prediction border
787 /* This function is used for debugging probability trees. */
788 static void print_prob_tree(vp8_prob
789 coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES])
791 /* print coef probability tree */
793 FILE* f = fopen("enc_tree_probs.txt", "a");
795 for (i = 0; i < BLOCK_TYPES; i++)
798 for (j = 0; j < COEF_BANDS; j++)
801 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
804 for (l = 0; l < ENTROPY_NODES; l++)
807 (unsigned int)(coef_probs [i][j][k][l]));
820 static void sum_probs_over_prev_coef_context(
821 const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
825 for (i=0; i < MAX_ENTROPY_TOKENS; ++i)
827 for (j=0; j < PREV_COEF_CONTEXTS; ++j)
829 const unsigned int tmp = out[i];
830 out[i] += probs[j][i];
838 static int prob_update_savings(const unsigned int *ct,
839 const vp8_prob oldp, const vp8_prob newp,
842 const int old_b = vp8_cost_branch(ct, oldp);
843 const int new_b = vp8_cost_branch(ct, newp);
844 const int update_b = 8 +
845 ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
847 return old_b - new_b - update_b;
850 static int independent_coef_context_savings(VP8_COMP *cpi)
860 unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = {0};
861 int prev_coef_savings[MAX_ENTROPY_TOKENS] = {0};
862 const unsigned int (*probs)[MAX_ENTROPY_TOKENS];
863 /* Calculate new probabilities given the constraint that
864 * they must be equal over the prev coef contexts
867 probs = (const unsigned int (*)[MAX_ENTROPY_TOKENS])
868 cpi->coef_counts[i][j];
870 /* Reset to default probabilities at key frames */
871 if (cpi->common.frame_type == KEY_FRAME)
872 probs = default_coef_counts[i][j];
874 sum_probs_over_prev_coef_context(probs, prev_coef_count_sum);
878 /* at every context */
880 /* calc probs and branch cts for this frame only */
881 //vp8_prob new_p [ENTROPY_NODES];
882 //unsigned int branch_ct [ENTROPY_NODES] [2];
884 int t = 0; /* token/prob index */
886 vp8_tree_probs_from_distribution(
887 MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
888 cpi->frame_coef_probs[i][j][k],
889 cpi->frame_branch_ct [i][j][k],
895 const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t];
896 const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
897 const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
898 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
899 const int s = prob_update_savings(ct, oldp, newp, upd);
901 if (cpi->common.frame_type != KEY_FRAME ||
902 (cpi->common.frame_type == KEY_FRAME && newp != oldp))
903 prev_coef_savings[t] += s;
905 while (++t < ENTROPY_NODES);
907 while (++k < PREV_COEF_CONTEXTS);
911 /* We only update probabilities if we can save bits, except
912 * for key frames where we have to update all probabilities
913 * to get the equal probabilities across the prev coef
916 if (prev_coef_savings[k] > 0 ||
917 cpi->common.frame_type == KEY_FRAME)
918 savings += prev_coef_savings[k];
920 while (++k < ENTROPY_NODES);
922 while (++j < COEF_BANDS);
924 while (++i < BLOCK_TYPES);
928 static int default_coef_context_savings(VP8_COMP *cpi)
940 /* at every context */
942 /* calc probs and branch cts for this frame only */
943 //vp8_prob new_p [ENTROPY_NODES];
944 //unsigned int branch_ct [ENTROPY_NODES] [2];
946 int t = 0; /* token/prob index */
948 vp8_tree_probs_from_distribution(
949 MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
950 cpi->frame_coef_probs [i][j][k],
951 cpi->frame_branch_ct [i][j][k],
952 cpi->coef_counts [i][j][k],
958 const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t];
959 const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
960 const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
961 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
962 const int s = prob_update_savings(ct, oldp, newp, upd);
969 while (++t < ENTROPY_NODES);
971 while (++k < PREV_COEF_CONTEXTS);
973 while (++j < COEF_BANDS);
975 while (++i < BLOCK_TYPES);
979 void vp8_calc_ref_frame_costs(int *ref_frame_cost,
985 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(prob_intra);
986 ref_frame_cost[LAST_FRAME] = vp8_cost_one(prob_intra)
987 + vp8_cost_zero(prob_last);
988 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(prob_intra)
989 + vp8_cost_one(prob_last)
990 + vp8_cost_zero(prob_garf);
991 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(prob_intra)
992 + vp8_cost_one(prob_last)
993 + vp8_cost_one(prob_garf);
997 int vp8_estimate_entropy_savings(VP8_COMP *cpi)
1001 const int *const rfct = cpi->count_mb_ref_frame_usage;
1002 const int rf_intra = rfct[INTRA_FRAME];
1003 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
1004 int new_intra, new_last, new_garf, oldtotal, newtotal;
1005 int ref_frame_cost[MAX_REF_FRAMES];
1007 vp8_clear_system_state(); //__asm emms;
1009 if (cpi->common.frame_type != KEY_FRAME)
1011 if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter)))
1014 new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
1016 new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
1017 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
1020 vp8_calc_ref_frame_costs(ref_frame_cost,new_intra,new_last,new_garf);
1023 rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
1024 rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
1025 rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
1026 rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
1030 vp8_calc_ref_frame_costs(ref_frame_cost,cpi->prob_intra_coded,
1031 cpi->prob_last_coded,cpi->prob_gf_coded);
1034 rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
1035 rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
1036 rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
1037 rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
1039 savings += (oldtotal - newtotal) / 256;
1043 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
1044 savings += independent_coef_context_savings(cpi);
1046 savings += default_coef_context_savings(cpi);
1052 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1053 int vp8_update_coef_context(VP8_COMP *cpi)
1058 if (cpi->common.frame_type == KEY_FRAME)
1060 /* Reset to default counts/probabilities at key frames */
1061 vp8_copy(cpi->coef_counts, default_coef_counts);
1064 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
1065 savings += independent_coef_context_savings(cpi);
1067 savings += default_coef_context_savings(cpi);
1073 void vp8_update_coef_probs(VP8_COMP *cpi)
1076 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1077 vp8_writer *const w = cpi->bc;
1081 vp8_clear_system_state(); //__asm emms;
1090 int prev_coef_savings[ENTROPY_NODES] = {0};
1091 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
1093 for (k = 0; k < PREV_COEF_CONTEXTS; ++k)
1095 int t; /* token/prob index */
1096 for (t = 0; t < ENTROPY_NODES; ++t)
1098 const unsigned int *ct = cpi->frame_branch_ct [i][j]
1100 const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
1101 const vp8_prob oldp = cpi->common.fc.coef_probs[i][j]
1103 const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
1105 prev_coef_savings[t] +=
1106 prob_update_savings(ct, oldp, newp, upd);
1113 //note: use result from vp8_estimate_entropy_savings, so no need to call vp8_tree_probs_from_distribution here.
1114 /* at every context */
1116 /* calc probs and branch cts for this frame only */
1117 //vp8_prob new_p [ENTROPY_NODES];
1118 //unsigned int branch_ct [ENTROPY_NODES] [2];
1120 int t = 0; /* token/prob index */
1122 //vp8_tree_probs_from_distribution(
1123 // MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
1124 // new_p, branch_ct, (unsigned int *)cpi->coef_counts [i][j][k],
1130 const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
1132 vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
1133 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
1135 int s = prev_coef_savings[t];
1138 if (!(cpi->oxcf.error_resilient_mode &
1139 VPX_ERROR_RESILIENT_PARTITIONS))
1141 s = prob_update_savings(
1142 cpi->frame_branch_ct [i][j][k][t],
1149 /* Force updates on key frames if the new is different,
1150 * so that we can be sure we end up with equal probabilities
1151 * over the prev coef contexts.
1153 if ((cpi->oxcf.error_resilient_mode &
1154 VPX_ERROR_RESILIENT_PARTITIONS) &&
1155 cpi->common.frame_type == KEY_FRAME && newp != *Pold)
1158 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1159 cpi->update_probs[i][j][k][t] = u;
1161 vp8_write(w, u, upd);
1165 #ifdef ENTROPY_STATS
1166 ++ tree_update_hist [i][j][k][t] [u];
1171 /* send/use new probability */
1174 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1175 vp8_write_literal(w, newp, 8);
1183 while (++t < ENTROPY_NODES);
1185 /* Accum token counts for generation of default statistics */
1186 #ifdef ENTROPY_STATS
1191 context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t];
1193 while (++t < MAX_ENTROPY_TOKENS);
1198 while (++k < PREV_COEF_CONTEXTS);
1200 while (++j < COEF_BANDS);
1202 while (++i < BLOCK_TYPES);
1206 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1207 static void pack_coef_probs(VP8_COMP *cpi)
1210 vp8_writer *const w = cpi->bc;
1222 int t = 0; /* token/prob index */
1226 const vp8_prob newp = cpi->common.fc.coef_probs [i][j][k][t];
1227 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
1229 const char u = cpi->update_probs[i][j][k][t] ;
1231 vp8_write(w, u, upd);
1235 /* send/use new probability */
1236 vp8_write_literal(w, newp, 8);
1239 while (++t < ENTROPY_NODES);
1241 while (++k < PREV_COEF_CONTEXTS);
1243 while (++j < COEF_BANDS);
1245 while (++i < BLOCK_TYPES);
1249 #ifdef PACKET_TESTING
1253 static void put_delta_q(vp8_writer *bc, int delta_q)
1257 vp8_write_bit(bc, 1);
1258 vp8_write_literal(bc, abs(delta_q), 4);
1261 vp8_write_bit(bc, 1);
1263 vp8_write_bit(bc, 0);
1266 vp8_write_bit(bc, 0);
1269 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned char * dest_end, unsigned long *size)
1273 VP8_COMMON *const pc = & cpi->common;
1274 vp8_writer *const bc = cpi->bc;
1275 MACROBLOCKD *const xd = & cpi->mb.e_mbd;
1276 int extra_bytes_packed = 0;
1278 unsigned char *cx_data = dest;
1279 unsigned char *cx_data_end = dest_end;
1280 const int *mb_feature_data_bits;
1282 oh.show_frame = (int) pc->show_frame;
1283 oh.type = (int)pc->frame_type;
1284 oh.version = pc->version;
1285 oh.first_partition_length_in_bytes = 0;
1287 mb_feature_data_bits = vp8_mb_feature_data_bits;
1289 bc[0].error = &pc->error;
1291 validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error);
1294 #if defined(SECTIONBITS_OUTPUT)
1295 Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
1298 // every keyframe send startcode, width, height, scale factor, clamp and color type
1299 if (oh.type == KEY_FRAME)
1303 validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error);
1305 // Start / synch code
1310 v = (pc->horiz_scale << 14) | pc->Width;
1312 cx_data[4] = v >> 8;
1314 v = (pc->vert_scale << 14) | pc->Height;
1316 cx_data[6] = v >> 8;
1319 extra_bytes_packed = 7;
1320 cx_data += extra_bytes_packed ;
1322 vp8_start_encode(bc, cx_data, cx_data_end);
1325 vp8_write_bit(bc, pc->clr_type);
1326 vp8_write_bit(bc, pc->clamp_type);
1330 vp8_start_encode(bc, cx_data, cx_data_end);
1333 // Signal whether or not Segmentation is enabled
1334 vp8_write_bit(bc, xd->segmentation_enabled);
1336 // Indicate which features are enabled
1337 if (xd->segmentation_enabled)
1339 // Signal whether or not the segmentation map is being updated.
1340 vp8_write_bit(bc, xd->update_mb_segmentation_map);
1341 vp8_write_bit(bc, xd->update_mb_segmentation_data);
1343 if (xd->update_mb_segmentation_data)
1347 vp8_write_bit(bc, xd->mb_segement_abs_delta);
1349 // For each segmentation feature (Quant and loop filter level)
1350 for (i = 0; i < MB_LVL_MAX; i++)
1352 // For each of the segments
1353 for (j = 0; j < MAX_MB_SEGMENTS; j++)
1355 Data = xd->segment_feature_data[i][j];
1360 vp8_write_bit(bc, 1);
1365 vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1366 vp8_write_bit(bc, 1);
1370 vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1371 vp8_write_bit(bc, 0);
1375 vp8_write_bit(bc, 0);
1380 if (xd->update_mb_segmentation_map)
1382 // Write the probs used to decode the segment id for each macro block.
1383 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
1385 int Data = xd->mb_segment_tree_probs[i];
1389 vp8_write_bit(bc, 1);
1390 vp8_write_literal(bc, Data, 8);
1393 vp8_write_bit(bc, 0);
1398 // Code to determine whether or not to update the scan order.
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 or ref frame (if they are enabled).
1404 vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled);
1406 if (xd->mode_ref_lf_delta_enabled)
1408 // Do the deltas need to be updated
1409 int send_update = xd->mode_ref_lf_delta_update
1410 || cpi->oxcf.error_resilient_mode;
1412 vp8_write_bit(bc, send_update);
1418 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1420 Data = xd->ref_lf_deltas[i];
1423 if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i]
1424 || cpi->oxcf.error_resilient_mode)
1426 xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
1427 vp8_write_bit(bc, 1);
1431 vp8_write_literal(bc, (Data & 0x3F), 6);
1432 vp8_write_bit(bc, 0); // sign
1437 vp8_write_literal(bc, (Data & 0x3F), 6);
1438 vp8_write_bit(bc, 1); // sign
1442 vp8_write_bit(bc, 0);
1446 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1448 Data = xd->mode_lf_deltas[i];
1450 if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i]
1451 || cpi->oxcf.error_resilient_mode)
1453 xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
1454 vp8_write_bit(bc, 1);
1458 vp8_write_literal(bc, (Data & 0x3F), 6);
1459 vp8_write_bit(bc, 0); // sign
1464 vp8_write_literal(bc, (Data & 0x3F), 6);
1465 vp8_write_bit(bc, 1); // sign
1469 vp8_write_bit(bc, 0);
1474 //signal here is multi token partition is enabled
1475 vp8_write_literal(bc, pc->multi_token_partition, 2);
1477 // Frame Qbaseline quantizer index
1478 vp8_write_literal(bc, pc->base_qindex, 7);
1480 // Transmit Dc, Second order and Uv quantizer delta information
1481 put_delta_q(bc, pc->y1dc_delta_q);
1482 put_delta_q(bc, pc->y2dc_delta_q);
1483 put_delta_q(bc, pc->y2ac_delta_q);
1484 put_delta_q(bc, pc->uvdc_delta_q);
1485 put_delta_q(bc, pc->uvac_delta_q);
1487 // When there is a key frame all reference buffers are updated using the new key frame
1488 if (pc->frame_type != KEY_FRAME)
1490 // Should the GF or ARF be updated using the transmitted frame or buffer
1491 vp8_write_bit(bc, pc->refresh_golden_frame);
1492 vp8_write_bit(bc, pc->refresh_alt_ref_frame);
1494 // If not being updated from current frame should either GF or ARF be updated from another buffer
1495 if (!pc->refresh_golden_frame)
1496 vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
1498 if (!pc->refresh_alt_ref_frame)
1499 vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
1501 // Indicate reference frame sign bias for Golden and ARF frames (always 0 for last frame buffer)
1502 vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
1503 vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
1506 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1507 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
1509 if (pc->frame_type == KEY_FRAME)
1510 pc->refresh_entropy_probs = 1;
1512 pc->refresh_entropy_probs = 0;
1516 vp8_write_bit(bc, pc->refresh_entropy_probs);
1518 if (pc->frame_type != KEY_FRAME)
1519 vp8_write_bit(bc, pc->refresh_last_frame);
1521 #ifdef ENTROPY_STATS
1523 if (pc->frame_type == INTER_FRAME)
1530 vp8_clear_system_state(); //__asm emms;
1532 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1533 pack_coef_probs(cpi);
1535 if (pc->refresh_entropy_probs == 0)
1537 // save a copy for later refresh
1538 vpx_memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
1541 vp8_update_coef_probs(cpi);
1544 #ifdef ENTROPY_STATS
1548 // Write out the mb_no_coeff_skip flag
1549 vp8_write_bit(bc, pc->mb_no_coeff_skip);
1551 if (pc->frame_type == KEY_FRAME)
1555 #ifdef ENTROPY_STATS
1561 pack_inter_mode_mvs(cpi);
1563 #ifdef ENTROPY_STATS
1568 vp8_stop_encode(bc);
1572 oh.first_partition_length_in_bytes = cpi->bc->pos;
1574 /* update frame tag */
1576 int v = (oh.first_partition_length_in_bytes << 5) |
1577 (oh.show_frame << 4) |
1586 *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos;
1588 cpi->partition_sz[0] = *size;
1590 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1592 const int num_part = (1 << pc->multi_token_partition);
1593 unsigned char * dp = cpi->partition_d[0] + cpi->partition_sz[0];
1597 /* write token part sizes (all but last) if more than 1 */
1598 validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0],
1601 cpi->partition_sz[0] += 3*(num_part-1);
1603 for(i = 1; i < num_part; i++)
1605 write_partition_size(dp, cpi->partition_sz[i]);
1610 if (!cpi->output_partition)
1612 /* concatenate partition buffers */
1613 for(i = 0; i < num_part; i++)
1615 vpx_memmove(dp, cpi->partition_d[i+1], cpi->partition_sz[i+1]);
1616 cpi->partition_d[i+1] = dp;
1617 dp += cpi->partition_sz[i+1];
1621 /* update total size */
1623 for(i = 0; i < num_part+1; i++)
1625 *size += cpi->partition_sz[i];
1629 if (pc->multi_token_partition != ONE_PARTITION)
1631 int num_part = 1 << pc->multi_token_partition;
1633 /* partition size table at the end of first partition */
1634 cpi->partition_sz[0] += 3 * (num_part - 1);
1635 *size += 3 * (num_part - 1);
1637 validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end,
1640 for(i = 1; i < num_part + 1; i++)
1642 cpi->bc[i].error = &pc->error;
1645 pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1),
1646 cx_data_end, num_part);
1648 for(i = 1; i < num_part; i++)
1650 cpi->partition_sz[i] = cpi->bc[i].pos;
1651 write_partition_size(cx_data, cpi->partition_sz[i]);
1653 *size += cpi->partition_sz[i]; /* add to total */
1656 /* add last partition to total size */
1657 cpi->partition_sz[i] = cpi->bc[i].pos;
1658 *size += cpi->partition_sz[i];
1662 bc[1].error = &pc->error;
1664 vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end);
1666 #if CONFIG_MULTITHREAD
1667 if (cpi->b_multi_threaded)
1668 pack_mb_row_tokens(cpi, &cpi->bc[1]);
1671 pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
1673 vp8_stop_encode(&cpi->bc[1]);
1675 *size += cpi->bc[1].pos;
1676 cpi->partition_sz[1] = cpi->bc[1].pos;
1681 #ifdef ENTROPY_STATS
1682 void print_tree_update_probs()
1685 FILE *f = fopen("context.c", "a");
1687 fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
1688 fprintf(f, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
1690 for (i = 0; i < BLOCK_TYPES; i++)
1692 fprintf(f, " { \n");
1694 for (j = 0; j < COEF_BANDS; j++)
1698 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
1702 for (l = 0; l < ENTROPY_NODES; l++)
1704 Sum = tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
1708 if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
1709 fprintf(f, "%3ld, ", (tree_update_hist[i][j][k][l][0] * 255) / Sum);
1711 fprintf(f, "%3ld, ", 1);
1714 fprintf(f, "%3ld, ", 128);
1720 fprintf(f, " },\n");
1723 fprintf(f, " },\n");