Statistics
| Branch: | Revision:

ffmpeg / libavcodec / motion_est.c @ da9c9637

History | View | Annotate | Download (70.4 KB)

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

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

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

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

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

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

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

    
69
static int minima_cmp(const void *a, const void *b){
70
    const Minima *da = (const Minima *) a;
71
    const Minima *db = (const Minima *) b;
72
    
73
    return da->height - db->height;
74
}
75

    
76
#define FLAG_QPEL   1 //must be 1
77
#define FLAG_CHROMA 2
78
#define FLAG_DIRECT 4
79

    
80
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){
81
    const int offset[3]= {
82
          y*c->  stride + x,
83
        ((y*c->uvstride + x)>>1),
84
        ((y*c->uvstride + x)>>1),
85
    };
86
    int i;
87
    for(i=0; i<3; i++){
88
        c->src[0][i]= src [i] + offset[i];
89
        c->ref[0][i]= ref [i] + offset[i];
90
    }
91
    if(ref_index){
92
        for(i=0; i<3; i++){
93
            c->ref[ref_index][i]= ref2[i] + offset[i];
94
        }
95
    }
96
}
97

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

    
104
static always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
105
                      const int size, const int h, int ref_index, int src_index,
106
                      me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
107
    MotionEstContext * const c= &s->me;
108
    const int stride= c->stride;
109
    const int uvstride= c->uvstride;
110
    const int qpel= flags&FLAG_QPEL;
111
    const int chroma= flags&FLAG_CHROMA;
112
    const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
113
    const int hx= subx + (x<<(1+qpel));
114
    const int hy= suby + (y<<(1+qpel));
115
    uint8_t * const * const ref= c->ref[ref_index];
116
    uint8_t * const * const src= c->src[src_index];
117
    int d;
118
    //FIXME check chroma 4mv, (no crashes ...)
119
    if(flags&FLAG_DIRECT){
120
        if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
121
            const int time_pp= s->pp_time;
122
            const int time_pb= s->pb_time;
123
            const int mask= 2*qpel+1;
124
            if(s->mv_type==MV_TYPE_8X8){
125
                int i;
126
                for(i=0; i<4; i++){
127
                    int fx = c->direct_basis_mv[i][0] + hx;
128
                    int fy = c->direct_basis_mv[i][1] + hy;
129
                    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));
130
                    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));
131
                    int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
132
                    int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
133
        
134
                    uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
135
                    if(qpel){
136
                        c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
137
                        c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
138
                    }else{
139
                        c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
140
                        c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
141
                    }
142
                }
143
            }else{
144
                int fx = c->direct_basis_mv[0][0] + hx;
145
                int fy = c->direct_basis_mv[0][1] + hy;
146
                int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
147
                int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
148
                int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
149
                int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
150
                
151
                if(qpel){
152
                    c->qpel_put[1][fxy](c->temp               , ref[0] + (fx>>2) + (fy>>2)*stride               , stride);
153
                    c->qpel_put[1][fxy](c->temp + 8           , ref[0] + (fx>>2) + (fy>>2)*stride + 8           , stride);
154
                    c->qpel_put[1][fxy](c->temp     + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride     + 8*stride, stride);
155
                    c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
156
                    c->qpel_avg[1][bxy](c->temp               , ref[8] + (bx>>2) + (by>>2)*stride               , stride);
157
                    c->qpel_avg[1][bxy](c->temp + 8           , ref[8] + (bx>>2) + (by>>2)*stride + 8           , stride);
158
                    c->qpel_avg[1][bxy](c->temp     + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride     + 8*stride, stride);
159
                    c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
160
                }else{            
161
                    assert((fx>>1) + 16*s->mb_x >= -16);
162
                    assert((fy>>1) + 16*s->mb_y >= -16);
163
                    assert((fx>>1) + 16*s->mb_x <= s->width);
164
                    assert((fy>>1) + 16*s->mb_y <= s->height);
165
                    assert((bx>>1) + 16*s->mb_x >= -16);
166
                    assert((by>>1) + 16*s->mb_y >= -16);
167
                    assert((bx>>1) + 16*s->mb_x <= s->width);
168
                    assert((by>>1) + 16*s->mb_y <= s->height);
169

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

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

    
220
#include "motion_est_template.c"
221

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

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

    
229
void ff_init_me(MpegEncContext *s){
230
    MotionEstContext * const c= &s->me;
231
    c->avctx= s->avctx;
232

    
233
    ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
234
    ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
235
    ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
236
    ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
237
    
238
    c->flags    = get_flags(c, 0, c->avctx->me_cmp    &FF_CMP_CHROMA);
239
    c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
240
    c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp    &FF_CMP_CHROMA);
241

    
242
/*FIXME s->no_rounding b_type*/
243
    if(s->flags&CODEC_FLAG_QPEL){
244
        c->sub_motion_search= qpel_motion_search;
245
        c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
246
        if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
247
        else               c->qpel_put= s->dsp.put_qpel_pixels_tab;
248
    }else{
249
        if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
250
            c->sub_motion_search= hpel_motion_search;
251
        else if(   c->avctx->me_sub_cmp == FF_CMP_SAD 
252
                && c->avctx->    me_cmp == FF_CMP_SAD 
253
                && c->avctx->    mb_cmp == FF_CMP_SAD)
254
            c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
255
        else
256
            c->sub_motion_search= hpel_motion_search;
257
    }
258
    c->hpel_avg= s->dsp.avg_pixels_tab;
259
    if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
260
    else               c->hpel_put= s->dsp.put_pixels_tab;
261

    
262
    if(s->linesize){
263
        c->stride  = s->linesize; 
264
        c->uvstride= s->uvlinesize;
265
    }else{
266
        c->stride  = 16*s->mb_width + 32;
267
        c->uvstride=  8*s->mb_width + 16;
268
    }
269

    
270
    // 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
271
    if(s->codec_id != CODEC_ID_SNOW){
272
        if((c->avctx->me_cmp&FF_CMP_CHROMA) && !s->dsp.me_cmp[2]){
273
            s->dsp.me_cmp[2]= zero_cmp;
274
        }
275
        if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
276
            s->dsp.me_sub_cmp[2]= zero_cmp;
277
        }
278
        c->hpel_put[2][0]= c->hpel_put[2][1]=
279
        c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
280
    }
281

    
282
    c->temp= c->scratchpad;
283
}
284
      
285
#if 0
286
static int pix_dev(uint8_t * pix, int line_size, int mean)
287
{
288
    int s, i, j;
289

290
    s = 0;
291
    for (i = 0; i < 16; i++) {
292
        for (j = 0; j < 16; j += 8) {
293
            s += ABS(pix[0]-mean);
294
            s += ABS(pix[1]-mean);
295
            s += ABS(pix[2]-mean);
296
            s += ABS(pix[3]-mean);
297
            s += ABS(pix[4]-mean);
298
            s += ABS(pix[5]-mean);
299
            s += ABS(pix[6]-mean);
300
            s += ABS(pix[7]-mean);
301
            pix += 8;
302
        }
303
        pix += line_size - 16;
304
    }
305
    return s;
306
}
307
#endif
308

    
309
static inline void no_motion_search(MpegEncContext * s,
310
                                    int *mx_ptr, int *my_ptr)
