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;\
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;
152 cmp_sub= s->dsp.me_sub_cmp[size];
153 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
155 if(s->me.skip){ //FIXME move out of hpel?
161 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
162 CMP_HPEL(dmin, 0, 0, mx, my, size);
163 if(mx || my || size>0)
164 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
167 if (mx > xmin && mx < xmax &&
168 my > ymin && my < ymax) {
169 int bx=2*mx, by=2*my;
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);
246 #endif /* CMP_HPEL */
250 #define CHECK_QUARTER_MV(dx, dy, x, y)\
252 const int hx= 4*(x)+(dx);\
253 const int hy= 4*(y)+(dy);\
254 CMP_QPEL(d, dx, dy, x, y, size);\
255 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
256 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
259 static int RENAME(qpel_motion_search)(MpegEncContext * s,
260 int *mx_ptr, int *my_ptr, int dmin,
261 int xmin, int ymin, int xmax, int ymax,
262 int pred_x, int pred_y, Picture *ref_picture,
263 int n, int size, uint16_t * const mv_penalty)
265 const int xx = 16 * s->mb_x + 8*(n&1);
266 const int yy = 16 * s->mb_y + 8*(n>>1);
267 const int mx = *mx_ptr;
268 const int my = *my_ptr;
269 const int penalty_factor= s->me.sub_penalty_factor;
270 const int map_generation= s->me.map_generation;
271 const int subpel_quality= s->avctx->me_subpel_quality;
272 uint32_t *map= s->me.map;
273 me_cmp_func cmp, chroma_cmp;
274 me_cmp_func cmp_sub, chroma_cmp_sub;
278 cmp= s->dsp.me_cmp[size];
279 chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
282 cmp_sub= s->dsp.me_sub_cmp[size];
283 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
285 if(s->me.skip){ //FIXME somehow move up (benchmark)
291 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
292 CMP_QPEL(dmin, 0, 0, mx, my, size);
293 if(mx || my || size>0)
294 dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
297 if (mx > xmin && mx < xmax &&
298 my > ymin && my < ymax) {
299 int bx=4*mx, by=4*my;
302 const int index= (my<<ME_MAP_SHIFT) + mx;
303 const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
304 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
305 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
306 const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
307 const int c= score_map[(index )&(ME_MAP_SIZE-1)];
311 memset(best, 64, sizeof(int)*8);
313 if(s->me.dia_size>=2){
314 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
315 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
316 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
317 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
319 for(ny= -3; ny <= 3; ny++){
320 for(nx= -3; nx <= 3; nx++){
321 const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
322 const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
323 const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
324 int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
327 if((nx&3)==0 && (ny&3)==0) continue;
329 score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
331 // if(nx&1) score-=1024*s->me.penalty_factor;
332 // if(ny&1) score-=1024*s->me.penalty_factor;
336 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
337 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
339 best_pos[i][0]= nx + 4*mx;
340 best_pos[i][1]= ny + 4*my;
348 const int cx = 4*(r - l);
349 const int cx2= r + l - 2*c;
350 const int cy = 4*(b - t);
351 const int cy2= b + t - 2*c;
354 if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
355 tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
357 CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
360 cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
362 assert(16*cx2 + 4*cx + 32*c == 32*r);
363 assert(16*cx2 - 4*cx + 32*c == 32*l);
364 assert(16*cy2 + 4*cy + 32*c == 32*b);
365 assert(16*cy2 - 4*cy + 32*c == 32*t);
366 assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
368 for(ny= -3; ny <= 3; ny++){
369 for(nx= -3; nx <= 3; nx++){
370 int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
373 if((nx&3)==0 && (ny&3)==0) continue;
375 score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
376 // if(nx&1) score-=32*s->me.penalty_factor;
377 // if(ny&1) score-=32*s->me.penalty_factor;
381 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
382 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
384 best_pos[i][0]= nx + 4*mx;
385 best_pos[i][1]= ny + 4*my;
392 for(i=0; i<subpel_quality; i++){
395 CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
399 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
400 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
401 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
402 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
403 // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
406 // nx= FFMAX(4*mx - bx, bx - 4*mx);
407 // ny= FFMAX(4*my - by, by - 4*my);
409 static int stats[7][7], count;
411 stats[4*mx - bx + 3][4*my - by + 3]++;
412 if(256*256*256*64 % count ==0){
414 if((i%7)==0) printf("\n");
415 printf("%6d ", stats[0][i]);
423 CHECK_QUARTER_MV(2, 2, mx-1, my-1)
424 CHECK_QUARTER_MV(0, 2, mx , my-1)
425 CHECK_QUARTER_MV(2, 2, mx , my-1)
426 CHECK_QUARTER_MV(2, 0, mx , my )
427 CHECK_QUARTER_MV(2, 2, mx , my )
428 CHECK_QUARTER_MV(0, 2, mx , my )
429 CHECK_QUARTER_MV(2, 2, mx-1, my )
430 CHECK_QUARTER_MV(2, 0, mx-1, my )
436 int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
437 int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
438 CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
443 CHECK_QUARTER_MV(1, 3, mx-1, my-1)
444 CHECK_QUARTER_MV(1, 2, mx-1, my-1)
445 CHECK_QUARTER_MV(1, 1, mx-1, my-1)
446 CHECK_QUARTER_MV(2, 1, mx-1, my-1)
447 CHECK_QUARTER_MV(3, 1, mx-1, my-1)
448 CHECK_QUARTER_MV(0, 1, mx , my-1)
449 CHECK_QUARTER_MV(1, 1, mx , my-1)
450 CHECK_QUARTER_MV(2, 1, mx , my-1)
451 CHECK_QUARTER_MV(3, 1, mx , my-1)
452 CHECK_QUARTER_MV(3, 2, mx , my-1)
453 CHECK_QUARTER_MV(3, 3, mx , my-1)
454 CHECK_QUARTER_MV(3, 0, mx , my )
455 CHECK_QUARTER_MV(3, 1, mx , my )
456 CHECK_QUARTER_MV(3, 2, mx , my )
457 CHECK_QUARTER_MV(3, 3, mx , my )
458 CHECK_QUARTER_MV(2, 3, mx , my )
459 CHECK_QUARTER_MV(1, 3, mx , my )
460 CHECK_QUARTER_MV(0, 3, mx , my )
461 CHECK_QUARTER_MV(3, 3, mx-1, my )
462 CHECK_QUARTER_MV(2, 3, mx-1, my )
463 CHECK_QUARTER_MV(1, 3, mx-1, my )
464 CHECK_QUARTER_MV(1, 2, mx-1, my )
465 CHECK_QUARTER_MV(1, 1, mx-1, my )
466 CHECK_QUARTER_MV(1, 0, mx-1, my )
468 assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
480 #endif /* CMP_QPEL */
482 #define CHECK_MV(x,y)\
484 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
485 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
486 /*printf("check_mv %d %d\n", x, y);*/\
487 if(map[index]!=key){\
490 score_map[index]= d;\
491 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
492 /*printf("score:%d\n", d);*/\
493 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
497 #define CHECK_CLIPED_MV(ax,ay)\
499 const int x= FFMAX(xmin, FFMIN(ax, xmax));\
500 const int y= FFMAX(ymin, FFMIN(ay, ymax));\
504 #define CHECK_MV_DIR(x,y,new_dir)\
506 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
507 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
508 /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
509 if(map[index]!=key){\
512 score_map[index]= d;\
513 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
514 /*printf("score:%d\n", d);*/\
524 #define check(x,y,S,v)\
525 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
526 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
527 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
528 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
531 static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
532 Picture *ref_picture,
533 int const pred_x, int const pred_y, int const penalty_factor,
534 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
535 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
538 me_cmp_func cmp, chroma_cmp;
540 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
542 cmp= s->dsp.me_cmp[size];
543 chroma_cmp= s->dsp.me_cmp[size+1];
545 { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
546 const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
547 const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
548 if(map[index]!=key){ //this will be executed only very rarey
549 CMP(score_map[index], best[0], best[1], size);
556 const int dir= next_dir;
557 const int x= best[0];
558 const int y= best[1];
562 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
563 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
564 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
565 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
573 static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
574 Picture *ref_picture,
575 int const pred_x, int const pred_y, int const penalty_factor,
576 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
577 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
580 me_cmp_func cmp, chroma_cmp;
582 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
584 cmp= s->dsp.me_cmp[size];
585 chroma_cmp= s->dsp.me_cmp[size+1];
587 for(dia_size=1; dia_size<=4; dia_size++){
589 const int x= best[0];
590 const int y= best[1];
592 if(dia_size&(dia_size-1)) continue;
594 if( x + dia_size > xmax
595 || x - dia_size < xmin
596 || y + dia_size > ymax
597 || y - dia_size < ymin)
600 for(dir= 0; dir<dia_size; dir+=2){
603 CHECK_MV(x + dir , y + dia_size - dir);
604 CHECK_MV(x + dia_size - dir, y - dir );
605 CHECK_MV(x - dir , y - dia_size + dir);
606 CHECK_MV(x - dia_size + dir, y + dir );
609 if(x!=best[0] || y!=best[1])
614 static int stats[8*8];
618 dx^=dy; dy^=dx; dx^=dy;
621 if(256*256*256*64 % (stats[0]+1)==0){
623 if((i&7)==0) printf("\n");
624 printf("%8d ", stats[i]);
634 #define SAB_CHECK_MV(ax,ay)\
636 const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
637 const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
638 /*printf("sab check %d %d\n", ax, ay);*/\
639 if(map[index]!=key){\
640 CMP(d, ax, ay, size);\
642 score_map[index]= d;\
643 d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
644 /*printf("score: %d\n", d);*/\
645 if(d < minima[minima_count-1].height){\
648 while(d >= minima[j].height) j++;\
650 memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
652 minima[j].checked= 0;\
653 minima[j].height= d;\
663 #define MAX_SAB_SIZE 16
664 static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
665 Picture *ref_picture,
666 int const pred_x, int const pred_y, int const penalty_factor,
667 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
668 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
671 me_cmp_func cmp, chroma_cmp;
672 Minima minima[MAX_SAB_SIZE];
673 const int minima_count= ABS(s->me.dia_size);
675 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
677 cmp= s->dsp.me_cmp[size];
678 chroma_cmp= s->dsp.me_cmp[size+1];
680 for(j=i=0; i<ME_MAP_SIZE; i++){
681 uint32_t key= map[i];
683 key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
685 if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
687 assert(j<MAX_SAB_SIZE); //max j = number of predictors
689 minima[j].height= score_map[i];
690 minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
691 minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
692 minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
693 minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
695 if(minima[j].x || minima[j].y)
696 minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
701 qsort(minima, j, sizeof(Minima), minima_cmp);
703 for(; j<minima_count; j++){
704 minima[j].height=256*256*256*64;
706 minima[j].x= minima[j].y=0;
709 for(i=0; i<minima_count; i++){
710 const int x= minima[i].x;
711 const int y= minima[i].y;
714 if(minima[i].checked) continue;
716 if( x >= xmax || x <= xmin
717 || y >= ymax || y <= ymin)
722 SAB_CHECK_MV(x , y-1)
723 SAB_CHECK_MV(x , y+1)
725 minima[i].checked= 1;
728 best[0]= minima[0].x;
729 best[1]= minima[0].y;
730 dmin= minima[0].height;
732 if( best[0] < xmax && best[0] > xmin
733 && best[1] < ymax && best[1] > ymin){
735 //ensure that the refernece samples for hpel refinement are in the map
736 CHECK_MV(best[0]-1, best[1])
737 CHECK_MV(best[0]+1, best[1])
738 CHECK_MV(best[0], best[1]-1)
739 CHECK_MV(best[0], best[1]+1)
744 static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
745 Picture *ref_picture,
746 int const pred_x, int const pred_y, int const penalty_factor,
747 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
748 uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
751 me_cmp_func cmp, chroma_cmp;
753 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
755 cmp= s->dsp.me_cmp[size];
756 chroma_cmp= s->dsp.me_cmp[size+1];
758 for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
760 const int x= best[0];
761 const int y= best[1];
763 start= FFMAX(0, y + dia_size - ymax);
764 end = FFMIN(dia_size, xmax - x + 1);
765 for(dir= start; dir<end; dir++){
768 //check(x + dir,y + dia_size - dir,0, a0)
769 CHECK_MV(x + dir , y + dia_size - dir);
772 start= FFMAX(0, x + dia_size - xmax);
773 end = FFMIN(dia_size, y - ymin + 1);
774 for(dir= start; dir<end; dir++){
777 //check(x + dia_size - dir, y - dir,0, a1)
778 CHECK_MV(x + dia_size - dir, y - dir );
781 start= FFMAX(0, -y + dia_size + ymin );
782 end = FFMIN(dia_size, x - xmin + 1);
783 for(dir= start; dir<end; dir++){
786 //check(x - dir,y - dia_size + dir,0, a2)
787 CHECK_MV(x - dir , y - dia_size + dir);
790 start= FFMAX(0, -x + dia_size + xmin );
791 end = FFMIN(dia_size, ymax - y + 1);
792 for(dir= start; dir<end; dir++){
795 //check(x - dia_size + dir, y + dir,0, a3)
796 CHECK_MV(x - dia_size + dir, y + dir );
799 if(x!=best[0] || y!=best[1])
804 static int stats[8*8];
808 if(256*256*256*64 % (stats[0]+1)==0){
810 if((i&7)==0) printf("\n");
811 printf("%6d ", stats[i]);
821 static int RENAME(epzs_motion_search)(MpegEncContext * s, int block,
822 int *mx_ptr, int *my_ptr,
823 int P[10][2], int pred_x, int pred_y,
824 int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2],
825 int ref_mv_scale, uint16_t * const mv_penalty)
829 const int shift= 1+s->quarter_sample;
830 uint32_t *map= s->me.map;
832 const int penalty_factor= s->me.penalty_factor;
834 const int ref_mv_stride= s->mb_width+2;
835 const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
836 me_cmp_func cmp, chroma_cmp;
837 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
839 cmp= s->dsp.me_cmp[size];
840 chroma_cmp= s->dsp.me_cmp[size+1];
842 map_generation= update_map_generation(s);
844 CMP(dmin, 0, 0, size);
845 map[0]= map_generation;
850 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
851 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
852 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
854 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
856 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
862 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
864 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
865 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
866 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
867 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
868 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
873 CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,
874 (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
875 CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
876 (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
878 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
879 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
880 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
881 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
885 if(s->avctx->last_predictor_count){
886 const int count= s->avctx->last_predictor_count;
887 const int xstart= FFMAX(0, s->mb_x - count);
888 const int ystart= FFMAX(0, s->mb_y - count);
889 const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
890 const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
893 for(mb_y=ystart; mb_y<yend; mb_y++){
895 for(mb_x=xstart; mb_x<xend; mb_x++){
896 const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
897 int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
898 int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
900 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
906 //check(best[0],best[1],0, b0)
907 if(s->me.dia_size==-1)
908 dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
909 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
910 shift, map, map_generation, size, mv_penalty);
911 else if(s->me.dia_size<-1)
912 dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
913 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
914 shift, map, map_generation, size, mv_penalty);
915 else if(s->me.dia_size<2)
916 dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
917 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
918 shift, map, map_generation, size, mv_penalty);
920 dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
921 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
922 shift, map, map_generation, size, mv_penalty);
924 //check(best[0],best[1],0, b1)
928 // printf("%d %d %d \n", best[0], best[1], dmin);
932 #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
933 static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block,
934 int *mx_ptr, int *my_ptr,
935 int P[10][2], int pred_x, int pred_y,
936 int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2],
937 int ref_mv_scale, uint16_t * const mv_penalty)
941 const int shift= 1+s->quarter_sample;
942 uint32_t *map= s->me.map;
944 const int penalty_factor= s->me.penalty_factor;
946 const int ref_mv_stride= s->mb_width+2;
947 const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
948 me_cmp_func cmp, chroma_cmp;
949 LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8);
951 cmp= s->dsp.me_cmp[size];
952 chroma_cmp= s->dsp.me_cmp[size+1];
954 map_generation= update_map_generation(s);
957 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
959 if (s->mb_y == 0 && block<2) {
960 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
961 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
962 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
963 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
965 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
966 //FIXME try some early stop
968 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
969 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
970 CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
971 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
972 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
973 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
977 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
978 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
979 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
980 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
983 if(s->me.dia_size==-1)
984 dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
985 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
986 shift, map, map_generation, size, mv_penalty);
987 else if(s->me.dia_size<-1)
988 dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
989 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
990 shift, map, map_generation, size, mv_penalty);
991 else if(s->me.dia_size<2)
992 dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
993 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
994 shift, map, map_generation, size, mv_penalty);
996 dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
997 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
998 shift, map, map_generation, size, mv_penalty);
1003 // printf("%d %d %d \n", best[0], best[1], dmin);
1006 #endif /* !CMP_DIRECT */