]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/motion_est.c
memmove fixes (Jon Burgess)
[ffmpeg] / libavcodec / motion_est.c
index a8517fb7cd63290d84db089c46cbfe8906c1c57e..505a97d83d60f8092c745f6634edeaa9c1dfe805 100644 (file)
  *
  * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
  */
+/**
+ * @file motion_est.c
+ * Motion estimation.
+ */
 #include <stdlib.h>
 #include <stdio.h>
 #include "avcodec.h"
@@ -41,7 +47,7 @@ static inline int sad_hpel_motion_search(MpegEncContext * s,
                                  int *mx_ptr, int *my_ptr, int dmin,
                                  int xmin, int ymin, int xmax, int ymax,
                                   int pred_x, int pred_y, Picture *picture,
-                                  int n, int size, uint16_t * const mv_penalty);
+                                  int n, int size, uint8_t * const mv_penalty);
 
 static inline int update_map_generation(MpegEncContext * s)
 {
@@ -61,8 +67,8 @@ typedef struct Minima{
 }Minima;
 
 static int minima_cmp(const void *a, const void *b){
-    Minima *da = (Minima *) a;
-    Minima *db = (Minima *) b;
+    const Minima *da = (const Minima *) a;
+    const Minima *db = (const Minima *) b;
     
     return da->height - db->height;
 }
@@ -387,7 +393,7 @@ void ff_init_me(MpegEncContext *s){
     }
 }
       
