Statistics
| Branch: | Revision:

ffmpeg / libavcodec / motion_est.c @ 622348f9

History | View | Annotate | Download (65.8 KB)

1
/*
2
 * Motion estimation 
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
4
 * Copyright (c) 2002-2003 Michael Niedermayer
5
 * 
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 *
21
 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
22
 */
23
 
24
/**
25
 * @file motion_est.c
26
 * Motion estimation.
27
 */
28
 
29
#include <stdlib.h>
30
#include <stdio.h>
31
#include <limits.h>
32
#include "avcodec.h"
33
#include "dsputil.h"
34
#include "mpegvideo.h"
35

    
36
//#undef NDEBUG
37
//#include <assert.h>
38

    
39
#define SQ(a) ((a)*(a))
40

    
41
#define P_LEFT P[1]
42
#define P_TOP P[2]
43
#define P_TOPRIGHT P[3]
44
#define P_MEDIAN P[4]
45
#define P_MV1 P[9]
46

    
47
static inline int sad_hpel_motion_search(MpegEncContext * s,
48
                                  int *mx_ptr, int *my_ptr, int dmin,
49
                                  int pred_x, int pred_y, uint8_t *src_data[3],
50
                                  uint8_t *ref_data[6], int stride, int uvstride,
51
                                  int size, int h, uint8_t * const mv_penalty);
52

    
53
static inline int update_map_generation(MpegEncContext * s)
54
{
55
    s->me.map_generation+= 1<<(ME_MAP_MV_BITS*2);
56
    if(s->me.map_generation==0){
57
        s->me.map_generation= 1<<(ME_MAP_MV_BITS*2);
58
        memset(s->me.map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
59
    }
60
    return s->me.map_generation;
61
}
62

    
63
/* shape adaptive search stuff */
64
typedef struct Minima{
65
    int height;
66
    int x, y;
67
    int checked;
68
}Minima;
69

    
70
static int minima_cmp(const void *a, const void *b){
71
    const Minima *da = (const Minima *) a;
72
    const Minima *db = (const Minima *) b;
73
    
74
    return da->height - db->height;
75
}
76
                                  
77
/* SIMPLE */
78
#define RENAME(a) simple_ ## a
79

    
80
#define CMP(d, x, y, size)\
81
d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride, h);
82

    
83
#define CMP_HPEL(d, dx, dy, x, y, size)\
84
{\
85
    const int dxy= (dx) + 2*(dy);\
86
    hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, h);\
87
    d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\
88
}
89

    
90

    
91
#define CMP_QPEL(d, dx, dy, x, y, size)\
92
{\
93
    const int dxy= (dx) + 4*(dy);\
94
    qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\
95
    d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\
96
}
97

    
98
#include "motion_est_template.c"
99
#undef RENAME
100
#undef CMP
101
#undef CMP_HPEL
102
#undef CMP_QPEL
103
#undef INIT
104

    
105
/* SIMPLE CHROMA */
106
#define RENAME(a) simple_chroma_ ## a
107

    
108
#define CMP(d, x, y, size)\
109
d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride, h);\
110
if(chroma_cmp){\
111
    int dxy= ((x)&1) + 2*((y)&1);\
112
    int c= ((x)>>1) + ((y)>>1)*uvstride;\
113
\
114
    chroma_hpel_put[0][dxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\
115
    d += chroma_cmp(s, s->me.scratchpad, src_u, uvstride, h>>1);\
116
    chroma_hpel_put[0][dxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\
117
    d += chroma_cmp(s, s->me.scratchpad, src_v, uvstride, h>>1);\
118
}
119

    
120
#define CMP_HPEL(d, dx, dy, x, y, size)\
121
{\
122
    const int dxy= (dx) + 2*(dy);\
123
    hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, h);\
124
    d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\
125
    if(chroma_cmp_sub){\
126
        int cxy= (dxy) | ((x)&1) | (2*((y)&1));\
127
        int c= ((x)>>1) + ((y)>>1)*uvstride;\
128
        chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\
129
        d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride, h>>1);\
130
        chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\
131
        d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride, h>>1);\
132
    }\
133
}
134

    
135
#define CMP_QPEL(d, dx, dy, x, y, size)\
136
{\
137
    const int dxy= (dx) + 4*(dy);\
138
    qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\
139
    d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\
140
    if(chroma_cmp_sub){\
141
        int cxy, c;\
142
        int cx= (4*(x) + (dx))/2;\
143
        int cy= (4*(y) + (dy))/2;\
144
        cx= (cx>>1)|(cx&1);\
145
        cy= (cy>>1)|(cy&1);\
146
        cxy= (cx&1) + 2*(cy&1);\
147
        c= ((cx)>>1) + ((cy)>>1)*uvstride;\
148
        chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\
149
        d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride, h>>1);\
150
        chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\
151
        d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride, h>>1);\
152
    }\
153
}
154

    
155
#include "motion_est_template.c"
156
#undef RENAME
157
#undef CMP
158
#undef CMP_HPEL
159
#undef CMP_QPEL
160
#undef INIT
161

    
162
/* SIMPLE DIRECT HPEL */
163
#define RENAME(a) simple_direct_hpel_ ## a
164
//FIXME precalc divisions stuff
165

    
166
#define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\
167
if((x) >= xmin && 2*(x) + (dx) <= 2*xmax && (y) >= ymin && 2*(y) + (dy) <= 2*ymax){\
168
    const int hx= 2*(x) + (dx);\
169
    const int hy= 2*(y) + (dy);\
170
    if(s->mv_type==MV_TYPE_8X8){\
171
        int i;\
172
        for(i=0; i<4; i++){\
173
            int fx = s->me.direct_basis_mv[i][0] + hx;\
174
            int fy = s->me.direct_basis_mv[i][1] + hy;\
175
            int bx = hx ? fx - s->me.co_located_mv[i][0] : s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + (i &1)*16;\
176
            int by = hy ? fy - s->me.co_located_mv[i][1] : s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + (i>>1)*16;\
177
            int fxy= (fx&1) + 2*(fy&1);\
178
            int bxy= (bx&1) + 2*(by&1);\
179
\
180
            uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\
181
            hpel_put[1][fxy](dst, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 8);\
182
            hpel_avg[1][bxy](dst, (ref_data[3]) + (bx>>1) + (by>>1)*(stride), stride, 8);\
183
        }\
184
    }else{\
185
        int fx = s->me.direct_basis_mv[0][0] + hx;\
186
        int fy = s->me.direct_basis_mv[0][1] + hy;\
187
        int bx = hx ? fx - s->me.co_located_mv[0][0] : (s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp);\
188
        int by = hy ? fy - s->me.co_located_mv[0][1] : (s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp);\
189
        int fxy= (fx&1) + 2*(fy&1);\
190
        int bxy= (bx&1) + 2*(by&1);\
191
        \
192
        assert((fx>>1) + 16*s->mb_x >= -16);\
193
        assert((fy>>1) + 16*s->mb_y >= -16);\
194
        assert((fx>>1) + 16*s->mb_x <= s->width);\
195
        assert((fy>>1) + 16*s->mb_y <= s->height);\
196
        assert((bx>>1) + 16*s->mb_x >= -16);\
197
        assert((by>>1) + 16*s->mb_y >= -16);\
198
        assert((bx>>1) + 16*s->mb_x <= s->width);\
199
        assert((by>>1) + 16*s->mb_y <= s->height);\
200
\
201
        hpel_put[0][fxy](s->me.scratchpad, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 16);\
202
        hpel_avg[0][bxy](s->me.scratchpad, (ref_data[3]) + (bx>>1) + (by>>1)*(stride), stride, 16);\
203
    }\
204
    d = cmp_func(s, s->me.scratchpad, src_y, stride, 16);\
205
}else\
206
    d= 256*256*256*32;
207

    
208

    
209
#define CMP_HPEL(d, dx, dy, x, y, size)\
210
    CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub)
211

    
212
#define CMP(d, x, y, size)\
213
    CMP_DIRECT(d, 0, 0, x, y, size, cmp)
214
    
