From 1f9aea9b91c653c6ad95e84b2ff250738e352eec Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Tue, 1 Apr 2003 15:38:01 +0000
Subject: [PATCH] fixing msmpeg4v2 encoding

Originally committed as revision 1723 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavcodec/mpegvideo.c      | 7 +++++--
 libavcodec/msmpeg4.c        | 8 +++-----
 libavcodec/wmv2.c           | 5 +++--
 tests/ffmpeg.regression.ref | 4 ++--
 4 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 867f4232a3d..99ac289f912 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -648,6 +648,7 @@ int MPV_encode_init(AVCodecContext *avctx)
         s->h263_pred = 1;
         s->unrestricted_mv = 1;
         s->msmpeg4_version= 3;
+        s->flipflop_rounding=1;
         avctx->delay=0;
         s->low_delay=1;
         break;
@@ -657,6 +658,7 @@ int MPV_encode_init(AVCodecContext *avctx)
         s->h263_pred = 1;
         s->unrestricted_mv = 1;
         s->msmpeg4_version= 4;
+        s->flipflop_rounding=1;
         avctx->delay=0;
         s->low_delay=1;
         break;
@@ -666,6 +668,7 @@ int MPV_encode_init(AVCodecContext *avctx)
         s->h263_pred = 1;
         s->unrestricted_mv = 1;
         s->msmpeg4_version= 5;
+        s->flipflop_rounding=1;
         avctx->delay=0;
         s->low_delay=1;
         break;
@@ -2834,8 +2837,8 @@ static void encode_picture(MpegEncContext *s, int picture_number)
     s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
     
     if(s->pict_type==I_TYPE){
-        if(s->msmpeg4_version) s->no_rounding=1;
-        else                   s->no_rounding=0;
+        if(s->msmpeg4_version >= 3) s->no_rounding=1;
+        else                        s->no_rounding=0;
     }else if(s->pict_type!=B_TYPE){
         if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
             s->no_rounding ^= 1;          
diff --git a/libavcodec/msmpeg4.c b/libavcodec/msmpeg4.c
index 4bac6d3b74a..115e71375f2 100644
--- a/libavcodec/msmpeg4.c
+++ b/libavcodec/msmpeg4.c
@@ -439,12 +439,10 @@ void msmpeg4_encode_ext_header(MpegEncContext * s)
 
         put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
 
-        if(s->msmpeg4_version<3)
-            s->flipflop_rounding=0;
-        else{
-            s->flipflop_rounding=1;
+        if(s->msmpeg4_version>=3)
             put_bits(&s->pb, 1, s->flipflop_rounding);
-        }
+        else
+            assert(s->flipflop_rounding==0);
 }
 
 #endif //CONFIG_ENCODERS
diff --git a/libavcodec/wmv2.c b/libavcodec/wmv2.c
index 99780f6f41b..2c15694212d 100644
--- a/libavcodec/wmv2.c
+++ b/libavcodec/wmv2.c
@@ -131,7 +131,10 @@ int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number)
     w->abt_type=0;
     w->j_type=0;
 
+    assert(s->flipflop_rounding);
+
     if (s->pict_type == I_TYPE) {
+        assert(s->no_rounding==1);
         if(w->j_type_bit) put_bits(&s->pb, 1, w->j_type);
         
         if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table);
@@ -144,7 +147,6 @@ int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number)
         put_bits(&s->pb, 1, s->dc_table_index);
 
         s->inter_intra_pred= 0;
-        s->no_rounding = 1;
     }else{
         int cbp_index;
 
@@ -181,7 +183,6 @@ int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number)
         put_bits(&s->pb, 1, s->mv_table_index);
     
         s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
-        s->no_rounding ^= 1;
     }
     s->esc3_level_length= 0;
     s->esc3_run_length= 0;
diff --git a/tests/ffmpeg.regression.ref b/tests/ffmpeg.regression.ref
index 4334839ce99..d90486acc0a 100644
--- a/tests/ffmpeg.regression.ref
+++ b/tests/ffmpeg.regression.ref
@@ -1,8 +1,8 @@
 ffmpeg regression test
 dda0ba041aef50a5101884291c06d4d9 *./data/a-mpeg1.mpg
 6713259d72260740bbddaea30631ea18 *./data/out.yuv
-8730846cd73f07689bfab061eac3e800 *./data/a-msmpeg4v2.avi
-2cfc9307a2fd9651cf9c8e42dea1a80e *./data/out.yuv
+8836c5e38c7891f04e8e75836e48551e *./data/a-msmpeg4v2.avi
+712aa6c959d1d90a78fe98657cbff19c *./data/out.yuv
 8b2a7f5e105784d65426989e4fce6e94 *./data/a-msmpeg4.avi
 8786aa956838234fe3e48d0ef8cbd46c *./data/out.yuv
 a0bec32a81f9a4cd174557a49cd26430 *./data/a-wmv1.avi
-- 
GitLab