Statistics
| Branch: | Revision:

ffmpeg / libavcodec / motion_est.c @ 668154df

History | View | Annotate | Download (55.2 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
#include <stdlib.h>
24
#include <stdio.h>
25
#include "avcodec.h"
26
#include "dsputil.h"
27
#include "mpegvideo.h"
28

    
29
//#undef NDEBUG
30
//#include <assert.h>
31

    
32
#define SQ(a) ((a)*(a))
33

    
34
#define P_LEFT P[1]
35
#define P_TOP P[2]
36
#define P_TOPRIGHT P[3]
37
#define P_MEDIAN P[4]
38
#define P_MV1 P[9]
39

    
40
static inline int sad_hpel_motion_search(MpegEncContext * s,
41
                                  int *mx_ptr, int *my_ptr, int dmin,
42
                                  int xmin, int ymin, int xmax, int ymax,
43
                                  int pred_x, int pred_y, Picture *picture,
44
                                  int n, int size, uint16_t * const mv_penalty);
45

    
46
static inline int update_map_generation(MpegEncContext * s)
47
{
48
    s->me.map_generation+= 1<<(ME_MAP_MV_BITS*2);
49
    if(s->me.map_generation==0){
50
        s->me.map_generation= 1<<(ME_MAP_MV_BITS*2);
51
        memset(s->me.map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
52
    }
53
    return s->me.map_generation;
54
}
55

    
56
/* shape adaptive search stuff */
57
typedef struct Minima{
58
    int height;
59
    int x, y;
60
    int checked;
61
}Minima;
62

    
63
static int minima_cmp(const void *a, const void *b){
64
    Minima *da = (Minima *) a;
65
    Minima *db = (Minima *) b;
66
    
67
    return da->height - db->height;
68
}
69
                                  
70
/* SIMPLE */
71
#define RENAME(a) simple_ ## a
72

    
73
#define CMP(d, x, y, size)\
74
d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride);
75

    
76
#define CMP_HPEL(d, dx, dy, x, y, size)\
77
{\
78
    const int dxy= (dx) + 2*(dy);\
79
    hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, (16>>size));\
80
    d = cmp_sub(s, s->me.scratchpad, src_y, stride);\
81
}
82

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

    
90
#include "motion_est_template.c"
91
#undef RENAME
92
#undef CMP
93
#undef CMP_HPEL
94
#undef CMP_QPEL
95
#undef INIT
96

    
97
/* SIMPLE CHROMA */
98
#define RENAME(a) simple_chroma_ ## a
99

    
100
#define CMP(d, x, y, size)\
101
d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride);\
102
if(chroma_cmp){\
103
    int dxy= ((x)&1) + 2*((y)&1);\
104
    int c= ((x)>>1) + ((y)>>1)*uvstride;\
105
\
106
    chroma_hpel_put[0][dxy](s->me.scratchpad, ref_u + c, uvstride, 8);\
107
    d += chroma_cmp(s, s->me.scratchpad, src_u, uvstride);\
108
    chroma_hpel_put[0][dxy](s->me.scratchpad, ref_v + c, uvstride, 8);\
109
    d += chroma_cmp(s, s->me.scratchpad, src_v, uvstride);\
110
}
111

    
112
#define CMP_HPEL(d, dx, dy, x, y, size)\
113
{\
114
    const int dxy= (dx) + 2*(dy);\
115
    hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, (16>>size));\
116
    d = cmp_sub(s, s->me.scratchpad, src_y, stride);\
117
    if(chroma_cmp_sub){\
118
        int cxy= (dxy) | ((x)&1) | (2*((y)&1));\
119
        int c= ((x)>>1) + ((y)>>1)*uvstride;\
120
        chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, 8);\
121
        d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride);\
122
        chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, 8);\
123
        d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride);\
124
    }\
125
}
126

    
127
#define CMP_QPEL(d, dx, dy, x, y, size)\
128
{\
129
    const int dxy= (dx) + 4*(dy);\
130
    qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\
131
    d = cmp_sub(s, s->me.scratchpad, src_y, stride);\
132
    if(chroma_cmp_sub){\
133
        int cxy, c;\
134
        int cx= (4*(x) + (dx))/2;\
135
        int cy= (4*(y) + (dy))/2;\
136
        cx= (cx>>1)|(cx&1);\
137
        cy= (cy>>1)|(cy&1);\
138
        cxy= (cx&1) + 2*(cy&1);\
139
        c= ((cx)>>1) + ((cy)>>1)*uvstride;\
140
        chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, 8);\
141
        d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride);\
142
        chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, 8);\
143
        d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride);\
144
    }\
145
}
146

    
147
#include "motion_est_template.c"
148
#undef RENAME
149
#undef CMP
150
#undef CMP_HPEL
151
#undef CMP_QPEL
152
#undef INIT
153

    
154
/* SIMPLE DIRECT HPEL */
155
#define RENAME(a) simple_direct_hpel_ ## a
156
//FIXME precalc divisions stuff
157

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

    
200

    
201
#define CMP_HPEL(d, dx, dy, x, y, size)\
202
    CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub)
203

    
204
#define CMP(d, x, y, size)\
205
    CMP_DIRECT(d, 0, 0, x, y, size, cmp)
206
    
