Coverage Report

Created: 2026-05-16 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Codec/pass2_strategy.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2019, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 3-Clause Clear License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at https://www.aomedia.org/license. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
10
 */
11
#include <stdint.h>
12
13
#include "definitions.h"
14
#include "pass2_strategy.h"
15
#include "rc_process.h"
16
#include "firstpass.h"
17
#include "sequence_control_set.h"
18
#include "entropy_coding.h"
19
#include "pd_process.h"
20
21
// Calculate a modified Error used in distributing bits between easier and
22
// harder frames.
23
static double calculate_modified_err(TWO_PASS* twopass, const FIRSTPASS_STATS* this_frame) {
24
    FIRSTPASS_STATS* stats = twopass->stats_buf_ctx->total_stats;
25
    if (stats == NULL) {
26
        return 0;
27
    }
28
    return (double)this_frame->stat_struct.total_num_bits;
29
}
30
31
// Resets the first pass file to the given position using a relative seek from
32
// the current position.
33
static void reset_fpf_position(TWO_PASS* p, const FIRSTPASS_STATS* position) {
34
    p->stats_in = position;
35
}
36
37
static int input_stats(TWO_PASS* p, FIRSTPASS_STATS* fps) {
38
    if (p->stats_in >= p->stats_buf_ctx->stats_in_end) {
39
        return EOF;
40
    }
41
42
    *fps = *p->stats_in;
43
    ++p->stats_in;
44
    return 1;
45
}
46
47
0
static void subtract_stats(FIRSTPASS_STATS* section, FIRSTPASS_STATS* frame) {
48
0
    section->frame -= frame->frame;
49
0
    section->coded_error -= frame->coded_error;
50
0
    section->count -= frame->count;
51
0
    section->duration -= frame->duration;
52
0
}
53
54
// This function returns the maximum target rate per frame.
55
static int frame_max_bits(RATE_CONTROL* rc, EncodeContext* enc_ctx) {
56
    int64_t max_bits = (int64_t)rc->avg_frame_bandwidth * enc_ctx->two_pass_cfg.vbrmax_section / 100;
57
    return (int)CLIP3(0, rc->max_frame_bandwidth, max_bits);
58
}
59
60
static const double q_pow_term[(QINDEX_RANGE >> 5) + 1] = {0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 0.95, 0.95};
61
#define ERR_DIVISOR 96.0
62
63
static double calc_correction_factor(double err_per_mb, int q) {
64
    double error_term = err_per_mb / ERR_DIVISOR;
65
    int    index      = q >> 5;
66
    // Adjustment to power term based on qindex
67
    double power_term = q_pow_term[index] + (((q_pow_term[index + 1] - q_pow_term[index]) * (q % 32)) / 32.0);
68
    assert(error_term >= 0.0);
69
    return fclamp(pow(error_term, power_term), 0.05, 5.0);
70
}
71
72
static int qbpm_enumerator(int rate_err_tol) {
73
    return 1250000 + ((300000 * AOMMIN(75, AOMMAX(rate_err_tol - 25, 0))) / 75);
74
}
75
76
// Similar to find_qindex_by_rate() function in ratectrl.c, but includes
77
// calculation of a correction_factor.
78
static int find_qindex_by_rate_with_correction(int desired_bits_per_mb, EbBitDepth bit_depth, double error_per_mb,
79
                                               double group_weight_factor, int rate_err_tol, int best_qindex,
80
                                               int worst_qindex) {
81
    assert(best_qindex <= worst_qindex);
82
    int low  = best_qindex;
83
    int high = worst_qindex;
84
85
    while (low < high) {
86
        int    mid             = (low + high) >> 1;
87
        double mid_factor      = calc_correction_factor(error_per_mb, mid);
88
        double q               = svt_av1_convert_qindex_to_q(mid, bit_depth);
89
        int    enumerator      = qbpm_enumerator(rate_err_tol);
90
        int    mid_bits_per_mb = (int)((enumerator * mid_factor * group_weight_factor) / q);
91
92
        if (mid_bits_per_mb > desired_bits_per_mb) {
93
            low = mid + 1;
94
        } else {
95
            high = mid;
96
        }
97
    }
98
    return low;
99
}
100
101
/*!\brief Choose a target maximum Q for a group of frames
102
 *
103
 * \ingroup rate_control
104
 *
105
 * This function is used to estimate a suitable maximum Q for a
106
 * group of frames. Inititally it is called to get a crude estimate
107
 * for the whole clip. It is then called for each ARF/GF group to get
108
 * a revised estimate for that group.
109
 *
110
 * \param[in]    cpi                 Top-level encoder structure
111
 * \param[in]    av_frame_err        The average per frame coded error score
112
 *                                   for frames making up this section/group.
113
 * \param[in]    inactive_zone       Used to mask off /ignore part of the
114
 *                                   frame. The most common use case is where
115
 *                                   a wide format video (e.g. 16:9) is
116
 *                                   letter-boxed into a more square format.
117
 *                                   Here we want to ignore the bands at the
118
 *                                   top and bottom.
119
 * \param[in]    av_target_bandwidth The target bits per frame
120
 * \param[in]    group_weight_factor A correction factor allowing the algorithm
121
 *                                   to correct for errors over time.
122
 *
123
 * \return The maximum Q for frames in the group.
124
 */
125
static int get_twopass_worst_quality(PictureParentControlSet* pcs, double section_err, double inactive_zone,
126
                                     int section_target_bandwidth, double group_weight_factor) {
127
    SequenceControlSet* scs     = pcs->scs;
128
    EncodeContext*      enc_ctx = scs->enc_ctx;
129
    RATE_CONTROL*       rc      = &enc_ctx->rc;
130
    RateControlCfg*     rc_cfg  = &enc_ctx->rc_cfg;
131
    uint32_t            mb_cols;
132
    uint32_t            mb_rows;
133
    if (scs->first_pass_downsample) {
134
        mb_cols = 2 * (scs->max_input_luma_width + 16 - 1) / 16;
135
        mb_rows = 2 * (scs->max_input_luma_height + 16 - 1) / 16;
136
    } else {
137
        mb_cols = (scs->max_input_luma_width + 16 - 1) / 16;
138
        mb_rows = (scs->max_input_luma_height + 16 - 1) / 16;
139
    }
140
    inactive_zone = fclamp(inactive_zone, 0.0, 1.0);
141
142
    if (section_target_bandwidth <= 0) {
143
        return rc->worst_quality; // Highest value allowed
144
    }
145
146
    int num_mbs = mb_cols * mb_rows;
147
    //(oxcf->resize_cfg.resize_mode != RESIZE_NONE)
148
    //    ? cpi->initial_mbs
149
    //    : cpi->common.mi_params.MBs;
150
    int    active_mbs              = AOMMAX(1, num_mbs - (int)(num_mbs * inactive_zone));
151
    double av_err_per_mb           = section_err / active_mbs;
152
    int    target_norm_bits_per_mb = (int)(((uint64_t)section_target_bandwidth << BPER_MB_NORMBITS) / active_mbs);
153
154
    int rate_err_tol = AOMMIN(rc_cfg->under_shoot_pct, rc_cfg->over_shoot_pct);
155
156
    // Try and pick a max Q that will be high enough to encode the
157
    // content at the given rate.
158
    int q = find_qindex_by_rate_with_correction(target_norm_bits_per_mb,
159
                                                scs->encoder_bit_depth,
160
                                                av_err_per_mb,
161
                                                group_weight_factor,
162
                                                rate_err_tol,
163
                                                rc->best_quality,
164
                                                rc->worst_quality);
165
    return q;
166
}
167
168
static void accumulate_this_frame_stats(FIRSTPASS_STATS* stats, double mod_frame_err, GF_GROUP_STATS* gf_stats) {
169
    gf_stats->gf_group_err += mod_frame_err;
170
    gf_stats->gf_group_raw_error += stats->coded_error;
171
}
172
173
// Calculate the total bits to allocate in this GF/ARF group.
174
/*!\brief Calculates the bit target for this GF/ARF group
175
 *
176
 * \ingroup rate_control
177
 *
178
 * Calculates the total bits to allocate in this GF/ARF group.
179
 *
180
 * \param[in]    cpi              Top-level encoder structure
181
 * \param[in]    gf_group_err     Cumulative coded error score for the
182
 *                                frames making up this group.
183
 *
184
 * \return The target total number of bits for this GF/ARF group.
185
 */
