Statistics
| Branch: | Revision:

ffmpeg / libavcodec / motion_est.c @ f66e4f5f

History | View | Annotate | Download (76.3 KB)

1
/*
2
 * Motion estimation
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
4
 * Copyright (c) 2002-2004 Michael Niedermayer
5
 *
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 *
23
 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
24
 */
25

    
26
/**
27
 * @file motion_est.c
28
 * Motion estimation.
29
 */
30

    
31
#include <stdlib.h>
32
#include <stdio.h>
33
#include <limits.h>
34
#include "avcodec.h"
35
#include "dsputil.h"
36
#include "mpegvideo.h"
37

    
38
#undef NDEBUG
39
#include <assert.h>
40

    
41
#define SQ(a) ((a)*(a))
42

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

    
49
static inline int sad_hpel_motion_search(MpegEncContext * s,
50
                                  int *mx_ptr, int *my_ptr, int dmin,
51
                                  int src_index, int ref_index,
52
                                  int size, int h);
53

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

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

    
71
static int minima_cmp(const void *a, const void *b){
72
    const Minima *da = (const Minima *) a;
73
    const Minima *db = (const Minima *) b;
74

    
75
    return da->height - db->height;
76
}
77

    
78
#define FLAG_QPEL   1 //must be 1
79
#define FLAG_CHROMA 2
80
#define FLAG_DIRECT 4
81

    
82
static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
83
    const int offset[3]= {
84
          y*c->  stride + x,
85
        ((y*c->uvstride + x)>>1),
86
        ((y*c->uvstride + x)>>1),
87
    };
88
    int i;
89
    for(i=0; i<3; i++){
90
        c->src[0][i]= src [i] + offset[i];
91
        c->ref[0][i]= ref [i] + offset[i];
92
    }
93
    if(ref_index){
94
        for(i=0; i<3; i++){
95
            c->ref[ref_index][i]= ref2[i] + offset[i];
96
        }
97
    }
98
}
99

    
100
static int get_flags(MotionEstContext *c, int direct, int chroma){
101
    return   ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
102
           + (direct ? FLAG_DIRECT : 0)
103
           + (chroma ? FLAG_CHROMA : 0);
104
}
105

    
106
static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
107
                      const int size, const int h, int ref_index, int src_index,
108
                      me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
109
    MotionEstContext * const c= &s->me;
110
    const int stride= c->stride;
111
    const int uvstride= c->uvstride;
112
    const int qpel= flags&FLAG_QPEL;
113
    const int chroma= flags&FLAG_CHROMA;
114
    const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
115
    const int hx= subx + (x<<(1+qpel));
116
    const int hy= suby + (y<<(1+qpel));
117
    uint8_t * const * const ref= c->ref[ref_index];
118
    uint8_t * const * const src= c->src[src_index];
119
    int d;
120
    //FIXME check chroma 4mv, (no crashes ...)
121
    if(flags&FLAG_DIRECT){
122
        assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
123
        if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
124
            const int time_pp= s->pp_time;
125
            const int time_pb= s->pb_time;
126
            const int mask= 2*qpel+1;
127
            if(s->mv_type==MV_TYPE_8X8){
128
                int i;
129
                for(i=0; i<4; i++){
130
                    int fx = c->direct_basis_mv[i][0] + hx;
131
                    int fy = c->direct_basis_mv[i][1] + hy;
132
                    int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));
133
                    int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));
134
                    int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
135
                    int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
136

    
137
                    uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
138
                    if(qpel){
139
                        c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
140
                        c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
141
                    }else{
142
                        c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
143
                        c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
144
                    }
145
                }
146
            }else{
147
                int fx = c->direct_basis_mv[0][0] + hx;
148
                int fy = c->direct_basis_mv[0][1] + hy;
149
                int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
150
                int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
151
                int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
152
                int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
153

    
154
                if(qpel){
155
                    c->qpel_put[1][fxy](c->temp               , ref[0] + (fx>>2) + (fy>>2)*stride               , stride);
156
                    c->qpel_put[1][fxy](c->temp + 8           , ref[0] + (fx>>2) + (fy>>2)*stride + 8           , stride);
157
                    c->qpel_put[1][fxy](c->temp     + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride     + 8*stride, stride);
158
                    c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
159
                    c->qpel_avg[1][bxy](c->temp               , ref[8] + (bx>>2) + (by>>2)*stride               , stride);
160
                    c->qpel_avg[1][bxy](c->temp + 8           , ref[8] + (bx>>2) + (by>>2)*stride + 8           , stride);
161
                    c->qpel_avg[1][bxy](c->temp     + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride     + 8*stride, stride);
162
                    c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
163
                }else{
164
                    assert((fx>>1) + 16*s->mb_x >= -16);
165
                    assert((fy>>1) + 16*s->mb_y >= -16);
166
                    assert((fx>>1) + 16*s->mb_x <= s->width);
167
                    assert((fy>>1) + 16*s->mb_y <= s->height);
168
                    assert((bx>>1) + 16*s->mb_x >= -16);
169
                    assert((by>>1) + 16*s->mb_y >= -16);
170
                    assert((bx>>1) + 16*s->mb_x <= s->width);
171
                    assert((by>>1) + 16*s->mb_y <= s->height);
172

    
173
                    c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
174
                    c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
175
                }
176
            }
177
            d = cmp_func(s, c->temp, src[0], stride, 16);
178
        }else
179
            d= 256*256*256*32;
180
    }else{
181
        int uvdxy;              /* no, it might not be used uninitialized */
182
        if(dxy){
183
            if(qpel){
184
                c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
185
                if(chroma){
186
                    int cx= hx/2;
187
                    int cy= hy/2;
188
                    cx= (cx>>1)|(cx&1);
189
                    cy= (cy>>1)|(cy&1);
190
                    uvdxy= (cx&1) + 2*(cy&1);
191
                    //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264
192
                }
193
            }else{
194
                c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
195
                if(chroma)
196
                    uvdxy= dxy | (x&1) | (2*(y&1));
197
            }
198
            d = cmp_func(s, c->temp, src[0], stride, h);
199
        }else{
200
            d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
201
            if(chroma)
202
                uvdxy= (x&1) + 2*(y&1);
203
        }
204
        if(chroma){
205
            uint8_t * const uvtemp= c->temp + 16*stride;
206
            c->hpel_put[size+1][uvdxy](uvtemp  , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
207
            c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
208
            d += chroma_cmp_func(s, uvtemp  , src[1], uvstride, h>>1);
209
            d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
210
        }
211
    }
212
#if 0
213
    if(full_pel){
214
        const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);
215
        score_map[index]= d;
216
    }
217

218
    d += (c->mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor;
219
#endif
220
    return d;
221
}
222

    
223
#include "motion_est_template.c"
224

    
225
static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
226
    return 0;
227
}
228

    
229
static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){
230
}
231

    
232
void ff_init_me(MpegEncContext *s){
233
    MotionEstContext * const c= &s->me;
234
    int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
235
    int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
236
    c->avctx= s->avctx;
237

    
238
    if(cache_size < 2*dia_size && !c->stride){
239
        av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
240
    }
241

    
242
    ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
243
    ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
244
    ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
245
    ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
246

    
247
    c->flags    = get_flags(c, 0, c->avctx->me_cmp    &FF_CMP_CHROMA);
248
    c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
249
    c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp    &FF_CMP_CHROMA);
250

    
251
/*FIXME s->no_rounding b_type*/
252
    if(s->flags&CODEC_FLAG_QPEL){
253
        c->sub_motion_search= qpel_motion_search;
254
        c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
255
        if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
256
        else               c->qpel_put= s->dsp.put_qpel_pixels_tab;
257
    }else{
258
        if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
259
            c->sub_motion_search= hpel_motion_search;
260
        else if(   c->avctx->me_sub_cmp == FF_CMP_SAD
261
                && c->avctx->    me_cmp == FF_CMP_SAD
262
                && c->avctx->    mb_cmp == FF_CMP_SAD)
263
            c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
264
        else
265
            c->sub_motion_search= hpel_motion_search;
266
    }
267
    c->hpel_avg= s->dsp.avg_pixels_tab;
268
    if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
269
    else               c->hpel_put= s->dsp.put_pixels_tab;
270

    
271
    if(s->linesize){
272
        c->stride  = s->linesize;
273
        c->uvstride= s->uvlinesize;
274
    }else{
275
        c->stride  = 16*s->mb_width + 32;
276
        c->uvstride=  8*s->mb_width + 16;
277
    }
278

    
279
    // 8x8 fullpel search would need a 4x4 chroma compare, which we dont have yet, and even if we had the motion estimation code doesnt expect it
280
    if(s->codec_id != CODEC_ID_SNOW){
281
        if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
282
            s->dsp.me_cmp[2]= zero_cmp;
283
        }