207
#include "motion_est_template.c"
208
#undef RENAME
209
#undef CMP
210
#undef CMP_HPEL
211
#undef CMP_QPEL
212
#undef INIT
213
#undef CMP_DIRECT
214

    
215
/* SIMPLE DIRECT QPEL */
216
#define RENAME(a) simple_direct_qpel_ ## a
217

    
218
#define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\
219
if((x) >= xmin && 4*(x) + (dx) <= 4*xmax && (y) >= ymin && 4*(y) + (dy) <= 4*ymax){\
220
    const int qx= 4*(x) + (dx);\
221
    const int qy= 4*(y) + (dy);\
222
    if(s->mv_type==MV_TYPE_8X8){\
223
        int i;\
224
        for(i=0; i<4; i++){\
225
            int fx = s->me.direct_basis_mv[i][0] + qx;\
226
            int fy = s->me.direct_basis_mv[i][1] + qy;\
227
            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;\
228
            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;\
229
            int fxy= (fx&3) + 4*(fy&3);\
230
            int bxy= (bx&3) + 4*(by&3);\
231
\
232
            uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\
233
            qpel_put[1][fxy](dst, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\
234
            qpel_avg[1][bxy](dst, (ref2_y) + (bx>>2) + (by>>2)*(stride), stride);\
235
        }\
236
    }else{\
237
        int fx = s->me.direct_basis_mv[0][0] + qx;\
238
        int fy = s->me.direct_basis_mv[0][1] + qy;\
239
        int bx = qx ? fx - s->me.co_located_mv[0][0] : s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp;\
240
        int by = qy ? fy - s->me.co_located_mv[0][1] : s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp;\
241
        int fxy= (fx&3) + 4*(fy&3);\
242
        int bxy= (bx&3) + 4*(by&3);\
243
\
244
        qpel_put[0][fxy](s->me.scratchpad, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\
245
        qpel_avg[0][bxy](s->me.scratchpad, (ref2_y) + (bx>>2) + (by>>2)*(stride), stride);\
246
    }\
247
    d = cmp_func(s, s->me.scratchpad, src_y, stride);\
248
}else\
249
    d= 256*256*256*32;
250

    
251

    
252
#define CMP_QPEL(d, dx, dy, x, y, size)\
253
    CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub)
254

    
255
#define CMP(d, x, y, size)\
256
    CMP_DIRECT(d, 0, 0, x, y, size, cmp)
257

    
258
#include "motion_est_template.c"
259
#undef RENAME
260
#undef CMP
261
#undef CMP_HPEL
262
#undef CMP_QPEL
263
#undef INIT
264
#undef CMP__DIRECT
265

    
266

    
267
static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride){
268
    return 0;
269
}
270

    
271
static void set_cmp(MpegEncContext *s, me_cmp_func *cmp, int type){
272
    DSPContext* c= &s->dsp;
273
    int i;
274
    
275
    memset(cmp, 0, sizeof(void*)*11);
276

    
277
    switch(type&0xFF){
278
    case FF_CMP_SAD:
279
        cmp[0]= c->sad[0];
280
        cmp[1]= c->sad[1];
281
        break;
282
    case FF_CMP_SATD:
283
        cmp[0]= c->hadamard8_diff[0];
284
        cmp[1]= c->hadamard8_diff[1];
285
        break;
286
    case FF_CMP_SSE:
287
        cmp[0]= c->sse[0];
288
        cmp[1]= c->sse[1];
289
        break;
290
    case FF_CMP_DCT:
291
        cmp[0]= c->dct_sad[0];
292
        cmp[1]= c->dct_sad[1];
293
        break;
294
    case FF_CMP_PSNR:
295
        cmp[0]= c->quant_psnr[0];
296
        cmp[1]= c->quant_psnr[1];
297
        break;
298
    case FF_CMP_BIT:
299
        cmp[0]= c->bit[0];
300
        cmp[1]= c->bit[1];
301
        break;
302
    case FF_CMP_RD:
303
        cmp[0]= c->rd[0];
304
        cmp[1]= c->rd[1];
305
        break;
306
    case FF_CMP_ZERO:
307
        for(i=0; i<7; i++){
308
            cmp[i]= zero_cmp;
309
        }
310
        break;
311
    default:
312
        fprintf(stderr,"internal error in cmp function selection\n");
313
    }
314
}
315

    
316
static inline int get_penalty_factor(MpegEncContext *s, int type){
317
    switch(type&0xFF){
318
    default:
319
    case FF_CMP_SAD:
320
        return s->qscale*2;
321
    case FF_CMP_DCT:
322
        return s->qscale*3;
323
    case FF_CMP_SATD:
324
        return s->qscale*6;
325
    case FF_CMP_SSE:
326
        return s->qscale*s->qscale*2;
327
    case FF_CMP_BIT:
328
        return 1;
329
    case FF_CMP_RD:
330
    case FF_CMP_PSNR:
331
        return (s->qscale*s->qscale*185 + 64)>>7;
332
    }
333
}
334

    
335
void ff_init_me(MpegEncContext *s){
336
    set_cmp(s, s->dsp.me_pre_cmp, s->avctx->me_pre_cmp);
337
    set_cmp(s, s->dsp.me_cmp, s->avctx->me_cmp);
338
    set_cmp(s, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
339
    set_cmp(s, s->dsp.mb_cmp, s->avctx->mb_cmp);
340

    
341
    if(s->flags&CODEC_FLAG_QPEL){
342
        if(s->avctx->me_sub_cmp&FF_CMP_CHROMA)
343
            s->me.sub_motion_search= simple_chroma_qpel_motion_search;
344
        else
345
            s->me.sub_motion_search= simple_qpel_motion_search;
346
    }else{
347
        if(s->avctx->me_sub_cmp&FF_CMP_CHROMA)
348
            s->me.sub_motion_search= simple_chroma_hpel_motion_search;
349
        else if(   s->avctx->me_sub_cmp == FF_CMP_SAD 
350
                && s->avctx->    me_cmp == FF_CMP_SAD 
351
                && s->avctx->    mb_cmp == FF_CMP_SAD)
352
            s->me.sub_motion_search= sad_hpel_motion_search;
353
        else
354
            s->me.sub_motion_search= simple_hpel_motion_search;
355
    }
356

    
357
    if(s->avctx->me_cmp&FF_CMP_CHROMA){
358
        s->me.motion_search[0]= simple_chroma_epzs_motion_search;
359
        s->me.motion_search[1]= simple_chroma_epzs_motion_search4;
360
    }else{
361
        s->me.motion_search[0]= simple_epzs_motion_search;
362
        s->me.motion_search[1]= simple_epzs_motion_search4;
363
    }
364
    
365
    if(s->avctx->me_pre_cmp&FF_CMP_CHROMA){
366
        s->me.pre_motion_search= simple_chroma_epzs_motion_search;
367
    }else{
368
        s->me.pre_motion_search= simple_epzs_motion_search;
369
    }
370
    
371
    if(s->flags&CODEC_FLAG_QPEL){
372
        if(s->avctx->mb_cmp&FF_CMP_CHROMA)
373
            s->me.get_mb_score= simple_chroma_qpel_get_mb_score;
374
        else
375
            s->me.get_mb_score= simple_qpel_get_mb_score;
376
    }else{
377
        if(s->avctx->mb_cmp&FF_CMP_CHROMA)
378
            s->me.get_mb_score= simple_chroma_hpel_get_mb_score;
379
        else
380
            s->me.get_mb_score= simple_hpel_get_mb_score;
381
    }
382
}
383
      
384
static int pix_dev(UINT8 * pix, int line_size, int mean)
385
{
386
    int s, i, j;
387

    
388
    s = 0;
389
    for (i = 0; i < 16; i++) {
390
        for (j = 0; j < 16; j += 8) {
391
            s += ABS(pix[0]-mean);
392
            s += ABS(pix[1]-mean);
393
            s += ABS(pix[2]-mean);
394
            s += ABS(pix[3]-mean);
395
            s += ABS(pix[4]-mean);
396
            s += ABS(pix[5]-mean);
397
            s += ABS(pix[6]-mean);
398
            s += ABS(pix[7]-mean);
399
            pix += 8;
400
        }
401
        pix += line_size - 16;
402
    }
403
    return s;
404
}
405

    
406
static inline void no_motion_search(MpegEncContext * s,
407
                                    int *mx_ptr, int *my_ptr)
408
{
409
    *mx_ptr = 16 * s->mb_x;
410
    *my_ptr = 16 * s->mb_y;
411
}
412

    
413
static int full_motion_search(MpegEncContext * s,
414
                              int *mx_ptr, int *my_ptr, int range,
415
                              int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
416
{
417
    int x1, y1, x2, y2, xx, yy, x, y;
418
    int mx, my, dmin, d;
419
    UINT8 *pix;
420

    
421
    xx = 16 * s->mb_x;
422
    yy = 16 * s->mb_y;
423
    x1 = xx - range + 1;        /* we loose one pixel to avoid boundary pb with half pixel pred */
424
    if (x1 < xmin)
425
        x1 = xmin;
426
    x2 = xx + range - 1;
427
    if (x2 > xmax)
428
        x2 = xmax;
429
    y1 = yy - range + 1;
430
    if (y1 < ymin)
431
        y1 = ymin;
432
    y2 = yy + range - 1;
433
    if (y2 > ymax)
434
        y2 = ymax;
435
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
436
    dmin = 0x7fffffff;
437
    mx = 0;
438
    my = 0;
439
    for (y = y1; y <= y2; y++) {
440
        for (x = x1; x <= x2; x++) {
441
            d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x,
442
                             s->linesize);
443
            if (d < dmin ||
444
                (d == dmin &&
445
                 (abs(x - xx) + abs(y - yy)) <
446
                 (abs(mx - xx) + abs(my - yy)))) {
447
                dmin = d;
448
                mx = x;
449
                my = y;
450
            }
451
        }
452
    }
453

    
454
    *mx_ptr = mx;
455
    *my_ptr = my;
456

    
457
#if 0
458
    if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
459
        *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
460
        fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
461
    }
462
#endif
463
    return dmin;
464
}
465

    
466

    
467
static int log_motion_search(MpegEncContext * s,
468
                             int *mx_ptr, int *my_ptr, int range,
469
                             int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
470
{
471
    int x1, y1, x2, y2, xx, yy, x, y;
472
    int mx, my, dmin, d;
473
    UINT8 *pix;
474

    
475
    xx = s->mb_x << 4;
476
    yy = s->mb_y << 4;
477

    
478
    /* Left limit */
479
    x1 = xx - range;
480
    if (x1 < xmin)
481
        x1 = xmin;
482

    
483
    /* Right limit */
484
    x2 = xx + range;
485
    if (x2 > xmax)
486
        x2 = xmax;
487

    
488
    /* Upper limit */
489
    y1 = yy - range;
490
    if (y1 < ymin)
491
        y1 = ymin;
492

    
493
    /* Lower limit */
494
    y2 = yy + range;
495
    if (y2 > ymax)
496
        y2 = ymax;
497

    
498
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
499
    dmin = 0x7fffffff;
500
    mx = 0;
501
    my = 0;
502

    
503
    do {
504
        for (y = y1; y <= y2; y += range) {
505
            for (x = x1; x <= x2; x += range) {
506
                d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
507
                if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
508
                    dmin = d;
509
                    mx = x;
510
                    my = y;
511
                }
512
            }
513
        }
514

    
515
        range = range >> 1;
516

    
517
        x1 = mx - range;
518
        if (x1 < xmin)
519
            x1 = xmin;
520

    
521
        x2 = mx + range;
522
        if (x2 > xmax)
523
            x2 = xmax;
524

    
525
        y1 = my - range;
526
        if (y1 < ymin)
527
            y1 = ymin;
528

    
529
        y2 = my + range;
530
        if (y2 > ymax)
531
            y2 = ymax;
532

    
533
    } while (range >= 1);
534

    
535
#ifdef DEBUG
536
    fprintf(stderr, "log       - MX: %d\tMY: %d\n", mx, my);
537
#endif
538
    *mx_ptr = mx;
539
    *my_ptr = my;
540
    return dmin;
541
}
542

    
543
static int phods_motion_search(MpegEncContext * s,
544
                               int *mx_ptr, int *my_ptr, int range,
545
                               int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
546
{
547
    int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
548
    int mx, my, dminx, dminy;
549
    UINT8 *pix;
550

    
551
    xx = s->mb_x << 4;
552
    yy = s->mb_y << 4;
553

    
554
    /* Left limit */
555
    x1 = xx - range;
556
    if (x1 < xmin)
557
        x1 = xmin;
558

    
559
    /* Right limit */
560
    x2 = xx + range;
561
    if (x2 > xmax)
562
        x2 = xmax;
563

    
564
    /* Upper limit */
565
    y1 = yy - range;
566
    if (y1 < ymin)
567
        y1 = ymin;
568

    
569
    /* Lower limit */
570
    y2 = yy + range;
571
    if (y2 > ymax)
572
        y2 = ymax;
573

    
574
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
575
    mx = 0;
576
    my = 0;
577

    
578
    x = xx;
579
    y = yy;
580
    do {
581
        dminx = 0x7fffffff;
582
        dminy = 0x7fffffff;
583

    
584
        lastx = x;
585
        for (x = x1; x <= x2; x += range) {
586
            d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
587
            if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
588
                dminx = d;
589
                mx = x;
590
            }
591
        }
592

    
593
        x = lastx;
594
        for (y = y1; y <= y2; y += range) {
595
            d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
596
            if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
597
                dminy = d;
598
                my = y;
599
            }
600
        }
601

    
602
        range = range >> 1;
603

    
604
        x = mx;
605
        y = my;
606
        x1 = mx - range;
607
        if (x1 < xmin)
608
            x1 = xmin;
609

    
610
        x2 = mx + range;
611
        if (x2 > xmax)
612
            x2 = xmax;
613

    
614
        y1 = my - range;
615
        if (y1 < ymin)
616
            y1 = ymin;
617

    
618
        y2 = my + range;
619
        if (y2 > ymax)
620
            y2 = ymax;
621

    
622
    } while (range >= 1);