186
static int64_t calculate_total_gf_group_bits(PictureParentControlSet* pcs, double gf_group_err) {
187
    SequenceControlSet* scs      = pcs->scs;
188
    EncodeContext*      enc_ctx  = scs->enc_ctx;
189
    RATE_CONTROL*       rc       = &enc_ctx->rc;
190
    TWO_PASS*           twopass  = &scs->twopass;
191
    int                 max_bits = frame_max_bits(rc, enc_ctx);
192
    int64_t             total_group_bits;
193
    // Calculate the bits to be allocated to the group as a whole.
194
    if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) {
195
        int64_t kf_group_bits;
196
        if (scs->lap_rc &&
197
            (scs->lad_mg + 1) * (1 << scs->static_config.hierarchical_levels) <
198
                scs->static_config.intra_period_length) {
199
            kf_group_bits = (int64_t)twopass->kf_group_bits * MIN(pcs->frames_in_sw, rc->frames_to_key) /
200
                rc->frames_to_key;
201
        } else {
202
            kf_group_bits = twopass->kf_group_bits;
203
        }
204
        total_group_bits = (int64_t)(kf_group_bits * (gf_group_err / twopass->kf_group_error_left));
205
    } else {
206
        total_group_bits = 0;
207
    }
208
209
    // Clamp odd edge cases.
210
    total_group_bits = (total_group_bits < 0)         ? 0
211
        : (total_group_bits > twopass->kf_group_bits) ? twopass->kf_group_bits
212
                                                      : total_group_bits;
213
214
    // Clip based on user supplied data rate variability limit.
215
    if (total_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) {
216
        total_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
217
    }
218
    twopass->kf_group_bits = AOMMAX(twopass->kf_group_bits - total_group_bits, 0);
219
    return total_group_bits;
220
}
221
222
/****************************************************************************************************
223
* Allocate the rate per frame based on the rate allocation of previous pass with same prediction
224
* structure and using cross multiplying
225
****************************************************************************************************/
226
static void av1_gop_bit_allocation_same_pred(PictureParentControlSet* pcs, int64_t gf_group_bits,
227
0
                                             GF_GROUP_STATS gf_stats) {
228
    // For key frames the frame target rate is already set
229
0
    int frame_index = (pcs->slice_type == I_SLICE) ? 1 : 0;
230
0
    for (int idx = frame_index; idx < pcs->gf_interval; ++idx) {
231
0
        assert(gf_stats.gf_group_err != 0);
232
0
        pcs->gf_group[idx]->base_frame_target = (int)(gf_group_bits * pcs->gf_group[idx]->stat_struct.total_num_bits /
233
0
                                                      gf_stats.gf_group_err);
234
0
    }
235
0
}
236
237
// Allocate bits to each frame in a GF / ARF group
238
static double layer_fraction[MAX_ARF_LAYERS + 1] = {1.0, 0.80, 0.7, 0.60, 0.60, 1.0, 1.0};
239
240
static void allocate_gf_group_bits(PictureParentControlSet* pcs, RATE_CONTROL* rc, int64_t gf_group_bits,
241
                                   int gf_arf_bits, int gf_interval, int key_frame, int use_arf) {
242
    int64_t total_group_bits = gf_group_bits;
243
    int     base_frame_bits;
244
    int     layer_frames[MAX_ARF_LAYERS + 1] = {0};
245
246
    // Subtract the extra bits set aside for ARF frames from the Group Total
247
    if (use_arf || !key_frame) {
248
        total_group_bits -= gf_arf_bits;
249
    }
250
251
    if (rc->baseline_gf_interval) {
252
        base_frame_bits = (int)(total_group_bits / rc->baseline_gf_interval);
253
    } else {
254
        base_frame_bits = 1;
255
    }
256
257
    // For key frames the frame target rate is already set
258
    int frame_index = key_frame ? 1 : 0;
259
260
    // Check the number of frames in each layer in case we have a
261
    // non standard group length.
262
    int max_arf_layer = pcs->hierarchical_levels;
263
    for (int idx = frame_index; idx < pcs->gf_interval; ++idx) {
264
        if ((pcs->gf_group[idx]->update_type == SVT_AV1_ARF_UPDATE) ||
265
            (pcs->gf_group[idx]->update_type == SVT_AV1_INTNL_ARF_UPDATE)) {
266
            layer_frames[pcs->gf_group[idx]->layer_depth]++;
267
        }
268
    }
269
    if (rc->baseline_gf_interval < (gf_interval >> 1)) {
270
        for (int idx = frame_index; idx < pcs->gf_interval; ++idx) {
271
            if (pcs->gf_group[idx]->update_type == SVT_AV1_ARF_UPDATE) {
272
                layer_frames[pcs->gf_group[idx]->layer_depth] += 1;
273
            }
274
            if (pcs->gf_group[idx]->update_type == SVT_AV1_INTNL_ARF_UPDATE) {
275
                layer_frames[pcs->gf_group[idx]->layer_depth] += 2;
276
            }
277
        }
278
    }
279
    // Allocate extra bits to each ARF layer
280
    int layer_extra_bits[MAX_ARF_LAYERS + 1] = {0};
281
    for (int i = 1; i <= max_arf_layer; ++i) {
282
        if (layer_frames[i]) { // to make sure there is a picture with the depth
283
            double fraction     = (i == max_arf_layer) ? 1.0 : layer_fraction[i];
284
            layer_extra_bits[i] = (int)((gf_arf_bits * fraction) / AOMMAX(1, layer_frames[i]));
285
            gf_arf_bits -= (int)(gf_arf_bits * fraction);
286
        }
287
    }
288
289
    // Now combine ARF layer and baseline bits to give total bits for each frame.
290
    int arf_extra_bits;
291
    for (int idx = frame_index; idx < pcs->gf_interval; ++idx) {
292
        switch (pcs->gf_group[idx]->update_type) {
293
        case SVT_AV1_ARF_UPDATE:
294
        case SVT_AV1_INTNL_ARF_UPDATE:
295
            arf_extra_bits                        = layer_extra_bits[pcs->gf_group[idx]->layer_depth];
296
            pcs->gf_group[idx]->base_frame_target = base_frame_bits + arf_extra_bits;
297
            break;
298
        case SVT_AV1_INTNL_OVERLAY_UPDATE:
299
        case SVT_AV1_OVERLAY_UPDATE:
300
            pcs->gf_group[idx]->base_frame_target = 0;
301
            break;
302
        default:
303
            pcs->gf_group[idx]->base_frame_target = base_frame_bits;
304
            break;
305
        }
306
    }
307
}
308
309
0
#define RC_FACTOR_MIN_GOP_CONST 0.5
310
0
#define RC_FACTOR_MIN_1P_VBR 1
311
0
#define RC_FACTOR_MIN 0.75
312
#define RC_FACTOR_MAX 2
313
314
static INLINE void set_baseline_gf_interval(PictureParentControlSet* pcs, int arf_position) {
315
    SequenceControlSet* scs     = pcs->scs;
316
    EncodeContext*      enc_ctx = scs->enc_ctx;
317
    RATE_CONTROL*       rc      = &enc_ctx->rc;
318
    if (frame_is_intra_only(pcs) && pcs->idr_flag) {
319
        rc->baseline_gf_interval = MAX(arf_position - 1, 1);
320
    } else {
321
        rc->baseline_gf_interval = pcs->gf_interval;
322
    }
323
}
324
325
// initialize GF_GROUP_STATS
326
static void init_gf_stats(GF_GROUP_STATS* gf_stats) {
327
    gf_stats->gf_group_err                  = 0.0;
328
    gf_stats->gf_stat_struct.poc            = 0;
329
    gf_stats->gf_stat_struct.total_num_bits = 1;
330
    gf_stats->gf_stat_struct.qindex         = 172;
331
    gf_stats->gf_stat_struct.worst_qindex   = 172;
332
    gf_stats->gf_group_raw_error            = 0.0;
333
    gf_stats->gf_group_skip_pct             = 0.0;
334
    gf_stats->gf_group_inactive_zone_rows   = 0.0;
335
}
336
337
/***********************************************************************************
338
* calculate_gf_stats()
339
* calculate the gf group stat by looping over frames within the gf
340
************************************************************************************/
341
static void calculate_gf_stats(PictureParentControlSet* ppcs, GF_GROUP_STATS* gf_stats, FIRSTPASS_STATS* this_frame,
342
0
                               int* use_alt_ref) {
343
0
    SequenceControlSet*    scs     = ppcs->scs;
344
0
    RATE_CONTROL*          rc      = &scs->enc_ctx->rc;
345
0
    TWO_PASS*              twopass = &scs->twopass;
346
0
    FIRSTPASS_STATS        next_frame;
347
0
    const FIRSTPASS_STATS* start_pos = twopass->stats_in;
348
349
0
    init_gf_stats(gf_stats);
350
351
    // Load stats for the current frame.
352
0
    double mod_frame_err = calculate_modified_err(twopass, this_frame);
353
354
    // Note the error of the frame at the start of the group. This will be
355
    // the GF frame error if we code a normal gf.
356
357
    // If this is a key frame or the overlay from a previous arf then
358
    // the error score / cost of this frame has already been accounted for.
359
    // There is no overlay support for now
360
0
    if (frame_is_intra_only(ppcs)) {
361
0
        gf_stats->gf_group_err -= mod_frame_err;
362
0
        gf_stats->gf_group_raw_error -= this_frame->coded_error;
363
0
    }
364
0
    gf_stats->gf_stat_struct = this_frame->stat_struct;
365
0
    int i                    = 0;
366
0
    while (i < ppcs->gf_interval) {
367
0
        ++i;
368
        // Accumulate error score of frames in this gf group.
369
0
        mod_frame_err = calculate_modified_err(twopass, this_frame);
370
        // accumulate stats for this frame
371
0
        accumulate_this_frame_stats(this_frame, mod_frame_err, gf_stats);
372
373
        // read in the next frame
374
0
        if (EOF == input_stats(twopass, &next_frame)) {
375
0
            break;
376
0
        }
377
0
        *this_frame = next_frame;
378
0
    }
379
380
    // Was the group length constrained by the requirement for a new KF?
381
0
    rc->constrained_gf_group = (i >= rc->frames_to_key) ? 1 : 0;
382
0
    *use_alt_ref             = (i > 2);
383
0
    set_baseline_gf_interval(ppcs, i);
384
    // Reset the file position.
385
0
    reset_fpf_position(twopass, start_pos);
386
0
}
387
388
/***********************************************************************************
389
 * calculate_active_worst_quality()
390
 * Calculate an estimate of the maxq needed for the group.
391
 ************************************************************************************/
