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.
14 #include "entropymode.h"
15 #include "findnearmv.h"
17 #include "systemdependent.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 # define make_endian_16(a) \
63 (((unsigned int)(a & 0xff)) << 8) | (((unsigned int)(a & 0xff00)) >> 8)
64 # define make_endian_32(a) \
65 (((unsigned int)(a & 0xff)) << 24) | (((unsigned int)(a & 0xff00)) << 8) | \
66 (((unsigned int)(a & 0xff0000)) >> 8) | (((unsigned int)(a & 0xff000000)) >> 24)
68 # define make_endian_16(a) a
69 # define make_endian_32(a) a
72 static void update_mode(
75 vp8_token tok
[/* n */],
77 vp8_prob Pnew
[/* n-1 */],
78 vp8_prob Pcur
[/* n-1 */],
79 unsigned int bct
[/* n-1 */] [2],
80 const unsigned int num_events
[/* n */]
83 unsigned int new_b
= 0, old_b
= 0;
86 vp8_tree_probs_from_distribution(
88 Pnew
, bct
, num_events
,
94 new_b
+= vp8_cost_branch(bct
[i
], Pnew
[i
]);
95 old_b
+= vp8_cost_branch(bct
[i
], Pcur
[i
]);
99 if (new_b
+ (n
<< 8) < old_b
)
107 const vp8_prob p
= Pnew
[i
];
109 vp8_write_literal(w
, Pcur
[i
] = p
? p
: 1, 8);
117 static void update_mbintra_mode_probs(VP8_COMP
*cpi
)
119 VP8_COMMON
*const x
= & cpi
->common
;
121 vp8_writer
*const w
= & cpi
->bc
;
124 vp8_prob Pnew
[VP8_YMODES
-1];
125 unsigned int bct
[VP8_YMODES
-1] [2];
128 w
, VP8_YMODES
, vp8_ymode_encodings
, vp8_ymode_tree
,
129 Pnew
, x
->fc
.ymode_prob
, bct
, (unsigned int *)cpi
->ymode_count
133 vp8_prob Pnew
[VP8_UV_MODES
-1];
134 unsigned int bct
[VP8_UV_MODES
-1] [2];
137 w
, VP8_UV_MODES
, vp8_uv_mode_encodings
, vp8_uv_mode_tree
,
138 Pnew
, x
->fc
.uv_mode_prob
, bct
, (unsigned int *)cpi
->uv_mode_count
143 static void write_ymode(vp8_writer
*bc
, int m
, const vp8_prob
*p
)
145 vp8_write_token(bc
, vp8_ymode_tree
, p
, vp8_ymode_encodings
+ m
);
148 static void kfwrite_ymode(vp8_writer
*bc
, int m
, const vp8_prob
*p
)
150 vp8_write_token(bc
, vp8_kf_ymode_tree
, p
, vp8_kf_ymode_encodings
+ m
);
153 static void write_uv_mode(vp8_writer
*bc
, int m
, const vp8_prob
*p
)
155 vp8_write_token(bc
, vp8_uv_mode_tree
, p
, vp8_uv_mode_encodings
+ m
);
159 static void write_bmode(vp8_writer
*bc
, int m
, const vp8_prob
*p
)
161 vp8_write_token(bc
, vp8_bmode_tree
, p
, vp8_bmode_encodings
+ m
);
164 static void write_split(vp8_writer
*bc
, int x
)
167 bc
, vp8_mbsplit_tree
, vp8_mbsplit_probs
, vp8_mbsplit_encodings
+ x
171 static const unsigned int norm
[256] =
173 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,
174 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,
175 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,
176 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,
177 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,
178 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,
179 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,
180 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
183 static void pack_tokens_c(vp8_writer
*w
, const TOKENEXTRA
*p
, int xcount
)
185 const TOKENEXTRA
*const stop
= p
+ xcount
;
188 int count
= w
->count
;
189 unsigned int range
= w
->range
;
190 unsigned int lowvalue
= w
->lowvalue
;
194 const int t
= p
->Token
;
195 vp8_token
*const a
= vp8_coef_encodings
+ t
;
196 const vp8_extra_bit_struct
*const b
= vp8_extra_bits
+ t
;
198 const unsigned char *pp
= p
->context_tree
;
202 if (p
->skip_eob_node
)
210 const int bb
= (v
>> --n
) & 1;
211 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
212 i
= vp8_coef_tree
[i
+bb
];
217 range
= range
- split
;
230 int offset
= shift
- count
;
232 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
236 while (x
>= 0 && w
->buffer
[x
] == 0xff)
238 w
->buffer
[x
] = (unsigned char)0;
245 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
248 lowvalue
&= 0xffffff;
259 const int e
= p
->Extra
, L
= b
->Len
;
263 const unsigned char *pp
= b
->prob
;
265 int n
= L
; /* number of bits in v, assumed nonzero */
270 const int bb
= (v
>> --n
) & 1;
271 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
277 range
= range
- split
;
290 int offset
= shift
- count
;
292 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
296 while (x
>= 0 && w
->buffer
[x
] == 0xff)
298 w
->buffer
[x
] = (unsigned char)0;
305 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
308 lowvalue
&= 0xffffff;
320 split
= (range
+ 1) >> 1;
325 range
= range
- split
;
334 if ((lowvalue
& 0x80000000))
338 while (x
>= 0 && w
->buffer
[x
] == 0xff)
340 w
->buffer
[x
] = (unsigned char)0;
353 w
->buffer
[w
->pos
++] = (lowvalue
>> 24);
354 lowvalue
&= 0xffffff;
364 w
->lowvalue
= lowvalue
;
369 static void write_partition_size(unsigned char *cx_data
, int size
)
375 csize
= (size
>> 8) & 0xff;
376 *(cx_data
+ 1) = csize
;
377 csize
= (size
>> 16) & 0xff;
378 *(cx_data
+ 2) = csize
;
382 static void pack_tokens_into_partitions_c(VP8_COMP
*cpi
, unsigned char *cx_data
, int num_part
, int *size
)
386 unsigned char *ptr
= cx_data
;
388 vp8_writer
*w
= &cpi
->bc2
;
389 *size
= 3 * (num_part
- 1);
390 ptr
= cx_data
+ (*size
);
392 for (i
= 0; i
< num_part
; i
++)
394 vp8_start_encode(w
, ptr
);
397 int count
= w
->count
;
398 unsigned int range
= w
->range
;
399 unsigned int lowvalue
= w
->lowvalue
;
402 for (mb_row
= i
; mb_row
< cpi
->common
.mb_rows
; mb_row
+= num_part
)
404 TOKENEXTRA
*p
= cpi
->tplist
[mb_row
].start
;
405 TOKENEXTRA
*stop
= cpi
->tplist
[mb_row
].stop
;
409 const int t
= p
->Token
;
410 vp8_token
*const a
= vp8_coef_encodings
+ t
;
411 const vp8_extra_bit_struct
*const b
= vp8_extra_bits
+ t
;
413 const unsigned char *pp
= p
->context_tree
;
417 if (p
->skip_eob_node
)
425 const int bb
= (v
>> --n
) & 1;
426 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
427 i
= vp8_coef_tree
[i
+bb
];
432 range
= range
- split
;
445 int offset
= shift
- count
;
447 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
451 while (x
>= 0 && w
->buffer
[x
] == 0xff)
453 w
->buffer
[x
] = (unsigned char)0;
460 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
463 lowvalue
&= 0xffffff;
474 const int e
= p
->Extra
, L
= b
->Len
;
478 const unsigned char *pp
= b
->prob
;
480 int n
= L
; /* number of bits in v, assumed nonzero */
485 const int bb
= (v
>> --n
) & 1;
486 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
492 range
= range
- split
;
505 int offset
= shift
- count
;
507 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
511 while (x
>= 0 && w
->buffer
[x
] == 0xff)
513 w
->buffer
[x
] = (unsigned char)0;
520 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
523 lowvalue
&= 0xffffff;
533 split
= (range
+ 1) >> 1;
538 range
= range
- split
;
547 if ((lowvalue
& 0x80000000))
551 while (x
>= 0 && w
->buffer
[x
] == 0xff)
553 w
->buffer
[x
] = (unsigned char)0;
566 w
->buffer
[w
->pos
++] = (lowvalue
>> 24);
567 lowvalue
&= 0xffffff;
578 w
->lowvalue
= lowvalue
;
586 if (i
< (num_part
- 1))
588 write_partition_size(cx_data
, w
->pos
);
596 static void pack_mb_row_tokens_c(VP8_COMP
*cpi
, vp8_writer
*w
)
600 int count
= w
->count
;
601 unsigned int range
= w
->range
;
602 unsigned int lowvalue
= w
->lowvalue
;
606 for (mb_row
= 0; mb_row
< cpi
->common
.mb_rows
; mb_row
++)
608 TOKENEXTRA
*p
= cpi
->tplist
[mb_row
].start
;
609 TOKENEXTRA
*stop
= cpi
->tplist
[mb_row
].stop
;
613 const int t
= p
->Token
;
614 vp8_token
*const a
= vp8_coef_encodings
+ t
;
615 const vp8_extra_bit_struct
*const b
= vp8_extra_bits
+ t
;
617 const unsigned char *pp
= p
->context_tree
;
621 if (p
->skip_eob_node
)
629 const int bb
= (v
>> --n
) & 1;
630 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
631 i
= vp8_coef_tree
[i
+bb
];
636 range
= range
- split
;
649 int offset
= shift
- count
;
651 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
655 while (x
>= 0 && w
->buffer
[x
] == 0xff)
657 w
->buffer
[x
] = (unsigned char)0;
664 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
667 lowvalue
&= 0xffffff;
678 const int e
= p
->Extra
, L
= b
->Len
;
682 const unsigned char *pp
= b
->prob
;
684 int n
= L
; /* number of bits in v, assumed nonzero */
689 const int bb
= (v
>> --n
) & 1;
690 split
= 1 + (((range
- 1) * pp
[i
>>1]) >> 8);
696 range
= range
- split
;
709 int offset
= shift
- count
;
711 if ((lowvalue
<< (offset
- 1)) & 0x80000000)
715 while (x
>= 0 && w
->buffer
[x
] == 0xff)
717 w
->buffer
[x
] = (unsigned char)0;
724 w
->buffer
[w
->pos
++] = (lowvalue
>> (24 - offset
));
727 lowvalue
&= 0xffffff;
737 split
= (range
+ 1) >> 1;
742 range
= range
- split
;
751 if ((lowvalue
& 0x80000000))
755 while (x
>= 0 && w
->buffer
[x
] == 0xff)
757 w
->buffer
[x
] = (unsigned char)0;
770 w
->buffer
[w
->pos
++] = (lowvalue
>> 24);
771 lowvalue
&= 0xffffff;
782 w
->lowvalue
= lowvalue
;
787 static void write_mv_ref
789 vp8_writer
*w
, MB_PREDICTION_MODE m
, const vp8_prob
*p
793 assert(NEARESTMV
<= m
&& m
<= SPLITMV
);
795 vp8_write_token(w
, vp8_mv_ref_tree
, p
,
796 vp8_mv_ref_encoding_array
- NEARESTMV
+ m
);
799 static void write_sub_mv_ref
801 vp8_writer
*w
, B_PREDICTION_MODE m
, const vp8_prob
*p
804 assert(LEFT4X4
<= m
&& m
<= NEW4X4
);
806 vp8_write_token(w
, vp8_sub_mv_ref_tree
, p
,
807 vp8_sub_mv_ref_encoding_array
- LEFT4X4
+ m
);
812 vp8_writer
*w
, const MV
*mv
, const MV
*ref
, const MV_CONTEXT
*mvc
816 e
.row
= mv
->row
- ref
->row
;
817 e
.col
= mv
->col
- ref
->col
;
819 vp8_encode_motion_vector(w
, &e
, mvc
);
822 static void write_mb_features(vp8_writer
*w
, const MB_MODE_INFO
*mi
, const MACROBLOCKD
*x
)
824 // Encode the MB segment id.
825 if (x
->segmentation_enabled
&& x
->update_mb_segmentation_map
)
827 switch (mi
->segment_id
)
830 vp8_write(w
, 0, x
->mb_segment_tree_probs
[0]);
831 vp8_write(w
, 0, x
->mb_segment_tree_probs
[1]);
834 vp8_write(w
, 0, x
->mb_segment_tree_probs
[0]);
835 vp8_write(w
, 1, x
->mb_segment_tree_probs
[1]);
838 vp8_write(w
, 1, x
->mb_segment_tree_probs
[0]);
839 vp8_write(w
, 0, x
->mb_segment_tree_probs
[2]);
842 vp8_write(w
, 1, x
->mb_segment_tree_probs
[0]);
843 vp8_write(w
, 1, x
->mb_segment_tree_probs
[2]);
846 // TRAP.. This should not happen
848 vp8_write(w
, 0, x
->mb_segment_tree_probs
[0]);
849 vp8_write(w
, 0, x
->mb_segment_tree_probs
[1]);
856 static void pack_inter_mode_mvs(VP8_COMP
*const cpi
)
858 VP8_COMMON
*const pc
= & cpi
->common
;
859 vp8_writer
*const w
= & cpi
->bc
;
860 const MV_CONTEXT
*mvc
= pc
->fc
.mvc
;
862 const int *const rfct
= cpi
->count_mb_ref_frame_usage
;
863 const int rf_intra
= rfct
[INTRA_FRAME
];
864 const int rf_inter
= rfct
[LAST_FRAME
] + rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
];
866 MODE_INFO
*m
= pc
->mi
, *ms
;
867 const int mis
= pc
->mode_info_stride
;
872 int prob_skip_false
= 0;
875 cpi
->mb
.partition_info
= cpi
->mb
.pi
;
877 // Calculate the probabilities to be used to code the reference frame based on actual useage this frame
878 if (!(cpi
->prob_intra_coded
= rf_intra
* 255 / (rf_intra
+ rf_inter
)))
879 cpi
->prob_intra_coded
= 1;
881 prob_last_coded
= rf_inter
? (rfct
[LAST_FRAME
] * 255) / rf_inter
: 128;
883 if (!prob_last_coded
)
886 prob_gf_coded
= (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
])
887 ? (rfct
[GOLDEN_FRAME
] * 255) / (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
]) : 128;
897 if (pc
->mb_no_coeff_skip
)
899 prob_skip_false
= cpi
->skip_false_count
* 256 / (cpi
->skip_false_count
+ cpi
->skip_true_count
);
901 if (prob_skip_false
<= 1)
904 if (prob_skip_false
> 255)
905 prob_skip_false
= 255;
907 cpi
->prob_skip_false
= prob_skip_false
;
908 vp8_write_literal(w
, prob_skip_false
, 8);
911 vp8_write_literal(w
, cpi
->prob_intra_coded
, 8);
912 vp8_write_literal(w
, prob_last_coded
, 8);
913 vp8_write_literal(w
, prob_gf_coded
, 8);
915 update_mbintra_mode_probs(cpi
);
917 vp8_write_mvprobs(cpi
);
919 while (++mb_row
< pc
->mb_rows
)
923 while (++mb_col
< pc
->mb_cols
)
925 const MB_MODE_INFO
*const mi
= & m
->mbmi
;
926 const MV_REFERENCE_FRAME rf
= mi
->ref_frame
;
927 const MB_PREDICTION_MODE mode
= mi
->mode
;
929 MACROBLOCKD
*xd
= &cpi
->mb
.e_mbd
;
931 // Distance of Mb to the various image edges.
932 // These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
933 xd
->mb_to_left_edge
= -((mb_col
* 16) << 3);
934 xd
->mb_to_right_edge
= ((pc
->mb_cols
- 1 - mb_col
) * 16) << 3;
935 xd
->mb_to_top_edge
= -((mb_row
* 16)) << 3;
936 xd
->mb_to_bottom_edge
= ((pc
->mb_rows
- 1 - mb_row
) * 16) << 3;
942 if (cpi
->mb
.e_mbd
.update_mb_segmentation_map
)
943 write_mb_features(w
, mi
, &cpi
->mb
.e_mbd
);
945 if (pc
->mb_no_coeff_skip
)
946 vp8_encode_bool(w
, m
->mbmi
.mb_skip_coeff
, prob_skip_false
);
948 if (rf
== INTRA_FRAME
)
950 vp8_write(w
, 0, cpi
->prob_intra_coded
);
954 write_ymode(w
, mode
, pc
->fc
.ymode_prob
);
961 write_bmode(w
, m
->bmi
[j
].mode
, pc
->fc
.bmode_prob
);
966 write_uv_mode(w
, mi
->uv_mode
, pc
->fc
.uv_mode_prob
);
968 else /* inter coded */
971 vp8_prob mv_ref_p
[VP8_MVREFS
-1];
973 vp8_write(w
, 1, cpi
->prob_intra_coded
);
975 if (rf
== LAST_FRAME
)
976 vp8_write(w
, 0, prob_last_coded
);
979 vp8_write(w
, 1, prob_last_coded
);
980 vp8_write(w
, (rf
== GOLDEN_FRAME
) ? 0 : 1, prob_gf_coded
);
987 vp8_find_near_mvs(xd
, m
, &n1
, &n2
, &best_mv
, ct
, rf
, cpi
->common
.ref_frame_sign_bias
);
988 vp8_mv_ref_probs(mv_ref_p
, ct
);
991 accum_mv_refs(mode
, ct
);
1000 write_mv_ref(w
, mode
, mv_ref_p
);
1002 switch (mode
) /* new, split require MVs */
1006 #ifdef ENTROPY_STATS
1010 write_mv(w
, &mi
->mv
.as_mv
, &best_mv
, mvc
);
1018 ++count_mb_seg
[mi
->partitioning
];
1021 write_split(w
, mi
->partitioning
);
1025 const B_MODE_INFO
*const b
= cpi
->mb
.partition_info
->bmi
+ j
;
1026 const int *const L
= vp8_mbsplits
[mi
->partitioning
];
1027 int k
= -1; /* first block in subset j */
1034 mv_contz
= vp8_mv_cont
1035 (&(vp8_left_bmi(m
, k
)->mv
.as_mv
),
1036 &(vp8_above_bmi(m
, k
, mis
)->mv
.as_mv
));
1037 write_sub_mv_ref(w
, b
->mode
, vp8_sub_mv_ref_prob2
[mv_contz
]); //pc->fc.sub_mv_ref_prob);
1039 if (b
->mode
== NEW4X4
)
1041 #ifdef ENTROPY_STATS
1042 active_section
= 11;
1044 write_mv(w
, &b
->mv
.as_mv
, &best_mv
, (const MV_CONTEXT
*) mvc
);
1047 while (++j
< cpi
->mb
.partition_info
->count
);
1056 cpi
->mb
.partition_info
++;
1059 ++m
; /* skip L prediction border */
1060 cpi
->mb
.partition_info
++;
1065 static void write_kfmodes(VP8_COMP
*cpi
)
1067 vp8_writer
*const bc
= & cpi
->bc
;
1068 const VP8_COMMON
*const c
= & cpi
->common
;
1070 MODE_INFO
*m
= c
->mi
;
1073 int prob_skip_false
= 0;
1075 if (c
->mb_no_coeff_skip
)
1077 prob_skip_false
= cpi
->skip_false_count
* 256 / (cpi
->skip_false_count
+ cpi
->skip_true_count
);
1079 if (prob_skip_false
<= 1)
1080 prob_skip_false
= 1;
1082 if (prob_skip_false
>= 255)
1083 prob_skip_false
= 255;
1085 cpi
->prob_skip_false
= prob_skip_false
;
1086 vp8_write_literal(bc
, prob_skip_false
, 8);
1089 while (++mb_row
< c
->mb_rows
)
1093 while (++mb_col
< c
->mb_cols
)
1095 const int ym
= m
->mbmi
.mode
;
1097 if (cpi
->mb
.e_mbd
.update_mb_segmentation_map
)
1098 write_mb_features(bc
, &m
->mbmi
, &cpi
->mb
.e_mbd
);
1100 if (c
->mb_no_coeff_skip
)
1101 vp8_encode_bool(bc
, m
->mbmi
.mb_skip_coeff
, prob_skip_false
);
1103 kfwrite_ymode(bc
, ym
, c
->kf_ymode_prob
);
1107 const int mis
= c
->mode_info_stride
;
1112 const B_PREDICTION_MODE A
= vp8_above_bmi(m
, i
, mis
)->mode
;
1113 const B_PREDICTION_MODE L
= vp8_left_bmi(m
, i
)->mode
;
1114 const int bm
= m
->bmi
[i
].mode
;
1116 #ifdef ENTROPY_STATS
1117 ++intra_mode_stats
[A
] [L
] [bm
];
1120 write_bmode(bc
, bm
, c
->kf_bmode_prob
[A
] [L
]);
1125 write_uv_mode(bc
, (m
++)->mbmi
.uv_mode
, c
->kf_uv_mode_prob
);
1128 m
++; // skip L prediction border
1131 int vp8_estimate_entropy_savings(VP8_COMP
*cpi
)
1136 const int *const rfct
= cpi
->count_mb_ref_frame_usage
;
1137 const int rf_intra
= rfct
[INTRA_FRAME
];
1138 const int rf_inter
= rfct
[LAST_FRAME
] + rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
];
1139 int new_intra
, new_last
, gf_last
, oldtotal
, newtotal
;
1140 int ref_frame_cost
[MAX_REF_FRAMES
];
1142 vp8_clear_system_state(); //__asm emms;
1144 if (cpi
->common
.frame_type
!= KEY_FRAME
)
1146 if (!(new_intra
= rf_intra
* 255 / (rf_intra
+ rf_inter
)))
1149 new_last
= rf_inter
? (rfct
[LAST_FRAME
] * 255) / rf_inter
: 128;
1151 gf_last
= (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
])
1152 ? (rfct
[GOLDEN_FRAME
] * 255) / (rfct
[GOLDEN_FRAME
] + rfct
[ALTREF_FRAME
]) : 128;
1155 ref_frame_cost
[INTRA_FRAME
] = vp8_cost_zero(new_intra
);
1156 ref_frame_cost
[LAST_FRAME
] = vp8_cost_one(new_intra
)
1157 + vp8_cost_zero(new_last
);
1158 ref_frame_cost
[GOLDEN_FRAME
] = vp8_cost_one(new_intra
)
1159 + vp8_cost_one(new_last
)
1160 + vp8_cost_zero(gf_last
);
1161 ref_frame_cost
[ALTREF_FRAME
] = vp8_cost_one(new_intra
)
1162 + vp8_cost_one(new_last
)
1163 + vp8_cost_one(gf_last
);
1166 rfct
[INTRA_FRAME
] * ref_frame_cost
[INTRA_FRAME
] +
1167 rfct
[LAST_FRAME
] * ref_frame_cost
[LAST_FRAME
] +
1168 rfct
[GOLDEN_FRAME
] * ref_frame_cost
[GOLDEN_FRAME
] +
1169 rfct
[ALTREF_FRAME
] * ref_frame_cost
[ALTREF_FRAME
];
1173 ref_frame_cost
[INTRA_FRAME
] = vp8_cost_zero(cpi
->prob_intra_coded
);
1174 ref_frame_cost
[LAST_FRAME
] = vp8_cost_one(cpi
->prob_intra_coded
)
1175 + vp8_cost_zero(cpi
->prob_last_coded
);
1176 ref_frame_cost
[GOLDEN_FRAME
] = vp8_cost_one(cpi
->prob_intra_coded
)
1177 + vp8_cost_one(cpi
->prob_last_coded
)
1178 + vp8_cost_zero(cpi
->prob_gf_coded
);
1179 ref_frame_cost
[ALTREF_FRAME
] = vp8_cost_one(cpi
->prob_intra_coded
)
1180 + vp8_cost_one(cpi
->prob_last_coded
)
1181 + vp8_cost_one(cpi
->prob_gf_coded
);
1184 rfct
[INTRA_FRAME
] * ref_frame_cost
[INTRA_FRAME
] +
1185 rfct
[LAST_FRAME
] * ref_frame_cost
[LAST_FRAME
] +
1186 rfct
[GOLDEN_FRAME
] * ref_frame_cost
[GOLDEN_FRAME
] +
1187 rfct
[ALTREF_FRAME
] * ref_frame_cost
[ALTREF_FRAME
];
1189 savings
+= (oldtotal
- newtotal
) / 256;
1203 /* at every context */
1205 /* calc probs and branch cts for this frame only */
1206 //vp8_prob new_p [vp8_coef_tokens-1];
1207 //unsigned int branch_ct [vp8_coef_tokens-1] [2];
1209 int t
= 0; /* token/prob index */
1211 vp8_tree_probs_from_distribution(
1212 vp8_coef_tokens
, vp8_coef_encodings
, vp8_coef_tree
,
1213 cpi
->frame_coef_probs
[i
][j
][k
], cpi
->frame_branch_ct
[i
][j
][k
], cpi
->coef_counts
[i
][j
][k
],
1219 const unsigned int *ct
= cpi
->frame_branch_ct
[i
][j
][k
][t
];
1220 const vp8_prob newp
= cpi
->frame_coef_probs
[i
][j
][k
][t
];
1222 const vp8_prob old
= cpi
->common
.fc
.coef_probs
[i
][j
][k
][t
];
1223 const vp8_prob upd
= vp8_coef_update_probs
[i
][j
][k
][t
];
1225 const int old_b
= vp8_cost_branch(ct
, old
);
1226 const int new_b
= vp8_cost_branch(ct
, newp
);
1228 const int update_b
= 8 +
1229 ((vp8_cost_one(upd
) - vp8_cost_zero(upd
)) >> 8);
1231 const int s
= old_b
- new_b
- update_b
;
1238 while (++t
< vp8_coef_tokens
- 1);
1242 while (++k
< PREV_COEF_CONTEXTS
);
1244 while (++j
< COEF_BANDS
);
1246 while (++i
< BLOCK_TYPES
);
1251 static void update_coef_probs(VP8_COMP
*cpi
)
1254 vp8_writer
*const w
= & cpi
->bc
;
1257 vp8_clear_system_state(); //__asm emms;
1270 //note: use result from vp8_estimate_entropy_savings, so no need to call vp8_tree_probs_from_distribution here.
1271 /* at every context */
1273 /* calc probs and branch cts for this frame only */
1274 //vp8_prob new_p [vp8_coef_tokens-1];
1275 //unsigned int branch_ct [vp8_coef_tokens-1] [2];
1277 int t
= 0; /* token/prob index */
1279 //vp8_tree_probs_from_distribution(
1280 // vp8_coef_tokens, vp8_coef_encodings, vp8_coef_tree,
1281 // new_p, branch_ct, (unsigned int *)cpi->coef_counts [i][j][k],
1287 const unsigned int *ct
= cpi
->frame_branch_ct
[i
][j
][k
][t
];
1288 const vp8_prob newp
= cpi
->frame_coef_probs
[i
][j
][k
][t
];
1290 vp8_prob
*Pold
= cpi
->common
.fc
.coef_probs
[i
][j
][k
] + t
;
1291 const vp8_prob old
= *Pold
;
1292 const vp8_prob upd
= vp8_coef_update_probs
[i
][j
][k
][t
];
1294 const int old_b
= vp8_cost_branch(ct
, old
);
1295 const int new_b
= vp8_cost_branch(ct
, newp
);
1297 const int update_b
= 8 +
1298 ((vp8_cost_one(upd
) - vp8_cost_zero(upd
)) >> 8);
1300 const int s
= old_b
- new_b
- update_b
;
1301 const int u
= s
> 0 ? 1 : 0;
1303 vp8_write(w
, u
, upd
);
1306 #ifdef ENTROPY_STATS
1307 ++ tree_update_hist
[i
][j
][k
][t
] [u
];
1312 /* send/use new probability */
1315 vp8_write_literal(w
, newp
, 8);
1322 while (++t
< vp8_coef_tokens
- 1);
1324 /* Accum token counts for generation of default statistics */
1325 #ifdef ENTROPY_STATS
1330 context_counters
[i
][j
][k
][t
] += cpi
->coef_counts
[i
][j
][k
][t
];
1332 while (++t
< vp8_coef_tokens
);
1337 while (++k
< PREV_COEF_CONTEXTS
);
1339 while (++j
< COEF_BANDS
);
1341 while (++i
< BLOCK_TYPES
);
1344 #ifdef PACKET_TESTING
1348 static void put_delta_q(vp8_writer
*bc
, int delta_q
)
1352 vp8_write_bit(bc
, 1);
1353 vp8_write_literal(bc
, abs(delta_q
), 4);
1356 vp8_write_bit(bc
, 1);
1358 vp8_write_bit(bc
, 0);
1361 vp8_write_bit(bc
, 0);
1364 void vp8_pack_bitstream(VP8_COMP
*cpi
, unsigned char *dest
, unsigned long *size
)
1368 VP8_COMMON
*const pc
= & cpi
->common
;
1369 vp8_writer
*const bc
= & cpi
->bc
;
1370 MACROBLOCKD
*const xd
= & cpi
->mb
.e_mbd
;
1371 int extra_bytes_packed
= 0;
1373 unsigned char *cx_data
= dest
;
1374 const int *mb_feature_data_bits
;
1376 oh
.show_frame
= (int) pc
->show_frame
;
1377 oh
.type
= (int)pc
->frame_type
;
1378 oh
.version
= pc
->version
;
1380 mb_feature_data_bits
= vp8_mb_feature_data_bits
;
1383 #if defined(SECTIONBITS_OUTPUT)
1384 Sectionbits
[active_section
= 1] += sizeof(VP8_HEADER
) * 8 * 256;
1387 //vp8_kf_default_bmode_probs() is called in vp8_setup_key_frame() once for each
1388 //K frame before encode frame. pc->kf_bmode_prob doesn't get changed anywhere
1389 //else. No need to call it again here. --yw
1390 //vp8_kf_default_bmode_probs( pc->kf_bmode_prob);
1392 // every keyframe send startcode, width, height, scale factor, clamp and color type
1393 if (oh
.type
== KEY_FRAME
)
1395 // Start / synch code
1400 *((unsigned short *)(cx_data
+ 3)) = make_endian_16((pc
->horiz_scale
<< 14) | pc
->Width
);
1401 *((unsigned short *)(cx_data
+ 5)) = make_endian_16((pc
->vert_scale
<< 14) | pc
->Height
);
1403 extra_bytes_packed
= 7;
1404 cx_data
+= extra_bytes_packed
;
1406 vp8_start_encode(bc
, cx_data
);
1409 vp8_write_bit(bc
, pc
->clr_type
);
1410 vp8_write_bit(bc
, pc
->clamp_type
);
1414 vp8_start_encode(bc
, cx_data
);
1417 // Signal whether or not Segmentation is enabled
1418 vp8_write_bit(bc
, (xd
->segmentation_enabled
) ? 1 : 0);
1420 // Indicate which features are enabled
1421 if (xd
->segmentation_enabled
)
1423 // Signal whether or not the segmentation map is being updated.
1424 vp8_write_bit(bc
, (xd
->update_mb_segmentation_map
) ? 1 : 0);
1425 vp8_write_bit(bc
, (xd
->update_mb_segmentation_data
) ? 1 : 0);
1427 if (xd
->update_mb_segmentation_data
)
1431 vp8_write_bit(bc
, (xd
->mb_segement_abs_delta
) ? 1 : 0);
1433 // For each segmentation feature (Quant and loop filter level)
1434 for (i
= 0; i
< MB_LVL_MAX
; i
++)
1436 // For each of the segments
1437 for (j
= 0; j
< MAX_MB_SEGMENTS
; j
++)
1439 Data
= xd
->segment_feature_data
[i
][j
];
1444 vp8_write_bit(bc
, 1);
1449 vp8_write_literal(bc
, Data
, mb_feature_data_bits
[i
]);
1450 vp8_write_bit(bc
, 1);
1454 vp8_write_literal(bc
, Data
, mb_feature_data_bits
[i
]);
1455 vp8_write_bit(bc
, 0);
1459 vp8_write_bit(bc
, 0);
1464 if (xd
->update_mb_segmentation_map
)
1466 // Write the probs used to decode the segment id for each macro block.
1467 for (i
= 0; i
< MB_FEATURE_TREE_PROBS
; i
++)
1469 int Data
= xd
->mb_segment_tree_probs
[i
];
1473 vp8_write_bit(bc
, 1);
1474 vp8_write_literal(bc
, Data
, 8);
1477 vp8_write_bit(bc
, 0);
1482 // Code to determine whether or not to update the scan order.
1483 vp8_write_bit(bc
, pc
->filter_type
);
1484 vp8_write_literal(bc
, pc
->filter_level
, 6);
1485 vp8_write_literal(bc
, pc
->sharpness_level
, 3);
1487 // Write out loop filter deltas applied at the MB level based on mode or ref frame (if they are enabled).
1488 vp8_write_bit(bc
, (xd
->mode_ref_lf_delta_enabled
) ? 1 : 0);
1490 if (xd
->mode_ref_lf_delta_enabled
)
1492 // Do the deltas need to be updated
1493 int send_update
= xd
->mode_ref_lf_delta_update
1494 || cpi
->oxcf
.error_resilient_mode
;
1496 vp8_write_bit(bc
, send_update
);
1502 for (i
= 0; i
< MAX_REF_LF_DELTAS
; i
++)
1504 Data
= xd
->ref_lf_deltas
[i
];
1507 if (xd
->ref_lf_deltas
[i
] != xd
->last_ref_lf_deltas
[i
]
1508 || cpi
->oxcf
.error_resilient_mode
)
1510 xd
->last_ref_lf_deltas
[i
] = xd
->ref_lf_deltas
[i
];
1511 vp8_write_bit(bc
, 1);
1515 vp8_write_literal(bc
, (Data
& 0x3F), 6);
1516 vp8_write_bit(bc
, 0); // sign
1521 vp8_write_literal(bc
, (Data
& 0x3F), 6);
1522 vp8_write_bit(bc
, 1); // sign
1526 vp8_write_bit(bc
, 0);
1530 for (i
= 0; i
< MAX_MODE_LF_DELTAS
; i
++)
1532 Data
= xd
->mode_lf_deltas
[i
];
1534 if (xd
->mode_lf_deltas
[i
] != xd
->last_mode_lf_deltas
[i
]
1535 || cpi
->oxcf
.error_resilient_mode
)
1537 xd
->last_mode_lf_deltas
[i
] = xd
->mode_lf_deltas
[i
];
1538 vp8_write_bit(bc
, 1);
1542 vp8_write_literal(bc
, (Data
& 0x3F), 6);
1543 vp8_write_bit(bc
, 0); // sign
1548 vp8_write_literal(bc
, (Data
& 0x3F), 6);
1549 vp8_write_bit(bc
, 1); // sign
1553 vp8_write_bit(bc
, 0);
1558 //signal here is multi token partition is enabled
1559 vp8_write_literal(bc
, pc
->multi_token_partition
, 2);
1561 // Frame Qbaseline quantizer index
1562 vp8_write_literal(bc
, pc
->base_qindex
, 7);
1564 // Transmit Dc, Second order and Uv quantizer delta information
1565 put_delta_q(bc
, pc
->y1dc_delta_q
);
1566 put_delta_q(bc
, pc
->y2dc_delta_q
);
1567 put_delta_q(bc
, pc
->y2ac_delta_q
);
1568 put_delta_q(bc
, pc
->uvdc_delta_q
);
1569 put_delta_q(bc
, pc
->uvac_delta_q
);
1571 // When there is a key frame all reference buffers are updated using the new key frame
1572 if (pc
->frame_type
!= KEY_FRAME
)
1574 // Should the GF or ARF be updated using the transmitted frame or buffer
1575 vp8_write_bit(bc
, pc
->refresh_golden_frame
);
1576 vp8_write_bit(bc
, pc
->refresh_alt_ref_frame
);
1578 // If not being updated from current frame should either GF or ARF be updated from another buffer
1579 if (!pc
->refresh_golden_frame
)
1580 vp8_write_literal(bc
, pc
->copy_buffer_to_gf
, 2);
1582 if (!pc
->refresh_alt_ref_frame
)
1583 vp8_write_literal(bc
, pc
->copy_buffer_to_arf
, 2);
1585 // Indicate reference frame sign bias for Golden and ARF frames (always 0 for last frame buffer)
1586 vp8_write_bit(bc
, pc
->ref_frame_sign_bias
[GOLDEN_FRAME
]);
1587 vp8_write_bit(bc
, pc
->ref_frame_sign_bias
[ALTREF_FRAME
]);
1590 vp8_write_bit(bc
, pc
->refresh_entropy_probs
);
1592 if (pc
->frame_type
!= KEY_FRAME
)
1593 vp8_write_bit(bc
, pc
->refresh_last_frame
);
1595 #ifdef ENTROPY_STATS
1597 if (pc
->frame_type
== INTER_FRAME
)
1604 vp8_clear_system_state(); //__asm emms;
1606 //************************************************
1607 // save a copy for later refresh
1609 vpx_memcpy(&cpi
->common
.lfc
, &cpi
->common
.fc
, sizeof(cpi
->common
.fc
));
1612 update_coef_probs(cpi
);
1614 #ifdef ENTROPY_STATS
1618 // Write out the mb_no_coeff_skip flag
1619 vp8_write_bit(bc
, pc
->mb_no_coeff_skip
);
1621 if (pc
->frame_type
== KEY_FRAME
)
1625 #ifdef ENTROPY_STATS
1631 pack_inter_mode_mvs(cpi
);
1633 #ifdef ENTROPY_STATS
1638 vp8_stop_encode(bc
);
1641 if (pc
->multi_token_partition
!= ONE_PARTITION
)
1645 num_part
= 1 << pc
->multi_token_partition
;
1647 pack_tokens_into_partitions(cpi
, cx_data
+ bc
->pos
, num_part
, &asize
);
1649 oh
.first_partition_length_in_bytes
= cpi
->bc
.pos
;
1651 *size
= cpi
->bc
.pos
+ VP8_HEADER_SIZE
+ asize
+ extra_bytes_packed
;
1655 vp8_start_encode(&cpi
->bc2
, cx_data
+ bc
->pos
);
1657 if (!cpi
->b_multi_threaded
)
1658 pack_tokens(&cpi
->bc2
, cpi
->tok
, cpi
->tok_count
);
1660 pack_mb_row_tokens(cpi
, &cpi
->bc2
);
1662 vp8_stop_encode(&cpi
->bc2
);
1663 oh
.first_partition_length_in_bytes
= cpi
->bc
.pos
;
1664 *size
= cpi
->bc2
.pos
+ cpi
->bc
.pos
+ VP8_HEADER_SIZE
+ extra_bytes_packed
;
1667 #if CONFIG_BIG_ENDIAN
1669 int v
= (oh
.first_partition_length_in_bytes
<< 5) |
1670 (oh
.show_frame
<< 4) |
1674 v
= make_endian_32(v
);
1675 vpx_memcpy(dest
, &v
, 3);
1678 vpx_memcpy(dest
, &oh
, 3);
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");