623

    
624
#ifdef DEBUG
625
    fprintf(stderr, "phods     - MX: %d\tMY: %d\n", mx, my);
626
#endif
627

    
628
    /* half pixel search */
629
    *mx_ptr = mx;
630
    *my_ptr = my;
631
    return dminy;
632
}
633

    
634

    
635
#define Z_THRESHOLD 256
636

    
637
#define CHECK_SAD_HALF_MV(suffix, x, y) \
638
{\
639
    d= pix_abs_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
640
    d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
641
    COPY3_IF_LT(dminh, d, dx, x, dy, y)\
642
}
643

    
644
static inline int sad_hpel_motion_search(MpegEncContext * s,
645
                                  int *mx_ptr, int *my_ptr, int dmin,
646
                                  int xmin, int ymin, int xmax, int ymax,
647
                                  int pred_x, int pred_y, Picture *picture,
648
                                  int n, int size, uint16_t * const mv_penalty)
649
{
650
    uint8_t *ref_picture= picture->data[0];
651
    uint32_t *score_map= s->me.score_map;
652
    const int penalty_factor= s->me.sub_penalty_factor;
653
    int mx, my, xx, yy, dminh;
654
    UINT8 *pix, *ptr;
655
    op_pixels_abs_func pix_abs_x2;
656
    op_pixels_abs_func pix_abs_y2;
657
    op_pixels_abs_func pix_abs_xy2;
658
    
659
    if(size==0){
660
        pix_abs_x2 = s->dsp.pix_abs16x16_x2;
661
        pix_abs_y2 = s->dsp.pix_abs16x16_y2;
662
        pix_abs_xy2= s->dsp.pix_abs16x16_xy2;
663
    }else{
664
        pix_abs_x2 = s->dsp.pix_abs8x8_x2;
665
        pix_abs_y2 = s->dsp.pix_abs8x8_y2;
666
        pix_abs_xy2= s->dsp.pix_abs8x8_xy2;
667
    }
668

    
669
    if(s->me.skip){
670
//    printf("S");
671
        *mx_ptr = 0;
672
        *my_ptr = 0;
673
        return dmin;
674
    }
675
//    printf("N");
676
        
677
    xx = 16 * s->mb_x + 8*(n&1);
678
    yy = 16 * s->mb_y + 8*(n>>1);
679
    pix =  s->new_picture.data[0] + (yy * s->linesize) + xx;
680

    
681
    mx = *mx_ptr;
682
    my = *my_ptr;
683
    ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx);
684
    
685
    dminh = dmin;
686

    
687
    if (mx > xmin && mx < xmax && 
688
        my > ymin && my < ymax) {
689
        int dx=0, dy=0;
690
        int d, pen_x, pen_y; 
691
        const int index= (my<<ME_MAP_SHIFT) + mx;
692
        const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
693
        const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)];
694
        const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)];
695
        const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
696
        mx<<=1;
697
        my<<=1;
698

    
699
        
700
        pen_x= pred_x + mx;
701
        pen_y= pred_y + my;
702

    
703
        ptr-= s->linesize;
