Coverage Report

Created: 2022-08-24 06:17

/src/x265/source/encoder/ratecontrol.cpp
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * Copyright (C) 2013-2020 MulticoreWare, Inc
3
 *
4
 * Authors: Sumalatha Polureddy <sumalatha@multicorewareinc.com>
5
 *          Aarthi Priya Thirumalai <aarthi@multicorewareinc.com>
6
 *          Xun Xu, PPLive Corporation <xunxu@pptv.com>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program 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
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
21
 *
22
 * This program is also available under a commercial proprietary license.
23
 * For more information, contact us at license @ x265.com.
24
 *****************************************************************************/
25
26
#if _MSC_VER
27
#pragma warning(disable: 4127) // conditional expression is constant, yes I know
28
#endif
29
30
#include "common.h"
31
#include "param.h"
32
#include "frame.h"
33
#include "framedata.h"
34
#include "picyuv.h"
35
36
#include "encoder.h"
37
#include "slicetype.h"
38
#include "ratecontrol.h"
39
#include "sei.h"
40
41
0
#define BR_SHIFT  6
42
0
#define CPB_SHIFT 4
43
44
using namespace X265_NS;
45
46
/* Amortize the partial cost of I frames over the next N frames */
47
48
const int RateControl::s_slidingWindowFrames = 20;
49
const char *RateControl::s_defaultStatFileName = "x265_2pass.log";
50
51
namespace {
52
0
#define CMP_OPT_FIRST_PASS(opt, param_val)\
53
0
{\
54
0
    bErr = 0;\
55
0
    p = strstr(opts, opt "=");\
56
0
    char* q = strstr(opts, "no-" opt " ");\
57
0
    if (p && sscanf(p, opt "=%d" , &i) && param_val != i)\
58
0
        bErr = 1;\
59
0
    else if (!param_val && !q && !p)\
60
0
        bErr = 1;\
61
0
    else if (param_val && (q || !strstr(opts, opt)))\
62
0
        bErr = 1;\
63
0
    if (bErr)\
64
0
    {\
65
0
        x265_log(m_param, X265_LOG_ERROR, "different " opt " setting than first pass (%d vs %d)\n", param_val, i);\
66
0
        return false;\
67
0
    }\
68
0
}
69
70
inline int calcScale(uint32_t x)
71
0
{
72
0
    static uint8_t lut[16] = {4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};
73
0
    int y, z = (((x & 0xffff) - 1) >> 27) & 16;
74
0
    x >>= z;
75
0
    z += y = (((x & 0xff) - 1) >> 28) & 8;
76
0
    x >>= y;
77
0
    z += y = (((x & 0xf) - 1) >> 29) & 4;
78
0
    x >>= y;
79
0
    return z + lut[x&0xf];
80
0
}
81
82
inline int calcLength(uint32_t x)
83
0
{
84
0
    static uint8_t lut[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
85
0
    int y, z = (((x >> 16) - 1) >> 27) & 16;
86
0
    x >>= z ^ 16;
87
0
    z += y = ((x - 0x100) >> 28) & 8;
88
0
    x >>= y ^ 8;
89
0
    z += y = ((x - 0x10) >> 29) & 4;
90
0
    x >>= y ^ 4;
91
0
    return z + lut[x];
92
0
}
93
94
inline char *strcatFilename(const char *input, const char *suffix)
95
0
{
96
0
    char *output = X265_MALLOC(char, strlen(input) + strlen(suffix) + 1);
97
0
    if (!output)
98
0
    {
99
0
        x265_log(NULL, X265_LOG_ERROR, "unable to allocate memory for filename\n");
100
0
        return NULL;
101
0
    }
102
0
    strcpy(output, input);
103
0
    strcat(output, suffix);
104
0
    return output;
105
0
}
106
107
inline double qScale2bits(RateControlEntry *rce, double qScale)
108
0
{
109
0
    if (qScale < 0.1)
110
0
        qScale = 0.1;
111
0
    return (rce->coeffBits + .1) * pow(rce->qScale / qScale, 1.1)
112
0
           + rce->mvBits * pow(X265_MAX(rce->qScale, 1) / X265_MAX(qScale, 1), 0.5)
113
0
           + rce->miscBits;
114
0
}
115
116
inline void copyRceData(RateControlEntry* rce, RateControlEntry* rce2Pass)
117
0
{
118
0
    rce->coeffBits = rce2Pass->coeffBits;
119
0
    rce->mvBits = rce2Pass->mvBits;
120
0
    rce->miscBits = rce2Pass->miscBits;
121
0
    rce->iCuCount = rce2Pass->iCuCount;
122
0
    rce->pCuCount = rce2Pass->pCuCount;
123
0
    rce->skipCuCount = rce2Pass->skipCuCount;
124
0
    rce->keptAsRef = rce2Pass->keptAsRef;
125
0
    rce->qScale = rce2Pass->qScale;
126
0
    rce->newQScale = rce2Pass->newQScale;
127
0
    rce->expectedBits = rce2Pass->expectedBits;
128
0
    rce->expectedVbv = rce2Pass->expectedVbv;
129
0
    rce->blurredComplexity = rce2Pass->blurredComplexity;
130
0
    rce->sliceType = rce2Pass->sliceType;
131
0
    rce->qpNoVbv = rce2Pass->qpNoVbv;
132
0
    rce->newQp = rce2Pass->newQp;
133
0
    rce->qRceq = rce2Pass->qRceq;
134
0
}
135
136
}  // end anonymous namespace
137
/* Returns the zone for the current frame */
138
x265_zone* RateControl::getZone()
139
698
{
140
698
    for (int i = m_param->rc.zoneCount - 1; i >= 0; i--)
141
0
    {
142
0
        x265_zone *z = &m_param->rc.zones[i];
143
0
        if (m_framesDone + 1 >= z->startFrame && m_framesDone < z->endFrame)
144
0
            return z;
145
0
    }
146
698
    return NULL;
147
698
}
148
149
RateControl::RateControl(x265_param& p, Encoder *top)
150
698
{
151
698
    m_param = &p;
152
698
    m_top = top;
153
698
    int lowresCuWidth = ((m_param->sourceWidth / 2) + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;
154
698
    int lowresCuHeight = ((m_param->sourceHeight / 2) + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;
155
698
    m_ncu = lowresCuWidth * lowresCuHeight;
156
157
698
    m_qCompress = (m_param->rc.cuTree && !m_param->rc.hevcAq) ? 1 : m_param->rc.qCompress;
158
159
    // validate for param->rc, maybe it is need to add a function like x265_parameters_valiate()
160
698
    m_zoneBufferIdx = 0;
161
698
    m_residualFrames = 0;
162
698
    m_partialResidualFrames = 0;
163
698
    m_residualCost = 0;
164
698
    m_partialResidualCost = 0;
165
698
    m_rateFactorMaxIncrement = 0;
166
698
    m_rateFactorMaxDecrement = 0;
167
698
    m_fps = (double)m_param->fpsNum / m_param->fpsDenom;
168
698
    m_startEndOrder.set(0);
169
698
    m_bTerminated = false;
170
698
    m_finalFrameCount = 0;
171
698
    m_numEntries = 0;
172
698
    m_isSceneTransition = false;
173
698
    m_lastPredictorReset = 0;
174
698
    m_avgPFrameQp = 0;
175
698
    m_isFirstMiniGop = false;
176
698
    m_lastScenecut = -1;
177
698
    m_lastScenecutAwareIFrame = -1;
178
698
    if (m_param->rc.rateControlMode == X265_RC_CRF)
179
503
    {
180
503
        m_param->rc.qp = (int)m_param->rc.rfConstant;
181
503
        m_param->rc.bitrate = 0;
182
183
503
        double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
184
503
        double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;
185
503
        m_rateFactorConstant = pow(baseCplx, 1 - m_qCompress) /
186
503
            x265_qp2qScale(m_param->rc.rfConstant + mbtree_offset);
187
503
        if (m_param->rc.rfConstantMax)
188
0
        {
189
0
            m_rateFactorMaxIncrement = m_param->rc.rfConstantMax - m_param->rc.rfConstant;
190
0
            if (m_rateFactorMaxIncrement <= 0)
191
0
            {
192
0
                x265_log(m_param, X265_LOG_WARNING, "CRF max must be greater than CRF\n");
193
0
                m_rateFactorMaxIncrement = 0;
194
0
            }
195
0
        }
196
503
        if (m_param->rc.rfConstantMin)
197
0
            m_rateFactorMaxDecrement = m_param->rc.rfConstant - m_param->rc.rfConstantMin;
198
503
    }
199
698
    m_isAbr = m_param->rc.rateControlMode != X265_RC_CQP && !m_param->rc.bStatRead;
200
698
    m_2pass = m_param->rc.rateControlMode != X265_RC_CQP && m_param->rc.bStatRead;
201
698
    m_bitrate = m_param->rc.bitrate * 1000;
202
698
    m_frameDuration = (double)m_param->fpsDenom / m_param->fpsNum;
203
698
    m_qp = m_param->rc.qp;
204
698
    m_lastRceq = 1; /* handles the cmplxrsum when the previous frame cost is zero */
205
698
    m_shortTermCplxSum = 0;
206
698
    m_shortTermCplxCount = 0;
207
698
    m_lastNonBPictType = I_SLICE;
208
698
    m_isAbrReset = false;
209
698
    m_lastAbrResetPoc = -1;
210
698
    m_statFileOut = NULL;
211
698
    m_cutreeStatFileOut = m_cutreeStatFileIn = NULL;
212
698
    m_rce2Pass = NULL;
213
698
    m_encOrder = NULL;
214
698
    m_lastBsliceSatdCost = 0;
215
698
    m_movingAvgSum = 0.0;
216
698
    m_isNextGop = false;
217
698
    m_relativeComplexity = NULL;
218
219
    // vbv initialization
220
698
    m_param->rc.vbvBufferSize = x265_clip3(0, 2000000, m_param->rc.vbvBufferSize);
221
698
    m_param->rc.vbvMaxBitrate = x265_clip3(0, 2000000, m_param->rc.vbvMaxBitrate);
222
698
    m_param->rc.vbvBufferInit = x265_clip3(0.0, 2000000.0, m_param->rc.vbvBufferInit);
223
698
    m_param->vbvBufferEnd = x265_clip3(0.0, 2000000.0, m_param->vbvBufferEnd);
224
698
    m_initVbv = false;
225
698
    m_singleFrameVbv = 0;
226
698
    m_rateTolerance = 1.0;
227
228
698
    if (m_param->rc.vbvBufferSize)
229
0
    {
230
0
        if (m_param->rc.rateControlMode == X265_RC_CQP)
231
0
        {
232
0
            x265_log(m_param, X265_LOG_WARNING, "VBV is incompatible with constant QP, ignored.\n");
233
0
            m_param->rc.vbvBufferSize = 0;
234
0
            m_param->rc.vbvMaxBitrate = 0;
235
0
        }
236
0
        else if (m_param->rc.vbvMaxBitrate == 0)
237
0
        {
238
0
            if (m_param->rc.rateControlMode == X265_RC_ABR)
239
0
            {
240
0
                x265_log(m_param, X265_LOG_WARNING, "VBV maxrate unspecified, assuming CBR\n");
241
0
                m_param->rc.vbvMaxBitrate = m_param->rc.bitrate;
242
0
            }
243
0
            else
244
0
            {
245
0
                x265_log(m_param, X265_LOG_WARNING, "VBV bufsize set but maxrate unspecified, ignored\n");
246
0
                m_param->rc.vbvBufferSize = 0;
247
0
            }
248
0
        }
249
0
        else if (m_param->rc.vbvMaxBitrate < m_param->rc.bitrate &&
250
0
                 m_param->rc.rateControlMode == X265_RC_ABR)
251
0
        {
252
0
            x265_log(m_param, X265_LOG_WARNING, "max bitrate less than average bitrate, assuming CBR\n");
253
0
            m_param->rc.bitrate = m_param->rc.vbvMaxBitrate;
254
0
        }
255
0
    }
256
698
    else if (m_param->rc.vbvMaxBitrate)
257
0
    {
258
0
        x265_log(m_param, X265_LOG_WARNING, "VBV maxrate specified, but no bufsize, ignored\n");
259
0
        m_param->rc.vbvMaxBitrate = 0;
260
0
    }
261
698
    m_isVbv = m_param->rc.vbvMaxBitrate > 0 && m_param->rc.vbvBufferSize > 0;
262
698
    if (m_param->vbvBufferEnd && !m_isVbv)
263
0
    {
264
0
        x265_log(m_param, X265_LOG_WARNING, "vbv-end requires VBV parameters, ignored\n");
265
0
        m_param->vbvBufferEnd = 0;
266
0
    }
267
698
    if (m_param->bEmitHRDSEI && !m_isVbv)
268
0
    {
269
0
        x265_log(m_param, X265_LOG_WARNING, "NAL HRD parameters require VBV parameters, ignored\n");
270
0
        m_param->bEmitHRDSEI = 0;
271
0
    }
272
698
    m_isCbr = m_param->rc.rateControlMode == X265_RC_ABR && m_isVbv && m_param->rc.vbvMaxBitrate <= m_param->rc.bitrate;
273
698
    if (m_param->rc.bStrictCbr && !m_isCbr)
274
0
    {
275
0
        x265_log(m_param, X265_LOG_WARNING, "strict CBR set without CBR mode, ignored\n");
276
0
        m_param->rc.bStrictCbr = 0;
277
0
    }
278
698
    if(m_param->rc.bStrictCbr)
279
0
        m_rateTolerance = 0.7;
280
281
698
    m_bframeBits = 0;
282
698
    m_leadingNoBSatd = 0;
283
698
    m_ipOffset = 6.0 * X265_LOG2(m_param->rc.ipFactor);
284
698
    m_pbOffset = 6.0 * X265_LOG2(m_param->rc.pbFactor);
285
286
48.8k
    for (int i = 0; i < QP_MAX_MAX; i++)
287
48.1k
        m_qpToEncodedBits[i] = 0;
288
289
    /* Adjust the first frame in order to stabilize the quality level compared to the rest */
290
5.55k
#define ABR_INIT_QP_MIN (24)
291
698
#define ABR_INIT_QP_MAX (37)
292
698
#define ABR_INIT_QP_GRAIN_MAX (33)
293
698
#define ABR_SCENECUT_INIT_QP_MIN (12)
294
3.52k
#define CRF_INIT_QP (int)m_param->rc.rfConstant
295
2.79k
    for (int i = 0; i < 3; i++)
296
2.09k
    {
297
2.09k
        m_lastQScaleFor[i] = x265_qp2qScale(m_param->rc.rateControlMode == X265_RC_CRF ? CRF_INIT_QP : ABR_INIT_QP_MIN);
298
2.09k
        m_lmin[i] = x265_qp2qScale(m_param->rc.qpMin);
299
2.09k
        m_lmax[i] = x265_qp2qScale(m_param->rc.qpMax);
300
2.09k
    }
301
302
698
    if (m_param->rc.rateControlMode == X265_RC_CQP)
303
195
    {
304
195
        if (m_qp && !m_param->bLossless)
305
0
        {
306
0
            m_qpConstant[P_SLICE] = m_qp;
307
0
            m_qpConstant[I_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_qp - m_ipOffset + 0.5));
308
0
            m_qpConstant[B_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_qp + m_pbOffset + 0.5));
309
0
        }
310
195
        else
311
195
        {
312
195
            m_qpConstant[P_SLICE] = m_qpConstant[I_SLICE] = m_qpConstant[B_SLICE] = m_qp;
313
195
        }
314
195
    }
315
316
    /* qpstep - value set as encoder specific */
317
698
    m_lstep = pow(2, m_param->rc.qpStep / 6.0);
318
319
2.09k
    for (int i = 0; i < 2; i++)
320
1.39k
        m_cuTreeStats.qpBuffer[i] = NULL;
321
698
}
322
323
bool RateControl::init(const SPS& sps)
324
698
{
325
698
    if (m_isVbv && !m_initVbv)
326
0
    {
327
        /* We don't support changing the ABR bitrate right now,
328
         * so if the stream starts as CBR, keep it CBR. */
329
0
        if (m_param->rc.vbvBufferSize < (int)(m_param->rc.vbvMaxBitrate / m_fps))
330
0
        {
331
0
            m_param->rc.vbvBufferSize = (int)(m_param->rc.vbvMaxBitrate / m_fps);
332
0
            x265_log(m_param, X265_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
333
0
                     m_param->rc.vbvBufferSize);
334
0
        }
335
0
        int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;
336
0
        int vbvMaxBitrate = m_param->rc.vbvMaxBitrate * 1000;
337
338
0
        if (m_param->bEmitHRDSEI && !m_param->decoderVbvMaxRate)
339
0
        {
340
0
            const HRDInfo* hrd = &sps.vuiParameters.hrdParameters;
341
0
            vbvBufferSize = hrd->cpbSizeValue << (hrd->cpbSizeScale + CPB_SHIFT);
342
0
            vbvMaxBitrate = hrd->bitRateValue << (hrd->bitRateScale + BR_SHIFT);
343
0
        }
344
0
        m_bufferRate = vbvMaxBitrate / m_fps;
345
0
        m_vbvMaxRate = vbvMaxBitrate;
346
0
        m_bufferSize = vbvBufferSize;
347
0
        m_singleFrameVbv = m_bufferRate * 1.1 > m_bufferSize;
348
349
0
        if (m_param->rc.vbvBufferInit > 1.)
350
0
            m_param->rc.vbvBufferInit = x265_clip3(0.0, 1.0, m_param->rc.vbvBufferInit / m_param->rc.vbvBufferSize);
351
0
        if (m_param->vbvBufferEnd > 1.)
352
0
            m_param->vbvBufferEnd = x265_clip3(0.0, 1.0, m_param->vbvBufferEnd / m_param->rc.vbvBufferSize);
353
0
        if (m_param->vbvEndFrameAdjust > 1.)
354
0
            m_param->vbvEndFrameAdjust = x265_clip3(0.0, 1.0, m_param->vbvEndFrameAdjust);
355
0
        m_param->rc.vbvBufferInit = x265_clip3(0.0, 1.0, X265_MAX(m_param->rc.vbvBufferInit, m_bufferRate / m_bufferSize));
356
0
        m_bufferFillFinal = m_bufferSize * m_param->rc.vbvBufferInit;
357
0
        m_bufferFillActual = m_bufferFillFinal;
358
0
        m_bufferExcess = 0;
359
0
        m_initVbv = true;
360
0
    }
361
362
698
    if (!m_param->bResetZoneConfig && (m_relativeComplexity == NULL))
363
0
    {
364
0
        m_relativeComplexity = X265_MALLOC(double, m_param->reconfigWindowSize);
365
0
        if (m_relativeComplexity == NULL)
366
0
        {
367
0
            x265_log(m_param, X265_LOG_ERROR, "Failed to allocate memory for m_relativeComplexity\n");
368
0
            return false;
369
0
        }
370
0
    }
371
372
698
    m_totalBits = 0;
373
698
    m_encodedBits = 0;
374
698
    m_framesDone = 0;
375
698
    m_residualCost = 0;
376
698
    m_partialResidualCost = 0;
377
698
    m_amortizeFraction = 0.85;
378
698
    m_amortizeFrames = 75;
379
698
    if (m_param->totalFrames && m_param->totalFrames <= 2 * m_fps && m_param->rc.bStrictCbr) /* Strict CBR segment encode */
380
0
    {
381
0
        m_amortizeFraction = 0.85;
382
0
        m_amortizeFrames = m_param->totalFrames / 2;
383
0
    }
384
385
14.6k
    for (int i = 0; i < s_slidingWindowFrames; i++)
386
13.9k
    {
387
13.9k
        m_satdCostWindow[i] = 0;
388
13.9k
        m_encodedBitsWindow[i] = 0;
389
13.9k
    }
390
698
    m_sliderPos = 0;
391
698
    m_isPatternPresent = false;
392
698
    m_numBframesInPattern = 0;
393
394
698
    m_isGrainEnabled = false;
395
698
    if(m_param->rc.bEnableGrain) // tune for grainy content OR equal p-b frame sizes
396
0
        m_isGrainEnabled = true;
397
2.79k
    for (int i = 0; i < 3; i++)
398
2.09k
        m_lastQScaleFor[i] = x265_qp2qScale(m_param->rc.rateControlMode == X265_RC_CRF ? CRF_INIT_QP : ABR_INIT_QP_MIN);
399
698
    m_avgPFrameQp = 0 ;
400
401
    /* 720p videos seem to be a good cutoff for cplxrSum */
402
698
    double tuneCplxFactor = (m_ncu > 3600 && m_param->rc.cuTree && !m_param->rc.hevcAq) ? 2.5 : m_param->rc.hevcAq ? 1.5 : m_isGrainEnabled ? 1.9 : 1.0;
403
404
    /* estimated ratio that produces a reasonable QP for the first I-frame */
405
698
    m_cplxrSum = .01 * pow(7.0e5, m_qCompress) * pow(m_ncu, 0.5) * tuneCplxFactor;
406
698
    m_wantedBitsWindow = m_bitrate * m_frameDuration;
407
698
    m_accumPNorm = .01;
408
698
    m_accumPQp = (m_param->rc.rateControlMode == X265_RC_CRF ? CRF_INIT_QP : ABR_INIT_QP_MIN) * m_accumPNorm;
409
410
411
    /* Frame Predictors used in vbv */
412
698
    initFramePredictors();
413
698
    if (!m_statFileOut && (m_param->rc.bStatWrite || m_param->rc.bStatRead))
414
0
    {
415
        /* If the user hasn't defined the stat filename, use the default value */
416
0
        const char *fileName = m_param->rc.statFileName;
417
0
        if (!fileName)
418
0
            fileName = s_defaultStatFileName;
419
        /* Load stat file and init 2pass algo */
420
0
        if (m_param->rc.bStatRead)
421
0
        {
422
0
            m_expectedBitsSum = 0;
423
0
            char *p, *statsIn, *statsBuf;
424
            /* read 1st pass stats */
425
0
            statsIn = statsBuf = x265_slurp_file(fileName);
426
0
            if (!statsBuf)
427
0
                return false;
428
0
            if (m_param->rc.cuTree)
429
0
            {
430
0
                char *tmpFile = strcatFilename(fileName, ".cutree");
431
0
                if (!tmpFile)
432
0
                    return false;
433
0
                m_cutreeStatFileIn = x265_fopen(tmpFile, "rb");
434
0
                X265_FREE(tmpFile);
435
0
                if (!m_cutreeStatFileIn)
436
0
                {
437
0
                    x265_log_file(m_param, X265_LOG_ERROR, "can't open stats file %s.cutree\n", fileName);
438
0
                    return false;
439
0
                }
440
0
            }
441
442
            /* check whether 1st pass options were compatible with current options */
443
0
            if (strncmp(statsBuf, "#options:", 9))
444
0
            {
445
0
                x265_log(m_param, X265_LOG_ERROR,"options list in stats file not valid\n");
446
0
                return false;
447
0
            }
448
0
            {
449
0
                int i, j, m;
450
0
                uint32_t k , l;
451
0
                bool bErr = false;
452
0
                char *opts = statsBuf;
453
0
                statsIn = strchr(statsBuf, '\n');
454
0
                if (!statsIn)
455
0
                {
456
0
                    x265_log(m_param, X265_LOG_ERROR, "Malformed stats file\n");
457
0
                    return false;
458
0
                }
459
0
                *statsIn = '\0';
460
0
                statsIn++;
461
0
                if ((p = strstr(opts, " input-res=")) == 0 || sscanf(p, " input-res=%dx%d", &i, &j) != 2)
462
0
                {
463
0
                    x265_log(m_param, X265_LOG_ERROR, "Resolution specified in stats file not valid\n");
464
0
                    return false;
465
0
                }
466
0
                if ((p = strstr(opts, " fps=")) == 0 || sscanf(p, " fps=%u/%u", &k, &l) != 2)
467
0
                {
468
0
                    x265_log(m_param, X265_LOG_ERROR, "fps specified in stats file not valid\n");
469
0
                    return false;
470
0
                }
471
0
                if (((p = strstr(opts, " vbv-maxrate=")) == 0 || sscanf(p, " vbv-maxrate=%d", &m) != 1) && m_param->rc.rateControlMode == X265_RC_CRF)
472
0
                {
473
0
                    x265_log(m_param, X265_LOG_ERROR, "Constant rate-factor is incompatible with 2pass without vbv-maxrate in the previous pass\n");
474
0
                    return false;
475
0
                }
476
0
                if (k != m_param->fpsNum || l != m_param->fpsDenom)
477
0
                {
478
0
                    x265_log(m_param, X265_LOG_ERROR, "fps mismatch with 1st pass (%u/%u vs %u/%u)\n",
479
0
                              m_param->fpsNum, m_param->fpsDenom, k, l);
480
0
                    return false;
481
0
                }
482
0
                if (m_param->analysisMultiPassRefine)
483
0
                {
484
0
                    p = strstr(opts, "ref=");
485
0
                    sscanf(p, "ref=%d", &i);
486
0
                    if (i > m_param->maxNumReferences)
487
0
                    {
488
0
                        x265_log(m_param, X265_LOG_ERROR, "maxNumReferences cannot be less than 1st pass (%d vs %d)\n",
489
0
                            i, m_param->maxNumReferences);
490
0
                        return false;
491
0
                    }
492
0
                }
493
0
                if (m_param->analysisMultiPassRefine || m_param->analysisMultiPassDistortion)
494
0
                {
495
0
                    p = strstr(opts, "ctu=");
496
0
                    sscanf(p, "ctu=%u", &k);
497
0
                    if (k != m_param->maxCUSize)
498
0
                    {
499
0
                        x265_log(m_param, X265_LOG_ERROR, "maxCUSize mismatch with 1st pass (%u vs %u)\n",
500
0
                            k, m_param->maxCUSize);
501
0
                        return false;
502
0
                    }
503
0
                }
504
0
                CMP_OPT_FIRST_PASS("bitdepth", m_param->internalBitDepth);
505
0
                CMP_OPT_FIRST_PASS("weightp", m_param->bEnableWeightedPred);
506
0
                CMP_OPT_FIRST_PASS("bframes", m_param->bframes);
507
0
                CMP_OPT_FIRST_PASS("b-pyramid", m_param->bBPyramid);
508
0
                CMP_OPT_FIRST_PASS("open-gop", m_param->bOpenGOP);
509
0
                CMP_OPT_FIRST_PASS(" keyint", m_param->keyframeMax);
510
0
                CMP_OPT_FIRST_PASS("scenecut", m_param->scenecutThreshold);
511
0
                CMP_OPT_FIRST_PASS("intra-refresh", m_param->bIntraRefresh);
512
0
                CMP_OPT_FIRST_PASS("frame-dup", m_param->bEnableFrameDuplication);
513
0
                if (m_param->bMultiPassOptRPS)
514
0
                {
515
0
                    CMP_OPT_FIRST_PASS("multi-pass-opt-rps", m_param->bMultiPassOptRPS);
516
0
                    CMP_OPT_FIRST_PASS("repeat-headers", m_param->bRepeatHeaders);
517
0
                    CMP_OPT_FIRST_PASS("min-keyint", m_param->keyframeMin);
518
0
                }
519
520
0
                if ((p = strstr(opts, "b-adapt=")) != 0 && sscanf(p, "b-adapt=%d", &i) && i >= X265_B_ADAPT_NONE && i <= X265_B_ADAPT_TRELLIS)
521
0
                {
522
0
                    m_param->bFrameAdaptive = i;
523
0
                }
524
0
                else if (m_param->bframes)
525
0
                {
526
0
                    x265_log(m_param, X265_LOG_ERROR, "b-adapt method specified in stats file not valid\n");
527
0
                    return false;
528
0
                }
529
530
0
                if ((p = strstr(opts, "rc-lookahead=")) != 0 && sscanf(p, "rc-lookahead=%d", &i))
531
0
                    m_param->lookaheadDepth = i;
532
0
            }
533
            /* find number of pics */
534
0
            p = statsIn;
535
0
            int numEntries;
536
0
            for (numEntries = -1; p; numEntries++)
537
0
                p = strchr(p + 1, ';');
538
0
            if (!numEntries)
539
0
            {
540
0
                x265_log(m_param, X265_LOG_ERROR, "empty stats file\n");
541
0
                return false;
542
0
            }
543
0
            m_numEntries = numEntries;
544
545
0
            if (m_param->totalFrames < m_numEntries && m_param->totalFrames > 0)
546
0
            {
547
0
                x265_log(m_param, X265_LOG_WARNING, "2nd pass has fewer frames than 1st pass (%d vs %d)\n",
548
0
                         m_param->totalFrames, m_numEntries);
549
0
            }
550
0
            if (m_param->totalFrames > m_numEntries && !m_param->bEnableFrameDuplication)
551
0
            {
552
0
                x265_log(m_param, X265_LOG_ERROR, "2nd pass has more frames than 1st pass (%d vs %d)\n",
553
0
                         m_param->totalFrames, m_numEntries);
554
0
                return false;
555
0
            }
556
557
0
            m_rce2Pass = X265_MALLOC(RateControlEntry, m_numEntries);
558
0
            if (!m_rce2Pass)
559
0
            {
560
0
                 x265_log(m_param, X265_LOG_ERROR, "Rce Entries for 2 pass cannot be allocated\n");
561
0
                 return false;
562
0
            }
563
0
            m_encOrder = X265_MALLOC(int, m_numEntries);
564
0
            if (!m_encOrder)
565
0
            {
566
0
                x265_log(m_param, X265_LOG_ERROR, "Encode order for 2 pass cannot be allocated\n");
567
0
                return false;
568
0
            }
569
            /* init all to skipped p frames */
570
0
            for (int i = 0; i < m_numEntries; i++)
571
0
            {
572
0
                RateControlEntry *rce = &m_rce2Pass[i];
573
0
                rce->sliceType = P_SLICE;
574
0
                rce->qScale = rce->newQScale = x265_qp2qScale(20);
575
0
                rce->miscBits = m_ncu + 10;
576
0
                rce->newQp = 0;
577
0
            }
578
            /* read stats */
579
0
            p = statsIn;
580
0
            double totalQpAq = 0;
581
0
            for (int i = 0; i < m_numEntries; i++)
582
0
            {
583
0
                RateControlEntry *rce;
584
0
                int frameNumber;
585
0
                int encodeOrder;
586
0
                char picType;
587
0
                int e;
588
0
                char *next;
589
0
                double qpRc, qpAq, qNoVbv, qRceq;
590
0
                next = strstr(p, ";");
591
0
                if (next)
592
0
                    *next++ = 0;
593
0
                e = sscanf(p, " in:%d out:%d", &frameNumber, &encodeOrder);
594
0
                if (frameNumber < 0 || frameNumber >= m_numEntries)
595
0
                {
596
0
                    x265_log(m_param, X265_LOG_ERROR, "bad frame number (%d) at stats line %d\n", frameNumber, i);
597
0
                    return false;
598
0
                }
599
0
                rce = &m_rce2Pass[encodeOrder];
600
0
                m_encOrder[frameNumber] = encodeOrder;
601
0
                if (!m_param->bMultiPassOptRPS)
602
0
                {
603
0
                    e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf",
604
0
                        &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,
605
0
                        &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,
606
0
                        &rce->skipCuCount);
607
0
                }
608
0
                else
609
0
                {
610
0
                    char deltaPOC[128];
611
0
                    char bUsed[40];
612
0
                    memset(deltaPOC, 0, sizeof(deltaPOC));
613
0
                    memset(bUsed, 0, sizeof(bUsed));
614
0
                    e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf nump:%d numnegp:%d numposp:%d deltapoc:%s bused:%s",
615
0
                        &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,
616
0
                        &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,
617
0
                        &rce->skipCuCount, &rce->rpsData.numberOfPictures, &rce->rpsData.numberOfNegativePictures, &rce->rpsData.numberOfPositivePictures, deltaPOC, bUsed);
618
0
                    splitdeltaPOC(deltaPOC, rce);
619
0
                    splitbUsed(bUsed, rce);
620
0
                    rce->rpsIdx = -1;
621
0
                }
622
0
                rce->keptAsRef = true;
623
0
                rce->isIdr = false;
624
0
                if (picType == 'b' || picType == 'p')
625
0
                    rce->keptAsRef = false;
626
0
                if (picType == 'I')
627
0
                    rce->isIdr = true;
628
0
                if (picType == 'I' || picType == 'i')
629
0
                    rce->sliceType = I_SLICE;
630
0
                else if (picType == 'P' || picType == 'p')
631
0
                    rce->sliceType = P_SLICE;
632
0
                else if (picType == 'B' || picType == 'b')
633
0
                    rce->sliceType = B_SLICE;
634
0
                else
635
0
                    e = -1;
636
0
                if (e < 10)
637
0
                {
638
0
                    x265_log(m_param, X265_LOG_ERROR, "statistics are damaged at line %d, parser out=%d\n", i, e);
639
0
                    return false;
640
0
                }
641
0
                rce->qScale = rce->newQScale = x265_qp2qScale(qpRc);
642
0
                totalQpAq += qpAq;
643
0
                rce->qpNoVbv = qNoVbv;
644
0
                rce->qpaRc = qpRc;
645
0
                rce->qpAq = qpAq;
646
0
                rce->qRceq = qRceq;
647
0
                p = next;
648
0
            }
649
0
            X265_FREE(statsBuf);
650
0
            if (m_param->rc.rateControlMode != X265_RC_CQP)
651
0
            {
652
0
                m_start = 0;
653
0
                m_isQpModified = true;
654
0
                if (!initPass2())
655
0
                    return false;
656
0
            } /* else we're using constant quant, so no need to run the bitrate allocation */
657
0
        }
658
        /* Open output file */
659
        /* If input and output files are the same, output to a temp file
660
         * and move it to the real name only when it's complete */
661
0
        if (m_param->rc.bStatWrite)
662
0
        {
663
0
            char *p, *statFileTmpname;
664
0
            statFileTmpname = strcatFilename(fileName, ".temp");
665
0
            if (!statFileTmpname)
666
0
                return false;
667
0
            m_statFileOut = x265_fopen(statFileTmpname, "wb");
668
0
            X265_FREE(statFileTmpname);
669
0
            if (!m_statFileOut)
670
0
            {
671
0
                x265_log_file(m_param, X265_LOG_ERROR, "can't open stats file %s.temp\n", fileName);
672
0
                return false;
673
0
            }
674
0
            p = x265_param2string(m_param, sps.conformanceWindow.rightOffset, sps.conformanceWindow.bottomOffset);
675
0
            if (p)
676
0
                fprintf(m_statFileOut, "#options: %s\n", p);
677
0
            X265_FREE(p);
678
0
            if (m_param->rc.cuTree && !m_param->rc.bStatRead)
679
0
            {
680
0
                statFileTmpname = strcatFilename(fileName, ".cutree.temp");
681
0
                if (!statFileTmpname)
682
0
                    return false;
683
0
                m_cutreeStatFileOut = x265_fopen(statFileTmpname, "wb");
684
0
                X265_FREE(statFileTmpname);
685
0
                if (!m_cutreeStatFileOut)
686
0
                {
687
0
                    x265_log_file(m_param, X265_LOG_ERROR, "can't open mbtree stats file %s.cutree.temp\n", fileName);
688
0
                    return false;
689
0
                }
690
0
            }
691
0
        }
692
0
        if (m_param->rc.cuTree)
693
0
        {
694
0
            if (m_param->rc.qgSize == 8)
695
0
            {
696
0
                m_cuTreeStats.qpBuffer[0] = X265_MALLOC(uint16_t, m_ncu * 4 * sizeof(uint16_t));
697
0
                if (m_param->bBPyramid && m_param->rc.bStatRead)
698
0
                    m_cuTreeStats.qpBuffer[1] = X265_MALLOC(uint16_t, m_ncu * 4 * sizeof(uint16_t));
699
0
            }
700
0
            else
701
0
            {
702
0
                m_cuTreeStats.qpBuffer[0] = X265_MALLOC(uint16_t, m_ncu * sizeof(uint16_t));
703
0
                if (m_param->bBPyramid && m_param->rc.bStatRead)
704
0
                    m_cuTreeStats.qpBuffer[1] = X265_MALLOC(uint16_t, m_ncu * sizeof(uint16_t));
705
0
            }
706
0
            m_cuTreeStats.qpBufPos = -1;
707
0
        }
708
0
    }