284
        if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
285
            s->dsp.me_sub_cmp[2]= zero_cmp;
286
        }
287
        c->hpel_put[2][0]= c->hpel_put[2][1]=
288
        c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
289
    }
290

    
291
    if(s->codec_id == CODEC_ID_H261){
292
        c->sub_motion_search= no_sub_motion_search;
293
    }
294

    
295
    c->temp= c->scratchpad;
296
}
297

    
298
#if 0
299
static int pix_dev(uint8_t * pix, int line_size, int mean)
300
{
301
    int s, i, j;
302

303
    s = 0;
304
    for (i = 0; i < 16; i++) {
305
        for (j = 0; j < 16; j += 8) {
306
            s += FFABS(pix[0]-mean);
307
            s += FFABS(pix[1]-mean);
308
            s += FFABS(pix[2]-mean);
309
            s += FFABS(pix[3]-mean);
310
            s += FFABS(pix[4]-mean);
311
            s += FFABS(pix[5]-mean);
312
            s += FFABS(pix[6]-mean);
313
            s += FFABS(pix[7]-mean);
314
            pix += 8;
315
        }
316
        pix += line_size - 16;
317
    }
318
    return s;
319
}
320
#endif
321

    
322
static inline void no_motion_search(MpegEncContext * s,
323
                                    int *mx_ptr, int *my_ptr)
324
{
325
    *mx_ptr = 16 * s->mb_x;
326
    *my_ptr = 16 * s->mb_y;
327
}
328

    
329
#if 0  /* the use of these functions is inside #if 0 */
330
static int full_motion_search(MpegEncContext * s,
331
                              int *mx_ptr, int *my_ptr, int range,
332
                              int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
333
{
334
    int x1, y1, x2, y2, xx, yy, x, y;
335
    int mx, my, dmin, d;
336
    uint8_t *pix;
337

338
    xx = 16 * s->mb_x;
339
    yy = 16 * s->mb_y;
340
    x1 = xx - range + 1;        /* we loose one pixel to avoid boundary pb with half pixel pred */
341
    if (x1 < xmin)
342
        x1 = xmin;
343
    x2 = xx + range - 1;
344
    if (x2 > xmax)
345
        x2 = xmax;
346
    y1 = yy - range + 1;
347
    if (y1 < ymin)
348
        y1 = ymin;
349
    y2 = yy + range - 1;
350
    if (y2 > ymax)
351
        y2 = ymax;
352
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
353
    dmin = 0x7fffffff;
354
    mx = 0;
355
    my = 0;
356
    for (y = y1; y <= y2; y++) {
357
        for (x = x1; x <= x2; x++) {
358
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x,
359
                             s->linesize, 16);
360
            if (d < dmin ||
361
                (d == dmin &&
362
                 (abs(x - xx) + abs(y - yy)) <
363
                 (abs(mx - xx) + abs(my - yy)))) {
364
                dmin = d;
365
                mx = x;
366
                my = y;
367
            }
368
        }
369
    }
370

371
    *mx_ptr = mx;
372
    *my_ptr = my;
373

374
#if 0
375
    if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
376
        *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
377
        av_log(NULL, AV_LOG_ERROR, "error %d %d\n", *mx_ptr, *my_ptr);
378
    }
379
#endif
380
    return dmin;
381
}
382

    
383

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

    
392
    xx = s->mb_x << 4;
393
    yy = s->mb_y << 4;
394

    
395
    /* Left limit */
396
    x1 = xx - range;
397
    if (x1 < xmin)
398
        x1 = xmin;
399

    
400
    /* Right limit */
401
    x2 = xx + range;
402
    if (x2 > xmax)
403
        x2 = xmax;
404

    
405
    /* Upper limit */
406
    y1 = yy - range;
407
    if (y1 < ymin)
408
        y1 = ymin;
409

    
410
    /* Lower limit */
411
    y2 = yy + range;
412
    if (y2 > ymax)
413
        y2 = ymax;
414

    
415
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
416
    dmin = 0x7fffffff;
417
    mx = 0;
418
    my = 0;
419

    
420
    do {
421
        for (y = y1; y <= y2; y += range) {
422
            for (x = x1; x <= x2; x += range) {
423
                d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
424
                if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
425
                    dmin = d;
426
                    mx = x;
427
                    my = y;
428
                }
429
            }
430
        }
431

    
432
        range = range >> 1;
433

    
434
        x1 = mx - range;
435
        if (x1 < xmin)
436
            x1 = xmin;
437

    
438
        x2 = mx + range;
439
        if (x2 > xmax)
440
            x2 = xmax;
441

    
442
        y1 = my - range;
443
        if (y1 < ymin)
444
            y1 = ymin;
445

    
446
        y2 = my + range;
447
        if (y2 > ymax)
448
            y2 = ymax;
449

    
450
    } while (range >= 1);
451

    
452
#ifdef DEBUG
453
    av_log(s->avctx, AV_LOG_DEBUG, "log       - MX: %d\tMY: %d\n", mx, my);
454
#endif
455
    *mx_ptr = mx;
456
    *my_ptr = my;
457
    return dmin;
458
}
459

    
460
static int phods_motion_search(MpegEncContext * s,
461
                               int *mx_ptr, int *my_ptr, int range,
462
                               int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
463
{
464
    int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
465
    int mx, my, dminx, dminy;
466
    uint8_t *pix;
467

    
468
    xx = s->mb_x << 4;
469
    yy = s->mb_y << 4;
470

    
471
    /* Left limit */
472
    x1 = xx - range;
473
    if (x1 < xmin)
474
        x1 = xmin;
475

    
476
    /* Right limit */
477
    x2 = xx + range;
478
    if (x2 > xmax)
479
        x2 = xmax;
480

    
481
    /* Upper limit */
482
    y1 = yy - range;
483
    if (y1 < ymin)
484
        y1 = ymin;
485

    
486
    /* Lower limit */
487
    y2 = yy + range;
488
    if (y2 > ymax)
489
        y2 = ymax;
490

    
491
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
492
    mx = 0;
493
    my = 0;
494

    
495
    x = xx;
496
    y = yy;
497
    do {
498
        dminx = 0x7fffffff;
499
        dminy = 0x7fffffff;
500

    
501
        lastx = x;
502
        for (x = x1; x <= x2; x += range) {
503
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
504
            if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
505
                dminx = d;
506
                mx = x;
507
            }
508
        }
509

    
510
        x = lastx;
511
        for (y = y1; y <= y2; y += range) {
512
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
513
            if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
514
                dminy = d;
515
                my = y;
516
            }
517
        }
518

    
519
        range = range >> 1;
520

    
521
        x = mx;
522
        y = my;
523
        x1 = mx - range;
524
        if (x1 < xmin)
525
            x1 = xmin;
526

    
527
        x2 = mx + range;
528
        if (x2 > xmax)
529
            x2 = xmax;
530

    
531
        y1 = my - range;
532
        if (y1 < ymin)
533
            y1 = ymin;
534

    
535
        y2 = my + range;
536
        if (y2 > ymax)
537
            y2 = ymax;
538

    
539
    } while (range >= 1);
540

    
541
#ifdef DEBUG
542
    av_log(s->avctx, AV_LOG_DEBUG, "phods     - MX: %d\tMY: %d\n", mx, my);
543
#endif
544

    
545
    /* half pixel search */
546
    *mx_ptr = mx;
547
    *my_ptr = my;
548
    return dminy;
549
}
550
#endif /* 0 */
551

    
552
#define Z_THRESHOLD 256
553

    
554
#define CHECK_SAD_HALF_MV(suffix, x, y) \
555
{\
556
    d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
557
    d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
558
    COPY3_IF_LT(dminh, d, dx, x, dy, y)\
559
}
560

    
561
static inline int sad_hpel_motion_search(MpegEncContext * s,
562
                                  int *mx_ptr, int *my_ptr, int dmin,
563
                                  int src_index, int ref_index,
564
                                  int size, int h)
565
{
566
    MotionEstContext * const c= &s->me;
567
    const int penalty_factor= c->sub_penalty_factor;
568
    int mx, my, dminh;
569
    uint8_t *pix, *ptr;
570
    int stride= c->stride;
571
    const int flags= c->sub_flags;
572
    LOAD_COMMON
573

    
574
    assert(flags == 0);
575

    
576
    if(c->skip){
577
//    printf("S");
578
        *mx_ptr = 0;
579
        *my_ptr = 0;
580
        return dmin;
581
    }
582
//    printf("N");
583

    
584
    pix = c->src[src_index][0];
585

    
586
    mx = *mx_ptr;
587
    my = *my_ptr;
588
    ptr = c->ref[ref_index][0] + (my * stride) + mx;
589

    
590
    dminh = dmin;
591

    
592
    if (mx > xmin && mx < xmax &&
593
        my > ymin && my < ymax) {
594
        int dx=0, dy=0;
595
        int d, pen_x, pen_y;
596
        const int index= (my<<ME_MAP_SHIFT) + mx;
597
        const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
598
        const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)];
