FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ratecontrol.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 353 617 57.2%
Functions: 14 20 70.0%
Branches: 160 327 48.9%

Line Branch Exec Source
1 /*
2 * Rate control for video encoders
3 *
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Rate control for video encoders.
26 */
27
28 #include "libavutil/attributes.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/mem.h"
31
32 #include "avcodec.h"
33 #include "ratecontrol.h"
34 #include "mpegvideoenc.h"
35 #include "libavutil/eval.h"
36
37 void ff_write_pass1_stats(MPVMainEncContext *const m)
38 {
39 const MPVEncContext *const s = &m->s;
40 snprintf(s->c.avctx->stats_out, 256,
41 "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
42 "fcode:%d bcode:%d mc-var:%"PRId64" var:%"PRId64" icount:%d hbits:%d;\n",
43 s->c.cur_pic.ptr->display_picture_number,
44 s->c.cur_pic.ptr->coded_picture_number,
45 s->c.pict_type,
46 s->c.cur_pic.ptr->f->quality,
47 s->i_tex_bits,
48 s->p_tex_bits,
49 s->mv_bits,
50 s->misc_bits,
51 s->f_code,
52 s->b_code,
53 m->mc_mb_var_sum,
54 m->mb_var_sum,
55 s->i_count,
56 m->header_bits);
57 }
58
59 21970 static AVRational get_fpsQ(AVCodecContext *avctx)
60 {
61
3/4
✓ Branch 0 taken 21958 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 21958 times.
✗ Branch 3 not taken.
21970 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
62 21958 return avctx->framerate;
63
64 12 return av_inv_q(avctx->time_base);
65 }
66
67 21970 static double get_fps(AVCodecContext *avctx)
68 {
69 21970 return av_q2d(get_fpsQ(avctx));
70 }
71
72 static inline double qp2bits(const RateControlEntry *rce, double qp)
73 {
74 if (qp <= 0.0) {
75 av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
76 }
77 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / qp;
78 }
79
80 static double qp2bits_cb(void *rce, double qp)
81 {
82 return qp2bits(rce, qp);
83 }
84
85 5092 static inline double bits2qp(const RateControlEntry *rce, double bits)
86 {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5092 times.
5092 if (bits < 0.9) {
88 av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
89 }
90 5092 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / bits;
91 }
92
93 static double bits2qp_cb(void *rce, double qp)
94 {
95 return bits2qp(rce, qp);
96 }
97
98 5042 static double get_diff_limited_q(MPVMainEncContext *m, const RateControlEntry *rce, double q)
99 {
100 5042 MPVEncContext *const s = &m->s;
101 5042 RateControlContext *const rcc = &m->rc_context;
102 5042 AVCodecContext *const a = s->c.avctx;
103 5042 const int pict_type = rce->new_pict_type;
104 5042 const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P];
105 5042 const double last_non_b_q = rcc->last_qscale_for[rcc->last_non_b_pict_type];
106
107
2/2
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 4149 times.
5042 if (pict_type == AV_PICTURE_TYPE_I &&
108
3/4
✓ Branch 0 taken 893 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
✓ Branch 3 taken 713 times.
893 (a->i_quant_factor > 0.0 || rcc->last_non_b_pict_type == AV_PICTURE_TYPE_P))
109 180 q = last_p_q * FFABS(a->i_quant_factor) + a->i_quant_offset;
110
2/2
✓ Branch 0 taken 715 times.
✓ Branch 1 taken 4147 times.
4862 else if (pict_type == AV_PICTURE_TYPE_B &&
111
1/2
✓ Branch 0 taken 715 times.
✗ Branch 1 not taken.
715 a->b_quant_factor > 0.0)
112 715 q = last_non_b_q * a->b_quant_factor + a->b_quant_offset;
113
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 5018 times.
5042 if (q < 1)
114 24 q = 1;
115
116 /* last qscale / qdiff stuff */
117
4/4
✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 3870 times.
✓ Branch 2 taken 930 times.
✓ Branch 3 taken 242 times.
5042 if (rcc->last_non_b_pict_type == pict_type || pict_type != AV_PICTURE_TYPE_I) {
118 4800 double last_q = rcc->last_qscale_for[pict_type];
119 4800 const int maxdiff = FF_QP2LAMBDA * a->max_qdiff;
120
121
2/2
✓ Branch 0 taken 818 times.
✓ Branch 1 taken 3982 times.
4800 if (q > last_q + maxdiff)
122 818 q = last_q + maxdiff;
123
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 3902 times.
3982 else if (q < last_q - maxdiff)
124 80 q = last_q - maxdiff;
125 }
126
127 5042 rcc->last_qscale_for[pict_type] = q; // Note we cannot do that after blurring
128
129
2/2
✓ Branch 0 taken 4327 times.
✓ Branch 1 taken 715 times.
5042 if (pict_type != AV_PICTURE_TYPE_B)
130 4327 rcc->last_non_b_pict_type = pict_type;
131
132 5042 return q;
133 }
134
135 /**
136 * Get the qmin & qmax for pict_type.
137 */
138 10084 static void get_qminmax(int *qmin_ret, int *qmax_ret, MPVMainEncContext *const m, int pict_type)
139 {
140 10084 MPVEncContext *const s = &m->s;
141 10084 int qmin = m->lmin;
142 10084 int qmax = m->lmax;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10084 times.
10084 av_assert0(qmin <= qmax);
145
146
3/3
✓ Branch 0 taken 1430 times.
✓ Branch 1 taken 1786 times.
✓ Branch 2 taken 6868 times.
10084 switch (pict_type) {
147 1430 case AV_PICTURE_TYPE_B:
148 1430 qmin = (int)(qmin * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset + 0.5);
149 1430 qmax = (int)(qmax * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset + 0.5);
150 1430 break;
151 1786 case AV_PICTURE_TYPE_I:
152 1786 qmin = (int)(qmin * FFABS(s->c.avctx->i_quant_factor) + s->c.avctx->i_quant_offset + 0.5);
153 1786 qmax = (int)(qmax * FFABS(s->c.avctx->i_quant_factor) + s->c.avctx->i_quant_offset + 0.5);
154 1786 break;
155 }
156
157 10084 qmin = av_clip(qmin, 1, FF_LAMBDA_MAX);
158 10084 qmax = av_clip(qmax, 1, FF_LAMBDA_MAX);
159
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10084 times.
10084 if (qmax < qmin)
161 qmax = qmin;
162
163 10084 *qmin_ret = qmin;
164 10084 *qmax_ret = qmax;
165 10084 }
166
167 5042 static double modify_qscale(MPVMainEncContext *const m, const RateControlEntry *rce,
168 double q, int frame_num)
169 {
170 5042 MPVEncContext *const s = &m->s;
171 5042 RateControlContext *const rcc = &m->rc_context;
172 5042 const double buffer_size = s->c.avctx->rc_buffer_size;
173 5042 const double fps = get_fps(s->c.avctx);
174 5042 const double min_rate = s->c.avctx->rc_min_rate / fps;
175 5042 const double max_rate = s->c.avctx->rc_max_rate / fps;
176 5042 const int pict_type = rce->new_pict_type;
177 int qmin, qmax;
178
179 5042 get_qminmax(&qmin, &qmax, m, pict_type);
180
181 /* modulation */
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (rcc->qmod_freq &&
183 frame_num % rcc->qmod_freq == 0 &&
184 pict_type == AV_PICTURE_TYPE_P)
185 q *= rcc->qmod_amp;
186
187 /* buffer overflow/underflow protection */
188
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5017 times.
5042 if (buffer_size) {
189 25 double expected_size = rcc->buffer_index;
190 double q_limit;
191
192
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (min_rate) {
193 25 double d = 2 * (buffer_size - expected_size) / buffer_size;
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (d > 1.0)
195 d = 1.0;
196
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 else if (d < 0.0001)
197 25 d = 0.0001;
198 25 q *= pow(d, 1.0 / rcc->buffer_aggressivity);
199
200 25 q_limit = bits2qp(rce,
201
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 FFMAX((min_rate - buffer_size + rcc->buffer_index) *
202 s->c.avctx->rc_min_vbv_overflow_use, 1));
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (q > q_limit) {
205 if (s->c.avctx->debug & FF_DEBUG_RC)
206 av_log(s->c.avctx, AV_LOG_DEBUG,
207 "limiting QP %f -> %f\n", q, q_limit);
208 q = q_limit;
209 }
210 }
211
212
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (max_rate) {
213 25 double d = 2 * expected_size / buffer_size;
214
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (d > 1.0)
215 25 d = 1.0;
216 else if (d < 0.0001)
217 d = 0.0001;
218 25 q /= pow(d, 1.0 / rcc->buffer_aggressivity);
219
220 25 q_limit = bits2qp(rce,
221
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 FFMAX(rcc->buffer_index *
222 s->c.avctx->rc_max_available_vbv_use,
223 1));
224
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
25 if (q < q_limit) {
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->c.avctx->debug & FF_DEBUG_RC)
226 av_log(s->c.avctx, AV_LOG_DEBUG,
227 "limiting QP %f -> %f\n", q, q_limit);
228 24 q = q_limit;
229 }
230 }
231 }
232 ff_dlog(s->c.avctx, "q:%f max:%f min:%f size:%f index:%f agr:%f\n",
233 q, max_rate, min_rate, buffer_size, rcc->buffer_index,
234 rcc->buffer_aggressivity);
235
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5042 if (rcc->qsquish == 0.0 || qmin == qmax) {
236
2/2
✓ Branch 0 taken 1912 times.
✓ Branch 1 taken 3130 times.
5042 if (q < qmin)
237 1912 q = qmin;
238
2/2
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 2619 times.
3130 else if (q > qmax)
239 511 q = qmax;
240 } else {
241 double min2 = log(qmin);
242 double max2 = log(qmax);
243
244 q = log(q);
245 q = (q - min2) / (max2 - min2) - 0.5;
246 q *= -4.0;
247 q = 1.0 / (1.0 + exp(q));
248 q = q * (max2 - min2) + min2;
249
250 q = exp(q);
251 }
252
253 5042 return q;
254 }
255
256 /**
257 * Modify the bitrate curve from pass1 for one frame.
258 */
259 5042 static double get_qscale(MPVMainEncContext *const m, RateControlEntry *rce,
260 double rate_factor, int frame_num)
261 {
262 5042 MPVEncContext *const s = &m->s;
263 5042 RateControlContext *rcc = &m->rc_context;
264 5042 AVCodecContext *const avctx = s->c.avctx;
265 5042 const int pict_type = rce->new_pict_type;
266 5042 const double mb_num = s->c.mb_num;
267 double q, bits;
268 int i;
269
270 15126 double const_values[] = {
271 M_PI,
272 M_E,
273 5042 rce->i_tex_bits * rce->qscale,
274 5042 rce->p_tex_bits * rce->qscale,
275 5042 (rce->i_tex_bits + rce->p_tex_bits) * (double)rce->qscale,
276 5042 rce->mv_bits / mb_num,
277
2/2
✓ Branch 0 taken 715 times.
✓ Branch 1 taken 4327 times.
5042 rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code) * 0.5 : rce->f_code,
278 5042 rce->i_count / mb_num,
279 5042 rce->mc_mb_var_sum / mb_num,
280 5042 rce->mb_var_sum / mb_num,
281
2/2
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 4149 times.
5042 rce->pict_type == AV_PICTURE_TYPE_I,
282
2/2
✓ Branch 0 taken 3434 times.
✓ Branch 1 taken 1608 times.
5042 rce->pict_type == AV_PICTURE_TYPE_P,
283
2/2
✓ Branch 0 taken 715 times.
✓ Branch 1 taken 4327 times.
5042 rce->pict_type == AV_PICTURE_TYPE_B,
284 5042 rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
285 5042 avctx->qcompress,
286 5042 rcc->i_cplx_sum[AV_PICTURE_TYPE_I] / (double)rcc->frame_count[AV_PICTURE_TYPE_I],
287 5042 rcc->i_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P],
288 5042 rcc->p_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P],
289 5042 rcc->p_cplx_sum[AV_PICTURE_TYPE_B] / (double)rcc->frame_count[AV_PICTURE_TYPE_B],
290 5042 (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
291 0
292 };
293
294 5042 bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (isnan(bits)) {
296 av_log(avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", rcc->rc_eq);
297 return -1;
298 }
299
300 5042 rcc->pass1_rc_eq_output_sum += bits;
301 5042 bits *= rate_factor;
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (bits < 0.0)
303 bits = 0.0;
304 5042 bits += 1.0; // avoid 1/0 issues
305
306 /* user override */
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 for (i = 0; i < avctx->rc_override_count; i++) {
308 RcOverride *rco = avctx->rc_override;
309 if (rco[i].start_frame > frame_num)
310 continue;
311 if (rco[i].end_frame < frame_num)
312 continue;
313
314 if (rco[i].qscale)
315 bits = qp2bits(rce, rco[i].qscale); // FIXME move at end to really force it?
316 else
317 bits *= rco[i].quality_factor;
318 }
319
320 5042 q = bits2qp(rce, bits);
321
322 /* I/B difference */
323
3/4
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 4149 times.
✓ Branch 2 taken 893 times.
✗ Branch 3 not taken.
5042 if (pict_type == AV_PICTURE_TYPE_I && avctx->i_quant_factor < 0.0)
324 893 q = -q * avctx->i_quant_factor + avctx->i_quant_offset;
325
3/4
✓ Branch 0 taken 715 times.
✓ Branch 1 taken 3434 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 715 times.
4149 else if (pict_type == AV_PICTURE_TYPE_B && avctx->b_quant_factor < 0.0)
326 q = -q * avctx->b_quant_factor + avctx->b_quant_offset;
327
2/2
✓ Branch 0 taken 1202 times.
✓ Branch 1 taken 3840 times.
5042 if (q < 1)
328 1202 q = 1;
329
330 5042 return q;
331 }
332
333 static int init_pass2(MPVMainEncContext *const m)
334 {
335 RateControlContext *const rcc = &m->rc_context;
336 MPVEncContext *const s = &m->s;
337 AVCodecContext *const avctx = s->c.avctx;
338 int i, toobig;
339 AVRational fps = get_fpsQ(avctx);
340 double complexity[5] = { 0 }; // approximate bits at quant=1
341 uint64_t const_bits[5] = { 0 }; // quantizer independent bits
342 uint64_t all_const_bits;
343 uint64_t all_available_bits = av_rescale_q(m->bit_rate,
344 (AVRational){rcc->num_entries,1},
345 fps);
346 double rate_factor = 0;
347 double step;
348 const int filter_size = (int)(avctx->qblur * 4) | 1;
349 double expected_bits = 0; // init to silence gcc warning
350 double *qscale, *blurred_qscale, qscale_sum;
351
352 /* find complexity & const_bits & decide the pict_types */
353 for (i = 0; i < rcc->num_entries; i++) {
354 RateControlEntry *rce = &rcc->entry[i];
355
356 rce->new_pict_type = rce->pict_type;
357 rcc->i_cplx_sum[rce->pict_type] += rce->i_tex_bits * rce->qscale;
358 rcc->p_cplx_sum[rce->pict_type] += rce->p_tex_bits * rce->qscale;
359 rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits;
360 rcc->frame_count[rce->pict_type]++;
361
362 complexity[rce->new_pict_type] += (rce->i_tex_bits + rce->p_tex_bits) *
363 (double)rce->qscale;
364 const_bits[rce->new_pict_type] += rce->mv_bits + rce->misc_bits;
365 }
366
367 all_const_bits = const_bits[AV_PICTURE_TYPE_I] +
368 const_bits[AV_PICTURE_TYPE_P] +
369 const_bits[AV_PICTURE_TYPE_B];
370
371 if (all_available_bits < all_const_bits) {
372 av_log(avctx, AV_LOG_ERROR, "requested bitrate is too low\n");
373 return -1;
374 }
375
376 qscale = av_malloc_array(rcc->num_entries, sizeof(double));
377 blurred_qscale = av_malloc_array(rcc->num_entries, sizeof(double));
378 if (!qscale || !blurred_qscale) {
379 av_free(qscale);
380 av_free(blurred_qscale);
381 return AVERROR(ENOMEM);
382 }
383 toobig = 0;
384
385 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
386 expected_bits = 0;
387 rate_factor += step;
388
389 rcc->buffer_index = avctx->rc_buffer_size / 2;
390
391 /* find qscale */
392 for (i = 0; i < rcc->num_entries; i++) {
393 const RateControlEntry *rce = &rcc->entry[i];
394
395 qscale[i] = get_qscale(m, &rcc->entry[i], rate_factor, i);
396 rcc->last_qscale_for[rce->pict_type] = qscale[i];
397 }
398 av_assert0(filter_size % 2 == 1);
399
400 /* fixed I/B QP relative to P mode */
401 for (i = FFMAX(0, rcc->num_entries - 300); i < rcc->num_entries; i++) {
402 const RateControlEntry *rce = &rcc->entry[i];
403
404 qscale[i] = get_diff_limited_q(m, rce, qscale[i]);
405 }
406
407 for (i = rcc->num_entries - 1; i >= 0; i--) {
408 const RateControlEntry *rce = &rcc->entry[i];
409
410 qscale[i] = get_diff_limited_q(m, rce, qscale[i]);
411 }
412
413 /* smooth curve */
414 for (i = 0; i < rcc->num_entries; i++) {
415 const RateControlEntry *rce = &rcc->entry[i];
416 const int pict_type = rce->new_pict_type;
417 int j;
418 double q = 0.0, sum = 0.0;
419
420 for (j = 0; j < filter_size; j++) {
421 int index = i + j - filter_size / 2;
422 double d = index - i;
423 double coeff = avctx->qblur == 0 ? 1.0 : exp(-d * d / (avctx->qblur * avctx->qblur));
424
425 if (index < 0 || index >= rcc->num_entries)
426 continue;
427 if (pict_type != rcc->entry[index].new_pict_type)
428 continue;
429 q += qscale[index] * coeff;
430 sum += coeff;
431 }
432 blurred_qscale[i] = q / sum;
433 }
434
435 /* find expected bits */
436 for (i = 0; i < rcc->num_entries; i++) {
437 RateControlEntry *rce = &rcc->entry[i];
438 double bits;
439
440 rce->new_qscale = modify_qscale(m, rce, blurred_qscale[i], i);
441
442 bits = qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits;
443 bits += 8 * ff_vbv_update(m, bits);
444
445 rce->expected_bits = expected_bits;
446 expected_bits += bits;
447 }
448
449 ff_dlog(avctx,
450 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
451 expected_bits, (int)all_available_bits, rate_factor);
452 if (expected_bits > all_available_bits) {
453 rate_factor -= step;
454 ++toobig;
455 }
456 }
457 av_free(qscale);
458 av_free(blurred_qscale);
459
460 /* check bitrate calculations and print info */
461 qscale_sum = 0.0;
462 for (i = 0; i < rcc->num_entries; i++) {
463 ff_dlog(avctx, "[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
464 i,
465 rcc->entry[i].new_qscale,
466 rcc->entry[i].new_qscale / FF_QP2LAMBDA);
467 qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA,
468 avctx->qmin, avctx->qmax);
469 }
470 av_assert0(toobig <= 40);
471 av_log(avctx, AV_LOG_DEBUG,
472 "[lavc rc] requested bitrate: %"PRId64" bps expected bitrate: %"PRId64" bps\n",
473 m->bit_rate,
474 (int64_t)(expected_bits / ((double)all_available_bits / m->bit_rate)));
475 av_log(avctx, AV_LOG_DEBUG,
476 "[lavc rc] estimated target average qp: %.3f\n",
477 (float)qscale_sum / rcc->num_entries);
478 if (toobig == 0) {
479 av_log(avctx, AV_LOG_INFO,
480 "[lavc rc] Using all of requested bitrate is not "
481 "necessary for this video with these parameters.\n");
482 } else if (toobig == 40) {
483 av_log(avctx, AV_LOG_ERROR,
484 "[lavc rc] Error: bitrate too low for this video "
485 "with these parameters.\n");
486 return -1;
487 } else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
488 av_log(avctx, AV_LOG_ERROR,
489 "[lavc rc] Error: 2pass curve failed to converge\n");
490 return -1;
491 }
492
493 return 0;
494 }
495
496 215 av_cold int ff_rate_control_init(MPVMainEncContext *const m)
497 {
498 215 MPVEncContext *const s = &m->s;
499 215 RateControlContext *rcc = &m->rc_context;
500 215 AVCodecContext *const avctx = s->c.avctx;
501 int i, res;
502 static const char * const const_names[] = {
503 "PI",
504 "E",
505 "iTex",
506 "pTex",
507 "tex",
508 "mv",
509 "fCode",
510 "iCount",
511 "mcVar",
512 "var",
513 "isI",
514 "isP",
515 "isB",
516 "avgQP",
517 "qComp",
518 "avgIITex",
519 "avgPITex",
520 "avgPPTex",
521 "avgBPTex",
522 "avgTex",
523 NULL
524 };
525 static double (* const func1[])(void *, double) = {
526 bits2qp_cb,
527 qp2bits_cb,
528 NULL
529 };
530 static const char * const func1_names[] = {
531 "bits2qp",
532 "qp2bits",
533 NULL
534 };
535
536
4/4
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 213 times.
215 if (!avctx->rc_max_available_vbv_use && avctx->rc_buffer_size) {
537
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (avctx->rc_max_rate) {
538 1 avctx->rc_max_available_vbv_use = av_clipf(avctx->rc_max_rate/(avctx->rc_buffer_size*get_fps(avctx)), 1.0/3, 1.0);
539 } else
540 avctx->rc_max_available_vbv_use = 1.0;
541 }
542
543 215 res = av_expr_parse(&rcc->rc_eq_eval,
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 rcc->rc_eq ? rcc->rc_eq : "tex^qComp",
545 const_names, func1_names, func1,
546 NULL, NULL, 0, avctx);
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (res < 0) {
548 av_log(avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", rcc->rc_eq);
549 return res;
550 }
551
552
2/2
✓ Branch 0 taken 1075 times.
✓ Branch 1 taken 215 times.
1290 for (i = 0; i < 5; i++) {
553 1075 rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0;
554 1075 rcc->pred[i].count = 1.0;
555 1075 rcc->pred[i].decay = 0.4;
556
557 1075 rcc->i_cplx_sum [i] =
558 1075 rcc->p_cplx_sum [i] =
559 1075 rcc->mv_bits_sum[i] =
560 1075 rcc->qscale_sum [i] =
561 1075 rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such
562
563 1075 rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5;
564 }
565 215 rcc->buffer_index = avctx->rc_initial_buffer_occupancy;
566
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 2 times.
215 if (!rcc->buffer_index)
567 213 rcc->buffer_index = avctx->rc_buffer_size * 3 / 4;
568
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (avctx->flags & AV_CODEC_FLAG_PASS2) {
570 int i;
571 char *p;
572
573 /* find number of pics */
574 p = avctx->stats_in;
575 for (i = -1; p; i++)
576 p = strchr(p + 1, ';');
577 i += m->max_b_frames;
578 if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry))
579 return -1;
580 rcc->entry = av_mallocz(i * sizeof(RateControlEntry));
581 if (!rcc->entry)
582 return AVERROR(ENOMEM);
583 rcc->num_entries = i;
584
585 /* init all to skipped P-frames
586 * (with B-frames we might have a not encoded frame at the end FIXME) */
587 for (i = 0; i < rcc->num_entries; i++) {
588 RateControlEntry *rce = &rcc->entry[i];
589
590 rce->pict_type = rce->new_pict_type = AV_PICTURE_TYPE_P;
591 rce->qscale = rce->new_qscale = FF_QP2LAMBDA * 2;
592 rce->misc_bits = s->c.mb_num + 10;
593 rce->mb_var_sum = s->c.mb_num * 100;
594 }
595
596 /* read stats */
597 p = avctx->stats_in;
598 for (i = 0; i < rcc->num_entries - m->max_b_frames; i++) {
599 RateControlEntry *rce;
600 int picture_number;
601 int e;
602 char *next;
603
604 next = strchr(p, ';');
605 if (next) {
606 (*next) = 0; // sscanf is unbelievably slow on looong strings // FIXME copy / do not write
607 next++;
608 }
609 e = sscanf(p, " in:%d ", &picture_number);
610
611 av_assert0(picture_number >= 0);
612 av_assert0(picture_number < rcc->num_entries);
613 rce = &rcc->entry[picture_number];
614
615 e += sscanf(p, " in:%*d out:%*d type:%d q:%f itex:%d ptex:%d "
616 "mv:%d misc:%d "
617 "fcode:%d bcode:%d "
618 "mc-var:%"SCNd64" var:%"SCNd64" "
619 "icount:%d hbits:%d",
620 &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits,
621 &rce->mv_bits, &rce->misc_bits,
622 &rce->f_code, &rce->b_code,
623 &rce->mc_mb_var_sum, &rce->mb_var_sum,
624 &rce->i_count, &rce->header_bits);
625 if (e != 13) {
626 av_log(avctx, AV_LOG_ERROR,
627 "statistics are damaged at line %d, parser out=%d\n",
628 i, e);
629 return -1;
630 }
631
632 p = next;
633 }
634
635 res = init_pass2(m);
636 if (res < 0)
637 return res;
638 }
639
640
1/2
✓ Branch 0 taken 215 times.
✗ Branch 1 not taken.
215 if (!(avctx->flags & AV_CODEC_FLAG_PASS2)) {
641 215 rcc->short_term_qsum = 0.001;
642 215 rcc->short_term_qcount = 0.001;
643
644 215 rcc->pass1_rc_eq_output_sum = 0.001;
645 215 rcc->pass1_wanted_bits = 0.001;
646
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (avctx->qblur > 1.0) {
648 av_log(avctx, AV_LOG_ERROR, "qblur too large\n");
649 return -1;
650 }
651 /* init stuff with the user specified complexity */
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (rcc->initial_cplx) {
653 for (i = 0; i < 60 * 30; i++) {
654 double bits = rcc->initial_cplx * (i / 10000.0 + 1.0) * s->c.mb_num;
655 RateControlEntry rce;
656
657 if (i % ((m->gop_size + 3) / 4) == 0)
658 rce.pict_type = AV_PICTURE_TYPE_I;
659 else if (i % (m->max_b_frames + 1))
660 rce.pict_type = AV_PICTURE_TYPE_B;
661 else
662 rce.pict_type = AV_PICTURE_TYPE_P;
663
664 rce.new_pict_type = rce.pict_type;
665 rce.mc_mb_var_sum = bits * s->c.mb_num / 100000;
666 rce.mb_var_sum = s->c.mb_num;
667
668 rce.qscale = FF_QP2LAMBDA * 2;
669 rce.f_code = 2;
670 rce.b_code = 1;
671 rce.misc_bits = 1;
672
673 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
674 rce.i_count = s->c.mb_num;
675 rce.i_tex_bits = bits;
676 rce.p_tex_bits = 0;
677 rce.mv_bits = 0;
678 } else {
679 rce.i_count = 0; // FIXME we do know this approx
680 rce.i_tex_bits = 0;
681 rce.p_tex_bits = bits * 0.9;
682 rce.mv_bits = bits * 0.1;
683 }
684 rcc->i_cplx_sum[rce.pict_type] += rce.i_tex_bits * rce.qscale;
685 rcc->p_cplx_sum[rce.pict_type] += rce.p_tex_bits * rce.qscale;
686 rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits;
687 rcc->frame_count[rce.pict_type]++;
688
689 get_qscale(m, &rce, rcc->pass1_wanted_bits / rcc->pass1_rc_eq_output_sum, i);
690
691 // FIXME misbehaves a little for variable fps
692 rcc->pass1_wanted_bits += m->bit_rate / get_fps(avctx);
693 }
694 }
695 }
696
697
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 199 times.
215 if (s->adaptive_quant) {
698 16 unsigned mb_array_size = s->c.mb_stride * s->c.mb_height;
699
700 16 rcc->cplx_tab = av_malloc_array(mb_array_size, 2 * sizeof(*rcc->cplx_tab));
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!rcc->cplx_tab)
702 return AVERROR(ENOMEM);
703 16 rcc->bits_tab = rcc->cplx_tab + mb_array_size;
704 }
705
706 215 return 0;
707 }
708
709 226 av_cold void ff_rate_control_uninit(RateControlContext *rcc)
710 {
711 // rc_eq is always managed via an AVOption and therefore not freed here.
712 226 av_expr_free(rcc->rc_eq_eval);
713 226 rcc->rc_eq_eval = NULL;
714 226 av_freep(&rcc->entry);
715 226 av_freep(&rcc->cplx_tab);
716 226 }
717
718 11885 int ff_vbv_update(MPVMainEncContext *m, int frame_size)
719 {
720 11885 MPVEncContext *const s = &m->s;
721 11885 RateControlContext *const rcc = &m->rc_context;
722 11885 AVCodecContext *const avctx = s->c.avctx;
723 11885 const double fps = get_fps(avctx);
724 11885 const int buffer_size = avctx->rc_buffer_size;
725 11885 const double min_rate = avctx->rc_min_rate / fps;
726 11885 const double max_rate = avctx->rc_max_rate / fps;
727
728 ff_dlog(avctx, "%d %f %d %f %f\n",
729 buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate);
730
731
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 11835 times.
11885 if (buffer_size) {
732 int left;
733
734 50 rcc->buffer_index -= frame_size;
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (rcc->buffer_index < 0) {
736 av_log(avctx, AV_LOG_ERROR, "rc buffer underflow\n");
737 if (frame_size > max_rate && s->c.qscale == avctx->qmax) {
738 av_log(avctx, AV_LOG_ERROR, "max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
739 }
740 rcc->buffer_index = 0;
741 }
742
743 50 left = buffer_size - rcc->buffer_index - 1;
744 50 rcc->buffer_index += av_clip(left, min_rate, max_rate);
745
746
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (rcc->buffer_index > buffer_size) {
747 50 int stuffing = ceil((rcc->buffer_index - buffer_size) / 8);
748
749
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 if (stuffing < 4 && s->c.codec_id == AV_CODEC_ID_MPEG4)
750 stuffing = 4;
751 50 rcc->buffer_index -= 8 * stuffing;
752
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (avctx->debug & FF_DEBUG_RC)
754 av_log(avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing);
755
756 50 return stuffing;
757 }
758 }
759 11835 return 0;
760 }
761
762 5042 static double predict_size(Predictor *p, double q, double var)
763 {
764 5042 return p->coeff * var / (q * p->count);
765 }
766
767 4860 static void update_predictor(Predictor *p, double q, double var, double size)
768 {
769 4860 double new_coeff = size * q / (var + 1);
770
2/2
✓ Branch 0 taken 2547 times.
✓ Branch 1 taken 2313 times.
4860 if (var < 10)
771 2547 return;
772
773 2313 p->count *= p->decay;
774 2313 p->coeff *= p->decay;
775 2313 p->count++;
776 2313 p->coeff += new_coeff;
777 }
778
779 800 static void adaptive_quantization(RateControlContext *const rcc,
780 MPVMainEncContext *const m, double q)
781 {
782 800 MPVEncContext *const s = &m->s;
783 800 const float lumi_masking = s->c.avctx->lumi_masking / (128.0 * 128.0);
784 800 const float dark_masking = s->c.avctx->dark_masking / (128.0 * 128.0);
785 800 const float temp_cplx_masking = s->c.avctx->temporal_cplx_masking;
786 800 const float spatial_cplx_masking = s->c.avctx->spatial_cplx_masking;
787 800 const float p_masking = s->c.avctx->p_masking;
788 800 const float border_masking = m->border_masking;
789 800 float bits_sum = 0.0;
790 800 float cplx_sum = 0.0;
791 800 float *cplx_tab = rcc->cplx_tab;
792 800 float *bits_tab = rcc->bits_tab;
793 800 const int qmin = s->c.avctx->mb_lmin;
794 800 const int qmax = s->c.avctx->mb_lmax;
795 800 const int mb_width = s->c.mb_width;
796 800 const int mb_height = s->c.mb_height;
797
798
2/2
✓ Branch 0 taken 239550 times.
✓ Branch 1 taken 800 times.
240350 for (int i = 0; i < s->c.mb_num; i++) {
799 239550 const int mb_xy = s->c.mb_index2xy[i];
800 239550 float temp_cplx = sqrt(s->mc_mb_var[mb_xy]); // FIXME merge in pow()
801 239550 float spat_cplx = sqrt(s->mb_var[mb_xy]);
802 239550 const int lumi = s->mb_mean[mb_xy];
803 float bits, cplx, factor;
804 239550 int mb_x = mb_xy % s->c.mb_stride;
805 239550 int mb_y = mb_xy / s->c.mb_stride;
806 int mb_distance;
807 239550 float mb_factor = 0.0;
808
2/2
✓ Branch 0 taken 6843 times.
✓ Branch 1 taken 232707 times.
239550 if (spat_cplx < 4)
809 6843 spat_cplx = 4; // FIXME fine-tune
810
2/2
✓ Branch 0 taken 99178 times.
✓ Branch 1 taken 140372 times.
239550 if (temp_cplx < 4)
811 99178 temp_cplx = 4; // FIXME fine-tune
812
813
2/2
✓ Branch 0 taken 47477 times.
✓ Branch 1 taken 192073 times.
239550 if ((s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTRA)) { // FIXME hq mode
814 47477 cplx = spat_cplx;
815 47477 factor = 1.0 + p_masking;
816 } else {
817 192073 cplx = temp_cplx;
818 192073 factor = pow(temp_cplx, -temp_cplx_masking);
819 }
820 239550 factor *= pow(spat_cplx, -spatial_cplx_masking);
821
822
2/2
✓ Branch 0 taken 131426 times.
✓ Branch 1 taken 108124 times.
239550 if (lumi > 127)
823 131426 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
824 else
825 108124 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
826
827
2/2
✓ Branch 0 taken 43200 times.
✓ Branch 1 taken 196350 times.
239550 if (mb_x < mb_width / 5) {
828 43200 mb_distance = mb_width / 5 - mb_x;
829 43200 mb_factor = (float)mb_distance / (float)(mb_width / 5);
830
2/2
✓ Branch 0 taken 43200 times.
✓ Branch 1 taken 153150 times.
196350 } else if (mb_x > 4 * mb_width / 5) {
831 43200 mb_distance = mb_x - 4 * mb_width / 5;
832 43200 mb_factor = (float)mb_distance / (float)(mb_width / 5);
833 }
834
2/2
✓ Branch 0 taken 39600 times.
✓ Branch 1 taken 199950 times.
239550 if (mb_y < mb_height / 5) {
835 39600 mb_distance = mb_height / 5 - mb_y;
836
2/2
✓ Branch 0 taken 6000 times.
✓ Branch 1 taken 33600 times.
39600 mb_factor = FFMAX(mb_factor,
837 (float)mb_distance / (float)(mb_height / 5));
838
2/2
✓ Branch 0 taken 39600 times.
✓ Branch 1 taken 160350 times.
199950 } else if (mb_y > 4 * mb_height / 5) {
839 39600 mb_distance = mb_y - 4 * mb_height / 5;
840
2/2
✓ Branch 0 taken 6000 times.
✓ Branch 1 taken 33600 times.
39600 mb_factor = FFMAX(mb_factor,
841 (float)mb_distance / (float)(mb_height / 5));
842 }
843
844 239550 factor *= 1.0 - border_masking * mb_factor;
845
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 239550 times.
239550 if (factor < 0.00001)
847 factor = 0.00001;
848
849 239550 bits = cplx * factor;
850 239550 cplx_sum += cplx;
851 239550 bits_sum += bits;
852 239550 cplx_tab[i] = cplx;
853 239550 bits_tab[i] = bits;
854 }
855
856 /* handle qmin/qmax clipping */
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
800 if (s->mpv_flags & FF_MPV_FLAG_NAQ) {
858 float factor = bits_sum / cplx_sum;
859 for (int i = 0; i < s->c.mb_num; i++) {
860 float newq = q * cplx_tab[i] / bits_tab[i];
861 newq *= factor;
862
863 if (newq > qmax) {
864 bits_sum -= bits_tab[i];
865 cplx_sum -= cplx_tab[i] * q / qmax;
866 } else if (newq < qmin) {
867 bits_sum -= bits_tab[i];
868 cplx_sum -= cplx_tab[i] * q / qmin;
869 }
870 }
871 if (bits_sum < 0.001)
872 bits_sum = 0.001;
873 if (cplx_sum < 0.001)
874 cplx_sum = 0.001;
875 }
876
877
2/2
✓ Branch 0 taken 239550 times.
✓ Branch 1 taken 800 times.
240350 for (int i = 0; i < s->c.mb_num; i++) {
878 239550 const int mb_xy = s->c.mb_index2xy[i];
879 239550 float newq = q * cplx_tab[i] / bits_tab[i];
880 int intq;
881
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 239550 times.
239550 if (s->mpv_flags & FF_MPV_FLAG_NAQ) {
883 newq *= bits_sum / cplx_sum;
884 }
885
886 239550 intq = (int)(newq + 0.5);
887
888
2/2
✓ Branch 0 taken 7875 times.
✓ Branch 1 taken 231675 times.
239550 if (intq > qmax)
889 7875 intq = qmax;
890
2/2
✓ Branch 0 taken 1713 times.
✓ Branch 1 taken 229962 times.
231675 else if (intq < qmin)
891 1713 intq = qmin;
892 239550 s->lambda_table[mb_xy] = intq;
893 }
894 800 }
895
896 void ff_get_2pass_fcode(MPVMainEncContext *const m)
897 {
898 MPVEncContext *const s = &m->s;
899 const RateControlContext *rcc = &m->rc_context;
900 const RateControlEntry *rce = &rcc->entry[s->picture_number];
901
902 s->f_code = rce->f_code;
903 s->b_code = rce->b_code;
904 }
905
906 // FIXME rd or at least approx for dquant
907
908 5042 float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
909 {
910 5042 MPVEncContext *const s = &m->s;
911 5042 RateControlContext *rcc = &m->rc_context;
912 5042 AVCodecContext *const a = s->c.avctx;
913 float q;
914 int qmin, qmax;
915 float br_compensation;
916 double diff;
917 double short_term_q;
918 double fps;
919 5042 int picture_number = s->picture_number;
920 int64_t wanted_bits;
921 RateControlEntry local_rce, *rce;
922 double bits;
923 double rate_factor;
924 int64_t var;
925 5042 const int pict_type = s->c.pict_type;
926
927 5042 get_qminmax(&qmin, &qmax, m, pict_type);
928
929 5042 fps = get_fps(s->c.avctx);
930 /* update predictors */
931
3/4
✓ Branch 0 taken 4860 times.
✓ Branch 1 taken 182 times.
✓ Branch 2 taken 4860 times.
✗ Branch 3 not taken.
5042 if (picture_number > 2 && !dry_run) {
932 4860 const int64_t last_var =
933 4860 m->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum
934
2/2
✓ Branch 0 taken 807 times.
✓ Branch 1 taken 4053 times.
4860 : rcc->last_mc_mb_var_sum;
935 av_assert1(m->frame_bits >= m->stuffing_bits);
936 4860 update_predictor(&rcc->pred[m->last_pict_type],
937 rcc->last_qscale,
938 sqrt(last_var),
939 4860 m->frame_bits - m->stuffing_bits);
940 }
941
942
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
943 av_assert0(picture_number >= 0);
944 if (picture_number >= rcc->num_entries) {
945 av_log(s->c.avctx, AV_LOG_ERROR, "Input is longer than 2-pass log file\n");
946 return -1;
947 }
948 rce = &rcc->entry[picture_number];
949 wanted_bits = rce->expected_bits;
950 } else {
951 const MPVPicture *dts_pic;
952 double wanted_bits_double;
953 5042 rce = &local_rce;
954
955 /* FIXME add a dts field to AVFrame and ensure it is set and use it
956 * here instead of reordering but the reordering is simpler for now
957 * until H.264 B-pyramid must be handled. */
958
4/4
✓ Branch 0 taken 4327 times.
✓ Branch 1 taken 715 times.
✓ Branch 2 taken 920 times.
✓ Branch 3 taken 3407 times.
5042 if (s->c.pict_type == AV_PICTURE_TYPE_B || s->c.low_delay)
959 1635 dts_pic = s->c.cur_pic.ptr;
960 else
961 3407 dts_pic = s->c.last_pic.ptr;
962
963
3/4
✓ Branch 0 taken 5002 times.
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5002 times.
5042 if (!dts_pic || dts_pic->f->pts == AV_NOPTS_VALUE)
964 40 wanted_bits_double = m->bit_rate * (double)picture_number / fps;
965 else
966 5002 wanted_bits_double = m->bit_rate * (double)dts_pic->f->pts / fps;
967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (wanted_bits_double > INT64_MAX) {
968 av_log(s->c.avctx, AV_LOG_WARNING, "Bits exceed 64bit range\n");
969 wanted_bits = INT64_MAX;
970 } else
971 5042 wanted_bits = (int64_t)wanted_bits_double;
972 }
973
974 5042 diff = m->total_bits - wanted_bits;
975 5042 br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance;
976
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 4679 times.
5042 if (br_compensation <= 0.0)
977 363 br_compensation = 0.001;
978
979
2/2
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 4149 times.
5042 var = pict_type == AV_PICTURE_TYPE_I ? m->mb_var_sum : m->mc_mb_var_sum;
980
981 5042 short_term_q = 0; /* avoid warning */
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
983 if (pict_type != AV_PICTURE_TYPE_I)
984 av_assert0(pict_type == rce->new_pict_type);
985
986 q = rce->new_qscale / br_compensation;
987 ff_dlog(s->c.avctx, "%f %f %f last:%d var:%"PRId64" type:%d//\n", q, rce->new_qscale,
988 br_compensation, m->frame_bits, var, pict_type);
989 } else {
990 5042 rce->pict_type =
991 5042 rce->new_pict_type = pict_type;
992 5042 rce->mc_mb_var_sum = m->mc_mb_var_sum;
993 5042 rce->mb_var_sum = m->mb_var_sum;
994 5042 rce->qscale = FF_QP2LAMBDA * 2;
995 5042 rce->f_code = s->f_code;
996 5042 rce->b_code = s->b_code;
997 5042 rce->misc_bits = 1;
998
999 5042 bits = predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
1000
2/2
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 4149 times.
5042 if (pict_type == AV_PICTURE_TYPE_I) {
1001 893 rce->i_count = s->c.mb_num;
1002 893 rce->i_tex_bits = bits;
1003 893 rce->p_tex_bits = 0;
1004 893 rce->mv_bits = 0;
1005 } else {
1006 4149 rce->i_count = 0; // FIXME we do know this approx
1007 4149 rce->i_tex_bits = 0;
1008 4149 rce->p_tex_bits = bits * 0.9;
1009 4149 rce->mv_bits = bits * 0.1;
1010 }
1011 5042 rcc->i_cplx_sum[pict_type] += rce->i_tex_bits * rce->qscale;
1012 5042 rcc->p_cplx_sum[pict_type] += rce->p_tex_bits * rce->qscale;
1013 5042 rcc->mv_bits_sum[pict_type] += rce->mv_bits;
1014 5042 rcc->frame_count[pict_type]++;
1015
1016 5042 rate_factor = rcc->pass1_wanted_bits /
1017 5042 rcc->pass1_rc_eq_output_sum * br_compensation;
1018
1019 5042 q = get_qscale(m, rce, rate_factor, picture_number);
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (q < 0)
1021 return -1;
1022
1023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 av_assert0(q > 0.0);
1024 5042 q = get_diff_limited_q(m, rce, q);
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 av_assert0(q > 0.0);
1026
1027 // FIXME type dependent blur like in 2-pass
1028
4/4
✓ Branch 0 taken 1608 times.
✓ Branch 1 taken 3434 times.
✓ Branch 2 taken 669 times.
✓ Branch 3 taken 939 times.
5042 if (pict_type == AV_PICTURE_TYPE_P || m->intra_only) {
1029 4103 rcc->short_term_qsum *= a->qblur;
1030 4103 rcc->short_term_qcount *= a->qblur;
1031
1032 4103 rcc->short_term_qsum += q;
1033 4103 rcc->short_term_qcount++;
1034 4103 q = short_term_q = rcc->short_term_qsum / rcc->short_term_qcount;
1035 }
1036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 av_assert0(q > 0.0);
1037
1038 5042 q = modify_qscale(m, rce, q, picture_number);
1039
1040 5042 rcc->pass1_wanted_bits += m->bit_rate / fps;
1041
1042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 av_assert0(q > 0.0);
1043 }
1044
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (s->c.avctx->debug & FF_DEBUG_RC) {
1046 av_log(s->c.avctx, AV_LOG_DEBUG,
1047 "%c qp:%d<%2.1f<%d %d want:%"PRId64" total:%"PRId64" comp:%f st_q:%2.2f "
1048 "size:%d var:%"PRId64"/%"PRId64" br:%"PRId64" fps:%d\n",
1049 av_get_picture_type_char(pict_type),
1050 qmin, q, qmax, picture_number,
1051 wanted_bits / 1000, m->total_bits / 1000,
1052 br_compensation, short_term_q, m->frame_bits,
1053 m->mb_var_sum, m->mc_mb_var_sum,
1054 m->bit_rate / 1000, (int)fps);
1055 }
1056
1057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 if (q < qmin)
1058 q = qmin;
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5042 times.
5042 else if (q > qmax)
1060 q = qmax;
1061
1062
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 4242 times.
5042 if (s->adaptive_quant)
1063 800 adaptive_quantization(rcc, m, q);
1064 else
1065 4242 q = (int)(q + 0.5);
1066
1067
1/2
✓ Branch 0 taken 5042 times.
✗ Branch 1 not taken.
5042 if (!dry_run) {
1068 5042 rcc->last_qscale = q;
1069 5042 rcc->last_mc_mb_var_sum = m->mc_mb_var_sum;
1070 5042 rcc->last_mb_var_sum = m->mb_var_sum;
1071 }
1072 5042 return q;
1073 }
1074