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 $
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"
38 struct x264_ratecontrol_t
44 int nmb; /* number of MBs */
51 int frames; /* frames in current gop */
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 */
61 int qp_avg_p; /* average QP for P frames */
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 */
70 int x264_ratecontrol_new( x264_t *h )
72 x264_ratecontrol_t *rc = x264_malloc( sizeof( x264_ratecontrol_t ) );
75 memset(rc, 0, sizeof(*rc));
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);
82 rc->qp = h->param.i_qp_constant;
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;
92 bpp = rc->bitrate / (rc->fps * h->param.i_width * h->param.i_height);
101 rc->gop_qp = rc->init_qp;
103 rc->bits_last_gop = 0;
106 fprintf(stderr, "%f fps, %i bps, bufsize %i\n",
107 rc->fps, rc->bitrate, rc->buffer_size);
115 void x264_ratecontrol_delete( x264_t *h )
117 x264_ratecontrol_t *rc = h->rc;
121 void x264_ratecontrol_start( x264_t *h, int i_slice_type )
123 x264_ratecontrol_t *rc = h->rc;
124 int gframes, iframes, pframes, bframes;
125 int minbits, maxbits;
134 x264_cpu_restore( h->param.cpu );
136 rc->slice_type = i_slice_type;
138 switch(i_slice_type){
140 gbuf = rc->buffer_fullness + (rc->gop_size-1) * rc->rcbufrate;
141 rc->bits_gop = gbuf - rc->buffer_size / 2;
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;
150 if(qp > rc->qp_last_p - 2)
153 qp = x264_clip3(qp, rc->gop_qp - 4, rc->gop_qp + 4);
155 x264_clip3(qp, h->param.i_qp_min, h->param.i_qp_max);
157 } else if(rc->frames > 4){
158 rc->gop_qp = rc->init_qp;
161 kp = h->param.f_ip_factor * h->param.f_pb_factor;
164 fprintf(stderr, "gbuf=%i bits_gop=%i frames=%i gop_qp=%i\n",
165 gbuf, rc->bits_gop, rc->frames, rc->gop_qp);
168 rc->bits_last_gop = 0;
175 kp = h->param.f_pb_factor;
183 fprintf(stderr, "x264: ratecontrol: unknown slice type %i\n",
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;
194 gbits = rc->bits_gop - rc->bits_last_gop;
196 (h->param.f_ip_factor * h->param.f_pb_factor * iframes +
197 h->param.f_pb_factor * pframes + bframes);
199 minbits = rc->buffer_fullness + rc->rcbufrate - rc->buffer_size;
202 maxbits = rc->buffer_fullness;
203 rc->fbits = x264_clip3(fbits, minbits, maxbits);
205 if(i_slice_type == SLICE_TYPE_I){
207 } else if(rc->ncoeffs && rc->ufbits){
210 nonzc = (rc->ncoeffs - rc->nzcoeffs);
213 else if(rc->fbits < INT_MAX / nonzc)
214 zn = rc->ncoeffs - rc->fbits * nonzc / rc->ufbits;
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;
224 if(rc->fbits > 0.9 * maxbits)
226 else if(rc->fbits > 0.8 * maxbits)
228 else if(rc->fbits < 1.1 * minbits)
230 else if(rc->fbits < 1.2 * minbits)
233 rc->qp = x264_clip3(rc->qp, h->param.i_qp_min, h->param.i_qp_max);
237 fprintf(stderr, "fbits=%i, qp=%i, z=%i, min=%i, max=%i\n",
238 rc->fbits, rc->qpm, zn, minbits, maxbits);
241 rc->fbits -= rc->overhead;
249 void x264_ratecontrol_mb( x264_t *h, int bits )
251 x264_ratecontrol_t *rc = h->rc;
258 if( !h->param.b_cbr )
261 x264_cpu_restore( h->param.cpu );
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);
271 if(rc->mb < rc->nmb / 16)
273 else if(rc->mb == rc->nmb)
276 rcoeffs = (rc->nmb - rc->mb) * 16 * 24;
277 rbits = rc->fbits - rc->ufbits;
281 /* zn = (rc->nmb - rc->mb) * 16 * 24; */
282 nonz = (rc->ncoeffs - rc->nzcoeffs);
285 else if(rc->ufbits && rbits < INT_MAX / nonz)
286 zn = rcoeffs - rbits * nonz / rc->ufbits;
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) *
293 rc->qpm = x264_clip3(rc->qpm + dqp, rc->qp - 3, rc->qp + 3);
296 rc->qpm = x264_clip3(rc->qpm, h->param.i_qp_min, h->param.i_qp_max);
299 int x264_ratecontrol_qp( x264_t *h )
304 void x264_ratecontrol_end( x264_t *h, int bits )
306 x264_ratecontrol_t *rc = h->rc;
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;
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;
322 } else if(rc->slice_type == SLICE_TYPE_I){
323 float err = (float) rc->ufbits / rc->fbits;
330 rc->overhead = bits - rc->ufbits;
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);
338 rc->bits_last_gop += bits;
345 indent-tabs-mode: nil