599
        const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)];
600
        const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
601
        mx<<=1;
602
        my<<=1;
603

    
604

    
605
        pen_x= pred_x + mx;
606
        pen_y= pred_y + my;
607

    
608
        ptr-= stride;
609
        if(t<=b){
610
            CHECK_SAD_HALF_MV(y2 , 0, -1)
611
            if(l<=r){
612
                CHECK_SAD_HALF_MV(xy2, -1, -1)
613
                if(t+r<=b+l){
614
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
615
                    ptr+= stride;
616
                }else{
617
                    ptr+= stride;
618
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
619
                }
620
                CHECK_SAD_HALF_MV(x2 , -1,  0)
621
            }else{
622
                CHECK_SAD_HALF_MV(xy2, +1, -1)
623
                if(t+l<=b+r){
624
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
625
                    ptr+= stride;
626
                }else{
627
                    ptr+= stride;
628
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
629
                }
630
                CHECK_SAD_HALF_MV(x2 , +1,  0)
631
            }
632
        }else{
633
            if(l<=r){
634
                if(t+l<=b+r){
635
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
636
                    ptr+= stride;
637
                }else{
638
                    ptr+= stride;
639
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
640
                }
641
                CHECK_SAD_HALF_MV(x2 , -1,  0)
642
                CHECK_SAD_HALF_MV(xy2, -1, +1)
643
            }else{
644
                if(t+r<=b+l){
645
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
646
                    ptr+= stride;
647
                }else{
648
                    ptr+= stride;
649
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
650
                }
651
                CHECK_SAD_HALF_MV(x2 , +1,  0)
652
                CHECK_SAD_HALF_MV(xy2, +1, +1)
653
            }
654
            CHECK_SAD_HALF_MV(y2 ,  0, +1)
655
        }
656
        mx+=dx;
657
        my+=dy;
658

    
659
    }else{
660
        mx<<=1;
661
        my<<=1;
662
    }
663

    
664
    *mx_ptr = mx;
665
    *my_ptr = my;
666
    return dminh;
667
}
668

    
669
static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
670
{
671
    const int xy= s->mb_x + s->mb_y*s->mb_stride;
672

    
673
    s->p_mv_table[xy][0] = mx;
674
    s->p_mv_table[xy][1] = my;
675

    
676
    /* has already been set to the 4 MV if 4MV is done */
677
    if(mv4){
678
        int mot_xy= s->block_index[0];
679

    
680
        s->current_picture.motion_val[0][mot_xy  ][0]= mx;
681
        s->current_picture.motion_val[0][mot_xy  ][1]= my;
682
        s->current_picture.motion_val[0][mot_xy+1][0]= mx;
683
        s->current_picture.motion_val[0][mot_xy+1][1]= my;
684

    
685
        mot_xy += s->b8_stride;
686
        s->current_picture.motion_val[0][mot_xy  ][0]= mx;
687
        s->current_picture.motion_val[0][mot_xy  ][1]= my;
688
        s->current_picture.motion_val[0][mot_xy+1][0]= mx;
689
        s->current_picture.motion_val[0][mot_xy+1][1]= my;
690
    }
691
}
692

    
693
/**
694
 * get fullpel ME search limits.
695
 */
696
static inline void get_limits(MpegEncContext *s, int x, int y)
697
{
698
    MotionEstContext * const c= &s->me;
699
    int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
700
/*
701
    if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
702
    else                   c->range= 16;
703
*/
704
    if (s->unrestricted_mv) {
705
        c->xmin = - x - 16;
706
        c->ymin = - y - 16;
707
        c->xmax = - x + s->mb_width *16;
708
        c->ymax = - y + s->mb_height*16;
709
    } else if (s->out_format == FMT_H261){
710
        // Search range of H261 is different from other codec standards
711
        c->xmin = (x > 15) ? - 15 : 0;
712
        c->ymin = (y > 15) ? - 15 : 0;
713
        c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
714
        c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
715
    } else {
716
        c->xmin = - x;
717
        c->ymin = - y;
718
        c->xmax = - x + s->mb_width *16 - 16;
719
        c->ymax = - y + s->mb_height*16 - 16;
720
    }
721
    if(range){
722
        c->xmin = FFMAX(c->xmin,-range);
723
        c->xmax = FFMIN(c->xmax, range);
724
        c->ymin = FFMAX(c->ymin,-range);
725
        c->ymax = FFMIN(c->ymax, range);
726
    }
727
}
728

    
729
static inline void init_mv4_ref(MotionEstContext *c){
730
    const int stride= c->stride;
731

    
732
    c->ref[1][0] = c->ref[0][0] + 8;
733
    c->ref[2][0] = c->ref[0][0] + 8*stride;
734
    c->ref[3][0] = c->ref[2][0] + 8;
735
    c->src[1][0] = c->src[0][0] + 8;
736
    c->src[2][0] = c->src[0][0] + 8*stride;
737
    c->src[3][0] = c->src[2][0] + 8;
738
}
739

    
740
static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
741
{
742
    MotionEstContext * const c= &s->me;
743
    const int size= 1;
744
    const int h=8;
745
    int block;
746
    int P[10][2];
747
    int dmin_sum=0, mx4_sum=0, my4_sum=0;
748
    int same=1;
749
    const int stride= c->stride;
750
    uint8_t *mv_penalty= c->current_mv_penalty;
751

    
752
    init_mv4_ref(c);
753

    
754
    for(block=0; block<4; block++){
755
        int mx4, my4;
756
        int pred_x4, pred_y4;
757
        int dmin4;
758
        static const int off[4]= {2, 1, 1, -1};
759
        const int mot_stride = s->b8_stride;
760
        const int mot_xy = s->block_index[block];
761

    
762
        P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
763
        P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
764

    
765
        if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
766

    
767
        /* special case for first line */
768
        if (s->first_slice_line && block<2) {
769
            c->pred_x= pred_x4= P_LEFT[0];
770
            c->pred_y= pred_y4= P_LEFT[1];
771
        } else {
772
            P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride             ][0];
773
            P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride             ][1];
774
            P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
775
            P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
776
            if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
777
            if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
778
            if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
779
            if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
780

    
781
            P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
782
            P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
783

    
784
            c->pred_x= pred_x4 = P_MEDIAN[0];
785
            c->pred_y= pred_y4 = P_MEDIAN[1];
786
        }
787
        P_MV1[0]= mx;
788
        P_MV1[1]= my;
789

    
790
        dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
791

    
792
        dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
793

    
794
        if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
795
            int dxy;
796
            const int offset= ((block&1) + (block>>1)*stride)*8;
797
            uint8_t *dest_y = c->scratchpad + offset;
798
            if(s->quarter_sample){
799
                uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
800
                dxy = ((my4 & 3) << 2) | (mx4 & 3);
801

    
802
                if(s->no_rounding)
803
                    s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y   , ref    , stride);
804
                else
805
                    s->dsp.put_qpel_pixels_tab       [1][dxy](dest_y   , ref    , stride);
806
            }else{
807
                uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
808
                dxy = ((my4 & 1) << 1) | (mx4 & 1);
809

    
810
                if(s->no_rounding)
811
                    s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y    , ref    , stride, h);
812
                else
813
                    s->dsp.put_pixels_tab       [1][dxy](dest_y    , ref    , stride, h);
814
            }
815
            dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
816
        }else
817
            dmin_sum+= dmin4;
818

    
819
        if(s->quarter_sample){
820
            mx4_sum+= mx4/2;
821
            my4_sum+= my4/2;
822
        }else{
823
            mx4_sum+= mx4;
824
            my4_sum+= my4;
825
        }
826

    
827
        s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4;
828
        s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4;
829

    
830
        if(mx4 != mx || my4 != my) same=0;
831
    }
832

    
833
    if(same)
834
        return INT_MAX;
835

    
836
    if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
837
        dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, c->scratchpad, stride, 16);
838
    }
839

    
840
    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
841
        int dxy;
842
        int mx, my;
843
        int offset;
844

    
845
        mx= ff_h263_round_chroma(mx4_sum);
846
        my= ff_h263_round_chroma(my4_sum);
847
        dxy = ((my & 1) << 1) | (mx & 1);
848

    
849
        offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
850

    
851
        if(s->no_rounding){
852
            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
853
            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
854
        }else{
855
            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
856
            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
857
        }
858

    
859
        dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad  , s->uvlinesize, 8);
860
        dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad+8, s->uvlinesize, 8);
861
    }
