Statistics
| Branch: | Revision:

ffmpeg / libavcodec / motion_est_template.c @ 186447f8

History | View | Annotate | Download (38.4 KB)

1 1457ab52 Michael Niedermayer
/*
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 983e3246 Michael Niedermayer
 
21
/**
22
 * @file motion_est_template.c
23
 * Motion estimation template.
24
 */
25 1457ab52 Michael Niedermayer
26
//lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
27
//Note, the last line is there to kill these ugly unused var warnings
28
#define LOAD_COMMON(x, y)\
29
    uint32_t * const score_map= s->me.score_map;\
30
    const int stride= s->linesize;\
31
    const int uvstride= s->uvlinesize;\
32
    const int time_pp= s->pp_time;\
33
    const int time_pb= s->pb_time;\
34
    uint8_t * const src_y= s->new_picture.data[0] + ((y) * stride) + (x);\
35
    uint8_t * const src_u= s->new_picture.data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
36
    uint8_t * const src_v= s->new_picture.data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
37
    uint8_t * const ref_y= ref_picture->data[0] + ((y) * stride) + (x);\
38
    uint8_t * const ref_u= ref_picture->data[1] + (((y)>>1) * uvstride) + ((x)>>1);\
39
    uint8_t * const ref_v= ref_picture->data[2] + (((y)>>1) * uvstride) + ((x)>>1);\
40
    uint8_t * const ref2_y= s->next_picture.data[0] + ((y) * stride) + (x);\
41
    op_pixels_func (*hpel_put)[4];\
42
    op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\
43
    op_pixels_func (*chroma_hpel_put)[4];\
44
    qpel_mc_func (*qpel_put)[16];\
45
    qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\
46 3db320ea Falk H├╝ffner
    const __attribute__((unused)) int unu= time_pp + time_pb + (size_t)src_u + (size_t)src_v + (size_t)ref_u + (size_t)ref_v\
47
                                           + (size_t)ref2_y + (size_t)hpel_avg + (size_t)qpel_avg + (size_t)score_map;\
48 1457ab52 Michael Niedermayer
    if(s->no_rounding /*FIXME b_type*/){\
49
        hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\
50
        chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\
51
        qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];\
52
    }else{\
53
        hpel_put=& s->dsp.put_pixels_tab[size];\
54
        chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];\
55
        qpel_put= &s->dsp.put_qpel_pixels_tab[size];\
56
    }
57
58
59
#ifdef CMP_HPEL
60
    
61
#define CHECK_HALF_MV(dx, dy, x, y)\
62
{\
63
    const int hx= 2*(x)+(dx);\
64
    const int hy= 2*(y)+(dy);\
65
    CMP_HPEL(d, dx, dy, x, y, size);\
66
    d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
67
    COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
68
}
69
70
#if 0
71
static int RENAME(hpel_motion_search)(MpegEncContext * s,
72
                                  int *mx_ptr, int *my_ptr, int dmin,
73
                                  int xmin, int ymin, int xmax, int ymax,
74
                                  int pred_x, int pred_y, Picture *ref_picture, 
75 30952237 Michael Niedermayer
                                  int n, int size, uint8_t * const mv_penalty)
76 1457ab52 Michael Niedermayer
{
77
    const int xx = 16 * s->mb_x + 8*(n&1);
78
    const int yy = 16 * s->mb_y + 8*(n>>1);
79
    const int mx = *mx_ptr;
80
    const int my = *my_ptr;
81 b07a5980 Michael Niedermayer
    const int penalty_factor= s->me.sub_penalty_factor;
82 1457ab52 Michael Niedermayer
    
83
    LOAD_COMMON(xx, yy);
84
    
85
 //   INIT;
86
 //FIXME factorize
87
    me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
88

89
    if(s->no_rounding /*FIXME b_type*/){
90
        hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
91
        chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
92
    }else{
93
        hpel_put=& s->dsp.put_pixels_tab[size];
94
        chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
95
    }
96
    cmp= s->dsp.me_cmp[size];
97
    chroma_cmp= s->dsp.me_cmp[size+1];
98
    cmp_sub= s->dsp.me_sub_cmp[size];
99
    chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
100

101
    if(s->me.skip){ //FIXME somehow move up (benchmark)
102
        *mx_ptr = 0;
103
        *my_ptr = 0;
104
        return dmin;
105
    }
106
        
107
    if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
108
        CMP_HPEL(dmin, 0, 0, mx, my, size);
109
        if(mx || my)
110
            dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
111
    }
112
        
113
    if (mx > xmin && mx < xmax && 
114
        my > ymin && my < ymax) {
115
        int bx=2*mx, by=2*my;
116
        int d= dmin;
117
        
118
        CHECK_HALF_MV(1, 1, mx-1, my-1)
119
        CHECK_HALF_MV(0, 1, mx  , my-1)        
120
        CHECK_HALF_MV(1, 1, mx  , my-1)
121
        CHECK_HALF_MV(1, 0, mx-1, my  )
122
        CHECK_HALF_MV(1, 0, mx  , my  )
123
        CHECK_HALF_MV(1, 1, mx-1, my  )
124
        CHECK_HALF_MV(0, 1, mx  , my  )        
125
        CHECK_HALF_MV(1, 1, mx  , my  )
126

127 b07a5980 Michael Niedermayer
        assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
128 1457ab52 Michael Niedermayer

129
        *mx_ptr = bx;
130
        *my_ptr = by;
131
    }else{
132
        *mx_ptr =2*mx;
133
        *my_ptr =2*my;
134
    }
