use correct qp & lambda for ME on the second pass
authorMichael Niedermayer <michaelni@gmx.at>
Mon, 19 Dec 2005 13:41:27 +0000 (13:41 +0000)
committerMichael Niedermayer <michaelni@gmx.at>
Mon, 19 Dec 2005 13:41:27 +0000 (13:41 +0000)
Originally committed as revision 4757 to svn://svn.ffmpeg.org/ffmpeg/trunk

libavcodec/mpegvideo.c
libavcodec/mpegvideo.h
libavcodec/ratecontrol.c

index 393a98f..6a72866 100644 (file)
@@ -5245,6 +5245,31 @@ static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src)
     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 @@ static void encode_picture(MpegEncContext *s, int picture_number)
             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 @@ static void encode_picture(MpegEncContext *s, int picture_number)
         }
     }
 
-    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
index da9f0ba..7dc83d0 100644 (file)
@@ -972,7 +972,7 @@ void ff_mjpeg_stuffing(PutBitContext * pbc);
 
 /* 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,
index 7f87696..8dd9d2b 100644 (file)
@@ -609,7 +609,7 @@ static void adaptive_quantization(MpegEncContext *s, double q){
 }
 //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 @@ float ff_rate_estimate_qscale(MpegEncContext *s)
     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 @@ float ff_rate_estimate_qscale(MpegEncContext *s)
     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;