libavcodec/motion_est.c:981: error: Buffer Overrun L1
  Offset: [8, 9] (⇐ [0, 1] + 8) Size: 4 by call to `interlaced_search`.

libavcodec/motion_est.c:981:16: Call
979.         if ((s->avctx->flags & CODEC_FLAG_INTERLACED_ME)
980.            && !c->skip){ //FIXME varc/d checks
981.             if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
                    ^
982.                 mb_type |= CANDIDATE_MB_TYPE_INTER_I;
983.         }

libavcodec/motion_est.c:718:1: Parameter `ref_index`
  716. }
  717. 
  718. static int interlaced_search(MpegEncContext *s, int ref_index,
       ^
  719.                              int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
  720. {

libavcodec/motion_est.c:778:20: Call
  776.             P_MV1[1]= my / 2;
  777. 
  778.             dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
                          ^
  779. 
  780.             dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);

libavcodec/motion_est_template.c:1051:1: Parameter `ref_index`
    1049. 
    1050. //try to merge with above FIXME (needs PSNR test)
    1051. static int epzs_motion_search2(MpegEncContext * s,
          ^
    1052.                              int *mx_ptr, int *my_ptr, int P[10][2],
    1053.                              int src_index, int ref_index, int16_t (*last_mv)[2],

libavcodec/motion_est_template.c:1079:9: Call
    1077.     /* first line */
    1078.     if (s->first_slice_line) {
    1079.         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
                  ^
    1080.         CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
    1081.                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)

libavcodec/motion_est.c:256:1: Parameter `ref_index`
      254.     against a proposed motion-compensated prediction of that block
      255.  */
      256. static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
           ^
      257.                       const int size, const int h, int ref_index, int src_index,
      258.                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){

libavcodec/motion_est.c:265:16: Call
      263.     }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
      264.        && subx==0 && suby==0){
      265.         return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);
                          ^
      266.     }else{
      267.         return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);

libavcodec/motion_est.c:233:1: Parameter `ref_index`
        231. }
        232. 
        233. static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y,
             ^
        234.                       const int size, const int h, int ref_index, int src_index,
        235.                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){

libavcodec/motion_est.c:237:16: Call
        235.                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
        236.     if(flags&FLAG_DIRECT){
        237.         return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
                            ^
        238.     }else{
        239.         return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);

libavcodec/motion_est.c:109:1: <Length trace>
107. }
108. 
109. static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
     ^
110.                       const int size, const int h, int ref_index, int src_index,
111.                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){

libavcodec/motion_est.c:109:1: Parameter `ref_index`
          107. }
          108. 
          109. static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
               ^
          110.                       const int size, const int h, int ref_index, int src_index,
          111.                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){

libavcodec/motion_est.c:116:5: Assignment
          114.     const int hx= subx + (x<<(1+qpel));
          115.     const int hy= suby + (y<<(1+qpel));
          116.     uint8_t * const * const ref= c->ref[ref_index];
                   ^
          117.     uint8_t * const * const src= c->src[src_index];
          118.     int d;

libavcodec/motion_est.c:172:50: Array access: Offset: [8, 9] (⇐ [0, 1] + 8) Size: 4 by call to `interlaced_search` 
          170. 
          171.                     c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
          172.                     c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
                                                                ^
          173.                 }
          174.             }