311
{
312
    *mx_ptr = 16 * s->mb_x;
313
    *my_ptr = 16 * s->mb_y;
314
}
315

    
316
static int full_motion_search(MpegEncContext * s,
317
                              int *mx_ptr, int *my_ptr, int range,
318
                              int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
319
{
320
    int x1, y1, x2, y2, xx, yy, x, y;
321
    int mx, my, dmin, d;
322
    uint8_t *pix;
323

    
324
    xx = 16 * s->mb_x;
325
    yy = 16 * s->mb_y;
326
    x1 = xx - range + 1;        /* we loose one pixel to avoid boundary pb with half pixel pred */
327
    if (x1 < xmin)
328
        x1 = xmin;
329
    x2 = xx + range - 1;
330
    if (x2 > xmax)
331
        x2 = xmax;
332
    y1 = yy - range + 1;
333
    if (y1 < ymin)
334
        y1 = ymin;
335
    y2 = yy + range - 1;
336
    if (y2 > ymax)
337
        y2 = ymax;
338
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
339
    dmin = 0x7fffffff;
340
    mx = 0;
341
    my = 0;
342
    for (y = y1; y <= y2; y++) {
343
        for (x = x1; x <= x2; x++) {
344
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x,
345
                             s->linesize, 16);
346
            if (d < dmin ||
347
                (d == dmin &&
348
                 (abs(x - xx) + abs(y - yy)) <
349
                 (abs(mx - xx) + abs(my - yy)))) {
350
                dmin = d;
351
                mx = x;
352
                my = y;
353
            }
354
        }
355
    }
356

    
357
    *mx_ptr = mx;
358
    *my_ptr = my;
359

    
360
#if 0
361
    if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
362
        *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
363
        fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
364
    }
365
#endif
366
    return dmin;
367
}
368

    
369

    
370
static int log_motion_search(MpegEncContext * s,
371
                             int *mx_ptr, int *my_ptr, int range,
372
                             int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
373
{
374
    int x1, y1, x2, y2, xx, yy, x, y;
375
    int mx, my, dmin, d;
376
    uint8_t *pix;
377

    
378
    xx = s->mb_x << 4;
379
    yy = s->mb_y << 4;
380

    
381
    /* Left limit */
382
    x1 = xx - range;
383
    if (x1 < xmin)
384
        x1 = xmin;
385

    
386
    /* Right limit */
387
    x2 = xx + range;
388
    if (x2 > xmax)
389
        x2 = xmax;
390

    
391
    /* Upper limit */
392
    y1 = yy - range;
393
    if (y1 < ymin)
394
        y1 = ymin;
395

    
396
    /* Lower limit */
397
    y2 = yy + range;
398
    if (y2 > ymax)
399
        y2 = ymax;
400

    
401
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
402
    dmin = 0x7fffffff;
403
    mx = 0;
404
    my = 0;
405

    
406
    do {
407
        for (y = y1; y <= y2; y += range) {
408
            for (x = x1; x <= x2; x += range) {
409
                d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
410
                if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
411
                    dmin = d;
412
                    mx = x;
413
                    my = y;
414
                }
415
            }
416
        }
417

    
418
        range = range >> 1;
419

    
420
        x1 = mx - range;
421
        if (x1 < xmin)
422
            x1 = xmin;
423

    
424
        x2 = mx + range;
425
        if (x2 > xmax)
426
            x2 = xmax;
427

    
428
        y1 = my - range;
429
        if (y1 < ymin)
430
            y1 = ymin;
431

    
432
        y2 = my + range;
433
        if (y2 > ymax)
434
            y2 = ymax;
435

    
436
    } while (range >= 1);
437

    
438
#ifdef DEBUG
439
    fprintf(stderr, "log       - MX: %d\tMY: %d\n", mx, my);
440
#endif
441
    *mx_ptr = mx;
442
    *my_ptr = my;
443
    return dmin;
444
}
445

    
446
static int phods_motion_search(MpegEncContext * s,
447
                               int *mx_ptr, int *my_ptr, int range,
448
                               int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
449
{
450
    int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
451
    int mx, my, dminx, dminy;
452
    uint8_t *pix;
453

    
454
    xx = s->mb_x << 4;
455
    yy = s->mb_y << 4;
456

    
457
    /* Left limit */
458
    x1 = xx - range;
459
    if (x1 < xmin)
460
        x1 = xmin;
461

    
462
    /* Right limit */
463
    x2 = xx + range;
464
    if (x2 > xmax)
465
        x2 = xmax;
466

    
467
    /* Upper limit */
468
    y1 = yy - range;
469
    if (y1 < ymin)
470
        y1 = ymin;
471

    
472
    /* Lower limit */
473
    y2 = yy + range;
474
    if (y2 > ymax)
475
        y2 = ymax;
476

    
477
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
478
    mx = 0;
479
    my = 0;
480

    
481
    x = xx;
482
    y = yy;
483
    do {
484
        dminx = 0x7fffffff;
485
        dminy = 0x7fffffff;
486

    
487
        lastx = x;
488
        for (x = x1; x <= x2; x += range) {
489
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
490
            if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
491
                dminx = d;
492
                mx = x;
493
            }
494
        }
495

    
496
        x = lastx;
497
        for (y = y1; y <= y2; y += range) {
498
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
499
            if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
500
                dminy = d;
501
                my = y;
502
            }
503
        }
504

    
505
        range = range >> 1;
506

    
507
        x = mx;
508
        y = my;
509
        x1 = mx - range;
510
        if (x1 < xmin)
511
            x1 = xmin;
512

    
513
        x2 = mx + range;
514
        if (x2 > xmax)
515
            x2 = xmax;
516

    
517
        y1 = my - range;
518
        if (y1 < ymin)
519
            y1 = ymin;
520

    
521
        y2 = my + range;
522
        if (y2 > ymax)
523
            y2 = ymax;
524

    
525
    } while (range >= 1);
526

    
527
#ifdef DEBUG
528
    fprintf(stderr, "phods     - MX: %d\tMY: %d\n", mx, my);
529
#endif
530

    
531
    /* half pixel search */
532
    *mx_ptr = mx;
533
    *my_ptr = my;
534
    return dminy;
535
}
536

    
537

    
538
#define Z_THRESHOLD 256
539

    
540
#define CHECK_SAD_HALF_MV(suffix, x, y) \
541
{\
542
    d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
543
    d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
544
    COPY3_IF_LT(dminh, d, dx, x, dy, y)\
545
}
546

    
547
static inline int sad_hpel_motion_search(MpegEncContext * s,
548
                                  int *mx_ptr, int *my_ptr, int dmin,
549
                                  int src_index, int ref_index,
550
                                  int size, int h)
551
{
552
    MotionEstContext * const c= &s->me;
553
    const int penalty_factor= c->sub_penalty_factor;
554
    int mx, my, dminh;
555
    uint8_t *pix, *ptr;
556
    int stride= c->stride;
557
    const int flags= c->sub_flags;
558
    LOAD_COMMON
559
    
560
    assert(flags == 0);
561

    
562
    if(c->skip){
563
//    printf("S");
564
        *mx_ptr = 0;
565
        *my_ptr = 0;
566
        return dmin;
567
    }
568
//    printf("N");
569
        
570
    pix = c->src[src_index][0];
571

    
572
    mx = *mx_ptr;
573
    my = *my_ptr;
574
    ptr = c->ref[ref_index][0] + (my * stride) + mx;
575
    
576
    dminh = dmin;
577

    
578
    if (mx > xmin && mx < xmax && 
579
        my > ymin && my < ymax) {
580
        int dx=0, dy=0;
581
        int d, pen_x, pen_y; 
582
        const int index= (my<<ME_MAP_SHIFT) + mx;
583
        const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
584
        const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)];
585
        const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)];
586
        const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
587
        mx<<=1;
588
        my<<=1;
589

    
590
        
591
        pen_x= pred_x + mx;
592
        pen_y= pred_y + my;
593

    
594
        ptr-= stride;