-static int pix_dev(UINT8 * pix, int line_size, int mean)
+static int pix_dev(uint8_t * pix, int line_size, int mean)
 {
     int s, i, j;
 
@@ -422,7 +428,7 @@ static int full_motion_search(MpegEncContext * s,
 {
     int x1, y1, x2, y2, xx, yy, x, y;
     int mx, my, dmin, d;
-    UINT8 *pix;
+    uint8_t *pix;
 
     xx = 16 * s->mb_x;
     yy = 16 * s->mb_y;
@@ -476,7 +482,7 @@ static int log_motion_search(MpegEncContext * s,
 {
     int x1, y1, x2, y2, xx, yy, x, y;
     int mx, my, dmin, d;
-    UINT8 *pix;
+    uint8_t *pix;
 
     xx = s->mb_x << 4;
     yy = s->mb_y << 4;
@@ -552,7 +558,7 @@ static int phods_motion_search(MpegEncContext * s,
 {
     int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
     int mx, my, dminx, dminy;
-    UINT8 *pix;
+    uint8_t *pix;
 
     xx = s->mb_x << 4;
     yy = s->mb_y << 4;
@@ -651,13 +657,13 @@ static inline int sad_hpel_motion_search(MpegEncContext * s,
                                  int *mx_ptr, int *my_ptr, int dmin,
                                  int xmin, int ymin, int xmax, int ymax,
                                   int pred_x, int pred_y, Picture *picture,
-                                  int n, int size, uint16_t * const mv_penalty)
+                                  int n, int size, uint8_t * const mv_penalty)
 {
     uint8_t *ref_picture= picture->data[0];
     uint32_t *score_map= s->me.score_map;
     const int penalty_factor= s->me.sub_penalty_factor;
     int mx, my, xx, yy, dminh;
-    UINT8 *pix, *ptr;
+    uint8_t *pix, *ptr;
     op_pixels_abs_func pix_abs_x2;
     op_pixels_abs_func pix_abs_y2;
     op_pixels_abs_func pix_abs_xy2;
@@ -769,7 +775,7 @@ static inline int sad_hpel_motion_search(MpegEncContext * s,
 
 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
 {
-    const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2);
+    const int xy= s->mb_x + s->mb_y*s->mb_stride;
     
     s->p_mv_table[xy][0] = mx;
     s->p_mv_table[xy][1] = my;
@@ -791,31 +797,28 @@ static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
     }
 }
 
-static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code)
+/**
+ * get fullpel ME search limits.
+ * @param range the approximate search range for the old ME code, unused for EPZS and newer
+ */
+static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax)
 {
-    *range = 8 * (1 << (f_code - 1));
-    /* XXX: temporary kludge to avoid overflow for msmpeg4 */
-    if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
-       *range *= 2;
+    if(s->avctx->me_range) *range= s->avctx->me_range >> 1;
+    else                   *range= 16;
 
     if (s->unrestricted_mv) {
         *xmin = -16;
         *ymin = -16;
-        if (s->h263_plus)
-            *range *= 2;
-        if(s->avctx->codec->id!=CODEC_ID_MPEG4){
-            *xmax = s->mb_width*16;
-            *ymax = s->mb_height*16;
-        }else {
-            *xmax = s->width;
-            *ymax = s->height;
-        }
+        *xmax = s->mb_width*16;
+        *ymax = s->mb_height*16;
     } else {
         *xmin = 0;
         *ymin = 0;
         *xmax = s->mb_width*16 - 16;
         *ymax = s->mb_height*16 - 16;
     }
+    
+    //FIXME try to limit x/y min/max if me_range is set
 }
 
 static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift)
@@ -823,7 +826,7 @@ static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xma
     int block;
     int P[10][2];
     int dmin_sum=0, mx4_sum=0, my4_sum=0;
-    uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
+    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
 
     for(block=0; block<4; block++){
         int mx4, my4;
@@ -964,7 +967,7 @@ static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xma
 void ff_estimate_p_frame_motion(MpegEncContext * s,
                                 int mb_x, int mb_y)
 {
-    UINT8 *pix, *ppix;
+    uint8_t *pix, *ppix;
     int sum, varc, vard, mx, my, range, dmin, xx, yy;
     int xmin, ymin, xmax, ymax;
     int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
@@ -974,7 +977,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
     int mb_type=0;
     uint8_t *ref_picture= s->last_picture.data[0];
     Picture * const pic= &s->current_picture;
-    uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
+    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
     
     assert(s->quarter_sample==0 || s->quarter_sample==1);
 
@@ -982,7 +985,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
     s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
     s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
 
-    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
+    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax);
     rel_xmin= xmin - mb_x*16;
     rel_xmax= xmax - mb_x*16;
     rel_ymin= ymin - mb_y*16;
@@ -1068,10 +1071,10 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
     vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize)+128)>>8;
 
 //printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
-    pic->mb_var   [s->mb_width * mb_y + mb_x] = varc;
-    pic->mc_mb_var[s->mb_width * mb_y + mb_x] = vard;
-    pic->mb_mean  [s->mb_width * mb_y + mb_x] = (sum+128)>>8;
-//    pic->mb_cmp_score[s->mb_width * mb_y + mb_x] = dmin; 
+    pic->mb_var   [s->mb_stride * mb_y + mb_x] = varc;
+    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
+    pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
+//    pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 
     pic->mb_var_sum    += varc;
     pic->mc_mb_var_sum += vard;
 //printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
@@ -1080,7 +1083,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
     printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
           varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
 #endif
-    if(s->flags&CODEC_FLAG_HQ){
+    if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
         if (vard <= 64 || vard < varc)
             s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
         else
@@ -1121,7 +1124,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
                 dmin=dmin4;
             }
         }
-        pic->mb_cmp_score[s->mb_width * mb_y + mb_x] = dmin; 
+        pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 
         set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V);
         
         if (vard <= 64 || vard < varc) {
@@ -1131,7 +1134,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
         }
     }
 
-    s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
+    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
 }
 
 int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
@@ -1143,15 +1146,14 @@ int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
     int pred_x=0, pred_y=0;
     int P[10][2];
     const int shift= 1+s->quarter_sample;
-    uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
-    const int mv_stride= s->mb_width + 2;
-    const int xy= mb_x + 1 + (mb_y + 1)*mv_stride;
+    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
+    const int xy= mb_x + mb_y*s->mb_stride;
     
     assert(s->quarter_sample==0 || s->quarter_sample==1);
 
     s->me.pre_penalty_factor    = get_penalty_factor(s, s->avctx->me_pre_cmp);
 
-    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
+    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax);
     rel_xmin= xmin - mb_x*16;
     rel_xmax= xmax - mb_x*16;
     rel_ymin= ymin - mb_y*16;
@@ -1170,10 +1172,10 @@ int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
         P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
         P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME 
     } else {
-        P_TOP[0]      = s->p_mv_table[xy + mv_stride    ][0];
-        P_TOP[1]      = s->p_mv_table[xy + mv_stride    ][1];
-        P_TOPRIGHT[0] = s->p_mv_table[xy + mv_stride - 1][0];
-        P_TOPRIGHT[1] = s->p_mv_table[xy + mv_stride - 1][1];
+        P_TOP[0]      = s->p_mv_table[xy + s->mb_stride    ][0];
+        P_TOP[1]      = s->p_mv_table[xy + s->mb_stride    ][1];
+        P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
+        P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
         if(P_TOP[1]      < (rel_ymin<<shift)) P_TOP[1]     = (rel_ymin<<shift);
         if(P_TOPRIGHT[0] > (rel_xmax<<shift)) P_TOPRIGHT[0]= (rel_xmax<<shift);
         if(P_TOPRIGHT[1] < (rel_ymin<<shift)) P_TOPRIGHT[1]= (rel_ymin<<shift);
@@ -1193,7 +1195,7 @@ int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
     return dmin;
 }
 
