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