595
        if(t<=b){
596
            CHECK_SAD_HALF_MV(y2 , 0, -1)
597
            if(l<=r){
598
                CHECK_SAD_HALF_MV(xy2, -1, -1)
599
                if(t+r<=b+l){
600
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
601
                    ptr+= stride;
602
                }else{
603
                    ptr+= stride;
604
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
605
                }
606
                CHECK_SAD_HALF_MV(x2 , -1,  0)
607
            }else{
608
                CHECK_SAD_HALF_MV(xy2, +1, -1)
609
                if(t+l<=b+r){
610
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
611
                    ptr+= stride;
612
                }else{
613
                    ptr+= stride;
614
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
615
                }
616
                CHECK_SAD_HALF_MV(x2 , +1,  0)
617
            }
618
        }else{
619
            if(l<=r){
620
                if(t+l<=b+r){
621
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
622
                    ptr+= stride;
623
                }else{
624
                    ptr+= stride;
625
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
626
                }
627
                CHECK_SAD_HALF_MV(x2 , -1,  0)
628
                CHECK_SAD_HALF_MV(xy2, -1, +1)
629
            }else{
630
                if(t+r<=b+l){
631
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
632
                    ptr+= stride;
633
                }else{
634
                    ptr+= stride;
635
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
636
                }
637
                CHECK_SAD_HALF_MV(x2 , +1,  0)
638
                CHECK_SAD_HALF_MV(xy2, +1, +1)
639
            }
640
            CHECK_SAD_HALF_MV(y2 ,  0, +1)
641
        }
642
        mx+=dx;
643
        my+=dy;
644

    
645
    }else{
646
        mx<<=1;
647
        my<<=1;
648
    }
649

    
650
    *mx_ptr = mx;
651
    *my_ptr = my;
652
    return dminh;
653
}
654

    
655
static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
656
{
657
    const int xy= s->mb_x + s->mb_y*s->mb_stride;
658
    
659
    s->p_mv_table[xy][0] = mx;
660
    s->p_mv_table[xy][1] = my;
661

    
662
    /* has allready been set to the 4 MV if 4MV is done */
663
    if(mv4){
664
        int mot_xy= s->block_index[0];
665

    
666
        s->current_picture.motion_val[0][mot_xy  ][0]= mx;
667
        s->current_picture.motion_val[0][mot_xy  ][1]= my;
668
        s->current_picture.motion_val[0][mot_xy+1][0]= mx;
669
        s->current_picture.motion_val[0][mot_xy+1][1]= my;
670

    
671
        mot_xy += s->b8_stride;
672
        s->current_picture.motion_val[0][mot_xy  ][0]= mx;
673
        s->current_picture.motion_val[0][mot_xy  ][1]= my;
674
        s->current_picture.motion_val[0][mot_xy+1][0]= mx;
675
        s->current_picture.motion_val[0][mot_xy+1][1]= my;
676
    }
677
}
678

    
679
/**
680
 * get fullpel ME search limits.
681
 */
682
static inline void get_limits(MpegEncContext *s, int x, int y)
683
{
684
    MotionEstContext * const c= &s->me;
685
/*
686
    if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
687
    else                   c->range= 16;
688
*/
689
    if (s->unrestricted_mv) {
690
        c->xmin = - x - 16;
691
        c->ymin = - y - 16;
692
        c->xmax = - x + s->mb_width *16;
693
        c->ymax = - y + s->mb_height*16;
694
    } else {
695
        c->xmin = - x;
696
        c->ymin = - y;
697
        c->xmax = - x + s->mb_width *16 - 16;
698
        c->ymax = - y + s->mb_height*16 - 16;
699
    }
700
}
701

    
702
static inline void init_mv4_ref(MotionEstContext *c){
703
    const int stride= c->stride;
704

    
705
    c->ref[1][0] = c->ref[0][0] + 8;
706
    c->ref[2][0] = c->ref[0][0] + 8*stride;
707
    c->ref[3][0] = c->ref[2][0] + 8;
708
    c->src[1][0] = c->src[0][0] + 8;
709
    c->src[2][0] = c->src[0][0] + 8*stride;
710
    c->src[3][0] = c->src[2][0] + 8;
711
}
712

    
713
static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
714
{
715
    MotionEstContext * const c= &s->me;
716
    const int size= 1;
717
    const int h=8;
718
    int block;
719
    int P[10][2];
720
    int dmin_sum=0, mx4_sum=0, my4_sum=0;
721
    int same=1;
722
    const int stride= c->stride;
723
    const int uvstride= c->uvstride;
724
    uint8_t *mv_penalty= c->current_mv_penalty;
725

    
726
    init_mv4_ref(c);
727
    
728
    for(block=0; block<4; block++){
729
        int mx4, my4;
730
        int pred_x4, pred_y4;
731
        int dmin4;
732
        static const int off[4]= {2, 1, 1, -1};
733
        const int mot_stride = s->b8_stride;
734
        const int mot_xy = s->block_index[block];
735

    
736
        P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
737
        P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
738

    
739
        if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
740

    
741
        /* special case for first line */
742
        if (s->first_slice_line && block<2) {
743
            c->pred_x= pred_x4= P_LEFT[0];
744
            c->pred_y= pred_y4= P_LEFT[1];
745
        } else {
746
            P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride             ][0];
747
            P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride             ][1];
748
            P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
749
            P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
750
            if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
751
            if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
752
            if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
753
            if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
754
    
755
            P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
756
            P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
757

    
758
            c->pred_x= pred_x4 = P_MEDIAN[0];
759
            c->pred_y= pred_y4 = P_MEDIAN[1];
760
        }
761
        P_MV1[0]= mx;
762
        P_MV1[1]= my;
763

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

    
766
        dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
767
        
768
        if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
769
            int dxy;
770
            const int offset= ((block&1) + (block>>1)*stride)*8;
771
            uint8_t *dest_y = c->scratchpad + offset;
772
            if(s->quarter_sample){
773
                uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
774
                dxy = ((my4 & 3) << 2) | (mx4 & 3);
775

    
776
                if(s->no_rounding)
777
                    s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y   , ref    , stride);
778
                else
779
                    s->dsp.put_qpel_pixels_tab       [1][dxy](dest_y   , ref    , stride);
780
            }else{
781
                uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
782
                dxy = ((my4 & 1) << 1) | (mx4 & 1);
783

    
784
                if(s->no_rounding)
785
                    s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y    , ref    , stride, h);
786
                else
787
                    s->dsp.put_pixels_tab       [1][dxy](dest_y    , ref    , stride, h);
788
            }
789
            dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
790
        }else
791
            dmin_sum+= dmin4;
792

    
793
        if(s->quarter_sample){
794
            mx4_sum+= mx4/2;
795
            my4_sum+= my4/2;
796
        }else{
797
            mx4_sum+= mx4;
798
            my4_sum+= my4;
799
        }
800
            
801
        s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4;
802
        s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4;
803

    
804
        if(mx4 != mx || my4 != my) same=0;
805
    }
806
    
807
    if(same)
808
        return INT_MAX;
809
    
810
    if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
811
        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);
812
    }
813
    
814
    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
815
        int dxy;
816
        int mx, my;
817
        int offset;
818

    
819
        mx= ff_h263_round_chroma(mx4_sum);
820
        my= ff_h263_round_chroma(my4_sum);
821
        dxy = ((my & 1) << 1) | (mx & 1);
822
        
823
        offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
824
       
825
        if(s->no_rounding){
826
            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
827
            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
828
        }else{
829
            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
830
            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
831
        }
832

    
833
        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);
834
        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);
835
    }
836
    
837
    c->pred_x= mx;
838
    c->pred_y= my;
839

    
840
    switch(c->avctx->mb_cmp&0xFF){
841
    /*case FF_CMP_SSE:
842
        return dmin_sum+ 32*s->qscale*s->qscale;*/
843
    case FF_CMP_RD:
844
        return dmin_sum;
845
    default:
846
        return dmin_sum+ 11*c->mb_penalty_factor;
847
    }
848
}
849

    
850
static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
851
    MotionEstContext * const c= &s->me;
852

    
853
    c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
854
    c->src[1][0] = c->src[0][0] + s->linesize;
