2 * Rate control for video encoders
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
25 * Rate control for video encoders.
28 #include "libavutil/attributes.h"
30 #include "ratecontrol.h"
31 #include "mpegutils.h"
32 #include "mpegvideo.h"
33 #include "libavutil/eval.h"
35 #undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
39 #define M_E 2.718281828
42 static int init_pass2(MpegEncContext *s);
43 static double get_qscale(MpegEncContext *s, RateControlEntry *rce,
44 double rate_factor, int frame_num);
46 void ff_write_pass1_stats(MpegEncContext *s)
48 snprintf(s->avctx->stats_out, 256,
49 "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
50 "fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d;\n",
51 s->current_picture_ptr->f.display_picture_number,
52 s->current_picture_ptr->f.coded_picture_number,
54 s->current_picture.f.quality,
61 s->current_picture.mc_mb_var_sum,
62 s->current_picture.mb_var_sum,
63 s->i_count, s->skip_count,
67 static double get_fps(AVCodecContext *avctx)
69 return 1.0 / av_q2d(avctx->time_base) / FFMAX(avctx->ticks_per_frame, 1);
72 static inline double qp2bits(RateControlEntry *rce, double qp)
75 av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
77 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / qp;
80 static inline double bits2qp(RateControlEntry *rce, double bits)
83 av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
85 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / bits;
88 av_cold int ff_rate_control_init(MpegEncContext *s)
90 RateControlContext *rcc = &s->rc_context;
92 static const char * const const_names[] = {
121 static double (* const func1[])(void *, double) = {
126 static const char * const func1_names[] = {
133 if (!s->avctx->rc_max_available_vbv_use && s->avctx->rc_buffer_size) {
134 if (s->avctx->rc_max_rate) {
135 s->avctx->rc_max_available_vbv_use = av_clipf(s->avctx->rc_max_rate/(s->avctx->rc_buffer_size*get_fps(s->avctx)), 1.0/3, 1.0);
137 s->avctx->rc_max_available_vbv_use = 1.0;
140 res = av_expr_parse(&rcc->rc_eq_eval,
141 s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp",
142 const_names, func1_names, func1,
143 NULL, NULL, 0, s->avctx);
145 av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->avctx->rc_eq);
149 for (i = 0; i < 5; i++) {
150 rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0;
151 rcc->pred[i].count = 1.0;
152 rcc->pred[i].decay = 0.4;
154 rcc->i_cplx_sum [i] =
155 rcc->p_cplx_sum [i] =
156 rcc->mv_bits_sum[i] =
157 rcc->qscale_sum [i] =
158 rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such
160 rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5;
162 rcc->buffer_index = s->avctx->rc_initial_buffer_occupancy;
163 if (!rcc->buffer_index)
164 rcc->buffer_index = s->avctx->rc_buffer_size * 3 / 4;
166 if (s->flags & CODEC_FLAG_PASS2) {
170 /* find number of pics */
171 p = s->avctx->stats_in;
173 p = strchr(p + 1, ';');
174 i += s->max_b_frames;
175 if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry))
177 rcc->entry = av_mallocz(i * sizeof(RateControlEntry));
178 rcc->num_entries = i;
180 /* init all to skipped p frames
181 * (with b frames we might have a not encoded frame at the end FIXME) */
182 for (i = 0; i < rcc->num_entries; i++) {
183 RateControlEntry *rce = &rcc->entry[i];
185 rce->pict_type = rce->new_pict_type = AV_PICTURE_TYPE_P;
186 rce->qscale = rce->new_qscale = FF_QP2LAMBDA * 2;
187 rce->misc_bits = s->mb_num + 10;
188 rce->mb_var_sum = s->mb_num * 100;
192 p = s->avctx->stats_in;
193 for (i = 0; i < rcc->num_entries - s->max_b_frames; i++) {
194 RateControlEntry *rce;
199 next = strchr(p, ';');
201 (*next) = 0; // sscanf in unbelievably slow on looong strings // FIXME copy / do not write
204 e = sscanf(p, " in:%d ", &picture_number);
206 assert(picture_number >= 0);
207 assert(picture_number < rcc->num_entries);
208 rce = &rcc->entry[picture_number];
210 e += sscanf(p, " in:%*d out:%*d type:%d q:%f itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d",
211 &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits,
212 &rce->mv_bits, &rce->misc_bits,
213 &rce->f_code, &rce->b_code,
214 &rce->mc_mb_var_sum, &rce->mb_var_sum,
215 &rce->i_count, &rce->skip_count, &rce->header_bits);
217 av_log(s->avctx, AV_LOG_ERROR,
218 "statistics are damaged at line %d, parser out=%d\n",
226 if (init_pass2(s) < 0)
229 // FIXME maybe move to end
230 if ((s->flags & CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) {
232 return ff_xvid_rate_control_init(s);
234 av_log(s->avctx, AV_LOG_ERROR,
235 "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
241 if (!(s->flags & CODEC_FLAG_PASS2)) {
242 rcc->short_term_qsum = 0.001;
243 rcc->short_term_qcount = 0.001;
245 rcc->pass1_rc_eq_output_sum = 0.001;
246 rcc->pass1_wanted_bits = 0.001;
248 if (s->avctx->qblur > 1.0) {
249 av_log(s->avctx, AV_LOG_ERROR, "qblur too large\n");
252 /* init stuff with the user specified complexity */
253 if (s->avctx->rc_initial_cplx) {
254 for (i = 0; i < 60 * 30; i++) {
255 double bits = s->avctx->rc_initial_cplx * (i / 10000.0 + 1.0) * s->mb_num;
256 RateControlEntry rce;
258 if (i % ((s->gop_size + 3) / 4) == 0)
259 rce.pict_type = AV_PICTURE_TYPE_I;
260 else if (i % (s->max_b_frames + 1))
261 rce.pict_type = AV_PICTURE_TYPE_B;
263 rce.pict_type = AV_PICTURE_TYPE_P;
265 rce.new_pict_type = rce.pict_type;
266 rce.mc_mb_var_sum = bits * s->mb_num / 100000;
267 rce.mb_var_sum = s->mb_num;
269 rce.qscale = FF_QP2LAMBDA * 2;
274 if (s->pict_type == AV_PICTURE_TYPE_I) {
275 rce.i_count = s->mb_num;
276 rce.i_tex_bits = bits;
280 rce.i_count = 0; // FIXME we do know this approx
282 rce.p_tex_bits = bits * 0.9;
283 rce.mv_bits = bits * 0.1;
285 rcc->i_cplx_sum[rce.pict_type] += rce.i_tex_bits * rce.qscale;
286 rcc->p_cplx_sum[rce.pict_type] += rce.p_tex_bits * rce.qscale;
287 rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits;
288 rcc->frame_count[rce.pict_type]++;
290 get_qscale(s, &rce, rcc->pass1_wanted_bits / rcc->pass1_rc_eq_output_sum, i);
292 // FIXME misbehaves a little for variable fps
293 rcc->pass1_wanted_bits += s->bit_rate / get_fps(s->avctx);
301 av_cold void ff_rate_control_uninit(MpegEncContext *s)
303 RateControlContext *rcc = &s->rc_context;
306 av_expr_free(rcc->rc_eq_eval);
307 av_freep(&rcc->entry);
310 if ((s->flags & CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
311 ff_xvid_rate_control_uninit(s);
315 int ff_vbv_update(MpegEncContext *s, int frame_size)
317 RateControlContext *rcc = &s->rc_context;
318 const double fps = get_fps(s->avctx);
319 const int buffer_size = s->avctx->rc_buffer_size;
320 const double min_rate = s->avctx->rc_min_rate / fps;
321 const double max_rate = s->avctx->rc_max_rate / fps;
323 av_dlog(s, "%d %f %d %f %f\n",
324 buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate);
329 rcc->buffer_index -= frame_size;
330 if (rcc->buffer_index < 0) {
331 av_log(s->avctx, AV_LOG_ERROR, "rc buffer underflow\n");
332 if (frame_size > max_rate && s->qscale == s->avctx->qmax) {
333 av_log(s->avctx, AV_LOG_ERROR, "max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
335 rcc->buffer_index = 0;
338 left = buffer_size - rcc->buffer_index - 1;
339 rcc->buffer_index += av_clip(left, min_rate, max_rate);
341 if (rcc->buffer_index > buffer_size) {
342 int stuffing = ceil((rcc->buffer_index - buffer_size) / 8);
344 if (stuffing < 4 && s->codec_id == AV_CODEC_ID_MPEG4)
346 rcc->buffer_index -= 8 * stuffing;
348 if (s->avctx->debug & FF_DEBUG_RC)
349 av_log(s->avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing);
358 * Modify the bitrate curve from pass1 for one frame.
360 static double get_qscale(MpegEncContext *s, RateControlEntry *rce,
361 double rate_factor, int frame_num)
363 RateControlContext *rcc = &s->rc_context;
364 AVCodecContext *a = s->avctx;
365 const int pict_type = rce->new_pict_type;
366 const double mb_num = s->mb_num;
370 double const_values[] = {
373 rce->i_tex_bits * rce->qscale,
374 rce->p_tex_bits * rce->qscale,
375 (rce->i_tex_bits + rce->p_tex_bits) * (double)rce->qscale,
376 rce->mv_bits / mb_num,
377 rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code) * 0.5 : rce->f_code,
378 rce->i_count / mb_num,
379 rce->mc_mb_var_sum / mb_num,
380 rce->mb_var_sum / mb_num,
381 rce->pict_type == AV_PICTURE_TYPE_I,
382 rce->pict_type == AV_PICTURE_TYPE_P,
383 rce->pict_type == AV_PICTURE_TYPE_B,
384 rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
387 rcc->last_qscale_for[AV_PICTURE_TYPE_I],
388 rcc->last_qscale_for[AV_PICTURE_TYPE_P],
389 rcc->last_qscale_for[AV_PICTURE_TYPE_B],
390 rcc->next_non_b_qscale,
392 rcc->i_cplx_sum[AV_PICTURE_TYPE_I] / (double)rcc->frame_count[AV_PICTURE_TYPE_I],
393 rcc->i_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P],
394 rcc->p_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P],
395 rcc->p_cplx_sum[AV_PICTURE_TYPE_B] / (double)rcc->frame_count[AV_PICTURE_TYPE_B],
396 (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
400 bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);
402 av_log(s->avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", s->avctx->rc_eq);
406 rcc->pass1_rc_eq_output_sum += bits;
410 bits += 1.0; // avoid 1/0 issues
413 for (i = 0; i < s->avctx->rc_override_count; i++) {
414 RcOverride *rco = s->avctx->rc_override;
415 if (rco[i].start_frame > frame_num)
417 if (rco[i].end_frame < frame_num)
421 bits = qp2bits(rce, rco[i].qscale); // FIXME move at end to really force it?
423 bits *= rco[i].quality_factor;
426 q = bits2qp(rce, bits);
429 if (pict_type == AV_PICTURE_TYPE_I && s->avctx->i_quant_factor < 0.0)
430 q = -q * s->avctx->i_quant_factor + s->avctx->i_quant_offset;
431 else if (pict_type == AV_PICTURE_TYPE_B && s->avctx->b_quant_factor < 0.0)
432 q = -q * s->avctx->b_quant_factor + s->avctx->b_quant_offset;
439 static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q)
441 RateControlContext *rcc = &s->rc_context;
442 AVCodecContext *a = s->avctx;
443 const int pict_type = rce->new_pict_type;
444 const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P];
445 const double last_non_b_q = rcc->last_qscale_for[rcc->last_non_b_pict_type];
447 if (pict_type == AV_PICTURE_TYPE_I &&
448 (a->i_quant_factor > 0.0 || rcc->last_non_b_pict_type == AV_PICTURE_TYPE_P))
449 q = last_p_q * FFABS(a->i_quant_factor) + a->i_quant_offset;
450 else if (pict_type == AV_PICTURE_TYPE_B &&
451 a->b_quant_factor > 0.0)
452 q = last_non_b_q * a->b_quant_factor + a->b_quant_offset;
456 /* last qscale / qdiff stuff */
457 if (rcc->last_non_b_pict_type == pict_type || pict_type != AV_PICTURE_TYPE_I) {
458 double last_q = rcc->last_qscale_for[pict_type];
459 const int maxdiff = FF_QP2LAMBDA * a->max_qdiff;
461 if (q > last_q + maxdiff)
462 q = last_q + maxdiff;
463 else if (q < last_q - maxdiff)
464 q = last_q - maxdiff;
467 rcc->last_qscale_for[pict_type] = q; // Note we cannot do that after blurring
469 if (pict_type != AV_PICTURE_TYPE_B)
470 rcc->last_non_b_pict_type = pict_type;
476 * Get the qmin & qmax for pict_type.
478 static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type)
480 int qmin = s->avctx->lmin;
481 int qmax = s->avctx->lmax;
483 assert(qmin <= qmax);
486 case AV_PICTURE_TYPE_B:
487 qmin = (int)(qmin * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset + 0.5);
488 qmax = (int)(qmax * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset + 0.5);
490 case AV_PICTURE_TYPE_I:
491 qmin = (int)(qmin * FFABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset + 0.5);
492 qmax = (int)(qmax * FFABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset + 0.5);
496 qmin = av_clip(qmin, 1, FF_LAMBDA_MAX);
497 qmax = av_clip(qmax, 1, FF_LAMBDA_MAX);
506 static double modify_qscale(MpegEncContext *s, RateControlEntry *rce,
507 double q, int frame_num)
509 RateControlContext *rcc = &s->rc_context;
510 const double buffer_size = s->avctx->rc_buffer_size;
511 const double fps = get_fps(s->avctx);
512 const double min_rate = s->avctx->rc_min_rate / fps;
513 const double max_rate = s->avctx->rc_max_rate / fps;
514 const int pict_type = rce->new_pict_type;
517 get_qminmax(&qmin, &qmax, s, pict_type);
520 if (s->avctx->rc_qmod_freq &&
521 frame_num % s->avctx->rc_qmod_freq == 0 &&
522 pict_type == AV_PICTURE_TYPE_P)
523 q *= s->avctx->rc_qmod_amp;
525 /* buffer overflow/underflow protection */
527 double expected_size = rcc->buffer_index;
531 double d = 2 * (buffer_size - expected_size) / buffer_size;
536 q *= pow(d, 1.0 / s->avctx->rc_buffer_aggressivity);
538 q_limit = bits2qp(rce,
539 FFMAX((min_rate - buffer_size + rcc->buffer_index) *
540 s->avctx->rc_min_vbv_overflow_use, 1));
543 if (s->avctx->debug & FF_DEBUG_RC)
544 av_log(s->avctx, AV_LOG_DEBUG,
545 "limiting QP %f -> %f\n", q, q_limit);
551 double d = 2 * expected_size / buffer_size;
556 q /= pow(d, 1.0 / s->avctx->rc_buffer_aggressivity);
558 q_limit = bits2qp(rce,
559 FFMAX(rcc->buffer_index *
560 s->avctx->rc_max_available_vbv_use,
563 if (s->avctx->debug & FF_DEBUG_RC)
564 av_log(s->avctx, AV_LOG_DEBUG,
565 "limiting QP %f -> %f\n", q, q_limit);
570 av_dlog(s, "q:%f max:%f min:%f size:%f index:%f agr:%f\n",
571 q, max_rate, min_rate, buffer_size, rcc->buffer_index,
572 s->avctx->rc_buffer_aggressivity);
573 if (s->avctx->rc_qsquish == 0.0 || qmin == qmax) {
579 double min2 = log(qmin);
580 double max2 = log(qmax);
583 q = (q - min2) / (max2 - min2) - 0.5;
585 q = 1.0 / (1.0 + exp(q));
586 q = q * (max2 - min2) + min2;
594 // ----------------------------------
597 static double predict_size(Predictor *p, double q, double var)
599 return p->coeff * var / (q * p->count);
602 static void update_predictor(Predictor *p, double q, double var, double size)
604 double new_coeff = size * q / (var + 1);
608 p->count *= p->decay;
609 p->coeff *= p->decay;
611 p->coeff += new_coeff;
614 static void adaptive_quantization(MpegEncContext *s, double q)
617 const float lumi_masking = s->avctx->lumi_masking / (128.0 * 128.0);
618 const float dark_masking = s->avctx->dark_masking / (128.0 * 128.0);
619 const float temp_cplx_masking = s->avctx->temporal_cplx_masking;
620 const float spatial_cplx_masking = s->avctx->spatial_cplx_masking;
621 const float p_masking = s->avctx->p_masking;
622 const float border_masking = s->avctx->border_masking;
623 float bits_sum = 0.0;
624 float cplx_sum = 0.0;
625 float *cplx_tab = s->cplx_tab;
626 float *bits_tab = s->bits_tab;
627 const int qmin = s->avctx->mb_lmin;
628 const int qmax = s->avctx->mb_lmax;
629 Picture *const pic = &s->current_picture;
630 const int mb_width = s->mb_width;
631 const int mb_height = s->mb_height;
633 for (i = 0; i < s->mb_num; i++) {
634 const int mb_xy = s->mb_index2xy[i];
635 float temp_cplx = sqrt(pic->mc_mb_var[mb_xy]); // FIXME merge in pow()
636 float spat_cplx = sqrt(pic->mb_var[mb_xy]);
637 const int lumi = pic->mb_mean[mb_xy];
638 float bits, cplx, factor;
639 int mb_x = mb_xy % s->mb_stride;
640 int mb_y = mb_xy / s->mb_stride;
642 float mb_factor = 0.0;
644 spat_cplx = 4; // FIXME finetune
646 temp_cplx = 4; // FIXME finetune
648 if ((s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTRA)) { // FIXME hq mode
650 factor = 1.0 + p_masking;
653 factor = pow(temp_cplx, -temp_cplx_masking);
655 factor *= pow(spat_cplx, -spatial_cplx_masking);
658 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
660 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
662 if (mb_x < mb_width / 5) {
663 mb_distance = mb_width / 5 - mb_x;
664 mb_factor = (float)mb_distance / (float)(mb_width / 5);
665 } else if (mb_x > 4 * mb_width / 5) {
666 mb_distance = mb_x - 4 * mb_width / 5;
667 mb_factor = (float)mb_distance / (float)(mb_width / 5);
669 if (mb_y < mb_height / 5) {
670 mb_distance = mb_height / 5 - mb_y;
671 mb_factor = FFMAX(mb_factor,
672 (float)mb_distance / (float)(mb_height / 5));
673 } else if (mb_y > 4 * mb_height / 5) {
674 mb_distance = mb_y - 4 * mb_height / 5;
675 mb_factor = FFMAX(mb_factor,
676 (float)mb_distance / (float)(mb_height / 5));
679 factor *= 1.0 - border_masking * mb_factor;
681 if (factor < 0.00001)
684 bits = cplx * factor;
691 /* handle qmin/qmax clipping */
692 if (s->flags & CODEC_FLAG_NORMALIZE_AQP) {
693 float factor = bits_sum / cplx_sum;
694 for (i = 0; i < s->mb_num; i++) {
695 float newq = q * cplx_tab[i] / bits_tab[i];
699 bits_sum -= bits_tab[i];
700 cplx_sum -= cplx_tab[i] * q / qmax;
701 } else if (newq < qmin) {
702 bits_sum -= bits_tab[i];
703 cplx_sum -= cplx_tab[i] * q / qmin;
706 if (bits_sum < 0.001)
708 if (cplx_sum < 0.001)
712 for (i = 0; i < s->mb_num; i++) {
713 const int mb_xy = s->mb_index2xy[i];
714 float newq = q * cplx_tab[i] / bits_tab[i];
717 if (s->flags & CODEC_FLAG_NORMALIZE_AQP) {
718 newq *= bits_sum / cplx_sum;
721 intq = (int)(newq + 0.5);
725 else if (intq < qmin)
727 s->lambda_table[mb_xy] = intq;
731 void ff_get_2pass_fcode(MpegEncContext *s)
733 RateControlContext *rcc = &s->rc_context;
734 RateControlEntry *rce = &rcc->entry[s->picture_number];
736 s->f_code = rce->f_code;
737 s->b_code = rce->b_code;
740 // FIXME rd or at least approx for dquant
742 float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
746 float br_compensation;
750 int picture_number = s->picture_number;
752 RateControlContext *rcc = &s->rc_context;
753 AVCodecContext *a = s->avctx;
754 RateControlEntry local_rce, *rce;
758 const int pict_type = s->pict_type;
759 Picture * const pic = &s->current_picture;
763 if ((s->flags & CODEC_FLAG_PASS2) &&
764 s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
765 return ff_xvid_rate_estimate_qscale(s, dry_run);
768 get_qminmax(&qmin, &qmax, s, pict_type);
770 fps = get_fps(s->avctx);
771 /* update predictors */
772 if (picture_number > 2 && !dry_run) {
773 const int last_var = s->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum
774 : rcc->last_mc_mb_var_sum;
775 av_assert1(s->frame_bits >= s->stuffing_bits);
776 update_predictor(&rcc->pred[s->last_pict_type],
779 s->frame_bits - s->stuffing_bits);
782 if (s->flags & CODEC_FLAG_PASS2) {
783 assert(picture_number >= 0);
784 if (picture_number >= rcc->num_entries) {
785 av_log(s, AV_LOG_ERROR, "Input is longer than 2-pass log file\n");
788 rce = &rcc->entry[picture_number];
789 wanted_bits = rce->expected_bits;
794 /* FIXME add a dts field to AVFrame and ensure it is set and use it
795 * here instead of reordering but the reordering is simpler for now
796 * until H.264 B-pyramid must be handled. */
797 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
798 dts_pic = s->current_picture_ptr;
800 dts_pic = s->last_picture_ptr;
802 if (!dts_pic || dts_pic->f.pts == AV_NOPTS_VALUE)
803 wanted_bits = (uint64_t)(s->bit_rate * (double)picture_number / fps);
805 wanted_bits = (uint64_t)(s->bit_rate * (double)dts_pic->f.pts / fps);
808 diff = s->total_bits - wanted_bits;
809 br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance;
810 if (br_compensation <= 0.0)
811 br_compensation = 0.001;
813 var = pict_type == AV_PICTURE_TYPE_I ? pic->mb_var_sum : pic->mc_mb_var_sum;
815 short_term_q = 0; /* avoid warning */
816 if (s->flags & CODEC_FLAG_PASS2) {
817 if (pict_type != AV_PICTURE_TYPE_I)
818 assert(pict_type == rce->new_pict_type);
820 q = rce->new_qscale / br_compensation;
821 av_dlog(s, "%f %f %f last:%d var:%d type:%d//\n", q, rce->new_qscale,
822 br_compensation, s->frame_bits, var, pict_type);
825 rce->new_pict_type = pict_type;
826 rce->mc_mb_var_sum = pic->mc_mb_var_sum;
827 rce->mb_var_sum = pic->mb_var_sum;
828 rce->qscale = FF_QP2LAMBDA * 2;
829 rce->f_code = s->f_code;
830 rce->b_code = s->b_code;
833 bits = predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
834 if (pict_type == AV_PICTURE_TYPE_I) {
835 rce->i_count = s->mb_num;
836 rce->i_tex_bits = bits;
840 rce->i_count = 0; // FIXME we do know this approx
842 rce->p_tex_bits = bits * 0.9;
843 rce->mv_bits = bits * 0.1;
845 rcc->i_cplx_sum[pict_type] += rce->i_tex_bits * rce->qscale;
846 rcc->p_cplx_sum[pict_type] += rce->p_tex_bits * rce->qscale;
847 rcc->mv_bits_sum[pict_type] += rce->mv_bits;
848 rcc->frame_count[pict_type]++;
850 bits = rce->i_tex_bits + rce->p_tex_bits;
851 rate_factor = rcc->pass1_wanted_bits /
852 rcc->pass1_rc_eq_output_sum * br_compensation;
854 q = get_qscale(s, rce, rate_factor, picture_number);
859 q = get_diff_limited_q(s, rce, q);
862 // FIXME type dependent blur like in 2-pass
863 if (pict_type == AV_PICTURE_TYPE_P || s->intra_only) {
864 rcc->short_term_qsum *= a->qblur;
865 rcc->short_term_qcount *= a->qblur;
867 rcc->short_term_qsum += q;
868 rcc->short_term_qcount++;
869 q = short_term_q = rcc->short_term_qsum / rcc->short_term_qcount;
873 q = modify_qscale(s, rce, q, picture_number);
875 rcc->pass1_wanted_bits += s->bit_rate / fps;
880 if (s->avctx->debug & FF_DEBUG_RC) {
881 av_log(s->avctx, AV_LOG_DEBUG,
882 "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f "
883 "size:%d var:%d/%d br:%d fps:%d\n",
884 av_get_picture_type_char(pict_type),
885 qmin, q, qmax, picture_number,
886 (int)wanted_bits / 1000, (int)s->total_bits / 1000,
887 br_compensation, short_term_q, s->frame_bits,
888 pic->mb_var_sum, pic->mc_mb_var_sum,
889 s->bit_rate / 1000, (int)fps);
897 if (s->adaptive_quant)
898 adaptive_quantization(s, q);
903 rcc->last_qscale = q;
904 rcc->last_mc_mb_var_sum = pic->mc_mb_var_sum;
905 rcc->last_mb_var_sum = pic->mb_var_sum;
910 // ----------------------------------------------
913 static int init_pass2(MpegEncContext *s)
915 RateControlContext *rcc = &s->rc_context;
916 AVCodecContext *a = s->avctx;
918 double fps = get_fps(s->avctx);
919 double complexity[5] = { 0 }; // approximate bits at quant=1
920 uint64_t const_bits[5] = { 0 }; // quantizer independent bits
921 uint64_t all_const_bits;
922 uint64_t all_available_bits = (uint64_t)(s->bit_rate *
923 (double)rcc->num_entries / fps);
924 double rate_factor = 0;
926 const int filter_size = (int)(a->qblur * 4) | 1;
927 double expected_bits = 0; // init to silence gcc warning
928 double *qscale, *blurred_qscale, qscale_sum;
930 /* find complexity & const_bits & decide the pict_types */
931 for (i = 0; i < rcc->num_entries; i++) {
932 RateControlEntry *rce = &rcc->entry[i];
934 rce->new_pict_type = rce->pict_type;
935 rcc->i_cplx_sum[rce->pict_type] += rce->i_tex_bits * rce->qscale;
936 rcc->p_cplx_sum[rce->pict_type] += rce->p_tex_bits * rce->qscale;
937 rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits;
938 rcc->frame_count[rce->pict_type]++;
940 complexity[rce->new_pict_type] += (rce->i_tex_bits + rce->p_tex_bits) *
942 const_bits[rce->new_pict_type] += rce->mv_bits + rce->misc_bits;
945 all_const_bits = const_bits[AV_PICTURE_TYPE_I] +
946 const_bits[AV_PICTURE_TYPE_P] +
947 const_bits[AV_PICTURE_TYPE_B];
949 if (all_available_bits < all_const_bits) {
950 av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is too low\n");
954 qscale = av_malloc(sizeof(double) * rcc->num_entries);
955 blurred_qscale = av_malloc(sizeof(double) * rcc->num_entries);
958 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
962 rcc->buffer_index = s->avctx->rc_buffer_size / 2;
965 for (i = 0; i < rcc->num_entries; i++) {
966 RateControlEntry *rce = &rcc->entry[i];
968 qscale[i] = get_qscale(s, &rcc->entry[i], rate_factor, i);
969 rcc->last_qscale_for[rce->pict_type] = qscale[i];
971 assert(filter_size % 2 == 1);
973 /* fixed I/B QP relative to P mode */
974 for (i = FFMAX(0, rcc->num_entries - 300); i < rcc->num_entries; i++) {
975 RateControlEntry *rce = &rcc->entry[i];
977 qscale[i] = get_diff_limited_q(s, rce, qscale[i]);
980 for (i = rcc->num_entries - 1; i >= 0; i--) {
981 RateControlEntry *rce = &rcc->entry[i];
983 qscale[i] = get_diff_limited_q(s, rce, qscale[i]);
987 for (i = 0; i < rcc->num_entries; i++) {
988 RateControlEntry *rce = &rcc->entry[i];
989 const int pict_type = rce->new_pict_type;
991 double q = 0.0, sum = 0.0;
993 for (j = 0; j < filter_size; j++) {
994 int index = i + j - filter_size / 2;
995 double d = index - i;
996 double coeff = a->qblur == 0 ? 1.0 : exp(-d * d / (a->qblur * a->qblur));
998 if (index < 0 || index >= rcc->num_entries)
1000 if (pict_type != rcc->entry[index].new_pict_type)
1002 q += qscale[index] * coeff;
1005 blurred_qscale[i] = q / sum;
1008 /* find expected bits */
1009 for (i = 0; i < rcc->num_entries; i++) {
1010 RateControlEntry *rce = &rcc->entry[i];
1013 rce->new_qscale = modify_qscale(s, rce, blurred_qscale[i], i);
1015 bits = qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits;
1016 bits += 8 * ff_vbv_update(s, bits);
1018 rce->expected_bits = expected_bits;
1019 expected_bits += bits;
1023 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
1024 expected_bits, (int)all_available_bits, rate_factor);
1025 if (expected_bits > all_available_bits) {
1026 rate_factor -= step;
1031 av_free(blurred_qscale);
1033 /* check bitrate calculations and print info */
1035 for (i = 0; i < rcc->num_entries; i++) {
1036 av_dlog(s, "[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
1038 rcc->entry[i].new_qscale,
1039 rcc->entry[i].new_qscale / FF_QP2LAMBDA);
1040 qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA,
1041 s->avctx->qmin, s->avctx->qmax);
1043 assert(toobig <= 40);
1044 av_log(s->avctx, AV_LOG_DEBUG,
1045 "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n",
1047 (int)(expected_bits / ((double)all_available_bits / s->bit_rate)));
1048 av_log(s->avctx, AV_LOG_DEBUG,
1049 "[lavc rc] estimated target average qp: %.3f\n",
1050 (float)qscale_sum / rcc->num_entries);
1052 av_log(s->avctx, AV_LOG_INFO,
1053 "[lavc rc] Using all of requested bitrate is not "
1054 "necessary for this video with these parameters.\n");
1055 } else if (toobig == 40) {
1056 av_log(s->avctx, AV_LOG_ERROR,
1057 "[lavc rc] Error: bitrate too low for this video "
1058 "with these parameters.\n");
1060 } else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
1061 av_log(s->avctx, AV_LOG_ERROR,
1062 "[lavc rc] Error: 2pass curve failed to converge\n");