862

    
863
    c->pred_x= mx;
864
    c->pred_y= my;
865

    
866
    switch(c->avctx->mb_cmp&0xFF){
867
    /*case FF_CMP_SSE:
868
        return dmin_sum+ 32*s->qscale*s->qscale;*/
869
    case FF_CMP_RD:
870
        return dmin_sum;
871
    default:
872
        return dmin_sum+ 11*c->mb_penalty_factor;
873
    }
874
}
875

    
876
static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
877
    MotionEstContext * const c= &s->me;
878

    
879
    c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
880
    c->src[1][0] = c->src[0][0] + s->linesize;
881
    if(c->flags & FLAG_CHROMA){
882
        c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
883
        c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
884
        c->src[1][1] = c->src[0][1] + s->uvlinesize;
885
        c->src[1][2] = c->src[0][2] + s->uvlinesize;
886
    }
887
}
888

    
889
static int interlaced_search(MpegEncContext *s, int ref_index,
890
                             int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
891
{
892
    MotionEstContext * const c= &s->me;
893
    const int size=0;
894
    const int h=8;
895
    int block;
896
    int P[10][2];
897
    uint8_t * const mv_penalty= c->current_mv_penalty;
898
    int same=1;
899
    const int stride= 2*s->linesize;
900
    int dmin_sum= 0;
901
    const int mot_stride= s->mb_stride;
902
    const int xy= s->mb_x + s->mb_y*mot_stride;
903

    
904
    c->ymin>>=1;
905
    c->ymax>>=1;
906
    c->stride<<=1;
907
    c->uvstride<<=1;
908
    init_interlaced_ref(s, ref_index);
909

    
910
    for(block=0; block<2; block++){
911
        int field_select;
912
        int best_dmin= INT_MAX;
913
        int best_field= -1;
914

    
915
        for(field_select=0; field_select<2; field_select++){
916
            int dmin, mx_i, my_i;
917
            int16_t (*mv_table)[2]= mv_tables[block][field_select];
918

    
919
            if(user_field_select){
920
                assert(field_select==0 || field_select==1);
921
                assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
922
                if(field_select_tables[block][xy] != field_select)
923
                    continue;
924
            }
925

    
926
            P_LEFT[0] = mv_table[xy - 1][0];
927
            P_LEFT[1] = mv_table[xy - 1][1];
928
            if(P_LEFT[0]       > (c->xmax<<1)) P_LEFT[0]       = (c->xmax<<1);
929

    
930
            c->pred_x= P_LEFT[0];
931
            c->pred_y= P_LEFT[1];
932

    
933
            if(!s->first_slice_line){
934
                P_TOP[0]      = mv_table[xy - mot_stride][0];
935
                P_TOP[1]      = mv_table[xy - mot_stride][1];
936
                P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
937
                P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
938
                if(P_TOP[1]      > (c->ymax<<1)) P_TOP[1]     = (c->ymax<<1);
939
                if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
940
                if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
941
                if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
942

    
943
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
944
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
945
            }
946
            P_MV1[0]= mx; //FIXME not correct if block != field_select
947
            P_MV1[1]= my / 2;
948

    
949
            dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
950

    
951
            dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
952

    
953
            mv_table[xy][0]= mx_i;
954
            mv_table[xy][1]= my_i;
955

    
956
            if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
957
                int dxy;
958

    
959
                //FIXME chroma ME
960
                uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
961
                dxy = ((my_i & 1) << 1) | (mx_i & 1);
962

    
963
                if(s->no_rounding){
964
                    s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref    , stride, h);
965
                }else{
966
                    s->dsp.put_pixels_tab       [size][dxy](c->scratchpad, ref    , stride, h);
967
                }
968
                dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
969
                dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
970
            }else
971
                dmin+= c->mb_penalty_factor; //field_select bits
972

    
973
            dmin += field_select != block; //slightly prefer same field
974

    
975
            if(dmin < best_dmin){
976
                best_dmin= dmin;
977
                best_field= field_select;
978
            }
979
        }
980
        {
981
            int16_t (*mv_table)[2]= mv_tables[block][best_field];
982

    
983
            if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
984
            if(mv_table[xy][1]&1) same=0;
985
            if(mv_table[xy][1]*2 != my) same=0;
986
            if(best_field != block) same=0;
987
        }
988

    
989
        field_select_tables[block][xy]= best_field;
990
        dmin_sum += best_dmin;
991
    }
992

    
993
    c->ymin<<=1;
994
    c->ymax<<=1;
995
    c->stride>>=1;
996
    c->uvstride>>=1;
997

    
998
    if(same)
999
        return INT_MAX;
1000

    
1001
    switch(c->avctx->mb_cmp&0xFF){
1002
    /*case FF_CMP_SSE:
1003
        return dmin_sum+ 32*s->qscale*s->qscale;*/
1004
    case FF_CMP_RD:
1005
        return dmin_sum;
1006
    default:
1007
        return dmin_sum+ 11*c->mb_penalty_factor;
1008
    }
1009
}
1010

    
1011
static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
1012
    int ymax= s->me.ymax>>interlaced;
1013
    int ymin= s->me.ymin>>interlaced;
1014

    
1015
    if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
1016
    if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
1017
    if(mv[1] <       ymin) mv[1] =       ymin;
1018
    if(mv[1] >       ymax) mv[1] =       ymax;
1019
}
1020

    
1021
static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
1022
    MotionEstContext * const c= &s->me;
1023
    Picture *p= s->current_picture_ptr;
1024
    int mb_xy= mb_x + mb_y*s->mb_stride;
1025
    int xy= 2*mb_x + 2*mb_y*s->b8_stride;
1026
    int mb_type= s->current_picture.mb_type[mb_xy];
1027
    int flags= c->flags;
1028
    int shift= (flags&FLAG_QPEL) + 1;
1029
    int mask= (1<<shift)-1;
1030
    int x, y, i;
1031
    int d=0;
1032
    me_cmp_func cmpf= s->dsp.sse[0];
1033
    me_cmp_func chroma_cmpf= s->dsp.sse[1];
1034

    
1035
    if(p_type && USES_LIST(mb_type, 1)){
1036
        av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
1037
        return INT_MAX/2;
1038
    }
1039
    assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
1040

    
1041
    for(i=0; i<4; i++){
1042
        int xy= s->block_index[i];
1043
        clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type));
1044
        clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type));
1045
    }
1046

    
1047
    if(IS_INTERLACED(mb_type)){
1048
        int xy2= xy  + s->b8_stride;
1049
        s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
1050
        c->stride<<=1;
1051
        c->uvstride<<=1;
1052

    
1053
        if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
1054
            av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
1055
            return INT_MAX/2;
1056
        }
1057

    
1058
        if(USES_LIST(mb_type, 0)){
1059
            int field_select0= p->ref_index[0][xy ];
1060
            int field_select1= p->ref_index[0][xy2];
1061
            assert(field_select0==0 ||field_select0==1);
1062
            assert(field_select1==0 ||field_select1==1);
1063
            init_interlaced_ref(s, 0);
1064

    
1065
            if(p_type){
1066
                s->p_field_select_table[0][mb_xy]= field_select0;
1067
                s->p_field_select_table[1][mb_xy]= field_select1;
1068
                *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
1069
                *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
1070
                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
1071
            }else{
1072
                s->b_field_select_table[0][0][mb_xy]= field_select0;
1073
                s->b_field_select_table[0][1][mb_xy]= field_select1;
1074
                *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
1075
                *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
1076
                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
1077
            }
1078

    
1079
            x= p->motion_val[0][xy ][0];
1080
            y= p->motion_val[0][xy ][1];
1081
            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
1082
            x= p->motion_val[0][xy2][0];
1083
            y= p->motion_val[0][xy2][1];
1084
            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
1085
        }
1086
        if(USES_LIST(mb_type, 1)){
1087
            int field_select0= p->ref_index[1][xy ];
1088
            int field_select1= p->ref_index[1][xy2];
1089
            assert(field_select0==0 ||field_select0==1);
1090
            assert(field_select1==0 ||field_select1==1);
1091
            init_interlaced_ref(s, 2);
1092

    
1093
            s->b_field_select_table[1][0][mb_xy]= field_select0;
1094
            s->b_field_select_table[1][1][mb_xy]= field_select1;
1095
            *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ];
1096
            *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[1][xy2];
1097
            if(USES_LIST(mb_type, 0)){
1098
                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
1099
            }else{
1100
                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
1101
            }
1102

    
1103
            x= p->motion_val[1][xy ][0];
1104
            y= p->motion_val[1][xy ][1];
1105
            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
1106
            x= p->motion_val[1][xy2][0];
1107
            y= p->motion_val[1][xy2][1];
1108
            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
1109
            //FIXME bidir scores