855
    if(c->flags & FLAG_CHROMA){
856
        c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
857
        c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
858
        c->src[1][1] = c->src[0][1] + s->uvlinesize;
859
        c->src[1][2] = c->src[0][2] + s->uvlinesize;
860
    }
861
}
862

    
863
static int interlaced_search(MpegEncContext *s, int ref_index, 
864
                             int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
865
{
866
    MotionEstContext * const c= &s->me;
867
    const int size=0;
868
    const int h=8;
869
    int block;
870
    int P[10][2];
871
    uint8_t * const mv_penalty= c->current_mv_penalty;
872
    int same=1;
873
    const int stride= 2*s->linesize;
874
    const int uvstride= 2*s->uvlinesize;
875
    int dmin_sum= 0;
876
    const int mot_stride= s->mb_stride;
877
    const int xy= s->mb_x + s->mb_y*mot_stride;
878
    
879
    c->ymin>>=1;
880
    c->ymax>>=1;
881
    c->stride<<=1;
882
    c->uvstride<<=1;
883
    init_interlaced_ref(s, ref_index);
884
    
885
    for(block=0; block<2; block++){
886
        int field_select;
887
        int best_dmin= INT_MAX;
888
        int best_field= -1;
889

    
890
        for(field_select=0; field_select<2; field_select++){
891
            int dmin, mx_i, my_i;
892
            int16_t (*mv_table)[2]= mv_tables[block][field_select];
893
            
894
            if(user_field_select){
895
                if(field_select_tables[block][xy] != field_select)
896
                    continue;
897
            }
898
            
899
            P_LEFT[0] = mv_table[xy - 1][0];
900
            P_LEFT[1] = mv_table[xy - 1][1];
901
            if(P_LEFT[0]       > (c->xmax<<1)) P_LEFT[0]       = (c->xmax<<1);
902
            
903
            c->pred_x= P_LEFT[0];
904
            c->pred_y= P_LEFT[1];
905
            
906
            if(!s->first_slice_line){
907
                P_TOP[0]      = mv_table[xy - mot_stride][0];
908
                P_TOP[1]      = mv_table[xy - mot_stride][1];
909
                P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
910
                P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
911
                if(P_TOP[1]      > (c->ymax<<1)) P_TOP[1]     = (c->ymax<<1);
912
                if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
913
                if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
914
                if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
915
    
916
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
917
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
918
            }
919
            P_MV1[0]= mx; //FIXME not correct if block != field_select
920
            P_MV1[1]= my / 2;
921
            
922
            dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
923

    
924
            dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
925
            
926
            mv_table[xy][0]= mx_i;
927
            mv_table[xy][1]= my_i;
928
            
929
            if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
930
                int dxy;
931

    
932
                //FIXME chroma ME
933
                uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
934
                dxy = ((my_i & 1) << 1) | (mx_i & 1);
935

    
936
                if(s->no_rounding){
937
                    s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref    , stride, h);
938
                }else{
939
                    s->dsp.put_pixels_tab       [size][dxy](c->scratchpad, ref    , stride, h);
940
                }
941
                dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
942
                dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
943
            }else
944
                dmin+= c->mb_penalty_factor; //field_select bits
945
                
946
            dmin += field_select != block; //slightly prefer same field
947
            
948
            if(dmin < best_dmin){
949
                best_dmin= dmin;
950
                best_field= field_select;
951
            }
952
        }
953
        {
954
            int16_t (*mv_table)[2]= mv_tables[block][best_field];
955

    
956
            if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
957
            if(mv_table[xy][1]&1) same=0;
958
            if(mv_table[xy][1]*2 != my) same=0; 
959
            if(best_field != block) same=0;
960
        }
961

    
962
        field_select_tables[block][xy]= best_field;
963
        dmin_sum += best_dmin;
964
    }
965
    
966
    c->ymin<<=1;
967
    c->ymax<<=1;
968
    c->stride>>=1;
969
    c->uvstride>>=1;
970

    
971
    if(same)
972
        return INT_MAX;
973
    
974
    switch(c->avctx->mb_cmp&0xFF){
975
    /*case FF_CMP_SSE:
976
        return dmin_sum+ 32*s->qscale*s->qscale;*/
977
    case FF_CMP_RD:
978
        return dmin_sum;
979
    default:
980
        return dmin_sum+ 11*c->mb_penalty_factor;
981
    }
982
}
983

    
984
static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
985
    int ymax= s->me.ymax>>interlaced;
986
    int ymin= s->me.ymin>>interlaced;
987
    
988
    if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
989
    if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
990
    if(mv[1] <       ymin) mv[1] =       ymin;
991
    if(mv[1] >       ymax) mv[1] =       ymax;
992
}
993

    
994
static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
995
    MotionEstContext * const c= &s->me;
996
    Picture *p= s->current_picture_ptr;
997
    int mb_xy= mb_x + mb_y*s->mb_stride;
998
    int xy= 2*mb_x + 2*mb_y*s->b8_stride;
999
    int mb_type= s->current_picture.mb_type[mb_xy];
1000
    int flags= c->flags;
1001
    int shift= (flags&FLAG_QPEL) + 1;
1002
    int mask= (1<<shift)-1;
1003
    int x, y, i;
1004
    int d=0;
1005
    me_cmp_func cmpf= s->dsp.sse[0];
1006
    me_cmp_func chroma_cmpf= s->dsp.sse[1];
1007
    
1008
    if(p_type && USES_LIST(mb_type, 1)){
1009
        av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
1010
        return INT_MAX;
1011
    }
1012
    assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
1013
    
1014
    for(i=0; i<4; i++){
1015
        int xy= s->block_index[i];
1016
        clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type));
1017
        clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type));
1018
    }
1019

    
1020
    if(IS_INTERLACED(mb_type)){
1021
        int xy2= xy  + s->b8_stride;
1022
        s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
1023
        c->stride<<=1;
1024
        c->uvstride<<=1;
1025
        
1026
        if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
1027
            av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
1028
            return INT_MAX;
1029
        }
1030

    
1031
        if(USES_LIST(mb_type, 0)){
1032
            int field_select0= p->ref_index[0][xy ];
1033
            int field_select1= p->ref_index[0][xy2];
1034
            assert(field_select0==0 ||field_select0==1);
1035
            assert(field_select1==0 ||field_select1==1);
1036
            init_interlaced_ref(s, 0);
1037

    
1038
            if(p_type){
1039
                s->p_field_select_table[0][mb_xy]= field_select0;
1040
                s->p_field_select_table[1][mb_xy]= field_select1;
1041
                *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
1042
                *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
1043
                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
1044
            }else{
1045
                s->b_field_select_table[0][0][mb_xy]= field_select0;
1046
                s->b_field_select_table[0][1][mb_xy]= field_select1;
1047
                *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
1048
                *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
1049
                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
1050
            }
1051

    
1052
            x= p->motion_val[0][xy ][0]; 
1053
            y= p->motion_val[0][xy ][1];
1054
            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
1055
            x= p->motion_val[0][xy2][0]; 
1056
            y= p->motion_val[0][xy2][1];
1057
            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
1058
        }
1059
        if(USES_LIST(mb_type, 1)){
1060
            int field_select0= p->ref_index[1][xy ];
1061
            int field_select1= p->ref_index[1][xy2];
1062
            assert(field_select0==0 ||field_select0==1);
1063
            assert(field_select1==0 ||field_select1==1);
1064
            init_interlaced_ref(s, 2);
1065

    
1066
            s->b_field_select_table[1][0][mb_xy]= field_select0;
1067
            s->b_field_select_table[1][1][mb_xy]= field_select1;
1068
            *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ];
1069
            *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[1][xy2];
1070
            if(USES_LIST(mb_type, 0)){
1071
                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
1072
            }else{
1073
                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
1074
            }