135

136
    return dmin;
137
}
138

139
#else
140
static int RENAME(hpel_motion_search)(MpegEncContext * s,
141
                                  int *mx_ptr, int *my_ptr, int dmin,
142
                                  int xmin, int ymin, int xmax, int ymax,
143
                                  int pred_x, int pred_y, Picture *ref_picture, 
144 30952237 Michael Niedermayer
                                  int n, int size, uint8_t * const mv_penalty)
145 1457ab52 Michael Niedermayer
{
146
    const int xx = 16 * s->mb_x + 8*(n&1);
147
    const int yy = 16 * s->mb_y + 8*(n>>1);
148
    const int mx = *mx_ptr;
149
    const int my = *my_ptr;   
150
    const int penalty_factor= s->me.sub_penalty_factor;
151
    me_cmp_func cmp_sub, chroma_cmp_sub;
152 67725183 Michael Niedermayer
    int bx=2*mx, by=2*my;
153 1457ab52 Michael Niedermayer
154
    LOAD_COMMON(xx, yy);
155
    
156
 //FIXME factorize
157
158
    cmp_sub= s->dsp.me_sub_cmp[size];
159
    chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
160
161
    if(s->me.skip){ //FIXME move out of hpel?
162
        *mx_ptr = 0;
163
        *my_ptr = 0;
164
        return dmin;
165
    }
166
        
167
    if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
168
        CMP_HPEL(dmin, 0, 0, mx, my, size);
169 1d0eab1d Michael Niedermayer
        if(mx || my || size>0)
170 1457ab52 Michael Niedermayer
            dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
171
    }
172
        
173
    if (mx > xmin && mx < xmax && 
174
        my > ymin && my < ymax) {
175
        int d= dmin;
176
        const int index= (my<<ME_MAP_SHIFT) + mx;
177
        const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] 
178 b07a5980 Michael Niedermayer
                     + (mv_penalty[bx   - pred_x] + mv_penalty[by-2 - pred_y])*s->me.penalty_factor;
179 1457ab52 Michael Niedermayer
        const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)]
180 b07a5980 Michael Niedermayer
                     + (mv_penalty[bx-2 - pred_x] + mv_penalty[by   - pred_y])*s->me.penalty_factor;
181 1457ab52 Michael Niedermayer
        const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)]
182 b07a5980 Michael Niedermayer
                     + (mv_penalty[bx+2 - pred_x] + mv_penalty[by   - pred_y])*s->me.penalty_factor;
183 1457ab52 Michael Niedermayer
        const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
184 b07a5980 Michael Niedermayer
                     + (mv_penalty[bx   - pred_x] + mv_penalty[by+2 - pred_y])*s->me.penalty_factor;
185
    
186 67725183 Michael Niedermayer
#if 1
187 b07a5980 Michael Niedermayer
        int key;
188
        int map_generation= s->me.map_generation;
189 802f454e Fabrice Bellard
#ifndef NDEBUG
190 b07a5980 Michael Niedermayer
        uint32_t *map= s->me.map;
191 802f454e Fabrice Bellard
#endif
192 b07a5980 Michael Niedermayer
        key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
193
        assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
194
        key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
195
        assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
196
        key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
197
        assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
198
        key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
199
        assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
200
#endif                
201 1457ab52 Michael Niedermayer
        if(t<=b){
202
            CHECK_HALF_MV(0, 1, mx  ,my-1)
203
            if(l<=r){
204
                CHECK_HALF_MV(1, 1, mx-1, my-1)
205
                if(t+r<=b+l){
206
                    CHECK_HALF_MV(1, 1, mx  , my-1)
207
                }else{
208
                    CHECK_HALF_MV(1, 1, mx-1, my  )
209
                }
210
                CHECK_HALF_MV(1, 0, mx-1, my  )
211
            }else{
212
                CHECK_HALF_MV(1, 1, mx  , my-1)
213
                if(t+l<=b+r){
214
                    CHECK_HALF_MV(1, 1, mx-1, my-1)
215
                }else{
216
                    CHECK_HALF_MV(1, 1, mx  , my  )
217
                }
218
                CHECK_HALF_MV(1, 0, mx  , my  )
219
            }
220
        }else{
221
            if(l<=r){
222
                if(t+l<=b+r){
223
                    CHECK_HALF_MV(1, 1, mx-1, my-1)
224
                }else{
225
                    CHECK_HALF_MV(1, 1, mx  , my  )
226
                }
227
                CHECK_HALF_MV(1, 0, mx-1, my)
228
                CHECK_HALF_MV(1, 1, mx-1, my)
229
            }else{
230
                if(t+r<=b+l){
231
                    CHECK_HALF_MV(1, 1, mx  , my-1)
232
                }else{
233
                    CHECK_HALF_MV(1, 1, mx-1, my)
234
                }
235
                CHECK_HALF_MV(1, 0, mx  , my)
236
                CHECK_HALF_MV(1, 1, mx  , my)
237
            }
238
            CHECK_HALF_MV(0, 1, mx  , my)
239
        }