704
        if(t<=b){
705
            CHECK_SAD_HALF_MV(y2 , 0, -1)
706
            if(l<=r){
707
                CHECK_SAD_HALF_MV(xy2, -1, -1)
708
                if(t+r<=b+l){
709
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
710
                    ptr+= s->linesize;
711
                }else{
712
                    ptr+= s->linesize;
713
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
714
                }
715
                CHECK_SAD_HALF_MV(x2 , -1,  0)
716
            }else{
717
                CHECK_SAD_HALF_MV(xy2, +1, -1)
718
                if(t+l<=b+r){
719
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
720
                    ptr+= s->linesize;
721
                }else{
722
                    ptr+= s->linesize;
723
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
724
                }
725
                CHECK_SAD_HALF_MV(x2 , +1,  0)
726
            }
727
        }else{
728
            if(l<=r){
729
                if(t+l<=b+r){
730
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
731
                    ptr+= s->linesize;
732
                }else{
733
                    ptr+= s->linesize;
734
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
735
                }
736
                CHECK_SAD_HALF_MV(x2 , -1,  0)
737
                CHECK_SAD_HALF_MV(xy2, -1, +1)
738
            }else{
739
                if(t+r<=b+l){
740
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
741
                    ptr+= s->linesize;
742
                }else{
743
                    ptr+= s->linesize;
744
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
745
                }
746
                CHECK_SAD_HALF_MV(x2 , +1,  0)
747
                CHECK_SAD_HALF_MV(xy2, +1, +1)
748
            }
749
            CHECK_SAD_HALF_MV(y2 ,  0, +1)
750
        }
751
        mx+=dx;
752
        my+=dy;
753

    
754
    }else{
755
        mx<<=1;
756
        my<<=1;
757
    }
758

    
759
    *mx_ptr = mx;
760
    *my_ptr = my;
761
    return dminh;
762
}
763

    
764
static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
765
{
766
    const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2);
767
    
768
    s->p_mv_table[xy][0] = mx;
769
    s->p_mv_table[xy][1] = my;
770

    
771
    /* has allready been set to the 4 MV if 4MV is done */
772
    if(mv4){
773
        int mot_xy= s->block_index[0];
774

    
775
        s->motion_val[mot_xy  ][0]= mx;
776
        s->motion_val[mot_xy  ][1]= my;
777
        s->motion_val[mot_xy+1][0]= mx;
778
        s->motion_val[mot_xy+1][1]= my;
779

    
780
        mot_xy += s->block_wrap[0];
781
        s->motion_val[mot_xy  ][0]= mx;
782
        s->motion_val[mot_xy  ][1]= my;
783
        s->motion_val[mot_xy+1][0]= mx;
784
        s->motion_val[mot_xy+1][1]= my;
785
    }
786
}
787

    
788
static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code)
789
{
790
    *range = 8 * (1 << (f_code - 1));
791
    /* XXX: temporary kludge to avoid overflow for msmpeg4 */
792
    if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
793
        *range *= 2;
794

    
795
    if (s->unrestricted_mv) {
796
        *xmin = -16;
797
        *ymin = -16;
798
        if (s->h263_plus)
799
            *range *= 2;
800
        if(s->avctx->codec->id!=CODEC_ID_MPEG4){
801
            *xmax = s->mb_width*16;
802
            *ymax = s->mb_height*16;
803
        }else {
804
            *xmax = s->width;
805
            *ymax = s->height;
806
        }
807
    } else {
808
        *xmin = 0;
809
        *ymin = 0;
810
        *xmax = s->mb_width*16 - 16;
811
        *ymax = s->mb_height*16 - 16;
812
    }
813
}
814

    
815
static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift)
816
{
817
    int block;
818
    int P[10][2];
819
    int dmin_sum=0, mx4_sum=0, my4_sum=0;
820
    uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
821

    
822
    for(block=0; block<4; block++){
823
        int mx4, my4;
824
        int pred_x4, pred_y4;
825
        int dmin4;
826
        static const int off[4]= {2, 1, 1, -1};
827
        const int mot_stride = s->block_wrap[0];
828
        const int mot_xy = s->block_index[block];
829
//        const int block_x= (block&1);
830
//        const int block_y= (block>>1);
831
#if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way
832
        const int rel_xmin4= xmin;
833
        const int rel_xmax4= xmax;
834
        const int rel_ymin4= ymin;
835
        const int rel_ymax4= ymax;
836
#else
837
        const int rel_xmin4= xmin - block_x*8;
838
        const int rel_xmax4= xmax - block_x*8 + 8;
839
        const int rel_ymin4= ymin - block_y*8;
840
        const int rel_ymax4= ymax - block_y*8 + 8;
841
#endif
842
        P_LEFT[0] = s->motion_val[mot_xy - 1][0];
843
        P_LEFT[1] = s->motion_val[mot_xy - 1][1];
844

    
845
        if(P_LEFT[0]       > (rel_xmax4<<shift)) P_LEFT[0]       = (rel_xmax4<<shift);
846

    
847
        /* special case for first line */
848
        if (s->mb_y == 0 && block<2) {
849
            pred_x4= P_LEFT[0];
850
            pred_y4= P_LEFT[1];
851
        } else {
852
            P_TOP[0]      = s->motion_val[mot_xy - mot_stride             ][0];
853
            P_TOP[1]      = s->motion_val[mot_xy - mot_stride             ][1];
854
            P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
855
            P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
856
            if(P_TOP[1]      > (rel_ymax4<<shift)) P_TOP[1]     = (rel_ymax4<<shift);
857
            if(P_TOPRIGHT[0] < (rel_xmin4<<shift)) P_TOPRIGHT[0]= (rel_xmin4<<shift);
858
            if(P_TOPRIGHT[0] > (rel_xmax4<<shift)) P_TOPRIGHT[0]= (rel_xmax4<<shift);
859
            if(P_TOPRIGHT[1] > (rel_ymax4<<shift)) P_TOPRIGHT[1]= (rel_ymax4<<shift);
860
    
861
            P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
862
            P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
863

    
864
//            if(s->out_format == FMT_H263){
865
                pred_x4 = P_MEDIAN[0];
866
                pred_y4 = P_MEDIAN[1];
867
#if 0
868
            }else { /* mpeg1 at least */
869
                pred_x4= P_LEFT[0];
870
                pred_y4= P_LEFT[1];
871
            }
872
#endif
873
        }
874
        P_MV1[0]= mx;
875
        P_MV1[1]= my;
876

    
877
        dmin4 = s->me.motion_search[1](s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, 
878
                                       &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty);
879

    
880
        dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, 
881
                                          pred_x4, pred_y4, &s->last_picture, block, 1, mv_penalty);
882
        
883
        if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
884
            int dxy;
885
            const int offset= ((block&1) + (block>>1)*s->linesize)*8;
886
            uint8_t *dest_y = s->me.scratchpad + offset;
887

    
888
            if(s->quarter_sample){
889
                uint8_t *ref= s->last_picture.data[0] + (s->mb_x*16 + (mx4>>2)) + (s->mb_y*16 + (my4>>2))*s->linesize + offset;
890
                dxy = ((my4 & 3) << 2) | (mx4 & 3);
891

    
892
                if(s->no_rounding)
893
                    s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y   , ref    , s->linesize);
894
                else
895
                    s->dsp.put_qpel_pixels_tab       [1][dxy](dest_y   , ref    , s->linesize);