1075

    
1076
            x= p->motion_val[1][xy ][0]; 
1077
            y= p->motion_val[1][xy ][1];
1078
            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
1079
            x= p->motion_val[1][xy2][0]; 
1080
            y= p->motion_val[1][xy2][1];
1081
            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
1082
            //FIXME bidir scores
1083
        }
1084
        c->stride>>=1;
1085
        c->uvstride>>=1;
1086
    }else if(IS_8X8(mb_type)){
1087
        if(!(s->flags & CODEC_FLAG_4MV)){
1088
            av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
1089
            return INT_MAX;
1090
        }
1091
        cmpf= s->dsp.sse[1];
1092
        chroma_cmpf= s->dsp.sse[1];
1093
        init_mv4_ref(c);
1094
        for(i=0; i<4; i++){
1095
            xy= s->block_index[i];
1096
            x= p->motion_val[0][xy][0]; 
1097
            y= p->motion_val[0][xy][1];
1098
            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
1099
        }
1100
        s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
1101
    }else{
1102
        if(USES_LIST(mb_type, 0)){
1103
            if(p_type){
1104
                *(uint32_t*)s->p_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
1105
                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
1106
            }else if(USES_LIST(mb_type, 1)){
1107
                *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
1108
                *(uint32_t*)s->b_bidir_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
1109
                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
1110
            }else{
1111
                *(uint32_t*)s->b_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
1112
                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
1113
            }
1114
            x= p->motion_val[0][xy][0]; 
1115
            y= p->motion_val[0][xy][1];
1116
            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
1117
        }else if(USES_LIST(mb_type, 1)){
1118
            *(uint32_t*)s->b_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
1119
            s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
1120
           
1121
            x= p->motion_val[1][xy][0]; 
1122
            y= p->motion_val[1][xy][1];
1123
            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
1124
        }else
1125
            s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
1126
    }
1127
    return d;
1128
}
1129

    
1130
void ff_estimate_p_frame_motion(MpegEncContext * s,
1131
                                int mb_x, int mb_y)
1132
{
1133
    MotionEstContext * const c= &s->me;
1134
    uint8_t *pix, *ppix;
1135
    int sum, varc, vard, mx, my, dmin;
1136
    int P[10][2];
1137
    const int shift= 1+s->quarter_sample;
1138
    int mb_type=0;
1139
    Picture * const pic= &s->current_picture;
1140
    
1141
    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
1142

    
1143
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1144
    assert(s->linesize == c->stride);
1145
    assert(s->uvlinesize == c->uvstride);
1146

    
1147
    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1148
    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1149
    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1150
    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1151

    
1152
    get_limits(s, 16*mb_x, 16*mb_y);
1153
    c->skip=0;
1154

    
1155
    /* intra / predictive decision */
1156
    pix = c->src[0][0];
1157
    sum = s->dsp.pix_sum(pix, s->linesize);
1158
    varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
1159

    
1160
    pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1161
    pic->mb_var [s->mb_stride * mb_y + mb_x] = varc;
1162
    c->mb_var_sum_temp += varc;
1163

    
1164
    if(c->avctx->me_threshold){
1165
        vard= (check_input_motion(s, mb_x, mb_y, 1)+128)>>8;
1166
        
1167
        if(vard<c->avctx->me_threshold){
1168
            pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
1169
            c->mc_mb_var_sum_temp += vard;
1170
            if (vard <= 64 || vard < varc) { //FIXME
1171
                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1172
            }else{
1173
                c->scene_change_score+= s->qscale;
1174
            }
1175
            return;
1176
        }
1177
        if(vard<c->avctx->mb_threshold)
1178
            mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
1179
    }
1180

    
1181
    switch(s->me_method) {
1182
    case ME_ZERO:
1183
    default:
1184
        no_motion_search(s, &mx, &my);
1185
        mx-= mb_x*16;
1186
        my-= mb_y*16;
1187
        dmin = 0;
1188
        break;
1189
#if 0
1190
    case ME_FULL:
1191
        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
1192
        mx-= mb_x*16;
1193
        my-= mb_y*16;
1194
        break;
1195
    case ME_LOG:
1196
        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
1197
        mx-= mb_x*16;
1198
        my-= mb_y*16;
1199
        break;
1200
    case ME_PHODS:
1201
        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
1202
        mx-= mb_x*16;
1203
        my-= mb_y*16;
1204
        break;
1205
#endif
1206
    case ME_X1:
1207
    case ME_EPZS:
1208
       {
1209
            const int mot_stride = s->b8_stride;
1210
            const int mot_xy = s->block_index[0];
1211

    
1212
            P_LEFT[0]       = s->current_picture.motion_val[0][mot_xy - 1][0];
1213
            P_LEFT[1]       = s->current_picture.motion_val[0][mot_xy - 1][1];
1214

    
1215
            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
1216

    
1217
            if(!s->first_slice_line) {
1218
                P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];
1219
                P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];
1220
                P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
1221
                P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
1222
                if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
1223
                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
1224
                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
1225
        
1226
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1227
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1228

    
1229
                if(s->out_format == FMT_H263){
1230
                    c->pred_x = P_MEDIAN[0];
1231
                    c->pred_y = P_MEDIAN[1];
1232
                }else { /* mpeg1 at least */
1233
                    c->pred_x= P_LEFT[0];
1234
                    c->pred_y= P_LEFT[1];
1235
                }
1236
            }else{
1237
                c->pred_x= P_LEFT[0];
1238
                c->pred_y= P_LEFT[1];
1239
            }
1240

    
1241
        }
1242
        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);       
1243

    
1244
        break;
1245
    }
1246

    
1247
    /* At this point (mx,my) are full-pell and the relative displacement */
1248
    ppix = c->ref[0][0] + (my * s->linesize) + mx;
1249
        
1250
    vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16)+128)>>8;
1251

    
1252
    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
1253
//    pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 
1254
    c->mc_mb_var_sum_temp += vard;
1255
    
1256
#if 0
1257
    printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
1258
           varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
1259
#endif
1260
    if(mb_type){
1261
        if (vard <= 64 || vard < varc)
1262
            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1263
        else
1264
            c->scene_change_score+= s->qscale;
1265

    
1266
        if(mb_type == CANDIDATE_MB_TYPE_INTER){
1267
            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1268
            set_p_mv_tables(s, mx, my, 1);
1269
        }else{
1270
            mx <<=shift;
1271
            my <<=shift;
1272
        }
1273
        if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
1274
            h263_mv4_search(s, mx, my, shift);
1275

    
1276
            set_p_mv_tables(s, mx, my, 0);
1277
        }
1278
        if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
1279
            interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
1280
        }
1281
    }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1282
        if (vard <= 64 || vard < varc)
1283
            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1284
        else
1285
            c->scene_change_score+= s->qscale;
1286

    
1287
        if (vard*2 + 200 > varc)
1288
            mb_type|= CANDIDATE_MB_TYPE_INTRA;
1289
        if (varc*2 + 200 > vard){
1290
            mb_type|= CANDIDATE_MB_TYPE_INTER;
1291
            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1292
            if(s->flags&CODEC_FLAG_MV0)
1293
                if(mx || my)
1294
                    mb_type |= CANDIDATE_MB_TYPE_SKIPED; //FIXME check difference
1295
        }else{
1296
            mx <<=shift;
1297
            my <<=shift;
1298
        }
1299
        if((s->flags&CODEC_FLAG_4MV)
1300
           && !c->skip && varc>50 && vard>10){
1301
            if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
1302
                mb_type|=CANDIDATE_MB_TYPE_INTER4V;
1303

    
1304
            set_p_mv_tables(s, mx, my, 0);
1305
        }else
1306
            set_p_mv_tables(s, mx, my, 1);