240
        assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
241
    }
242
243 67725183 Michael Niedermayer
    *mx_ptr = bx;
244
    *my_ptr = by;
245
    
246 1457ab52 Michael Niedermayer
    return dmin;
247
}
248
#endif
249
250 67725183 Michael Niedermayer
static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture, 
251 30952237 Michael Niedermayer
                                  uint8_t * const mv_penalty)
252 67725183 Michael Niedermayer
{
253
//    const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
254
    const int size= 0;
255
    const int xx = 16 * s->mb_x;
256
    const int yy = 16 * s->mb_y;
257
    const int penalty_factor= s->me.mb_penalty_factor;
258
    const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these
259
    const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit
260
    me_cmp_func cmp_sub, chroma_cmp_sub;
261
    int d;
262
263
    LOAD_COMMON(xx, yy);
264
    
265
 //FIXME factorize
266
267
    cmp_sub= s->dsp.mb_cmp[size];
268
    chroma_cmp_sub= s->dsp.mb_cmp[size+1];
269
    
270
    assert(!s->me.skip);
271
    assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
272
273
    CMP_HPEL(d, mx&1, my&1, mx>>1, my>>1, size);
274
    //FIXME check cbp before adding penalty for (0,0) vector
275
    if(mx || my || size>0)
276
        d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
277
        
278
    return d;
279
}
280
281 1457ab52 Michael Niedermayer
#endif /* CMP_HPEL */
282
283 67725183 Michael Niedermayer
284
285 1457ab52 Michael Niedermayer
#ifdef CMP_QPEL
286
287
#define CHECK_QUARTER_MV(dx, dy, x, y)\
288
{\
289
    const int hx= 4*(x)+(dx);\
290
    const int hy= 4*(y)+(dy);\
291
    CMP_QPEL(d, dx, dy, x, y, size);\
292
    d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
293
    COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
294
}
295
296
static int RENAME(qpel_motion_search)(MpegEncContext * s,
297
                                  int *mx_ptr, int *my_ptr, int dmin,
298
                                  int xmin, int ymin, int xmax, int ymax,
299
                                  int pred_x, int pred_y, Picture *ref_picture, 
300 30952237 Michael Niedermayer
                                  int n, int size, uint8_t * const mv_penalty)
301 1457ab52 Michael Niedermayer
{
302
    const int xx = 16 * s->mb_x + 8*(n&1);
303
    const int yy = 16 * s->mb_y + 8*(n>>1);
304
    const int mx = *mx_ptr;
305
    const int my = *my_ptr;   
306
    const int penalty_factor= s->me.sub_penalty_factor;
307
    const int map_generation= s->me.map_generation;
308 826f429a Michael Niedermayer
    const int subpel_quality= s->avctx->me_subpel_quality;
309 1457ab52 Michael Niedermayer
    uint32_t *map= s->me.map;
310
    me_cmp_func cmp, chroma_cmp;
311
    me_cmp_func cmp_sub, chroma_cmp_sub;
312
313
    LOAD_COMMON(xx, yy);
314
    
315
    cmp= s->dsp.me_cmp[size];
316
    chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
317
 //FIXME factorize
318
319
    cmp_sub= s->dsp.me_sub_cmp[size];
320
    chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
321
322
    if(s->me.skip){ //FIXME somehow move up (benchmark)
323
        *mx_ptr = 0;
324
        *my_ptr = 0;
325
        return dmin;
326
    }
327
        
328
    if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
329
        CMP_QPEL(dmin, 0, 0, mx, my, size);
330 1d0eab1d Michael Niedermayer
        if(mx || my || size>0)
331 1457ab52 Michael Niedermayer
            dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
332
    }
333
        
