LCOV - code coverage report
Current view: top level - libavcodec - ratecontrol.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 312 584 53.4 %
Date: 2017-12-14 19:11:59 Functions: 13 17 76.5 %

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

Generated by: LCOV version 1.13