FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ratecontrol.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 335 589 56.9%
Functions: 13 19 68.4%
Branches: 154 323 47.7%

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