Statistics
| Branch: | Revision:

ffmpeg / libavcodec / motion_est.c @ ec697587

History | View | Annotate | Download (70.4 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Motion estimation 
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2000,2001 Fabrice Bellard.
4 8f2ab833 Michael Niedermayer
 * Copyright (c) 2002-2004 Michael Niedermayer
5 de6d9b64 Fabrice Bellard
 * 
6
 *
7 ff4ec49e Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
12 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 ff4ec49e Fabrice Bellard
 * 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 45870f57 Michael Niedermayer
 *
21
 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
22 de6d9b64 Fabrice Bellard
 */
23 983e3246 Michael Niedermayer
 
24
/**
25
 * @file motion_est.c
26
 * Motion estimation.
27
 */
28
 
29 de6d9b64 Fabrice Bellard
#include <stdlib.h>
30
#include <stdio.h>
31 2d5e962b Michael Niedermayer
#include <limits.h>
32 de6d9b64 Fabrice Bellard
#include "avcodec.h"
33
#include "dsputil.h"
34
#include "mpegvideo.h"
35
36 2750b827 Michael Niedermayer
#undef NDEBUG
37
#include <assert.h>
38 1457ab52 Michael Niedermayer
39 0d21a846 Michael Niedermayer
#define SQ(a) ((a)*(a))
40 b3987771 Michael Niedermayer
41 0d21a846 Michael Niedermayer
#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 1457ab52 Michael Niedermayer
static inline int sad_hpel_motion_search(MpegEncContext * s,
48
                                  int *mx_ptr, int *my_ptr, int dmin,
49 2750b827 Michael Niedermayer
                                  int src_index, int ref_index,
50
                                  int size, int h);
51 1457ab52 Michael Niedermayer
52 af4091f1 Michael Niedermayer
static inline int update_map_generation(MotionEstContext *c)
53 1457ab52 Michael Niedermayer
{
54 af4091f1 Michael Niedermayer
    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 1457ab52 Michael Niedermayer
    }
59 af4091f1 Michael Niedermayer
    return c->map_generation;
60 1457ab52 Michael Niedermayer
}
61
62 b07a5980 Michael Niedermayer
/* shape adaptive search stuff */
63
typedef struct Minima{
64
    int height;
65
    int x, y;
66
    int checked;
67
}Minima;
68 1457ab52 Michael Niedermayer
69 b07a5980 Michael Niedermayer
static int minima_cmp(const void *a, const void *b){
70 5c91a675 Zdenek Kabelac
    const Minima *da = (const Minima *) a;
71
    const Minima *db = (const Minima *) b;
72 b07a5980 Michael Niedermayer
    
73
    return da->height - db->height;
74
}
75 1457ab52 Michael Niedermayer
76 2750b827 Michael Niedermayer
#define FLAG_QPEL   1 //must be 1
77
#define FLAG_CHROMA 2
78
#define FLAG_DIRECT 4
79 1457ab52 Michael Niedermayer
80 af4091f1 Michael Niedermayer
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 2750b827 Michael Niedermayer
    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 1457ab52 Michael Niedermayer
}
97
98 1f202b0d Michael Niedermayer
static int get_flags(MotionEstContext *c, int direct, int chroma){
99
    return   ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
100 2750b827 Michael Niedermayer
           + (direct ? FLAG_DIRECT : 0) 
101
           + (chroma ? FLAG_CHROMA : 0);
102 1457ab52 Michael Niedermayer
}
103
104 2750b827 Michael Niedermayer
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 1457ab52 Michael Niedermayer
220
#include "motion_est_template.c"
221
222 dbc56b39 Michael Niedermayer
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 1457ab52 Michael Niedermayer
void ff_init_me(MpegEncContext *s){
230 9964df63 Michael Niedermayer
    MotionEstContext * const c= &s->me;
231 1f202b0d Michael Niedermayer
    c->avctx= s->avctx;
232 9964df63 Michael Niedermayer
233 1f202b0d Michael Niedermayer
    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 2750b827 Michael Niedermayer
    
238 1f202b0d Michael Niedermayer
    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 de6d9b64 Fabrice Bellard
242 9964df63 Michael Niedermayer
/*FIXME s->no_rounding b_type*/
243 1457ab52 Michael Niedermayer
    if(s->flags&CODEC_FLAG_QPEL){
244 af4091f1 Michael Niedermayer
        c->sub_motion_search= qpel_motion_search;
245 9964df63 Michael Niedermayer
        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 1457ab52 Michael Niedermayer
    }else{
249 1f202b0d Michael Niedermayer
        if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
250 af4091f1 Michael Niedermayer
            c->sub_motion_search= hpel_motion_search;
251 1f202b0d Michael Niedermayer
        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 af4091f1 Michael Niedermayer
            c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
255 1457ab52 Michael Niedermayer
        else
256 af4091f1 Michael Niedermayer
            c->sub_motion_search= hpel_motion_search;
257 826f429a Michael Niedermayer
    }
258 dbc56b39 Michael Niedermayer
    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 2750b827 Michael Niedermayer
    if(s->linesize){
263 af4091f1 Michael Niedermayer
        c->stride  = s->linesize; 
264
        c->uvstride= s->uvlinesize;
265 67725183 Michael Niedermayer
    }else{
266 af4091f1 Michael Niedermayer
        c->stride  = 16*s->mb_width + 32;
267
        c->uvstride=  8*s->mb_width + 16;
268 67725183 Michael Niedermayer
    }
269 9964df63 Michael Niedermayer
270 dbc56b39 Michael Niedermayer
    // 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 155ec6ed Michael Niedermayer
    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 dbc56b39 Michael Niedermayer
    }
281
282 9964df63 Michael Niedermayer
    c->temp= c->scratchpad;
283 1457ab52 Michael Niedermayer
}
284
      
