static int sab_diamond_search(MpegEncContext * s, int *best, int dmin,
                                       int src_index, int ref_index, int const penalty_factor,
                                       int size, int h, int flags)
{
    MotionEstContext * const c= &s->me;
    me_cmp_func cmpf, chroma_cmpf;
    Minima minima[MAX_SAB_SIZE];
    const int minima_count= FFABS(c->dia_size);
    int i, j;
    LOAD_COMMON
    LOAD_COMMON2
    int map_generation= c->map_generation;
    cmpf= s->dsp.me_cmp[size];
    chroma_cmpf= s->dsp.me_cmp[size+1];
    for(j=i=0; i<ME_MAP_SIZE && j<MAX_SAB_SIZE; i++){
        uint32_t key= map[i];
        key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
        if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
        minima[j].height= score_map[i];
        minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
        minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
        minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
        minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
        if(   minima[j].x > xmax || minima[j].x < xmin
           || minima[j].y > ymax || minima[j].y < ymin)
            continue;
        minima[j].checked=0;
        if(minima[j].x || minima[j].y)
            minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
        j++;
    }
    qsort(minima, j, sizeof(Minima), minima_cmp);
    for(; j<minima_count; j++){
        minima[j].height=256*256*256*64;
        minima[j].checked=0;
        minima[j].x= minima[j].y=0;
    }
    for(i=0; i<minima_count; i++){
        const int x= minima[i].x;
        const int y= minima[i].y;
        int d;
        if(minima[i].checked) continue;
        if(   x >= xmax || x <= xmin
           || y >= ymax || y <= ymin)
           continue;
        SAB_CHECK_MV(x-1, y)
        SAB_CHECK_MV(x+1, y)
        SAB_CHECK_MV(x  , y-1)
        SAB_CHECK_MV(x  , y+1)
        minima[i].checked= 1;
    }
    best[0]= minima[0].x;
    best[1]= minima[0].y;
    dmin= minima[0].height;
    if(   best[0] < xmax && best[0] > xmin
       && best[1] < ymax && best[1] > ymin){
        int d;
        CHECK_MV(best[0]-1, best[1])
        CHECK_MV(best[0]+1, best[1])
        CHECK_MV(best[0], best[1]-1)
        CHECK_MV(best[0], best[1]+1)
    }
    return dmin;
}
['static int sab_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    Minima minima[MAX_SAB_SIZE];\n    const int minima_count= FFABS(c->dia_size);\n    int i, j;\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(j=i=0; i<ME_MAP_SIZE && j<MAX_SAB_SIZE; i++){\n        uint32_t key= map[i];\n        key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));\n        if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;\n        minima[j].height= score_map[i];\n        minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;\n        minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);\n        minima[j].x-= (1<<(ME_MAP_MV_BITS-1));\n        minima[j].y-= (1<<(ME_MAP_MV_BITS-1));\n        if(   minima[j].x > xmax || minima[j].x < xmin\n           || minima[j].y > ymax || minima[j].y < ymin)\n            continue;\n        minima[j].checked=0;\n        if(minima[j].x || minima[j].y)\n            minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;\n        j++;\n    }\n    qsort(minima, j, sizeof(Minima), minima_cmp);\n    for(; j<minima_count; j++){\n        minima[j].height=256*256*256*64;\n        minima[j].checked=0;\n        minima[j].x= minima[j].y=0;\n    }\n    for(i=0; i<minima_count; i++){\n        const int x= minima[i].x;\n        const int y= minima[i].y;\n        int d;\n        if(minima[i].checked) continue;\n        if(   x >= xmax || x <= xmin\n           || y >= ymax || y <= ymin)\n           continue;\n        SAB_CHECK_MV(x-1, y)\n        SAB_CHECK_MV(x+1, y)\n        SAB_CHECK_MV(x  , y-1)\n        SAB_CHECK_MV(x  , y+1)\n        minima[i].checked= 1;\n    }\n    best[0]= minima[0].x;\n    best[1]= minima[0].y;\n    dmin= minima[0].height;\n    if(   best[0] < xmax && best[0] > xmin\n       && best[1] < ymax && best[1] > ymin){\n        int d;\n        CHECK_MV(best[0]-1, best[1])\n        CHECK_MV(best[0]+1, best[1])\n        CHECK_MV(best[0], best[1]-1)\n        CHECK_MV(best[0], best[1]+1)\n    }\n    return dmin;\n}']