1110
        }
1111
        c->stride>>=1;
1112
        c->uvstride>>=1;
1113
    }else if(IS_8X8(mb_type)){
1114
        if(!(s->flags & CODEC_FLAG_4MV)){
1115
            av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
1116
            return INT_MAX/2;
1117
        }
1118
        cmpf= s->dsp.sse[1];
1119
        chroma_cmpf= s->dsp.sse[1];
1120
        init_mv4_ref(c);
1121
        for(i=0; i<4; i++){
1122
            xy= s->block_index[i];
1123
            x= p->motion_val[0][xy][0];
1124
            y= p->motion_val[0][xy][1];
1125
            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
1126
        }
1127
        s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
1128
    }else{
1129
        if(USES_LIST(mb_type, 0)){
1130
            if(p_type){
1131
                *(uint32_t*)s->p_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
1132
                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
1133
            }else if(USES_LIST(mb_type, 1)){
1134
                *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
1135
                *(uint32_t*)s->b_bidir_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
1136
                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
1137
            }else{
1138
                *(uint32_t*)s->b_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
1139
                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
1140
            }
1141
            x= p->motion_val[0][xy][0];
1142
            y= p->motion_val[0][xy][1];
1143
            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
1144
        }else if(USES_LIST(mb_type, 1)){
1145
            *(uint32_t*)s->b_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
1146
            s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
1147

    
1148
            x= p->motion_val[1][xy][0];
1149
            y= p->motion_val[1][xy][1];
1150
            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
1151
        }else
1152
            s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
1153
    }
1154
    return d;
1155
}
1156

    
1157
void ff_estimate_p_frame_motion(MpegEncContext * s,
1158
                                int mb_x, int mb_y)
1159
{
1160
    MotionEstContext * const c= &s->me;
1161
    uint8_t *pix, *ppix;
1162
    int sum, mx, my, dmin;
1163
    int varc;            ///< the variance of the block (sum of squared (p[y][x]-average))
1164
    int vard;            ///< sum of squared differences with the estimated motion vector
1165
    int P[10][2];
1166
    const int shift= 1+s->quarter_sample;
1167
    int mb_type=0;
1168
    Picture * const pic= &s->current_picture;
1169

    
1170
    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
1171

    
1172
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1173
    assert(s->linesize == c->stride);
1174
    assert(s->uvlinesize == c->uvstride);
1175

    
1176
    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1177
    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1178
    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1179
    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1180

    
1181
    get_limits(s, 16*mb_x, 16*mb_y);
1182
    c->skip=0;
1183

    
1184
    /* intra / predictive decision */
1185
    pix = c->src[0][0];
1186
    sum = s->dsp.pix_sum(pix, s->linesize);
1187
    varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
1188

    
1189
    pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1190
    pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
1191
    c->mb_var_sum_temp += (varc+128)>>8;
1192

    
1193
    if(c->avctx->me_threshold){
1194
        vard= check_input_motion(s, mb_x, mb_y, 1);
1195

    
1196
        if((vard+128)>>8 < c->avctx->me_threshold){
1197
            int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1198
            int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1199
            pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
1200
            c->mc_mb_var_sum_temp += (vard+128)>>8;
1201
            c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1202
            return;
1203
        }
1204
        if((vard+128)>>8 < c->avctx->mb_threshold)
1205
            mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
1206
    }
1207

    
1208
    switch(s->me_method) {
1209
    case ME_ZERO:
1210
    default:
1211
        no_motion_search(s, &mx, &my);
1212
        mx-= mb_x*16;
1213
        my-= mb_y*16;
1214
        dmin = 0;
1215
        break;
1216
#if 0
1217
    case ME_FULL:
1218
        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
1219
        mx-= mb_x*16;
1220
        my-= mb_y*16;
1221
        break;
1222
    case ME_LOG:
1223
        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
1224
        mx-= mb_x*16;
1225
        my-= mb_y*16;
1226
        break;
1227
    case ME_PHODS:
1228
        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
1229
        mx-= mb_x*16;
1230
        my-= mb_y*16;
1231
        break;
1232
#endif
1233
    case ME_X1:
1234
    case ME_EPZS:
1235
       {
1236
            const int mot_stride = s->b8_stride;
1237
            const int mot_xy = s->block_index[0];
1238

    
1239
            P_LEFT[0]       = s->current_picture.motion_val[0][mot_xy - 1][0];
1240
            P_LEFT[1]       = s->current_picture.motion_val[0][mot_xy - 1][1];
1241

    
1242
            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
1243

    
1244
            if(!s->first_slice_line) {
1245
                P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];
1246
                P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];
1247
                P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
1248
                P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
1249
                if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
1250
                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
1251
                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
1252

    
1253
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1254
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1255

    
1256
                if(s->out_format == FMT_H263){
1257
                    c->pred_x = P_MEDIAN[0];
1258
                    c->pred_y = P_MEDIAN[1];
1259
                }else { /* mpeg1 at least */
1260
                    c->pred_x= P_LEFT[0];
1261
                    c->pred_y= P_LEFT[1];
1262
                }
1263
            }else{
1264
                c->pred_x= P_LEFT[0];
1265
                c->pred_y= P_LEFT[1];
1266
            }
1267

    
1268
        }
1269
        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
1270

    
1271
        break;
1272
    }
1273

    
1274
    /* At this point (mx,my) are full-pell and the relative displacement */
1275
    ppix = c->ref[0][0] + (my * s->linesize) + mx;
1276

    
1277
    vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
1278

    
1279
    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
1280
//    pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
1281
    c->mc_mb_var_sum_temp += (vard+128)>>8;
1282

    
1283
#if 0
1284
    printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
1285
           varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
1286
#endif
1287
    if(mb_type){
1288
        int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1289
        int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1290
        c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1291

    
1292
        if(mb_type == CANDIDATE_MB_TYPE_INTER){
1293
            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1294
            set_p_mv_tables(s, mx, my, 1);
1295
        }else{
1296
            mx <<=shift;
1297
            my <<=shift;
1298
        }
1299
        if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
1300
            h263_mv4_search(s, mx, my, shift);
1301

    
1302
            set_p_mv_tables(s, mx, my, 0);
1303
        }
1304
        if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
1305
            interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
1306
        }
1307
    }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1308
        int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1309
        int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1310
        c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1311

    
1312
        if (vard*2 + 200*256 > varc)
1313
            mb_type|= CANDIDATE_MB_TYPE_INTRA;
1314
        if (varc*2 + 200*256 > vard || s->qscale > 24){
1315
//        if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
1316
            mb_type|= CANDIDATE_MB_TYPE_INTER;
1317
            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1318
            if(s->flags&CODEC_FLAG_MV0)
1319
                if(mx || my)
1320
                    mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
1321
        }else{
1322
            mx <<=shift;
1323
            my <<=shift;
1324
        }
1325
        if((s->flags&CODEC_FLAG_4MV)
1326
           && !c->skip && varc>50<<8 && vard>10<<8){
1327
            if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
1328
                mb_type|=CANDIDATE_MB_TYPE_INTER4V;
1329

    
1330
            set_p_mv_tables(s, mx, my, 0);
1331
        }else
1332
            set_p_mv_tables(s, mx, my, 1);
1333
        if((s->flags&CODEC_FLAG_INTERLACED_ME)
1334
           && !c->skip){ //FIXME varc/d checks
1335
            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
1336
                mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1337
        }
1338
    }else{
1339
        int intra_score, i;
1340
        mb_type= CANDIDATE_MB_TYPE_INTER;
1341

    
1342
        dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1343
        if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1344
            dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1345

    
1346
        if((s->flags&CODEC_FLAG_4MV)
1347
           && !c->skip && varc>50<<8 && vard>10<<8){
1348
            int dmin4= h263_mv4_search(s, mx, my, shift);
1349
            if(dmin4 < dmin){
1350
                mb_type= CANDIDATE_MB_TYPE_INTER4V;
1351
                dmin=dmin4;
1352
            }
1353
        }
1354
        if((s->flags&CODEC_FLAG_INTERLACED_ME)
1355
           && !c->skip){ //FIXME varc/d checks
1356
            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
1357
            if(dmin_i < dmin){
1358
                mb_type = CANDIDATE_MB_TYPE_INTER_I;
1359
                dmin= dmin_i;
1360
            }
1361
        }
1362

    
1363
//        pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
1364
        set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1365

    
1366
        /* get intra luma score */
1367
        if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1368
            intra_score= varc - 500;
1369
        }else{
1370
            int mean= (sum+128)>>8;
1371
            mean*= 0x01010101;
1372

    
1373
            for(i=0; i<16; i++){
1374
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
1375
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
1376
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
1377
                *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
1378
            }
1379

    
1380
            intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
1381
        }
