3 * Copyright (c) 2002-2004 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.
25 //FIXME ref2_y next_pic?
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
29 uint32_t * const score_map= s->me.score_map;\
30 const int time_pp= s->pp_time;\
31 const int time_pb= s->pb_time;\
32 const int xmin= s->me.xmin;\
33 const int ymin= s->me.ymin;\
34 const int xmax= s->me.xmax;\
35 const int ymax= s->me.ymax;\
36 uint8_t * const src_y= src_data[0];\
37 uint8_t * const src_u= src_data[1];\
38 uint8_t * const src_v= src_data[2];\
39 uint8_t * const ref_y= ref_data[0];\
40 uint8_t * const ref_u= ref_data[1];\
41 uint8_t * const ref_v= ref_data[2];\
42 op_pixels_func (*hpel_put)[4];\
43 op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\
44 op_pixels_func (*chroma_hpel_put)[4];\
45 qpel_mc_func (*qpel_put)[16];\
46 qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\
47 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\
48 + (size_t)hpel_avg + (size_t)qpel_avg + (size_t)score_map\
49 + xmin + xmax + ymin + ymax;\
50 if(s->no_rounding /*FIXME b_type*/){\
51 hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\
52 chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\
53 qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];\
55 hpel_put=& s->dsp.put_pixels_tab[size];\
56 chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];\
57 qpel_put= &s->dsp.put_qpel_pixels_tab[size];\
63 #define CHECK_HALF_MV(dx, dy, x, y)\
65 const int hx= 2*(x)+(dx);\
66 const int hy= 2*(y)+(dy);\
67 CMP_HPEL(d, dx, dy, x, y, size);\
68 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
69 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
73 static int RENAME(hpel_motion_search)(MpegEncContext * s,
74 int *mx_ptr, int *my_ptr, int dmin,
75 int pred_x, int pred_y, uint8_t *ref_data[3],
76 int size, uint8_t * const mv_penalty)
78 const int xx = 16 * s->mb_x + 8*(n&1);
79 const int yy = 16 * s->mb_y + 8*(n>>1);
80 const int mx = *mx_ptr;
81 const int my = *my_ptr;
82 const int penalty_factor= s->me.sub_penalty_factor;
88 me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
90 if(s->no_rounding /*FIXME b_type*/){
91 hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
92 chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
94 hpel_put=& s->dsp.put_pixels_tab[size];
95 chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
97 cmp= s->dsp.me_cmp[size];
98 chroma_cmp= s->dsp.me_cmp[size+1];
99 cmp_sub= s->dsp.me_sub_cmp[size];
100 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
102 if(s->me.skip){ //FIXME somehow move up (benchmark)
108 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
109 CMP_HPEL(dmin, 0, 0, mx, my, size);
111 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
114 if (mx > xmin && mx < xmax &&
115 my > ymin && my < ymax) {
116 int bx=2*mx, by=2*my;
119 CHECK_HALF_MV(1, 1, mx-1, my-1)
120 CHECK_HALF_MV(0, 1, mx , my-1)
121 CHECK_HALF_MV(1, 1, mx , my-1)
122 CHECK_HALF_MV(1, 0, mx-1, my )
123 CHECK_HALF_MV(1, 0, mx , my )
124 CHECK_HALF_MV(1, 1, mx-1, my )
125 CHECK_HALF_MV(0, 1, mx , my )
126 CHECK_HALF_MV(1, 1, mx , my )
128 assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
141 static int RENAME(hpel_motion_search)(MpegEncContext * s,
142 int *mx_ptr, int *my_ptr, int dmin,
143 int pred_x, int pred_y, uint8_t *src_data[3],
144 uint8_t *ref_data[3], int stride, int uvstride,
145 int size, int h, uint8_t * const mv_penalty)
147 const int mx = *mx_ptr;
148 const int my = *my_ptr;
149 const int penalty_factor= s->me.sub_penalty_factor;
150 me_cmp_func cmp_sub, chroma_cmp_sub;
151 int bx=2*mx, by=2*my;
157 cmp_sub= s->dsp.me_sub_cmp[size];
158 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
160 if(s->me.skip){ //FIXME move out of hpel?
166 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
167 CMP_HPEL(dmin, 0, 0, mx, my, size);
168 if(mx || my || size>0)
169 dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
172 if (mx > xmin && mx < xmax &&
173 my > ymin && my < ymax) {
175 const int index= (my<<ME_MAP_SHIFT) + mx;
176 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
177 + (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*s->me.penalty_factor;
178 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]
179 + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor;
180 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]
181 + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor;
182 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
183 + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*s->me.penalty_factor;
187 int map_generation= s->me.map_generation;
189 uint32_t *map= s->me.map;
191 key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
192 assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
193 key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
194 assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
195 key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
196 assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
197 key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
198 assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
201 CHECK_HALF_MV(0, 1, mx ,my-1)
203 CHECK_HALF_MV(1, 1, mx-1, my-1)
205 CHECK_HALF_MV(1, 1, mx , my-1)
207 CHECK_HALF_MV(1, 1, mx-1, my )
209 CHECK_HALF_MV(1, 0, mx-1, my )
211 CHECK_HALF_MV(1, 1, mx , my-1)
213 CHECK_HALF_MV(1, 1, mx-1, my-1)
215 CHECK_HALF_MV(1, 1, mx , my )
217 CHECK_HALF_MV(1, 0, mx , my )
222 CHECK_HALF_MV(1, 1, mx-1, my-1)
224 CHECK_HALF_MV(1, 1, mx , my )
226 CHECK_HALF_MV(1, 0, mx-1, my)
227 CHECK_HALF_MV(1, 1, mx-1, my)
230 CHECK_HALF_MV(1, 1, mx , my-1)
232 CHECK_HALF_MV(1, 1, mx-1, my)
234 CHECK_HALF_MV(1, 0, mx , my)
235 CHECK_HALF_MV(1, 1, mx , my)
237 CHECK_HALF_MV(0, 1, mx , my)
239 assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
249 static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, uint8_t *src_data[3],
250 uint8_t *ref_data[3], int stride, int uvstride,
251 uint8_t * const mv_penalty)
253 // const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
256 const int penalty_factor= s->me.mb_penalty_factor;
257 me_cmp_func cmp_sub, chroma_cmp_sub;
264 cmp_sub= s->dsp.mb_cmp[size];
265 chroma_cmp_sub= s->dsp.mb_cmp[size+1];
268 assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
270 CMP_HPEL(d, mx&1, my&1, mx>>1, my>>1, size);
271 //FIXME check cbp before adding penalty for (0,0) vector
272 if(mx || my || size>0)
273 d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
278 #endif /* CMP_HPEL */
284 #define CHECK_QUARTER_MV(dx, dy, x, y)\
286 const int hx= 4*(x)+(dx);\
287 const int hy= 4*(y)+(dy);\
288 CMP_QPEL(d, dx, dy, x, y, size);\
289 d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
290 COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
293 static int RENAME(qpel_motion_search)(MpegEncContext * s,
294 int *mx_ptr, int *my_ptr, int dmin,
295 int pred_x, int pred_y, uint8_t *src_data[3],
296 uint8_t *ref_data[3], int stride, int uvstride,
297 int size, int h, uint8_t * const mv_penalty)
299 const int mx = *mx_ptr;
300 const int my = *my_ptr;
301 const int penalty_factor= s->me.sub_penalty_factor;
302 const int map_generation= s->me.map_generation;
303 const int subpel_quality= s->avctx->me_subpel_quality;
304 uint32_t *map= s->me.map;
305 me_cmp_func cmp, chroma_cmp;
306 me_cmp_func cmp_sub, chroma_cmp_sub;
310 cmp= s->dsp.me_cmp[size];
311 chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
314 cmp_sub= s->dsp.me_sub_cmp[size];
315 chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
317 if(s->me.skip){ //FIXME somehow move up (benchmark)
323 if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
324 CMP_QPEL(dmin, 0, 0, mx, my, size);
325 if(mx || my || size>0)
326 dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
329 if (mx > xmin && mx < xmax &&
330 my > ymin && my < ymax) {
331 int bx=4*mx, by=4*my;
334 const int index= (my<<ME_MAP_SHIFT) + mx;
335 const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
336 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
337 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
338 const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
339 const int c= score_map[(index )&(ME_MAP_SIZE-1)];
343 memset(best, 64, sizeof(int)*8);
345 if(s->me.dia_size>=2){
346 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
347 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
348 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
349 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
351 for(ny= -3; ny <= 3; ny++){
352 for(nx= -3; nx <= 3; nx++){
353 const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
354 const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
355 const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
356 int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
359 if((nx&3)==0 && (ny&3)==0) continue;
361 score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
363 // if(nx&1) score-=1024*s->me.penalty_factor;
364 // if(ny&1) score-=1024*s->me.penalty_factor;
368 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
369 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
371 best_pos[i][0]= nx + 4*mx;
372 best_pos[i][1]= ny + 4*my;
380 const int cx = 4*(r - l);
381 const int cx2= r + l - 2*c;
382 const int cy = 4*(b - t);
383 const int cy2= b + t - 2*c;
386 if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
387 tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
389 CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
392 cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
394 assert(16*cx2 + 4*cx + 32*c == 32*r);
395 assert(16*cx2 - 4*cx + 32*c == 32*l);
396 assert(16*cy2 + 4*cy + 32*c == 32*b);
397 assert(16*cy2 - 4*cy + 32*c == 32*t);
398 assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
400 for(ny= -3; ny <= 3; ny++){
401 for(nx= -3; nx <= 3; nx++){
402 int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
405 if((nx&3)==0 && (ny&3)==0) continue;
407 score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
408 // if(nx&1) score-=32*s->me.penalty_factor;
409 // if(ny&1) score-=32*s->me.penalty_factor;
413 memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
414 memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
416 best_pos[i][0]= nx + 4*mx;
417 best_pos[i][1]= ny + 4*my;
424 for(i=0; i<subpel_quality; i++){
427 CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
431 const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
432 const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
433 const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
434 const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
435 // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
438 // nx= FFMAX(4*mx - bx, bx - 4*mx);
439 // ny= FFMAX(4*my - by, by - 4*my);
441 static int stats[7][7], count;
443 stats[4*mx - bx + 3][4*my - by + 3]++;
444 if(256*256*256*64 % count ==0){
446 if((i%7)==0) printf("\n");
447 printf("%6d ", stats[0][i]);
455 CHECK_QUARTER_MV(2, 2, mx-1, my-1)
456 CHECK_QUARTER_MV(0, 2, mx , my-1)
457 CHECK_QUARTER_MV(2, 2, mx , my-1)
458 CHECK_QUARTER_MV(2, 0, mx , my )
459 CHECK_QUARTER_MV(2, 2, mx , my )
460 CHECK_QUARTER_MV(0, 2, mx , my )
461 CHECK_QUARTER_MV(2, 2, mx-1, my )
462 CHECK_QUARTER_MV(2, 0, mx-1, my )
468 int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
469 int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
470 CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
475 CHECK_QUARTER_MV(1, 3, mx-1, my-1)
476 CHECK_QUARTER_MV(1, 2, mx-1, my-1)
477 CHECK_QUARTER_MV(1, 1, mx-1, my-1)
478 CHECK_QUARTER_MV(2, 1, mx-1, my-1)
479 CHECK_QUARTER_MV(3, 1, mx-1, my-1)
480 CHECK_QUARTER_MV(0, 1, mx , my-1)
481 CHECK_QUARTER_MV(1, 1, mx , my-1)
482 CHECK_QUARTER_MV(2, 1, mx , my-1)
483 CHECK_QUARTER_MV(3, 1, mx , my-1)
484 CHECK_QUARTER_MV(3, 2, mx , my-1)
485 CHECK_QUARTER_MV(3, 3, mx , my-1)
486 CHECK_QUARTER_MV(3, 0, mx , my )
487 CHECK_QUARTER_MV(3, 1, mx , my )
488 CHECK_QUARTER_MV(3, 2, mx , my )
489 CHECK_QUARTER_MV(3, 3, mx , my )
490 CHECK_QUARTER_MV(2, 3, mx , my )
491 CHECK_QUARTER_MV(1, 3, mx , my )
492 CHECK_QUARTER_MV(0, 3, mx , my )
493 CHECK_QUARTER_MV(3, 3, mx-1, my )
494 CHECK_QUARTER_MV(2, 3, mx-1, my )
495 CHECK_QUARTER_MV(1, 3, mx-1, my )
496 CHECK_QUARTER_MV(1, 2, mx-1, my )
497 CHECK_QUARTER_MV(1, 1, mx-1, my )
498 CHECK_QUARTER_MV(1, 0, mx-1, my )
500 assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
512 static int RENAME(qpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, uint8_t *src_data[3],
513 uint8_t *ref_data[3], int stride, int uvstride,
514 uint8_t * const mv_penalty)
518 const int penalty_factor= s->me.mb_penalty_factor;
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)\
562 const int x2= FFMAX(xmin, FFMIN(x, xmax));\
563 const int y2= FFMAX(ymin, FFMIN(y, ymax));\
567 #define CHECK_MV_DIR(x,y,new_dir)\
569 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
570 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
571 /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
572 if(map[index]!=key){\
575 score_map[index]= d;\
576 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
577 /*printf("score:%d\n", d);*/\
587 #define check(x,y,S,v)\
588 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
589 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
590 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
591 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
594 static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
595 uint8_t *src_data[3],
596 uint8_t *ref_data[3], int stride, int uvstride,
597 int const pred_x, int const pred_y, int const penalty_factor,
599 uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
602 me_cmp_func cmp, chroma_cmp;
606 cmp= s->dsp.me_cmp[size];
607 chroma_cmp= s->dsp.me_cmp[size+1];
609 { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
610 const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
611 const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
612 if(map[index]!=key){ //this will be executed only very rarey
613 CMP(score_map[index], best[0], best[1], size);
620 const int dir= next_dir;
621 const int x= best[0];
622 const int y= best[1];
626 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
627 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
628 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
629 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
637 static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
638 uint8_t *src_data[3],
639 uint8_t *ref_data[3], int stride, int uvstride,
640 int const pred_x, int const pred_y, int const penalty_factor,
642 uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
645 me_cmp_func cmp, chroma_cmp;
649 cmp= s->dsp.me_cmp[size];
650 chroma_cmp= s->dsp.me_cmp[size+1];
652 for(dia_size=1; dia_size<=4; dia_size++){
654 const int x= best[0];
655 const int y= best[1];
657 if(dia_size&(dia_size-1)) continue;
659 if( x + dia_size > xmax
660 || x - dia_size < xmin
661 || y + dia_size > ymax
662 || y - dia_size < ymin)
665 for(dir= 0; dir<dia_size; dir+=2){
668 CHECK_MV(x + dir , y + dia_size - dir);
669 CHECK_MV(x + dia_size - dir, y - dir );
670 CHECK_MV(x - dir , y - dia_size + dir);
671 CHECK_MV(x - dia_size + dir, y + dir );
674 if(x!=best[0] || y!=best[1])
679 static int stats[8*8];
683 dx^=dy; dy^=dx; dx^=dy;
686 if(256*256*256*64 % (stats[0]+1)==0){
688 if((i&7)==0) printf("\n");
689 printf("%8d ", stats[i]);
699 #define SAB_CHECK_MV(ax,ay)\
701 const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
702 const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
703 /*printf("sab check %d %d\n", ax, ay);*/\
704 if(map[index]!=key){\
705 CMP(d, ax, ay, size);\
707 score_map[index]= d;\
708 d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
709 /*printf("score: %d\n", d);*/\
710 if(d < minima[minima_count-1].height){\
713 while(d >= minima[j].height) j++;\
715 memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
717 minima[j].checked= 0;\
718 minima[j].height= d;\
728 #define MAX_SAB_SIZE 16
729 static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
730 uint8_t *src_data[3],
731 uint8_t *ref_data[3], int stride, int uvstride,
732 int const pred_x, int const pred_y, int const penalty_factor,
734 uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
737 me_cmp_func cmp, chroma_cmp;
738 Minima minima[MAX_SAB_SIZE];
739 const int minima_count= ABS(s->me.dia_size);
743 cmp= s->dsp.me_cmp[size];
744 chroma_cmp= s->dsp.me_cmp[size+1];
746 for(j=i=0; i<ME_MAP_SIZE; i++){
747 uint32_t key= map[i];
749 key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
751 if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
753 assert(j<MAX_SAB_SIZE); //max j = number of predictors
755 minima[j].height= score_map[i];
756 minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
757 minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
758 minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
759 minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
761 if(minima[j].x || minima[j].y)
762 minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
767 qsort(minima, j, sizeof(Minima), minima_cmp);
769 for(; j<minima_count; j++){
770 minima[j].height=256*256*256*64;
772 minima[j].x= minima[j].y=0;
775 for(i=0; i<minima_count; i++){
776 const int x= minima[i].x;
777 const int y= minima[i].y;
780 if(minima[i].checked) continue;
782 if( x >= xmax || x <= xmin
783 || y >= ymax || y <= ymin)
788 SAB_CHECK_MV(x , y-1)
789 SAB_CHECK_MV(x , y+1)
791 minima[i].checked= 1;
794 best[0]= minima[0].x;
795 best[1]= minima[0].y;
796 dmin= minima[0].height;
798 if( best[0] < xmax && best[0] > xmin
799 && best[1] < ymax && best[1] > ymin){
801 //ensure that the refernece samples for hpel refinement are in the map
802 CHECK_MV(best[0]-1, best[1])
803 CHECK_MV(best[0]+1, best[1])
804 CHECK_MV(best[0], best[1]-1)
805 CHECK_MV(best[0], best[1]+1)
810 static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
811 uint8_t *src_data[3],
812 uint8_t *ref_data[3], int stride, int uvstride,
813 int const pred_x, int const pred_y, int const penalty_factor,
815 uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
818 me_cmp_func cmp, chroma_cmp;
822 cmp= s->dsp.me_cmp[size];
823 chroma_cmp= s->dsp.me_cmp[size+1];
825 for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
827 const int x= best[0];
828 const int y= best[1];
830 start= FFMAX(0, y + dia_size - ymax);
831 end = FFMIN(dia_size, xmax - x + 1);
832 for(dir= start; dir<end; dir++){
835 //check(x + dir,y + dia_size - dir,0, a0)
836 CHECK_MV(x + dir , y + dia_size - dir);
839 start= FFMAX(0, x + dia_size - xmax);
840 end = FFMIN(dia_size, y - ymin + 1);
841 for(dir= start; dir<end; dir++){
844 //check(x + dia_size - dir, y - dir,0, a1)
845 CHECK_MV(x + dia_size - dir, y - dir );
848 start= FFMAX(0, -y + dia_size + ymin );
849 end = FFMIN(dia_size, x - xmin + 1);
850 for(dir= start; dir<end; dir++){
853 //check(x - dir,y - dia_size + dir,0, a2)
854 CHECK_MV(x - dir , y - dia_size + dir);
857 start= FFMAX(0, -x + dia_size + xmin );
858 end = FFMIN(dia_size, ymax - y + 1);
859 for(dir= start; dir<end; dir++){
862 //check(x - dia_size + dir, y + dir,0, a3)
863 CHECK_MV(x - dia_size + dir, y + dir );
866 if(x!=best[0] || y!=best[1])
871 static int stats[8*8];
875 if(256*256*256*64 % (stats[0]+1)==0){
877 if((i&7)==0) printf("\n");
878 printf("%6d ", stats[i]);
888 static int RENAME(epzs_motion_search)(MpegEncContext * s,
889 int *mx_ptr, int *my_ptr,
890 int P[10][2], int pred_x, int pred_y, uint8_t *src_data[3],
891 uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2],
892 int ref_mv_scale, uint8_t * const mv_penalty)
896 const int shift= 1+s->quarter_sample;
897 uint32_t *map= s->me.map;
899 const int penalty_factor= s->me.penalty_factor;
902 const int ref_mv_stride= s->mb_stride; //pass as arg FIXME
903 const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME
904 me_cmp_func cmp, chroma_cmp;
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;
917 if (s->first_slice_line) {
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 if(!s->first_slice_line)
944 CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
945 (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
947 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
948 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
949 if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
950 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
951 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
955 if(s->avctx->last_predictor_count){
956 const int count= s->avctx->last_predictor_count;
957 const int xstart= FFMAX(0, s->mb_x - count);
958 const int ystart= FFMAX(0, s->mb_y - count);
959 const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
960 const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
963 for(mb_y=ystart; mb_y<yend; mb_y++){
965 for(mb_x=xstart; mb_x<xend; mb_x++){
966 const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
967 int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
968 int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
970 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
976 //check(best[0],best[1],0, b0)
977 if(s->me.dia_size==-1)
978 dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
979 pred_x, pred_y, penalty_factor,
980 shift, map, map_generation, size, h, mv_penalty);
981 else if(s->me.dia_size<-1)
982 dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
983 pred_x, pred_y, penalty_factor,
984 shift, map, map_generation, size, h, mv_penalty);
985 else if(s->me.dia_size<2)
986 dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
987 pred_x, pred_y, penalty_factor,
988 shift, map, map_generation, size, h, mv_penalty);
990 dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
991 pred_x, pred_y, penalty_factor,
992 shift, map, map_generation, size, h, mv_penalty);
994 //check(best[0],best[1],0, b1)
998 // printf("%d %d %d \n", best[0], best[1], dmin);
1002 #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
1003 static int RENAME(epzs_motion_search4)(MpegEncContext * s,
1004 int *mx_ptr, int *my_ptr,
1005 int P[10][2], int pred_x, int pred_y,
1006 uint8_t *src_data[3],
1007 uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2],
1008 int ref_mv_scale, uint8_t * const mv_penalty)
1012 const int shift= 1+s->quarter_sample;
1013 uint32_t *map= s->me.map;
1015 const int penalty_factor= s->me.penalty_factor;
1018 const int ref_mv_stride= s->mb_stride;
1019 const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
1020 me_cmp_func cmp, chroma_cmp;
1023 cmp= s->dsp.me_cmp[size];
1024 chroma_cmp= s->dsp.me_cmp[size+1];
1026 map_generation= update_map_generation(s);
1029 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
1031 if (s->first_slice_line) {
1032 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[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)
1035 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1037 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1038 //FIXME try some early stop
1040 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
1041 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1042 CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
1043 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
1044 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
1045 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1049 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
1050 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
1051 if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
1052 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
1053 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
1056 if(s->me.dia_size==-1)
1057 dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
1058 pred_x, pred_y, penalty_factor,
1059 shift, map, map_generation, size, h, mv_penalty);
1060 else if(s->me.dia_size<-1)
1061 dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
1062 pred_x, pred_y, penalty_factor,
1063 shift, map, map_generation, size, h, mv_penalty);
1064 else if(s->me.dia_size<2)
1065 dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
1066 pred_x, pred_y, penalty_factor,
1067 shift, map, map_generation, size, h, mv_penalty);
1069 dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
1070 pred_x, pred_y, penalty_factor,
1071 shift, map, map_generation, size, h, mv_penalty);
1077 // printf("%d %d %d \n", best[0], best[1], dmin);
1081 //try to merge with above FIXME (needs PSNR test)
1082 static int RENAME(epzs_motion_search2)(MpegEncContext * s,
1083 int *mx_ptr, int *my_ptr,
1084 int P[10][2], int pred_x, int pred_y,
1085 uint8_t *src_data[3],
1086 uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2],
1087 int ref_mv_scale, uint8_t * const mv_penalty)
1091 const int shift= 1+s->quarter_sample;
1092 uint32_t *map= s->me.map;
1094 const int penalty_factor= s->me.penalty_factor;
1095 const int size=0; //FIXME pass as arg
1097 const int ref_mv_stride= s->mb_stride;
1098 const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
1099 me_cmp_func cmp, chroma_cmp;
1102 cmp= s->dsp.me_cmp[size];
1103 chroma_cmp= s->dsp.me_cmp[size+1];
1105 map_generation= update_map_generation(s);
1108 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
1110 if (s->first_slice_line) {
1111 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1112 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
1113 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1114 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1116 CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1117 //FIXME try some early stop
1119 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
1120 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1121 CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
1122 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
1123 CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
1124 (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1128 CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
1129 (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
1130 if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
1131 CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
1132 (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
1135 if(s->me.dia_size==-1)
1136 dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
1137 pred_x, pred_y, penalty_factor,
1138 shift, map, map_generation, size, h, mv_penalty);
1139 else if(s->me.dia_size<-1)
1140 dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
1141 pred_x, pred_y, penalty_factor,
1142 shift, map, map_generation, size, h, mv_penalty);
1143 else if(s->me.dia_size<2)
1144 dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
1145 pred_x, pred_y, penalty_factor,
1146 shift, map, map_generation, size, h, mv_penalty);
1148 dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
1149 pred_x, pred_y, penalty_factor,
1150 shift, map, map_generation, size, h, mv_penalty);
1156 // printf("%d %d %d \n", best[0], best[1], dmin);
1159 #endif /* !CMP_DIRECT */