]> git.sesse.net Git - x264/blob - encoder/ratecontrol.c
Fixes by Loren Merritt (lorenm at u.washington.edu).
[x264] / encoder / ratecontrol.c
1 /***************************************************-*- coding: iso-8859-1 -*-
2  * ratecontrol.c: h264 encoder library (Rate Control)
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: ratecontrol.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
6  *
7  * Authors: Måns Rullgård <mru@mru.ath.cx>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #define _ISOC99_SOURCE
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <math.h>
30 #include <limits.h>
31
32 #include "../core/common.h"
33 #include "../core/cpu.h"
34 #include "ratecontrol.h"
35
36 #define DEBUG_RC 0
37
38 struct x264_ratecontrol_t
39 {
40     /* constants */
41     float fps;
42     int gop_size;
43     int bitrate;
44     int nmb;                    /* number of MBs */
45     int buffer_size;
46     int rcbufrate;
47     int init_qp;
48
49     int gop_qp;
50     int buffer_fullness;
51     int frames;                 /* frames in current gop */
52     int pframes;
53     int slice_type;
54     int mb;                     /* MBs processed in current frame */
55     int bits_gop;               /* allocated bits current gop */
56     int bits_last_gop;          /* bits consumed in gop */
57     int qp;                     /* qp for current frame */
58     int qpm;                    /* qp for next MB */
59     int qpa;                    /* average qp for last frame */
60     int qps;
61     int qp_avg_p;               /* average QP for P frames */
62     int qp_last_p;
63     int fbits;                  /* bits allocated for current frame */
64     int ufbits;                 /* bits used for current frame */
65     int nzcoeffs;               /* # of 0-quantized coefficients */
66     int ncoeffs;                /* total # of coefficients */
67     int overhead;
68 };
69
70 int x264_ratecontrol_new( x264_t *h )
71 {
72     x264_ratecontrol_t *rc = x264_malloc( sizeof( x264_ratecontrol_t ) );
73     float bpp;
74
75     memset(rc, 0, sizeof(*rc));
76
77     rc->fps = h->param.f_fps > 0.1 ? h->param.f_fps : 25.0f;
78     rc->gop_size = h->param.i_iframe;
79     rc->bitrate = h->param.i_bitrate * 1000;
80     rc->nmb = ((h->param.i_width + 15) / 16) * ((h->param.i_height + 15) / 16);
81
82     rc->qp = h->param.i_qp_constant;
83     rc->qpa = rc->qp;
84     rc->qpm = rc->qp;
85
86     rc->buffer_size = h->param.i_rc_buffer_size * 1000;
87     if(rc->buffer_size <= 0)
88         rc->buffer_size = rc->bitrate / 2;
89     rc->buffer_fullness = h->param.i_rc_init_buffer;
90     rc->rcbufrate = rc->bitrate / rc->fps;
91
92     bpp = rc->bitrate / (rc->fps * h->param.i_width * h->param.i_height);
93     if(bpp <= 0.6)
94         rc->init_qp = 31;
95     else if(bpp <= 1.4)
96         rc->init_qp = 25;
97     else if(bpp <= 2.4)
98         rc->init_qp = 20;
99     else
100         rc->init_qp = 10;
101     rc->gop_qp = rc->init_qp;
102
103     rc->bits_last_gop = 0;
104
105 #if DEBUG_RC
106     fprintf(stderr, "%f fps, %i bps, bufsize %i\n",
107          rc->fps, rc->bitrate, rc->buffer_size);
108 #endif
109
110     h->rc = rc;
111
112     return 0;
113 }
114
115 void x264_ratecontrol_delete( x264_t *h )
116 {
117     x264_ratecontrol_t *rc = h->rc;
118     x264_free( rc );
119 }
120
121 void x264_ratecontrol_start( x264_t *h, int i_slice_type )
122 {
123     x264_ratecontrol_t *rc = h->rc;
124     int gframes, iframes, pframes, bframes;
125     int minbits, maxbits;
126     int gbits, fbits;
127     int zn = 0;
128     float kp;
129     int gbuf;
130
131     if(!h->param.b_cbr)
132         return;
133
134     x264_cpu_restore( h->param.cpu );
135
136     rc->slice_type = i_slice_type;
137
138     switch(i_slice_type){
139     case SLICE_TYPE_I:
140         gbuf = rc->buffer_fullness + (rc->gop_size-1) * rc->rcbufrate;
141         rc->bits_gop = gbuf - rc->buffer_size / 2;
142
143         if(!rc->mb && rc->pframes){
144             int qp = (float) rc->qp_avg_p / rc->pframes + 0.5;
145 #if 0 /* JM does this without explaining why */
146             int gdq = (float) rc->gop_size / 15 + 0.5;
147             if(gdq > 2)
148                 gdq = 2;
149             qp -= gdq;
150             if(qp > rc->qp_last_p - 2)
151                 qp--;
152 #endif
153             qp = x264_clip3(qp, rc->gop_qp - 4, rc->gop_qp + 4);
154             qp =
155                 x264_clip3(qp, h->param.i_qp_min, h->param.i_qp_max);
156             rc->gop_qp = qp;
157         } else if(rc->frames > 4){
158             rc->gop_qp = rc->init_qp;
159         }
160
161         kp = h->param.f_ip_factor * h->param.f_pb_factor;
162
163 #if DEBUG_RC
164         fprintf(stderr, "gbuf=%i bits_gop=%i frames=%i gop_qp=%i\n",
165                 gbuf, rc->bits_gop, rc->frames, rc->gop_qp);
166 #endif
167
168         rc->bits_last_gop = 0;
169         rc->frames = 0;
170         rc->pframes = 0;
171         rc->qp_avg_p = 0;
172         break;
173
174     case SLICE_TYPE_P:
175         kp = h->param.f_pb_factor;
176         break;
177
178     case SLICE_TYPE_B:
179         kp = 1.0;
180         break;
181
182     default:
183         fprintf(stderr, "x264: ratecontrol: unknown slice type %i\n",
184                 i_slice_type);
185         kp = 1.0;
186         break;
187     }
188
189     gframes = rc->gop_size - rc->frames;
190     iframes = gframes / rc->gop_size;
191     pframes = gframes / (h->param.i_bframe + 1) - iframes;
192     bframes = gframes - pframes - iframes;
193
194     gbits = rc->bits_gop - rc->bits_last_gop;
195     fbits = kp * gbits /
196         (h->param.f_ip_factor * h->param.f_pb_factor * iframes +
197          h->param.f_pb_factor * pframes + bframes);
198
199     minbits = rc->buffer_fullness + rc->rcbufrate - rc->buffer_size;
200     if(minbits < 0)
201         minbits = 0;
202     maxbits = rc->buffer_fullness;
203     rc->fbits = x264_clip3(fbits, minbits, maxbits);
204
205     if(i_slice_type == SLICE_TYPE_I){
206         rc->qp = rc->gop_qp;
207     } else if(rc->ncoeffs && rc->ufbits){
208         int dqp, nonzc;
209
210         nonzc = (rc->ncoeffs - rc->nzcoeffs);
211         if(nonzc == 0)
212             zn = rc->ncoeffs;
213         else if(rc->fbits < INT_MAX / nonzc)
214             zn = rc->ncoeffs - rc->fbits * nonzc / rc->ufbits;
215         else
216             zn = 0;
217         zn = x264_clip3(zn, 0, rc->ncoeffs);
218         dqp = h->param.i_rc_sens * exp2f((float) rc->qpa / 6) *
219             (zn - rc->nzcoeffs) / rc->nzcoeffs;
220         dqp = x264_clip3(dqp, -h->param.i_qp_step, h->param.i_qp_step);
221         rc->qp = rc->qpa + dqp;
222     }
223
224     if(rc->fbits > 0.9 * maxbits)
225         rc->qp += 2;
226     else if(rc->fbits > 0.8 * maxbits)
227         rc->qp += 1;
228     else if(rc->fbits < 1.1 * minbits)
229         rc->qp -= 2;
230     else if(rc->fbits < 1.2 * minbits)
231         rc->qp -= 1;
232
233     rc->qp = x264_clip3(rc->qp, h->param.i_qp_min, h->param.i_qp_max);
234     rc->qpm = rc->qp;
235
236 #if DEBUG_RC > 1
237     fprintf(stderr, "fbits=%i, qp=%i, z=%i, min=%i, max=%i\n",
238          rc->fbits, rc->qpm, zn, minbits, maxbits);
239 #endif
240
241     rc->fbits -= rc->overhead;
242     rc->ufbits = 0;
243     rc->ncoeffs = 0;
244     rc->nzcoeffs = 0;
245     rc->mb = 0;
246     rc->qps = 0;
247 }
248
249 void x264_ratecontrol_mb( x264_t *h, int bits )
250 {
251     x264_ratecontrol_t *rc = h->rc;
252     int rbits;
253     int zn, enz, nonz;
254     int rcoeffs;
255     int dqp;
256     int i;
257
258     if( !h->param.b_cbr )
259         return;
260
261     x264_cpu_restore( h->param.cpu );
262
263     rc->qps += rc->qpm;
264     rc->ufbits += bits;
265     rc->mb++;
266
267     for(i = 0; i < 16 + 8; i++)
268         rc->nzcoeffs += 16 - h->mb.cache.non_zero_count[x264_scan8[i]];
269     rc->ncoeffs += 16 * (16 + 8);
270
271     if(rc->mb < rc->nmb / 16)
272         return;
273     else if(rc->mb == rc->nmb)
274         return;
275
276     rcoeffs = (rc->nmb - rc->mb) * 16 * 24;
277     rbits = rc->fbits - rc->ufbits;
278 /*     if(rbits < 0) */
279 /*      rbits = 0; */
280
281 /*     zn = (rc->nmb - rc->mb) * 16 * 24; */
282     nonz = (rc->ncoeffs - rc->nzcoeffs);
283     if(nonz == 0)
284         zn = rcoeffs;
285     else if(rc->ufbits && rbits < INT_MAX / nonz)
286         zn = rcoeffs - rbits * nonz / rc->ufbits;
287     else
288         zn = 0;
289     zn = x264_clip3(zn, 0, rcoeffs);
290     enz = rc->nzcoeffs * (rc->nmb - rc->mb) / rc->mb;
291     dqp = (float) 2*h->param.i_rc_sens * exp2f((float) rc->qps / rc->mb / 6) *
292         (zn - enz) / enz;
293     rc->qpm = x264_clip3(rc->qpm + dqp, rc->qp - 3, rc->qp + 3);
294     if(rbits <= 0)
295         rc->qpm++;
296     rc->qpm = x264_clip3(rc->qpm, h->param.i_qp_min, h->param.i_qp_max);
297 }
298
299 int  x264_ratecontrol_qp( x264_t *h )
300 {
301     return h->rc->qpm;
302 }
303
304 void x264_ratecontrol_end( x264_t *h, int bits )
305 {
306     x264_ratecontrol_t *rc = h->rc;
307
308     if(!h->param.b_cbr)
309         return;
310
311     rc->buffer_fullness += rc->rcbufrate - bits;
312     if(rc->buffer_fullness < 0){
313         fprintf(stderr, "x264: buffer underflow %i\n", rc->buffer_fullness);
314         rc->buffer_fullness = 0;
315     }
316
317     rc->qpa = rc->qps / rc->mb;
318     if(rc->slice_type == SLICE_TYPE_P){
319         rc->qp_avg_p += rc->qpa;
320         rc->qp_last_p = rc->qpa;
321         rc->pframes++;
322     } else if(rc->slice_type == SLICE_TYPE_I){
323         float err = (float) rc->ufbits / rc->fbits;
324         if(err > 1.1)
325             rc->gop_qp++;
326         else if(err < 0.9)
327             rc->gop_qp--;
328     }
329
330     rc->overhead = bits - rc->ufbits;
331
332 #if DEBUG_RC > 1
333     fprintf(stderr, " bits=%i, qp=%i, z=%i, zr=%6.3f, buf=%i\n",
334          bits, rc->qpa, rc->nzcoeffs,
335          (float) rc->nzcoeffs / rc->ncoeffs, rc->buffer_fullness);
336 #endif
337
338     rc->bits_last_gop += bits;
339     rc->frames++;
340     rc->mb = 0;
341 }
342
343 /*
344   Local Variables:
345   indent-tabs-mode: nil
346   End:
347 */