709
698
    return true;
710
698
}
711
712
void RateControl::reconfigureRC()
713
0
{
714
0
    if (m_isVbv)
715
0
    {
716
0
        m_param->rc.vbvBufferSize = x265_clip3(0, 2000000, m_param->rc.vbvBufferSize);
717
0
        m_param->rc.vbvMaxBitrate = x265_clip3(0, 2000000, m_param->rc.vbvMaxBitrate);
718
0
        if (m_param->reconfigWindowSize)
719
0
            m_param->rc.vbvMaxBitrate = (int)(m_param->rc.vbvMaxBitrate * (double)(m_fps / m_param->reconfigWindowSize));
720
0
        if (m_param->rc.vbvMaxBitrate < m_param->rc.bitrate &&
721
0
            m_param->rc.rateControlMode == X265_RC_ABR)
722
0
        {
723
0
            x265_log(m_param, X265_LOG_WARNING, "max bitrate less than average bitrate, assuming CBR\n");
724
0
            m_param->rc.bitrate = m_param->rc.vbvMaxBitrate;
725
0
        }
726
727
0
        if (m_param->rc.vbvBufferSize < (int)(m_param->rc.vbvMaxBitrate / m_fps))
728
0
        {
729
0
            m_param->rc.vbvBufferSize = (int)(m_param->rc.vbvMaxBitrate / m_fps);
730
0
            x265_log(m_param, X265_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
731
0
                m_param->rc.vbvBufferSize);
732
0
        }
733
0
        int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;
734
0
        int vbvMaxBitrate = m_param->rc.vbvMaxBitrate * 1000;
735
0
        m_bufferRate = vbvMaxBitrate / m_fps;
736
0
        m_vbvMaxRate = vbvMaxBitrate;
737
0
        m_bufferSize = vbvBufferSize;
738
0
        m_singleFrameVbv = m_bufferRate * 1.1 > m_bufferSize;
739
0
    }
740
0
    if (m_param->rc.rateControlMode == X265_RC_CRF)
741
0
    {
742
503
        #define CRF_INIT_QP (int)m_param->rc.rfConstant
743
0
        m_param->rc.bitrate = 0;
744
0
        double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
745
0
        double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;
746
0
        m_rateFactorConstant = pow(baseCplx, 1 - m_qCompress) /
747
0
            x265_qp2qScale(m_param->rc.rfConstant + mbtree_offset);
748
0
        if (m_param->rc.rfConstantMax)
749
0
        {
750
0
            m_rateFactorMaxIncrement = m_param->rc.rfConstantMax - m_param->rc.rfConstant;
751
0
            if (m_rateFactorMaxIncrement <= 0)
752
0
            {
753
0
                x265_log(m_param, X265_LOG_WARNING, "CRF max must be greater than CRF\n");
754
0
                m_rateFactorMaxIncrement = 0;
755
0
            }
756
0
        }
757
0
        if (m_param->rc.rfConstantMin)
758
0
            m_rateFactorMaxDecrement = m_param->rc.rfConstant - m_param->rc.rfConstantMin;
759
0
    }
760
0
    if (m_param->rc.rateControlMode == X265_RC_CQP)
761
0
    {
762
0
        m_qp = m_param->rc.qp;
763
0
        if (m_qp && !m_param->bLossless)
764
0
        {
765
0
            m_qpConstant[P_SLICE] = m_qp;
766
0
            m_qpConstant[I_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_qp - m_ipOffset + 0.5));
767
0
            m_qpConstant[B_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_qp + m_pbOffset + 0.5));
768
0
        }
769
0
        else
770
0
        {
771
0
            m_qpConstant[P_SLICE] = m_qpConstant[I_SLICE] = m_qpConstant[B_SLICE] = m_qp;
772
0
        }
773
0
    }
774
0
    m_bitrate = (double)m_param->rc.bitrate * 1000;
