summaryrefslogtreecommitdiff
path: root/libavcodec/ratecontrol.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/ratecontrol.c')
-rw-r--r--libavcodec/ratecontrol.c40
1 files changed, 30 insertions, 10 deletions
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index 2cb5eeaefe..8f8647c058 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -3,20 +3,20 @@
*
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -49,6 +49,10 @@ void ff_write_pass1_stats(MpegEncContext *s){
s->f_code, s->b_code, s->current_picture.mc_mb_var_sum, s->current_picture.mb_var_sum, s->i_count, s->skip_count, s->header_bits);
}
+static double get_fps(AVCodecContext *avctx){
+ return 1.0 / av_q2d(avctx->time_base) / FFMAX(avctx->ticks_per_frame, 1);
+}
+
static inline double qp2bits(RateControlEntry *rce, double qp){
if(qp<=0.0){
av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
@@ -106,6 +110,13 @@ int ff_rate_control_init(MpegEncContext *s)
};
emms_c();
+ if (!s->avctx->rc_max_available_vbv_use && s->avctx->rc_buffer_size) {
+ if (s->avctx->rc_max_rate) {
+ s->avctx->rc_max_available_vbv_use = av_clipf(s->avctx->rc_max_rate/(s->avctx->rc_buffer_size*get_fps(s->avctx)), 1.0/0.3, 1.0);
+ } else
+ s->avctx->rc_max_available_vbv_use = 1.0;
+ }
+
res = av_expr_parse(&rcc->rc_eq_eval, s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, 0, s->avctx);
if (res < 0) {
av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->avctx->rc_eq);
@@ -125,6 +136,8 @@ int ff_rate_control_init(MpegEncContext *s)
rcc->last_qscale_for[i]=FF_QP2LAMBDA * 5;
}
rcc->buffer_index= s->avctx->rc_initial_buffer_occupancy;
+ if (!rcc->buffer_index)
+ rcc->buffer_index = s->avctx->rc_buffer_size * 3 / 4;
if(s->flags&CODEC_FLAG_PASS2){
int i;
@@ -240,7 +253,7 @@ int ff_rate_control_init(MpegEncContext *s)
rcc->frame_count[rce.pict_type] ++;
get_qscale(s, &rce, rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum, i);
- rcc->pass1_wanted_bits+= s->bit_rate/(1/av_q2d(s->avctx->time_base)); //FIXME misbehaves a little for variable fps
+ rcc->pass1_wanted_bits+= s->bit_rate/get_fps(s->avctx); //FIXME misbehaves a little for variable fps
}
}
@@ -265,7 +278,7 @@ void ff_rate_control_uninit(MpegEncContext *s)
int ff_vbv_update(MpegEncContext *s, int frame_size){
RateControlContext *rcc= &s->rc_context;
- const double fps= 1/av_q2d(s->avctx->time_base);
+ const double fps= get_fps(s->avctx);
const int buffer_size= s->avctx->rc_buffer_size;
const double min_rate= s->avctx->rc_min_rate/fps;
const double max_rate= s->avctx->rc_max_rate/fps;
@@ -435,7 +448,7 @@ static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q,
int qmin, qmax;
const int pict_type= rce->new_pict_type;
const double buffer_size= s->avctx->rc_buffer_size;
- const double fps= 1/av_q2d(s->avctx->time_base);
+ const double fps= get_fps(s->avctx);
const double min_rate= s->avctx->rc_min_rate / fps;
const double max_rate= s->avctx->rc_max_rate / fps;
@@ -668,11 +681,12 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
get_qminmax(&qmin, &qmax, s, pict_type);
- fps= 1/av_q2d(s->avctx->time_base);
+ fps= get_fps(s->avctx);
/* update predictors */
if(picture_number>2 && !dry_run){
const int last_var= s->last_pict_type == AV_PICTURE_TYPE_I ? 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);
+ av_assert1(s->frame_bits >= s->stuffing_bits);
+ update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits - s->stuffing_bits);
}
if(s->flags&CODEC_FLAG_PASS2){
@@ -798,7 +812,7 @@ static int init_pass2(MpegEncContext *s)
RateControlContext *rcc= &s->rc_context;
AVCodecContext *a= s->avctx;
int i, toobig;
- double fps= 1/av_q2d(s->avctx->time_base);
+ double fps= get_fps(s->avctx);
double complexity[5]={0,0,0,0,0}; // aproximate bits at quant=1
uint64_t const_bits[5]={0,0,0,0,0}; // quantizer independent bits
uint64_t all_const_bits;
@@ -849,6 +863,12 @@ static int init_pass2(MpegEncContext *s)
assert(filter_size%2==1);
/* fixed I/B QP relative to P mode */
+ for(i=FFMAX(0, rcc->num_entries-300); i<rcc->num_entries; i++){
+ RateControlEntry *rce= &rcc->entry[i];
+
+ qscale[i]= get_diff_limited_q(s, rce, qscale[i]);
+ }
+
for(i=rcc->num_entries-1; i>=0; i--){
RateControlEntry *rce= &rcc->entry[i];