1382
#if 0 //FIXME
1383
        /* get chroma score */
1384
        if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1385
            for(i=1; i<3; i++){
1386
                uint8_t *dest_c;
1387
                int mean;
1388

1389
                if(s->out_format == FMT_H263){
1390
                    mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;)
1391
                }else{
1392
                    mean= (s->last_dc[i] + 4)>>3;
1393
                }
1394
                dest_c = s->new_picture.data[i] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
1395

1396
                mean*= 0x01010101;
1397
                for(i=0; i<8; i++){
1398
                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean;
1399
                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean;
1400
                }
1401

1402
                intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize);
1403
            }
1404
        }
1405
#endif
1406
        intra_score += c->mb_penalty_factor*16;
1407

    
1408
        if(intra_score < dmin){
1409
            mb_type= CANDIDATE_MB_TYPE_INTRA;
1410
            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
1411
        }else
1412
            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;
1413

    
1414
        {
1415
            int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1416
            int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1417
            c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1418
        }
1419
    }
1420

    
1421
    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
1422
}
1423

    
1424
int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
1425
                                    int mb_x, int mb_y)
1426
{
1427
    MotionEstContext * const c= &s->me;
1428
    int mx, my, dmin;
1429
    int P[10][2];
1430
    const int shift= 1+s->quarter_sample;
1431
    const int xy= mb_x + mb_y*s->mb_stride;
1432
    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
1433

    
1434
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1435

    
1436
    c->pre_penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
1437
    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1438

    
1439
    get_limits(s, 16*mb_x, 16*mb_y);
1440
    c->skip=0;
1441

    
1442
    P_LEFT[0]       = s->p_mv_table[xy + 1][0];
1443
    P_LEFT[1]       = s->p_mv_table[xy + 1][1];
1444

    
1445
    if(P_LEFT[0]       < (c->xmin<<shift)) P_LEFT[0]       = (c->xmin<<shift);
1446

    
1447
    /* special case for first line */
1448
    if (s->first_slice_line) {
1449
        c->pred_x= P_LEFT[0];
1450
        c->pred_y= P_LEFT[1];
1451
        P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1452
        P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
1453
    } else {
1454
        P_TOP[0]      = s->p_mv_table[xy + s->mb_stride    ][0];
1455
        P_TOP[1]      = s->p_mv_table[xy + s->mb_stride    ][1];
1456
        P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
1457
        P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
1458
        if(P_TOP[1]      < (c->ymin<<shift)) P_TOP[1]     = (c->ymin<<shift);
1459
        if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
1460
        if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
1461

    
1462
        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1463
        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1464

    
1465
        c->pred_x = P_MEDIAN[0];
1466
        c->pred_y = P_MEDIAN[1];
1467
    }
1468

    
1469
    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
1470

    
1471
    s->p_mv_table[xy][0] = mx<<shift;
1472
    s->p_mv_table[xy][1] = my<<shift;
1473

    
1474
    return dmin;
1475
}
1476

    
1477
static int ff_estimate_motion_b(MpegEncContext * s,
1478
                       int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
1479
{
1480
    MotionEstContext * const c= &s->me;
1481
    int mx, my, dmin;
1482
    int P[10][2];
1483
    const int shift= 1+s->quarter_sample;
1484
    const int mot_stride = s->mb_stride;
1485
    const int mot_xy = mb_y*mot_stride + mb_x;
1486
    uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
1487
    int mv_scale;
1488

    
1489
    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1490
    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1491
    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1492
    c->current_mv_penalty= mv_penalty;
1493

    
1494
    get_limits(s, 16*mb_x, 16*mb_y);
1495

    
1496
    switch(s->me_method) {
1497
    case ME_ZERO:
1498
    default:
1499
        no_motion_search(s, &mx, &my);
1500
        dmin = 0;
1501
        mx-= mb_x*16;
1502
        my-= mb_y*16;
1503
        break;
1504
#if 0
1505
    case ME_FULL:
1506
        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
1507
        mx-= mb_x*16;
1508
        my-= mb_y*16;
1509
        break;
1510
    case ME_LOG:
1511
        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
1512
        mx-= mb_x*16;
1513
        my-= mb_y*16;
1514
        break;
1515
    case ME_PHODS:
1516
        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
1517
        mx-= mb_x*16;
1518
        my-= mb_y*16;
1519
        break;
1520
#endif
1521
    case ME_X1:
1522
    case ME_EPZS:
1523
       {
1524
            P_LEFT[0]        = mv_table[mot_xy - 1][0];
1525
            P_LEFT[1]        = mv_table[mot_xy - 1][1];
1526

    
1527
            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
1528

    
1529
            /* special case for first line */
1530
            if (!s->first_slice_line) {
1531
                P_TOP[0] = mv_table[mot_xy - mot_stride             ][0];
1532
                P_TOP[1] = mv_table[mot_xy - mot_stride             ][1];
1533
                P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1         ][0];
1534
                P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1         ][1];
1535
                if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);
1536
                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
1537
                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
1538

    
1539
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1540
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1541
            }
1542
            c->pred_x= P_LEFT[0];
1543
            c->pred_y= P_LEFT[1];
1544
        }
1545

    
1546
        if(mv_table == s->b_forw_mv_table){
1547
            mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
1548
        }else{
1549
            mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
1550
        }
1551

    
1552
        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
1553

    
1554
        break;
1555
    }
1556

    
1557
    dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
1558

    
1559
    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1560
        dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
1561

    
1562
//printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
1563
//    s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1564
    mv_table[mot_xy][0]= mx;
1565
    mv_table[mot_xy][1]= my;
1566

    
1567
    return dmin;
1568
}
1569

    
1570
static inline int check_bidir_mv(MpegEncContext * s,
1571
                   int motion_fx, int motion_fy,
1572
                   int motion_bx, int motion_by,
1573
                   int pred_fx, int pred_fy,
1574
                   int pred_bx, int pred_by,
1575
                   int size, int h)
1576
{
1577
    //FIXME optimize?
1578
    //FIXME better f_code prediction (max mv & distance)
1579
    //FIXME pointers
1580
    MotionEstContext * const c= &s->me;
1581
    uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
1582
    uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
1583
    int stride= c->stride;
1584
    uint8_t *dest_y = c->scratchpad;
1585
    uint8_t *ptr;
1586
    int dxy;
1587
    int src_x, src_y;
1588
    int fbmin;
1589
    uint8_t **src_data= c->src[0];
1590
    uint8_t **ref_data= c->ref[0];
1591
    uint8_t **ref2_data= c->ref[2];
1592

    
1593
    if(s->quarter_sample){
1594
        dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1595
        src_x = motion_fx >> 2;
1596
        src_y = motion_fy >> 2;
1597

    
1598
        ptr = ref_data[0] + (src_y * stride) + src_x;
1599
        s->dsp.put_qpel_pixels_tab[0][dxy](dest_y    , ptr    , stride);
1600

    
1601
        dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1602
        src_x = motion_bx >> 2;
1603
        src_y = motion_by >> 2;
1604

    
1605
        ptr = ref2_data[0] + (src_y * stride) + src_x;
1606
        s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y    , ptr    , stride);
1607
    }else{
1608
        dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1609
        src_x = motion_fx >> 1;
1610
        src_y = motion_fy >> 1;
1611

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

    
1615
        dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1616
        src_x = motion_bx >> 1;
1617
        src_y = motion_by >> 1;
1618

    
1619
        ptr = ref2_data[0] + (src_y * stride) + src_x;
1620
        s->dsp.avg_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
1621
    }
1622

    
1623
    fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
1624
           +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
1625
           + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
1626

    
1627
    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1628
    }
1629
    //FIXME CHROMA !!!
1630

    
1631
    return fbmin;
