Coverage Report

Created: 2024-09-06 07:53

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