896
            }else{
897
                uint8_t *ref= s->last_picture.data[0] + (s->mb_x*16 + (mx4>>1)) + (s->mb_y*16 + (my4>>1))*s->linesize + offset;
898
                dxy = ((my4 & 1) << 1) | (mx4 & 1);
899

    
900
                if(s->no_rounding)
901
                    s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y    , ref    , s->linesize, 8);
902
                else
903
                    s->dsp.put_pixels_tab       [1][dxy](dest_y    , ref    , s->linesize, 8);
904
            }
905
            dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*s->me.mb_penalty_factor;
906
        }else
907
            dmin_sum+= dmin4;
908

    
909
        if(s->quarter_sample){
910
            mx4_sum+= mx4/2;
911
            my4_sum+= my4/2;
912
        }else{
913
            mx4_sum+= mx4;
914
            my4_sum+= my4;
915
        }
916
            
917
        s->motion_val[ s->block_index[block] ][0]= mx4;
918
        s->motion_val[ s->block_index[block] ][1]= my4;
919
    }
920
    
921
    if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
922
        dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*s->linesize, s->me.scratchpad, s->linesize);
923
    }
924
    
925
    if(s->avctx->mb_cmp&FF_CMP_CHROMA){
926
        int dxy;
927
        int mx, my;
928
        int offset;
929

    
930
        mx= ff_h263_round_chroma(mx4_sum);
931
        my= ff_h263_round_chroma(my4_sum);
932
        dxy = ((my & 1) << 1) | (mx & 1);
933
        
934
        offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
935
       
936
        if(s->no_rounding){
937
            s->dsp.put_no_rnd_pixels_tab[1][dxy](s->me.scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
938
            s->dsp.put_no_rnd_pixels_tab[1][dxy](s->me.scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
939
        }else{
940
            s->dsp.put_pixels_tab       [1][dxy](s->me.scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
941
            s->dsp.put_pixels_tab       [1][dxy](s->me.scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
942
        }
943

    
944
        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);
945
        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);
946
    }
947

    
948
    switch(s->avctx->mb_cmp&0xFF){
949
    /*case FF_CMP_SSE:
950
        return dmin_sum+ 32*s->qscale*s->qscale;*/
951
    case FF_CMP_RD:
952
        return dmin_sum;
953
    default:
954
        return dmin_sum+ 11*s->me.mb_penalty_factor;
955
    }
956
}
957

    
958
void ff_estimate_p_frame_motion(MpegEncContext * s,
959
                                int mb_x, int mb_y)
960
{
961
    UINT8 *pix, *ppix;
962
    int sum, varc, vard, mx, my, range, dmin, xx, yy;
963
    int xmin, ymin, xmax, ymax;
964
    int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
965
    int pred_x=0, pred_y=0;
966
    int P[10][2];
967
    const int shift= 1+s->quarter_sample;
968
    int mb_type=0;
969
    uint8_t *ref_picture= s->last_picture.data[0];
970
    Picture * const pic= &s->current_picture;
971
    uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
972
    
973
    assert(s->quarter_sample==0 || s->quarter_sample==1);
974

    
975
    s->me.penalty_factor    = get_penalty_factor(s, s->avctx->me_cmp);
976
    s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
977
    s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
978

    
979
    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
980
    rel_xmin= xmin - mb_x*16;
981
    rel_xmax= xmax - mb_x*16;
982
    rel_ymin= ymin - mb_y*16;
983
    rel_ymax= ymax - mb_y*16;
984
    s->me.skip=0;
985

    
986
    switch(s->me_method) {
987
    case ME_ZERO:
988
    default:
989
        no_motion_search(s, &mx, &my);
990
        mx-= mb_x*16;
991
        my-= mb_y*16;
992
        dmin = 0;
993
        break;
994
    case ME_FULL:
995
        dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
996
        mx-= mb_x*16;
997
        my-= mb_y*16;
998
        break;
999
    case ME_LOG:
1000
        dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1001
        mx-= mb_x*16;
1002
        my-= mb_y*16;
1003
        break;
1004
    case ME_PHODS:
1005
        dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1006
        mx-= mb_x*16;
1007
        my-= mb_y*16;
1008
        break;
1009
    case ME_X1:
1010
    case ME_EPZS:
1011
       {
1012
            const int mot_stride = s->block_wrap[0];
1013
            const int mot_xy = s->block_index[0];
1014

    
1015
            P_LEFT[0]       = s->motion_val[mot_xy - 1][0];
1016
            P_LEFT[1]       = s->motion_val[mot_xy - 1][1];
1017

    
1018
            if(P_LEFT[0]       > (rel_xmax<<shift)) P_LEFT[0]       = (rel_xmax<<shift);
1019

    
1020
            if(mb_y) {
1021
                P_TOP[0]      = s->motion_val[mot_xy - mot_stride    ][0];
1022
                P_TOP[1]      = s->motion_val[mot_xy - mot_stride    ][1];
1023
                P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + 2][0];
1024
                P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + 2][1];
1025
                if(P_TOP[1]      > (rel_ymax<<shift)) P_TOP[1]     = (rel_ymax<<shift);
1026
                if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
1027
                if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
1028
        
1029
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1030
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1031

    
1032
                if(s->out_format == FMT_H263){
1033
                    pred_x = P_MEDIAN[0];
1034
                    pred_y = P_MEDIAN[1];
1035
                }else { /* mpeg1 at least */
1036
                    pred_x= P_LEFT[0];
1037
                    pred_y= P_LEFT[1];
1038
                }
1039
            }else{
1040
                pred_x= P_LEFT[0];
1041
                pred_y= P_LEFT[1];
1042
            }
1043

    
1044
        }
1045
        dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, 
1046
                                      &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty);
1047
 
1048
        break;
1049
    }
1050

    
1051
    /* intra / predictive decision */
1052
    xx = mb_x * 16;
1053
    yy = mb_y * 16;
1054

    
1055
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
1056
    /* At this point (mx,my) are full-pell and the relative displacement */
1057
    ppix = ref_picture + ((yy+my) * s->linesize) + (xx+mx);
1058
    
1059
    sum = s->dsp.pix_sum(pix, s->linesize);
1060
    
1061
    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
1062
    vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize)+128)>>8;
1063

    
1064
//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);
1065
    pic->mb_var   [s->mb_width * mb_y + mb_x] = varc;
1066
    pic->mc_mb_var[s->mb_width * mb_y + mb_x] = vard;
1067
    pic->mb_mean  [s->mb_width * mb_y + mb_x] = (sum+128)>>8;
1068
//    pic->mb_cmp_score[s->mb_width * mb_y + mb_x] = dmin; 
1069
    pic->mb_var_sum    += varc;
1070
    pic->mc_mb_var_sum += vard;
1071
//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);
1072
    
1073
#if 0
1074
    printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
1075
           varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
1076
#endif
1077
    if(s->flags&CODEC_FLAG_HQ){
1078
        if (vard <= 64 || vard < varc)
1079
            s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1080
        else
1081
            s->scene_change_score+= s->qscale;
1082

    
1083
        if (vard*2 + 200 > varc)
1084
            mb_type|= MB_TYPE_INTRA;
1085
        if (varc*2 + 200 > vard){
1086
            mb_type|= MB_TYPE_INTER;
1087
            s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1088
                                   pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty);
1089
        }else{
1090
            mx <<=shift;
1091
            my <<=shift;
1092
        }
1093
        if((s->flags&CODEC_FLAG_4MV)
1094
           && !s->me.skip && varc>50 && vard>10){
1095
            h263_mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
1096
            mb_type|=MB_TYPE_INTER4V;
1097

    
1098
            set_p_mv_tables(s, mx, my, 0);
1099
        }else
