// all times are in usec
int64_t timestamp; // when was speedcontrol last invoked
int64_t cpu_time; // time spent encoding the previous frame
- int64_t buffer_size; // assumed application-side buffer of frames to be streamed,
+ int64_t buffer_size; // assumed application-side buffer of frames to be streamed (measured in microseconds),
int64_t buffer_fill; // where full = we don't have to hurry
int64_t compensation_period; // how quickly we try to return to the target buffer fullness
- float fps, spf;
+ float uspf; // microseconds per frame
int preset; // which setting was used in the previous frame
int prev_frame;
float cplx_num; // rolling average of estimated spf for preset #0
if( h->param.sc.f_speed <= 0 )
h->param.sc.f_speed = 1;
- sc->fps = h->param.i_fps_num / h->param.i_fps_den;
- sc->spf = 1e6 / sc->fps;
+ float fps = h->param.i_fps_num / h->param.i_fps_den;
+ sc->uspf = 1e6 / fps;
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_size = h->param.sc.i_buffer_size * sc->uspf;
sc->buffer_fill = sc->buffer_size * h->param.sc.f_buffer_init;
- sc->buffer_fill = x264_clip3( sc->buffer_fill, sc->spf, sc->buffer_size );
+ sc->buffer_fill = x264_clip3( sc->buffer_fill, sc->uspf, sc->buffer_size );
sc->compensation_period = sc->buffer_size/4;
sc->timestamp = x264_mdate();
sc->preset = -1;
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;
+ delta_buffer = delta_f * sc->uspf / h->param.sc.f_speed - delta_t;
if( !sc->buffer_complete )
sc->buffer_fill += delta_buffer;
sc->prev_frame = h->i_frame;
{
// pick the preset that should return the buffer to 3/4-full within a time
// specified by compensation_period
- float target = sc->spf / h->param.sc.f_speed
+ float target = sc->uspf / h->param.sc.f_speed
* (sc->buffer_fill + sc->compensation_period)
/ (sc->buffer_size*3/4 + sc->compensation_period);
float cplx = sc->cplx_num / sc->cplx_den;
if( !h->param.sc.i_buffer_size )
return;
if( 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;
+ h->param.sc.i_buffer_size = X264_MAX( 3, i_buffer_size );
+ sc->buffer_size = h->param.sc.i_buffer_size * sc->uspf;
+ sc->cplx_decay = 1 - 1./h->param.sc.i_buffer_size;
+ sc->compensation_period = sc->buffer_size/4;
sc->buffer_fill = sc->buffer_size * f_buffer_fill;
sc->buffer_complete = !!buffer_complete;
}