-int ff_estimate_motion_b(MpegEncContext * s,
+static int ff_estimate_motion_b(MpegEncContext * s,
                        int mb_x, int mb_y, int16_t (*mv_table)[2], Picture *picture, int f_code)
 {
     int mx, my, range, dmin;
@@ -1202,17 +1204,17 @@ int ff_estimate_motion_b(MpegEncContext * s,
     int pred_x=0, pred_y=0;
     int P[10][2];
     const int shift= 1+s->quarter_sample;
-    const int mot_stride = s->mb_width + 2;
-    const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
+    const int mot_stride = s->mb_stride;
+    const int mot_xy = mb_y*mot_stride + mb_x;
     uint8_t * const ref_picture= picture->data[0];
-    uint16_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV;
+    uint8_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV;
     int mv_scale;
         
     s->me.penalty_factor    = get_penalty_factor(s, s->avctx->me_cmp);
     s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
     s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp);
 
-    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
+    get_limits(s, &range, &xmin, &ymin, &xmax, &ymax);
     rel_xmin= xmin - mb_x*16;
     rel_xmax= xmax - mb_x*16;
     rel_ymin= ymin - mb_y*16;
@@ -1302,7 +1304,7 @@ static inline int check_bidir_mv(MpegEncContext * s,
     //FIXME optimize?
     //FIXME move into template?
     //FIXME better f_code prediction (max mv & distance)
-    UINT16 *mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
+    uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
     uint8_t *dest_y = s->me.scratchpad;
     uint8_t *ptr;
     int dxy;
@@ -1313,8 +1315,8 @@ static inline int check_bidir_mv(MpegEncContext * s,
         dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
         src_x = mb_x * 16 + (motion_fx >> 2);
         src_y = mb_y * 16 + (motion_fy >> 2);
-        assert(src_x >=-16 && src_x<=s->width);
-        assert(src_y >=-16 && src_y<=s->height);
+        assert(src_x >=-16 && src_x<=s->h_edge_pos);
+        assert(src_y >=-16 && src_y<=s->v_edge_pos);
 
         ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x;
         s->dsp.put_qpel_pixels_tab[0][dxy](dest_y    , ptr    , s->linesize);
@@ -1322,8 +1324,8 @@ static inline int check_bidir_mv(MpegEncContext * s,
         dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
         src_x = mb_x * 16 + (motion_bx >> 2);
         src_y = mb_y * 16 + (motion_by >> 2);
-        assert(src_x >=-16 && src_x<=s->width);
-        assert(src_y >=-16 && src_y<=s->height);
+        assert(src_x >=-16 && src_x<=s->h_edge_pos);
+        assert(src_y >=-16 && src_y<=s->v_edge_pos);
     
         ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x;
         s->dsp.avg_qpel_pixels_tab[0][dxy](dest_y    , ptr    , s->linesize);
@@ -1331,8 +1333,8 @@ static inline int check_bidir_mv(MpegEncContext * s,
         dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
         src_x = mb_x * 16 + (motion_fx >> 1);
         src_y = mb_y * 16 + (motion_fy >> 1);
-        assert(src_x >=-16 && src_x<=s->width);
-        assert(src_y >=-16 && src_y<=s->height);
+        assert(src_x >=-16 && src_x<=s->h_edge_pos);
+        assert(src_y >=-16 && src_y<=s->v_edge_pos);
 
         ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x;
         s->dsp.put_pixels_tab[0][dxy](dest_y    , ptr    , s->linesize, 16);
@@ -1340,8 +1342,8 @@ static inline int check_bidir_mv(MpegEncContext * s,
         dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
         src_x = mb_x * 16 + (motion_bx >> 1);
         src_y = mb_y * 16 + (motion_by >> 1);
-        assert(src_x >=-16 && src_x<=s->width);
-        assert(src_y >=-16 && src_y<=s->height);
+        assert(src_x >=-16 && src_x<=s->h_edge_pos);
+        assert(src_y >=-16 && src_y<=s->v_edge_pos);
     
         ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x;
         s->dsp.avg_pixels_tab[0][dxy](dest_y    , ptr    , s->linesize, 16);
@@ -1362,8 +1364,8 @@ static inline int check_bidir_mv(MpegEncContext * s,
 static inline int bidir_refine(MpegEncContext * s,
                                   int mb_x, int mb_y)
 {
-    const int mot_stride = s->mb_width + 2;
-    const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
+    const int mot_stride = s->mb_stride;
+    const int xy = mb_y *mot_stride + mb_x;
     int fbmin;
     int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
     int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
@@ -1389,20 +1391,20 @@ static inline int direct_search(MpegEncContext * s,
                                 int mb_x, int mb_y)
 {
     int P[10][2];
-    const int mot_stride = s->mb_width + 2;
-    const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
+    const int mot_stride = s->mb_stride;
+    const int mot_xy = mb_y*mot_stride + mb_x;
     const int shift= 1+s->quarter_sample;
     int dmin, i;
     const int time_pp= s->pp_time;
     const int time_pb= s->pb_time;
     int mx, my, xmin, xmax, ymin, ymax;
     int16_t (*mv_table)[2]= s->b_direct_mv_table;
-    uint16_t * const mv_penalty= s->me.mv_penalty[1] + MAX_MV;
+    uint8_t * const mv_penalty= s->me.mv_penalty[1] + MAX_MV;
     
     ymin= xmin=(-32)>>shift;
     ymax= xmax=   31>>shift;
 
-    if(s->co_located_type_table[mb_x + mb_y*s->mb_width]==CO_LOCATED_TYPE_4MV){
+    if(IS_8X8(s->next_picture.mb_type[mot_xy])){
         s->mv_type= MV_TYPE_8X8;
     }else{
         s->mv_type= MV_TYPE_16X16;
@@ -1518,15 +1520,15 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
         
         score= ((unsigned)(score*score + 128*256))>>16;
         s->current_picture.mc_mb_var_sum += score;
-        s->current_picture.mc_mb_var[mb_y*s->mb_width + mb_x] = score; //FIXME use SSE
+        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
     }
 
-    if(s->flags&CODEC_FLAG_HQ){
+    if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
         type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT; //FIXME something smarter
         if(dmin>256*256*16) type&= ~MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
     }
 
-    s->mb_type[mb_y*s->mb_width + mb_x]= type;
+    s->mb_type[mb_y*s->mb_stride + mb_x]= type;
 }
 
 /* find best f_code for ME which do unlimited searches */
@@ -1535,7 +1537,7 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
     if(s->me_method>=ME_EPZS){
         int score[8];
         int i, y;
-        UINT8 * fcode_tab= s->fcode_tab;
+        uint8_t * fcode_tab= s->fcode_tab;
         int best_fcode=-1;
         int best_score=-10000000;
 
@@ -1543,20 +1545,18 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
 
         for(y=0; y<s->mb_height; y++){
             int x;
-            int xy= (y+1)* (s->mb_width+2) + 1;
-            i= y*s->mb_width;
+            int xy= y*s->mb_stride;
             for(x=0; x<s->mb_width; x++){
-                if(s->mb_type[i] & type){
+                if(s->mb_type[xy] & type){
                     int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV],
                                      fcode_tab[mv_table[xy][1] + MAX_MV]);
                     int j;
                     
                     for(j=0; j<fcode && j<8; j++){
-                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[i] < s->current_picture.mb_var[i])
+                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
                             score[j]-= 170;
                     }
                 }
-                i++;
                 xy++;
             }
         }
@@ -1583,32 +1583,29 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
 void ff_fix_long_p_mvs(MpegEncContext * s)
 {
     const int f_code= s->f_code;
-    int y;
-    UINT8 * fcode_tab= s->fcode_tab;
-//int clip=0;
-//int noclip=0;
+    int y, range;
+
+    range = (((s->codec_id == CODEC_ID_MPEG1VIDEO) ? 8 : 16) << f_code);
+    
+    if(s->msmpeg4_version) range= 16;
+    
+    if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range;
+    
     /* clip / convert to intra 16x16 type MVs */
     for(y=0; y<s->mb_height; y++){
         int x;
-        int xy= (y+1)* (s->mb_width+2)+1;
-        int i= y*s->mb_width;
+        int xy= y*s->mb_stride;
         for(x=0; x<s->mb_width; x++){
-            if(s->mb_type[i]&MB_TYPE_INTER){
-                if(   fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code
-                   || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
-                   || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
-                   || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
-                    s->mb_type[i] &= ~MB_TYPE_INTER;
-                    s->mb_type[i] |= MB_TYPE_INTRA;
+            if(s->mb_type[xy]&MB_TYPE_INTER){
+                if(   s->p_mv_table[xy][0] >=range || s->p_mv_table[xy][0] <-range
+                   || s->p_mv_table[xy][1] >=range || s->p_mv_table[xy][1] <-range){
+                    s->mb_type[xy] &= ~MB_TYPE_INTER;
+                    s->mb_type[xy] |= MB_TYPE_INTRA;
                     s->p_mv_table[xy][0] = 0;
                     s->p_mv_table[xy][1] = 0;
-//clip++;
                 }
-//else
-//  noclip++;
             }
             xy++;
-            i++;
         }
     }
 //printf("%d no:%d %d//\n", clip, noclip, f_code);
@@ -1618,7 +1615,7 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
         /* clip / convert to intra 8x8 type MVs */
         for(y=0; y<s->mb_height; y++){
             int xy= (y*2 + 1)*wrap + 1;
-            int i= y*s->mb_width;
+            int i= y*s->mb_stride;
             int x;
 
             for(x=0; x<s->mb_width; x++){
@@ -1629,10 +1626,8 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
                         int mx= s->motion_val[ xy + off ][0];
                         int my= s->motion_val[ xy + off ][1];
 
-                        if(   fcode_tab[mx + MAX_MV] > f_code
-                           || fcode_tab[mx + MAX_MV] == 0
-                           || fcode_tab[my + MAX_MV] > f_code
-                           || fcode_tab[my + MAX_MV] == 0 ){
+                        if(   mx >=range || mx <-range
+                           || my >=range || my <-range){
                             s->mb_type[i] &= ~MB_TYPE_INTER4V;
                             s->mb_type[i] |= MB_TYPE_INTRA;
                         }
@@ -1648,37 +1643,31 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type)
 {
     int y;
-    UINT8 * fcode_tab= s->fcode_tab;
 
     // RAL: 8 in MPEG-1, 16 in MPEG-4
     int range = (((s->codec_id == CODEC_ID_MPEG1VIDEO) ? 8 : 16) << f_code);
+    
+    if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range;
 
     /* clip / convert to intra 16x16 type MVs */
     for(y=0; y<s->mb_height; y++){
         int x;
-        int xy= (y+1)* (s->mb_width+2)+1;
-        int i= y*s->mb_width;
-        for(x=0; x<s->mb_width; x++)
-            {
-            if (s->mb_type[i] & type)    // RAL: "type" test added...
-                {
-                if (fcode_tab[mv_table[xy][0] + MAX_MV] > f_code || fcode_tab[mv_table[xy][0] + MAX_MV] == 0)
-                    {
-                    if(mv_table[xy][0]>0) 
-                        mv_table[xy][0]=  range-1;
-                    else
-                        mv_table[xy][0]= -range;
+        int xy= y*s->mb_stride;
+        for(x=0; x<s->mb_width; x++){
+            if (s->mb_type[xy] & type){    // RAL: "type" test added...
+                if(   mv_table[xy][0] >=range || mv_table[xy][0] <-range
+                   || mv_table[xy][1] >=range || mv_table[xy][1] <-range){
+
+                    if(s->codec_id == CODEC_ID_MPEG1VIDEO && 0){
+                    }else{
+                        if     (mv_table[xy][0] > range-1) mv_table[xy][0]=  range-1;
+                        else if(mv_table[xy][0] < -range ) mv_table[xy][0]= -range;
+                        if     (mv_table[xy][1] > range-1) mv_table[xy][1]=  range-1;
+                        else if(mv_table[xy][1] < -range ) mv_table[xy][1]= -range;
                     }
-                if (fcode_tab[mv_table[xy][1] + MAX_MV] > f_code || fcode_tab[mv_table[xy][1] + MAX_MV] == 0)
-                    {
-                    if(mv_table[xy][1]>0) 
-                        mv_table[xy][1]=  range-1;
-                    else                  
-                        mv_table[xy][1]= -range;
-            }
+                }
             }
             xy++;
-            i++;
         }
     }
 }