4 #include "common/common.h"
5 #include "common/cpu.h"
7 struct x264_speedcontrol_t
9 // all times are in usec
10 int64_t timestamp; // when was speedcontrol last invoked
11 int64_t cpu_time; // time spent encoding the previous frame
12 int64_t buffer_size; // assumed application-side buffer of frames to be streamed (measured in microseconds),
13 int64_t buffer_fill; // where full = we don't have to hurry
14 int64_t compensation_period; // how quickly we try to return to the target buffer fullness
15 float uspf; // microseconds per frame
16 int preset; // which setting was used in the previous frame
18 float cplx_num; // rolling average of estimated spf for preset #0
28 int64_t min_buffer, max_buffer;
34 void x264_speedcontrol_new( x264_t *h )
36 x264_speedcontrol_t *sc = h->sc = x264_malloc( sizeof(x264_speedcontrol_t) );
38 memset( sc, 0, sizeof(x264_speedcontrol_t) );
40 if( h->param.sc.f_speed <= 0 )
41 h->param.sc.f_speed = 1;
42 float fps = h->param.i_fps_num / h->param.i_fps_den;
44 h->param.sc.i_buffer_size = X264_MAX( 3, h->param.sc.i_buffer_size );
45 sc->buffer_size = h->param.sc.i_buffer_size * sc->uspf;
46 sc->buffer_fill = sc->buffer_size * h->param.sc.f_buffer_init;
47 sc->buffer_fill = x264_clip3( sc->buffer_fill, sc->uspf, sc->buffer_size );
48 sc->compensation_period = sc->buffer_size/4;
49 sc->timestamp = x264_mdate();
52 sc->cplx_num = 3e3; //FIXME estimate initial complexity
54 sc->cplx_decay = 1 - 1./h->param.sc.i_buffer_size;
55 sc->stat.min_buffer = sc->buffer_size;
56 sc->stat.max_buffer = 0;
58 sc->buffer_complete = 0;
61 void x264_speedcontrol_delete( x264_t *h )
63 x264_speedcontrol_t *sc = h->sc;
66 x264_log( h, X264_LOG_INFO, "speedcontrol: avg preset=%.3f buffer min=%.3f max=%.3f\n",
67 sc->stat.avg_preset / sc->stat.den,
68 (float)sc->stat.min_buffer / sc->buffer_size,
69 (float)sc->stat.max_buffer / sc->buffer_size );
70 // x264_log( h, X264_LOG_INFO, "speedcontrol: avg cplx=%.5f\n", sc->cplx_num / sc->cplx_den );
74 static int dither( x264_speedcontrol_t *sc, float f )
80 if( sc->dither >= 1. )
90 float time; // relative encoding time, compared to the other presets
102 static const sc_preset_t presets[SC_PRESETS] =
104 #define I4 X264_ANALYSE_I4x4
105 #define I8 X264_ANALYSE_I8x8
106 #define P8 X264_ANALYSE_PSUB16x16
107 #define B8 X264_ANALYSE_BSUB16x16
108 /*0*/ { .time=1.000, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=0, .psy_rd=0 },
109 /*1*/ { .time=1.009, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=0 },
110 /*2*/ { .time=1.843, .subme=3, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=0 },
111 /*3*/ { .time=1.984, .subme=5, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=1.0 },
112 /*4*/ { .time=2.289, .subme=6, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=1.0 },
113 /*5*/ { .time=3.113, .subme=6, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4, .psy_rd=1.0 },
114 /*6*/ { .time=3.400, .subme=6, .me=X264_ME_HEX, .refs=2, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4, .psy_rd=1.0 },
115 /*7*/ { .time=3.755, .subme=7, .me=X264_ME_HEX, .refs=2, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4, .psy_rd=1.0 },
116 /*8*/ { .time=4.592, .subme=7, .me=X264_ME_HEX, .refs=2, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 },
117 /*9*/ { .time=4.730, .subme=7, .me=X264_ME_HEX, .refs=3, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 },
118 /*10*/ { .time=5.453, .subme=8, .me=X264_ME_HEX, .refs=3, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 },
119 /*11*/ { .time=8.277, .subme=8, .me=X264_ME_UMH, .refs=3, .mix=1, .chromame=1, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 },
120 /*12*/ { .time=8.410, .subme=8, .me=X264_ME_UMH, .refs=4, .mix=1, .chromame=1, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 }
123 static void apply_preset( x264_t *h, int preset )
125 x264_speedcontrol_t *sc = h->sc;
126 preset = x264_clip3( preset, 0, h->param.sc.max_preset-1 );
127 //if( preset != sc->preset )
129 const sc_preset_t *s = &presets[preset];
130 x264_param_t p = h->param;
132 p.i_frame_reference = s->refs;
133 p.analyse.inter = s->partitions;
134 p.analyse.i_subpel_refine = s->subme;
135 p.analyse.i_me_method = s->me;
136 p.analyse.i_trellis = s->trellis;
137 p.analyse.b_mixed_references = s->mix;
138 p.analyse.b_chroma_me = s->chromame;
139 p.analyse.f_psy_rd = s->psy_rd;
140 p.analyse.f_psy_trellis = s->psy_trellis;
141 x264_encoder_reconfig( h, &p );
143 x264_log( h, X264_LOG_DEBUG, "Applying speedcontrol preset %d.\n", preset );
147 void x264_speedcontrol_frame_end( x264_t *h )
149 x264_speedcontrol_t *sc = h->sc;
150 if( h->param.sc.b_alt_timer )
151 sc->cpu_time = x264_mdate() - sc->timestamp;
154 void x264_speedcontrol_frame( x264_t *h )
156 x264_speedcontrol_t *sc = h->sc;
157 int64_t t, delta_t, delta_buffer;
162 // update buffer state after encoding and outputting the previous frame(s)
165 t = sc->timestamp = x264_mdate();
171 delta_f = h->i_frame - sc->prev_frame;
172 delta_t = t - sc->timestamp;
173 delta_buffer = delta_f * sc->uspf / h->param.sc.f_speed - delta_t;
174 if( !sc->buffer_complete )
175 sc->buffer_fill += delta_buffer;
176 sc->prev_frame = h->i_frame;
179 // update the time predictor
182 int cpu_time = h->param.sc.b_alt_timer ? sc->cpu_time : delta_t;
183 float decay = powf( sc->cplx_decay, delta_f );
184 sc->cplx_num *= decay;
185 sc->cplx_den *= decay;
186 sc->cplx_num += cpu_time / presets[sc->preset].time;
187 sc->cplx_den += delta_f;
189 sc->stat.avg_preset += sc->preset * delta_f;
190 sc->stat.den += delta_f;
192 sc->stat.min_buffer = X264_MIN( sc->buffer_fill, sc->stat.min_buffer );
193 sc->stat.max_buffer = X264_MAX( sc->buffer_fill, sc->stat.max_buffer );
195 if( sc->buffer_fill > sc->buffer_size ) // oops, cpu was idle
197 // not really an error, but we'll warn for debugging purposes
198 static int64_t idle_t = 0, print_interval = 0;
199 idle_t += sc->buffer_fill - sc->buffer_size;
200 if( t - print_interval > 1e6 )
202 x264_log( h, X264_LOG_DEBUG, "speedcontrol idle (%.6f sec)\n", idle_t/1e6 );
206 sc->buffer_fill = sc->buffer_size;
208 else if( sc->buffer_fill < 0 && delta_buffer < 0 ) // oops, we're late
210 // don't clip fullness to 0; we'll hope the real buffer was bigger than
211 // specified, and maybe we can catch up. if the application had to drop
212 // frames, then it should override the buffer fullness (FIXME implement this).
213 x264_log( h, X264_LOG_WARNING, "speedcontrol underflow (%.6f sec)\n", sc->buffer_fill/1e6 );
217 // pick the preset that should return the buffer to 3/4-full within a time
218 // specified by compensation_period
219 float target = sc->uspf / h->param.sc.f_speed
220 * (sc->buffer_fill + sc->compensation_period)
221 / (sc->buffer_size*3/4 + sc->compensation_period);
222 float cplx = sc->cplx_num / sc->cplx_den;
224 float filled = (float) sc->buffer_fill / sc->buffer_size;
226 t0 = presets[0].time * cplx;
229 t1 = presets[i].time * cplx;
230 if( t1 >= target || i == h->param.sc.max_preset-1 )
234 // linear interpolation between states
235 set = i-1 + (target - t0) / (t1 - t0);
236 // Even if our time estimations in the SC_PRESETS array are off
237 // this will push us towards our target fullness
238 set += (20 * (filled-0.75));
239 set = x264_clip3f( set, 0 , h->param.sc.max_preset-1 );
240 apply_preset( h, dither( sc, set ) );
243 if (h->param.i_log_level >= X264_LOG_DEBUG)
245 static float cpu, wall, tgt, den;
246 float decay = 1-1/100.;
247 cpu = cpu*decay + sc->cpu_time;
248 wall = wall*decay + delta_t;
249 tgt = tgt*decay + target;
251 x264_log( h, X264_LOG_DEBUG, "speed: %.2f %d[%.5f] (t/c/w: %6.0f/%6.0f/%6.0f = %.4f) fps=%.2f\r",
252 set, sc->preset, (float)sc->buffer_fill / sc->buffer_size,
253 tgt/den, cpu/den, wall/den, cpu/wall, 1e6*den/wall );
259 void x264_speedcontrol_sync( x264_t *h, float f_buffer_fill, int i_buffer_size, int buffer_complete )
261 x264_speedcontrol_t *sc = h->sc;
262 if( !h->param.sc.i_buffer_size )
265 h->param.sc.i_buffer_size = X264_MAX( 3, i_buffer_size );
266 sc->buffer_size = h->param.sc.i_buffer_size * sc->uspf;
267 sc->cplx_decay = 1 - 1./h->param.sc.i_buffer_size;
268 sc->compensation_period = sc->buffer_size/4;
269 sc->buffer_fill = sc->buffer_size * f_buffer_fill;
270 sc->buffer_complete = !!buffer_complete;