285 802f454e Fabrice Bellard
#if 0
286 0c1a9eda Zdenek Kabelac
static int pix_dev(uint8_t * pix, int line_size, int mean)
287 b3987771 Michael Niedermayer
{
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 802f454e Fabrice Bellard
#endif
308 b3987771 Michael Niedermayer
309 eb4b3dd3 Zdenek Kabelac
static inline void no_motion_search(MpegEncContext * s,
310
                                    int *mx_ptr, int *my_ptr)
311 de6d9b64 Fabrice Bellard
{
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 9dbcbd92 Michael Niedermayer
                              int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
319 de6d9b64 Fabrice Bellard
{
320
    int x1, y1, x2, y2, xx, yy, x, y;
321
    int mx, my, dmin, d;
322 0c1a9eda Zdenek Kabelac
    uint8_t *pix;
323 de6d9b64 Fabrice Bellard
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 1e491e29 Michael Niedermayer
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
339 de6d9b64 Fabrice Bellard
    dmin = 0x7fffffff;
340
    mx = 0;
341
    my = 0;
342
    for (y = y1; y <= y2; y++) {
343
        for (x = x1; x <= x2; x++) {
344 bb198e19 Michael Niedermayer
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x,
345
                             s->linesize, 16);
346 de6d9b64 Fabrice Bellard
            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 9dbcbd92 Michael Niedermayer
                             int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
373 de6d9b64 Fabrice Bellard
{
374
    int x1, y1, x2, y2, xx, yy, x, y;
375
    int mx, my, dmin, d;
376 0c1a9eda Zdenek Kabelac
    uint8_t *pix;
377 de6d9b64 Fabrice Bellard
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 1e491e29 Michael Niedermayer
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
402 de6d9b64 Fabrice Bellard
    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 bb198e19 Michael Niedermayer
                d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
410 de6d9b64 Fabrice Bellard
                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 9dbcbd92 Michael Niedermayer
                               int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
449 de6d9b64 Fabrice Bellard
{
450
    int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
451
    int mx, my, dminx, dminy;
452 0c1a9eda Zdenek Kabelac
    uint8_t *pix;
453 de6d9b64 Fabrice Bellard
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 1e491e29 Michael Niedermayer
    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
478 de6d9b64 Fabrice Bellard
    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 bb198e19 Michael Niedermayer
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
490 de6d9b64 Fabrice Bellard
            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 bb198e19 Michael Niedermayer
            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
499 de6d9b64 Fabrice Bellard
            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 45870f57 Michael Niedermayer
538
#define Z_THRESHOLD 256
539
540 1457ab52 Michael Niedermayer
#define CHECK_SAD_HALF_MV(suffix, x, y) \
541 0d21a846 Michael Niedermayer
{\
542 bb198e19 Michael Niedermayer
    d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
543 1457ab52 Michael Niedermayer
    d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
544 0d21a846 Michael Niedermayer
    COPY3_IF_LT(dminh, d, dx, x, dy, y)\
545
}
546 45870f57 Michael Niedermayer
547 1457ab52 Michael Niedermayer
static inline int sad_hpel_motion_search(MpegEncContext * s,
548 de6d9b64 Fabrice Bellard
                                  int *mx_ptr, int *my_ptr, int dmin,
549 2750b827 Michael Niedermayer
                                  int src_index, int ref_index,
550
                                  int size, int h)
551 de6d9b64 Fabrice Bellard
{
552 af4091f1 Michael Niedermayer
    MotionEstContext * const c= &s->me;
553
    const int penalty_factor= c->sub_penalty_factor;
554 bb198e19 Michael Niedermayer
    int mx, my, dminh;
555 0c1a9eda Zdenek Kabelac
    uint8_t *pix, *ptr;
556 af4091f1 Michael Niedermayer
    int stride= c->stride;
557
    const int flags= c->sub_flags;
558 2750b827 Michael Niedermayer
    LOAD_COMMON
559
    
560
    assert(flags == 0);
561 de6d9b64 Fabrice Bellard
562 af4091f1 Michael Niedermayer
    if(c->skip){
563 0d21a846 Michael Niedermayer
//    printf("S");
564
        *mx_ptr = 0;
565
        *my_ptr = 0;
566
        return dmin;
567
    }
568
//    printf("N");
569
        
570 af4091f1 Michael Niedermayer
    pix = c->src[src_index][0];
571 0d21a846 Michael Niedermayer
572 ba6802de Michael Niedermayer
    mx = *mx_ptr;
573
    my = *my_ptr;
574 af4091f1 Michael Niedermayer
    ptr = c->ref[ref_index][0] + (my * stride) + mx;
575 0d21a846 Michael Niedermayer
    
576 ba6802de Michael Niedermayer
    dminh = dmin;
577
578
    if (mx > xmin && mx < xmax && 
579
        my > ymin && my < ymax) {
580 0d21a846 Michael Niedermayer
        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 ba6802de Michael Niedermayer
590
        
591
        pen_x= pred_x + mx;
592
        pen_y= pred_y + my;
593
594 bb198e19 Michael Niedermayer
        ptr-= stride;
595 0d21a846 Michael Niedermayer
        if(t<=b){
596 1457ab52 Michael Niedermayer
            CHECK_SAD_HALF_MV(y2 , 0, -1)
597 0d21a846 Michael Niedermayer
            if(l<=r){
598 1457ab52 Michael Niedermayer
                CHECK_SAD_HALF_MV(xy2, -1, -1)
599 0d21a846 Michael Niedermayer
                if(t+r<=b+l){
600 1457ab52 Michael Niedermayer
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
601 bb198e19 Michael Niedermayer
                    ptr+= stride;
602 0d21a846 Michael Niedermayer
                }else{
603 bb198e19 Michael Niedermayer
                    ptr+= stride;
604 1457ab52 Michael Niedermayer
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
605 0d21a846 Michael Niedermayer
                }
606 1457ab52 Michael Niedermayer
                CHECK_SAD_HALF_MV(x2 , -1,  0)
607 0d21a846 Michael Niedermayer
            }else{
608 1457ab52 Michael Niedermayer
                CHECK_SAD_HALF_MV(xy2, +1, -1)
609 0d21a846 Michael Niedermayer
                if(t+l<=b+r){
610 1457ab52 Michael Niedermayer
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
611 bb198e19 Michael Niedermayer
                    ptr+= stride;
612 0d21a846 Michael Niedermayer
                }else{
613 bb198e19 Michael Niedermayer
                    ptr+= stride;
614 1457ab52 Michael Niedermayer
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
615 0d21a846 Michael Niedermayer
                }
616 1457ab52 Michael Niedermayer
                CHECK_SAD_HALF_MV(x2 , +1,  0)
617 0d21a846 Michael Niedermayer
            }
618
        }else{
619
            if(l<=r){
620
                if(t+l<=b+r){
621 1457ab52 Michael Niedermayer
                    CHECK_SAD_HALF_MV(xy2, -1, -1)
622 bb198e19 Michael Niedermayer
                    ptr+= stride;
623 0d21a846 Michael Niedermayer
                }else{
624 bb198e19 Michael Niedermayer
                    ptr+= stride;
625 1457ab52 Michael Niedermayer
                    CHECK_SAD_HALF_MV(xy2, +1, +1)
626 0d21a846 Michael Niedermayer
                }
627 1457ab52 Michael Niedermayer
                CHECK_SAD_HALF_MV(x2 , -1,  0)
628
                CHECK_SAD_HALF_MV(xy2, -1, +1)
629 0d21a846 Michael Niedermayer
            }else{
630
                if(t+r<=b+l){
631 1457ab52 Michael Niedermayer
                    CHECK_SAD_HALF_MV(xy2, +1, -1)
632 bb198e19 Michael Niedermayer
                    ptr+= stride;
633 0d21a846 Michael Niedermayer
                }else{
634 bb198e19 Michael Niedermayer
                    ptr+= stride;
635 1457ab52 Michael Niedermayer
                    CHECK_SAD_HALF_MV(xy2, -1, +1)
636 0d21a846 Michael Niedermayer
                }
637 1457ab52 Michael Niedermayer
                CHECK_SAD_HALF_MV(x2 , +1,  0)
638
                CHECK_SAD_HALF_MV(xy2, +1, +1)
639 0d21a846 Michael Niedermayer
            }
640 1457ab52 Michael Niedermayer
            CHECK_SAD_HALF_MV(y2 ,  0, +1)
641 0d21a846 Michael Niedermayer
        }
642
        mx+=dx;
643
        my+=dy;
644 ba6802de Michael Niedermayer
645
    }else{
646 0d21a846 Michael Niedermayer
        mx<<=1;
647
        my<<=1;
648 ba6802de Michael Niedermayer
    }
649
650
    *mx_ptr = mx;
651
    *my_ptr = my;
652 0d21a846 Michael Niedermayer
    return dminh;
653 ba6802de Michael Niedermayer
}
654
655 0d21a846 Michael Niedermayer
static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
656 ba6802de Michael Niedermayer
{
657 7bc9090a Michael Niedermayer
    const int xy= s->mb_x + s->mb_y*s->mb_stride;
658 ba6802de Michael Niedermayer
    
659 9dbcbd92 Michael Niedermayer
    s->p_mv_table[xy][0] = mx;
660
    s->p_mv_table[xy][1] = my;
661 ba6802de Michael Niedermayer
662
    /* has allready been set to the 4 MV if 4MV is done */
663 0d21a846 Michael Niedermayer
    if(mv4){
664 ba6802de Michael Niedermayer
        int mot_xy= s->block_index[0];
665
666 8d7ec294 Wolfgang Hesseler
        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 ba6802de Michael Niedermayer
671 137c8468 Michael Niedermayer
        mot_xy += s->b8_stride;
672 8d7ec294 Wolfgang Hesseler
        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 ba6802de Michael Niedermayer
    }
677
}
678
679 ebbcdc9a Michael Niedermayer
/**
680
 * get fullpel ME search limits.
681
 */
682 bb198e19 Michael Niedermayer
static inline void get_limits(MpegEncContext *s, int x, int y)
683 de6d9b64 Fabrice Bellard
{
684 af4091f1 Michael Niedermayer
    MotionEstContext * const c= &s->me;
685 bb198e19 Michael Niedermayer
/*
686 1f202b0d Michael Niedermayer
    if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
687 af4091f1 Michael Niedermayer
    else                   c->range= 16;
688 bb198e19 Michael Niedermayer
*/
689 de6d9b64 Fabrice Bellard
    if (s->unrestricted_mv) {
690 af4091f1 Michael Niedermayer
        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 de6d9b64 Fabrice Bellard
    } else {
695 af4091f1 Michael Niedermayer
        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 de6d9b64 Fabrice Bellard
    }
700 9dbcbd92 Michael Niedermayer
}
701
702 1f202b0d Michael Niedermayer
static inline void init_mv4_ref(MotionEstContext *c){
703
    const int stride= c->stride;
704 9964df63 Michael Niedermayer
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 bb198e19 Michael Niedermayer
static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
714 0d21a846 Michael Niedermayer
{
715 2750b827 Michael Niedermayer
    MotionEstContext * const c= &s->me;
716 bb198e19 Michael Niedermayer
    const int size= 1;
717
    const int h=8;
718 0d21a846 Michael Niedermayer
    int block;
719
    int P[10][2];
720 67725183 Michael Niedermayer
    int dmin_sum=0, mx4_sum=0, my4_sum=0;
721 ca7d05d5 Michael Niedermayer
    int same=1;
722 1f202b0d Michael Niedermayer
    const int stride= c->stride;
723
    const int uvstride= c->uvstride;
724 af4091f1 Michael Niedermayer
    uint8_t *mv_penalty= c->current_mv_penalty;
725 2750b827 Michael Niedermayer
726 1f202b0d Michael Niedermayer
    init_mv4_ref(c);
727 2750b827 Michael Niedermayer
    
728 0d21a846 Michael Niedermayer
    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 137c8468 Michael Niedermayer
        const int mot_stride = s->b8_stride;
734 0d21a846 Michael Niedermayer
        const int mot_xy = s->block_index[block];
735 bb198e19 Michael Niedermayer
736 8d7ec294 Wolfgang Hesseler
        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 0d21a846 Michael Niedermayer
739 af4091f1 Michael Niedermayer
        if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
740 0d21a846 Michael Niedermayer
741
        /* special case for first line */
742 9c3d33d6 Michael Niedermayer
        if (s->first_slice_line && block<2) {
743 af4091f1 Michael Niedermayer
            c->pred_x= pred_x4= P_LEFT[0];
744
            c->pred_y= pred_y4= P_LEFT[1];
745 0d21a846 Michael Niedermayer
        } else {
746 8d7ec294 Wolfgang Hesseler
            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 af4091f1 Michael Niedermayer
            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 0d21a846 Michael Niedermayer
    
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 af4091f1 Michael Niedermayer
            c->pred_x= pred_x4 = P_MEDIAN[0];
759
            c->pred_y= pred_y4 = P_MEDIAN[1];
760 0d21a846 Michael Niedermayer
        }
761
        P_MV1[0]= mx;
762
        P_MV1[1]= my;
763
764 2750b827 Michael Niedermayer
        dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
765 0d21a846 Michael Niedermayer
766 af4091f1 Michael Niedermayer
        dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
767 cb009a8c Michael Niedermayer
        
768 2750b827 Michael Niedermayer
        if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
769 67725183 Michael Niedermayer
            int dxy;
770 bb198e19 Michael Niedermayer
            const int offset= ((block&1) + (block>>1)*stride)*8;
771 af4091f1 Michael Niedermayer
            uint8_t *dest_y = c->scratchpad + offset;
772 67725183 Michael Niedermayer
            if(s->quarter_sample){
773 2750b827 Michael Niedermayer
                uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
774 67725183 Michael Niedermayer
                dxy = ((my4 & 3) << 2) | (mx4 & 3);
775
776
                if(s->no_rounding)
777 9c3d33d6 Michael Niedermayer
                    s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y   , ref    , stride);
778 67725183 Michael Niedermayer
                else
779 bb198e19 Michael Niedermayer
                    s->dsp.put_qpel_pixels_tab       [1][dxy](dest_y   , ref    , stride);
780 67725183 Michael Niedermayer
            }else{
781 2750b827 Michael Niedermayer
                uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
782 67725183 Michael Niedermayer
                dxy = ((my4 & 1) << 1) | (mx4 & 1);
783
784
                if(s->no_rounding)
785 bb198e19 Michael Niedermayer
                    s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y    , ref    , stride, h);
786 67725183 Michael Niedermayer
                else
787 bb198e19 Michael Niedermayer
                    s->dsp.put_pixels_tab       [1][dxy](dest_y    , ref    , stride, h);
788 67725183 Michael Niedermayer
            }
789 af4091f1 Michael Niedermayer
            dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
790 67725183 Michael Niedermayer
        }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 8d7ec294 Wolfgang Hesseler
        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 ca7d05d5 Michael Niedermayer
804
        if(mx4 != mx || my4 != my) same=0;
805 0d21a846 Michael Niedermayer
    }
806 67725183 Michael Niedermayer
    
807 ca7d05d5 Michael Niedermayer
    if(same)
808
        return INT_MAX;
809
    
810 cb009a8c Michael Niedermayer
    if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
811 af4091f1 Michael Niedermayer
        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 67725183 Michael Niedermayer
    }
813
    
814 1f202b0d Michael Niedermayer
    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
815 67725183 Michael Niedermayer
        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 af4091f1 Michael Niedermayer
            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 67725183 Michael Niedermayer
        }else{
829 af4091f1 Michael Niedermayer
            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 67725183 Michael Niedermayer
        }
832
833 af4091f1 Michael Niedermayer
        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 67725183 Michael Niedermayer
    }
836 2750b827 Michael Niedermayer
    
837 af4091f1 Michael Niedermayer
    c->pred_x= mx;
838
    c->pred_y= my;
839 67725183 Michael Niedermayer
840 1f202b0d Michael Niedermayer
    switch(c->avctx->mb_cmp&0xFF){
841 67725183 Michael Niedermayer
    /*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 af4091f1 Michael Niedermayer
        return dmin_sum+ 11*c->mb_penalty_factor;
847 67725183 Michael Niedermayer
    }
848 0d21a846 Michael Niedermayer
}
849
850 9964df63 Michael Niedermayer
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 2750b827 Michael Niedermayer
static int interlaced_search(MpegEncContext *s, int ref_index, 
864 f20f8a8b Michael Niedermayer
                             int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
865 bb198e19 Michael Niedermayer
{
866 2750b827 Michael Niedermayer
    MotionEstContext * const c= &s->me;
867 bb198e19 Michael Niedermayer
    const int size=0;
868
    const int h=8;
869
    int block;
870
    int P[10][2];
871 2750b827 Michael Niedermayer
    uint8_t * const mv_penalty= c->current_mv_penalty;
872 bb198e19 Michael Niedermayer
    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 2750b827 Michael Niedermayer
    c->ymin>>=1;
880
    c->ymax>>=1;
881
    c->stride<<=1;
882
    c->uvstride<<=1;
883 9964df63 Michael Niedermayer
    init_interlaced_ref(s, ref_index);
884 bb198e19 Michael Niedermayer
    
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 2750b827 Michael Niedermayer
            int dmin, mx_i, my_i;
892 bb198e19 Michael Niedermayer
            int16_t (*mv_table)[2]= mv_tables[block][field_select];
893
            
894 f20f8a8b Michael Niedermayer
            if(user_field_select){
895
                if(field_select_tables[block][xy] != field_select)
896
                    continue;
897
            }
898
            
899 bb198e19 Michael Niedermayer
            P_LEFT[0] = mv_table[xy - 1][0];
900
            P_LEFT[1] = mv_table[xy - 1][1];
901 2750b827 Michael Niedermayer
            if(P_LEFT[0]       > (c->xmax<<1)) P_LEFT[0]       = (c->xmax<<1);
902 bb198e19 Michael Niedermayer
            
903 af4091f1 Michael Niedermayer
            c->pred_x= P_LEFT[0];
904
            c->pred_y= P_LEFT[1];
905 bb198e19 Michael Niedermayer
            
906 9c3d33d6 Michael Niedermayer
            if(!s->first_slice_line){
907 bb198e19 Michael Niedermayer
                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 2750b827 Michael Niedermayer
                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 bb198e19 Michael Niedermayer
    
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 2750b827 Michael Niedermayer
            dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
923 bb198e19 Michael Niedermayer
924 2750b827 Michael Niedermayer
            dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
925 bb198e19 Michael Niedermayer
            
926
            mv_table[xy][0]= mx_i;
927
            mv_table[xy][1]= my_i;
928
            
929 2750b827 Michael Niedermayer
            if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
930 bb198e19 Michael Niedermayer
                int dxy;
931
932
                //FIXME chroma ME
933 2750b827 Michael Niedermayer
                uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
934 bb198e19 Michael Niedermayer
                dxy = ((my_i & 1) << 1) | (mx_i & 1);
935
936
                if(s->no_rounding){
937 2750b827 Michael Niedermayer
                    s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref    , stride, h);
938 bb198e19 Michael Niedermayer
                }else{
939 2750b827 Michael Niedermayer
                    s->dsp.put_pixels_tab       [size][dxy](c->scratchpad, ref    , stride, h);
940 bb198e19 Michael Niedermayer
                }
941 2750b827 Michael Niedermayer
                dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
942 af4091f1 Michael Niedermayer
                dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
943 bb198e19 Michael Niedermayer
            }else
944 2750b827 Michael Niedermayer
                dmin+= c->mb_penalty_factor; //field_select bits
945 bb198e19 Michael Niedermayer
                
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 2750b827 Michael Niedermayer
    c->ymin<<=1;
967
    c->ymax<<=1;
968
    c->stride>>=1;
969
    c->uvstride>>=1;
970 bb198e19 Michael Niedermayer
971
    if(same)
972
        return INT_MAX;
973
    
974 1f202b0d Michael Niedermayer
    switch(c->avctx->mb_cmp&0xFF){
975 bb198e19 Michael Niedermayer
    /*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 2750b827 Michael Niedermayer
        return dmin_sum+ 11*c->mb_penalty_factor;
981 bb198e19 Michael Niedermayer
    }
982
}
983
984 ae1dbde1 Michael Niedermayer
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 f4f3223f Michael Niedermayer
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 9964df63 Michael Niedermayer
    int x, y, i;
1004 f4f3223f Michael Niedermayer
    int d=0;
1005
    me_cmp_func cmpf= s->dsp.sse[0];
1006
    me_cmp_func chroma_cmpf= s->dsp.sse[1];
1007
    
1008 ae1dbde1 Michael Niedermayer
    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 f4f3223f Michael Niedermayer
    assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
1013
    
1014 ae1dbde1 Michael Niedermayer
    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 f4f3223f Michael Niedermayer
    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 aacc6615 Michael Niedermayer
        
1026 1545acea Michael Niedermayer
        if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
1027 1f202b0d Michael Niedermayer
            av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
1028 ae1dbde1 Michael Niedermayer
            return INT_MAX;
1029 1545acea Michael Niedermayer
        }
1030 9964df63 Michael Niedermayer
1031 f4f3223f Michael Niedermayer
        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 f20f8a8b Michael Niedermayer
            init_interlaced_ref(s, 0);
1037
1038 f4f3223f Michael Niedermayer
            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 f20f8a8b Michael Niedermayer
            init_interlaced_ref(s, 2);
1065
1066 f4f3223f Michael Niedermayer
            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 9964df63 Michael Niedermayer
    }else if(IS_8X8(mb_type)){
1087 1545acea Michael Niedermayer
        if(!(s->flags & CODEC_FLAG_4MV)){
1088 1f202b0d Michael Niedermayer
            av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
1089 ae1dbde1 Michael Niedermayer
            return INT_MAX;
1090 1545acea Michael Niedermayer
        }
1091 9964df63 Michael Niedermayer
        cmpf= s->dsp.sse[1];
1092
        chroma_cmpf= s->dsp.sse[1];
1093 1f202b0d Michael Niedermayer
        init_mv4_ref(c);
1094 9964df63 Michael Niedermayer
        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 f4f3223f Michael Niedermayer
    }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 9dbcbd92 Michael Niedermayer
void ff_estimate_p_frame_motion(MpegEncContext * s,
1131
                                int mb_x, int mb_y)
1132
{
1133 2750b827 Michael Niedermayer
    MotionEstContext * const c= &s->me;
1134 0c1a9eda Zdenek Kabelac
    uint8_t *pix, *ppix;
1135 f20f8a8b Michael Niedermayer
    int sum, varc, vard, mx, my, dmin;
1136 0d21a846 Michael Niedermayer
    int P[10][2];
1137 9dbcbd92 Michael Niedermayer
    const int shift= 1+s->quarter_sample;
1138
    int mb_type=0;
1139 1e491e29 Michael Niedermayer
    Picture * const pic= &s->current_picture;
1140 2750b827 Michael Niedermayer
    
1141 af4091f1 Michael Niedermayer
    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
1142 bb198e19 Michael Niedermayer
1143 1457ab52 Michael Niedermayer
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1144 af4091f1 Michael Niedermayer
    assert(s->linesize == c->stride);
1145
    assert(s->uvlinesize == c->uvstride);
1146 1457ab52 Michael Niedermayer
1147 26efc54e Michael Niedermayer
    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 af4091f1 Michael Niedermayer
    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1151 9dbcbd92 Michael Niedermayer
1152 bb198e19 Michael Niedermayer
    get_limits(s, 16*mb_x, 16*mb_y);
1153 af4091f1 Michael Niedermayer
    c->skip=0;
1154 9dbcbd92 Michael Niedermayer
1155 f20f8a8b Michael Niedermayer
    /* 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 1f202b0d Michael Niedermayer
    c->mb_var_sum_temp += varc;
1163 f20f8a8b Michael Niedermayer
1164 1f202b0d Michael Niedermayer
    if(c->avctx->me_threshold){
1165 f4f3223f Michael Niedermayer
        vard= (check_input_motion(s, mb_x, mb_y, 1)+128)>>8;
1166
        
1167 1f202b0d Michael Niedermayer
        if(vard<c->avctx->me_threshold){
1168 f4f3223f Michael Niedermayer
            pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
1169 1f202b0d Michael Niedermayer
            c->mc_mb_var_sum_temp += vard;
1170 f4f3223f Michael Niedermayer
            if (vard <= 64 || vard < varc) { //FIXME
1171 1f202b0d Michael Niedermayer
                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1172 f4f3223f Michael Niedermayer
            }else{
1173 1f202b0d Michael Niedermayer
                c->scene_change_score+= s->qscale;
1174 f4f3223f Michael Niedermayer
            }
1175
            return;
1176
        }
1177 1f202b0d Michael Niedermayer
        if(vard<c->avctx->mb_threshold)
1178 f20f8a8b Michael Niedermayer
            mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
1179 f4f3223f Michael Niedermayer
    }
1180
1181 e4986da9 Juanjo
    switch(s->me_method) {
1182 de6d9b64 Fabrice Bellard
    case ME_ZERO:
1183
    default:
1184
        no_motion_search(s, &mx, &my);
1185 0d21a846 Michael Niedermayer
        mx-= mb_x*16;
1186
        my-= mb_y*16;
1187 de6d9b64 Fabrice Bellard
        dmin = 0;
1188
        break;
1189 bb198e19 Michael Niedermayer
#if 0
1190 de6d9b64 Fabrice Bellard
    case ME_FULL:
1191 bb198e19 Michael Niedermayer
        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
1192 0d21a846 Michael Niedermayer
        mx-= mb_x*16;
1193
        my-= mb_y*16;
1194 de6d9b64 Fabrice Bellard
        break;
1195
    case ME_LOG:
1196 bb198e19 Michael Niedermayer
        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
1197 0d21a846 Michael Niedermayer
        mx-= mb_x*16;
1198
        my-= mb_y*16;
1199 de6d9b64 Fabrice Bellard
        break;
1200
    case ME_PHODS:
1201 bb198e19 Michael Niedermayer
        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
1202 0d21a846 Michael Niedermayer
        mx-= mb_x*16;
1203
        my-= mb_y*16;
1204 de6d9b64 Fabrice Bellard
        break;
1205 bb198e19 Michael Niedermayer
#endif
1206 174489bd Michael Niedermayer
    case ME_X1:
1207 45870f57 Michael Niedermayer
    case ME_EPZS:
1208 174489bd Michael Niedermayer
       {
1209 137c8468 Michael Niedermayer
            const int mot_stride = s->b8_stride;
1210 ba6802de Michael Niedermayer
            const int mot_xy = s->block_index[0];
1211 174489bd Michael Niedermayer
1212 8d7ec294 Wolfgang Hesseler
            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 174489bd Michael Niedermayer
1215 af4091f1 Michael Niedermayer
            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
1216 c5b1c10a Michael Niedermayer
1217 9c3d33d6 Michael Niedermayer
            if(!s->first_slice_line) {
1218 8d7ec294 Wolfgang Hesseler
                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 af4091f1 Michael Niedermayer
                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 c5b1c10a Michael Niedermayer
        
1226 0d21a846 Michael Niedermayer
                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 2750b827 Michael Niedermayer
                    c->pred_x = P_MEDIAN[0];
1231
                    c->pred_y = P_MEDIAN[1];
1232 0d21a846 Michael Niedermayer
                }else { /* mpeg1 at least */
1233 2750b827 Michael Niedermayer
                    c->pred_x= P_LEFT[0];
1234
                    c->pred_y= P_LEFT[1];
1235 0d21a846 Michael Niedermayer
                }
1236 f931ff7b Michael Niedermayer
            }else{
1237 2750b827 Michael Niedermayer
                c->pred_x= P_LEFT[0];
1238
                c->pred_y= P_LEFT[1];
1239 174489bd Michael Niedermayer
            }