1100
            set_p_mv_tables(s, mx, my, 1);
1101
    }else{
1102
        mb_type= MB_TYPE_INTER;
1103

    
1104
        dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1105
                                    pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty);
1106
        
1107
        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
1108
            dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, &s->last_picture, mv_penalty);
1109

    
1110
        if((s->flags&CODEC_FLAG_4MV)
1111
           && !s->me.skip && varc>50 && vard>10){
1112
            int dmin4= h263_mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
1113
            if(dmin4 < dmin){
1114
                mb_type= MB_TYPE_INTER4V;
1115
                dmin=dmin4;
1116
            }
1117
        }
1118
        pic->mb_cmp_score[s->mb_width * mb_y + mb_x] = dmin; 
1119
        set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V);
1120
        
1121
        if (vard <= 64 || vard < varc) {
1122
            s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1123
        }else{
1124
            s->scene_change_score+= s->qscale;
1125
        }
1126
    }
1127

    
1128
    s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1129
}
1130

    
1131
int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
1132
                                    int mb_x, int mb_y)
1133
{
1134
    int mx, my, range, dmin;
1135
    int xmin, ymin, xmax, ymax;
1136
    int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
1137
    int pred_x=0, pred_y=0;
1138
    int P[10][2];
1139
    const int shift= 1+s->quarter_sample;
1140
    uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
1141
    const int mv_stride= s->mb_width + 2;
1142
    const int xy= mb_x + 1 + (mb_y + 1)*mv_stride;
1143
    
1144
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1145

    
1146
    s->me.pre_penalty_factor    = get_penalty_factor(s, s->avctx->me_pre_cmp);
1147

    
1148
    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
1149
    rel_xmin= xmin - mb_x*16;
1150
    rel_xmax= xmax - mb_x*16;
1151
    rel_ymin= ymin - mb_y*16;
1152
    rel_ymax= ymax - mb_y*16;
1153
    s->me.skip=0;
1154

    
1155
    P_LEFT[0]       = s->p_mv_table[xy + 1][0];
1156
    P_LEFT[1]       = s->p_mv_table[xy + 1][1];
1157

    
1158
    if(P_LEFT[0]       < (rel_xmin<<shift)) P_LEFT[0]       = (rel_xmin<<shift);
1159

    
1160
    /* special case for first line */
1161
    if (mb_y == s->mb_height-1) {
1162
        pred_x= P_LEFT[0];
1163
        pred_y= P_LEFT[1];
1164
        P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1165
        P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME 
1166
    } else {
1167
        P_TOP[0]      = s->p_mv_table[xy + mv_stride    ][0];
1168
        P_TOP[1]      = s->p_mv_table[xy + mv_stride    ][1];
1169
        P_TOPRIGHT[0] = s->p_mv_table[xy + mv_stride - 1][0];
1170
        P_TOPRIGHT[1] = s->p_mv_table[xy + mv_stride - 1][1];
1171
        if(P_TOP[1]      < (rel_ymin<<shift)) P_TOP[1]     = (rel_ymin<<shift);
1172
        if(P_TOPRIGHT[0] > (rel_xmax<<shift)) P_TOPRIGHT[0]= (rel_xmax<<shift);
1173
        if(P_TOPRIGHT[1] < (rel_ymin<<shift)) P_TOPRIGHT[1]= (rel_ymin<<shift);
1174
    
1175
        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1176
        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1177

    
1178
        pred_x = P_MEDIAN[0];
1179
        pred_y = P_MEDIAN[1];
1180
    }
1181
    dmin = s->me.pre_motion_search(s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, 
1182
                                   &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty);
1183

    
1184
    s->p_mv_table[xy][0] = mx<<shift;
1185
    s->p_mv_table[xy][1] = my<<shift;
1186
    
1187
    return dmin;
1188
}
1189

    
1190
int ff_estimate_motion_b(MpegEncContext * s,
1191
                       int mb_x, int mb_y, int16_t (*mv_table)[2], Picture *picture, int f_code)
1192
{
1193
    int mx, my, range, dmin;
1194
    int xmin, ymin, xmax, ymax;
1195
    int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
1196
    int pred_x=0, pred_y=0;
1197
    int P[10][2];
1198
    const int shift= 1+s->quarter_sample;
1199
    const int mot_stride = s->mb_width + 2;
1200
    const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
1201
    uint8_t * const ref_picture= picture->data[0];
1202
    uint16_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV;
1203
    int mv_scale;
1204
        
1205
    s->me.penalty_factor    = get_penalty_factor(s, s->avctx->me_cmp);
1206
    s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
1207
    s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
1208

    
1209
    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
1210
    rel_xmin= xmin - mb_x*16;
1211
    rel_xmax= xmax - mb_x*16;
1212
    rel_ymin= ymin - mb_y*16;
1213
    rel_ymax= ymax - mb_y*16;
1214

    
1215
    switch(s->me_method) {
1216
    case ME_ZERO:
1217
    default:
1218
        no_motion_search(s, &mx, &my);
1219
        dmin = 0;
1220
        mx-= mb_x*16;
1221
        my-= mb_y*16;
1222
        break;
1223
    case ME_FULL:
1224
        dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
1225
        mx-= mb_x*16;
1226
        my-= mb_y*16;
1227
        break;
1228
    case ME_LOG:
1229
        dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1230
        mx-= mb_x*16;
1231
        my-= mb_y*16;
1232
        break;
1233
    case ME_PHODS:
1234
        dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1235
        mx-= mb_x*16;
1236
        my-= mb_y*16;
1237
        break;
1238
    case ME_X1:
1239
    case ME_EPZS:
1240
       {
1241
            P_LEFT[0]        = mv_table[mot_xy - 1][0];
1242
            P_LEFT[1]        = mv_table[mot_xy - 1][1];
1243

    
1244
            if(P_LEFT[0]       > (rel_xmax<<shift)) P_LEFT[0]       = (rel_xmax<<shift);
1245

    
1246
            /* special case for first line */
1247
            if (mb_y) {
1248
                P_TOP[0] = mv_table[mot_xy - mot_stride             ][0];
1249
                P_TOP[1] = mv_table[mot_xy - mot_stride             ][1];
1250
                P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1         ][0];
1251
                P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1         ][1];
1252
                if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1]= (rel_ymax<<shift);
1253
                if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
1254
                if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
1255
        
1256
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1257
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1258
            }
1259
            pred_x= P_LEFT[0];
1260
            pred_y= P_LEFT[1];
1261
        }
1262
        
1263
        if(mv_table == s->b_forw_mv_table){
1264
            mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
1265
        }else{
1266
            mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
1267
        }
1268
        
1269
        dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, 
1270
                                      picture, s->p_mv_table, mv_scale, mv_penalty);
1271
 
1272
        break;
1273
    }
1274
    
1275
    dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1276
                                   pred_x, pred_y, picture, 0, 0, mv_penalty);
1277
                                   
1278
    if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
1279
        dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, picture, mv_penalty);
1280

    
1281
//printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
1282
//    s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1283
    mv_table[mot_xy][0]= mx;
1284
    mv_table[mot_xy][1]= my;
1285

    
1286
    return dmin;
1287
}
1288

    
1289
static inline int check_bidir_mv(MpegEncContext * s,
1290
                   int mb_x, int mb_y,
1291
                   int motion_fx, int motion_fy,
1292
                   int motion_bx, int motion_by,
1293
                   int pred_fx, int pred_fy,
1294
                   int pred_bx, int pred_by)