1307
        if((s->flags&CODEC_FLAG_INTERLACED_ME)
1308
           && !c->skip){ //FIXME varc/d checks
1309
            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
1310
                mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1311
        }
1312
    }else{
1313
        int intra_score, i;
1314
        mb_type= CANDIDATE_MB_TYPE_INTER;
1315

    
1316
        dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1317
        if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1318
            dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1319

    
1320
        if((s->flags&CODEC_FLAG_4MV)
1321
           && !c->skip && varc>50 && vard>10){
1322
            int dmin4= h263_mv4_search(s, mx, my, shift);
1323
            if(dmin4 < dmin){
1324
                mb_type= CANDIDATE_MB_TYPE_INTER4V;
1325
                dmin=dmin4;
1326
            }
1327
        }
1328
        if((s->flags&CODEC_FLAG_INTERLACED_ME)
1329
           && !c->skip){ //FIXME varc/d checks
1330
            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
1331
            if(dmin_i < dmin){
1332
                mb_type = CANDIDATE_MB_TYPE_INTER_I;
1333
                dmin= dmin_i;
1334
            }
1335
        }
1336
                
1337
//        pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 
1338
        set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1339

    
1340
        /* get intra luma score */
1341
        if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1342
            intra_score= (varc<<8) - 500; //FIXME dont scale it down so we dont have to fix it
1343
        }else{
1344
            int mean= (sum+128)>>8;
1345
            mean*= 0x01010101;
1346
            
1347
            for(i=0; i<16; i++){
1348
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
1349
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
1350
                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
1351
                *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
1352
            }
1353

    
1354
            intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
1355
        }
1356
#if 0 //FIXME
1357
        /* get chroma score */
1358
        if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1359
            for(i=1; i<3; i++){
1360
                uint8_t *dest_c;
1361
                int mean;
1362
                
1363
                if(s->out_format == FMT_H263){
1364
                    mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;)
1365
                }else{
1366
                    mean= (s->last_dc[i] + 4)>>3;
1367
                }
1368
                dest_c = s->new_picture.data[i] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
1369
                
1370
                mean*= 0x01010101;
1371
                for(i=0; i<8; i++){
1372
                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean;
1373
                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean;
1374
                }
1375
                
1376
                intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize);
1377
            }                
1378
        }
1379
#endif
1380
        intra_score += c->mb_penalty_factor*16;
1381
        
1382
        if(intra_score < dmin){
1383
            mb_type= CANDIDATE_MB_TYPE_INTRA;
1384
            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
1385
        }else
1386
            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;
1387
        
1388
        if (vard <= 64 || vard < varc) { //FIXME
1389
            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1390
        }else{
1391
            c->scene_change_score+= s->qscale;
1392
        }
1393
    }
1394

    
1395
    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
1396
}
1397

    
1398
int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
1399
                                    int mb_x, int mb_y)
1400
{
1401
    MotionEstContext * const c= &s->me;
1402
    int mx, my, dmin;
1403
    int P[10][2];
1404
    const int shift= 1+s->quarter_sample;
1405
    const int xy= mb_x + mb_y*s->mb_stride;
1406
    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
1407
    
1408
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1409

    
1410
    c->pre_penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
1411
    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1412

    
1413
    get_limits(s, 16*mb_x, 16*mb_y);
1414
    c->skip=0;
1415

    
1416
    P_LEFT[0]       = s->p_mv_table[xy + 1][0];
1417
    P_LEFT[1]       = s->p_mv_table[xy + 1][1];
1418

    
1419
    if(P_LEFT[0]       < (c->xmin<<shift)) P_LEFT[0]       = (c->xmin<<shift);
1420

    
1421
    /* special case for first line */
1422
    if (s->first_slice_line) {
1423
        c->pred_x= P_LEFT[0];
1424
        c->pred_y= P_LEFT[1];
1425
        P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1426
        P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME 
1427
    } else {
1428
        P_TOP[0]      = s->p_mv_table[xy + s->mb_stride    ][0];
1429
        P_TOP[1]      = s->p_mv_table[xy + s->mb_stride    ][1];
1430
        P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
1431
        P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
1432
        if(P_TOP[1]      < (c->ymin<<shift)) P_TOP[1]     = (c->ymin<<shift);
1433
        if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
1434
        if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
1435
    
1436
        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1437
        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1438

    
1439
        c->pred_x = P_MEDIAN[0];
1440
        c->pred_y = P_MEDIAN[1];
1441
    }
1442

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

    
1445
    s->p_mv_table[xy][0] = mx<<shift;
1446
    s->p_mv_table[xy][1] = my<<shift;
1447
    
1448
    return dmin;
1449
}
1450

    
1451
static int ff_estimate_motion_b(MpegEncContext * s,
1452
                       int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
1453
{
1454
    MotionEstContext * const c= &s->me;
1455
    int mx, my, dmin;
1456
    int P[10][2];
1457
    const int shift= 1+s->quarter_sample;
1458
    const int mot_stride = s->mb_stride;
1459
    const int mot_xy = mb_y*mot_stride + mb_x;
1460
    uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
1461
    int mv_scale;
1462
        
1463
    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1464
    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1465
    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1466
    c->current_mv_penalty= mv_penalty;
1467

    
1468
    get_limits(s, 16*mb_x, 16*mb_y);
1469

    
1470
    switch(s->me_method) {
1471
    case ME_ZERO:
1472
    default:
1473
        no_motion_search(s, &mx, &my);
1474
        dmin = 0;
1475
        mx-= mb_x*16;
1476
        my-= mb_y*16;
1477
        break;
1478
#if 0
1479
    case ME_FULL:
1480
        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
1481
        mx-= mb_x*16;
1482
        my-= mb_y*16;
1483
        break;
1484
    case ME_LOG:
1485
        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
1486
        mx-= mb_x*16;
1487
        my-= mb_y*16;
1488
        break;
1489
    case ME_PHODS:
1490
        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
1491
        mx-= mb_x*16;
1492
        my-= mb_y*16;
1493
        break;
1494
#endif
1495
    case ME_X1:
1496
    case ME_EPZS:
1497
       {
1498
            P_LEFT[0]        = mv_table[mot_xy - 1][0];
1499
            P_LEFT[1]        = mv_table[mot_xy - 1][1];
1500

    
1501
            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
1502

    
1503
            /* special case for first line */
1504
            if (!s->first_slice_line) {
1505
                P_TOP[0] = mv_table[mot_xy - mot_stride             ][0];
1506
                P_TOP[1] = mv_table[mot_xy - mot_stride             ][1];
1507
                P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1         ][0];
1508
                P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1         ][1];
1509
                if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);
1510
                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
1511
                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
1512
        
1513
                P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1514
                P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1515
            }
1516
            c->pred_x= P_LEFT[0];
1517
            c->pred_y= P_LEFT[1];
1518
        }
1519
        
1520
        if(mv_table == s->b_forw_mv_table){
1521
            mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
1522
        }else{
1523
            mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
1524
        }
1525
        
1526
        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
1527
 
1528
        break;
1529
    }
1530
    
1531
    dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
1532
                                   
1533
    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1534
        dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
1535

    
1536
//printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
1537
//    s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1538
    mv_table[mot_xy][0]= mx;
1539
    mv_table[mot_xy][1]= my;
1540

    
1541
    return dmin;
1542
}
1543

    
1544
static inline int check_bidir_mv(MpegEncContext * s,
1545
                   int motion_fx, int motion_fy,
1546
                   int motion_bx, int motion_by,
1547
                   int pred_fx, int pred_fy,
1548
                   int pred_bx, int pred_by,
1549
                   int size, int h)
