]> git.sesse.net Git - x264/blob - encoder/speed.c
a449ddd241256f55979b508f57824d344a11d1a8
[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
23     int first;
24
25     struct
26     {
27         int64_t min_buffer, max_buffer;
28         double avg_preset;
29         int den;
30     } stat;
31 };
32
33 void x264_speedcontrol_new( x264_t *h )
34 {
35     x264_speedcontrol_t *sc = h->sc = x264_malloc( sizeof(x264_speedcontrol_t) );
36     x264_emms();
37     memset( sc, 0, sizeof(x264_speedcontrol_t) );
38
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();
49     sc->preset = -1;
50     sc->prev_frame = 0;
51     sc->cplx_num = 3e3; //FIXME estimate initial complexity
52     sc->cplx_den = .1;
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;
56     sc->first = 1;
57 }
58
59 void x264_speedcontrol_delete( x264_t *h )
60 {
61     x264_speedcontrol_t *sc = h->sc;
62     if( !sc )
63         return;
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 );
69     x264_free( sc );
70 }
71
72 static int dither( x264_speedcontrol_t *sc, float f )
73 {
74     int i = f;
75     if( f < 0 )
76         i--;
77     sc->dither += f - i;
78     if( sc->dither >= 1. )
79     {
80         sc->dither--;
81         i++;
82     }
83     return i;
84 }
85
86 typedef struct
87 {
88     float time; // relative encoding time, compared to the other presets
89     int subme;
90     int me;
91     int refs;
92     int mix;
93     int trellis;
94     int partitions;
95     int chromame;
96     float psy_rd;
97     float psy_trellis;
98 } sc_preset_t;
99
100 #define PRESETS 10
101 static const sc_preset_t presets[PRESETS] =
102 {
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 }
120 };
121
122 static void apply_preset( x264_t *h, int preset )
123 {
124     x264_speedcontrol_t *sc = h->sc;
125     preset = x264_clip3( preset, 0, PRESETS-1 );
126     //if( preset != sc->preset )
127     {
128         const sc_preset_t *s = &presets[preset];
129         x264_param_t p = h->param;
130
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 );
141         sc->preset = preset;
142         x264_log( h, X264_LOG_DEBUG, "Applying speedcontrol preset %d.\n", preset );
143     }
144 }
145
146 void x264_speedcontrol_frame_end( x264_t *h )
147 {
148     x264_speedcontrol_t *sc = h->sc;
149     if( h->param.sc.b_alt_timer )
150         sc->cpu_time = x264_mdate() - sc->timestamp;
151 }
152
153 void x264_speedcontrol_frame( x264_t *h )
154 {
155     x264_speedcontrol_t *sc = h->sc;
156     int64_t t, delta_t, delta_buffer;
157     int delta_f;
158
159     x264_emms();
160
161     // update buffer state after encoding and outputting the previous frame(s)
162     if( sc->first )
163     {
164         t = sc->timestamp = x264_mdate();
165         sc->first = 0;
166     }
167     else
168         t = x264_mdate();
169
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;
175     sc->timestamp = t;
176
177     // update the time predictor
178     if( delta_f )
179     {
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;
186
187         sc->stat.avg_preset += sc->preset * delta_f;
188         sc->stat.den += delta_f;
189     }
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 );
192
193     if( sc->buffer_fill > sc->buffer_size ) // oops, cpu was idle
194     {
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 )
199         {
200             x264_log( h, X264_LOG_INFO, "speedcontrol idle (%.6f sec)\n", idle_t/1e6 );
201             print_interval = t;
202             idle_t = 0;
203         }
204         sc->buffer_fill = sc->buffer_size;
205     }
206     else if( sc->buffer_fill < 0 && delta_buffer < 0 ) // oops, we're late
207     {
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 );
212     }
213
214     {
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;
221         float set, t0, t1;
222         float filled = (float) sc->buffer_fill / sc->buffer_size;
223         int i;
224         t0 = presets[0].time * cplx;
225         for( i=1;; i++ )
226         {
227             t1 = presets[i].time * cplx;
228             if( t1 >= target || i == PRESETS-1 )
229                 break;
230             t0 = t1;
231         }
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 ) );
239
240         // FIXME
241         if (h->param.i_log_level >= X264_LOG_DEBUG)
242         {
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;
248             den = den*decay + 1;
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 );
252         }
253     }
254
255 }
256
257 void x264_speedcontrol_sync( x264_t *h, float f_buffer_fill, int i_buffer_size )
258 {
259     x264_speedcontrol_t *sc = h->sc;
260     if( !h->param.sc.i_buffer_size )
261         return;
262     if( 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;
266 }