Statistics
| Branch: | Revision:

ffmpeg / libavcodec / motion_est.c @ 2f16af06

History | View | Annotate | Download (76 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
        if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
123
            const int time_pp= s->pp_time;
124
            const int time_pb= s->pb_time;
125
            const int mask= 2*qpel+1;
126
            if(s->mv_type==MV_TYPE_8X8){
127
                int i;
128
                for(i=0; i<4; i++){
129
                    int fx = c->direct_basis_mv[i][0] + hx;
130
                    int fy = c->direct_basis_mv[i][1] + hy;
131
                    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));
132
                    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));
133
                    int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
134
                    int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
135

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

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

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

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

    
222
#include "motion_est_template.c"
223

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

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

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

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

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

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

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

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

    
278
    // 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
279
    if(s->codec_id != CODEC_ID_SNOW){
280
        if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
281
            s->dsp.me_cmp[2]= zero_cmp;
282
        }
283
        if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
284
            s->dsp.me_sub_cmp[2]= zero_cmp;
285
        }
286
        c->hpel_put[2][0]= c->hpel_put[2][1]=
287
        c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
288
    }
289

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

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

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

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

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

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

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

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

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

    
382

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

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

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

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

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

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

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

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

    
431
        range = range >> 1;
432

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

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

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

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

    
449
    } while (range >= 1);
450

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

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

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

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

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

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

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

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

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

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

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

    
518
        range = range >> 1;
519

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

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

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

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

    
538
    } while (range >= 1);
539

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

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

    
551
#define Z_THRESHOLD 256
552

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

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

    
573
    assert(flags == 0);
574

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

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

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

    
589
    dminh = dmin;
590

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

    
603

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

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

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

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

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

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

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

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

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

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

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

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

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

    
751
    init_mv4_ref(c);
752

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
832
    if(same)
833
        return INT_MAX;
834

    
835
    if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
836
        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);
837
    }
838

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

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

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

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

    
858
        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);
859
        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);
860
    }
861

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
997
    if(same)
998
        return INT_MAX;
999

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1270
        break;
1271
    }
1272

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1473
    return dmin;
1474
}
1475

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

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

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

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

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

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

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

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

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

    
1553
        break;
1554
    }
1555

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

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

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

    
1566
    return dmin;
1567
}
1568

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

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

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

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

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

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

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

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

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

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

    
1630
    return fbmin;
1631
}
1632

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

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

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

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

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

    
1695
        do{
1696
            end=1;
1697

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

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

    
1728
    return fbmin;
1729
}
1730

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1830
    return dmin;
1831
}
1832

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

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

    
1845
    c->skip=0;
1846

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

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

    
1855
        return;
1856
    }
1857

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

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

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

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

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

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

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

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

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

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

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

    
1986
    s->mb_type[mb_y*s->mb_stride + mb_x]= type;
1987
}
1988

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

    
1999
        if(s->msmpeg4_version)
2000
            range= FFMIN(range, 16);
2001
        else if(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
2002
            range= FFMIN(range, 256);
2003

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

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

    
2017
                        if(mx >= range || mx < -range ||
2018
                           my >= range || my < -range)
2019
                            continue;
2020

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

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

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

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

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

    
2058
    assert(range <= 16 || !s->msmpeg4_version);
2059
    assert(range <=256 || !(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
2060

    
2061
    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
2062

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

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

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

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

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

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

    
2109
    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
2110

    
2111
    h_range= range;
2112
    v_range= field_select_table ? range>>1 : range;
2113

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

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