392
0
static void calculate_active_worst_quality(PictureParentControlSet* ppcs, GF_GROUP_STATS gf_stats) {
393
0
    SequenceControlSet* scs        = ppcs->scs;
394
0
    RATE_CONTROL*       rc         = &scs->enc_ctx->rc;
395
0
    FrameInfo*          frame_info = &scs->enc_ctx->frame_info;
396
397
    // Calculate an estimate of the maxq needed for the group.
398
    // We are more agressive about correcting for sections
399
    // where there could be significant overshoot than for easier
400
    // sections where we do not wish to risk creating an overshoot
401
    // of the allocated bit budget.
402
0
    if (rc->baseline_gf_interval > 1) {
403
0
        int    vbr_group_bits_per_frame = (int)(rc->gf_group_bits / rc->baseline_gf_interval);
404
0
        double group_av_err             = gf_stats.gf_group_raw_error / rc->baseline_gf_interval;
405
0
        double group_av_skip_pct        = gf_stats.gf_group_skip_pct / rc->baseline_gf_interval;
406
0
        double group_av_inactive_zone   = ((gf_stats.gf_group_inactive_zone_rows * 2) /
407
0
                                         (rc->baseline_gf_interval * (double)frame_info->mb_rows));
408
409
0
        int tmp_q;
410
        // rc factor is a weight factor that corrects for local rate control drift.
411
0
        double  rc_factor = 1.0;
412
0
        int64_t bits      = scs->static_config.target_bit_rate; //oxcf->target_bandwidth;
413
414
0
        if (bits > 0) {
415
0
            int rate_error;
416
417
0
            rate_error = (int)((rc->vbr_bits_off_target * 100) / bits);
418
0
            rate_error = clamp(rate_error, -100, 100);
419
0
            if (rate_error > 0) {
420
0
                double rc_factor_min = scs->static_config.gop_constraint_rc ? RC_FACTOR_MIN_GOP_CONST
421
0
                    : (scs->static_config.pass == ENC_SINGLE_PASS)          ? RC_FACTOR_MIN_1P_VBR
422
0
                                                                            : RC_FACTOR_MIN;
423
0
                rc_factor            = AOMMAX(rc_factor_min, (double)(100 - rate_error) / 100.0);
424
0
            } else {
425
0
                rc_factor = AOMMIN(RC_FACTOR_MAX, (double)(100 - rate_error) / 100.0);
426
0
            }
427
0
        }
428
0
        tmp_q = get_twopass_worst_quality(
429
0
            ppcs, group_av_err, (group_av_skip_pct + group_av_inactive_zone), vbr_group_bits_per_frame, rc_factor);
430
0
        if (scs->twopass.passes == 2) {
431
0
            int     ref_qindex           = gf_stats.gf_stat_struct.worst_qindex;
432
0
            double  ref_q                = svt_av1_convert_qindex_to_q(ref_qindex, scs->encoder_bit_depth);
433
0
            int64_t ref_gf_group_bits    = (int64_t)(gf_stats.gf_group_err);
434
0
            int64_t target_gf_group_bits = rc->gf_group_bits;
435
0
            {
436
0
                int low  = rc->best_quality;
437
0
                int high = rc->worst_quality;
438
439
0
                while (low < high) {
440
0
                    int    mid      = (low + high) >> 1;
441
0
                    double q        = svt_av1_convert_qindex_to_q(mid, scs->encoder_bit_depth);
442
0
                    int    mid_bits = (int)(ref_gf_group_bits * ref_q * rc_factor / q);
443
444
0
                    if (mid_bits > target_gf_group_bits) {
445
0
                        low = mid + 1;
446
0
                    } else {
447
0
                        high = mid;
448
0
                    }
449
0
                }
450
0
                tmp_q = low;
451
0
            }
452
0
        }
453
0
        rc->active_worst_quality = AOMMAX(tmp_q, rc->active_worst_quality >> 1);
454
0
    }
455
0
}
456
457
static void av1_gop_bit_allocation(PictureParentControlSet* ppcs, RATE_CONTROL* rc, int is_key_frame, int gf_interval,
458
0
                                   int use_arf, int64_t gf_group_bits) {
459
    // Calculate the extra bits to be used for boosted frame(s)
460
0
    int gf_arf_bits = svt_av1_calculate_boost_bits(rc->baseline_gf_interval, rc->gfu_boost, gf_group_bits);
461
    // Allocate bits to each of the frames in the GF group.
462
0
    allocate_gf_group_bits(ppcs, rc, gf_group_bits, gf_arf_bits, gf_interval, is_key_frame, use_arf);
463
0
}
464
465
/*!\brief Assign rate to the GF group or mini gop.
466
 *
467
 * \ingroup gf_group_algo
468
 * This function assigns setup the various parameters regarding bit-allocation and quality setup.
469
 *
470
 * \param[in]    pcs         PictureParentControlSet
471
 * \param[in]    this_frame      First pass statistics structure
472
 *
473
 * \return Nothing is returned. Instead, enc_ctx->gf_group is changed.
474
 */
475
0
static void gf_group_rate_assingment(PictureParentControlSet* pcs, FIRSTPASS_STATS* this_frame) {
476
0
    SequenceControlSet*    scs       = pcs->scs;
477
0
    EncodeContext*         enc_ctx   = scs->enc_ctx;
478
0
    RATE_CONTROL*          rc        = &enc_ctx->rc;
479
0
    TWO_PASS*              twopass   = &scs->twopass;
480
0
    const FIRSTPASS_STATS* start_pos = twopass->stats_in;
481
0
    GF_GROUP_STATS         gf_stats;
482
0
    int                    use_alt_ref;
483
0
    calculate_gf_stats(pcs, &gf_stats, this_frame, &use_alt_ref);
484
485
    // Calculate the bits to be allocated to the gf/arf group as a whole
486
0
    rc->gf_group_bits = calculate_total_gf_group_bits(pcs, gf_stats.gf_group_err);
487
    // Calculate an estimate of the maxq needed for the group.
488
0
    calculate_active_worst_quality(pcs, gf_stats);
489
490
    // Adjust KF group bits and error remaining.
491
0
    twopass->kf_group_error_left -= (int64_t)gf_stats.gf_group_err;
492
493
    // Reset the file position.
494
0
    reset_fpf_position(twopass, start_pos);
495
0
    if (twopass->passes == 2 && scs->static_config.pass == ENC_SECOND_PASS) {
496
0
        av1_gop_bit_allocation_same_pred(pcs, rc->gf_group_bits, gf_stats);
497
0
    } else {
498
0
        av1_gop_bit_allocation(pcs,
499
0
                               rc,
500
0
                               pcs->frm_hdr.frame_type == KEY_FRAME,
501
0
                               (1 << scs->static_config.hierarchical_levels),
502
0
                               use_alt_ref,
503
0
                               rc->gf_group_bits);
504
0
    }
505
0
}
506
507
/*!\brief Variable initialization for lap_rc
508
 *
509
 * This function initialized some variable for lap_rc by looping over the look ahead
510
 *
511
 */