775
0
}
776
777
void RateControl::initHRD(SPS& sps)
778
0
{
779
0
    int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;
780
0
    int vbvMaxBitrate = m_param->rc.vbvMaxBitrate * 1000;
781
782
    // Init HRD
783
0
    HRDInfo* hrd = &sps.vuiParameters.hrdParameters;
784
0
    hrd->cbrFlag = m_isCbr;
785
0
    if (m_param->reconfigWindowSize) {
786
0
        hrd->cbrFlag = 0;
787
0
        vbvMaxBitrate = m_param->decoderVbvMaxRate * 1000;
788
0
    }
789
790
    // normalize HRD size and rate to the value / scale notation
791
0
    hrd->bitRateScale = x265_clip3(0, 15, calcScale(vbvMaxBitrate) - BR_SHIFT);
792
0
    hrd->bitRateValue = (vbvMaxBitrate >> (hrd->bitRateScale + BR_SHIFT));
793
794
0
    hrd->cpbSizeScale = x265_clip3(0, 15, calcScale(vbvBufferSize) - CPB_SHIFT);
795
0
    hrd->cpbSizeValue = (vbvBufferSize >> (hrd->cpbSizeScale + CPB_SHIFT));
796
0
    int bitRateUnscale = hrd->bitRateValue << (hrd->bitRateScale + BR_SHIFT);
797
0
    int cpbSizeUnscale = hrd->cpbSizeValue << (hrd->cpbSizeScale + CPB_SHIFT);
798
799
    // arbitrary
800
0
    #define MAX_DURATION 0.5
801
802
0
    TimingInfo *time = &sps.vuiParameters.timingInfo;
803
0
    int maxCpbOutputDelay = (int)(X265_MIN(m_param->keyframeMax * MAX_DURATION * time->timeScale / time->numUnitsInTick, INT_MAX));
804
0
    int maxDpbOutputDelay = (int)(sps.maxDecPicBuffering * MAX_DURATION * time->timeScale / time->numUnitsInTick);
805
0
    int maxDelay = (int)(90000.0 * cpbSizeUnscale / bitRateUnscale + 0.5);
806
807
0
    hrd->initialCpbRemovalDelayLength = 2 + x265_clip3(4, 22, 32 - calcLength(maxDelay));
808
0
    hrd->cpbRemovalDelayLength = x265_clip3(4, 31, 32 - calcLength(maxCpbOutputDelay));
809
0
    hrd->dpbOutputDelayLength = x265_clip3(4, 31, 32 - calcLength(maxDpbOutputDelay));
810
811
0
    #undef MAX_DURATION
812
0
}
813
814
bool RateControl::analyseABR2Pass(uint64_t allAvailableBits)
815
0
{
816
0
    double rateFactor, stepMult;
817
0
    double qBlur = m_param->rc.qblur;
818
0
    double cplxBlur = m_param->rc.complexityBlur;
819
0
    const int filterSize = (int)(qBlur * 4) | 1;
820
0
    double expectedBits;
821
0
    double *qScale, *blurredQscale;
822
0
    double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
823
0
    double clippedDuration = CLIP_DURATION(m_frameDuration) / BASE_FRAME_DURATION;
824
    /* Blur complexities, to reduce local fluctuation of QP.
825
     * We don't blur the QPs directly, because then one very simple frame
826
     * could drag down the QP of a nearby complex frame and give it more
827
     * bits than intended. */
828
0
    for (int i = 0; i < m_numEntries; i++)
829
0
    {
830
0
        double weightSum = 0;
831
0
        double cplxSum = 0;
832
0
        double weight = 1.0;
833
0
        double gaussianWeight;
834
        /* weighted average of cplx of future frames */
835
0
        for (int j = 1; j < cplxBlur * 2 && j < m_numEntries - i; j++)
836
0
        {
837
0
            int index = i+j;
838
0
            RateControlEntry *rcj = &m_rce2Pass[index];
839
0
            weight *= 1 - pow(rcj->iCuCount / m_ncu, 2);
840
0
            if (weight < 0.0001)
841
0
                break;
842
0
            gaussianWeight = weight * exp(-j * j / 200.0);
843
0
            weightSum += gaussianWeight;
844
0
            cplxSum += gaussianWeight * (qScale2bits(rcj, 1) - rcj->miscBits) / clippedDuration;
845
0
        }
846
        /* weighted average of cplx of past frames */
847
0
        weight = 1.0;
848
0
        for (int j = 0; j <= cplxBlur * 2 && j <= i; j++)
849
0
        {
850
0
            int index = i-j;
851
0
            RateControlEntry *rcj = &m_rce2Pass[index];
852
0
            gaussianWeight = weight * exp(-j * j / 200.0);
853
0
            weightSum += gaussianWeight;
854
0
            cplxSum += gaussianWeight * (qScale2bits(rcj, 1) - rcj->miscBits) / clippedDuration;
855
0
            weight *= 1 - pow(rcj->iCuCount / m_ncu, 2);
856
0
            if (weight < .0001)
857
0
                break;
858
0
        }
859
0
        m_rce2Pass[i].blurredComplexity= cplxSum / weightSum;
860
0
    }
861
0
    CHECKED_MALLOC(qScale, double, m_numEntries);
862
0
    if (filterSize > 1)
863
0
    {
864
0
        CHECKED_MALLOC(blurredQscale, double, m_numEntries);
865
0
    }
866
0
    else
867
0
        blurredQscale = qScale;
868
869
    /* Search for a factor which, when multiplied by the RCEQ values from
870
     * each frame, adds up to the desired total size.
871
     * There is no exact closed-form solution because of VBV constraints and
872
     * because qscale2bits is not invertible, but we can start with the simple
873
     * approximation of scaling the 1st pass by the ratio of bitrates.
874
     * The search range is probably overkill, but speed doesn't matter here. */
875
0
    expectedBits = 1;
876
0
    for (int i = 0; i < m_numEntries; i++)
877
0
    {
878
0
        RateControlEntry* rce = &m_rce2Pass[i];
879
0
        double q = getQScale(rce, 1.0);
880
0
        expectedBits += qScale2bits(rce, q);
881
0
        m_lastQScaleFor[rce->sliceType] = q;
882
0
    }
883
0
    stepMult = allAvailableBits / expectedBits;
884
885
0
    rateFactor = 0;
886
0
    for (double step = 1E4 * stepMult; step > 1E-7 * stepMult; step *= 0.5)
887
0
    {
888
0
        expectedBits = 0;
889
0
        rateFactor += step;
890
891
0
        m_lastNonBPictType = -1;
892
0
        m_lastAccumPNorm = 1;
893
0
        m_accumPNorm = 0;
894
895
0
        m_lastQScaleFor[0] = m_lastQScaleFor[1] =
896
0
        m_lastQScaleFor[2] = pow(baseCplx, 1 - m_qCompress) / rateFactor;
897
898
        /* find qscale */
899
0
        for (int i = 0; i < m_numEntries; i++)
900
0
        {
901
0
            RateControlEntry *rce = &m_rce2Pass[i];
902
0
            qScale[i] = getQScale(rce, rateFactor);
903
0
            m_lastQScaleFor[rce->sliceType] = qScale[i];
904
0
        }
905
906
        /* fixed I/B qscale relative to P */
907
0
        for (int i = 0; i < m_numEntries; i++)
908
0
        {
909
0
            qScale[i] = getDiffLimitedQScale(&m_rce2Pass[i], qScale[i]);
910
0
            X265_CHECK(qScale[i] >= 0, "qScale became negative\n");
911
0
        }
912
913
        /* smooth curve */
914
0
        if (filterSize > 1)
915
0
        {
916
0
            X265_CHECK(filterSize % 2 == 1, "filterSize not an odd number\n");
917
0
            for (int i = 0; i < m_numEntries; i++)
918
0
            {
919
0
                double q = 0.0, sum = 0.0;
920
0
                for (int j = 0; j < filterSize; j++)
921
0
                {
922
0
                    int idx = i + j - filterSize / 2;
923
0
                    double d = idx - i;
924
0
                    double coeff = qBlur == 0 ? 1.0 : exp(-d * d / (qBlur * qBlur));
925
0
                    if (idx < 0 || idx >= m_numEntries)
926
0
                        continue;
927
0
                    if (m_rce2Pass[i].sliceType != m_rce2Pass[idx].sliceType)
928
0
                        continue;
929
0
                    q += qScale[idx] * coeff;
930
0
                    sum += coeff;
931
0
                }
932
0
                blurredQscale[i] = q / sum;
933
0
            }
934
0
        }
935
936
        /* find expected bits */
937
0
        for (int i = 0; i < m_numEntries; i++)
938
0
        {
939
0
            RateControlEntry *rce = &m_rce2Pass[i];
940
0
            rce->newQScale = clipQscale(NULL, rce, blurredQscale[i]); // check if needed
941
0
            X265_CHECK(rce->newQScale >= 0, "new Qscale is negative\n");
942
0
            expectedBits += qScale2bits(rce, rce->newQScale);
943
0
        }
944
945
0
        if (expectedBits > allAvailableBits)
946
0
            rateFactor -= step;
947
0
    }
948
949
0
    X265_FREE(qScale);
950
0
    if (filterSize > 1)
951
0
        X265_FREE(blurredQscale);
952
0
    if (m_isVbv)
953
0
    if (!vbv2Pass(allAvailableBits, m_numEntries - 1, 0))
954
0
            return false;
955
0
    expectedBits = countExpectedBits(0, m_numEntries - 1);
956
0
    if (fabs(expectedBits / allAvailableBits - 1.0) > 0.01)
957
0
    {
958
0
        double avgq = 0;
959
0
        for (int i = 0; i < m_numEntries; i++)
960
0
            avgq += m_rce2Pass[i].newQScale;
961
0
        avgq = x265_qScale2qp(avgq / m_numEntries);
962
963
0
        if (expectedBits > allAvailableBits || !m_isVbv)
964
0
            x265_log(m_param, X265_LOG_WARNING, "Error: 2pass curve failed to converge\n");
965
0
        x265_log(m_param, X265_LOG_WARNING, "target: %.2f kbit/s, expected: %.2f kbit/s, avg QP: %.4f\n",
966
0
                 (double)m_param->rc.bitrate,
967
0
                 expectedBits * m_fps / (m_numEntries * 1000.),
968
0
                 avgq);
969
0
        if (expectedBits < allAvailableBits && avgq < m_param->rc.qpMin + 2)
970
0
        {
971
0
            if (m_param->rc.qpMin > 0)
972
0
                x265_log(m_param, X265_LOG_WARNING, "try reducing target bitrate or reducing qp_min (currently %d)\n", m_param->rc.qpMin);
973
0
            else
974
0
                x265_log(m_param, X265_LOG_WARNING, "try reducing target bitrate\n");
975
0
        }
976
0
        else if (expectedBits > allAvailableBits && avgq > m_param->rc.qpMax - 2)
977
0
        {
978
0
            if (m_param->rc.qpMax < QP_MAX_MAX)
979
0
                x265_log(m_param, X265_LOG_WARNING, "try increasing target bitrate or increasing qp_max (currently %d)\n", m_param->rc.qpMax);
980
0
            else
981
0
                x265_log(m_param, X265_LOG_WARNING, "try increasing target bitrate\n");
982
0
        }
983
0
        else if (!(m_2pass && m_isVbv))
984
0
            x265_log(m_param, X265_LOG_WARNING, "internal error\n");
985
0
    }
986
987
0
    return true;
988
989
0
fail:
990
0
    x265_log(m_param, X265_LOG_WARNING, "two-pass ABR initialization failed\n");
991
0
    return false;
992
0
}
993
994
bool RateControl::initPass2()
995
0
{
996
0
    uint64_t allConstBits = 0, allCodedBits = 0;
997
0
    uint64_t allAvailableBits = uint64_t(m_param->rc.bitrate * 1000. * m_numEntries * m_frameDuration);
998
0
    int startIndex, framesCount, endIndex;
999
0
    int fps = X265_MIN(m_param->keyframeMax, (int)(m_fps + 0.5));
1000
0
    startIndex = endIndex = framesCount = 0;
1001
0
    int diffQp = 0;
1002
0
    double targetBits = 0;
1003
0
    double expectedBits = 0;
1004
0
    for (startIndex = m_start, endIndex = m_start; endIndex < m_numEntries; endIndex++)
1005
0
    {
1006
0
        allConstBits += m_rce2Pass[endIndex].miscBits;
1007
0
        allCodedBits += m_rce2Pass[endIndex].coeffBits + m_rce2Pass[endIndex].mvBits;
1008
0
        if (m_param->rc.rateControlMode == X265_RC_CRF)
1009
0
        {
1010
0
            framesCount = endIndex - startIndex + 1;
1011
0
            diffQp += int (m_rce2Pass[endIndex].qpaRc - m_rce2Pass[endIndex].qpNoVbv);
1012
0
            if (framesCount > fps)
1013
0
                diffQp -= int (m_rce2Pass[endIndex - fps].qpaRc - m_rce2Pass[endIndex - fps].qpNoVbv);
1014
0
            if (framesCount >= fps)
1015
0
            {
1016
0
                if (diffQp >= 1)
1017
0
                {
1018
0
                    if (!m_isQpModified && endIndex > fps)
1019
0
                    {
1020
0
                        double factor = 2;
1021
0
                        double step = 0;
1022
0
                        if (endIndex + fps >= m_numEntries)
1023
0
                        {
1024
0
                            m_start = endIndex - (endIndex % fps);
1025
0
                            return true;
1026
0
                        }
1027
0
                        for (int start = endIndex + 1; start <= endIndex + fps && start < m_numEntries; start++)
1028
0
                        {
1029
0
                            RateControlEntry *rce = &m_rce2Pass[start];
1030
0
                            targetBits += qScale2bits(rce, x265_qp2qScale(rce->qpNoVbv));
1031
0
                            expectedBits += qScale2bits(rce, rce->qScale);
1032
0
                        }
1033
0
                        if (expectedBits < 0.95 * targetBits)
1034
0
                        {
1035
0
                            m_isQpModified = true;
1036
0
                            m_isGopReEncoded = true;
1037
0
                            while (endIndex + fps < m_numEntries)
1038
0
                            {
1039
0
                                step = pow(2, factor / 6.0);
1040
0
                                expectedBits = 0;
1041
0
                                for (int start = endIndex + 1; start <= endIndex + fps; start++)
1042
0
                                {
1043
0
                                    RateControlEntry *rce = &m_rce2Pass[start];
1044
0
                                    rce->newQScale = rce->qScale / step;
1045
0
                                    X265_CHECK(rce->newQScale >= 0, "new Qscale is negative\n");
1046
0
                                    expectedBits += qScale2bits(rce, rce->newQScale);
1047
0
                                    rce->newQp = x265_qScale2qp(rce->newQScale);
1048
0
                                }
1049
0
                                if (expectedBits >= targetBits && step > 1)
1050
0
                                    factor *= 0.90;
1051
0
                                else
1052
0
                                    break;
1053
0
                            }
1054
1055
0
                            if (m_isVbv && endIndex + fps < m_numEntries)
1056
0
                                if (!vbv2Pass((uint64_t)targetBits, endIndex + fps, endIndex + 1))
1057
0
                                    return false;
1058
1059
0
                            targetBits = 0;
1060
0
                            expectedBits = 0;
1061
1062
0
                            for (int start = endIndex - fps + 1; start <= endIndex; start++)
1063
0
                            {
1064
0
                                RateControlEntry *rce = &m_rce2Pass[start];
1065
0
                                targetBits += qScale2bits(rce, x265_qp2qScale(rce->qpNoVbv));
1066
0
                            }
1067
0
                            while (1)
1068
0
                            {
1069
0
                                step = pow(2, factor / 6.0);
1070
0
                                expectedBits = 0;
1071
0
                                for (int start = endIndex - fps + 1; start <= endIndex; start++)
1072
0
                                {
1073
0
                                    RateControlEntry *rce = &m_rce2Pass[start];
1074
0
                                    rce->newQScale = rce->qScale * step;
1075
0
                                    X265_CHECK(rce->newQScale >= 0, "new Qscale is negative\n");
1076
0
                                    expectedBits += qScale2bits(rce, rce->newQScale);
1077
0
                                    rce->newQp = x265_qScale2qp(rce->newQScale);
1078
0
                                }
1079
0
                                if (expectedBits > targetBits && step > 1)
1080
0
                                    factor *= 1.1;
1081
0
                                else
1082
0
                                     break;
1083
0
                            }
1084
0
                            if (m_isVbv)
1085
0
                                if (!vbv2Pass((uint64_t)targetBits, endIndex, endIndex - fps + 1))
1086
0
                                    return false;
1087
0
                            diffQp = 0;
1088
0
                            m_reencode = endIndex - fps + 1;
1089
0
                            endIndex = endIndex + fps;
1090
0
                            startIndex = endIndex + 1;
1091
0
                            m_start = startIndex;
1092
0
                            targetBits = expectedBits = 0;
1093
0
                        }
1094
0
                        else
1095
0
                            targetBits = expectedBits = 0;
1096
0
                    }
1097
0
                }
1098
0
                else
1099
0
                    m_isQpModified = false;
1100
0
            }
1101
0
        }
1102
0
    }
1103
1104
0
    if (m_param->rc.rateControlMode == X265_RC_ABR)
1105
0
    {
1106
0
        if (allAvailableBits < allConstBits)
1107
0
        {
1108
0
            x265_log(m_param, X265_LOG_ERROR, "requested bitrate is too low. estimated minimum is %d kbps\n",
1109
0
                     (int)(allConstBits * m_fps / framesCount * 1000.));
1110
0
            return false;
1111
0
        }
1112
0
        if (!analyseABR2Pass(allAvailableBits))
1113
0
            return false;
1114
0
    }
1115
1116
0
    m_start = X265_MAX(m_start, endIndex - fps);
1117
1118
0
    return true;
1119
0
}
1120
1121
bool RateControl::vbv2Pass(uint64_t allAvailableBits, int endPos, int startPos)
1122
0
{
1123
    /* for each interval of bufferFull .. underflow, uniformly increase the qp of all
1124
     * frames in the interval until either buffer is full at some intermediate frame or the
1125
     * last frame in the interval no longer underflows.  Recompute intervals and repeat.
1126
     * Then do the converse to put bits back into overflow areas until target size is met */
1127
1128
0
    double *fills;
1129
0
    double expectedBits = 0;
1130
0
    double adjustment;
1131
0
    double prevBits = 0;
1132
0
    int t0, t1;
1133
0
    double qScaleMin = x265_qp2qScale(m_param->rc.qpMin);
1134
0
    double qScaleMax = x265_qp2qScale(m_param->rc.qpMax);
1135
0
    int iterations = 0 , adjMin, adjMax;
1136
0
    CHECKED_MALLOC(fills, double, m_numEntries + 1);
1137
0
    fills++;
1138
1139
    /* adjust overall stream size */
1140
0
    do
1141
0
    {
1142
0
        iterations++;
1143
0
        prevBits = expectedBits;
1144
1145
0
        if (expectedBits)
1146
0
        {   /* not first iteration */
1147
0
            adjustment = X265_MAX(X265_MIN(expectedBits / allAvailableBits, 0.999), 0.9);
1148
0
            fills[-1] = m_bufferSize * m_param->rc.vbvBufferInit;
1149
0
            t0 = startPos;
1150
            /* fix overflows */
1151
0
            adjMin = 1;
1152
0
            while (adjMin && findUnderflow(fills, &t0, &t1, 1, endPos))
1153
0
            {
1154
0
                adjMin = fixUnderflow(t0, t1, adjustment, qScaleMin, qScaleMax);
1155
0
                t0 = t1;
1156
0
            }
1157
0
        }
1158
1159
0
        fills[-1] = m_bufferSize * (1. - m_param->rc.vbvBufferInit);
1160
0
        t0 = 0;
1161
        /* fix underflows -- should be done after overflow, as we'd better undersize target than underflowing VBV */
1162
0
        adjMax = 1;
1163
0
        while (adjMax && findUnderflow(fills, &t0, &t1, 0, endPos))
1164
0
            adjMax = fixUnderflow(t0, t1, 1.001, qScaleMin, qScaleMax);
1165
0
        expectedBits = countExpectedBits(startPos, endPos);
1166
0
    }
1167
0
    while ((expectedBits < .995 * allAvailableBits) && ((int64_t)(expectedBits+.5) > (int64_t)(prevBits+.5)) && !(m_param->rc.rateControlMode == X265_RC_CRF));
1168
0
    if (!adjMax)
1169
0
        x265_log(m_param, X265_LOG_WARNING, "vbv-maxrate issue, qpmax or vbv-maxrate too low\n");
1170
    /* store expected vbv filling values for tracking when encoding */
1171
0
    for (int i = startPos; i <= endPos; i++)
1172
0
        m_rce2Pass[i].expectedVbv = m_bufferSize - fills[i];
1173
0
    X265_FREE(fills - 1);
1174
0
    return true;
1175
1176
0
fail:
1177
0
    x265_log(m_param, X265_LOG_ERROR, "malloc failure in two-pass VBV init\n");
1178
0
    return false;
1179
0
}
1180
1181
/* In 2pass, force the same frame types as in the 1st pass */
1182
int RateControl::rateControlSliceType(int frameNum)
1183
0
{
1184
0
    if (m_param->rc.bStatRead)
1185
0
    {
1186
0
        if (frameNum >= m_numEntries)
1187
0
        {
1188
            /* We could try to initialize everything required for ABR and
1189
             * adaptive B-frames, but that would be complicated.
1190
             * So just calculate the average QP used so far. */
1191
0
            m_param->rc.qp = (m_accumPQp < 1) ? ABR_INIT_QP_MAX : (int)(m_accumPQp + 0.5);
1192
0
            m_qpConstant[P_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, m_param->rc.qp);
1193
0
            m_qpConstant[I_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_param->rc.qp - m_ipOffset + 0.5));
1194
0
            m_qpConstant[B_SLICE] = x265_clip3(QP_MIN, QP_MAX_MAX, (int)(m_param->rc.qp + m_pbOffset + 0.5));
1195
1196
0
            x265_log(m_param, X265_LOG_ERROR, "2nd pass has more frames than 1st pass (%d)\n", m_numEntries);
1197
0
            x265_log(m_param, X265_LOG_ERROR, "continuing anyway, at constant QP=%d\n", m_param->rc.qp);
1198
0
            if (m_param->bFrameAdaptive)
1199
0
                x265_log(m_param, X265_LOG_ERROR, "disabling adaptive B-frames\n");
1200
1201
0
            m_isAbr = 0;
1202
0
            m_2pass = 0;
1203
0
            m_param->rc.rateControlMode = X265_RC_CQP;
1204
0
            m_param->rc.bStatRead = 0;
1205
0
            m_param->bFrameAdaptive = 0;
1206
0
            m_param->scenecutThreshold = 0;
1207
0
            m_param->bHistBasedSceneCut = 0;
1208
0
            m_param->rc.cuTree = 0;
1209
0
            if (m_param->bframes > 1)
1210
0
                m_param->bframes = 1;
1211
0
            return X265_TYPE_AUTO;
1212
0
        }
1213
0
        int index = m_encOrder[frameNum];
1214
0
        int frameType = m_rce2Pass[index].sliceType == I_SLICE ? (m_rce2Pass[index].isIdr ? X265_TYPE_IDR : X265_TYPE_I)
1215
0
                        : m_rce2Pass[index].sliceType == P_SLICE ? X265_TYPE_P
1216
0
                        : (m_rce2Pass[index].sliceType == B_SLICE && m_rce2Pass[index].keptAsRef ? X265_TYPE_BREF : X265_TYPE_B);
1217
0
        return frameType;
1218
0
    }
1219
0
    else
1220
0
        return X265_TYPE_AUTO;