215
#include "motion_est_template.c"
216
#undef RENAME
217
#undef CMP
218
#undef CMP_HPEL
219
#undef CMP_QPEL
220
#undef INIT
221
#undef CMP_DIRECT
222

    
223
/* SIMPLE DIRECT QPEL */
224
#define RENAME(a) simple_direct_qpel_ ## a
225

    
226
#define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\
227
if((x) >= xmin && 4*(x) + (dx) <= 4*xmax && (y) >= ymin && 4*(y) + (dy) <= 4*ymax){\
228
    const int qx= 4*(x) + (dx);\
229
    const int qy= 4*(y) + (dy);\
230
    if(s->mv_type==MV_TYPE_8X8){\
231
        int i;\
232
        for(i=0; i<4; i++){\
233
            int fx = s->me.direct_basis_mv[i][0] + qx;\
234
            int fy = s->me.direct_basis_mv[i][1] + qy;\
235
            int bx = qx ? fx - s->me.co_located_mv[i][0] : s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + (i &1)*16;\
236
            int by = qy ? fy - s->me.co_located_mv[i][1] : s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + (i>>1)*16;\
237
            int fxy= (fx&3) + 4*(fy&3);\
238
            int bxy= (bx&3) + 4*(by&3);\
239
\
240
            uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\
241
            qpel_put[1][fxy](dst, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\
242
            qpel_avg[1][bxy](dst, (ref_data[3]) + (bx>>2) + (by>>2)*(stride), stride);\
243
        }\
244
    }else{\
245
        int fx = s->me.direct_basis_mv[0][0] + qx;\
246
        int fy = s->me.direct_basis_mv[0][1] + qy;\
247
        int bx = qx ? fx - s->me.co_located_mv[0][0] : s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp;\
248
        int by = qy ? fy - s->me.co_located_mv[0][1] : s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp;\
249
        int fxy= (fx&3) + 4*(fy&3);\
250
        int bxy= (bx&3) + 4*(by&3);\
251
\
252
        qpel_put[1][fxy](s->me.scratchpad               , (ref_y ) + (fx>>2) + (fy>>2)*(stride)               , stride);\
253
        qpel_put[1][fxy](s->me.scratchpad + 8           , (ref_y ) + (fx>>2) + (fy>>2)*(stride) + 8           , stride);\
254
        qpel_put[1][fxy](s->me.scratchpad     + 8*stride, (ref_y ) + (fx>>2) + (fy>>2)*(stride)     + 8*stride, stride);\
255
        qpel_put[1][fxy](s->me.scratchpad + 8 + 8*stride, (ref_y ) + (fx>>2) + (fy>>2)*(stride) + 8 + 8*stride, stride);\
256
        qpel_avg[1][bxy](s->me.scratchpad               , (ref_data[3]) + (bx>>2) + (by>>2)*(stride)               , stride);\
257
        qpel_avg[1][bxy](s->me.scratchpad + 8           , (ref_data[3]) + (bx>>2) + (by>>2)*(stride) + 8           , stride);\
258
        qpel_avg[1][bxy](s->me.scratchpad     + 8*stride, (ref_data[3]) + (bx>>2) + (by>>2)*(stride)     + 8*stride, stride);\
259
        qpel_avg[1][bxy](s->me.scratchpad + 8 + 8*stride, (ref_data[3]) + (bx>>2) + (by>>2)*(stride) + 8 + 8*stride, stride);\
260
    }\
261
    d = cmp_func(s, s->me.scratchpad, src_y, stride, 16);\
262
}else\
263
    d= 256*256*256*32;
264

    
265

    
266
#define CMP_QPEL(d, dx, dy, x, y, size)\
267
    CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub)
268

    
269
#define CMP(d, x, y, size)\
270
    CMP_DIRECT(d, 0, 0, x, y, size, cmp)
271

    
272
#include "motion_est_template.c"
273
#undef RENAME
274
#undef CMP
275
#undef CMP_HPEL
276
#undef CMP_QPEL
277
#undef INIT
278
#undef CMP__DIRECT
279

    
280
static inline int get_penalty_factor(MpegEncContext *s, int type){
281
    switch(type&0xFF){
282
    default:
283
    case FF_CMP_SAD:
284
        return s->qscale*2;
285
    case FF_CMP_DCT:
286
        return s->qscale*3;
287
    case FF_CMP_SATD:
288
        return s->qscale*6;
289
    case FF_CMP_SSE:
290
        return s->qscale*s->qscale*2;
291
    case FF_CMP_BIT:
292
        return 1;
293
    case FF_CMP_RD:
294
    case FF_CMP_PSNR:
295
        return (s->qscale*s->qscale*185 + 64)>>7;
296
    }
297
}
298

    
299
void ff_init_me(MpegEncContext *s){
300
    ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, s->avctx->me_pre_cmp);
301
    ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
302
    ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
303
    ff_set_cmp(&s->dsp, s->dsp.mb_cmp, s->avctx->mb_cmp);
304

    
305
    if(s->flags&CODEC_FLAG_QPEL){
306
        if(s->avctx->me_sub_cmp&FF_CMP_CHROMA)
307
            s->me.sub_motion_search= simple_chroma_qpel_motion_search;
308
        else
309
            s->me.sub_motion_search= simple_qpel_motion_search;
310
    }else{
311
        if(s->avctx->me_sub_cmp&FF_CMP_CHROMA)
312
            s->me.sub_motion_search= simple_chroma_hpel_motion_search;
313
        else if(   s->avctx->me_sub_cmp == FF_CMP_SAD 
314
                && s->avctx->    me_cmp == FF_CMP_SAD 
315
                && s->avctx->    mb_cmp == FF_CMP_SAD)
316
            s->me.sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
317
        else
318
            s->me.sub_motion_search= simple_hpel_motion_search;
319
    }
320

    
321
    if(s->avctx->me_cmp&FF_CMP_CHROMA){
322
        s->me.motion_search[0]= simple_chroma_epzs_motion_search;
323
        s->me.motion_search[1]= simple_chroma_epzs_motion_search4;
324
        s->me.motion_search[4]= simple_chroma_epzs_motion_search2;
325
    }else{
326
        s->me.motion_search[0]= simple_epzs_motion_search;
327
        s->me.motion_search[1]= simple_epzs_motion_search4;
328
        s->me.motion_search[4]= simple_epzs_motion_search2;
329
    }
330
    
331
    if(s->avctx->me_pre_cmp&FF_CMP_CHROMA){
332
        s->me.pre_motion_search= simple_chroma_epzs_motion_search;
333
    }else{
334
        s->me.pre_motion_search= simple_epzs_motion_search;
335
    }
336
    
337
    if(s->flags&CODEC_FLAG_QPEL){
338
        if(s->avctx->mb_cmp&FF_CMP_CHROMA)
339
            s->me.get_mb_score= simple_chroma_qpel_get_mb_score;
340
        else
341
            s->me.get_mb_score= simple_qpel_get_mb_score;
342
    }else{
343
        if(s->avctx->mb_cmp&FF_CMP_CHROMA)
344
            s->me.get_mb_score= simple_chroma_hpel_get_mb_score;
345
        else
346
            s->me.get_mb_score= simple_hpel_get_mb_score;
347
    }
348
}
349
      
350
#if 0
351
static int pix_dev(uint8_t * pix, int line_size, int mean)
352
{
353
    int s, i, j;
354

355
    s = 0;
356
    for (i = 0; i < 16; i++) {
357
        for (j = 0; j < 16; j += 8) {
358
            s += ABS(pix[0]-mean);
359
            s += ABS(pix[1]-mean);
360
            s += ABS(pix[2]-mean);
361
            s += ABS(pix[3]-mean);
362
            s += ABS(pix[4]-mean);
363
            s += ABS(pix[5]-mean);
364
            s += ABS(pix[6]-mean);
365
            s += ABS(pix[7]-mean);
366
            pix += 8;
367
        }
368
        pix += line_size - 16;
369
    }
370
    return s;
371
}
372
#endif
373

    
374
static inline void no_motion_search(MpegEncContext * s,
375
                                    int *mx_ptr, int *my_ptr)
376
{
377
    *mx_ptr = 16 * s->mb_x;
378
    *my_ptr = 16 * s->mb_y;
379
}
380

    
381
static int full_motion_search(MpegEncContext * s,
382
                              int *mx_ptr, int *my_ptr, int range,
383
                              int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
384
{
385
    int x1, y1, x2, y2, xx, yy, x, y;
386
    int mx, my, dmin, d;
387
    uint8_t *pix;
388

    
389
    xx = 16 * s->mb_x;
390
    yy = 16 * s->mb_y;
391
    x1 = xx - range + 1;        /* we loose one pixel to avoid boundary pb with half pixel pred */
392
    if (x1 < xmin)
393
        x1 = xmin;
394
    x2 = xx + range - 1;
395
    if (x2 > xmax)
396
        x2 = xmax;
397
    y1 = yy - range + 1;
398
    if (y1 < ymin)
399
        y1 = ymin;
400
    y2 = yy + range - 1;
401
    if (y2 > ymax)
402
        y2 = ymax;
403
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
404
    dmin = 0x7fffffff;
405
    mx = 0;
406
    my = 0;
407
    for (y = y1; y <= y2; y++) {
408
        for (x = x1; x <= x2; x++) {
409
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x,
410
                             s->linesize, 16);
411
            if (d < dmin ||
412
                (d == dmin &&
413
                 (abs(x - xx) + abs(y - yy)) <
414
                 (abs(mx - xx) + abs(my - yy)))) {
415
                dmin = d;
416
                mx = x;
417
                my = y;
418
            }
419
        }
420
    }
421

    
422
    *mx_ptr = mx;
423
    *my_ptr = my;
424

    
425
#if 0
426
    if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
427
        *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
428
        fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
429
    }
430
#endif
431
    return dmin;