1240 f931ff7b Michael Niedermayer
1241 c5b1c10a Michael Niedermayer
        }
1242 26efc54e Michael Niedermayer
        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);       
1243 2750b827 Michael Niedermayer
1244 45870f57 Michael Niedermayer
        break;
1245 de6d9b64 Fabrice Bellard
    }
1246
1247 0d21a846 Michael Niedermayer
    /* At this point (mx,my) are full-pell and the relative displacement */
1248 2750b827 Michael Niedermayer
    ppix = c->ref[0][0] + (my * s->linesize) + mx;
1249 f20f8a8b Michael Niedermayer
        
1250 bb198e19 Michael Niedermayer
    vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16)+128)>>8;
1251 1fb4890b Michael Niedermayer
1252 7bc9090a Michael Niedermayer
    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 1f202b0d Michael Niedermayer
    c->mc_mb_var_sum_temp += vard;
1255 e4986da9 Juanjo
    
1256 de6d9b64 Fabrice Bellard
#if 0
1257 e03c341e Juanjo
    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 de6d9b64 Fabrice Bellard
#endif
1260 f20f8a8b Michael Niedermayer
    if(mb_type){
1261
        if (vard <= 64 || vard < varc)
1262 1f202b0d Michael Niedermayer
            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1263 f20f8a8b Michael Niedermayer
        else
1264 1f202b0d Michael Niedermayer
            c->scene_change_score+= s->qscale;
1265 f20f8a8b Michael Niedermayer
1266
        if(mb_type == CANDIDATE_MB_TYPE_INTER){
1267 af4091f1 Michael Niedermayer
            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1268 f20f8a8b Michael Niedermayer
            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 1f202b0d Michael Niedermayer
    }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1282 1fb4890b Michael Niedermayer
        if (vard <= 64 || vard < varc)
1283 1f202b0d Michael Niedermayer
            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1284 1fb4890b Michael Niedermayer
        else
1285 1f202b0d Michael Niedermayer
            c->scene_change_score+= s->qscale;
1286 1fb4890b Michael Niedermayer
1287 ba6802de Michael Niedermayer
        if (vard*2 + 200 > varc)
1288 bb198e19 Michael Niedermayer
            mb_type|= CANDIDATE_MB_TYPE_INTRA;
1289 ba6802de Michael Niedermayer
        if (varc*2 + 200 > vard){
1290 bb198e19 Michael Niedermayer
            mb_type|= CANDIDATE_MB_TYPE_INTER;
1291 af4091f1 Michael Niedermayer
            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1292 ca7d05d5 Michael Niedermayer
            if(s->flags&CODEC_FLAG_MV0)
1293
                if(mx || my)
1294 bb198e19 Michael Niedermayer
                    mb_type |= CANDIDATE_MB_TYPE_SKIPED; //FIXME check difference
1295 c60cf138 Michael Niedermayer
        }else{
1296 1457ab52 Michael Niedermayer
            mx <<=shift;
1297
            my <<=shift;
1298 de6d9b64 Fabrice Bellard
        }
1299 0d21a846 Michael Niedermayer
        if((s->flags&CODEC_FLAG_4MV)
1300 af4091f1 Michael Niedermayer
           && !c->skip && varc>50 && vard>10){
1301 bb198e19 Michael Niedermayer
            if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
1302
                mb_type|=CANDIDATE_MB_TYPE_INTER4V;
1303 0d21a846 Michael Niedermayer
1304
            set_p_mv_tables(s, mx, my, 0);
1305
        }else
1306
            set_p_mv_tables(s, mx, my, 1);
1307 bb198e19 Michael Niedermayer
        if((s->flags&CODEC_FLAG_INTERLACED_ME)
1308 af4091f1 Michael Niedermayer
           && !c->skip){ //FIXME varc/d checks
1309 f20f8a8b Michael Niedermayer
            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
1310 bb198e19 Michael Niedermayer
                mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1311
        }
1312 ba6802de Michael Niedermayer
    }else{
1313 f7190f73 Michael Niedermayer
        int intra_score, i;
1314 bb198e19 Michael Niedermayer
        mb_type= CANDIDATE_MB_TYPE_INTER;
1315 0d21a846 Michael Niedermayer
1316 af4091f1 Michael Niedermayer
        dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1317 1f202b0d Michael Niedermayer
        if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1318 155ec6ed Michael Niedermayer
            dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1319 67725183 Michael Niedermayer
1320
        if((s->flags&CODEC_FLAG_4MV)
1321 af4091f1 Michael Niedermayer
           && !c->skip && varc>50 && vard>10){
1322 bb198e19 Michael Niedermayer
            int dmin4= h263_mv4_search(s, mx, my, shift);
1323 67725183 Michael Niedermayer
            if(dmin4 < dmin){
1324 bb198e19 Michael Niedermayer
                mb_type= CANDIDATE_MB_TYPE_INTER4V;
1325 67725183 Michael Niedermayer
                dmin=dmin4;
1326 e4986da9 Juanjo
            }
1327 67725183 Michael Niedermayer
        }
1328 bb198e19 Michael Niedermayer
        if((s->flags&CODEC_FLAG_INTERLACED_ME)
1329 af4091f1 Michael Niedermayer
           && !c->skip){ //FIXME varc/d checks
1330 f20f8a8b Michael Niedermayer
            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
1331 bb198e19 Michael Niedermayer
            if(dmin_i < dmin){
1332
                mb_type = CANDIDATE_MB_TYPE_INTER_I;
1333
                dmin= dmin_i;
1334
            }
1335
        }
1336 f7190f73 Michael Niedermayer
                
1337
//        pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 
1338 bb198e19 Michael Niedermayer
        set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1339 f7190f73 Michael Niedermayer
1340
        /* get intra luma score */
1341 1f202b0d Michael Niedermayer
        if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1342 f7190f73 Michael Niedermayer
            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 af4091f1 Michael Niedermayer
                *(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 f7190f73 Michael Niedermayer
            }
1353
1354 af4091f1 Michael Niedermayer
            intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
1355 f7190f73 Michael Niedermayer
        }
