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,
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
16 int preset; // which setting was used in the previous frame
18 float cplx_num; // rolling average of estimated spf for preset #0
27 int64_t min_buffer, max_buffer;
33 void x264_speedcontrol_new( x264_t *h )
35 x264_speedcontrol_t *sc = h->sc = x264_malloc( sizeof(x264_speedcontrol_t) );
37 memset( sc, 0, sizeof(x264_speedcontrol_t) );
39 if( h->param.sc.f_speed <= 0 )
40 h->param.sc.f_speed = 1;
41 sc->fps = h->param.i_fps_num / h->param.i_fps_den;
42 sc->spf = 1e6 / sc->fps;
43 h->param.sc.i_buffer_size = X264_MAX( 3, h->param.sc.i_buffer_size );
44 sc->buffer_size = h->param.sc.i_buffer_size * 1e6 / sc->fps;
45 sc->buffer_fill = sc->buffer_size * h->param.sc.f_buffer_init;
46 sc->buffer_fill = x264_clip3( sc->buffer_fill, sc->spf, sc->buffer_size );
47 sc->compensation_period = sc->buffer_size/4;
48 sc->timestamp = x264_mdate();
51 sc->cplx_num = 3e3; //FIXME estimate initial complexity
53 sc->cplx_decay = 1 - 1./h->param.sc.i_buffer_size;
54 sc->stat.min_buffer = sc->buffer_size;
55 sc->stat.max_buffer = 0;
59 void x264_speedcontrol_delete( x264_t *h )
61 x264_speedcontrol_t *sc = h->sc;
64 x264_log( h, X264_LOG_INFO, "speedcontrol: avg preset=%.3f buffer min=%.3f max=%.3f\n",
65 sc->stat.avg_preset / sc->stat.den,
66 (float)sc->stat.min_buffer / sc->buffer_size,
67 (float)sc->stat.max_buffer / sc->buffer_size );
68 // x264_log( h, X264_LOG_INFO, "speedcontrol: avg cplx=%.5f\n", sc->cplx_num / sc->cplx_den );
72 static int dither( x264_speedcontrol_t *sc, float f )
78 if( sc->dither >= 1. )
88 float time; // relative encoding time, compared to the other presets
101 static const sc_preset_t presets[PRESETS] =
103 #define I4 X264_ANALYSE_I4x4
104 #define I8 X264_ANALYSE_I8x8
105 #define P8 X264_ANALYSE_PSUB16x16
106 #define B8 X264_ANALYSE_BSUB16x16
107 /*0*/ { .time=1.000, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=0, .psy_rd=0 },
108 /*1*/ { .time=1.009, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=0 },
109 /*2*/ { .time=1.843, .subme=3, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=0 },
110 /*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 },
111 /*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 },
112 /*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 },
113 /*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 },
114 /*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 },
115 /*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 },
116 /*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 },
117 /*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 },
118 /*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 },
119 /*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 }
122 static void apply_preset( x264_t *h, int preset )
124 x264_speedcontrol_t *sc = h->sc;
125 preset = x264_clip3( preset, 0, PRESETS-1 );
126 //if( preset != sc->preset )
128 const sc_preset_t *s = &presets[preset];
129 x264_param_t p = h->param;
131 p.i_frame_reference = s->refs;
132 p.analyse.inter = s->partitions;
133 p.analyse.i_subpel_refine = s->subme;
134 p.analyse.i_me_method = s->me;
135 p.analyse.i_trellis = s->trellis;
136 p.analyse.b_mixed_references = s->mix;
137 p.analyse.b_chroma_me = s->chromame;
138 p.analyse.f_psy_rd = s->psy_rd;
139 p.analyse.f_psy_trellis = s->psy_trellis;
140 x264_encoder_reconfig( h, &p );
142 x264_log( h, X264_LOG_DEBUG, "Applying speedcontrol preset %d.\n", preset );
146 void x264_speedcontrol_frame_end( x264_t *h )
148 x264_speedcontrol_t *sc = h->sc;
149 if( h->param.sc.b_alt_timer )
150 sc->cpu_time = x264_mdate() - sc->timestamp;
153 void x264_speedcontrol_frame( x264_t *h )
155 x264_speedcontrol_t *sc = h->sc;
156 int64_t t, delta_t, delta_buffer;
161 // update buffer state after encoding and outputting the previous frame(s)
164 t = sc->timestamp = x264_mdate();
170 delta_f = h->i_frame - sc->prev_frame;
171 delta_t = t - sc->timestamp;
172 delta_buffer = delta_f * sc->spf / h->param.sc.f_speed - delta_t;
173 sc->buffer_fill += delta_buffer;
174 sc->prev_frame = h->i_frame;
177 // update the time predictor
180 int cpu_time = h->param.sc.b_alt_timer ? sc->cpu_time : delta_t;
181 float decay = powf( sc->cplx_decay, delta_f );
182 sc->cplx_num *= decay;
183 sc->cplx_den *= decay;
184 sc->cplx_num += cpu_time / presets[sc->preset].time;
185 sc->cplx_den += delta_f;
187 sc->stat.avg_preset += sc->preset * delta_f;
188 sc->stat.den += delta_f;
190 sc->stat.min_buffer = X264_MIN( sc->buffer_fill, sc->stat.min_buffer );
191 sc->stat.max_buffer = X264_MAX( sc->buffer_fill, sc->stat.max_buffer );
193 if( sc->buffer_fill > sc->buffer_size ) // oops, cpu was idle
195 // not really an error, but we'll warn for debugging purposes
196 static int64_t idle_t = 0, print_interval = 0;
197 idle_t += sc->buffer_fill - sc->buffer_size;
198 if( t - print_interval > 1e6 )
200 x264_log( h, X264_LOG_DEBUG, "speedcontrol idle (%.6f sec)\n", idle_t/1e6 );
204 sc->buffer_fill = sc->buffer_size;
206 else if( sc->buffer_fill < 0 && delta_buffer < 0 ) // oops, we're late
208 // don't clip fullness to 0; we'll hope the real buffer was bigger than
209 // specified, and maybe we can catch up. if the application had to drop
210 // frames, then it should override the buffer fullness (FIXME implement this).
211 x264_log( h, X264_LOG_WARNING, "speedcontrol underflow (%.6f sec)\n", sc->buffer_fill/1e6 );
215 // pick the preset that should return the buffer to 3/4-full within a time
216 // specified by compensation_period
217 float target = sc->spf / h->param.sc.f_speed
218 * (sc->buffer_fill + sc->compensation_period)
219 / (sc->buffer_size*3/4 + sc->compensation_period);
220 float cplx = sc->cplx_num / sc->cplx_den;
222 float filled = (float) sc->buffer_fill / sc->buffer_size;
224 t0 = presets[0].time * cplx;
227 t1 = presets[i].time * cplx;
228 if( t1 >= target || i == PRESETS-1 )
232 // linear interpolation between states
233 set = i-1 + (target - t0) / (t1 - t0);
234 // Even if our time estimations in the PRESETS array are off
235 // this will push us towards our target fullness
236 set += (20 * (filled-0.75));
237 set = x264_clip3f(set,0,PRESETS-1);
238 apply_preset( h, dither( sc, set ) );
241 if (h->param.i_log_level >= X264_LOG_DEBUG)
243 static float cpu, wall, tgt, den;
244 float decay = 1-1/100.;
245 cpu = cpu*decay + sc->cpu_time;
246 wall = wall*decay + delta_t;
247 tgt = tgt*decay + target;
249 x264_log( h, X264_LOG_DEBUG, "speed: %.2f %d[%.5f] (t/c/w: %6.0f/%6.0f/%6.0f = %.4f) fps=%.2f\r",
250 set, sc->preset, (float)sc->buffer_fill / sc->buffer_size,
251 tgt/den, cpu/den, wall/den, cpu/wall, 1e6*den/wall );
257 void x264_speedcontrol_sync( x264_t *h, float f_buffer_fill, int i_buffer_size )
259 x264_speedcontrol_t *sc = h->sc;
260 if( !h->param.sc.i_buffer_size )
263 h->param.sc.i_buffer_size = X264_MAX( 3, h->param.sc.i_buffer_size );
264 sc->buffer_size = h->param.sc.i_buffer_size * 1e6 / sc->fps;
265 sc->buffer_fill = sc->buffer_size * f_buffer_fill;