libavcodec/motion_est.c:1819: error: Buffer Overrun L1
  Offset: 8 Size: 4 by call to `ff_epzs_motion_search`.

libavcodec/motion_est.c:1819:12: Call
1817.     }
1818. 
1819.     dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
                 ^
1820.     if(c->sub_flags&FLAG_QPEL)
1821.         dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);

libavcodec/motion_est_template.c:1116:1: Parameter `ref_index`
  1114. 
  1115. //this function is dedicated to the braindamaged gcc
  1116. inline int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,
        ^
  1117.                              int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
  1118.                              int ref_mv_scale, int size, int h)

libavcodec/motion_est_template.c:1123:16: Call
  1121. //FIXME convert other functions in the same way if faster
  1122.     if(c->flags==0 && h==16 && size==0){
  1123.         return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16);
                       ^
  1124. //    case FLAG_QPEL:
  1125. //        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL);

libavcodec/motion_est_template.c:999:1: Parameter `ref_index`
     997.    optimal mv.
     998.  */
     999. static av_always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,
          ^
    1000.                              int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
    1001.                              int ref_mv_scale, int flags, int size, int h)

libavcodec/motion_est_template.c:1105:11: Call
    1103. 
    1104. //check(best[0],best[1],0, b0)
    1105.     dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
                    ^
    1106. 
    1107. //check(best[0],best[1],0, b1)

libavcodec/motion_est_template.c:973:1: Parameter `ref_index`
      971. }
      972. 
      973. static av_always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,
           ^
      974.                                        int src_index, int ref_index, int const penalty_factor,
      975.                                        int size, int h, int flags){

libavcodec/motion_est_template.c:990:18: Call
      988.         return       l2s_dia_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
      989.     else
      990.         return   var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
                            ^
      991. }
      992. 

libavcodec/motion_est_template.c:896:1: Parameter `ref_index`
        894. }
        895. 
        896. static int var_diamond_search(MpegEncContext * s, int *best, int dmin,
             ^
        897.                                        int src_index, int ref_index, int const penalty_factor,
        898.                                        int size, int h, int flags)

libavcodec/motion_est_template.c:921:13: Call
        919. 
        920. //check(x + dir,y + dia_size - dir,0, a0)
        921.             CHECK_MV(x + dir           , y + dia_size - dir);
                         ^
        922.         }
        923. 

libavcodec/motion_est.c:108:1: <Length trace>
106.     against a proposed motion-compensated prediction of that block
107.  */
108. static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
     ^
109.                       const int size, const int h, int ref_index, int src_index,
110.                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){

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

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

libavcodec/motion_est.c:176:50: Array access: Offset: 8 Size: 4 by call to `ff_epzs_motion_search` 
          174. 
          175.                     c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
          176.                     c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
                                                                ^
          177.                 }
          178.             }