334
    if (mx > xmin && mx < xmax && 
335
        my > ymin && my < ymax) {
336
        int bx=4*mx, by=4*my;
337
        int d= dmin;
338
        int i, nx, ny;
339
        const int index= (my<<ME_MAP_SHIFT) + mx;
340
        const int t= score_map[(index-(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
341
        const int l= score_map[(index- 1                 )&(ME_MAP_SIZE-1)];
342
        const int r= score_map[(index+ 1                 )&(ME_MAP_SIZE-1)];
343
        const int b= score_map[(index+(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
344
        const int c= score_map[(index                    )&(ME_MAP_SIZE-1)];
345
        int best[8];
346
        int best_pos[8][2];
347
        
348
        memset(best, 64, sizeof(int)*8);
349
#if 1
350 826f429a Michael Niedermayer
        if(s->me.dia_size>=2){        
351 1457ab52 Michael Niedermayer
            const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
352
            const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
353
            const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
354
            const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
355
356
            for(ny= -3; ny <= 3; ny++){
357
                for(nx= -3; nx <= 3; nx++){
358
                    const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
359
                    const int c2= nx*nx*( r +  l - 2*c) + 4*nx*( r- l) + 32*c;
360
                    const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
361
                    int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
362
                    int i;
363
                    
364
                    if((nx&3)==0 && (ny&3)==0) continue;
365
                    
366
                    score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
367
                    
368
//                    if(nx&1) score-=1024*s->me.penalty_factor;
369
//                    if(ny&1) score-=1024*s->me.penalty_factor;
370
                    
371
                    for(i=0; i<8; i++){
372
                        if(score < best[i]){
373
                            memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
374
                            memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
375
                            best[i]= score;
376
                            best_pos[i][0]= nx + 4*mx;
377
                            best_pos[i][1]= ny + 4*my;
378
                            break;
379
                        }
380
                    }
381
                }
382
            }
383
        }else{
384
            int tl;
385
            const int cx = 4*(r - l);
386
            const int cx2= r + l - 2*c; 
387
            const int cy = 4*(b - t);
388
            const int cy2= b + t - 2*c;
389
            int cxy;
390
              
391
            if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
392
                tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
393
            }else{
394
                CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
395
            }
396
            
397
            cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c; 
398
           
399
            assert(16*cx2 + 4*cx + 32*c == 32*r);
400
            assert(16*cx2 - 4*cx + 32*c == 32*l);
401
            assert(16*cy2 + 4*cy + 32*c == 32*b);
402
            assert(16*cy2 - 4*cy + 32*c == 32*t);
403
            assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
404
            
405
            for(ny= -3; ny <= 3; ny++){
406
                for(nx= -3; nx <= 3; nx++){
407
                    int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
408
                    int i;
409
                    
410
                    if((nx&3)==0 && (ny&3)==0) continue;
411
                
412
                    score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
413
//                    if(nx&1) score-=32*s->me.penalty_factor;
414
  //                  if(ny&1) score-=32*s->me.penalty_factor;
415
                    
416
                    for(i=0; i<8; i++){
417
                        if(score < best[i]){
418
                            memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
419
                            memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
420
                            best[i]= score;
421
                            best_pos[i][0]= nx + 4*mx;
422
                            best_pos[i][1]= ny + 4*my;
423
                            break;
424
                        }
425
                    }
426
                }
427
            }            
428
        }
429 826f429a Michael Niedermayer
        for(i=0; i<subpel_quality; i++){
430 1457ab52 Michael Niedermayer
            nx= best_pos[i][0];
431
            ny= best_pos[i][1];
432
            CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
433
        }
434 826f429a Michael Niedermayer
435 1457ab52 Michael Niedermayer
#if 0
436 826f429a Michael Niedermayer
            const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
437
            const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
438
            const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
439
            const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
440
//            if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
441
            if(tl<br){
442

443
//            nx= FFMAX(4*mx - bx, bx - 4*mx);
444
//            ny= FFMAX(4*my - by, by - 4*my);
445 1457ab52 Michael Niedermayer
            
446 826f429a Michael Niedermayer
            static int stats[7][7], count;
447
            count++;
448
            stats[4*mx - bx + 3][4*my - by + 3]++;
449
            if(256*256*256*64 % count ==0){
450
                for(i=0; i<49; i++){
451
                    if((i%7)==0) printf("\n");
452 1457ab52 Michael Niedermayer
                    printf("%6d ", stats[0][i]);
453
                }
454
                printf("\n");
455
            }
456 826f429a Michael Niedermayer
            }
457 1457ab52 Michael Niedermayer
#endif
458
#else
459
460
        CHECK_QUARTER_MV(2, 2, mx-1, my-1)
461
        CHECK_QUARTER_MV(0, 2, mx  , my-1)        
462
        CHECK_QUARTER_MV(2, 2, mx  , my-1)
463
        CHECK_QUARTER_MV(2, 0, mx  , my  )
464
        CHECK_QUARTER_MV(2, 2, mx  , my  )
465
        CHECK_QUARTER_MV(0, 2, mx  , my  )
466
        CHECK_QUARTER_MV(2, 2, mx-1, my  )
467
        CHECK_QUARTER_MV(2, 0, mx-1, my  )
468
        
469
        nx= bx;
470
        ny= by;
471
        
472
        for(i=0; i<8; i++){
473
            int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
474
            int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
475
            CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
476
        }
477
#endif
478
#if 0
479
        //outer ring
480
        CHECK_QUARTER_MV(1, 3, mx-1, my-1)
481
        CHECK_QUARTER_MV(1, 2, mx-1, my-1)
482
        CHECK_QUARTER_MV(1, 1, mx-1, my-1)
483
        CHECK_QUARTER_MV(2, 1, mx-1, my-1)
484
        CHECK_QUARTER_MV(3, 1, mx-1, my-1)
485
        CHECK_QUARTER_MV(0, 1, mx  , my-1)
486
        CHECK_QUARTER_MV(1, 1, mx  , my-1)
487
        CHECK_QUARTER_MV(2, 1, mx  , my-1)
488
        CHECK_QUARTER_MV(3, 1, mx  , my-1)
489
        CHECK_QUARTER_MV(3, 2, mx  , my-1)
490
        CHECK_QUARTER_MV(3, 3, mx  , my-1)
491
        CHECK_QUARTER_MV(3, 0, mx  , my  )
492
        CHECK_QUARTER_MV(3, 1, mx  , my  )
493
        CHECK_QUARTER_MV(3, 2, mx  , my  )
494
        CHECK_QUARTER_MV(3, 3, mx  , my  )
495
        CHECK_QUARTER_MV(2, 3, mx  , my  )
496
        CHECK_QUARTER_MV(1, 3, mx  , my  )
497
        CHECK_QUARTER_MV(0, 3, mx  , my  )
498
        CHECK_QUARTER_MV(3, 3, mx-1, my  )
499
        CHECK_QUARTER_MV(2, 3, mx-1, my  )
500
        CHECK_QUARTER_MV(1, 3, mx-1, my  )
501
        CHECK_QUARTER_MV(1, 2, mx-1, my  )
502
        CHECK_QUARTER_MV(1, 1, mx-1, my  )
503
        CHECK_QUARTER_MV(1, 0, mx-1, my  )
504
#endif
505
        assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
506
507
        *mx_ptr = bx;
508
        *my_ptr = by;
509
    }else{
510
        *mx_ptr =4*mx;
511
        *my_ptr =4*my;
512
    }
513
514
    return dmin;
515
}
516
517 67725183 Michael Niedermayer
static int RENAME(qpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture, 
518 30952237 Michael Niedermayer
                                  uint8_t * const mv_penalty)
519 67725183 Michael Niedermayer
{
520
    const int size= 0;
521
    const int xx = 16 * s->mb_x;
522
    const int yy = 16 * s->mb_y;
523
    const int penalty_factor= s->me.mb_penalty_factor;
524
    const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these
525
    const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit
526
    me_cmp_func cmp_sub, chroma_cmp_sub;
527
    int d;
528
529
    LOAD_COMMON(xx, yy);
530
    
531
 //FIXME factorize
532
533
    cmp_sub= s->dsp.mb_cmp[size];
534
    chroma_cmp_sub= s->dsp.mb_cmp[size+1];
535
    
536
    assert(!s->me.skip);
537
    assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp);
538
539
    CMP_QPEL(d, mx&3, my&3, mx>>2, my>>2, size);
540
    //FIXME check cbp before adding penalty for (0,0) vector
541
    if(mx || my || size>0)
542
        d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
543
        
544
    return d;
545
}
546
547
548 1457ab52 Michael Niedermayer
#endif /* CMP_QPEL */
549
550
#define CHECK_MV(x,y)\
551
{\
552
    const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
553
    const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
554 b07a5980 Michael Niedermayer
/*printf("check_mv %d %d\n", x, y);*/\
555 1457ab52 Michael Niedermayer
    if(map[index]!=key){\
556
        CMP(d, x, y, size);\
557
        map[index]= key;\
558
        score_map[index]= d;\
559
        d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
560 b07a5980 Michael Niedermayer
/*printf("score:%d\n", d);*/\
561 1457ab52 Michael Niedermayer
        COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
562
    }\
563
}
564
565 b07a5980 Michael Niedermayer
#define CHECK_CLIPED_MV(ax,ay)\
566
{\
567
    const int x= FFMAX(xmin, FFMIN(ax, xmax));\
568
    const int y= FFMAX(ymin, FFMIN(ay, ymax));\
569
    CHECK_MV(x, y)\
570
}
571
572 1457ab52 Michael Niedermayer
#define CHECK_MV_DIR(x,y,new_dir)\
573
{\
574
    const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
575
    const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
576 b07a5980 Michael Niedermayer
/*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
577 1457ab52 Michael Niedermayer
    if(map[index]!=key){\
578
        CMP(d, x, y, size);\
579
        map[index]= key;\
580
        score_map[index]= d;\
581
        d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
582 b07a5980 Michael Niedermayer
/*printf("score:%d\n", d);*/\
583 1457ab52 Michael Niedermayer
        if(d<dmin){\
584
            best[0]=x;\
585
            best[1]=y;\
586
            dmin=d;\
587
            next_dir= new_dir;\
588
        }\
589
    }\
590
}
591
592
#define check(x,y,S,v)\
593
if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
594
if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
595
if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
596
if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
597
598
599
static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
600
                                       Picture *ref_picture,
601
                                       int const pred_x, int const pred_y, int const penalty_factor,
602
                                       int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
603 30952237 Michael Niedermayer
                                       uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty
604 1457ab52 Michael Niedermayer
                                       )
605
{
606
    me_cmp_func cmp, chroma_cmp;
607
    int next_dir=-1;
608
    LOAD_COMMON(s->mb_x*16, s->mb_y*16);
609
    
610
    cmp= s->dsp.me_cmp[size];
611
    chroma_cmp= s->dsp.me_cmp[size+1];
612
613 b07a5980 Michael Niedermayer
    { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
614
        const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
615
        const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
616
        if(map[index]!=key){ //this will be executed only very rarey
617
            CMP(score_map[index], best[0], best[1], size);
618
            map[index]= key;
619
        }
620
    }
621
622 1457ab52 Michael Niedermayer
    for(;;){
623
        int d;
624
        const int dir= next_dir;
625
        const int x= best[0];
626
        const int y= best[1];
627
        next_dir=-1;
628
629
//printf("%d", dir);
630
        if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y  , 0)
631
        if(dir!=3 && y>ymin) CHECK_MV_DIR(x  , y-1, 1)
632
        if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y  , 2)
633
        if(dir!=1 && y<ymax) CHECK_MV_DIR(x  , y+1, 3)
634
635
        if(next_dir==-1){
636
            return dmin;
637
        }
638
    }