432
}
433

    
434

    
435
static int log_motion_search(MpegEncContext * s,
436
                             int *mx_ptr, int *my_ptr, int range,
437
                             int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
438
{
439
    int x1, y1, x2, y2, xx, yy, x, y;
440
    int mx, my, dmin, d;
441
    uint8_t *pix;
442

    
443
    xx = s->mb_x << 4;
444
    yy = s->mb_y << 4;
445

    
446
    /* Left limit */
447
    x1 = xx - range;
448
    if (x1 < xmin)
449
        x1 = xmin;
450

    
451
    /* Right limit */
452
    x2 = xx + range;
453
    if (x2 > xmax)
454
        x2 = xmax;
455

    
456
    /* Upper limit */
457
    y1 = yy - range;
458
    if (y1 < ymin)
459
        y1 = ymin;
460

    
461
    /* Lower limit */
462
    y2 = yy + range;
463
    if (y2 > ymax)
464
        y2 = ymax;
465

    
466
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
467
    dmin = 0x7fffffff;
468
    mx = 0;
469
    my = 0;
470

    
471
    do {
472
        for (y = y1; y <= y2; y += range) {
473
            for (x = x1; x <= x2; x += range) {
474
                d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
475
                if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
476
                    dmin = d;
477
                    mx = x;
478
                    my = y;
479
                }
480
            }
481
        }
482

    
483
        range = range >> 1;
484

    
485
        x1 = mx - range;
486
        if (x1 < xmin)
487
            x1 = xmin;
488

    
489
        x2 = mx + range;
490
        if (x2 > xmax)
491
            x2 = xmax;
492

    
493
        y1 = my - range;
494
        if (y1 < ymin)
495
            y1 = ymin;
496

    
497
        y2 = my + range;
498
        if (y2 > ymax)
499
            y2 = ymax;
500

    
501
    } while (range >= 1);
502

    
503
#ifdef DEBUG
504
    fprintf(stderr, "log       - MX: %d\tMY: %d\n", mx, my);
505
#endif
506
    *mx_ptr = mx;
507
    *my_ptr = my;
508
    return dmin;
509
}
510

    
511
static int phods_motion_search(MpegEncContext * s,
512
                               int *mx_ptr, int *my_ptr, int range,
513
                               int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
514
{
515
    int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
516
    int mx, my, dminx, dminy;
517
    uint8_t *pix;
518

    
519
    xx = s->mb_x << 4;
520
    yy = s->mb_y << 4;
521

    
522
    /* Left limit */
523
    x1 = xx - range;
524
    if (x1 < xmin)
525
        x1 = xmin;
526

    
527
    /* Right limit */
528
    x2 = xx + range;
529
    if (x2 > xmax)
530
        x2 = xmax;
531

    
532
    /* Upper limit */
533
    y1 = yy - range;
534
    if (y1 < ymin)
535
        y1 = ymin;
536

    
537
    /* Lower limit */
538
    y2 = yy + range;
539
    if (y2 > ymax)
540
        y2 = ymax;
541

    
542
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
543
    mx = 0;
544
    my = 0;
545

    
546
    x = xx;
547
    y = yy;
548
    do {
549
        dminx = 0x7fffffff;
550
        dminy = 0x7fffffff;
551

    
552
        lastx = x;
553
        for (x = x1; x <= x2; x += range) {
554
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
555
            if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
556
                dminx = d;
557
                mx = x;
558
            }
559
        }
560

    
561
        x = lastx;
562
        for (y = y1; y <= y2; y += range) {
563
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
564
            if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
565
                dminy = d;
566
                my = y;
567
            }
568
        }
569

    
570
        range = range >> 1;
571

    
572
        x = mx;
573
        y = my;
574
        x1 = mx - range;
575
        if (x1 < xmin)
576
            x1 = xmin;
577

    
578
        x2 = mx + range;
579
        if (x2 > xmax)
580
            x2 = xmax;
581

    
582
        y1 = my - range;
583
        if (y1 < ymin)
584
            y1 = ymin;
585

    
586
        y2 = my + range;
587
        if (y2 > ymax)
588
            y2 = ymax;
589

    
590
    } while (range >= 1);
591

    
592
#ifdef DEBUG
593
    fprintf(stderr, "phods     - MX: %d\tMY: %d\n", mx, my);
594
#endif
595

    
596
    /* half pixel search */
597
    *mx_ptr = mx;
598
    *my_ptr = my;
599
    return dminy;
600
}
601

    
602

    
603
#define Z_THRESHOLD 256
604

    
605
#define CHECK_SAD_HALF_MV(suffix, x, y) \
606
{\
607
    d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
608
    d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
609
    COPY3_IF_LT(dminh, d, dx, x, dy, y)\
610
}
611

    
612
static inline int sad_hpel_motion_search(MpegEncContext * s,
613
                                  int *mx_ptr, int *my_ptr, int dmin,
614
                                  int pred_x, int pred_y, uint8_t *src_data[3],
615
                                  uint8_t *ref_data[6], int stride, int uvstride,                                  
616
                                  int size, int h, uint8_t * const mv_penalty)
617
{
618
    uint32_t *score_map= s->me.score_map;
619
    const int penalty_factor= s->me.sub_penalty_factor;
620
    int mx, my, dminh;
621
    uint8_t *pix, *ptr;
622
    const int xmin= s->me.xmin;
623
    const int ymin= s->me.ymin;
624
    const int xmax= s->me.xmax;
625
    const int ymax= s->me.ymax;
626

    
627
    if(s->me.skip){
628
//    printf("S");
629
        *mx_ptr = 0;
630
        *my_ptr = 0;
631
        return dmin;
632
    }
633
//    printf("N");
634
        
635
    pix = src_data[0];
636

    
637
    mx = *mx_ptr;
638
    my = *my_ptr;
639
    ptr = ref_data[0] + (my * stride) + mx;
640
    
641
    dminh = dmin;
642

    
643
    if (mx > xmin && mx < xmax && 
644
        my > ymin && my < ymax) {
645
        int dx=0, dy=0;
646
        int d, pen_x, pen_y; 
647
        const int index= (my<<ME_MAP_SHIFT) + mx;
648
        const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
649
        const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)];
650
        const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)];
651
        const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
652
        mx<<=1;
653
        my<<=1;
654

    
655
        
656
        pen_x= pred_x + mx;
657
        pen_y= pred_y + my;
658

    
659
        ptr-= stride;
660
        if(t<=b){
661
            CHECK_SAD_HALF_MV(y2 , 0, -1)
662
            if(l<=r){
663
                CHECK_SAD_HALF_MV(xy2, -1, -1)
664
                if(t+r<=b+l){
665
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
666
                    ptr+= stride;
667
                }else{
668
                    ptr+= stride;
669
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
670
                }
671
                CHECK_SAD_HALF_MV(x2 , -1,  0)
672
            }else{
673
                CHECK_SAD_HALF_MV(xy2, +1, -1)
674
                if(t+l<=b+r){
675
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
676
                    ptr+= stride;
677
                }else{
678
                    ptr+= stride;
679
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
680
                }
681
                CHECK_SAD_HALF_MV(x2 , +1,  0)
682
            }
683
        }else{
684
            if(l<=r){
685
                if(t+l<=b+r){
686
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
687
                    ptr+= stride;
688
                }else{
689
                    ptr+= stride;
690
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
691
                }
692
                CHECK_SAD_HALF_MV(x2 , -1,  0)
693
                CHECK_SAD_HALF_MV(xy2, -1, +1)
694
            }else{
695
                if(t+r<=b+l){
696
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
697
                    ptr+= stride;
698
                }else{
699
                    ptr+= stride;
700
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
701
                }
702
                CHECK_SAD_HALF_MV(x2 , +1,  0)
703
                CHECK_SAD_HALF_MV(xy2, +1, +1)
704
            }
705
            CHECK_SAD_HALF_MV(y2 ,  0, +1)
706
        }
707
        mx+=dx;
708
        my+=dy;
709

    
710
    }else{
711
        mx<<=1;
712
        my<<=1;
713
    }
714

    
715
    *mx_ptr = mx;
716
    *my_ptr = my;
717
    return dminh;
718
}
719

    
720
static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
721
{
722
    const int xy= s->mb_x + s->mb_y*s->mb_stride;
723
    
724
    s->p_mv_table[xy][0] = mx;
725
    s->p_mv_table[xy][1] = my;
726

    
727
    /* has allready been set to the 4 MV if 4MV is done */
728
    if(mv4){
729
        int mot_xy= s->block_index[0];
730

    
731
        s->current_picture.motion_val[0][mot_xy  ][0]= mx;
732
        s->current_picture.motion_val[0][mot_xy  ][1]= my;
733
        s->current_picture.motion_val[0][mot_xy+1][0]= mx;
734
        s->current_picture.motion_val[0][mot_xy+1][1]= my;
735

    
736
        mot_xy += s->block_wrap[0];
737
        s->current_picture.motion_val[0][mot_xy  ][0]= mx;
738
        s->current_picture.motion_val[0][mot_xy  ][1]= my;
739
        s->current_picture.motion_val[0][mot_xy+1][0]= mx;
740
        s->current_picture.motion_val[0][mot_xy+1][1]= my;
741
    }
742
}
743

    
744
/**
745
 * get fullpel ME search limits.
746
 */
