]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/motion_est_template.c
bugs
[frescor/ffmpeg.git] / 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;\
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
148     LOAD_COMMON(xx, yy);
149     
150  //FIXME factorize
151
152     cmp_sub= s->dsp.me_sub_cmp[size];
153     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
154
155     if(s->me.skip){ //FIXME move out of hpel?
156         *mx_ptr = 0;
157         *my_ptr = 0;
158         return dmin;
159     }
160         
161     if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
162         CMP_HPEL(dmin, 0, 0, mx, my, size);
163         if(mx || my || size>0)
164             dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
165     }
166         
167     if (mx > xmin && mx < xmax && 
168         my > ymin && my < ymax) {
169         int bx=2*mx, by=2*my;
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 0
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         *mx_ptr = bx;
236         *my_ptr = by;
237     }else{
238         *mx_ptr =2*mx;
239         *my_ptr =2*my;
240     }
241
242     return dmin;
243 }
244 #endif
245
246 #endif /* CMP_HPEL */
247
248 #ifdef CMP_QPEL
249
250 #define CHECK_QUARTER_MV(dx, dy, x, y)\
251 {\
252     const int hx= 4*(x)+(dx);\
253     const int hy= 4*(y)+(dy);\
254     CMP_QPEL(d, dx, dy, x, y, size);\
255     d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
256     COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
257 }
258
259 static int RENAME(qpel_motion_search)(MpegEncContext * s,
260                                   int *mx_ptr, int *my_ptr, int dmin,
261                                   int xmin, int ymin, int xmax, int ymax,
262                                   int pred_x, int pred_y, Picture *ref_picture, 
263                                   int n, int size, uint16_t * const mv_penalty)
264 {
265     const int xx = 16 * s->mb_x + 8*(n&1);
266     const int yy = 16 * s->mb_y + 8*(n>>1);
267     const int mx = *mx_ptr;
268     const int my = *my_ptr;   
269     const int penalty_factor= s->me.sub_penalty_factor;
270     const int map_generation= s->me.map_generation;
271     const int subpel_quality= s->avctx->me_subpel_quality;
272     uint32_t *map= s->me.map;
273     me_cmp_func cmp, chroma_cmp;
274     me_cmp_func cmp_sub, chroma_cmp_sub;
275
276     LOAD_COMMON(xx, yy);
277     
278     cmp= s->dsp.me_cmp[size];
279     chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
280  //FIXME factorize
281
282     cmp_sub= s->dsp.me_sub_cmp[size];
283     chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
284
285     if(s->me.skip){ //FIXME somehow move up (benchmark)
286         *mx_ptr = 0;
287         *my_ptr = 0;
288         return dmin;
289     }
290         
291     if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
292         CMP_QPEL(dmin, 0, 0, mx, my, size);
293         if(mx || my || size>0)
294             dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
295     }
296         
297     if (mx > xmin && mx < xmax && 
298         my > ymin && my < ymax) {
299         int bx=4*mx, by=4*my;
300         int d= dmin;
301         int i, nx, ny;
302         const int index= (my<<ME_MAP_SHIFT) + mx;
303         const int t= score_map[(index-(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
304         const int l= score_map[(index- 1                 )&(ME_MAP_SIZE-1)];
305         const int r= score_map[(index+ 1                 )&(ME_MAP_SIZE-1)];
306         const int b= score_map[(index+(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
307         const int c= score_map[(index                    )&(ME_MAP_SIZE-1)];
308         int best[8];
309         int best_pos[8][2];
310         
311         memset(best, 64, sizeof(int)*8);
312 #if 1
313         if(s->me.dia_size>=2){        
314             const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
315             const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
316             const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
317             const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
318
319             for(ny= -3; ny <= 3; ny++){
320                 for(nx= -3; nx <= 3; nx++){
321                     const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
322                     const int c2= nx*nx*( r +  l - 2*c) + 4*nx*( r- l) + 32*c;
323                     const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
324                     int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
325                     int i;
326                     
327                     if((nx&3)==0 && (ny&3)==0) continue;
328                     
329                     score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
330                     
331 //                    if(nx&1) score-=1024*s->me.penalty_factor;
332 //                    if(ny&1) score-=1024*s->me.penalty_factor;
333                     
334                     for(i=0; i<8; i++){
335                         if(score < best[i]){
336                             memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
337                             memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
338                             best[i]= score;
339                             best_pos[i][0]= nx + 4*mx;
340                             best_pos[i][1]= ny + 4*my;
341                             break;
342                         }
343                     }
344                 }
345             }
346         }else{
347             int tl;
348             const int cx = 4*(r - l);
349             const int cx2= r + l - 2*c; 
350             const int cy = 4*(b - t);
351             const int cy2= b + t - 2*c;
352             int cxy;
353               
354             if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
355                 tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
356             }else{
357                 CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
358             }
359             
360             cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c; 
361            
362             assert(16*cx2 + 4*cx + 32*c == 32*r);
363             assert(16*cx2 - 4*cx + 32*c == 32*l);
364             assert(16*cy2 + 4*cy + 32*c == 32*b);
365             assert(16*cy2 - 4*cy + 32*c == 32*t);
366             assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
367             
368             for(ny= -3; ny <= 3; ny++){
369                 for(nx= -3; nx <= 3; nx++){
370                     int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
371                     int i;
372                     
373                     if((nx&3)==0 && (ny&3)==0) continue;
374                 
375                     score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
376 //                    if(nx&1) score-=32*s->me.penalty_factor;
377   //                  if(ny&1) score-=32*s->me.penalty_factor;
378                     
379                     for(i=0; i<8; i++){
380                         if(score < best[i]){
381                             memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
382                             memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
383                             best[i]= score;
384                             best_pos[i][0]= nx + 4*mx;
385                             best_pos[i][1]= ny + 4*my;
386                             break;
387                         }
388                     }
389                 }
390             }            
391         }
392         for(i=0; i<subpel_quality; i++){
393             nx= best_pos[i][0];
394             ny= best_pos[i][1];
395             CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
396         }
397
398 #if 0
399             const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
400             const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
401             const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
402             const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
403 //            if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
404             if(tl<br){
405
406 //            nx= FFMAX(4*mx - bx, bx - 4*mx);
407 //            ny= FFMAX(4*my - by, by - 4*my);
408             
409             static int stats[7][7], count;
410             count++;
411             stats[4*mx - bx + 3][4*my - by + 3]++;
412             if(256*256*256*64 % count ==0){
413                 for(i=0; i<49; i++){
414                     if((i%7)==0) printf("\n");
415                     printf("%6d ", stats[0][i]);
416                 }
417                 printf("\n");
418             }
419             }
420 #endif
421 #else
422
423         CHECK_QUARTER_MV(2, 2, mx-1, my-1)
424         CHECK_QUARTER_MV(0, 2, mx  , my-1)        
425         CHECK_QUARTER_MV(2, 2, mx  , my-1)
426         CHECK_QUARTER_MV(2, 0, mx  , my  )
427         CHECK_QUARTER_MV(2, 2, mx  , my  )
428         CHECK_QUARTER_MV(0, 2, mx  , my  )
429         CHECK_QUARTER_MV(2, 2, mx-1, my  )
430         CHECK_QUARTER_MV(2, 0, mx-1, my  )
431         
432         nx= bx;
433         ny= by;
434         
435         for(i=0; i<8; i++){
436             int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
437             int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
438             CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
439         }
440 #endif
441 #if 0
442         //outer ring
443         CHECK_QUARTER_MV(1, 3, mx-1, my-1)
444         CHECK_QUARTER_MV(1, 2, mx-1, my-1)
445         CHECK_QUARTER_MV(1, 1, mx-1, my-1)
446         CHECK_QUARTER_MV(2, 1, mx-1, my-1)
447         CHECK_QUARTER_MV(3, 1, mx-1, my-1)
448         CHECK_QUARTER_MV(0, 1, mx  , my-1)
449         CHECK_QUARTER_MV(1, 1, mx  , my-1)
450         CHECK_QUARTER_MV(2, 1, mx  , my-1)
451         CHECK_QUARTER_MV(3, 1, mx  , my-1)
452         CHECK_QUARTER_MV(3, 2, mx  , my-1)
453         CHECK_QUARTER_MV(3, 3, mx  , my-1)
454         CHECK_QUARTER_MV(3, 0, mx  , my  )
455         CHECK_QUARTER_MV(3, 1, mx  , my  )
456         CHECK_QUARTER_MV(3, 2, mx  , my  )
457         CHECK_QUARTER_MV(3, 3, mx  , my  )
458         CHECK_QUARTER_MV(2, 3, mx  , my  )
459         CHECK_QUARTER_MV(1, 3, mx  , my  )
460         CHECK_QUARTER_MV(0, 3, mx  , my  )
461         CHECK_QUARTER_MV(3, 3, mx-1, my  )
462         CHECK_QUARTER_MV(2, 3, mx-1, my  )
463         CHECK_QUARTER_MV(1, 3, mx-1, my  )
464         CHECK_QUARTER_MV(1, 2, mx-1, my  )
465         CHECK_QUARTER_MV(1, 1, mx-1, my  )
466         CHECK_QUARTER_MV(1, 0, mx-1, my  )
467 #endif
468         assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
469
470         *mx_ptr = bx;
471         *my_ptr = by;
472     }else{
473         *mx_ptr =4*mx;
474         *my_ptr =4*my;
475     }
476
477     return dmin;
478 }
479
480 #endif /* CMP_QPEL */
481
482 #define CHECK_MV(x,y)\
483 {\
484     const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
485     const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
486 /*printf("check_mv %d %d\n", x, y);*/\
487     if(map[index]!=key){\
488         CMP(d, x, y, size);\
489         map[index]= key;\
490         score_map[index]= d;\
491         d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
492 /*printf("score:%d\n", d);*/\
493         COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
494     }\
495 }
496
497 #define CHECK_CLIPED_MV(ax,ay)\
498 {\
499     const int x= FFMAX(xmin, FFMIN(ax, xmax));\
500     const int y= FFMAX(ymin, FFMIN(ay, ymax));\
501     CHECK_MV(x, y)\
502 }
503
504 #define CHECK_MV_DIR(x,y,new_dir)\
505 {\
506     const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
507     const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
508 /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
509     if(map[index]!=key){\
510         CMP(d, x, y, size);\
511         map[index]= key;\
512         score_map[index]= d;\
513         d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
514 /*printf("score:%d\n", d);*/\
515         if(d<dmin){\
516             best[0]=x;\
517             best[1]=y;\
518             dmin=d;\
519             next_dir= new_dir;\
520         }\
521     }\
522 }
523
524 #define check(x,y,S,v)\
525 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
526 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
527 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
528 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
529
530
531 static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
532                                        Picture *ref_picture,
533                                        int const pred_x, int const pred_y, int const penalty_factor,
534                                        int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
535                                        uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
536                                        )
537 {
538     me_cmp_func cmp, chroma_cmp;
539     int next_dir=-1;
540     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
541     
542     cmp= s->dsp.me_cmp[size];
543     chroma_cmp= s->dsp.me_cmp[size+1];
544
545     { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
546         const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
547         const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
548         if(map[index]!=key){ //this will be executed only very rarey
549             CMP(score_map[index], best[0], best[1], size);
550             map[index]= key;
551         }
552     }
553
554     for(;;){
555         int d;
556         const int dir= next_dir;
557         const int x= best[0];
558         const int y= best[1];
559         next_dir=-1;
560
561 //printf("%d", dir);
562         if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y  , 0)
563         if(dir!=3 && y>ymin) CHECK_MV_DIR(x  , y-1, 1)
564         if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y  , 2)
565         if(dir!=1 && y<ymax) CHECK_MV_DIR(x  , y+1, 3)
566
567         if(next_dir==-1){
568             return dmin;
569         }
570     }
571 }
572
573 static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
574                                        Picture *ref_picture,
575                                        int const pred_x, int const pred_y, int const penalty_factor,
576                                        int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
577                                        uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
578                                        )
579 {
580     me_cmp_func cmp, chroma_cmp;
581     int dia_size;
582     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
583     
584     cmp= s->dsp.me_cmp[size];
585     chroma_cmp= s->dsp.me_cmp[size+1];
586
587     for(dia_size=1; dia_size<=4; dia_size++){
588         int dir;
589         const int x= best[0];
590         const int y= best[1];
591         
592         if(dia_size&(dia_size-1)) continue;
593
594         if(   x + dia_size > xmax
595            || x - dia_size < xmin
596            || y + dia_size > ymax
597            || y - dia_size < ymin)
598            continue;
599         
600         for(dir= 0; dir<dia_size; dir+=2){
601             int d;
602
603             CHECK_MV(x + dir           , y + dia_size - dir);
604             CHECK_MV(x + dia_size - dir, y - dir           );
605             CHECK_MV(x - dir           , y - dia_size + dir);
606             CHECK_MV(x - dia_size + dir, y + dir           );
607         }
608
609         if(x!=best[0] || y!=best[1])
610             dia_size=0;
611 #if 0
612 {
613 int dx, dy, i;
614 static int stats[8*8];
615 dx= ABS(x-best[0]);
616 dy= ABS(y-best[1]);
617 if(dy>dx){
618     dx^=dy; dy^=dx; dx^=dy;
619 }
620 stats[dy*8 + dx] ++;
621 if(256*256*256*64 % (stats[0]+1)==0){
622     for(i=0; i<64; i++){
623         if((i&7)==0) printf("\n");
624         printf("%8d ", stats[i]);
625     }
626     printf("\n");
627 }
628 }
629 #endif
630     }
631     return dmin;    
632 }
633
634 #define SAB_CHECK_MV(ax,ay)\
635 {\
636     const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
637     const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
638 /*printf("sab check %d %d\n", ax, ay);*/\
639     if(map[index]!=key){\
640         CMP(d, ax, ay, size);\
641         map[index]= key;\
642         score_map[index]= d;\
643         d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
644 /*printf("score: %d\n", d);*/\
645         if(d < minima[minima_count-1].height){\
646             int j=0;\
647             \
648             while(d >= minima[j].height) j++;\
649 \
650             memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
651 \
652             minima[j].checked= 0;\
653             minima[j].height= d;\
654             minima[j].x= ax;\
655             minima[j].y= ay;\
656             \
657             i=-1;\
658             continue;\
659         }\
660     }\
661 }
662
663 #define MAX_SAB_SIZE 16
664 static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
665                                        Picture *ref_picture,
666                                        int const pred_x, int const pred_y, int const penalty_factor,
667                                        int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
668                                        uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
669                                        )
670 {
671     me_cmp_func cmp, chroma_cmp;
672     Minima minima[MAX_SAB_SIZE];
673     const int minima_count= ABS(s->me.dia_size);
674     int i, j;
675     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
676     
677     cmp= s->dsp.me_cmp[size];
678     chroma_cmp= s->dsp.me_cmp[size+1];
679     
680     for(j=i=0; i<ME_MAP_SIZE; i++){
681         uint32_t key= map[i];
682
683         key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
684         
685         if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
686         
687         assert(j<MAX_SAB_SIZE); //max j = number of predictors
688         
689         minima[j].height= score_map[i];
690         minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
691         minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
692         minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
693         minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
694         minima[j].checked=0;
695         if(minima[j].x || minima[j].y)
696             minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
697         
698         j++;
699     }
700     
701     qsort(minima, j, sizeof(Minima), minima_cmp);
702     
703     for(; j<minima_count; j++){
704         minima[j].height=256*256*256*64;
705         minima[j].checked=0;
706         minima[j].x= minima[j].y=0;
707     }
708     
709     for(i=0; i<minima_count; i++){
710         const int x= minima[i].x;
711         const int y= minima[i].y;
712         int d;
713         
714         if(minima[i].checked) continue;
715         
716         if(   x >= xmax || x <= xmin
717            || y >= ymax || y <= ymin)
718            continue;
719
720         SAB_CHECK_MV(x-1, y)
721         SAB_CHECK_MV(x+1, y)
722         SAB_CHECK_MV(x  , y-1)
723         SAB_CHECK_MV(x  , y+1)
724         
725         minima[i].checked= 1;
726     }
727     
728     best[0]= minima[0].x;
729     best[1]= minima[0].y;
730     dmin= minima[0].height;
731     
732     if(   best[0] < xmax && best[0] > xmin
733        && best[1] < ymax && best[1] > ymin){
734         int d;
735         //ensure that the refernece samples for hpel refinement are in the map
736         CHECK_MV(best[0]-1, best[1])
737         CHECK_MV(best[0]+1, best[1])
738         CHECK_MV(best[0], best[1]-1)
739         CHECK_MV(best[0], best[1]+1)
740     }
741     return dmin;    
742 }
743
744 static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
745                                        Picture *ref_picture,
746                                        int const pred_x, int const pred_y, int const penalty_factor,
747                                        int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
748                                        uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
749                                        )
750 {
751     me_cmp_func cmp, chroma_cmp;
752     int dia_size;
753     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
754     
755     cmp= s->dsp.me_cmp[size];
756     chroma_cmp= s->dsp.me_cmp[size+1];
757
758     for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
759         int dir, start, end;
760         const int x= best[0];
761         const int y= best[1];
762
763         start= FFMAX(0, y + dia_size - ymax);
764         end  = FFMIN(dia_size, xmax - x + 1);
765         for(dir= start; dir<end; dir++){
766             int d;
767
768 //check(x + dir,y + dia_size - dir,0, a0)
769             CHECK_MV(x + dir           , y + dia_size - dir);
770         }
771
772         start= FFMAX(0, x + dia_size - xmax);
773         end  = FFMIN(dia_size, y - ymin + 1);
774         for(dir= start; dir<end; dir++){
775             int d;
776
777 //check(x + dia_size - dir, y - dir,0, a1)
778             CHECK_MV(x + dia_size - dir, y - dir           );
779         }
780
781         start= FFMAX(0, -y + dia_size + ymin );
782         end  = FFMIN(dia_size, x - xmin + 1);
783         for(dir= start; dir<end; dir++){
784             int d;
785
786 //check(x - dir,y - dia_size + dir,0, a2)
787             CHECK_MV(x - dir           , y - dia_size + dir);
788         }
789
790         start= FFMAX(0, -x + dia_size + xmin );
791         end  = FFMIN(dia_size, ymax - y + 1);
792         for(dir= start; dir<end; dir++){
793             int d;
794
795 //check(x - dia_size + dir, y + dir,0, a3)
796             CHECK_MV(x - dia_size + dir, y + dir           );
797         }
798
799         if(x!=best[0] || y!=best[1])
800             dia_size=0;
801 #if 0
802 {
803 int dx, dy, i;
804 static int stats[8*8];
805 dx= ABS(x-best[0]);
806 dy= ABS(y-best[1]);
807 stats[dy*8 + dx] ++;
808 if(256*256*256*64 % (stats[0]+1)==0){
809     for(i=0; i<64; i++){
810         if((i&7)==0) printf("\n");
811         printf("%6d ", stats[i]);
812     }
813     printf("\n");
814 }
815 }
816 #endif
817     }
818     return dmin;    
819 }
820
821 static int RENAME(epzs_motion_search)(MpegEncContext * s, int block,
822                              int *mx_ptr, int *my_ptr,
823                              int P[10][2], int pred_x, int pred_y,
824                              int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], 
825                              int ref_mv_scale, uint16_t * const mv_penalty)
826 {
827     int best[2]={0, 0};
828     int d, dmin; 
829     const int shift= 1+s->quarter_sample;
830     uint32_t *map= s->me.map;
831     int map_generation;
832     const int penalty_factor= s->me.penalty_factor;
833     const int size=0;
834     const int ref_mv_stride= s->mb_width+2;
835     const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
836     me_cmp_func cmp, chroma_cmp;
837     LOAD_COMMON(s->mb_x*16, s->mb_y*16);
838     
839     cmp= s->dsp.me_cmp[size];
840     chroma_cmp= s->dsp.me_cmp[size+1];
841     
842     map_generation= update_map_generation(s);
843
844     CMP(dmin, 0, 0, size);
845     map[0]= map_generation;
846     score_map[0]= dmin;
847
848     /* first line */
849     if (s->mb_y == 0) {
850         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
851         CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
852                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
853     }else{
854         if(dmin<256 && ( P_LEFT[0]    |P_LEFT[1]
855                         |P_TOP[0]     |P_TOP[1]
856                         |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
857             *mx_ptr= 0;
858             *my_ptr= 0;
859             s->me.skip=1;
860             return dmin;
861         }
862         CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
863         if(dmin>256*2){
864             CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
865                             (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
866             CHECK_MV(P_LEFT[0]    >>shift, P_LEFT[1]    >>shift)
867             CHECK_MV(P_TOP[0]     >>shift, P_TOP[1]     >>shift)
868             CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
869         }
870     }
871     if(dmin>256*4){
872         if(s->me.pre_pass){
873             CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16, 
874                             (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
875             CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
876                             (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
877         }else{
878             CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, 
879                             (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
880             CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
881                             (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
882         }
883     }
884
885     if(s->avctx->last_predictor_count){
886         const int count= s->avctx->last_predictor_count;
887         const int xstart= FFMAX(0, s->mb_x - count);
888         const int ystart= FFMAX(0, s->mb_y - count);
889         const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
890         const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
891         int mb_y;
892
893         for(mb_y=ystart; mb_y<yend; mb_y++){
894             int mb_x;
895             for(mb_x=xstart; mb_x<xend; mb_x++){
896                 const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
897                 int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
898                 int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
899
900                 if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
901                 CHECK_MV(mx,my)
902             }
903         }
904     }
905
906 //check(best[0],best[1],0, b0)
907     if(s->me.dia_size==-1)
908         dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
909                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
910                                    shift, map, map_generation, size, mv_penalty);
911     else if(s->me.dia_size<-1)
912         dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
913                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
914                                    shift, map, map_generation, size, mv_penalty);
915     else if(s->me.dia_size<2)
916         dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
917                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
918                                    shift, map, map_generation, size, mv_penalty);
919     else
920         dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
921                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
922                                    shift, map, map_generation, size, mv_penalty);
923
924 //check(best[0],best[1],0, b1)
925     *mx_ptr= best[0];
926     *my_ptr= best[1];    
927
928 //    printf("%d %d %d \n", best[0], best[1], dmin);
929     return dmin;
930 }
931
932 #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
933 static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block,
934                              int *mx_ptr, int *my_ptr,
935                              int P[10][2], int pred_x, int pred_y,
936                              int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], 
937                              int ref_mv_scale, uint16_t * const mv_penalty)
938 {
939     int best[2]={0, 0};
940     int d, dmin; 
941     const int shift= 1+s->quarter_sample;
942     uint32_t *map= s->me.map;
943     int map_generation;
944     const int penalty_factor= s->me.penalty_factor;
945     const int size=1;
946     const int ref_mv_stride= s->mb_width+2;
947     const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
948     me_cmp_func cmp, chroma_cmp;
949     LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8);
950     
951     cmp= s->dsp.me_cmp[size];
952     chroma_cmp= s->dsp.me_cmp[size+1];
953
954     map_generation= update_map_generation(s);
955
956     dmin = 1000000;
957 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); 
958     /* first line */
959     if (s->mb_y == 0 && block<2) {
960         CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
961         CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
962                         (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
963         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
964     }else{
965         CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
966         //FIXME try some early stop
967         if(dmin>64*2){
968             CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
969             CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
970             CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
971             CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
972             CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
973                             (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
974         }
975     }
976     if(dmin>64*4){
977         CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, 
978                         (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
979         CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
980                         (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
981     }
982
983     if(s->me.dia_size==-1)
984         dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
985                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
986                                    shift, map, map_generation, size, mv_penalty);
987     else if(s->me.dia_size<-1)
988         dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
989                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
990                                    shift, map, map_generation, size, mv_penalty);
991     else if(s->me.dia_size<2)
992         dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
993                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
994                                    shift, map, map_generation, size, mv_penalty);
995     else
996         dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
997                                    pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
998                                    shift, map, map_generation, size, mv_penalty);
999
1000     *mx_ptr= best[0];
1001     *my_ptr= best[1];    
1002
1003 //    printf("%d %d %d \n", best[0], best[1], dmin);
1004     return dmin;
1005 }
1006 #endif /* !CMP_DIRECT */