639
}
640
641 b07a5980 Michael Niedermayer
static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
642
                                       Picture *ref_picture,
643
                                       int const pred_x, int const pred_y, int const penalty_factor,
644
                                       int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
645 30952237 Michael Niedermayer
                                       uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty
646 b07a5980 Michael Niedermayer
                                       )
647
{
648
    me_cmp_func cmp, chroma_cmp;
649
    int dia_size;
650
    LOAD_COMMON(s->mb_x*16, s->mb_y*16);
651
    
652
    cmp= s->dsp.me_cmp[size];
653
    chroma_cmp= s->dsp.me_cmp[size+1];
654
655
    for(dia_size=1; dia_size<=4; dia_size++){
656
        int dir;
657
        const int x= best[0];
658
        const int y= best[1];
659
        
660
        if(dia_size&(dia_size-1)) continue;
661
662
        if(   x + dia_size > xmax
663
           || x - dia_size < xmin
664
           || y + dia_size > ymax
665
           || y - dia_size < ymin)
666
           continue;
667
        
668
        for(dir= 0; dir<dia_size; dir+=2){
669
            int d;
670
671
            CHECK_MV(x + dir           , y + dia_size - dir);
672
            CHECK_MV(x + dia_size - dir, y - dir           );
673
            CHECK_MV(x - dir           , y - dia_size + dir);
674
            CHECK_MV(x - dia_size + dir, y + dir           );
675
        }
676
677
        if(x!=best[0] || y!=best[1])
678
            dia_size=0;
679
#if 0
680
{
681
int dx, dy, i;
682
static int stats[8*8];
683
dx= ABS(x-best[0]);
684
dy= ABS(y-best[1]);
685
if(dy>dx){
686
    dx^=dy; dy^=dx; dx^=dy;
687
}
688
stats[dy*8 + dx] ++;
689
if(256*256*256*64 % (stats[0]+1)==0){
690
    for(i=0; i<64; i++){
691
        if((i&7)==0) printf("\n");
692
        printf("%8d ", stats[i]);
693
    }
694
    printf("\n");
695
}
696
}
697
#endif
698
    }
699
    return dmin;    
700
}
701
702
#define SAB_CHECK_MV(ax,ay)\
703
{\
704
    const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
705
    const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
706
/*printf("sab check %d %d\n", ax, ay);*/\
707
    if(map[index]!=key){\
708
        CMP(d, ax, ay, size);\
709
        map[index]= key;\
710
        score_map[index]= d;\
711
        d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
712
/*printf("score: %d\n", d);*/\
713
        if(d < minima[minima_count-1].height){\
714
            int j=0;\
715
            \
716
            while(d >= minima[j].height) j++;\
717
\
718
            memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
719
\
720
            minima[j].checked= 0;\
721
            minima[j].height= d;\
722
            minima[j].x= ax;\
723
            minima[j].y= ay;\
724
            \
725
            i=-1;\
726
            continue;\
727
        }\
728
    }\
729
}
730
731
#define MAX_SAB_SIZE 16
732
static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
733
                                       Picture *ref_picture,
