static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
    MpegEncContext * const s = &h->s;
    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];
    int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
    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;
    if(total_coeff > (unsigned)max_coeff) {
        av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
        return -1;
    }
    trailing_ones= coeff_token&3;
    tprintf(h->s.avctx, "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);
    }
    if(i<total_coeff) {
        int level_code, mask;
        int suffix_length = total_coeff > 10 && trailing_ones < 3;
        int prefix= get_level_prefix(gb);
        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(trailing_ones < 3) level_code += 2;
        suffix_length = 1;
        if(level_code > 5)
            suffix_length++;
        mask= -(level_code&1);
        level[i]= (((2+level_code)>>1) ^ mask) - mask;
        i++;
        for(;i<total_coeff;i++) {
            static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
            prefix = get_level_prefix(gb);
            if(prefix<15){
                level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
            }else if(prefix==15){
                level_code =  (prefix<<suffix_length) + get_bits(gb, 12);
            }else{
                av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
                return -1;
            }
            mask= -(level_code&1);
            level[i]= (((2+level_code)>>1) ^ mask) - mask;
            if(level_code > suffix_limit[suffix_length])
                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);
    }
    coeff_num = zeros_left + total_coeff - 1;
    j = scantable[coeff_num];
    if(n > 24){
        block[j] = level[0];
        for(i=1;i<total_coeff;i++) {
            if(zeros_left <= 0)
                run_before = 0;
            else if(zeros_left < 7){
                run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
            }else{
                run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
            }
            zeros_left -= run_before;
            coeff_num -= 1 + run_before;
            j= scantable[ coeff_num ];
            block[j]= level[i];
        }
    }else{
        block[j] = (level[0] * qmul[j] + 32)>>6;
        for(i=1;i<total_coeff;i++) {
            if(zeros_left <= 0)
                run_before = 0;
            else if(zeros_left < 7){
                run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
            }else{
                run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
            }
            zeros_left -= run_before;
            coeff_num -= 1 + run_before;
            j= scantable[ coeff_num ];
            block[j]= (level[i] * qmul[j] + 32)>>6;
        }
    }
    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;
    }
    return 0;
}
['static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){\n    MpegEncContext * const s = &h->s;\n    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};\n    int level[16];\n    int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;\n    if(n == CHROMA_DC_BLOCK_INDEX){\n        coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);\n        total_coeff= coeff_token>>2;\n    }else{\n        if(n == LUMA_DC_BLOCK_INDEX){\n            total_coeff= pred_non_zero_count(h, 0);\n            coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);\n            total_coeff= coeff_token>>2;\n        }else{\n            total_coeff= pred_non_zero_count(h, n);\n            coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);\n            total_coeff= coeff_token>>2;\n            h->non_zero_count_cache[ scan8[n] ]= total_coeff;\n        }\n    }\n    if(total_coeff==0)\n        return 0;\n    if(total_coeff > (unsigned)max_coeff) {\n        av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\\n", s->mb_x, s->mb_y, total_coeff);\n        return -1;\n    }\n    trailing_ones= coeff_token&3;\n    tprintf(h->s.avctx, "trailing:%d, total:%d\\n", trailing_ones, total_coeff);\n    assert(total_coeff<=16);\n    for(i=0; i<trailing_ones; i++){\n        level[i]= 1 - 2*get_bits1(gb);\n    }\n    if(i<total_coeff) {\n        int level_code, mask;\n        int suffix_length = total_coeff > 10 && trailing_ones < 3;\n        int prefix= get_level_prefix(gb);\n        if(prefix<14){\n            if(suffix_length)\n                level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length);\n            else\n                level_code= (prefix<<suffix_length);\n        }else if(prefix==14){\n            if(suffix_length)\n                level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length);\n            else\n                level_code= prefix + get_bits(gb, 4);\n        }else if(prefix==15){\n            level_code= (prefix<<suffix_length) + get_bits(gb, 12);\n            if(suffix_length==0) level_code+=15;\n        }else{\n            av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\\n", s->mb_x, s->mb_y);\n            return -1;\n        }\n        if(trailing_ones < 3) level_code += 2;\n        suffix_length = 1;\n        if(level_code > 5)\n            suffix_length++;\n        mask= -(level_code&1);\n        level[i]= (((2+level_code)>>1) ^ mask) - mask;\n        i++;\n        for(;i<total_coeff;i++) {\n            static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };\n            prefix = get_level_prefix(gb);\n            if(prefix<15){\n                level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);\n            }else if(prefix==15){\n                level_code =  (prefix<<suffix_length) + get_bits(gb, 12);\n            }else{\n                av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\\n", s->mb_x, s->mb_y);\n                return -1;\n            }\n            mask= -(level_code&1);\n            level[i]= (((2+level_code)>>1) ^ mask) - mask;\n            if(level_code > suffix_limit[suffix_length])\n                suffix_length++;\n        }\n    }\n    if(total_coeff == max_coeff)\n        zeros_left=0;\n    else{\n        if(n == CHROMA_DC_BLOCK_INDEX)\n            zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);\n        else\n            zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);\n    }\n    coeff_num = zeros_left + total_coeff - 1;\n    j = scantable[coeff_num];\n    if(n > 24){\n        block[j] = level[0];\n        for(i=1;i<total_coeff;i++) {\n            if(zeros_left <= 0)\n                run_before = 0;\n            else if(zeros_left < 7){\n                run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);\n            }else{\n                run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);\n            }\n            zeros_left -= run_before;\n            coeff_num -= 1 + run_before;\n            j= scantable[ coeff_num ];\n            block[j]= level[i];\n        }\n    }else{\n        block[j] = (level[0] * qmul[j] + 32)>>6;\n        for(i=1;i<total_coeff;i++) {\n            if(zeros_left <= 0)\n                run_before = 0;\n            else if(zeros_left < 7){\n                run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);\n            }else{\n                run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);\n            }\n            zeros_left -= run_before;\n            coeff_num -= 1 + run_before;\n            j= scantable[ coeff_num ];\n            block[j]= (level[i] * qmul[j] + 32)>>6;\n        }\n    }\n    if(zeros_left<0){\n        av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\\n", s->mb_x, s->mb_y);\n        return -1;\n    }\n    return 0;\n}']