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

Generated on Fri Sep 16 2011 17:17:39 for FFmpeg by  doxygen 1.7.1