FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ratecontrol.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 355 620 57.3%
Functions: 14 20 70.0%
Branches: 159 327 48.6%

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