From 82b019cefd98be3cd778728ee718d40044b9e15d Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Thu, 22 Dec 2005 22:48:09 +0000
Subject: [PATCH] better f_code guessing in 2nd pass

Originally committed as revision 4766 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavcodec/mpegvideo.c   |  5 +++--
 libavcodec/mpegvideo.h   |  2 +-
 libavcodec/ratecontrol.c | 11 +++++++++++
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 729cfdd89d..c9adcaa5b7 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -5298,9 +5298,10 @@ static void encode_picture(MpegEncContext *s, int picture_number)
             s->no_rounding ^= 1;
     }
 
-    if(s->flags & CODEC_FLAG_PASS2)
+    if(s->flags & CODEC_FLAG_PASS2){
         estimate_qp(s, 1);
-    else if(!(s->flags & CODEC_FLAG_QSCALE)){
+        ff_get_2pass_fcode(s);
+    }else if(!(s->flags & CODEC_FLAG_QSCALE)){
         RateControlContext *rcc= &s->rc_context;
 
         if(s->pict_type==B_TYPE)
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index b01e6e8029..ce8e6e2939 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -980,6 +980,6 @@ double ff_eval(char *s, double *const_value, const char **const_name,
                double (**func2)(void *, double, double), char **func2_name,
                void *opaque);
 int ff_vbv_update(MpegEncContext *s, int frame_size);
-
+void ff_get_2pass_fcode(MpegEncContext *s);
 
 #endif /* AVCODEC_MPEGVIDEO_H */
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index 8dd9d2b6d8..07e84235bd 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -607,6 +607,17 @@ static void adaptive_quantization(MpegEncContext *s, double q){
         s->lambda_table[mb_xy]= intq;
     }
 }
+
+void ff_get_2pass_fcode(MpegEncContext *s){
+    RateControlContext *rcc= &s->rc_context;
+    int picture_number= s->picture_number;
+    RateControlEntry *rce;
+
+    rce= &rcc->entry[picture_number];
+    s->f_code= rce->f_code;
+    s->b_code= rce->b_code;
+}
+
 //FIXME rd or at least approx for dquant
 
 float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)