1356
#if 0 //FIXME
1357
        /* get chroma score */
1358 1f202b0d Michael Niedermayer
        if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1359 f7190f73 Michael Niedermayer
            for(i=1; i<3; i++){
1360
                uint8_t *dest_c;
1361
                int mean;
1362
                
1363
                if(s->out_format == FMT_H263){
1364 137c8468 Michael Niedermayer
                    mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;)
1365 f7190f73 Michael Niedermayer
                }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 af4091f1 Michael Niedermayer
                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean;
1373
                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean;
1374 f7190f73 Michael Niedermayer
                }
1375
                
1376 af4091f1 Michael Niedermayer
                intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize);
1377 f7190f73 Michael Niedermayer
            }                
1378
        }
1379
#endif
1380 af4091f1 Michael Niedermayer
        intra_score += c->mb_penalty_factor*16;
1381 f7190f73 Michael Niedermayer
        
1382
        if(intra_score < dmin){
1383 bb198e19 Michael Niedermayer
            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 f7190f73 Michael Niedermayer
        }else
1386
            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;
1387 67725183 Michael Niedermayer
        
1388 f7190f73 Michael Niedermayer
        if (vard <= 64 || vard < varc) { //FIXME
1389 1f202b0d Michael Niedermayer
            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1390 ba6802de Michael Niedermayer
        }else{
1391 1f202b0d Michael Niedermayer
            c->scene_change_score+= s->qscale;
1392 ba6802de Michael Niedermayer
        }
