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
22 * @file motion_est_template.c
23 * Motion estimation template.
26 //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
27 //Note, the last line is there to kill these ugly unused var warnings
28 #define LOAD_COMMON(x, y)\
29 uint32_t * const score_map= s->me.score_map;\
30 const int stride= s->linesize;\
31 const int uvstride= s->uvlinesize;\
32 const int time_pp= s->pp_time;\
33 const int time_pb= s->pb_time;\
34 uint8_t * const src_y= s->new_picture.data[0] + ((y) * stride) + (x);\
35 uint8_t * const src_u= s->new_picture.data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
36 uint8_t * const src_v= s->new_picture.data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
37 uint8_t * const ref_y= ref_picture->data[0] + ((y) * stride) + (x);\
38 uint8_t * const ref_u= ref_picture->data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
39 uint8_t * const ref_v= ref_picture->data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
40 uint8_t * const ref2_y= s->next_picture.data[0] + ((y) * stride) + (x);\
41 op_pixels_func (*hpel_put)[4];\
42 op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\
43 op_pixels_func (*chroma_hpel_put)[4];\
44 qpel_mc_func (*qpel_put)[16];\
45 qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\
46 const __attribute__((unused)) int unu= time_pp + time_pb + (size_t)src_u + (size_t)src_v + (size_t)ref_u + (size_t)ref_v\
47 + (size_t)ref2_y + (size_t)hpel_avg + (size_t)qpel_avg + (size_t)score_map;\
48 if(s->no_rounding /*FIXME b_type*/){\
49 hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\
50 chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\
51 qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];\
53 hpel_put=& s->dsp.put_pixels_tab[size];\
54 chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];\
55 qpel_put= &s->dsp.put_qpel_pixels_tab[size];\
61 #define CHECK_HALF_MV(dx, dy, x, y)\
63 const int hx= 2*(x)+(dx);\
64 const int hy= 2*(y)+(dy);\
65 CMP_HPEL(d, dx, dy, x, y, size);\
66 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
67 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
71 static int RENAME(hpel_motion_search)(MpegEncContext * s,
72 int *mx_ptr, int *my_ptr, int dmin,
73 int xmin, int ymin, int xmax, int ymax,
74 int pred_x, int pred_y, Picture *ref_picture,
75 int n, int size, uint8_t * const mv_penalty)
77 const int xx = 16 * s->mb_x + 8*(n&1);
78 const int yy = 16 * s->mb_y + 8*(n>>1);
79 const int mx = *mx_ptr;
80 const int my = *my_ptr;
81 const int penalty_factor= s->me.sub_penalty_factor;
87 me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
89 if(s->no_rounding /*FIXME b_type*/){
90 hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
91 chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
93 hpel_put=& s->dsp.put_pixels_tab[size];
94 chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
96 cmp= s->dsp.me_cmp[size];
97 chroma_cmp= s->dsp.me_cmp[size+1];
98 cmp_sub= s->dsp.me_sub_cmp[size];
99 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
101 if(s->me.skip){ //FIXME somehow move up (benchmark)
107 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
108 CMP_HPEL(dmin, 0, 0, mx, my, size);
110 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
113 if (mx > xmin && mx < xmax &&
114 my > ymin && my < ymax) {
115 int bx=2*mx, by=2*my;
118 CHECK_HALF_MV(1, 1, mx-1, my-1)
119 CHECK_HALF_MV(0, 1, mx , my-1)
120 CHECK_HALF_MV(1, 1, mx , my-1)
121 CHECK_HALF_MV(1, 0, mx-1, my )
122 CHECK_HALF_MV(1, 0, mx , my )
123 CHECK_HALF_MV(1, 1, mx-1, my )
124 CHECK_HALF_MV(0, 1, mx , my )
125 CHECK_HALF_MV(1, 1, mx , my )
127 assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
140 static int RENAME(hpel_motion_search)(MpegEncContext * s,
141 int *mx_ptr, int *my_ptr, int dmin,
142 int xmin, int ymin, int xmax, int ymax,
143 int pred_x, int pred_y, Picture *ref_picture,
144 int n, int size, uint8_t * const mv_penalty)
146 const int xx = 16 * s->mb_x + 8*(n&1);
147 const int yy = 16 * s->mb_y + 8*(n>>1);
148 const int mx = *mx_ptr;
149 const int my = *my_ptr;
150 const int penalty_factor= s->me.sub_penalty_factor;
151 me_cmp_func cmp_sub, chroma_cmp_sub;
152 int bx=2*mx, by=2*my;
158 cmp_sub= s->dsp.me_sub_cmp[size];
159 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
161 if(s->me.skip){ //FIXME move out of hpel?
167 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
168 CMP_HPEL(dmin, 0, 0, mx, my, size);
169 if(mx || my || size>0)
170 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
173 if (mx > xmin && mx < xmax &&
174 my > ymin && my < ymax) {
176 const int index= (my<<ME_MAP_SHIFT) + mx;
177 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
178 + (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*s->me.penalty_factor;
179 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]
180 + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor;
181 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]
182 + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor;
183 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
184 + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*s->me.penalty_factor;
188 int map_generation= s->me.map_generation;
190 uint32_t *map= s->me.map;
192 key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
193 assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
194 key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
195 assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
196 key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
197 assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
198 key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
199 assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
202 CHECK_HALF_MV(0, 1, mx ,my-1)
204 CHECK_HALF_MV(1, 1, mx-1, my-1)
206 CHECK_HALF_MV(1, 1, mx , my-1)
208 CHECK_HALF_MV(1, 1, mx-1, my )
210 CHECK_HALF_MV(1, 0, mx-1, my )
212 CHECK_HALF_MV(1, 1, mx , my-1)
214 CHECK_HALF_MV(1, 1, mx-1, my-1)
216 CHECK_HALF_MV(1, 1, mx , my )
218 CHECK_HALF_MV(1, 0, mx , my )
223 CHECK_HALF_MV(1, 1, mx-1, my-1)
225 CHECK_HALF_MV(1, 1, mx , my )
227 CHECK_HALF_MV(1, 0, mx-1, my)
228 CHECK_HALF_MV(1, 1, mx-1, my)
231 CHECK_HALF_MV(1, 1, mx , my-1)
233 CHECK_HALF_MV(1, 1, mx-1, my)
235 CHECK_HALF_MV(1, 0, mx , my)
236 CHECK_HALF_MV(1, 1, mx , my)
238 CHECK_HALF_MV(0, 1, mx , my)
240 assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
250 static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture,
251 uint8_t * const mv_penalty)
253 // const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
255 const int xx = 16 * s->mb_x;
256 const int yy = 16 * s->mb_y;
257 const int penalty_factor= s->me.mb_penalty_factor;
258 const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these
259 const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit
260 me_cmp_func cmp_sub, chroma_cmp_sub;
267 cmp_sub= s->dsp.mb_cmp[size];
268 chroma_cmp_sub= s->dsp.mb_cmp[size+1];
271 assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
273 CMP_HPEL(d, mx&1, my&1, mx>>1, my>>1, size);
274 //FIXME check cbp before adding penalty for (0,0) vector
275 if(mx || my || size>0)
276 d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
281 #endif /* CMP_HPEL */
287 #define CHECK_QUARTER_MV(dx, dy, x, y)\
289 const int hx= 4*(x)+(dx);\
290 const int hy= 4*(y)+(dy);\
291 CMP_QPEL(d, dx, dy, x, y, size);\
292 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
293 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
296 static int RENAME(qpel_motion_search)(MpegEncContext * s,
297 int *mx_ptr, int *my_ptr, int dmin,
298 int xmin, int ymin, int xmax, int ymax,
299 int pred_x, int pred_y, Picture *ref_picture,
300 int n, int size, uint8_t * const mv_penalty)
302 const int xx = 16 * s->mb_x + 8*(n&1);
303 const int yy = 16 * s->mb_y + 8*(n>>1);
304 const int mx = *mx_ptr;
305 const int my = *my_ptr;
306 const int penalty_factor= s->me.sub_penalty_factor;
307 const int map_generation= s->me.map_generation;
308 const int subpel_quality= s->avctx->me_subpel_quality;
309 uint32_t *map= s->me.map;
310 me_cmp_func cmp, chroma_cmp;
311 me_cmp_func cmp_sub, chroma_cmp_sub;
315 cmp= s->dsp.me_cmp[size];
316 chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
319 cmp_sub= s->dsp.me_sub_cmp[size];
320 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
322 if(s->me.skip){ //FIXME somehow move up (benchmark)
328 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
329 CMP_QPEL(dmin, 0, 0, mx, my, size);
330 if(mx || my || size>0)
331 dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
334 if (mx > xmin && mx < xmax &&
335 my > ymin && my < ymax) {
336 int bx=4*mx, by=4*my;
339 const int index= (my<<ME_MAP_SHIFT) + mx;
340 const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
341 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
342 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
343 const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
344 const int c= score_map[(index )&(ME_MAP_SIZE-1)];
348 memset(best, 64, sizeof(int)*8);
350 if(s->me.dia_size>=2){
351 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
352 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
353 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
354 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
356 for(ny= -3; ny <= 3; ny++){
357 for(nx= -3; nx <= 3; nx++){
358 const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
359 const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
360 const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
361 int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
364 if((nx&3)==0 && (ny&3)==0) continue;
366 score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
368 // if(nx&1) score-=1024*s->me.penalty_factor;
369 // if(ny&1) score-=1024*s->me.penalty_factor;
373 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
374 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
376 best_pos[i][0]= nx + 4*mx;
377 best_pos[i][1]= ny + 4*my;
385 const int cx = 4*(r - l);
386 const int cx2= r + l - 2*c;
387 const int cy = 4*(b - t);
388 const int cy2= b + t - 2*c;
391 if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
392 tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
394 CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
397 cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
399 assert(16*cx2 + 4*cx + 32*c == 32*r);
400 assert(16*cx2 - 4*cx + 32*c == 32*l);
401 assert(16*cy2 + 4*cy + 32*c == 32*b);
402 assert(16*cy2 - 4*cy + 32*c == 32*t);
403 assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
405 for(ny= -3; ny <= 3; ny++){
406 for(nx= -3; nx <= 3; nx++){
407 int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
410 if((nx&3)==0 && (ny&3)==0) continue;
412 score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
413 // if(nx&1) score-=32*s->me.penalty_factor;
414 // if(ny&1) score-=32*s->me.penalty_factor;
418 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
419 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
421 best_pos[i][0]= nx + 4*mx;
422 best_pos[i][1]= ny + 4*my;
429 for(i=0; i<subpel_quality; i++){
432 CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
436 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
437 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
438 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
439 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
440 // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
443 // nx= FFMAX(4*mx - bx, bx - 4*mx);
444 // ny= FFMAX(4*my - by, by - 4*my);
446 static int stats[7][7], count;
448 stats[4*mx - bx + 3][4*my - by + 3]++;
449 if(256*256*256*64 % count ==0){
451 if((i%7)==0) printf("\n");
452 printf("%6d ", stats[0][i]);
460 CHECK_QUARTER_MV(2, 2, mx-1, my-1)
461 CHECK_QUARTER_MV(0, 2, mx , my-1)
462 CHECK_QUARTER_MV(2, 2, mx , my-1)
463 CHECK_QUARTER_MV(2, 0, mx , my )
464 CHECK_QUARTER_MV(2, 2, mx , my )
465 CHECK_QUARTER_MV(0, 2, mx , my )
466 CHECK_QUARTER_MV(2, 2, mx-1, my )
467 CHECK_QUARTER_MV(2, 0, mx-1, my )
473 int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
474 int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
475 CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
480 CHECK_QUARTER_MV(1, 3, mx-1, my-1)
481 CHECK_QUARTER_MV(1, 2, mx-1, my-1)
482 CHECK_QUARTER_MV(1, 1, mx-1, my-1)
483 CHECK_QUARTER_MV(2, 1, mx-1, my-1)
484 CHECK_QUARTER_MV(3, 1, mx-1, my-1)
485 CHECK_QUARTER_MV(0, 1, mx , my-1)
486 CHECK_QUARTER_MV(1, 1, mx , my-1)
487 CHECK_QUARTER_MV(2, 1, mx , my-1)
488 CHECK_QUARTER_MV(3, 1, mx , my-1)
489 CHECK_QUARTER_MV(3, 2, mx , my-1)
490 CHECK_QUARTER_MV(3, 3, mx , my-1)
491 CHECK_QUARTER_MV(3, 0, mx , my )
492 CHECK_QUARTER_MV(3, 1, mx , my )
493 CHECK_QUARTER_MV(3, 2, mx , my )
494 CHECK_QUARTER_MV(3, 3, mx , my )
495 CHECK_QUARTER_MV(2, 3, mx , my )
496 CHECK_QUARTER_MV(1, 3, mx , my )
497 CHECK_QUARTER_MV(0, 3, mx , my )
498 CHECK_QUARTER_MV(3, 3, mx-1, my )
499 CHECK_QUARTER_MV(2, 3, mx-1, my )
500 CHECK_QUARTER_MV(1, 3, mx-1, my )
501 CHECK_QUARTER_MV(1, 2, mx-1, my )
502 CHECK_QUARTER_MV(1, 1, mx-1, my )
503 CHECK_QUARTER_MV(1, 0, mx-1, my )
505 assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
517 static int RENAME(qpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture,
518 uint8_t * const mv_penalty)
521 const int xx = 16 * s->mb_x;
522 const int yy = 16 * s->mb_y;
523 const int penalty_factor= s->me.mb_penalty_factor;
524 const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these
525 const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit
526 me_cmp_func cmp_sub, chroma_cmp_sub;
533 cmp_sub= s->dsp.mb_cmp[size];
534 chroma_cmp_sub= s->dsp.mb_cmp[size+1];
537 assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
539 CMP_QPEL(d, mx&3, my&3, mx>>2, my>>2, size);
540 //FIXME check cbp before adding penalty for (0,0) vector
541 if(mx || my || size>0)
542 d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
548 #endif /* CMP_QPEL */
550 #define CHECK_MV(x,y)\
552 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
553 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
554 /*printf("check_mv %d %d\n", x, y);*/\
555 if(map[index]!=key){\
558 score_map[index]= d;\
559 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
560 /*printf("score:%d\n", d);*/\
561 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
565 #define CHECK_CLIPED_MV(ax,ay)\
567 const int x= FFMAX(xmin, FFMIN(ax, xmax));\
568 const int y= FFMAX(ymin, FFMIN(ay, ymax));\
572 #define CHECK_MV_DIR(x,y,new_dir)\
574 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
575 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
576 /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
577 if(map[index]!=key){\
580 score_map[index]= d;\
581 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
582 /*printf("score:%d\n", d);*/\
592 #define check(x,y,S,v)\
593 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
594 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
595 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
596 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
599 static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
600 Picture *ref_picture,
601 int const pred_x, int const pred_y, int const penalty_factor,
602 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
603 uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty
606 me_cmp_func cmp, chroma_cmp;
608 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
610 cmp= s->dsp.me_cmp[size];
611 chroma_cmp= s->dsp.me_cmp[size+1];
613 { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
614 const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
615 const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
616 if(map[index]!=key){ //this will be executed only very rarey
617 CMP(score_map[index], best[0], best[1], size);
624 const int dir= next_dir;
625 const int x= best[0];
626 const int y= best[1];
630 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
631 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
632 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
633 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
641 static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
642 Picture *ref_picture,
643 int const pred_x, int const pred_y, int const penalty_factor,
644 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
645 uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty
648 me_cmp_func cmp, chroma_cmp;
650 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
652 cmp= s->dsp.me_cmp[size];
653 chroma_cmp= s->dsp.me_cmp[size+1];
655 for(dia_size=1; dia_size<=4; dia_size++){
657 const int x= best[0];
658 const int y= best[1];
660 if(dia_size&(dia_size-1)) continue;
662 if( x + dia_size > xmax
663 || x - dia_size < xmin
664 || y + dia_size > ymax
665 || y - dia_size < ymin)
668 for(dir= 0; dir<dia_size; dir+=2){
671 CHECK_MV(x + dir , y + dia_size - dir);
672 CHECK_MV(x + dia_size - dir, y - dir );
673 CHECK_MV(x - dir , y - dia_size + dir);
674 CHECK_MV(x - dia_size + dir, y + dir );
677 if(x!=best[0] || y!=best[1])
682 static int stats[8*8];
686 dx^=dy; dy^=dx; dx^=dy;
689 if(256*256*256*64 % (stats[0]+1)==0){
691 if((i&7)==0) printf("\n");
692 printf("%8d ", stats[i]);
702 #define SAB_CHECK_MV(ax,ay)\
704 const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
705 const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
706 /*printf("sab check %d %d\n", ax, ay);*/\
707 if(map[index]!=key){\
708 CMP(d, ax, ay, size);\
710 score_map[index]= d;\
711 d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
712 /*printf("score: %d\n", d);*/\
713 if(d < minima[minima_count-1].height){\
716 while(d >= minima[j].height) j++;\
718 memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
720 minima[j].checked= 0;\
721 minima[j].height= d;\
731 #define MAX_SAB_SIZE 16
732 static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
733 Picture *ref_picture,
734 int const pred_x, int const pred_y, int const penalty_factor,
735 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
736 uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty
739 me_cmp_func cmp, chroma_cmp;
740 Minima minima[MAX_SAB_SIZE];
741 const int minima_count= ABS(s->me.dia_size);
743 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
745 cmp= s->dsp.me_cmp[size];
746 chroma_cmp= s->dsp.me_cmp[size+1];
748 for(j=i=0; i<ME_MAP_SIZE; i++){
749 uint32_t key= map[i];
751 key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
753 if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
755 assert(j<MAX_SAB_SIZE); //max j = number of predictors
757 minima[j].height= score_map[i];
758 minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
759 minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
760 minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
761 minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
763 if(minima[j].x || minima[j].y)
764 minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
769 qsort(minima, j, sizeof(Minima), minima_cmp);
771 for(; j<minima_count; j++){
772 minima[j].height=256*256*256*64;
774 minima[j].x= minima[j].y=0;
777 for(i=0; i<minima_count; i++){
778 const int x= minima[i].x;
779 const int y= minima[i].y;
782 if(minima[i].checked) continue;
784 if( x >= xmax || x <= xmin
785 || y >= ymax || y <= ymin)
790 SAB_CHECK_MV(x , y-1)
791 SAB_CHECK_MV(x , y+1)
793 minima[i].checked= 1;
796 best[0]= minima[0].x;
797 best[1]= minima[0].y;
798 dmin= minima[0].height;
800 if( best[0] < xmax && best[0] > xmin
801 && best[1] < ymax && best[1] > ymin){
803 //ensure that the refernece samples for hpel refinement are in the map
804 CHECK_MV(best[0]-1, best[1])
805 CHECK_MV(best[0]+1, best[1])
806 CHECK_MV(best[0], best[1]-1)
807 CHECK_MV(best[0], best[1]+1)
812 static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
813 Picture *ref_picture,
814 int const pred_x, int const pred_y, int const penalty_factor,
815 int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
816 uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty
819 me_cmp_func cmp, chroma_cmp;
821 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
823 cmp= s->dsp.me_cmp[size];
824 chroma_cmp= s->dsp.me_cmp[size+1];
826 for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
828 const int x= best[0];
829 const int y= best[1];
831 start= FFMAX(0, y + dia_size - ymax);
832 end = FFMIN(dia_size, xmax - x + 1);
833 for(dir= start; dir<end; dir++){
836 //check(x + dir,y + dia_size - dir,0, a0)
837 CHECK_MV(x + dir , y + dia_size - dir);
840 start= FFMAX(0, x + dia_size - xmax);
841 end = FFMIN(dia_size, y - ymin + 1);
842 for(dir= start; dir<end; dir++){
845 //check(x + dia_size - dir, y - dir,0, a1)
846 CHECK_MV(x + dia_size - dir, y - dir );
849 start= FFMAX(0, -y + dia_size + ymin );
850 end = FFMIN(dia_size, x - xmin + 1);
851 for(dir= start; dir<end; dir++){
854 //check(x - dir,y - dia_size + dir,0, a2)
855 CHECK_MV(x - dir , y - dia_size + dir);
858 start= FFMAX(0, -x + dia_size + xmin );
859 end = FFMIN(dia_size, ymax - y + 1);
860 for(dir= start; dir<end; dir++){
863 //check(x - dia_size + dir, y + dir,0, a3)
864 CHECK_MV(x - dia_size + dir, y + dir );
867 if(x!=best[0] || y!=best[1])
872 static int stats[8*8];
876 if(256*256*256*64 % (stats[0]+1)==0){
878 if((i&7)==0) printf("\n");
879 printf("%6d ", stats[i]);
889 static int RENAME(epzs_motion_search)(MpegEncContext * s, int block,
890 int *mx_ptr, int *my_ptr,
891 int P[10][2], int pred_x, int pred_y,
892 int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2],
893 int ref_mv_scale, uint8_t * const mv_penalty)
897 const int shift= 1+s->quarter_sample;
898 uint32_t *map= s->me.map;
900 const int penalty_factor= s->me.penalty_factor;
902 const int ref_mv_stride= s->mb_stride;
903 const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride;
904 me_cmp_func cmp, chroma_cmp;
905 LOAD_COMMON(s->mb_x*16, s->mb_y*16);
907 cmp= s->dsp.me_cmp[size];
908 chroma_cmp= s->dsp.me_cmp[size+1];
910 map_generation= update_map_generation(s);
912 CMP(dmin, 0, 0, size);
913 map[0]= map_generation;
918 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
919 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
920 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
922 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
924 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
930 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
932 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
933 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
934 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
935 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
936 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
941 CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,
942 (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
943 CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
944 (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
946 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
947 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
948 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
949 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
953 if(s->avctx->last_predictor_count){
954 const int count= s->avctx->last_predictor_count;
955 const int xstart= FFMAX(0, s->mb_x - count);
956 const int ystart= FFMAX(0, s->mb_y - count);
957 const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
958 const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
961 for(mb_y=ystart; mb_y<yend; mb_y++){
963 for(mb_x=xstart; mb_x<xend; mb_x++){
964 const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
965 int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
966 int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
968 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
974 //check(best[0],best[1],0, b0)
975 if(s->me.dia_size==-1)
976 dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
977 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
978 shift, map, map_generation, size, mv_penalty);
979 else if(s->me.dia_size<-1)
980 dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
981 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
982 shift, map, map_generation, size, mv_penalty);
983 else if(s->me.dia_size<2)
984 dmin= RENAME(small_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);
988 dmin= RENAME(var_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);
992 //check(best[0],best[1],0, b1)
996 // printf("%d %d %d \n", best[0], best[1], dmin);
1000 #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
1001 static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block,
1002 int *mx_ptr, int *my_ptr,
1003 int P[10][2], int pred_x, int pred_y,
1004 int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2],
1005 int ref_mv_scale, uint8_t * const mv_penalty)
1009 const int shift= 1+s->quarter_sample;
1010 uint32_t *map= s->me.map;
1012 const int penalty_factor= s->me.penalty_factor;
1014 const int ref_mv_stride= s->mb_stride;
1015 const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
1016 me_cmp_func cmp, chroma_cmp;
1017 LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8);
1019 cmp= s->dsp.me_cmp[size];
1020 chroma_cmp= s->dsp.me_cmp[size+1];
1022 map_generation= update_map_generation(s);
1025 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
1027 if (s->mb_y == 0 && block<2) {
1028 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1029 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
1030 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1031 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1033 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1034 //FIXME try some early stop
1036 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
1037 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1038 CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
1039 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
1040 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
1041 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1045 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
1046 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
1047 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
1048 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
1051 if(s->me.dia_size==-1)
1052 dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
1053 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
1054 shift, map, map_generation, size, mv_penalty);
1055 else if(s->me.dia_size<-1)
1056 dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
1057 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
1058 shift, map, map_generation, size, mv_penalty);
1059 else if(s->me.dia_size<2)
1060 dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
1061 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
1062 shift, map, map_generation, size, mv_penalty);
1064 dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
1065 pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
1066 shift, map, map_generation, size, mv_penalty);
1071 // printf("%d %d %d \n", best[0], best[1], dmin);
1074 #endif /* !CMP_DIRECT */