747
static inline void get_limits(MpegEncContext *s, int x, int y)
748
{
749
/*
750
    if(s->avctx->me_range) s->me.range= s->avctx->me_range >> 1;
751
    else                   s->me.range= 16;
752
*/
753
    if (s->unrestricted_mv) {
754
        s->me.xmin = - x - 16;
755
        s->me.ymin = - y - 16;
756
        s->me.xmax = - x + s->mb_width *16;
757
        s->me.ymax = - y + s->mb_height*16;
758
    } else {
759
        s->me.xmin = - x;
760
        s->me.ymin = - y;
761
        s->me.xmax = - x + s->mb_width *16 - 16;
762
        s->me.ymax = - y + s->mb_height*16 - 16;
763
    }
764
}
765

    
766
static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
767
{
768
    const int size= 1;
769
    const int h=8;
770
    int block;
771
    int P[10][2];
772
    int dmin_sum=0, mx4_sum=0, my4_sum=0;
773
    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
774
    int same=1;
775
    const int stride= s->linesize;
776
    const int uvstride= s->uvlinesize;
777
    const int xmin= s->me.xmin;
778
    const int ymin= s->me.ymin;
779
    const int xmax= s->me.xmax;
780
    const int ymax= s->me.ymax;
781

    
782
    for(block=0; block<4; block++){
783
        int mx4, my4;
784
        int pred_x4, pred_y4;
785
        int dmin4;
786
        static const int off[4]= {2, 1, 1, -1};
787
        const int mot_stride = s->block_wrap[0];
788
        const int mot_xy = s->block_index[block];
789
        const int block_x= (block&1);
790
        const int block_y= (block>>1);
791
        uint8_t *src_data[3]= {
792
            s->new_picture.data[0] + 8*(2*s->mb_x + block_x) + stride  *8*(2*s->mb_y + block_y), //FIXME chroma?
793
            s->new_picture.data[1] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y),
794
            s->new_picture.data[2] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y)
795
        };
796
        uint8_t *ref_data[3]= {
797
            s->last_picture.data[0] + 8*(2*s->mb_x + block_x) + stride  *8*(2*s->mb_y + block_y), //FIXME chroma?
798
            s->last_picture.data[1] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y),
799
            s->last_picture.data[2] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y)
800
        };
801

    
802
        P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
803
        P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
804

    
805
        if(P_LEFT[0]       > (s->me.xmax<<shift)) P_LEFT[0]       = (s->me.xmax<<shift);
806

    
807
        /* special case for first line */
808
        if (s->mb_y == 0 && block<2) {
809
            pred_x4= P_LEFT[0];
810
            pred_y4= P_LEFT[1];
811
        } else {
812
            P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride             ][0];
813
            P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride             ][1];
814
            P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
815
            P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
816
            if(P_TOP[1]      > (s->me.ymax<<shift)) P_TOP[1]     = (s->me.ymax<<shift);
817
            if(P_TOPRIGHT[0] < (s->me.xmin<<shift)) P_TOPRIGHT[0]= (s->me.xmin<<shift);
818
            if(P_TOPRIGHT[0] > (s->me.xmax<<shift)) P_TOPRIGHT[0]= (s->me.xmax<<shift);
819
            if(P_TOPRIGHT[1] > (s->me.ymax<<shift)) P_TOPRIGHT[1]= (s->me.ymax<<shift);
820
    
821
            P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
822
            P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
823

    
824
//            if(s->out_format == FMT_H263){
825
                pred_x4 = P_MEDIAN[0];
826
                pred_y4 = P_MEDIAN[1];
827
#if 0
828
            }else { /* mpeg1 at least */
829
                pred_x4= P_LEFT[0];
830
                pred_y4= P_LEFT[1];
831
            }
832
#endif
833
        }
834
        P_MV1[0]= mx;
835
        P_MV1[1]= my;
836

    
837
        dmin4 = s->me.motion_search[1](s, &mx4, &my4, P, pred_x4, pred_y4, 
838
                                       src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty);
839

    
840
        dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, 
841
                                          pred_x4, pred_y4, src_data, ref_data, stride, uvstride, size, h, mv_penalty);
842
        
843
        if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
844
            int dxy;
845
            const int offset= ((block&1) + (block>>1)*stride)*8;
846
            uint8_t *dest_y = s->me.scratchpad + offset;
847

    
848
            if(s->quarter_sample){
849
                uint8_t *ref= ref_data[0] + (mx4>>2) + (my4>>2)*stride + offset;
850
                dxy = ((my4 & 3) << 2) | (mx4 & 3);
851

    
852
                if(s->no_rounding)
853
                    s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y   , ref    , s->linesize);
854
                else
855
                    s->dsp.put_qpel_pixels_tab       [1][dxy](dest_y   , ref    , stride);
856
            }else{
857
                uint8_t *ref= ref_data[0] + (mx4>>1) + (my4>>1)*stride + offset;
858
                dxy = ((my4 & 1) << 1) | (mx4 & 1);
859

    
860
                if(s->no_rounding)
861
                    s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y    , ref    , stride, h);
862
                else
863
                    s->dsp.put_pixels_tab       [1][dxy](dest_y    , ref    , stride, h);
864
            }
865
            dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*s->me.mb_penalty_factor;
866
        }else
867
            dmin_sum+= dmin4;
868

    
869
        if(s->quarter_sample){
870
            mx4_sum+= mx4/2;
871
            my4_sum+= my4/2;
872
        }else{
873
            mx4_sum+= mx4;
874
            my4_sum+= my4;
875
        }
876
            
877
        s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4;
878
        s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4;
879

    
880
        if(mx4 != mx || my4 != my) same=0;
881
    }
882
    
883
    if(same)
884
        return INT_MAX;
885
    
886
    if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
887
        dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, s->me.scratchpad, stride, 16);
888
    }
889
    
890
    if(s->avctx->mb_cmp&FF_CMP_CHROMA){
891
        int dxy;
892
        int mx, my;
893
        int offset;
894

    
895
        mx= ff_h263_round_chroma(mx4_sum);
896
        my= ff_h263_round_chroma(my4_sum);
897
        dxy = ((my & 1) << 1) | (mx & 1);
898
        
899
        offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
900
       
901
        if(s->no_rounding){
902
            s->dsp.put_no_rnd_pixels_tab[1][dxy](s->me.scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
903
            s->dsp.put_no_rnd_pixels_tab[1][dxy](s->me.scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
904
        }else{
905
            s->dsp.put_pixels_tab       [1][dxy](s->me.scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
906
            s->dsp.put_pixels_tab       [1][dxy](s->me.scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
907
        }
908

    
909
        dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad  , s->uvlinesize, 8);
910
        dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad+8, s->uvlinesize, 8);
911
    }
912

    
913
    switch(s->avctx->mb_cmp&0xFF){
914
    /*case FF_CMP_SSE:
915
        return dmin_sum+ 32*s->qscale*s->qscale;*/
916
    case FF_CMP_RD:
917
        return dmin_sum;
918
    default:
919
        return dmin_sum+ 11*s->me.mb_penalty_factor;
920
    }
921
}
922

    
923
static int interlaced_search(MpegEncContext *s, uint8_t *frame_src_data[3], uint8_t *frame_ref_data[3], 
924
                             int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int f_code, int mx, int my)
925
{
926
    const int size=0;
927
    const int h=8;
928
    int block;
929
    int P[10][2];
930
    uint8_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV;
931
    int same=1;
932
    const int stride= 2*s->linesize;
933
    const int uvstride= 2*s->uvlinesize;
934
    int dmin_sum= 0;
935
    const int mot_stride= s->mb_stride;
936
    const int xy= s->mb_x + s->mb_y*mot_stride;
937
    
938
    s->me.ymin>>=1;
939
    s->me.ymax>>=1;
940
    
941
    for(block=0; block<2; block++){
942
        int field_select;
943
        int best_dmin= INT_MAX;
944
        int best_field= -1;
945

    
946
        uint8_t *src_data[3]= {
947
            frame_src_data[0] + s->  linesize*block,
948
            frame_src_data[1] + s->uvlinesize*block,
949
            frame_src_data[2] + s->uvlinesize*block
950
        };
951

    
952
        for(field_select=0; field_select<2; field_select++){
953
            int dmin, mx_i, my_i, pred_x, pred_y;
954
            uint8_t *ref_data[3]= {
955
                frame_ref_data[0] + s->  linesize*field_select,
956
                frame_ref_data[1] + s->uvlinesize*field_select,
957
                frame_ref_data[2] + s->uvlinesize*field_select
958
            };
959
            int16_t (*mv_table)[2]= mv_tables[block][field_select];
960
            
961
            P_LEFT[0] = mv_table[xy - 1][0];
962
            P_LEFT[1] = mv_table[xy - 1][1];
963
            if(P_LEFT[0]       > (s->me.xmax<<1)) P_LEFT[0]       = (s->me.xmax<<1);
964
            
965
            pred_x= P_LEFT[0];
966
            pred_y= P_LEFT[1];
967
            
968
            if(s->mb_y){
969
                P_TOP[0]      = mv_table[xy - mot_stride][0];
970
                P_TOP[1]      = mv_table[xy - mot_stride][1];
971
                P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
972
                P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
973
                if(P_TOP[1]      > (s->me.ymax<<1)) P_TOP[1]     = (s->me.ymax<<1);
974
                if(P_TOPRIGHT[0] < (s->me.xmin<<1)) P_TOPRIGHT[0]= (s->me.xmin<<1);
975
                if(P_TOPRIGHT[0] > (s->me.xmax<<1)) P_TOPRIGHT[0]= (s->me.xmax<<1);
976
                if(P_TOPRIGHT[1] > (s->me.ymax<<1)) P_TOPRIGHT[1]= (s->me.ymax<<1);
977
    
978
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
979
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
980
            }
981
            P_MV1[0]= mx; //FIXME not correct if block != field_select
982
            P_MV1[1]= my / 2;
983
            
984
            dmin = s->me.motion_search[4](s, &mx_i, &my_i, P, pred_x, pred_y, 
985
                                           src_data, ref_data, stride, uvstride, mv_table, (1<<16)>>1, mv_penalty);
986

    
987
            dmin= s->me.sub_motion_search(s, &mx_i, &my_i, dmin, 
988
                                           pred_x, pred_y, src_data, ref_data, stride, uvstride, size, h, mv_penalty);
989
            
990
            mv_table[xy][0]= mx_i;
991
            mv_table[xy][1]= my_i;
992
            
993
            if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
994
                int dxy;
995

    
996
                //FIXME chroma ME
997
                uint8_t *ref= ref_data[0] + (mx_i>>1) + (my_i>>1)*stride;
998
                dxy = ((my_i & 1) << 1) | (mx_i & 1);
999

    
1000
                if(s->no_rounding){
1001
                    s->dsp.put_no_rnd_pixels_tab[size][dxy](s->me.scratchpad, ref    , stride, h);
1002
                }else{
1003
                    s->dsp.put_pixels_tab       [size][dxy](s->me.scratchpad, ref    , stride, h);
1004
                }
1005
                dmin= s->dsp.mb_cmp[size](s, src_data[0], s->me.scratchpad, stride, h);
1006
                dmin+= (mv_penalty[mx_i-pred_x] + mv_penalty[my_i-pred_y] + 1)*s->me.mb_penalty_factor;
1007
            }else
1008
                dmin+= s->me.mb_penalty_factor; //field_select bits
1009
                
1010
            dmin += field_select != block; //slightly prefer same field
1011
            
1012
            if(dmin < best_dmin){
1013
                best_dmin= dmin;
1014
                best_field= field_select;
1015
            }
1016
        }
1017
        {
1018
            int16_t (*mv_table)[2]= mv_tables[block][best_field];
1019

    
1020
            if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
1021
            if(mv_table[xy][1]&1) same=0;
1022
            if(mv_table[xy][1]*2 != my) same=0; 
1023
            if(best_field != block) same=0;
1024
        }
1025

    
1026
        field_select_tables[block][xy]= best_field;
1027
        dmin_sum += best_dmin;
1028
    }
1029
    
1030
    s->me.ymin<<=1;
1031
    s->me.ymax<<=1;
1032

    
1033
    if(same)
1034
        return INT_MAX;
1035
    
1036
    switch(s->avctx->mb_cmp&0xFF){
1037
    /*case FF_CMP_SSE:
1038
        return dmin_sum+ 32*s->qscale*s->qscale;*/
1039
    case FF_CMP_RD:
1040
        return dmin_sum;
1041
    default:
1042
        return dmin_sum+ 11*s->me.mb_penalty_factor;
1043
    }
1044
}
1045

    
1046
void ff_estimate_p_frame_motion(MpegEncContext * s,
1047
                                int mb_x, int mb_y)