1393 de6d9b64 Fabrice Bellard
    }
1394 ba6802de Michael Niedermayer
1395 7bc9090a Michael Niedermayer
    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
1396 9dbcbd92 Michael Niedermayer
}
1397
1398 f5fb6b34 Michael Niedermayer
int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
1399
                                    int mb_x, int mb_y)
1400
{
1401 2750b827 Michael Niedermayer
    MotionEstContext * const c= &s->me;
1402 bb198e19 Michael Niedermayer
    int mx, my, dmin;
1403 f5fb6b34 Michael Niedermayer
    int P[10][2];
1404
    const int shift= 1+s->quarter_sample;
1405 7bc9090a Michael Niedermayer
    const int xy= mb_x + mb_y*s->mb_stride;
1406 af4091f1 Michael Niedermayer
    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
1407 f5fb6b34 Michael Niedermayer
    
1408
    assert(s->quarter_sample==0 || s->quarter_sample==1);
1409
1410 26efc54e Michael Niedermayer
    c->pre_penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
1411 af4091f1 Michael Niedermayer
    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1412 f5fb6b34 Michael Niedermayer
1413 bb198e19 Michael Niedermayer
    get_limits(s, 16*mb_x, 16*mb_y);
1414 af4091f1 Michael Niedermayer
    c->skip=0;
1415 f5fb6b34 Michael Niedermayer
1416
    P_LEFT[0]       = s->p_mv_table[xy + 1][0];
1417
    P_LEFT[1]       = s->p_mv_table[xy + 1][1];
1418
1419 af4091f1 Michael Niedermayer
    if(P_LEFT[0]       < (c->xmin<<shift)) P_LEFT[0]       = (c->xmin<<shift);
1420 f5fb6b34 Michael Niedermayer
1421
    /* special case for first line */
1422 9c3d33d6 Michael Niedermayer
    if (s->first_slice_line) {
1423 2750b827 Michael Niedermayer
        c->pred_x= P_LEFT[0];
1424
        c->pred_y= P_LEFT[1];
1425 f931ff7b Michael Niedermayer
        P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1426
        P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME 
1427 f5fb6b34 Michael Niedermayer
    } else {
1428 7bc9090a Michael Niedermayer
        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 af4091f1 Michael Niedermayer
        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 f5fb6b34 Michael Niedermayer
    
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 2750b827 Michael Niedermayer
        c->pred_x = P_MEDIAN[0];
1440
        c->pred_y = P_MEDIAN[1];
1441 f5fb6b34 Michael Niedermayer
    }
1442 2750b827 Michael Niedermayer
1443 26efc54e Michael Niedermayer
    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);       
