1 #include "x264_speed_control.h"
11 #include <type_traits>
17 using namespace std::chrono;
21 X264SpeedControl::X264SpeedControl(x264_t *x264, float f_speed, int i_buffer_size, float f_buffer_init)
22 : dyn(load_x264_for_bit_depth(global_flags.x264_bit_depth)),
23 x264(x264), f_speed(f_speed)
26 dyn.x264_encoder_parameters(x264, ¶m);
28 float fps = (float)param.i_fps_num / param.i_fps_den;
30 set_buffer_size(i_buffer_size);
31 buffer_fill = buffer_size * f_buffer_init;
32 buffer_fill = max<int64_t>(buffer_fill, uspf);
33 buffer_fill = min(buffer_fill, buffer_size);
34 timestamp = steady_clock::now();
36 cplx_num = 3e3; //FIXME estimate initial complexity
38 stat.min_buffer = buffer_size;
40 stat.avg_preset = 0.0;
43 metric_x264_speedcontrol_buffer_available_seconds = buffer_fill * 1e-6;
44 metric_x264_speedcontrol_buffer_size_seconds = buffer_size * 1e-6;
45 metric_x264_speedcontrol_preset_used_frames.init_uniform(SC_PRESETS);
46 global_metrics.add("x264_speedcontrol_preset_used_frames", &metric_x264_speedcontrol_preset_used_frames);
47 global_metrics.add("x264_speedcontrol_buffer_available_seconds", &metric_x264_speedcontrol_buffer_available_seconds, Metrics::TYPE_GAUGE);
48 global_metrics.add("x264_speedcontrol_buffer_size_seconds", &metric_x264_speedcontrol_buffer_size_seconds, Metrics::TYPE_GAUGE);
49 global_metrics.add("x264_speedcontrol_idle_frames", &metric_x264_speedcontrol_idle_frames);
50 global_metrics.add("x264_speedcontrol_late_frames", &metric_x264_speedcontrol_late_frames);
53 X264SpeedControl::~X264SpeedControl()
55 fprintf(stderr, "speedcontrol: avg preset=%.3f buffer min=%.3f max=%.3f\n",
56 stat.avg_preset / stat.den,
57 (float)stat.min_buffer / buffer_size,
58 (float)stat.max_buffer / buffer_size );
59 // x264_log( x264, X264_LOG_INFO, "speedcontrol: avg cplx=%.5f\n", cplx_num / cplx_den );
67 float time; // relative encoding time, compared to the other presets
80 // The actual presets, including the equivalent commandline options. Note that
81 // all presets are benchmarked with --weightp 1 --mbtree --rc-lookahead 20
82 // on top of the given settings (equivalent settings to the "faster" preset).
83 // Timings and SSIM measurements were done on a quadcore Haswell i5 3.2 GHz
84 // on the first 1000 frames of "Elephants Dream" in 1080p.
85 // See experiments/measure-x264.pl for a way to reproduce.
87 // Note that the two first and the two last are also used for extrapolation
88 // should the desired time be outside the range. Thus, it is disadvantageous if
89 // they are chosen so that the timings are too close to each other.
90 static const sc_preset_t presets[SC_PRESETS] = {
91 #define I4 X264_ANALYSE_I4x4
92 #define I8 X264_ANALYSE_I8x8
93 #define P4 X264_ANALYSE_PSUB8x8
94 #define P8 X264_ANALYSE_PSUB16x16
95 #define B8 X264_ANALYSE_BSUB16x16
96 // Preset 0: 16.583db, --preset superfast --b-adapt 0 --bframes 0
97 { .time= 1.000, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .trellis=0, .partitions=I8|I4, .badapt=0, .bframes=0, .direct=0, .merange=16 },
99 // Preset 1: 17.386db, --preset superfast
100 { .time= 1.288, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .trellis=0, .partitions=I8|I4, .badapt=1, .bframes=3, .direct=1, .merange=16 },
102 // Preset 2: 17.919db, --preset superfast --subme 2
103 { .time= 2.231, .subme=2, .me=X264_ME_DIA, .refs=1, .mix=0, .trellis=0, .partitions=I8|I4, .badapt=1, .bframes=3, .direct=1, .merange=16 },
105 // Preset 3: 18.051db, --preset veryfast
106 { .time= 2.403, .subme=2, .me=X264_ME_HEX, .refs=1, .mix=0, .trellis=0, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
108 // Preset 4: 18.422db, --preset veryfast --subme 3
109 { .time= 2.636, .subme=3, .me=X264_ME_HEX, .refs=1, .mix=0, .trellis=0, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
111 // Preset 5: 18.514db, --preset veryfast --subme 3 --ref 2
112 { .time= 2.844, .subme=3, .me=X264_ME_HEX, .refs=2, .mix=0, .trellis=0, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
114 // Preset 6: 18.564db, --preset veryfast --subme 4 --ref 2
115 { .time= 3.366, .subme=4, .me=X264_ME_HEX, .refs=2, .mix=0, .trellis=0, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
117 // Preset 7: 18.411db, --preset faster
118 { .time= 3.450, .subme=4, .me=X264_ME_HEX, .refs=2, .mix=0, .trellis=1, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
120 // Preset 8: 18.429db, --preset faster --mixed-refs
121 { .time= 3.701, .subme=4, .me=X264_ME_HEX, .refs=2, .mix=1, .trellis=1, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
123 // Preset 9: 18.454db, --preset faster --mixed-refs --subme 5
124 { .time= 4.297, .subme=5, .me=X264_ME_HEX, .refs=2, .mix=1, .trellis=1, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
126 // Preset 10: 18.528db, --preset fast
127 { .time= 5.181, .subme=6, .me=X264_ME_HEX, .refs=2, .mix=1, .trellis=1, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
129 // Preset 11: 18.762db, --preset fast --subme 7
130 { .time= 5.357, .subme=7, .me=X264_ME_HEX, .refs=2, .mix=1, .trellis=1, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
132 // Preset 12: 18.819db, --preset medium
133 { .time= 6.040, .subme=7, .me=X264_ME_HEX, .refs=3, .mix=1, .trellis=1, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
135 // Preset 13: 18.889db, --preset medium --subme 8
136 { .time= 7.408, .subme=8, .me=X264_ME_HEX, .refs=3, .mix=1, .trellis=1, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
138 // Preset 14: 19.127db, --preset medium --subme 8 --trellis 2
139 { .time=10.124, .subme=8, .me=X264_ME_HEX, .refs=3, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=1, .merange=16 },
141 // Preset 15: 19.118db, --preset medium --subme 8 --trellis 2 --direct auto
142 { .time=10.144, .subme=8, .me=X264_ME_HEX, .refs=3, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=3, .merange=16 },
144 // Preset 16: 19.172db, --preset slow
145 { .time=11.142, .subme=8, .me=X264_ME_HEX, .refs=5, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8, .badapt=1, .bframes=3, .direct=3, .merange=16 },
147 // Preset 17: 19.309db, --preset slow --b-adapt 2 --subme 9
148 { .time=11.168, .subme=9, .me=X264_ME_HEX, .refs=5, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8, .badapt=2, .bframes=3, .direct=3, .merange=16 },
150 // Preset 18: 19.316db, --preset slow --b-adapt 2 --subme 9 --me umh
151 { .time=12.942, .subme=9, .me=X264_ME_UMH, .refs=5, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8, .badapt=2, .bframes=3, .direct=3, .merange=16 },
153 // Preset 19: 19.342db, --preset slow --b-adapt 2 --subme 9 --me umh --ref 6
154 { .time=14.302, .subme=9, .me=X264_ME_UMH, .refs=6, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8, .badapt=2, .bframes=3, .direct=3, .merange=16 },
156 // Preset 20: 19.365db, --preset slow --b-adapt 2 --subme 9 --me umh --ref 7
157 { .time=15.554, .subme=9, .me=X264_ME_UMH, .refs=7, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8, .badapt=2, .bframes=3, .direct=3, .merange=16 },
159 // Preset 21: 19.396db, --preset slower
160 { .time=17.551, .subme=9, .me=X264_ME_UMH, .refs=8, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8|P4, .badapt=2, .bframes=3, .direct=3, .merange=16 },
162 // Preset 22: 19.491db, --preset slower --subme 10
163 { .time=21.321, .subme=10, .me=X264_ME_UMH, .refs=8, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8|P4, .badapt=2, .bframes=3, .direct=3, .merange=16 },
165 // Preset 23: 19.764db, --preset slower --subme 10 --bframes 8
166 { .time=23.200, .subme=10, .me=X264_ME_UMH, .refs=8, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8|P4, .badapt=2, .bframes=8, .direct=3, .merange=16 },
168 // Preset 24: 19.807db, --preset veryslow
169 { .time=36.922, .subme=10, .me=X264_ME_UMH, .refs=16, .mix=1, .trellis=2, .partitions=I8|I4|P8|B8|P4, .badapt=2, .bframes=8, .direct=3, .merange=24 },
177 void X264SpeedControl::before_frame(float new_buffer_fill, int new_buffer_size, float new_uspf)
179 if (new_uspf > 0.0) {
182 if (new_buffer_size) {
183 set_buffer_size(new_buffer_size);
185 buffer_fill = buffer_size * new_buffer_fill;
186 metric_x264_speedcontrol_buffer_available_seconds = buffer_fill * 1e-6;
188 steady_clock::time_point t;
190 // update buffer state after encoding and outputting the previous frame(s)
192 t = timestamp = steady_clock::now();
195 t = steady_clock::now();
198 auto delta_t = t - timestamp;
201 // update the time predictor
203 int cpu_time = duration_cast<microseconds>(cpu_time_last_frame).count();
204 cplx_num *= cplx_decay;
205 cplx_den *= cplx_decay;
206 cplx_num += cpu_time / presets[preset].time;
209 stat.avg_preset += preset;
213 stat.min_buffer = min(buffer_fill, stat.min_buffer);
214 stat.max_buffer = max(buffer_fill, stat.max_buffer);
216 if (buffer_fill >= buffer_size) { // oops, cpu was idle
217 // not really an error, but we'll warn for debugging purposes
218 static int64_t idle_t = 0;
219 static steady_clock::time_point print_interval;
220 static bool first = false;
221 idle_t += buffer_fill - buffer_size;
222 if (first || duration<double>(t - print_interval).count() > 0.1) {
223 //fprintf(stderr, "speedcontrol idle (%.6f sec)\n", idle_t/1e6);
228 buffer_fill = buffer_size;
229 metric_x264_speedcontrol_buffer_available_seconds = buffer_fill * 1e-6;
230 ++metric_x264_speedcontrol_idle_frames;
231 } else if (buffer_fill <= 0) { // oops, we're late
232 // fprintf(stderr, "speedcontrol underflow (%.6f sec)\n", buffer_fill/1e6);
233 ++metric_x264_speedcontrol_late_frames;
237 // Pick the preset that should return the buffer to 3/4-full within a time
238 // specified by compensation_period.
240 // NOTE: This doesn't actually do that, at least assuming the same target is
241 // chosen for every frame; exactly what it does is unclear to me. It seems
242 // to consistently undershoot a bit, so it needs to be saved by the second
243 // predictor below. However, fixing the formula seems to yield somewhat less
244 // stable results in practice; in particular, once the buffer is half-full
245 // or so, it would give us a negative target. Perhaps increasing
246 // compensation_period would be a good idea, but initial (very brief) tests
247 // did not yield good results.
248 float target = uspf / f_speed
249 * (buffer_fill + compensation_period)
250 / (buffer_size*3/4 + compensation_period);
251 float cplx = cplx_num / cplx_den;
253 float filled = (float) buffer_fill / buffer_size;
255 t0 = presets[0].time * cplx;
257 t1 = presets[i].time * cplx;
258 if (t1 >= target || i == SC_PRESETS - 1)
262 // exponential interpolation between states
263 set = i-1 + (log(target) - log(t0)) / (log(t1) - log(t0));
264 set = max<float>(set, -5);
265 set = min<float>(set, (SC_PRESETS-1) + 5);
266 // Even if our time estimations in the SC_PRESETS array are off
267 // this will push us towards our target fullness
269 set += (40 * (filled-0.75));
270 float s2 = (40 * (filled-0.75));
271 set = min<float>(max<float>(set, 0), SC_PRESETS - 1);
272 apply_preset(dither_preset(set));
274 if (global_flags.x264_speedcontrol_verbose) {
275 static float cpu, wall, tgt, den;
276 const float decay = 1-1/100.;
277 cpu = cpu*decay + duration_cast<microseconds>(cpu_time_last_frame).count();
278 wall = wall*decay + duration_cast<microseconds>(delta_t).count();
279 tgt = tgt*decay + target;
281 fprintf(stderr, "speed: %.2f+%.2f %d[%.5f] (t/c/w: %6.0f/%6.0f/%6.0f = %.4f) fps=%.2f\r",
282 s1, s2, preset, (float)buffer_fill / buffer_size,
283 tgt/den, cpu/den, wall/den, cpu/wall, 1e6*den/wall );
289 void X264SpeedControl::after_frame()
291 cpu_time_last_frame = steady_clock::now() - timestamp;
294 void X264SpeedControl::set_buffer_size(int new_buffer_size)
296 new_buffer_size = max(3, new_buffer_size);
297 buffer_size = new_buffer_size * uspf;
298 cplx_decay = 1 - 1./new_buffer_size;
299 compensation_period = buffer_size/4;
300 metric_x264_speedcontrol_buffer_size_seconds = buffer_size * 1e-6;
303 int X264SpeedControl::dither_preset(float f)
317 void X264SpeedControl::apply_preset(int new_preset)
319 new_preset = max(new_preset, 0);
320 new_preset = min(new_preset, SC_PRESETS - 1);
322 const sc_preset_t *s = &presets[new_preset];
324 dyn.x264_encoder_parameters(x264, &p);
326 p.i_frame_reference = s->refs;
327 p.i_bframe_adaptive = s->badapt;
328 p.i_bframe = s->bframes;
329 p.analyse.inter = s->partitions;
330 p.analyse.i_subpel_refine = s->subme;
331 p.analyse.i_me_method = s->me;
332 p.analyse.i_trellis = s->trellis;
333 p.analyse.b_mixed_references = s->mix;
334 p.analyse.i_direct_mv_pred = s->direct;
335 p.analyse.i_me_range = s->merange;
339 dyn.x264_encoder_reconfig(x264, &p);
342 metric_x264_speedcontrol_preset_used_frames.count_event(new_preset);