512
0
static void lap_rc_init(PictureParentControlSet* pcs, FIRSTPASS_STATS this_frame) {
513
0
    SequenceControlSet*    scs                  = pcs->scs;
514
0
    EncodeContext*         enc_ctx              = scs->enc_ctx;
515
0
    TWO_PASS*              twopass              = &scs->twopass;
516
0
    int                    num_stats            = 0;
517
0
    double                 modified_error_total = 0.0;
518
0
    double                 coded_error_total    = 0.0;
519
0
    const FIRSTPASS_STATS* start_position       = twopass->stats_in;
520
0
    FIRSTPASS_STATS        this_frame_ref       = this_frame;
521
522
    // loop over the look ahead and calculate the coded error
523
0
    while (twopass->stats_in <= twopass->stats_buf_ctx->stats_in_end) {
524
        // Accumulate total number of stats available till end of the look ahead
525
0
        num_stats++;
526
        // Accumulate error.
527
0
        coded_error_total += this_frame.coded_error;
528
        // Load the next frame's stats.
529
0
        if (input_stats(twopass, &this_frame) == EOF) {
530
0
            break;
531
0
        }
532
0
    }
533
    // Calculate modified_error_min and modified_error_max which is needed in modified_error_total
534
    // calculation
535
0
    double avg_error = coded_error_total / DOUBLE_DIVIDE_CHECK(num_stats);
536
537
0
    twopass->modified_error_min = (avg_error * enc_ctx->two_pass_cfg.vbrmin_section) / 100;
538
0
    twopass->modified_error_max = (avg_error * enc_ctx->two_pass_cfg.vbrmax_section) / 100;
539
0
    reset_fpf_position(twopass, start_position);
540
0
    this_frame = this_frame_ref;
541
542
    // loop over the look ahead and calculate the modified_error_total
543
0
    while (twopass->stats_in <= twopass->stats_buf_ctx->stats_in_end) {
544
        // Accumulate error.
545
0
        modified_error_total += calculate_modified_err(twopass, &this_frame);
546
        // Load the next frame's stats.
547
0
        if (input_stats(twopass, &this_frame) == EOF) {
548
0
            break;
549
0
        }
550
0
    }
551
552
0
    twopass->modified_error_left = modified_error_total;
553
0
    twopass->bits_left += (int64_t)(num_stats * (scs->static_config.target_bit_rate / scs->new_framerate));
554
0
    reset_fpf_position(twopass, start_position);
555
0
}
556
557
/*!\brief calculating group_error for lap_rc
558
 *
559
 * This function calculates group error for lap_rc by looping over the look ahead
560
 *
561
 */
562
0
static double lap_rc_group_error_calc(PictureParentControlSet* pcs, FIRSTPASS_STATS this_frame) {
563
0
    SequenceControlSet*    scs                  = pcs->scs;
564
0
    TWO_PASS*              twopass              = &scs->twopass;
565
0
    int                    num_stats            = 0;
566
0
    double                 modified_error_total = 0.0;
567
0
    const FIRSTPASS_STATS* start_position       = twopass->stats_in;
568
569
    // loop over the look ahead and calculate the modified_error_total
570
0
    while (twopass->stats_in <= twopass->stats_buf_ctx->stats_in_end && num_stats < scs->enc_ctx->rc.frames_to_key) {
571
0
        num_stats++;
572
        // Accumulate error.
573
0
        modified_error_total += calculate_modified_err(twopass, &this_frame);
574
        // Load the next frame's stats.
575
0
        if (input_stats(twopass, &this_frame) == EOF) {
576
0
            break;
577
0
        }
578
0
    }
579
0
    reset_fpf_position(twopass, start_position);
580
0
    return modified_error_total;
581
0
}
582
583
/*!\brief Sets frames to key.
584
 *
585
 * \ingroup gf_group_algo
586
 * This function sets the frames to key for different scenarios
587
 *
588
 * \param[in]    this_frame       Pointer to first pass stats
589
 * \param[out]   kf_group_err     The total error in the KF group
590
 * \param[in]    num_frames_to_detect_scenecut Maximum lookahead frames.
591
 *
592
 * \return       Number of frames to the next key.
593
 */