1444 f931ff7b Michael Niedermayer
1445 f5fb6b34 Michael Niedermayer
    s->p_mv_table[xy][0] = mx<<shift;
1446
    s->p_mv_table[xy][1] = my<<shift;
1447
    
1448
    return dmin;
1449
}
1450
1451 5c91a675 Zdenek Kabelac
static int ff_estimate_motion_b(MpegEncContext * s,
1452 2750b827 Michael Niedermayer
                       int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
1453 9dbcbd92 Michael Niedermayer
{
1454 af4091f1 Michael Niedermayer
    MotionEstContext * const c= &s->me;
1455 bb198e19 Michael Niedermayer
    int mx, my, dmin;
1456 0d21a846 Michael Niedermayer
    int P[10][2];
1457 9dbcbd92 Michael Niedermayer
    const int shift= 1+s->quarter_sample;
1458 7bc9090a Michael Niedermayer
    const int mot_stride = s->mb_stride;
1459
    const int mot_xy = mb_y*mot_stride + mb_x;
1460 af4091f1 Michael Niedermayer
    uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
1461 b07a5980 Michael Niedermayer
    int mv_scale;
1462 1457ab52 Michael Niedermayer
        
1463 26efc54e Michael Niedermayer
    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 af4091f1 Michael Niedermayer
    c->current_mv_penalty= mv_penalty;
1467 1457ab52 Michael Niedermayer
1468 bb198e19 Michael Niedermayer
    get_limits(s, 16*mb_x, 16*mb_y);
1469 9dbcbd92 Michael Niedermayer
1470
    switch(s->me_method) {
1471
    case ME_ZERO:
1472
    default:
1473
        no_motion_search(s, &mx, &my);
1474
        dmin = 0;
1475 0d21a846 Michael Niedermayer
        mx-= mb_x*16;
1476
        my-= mb_y*16;
1477 9dbcbd92 Michael Niedermayer
        break;
1478 bb198e19 Michael Niedermayer
#if 0
1479 9dbcbd92 Michael Niedermayer
    case ME_FULL:
1480 bb198e19 Michael Niedermayer
        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
1481 0d21a846 Michael Niedermayer
        mx-= mb_x*16;
1482
        my-= mb_y*16;
1483 9dbcbd92 Michael Niedermayer
        break;
1484
    case ME_LOG:
1485 bb198e19 Michael Niedermayer
        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
1486 0d21a846 Michael Niedermayer
        mx-= mb_x*16;
1487
        my-= mb_y*16;
1488 9dbcbd92 Michael Niedermayer
        break;
1489
    case ME_PHODS:
1490 bb198e19 Michael Niedermayer
        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
1491 0d21a846 Michael Niedermayer
        mx-= mb_x*16;
1492
        my-= mb_y*16;
1493 9dbcbd92 Michael Niedermayer
        break;
1494 bb198e19 Michael Niedermayer
#endif
1495 9dbcbd92 Michael Niedermayer
    case ME_X1:
1496
    case ME_EPZS:
1497
       {
1498 0d21a846 Michael Niedermayer
            P_LEFT[0]        = mv_table[mot_xy - 1][0];
1499
            P_LEFT[1]        = mv_table[mot_xy - 1][1];
1500 9dbcbd92 Michael Niedermayer
1501 af4091f1 Michael Niedermayer
            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
1502 9dbcbd92 Michael Niedermayer
1503
            /* special case for first line */
1504 9c3d33d6 Michael Niedermayer
            if (!s->first_slice_line) {
1505 0d21a846 Michael Niedermayer
                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 af4091f1 Michael Niedermayer
                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 9dbcbd92 Michael Niedermayer
        
1513 0d21a846 Michael Niedermayer
                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 9dbcbd92 Michael Niedermayer
            }
1516 af4091f1 Michael Niedermayer
            c->pred_x= P_LEFT[0];
1517
            c->pred_y= P_LEFT[1];
1518 9dbcbd92 Michael Niedermayer
        }
1519 b07a5980 Michael Niedermayer
        
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 26efc54e Michael Niedermayer
        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
1527 9dbcbd92 Michael Niedermayer
 
1528
        break;
1529
    }
1530
    
1531 af4091f1 Michael Niedermayer
    dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
1532 67725183 Michael Niedermayer
                                   
1533 1f202b0d Michael Niedermayer
    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1534 155ec6ed Michael Niedermayer
        dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
1535 67725183 Michael Niedermayer
1536 0d21a846 Michael Niedermayer
//printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
1537 9dbcbd92 Michael Niedermayer
//    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 1457ab52 Michael Niedermayer
1541 91029be7 Michael Niedermayer
    return dmin;
1542 de6d9b64 Fabrice Bellard
}
1543
1544 2750b827 Michael Niedermayer
static inline int check_bidir_mv(MpegEncContext * s,
1545 91029be7 Michael Niedermayer
                   int motion_fx, int motion_fy,
1546
                   int motion_bx, int motion_by,
1547
                   int pred_fx, int pred_fy,
1548 bb198e19 Michael Niedermayer
                   int pred_bx, int pred_by,
1549
                   int size, int h)
1550 de6d9b64 Fabrice Bellard
{
1551 91029be7 Michael Niedermayer
    //FIXME optimize?
1552 1457ab52 Michael Niedermayer
    //FIXME better f_code prediction (max mv & distance)
1553 bb198e19 Michael Niedermayer
    //FIXME pointers
1554 2750b827 Michael Niedermayer
    MotionEstContext * const c= &s->me;
1555 af4091f1 Michael Niedermayer
    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 91029be7 Michael Niedermayer
    uint8_t *ptr;
1560
    int dxy;
1561
    int src_x, src_y;
1562
    int fbmin;
1563 2750b827 Michael Niedermayer
    uint8_t **src_data= c->src[0];
1564
    uint8_t **ref_data= c->ref[0];
1565
    uint8_t **ref2_data= c->ref[2];
1566 91029be7 Michael Niedermayer
1567 1457ab52 Michael Niedermayer
    if(s->quarter_sample){
1568
        dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1569 bb198e19 Michael Niedermayer
        src_x = motion_fx >> 2;
1570
        src_y = motion_fy >> 2;
1571 1457ab52 Michael Niedermayer
1572 bb198e19 Michael Niedermayer
        ptr = ref_data[0] + (src_y * stride) + src_x;
1573
        s->dsp.put_qpel_pixels_tab[0][dxy](dest_y    , ptr    , stride);
1574 1457ab52 Michael Niedermayer
1575
        dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1576 bb198e19 Michael Niedermayer
        src_x = motion_bx >> 2;
1577
        src_y = motion_by >> 2;
1578 1457ab52 Michael Niedermayer
    
1579 2750b827 Michael Niedermayer
        ptr = ref2_data[0] + (src_y * stride) + src_x;
1580 bb198e19 Michael Niedermayer
        s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y    , ptr    , stride);
1581 1457ab52 Michael Niedermayer
    }else{
1582
        dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1583 bb198e19 Michael Niedermayer
        src_x = motion_fx >> 1;
1584
        src_y = motion_fy >> 1;
1585 1457ab52 Michael Niedermayer
1586 bb198e19 Michael Niedermayer
        ptr = ref_data[0] + (src_y * stride) + src_x;
1587
        s->dsp.put_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
1588 1457ab52 Michael Niedermayer
1589
        dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1590 bb198e19 Michael Niedermayer
        src_x = motion_bx >> 1;
1591
        src_y = motion_by >> 1;
1592 1457ab52 Michael Niedermayer
    
1593 2750b827 Michael Niedermayer
        ptr = ref2_data[0] + (src_y * stride) + src_x;
1594 bb198e19 Michael Niedermayer
        s->dsp.avg_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
1595 1457ab52 Michael Niedermayer
    }
1596
1597 af4091f1 Michael Niedermayer
    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 bb198e19 Michael Niedermayer
           + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
1600 67725183 Michael Niedermayer
           
1601 1f202b0d Michael Niedermayer
    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1602 67725183 Michael Niedermayer
    }
1603
    //FIXME CHROMA !!!
1604
           
1605 91029be7 Michael Niedermayer
    return fbmin;
