]> git.sesse.net Git - x264/blob - encoder/ratecontrol.c
* all: Patch from Måns Rullgård <mru AT mru DOT ath DOT cx>
[x264] / encoder / ratecontrol.c
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 struct x264_ratecontrol_t
37 {
38     /* constants */
39     float fps;
40     int gop_size;
41     int bitrate;
42     int nmb;                    /* number of MBs */
43     int buffer_size;
44     int rcbufrate;
45     int init_qp;
46
47     int gop_qp;
48     int buffer_fullness;
49     int frames;                 /* frames in current gop */
50     int pframes;
51     int slice_type;
52     int mb;                     /* MBs processed in current frame */
53     int bits_gop;               /* allocated bits current gop */
54     int bits_last_gop;          /* bits consumed in gop */
55     int qp;                     /* qp for current frame */
56     int qpm;                    /* qp for next MB */
57     int qpa;                    /* average qp for last frame */
58     int qps;
59     int qp_avg_p;               /* average QP for P frames */
60     int qp_last_p;
61     int fbits;                  /* bits allocated for current frame */
62     int ufbits;                 /* bits used for current frame */
63     int nzcoeffs;               /* # of 0-quantized coefficients */
64     int ncoeffs;                /* total # of coefficients */
65     int overhead;
66 };
67
68 int x264_ratecontrol_new( x264_t *h )
69 {
70     x264_ratecontrol_t *rc = x264_malloc( sizeof( x264_ratecontrol_t ) );
71     float bpp;
72
73     memset(rc, 0, sizeof(*rc));
74
75     rc->fps = h->param.f_fps > 0.1 ? h->param.f_fps : 25.0f;
76     rc->gop_size = h->param.i_iframe;
77     rc->bitrate = h->param.i_bitrate * 1000;
78     rc->nmb = ((h->param.i_width + 15) / 16) * ((h->param.i_height + 15) / 16);
79
80     rc->qp = h->param.i_qp_constant;
81     rc->qpa = rc->qp;
82     rc->qpm = rc->qp;
83
84     rc->buffer_size = h->param.i_rc_buffer_size;
85     if(rc->buffer_size <= 0)
86         rc->buffer_size = rc->bitrate / 2;
87     rc->buffer_fullness = h->param.i_rc_init_buffer;
88     rc->rcbufrate = rc->bitrate / rc->fps;
89
90     bpp = rc->bitrate / (rc->fps * h->param.i_width * h->param.i_height);
91     if(bpp <= 0.6)
92         rc->init_qp = 31;
93     else if(bpp <= 1.4)
94         rc->init_qp = 25;
95     else if(bpp <= 2.4)
96         rc->init_qp = 20;
97     else
98         rc->init_qp = 10;
99     rc->gop_qp = rc->init_qp;
100
101     rc->bits_last_gop = 0;
102
103 /*     fprintf(stderr, "%f fps, %i bps, bufsize %i\n", */
104 /*          rc->fps, rc->bitrate, rc->buffer_size); */
105
106     h->rc = rc;
107
108     return 0;
109 }
110
111 void x264_ratecontrol_delete( x264_t *h )
112 {
113     x264_ratecontrol_t *rc = h->rc;
114     x264_free( rc );
115 }
116
117 void x264_ratecontrol_start( x264_t *h, int i_slice_type )
118 {
119     x264_ratecontrol_t *rc = h->rc;
120     int gframes, iframes, pframes, bframes;
121     int minbits, maxbits;
122     int gbits, fbits;
123     int zn = 0;
124     float kp;
125     int gbuf;
126
127     if(!h->param.b_cbr)
128         return;
129
130     x264_cpu_restore( h->param.cpu );
131
132     rc->slice_type = i_slice_type;
133
134     switch(i_slice_type){
135     case SLICE_TYPE_I:
136         gbuf = rc->buffer_fullness + (rc->gop_size-1) * rc->rcbufrate;
137         rc->bits_gop = gbuf - rc->buffer_size / 2;
138
139         if(!rc->mb && rc->pframes){
140             int qp = (float) rc->qp_avg_p / rc->pframes + 0.5;
141 #if 0 /* JM does this without explaining why */
142             int gdq = (float) rc->gop_size / 15 + 0.5;
143             if(gdq > 2)
144                 gdq = 2;
145             qp -= gdq;
146             if(qp > rc->qp_last_p - 2)
147                 qp--;
148 #endif
149             qp = x264_clip3(qp, rc->gop_qp - 4, rc->gop_qp + 4);
150             qp =
151                 x264_clip3(qp, h->param.i_qp_min, h->param.i_qp_max);
152             rc->gop_qp = qp;
153         } else {
154             rc->gop_qp = rc->init_qp;
155         }
156
157         kp = h->param.f_ip_factor * h->param.f_pb_factor;
158
159         rc->bits_last_gop = 0;
160         rc->frames = 0;
161         rc->pframes = 0;
162         rc->qp_avg_p = 0;
163         break;
164
165     case SLICE_TYPE_P:
166         kp = h->param.f_pb_factor;
167         break;
168
169     case SLICE_TYPE_B:
170         kp = 1.0;
171         break;
172
173     default:
174         fprintf(stderr, "x264: ratecontrol: unknown slice type %i\n",
175                 i_slice_type);
176         kp = 1.0;
177         break;
178     }
179
180     gframes = rc->gop_size - rc->frames;
181     iframes = gframes / rc->gop_size;
182     pframes = gframes / (h->param.i_bframe + 1) - iframes;
183     bframes = gframes - pframes - iframes;
184
185     gbits = rc->bits_gop - rc->bits_last_gop;
186     fbits = kp * gbits /
187         (h->param.f_ip_factor * h->param.f_pb_factor * iframes +
188          h->param.f_pb_factor * pframes + bframes);
189
190     minbits = rc->buffer_fullness + rc->rcbufrate - rc->buffer_size;
191     if(minbits < 0)
192         minbits = 0;
193     maxbits = rc->buffer_fullness;
194     rc->fbits = x264_clip3(fbits, minbits, maxbits);
195
196     if(i_slice_type == SLICE_TYPE_I){
197         rc->qp = rc->gop_qp;
198     } else if(rc->ncoeffs){
199         int dqp;
200
201         zn = rc->ncoeffs -
202             rc->fbits * (rc->ncoeffs - rc->nzcoeffs) / rc->ufbits;
203         dqp = h->param.i_rc_sens * exp2f((float) rc->qpa / 6) *
204             (zn - rc->nzcoeffs) / rc->nzcoeffs;
205         dqp = x264_clip3(dqp, -h->param.i_qp_step, h->param.i_qp_step);
206         rc->qp = rc->qpa + dqp;
207     }
208
209     if(rc->fbits > 0.8 * maxbits)
210         rc->qp += 1;
211     else if(rc->fbits > 0.9 * maxbits)
212         rc->qp += 2;
213     else if(rc->fbits < 1.2 * minbits)
214         rc->qp -= 1;
215     else if(rc->fbits < 1.1 * minbits)
216         rc->qp -= 2;
217
218     rc->qp = x264_clip3(rc->qp, h->param.i_qp_min, h->param.i_qp_max);
219     rc->qpm = rc->qp;
220
221 /*     fprintf(stderr, "fbits=%i, qp=%i, z=%i, min=%i, max=%i\n", */
222 /*          rc->fbits, rc->qpm, zn, minbits, maxbits); */
223
224     rc->fbits -= rc->overhead;
225     rc->ufbits = 0;
226     rc->ncoeffs = 0;
227     rc->nzcoeffs = 0;
228     rc->mb = 0;
229     rc->qps = 0;
230 }
231
232 void x264_ratecontrol_mb( x264_t *h, int bits )
233 {
234     x264_ratecontrol_t *rc = h->rc;
235     int rbits;
236     int zn, enz;
237     int dqp;
238     int i;
239
240     x264_cpu_restore( h->param.cpu );
241
242     rc->qps += rc->qpm;
243     rc->ufbits += bits;
244     rc->mb++;
245
246     for(i = 0; i < 16 + 8; i++)
247         rc->nzcoeffs += 16 - h->mb.cache.non_zero_count[x264_scan8[i]];
248     rc->ncoeffs += 16 * (16 + 8);
249
250     if(rc->mb < rc->nmb / 16)
251         return;
252     else if(rc->mb == rc->nmb)
253         return;
254
255     rbits = rc->fbits - rc->ufbits;
256 /*     if(rbits < 0) */
257 /*      rbits = 0; */
258
259     zn = (rc->nmb - rc->mb) * 16 * 24;
260     if(rc->ufbits)
261         zn -= rbits * (rc->ncoeffs - rc->nzcoeffs) / rc->ufbits;
262     enz = rc->nzcoeffs * (rc->nmb - rc->mb) / rc->mb;
263     dqp = (float) 2*h->param.i_rc_sens * exp2f((float) rc->qps / rc->mb / 6) *
264         (zn - enz) / enz;
265     rc->qpm = x264_clip3(rc->qpm + dqp, rc->qp - 3, rc->qp + 3);
266     if(rbits <= 0)
267         rc->qpm++;
268     rc->qpm = x264_clip3(rc->qpm, h->param.i_qp_min, h->param.i_qp_max);
269 }
270
271 int  x264_ratecontrol_qp( x264_t *h )
272 {
273     return h->rc->qpm;
274 }
275
276 void x264_ratecontrol_end( x264_t *h, int bits )
277 {
278     x264_ratecontrol_t *rc = h->rc;
279
280     if(!h->param.b_cbr)
281         return;
282
283     rc->buffer_fullness += rc->rcbufrate - bits;
284     if(rc->buffer_fullness < 0){
285         fprintf(stderr, "x264: buffer underflow %i\n", rc->buffer_fullness);
286         rc->buffer_fullness = 0;
287     }
288
289     rc->qpa = rc->qps / rc->mb;
290     if(rc->slice_type == SLICE_TYPE_P){
291         rc->qp_avg_p += rc->qpa;
292         rc->qp_last_p = rc->qpa;
293         rc->pframes++;
294     }
295
296     rc->overhead = bits - rc->ufbits;
297
298 /*     fprintf(stderr, " bits=%i, qp=%i, z=%i, zr=%6.3f, buf=%i\n", */
299 /*          bits, rc->qpa, rc->nzcoeffs, */
300 /*          (float) rc->nzcoeffs / rc->ncoeffs, rc->buffer_fullness); */
301
302     rc->bits_last_gop += bits;
303     rc->frames++;
304     rc->mb = 0;
305 }
306