1048
{
1049
    uint8_t *pix, *ppix;
1050
    int sum, varc, vard, mx, my, dmin, xx, yy;
1051
    int pred_x=0, pred_y=0;
1052
    int P[10][2];
1053
    const int shift= 1+s->quarter_sample;
1054
    int mb_type=0;
1055
    uint8_t *ref_picture= s->last_picture.data[0];
1056
    Picture * const pic= &s->current_picture;
1057
    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
1058
    const int stride= s->linesize;
1059
    const int uvstride= s->uvlinesize;
1060
    uint8_t *src_data[3]= {
1061
        s->new_picture.data[0] + 16*(mb_x + stride*mb_y),
1062
        s->new_picture.data[1] + 8*(mb_x + uvstride*mb_y),
1063
        s->new_picture.data[2] + 8*(mb_x + uvstride*mb_y)
1064
    };
1065
    uint8_t *ref_data[3]= {
1066
        s->last_picture.data[0] + 16*(mb_x + stride*mb_y),
1067
        s->last_picture.data[1] + 8*(mb_x + uvstride*mb_y),
1068
        s->last_picture.data[2] + 8*(mb_x + uvstride*mb_y)
1069
    };
1070

    
1071
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1072

    
1073
    s->me.penalty_factor    = get_penalty_factor(s, s->avctx->me_cmp);
1074
    s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
1075
    s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
1076

    
1077
    get_limits(s, 16*mb_x, 16*mb_y);
1078
    s->me.skip=0;
1079

    
1080
    switch(s->me_method) {
1081
    case ME_ZERO:
1082
    default:
1083
        no_motion_search(s, &mx, &my);
1084
        mx-= mb_x*16;
1085
        my-= mb_y*16;
1086
        dmin = 0;
1087
        break;
1088
#if 0
1089
    case ME_FULL:
1090
        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
1091
        mx-= mb_x*16;
1092
        my-= mb_y*16;
1093
        break;
1094
    case ME_LOG:
1095
        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
1096
        mx-= mb_x*16;
1097
        my-= mb_y*16;
1098
        break;
1099
    case ME_PHODS:
1100
        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
1101
        mx-= mb_x*16;
1102
        my-= mb_y*16;
1103
        break;
1104
#endif
1105
    case ME_X1:
1106
    case ME_EPZS:
1107
       {
1108
            const int mot_stride = s->block_wrap[0];
1109
            const int mot_xy = s->block_index[0];
1110

    
1111
            P_LEFT[0]       = s->current_picture.motion_val[0][mot_xy - 1][0];
1112
            P_LEFT[1]       = s->current_picture.motion_val[0][mot_xy - 1][1];
1113

    
1114
            if(P_LEFT[0]       > (s->me.xmax<<shift)) P_LEFT[0]       = (s->me.xmax<<shift);
1115

    
1116
            if(mb_y) {
1117
                P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];
1118
                P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];
1119
                P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
1120
                P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
1121
                if(P_TOP[1]      > (s->me.ymax<<shift)) P_TOP[1]     = (s->me.ymax<<shift);
1122
                if(P_TOPRIGHT[0] < (s->me.xmin<<shift)) P_TOPRIGHT[0]= (s->me.xmin<<shift);
1123
                if(P_TOPRIGHT[1] > (s->me.ymax<<shift)) P_TOPRIGHT[1]= (s->me.ymax<<shift);
1124
        
1125
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1126
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1127

    
1128
                if(s->out_format == FMT_H263){
1129
                    pred_x = P_MEDIAN[0];
1130
                    pred_y = P_MEDIAN[1];
1131
                }else { /* mpeg1 at least */
1132
                    pred_x= P_LEFT[0];
1133
                    pred_y= P_LEFT[1];
1134
                }
1135
            }else{
1136
                pred_x= P_LEFT[0];
1137
                pred_y= P_LEFT[1];
1138
            }
1139

    
1140
        }
1141
        dmin = s->me.motion_search[0](s, &mx, &my, P, pred_x, pred_y, 
1142
                                      src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty);
1143
 
1144
        break;
1145
    }
1146

    
1147
    /* intra / predictive decision */
1148
    xx = mb_x * 16;
1149
    yy = mb_y * 16;
1150

    
1151
    pix = src_data[0];
1152
    /* At this point (mx,my) are full-pell and the relative displacement */
1153
    ppix = ref_data[0] + (my * s->linesize) + mx;
1154
    
1155
    sum = s->dsp.pix_sum(pix, s->linesize);
1156
    
1157
    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
1158
    vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16)+128)>>8;
1159

    
1160
//printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
1161
    pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;
1162
    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
1163
    pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1164
//    pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 
1165
    pic->mb_var_sum    += varc;
1166
    pic->mc_mb_var_sum += vard;
1167
//printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
1168
    
1169
#if 0
1170
    printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
1171
           varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
1172
#endif
1173
    if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1174
        if (vard <= 64 || vard < varc)
1175
            s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1176
        else
1177
            s->scene_change_score+= s->qscale;
1178

    
1179
        if (vard*2 + 200 > varc)
1180
            mb_type|= CANDIDATE_MB_TYPE_INTRA;
1181
        if (varc*2 + 200 > vard){
1182
            mb_type|= CANDIDATE_MB_TYPE_INTER;
1183
            s->me.sub_motion_search(s, &mx, &my, dmin,
1184
                                   pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty);
1185
            if(s->flags&CODEC_FLAG_MV0)
1186
                if(mx || my)
1187
                    mb_type |= CANDIDATE_MB_TYPE_SKIPED; //FIXME check difference
1188
        }else{
1189
            mx <<=shift;
1190
            my <<=shift;
1191
        }
1192
        if((s->flags&CODEC_FLAG_4MV)
1193
           && !s->me.skip && varc>50 && vard>10){
1194
            if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
1195
                mb_type|=CANDIDATE_MB_TYPE_INTER4V;
1196

    
1197
            set_p_mv_tables(s, mx, my, 0);
1198
        }else
