diff --git a/ffmpeg.c b/ffmpeg.c index 40369b8374..59e19bf205 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -1369,6 +1369,11 @@ void opt_video_bitrate_min(const char *arg) video_rc_min_rate = atoi(arg) * 1000; } +void opt_video_buffer_size(const char *arg) +{ + video_rc_buffer_size = atoi(arg) * 1000; +} + void opt_video_rc_eq(char *arg) { video_rc_eq = arg; @@ -1900,6 +1905,7 @@ void opt_output_file(const char *filename) video_enc->rc_max_rate = video_rc_max_rate; video_enc->rc_min_rate = video_rc_min_rate; video_enc->rc_buffer_size = video_rc_buffer_size; + video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity; video_enc->i_quant_factor = video_i_qfactor; video_enc->b_quant_factor = video_b_qfactor; video_enc->i_quant_offset = video_i_qoffset; @@ -2260,6 +2266,7 @@ const OptionDef options[] = { { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" }, { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" }, { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" }, + { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" }, { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" }, { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" }, { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method", diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c index 77af3c93c4..252ce7f7f6 100644 --- a/libavcodec/ratecontrol.c +++ b/libavcodec/ratecontrol.c @@ -377,7 +377,7 @@ static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, q*= s->avctx->rc_qmod_amp; bits= qp2bits(rce, q); - +//printf("q:%f\n", q); /* buffer overflow/underflow protection */ if(buffer_size){ double expected_size= rcc->buffer_index - bits; @@ -385,16 +385,18 @@ static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, if(min_rate){ double d= 2*(buffer_size - (expected_size + min_rate))/buffer_size; if(d>1.0) d=1.0; - q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); + else if(d<0.0001) d=0.0001; + q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); } if(max_rate){ double d= 2*expected_size/buffer_size; if(d>1.0) d=1.0; - q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); + else if(d<0.0001) d=0.0001; + q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); } } - +//printf("q:%f max:%f min:%f size:%f index:%d bits:%f agr:%f\n", q,max_rate, min_rate, buffer_size, rcc->buffer_index, bits, s->avctx->rc_buffer_aggressivity); if(s->avctx->rc_qsquish==0.0 || qmin==qmax){ if (qqmax) q=qmax; @@ -460,7 +462,7 @@ int ff_rate_estimate_qscale(MpegEncContext *s) get_qminmax(&qmin, &qmax, s, pict_type); fps= (double)s->frame_rate / FRAME_RATE_BASE; -//printf("input_picture_number:%d picture_number:%d\n", s->input_picture_number, s->picture_number); +//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){ const int last_var= s->last_pict_type == I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum; @@ -544,6 +546,8 @@ int ff_rate_estimate_qscale(MpegEncContext *s) q= short_term_q= rcc->short_term_qsum/rcc->short_term_qcount; //printf("%f ", q); } + assert(q>0.0); + q= modify_qscale(s, rce, q, picture_number); rcc->pass1_wanted_bits+= s->bit_rate/fps; @@ -566,8 +570,8 @@ int ff_rate_estimate_qscale(MpegEncContext *s) qscale= (int)(q + 0.5); //printf("%d ", qscale); -//printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, -// rate_q, short_term_q, s->mc_mb_var, s->frame_bits); +//printf("q:%d diff:%d comp:%f st_q:%f last_size:%d\n", qscale, (int)diff, br_compensation, +// short_term_q, s->frame_bits); //printf("%d %d\n", s->bit_rate, (int)fps); rcc->last_qscale= qscale;