FFmpeg
Loading...
Searching...
No Matches
ratecontrol.c
Go to the documentation of this file.
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
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
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,
54 m->mb_var_sum,
55 s->i_count,
56 m->header_bits);
57}
58
60{
61 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
62 return avctx->framerate;
63
64 return av_inv_q(avctx->time_base);
65}
66
67static double get_fps(AVCodecContext *avctx)
68{
69 return av_q2d(get_fpsQ(avctx));
70}
71
72static inline double qp2bits(const RateControlEntry *rce, double qp)
73{
74 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
80static double qp2bits_cb(void *rce, double qp)
81{
82 return qp2bits(rce, qp);
83}
84
85static inline double bits2qp(const RateControlEntry *rce, double bits)
86{
87 if (bits < 0.9) {
88 av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
89 }
90 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / bits;
91}
92
93static double bits2qp_cb(void *rce, double qp)
94{
95 return bits2qp(rce, qp);
96}
97
98static double get_diff_limited_q(MPVMainEncContext *m, const RateControlEntry *rce, double q)
99{
100 MPVEncContext *const s = &m->s;
101 RateControlContext *const rcc = &m->rc_context;
102 AVCodecContext *const a = s->c.avctx;
103 const int pict_type = rce->new_pict_type;
104 const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P];
105 const double last_non_b_q = rcc->last_qscale_for[rcc->last_non_b_pict_type];
106
107 if (pict_type == AV_PICTURE_TYPE_I &&
108 (a->i_quant_factor > 0.0 || rcc->last_non_b_pict_type == AV_PICTURE_TYPE_P))
109 q = last_p_q * FFABS(a->i_quant_factor) + a->i_quant_offset;
110 else if (pict_type == AV_PICTURE_TYPE_B &&
111 a->b_quant_factor > 0.0)
112 q = last_non_b_q * a->b_quant_factor + a->b_quant_offset;
113 if (q < 1)
114 q = 1;
115
116 /* last qscale / qdiff stuff */
117 if (rcc->last_non_b_pict_type == pict_type || pict_type != AV_PICTURE_TYPE_I) {
118 double last_q = rcc->last_qscale_for[pict_type];
119 const int maxdiff = FF_QP2LAMBDA * a->max_qdiff;
120
121 if (q > last_q + maxdiff)
122 q = last_q + maxdiff;
123 else if (q < last_q - maxdiff)
124 q = last_q - maxdiff;
125 }
126
127 rcc->last_qscale_for[pict_type] = q; // Note we cannot do that after blurring
128
129 if (pict_type != AV_PICTURE_TYPE_B)
130 rcc->last_non_b_pict_type = pict_type;
131
132 return q;
133}
134
135/**
136 * Get the qmin & qmax for pict_type.
137 */
138static void get_qminmax(int *qmin_ret, int *qmax_ret, MPVMainEncContext *const m, int pict_type)
139{
140 MPVEncContext *const s = &m->s;
141 int qmin = m->lmin;
142 int qmax = m->lmax;
143
144 av_assert0(qmin <= qmax);
145
146 switch (pict_type) {
148 qmin = (int)(qmin * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset + 0.5);
149 qmax = (int)(qmax * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset + 0.5);
150 break;
152 qmin = (int)(qmin * FFABS(s->c.avctx->i_quant_factor) + s->c.avctx->i_quant_offset + 0.5);
153 qmax = (int)(qmax * FFABS(s->c.avctx->i_quant_factor) + s->c.avctx->i_quant_offset + 0.5);
154 break;
155 }
156
157 qmin = av_clip(qmin, 1, FF_LAMBDA_MAX);
158 qmax = av_clip(qmax, 1, FF_LAMBDA_MAX);
159
160 if (qmax < qmin)
161 qmax = qmin;
162
163 *qmin_ret = qmin;
164 *qmax_ret = qmax;
165}
166
167static double modify_qscale(MPVMainEncContext *const m, const RateControlEntry *rce,
168 double q, int frame_num)
169{
170 MPVEncContext *const s = &m->s;
171 RateControlContext *const rcc = &m->rc_context;
172 const double buffer_size = s->c.avctx->rc_buffer_size;
173 const double fps = get_fps(s->c.avctx);
174 const double min_rate = s->c.avctx->rc_min_rate / fps;
175 const double max_rate = s->c.avctx->rc_max_rate / fps;
176 const int pict_type = rce->new_pict_type;
177 int qmin, qmax;
178
179 get_qminmax(&qmin, &qmax, m, pict_type);
180
181 /* modulation */
182 if (rcc->qmod_freq &&
183 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 if (buffer_size) {
189 double expected_size = rcc->buffer_index;
190 double q_limit;
191
192 if (min_rate) {
193 double d = 2 * (buffer_size - expected_size) / buffer_size;
194 if (d > 1.0)
195 d = 1.0;
196 else if (d < 0.0001)
197 d = 0.0001;
198 q *= pow(d, 1.0 / rcc->buffer_aggressivity);
199
200 q_limit = bits2qp(rce,
201 FFMAX((min_rate - buffer_size + rcc->buffer_index) *
202 s->c.avctx->rc_min_vbv_overflow_use, 1));
203
204 if (q > q_limit) {
205 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 if (max_rate) {
213 double d = 2 * expected_size / buffer_size;
214 if (d > 1.0)
215 d = 1.0;
216 else if (d < 0.0001)
217 d = 0.0001;
218 q /= pow(d, 1.0 / rcc->buffer_aggressivity);
219
220 q_limit = bits2qp(rce,
221 FFMAX(rcc->buffer_index *
222 s->c.avctx->rc_max_available_vbv_use,
223 1));
224 if (q < q_limit) {
225 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 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,
235 if (rcc->qsquish == 0.0 || qmin == qmax) {
236 if (q < qmin)
237 q = qmin;
238 else if (q > qmax)
239 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 return q;
254}
255
256/**
257 * Modify the bitrate curve from pass1 for one frame.
258 */
259static double get_qscale(MPVMainEncContext *const m, RateControlEntry *rce,
260 double rate_factor, int frame_num)
261{
262 MPVEncContext *const s = &m->s;
264 AVCodecContext *const avctx = s->c.avctx;
265 const int pict_type = rce->new_pict_type;
266 const double mb_num = s->c.mb_num;
267 double q, bits;
268 int i;
269
270 double const_values[] = {
271 M_PI,
272 M_E,
273 rce->i_tex_bits * rce->qscale,
274 rce->p_tex_bits * rce->qscale,
275 (rce->i_tex_bits + rce->p_tex_bits) * (double)rce->qscale,
276 rce->mv_bits / mb_num,
277 rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code) * 0.5 : rce->f_code,
278 rce->i_count / mb_num,
279 rce->mc_mb_var_sum / mb_num,
280 rce->mb_var_sum / mb_num,
284 rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
285 avctx->qcompress,
290 (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
291 0
292 };
293
295 if (isnan(bits)) {
296 av_log(avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", rcc->rc_eq);
297 return -1;
298 }
299
301 bits *= rate_factor;
302 if (bits < 0.0)
303 bits = 0.0;
304 bits += 1.0; // avoid 1/0 issues
305
306 /* user override */
307 for (i = 0; i < avctx->rc_override_count; i++) {
308 RcOverride *rco = avctx->rc_override;
309 if (rco[i].start_frame > frame_num)
310 continue;
311 if (rco[i].end_frame < frame_num)
312 continue;
313
314 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 q = bits2qp(rce, bits);
321
322 /* I/B difference */
323 if (pict_type == AV_PICTURE_TYPE_I && avctx->i_quant_factor < 0.0)
324 q = -q * avctx->i_quant_factor + avctx->i_quant_offset;
325 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 if (q < 1)
328 q = 1;
329
330 return q;
331}
332
333static 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 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 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 if (!qscale || !blurred_qscale) {
379 av_free(qscale);
380 av_free(blurred_qscale);
381 return AVERROR(ENOMEM);
382 }
383 toobig = 0;
384
385 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 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 av_assert0(filter_size % 2 == 1);
399
400 /* fixed I/B QP relative to P mode */
401 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 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 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 for (j = 0; j < filter_size; j++) {
421 int index = i + j - filter_size / 2;
422 double d = index - i;
423 double coeff = avctx->qblur == 0 ? 1.0 : exp(-d * d / (avctx->qblur * avctx->qblur));
424
426 continue;
427 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 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 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 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,
467 qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA,
468 avctx->qmin, avctx->qmax);
469 }
470 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 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 } 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 } 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
497{
498 MPVEncContext *const s = &m->s;
500 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) = {
528 NULL
529 };
530 static const char * const func1_names[] = {
531 "bits2qp",
532 "qp2bits",
533 NULL
534 };
535
536 if (!avctx->rc_max_available_vbv_use && avctx->rc_buffer_size) {
537 if (avctx->rc_max_rate) {
538 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 res = av_expr_parse(&rcc->rc_eq_eval,
544 rcc->rc_eq ? rcc->rc_eq : "tex^qComp",
546 NULL, NULL, 0, avctx);
547 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 for (i = 0; i < 5; i++) {
553 rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0;
554 rcc->pred[i].count = 1.0;
555 rcc->pred[i].decay = 0.4;
556
557 rcc->i_cplx_sum [i] =
558 rcc->p_cplx_sum [i] =
559 rcc->mv_bits_sum[i] =
560 rcc->qscale_sum [i] =
561 rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such
562
563 rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5;
564 }
566 if (!rcc->buffer_index)
567 rcc->buffer_index = avctx->rc_buffer_size * 3 / 4;
568
569 if (avctx->flags & AV_CODEC_FLAG_PASS2) {
570 int i;
571 char *p;
572
573 /* find number of pics */
574 p = avctx->stats_in;
575 for (i = -1; p; i++)
576 p = strchr(p + 1, ';');
577 i += m->max_b_frames;
578 if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry))
579 return -1;
580 rcc->entry = av_mallocz(i * sizeof(RateControlEntry));
581 if (!rcc->entry)
582 return AVERROR(ENOMEM);
583 rcc->num_entries = i;
584
585 /* init all to skipped P-frames
586 * (with B-frames we might have a not encoded frame at the end FIXME) */
587 for (i = 0; i < rcc->num_entries; i++) {
588 RateControlEntry *rce = &rcc->entry[i];
589
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 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 if (next) {
606 (*next) = 0; // sscanf is unbelievably slow on looong strings // FIXME copy / do not write
607 next++;
608 }
609 e = sscanf(p, " in:%d ", &picture_number);
610
611 av_assert0(picture_number >= 0);
612 av_assert0(picture_number < rcc->num_entries);
613 rce = &rcc->entry[picture_number];
614
615 e += sscanf(p, " in:%*d out:%*d type:%d q:%f itex:%d ptex:%d "
616 "mv:%d misc:%d "
617 "fcode:%d bcode:%d "
618 "mc-var:%"SCNd64" var:%"SCNd64" "
619 "icount:%d hbits:%d",
620 &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits,
621 &rce->mv_bits, &rce->misc_bits,
622 &rce->f_code, &rce->b_code,
623 &rce->mc_mb_var_sum, &rce->mb_var_sum,
624 &rce->i_count, &rce->header_bits);
625 if (e != 13) {
626 av_log(avctx, AV_LOG_ERROR,
627 "statistics are damaged at line %d, parser out=%d\n",
628 i, e);
629 return -1;
630 }
632 av_log(avctx, AV_LOG_ERROR, "invalid picture type %d at line %d\n",
633 rce->pict_type, i);
634 return -1;
635 }
636
637 p = next;
638 }
639
640 res = init_pass2(m);
641 if (res < 0)
642 return res;
643 }
644
645 if (!(avctx->flags & AV_CODEC_FLAG_PASS2)) {
646 rcc->short_term_qsum = 0.001;
647 rcc->short_term_qcount = 0.001;
648
649 rcc->pass1_rc_eq_output_sum = 0.001;
650 rcc->pass1_wanted_bits = 0.001;
651
652 if (avctx->qblur > 1.0) {
653 av_log(avctx, AV_LOG_ERROR, "qblur too large\n");
654 return -1;
655 }
656 /* init stuff with the user specified complexity */
657 if (rcc->initial_cplx) {
658 for (i = 0; i < 60 * 30; i++) {
659 double bits = rcc->initial_cplx * (i / 10000.0 + 1.0) * s->c.mb_num;
661
662 if (i % ((m->gop_size + 3) / 4) == 0)
664 else if (i % (m->max_b_frames + 1))
666 else
668
669 rce.new_pict_type = rce.pict_type;
670 rce.mc_mb_var_sum = bits * s->c.mb_num / 100000;
671 rce.mb_var_sum = s->c.mb_num;
672
673 rce.qscale = FF_QP2LAMBDA * 2;
674 rce.f_code = 2;
675 rce.b_code = 1;
676 rce.misc_bits = 1;
677
678 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
679 rce.i_count = s->c.mb_num;
680 rce.i_tex_bits = bits;
681 rce.p_tex_bits = 0;
682 rce.mv_bits = 0;
683 } else {
684 rce.i_count = 0; // FIXME we do know this approx
685 rce.i_tex_bits = 0;
686 rce.p_tex_bits = bits * 0.9;
687 rce.mv_bits = bits * 0.1;
688 }
689 rcc->i_cplx_sum[rce.pict_type] += rce.i_tex_bits * rce.qscale;
690 rcc->p_cplx_sum[rce.pict_type] += rce.p_tex_bits * rce.qscale;
691 rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits;
692 rcc->frame_count[rce.pict_type]++;
693
695
696 // FIXME misbehaves a little for variable fps
697 rcc->pass1_wanted_bits += m->bit_rate / get_fps(avctx);
698 }
699 }
700 }
701
702 if (s->adaptive_quant) {
703 unsigned mb_array_size = s->c.mb_stride * s->c.mb_height;
704
705 rcc->cplx_tab = av_malloc_array(mb_array_size, 2 * sizeof(*rcc->cplx_tab));
706 if (!rcc->cplx_tab)
707 return AVERROR(ENOMEM);
708 rcc->bits_tab = rcc->cplx_tab + mb_array_size;
709 }
710
711 return 0;
712}
713
715{
716 // rc_eq is always managed via an AVOption and therefore not freed here.
718 rcc->rc_eq_eval = NULL;
719 av_freep(&rcc->entry);
720 av_freep(&rcc->cplx_tab);
721}
722
724{
725 MPVEncContext *const s = &m->s;
726 RateControlContext *const rcc = &m->rc_context;
727 AVCodecContext *const avctx = s->c.avctx;
728 const double fps = get_fps(avctx);
729 const int buffer_size = avctx->rc_buffer_size;
730 const double min_rate = avctx->rc_min_rate / fps;
731 const double max_rate = avctx->rc_max_rate / fps;
732
733 ff_dlog(avctx, "%d %f %d %f %f\n",
734 buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate);
735
736 if (buffer_size) {
737 int left;
738
739 rcc->buffer_index -= frame_size;
740 if (rcc->buffer_index < 0) {
741 av_log(avctx, AV_LOG_ERROR, "rc buffer underflow\n");
742 if (frame_size > max_rate && s->c.qscale == avctx->qmax) {
743 av_log(avctx, AV_LOG_ERROR, "max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
744 }
745 rcc->buffer_index = 0;
746 }
747
748 left = buffer_size - rcc->buffer_index - 1;
749 rcc->buffer_index += av_clip(left, min_rate, max_rate);
750
751 if (rcc->buffer_index > buffer_size) {
752 int stuffing = ceil((rcc->buffer_index - buffer_size) / 8);
753
754 if (stuffing < 4 && s->c.codec_id == AV_CODEC_ID_MPEG4)
755 stuffing = 4;
756 rcc->buffer_index -= 8 * stuffing;
757
758 if (avctx->debug & FF_DEBUG_RC)
759 av_log(avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing);
760
761 return stuffing;
762 }
763 }
764 return 0;
765}
766
767static double predict_size(Predictor *p, double q, double var)
768{
769 return p->coeff * var / (q * p->count);
770}
771
772static void update_predictor(Predictor *p, double q, double var, double size)
773{
774 double new_coeff = size * q / (var + 1);
775 if (var < 10)
776 return;
777
778 p->count *= p->decay;
779 p->coeff *= p->decay;
780 p->count++;
781 p->coeff += new_coeff;
782}
783
785 MPVMainEncContext *const m, double q)
786{
787 MPVEncContext *const s = &m->s;
788 const float lumi_masking = s->c.avctx->lumi_masking / (128.0 * 128.0);
789 const float dark_masking = s->c.avctx->dark_masking / (128.0 * 128.0);
790 const float temp_cplx_masking = s->c.avctx->temporal_cplx_masking;
791 const float spatial_cplx_masking = s->c.avctx->spatial_cplx_masking;
792 const float p_masking = s->c.avctx->p_masking;
793 const float border_masking = m->border_masking;
794 float bits_sum = 0.0;
795 float cplx_sum = 0.0;
796 float *cplx_tab = rcc->cplx_tab;
797 float *bits_tab = rcc->bits_tab;
798 const int qmin = s->c.avctx->mb_lmin;
799 const int qmax = s->c.avctx->mb_lmax;
800 const int mb_width = s->c.mb_width;
801 const int mb_height = s->c.mb_height;
802
803 for (int i = 0; i < s->c.mb_num; i++) {
804 const int mb_xy = s->c.mb_index2xy[i];
805 float temp_cplx = sqrt(s->mc_mb_var[mb_xy]); // FIXME merge in pow()
806 float spat_cplx = sqrt(s->mb_var[mb_xy]);
807 const int lumi = s->mb_mean[mb_xy];
808 float bits, cplx, factor;
809 int mb_x = mb_xy % s->c.mb_stride;
810 int mb_y = mb_xy / s->c.mb_stride;
811 int mb_distance;
812 float mb_factor = 0.0;
813 if (spat_cplx < 4)
814 spat_cplx = 4; // FIXME fine-tune
815 if (temp_cplx < 4)
816 temp_cplx = 4; // FIXME fine-tune
817
818 if ((s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTRA)) { // FIXME hq mode
819 cplx = spat_cplx;
820 factor = 1.0 + p_masking;
821 } else {
822 cplx = temp_cplx;
823 factor = pow(temp_cplx, -temp_cplx_masking);
824 }
825 factor *= pow(spat_cplx, -spatial_cplx_masking);
826
827 if (lumi > 127)
828 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
829 else
830 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
831
832 if (mb_x < mb_width / 5) {
833 mb_distance = mb_width / 5 - mb_x;
834 mb_factor = (float)mb_distance / (float)(mb_width / 5);
835 } else if (mb_x > 4 * mb_width / 5) {
836 mb_distance = mb_x - 4 * mb_width / 5;
837 mb_factor = (float)mb_distance / (float)(mb_width / 5);
838 }
839 if (mb_y < mb_height / 5) {
840 mb_distance = mb_height / 5 - mb_y;
841 mb_factor = FFMAX(mb_factor,
842 (float)mb_distance / (float)(mb_height / 5));
843 } else if (mb_y > 4 * mb_height / 5) {
844 mb_distance = mb_y - 4 * mb_height / 5;
845 mb_factor = FFMAX(mb_factor,
846 (float)mb_distance / (float)(mb_height / 5));
847 }
848
849 factor *= 1.0 - border_masking * mb_factor;
850
851 if (factor < 0.00001)
852 factor = 0.00001;
853
854 bits = cplx * factor;
855 cplx_sum += cplx;
856 bits_sum += bits;
857 cplx_tab[i] = cplx;
858 bits_tab[i] = bits;
859 }
860
861 /* handle qmin/qmax clipping */
862 if (s->mpv_flags & FF_MPV_FLAG_NAQ) {
863 float factor = bits_sum / cplx_sum;
864 for (int i = 0; i < s->c.mb_num; i++) {
865 float newq = q * cplx_tab[i] / bits_tab[i];
866 newq *= factor;
867
868 if (newq > qmax) {
869 bits_sum -= bits_tab[i];
870 cplx_sum -= cplx_tab[i] * q / qmax;
871 } else if (newq < qmin) {
872 bits_sum -= bits_tab[i];
873 cplx_sum -= cplx_tab[i] * q / qmin;
874 }
875 }
876 if (bits_sum < 0.001)
877 bits_sum = 0.001;
878 if (cplx_sum < 0.001)
879 cplx_sum = 0.001;
880 }
881
882 for (int i = 0; i < s->c.mb_num; i++) {
883 const int mb_xy = s->c.mb_index2xy[i];
884 float newq = q * cplx_tab[i] / bits_tab[i];
885 int intq;
886
887 if (s->mpv_flags & FF_MPV_FLAG_NAQ) {
888 newq *= bits_sum / cplx_sum;
889 }
890
891 intq = (int)(newq + 0.5);
892
893 if (intq > qmax)
894 intq = qmax;
895 else if (intq < qmin)
896 intq = qmin;
897 s->lambda_table[mb_xy] = intq;
898 }
899}
900
902{
903 MPVEncContext *const s = &m->s;
904 const RateControlContext *rcc = &m->rc_context;
905 const RateControlEntry *rce = &rcc->entry[s->picture_number];
906
907 s->f_code = rce->f_code;
908 s->b_code = rce->b_code;
909}
910
911// FIXME rd or at least approx for dquant
912
913float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
914{
915 MPVEncContext *const s = &m->s;
917 AVCodecContext *const a = s->c.avctx;
918 float q;
919 int qmin, qmax;
920 float br_compensation;
921 double diff;
922 double short_term_q;
923 double fps;
924 int picture_number = s->picture_number;
925 int64_t wanted_bits;
926 RateControlEntry local_rce, *rce;
927 double bits;
928 double rate_factor;
929 int64_t var;
930 const int pict_type = s->c.pict_type;
931
932 get_qminmax(&qmin, &qmax, m, pict_type);
933
934 fps = get_fps(s->c.avctx);
935 /* update predictors */
936 if (picture_number > 2 && !dry_run) {
937 const int64_t last_var =
939 : rcc->last_mc_mb_var_sum;
942 rcc->last_qscale,
943 sqrt(last_var),
944 m->frame_bits - m->stuffing_bits);
945 }
946
947 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 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 if (s->c.pict_type == AV_PICTURE_TYPE_B || s->c.low_delay)
964 dts_pic = s->c.cur_pic.ptr;
965 else
966 dts_pic = s->c.last_pic.ptr;
967
968 if (!dts_pic || dts_pic->f->pts == AV_NOPTS_VALUE)
969 wanted_bits_double = m->bit_rate * (double)picture_number / fps;
970 else
971 wanted_bits_double = m->bit_rate * (double)dts_pic->f->pts / fps;
972 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 wanted_bits = (int64_t)wanted_bits_double;
977 }
978
979 diff = m->total_bits - wanted_bits;
980 br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance;
981 if (br_compensation <= 0.0)
982 br_compensation = 0.001;
983
984 var = pict_type == AV_PICTURE_TYPE_I ? m->mb_var_sum : m->mc_mb_var_sum;
985
986 short_term_q = 0; /* avoid warning */
987 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 rce->pict_type =
996 rce->new_pict_type = pict_type;
998 rce->mb_var_sum = m->mb_var_sum;
999 rce->qscale = FF_QP2LAMBDA * 2;
1000 rce->f_code = s->f_code;
1001 rce->b_code = s->b_code;
1002 rce->misc_bits = 1;
1003
1004 bits = predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
1005 if (pict_type == AV_PICTURE_TYPE_I) {
1006 rce->i_count = s->c.mb_num;
1007 rce->i_tex_bits = bits;
1008 rce->p_tex_bits = 0;
1009 rce->mv_bits = 0;
1010 } else {
1011 rce->i_count = 0; // FIXME we do know this approx
1012 rce->i_tex_bits = 0;
1013 rce->p_tex_bits = bits * 0.9;
1014 rce->mv_bits = bits * 0.1;
1015 }
1016 rcc->i_cplx_sum[pict_type] += rce->i_tex_bits * rce->qscale;
1017 rcc->p_cplx_sum[pict_type] += rce->p_tex_bits * rce->qscale;
1018 rcc->mv_bits_sum[pict_type] += rce->mv_bits;
1019 rcc->frame_count[pict_type]++;
1020
1021 rate_factor = rcc->pass1_wanted_bits /
1022 rcc->pass1_rc_eq_output_sum * br_compensation;
1023
1024 q = get_qscale(m, rce, rate_factor, picture_number);
1025 if (q < 0)
1026 return -1;
1027
1028 av_assert0(q > 0.0);
1029 q = get_diff_limited_q(m, rce, q);
1030 av_assert0(q > 0.0);
1031
1032 // FIXME type dependent blur like in 2-pass
1033 if (pict_type == AV_PICTURE_TYPE_P || m->intra_only) {
1034 rcc->short_term_qsum *= a->qblur;
1035 rcc->short_term_qcount *= a->qblur;
1036
1037 rcc->short_term_qsum += q;
1038 rcc->short_term_qcount++;
1039 q = short_term_q = rcc->short_term_qsum / rcc->short_term_qcount;
1040 }
1041 av_assert0(q > 0.0);
1042
1043 q = modify_qscale(m, rce, q, picture_number);
1044
1045 rcc->pass1_wanted_bits += m->bit_rate / fps;
1046
1047 av_assert0(q > 0.0);
1048 }
1049
1050 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,
1059 m->bit_rate / 1000, (int)fps);
1060 }
1061
1062 if (q < qmin)
1063 q = qmin;
1064 else if (q > qmax)
1065 q = qmax;
1066
1067 if (s->adaptive_quant)
1068 adaptive_quantization(rcc, m, q);
1069 else
1070 q = (int)(q + 0.5);
1071
1072 if (!dry_run) {
1073 rcc->last_qscale = q;
1075 rcc->last_mb_var_sum = m->mb_var_sum;
1076 }
1077 return q;
1078}
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_DEBUG_RC
Definition avcodec.h:1395
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define av_clip
Definition common.h:100
#define av_clipf
Definition common.h:145
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float ceil(float a)
static __device__ float fabs(float a)
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition eval.c:368
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition eval.c:824
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition eval.c:735
int8_t exp
Definition eval.c:76
simple arithmetic expression evaluator
static const uint8_t bits[8]
Definition fastaudio.c:100
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition avcodec.h:294
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
#define FF_LAMBDA_MAX
Definition avutil.h:227
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition avutil.h:226
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition utils.c:40
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
int index
Definition gxfenc.c:90
int a
static const int factor[16]
Definition vf_pp7.c:98
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
common internal API header
#define FFMAX(a, b)
Definition macros.h:47
#define M_E
Definition mathematics.h:37
#define M_PI
Definition mathematics.h:67
Memory handling functions.
mpegvideo header.
#define CANDIDATE_MB_TYPE_INTRA
#define FF_MPV_FLAG_NAQ
static double get_qscale(MPVMainEncContext *const m, RateControlEntry *rce, double rate_factor, int frame_num)
Modify the bitrate curve from pass1 for one frame.
static void update_predictor(Predictor *p, double q, double var, double size)
void ff_write_pass1_stats(MPVMainEncContext *const m)
Definition ratecontrol.c:37
static double get_fps(AVCodecContext *avctx)
Definition ratecontrol.c:67
static double qp2bits(const RateControlEntry *rce, double qp)
Definition ratecontrol.c:72
static double qp2bits_cb(void *rce, double qp)
Definition ratecontrol.c:80
static double predict_size(Predictor *p, double q, double var)
static AVRational get_fpsQ(AVCodecContext *avctx)
Definition ratecontrol.c:59
static void get_qminmax(int *qmin_ret, int *qmax_ret, MPVMainEncContext *const m, int pict_type)
Get the qmin & qmax for pict_type.
static void adaptive_quantization(RateControlContext *const rcc, MPVMainEncContext *const m, double q)
void ff_get_2pass_fcode(MPVMainEncContext *const m)
static double modify_qscale(MPVMainEncContext *const m, const RateControlEntry *rce, double q, int frame_num)
static double bits2qp(const RateControlEntry *rce, double bits)
Definition ratecontrol.c:85
av_cold void ff_rate_control_uninit(RateControlContext *rcc)
static int init_pass2(MPVMainEncContext *const m)
float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
int ff_vbv_update(MPVMainEncContext *m, int frame_size)
static double get_diff_limited_q(MPVMainEncContext *m, const RateControlEntry *rce, double q)
Definition ratecontrol.c:98
av_cold int ff_rate_control_init(MPVMainEncContext *const m)
static double bits2qp_cb(void *rce, double qp)
Definition ratecontrol.c:93
ratecontrol header.
main external API structure.
Definition avcodec.h:443
float rc_max_available_vbv_use
Ratecontrol attempt to use, at maximum, of what can be used without an underflow.
Definition avcodec.h:1302
int rc_buffer_size
decoder bitstream buffer size
Definition avcodec.h:1273
int debug
debug
Definition avcodec.h:1393
float b_quant_offset
qscale offset between IP and B-frames
Definition avcodec.h:797
int qmin
minimum quantizer
Definition avcodec.h:1252
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition avcodec.h:790
AVRational framerate
Definition avcodec.h:563
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition avcodec.h:1338
int rc_override_count
ratecontrol override, see RcOverride
Definition avcodec.h:1280
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition avcodec.h:1316
int64_t rc_max_rate
maximum bitrate
Definition avcodec.h:1288
int qmax
maximum quantizer
Definition avcodec.h:1259
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
int64_t rc_min_rate
minimum bitrate
Definition avcodec.h:1295
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition avcodec.h:1244
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition avcodec.h:1245
RcOverride * rc_override
Definition avcodec.h:1281
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition avcodec.h:806
float i_quant_offset
qscale offset between P and I-frames
Definition avcodec.h:813
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
int max_b_frames
max number of B-frames
int frame_bits
bits used for the current frame
int stuffing_bits
bits used for stuffing
MPVEncContext s
The main slicecontext.
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
int64_t mc_mb_var_sum
motion compensated MB variance for current frame
int64_t mb_var_sum
sum of MB variance for current frame
int intra_only
if true, only intra pictures are generated
MPVPicture.
Definition mpegpicture.h:58
struct AVFrame * f
Definition mpegpicture.h:59
double decay
Definition ratecontrol.h:36
double count
Definition ratecontrol.h:35
double coeff
Definition ratecontrol.h:34
rate control context.
Definition ratecontrol.h:60
float qsquish
ratecontrol qmin qmax limiting method 0-> clipping, 1-> use a nice continuous function to limit qscal...
Definition ratecontrol.h:84
Predictor pred[5]
Definition ratecontrol.h:64
struct AVExpr * rc_eq_eval
Definition ratecontrol.h:91
uint64_t qscale_sum[5]
Definition ratecontrol.h:76
uint64_t i_cplx_sum[5]
Definition ratecontrol.h:73
double short_term_qcount
count of recent qscales
Definition ratecontrol.h:66
RateControlEntry * entry
Definition ratecontrol.h:62
uint64_t p_cplx_sum[5]
Definition ratecontrol.h:74
double buffer_index
amount of bits in the video/audio buffer
Definition ratecontrol.h:63
int num_entries
number of RateControlEntries
Definition ratecontrol.h:61
double pass1_rc_eq_output_sum
sum of the output of the rc equation, this is used for normalization
Definition ratecontrol.h:67
int64_t last_mb_var_sum
Definition ratecontrol.h:72
double pass1_wanted_bits
bits which should have been output by the pass1 code (including complexity init)
Definition ratecontrol.h:68
uint64_t mv_bits_sum[5]
Definition ratecontrol.h:75
int64_t last_mc_mb_var_sum
Definition ratecontrol.h:71
double last_qscale_for[5]
last qscale for a specific pict type, used for max_diff & ipb factor stuff
Definition ratecontrol.h:70
double short_term_qsum
sum of recent qscales
Definition ratecontrol.h:65
uint64_t expected_bits
Definition ratecontrol.h:50
int64_t mc_mb_var_sum
Definition ratecontrol.h:53
float quality_factor
Definition avcodec.h:197
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
static const double const_values[]
Definition eval.c:28
static const char *const const_names[]
Definition eval.c:34
int size
static const double coeff[2][5]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double(*const func1[])(void *, double)
Definition vf_rotate.c:182
static const char *const func1_names[]
Definition vf_rotate.c:188
static double c[64]