libavcodec/motion_est.c
Go to the documentation of this file.
00001 /*
00002  * Motion estimation
00003  * Copyright (c) 2000,2001 Fabrice Bellard
00004  * Copyright (c) 2002-2004 Michael Niedermayer
00005  *
00006  * new motion estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <limits.h>
00033 #include "libavutil/intmath.h"
00034 #include "avcodec.h"
00035 #include "dsputil.h"
00036 #include "mathops.h"
00037 #include "mpegvideo.h"
00038 
00039 #undef NDEBUG
00040 #include <assert.h>
00041 
00042 #define SQ(a) ((a)*(a))
00043 
00044 #define P_LEFT P[1]
00045 #define P_TOP P[2]
00046 #define P_TOPRIGHT P[3]
00047 #define P_MEDIAN P[4]
00048 #define P_MV1 P[9]
00049 
00050 static inline int sad_hpel_motion_search(MpegEncContext * s,
00051                                   int *mx_ptr, int *my_ptr, int dmin,
00052                                   int src_index, int ref_index,
00053                                   int size, int h);
00054 
00055 static inline unsigned update_map_generation(MotionEstContext *c)
00056 {
00057     c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
00058     if(c->map_generation==0){
00059         c->map_generation= 1<<(ME_MAP_MV_BITS*2);
00060         memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
00061     }
00062     return c->map_generation;
00063 }
00064 
00065 /* shape adaptive search stuff */
00066 typedef struct Minima{
00067     int height;
00068     int x, y;
00069     int checked;
00070 }Minima;
00071 
00072 static int minima_cmp(const void *a, const void *b){
00073     const Minima *da = (const Minima *) a;
00074     const Minima *db = (const Minima *) b;
00075 
00076     return da->height - db->height;
00077 }
00078 
00079 #define FLAG_QPEL   1 //must be 1
00080 #define FLAG_CHROMA 2
00081 #define FLAG_DIRECT 4
00082 
00083 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){
00084     const int offset[3]= {
00085           y*c->  stride + x,
00086         ((y*c->uvstride + x)>>1),
00087         ((y*c->uvstride + x)>>1),
00088     };
00089     int i;
00090     for(i=0; i<3; i++){
00091         c->src[0][i]= src [i] + offset[i];
00092         c->ref[0][i]= ref [i] + offset[i];
00093     }
00094     if(ref_index){
00095         for(i=0; i<3; i++){
00096             c->ref[ref_index][i]= ref2[i] + offset[i];
00097         }
00098     }
00099 }
00100 
00101 static int get_flags(MotionEstContext *c, int direct, int chroma){
00102     return   ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
00103            + (direct ? FLAG_DIRECT : 0)
00104            + (chroma ? FLAG_CHROMA : 0);
00105 }
00106 
00107 static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
00108                       const int size, const int h, int ref_index, int src_index,
00109                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){
00110     MotionEstContext * const c= &s->me;
00111     const int stride= c->stride;
00112     const int hx= subx + (x<<(1+qpel));
00113     const int hy= suby + (y<<(1+qpel));
00114     uint8_t * const * const ref= c->ref[ref_index];
00115     uint8_t * const * const src= c->src[src_index];
00116     int d;
00117     //FIXME check chroma 4mv, (no crashes ...)
00118         assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
00119         if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
00120             const int time_pp= s->pp_time;
00121             const int time_pb= s->pb_time;
00122             const int mask= 2*qpel+1;
00123             if(s->mv_type==MV_TYPE_8X8){
00124                 int i;
00125                 for(i=0; i<4; i++){
00126                     int fx = c->direct_basis_mv[i][0] + hx;
00127                     int fy = c->direct_basis_mv[i][1] + hy;
00128                     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));
00129                     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));
00130                     int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
00131                     int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
00132 
00133                     uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
00134                     if(qpel){
00135                         c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
00136                         c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
00137                     }else{
00138                         c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
00139                         c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
00140                     }
00141                 }
00142             }else{
00143                 int fx = c->direct_basis_mv[0][0] + hx;
00144                 int fy = c->direct_basis_mv[0][1] + hy;
00145                 int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
00146                 int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
00147                 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
00148                 int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
00149 
00150                 if(qpel){
00151                     c->qpel_put[1][fxy](c->temp               , ref[0] + (fx>>2) + (fy>>2)*stride               , stride);
00152                     c->qpel_put[1][fxy](c->temp + 8           , ref[0] + (fx>>2) + (fy>>2)*stride + 8           , stride);
00153                     c->qpel_put[1][fxy](c->temp     + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride     + 8*stride, stride);
00154                     c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
00155                     c->qpel_avg[1][bxy](c->temp               , ref[8] + (bx>>2) + (by>>2)*stride               , stride);
00156                     c->qpel_avg[1][bxy](c->temp + 8           , ref[8] + (bx>>2) + (by>>2)*stride + 8           , stride);
00157                     c->qpel_avg[1][bxy](c->temp     + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride     + 8*stride, stride);
00158                     c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
00159                 }else{
00160                     assert((fx>>1) + 16*s->mb_x >= -16);
00161                     assert((fy>>1) + 16*s->mb_y >= -16);
00162                     assert((fx>>1) + 16*s->mb_x <= s->width);
00163                     assert((fy>>1) + 16*s->mb_y <= s->height);
00164                     assert((bx>>1) + 16*s->mb_x >= -16);
00165                     assert((by>>1) + 16*s->mb_y >= -16);
00166                     assert((bx>>1) + 16*s->mb_x <= s->width);
00167                     assert((by>>1) + 16*s->mb_y <= s->height);
00168 
00169                     c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
00170                     c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
00171                 }
00172             }
00173             d = cmp_func(s, c->temp, src[0], stride, 16);
00174         }else
00175             d= 256*256*256*32;
00176     return d;
00177 }
00178 
00179 static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
00180                       const int size, const int h, int ref_index, int src_index,
00181                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){
00182     MotionEstContext * const c= &s->me;
00183     const int stride= c->stride;
00184     const int uvstride= c->uvstride;
00185     const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
00186     const int hx= subx + (x<<(1+qpel));
00187     const int hy= suby + (y<<(1+qpel));
00188     uint8_t * const * const ref= c->ref[ref_index];
00189     uint8_t * const * const src= c->src[src_index];
00190     int d;
00191     //FIXME check chroma 4mv, (no crashes ...)
00192         int uvdxy;              /* no, it might not be used uninitialized */
00193         if(dxy){
00194             if(qpel){
00195                 c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
00196                 if(chroma){
00197                     int cx= hx/2;
00198                     int cy= hy/2;
00199                     cx= (cx>>1)|(cx&1);
00200                     cy= (cy>>1)|(cy&1);
00201                     uvdxy= (cx&1) + 2*(cy&1);
00202                     //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264
00203                 }
00204             }else{
00205                 c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
00206                 if(chroma)
00207                     uvdxy= dxy | (x&1) | (2*(y&1));
00208             }
00209             d = cmp_func(s, c->temp, src[0], stride, h);
00210         }else{
00211             d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
00212             if(chroma)
00213                 uvdxy= (x&1) + 2*(y&1);
00214         }
00215         if(chroma){
00216             uint8_t * const uvtemp= c->temp + 16*stride;
00217             c->hpel_put[size+1][uvdxy](uvtemp  , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
00218             c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
00219             d += chroma_cmp_func(s, uvtemp  , src[1], uvstride, h>>1);
00220             d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
00221         }
00222     return d;
00223 }
00224 
00225 static int cmp_simple(MpegEncContext *s, const int x, const int y,
00226                       int ref_index, int src_index,
00227                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func){
00228     return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0);
00229 }
00230 
00231 static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y,
00232                       const int size, const int h, int ref_index, int src_index,
00233                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
00234     if(flags&FLAG_DIRECT){
00235         return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
00236     }else{
00237         return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
00238     }
00239 }
00240 
00241 static int cmp_internal(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
00242                       const int size, const int h, int ref_index, int src_index,
00243                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
00244     if(flags&FLAG_DIRECT){
00245         return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
00246     }else{
00247         return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL, flags&FLAG_CHROMA);
00248     }
00249 }
00250 
00254 static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
00255                       const int size, const int h, int ref_index, int src_index,
00256                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
00257     if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size)
00258        && av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
00259        && flags==0 && h==16 && size==0 && subx==0 && suby==0){
00260         return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);
00261     }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
00262        && subx==0 && suby==0){
00263         return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);
00264     }else{
00265         return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
00266     }
00267 }
00268 
00269 static int cmp_hpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
00270                       const int size, const int h, int ref_index, int src_index,
00271                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
00272     if(flags&FLAG_DIRECT){
00273         return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0);
00274     }else{
00275         return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
00276     }
00277 }
00278 
00279 static int cmp_qpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
00280                       const int size, const int h, int ref_index, int src_index,
00281                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
00282     if(flags&FLAG_DIRECT){
00283         return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1);
00284     }else{
00285         return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1, flags&FLAG_CHROMA);
00286     }
00287 }
00288 
00289 #include "motion_est_template.c"
00290 
00291 static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
00292     return 0;
00293 }
00294 
00295 static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){
00296 }
00297 
00298 int ff_init_me(MpegEncContext *s){
00299     MotionEstContext * const c= &s->me;
00300     int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
00301     int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
00302 
00303     if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -ME_MAP_SIZE){
00304         av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
00305         return -1;
00306     }
00307     //special case of snow is needed because snow uses its own iterative ME code
00308     if(s->me_method!=ME_ZERO && s->me_method!=ME_EPZS && s->me_method!=ME_X1 && s->avctx->codec_id != CODEC_ID_SNOW){
00309         av_log(s->avctx, AV_LOG_ERROR, "me_method is only allowed to be set to zero and epzs; for hex,umh,full and others see dia_size\n");
00310         return -1;
00311     }
00312 
00313     c->avctx= s->avctx;
00314 
00315     if(cache_size < 2*dia_size && !c->stride){
00316         av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
00317     }
00318 
00319     ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
00320     ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
00321     ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
00322     ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
00323 
00324     c->flags    = get_flags(c, 0, c->avctx->me_cmp    &FF_CMP_CHROMA);
00325     c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
00326     c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp    &FF_CMP_CHROMA);
00327 
00328 /*FIXME s->no_rounding b_type*/
00329     if(s->flags&CODEC_FLAG_QPEL){
00330         c->sub_motion_search= qpel_motion_search;
00331         c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
00332         if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
00333         else               c->qpel_put= s->dsp.put_qpel_pixels_tab;
00334     }else{
00335         if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
00336             c->sub_motion_search= hpel_motion_search;
00337         else if(   c->avctx->me_sub_cmp == FF_CMP_SAD
00338                 && c->avctx->    me_cmp == FF_CMP_SAD
00339                 && c->avctx->    mb_cmp == FF_CMP_SAD)
00340             c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
00341         else
00342             c->sub_motion_search= hpel_motion_search;
00343     }
00344     c->hpel_avg= s->dsp.avg_pixels_tab;
00345     if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
00346     else               c->hpel_put= s->dsp.put_pixels_tab;
00347 
00348     if(s->linesize){
00349         c->stride  = s->linesize;
00350         c->uvstride= s->uvlinesize;
00351     }else{
00352         c->stride  = 16*s->mb_width + 32;
00353         c->uvstride=  8*s->mb_width + 16;
00354     }
00355 
00356     /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
00357      * not have yet, and even if we had, the motion estimation code
00358      * does not expect it. */
00359     if(s->codec_id != CODEC_ID_SNOW){
00360         if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
00361             s->dsp.me_cmp[2]= zero_cmp;
00362         }
00363         if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
00364             s->dsp.me_sub_cmp[2]= zero_cmp;
00365         }
00366         c->hpel_put[2][0]= c->hpel_put[2][1]=
00367         c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
00368     }
00369 
00370     if(s->codec_id == CODEC_ID_H261){
00371         c->sub_motion_search= no_sub_motion_search;
00372     }
00373 
00374     return 0;
00375 }
00376 
00377 static inline void no_motion_search(MpegEncContext * s,
00378                                     int *mx_ptr, int *my_ptr)
00379 {
00380     *mx_ptr = 16 * s->mb_x;
00381     *my_ptr = 16 * s->mb_y;
00382 }
00383 
00384 #define Z_THRESHOLD 256
00385 
00386 #define CHECK_SAD_HALF_MV(suffix, x, y) \
00387 {\
00388     d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
00389     d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
00390     COPY3_IF_LT(dminh, d, dx, x, dy, y)\
00391 }
00392 
00393 static inline int sad_hpel_motion_search(MpegEncContext * s,
00394                                   int *mx_ptr, int *my_ptr, int dmin,
00395                                   int src_index, int ref_index,
00396                                   int size, int h)
00397 {
00398     MotionEstContext * const c= &s->me;
00399     const int penalty_factor= c->sub_penalty_factor;
00400     int mx, my, dminh;
00401     uint8_t *pix, *ptr;
00402     int stride= c->stride;
00403     const int flags= c->sub_flags;
00404     LOAD_COMMON
00405 
00406     assert(flags == 0);
00407 
00408     if(c->skip){
00409 //    printf("S");
00410         *mx_ptr = 0;
00411         *my_ptr = 0;
00412         return dmin;
00413     }
00414 //    printf("N");
00415 
00416     pix = c->src[src_index][0];
00417 
00418     mx = *mx_ptr;
00419     my = *my_ptr;
00420     ptr = c->ref[ref_index][0] + (my * stride) + mx;
00421 
00422     dminh = dmin;
00423 
00424     if (mx > xmin && mx < xmax &&
00425         my > ymin && my < ymax) {
00426         int dx=0, dy=0;
00427         int d, pen_x, pen_y;
00428         const int index= (my<<ME_MAP_SHIFT) + mx;
00429         const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
00430         const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)];
00431         const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)];
00432         const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
00433         mx<<=1;
00434         my<<=1;
00435 
00436 
00437         pen_x= pred_x + mx;
00438         pen_y= pred_y + my;
00439 
00440         ptr-= stride;
00441         if(t<=b){
00442             CHECK_SAD_HALF_MV(y2 , 0, -1)
00443             if(l<=r){
00444                 CHECK_SAD_HALF_MV(xy2, -1, -1)
00445                 if(t+r<=b+l){
00446                     CHECK_SAD_HALF_MV(xy2, +1, -1)
00447                     ptr+= stride;
00448                 }else{
00449                     ptr+= stride;
00450                     CHECK_SAD_HALF_MV(xy2, -1, +1)
00451                 }
00452                 CHECK_SAD_HALF_MV(x2 , -1,  0)
00453             }else{
00454                 CHECK_SAD_HALF_MV(xy2, +1, -1)
00455                 if(t+l<=b+r){
00456                     CHECK_SAD_HALF_MV(xy2, -1, -1)
00457                     ptr+= stride;
00458                 }else{
00459                     ptr+= stride;
00460                     CHECK_SAD_HALF_MV(xy2, +1, +1)
00461                 }
00462                 CHECK_SAD_HALF_MV(x2 , +1,  0)
00463             }
00464         }else{
00465             if(l<=r){
00466                 if(t+l<=b+r){
00467                     CHECK_SAD_HALF_MV(xy2, -1, -1)
00468                     ptr+= stride;
00469                 }else{
00470                     ptr+= stride;
00471                     CHECK_SAD_HALF_MV(xy2, +1, +1)
00472                 }
00473                 CHECK_SAD_HALF_MV(x2 , -1,  0)
00474                 CHECK_SAD_HALF_MV(xy2, -1, +1)
00475             }else{
00476                 if(t+r<=b+l){
00477                     CHECK_SAD_HALF_MV(xy2, +1, -1)
00478                     ptr+= stride;
00479                 }else{
00480                     ptr+= stride;
00481                     CHECK_SAD_HALF_MV(xy2, -1, +1)
00482                 }
00483                 CHECK_SAD_HALF_MV(x2 , +1,  0)
00484                 CHECK_SAD_HALF_MV(xy2, +1, +1)
00485             }
00486             CHECK_SAD_HALF_MV(y2 ,  0, +1)
00487         }
00488         mx+=dx;
00489         my+=dy;
00490 
00491     }else{
00492         mx<<=1;
00493         my<<=1;
00494     }
00495 
00496     *mx_ptr = mx;
00497     *my_ptr = my;
00498     return dminh;
00499 }
00500 
00501 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
00502 {
00503     const int xy= s->mb_x + s->mb_y*s->mb_stride;
00504 
00505     s->p_mv_table[xy][0] = mx;
00506     s->p_mv_table[xy][1] = my;
00507 
00508     /* has already been set to the 4 MV if 4MV is done */
00509     if(mv4){
00510         int mot_xy= s->block_index[0];
00511 
00512         s->current_picture.f.motion_val[0][mot_xy    ][0] = mx;
00513         s->current_picture.f.motion_val[0][mot_xy    ][1] = my;
00514         s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
00515         s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
00516 
00517         mot_xy += s->b8_stride;
00518         s->current_picture.f.motion_val[0][mot_xy    ][0] = mx;
00519         s->current_picture.f.motion_val[0][mot_xy    ][1] = my;
00520         s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
00521         s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
00522     }
00523 }
00524 
00528 static inline void get_limits(MpegEncContext *s, int x, int y)
00529 {
00530     MotionEstContext * const c= &s->me;
00531     int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
00532 /*
00533     if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
00534     else                   c->range= 16;
00535 */
00536     if (s->unrestricted_mv) {
00537         c->xmin = - x - 16;
00538         c->ymin = - y - 16;
00539         c->xmax = - x + s->width;
00540         c->ymax = - y + s->height;
00541     } else if (s->out_format == FMT_H261){
00542         // Search range of H261 is different from other codec standards
00543         c->xmin = (x > 15) ? - 15 : 0;
00544         c->ymin = (y > 15) ? - 15 : 0;
00545         c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
00546         c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
00547     } else {
00548         c->xmin = - x;
00549         c->ymin = - y;
00550         c->xmax = - x + s->mb_width *16 - 16;
00551         c->ymax = - y + s->mb_height*16 - 16;
00552     }
00553     if(range){
00554         c->xmin = FFMAX(c->xmin,-range);
00555         c->xmax = FFMIN(c->xmax, range);
00556         c->ymin = FFMAX(c->ymin,-range);
00557         c->ymax = FFMIN(c->ymax, range);
00558     }
00559 }
00560 
00561 static inline void init_mv4_ref(MotionEstContext *c){
00562     const int stride= c->stride;
00563 
00564     c->ref[1][0] = c->ref[0][0] + 8;
00565     c->ref[2][0] = c->ref[0][0] + 8*stride;
00566     c->ref[3][0] = c->ref[2][0] + 8;
00567     c->src[1][0] = c->src[0][0] + 8;
00568     c->src[2][0] = c->src[0][0] + 8*stride;
00569     c->src[3][0] = c->src[2][0] + 8;
00570 }
00571 
00572 static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
00573 {
00574     MotionEstContext * const c= &s->me;
00575     const int size= 1;
00576     const int h=8;
00577     int block;
00578     int P[10][2];
00579     int dmin_sum=0, mx4_sum=0, my4_sum=0, i;
00580     int same=1;
00581     const int stride= c->stride;
00582     uint8_t *mv_penalty= c->current_mv_penalty;
00583     int saftey_cliping= s->unrestricted_mv && (s->width&15) && (s->height&15);
00584 
00585     init_mv4_ref(c);
00586 
00587     for(block=0; block<4; block++){
00588         int mx4, my4;
00589         int pred_x4, pred_y4;
00590         int dmin4;
00591         static const int off[4]= {2, 1, 1, -1};
00592         const int mot_stride = s->b8_stride;
00593         const int mot_xy = s->block_index[block];
00594 
00595         if(saftey_cliping){
00596             c->xmax = - 16*s->mb_x + s->width  - 8*(block &1);
00597             c->ymax = - 16*s->mb_y + s->height - 8*(block>>1);
00598         }
00599 
00600         P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
00601         P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
00602 
00603         if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
00604 
00605         /* special case for first line */
00606         if (s->first_slice_line && block<2) {
00607             c->pred_x= pred_x4= P_LEFT[0];
00608             c->pred_y= pred_y4= P_LEFT[1];
00609         } else {
00610             P_TOP[0]      = s->current_picture.f.motion_val[0][mot_xy - mot_stride             ][0];
00611             P_TOP[1]      = s->current_picture.f.motion_val[0][mot_xy - mot_stride             ][1];
00612             P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][0];
00613             P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][1];
00614             if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
00615             if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
00616             if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
00617             if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
00618 
00619             P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
00620             P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
00621 
00622             c->pred_x= pred_x4 = P_MEDIAN[0];
00623             c->pred_y= pred_y4 = P_MEDIAN[1];
00624         }
00625         P_MV1[0]= mx;
00626         P_MV1[1]= my;
00627         if(saftey_cliping)
00628             for(i=0; i<10; i++){
00629                 if(P[i][0] > (c->xmax<<shift)) P[i][0]= (c->xmax<<shift);
00630                 if(P[i][1] > (c->ymax<<shift)) P[i][1]= (c->ymax<<shift);
00631             }
00632 
00633         dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
00634 
00635         dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
00636 
00637         if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
00638             int dxy;
00639             const int offset= ((block&1) + (block>>1)*stride)*8;
00640             uint8_t *dest_y = c->scratchpad + offset;
00641             if(s->quarter_sample){
00642                 uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
00643                 dxy = ((my4 & 3) << 2) | (mx4 & 3);
00644 
00645                 if(s->no_rounding)
00646                     s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y   , ref    , stride);
00647                 else
00648                     s->dsp.put_qpel_pixels_tab       [1][dxy](dest_y   , ref    , stride);
00649             }else{
00650                 uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
00651                 dxy = ((my4 & 1) << 1) | (mx4 & 1);
00652 
00653                 if(s->no_rounding)
00654                     s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y    , ref    , stride, h);
00655                 else
00656                     s->dsp.put_pixels_tab       [1][dxy](dest_y    , ref    , stride, h);
00657             }
00658             dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
00659         }else
00660             dmin_sum+= dmin4;
00661 
00662         if(s->quarter_sample){
00663             mx4_sum+= mx4/2;
00664             my4_sum+= my4/2;
00665         }else{
00666             mx4_sum+= mx4;
00667             my4_sum+= my4;
00668         }
00669 
00670         s->current_picture.f.motion_val[0][s->block_index[block]][0] = mx4;
00671         s->current_picture.f.motion_val[0][s->block_index[block]][1] = my4;
00672 
00673         if(mx4 != mx || my4 != my) same=0;
00674     }
00675 
00676     if(same)
00677         return INT_MAX;
00678 
00679     if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
00680         dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*16*stride, c->scratchpad, stride, 16);
00681     }
00682 
00683     if(c->avctx->mb_cmp&FF_CMP_CHROMA){
00684         int dxy;
00685         int mx, my;
00686         int offset;
00687 
00688         mx= ff_h263_round_chroma(mx4_sum);
00689         my= ff_h263_round_chroma(my4_sum);
00690         dxy = ((my & 1) << 1) | (mx & 1);
00691 
00692         offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
00693 
00694         if(s->no_rounding){
00695             s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad    , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
00696             s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
00697         }else{
00698             s->dsp.put_pixels_tab       [1][dxy](c->scratchpad    , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
00699             s->dsp.put_pixels_tab       [1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
00700         }
00701 
00702         dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.f.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad  , s->uvlinesize, 8);
00703         dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.f.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad+8, s->uvlinesize, 8);
00704     }
00705 
00706     c->pred_x= mx;
00707     c->pred_y= my;
00708 
00709     switch(c->avctx->mb_cmp&0xFF){
00710     /*case FF_CMP_SSE:
00711         return dmin_sum+ 32*s->qscale*s->qscale;*/
00712     case FF_CMP_RD:
00713         return dmin_sum;
00714     default:
00715         return dmin_sum+ 11*c->mb_penalty_factor;
00716     }
00717 }
00718 
00719 static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
00720     MotionEstContext * const c= &s->me;
00721 
00722     c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
00723     c->src[1][0] = c->src[0][0] + s->linesize;
00724     if(c->flags & FLAG_CHROMA){
00725         c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
00726         c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
00727         c->src[1][1] = c->src[0][1] + s->uvlinesize;
00728         c->src[1][2] = c->src[0][2] + s->uvlinesize;
00729     }
00730 }
00731 
00732 static int interlaced_search(MpegEncContext *s, int ref_index,
00733                              int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
00734 {
00735     MotionEstContext * const c= &s->me;
00736     const int size=0;
00737     const int h=8;
00738     int block;
00739     int P[10][2];
00740     uint8_t * const mv_penalty= c->current_mv_penalty;
00741     int same=1;
00742     const int stride= 2*s->linesize;
00743     int dmin_sum= 0;
00744     const int mot_stride= s->mb_stride;
00745     const int xy= s->mb_x + s->mb_y*mot_stride;
00746 
00747     c->ymin>>=1;
00748     c->ymax>>=1;
00749     c->stride<<=1;
00750     c->uvstride<<=1;
00751     init_interlaced_ref(s, ref_index);
00752 
00753     for(block=0; block<2; block++){
00754         int field_select;
00755         int best_dmin= INT_MAX;
00756         int best_field= -1;
00757 
00758         for(field_select=0; field_select<2; field_select++){
00759             int dmin, mx_i, my_i;
00760             int16_t (*mv_table)[2]= mv_tables[block][field_select];
00761 
00762             if(user_field_select){
00763                 assert(field_select==0 || field_select==1);
00764                 assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
00765                 if(field_select_tables[block][xy] != field_select)
00766                     continue;
00767             }
00768 
00769             P_LEFT[0] = mv_table[xy - 1][0];
00770             P_LEFT[1] = mv_table[xy - 1][1];
00771             if(P_LEFT[0]       > (c->xmax<<1)) P_LEFT[0]       = (c->xmax<<1);
00772 
00773             c->pred_x= P_LEFT[0];
00774             c->pred_y= P_LEFT[1];
00775 
00776             if(!s->first_slice_line){
00777                 P_TOP[0]      = mv_table[xy - mot_stride][0];
00778                 P_TOP[1]      = mv_table[xy - mot_stride][1];
00779                 P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
00780                 P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
00781                 if(P_TOP[1]      > (c->ymax<<1)) P_TOP[1]     = (c->ymax<<1);
00782                 if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
00783                 if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
00784                 if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
00785 
00786                 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
00787                 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
00788             }
00789             P_MV1[0]= mx; //FIXME not correct if block != field_select
00790             P_MV1[1]= my / 2;
00791 
00792             dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
00793 
00794             dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
00795 
00796             mv_table[xy][0]= mx_i;
00797             mv_table[xy][1]= my_i;
00798 
00799             if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
00800                 int dxy;
00801 
00802                 //FIXME chroma ME
00803                 uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
00804                 dxy = ((my_i & 1) << 1) | (mx_i & 1);
00805 
00806                 if(s->no_rounding){
00807                     s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref    , stride, h);
00808                 }else{
00809                     s->dsp.put_pixels_tab       [size][dxy](c->scratchpad, ref    , stride, h);
00810                 }
00811                 dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
00812                 dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
00813             }else
00814                 dmin+= c->mb_penalty_factor; //field_select bits
00815 
00816             dmin += field_select != block; //slightly prefer same field
00817 
00818             if(dmin < best_dmin){
00819                 best_dmin= dmin;
00820                 best_field= field_select;
00821             }
00822         }
00823         {
00824             int16_t (*mv_table)[2]= mv_tables[block][best_field];
00825 
00826             if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
00827             if(mv_table[xy][1]&1) same=0;
00828             if(mv_table[xy][1]*2 != my) same=0;
00829             if(best_field != block) same=0;
00830         }
00831 
00832         field_select_tables[block][xy]= best_field;
00833         dmin_sum += best_dmin;
00834     }
00835 
00836     c->ymin<<=1;
00837     c->ymax<<=1;
00838     c->stride>>=1;
00839     c->uvstride>>=1;
00840 
00841     if(same)
00842         return INT_MAX;
00843 
00844     switch(c->avctx->mb_cmp&0xFF){
00845     /*case FF_CMP_SSE:
00846         return dmin_sum+ 32*s->qscale*s->qscale;*/
00847     case FF_CMP_RD:
00848         return dmin_sum;
00849     default:
00850         return dmin_sum+ 11*c->mb_penalty_factor;
00851     }
00852 }
00853 
00854 static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
00855     int ymax= s->me.ymax>>interlaced;
00856     int ymin= s->me.ymin>>interlaced;
00857 
00858     if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
00859     if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
00860     if(mv[1] <       ymin) mv[1] =       ymin;
00861     if(mv[1] >       ymax) mv[1] =       ymax;
00862 }
00863 
00864 static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
00865     MotionEstContext * const c= &s->me;
00866     Picture *p= s->current_picture_ptr;
00867     int mb_xy= mb_x + mb_y*s->mb_stride;
00868     int xy= 2*mb_x + 2*mb_y*s->b8_stride;
00869     int mb_type= s->current_picture.f.mb_type[mb_xy];
00870     int flags= c->flags;
00871     int shift= (flags&FLAG_QPEL) + 1;
00872     int mask= (1<<shift)-1;
00873     int x, y, i;
00874     int d=0;
00875     me_cmp_func cmpf= s->dsp.sse[0];
00876     me_cmp_func chroma_cmpf= s->dsp.sse[1];
00877 
00878     if(p_type && USES_LIST(mb_type, 1)){
00879         av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
00880         return INT_MAX/2;
00881     }
00882     assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
00883 
00884     for(i=0; i<4; i++){
00885         int xy= s->block_index[i];
00886         clip_input_mv(s, p->f.motion_val[0][xy], !!IS_INTERLACED(mb_type));
00887         clip_input_mv(s, p->f.motion_val[1][xy], !!IS_INTERLACED(mb_type));
00888     }
00889 
00890     if(IS_INTERLACED(mb_type)){
00891         int xy2= xy  + s->b8_stride;
00892         s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
00893         c->stride<<=1;
00894         c->uvstride<<=1;
00895 
00896         if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
00897             av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
00898             return INT_MAX/2;
00899         }
00900 
00901         if(USES_LIST(mb_type, 0)){
00902             int field_select0= p->f.ref_index[0][4*mb_xy  ];
00903             int field_select1= p->f.ref_index[0][4*mb_xy+2];
00904             assert(field_select0==0 ||field_select0==1);
00905             assert(field_select1==0 ||field_select1==1);
00906             init_interlaced_ref(s, 0);
00907 
00908             if(p_type){
00909                 s->p_field_select_table[0][mb_xy]= field_select0;
00910                 s->p_field_select_table[1][mb_xy]= field_select1;
00911                 *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
00912                 *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
00913                 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
00914             }else{
00915                 s->b_field_select_table[0][0][mb_xy]= field_select0;
00916                 s->b_field_select_table[0][1][mb_xy]= field_select1;
00917                 *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
00918                 *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
00919                 s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
00920             }
00921 
00922             x = p->f.motion_val[0][xy ][0];
00923             y = p->f.motion_val[0][xy ][1];
00924             d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
00925             x = p->f.motion_val[0][xy2][0];
00926             y = p->f.motion_val[0][xy2][1];
00927             d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
00928         }
00929         if(USES_LIST(mb_type, 1)){
00930             int field_select0 = p->f.ref_index[1][4 * mb_xy    ];
00931             int field_select1 = p->f.ref_index[1][4 * mb_xy + 2];
00932             assert(field_select0==0 ||field_select0==1);
00933             assert(field_select1==0 ||field_select1==1);
00934             init_interlaced_ref(s, 2);
00935 
00936             s->b_field_select_table[1][0][mb_xy]= field_select0;
00937             s->b_field_select_table[1][1][mb_xy]= field_select1;
00938             *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy ];
00939             *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy2];
00940             if(USES_LIST(mb_type, 0)){
00941                 s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
00942             }else{
00943                 s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
00944             }
00945 
00946             x = p->f.motion_val[1][xy ][0];
00947             y = p->f.motion_val[1][xy ][1];
00948             d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
00949             x = p->f.motion_val[1][xy2][0];
00950             y = p->f.motion_val[1][xy2][1];
00951             d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
00952             //FIXME bidir scores
00953         }
00954         c->stride>>=1;
00955         c->uvstride>>=1;
00956     }else if(IS_8X8(mb_type)){
00957         if(!(s->flags & CODEC_FLAG_4MV)){
00958             av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
00959             return INT_MAX/2;
00960         }
00961         cmpf= s->dsp.sse[1];
00962         chroma_cmpf= s->dsp.sse[1];
00963         init_mv4_ref(c);
00964         for(i=0; i<4; i++){
00965             xy= s->block_index[i];
00966             x= p->f.motion_val[0][xy][0];
00967             y= p->f.motion_val[0][xy][1];
00968             d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
00969         }
00970         s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
00971     }else{
00972         if(USES_LIST(mb_type, 0)){
00973             if(p_type){
00974                 *(uint32_t*)s->p_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
00975                 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
00976             }else if(USES_LIST(mb_type, 1)){
00977                 *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
00978                 *(uint32_t*)s->b_bidir_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
00979                 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
00980             }else{
00981                 *(uint32_t*)s->b_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
00982                 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
00983             }
00984             x = p->f.motion_val[0][xy][0];
00985             y = p->f.motion_val[0][xy][1];
00986             d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
00987         }else if(USES_LIST(mb_type, 1)){
00988             *(uint32_t*)s->b_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
00989             s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
00990 
00991             x = p->f.motion_val[1][xy][0];
00992             y = p->f.motion_val[1][xy][1];
00993             d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
00994         }else
00995             s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
00996     }
00997     return d;
00998 }
00999 
01000 void ff_estimate_p_frame_motion(MpegEncContext * s,
01001                                 int mb_x, int mb_y)
01002 {
01003     MotionEstContext * const c= &s->me;
01004     uint8_t *pix, *ppix;
01005     int sum, mx, my, dmin;
01006     int varc;            
01007     int vard;            
01008     int P[10][2];
01009     const int shift= 1+s->quarter_sample;
01010     int mb_type=0;
01011     Picture * const pic= &s->current_picture;
01012 
01013     init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
01014 
01015     assert(s->quarter_sample==0 || s->quarter_sample==1);
01016     assert(s->linesize == c->stride);
01017     assert(s->uvlinesize == c->uvstride);
01018 
01019     c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
01020     c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
01021     c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
01022     c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
01023 
01024     get_limits(s, 16*mb_x, 16*mb_y);
01025     c->skip=0;
01026 
01027     /* intra / predictive decision */
01028     pix = c->src[0][0];
01029     sum = s->dsp.pix_sum(pix, s->linesize);
01030     varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500;
01031 
01032     pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
01033     pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
01034     c->mb_var_sum_temp += (varc+128)>>8;
01035 
01036     if(c->avctx->me_threshold){
01037         vard= check_input_motion(s, mb_x, mb_y, 1);
01038 
01039         if((vard+128)>>8 < c->avctx->me_threshold){
01040             int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
01041             int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
01042             pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
01043             c->mc_mb_var_sum_temp += (vard+128)>>8;
01044             c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
01045             return;
01046         }
01047         if((vard+128)>>8 < c->avctx->mb_threshold)
01048             mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
01049     }
01050 
01051     switch(s->me_method) {
01052     case ME_ZERO:
01053     default:
01054         no_motion_search(s, &mx, &my);
01055         mx-= mb_x*16;
01056         my-= mb_y*16;
01057         dmin = 0;
01058         break;
01059     case ME_X1:
01060     case ME_EPZS:
01061        {
01062             const int mot_stride = s->b8_stride;
01063             const int mot_xy = s->block_index[0];
01064 
01065             P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
01066             P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
01067 
01068             if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
01069 
01070             if(!s->first_slice_line) {
01071                 P_TOP[0]      = s->current_picture.f.motion_val[0][mot_xy - mot_stride    ][0];
01072                 P_TOP[1]      = s->current_picture.f.motion_val[0][mot_xy - mot_stride    ][1];
01073                 P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][0];
01074                 P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][1];
01075                 if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
01076                 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
01077                 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
01078 
01079                 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
01080                 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
01081 
01082                 if(s->out_format == FMT_H263){
01083                     c->pred_x = P_MEDIAN[0];
01084                     c->pred_y = P_MEDIAN[1];
01085                 }else { /* mpeg1 at least */
01086                     c->pred_x= P_LEFT[0];
01087                     c->pred_y= P_LEFT[1];
01088                 }
01089             }else{
01090                 c->pred_x= P_LEFT[0];
01091                 c->pred_y= P_LEFT[1];
01092             }
01093 
01094         }
01095         dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
01096 
01097         break;
01098     }
01099 
01100     /* At this point (mx,my) are full-pell and the relative displacement */
01101     ppix = c->ref[0][0] + (my * s->linesize) + mx;
01102 
01103     vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
01104 
01105     pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
01106 //    pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
01107     c->mc_mb_var_sum_temp += (vard+128)>>8;
01108 
01109     if(mb_type){
01110         int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
01111         int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
01112         c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
01113 
01114         if(mb_type == CANDIDATE_MB_TYPE_INTER){
01115             c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
01116             set_p_mv_tables(s, mx, my, 1);
01117         }else{
01118             mx <<=shift;
01119             my <<=shift;
01120         }
01121         if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
01122             h263_mv4_search(s, mx, my, shift);
01123 
01124             set_p_mv_tables(s, mx, my, 0);
01125         }
01126         if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
01127             interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
01128         }
01129     }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
01130         int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
01131         int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
01132         c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
01133 
01134         if (vard*2 + 200*256 > varc)
01135             mb_type|= CANDIDATE_MB_TYPE_INTRA;
01136         if (varc*2 + 200*256 > vard || s->qscale > 24){
01137 //        if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
01138             mb_type|= CANDIDATE_MB_TYPE_INTER;
01139             c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
01140             if(s->flags&CODEC_FLAG_MV0)
01141                 if(mx || my)
01142                     mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
01143         }else{
01144             mx <<=shift;
01145             my <<=shift;
01146         }
01147         if((s->flags&CODEC_FLAG_4MV)
01148            && !c->skip && varc>50<<8 && vard>10<<8){
01149             if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
01150                 mb_type|=CANDIDATE_MB_TYPE_INTER4V;
01151 
01152             set_p_mv_tables(s, mx, my, 0);
01153         }else
01154             set_p_mv_tables(s, mx, my, 1);
01155         if((s->flags&CODEC_FLAG_INTERLACED_ME)
01156            && !c->skip){ //FIXME varc/d checks
01157             if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
01158                 mb_type |= CANDIDATE_MB_TYPE_INTER_I;
01159         }
01160     }else{
01161         int intra_score, i;
01162         mb_type= CANDIDATE_MB_TYPE_INTER;
01163 
01164         dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
01165         if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
01166             dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
01167 
01168         if((s->flags&CODEC_FLAG_4MV)
01169            && !c->skip && varc>50<<8 && vard>10<<8){
01170             int dmin4= h263_mv4_search(s, mx, my, shift);
01171             if(dmin4 < dmin){
01172                 mb_type= CANDIDATE_MB_TYPE_INTER4V;
01173                 dmin=dmin4;
01174             }
01175         }
01176         if((s->flags&CODEC_FLAG_INTERLACED_ME)
01177            && !c->skip){ //FIXME varc/d checks
01178             int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
01179             if(dmin_i < dmin){
01180                 mb_type = CANDIDATE_MB_TYPE_INTER_I;
01181                 dmin= dmin_i;
01182             }
01183         }
01184 
01185 //        pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
01186         set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
01187 
01188         /* get intra luma score */
01189         if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
01190             intra_score= varc - 500;
01191         }else{
01192             unsigned mean = (sum+128)>>8;
01193             mean*= 0x01010101;
01194 
01195             for(i=0; i<16; i++){
01196                 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
01197                 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
01198                 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
01199                 *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
01200             }
01201 
01202             intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
01203         }
01204         intra_score += c->mb_penalty_factor*16;
01205 
01206         if(intra_score < dmin){
01207             mb_type= CANDIDATE_MB_TYPE_INTRA;
01208             s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
01209         }else
01210             s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = 0;
01211 
01212         {
01213             int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
01214             int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
01215             c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
01216         }
01217     }
01218 
01219     s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
01220 }
01221 
01222 int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
01223                                     int mb_x, int mb_y)
01224 {
01225     MotionEstContext * const c= &s->me;
01226     int mx, my, dmin;
01227     int P[10][2];
01228     const int shift= 1+s->quarter_sample;
01229     const int xy= mb_x + mb_y*s->mb_stride;
01230     init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
01231 
01232     assert(s->quarter_sample==0 || s->quarter_sample==1);
01233 
01234     c->pre_penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
01235     c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
01236 
01237     get_limits(s, 16*mb_x, 16*mb_y);
01238     c->skip=0;
01239 
01240     P_LEFT[0]       = s->p_mv_table[xy + 1][0];
01241     P_LEFT[1]       = s->p_mv_table[xy + 1][1];
01242 
01243     if(P_LEFT[0]       < (c->xmin<<shift)) P_LEFT[0]       = (c->xmin<<shift);
01244 
01245     /* special case for first line */
01246     if (s->first_slice_line) {
01247         c->pred_x= P_LEFT[0];
01248         c->pred_y= P_LEFT[1];
01249         P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
01250         P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
01251     } else {
01252         P_TOP[0]      = s->p_mv_table[xy + s->mb_stride    ][0];
01253         P_TOP[1]      = s->p_mv_table[xy + s->mb_stride    ][1];
01254         P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
01255         P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
01256         if(P_TOP[1]      < (c->ymin<<shift)) P_TOP[1]     = (c->ymin<<shift);
01257         if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
01258         if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
01259 
01260         P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
01261         P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
01262 
01263         c->pred_x = P_MEDIAN[0];
01264         c->pred_y = P_MEDIAN[1];
01265     }
01266 
01267     dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
01268 
01269     s->p_mv_table[xy][0] = mx<<shift;
01270     s->p_mv_table[xy][1] = my<<shift;
01271 
01272     return dmin;
01273 }
01274 
01275 static int ff_estimate_motion_b(MpegEncContext * s,
01276                        int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
01277 {
01278     MotionEstContext * const c= &s->me;
01279     int mx, my, dmin;
01280     int P[10][2];
01281     const int shift= 1+s->quarter_sample;
01282     const int mot_stride = s->mb_stride;
01283     const int mot_xy = mb_y*mot_stride + mb_x;
01284     uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
01285     int mv_scale;
01286 
01287     c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
01288     c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
01289     c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
01290     c->current_mv_penalty= mv_penalty;
01291 
01292     get_limits(s, 16*mb_x, 16*mb_y);
01293 
01294     switch(s->me_method) {
01295     case ME_ZERO:
01296     default:
01297         no_motion_search(s, &mx, &my);
01298         dmin = 0;
01299         mx-= mb_x*16;
01300         my-= mb_y*16;
01301         break;
01302     case ME_X1:
01303     case ME_EPZS:
01304         P_LEFT[0] = mv_table[mot_xy - 1][0];
01305         P_LEFT[1] = mv_table[mot_xy - 1][1];
01306 
01307         if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
01308 
01309         /* special case for first line */
01310         if (!s->first_slice_line) {
01311             P_TOP[0]      = mv_table[mot_xy - mot_stride    ][0];
01312             P_TOP[1]      = mv_table[mot_xy - mot_stride    ][1];
01313             P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
01314             P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
01315             if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
01316             if (P_TOPRIGHT[0] < (c->xmin << shift)) P_TOPRIGHT[0] = (c->xmin << shift);
01317             if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
01318 
01319             P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
01320             P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
01321         }
01322         c->pred_x = P_LEFT[0];
01323         c->pred_y = P_LEFT[1];
01324 
01325         if(mv_table == s->b_forw_mv_table){
01326             mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
01327         }else{
01328             mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
01329         }
01330 
01331         dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
01332 
01333         break;
01334     }
01335 
01336     dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
01337 
01338     if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
01339         dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
01340 
01341 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
01342 //    s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
01343     mv_table[mot_xy][0]= mx;
01344     mv_table[mot_xy][1]= my;
01345 
01346     return dmin;
01347 }
01348 
01349 static inline int check_bidir_mv(MpegEncContext * s,
01350                    int motion_fx, int motion_fy,
01351                    int motion_bx, int motion_by,
01352                    int pred_fx, int pred_fy,
01353                    int pred_bx, int pred_by,
01354                    int size, int h)
01355 {
01356     //FIXME optimize?
01357     //FIXME better f_code prediction (max mv & distance)
01358     //FIXME pointers
01359     MotionEstContext * const c= &s->me;
01360     uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
01361     uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
01362     int stride= c->stride;
01363     uint8_t *dest_y = c->scratchpad;
01364     uint8_t *ptr;
01365     int dxy;
01366     int src_x, src_y;
01367     int fbmin;
01368     uint8_t **src_data= c->src[0];
01369     uint8_t **ref_data= c->ref[0];
01370     uint8_t **ref2_data= c->ref[2];
01371 
01372     if(s->quarter_sample){
01373         dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
01374         src_x = motion_fx >> 2;
01375         src_y = motion_fy >> 2;
01376 
01377         ptr = ref_data[0] + (src_y * stride) + src_x;
01378         s->dsp.put_qpel_pixels_tab[0][dxy](dest_y    , ptr    , stride);
01379 
01380         dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
01381         src_x = motion_bx >> 2;
01382         src_y = motion_by >> 2;
01383 
01384         ptr = ref2_data[0] + (src_y * stride) + src_x;
01385         s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y    , ptr    , stride);
01386     }else{
01387         dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
01388         src_x = motion_fx >> 1;
01389         src_y = motion_fy >> 1;
01390 
01391         ptr = ref_data[0] + (src_y * stride) + src_x;
01392         s->dsp.put_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
01393 
01394         dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
01395         src_x = motion_bx >> 1;
01396         src_y = motion_by >> 1;
01397 
01398         ptr = ref2_data[0] + (src_y * stride) + src_x;
01399         s->dsp.avg_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
01400     }
01401 
01402     fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
01403            +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
01404            + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
01405 
01406     if(c->avctx->mb_cmp&FF_CMP_CHROMA){
01407     }
01408     //FIXME CHROMA !!!
01409 
01410     return fbmin;
01411 }
01412 
01413 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
01414 static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
01415 {
01416     MotionEstContext * const c= &s->me;
01417     const int mot_stride = s->mb_stride;
01418     const int xy = mb_y *mot_stride + mb_x;
01419     int fbmin;
01420     int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
01421     int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
01422     int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
01423     int pred_by= s->b_bidir_back_mv_table[xy-1][1];
01424     int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
01425     int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
01426     int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
01427     int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
01428     const int flags= c->sub_flags;
01429     const int qpel= flags&FLAG_QPEL;
01430     const int shift= 1+qpel;
01431     const int xmin= c->xmin<<shift;
01432     const int ymin= c->ymin<<shift;
01433     const int xmax= c->xmax<<shift;
01434     const int ymax= c->ymax<<shift;
01435 #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
01436 #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
01437     int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
01438     uint8_t map[256];
01439 
01440     memset(map,0,sizeof(map));
01441     map[hashidx&255] = 1;
01442 
01443     fbmin= check_bidir_mv(s, motion_fx, motion_fy,
01444                           motion_bx, motion_by,
01445                           pred_fx, pred_fy,
01446                           pred_bx, pred_by,
01447                           0, 16);
01448 
01449     if(s->avctx->bidir_refine){
01450         int end;
01451         static const uint8_t limittab[5]={0,8,32,64,80};
01452         const int limit= limittab[s->avctx->bidir_refine];
01453         static const int8_t vect[][4]={
01454 { 0, 0, 0, 1}, { 0, 0, 0,-1}, { 0, 0, 1, 0}, { 0, 0,-1, 0}, { 0, 1, 0, 0}, { 0,-1, 0, 0}, { 1, 0, 0, 0}, {-1, 0, 0, 0},
01455 
01456 { 0, 0, 1, 1}, { 0, 0,-1,-1}, { 0, 1, 1, 0}, { 0,-1,-1, 0}, { 1, 1, 0, 0}, {-1,-1, 0, 0}, { 1, 0, 0, 1}, {-1, 0, 0,-1},
01457 { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
01458 { 0, 0,-1, 1}, { 0, 0, 1,-1}, { 0,-1, 1, 0}, { 0, 1,-1, 0}, {-1, 1, 0, 0}, { 1,-1, 0, 0}, { 1, 0, 0,-1}, {-1, 0, 0, 1},
01459 { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
01460 
01461 { 0, 1, 1, 1}, { 0,-1,-1,-1}, { 1, 1, 1, 0}, {-1,-1,-1, 0}, { 1, 1, 0, 1}, {-1,-1, 0,-1}, { 1, 0, 1, 1}, {-1, 0,-1,-1},
01462 { 0,-1, 1, 1}, { 0, 1,-1,-1}, {-1, 1, 1, 0}, { 1,-1,-1, 0}, { 1, 1, 0,-1}, {-1,-1, 0, 1}, { 1, 0,-1, 1}, {-1, 0, 1,-1},
01463 { 0, 1,-1, 1}, { 0,-1, 1,-1}, { 1,-1, 1, 0}, {-1, 1,-1, 0}, {-1, 1, 0, 1}, { 1,-1, 0,-1}, { 1, 0, 1,-1}, {-1, 0,-1, 1},
01464 { 0, 1, 1,-1}, { 0,-1,-1, 1}, { 1, 1,-1, 0}, {-1,-1, 1, 0}, { 1,-1, 0, 1}, {-1, 1, 0,-1}, {-1, 0, 1, 1}, { 1, 0,-1,-1},
01465 
01466 { 1, 1, 1, 1}, {-1,-1,-1,-1},
01467 { 1, 1, 1,-1}, {-1,-1,-1, 1}, { 1, 1,-1, 1}, {-1,-1, 1,-1}, { 1,-1, 1, 1}, {-1, 1,-1,-1}, {-1, 1, 1, 1}, { 1,-1,-1,-1},
01468 { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
01469         };
01470         static const uint8_t hash[]={
01471 HASH8( 0, 0, 0, 1), HASH8( 0, 0, 0,-1), HASH8( 0, 0, 1, 0), HASH8( 0, 0,-1, 0), HASH8( 0, 1, 0, 0), HASH8( 0,-1, 0, 0), HASH8( 1, 0, 0, 0), HASH8(-1, 0, 0, 0),
01472 
01473 HASH8( 0, 0, 1, 1), HASH8( 0, 0,-1,-1), HASH8( 0, 1, 1, 0), HASH8( 0,-1,-1, 0), HASH8( 1, 1, 0, 0), HASH8(-1,-1, 0, 0), HASH8( 1, 0, 0, 1), HASH8(-1, 0, 0,-1),
01474 HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
01475 HASH8( 0, 0,-1, 1), HASH8( 0, 0, 1,-1), HASH8( 0,-1, 1, 0), HASH8( 0, 1,-1, 0), HASH8(-1, 1, 0, 0), HASH8( 1,-1, 0, 0), HASH8( 1, 0, 0,-1), HASH8(-1, 0, 0, 1),
01476 HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
01477 
01478 HASH8( 0, 1, 1, 1), HASH8( 0,-1,-1,-1), HASH8( 1, 1, 1, 0), HASH8(-1,-1,-1, 0), HASH8( 1, 1, 0, 1), HASH8(-1,-1, 0,-1), HASH8( 1, 0, 1, 1), HASH8(-1, 0,-1,-1),
01479 HASH8( 0,-1, 1, 1), HASH8( 0, 1,-1,-1), HASH8(-1, 1, 1, 0), HASH8( 1,-1,-1, 0), HASH8( 1, 1, 0,-1), HASH8(-1,-1, 0, 1), HASH8( 1, 0,-1, 1), HASH8(-1, 0, 1,-1),
01480 HASH8( 0, 1,-1, 1), HASH8( 0,-1, 1,-1), HASH8( 1,-1, 1, 0), HASH8(-1, 1,-1, 0), HASH8(-1, 1, 0, 1), HASH8( 1,-1, 0,-1), HASH8( 1, 0, 1,-1), HASH8(-1, 0,-1, 1),
01481 HASH8( 0, 1, 1,-1), HASH8( 0,-1,-1, 1), HASH8( 1, 1,-1, 0), HASH8(-1,-1, 1, 0), HASH8( 1,-1, 0, 1), HASH8(-1, 1, 0,-1), HASH8(-1, 0, 1, 1), HASH8( 1, 0,-1,-1),
01482 
01483 HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
01484 HASH8( 1, 1, 1,-1), HASH8(-1,-1,-1, 1), HASH8( 1, 1,-1, 1), HASH8(-1,-1, 1,-1), HASH8( 1,-1, 1, 1), HASH8(-1, 1,-1,-1), HASH8(-1, 1, 1, 1), HASH8( 1,-1,-1,-1),
01485 HASH8( 1, 1,-1,-1), HASH8(-1,-1, 1, 1), HASH8( 1,-1,-1, 1), HASH8(-1, 1, 1,-1), HASH8( 1,-1, 1,-1), HASH8(-1, 1,-1, 1),
01486 };
01487 
01488 #define CHECK_BIDIR(fx,fy,bx,by)\
01489     if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
01490        &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
01491        &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
01492         int score;\
01493         map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
01494         score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\
01495         if(score < fbmin){\
01496             hashidx += HASH(fx,fy,bx,by);\
01497             fbmin= score;\
01498             motion_fx+=fx;\
01499             motion_fy+=fy;\
01500             motion_bx+=bx;\
01501             motion_by+=by;\
01502             end=0;\
01503         }\
01504     }
01505 #define CHECK_BIDIR2(a,b,c,d)\
01506 CHECK_BIDIR(a,b,c,d)\
01507 CHECK_BIDIR(-(a),-(b),-(c),-(d))
01508 
01509         do{
01510             int i;
01511             int borderdist=0;
01512             end=1;
01513 
01514             CHECK_BIDIR2(0,0,0,1)
01515             CHECK_BIDIR2(0,0,1,0)
01516             CHECK_BIDIR2(0,1,0,0)
01517             CHECK_BIDIR2(1,0,0,0)
01518 
01519             for(i=8; i<limit; i++){
01520                 int fx= motion_fx+vect[i][0];
01521                 int fy= motion_fy+vect[i][1];
01522                 int bx= motion_bx+vect[i][2];
01523                 int by= motion_by+vect[i][3];
01524                 if(borderdist<=0){
01525                     int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
01526                     int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
01527                     if((a|b) < 0)
01528                         map[(hashidx+hash[i])&255] = 1;
01529                 }
01530                 if(!map[(hashidx+hash[i])&255]){
01531                     int score;
01532                     map[(hashidx+hash[i])&255] = 1;
01533                     score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
01534                     if(score < fbmin){
01535                         hashidx += hash[i];
01536                         fbmin= score;
01537                         motion_fx=fx;
01538                         motion_fy=fy;
01539                         motion_bx=bx;
01540                         motion_by=by;
01541                         end=0;
01542                         borderdist--;
01543                         if(borderdist<=0){
01544                             int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
01545                             int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
01546                             borderdist= FFMIN(a,b);
01547                         }
01548                     }
01549                 }
01550             }
01551         }while(!end);
01552     }
01553 
01554     s->b_bidir_forw_mv_table[xy][0]= motion_fx;
01555     s->b_bidir_forw_mv_table[xy][1]= motion_fy;
01556     s->b_bidir_back_mv_table[xy][0]= motion_bx;
01557     s->b_bidir_back_mv_table[xy][1]= motion_by;
01558 
01559     return fbmin;
01560 }
01561 
01562 static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
01563 {
01564     MotionEstContext * const c= &s->me;
01565     int P[10][2];
01566     const int mot_stride = s->mb_stride;
01567     const int mot_xy = mb_y*mot_stride + mb_x;
01568     const int shift= 1+s->quarter_sample;
01569     int dmin, i;
01570     const int time_pp= s->pp_time;
01571     const int time_pb= s->pb_time;
01572     int mx, my, xmin, xmax, ymin, ymax;
01573     int16_t (*mv_table)[2]= s->b_direct_mv_table;
01574 
01575     c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
01576     ymin= xmin=(-32)>>shift;
01577     ymax= xmax=   31>>shift;
01578 
01579     if (IS_8X8(s->next_picture.f.mb_type[mot_xy])) {
01580         s->mv_type= MV_TYPE_8X8;
01581     }else{
01582         s->mv_type= MV_TYPE_16X16;
01583     }
01584 
01585     for(i=0; i<4; i++){
01586         int index= s->block_index[i];
01587         int min, max;
01588 
01589         c->co_located_mv[i][0] = s->next_picture.f.motion_val[0][index][0];
01590         c->co_located_mv[i][1] = s->next_picture.f.motion_val[0][index][1];
01591         c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
01592         c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
01593 //        c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
01594 //        c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
01595 
01596         max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
01597         min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
01598         max+= 16*mb_x + 1; // +-1 is for the simpler rounding
01599         min+= 16*mb_x - 1;
01600         xmax= FFMIN(xmax, s->width - max);
01601         xmin= FFMAX(xmin, - 16     - min);
01602 
01603         max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
01604         min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
01605         max+= 16*mb_y + 1; // +-1 is for the simpler rounding
01606         min+= 16*mb_y - 1;
01607         ymax= FFMIN(ymax, s->height - max);
01608         ymin= FFMAX(ymin, - 16      - min);
01609 
01610         if(s->mv_type == MV_TYPE_16X16) break;
01611     }
01612 
01613     assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
01614 
01615     if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
01616         s->b_direct_mv_table[mot_xy][0]= 0;
01617         s->b_direct_mv_table[mot_xy][1]= 0;
01618 
01619         return 256*256*256*64;
01620     }
01621 
01622     c->xmin= xmin;
01623     c->ymin= ymin;
01624     c->xmax= xmax;
01625     c->ymax= ymax;
01626     c->flags     |= FLAG_DIRECT;
01627     c->sub_flags |= FLAG_DIRECT;
01628     c->pred_x=0;
01629     c->pred_y=0;
01630 
01631     P_LEFT[0]        = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
01632     P_LEFT[1]        = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
01633 
01634     /* special case for first line */
01635     if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
01636         P_TOP[0]      = av_clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);
01637         P_TOP[1]      = av_clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);
01638         P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);
01639         P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);
01640 
01641         P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
01642         P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
01643     }
01644 
01645     dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
01646     if(c->sub_flags&FLAG_QPEL)
01647         dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
01648     else
01649         dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
01650 
01651     if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
01652         dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
01653 
01654     get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
01655 
01656     mv_table[mot_xy][0]= mx;
01657     mv_table[mot_xy][1]= my;
01658     c->flags     &= ~FLAG_DIRECT;
01659     c->sub_flags &= ~FLAG_DIRECT;
01660 
01661     return dmin;
01662 }
01663 
01664 void ff_estimate_b_frame_motion(MpegEncContext * s,
01665                              int mb_x, int mb_y)
01666 {
01667     MotionEstContext * const c= &s->me;
01668     const int penalty_factor= c->mb_penalty_factor;
01669     int fmin, bmin, dmin, fbmin, bimin, fimin;
01670     int type=0;
01671     const int xy = mb_y*s->mb_stride + mb_x;
01672     init_ref(c, s->new_picture.f.data, s->last_picture.f.data,
01673              s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);
01674 
01675     get_limits(s, 16*mb_x, 16*mb_y);
01676 
01677     c->skip=0;
01678 
01679     if (s->codec_id == CODEC_ID_MPEG4 && s->next_picture.f.mbskip_table[xy]) {
01680         int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
01681 
01682         score= ((unsigned)(score*score + 128*256))>>16;
01683         c->mc_mb_var_sum_temp += score;
01684         s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
01685         s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
01686 
01687         return;
01688     }
01689 
01690     if(c->avctx->me_threshold){
01691         int vard= check_input_motion(s, mb_x, mb_y, 0);
01692 
01693         if((vard+128)>>8 < c->avctx->me_threshold){
01694 //            pix = c->src[0][0];
01695 //            sum = s->dsp.pix_sum(pix, s->linesize);
01696 //            varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
01697 
01698 //            pic->mb_var   [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
01699              s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
01700 /*            pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
01701             c->mb_var_sum_temp    += (varc+128)>>8;*/
01702             c->mc_mb_var_sum_temp += (vard+128)>>8;
01703 /*            if (vard <= 64<<8 || vard < varc) {
01704                 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
01705             }else{
01706                 c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
01707             }*/
01708             return;
01709         }
01710         if((vard+128)>>8 < c->avctx->mb_threshold){
01711             type= s->mb_type[mb_y*s->mb_stride + mb_x];
01712             if(type == CANDIDATE_MB_TYPE_DIRECT){
01713                 direct_search(s, mb_x, mb_y);
01714             }
01715             if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
01716                 c->skip=0;
01717                 ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
01718             }
01719             if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
01720                 c->skip=0;
01721                 ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
01722             }
01723             if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
01724                 c->skip=0;
01725                 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
01726                 interlaced_search(s, 0,
01727                                         s->b_field_mv_table[0], s->b_field_select_table[0],
01728                                         s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
01729             }
01730             if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
01731                 c->skip=0;
01732                 c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
01733                 interlaced_search(s, 2,
01734                                         s->b_field_mv_table[1], s->b_field_select_table[1],
01735                                         s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
01736             }
01737             return;
01738         }
01739     }
01740 
01741     if (s->codec_id == CODEC_ID_MPEG4)
01742         dmin= direct_search(s, mb_x, mb_y);
01743     else
01744         dmin= INT_MAX;
01745 //FIXME penalty stuff for non mpeg4
01746     c->skip=0;
01747     fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
01748 
01749     c->skip=0;
01750     bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
01751 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
01752 
01753     c->skip=0;
01754     fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
01755 //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
01756 
01757     if(s->flags & CODEC_FLAG_INTERLACED_ME){
01758 //FIXME mb type penalty
01759         c->skip=0;
01760         c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
01761         fimin= interlaced_search(s, 0,
01762                                  s->b_field_mv_table[0], s->b_field_select_table[0],
01763                                  s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
01764         c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
01765         bimin= interlaced_search(s, 2,
01766                                  s->b_field_mv_table[1], s->b_field_select_table[1],
01767                                  s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
01768     }else
01769         fimin= bimin= INT_MAX;
01770 
01771     {
01772         int score= fmin;
01773         type = CANDIDATE_MB_TYPE_FORWARD;
01774 
01775         if (dmin <= score){
01776             score = dmin;
01777             type = CANDIDATE_MB_TYPE_DIRECT;
01778         }
01779         if(bmin<score){
01780             score=bmin;
01781             type= CANDIDATE_MB_TYPE_BACKWARD;
01782         }
01783         if(fbmin<score){
01784             score=fbmin;
01785             type= CANDIDATE_MB_TYPE_BIDIR;
01786         }
01787         if(fimin<score){
01788             score=fimin;
01789             type= CANDIDATE_MB_TYPE_FORWARD_I;
01790         }
01791         if(bimin<score){
01792             score=bimin;
01793             type= CANDIDATE_MB_TYPE_BACKWARD_I;
01794         }
01795 
01796         score= ((unsigned)(score*score + 128*256))>>16;
01797         c->mc_mb_var_sum_temp += score;
01798         s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
01799     }
01800 
01801     if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
01802         type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
01803         if(fimin < INT_MAX)
01804             type |= CANDIDATE_MB_TYPE_FORWARD_I;
01805         if(bimin < INT_MAX)
01806             type |= CANDIDATE_MB_TYPE_BACKWARD_I;
01807         if(fimin < INT_MAX && bimin < INT_MAX){
01808             type |= CANDIDATE_MB_TYPE_BIDIR_I;
01809         }
01810          //FIXME something smarter
01811         if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
01812         if(s->codec_id == CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
01813             type |= CANDIDATE_MB_TYPE_DIRECT0;
01814     }
01815 
01816     s->mb_type[mb_y*s->mb_stride + mb_x]= type;
01817 }
01818 
01819 /* find best f_code for ME which do unlimited searches */
01820 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
01821 {
01822     if(s->me_method>=ME_EPZS){
01823         int score[8];
01824         int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
01825         uint8_t * fcode_tab= s->fcode_tab;
01826         int best_fcode=-1;
01827         int best_score=-10000000;
01828 
01829         if(s->msmpeg4_version)
01830             range= FFMIN(range, 16);
01831         else if(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
01832             range= FFMIN(range, 256);
01833 
01834         for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
01835 
01836         for(y=0; y<s->mb_height; y++){
01837             int x;
01838             int xy= y*s->mb_stride;
01839             for(x=0; x<s->mb_width; x++){
01840                 if(s->mb_type[xy] & type){
01841                     int mx= mv_table[xy][0];
01842                     int my= mv_table[xy][1];
01843                     int fcode= FFMAX(fcode_tab[mx + MAX_MV],
01844                                      fcode_tab[my + MAX_MV]);
01845                     int j;
01846 
01847                         if(mx >= range || mx < -range ||
01848                            my >= range || my < -range)
01849                             continue;
01850 
01851                     for(j=0; j<fcode && j<8; j++){
01852                         if(s->pict_type==AV_PICTURE_TYPE_B || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
01853                             score[j]-= 170;
01854                     }
01855                 }
01856                 xy++;
01857             }
01858         }
01859 
01860         for(i=1; i<8; i++){
01861             if(score[i] > best_score){
01862                 best_score= score[i];
01863                 best_fcode= i;
01864             }
01865 //            printf("%d %d\n", i, score[i]);
01866         }
01867 
01868 //    printf("fcode: %d type: %d\n", i, s->pict_type);
01869         return best_fcode;
01870 /*        for(i=0; i<=MAX_FCODE; i++){
01871             printf("%d ", mv_num[i]);
01872         }
01873         printf("\n");*/
01874     }else{
01875         return 1;
01876     }
01877 }
01878 
01879 void ff_fix_long_p_mvs(MpegEncContext * s)
01880 {
01881     MotionEstContext * const c= &s->me;
01882     const int f_code= s->f_code;
01883     int y, range;
01884     assert(s->pict_type==AV_PICTURE_TYPE_P);
01885 
01886     range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
01887 
01888     assert(range <= 16 || !s->msmpeg4_version);
01889     assert(range <=256 || !(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
01890 
01891     if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
01892 
01893 //printf("%d no:%d %d//\n", clip, noclip, f_code);
01894     if(s->flags&CODEC_FLAG_4MV){
01895         const int wrap= s->b8_stride;
01896 
01897         /* clip / convert to intra 8x8 type MVs */
01898         for(y=0; y<s->mb_height; y++){
01899             int xy= y*2*wrap;
01900             int i= y*s->mb_stride;
01901             int x;
01902 
01903             for(x=0; x<s->mb_width; x++){
01904                 if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
01905                     int block;
01906                     for(block=0; block<4; block++){
01907                         int off= (block& 1) + (block>>1)*wrap;
01908                         int mx = s->current_picture.f.motion_val[0][ xy + off ][0];
01909                         int my = s->current_picture.f.motion_val[0][ xy + off ][1];
01910 
01911                         if(   mx >=range || mx <-range
01912                            || my >=range || my <-range){
01913                             s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
01914                             s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
01915                             s->current_picture.f.mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
01916                         }
01917                     }
01918                 }
01919                 xy+=2;
01920                 i++;
01921             }
01922         }
01923     }
01924 }
01925 
01930 void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
01931                      int16_t (*mv_table)[2], int f_code, int type, int truncate)
01932 {
01933     MotionEstContext * const c= &s->me;
01934     int y, h_range, v_range;
01935 
01936     // RAL: 8 in MPEG-1, 16 in MPEG-4
01937     int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
01938 
01939     if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
01940 
01941     h_range= range;
01942     v_range= field_select_table ? range>>1 : range;
01943 
01944     /* clip / convert to intra 16x16 type MVs */
01945     for(y=0; y<s->mb_height; y++){
01946         int x;
01947         int xy= y*s->mb_stride;
01948         for(x=0; x<s->mb_width; x++){
01949             if (s->mb_type[xy] & type){    // RAL: "type" test added...
01950                 if(field_select_table==NULL || field_select_table[xy] == field_select){
01951                     if(   mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
01952                        || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
01953 
01954                         if(truncate){
01955                             if     (mv_table[xy][0] > h_range-1) mv_table[xy][0]=  h_range-1;
01956                             else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
01957                             if     (mv_table[xy][1] > v_range-1) mv_table[xy][1]=  v_range-1;
01958                             else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
01959                         }else{
01960                             s->mb_type[xy] &= ~type;
01961                             s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
01962                             mv_table[xy][0]=
01963                             mv_table[xy][1]= 0;
01964                         }
01965                     }
01966                 }
01967             }
01968             xy++;
01969         }
01970     }
01971 }