1550
{
1551
    //FIXME optimize?
1552
    //FIXME better f_code prediction (max mv & distance)
1553
    //FIXME pointers
1554
    MotionEstContext * const c= &s->me;
1555
    uint8_t * const mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
1556
    int stride= c->stride;
1557
    int uvstride= c->uvstride;
1558
    uint8_t *dest_y = c->scratchpad;
1559
    uint8_t *ptr;
1560
    int dxy;
1561
    int src_x, src_y;
1562
    int fbmin;
1563
    uint8_t **src_data= c->src[0];
1564
    uint8_t **ref_data= c->ref[0];
1565
    uint8_t **ref2_data= c->ref[2];
1566

    
1567
    if(s->quarter_sample){
1568
        dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1569
        src_x = motion_fx >> 2;
1570
        src_y = motion_fy >> 2;
1571

    
1572
        ptr = ref_data[0] + (src_y * stride) + src_x;
1573
        s->dsp.put_qpel_pixels_tab[0][dxy](dest_y    , ptr    , stride);
1574

    
1575
        dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1576
        src_x = motion_bx >> 2;
1577
        src_y = motion_by >> 2;
1578
    
1579
        ptr = ref2_data[0] + (src_y * stride) + src_x;
1580
        s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y    , ptr    , stride);
1581
    }else{
1582
        dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1583
        src_x = motion_fx >> 1;
1584
        src_y = motion_fy >> 1;
1585

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

    
1589
        dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1590
        src_x = motion_bx >> 1;
1591
        src_y = motion_by >> 1;
1592
    
1593
        ptr = ref2_data[0] + (src_y * stride) + src_x;
1594
        s->dsp.avg_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
1595
    }
1596

    
1597
    fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*c->mb_penalty_factor
1598
           +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*c->mb_penalty_factor
1599
           + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
1600
           
1601
    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1602
    }
1603
    //FIXME CHROMA !!!
1604
           
1605
    return fbmin;
1606
}
1607

    
1608
/* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1609
static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
1610
{
1611
    const int mot_stride = s->mb_stride;
1612
    const int xy = mb_y *mot_stride + mb_x;
1613
    int fbmin;
1614
    int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1615
    int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1616
    int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1617
    int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1618
    int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1619
    int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1620
    int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1621
    int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1622

    
1623
    //FIXME do refinement and add flag
1624
    
1625
    fbmin= check_bidir_mv(s, motion_fx, motion_fy,
1626
                          motion_bx, motion_by,
1627
                          pred_fx, pred_fy,
1628
                          pred_bx, pred_by,
1629
                          0, 16);
1630

    
1631
   return fbmin;
1632
}
1633

    
1634
static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
1635
{
1636
    MotionEstContext * const c= &s->me;
1637
    int P[10][2];
1638
    const int mot_stride = s->mb_stride;
1639
    const int mot_xy = mb_y*mot_stride + mb_x;
1640
    const int shift= 1+s->quarter_sample;
1641
    int dmin, i;
1642
    const int time_pp= s->pp_time;
1643
    const int time_pb= s->pb_time;
1644
    int mx, my, xmin, xmax, ymin, ymax;
1645
    int16_t (*mv_table)[2]= s->b_direct_mv_table;
1646
    
1647
    c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
1648
    ymin= xmin=(-32)>>shift;
1649
    ymax= xmax=   31>>shift;
1650

    
1651
    if(IS_8X8(s->next_picture.mb_type[mot_xy])){
1652
        s->mv_type= MV_TYPE_8X8;
1653
    }else{
1654
        s->mv_type= MV_TYPE_16X16;
1655
    }
1656

    
1657
    for(i=0; i<4; i++){
1658
        int index= s->block_index[i];
1659
        int min, max;
1660
    
1661
        c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
1662
        c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
1663
        c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1664
        c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1665
//        c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
1666
//        c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
1667

    
1668
        max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1669
        min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1670
        max+= 16*mb_x + 1; // +-1 is for the simpler rounding
1671
        min+= 16*mb_x - 1;
1672
        xmax= FFMIN(xmax, s->width - max);
1673
        xmin= FFMAX(xmin, - 16     - min);
1674

    
1675
        max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1676
        min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1677
        max+= 16*mb_y + 1; // +-1 is for the simpler rounding
1678
        min+= 16*mb_y - 1;
1679
        ymax= FFMIN(ymax, s->height - max);
1680
        ymin= FFMAX(ymin, - 16      - min);
1681
        
1682
        if(s->mv_type == MV_TYPE_16X16) break;
1683
    }
1684
    
1685
    assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1686
    
1687
    if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1688
        s->b_direct_mv_table[mot_xy][0]= 0;
1689
        s->b_direct_mv_table[mot_xy][1]= 0;
1690

    
1691
        return 256*256*256*64;
1692
    }
1693
    
1694
    c->xmin= xmin;
1695
    c->ymin= ymin;
1696
    c->xmax= xmax;
1697
    c->ymax= ymax;
1698
    c->flags     |= FLAG_DIRECT;
1699
    c->sub_flags |= FLAG_DIRECT;
1700
    c->pred_x=0;
1701
    c->pred_y=0;
1702

    
1703
    P_LEFT[0]        = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
1704
    P_LEFT[1]        = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
1705

    
1706
    /* special case for first line */
1707
    if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as its cliped
1708
        P_TOP[0]      = clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);
1709
        P_TOP[1]      = clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);
1710
        P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);
1711
        P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);
1712
    
1713
        P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1714
        P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1715
    }
1716
 
1717
    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
1718
    if(c->sub_flags&FLAG_QPEL) 
1719
        dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1720
    else
1721
        dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1722
    
1723
    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1724
        dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1725
    
1726
    get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
1727

    
1728
    s->b_direct_mv_table[mot_xy][0]= mx;
1729
    s->b_direct_mv_table[mot_xy][1]= my;
1730
    c->flags     &= ~FLAG_DIRECT;
1731
    c->sub_flags &= ~FLAG_DIRECT;
1732

    
1733
    return dmin;
1734
}
1735

    
1736
void ff_estimate_b_frame_motion(MpegEncContext * s,
1737
                             int mb_x, int mb_y)
1738
{
1739
    MotionEstContext * const c= &s->me;
1740
    const int penalty_factor= c->mb_penalty_factor;
1741
    int fmin, bmin, dmin, fbmin, bimin, fimin;
1742
    int type=0;
1743
    const int xy = mb_y*s->mb_stride + mb_x;
1744
    init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
1745

    
1746
    get_limits(s, 16*mb_x, 16*mb_y);
1747
    
1748
    c->skip=0;
1749
    if(c->avctx->me_threshold){
1750
        int vard= (check_input_motion(s, mb_x, mb_y, 0)+128)>>8;
1751
        
1752
        if(vard<c->avctx->me_threshold){
1753
//            pix = c->src[0][0];
1754
//            sum = s->dsp.pix_sum(pix, s->linesize);
1755
//            varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
1756
        
1757
//            pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;
1758
             s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
1759
/*            pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1760
            c->mb_var_sum_temp    += varc;*/
1761
            c->mc_mb_var_sum_temp += vard;
1762
/*            if (vard <= 64 || vard < varc) {
1763
                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1764
            }else{
1765
                c->scene_change_score+= s->qscale;
1766
            }*/
1767
            return;
1768
        }
1769
        if(vard<c->avctx->mb_threshold){
1770
            type= s->mb_type[mb_y*s->mb_stride + mb_x];
1771
            if(type == CANDIDATE_MB_TYPE_DIRECT){
1772
                direct_search(s, mb_x, mb_y);
1773
            }
1774
            if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
1775
                c->skip=0;
1776
                ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
1777
            }
1778
            if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
1779
                c->skip=0;
1780
                ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
1781
            }
1782
            if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
1783
                c->skip=0;
1784
                c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1785
                interlaced_search(s, 0,
1786
                                        s->b_field_mv_table[0], s->b_field_select_table[0],
1787
                                        s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
1788
            }
1789
            if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
1790
                c->skip=0;
1791
                c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
1792
                interlaced_search(s, 2,
1793
                                        s->b_field_mv_table[1], s->b_field_select_table[1],
1794
                                        s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
1795
            }
1796
            return;
1797
        }