1221
0
}
1222
1223
void RateControl::initFramePredictors()
1224
698
{
1225
    /* Frame Predictors used in vbv */
1226
3.49k
    for (int i = 0; i < 4; i++)
1227
2.79k
    {
1228
2.79k
        m_pred[i].coeffMin = 1.0 / 4;
1229
2.79k
        m_pred[i].coeff = 1.0;
1230
2.79k
        m_pred[i].count = 1.0;
1231
2.79k
        m_pred[i].decay = 0.5;
1232
2.79k
        m_pred[i].offset = 0.0;
1233
2.79k
    }
1234
698
    m_pred[0].coeff = m_pred[3].coeff = 0.75;
1235
698
    m_pred[0].coeffMin = m_pred[3].coeffMin = 0.75 / 4;
1236
698
    if (m_isGrainEnabled) // when tuned for grain 
1237
0
    {
1238
0
        m_pred[1].coeffMin = 0.75 / 4;
1239
0
        m_pred[1].coeff = 0.75;
1240
0
        m_pred[0].coeff = m_pred[3].coeff = 0.75;
1241
0
        m_pred[0].coeffMin = m_pred[3].coeffMin = 0.75 / 4;
1242
0
    }
1243
698
}
1244
1245
int RateControl::rateControlStart(Frame* curFrame, RateControlEntry* rce, Encoder* enc)
1246
698
{
1247
698
    int orderValue = m_startEndOrder.get();
1248
698
    int startOrdinal = rce->encodeOrder * 2;
1249
1250
698
    while (orderValue < startOrdinal && !m_bTerminated)
1251
0
        orderValue = m_startEndOrder.waitForChange(orderValue);
1252
1253
698
    if (!curFrame)
1254
0
    {
1255
        // faked rateControlStart calls when the encoder is flushing
1256
0
        m_startEndOrder.incr();
1257
0
        return 0;
1258
0
    }
1259
1260
698
    FrameData& curEncData = *curFrame->m_encData;
1261
698
    m_curSlice = curEncData.m_slice;
1262
698
    m_sliceType = m_curSlice->m_sliceType;
1263
698
    rce->sliceType = m_sliceType;
1264
698
    if (!m_2pass)
1265
698
        rce->keptAsRef = IS_REFERENCED(curFrame);
1266
698
    m_predType = getPredictorType(curFrame->m_lowres.sliceType, m_sliceType);
1267
698
    rce->poc = m_curSlice->m_poc;
1268
1269
698
    if (!m_param->bResetZoneConfig && (rce->encodeOrder % m_param->reconfigWindowSize == 0))
1270
0
    {
1271
0
        int index = m_zoneBufferIdx % m_param->rc.zonefileCount;
1272
0
        int read = m_top->zoneReadCount[index].get();
1273
0
        int write = m_top->zoneWriteCount[index].get();
1274
0
        if (write <= read)
1275
0
            write = m_top->zoneWriteCount[index].waitForChange(write);
1276
0
        m_zoneBufferIdx++;
1277
1278
0
        for (int i = 0; i < m_param->rc.zonefileCount; i++)
1279
0
        {
1280
0
            if (m_param->rc.zones[i].startFrame == rce->encodeOrder)
1281
0
            {
1282
0
                m_param->rc.bitrate = m_param->rc.zones[i].zoneParam->rc.bitrate;
1283
0
                m_param->rc.vbvMaxBitrate = m_param->rc.zones[i].zoneParam->rc.vbvMaxBitrate;
1284
0
                memcpy(m_relativeComplexity, m_param->rc.zones[i].relativeComplexity, sizeof(double) * m_param->reconfigWindowSize);
1285
0
                reconfigureRC();
1286
0
                m_isCbr = 1; /* Always vbvmaxrate == bitrate here*/
1287
0
                m_top->zoneReadCount[i].incr();
1288
0
            }
1289
0
        }
1290
0
    }
1291
    
1292
    
1293
698
    if (m_param->bResetZoneConfig)
1294
698
    {
1295
        /* change ratecontrol stats for next zone if specified */
1296
698
        for (int i = 0; i < m_param->rc.zonefileCount; i++)
1297
0
        {
1298
0
            if (m_param->rc.zones[i].startFrame == curFrame->m_encodeOrder)
1299
0
            {
1300
0
                m_param = m_param->rc.zones[i].zoneParam;
1301
0
                reconfigureRC();
1302
0
                init(*m_curSlice->m_sps);
1303
0
            }
1304
0
        }
1305
698
    }
1306
1307
698
    if (m_param->rc.bStatRead)
1308
0
    {
1309
0
        X265_CHECK(rce->poc >= 0 && rce->poc < m_numEntries, "bad encode ordinal\n");
1310
0
        int index = m_encOrder[rce->poc];
1311
0
        copyRceData(rce, &m_rce2Pass[index]);
1312
0
    }
1313
698
    rce->isActive = true;
1314
698
    rce->scenecut = false;
1315
698
    rce->isFadeEnd = curFrame->m_lowres.bIsFadeEnd;
1316
698
    bool isRefFrameScenecut = m_sliceType!= I_SLICE && m_curSlice->m_refFrameList[0][0]->m_lowres.bScenecut;
1317
698
    m_isFirstMiniGop = m_sliceType == I_SLICE ? true : m_isFirstMiniGop;
1318
698
    if (curFrame->m_lowres.bScenecut)
1319
0
    {
1320
0
        m_isSceneTransition = true;
1321
0
        rce->scenecut = true;
1322
0
        m_lastPredictorReset = rce->encodeOrder;
1323
1324
0
        initFramePredictors();
1325
0
    }
1326
698
    else if (m_sliceType != B_SLICE && !isRefFrameScenecut)
1327
698
        m_isSceneTransition = false;
1328
1329
698
    if (rce->encodeOrder < m_lastPredictorReset + m_param->frameNumThreads)
1330
698
    {
1331
698
        rce->rowPreds[0][0].count = 0;
1332
698
    }
1333
1334
698
    rce->bLastMiniGopBFrame = curFrame->m_lowres.bLastMiniGopBFrame;
1335
698
    rce->bufferRate = m_bufferRate;
1336
698
    rce->rowCplxrSum = 0.0;
1337
698
    rce->rowTotalBits = 0;
1338
698
    if (m_isVbv)
1339
0
    {
1340
0
        if (rce->rowPreds[0][0].count == 0)
1341
0
        {
1342
0
            for (int i = 0; i < 3; i++)
1343
0
            {
1344
0
                for (int j = 0; j < 2; j++)
1345
0
                {
1346
0
                    rce->rowPreds[i][j].coeffMin = 0.25 / 4;
1347
0
                    rce->rowPreds[i][j].coeff = 0.25;
1348
0
                    rce->rowPreds[i][j].count = 1.0;
1349
0
                    rce->rowPreds[i][j].decay = 0.5;
1350
0
                    rce->rowPreds[i][j].offset = 0.0;
1351
0
                }
1352
0
            }
1353
0
        }
1354
0
        rce->rowPred[0] = &rce->rowPreds[m_sliceType][0];
1355
0
        rce->rowPred[1] = &rce->rowPreds[m_sliceType][1];
1356
0
        m_predictedBits = m_totalBits;
1357
0
        updateVbvPlan(enc);
1358
0
        rce->bufferFill = m_bufferFill;
1359
0
        rce->vbvEndAdj = false;
1360
0
        if (m_param->vbvBufferEnd && rce->encodeOrder >= m_param->vbvEndFrameAdjust * m_param->totalFrames)
1361
0
        {
1362
0
            rce->vbvEndAdj = true;
1363
0
            rce->targetFill = 0;
1364
0
        }
1365
1366
0
        int mincr = enc->m_vps.ptl.minCrForLevel;
1367
        /* Profiles above Main10 don't require maxAU size check, so just set the maximum to a large value. */
1368
0
        if (enc->m_vps.ptl.profileIdc > Profile::MAIN10 || enc->m_vps.ptl.levelIdc == Level::NONE)
1369
0
            rce->frameSizeMaximum = 1e9;
1370
0
        else
1371
0
        {
1372
            /* The spec has a special case for the first frame. */
1373
0
            if (curFrame->m_lowres.bKeyframe)
1374
0
            {
1375
                /* 1.5 * (Max( PicSizeInSamplesY, fR * MaxLumaSr) + MaxLumaSr * (AuCpbRemovalTime[ 0 ] -AuNominalRemovalTime[ 0 ])) ? MinCr */
1376
0
                double fr = 1. / 300;
1377
0
                int picSizeInSamplesY = m_param->sourceWidth * m_param->sourceHeight;
1378
0
                rce->frameSizeMaximum = 8 * 1.5 * X265_MAX(picSizeInSamplesY, fr * enc->m_vps.ptl.maxLumaSrForLevel) / mincr;
1379
0
            }
1380
0
            else
1381
0
            {
1382
                /* 1.5 * MaxLumaSr * (AuCpbRemovalTime[ n ] - AuCpbRemovalTime[ n - 1 ]) / MinCr */
1383
0
                rce->frameSizeMaximum = 8 * 1.5 * enc->m_vps.ptl.maxLumaSrForLevel * m_frameDuration / mincr;
1384
0
            }
1385
0
            rce->frameSizeMaximum *= m_param->maxAUSizeFactor;
1386
0
        }
1387
0
    }
1388
698
    if (!m_isAbr && m_2pass && m_param->rc.rateControlMode == X265_RC_CRF)
1389
0
    {
1390
0
        rce->qpPrev = x265_qScale2qp(rce->qScale);
1391
0
        rce->qScale = rce->newQScale;
1392
0
        rce->qpaRc = curEncData.m_avgQpRc = curEncData.m_avgQpAq = x265_qScale2qp(rce->newQScale);
1393
0
        m_qp = int(rce->qpaRc + 0.5);
1394
0
        rce->frameSizePlanned = qScale2bits(rce, rce->qScale);
1395
0
        m_framesDone++;
1396
0
        return m_qp;
1397
0
    }
1398
1399
698
    if (m_isAbr || m_2pass) // ABR,CRF
1400
503
    {
1401
503
        if (m_isAbr || m_isVbv)
1402
503
        {
1403
503
            m_currentSatd = curFrame->m_lowres.satdCost >> (X265_DEPTH - 8);
1404
            /* Update rce for use in rate control VBV later */
1405
503
            rce->lastSatd = m_currentSatd;
1406
503
            X265_CHECK(rce->lastSatd, "satdcost cannot be zero\n");
1407
            /* Detect a pattern for B frames with same SATDcost to identify a series of static frames
1408
             * and the P frame at the end of the series marks a possible case for ABR reset logic */
1409
503
            if (m_param->bframes)
1410
0
            {
1411
0
                if (m_sliceType != B_SLICE && m_numBframesInPattern > m_param->bframes)
1412
0
                {
1413
0
                    m_isPatternPresent = true;
1414
0
                }
1415
0
                else if (m_sliceType == B_SLICE && !IS_REFERENCED(curFrame))
1416
0
                {
1417
0
                    if (m_currentSatd != m_lastBsliceSatdCost && !rce->bLastMiniGopBFrame)
1418
0
                    {
1419
0
                        m_isPatternPresent = false;
1420
0
                        m_lastBsliceSatdCost = m_currentSatd;
1421
0
                        m_numBframesInPattern = 0;
1422
0
                    }
1423
0
                    else if (m_currentSatd == m_lastBsliceSatdCost)
1424
0
                        m_numBframesInPattern++;
1425
0
                }
1426
0
            }
1427
503
            if (rce->isFadeEnd)
1428
0
                m_isPatternPresent = true;
1429
503
        }
1430
        /* For a scenecut that occurs within the mini-gop, enable scene transition
1431
         * switch until the next mini-gop to ensure a min qp for all the frames within 
1432
         * the scene-transition mini-gop */
1433
1434
503
        double q = x265_qScale2qp(rateEstimateQscale(curFrame, rce));
1435
503
        q = x265_clip3((double)m_param->rc.qpMin, (double)m_param->rc.qpMax, q);
1436
503
        m_qp = int(q + 0.5);
1437
503
        q = m_isGrainEnabled ? m_qp : q;
1438
503
        rce->qpaRc = curEncData.m_avgQpRc = curEncData.m_avgQpAq = q;
1439
        /* copy value of lastRceq into thread local rce struct *to be used in RateControlEnd() */
1440
503
        rce->qRceq = m_lastRceq;
1441
503
        accumPQpUpdate();
1442
503
        curFrame->m_rcData->cumulativePQp = m_accumPQp;
1443
503
        curFrame->m_rcData->cumulativePNorm = m_accumPNorm;
1444
2.01k
        for (int i = 0; i < 3; i++)
1445
1.50k
            curFrame->m_rcData->lastQScaleFor[i] = m_lastQScaleFor[i];
1446
503
        curFrame->m_rcData->shortTermCplxSum = m_shortTermCplxSum;
1447
503
        curFrame->m_rcData->shortTermCplxCount = m_shortTermCplxCount;
1448
503
    }
1449
195
    else // CQP
1450
195
    {
1451
195
        if (m_sliceType == B_SLICE && IS_REFERENCED(curFrame))
1452
0
            m_qp = (m_qpConstant[B_SLICE] + m_qpConstant[P_SLICE]) / 2;
1453
195
        else
1454
195
            m_qp = m_qpConstant[m_sliceType];
1455
195
        curEncData.m_avgQpAq = curEncData.m_avgQpRc = m_qp;
1456
        
1457
195
        x265_zone* zone = getZone();
1458
195
        if (zone)
1459
0
        {
1460
0
            if (zone->bForceQp)
1461
0
                m_qp += zone->qp - m_qpConstant[P_SLICE];
1462
0
            else
1463
0
                m_qp -= (int)(6.0 * X265_LOG2(zone->bitrateFactor));
1464
0
        }
1465
195
    }
1466
698
    if (m_sliceType != B_SLICE)
1467
698
    {
1468
698
        m_lastNonBPictType = m_sliceType;
1469
698
        m_leadingNoBSatd = m_currentSatd;
1470
698
    }
1471
698
    rce->leadingNoBSatd = m_leadingNoBSatd;
1472
698
    if (curFrame->m_forceqp)
1473
0
    {
1474
0
        m_qp = (int32_t)(curFrame->m_forceqp + 0.5) - 1;
1475
0
        m_qp = x265_clip3(m_param->rc.qpMin, m_param->rc.qpMax, m_qp);
1476
0
        rce->qpaRc = curEncData.m_avgQpRc = curEncData.m_avgQpAq = m_qp;
1477
0
        if (m_isAbr || m_2pass)
1478
0
        {
1479
0
            rce->qpNoVbv = rce->qpaRc;
1480
0
            m_lastQScaleFor[m_sliceType] = x265_qp2qScale(rce->qpaRc);
1481
0
            if (rce->poc == 0)
1482
0
                 m_lastQScaleFor[P_SLICE] = m_lastQScaleFor[m_sliceType] * fabs(m_param->rc.ipFactor);
1483
0
            rce->frameSizePlanned = predictSize(&m_pred[m_predType], m_qp, (double)m_currentSatd);
1484
0
        }
1485
0
    }
1486
698
    m_framesDone++;
1487
1488
698
    return m_qp;
1489
698
}
1490
1491
void RateControl::accumPQpUpdate()
1492
503
{
1493
503
    m_accumPQp   *= .95;
1494
503
    m_accumPNorm *= .95;
1495
503
    m_accumPNorm += 1;
1496
503
    if (m_sliceType == I_SLICE)
1497
503
        m_accumPQp += m_qp + m_ipOffset;
1498
0
    else
1499
0
        m_accumPQp += m_qp;
1500
503
}
1501
1502
int RateControl::getPredictorType(int lowresSliceType, int sliceType)
1503
698
{
1504
    /* Use a different predictor for B Ref and B frames for vbv frame size predictions */
1505
698
    if (lowresSliceType == X265_TYPE_BREF)
1506
0
        return 3;
1507
698
    return sliceType;
1508
698
}
1509
1510
double RateControl::getDiffLimitedQScale(RateControlEntry *rce, double q)
1511
0
{
1512
    // force I/B quants as a function of P quants
1513
0
    const double lastPqScale    = m_lastQScaleFor[P_SLICE];
1514
0
    const double lastNonBqScale = m_lastQScaleFor[m_lastNonBPictType];
1515
0
    if (rce->sliceType == I_SLICE)
1516
0
    {
1517
0
        double iq = q;
1518
0
        double pq = x265_qp2qScale(m_accumPQp / m_accumPNorm);
1519
0
        double ipFactor = fabs(m_param->rc.ipFactor);
1520
        /* don't apply ipFactor if the following frame is also I */
1521
0
        if (m_accumPNorm <= 0)
1522
0
            q = iq;
1523
0
        else if (m_param->rc.ipFactor < 0)
1524
0
            q = iq / ipFactor;
1525
0
        else if (m_accumPNorm >= 1)
1526
0
            q = pq / ipFactor;
1527
0
        else
1528
0
            q = m_accumPNorm * pq / ipFactor + (1 - m_accumPNorm) * iq;
1529
0
    }
1530
0
    else if (rce->sliceType == B_SLICE)
1531
0
    {
1532
0
        if (m_param->rc.pbFactor > 0)
1533
0
            q = lastNonBqScale;
1534
0
        if (!rce->keptAsRef)
1535
0
            q *= fabs(m_param->rc.pbFactor);
1536
0
    }
1537
0
    else if (rce->sliceType == P_SLICE
1538
0
             && m_lastNonBPictType == P_SLICE
1539
0
             && rce->coeffBits == 0)
1540
0
    {
1541
0
        q = lastPqScale;
1542
0
    }
1543
1544
    /* last qscale / qdiff stuff */
1545
0
    if (m_lastNonBPictType == rce->sliceType &&
1546
0
        (rce->sliceType != I_SLICE || m_lastAccumPNorm < 1))
1547
0
    {
1548
0
        double maxQscale = m_lastQScaleFor[rce->sliceType] * m_lstep;
1549
0
        double minQscale = m_lastQScaleFor[rce->sliceType] / m_lstep;
1550
0
        q = x265_clip3(minQscale, maxQscale, q);
1551
0
    }
1552
1553
0
    m_lastQScaleFor[rce->sliceType] = q;
1554
0
    if (rce->sliceType != B_SLICE)
1555
0
        m_lastNonBPictType = rce->sliceType;
1556
0
    if (rce->sliceType == I_SLICE)
1557
0
    {
1558
0
        m_lastAccumPNorm = m_accumPNorm;
1559
0
        m_accumPNorm = 0;
1560
0
        m_accumPQp = 0;
1561
0
    }
1562
0
    if (rce->sliceType == P_SLICE)
1563
0
    {
1564
0
        double mask = 1 - pow(rce->iCuCount / m_ncu, 2);
1565
0
        m_accumPQp   = mask * (x265_qScale2qp(q) + m_accumPQp);
1566
0
        m_accumPNorm = mask * (1 + m_accumPNorm);
1567
0
    }
1568
1569
0
    return q;
1570
0
}
1571
1572
double RateControl::countExpectedBits(int startPos, int endPos)
1573
0
{
1574
0
    double expectedBits = 0;
1575
0
    for (int i = startPos; i <= endPos; i++)
1576
0
    {
1577
0
        RateControlEntry *rce = &m_rce2Pass[i];
1578
0
        rce->expectedBits = (uint64_t)expectedBits;
1579
0
        expectedBits += qScale2bits(rce, rce->newQScale);
1580
0
    }
1581
0
    return expectedBits;
1582
0
}
1583
1584
bool RateControl::findUnderflow(double *fills, int *t0, int *t1, int over, int endPos)
1585
0
{
1586
    /* find an interval ending on an overflow or underflow (depending on whether
1587
     * we're adding or removing bits), and starting on the earliest frame that
1588
     * can influence the buffer fill of that end frame. */
1589
0
    const double bufferMin = .1 * m_bufferSize;
1590
0
    const double bufferMax = .9 * m_bufferSize;
1591
0
    double fill = fills[*t0 - 1];
1592
0
    double parity = over ? 1. : -1.;
1593
0
    int start = -1, end = -1;
1594
0
    for (int i = *t0; i <= endPos; i++)
1595
0
    {
1596
0
        fill += (m_frameDuration * m_vbvMaxRate -
1597
0
                 qScale2bits(&m_rce2Pass[i], m_rce2Pass[i].newQScale)) * parity;
1598
0
        fill = x265_clip3(0.0, m_bufferSize, fill);
1599
0
        fills[i] = fill;
1600
0
        if (fill <= bufferMin || i == 0)
1601
0
        {
1602
0
            if (end >= 0)
1603
0
                break;
1604
0
            start = i;
1605
0
        }
1606
0
        else if (fill >= bufferMax && start >= 0)
1607
0
            end = i;
1608
0
    }
1609
0
    *t0 = start;
1610
0
    *t1 = end;
1611
0
    return start >= 0 && end >= 0;
1612
0
}
1613
1614
bool RateControl::fixUnderflow(int t0, int t1, double adjustment, double qscaleMin, double qscaleMax)
1615
0
{
1616
0
    double qscaleOrig, qscaleNew;
1617
0
    bool adjusted = false;
1618
0
    if (t0 > 0)
1619
0
        t0++;
1620
0
    for (int i = t0; i <= t1; i++)
1621
0
    {
1622
0
        qscaleOrig = m_rce2Pass[i].newQScale;
1623
0
        qscaleOrig = x265_clip3(qscaleMin, qscaleMax, qscaleOrig);
1624
0
        qscaleNew  = qscaleOrig * adjustment;
1625
0
        qscaleNew  = x265_clip3(qscaleMin, qscaleMax, qscaleNew);
1626
0
        m_rce2Pass[i].newQScale = qscaleNew;
1627
0
        adjusted = adjusted || (qscaleNew != qscaleOrig);
1628
0
    }
1629
0
    return adjusted;
1630
0
}
1631
1632
bool RateControl::cuTreeReadFor2Pass(Frame* frame)
1633
0
{
1634
0
    int index = m_encOrder[frame->m_poc];
1635
0
    uint8_t sliceTypeActual = (uint8_t)m_rce2Pass[index].sliceType;
1636
0
    int ncu;
1637
0
    if (m_param->rc.qgSize == 8)
1638
0
        ncu = m_ncu * 4;
1639
0
    else
1640
0
        ncu = m_ncu;
1641
0
    if (m_rce2Pass[index].keptAsRef)
1642
0
    {
1643
        /* TODO: We don't need pre-lookahead to measure AQ offsets, but there is currently
1644
         * no way to signal this */
1645
0
        uint8_t type;
1646
0
        if (m_cuTreeStats.qpBufPos < 0)
1647
0
        {
1648
0
            do
1649
0
            {
1650
0
                m_cuTreeStats.qpBufPos++;
1651
1652
0
                if (!fread(&type, 1, 1, m_cutreeStatFileIn))
1653
0
                    goto fail;
1654
0
                if (fread(m_cuTreeStats.qpBuffer[m_cuTreeStats.qpBufPos], sizeof(uint16_t), ncu, m_cutreeStatFileIn) != (size_t)ncu)
1655
0
                    goto fail;
1656
1657
0
                if (type != sliceTypeActual && m_cuTreeStats.qpBufPos == 1)
1658
0
                {
1659
0
                    x265_log(m_param, X265_LOG_ERROR, "CU-tree frametype %d doesn't match actual frametype %d.\n", type, sliceTypeActual);
1660
0
                    return false;
1661
0
                }
1662
0
            }
1663
0
            while(type != sliceTypeActual);
1664
0
        }
1665
0
        primitives.fix8Unpack(frame->m_lowres.qpCuTreeOffset, m_cuTreeStats.qpBuffer[m_cuTreeStats.qpBufPos], ncu);
1666
0
        for (int i = 0; i < ncu; i++)
1667
0
            frame->m_lowres.invQscaleFactor[i] = x265_exp2fix8(frame->m_lowres.qpCuTreeOffset[i]);
1668
0
        m_cuTreeStats.qpBufPos--;
1669
0
    }
1670
0
    return true;
1671
1672
0
fail:
1673
0
    x265_log(m_param, X265_LOG_ERROR, "Incomplete CU-tree stats file.\n");
1674
0
    return false;
1675
0
}
1676
1677
double RateControl::tuneAbrQScaleFromFeedback(double qScale)
1678
0
{
1679
0
    double abrBuffer = 2 * m_rateTolerance * m_bitrate;
1680
    /* use framesDone instead of POC as poc count is not serial with bframes enabled */
1681
0
    double overflow = 1.0;
1682
0
    double timeDone = (double)(m_framesDone - m_param->frameNumThreads + 1) * m_frameDuration;
1683
0
    double wantedBits = timeDone * m_bitrate;
1684
0
    int64_t encodedBits = m_totalBits;
1685
0
    if (m_param->totalFrames && m_param->totalFrames <= 2 * m_fps)
1686
0
    {
1687
0
        abrBuffer = m_param->totalFrames * (m_bitrate / m_fps);
1688
0
        encodedBits = m_encodedBits;
1689
0
    }
1690
1691
0
    if (wantedBits > 0 && encodedBits > 0 && (!m_partialResidualFrames || 
1692
0
        m_param->rc.bStrictCbr || m_isGrainEnabled))
1693
0
    {
1694
0
        abrBuffer *= X265_MAX(1, sqrt(timeDone));
1695
0
        overflow = x265_clip3(.5, 2.0, 1.0 + (encodedBits - wantedBits) / abrBuffer);
1696
0
        qScale *= overflow;
1697
0
    }
1698
0
    return qScale;
1699
0
}
1700
1701
double RateControl::tuneQScaleForZone(RateControlEntry *rce, double qScale)
1702
0
{
1703
0
    rce->frameSizePlanned = predictSize(&m_pred[m_predType], qScale, (double)m_currentSatd);
1704
0
    int loop = 0;
1705
1706
0
    double availableBits = (double)m_param->rc.bitrate * 1000 * m_relativeComplexity[rce->encodeOrder % m_param->reconfigWindowSize];
1707
1708
    // Tune qScale to adhere to the available frame bits.
1709
0
    for (int i = 0; i < 1000 && loop != 3; i++)
1710
0
    {
1711
0
        if (rce->frameSizePlanned < availableBits)
1712
0
        {
1713
0
            qScale = qScale / 1.01;
1714
0
            loop = loop | 1;
1715
0
        }
1716
0
        else if (rce->frameSizePlanned > availableBits)
1717
0
        {
1718
0
            qScale = qScale * 1.01;
1719
0
            loop = loop | 2;
1720
0
        }
1721
0
        rce->frameSizePlanned = predictSize(&m_pred[m_predType], qScale, (double)m_currentSatd);
1722
0
    }
1723
0
    return qScale;
1724
0
}
1725
1726
double RateControl::tuneQScaleForGrain(double rcOverflow)
1727
0
{
1728
0
    double qpstep = rcOverflow > 1.1 ? rcOverflow : m_lstep;
1729
0
    double qScaleAvg = x265_qp2qScale(m_avgPFrameQp);
1730
0
    double  q = m_lastQScaleFor[P_SLICE];
1731
0
    int curQp = int (x265_qScale2qp(m_lastQScaleFor[P_SLICE]) + 0.5);
1732
0
    double curBitrate = m_qpToEncodedBits[curQp] * int(m_fps + 0.5);
1733
0
    int newQp = rcOverflow > 1.1 ? curQp + 2 : rcOverflow > 1 ? curQp + 1 : curQp - 1 ;
1734
0
    double projectedBitrate =  int(m_fps + 0.5) * m_qpToEncodedBits[newQp];
1735
0
    if (curBitrate > 0 && projectedBitrate > 0)
1736
0
        q =  abs(projectedBitrate - m_bitrate) < abs (curBitrate - m_bitrate) ? x265_qp2qScale(newQp) : m_lastQScaleFor[P_SLICE];
1737
0
    else
1738
0
        q = rcOverflow > 1 ? qScaleAvg * qpstep : rcOverflow < 1 ?  qScaleAvg / qpstep : m_lastQScaleFor[P_SLICE];
1739
0
    return q;
1740
0
}
1741
1742
double RateControl::rateEstimateQscale(Frame* curFrame, RateControlEntry *rce)
1743
503
{
1744
503
    double q;
1745
1746
503
    if (m_2pass)
1747
0
    {
1748
0
        if (m_sliceType != rce->sliceType)
1749
0
        {
1750
0
            x265_log(m_param, X265_LOG_ERROR, "slice=%c but 2pass stats say %c\n",
1751
0
                     g_sliceTypeToChar[m_sliceType], g_sliceTypeToChar[rce->sliceType]);
1752
0
        }
1753
0
    }
1754
503
    else
1755
503
    {
1756
503
        if (m_isAbr)
1757
503
        {
1758
503
            int pos = m_sliderPos % s_slidingWindowFrames;
1759
503
            int addPos = (pos + s_slidingWindowFrames - 1) % s_slidingWindowFrames;
1760
503
            if (m_sliderPos > s_slidingWindowFrames)
1761
0
            {
1762
0
                const static double base = pow(0.5, s_slidingWindowFrames - 1);
1763
0
                m_movingAvgSum -= m_lastRemovedSatdCost * base;
1764
0
                m_movingAvgSum *= 0.5;
1765
0
                m_movingAvgSum += m_satdCostWindow[addPos];
1766
0
            }
1767
503
            else if (m_sliderPos == s_slidingWindowFrames)
1768
0
            {
1769
0
                m_movingAvgSum += m_satdCostWindow[addPos];
1770
0
            }
1771
503
            else if (m_sliderPos > 0)
1772
0
            {
1773
0
                m_movingAvgSum += m_satdCostWindow[addPos];
1774
0
                m_movingAvgSum *= 0.5;
1775
0
            }
1776
1777
503
            rce->movingAvgSum = m_movingAvgSum;
1778
503
            m_lastRemovedSatdCost = m_satdCostWindow[pos];
1779
503
            m_satdCostWindow[pos] = rce->lastSatd;
1780
503
            m_sliderPos++;
1781
503
        }
1782
503
    }
1783
1784
503
    if (m_sliceType == B_SLICE)
1785
0
    {
1786
        /* B-frames don't have independent rate control, but rather get the
1787
         * average QP of the two adjacent P-frames + an offset */
1788
0
        Slice* prevRefSlice = m_curSlice->m_refFrameList[0][0]->m_encData->m_slice;
1789
0
        Slice* nextRefSlice = m_curSlice->m_refFrameList[1][0]->m_encData->m_slice;
1790
0
        double q0 = m_curSlice->m_refFrameList[0][0]->m_encData->m_avgQpRc;
1791
0
        double q1 = m_curSlice->m_refFrameList[1][0]->m_encData->m_avgQpRc;
1792
0
        bool i0 = prevRefSlice->m_sliceType == I_SLICE;
1793
0
        bool i1 = nextRefSlice->m_sliceType == I_SLICE;
1794
0
        int dt0 = abs(m_curSlice->m_poc - prevRefSlice->m_poc);
1795
0
        int dt1 = abs(m_curSlice->m_poc - nextRefSlice->m_poc);
1796
1797
        // Skip taking a reference frame before the Scenecut if ABR has been reset.
1798
0
        if (m_lastAbrResetPoc >= 0)
1799
0
        {
1800
0
            if (prevRefSlice->m_sliceType == P_SLICE && prevRefSlice->m_poc < m_lastAbrResetPoc)
1801
0
            {
1802
0
                i0 = i1;
1803
0
                dt0 = dt1;
1804
0
                q0 = q1;
1805
0
            }
1806
0
        }
1807
1808
0
        if (prevRefSlice->m_sliceType == B_SLICE && IS_REFERENCED(m_curSlice->m_refFrameList[0][0]))
1809
0
            q0 -= m_pbOffset / 2;
1810
0
        if (nextRefSlice->m_sliceType == B_SLICE && IS_REFERENCED(m_curSlice->m_refFrameList[1][0]))
1811
0
            q1 -= m_pbOffset / 2;
1812
0
        if (i0 && i1)
1813
0
            q = (q0 + q1) / 2 + m_ipOffset;
1814
0
        else if (i0)
1815
0
            q = q1;
1816
0
        else if (i1)
1817
0
            q = q0;
1818
0
        else if(m_isGrainEnabled && !m_2pass)
1819
0
                q = q1;
1820
0
            else
1821
0
            q = (q0 * dt1 + q1 * dt0) / (dt0 + dt1);
1822
1823
0
        if (IS_REFERENCED(curFrame))
1824
0
            q += m_pbOffset / 2;
1825
0
        else
1826
0
            q += m_pbOffset;
1827
1828
                /* Set a min qp at scenechanges and transitions */
1829
0
        if (m_isSceneTransition)
1830
0
        {
1831
0
            q = X265_MAX(ABR_SCENECUT_INIT_QP_MIN, q);
1832
0
            double minScenecutQscale =x265_qp2qScale(ABR_SCENECUT_INIT_QP_MIN); 
1833
0
            m_lastQScaleFor[P_SLICE] = X265_MAX(minScenecutQscale, m_lastQScaleFor[P_SLICE]);
1834
0
        }
1835
0
        double qScale = x265_qp2qScale(q);
1836
0
        rce->qpNoVbv = q;
1837
0
        double lmin = 0, lmax = 0;
1838
0
        if (m_isGrainEnabled && m_isFirstMiniGop)
1839
0
        {
1840
0
            lmin = m_lastQScaleFor[P_SLICE] / m_lstep;
1841
0
            lmax = m_lastQScaleFor[P_SLICE] * m_lstep;
1842
0
            double tunedQscale = tuneAbrQScaleFromFeedback(qScale);
1843
0
            double overflow = tunedQscale / qScale;
1844
0
            if (!m_isAbrReset)
1845
0
                qScale = x265_clip3(lmin, lmax, qScale);
1846
0
            m_avgPFrameQp = m_avgPFrameQp == 0 ? rce->qpNoVbv : m_avgPFrameQp;
1847
0
            if (overflow != 1)
1848
0
            {
1849
0
                qScale = tuneQScaleForGrain(overflow);
1850
0
                q = x265_qScale2qp(qScale);
1851
0
            }
1852
0
            rce->qpNoVbv = q;
1853
0
        }
1854
        /* Scenecut Aware QP offsets*/
1855
0
        if (m_param->bEnableSceneCutAwareQp)
1856
0
        {
1857
0
            double lqmin = m_lmin[m_sliceType];
1858
0
            double lqmax = m_lmax[m_sliceType];
1859
0
            qScale = scenecutAwareQp(curFrame, qScale);
1860
0
            qScale = x265_clip3(lqmin, lqmax, qScale);
1861
0
            q = x265_qScale2qp(qScale);
1862
0
            rce->qpNoVbv = q;
1863
0
        }
1864
0
        if (m_isVbv)
1865
0
        {
1866
0
            lmin = m_lastQScaleFor[P_SLICE] / m_lstep;
1867
0
            lmax = m_lastQScaleFor[P_SLICE] * m_lstep;
1868
1869
0
            if (m_isCbr && !m_isGrainEnabled)
1870
0
            {
1871
0
                qScale = tuneAbrQScaleFromFeedback(qScale);
1872
0
                if (!m_isAbrReset)
1873
0
                    qScale = x265_clip3(lmin, lmax, qScale);
1874
0
                q = x265_qScale2qp(qScale);
1875
0
            }
1876
1877
0
            if (!m_param->bResetZoneConfig)
1878
0
            {
1879
0
                double lqmin = m_lmin[m_sliceType];
1880
0
                double lqmax = m_lmax[m_sliceType];
1881
0
                qScale = tuneQScaleForZone(rce, qScale);
1882
0
                qScale = x265_clip3(lqmin, lqmax, qScale);
1883
0
            }
1884
1885
0
            if (!m_2pass)
1886
0
            {
1887
                /* clip qp to permissible range after vbv-lookahead estimation to avoid possible 
1888
                 * mispredictions by initial frame size predictors */
1889
0
                qScale = clipQscale(curFrame, rce, qScale);
1890
1891
0
                if (m_pred[m_predType].count == 1)
1892
0
                    qScale = x265_clip3(lmin, lmax, qScale);
1893
0
                m_lastQScaleFor[m_sliceType] = qScale;
1894
0
            }
1895
0
        }
1896
1897
0
        if (m_2pass)
1898
0
            rce->frameSizePlanned = qScale2bits(rce, qScale);
1899
0
        else
1900
0
            rce->frameSizePlanned = predictSize(&m_pred[m_predType], qScale, (double)m_currentSatd);
1901
1902
        /* Limit planned size by MinCR */
1903
0
        if (m_isVbv)
1904
0
            rce->frameSizePlanned = X265_MIN(rce->frameSizePlanned, rce->frameSizeMaximum);
1905
0
        rce->frameSizeEstimated = rce->frameSizePlanned;
1906
1907
0
        rce->newQScale = qScale;
1908
0
        if(rce->bLastMiniGopBFrame)
1909
0
        {
1910
0
            if (m_isFirstMiniGop && m_isGrainEnabled)
1911
0
            {
1912
0
                m_avgPFrameQp = (m_avgPFrameQp + rce->qpNoVbv) / 2;
1913
0
                m_lastQScaleFor[P_SLICE] = x265_qp2qScale(m_avgPFrameQp);
1914
0
            }
1915
0
            m_isFirstMiniGop = false;
1916
0
        }
1917
0
        return qScale;
1918
0
    }
1919
503
    else
1920
503
    {
1921
503
        double abrBuffer = 2 * m_rateTolerance * m_bitrate;
1922
503
        if (m_2pass)
1923
0
        {
1924
0
            double lmin = m_lmin[m_sliceType];
1925
0
            double lmax = m_lmax[m_sliceType];
1926
0
            int64_t diff;
1927
0
            if (!m_isVbv)
1928
0
            {
1929
0
                m_predictedBits = m_totalBits;
1930
0
                if (rce->encodeOrder < m_param->frameNumThreads)
1931
0
                    m_predictedBits += (int64_t)(rce->encodeOrder * m_bitrate / m_fps);
1932
0
                else
1933
0
                    m_predictedBits += (int64_t)(m_param->frameNumThreads * m_bitrate / m_fps);
1934
0
            }
1935
            /* Adjust ABR buffer based on distance to the end of the video. */
1936
0
            if (m_numEntries > rce->encodeOrder)
1937
0
            {
1938
0
                uint64_t finalBits = m_rce2Pass[m_numEntries - 1].expectedBits;
1939
0
                double videoPos = (double)rce->expectedBits / finalBits;
1940
0
                double scaleFactor = sqrt((1 - videoPos) * m_numEntries);
1941
0
                abrBuffer *= 0.5 * X265_MAX(scaleFactor, 0.5);
1942
0
            }
1943
0
            diff = m_predictedBits - (int64_t)rce->expectedBits;
1944
0
            q = rce->newQScale;
1945
0
            x265_zone* zone = getZone();
1946
0
            if (zone)
1947
0
            {
1948
0
                if (zone->bForceQp)
1949
0
                    q = x265_qp2qScale(zone->qp);
1950
0
                else
1951
0
                    q /= zone->bitrateFactor;
1952
0
            }
1953
0
            q /= x265_clip3(0.5, 2.0, (double)(abrBuffer - diff) / abrBuffer);
1954
0
            if (m_expectedBitsSum > 0)
1955
0
            {
1956
                /* Adjust quant based on the difference between
1957
                 * achieved and expected bitrate so far */
1958
0
                double curTime = (double)rce->encodeOrder / m_numEntries;
1959
0
                double w = x265_clip3(0.0, 1.0, curTime);
1960
0
                q *= pow((double)m_totalBits / m_expectedBitsSum, w);
1961
0
            }
1962
0
            if (m_framesDone == 0 && m_param->rc.rateControlMode == X265_RC_ABR && m_isGrainEnabled)
1963
0
                q = X265_MIN(x265_qp2qScale(ABR_INIT_QP_GRAIN_MAX), q);
1964
0
            rce->qpNoVbv = x265_qScale2qp(q);
1965
0
            if ((m_sliceType == I_SLICE && m_param->keyframeMax > 1
1966
0
                && m_lastNonBPictType != I_SLICE && !m_isAbrReset) || (m_isNextGop && !m_framesDone))
1967
0
                m_avgPFrameQp = 0;
1968
0
            if (m_sliceType == P_SLICE)
1969
0
            {
1970
0
                m_avgPFrameQp = m_avgPFrameQp == 0 ? rce->qpNoVbv : m_avgPFrameQp;
1971
0
                m_avgPFrameQp = (m_avgPFrameQp + rce->qpNoVbv) / 2;
1972
0
            }
1973
1974
0
            if (m_isVbv)
1975
0
            {
1976
                /* Do not overflow vbv */
1977
0
                double expectedSize = qScale2bits(rce, q);
1978
0
                double expectedVbv = m_bufferFill + m_bufferRate - expectedSize;
1979
0
                double expectedFullness = rce->expectedVbv / m_bufferSize;
1980
0
                double qmax = q * (2 - expectedFullness);
1981
0
                double sizeConstraint = 1 + expectedFullness;
1982
0
                qmax = X265_MAX(qmax, rce->newQScale);
1983
0
                if (expectedFullness < .05)
1984
0
                    qmax = lmax;
1985
0
                qmax = X265_MIN(qmax, lmax);
1986
0
                while (((expectedVbv < rce->expectedVbv/sizeConstraint) && (q < qmax)) ||
1987
0
                        ((expectedVbv < 0) && (q < lmax)))
1988
0
                {
1989
0
                    q *= 1.05;
1990
0
                    expectedSize = qScale2bits(rce, q);
1991
0
                    expectedVbv = m_bufferFill + m_bufferRate - expectedSize;
1992
0
                }
1993
0
            }
1994
0
            q = x265_clip3(lmin, lmax, q);
1995
0
        }
1996
503
        else
1997
503
        {
1998
            /* 1pass ABR */
1999
2000
            /* Calculate the quantizer which would have produced the desired
2001
             * average bitrate if it had been applied to all frames so far.
2002
             * Then modulate that quant based on the current frame's complexity
2003
             * relative to the average complexity so far (using the 2pass RCEQ).
2004
             * Then bias the quant up or down if total size so far was far from
2005
             * the target.
2006
             * Result: Depending on the value of rate_tolerance, there is a
2007
             * trade-off between quality and bitrate precision. But at large
2008
             * tolerances, the bit distribution approaches that of 2pass. */
2009
2010
503
            double overflow = 1;
2011
503
            double lqmin = m_lmin[m_sliceType];
2012
503
            double lqmax = m_lmax[m_sliceType];
2013
503
            m_shortTermCplxSum *= 0.5;
2014
503
            m_shortTermCplxCount *= 0.5;
2015
503
            m_shortTermCplxSum += m_currentSatd / (CLIP_DURATION(m_frameDuration) / BASE_FRAME_DURATION);
2016
503
            m_shortTermCplxCount++;
2017
            /* coeffBits to be used in 2-pass */
2018
503
            rce->coeffBits = (int)m_currentSatd;
2019
503
            rce->blurredComplexity = m_shortTermCplxSum / m_shortTermCplxCount;
2020
503
            rce->mvBits = 0;
2021
503
            rce->sliceType = m_sliceType;
2022
2023
503
            if (m_param->rc.rateControlMode == X265_RC_CRF)
2024
503
            {
2025
503
                q = getQScale(rce, m_rateFactorConstant);
2026
503
                x265_zone* zone = getZone();
2027
503
                if (zone)
2028
0
                {
2029
0
                    if (zone->bForceQp)
2030
0
                        q = x265_qp2qScale(zone->qp);
2031
0
                    else
2032
0
                        q /= zone->bitrateFactor;
2033
0
                }
2034
503
            }
2035
0
            else
2036
0
            {
2037
0
                if (!m_param->rc.bStatRead)
2038
0
                    checkAndResetABR(rce, false);
2039
0
                double initialQScale = getQScale(rce, m_wantedBitsWindow / m_cplxrSum);
2040
0
                x265_zone* zone = getZone();
2041
0
                if (zone)
2042
0
                {
2043
0
                    if (zone->bForceQp)
2044
0
                        initialQScale = x265_qp2qScale(zone->qp);
2045
0
                    else
2046
0
                        initialQScale /= zone->bitrateFactor;
2047
0
                }
2048
0
                double tunedQScale = tuneAbrQScaleFromFeedback(initialQScale);
2049
0
                overflow = tunedQScale / initialQScale;
2050
0
                q = !m_partialResidualFrames? tunedQScale : initialQScale;
2051
0
                bool isEncodeEnd = (m_param->totalFrames && 
2052
0
                    m_framesDone > 0.75 * m_param->totalFrames) ? 1 : 0;
2053
0
                bool isEncodeBeg = m_framesDone < (int)(m_fps + 0.5);
2054
0
                if (m_isGrainEnabled)
2055
0
                {
2056
0
                    if(m_sliceType!= I_SLICE && m_framesDone && !isEncodeEnd &&
2057
0
                        ((overflow < 1.05 && overflow > 0.95) || isEncodeBeg))
2058
0
                    {
2059
0
                        q = tuneQScaleForGrain(overflow);
2060
0
                    }
2061
0
                }
2062
0
            }
2063
503
            if ((m_sliceType == I_SLICE && m_param->keyframeMax > 1
2064
503
                && m_lastNonBPictType != I_SLICE && !m_isAbrReset) || (m_isNextGop && !m_framesDone))
2065
0
            {
2066
0
                if (!m_param->rc.bStrictCbr)
2067
0
                    q = x265_qp2qScale(m_accumPQp / m_accumPNorm);
2068
0
                q /= fabs(m_param->rc.ipFactor);
2069
0
                m_avgPFrameQp = 0;
2070
0
            }
2071
503
            else if (m_framesDone > 0)
2072
0
            {
2073
0
                if (m_param->rc.rateControlMode != X265_RC_CRF)
2074
0
                {
2075
0
                    lqmin = m_lastQScaleFor[m_sliceType] / m_lstep;
2076
0
                    lqmax = m_lastQScaleFor[m_sliceType] * m_lstep;
2077
0
                    if (!m_partialResidualFrames || m_isGrainEnabled)
2078
0
                    {
2079
0
                        if (overflow > 1.1 && m_framesDone > 3)
2080
0
                            lqmax *= m_lstep;
2081
0
                        else if (overflow < 0.9)
2082
0
                            lqmin /= m_lstep;
2083
0
                    }
2084
0
                    q = x265_clip3(lqmin, lqmax, q);
2085
0
                }
2086
0
            }
2087
503
            else if (m_qCompress != 1 && m_param->rc.rateControlMode == X265_RC_CRF)
2088
503
            {
2089
503
                q = x265_qp2qScale(CRF_INIT_QP) / fabs(m_param->rc.ipFactor);
2090
503
            }
2091
0
            else if (m_framesDone == 0 && !m_isVbv && m_param->rc.rateControlMode == X265_RC_ABR)
2092
0
            {
2093
                /* for ABR alone, clip the first I frame qp */
2094
0
                lqmax = (m_isGrainEnabled && m_lstep) ? x265_qp2qScale(ABR_INIT_QP_GRAIN_MAX) :
2095
0
                        x265_qp2qScale(ABR_INIT_QP_MAX);
2096
0
                q = X265_MIN(lqmax, q);
2097
0
            }
2098
503
            q = x265_clip3(lqmin, lqmax, q);
2099
            /* Set a min qp at scenechanges and transitions */
2100
503
            if (m_isSceneTransition)
2101
0
            {
2102
0
                double minScenecutQscale = x265_qp2qScale(ABR_SCENECUT_INIT_QP_MIN);
2103
0
                q = X265_MAX(minScenecutQscale, q);
2104
0
                m_lastQScaleFor[P_SLICE] = X265_MAX(minScenecutQscale, m_lastQScaleFor[P_SLICE]);
2105
0
            }
2106
503
            rce->qpNoVbv = x265_qScale2qp(q);
2107
503
            if (m_sliceType == P_SLICE)
2108
0
            {
2109
0
                m_avgPFrameQp = m_avgPFrameQp == 0 ? rce->qpNoVbv : m_avgPFrameQp;
2110
0
                m_avgPFrameQp = (m_avgPFrameQp + rce->qpNoVbv) / 2;
2111
0
            }
2112
2113
503
            if (!m_param->bResetZoneConfig)
2114
0
            {
2115
0
                q = tuneQScaleForZone(rce, q);
2116
0
                q = x265_clip3(lqmin, lqmax, q);
2117
0
            }
2118
            /* Scenecut Aware QP offsets*/
2119
503
            if (m_param->bEnableSceneCutAwareQp)
2120
0
            {
2121
0
                double qmin = m_lmin[m_sliceType];
2122
0
                double qmax = m_lmax[m_sliceType];
2123
0
                q = scenecutAwareQp(curFrame, q);
2124
0
                q = x265_clip3(qmin, qmax, q);
2125
0
                rce->qpNoVbv = x265_qScale2qp(q);
2126
0
            }
2127
503
            q = clipQscale(curFrame, rce, q);
2128
2129
2130
503
            if (m_2pass)
2131
0
                rce->frameSizePlanned = qScale2bits(rce, q);
2132
503
            else
2133
503
                rce->frameSizePlanned = predictSize(&m_pred[m_predType], q, (double)m_currentSatd);
2134
2135
            /*  clip qp to permissible range after vbv-lookahead estimation to avoid possible
2136
             * mispredictions by initial frame size predictors, after each scenecut */
2137
503
            bool isFrameAfterScenecut = m_sliceType!= I_SLICE && m_curSlice->m_refFrameList[0][0]->m_lowres.bScenecut;
2138
503
            if (!m_2pass && m_isVbv && isFrameAfterScenecut)
2139
0
                q = x265_clip3(lqmin, lqmax, q);
2140
503
        }
2141
503
        m_lastQScaleFor[m_sliceType] = q;
2142
503
        if ((m_curSlice->m_poc == 0 || m_lastQScaleFor[P_SLICE] < q) && !(m_2pass && !m_isVbv))
2143
503
            m_lastQScaleFor[P_SLICE] = q * fabs(m_param->rc.ipFactor);
2144
2145
503
        if (m_2pass)
2146
0
            rce->frameSizePlanned = qScale2bits(rce, q);
2147
503
        else
2148
503
            rce->frameSizePlanned = predictSize(&m_pred[m_predType], q, (double)m_currentSatd);
2149
2150
        /* Always use up the whole VBV in this case. */
2151
503
        if (m_singleFrameVbv)
2152
0
            rce->frameSizePlanned = m_bufferRate;
2153
        /* Limit planned size by MinCR */
2154
503
        if (m_isVbv)
2155
0
            rce->frameSizePlanned = X265_MIN(rce->frameSizePlanned, rce->frameSizeMaximum);
2156
503
        rce->frameSizeEstimated = rce->frameSizePlanned;
2157
503
        rce->newQScale = q;
2158
503
        return q;
2159
503
    }
2160
503
}
2161
2162
void RateControl::rateControlUpdateStats(RateControlEntry* rce)
2163
0
{
2164
0
    if (!m_param->rc.bStatWrite && !m_param->rc.bStatRead)
2165
0
    {
2166
0
        if (rce->sliceType == I_SLICE)
2167
0
        {
2168
            /* previous I still had a residual; roll it into the new loan */
2169
0
            if (m_partialResidualFrames)
2170
0
                rce->rowTotalBits += m_partialResidualCost * m_partialResidualFrames;
2171
0
            if ((m_param->totalFrames != 0) && (m_amortizeFrames > (m_param->totalFrames - m_framesDone)))
2172
0
            {
2173
0
                m_amortizeFrames = 0;
2174
0
                m_amortizeFraction = 0;
2175
0
            }
2176
0
            else
2177
0
            {
2178
0
                double depreciateRate = 1.1;
2179
0
                m_amortizeFrames = (int)(m_amortizeFrames / depreciateRate);
2180
0
                m_amortizeFraction /= depreciateRate;
2181
0
                m_amortizeFrames = X265_MAX(m_amortizeFrames, MIN_AMORTIZE_FRAME);
2182
0
                m_amortizeFraction = X265_MAX(m_amortizeFraction, MIN_AMORTIZE_FRACTION);
2183
0
            }
2184
0
            rce->amortizeFrames = m_amortizeFrames;
2185
0
            rce->amortizeFraction = m_amortizeFraction;
2186
0
            m_partialResidualFrames = X265_MIN((int)rce->amortizeFrames, m_param->keyframeMax);
2187
0
            m_partialResidualCost = (int)((rce->rowTotalBits * rce->amortizeFraction) / m_partialResidualFrames);
2188
0
            rce->rowTotalBits -= m_partialResidualCost * m_partialResidualFrames;
2189
0
        }
2190
0
        else if (m_partialResidualFrames)
2191
0
        {
2192
0
             rce->rowTotalBits += m_partialResidualCost;
2193
0
             m_partialResidualFrames--;
2194
0
        }
2195
0
    }
2196
0
    if (rce->sliceType != B_SLICE)
2197
0
        rce->rowCplxrSum = rce->rowTotalBits * x265_qp2qScale(rce->qpaRc) / rce->qRceq;
2198
0
    else
2199
0
        rce->rowCplxrSum = rce->rowTotalBits * x265_qp2qScale(rce->qpaRc) / (rce->qRceq * fabs(m_param->rc.pbFactor));
2200
2201
0
    m_cplxrSum += rce->rowCplxrSum;
2202
0
    m_totalBits += rce->rowTotalBits;
2203
2204
    /* do not allow the next frame to enter rateControlStart() until this
2205
     * frame has updated its mid-frame statistics */
2206
0
    if (m_param->rc.rateControlMode == X265_RC_ABR || m_isVbv)
2207
0
    {
2208
0
        m_startEndOrder.incr();
2209
2210
0
        if (rce->encodeOrder < m_param->frameNumThreads - 1)
2211
0
            m_startEndOrder.incr(); // faked rateControlEnd calls for negative frames
2212
0
    }
2213
0
}
2214
2215
void RateControl::checkAndResetABR(RateControlEntry* rce, bool isFrameDone)
2216
0
{
2217
0
    double abrBuffer = 2 * m_rateTolerance * m_bitrate;
2218
2219
    // Check if current Slice is a scene cut that follows low detailed/blank frames
2220
0
    if (rce->lastSatd > 4 * rce->movingAvgSum || rce->scenecut || rce->isFadeEnd)
2221
0
    {
2222
0
        if (!m_isAbrReset && rce->movingAvgSum > 0
2223
0
            && (m_isPatternPresent || !m_param->bframes))
2224
0
        {
2225
0
            int pos = X265_MAX(m_sliderPos - m_param->frameNumThreads, 0);
2226
0
            int64_t shrtTermWantedBits = (int64_t) (X265_MIN(pos, s_slidingWindowFrames) * m_bitrate * m_frameDuration);
2227
0
            int64_t shrtTermTotalBitsSum = 0;
2228
            // Reset ABR if prev frames are blank to prevent further sudden overflows/ high bit rate spikes.
2229
0
            for (int i = 0; i < s_slidingWindowFrames ; i++)
2230
0
                shrtTermTotalBitsSum += m_encodedBitsWindow[i];
2231
0
            double underflow = (shrtTermTotalBitsSum - shrtTermWantedBits) / abrBuffer;
2232
0
            const double epsilon = 0.0001f;
2233
0
            if ((underflow < epsilon || rce->isFadeEnd) && !isFrameDone)
2234
0
            {
2235
0
                init(*m_curSlice->m_sps);
2236
                // Reduce tune complexity factor for scenes that follow blank frames
2237
0
                double tuneCplxFactor = (m_ncu > 3600 && m_param->rc.cuTree && !m_param->rc.hevcAq) ? 2.5 : m_param->rc.hevcAq ? 1.5 : m_isGrainEnabled ? 1.9 : 1.0;
2238
0
                m_cplxrSum /= tuneCplxFactor;
2239
0
                m_shortTermCplxSum = rce->lastSatd / (CLIP_DURATION(m_frameDuration) / BASE_FRAME_DURATION);
2240
0
                m_shortTermCplxCount = 1;
2241
0
                m_isAbrReset = true;
2242
0
                m_lastAbrResetPoc = rce->poc;
2243
0
            }
2244
0
        }
2245
0
        else if (m_isAbrReset && isFrameDone)
2246
0
        {
2247
            // Clear flag to reset ABR and continue as usual.
2248
0
            m_isAbrReset = false;
2249
0
        }
2250
0
    }
2251
0
}
2252
2253
void RateControl::hrdFullness(SEIBufferingPeriod *seiBP)
2254
0
{
2255
0
    const VUI* vui = &m_curSlice->m_sps->vuiParameters;
2256
0
    const HRDInfo* hrd = &vui->hrdParameters;
2257
0
    int num = 90000;
2258
0
    int denom = hrd->bitRateValue << (hrd->bitRateScale + BR_SHIFT);
2259
0
    int64_t cpbState = (int64_t)m_bufferFillFinal;
2260
0
    int64_t cpbSize = (int64_t)hrd->cpbSizeValue << (hrd->cpbSizeScale + CPB_SHIFT);
2261
2262
0
    if (cpbState < 0 || cpbState > cpbSize)
2263
0
    {
2264
0
        x265_log(m_param, X265_LOG_WARNING, "CPB %s: %.0lf bits in a %.0lf-bit buffer\n",
2265
0
                 cpbState < 0 ? "underflow" : "overflow", (float)cpbState, (float)cpbSize);
2266
0
    }
2267
2268
0
    seiBP->m_initialCpbRemovalDelay = (uint32_t)(num * cpbState / denom);
2269
0
    seiBP->m_initialCpbRemovalDelayOffset = (uint32_t)(num * cpbSize / denom - seiBP->m_initialCpbRemovalDelay);
2270
0
}
2271
2272
void RateControl::updateVbvPlan(Encoder* enc)
2273
0
{
2274
0
    m_bufferFill = m_bufferFillFinal;
2275
0
    enc->updateVbvPlan(this);
2276
0
}
2277
2278
double RateControl::predictSize(Predictor *p, double q, double var)
2279
1.00k
{
2280
1.00k
    return (p->coeff * var + p->offset) / (q * p->count);
2281
1.00k
}
2282
2283
double RateControl::clipQscale(Frame* curFrame, RateControlEntry* rce, double q)
2284
503
{
2285
    // B-frames are not directly subject to VBV,
2286
    // since they are controlled by referenced P-frames' QPs.
2287
503
    double lmin = m_lmin[rce->sliceType];
2288
503
    double lmax = m_lmax[rce->sliceType];
2289
503
    double q0 = q;
2290
503
    if (m_isVbv && m_currentSatd > 0 && curFrame)
2291
0
    {
2292
0
        if (m_param->lookaheadDepth || m_param->rc.cuTree ||
2293
0
            (m_param->scenecutThreshold || m_param->bHistBasedSceneCut) ||
2294
0
            (m_param->bFrameAdaptive && m_param->bframes))
2295
0
        {
2296
           /* Lookahead VBV: If lookahead is done, raise the quantizer as necessary
2297
            * such that no frames in the lookahead overflow and such that the buffer
2298
            * is in a reasonable state by the end of the lookahead. */
2299
0
            int loopTerminate = 0;
2300
            /* Avoid an infinite loop. */
2301
0
            for (int iterations = 0; iterations < 1000 && loopTerminate != 3; iterations++)
2302
0
            {
2303
0
                double frameQ[3];
2304
0
                double curBits;
2305
0
                curBits = predictSize(&m_pred[m_predType], q, (double)m_currentSatd);
2306
0
                double bufferFillCur = m_bufferFill - curBits;
2307
0
                double targetFill;
2308
0
                double totalDuration = m_frameDuration;
2309
0
                frameQ[P_SLICE] = m_sliceType == I_SLICE ? q * m_param->rc.ipFactor : (m_sliceType == B_SLICE ? q / m_param->rc.pbFactor : q);
2310
0
                frameQ[B_SLICE] = frameQ[P_SLICE] * m_param->rc.pbFactor;
2311
0
                frameQ[I_SLICE] = frameQ[P_SLICE] / m_param->rc.ipFactor;
2312
                /* Loop over the planned future frames. */
2313
0
                bool iter = true;
2314
0
                for (int j = 0; bufferFillCur >= 0 && iter ; j++)
2315
0
                {
2316
0
                    int type = curFrame->m_lowres.plannedType[j];
2317
0
                    if (type == X265_TYPE_AUTO || totalDuration >= 1.0)
2318
0
                        break;
2319
0
                    totalDuration += m_frameDuration;
2320
0
                    double wantedFrameSize = m_vbvMaxRate * m_frameDuration;
2321
0
                    if (bufferFillCur + wantedFrameSize <= m_bufferSize)
2322
0
                        bufferFillCur += wantedFrameSize;
2323
0
                    int64_t satd = curFrame->m_lowres.plannedSatd[j] >> (X265_DEPTH - 8);
2324
0
                    type = IS_X265_TYPE_I(type) ? I_SLICE : IS_X265_TYPE_B(type) ? B_SLICE : P_SLICE;
2325
0
                    int predType = getPredictorType(curFrame->m_lowres.plannedType[j], type);
2326
0
                    curBits = predictSize(&m_pred[predType], frameQ[type], (double)satd);
2327
0
                    bufferFillCur -= curBits;
2328
0
                    if (!m_param->bResetZoneConfig && ((uint64_t)j == (m_param->reconfigWindowSize - 1)))
2329
0
                        iter = false;
2330
0
                }
2331
0
                if (rce->vbvEndAdj)
2332
0
                {
2333
0
                    bool loopBreak = false;
2334
0
                    double bufferDiff = m_param->vbvBufferEnd - (m_bufferFill / m_bufferSize);
2335
0
                    rce->targetFill = m_bufferFill + m_bufferSize * (bufferDiff / (m_param->totalFrames - rce->encodeOrder));
2336
0
                    if (bufferFillCur < rce->targetFill)
2337
0
                    {
2338
0
                        q *= 1.01;
2339
0
                        loopTerminate |= 1;
2340
0
                        loopBreak = true;
2341
0
                    }
2342
0
                    if (bufferFillCur > m_param->vbvBufferEnd * m_bufferSize)
2343
0
                    {
2344
0
                        q /= 1.01;
2345
0
                        loopTerminate |= 2;
2346
0
                        loopBreak = true;
2347
0
                    }
2348
0
                    if (!loopBreak)
2349
0
                        break;
2350
0
                }
2351
0
                else
2352
0
                {
2353
                    /* Try to get the buffer at least 50% filled, but don't set an impossible goal. */
2354
0
                    double finalDur = 1;
2355
0
                    if (m_param->rc.bStrictCbr)
2356
0
                    {
2357
0
                        finalDur = x265_clip3(0.4, 1.0, totalDuration);
2358
0
                    }
2359
0
                    targetFill = X265_MIN(m_bufferFill + totalDuration * m_vbvMaxRate * 0.5, m_bufferSize * (1 - 0.5 * finalDur));
2360
0
                    if (bufferFillCur < targetFill)
2361
0
                    {
2362
0
                        q *= 1.01;
2363
0
                        loopTerminate |= 1;
2364
0
                        continue;
2365
0
                    }
2366
                    /* Try to get the buffer not more than 80% filled, but don't set an impossible goal. */
2367
0
                    targetFill = x265_clip3(m_bufferSize * (1 - 0.2 * finalDur), m_bufferSize, m_bufferFill - totalDuration * m_vbvMaxRate * 0.5);
2368
0
                    if (m_isCbr && bufferFillCur > targetFill && !m_isSceneTransition)
2369
0
                    {
2370
0
                        q /= 1.01;
2371
0
                        loopTerminate |= 2;
2372
0
                        continue;
2373
0
                    }
2374
0
                    break;
2375
0
                }
2376
0
            }
2377
0
            q = X265_MAX(q0 / 2, q);
2378
0
        }
2379
0
        else
2380
0
        {
2381
            /* Fallback to old purely-reactive algorithm: no lookahead. */
2382
0
            if ((m_sliceType == P_SLICE || m_sliceType == B_SLICE ||
2383
0
                    (m_sliceType == I_SLICE && m_lastNonBPictType == I_SLICE)) &&
2384
0
                m_bufferFill / m_bufferSize < 0.5)
2385
0
            {
2386
0
                q /= x265_clip3(0.5, 1.0, 2.0 * m_bufferFill / m_bufferSize);
2387
0
            }
2388
            // Now a hard threshold to make sure the frame fits in VBV.
2389
            // This one is mostly for I-frames.
2390
0
            double bits = predictSize(&m_pred[m_predType], q, (double)m_currentSatd);
2391
2392
            // For small VBVs, allow the frame to use up the entire VBV.
2393
0
            double maxFillFactor;
2394
0
            maxFillFactor = m_bufferSize >= 5 * m_bufferRate ? 2 : 1;
2395
            // For single-frame VBVs, request that the frame use up the entire VBV.
2396
0
            double minFillFactor = m_singleFrameVbv ? 1 : 2;
2397
2398
0
            for (int iterations = 0; iterations < 10; iterations++)
2399
0
            {
2400
0
                double qf = 1.0;
2401
0
                if (bits > m_bufferFill / maxFillFactor)
2402
0
                    qf = x265_clip3(0.2, 1.0, m_bufferFill / (maxFillFactor * bits));
2403
0
                q /= qf;
2404
0
                bits *= qf;
2405
0
                if (bits < m_bufferRate / minFillFactor)
2406
0
                    q *= bits * minFillFactor / m_bufferRate;
2407
0
                bits = predictSize(&m_pred[m_predType], q, (double)m_currentSatd);
2408
0
            }
2409
2410
0
            q = X265_MAX(q0, q);
2411
0
        }
2412
2413
        /* Apply MinCR restrictions */
2414
0
        double pbits = predictSize(&m_pred[m_predType], q, (double)m_currentSatd);
2415
0
        if (pbits > rce->frameSizeMaximum)
2416
0
            q *= pbits / rce->frameSizeMaximum;
2417
        /* To detect frames that are more complex in SATD costs compared to prev window, yet 
2418
         * lookahead vbv reduces its qscale by half its value. Be on safer side and avoid drastic 
2419
         * qscale reductions for frames high in complexity */
2420
0
        bool mispredCheck = rce->movingAvgSum && m_currentSatd >= rce->movingAvgSum && q <= q0 / 2;
2421
0
        if (!m_isCbr || (m_isAbr && mispredCheck))
2422
0
            q = X265_MAX(q0, q);
2423
2424
0
        if (m_rateFactorMaxIncrement)
2425
0
        {
2426
0
            double qpNoVbv = x265_qScale2qp(q0);
2427
0
            double qmax = X265_MIN(lmax,x265_qp2qScale(qpNoVbv + m_rateFactorMaxIncrement));
2428
0
            return x265_clip3(lmin, qmax, q);
2429
0
        }
2430
0
    }
2431
503
    if (m_2pass)
2432
0
    {
2433
0
        double min = log(lmin);
2434
0
        double max = log(lmax);
2435
0
        q = (log(q) - min) / (max - min) - 0.5;
2436
0
        q = 1.0 / (1.0 + exp(-4 * q));
2437
0
        q = q*(max - min) + min;
2438
0
        return exp(q);
2439
0
    }
2440
503
    return x265_clip3(lmin, lmax, q);
2441
503
}
2442
2443
double RateControl::predictRowsSizeSum(Frame* curFrame, RateControlEntry* rce, double qpVbv, int32_t& encodedBitsSoFar)
2444
0
{
2445
0
    uint32_t rowSatdCostSoFar = 0, totalSatdBits = 0;
2446
0
    encodedBitsSoFar = 0;
2447
2448
0
    double qScale = x265_qp2qScale(qpVbv);
2449
0
    FrameData& curEncData = *curFrame->m_encData;
2450
0
    int picType = curEncData.m_slice->m_sliceType;
2451
0
    Frame* refFrame = curEncData.m_slice->m_refFrameList[0][0];
2452
2453
0
    uint32_t maxRows = curEncData.m_slice->m_sps->numCuInHeight;
2454
0
    uint32_t maxCols = curEncData.m_slice->m_sps->numCuInWidth;
2455
2456
0
    for (uint32_t row = 0; row < maxRows; row++)
2457
0
    {
2458
0
        encodedBitsSoFar += curEncData.m_rowStat[row].encodedBits;
2459
0
        rowSatdCostSoFar = curEncData.m_rowStat[row].rowSatd;
2460
0
        uint32_t satdCostForPendingCus = curEncData.m_rowStat[row].satdForVbv - rowSatdCostSoFar;
2461
0
        satdCostForPendingCus >>= X265_DEPTH - 8;
2462
0
        if (satdCostForPendingCus  > 0)
2463
0
        {
2464
0
            double pred_s = predictSize(rce->rowPred[0], qScale, satdCostForPendingCus);
2465
0
            uint32_t refRowSatdCost = 0, refRowBits = 0, intraCostForPendingCus = 0;
2466
0
            double refQScale = 0;
2467
2468
0
            if (picType != I_SLICE && !m_param->rc.bEnableConstVbv)
2469
0
            {
2470
0
                FrameData& refEncData = *refFrame->m_encData;
2471
0
                uint32_t endCuAddr = maxCols * (row + 1);
2472
0
                uint32_t startCuAddr = curEncData.m_rowStat[row].numEncodedCUs;
2473
0
                if (startCuAddr)
2474
0
                {
2475
0
                    for (uint32_t cuAddr = startCuAddr + 1 ; cuAddr < endCuAddr; cuAddr++)
2476
0
                    {
2477
0
                        refRowSatdCost += refEncData.m_cuStat[cuAddr].vbvCost;
2478
0
                        refRowBits += refEncData.m_cuStat[cuAddr].totalBits;
2479
0
                    }
2480
0
                }
2481
0
                else
2482
0
                {
2483
0
                    refRowBits = refEncData.m_rowStat[row].encodedBits;
2484
0
                    refRowSatdCost = refEncData.m_rowStat[row].satdForVbv;
2485
0
                }
2486
2487
0
                refRowSatdCost >>= X265_DEPTH - 8;
2488
0
                refQScale = refEncData.m_rowStat[row].rowQpScale;
2489
0
            }
2490
2491
0
            if (picType == I_SLICE || qScale >= refQScale)
2492
0
            {
2493
0
                if (picType == P_SLICE 
2494
0
                    && refFrame 
2495
0
                    && refFrame->m_encData->m_slice->m_sliceType == picType
2496
0
                    && refQScale > 0
2497
0
                    && refRowBits > 0
2498
0
                    && !m_param->rc.bEnableConstVbv)
2499
0
                {
2500
0
                    if (abs((int32_t)(refRowSatdCost - satdCostForPendingCus)) < (int32_t)satdCostForPendingCus / 2)
2501
0
                    {
2502
0
                        double predTotal = refRowBits * satdCostForPendingCus / refRowSatdCost * refQScale / qScale;
2503
0
                        totalSatdBits += (int32_t)((pred_s + predTotal) * 0.5);
2504
0
                        continue;
2505
0
                    }
2506
0
                }
2507
0
                totalSatdBits += (int32_t)pred_s;
2508
0
            }
2509
0
            else if (picType == P_SLICE)
2510
0
            {
2511
0
                intraCostForPendingCus = curEncData.m_rowStat[row].intraSatdForVbv - curEncData.m_rowStat[row].rowIntraSatd;
2512
0
                intraCostForPendingCus >>= X265_DEPTH - 8;
2513
                /* Our QP is lower than the reference! */
2514
0
                double pred_intra = predictSize(rce->rowPred[1], qScale, intraCostForPendingCus);
2515
                /* Sum: better to overestimate than underestimate by using only one of the two predictors. */
2516
0
                totalSatdBits += (int32_t)(pred_intra + pred_s);
2517
0
            }
2518
0
            else
2519
0
                totalSatdBits += (int32_t)pred_s;
2520
0
        }
2521
0
    }
2522
2523
0
    return totalSatdBits + encodedBitsSoFar;
2524
0
}
2525
2526
int RateControl::rowVbvRateControl(Frame* curFrame, uint32_t row, RateControlEntry* rce, double& qpVbv, uint32_t* m_sliceBaseRow, uint32_t sliceId)
2527
0
{
2528
0
    FrameData& curEncData = *curFrame->m_encData;
2529
0
    double qScaleVbv = x265_qp2qScale(qpVbv);
2530
0
    uint64_t rowSatdCost = curEncData.m_rowStat[row].rowSatd;
2531
0
    double encodedBits = curEncData.m_rowStat[row].encodedBits;
2532
0
    uint32_t rowInSlice = row - m_sliceBaseRow[sliceId];
2533
2534
0
    if (m_param->bEnableWavefront && rowInSlice == 1)
2535
0
    {
2536
0
        rowSatdCost += curEncData.m_rowStat[row - 1].rowSatd;
2537
0
        encodedBits += curEncData.m_rowStat[row - 1].encodedBits;
2538
0
    }
2539
0
    rowSatdCost >>= X265_DEPTH - 8;
2540
0
    updatePredictor(rce->rowPred[0], qScaleVbv, (double)rowSatdCost, encodedBits);
2541
0
    if (curEncData.m_slice->m_sliceType != I_SLICE && !m_param->rc.bEnableConstVbv)
2542
0
    {
2543
0
        Frame* refFrame = curEncData.m_slice->m_refFrameList[0][0];
2544
0
        if (qpVbv < refFrame->m_encData->m_rowStat[row].rowQp)
2545
0
        {
2546
0
            uint64_t intraRowSatdCost = curEncData.m_rowStat[row].rowIntraSatd;
2547
0
            if (m_param->bEnableWavefront && rowInSlice == 1)
2548
0
                intraRowSatdCost += curEncData.m_rowStat[row - 1].rowIntraSatd;
2549
0
            intraRowSatdCost >>= X265_DEPTH - 8;
2550
0
            updatePredictor(rce->rowPred[1], qScaleVbv, (double)intraRowSatdCost, encodedBits);
2551
0
        }
2552
0
    }
2553
2554
0
    int canReencodeRow = 1;
2555
    /* tweak quality based on difference from predicted size */
2556
0
    double prevRowQp = qpVbv;
2557
0
    double qpAbsoluteMax = m_param->rc.qpMax;
2558
0
    double qpAbsoluteMin = m_param->rc.qpMin;
2559
0
    if (m_rateFactorMaxIncrement)
2560
0
        qpAbsoluteMax = X265_MIN(qpAbsoluteMax, rce->qpNoVbv + m_rateFactorMaxIncrement);
2561
2562
0
    if (m_rateFactorMaxDecrement)
2563
0
        qpAbsoluteMin = X265_MAX(qpAbsoluteMin, rce->qpNoVbv - m_rateFactorMaxDecrement);
2564
2565
0
    double qpMax = X265_MIN(prevRowQp + m_param->rc.qpStep, qpAbsoluteMax);
2566
0
    double qpMin = X265_MAX(prevRowQp - m_param->rc.qpStep, qpAbsoluteMin);
2567
0
    double stepSize = 0.5;
2568
0
    double bufferLeftPlanned = rce->bufferFill - rce->frameSizePlanned;
2569
2570
0
    const SPS& sps = *curEncData.m_slice->m_sps;
2571
0
    double maxFrameError = X265_MAX(0.05, 1.0 / sps.numCuInHeight);
2572
2573
0
    if (row < m_sliceBaseRow[sliceId + 1] - 1)
2574
0
    {
2575
        /* More threads means we have to be more cautious in letting ratecontrol use up extra bits. */
2576
0
        double rcTol = bufferLeftPlanned / m_param->frameNumThreads * m_rateTolerance;
2577
0
        int32_t encodedBitsSoFar = 0;
2578
0
        double accFrameBits = predictRowsSizeSum(curFrame, rce, qpVbv, encodedBitsSoFar);
2579
0
        double vbvEndBias = 0.95;
2580
2581
        /* * Don't increase the row QPs until a sufficent amount of the bits of
2582
         * the frame have been processed, in case a flat area at the top of the
2583
         * frame was measured inaccurately. */
2584
0
        if (encodedBitsSoFar < 0.05f * rce->frameSizePlanned)
2585
0
            qpMax = qpAbsoluteMax = prevRowQp;
2586
2587
0
        if (rce->sliceType != I_SLICE || (m_param->rc.bStrictCbr && rce->poc > 0))
2588
0
            rcTol *= 0.5;
2589
2590
0
        if (!m_isCbr)
2591
0
            qpMin = X265_MAX(qpMin, rce->qpNoVbv);
2592
2593
0
        double totalBitsNeeded = m_wantedBitsWindow;
2594
0
        if (m_param->totalFrames)
2595
0
            totalBitsNeeded = (m_param->totalFrames * m_bitrate) / m_fps;
2596
0
        double abrOvershoot = (accFrameBits + m_totalBits - m_wantedBitsWindow) / totalBitsNeeded;
2597
2598
0
        while (qpVbv < qpMax
2599
0
               && (((accFrameBits > rce->frameSizePlanned + rcTol) ||
2600
0
                   (rce->bufferFill - accFrameBits < bufferLeftPlanned * 0.5) ||
2601
0
                   (accFrameBits > rce->frameSizePlanned && qpVbv < rce->qpNoVbv) ||
2602
0
                   (rce->vbvEndAdj && ((rce->bufferFill - accFrameBits) < (rce->targetFill * vbvEndBias))))
2603
0
                   && (!m_param->rc.bStrictCbr ? 1 : abrOvershoot > 0.1)))
2604
0
        {
2605
0
            qpVbv += stepSize;
2606
0
            accFrameBits = predictRowsSizeSum(curFrame, rce, qpVbv, encodedBitsSoFar);
2607
0
            abrOvershoot = (accFrameBits + m_totalBits - m_wantedBitsWindow) / totalBitsNeeded;
2608
0
        }
2609
2610
0
        while (qpVbv > qpMin
2611
0
               && (qpVbv > curEncData.m_rowStat[0].rowQp || m_singleFrameVbv)
2612
0
               && (((accFrameBits < rce->frameSizePlanned * 0.8f && qpVbv <= prevRowQp)
2613
0
                   || accFrameBits < (rce->bufferFill - m_bufferSize + m_bufferRate) * 1.1
2614
0
                   || (rce->vbvEndAdj && ((rce->bufferFill - accFrameBits) > (rce->targetFill * vbvEndBias))))
2615
0
                   && (!m_param->rc.bStrictCbr ? 1 : abrOvershoot < 0)))
2616
0
        {
2617
0
            qpVbv -= stepSize;
2618
0
            accFrameBits = predictRowsSizeSum(curFrame, rce, qpVbv, encodedBitsSoFar);
2619
0
            abrOvershoot = (accFrameBits + m_totalBits - m_wantedBitsWindow) / totalBitsNeeded;
2620
0
        }
2621
2622
0
        if (m_param->rc.bStrictCbr && m_param->totalFrames)
2623
0
        {
2624
0
            double timeDone = (double)(m_framesDone) / m_param->totalFrames;
2625
0
            while (qpVbv < qpMax && (qpVbv < rce->qpNoVbv + (m_param->rc.qpStep * timeDone)) &&
2626
0
                   (timeDone > 0.75 && abrOvershoot > 0))
2627
0
            {
2628
0
                qpVbv += stepSize;
2629
0
                accFrameBits = predictRowsSizeSum(curFrame, rce, qpVbv, encodedBitsSoFar);
2630
0
                abrOvershoot = (accFrameBits + m_totalBits - m_wantedBitsWindow) / totalBitsNeeded;
2631
0
            }
2632
0
            if (qpVbv > curEncData.m_rowStat[0].rowQp &&
2633
0
                abrOvershoot < -0.1 && timeDone > 0.5 && accFrameBits < rce->frameSizePlanned - rcTol)
2634
0
            {
2635
0
                qpVbv -= stepSize;
2636
0
                accFrameBits = predictRowsSizeSum(curFrame, rce, qpVbv, encodedBitsSoFar);
2637
0
            }
2638
0
        }
2639
2640
        /* avoid VBV underflow or MinCr violation */
2641
0
        while ((qpVbv < qpAbsoluteMax)
2642
0
               && ((rce->bufferFill - accFrameBits < m_bufferRate * maxFrameError) ||
2643
0
                   (rce->frameSizeMaximum - accFrameBits < rce->frameSizeMaximum * maxFrameError)))
2644
0
        {
2645
0
            qpVbv += stepSize;
2646
0
            accFrameBits = predictRowsSizeSum(curFrame, rce, qpVbv, encodedBitsSoFar);
2647
0
        }
2648
2649
0
        rce->frameSizeEstimated = accFrameBits;
2650
2651
        /* If the current row was large enough to cause a large QP jump, try re-encoding it. */
2652
0
        if (qpVbv > qpMax && prevRowQp < qpMax && canReencodeRow)
2653
0
        {
2654
            /* Bump QP to halfway in between... close enough. */
2655
0
            qpVbv = x265_clip3(prevRowQp + 1.0f, qpMax, (prevRowQp + qpVbv) * 0.5);
2656
0
            return -1;
2657
0
        }
2658
2659
0
        if (m_param->rc.rfConstantMin)
2660
0
        {
2661
0
            if (qpVbv < qpMin && prevRowQp > qpMin && canReencodeRow)
2662
0
            {
2663
0
                qpVbv = x265_clip3(qpMin, prevRowQp, (prevRowQp + qpVbv) * 0.5);
2664
0
                return -1;
2665
0
            }
2666
0
        }
2667
0
    }
2668
0
    else
2669
0
    {
2670
0
        int32_t encodedBitsSoFar = 0;
2671
0
        rce->frameSizeEstimated = predictRowsSizeSum(curFrame, rce, qpVbv, encodedBitsSoFar);
2672
2673
        /* Last-ditch attempt: if the last row of the frame underflowed the VBV,
2674
         * try again. */
2675
0
        if ((rce->frameSizeEstimated > (rce->bufferFill - m_bufferRate * maxFrameError) &&
2676
0
             qpVbv < qpMax && canReencodeRow))
2677
0
        {
2678
0
            qpVbv = qpMax;
2679
0
            return -1;
2680
0
        }
2681
0
    }
2682
0
    return 0;
2683
0
}
2684
2685
/* modify the bitrate curve from pass1 for one frame */
2686
double RateControl::getQScale(RateControlEntry *rce, double rateFactor)
2687
503
{
2688
503
    double q;
2689
2690
503
    if (m_param->rc.cuTree && !m_param->rc.hevcAq)
2691
0
    {
2692
        // Scale and units are obtained from rateNum and rateDenom for videos with fixed frame rates.
2693
0
        double timescale = (double)m_param->fpsDenom / (2 * m_param->fpsNum);
2694
0
        q = pow(BASE_FRAME_DURATION / CLIP_DURATION(2 * timescale), 1 - m_param->rc.qCompress);
2695
0
    }
2696
503
    else
2697
503
        q = pow(rce->blurredComplexity, 1 - m_param->rc.qCompress);
2698
2699
    // avoid NaN's in the Rceq
2700
503
    if (rce->coeffBits + rce->mvBits == 0)
2701
0
        q = m_lastQScaleFor[rce->sliceType];
2702
503
    else
2703
503
    {
2704
503
        m_lastRceq = q;
2705
503
        q /= rateFactor;
2706
503
    }
2707
2708
503
    return q;
2709
503
}
2710
2711
void RateControl::updatePredictor(Predictor *p, double q, double var, double bits)
2712
0
{
2713
0
    if (var < 10)
2714
0
        return;
2715
0
    const double range = 2;
2716
0
    double old_coeff = p->coeff / p->count;
2717
0
    double old_offset = p->offset / p->count;
2718
0
    double new_coeff = X265_MAX((bits * q - old_offset) / var, p->coeffMin );
2719
0
    double new_coeff_clipped = x265_clip3(old_coeff / range, old_coeff * range, new_coeff);
2720
0
    double new_offset = bits * q - new_coeff_clipped * var;
2721
0
    if (new_offset >= 0)
2722
0
        new_coeff = new_coeff_clipped;
2723
0
    else
2724
0
        new_offset = 0;
2725
0
    p->count  *= p->decay;
2726
0
    p->coeff  *= p->decay;
2727
0
    p->offset *= p->decay;
2728
0
    p->count++;
2729
0
    p->coeff  += new_coeff;
2730
0
    p->offset += new_offset;
2731
0
}
2732
2733
int RateControl::updateVbv(int64_t bits, RateControlEntry* rce)
2734
0
{
2735
0
    int predType = rce->sliceType;
2736
0
    int filler = 0;
2737
0
    double bufferBits;
2738
0
    predType = rce->sliceType == B_SLICE && rce->keptAsRef ? 3 : predType;
2739
0
    if (rce->lastSatd >= m_ncu && rce->encodeOrder >= m_lastPredictorReset)
2740
0
        updatePredictor(&m_pred[predType], x265_qp2qScale(rce->qpaRc), (double)rce->lastSatd, (double)bits);
2741
0
    if (!m_isVbv)
2742
0
        return 0;
2743
2744
0
    m_bufferFillFinal -= bits;
2745
2746
0
    if (m_bufferFillFinal < 0)
2747
0
        x265_log(m_param, X265_LOG_WARNING, "poc:%d, VBV underflow (%.0f bits)\n", rce->poc, m_bufferFillFinal);
2748
2749
0
    m_bufferFillFinal = X265_MAX(m_bufferFillFinal, 0);
2750
0
    m_bufferFillFinal += rce->bufferRate;
2751
0
    if (m_param->csvLogLevel >= 2)
2752
0
        m_unclippedBufferFillFinal = m_bufferFillFinal;
2753
2754
0
    if (m_param->rc.bStrictCbr)
2755
0
    {
2756
0
        if (m_bufferFillFinal > m_bufferSize)
2757
0
        {
2758
0
            filler = (int)(m_bufferFillFinal - m_bufferSize);
2759
0
            filler += FILLER_OVERHEAD * 8;
2760
0
        }
2761
0
        m_bufferFillFinal -= filler;
2762
0
        bufferBits = X265_MIN(bits + filler + m_bufferExcess, rce->bufferRate);
2763
0
        m_bufferExcess = X265_MAX(m_bufferExcess - bufferBits + bits + filler, 0);
2764
0
        m_bufferFillActual += bufferBits - bits - filler;
2765
0
    }
2766
0
    else
2767
0
    {
2768
0
        m_bufferFillFinal = X265_MIN(m_bufferFillFinal, m_bufferSize);
2769
0
        bufferBits = X265_MIN(bits + m_bufferExcess, rce->bufferRate);
2770
0
        m_bufferExcess = X265_MAX(m_bufferExcess - bufferBits + bits, 0);
2771
0
        m_bufferFillActual += bufferBits - bits;
2772
0
        m_bufferFillActual = X265_MIN(m_bufferFillActual, m_bufferSize);
2773
0
    }
2774
0
    return filler;
2775
0
}
2776
2777
/* After encoding one frame, update rate control state */
2778
int RateControl::rateControlEnd(Frame* curFrame, int64_t bits, RateControlEntry* rce, int *filler)
2779
698
{
2780
698
    int orderValue = m_startEndOrder.get();
2781
698
    int endOrdinal = (rce->encodeOrder + m_param->frameNumThreads) * 2 - 1;
2782
698
    while (orderValue < endOrdinal && !m_bTerminated)
2783
645
    {
2784
        /* no more frames are being encoded, so fake the start event if we would
2785
         * have blocked on it. Note that this does not enforce rateControlEnd()
2786
         * ordering during flush, but this has no impact on the outputs */
2787
645
        if (m_finalFrameCount && orderValue >= 2 * m_finalFrameCount)
2788
645
            break;
2789
0
        orderValue = m_startEndOrder.waitForChange(orderValue);
2790
0
    }
2791
2792
698
    FrameData& curEncData = *curFrame->m_encData;
2793
698
    int64_t actualBits = bits;
2794
698
    Slice *slice = curEncData.m_slice;
2795
698
    bool bEnableDistOffset = m_param->analysisMultiPassDistortion && m_param->rc.bStatRead;
2796
2797
698
    if (m_param->rc.aqMode || m_isVbv || m_param->bAQMotion || bEnableDistOffset)
2798
503
    {
2799
503
        if (m_isVbv && !(m_2pass && m_param->rc.rateControlMode == X265_RC_CRF))
2800
0
        {
2801
0
            double avgQpRc = 0;
2802
            /* determine avg QP decided by VBV rate control */
2803
0
            for (uint32_t i = 0; i < slice->m_sps->numCuInHeight; i++)
2804
0
                avgQpRc += curEncData.m_rowStat[i].sumQpRc;
2805
2806
0
            avgQpRc /= slice->m_sps->numCUsInFrame;
2807
0
            curEncData.m_avgQpRc = x265_clip3((double)m_param->rc.qpMin, (double)m_param->rc.qpMax, avgQpRc);
2808
0
            rce->qpaRc = curEncData.m_avgQpRc;
2809
0
        }
2810
2811
503
        if (m_param->rc.aqMode || m_param->bAQMotion || bEnableDistOffset)
2812
503
        {
2813
503
            double avgQpAq = 0;
2814
            /* determine actual avg encoded QP, after AQ/cutree/distortion adjustments */
2815
2.90k
            for (uint32_t i = 0; i < slice->m_sps->numCuInHeight; i++)
2816
2.39k
                avgQpAq += curEncData.m_rowStat[i].sumQpAq;
2817
2818
503
            avgQpAq /= (slice->m_sps->numCUsInFrame * m_param->num4x4Partitions);
2819
503
            curEncData.m_avgQpAq = avgQpAq;
2820
503
        }
2821
0
        else
2822
0
            curEncData.m_avgQpAq = curEncData.m_avgQpRc;
2823
503
    }
2824
2825
698
    if (m_isAbr)
2826
503
    {
2827
503
        if (m_param->rc.rateControlMode == X265_RC_ABR && !m_param->rc.bStatRead)
2828
0
            checkAndResetABR(rce, true);
2829
503
    }
2830
698
    if (m_param->rc.rateControlMode == X265_RC_CRF)
2831
503
    {
2832
503
        double crfVal, qpRef = curEncData.m_avgQpRc;
2833
503
        bool is2passCrfChange = false;
2834
503
        if (m_2pass)
2835
0
        {
2836
0
            if (fabs(curEncData.m_avgQpRc - rce->qpPrev) > 0.1)
2837
0
            {
2838
0
                qpRef = rce->qpPrev;
2839
0
                is2passCrfChange = true;
2840
0
            }
2841
0
        }
2842
503
        if (is2passCrfChange || fabs(qpRef - rce->qpNoVbv) > 0.5)
2843
0
        {
2844
0
            double crfFactor = rce->qRceq /x265_qp2qScale(qpRef);
2845
0
            double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
2846
0
            double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;
2847
0
            crfVal = x265_qScale2qp(pow(baseCplx, 1 - m_qCompress) / crfFactor) - mbtree_offset;
2848
0
        }
2849
503
        else
2850
503
            crfVal = rce->sliceType == I_SLICE ? m_param->rc.rfConstant - m_ipOffset : 
2851
503
            (rce->sliceType == B_SLICE ? m_param->rc.rfConstant + m_pbOffset : m_param->rc.rfConstant);
2852
2853
503
        curEncData.m_rateFactor = crfVal;
2854
503
    }
2855
2856
698
    if (m_isAbr && !m_isAbrReset)
2857
503
    {
2858
        /* amortize part of each I slice over the next several frames, up to
2859
         * keyint-max, to avoid over-compensating for the large I slice cost */
2860
503
        if (!m_param->rc.bStatWrite && !m_param->rc.bStatRead)
2861
503
        {
2862
503
            if (rce->sliceType == I_SLICE)
2863
503
            {
2864
                /* previous I still had a residual; roll it into the new loan */
2865
503
                if (m_residualFrames)
2866
0
                    bits += m_residualCost * m_residualFrames;
2867
503
                m_residualFrames = X265_MIN((int)rce->amortizeFrames, m_param->keyframeMax);
2868
503
                m_residualCost = (int)((bits * rce->amortizeFraction) / m_residualFrames);
2869
503
                bits -= m_residualCost * m_residualFrames;
2870
503
            }
2871
0
            else if (m_residualFrames)
2872
0
            {
2873
0
                bits += m_residualCost;
2874
0
                m_residualFrames--;
2875
0
            }
2876
503
        }
2877
503
        if (rce->sliceType != B_SLICE)
2878
503
        {
2879
            /* The factor 1.5 is to tune up the actual bits, otherwise the cplxrSum is scaled too low
2880
                * to improve short term compensation for next frame. */
2881
503
            m_cplxrSum += (bits * x265_qp2qScale(rce->qpaRc) / rce->qRceq) - (rce->rowCplxrSum);
2882
503
        }
2883
0
        else
2884
0
        {
2885
            /* Depends on the fact that B-frame's QP is an offset from the following P-frame's.
2886
                * Not perfectly accurate with B-refs, but good enough. */
2887
0
            m_cplxrSum += (bits * x265_qp2qScale(rce->qpaRc) / (rce->qRceq * fabs(m_param->rc.pbFactor))) - (rce->rowCplxrSum);
2888
0
        }
2889
503
        m_wantedBitsWindow += m_frameDuration * m_bitrate;
2890
503
        m_totalBits += bits - rce->rowTotalBits;
2891
503
        m_encodedBits += actualBits;
2892
503
        int pos = m_sliderPos - m_param->frameNumThreads;
2893
503
        if (pos >= 0)
2894
41
            m_encodedBitsWindow[pos % s_slidingWindowFrames] = actualBits;
2895
503
        if(rce->sliceType != I_SLICE)
2896
0
        {
2897
0
            int qp = int (rce->qpaRc + 0.5);
2898
0
            m_qpToEncodedBits[qp] =  m_qpToEncodedBits[qp] == 0 ? actualBits : (m_qpToEncodedBits[qp] + actualBits) * 0.5;
2899
0
        }
2900
503
        curFrame->m_rcData->wantedBitsWindow = m_wantedBitsWindow;
2901
503
        curFrame->m_rcData->cplxrSum = m_cplxrSum;
2902
503
        curFrame->m_rcData->totalBits = m_totalBits;
2903
503
        curFrame->m_rcData->encodedBits = m_encodedBits;
2904
503
    }
2905
2906
698
    if (m_2pass)
2907
0
    {
2908
0
        m_expectedBitsSum += qScale2bits(rce, x265_qp2qScale(rce->newQp));
2909
0
        m_totalBits += bits - rce->rowTotalBits;
2910
0
    }
2911
2912
698
    if (m_isVbv)
2913
0
    {
2914
0
        *filler = updateVbv(actualBits, rce);
2915
2916
0
        curFrame->m_rcData->bufferFillFinal = m_bufferFillFinal;
2917
0
        for (int i = 0; i < 4; i++)
2918
0
        {
2919
0
            curFrame->m_rcData->coeff[i] = m_pred[i].coeff;
2920
0
            curFrame->m_rcData->count[i] = m_pred[i].count;
2921
0
            curFrame->m_rcData->offset[i] = m_pred[i].offset;
2922
0
        }
2923
0
        if (m_param->bEmitHRDSEI)
2924
0
        {
2925
0
            const VUI *vui = &curEncData.m_slice->m_sps->vuiParameters;
2926
0
            const HRDInfo *hrd = &vui->hrdParameters;
2927
0
            const TimingInfo *time = &vui->timingInfo;
2928
0
            if (!curFrame->m_poc)
2929
0
            {
2930
                // first access unit initializes the HRD
2931
0
                rce->hrdTiming->cpbInitialAT = 0;
2932
0
                rce->hrdTiming->cpbRemovalTime = m_nominalRemovalTime = (double)m_bufPeriodSEI.m_initialCpbRemovalDelay / 90000;
2933
0
            }
2934
0
            else
2935
0
            {
2936
0
                rce->hrdTiming->cpbRemovalTime = m_nominalRemovalTime + (double)rce->picTimingSEI->m_auCpbRemovalDelay * time->numUnitsInTick / time->timeScale;
2937
0
                double cpbEarliestAT = rce->hrdTiming->cpbRemovalTime - (double)m_bufPeriodSEI.m_initialCpbRemovalDelay / 90000;
2938
0
                if (!curFrame->m_lowres.bKeyframe)
2939
0
                    cpbEarliestAT -= (double)m_bufPeriodSEI.m_initialCpbRemovalDelayOffset / 90000;
2940
2941
0
                rce->hrdTiming->cpbInitialAT = hrd->cbrFlag ? m_prevCpbFinalAT : X265_MAX(m_prevCpbFinalAT, cpbEarliestAT);
2942
0
            }
2943
0
            int filler_bits = *filler ? (*filler - START_CODE_OVERHEAD * 8)  : 0; 
2944
0
            uint32_t cpbsizeUnscale = hrd->cpbSizeValue << (hrd->cpbSizeScale + CPB_SHIFT);
2945
0
            rce->hrdTiming->cpbFinalAT = m_prevCpbFinalAT = rce->hrdTiming->cpbInitialAT + (actualBits + filler_bits)/ cpbsizeUnscale;
2946
0
            rce->hrdTiming->dpbOutputTime = (double)rce->picTimingSEI->m_picDpbOutputDelay * time->numUnitsInTick / time->timeScale + rce->hrdTiming->cpbRemovalTime;
2947
0
        }
2948
0
    }
2949
698
    rce->isActive = false;
2950
    // Allow rateControlStart of next frame only when rateControlEnd of previous frame is over
2951
698
    m_startEndOrder.incr();
2952
698
    return 0;
2953
698
}
2954
2955
/* called to write out the rate control frame stats info in multipass encodes */
2956
int RateControl::writeRateControlFrameStats(Frame* curFrame, RateControlEntry* rce)
2957
0
{
2958
0
    FrameData& curEncData = *curFrame->m_encData;    
2959
0
    int ncu = (m_param->rc.qgSize == 8) ? m_ncu * 4 : m_ncu;
2960
0
    char cType = rce->sliceType == I_SLICE ? (curFrame->m_lowres.sliceType == X265_TYPE_IDR ? 'I' : 'i')
2961
0
        : rce->sliceType == P_SLICE ? 'P'
2962
0
        : IS_REFERENCED(curFrame) ? 'B' : 'b';
2963
    
2964
0
    if (!curEncData.m_param->bMultiPassOptRPS)
2965
0
    {
2966
0
        if (fprintf(m_statFileOut,
2967
0
            "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f ;\n",
2968
0
            rce->poc, rce->encodeOrder,
2969
0
            cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,
2970
0
            rce->qpNoVbv, rce->qRceq,
2971
0
            curFrame->m_encData->m_frameStats.coeffBits,
2972
0
            curFrame->m_encData->m_frameStats.mvBits,
2973
0
            curFrame->m_encData->m_frameStats.miscBits,
2974
0
            curFrame->m_encData->m_frameStats.percent8x8Intra * m_ncu,
2975
0
            curFrame->m_encData->m_frameStats.percent8x8Inter * m_ncu,
2976
0
            curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu) < 0)
2977
0
            goto writeFailure;
2978
0
    }
2979
0
    else
2980
0
    {
2981
0
        RPS* rpsWriter = &curFrame->m_encData->m_slice->m_rps;
2982
0
        int i, num = rpsWriter->numberOfPictures;
2983
0
        char deltaPOC[128];
2984
0
        char bUsed[40];
2985
0
        memset(deltaPOC, 0, sizeof(deltaPOC));
2986
0
        memset(bUsed, 0, sizeof(bUsed));
2987
0
        sprintf(deltaPOC, "deltapoc:~");
2988
0
        sprintf(bUsed, "bused:~");
2989
2990
0
        for (i = 0; i < num; i++)
2991
0
        {
2992
0
            sprintf(deltaPOC, "%s%d~", deltaPOC, rpsWriter->deltaPOC[i]);
2993
0
            sprintf(bUsed, "%s%d~", bUsed, rpsWriter->bUsed[i]);
2994
0
        }
2995
2996
0
        if (fprintf(m_statFileOut,
2997
0
            "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f nump:%d numnegp:%d numposp:%d %s %s ;\n",
2998
0
            rce->poc, rce->encodeOrder,
2999
0
            cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,
3000
0
            rce->qpNoVbv, rce->qRceq,
3001
0
            curFrame->m_encData->m_frameStats.coeffBits,
3002
0
            curFrame->m_encData->m_frameStats.mvBits,
3003
0
            curFrame->m_encData->m_frameStats.miscBits,
3004
0
            curFrame->m_encData->m_frameStats.percent8x8Intra * m_ncu,
3005
0
            curFrame->m_encData->m_frameStats.percent8x8Inter * m_ncu,
3006
0
            curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu,
3007
0
            rpsWriter->numberOfPictures,
3008
0
            rpsWriter->numberOfNegativePictures,
3009
0
            rpsWriter->numberOfPositivePictures,
3010
0
            deltaPOC, bUsed) < 0)
3011
0
            goto writeFailure;
3012
0
    }
3013
    /* Don't re-write the data in multi-pass mode. */
3014
0
    if (m_param->rc.cuTree && IS_REFERENCED(curFrame) && !m_param->rc.bStatRead)
3015
0
    {
3016
0
        uint8_t sliceType = (uint8_t)rce->sliceType;
3017
0
        primitives.fix8Pack(m_cuTreeStats.qpBuffer[0], curFrame->m_lowres.qpCuTreeOffset, ncu);
3018
0
        if (fwrite(&sliceType, 1, 1, m_cutreeStatFileOut) < 1)
3019
0
            goto writeFailure;
3020
0
        if (fwrite(m_cuTreeStats.qpBuffer[0], sizeof(uint16_t), ncu, m_cutreeStatFileOut) < (size_t)ncu)
3021
0
            goto writeFailure;
3022
0
    }
3023
0
    return 0;
3024
3025
0
    writeFailure:
3026
0
    x265_log(m_param, X265_LOG_ERROR, "RatecontrolEnd: stats file write failure\n");
3027
0
    return 1;
3028
0
}
3029
#if defined(_MSC_VER)
3030
#pragma warning(disable: 4996) // POSIX function names are just fine, thank you
3031
#endif
3032
3033
/* called when the encoder is flushing, and thus the final frame count is
3034
 * unambiguously known */
