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