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