1606
}
1607 9dbcbd92 Michael Niedermayer
1608 91029be7 Michael Niedermayer
/* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1609 2750b827 Michael Niedermayer
static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
1610 91029be7 Michael Niedermayer
{
1611 7bc9090a Michael Niedermayer
    const int mot_stride = s->mb_stride;
1612
    const int xy = mb_y *mot_stride + mb_x;
1613 91029be7 Michael Niedermayer
    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 2750b827 Michael Niedermayer
    fbmin= check_bidir_mv(s, motion_fx, motion_fy,
1626 91029be7 Michael Niedermayer
                          motion_bx, motion_by,
1627
                          pred_fx, pred_fy,
1628 bb198e19 Michael Niedermayer
                          pred_bx, pred_by,
1629
                          0, 16);
1630 91029be7 Michael Niedermayer
1631
   return fbmin;
1632
}
1633
1634 2750b827 Michael Niedermayer
static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
1635 91029be7 Michael Niedermayer
{
1636 af4091f1 Michael Niedermayer
    MotionEstContext * const c= &s->me;
1637 0d21a846 Michael Niedermayer
    int P[10][2];
1638 7bc9090a Michael Niedermayer
    const int mot_stride = s->mb_stride;
1639
    const int mot_xy = mb_y*mot_stride + mb_x;
1640 1457ab52 Michael Niedermayer
    const int shift= 1+s->quarter_sample;
1641
    int dmin, i;
1642 91029be7 Michael Niedermayer
    const int time_pp= s->pp_time;
1643 bea669e5 Michael Niedermayer
    const int time_pb= s->pb_time;
1644 1457ab52 Michael Niedermayer
    int mx, my, xmin, xmax, ymin, ymax;
1645 91029be7 Michael Niedermayer
    int16_t (*mv_table)[2]= s->b_direct_mv_table;
1646 1457ab52 Michael Niedermayer
    
1647 af4091f1 Michael Niedermayer
    c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
1648 1457ab52 Michael Niedermayer
    ymin= xmin=(-32)>>shift;
1649
    ymax= xmax=   31>>shift;
1650
1651 7bc9090a Michael Niedermayer
    if(IS_8X8(s->next_picture.mb_type[mot_xy])){
1652 1457ab52 Michael Niedermayer
        s->mv_type= MV_TYPE_8X8;
1653
    }else{
1654
        s->mv_type= MV_TYPE_16X16;
1655 91029be7 Michael Niedermayer
    }
1656 1457ab52 Michael Niedermayer
1657
    for(i=0; i<4; i++){
1658
        int index= s->block_index[i];
1659
        int min, max;
1660
    
1661 af4091f1 Michael Niedermayer
        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 5a603607 Michael Niedermayer
        max+= 16*mb_x + 1; // +-1 is for the simpler rounding
1671
        min+= 16*mb_x - 1;
1672 4a711c33 Michael Niedermayer
        xmax= FFMIN(xmax, s->width - max);
1673
        xmin= FFMAX(xmin, - 16     - min);
1674 1457ab52 Michael Niedermayer
1675 af4091f1 Michael Niedermayer
        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 5a603607 Michael Niedermayer
        max+= 16*mb_y + 1; // +-1 is for the simpler rounding
1678
        min+= 16*mb_y - 1;
1679 4a711c33 Michael Niedermayer
        ymax= FFMIN(ymax, s->height - max);
1680
        ymin= FFMAX(ymin, - 16      - min);
1681 1457ab52 Michael Niedermayer
        
1682
        if(s->mv_type == MV_TYPE_16X16) break;
1683 91029be7 Michael Niedermayer
    }
1684 1457ab52 Michael Niedermayer
    
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 bb198e19 Michael Niedermayer
    
1694 af4091f1 Michael Niedermayer
    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 1457ab52 Michael Niedermayer
1703 0a13093d Michael Niedermayer
    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 9c3d33d6 Michael Niedermayer
    if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as its cliped
1708 0a13093d Michael Niedermayer
        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 67725183 Michael Niedermayer
 
1717 26efc54e Michael Niedermayer
    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
1718 af4091f1 Michael Niedermayer
    if(c->sub_flags&FLAG_QPEL) 
1719 2750b827 Michael Niedermayer
        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 1f202b0d Michael Niedermayer
    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1724 155ec6ed Michael Niedermayer
        dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1725 bb198e19 Michael Niedermayer
    
1726 af4091f1 Michael Niedermayer
    get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
1727 91029be7 Michael Niedermayer
1728
    s->b_direct_mv_table[mot_xy][0]= mx;
1729
    s->b_direct_mv_table[mot_xy][1]= my;
1730 af4091f1 Michael Niedermayer
    c->flags     &= ~FLAG_DIRECT;
1731
    c->sub_flags &= ~FLAG_DIRECT;
1732 2750b827 Michael Niedermayer
1733 91029be7 Michael Niedermayer
    return dmin;
1734 9dbcbd92 Michael Niedermayer
}
1735
1736
void ff_estimate_b_frame_motion(MpegEncContext * s,
1737
                             int mb_x, int mb_y)
1738
{
1739 af4091f1 Michael Niedermayer
    MotionEstContext * const c= &s->me;
1740
    const int penalty_factor= c->mb_penalty_factor;
1741 bb198e19 Michael Niedermayer
    int fmin, bmin, dmin, fbmin, bimin, fimin;
1742 91029be7 Michael Niedermayer
    int type=0;
1743 f20f8a8b Michael Niedermayer
    const int xy = mb_y*s->mb_stride + mb_x;
1744 af4091f1 Michael Niedermayer
    init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
1745 f20f8a8b Michael Niedermayer
1746 ae1dbde1 Michael Niedermayer
    get_limits(s, 16*mb_x, 16*mb_y);
1747 91029be7 Michael Niedermayer
    
1748 af4091f1 Michael Niedermayer
    c->skip=0;
1749 1f202b0d Michael Niedermayer
    if(c->avctx->me_threshold){
1750 f4f3223f Michael Niedermayer
        int vard= (check_input_motion(s, mb_x, mb_y, 0)+128)>>8;
1751
        
1752 1f202b0d Michael Niedermayer
        if(vard<c->avctx->me_threshold){
1753 f4f3223f Michael Niedermayer
//            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 1f202b0d Michael Niedermayer
            c->mb_var_sum_temp    += varc;*/
1761
            c->mc_mb_var_sum_temp += vard;
1762 f4f3223f Michael Niedermayer
/*            if (vard <= 64 || vard < varc) {
1763 1f202b0d Michael Niedermayer
                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1764 f4f3223f Michael Niedermayer
            }else{
1765 1f202b0d Michael Niedermayer
                c->scene_change_score+= s->qscale;
1766 f4f3223f Michael Niedermayer
            }*/
1767
            return;
1768
        }
1769 1f202b0d Michael Niedermayer
        if(vard<c->avctx->mb_threshold){
1770 f20f8a8b Michael Niedermayer
            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 af4091f1 Michael Niedermayer
                c->skip=0;
1776 f20f8a8b Michael Niedermayer
                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 af4091f1 Michael Niedermayer
                c->skip=0;
1780 f20f8a8b Michael Niedermayer
                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 af4091f1 Michael Niedermayer
                c->skip=0;
1784
                c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1785 f20f8a8b Michael Niedermayer
                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 af4091f1 Michael Niedermayer
                c->skip=0;
1791
                c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
1792 f20f8a8b Michael Niedermayer
                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 f4f3223f Michael Niedermayer
    }
1799
1800 2d5e962b Michael Niedermayer
    if (s->codec_id == CODEC_ID_MPEG4)
1801 2750b827 Michael Niedermayer
        dmin= direct_search(s, mb_x, mb_y);
1802 2d5e962b Michael Niedermayer
    else
1803
        dmin= INT_MAX;
1804 bb198e19 Michael Niedermayer
//FIXME penalty stuff for non mpeg4
1805 af4091f1 Michael Niedermayer
    c->skip=0;
1806 2750b827 Michael Niedermayer
    fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
1807 2d5e962b Michael Niedermayer
    
1808 af4091f1 Michael Niedermayer
    c->skip=0;
1809 2750b827 Michael Niedermayer
    bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
1810 9dbcbd92 Michael Niedermayer
//printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1811 91029be7 Michael Niedermayer
1812 af4091f1 Michael Niedermayer
    c->skip=0;
1813 2750b827 Michael Niedermayer
    fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
1814 67725183 Michael Niedermayer
//printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1815 bb198e19 Michael Niedermayer
    