1295
{
1296
    //FIXME optimize?
1297
    //FIXME move into template?
1298
    //FIXME better f_code prediction (max mv & distance)
1299
    UINT16 *mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
1300
    uint8_t *dest_y = s->me.scratchpad;
1301
    uint8_t *ptr;
1302
    int dxy;
1303
    int src_x, src_y;
1304
    int fbmin;
1305

    
1306
    if(s->quarter_sample){
1307
        dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1308
        src_x = mb_x * 16 + (motion_fx >> 2);
1309
        src_y = mb_y * 16 + (motion_fy >> 2);
1310
        assert(src_x >=-16 && src_x<=s->width);
1311
        assert(src_y >=-16 && src_y<=s->height);
1312

    
1313
        ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x;
1314
        s->dsp.put_qpel_pixels_tab[0][dxy](dest_y    , ptr    , s->linesize);
1315

    
1316
        dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1317
        src_x = mb_x * 16 + (motion_bx >> 2);
1318
        src_y = mb_y * 16 + (motion_by >> 2);
1319
        assert(src_x >=-16 && src_x<=s->width);
1320
        assert(src_y >=-16 && src_y<=s->height);
1321
    
1322
        ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x;
1323
        s->dsp.avg_qpel_pixels_tab[0][dxy](dest_y    , ptr    , s->linesize);
1324
    }else{
1325
        dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1326
        src_x = mb_x * 16 + (motion_fx >> 1);
1327
        src_y = mb_y * 16 + (motion_fy >> 1);
1328
        assert(src_x >=-16 && src_x<=s->width);
1329
        assert(src_y >=-16 && src_y<=s->height);
1330

    
1331
        ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x;
1332
        s->dsp.put_pixels_tab[0][dxy](dest_y    , ptr    , s->linesize, 16);
1333

    
1334
        dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1335
        src_x = mb_x * 16 + (motion_bx >> 1);
1336
        src_y = mb_y * 16 + (motion_by >> 1);
1337
        assert(src_x >=-16 && src_x<=s->width);
1338
        assert(src_y >=-16 && src_y<=s->height);
1339
    
1340
        ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x;
1341
        s->dsp.avg_pixels_tab[0][dxy](dest_y    , ptr    , s->linesize, 16);
1342
    }
1343

    
1344
    fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->me.mb_penalty_factor
1345
           +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->me.mb_penalty_factor
1346
           + s->dsp.mb_cmp[0](s, s->new_picture.data[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize);
1347
           
1348
    if(s->avctx->mb_cmp&FF_CMP_CHROMA){
1349
    }
1350
    //FIXME CHROMA !!!
1351
           
1352
    return fbmin;
1353
}
1354

    
1355
/* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1356
static inline int bidir_refine(MpegEncContext * s,
1357
                                  int mb_x, int mb_y)
1358
{
1359
    const int mot_stride = s->mb_width + 2;
1360
    const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
1361
    int fbmin;
1362
    int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1363
    int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1364
    int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1365
    int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1366
    int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1367
    int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1368
    int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1369
    int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1370

    
1371
    //FIXME do refinement and add flag
1372
    
1373
    fbmin= check_bidir_mv(s, mb_x, mb_y, 
1374
                          motion_fx, motion_fy,
1375
                          motion_bx, motion_by,
1376
                          pred_fx, pred_fy,
1377
                          pred_bx, pred_by);
1378

    
1379
   return fbmin;
1380
}
1381

    
1382
static inline int direct_search(MpegEncContext * s,
1383
                                int mb_x, int mb_y)
1384
{
1385
    int P[10][2];
1386
    const int mot_stride = s->mb_width + 2;
1387
    const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
1388
    const int shift= 1+s->quarter_sample;
1389
    int dmin, i;
1390
    const int time_pp= s->pp_time;
1391
    const int time_pb= s->pb_time;
1392
    int mx, my, xmin, xmax, ymin, ymax;
1393
    int16_t (*mv_table)[2]= s->b_direct_mv_table;
1394
    uint16_t * const mv_penalty= s->me.mv_penalty[1] + MAX_MV;
1395
    
1396
    ymin= xmin=(-32)>>shift;
1397
    ymax= xmax=   31>>shift;
1398

    
1399
    if(s->co_located_type_table[mb_x + mb_y*s->mb_width]==CO_LOCATED_TYPE_4MV){
1400
        s->mv_type= MV_TYPE_8X8;
1401
    }else{
1402
        s->mv_type= MV_TYPE_16X16;
1403
    }
1404

    
1405
    for(i=0; i<4; i++){
1406
        int index= s->block_index[i];
1407
        int min, max;
1408
    
1409
        s->me.co_located_mv[i][0]= s->motion_val[index][0];
1410
        s->me.co_located_mv[i][1]= s->motion_val[index][1];
1411
        s->me.direct_basis_mv[i][0]= s->me.co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1412
        s->me.direct_basis_mv[i][1]= s->me.co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1413
//        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);
1414
//        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);
1415

    
1416
        max= FFMAX(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift;
1417
        min= FFMIN(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift;
1418
        max+= (2*mb_x + (i& 1))*8 + 1; // +-1 is for the simpler rounding
1419
        min+= (2*mb_x + (i& 1))*8 - 1;
1420
        xmax= FFMIN(xmax, s->width - max);
1421
        xmin= FFMAX(xmin, - 16     - min);
1422

    
1423
        max= FFMAX(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift;
1424
        min= FFMIN(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift;
1425
        max+= (2*mb_y + (i>>1))*8 + 1; // +-1 is for the simpler rounding
1426
        min+= (2*mb_y + (i>>1))*8 - 1;
1427
        ymax= FFMIN(ymax, s->height - max);
1428
        ymin= FFMAX(ymin, - 16      - min);
1429
        
1430
        if(s->mv_type == MV_TYPE_16X16) break;
1431
    }
1432
    
1433
    assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1434
    
1435
    if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1436
        s->b_direct_mv_table[mot_xy][0]= 0;
1437
        s->b_direct_mv_table[mot_xy][1]= 0;
1438

    
1439
        return 256*256*256*64;
1440
    }
1441

    
1442
    P_LEFT[0]        = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
1443
    P_LEFT[1]        = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
1444

    
1445
    /* special case for first line */
1446
    if (mb_y) {
1447
        P_TOP[0]      = clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);
1448
        P_TOP[1]      = clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);
1449
        P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);
1450
        P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);
1451
    
1452
        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1453
        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1454
    }
1455
 
1456
    //FIXME direct_search  ptr in context!!! (needed for chroma anyway or this will get messy)   
1457
    if(s->flags&CODEC_FLAG_QPEL){
1458
        dmin = simple_direct_qpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax, 
1459
                                                     &s->last_picture, mv_table, 1<<14, mv_penalty);
1460
        dmin = simple_direct_qpel_qpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax,
1461
                                                0, 0, &s->last_picture, 0, 0, mv_penalty);
1462
        
1463
        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
1464
            dmin= simple_direct_qpel_qpel_get_mb_score(s, mx, my, 0, 0, &s->last_picture, mv_penalty);
1465
    }else{
1466
        dmin = simple_direct_hpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax, 
1467
                                                     &s->last_picture, mv_table, 1<<15, mv_penalty);
1468
        dmin = simple_direct_hpel_hpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax,
1469
                                                0, 0, &s->last_picture, 0, 0, mv_penalty);
1470

    
1471
        if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip)
1472
            dmin= simple_direct_hpel_hpel_get_mb_score(s, mx, my, 0, 0, &s->last_picture, mv_penalty);
