Commit 8ed9f9ab authored by Michael Niedermayer's avatar Michael Niedermayer

use correct qp & lambda for ME on the second pass

Originally committed as revision 4757 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 2ceff404
...@@ -5245,6 +5245,31 @@ static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src) ...@@ -5245,6 +5245,31 @@ static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src)
flush_put_bits(&dst->pb); 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) static void encode_picture(MpegEncContext *s, int picture_number)
{ {
int i; int i;
...@@ -5273,6 +5298,10 @@ static void encode_picture(MpegEncContext *s, int picture_number) ...@@ -5273,6 +5298,10 @@ static void encode_picture(MpegEncContext *s, int picture_number)
s->no_rounding ^= 1; 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 s->mb_intra=0; //for the rate distortion & bit compare functions
for(i=1; i<s->avctx->thread_count; i++){ for(i=1; i<s->avctx->thread_count; i++){
ff_update_duplicate_context(s->thread_context[i], s); ff_update_duplicate_context(s->thread_context[i], s);
...@@ -5369,28 +5398,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) ...@@ -5369,28 +5398,7 @@ static void encode_picture(MpegEncContext *s, int picture_number)
} }
} }
if (!s->fixed_qscale) estimate_qp(s, 0);
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);
if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE)) if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
s->qscale= 3; //reduce clipping problems s->qscale= 3; //reduce clipping problems
......
...@@ -972,7 +972,7 @@ void ff_mjpeg_stuffing(PutBitContext * pbc); ...@@ -972,7 +972,7 @@ void ff_mjpeg_stuffing(PutBitContext * pbc);
/* rate control */ /* rate control */
int ff_rate_control_init(MpegEncContext *s); 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_write_pass1_stats(MpegEncContext *s);
void ff_rate_control_uninit(MpegEncContext *s); void ff_rate_control_uninit(MpegEncContext *s);
double ff_eval(char *s, double *const_value, const char **const_name, double ff_eval(char *s, double *const_value, const char **const_name,
......
...@@ -609,7 +609,7 @@ static void adaptive_quantization(MpegEncContext *s, double q){ ...@@ -609,7 +609,7 @@ static void adaptive_quantization(MpegEncContext *s, double q){
} }
//FIXME rd or at least approx for dquant //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; float q;
int qmin, qmax; int qmin, qmax;
...@@ -634,7 +634,7 @@ float ff_rate_estimate_qscale(MpegEncContext *s) ...@@ -634,7 +634,7 @@ float ff_rate_estimate_qscale(MpegEncContext *s)
fps= 1/av_q2d(s->avctx->time_base); 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); //printf("input_pic_num:%d pic_num:%d frame_rate:%d\n", s->input_picture_number, s->picture_number, s->frame_rate);
/* update predictors */ /* 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; 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); 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) ...@@ -735,9 +735,11 @@ float ff_rate_estimate_qscale(MpegEncContext *s)
else else
q= (int)(q + 0.5); q= (int)(q + 0.5);
rcc->last_qscale= q; if(!dry_run){
rcc->last_mc_mb_var_sum= pic->mc_mb_var_sum; rcc->last_qscale= q;
rcc->last_mb_var_sum= pic->mb_var_sum; rcc->last_mc_mb_var_sum= pic->mc_mb_var_sum;
rcc->last_mb_var_sum= pic->mb_var_sum;
}
#if 0 #if 0
{ {
static int mvsum=0, texsum=0; static int mvsum=0, texsum=0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment