29 #define UNCHECKED_BITSTREAM_READER 1
40 0, 1, 2, 4, 8, 3, 5,10,12,15, 7,11,13,14, 6, 9,
44 15, 0, 7,11,13,14, 3, 5,10,12, 1, 2, 4, 8, 6, 9,
90 6, 2, 0, 0, 8, 6, 3, 0, 9, 8, 7, 5, 10, 9, 8, 6,
91 11,10, 9, 7, 13,11,10, 8, 13,13,11, 9, 13,13,13,10,
92 14,14,13,11, 14,14,14,13, 15,15,14,14, 15,15,15,14,
93 16,15,15,15, 16,16,16,15, 16,16,16,16, 16,16,16,16,
97 6, 2, 0, 0, 6, 5, 3, 0, 7, 6, 6, 4, 8, 6, 6, 4,
98 8, 7, 7, 5, 9, 8, 8, 6, 11, 9, 9, 6, 11,11,11, 7,
99 12,11,11, 9, 12,12,12,11, 12,12,12,11, 13,13,13,12,
100 13,13,13,13, 13,14,13,13, 14,14,14,13, 14,14,14,14,
104 6, 4, 0, 0, 6, 5, 4, 0, 6, 5, 5, 4, 7, 5, 5, 4,
105 7, 5, 5, 4, 7, 6, 6, 4, 7, 6, 6, 4, 8, 7, 7, 5,
106 8, 8, 7, 6, 9, 8, 8, 7, 9, 9, 8, 8, 9, 9, 9, 8,
107 10, 9, 9, 9, 10,10,10,10, 10,10,10,10, 10,10,10,10,
111 6, 6, 0, 0, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6,
112 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
113 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
114 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
121 5, 1, 0, 0, 7, 4, 1, 0, 7, 6, 5, 3, 7, 6, 5, 3,
122 7, 6, 5, 4, 15, 6, 5, 4, 11,14, 5, 4, 8,10,13, 4,
123 15,14, 9, 4, 11,10,13,12, 15,14, 9,12, 11,10,13, 8,
124 15, 1, 9,12, 11,14,13, 8, 7,10, 9,12, 4, 6, 5, 8,
128 11, 2, 0, 0, 7, 7, 3, 0, 7,10, 9, 5, 7, 6, 5, 4,
129 4, 6, 5, 6, 7, 6, 5, 8, 15, 6, 5, 4, 11,14,13, 4,
130 15,10, 9, 4, 11,14,13,12, 8,10, 9, 8, 15,14,13,12,
131 11,10, 9,12, 7,11, 6, 8, 9, 8,10, 1, 7, 6, 5, 4,
135 15,14, 0, 0, 11,15,13, 0, 8,12,14,12, 15,10,11,11,
136 11, 8, 9,10, 9,14,13, 9, 8,10, 9, 8, 15,14,13,13,
137 11,14,10,12, 15,10,13,12, 11,14, 9,12, 8,10,13, 8,
138 13, 7, 9,12, 9,12,11,10, 5, 8, 7, 6, 1, 4, 3, 2,
142 0, 1, 0, 0, 4, 5, 6, 0, 8, 9,10,11, 12,13,14,15,
143 16,17,18,19, 20,21,22,23, 24,25,26,27, 28,29,30,31,
144 32,33,34,35, 36,37,38,39, 40,41,42,43, 44,45,46,47,
145 48,49,50,51, 52,53,54,55, 56,57,58,59, 60,61,62,63,
150 {1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
151 {3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
152 {4,3,3,3,4,4,3,3,4,5,5,6,5,6},
153 {5,3,4,4,3,3,3,4,3,4,5,5,5},
154 {4,4,4,3,3,3,3,3,4,5,4,5},
155 {6,5,3,3,3,3,3,3,4,3,6},
156 {6,5,3,3,3,2,3,4,3,6},
168 {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
169 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
170 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
171 {3,7,5,4,6,5,4,3,3,2,2,1,0},
172 {5,4,3,7,6,5,4,3,2,1,1,0},
173 {1,1,7,6,5,4,3,2,1,1,0},
174 {1,1,5,4,3,3,2,1,1,0},
198 { 1, 3, 3, 4, 4, 4, 5, 5 },
199 { 3, 2, 3, 3, 3, 3, 3 },
200 { 3, 3, 2, 2, 3, 3 },
208 { 1, 2, 3, 2, 3, 1, 1, 0 },
209 { 0, 1, 1, 4, 5, 6, 7 },
210 { 0, 1, 1, 2, 6, 7 },
224 {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
234 {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
269 #define LEVEL_TAB_BITS 8
272 #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
273 #define CHROMA422_DC_COEFF_TOKEN_VLC_BITS 13
274 #define COEFF_TOKEN_VLC_BITS 8
275 #define TOTAL_ZEROS_VLC_BITS 9
276 #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
277 #define CHROMA422_DC_TOTAL_ZEROS_VLC_BITS 5
278 #define RUN_VLC_BITS 3
279 #define RUN7_VLC_BITS 6
287 const int index8=
scan8[n];
292 if(
i<64)
i= (
i+1)>>1;
303 for(suffix_length=0; suffix_length<7; suffix_length++){
308 int level_code = (prefix << suffix_length) +
309 (
i >> (
av_log2(
i) - suffix_length)) - (1 << suffix_length);
310 int mask = -(level_code&1);
311 level_code = (((2 + level_code) >> 1) ^
mask) -
mask;
344 for (
int i = 0;
i < 4;
i++) {
360 for (
int i = 0;
i < 3;
i++) {
370 for (
int i = 0;
i < 7;
i++) {
380 for (
int i = 0;
i < 15;
i++) {
390 for (
int i = 0;
i < 6;
i++) {
434 const uint8_t *scantable,
const uint32_t *qmul,
437 static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
439 int zeros_left, coeff_token, total_coeff,
i, trailing_ones, run_before;
448 total_coeff= coeff_token>>2;
453 total_coeff= coeff_token>>2;
457 total_coeff= coeff_token>>2;
466 if(total_coeff > (
unsigned)max_coeff) {
471 trailing_ones= coeff_token&3;
472 ff_tlog(
h->avctx,
"trailing:%d, total:%d\n", trailing_ones, total_coeff);
481 if(trailing_ones<total_coeff) {
483 int suffix_length = total_coeff > 10 & trailing_ones < 3;
488 if(level_code >= 100){
489 prefix= level_code - 100;
499 }
else if(prefix==14){
503 level_code= prefix +
get_bits(gb, 4);
511 level_code += (1<<(prefix-3))-4096;
513 level_code +=
get_bits(gb, prefix-3);
516 if(trailing_ones < 3) level_code += 2;
519 mask= -(level_code&1);
522 level_code += ((level_code>>31)|1) & -(trailing_ones < 3);
524 suffix_length = 1 + (level_code + 3
U > 6
U);
525 level[trailing_ones]= level_code;
529 for(
i=trailing_ones+1;
i<total_coeff;
i++) {
530 static const unsigned int suffix_limit[7] = {0,3,6,12,24,48,INT_MAX };
535 if(level_code >= 100){
536 prefix= level_code - 100;
541 level_code = (prefix<<suffix_length) +
get_bits(gb, suffix_length);
543 level_code = 15<<suffix_length;
549 level_code += (1<<(prefix-3))-4096;
551 level_code +=
get_bits(gb, prefix-3);
553 mask= -(level_code&1);
554 level_code= (((2+level_code)>>1) ^
mask) -
mask;
557 suffix_length+= suffix_limit[suffix_length] + level_code > 2
U*suffix_limit[suffix_length];
561 if(total_coeff == max_coeff)
564 if (max_coeff <= 8) {
576 #define STORE_BLOCK(type) \
577 scantable += zeros_left + total_coeff - 1; \
578 if(n >= LUMA_DC_BLOCK_INDEX){ \
579 ((type*)block)[*scantable] = level[0]; \
580 for(i=1;i<total_coeff && zeros_left > 0;i++) { \
582 run_before= get_vlc2(gb, run_vlc[zeros_left].table, RUN_VLC_BITS, 1); \
584 run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); \
585 zeros_left -= run_before; \
586 scantable -= 1 + run_before; \
587 ((type*)block)[*scantable]= level[i]; \
589 for(;i<total_coeff;i++) { \
591 ((type*)block)[*scantable]= level[i]; \
594 ((type*)block)[*scantable] = ((int)(level[0] * qmul[*scantable] + 32))>>6; \
595 for(i=1;i<total_coeff && zeros_left > 0;i++) { \
597 run_before= get_vlc2(gb, run_vlc[zeros_left].table, RUN_VLC_BITS, 1); \
599 run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); \
600 zeros_left -= run_before; \
601 scantable -= 1 + run_before; \
602 ((type*)block)[*scantable]= ((int)(level[i] * qmul[*scantable] + 32))>>6; \
604 for(;i<total_coeff;i++) { \
606 ((type*)block)[*scantable]= ((int)(level[i] * qmul[*scantable] + 32))>>6; \
610 if (
h->pixel_shift) {
627 const uint8_t *scan8x8,
int pixel_shift,
628 int mb_type,
int cbp,
int p)
644 for(i8x8=0; i8x8<4; i8x8++){
645 for(i4x4=0; i4x4<4; i4x4++){
646 const int index= i4x4 + 4*i8x8 + p*16;
648 index, scan + 1,
h->ps.pps->dequant4_coeff[p][qscale], 15) < 0 ){
659 int cqm = (
IS_INTRA( mb_type ) ? 0:3)+p;
662 for(i8x8=0; i8x8<4; i8x8++){
665 int16_t *buf = &sl->
mb[64*i8x8+256*p << pixel_shift];
667 for(i4x4=0; i4x4<4; i4x4++){
668 const int index= i4x4 + 4*i8x8 + p*16;
670 h->ps.pps->dequant8_coeff[cqm][qscale], 16) < 0 )
674 nnz[0] += nnz[1] + nnz[8] + nnz[9];
675 new_cbp |= !!nnz[0] << i8x8;
677 for(i4x4=0; i4x4<4; i4x4++){
678 const int index= i4x4 + 4*i8x8 + p*16;
680 scan,
h->ps.pps->dequant4_coeff[cqm][qscale], 16) < 0 ){
688 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
699 unsigned int mb_type, cbp;
700 int dct8x8_allowed =
h->ps.pps->transform_8x8_mode;
701 const int decode_chroma =
h->ps.sps->chroma_format_idc == 1 ||
h->ps.sps->chroma_format_idc == 2;
702 const int pixel_shift =
h->pixel_shift;
712 if (mb_skip_run >
h->mb_num) {
729 if ((sl->
mb_y & 1) == 0)
742 goto decode_intra_mb;
750 goto decode_intra_mb;
774 h->ps.sps->bit_depth_luma;
785 h->cur_pic.qscale_table[mb_xy] = 0;
787 memset(
h->non_zero_count[mb_xy], 16, 48);
789 h->cur_pic.mb_type[mb_xy] = mb_type;
809 for(
i=0;
i<16;
i+=di){
814 mode = rem_mode + (rem_mode >=
mode);
841 }
else if(partition_count==4){
842 int i, j, sub_partition_count[4],
list,
ref[2][4];
882 }
else if(ref_count == 2){
914 for(j=0; j<sub_partition_count[
i]; j++){
916 const int index= 4*
i + block_width*j;
921 ff_tlog(
h->avctx,
"final mv:%d %d\n", mx, my);
925 mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
927 mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
929 mv_cache[ 1 ][0]= mx;
930 mv_cache[ 1 ][1]= my;
932 mv_cache[ 8 ][0]= mx;
933 mv_cache[ 8 ][1]= my;
935 mv_cache[ 0 ][0]= mx;
936 mv_cache[ 0 ][1]= my;
947 dct8x8_allowed &=
h->ps.sps->direct_8x8_inference_flag;
958 }
else if (rc == 2) {
975 ff_tlog(
h->avctx,
"final mv:%d %d\n", mx, my);
989 }
else if (rc == 2) {
1010 ff_tlog(
h->avctx,
"final mv:%d %d\n", mx, my);
1027 }
else if (rc == 2) {
1048 ff_tlog(
h->avctx,
"final mv:%d %d\n", mx, my);
1083 if (!decode_chroma && cbp>15) {
1089 if(dct8x8_allowed && (cbp&15) && !
IS_INTRA(mb_type)){
1093 h->cbp_table[mb_xy]= cbp;
1094 h->cur_pic.mb_type[mb_xy] = mb_type;
1097 int i4x4, i8x8, chroma_idx;
1101 const uint8_t *scan, *scan8x8;
1102 const int max_qp = 51 + 6 * (
h->ps.sps->bit_depth_luma - 8);
1106 sl->
qscale += (unsigned)dquant;
1108 if (((
unsigned)sl->
qscale) > max_qp){
1110 else sl->
qscale -= max_qp+1;
1111 if (((
unsigned)sl->
qscale) > max_qp){
1122 scan8x8 = sl->
qscale ?
h->field_scan8x8_cavlc :
h->field_scan8x8_cavlc_q0;
1123 scan = sl->
qscale ?
h->field_scan :
h->field_scan_q0;
1125 scan8x8 = sl->
qscale ?
h->zigzag_scan8x8_cavlc :
h->zigzag_scan8x8_cavlc_q0;
1126 scan = sl->
qscale ?
h->zigzag_scan :
h->zigzag_scan_q0;
1132 h->cbp_table[mb_xy] |=
ret << 12;
1141 const int num_c8x8 =
h->ps.sps->chroma_format_idc;
1144 for(chroma_idx=0; chroma_idx<2; chroma_idx++)
1148 NULL, 4 * num_c8x8) < 0) {
1154 for(chroma_idx=0; chroma_idx<2; chroma_idx++){
1155 const uint32_t *qmul =
h->ps.pps->dequant4_coeff[chroma_idx+1+(
IS_INTRA( mb_type ) ? 0:3)][sl->
chroma_qp[chroma_idx]];
1156 int16_t *
mb = sl->
mb + (16*(16 + 16*chroma_idx) << pixel_shift);
1157 for (i8x8 = 0; i8x8<num_c8x8; i8x8++) {
1158 for (i4x4 = 0; i4x4 < 4; i4x4++) {
1159 const int index = 16 + 16*chroma_idx + 8*i8x8 + i4x4;
1162 mb += 16 << pixel_shift;
1176 h->cur_pic.qscale_table[mb_xy] = sl->
qscale;