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 $
7 * Authors: Måns Rullgård <mru@mru.ath.cx>
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.
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.
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 *****************************************************************************/
24 #define _ISOC99_SOURCE
32 #include "../core/common.h"
33 #include "../core/cpu.h"
34 #include "ratecontrol.h"
36 struct x264_ratecontrol_t
42 int nmb; /* number of MBs */
49 int frames; /* frames in current gop */
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 */
59 int qp_avg_p; /* average QP for P frames */
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 */
68 int x264_ratecontrol_new( x264_t *h )
70 x264_ratecontrol_t *rc = x264_malloc( sizeof( x264_ratecontrol_t ) );
73 memset(rc, 0, sizeof(*rc));
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);
80 rc->qp = h->param.i_qp_constant;
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;
90 bpp = rc->bitrate / (rc->fps * h->param.i_width * h->param.i_height);
99 rc->gop_qp = rc->init_qp;
101 rc->bits_last_gop = 0;
103 /* fprintf(stderr, "%f fps, %i bps, bufsize %i\n", */
104 /* rc->fps, rc->bitrate, rc->buffer_size); */
111 void x264_ratecontrol_delete( x264_t *h )
113 x264_ratecontrol_t *rc = h->rc;
117 void x264_ratecontrol_start( x264_t *h, int i_slice_type )
119 x264_ratecontrol_t *rc = h->rc;
120 int gframes, iframes, pframes, bframes;
121 int minbits, maxbits;
130 x264_cpu_restore( h->param.cpu );
132 rc->slice_type = i_slice_type;
134 switch(i_slice_type){
136 gbuf = rc->buffer_fullness + (rc->gop_size-1) * rc->rcbufrate;
137 rc->bits_gop = gbuf - rc->buffer_size / 2;
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;
146 if(qp > rc->qp_last_p - 2)
149 qp = x264_clip3(qp, rc->gop_qp - 4, rc->gop_qp + 4);
151 x264_clip3(qp, h->param.i_qp_min, h->param.i_qp_max);
154 rc->gop_qp = rc->init_qp;
157 kp = h->param.f_ip_factor * h->param.f_pb_factor;
159 rc->bits_last_gop = 0;
166 kp = h->param.f_pb_factor;
174 fprintf(stderr, "x264: ratecontrol: unknown slice type %i\n",
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;
185 gbits = rc->bits_gop - rc->bits_last_gop;
187 (h->param.f_ip_factor * h->param.f_pb_factor * iframes +
188 h->param.f_pb_factor * pframes + bframes);
190 minbits = rc->buffer_fullness + rc->rcbufrate - rc->buffer_size;
193 maxbits = rc->buffer_fullness;
194 rc->fbits = x264_clip3(fbits, minbits, maxbits);
196 if(i_slice_type == SLICE_TYPE_I){
198 } else if(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;
209 if(rc->fbits > 0.8 * maxbits)
211 else if(rc->fbits > 0.9 * maxbits)
213 else if(rc->fbits < 1.2 * minbits)
215 else if(rc->fbits < 1.1 * minbits)
218 rc->qp = x264_clip3(rc->qp, h->param.i_qp_min, h->param.i_qp_max);
221 /* fprintf(stderr, "fbits=%i, qp=%i, z=%i, min=%i, max=%i\n", */
222 /* rc->fbits, rc->qpm, zn, minbits, maxbits); */
224 rc->fbits -= rc->overhead;
232 void x264_ratecontrol_mb( x264_t *h, int bits )
234 x264_ratecontrol_t *rc = h->rc;
240 x264_cpu_restore( h->param.cpu );
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);
250 if(rc->mb < rc->nmb / 16)
252 else if(rc->mb == rc->nmb)
255 rbits = rc->fbits - rc->ufbits;
259 zn = (rc->nmb - rc->mb) * 16 * 24;
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) *
265 rc->qpm = x264_clip3(rc->qpm + dqp, rc->qp - 3, rc->qp + 3);
268 rc->qpm = x264_clip3(rc->qpm, h->param.i_qp_min, h->param.i_qp_max);
271 int x264_ratecontrol_qp( x264_t *h )
276 void x264_ratecontrol_end( x264_t *h, int bits )
278 x264_ratecontrol_t *rc = h->rc;
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;
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;
296 rc->overhead = bits - rc->ufbits;
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); */
302 rc->bits_last_gop += bits;