FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ratecontrol.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 345 601 57.4%
Functions: 14 20 70.0%
Branches: 158 327 48.3%

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(MpegEncContext *s)
39 {
40 snprintf(s->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->cur_pic.ptr->display_picture_number,
44 s->cur_pic.ptr->coded_picture_number,
45 s->pict_type,
46 s->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 s->mc_mb_var_sum,
54 s->mb_var_sum,
55 s->i_count,
56 s->header_bits);
57 }
58
59 17200 static AVRational get_fpsQ(AVCodecContext *avctx)
60 {
61
2/4
✓ Branch 0 taken 17200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17200 times.
✗ Branch 3 not taken.
17200 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
62 17200 return avctx->framerate;
63
64 FF_DISABLE_DEPRECATION_WARNINGS
65 #if FF_API_TICKS_PER_FRAME
66 return av_div_q((AVRational){1, FFMAX(avctx->ticks_per_frame, 1)}, avctx->time_base);
67 #else
68 return av_inv_q(avctx->time_base);
69 #endif
70 FF_ENABLE_DEPRECATION_WARNINGS
71 }
72
73 17200 static double get_fps(AVCodecContext *avctx)
74 {
75 17200 return av_q2d(get_fpsQ(avctx));
76 }
77
78 static inline double qp2bits(const RateControlEntry *rce, double qp)
79 {
80 if (qp <= 0.0) {
81 av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
82 }
83 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / qp;
84 }
85
86 static double qp2bits_cb(void *rce, double qp)
87 {
88 return qp2bits(rce, qp);
89 }
90
91 3502 static inline double bits2qp(const RateControlEntry *rce, double bits)
92 {
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3502 times.
3502 if (bits < 0.9) {
94 av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
95 }
96 3502 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / bits;
97 }
98
99 static double bits2qp_cb(void *rce, double qp)
100 {
101 return bits2qp(rce, qp);
102 }
103
104 3452 static double get_diff_limited_q(MpegEncContext *s, const RateControlEntry *rce, double q)
105 {
106 3452 RateControlContext *rcc = &s->rc_context;
107 3452 AVCodecContext *a = s->avctx;
108 3452 const int pict_type = rce->new_pict_type;
109 3452 const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P];
110 3452 const double last_non_b_q = rcc->last_qscale_for[rcc->last_non_b_pict_type];
111
112
2/2
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 2611 times.
3452 if (pict_type == AV_PICTURE_TYPE_I &&
113
3/4
✓ Branch 0 taken 841 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 151 times.
✓ Branch 3 taken 690 times.
841 (a->i_quant_factor > 0.0 || rcc->last_non_b_pict_type == AV_PICTURE_TYPE_P))
114 151 q = last_p_q * FFABS(a->i_quant_factor) + a->i_quant_offset;
115
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 2533 times.
3301 else if (pict_type == AV_PICTURE_TYPE_B &&
116
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 a->b_quant_factor > 0.0)
117 768 q = last_non_b_q * a->b_quant_factor + a->b_quant_offset;
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (q < 1)
119 q = 1;
120
121 /* last qscale / qdiff stuff */
122
4/4
✓ Branch 0 taken 1151 times.
✓ Branch 1 taken 2301 times.
✓ Branch 2 taken 949 times.
✓ Branch 3 taken 202 times.
3452 if (rcc->last_non_b_pict_type == pict_type || pict_type != AV_PICTURE_TYPE_I) {
123 3250 double last_q = rcc->last_qscale_for[pict_type];
124 3250 const int maxdiff = FF_QP2LAMBDA * a->max_qdiff;
125
126
2/2
✓ Branch 0 taken 831 times.
✓ Branch 1 taken 2419 times.
3250 if (q > last_q + maxdiff)
127 831 q = last_q + maxdiff;
128
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 2339 times.
2419 else if (q < last_q - maxdiff)
129 80 q = last_q - maxdiff;
130 }
131
132 3452 rcc->last_qscale_for[pict_type] = q; // Note we cannot do that after blurring
133
134
2/2
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 768 times.
3452 if (pict_type != AV_PICTURE_TYPE_B)
135 2684 rcc->last_non_b_pict_type = pict_type;
136
137 3452 return q;
138 }
139
140 /**
141 * Get the qmin & qmax for pict_type.
142 */
143 6904 static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type)
144 {
145 6904 int qmin = s->lmin;
146 6904 int qmax = s->lmax;
147
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6904 times.
6904 av_assert0(qmin <= qmax);
149
150
3/3
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 1682 times.
✓ Branch 2 taken 3686 times.
6904 switch (pict_type) {
151 1536 case AV_PICTURE_TYPE_B:
152 1536 qmin = (int)(qmin * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset + 0.5);
153 1536 qmax = (int)(qmax * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset + 0.5);
154 1536 break;
155 1682 case AV_PICTURE_TYPE_I:
156 1682 qmin = (int)(qmin * FFABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset + 0.5);
157 1682 qmax = (int)(qmax * FFABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset + 0.5);
158 1682 break;
159 }
160
161 6904 qmin = av_clip(qmin, 1, FF_LAMBDA_MAX);
162 6904 qmax = av_clip(qmax, 1, FF_LAMBDA_MAX);
163
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6904 times.
6904 if (qmax < qmin)
165 qmax = qmin;
166
167 6904 *qmin_ret = qmin;
168 6904 *qmax_ret = qmax;
169 6904 }
170
171 3452 static double modify_qscale(MpegEncContext *s, const RateControlEntry *rce,
172 double q, int frame_num)
173 {
174 3452 RateControlContext *rcc = &s->rc_context;
175 3452 const double buffer_size = s->avctx->rc_buffer_size;
176 3452 const double fps = get_fps(s->avctx);
177 3452 const double min_rate = s->avctx->rc_min_rate / fps;
178 3452 const double max_rate = s->avctx->rc_max_rate / fps;
179 3452 const int pict_type = rce->new_pict_type;
180 int qmin, qmax;
181
182 3452 get_qminmax(&qmin, &qmax, s, pict_type);
183
184 /* modulation */
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (rcc->qmod_freq &&
186 frame_num % rcc->qmod_freq == 0 &&
187 pict_type == AV_PICTURE_TYPE_P)
188 q *= rcc->qmod_amp;
189
190 /* buffer overflow/underflow protection */
191
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 3427 times.
3452 if (buffer_size) {
192 25 double expected_size = rcc->buffer_index;
193 double q_limit;
194
195
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (min_rate) {
196 25 double d = 2 * (buffer_size - expected_size) / buffer_size;
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (d > 1.0)
198 d = 1.0;
199
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 else if (d < 0.0001)
200 25 d = 0.0001;
201 25 q *= pow(d, 1.0 / rcc->buffer_aggressivity);
202
203 25 q_limit = bits2qp(rce,
204
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 FFMAX((min_rate - buffer_size + rcc->buffer_index) *
205 s->avctx->rc_min_vbv_overflow_use, 1));
206
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (q > q_limit) {
208 if (s->avctx->debug & FF_DEBUG_RC)
209 av_log(s->avctx, AV_LOG_DEBUG,
210 "limiting QP %f -> %f\n", q, q_limit);
211 q = q_limit;
212 }
213 }
214
215
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (max_rate) {
216 25 double d = 2 * expected_size / buffer_size;
217
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 if (d > 1.0)
218 25 d = 1.0;
219 else if (d < 0.0001)
220 d = 0.0001;
221 25 q /= pow(d, 1.0 / rcc->buffer_aggressivity);
222
223 25 q_limit = bits2qp(rce,
224
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 FFMAX(rcc->buffer_index *
225 s->avctx->rc_max_available_vbv_use,
226 1));
227
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
25 if (q < q_limit) {
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->avctx->debug & FF_DEBUG_RC)
229 av_log(s->avctx, AV_LOG_DEBUG,
230 "limiting QP %f -> %f\n", q, q_limit);
231 24 q = q_limit;
232 }
233 }
234 }
235 ff_dlog(s->avctx, "q:%f max:%f min:%f size:%f index:%f agr:%f\n",
236 q, max_rate, min_rate, buffer_size, rcc->buffer_index,
237 rcc->buffer_aggressivity);
238
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3452 if (rcc->qsquish == 0.0 || qmin == qmax) {
239
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 2750 times.
3452 if (q < qmin)
240 702 q = qmin;
241
2/2
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 2239 times.
2750 else if (q > qmax)
242 511 q = qmax;
243 } else {
244 double min2 = log(qmin);
245 double max2 = log(qmax);
246
247 q = log(q);
248 q = (q - min2) / (max2 - min2) - 0.5;
249 q *= -4.0;
250 q = 1.0 / (1.0 + exp(q));
251 q = q * (max2 - min2) + min2;
252
253 q = exp(q);
254 }
255
256 3452 return q;
257 }
258
259 /**
260 * Modify the bitrate curve from pass1 for one frame.
261 */
262 3452 static double get_qscale(MpegEncContext *s, RateControlEntry *rce,
263 double rate_factor, int frame_num)
264 {
265 3452 RateControlContext *rcc = &s->rc_context;
266 3452 AVCodecContext *a = s->avctx;
267 3452 const int pict_type = rce->new_pict_type;
268 3452 const double mb_num = s->mb_num;
269 double q, bits;
270 int i;
271
272 10356 double const_values[] = {
273 M_PI,
274 M_E,
275 3452 rce->i_tex_bits * rce->qscale,
276 3452 rce->p_tex_bits * rce->qscale,
277 3452 (rce->i_tex_bits + rce->p_tex_bits) * (double)rce->qscale,
278 3452 rce->mv_bits / mb_num,
279
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 2684 times.
3452 rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code) * 0.5 : rce->f_code,
280 3452 rce->i_count / mb_num,
281 3452 rce->mc_mb_var_sum / mb_num,
282 3452 rce->mb_var_sum / mb_num,
283
2/2
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 2611 times.
3452 rce->pict_type == AV_PICTURE_TYPE_I,
284
2/2
✓ Branch 0 taken 1843 times.
✓ Branch 1 taken 1609 times.
3452 rce->pict_type == AV_PICTURE_TYPE_P,
285
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 2684 times.
3452 rce->pict_type == AV_PICTURE_TYPE_B,
286 3452 rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
287 3452 a->qcompress,
288 3452 rcc->i_cplx_sum[AV_PICTURE_TYPE_I] / (double)rcc->frame_count[AV_PICTURE_TYPE_I],
289 3452 rcc->i_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P],
290 3452 rcc->p_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P],
291 3452 rcc->p_cplx_sum[AV_PICTURE_TYPE_B] / (double)rcc->frame_count[AV_PICTURE_TYPE_B],
292 3452 (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
293 0
294 };
295
296 3452 bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (isnan(bits)) {
298 av_log(s->avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", rcc->rc_eq);
299 return -1;
300 }
301
302 3452 rcc->pass1_rc_eq_output_sum += bits;
303 3452 bits *= rate_factor;
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (bits < 0.0)
305 bits = 0.0;
306 3452 bits += 1.0; // avoid 1/0 issues
307
308 /* user override */
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 for (i = 0; i < s->avctx->rc_override_count; i++) {
310 RcOverride *rco = s->avctx->rc_override;
311 if (rco[i].start_frame > frame_num)
312 continue;
313 if (rco[i].end_frame < frame_num)
314 continue;
315
316 if (rco[i].qscale)
317 bits = qp2bits(rce, rco[i].qscale); // FIXME move at end to really force it?
318 else
319 bits *= rco[i].quality_factor;
320 }
321
322 3452 q = bits2qp(rce, bits);
323
324 /* I/B difference */
325
3/4
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 2611 times.
✓ Branch 2 taken 841 times.
✗ Branch 3 not taken.
3452 if (pict_type == AV_PICTURE_TYPE_I && s->avctx->i_quant_factor < 0.0)
326 841 q = -q * s->avctx->i_quant_factor + s->avctx->i_quant_offset;
327
3/4
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 1843 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 768 times.
2611 else if (pict_type == AV_PICTURE_TYPE_B && s->avctx->b_quant_factor < 0.0)
328 q = -q * s->avctx->b_quant_factor + s->avctx->b_quant_offset;
329
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3441 times.
3452 if (q < 1)
330 11 q = 1;
331
332 3452 return q;
333 }
334
335 static int init_pass2(MpegEncContext *s)
336 {
337 RateControlContext *rcc = &s->rc_context;
338 AVCodecContext *a = s->avctx;
339 int i, toobig;
340 AVRational fps = get_fpsQ(s->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(s->bit_rate,
345 (AVRational){rcc->num_entries,1},
346 fps);
347 double rate_factor = 0;
348 double step;
349 const int filter_size = (int)(a->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(s->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 = s->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(s, &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(s, 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(s, 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 = a->qblur == 0 ? 1.0 : exp(-d * d / (a->qblur * a->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(s, 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(s, bits);
445
446 rce->expected_bits = expected_bits;
447 expected_bits += bits;
448 }
449
450 ff_dlog(s->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(s->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 s->avctx->qmin, s->avctx->qmax);
470 }
471 av_assert0(toobig <= 40);
472 av_log(s->avctx, AV_LOG_DEBUG,
473 "[lavc rc] requested bitrate: %"PRId64" bps expected bitrate: %"PRId64" bps\n",
474 s->bit_rate,
475 (int64_t)(expected_bits / ((double)all_available_bits / s->bit_rate)));
476 av_log(s->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(s->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(s->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(s->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 204 av_cold int ff_rate_control_init(MpegEncContext *s)
498 {
499 204 RateControlContext *rcc = &s->rc_context;
500 int i, res;
501 static const char * const const_names[] = {
502 "PI",
503 "E",
504 "iTex",
505 "pTex",
506 "tex",
507 "mv",
508 "fCode",
509 "iCount",
510 "mcVar",
511 "var",
512 "isI",
513 "isP",
514 "isB",
515 "avgQP",
516 "qComp",
517 "avgIITex",
518 "avgPITex",
519 "avgPPTex",
520 "avgBPTex",
521 "avgTex",
522 NULL
523 };
524 static double (* const func1[])(void *, double) = {
525 bits2qp_cb,
526 qp2bits_cb,
527 NULL
528 };
529 static const char * const func1_names[] = {
530 "bits2qp",
531 "qp2bits",
532 NULL
533 };
534 204 emms_c();
535
536
4/4
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 202 times.
204 if (!s->avctx->rc_max_available_vbv_use && s->avctx->rc_buffer_size) {
537
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->avctx->rc_max_rate) {
538 1 s->avctx->rc_max_available_vbv_use = av_clipf(s->avctx->rc_max_rate/(s->avctx->rc_buffer_size*get_fps(s->avctx)), 1.0/3, 1.0);
539 } else
540 s->avctx->rc_max_available_vbv_use = 1.0;
541 }
542
543 204 res = av_expr_parse(&rcc->rc_eq_eval,
544 204 rcc->rc_eq ? rcc->rc_eq : "tex^qComp",
545 const_names, func1_names, func1,
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 NULL, NULL, 0, s->avctx);
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 if (res < 0) {
548 av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", rcc->rc_eq);
549 return res;
550 }
551
552
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 204 times.
1224 for (i = 0; i < 5; i++) {
553 1020 rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0;
554 1020 rcc->pred[i].count = 1.0;
555 1020 rcc->pred[i].decay = 0.4;
556
557 1020 rcc->i_cplx_sum [i] =
558 1020 rcc->p_cplx_sum [i] =
559 1020 rcc->mv_bits_sum[i] =
560 1020 rcc->qscale_sum [i] =
561 1020 rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such
562
563 1020 rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5;
564 }
565 204 rcc->buffer_index = s->avctx->rc_initial_buffer_occupancy;
566
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 2 times.
204 if (!rcc->buffer_index)
567 202 rcc->buffer_index = s->avctx->rc_buffer_size * 3 / 4;
568
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
570 int i;
571 char *p;
572
573 /* find number of pics */
574 p = s->avctx->stats_in;
575 for (i = -1; p; i++)
576 p = strchr(p + 1, ';');
577 i += s->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->mb_num + 10;
593 rce->mb_var_sum = s->mb_num * 100;
594 }
595
596 /* read stats */
597 p = s->avctx->stats_in;
598 for (i = 0; i < rcc->num_entries - s->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(s->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(s);
636 if (res < 0)
637 return res;
638 }
639
640
1/2
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
204 if (!(s->avctx->flags & AV_CODEC_FLAG_PASS2)) {
641 204 rcc->short_term_qsum = 0.001;
642 204 rcc->short_term_qcount = 0.001;
643
644 204 rcc->pass1_rc_eq_output_sum = 0.001;
645 204 rcc->pass1_wanted_bits = 0.001;
646
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 if (s->avctx->qblur > 1.0) {
648 av_log(s->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 204 times.
204 if (rcc->initial_cplx) {
653 for (i = 0; i < 60 * 30; i++) {
654 double bits = rcc->initial_cplx * (i / 10000.0 + 1.0) * s->mb_num;
655 RateControlEntry rce;
656
657 if (i % ((s->gop_size + 3) / 4) == 0)
658 rce.pict_type = AV_PICTURE_TYPE_I;
659 else if (i % (s->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->mb_num / 100000;
666 rce.mb_var_sum = s->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->pict_type == AV_PICTURE_TYPE_I) {
674 rce.i_count = s->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(s, &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 += s->bit_rate / get_fps(s->avctx);
693 }
694 }
695 }
696
697
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 188 times.
204 if (s->adaptive_quant) {
698 16 unsigned mb_array_size = s->mb_stride * s->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 204 return 0;
707 }
708
709 215 av_cold void ff_rate_control_uninit(RateControlContext *rcc)
710 {
711 215 emms_c();
712
713 // rc_eq is always managed via an AVOption and therefore not freed here.
714 215 av_expr_free(rcc->rc_eq_eval);
715 215 rcc->rc_eq_eval = NULL;
716 215 av_freep(&rcc->entry);
717 215 av_freep(&rcc->cplx_tab);
718 215 }
719
720 10295 int ff_vbv_update(MpegEncContext *s, int frame_size)
721 {
722 10295 RateControlContext *rcc = &s->rc_context;
723 10295 const double fps = get_fps(s->avctx);
724 10295 const int buffer_size = s->avctx->rc_buffer_size;
725 10295 const double min_rate = s->avctx->rc_min_rate / fps;
726 10295 const double max_rate = s->avctx->rc_max_rate / fps;
727
728 ff_dlog(s->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 10245 times.
10295 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(s->avctx, AV_LOG_ERROR, "rc buffer underflow\n");
737 if (frame_size > max_rate && s->qscale == s->avctx->qmax) {
738 av_log(s->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->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 (s->avctx->debug & FF_DEBUG_RC)
754 av_log(s->avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing);
755
756 50 return stuffing;
757 }
758 }
759 10245 return 0;
760 }
761
762 3452 static double predict_size(Predictor *p, double q, double var)
763 {
764 3452 return p->coeff * var / (q * p->count);
765 }
766
767 3301 static void update_predictor(Predictor *p, double q, double var, double size)
768 {
769 3301 double new_coeff = size * q / (var + 1);
770
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 2289 times.
3301 if (var < 10)
771 1012 return;
772
773 2289 p->count *= p->decay;
774 2289 p->coeff *= p->decay;
775 2289 p->count++;
776 2289 p->coeff += new_coeff;
777 }
778
779 800 static void adaptive_quantization(RateControlContext *const rcc,
780 MpegEncContext *const s, double q)
781 {
782 int i;
783 800 const float lumi_masking = s->avctx->lumi_masking / (128.0 * 128.0);
784 800 const float dark_masking = s->avctx->dark_masking / (128.0 * 128.0);
785 800 const float temp_cplx_masking = s->avctx->temporal_cplx_masking;
786 800 const float spatial_cplx_masking = s->avctx->spatial_cplx_masking;
787 800 const float p_masking = s->avctx->p_masking;
788 800 const float border_masking = s->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->avctx->mb_lmin;
794 800 const int qmax = s->avctx->mb_lmax;
795 800 const int mb_width = s->mb_width;
796 800 const int mb_height = s->mb_height;
797
798
2/2
✓ Branch 0 taken 239550 times.
✓ Branch 1 taken 800 times.
240350 for (i = 0; i < s->mb_num; i++) {
799 239550 const int mb_xy = s->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->mb_stride;
805 239550 int mb_y = mb_xy / s->mb_stride;
806 int mb_distance;
807 239550 float mb_factor = 0.0;
808
2/2
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 232708 times.
239550 if (spat_cplx < 4)
809 6842 spat_cplx = 4; // FIXME fine-tune
810
2/2
✓ Branch 0 taken 101018 times.
✓ Branch 1 taken 138532 times.
239550 if (temp_cplx < 4)
811 101018 temp_cplx = 4; // FIXME fine-tune
812
813
2/2
✓ Branch 0 taken 45078 times.
✓ Branch 1 taken 194472 times.
239550 if ((s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTRA)) { // FIXME hq mode
814 45078 cplx = spat_cplx;
815 45078 factor = 1.0 + p_masking;
816 } else {
817 194472 cplx = temp_cplx;
818 194472 factor = pow(temp_cplx, -temp_cplx_masking);
819 }
820 239550 factor *= pow(spat_cplx, -spatial_cplx_masking);
821
822
2/2
✓ Branch 0 taken 131480 times.
✓ Branch 1 taken 108070 times.
239550 if (lumi > 127)
823 131480 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
824 else
825 108070 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 (i = 0; i < s->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 (i = 0; i < s->mb_num; i++) {
878 239550 const int mb_xy = s->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 17875 times.
✓ Branch 1 taken 221675 times.
239550 if (intq > qmax)
889 17875 intq = qmax;
890
2/2
✓ Branch 0 taken 1713 times.
✓ Branch 1 taken 219962 times.
221675 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(MpegEncContext *s)
897 {
898 const RateControlContext *rcc = &s->rc_context;
899 const RateControlEntry *rce = &rcc->entry[s->picture_number];
900
901 s->f_code = rce->f_code;
902 s->b_code = rce->b_code;
903 }
904
905 // FIXME rd or at least approx for dquant
906
907 3452 float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
908 {
909 float q;
910 int qmin, qmax;
911 float br_compensation;
912 double diff;
913 double short_term_q;
914 double fps;
915 3452 int picture_number = s->picture_number;
916 int64_t wanted_bits;
917 3452 RateControlContext *rcc = &s->rc_context;
918 3452 AVCodecContext *a = s->avctx;
919 RateControlEntry local_rce, *rce;
920 double bits;
921 double rate_factor;
922 int64_t var;
923 3452 const int pict_type = s->pict_type;
924 3452 emms_c();
925
926 3452 get_qminmax(&qmin, &qmax, s, pict_type);
927
928 3452 fps = get_fps(s->avctx);
929 /* update predictors */
930
3/4
✓ Branch 0 taken 3301 times.
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 3301 times.
✗ Branch 3 not taken.
3452 if (picture_number > 2 && !dry_run) {
931 3301 const int64_t last_var =
932 3301 s->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum
933
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 2521 times.
3301 : rcc->last_mc_mb_var_sum;
934 av_assert1(s->frame_bits >= s->stuffing_bits);
935 3301 update_predictor(&rcc->pred[s->last_pict_type],
936 rcc->last_qscale,
937 sqrt(last_var),
938 3301 s->frame_bits - s->stuffing_bits);
939 }
940
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
942 av_assert0(picture_number >= 0);
943 if (picture_number >= rcc->num_entries) {
944 av_log(s->avctx, AV_LOG_ERROR, "Input is longer than 2-pass log file\n");
945 return -1;
946 }
947 rce = &rcc->entry[picture_number];
948 wanted_bits = rce->expected_bits;
949 } else {
950 const MPVPicture *dts_pic;
951 double wanted_bits_double;
952 3452 rce = &local_rce;
953
954 /* FIXME add a dts field to AVFrame and ensure it is set and use it
955 * here instead of reordering but the reordering is simpler for now
956 * until H.264 B-pyramid must be handled. */
957
4/4
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 768 times.
✓ Branch 2 taken 916 times.
✓ Branch 3 taken 1768 times.
3452 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
958 1684 dts_pic = s->cur_pic.ptr;
959 else
960 1768 dts_pic = s->last_pic.ptr;
961
962
3/4
✓ Branch 0 taken 3421 times.
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3421 times.
3452 if (!dts_pic || dts_pic->f->pts == AV_NOPTS_VALUE)
963 31 wanted_bits_double = s->bit_rate * (double)picture_number / fps;
964 else
965 3421 wanted_bits_double = s->bit_rate * (double)dts_pic->f->pts / fps;
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (wanted_bits_double > INT64_MAX) {
967 av_log(s->avctx, AV_LOG_WARNING, "Bits exceed 64bit range\n");
968 wanted_bits = INT64_MAX;
969 } else
970 3452 wanted_bits = (int64_t)wanted_bits_double;
971 }
972
973 3452 diff = s->total_bits - wanted_bits;
974 3452 br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance;
975
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 3089 times.
3452 if (br_compensation <= 0.0)
976 363 br_compensation = 0.001;
977
978
2/2
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 2611 times.
3452 var = pict_type == AV_PICTURE_TYPE_I ? s->mb_var_sum : s->mc_mb_var_sum;
979
980 3452 short_term_q = 0; /* avoid warning */
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
982 if (pict_type != AV_PICTURE_TYPE_I)
983 av_assert0(pict_type == rce->new_pict_type);
984
985 q = rce->new_qscale / br_compensation;
986 ff_dlog(s->avctx, "%f %f %f last:%d var:%"PRId64" type:%d//\n", q, rce->new_qscale,
987 br_compensation, s->frame_bits, var, pict_type);
988 } else {
989 3452 rce->pict_type =
990 3452 rce->new_pict_type = pict_type;
991 3452 rce->mc_mb_var_sum = s->mc_mb_var_sum;
992 3452 rce->mb_var_sum = s->mb_var_sum;
993 3452 rce->qscale = FF_QP2LAMBDA * 2;
994 3452 rce->f_code = s->f_code;
995 3452 rce->b_code = s->b_code;
996 3452 rce->misc_bits = 1;
997
998 3452 bits = predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
999
2/2
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 2611 times.
3452 if (pict_type == AV_PICTURE_TYPE_I) {
1000 841 rce->i_count = s->mb_num;
1001 841 rce->i_tex_bits = bits;
1002 841 rce->p_tex_bits = 0;
1003 841 rce->mv_bits = 0;
1004 } else {
1005 2611 rce->i_count = 0; // FIXME we do know this approx
1006 2611 rce->i_tex_bits = 0;
1007 2611 rce->p_tex_bits = bits * 0.9;
1008 2611 rce->mv_bits = bits * 0.1;
1009 }
1010 3452 rcc->i_cplx_sum[pict_type] += rce->i_tex_bits * rce->qscale;
1011 3452 rcc->p_cplx_sum[pict_type] += rce->p_tex_bits * rce->qscale;
1012 3452 rcc->mv_bits_sum[pict_type] += rce->mv_bits;
1013 3452 rcc->frame_count[pict_type]++;
1014
1015 3452 rate_factor = rcc->pass1_wanted_bits /
1016 3452 rcc->pass1_rc_eq_output_sum * br_compensation;
1017
1018 3452 q = get_qscale(s, rce, rate_factor, picture_number);
1019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (q < 0)
1020 return -1;
1021
1022
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 av_assert0(q > 0.0);
1023 3452 q = get_diff_limited_q(s, rce, q);
1024
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 av_assert0(q > 0.0);
1025
1026 // FIXME type dependent blur like in 2-pass
1027
4/4
✓ Branch 0 taken 1609 times.
✓ Branch 1 taken 1843 times.
✓ Branch 2 taken 651 times.
✓ Branch 3 taken 958 times.
3452 if (pict_type == AV_PICTURE_TYPE_P || s->intra_only) {
1028 2494 rcc->short_term_qsum *= a->qblur;
1029 2494 rcc->short_term_qcount *= a->qblur;
1030
1031 2494 rcc->short_term_qsum += q;
1032 2494 rcc->short_term_qcount++;
1033 2494 q = short_term_q = rcc->short_term_qsum / rcc->short_term_qcount;
1034 }
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 av_assert0(q > 0.0);
1036
1037 3452 q = modify_qscale(s, rce, q, picture_number);
1038
1039 3452 rcc->pass1_wanted_bits += s->bit_rate / fps;
1040
1041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 av_assert0(q > 0.0);
1042 }
1043
1044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (s->avctx->debug & FF_DEBUG_RC) {
1045 av_log(s->avctx, AV_LOG_DEBUG,
1046 "%c qp:%d<%2.1f<%d %d want:%"PRId64" total:%"PRId64" comp:%f st_q:%2.2f "
1047 "size:%d var:%"PRId64"/%"PRId64" br:%"PRId64" fps:%d\n",
1048 av_get_picture_type_char(pict_type),
1049 qmin, q, qmax, picture_number,
1050 wanted_bits / 1000, s->total_bits / 1000,
1051 br_compensation, short_term_q, s->frame_bits,
1052 s->mb_var_sum, s->mc_mb_var_sum,
1053 s->bit_rate / 1000, (int)fps);
1054 }
1055
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 if (q < qmin)
1057 q = qmin;
1058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3452 times.
3452 else if (q > qmax)
1059 q = qmax;
1060
1061
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 2652 times.
3452 if (s->adaptive_quant)
1062 800 adaptive_quantization(rcc, s, q);
1063 else
1064 2652 q = (int)(q + 0.5);
1065
1066
1/2
✓ Branch 0 taken 3452 times.
✗ Branch 1 not taken.
3452 if (!dry_run) {
1067 3452 rcc->last_qscale = q;
1068 3452 rcc->last_mc_mb_var_sum = s->mc_mb_var_sum;
1069 3452 rcc->last_mb_var_sum = s->mb_var_sum;
1070 }
1071 3452 return q;
1072 }
1073