1632
}
1633

    
1634
/* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1635
static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
1636
{
1637
    MotionEstContext * const c= &s->me;
1638
    const int mot_stride = s->mb_stride;
1639
    const int xy = mb_y *mot_stride + mb_x;
1640
    int fbmin;
1641
    int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1642
    int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1643
    int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1644
    int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1645
    int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1646
    int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1647
    int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1648
    int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1649
    const int flags= c->sub_flags;
1650
    const int qpel= flags&FLAG_QPEL;
1651
    const int shift= 1+qpel;
1652
    const int xmin= c->xmin<<shift;
1653
    const int ymin= c->ymin<<shift;
1654
    const int xmax= c->xmax<<shift;
1655
    const int ymax= c->ymax<<shift;
1656
    uint8_t map[8][8][8][8];
1657

    
1658
    memset(map,0,sizeof(map));
1659
#define BIDIR_MAP(fx,fy,bx,by) \
1660
    map[(motion_fx+fx)&7][(motion_fy+fy)&7][(motion_bx+bx)&7][(motion_by+by)&7]
1661
    BIDIR_MAP(0,0,0,0) = 1;
1662

    
1663
    fbmin= check_bidir_mv(s, motion_fx, motion_fy,
1664
                          motion_bx, motion_by,
1665
                          pred_fx, pred_fy,
1666
                          pred_bx, pred_by,
1667
                          0, 16);
1668

    
1669
    if(s->avctx->bidir_refine){
1670
        int score, end;
1671
#define CHECK_BIDIR(fx,fy,bx,by)\
1672
    if( !BIDIR_MAP(fx,fy,bx,by)\
1673
       &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
1674
       &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
1675
        BIDIR_MAP(fx,fy,bx,by) = 1;\
1676
        score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\
1677
        if(score < fbmin){\
1678
            fbmin= score;\
1679
            motion_fx+=fx;\
1680
            motion_fy+=fy;\
1681
            motion_bx+=bx;\
1682
            motion_by+=by;\
1683
            end=0;\
1684
        }\
1685
    }
1686
#define CHECK_BIDIR2(a,b,c,d)\
1687
CHECK_BIDIR(a,b,c,d)\
1688
CHECK_BIDIR(-(a),-(b),-(c),-(d))
1689

    
1690
#define CHECK_BIDIRR(a,b,c,d)\
1691
CHECK_BIDIR2(a,b,c,d)\
1692
CHECK_BIDIR2(b,c,d,a)\
1693
CHECK_BIDIR2(c,d,a,b)\
1694
CHECK_BIDIR2(d,a,b,c)
1695

    
1696
        do{
1697
            end=1;
1698

    
1699
            CHECK_BIDIRR( 0, 0, 0, 1)
1700
            if(s->avctx->bidir_refine > 1){
1701
                CHECK_BIDIRR( 0, 0, 1, 1)
1702
                CHECK_BIDIR2( 0, 1, 0, 1)
1703
                CHECK_BIDIR2( 1, 0, 1, 0)
1704
                CHECK_BIDIRR( 0, 0,-1, 1)
1705
                CHECK_BIDIR2( 0,-1, 0, 1)
1706
                CHECK_BIDIR2(-1, 0, 1, 0)
1707
                if(s->avctx->bidir_refine > 2){
1708
                    CHECK_BIDIRR( 0, 1, 1, 1)
1709
                    CHECK_BIDIRR( 0,-1, 1, 1)
1710
                    CHECK_BIDIRR( 0, 1,-1, 1)
1711
                    CHECK_BIDIRR( 0, 1, 1,-1)
1712
                    if(s->avctx->bidir_refine > 3){
1713
                        CHECK_BIDIR2( 1, 1, 1, 1)
1714
                        CHECK_BIDIRR( 1, 1, 1,-1)
1715
                        CHECK_BIDIR2( 1, 1,-1,-1)
1716
                        CHECK_BIDIR2( 1,-1,-1, 1)
1717
                        CHECK_BIDIR2( 1,-1, 1,-1)
1718
                    }
1719
                }
1720
            }
1721
        }while(!end);
1722
    }
1723

    
1724
    s->b_bidir_forw_mv_table[xy][0]= motion_fx;
1725
    s->b_bidir_forw_mv_table[xy][1]= motion_fy;
1726
    s->b_bidir_back_mv_table[xy][0]= motion_bx;
1727
    s->b_bidir_back_mv_table[xy][1]= motion_by;
1728

    
1729
    return fbmin;
1730
}
1731

    
1732
static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
1733
{
1734
    MotionEstContext * const c= &s->me;
1735
    int P[10][2];
1736
    const int mot_stride = s->mb_stride;
1737
    const int mot_xy = mb_y*mot_stride + mb_x;
1738
    const int shift= 1+s->quarter_sample;
1739
    int dmin, i;
1740
    const int time_pp= s->pp_time;
1741
    const int time_pb= s->pb_time;
1742
    int mx, my, xmin, xmax, ymin, ymax;
1743
    int16_t (*mv_table)[2]= s->b_direct_mv_table;
1744

    
1745
    c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
1746
    ymin= xmin=(-32)>>shift;
1747
    ymax= xmax=   31>>shift;
1748

    
1749
    if(IS_8X8(s->next_picture.mb_type[mot_xy])){
1750
        s->mv_type= MV_TYPE_8X8;
1751
    }else{
1752
        s->mv_type= MV_TYPE_16X16;
1753
    }
1754

    
1755
    for(i=0; i<4; i++){
1756
        int index= s->block_index[i];
1757
        int min, max;
1758

    
1759
        c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
1760
        c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
1761
        c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1762
        c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1763
//        c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
1764
//        c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
1765

    
1766
        max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1767
        min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1768
        max+= 16*mb_x + 1; // +-1 is for the simpler rounding
1769
        min+= 16*mb_x - 1;
1770
        xmax= FFMIN(xmax, s->width - max);
1771
        xmin= FFMAX(xmin, - 16     - min);
1772

    
1773
        max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1774
        min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1775
        max+= 16*mb_y + 1; // +-1 is for the simpler rounding
1776
        min+= 16*mb_y - 1;
1777
        ymax= FFMIN(ymax, s->height - max);
1778
        ymin= FFMAX(ymin, - 16      - min);
1779

    
1780
        if(s->mv_type == MV_TYPE_16X16) break;
1781
    }
1782

    
1783
    assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1784

    
1785
    if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1786
        s->b_direct_mv_table[mot_xy][0]= 0;
1787
        s->b_direct_mv_table[mot_xy][1]= 0;
1788

    
1789
        return 256*256*256*64;
1790
    }
1791

    
1792
    c->xmin= xmin;
1793
    c->ymin= ymin;
1794
    c->xmax= xmax;
1795
    c->ymax= ymax;
1796
    c->flags     |= FLAG_DIRECT;
1797
    c->sub_flags |= FLAG_DIRECT;
1798
    c->pred_x=0;
1799
    c->pred_y=0;
1800

    
1801
    P_LEFT[0]        = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
1802
    P_LEFT[1]        = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
1803

    
1804
    /* special case for first line */
1805
    if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as its clipped
1806
        P_TOP[0]      = av_clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);
1807
        P_TOP[1]      = av_clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);
1808
        P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);
1809
        P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);
1810

    
1811
        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1812
        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1813
    }
1814

    
1815
    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
1816
    if(c->sub_flags&FLAG_QPEL)
1817
        dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1818
    else
1819
        dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1820

    
1821
    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1822
        dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1823

    
1824
    get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
1825

    
1826
    mv_table[mot_xy][0]= mx;
1827
    mv_table[mot_xy][1]= my;
1828
    c->flags     &= ~FLAG_DIRECT;
1829
    c->sub_flags &= ~FLAG_DIRECT;
1830

    
1831
    return dmin;
1832
}
1833

    
1834
void ff_estimate_b_frame_motion(MpegEncContext * s,
1835
                             int mb_x, int mb_y)
1836
{
1837
    MotionEstContext * const c= &s->me;
1838
    const int penalty_factor= c->mb_penalty_factor;
1839
    int fmin, bmin, dmin, fbmin, bimin, fimin;
1840
    int type=0;
1841
    const int xy = mb_y*s->mb_stride + mb_x;
1842
    init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
1843

    
1844
    get_limits(s, 16*mb_x, 16*mb_y);
1845

    
1846
    c->skip=0;
1847

    
1848
    if(s->codec_id == CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]){
1849
        int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
1850

    
1851
        score= ((unsigned)(score*score + 128*256))>>16;
1852
        c->mc_mb_var_sum_temp += score;
1853
        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1854
        s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
1855

    
1856
        return;
1857
    }
1858

    
1859
    if(c->avctx->me_threshold){
1860
        int vard= check_input_motion(s, mb_x, mb_y, 0);
1861

    
1862
        if((vard+128)>>8 < c->avctx->me_threshold){
1863
//            pix = c->src[0][0];
1864
//            sum = s->dsp.pix_sum(pix, s->linesize);
1865
//            varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
1866

    
1867
//            pic->mb_var   [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
1868
             s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
1869
/*            pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1870
            c->mb_var_sum_temp    += (varc+128)>>8;*/
1871
            c->mc_mb_var_sum_temp += (vard+128)>>8;
1872
/*            if (vard <= 64<<8 || vard < varc) {
1873
                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1874
            }else{
1875
                c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
1876
            }*/
1877
            return;
1878
        }
1879
        if((vard+128)>>8 < c->avctx->mb_threshold){
1880
            type= s->mb_type[mb_y*s->mb_stride + mb_x];
1881
            if(type == CANDIDATE_MB_TYPE_DIRECT){
1882
                direct_search(s, mb_x, mb_y);
1883
            }
1884
            if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
1885
                c->skip=0;
1886
                ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
1887
            }
1888
            if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
1889
                c->skip=0;
1890
                ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
1891
            }
