../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
353 / 627
56.3%
Branches:
164 / 345
47.5%
Functions:
14 / 20
70.0%

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