static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
                      const int size, const int h, int ref_index, int src_index,
                      me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){
    MotionEstContext * const c= &s->me;
    const int stride= c->stride;
    const int hx= subx + (x<<(1+qpel));
    const int hy= suby + (y<<(1+qpel));
    uint8_t * const * const ref= c->ref[ref_index];
    uint8_t * const * const src= c->src[src_index];
    int d;
        assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
        if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
            const int time_pp= s->pp_time;
            const int time_pb= s->pb_time;
            const int mask= 2*qpel+1;
            if(s->mv_type==MV_TYPE_8X8){
                int i;
                for(i=0; i<4; i++){
                    int fx = c->direct_basis_mv[i][0] + hx;
                    int fy = c->direct_basis_mv[i][1] + hy;
                    int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));
                    int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));
                    int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
                    int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
                    uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
                    if(qpel){
                        c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
                        c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
                    }else{
                        c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
                        c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
                    }
                }
            }else{
                int fx = c->direct_basis_mv[0][0] + hx;
                int fy = c->direct_basis_mv[0][1] + hy;
                int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
                int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
                int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
                int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
                if(qpel){
                    c->qpel_put[1][fxy](c->temp               , ref[0] + (fx>>2) + (fy>>2)*stride               , stride);
                    c->qpel_put[1][fxy](c->temp + 8           , ref[0] + (fx>>2) + (fy>>2)*stride + 8           , stride);
                    c->qpel_put[1][fxy](c->temp     + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride     + 8*stride, stride);
                    c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
                    c->qpel_avg[1][bxy](c->temp               , ref[8] + (bx>>2) + (by>>2)*stride               , stride);
                    c->qpel_avg[1][bxy](c->temp + 8           , ref[8] + (bx>>2) + (by>>2)*stride + 8           , stride);
                    c->qpel_avg[1][bxy](c->temp     + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride     + 8*stride, stride);
                    c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
                }else{
                    assert((fx>>1) + 16*s->mb_x >= -16);
                    assert((fy>>1) + 16*s->mb_y >= -16);
                    assert((fx>>1) + 16*s->mb_x <= s->width);
                    assert((fy>>1) + 16*s->mb_y <= s->height);
                    assert((bx>>1) + 16*s->mb_x >= -16);
                    assert((by>>1) + 16*s->mb_y >= -16);
                    assert((bx>>1) + 16*s->mb_x <= s->width);
                    assert((by>>1) + 16*s->mb_y <= s->height);
                    c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
                    c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
                }
            }
            d = cmp_func(s, c->temp, src[0], stride, 16);
        }else
            d= 256*256*256*32;
    return d;
}
['void ff_estimate_p_frame_motion(MpegEncContext * s,\n                                int mb_x, int mb_y)\n{\n    MotionEstContext * const c= &s->me;\n    uint8_t *pix, *ppix;\n    int sum, mx, my, dmin;\n    int varc;\n    int vard;\n    int P[10][2];\n    const int shift= 1+s->quarter_sample;\n    int mb_type=0;\n    Picture * const pic= &s->current_picture;\n    init_ref(c, s->new_picture.f->data, s->last_picture.f->data, NULL, 16*mb_x, 16*mb_y, 0);\n    assert(s->quarter_sample==0 || s->quarter_sample==1);\n    assert(s->linesize == c->stride);\n    assert(s->uvlinesize == c->uvstride);\n    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);\n    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);\n    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);\n    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n    get_limits(s, 16*mb_x, 16*mb_y);\n    c->skip=0;\n    pix = c->src[0][0];\n    sum  = s->mpvencdsp.pix_sum(pix, s->linesize);\n    varc = s->mpvencdsp.pix_norm1(pix, s->linesize) -\n           (((unsigned) sum * sum) >> 8) + 500;\n    pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;\n    pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;\n    c->mb_var_sum_temp += (varc+128)>>8;\n    switch(s->me_method) {\n    case ME_ZERO:\n    default:\n        mx   = 0;\n        my   = 0;\n        dmin = 0;\n        break;\n    case ME_X1:\n    case ME_EPZS:\n       {\n            const int mot_stride = s->b8_stride;\n            const int mot_xy = s->block_index[0];\n            P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];\n            P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];\n            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);\n            if(!s->first_slice_line) {\n                P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];\n                P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];\n                P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];\n                P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];\n                if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);\n                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);\n                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);\n                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);\n                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);\n                if(s->out_format == FMT_H263){\n                    c->pred_x = P_MEDIAN[0];\n                    c->pred_y = P_MEDIAN[1];\n                }else {\n                    c->pred_x= P_LEFT[0];\n                    c->pred_y= P_LEFT[1];\n                }\n            }else{\n                c->pred_x= P_LEFT[0];\n                c->pred_y= P_LEFT[1];\n            }\n        }\n        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);\n        break;\n    }\n    ppix = c->ref[0][0] + (my * s->linesize) + mx;\n    vard = s->mecc.sse[0](NULL, pix, ppix, s->linesize, 16);\n    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;\n    c->mc_mb_var_sum_temp += (vard+128)>>8;\n    if (c->avctx->mb_decision > FF_MB_DECISION_SIMPLE) {\n        int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);\n        int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;\n        c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);\n        if (vard*2 + 200*256 > varc)\n            mb_type|= CANDIDATE_MB_TYPE_INTRA;\n        if (varc*2 + 200*256 > vard || s->qscale > 24){\n            mb_type|= CANDIDATE_MB_TYPE_INTER;\n            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);\n            if (s->mpv_flags & FF_MPV_FLAG_MV0)\n                if(mx || my)\n                    mb_type |= CANDIDATE_MB_TYPE_SKIPPED;\n        }else{\n            mx <<=shift;\n            my <<=shift;\n        }\n        if ((s->avctx->flags & CODEC_FLAG_4MV)\n           && !c->skip && varc>50<<8 && vard>10<<8){\n            if(h263_mv4_search(s, mx, my, shift) < INT_MAX)\n                mb_type|=CANDIDATE_MB_TYPE_INTER4V;\n            set_p_mv_tables(s, mx, my, 0);\n        }else\n            set_p_mv_tables(s, mx, my, 1);\n        if ((s->avctx->flags & CODEC_FLAG_INTERLACED_ME)\n           && !c->skip){\n            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)\n                mb_type |= CANDIDATE_MB_TYPE_INTER_I;\n        }\n    }else{\n        int intra_score, i;\n        mb_type= CANDIDATE_MB_TYPE_INTER;\n        dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);\n        if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)\n            dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);\n        if ((s->avctx->flags & CODEC_FLAG_4MV)\n           && !c->skip && varc>50<<8 && vard>10<<8){\n            int dmin4= h263_mv4_search(s, mx, my, shift);\n            if(dmin4 < dmin){\n                mb_type= CANDIDATE_MB_TYPE_INTER4V;\n                dmin=dmin4;\n            }\n        }\n        if ((s->avctx->flags & CODEC_FLAG_INTERLACED_ME)\n           && !c->skip){\n            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);\n            if(dmin_i < dmin){\n                mb_type = CANDIDATE_MB_TYPE_INTER_I;\n                dmin= dmin_i;\n            }\n        }\n        set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);\n        if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){\n            intra_score= varc - 500;\n        }else{\n            unsigned mean = (sum+128)>>8;\n            mean*= 0x01010101;\n            for(i=0; i<16; i++){\n                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;\n                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;\n                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;\n                *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;\n            }\n            intra_score= s->mecc.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);\n        }\n        intra_score += c->mb_penalty_factor*16;\n        if(intra_score < dmin){\n            mb_type= CANDIDATE_MB_TYPE_INTRA;\n            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA;\n        }else\n            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = 0;\n        {\n            int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);\n            int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;\n            c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);\n        }\n    }\n    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;\n}', 'static int interlaced_search(MpegEncContext *s, int ref_index,\n                             int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)\n{\n    MotionEstContext * const c= &s->me;\n    const int size=0;\n    const int h=8;\n    int block;\n    int P[10][2];\n    uint8_t * const mv_penalty= c->current_mv_penalty;\n    int same=1;\n    const int stride= 2*s->linesize;\n    int dmin_sum= 0;\n    const int mot_stride= s->mb_stride;\n    const int xy= s->mb_x + s->mb_y*mot_stride;\n    c->ymin>>=1;\n    c->ymax>>=1;\n    c->stride<<=1;\n    c->uvstride<<=1;\n    init_interlaced_ref(s, ref_index);\n    for(block=0; block<2; block++){\n        int field_select;\n        int best_dmin= INT_MAX;\n        int best_field= -1;\n        for(field_select=0; field_select<2; field_select++){\n            int dmin, mx_i, my_i;\n            int16_t (*mv_table)[2]= mv_tables[block][field_select];\n            if(user_field_select){\n                assert(field_select==0 || field_select==1);\n                assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);\n                if(field_select_tables[block][xy] != field_select)\n                    continue;\n            }\n            P_LEFT[0] = mv_table[xy - 1][0];\n            P_LEFT[1] = mv_table[xy - 1][1];\n            if(P_LEFT[0]       > (c->xmax<<1)) P_LEFT[0]       = (c->xmax<<1);\n            c->pred_x= P_LEFT[0];\n            c->pred_y= P_LEFT[1];\n            if(!s->first_slice_line){\n                P_TOP[0]      = mv_table[xy - mot_stride][0];\n                P_TOP[1]      = mv_table[xy - mot_stride][1];\n                P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];\n                P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];\n                if(P_TOP[1]      > (c->ymax<<1)) P_TOP[1]     = (c->ymax<<1);\n                if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);\n                if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);\n                if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);\n                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);\n                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);\n            }\n            P_MV1[0]= mx;\n            P_MV1[1]= my / 2;\n            dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);\n            dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);\n            mv_table[xy][0]= mx_i;\n            mv_table[xy][1]= my_i;\n            if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {\n                int dxy;\n                uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;\n                dxy = ((my_i & 1) << 1) | (mx_i & 1);\n                if(s->no_rounding){\n                    s->hdsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref    , stride, h);\n                }else{\n                    s->hdsp.put_pixels_tab       [size][dxy](c->scratchpad, ref    , stride, h);\n                }\n                dmin = s->mecc.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);\n                dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;\n            }else\n                dmin+= c->mb_penalty_factor;\n            dmin += field_select != block;\n            if(dmin < best_dmin){\n                best_dmin= dmin;\n                best_field= field_select;\n            }\n        }\n        {\n            int16_t (*mv_table)[2]= mv_tables[block][best_field];\n            if(mv_table[xy][0] != mx) same=0;\n            if(mv_table[xy][1]&1) same=0;\n            if(mv_table[xy][1]*2 != my) same=0;\n            if(best_field != block) same=0;\n        }\n        field_select_tables[block][xy]= best_field;\n        dmin_sum += best_dmin;\n    }\n    c->ymin<<=1;\n    c->ymax<<=1;\n    c->stride>>=1;\n    c->uvstride>>=1;\n    if(same)\n        return INT_MAX;\n    switch(c->avctx->mb_cmp&0xFF){\n    case FF_CMP_RD:\n        return dmin_sum;\n    default:\n        return dmin_sum+ 11*c->mb_penalty_factor;\n    }\n}', 'static int epzs_motion_search2(MpegEncContext * s,\n                             int *mx_ptr, int *my_ptr, int P[10][2],\n                             int src_index, int ref_index, int16_t (*last_mv)[2],\n                             int ref_mv_scale)\n{\n    MotionEstContext * const c= &s->me;\n    int best[2]={0, 0};\n    int d, dmin;\n    unsigned map_generation;\n    const int penalty_factor= c->penalty_factor;\n    const int size=0;\n    const int h=8;\n    const int ref_mv_stride= s->mb_stride;\n    const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;\n    me_cmp_func cmpf, chroma_cmpf;\n    LOAD_COMMON\n    int flags= c->flags;\n    LOAD_COMMON2\n    cmpf        = s->mecc.me_cmp[size];\n    chroma_cmpf = s->mecc.me_cmp[size + 1];\n    map_generation= update_map_generation(c);\n    dmin = 1000000;\n    if (s->first_slice_line) {\n        CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)\n        CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,\n                        (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)\n        CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)\n    }else{\n        CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)\n        CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)\n        CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)\n        CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)\n        CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)\n        CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,\n                        (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)\n    }\n    if(dmin>64*4){\n        CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,\n                        (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)\n        if(s->mb_y+1<s->end_mb_y)\n            CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,\n                            (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)\n    }\n    dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n    *mx_ptr= best[0];\n    *my_ptr= best[1];\n    return dmin;\n}', 'static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,\n                      const int size, const int h, int ref_index, int src_index,\n                      me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){\n    if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size)\n       && av_builtin_constant_p(subx) && av_builtin_constant_p(suby)\n       && flags==0 && h==16 && size==0 && subx==0 && suby==0){\n        return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);\n    }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)\n       && subx==0 && suby==0){\n        return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);\n    }else{\n        return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);\n    }\n}', 'static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y,\n                      const int size, const int h, int ref_index, int src_index,\n                      me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){\n    if(flags&FLAG_DIRECT){\n        return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);\n    }else{\n        return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);\n    }\n}', 'static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,\n                      const int size, const int h, int ref_index, int src_index,\n                      me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){\n    MotionEstContext * const c= &s->me;\n    const int stride= c->stride;\n    const int hx= subx + (x<<(1+qpel));\n    const int hy= suby + (y<<(1+qpel));\n    uint8_t * const * const ref= c->ref[ref_index];\n    uint8_t * const * const src= c->src[src_index];\n    int d;\n        assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));\n        if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){\n            const int time_pp= s->pp_time;\n            const int time_pb= s->pb_time;\n            const int mask= 2*qpel+1;\n            if(s->mv_type==MV_TYPE_8X8){\n                int i;\n                for(i=0; i<4; i++){\n                    int fx = c->direct_basis_mv[i][0] + hx;\n                    int fy = c->direct_basis_mv[i][1] + hy;\n                    int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));\n                    int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));\n                    int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));\n                    int bxy= (bx&mask) + ((by&mask)<<(qpel+1));\n                    uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);\n                    if(qpel){\n                        c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);\n                        c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);\n                    }else{\n                        c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);\n                        c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);\n                    }\n                }\n            }else{\n                int fx = c->direct_basis_mv[0][0] + hx;\n                int fy = c->direct_basis_mv[0][1] + hy;\n                int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);\n                int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);\n                int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));\n                int bxy= (bx&mask) + ((by&mask)<<(qpel+1));\n                if(qpel){\n                    c->qpel_put[1][fxy](c->temp               , ref[0] + (fx>>2) + (fy>>2)*stride               , stride);\n                    c->qpel_put[1][fxy](c->temp + 8           , ref[0] + (fx>>2) + (fy>>2)*stride + 8           , stride);\n                    c->qpel_put[1][fxy](c->temp     + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride     + 8*stride, stride);\n                    c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);\n                    c->qpel_avg[1][bxy](c->temp               , ref[8] + (bx>>2) + (by>>2)*stride               , stride);\n                    c->qpel_avg[1][bxy](c->temp + 8           , ref[8] + (bx>>2) + (by>>2)*stride + 8           , stride);\n                    c->qpel_avg[1][bxy](c->temp     + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride     + 8*stride, stride);\n                    c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);\n                }else{\n                    assert((fx>>1) + 16*s->mb_x >= -16);\n                    assert((fy>>1) + 16*s->mb_y >= -16);\n                    assert((fx>>1) + 16*s->mb_x <= s->width);\n                    assert((fy>>1) + 16*s->mb_y <= s->height);\n                    assert((bx>>1) + 16*s->mb_x >= -16);\n                    assert((by>>1) + 16*s->mb_y >= -16);\n                    assert((bx>>1) + 16*s->mb_x <= s->width);\n                    assert((by>>1) + 16*s->mb_y <= s->height);\n                    c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);\n                    c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);\n                }\n            }\n            d = cmp_func(s, c->temp, src[0], stride, 16);\n        }else\n            d= 256*256*256*32;\n    return d;\n}']