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