]> git.sesse.net Git - x264/blob - encoder/speed.c
Add speedcontrol file.
[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     struct {
25         int64_t min_buffer, max_buffer;
26         double avg_preset;
27         int den;
28     } stat;
29 };
30
31 void x264_speedcontrol_new( x264_t *h )
32 {
33     x264_speedcontrol_t *sc = h->sc = x264_malloc( sizeof(x264_speedcontrol_t) );
34     x264_emms();
35     memset( sc, 0, sizeof(x264_speedcontrol_t) );
36
37     if( h->param.sc.f_speed <= 0 )
38         h->param.sc.f_speed = 1;
39     sc->fps = h->param.i_fps_num / h->param.i_fps_den;
40     sc->spf = 1e6 / sc->fps;
41     h->param.sc.i_buffer_size = X264_MAX( 3, h->param.sc.i_buffer_size );
42     sc->buffer_size = h->param.sc.i_buffer_size * 1e6 / sc->fps;
43     sc->buffer_fill = sc->buffer_size * h->param.sc.f_buffer_init;
44     sc->buffer_fill = x264_clip3( sc->buffer_fill, sc->spf, sc->buffer_size );
45     sc->compensation_period = sc->buffer_size/4;
46     sc->timestamp = x264_mdate();
47     sc->preset = -1;
48     sc->prev_frame = 0;
49     sc->cplx_num = 3e3; //FIXME estimate initial complexity
50     sc->cplx_den = .1;
51     sc->cplx_decay = 1 - 1./h->param.sc.i_buffer_size;
52     sc->stat.min_buffer = sc->buffer_size;
53     sc->stat.max_buffer = 0;
54     sc->user_param = h->param;
55 }
56
57 void x264_speedcontrol_delete( x264_t *h )
58 {
59     x264_speedcontrol_t *sc = h->sc;
60     if( !sc )
61         return;
62     x264_log( h, X264_LOG_INFO, "speedcontrol: avg preset=%.3f  buffer min=%.3f max=%.3f\n",
63               sc->stat.avg_preset / sc->stat.den,
64               (float)sc->stat.min_buffer / sc->buffer_size,
65               (float)sc->stat.max_buffer / sc->buffer_size );
66 //  x264_log( h, X264_LOG_INFO, "speedcontrol: avg cplx=%.5f\n", sc->cplx_num / sc->cplx_den );
67     x264_free( sc );
68 }
69
70 static int dither( x264_speedcontrol_t *sc, float f )
71 {
72     int i = f;
73     if( f < 0 )
74         i--;
75     sc->dither += f - i;
76     if( sc->dither >= 1. )
77     {
78         sc->dither--;
79         i++;
80     }
81     return i;
82 }
83
84 typedef struct
85 {
86     float time; // relative encoding time, compared to the other presets
87     int subme;
88     int me;
89     int refs;
90     int mix;
91     int trellis;
92     int partitions;
93     int chromame;
94     float psy_rd;
95     float psy_trellis;
96 } sc_preset_t;
97
98 #define PRESETS 13
99 static const sc_preset_t presets[PRESETS] =
100 {
101 #define I4 X264_ANALYSE_I4x4
102 #define I8 X264_ANALYSE_I8x8
103 #define P8 X264_ANALYSE_PSUB16x16
104 #define B8 X264_ANALYSE_BSUB16x16
105 /*0*/    { .time=1.060, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=0, .psy_rd=0 },
106 /*1*/    { .time=1.120, .subme=1, .me=X264_ME_DIA, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=0 },
107 /*2*/    { .time=1.440, .subme=3, .me=X264_ME_HEX, .refs=1, .mix=0, .chromame=0, .trellis=0, .partitions=I8|I4, .psy_rd=0 },
108 /*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 },
109 /*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 },
110 /*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 },
111 /*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 },
112 /*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 },
113 /*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 },
114 /*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 },
115 /*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 },
116 /*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 },
117 /*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 }
118 };
119
120 static void apply_preset( x264_t *h, int preset )
121 {
122     x264_speedcontrol_t *sc = h->sc;
123     preset = x264_clip3( preset, 0, PRESETS-1 );
124     //if( preset != sc->preset )
125     {
126         const sc_preset_t *s = &presets[preset];
127         x264_param_t p = sc->user_param;
128
129         p.i_frame_reference = s->refs;
130         p.analyse.inter = s->partitions;
131         p.analyse.i_subpel_refine = s->subme;
132         p.analyse.i_me_method = s->me;
133         p.analyse.i_trellis = s->trellis;
134         p.analyse.b_mixed_references = s->mix;
135         p.analyse.b_chroma_me = s->chromame;
136         p.analyse.f_psy_rd = s->psy_rd;
137         p.analyse.f_psy_trellis = s->psy_trellis;
138         x264_encoder_reconfig( h, &p );
139         sc->preset = preset;
140         x264_log( h, X264_LOG_DEBUG, "Applying speedcontrol preset %d.\n", preset );
141     }
142 }
143
144 void x264_speedcontrol_frame_end( x264_t *h )
145 {
146     x264_speedcontrol_t *sc = h->sc;
147     if( h->param.sc.b_alt_timer )
148         sc->cpu_time = x264_mdate() - sc->timestamp;
149 }
150
151 void x264_speedcontrol_frame( x264_t *h )
152 {
153     x264_speedcontrol_t *sc = h->sc;
154     int64_t t, delta_t, delta_buffer;
155     int delta_f;
156
157     x264_emms();
158
159     // update buffer state after encoding and outputting the previous frame(s)
160     t = x264_mdate();
161     delta_f = h->i_frame - sc->prev_frame;
162     delta_t = t - sc->timestamp;
163     delta_buffer = delta_f * sc->spf / h->param.sc.f_speed - delta_t;
164     sc->buffer_fill += delta_buffer;
165     sc->prev_frame = h->i_frame;
166     sc->timestamp = t;
167
168     // update the time predictor
169     if( delta_f )
170     {
171         int cpu_time = h->param.sc.b_alt_timer ? sc->cpu_time : delta_t;
172         float decay = powf( sc->cplx_decay, delta_f );
173         sc->cplx_num *= decay;
174         sc->cplx_den *= decay;
175         sc->cplx_num += cpu_time / presets[sc->preset].time;
176         sc->cplx_den += delta_f;
177
178         sc->stat.avg_preset += sc->preset * delta_f;
179         sc->stat.den += delta_f;
180     }
181     sc->stat.min_buffer = X264_MIN( sc->buffer_fill, sc->stat.min_buffer );
182     sc->stat.max_buffer = X264_MAX( sc->buffer_fill, sc->stat.max_buffer );
183
184     if( sc->buffer_fill > sc->buffer_size ) // oops, cpu was idle
185     {
186         // not really an error, but we'll warn for debugging purposes
187         static int64_t idle_t = 0, print_interval = 0;
188         idle_t += sc->buffer_fill - sc->buffer_size;
189         if( t - print_interval > 1e6 )
190         {
191             x264_log( h, X264_LOG_WARNING, "speedcontrol idle (%.6f sec)\n", idle_t/1e6 );
192             print_interval = t;
193             idle_t = 0;
194         }
195         sc->buffer_fill = sc->buffer_size;
196     }
197     else if( sc->buffer_fill < 0 && delta_buffer < 0 ) // oops, we're late
198     {
199         // don't clip fullness to 0; we'll hope the real buffer was bigger than
200         // specified, and maybe we can catch up. if the application had to drop
201         // frames, then it should override the buffer fullness (FIXME implement this).
202         x264_log( h, X264_LOG_WARNING, "speedcontrol underflow (%.6f sec)\n", sc->buffer_fill/1e6 );
203     }
204
205     {
206         // pick the preset that should return the buffer to 3/4-full within a time
207         // specified by compensation_period
208         float target = sc->spf / h->param.sc.f_speed
209                      * (sc->buffer_fill + sc->compensation_period)
210                      / (sc->buffer_size*3/4 + sc->compensation_period);
211         float cplx = sc->cplx_num / sc->cplx_den;
212         float set, t0, t1;
213         float filled = (float) sc->buffer_fill / sc->buffer_size;
214         int i;
215         t0 = presets[0].time * cplx;
216         for( i=1;; i++ )
217         {
218             t1 = presets[i].time * cplx;
219             if( t1 >= target || i == PRESETS-1 )
220                 break;
221             t0 = t1;
222         }
223         // linear interpolation between states
224         set = i-1 + (target - t0) / (t1 - t0);
225         // Even if our time estimations in the PRESETS array are off
226         // this will push us towards our target fullness
227         set += (20 * (filled-0.75));
228         set = x264_clip3f(set,0,PRESETS-1);
229         apply_preset( h, dither( sc, set ) );
230
231         // FIXME
232         if (h->param.i_log_level >= X264_LOG_DEBUG)
233         {
234             static float cpu, wall, tgt, den;
235             float decay = 1-1/100.;
236             cpu = cpu*decay + sc->cpu_time;
237             wall = wall*decay + delta_t;
238             tgt = tgt*decay + target;
239             den = den*decay + 1;
240             fprintf( stderr, "speed: %.2f %d[%.5f] (t/c/w: %6.0f/%6.0f/%6.0f = %.4f) fps=%.2f\r",
241                      set, sc->preset, (float)sc->buffer_fill / sc->buffer_size,
242                      tgt/den, cpu/den, wall/den, cpu/wall, 1e6*den/wall );
243         }
244     }
245
246 }
247
248 void x264_speedcontrol_sync( x264_t *h, float f_buffer_fill, int i_buffer_size )
249 {
250     x264_speedcontrol_t *sc = h->sc;
251     if( !h->param.sc.i_buffer_size )
252         return;
253     if( i_buffer_size )
254         h->param.sc.i_buffer_size = X264_MAX( 3, h->param.sc.i_buffer_size );
255     sc->buffer_size = h->param.sc.i_buffer_size * 1e6 / sc->fps;
256     sc->buffer_fill = sc->buffer_size * f_buffer_fill;
257 }