1199
            set_p_mv_tables(s, mx, my, 1);
1200
        if((s->flags&CODEC_FLAG_INTERLACED_ME)
1201
           && !s->me.skip){ //FIXME varc/d checks
1202
            if(interlaced_search(s, src_data, ref_data, s->p_field_mv_table, s->p_field_select_table, s->f_code, mx, my) < INT_MAX)
1203
                mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1204
        }
1205
    }else{
1206
        int intra_score, i;
1207
        mb_type= CANDIDATE_MB_TYPE_INTER;
1208

    
1209
        dmin= s->me.sub_motion_search(s, &mx, &my, dmin,
1210
                                    pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty);
1211
        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
1212
            dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, src_data, ref_data, stride, uvstride, mv_penalty);
1213

    
1214
        if((s->flags&CODEC_FLAG_4MV)
1215
           && !s->me.skip && varc>50 && vard>10){
1216
            int dmin4= h263_mv4_search(s, mx, my, shift);
1217
            if(dmin4 < dmin){
1218
                mb_type= CANDIDATE_MB_TYPE_INTER4V;
1219
                dmin=dmin4;
1220
            }
1221
        }
1222
        if((s->flags&CODEC_FLAG_INTERLACED_ME)
1223
           && !s->me.skip){ //FIXME varc/d checks
1224
            int dmin_i= interlaced_search(s, src_data, ref_data, s->p_field_mv_table, s->p_field_select_table, s->f_code, mx, my);
1225
            if(dmin_i < dmin){
1226
                mb_type = CANDIDATE_MB_TYPE_INTER_I;
1227
                dmin= dmin_i;
1228
            }
1229
        }
1230
                
1231
//        pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 
1232
        set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1233

    
1234
        /* get intra luma score */
1235
        if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1236
            intra_score= (varc<<8) - 500; //FIXME dont scale it down so we dont have to fix it
1237
        }else{
1238
            int mean= (sum+128)>>8;
1239
            mean*= 0x01010101;
1240
            
1241
            for(i=0; i<16; i++){
1242
                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
1243
                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
1244
                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
1245
                *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
1246
            }
1247

    
1248
            intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, pix, s->linesize, 16);
1249
        }
1250
#if 0 //FIXME
1251
        /* get chroma score */
1252
        if(s->avctx->mb_cmp&FF_CMP_CHROMA){
1253
            for(i=1; i<3; i++){
1254
                uint8_t *dest_c;
1255
                int mean;
1256
                
1257
                if(s->out_format == FMT_H263){
1258
                    mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
1259
                }else{
1260
                    mean= (s->last_dc[i] + 4)>>3;
1261
                }
1262
                dest_c = s->new_picture.data[i] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
1263
                
1264
                mean*= 0x01010101;
1265
                for(i=0; i<8; i++){
1266
                    *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
1267
                    *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
1268
                }
1269
                
1270
                intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
1271
            }                
1272
        }
1273
#endif
1274
        intra_score += s->me.mb_penalty_factor*16;
1275
        
1276
        if(intra_score < dmin){
1277
            mb_type= CANDIDATE_MB_TYPE_INTRA;
1278
            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
1279
        }else
1280
            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;
1281
        
1282
        if (vard <= 64 || vard < varc) { //FIXME
1283
            s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1284
        }else{
1285
            s->scene_change_score+= s->qscale;
1286
        }
1287
    }
1288

    
1289
    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
1290
}
1291

    
1292
int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
1293
                                    int mb_x, int mb_y)
1294
{
1295
    int mx, my, dmin;
1296
    int pred_x=0, pred_y=0;
1297
    int P[10][2];
1298
    const int shift= 1+s->quarter_sample;
1299
    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
1300
    const int xy= mb_x + mb_y*s->mb_stride;
1301
    const int stride= s->linesize;
1302
    const int uvstride= s->uvlinesize;
1303
    uint8_t *src_data[3]= {
1304
        s->new_picture.data[0] + 16*(mb_x + stride*mb_y),
1305
        s->new_picture.data[1] + 8*(mb_x + uvstride*mb_y),
1306
        s->new_picture.data[2] + 8*(mb_x + uvstride*mb_y)
1307
    };
1308
    uint8_t *ref_data[3]= {
1309
        s->last_picture.data[0] + 16*(mb_x + stride*mb_y),
1310
        s->last_picture.data[1] + 8*(mb_x + uvstride*mb_y),
1311
        s->last_picture.data[2] + 8*(mb_x + uvstride*mb_y)
1312
    };
1313
    
1314
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1315

    
1316
    s->me.pre_penalty_factor    = get_penalty_factor(s, s->avctx->me_pre_cmp);
1317

    
1318
    get_limits(s, 16*mb_x, 16*mb_y);
1319
    s->me.skip=0;
1320

    
1321
    P_LEFT[0]       = s->p_mv_table[xy + 1][0];
1322
    P_LEFT[1]       = s->p_mv_table[xy + 1][1];
1323

    
1324
    if(P_LEFT[0]       < (s->me.xmin<<shift)) P_LEFT[0]       = (s->me.xmin<<shift);
1325

    
1326
    /* special case for first line */
1327
    if (mb_y == s->mb_height-1) {
1328
        pred_x= P_LEFT[0];
1329
        pred_y= P_LEFT[1];
1330
        P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1331
        P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME 
1332
    } else {
1333
        P_TOP[0]      = s->p_mv_table[xy + s->mb_stride    ][0];
1334
        P_TOP[1]      = s->p_mv_table[xy + s->mb_stride    ][1];
1335
        P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
1336
        P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
1337
        if(P_TOP[1]      < (s->me.ymin<<shift)) P_TOP[1]     = (s->me.ymin<<shift);
1338
        if(P_TOPRIGHT[0] > (s->me.xmax<<shift)) P_TOPRIGHT[0]= (s->me.xmax<<shift);
1339
        if(P_TOPRIGHT[1] < (s->me.ymin<<shift)) P_TOPRIGHT[1]= (s->me.ymin<<shift);
1340
    
1341
        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1342
        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1343

    
1344
        pred_x = P_MEDIAN[0];
1345
        pred_y = P_MEDIAN[1];
1346
    }
1347
    dmin = s->me.pre_motion_search(s, &mx, &my, P, pred_x, pred_y, 
1348
                                   src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty);
1349

    
1350
    s->p_mv_table[xy][0] = mx<<shift;
1351
    s->p_mv_table[xy][1] = my<<shift;
1352
    
1353
    return dmin;
1354
}
1355

    
1356
static int ff_estimate_motion_b(MpegEncContext * s,
1357
                       int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *src_data[3],
1358
                       uint8_t *ref_data[3], int stride, int uvstride, int f_code)
1359
{
1360
    int mx, my, dmin;
1361
    int pred_x=0, pred_y=0;
1362
    int P[10][2];
1363
    const int shift= 1+s->quarter_sample;
1364
    const int mot_stride = s->mb_stride;
1365
    const int mot_xy = mb_y*mot_stride + mb_x;
1366
    uint8_t * const ref_picture= ref_data[0] - 16*s->mb_x - 16*s->mb_y*s->linesize; //FIXME ugly
1367
    uint8_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV;
1368
    int mv_scale;
1369
        
1370
    s->me.penalty_factor    = get_penalty_factor(s, s->avctx->me_cmp);
1371
    s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
1372
    s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
1373

    
1374
    get_limits(s, 16*mb_x, 16*mb_y);
1375

    
1376
    switch(s->me_method) {
1377
    case ME_ZERO:
1378
    default:
1379
        no_motion_search(s, &mx, &my);
1380
        dmin = 0;
1381
        mx-= mb_x*16;
1382
        my-= mb_y*16;
1383
        break;
1384
#if 0
1385
    case ME_FULL:
1386
        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
1387
        mx-= mb_x*16;
1388
        my-= mb_y*16;
1389
        break;
1390
    case ME_LOG:
1391
        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
1392
        mx-= mb_x*16;
1393
        my-= mb_y*16;
1394
        break;
1395
    case ME_PHODS:
1396
        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
1397
        mx-= mb_x*16;
1398
        my-= mb_y*16;
1399
        break;
1400
#endif
1401
    case ME_X1:
1402
    case ME_EPZS:
1403
       {
1404
            P_LEFT[0]        = mv_table[mot_xy - 1][0];
1405
            P_LEFT[1]        = mv_table[mot_xy - 1][1];
1406

    
1407
            if(P_LEFT[0]       > (s->me.xmax<<shift)) P_LEFT[0]       = (s->me.xmax<<shift);
1408

    
1409
            /* special case for first line */
1410
            if (mb_y) {
1411
                P_TOP[0] = mv_table[mot_xy - mot_stride             ][0];
1412
                P_TOP[1] = mv_table[mot_xy - mot_stride             ][1];
1413
                P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1         ][0];
1414
                P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1         ][1];
1415
                if(P_TOP[1] > (s->me.ymax<<shift)) P_TOP[1]= (s->me.ymax<<shift);
1416
                if(P_TOPRIGHT[0] < (s->me.xmin<<shift)) P_TOPRIGHT[0]= (s->me.xmin<<shift);
1417
                if(P_TOPRIGHT[1] > (s->me.ymax<<shift)) P_TOPRIGHT[1]= (s->me.ymax<<shift);
1418
        
1419
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1420
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1421
            }
1422
            pred_x= P_LEFT[0];
1423
            pred_y= P_LEFT[1];
1424
        }
