]> git.sesse.net Git - ffmpeg/blob - libavcodec/motion_est_template.c
trying to fix mb skip bug in mpeg1/2 if slices are not used
[ffmpeg] / libavcodec / motion_est_template.c
1 /*
2  * Motion estimation 
3  * Copyright (c) 2002 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 //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
22 //Note, the last line is there to kill these ugly unused var warnings
23 #define LOAD_COMMON(x, y)\
24     uint32_t * const score_map= s->me.score_map;\
25     const int stride= s->linesize;\
26     const int uvstride= s->uvlinesize;\
27     const int time_pp= s->pp_time;\
28     const int time_pb= s->pb_time;\
29     uint8_t * const src_y= s->new_picture.data[0] + ((y) * stride) + (x);\
30     uint8_t * const src_u= s->new_picture.data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
31     uint8_t * const src_v= s->new_picture.data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
32     uint8_t * const ref_y= ref_picture->data[0] + ((y) * stride) + (x);\
33     uint8_t * const ref_u= ref_picture->data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
34     uint8_t * const ref_v= ref_picture->data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
35     uint8_t * const ref2_y= s->next_picture.data[0] + ((y) * stride) + (x);\
36     op_pixels_func (*hpel_put)[4];\
37     op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\
38     op_pixels_func (*chroma_hpel_put)[4];\
39     qpel_mc_func (*qpel_put)[16];\
40     qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\
41     const __attribute__((unused)) int unu= time_pp + time_pb + (int)src_u + (int)src_v + (int)ref_u + (int)ref_v\
42                                            + (int)ref2_y + (int)hpel_avg + (int)qpel_avg + (int)score_map;\
43     if(s->no_rounding /*FIXME b_type*/){\
44         hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\
45         chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\
46         qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];\
47     }else{\
48         hpel_put=& s->dsp.put_pixels_tab[size];\
49         chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];\
50         qpel_put= &s->dsp.put_qpel_pixels_tab[size];\
51     }
52
53
54 #ifdef CMP_HPEL
55     
56 #define CHECK_HALF_MV(dx, dy, x, y)\
57 {\
58     const int hx= 2*(x)+(dx);\
59     const int hy= 2*(y)+(dy);\
60     CMP_HPEL(d, dx, dy, x, y, size);\
61     d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
62     COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
63 }
64
65 #if 0
66 static int RENAME(hpel_motion_search)(MpegEncContext * s,
67                                   int *mx_ptr, int *my_ptr, int dmin,
68                                   int xmin, int ymin, int xmax, int ymax,
69                                   int pred_x, int pred_y, Picture *ref_picture, 
70                                   int n, int size, uint16_t * const mv_penalty)
71 {
72     const int xx = 16 * s->mb_x + 8*(n&1);
73     const int yy = 16 * s->mb_y + 8*(n>>1);
74     const int mx = *mx_ptr;
75     const int my = *my_ptr;
76     const int penalty_factor= s->me.sub_penalty_factor;
77     
78     LOAD_COMMON(xx, yy);
79     
80  //   INIT;
81  //FIXME factorize
82     me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
83
84     if(s->no_rounding /*FIXME b_type*/){
85         hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
86         chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
87     }else{
88         hpel_put=& s->dsp.put_pixels_tab[size];
89         chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
90     }
91     cmp= s->dsp.me_cmp[size];
92     chroma_cmp= s->dsp.me_cmp[size+1];
93     cmp_sub= s->dsp.me_sub_cmp[size];
94     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
95
96     if(s->me.skip){ //FIXME somehow move up (benchmark)
97         *mx_ptr = 0;
98         *my_ptr = 0;
99         return dmin;
100     }
101         
102     if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
103         CMP_HPEL(dmin, 0, 0, mx, my, size);
104         if(mx || my)
105             dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
106     }
107         
108     if (mx > xmin && mx < xmax && 
109         my > ymin && my < ymax) {
110         int bx=2*mx, by=2*my;
111         int d= dmin;
112         
113         CHECK_HALF_MV(1, 1, mx-1, my-1)
114         CHECK_HALF_MV(0, 1, mx  , my-1)        
115         CHECK_HALF_MV(1, 1, mx  , my-1)
116         CHECK_HALF_MV(1, 0, mx-1, my  )
117         CHECK_HALF_MV(1, 0, mx  , my  )
118         CHECK_HALF_MV(1, 1, mx-1, my  )
119         CHECK_HALF_MV(0, 1, mx  , my  )        
120         CHECK_HALF_MV(1, 1, mx  , my  )
121
122         assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
123
124         *mx_ptr = bx;
125         *my_ptr = by;
126     }else{
127         *mx_ptr =2*mx;
128         *my_ptr =2*my;
129     }
130
131     return dmin;
132 }
133
134 #else
135 static int RENAME(hpel_motion_search)(MpegEncContext * s,
136                                   int *mx_ptr, int *my_ptr, int dmin,
137                                   int xmin, int ymin, int xmax, int ymax,
138                                   int pred_x, int pred_y, Picture *ref_picture, 
139                                   int n, int size, uint16_t * const mv_penalty)
140 {
141     const int xx = 16 * s->mb_x + 8*(n&1);
142     const int yy = 16 * s->mb_y + 8*(n>>1);
143     const int mx = *mx_ptr;
144     const int my = *my_ptr;   
145     const int penalty_factor= s->me.sub_penalty_factor;
146     me_cmp_func cmp_sub, chroma_cmp_sub;
147     int bx=2*mx, by=2*my;
148
149     LOAD_COMMON(xx, yy);
150     
151  //FIXME factorize
152
153     cmp_sub= s->dsp.me_sub_cmp[size];
154     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
155
156     if(s->me.skip){ //FIXME move out of hpel?
157         *mx_ptr = 0;
158         *my_ptr = 0;
159         return dmin;
160     }
161         
162     if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
163         CMP_HPEL(dmin, 0, 0, mx, my, size);
164         if(mx || my || size>0)
165             dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
166     }
167         
168     if (mx > xmin && mx < xmax && 
169         my > ymin && my < ymax) {
170         int d= dmin;
171         const int index= (my<<ME_MAP_SHIFT) + mx;
172         const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] 
173                      + (mv_penalty[bx   - pred_x] + mv_penalty[by-2 - pred_y])*s->me.penalty_factor;
174         const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)]
175                      + (mv_penalty[bx-2 - pred_x] + mv_penalty[by   - pred_y])*s->me.penalty_factor;
176         const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)]
177                      + (mv_penalty[bx+2 - pred_x] + mv_penalty[by   - pred_y])*s->me.penalty_factor;
178         const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
179                      + (mv_penalty[bx   - pred_x] + mv_penalty[by+2 - pred_y])*s->me.penalty_factor;
180     
181 #if 1
182         int key;
183         int map_generation= s->me.map_generation;
184         uint32_t *map= s->me.map;
185         key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
186         assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
187         key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
188         assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
189         key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
190         assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
191         key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
192         assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
193 #endif                
194         if(t<=b){
195             CHECK_HALF_MV(0, 1, mx  ,my-1)
196             if(l<=r){
197                 CHECK_HALF_MV(1, 1, mx-1, my-1)
198                 if(t+r<=b+l){
199                     CHECK_HALF_MV(1, 1, mx  , my-1)
200                 }else{
201                     CHECK_HALF_MV(1, 1, mx-1, my  )
202                 }
203                 CHECK_HALF_MV(1, 0, mx-1, my  )
204             }else{
205                 CHECK_HALF_MV(1, 1, mx  , my-1)
206                 if(t+l<=b+r){
207                     CHECK_HALF_MV(1, 1, mx-1, my-1)
208                 }else{
209                     CHECK_HALF_MV(1, 1, mx  , my  )
210                 }
211                 CHECK_HALF_MV(1, 0, mx  , my  )
212             }
213         }else{
214             if(l<=r){
215                 if(t+l<=b+r){
216                     CHECK_HALF_MV(1, 1, mx-1, my-1)
217                 }else{
218                     CHECK_HALF_MV(1, 1, mx  , my  )
219                 }
220                 CHECK_HALF_MV(1, 0, mx-1, my)
221                 CHECK_HALF_MV(1, 1, mx-1, my)
222             }else{
223                 if(t+r<=b+l){
224                     CHECK_HALF_MV(1, 1, mx  , my-1)
225                 }else{
226                     CHECK_HALF_MV(1, 1, mx-1, my)
227                 }
228                 CHECK_HALF_MV(1, 0, mx  , my)
229                 CHECK_HALF_MV(1, 1, mx  , my)
230             }
231             CHECK_HALF_MV(0, 1, mx  , my)
232         }
233         assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
234     }
235
236     *mx_ptr = bx;
237     *my_ptr = by;
238     
239     return dmin;
240 }
241 #endif
242
243 static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture, 
244                                   uint16_t * const mv_penalty)
245 {
246 //    const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
247     const int size= 0;
248     const int xx = 16 * s->mb_x;
249     const int yy = 16 * s->mb_y;
250     const int penalty_factor= s->me.mb_penalty_factor;
251     const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these
252     const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit
253     me_cmp_func cmp_sub, chroma_cmp_sub;
254     int d;
255
256     LOAD_COMMON(xx, yy);
257     
258  //FIXME factorize
259
260     cmp_sub= s->dsp.mb_cmp[size];
261     chroma_cmp_sub= s->dsp.mb_cmp[size+1];
262     
263     assert(!s->me.skip);
264     assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
265
266     CMP_HPEL(d, mx&1, my&1, mx>>1, my>>1, size);
267     //FIXME check cbp before adding penalty for (0,0) vector
268     if(mx || my || size>0)
269         d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
270         
271     return d;
272 }
273
274 #endif /* CMP_HPEL */
275
276
277
278 #ifdef CMP_QPEL
279
280 #define CHECK_QUARTER_MV(dx, dy, x, y)\
281 {\
282     const int hx= 4*(x)+(dx);\
283     const int hy= 4*(y)+(dy);\
284     CMP_QPEL(d, dx, dy, x, y, size);\
285     d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
286     COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
287 }
288
289 static int RENAME(qpel_motion_search)(MpegEncContext * s,
290                                   int *mx_ptr, int *my_ptr, int dmin,
291                                   int xmin, int ymin, int xmax, int ymax,
292                                   int pred_x, int pred_y, Picture *ref_picture, 
293                                   int n, int size, uint16_t * const mv_penalty)
294 {
295     const int xx = 16 * s->mb_x + 8*(n&1);
296     const int yy = 16 * s->mb_y + 8*(n>>1);
297     const int mx = *mx_ptr;
298     const int my = *my_ptr;   
299     const int penalty_factor= s->me.sub_penalty_factor;
300     const int map_generation= s->me.map_generation;
301     const int subpel_quality= s->avctx->me_subpel_quality;
302     uint32_t *map= s->me.map;
303     me_cmp_func cmp, chroma_cmp;
304     me_cmp_func cmp_sub, chroma_cmp_sub;
305
306     LOAD_COMMON(xx, yy);
307     
308     cmp= s->dsp.me_cmp[size];
309     chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
310  //FIXME factorize
311
312     cmp_sub= s->dsp.me_sub_cmp[size];
313     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
314
315     if(s->me.skip){ //FIXME somehow move up (benchmark)
316         *mx_ptr = 0;
317         *my_ptr = 0;
318         return dmin;
319     }
320         
321     if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
322         CMP_QPEL(dmin, 0, 0, mx, my, size);
323         if(mx || my || size>0)
324             dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
325     }
326         
327     if (mx > xmin && mx < xmax && 
328         my > ymin && my < ymax) {
329         int bx=4*mx, by=4*my;
330         int d= dmin;
331         int i, nx, ny;
332         const int index= (my<<ME_MAP_SHIFT) + mx;
333         const int t= score_map[(index-(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
334         const int l= score_map[(index- 1                 )&(ME_MAP_SIZE-1)];
335         const int r= score_map[(index+ 1                 )&(ME_MAP_SIZE-1)];
336         const int b= score_map[(index+(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
337         const int c= score_map[(index                    )&(ME_MAP_SIZE-1)];
338         int best[8];
339         int best_pos[8][2];
340         
341         memset(best, 64, sizeof(int)*8);
342 #if 1
343         if(s->me.dia_size>=2){        
344             const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
345             const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
346             const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
347             const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
348
349             for(ny= -3; ny <= 3; ny++){
350                 for(nx= -3; nx <= 3; nx++){
351                     const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
352                     const int c2= nx*nx*( r +  l - 2*c) + 4*nx*( r- l) + 32*c;
353                     const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
354                     int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
355                     int i;
356                     
357                     if((nx&3)==0 && (ny&3)==0) continue;
358                     
359                     score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
360                     
361 //                    if(nx&1) score-=1024*s->me.penalty_factor;
362 //                    if(ny&1) score-=1024*s->me.penalty_factor;
363                     
364                     for(i=0; i<8; i++){
365                         if(score < best[i]){
366                             memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
367                             memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
368                             best[i]= score;
369                             best_pos[i][0]= nx + 4*mx;
370                             best_pos[i][1]= ny + 4*my;
371                             break;
372                         }
373                     }
374                 }
375             }
376         }else{
377             int tl;
378             const int cx = 4*(r - l);
379             const int cx2= r + l - 2*c; 
380             const int cy = 4*(b - t);
381             const int cy2= b + t - 2*c;
382             int cxy;
383               
384             if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
385                 tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
386             }else{
387                 CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
388             }
389             
390             cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c; 
391            
392             assert(16*cx2 + 4*cx + 32*c == 32*r);
393             assert(16*cx2 - 4*cx + 32*c == 32*l);
394             assert(16*cy2 + 4*cy + 32*c == 32*b);
395             assert(16*cy2 - 4*cy + 32*c == 32*t);
396             assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
397             
398             for(ny= -3; ny <= 3; ny++){
399                 for(nx= -3; nx <= 3; nx++){
400                     int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
401                     int i;
402                     
403                     if((nx&3)==0 && (ny&3)==0) continue;
404                 
405                     score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
406 //                    if(nx&1) score-=32*s->me.penalty_factor;
407   //                  if(ny&1) score-=32*s->me.penalty_factor;
408                     
409                     for(i=0; i<8; i++){
410                         if(score < best[i]){
411                             memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
412                             memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
413                             best[i]= score;
414                             best_pos[i][0]= nx + 4*mx;
415                             best_pos[i][1]= ny + 4*my;
416                             break;
417                         }
418                     }
419                 }
420             }            
421         }
422         for(i=0; i<subpel_quality; i++){
423             nx= best_pos[i][0];
424             ny= best_pos[i][1];
425             CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
426         }
427
428 #if 0
429             const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
430             const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
431             const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
432             const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
433 //            if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
434             if(tl<br){
435
436 //            nx= FFMAX(4*mx - bx, bx - 4*mx);
437 //            ny= FFMAX(4*my - by, by - 4*my);
438             
439             static int stats[7][7], count;
440             count++;
441             stats[4*mx - bx + 3][4*my - by + 3]++;
442             if(256*256*256*64 % count ==0){
443                 for(i=0; i<49; i++){
444                     if((i%7)==0) printf("\n");
445                     printf("%6d ", stats[0][i]);
446                 }
447                 printf("\n");
448             }
449             }
450 #endif
451 #else
452
453         CHECK_QUARTER_MV(2, 2, mx-1, my-1)
454         CHECK_QUARTER_MV(0, 2, mx  , my-1)        
455         CHECK_QUARTER_MV(2, 2, mx  , my-1)
456         CHECK_QUARTER_MV(2, 0, mx  , my  )
457         CHECK_QUARTER_MV(2, 2, mx  , my  )
458         CHECK_QUARTER_MV(0, 2, mx  , my  )
459         CHECK_QUARTER_MV(2, 2, mx-1, my  )
460         CHECK_QUARTER_MV(2, 0, mx-1, my  )
461         
462         nx= bx;
463         ny= by;
464         
465         for(i=0; i<8; i++){
466             int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
467             int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
468             CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
469         }
470 #endif
471 #if 0
472         //outer ring
473         CHECK_QUARTER_MV(1, 3, mx-1, my-1)
474         CHECK_QUARTER_MV(1, 2, mx-1, my-1)
475         CHECK_QUARTER_MV(1, 1, mx-1, my-1)
476         CHECK_QUARTER_MV(2, 1, mx-1, my-1)
477         CHECK_QUARTER_MV(3, 1, mx-1, my-1)
478         CHECK_QUARTER_MV(0, 1, mx  , my-1)
479         CHECK_QUARTER_MV(1, 1, mx  , my-1)
480         CHECK_QUARTER_MV(2, 1, mx  , my-1)
481         CHECK_QUARTER_MV(3, 1, mx  , my-1)
482         CHECK_QUARTER_MV(3, 2, mx  , my-1)
483         CHECK_QUARTER_MV(3, 3, mx  , my-1)
484         CHECK_QUARTER_MV(3, 0, mx  , my  )
485         CHECK_QUARTER_MV(3, 1, mx  , my  )
486         CHECK_QUARTER_MV(3, 2, mx  , my  )
487         CHECK_QUARTER_MV(3, 3, mx  , my  )
488         CHECK_QUARTER_MV(2, 3, mx  , my  )
489         CHECK_QUARTER_MV(1, 3, mx  , my  )
490         CHECK_QUARTER_MV(0, 3, mx  , my  )
491         CHECK_QUARTER_MV(3, 3, mx-1, my  )
492         CHECK_QUARTER_MV(2, 3, mx-1, my  )
493         CHECK_QUARTER_MV(1, 3, mx-1, my  )
494         CHECK_QUARTER_MV(1, 2, mx-1, my  )
495         CHECK_QUARTER_MV(1, 1, mx-1, my  )
496         CHECK_QUARTER_MV(1, 0, mx-1, my  )
497 #endif
498         assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
499
500         *mx_ptr = bx;
501         *my_ptr = by;
502     }else{
503         *mx_ptr =4*mx;
504         *my_ptr =4*my;
505     }
506
507     return dmin;
508 }
509
510 static int RENAME(qpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture, 
511                                   uint16_t * const mv_penalty)
512 {
513     const int size= 0;
514     const int xx = 16 * s->mb_x;
515     const int yy = 16 * s->mb_y;
516     const int penalty_factor= s->me.mb_penalty_factor;
517     const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these
518     const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit
519     me_cmp_func cmp_sub, chroma_cmp_sub;
520     int d;
521
522     LOAD_COMMON(xx, yy);
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= FFMAX(xmin, FFMIN(ax, xmax));\
561     const int y= FFMAX(ymin, FFMIN(ay, ymax));\
562     CHECK_MV(x, y)\
563 }
564
565 #define CHECK_MV_DIR(x,y,new_dir)\
566 {\
567     const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
568     const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
569 /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
570     if(map[index]!=key){\
571         CMP(d, x, y, size);\
572         map[index]= key;\
573         score_map[index]= d;\
574         d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
575 /*printf("score:%d\n", d);*/\
576         if(d<dmin){\
577             best[0]=x;\
578             best[1]=y;\
579             dmin=d;\
580             next_dir= new_dir;\
581         }\
582     }\
583 }
584
585 #define check(x,y,S,v)\
586 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
587 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
588 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
589 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
590
591
592 static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
593                                        Picture *ref_picture,
594                                        int const pred_x, int const pred_y, int const penalty_factor,
595                                        int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
596                                        uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
597                                        )
598 {
599     me_cmp_func cmp, chroma_cmp;
600     int next_dir=-1;
601     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
602     
603     cmp= s->dsp.me_cmp[size];
604     chroma_cmp= s->dsp.me_cmp[size+1];
605
606     { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
607         const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
608         const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
609         if(map[index]!=key){ //this will be executed only very rarey
610             CMP(score_map[index], best[0], best[1], size);
611             map[index]= key;
612         }
613     }
614
615     for(;;){
616         int d;
617         const int dir= next_dir;
618         const int x= best[0];
619         const int y= best[1];
620         next_dir=-1;
621
622 //printf("%d", dir);
623         if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y  , 0)
624         if(dir!=3 && y>ymin) CHECK_MV_DIR(x  , y-1, 1)
625         if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y  , 2)
626         if(dir!=1 && y<ymax) CHECK_MV_DIR(x  , y+1, 3)
627
628         if(next_dir==-1){
629             return dmin;
630         }
631     }
632 }
633
634 static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
635                                        Picture *ref_picture,
636                                        int const pred_x, int const pred_y, int const penalty_factor,
637                                        int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
638                                        uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
639                                        )
640 {
641     me_cmp_func cmp, chroma_cmp;
642     int dia_size;
643     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
644     
645     cmp= s->dsp.me_cmp[size];
646     chroma_cmp= s->dsp.me_cmp[size+1];
647
648     for(dia_size=1; dia_size<=4; dia_size++){
649         int dir;
650         const int x= best[0];
651         const int y= best[1];
652         
653         if(dia_size&(dia_size-1)) continue;
654
655         if(   x + dia_size > xmax
656            || x - dia_size < xmin
657            || y + dia_size > ymax
658            || y - dia_size < ymin)
659            continue;
660         
661         for(dir= 0; dir<dia_size; dir+=2){
662             int d;
663
664             CHECK_MV(x + dir           , y + dia_size - dir);
665             CHECK_MV(x + dia_size - dir, y - dir           );
666             CHECK_MV(x - dir           , y - dia_size + dir);
667             CHECK_MV(x - dia_size + dir, y + dir           );
668         }
669
670         if(x!=best[0] || y!=best[1])
671             dia_size=0;
672 #if 0
673 {
674 int dx, dy, i;
675 static int stats[8*8];
676 dx= ABS(x-best[0]);
677 dy= ABS(y-best[1]);
678 if(dy>dx){
679     dx^=dy; dy^=dx; dx^=dy;
680 }
681 stats[dy*8 + dx] ++;
682 if(256*256*256*64 % (stats[0]+1)==0){
683     for(i=0; i<64; i++){
684         if((i&7)==0) printf("\n");
685         printf("%8d ", stats[i]);
686     }
687     printf("\n");
688 }
689 }
690 #endif
691     }
692     return dmin;    
693 }
694
695 #define SAB_CHECK_MV(ax,ay)\
696 {\
697     const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
698     const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
699 /*printf("sab check %d %d\n", ax, ay);*/\
700     if(map[index]!=key){\
701         CMP(d, ax, ay, size);\
702         map[index]= key;\
703         score_map[index]= d;\
704         d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
705 /*printf("score: %d\n", d);*/\
706         if(d < minima[minima_count-1].height){\
707             int j=0;\
708             \
709             while(d >= minima[j].height) j++;\
710 \
711             memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
712 \
713             minima[j].checked= 0;\
714             minima[j].height= d;\
715             minima[j].x= ax;\
716             minima[j].y= ay;\
717             \
718             i=-1;\
719             continue;\
720         }\
721     }\
722 }
723
724 #define MAX_SAB_SIZE 16
725 static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
726                                        Picture *ref_picture,
727                                        int const pred_x, int const pred_y, int const penalty_factor,
728                                        int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
729                                        uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
730                                        )
731 {
732     me_cmp_func cmp, chroma_cmp;
733     Minima minima[MAX_SAB_SIZE];
734     const int minima_count= ABS(s->me.dia_size);
735     int i, j;
736     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
737     
738     cmp= s->dsp.me_cmp[size];
739     chroma_cmp= s->dsp.me_cmp[size+1];
740     
741     for(j=i=0; i<ME_MAP_SIZE; i++){
742         uint32_t key= map[i];
743
744         key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
745         
746         if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
747         
748         assert(j<MAX_SAB_SIZE); //max j = number of predictors
749         
750         minima[j].height= score_map[i];
751         minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
752         minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
753         minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
754         minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
755         minima[j].checked=0;
756         if(minima[j].x || minima[j].y)
757             minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
758         
759         j++;
760     }
761     
762     qsort(minima, j, sizeof(Minima), minima_cmp);
763     
764     for(; j<minima_count; j++){
765         minima[j].height=256*256*256*64;
766         minima[j].checked=0;
767         minima[j].x= minima[j].y=0;
768     }
769     
770     for(i=0; i<minima_count; i++){
771         const int x= minima[i].x;
772         const int y= minima[i].y;
773         int d;
774         
775         if(minima[i].checked) continue;
776         
777         if(   x >= xmax || x <= xmin
778            || y >= ymax || y <= ymin)
779            continue;
780
781         SAB_CHECK_MV(x-1, y)
782         SAB_CHECK_MV(x+1, y)
783         SAB_CHECK_MV(x  , y-1)
784         SAB_CHECK_MV(x  , y+1)
785         
786         minima[i].checked= 1;
787     }
788     
789     best[0]= minima[0].x;
790     best[1]= minima[0].y;
791     dmin= minima[0].height;
792     
793     if(   best[0] < xmax && best[0] > xmin
794        && best[1] < ymax && best[1] > ymin){
795         int d;
796         //ensure that the refernece samples for hpel refinement are in the map
797         CHECK_MV(best[0]-1, best[1])
798         CHECK_MV(best[0]+1, best[1])
799         CHECK_MV(best[0], best[1]-1)
800         CHECK_MV(best[0], best[1]+1)
801     }
802     return dmin;    
803 }
804
805 static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
806                                        Picture *ref_picture,
807                                        int const pred_x, int const pred_y, int const penalty_factor,
808                                        int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
809                                        uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
810                                        )
811 {
812     me_cmp_func cmp, chroma_cmp;
813     int dia_size;
814     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
815     
816     cmp= s->dsp.me_cmp[size];
817     chroma_cmp= s->dsp.me_cmp[size+1];
818
819     for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
820         int dir, start, end;
821         const int x= best[0];
822         const int y= best[1];
823
824         start= FFMAX(0, y + dia_size - ymax);
825         end  = FFMIN(dia_size, xmax - x + 1);
826         for(dir= start; dir<end; dir++){
827             int d;
828
829 //check(x + dir,y + dia_size - dir,0, a0)
830             CHECK_MV(x + dir           , y + dia_size - dir);
831         }
832
833         start= FFMAX(0, x + dia_size - xmax);
834         end  = FFMIN(dia_size, y - ymin + 1);
835         for(dir= start; dir<end; dir++){
836             int d;
837
838 //check(x + dia_size - dir, y - dir,0, a1)
839             CHECK_MV(x + dia_size - dir, y - dir           );
840         }
841
842         start= FFMAX(0, -y + dia_size + ymin );
843         end  = FFMIN(dia_size, x - xmin + 1);
844         for(dir= start; dir<end; dir++){
845             int d;
846
847 //check(x - dir,y - dia_size + dir,0, a2)
848             CHECK_MV(x - dir           , y - dia_size + dir);
849         }
850
851         start= FFMAX(0, -x + dia_size + xmin );
852         end  = FFMIN(dia_size, ymax - y + 1);
853         for(dir= start; dir<end; dir++){
854             int d;
855
856 //check(x - dia_size + dir, y + dir,0, a3)
857             CHECK_MV(x - dia_size + dir, y + dir           );
858         }
859
860         if(x!=best[0] || y!=best[1])
861             dia_size=0;
862 #if 0
863 {
864 int dx, dy, i;
865 static int stats[8*8];
866 dx= ABS(x-best[0]);
867 dy= ABS(y-best[1]);
868 stats[dy*8 + dx] ++;
869 if(256*256*256*64 % (stats[0]+1)==0){
870     for(i=0; i<64; i++){
871         if((i&7)==0) printf("\n");
872         printf("%6d ", stats[i]);
873     }
874     printf("\n");
875 }
876 }
877 #endif
878     }
879     return dmin;    
880 }
881
882 static int RENAME(epzs_motion_search)(MpegEncContext * s, int block,
883                              int *mx_ptr, int *my_ptr,
884                              int P[10][2], int pred_x, int pred_y,
885                              int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], 
886                              int ref_mv_scale, uint16_t * const mv_penalty)
887 {
888     int best[2]={0, 0};
889     int d, dmin; 
890     const int shift= 1+s->quarter_sample;
891     uint32_t *map= s->me.map;
892     int map_generation;
893     const int penalty_factor= s->me.penalty_factor;
894     const int size=0;
895     const int ref_mv_stride= s->mb_width+2;
896     const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
897     me_cmp_func cmp, chroma_cmp;
898     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
899     
900     cmp= s->dsp.me_cmp[size];
901     chroma_cmp= s->dsp.me_cmp[size+1];
902     
903     map_generation= update_map_generation(s);
904
905     CMP(dmin, 0, 0, size);
906     map[0]= map_generation;
907     score_map[0]= dmin;
908
909     /* first line */
910     if (s->mb_y == 0) {
911         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
912         CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
913                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
914     }else{
915         if(dmin<256 && ( P_LEFT[0]    |P_LEFT[1]
916                         |P_TOP[0]     |P_TOP[1]
917                         |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
918             *mx_ptr= 0;
919             *my_ptr= 0;
920             s->me.skip=1;
921             return dmin;
922         }
923         CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
924         if(dmin>256*2){
925             CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
926                             (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
927             CHECK_MV(P_LEFT[0]    >>shift, P_LEFT[1]    >>shift)
928             CHECK_MV(P_TOP[0]     >>shift, P_TOP[1]     >>shift)
929             CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
930         }
931     }
932     if(dmin>256*4){
933         if(s->me.pre_pass){
934             CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16, 
935                             (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
936             CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
937                             (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
938         }else{
939             CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, 
940                             (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
941             CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
942                             (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
943         }
944     }
945
946     if(s->avctx->last_predictor_count){
947         const int count= s->avctx->last_predictor_count;
948         const int xstart= FFMAX(0, s->mb_x - count);
949         const int ystart= FFMAX(0, s->mb_y - count);
950         const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
951         const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
952         int mb_y;
953
954         for(mb_y=ystart; mb_y<yend; mb_y++){
955             int mb_x;
956             for(mb_x=xstart; mb_x<xend; mb_x++){
957                 const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
958                 int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
959                 int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
960
961                 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
962                 CHECK_MV(mx,my)
963             }
964         }
965     }
966
967 //check(best[0],best[1],0, b0)
968     if(s->me.dia_size==-1)
969         dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
970                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
971                                    shift, map, map_generation, size, mv_penalty);
972     else if(s->me.dia_size<-1)
973         dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
974                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
975                                    shift, map, map_generation, size, mv_penalty);
976     else if(s->me.dia_size<2)
977         dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
978                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
979                                    shift, map, map_generation, size, mv_penalty);
980     else
981         dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
982                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
983                                    shift, map, map_generation, size, mv_penalty);
984
985 //check(best[0],best[1],0, b1)
986     *mx_ptr= best[0];
987     *my_ptr= best[1];    
988
989 //    printf("%d %d %d \n", best[0], best[1], dmin);
990     return dmin;
991 }
992
993 #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
994 static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block,
995                              int *mx_ptr, int *my_ptr,
996                              int P[10][2], int pred_x, int pred_y,
997                              int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], 
998                              int ref_mv_scale, uint16_t * const mv_penalty)
999 {
1000     int best[2]={0, 0};
1001     int d, dmin; 
1002     const int shift= 1+s->quarter_sample;
1003     uint32_t *map= s->me.map;
1004     int map_generation;
1005     const int penalty_factor= s->me.penalty_factor;
1006     const int size=1;
1007     const int ref_mv_stride= s->mb_width+2;
1008     const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
1009     me_cmp_func cmp, chroma_cmp;
1010     LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8);
1011     
1012     cmp= s->dsp.me_cmp[size];
1013     chroma_cmp= s->dsp.me_cmp[size+1];
1014
1015     map_generation= update_map_generation(s);
1016
1017     dmin = 1000000;
1018 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); 
1019     /* first line */
1020     if (s->mb_y == 0 && block<2) {
1021         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1022         CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
1023                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1024         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1025     }else{
1026         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1027         //FIXME try some early stop
1028         if(dmin>64*2){
1029             CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
1030             CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1031             CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
1032             CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
1033             CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
1034                             (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1035         }
1036     }
1037     if(dmin>64*4){
1038         CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, 
1039                         (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
1040         CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
1041                         (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
1042     }
1043
1044     if(s->me.dia_size==-1)
1045         dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
1046                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
1047                                    shift, map, map_generation, size, mv_penalty);
1048     else if(s->me.dia_size<-1)
1049         dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
1050                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
1051                                    shift, map, map_generation, size, mv_penalty);
1052     else if(s->me.dia_size<2)
1053         dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
1054                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
1055                                    shift, map, map_generation, size, mv_penalty);
1056     else
1057         dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
1058                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
1059                                    shift, map, map_generation, size, mv_penalty);
1060
1061     *mx_ptr= best[0];
1062     *my_ptr= best[1];    
1063
1064 //    printf("%d %d %d \n", best[0], best[1], dmin);
1065     return dmin;
1066 }
1067 #endif /* !CMP_DIRECT */