734
                                       int const pred_x, int const pred_y, int const penalty_factor,
735
                                       int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
736 30952237 Michael Niedermayer
                                       uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty
737 b07a5980 Michael Niedermayer
                                       )
738
{
739
    me_cmp_func cmp, chroma_cmp;
740
    Minima minima[MAX_SAB_SIZE];
741 826f429a Michael Niedermayer
    const int minima_count= ABS(s->me.dia_size);
742 b07a5980 Michael Niedermayer
    int i, j;
743
    LOAD_COMMON(s->mb_x*16, s->mb_y*16);
744
    
745
    cmp= s->dsp.me_cmp[size];
746
    chroma_cmp= s->dsp.me_cmp[size+1];
747
    
748
    for(j=i=0; i<ME_MAP_SIZE; i++){
749
        uint32_t key= map[i];
750
751
        key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
752
        
753
        if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
754
        
755
        assert(j<MAX_SAB_SIZE); //max j = number of predictors
756
        
757
        minima[j].height= score_map[i];
758
        minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
759
        minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
760
        minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
761
        minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
762
        minima[j].checked=0;
763
        if(minima[j].x || minima[j].y)
764
            minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
765
        
766
        j++;
767
    }
768
    
769
    qsort(minima, j, sizeof(Minima), minima_cmp);
770
    
771
    for(; j<minima_count; j++){
772
        minima[j].height=256*256*256*64;
773
        minima[j].checked=0;
774
        minima[j].x= minima[j].y=0;
775
    }
776
    
777
    for(i=0; i<minima_count; i++){
778
        const int x= minima[i].x;
779
        const int y= minima[i].y;
780
        int d;
781
        
782
        if(minima[i].checked) continue;
783
        
784
        if(   x >= xmax || x <= xmin
785
           || y >= ymax || y <= ymin)
786
           continue;
787
788
        SAB_CHECK_MV(x-1, y)
789
        SAB_CHECK_MV(x+1, y)
790
        SAB_CHECK_MV(x  , y-1)
791
        SAB_CHECK_MV(x  , y+1)
792
        
793
        minima[i].checked= 1;
794
    }
795
    
796
    best[0]= minima[0].x;
797
    best[1]= minima[0].y;
798
    dmin= minima[0].height;
799
    
800
    if(   best[0] < xmax && best[0] > xmin
801
       && best[1] < ymax && best[1] > ymin){
802
        int d;
803
        //ensure that the refernece samples for hpel refinement are in the map
804
        CHECK_MV(best[0]-1, best[1])
805
        CHECK_MV(best[0]+1, best[1])
806
        CHECK_MV(best[0], best[1]-1)
807
        CHECK_MV(best[0], best[1]+1)
808
    }
809
    return dmin;    
810
}
811
812 1457ab52 Michael Niedermayer
static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
813
                                       Picture *ref_picture,
