mpeg1 bframe encoding patch by (Raphaƫl LEGRAND) with some modifications by me
[ffmpeg.git] / libavcodec / motion_est.c
index 89f814b924e7a0bdc96dace4eeaed603e575040f..5814cb33cf2eeb5db76cb8dba41b6ab9df29ac63 100644 (file)
@@ -1493,12 +1493,13 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
     fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
 //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
     {
-        int score= dmin;
-        type=MB_TYPE_DIRECT;
+        int score= fmin;
+        type = MB_TYPE_FORWARD;
         
-        if(fmin<score){
-            score=fmin;
-            type= MB_TYPE_FORWARD; 
+        // RAL: No MB_TYPE_DIRECT in MPEG-1 video (only MPEG-4)
+        if (s->codec_id != CODEC_ID_MPEG1VIDEO && dmin <= score){
+            score = dmin;
+            type = MB_TYPE_DIRECT;
         }
         if(bmin<score){
             score=bmin;
@@ -1643,21 +1644,32 @@ void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, i
     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);
+
     /* 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(   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]=  (16<<f_code)-1;
-                else                  mv_table[xy][0]= -(16<<f_code);
+        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;
+                    }
+                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;
             }
-            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]=  (16<<f_code)-1;
-                else                  mv_table[xy][1]= -(16<<f_code);
             }
             xy++;
             i++;