libavcodec/mpegvideo_enc.c:2086: error: Buffer Overrun L2
  Offset: [0, 70] (⇐ [0, 7] + [0, 63]) Size: 8 by call to `encode_mb_internal`.

libavcodec/mpegvideo_enc.c:2086:41: Call
2084. {
2085.     if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y,  8, 6);
2086.     else                                encode_mb_internal(s, motion_x, motion_y, 16, 8);
                                              ^
2087. }
2088. 

libavcodec/mpegvideo_enc.c:1721:1: Array declaration
  1719. }
  1720. 
  1721. static av_always_inline void encode_mb_internal(MpegEncContext *s,
        ^
  1722.                                                 int motion_x, int motion_y,
  1723.                                                 int mb_block_height,

libavcodec/mpegvideo_enc.c:1999:25: Call
  1997.                 if (!skip_dct[i]) {
  1998.                     s->block_last_index[i] =
  1999.                         dct_quantize_refine(s, s->block[i], weight[i],
                                ^
  2000.                                             orig[i], i, s->qscale);
  2001.                 }

libavcodec/mpegvideo_enc.c:3683:9: <Offset trace>
3681. #endif
3682.     dc += (1<<(RECON_SHIFT-1));
3683.     for(i=0; i<64; i++){
              ^
3684.         rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME  use orig dirrectly instead of copying to rem[]
3685.     }

libavcodec/mpegvideo_enc.c:3683:9: Assignment
    3681. #endif
    3682.     dc += (1<<(RECON_SHIFT-1));
    3683.     for(i=0; i<64; i++){
                  ^
    3684.         rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME  use orig dirrectly instead of copying to rem[]
    3685.     }

libavcodec/mpegvideo_enc.c:3619:1: <Length trace>
3617. }
3618. 
3619. static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
      ^
