• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1