814
                                       int const pred_x, int const pred_y, int const penalty_factor,
815
                                       int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
816 30952237 Michael Niedermayer
                                       uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty
817 1457ab52 Michael Niedermayer
                                       )
818
{
819
    me_cmp_func cmp, chroma_cmp;
820 b07a5980 Michael Niedermayer
    int dia_size;
821 1457ab52 Michael Niedermayer
    LOAD_COMMON(s->mb_x*16, s->mb_y*16);
822
    
823
    cmp= s->dsp.me_cmp[size];
824
    chroma_cmp= s->dsp.me_cmp[size+1];
825
826 826f429a Michael Niedermayer
    for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
827 1457ab52 Michael Niedermayer
        int dir, start, end;
828
        const int x= best[0];
829
        const int y= best[1];
830
831
        start= FFMAX(0, y + dia_size - ymax);
832 b07a5980 Michael Niedermayer
        end  = FFMIN(dia_size, xmax - x + 1);
833 1457ab52 Michael Niedermayer
        for(dir= start; dir<end; dir++){
834
            int d;
835
836
//check(x + dir,y + dia_size - dir,0, a0)
837
            CHECK_MV(x + dir           , y + dia_size - dir);
838
        }
839
840
        start= FFMAX(0, x + dia_size - xmax);
841 b07a5980 Michael Niedermayer
        end  = FFMIN(dia_size, y - ymin + 1);
842 1457ab52 Michael Niedermayer
        for(dir= start; dir<end; dir++){
843
            int d;
844
845
//check(x + dia_size - dir, y - dir,0, a1)
846
            CHECK_MV(x + dia_size - dir, y - dir           );
847
        }
848
849
        start= FFMAX(0, -y + dia_size + ymin );
850 b07a5980 Michael Niedermayer
        end  = FFMIN(dia_size, x - xmin + 1);
851 1457ab52 Michael Niedermayer
        for(dir= start; dir<end; dir++){
852
            int d;
853
854
//check(x - dir,y - dia_size + dir,0, a2)
855
            CHECK_MV(x - dir           , y - dia_size + dir);
856
        }
857
858
        start= FFMAX(0, -x + dia_size + xmin );
859 b07a5980 Michael Niedermayer
        end  = FFMIN(dia_size, ymax - y + 1);
860 1457ab52 Michael Niedermayer
        for(dir= start; dir<end; dir++){
861
            int d;
862
863
//check(x - dia_size + dir, y + dir,0, a3)
864
            CHECK_MV(x - dia_size + dir, y + dir           );
865
        }
866
867
        if(x!=best[0] || y!=best[1])
868
            dia_size=0;
869 b07a5980 Michael Niedermayer
#if 0
870
{
871
int dx, dy, i;
872
static int stats[8*8];
873
dx= ABS(x-best[0]);
874
dy= ABS(y-best[1]);
875
stats[dy*8 + dx] ++;
876
if(256*256*256*64 % (stats[0]+1)==0){
877
    for(i=0; i<64; i++){
878
        if((i&7)==0) printf("\n");
879
        printf("%6d ", stats[i]);
880
    }
881
    printf("\n");
882
}
883
}
884
#endif
885 1457ab52 Michael Niedermayer
    }
886
    return dmin;    
887
}
888
889
static int RENAME(epzs_motion_search)(MpegEncContext * s, int block,
890
                             int *mx_ptr, int *my_ptr,
891
                             int P[10][2], int pred_x, int pred_y,
892 b07a5980 Michael Niedermayer
                             int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], 
893 30952237 Michael Niedermayer
                             int ref_mv_scale, uint8_t * const mv_penalty)