3620.                         DCTELEM *block, int16_t *weight, DCTELEM *orig,
3621.                         int n, int qscale){

libavcodec/mpegvideo_enc.c:3619:1: Parameter `*orig`
    3617. }
    3618. 
    3619. static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
          ^
    3620.                         DCTELEM *block, int16_t *weight, DCTELEM *orig,
    3621.                         int n, int qscale){

libavcodec/mpegvideo_enc.c:3684:23: Array access: Offset: [0, 70] (⇐ [0, 7] + [0, 63]) Size: 8 by call to `encode_mb_internal` 
    3682.     dc += (1<<(RECON_SHIFT-1));
    3683.     for(i=0; i<64; i++){
    3684.         rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME  use orig dirrectly instead of copying to rem[]
                                ^
    3685.     }
    3686. #ifdef REFINE_STATS

static int dct_quantize_refine(MpegEncContext *s,
                        DCTELEM *block, int16_t *weight, DCTELEM *orig,
                        int n, int qscale){
    int16_t rem[64];
    LOCAL_ALIGNED_16(DCTELEM, d1, [64]);
    const uint8_t *scantable= s->intra_scantable.scantable;
    const uint8_t *perm_scantable= s->intra_scantable.permutated;
    int run_tab[65];
    int prev_run=0;
    int prev_level=0;
    int qmul, qadd, start_i, last_non_zero, i, dc;
    uint8_t * length;
    uint8_t * last_length;
    int lambda;
    int rle_index, run, q = 1, sum;
#ifdef REFINE_STATS
static int count=0;
static int after_last=0;
static int to_zero=0;
static int from_zero=0;
static int raise=0;
static int lower=0;
static int messed_sign=0;
#endif
    if(basis[0][0] == 0)
        build_basis(s->dsp.idct_permutation);
    qmul= qscale*2;
    qadd= (qscale-1)|1;
    if (s->mb_intra) {
        if (!s->h263_aic) {
            if (n < 4)
                q = s->y_dc_scale;
            else
                q = s->c_dc_scale;
        } else{
            q = 1;
            qadd=0;
        }
        q <<= RECON_SHIFT-3;
        dc= block[0]*q;
        start_i = 1;
        length     = s->intra_ac_vlc_length;
        last_length= s->intra_ac_vlc_last_length;
    } else {
        dc= 0;
        start_i = 0;
        length     = s->inter_ac_vlc_length;
        last_length= s->inter_ac_vlc_last_length;
    }
    last_non_zero = s->block_last_index[n];
#ifdef REFINE_STATS
{START_TIMER
#endif
    dc += (1<<(RECON_SHIFT-1));
    for(i=0; i<64; i++){
        rem[i]= dc - (orig[i]<<RECON_SHIFT);
    }
#ifdef REFINE_STATS
STOP_TIMER("memset rem[]")}
#endif
    sum=0;
    for(i=0; i<64; i++){
        int one= 36;
        int qns=4;
        int w;
        w= FFABS(weight[i]) + qns*one;
        w= 15 + (48*qns*one + w/2)/w;
        weight[i] = w;
        assert(w>0);
        assert(w<(1<<6));
        sum += w*w;
    }
    lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
#ifdef REFINE_STATS
{START_TIMER
#endif
    run=0;
    rle_index=0;
    for(i=start_i; i<=last_non_zero; i++){
        int j= perm_scantable[i];
        const int level= block[j];
        int coeff;
        if(level){
            if(level<0) coeff= qmul*level - qadd;
            else        coeff= qmul*level + qadd;
            run_tab[rle_index++]=run;
            run=0;
            s->dsp.add_8x8basis(rem, basis[j], coeff);
        }else{
            run++;
        }
    }
#ifdef REFINE_STATS
if(last_non_zero>0){
STOP_TIMER("init rem[]")
}
}
{START_TIMER
#endif
    for(;;){
        int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
        int best_coeff=0;
        int best_change=0;
        int run2, best_unquant_change=0, analyze_gradient;
#ifdef REFINE_STATS
{START_TIMER
#endif
        analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
        if(analyze_gradient){
#ifdef REFINE_STATS
{START_TIMER
#endif
            for(i=0; i<64; i++){
                int w= weight[i];
                d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
            }
#ifdef REFINE_STATS
STOP_TIMER("rem*w*w")}
{START_TIMER
#endif
            s->dsp.fdct(d1);
#ifdef REFINE_STATS
STOP_TIMER("dct")}
#endif
        }
        if(start_i){
            const int level= block[0];
            int change, old_coeff;
            assert(s->mb_intra);
            old_coeff= q*level;
            for(change=-1; change<=1; change+=2){
                int new_level= level + change;
                int score, new_coeff;
                new_coeff= q*new_level;
                if(new_coeff >= 2048 || new_coeff < 0)
                    continue;
                score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
                if(score<best_score){
                    best_score= score;
                    best_coeff= 0;
                    best_change= change;
                    best_unquant_change= new_coeff - old_coeff;
                }
            }
        }
        run=0;
        rle_index=0;
        run2= run_tab[rle_index++];
        prev_level=0;
        prev_run=0;
        for(i=start_i; i<64; i++){
            int j= perm_scantable[i];
            const int level= block[j];
            int change, old_coeff;
            if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
                break;
            if(level){
                if(level<0) old_coeff= qmul*level - qadd;
                else        old_coeff= qmul*level + qadd;
                run2= run_tab[rle_index++];
            }else{
                old_coeff=0;
                run2--;
                assert(run2>=0 || i >= last_non_zero );
            }
            for(change=-1; change<=1; change+=2){
                int new_level= level + change;
                int score, new_coeff, unquant_change;
                score=0;
                if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
                   continue;
                if(new_level){
                    if(new_level<0) new_coeff= qmul*new_level - qadd;
                    else            new_coeff= qmul*new_level + qadd;
                    if(new_coeff >= 2048 || new_coeff <= -2048)
                        continue;
                    if(level){
                        if(level < 63 && level > -63){
                            if(i < last_non_zero)
                                score +=   length[UNI_AC_ENC_INDEX(run, new_level+64)]
                                         - length[UNI_AC_ENC_INDEX(run, level+64)];
                            else
                                score +=   last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
                                         - last_length[UNI_AC_ENC_INDEX(run, level+64)];
                        }
                    }else{
                        assert(FFABS(new_level)==1);
                        if(analyze_gradient){
                            int g= d1[ scantable[i] ];
                            if(g && (g^new_level) >= 0)
                                continue;
                        }
                        if(i < last_non_zero){
                            int next_i= i + run2 + 1;
                            int next_level= block[ perm_scantable[next_i] ] + 64;
                            if(next_level&(~127))
                                next_level= 0;
                            if(next_i < last_non_zero)
                                score +=   length[UNI_AC_ENC_INDEX(run, 65)]
                                         + length[UNI_AC_ENC_INDEX(run2, next_level)]
                                         - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
                            else
                                score +=  length[UNI_AC_ENC_INDEX(run, 65)]
                                        + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
                                        - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
                        }else{
                            score += last_length[UNI_AC_ENC_INDEX(run, 65)];
                            if(prev_level){
                                score +=  length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
                                        - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
                            }
                        }
                    }
                }else{
                    new_coeff=0;
                    assert(FFABS(level)==1);
                    if(i < last_non_zero){
                        int next_i= i + run2 + 1;
                        int next_level= block[ perm_scantable[next_i] ] + 64;
                        if(next_level&(~127))
                            next_level= 0;
                        if(next_i < last_non_zero)
                            score +=   length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
                                     - length[UNI_AC_ENC_INDEX(run2, next_level)]
                                     - length[UNI_AC_ENC_INDEX(run, 65)];
                        else
                            score +=   last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
                                     - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
                                     - length[UNI_AC_ENC_INDEX(run, 65)];
                    }else{
                        score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
                        if(prev_level){
                            score +=  last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
                                    - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
                        }
                    }
                }
                score *= lambda;
                unquant_change= new_coeff - old_coeff;
                assert((score < 100*lambda && score > -100*lambda) || lambda==0);
                score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
                if(score<best_score){
                    best_score= score;
                    best_coeff= i;
                    best_change= change;
                    best_unquant_change= unquant_change;
                }
            }
            if(level){
                prev_level= level + 64;
                if(prev_level&(~127))
                    prev_level= 0;
                prev_run= run;
                run=0;
            }else{
                run++;
            }
        }
#ifdef REFINE_STATS
STOP_TIMER("iterative step")}
#endif
        if(best_change){
            int j= perm_scantable[ best_coeff ];
            block[j] += best_change;
            if(best_coeff > last_non_zero){
                last_non_zero= best_coeff;
                assert(block[j]);
#ifdef REFINE_STATS
after_last++;
#endif
            }else{
#ifdef REFINE_STATS
if(block[j]){
    if(block[j] - best_change){
        if(FFABS(block[j]) > FFABS(block[j] - best_change)){
            raise++;
        }else{
            lower++;
        }
    }else{
        from_zero++;
    }
}else{
    to_zero++;
}
#endif
                for(; last_non_zero>=start_i; last_non_zero--){
                    if(block[perm_scantable[last_non_zero]])
                        break;
                }
            }
#ifdef REFINE_STATS
count++;
if(256*256*256*64 % count == 0){
    printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
}
#endif
            run=0;
            rle_index=0;
            for(i=start_i; i<=last_non_zero; i++){
                int j= perm_scantable[i];
                const int level= block[j];
                 if(level){
                     run_tab[rle_index++]=run;
                     run=0;
                 }else{
                     run++;
                 }
            }
            s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
        }else{
            break;
        }
    }
#ifdef REFINE_STATS
if(last_non_zero>0){
STOP_TIMER("iterative search")
}
}
#endif
    return last_non_zero;
}
['static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)\n{\n    if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y,  8, 6);\n    else                                encode_mb_internal(s, motion_x, motion_y, 16, 8);\n}', 'static av_always_inline void encode_mb_internal(MpegEncContext *s,\n                                                int motion_x, int motion_y,\n                                                int mb_block_height,\n                                                int mb_block_count)\n{\n    int16_t weight[8][64];\n    DCTELEM orig[8][64];\n    const int mb_x = s->mb_x;\n    const int mb_y = s->mb_y;\n    int i;\n    int skip_dct[8];\n    int dct_offset = s->linesize * 8;\n    uint8_t *ptr_y, *ptr_cb, *ptr_cr;\n    int wrap_y, wrap_c;\n    for (i = 0; i < mb_block_count; i++)\n        skip_dct[i] = s->skipdct;\n    if (s->adaptive_quant) {\n        const int last_qp = s->qscale;\n        const int mb_xy = mb_x + mb_y * s->mb_stride;\n        s->lambda = s->lambda_table[mb_xy];\n        update_qscale(s);\n        if (!(s->flags & CODEC_FLAG_QP_RD)) {\n            s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];\n            s->dquant = s->qscale - last_qp;\n            if (s->out_format == FMT_H263) {\n                s->dquant = av_clip(s->dquant, -2, 2);\n                if (s->codec_id == CODEC_ID_MPEG4) {\n                    if (!s->mb_intra) {\n                        if (s->pict_type == AV_PICTURE_TYPE_B) {\n                            if (s->dquant & 1 || s->mv_dir & MV_DIRECT)\n                                s->dquant = 0;\n                        }\n                        if (s->mv_type == MV_TYPE_8X8)\n                            s->dquant = 0;\n                    }\n                }\n            }\n        }\n        ff_set_qscale(s, last_qp + s->dquant);\n    } else if (s->flags & CODEC_FLAG_QP_RD)\n        ff_set_qscale(s, s->qscale + s->dquant);\n    wrap_y = s->linesize;\n    wrap_c = s->uvlinesize;\n    ptr_y  = s->new_picture.f.data[0] +\n             (mb_y * 16 * wrap_y)              + mb_x * 16;\n    ptr_cb = s->new_picture.f.data[1] +\n             (mb_y * mb_block_height * wrap_c) + mb_x * 8;\n    ptr_cr = s->new_picture.f.data[2] +\n             (mb_y * mb_block_height * wrap_c) + mb_x * 8;\n    if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {\n        uint8_t *ebuf = s->edge_emu_buffer + 32;\n        s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,\n                                mb_y * 16, s->width, s->height);\n        ptr_y = ebuf;\n        s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8,\n                                mb_block_height, mb_x * 8, mb_y * 8,\n                                s->width >> 1, s->height >> 1);\n        ptr_cb = ebuf + 18 * wrap_y;\n        s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8,\n                                mb_block_height, mb_x * 8, mb_y * 8,\n                                s->width >> 1, s->height >> 1);\n        ptr_cr = ebuf + 18 * wrap_y + 8;\n    }\n    if (s->mb_intra) {\n        if (s->flags & CODEC_FLAG_INTERLACED_DCT) {\n            int progressive_score, interlaced_score;\n            s->interlaced_dct = 0;\n            progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,\n                                                    NULL, wrap_y, 8) +\n                                s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,\n                                                    NULL, wrap_y, 8) - 400;\n            if (progressive_score > 0) {\n                interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,\n                                                       NULL, wrap_y * 2, 8) +\n                                   s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,\n                                                       NULL, wrap_y * 2, 8);\n                if (progressive_score > interlaced_score) {\n                    s->interlaced_dct = 1;\n                    dct_offset = wrap_y;\n                    wrap_y <<= 1;\n                    if (s->chroma_format == CHROMA_422)\n                        wrap_c <<= 1;\n                }\n            }\n        }\n        s->dsp.get_pixels(s->block[0], ptr_y                  , wrap_y);\n        s->dsp.get_pixels(s->block[1], ptr_y              + 8 , wrap_y);\n        s->dsp.get_pixels(s->block[2], ptr_y + dct_offset     , wrap_y);\n        s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);\n        if (s->flags & CODEC_FLAG_GRAY) {\n            skip_dct[4] = 1;\n            skip_dct[5] = 1;\n        } else {\n            s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);\n            s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);\n            if (!s->chroma_y_shift) {\n                s->dsp.get_pixels(s->block[6],\n                                  ptr_cb + (dct_offset >> 1), wrap_c);\n                s->dsp.get_pixels(s->block[7],\n                                  ptr_cr + (dct_offset >> 1), wrap_c);\n            }\n        }\n    } else {\n        op_pixels_func (*op_pix)[4];\n        qpel_mc_func (*op_qpix)[16];\n        uint8_t *dest_y, *dest_cb, *dest_cr;\n        dest_y  = s->dest[0];\n        dest_cb = s->dest[1];\n        dest_cr = s->dest[2];\n        if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {\n            op_pix  = s->dsp.put_pixels_tab;\n            op_qpix = s->dsp.put_qpel_pixels_tab;\n        } else {\n            op_pix  = s->dsp.put_no_rnd_pixels_tab;\n            op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;\n        }\n        if (s->mv_dir & MV_DIR_FORWARD) {\n            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data,\n                       op_pix, op_qpix);\n            op_pix  = s->dsp.avg_pixels_tab;\n            op_qpix = s->dsp.avg_qpel_pixels_tab;\n        }\n        if (s->mv_dir & MV_DIR_BACKWARD) {\n            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data,\n                       op_pix, op_qpix);\n        }\n        if (s->flags & CODEC_FLAG_INTERLACED_DCT) {\n            int progressive_score, interlaced_score;\n            s->interlaced_dct = 0;\n            progressive_score = s->dsp.ildct_cmp[0](s, dest_y,\n                                                    ptr_y,              wrap_y,\n                                                    8) +\n                                s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,\n                                                    ptr_y + wrap_y * 8, wrap_y,\n                                                    8) - 400;\n            if (s->avctx->ildct_cmp == FF_CMP_VSSE)\n                progressive_score -= 400;\n            if (progressive_score > 0) {\n                interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,\n                                                       ptr_y,\n                                                       wrap_y * 2, 8) +\n                                   s->dsp.ildct_cmp[0](s, dest_y + wrap_y,\n                                                       ptr_y + wrap_y,\n                                                       wrap_y * 2, 8);\n                if (progressive_score > interlaced_score) {\n                    s->interlaced_dct = 1;\n                    dct_offset = wrap_y;\n                    wrap_y <<= 1;\n                    if (s->chroma_format == CHROMA_422)\n                        wrap_c <<= 1;\n                }\n            }\n        }\n        s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);\n        s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);\n        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,\n                           dest_y + dct_offset, wrap_y);\n        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,\n                           dest_y + dct_offset + 8, wrap_y);\n        if (s->flags & CODEC_FLAG_GRAY) {\n            skip_dct[4] = 1;\n            skip_dct[5] = 1;\n        } else {\n            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);\n            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);\n            if (!s->chroma_y_shift) {\n                s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),\n                                   dest_cb + (dct_offset >> 1), wrap_c);\n                s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),\n                                   dest_cr + (dct_offset >> 1), wrap_c);\n            }\n        }\n        if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <\n                2 * s->qscale * s->qscale) {\n            if (s->dsp.sad[1](NULL, ptr_y , dest_y,\n                              wrap_y, 8) < 20 * s->qscale)\n                skip_dct[0] = 1;\n            if (s->dsp.sad[1](NULL, ptr_y + 8,\n                              dest_y + 8, wrap_y, 8) < 20 * s->qscale)\n                skip_dct[1] = 1;\n            if (s->dsp.sad[1](NULL, ptr_y + dct_offset,\n                              dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)\n                skip_dct[2] = 1;\n            if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,\n                              dest_y + dct_offset + 8,\n                              wrap_y, 8) < 20 * s->qscale)\n                skip_dct[3] = 1;\n            if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,\n                              wrap_c, 8) < 20 * s->qscale)\n                skip_dct[4] = 1;\n            if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,\n                              wrap_c, 8) < 20 * s->qscale)\n                skip_dct[5] = 1;\n            if (!s->chroma_y_shift) {\n                if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),\n                                  dest_cb + (dct_offset >> 1),\n                                  wrap_c, 8) < 20 * s->qscale)\n                    skip_dct[6] = 1;\n                if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),\n                                  dest_cr + (dct_offset >> 1),\n                                  wrap_c, 8) < 20 * s->qscale)\n                    skip_dct[7] = 1;\n            }\n        }\n    }\n    if (s->avctx->quantizer_noise_shaping) {\n        if (!skip_dct[0])\n            get_visual_weight(weight[0], ptr_y                 , wrap_y);\n        if (!skip_dct[1])\n            get_visual_weight(weight[1], ptr_y              + 8, wrap_y);\n        if (!skip_dct[2])\n            get_visual_weight(weight[2], ptr_y + dct_offset    , wrap_y);\n        if (!skip_dct[3])\n            get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);\n        if (!skip_dct[4])\n            get_visual_weight(weight[4], ptr_cb                , wrap_c);\n        if (!skip_dct[5])\n            get_visual_weight(weight[5], ptr_cr                , wrap_c);\n        if (!s->chroma_y_shift) {\n            if (!skip_dct[6])\n                get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),\n                                  wrap_c);\n            if (!skip_dct[7])\n                get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),\n                                  wrap_c);\n        }\n        memcpy(orig[0], s->block[0], sizeof(DCTELEM) * 64 * mb_block_count);\n    }\n    assert(s->out_format != FMT_MJPEG || s->qscale == 8);\n    {\n        for (i = 0; i < mb_block_count; i++) {\n            if (!skip_dct[i]) {\n                int overflow;\n                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);\n                if (overflow)\n                    clip_coeffs(s, s->block[i], s->block_last_index[i]);\n            } else\n                s->block_last_index[i] = -1;\n        }\n        if (s->avctx->quantizer_noise_shaping) {\n            for (i = 0; i < mb_block_count; i++) {\n                if (!skip_dct[i]) {\n                    s->block_last_index[i] =\n                        dct_quantize_refine(s, s->block[i], weight[i],\n                                            orig[i], i, s->qscale);\n                }\n            }\n        }\n        if (s->luma_elim_threshold && !s->mb_intra)\n            for (i = 0; i < 4; i++)\n                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);\n        if (s->chroma_elim_threshold && !s->mb_intra)\n            for (i = 4; i < mb_block_count; i++)\n                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);\n        if (s->flags & CODEC_FLAG_CBP_RD) {\n            for (i = 0; i < mb_block_count; i++) {\n                if (s->block_last_index[i] == -1)\n                    s->coded_score[i] = INT_MAX / 256;\n            }\n        }\n    }\n    if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {\n        s->block_last_index[4] =\n        s->block_last_index[5] = 0;\n        s->block[4][0] =\n        s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;\n    }\n    if (s->alternate_scan && s->dct_quantize != dct_quantize_c) {\n        for (i = 0; i < mb_block_count; i++) {\n            int j;\n            if (s->block_last_index[i] > 0) {\n                for (j = 63; j > 0; j--) {\n                    if (s->block[i][s->intra_scantable.permutated[j]])\n                        break;\n                }\n                s->block_last_index[i] = j;\n            }\n        }\n    }\n    switch(s->codec_id){\n    case CODEC_ID_MPEG1VIDEO:\n    case CODEC_ID_MPEG2VIDEO:\n        if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)\n            mpeg1_encode_mb(s, s->block, motion_x, motion_y);\n        break;\n    case CODEC_ID_MPEG4:\n        if (CONFIG_MPEG4_ENCODER)\n            mpeg4_encode_mb(s, s->block, motion_x, motion_y);\n        break;\n    case CODEC_ID_MSMPEG4V2:\n    case CODEC_ID_MSMPEG4V3:\n    case CODEC_ID_WMV1:\n        if (CONFIG_MSMPEG4_ENCODER)\n            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);\n        break;\n    case CODEC_ID_WMV2:\n        if (CONFIG_WMV2_ENCODER)\n            ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);\n        break;\n    case CODEC_ID_H261:\n        if (CONFIG_H261_ENCODER)\n            ff_h261_encode_mb(s, s->block, motion_x, motion_y);\n        break;\n    case CODEC_ID_H263:\n    case CODEC_ID_H263P:\n    case CODEC_ID_FLV1:\n    case CODEC_ID_RV10:\n    case CODEC_ID_RV20:\n        if (CONFIG_H263_ENCODER)\n            h263_encode_mb(s, s->block, motion_x, motion_y);\n        break;\n    case CODEC_ID_MJPEG:\n        if (CONFIG_MJPEG_ENCODER)\n            ff_mjpeg_encode_mb(s, s->block);\n        break;\n    default:\n        assert(0);\n    }\n}', 'static int dct_quantize_refine(MpegEncContext *s,\n                        DCTELEM *block, int16_t *weight, DCTELEM *orig,\n                        int n, int qscale){\n    int16_t rem[64];\n    LOCAL_ALIGNED_16(DCTELEM, d1, [64]);\n    const uint8_t *scantable= s->intra_scantable.scantable;\n    const uint8_t *perm_scantable= s->intra_scantable.permutated;\n    int run_tab[65];\n    int prev_run=0;\n    int prev_level=0;\n    int qmul, qadd, start_i, last_non_zero, i, dc;\n    uint8_t * length;\n    uint8_t * last_length;\n    int lambda;\n    int rle_index, run, q = 1, sum;\n#ifdef REFINE_STATS\nstatic int count=0;\nstatic int after_last=0;\nstatic int to_zero=0;\nstatic int from_zero=0;\nstatic int raise=0;\nstatic int lower=0;\nstatic int messed_sign=0;\n#endif\n    if(basis[0][0] == 0)\n        build_basis(s->dsp.idct_permutation);\n    qmul= qscale*2;\n    qadd= (qscale-1)|1;\n    if (s->mb_intra) {\n        if (!s->h263_aic) {\n            if (n < 4)\n                q = s->y_dc_scale;\n            else\n                q = s->c_dc_scale;\n        } else{\n            q = 1;\n            qadd=0;\n        }\n        q <<= RECON_SHIFT-3;\n        dc= block[0]*q;\n        start_i = 1;\n        length     = s->intra_ac_vlc_length;\n        last_length= s->intra_ac_vlc_last_length;\n    } else {\n        dc= 0;\n        start_i = 0;\n        length     = s->inter_ac_vlc_length;\n        last_length= s->inter_ac_vlc_last_length;\n    }\n    last_non_zero = s->block_last_index[n];\n#ifdef REFINE_STATS\n{START_TIMER\n#endif\n    dc += (1<<(RECON_SHIFT-1));\n    for(i=0; i<64; i++){\n        rem[i]= dc - (orig[i]<<RECON_SHIFT);\n    }\n#ifdef REFINE_STATS\nSTOP_TIMER("memset rem[]")}\n#endif\n    sum=0;\n    for(i=0; i<64; i++){\n        int one= 36;\n        int qns=4;\n        int w;\n        w= FFABS(weight[i]) + qns*one;\n        w= 15 + (48*qns*one + w/2)/w;\n        weight[i] = w;\n        assert(w>0);\n        assert(w<(1<<6));\n        sum += w*w;\n    }\n    lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);\n#ifdef REFINE_STATS\n{START_TIMER\n#endif\n    run=0;\n    rle_index=0;\n    for(i=start_i; i<=last_non_zero; i++){\n        int j= perm_scantable[i];\n        const int level= block[j];\n        int coeff;\n        if(level){\n            if(level<0) coeff= qmul*level - qadd;\n            else        coeff= qmul*level + qadd;\n            run_tab[rle_index++]=run;\n            run=0;\n            s->dsp.add_8x8basis(rem, basis[j], coeff);\n        }else{\n            run++;\n        }\n    }\n#ifdef REFINE_STATS\nif(last_non_zero>0){\nSTOP_TIMER("init rem[]")\n}\n}\n{START_TIMER\n#endif\n    for(;;){\n        int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);\n        int best_coeff=0;\n        int best_change=0;\n        int run2, best_unquant_change=0, analyze_gradient;\n#ifdef REFINE_STATS\n{START_TIMER\n#endif\n        analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;\n        if(analyze_gradient){\n#ifdef REFINE_STATS\n{START_TIMER\n#endif\n            for(i=0; i<64; i++){\n                int w= weight[i];\n                d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);\n            }\n#ifdef REFINE_STATS\nSTOP_TIMER("rem*w*w")}\n{START_TIMER\n#endif\n            s->dsp.fdct(d1);\n#ifdef REFINE_STATS\nSTOP_TIMER("dct")}\n#endif\n        }\n        if(start_i){\n            const int level= block[0];\n            int change, old_coeff;\n            assert(s->mb_intra);\n            old_coeff= q*level;\n            for(change=-1; change<=1; change+=2){\n                int new_level= level + change;\n                int score, new_coeff;\n                new_coeff= q*new_level;\n                if(new_coeff >= 2048 || new_coeff < 0)\n                    continue;\n                score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);\n                if(score<best_score){\n                    best_score= score;\n                    best_coeff= 0;\n                    best_change= change;\n                    best_unquant_change= new_coeff - old_coeff;\n                }\n            }\n        }\n        run=0;\n        rle_index=0;\n        run2= run_tab[rle_index++];\n        prev_level=0;\n        prev_run=0;\n        for(i=start_i; i<64; i++){\n            int j= perm_scantable[i];\n            const int level= block[j];\n            int change, old_coeff;\n            if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)\n                break;\n            if(level){\n                if(level<0) old_coeff= qmul*level - qadd;\n                else        old_coeff= qmul*level + qadd;\n                run2= run_tab[rle_index++];\n            }else{\n                old_coeff=0;\n                run2--;\n                assert(run2>=0 || i >= last_non_zero );\n            }\n            for(change=-1; change<=1; change+=2){\n                int new_level= level + change;\n                int score, new_coeff, unquant_change;\n                score=0;\n                if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))\n                   continue;\n                if(new_level){\n                    if(new_level<0) new_coeff= qmul*new_level - qadd;\n                    else            new_coeff= qmul*new_level + qadd;\n                    if(new_coeff >= 2048 || new_coeff <= -2048)\n                        continue;\n                    if(level){\n                        if(level < 63 && level > -63){\n                            if(i < last_non_zero)\n                                score +=   length[UNI_AC_ENC_INDEX(run, new_level+64)]\n                                         - length[UNI_AC_ENC_INDEX(run, level+64)];\n                            else\n                                score +=   last_length[UNI_AC_ENC_INDEX(run, new_level+64)]\n                                         - last_length[UNI_AC_ENC_INDEX(run, level+64)];\n                        }\n                    }else{\n                        assert(FFABS(new_level)==1);\n                        if(analyze_gradient){\n                            int g= d1[ scantable[i] ];\n                            if(g && (g^new_level) >= 0)\n                                continue;\n                        }\n                        if(i < last_non_zero){\n                            int next_i= i + run2 + 1;\n                            int next_level= block[ perm_scantable[next_i] ] + 64;\n                            if(next_level&(~127))\n                                next_level= 0;\n                            if(next_i < last_non_zero)\n                                score +=   length[UNI_AC_ENC_INDEX(run, 65)]\n                                         + length[UNI_AC_ENC_INDEX(run2, next_level)]\n                                         - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];\n                            else\n                                score +=  length[UNI_AC_ENC_INDEX(run, 65)]\n                                        + last_length[UNI_AC_ENC_INDEX(run2, next_level)]\n                                        - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];\n                        }else{\n                            score += last_length[UNI_AC_ENC_INDEX(run, 65)];\n                            if(prev_level){\n                                score +=  length[UNI_AC_ENC_INDEX(prev_run, prev_level)]\n                                        - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];\n                            }\n                        }\n                    }\n                }else{\n                    new_coeff=0;\n                    assert(FFABS(level)==1);\n                    if(i < last_non_zero){\n                        int next_i= i + run2 + 1;\n                        int next_level= block[ perm_scantable[next_i] ] + 64;\n                        if(next_level&(~127))\n                            next_level= 0;\n                        if(next_i < last_non_zero)\n                            score +=   length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]\n                                     - length[UNI_AC_ENC_INDEX(run2, next_level)]\n                                     - length[UNI_AC_ENC_INDEX(run, 65)];\n                        else\n                            score +=   last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]\n                                     - last_length[UNI_AC_ENC_INDEX(run2, next_level)]\n                                     - length[UNI_AC_ENC_INDEX(run, 65)];\n                    }else{\n                        score += -last_length[UNI_AC_ENC_INDEX(run, 65)];\n                        if(prev_level){\n                            score +=  last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]\n                                    - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];\n                        }\n                    }\n                }\n                score *= lambda;\n                unquant_change= new_coeff - old_coeff;\n                assert((score < 100*lambda && score > -100*lambda) || lambda==0);\n                score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);\n                if(score<best_score){\n                    best_score= score;\n                    best_coeff= i;\n                    best_change= change;\n                    best_unquant_change= unquant_change;\n                }\n            }\n            if(level){\n                prev_level= level + 64;\n                if(prev_level&(~127))\n                    prev_level= 0;\n                prev_run= run;\n                run=0;\n            }else{\n                run++;\n            }\n        }\n#ifdef REFINE_STATS\nSTOP_TIMER("iterative step")}\n#endif\n        if(best_change){\n            int j= perm_scantable[ best_coeff ];\n            block[j] += best_change;\n            if(best_coeff > last_non_zero){\n                last_non_zero= best_coeff;\n                assert(block[j]);\n#ifdef REFINE_STATS\nafter_last++;\n#endif\n            }else{\n#ifdef REFINE_STATS\nif(block[j]){\n    if(block[j] - best_change){\n        if(FFABS(block[j]) > FFABS(block[j] - best_change)){\n            raise++;\n        }else{\n            lower++;\n        }\n    }else{\n        from_zero++;\n    }\n}else{\n    to_zero++;\n}\n#endif\n                for(; last_non_zero>=start_i; last_non_zero--){\n                    if(block[perm_scantable[last_non_zero]])\n                        break;\n                }\n            }\n#ifdef REFINE_STATS\ncount++;\nif(256*256*256*64 % count == 0){\n    printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);\n}\n#endif\n            run=0;\n            rle_index=0;\n            for(i=start_i; i<=last_non_zero; i++){\n                int j= perm_scantable[i];\n                const int level= block[j];\n                 if(level){\n                     run_tab[rle_index++]=run;\n                     run=0;\n                 }else{\n                     run++;\n                 }\n            }\n            s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);\n        }else{\n            break;\n        }\n    }\n#ifdef REFINE_STATS\nif(last_non_zero>0){\nSTOP_TIMER("iterative search")\n}\n}\n#endif\n    return last_non_zero;\n}']