594
static void set_kf_interval_variables(PictureParentControlSet* pcs, FIRSTPASS_STATS* this_frame, double* kf_group_err,
595
0
                                      int num_frames_to_detect_scenecut) {
596
0
    SequenceControlSet* scs     = pcs->scs;
597
0
    EncodeContext*      enc_ctx = scs->enc_ctx;
598
0
    RATE_CONTROL*       rc      = &enc_ctx->rc;
599
0
    TWO_PASS*           twopass = &scs->twopass;
600
601
0
    int frames_to_key = 0;
602
0
    if (num_frames_to_detect_scenecut == 0) {
603
0
        return;
604
0
    }
605
606
0
    while (twopass->stats_in <= twopass->stats_buf_ctx->stats_in_end && frames_to_key < num_frames_to_detect_scenecut) {
607
        // Accumulate total number of stats available till next key frame
608
609
        // Accumulate kf group error.
610
0
        if (kf_group_err != NULL) {
611
0
            *kf_group_err += calculate_modified_err(twopass, this_frame);
612
0
        }
613
614
0
        ++frames_to_key;
615
0
        if (input_stats(twopass, this_frame) == EOF) {
616
0
            break;
617
0
        }
618
0
    }
619
0
    if (scs->lap_rc && pcs->end_of_sequence_region) {
620
0
        pcs->rate_control_param_ptr->end_of_seq_seen = 1;
621
0
    }
622
0
    if (scs->lap_rc && !pcs->end_of_sequence_region) {
623
0
        rc->frames_to_key = scs->static_config.intra_period_length + 1;
624
0
    } else {
625
0
        rc->frames_to_key = AOMMIN((scs->static_config.intra_period_length + 1), frames_to_key);
626
0
    }
627
0
}
628
629
static int64_t get_kf_group_bits(PictureParentControlSet* pcs, double kf_group_err) {
630
    SequenceControlSet* scs     = pcs->scs;
631
    EncodeContext*      enc_ctx = scs->enc_ctx;
632
    RATE_CONTROL*       rc      = &enc_ctx->rc;
633
    TWO_PASS*           twopass = &scs->twopass;
634
    int64_t             kf_group_bits;
635
    if (scs->lap_rc && pcs->frames_in_sw < scs->static_config.intra_period_length && !pcs->end_of_sequence_region) {
636
        kf_group_bits = (int64_t)rc->frames_to_key * rc->avg_frame_bandwidth;
637
    } else {
638
        kf_group_bits = (int64_t)(twopass->bits_left * (kf_group_err / twopass->modified_error_left));
639
    }
640
641
    return kf_group_bits;
642
}
643
644
0
#define MAX_KF_BITS_INTERVAL_SINGLE_PASS 5
645
646
/*****************************************************************************/
647
// kf_group_rate_assingment
648
// Rate assignment for the next kf group
649
// Only works for key frames, and scene change is not detected for now
650
/*****************************************************************************/
651
0
static void kf_group_rate_assingment(PictureParentControlSet* pcs, FIRSTPASS_STATS this_frame) {
652
0
    SequenceControlSet* scs     = pcs->scs;
653
0
    EncodeContext*      enc_ctx = scs->enc_ctx;
654
0
    RATE_CONTROL*       rc      = &enc_ctx->rc;
655
0
    TWO_PASS*           twopass = &scs->twopass;
656
0
    FIRSTPASS_STATS     next_frame;
657
0
    av1_zero(next_frame);
658
659
0
    rc->frames_since_key = 0;
660
661
0
    const FIRSTPASS_STATS*       start_position = twopass->stats_in;
662
0
    int                          kf_bits        = 0;
663
0
    double                       kf_mod_err;
664
0
    double                       kf_group_err          = 0.0;
665
0
    int                          frames_to_key_clipped = INT_MAX;
666
0
    int64_t                      kf_group_bits_clipped = INT64_MAX;
667
668
0
    twopass->kf_group_bits       = 0; // Total bits available to kf group
669
0
    twopass->kf_group_error_left = 0; // Group modified error score.
670
0
    kf_mod_err                   = calculate_modified_err(twopass, &this_frame);
671
0
    set_kf_interval_variables(pcs, &this_frame, &kf_group_err, scs->static_config.intra_period_length + 1);
672
673
    // Calculate the number of bits that should be assigned to the kf group.
674
0
    if ((twopass->bits_left > 0 && twopass->modified_error_left > 0.0) || scs->lap_rc) {
675
        // Maximum number of bits for a single normal frame (not key frame).
676
0
        int max_bits = frame_max_bits(rc, enc_ctx);
677
678
        // Maximum number of bits allocated to the key frame group.
679
0
        int64_t max_grp_bits;
680
681
        // Default allocation based on bits left and relative
682
        // complexity of the section.
683
0
        twopass->kf_group_bits = get_kf_group_bits(pcs, kf_group_err /*, kf_group_avg_error*/);
684
        // Clip based on maximum per frame rate defined by the user.
685
0
        max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
686
0
        if (twopass->kf_group_bits > max_grp_bits) {
687
0
            twopass->kf_group_bits = max_grp_bits;
688
0
        }
689
0
    } else {
690
0
        twopass->kf_group_bits = 0;
691
0
    }
692
0
    twopass->kf_group_bits = AOMMAX(0, twopass->kf_group_bits);
693
0
    if (scs->lap_rc) {
694
        // For 1 PASS VBR, as the lookahead is moving, the bits left is recalculated for the next KF. The second term is added again as it is part of look ahead of the next KF
695
0
        twopass->bits_left -= (twopass->kf_group_bits +
696
0
                               (int64_t)(((int64_t)pcs->frames_in_sw - (int64_t)rc->frames_to_key) *
697
0
                                         (scs->static_config.target_bit_rate / scs->new_framerate)));
698
0
    } else {
699
0
        twopass->bits_left = AOMMAX(twopass->bits_left - twopass->kf_group_bits, 0);
700
0
    }
701
0
    if (scs->lap_rc) {
702
        // In the case of single pass based on LAP, frames to  key may have an
703
        // inaccurate value, and hence should be clipped to an appropriate
704
        // interval.
705
0
        frames_to_key_clipped = (int)(MAX_KF_BITS_INTERVAL_SINGLE_PASS * scs->new_framerate);
706
        // This variable calculates the bits allocated to kf_group with a clipped
707
        // frames_to_key.
708
0
        if (rc->frames_to_key > frames_to_key_clipped) {
709
0
            kf_group_bits_clipped = (int64_t)((double)twopass->kf_group_bits * frames_to_key_clipped /
710
0
                                              rc->frames_to_key);
711
0
        }
712
0
    }
713
    // Reset the first pass file position.
714
0
    reset_fpf_position(twopass, start_position);
715
716
    // Store the zero motion percentage
717
0
    twopass->kf_zeromotion_pct = 0;
718
    // Work out how many bits to allocate for the key frame itself.
719
    // In case of LAP enabled for VBR, if the frames_to_key value is
720
    // very high, we calculate the bits based on a clipped value of
721
    // frames_to_key.
722
0
    if (twopass->passes == 2) {
723
0
        kf_bits = (int)(twopass->kf_group_bits * (twopass->stats_in - 1)->stat_struct.total_num_bits / kf_group_err);
724
0
    } else {
725
0
        kf_bits = svt_av1_calculate_boost_bits(AOMMIN(rc->frames_to_key, frames_to_key_clipped) - 1,
726
0
                                               rc->kf_boost,
727
0
                                               AOMMIN(twopass->kf_group_bits, kf_group_bits_clipped));
728
0
    }
729
730
0
    twopass->kf_group_bits -= kf_bits;
731
732
    // Save the bits to spend on the key frame.
733
0
    pcs->base_frame_target = kf_bits;
734
735
    // Note the total error score of the kf group minus the key frame itself.
736
0
    twopass->kf_group_error_left = (int64_t)(kf_group_err - kf_mod_err);
737
738
    // Adjust the count of total modified error left.
739
    // The count of bits left is adjusted elsewhere based on real coded frame
740
    // sizes.
741
0
    twopass->modified_error_left -= kf_group_err;
742
0
}
743
744
#define DEFAULT_GRP_WEIGHT 1.0
745
746
static int get_section_target_bandwidth(PictureParentControlSet* pcs) {
747
    SequenceControlSet* scs     = pcs->scs;
748
    EncodeContext*      enc_ctx = scs->enc_ctx;
749
    TWO_PASS*           twopass = &scs->twopass;
750
    RATE_CONTROL*       rc      = &enc_ctx->rc;
751
    int                 section_target_bandwidth;
752
    int                 frames_left = (int)(twopass->stats_buf_ctx->total_stats->count - pcs->picture_number);
753
    if (scs->lap_rc) {
754
        section_target_bandwidth = (int)rc->avg_frame_bandwidth;
755
    } else {
756
        section_target_bandwidth = (int)(twopass->bits_left / frames_left);
757
    }
758
    return section_target_bandwidth;
759
}
760
761
static void process_first_pass_stats(PictureParentControlSet* pcs, FIRSTPASS_STATS* this_frame) {
762
    SequenceControlSet* scs     = pcs->scs;
763
    EncodeContext*      enc_ctx = scs->enc_ctx;
764
    TWO_PASS*           twopass = &scs->twopass;
765
    RATE_CONTROL*       rc      = &enc_ctx->rc;
766
    RateControlCfg*     rc_cfg  = &enc_ctx->rc_cfg;
767
    if (pcs->picture_number == 0 && twopass->stats_buf_ctx->total_stats && twopass->stats_buf_ctx->total_left_stats) {
768
        if (scs->lap_rc) {
769
            /*
770
       * Accumulate total_stats using available limited number of stats,
771
       * and assign it to total_left_stats.
772
       */
773
            *twopass->stats_buf_ctx->total_left_stats = *twopass->stats_buf_ctx->total_stats;
774
        }
775
        // Special case code for first frame.
776
        int    section_target_bandwidth = get_section_target_bandwidth(pcs);
777
        double section_length           = twopass->stats_buf_ctx->total_left_stats->count;
778
        double section_error            = twopass->stats_buf_ctx->total_left_stats->coded_error / section_length;
779
        int    tmp_q;
780
        if (scs->passes == 2) {
781
            int     ref_qindex           = twopass->stats_buf_ctx->stats_in_start->stat_struct.worst_qindex;
782
            double  ref_q                = svt_av1_convert_qindex_to_q(ref_qindex, scs->encoder_bit_depth);
783
            int64_t ref_gf_group_bits    = (int64_t)(twopass->stats_buf_ctx->total_stats->stat_struct.total_num_bits);
784
            int64_t target_gf_group_bits = twopass->bits_left;
785
            {
786
                int low  = rc->best_quality;
787
                int high = rc->worst_quality;
788
789
                while (low < high) {
790
                    int    mid      = (low + high) >> 1;
791
                    double q        = svt_av1_convert_qindex_to_q(mid, scs->encoder_bit_depth);
792
                    int    mid_bits = (int)(ref_gf_group_bits * ref_q / q);
793
794
                    if (mid_bits > target_gf_group_bits) {
795
                        low = mid + 1;
796
                    } else {
797
                        high = mid;
798
                    }
799
                }
800
                tmp_q = low;
801
            }
802
        } else {
803
            tmp_q = get_twopass_worst_quality(pcs, section_error, 0, section_target_bandwidth, DEFAULT_GRP_WEIGHT);
804
        }
805
806
        rc->active_worst_quality          = tmp_q;
807
        rc->avg_frame_qindex[INTER_FRAME] = tmp_q;
808
        rc->avg_frame_qindex[KEY_FRAME]   = (tmp_q + rc_cfg->best_allowed_q) / 2;
809
    }
810
811
    if (input_stats(twopass, this_frame) == EOF) {
812
        return;
813
    }
814
815
    // Update the total stats remaining structure.
816
    if (twopass->stats_buf_ctx->total_left_stats) {
817
        subtract_stats(twopass->stats_buf_ctx->total_left_stats, this_frame);
818
    }
819
}
820
821
// Calculates is new gf group and stores in pcs->is_new_gf_group
822
// For P pictures in the incomplete minigops, since there is no order, we search all of them and set the flag accordingly
823
0
static void is_new_gf_group(PictureParentControlSet* pcs) {
824
0
    pcs->is_new_gf_group = 0;
825
0
    if (!svt_aom_is_incomp_mg_frame(pcs)) {
826
0
        pcs->is_new_gf_group = pcs->gf_update_due;
827
0
    } else {
828
0
        for (int pic_i = 0; pic_i < pcs->gf_interval; ++pic_i) {
829
            // For P-pictures, since the pictures might get released and replaced by other pictures, we check the POC difference
830
0
            if (pcs->gf_group[pic_i] &&
831
0
                (int)ABS((int64_t)pcs->gf_group[pic_i]->picture_number - (int64_t)pcs->picture_number) <=
832
0
                    pcs->gf_interval &&
833
0
                svt_aom_is_incomp_mg_frame(pcs->gf_group[pic_i]) && pcs->gf_group[pic_i]->gf_update_due) {
834
0
                pcs->is_new_gf_group = 1;
835
0
            }
836
0
        }
837
0
        if (pcs->is_new_gf_group) {
838
0
            for (int pic_i = 0; pic_i < pcs->gf_interval; ++pic_i) {
839
0
                if (pcs->gf_group[pic_i]) {
840
0
                    pcs->gf_group[pic_i]->gf_update_due = 0;
841
0
                }
842
0
            }
843
0
        }
844
0
    }
845
0
}
846
847
0
void svt_aom_process_rc_stat(PictureParentControlSet* pcs) {
848
0
    SequenceControlSet* scs     = pcs->scs;
849
0
    TWO_PASS*           twopass = &scs->twopass;
850
0
    FIRSTPASS_STATS     this_frame;
851
0
    av1_zero(this_frame);
852
0
    process_first_pass_stats(pcs, &this_frame);
853
854
    // Keyframe and section processing.
855
0
    if (frame_is_intra_only(pcs) && pcs->idr_flag) {
856
0
        if (scs->lap_rc) {
857
0
            lap_rc_init(pcs, this_frame);
858
0
        }
859
        // Rate assignment for the next kf group
860
0
        kf_group_rate_assingment(pcs, this_frame);
861
0
    }
862
    // Define a new GF/ARF group. (Should always enter here for key frames).
863
0
    is_new_gf_group(pcs);
864
0
    if (pcs->is_new_gf_group) {
865
        // For 1 pass VBR, as the look ahead moves, the kf_group_error_left changes. It is because in modified_error calculation, av_weight and av_err depand on total_stats, which gets updated.
866
        // So, we recalculate kf_group_error_left for each mini gop except the first one after KF
867
0
        if (!(frame_is_intra_only(pcs) && pcs->idr_flag) && scs->lap_rc) {
868
0
            twopass->kf_group_error_left = (int)lap_rc_group_error_calc(pcs, this_frame);
869
0
        }
870
871
0
        gf_group_rate_assingment(pcs, &this_frame);
872
0
    }
873
0
}
874
875
// Max rate target for 1080P and below encodes under normal circumstances
876
// (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
877
#define MAX_MB_RATE 250
878
#define MAXRATE_1080P 2025000
879
880
0
static void av1_rc_update_framerate(SequenceControlSet* scs) {
881
0
    EncodeContext* enc_ctx    = scs->enc_ctx;
882
0
    RATE_CONTROL*  rc         = &enc_ctx->rc;
883
0
    FrameInfo*     frame_info = &enc_ctx->frame_info;
884
0
    int            vbr_max_bits;
885
0
    int            MBs = frame_info->num_mbs;
886
887
0
    rc->avg_frame_bandwidth = (int)(scs->static_config.target_bit_rate / scs->new_framerate);
888
    // A maximum bitrate for a frame is defined.
889
    // The baseline for this aligns with HW implementations that
890
    // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
891
    // per 16x16 MB (averaged over a frame). However this limit is extended if
892
    // a very high rate is given on the command line or the rate cannot
893
    // be achieved because of a user specified max q (e.g. when the user
894
    // specifies lossless encode.
895
0
    vbr_max_bits            = (int)(((int64_t)rc->avg_frame_bandwidth * enc_ctx->two_pass_cfg.vbrmax_section) / 100);
896
0
    vbr_max_bits            = AOMMIN(vbr_max_bits, INT_MAX);
897
0
    rc->max_frame_bandwidth = AOMMAX(AOMMAX((MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
898
0
}
899
900
// from aom encoder.c
901
0
void svt_av1_new_framerate(SequenceControlSet* scs, double framerate) {
902
0
    scs->new_framerate = framerate < 0.1 ? 30 : framerate;
903
0
    av1_rc_update_framerate(scs);
904
0
}
905
906
474
void svt_aom_set_rc_param(SequenceControlSet* scs) {
907
474
    EncodeContext* enc_ctx    = scs->enc_ctx;
908
474
    FrameInfo*     frame_info = &enc_ctx->frame_info;
909
910
474
    if (scs->first_pass_downsample) {
911
0
        frame_info->frame_width  = scs->max_input_luma_width << 1;
912
0
        frame_info->frame_height = scs->max_input_luma_height << 1;
913
0
        frame_info->mb_cols      = ((scs->max_input_luma_width + 16 - 1) / 16) << 1;
914
0
        frame_info->mb_rows      = ((scs->max_input_luma_height + 16 - 1) / 16) << 1;
915
474
    } else {
916
474
        frame_info->frame_width  = scs->max_input_luma_width;
917
474
        frame_info->frame_height = scs->max_input_luma_height;
918
474
        frame_info->mb_cols      = (scs->max_input_luma_width + 16 - 1) / 16;
919
474
        frame_info->mb_rows      = (scs->max_input_luma_height + 16 - 1) / 16;
920
474
    }
921
474
    frame_info->num_mbs   = frame_info->mb_cols * frame_info->mb_rows;
922
474
    frame_info->bit_depth = scs->static_config.encoder_bit_depth;
923
    // input config  from options
924
474
    enc_ctx->two_pass_cfg.vbrmin_section = scs->static_config.vbr_min_section_pct;
925
474
    enc_ctx->two_pass_cfg.vbrmax_section = scs->static_config.vbr_max_section_pct;
926
474
    enc_ctx->rc_cfg.mode                 = scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_VBR
927
474
                        ? AOM_VBR
928
474
                        : (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR ? AOM_CBR : AOM_Q);
929
474
    enc_ctx->rc_cfg.best_allowed_q       = (int32_t)quantizer_to_qindex[scs->static_config.min_qp_allowed];
930
474
    enc_ctx->rc_cfg.worst_allowed_q      = (int32_t)quantizer_to_qindex[scs->static_config.max_qp_allowed];
931
932
474
    if (scs->static_config.gop_constraint_rc) {
933
0
        enc_ctx->rc_cfg.over_shoot_pct  = 0;
934
0
        enc_ctx->rc_cfg.under_shoot_pct = 0;
935
474
    } else {
936
474
        enc_ctx->rc_cfg.over_shoot_pct  = scs->static_config.over_shoot_pct;
937
474
        enc_ctx->rc_cfg.under_shoot_pct = scs->static_config.under_shoot_pct;
938
474
    }
939
474
    bool is_vbr                              = enc_ctx->rc_cfg.mode == AOM_VBR;
940
474
    enc_ctx->rc_cfg.maximum_buffer_size_ms   = is_vbr ? 240000 : scs->static_config.maximum_buffer_size_ms;
941
474
    enc_ctx->rc_cfg.starting_buffer_level_ms = is_vbr ? 60000 : scs->static_config.starting_buffer_level_ms;
942
474
    enc_ctx->rc_cfg.optimal_buffer_level_ms  = is_vbr ? 60000 : scs->static_config.optimal_buffer_level_ms;
943
944
474
    enc_ctx->rc_cfg.max_intra_bitrate_pct = scs->static_config.max_intra_bitrate_pct;
945
474
    enc_ctx->rc_cfg.max_inter_bitrate_pct = scs->static_config.max_inter_bitrate_pct;
946
947
474
    enc_ctx->sf_cfg.sframe_dist = scs->static_config.sframe_dist;
948
474
    enc_ctx->sf_cfg.sframe_mode = scs->static_config.sframe_mode;
949
474
}
950
951
/******************************************************
952
 * Read Stat from File
953
 * reads StatStruct per frame from the file and stores under pcs
954
 ******************************************************/
955
0
static void read_stat_from_file(SequenceControlSet* scs) {
956
0
    TWO_PASS*        twopass                                = &scs->twopass;
957
0
    FIRSTPASS_STATS* this_frame                             = (FIRSTPASS_STATS*)twopass->stats_in;
958
0
    uint64_t         total_num_bits                         = 0;
959
0
    uint64_t         previous_num_bits[MAX_TEMPORAL_LAYERS] = {0};
960
0
    while (this_frame < twopass->stats_buf_ctx->stats_in_end) {
961
0
        if (this_frame->stat_struct.total_num_bits == 0) {
962
0
            this_frame->stat_struct.total_num_bits = previous_num_bits[this_frame->stat_struct.temporal_layer_index];
963
0
        }
964
0
        previous_num_bits[this_frame->stat_struct.temporal_layer_index] = this_frame->stat_struct.total_num_bits;
965
0
        total_num_bits += this_frame->stat_struct.total_num_bits;
966
0
        this_frame++;
967
0
    }
968
0
    twopass->stats_buf_ctx->total_stats->stat_struct.total_num_bits = total_num_bits;
969
0
}
970
971
0
void svt_av1_init_single_pass_lap(SequenceControlSet* scs) {
972
0
    TWO_PASS*      twopass = &scs->twopass;
973
0
    EncodeContext* enc_ctx = scs->enc_ctx;
974
0
    if (!twopass->stats_buf_ctx->stats_in_end) {
975
0
        return;
976
0
    }
977
978
0
    svt_aom_set_rc_param(scs);
979
980
    // This variable monitors how far behind the second ref update is lagging.
981
982
0
    twopass->bits_left           = 0;
983
0
    twopass->modified_error_min  = 0.0;
984
0
    twopass->modified_error_max  = 0.0;
985
0
    twopass->modified_error_left = 0.0;
986
987
    // Reset the vbr bits off target counters
988
0
    enc_ctx->rc.vbr_bits_off_target      = 0;
989
0
    enc_ctx->rc.vbr_bits_off_target_fast = 0;
990
991
0
    enc_ctx->rc.rate_error_estimate = 0;
992
993
    // Static sequence monitor variables.
994
0
    twopass->kf_zeromotion_pct = 100;
995
0
}
996
997
0
void svt_av1_init_second_pass(SequenceControlSet* scs) {
998
0
    TWO_PASS*      twopass = &scs->twopass;
999
0
    EncodeContext* enc_ctx = scs->enc_ctx;
1000
1001
0
    double           frame_rate;
1002
0
    FIRSTPASS_STATS* stats;
1003
1004
0
    if (!twopass->stats_buf_ctx->stats_in_end) {
1005
0
        return;
1006
0
    }
1007
0
    {
1008
0
        svt_av1_twopass_zero_stats(twopass->stats_buf_ctx->stats_in_end);
1009
0
        FIRSTPASS_STATS* this_frame     = (FIRSTPASS_STATS*)scs->twopass.stats_in;
1010
0
        uint64_t         total_num_bits = 0;
1011
1012
0
        while (this_frame < scs->twopass.stats_buf_ctx->stats_in_end) {
1013
0
            if (twopass->stats_buf_ctx->stats_in_end != NULL) {
1014
0
                svt_av1_accumulate_stats(twopass->stats_buf_ctx->stats_in_end, this_frame);
1015
0
            }
1016
0
            total_num_bits += this_frame->stat_struct.total_num_bits;
1017
0
            this_frame++;
1018
0
        }
1019
0
        twopass->stats_buf_ctx->stats_in_end->stat_struct.total_num_bits = total_num_bits;
1020
0
    }
1021
0
    svt_aom_set_rc_param(scs);
1022
0
    stats                                     = twopass->stats_buf_ctx->total_stats;
1023
0
    *stats                                    = *twopass->stats_buf_ctx->stats_in_end;
1024
0
    *twopass->stats_buf_ctx->total_left_stats = *stats;
1025
1026
0
    frame_rate = 10000000.0 * stats->count / stats->duration;
1027
    // Each frame can have a different duration, as the frame rate in the source
1028
    // isn't guaranteed to be constant. The frame rate prior to the first frame
1029
    // encoded in the second pass is a guess. However, the sum duration is not.
1030
    // It is calculated based on the actual durations of all frames from the
1031
    // first pass.
1032
0
    svt_av1_new_framerate(scs, frame_rate);
1033
0
    twopass->bits_left = (int64_t)(stats->duration * (int64_t)scs->static_config.target_bit_rate / 10000000.0);
1034
0
    read_stat_from_file(scs);
1035
1036
    // Scan the first pass file and calculate a modified total error based upon
1037
    // the bias/power function used to allocate bits.
1038
0
    {
1039
0
        double                 avg_error            = stats->coded_error / DOUBLE_DIVIDE_CHECK(stats->count);
1040
0
        const FIRSTPASS_STATS* s                    = twopass->stats_in;
1041
0
        double                 modified_error_total = 0.0;
1042
0
        twopass->modified_error_min                 = (avg_error * enc_ctx->two_pass_cfg.vbrmin_section) / 100;
1043
0
        twopass->modified_error_max                 = (avg_error * enc_ctx->two_pass_cfg.vbrmax_section) / 100;
1044
0
        while (s < twopass->stats_buf_ctx->stats_in_end) {
1045
0
            modified_error_total += calculate_modified_err(twopass, s);
1046
0
            ++s;
1047
0
        }
1048
0
        twopass->modified_error_left = modified_error_total;
1049
0
    }
1050
1051
    // Reset the vbr bits off target counters
1052
0
    enc_ctx->rc.vbr_bits_off_target      = 0;
1053
0
    enc_ctx->rc.vbr_bits_off_target_fast = 0;
1054
0
    enc_ctx->rc.rate_error_estimate      = 0;
1055
1056
    // Static sequence monitor variables.
1057
0
    twopass->kf_zeromotion_pct = 100;
1058
0
}
1059
1060
/*********************************************************************************************
1061
 * Update the internal RC and TWO_PASS struct stats based on the received feedback
1062
 ***********************************************************************************************/
1063
0
void svt_av1_twopass_postencode_update_gop_const(PictureParentControlSet* ppcs) {
1064
0
    SequenceControlSet*              scs          = ppcs->scs;
1065
0
    EncodeContext*                   enc_cont     = scs->enc_ctx;
1066
0
    RATE_CONTROL*                    rc           = &enc_cont->rc;
1067
0
    RateControlCfg*                  rc_cfg       = &enc_cont->rc_cfg;
1068
0
    RateControlIntervalParamContext* rc_param_ptr = ppcs->rate_control_param_ptr;
1069
1070
    // VBR correction is done through rc->vbr_bits_off_target. Based on the
1071
    // sign of this value, a limited % adjustment is made to the target rate
1072
    // of subsequent frames, to try and push it back towards 0. This method
1073
    // is designed to prevent extreme behaviour at the end of a clip
1074
    // or group of frames.
1075
0
    rc_param_ptr->vbr_bits_off_target += ppcs->base_frame_target - ppcs->projected_frame_size;
1076
    // Target vs actual bits for this arf group.
1077
0
    int rate_error_estimate_target = 0;
1078
    // Calculate the pct rc error.
1079
0
    if (rc_param_ptr->total_actual_bits) {
1080
0
        if (rc_param_ptr->total_target_bits) {
1081
0
            rate_error_estimate_target = (int)((rc_param_ptr->vbr_bits_off_target * 100) /
1082
0
                                               rc_param_ptr->total_target_bits);
1083
0
        }
1084
0
        rc_param_ptr->rate_error_estimate = (int)((rc_param_ptr->vbr_bits_off_target * 100) /
1085
0
                                                  rc_param_ptr->total_actual_bits);
1086
0
        rc_param_ptr->rate_error_estimate = clamp(rc_param_ptr->rate_error_estimate, -100, 100);
1087
0
    } else {
1088
0
        rc_param_ptr->rate_error_estimate = 0;
1089
0
    }
1090
1091
    // Update the active best quality pyramid.
1092
0
    if (!ppcs->is_overlay) {
1093
0
        int pyramid_level = ppcs->layer_depth;
1094
0
        int i;
1095
0
        for (i = pyramid_level; i <= MAX_ARF_LAYERS; ++i) {
1096
0
            rc->active_best_quality[i] = ppcs->frm_hdr.quantization_params.base_q_idx;
1097
0
        }
1098
0
    }
1099
1100
    // If the rate control is drifting consider adjustment to min or maxq.
1101
0
    if (!ppcs->is_overlay) {
1102
0
        int maxq_adj_limit = rc->worst_quality - rc->active_worst_quality;
1103
0
        int minq_adj_limit = MINQ_ADJ_LIMIT;
1104
1105
        // Undershoot.
1106
0
        if (rc_param_ptr->rate_error_estimate > rc_cfg->under_shoot_pct) {
1107
0
            --rc_param_ptr->extend_maxq;
1108
0
            if (rc_param_ptr->rolling_target_bits >= rc_param_ptr->rolling_actual_bits) {
1109
0
                ++rc_param_ptr->extend_minq;
1110
0
            }
1111
            // Overshoot.
1112
0
        } else if (rc_param_ptr->rate_error_estimate < -rc_cfg->over_shoot_pct) {
1113
0
            --rc_param_ptr->extend_minq;
1114
0
            if (rc_param_ptr->rolling_target_bits < rc_param_ptr->rolling_actual_bits) {
1115
0
                rc_param_ptr->extend_maxq += (scs->is_short_clip) ? rate_error_estimate_target < -100 ? 10 : 2 : 1;
1116
0
            }
1117
0
        } else {
1118
            // Adjustment for extreme local overshoot.
1119
0
            if (ppcs->projected_frame_size > (2 * ppcs->base_frame_target) &&
1120
0
                ppcs->projected_frame_size > (2 * rc->avg_frame_bandwidth)) {
1121
0
                ++rc_param_ptr->extend_maxq;
1122
0
            }
1123
1124
            // Unwind undershoot or overshoot adjustment.
1125
0
            if (rc_param_ptr->rolling_target_bits < rc_param_ptr->rolling_actual_bits) {
1126
0
                --rc_param_ptr->extend_minq;
1127
0
            } else if (rc_param_ptr->rolling_target_bits > rc_param_ptr->rolling_actual_bits) {
1128
0
                --rc_param_ptr->extend_maxq;
1129
0
            }
1130
0
            if (scs->is_short_clip) {
1131
0
                if (rc_param_ptr->extend_minq > minq_adj_limit / 3) {
1132
0
                    rc_param_ptr->extend_minq -= 5;
1133
0
                }
1134
0
                if (rc_param_ptr->extend_maxq < -maxq_adj_limit / 3) {
1135
0
                    rc_param_ptr->extend_maxq += 5;
1136
0
                }
1137
0
            }
1138
0
        }
1139
0
        if (scs->is_short_clip) {
1140
0
            rc_param_ptr->extend_minq = clamp(rc_param_ptr->extend_minq, -minq_adj_limit / 4, minq_adj_limit);
1141
0
        } else {
1142
0
            rc_param_ptr->extend_minq = clamp(rc_param_ptr->extend_minq, 0, minq_adj_limit);
1143
0
        }
1144
0
        if (!scs->is_short_clip) {
1145
0
            rc_param_ptr->extend_maxq = clamp(rc_param_ptr->extend_maxq, 0, maxq_adj_limit);
1146
0
        }
1147
1148
        // If there is a big and undexpected undershoot then feed the extra
1149
        // bits back in quickly. One situation where this may happen is if a
1150
        // frame is unexpectedly almost perfectly predicted by the ARF or GF
1151
        // but not very well predcited by the previous frame.
1152
0
        if (!svt_aom_frame_is_kf_gf_arf(ppcs) && !ppcs->is_overlay) {
1153
0
            int fast_extra_thresh = ppcs->base_frame_target / HIGH_UNDERSHOOT_RATIO;
1154
0
            if (ppcs->projected_frame_size < fast_extra_thresh && rc_param_ptr->rate_error_estimate > 0) {
1155
0
                rc_param_ptr->vbr_bits_off_target_fast += fast_extra_thresh - ppcs->projected_frame_size;
1156
0
                rc_param_ptr->vbr_bits_off_target_fast = AOMMIN(rc_param_ptr->vbr_bits_off_target_fast,
1157
0
                                                                (4 * rc->avg_frame_bandwidth));
1158
1159
                // Fast adaptation of minQ if necessary to use up the extra bits.
1160
0
                if (rc->avg_frame_bandwidth) {
1161
0
                    rc_param_ptr->extend_minq_fast = (int)(rc_param_ptr->vbr_bits_off_target_fast * 8 /
1162
0
                                                           rc->avg_frame_bandwidth);
1163
0
                }
1164
0
                rc_param_ptr->extend_minq_fast = AOMMIN(rc_param_ptr->extend_minq_fast,
1165
0
                                                        minq_adj_limit - rc_param_ptr->extend_minq);
1166
0
            } else if (rc_param_ptr->vbr_bits_off_target_fast) {
1167
0
                rc_param_ptr->extend_minq_fast = AOMMIN(rc_param_ptr->extend_minq_fast,
1168
0
                                                        minq_adj_limit - rc_param_ptr->extend_minq);
1169
0
            } else {
1170
0
                rc_param_ptr->extend_minq_fast = 0;
1171
0
            }
1172
0
        }
1173
0
    }
1174
0
}
1175
1176
0
void svt_av1_twopass_postencode_update(PictureParentControlSet* ppcs) {
1177
0
    SequenceControlSet* scs     = ppcs->scs;
1178
0
    EncodeContext*      enc_ctx = scs->enc_ctx;
1179
0
    RATE_CONTROL*       rc      = &enc_ctx->rc;
1180
0
    TWO_PASS*           twopass = &scs->twopass;
1181
0
    RateControlCfg*     rc_cfg  = &enc_ctx->rc_cfg;
1182
1183
    // VBR correction is done through rc->vbr_bits_off_target. Based on the
1184
    // sign of this value, a limited % adjustment is made to the target rate
1185
    // of subsequent frames, to try and push it back towards 0. This method
1186
    // is designed to prevent extreme behaviour at the end of a clip
1187
    // or group of frames.
1188
0
    rc->vbr_bits_off_target += ppcs->base_frame_target - ppcs->projected_frame_size;
1189
    // Target vs actual bits for this arf group.
1190
0
    int rate_error_estimate_target = 0;
1191
    // Calculate the pct rc error.
1192
0
    if (rc->total_actual_bits) {
1193
0
        if (rc->total_target_bits) {
1194
0
            rate_error_estimate_target = (int)((rc->vbr_bits_off_target * 100) / rc->total_target_bits);
1195
0
        }
1196
0
        rc->rate_error_estimate = (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits);
1197
0
        rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100);
1198
0
    } else {
1199
0
        rc->rate_error_estimate = 0;
1200
0
    }
1201
1202
    // Update the active best quality pyramid.
1203
0
    if (!ppcs->is_overlay) {
1204
0
        int pyramid_level = ppcs->layer_depth;
1205
0
        int i;
1206
0
        for (i = pyramid_level; i <= MAX_ARF_LAYERS; ++i) {
1207
0
            rc->active_best_quality[i] = ppcs->frm_hdr.quantization_params.base_q_idx;
1208
0
        }
1209
0
    }
1210
1211
    // If the rate control is drifting consider adjustment to min or maxq.
1212
0
    if (!ppcs->is_overlay) {
1213
0
        int maxq_adj_limit = rc->worst_quality - rc->active_worst_quality;
1214
0
        int minq_adj_limit = MINQ_ADJ_LIMIT;
1215
1216
        // Undershoot.
1217
0
        if (rc->rate_error_estimate > rc_cfg->under_shoot_pct) {
1218
0
            --twopass->extend_maxq;
1219
0
            if (rc->rolling_target_bits >= rc->rolling_actual_bits) {
1220
0
                ++twopass->extend_minq;
1221
0
            }
1222
            // Overshoot.
1223
0
        } else if (rc->rate_error_estimate < -rc_cfg->over_shoot_pct) {
1224
0
            --twopass->extend_minq;
1225
0
            if (rc->rolling_target_bits < rc->rolling_actual_bits) {
1226
0
                twopass->extend_maxq += (scs->is_short_clip) ? rate_error_estimate_target < -100 ? 10 : 2 : 1;
1227
0
            }
1228
0
        } else {
1229
            // Adjustment for extreme local overshoot.
1230
0
            if (ppcs->projected_frame_size > (2 * ppcs->base_frame_target) &&
1231
0
                ppcs->projected_frame_size > (2 * rc->avg_frame_bandwidth)) {
1232
0
                ++twopass->extend_maxq;
1233
0
            }
1234
1235
            // Unwind undershoot or overshoot adjustment.
1236
0
            if (rc->rolling_target_bits < rc->rolling_actual_bits) {
1237
0
                --twopass->extend_minq;
1238
0
            } else if (rc->rolling_target_bits > rc->rolling_actual_bits) {
1239
0
                --twopass->extend_maxq;
1240
0
            }
1241
0
        }
1242
1243
0
        twopass->extend_minq = clamp(twopass->extend_minq, 0, minq_adj_limit);
1244
0
        if (!scs->is_short_clip) {
1245
0
            twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit);
1246
0
        }
1247
1248
        // If there is a big and undexpected undershoot then feed the extra
1249
        // bits back in quickly. One situation where this may happen is if a
1250
        // frame is unexpectedly almost perfectly predicted by the ARF or GF
1251
        // but not very well predcited by the previous frame.
1252
0
        if (!svt_aom_frame_is_kf_gf_arf(ppcs) && !ppcs->is_overlay) {
1253
0
            int fast_extra_thresh = ppcs->base_frame_target / HIGH_UNDERSHOOT_RATIO;
1254
0
            if (ppcs->projected_frame_size < fast_extra_thresh && rc->rate_error_estimate > 0) {
1255
0
                rc->vbr_bits_off_target_fast += fast_extra_thresh - ppcs->projected_frame_size;
1256
0
                rc->vbr_bits_off_target_fast = AOMMIN(rc->vbr_bits_off_target_fast, (4 * rc->avg_frame_bandwidth));
1257
1258
                // Fast adaptation of minQ if necessary to use up the extra bits.
1259
0
                if (rc->avg_frame_bandwidth) {
1260
0
                    twopass->extend_minq_fast = (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth);
1261
0
                }
1262
0
                twopass->extend_minq_fast = AOMMIN(twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
1263
0
            } else if (rc->vbr_bits_off_target_fast) {
1264
0
                twopass->extend_minq_fast = AOMMIN(twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
1265
0
            } else {
1266
0
                twopass->extend_minq_fast = 0;
1267
0
            }
1268
0
        }
1269
0
    }
1270
0
}