]> git.sesse.net Git - x264/blob - encoder/speed.c
cd73fb90091a6a8bbffd29cc8263942f4a70913d
[x264] / encoder / speed.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include "common/common.h"
5 #include "common/cpu.h"
6
7 struct x264_speedcontrol_t
8 {
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
15     float fps, spf;
16     int preset;          // which setting was used in the previous frame
17     int prev_frame;
18     float cplx_num;      // rolling average of estimated spf for preset #0
19     float cplx_den;
20     float cplx_decay;
21     float dither;
22     x264_param_t user_param;
23
24     int first;
25
26     struct
27     {
28         int64_t min_buffer, max_buffer;
29         double avg_preset;
30         int den;
31     } stat;
32 };
33
34 void x264_speedcontrol_new( x264_t *h )
35 {
36     x264_speedcontrol_t *sc = h->sc = x264_malloc( sizeof(x264_speedcontrol_t) );
37     x264_emms();
38     memset( sc, 0, sizeof(x264_speedcontrol_t) );
39
40     if( h->param.sc.f_speed <= 0 )
41         h->param.sc.f_speed = 1;
42     sc->fps = h->param.i_fps_num / h->param.i_fps_den;
43     sc->spf = 1e6 / sc->fps;
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 * 1e6 / sc->fps;
46     sc->buffer_fill = sc->buffer_size * h->param.sc.f_buffer_init;
47     sc->buffer_fill = x264_clip3( sc->buffer_fill, sc->spf, sc->buffer_size );
48     sc->compensation_period = sc->buffer_size/4;
49     sc->timestamp = x264_mdate();
50     sc->preset = -1;
51     sc->prev_frame = 0;
52     sc->cplx_num = 3e3; //FIXME estimate initial complexity
53     sc->cplx_den = .1;
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;
57     sc->user_param = h->param;
58     sc->first = 1;
59 }
60
61 void x264_speedcontrol_delete( x264_t *h )
62 {
63     x264_speedcontrol_t *sc = h->sc;
64     if( !sc )
65         return;
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 );
71     x264_free( sc );
72 }
73
74 static int dither( x264_speedcontrol_t *sc, float f )
75 {
76     int i = f;
77     if( f < 0 )
78         i--;
79     sc->dither += f - i;
80     if( sc->dither >= 1. )
81     {
82         sc->dither--;
83         i++;
84     }
85     return i;
86 }
87
88 typedef struct
89 {
90     float time; // relative encoding time, compared to the other presets
91     int subme;
92     int me;
93     int refs;
94     int mix;
95     int trellis;
96     int partitions;
97     int chromame;
98     float psy_rd;
99     float psy_trellis;
100 } sc_preset_t;
101
102 #define PRESETS 13
103 static const sc_preset_t presets[PRESETS] =
104 {
105 #define I4 X264_ANALYSE_I4x4
106 #define I8 X264_ANALYSE_I8x8
107 #define P8 X264_ANALYSE_PSUB16x16
108 #define B8 X264_ANALYSE_BSUB16x16
109 /*0*/    { .time=1.060, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=0, .psy_rd=0 },
110 /*1*/    { .time=1.120, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=0 },
111 /*2*/    { .time=1.440, .subme=3, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=0 },
112 /*3*/    { .time=1.620, .subme=5, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=1.0 },
113 /*4*/    { .time=2.660, .subme=6, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=1.0 },
114 /*5*/    { .time=3.560, .subme=6, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4, .psy_rd=1.0 },
115 /*6*/    { .time=4.640, .subme=6, .me=X264_ME_HEX, .refs=2, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4, .psy_rd=1.0 },
116 /*7*/    { .time=5.190, .subme=7, .me=X264_ME_HEX, .refs=2, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4, .psy_rd=1.0 },
117 /*8*/    { .time=6.190, .subme=7, .me=X264_ME_HEX, .refs=2, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 },
118 /*9*/    { .time=6.920, .subme=7, .me=X264_ME_HEX, .refs=3, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 },
119 /*10*/   { .time=7.070, .subme=8, .me=X264_ME_HEX, .refs=3, .mix=0, .chromame=0, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 },
120 /*11*/   { .time=8.800, .subme=8, .me=X264_ME_UMH, .refs=3, .mix=1, .chromame=1, .trellis=1, .partitions=I8|I4|P8|B8, .psy_rd=1.0 },
121 /*12*/   { .time=18.570, .subme=8, .me=X264_ME_UMH, .refs=3, .mix=1, .chromame=1, .trellis=2, .partitions=I8|I4|P8|B8, .psy_rd=1.0 }
122 };
123
124 static void apply_preset( x264_t *h, int preset )
125 {
126     x264_speedcontrol_t *sc = h->sc;
127     preset = x264_clip3( preset, 0, PRESETS-1 );
128     //if( preset != sc->preset )
129     {
130         const sc_preset_t *s = &presets[preset];
131         x264_param_t p = sc->user_param;
132
133         p.i_frame_reference = s->refs;
134         p.analyse.inter = s->partitions;
135         p.analyse.i_subpel_refine = s->subme;
136         p.analyse.i_me_method = s->me;
137         p.analyse.i_trellis = s->trellis;
138         p.analyse.b_mixed_references = s->mix;
139         p.analyse.b_chroma_me = s->chromame;
140         p.analyse.f_psy_rd = s->psy_rd;
141         p.analyse.f_psy_trellis = s->psy_trellis;
142         x264_encoder_reconfig( h, &p );
143         sc->preset = preset;
144         x264_log( h, X264_LOG_DEBUG, "Applying speedcontrol preset %d.\n", preset );
145     }
146 }
147
148 void x264_speedcontrol_frame_end( x264_t *h )
149 {
150     x264_speedcontrol_t *sc = h->sc;
151     if( h->param.sc.b_alt_timer )
152         sc->cpu_time = x264_mdate() - sc->timestamp;
153 }
154
155 void x264_speedcontrol_frame( x264_t *h )
156 {
157     x264_speedcontrol_t *sc = h->sc;
158     int64_t t, delta_t, delta_buffer;
159     int delta_f;
160
161     x264_emms();
162
163     // update buffer state after encoding and outputting the previous frame(s)
164     if( sc->first )
165     {
166         t = sc->timestamp = x264_mdate();
167         sc->first = 0;
168     }
169     else
170         t = x264_mdate();
171
172     delta_f = h->i_frame - sc->prev_frame;
173     delta_t = t - sc->timestamp;
174     delta_buffer = delta_f * sc->spf / h->param.sc.f_speed - delta_t;
175     sc->buffer_fill += delta_buffer;
176     sc->prev_frame = h->i_frame;
177     sc->timestamp = t;
178
179     // update the time predictor
180     if( delta_f )
181     {
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;
188
189         sc->stat.avg_preset += sc->preset * delta_f;
190         sc->stat.den += delta_f;
191     }
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 );
194
195     if( sc->buffer_fill > sc->buffer_size ) // oops, cpu was idle
196     {
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 )
201         {
202             x264_log( h, X264_LOG_WARNING, "speedcontrol idle (%.6f sec)\n", idle_t/1e6 );
203             print_interval = t;
204             idle_t = 0;
205         }
206         sc->buffer_fill = sc->buffer_size;
207     }
208     else if( sc->buffer_fill < 0 && delta_buffer < 0 ) // oops, we're late
209     {
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 );
214     }
215
216     {
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->spf / 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;
223         float set, t0, t1;
224         float filled = (float) sc->buffer_fill / sc->buffer_size;
225         int i;
226         t0 = presets[0].time * cplx;
227         for( i=1;; i++ )
228         {
229             t1 = presets[i].time * cplx;
230             if( t1 >= target || i == PRESETS-1 )
231                 break;
232             t0 = t1;
233         }
234         // linear interpolation between states
235         set = i-1 + (target - t0) / (t1 - t0);
236         // Even if our time estimations in the 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,PRESETS-1);
240         apply_preset( h, dither( sc, set ) );
241
242         // FIXME
243         if (h->param.i_log_level >= X264_LOG_DEBUG)
244         {
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;
250             den = den*decay + 1;
251             fprintf( stderr, "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 );
254         }
255     }
256
257 }
258
259 void x264_speedcontrol_sync( x264_t *h, float f_buffer_fill, int i_buffer_size )
260 {
261     x264_speedcontrol_t *sc = h->sc;
262     if( !h->param.sc.i_buffer_size )
263         return;
264     if( i_buffer_size )
265         h->param.sc.i_buffer_size = X264_MAX( 3, h->param.sc.i_buffer_size );
266     sc->buffer_size = h->param.sc.i_buffer_size * 1e6 / sc->fps;
267     sc->buffer_fill = sc->buffer_size * f_buffer_fill;
268 }