3 * Copyright (c) 2002 Michael Niedermayer
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
22 //Note, the last line is there to kill these ugly unused var warnings
23 #define LOAD_COMMON(x, y)\
24 uint32_t * const score_map= s->me.score_map;\
25 const int stride= s->linesize;\
26 const int uvstride= s->uvlinesize;\
27 const int time_pp= s->pp_time;\
28 const int time_pb= s->pb_time;\
29 uint8_t * const src_y= s->new_picture.data[0] + ((y) * stride) + (x);\
30 uint8_t * const src_u= s->new_picture.data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
31 uint8_t * const src_v= s->new_picture.data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
32 uint8_t * const ref_y= ref_picture->data[0] + ((y) * stride) + (x);\
33 uint8_t * const ref_u= ref_picture->data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
34 uint8_t * const ref_v= ref_picture->data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
35 uint8_t * const ref2_y= s->next_picture.data[0] + ((y) * stride) + (x);\
36 op_pixels_func (*hpel_put)[4];\
37 op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\
38 op_pixels_func (*chroma_hpel_put)[4];\
39 qpel_mc_func (*qpel_put)[16];\
40 qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\
41 const __attribute__((unused)) int unu= time_pp + time_pb + (int)src_u + (int)src_v + (int)ref_u + (int)ref_v\
42 + (int)ref2_y + (int)hpel_avg + (int)qpel_avg + (int)score_map;\
43 if(s->no_rounding /*FIXME b_type*/){\
44 hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\
45 chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\
46 qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];\
48 hpel_put=& s->dsp.put_pixels_tab[size];\
49 chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];\
50 qpel_put= &s->dsp.put_qpel_pixels_tab[size];\
56 #define CHECK_HALF_MV(dx, dy, x, y)\
58 const int hx= 2*(x)+(dx);\
59 const int hy= 2*(y)+(dy);\
60 CMP_HPEL(d, dx, dy, x, y, size);\
61 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
62 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
66 static int RENAME(hpel_motion_search)(MpegEncContext * s,
67 int *mx_ptr, int *my_ptr, int dmin,
68 int xmin, int ymin, int xmax, int ymax,
69 int pred_x, int pred_y, Picture *ref_picture,
70 int n, int size, uint16_t * const mv_penalty)
72 const int xx = 16 * s->mb_x + 8*(n&1);
73 const int yy = 16 * s->mb_y + 8*(n>>1);
74 const int mx = *mx_ptr;
75 const int my = *my_ptr;
76 const int penalty_factor= s->me.sub_penalty_factor;
82 me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
84 if(s->no_rounding /*FIXME b_type*/){
85 hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
86 chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
88 hpel_put=& s->dsp.put_pixels_tab[size];
89 chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
91 cmp= s->dsp.me_cmp[size];
92 chroma_cmp= s->dsp.me_cmp[size+1];
93 cmp_sub= s->dsp.me_sub_cmp[size];
94 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
96 if(s->me.skip){ //FIXME somehow move up (benchmark)
102 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
103 CMP_HPEL(dmin, 0, 0, mx, my, size);
105 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
108 if (mx > xmin && mx < xmax &&
109 my > ymin && my < ymax) {
110 int bx=2*mx, by=2*my;
113 CHECK_HALF_MV(1, 1, mx-1, my-1)
114 CHECK_HALF_MV(0, 1, mx , my-1)
115 CHECK_HALF_MV(1, 1, mx , my-1)
116 CHECK_HALF_MV(1, 0, mx-1, my )
117 CHECK_HALF_MV(1, 0, mx , my )
118 CHECK_HALF_MV(1, 1, mx-1, my )
119 CHECK_HALF_MV(0, 1, mx , my )
120 CHECK_HALF_MV(1, 1, mx , my )
122 assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
135 static int RENAME(hpel_motion_search)(MpegEncContext * s,
136 int *mx_ptr, int *my_ptr, int dmin,
137 int xmin, int ymin, int xmax, int ymax,
138 int pred_x, int pred_y, Picture *ref_picture,
139 int n, int size, uint16_t * const mv_penalty)
141 const int xx = 16 * s->mb_x + 8*(n&1);
142 const int yy = 16 * s->mb_y + 8*(n>>1);
143 const int mx = *mx_ptr;
144 const int my = *my_ptr;
145 const int penalty_factor= s->me.sub_penalty_factor;
146 me_cmp_func cmp_sub, chroma_cmp_sub;
147 int bx=2*mx, by=2*my;
153 cmp_sub= s->dsp.me_sub_cmp[size];
154 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
156 if(s->me.skip){ //FIXME move out of hpel?
162 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
163 CMP_HPEL(dmin, 0, 0, mx, my, size);
164 if(mx || my || size>0)
165 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
168 if (mx > xmin && mx < xmax &&
169 my > ymin && my < ymax) {
171 const int index= (my<<ME_MAP_SHIFT) + mx;
172 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
173 + (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*s->me.penalty_factor;
174 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]
175 + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor;
176 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]
177 + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor;
178 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
179 + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*s->me.penalty_factor;
183 int map_generation= s->me.map_generation;
184 uint32_t *map= s->me.map;
185 key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
186 assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
187 key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
188 assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
189 key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
190 assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
191 key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
192 assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
195 CHECK_HALF_MV(0, 1, mx ,my-1)
197 CHECK_HALF_MV(1, 1, mx-1, my-1)
199 CHECK_HALF_MV(1, 1, mx , my-1)
201 CHECK_HALF_MV(1, 1, mx-1, my )
203 CHECK_HALF_MV(1, 0, mx-1, my )
205 CHECK_HALF_MV(1, 1, mx , my-1)
207 CHECK_HALF_MV(1, 1, mx-1, my-1)
209 CHECK_HALF_MV(1, 1, mx , my )
211 CHECK_HALF_MV(1, 0, mx , my )
216 CHECK_HALF_MV(1, 1, mx-1, my-1)
218 CHECK_HALF_MV(1, 1, mx , my )
220 CHECK_HALF_MV(1, 0, mx-1, my)
221 CHECK_HALF_MV(1, 1, mx-1, my)
224 CHECK_HALF_MV(1, 1, mx , my-1)
226 CHECK_HALF_MV(1, 1, mx-1, my)
228 CHECK_HALF_MV(1, 0, mx , my)
229 CHECK_HALF_MV(1, 1, mx , my)
231 CHECK_HALF_MV(0, 1, mx , my)
233 assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
243 static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture,
244 uint16_t * const mv_penalty)
246 // const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
248 const int xx = 16 * s->mb_x;
249 const int yy = 16 * s->mb_y;
250 const int penalty_factor= s->me.mb_penalty_factor;
251 const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these
252 const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit
253 me_cmp_func cmp_sub, chroma_cmp_sub;
260 cmp_sub= s->dsp.mb_cmp[size];
261 chroma_cmp_sub= s->dsp.mb_cmp[size+1];
264 assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
266 CMP_HPEL(d, mx&1, my&1, mx>>1, my>>1, size);
267 //FIXME check cbp before adding penalty for (0,0) vector
268 if(mx || my || size>0)
269 d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
274 #endif /* CMP_HPEL */
280 #define CHECK_QUARTER_MV(dx, dy, x, y)\
282 const int hx= 4*(x)+(dx);\
283 const int hy= 4*(y)+(dy);\
284 CMP_QPEL(d, dx, dy, x, y, size);\
285 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
286 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
289 static int RENAME(qpel_motion_search)(MpegEncContext * s,
290 int *mx_ptr, int *my_ptr, int dmin,
291 int xmin, int ymin, int xmax, int ymax,
292 int pred_x, int pred_y, Picture *ref_picture,
293 int n, int size, uint16_t * const mv_penalty)
295 const int xx = 16 * s->mb_x + 8*(n&1);
296 const int yy = 16 * s->mb_y + 8*(n>>1);
297 const int mx = *mx_ptr;
298 const int my = *my_ptr;
299 const int penalty_factor= s->me.sub_penalty_factor;
300 const int map_generation= s->me.map_generation;
301 const int subpel_quality= s->avctx->me_subpel_quality;
302 uint32_t *map= s->me.map;
303 me_cmp_func cmp, chroma_cmp;
304 me_cmp_func cmp_sub, chroma_cmp_sub;
308 cmp= s->dsp.me_cmp[size];
309 chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
312 cmp_sub= s->dsp.me_sub_cmp[size];
313 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
315 if(s->me.skip){ //FIXME somehow move up (benchmark)
321 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
322 CMP_QPEL(dmin, 0, 0, mx, my, size);
323 if(mx || my || size>0)
324 dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
327 if (mx > xmin && mx < xmax &&
328 my > ymin && my < ymax) {
329 int bx=4*mx, by=4*my;
332 const int index= (my<<ME_MAP_SHIFT) + mx;
333 const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
334 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
335 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
336 const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
337 const int c= score_map[(index )&(ME_MAP_SIZE-1)];
341 memset(best, 64, sizeof(int)*8);
343 if(s->me.dia_size>=2){
344 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
345 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
346 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
347 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
349 for(ny= -3; ny <= 3; ny++){
350 for(nx= -3; nx <= 3; nx++){
351 const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
352 const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
353 const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
354 int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
357 if((nx&3)==0 && (ny&3)==0) continue;
359 score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
361 // if(nx&1) score-=1024*s->me.penalty_factor;
362 // if(ny&1) score-=1024*s->me.penalty_factor;
366 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
367 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
369 best_pos[i][0]= nx + 4*mx;
370 best_pos[i][1]= ny + 4*my;
378 const int cx = 4*(r - l);
379 const int cx2= r + l - 2*c;
380 const int cy = 4*(b - t);
381 const int cy2= b + t - 2*c;
384 if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
385 tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
387 CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
390 cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
392 assert(16*cx2 + 4*cx + 32*c == 32*r);
393 assert(16*cx2 - 4*cx + 32*c == 32*l);
394 assert(16*cy2 + 4*cy + 32*c == 32*b);
395 assert(16*cy2 - 4*cy + 32*c == 32*t);
396 assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
398 for(ny= -3; ny <= 3; ny++){
399 for(nx= -3; nx <= 3; nx++){
400 int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
403 if((nx&3)==0 && (ny&3)==0) continue;
405 score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
406 // if(nx&1) score-=32*s->me.penalty_factor;
407 // if(ny&1) score-=32*s->me.penalty_factor;
411 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
412 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
414 best_pos[i][0]= nx + 4*mx;
415 best_pos[i][1]= ny + 4*my;
422 for(i=0; i<subpel_quality; i++){
425 CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
429 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
430 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
431 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
432 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
433 // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
436 // nx= FFMAX(4*mx - bx, bx - 4*mx);
437 // ny= FFMAX(4*my - by, by - 4*my);
439 static int stats[7][7], count;
441 stats[4*mx - bx + 3][4*my - by + 3]++;
442 if(256*256*256*64 % count ==0){
444 if((i%7)==0) printf("\n");
445 printf("%6d ", stats[0][i]);
453 CHECK_QUARTER_MV(2, 2, mx-1, my-1)
454 CHECK_QUARTER_MV(0, 2, mx , my-1)
455 CHECK_QUARTER_MV(2, 2, mx , my-1)
456 CHECK_QUARTER_MV(2, 0, mx , my )
457 CHECK_QUARTER_MV(2, 2, mx , my )
458 CHECK_QUARTER_MV(0, 2, mx , my )
459 CHECK_QUARTER_MV(2, 2, mx-1, my )
460 CHECK_QUARTER_MV(2, 0, mx-1, my )
466 int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
467 int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
468 CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
473 CHECK_QUARTER_MV(1, 3, mx-1, my-1)
474 CHECK_QUARTER_MV(1, 2, mx-1, my-1)
475 CHECK_QUARTER_MV(1, 1, mx-1, my-1)
476 CHECK_QUARTER_MV(2, 1, mx-1, my-1)
477 CHECK_QUARTER_MV(3, 1, mx-1, my-1)
478 CHECK_QUARTER_MV(0, 1, mx , my-1)
479 CHECK_QUARTER_MV(1, 1, mx , my-1)
480 CHECK_QUARTER_MV(2, 1, mx , my-1)
481 CHECK_QUARTER_MV(3, 1, mx , my-1)
482 CHECK_QUARTER_MV(3, 2, mx , my-1)
483 CHECK_QUARTER_MV(3, 3, mx , my-1)
484 CHECK_QUARTER_MV(3, 0, mx , my )
485 CHECK_QUARTER_MV(3, 1, mx , my )
486 CHECK_QUARTER_MV(3, 2, mx , my )
487 CHECK_QUARTER_MV(3, 3, mx , my )
488 CHECK_QUARTER_MV(2, 3, mx , my )
489 CHECK_QUARTER_MV(1, 3, mx , my )
490 CHECK_QUARTER_MV(0, 3, mx , my )
491 CHECK_QUARTER_MV(3, 3, mx-1, my )
492 CHECK_QUARTER_MV(2, 3, mx-1, my )
493 CHECK_QUARTER_MV(1, 3, mx-1, my )
494 CHECK_QUARTER_MV(1, 2, mx-1, my )
495 CHECK_QUARTER_MV(1, 1, mx-1, my )
496 CHECK_QUARTER_MV(1, 0, mx-1, my )
498 assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
510 static int RENAME(qpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture,
511 uint16_t * const mv_penalty)
514 const int xx = 16 * s->mb_x;
515 const int yy = 16 * s->mb_y;
516 const int penalty_factor= s->me.mb_penalty_factor;
517 const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these
518 const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit
519 me_cmp_func cmp_sub, chroma_cmp_sub;
526 cmp_sub= s->dsp.mb_cmp[size];
527 chroma_cmp_sub= s->dsp.mb_cmp[size+1];
530 assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
532 CMP_QPEL(d, mx&3, my&3, mx>>2, my>>2, size);
533 //FIXME check cbp before adding penalty for (0,0) vector
534 if(mx || my || size>0)
535 d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
541 #endif /* CMP_QPEL */
543 #define CHECK_MV(x,y)\
545 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
546 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
547 /*printf("check_mv %d %d\n", x, y);*/\
548 if(map[index]!=key){\
551 score_map[index]= d;\
552 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
553 /*printf("score:%d\n", d);*/\
554 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
558 #define CHECK_CLIPED_MV(ax,ay)\
560 const int x= FFMAX(xmin, FFMIN(ax, xmax));\
561 const int y= FFMAX(ymin, FFMIN(ay, ymax));\
565 #define CHECK_MV_DIR(x,y,new_dir)\
567 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
568 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
569 /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
570 if(map[index]!=key){\
573 score_map[index]= d;\
574 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
575 /*printf("score:%d\n", d);*/\
585 #define check(x,y,S,v)\
586 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
587 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
588 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
589 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
592 static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
593 Picture *ref_picture,
594 int const pred_x, int const pred_y, int const penalty_factor,
595 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
596 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
599 me_cmp_func cmp, chroma_cmp;
601 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
603 cmp= s->dsp.me_cmp[size];
604 chroma_cmp= s->dsp.me_cmp[size+1];
606 { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
607 const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
608 const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
609 if(map[index]!=key){ //this will be executed only very rarey
610 CMP(score_map[index], best[0], best[1], size);
617 const int dir= next_dir;
618 const int x= best[0];
619 const int y= best[1];
623 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
624 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
625 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
626 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
634 static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
635 Picture *ref_picture,
636 int const pred_x, int const pred_y, int const penalty_factor,
637 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
638 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
641 me_cmp_func cmp, chroma_cmp;
643 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
645 cmp= s->dsp.me_cmp[size];
646 chroma_cmp= s->dsp.me_cmp[size+1];
648 for(dia_size=1; dia_size<=4; dia_size++){
650 const int x= best[0];
651 const int y= best[1];
653 if(dia_size&(dia_size-1)) continue;
655 if( x + dia_size > xmax
656 || x - dia_size < xmin
657 || y + dia_size > ymax
658 || y - dia_size < ymin)
661 for(dir= 0; dir<dia_size; dir+=2){
664 CHECK_MV(x + dir , y + dia_size - dir);
665 CHECK_MV(x + dia_size - dir, y - dir );
666 CHECK_MV(x - dir , y - dia_size + dir);
667 CHECK_MV(x - dia_size + dir, y + dir );
670 if(x!=best[0] || y!=best[1])
675 static int stats[8*8];
679 dx^=dy; dy^=dx; dx^=dy;
682 if(256*256*256*64 % (stats[0]+1)==0){
684 if((i&7)==0) printf("\n");
685 printf("%8d ", stats[i]);
695 #define SAB_CHECK_MV(ax,ay)\
697 const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
698 const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
699 /*printf("sab check %d %d\n", ax, ay);*/\
700 if(map[index]!=key){\
701 CMP(d, ax, ay, size);\
703 score_map[index]= d;\
704 d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
705 /*printf("score: %d\n", d);*/\
706 if(d < minima[minima_count-1].height){\
709 while(d >= minima[j].height) j++;\
711 memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
713 minima[j].checked= 0;\
714 minima[j].height= d;\
724 #define MAX_SAB_SIZE 16
725 static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
726 Picture *ref_picture,
727 int const pred_x, int const pred_y, int const penalty_factor,
728 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
729 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
732 me_cmp_func cmp, chroma_cmp;
733 Minima minima[MAX_SAB_SIZE];
734 const int minima_count= ABS(s->me.dia_size);
736 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
738 cmp= s->dsp.me_cmp[size];
739 chroma_cmp= s->dsp.me_cmp[size+1];
741 for(j=i=0; i<ME_MAP_SIZE; i++){
742 uint32_t key= map[i];
744 key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
746 if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
748 assert(j<MAX_SAB_SIZE); //max j = number of predictors
750 minima[j].height= score_map[i];
751 minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
752 minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
753 minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
754 minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
756 if(minima[j].x || minima[j].y)
757 minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
762 qsort(minima, j, sizeof(Minima), minima_cmp);
764 for(; j<minima_count; j++){
765 minima[j].height=256*256*256*64;
767 minima[j].x= minima[j].y=0;
770 for(i=0; i<minima_count; i++){
771 const int x= minima[i].x;
772 const int y= minima[i].y;
775 if(minima[i].checked) continue;
777 if( x >= xmax || x <= xmin
778 || y >= ymax || y <= ymin)
783 SAB_CHECK_MV(x , y-1)
784 SAB_CHECK_MV(x , y+1)
786 minima[i].checked= 1;
789 best[0]= minima[0].x;
790 best[1]= minima[0].y;
791 dmin= minima[0].height;
793 if( best[0] < xmax && best[0] > xmin
794 && best[1] < ymax && best[1] > ymin){
796 //ensure that the refernece samples for hpel refinement are in the map
797 CHECK_MV(best[0]-1, best[1])
798 CHECK_MV(best[0]+1, best[1])
799 CHECK_MV(best[0], best[1]-1)
800 CHECK_MV(best[0], best[1]+1)
805 static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
806 Picture *ref_picture,
807 int const pred_x, int const pred_y, int const penalty_factor,
808 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
809 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
812 me_cmp_func cmp, chroma_cmp;
814 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
816 cmp= s->dsp.me_cmp[size];
817 chroma_cmp= s->dsp.me_cmp[size+1];
819 for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
821 const int x= best[0];
822 const int y= best[1];
824 start= FFMAX(0, y + dia_size - ymax);
825 end = FFMIN(dia_size, xmax - x + 1);
826 for(dir= start; dir<end; dir++){
829 //check(x + dir,y + dia_size - dir,0, a0)
830 CHECK_MV(x + dir , y + dia_size - dir);
833 start= FFMAX(0, x + dia_size - xmax);
834 end = FFMIN(dia_size, y - ymin + 1);
835 for(dir= start; dir<end; dir++){
838 //check(x + dia_size - dir, y - dir,0, a1)
839 CHECK_MV(x + dia_size - dir, y - dir );
842 start= FFMAX(0, -y + dia_size + ymin );
843 end = FFMIN(dia_size, x - xmin + 1);
844 for(dir= start; dir<end; dir++){
847 //check(x - dir,y - dia_size + dir,0, a2)
848 CHECK_MV(x - dir , y - dia_size + dir);
851 start= FFMAX(0, -x + dia_size + xmin );
852 end = FFMIN(dia_size, ymax - y + 1);
853 for(dir= start; dir<end; dir++){
856 //check(x - dia_size + dir, y + dir,0, a3)
857 CHECK_MV(x - dia_size + dir, y + dir );
860 if(x!=best[0] || y!=best[1])
865 static int stats[8*8];
869 if(256*256*256*64 % (stats[0]+1)==0){
871 if((i&7)==0) printf("\n");
872 printf("%6d ", stats[i]);
882 static int RENAME(epzs_motion_search)(MpegEncContext * s, int block,
883 int *mx_ptr, int *my_ptr,
884 int P[10][2], int pred_x, int pred_y,
885 int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2],
886 int ref_mv_scale, uint16_t * const mv_penalty)
890 const int shift= 1+s->quarter_sample;
891 uint32_t *map= s->me.map;
893 const int penalty_factor= s->me.penalty_factor;
895 const int ref_mv_stride= s->mb_width+2;
896 const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
897 me_cmp_func cmp, chroma_cmp;
898 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
900 cmp= s->dsp.me_cmp[size];
901 chroma_cmp= s->dsp.me_cmp[size+1];
903 map_generation= update_map_generation(s);
905 CMP(dmin, 0, 0, size);
906 map[0]= map_generation;
911 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
912 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
913 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
915 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
917 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
923 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
925 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
926 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
927 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
928 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
929 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
934 CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,
935 (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
936 CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
937 (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
939 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
940 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
941 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
942 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
946 if(s->avctx->last_predictor_count){
947 const int count= s->avctx->last_predictor_count;
948 const int xstart= FFMAX(0, s->mb_x - count);
949 const int ystart= FFMAX(0, s->mb_y - count);
950 const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
951 const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
954 for(mb_y=ystart; mb_y<yend; mb_y++){
956 for(mb_x=xstart; mb_x<xend; mb_x++){
957 const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
958 int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
959 int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
961 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
967 //check(best[0],best[1],0, b0)
968 if(s->me.dia_size==-1)
969 dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
970 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
971 shift, map, map_generation, size, mv_penalty);
972 else if(s->me.dia_size<-1)
973 dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
974 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
975 shift, map, map_generation, size, mv_penalty);
976 else if(s->me.dia_size<2)
977 dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
978 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
979 shift, map, map_generation, size, mv_penalty);
981 dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
982 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
983 shift, map, map_generation, size, mv_penalty);
985 //check(best[0],best[1],0, b1)
989 // printf("%d %d %d \n", best[0], best[1], dmin);
993 #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
994 static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block,
995 int *mx_ptr, int *my_ptr,
996 int P[10][2], int pred_x, int pred_y,
997 int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2],
998 int ref_mv_scale, uint16_t * const mv_penalty)
1002 const int shift= 1+s->quarter_sample;
1003 uint32_t *map= s->me.map;
1005 const int penalty_factor= s->me.penalty_factor;
1007 const int ref_mv_stride= s->mb_width+2;
1008 const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
1009 me_cmp_func cmp, chroma_cmp;
1010 LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8);
1012 cmp= s->dsp.me_cmp[size];
1013 chroma_cmp= s->dsp.me_cmp[size+1];
1015 map_generation= update_map_generation(s);
1018 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
1020 if (s->mb_y == 0 && block<2) {
1021 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1022 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
1023 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1024 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1026 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1027 //FIXME try some early stop
1029 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
1030 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1031 CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
1032 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
1033 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
1034 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1038 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
1039 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
1040 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
1041 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
1044 if(s->me.dia_size==-1)
1045 dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
1046 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
1047 shift, map, map_generation, size, mv_penalty);
1048 else if(s->me.dia_size<-1)
1049 dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
1050 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
1051 shift, map, map_generation, size, mv_penalty);
1052 else if(s->me.dia_size<2)
1053 dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
1054 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
1055 shift, map, map_generation, size, mv_penalty);
1057 dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
1058 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
1059 shift, map, map_generation, size, mv_penalty);
1064 // printf("%d %d %d \n", best[0], best[1], dmin);
1067 #endif /* !CMP_DIRECT */