1798
    }
1799

    
1800
    if (s->codec_id == CODEC_ID_MPEG4)
1801
        dmin= direct_search(s, mb_x, mb_y);
1802
    else
1803
        dmin= INT_MAX;
1804
//FIXME penalty stuff for non mpeg4
1805
    c->skip=0;
1806
    fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
1807
    
1808
    c->skip=0;
1809
    bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
1810
//printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1811

    
1812
    c->skip=0;
1813
    fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
1814
//printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1815
    
1816
    if(s->flags & CODEC_FLAG_INTERLACED_ME){
1817
//FIXME mb type penalty
1818
        c->skip=0;
1819
        c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1820
        fimin= interlaced_search(s, 0,
1821
                                 s->b_field_mv_table[0], s->b_field_select_table[0],
1822
                                 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
1823
        c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
1824
        bimin= interlaced_search(s, 2,
1825
                                 s->b_field_mv_table[1], s->b_field_select_table[1],
1826
                                 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
1827
    }else
1828
        fimin= bimin= INT_MAX;
1829

    
1830
    {
1831
        int score= fmin;
1832
        type = CANDIDATE_MB_TYPE_FORWARD;
1833
        
1834
        if (dmin <= score){
1835
            score = dmin;
1836
            type = CANDIDATE_MB_TYPE_DIRECT;
1837
        }
1838
        if(bmin<score){
1839
            score=bmin;
1840
            type= CANDIDATE_MB_TYPE_BACKWARD; 
1841
        }
1842
        if(fbmin<score){
1843
            score=fbmin;
1844
            type= CANDIDATE_MB_TYPE_BIDIR;
1845
        }
1846
        if(fimin<score){
1847
            score=fimin;
1848
            type= CANDIDATE_MB_TYPE_FORWARD_I;
1849
        }
1850
        if(bimin<score){
1851
            score=bimin;
1852
            type= CANDIDATE_MB_TYPE_BACKWARD_I;
1853
        }
1854
        
1855
        score= ((unsigned)(score*score + 128*256))>>16;
1856
        c->mc_mb_var_sum_temp += score;
1857
        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1858
    }
1859

    
1860
    if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1861
        type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
1862
        if(fimin < INT_MAX)
1863
            type |= CANDIDATE_MB_TYPE_FORWARD_I;
1864
        if(bimin < INT_MAX)
1865
            type |= CANDIDATE_MB_TYPE_BACKWARD_I;
1866
        if(fimin < INT_MAX && bimin < INT_MAX){
1867
            type |= CANDIDATE_MB_TYPE_BIDIR_I;
1868
        }
1869
         //FIXME something smarter
1870
        if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
1871
#if 0        
1872
        if(s->out_format == FMT_MPEG1)
1873
            type |= CANDIDATE_MB_TYPE_INTRA;
1874
#endif
1875
    }
1876

    
1877
    s->mb_type[mb_y*s->mb_stride + mb_x]= type;
1878
}
1879

    
1880
/* find best f_code for ME which do unlimited searches */
1881
int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
1882
{
1883
    if(s->me_method>=ME_EPZS){
1884
        int score[8];
1885
        int i, y;
1886
        uint8_t * fcode_tab= s->fcode_tab;
1887
        int best_fcode=-1;
1888
        int best_score=-10000000;
1889

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

    
1892
        for(y=0; y<s->mb_height; y++){
1893
            int x;
1894
            int xy= y*s->mb_stride;
1895
            for(x=0; x<s->mb_width; x++){
1896
                if(s->mb_type[xy] & type){
1897
                    int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV],
1898
                                     fcode_tab[mv_table[xy][1] + MAX_MV]);
1899
                    int j;
1900
                    
1901
                    for(j=0; j<fcode && j<8; j++){
1902
                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
1903
                            score[j]-= 170;
1904
                    }
1905
                }
1906
                xy++;
1907
            }
1908
        }
1909
        
1910
        for(i=1; i<8; i++){
1911
            if(score[i] > best_score){
1912
                best_score= score[i];
1913
                best_fcode= i;
1914
            }
1915
//            printf("%d %d\n", i, score[i]);
1916
        }
1917

    
1918
//    printf("fcode: %d type: %d\n", i, s->pict_type);
1919
        return best_fcode;
1920
/*        for(i=0; i<=MAX_FCODE; i++){
1921
            printf("%d ", mv_num[i]);
1922
        }
1923
        printf("\n");*/
1924
    }else{
1925
        return 1;
1926
    }
1927
}
1928

    
1929
void ff_fix_long_p_mvs(MpegEncContext * s)
1930
{
1931
    MotionEstContext * const c= &s->me;
1932
    const int f_code= s->f_code;
1933
    int y, range;
1934
    assert(s->pict_type==P_TYPE);
1935

    
1936
    range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
1937
    
1938
    if(s->msmpeg4_version) range= 16;
1939
    
1940
    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1941
    
1942
//printf("%d no:%d %d//\n", clip, noclip, f_code);
1943
    if(s->flags&CODEC_FLAG_4MV){
1944
        const int wrap= s->b8_stride;
1945

    
1946
        /* clip / convert to intra 8x8 type MVs */
1947
        for(y=0; y<s->mb_height; y++){
1948
            int xy= y*2*wrap;
1949
            int i= y*s->mb_stride;
1950
            int x;
1951

    
1952
            for(x=0; x<s->mb_width; x++){
1953
                if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
1954
                    int block;
1955
                    for(block=0; block<4; block++){
1956
                        int off= (block& 1) + (block>>1)*wrap;
1957
                        int mx= s->current_picture.motion_val[0][ xy + off ][0];
1958
                        int my= s->current_picture.motion_val[0][ xy + off ][1];
1959

    
1960
                        if(   mx >=range || mx <-range
1961
                           || my >=range || my <-range){
1962
                            s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
1963
                            s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
1964
                            s->current_picture.mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
1965
                        }
1966
                    }
1967
                }
1968
                xy+=2;
1969
                i++;
1970
            }
1971
        }
1972
    }
1973
}
1974

    
1975
/**
1976
 *
1977
 * @param truncate 1 for truncation, 0 for using intra
1978
 */
1979
void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select, 
1980
                     int16_t (*mv_table)[2], int f_code, int type, int truncate)
1981
{
1982
    MotionEstContext * const c= &s->me;
1983
    int y, h_range, v_range;
1984

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

    
1988
    if(s->msmpeg4_version) range= 16;
1989
    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1990

    
1991
    h_range= range;
1992
    v_range= field_select_table ? range>>1 : range;
1993

    
1994
    /* clip / convert to intra 16x16 type MVs */
1995
    for(y=0; y<s->mb_height; y++){
1996
        int x;
1997
        int xy= y*s->mb_stride;
1998
        for(x=0; x<s->mb_width; x++){
1999
            if (s->mb_type[xy] & type){    // RAL: "type" test added...
2000
                if(field_select_table==NULL || field_select_table[xy] == field_select){
2001
                    if(   mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
2002
                       || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
2003

    
2004
                        if(truncate){
2005
                            if     (mv_table[xy][0] > h_range-1) mv_table[xy][0]=  h_range-1;
2006
                            else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
2007
                            if     (mv_table[xy][1] > v_range-1) mv_table[xy][1]=  v_range-1;
2008
                            else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
2009
                        }else{
2010
                            s->mb_type[xy] &= ~type;
2011
                            s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
2012
                            mv_table[xy][0]=
2013
                            mv_table[xy][1]= 0;
2014
                        }
2015
                    }
2016
                }
2017
            }
2018
            xy++;
2019
        }
2020
    }
2021
}