From f151fcdf20e95b3f0af6d8a1e8e4f4a7d95aeba0 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Mon, 21 Feb 2011 02:57:27 +0000 Subject: [PATCH] Merge speedcontrol. --- Makefile | 1 + common/common.c | 16 ++++++++++++++++ common/common.h | 2 ++ encoder/encoder.c | 14 ++++++++++++++ encoder/ratecontrol.h | 7 +++++++ x264.c | 10 ++++++++++ x264.h | 13 +++++++++++++ 7 files changed, 63 insertions(+) diff --git a/Makefile b/Makefile index d0b16338..ffae9834 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ SRCS = common/mc.c common/predict.c common/pixel.c common/macroblock.c \ common/mvpred.c common/bitstream.c \ encoder/analyse.c encoder/me.c encoder/ratecontrol.c \ encoder/set.c encoder/macroblock.c encoder/cabac.c \ + encoder/speed.c \ encoder/cavlc.c encoder/encoder.c encoder/lookahead.c SRCCLI = x264.c input/input.c input/timecode.c input/raw.c input/y4m.c \ diff --git a/common/common.c b/common/common.c index 607aefdd..645b1d31 100644 --- a/common/common.c +++ b/common/common.c @@ -122,6 +122,11 @@ void x264_param_default( x264_param_t *param ) param->rc.i_zones = 0; param->rc.b_mb_tree = 1; + // speedcontrol + param->sc.f_speed = 0; + param->sc.i_buffer_size = 30; + param->sc.f_buffer_init = 0.75; + /* Log */ param->pf_log = x264_log_default; param->p_log_private = NULL; @@ -1016,6 +1021,14 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) p->rc.f_complexity_blur = atof(value); OPT("zones") p->rc.psz_zones = strdup(value); + OPT("speed") + p->sc.f_speed = atof(value); + OPT("speed-bufsize") + p->sc.i_buffer_size = atoi(value); + OPT("speed-init") + p->sc.f_buffer_init = atof(value); + OPT("speed-alt-timer") + p->sc.b_alt_timer = atobool(value); OPT("crop-rect") b_error |= sscanf( value, "%u,%u,%u,%u", &p->crop_rect.i_left, &p->crop_rect.i_top, &p->crop_rect.i_right, &p->crop_rect.i_bottom ) != 4; @@ -1333,6 +1346,9 @@ char *x264_param2string( x264_param_t *p, int b_res ) if( p->b_opencl ) s += sprintf( s, "opencl=%d ", p->b_opencl ); + + // FIXME speedcontrol stuff + s += sprintf( s, "cabac=%d", p->b_cabac ); s += sprintf( s, " ref=%d", p->i_frame_reference ); s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter, diff --git a/common/common.h b/common/common.h index 3a74c9e7..9cb045b6 100644 --- a/common/common.h +++ b/common/common.h @@ -447,6 +447,7 @@ typedef struct x264_lookahead_t } x264_lookahead_t; typedef struct x264_ratecontrol_t x264_ratecontrol_t; +typedef struct x264_speedcontrol_t x264_speedcontrol_t; typedef struct x264_left_table_t { @@ -891,6 +892,7 @@ struct x264_t /* rate control encoding only */ x264_ratecontrol_t *rc; + x264_speedcontrol_t *sc; /* stats */ struct diff --git a/encoder/encoder.c b/encoder/encoder.c index 66f83575..a5bd0d9f 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -1249,6 +1249,8 @@ static int x264_validate_parameters( x264_t *h, int b_open ) h->param.rc.f_qblur = 0; if( h->param.rc.f_complexity_blur < 0 ) h->param.rc.f_complexity_blur = 0; + if( h->param.sc.i_buffer_size < 0 || h->param.sc.f_speed <= 0 ) + h->param.sc.i_buffer_size = 0; h->param.i_sps_id &= 31; @@ -1550,6 +1552,10 @@ x264_t *x264_encoder_open( x264_param_t *param ) mbcmp_init( h ); chroma_dsp_init( h ); + if( h->param.sc.i_buffer_size ) + x264_speedcontrol_new( h ); + + p = buf + sprintf( buf, "using cpu capabilities:" ); for( int i = 0; x264_cpu_names[i].flags; i++ ) { @@ -3685,6 +3691,10 @@ int x264_encoder_encode( x264_t *h, overhead += h->out.nal[h->out.i_nal-1].i_payload + h->out.nal[h->out.i_nal-1].i_padding + SEI_OVERHEAD; } + /* Init the speed control */ + if( h->param.sc.i_buffer_size ) + x264_speedcontrol_frame( h ); + /* Init the rate control */ /* FIXME: Include slice header bit cost. */ x264_ratecontrol_start( h, h->fenc->i_qpplus1, overhead*8 ); @@ -3875,6 +3885,9 @@ static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current, x264_noise_reduction_update( h ); + if( h->param.sc.i_buffer_size ) + x264_speedcontrol_frame_end( h ); + /* ---------------------- Compute/Print statistics --------------------- */ x264_thread_sync_stat( h, h->thread[0] ); @@ -4327,6 +4340,7 @@ void x264_encoder_close ( x264_t *h ) /* rc */ x264_ratecontrol_delete( h ); + x264_speedcontrol_delete( h ); /* param */ if( h->param.rc.psz_stat_out ) diff --git a/encoder/ratecontrol.h b/encoder/ratecontrol.h index 490a1edb..ca10bf31 100644 --- a/encoder/ratecontrol.h +++ b/encoder/ratecontrol.h @@ -64,5 +64,12 @@ int x264_weighted_reference_duplicate( x264_t *h, int i_ref, const x264_weight_t void x264_threads_distribute_ratecontrol( x264_t *h ); void x264_threads_merge_ratecontrol( x264_t *h ); void x264_hrd_fullness( x264_t *h ); + +// speedcontrol +void x264_speedcontrol_new( x264_t *h ); +void x264_speedcontrol_delete( x264_t *h ); +void x264_speedcontrol_frame( x264_t *h ); +void x264_speedcontrol_frame_end( x264_t *h ); + #endif diff --git a/x264.c b/x264.c index b9520719..414252c9 100644 --- a/x264.c +++ b/x264.c @@ -761,6 +761,14 @@ static void help( x264_param_t *defaults, int longhelp ) " K= depending on open-gop setting\n" " QPs are restricted by qpmin/qpmax.\n" ); H1( "\n" ); + + H1( "Speedcontrol:\n" ); + H1( "\n" ); + H1( " --speed Automatically adjust other options to achieve this\n" ); + H1( " fraction of realtime.\n" ); + H1( " --speed-bufsize Averaging period for speed. (in frames) [%d]\n", defaults->sc.i_buffer_size ); + H1( "\n" ); + H1( "Analysis:\n" ); H1( "\n" ); H1( " -A, --partitions Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n" @@ -1079,6 +1087,8 @@ static struct option long_options[] = { "cplxblur", required_argument, NULL, 0 }, { "zones", required_argument, NULL, 0 }, { "qpfile", required_argument, NULL, OPT_QPFILE }, + { "speed", required_argument, NULL, 0 }, + { "speed-bufsize", required_argument, NULL, 0 }, { "threads", required_argument, NULL, 0 }, { "lookahead-threads", required_argument, NULL, 0 }, { "sliced-threads", no_argument, NULL, 0 }, diff --git a/x264.h b/x264.h index 5581ab9b..fd5c0f7a 100644 --- a/x264.h +++ b/x264.h @@ -456,6 +456,15 @@ typedef struct x264_param_t /* frame packing arrangement flag */ int i_frame_packing; + /* Speed control parameters */ + struct + { + 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 */ + } sc; + /* Muxing parameters */ int b_aud; /* generate access unit delimiters */ int b_repeat_headers; /* put SPS/PPS before each keyframe */ @@ -952,6 +961,10 @@ void x264_encoder_intra_refresh( x264_t * ); * Returns 0 on success, negative on failure. */ 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 ); + #ifdef __cplusplus } #endif -- 2.39.2