1816
    if(s->flags & CODEC_FLAG_INTERLACED_ME){
1817
//FIXME mb type penalty
1818 af4091f1 Michael Niedermayer
        c->skip=0;
1819
        c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
1820 2750b827 Michael Niedermayer
        fimin= interlaced_search(s, 0,
1821
                                 s->b_field_mv_table[0], s->b_field_select_table[0],
1822 f20f8a8b Michael Niedermayer
                                 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
1823 af4091f1 Michael Niedermayer
        c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
1824 2750b827 Michael Niedermayer
        bimin= interlaced_search(s, 2,
1825
                                 s->b_field_mv_table[1], s->b_field_select_table[1],
1826 f20f8a8b Michael Niedermayer
                                 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
1827 bb198e19 Michael Niedermayer
    }else
1828
        fimin= bimin= INT_MAX;
1829
1830 3aa102be Michael Niedermayer
    {
1831 63b15e55 Michael Niedermayer
        int score= fmin;
1832 bb198e19 Michael Niedermayer
        type = CANDIDATE_MB_TYPE_FORWARD;
1833 91029be7 Michael Niedermayer
        
1834 2d5e962b Michael Niedermayer
        if (dmin <= score){
1835 63b15e55 Michael Niedermayer
            score = dmin;
1836 bb198e19 Michael Niedermayer
            type = CANDIDATE_MB_TYPE_DIRECT;
1837 91029be7 Michael Niedermayer
        }
1838
        if(bmin<score){
1839
            score=bmin;
1840 bb198e19 Michael Niedermayer
            type= CANDIDATE_MB_TYPE_BACKWARD; 
1841 91029be7 Michael Niedermayer
        }
1842
        if(fbmin<score){
1843
            score=fbmin;
1844 bb198e19 Michael Niedermayer
            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 91029be7 Michael Niedermayer
        }
1854 67725183 Michael Niedermayer
        
1855 0bfacb95 Michael Niedermayer
        score= ((unsigned)(score*score + 128*256))>>16;
1856 1f202b0d Michael Niedermayer
        c->mc_mb_var_sum_temp += score;
1857 7bc9090a Michael Niedermayer
        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1858 91029be7 Michael Niedermayer
    }
1859 3aa102be Michael Niedermayer
1860 1f202b0d Michael Niedermayer
    if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1861 bb198e19 Michael Niedermayer
        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 622348f9 Michael Niedermayer
#if 0        
1872
        if(s->out_format == FMT_MPEG1)
1873
            type |= CANDIDATE_MB_TYPE_INTRA;
1874
#endif
1875 3aa102be Michael Niedermayer
    }
1876
1877 7bc9090a Michael Niedermayer
    s->mb_type[mb_y*s->mb_stride + mb_x]= type;
1878 9dbcbd92 Michael Niedermayer
}
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 0d21a846 Michael Niedermayer
        int score[8];
1885 9dbcbd92 Michael Niedermayer
        int i, y;
1886 0c1a9eda Zdenek Kabelac
        uint8_t * fcode_tab= s->fcode_tab;
1887 0d21a846 Michael Niedermayer
        int best_fcode=-1;
1888
        int best_score=-10000000;
1889 9dbcbd92 Michael Niedermayer
1890 1457ab52 Michael Niedermayer
        for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
1891 9dbcbd92 Michael Niedermayer
1892
        for(y=0; y<s->mb_height; y++){
1893
            int x;
1894 7bc9090a Michael Niedermayer
            int xy= y*s->mb_stride;
1895 9dbcbd92 Michael Niedermayer
            for(x=0; x<s->mb_width; x++){
1896 d3985de7 Michael Niedermayer
                if(s->mb_type[xy] & type){
1897 b8a78f41 Michael Niedermayer
                    int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV],
1898
                                     fcode_tab[mv_table[xy][1] + MAX_MV]);
1899 0d21a846 Michael Niedermayer
                    int j;
1900
                    
1901
                    for(j=0; j<fcode && j<8; j++){
1902 7bc9090a Michael Niedermayer
                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
1903 0d21a846 Michael Niedermayer
                            score[j]-= 170;
1904
                    }
1905 9dbcbd92 Michael Niedermayer
                }
1906
                xy++;
1907
            }
1908
        }
1909 0d21a846 Michael Niedermayer
        
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 9dbcbd92 Michael Niedermayer
        }
1917 0d21a846 Michael Niedermayer
1918 9dbcbd92 Michael Niedermayer
//    printf("fcode: %d type: %d\n", i, s->pict_type);
1919 0d21a846 Michael Niedermayer
        return best_fcode;
1920 9dbcbd92 Michael Niedermayer
/*        for(i=0; i<=MAX_FCODE; i++){
1921
            printf("%d ", mv_num[i]);
1922
        }
1923
        printf("\n");*/
1924
    }else{
1925
        return 1;
1926 de6d9b64 Fabrice Bellard
    }
1927
}
1928
1929 9dbcbd92 Michael Niedermayer
void ff_fix_long_p_mvs(MpegEncContext * s)
1930
{
1931 1f202b0d Michael Niedermayer
    MotionEstContext * const c= &s->me;
1932 9dbcbd92 Michael Niedermayer
    const int f_code= s->f_code;
1933 ebbcdc9a Michael Niedermayer
    int y, range;
1934 029911d1 Michael Niedermayer
    assert(s->pict_type==P_TYPE);
1935 ebbcdc9a Michael Niedermayer
1936 029911d1 Michael Niedermayer
    range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
1937 ebbcdc9a Michael Niedermayer
    
1938
    if(s->msmpeg4_version) range= 16;
1939
    
1940 1f202b0d Michael Niedermayer
    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1941 ebbcdc9a Michael Niedermayer
    
1942 0d21a846 Michael Niedermayer
//printf("%d no:%d %d//\n", clip, noclip, f_code);
1943 9dbcbd92 Michael Niedermayer
    if(s->flags&CODEC_FLAG_4MV){
1944 137c8468 Michael Niedermayer
        const int wrap= s->b8_stride;
1945 9dbcbd92 Michael Niedermayer
1946
        /* clip / convert to intra 8x8 type MVs */
1947
        for(y=0; y<s->mb_height; y++){
1948 137c8468 Michael Niedermayer
            int xy= y*2*wrap;
1949 7bc9090a Michael Niedermayer
            int i= y*s->mb_stride;
1950 9dbcbd92 Michael Niedermayer
            int x;
1951
1952
            for(x=0; x<s->mb_width; x++){
1953 bb198e19 Michael Niedermayer
                if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
1954 9dbcbd92 Michael Niedermayer
                    int block;
1955
                    for(block=0; block<4; block++){
1956
                        int off= (block& 1) + (block>>1)*wrap;
1957 8d7ec294 Wolfgang Hesseler
                        int mx= s->current_picture.motion_val[0][ xy + off ][0];
1958
                        int my= s->current_picture.motion_val[0][ xy + off ][1];
1959 9dbcbd92 Michael Niedermayer
1960 ebbcdc9a Michael Niedermayer
                        if(   mx >=range || mx <-range
1961
                           || my >=range || my <-range){
1962 bb198e19 Michael Niedermayer
                            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 9dbcbd92 Michael Niedermayer
                        }
1966
                    }
1967
                }
1968 f10bd870 Michael Niedermayer
                xy+=2;
1969
                i++;
1970 9dbcbd92 Michael Niedermayer
            }
1971
        }
1972
    }
1973
}
1974
1975 bb198e19 Michael Niedermayer
/**
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 9dbcbd92 Michael Niedermayer
{
1982 1f202b0d Michael Niedermayer
    MotionEstContext * const c= &s->me;
1983 bb198e19 Michael Niedermayer
    int y, h_range, v_range;
1984 9dbcbd92 Michael Niedermayer
1985 63b15e55 Michael Niedermayer
    // RAL: 8 in MPEG-1, 16 in MPEG-4
1986 029911d1 Michael Niedermayer
    int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
1987 bb198e19 Michael Niedermayer
1988
    if(s->msmpeg4_version) range= 16;
1989 1f202b0d Michael Niedermayer
    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1990 63b15e55 Michael Niedermayer
1991 bb198e19 Michael Niedermayer
    h_range= range;
1992
    v_range= field_select_table ? range>>1 : range;
1993
1994 9dbcbd92 Michael Niedermayer
    /* clip / convert to intra 16x16 type MVs */
1995
    for(y=0; y<s->mb_height; y++){
1996
        int x;
1997 7bc9090a Michael Niedermayer
        int xy= y*s->mb_stride;
1998 ebbcdc9a Michael Niedermayer
        for(x=0; x<s->mb_width; x++){
1999 7bc9090a Michael Niedermayer
            if (s->mb_type[xy] & type){    // RAL: "type" test added...
2000 bb198e19 Michael Niedermayer
                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 63b15e55 Michael Niedermayer
                    }
2016 ebbcdc9a Michael Niedermayer
                }
2017 9dbcbd92 Michael Niedermayer
            }
2018
            xy++;
2019
        }
2020
    }
2021
}