1473
    }
1474

    
1475
    s->b_direct_mv_table[mot_xy][0]= mx;
1476
    s->b_direct_mv_table[mot_xy][1]= my;
1477
    return dmin;
1478
}
1479

    
1480
void ff_estimate_b_frame_motion(MpegEncContext * s,
1481
                             int mb_x, int mb_y)
1482
{
1483
    const int penalty_factor= s->me.mb_penalty_factor;
1484
    int fmin, bmin, dmin, fbmin;
1485
    int type=0;
1486
    
1487
    dmin= direct_search(s, mb_x, mb_y);
1488

    
1489
    fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, &s->last_picture, s->f_code) + 3*penalty_factor;
1490
    bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, &s->next_picture, s->b_code) + 2*penalty_factor;
1491
//printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1492

    
1493
    fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
1494
//printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1495
    {
1496
        int score= dmin;
1497
        type=MB_TYPE_DIRECT;
1498
        
1499
        if(fmin<score){
1500
            score=fmin;
1501
            type= MB_TYPE_FORWARD; 
1502
        }
1503
        if(bmin<score){
1504
            score=bmin;
1505
            type= MB_TYPE_BACKWARD; 
1506
        }
1507
        if(fbmin<score){
1508
            score=fbmin;
1509
            type= MB_TYPE_BIDIR;
1510
        }
1511
        
1512
        score= ((unsigned)(score*score + 128*256))>>16;
1513
        s->current_picture.mc_mb_var_sum += score;
1514
        s->current_picture.mc_mb_var[mb_y*s->mb_width + mb_x] = score; //FIXME use SSE
1515
    }
1516

    
1517
    if(s->flags&CODEC_FLAG_HQ){
1518
        type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT; //FIXME something smarter
1519
        if(dmin>256*256*16) type&= ~MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
1520
    }
1521

    
1522
    s->mb_type[mb_y*s->mb_width + mb_x]= type;
1523
}
1524

    
1525
/* find best f_code for ME which do unlimited searches */
1526
int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
1527
{
1528
    if(s->me_method>=ME_EPZS){
1529
        int score[8];
1530
        int i, y;
1531
        UINT8 * fcode_tab= s->fcode_tab;
1532
        int best_fcode=-1;
1533
        int best_score=-10000000;
1534

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

    
1537
        for(y=0; y<s->mb_height; y++){
1538
            int x;
1539
            int xy= (y+1)* (s->mb_width+2) + 1;
1540
            i= y*s->mb_width;
1541
            for(x=0; x<s->mb_width; x++){
1542
                if(s->mb_type[i] & type){
1543
                    int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV],
1544
                                     fcode_tab[mv_table[xy][1] + MAX_MV]);
1545
                    int j;
1546
                    
1547
                    for(j=0; j<fcode && j<8; j++){
1548
                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[i] < s->current_picture.mb_var[i])
1549
                            score[j]-= 170;
1550
                    }
1551
                }
1552
                i++;
1553
                xy++;
1554
            }
1555
        }
1556
        
1557
        for(i=1; i<8; i++){
1558
            if(score[i] > best_score){
1559
                best_score= score[i];
1560
                best_fcode= i;
1561
            }
1562
//            printf("%d %d\n", i, score[i]);
1563
        }
1564

    
1565
//    printf("fcode: %d type: %d\n", i, s->pict_type);
1566
        return best_fcode;
1567
/*        for(i=0; i<=MAX_FCODE; i++){
1568
            printf("%d ", mv_num[i]);
1569
        }
1570
        printf("\n");*/
1571
    }else{
1572
        return 1;
1573
    }
1574
}
1575

    
1576
void ff_fix_long_p_mvs(MpegEncContext * s)
1577
{
1578
    const int f_code= s->f_code;
1579
    int y;
1580
    UINT8 * fcode_tab= s->fcode_tab;
1581
//int clip=0;
1582
//int noclip=0;
1583
    /* clip / convert to intra 16x16 type MVs */
1584
    for(y=0; y<s->mb_height; y++){
1585
        int x;
1586
        int xy= (y+1)* (s->mb_width+2)+1;
1587
        int i= y*s->mb_width;
1588
        for(x=0; x<s->mb_width; x++){
1589
            if(s->mb_type[i]&MB_TYPE_INTER){
1590
                if(   fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code
1591
                   || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
1592
                   || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
1593
                   || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
1594
                    s->mb_type[i] &= ~MB_TYPE_INTER;
1595
                    s->mb_type[i] |= MB_TYPE_INTRA;
1596
                    s->p_mv_table[xy][0] = 0;
1597
                    s->p_mv_table[xy][1] = 0;
1598
//clip++;
1599
                }
1600
//else
1601
//  noclip++;
1602
            }
1603
            xy++;
1604
            i++;
1605
        }
1606
    }
1607
//printf("%d no:%d %d//\n", clip, noclip, f_code);
1608
    if(s->flags&CODEC_FLAG_4MV){
1609
        const int wrap= 2+ s->mb_width*2;
1610

    
1611
        /* clip / convert to intra 8x8 type MVs */
1612
        for(y=0; y<s->mb_height; y++){
1613
            int xy= (y*2 + 1)*wrap + 1;
1614
            int i= y*s->mb_width;
1615
            int x;
1616

    
1617
            for(x=0; x<s->mb_width; x++){
1618
                if(s->mb_type[i]&MB_TYPE_INTER4V){
1619
                    int block;
1620
                    for(block=0; block<4; block++){
1621
                        int off= (block& 1) + (block>>1)*wrap;
1622
                        int mx= s->motion_val[ xy + off ][0];
1623
                        int my= s->motion_val[ xy + off ][1];
1624

    
1625
                        if(   fcode_tab[mx + MAX_MV] > f_code
1626
                           || fcode_tab[mx + MAX_MV] == 0
1627
                           || fcode_tab[my + MAX_MV] > f_code
1628
                           || fcode_tab[my + MAX_MV] == 0 ){
1629
                            s->mb_type[i] &= ~MB_TYPE_INTER4V;
1630
                            s->mb_type[i] |= MB_TYPE_INTRA;
1631
                        }
1632
                    }
1633
                }
1634
                xy+=2;
1635
                i++;
1636
            }
1637
        }
1638
    }
1639
}
1640

    
1641
void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type)
1642
{
1643
    int y;
1644
    UINT8 * fcode_tab= s->fcode_tab;
1645

    
1646
    /* clip / convert to intra 16x16 type MVs */
1647
    for(y=0; y<s->mb_height; y++){
1648
        int x;
1649
        int xy= (y+1)* (s->mb_width+2)+1;
1650
        int i= y*s->mb_width;
1651
        for(x=0; x<s->mb_width; x++){
1652
            if(   fcode_tab[mv_table[xy][0] + MAX_MV] > f_code
1653
               || fcode_tab[mv_table[xy][0] + MAX_MV] == 0){
1654
                if(mv_table[xy][0]>0) mv_table[xy][0]=  (16<<f_code)-1;
1655
                else                  mv_table[xy][0]= -(16<<f_code);
1656
            }
1657
            if(   fcode_tab[mv_table[xy][1] + MAX_MV] > f_code
1658
               || fcode_tab[mv_table[xy][1] + MAX_MV] == 0){
1659
                if(mv_table[xy][1]>0) mv_table[xy][1]=  (16<<f_code)-1;
1660
                else                  mv_table[xy][1]= -(16<<f_code);
1661
            }
1662
            xy++;
1663
            i++;
1664
        }
1665
    }
1666
}