• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/motion_est_template.c

Go to the documentation of this file.
00001 /*
00002  * Motion estimation
00003  * Copyright (c) 2002-2004 Michael Niedermayer
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 //Let us hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
00028 #define LOAD_COMMON\
00029     uint32_t av_unused * const score_map= c->score_map;\
00030     const int av_unused xmin= c->xmin;\
00031     const int av_unused ymin= c->ymin;\
00032     const int av_unused xmax= c->xmax;\
00033     const int av_unused ymax= c->ymax;\
00034     uint8_t *mv_penalty= c->current_mv_penalty;\
00035     const int pred_x= c->pred_x;\
00036     const int pred_y= c->pred_y;\
00037 
00038 #define CHECK_HALF_MV(dx, dy, x, y)\
00039 {\
00040     const int hx= 2*(x)+(dx);\
00041     const int hy= 2*(y)+(dy);\
00042     d= cmp_hpel(s, x, y, dx, dy, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);\
00043     d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
00044     COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
00045 }
00046 
00047 static int hpel_motion_search(MpegEncContext * s,
00048                                   int *mx_ptr, int *my_ptr, int dmin,
00049                                   int src_index, int ref_index,
00050                                   int size, int h)
00051 {
00052     MotionEstContext * const c= &s->me;
00053     const int mx = *mx_ptr;
00054     const int my = *my_ptr;
00055     const int penalty_factor= c->sub_penalty_factor;
00056     me_cmp_func cmp_sub, chroma_cmp_sub;
00057     int bx=2*mx, by=2*my;
00058 
00059     LOAD_COMMON
00060     int flags= c->sub_flags;
00061 
00062  //FIXME factorize
00063 
00064     cmp_sub= s->dsp.me_sub_cmp[size];
00065     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
00066 
00067     if(c->skip){ //FIXME move out of hpel?
00068         *mx_ptr = 0;
00069         *my_ptr = 0;
00070         return dmin;
00071     }
00072 
00073     if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
00074         dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
00075         if(mx || my || size>0)
00076             dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
00077     }
00078 
00079     if (mx > xmin && mx < xmax &&
00080         my > ymin && my < ymax) {
00081         int d= dmin;
00082         const int index= (my<<ME_MAP_SHIFT) + mx;
00083         const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
00084                      + (mv_penalty[bx   - pred_x] + mv_penalty[by-2 - pred_y])*c->penalty_factor;
00085         const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)]
00086                      + (mv_penalty[bx-2 - pred_x] + mv_penalty[by   - pred_y])*c->penalty_factor;
00087         const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)]
00088                      + (mv_penalty[bx+2 - pred_x] + mv_penalty[by   - pred_y])*c->penalty_factor;
00089         const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
00090                      + (mv_penalty[bx   - pred_x] + mv_penalty[by+2 - pred_y])*c->penalty_factor;
00091 
00092         unsigned key;
00093         unsigned map_generation= c->map_generation;
00094 #ifndef NDEBUG
00095         uint32_t *map= c->map;
00096 #endif
00097         key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
00098         assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
00099         key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
00100         assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
00101         key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
00102         assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
00103         key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
00104         assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
00105         if(t<=b){
00106             CHECK_HALF_MV(0, 1, mx  ,my-1)
00107             if(l<=r){
00108                 CHECK_HALF_MV(1, 1, mx-1, my-1)
00109                 if(t+r<=b+l){
00110                     CHECK_HALF_MV(1, 1, mx  , my-1)
00111                 }else{
00112                     CHECK_HALF_MV(1, 1, mx-1, my  )
00113                 }
00114                 CHECK_HALF_MV(1, 0, mx-1, my  )
00115             }else{
00116                 CHECK_HALF_MV(1, 1, mx  , my-1)
00117                 if(t+l<=b+r){
00118                     CHECK_HALF_MV(1, 1, mx-1, my-1)
00119                 }else{
00120                     CHECK_HALF_MV(1, 1, mx  , my  )
00121                 }
00122                 CHECK_HALF_MV(1, 0, mx  , my  )
00123             }
00124         }else{
00125             if(l<=r){
00126                 if(t+l<=b+r){
00127                     CHECK_HALF_MV(1, 1, mx-1, my-1)
00128                 }else{
00129                     CHECK_HALF_MV(1, 1, mx  , my  )
00130                 }
00131                 CHECK_HALF_MV(1, 0, mx-1, my)
00132                 CHECK_HALF_MV(1, 1, mx-1, my)
00133             }else{
00134                 if(t+r<=b+l){
00135                     CHECK_HALF_MV(1, 1, mx  , my-1)
00136                 }else{
00137                     CHECK_HALF_MV(1, 1, mx-1, my)
00138                 }
00139                 CHECK_HALF_MV(1, 0, mx  , my)
00140                 CHECK_HALF_MV(1, 1, mx  , my)
00141             }
00142             CHECK_HALF_MV(0, 1, mx  , my)
00143         }
00144         assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
00145     }
00146 
00147     *mx_ptr = bx;
00148     *my_ptr = by;
00149 
00150     return dmin;
00151 }
00152 
00153 static int no_sub_motion_search(MpegEncContext * s,
00154           int *mx_ptr, int *my_ptr, int dmin,
00155                                   int src_index, int ref_index,
00156                                   int size, int h)
00157 {
00158     (*mx_ptr)<<=1;
00159     (*my_ptr)<<=1;
00160     return dmin;
00161 }
00162 
00163 inline int ff_get_mb_score(MpegEncContext * s, int mx, int my, int src_index,
00164                                int ref_index, int size, int h, int add_rate)
00165 {
00166 //    const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
00167     MotionEstContext * const c= &s->me;
00168     const int penalty_factor= c->mb_penalty_factor;
00169     const int flags= c->mb_flags;
00170     const int qpel= flags & FLAG_QPEL;
00171     const int mask= 1+2*qpel;
00172     me_cmp_func cmp_sub, chroma_cmp_sub;
00173     int d;
00174 
00175     LOAD_COMMON
00176 
00177  //FIXME factorize
00178 
00179     cmp_sub= s->dsp.mb_cmp[size];
00180     chroma_cmp_sub= s->dsp.mb_cmp[size+1];
00181 
00182 //    assert(!c->skip);
00183 //    assert(c->avctx->me_sub_cmp != c->avctx->mb_cmp);
00184 
00185     d= cmp(s, mx>>(qpel+1), my>>(qpel+1), mx&mask, my&mask, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
00186     //FIXME check cbp before adding penalty for (0,0) vector
00187     if(add_rate && (mx || my || size>0))
00188         d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
00189 
00190     return d;
00191 }
00192 
00193 #define CHECK_QUARTER_MV(dx, dy, x, y)\
00194 {\
00195     const int hx= 4*(x)+(dx);\
00196     const int hy= 4*(y)+(dy);\
00197     d= cmp_qpel(s, x, y, dx, dy, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
00198     d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
00199     COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
00200 }
00201 
00202 static int qpel_motion_search(MpegEncContext * s,
00203                                   int *mx_ptr, int *my_ptr, int dmin,
00204                                   int src_index, int ref_index,
00205                                   int size, int h)
00206 {
00207     MotionEstContext * const c= &s->me;
00208     const int mx = *mx_ptr;
00209     const int my = *my_ptr;
00210     const int penalty_factor= c->sub_penalty_factor;
00211     const unsigned map_generation = c->map_generation;
00212     const int subpel_quality= c->avctx->me_subpel_quality;
00213     uint32_t *map= c->map;
00214     me_cmp_func cmpf, chroma_cmpf;
00215     me_cmp_func cmp_sub, chroma_cmp_sub;
00216 
00217     LOAD_COMMON
00218     int flags= c->sub_flags;
00219 
00220     cmpf= s->dsp.me_cmp[size];
00221     chroma_cmpf= s->dsp.me_cmp[size+1]; //factorize FIXME
00222  //FIXME factorize
00223 
00224     cmp_sub= s->dsp.me_sub_cmp[size];
00225     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
00226 
00227     if(c->skip){ //FIXME somehow move up (benchmark)
00228         *mx_ptr = 0;
00229         *my_ptr = 0;
00230         return dmin;
00231     }
00232 
00233     if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
00234         dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
00235         if(mx || my || size>0)
00236             dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
00237     }
00238 
00239     if (mx > xmin && mx < xmax &&
00240         my > ymin && my < ymax) {
00241         int bx=4*mx, by=4*my;
00242         int d= dmin;
00243         int i, nx, ny;
00244         const int index= (my<<ME_MAP_SHIFT) + mx;
00245         const int t= score_map[(index-(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
00246         const int l= score_map[(index- 1                 )&(ME_MAP_SIZE-1)];
00247         const int r= score_map[(index+ 1                 )&(ME_MAP_SIZE-1)];
00248         const int b= score_map[(index+(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
00249         const int c= score_map[(index                    )&(ME_MAP_SIZE-1)];
00250         int best[8];
00251         int best_pos[8][2];
00252 
00253         memset(best, 64, sizeof(int)*8);
00254         if(s->me.dia_size>=2){
00255             const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
00256             const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
00257             const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
00258             const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
00259 
00260             for(ny= -3; ny <= 3; ny++){
00261                 for(nx= -3; nx <= 3; nx++){
00262                     //FIXME this could overflow (unlikely though)
00263                     const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
00264                     const int64_t c2= nx*nx*( r +  l - 2*c) + 4*nx*( r- l) + 32*c;
00265                     const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
00266                     int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10;
00267                     int i;
00268 
00269                     if((nx&3)==0 && (ny&3)==0) continue;
00270 
00271                     score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
00272 
00273 //                    if(nx&1) score-=1024*c->penalty_factor;
00274 //                    if(ny&1) score-=1024*c->penalty_factor;
00275 
00276                     for(i=0; i<8; i++){
00277                         if(score < best[i]){
00278                             memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
00279                             memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
00280                             best[i]= score;
00281                             best_pos[i][0]= nx + 4*mx;
00282                             best_pos[i][1]= ny + 4*my;
00283                             break;
00284                         }
00285                     }
00286                 }
00287             }
00288         }else{
00289             int tl;
00290             //FIXME this could overflow (unlikely though)
00291             const int cx = 4*(r - l);
00292             const int cx2= r + l - 2*c;
00293             const int cy = 4*(b - t);
00294             const int cy2= b + t - 2*c;
00295             int cxy;
00296 
00297             if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
00298                 tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
00299             }else{
00300                 tl= cmp(s, mx-1, my-1, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);//FIXME wrong if chroma me is different
00301             }
00302 
00303             cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
00304 
00305             assert(16*cx2 + 4*cx + 32*c == 32*r);
00306             assert(16*cx2 - 4*cx + 32*c == 32*l);
00307             assert(16*cy2 + 4*cy + 32*c == 32*b);
00308             assert(16*cy2 - 4*cy + 32*c == 32*t);
00309             assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
00310 
00311             for(ny= -3; ny <= 3; ny++){
00312                 for(nx= -3; nx <= 3; nx++){
00313                     //FIXME this could overflow (unlikely though)
00314                     int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
00315                     int i;
00316 
00317                     if((nx&3)==0 && (ny&3)==0) continue;
00318 
00319                     score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
00320 //                    if(nx&1) score-=32*c->penalty_factor;
00321   //                  if(ny&1) score-=32*c->penalty_factor;
00322 
00323                     for(i=0; i<8; i++){
00324                         if(score < best[i]){
00325                             memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
00326                             memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
00327                             best[i]= score;
00328                             best_pos[i][0]= nx + 4*mx;
00329                             best_pos[i][1]= ny + 4*my;
00330                             break;
00331                         }
00332                     }
00333                 }
00334             }
00335         }
00336         for(i=0; i<subpel_quality; i++){
00337             nx= best_pos[i][0];
00338             ny= best_pos[i][1];
00339             CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
00340         }
00341 
00342         assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
00343 
00344         *mx_ptr = bx;
00345         *my_ptr = by;
00346     }else{
00347         *mx_ptr =4*mx;
00348         *my_ptr =4*my;
00349     }
00350 
00351     return dmin;
00352 }
00353 
00354 
00355 #define CHECK_MV(x,y)\
00356 {\
00357     const unsigned key = ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
00358     const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
00359     assert((x) >= xmin);\
00360     assert((x) <= xmax);\
00361     assert((y) >= ymin);\
00362     assert((y) <= ymax);\
00363 /*printf("check_mv %d %d\n", x, y);*/\
00364     if(map[index]!=key){\
00365         d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
00366         map[index]= key;\
00367         score_map[index]= d;\
00368         d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
00369 /*printf("score:%d\n", d);*/\
00370         COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
00371     }\
00372 }
00373 
00374 #define CHECK_CLIPPED_MV(ax,ay)\
00375 {\
00376     const int Lx= ax;\
00377     const int Ly= ay;\
00378     const int Lx2= FFMAX(xmin, FFMIN(Lx, xmax));\
00379     const int Ly2= FFMAX(ymin, FFMIN(Ly, ymax));\
00380     CHECK_MV(Lx2, Ly2)\
00381 }
00382 
00383 #define CHECK_MV_DIR(x,y,new_dir)\
00384 {\
00385     const unsigned key = ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
00386     const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
00387 /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
00388     if(map[index]!=key){\
00389         d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
00390         map[index]= key;\
00391         score_map[index]= d;\
00392         d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
00393 /*printf("score:%d\n", d);*/\
00394         if(d<dmin){\
00395             best[0]=x;\
00396             best[1]=y;\
00397             dmin=d;\
00398             next_dir= new_dir;\
00399         }\
00400     }\
00401 }
00402 
00403 #define check(x,y,S,v)\
00404 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
00405 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
00406 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
00407 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
00408 
00409 #define LOAD_COMMON2\
00410     uint32_t *map= c->map;\
00411     const int qpel= flags&FLAG_QPEL;\
00412     const int shift= 1+qpel;\
00413 
00414 static av_always_inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
00415                                        int src_index, int ref_index, int const penalty_factor,
00416                                        int size, int h, int flags)
00417 {
00418     MotionEstContext * const c= &s->me;
00419     me_cmp_func cmpf, chroma_cmpf;
00420     int next_dir=-1;
00421     LOAD_COMMON
00422     LOAD_COMMON2
00423     unsigned map_generation = c->map_generation;
00424 
00425     cmpf= s->dsp.me_cmp[size];
00426     chroma_cmpf= s->dsp.me_cmp[size+1];
00427 
00428     { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
00429         const unsigned key = (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
00430         const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
00431         if(map[index]!=key){ //this will be executed only very rarey
00432             score_map[index]= cmp(s, best[0], best[1], 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
00433             map[index]= key;
00434         }
00435     }
00436 
00437     for(;;){
00438         int d;
00439         const int dir= next_dir;
00440         const int x= best[0];
00441         const int y= best[1];
00442         next_dir=-1;
00443 
00444 //printf("%d", dir);
00445         if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y  , 0)
00446         if(dir!=3 && y>ymin) CHECK_MV_DIR(x  , y-1, 1)
00447         if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y  , 2)
00448         if(dir!=1 && y<ymax) CHECK_MV_DIR(x  , y+1, 3)
00449 
00450         if(next_dir==-1){
00451             return dmin;
00452         }
00453     }
00454 }
00455 
00456 static int funny_diamond_search(MpegEncContext * s, int *best, int dmin,
00457                                        int src_index, int ref_index, int const penalty_factor,
00458                                        int size, int h, int flags)
00459 {
00460     MotionEstContext * const c= &s->me;
00461     me_cmp_func cmpf, chroma_cmpf;
00462     int dia_size;
00463     LOAD_COMMON
00464     LOAD_COMMON2
00465     unsigned map_generation = c->map_generation;
00466 
00467     cmpf= s->dsp.me_cmp[size];
00468     chroma_cmpf= s->dsp.me_cmp[size+1];
00469 
00470     for(dia_size=1; dia_size<=4; dia_size++){
00471         int dir;
00472         const int x= best[0];
00473         const int y= best[1];
00474 
00475         if(dia_size&(dia_size-1)) continue;
00476 
00477         if(   x + dia_size > xmax
00478            || x - dia_size < xmin
00479            || y + dia_size > ymax
00480            || y - dia_size < ymin)
00481            continue;
00482 
00483         for(dir= 0; dir<dia_size; dir+=2){
00484             int d;
00485 
00486             CHECK_MV(x + dir           , y + dia_size - dir);
00487             CHECK_MV(x + dia_size - dir, y - dir           );
00488             CHECK_MV(x - dir           , y - dia_size + dir);
00489             CHECK_MV(x - dia_size + dir, y + dir           );
00490         }
00491 
00492         if(x!=best[0] || y!=best[1])
00493             dia_size=0;
00494     }
00495     return dmin;
00496 }
00497 
00498 static int hex_search(MpegEncContext * s, int *best, int dmin,
00499                                        int src_index, int ref_index, int const penalty_factor,
00500                                        int size, int h, int flags, int dia_size)
00501 {
00502     MotionEstContext * const c= &s->me;
00503     me_cmp_func cmpf, chroma_cmpf;
00504     LOAD_COMMON
00505     LOAD_COMMON2
00506     unsigned map_generation = c->map_generation;
00507     int x,y,d;
00508     const int dec= dia_size & (dia_size-1);
00509 
00510     cmpf= s->dsp.me_cmp[size];
00511     chroma_cmpf= s->dsp.me_cmp[size+1];
00512 
00513     for(;dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){
00514         do{
00515             x= best[0];
00516             y= best[1];
00517 
00518             CHECK_CLIPPED_MV(x  -dia_size    , y);
00519             CHECK_CLIPPED_MV(x+  dia_size    , y);
00520             CHECK_CLIPPED_MV(x+( dia_size>>1), y+dia_size);
00521             CHECK_CLIPPED_MV(x+( dia_size>>1), y-dia_size);
00522             if(dia_size>1){
00523                 CHECK_CLIPPED_MV(x+(-dia_size>>1), y+dia_size);
00524                 CHECK_CLIPPED_MV(x+(-dia_size>>1), y-dia_size);
00525             }
00526         }while(best[0] != x || best[1] != y);
00527     }
00528 
00529     return dmin;
00530 }
00531 
00532 static int l2s_dia_search(MpegEncContext * s, int *best, int dmin,
00533                                        int src_index, int ref_index, int const penalty_factor,
00534                                        int size, int h, int flags)
00535 {
00536     MotionEstContext * const c= &s->me;
00537     me_cmp_func cmpf, chroma_cmpf;
00538     LOAD_COMMON
00539     LOAD_COMMON2
00540     unsigned map_generation = c->map_generation;
00541     int x,y,i,d;
00542     int dia_size= c->dia_size&0xFF;
00543     const int dec= dia_size & (dia_size-1);
00544     static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1},
00545                                 { 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};
00546 
00547     cmpf= s->dsp.me_cmp[size];
00548     chroma_cmpf= s->dsp.me_cmp[size+1];
00549 
00550     for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){
00551         do{
00552             x= best[0];
00553             y= best[1];
00554             for(i=0; i<8; i++){
00555                 CHECK_CLIPPED_MV(x+hex[i][0]*dia_size, y+hex[i][1]*dia_size);
00556             }
00557         }while(best[0] != x || best[1] != y);
00558     }
00559 
00560     x= best[0];
00561     y= best[1];
00562     CHECK_CLIPPED_MV(x+1, y);
00563     CHECK_CLIPPED_MV(x, y+1);
00564     CHECK_CLIPPED_MV(x-1, y);
00565     CHECK_CLIPPED_MV(x, y-1);
00566 
00567     return dmin;
00568 }
00569 
00570 static int umh_search(MpegEncContext * s, int *best, int dmin,
00571                                        int src_index, int ref_index, int const penalty_factor,
00572                                        int size, int h, int flags)
00573 {
00574     MotionEstContext * const c= &s->me;
00575     me_cmp_func cmpf, chroma_cmpf;
00576     LOAD_COMMON
00577     LOAD_COMMON2
00578     unsigned map_generation = c->map_generation;
00579     int x,y,x2,y2, i, j, d;
00580     const int dia_size= c->dia_size&0xFE;
00581     static const int hex[16][2]={{-4,-2}, {-4,-1}, {-4, 0}, {-4, 1}, {-4, 2},
00582                                  { 4,-2}, { 4,-1}, { 4, 0}, { 4, 1}, { 4, 2},
00583                                  {-2, 3}, { 0, 4}, { 2, 3},
00584                                  {-2,-3}, { 0,-4}, { 2,-3},};
00585 
00586     cmpf= s->dsp.me_cmp[size];
00587     chroma_cmpf= s->dsp.me_cmp[size+1];
00588 
00589     x= best[0];
00590     y= best[1];
00591     for(x2=FFMAX(x-dia_size+1, xmin); x2<=FFMIN(x+dia_size-1,xmax); x2+=2){
00592         CHECK_MV(x2, y);
00593     }
00594     for(y2=FFMAX(y-dia_size/2+1, ymin); y2<=FFMIN(y+dia_size/2-1,ymax); y2+=2){
00595         CHECK_MV(x, y2);
00596     }
00597 
00598     x= best[0];
00599     y= best[1];
00600     for(y2=FFMAX(y-2, ymin); y2<=FFMIN(y+2,ymax); y2++){
00601         for(x2=FFMAX(x-2, xmin); x2<=FFMIN(x+2,xmax); x2++){
00602             CHECK_MV(x2, y2);
00603         }
00604     }
00605 
00606 //FIXME prevent the CLIP stuff
00607 
00608     for(j=1; j<=dia_size/4; j++){
00609         for(i=0; i<16; i++){
00610             CHECK_CLIPPED_MV(x+hex[i][0]*j, y+hex[i][1]*j);
00611         }
00612     }
00613 
00614     return hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, 2);
00615 }
00616 
00617 static int full_search(MpegEncContext * s, int *best, int dmin,
00618                                        int src_index, int ref_index, int const penalty_factor,
00619                                        int size, int h, int flags)
00620 {
00621     MotionEstContext * const c= &s->me;
00622     me_cmp_func cmpf, chroma_cmpf;
00623     LOAD_COMMON
00624     LOAD_COMMON2
00625     unsigned map_generation = c->map_generation;
00626     int x,y, d;
00627     const int dia_size= c->dia_size&0xFF;
00628 
00629     cmpf= s->dsp.me_cmp[size];
00630     chroma_cmpf= s->dsp.me_cmp[size+1];
00631 
00632     for(y=FFMAX(-dia_size, ymin); y<=FFMIN(dia_size,ymax); y++){
00633         for(x=FFMAX(-dia_size, xmin); x<=FFMIN(dia_size,xmax); x++){
00634             CHECK_MV(x, y);
00635         }
00636     }
00637 
00638     x= best[0];
00639     y= best[1];
00640     d= dmin;
00641     CHECK_CLIPPED_MV(x  , y);
00642     CHECK_CLIPPED_MV(x+1, y);
00643     CHECK_CLIPPED_MV(x, y+1);
00644     CHECK_CLIPPED_MV(x-1, y);
00645     CHECK_CLIPPED_MV(x, y-1);
00646     best[0]= x;
00647     best[1]= y;
00648 
00649     return d;
00650 }
00651 
00652 #define SAB_CHECK_MV(ax,ay)\
00653 {\
00654     const unsigned key = ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
00655     const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
00656 /*printf("sab check %d %d\n", ax, ay);*/\
00657     if(map[index]!=key){\
00658         d= cmp(s, ax, ay, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
00659         map[index]= key;\
00660         score_map[index]= d;\
00661         d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
00662 /*printf("score: %d\n", d);*/\
00663         if(d < minima[minima_count-1].height){\
00664             int j=0;\
00665             \
00666             while(d >= minima[j].height) j++;\
00667 \
00668             memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
00669 \
00670             minima[j].checked= 0;\
00671             minima[j].height= d;\
00672             minima[j].x= ax;\
00673             minima[j].y= ay;\
00674             \
00675             i=-1;\
00676             continue;\
00677         }\
00678     }\
00679 }
00680 
00681 #define MAX_SAB_SIZE ME_MAP_SIZE
00682 static int sab_diamond_search(MpegEncContext * s, int *best, int dmin,
00683                                        int src_index, int ref_index, int const penalty_factor,
00684                                        int size, int h, int flags)
00685 {
00686     MotionEstContext * const c= &s->me;
00687     me_cmp_func cmpf, chroma_cmpf;
00688     Minima minima[MAX_SAB_SIZE];
00689     const int minima_count= FFABS(c->dia_size);
00690     int i, j;
00691     LOAD_COMMON
00692     LOAD_COMMON2
00693     unsigned map_generation = c->map_generation;
00694 
00695     cmpf= s->dsp.me_cmp[size];
00696     chroma_cmpf= s->dsp.me_cmp[size+1];
00697 
00698     /*Note j<MAX_SAB_SIZE is needed if MAX_SAB_SIZE < ME_MAP_SIZE as j can
00699       become larger due to MVs overflowing their ME_MAP_MV_BITS bits space in map
00700      */
00701     for(j=i=0; i<ME_MAP_SIZE && j<MAX_SAB_SIZE; i++){
00702         uint32_t key= map[i];
00703 
00704         key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
00705 
00706         if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
00707 
00708         minima[j].height= score_map[i];
00709         minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
00710         minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
00711         minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
00712         minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
00713 
00714         // all entries in map should be in range except if the mv overflows their ME_MAP_MV_BITS bits space
00715         if(   minima[j].x > xmax || minima[j].x < xmin
00716            || minima[j].y > ymax || minima[j].y < ymin)
00717             continue;
00718 
00719         minima[j].checked=0;
00720         if(minima[j].x || minima[j].y)
00721             minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
00722 
00723         j++;
00724     }
00725 
00726     qsort(minima, j, sizeof(Minima), minima_cmp);
00727 
00728     for(; j<minima_count; j++){
00729         minima[j].height=256*256*256*64;
00730         minima[j].checked=0;
00731         minima[j].x= minima[j].y=0;
00732     }
00733 
00734     for(i=0; i<minima_count; i++){
00735         const int x= minima[i].x;
00736         const int y= minima[i].y;
00737         int d;
00738 
00739         if(minima[i].checked) continue;
00740 
00741         if(   x >= xmax || x <= xmin
00742            || y >= ymax || y <= ymin)
00743            continue;
00744 
00745         SAB_CHECK_MV(x-1, y)
00746         SAB_CHECK_MV(x+1, y)
00747         SAB_CHECK_MV(x  , y-1)
00748         SAB_CHECK_MV(x  , y+1)
00749 
00750         minima[i].checked= 1;
00751     }
00752 
00753     best[0]= minima[0].x;
00754     best[1]= minima[0].y;
00755     dmin= minima[0].height;
00756 
00757     if(   best[0] < xmax && best[0] > xmin
00758        && best[1] < ymax && best[1] > ymin){
00759         int d;
00760         //ensure that the refernece samples for hpel refinement are in the map
00761         CHECK_MV(best[0]-1, best[1])
00762         CHECK_MV(best[0]+1, best[1])
00763         CHECK_MV(best[0], best[1]-1)
00764         CHECK_MV(best[0], best[1]+1)
00765     }
00766     return dmin;
00767 }
00768 
00769 static int var_diamond_search(MpegEncContext * s, int *best, int dmin,
00770                                        int src_index, int ref_index, int const penalty_factor,
00771                                        int size, int h, int flags)
00772 {
00773     MotionEstContext * const c= &s->me;
00774     me_cmp_func cmpf, chroma_cmpf;
00775     int dia_size;
00776     LOAD_COMMON
00777     LOAD_COMMON2
00778     unsigned map_generation = c->map_generation;
00779 
00780     cmpf= s->dsp.me_cmp[size];
00781     chroma_cmpf= s->dsp.me_cmp[size+1];
00782 
00783     for(dia_size=1; dia_size<=c->dia_size; dia_size++){
00784         int dir, start, end;
00785         const int x= best[0];
00786         const int y= best[1];
00787 
00788         start= FFMAX(0, y + dia_size - ymax);
00789         end  = FFMIN(dia_size, xmax - x + 1);
00790         for(dir= start; dir<end; dir++){
00791             int d;
00792 
00793 //check(x + dir,y + dia_size - dir,0, a0)
00794             CHECK_MV(x + dir           , y + dia_size - dir);
00795         }
00796 
00797         start= FFMAX(0, x + dia_size - xmax);
00798         end  = FFMIN(dia_size, y - ymin + 1);
00799         for(dir= start; dir<end; dir++){
00800             int d;
00801 
00802 //check(x + dia_size - dir, y - dir,0, a1)
00803             CHECK_MV(x + dia_size - dir, y - dir           );
00804         }
00805 
00806         start= FFMAX(0, -y + dia_size + ymin );
00807         end  = FFMIN(dia_size, x - xmin + 1);
00808         for(dir= start; dir<end; dir++){
00809             int d;
00810 
00811 //check(x - dir,y - dia_size + dir,0, a2)
00812             CHECK_MV(x - dir           , y - dia_size + dir);
00813         }
00814 
00815         start= FFMAX(0, -x + dia_size + xmin );
00816         end  = FFMIN(dia_size, ymax - y + 1);
00817         for(dir= start; dir<end; dir++){
00818             int d;
00819 
00820 //check(x - dia_size + dir, y + dir,0, a3)
00821             CHECK_MV(x - dia_size + dir, y + dir           );
00822         }
00823 
00824         if(x!=best[0] || y!=best[1])
00825             dia_size=0;
00826     }
00827     return dmin;
00828 }
00829 
00830 static av_always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,
00831                                        int src_index, int ref_index, int const penalty_factor,
00832                                        int size, int h, int flags){
00833     MotionEstContext * const c= &s->me;
00834     if(c->dia_size==-1)
00835         return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
00836     else if(c->dia_size<-1)
00837         return   sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
00838     else if(c->dia_size<2)
00839         return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
00840     else if(c->dia_size>1024)
00841         return          full_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
00842     else if(c->dia_size>768)
00843         return           umh_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
00844     else if(c->dia_size>512)
00845         return           hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, c->dia_size&0xFF);
00846     else if(c->dia_size>256)
00847         return       l2s_dia_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
00848     else
00849         return   var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
00850 }
00851 
00858 static av_always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,
00859                              int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
00860                              int ref_mv_scale, int flags, int size, int h)
00861 {
00862     MotionEstContext * const c= &s->me;
00863     int best[2]={0, 0};      
00867     int d;                   
00868     int dmin;                
00870     unsigned map_generation;
00871     int penalty_factor;
00872     const int ref_mv_stride= s->mb_stride; //pass as arg  FIXME
00873     const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME
00874     me_cmp_func cmpf, chroma_cmpf;
00875 
00876     LOAD_COMMON
00877     LOAD_COMMON2
00878 
00879     if(c->pre_pass){
00880         penalty_factor= c->pre_penalty_factor;
00881         cmpf= s->dsp.me_pre_cmp[size];
00882         chroma_cmpf= s->dsp.me_pre_cmp[size+1];
00883     }else{
00884         penalty_factor= c->penalty_factor;
00885         cmpf= s->dsp.me_cmp[size];
00886         chroma_cmpf= s->dsp.me_cmp[size+1];
00887     }
00888 
00889     map_generation= update_map_generation(c);
00890 
00891     assert(cmpf);
00892     dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
00893     map[0]= map_generation;
00894     score_map[0]= dmin;
00895 
00896     //FIXME precalc first term below?
00897     if((s->pict_type == AV_PICTURE_TYPE_B && !(c->flags & FLAG_DIRECT)) || s->flags&CODEC_FLAG_MV0)
00898         dmin += (mv_penalty[pred_x] + mv_penalty[pred_y])*penalty_factor;
00899 
00900     /* first line */
00901     if (s->first_slice_line) {
00902         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
00903         CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
00904                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
00905     }else{
00906         if(dmin<((h*h*s->avctx->mv0_threshold)>>8)
00907                     && ( P_LEFT[0]    |P_LEFT[1]
00908                         |P_TOP[0]     |P_TOP[1]
00909                         |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
00910             *mx_ptr= 0;
00911             *my_ptr= 0;
00912             c->skip=1;
00913             return dmin;
00914         }
00915         CHECK_MV(    P_MEDIAN[0] >>shift ,    P_MEDIAN[1] >>shift)
00916         CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)  , (P_MEDIAN[1]>>shift)-1)
00917         CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)  , (P_MEDIAN[1]>>shift)+1)
00918         CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)-1, (P_MEDIAN[1]>>shift)  )
00919         CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)+1, (P_MEDIAN[1]>>shift)  )
00920         CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
00921                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
00922         CHECK_MV(P_LEFT[0]    >>shift, P_LEFT[1]    >>shift)
00923         CHECK_MV(P_TOP[0]     >>shift, P_TOP[1]     >>shift)
00924         CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
00925     }
00926     if(dmin>h*h*4){
00927         if(c->pre_pass){
00928             CHECK_CLIPPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,
00929                             (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
00930             if(!s->first_slice_line)
00931                 CHECK_CLIPPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
00932                                 (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
00933         }else{
00934             CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
00935                             (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
00936             if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line
00937                 CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
00938                                 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
00939         }
00940     }
00941 
00942     if(c->avctx->last_predictor_count){
00943         const int count= c->avctx->last_predictor_count;
00944         const int xstart= FFMAX(0, s->mb_x - count);
00945         const int ystart= FFMAX(0, s->mb_y - count);
00946         const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
00947         const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
00948         int mb_y;
00949 
00950         for(mb_y=ystart; mb_y<yend; mb_y++){
00951             int mb_x;
00952             for(mb_x=xstart; mb_x<xend; mb_x++){
00953                 const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
00954                 int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
00955                 int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
00956 
00957                 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
00958                 CHECK_MV(mx,my)
00959             }
00960         }
00961     }
00962 
00963 //check(best[0],best[1],0, b0)
00964     dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
00965 
00966 //check(best[0],best[1],0, b1)
00967     *mx_ptr= best[0];
00968     *my_ptr= best[1];
00969 
00970 //    printf("%d %d %d \n", best[0], best[1], dmin);
00971     return dmin;
00972 }
00973 
00974 //this function is dedicated to the braindamaged gcc
00975 inline int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,
00976                              int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
00977                              int ref_mv_scale, int size, int h)
00978 {
00979     MotionEstContext * const c= &s->me;
00980 //FIXME convert other functions in the same way if faster
00981     if(c->flags==0 && h==16 && size==0){
00982         return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16);
00983 //    case FLAG_QPEL:
00984 //        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL);
00985     }else{
00986         return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags, size, h);
00987     }
00988 }
00989 
00990 static int epzs_motion_search4(MpegEncContext * s,
00991                              int *mx_ptr, int *my_ptr, int P[10][2],
00992                              int src_index, int ref_index, int16_t (*last_mv)[2],
00993                              int ref_mv_scale)
00994 {
00995     MotionEstContext * const c= &s->me;
00996     int best[2]={0, 0};
00997     int d, dmin;
00998     unsigned map_generation;
00999     const int penalty_factor= c->penalty_factor;
01000     const int size=1;
01001     const int h=8;
01002     const int ref_mv_stride= s->mb_stride;
01003     const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
01004     me_cmp_func cmpf, chroma_cmpf;
01005     LOAD_COMMON
01006     int flags= c->flags;
01007     LOAD_COMMON2
01008 
01009     cmpf= s->dsp.me_cmp[size];
01010     chroma_cmpf= s->dsp.me_cmp[size+1];
01011 
01012     map_generation= update_map_generation(c);
01013 
01014     dmin = 1000000;
01015 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
01016     /* first line */
01017     if (s->first_slice_line) {
01018         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
01019         CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
01020                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
01021         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
01022     }else{
01023         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
01024         //FIXME try some early stop
01025         CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
01026         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
01027         CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
01028         CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
01029         CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
01030                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
01031     }
01032     if(dmin>64*4){
01033         CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
01034                         (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
01035         if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line
01036             CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
01037                             (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
01038     }
01039 
01040     dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
01041 
01042     *mx_ptr= best[0];
01043     *my_ptr= best[1];
01044 
01045 //    printf("%d %d %d \n", best[0], best[1], dmin);
01046     return dmin;
01047 }
01048 
01049 //try to merge with above FIXME (needs PSNR test)
01050 static int epzs_motion_search2(MpegEncContext * s,
01051                              int *mx_ptr, int *my_ptr, int P[10][2],
01052                              int src_index, int ref_index, int16_t (*last_mv)[2],
01053                              int ref_mv_scale)
01054 {
01055     MotionEstContext * const c= &s->me;
01056     int best[2]={0, 0};
01057     int d, dmin;
01058     unsigned map_generation;
01059     const int penalty_factor= c->penalty_factor;
01060     const int size=0; //FIXME pass as arg
01061     const int h=8;
01062     const int ref_mv_stride= s->mb_stride;
01063     const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
01064     me_cmp_func cmpf, chroma_cmpf;
01065     LOAD_COMMON
01066     int flags= c->flags;
01067     LOAD_COMMON2
01068 
01069     cmpf= s->dsp.me_cmp[size];
01070     chroma_cmpf= s->dsp.me_cmp[size+1];
01071 
01072     map_generation= update_map_generation(c);
01073 
01074     dmin = 1000000;
01075 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
01076     /* first line */
01077     if (s->first_slice_line) {
01078         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
01079         CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
01080                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
01081         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
01082     }else{
01083         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
01084         //FIXME try some early stop
01085         CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
01086         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
01087         CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
01088         CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
01089         CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
01090                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
01091     }
01092     if(dmin>64*4){
01093         CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
01094                         (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
01095         if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line
01096             CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
01097                             (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
01098     }
01099 
01100     dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
01101 
01102     *mx_ptr= best[0];
01103     *my_ptr= best[1];
01104 
01105 //    printf("%d %d %d \n", best[0], best[1], dmin);
01106     return dmin;
01107 }
Generated on Thu Jul 11 2013 15:38:20 for Libav by doxygen 1.7.1