static av_always_inline int cmp(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, const int flags){
    MotionEstContext * const c= &s->me;
    const int stride= c->stride;
    const int uvstride= c->uvstride;
    const int qpel= flags&FLAG_QPEL;
    const int chroma= flags&FLAG_CHROMA;
    const int dxy= subx + (suby<<(1+qpel));
    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;
    if(flags&FLAG_DIRECT){
        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;
    }else{
        int uvdxy;
        if(dxy){
            if(qpel){
                c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride);
                if(chroma){
                    int cx= hx/2;
                    int cy= hy/2;
                    cx= (cx>>1)|(cx&1);
                    cy= (cy>>1)|(cy&1);
                    uvdxy= (cx&1) + 2*(cy&1);
                }
            }else{
                c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
                if(chroma)
                    uvdxy= dxy | (x&1) | (2*(y&1));
            }
            d = cmp_func(s, c->temp, src[0], stride, h);
        }else{
            d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
            if(chroma)
                uvdxy= (x&1) + 2*(y&1);
        }
        if(chroma){
            uint8_t * const uvtemp= c->temp + 16*stride;
            c->hpel_put[size+1][uvdxy](uvtemp  , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
            c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
            d += chroma_cmp_func(s, uvtemp  , src[1], uvstride, h>>1);
            d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
        }
    }
#if 0
    if(full_pel){
        const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);
        score_map[index]= d;
    }
    d += (c->mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor;
#endif
    return d;
}
['static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)\n{\n    MotionEstContext * const c= &s->me;\n    int P[10][2];\n    const int mot_stride = s->mb_stride;\n    const int mot_xy = mb_y*mot_stride + mb_x;\n    const int shift= 1+s->quarter_sample;\n    int dmin, i;\n    const int time_pp= s->pp_time;\n    const int time_pb= s->pb_time;\n    int mx, my, xmin, xmax, ymin, ymax;\n    int16_t (*mv_table)[2]= s->b_direct_mv_table;\n    c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;\n    ymin= xmin=(-32)>>shift;\n    ymax= xmax=   31>>shift;\n    if(IS_8X8(s->next_picture.mb_type[mot_xy])){\n        s->mv_type= MV_TYPE_8X8;\n    }else{\n        s->mv_type= MV_TYPE_16X16;\n    }\n    for(i=0; i<4; i++){\n        int index= s->block_index[i];\n        int min, max;\n        c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];\n        c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];\n        c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));\n        c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));\n        max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;\n        min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;\n        max+= 16*mb_x + 1;\n        min+= 16*mb_x - 1;\n        xmax= FFMIN(xmax, s->width - max);\n        xmin= FFMAX(xmin, - 16     - min);\n        max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;\n        min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;\n        max+= 16*mb_y + 1;\n        min+= 16*mb_y - 1;\n        ymax= FFMIN(ymax, s->height - max);\n        ymin= FFMAX(ymin, - 16      - min);\n        if(s->mv_type == MV_TYPE_16X16) break;\n    }\n    assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);\n    if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){\n        s->b_direct_mv_table[mot_xy][0]= 0;\n        s->b_direct_mv_table[mot_xy][1]= 0;\n        return 256*256*256*64;\n    }\n    c->xmin= xmin;\n    c->ymin= ymin;\n    c->xmax= xmax;\n    c->ymax= ymax;\n    c->flags     |= FLAG_DIRECT;\n    c->sub_flags |= FLAG_DIRECT;\n    c->pred_x=0;\n    c->pred_y=0;\n    P_LEFT[0]        = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);\n    P_LEFT[1]        = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);\n    if (!s->first_slice_line) {\n        P_TOP[0]      = av_clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);\n        P_TOP[1]      = av_clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);\n        P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);\n        P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, 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    }\n    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);\n    if(c->sub_flags&FLAG_QPEL)\n        dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);\n    else\n        dmin = hpel_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= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);\n    get_limits(s, 16*mb_x, 16*mb_y);\n    mv_table[mot_xy][0]= mx;\n    mv_table[mot_xy][1]= my;\n    c->flags     &= ~FLAG_DIRECT;\n    c->sub_flags &= ~FLAG_DIRECT;\n    return dmin;\n}', 'inline int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,\n                             int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],\n                             int ref_mv_scale, int size, int h)\n{\n    MotionEstContext * const c= &s->me;\n    if(c->flags==0 && h==16 && size==0){\n        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16);\n    }else{\n        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags, size, h);\n    }\n}', 'static av_always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,\n                             int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],\n                             int ref_mv_scale, int flags, int size, int h)\n{\n    MotionEstContext * const c= &s->me;\n    int best[2]={0, 0};\n    int d;\n    int dmin;\n    int map_generation;\n    int penalty_factor;\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    LOAD_COMMON2\n    if(c->pre_pass){\n        penalty_factor= c->pre_penalty_factor;\n        cmpf= s->dsp.me_pre_cmp[size];\n        chroma_cmpf= s->dsp.me_pre_cmp[size+1];\n    }else{\n        penalty_factor= c->penalty_factor;\n        cmpf= s->dsp.me_cmp[size];\n        chroma_cmpf= s->dsp.me_cmp[size+1];\n    }\n    map_generation= update_map_generation(c);\n    assert(cmpf);\n    dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\n    map[0]= map_generation;\n    score_map[0]= dmin;\n    if((s->pict_type == FF_B_TYPE && !(c->flags & FLAG_DIRECT)) || s->flags&CODEC_FLAG_MV0)\n        dmin += (mv_penalty[pred_x] + mv_penalty[pred_y])*penalty_factor;\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    }else{\n        if(dmin<((h*h*s->avctx->mv0_threshold)>>8)\n                    && ( P_LEFT[0]    |P_LEFT[1]\n                        |P_TOP[0]     |P_TOP[1]\n                        |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){\n            *mx_ptr= 0;\n            *my_ptr= 0;\n            c->skip=1;\n            return dmin;\n        }\n        CHECK_MV(    P_MEDIAN[0] >>shift ,    P_MEDIAN[1] >>shift)\n        CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)  , (P_MEDIAN[1]>>shift)-1)\n        CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)  , (P_MEDIAN[1]>>shift)+1)\n        CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)-1, (P_MEDIAN[1]>>shift)  )\n        CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)+1, (P_MEDIAN[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_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    }\n    if(dmin>h*h*4){\n        if(c->pre_pass){\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->first_slice_line)\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        }else{\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    }\n    if(c->avctx->last_predictor_count){\n        const int count= c->avctx->last_predictor_count;\n        const int xstart= FFMAX(0, s->mb_x - count);\n        const int ystart= FFMAX(0, s->mb_y - count);\n        const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);\n        const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);\n        int mb_y;\n        for(mb_y=ystart; mb_y<yend; mb_y++){\n            int mb_x;\n            for(mb_x=xstart; mb_x<xend; mb_x++){\n                const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;\n                int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;\n                int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;\n                if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;\n                CHECK_MV(mx,my)\n            }\n        }\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 diamond_search(MpegEncContext * s, int *best, int dmin,\n                                       int src_index, int ref_index, int const penalty_factor,\n                                       int size, int h, int flags){\n    MotionEstContext * const c= &s->me;\n    if(c->dia_size==-1)\n        return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n    else if(c->dia_size<-1)\n        return   sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n    else if(c->dia_size<2)\n        return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n    else if(c->dia_size>768)\n        return           umh_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n    else if(c->dia_size>512)\n        return           hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, c->dia_size&0xFF);\n    else if(c->dia_size>256)\n        return       l2s_dia_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n    else\n        return   var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n}', 'static int var_diamond_search(MpegEncContext * s, int *best, int dmin,\n                                       int src_index, int ref_index, int const penalty_factor,\n                                       int size, int h, int flags)\n{\n    MotionEstContext * const c= &s->me;\n    me_cmp_func cmpf, chroma_cmpf;\n    int dia_size;\n    LOAD_COMMON\n    LOAD_COMMON2\n    int map_generation= c->map_generation;\n    cmpf= s->dsp.me_cmp[size];\n    chroma_cmpf= s->dsp.me_cmp[size+1];\n    for(dia_size=1; dia_size<=c->dia_size; dia_size++){\n        int dir, start, end;\n        const int x= best[0];\n        const int y= best[1];\n        start= FFMAX(0, y + dia_size - ymax);\n        end  = FFMIN(dia_size, xmax - x + 1);\n        for(dir= start; dir<end; dir++){\n            int d;\n            CHECK_MV(x + dir           , y + dia_size - dir);\n        }\n        start= FFMAX(0, x + dia_size - xmax);\n        end  = FFMIN(dia_size, y - ymin + 1);\n        for(dir= start; dir<end; dir++){\n            int d;\n            CHECK_MV(x + dia_size - dir, y - dir           );\n        }\n        start= FFMAX(0, -y + dia_size + ymin );\n        end  = FFMIN(dia_size, x - xmin + 1);\n        for(dir= start; dir<end; dir++){\n            int d;\n            CHECK_MV(x - dir           , y - dia_size + dir);\n        }\n        start= FFMAX(0, -x + dia_size + xmin );\n        end  = FFMIN(dia_size, ymax - y + 1);\n        for(dir= start; dir<end; dir++){\n            int d;\n            CHECK_MV(x - dia_size + dir, y + dir           );\n        }\n        if(x!=best[0] || y!=best[1])\n            dia_size=0;\n#if 0\n{\nint dx, dy, i;\nstatic int stats[8*8];\ndx= FFABS(x-best[0]);\ndy= FFABS(y-best[1]);\nstats[dy*8 + dx] ++;\nif(256*256*256*64 % (stats[0]+1)==0){\n    for(i=0; i<64; i++){\n        if((i&7)==0) printf("\\n");\n        printf("%6d ", stats[i]);\n    }\n    printf("\\n");\n}\n}\n#endif\n    }\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    MotionEstContext * const c= &s->me;\n    const int stride= c->stride;\n    const int uvstride= c->uvstride;\n    const int qpel= flags&FLAG_QPEL;\n    const int chroma= flags&FLAG_CHROMA;\n    const int dxy= subx + (suby<<(1+qpel));\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    if(flags&FLAG_DIRECT){\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    }else{\n        int uvdxy;\n        if(dxy){\n            if(qpel){\n                c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride);\n                if(chroma){\n                    int cx= hx/2;\n                    int cy= hy/2;\n                    cx= (cx>>1)|(cx&1);\n                    cy= (cy>>1)|(cy&1);\n                    uvdxy= (cx&1) + 2*(cy&1);\n                }\n            }else{\n                c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);\n                if(chroma)\n                    uvdxy= dxy | (x&1) | (2*(y&1));\n            }\n            d = cmp_func(s, c->temp, src[0], stride, h);\n        }else{\n            d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);\n            if(chroma)\n                uvdxy= (x&1) + 2*(y&1);\n        }\n        if(chroma){\n            uint8_t * const uvtemp= c->temp + 16*stride;\n            c->hpel_put[size+1][uvdxy](uvtemp  , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);\n            c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);\n            d += chroma_cmp_func(s, uvtemp  , src[1], uvstride, h>>1);\n            d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);\n        }\n    }\n#if 0\n    if(full_pel){\n        const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\n        score_map[index]= d;\n    }\n    d += (c->mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor;\n#endif\n    return d;\n}']