1892
            if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
1893
                c->skip=0;
1894
                c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1895
                interlaced_search(s, 0,
1896
                                        s->b_field_mv_table[0], s->b_field_select_table[0],
1897
                                        s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
1898
            }
1899
            if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
1900
                c->skip=0;
1901
                c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
1902
                interlaced_search(s, 2,
1903
                                        s->b_field_mv_table[1], s->b_field_select_table[1],
1904
                                        s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
1905
            }
1906
            return;
1907
        }
1908
    }
1909

    
1910
    if (s->codec_id == CODEC_ID_MPEG4)
1911
        dmin= direct_search(s, mb_x, mb_y);
1912
    else
1913
        dmin= INT_MAX;
1914
//FIXME penalty stuff for non mpeg4
1915
    c->skip=0;
1916
    fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
1917

    
1918
    c->skip=0;
1919
    bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
1920
//printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1921

    
1922
    c->skip=0;
1923
    fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
1924
//printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1925

    
1926
    if(s->flags & CODEC_FLAG_INTERLACED_ME){
1927
//FIXME mb type penalty
1928
        c->skip=0;
1929
        c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1930
        fimin= interlaced_search(s, 0,
1931
                                 s->b_field_mv_table[0], s->b_field_select_table[0],
1932
                                 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
1933
        c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
1934
        bimin= interlaced_search(s, 2,
1935
                                 s->b_field_mv_table[1], s->b_field_select_table[1],
1936
                                 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
1937
    }else
1938
        fimin= bimin= INT_MAX;
1939

    
1940
    {
1941
        int score= fmin;
1942
        type = CANDIDATE_MB_TYPE_FORWARD;
1943

    
1944
        if (dmin <= score){
1945
            score = dmin;
1946
            type = CANDIDATE_MB_TYPE_DIRECT;
1947
        }
1948
        if(bmin<score){
1949
            score=bmin;
1950
            type= CANDIDATE_MB_TYPE_BACKWARD;
1951
        }
1952
        if(fbmin<score){
1953
            score=fbmin;
1954
            type= CANDIDATE_MB_TYPE_BIDIR;
1955
        }
1956
        if(fimin<score){
1957
            score=fimin;
1958
            type= CANDIDATE_MB_TYPE_FORWARD_I;
1959
        }
1960
        if(bimin<score){
1961
            score=bimin;
1962
            type= CANDIDATE_MB_TYPE_BACKWARD_I;
1963
        }
1964

    
1965
        score= ((unsigned)(score*score + 128*256))>>16;
1966
        c->mc_mb_var_sum_temp += score;
1967
        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1968
    }
1969

    
1970
    if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1971
        type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
1972
        if(fimin < INT_MAX)
1973
            type |= CANDIDATE_MB_TYPE_FORWARD_I;
1974
        if(bimin < INT_MAX)
1975
            type |= CANDIDATE_MB_TYPE_BACKWARD_I;
1976
        if(fimin < INT_MAX && bimin < INT_MAX){
1977
            type |= CANDIDATE_MB_TYPE_BIDIR_I;
1978
        }
1979
         //FIXME something smarter
1980
        if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
1981
        if(s->codec_id == CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
1982
            type |= CANDIDATE_MB_TYPE_DIRECT0;
1983
#if 0
1984
        if(s->out_format == FMT_MPEG1)
1985
            type |= CANDIDATE_MB_TYPE_INTRA;
1986
#endif
1987
    }
1988

    
1989
    s->mb_type[mb_y*s->mb_stride + mb_x]= type;
1990
}
1991

    
1992
/* find best f_code for ME which do unlimited searches */
1993
int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
1994
{
1995
    if(s->me_method>=ME_EPZS){
1996
        int score[8];
1997
        int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
1998
        uint8_t * fcode_tab= s->fcode_tab;
1999
        int best_fcode=-1;
2000
        int best_score=-10000000;
2001

    
2002
        if(s->msmpeg4_version)
2003
            range= FFMIN(range, 16);
2004
        else if(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
2005
            range= FFMIN(range, 256);
2006

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

    
2009
        for(y=0; y<s->mb_height; y++){
2010
            int x;
2011
            int xy= y*s->mb_stride;
2012
            for(x=0; x<s->mb_width; x++){
2013
                if(s->mb_type[xy] & type){
2014
                    int mx= mv_table[xy][0];
2015
                    int my= mv_table[xy][1];
2016
                    int fcode= FFMAX(fcode_tab[mx + MAX_MV],
2017
                                     fcode_tab[my + MAX_MV]);
2018
                    int j;
2019

    
2020
                        if(mx >= range || mx < -range ||
2021
                           my >= range || my < -range)
2022
                            continue;
2023

    
2024
                    for(j=0; j<fcode && j<8; j++){
2025
                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
2026
                            score[j]-= 170;
2027
                    }
2028
                }
2029
                xy++;
2030
            }
2031
        }
2032

    
2033
        for(i=1; i<8; i++){
2034
            if(score[i] > best_score){
2035
                best_score= score[i];
2036
                best_fcode= i;
2037
            }
2038
//            printf("%d %d\n", i, score[i]);
2039
        }
2040

    
2041
//    printf("fcode: %d type: %d\n", i, s->pict_type);
2042
        return best_fcode;
2043
/*        for(i=0; i<=MAX_FCODE; i++){
2044
            printf("%d ", mv_num[i]);
2045
        }
2046
        printf("\n");*/
2047
    }else{
2048
        return 1;
2049
    }
2050
}
2051

    
2052
void ff_fix_long_p_mvs(MpegEncContext * s)
2053
{
2054
    MotionEstContext * const c= &s->me;
2055
    const int f_code= s->f_code;
2056
    int y, range;
2057
    assert(s->pict_type==P_TYPE);
2058

    
2059
    range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
2060

    
2061
    assert(range <= 16 || !s->msmpeg4_version);
2062
    assert(range <=256 || !(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
2063

    
2064
    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
2065

    
2066
//printf("%d no:%d %d//\n", clip, noclip, f_code);
2067
    if(s->flags&CODEC_FLAG_4MV){
2068
        const int wrap= s->b8_stride;
2069

    
2070
        /* clip / convert to intra 8x8 type MVs */
2071
        for(y=0; y<s->mb_height; y++){
2072
            int xy= y*2*wrap;
2073
            int i= y*s->mb_stride;
2074
            int x;
2075

    
2076
            for(x=0; x<s->mb_width; x++){
2077
                if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
2078
                    int block;
2079
                    for(block=0; block<4; block++){
2080
                        int off= (block& 1) + (block>>1)*wrap;
2081
                        int mx= s->current_picture.motion_val[0][ xy + off ][0];
2082
                        int my= s->current_picture.motion_val[0][ xy + off ][1];
2083

    
2084
                        if(   mx >=range || mx <-range
2085
                           || my >=range || my <-range){
2086
                            s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
2087
                            s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
2088
                            s->current_picture.mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
2089
                        }
2090
                    }
2091
                }
2092
                xy+=2;
2093
                i++;
2094
            }
2095
        }
2096
    }
2097
}
2098

    
2099
/**
2100
 *
2101
 * @param truncate 1 for truncation, 0 for using intra
2102
 */
2103
void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
2104
                     int16_t (*mv_table)[2], int f_code, int type, int truncate)
2105
{
2106
    MotionEstContext * const c= &s->me;
2107
    int y, h_range, v_range;
2108

    
2109
    // RAL: 8 in MPEG-1, 16 in MPEG-4
2110
    int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
2111

    
2112
    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
2113

    
2114
    h_range= range;
2115
    v_range= field_select_table ? range>>1 : range;
2116

    
2117
    /* clip / convert to intra 16x16 type MVs */
2118
    for(y=0; y<s->mb_height; y++){
2119
        int x;
2120
        int xy= y*s->mb_stride;
2121
        for(x=0; x<s->mb_width; x++){
2122
            if (s->mb_type[xy] & type){    // RAL: "type" test added...
2123
                if(field_select_table==NULL || field_select_table[xy] == field_select){
2124
                    if(   mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
2125
                       || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
2126

    
2127
                        if(truncate){
2128
                            if     (mv_table[xy][0] > h_range-1) mv_table[xy][0]=  h_range-1;
2129
                            else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
2130
                            if     (mv_table[xy][1] > v_range-1) mv_table[xy][1]=  v_range-1;
2131
                            else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
2132
                        }else{
2133
                            s->mb_type[xy] &= ~type;
2134
                            s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
2135
                            mv_table[xy][0]=
2136
                            mv_table[xy][1]= 0;
2137
                        }
2138
                    }
2139
                }
2140
            }
2141
            xy++;
2142
        }
2143
    }
2144
}