894 1457ab52 Michael Niedermayer
{
895
    int best[2]={0, 0};
896
    int d, dmin; 
897
    const int shift= 1+s->quarter_sample;
898
    uint32_t *map= s->me.map;
899
    int map_generation;
900
    const int penalty_factor= s->me.penalty_factor;
901
    const int size=0;
902 7bc9090a Michael Niedermayer
    const int ref_mv_stride= s->mb_stride;
903
    const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride;
904 1457ab52 Michael Niedermayer
    me_cmp_func cmp, chroma_cmp;
905
    LOAD_COMMON(s->mb_x*16, s->mb_y*16);
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 f931ff7b Michael Niedermayer
    if (s->mb_y == 0) {
918 1457ab52 Michael Niedermayer
        CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
919 b07a5980 Michael Niedermayer
        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 1457ab52 Michael Niedermayer
    }else{
922
        if(dmin<256 && ( P_LEFT[0]    |P_LEFT[1]
923
                        |P_TOP[0]     |P_TOP[1]
924 b07a5980 Michael Niedermayer
                        |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
925 1457ab52 Michael Niedermayer
            *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 b07a5980 Michael Niedermayer
            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 1457ab52 Michael Niedermayer
            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 f931ff7b Michael Niedermayer
        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
            CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
944
                            (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
945
        }else{
946
            CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, 
947
                            (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
948
            CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
949
                            (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
950
        }
951 1457ab52 Michael Niedermayer
    }
952 b07a5980 Michael Niedermayer
953
    if(s->avctx->last_predictor_count){
954
        const int count= s->avctx->last_predictor_count;
955
        const int xstart= FFMAX(0, s->mb_x - count);
956
        const int ystart= FFMAX(0, s->mb_y - count);
957
        const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
958
        const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
959
        int mb_y;
960
961
        for(mb_y=ystart; mb_y<yend; mb_y++){
962
            int mb_x;
963
            for(mb_x=xstart; mb_x<xend; mb_x++){
964
                const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
965
                int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
966
                int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
967
968
                if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
969
                CHECK_MV(mx,my)
970 1457ab52 Michael Niedermayer
            }
971
        }
972
    }
973 b07a5980 Michael Niedermayer
974 1457ab52 Michael Niedermayer
//check(best[0],best[1],0, b0)
975 826f429a Michael Niedermayer
    if(s->me.dia_size==-1)
976 b07a5980 Michael Niedermayer
        dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
977
                                   pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
978
                                   shift, map, map_generation, size, mv_penalty);
979 826f429a Michael Niedermayer
    else if(s->me.dia_size<-1)
980 b07a5980 Michael Niedermayer
        dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
981
                                   pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
982
                                   shift, map, map_generation, size, mv_penalty);
983 826f429a Michael Niedermayer
    else if(s->me.dia_size<2)
984 1457ab52 Michael Niedermayer
        dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
985
                                   pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
986
                                   shift, map, map_generation, size, mv_penalty);
987
    else
988
        dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
989
                                   pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
990
                                   shift, map, map_generation, size, mv_penalty);
991
992
//check(best[0],best[1],0, b1)
993
    *mx_ptr= best[0];
994
    *my_ptr= best[1];    
995
996
//    printf("%d %d %d \n", best[0], best[1], dmin);
997
    return dmin;
998
}
999
1000
#ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
1001
static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block,
1002
                             int *mx_ptr, int *my_ptr,
1003
                             int P[10][2], int pred_x, int pred_y,
1004 b07a5980 Michael Niedermayer
                             int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], 
1005 30952237 Michael Niedermayer
                             int ref_mv_scale, uint8_t * const mv_penalty)
1006 1457ab52 Michael Niedermayer
{
1007
    int best[2]={0, 0};
1008
    int d, dmin; 
1009
    const int shift= 1+s->quarter_sample;
1010
    uint32_t *map= s->me.map;
1011
    int map_generation;
1012
    const int penalty_factor= s->me.penalty_factor;
1013
    const int size=1;
1014 7bc9090a Michael Niedermayer
    const int ref_mv_stride= s->mb_stride;
1015
    const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
1016 1457ab52 Michael Niedermayer
    me_cmp_func cmp, chroma_cmp;
1017
    LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8);
1018
    
1019
    cmp= s->dsp.me_cmp[size];
1020
    chroma_cmp= s->dsp.me_cmp[size+1];
1021
1022
    map_generation= update_map_generation(s);
1023
1024
    dmin = 1000000;
1025
//printf("%d %d %d %d //",xmin, ymin, xmax, ymax); 
1026
    /* first line */
1027 f931ff7b Michael Niedermayer
    if (s->mb_y == 0 && block<2) {
1028 1457ab52 Michael Niedermayer
        CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1029 b07a5980 Michael Niedermayer
        CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
1030
                        (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1031 1457ab52 Michael Niedermayer
        CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1032
    }else{
1033
        CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
1034
        //FIXME try some early stop
1035
        if(dmin>64*2){
1036
            CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
1037
            CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
1038
            CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
1039
            CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
1040 b07a5980 Michael Niedermayer
            CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, 
1041
                            (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
1042 1457ab52 Michael Niedermayer
        }
1043
    }
1044
    if(dmin>64*4){
1045 b07a5980 Michael Niedermayer
        CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, 
1046
                        (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
1047
        CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, 
1048
                        (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
1049 1457ab52 Michael Niedermayer
    }
1050
1051 826f429a Michael Niedermayer
    if(s->me.dia_size==-1)
1052 b07a5980 Michael Niedermayer
        dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
1053
                                   pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
1054
                                   shift, map, map_generation, size, mv_penalty);
1055 826f429a Michael Niedermayer
    else if(s->me.dia_size<-1)
1056 b07a5980 Michael Niedermayer
        dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
1057
                                   pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
1058
                                   shift, map, map_generation, size, mv_penalty);
1059 826f429a Michael Niedermayer
    else if(s->me.dia_size<2)
1060 1457ab52 Michael Niedermayer
        dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
1061
                                   pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
1062
                                   shift, map, map_generation, size, mv_penalty);
1063
    else
1064
        dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
1065
                                   pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, 
1066
                                   shift, map, map_generation, size, mv_penalty);
1067 b07a5980 Michael Niedermayer
1068 1457ab52 Michael Niedermayer
    *mx_ptr= best[0];
1069
    *my_ptr= best[1];    
1070
1071
//    printf("%d %d %d \n", best[0], best[1], dmin);
1072
    return dmin;
1073
}
1074
#endif /* !CMP_DIRECT */