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"
20 #include "vp8/common/pragmas.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "bitstream.h"
24 const int vp8cx_base_skip_false_prob
[128] =
26 255, 255, 255, 255, 255, 255, 255, 255,
27 255, 255, 255, 255, 255, 255, 255, 255,
28 255, 255, 255, 255, 255, 255, 255, 255,
29 255, 255, 255, 255, 255, 255, 255, 255,
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 251, 248, 244, 240, 236, 232, 229, 225,
34 221, 217, 213, 208, 204, 199, 194, 190,
35 187, 183, 179, 175, 172, 168, 164, 160,
36 157, 153, 149, 145, 142, 138, 134, 130,
37 127, 124, 120, 117, 114, 110, 107, 104,
38 101, 98, 95, 92, 89, 86, 83, 80,
39 77, 74, 71, 68, 65, 62, 59, 56,
40 53, 50, 47, 44, 41, 38, 35, 32,
41 30, 28, 26, 24, 22, 20, 18, 16,
44 #define __int64 long long
47 #if defined(SECTIONBITS_OUTPUT)
48 unsigned __int64 Sectionbits
[500];
52 int intra_mode_stats
[10][10][10];
53 static unsigned int tree_update_hist
[BLOCK_TYPES
] [COEF_BANDS
] [PREV_COEF_CONTEXTS
] [vp8_coef_tokens
-1] [2];
54 extern unsigned int active_section
;
58 int count_mb_seg
[4] = { 0, 0, 0, 0 };
62 static void update_mode(
65 vp8_token tok
[/* n */],
67 vp8_prob Pnew
[/* n-1 */],
68 vp8_prob Pcur
[/* n-1 */],
69 unsigned int bct
[/* n-1 */] [2],
70 const unsigned int num_events
[/* n */]
73 unsigned int new_b
= 0, old_b
= 0;
76 vp8_tree_probs_from_distribution(
78 Pnew
, bct
, num_events
,
84 new_b
+= vp8_cost_branch(bct
[i
], Pnew
[i
]);
85 old_b
+= vp8_cost_branch(bct
[i
], Pcur
[i
]);
89 if (new_b
+ (n
<< 8) < old_b
)
97 const vp8_prob p
= Pnew
[i
];
99 vp8_write_literal(w
, Pcur
[i
] = p
? p
: 1, 8);
107 static void update_mbintra_mode_probs(VP8_COMP
*cpi
)
109 VP8_COMMON
*const x
= & cpi
->common
;
111 vp8_writer
*const w
= & cpi
->bc
;
114 vp8_prob Pnew
[VP8_YMODES
-1];
115 unsigned int bct
[VP8_YMODES
-1] [2];
118 w
, VP8_YMODES
, vp8_ymode_encodings
, vp8_ymode_tree
,
119 Pnew
, x
->fc
.ymode_prob
, bct
, (unsigned int *)cpi
->ymode_count
123 vp8_prob Pnew
[VP8_UV_MODES
-1];
124 unsigned int bct
[VP8_UV_MODES
-1] [2];
127 w
, VP8_UV_MODES
, vp8_uv_mode_encodings
, vp8_uv_mode_tree
,
128 Pnew
, x
->fc
.uv_mode_prob
, bct
, (unsigned int *)cpi
->uv_mode_count
133 static void write_ymode(vp8_writer
*bc
, int m
, const vp8_prob
*p
)
135 vp8_write_token(bc
, vp8_ymode_tree
, p
, vp8_ymode_encodings
+ m
);
138 static void kfwrite_ymode(vp8_writer
*bc
, int m
, const vp8_prob
*p
)
140 vp8_write_token(bc
, vp8_kf_ymode_tree
, p
, vp8_kf_ymode_encodings
+ m
);
143 static void write_uv_mode(vp8_writer
*bc
, int m
, const vp8_prob
*p
)
145 vp8_write_token(bc
, vp8_uv_mode_tree
, p
, vp8_uv_mode_encodings
+ m
);
149 static void write_bmode(vp8_writer
*bc
, int m
, const vp8_prob
*p
)
151 vp8_write_token(bc
, vp8_bmode_tree
, p
, vp8_bmode_encodings
+ m
);
154 static void write_split(vp8_writer
*bc
, int x
)
157 bc
, vp8_mbsplit_tree
, vp8_mbsplit_probs
, vp8_mbsplit_encodings
+ x
161 static const unsigned int norm
[256] =
163 0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
164 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
165 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
166 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
173 static void pack_tokens_c(vp8_writer
*w
, const TOKENEXTRA
*p
, int xcount
)
175 const TOKENEXTRA
*const stop
= p
+ xcount
;
178 int count
= w
->count
;
179 unsigned int range
= w
->range
;
180 unsigned int lowvalue
= w
->lowvalue
;
184 const int t
= p
->Token
;
185 vp8_token
*const a
= vp8_coef_encodings
+ t
;
186 const vp8_extra_bit_struct
*const b
= vp8_extra_bits
+ t
;
188 const unsigned char *pp
= p
->context_tree
;
192 if (p
->skip_eob_node
)
200 const int bb
= (v
>> --n
) & 1;
201 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
202 i
= vp8_coef_tree
[i
+bb
];
207 range
= range
- split
;
220 int offset
= shift
- count
;
222 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
226 while (x
>= 0 && w
->buffer
[x
] == 0xff)
228 w
->buffer
[x
] = (unsigned char)0;
235 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
238 lowvalue
&= 0xffffff;
249 const int e
= p
->Extra
, L
= b
->Len
;
253 const unsigned char *pp
= b
->prob
;
255 int n
= L
; /* number of bits in v, assumed nonzero */
260 const int bb
= (v
>> --n
) & 1;
261 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
267 range
= range
- split
;
280 int offset
= shift
- count
;
282 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
286 while (x
>= 0 && w
->buffer
[x
] == 0xff)
288 w
->buffer
[x
] = (unsigned char)0;
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;
343 w
->buffer
[w
->pos
++] = (lowvalue
>> 24);
344 lowvalue
&= 0xffffff;
354 w
->lowvalue
= lowvalue
;
359 static void write_partition_size(unsigned char *cx_data
, int size
)
365 csize
= (size
>> 8) & 0xff;
366 *(cx_data
+ 1) = csize
;
367 csize
= (size
>> 16) & 0xff;
368 *(cx_data
+ 2) = csize
;
372 static void pack_tokens_into_partitions_c(VP8_COMP
*cpi
, unsigned char *cx_data
, int num_part
, int *size
)
376 unsigned char *ptr
= cx_data
;
378 vp8_writer
*w
= &cpi
->bc2
;
379 *size
= 3 * (num_part
- 1);
380 ptr
= cx_data
+ (*size
);
382 for (i
= 0; i
< num_part
; i
++)
384 vp8_start_encode(w
, ptr
);
387 int count
= w
->count
;
388 unsigned int range
= w
->range
;
389 unsigned int lowvalue
= w
->lowvalue
;
392 for (mb_row
= i
; mb_row
< cpi
->common
.mb_rows
; mb_row
+= num_part
)
394 TOKENEXTRA
*p
= cpi
->tplist
[mb_row
].start
;
395 TOKENEXTRA
*stop
= cpi
->tplist
[mb_row
].stop
;
399 const int t
= p
->Token
;
400 vp8_token
*const a
= vp8_coef_encodings
+ t
;
401 const vp8_extra_bit_struct
*const b
= vp8_extra_bits
+ t
;
403 const unsigned char *pp
= p
->context_tree
;
407 if (p
->skip_eob_node
)
415 const int bb
= (v
>> --n
) & 1;
416 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
417 i
= vp8_coef_tree
[i
+bb
];
422 range
= range
- split
;
435 int offset
= shift
- count
;
437 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
441 while (x
>= 0 && w
->buffer
[x
] == 0xff)
443 w
->buffer
[x
] = (unsigned char)0;
450 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
453 lowvalue
&= 0xffffff;
464 const int e
= p
->Extra
, L
= b
->Len
;
468 const unsigned char *pp
= b
->prob
;
470 int n
= L
; /* number of bits in v, assumed nonzero */
475 const int bb
= (v
>> --n
) & 1;
476 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
482 range
= range
- split
;
495 int offset
= shift
- count
;
497 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
501 while (x
>= 0 && w
->buffer
[x
] == 0xff)
503 w
->buffer
[x
] = (unsigned char)0;
510 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
513 lowvalue
&= 0xffffff;
523 split
= (range
+ 1) >> 1;
528 range
= range
- split
;
537 if ((lowvalue
& 0x80000000))
541 while (x
>= 0 && w
->buffer
[x
] == 0xff)
543 w
->buffer
[x
] = (unsigned char)0;
556 w
->buffer
[w
->pos
++] = (lowvalue
>> 24);
557 lowvalue
&= 0xffffff;
568 w
->lowvalue
= lowvalue
;
576 if (i
< (num_part
- 1))
578 write_partition_size(cx_data
, w
->pos
);
586 static void pack_mb_row_tokens_c(VP8_COMP
*cpi
, vp8_writer
*w
)
590 int count
= w
->count
;
591 unsigned int range
= w
->range
;
592 unsigned int lowvalue
= w
->lowvalue
;
596 for (mb_row
= 0; mb_row
< cpi
->common
.mb_rows
; mb_row
++)
598 TOKENEXTRA
*p
= cpi
->tplist
[mb_row
].start
;
599 TOKENEXTRA
*stop
= cpi
->tplist
[mb_row
].stop
;
603 const int t
= p
->Token
;
604 vp8_token
*const a
= vp8_coef_encodings
+ t
;
605 const vp8_extra_bit_struct
*const b
= vp8_extra_bits
+ t
;
607 const unsigned char *pp
= p
->context_tree
;
611 if (p
->skip_eob_node
)
619 const int bb
= (v
>> --n
) & 1;
620 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
621 i
= vp8_coef_tree
[i
+bb
];
626 range
= range
- split
;
639 int offset
= shift
- count
;
641 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
645 while (x
>= 0 && w
->buffer
[x
] == 0xff)
647 w
->buffer
[x
] = (unsigned char)0;
654 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
657 lowvalue
&= 0xffffff;
668 const int e
= p
->Extra
, L
= b
->Len
;
672 const unsigned char *pp
= b
->prob
;
674 int n
= L
; /* number of bits in v, assumed nonzero */
679 const int bb
= (v
>> --n
) & 1;
680 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
686 range
= range
- split
;
699 int offset
= shift
- count
;
701 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
705 while (x
>= 0 && w
->buffer
[x
] == 0xff)
707 w
->buffer
[x
] = (unsigned char)0;
714 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
717 lowvalue
&= 0xffffff;
727 split
= (range
+ 1) >> 1;
732 range
= range
- split
;
741 if ((lowvalue
& 0x80000000))
745 while (x
>= 0 && w
->buffer
[x
] == 0xff)
747 w
->buffer
[x
] = (unsigned char)0;
760 w
->buffer
[w
->pos
++] = (lowvalue
>> 24);
761 lowvalue
&= 0xffffff;
772 w
->lowvalue
= lowvalue
;
777 static void write_mv_ref
779 vp8_writer
*w
, MB_PREDICTION_MODE m
, const vp8_prob
*p
783 assert(NEARESTMV
<= m
&& m
<= SPLITMV
);
785 vp8_write_token(w
, vp8_mv_ref_tree
, p
,
786 vp8_mv_ref_encoding_array
- NEARESTMV
+ m
);
789 static void write_sub_mv_ref
791 vp8_writer
*w
, B_PREDICTION_MODE m
, const vp8_prob
*p
794 assert(LEFT4X4
<= m
&& m
<= NEW4X4
);
796 vp8_write_token(w
, vp8_sub_mv_ref_tree
, p
,
797 vp8_sub_mv_ref_encoding_array
- LEFT4X4
+ m
);
802 vp8_writer
*w
, const MV
*mv
, const int_mv
*ref
, const MV_CONTEXT
*mvc
806 e
.row
= mv
->row
- ref
->as_mv
.row
;
807 e
.col
= mv
->col
- ref
->as_mv
.col
;
809 vp8_encode_motion_vector(w
, &e
, mvc
);
812 static void write_mb_features(vp8_writer
*w
, const MB_MODE_INFO
*mi
, const MACROBLOCKD
*x
)
814 // Encode the MB segment id.
815 if (x
->segmentation_enabled
&& x
->update_mb_segmentation_map
)
817 switch (mi
->segment_id
)
820 vp8_write(w
, 0, x
->mb_segment_tree_probs
[0]);
821 vp8_write(w
, 0, x
->mb_segment_tree_probs
[1]);
824 vp8_write(w
, 0, x
->mb_segment_tree_probs
[0]);
825 vp8_write(w
, 1, x
->mb_segment_tree_probs
[1]);
828 vp8_write(w
, 1, x
->mb_segment_tree_probs
[0]);
829 vp8_write(w
, 0, x
->mb_segment_tree_probs
[2]);
832 vp8_write(w
, 1, x
->mb_segment_tree_probs
[0]);
833 vp8_write(w
, 1, x
->mb_segment_tree_probs
[2]);
836 // TRAP.. This should not happen
838 vp8_write(w
, 0, x
->mb_segment_tree_probs
[0]);
839 vp8_write(w
, 0, x
->mb_segment_tree_probs
[1]);
846 static void pack_inter_mode_mvs(VP8_COMP
*const cpi
)
848 VP8_COMMON
*const pc
= & cpi
->common
;
849 vp8_writer
*const w
= & cpi
->bc
;
850 const MV_CONTEXT
*mvc
= pc
->fc
.mvc
;
852 const int *const rfct
= cpi
->count_mb_ref_frame_usage
;
853 const int rf_intra
= rfct
[INTRA_FRAME
];
854 const int rf_inter
= rfct
[LAST_FRAME
] + rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
];
856 MODE_INFO
*m
= pc
->mi
, *ms
;
857 const int mis
= pc
->mode_info_stride
;
862 int prob_skip_false
= 0;
865 cpi
->mb
.partition_info
= cpi
->mb
.pi
;
867 // Calculate the probabilities to be used to code the reference frame based on actual useage this frame
868 if (!(cpi
->prob_intra_coded
= rf_intra
* 255 / (rf_intra
+ rf_inter
)))
869 cpi
->prob_intra_coded
= 1;
871 prob_last_coded
= rf_inter
? (rfct
[LAST_FRAME
] * 255) / rf_inter
: 128;
873 if (!prob_last_coded
)
876 prob_gf_coded
= (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
])
877 ? (rfct
[GOLDEN_FRAME
] * 255) / (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
]) : 128;
887 if (pc
->mb_no_coeff_skip
)
889 prob_skip_false
= cpi
->skip_false_count
* 256 / (cpi
->skip_false_count
+ cpi
->skip_true_count
);
891 if (prob_skip_false
<= 1)
894 if (prob_skip_false
> 255)
895 prob_skip_false
= 255;
897 cpi
->prob_skip_false
= prob_skip_false
;
898 vp8_write_literal(w
, prob_skip_false
, 8);
901 vp8_write_literal(w
, cpi
->prob_intra_coded
, 8);
902 vp8_write_literal(w
, prob_last_coded
, 8);
903 vp8_write_literal(w
, prob_gf_coded
, 8);
905 update_mbintra_mode_probs(cpi
);
907 vp8_write_mvprobs(cpi
);
909 while (++mb_row
< pc
->mb_rows
)
913 while (++mb_col
< pc
->mb_cols
)
915 const MB_MODE_INFO
*const mi
= & m
->mbmi
;
916 const MV_REFERENCE_FRAME rf
= mi
->ref_frame
;
917 const MB_PREDICTION_MODE mode
= mi
->mode
;
919 MACROBLOCKD
*xd
= &cpi
->mb
.e_mbd
;
921 // Distance of Mb to the various image edges.
922 // These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
923 xd
->mb_to_left_edge
= -((mb_col
* 16) << 3);
924 xd
->mb_to_right_edge
= ((pc
->mb_cols
- 1 - mb_col
) * 16) << 3;
925 xd
->mb_to_top_edge
= -((mb_row
* 16)) << 3;
926 xd
->mb_to_bottom_edge
= ((pc
->mb_rows
- 1 - mb_row
) * 16) << 3;
932 if (cpi
->mb
.e_mbd
.update_mb_segmentation_map
)
933 write_mb_features(w
, mi
, &cpi
->mb
.e_mbd
);
935 if (pc
->mb_no_coeff_skip
)
936 vp8_encode_bool(w
, m
->mbmi
.mb_skip_coeff
, prob_skip_false
);
938 if (rf
== INTRA_FRAME
)
940 vp8_write(w
, 0, cpi
->prob_intra_coded
);
944 write_ymode(w
, mode
, pc
->fc
.ymode_prob
);
951 write_bmode(w
, m
->bmi
[j
].as_mode
, pc
->fc
.bmode_prob
);
955 write_uv_mode(w
, mi
->uv_mode
, pc
->fc
.uv_mode_prob
);
957 else /* inter coded */
960 vp8_prob mv_ref_p
[VP8_MVREFS
-1];
962 vp8_write(w
, 1, cpi
->prob_intra_coded
);
964 if (rf
== LAST_FRAME
)
965 vp8_write(w
, 0, prob_last_coded
);
968 vp8_write(w
, 1, prob_last_coded
);
969 vp8_write(w
, (rf
== GOLDEN_FRAME
) ? 0 : 1, prob_gf_coded
);
976 vp8_find_near_mvs(xd
, m
, &n1
, &n2
, &best_mv
, ct
, rf
, cpi
->common
.ref_frame_sign_bias
);
977 vp8_mv_ref_probs(mv_ref_p
, ct
);
980 accum_mv_refs(mode
, ct
);
989 write_mv_ref(w
, mode
, mv_ref_p
);
991 switch (mode
) /* new, split require MVs */
999 write_mv(w
, &mi
->mv
.as_mv
, &best_mv
, mvc
);
1007 ++count_mb_seg
[mi
->partitioning
];
1010 write_split(w
, mi
->partitioning
);
1014 const B_MODE_INFO
*const b
= cpi
->mb
.partition_info
->bmi
+ j
;
1015 const int *const L
= vp8_mbsplits
[mi
->partitioning
];
1016 int k
= -1; /* first block in subset j */
1018 int_mv leftmv
, abovemv
;
1024 leftmv
.as_int
= left_block_mv(m
, k
);
1025 abovemv
.as_int
= above_block_mv(m
, k
, mis
);
1026 mv_contz
= vp8_mv_cont(&(leftmv
.as_mv
), &(abovemv
.as_mv
));
1028 write_sub_mv_ref(w
, b
->mode
, vp8_sub_mv_ref_prob2
[mv_contz
]); //pc->fc.sub_mv_ref_prob);
1030 if (b
->mode
== NEW4X4
)
1032 #ifdef ENTROPY_STATS
1033 active_section
= 11;
1035 write_mv(w
, &b
->mv
.as_mv
, &best_mv
, (const MV_CONTEXT
*) mvc
);
1038 while (++j
< cpi
->mb
.partition_info
->count
);
1047 cpi
->mb
.partition_info
++;
1050 ++m
; /* skip L prediction border */
1051 cpi
->mb
.partition_info
++;
1056 static void write_kfmodes(VP8_COMP
*cpi
)
1058 vp8_writer
*const bc
= & cpi
->bc
;
1059 const VP8_COMMON
*const c
= & cpi
->common
;
1061 MODE_INFO
*m
= c
->mi
;
1064 int prob_skip_false
= 0;
1066 if (c
->mb_no_coeff_skip
)
1068 prob_skip_false
= cpi
->skip_false_count
* 256 / (cpi
->skip_false_count
+ cpi
->skip_true_count
);
1070 if (prob_skip_false
<= 1)
1071 prob_skip_false
= 1;
1073 if (prob_skip_false
>= 255)
1074 prob_skip_false
= 255;
1076 cpi
->prob_skip_false
= prob_skip_false
;
1077 vp8_write_literal(bc
, prob_skip_false
, 8);
1080 while (++mb_row
< c
->mb_rows
)
1084 while (++mb_col
< c
->mb_cols
)
1086 const int ym
= m
->mbmi
.mode
;
1088 if (cpi
->mb
.e_mbd
.update_mb_segmentation_map
)
1089 write_mb_features(bc
, &m
->mbmi
, &cpi
->mb
.e_mbd
);
1091 if (c
->mb_no_coeff_skip
)
1092 vp8_encode_bool(bc
, m
->mbmi
.mb_skip_coeff
, prob_skip_false
);
1094 kfwrite_ymode(bc
, ym
, c
->kf_ymode_prob
);
1098 const int mis
= c
->mode_info_stride
;
1103 const B_PREDICTION_MODE A
= above_block_mode(m
, i
, mis
);
1104 const B_PREDICTION_MODE L
= left_block_mode(m
, i
);
1105 const int bm
= m
->bmi
[i
].as_mode
;
1107 #ifdef ENTROPY_STATS
1108 ++intra_mode_stats
[A
] [L
] [bm
];
1111 write_bmode(bc
, bm
, c
->kf_bmode_prob
[A
] [L
]);
1116 write_uv_mode(bc
, (m
++)->mbmi
.uv_mode
, c
->kf_uv_mode_prob
);
1119 m
++; // skip L prediction border
1122 int vp8_estimate_entropy_savings(VP8_COMP
*cpi
)
1127 const int *const rfct
= cpi
->count_mb_ref_frame_usage
;
1128 const int rf_intra
= rfct
[INTRA_FRAME
];
1129 const int rf_inter
= rfct
[LAST_FRAME
] + rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
];
1130 int new_intra
, new_last
, gf_last
, oldtotal
, newtotal
;
1131 int ref_frame_cost
[MAX_REF_FRAMES
];
1133 vp8_clear_system_state(); //__asm emms;
1135 if (cpi
->common
.frame_type
!= KEY_FRAME
)
1137 if (!(new_intra
= rf_intra
* 255 / (rf_intra
+ rf_inter
)))
1140 new_last
= rf_inter
? (rfct
[LAST_FRAME
] * 255) / rf_inter
: 128;
1142 gf_last
= (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
])
1143 ? (rfct
[GOLDEN_FRAME
] * 255) / (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
]) : 128;
1146 ref_frame_cost
[INTRA_FRAME
] = vp8_cost_zero(new_intra
);
1147 ref_frame_cost
[LAST_FRAME
] = vp8_cost_one(new_intra
)
1148 + vp8_cost_zero(new_last
);
1149 ref_frame_cost
[GOLDEN_FRAME
] = vp8_cost_one(new_intra
)
1150 + vp8_cost_one(new_last
)
1151 + vp8_cost_zero(gf_last
);
1152 ref_frame_cost
[ALTREF_FRAME
] = vp8_cost_one(new_intra
)
1153 + vp8_cost_one(new_last
)
1154 + vp8_cost_one(gf_last
);
1157 rfct
[INTRA_FRAME
] * ref_frame_cost
[INTRA_FRAME
] +
1158 rfct
[LAST_FRAME
] * ref_frame_cost
[LAST_FRAME
] +
1159 rfct
[GOLDEN_FRAME
] * ref_frame_cost
[GOLDEN_FRAME
] +
1160 rfct
[ALTREF_FRAME
] * ref_frame_cost
[ALTREF_FRAME
];
1164 ref_frame_cost
[INTRA_FRAME
] = vp8_cost_zero(cpi
->prob_intra_coded
);
1165 ref_frame_cost
[LAST_FRAME
] = vp8_cost_one(cpi
->prob_intra_coded
)
1166 + vp8_cost_zero(cpi
->prob_last_coded
);
1167 ref_frame_cost
[GOLDEN_FRAME
] = vp8_cost_one(cpi
->prob_intra_coded
)
1168 + vp8_cost_one(cpi
->prob_last_coded
)
1169 + vp8_cost_zero(cpi
->prob_gf_coded
);
1170 ref_frame_cost
[ALTREF_FRAME
] = vp8_cost_one(cpi
->prob_intra_coded
)
1171 + vp8_cost_one(cpi
->prob_last_coded
)
1172 + vp8_cost_one(cpi
->prob_gf_coded
);
1175 rfct
[INTRA_FRAME
] * ref_frame_cost
[INTRA_FRAME
] +
1176 rfct
[LAST_FRAME
] * ref_frame_cost
[LAST_FRAME
] +
1177 rfct
[GOLDEN_FRAME
] * ref_frame_cost
[GOLDEN_FRAME
] +
1178 rfct
[ALTREF_FRAME
] * ref_frame_cost
[ALTREF_FRAME
];
1180 savings
+= (oldtotal
- newtotal
) / 256;
1194 /* at every context */
1196 /* calc probs and branch cts for this frame only */
1197 //vp8_prob new_p [vp8_coef_tokens-1];
1198 //unsigned int branch_ct [vp8_coef_tokens-1] [2];
1200 int t
= 0; /* token/prob index */
1202 vp8_tree_probs_from_distribution(
1203 vp8_coef_tokens
, vp8_coef_encodings
, vp8_coef_tree
,
1204 cpi
->frame_coef_probs
[i
][j
][k
], cpi
->frame_branch_ct
[i
][j
][k
], cpi
->coef_counts
[i
][j
][k
],
1210 const unsigned int *ct
= cpi
->frame_branch_ct
[i
][j
][k
][t
];
1211 const vp8_prob newp
= cpi
->frame_coef_probs
[i
][j
][k
][t
];
1213 const vp8_prob old
= cpi
->common
.fc
.coef_probs
[i
][j
][k
][t
];
1214 const vp8_prob upd
= vp8_coef_update_probs
[i
][j
][k
][t
];
1216 const int old_b
= vp8_cost_branch(ct
, old
);
1217 const int new_b
= vp8_cost_branch(ct
, newp
);
1219 const int update_b
= 8 +
1220 ((vp8_cost_one(upd
) - vp8_cost_zero(upd
)) >> 8);
1222 const int s
= old_b
- new_b
- update_b
;
1229 while (++t
< vp8_coef_tokens
- 1);
1233 while (++k
< PREV_COEF_CONTEXTS
);
1235 while (++j
< COEF_BANDS
);
1237 while (++i
< BLOCK_TYPES
);
1242 static void update_coef_probs(VP8_COMP
*cpi
)
1245 vp8_writer
*const w
= & cpi
->bc
;
1248 vp8_clear_system_state(); //__asm emms;
1261 //note: use result from vp8_estimate_entropy_savings, so no need to call vp8_tree_probs_from_distribution here.
1262 /* at every context */
1264 /* calc probs and branch cts for this frame only */
1265 //vp8_prob new_p [vp8_coef_tokens-1];
1266 //unsigned int branch_ct [vp8_coef_tokens-1] [2];
1268 int t
= 0; /* token/prob index */
1270 //vp8_tree_probs_from_distribution(
1271 // vp8_coef_tokens, vp8_coef_encodings, vp8_coef_tree,
1272 // new_p, branch_ct, (unsigned int *)cpi->coef_counts [i][j][k],
1278 const unsigned int *ct
= cpi
->frame_branch_ct
[i
][j
][k
][t
];
1279 const vp8_prob newp
= cpi
->frame_coef_probs
[i
][j
][k
][t
];
1281 vp8_prob
*Pold
= cpi
->common
.fc
.coef_probs
[i
][j
][k
] + t
;
1282 const vp8_prob old
= *Pold
;
1283 const vp8_prob upd
= vp8_coef_update_probs
[i
][j
][k
][t
];
1285 const int old_b
= vp8_cost_branch(ct
, old
);
1286 const int new_b
= vp8_cost_branch(ct
, newp
);
1288 const int update_b
= 8 +
1289 ((vp8_cost_one(upd
) - vp8_cost_zero(upd
)) >> 8);
1291 const int s
= old_b
- new_b
- update_b
;
1292 const int u
= s
> 0 ? 1 : 0;
1294 vp8_write(w
, u
, upd
);
1297 #ifdef ENTROPY_STATS
1298 ++ tree_update_hist
[i
][j
][k
][t
] [u
];
1303 /* send/use new probability */
1306 vp8_write_literal(w
, newp
, 8);
1313 while (++t
< vp8_coef_tokens
- 1);
1315 /* Accum token counts for generation of default statistics */
1316 #ifdef ENTROPY_STATS
1321 context_counters
[i
][j
][k
][t
] += cpi
->coef_counts
[i
][j
][k
][t
];
1323 while (++t
< vp8_coef_tokens
);
1328 while (++k
< PREV_COEF_CONTEXTS
);
1330 while (++j
< COEF_BANDS
);
1332 while (++i
< BLOCK_TYPES
);
1335 #ifdef PACKET_TESTING
1339 static void put_delta_q(vp8_writer
*bc
, int delta_q
)
1343 vp8_write_bit(bc
, 1);
1344 vp8_write_literal(bc
, abs(delta_q
), 4);
1347 vp8_write_bit(bc
, 1);
1349 vp8_write_bit(bc
, 0);
1352 vp8_write_bit(bc
, 0);
1355 void vp8_pack_bitstream(VP8_COMP
*cpi
, unsigned char *dest
, unsigned long *size
)
1359 VP8_COMMON
*const pc
= & cpi
->common
;
1360 vp8_writer
*const bc
= & cpi
->bc
;
1361 MACROBLOCKD
*const xd
= & cpi
->mb
.e_mbd
;
1362 int extra_bytes_packed
= 0;
1364 unsigned char *cx_data
= dest
;
1365 const int *mb_feature_data_bits
;
1367 oh
.show_frame
= (int) pc
->show_frame
;
1368 oh
.type
= (int)pc
->frame_type
;
1369 oh
.version
= pc
->version
;
1370 oh
.first_partition_length_in_bytes
= 0;
1372 mb_feature_data_bits
= vp8_mb_feature_data_bits
;
1375 #if defined(SECTIONBITS_OUTPUT)
1376 Sectionbits
[active_section
= 1] += sizeof(VP8_HEADER
) * 8 * 256;
1379 //vp8_kf_default_bmode_probs() is called in vp8_setup_key_frame() once for each
1380 //K frame before encode frame. pc->kf_bmode_prob doesn't get changed anywhere
1381 //else. No need to call it again here. --yw
1382 //vp8_kf_default_bmode_probs( pc->kf_bmode_prob);
1384 // every keyframe send startcode, width, height, scale factor, clamp and color type
1385 if (oh
.type
== KEY_FRAME
)
1389 // Start / synch code
1394 v
= (pc
->horiz_scale
<< 14) | pc
->Width
;
1396 cx_data
[4] = v
>> 8;
1398 v
= (pc
->vert_scale
<< 14) | pc
->Height
;
1400 cx_data
[6] = v
>> 8;
1402 extra_bytes_packed
= 7;
1403 cx_data
+= extra_bytes_packed
;
1405 vp8_start_encode(bc
, cx_data
);
1408 vp8_write_bit(bc
, pc
->clr_type
);
1409 vp8_write_bit(bc
, pc
->clamp_type
);
1413 vp8_start_encode(bc
, cx_data
);
1416 // Signal whether or not Segmentation is enabled
1417 vp8_write_bit(bc
, (xd
->segmentation_enabled
) ? 1 : 0);
1419 // Indicate which features are enabled
1420 if (xd
->segmentation_enabled
)
1422 // Signal whether or not the segmentation map is being updated.
1423 vp8_write_bit(bc
, (xd
->update_mb_segmentation_map
) ? 1 : 0);
1424 vp8_write_bit(bc
, (xd
->update_mb_segmentation_data
) ? 1 : 0);
1426 if (xd
->update_mb_segmentation_data
)
1430 vp8_write_bit(bc
, (xd
->mb_segement_abs_delta
) ? 1 : 0);
1432 // For each segmentation feature (Quant and loop filter level)
1433 for (i
= 0; i
< MB_LVL_MAX
; i
++)
1435 // For each of the segments
1436 for (j
= 0; j
< MAX_MB_SEGMENTS
; j
++)
1438 Data
= xd
->segment_feature_data
[i
][j
];
1443 vp8_write_bit(bc
, 1);
1448 vp8_write_literal(bc
, Data
, mb_feature_data_bits
[i
]);
1449 vp8_write_bit(bc
, 1);
1453 vp8_write_literal(bc
, Data
, mb_feature_data_bits
[i
]);
1454 vp8_write_bit(bc
, 0);
1458 vp8_write_bit(bc
, 0);
1463 if (xd
->update_mb_segmentation_map
)
1465 // Write the probs used to decode the segment id for each macro block.
1466 for (i
= 0; i
< MB_FEATURE_TREE_PROBS
; i
++)
1468 int Data
= xd
->mb_segment_tree_probs
[i
];
1472 vp8_write_bit(bc
, 1);
1473 vp8_write_literal(bc
, Data
, 8);
1476 vp8_write_bit(bc
, 0);
1481 // Code to determine whether or not to update the scan order.
1482 vp8_write_bit(bc
, pc
->filter_type
);
1483 vp8_write_literal(bc
, pc
->filter_level
, 6);
1484 vp8_write_literal(bc
, pc
->sharpness_level
, 3);
1486 // Write out loop filter deltas applied at the MB level based on mode or ref frame (if they are enabled).
1487 vp8_write_bit(bc
, (xd
->mode_ref_lf_delta_enabled
) ? 1 : 0);
1489 if (xd
->mode_ref_lf_delta_enabled
)
1491 // Do the deltas need to be updated
1492 int send_update
= xd
->mode_ref_lf_delta_update
1493 || cpi
->oxcf
.error_resilient_mode
;
1495 vp8_write_bit(bc
, send_update
);
1501 for (i
= 0; i
< MAX_REF_LF_DELTAS
; i
++)
1503 Data
= xd
->ref_lf_deltas
[i
];
1506 if (xd
->ref_lf_deltas
[i
] != xd
->last_ref_lf_deltas
[i
]
1507 || cpi
->oxcf
.error_resilient_mode
)
1509 xd
->last_ref_lf_deltas
[i
] = xd
->ref_lf_deltas
[i
];
1510 vp8_write_bit(bc
, 1);
1514 vp8_write_literal(bc
, (Data
& 0x3F), 6);
1515 vp8_write_bit(bc
, 0); // sign
1520 vp8_write_literal(bc
, (Data
& 0x3F), 6);
1521 vp8_write_bit(bc
, 1); // sign
1525 vp8_write_bit(bc
, 0);
1529 for (i
= 0; i
< MAX_MODE_LF_DELTAS
; i
++)
1531 Data
= xd
->mode_lf_deltas
[i
];
1533 if (xd
->mode_lf_deltas
[i
] != xd
->last_mode_lf_deltas
[i
]
1534 || cpi
->oxcf
.error_resilient_mode
)
1536 xd
->last_mode_lf_deltas
[i
] = xd
->mode_lf_deltas
[i
];
1537 vp8_write_bit(bc
, 1);
1541 vp8_write_literal(bc
, (Data
& 0x3F), 6);
1542 vp8_write_bit(bc
, 0); // sign
1547 vp8_write_literal(bc
, (Data
& 0x3F), 6);
1548 vp8_write_bit(bc
, 1); // sign
1552 vp8_write_bit(bc
, 0);
1557 //signal here is multi token partition is enabled
1558 vp8_write_literal(bc
, pc
->multi_token_partition
, 2);
1560 // Frame Qbaseline quantizer index
1561 vp8_write_literal(bc
, pc
->base_qindex
, 7);
1563 // Transmit Dc, Second order and Uv quantizer delta information
1564 put_delta_q(bc
, pc
->y1dc_delta_q
);
1565 put_delta_q(bc
, pc
->y2dc_delta_q
);
1566 put_delta_q(bc
, pc
->y2ac_delta_q
);
1567 put_delta_q(bc
, pc
->uvdc_delta_q
);
1568 put_delta_q(bc
, pc
->uvac_delta_q
);
1570 // When there is a key frame all reference buffers are updated using the new key frame
1571 if (pc
->frame_type
!= KEY_FRAME
)
1573 // Should the GF or ARF be updated using the transmitted frame or buffer
1574 vp8_write_bit(bc
, pc
->refresh_golden_frame
);
1575 vp8_write_bit(bc
, pc
->refresh_alt_ref_frame
);
1577 // If not being updated from current frame should either GF or ARF be updated from another buffer
1578 if (!pc
->refresh_golden_frame
)
1579 vp8_write_literal(bc
, pc
->copy_buffer_to_gf
, 2);
1581 if (!pc
->refresh_alt_ref_frame
)
1582 vp8_write_literal(bc
, pc
->copy_buffer_to_arf
, 2);
1584 // Indicate reference frame sign bias for Golden and ARF frames (always 0 for last frame buffer)
1585 vp8_write_bit(bc
, pc
->ref_frame_sign_bias
[GOLDEN_FRAME
]);
1586 vp8_write_bit(bc
, pc
->ref_frame_sign_bias
[ALTREF_FRAME
]);
1589 vp8_write_bit(bc
, pc
->refresh_entropy_probs
);
1591 if (pc
->frame_type
!= KEY_FRAME
)
1592 vp8_write_bit(bc
, pc
->refresh_last_frame
);
1594 #ifdef ENTROPY_STATS
1596 if (pc
->frame_type
== INTER_FRAME
)
1603 vp8_clear_system_state(); //__asm emms;
1605 //************************************************
1606 // save a copy for later refresh
1608 vpx_memcpy(&cpi
->common
.lfc
, &cpi
->common
.fc
, sizeof(cpi
->common
.fc
));
1611 update_coef_probs(cpi
);
1613 #ifdef ENTROPY_STATS
1617 // Write out the mb_no_coeff_skip flag
1618 vp8_write_bit(bc
, pc
->mb_no_coeff_skip
);
1620 if (pc
->frame_type
== KEY_FRAME
)
1624 #ifdef ENTROPY_STATS
1630 pack_inter_mode_mvs(cpi
);
1632 #ifdef ENTROPY_STATS
1637 vp8_stop_encode(bc
);
1639 oh
.first_partition_length_in_bytes
= cpi
->bc
.pos
;
1641 /* update frame tag */
1643 int v
= (oh
.first_partition_length_in_bytes
<< 5) |
1644 (oh
.show_frame
<< 4) |
1653 *size
= VP8_HEADER_SIZE
+ extra_bytes_packed
+ cpi
->bc
.pos
;
1655 if (pc
->multi_token_partition
!= ONE_PARTITION
)
1659 num_part
= 1 << pc
->multi_token_partition
;
1661 pack_tokens_into_partitions(cpi
, cx_data
+ bc
->pos
, num_part
, &asize
);
1667 vp8_start_encode(&cpi
->bc2
, cx_data
+ bc
->pos
);
1669 #if CONFIG_MULTITHREAD
1670 if (cpi
->b_multi_threaded
)
1671 pack_mb_row_tokens(cpi
, &cpi
->bc2
);
1674 pack_tokens(&cpi
->bc2
, cpi
->tok
, cpi
->tok_count
);
1676 vp8_stop_encode(&cpi
->bc2
);
1678 *size
+= cpi
->bc2
.pos
;
1682 #ifdef ENTROPY_STATS
1683 void print_tree_update_probs()
1686 FILE *f
= fopen("context.c", "a");
1688 fprintf(f
, "\n/* Update probabilities for token entropy tree. */\n\n");
1689 fprintf(f
, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1] = {\n");
1691 for (i
= 0; i
< BLOCK_TYPES
; i
++)
1693 fprintf(f
, " { \n");
1695 for (j
= 0; j
< COEF_BANDS
; j
++)
1699 for (k
= 0; k
< PREV_COEF_CONTEXTS
; k
++)
1703 for (l
= 0; l
< MAX_ENTROPY_TOKENS
- 1; l
++)
1705 Sum
= tree_update_hist
[i
][j
][k
][l
][0] + tree_update_hist
[i
][j
][k
][l
][1];
1709 if (((tree_update_hist
[i
][j
][k
][l
][0] * 255) / Sum
) > 0)
1710 fprintf(f
, "%3ld, ", (tree_update_hist
[i
][j
][k
][l
][0] * 255) / Sum
);
1712 fprintf(f
, "%3ld, ", 1);
1715 fprintf(f
, "%3ld, ", 128);
1721 fprintf(f
, " },\n");
1724 fprintf(f
, " },\n");