1425
        
1426
        if(mv_table == s->b_forw_mv_table){
1427
            mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
1428
        }else{
1429
            mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
1430
        }
1431
        
1432
        dmin = s->me.motion_search[0](s, &mx, &my, P, pred_x, pred_y, 
1433
                                      src_data, ref_data, stride, uvstride, s->p_mv_table, mv_scale, mv_penalty);
1434
 
1435
        break;
1436
    }
1437
    
1438
    dmin= s->me.sub_motion_search(s, &mx, &my, dmin,
1439
                                   pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty);
1440
                                   
1441
    if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
1442
        dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, src_data, ref_data, stride, uvstride, mv_penalty);
1443

    
1444
//printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
1445
//    s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1446
    mv_table[mot_xy][0]= mx;
1447
    mv_table[mot_xy][1]= my;
1448

    
1449
    return dmin;
1450
}
1451

    
1452
static inline int check_bidir_mv(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6],
1453
                   int stride, int uvstride,
1454
                   int motion_fx, int motion_fy,
1455
                   int motion_bx, int motion_by,
1456
                   int pred_fx, int pred_fy,
1457
                   int pred_bx, int pred_by,
1458
                   int size, int h)
1459
{
1460
    //FIXME optimize?
1461
    //FIXME move into template?
1462
    //FIXME better f_code prediction (max mv & distance)
1463
    //FIXME pointers
1464
    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
1465
    uint8_t *dest_y = s->me.scratchpad;
1466
    uint8_t *ptr;
1467
    int dxy;
1468
    int src_x, src_y;
1469
    int fbmin;
1470

    
1471
    if(s->quarter_sample){
1472
        dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1473
        src_x = motion_fx >> 2;
1474
        src_y = motion_fy >> 2;
1475

    
1476
        ptr = ref_data[0] + (src_y * stride) + src_x;
1477
        s->dsp.put_qpel_pixels_tab[0][dxy](dest_y    , ptr    , stride);
1478

    
1479
        dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1480
        src_x = motion_bx >> 2;
1481
        src_y = motion_by >> 2;
1482
    
1483
        ptr = ref_data[3] + (src_y * stride) + src_x;
1484
        s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y    , ptr    , stride);
1485
    }else{
1486
        dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1487
        src_x = motion_fx >> 1;
1488
        src_y = motion_fy >> 1;
1489

    
1490
        ptr = ref_data[0] + (src_y * stride) + src_x;
1491
        s->dsp.put_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
1492

    
1493
        dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1494
        src_x = motion_bx >> 1;
1495
        src_y = motion_by >> 1;
1496
    
1497
        ptr = ref_data[3] + (src_y * stride) + src_x;
1498
        s->dsp.avg_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
1499
    }
1500

    
1501
    fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->me.mb_penalty_factor
1502
           +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->me.mb_penalty_factor
1503
           + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
1504
           
1505
    if(s->avctx->mb_cmp&FF_CMP_CHROMA){
1506
    }
1507
    //FIXME CHROMA !!!
1508
           
1509
    return fbmin;
1510
}
1511

    
1512
/* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1513
static inline int bidir_refine(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6],
1514
                                  int stride, int uvstride,
1515
                                  int mb_x, int mb_y)
1516
{
1517
    const int mot_stride = s->mb_stride;
1518
    const int xy = mb_y *mot_stride + mb_x;
1519
    int fbmin;
1520
    int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1521
    int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1522
    int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1523
    int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1524
    int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1525
    int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1526
    int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1527
    int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1528

    
1529
    //FIXME do refinement and add flag
1530
    
1531
    fbmin= check_bidir_mv(s, src_data, ref_data, stride, uvstride,
1532
                          motion_fx, motion_fy,
1533
                          motion_bx, motion_by,
1534
                          pred_fx, pred_fy,
1535
                          pred_bx, pred_by,
1536
                          0, 16);
1537

    
1538
   return fbmin;
1539
}
1540

    
1541
static inline int direct_search(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6],
1542
                                int stride, int uvstride,
1543
                                int mb_x, int mb_y)
1544
{
1545
    int P[10][2];
1546
    const int mot_stride = s->mb_stride;
1547
    const int mot_xy = mb_y*mot_stride + mb_x;
1548
    const int shift= 1+s->quarter_sample;
1549
    int dmin, i;
1550
    const int time_pp= s->pp_time;
1551
    const int time_pb= s->pb_time;
1552
    int mx, my, xmin, xmax, ymin, ymax;
1553
    int16_t (*mv_table)[2]= s->b_direct_mv_table;
1554
    uint8_t * const mv_penalty= s->me.mv_penalty[1] + MAX_MV;
1555
    
1556
    ymin= xmin=(-32)>>shift;
1557
    ymax= xmax=   31>>shift;
1558

    
1559
    if(IS_8X8(s->next_picture.mb_type[mot_xy])){
1560
        s->mv_type= MV_TYPE_8X8;
1561
    }else{
1562
        s->mv_type= MV_TYPE_16X16;
1563
    }
1564

    
1565
    for(i=0; i<4; i++){
1566
        int index= s->block_index[i];
1567
        int min, max;
1568
    
1569
        s->me.co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
1570
        s->me.co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
1571
        s->me.direct_basis_mv[i][0]= s->me.co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1572
        s->me.direct_basis_mv[i][1]= s->me.co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1573
//        s->me.direct_basis_mv[1][i][0]= s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
1574
//        s->me.direct_basis_mv[1][i][1]= s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
1575

    
1576
        max= FFMAX(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift;
1577
        min= FFMIN(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift;
1578
        max+= (2*mb_x + (i& 1))*8 + 1; // +-1 is for the simpler rounding
1579
        min+= (2*mb_x + (i& 1))*8 - 1;
1580
        xmax= FFMIN(xmax, s->width - max);
1581
        xmin= FFMAX(xmin, - 16     - min);
1582

    
1583
        max= FFMAX(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift;
1584
        min= FFMIN(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift;
1585
        max+= (2*mb_y + (i>>1))*8 + 1; // +-1 is for the simpler rounding
1586
        min+= (2*mb_y + (i>>1))*8 - 1;
1587
        ymax= FFMIN(ymax, s->height - max);
1588
        ymin= FFMAX(ymin, - 16      - min);
1589
        
1590
        if(s->mv_type == MV_TYPE_16X16) break;
1591
    }
1592
    
1593
    assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1594
    
1595
    if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1596
        s->b_direct_mv_table[mot_xy][0]= 0;
1597
        s->b_direct_mv_table[mot_xy][1]= 0;
1598

    
1599
        return 256*256*256*64;
1600
    }
1601
    
1602
    s->me.xmin= xmin;
1603
    s->me.ymin= ymin;
1604
    s->me.xmax= xmax;
1605
    s->me.ymax= ymax;
1606

    
1607
    P_LEFT[0]        = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
1608
    P_LEFT[1]        = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
1609

    
1610
    /* special case for first line */
1611
    if (mb_y) {
1612
        P_TOP[0]      = clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);
1613
        P_TOP[1]      = clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);
1614
        P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);
1615
        P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);
1616
    
1617
        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1618
        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1619
    }
1620
 
1621
    //FIXME direct_search  ptr in context!!! (needed for chroma anyway or this will get messy)   
1622
    if(s->flags&CODEC_FLAG_QPEL){
1623
        dmin = simple_direct_qpel_epzs_motion_search(s, &mx, &my, P, 0, 0, 
1624
                                                     src_data, ref_data, stride, uvstride, mv_table, 1<<14, mv_penalty);
1625
        dmin = simple_direct_qpel_qpel_motion_search(s, &mx, &my, dmin,
1626
                                                0, 0, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty);
1627
        
1628
        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
1629
            dmin= simple_direct_qpel_qpel_get_mb_score(s, mx, my, 0, 0, src_data, ref_data, stride, uvstride, mv_penalty);
1630
    }else{
1631
        dmin = simple_direct_hpel_epzs_motion_search(s, &mx, &my, P, 0, 0, 
1632
                                                     src_data, ref_data, stride, uvstride, mv_table, 1<<15, mv_penalty);
1633
        dmin = simple_direct_hpel_hpel_motion_search(s, &mx, &my, dmin,
1634
                                                0, 0, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty);
1635

    
1636
        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
1637
            dmin= simple_direct_hpel_hpel_get_mb_score(s, mx, my, 0, 0, src_data, ref_data, stride, uvstride, mv_penalty);
1638
    }
1639
    
1640
    get_limits(s, 16*mb_x, 16*mb_y); //restore s->me.?min/max, maybe not needed
1641

    
1642
    s->b_direct_mv_table[mot_xy][0]= mx;
1643
    s->b_direct_mv_table[mot_xy][1]= my;
1644
    return dmin;
