int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
                              int n, int coded, const uint8_t *scan_table)
{
    int level, i, last, run, run_diff;
    int dc_pred_dir;
    RLTable *rl;
    RL_VLC_ELEM *rl_vlc;
    int qmul, qadd;
    if (s->mb_intra) {
        qmul=1;
        qadd=0;
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
        if (level < 0){
            av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
            if(s->inter_intra_pred) level=0;
            else                    return -1;
        }
        if (n < 4) {
            rl = &rl_table[s->rl_table_index];
            if(level > 256*s->y_dc_scale){
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
                if(!s->inter_intra_pred) return -1;
            }
        } else {
            rl = &rl_table[3 + s->rl_chroma_table_index];
            if(level > 256*s->c_dc_scale){
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
                if(!s->inter_intra_pred) return -1;
            }
        }
        block[0] = level;
        run_diff = s->msmpeg4_version >= 4;
        i = 0;
        if (!coded) {
            goto not_coded;
        }
        if (s->ac_pred) {
            if (dc_pred_dir == 0)
                scan_table = s->intra_v_scantable.permutated;
            else
                scan_table = s->intra_h_scantable.permutated;
        } else {
            scan_table = s->intra_scantable.permutated;
        }
        rl_vlc= rl->rl_vlc[0];
    } else {
        qmul = s->qscale << 1;
        qadd = (s->qscale - 1) | 1;
        i = -1;
        rl = &rl_table[3 + s->rl_table_index];
        if(s->msmpeg4_version==2)
            run_diff = 0;
        else
            run_diff = 1;
        if (!coded) {
            s->block_last_index[n] = i;
            return 0;
        }
        if(!scan_table)
            scan_table = s->inter_scantable.permutated;
        rl_vlc= rl->rl_vlc[s->qscale];
    }
  {
    OPEN_READER(re, &s->gb);
    for(;;) {
        UPDATE_CACHE(re, &s->gb);
        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
        if (level==0) {
            int cache;
            cache= GET_CACHE(re, &s->gb);
            if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
                if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
                    if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
                    UPDATE_CACHE(re, &s->gb);
                    if(s->msmpeg4_version<=3){
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
                        run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
                        level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
                        SKIP_COUNTER(re, &s->gb, 1+6+8);
                    }else{
                        int sign;
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
                        if(!s->esc3_level_length){
                            int ll;
                            if(s->qscale<8){
                                ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
                                if(ll==0){
                                    if(SHOW_UBITS(re, &s->gb, 1)) av_log(s->avctx, AV_LOG_ERROR, "cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
                                    SKIP_BITS(re, &s->gb, 1);
                                    ll=8;
                                }
                            }else{
                                ll=2;
                                while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
                                    ll++;
                                    SKIP_BITS(re, &s->gb, 1);
                                }
                                if(ll<8) SKIP_BITS(re, &s->gb, 1);
                            }
                            s->esc3_level_length= ll;
                            s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
                            UPDATE_CACHE(re, &s->gb);
                        }
                        run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length);
                        SKIP_BITS(re, &s->gb, s->esc3_run_length);
                        sign=  SHOW_UBITS(re, &s->gb, 1);
                        SKIP_BITS(re, &s->gb, 1);
                        level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
                        SKIP_BITS(re, &s->gb, s->esc3_level_length);
                        if(sign) level= -level;
                    }
#if 0
                    {
                        const int abs_level= FFABS(level);
                        const int run1= run - rl->max_run[last][abs_level] - run_diff;
                        if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
                            if(abs_level <= rl->max_level[last][run]){
                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
                                return DECODING_AC_LOST;
                            }
                            if(abs_level <= rl->max_level[last][run]*2){
                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
                                return DECODING_AC_LOST;
                            }
                            if(run1>=0 && abs_level <= rl->max_level[last][run1]){
                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
                                return DECODING_AC_LOST;
                            }
                        }
                    }
#endif
                    if (level>0) level= level * qmul + qadd;
                    else         level= level * qmul - qadd;
#if 0
                    if(level>2048 || level<-2048){
                        av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
                        return DECODING_AC_LOST;
                    }
#endif
                    i+= run + 1;
                    if(last) i+=192;
#ifdef ERROR_DETAILS
                if(run==66)
                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
                else if((i>62 && i<192) || i>192+63)
                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
#endif
                } else {
#if MIN_CACHE_BITS < 23
                    LAST_SKIP_BITS(re, &s->gb, 2);
                    UPDATE_CACHE(re, &s->gb);
#else
                    SKIP_BITS(re, &s->gb, 2);
#endif
                    GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
                    i+= run + rl->max_run[run>>7][level/qmul] + run_diff;
                    level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                    LAST_SKIP_BITS(re, &s->gb, 1);
#ifdef ERROR_DETAILS
                if(run==66)
                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
                else if((i>62 && i<192) || i>192+63)
                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
#endif
                }
            } else {
#if MIN_CACHE_BITS < 22
                LAST_SKIP_BITS(re, &s->gb, 1);
                UPDATE_CACHE(re, &s->gb);
#else
                SKIP_BITS(re, &s->gb, 1);
#endif
                GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
                i+= run;
                level = level + rl->max_level[run>>7][(run-1)&63] * qmul;
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
                LAST_SKIP_BITS(re, &s->gb, 1);
#ifdef ERROR_DETAILS
                if(run==66)
                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
                else if((i>62 && i<192) || i>192+63)
                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
#endif
            }
        } else {
            i+= run;
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
            LAST_SKIP_BITS(re, &s->gb, 1);
#ifdef ERROR_DETAILS
                if(run==66)
                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
                else if((i>62 && i<192) || i>192+63)
                    av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
#endif
        }
        if (i > 62){
            i-= 192;
            if(i&(~63)){
                const int left= s->gb.size_in_bits - get_bits_count(&s->gb);
                if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){
                    av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
                    break;
                }else{
                    av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
                    return -1;
                }
            }
            block[scan_table[i]] = level;
            break;
        }
        block[scan_table[i]] = level;
    }
    CLOSE_READER(re, &s->gb);
  }
 not_coded:
    if (s->mb_intra) {
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
        if (s->ac_pred) {
            i = 63;
        }
    }
    if(s->msmpeg4_version>=4 && i>0) i=63;
    s->block_last_index[n] = i;
    return 0;
}
['int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,\n                              int n, int coded, const uint8_t *scan_table)\n{\n    int level, i, last, run, run_diff;\n    int dc_pred_dir;\n    RLTable *rl;\n    RL_VLC_ELEM *rl_vlc;\n    int qmul, qadd;\n    if (s->mb_intra) {\n        qmul=1;\n        qadd=0;\n        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);\n        if (level < 0){\n            av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\\n", n, s->qscale);\n            if(s->inter_intra_pred) level=0;\n            else                    return -1;\n        }\n        if (n < 4) {\n            rl = &rl_table[s->rl_table_index];\n            if(level > 256*s->y_dc_scale){\n                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\\n", s->qscale);\n                if(!s->inter_intra_pred) return -1;\n            }\n        } else {\n            rl = &rl_table[3 + s->rl_chroma_table_index];\n            if(level > 256*s->c_dc_scale){\n                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\\n", s->qscale);\n                if(!s->inter_intra_pred) return -1;\n            }\n        }\n        block[0] = level;\n        run_diff = s->msmpeg4_version >= 4;\n        i = 0;\n        if (!coded) {\n            goto not_coded;\n        }\n        if (s->ac_pred) {\n            if (dc_pred_dir == 0)\n                scan_table = s->intra_v_scantable.permutated;\n            else\n                scan_table = s->intra_h_scantable.permutated;\n        } else {\n            scan_table = s->intra_scantable.permutated;\n        }\n        rl_vlc= rl->rl_vlc[0];\n    } else {\n        qmul = s->qscale << 1;\n        qadd = (s->qscale - 1) | 1;\n        i = -1;\n        rl = &rl_table[3 + s->rl_table_index];\n        if(s->msmpeg4_version==2)\n            run_diff = 0;\n        else\n            run_diff = 1;\n        if (!coded) {\n            s->block_last_index[n] = i;\n            return 0;\n        }\n        if(!scan_table)\n            scan_table = s->inter_scantable.permutated;\n        rl_vlc= rl->rl_vlc[s->qscale];\n    }\n  {\n    OPEN_READER(re, &s->gb);\n    for(;;) {\n        UPDATE_CACHE(re, &s->gb);\n        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);\n        if (level==0) {\n            int cache;\n            cache= GET_CACHE(re, &s->gb);\n            if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {\n                if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {\n                    if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);\n                    UPDATE_CACHE(re, &s->gb);\n                    if(s->msmpeg4_version<=3){\n                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);\n                        run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);\n                        level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);\n                        SKIP_COUNTER(re, &s->gb, 1+6+8);\n                    }else{\n                        int sign;\n                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);\n                        if(!s->esc3_level_length){\n                            int ll;\n                            if(s->qscale<8){\n                                ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);\n                                if(ll==0){\n                                    if(SHOW_UBITS(re, &s->gb, 1)) av_log(s->avctx, AV_LOG_ERROR, "cool a new vlc code ,contact the ffmpeg developers and upload the file\\n");\n                                    SKIP_BITS(re, &s->gb, 1);\n                                    ll=8;\n                                }\n                            }else{\n                                ll=2;\n                                while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){\n                                    ll++;\n                                    SKIP_BITS(re, &s->gb, 1);\n                                }\n                                if(ll<8) SKIP_BITS(re, &s->gb, 1);\n                            }\n                            s->esc3_level_length= ll;\n                            s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);\n                            UPDATE_CACHE(re, &s->gb);\n                        }\n                        run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length);\n                        SKIP_BITS(re, &s->gb, s->esc3_run_length);\n                        sign=  SHOW_UBITS(re, &s->gb, 1);\n                        SKIP_BITS(re, &s->gb, 1);\n                        level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);\n                        SKIP_BITS(re, &s->gb, s->esc3_level_length);\n                        if(sign) level= -level;\n                    }\n#if 0\n                    {\n                        const int abs_level= FFABS(level);\n                        const int run1= run - rl->max_run[last][abs_level] - run_diff;\n                        if(abs_level<=MAX_LEVEL && run<=MAX_RUN){\n                            if(abs_level <= rl->max_level[last][run]){\n                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\\n");\n                                return DECODING_AC_LOST;\n                            }\n                            if(abs_level <= rl->max_level[last][run]*2){\n                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\\n");\n                                return DECODING_AC_LOST;\n                            }\n                            if(run1>=0 && abs_level <= rl->max_level[last][run1]){\n                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\\n");\n                                return DECODING_AC_LOST;\n                            }\n                        }\n                    }\n#endif\n                    if (level>0) level= level * qmul + qadd;\n                    else         level= level * qmul - qadd;\n#if 0\n                    if(level>2048 || level<-2048){\n                        av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\\n");\n                        return DECODING_AC_LOST;\n                    }\n#endif\n                    i+= run + 1;\n                    if(last) i+=192;\n#ifdef ERROR_DETAILS\n                if(run==66)\n                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\\n", level);\n                else if((i>62 && i<192) || i>192+63)\n                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\\n", i, run, level);\n#endif\n                } else {\n#if MIN_CACHE_BITS < 23\n                    LAST_SKIP_BITS(re, &s->gb, 2);\n                    UPDATE_CACHE(re, &s->gb);\n#else\n                    SKIP_BITS(re, &s->gb, 2);\n#endif\n                    GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);\n                    i+= run + rl->max_run[run>>7][level/qmul] + run_diff;\n                    level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);\n                    LAST_SKIP_BITS(re, &s->gb, 1);\n#ifdef ERROR_DETAILS\n                if(run==66)\n                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\\n", level);\n                else if((i>62 && i<192) || i>192+63)\n                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\\n", i, run, level);\n#endif\n                }\n            } else {\n#if MIN_CACHE_BITS < 22\n                LAST_SKIP_BITS(re, &s->gb, 1);\n                UPDATE_CACHE(re, &s->gb);\n#else\n                SKIP_BITS(re, &s->gb, 1);\n#endif\n                GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);\n                i+= run;\n                level = level + rl->max_level[run>>7][(run-1)&63] * qmul;\n                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);\n                LAST_SKIP_BITS(re, &s->gb, 1);\n#ifdef ERROR_DETAILS\n                if(run==66)\n                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\\n", level);\n                else if((i>62 && i<192) || i>192+63)\n                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\\n", i, run, level);\n#endif\n            }\n        } else {\n            i+= run;\n            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);\n            LAST_SKIP_BITS(re, &s->gb, 1);\n#ifdef ERROR_DETAILS\n                if(run==66)\n                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\\n", level);\n                else if((i>62 && i<192) || i>192+63)\n                    av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\\n", i, run, level);\n#endif\n        }\n        if (i > 62){\n            i-= 192;\n            if(i&(~63)){\n                const int left= s->gb.size_in_bits - get_bits_count(&s->gb);\n                if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){\n                    av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\\n", s->mb_x, s->mb_y);\n                    break;\n                }else{\n                    av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\\n", s->mb_x, s->mb_y);\n                    return -1;\n                }\n            }\n            block[scan_table[i]] = level;\n            break;\n        }\n        block[scan_table[i]] = level;\n    }\n    CLOSE_READER(re, &s->gb);\n  }\n not_coded:\n    if (s->mb_intra) {\n        mpeg4_pred_ac(s, block, n, dc_pred_dir);\n        if (s->ac_pred) {\n            i = 63;\n        }\n    }\n    if(s->msmpeg4_version>=4 && i>0) i=63;\n    s->block_last_index[n] = i;\n    return 0;\n}']