From 35a05f459dbdf46a7918aefcdd5166d32b104261 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 30 Mar 2012 16:50:13 +0100 Subject: [PATCH] Add ability to signal to x264 when speedcontrol buffering is complete --- common/common.h | 1 + encoder/encoder.c | 2 ++ encoder/speed.c | 19 +++++++++++-------- x264.h | 5 +++-- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/common/common.h b/common/common.h index 9cb045b6..8e3123ca 100644 --- a/common/common.h +++ b/common/common.h @@ -111,6 +111,7 @@ do {\ #define FILLER_OVERHEAD (NALU_OVERHEAD+1) #define SEI_OVERHEAD (NALU_OVERHEAD - (h->param.b_annexb && !h->param.i_avcintra_class && (h->out.i_nal-1))) +#define SC_PRESETS 13 /**************************************************************************** * Includes ****************************************************************************/ diff --git a/encoder/encoder.c b/encoder/encoder.c index a5bd0d9f..4692d3ca 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -1272,6 +1272,8 @@ static int x264_validate_parameters( x264_t *h, int b_open ) if( h->param.i_nal_hrd == X264_NAL_HRD_CBR ) h->param.rc.b_filler = 1; + h->param.sc.max_preset = x264_clip3( h->param.sc.max_preset, 1, SC_PRESETS ); + /* ensure the booleans are 0 or 1 so they can be used in math */ #define BOOLIFY(x) h->param.x = !!h->param.x BOOLIFY( b_cabac ); diff --git a/encoder/speed.c b/encoder/speed.c index 5a4b3703..faade8e6 100644 --- a/encoder/speed.c +++ b/encoder/speed.c @@ -21,6 +21,7 @@ struct x264_speedcontrol_t float dither; int first; + int buffer_complete; struct { @@ -54,6 +55,7 @@ void x264_speedcontrol_new( x264_t *h ) sc->stat.min_buffer = sc->buffer_size; sc->stat.max_buffer = 0; sc->first = 1; + sc->buffer_complete = 0; } void x264_speedcontrol_delete( x264_t *h ) @@ -97,8 +99,7 @@ typedef struct float psy_trellis; } sc_preset_t; -#define PRESETS 10 -static const sc_preset_t presets[PRESETS] = +static const sc_preset_t presets[SC_PRESETS] = { #define I4 X264_ANALYSE_I4x4 #define I8 X264_ANALYSE_I8x8 @@ -122,7 +123,7 @@ static const sc_preset_t presets[PRESETS] = static void apply_preset( x264_t *h, int preset ) { x264_speedcontrol_t *sc = h->sc; - preset = x264_clip3( preset, 0, PRESETS-1 ); + preset = x264_clip3( preset, 0, h->param.sc.max_preset-1 ); //if( preset != sc->preset ) { const sc_preset_t *s = &presets[preset]; @@ -170,7 +171,8 @@ void x264_speedcontrol_frame( x264_t *h ) delta_f = h->i_frame - sc->prev_frame; delta_t = t - sc->timestamp; delta_buffer = delta_f * sc->spf / h->param.sc.f_speed - delta_t; - sc->buffer_fill += delta_buffer; + if( !sc->buffer_complete ) + sc->buffer_fill += delta_buffer; sc->prev_frame = h->i_frame; sc->timestamp = t; @@ -225,16 +227,16 @@ void x264_speedcontrol_frame( x264_t *h ) for( i=1;; i++ ) { t1 = presets[i].time * cplx; - if( t1 >= target || i == PRESETS-1 ) + if( t1 >= target || i == h->param.sc.max_preset-1 ) break; t0 = t1; } // linear interpolation between states set = i-1 + (target - t0) / (t1 - t0); - // Even if our time estimations in the PRESETS array are off + // Even if our time estimations in the SC_PRESETS array are off // this will push us towards our target fullness set += (20 * (filled-0.75)); - set = x264_clip3f(set,0,PRESETS-1); + set = x264_clip3f( set, 0 , h->param.sc.max_preset-1 ); apply_preset( h, dither( sc, set ) ); // FIXME @@ -254,7 +256,7 @@ void x264_speedcontrol_frame( x264_t *h ) } -void x264_speedcontrol_sync( x264_t *h, float f_buffer_fill, int i_buffer_size ) +void x264_speedcontrol_sync( x264_t *h, float f_buffer_fill, int i_buffer_size, int buffer_complete ) { x264_speedcontrol_t *sc = h->sc; if( !h->param.sc.i_buffer_size ) @@ -263,4 +265,5 @@ void x264_speedcontrol_sync( x264_t *h, float f_buffer_fill, int i_buffer_size ) h->param.sc.i_buffer_size = X264_MAX( 3, h->param.sc.i_buffer_size ); sc->buffer_size = h->param.sc.i_buffer_size * 1e6 / sc->fps; sc->buffer_fill = sc->buffer_size * f_buffer_fill; + sc->buffer_complete = !!buffer_complete; } diff --git a/x264.h b/x264.h index fd5c0f7a..ccff7c36 100644 --- a/x264.h +++ b/x264.h @@ -462,7 +462,8 @@ typedef struct x264_param_t float f_speed; /* ratio from realtime */ int i_buffer_size; /* number of frames */ float f_buffer_init; /* fraction of size */ - int b_alt_timer; /* use a different method of measuring encode time FIXME */ + int b_alt_timer; /* use a different method of measuring encode time */ + int max_preset; /* maximum speedcontrol preset to use */ } sc; /* Muxing parameters */ @@ -963,7 +964,7 @@ int x264_encoder_invalidate_reference( x264_t *, int64_t pts ); /* x264_speedcontrol_sync: * override speedcontrol's internal clock */ -void x264_speedcontrol_sync( x264_t *, float f_buffer_fill, int i_buffer_size ); +void x264_speedcontrol_sync( x264_t *, float f_buffer_fill, int i_buffer_size, int buffer_complete ); #ifdef __cplusplus } -- 2.39.2