]> git.sesse.net Git - ffmpeg/blob - libavcodec/motion_est_template.c
49c2e57b5cce298dc8e3211953ca454cdc9027cc
[ffmpeg] / libavcodec / motion_est_template.c
1 /*
2  * Motion estimation 
3  * Copyright (c) 2002-2004 Michael Niedermayer
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20  
21 /**
22  * @file motion_est_template.c
23  * Motion estimation template.
24  */
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
28 #define LOAD_COMMON\
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];\
54     }else{\
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];\
58     }
59
60
61 #ifdef CMP_HPEL
62     
63 #define CHECK_HALF_MV(dx, dy, x, y)\
64 {\
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)\
70 }
71
72 #if 0
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)
77 {
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;
83     
84     LOAD_COMMON
85     
86  //   INIT;
87  //FIXME factorize
88     me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
89
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];
93     }else{
94         hpel_put=& s->dsp.put_pixels_tab[size];
95         chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
96     }
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];
101
102     if(s->me.skip){ //FIXME somehow move up (benchmark)
103         *mx_ptr = 0;
104         *my_ptr = 0;
105         return dmin;
106     }
107         
108     if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
109         CMP_HPEL(dmin, 0, 0, mx, my, size);
110         if(mx || my)
111             dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
112     }
113         
114     if (mx > xmin && mx < xmax && 
115         my > ymin && my < ymax) {
116         int bx=2*mx, by=2*my;
117         int d= dmin;
118         
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  )
127
128         assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
129
130         *mx_ptr = bx;
131         *my_ptr = by;
132     }else{
133         *mx_ptr =2*mx;
134         *my_ptr =2*my;
135     }
136
137     return dmin;
138 }
139
140 #else
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)
146 {
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;
152
153     LOAD_COMMON
154     
155  //FIXME factorize
156
157     cmp_sub= s->dsp.me_sub_cmp[size];
158     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
159
160     if(s->me.skip){ //FIXME move out of hpel?
161         *mx_ptr = 0;
162         *my_ptr = 0;
163         return dmin;
164     }
165         
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;
170     }
171         
172     if (mx > xmin && mx < xmax && 
173         my > ymin && my < ymax) {
174         int d= dmin;
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;
184     
185 #if 1
186         int key;
187         int map_generation= s->me.map_generation;
188 #ifndef NDEBUG
189         uint32_t *map= s->me.map;
190 #endif
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);
199 #endif                
200         if(t<=b){
201             CHECK_HALF_MV(0, 1, mx  ,my-1)
202             if(l<=r){
203                 CHECK_HALF_MV(1, 1, mx-1, my-1)
204                 if(t+r<=b+l){
205                     CHECK_HALF_MV(1, 1, mx  , my-1)
206                 }else{
207                     CHECK_HALF_MV(1, 1, mx-1, my  )
208                 }
209                 CHECK_HALF_MV(1, 0, mx-1, my  )
210             }else{
211                 CHECK_HALF_MV(1, 1, mx  , my-1)
212                 if(t+l<=b+r){
213                     CHECK_HALF_MV(1, 1, mx-1, my-1)
214                 }else{
215                     CHECK_HALF_MV(1, 1, mx  , my  )
216                 }
217                 CHECK_HALF_MV(1, 0, mx  , my  )
218             }
219         }else{
220             if(l<=r){
221                 if(t+l<=b+r){
222                     CHECK_HALF_MV(1, 1, mx-1, my-1)
223                 }else{
224                     CHECK_HALF_MV(1, 1, mx  , my  )
225                 }
226                 CHECK_HALF_MV(1, 0, mx-1, my)
227                 CHECK_HALF_MV(1, 1, mx-1, my)
228             }else{
229                 if(t+r<=b+l){
230                     CHECK_HALF_MV(1, 1, mx  , my-1)
231                 }else{
232                     CHECK_HALF_MV(1, 1, mx-1, my)
233                 }
234                 CHECK_HALF_MV(1, 0, mx  , my)
235                 CHECK_HALF_MV(1, 1, mx  , my)
236             }
237             CHECK_HALF_MV(0, 1, mx  , my)
238         }
239         assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
240     }
241
242     *mx_ptr = bx;
243     *my_ptr = by;
244     
245     return dmin;
246 }
247 #endif
248
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)
252 {
253 //    const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
254     const int size= 0;
255     const int h= 16;
256     const int penalty_factor= s->me.mb_penalty_factor;
257     me_cmp_func cmp_sub, chroma_cmp_sub;
258     int d;
259
260     LOAD_COMMON
261     
262  //FIXME factorize
263
264     cmp_sub= s->dsp.mb_cmp[size];
265     chroma_cmp_sub= s->dsp.mb_cmp[size+1];
266     
267     assert(!s->me.skip);
268     assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
269
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;
274         
275     return d;
276 }
277
278 #endif /* CMP_HPEL */
279
280
281
282 #ifdef CMP_QPEL
283
284 #define CHECK_QUARTER_MV(dx, dy, x, y)\
285 {\
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)\
291 }
292
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)
298 {
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;
307
308     LOAD_COMMON
309     
310     cmp= s->dsp.me_cmp[size];
311     chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
312  //FIXME factorize
313
314     cmp_sub= s->dsp.me_sub_cmp[size];
315     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
316
317     if(s->me.skip){ //FIXME somehow move up (benchmark)
318         *mx_ptr = 0;
319         *my_ptr = 0;
320         return dmin;
321     }
322         
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;
327     }
328         
329     if (mx > xmin && mx < xmax && 
330         my > ymin && my < ymax) {
331         int bx=4*mx, by=4*my;
332         int d= dmin;
333         int i, nx, ny;
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)];
340         int best[8];
341         int best_pos[8][2];
342         
343         memset(best, 64, sizeof(int)*8);
344 #if 1
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)];
350
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;
357                     int i;
358                     
359                     if((nx&3)==0 && (ny&3)==0) continue;
360                     
361                     score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
362                     
363 //                    if(nx&1) score-=1024*s->me.penalty_factor;
364 //                    if(ny&1) score-=1024*s->me.penalty_factor;
365                     
366                     for(i=0; i<8; i++){
367                         if(score < best[i]){
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));
370                             best[i]= score;
371                             best_pos[i][0]= nx + 4*mx;
372                             best_pos[i][1]= ny + 4*my;
373                             break;
374                         }
375                     }
376                 }
377             }
378         }else{
379             int tl;
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;
384             int cxy;
385               
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)];
388             }else{
389                 CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
390             }
391             
392             cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c; 
393            
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);
399             
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
403                     int i;
404                     
405                     if((nx&3)==0 && (ny&3)==0) continue;
406                 
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;
410                     
411                     for(i=0; i<8; i++){
412                         if(score < best[i]){
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));
415                             best[i]= score;
416                             best_pos[i][0]= nx + 4*mx;
417                             best_pos[i][1]= ny + 4*my;
418                             break;
419                         }
420                     }
421                 }
422             }            
423         }
424         for(i=0; i<subpel_quality; i++){
425             nx= best_pos[i][0];
426             ny= best_pos[i][1];
427             CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
428         }
429
430 #if 0
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){
436             if(tl<br){
437
438 //            nx= FFMAX(4*mx - bx, bx - 4*mx);
439 //            ny= FFMAX(4*my - by, by - 4*my);
440             
441             static int stats[7][7], count;
442             count++;
443             stats[4*mx - bx + 3][4*my - by + 3]++;
444             if(256*256*256*64 % count ==0){
445                 for(i=0; i<49; i++){
446                     if((i%7)==0) printf("\n");
447                     printf("%6d ", stats[0][i]);
448                 }
449                 printf("\n");
450             }
451             }
452 #endif
453 #else
454
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  )
463         
464         nx= bx;
465         ny= by;
466         
467         for(i=0; i<8; i++){
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)
471         }
472 #endif
473 #if 0
474         //outer ring
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  )
499 #endif
500         assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
501
502         *mx_ptr = bx;
503         *my_ptr = by;
504     }else{
505         *mx_ptr =4*mx;
506         *my_ptr =4*my;
507     }
508
509     return dmin;
510 }
511
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)
515 {
516     const int size= 0;
517     const int h= 16;
518     const int penalty_factor= s->me.mb_penalty_factor;
519     me_cmp_func cmp_sub, chroma_cmp_sub;
520     int d;
521
522     LOAD_COMMON
523     
524  //FIXME factorize
525
526     cmp_sub= s->dsp.mb_cmp[size];
527     chroma_cmp_sub= s->dsp.mb_cmp[size+1];
528     
529     assert(!s->me.skip);
530     assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
531
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;
536         
537     return d;
538 }
539
540
541 #endif /* CMP_QPEL */
542
543 #define CHECK_MV(x,y)\
544 {\
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){\
549         CMP(d, x, y, size);\
550         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)\
555     }\
556 }
557
558 #define CHECK_CLIPED_MV(ax,ay)\
559 {\
560     const int x= ax;\
561     const int y= ay;\
562     const int x2= FFMAX(xmin, FFMIN(x, xmax));\
563     const int y2= FFMAX(ymin, FFMIN(y, ymax));\
564     CHECK_MV(x2, y2)\
565 }
566
567 #define CHECK_MV_DIR(x,y,new_dir)\
568 {\
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){\
573         CMP(d, x, y, size);\
574         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);*/\
578         if(d<dmin){\
579             best[0]=x;\
580             best[1]=y;\
581             dmin=d;\
582             next_dir= new_dir;\
583         }\
584     }\
585 }
586
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);\
592
593
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,
598                                        int const shift,
599                                        uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
600                                        )
601 {
602     me_cmp_func cmp, chroma_cmp;
603     int next_dir=-1;
604     LOAD_COMMON
605     
606     cmp= s->dsp.me_cmp[size];
607     chroma_cmp= s->dsp.me_cmp[size+1];
608
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);
614             map[index]= key;
615         }
616     }
617
618     for(;;){
619         int d;
620         const int dir= next_dir;
621         const int x= best[0];
622         const int y= best[1];
623         next_dir=-1;
624
625 //printf("%d", dir);
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)
630
631         if(next_dir==-1){
632             return dmin;
633         }
634     }
635 }
636
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,
641                                        int const shift,
642                                        uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
643                                        )
644 {
645     me_cmp_func cmp, chroma_cmp;
646     int dia_size;
647     LOAD_COMMON
648     
649     cmp= s->dsp.me_cmp[size];
650     chroma_cmp= s->dsp.me_cmp[size+1];
651
652     for(dia_size=1; dia_size<=4; dia_size++){
653         int dir;
654         const int x= best[0];
655         const int y= best[1];
656         
657         if(dia_size&(dia_size-1)) continue;
658
659         if(   x + dia_size > xmax
660            || x - dia_size < xmin
661            || y + dia_size > ymax
662            || y - dia_size < ymin)
663            continue;
664         
665         for(dir= 0; dir<dia_size; dir+=2){
666             int d;
667
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           );
672         }
673
674         if(x!=best[0] || y!=best[1])
675             dia_size=0;
676 #if 0
677 {
678 int dx, dy, i;
679 static int stats[8*8];
680 dx= ABS(x-best[0]);
681 dy= ABS(y-best[1]);
682 if(dy>dx){
683     dx^=dy; dy^=dx; dx^=dy;
684 }
685 stats[dy*8 + dx] ++;
686 if(256*256*256*64 % (stats[0]+1)==0){
687     for(i=0; i<64; i++){
688         if((i&7)==0) printf("\n");
689         printf("%8d ", stats[i]);
690     }
691     printf("\n");
692 }
693 }
694 #endif
695     }
696     return dmin;    
697 }
698
699 #define SAB_CHECK_MV(ax,ay)\
700 {\
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);\
706         map[index]= key;\
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){\
711             int j=0;\
712             \
713             while(d >= minima[j].height) j++;\
714 \
715             memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
716 \
717             minima[j].checked= 0;\
718             minima[j].height= d;\
719             minima[j].x= ax;\
720             minima[j].y= ay;\
721             \
722             i=-1;\
723             continue;\
724         }\
725     }\
726 }
727
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,
733                                        int const shift,
734                                        uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
735                                        )
736 {
737     me_cmp_func cmp, chroma_cmp;
738     Minima minima[MAX_SAB_SIZE];
739     const int minima_count= ABS(s->me.dia_size);
740     int i, j;
741     LOAD_COMMON
742     
743     cmp= s->dsp.me_cmp[size];
744     chroma_cmp= s->dsp.me_cmp[size+1];
745     
746     for(j=i=0; i<ME_MAP_SIZE; i++){
747         uint32_t key= map[i];
748
749         key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
750         
751         if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
752         
753         assert(j<MAX_SAB_SIZE); //max j = number of predictors
754         
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));
760         minima[j].checked=0;
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;
763         
764         j++;
765     }
766     
767     qsort(minima, j, sizeof(Minima), minima_cmp);
768     
769     for(; j<minima_count; j++){
770         minima[j].height=256*256*256*64;
771         minima[j].checked=0;
772         minima[j].x= minima[j].y=0;
773     }
774     
775     for(i=0; i<minima_count; i++){
776         const int x= minima[i].x;
777         const int y= minima[i].y;
778         int d;
779         
780         if(minima[i].checked) continue;
781         
782         if(   x >= xmax || x <= xmin
783            || y >= ymax || y <= ymin)
784            continue;
785
786         SAB_CHECK_MV(x-1, y)
787         SAB_CHECK_MV(x+1, y)
788         SAB_CHECK_MV(x  , y-1)
789         SAB_CHECK_MV(x  , y+1)
790         
791         minima[i].checked= 1;
792     }
793     
794     best[0]= minima[0].x;
795     best[1]= minima[0].y;
796     dmin= minima[0].height;
797     
798     if(   best[0] < xmax && best[0] > xmin
799        && best[1] < ymax && best[1] > ymin){
800         int d;
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)
806     }
807     return dmin;    
808 }
809
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,
814                                        int const shift,
815                                        uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
816                                        )
817 {
818     me_cmp_func cmp, chroma_cmp;
819     int dia_size;
820     LOAD_COMMON
821     
822     cmp= s->dsp.me_cmp[size];
823     chroma_cmp= s->dsp.me_cmp[size+1];
824
825     for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
826         int dir, start, end;
827         const int x= best[0];
828         const int y= best[1];
829
830         start= FFMAX(0, y + dia_size - ymax);
831         end  = FFMIN(dia_size, xmax - x + 1);
832         for(dir= start; dir<end; dir++){
833             int d;
834
835 //check(x + dir,y + dia_size - dir,0, a0)
836             CHECK_MV(x + dir           , y + dia_size - dir);
837         }
838
839         start= FFMAX(0, x + dia_size - xmax);
840         end  = FFMIN(dia_size, y - ymin + 1);
841         for(dir= start; dir<end; dir++){
842             int d;
843
844 //check(x + dia_size - dir, y - dir,0, a1)
845             CHECK_MV(x + dia_size - dir, y - dir           );
846         }
847
848         start= FFMAX(0, -y + dia_size + ymin );
849         end  = FFMIN(dia_size, x - xmin + 1);
850         for(dir= start; dir<end; dir++){
851             int d;
852
853 //check(x - dir,y - dia_size + dir,0, a2)
854             CHECK_MV(x - dir           , y - dia_size + dir);
855         }
856
857         start= FFMAX(0, -x + dia_size + xmin );
858         end  = FFMIN(dia_size, ymax - y + 1);
859         for(dir= start; dir<end; dir++){
860             int d;
861
862 //check(x - dia_size + dir, y + dir,0, a3)
863             CHECK_MV(x - dia_size + dir, y + dir           );
864         }
865
866         if(x!=best[0] || y!=best[1])
867             dia_size=0;
868 #if 0
869 {
870 int dx, dy, i;
871 static int stats[8*8];
872 dx= ABS(x-best[0]);
873 dy= ABS(y-best[1]);
874 stats[dy*8 + dx] ++;
875 if(256*256*256*64 % (stats[0]+1)==0){
876     for(i=0; i<64; i++){
877         if((i&7)==0) printf("\n");
878         printf("%6d ", stats[i]);
879     }
880     printf("\n");
881 }
882 }
883 #endif
884     }
885     return dmin;    
886 }
887
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)
893 {
894     int best[2]={0, 0};
895     int d, dmin; 
896     const int shift= 1+s->quarter_sample;
897     uint32_t *map= s->me.map;
898     int map_generation;
899     const int penalty_factor= s->me.penalty_factor;
900     const int size=0;
901     const int h=16;
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;
905     LOAD_COMMON
906     
907     cmp= s->dsp.me_cmp[size];
908     chroma_cmp= s->dsp.me_cmp[size+1];
909     
910     map_generation= update_map_generation(s);
911
912     CMP(dmin, 0, 0, size);
913     map[0]= map_generation;
914     score_map[0]= dmin;
915
916     /* first line */
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)
921     }else{
922         if(dmin<256 && ( P_LEFT[0]    |P_LEFT[1]
923                         |P_TOP[0]     |P_TOP[1]
924                         |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
925             *mx_ptr= 0;
926             *my_ptr= 0;
927             s->me.skip=1;
928             return dmin;
929         }
930         CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
931         if(dmin>256*2){
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)
937         }
938     }
939     if(dmin>256*4){
940         if(s->me.pre_pass){
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)
946         }else{
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)
952         }
953     }
954
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);
961         int mb_y;
962
963         for(mb_y=ystart; mb_y<yend; mb_y++){
964             int mb_x;
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;
969
970                 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
971                 CHECK_MV(mx,my)
972             }
973         }
974     }
975
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);
989     else
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);
993
994 //check(best[0],best[1],0, b1)
995     *mx_ptr= best[0];
996     *my_ptr= best[1];    
997
998 //    printf("%d %d %d \n", best[0], best[1], dmin);
999     return dmin;
1000 }
1001
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)
1009 {
1010     int best[2]={0, 0};
1011     int d, dmin; 
1012     const int shift= 1+s->quarter_sample;
1013     uint32_t *map= s->me.map;
1014     int map_generation;
1015     const int penalty_factor= s->me.penalty_factor;
1016     const int size=1;
1017     const int h=8;
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;
1021     LOAD_COMMON
1022     
1023     cmp= s->dsp.me_cmp[size];
1024     chroma_cmp= s->dsp.me_cmp[size+1];
1025
1026     map_generation= update_map_generation(s);
1027
1028     dmin = 1000000;
1029 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); 
1030     /* first line */
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)
1036     }else{
1037         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1038         //FIXME try some early stop
1039         if(dmin>64*2){
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)
1046         }
1047     }
1048     if(dmin>64*4){
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)
1054     }
1055
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);
1068     else
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);
1072
1073
1074     *mx_ptr= best[0];
1075     *my_ptr= best[1];    
1076
1077 //    printf("%d %d %d \n", best[0], best[1], dmin);
1078     return dmin;
1079 }
1080
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)
1088 {
1089     int best[2]={0, 0};
1090     int d, dmin; 
1091     const int shift= 1+s->quarter_sample;
1092     uint32_t *map= s->me.map;
1093     int map_generation;
1094     const int penalty_factor= s->me.penalty_factor;
1095     const int size=0; //FIXME pass as arg
1096     const int h=8;
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;
1100     LOAD_COMMON
1101     
1102     cmp= s->dsp.me_cmp[size];
1103     chroma_cmp= s->dsp.me_cmp[size+1];
1104
1105     map_generation= update_map_generation(s);
1106
1107     dmin = 1000000;
1108 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); 
1109     /* first line */
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)
1115     }else{
1116         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1117         //FIXME try some early stop
1118         if(dmin>64*2){
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)
1125         }
1126     }
1127     if(dmin>64*4){
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)
1133     }
1134
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);
1147     else
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);
1151
1152
1153     *mx_ptr= best[0];
1154     *my_ptr= best[1];    
1155
1156 //    printf("%d %d %d \n", best[0], best[1], dmin);
1157     return dmin;
1158 }
1159 #endif /* !CMP_DIRECT */