static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){

    MpegEncContext * const s = &h->s;

    const uint16_t *qmul= dequant_coeff[qp];

    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};

    int level[16], run[16];

    int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;



    //FIXME put trailing_onex into the context



    if(n == CHROMA_DC_BLOCK_INDEX){

        coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);

        total_coeff= coeff_token>>2;

    }else{    

        if(n == LUMA_DC_BLOCK_INDEX){

            total_coeff= pred_non_zero_count(h, 0);

            coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);

            total_coeff= coeff_token>>2;

        }else{

            total_coeff= pred_non_zero_count(h, n);

            coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);

            total_coeff= coeff_token>>2;

            h->non_zero_count_cache[ scan8[n] ]= total_coeff;

        }

    }



    //FIXME set last_non_zero?



    if(total_coeff==0)

        return 0;

        

    trailing_ones= coeff_token&3;

    tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);

    assert(total_coeff<=16);

    

    for(i=0; i<trailing_ones; i++){

        level[i]= 1 - 2*get_bits1(gb);

    }



    suffix_length= total_coeff > 10 && trailing_ones < 3;



    for(; i<total_coeff; i++){

        const int prefix= get_level_prefix(gb);

        int level_code, mask;



        if(prefix<14){ //FIXME try to build a large unified VLC table for all this

            if(suffix_length)

                level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part

            else

                level_code= (prefix<<suffix_length); //part

        }else if(prefix==14){

            if(suffix_length)

                level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part

            else

                level_code= prefix + get_bits(gb, 4); //part

        }else if(prefix==15){

            level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part

            if(suffix_length==0) level_code+=15; //FIXME doesnt make (much)sense

        }else{

            av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);

            return -1;

        }



        if(i==trailing_ones && i<3) level_code+= 2; //FIXME split first iteration



        mask= -(level_code&1);

        level[i]= (((2+level_code)>>1) ^ mask) - mask;



        if(suffix_length==0) suffix_length=1; //FIXME split first iteration



#if 1

        if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;

#else        

        if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;

        ? == prefix > 2 or sth

#endif

        tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);

    }



    if(total_coeff == max_coeff)

        zeros_left=0;

    else{

        if(n == CHROMA_DC_BLOCK_INDEX)

            zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);

        else

            zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);

    }

    

    for(i=0; i<total_coeff-1; i++){

        if(zeros_left <=0)

            break;

        else if(zeros_left < 7){

            run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);

        }else{

            run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);

        }

        zeros_left -= run[i];

    }



    if(zeros_left<0){

        av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);

        return -1;

    }

    

    for(; i<total_coeff-1; i++){

        run[i]= 0;

    }



    run[i]= zeros_left;



    coeff_num=-1;

    if(n > 24){

        for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?

            int j;



            coeff_num += run[i] + 1; //FIXME add 1 earlier ?

            j= scantable[ coeff_num ];



            block[j]= level[i];

        }

    }else{

        for(i=total_coeff-1; i>=0; i--){ //FIXME merge into  rundecode?

            int j;



            coeff_num += run[i] + 1; //FIXME add 1 earlier ?

            j= scantable[ coeff_num ];



            block[j]= level[i] * qmul[j];

//            printf("%d %d  ", block[j], qmul[j]);

        }

    }

    return 0;

}

static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){

    MpegEncContext * const s = &h->s;

    const uint16_t *qmul= dequant_coeff[qp];

    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};

    int level[16], run[16];

    int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;



    



    if(n == CHROMA_DC_BLOCK_INDEX){

        coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);

        total_coeff= coeff_token>>2;

    }else{    

        if(n == LUMA_DC_BLOCK_INDEX){

            total_coeff= pred_non_zero_count(h, 0);

            coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);

            total_coeff= coeff_token>>2;

        }else{

            total_coeff= pred_non_zero_count(h, n);

            coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);

            total_coeff= coeff_token>>2;

            h->non_zero_count_cache[ scan8[n] ]= total_coeff;

        }

    }



    



    if(total_coeff==0)

        return 0;

        

    trailing_ones= coeff_token&3;

    tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);

    assert(total_coeff<=16);

    

    for(i=0; i<trailing_ones; i++){

        level[i]= 1 - 2*get_bits1(gb);

    }



    suffix_length= total_coeff > 10 && trailing_ones < 3;



    for(; i<total_coeff; i++){

        const int prefix= get_level_prefix(gb);

        int level_code, mask;



        if(prefix<14){ 

            if(suffix_length)

                level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); 

            else

                level_code= (prefix<<suffix_length); 

        }else if(prefix==14){

            if(suffix_length)

                level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); 

            else

                level_code= prefix + get_bits(gb, 4); 

        }else if(prefix==15){

            level_code= (prefix<<suffix_length) + get_bits(gb, 12); 

            if(suffix_length==0) level_code+=15; 

        }else{

            av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);

            return -1;

        }



        if(i==trailing_ones && i<3) level_code+= 2; 



        mask= -(level_code&1);

        level[i]= (((2+level_code)>>1) ^ mask) - mask;



        if(suffix_length==0) suffix_length=1; 