1645
}
1646

    
1647
void ff_estimate_b_frame_motion(MpegEncContext * s,
1648
                             int mb_x, int mb_y)
1649
{
1650
    const int penalty_factor= s->me.mb_penalty_factor;
1651
    int fmin, bmin, dmin, fbmin, bimin, fimin;
1652
    int type=0;
1653
    const int stride= s->linesize;
1654
    const int uvstride= s->uvlinesize;
1655
    uint8_t *src_data[3]= {
1656
        s->new_picture.data[0] + 16*(s->mb_x + stride*s->mb_y),
1657
        s->new_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y),
1658
        s->new_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y)
1659
    };
1660
    uint8_t *ref_data[6]= {
1661
        s->last_picture.data[0] + 16*(s->mb_x + stride*s->mb_y),
1662
        s->last_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y),
1663
        s->last_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y),
1664
        s->next_picture.data[0] + 16*(s->mb_x + stride*s->mb_y),
1665
        s->next_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y),
1666
        s->next_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y)
1667
    };
1668
    
1669
    s->me.skip=0;
1670
    if (s->codec_id == CODEC_ID_MPEG4)
1671
        dmin= direct_search(s, src_data, ref_data, stride, uvstride, mb_x, mb_y);
1672
    else
1673
        dmin= INT_MAX;
1674
//FIXME penalty stuff for non mpeg4
1675
    s->me.skip=0;
1676
    fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, src_data, 
1677
                               ref_data, stride, uvstride, s->f_code) + 3*penalty_factor;
1678
    
1679
    s->me.skip=0;
1680
    bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, src_data, 
1681
                               ref_data+3, stride, uvstride, s->b_code) + 2*penalty_factor;
1682
//printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1683

    
1684
    s->me.skip=0;
1685
    fbmin= bidir_refine(s, src_data, ref_data, stride, uvstride, mb_x, mb_y) + penalty_factor;
1686
//printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1687
    
1688
    if(s->flags & CODEC_FLAG_INTERLACED_ME){
1689
        const int xy = mb_y*s->mb_stride + mb_x;
1690

    
1691
//FIXME mb type penalty
1692
        s->me.skip=0;
1693
        fimin= interlaced_search(s, src_data, ref_data  , 
1694
                                 s->b_field_mv_table[0], s->b_field_select_table[0], s->f_code,
1695
                                 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1696
        bimin= interlaced_search(s, src_data, ref_data+3, 
1697
                                 s->b_field_mv_table[1], s->b_field_select_table[1], s->b_code,
1698
                                 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1]);
1699
    }else
1700
        fimin= bimin= INT_MAX;
1701

    
1702
    {
1703
        int score= fmin;
1704
        type = CANDIDATE_MB_TYPE_FORWARD;
1705
        
1706
        if (dmin <= score){
1707
            score = dmin;
1708
            type = CANDIDATE_MB_TYPE_DIRECT;
1709
        }
1710
        if(bmin<score){
1711
            score=bmin;
1712
            type= CANDIDATE_MB_TYPE_BACKWARD; 
1713
        }
1714
        if(fbmin<score){
1715
            score=fbmin;
1716
            type= CANDIDATE_MB_TYPE_BIDIR;
1717
        }
1718
        if(fimin<score){
1719
            score=fimin;
1720
            type= CANDIDATE_MB_TYPE_FORWARD_I;
1721
        }
1722
        if(bimin<score){
1723
            score=bimin;
1724
            type= CANDIDATE_MB_TYPE_BACKWARD_I;
1725
        }
1726
        
1727
        score= ((unsigned)(score*score + 128*256))>>16;
1728
        s->current_picture.mc_mb_var_sum += score;
1729
        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1730
    }
1731

    
1732
    if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1733
        type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
1734
        if(fimin < INT_MAX)
1735
            type |= CANDIDATE_MB_TYPE_FORWARD_I;
1736
        if(bimin < INT_MAX)
1737
            type |= CANDIDATE_MB_TYPE_BACKWARD_I;
1738
        if(fimin < INT_MAX && bimin < INT_MAX){
1739
            type |= CANDIDATE_MB_TYPE_BIDIR_I;
1740
        }
1741
         //FIXME something smarter
1742
        if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
1743
#if 0        
1744
        if(s->out_format == FMT_MPEG1)
1745
            type |= CANDIDATE_MB_TYPE_INTRA;
1746
#endif
1747
    }
1748

    
1749
    s->mb_type[mb_y*s->mb_stride + mb_x]= type;
1750
}
1751

    
1752
/* find best f_code for ME which do unlimited searches */
1753
int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
1754
{
1755
    if(s->me_method>=ME_EPZS){
1756
        int score[8];
1757
        int i, y;
1758
        uint8_t * fcode_tab= s->fcode_tab;
1759
        int best_fcode=-1;
1760
        int best_score=-10000000;
1761

    
1762
        for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
1763

    
1764
        for(y=0; y<s->mb_height; y++){
1765
            int x;
1766
            int xy= y*s->mb_stride;
1767
            for(x=0; x<s->mb_width; x++){
1768
                if(s->mb_type[xy] & type){
1769
                    int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV],
1770
                                     fcode_tab[mv_table[xy][1] + MAX_MV]);
1771
                    int j;
1772
                    
1773
                    for(j=0; j<fcode && j<8; j++){
1774
                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
1775
                            score[j]-= 170;
1776
                    }
1777
                }
1778
                xy++;
1779
            }
1780
        }
1781
        
1782
        for(i=1; i<8; i++){
1783
            if(score[i] > best_score){
1784
                best_score= score[i];
1785
                best_fcode= i;
1786
            }
1787
//            printf("%d %d\n", i, score[i]);
1788
        }
1789

    
1790
//    printf("fcode: %d type: %d\n", i, s->pict_type);
1791
        return best_fcode;
1792
/*        for(i=0; i<=MAX_FCODE; i++){
1793
            printf("%d ", mv_num[i]);
1794
        }
1795
        printf("\n");*/
1796
    }else{
1797
        return 1;
1798
    }
1799
}
1800

    
1801
void ff_fix_long_p_mvs(MpegEncContext * s)
1802
{
1803
    const int f_code= s->f_code;
1804
    int y, range;
1805
    assert(s->pict_type==P_TYPE);
1806

    
1807
    range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
1808
    
1809
    if(s->msmpeg4_version) range= 16;
1810
    
1811
    if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range;
1812
    
1813
//printf("%d no:%d %d//\n", clip, noclip, f_code);
1814
    if(s->flags&CODEC_FLAG_4MV){
1815
        const int wrap= 2+ s->mb_width*2;
1816

    
1817
        /* clip / convert to intra 8x8 type MVs */
1818
        for(y=0; y<s->mb_height; y++){
1819
            int xy= (y*2 + 1)*wrap + 1;
1820
            int i= y*s->mb_stride;
1821
            int x;
1822

    
1823
            for(x=0; x<s->mb_width; x++){
1824
                if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
1825
                    int block;
1826
                    for(block=0; block<4; block++){
1827
                        int off= (block& 1) + (block>>1)*wrap;
1828
                        int mx= s->current_picture.motion_val[0][ xy + off ][0];
1829
                        int my= s->current_picture.motion_val[0][ xy + off ][1];
1830

    
1831
                        if(   mx >=range || mx <-range
1832
                           || my >=range || my <-range){
1833
                            s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
1834
                            s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
1835
                            s->current_picture.mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
1836
                        }
1837
                    }
1838
                }
1839
                xy+=2;
1840
                i++;
1841
            }
1842
        }
1843
    }
1844
}
1845

    
1846
/**
1847
 *
1848
 * @param truncate 1 for truncation, 0 for using intra
1849
 */
1850
void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select, 
1851
                     int16_t (*mv_table)[2], int f_code, int type, int truncate)
1852
{
1853
    int y, h_range, v_range;
1854

    
1855
    // RAL: 8 in MPEG-1, 16 in MPEG-4
1856
    int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
1857

    
1858
    if(s->msmpeg4_version) range= 16;
1859
    if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range;
1860

    
1861
    h_range= range;
1862
    v_range= field_select_table ? range>>1 : range;
1863

    
1864
    /* clip / convert to intra 16x16 type MVs */
1865
    for(y=0; y<s->mb_height; y++){
1866
        int x;
1867
        int xy= y*s->mb_stride;
1868
        for(x=0; x<s->mb_width; x++){
1869
            if (s->mb_type[xy] & type){    // RAL: "type" test added...
1870
                if(field_select_table==NULL || field_select_table[xy] == field_select){
1871
                    if(   mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
1872
                       || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
1873

    
1874
                        if(truncate){
1875
                            if     (mv_table[xy][0] > h_range-1) mv_table[xy][0]=  h_range-1;
1876
                            else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
1877
                            if     (mv_table[xy][1] > v_range-1) mv_table[xy][1]=  v_range-1;
1878
                            else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
1879
                        }else{
1880
                            s->mb_type[xy] &= ~type;
1881
                            s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
1882
                            mv_table[xy][0]=
1883
                            mv_table[xy][1]= 0;
1884
                        }
1885
                    }
1886
                }
1887
            }
1888
            xy++;
1889
        }
1890
    }
1891
}