changeset 2974:e7189a8c22ed libavcodec

use correct qp & lambda for ME on the second pass
author michael
date Mon, 19 Dec 2005 13:41:27 +0000
parents bb0222145083
children 18e7ecf83aa2
files mpegvideo.c mpegvideo.h ratecontrol.c
diffstat 3 files changed, 38 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/mpegvideo.c	Mon Dec 19 03:08:08 2005 +0000
+++ b/mpegvideo.c	Mon Dec 19 13:41:27 2005 +0000
@@ -5245,6 +5245,31 @@
     flush_put_bits(&dst->pb);
 }
 
+static void estimate_qp(MpegEncContext *s, int dry_run){
+    if (!s->fixed_qscale)
+        s->current_picture_ptr->quality=
+        s->current_picture.quality = ff_rate_estimate_qscale(s, dry_run);
+
+    if(s->adaptive_quant){
+        switch(s->codec_id){
+        case CODEC_ID_MPEG4:
+            ff_clean_mpeg4_qscales(s);
+            break;
+        case CODEC_ID_H263:
+        case CODEC_ID_H263P:
+        case CODEC_ID_FLV1:
+            ff_clean_h263_qscales(s);
+            break;
+        }
+
+        s->lambda= s->lambda_table[0];
+        //FIXME broken
+    }else
+        s->lambda= s->current_picture.quality;
+//printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
+    update_qscale(s);
+}
+
 static void encode_picture(MpegEncContext *s, int picture_number)
 {
     int i;
@@ -5273,6 +5298,10 @@
             s->no_rounding ^= 1;
     }
 
+    if(s->flags & CODEC_FLAG_PASS2)
+        estimate_qp(s, 1);
+
+
     s->mb_intra=0; //for the rate distortion & bit compare functions
     for(i=1; i<s->avctx->thread_count; i++){
         ff_update_duplicate_context(s->thread_context[i], s);
@@ -5369,28 +5398,7 @@
         }
     }
 
-    if (!s->fixed_qscale)
-        s->current_picture_ptr->quality=
-        s->current_picture.quality = ff_rate_estimate_qscale(s);
-
-    if(s->adaptive_quant){
-        switch(s->codec_id){
-        case CODEC_ID_MPEG4:
-            ff_clean_mpeg4_qscales(s);
-            break;
-        case CODEC_ID_H263:
-        case CODEC_ID_H263P:
-        case CODEC_ID_FLV1:
-            ff_clean_h263_qscales(s);
-            break;
-        }
-
-        s->lambda= s->lambda_table[0];
-        //FIXME broken
-    }else
-        s->lambda= s->current_picture.quality;
-//printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
-    update_qscale(s);
+    estimate_qp(s, 0);
 
     if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
         s->qscale= 3; //reduce clipping problems
--- a/mpegvideo.h	Mon Dec 19 03:08:08 2005 +0000
+++ b/mpegvideo.h	Mon Dec 19 13:41:27 2005 +0000
@@ -972,7 +972,7 @@
 
 /* rate control */
 int ff_rate_control_init(MpegEncContext *s);
-float ff_rate_estimate_qscale(MpegEncContext *s);
+float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run);
 void ff_write_pass1_stats(MpegEncContext *s);
 void ff_rate_control_uninit(MpegEncContext *s);
 double ff_eval(char *s, double *const_value, const char **const_name,
--- a/ratecontrol.c	Mon Dec 19 03:08:08 2005 +0000
+++ b/ratecontrol.c	Mon Dec 19 13:41:27 2005 +0000
@@ -609,7 +609,7 @@
 }
 //FIXME rd or at least approx for dquant
 
-float ff_rate_estimate_qscale(MpegEncContext *s)
+float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
 {
     float q;
     int qmin, qmax;
@@ -634,7 +634,7 @@
     fps= 1/av_q2d(s->avctx->time_base);
 //printf("input_pic_num:%d pic_num:%d frame_rate:%d\n", s->input_picture_number, s->picture_number, s->frame_rate);
         /* update predictors */
-    if(picture_number>2){
+    if(picture_number>2 && !dry_run){
         const int last_var= s->last_pict_type == I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum;
         update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits);
     }
@@ -735,9 +735,11 @@
     else
         q= (int)(q + 0.5);
 
-    rcc->last_qscale= q;
-    rcc->last_mc_mb_var_sum= pic->mc_mb_var_sum;
-    rcc->last_mb_var_sum= pic->mb_var_sum;
+    if(!dry_run){
+        rcc->last_qscale= q;
+        rcc->last_mc_mb_var_sum= pic->mc_mb_var_sum;
+        rcc->last_mb_var_sum= pic->mb_var_sum;
+    }
 #if 0
 {
     static int mvsum=0, texsum=0;