3035
void RateControl::setFinalFrameCount(int count)
3036
3.87k
{
3037
3.87k
    m_finalFrameCount = count;
3038
    /* unblock waiting threads */
3039
3.87k
    m_startEndOrder.poke();
3040
3.87k
}
3041
3042
/* called when the encoder is closing, and no more frames will be output.
3043
 * all blocked functions must finish so the frame encoder threads can be
3044
 * closed */
3045
void RateControl::terminate()
3046
698
{
3047
698
    m_bTerminated = true;
3048
    /* unblock waiting threads */
3049
698
    m_startEndOrder.poke();
3050
698
}
3051
3052
void RateControl::destroy()
3053
698
{
3054
698
    const char *fileName = m_param->rc.statFileName;
3055
698
    if (!fileName)
3056
698
        fileName = s_defaultStatFileName;
3057
3058
698
    if (m_statFileOut)
3059
0
    {
3060
0
        fclose(m_statFileOut);
3061
0
        char *tmpFileName = strcatFilename(fileName, ".temp");
3062
0
        int bError = 1;
3063
0
        if (tmpFileName)
3064
0
        {
3065
0
            x265_unlink(fileName);
3066
0
            bError = x265_rename(tmpFileName, fileName);
3067
0
        }
3068
0
        if (bError)
3069
0
        {
3070
0
            x265_log_file(m_param, X265_LOG_ERROR, "failed to rename output stats file to \"%s\"\n", fileName);
3071
0
        }
3072
0
        X265_FREE(tmpFileName);
3073
0
    }
3074
3075
698
    if (m_cutreeStatFileOut)
3076
0
    {
3077
0
        fclose(m_cutreeStatFileOut);
3078
0
        char *tmpFileName = strcatFilename(fileName, ".cutree.temp");
3079
0
        char *newFileName = strcatFilename(fileName, ".cutree");
3080
0
        int bError = 1;
3081
0
        if (tmpFileName && newFileName)
3082
0
        {
3083
0
            x265_unlink(newFileName);
3084
0
            bError = x265_rename(tmpFileName, newFileName);
3085
0
        }
3086
0
        if (bError)
3087
0
        {
3088
0
            x265_log_file(m_param, X265_LOG_ERROR, "failed to rename cutree output stats file to \"%s\"\n", newFileName);
3089
0
        }
3090
0
        X265_FREE(tmpFileName);
3091
0
        X265_FREE(newFileName);
3092
0
    }
3093
3094
698
    if (m_cutreeStatFileIn)
3095
0
        fclose(m_cutreeStatFileIn);
3096
3097
698
    X265_FREE(m_rce2Pass);
3098
698
    X265_FREE(m_encOrder);
3099
2.09k
    for (int i = 0; i < 2; i++)
3100
1.39k
        X265_FREE(m_cuTreeStats.qpBuffer[i]);
3101
    
3102
698
    if (m_relativeComplexity)
3103
0
        X265_FREE(m_relativeComplexity);
3104
3105
698
}
3106
3107
void RateControl::splitdeltaPOC(char deltapoc[], RateControlEntry *rce)
3108
0
{
3109
0
    int idx = 0, length = 0;
3110
0
    char tmpStr[128];
3111
0
    char* src = deltapoc;
3112
0
    char* buf = strstr(src, "~");
3113
0
    while (buf)
3114
0
    {
3115
0
        memset(tmpStr, 0, sizeof(tmpStr));
3116
0
        length = (int)(buf - src);
3117
0
        if (length != 0)
3118
0
        {
3119
0
            strncpy(tmpStr, src, length);
3120
0
            rce->rpsData.deltaPOC[idx] = atoi(tmpStr);
3121
0
            idx++;
3122
0
            if (idx == rce->rpsData.numberOfPictures)
3123
0
                break;
3124
0
        }
3125
0
        src += (length + 1);
3126
0
        buf = strstr(src, "~");
3127
0
    }
3128
0
}
3129
3130
void RateControl::splitbUsed(char bused[], RateControlEntry *rce)
3131
0
{
3132
0
    int idx = 0, length = 0;
3133
0
    char tmpStr[128];
3134
0
    char* src = bused;
3135
0
    char* buf = strstr(src, "~");
3136
0
    while (buf)
3137
0
    {
3138
0
        memset(tmpStr, 0, sizeof(tmpStr));
3139
0
        length = (int)(buf - src);
3140
0
        if (length != 0)
3141
0
        {
3142
0
            strncpy(tmpStr, src, length);
3143
0
            rce->rpsData.bUsed[idx] = atoi(tmpStr) > 0;
3144
0
            idx++;
3145
0
            if (idx == rce->rpsData.numberOfPictures)
3146
0
                break;
3147
0
        }
3148
0
        src += (length + 1);
3149
0
        buf = strstr(src, "~");
3150
0
    }
3151
0
}
3152
3153
double RateControl::scenecutAwareQp(Frame* curFrame, double q)
3154
0
{
3155
0
    uint32_t maxWindowSize = uint32_t((m_param->scenecutWindow / 1000.0) * (m_param->fpsNum / m_param->fpsDenom) + 0.5);
3156
0
    uint32_t windowSize = maxWindowSize / 3;
3157
0
    int lastScenecut = m_top->m_rateControl->m_lastScenecut;
3158
0
    int lastIFrame = m_top->m_rateControl->m_lastScenecutAwareIFrame;
3159
0
    double maxQpDelta = x265_qp2qScale(double(m_param->maxQpDelta));
3160
0
    double iSliceDelta = x265_qp2qScale(double(I_SLICE_DELTA));
3161
0
    double sliceTypeDelta = SLICE_TYPE_DELTA * maxQpDelta;
3162
0
    double window2Delta = WINDOW2_DELTA * maxQpDelta;
3163
0
    double window3Delta = WINDOW3_DELTA * maxQpDelta;
3164
3165
0
    bool isFrameInsideWindow = curFrame->m_poc > lastScenecut && curFrame->m_poc <= (lastScenecut + int(maxWindowSize));
3166
3167
0
    if (isFrameInsideWindow && IS_X265_TYPE_I(curFrame->m_lowres.sliceType))
3168
0
    {
3169
0
        m_top->m_rateControl->m_lastScenecutAwareIFrame = curFrame->m_poc;
3170
0
    }
3171
0
    else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_P))
3172
0
    {
3173
0
        if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut + int(maxWindowSize))
3174
0
            && curFrame->m_poc > lastIFrame))
3175
0
        {
3176
0
            q += maxQpDelta - sliceTypeDelta;
3177
0
            if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
3178
0
                q -= window2Delta;
3179
0
            else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))
3180
0
                q -= window3Delta;
3181
0
        }
3182
0
    }
3183
0
    else if (isFrameInsideWindow && IS_X265_TYPE_B(curFrame->m_lowres.sliceType))
3184
0
    {
3185
0
        if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut + int(maxWindowSize))
3186
0
            && curFrame->m_poc > lastIFrame))
3187
0
        {
3188
0
            q += maxQpDelta;
3189
0
            if (curFrame->m_lowres.sliceType == X265_TYPE_B)
3190
0
                q += sliceTypeDelta;
3191
0
            if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
3192
0
                q -= window2Delta;
3193
0
            else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))
3194
0
                q -= window3Delta;
3195
0
        }
3196
0
    }
3197
0
    if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) && curFrame->m_lowres.bScenecut)
3198
0
        q = q - iSliceDelta;
3199
0
    return q;
3200
0
}