#if 1

        if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;

#else        

        if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;

        ? == prefix > 2 or sth

#endif

        tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);

    }



    if(total_coeff == max_coeff)

        zeros_left=0;

    else{

        if(n == CHROMA_DC_BLOCK_INDEX)

            zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);

        else

            zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);

    }

    

    for(i=0; i<total_coeff-1; i++){

        if(zeros_left <=0)

            break;

        else if(zeros_left < 7){

            run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);

        }else{

            run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);

        }

        zeros_left -= run[i];

    }



    if(zeros_left<0){

        av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);

        return -1;

    }

    

    for(; i<total_coeff-1; i++){

        run[i]= 0;

    }



    run[i]= zeros_left;



    coeff_num=-1;

    if(n > 24){

        for(i=total_coeff-1; i>=0; i--){ 

            int j;



            coeff_num += run[i] + 1; 

            j= scantable[ coeff_num ];



            block[j]= level[i];

        }

    }else{

        for(i=total_coeff-1; i>=0; i--){ 

            int j;



            coeff_num += run[i] + 1; 

            j= scantable[ coeff_num ];



            block[j]= level[i] * qmul[j];



        }

    }

    return 0;

}

static int FUNC_0(H264Context *VAR_0, GetBitContext *VAR_1, DCTELEM *VAR_2, int VAR_3, const uint8_t *VAR_4, int VAR_5, int VAR_6){

    MpegEncContext * const s = &VAR_0->s;

    const uint16_t *VAR_7= dequant_coeff[VAR_5];

    static const int VAR_8[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};

    int VAR_9[16], VAR_10[16];

    int VAR_11, VAR_12, VAR_13, VAR_14, VAR_15, VAR_16, VAR_17;



    



    if(VAR_3 == CHROMA_DC_BLOCK_INDEX){

        VAR_14= get_vlc2(VAR_1, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);

        VAR_15= VAR_14>>2;

    }else{    

        if(VAR_3 == LUMA_DC_BLOCK_INDEX){

            VAR_15= pred_non_zero_count(VAR_0, 0);

            VAR_14= get_vlc2(VAR_1, coeff_token_vlc[ VAR_8[VAR_15] ].table, COEFF_TOKEN_VLC_BITS, 2);

            VAR_15= VAR_14>>2;

        }else{

            VAR_15= pred_non_zero_count(VAR_0, VAR_3);

            VAR_14= get_vlc2(VAR_1, coeff_token_vlc[ VAR_8[VAR_15] ].table, COEFF_TOKEN_VLC_BITS, 2);

            VAR_15= VAR_14>>2;

            VAR_0->non_zero_count_cache[ scan8[VAR_3] ]= VAR_15;

        }

    }



    



    if(VAR_15==0)

        return 0;

        

    VAR_17= VAR_14&3;

    tprintf("trailing:%d, total:%d\VAR_3", VAR_17, VAR_15);

    assert(VAR_15<=16);

    

    for(VAR_16=0; VAR_16<VAR_17; VAR_16++){

        VAR_9[VAR_16]= 1 - 2*get_bits1(VAR_1);

    }



    VAR_11= VAR_15 > 10 && VAR_17 < 3;



    for(; VAR_16<VAR_15; VAR_16++){

        const int VAR_18= get_level_prefix(VAR_1);

        int VAR_19, VAR_20;



        if(VAR_18<14){ 

            if(VAR_11)

                VAR_19= (VAR_18<<VAR_11) + get_bits(VAR_1, VAR_11); 

            else

                VAR_19= (VAR_18<<VAR_11); 

        }else if(VAR_18==14){

            if(VAR_11)

                VAR_19= (VAR_18<<VAR_11) + get_bits(VAR_1, VAR_11); 

            else

                VAR_19= VAR_18 + get_bits(VAR_1, 4); 

        }else if(VAR_18==15){

            VAR_19= (VAR_18<<VAR_11) + get_bits(VAR_1, 12); 

            if(VAR_11==0) VAR_19+=15; 

        }else{

            av_log(VAR_0->s.avctx, AV_LOG_ERROR, "VAR_18 too large at %d %d\VAR_3", s->mb_x, s->mb_y);

            return -1;

        }



        if(VAR_16==VAR_17 && VAR_16<3) VAR_19+= 2; 



        VAR_20= -(VAR_19&1);

        VAR_9[VAR_16]= (((2+VAR_19)>>1) ^ VAR_20) - VAR_20;



        if(VAR_11==0) VAR_11=1; 



#if 1

        if(ABS(VAR_9[VAR_16]) > (3<<(VAR_11-1)) && VAR_11<6) VAR_11++;

#else        

        if((2+VAR_19)>>1) > (3<<(VAR_11-1)) && VAR_11<6) VAR_11++;

        ? == VAR_18 > 2 or sth

#endif

        tprintf("VAR_9: %d VAR_11:%d\VAR_3", VAR_9[VAR_16], VAR_11);

    }



    if(VAR_15 == VAR_6)

        VAR_12=0;

    else{

        if(VAR_3 == CHROMA_DC_BLOCK_INDEX)

            VAR_12= get_vlc2(VAR_1, chroma_dc_total_zeros_vlc[ VAR_15-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);

        else

            VAR_12= get_vlc2(VAR_1, total_zeros_vlc[ VAR_15-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);

    }

    

    for(VAR_16=0; VAR_16<VAR_15-1; VAR_16++){

        if(VAR_12 <=0)

            break;

        else if(VAR_12 < 7){

            VAR_10[VAR_16]= get_vlc2(VAR_1, run_vlc[VAR_12-1].table, RUN_VLC_BITS, 1);

        }else{

            VAR_10[VAR_16]= get_vlc2(VAR_1, run7_vlc.table, RUN7_VLC_BITS, 2);

        }

        VAR_12 -= VAR_10[VAR_16];

    }



    if(VAR_12<0){

        av_log(VAR_0->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\VAR_3", s->mb_x, s->mb_y);

        return -1;

    }

    

    for(; VAR_16<VAR_15-1; VAR_16++){

        VAR_10[VAR_16]= 0;

    }



    VAR_10[VAR_16]= VAR_12;



    VAR_13=-1;

    if(VAR_3 > 24){

        for(VAR_16=VAR_15-1; VAR_16>=0; VAR_16--){ 

            int VAR_22;



            VAR_13 += VAR_10[VAR_16] + 1; 

            VAR_22= VAR_4[ VAR_13 ];



            VAR_2[VAR_22]= VAR_9[VAR_16];

        }

    }else{

        for(VAR_16=VAR_15-1; VAR_16>=0; VAR_16--){ 

            int VAR_22;



            VAR_13 += VAR_10[VAR_16] + 1; 

            VAR_22= VAR_4[ VAR_13 ];



            VAR_2[VAR_22]= VAR_9[VAR_16] * VAR_7[VAR_22];



        }

    }

    return 0;

}

static int FUNC_0(H264Context *VAR_0, GetBitContext *VAR_1, DCTELEM *VAR_2, int VAR_3, const uint8_t *VAR_4, int VAR_5, int VAR_6){ MpegEncContext * const s = &VAR_0->s; const uint16_t *VAR_7= dequant_coeff[VAR_5]; static const int VAR_8[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3}; int VAR_9[16], VAR_10[16]; int VAR_11, VAR_12, VAR_13, VAR_14, VAR_15, VAR_16, VAR_17; if(VAR_3 == CHROMA_DC_BLOCK_INDEX){ VAR_14= get_vlc2(VAR_1, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1); VAR_15= VAR_14>>2; }else{ if(VAR_3 == LUMA_DC_BLOCK_INDEX){ VAR_15= pred_non_zero_count(VAR_0, 0); VAR_14= get_vlc2(VAR_1, coeff_token_vlc[ VAR_8[VAR_15] ].table, COEFF_TOKEN_VLC_BITS, 2); VAR_15= VAR_14>>2; }else{ VAR_15= pred_non_zero_count(VAR_0, VAR_3); VAR_14= get_vlc2(VAR_1, coeff_token_vlc[ VAR_8[VAR_15] ].table, COEFF_TOKEN_VLC_BITS, 2); VAR_15= VAR_14>>2; VAR_0->non_zero_count_cache[ scan8[VAR_3] ]= VAR_15; } } if(VAR_15==0)
return 0; VAR_17= VAR_14&3; tprintf("trailing:%d, total:%d\VAR_3", VAR_17, VAR_15); assert(VAR_15<=16); for(VAR_16=0; VAR_16<VAR_17; VAR_16++){ VAR_9[VAR_16]= 1 - 2*get_bits1(VAR_1); } VAR_11= VAR_15 > 10 && VAR_17 < 3; for(; VAR_16<VAR_15; VAR_16++){ const int VAR_18= get_level_prefix(VAR_1); int VAR_19, VAR_20; if(VAR_18<14){ if(VAR_11)
VAR_19= (VAR_18<<VAR_11) + get_bits(VAR_1, VAR_11); else
VAR_19= (VAR_18<<VAR_11); }else if(VAR_18==14){ if(VAR_11)
VAR_19= (VAR_18<<VAR_11) + get_bits(VAR_1, VAR_11); else
VAR_19= VAR_18 + get_bits(VAR_1, 4); }else if(VAR_18==15){ VAR_19= (VAR_18<<VAR_11) + get_bits(VAR_1, 12); if(VAR_11==0) VAR_19+=15; }else{ av_log(VAR_0->s.avctx, AV_LOG_ERROR, "VAR_18 too large at %d %d\VAR_3", s->mb_x, s->mb_y); return -1; } if(VAR_16==VAR_17 && VAR_16<3) VAR_19+= 2; VAR_20= -(VAR_19&1); VAR_9[VAR_16]= (((2+VAR_19)>>1) ^ VAR_20) - VAR_20; if(VAR_11==0) VAR_11=1; #if 1
if(ABS(VAR_9[VAR_16]) > (3<<(VAR_11-1)) && VAR_11<6) VAR_11++; #else
if((2+VAR_19)>>1) > (3<<(VAR_11-1)) && VAR_11<6) VAR_11++; ? == VAR_18 > 2 or sth
#endif
tprintf("VAR_9: %d VAR_11:%d\VAR_3", VAR_9[VAR_16], VAR_11); } if(VAR_15 == VAR_6)
VAR_12=0; else{ if(VAR_3 == CHROMA_DC_BLOCK_INDEX)
VAR_12= get_vlc2(VAR_1, chroma_dc_total_zeros_vlc[ VAR_15-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1); else
VAR_12= get_vlc2(VAR_1, total_zeros_vlc[ VAR_15-1 ].table, TOTAL_ZEROS_VLC_BITS, 1); } for(VAR_16=0; VAR_16<VAR_15-1; VAR_16++){ if(VAR_12 <=0)
break; else if(VAR_12 < 7){ VAR_10[VAR_16]= get_vlc2(VAR_1, run_vlc[VAR_12-1].table, RUN_VLC_BITS, 1); }else{ VAR_10[VAR_16]= get_vlc2(VAR_1, run7_vlc.table, RUN7_VLC_BITS, 2); } VAR_12 -= VAR_10[VAR_16]; } if(VAR_12<0){ av_log(VAR_0->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\VAR_3", s->mb_x, s->mb_y); return -1; } for(; VAR_16<VAR_15-1; VAR_16++){ VAR_10[VAR_16]= 0; } VAR_10[VAR_16]= VAR_12; VAR_13=-1; if(VAR_3 > 24){ for(VAR_16=VAR_15-1; VAR_16>=0; VAR_16--){ int VAR_22; VAR_13 += VAR_10[VAR_16] + 1; VAR_22= VAR_4[ VAR_13 ]; VAR_2[VAR_22]= VAR_9[VAR_16]; } }else{ for(VAR_16=VAR_15-1; VAR_16>=0; VAR_16--){ int VAR_22; VAR_13 += VAR_10[VAR_16] + 1; VAR_22= VAR_4[ VAR_13 ]; VAR_2[VAR_22]= VAR_9[VAR_16] * VAR_7[VAR_22]; } } return 0; }